1 | /* |
1 | /* |
2 | * static char *rcsid_object_c = |
2 | * static char *rcsid_object_c = |
3 | * "$Id: object.C,v 1.12 2006/09/03 14:33:47 elmex Exp $"; |
3 | * "$Id: object.C,v 1.21 2006/09/09 21:48:28 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
37 | #endif /* win32 */ |
37 | #endif /* win32 */ |
38 | #include <object.h> |
38 | #include <object.h> |
39 | #include <funcpoint.h> |
39 | #include <funcpoint.h> |
40 | #include <skills.h> |
40 | #include <skills.h> |
41 | #include <loader.h> |
41 | #include <loader.h> |
42 | #ifdef MEMORY_DEBUG |
|
|
43 | int nroffreeobjects = 0; |
|
|
44 | int nrofallocobjects = 0; |
42 | int nrofallocobjects = 0; |
45 | #undef OBJ_EXPAND |
|
|
46 | #define OBJ_EXPAND 1 |
|
|
47 | #else |
|
|
48 | object objarray[STARTMAX]; /* All objects, allocated this way at first */ |
|
|
49 | int nroffreeobjects = STARTMAX; /* How many OBs allocated and free (free) */ |
|
|
50 | int nrofallocobjects = STARTMAX; /* How many OBs allocated (free + used) */ |
|
|
51 | #endif |
|
|
52 | |
43 | |
53 | object *objects; /* Pointer to the list of used objects */ |
44 | object *objects; /* Pointer to the list of used objects */ |
54 | object *free_objects; /* Pointer to the list of unused objects */ |
|
|
55 | object *active_objects; /* List of active objects that need to be processed */ |
45 | object *active_objects; /* List of active objects that need to be processed */ |
56 | |
46 | |
57 | short freearr_x[SIZEOFFREE]= |
47 | short freearr_x[SIZEOFFREE]= |
58 | {0,0,1,1,1,0,-1,-1,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1, |
48 | {0,0,1,1,1,0,-1,-1,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1, |
59 | 0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1}; |
49 | 0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1}; |
… | |
… | |
64 | {0,9,10,13,14,17,18,21,22,25,26,27,30,31,32,33,36,37,39,39,42,43,44,45, |
54 | {0,9,10,13,14,17,18,21,22,25,26,27,30,31,32,33,36,37,39,39,42,43,44,45, |
65 | 48,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49}; |
55 | 48,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49}; |
66 | int freedir[SIZEOFFREE]= { |
56 | int freedir[SIZEOFFREE]= { |
67 | 0,1,2,3,4,5,6,7,8,1,2,2,2,3,4,4,4,5,6,6,6,7,8,8,8, |
57 | 0,1,2,3,4,5,6,7,8,1,2,2,2,3,4,4,4,5,6,6,6,7,8,8,8, |
68 | 1,2,2,2,2,2,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8}; |
58 | 1,2,2,2,2,2,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8}; |
69 | |
|
|
70 | |
59 | |
71 | /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ |
60 | /* Returns TRUE if every key_values in wants has a partner with the same value in has. */ |
72 | static int compare_ob_value_lists_one(const object * wants, const object * has) { |
61 | static int compare_ob_value_lists_one(const object * wants, const object * has) { |
73 | key_value * wants_field; |
62 | key_value * wants_field; |
74 | |
63 | |
… | |
… | |
121 | * |
110 | * |
122 | * Improvements made with merge: Better checking on potion, and also |
111 | * Improvements made with merge: Better checking on potion, and also |
123 | * check weight |
112 | * check weight |
124 | */ |
113 | */ |
125 | |
114 | |
126 | int CAN_MERGE(object *ob1, object *ob2) { |
115 | bool |
127 | |
116 | object::can_merge (object *ob1, object *ob2) |
|
|
117 | { |
128 | /* A couple quicksanity checks */ |
118 | /* A couple quicksanity checks */ |
129 | if ((ob1 == ob2) || (ob1->type != ob2->type)) return 0; |
119 | if ((ob1 == ob2) || (ob1->type != ob2->type)) |
130 | |
|
|
131 | if (ob1->speed != ob2->speed) return 0; |
|
|
132 | /* Note sure why the following is the case - either the object has to |
|
|
133 | * be animated or have a very low speed. Is this an attempted monster |
|
|
134 | * check? |
|
|
135 | */ |
|
|
136 | if (!QUERY_FLAG(ob1,FLAG_ANIMATE) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED) |
|
|
137 | return 0; |
120 | return 0; |
138 | |
121 | |
|
|
122 | if (ob1->speed != ob2->speed) |
|
|
123 | return 0; |
|
|
124 | |
139 | /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that |
125 | /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that |
140 | * value could not be stored in a sint32 (which unfortunately sometimes is |
126 | * value could not be stored in a sint32 (which unfortunately sometimes is |
141 | * used to store nrof). |
127 | * used to store nrof). |
142 | */ |
128 | */ |
143 | if (ob1->nrof+ob2->nrof >= 1UL<<31) |
129 | if (ob1->nrof + ob2->nrof >= 1UL << 31) |
144 | return 0; |
130 | return 0; |
145 | |
131 | |
|
|
132 | /* If the objects have been identified, set the BEEN_APPLIED flag. |
|
|
133 | * This is to the comparison of the flags below will be OK. We |
|
|
134 | * just can't ignore the been applied or identified flags, as they |
|
|
135 | * are not equal - just if it has been identified, the been_applied |
|
|
136 | * flags lose any meaning. |
|
|
137 | */ |
|
|
138 | if (QUERY_FLAG (ob1, FLAG_IDENTIFIED)) |
|
|
139 | SET_FLAG (ob1, FLAG_BEEN_APPLIED); |
|
|
140 | |
|
|
141 | if (QUERY_FLAG (ob2, FLAG_IDENTIFIED)) |
|
|
142 | SET_FLAG (ob2, FLAG_BEEN_APPLIED); |
|
|
143 | |
|
|
144 | |
|
|
145 | /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something |
|
|
146 | * being locked in inventory should prevent merging. |
|
|
147 | * 0x4 in flags3 is CLIENT_SENT |
|
|
148 | */ |
|
|
149 | if ((ob1->arch != ob2->arch) || |
|
|
150 | (ob1->flags[0] != ob2->flags[0]) || |
|
|
151 | (ob1->flags[1] != ob2->flags[1]) || |
|
|
152 | ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~0x400000)) || |
|
|
153 | ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) || |
|
|
154 | (ob1->name != ob2->name) || |
|
|
155 | (ob1->title != ob2->title) || |
|
|
156 | (ob1->msg != ob2->msg) || |
|
|
157 | (ob1->weight != ob2->weight) || |
|
|
158 | (memcmp (&ob1->resist, &ob2->resist, sizeof (ob1->resist)) != 0) || |
|
|
159 | (memcmp (&ob1->stats, &ob2->stats, sizeof (ob1->stats)) != 0) || |
|
|
160 | (ob1->attacktype != ob2->attacktype) || |
|
|
161 | (ob1->magic != ob2->magic) || |
|
|
162 | (ob1->slaying != ob2->slaying) || |
|
|
163 | (ob1->skill != ob2->skill) || |
|
|
164 | (ob1->value != ob2->value) || |
|
|
165 | (ob1->animation_id != ob2->animation_id) || |
|
|
166 | (ob1->client_type != ob2->client_type) || |
|
|
167 | (ob1->materialname != ob2->materialname) || |
|
|
168 | (ob1->lore != ob2->lore) || |
|
|
169 | (ob1->subtype != ob2->subtype) || |
|
|
170 | (ob1->move_type != ob2->move_type) || |
|
|
171 | (ob1->move_block != ob2->move_block) || |
|
|
172 | (ob1->move_allow != ob2->move_allow) || |
|
|
173 | (ob1->move_on != ob2->move_on) || |
|
|
174 | (ob1->move_off != ob2->move_off) || |
|
|
175 | (ob1->move_slow != ob2->move_slow) || |
|
|
176 | (ob1->move_slow_penalty != ob2->move_slow_penalty)) |
|
|
177 | return 0; |
|
|
178 | |
146 | /* This is really a spellbook check - really, we should |
179 | /* This is really a spellbook check - really, we should |
147 | * check all objects in the inventory. |
180 | * check all objects in the inventory. |
148 | */ |
181 | */ |
149 | if (ob1->inv || ob2->inv) { |
182 | if (ob1->inv || ob2->inv) |
|
|
183 | { |
150 | /* if one object has inventory but the other doesn't, not equiv */ |
184 | /* if one object has inventory but the other doesn't, not equiv */ |
151 | if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) return 0; |
185 | if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) |
|
|
186 | return 0; |
152 | |
187 | |
153 | /* Now check to see if the two inventory objects could merge */ |
188 | /* Now check to see if the two inventory objects could merge */ |
154 | if (!CAN_MERGE(ob1->inv, ob2->inv)) return 0; |
189 | if (!CAN_MERGE (ob1->inv, ob2->inv)) |
|
|
190 | return 0; |
155 | |
191 | |
156 | /* inventory ok - still need to check rest of this object to see |
192 | /* inventory ok - still need to check rest of this object to see |
157 | * if it is valid. |
193 | * if it is valid. |
158 | */ |
194 | */ |
159 | } |
195 | } |
160 | |
196 | |
161 | /* If the objects have been identified, set the BEEN_APPLIED flag. |
|
|
162 | * This is to the comparison of the flags below will be OK. We |
|
|
163 | * just can't ignore the been applied or identified flags, as they |
|
|
164 | * are not equal - just if it has been identified, the been_applied |
|
|
165 | * flags lose any meaning. |
|
|
166 | */ |
|
|
167 | if (QUERY_FLAG(ob1, FLAG_IDENTIFIED)) |
|
|
168 | SET_FLAG(ob1, FLAG_BEEN_APPLIED); |
|
|
169 | |
|
|
170 | if (QUERY_FLAG(ob2, FLAG_IDENTIFIED)) |
|
|
171 | SET_FLAG(ob2, FLAG_BEEN_APPLIED); |
|
|
172 | |
|
|
173 | |
|
|
174 | /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something |
|
|
175 | * being locked in inventory should prevent merging. |
|
|
176 | * 0x4 in flags3 is CLIENT_SENT |
|
|
177 | */ |
|
|
178 | if ((ob1->arch != ob2->arch) || |
|
|
179 | (ob1->flags[0] != ob2->flags[0]) || |
|
|
180 | (ob1->flags[1] != ob2->flags[1]) || |
|
|
181 | ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~ 0x400000)) || |
|
|
182 | ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) || |
|
|
183 | (ob1->name != ob2->name) || |
|
|
184 | (ob1->title != ob2->title) || |
|
|
185 | (ob1->msg != ob2->msg) || |
|
|
186 | (ob1->weight != ob2->weight) || |
|
|
187 | (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist))!=0) || |
|
|
188 | (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats))!=0) || |
|
|
189 | (ob1->attacktype != ob2->attacktype) || |
|
|
190 | (ob1->magic != ob2->magic) || |
|
|
191 | (ob1->slaying != ob2->slaying) || |
|
|
192 | (ob1->skill != ob2->skill) || |
|
|
193 | (ob1->value != ob2->value) || |
|
|
194 | (ob1->animation_id != ob2->animation_id) || |
|
|
195 | (ob1->client_type != ob2->client_type) || |
|
|
196 | (ob1->materialname != ob2->materialname) || |
|
|
197 | (ob1->lore != ob2->lore) || |
|
|
198 | (ob1->subtype != ob2->subtype) || |
|
|
199 | (ob1->move_type != ob2->move_type) || |
|
|
200 | (ob1->move_block != ob2->move_block) || |
|
|
201 | (ob1->move_allow != ob2->move_allow) || |
|
|
202 | (ob1->move_on != ob2->move_on) || |
|
|
203 | (ob1->move_off != ob2->move_off) || |
|
|
204 | (ob1->move_slow != ob2->move_slow) || |
|
|
205 | (ob1->move_slow_penalty != ob2->move_slow_penalty) |
|
|
206 | ) |
|
|
207 | return 0; |
|
|
208 | |
|
|
209 | /* Don't merge objects that are applied. With the new 'body' code, |
197 | /* Don't merge objects that are applied. With the new 'body' code, |
210 | * it is possible for most any character to have more than one of |
198 | * it is possible for most any character to have more than one of |
211 | * some items equipped, and we don't want those to merge. |
199 | * some items equipped, and we don't want those to merge. |
212 | */ |
200 | */ |
213 | if (QUERY_FLAG(ob1, FLAG_APPLIED) || QUERY_FLAG(ob2, FLAG_APPLIED)) |
201 | if (QUERY_FLAG (ob1, FLAG_APPLIED) || QUERY_FLAG (ob2, FLAG_APPLIED)) |
214 | return 0; |
202 | return 0; |
215 | |
203 | |
|
|
204 | /* Note sure why the following is the case - either the object has to |
|
|
205 | * be animated or have a very low speed. Is this an attempted monster |
|
|
206 | * check? |
|
|
207 | */ |
|
|
208 | if (!QUERY_FLAG (ob1, FLAG_ANIMATE) |
|
|
209 | && FABS ((ob1)->speed) > MIN_ACTIVE_SPEED) |
|
|
210 | return 0; |
|
|
211 | |
216 | switch (ob1->type) { |
212 | switch (ob1->type) |
|
|
213 | { |
217 | case SCROLL: |
214 | case SCROLL: |
218 | if (ob1->level != ob2->level) return 0; |
215 | if (ob1->level != ob2->level) |
|
|
216 | return 0; |
219 | break; |
217 | break; |
220 | |
|
|
221 | } |
218 | } |
|
|
219 | |
222 | if (ob1->key_values != NULL || ob2->key_values != NULL) { |
220 | if (ob1->key_values != NULL || ob2->key_values != NULL) |
|
|
221 | { |
223 | /* At least one of these has key_values. */ |
222 | /* At least one of these has key_values. */ |
224 | if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) { |
223 | if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) |
225 | /* One has fields, but the other one doesn't. */ |
224 | /* One has fields, but the other one doesn't. */ |
226 | return 0; |
225 | return 0; |
227 | } else if (!compare_ob_value_lists(ob1, ob2)) { |
226 | else if (!compare_ob_value_lists (ob1, ob2)) |
228 | return 0; |
227 | return 0; |
229 | } |
228 | } |
|
|
229 | |
|
|
230 | //TODO: generate an event or call into perl for additional checks |
|
|
231 | if (ob1->self || ob2->self) |
230 | } |
232 | { |
|
|
233 | ob1->optimise (); |
|
|
234 | ob2->optimise (); |
231 | |
235 | |
|
|
236 | if (ob1->self || ob2->self) |
|
|
237 | return 0; |
|
|
238 | } |
|
|
239 | |
232 | /* Everything passes, must be OK. */ |
240 | /* Everything passes, must be OK. */ |
233 | return 1; |
241 | return 1; |
234 | } |
242 | } |
235 | |
|
|
236 | /* |
243 | /* |
237 | * sum_weight() is a recursive function which calculates the weight |
244 | * sum_weight() is a recursive function which calculates the weight |
238 | * an object is carrying. It goes through in figures out how much |
245 | * an object is carrying. It goes through in figures out how much |
239 | * containers are carrying, and sums it up. |
246 | * containers are carrying, and sums it up. |
240 | */ |
247 | */ |
… | |
… | |
388 | break; |
395 | break; |
389 | |
396 | |
390 | return op; |
397 | return op; |
391 | } |
398 | } |
392 | |
399 | |
393 | void free_all_object_data(void) { |
400 | void free_all_object_data () |
394 | #ifdef MEMORY_DEBUG |
401 | { |
395 | object *op, *next; |
402 | LOG (llevDebug, "%d allocated objects\n", nrofallocobjects); |
396 | |
|
|
397 | for (op=free_objects; op!=NULL; ) { |
|
|
398 | next=op->next; |
|
|
399 | free(op); |
|
|
400 | nrofallocobjects--; |
|
|
401 | nroffreeobjects--; |
|
|
402 | op=next; |
|
|
403 | } |
|
|
404 | #endif |
|
|
405 | LOG(llevDebug,"%d allocated objects, %d free objects, STARMAX=%d\n", |
|
|
406 | nrofallocobjects, nroffreeobjects,STARTMAX); |
|
|
407 | } |
403 | } |
408 | |
404 | |
409 | /* |
405 | /* |
410 | * Returns the object which this object marks as being the owner. |
406 | * Returns the object which this object marks as being the owner. |
411 | * A id-scheme is used to avoid pointing to objects which have been |
407 | * A id-scheme is used to avoid pointing to objects which have been |
… | |
… | |
438 | op->owner->refcount--; |
434 | op->owner->refcount--; |
439 | |
435 | |
440 | op->owner = NULL; |
436 | op->owner = NULL; |
441 | op->ownercount = 0; |
437 | op->ownercount = 0; |
442 | } |
438 | } |
443 | |
|
|
444 | |
|
|
445 | |
439 | |
446 | /* |
440 | /* |
447 | * Sets the owner and sets the skill and exp pointers to owner's current |
441 | * Sets the owner and sets the skill and exp pointers to owner's current |
448 | * skill and experience objects. |
442 | * skill and experience objects. |
449 | */ |
443 | */ |
… | |
… | |
496 | } |
490 | } |
497 | set_owner(op, owner); |
491 | set_owner(op, owner); |
498 | |
492 | |
499 | } |
493 | } |
500 | |
494 | |
501 | /* |
|
|
502 | * Resets vital variables in an object |
|
|
503 | */ |
|
|
504 | |
|
|
505 | void reset_object(object *op) { |
|
|
506 | |
|
|
507 | op->name = NULL; |
|
|
508 | op->name_pl = NULL; |
|
|
509 | op->title = NULL; |
|
|
510 | op->race = NULL; |
|
|
511 | op->slaying = NULL; |
|
|
512 | op->skill = NULL; |
|
|
513 | op->msg = NULL; |
|
|
514 | op->materialname = NULL; |
|
|
515 | op->lore = NULL; |
|
|
516 | clear_object(op); |
|
|
517 | } |
|
|
518 | |
|
|
519 | /* Zero the key_values on op, decrementing the shared-string |
495 | /* Zero the key_values on op, decrementing the shared-string |
520 | * refcounts and freeing the links. |
496 | * refcounts and freeing the links. |
521 | */ |
497 | */ |
522 | static void free_key_values(object * op) |
498 | static void free_key_values(object * op) |
523 | { |
499 | { |
… | |
… | |
529 | } |
505 | } |
530 | |
506 | |
531 | op->key_values = 0; |
507 | op->key_values = 0; |
532 | } |
508 | } |
533 | |
509 | |
|
|
510 | void object::clear () |
|
|
511 | { |
|
|
512 | attachable_base::clear (); |
|
|
513 | |
|
|
514 | free_key_values (this); |
|
|
515 | |
|
|
516 | name = 0; |
|
|
517 | name_pl = 0; |
|
|
518 | title = 0; |
|
|
519 | race = 0; |
|
|
520 | slaying = 0; |
|
|
521 | skill = 0; |
|
|
522 | msg = 0; |
|
|
523 | lore = 0; |
|
|
524 | custom_name = 0; |
|
|
525 | materialname = 0; |
|
|
526 | |
|
|
527 | memset (static_cast<object_pod *>(this), 0, sizeof (object_pod)); |
|
|
528 | |
|
|
529 | SET_FLAG (this, FLAG_REMOVED); |
|
|
530 | } |
|
|
531 | |
|
|
532 | void object::clone (object *destination) |
|
|
533 | { |
|
|
534 | *(object_copy *)destination = *(object_copy *)this; |
|
|
535 | *(object_pod *)destination = *(object_pod *)this; |
|
|
536 | |
|
|
537 | if (self || cb) |
|
|
538 | INVOKE_OBJECT (CLONE, this, ARG_OBJECT (destination)); |
|
|
539 | } |
|
|
540 | |
534 | /* |
541 | /* |
535 | * clear_object() frees everything allocated by an object, and also |
542 | * clear_object() frees everything allocated by an object, and also |
536 | * clears all variables and flags to default settings. |
543 | * clears all variables and flags to default settings. |
537 | */ |
544 | */ |
538 | |
545 | |
539 | void clear_object(object *op) |
546 | void clear_object (object *op) |
540 | { |
547 | { |
541 | op->clear (); |
548 | op->clear (); |
542 | |
|
|
543 | free_key_values (op); |
|
|
544 | |
|
|
545 | /* the memset will clear all these values for us, but we need |
|
|
546 | * to reduce the refcount on them. |
|
|
547 | */ |
|
|
548 | op->name = 0; |
|
|
549 | op->name_pl = 0; |
|
|
550 | op->title = 0; |
|
|
551 | op->race = 0; |
|
|
552 | op->slaying = 0; |
|
|
553 | op->skill = 0; |
|
|
554 | op->msg = 0; |
|
|
555 | op->lore = 0; |
|
|
556 | op->materialname = 0; |
|
|
557 | |
|
|
558 | memset((void*)op, 0, sizeof (object_special)); |
|
|
559 | /* Below here, we clear things that are not done by the memset, |
|
|
560 | * or set default values that are not zero. |
|
|
561 | */ |
|
|
562 | /* This is more or less true */ |
|
|
563 | SET_FLAG (op, FLAG_REMOVED); |
|
|
564 | |
549 | |
565 | op->contr = NULL; |
550 | op->contr = NULL; |
566 | op->below = NULL; |
551 | op->below = NULL; |
567 | op->above = NULL; |
552 | op->above = NULL; |
568 | op->inv = NULL; |
553 | op->inv = NULL; |
… | |
… | |
574 | op->refcount=0; |
559 | op->refcount=0; |
575 | op->active_next = NULL; |
560 | op->active_next = NULL; |
576 | op->active_prev = NULL; |
561 | op->active_prev = NULL; |
577 | /* What is not cleared is next, prev, and count */ |
562 | /* What is not cleared is next, prev, and count */ |
578 | |
563 | |
579 | op->expmul=1.0; |
564 | op->expmul = 1.0; |
580 | op->face = blank_face; |
565 | op->face = blank_face; |
581 | op->attacked_by_count = (tag_t) -1; |
566 | op->attacked_by_count = -1; |
582 | |
567 | |
583 | if (settings.casting_time) |
568 | if (settings.casting_time) |
584 | op->casting_time = -1; |
569 | op->casting_time = -1; |
585 | } |
570 | } |
586 | |
571 | |
… | |
… | |
591 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
576 | * data that is malloc'd needs to be re-malloc/copied. Otherwise, |
592 | * if the first object is freed, the pointers in the new object |
577 | * if the first object is freed, the pointers in the new object |
593 | * will point at garbage. |
578 | * will point at garbage. |
594 | */ |
579 | */ |
595 | |
580 | |
596 | void copy_object(object *op2, object *op) |
581 | void copy_object (object *op2, object *op) |
597 | { |
582 | { |
598 | int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED); |
583 | bool is_freed = QUERY_FLAG (op, FLAG_FREED); |
|
|
584 | bool is_removed = QUERY_FLAG (op, FLAG_REMOVED); |
599 | |
585 | |
600 | op->clear (); |
|
|
601 | |
|
|
602 | free_key_values (op); |
|
|
603 | |
|
|
604 | *(object_special *)op = *(object_special *)op2; |
|
|
605 | op2->clone (op); |
586 | op2->clone (op); |
606 | |
587 | |
607 | if (is_freed) SET_FLAG(op,FLAG_FREED); |
588 | if (is_freed) SET_FLAG (op, FLAG_FREED); |
608 | if (is_removed) SET_FLAG(op,FLAG_REMOVED); |
589 | if (is_removed) SET_FLAG (op, FLAG_REMOVED); |
609 | |
590 | |
610 | if (op2->speed < 0) |
591 | if (op2->speed < 0) |
611 | op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0; |
592 | op->speed_left = op2->speed_left - RANDOM() % 200 / 100.0; |
612 | |
593 | |
613 | /* Copy over key_values, if any. */ |
594 | /* Copy over key_values, if any. */ |
614 | if (op2->key_values != NULL) { |
595 | if (op2->key_values != NULL) |
|
|
596 | { |
615 | key_value *tail = NULL; |
597 | key_value *tail = NULL; |
616 | key_value *i; |
598 | key_value *i; |
617 | |
599 | |
618 | op->key_values = NULL; |
600 | op->key_values = NULL; |
619 | |
601 | |
… | |
… | |
634 | else |
616 | else |
635 | { |
617 | { |
636 | tail->next = new_link; |
618 | tail->next = new_link; |
637 | tail = new_link; |
619 | tail = new_link; |
638 | } |
620 | } |
639 | } |
621 | } |
640 | } |
622 | } |
641 | |
623 | |
642 | update_ob_speed (op); |
624 | update_ob_speed (op); |
643 | } |
|
|
644 | |
|
|
645 | /* |
|
|
646 | * expand_objects() allocates more objects for the list of unused objects. |
|
|
647 | * It is called from get_object() if the unused list is empty. |
|
|
648 | */ |
|
|
649 | |
|
|
650 | void expand_objects(void) { |
|
|
651 | int i; |
|
|
652 | object *obj; |
|
|
653 | obj = new object [OBJ_EXPAND]; |
|
|
654 | |
|
|
655 | free_objects=obj; |
|
|
656 | obj[0].prev=NULL; |
|
|
657 | obj[0].next= &obj[1], |
|
|
658 | SET_FLAG(&(obj[0]), FLAG_REMOVED); |
|
|
659 | SET_FLAG(&(obj[0]), FLAG_FREED); |
|
|
660 | |
|
|
661 | for(i=1;i<OBJ_EXPAND-1;i++) { |
|
|
662 | obj[i].next= &obj[i+1], |
|
|
663 | obj[i].prev= &obj[i-1], |
|
|
664 | SET_FLAG(&(obj[i]), FLAG_REMOVED); |
|
|
665 | SET_FLAG(&(obj[i]), FLAG_FREED); |
|
|
666 | } |
|
|
667 | obj[OBJ_EXPAND-1].prev= &obj[OBJ_EXPAND-2], |
|
|
668 | obj[OBJ_EXPAND-1].next=NULL, |
|
|
669 | SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_REMOVED); |
|
|
670 | SET_FLAG(&(obj[OBJ_EXPAND-1]), FLAG_FREED); |
|
|
671 | |
|
|
672 | nrofallocobjects += OBJ_EXPAND; |
|
|
673 | nroffreeobjects += OBJ_EXPAND; |
|
|
674 | } |
|
|
675 | |
|
|
676 | /* |
|
|
677 | * get_object() grabs an object from the list of unused objects, makes |
|
|
678 | * sure it is initialised, and returns it. |
|
|
679 | * If there are no free objects, expand_objects() is called to get more. |
|
|
680 | */ |
|
|
681 | |
|
|
682 | object *get_object(void) |
|
|
683 | { |
|
|
684 | object *op; |
|
|
685 | |
|
|
686 | if(free_objects==NULL) |
|
|
687 | expand_objects(); |
|
|
688 | |
|
|
689 | op = free_objects; |
|
|
690 | |
|
|
691 | if (!QUERY_FLAG (op, FLAG_FREED)) |
|
|
692 | LOG(llevError,"Fatal: Getting busy object.\n"); |
|
|
693 | |
|
|
694 | free_objects = op->next; |
|
|
695 | |
|
|
696 | if (free_objects != NULL) |
|
|
697 | free_objects->prev = NULL; |
|
|
698 | |
|
|
699 | op->count= ++ob_count; |
|
|
700 | op->name=NULL; |
|
|
701 | op->name_pl=NULL; |
|
|
702 | op->title=NULL; |
|
|
703 | op->race=NULL; |
|
|
704 | op->slaying=NULL; |
|
|
705 | op->skill = NULL; |
|
|
706 | op->lore=NULL; |
|
|
707 | op->msg=NULL; |
|
|
708 | op->materialname=NULL; |
|
|
709 | op->next=objects; |
|
|
710 | op->prev=NULL; |
|
|
711 | op->active_next = NULL; |
|
|
712 | op->active_prev = NULL; |
|
|
713 | if(objects!=NULL) |
|
|
714 | objects->prev=op; |
|
|
715 | objects=op; |
|
|
716 | clear_object(op); |
|
|
717 | SET_FLAG(op,FLAG_REMOVED); |
|
|
718 | nroffreeobjects--; |
|
|
719 | return op; |
|
|
720 | } |
625 | } |
721 | |
626 | |
722 | /* |
627 | /* |
723 | * If an object with the IS_TURNABLE() flag needs to be turned due |
628 | * If an object with the IS_TURNABLE() flag needs to be turned due |
724 | * to the closest player being on the other side, this function can |
629 | * to the closest player being on the other side, this function can |
… | |
… | |
888 | update_now=1; |
793 | update_now=1; |
889 | |
794 | |
890 | if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE)) |
795 | if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE)) |
891 | update_now=1; |
796 | update_now=1; |
892 | |
797 | |
|
|
798 | if (op->type == SAFE_GROUND && !(flags & P_SAFE)) |
|
|
799 | update_now=1; |
|
|
800 | |
893 | if ((move_on | op->move_on) != move_on) update_now=1; |
801 | if ((move_on | op->move_on) != move_on) update_now=1; |
|
|
802 | |
894 | if ((move_off | op->move_off) != move_off) update_now=1; |
803 | if ((move_off | op->move_off) != move_off) update_now=1; |
|
|
804 | |
895 | /* This isn't perfect, but I don't expect a lot of objects to |
805 | /* This isn't perfect, but I don't expect a lot of objects to |
896 | * to have move_allow right now. |
806 | * to have move_allow right now. |
897 | */ |
807 | */ |
898 | if (((move_block | op->move_block) & ~op->move_allow) != move_block) |
808 | if (((move_block | op->move_block) & ~op->move_allow) != move_block) |
899 | update_now=1; |
809 | update_now=1; |
|
|
810 | |
900 | if ((move_slow | op->move_slow) != move_slow) update_now=1; |
811 | if ((move_slow | op->move_slow) != move_slow) |
|
|
812 | update_now=1; |
901 | } |
813 | } |
902 | /* if the object is being removed, we can't make intelligent |
814 | /* if the object is being removed, we can't make intelligent |
903 | * decisions, because remove_ob can't really pass the object |
815 | * decisions, because remove_ob can't really pass the object |
904 | * that is being removed. |
816 | * that is being removed. |
905 | */ |
817 | */ |
… | |
… | |
919 | |
831 | |
920 | if(op->more!=NULL) |
832 | if(op->more!=NULL) |
921 | update_object(op->more, action); |
833 | update_object(op->more, action); |
922 | } |
834 | } |
923 | |
835 | |
|
|
836 | static std::vector<object *> mortals; |
|
|
837 | |
|
|
838 | void object::free_mortals () |
|
|
839 | { |
|
|
840 | for (std::vector<object *>::iterator i = mortals.begin (); i != mortals.end (); ++i) |
|
|
841 | delete *i; |
|
|
842 | |
|
|
843 | mortals.clear (); |
|
|
844 | } |
|
|
845 | |
|
|
846 | object::object () |
|
|
847 | { |
|
|
848 | count = ++ob_count; |
|
|
849 | |
|
|
850 | prev = 0; |
|
|
851 | next = objects; |
|
|
852 | |
|
|
853 | if (objects) |
|
|
854 | objects->prev = this; |
|
|
855 | |
|
|
856 | objects = this; |
|
|
857 | |
|
|
858 | SET_FLAG (this, FLAG_REMOVED); |
|
|
859 | |
|
|
860 | expmul = 1.0; |
|
|
861 | face = blank_face; |
|
|
862 | attacked_by_count = -1; |
|
|
863 | } |
|
|
864 | |
|
|
865 | object::~object () |
|
|
866 | { |
|
|
867 | } |
|
|
868 | |
|
|
869 | object *object::create () |
|
|
870 | { |
|
|
871 | return new object; |
|
|
872 | } |
924 | |
873 | |
925 | /* |
874 | /* |
926 | * free_object() frees everything allocated by an object, removes |
875 | * free_object() frees everything allocated by an object, removes |
927 | * it from the list of used objects, and puts it on the list of |
876 | * it from the list of used objects, and puts it on the list of |
928 | * free objects. The IS_FREED() flag is set in the object. |
877 | * free objects. The IS_FREED() flag is set in the object. |
… | |
… | |
930 | * this function to succeed. |
879 | * this function to succeed. |
931 | * |
880 | * |
932 | * If free_inventory is set, free inventory as well. Else drop items in |
881 | * If free_inventory is set, free inventory as well. Else drop items in |
933 | * inventory to the ground. |
882 | * inventory to the ground. |
934 | */ |
883 | */ |
935 | |
884 | void |
936 | void free_object(object *ob) { |
885 | object::free (bool free_inventory) |
937 | free_object2(ob, 0); |
886 | { |
938 | } |
|
|
939 | void free_object2(object *ob, int free_inventory) { |
|
|
940 | object *tmp,*op; |
|
|
941 | |
|
|
942 | ob->clear (); |
|
|
943 | |
|
|
944 | if (!QUERY_FLAG(ob,FLAG_REMOVED)) { |
887 | if (!QUERY_FLAG (this, FLAG_REMOVED)) |
|
|
888 | { |
945 | LOG(llevDebug,"Free object called with non removed object\n"); |
889 | LOG (llevDebug, "Free object called with non removed object\n"); |
946 | dump_object(ob); |
890 | dump_object (this); |
947 | #ifdef MANY_CORES |
891 | #ifdef MANY_CORES |
948 | abort(); |
892 | abort (); |
949 | #endif |
893 | #endif |
950 | } |
894 | } |
|
|
895 | |
951 | if(QUERY_FLAG(ob,FLAG_FRIENDLY)) { |
896 | if (QUERY_FLAG (this, FLAG_FRIENDLY)) |
|
|
897 | { |
952 | LOG(llevMonster,"Warning: tried to free friendly object.\n"); |
898 | LOG (llevMonster, "Warning: tried to free friendly object.\n"); |
953 | remove_friendly_object(ob); |
899 | remove_friendly_object (this); |
954 | } |
900 | } |
|
|
901 | |
955 | if(QUERY_FLAG(ob,FLAG_FREED)) { |
902 | if (QUERY_FLAG (this, FLAG_FREED)) |
|
|
903 | { |
956 | dump_object(ob); |
904 | dump_object (this); |
957 | LOG(llevError,"Trying to free freed object.\n%s\n",errmsg); |
905 | LOG (llevError, "Trying to free freed object.\n%s\n", errmsg); |
958 | return; |
906 | return; |
|
|
907 | } |
|
|
908 | |
|
|
909 | if (more) |
959 | } |
910 | { |
960 | if(ob->more!=NULL) { |
911 | more->free (free_inventory); |
961 | free_object2(ob->more, free_inventory); |
912 | more = 0; |
962 | ob->more=NULL; |
913 | } |
|
|
914 | |
|
|
915 | if (inv) |
963 | } |
916 | { |
964 | if (ob->inv) { |
|
|
965 | /* Only if the space blocks everything do we not process - |
917 | /* Only if the space blocks everything do we not process - |
966 | * if some form of movemnt is allowed, let objects |
918 | * if some form of movement is allowed, let objects |
967 | * drop on that space. |
919 | * drop on that space. |
968 | */ |
920 | */ |
969 | if (free_inventory || ob->map==NULL || ob->map->in_memory!=MAP_IN_MEMORY || |
921 | if (free_inventory || !map |
|
|
922 | || map->in_memory != MAP_IN_MEMORY |
970 | (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL)) |
923 | || (GET_MAP_MOVE_BLOCK (map, x, y) == MOVE_ALL)) |
971 | { |
924 | { |
972 | op=ob->inv; |
925 | object *op = inv; |
973 | while(op!=NULL) { |
926 | |
974 | tmp=op->below; |
927 | while (op) |
|
|
928 | { |
|
|
929 | object *tmp = op->below; |
975 | remove_ob(op); |
930 | remove_ob (op); |
976 | free_object2(op, free_inventory); |
931 | op->free (free_inventory); |
977 | op=tmp; |
932 | op = tmp; |
978 | } |
|
|
979 | } |
|
|
980 | else { /* Put objects in inventory onto this space */ |
|
|
981 | op=ob->inv; |
|
|
982 | while(op!=NULL) { |
|
|
983 | tmp=op->below; |
|
|
984 | remove_ob(op); |
|
|
985 | if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) || |
|
|
986 | op->type==RUNE || op->type==TRAP || QUERY_FLAG(op,FLAG_IS_A_TEMPLATE)) |
|
|
987 | free_object(op); |
|
|
988 | else { |
|
|
989 | op->x=ob->x; |
|
|
990 | op->y=ob->y; |
|
|
991 | insert_ob_in_map(op,ob->map,NULL,0); /* Insert in same map as the envir */ |
|
|
992 | } |
|
|
993 | op=tmp; |
|
|
994 | } |
|
|
995 | } |
|
|
996 | } |
933 | } |
|
|
934 | } |
|
|
935 | else |
|
|
936 | { /* Put objects in inventory onto this space */ |
|
|
937 | object *op = inv; |
|
|
938 | |
|
|
939 | while (op) |
|
|
940 | { |
|
|
941 | object *tmp = op->below; |
|
|
942 | remove_ob (op); |
|
|
943 | |
|
|
944 | if (QUERY_FLAG (op, FLAG_STARTEQUIP) |
|
|
945 | || QUERY_FLAG (op, FLAG_NO_DROP) || op->type == RUNE |
|
|
946 | || op->type == TRAP || QUERY_FLAG (op, FLAG_IS_A_TEMPLATE)) |
|
|
947 | free_object (op); |
|
|
948 | else |
|
|
949 | { |
|
|
950 | op->x = x; |
|
|
951 | op->y = y; |
|
|
952 | insert_ob_in_map (op, map, 0, 0); /* Insert in same map as the envir */ |
|
|
953 | } |
|
|
954 | |
|
|
955 | op = tmp; |
|
|
956 | } |
|
|
957 | } |
|
|
958 | } |
|
|
959 | |
997 | /* Remove object from the active list */ |
960 | /* Remove object from the active list */ |
998 | ob->speed = 0; |
961 | speed = 0; |
999 | update_ob_speed(ob); |
962 | update_ob_speed (this); |
1000 | |
963 | |
1001 | SET_FLAG(ob, FLAG_FREED); |
964 | SET_FLAG (this, FLAG_FREED); |
1002 | ob->count = 0; |
|
|
1003 | |
965 | |
1004 | /* Remove this object from the list of used objects */ |
966 | mortalise (); |
1005 | if(ob->prev==NULL) { |
|
|
1006 | objects=ob->next; |
|
|
1007 | if(objects!=NULL) |
|
|
1008 | objects->prev=NULL; |
|
|
1009 | } |
|
|
1010 | else { |
|
|
1011 | ob->prev->next=ob->next; |
|
|
1012 | if(ob->next!=NULL) |
|
|
1013 | ob->next->prev=ob->prev; |
|
|
1014 | } |
|
|
1015 | |
|
|
1016 | ob->name = 0; |
|
|
1017 | ob->name_pl = 0; |
|
|
1018 | ob->title = 0; |
|
|
1019 | ob->race = 0; |
|
|
1020 | ob->slaying = 0; |
|
|
1021 | ob->skill = 0; |
|
|
1022 | ob->lore = 0; |
|
|
1023 | ob->msg = 0; |
|
|
1024 | ob->materialname = 0; |
|
|
1025 | |
|
|
1026 | free_key_values (ob); |
|
|
1027 | |
|
|
1028 | /* Now link it with the free_objects list: */ |
|
|
1029 | ob->prev=NULL; |
|
|
1030 | ob->next=free_objects; |
|
|
1031 | |
|
|
1032 | if(free_objects!=NULL) |
|
|
1033 | free_objects->prev=ob; |
|
|
1034 | |
|
|
1035 | free_objects=ob; |
|
|
1036 | nroffreeobjects++; |
|
|
1037 | } |
967 | } |
1038 | |
968 | |
1039 | /* |
969 | void |
1040 | * count_free() returns the number of objects on the list of free objects. |
970 | object::mortalise () |
1041 | */ |
|
|
1042 | |
|
|
1043 | int count_free () |
|
|
1044 | { |
971 | { |
1045 | int i=0; |
972 | count = 0; |
1046 | object *tmp=free_objects; |
|
|
1047 | while(tmp!=NULL) |
|
|
1048 | tmp=tmp->next, i++; |
|
|
1049 | return i; |
|
|
1050 | } |
|
|
1051 | |
973 | |
1052 | /* |
974 | /* Remove this object from the list of used objects */ |
1053 | * count_used() returns the number of objects on the list of used objects. |
975 | if (prev) prev->next = next; |
1054 | */ |
976 | if (next) next->prev = prev; |
|
|
977 | if (this == objects) objects = next; |
1055 | |
978 | |
1056 | int count_used() |
979 | free_key_values (this); |
1057 | { |
|
|
1058 | int i=0; |
|
|
1059 | object *tmp=objects; |
|
|
1060 | while(tmp!=NULL) |
|
|
1061 | tmp=tmp->next, i++; |
|
|
1062 | return i; |
|
|
1063 | } |
|
|
1064 | |
980 | |
1065 | /* |
981 | mortals.push_back (this); |
1066 | * count_active() returns the number of objects on the list of active objects. |
|
|
1067 | */ |
|
|
1068 | |
|
|
1069 | int count_active() |
|
|
1070 | { |
|
|
1071 | int i=0; |
|
|
1072 | object *tmp=active_objects; |
|
|
1073 | while(tmp!=NULL) |
|
|
1074 | tmp=tmp->active_next, i++; |
|
|
1075 | return i; |
|
|
1076 | } |
982 | } |
1077 | |
983 | |
1078 | /* |
984 | /* |
1079 | * sub_weight() recursively (outwards) subtracts a number from the |
985 | * sub_weight() recursively (outwards) subtracts a number from the |
1080 | * weight of an object (and what is carried by it's environment(s)). |
986 | * weight of an object (and what is carried by it's environment(s)). |
… | |
… | |
2485 | fclose(tempfile); |
2391 | fclose(tempfile); |
2486 | |
2392 | |
2487 | op=get_object(); |
2393 | op=get_object(); |
2488 | |
2394 | |
2489 | object_thawer thawer (filename); |
2395 | object_thawer thawer (filename); |
|
|
2396 | |
2490 | if (thawer) |
2397 | if (thawer) |
2491 | load_object(thawer,op,LO_NEWFILE,0); |
2398 | load_object(thawer,op,0); |
|
|
2399 | |
2492 | LOG(llevDebug," load str completed, object=%s\n", &op->name); |
2400 | LOG(llevDebug," load str completed, object=%s\n", &op->name); |
2493 | CLEAR_FLAG(op,FLAG_REMOVED); |
2401 | CLEAR_FLAG(op,FLAG_REMOVED); |
2494 | |
2402 | |
2495 | return op; |
2403 | return op; |
2496 | } |
2404 | } |