2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "cave/object/caveobjectrandomfill.hpp"
27 #include "cave/elementproperties.hpp"
29 #include <glib/gi18n.h>
31 #include "fileops/bdcffhelper.hpp"
32 #include "cave/caverendered.hpp"
33 #include "misc/printf.hpp"
34 #include "misc/util.hpp"
36 std::string
CaveRandomFill::get_bdcff() const {
37 const char *type
= c64_random
? "RandomFillC64" : "RandomFill";
41 f
<< seed
[0] << seed
[1] << seed
[2] << seed
[3] << seed
[4];
43 if (random_fill_probability_1
!= 0)
44 f
<< random_fill_1
<< random_fill_probability_1
;
45 if (random_fill_probability_2
!= 0)
46 f
<< random_fill_2
<< random_fill_probability_2
;
47 if (random_fill_probability_3
!= 0)
48 f
<< random_fill_3
<< random_fill_probability_3
;
49 if (random_fill_probability_4
!= 0)
50 f
<< random_fill_4
<< random_fill_probability_4
;
51 if (replace_only
!= O_NONE
)
57 CaveRandomFill
*CaveRandomFill::clone_from_bdcff(const std::string
&name
, std::istream
&is
) const {
60 GdElementEnum initial_fill
, replace_only
;
61 GdElementEnum random_fill
[4] = {O_DIRT
, O_DIRT
, O_DIRT
, O_DIRT
};
62 int random_prob
[4] = {0, 0, 0, 0};
66 if (!(is
>> p1
>> p2
>> seed
[0] >> seed
[1] >> seed
[2] >> seed
[3] >> seed
[4] >> initial_fill
))
69 /* now come fill element&probability pairs. read as we find two words */
70 while (j
< 4 && (is
>> s1
>> s2
)) {
71 std::istringstream
is1(s1
), is2(s2
);
72 // read two strings - they must be fill & prob
73 if (!(is1
>> random_fill
[j
]))
75 if (!(is2
>> random_prob
[j
]))
81 /* if we could read s1, but not read s2, now s1 contains a "replace only" element. */
82 /* so process it later. */
83 /* if j=4, no more reads took place, but there might be still one string at the end. */
84 /* so read if j=4 is the case. */
87 /* ok now try if we have a replace only element. */
89 std::istringstream
is(s1
);
90 // one string left - must be a replace only element
91 if (!(is
>> replace_only
))
94 replace_only
= O_NONE
;
96 CaveRandomFill
*r
= new CaveRandomFill(p1
, p2
);
97 r
->set_replace_only(replace_only
);
98 r
->set_seed(seed
[0], seed
[1], seed
[2], seed
[3], seed
[4]);
99 r
->set_random_fill(initial_fill
, random_fill
[0], random_fill
[1], random_fill
[2], random_fill
[3]);
100 r
->set_random_prob(random_prob
[0], random_prob
[1], random_prob
[2], random_prob
[3]);
101 r
->set_c64_random(gd_str_ascii_caseequal(name
, "RandomFillC64"));
106 CaveRandomFill
*CaveRandomFill::clone() const {
107 return new CaveRandomFill(*this);
111 CaveRandomFill::CaveRandomFill(Coordinate _p1
, Coordinate _p2
)
112 : CaveRectangular(GD_RANDOM_FILL
, _p1
, _p2
),
113 replace_only(O_NONE
),
115 initial_fill
= O_SPACE
;
116 random_fill_1
= O_DIRT
;
117 random_fill_probability_1
= 0;
118 random_fill_2
= O_DIRT
;
119 random_fill_probability_2
= 0;
120 random_fill_3
= O_DIRT
;
121 random_fill_probability_3
= 0;
122 random_fill_4
= O_DIRT
;
123 random_fill_probability_4
= 0;
124 for (int j
= 0; j
< 5; j
++)
128 void CaveRandomFill::set_random_fill(GdElementEnum initial
, GdElementEnum e1
, GdElementEnum e2
, GdElementEnum e3
, GdElementEnum e4
) {
129 initial_fill
= initial
;
136 void CaveRandomFill::set_random_prob(int i1
, int i2
, int i3
, int i4
) {
137 random_fill_probability_1
= i1
;
138 random_fill_probability_2
= i2
;
139 random_fill_probability_3
= i3
;
140 random_fill_probability_4
= i4
;
143 void CaveRandomFill::set_seed(int s1
, int s2
, int s3
, int s4
, int s5
) {
151 void CaveRandomFill::draw(CaveRendered
&cave
) const {
152 /* -1 means that it should be different every time played. */
154 if (seed
[cave
.rendered_on
] == -1)
155 s
= cave
.random
.rand_int();
157 s
= seed
[cave
.rendered_on
];
159 RandomGenerator rand
;
161 /* for c64 random, use the 2*8 lsb. */
162 C64RandomGenerator c64rand
;
165 /* change coordinates if not in correct order */
175 for (int y
= y1
; y
<= y2
; y
++)
176 for (int x
= x1
; x
<= x2
; x
++) {
179 if (c64_random
) /* use c64 random generator */
180 randm
= c64rand
.random();
181 else /* use the much better glib random generator */
182 randm
= rand
.rand_int_range(0, 256);
184 GdElementEnum element
= initial_fill
;
185 if (randm
< random_fill_probability_1
)
186 element
= random_fill_1
;
187 if (randm
< random_fill_probability_2
)
188 element
= random_fill_2
;
189 if (randm
< random_fill_probability_3
)
190 element
= random_fill_3
;
191 if (randm
< random_fill_probability_4
)
192 element
= random_fill_4
;
193 if (replace_only
== O_NONE
|| cave
.map(x
, y
) == replace_only
)
194 cave
.store_rc(x
, y
, element
, this);
198 PropertyDescription
const CaveRandomFill::descriptor
[] = {
199 {"", GD_TAB
, 0, N_("Random Fill")},
200 {"", GD_TYPE_BOOLEAN_LEVELS
, 0, N_("Levels"), GetterBase::create_new(&CaveRandomFill::seen_on
), N_("Levels on which this object is visible.")},
201 {"", GD_TYPE_COORDINATE
, 0, N_("Start corner"), GetterBase::create_new(&CaveRandomFill::p1
), N_("Specifies one of the corners of the object."), 0, 127},
202 {"", GD_TYPE_COORDINATE
, 0, N_("End corner"), GetterBase::create_new(&CaveRandomFill::p2
), N_("Specifies one of the corners of the object."), 0, 127},
203 {"", GD_TYPE_ELEMENT
, 0, N_("Replace only"), GetterBase::create_new(&CaveRandomFill::replace_only
), N_("If this setting is set to 'no element' but another one, only that will be replaced when drawing the fill, and other elements will not be overwritten. Otherwise the whole block is filled.")},
204 {"", GD_TAB
, 0, N_("Generator")},
205 {"", GD_TYPE_BOOLEAN
, 0, N_("C64 random"), GetterBase::create_new(&CaveRandomFill::c64_random
), N_("Controls if the C64 random generator is used for the fill, or a more advanced one. By using the C64 generator, you can recreate the patterns of the original game.")},
206 {"", GD_TYPE_INT_LEVELS
, 0, N_("Random seed"), GetterBase::create_new(&CaveRandomFill::seed
), N_("Random seed value controls the predictable random number generator, which fills the cave initially. If set to -1, cave is totally random every time it is played."), -1, GD_CAVE_SEED_MAX
},
207 {"", GD_TYPE_ELEMENT
, 0, N_("Initial fill"), GetterBase::create_new(&CaveRandomFill::initial_fill
), 0},
208 {"", GD_TYPE_INT
, 0, N_("Probability 1"), GetterBase::create_new(&CaveRandomFill::random_fill_probability_1
), 0, 0, 255},
209 {"", GD_TYPE_ELEMENT
, 0, N_("Random fill 1"), GetterBase::create_new(&CaveRandomFill::random_fill_1
), 0},
210 {"", GD_TYPE_INT
, 0, N_("Probability 2"), GetterBase::create_new(&CaveRandomFill::random_fill_probability_2
), 0, 0, 255},
211 {"", GD_TYPE_ELEMENT
, 0, N_("Random fill 2"), GetterBase::create_new(&CaveRandomFill::random_fill_2
), 0},
212 {"", GD_TYPE_INT
, 0, N_("Probability 3"), GetterBase::create_new(&CaveRandomFill::random_fill_probability_3
), 0, 0, 255},
213 {"", GD_TYPE_ELEMENT
, 0, N_("Random fill 3"), GetterBase::create_new(&CaveRandomFill::random_fill_3
), 0},
214 {"", GD_TYPE_INT
, 0, N_("Probability 4"), GetterBase::create_new(&CaveRandomFill::random_fill_probability_4
), 0, 0, 255},
215 {"", GD_TYPE_ELEMENT
, 0, N_("Random fill 4"), GetterBase::create_new(&CaveRandomFill::random_fill_4
), 0},
219 PropertyDescription
const *CaveRandomFill::get_description_array() const {
223 std::string
CaveRandomFill::get_description_markup() const {
224 if (replace_only
== O_NONE
)
225 return SPrintf(_("Random fill from %d,%d to %d,%d"))
226 % p1
.x
% p1
.y
% p2
.x
% p2
.y
;
228 return SPrintf(_("Random fill from %d,%d to %d,%d, replacing %ms"))
229 % p1
.x
% p1
.y
% p2
.x
% p2
.y
% visible_name_lowercase(replace_only
);
232 GdElementEnum
CaveRandomFill::get_characteristic_element() const {