… | |
… | |
140 | { |
140 | { |
141 | read_uuid (); |
141 | read_uuid (); |
142 | } |
142 | } |
143 | |
143 | |
144 | /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ |
144 | /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ |
145 | static int |
145 | static bool |
146 | compare_ob_value_lists_one (const object *wants, const object *has) |
146 | compare_ob_value_lists_one (const object *wants, const object *has) |
147 | { |
147 | { |
148 | key_value *wants_field; |
148 | key_value *wants_field; |
149 | |
149 | |
150 | /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both |
150 | /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both |
… | |
… | |
158 | key_value *has_field; |
158 | key_value *has_field; |
159 | |
159 | |
160 | /* Look for a field in has with the same key. */ |
160 | /* Look for a field in has with the same key. */ |
161 | has_field = get_ob_key_link (has, wants_field->key); |
161 | has_field = get_ob_key_link (has, wants_field->key); |
162 | |
162 | |
163 | if (has_field == NULL) |
163 | if (!has_field) |
164 | { |
|
|
165 | /* No field with that name. */ |
164 | return 0; /* No field with that name. */ |
166 | return FALSE; |
|
|
167 | } |
|
|
168 | |
165 | |
169 | /* Found the matching field. */ |
166 | /* Found the matching field. */ |
170 | if (has_field->value != wants_field->value) |
167 | if (has_field->value != wants_field->value) |
171 | { |
|
|
172 | /* Values don't match, so this half of the comparison is false. */ |
168 | return 0; /* Values don't match, so this half of the comparison is false. */ |
173 | return FALSE; |
|
|
174 | } |
|
|
175 | |
169 | |
176 | /* If we get here, we found a match. Now for the next field in wants. */ |
170 | /* If we get here, we found a match. Now for the next field in wants. */ |
177 | } |
171 | } |
178 | |
172 | |
179 | /* If we get here, every field in wants has a matching field in has. */ |
173 | /* If we get here, every field in wants has a matching field in has. */ |
180 | return TRUE; |
174 | return 1; |
181 | } |
175 | } |
182 | |
176 | |
183 | /* Returns TRUE if ob1 has the same key_values as ob2. */ |
177 | /* Returns TRUE if ob1 has the same key_values as ob2. */ |
184 | static int |
178 | static bool |
185 | compare_ob_value_lists (const object *ob1, const object *ob2) |
179 | compare_ob_value_lists (const object *ob1, const object *ob2) |
186 | { |
180 | { |
187 | /* However, there may be fields in has which aren't partnered in wants, |
181 | /* However, there may be fields in has which aren't partnered in wants, |
188 | * so we need to run the comparison *twice*. :( |
182 | * so we need to run the comparison *twice*. :( |
189 | */ |
183 | */ |
… | |
… | |
210 | || ob1->speed != ob2->speed |
204 | || ob1->speed != ob2->speed |
211 | || ob1->value != ob2->value |
205 | || ob1->value != ob2->value |
212 | || ob1->name != ob2->name) |
206 | || ob1->name != ob2->name) |
213 | return 0; |
207 | return 0; |
214 | |
208 | |
215 | //TODO: this ain't working well, use nicer and correct overflow check |
209 | /* Do not merge objects if nrof would overflow. First part checks |
216 | /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that |
210 | * for unsigned overflow (2c), second part checks wether the result |
217 | * value could not be stored in a sint32 (which unfortunately sometimes is |
211 | * would fit into a 32 bit signed int, which is often used to hold |
218 | * used to store nrof). |
212 | * nrof values. |
219 | */ |
213 | */ |
220 | if (ob1->nrof + ob2->nrof >= 1UL << 31) |
214 | if (~ob1->nrof < ob2->nrof || ob1->nrof + ob2->nrof > (1UL << 31)) |
221 | return 0; |
215 | return 0; |
222 | |
216 | |
223 | /* If the objects have been identified, set the BEEN_APPLIED flag. |
217 | /* If the objects have been identified, set the BEEN_APPLIED flag. |
224 | * This is to the comparison of the flags below will be OK. We |
218 | * This is to the comparison of the flags below will be OK. We |
225 | * just can't ignore the been applied or identified flags, as they |
219 | * just can't ignore the been applied or identified flags, as they |
226 | * are not equal - just if it has been identified, the been_applied |
220 | * are not equal - just if it has been identified, the been_applied |
227 | * flags lose any meaning. |
221 | * flags lose any meaning. |
228 | */ |
222 | */ |
229 | if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) |
223 | if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) |
… | |
… | |
257 | || ob1->move_off != ob2->move_off |
251 | || ob1->move_off != ob2->move_off |
258 | || ob1->move_slow != ob2->move_slow |
252 | || ob1->move_slow != ob2->move_slow |
259 | || ob1->move_slow_penalty != ob2->move_slow_penalty) |
253 | || ob1->move_slow_penalty != ob2->move_slow_penalty) |
260 | return 0; |
254 | return 0; |
261 | |
255 | |
262 | /* This is really a spellbook check - really, we should |
256 | /* This is really a spellbook check - we should in general |
263 | * check all objects in the inventory. |
257 | * not merge objects with real inventories, as splitting them |
|
|
258 | * is hard. |
264 | */ |
259 | */ |
265 | if (ob1->inv || ob2->inv) |
260 | if (ob1->inv || ob2->inv) |
266 | { |
261 | { |
267 | if (!(ob1->inv && ob2->inv)) |
262 | if (!(ob1->inv && ob2->inv)) |
268 | return 0; /* inventories differ in length */ |
263 | return 0; /* inventories differ in length */ |
269 | |
264 | |
270 | if (ob1->inv->below || ob2->inv->below) |
265 | if (ob1->inv->below || ob2->inv->below) |
271 | return 0; /* more than one object in inv */ |
266 | return 0; /* more than one object in inv */ |
272 | |
267 | |
273 | if (!object::can_merge (ob1->inv, ob2->inv)) |
268 | if (!object::can_merge (ob1->inv, ob2->inv)) |
274 | return 0; /* inventory objexts differ */ |
269 | return 0; /* inventory objects differ */ |
275 | |
270 | |
276 | /* inventory ok - still need to check rest of this object to see |
271 | /* inventory ok - still need to check rest of this object to see |
277 | * if it is valid. |
272 | * if it is valid. |
278 | */ |
273 | */ |
279 | } |
274 | } |
… | |
… | |
298 | if (ob1->level != ob2->level) |
293 | if (ob1->level != ob2->level) |
299 | return 0; |
294 | return 0; |
300 | break; |
295 | break; |
301 | } |
296 | } |
302 | |
297 | |
303 | if (ob1->key_values != NULL || ob2->key_values != NULL) |
298 | if (ob1->key_values || ob2->key_values) |
304 | { |
299 | { |
305 | /* At least one of these has key_values. */ |
300 | /* At least one of these has key_values. */ |
306 | if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) |
301 | if ((!ob1->key_values) != (!ob2->key_values)) |
307 | /* One has fields, but the other one doesn't. */ |
302 | /* One has fields, but the other one doesn't. */ |
308 | return 0; |
303 | return 0; |
309 | else if (!compare_ob_value_lists (ob1, ob2)) |
304 | else if (!compare_ob_value_lists (ob1, ob2)) |
310 | return 0; |
305 | return 0; |
311 | } |
306 | } |
… | |
… | |
1484 | } |
1479 | } |
1485 | |
1480 | |
1486 | object * |
1481 | object * |
1487 | object::insert_at (object *where, object *originator, int flags) |
1482 | object::insert_at (object *where, object *originator, int flags) |
1488 | { |
1483 | { |
|
|
1484 | if (where->env) |
|
|
1485 | return where->env->insert (this); |
|
|
1486 | else |
1489 | return where->map->insert (this, where->x, where->y, originator, flags); |
1487 | return where->map->insert (this, where->x, where->y, originator, flags); |
1490 | } |
1488 | } |
1491 | |
1489 | |
1492 | /* |
1490 | /* |
1493 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
1491 | * get_split_ob(ob,nr) splits up ob into two parts. The part which |
1494 | * is returned contains nr objects, and the remaining parts contains |
1492 | * is returned contains nr objects, and the remaining parts contains |