ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/object.c
Revision: 1.2
Committed: Tue Feb 7 23:29:55 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +5 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * static char *rcsid_object_c =
3 root 1.2 * "$Id: object.c,v 1.1.1.1 2006/02/03 07:11:38 root Exp $";
4 root 1.1 */
5    
6     /*
7     CrossFire, A Multiplayer game for X-windows
8    
9     Copyright (C) 2001 Mark Wedel & Crossfire Development Team
10     Copyright (C) 1992 Frank Tore Johansen
11    
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16    
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     GNU General Public License for more details.
21    
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25    
26     The authors can be reached via e-mail at crossfire-devel@real-time.com
27     */
28    
29     /* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects.
30     sub/add_weight will transcend the environment updating the carrying
31     variable. */
32     #include <global.h>
33     #ifndef WIN32 /* ---win32 exclude headers */
34     #include <stdio.h>
35     #include <sys/types.h>
36     #include <sys/uio.h>
37     #endif /* win32 */
38     #include <object.h>
39     #include <funcpoint.h>
40     #include <skills.h>
41     #include <loader.h>
42     #ifdef MEMORY_DEBUG
43     int nroffreeobjects = 0;
44     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    
53     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 */
56    
57 root 1.2 void (*object_free_callback)(object *ob);
58 root 1.1
59     short freearr_x[SIZEOFFREE]=
60     {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,
61     0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3,-3,-3,-3,-3,-2,-1};
62     short freearr_y[SIZEOFFREE]=
63     {0,-1,-1,0,1,1,1,0,-1,-2,-2,-2,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,
64     -3,-3,-3,-3,-2,-1,0,1,2,3,3,3,3,3,3,3,2,1,0,-1,-2,-3,-3,-3};
65     int maxfree[SIZEOFFREE]=
66     {0,9,10,13,14,17,18,21,22,25,26,27,30,31,32,33,36,37,39,39,42,43,44,45,
67     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};
68     int freedir[SIZEOFFREE]= {
69     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,
70     1,2,2,2,2,2,3,4,4,4,4,4,5,6,6,6,6,6,7,8,8,8,8,8};
71    
72    
73     /* Returns TRUE if every key_values in wants has a partner with the same value in has. */
74     static int compare_ob_value_lists_one(object * wants, object * has) {
75     key_value * wants_field;
76    
77     /* n-squared behaviour (see get_ob_key_link()), but I'm hoping both
78     * objects with lists are rare, and lists stay short. If not, use a
79     * different structure or at least keep the lists sorted...
80     */
81    
82     /* For each field in wants, */
83     for (wants_field = wants->key_values; wants_field != NULL; wants_field = wants_field->next) {
84     key_value * has_field;
85    
86     /* Look for a field in has with the same key. */
87     has_field = get_ob_key_link(has, wants_field->key);
88    
89     if (has_field == NULL) {
90     /* No field with that name. */
91     return FALSE;
92     }
93    
94     /* Found the matching field. */
95     if (has_field->value != wants_field->value) {
96     /* Values don't match, so this half of the comparison is false. */
97     return FALSE;
98     }
99    
100     /* If we get here, we found a match. Now for the next field in wants. */
101     }
102    
103     /* If we get here, every field in wants has a matching field in has. */
104     return TRUE;
105     }
106    
107     /* Returns TRUE if ob1 has the same key_values as ob2. */
108     static int compare_ob_value_lists(object * ob1, object * ob2) {
109     /* However, there may be fields in has which aren't partnered in wants,
110     * so we need to run the comparison *twice*. :(
111     */
112     return compare_ob_value_lists_one(ob1, ob2) && compare_ob_value_lists_one(ob2, ob1);
113     }
114    
115     /* Function examines the 2 objects given to it, and returns true if
116     * they can be merged together.
117     *
118     * Note that this function appears a lot longer than the macro it
119     * replaces - this is mostly for clarity - a decent compiler should hopefully
120     * reduce this to the same efficiency.
121     *
122     * Check nrof variable *before* calling CAN_MERGE()
123     *
124     * Improvements made with merge: Better checking on potion, and also
125     * check weight
126     */
127    
128     int CAN_MERGE(object *ob1, object *ob2) {
129    
130     /* A couple quicksanity checks */
131     if ((ob1 == ob2) || (ob1->type != ob2->type)) return 0;
132    
133     if (ob1->speed != ob2->speed) return 0;
134     /* Note sure why the following is the case - either the object has to
135     * be animated or have a very low speed. Is this an attempted monster
136     * check?
137     */
138     if (!QUERY_FLAG(ob1,FLAG_ANIMATE) && FABS((ob1)->speed) > MIN_ACTIVE_SPEED)
139     return 0;
140    
141     /* Do not merge objects if nrof would overflow. We use 1UL<<31 since that
142     * value could not be stored in a sint32 (which unfortunately sometimes is
143     * used to store nrof).
144     */
145     if (ob1->nrof+ob2->nrof >= 1UL<<31)
146     return 0;
147    
148     /* This is really a spellbook check - really, we should
149     * check all objects in the inventory.
150     */
151     if (ob1->inv || ob2->inv) {
152     /* if one object has inventory but the other doesn't, not equiv */
153     if ((ob1->inv && !ob2->inv) || (ob2->inv && !ob1->inv)) return 0;
154    
155     /* Now check to see if the two inventory objects could merge */
156     if (!CAN_MERGE(ob1->inv, ob2->inv)) return 0;
157    
158     /* inventory ok - still need to check rest of this object to see
159     * if it is valid.
160     */
161     }
162    
163     /* If the objects have been identified, set the BEEN_APPLIED flag.
164     * This is to the comparison of the flags below will be OK. We
165     * just can't ignore the been applied or identified flags, as they
166     * are not equal - just if it has been identified, the been_applied
167     * flags lose any meaning.
168     */
169     if (QUERY_FLAG(ob1, FLAG_IDENTIFIED))
170     SET_FLAG(ob1, FLAG_BEEN_APPLIED);
171    
172     if (QUERY_FLAG(ob2, FLAG_IDENTIFIED))
173     SET_FLAG(ob2, FLAG_BEEN_APPLIED);
174    
175    
176     /* the 0x400000 on flags2 is FLAG_INV_LOCK. I don't think something
177     * being locked in inventory should prevent merging.
178     * 0x4 in flags3 is CLIENT_SENT
179     */
180     if ((ob1->arch != ob2->arch) ||
181     (ob1->flags[0] != ob2->flags[0]) ||
182     (ob1->flags[1] != ob2->flags[1]) ||
183     ((ob1->flags[2] & ~0x400000) != (ob2->flags[2] & ~ 0x400000)) ||
184     ((ob1->flags[3] & ~0x4) != (ob2->flags[3] & ~0x4)) ||
185     (ob1->name != ob2->name) ||
186     (ob1->title != ob2->title) ||
187     (ob1->msg != ob2->msg) ||
188     (ob1->weight != ob2->weight) ||
189     (memcmp(&ob1->resist, &ob2->resist, sizeof(ob1->resist))!=0) ||
190     (memcmp(&ob1->stats, &ob2->stats, sizeof(ob1->stats))!=0) ||
191     (ob1->attacktype != ob2->attacktype) ||
192     (ob1->magic != ob2->magic) ||
193     (ob1->slaying != ob2->slaying) ||
194     (ob1->skill != ob2->skill) ||
195     (ob1->value != ob2->value) ||
196     (ob1->animation_id != ob2->animation_id) ||
197     (ob1->client_type != ob2->client_type) ||
198     (ob1->materialname != ob2->materialname) ||
199     (ob1->lore != ob2->lore) ||
200     (ob1->subtype != ob2->subtype) ||
201     (ob1->move_type != ob2->move_type) ||
202     (ob1->move_block != ob2->move_block) ||
203     (ob1->move_on != ob2->move_on) ||
204     (ob1->move_off != ob2->move_off) ||
205     (ob1->move_slow != ob2->move_slow) ||
206     (ob1->move_slow_penalty != ob2->move_slow_penalty)
207     )
208     return 0;
209    
210     /* Don't merge objects that are applied. With the new 'body' code,
211     * it is possible for most any character to have more than one of
212     * some items equipped, and we don't want those to merge.
213     */
214     if (QUERY_FLAG(ob1, FLAG_APPLIED) || QUERY_FLAG(ob2, FLAG_APPLIED))
215     return 0;
216    
217     if (ob1->key_values != NULL || ob2->key_values != NULL) {
218     /* At least one of these has key_values. */
219     if ((ob1->key_values == NULL) != (ob2->key_values == NULL)) {
220     /* One has fields, but the other one doesn't. */
221     return 0;
222     } else {
223     return compare_ob_value_lists(ob1, ob2);
224     }
225     }
226    
227     switch (ob1->type) {
228     case SCROLL:
229     if (ob1->level != ob2->level) return 0;
230     break;
231    
232     }
233     /* Everything passes, must be OK. */
234     return 1;
235     }
236    
237     /*
238     * sum_weight() is a recursive function which calculates the weight
239     * an object is carrying. It goes through in figures out how much
240     * containers are carrying, and sums it up.
241     */
242     signed long sum_weight(object *op) {
243     signed long sum;
244     object *inv;
245     for(sum = 0, inv = op->inv; inv != NULL; inv = inv->below) {
246     if (inv->inv)
247     sum_weight(inv);
248     sum += inv->carrying + inv->weight * (inv->nrof ? inv->nrof : 1);
249     }
250     if (op->type == CONTAINER && op->stats.Str)
251     sum = (sum * (100 - op->stats.Str))/100;
252     if(op->carrying != sum)
253     op->carrying = sum;
254     return sum;
255     }
256    
257     /**
258     * Return the outermost environment object for a given object.
259     */
260    
261     object *object_get_env_recursive (object *op) {
262     while (op->env != NULL)
263     op = op->env;
264     return op;
265     }
266    
267     /*
268     * Eneq(@csd.uu.se): Since we can have items buried in a character we need
269     * a better check. We basically keeping traversing up until we can't
270     * or find a player.
271     */
272    
273     object *is_player_inv (object *op) {
274     for (;op!=NULL&&op->type!=PLAYER; op=op->env)
275     if (op->env==op)
276     op->env = NULL;
277     return op;
278     }
279    
280     /*
281     * Used by: Crossedit: dump. Server DM commands: dumpbelow, dump.
282     * Some error messages.
283     * The result of the dump is stored in the static global errmsg array.
284     */
285    
286     void dump_object2(object *op) {
287     char *cp;
288     /* object *tmp;*/
289    
290     if(op->arch!=NULL) {
291     strcat(errmsg,"arch ");
292     strcat(errmsg,op->arch->name?op->arch->name:"(null)");
293     strcat(errmsg,"\n");
294     if((cp=get_ob_diff(op,&empty_archetype->clone))!=NULL)
295     strcat(errmsg,cp);
296     #if 0
297     /* Don't dump player diffs - they are too long, mostly meaningless, and
298     * will overflow the buffer.
299     * Changed so that we don't dump inventory either. This may
300     * also overflow the buffer.
301     */
302     if(op->type!=PLAYER && (cp=get_ob_diff(op,&empty_archetype->clone))!=NULL)
303     strcat(errmsg,cp);
304     for (tmp=op->inv; tmp; tmp=tmp->below)
305     dump_object2(tmp);
306     #endif
307     strcat(errmsg,"end\n");
308     } else {
309     strcat(errmsg,"Object ");
310     if (op->name==NULL) strcat(errmsg, "(null)");
311     else strcat(errmsg,op->name);
312     strcat(errmsg,"\n");
313     #if 0
314     if((cp=get_ob_diff(op,&empty_archetype->clone))!=NULL)
315     strcat(errmsg,cp);
316     for (tmp=op->inv; tmp; tmp=tmp->below)
317     dump_object2(tmp);
318     #endif
319     strcat(errmsg,"end\n");
320     }
321     }
322    
323     /*
324     * Dumps an object. Returns output in the static global errmsg array.
325     */
326    
327     void dump_object(object *op) {
328     if(op==NULL) {
329     strcpy(errmsg,"[NULL pointer]");
330     return;
331     }
332     errmsg[0]='\0';
333     dump_object2(op);
334     }
335    
336     /* GROS - Dumps an object. Return the result into a string */
337     /* Note that no checking is done for the validity of the target string, so */
338     /* you need to be sure that you allocated enough space for it. */
339     void dump_me(object *op, char *outstr)
340     {
341     char *cp;
342    
343     if(op==NULL)
344     {
345     strcpy(outstr,"[NULL pointer]");
346     return;
347     }
348     outstr[0]='\0';
349    
350     if(op->arch!=NULL)
351     {
352     strcat(outstr,"arch ");
353     strcat(outstr,op->arch->name?op->arch->name:"(null)");
354     strcat(outstr,"\n");
355     if((cp=get_ob_diff(op,&empty_archetype->clone))!=NULL)
356     strcat(outstr,cp);
357     strcat(outstr,"end\n");
358     }
359     else
360     {
361     strcat(outstr,"Object ");
362     if (op->name==NULL)
363     strcat(outstr, "(null)");
364     else
365     strcat(outstr,op->name);
366     strcat(outstr,"\n");
367     strcat(outstr,"end\n");
368     }
369     }
370    
371     /*
372     * This is really verbose...Can be triggered by the P key while in DM mode.
373     * All objects are dumped to stderr (or alternate logfile, if in server-mode)
374     */
375    
376     void dump_all_objects(void) {
377     object *op;
378     for(op=objects;op!=NULL;op=op->next) {
379     dump_object(op);
380     fprintf(logfile, "Object %d\n:%s\n", op->count, errmsg);
381     }
382     }
383    
384     /*
385     * get_nearest_part(multi-object, object 2) returns the part of the
386     * multi-object 1 which is closest to the second object.
387     * If it's not a multi-object, it is returned.
388     */
389    
390     object *get_nearest_part(object *op,object *pl) {
391     object *tmp,*closest;
392     int last_dist,i;
393     if(op->more==NULL)
394     return op;
395     for(last_dist=distance(op,pl),closest=op,tmp=op->more;tmp!=NULL;tmp=tmp->more)
396     if((i=distance(tmp,pl))<last_dist)
397     closest=tmp,last_dist=i;
398     return closest;
399     }
400    
401     /*
402     * Returns the object which has the count-variable equal to the argument.
403     */
404    
405     object *find_object(tag_t i) {
406     object *op;
407     for(op=objects;op!=NULL;op=op->next)
408     if(op->count==i)
409     break;
410     return op;
411     }
412    
413     /*
414     * Returns the first object which has a name equal to the argument.
415     * Used only by the patch command, but not all that useful.
416     * Enables features like "patch <name-of-other-player> food 999"
417     */
418    
419     object *find_object_name(char *str) {
420     const char *name=add_string(str);
421     object *op;
422     for(op=objects;op!=NULL;op=op->next)
423     if(op->name==name)
424     break;
425     free_string(name);
426     return op;
427     }
428    
429     void free_all_object_data(void) {
430     #ifdef MEMORY_DEBUG
431     object *op, *next;
432    
433     for (op=free_objects; op!=NULL; ) {
434     next=op->next;
435     free(op);
436     nrofallocobjects--;
437     nroffreeobjects--;
438     op=next;
439     }
440     #endif
441     LOG(llevDebug,"%d allocated objects, %d free objects, STARMAX=%d\n",
442     nrofallocobjects, nroffreeobjects,STARTMAX);
443     }
444    
445     /*
446     * Returns the object which this object marks as being the owner.
447     * A id-scheme is used to avoid pointing to objects which have been
448     * freed and are now reused. If this is detected, the owner is
449     * set to NULL, and NULL is returned.
450     * Changed 2004-02-12 - if the player is setting at the play again
451     * prompt, he is removed, and we don't want to treat him as an owner of
452     * anything, so check removed flag. I don't expect that this should break
453     * anything - once an object is removed, it is basically dead anyways.
454     */
455    
456     object *get_owner(object *op) {
457     if(op->owner==NULL)
458     return NULL;
459    
460     if (!QUERY_FLAG(op->owner,FLAG_FREED) && !QUERY_FLAG(op->owner, FLAG_REMOVED) &&
461     op->owner->count==op->ownercount)
462     return op->owner;
463    
464     op->owner=NULL;
465     op->ownercount=0;
466     return NULL;
467     }
468    
469     void clear_owner(object *op)
470     {
471     if (!op) return;
472    
473     if (op->owner && op->ownercount == op->owner->count)
474     op->owner->refcount--;
475    
476     op->owner = NULL;
477     op->ownercount = 0;
478     }
479    
480    
481    
482     /*
483     * Sets the owner and sets the skill and exp pointers to owner's current
484     * skill and experience objects.
485     */
486     void set_owner (object *op, object *owner)
487     {
488     if(owner==NULL||op==NULL)
489     return;
490    
491     /* next line added to allow objects which own objects */
492     /* Add a check for ownercounts in here, as I got into an endless loop
493     * with the fireball owning a poison cloud which then owned the
494     * fireball. I believe that was caused by one of the objects getting
495     * freed and then another object replacing it. Since the ownercounts
496     * didn't match, this check is valid and I believe that cause is valid.
497     */
498     while (owner->owner && owner!=owner->owner &&
499     owner->ownercount==owner->owner->count) owner=owner->owner;
500    
501     /* IF the owner still has an owner, we did not resolve to a final owner.
502     * so lets not add to that.
503     */
504     if (owner->owner) return;
505    
506     op->owner=owner;
507    
508     op->ownercount=owner->count;
509     owner->refcount++;
510    
511     }
512    
513     /* Set the owner to clone's current owner and set the skill and experience
514     * objects to clone's objects (typically those objects that where the owner's
515     * current skill and experience objects at the time when clone's owner was
516     * set - not the owner's current skill and experience objects).
517     *
518     * Use this function if player created an object (e.g. fire bullet, swarm
519     * spell), and this object creates further objects whose kills should be
520     * accounted for the player's original skill, even if player has changed
521     * skills meanwhile.
522     */
523     void copy_owner (object *op, object *clone)
524     {
525     object *owner = get_owner (clone);
526     if (owner == NULL) {
527     /* players don't have owners - they own themselves. Update
528     * as appropriate.
529     */
530     if (clone->type == PLAYER) owner=clone;
531     else return;
532     }
533     set_owner(op, owner);
534    
535     }
536    
537     /*
538     * Resets vital variables in an object
539     */
540    
541     void reset_object(object *op) {
542    
543     op->name = NULL;
544     op->name_pl = NULL;
545     op->title = NULL;
546     op->race = NULL;
547     op->slaying = NULL;
548     op->skill = NULL;
549     op->msg = NULL;
550     op->materialname = NULL;
551     op->lore = NULL;
552     op->current_weapon_script = NULL;
553     clear_object(op);
554     }
555    
556     /* Zero the key_values on op, decrementing the shared-string
557     * refcounts and freeing the links.
558     */
559     static void free_key_values(object * op) {
560     key_value * i;
561     key_value * next = NULL;
562    
563     if (op->key_values == NULL) return;
564    
565     for (i = op->key_values; i != NULL; i = next) {
566     /* Store next *first*. */
567     next = i->next;
568    
569     if (i->key) FREE_AND_CLEAR_STR(i->key);
570     if (i->value) FREE_AND_CLEAR_STR(i->value);
571     i->next = NULL;
572     free(i);
573     }
574    
575     op->key_values = NULL;
576     }
577    
578    
579     /*
580     * clear_object() frees everything allocated by an object, and also
581     * clears all variables and flags to default settings.
582     */
583    
584     void clear_object(object *op) {
585    
586     event *evt;
587     event *evt2;
588    
589     /* redo this to be simpler/more efficient. Was also seeing
590     * crashes in the old code. Move this to the top - am
591     * seeing periodic crashes in this code, and would like to have
592     * as much info available as possible (eg, object name).
593     */
594     for (evt = op->events; evt; evt=evt2) {
595     evt2 = evt->next;
596    
597     if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
598     if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
599     if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
600    
601     free(evt);
602     }
603     op->events = NULL;
604    
605     free_key_values(op);
606    
607     /* the memset will clear all these values for us, but we need
608     * to reduce the refcount on them.
609     */
610     if (op->name!=NULL) FREE_AND_CLEAR_STR(op->name);
611     if (op->name_pl!=NULL) FREE_AND_CLEAR_STR(op->name_pl);
612     if (op->title != NULL) FREE_AND_CLEAR_STR(op->title);
613     if (op->race!=NULL) FREE_AND_CLEAR_STR(op->race);
614     if (op->slaying!=NULL) FREE_AND_CLEAR_STR(op->slaying);
615     if (op->skill!=NULL) FREE_AND_CLEAR_STR(op->skill);
616     if (op->msg!=NULL) FREE_AND_CLEAR_STR(op->msg);
617     if (op->lore!=NULL) FREE_AND_CLEAR_STR(op->lore);
618     if (op->materialname!= NULL) FREE_AND_CLEAR_STR(op->materialname);
619    
620    
621     memset((void*)((char*)op + offsetof(object, name)),
622     0, sizeof(object)-offsetof(object, name));
623     /* Below here, we clear things that are not done by the memset,
624     * or set default values that are not zero.
625     */
626     /* This is more or less true */
627     SET_FLAG(op, FLAG_REMOVED);
628    
629    
630     op->contr = NULL;
631     op->below=NULL;
632     op->above=NULL;
633     op->inv=NULL;
634     op->events=NULL;
635     op->container=NULL;
636     op->env=NULL;
637     op->more=NULL;
638     op->head=NULL;
639     op->map=NULL;
640     op->refcount=0;
641     op->active_next = NULL;
642     op->active_prev = NULL;
643     /* What is not cleared is next, prev, and count */
644    
645     op->expmul=1.0;
646     op->face = blank_face;
647     op->attacked_by_count= -1;
648     if (settings.casting_time)
649     op->casting_time = -1;
650    
651     }
652    
653     /*
654     * copy object first frees everything allocated by the second object,
655     * and then copies the contends of the first object into the second
656     * object, allocating what needs to be allocated. Basically, any
657     * data that is malloc'd needs to be re-malloc/copied. Otherwise,
658     * if the first object is freed, the pointers in the new object
659     * will point at garbage.
660     */
661    
662     void copy_object(object *op2, object *op) {
663     int is_freed=QUERY_FLAG(op,FLAG_FREED),is_removed=QUERY_FLAG(op,FLAG_REMOVED);
664     event *evt, *evt2, *evt_new;
665    
666     /* Decrement the refcounts, but don't bother zeroing the fields;
667     they'll be overwritten by memcpy. */
668     if(op->name!=NULL) free_string(op->name);
669     if(op->name_pl!=NULL) free_string(op->name_pl);
670     if(op->title!=NULL) free_string(op->title);
671     if(op->race!=NULL) free_string(op->race);
672     if(op->slaying!=NULL) free_string(op->slaying);
673     if(op->skill!=NULL) free_string(op->skill);
674     if(op->msg!=NULL) free_string(op->msg);
675     if(op->lore!=NULL) free_string(op->lore);
676     if(op->materialname != NULL) free_string(op->materialname);
677     if(op->custom_name != NULL) free_string(op->custom_name);
678    
679     /* Basically, same code as from clear_object() */
680     for (evt = op->events; evt; evt=evt2) {
681     evt2 = evt->next;
682    
683     if (evt->hook != NULL) FREE_AND_CLEAR_STR(evt->hook);
684     if (evt->plugin != NULL) FREE_AND_CLEAR_STR(evt->plugin);
685     if (evt->options != NULL) FREE_AND_CLEAR_STR(evt->options);
686    
687     free(evt);
688     }
689     op->events = NULL;
690    
691     free_key_values(op);
692    
693     /* op is the destination, op2 is the source. */
694     (void) memcpy((void *)((char *) op +offsetof(object,name)),
695     (void *)((char *) op2+offsetof(object,name)),
696     sizeof(object)-offsetof(object, name));
697    
698     if(is_freed) SET_FLAG(op,FLAG_FREED);
699     if(is_removed) SET_FLAG(op,FLAG_REMOVED);
700     if(op->name!=NULL) add_refcount(op->name);
701     if(op->name_pl!=NULL) add_refcount(op->name_pl);
702     if(op->title!=NULL) add_refcount(op->title);
703     if(op->race!=NULL) add_refcount(op->race);
704     if(op->slaying!=NULL) add_refcount(op->slaying);
705     if(op->skill!=NULL) add_refcount(op->skill);
706     if(op->lore!=NULL) add_refcount(op->lore);
707     if(op->msg!=NULL) add_refcount(op->msg);
708     if(op->custom_name!=NULL) add_refcount(op->custom_name);
709     if (op->materialname != NULL) add_refcount(op->materialname);
710    
711     if((op2->speed<0) && !editor)
712     op->speed_left=op2->speed_left-RANDOM()%200/100.0;
713    
714     /* Copy over event information */
715     evt2 = NULL;
716     for (evt = op2->events; evt; evt=evt->next) {
717     evt_new = malloc(sizeof(event));
718     memcpy(evt_new, evt, sizeof(event));
719     if (evt_new->hook) add_refcount(evt_new->hook);
720     if (evt_new->plugin) add_refcount(evt_new->plugin);
721     if (evt_new->options) add_refcount(evt_new->options);
722     evt_new->next = NULL;
723    
724     /* Try to be a little clever here, and store away the
725     * last event we copied, so that its simpler to update the
726     * pointer.
727     */
728     if (evt2)
729     evt2->next = evt_new;
730     else
731     op->events = evt_new;
732    
733     evt2 = evt_new;
734     }
735     /* Copy over key_values, if any. */
736     if (op2->key_values != NULL) {
737     key_value * tail = NULL;
738     key_value * i;
739    
740     op->key_values = NULL;
741    
742     for (i = op2->key_values; i != NULL; i = i->next) {
743     key_value * new_link = malloc(sizeof(key_value));
744    
745     new_link->next = NULL;
746     new_link->key = add_refcount(i->key);
747     if (i->value)
748     new_link->value = add_refcount(i->value);
749     else
750     new_link->value = NULL;
751    
752     /* Try and be clever here, too. */
753     if (op->key_values == NULL) {
754     op->key_values = new_link;
755     tail = new_link;
756     } else {
757     tail->next = new_link;
758     tail = new_link;
759     }
760     }
761     }
762    
763     update_ob_speed(op);
764     }
765    
766     /*
767     * expand_objects() allocates more objects for the list of unused objects.
768     * It is called from get_object() if the unused list is empty.
769     */
770    
771     void expand_objects(void) {
772     int i;
773     object *new;
774     new = (object *) CALLOC(OBJ_EXPAND,sizeof(object));
775    
776     if(new==NULL)
777     fatal(OUT_OF_MEMORY);
778     free_objects=new;
779     new[0].prev=NULL;
780     new[0].next= &new[1],
781     SET_FLAG(&(new[0]), FLAG_REMOVED);
782     SET_FLAG(&(new[0]), FLAG_FREED);
783    
784     for(i=1;i<OBJ_EXPAND-1;i++) {
785     new[i].next= &new[i+1],
786     new[i].prev= &new[i-1],
787     SET_FLAG(&(new[i]), FLAG_REMOVED);
788     SET_FLAG(&(new[i]), FLAG_FREED);
789     }
790     new[OBJ_EXPAND-1].prev= &new[OBJ_EXPAND-2],
791     new[OBJ_EXPAND-1].next=NULL,
792     SET_FLAG(&(new[OBJ_EXPAND-1]), FLAG_REMOVED);
793     SET_FLAG(&(new[OBJ_EXPAND-1]), FLAG_FREED);
794    
795     nrofallocobjects += OBJ_EXPAND;
796     nroffreeobjects += OBJ_EXPAND;
797     }
798    
799     /*
800     * get_object() grabs an object from the list of unused objects, makes
801     * sure it is initialised, and returns it.
802     * If there are no free objects, expand_objects() is called to get more.
803     */
804    
805     object *get_object(void) {
806     object *op;
807    
808     if(free_objects==NULL) {
809     expand_objects();
810     }
811     op=free_objects;
812     #ifdef MEMORY_DEBUG
813     /* The idea is hopefully by doing a realloc, the memory
814     * debugging program will now use the current stack trace to
815     * report leaks.
816     */
817     op = realloc(op, sizeof(object));
818     SET_FLAG(op, FLAG_REMOVED);
819     SET_FLAG(op, FLAG_FREED);
820     #endif
821    
822     if(!QUERY_FLAG(op,FLAG_FREED)) {
823     LOG(llevError,"Fatal: Getting busy object.\n");
824     }
825     free_objects=op->next;
826     if(free_objects!=NULL)
827     free_objects->prev=NULL;
828     op->count= ++ob_count;
829     op->name=NULL;
830     op->name_pl=NULL;
831     op->title=NULL;
832     op->race=NULL;
833     op->slaying=NULL;
834     op->skill = NULL;
835     op->lore=NULL;
836     op->msg=NULL;
837     op->materialname=NULL;
838     op->next=objects;
839     op->prev=NULL;
840     op->active_next = NULL;
841     op->active_prev = NULL;
842     if(objects!=NULL)
843     objects->prev=op;
844     objects=op;
845     clear_object(op);
846     SET_FLAG(op,FLAG_REMOVED);
847     nroffreeobjects--;
848     return op;
849     }
850    
851     /*
852     * If an object with the IS_TURNABLE() flag needs to be turned due
853     * to the closest player being on the other side, this function can
854     * be called to update the face variable, _and_ how it looks on the map.
855     */
856    
857     void update_turn_face(object *op) {
858     if(!QUERY_FLAG(op,FLAG_IS_TURNABLE)||op->arch==NULL)
859     return;
860     SET_ANIMATION(op, op->direction);
861     update_object(op,UP_OBJ_FACE);
862     }
863    
864     /*
865     * Updates the speed of an object. If the speed changes from 0 to another
866     * value, or vice versa, then add/remove the object from the active list.
867     * This function needs to be called whenever the speed of an object changes.
868     */
869    
870     void update_ob_speed(object *op) {
871     extern int arch_init;
872    
873     /* No reason putting the archetypes objects on the speed list,
874     * since they never really need to be updated.
875     */
876    
877     if (QUERY_FLAG(op, FLAG_FREED) && op->speed) {
878     LOG(llevError,"Object %s is freed but has speed.\n", op->name);
879     #ifdef MANY_CORES
880     abort();
881     #else
882     op->speed = 0;
883     #endif
884     }
885     if (arch_init) {
886     return;
887     }
888     if (FABS(op->speed)>MIN_ACTIVE_SPEED) {
889     /* If already on active list, don't do anything */
890     if (op->active_next || op->active_prev || op==active_objects)
891     return;
892    
893     /* process_events() expects us to insert the object at the beginning
894     * of the list. */
895     op->active_next = active_objects;
896     if (op->active_next!=NULL)
897     op->active_next->active_prev = op;
898     active_objects = op;
899     }
900     else {
901     /* If not on the active list, nothing needs to be done */
902     if (!op->active_next && !op->active_prev && op!=active_objects)
903     return;
904    
905     if (op->active_prev==NULL) {
906     active_objects = op->active_next;
907     if (op->active_next!=NULL)
908     op->active_next->active_prev = NULL;
909     }
910     else {
911     op->active_prev->active_next = op->active_next;
912     if (op->active_next)
913     op->active_next->active_prev = op->active_prev;
914     }
915     op->active_next = NULL;
916     op->active_prev = NULL;
917     }
918     }
919    
920     /* This function removes object 'op' from the list of active
921     * objects.
922     * This should only be used for style maps or other such
923     * reference maps where you don't want an object that isn't
924     * in play chewing up cpu time getting processed.
925     * The reverse of this is to call update_ob_speed, which
926     * will do the right thing based on the speed of the object.
927     */
928     void remove_from_active_list(object *op)
929     {
930     /* If not on the active list, nothing needs to be done */
931     if (!op->active_next && !op->active_prev && op!=active_objects)
932     return;
933    
934     if (op->active_prev==NULL) {
935     active_objects = op->active_next;
936     if (op->active_next!=NULL)
937     op->active_next->active_prev = NULL;
938     }
939     else {
940     op->active_prev->active_next = op->active_next;
941     if (op->active_next)
942     op->active_next->active_prev = op->active_prev;
943     }
944     op->active_next = NULL;
945     op->active_prev = NULL;
946     }
947    
948     /*
949     * update_object() updates the array which represents the map.
950     * It takes into account invisible objects (and represent squares covered
951     * by invisible objects by whatever is below them (unless it's another
952     * invisible object, etc...)
953     * If the object being updated is beneath a player, the look-window
954     * of that player is updated (this might be a suboptimal way of
955     * updating that window, though, since update_object() is called _often_)
956     *
957     * action is a hint of what the caller believes need to be done.
958     * For example, if the only thing that has changed is the face (due to
959     * an animation), we don't need to call update_position until that actually
960     * comes into view of a player. OTOH, many other things, like addition/removal
961     * of walls or living creatures may need us to update the flags now.
962     * current action are:
963     * UP_OBJ_INSERT: op was inserted
964     * UP_OBJ_REMOVE: op was removed
965     * UP_OBJ_CHANGE: object has somehow changed. In this case, we always update
966     * as that is easier than trying to look at what may have changed.
967     * UP_OBJ_FACE: only the objects face has changed.
968     */
969    
970     void update_object(object *op, int action) {
971     int update_now=0, flags;
972     MoveType move_on, move_off, move_block, move_slow;
973    
974     if (op == NULL) {
975     /* this should never happen */
976     LOG(llevDebug,"update_object() called for NULL object.\n");
977     return;
978     }
979    
980     if(op->env!=NULL) {
981     /* Animation is currently handled by client, so nothing
982     * to do in this case.
983     */
984     return;
985     }
986    
987     /* If the map is saving, don't do anything as everything is
988     * going to get freed anyways.
989     */
990     if (!op->map || op->map->in_memory == MAP_SAVING) return;
991    
992     /* make sure the object is within map boundaries */
993     if (op->x < 0 || op->x >= MAP_WIDTH(op->map) ||
994     op->y < 0 || op->y >= MAP_HEIGHT(op->map)) {
995     LOG(llevError,"update_object() called for object out of map!\n");
996     #ifdef MANY_CORES
997     abort();
998     #endif
999     return;
1000     }
1001    
1002     flags = GET_MAP_FLAGS(op->map, op->x, op->y);
1003     SET_MAP_FLAGS(op->map, op->x, op->y, flags | P_NEED_UPDATE);
1004     move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
1005     move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
1006     move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
1007     move_off = GET_MAP_MOVE_OFF(op->map, op->x, op->y);
1008    
1009     if (action == UP_OBJ_INSERT) {
1010     if (QUERY_FLAG(op, FLAG_BLOCKSVIEW) && !(flags & P_BLOCKSVIEW))
1011     update_now=1;
1012    
1013     if (QUERY_FLAG(op, FLAG_NO_MAGIC) && !(flags & P_NO_MAGIC))
1014     update_now=1;
1015    
1016     if (QUERY_FLAG(op, FLAG_DAMNED) && !(flags & P_NO_CLERIC))
1017     update_now=1;
1018    
1019     if (QUERY_FLAG(op, FLAG_ALIVE) && !(flags & P_IS_ALIVE))
1020     update_now=1;
1021    
1022     if ((move_on | op->move_on) != move_on) update_now=1;
1023     if ((move_off | op->move_off) != move_off) update_now=1;
1024     if ((move_block | op->move_block) != move_block) update_now=1;
1025     if ((move_slow | op->move_slow) != move_slow) update_now=1;
1026     }
1027     /* if the object is being removed, we can't make intelligent
1028     * decisions, because remove_ob can't really pass the object
1029     * that is being removed.
1030     */
1031     else if (action == UP_OBJ_CHANGE || action == UP_OBJ_REMOVE) {
1032     update_now=1;
1033     } else if (action == UP_OBJ_FACE) {
1034     /* Nothing to do for that case */
1035     }
1036     else {
1037     LOG(llevError,"update_object called with invalid action: %d\n", action);
1038     }
1039    
1040     if (update_now) {
1041     SET_MAP_FLAGS(op->map, op->x, op->y, flags | P_NO_ERROR | P_NEED_UPDATE);
1042     update_position(op->map, op->x, op->y);
1043     }
1044    
1045     if(op->more!=NULL)
1046     update_object(op->more, action);
1047     }
1048    
1049    
1050     /*
1051     * free_object() frees everything allocated by an object, removes
1052     * it from the list of used objects, and puts it on the list of
1053     * free objects. The IS_FREED() flag is set in the object.
1054     * The object must have been removed by remove_ob() first for
1055     * this function to succeed.
1056     *
1057     * If free_inventory is set, free inventory as well. Else drop items in
1058     * inventory to the ground.
1059     */
1060    
1061     void free_object(object *ob) {
1062     free_object2(ob, 0);
1063     }
1064     void free_object2(object *ob, int free_inventory) {
1065     object *tmp,*op;
1066    
1067 root 1.2 if (object_free_callback)
1068     object_free_callback (ob);
1069    
1070 root 1.1 if (!QUERY_FLAG(ob,FLAG_REMOVED)) {
1071     LOG(llevDebug,"Free object called with non removed object\n");
1072     dump_object(ob);
1073     #ifdef MANY_CORES
1074     abort();
1075     #endif
1076     }
1077     if(QUERY_FLAG(ob,FLAG_FRIENDLY)) {
1078     LOG(llevMonster,"Warning: tried to free friendly object.\n");
1079     remove_friendly_object(ob);
1080     }
1081     if(QUERY_FLAG(ob,FLAG_FREED)) {
1082     dump_object(ob);
1083     LOG(llevError,"Trying to free freed object.\n%s\n",errmsg);
1084     return;
1085     }
1086     if(ob->more!=NULL) {
1087     free_object2(ob->more, free_inventory);
1088     ob->more=NULL;
1089     }
1090     if (ob->inv) {
1091     /* Only if the space blocks everything do we not process -
1092     * if some form of movemnt is allowed, let objects
1093     * drop on that space.
1094     */
1095     if (free_inventory || ob->map==NULL || ob->map->in_memory!=MAP_IN_MEMORY ||
1096     (GET_MAP_MOVE_BLOCK(ob->map, ob->x, ob->y) == MOVE_ALL))
1097     {
1098     op=ob->inv;
1099     while(op!=NULL) {
1100     tmp=op->below;
1101     remove_ob(op);
1102     free_object2(op, free_inventory);
1103     op=tmp;
1104     }
1105     }
1106     else { /* Put objects in inventory onto this space */
1107     op=ob->inv;
1108     while(op!=NULL) {
1109     tmp=op->below;
1110     remove_ob(op);
1111     if(QUERY_FLAG(op,FLAG_STARTEQUIP)||QUERY_FLAG(op,FLAG_NO_DROP) ||
1112     op->type==RUNE || op->type==TRAP || QUERY_FLAG(op,FLAG_IS_A_TEMPLATE))
1113     free_object(op);
1114     else {
1115     op->x=ob->x;
1116     op->y=ob->y;
1117     insert_ob_in_map(op,ob->map,NULL,0); /* Insert in same map as the envir */
1118     }
1119     op=tmp;
1120     }
1121     }
1122     }
1123     /* Remove object from the active list */
1124     ob->speed = 0;
1125     update_ob_speed(ob);
1126    
1127     SET_FLAG(ob, FLAG_FREED);
1128     ob->count = 0;
1129    
1130     /* Remove this object from the list of used objects */
1131     if(ob->prev==NULL) {
1132     objects=ob->next;
1133     if(objects!=NULL)
1134     objects->prev=NULL;
1135     }
1136     else {
1137     ob->prev->next=ob->next;
1138     if(ob->next!=NULL)
1139     ob->next->prev=ob->prev;
1140     }
1141    
1142     if(ob->name!=NULL) FREE_AND_CLEAR_STR(ob->name);
1143     if(ob->name_pl!=NULL) FREE_AND_CLEAR_STR(ob->name_pl);
1144     if(ob->title!=NULL) FREE_AND_CLEAR_STR(ob->title);
1145     if(ob->race!=NULL) FREE_AND_CLEAR_STR(ob->race);
1146     if(ob->slaying!=NULL) FREE_AND_CLEAR_STR(ob->slaying);
1147     if(ob->skill!=NULL) FREE_AND_CLEAR_STR(ob->skill);
1148     if(ob->lore!=NULL) FREE_AND_CLEAR_STR(ob->lore);
1149     if(ob->msg!=NULL) FREE_AND_CLEAR_STR(ob->msg);
1150     if(ob->materialname!=NULL) FREE_AND_CLEAR_STR(ob->materialname);
1151    
1152    
1153     /* Why aren't events freed? */
1154     free_key_values(ob);
1155    
1156     #if 0 /* MEMORY_DEBUG*/
1157     /* This is a nice idea. Unfortunately, a lot of the code in crossfire
1158     * presumes the freed_object will stick around for at least a little
1159     * bit
1160     */
1161     /* this is necessary so that memory debugging programs will
1162     * be able to accurately report source of malloc. If we recycle
1163     * objects, then some other area may be doing the get_object
1164     * and not freeing it, but the original one that malloc'd the
1165     * object will get the blame.
1166     */
1167     free(ob);
1168     #else
1169    
1170     /* Now link it with the free_objects list: */
1171     ob->prev=NULL;
1172     ob->next=free_objects;
1173     if(free_objects!=NULL)
1174     free_objects->prev=ob;
1175     free_objects=ob;
1176     nroffreeobjects++;
1177     #endif
1178     }
1179    
1180     /*
1181     * count_free() returns the number of objects on the list of free objects.
1182     */
1183    
1184     int count_free(void) {
1185     int i=0;
1186     object *tmp=free_objects;
1187     while(tmp!=NULL)
1188     tmp=tmp->next, i++;
1189     return i;
1190     }
1191    
1192     /*
1193     * count_used() returns the number of objects on the list of used objects.
1194     */
1195    
1196     int count_used(void) {
1197     int i=0;
1198     object *tmp=objects;
1199     while(tmp!=NULL)
1200     tmp=tmp->next, i++;
1201     return i;
1202     }
1203    
1204     /*
1205     * count_active() returns the number of objects on the list of active objects.
1206     */
1207    
1208     int count_active(void) {
1209     int i=0;
1210     object *tmp=active_objects;
1211     while(tmp!=NULL)
1212     tmp=tmp->active_next, i++;
1213     return i;
1214     }
1215    
1216     /*
1217     * sub_weight() recursively (outwards) subtracts a number from the
1218     * weight of an object (and what is carried by it's environment(s)).
1219     */
1220    
1221     void sub_weight (object *op, signed long weight) {
1222     while (op != NULL) {
1223     if (op->type == CONTAINER) {
1224     weight=(signed long)(weight*(100-op->stats.Str)/100);
1225     }
1226     op->carrying-=weight;
1227     op = op->env;
1228     }
1229     }
1230    
1231     /* remove_ob(op):
1232     * This function removes the object op from the linked list of objects
1233     * which it is currently tied to. When this function is done, the
1234     * object will have no environment. If the object previously had an
1235     * environment, the x and y coordinates will be updated to
1236     * the previous environment.
1237     * Beware: This function is called from the editor as well!
1238     */
1239    
1240     void remove_ob(object *op) {
1241     object *tmp,*last=NULL;
1242     object *otmp;
1243     tag_t tag;
1244     int check_walk_off;
1245     mapstruct *m;
1246     sint16 x,y;
1247    
1248    
1249     if(QUERY_FLAG(op,FLAG_REMOVED)) {
1250     dump_object(op);
1251     LOG(llevError,"Trying to remove removed object.\n%s\n",errmsg);
1252    
1253     /* Changed it to always dump core in this case. As has been learned
1254     * in the past, trying to recover from errors almost always
1255     * make things worse, and this is a real error here - something
1256     * that should not happen.
1257     * Yes, if this was a mission critical app, trying to do something
1258     * to recover may make sense, but that is because failure of the app
1259     * may have other disastrous problems. Cf runs out of a script
1260     * so is easily enough restarted without any real problems.
1261     * MSW 2001-07-01
1262     */
1263     abort();
1264     }
1265     if(op->more!=NULL)
1266     remove_ob(op->more);
1267    
1268     SET_FLAG(op, FLAG_REMOVED);
1269    
1270     /*
1271     * In this case, the object to be removed is in someones
1272     * inventory.
1273     */
1274     if(op->env!=NULL) {
1275     if(op->nrof)
1276     sub_weight(op->env, op->weight*op->nrof);
1277     else
1278     sub_weight(op->env, op->weight+op->carrying);
1279    
1280     /* NO_FIX_PLAYER is set when a great many changes are being
1281     * made to players inventory. If set, avoiding the call
1282     * to save cpu time.
1283     */
1284     if ((otmp=is_player_inv(op->env))!=NULL && otmp->contr &&
1285     !QUERY_FLAG(otmp,FLAG_NO_FIX_PLAYER))
1286     fix_player(otmp);
1287    
1288     if(op->above!=NULL)
1289     op->above->below=op->below;
1290     else
1291     op->env->inv=op->below;
1292    
1293     if(op->below!=NULL)
1294     op->below->above=op->above;
1295    
1296     /* we set up values so that it could be inserted into
1297     * the map, but we don't actually do that - it is up
1298     * to the caller to decide what we want to do.
1299     */
1300     op->x=op->env->x,op->y=op->env->y;
1301     op->ox=op->x,op->oy=op->y;
1302     op->map=op->env->map;
1303     op->above=NULL,op->below=NULL;
1304     op->env=NULL;
1305     return;
1306     }
1307    
1308     /* If we get here, we are removing it from a map */
1309     if (op->map == NULL) return;
1310    
1311     x = op->x;
1312     y = op->y;
1313     m = get_map_from_coord(op->map, &x, &y);
1314    
1315     if (!m) {
1316     LOG(llevError,"remove_ob called when object was on map but appears to not be within valid coordinates? %s (%d,%d)\n",
1317     op->map->path, op->x, op->y);
1318     /* in old days, we used to set x and y to 0 and continue.
1319     * it seems if we get into this case, something is probablye
1320     * screwed up and should be fixed.
1321     */
1322     abort();
1323     }
1324     if (op->map != m) {
1325     LOG(llevDebug,"remove_ob: Object not really on map it claimed to be on? %s != %s, %d,%d != %d,%d\n",
1326     op->map->path, m->path, op->x, op->y, x, y);
1327     }
1328    
1329     /* Re did the following section of code - it looks like it had
1330     * lots of logic for things we no longer care about
1331     */
1332    
1333     /* link the object above us */
1334     if (op->above)
1335     op->above->below=op->below;
1336     else
1337     SET_MAP_TOP(m,x,y,op->below); /* we were top, set new top */
1338    
1339     /* Relink the object below us, if there is one */
1340     if(op->below) {
1341     op->below->above=op->above;
1342     } else {
1343     /* Nothing below, which means we need to relink map object for this space
1344     * use translated coordinates in case some oddness with map tiling is
1345     * evident
1346     */
1347     if(GET_MAP_OB(m,x,y)!=op) {
1348     dump_object(op);
1349     LOG(llevError,"remove_ob: GET_MAP_OB does not return object to be removed even though it appears to be on the bottom?\n%s\n", errmsg);
1350     dump_object(GET_MAP_OB(m,x,y));
1351     LOG(llevError,"%s\n",errmsg);
1352     }
1353     SET_MAP_OB(m,x,y,op->above); /* goes on above it. */
1354     }
1355     op->above=NULL;
1356     op->below=NULL;
1357    
1358     if (op->map->in_memory == MAP_SAVING)
1359     return;
1360    
1361     tag = op->count;
1362     check_walk_off = ! QUERY_FLAG (op, FLAG_NO_APPLY);
1363     for(tmp=GET_MAP_OB(m,x,y);tmp!=NULL;tmp=tmp->above) {
1364     /* No point updating the players look faces if he is the object
1365     * being removed.
1366     */
1367    
1368     if(tmp->type==PLAYER && tmp!=op) {
1369     /* If a container that the player is currently using somehow gets
1370     * removed (most likely destroyed), update the player view
1371     * appropriately.
1372     */
1373     if (tmp->container==op) {
1374     CLEAR_FLAG(op, FLAG_APPLIED);
1375     tmp->container=NULL;
1376     }
1377     tmp->contr->socket.update_look=1;
1378     }
1379     /* See if player moving off should effect something */
1380     if (check_walk_off && ((op->move_type & tmp->move_off) &&
1381     (op->move_type & ~tmp->move_off & ~tmp->move_block)==0)) {
1382    
1383     move_apply(tmp, op, NULL);
1384     if (was_destroyed (op, tag)) {
1385     LOG (llevError, "BUG: remove_ob(): name %s, archname %s destroyed "
1386     "leaving object\n", tmp->name, tmp->arch->name);
1387     }
1388     }
1389    
1390     /* Eneq(@csd.uu.se): Fixed this to skip tmp->above=tmp */
1391    
1392     if(tmp->above == tmp)
1393     tmp->above = NULL;
1394     last=tmp;
1395     }
1396     /* last == NULL of there are no objects on this space */
1397     if (last==NULL) {
1398     /* set P_NEED_UPDATE, otherwise update_position will complain. In theory,
1399     * we could preserve the flags (GET_MAP_FLAGS), but update_position figures
1400     * those out anyways, and if there are any flags set right now, they won't
1401     * be correct anyways.
1402     */
1403     SET_MAP_FLAGS(op->map, op->x, op->y, P_NEED_UPDATE);
1404     update_position(op->map, op->x, op->y);
1405     }
1406     else
1407     update_object(last, UP_OBJ_REMOVE);
1408    
1409     if(QUERY_FLAG(op,FLAG_BLOCKSVIEW)|| (op->glow_radius != 0))
1410     update_all_los(op->map, op->x, op->y);
1411    
1412     }
1413    
1414     /*
1415     * merge_ob(op,top):
1416     *
1417     * This function goes through all objects below and including top, and
1418     * merges op to the first matching object.
1419     * If top is NULL, it is calculated.
1420     * Returns pointer to object if it succeded in the merge, otherwise NULL
1421     */
1422    
1423     object *merge_ob(object *op, object *top) {
1424     if(!op->nrof)
1425     return 0;
1426     if(top==NULL)
1427     for(top=op;top!=NULL&&top->above!=NULL;top=top->above);
1428     for(;top!=NULL;top=top->below) {
1429     if(top==op)
1430     continue;
1431     if (CAN_MERGE(op,top))
1432     {
1433     top->nrof+=op->nrof;
1434     /* CLEAR_FLAG(top,FLAG_STARTEQUIP);*/
1435     op->weight = 0; /* Don't want any adjustements now */
1436     remove_ob(op);
1437     free_object(op);
1438     return top;
1439     }
1440     }
1441     return NULL;
1442     }
1443    
1444     /*
1445     * same as insert_ob_in_map except it handle separate coordinates and do a clean
1446     * job preparing multi-part monsters
1447     */
1448     object *insert_ob_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y){
1449     object* tmp;
1450     if (op->head)
1451     op=op->head;
1452     for (tmp=op;tmp;tmp=tmp->more){
1453     tmp->x=x+tmp->arch->clone.x;
1454     tmp->y=y+tmp->arch->clone.y;
1455     }
1456     return insert_ob_in_map (op, m, originator, flag);
1457     }
1458    
1459     /*
1460     * insert_ob_in_map (op, map, originator, flag):
1461     * This function inserts the object in the two-way linked list
1462     * which represents what is on a map.
1463     * The second argument specifies the map, and the x and y variables
1464     * in the object about to be inserted specifies the position.
1465     *
1466     * originator: Player, monster or other object that caused 'op' to be inserted
1467     * into 'map'. May be NULL.
1468     *
1469     * flag is a bitmask about special things to do (or not do) when this
1470     * function is called. see the object.h file for the INS_ values.
1471     * Passing 0 for flag gives proper default values, so flag really only needs
1472     * to be set if special handling is needed.
1473     *
1474     * Return value:
1475     * new object if 'op' was merged with other object
1476     * NULL if 'op' was destroyed
1477     * just 'op' otherwise
1478     */
1479    
1480     object *insert_ob_in_map (object *op, mapstruct *m, object *originator, int flag)
1481     {
1482     object *tmp, *top, *floor=NULL;
1483     sint16 x,y;
1484    
1485     if (QUERY_FLAG (op, FLAG_FREED)) {
1486     LOG (llevError, "Trying to insert freed object!\n");
1487     return NULL;
1488     }
1489     if(m==NULL) {
1490     dump_object(op);
1491     LOG(llevError,"Trying to insert in null-map!\n%s\n",errmsg);
1492     return op;
1493     }
1494     if(out_of_map(m,op->x,op->y)) {
1495     dump_object(op);
1496     LOG(llevError,"Trying to insert object outside the map.\n%s\n", errmsg);
1497     #ifdef MANY_CORES
1498     /* Better to catch this here, as otherwise the next use of this object
1499     * is likely to cause a crash. Better to find out where it is getting
1500     * improperly inserted.
1501     */
1502     abort();
1503     #endif
1504     return op;
1505     }
1506     if(!QUERY_FLAG(op,FLAG_REMOVED)) {
1507     dump_object(op);
1508     LOG(llevError,"Trying to insert (map) inserted object.\n%s\n", errmsg);
1509     return op;
1510     }
1511     if(op->more!=NULL) {
1512     /* The part may be on a different map. */
1513    
1514     object *more = op->more;
1515    
1516     /* Debugging information so you can see the last coordinates this object had */
1517     more->ox = more->x;
1518     more->oy = more->y;
1519     more->map = get_map_from_coord(m, &more->x, &more->y);
1520    
1521     if (insert_ob_in_map(more, more->map, originator, flag) == NULL) {
1522     if ( ! op->head)
1523     LOG (llevError, "BUG: insert_ob_in_map(): inserting op->more killed op\n");
1524     return NULL;
1525     }
1526     }
1527     CLEAR_FLAG(op,FLAG_REMOVED);
1528    
1529     /* Debugging information so you can see the last coordinates this object had */
1530     op->ox=op->x;
1531     op->oy=op->y;
1532     x = op->x;
1533     y = op->y;
1534     op->map=get_map_from_coord(m, &x, &y);
1535    
1536     /* this has to be done after we translate the coordinates.
1537     */
1538     if(op->nrof && !(flag & INS_NO_MERGE)) {
1539     for(tmp=GET_MAP_OB(op->map,x,y);tmp!=NULL;tmp=tmp->above)
1540     if (CAN_MERGE(op,tmp)) {
1541     op->nrof+=tmp->nrof;
1542     remove_ob(tmp);
1543     free_object(tmp);
1544     }
1545     }
1546    
1547     /* Ideally, the caller figures this out. However, it complicates a lot
1548     * of areas of callers (eg, anything that uses find_free_spot would now
1549     * need extra work
1550     */
1551     if (op->map != m) {
1552     /* coordinates should not change unless map also changes */
1553     op->x = x;
1554     op->y = y;
1555     }
1556    
1557     CLEAR_FLAG(op,FLAG_APPLIED); /* hack for fixing F_APPLIED in items of dead people */
1558     CLEAR_FLAG(op, FLAG_INV_LOCKED);
1559     if (!QUERY_FLAG(op, FLAG_ALIVE))
1560     CLEAR_FLAG(op, FLAG_NO_STEAL);
1561    
1562     if (flag & INS_BELOW_ORIGINATOR) {
1563     if (originator->map != op->map || originator->x != op->x ||
1564     originator->y != op->y) {
1565     LOG(llevError,"insert_ob_in_map called with INS_BELOW_ORIGINATOR when originator not on same space!\n");
1566     abort();
1567     }
1568     op->above = originator;
1569     op->below = originator->below;
1570     if (op->below) op->below->above = op;
1571     else SET_MAP_OB(op->map, op->x, op->y, op);
1572     /* since *below* originator, no need to update top */
1573     originator->below = op;
1574     } else {
1575     /* If there are other objects, then */
1576     if((! (flag & INS_MAP_LOAD)) && ((top=GET_MAP_OB(op->map,op->x,op->y))!=NULL)) {
1577     object *last=NULL;
1578     /*
1579     * If there are multiple objects on this space, we do some trickier handling.
1580     * We've already dealt with merging if appropriate.
1581     * Generally, we want to put the new object on top. But if
1582     * flag contains INS_ABOVE_FLOOR_ONLY, once we find the last
1583     * floor, we want to insert above that and no further.
1584     * Also, if there are spell objects on this space, we stop processing
1585     * once we get to them. This reduces the need to traverse over all of
1586     * them when adding another one - this saves quite a bit of cpu time
1587     * when lots of spells are cast in one area. Currently, it is presumed
1588     * that flying non pickable objects are spell objects.
1589     */
1590    
1591     while (top != NULL) {
1592     if (QUERY_FLAG(top, FLAG_IS_FLOOR) ||
1593     QUERY_FLAG(top, FLAG_OVERLAY_FLOOR)) floor = top;
1594     if (QUERY_FLAG(top, FLAG_NO_PICK) &&
1595     (top->move_type & (MOVE_FLY_LOW |MOVE_FLY_HIGH))) {
1596     /* We insert above top, so we want this object below this */
1597     top=top->below;
1598     break;
1599     }
1600     last = top;
1601     top = top->above;
1602     }
1603     /* Don't want top to be NULL, so set it to the last valid object */
1604     top = last;
1605    
1606     /* We let update_position deal with figuring out what the space
1607     * looks like instead of lots of conditions here.
1608     * makes things faster, and effectively the same result.
1609     */
1610    
1611     /* Have object 'fall below' other objects that block view.
1612     * Unless those objects are exits, type 66
1613     * If INS_ON_TOP is used, don't do this processing
1614     * Need to find the object that in fact blocks view, otherwise
1615     * stacking is a bit odd.
1616     */
1617     if (!(flag & INS_ON_TOP) &&
1618     (get_map_flags(op->map, NULL, op->x, op->y, NULL, NULL) & P_BLOCKSVIEW) &&
1619     (op->face && !op->face->visibility)) {
1620     for (last=top; last != floor; last=last->below)
1621     if (QUERY_FLAG(last, FLAG_BLOCKSVIEW)&&(last->type != EXIT)) break;
1622     /* Check to see i we found the object that blocks view,
1623     * and make sure we have a below pointer for it so that
1624     * we can get inserted below this one, which requires we
1625     * set top to the object below us.
1626     */
1627     if (last && last->below && last != floor) top=last->below;
1628     }
1629     } /* If objects on this space */
1630     if (flag & INS_MAP_LOAD)
1631     top = GET_MAP_TOP(op->map,op->x,op->y);
1632     if (flag & INS_ABOVE_FLOOR_ONLY) top = floor;
1633    
1634     /* Top is the object that our object (op) is going to get inserted above.
1635     */
1636    
1637     /* First object on this space */
1638     if (!top) {
1639     op->above = GET_MAP_OB(op->map, op->x, op->y);
1640     if (op->above) op->above->below = op;
1641     op->below = NULL;
1642     SET_MAP_OB(op->map, op->x, op->y, op);
1643     } else { /* get inserted into the stack above top */
1644     op->above = top->above;
1645     if (op->above) op->above->below = op;
1646     op->below = top;
1647     top->above = op;
1648     }
1649     if (op->above==NULL)
1650     SET_MAP_TOP(op->map,op->x, op->y, op);
1651     } /* else not INS_BELOW_ORIGINATOR */
1652    
1653     if(op->type==PLAYER)
1654     op->contr->do_los=1;
1655    
1656     /* If we have a floor, we know the player, if any, will be above
1657     * it, so save a few ticks and start from there.
1658     */
1659     if (!(flag & INS_MAP_LOAD))
1660     for(tmp=floor?floor:GET_MAP_OB(op->map,op->x,op->y);tmp!=NULL;tmp=tmp->above) {
1661     if (tmp->type == PLAYER)
1662     tmp->contr->socket.update_look=1;
1663     }
1664    
1665     /* If this object glows, it may affect lighting conditions that are
1666     * visible to others on this map. But update_all_los is really
1667     * an inefficient way to do this, as it means los for all players
1668     * on the map will get recalculated. The players could very well
1669     * be far away from this change and not affected in any way -
1670     * this should get redone to only look for players within range,
1671     * or just updating the P_NEED_UPDATE for spaces within this area
1672     * of effect may be sufficient.
1673     */
1674     if(MAP_DARKNESS(op->map) && (op->glow_radius != 0))
1675     update_all_los(op->map, op->x, op->y);
1676    
1677    
1678     /* updates flags (blocked, alive, no magic, etc) for this map space */
1679     update_object(op,UP_OBJ_INSERT);
1680    
1681    
1682     /* Don't know if moving this to the end will break anything. However,
1683     * we want to have update_look set above before calling this.
1684     *
1685     * check_move_on() must be after this because code called from
1686     * check_move_on() depends on correct map flags (so functions like
1687     * blocked() and wall() work properly), and these flags are updated by
1688     * update_object().
1689     */
1690    
1691     /* if this is not the head or flag has been passed, don't check walk on status */
1692    
1693     if (!(flag & INS_NO_WALK_ON) && !op->head) {
1694     if (check_move_on(op, originator))
1695     return NULL;
1696    
1697     /* If we are a multi part object, lets work our way through the check
1698     * walk on's.
1699     */
1700     for (tmp=op->more; tmp!=NULL; tmp=tmp->more)
1701     if (check_move_on (tmp, originator))
1702     return NULL;
1703     }
1704     return op;
1705     }
1706    
1707     /* this function inserts an object in the map, but if it
1708     * finds an object of its own type, it'll remove that one first.
1709     * op is the object to insert it under: supplies x and the map.
1710     */
1711     void replace_insert_ob_in_map(char *arch_string, object *op) {
1712     object *tmp;
1713     object *tmp1;
1714    
1715     /* first search for itself and remove any old instances */
1716    
1717     for(tmp=GET_MAP_OB(op->map,op->x,op->y); tmp!=NULL; tmp=tmp->above) {
1718     if(!strcmp(tmp->arch->name,arch_string)) /* same archetype */ {
1719     remove_ob(tmp);
1720     free_object(tmp);
1721     }
1722     }
1723    
1724     tmp1=arch_to_object(find_archetype(arch_string));
1725    
1726    
1727     tmp1->x = op->x; tmp1->y = op->y;
1728     insert_ob_in_map(tmp1,op->map,op,0);
1729     }
1730    
1731     /*
1732     * get_split_ob(ob,nr) splits up ob into two parts. The part which
1733     * is returned contains nr objects, and the remaining parts contains
1734     * the rest (or is removed and freed if that number is 0).
1735     * On failure, NULL is returned, and the reason put into the
1736     * global static errmsg array.
1737     */
1738    
1739     object *get_split_ob(object *orig_ob, uint32 nr) {
1740     object *newob;
1741     int is_removed = (QUERY_FLAG (orig_ob, FLAG_REMOVED) != 0);
1742    
1743     if(orig_ob->nrof<nr) {
1744     sprintf(errmsg,"There are only %d %ss.",
1745     orig_ob->nrof?orig_ob->nrof:1, orig_ob->name);
1746     return NULL;
1747     }
1748     newob = object_create_clone(orig_ob);
1749     if((orig_ob->nrof-=nr)<1) {
1750     if ( ! is_removed)
1751     remove_ob(orig_ob);
1752     free_object2(orig_ob, 1);
1753     }
1754     else if ( ! is_removed) {
1755     if(orig_ob->env!=NULL)
1756     sub_weight (orig_ob->env,orig_ob->weight*nr);
1757     if (orig_ob->env == NULL && orig_ob->map->in_memory!=MAP_IN_MEMORY) {
1758     strcpy(errmsg, "Tried to split object whose map is not in memory.");
1759     LOG(llevDebug,
1760     "Error, Tried to split object whose map is not in memory.\n");
1761     return NULL;
1762     }
1763     }
1764     newob->nrof=nr;
1765    
1766     return newob;
1767     }
1768    
1769     /*
1770     * decrease_ob_nr(object, number) decreases a specified number from
1771     * the amount of an object. If the amount reaches 0, the object
1772     * is subsequently removed and freed.
1773     *
1774     * Return value: 'op' if something is left, NULL if the amount reached 0
1775     */
1776    
1777     object *decrease_ob_nr (object *op, uint32 i)
1778     {
1779     object *tmp;
1780     player *pl;
1781    
1782     if (i == 0) /* objects with op->nrof require this check */
1783     return op;
1784    
1785     if (i > op->nrof)
1786     i = op->nrof;
1787    
1788     if (QUERY_FLAG (op, FLAG_REMOVED))
1789     {
1790     op->nrof -= i;
1791     }
1792     else if (op->env != NULL)
1793     {
1794     /* is this object in the players inventory, or sub container
1795     * therein?
1796     */
1797     tmp = is_player_inv (op->env);
1798     /* nope. Is this a container the player has opened?
1799     * If so, set tmp to that player.
1800     * IMO, searching through all the players will mostly
1801     * likely be quicker than following op->env to the map,
1802     * and then searching the map for a player.
1803     */
1804     if (!tmp) {
1805     for (pl=first_player; pl; pl=pl->next)
1806     if (pl->ob->container == op->env) break;
1807     if (pl) tmp=pl->ob;
1808     else tmp=NULL;
1809     }
1810    
1811     if (i < op->nrof) {
1812     sub_weight (op->env, op->weight * i);
1813     op->nrof -= i;
1814     if (tmp) {
1815     esrv_send_item(tmp, op);
1816     }
1817     } else {
1818     remove_ob (op);
1819     op->nrof = 0;
1820     if (tmp) {
1821     esrv_del_item(tmp->contr, op->count);
1822     }
1823     }
1824     }
1825     else
1826     {
1827     object *above = op->above;
1828    
1829     if (i < op->nrof) {
1830     op->nrof -= i;
1831     } else {
1832     remove_ob (op);
1833     op->nrof = 0;
1834     }
1835     /* Since we just removed op, op->above is null */
1836     for (tmp = above; tmp != NULL; tmp = tmp->above)
1837     if (tmp->type == PLAYER) {
1838     if (op->nrof)
1839     esrv_send_item(tmp, op);
1840     else
1841     esrv_del_item(tmp->contr, op->count);
1842     }
1843     }
1844    
1845     if (op->nrof) {
1846     return op;
1847     } else {
1848     free_object (op);
1849     return NULL;
1850     }
1851     }
1852    
1853     /*
1854     * add_weight(object, weight) adds the specified weight to an object,
1855     * and also updates how much the environment(s) is/are carrying.
1856     */
1857    
1858     void add_weight (object *op, signed long weight) {
1859     while (op!=NULL) {
1860     if (op->type == CONTAINER) {
1861     weight=(signed long)(weight*(100-op->stats.Str)/100);
1862     }
1863     op->carrying+=weight;
1864     op=op->env;
1865     }
1866     }
1867    
1868     /*
1869     * insert_ob_in_ob(op,environment):
1870     * This function inserts the object op in the linked list
1871     * inside the object environment.
1872     *
1873     * Eneq(@csd.uu.se): Altered insert_ob_in_ob to make things picked up enter
1874     * the inventory at the last position or next to other objects of the same
1875     * type.
1876     * Frank: Now sorted by type, archetype and magic!
1877     *
1878     * The function returns now pointer to inserted item, and return value can
1879     * be != op, if items are merged. -Tero
1880     */
1881    
1882     object *insert_ob_in_ob(object *op,object *where) {
1883     object *tmp, *otmp;
1884    
1885     if(!QUERY_FLAG(op,FLAG_REMOVED)) {
1886     dump_object(op);
1887     LOG(llevError,"Trying to insert (ob) inserted object.\n%s\n", errmsg);
1888     return op;
1889     }
1890     if(where==NULL) {
1891     dump_object(op);
1892     LOG(llevError,"Trying to put object in NULL.\n%s\n", errmsg);
1893     return op;
1894     }
1895     if (where->head) {
1896     LOG(llevDebug,
1897     "Warning: Tried to insert object wrong part of multipart object.\n");
1898     where = where->head;
1899     }
1900     if (op->more) {
1901     LOG(llevError, "Tried to insert multipart object %s (%d)\n",
1902     op->name, op->count);
1903     return op;
1904     }
1905     CLEAR_FLAG(op, FLAG_OBJ_ORIGINAL);
1906     CLEAR_FLAG(op, FLAG_REMOVED);
1907     if(op->nrof) {
1908     for(tmp=where->inv;tmp!=NULL;tmp=tmp->below)
1909     if ( CAN_MERGE(tmp,op) ) {
1910     /* return the original object and remove inserted object
1911     (client needs the original object) */
1912     tmp->nrof += op->nrof;
1913     /* Weight handling gets pretty funky. Since we are adding to
1914     * tmp->nrof, we need to increase the weight.
1915     */
1916     add_weight (where, op->weight*op->nrof);
1917     SET_FLAG(op, FLAG_REMOVED);
1918     free_object(op); /* free the inserted object */
1919     op = tmp;
1920     remove_ob (op); /* and fix old object's links */
1921     CLEAR_FLAG(op, FLAG_REMOVED);
1922     break;
1923     }
1924    
1925     /* I assume combined objects have no inventory
1926     * We add the weight - this object could have just been removed
1927     * (if it was possible to merge). calling remove_ob will subtract
1928     * the weight, so we need to add it in again, since we actually do
1929     * the linking below
1930     */
1931     add_weight (where, op->weight*op->nrof);
1932     } else
1933     add_weight (where, (op->weight+op->carrying));
1934    
1935     otmp=is_player_inv(where);
1936     if (otmp&&otmp->contr!=NULL) {
1937     if (!QUERY_FLAG(otmp,FLAG_NO_FIX_PLAYER))
1938     fix_player(otmp);
1939     }
1940    
1941     op->map=NULL;
1942     op->env=where;
1943     op->above=NULL;
1944     op->below=NULL;
1945     op->x=0,op->y=0;
1946     op->ox=0,op->oy=0;
1947    
1948     /* reset the light list and los of the players on the map */
1949     if((op->glow_radius!=0)&&where->map)
1950     {
1951     #ifdef DEBUG_LIGHTS
1952     LOG(llevDebug, " insert_ob_in_ob(): got %s to insert in map/op\n",
1953     op->name);
1954     #endif /* DEBUG_LIGHTS */
1955     if (MAP_DARKNESS(where->map)) update_all_los(where->map, where->x, where->y);
1956     }
1957    
1958     /* Client has no idea of ordering so lets not bother ordering it here.
1959     * It sure simplifies this function...
1960     */
1961     if (where->inv==NULL)
1962     where->inv=op;
1963     else {
1964     op->below = where->inv;
1965     op->below->above = op;
1966     where->inv = op;
1967     }
1968     return op;
1969     }
1970    
1971     /*
1972     * Checks if any objects has a move_type that matches objects
1973     * that effect this object on this space. Call apply() to process
1974     * these events.
1975     *
1976     * Any speed-modification due to SLOW_MOVE() of other present objects
1977     * will affect the speed_left of the object.
1978     *
1979     * originator: Player, monster or other object that caused 'op' to be inserted
1980     * into 'map'. May be NULL.
1981     *
1982     * Return value: 1 if 'op' was destroyed, 0 otherwise.
1983     *
1984     * 4-21-95 added code to check if appropriate skill was readied - this will
1985     * permit faster movement by the player through this terrain. -b.t.
1986     *
1987     * MSW 2001-07-08: Check all objects on space, not just those below
1988     * object being inserted. insert_ob_in_map may not put new objects
1989     * on top.
1990     */
1991    
1992     int check_move_on (object *op, object *originator)
1993     {
1994     object *tmp;
1995     tag_t tag;
1996     mapstruct *m=op->map;
1997     int x=op->x, y=op->y;
1998     MoveType move_on, move_slow, move_block;
1999    
2000     if(QUERY_FLAG(op,FLAG_NO_APPLY))
2001     return 0;
2002    
2003     tag = op->count;
2004    
2005     move_on = GET_MAP_MOVE_ON(op->map, op->x, op->y);
2006     move_slow = GET_MAP_MOVE_SLOW(op->map, op->x, op->y);
2007     move_block = GET_MAP_MOVE_BLOCK(op->map, op->x, op->y);
2008    
2009     /* if nothing on this space will slow op down or be applied,
2010     * no need to do checking below. have to make sure move_type
2011     * is set, as lots of objects don't have it set - we treat that
2012     * as walking.
2013     */
2014     if (op->move_type && !(op->move_type & move_on) && !(op->move_type & move_slow))
2015     return 0;
2016    
2017     /* This is basically inverse logic of that below - basically,
2018     * if the object can avoid the move on or slow move, they do so,
2019     * but can't do it if the alternate movement they are using is
2020     * blocked. Logic on this seems confusing, but does seem correct.
2021     */
2022     if ((op->move_type & ~move_on & ~move_block) != 0 &&
2023     (op->move_type & ~move_slow & ~move_block) != 0) return 0;
2024    
2025     /* The objects have to be checked from top to bottom.
2026     * Hence, we first go to the top:
2027     */
2028    
2029     for (tmp=GET_MAP_OB(op->map, op->x, op->y); tmp!=NULL &&
2030     tmp->above!=NULL; tmp=tmp->above) {
2031     /* Trim the search when we find the first other spell effect
2032     * this helps performance so that if a space has 50 spell objects,
2033     * we don't need to check all of them.
2034     */
2035     if ((tmp->move_type & MOVE_FLY_LOW) && QUERY_FLAG(tmp, FLAG_NO_PICK)) break;
2036     }
2037     for(;tmp!=NULL; tmp=tmp->below) {
2038     if (tmp == op) continue; /* Can't apply yourself */
2039    
2040     /* Check to see if one of the movement types should be slowed down.
2041     * Second check makes sure that the movement types not being slowed
2042     * (~slow_move) is not blocked on this space - just because the
2043     * space doesn't slow down swimming (for example), if you can't actually
2044     * swim on that space, can't use it to avoid the penalty.
2045     */
2046     if (!QUERY_FLAG(op, FLAG_WIZPASS)) {
2047     if ((!op->move_type && tmp->move_slow & MOVE_WALK) ||
2048     ((op->move_type & tmp->move_slow) &&
2049     (op->move_type & ~tmp->move_slow & ~tmp->move_block) == 0)) {
2050    
2051     float diff;
2052    
2053     diff = tmp->move_slow_penalty*FABS(op->speed);
2054     if (op->type == PLAYER) {
2055     if ((QUERY_FLAG(tmp, FLAG_IS_HILLY) && find_skill_by_number(op, SK_CLIMBING)) ||
2056     (QUERY_FLAG(tmp, FLAG_IS_WOODED) && find_skill_by_number(op, SK_WOODSMAN))) {
2057     diff /= 4.0;
2058     }
2059     }
2060     op->speed_left -= diff;
2061     }
2062     }
2063    
2064     /* Basically same logic as above, except now for actual apply. */
2065     if ((!op->move_type && tmp->move_on & MOVE_WALK) ||
2066     ((op->move_type & tmp->move_on) &&
2067     (op->move_type & ~tmp->move_on & ~tmp->move_block)==0)) {
2068    
2069     move_apply(tmp, op, originator);
2070     if (was_destroyed (op, tag))
2071     return 1;
2072    
2073     /* what the person/creature stepped onto has moved the object
2074     * someplace new. Don't process any further - if we did,
2075     * have a feeling strange problems would result.
2076     */
2077     if (op->map != m || op->x != x || op->y != y) return 0;
2078     }
2079     }
2080     return 0;
2081     }
2082    
2083     /*
2084     * present_arch(arch, map, x, y) searches for any objects with
2085     * a matching archetype at the given map and coordinates.
2086     * The first matching object is returned, or NULL if none.
2087     */
2088    
2089     object *present_arch(archetype *at, mapstruct *m, int x, int y) {
2090     object *tmp;
2091     if(m==NULL || out_of_map(m,x,y)) {
2092     LOG(llevError,"Present_arch called outside map.\n");
2093     return NULL;
2094     }
2095     for(tmp=GET_MAP_OB(m,x,y); tmp != NULL; tmp = tmp->above)
2096     if(tmp->arch == at)
2097     return tmp;
2098     return NULL;
2099     }
2100    
2101     /*
2102     * present(type, map, x, y) searches for any objects with
2103     * a matching type variable at the given map and coordinates.
2104     * The first matching object is returned, or NULL if none.
2105     */
2106    
2107     object *present(unsigned char type,mapstruct *m, int x,int y) {
2108     object *tmp;
2109     if(out_of_map(m,x,y)) {
2110     LOG(llevError,"Present called outside map.\n");
2111     return NULL;
2112     }
2113     for(tmp=GET_MAP_OB(m,x,y);tmp!=NULL;tmp=tmp->above)
2114     if(tmp->type==type)
2115     return tmp;
2116     return NULL;
2117     }
2118    
2119     /*
2120     * present_in_ob(type, object) searches for any objects with
2121     * a matching type variable in the inventory of the given object.
2122     * The first matching object is returned, or NULL if none.
2123     */
2124    
2125     object *present_in_ob(unsigned char type,object *op) {
2126     object *tmp;
2127     for(tmp=op->inv;tmp!=NULL;tmp=tmp->below)
2128     if(tmp->type==type)
2129     return tmp;
2130     return NULL;
2131     }
2132    
2133     /*
2134     * present_in_ob (type, str, object) searches for any objects with
2135     * a matching type & name variable in the inventory of the given object.
2136     * The first matching object is returned, or NULL if none.
2137     * This is mostly used by spell effect code, so that we only
2138     * have one spell effect at a time.
2139     * type can be used to narrow the search - if type is set,
2140     * the type must also match. -1 can be passed for the type,
2141     * in which case the type does not need to pass.
2142     * str is the string to match against. Note that we match against
2143     * the object name, not the archetype name. this is so that the
2144     * spell code can use one object type (force), but change it's name
2145     * to be unique.
2146     */
2147    
2148     object *present_in_ob_by_name(int type, char *str,object *op) {
2149     object *tmp;
2150    
2151     for(tmp=op->inv; tmp!=NULL; tmp=tmp->below) {
2152     if ((type==-1 || tmp->type==type) && (!strcmp(str, tmp->name)))
2153     return tmp;
2154     }
2155     return NULL;
2156     }
2157    
2158     /*
2159     * present_arch_in_ob(archetype, object) searches for any objects with
2160     * a matching archetype in the inventory of the given object.
2161     * The first matching object is returned, or NULL if none.
2162     */
2163    
2164     object *present_arch_in_ob(archetype *at, object *op) {
2165     object *tmp;
2166     for(tmp=op->inv;tmp!=NULL;tmp=tmp->below)
2167     if( tmp->arch == at)
2168     return tmp;
2169     return NULL;
2170     }
2171    
2172     /*
2173     * activate recursively a flag on an object inventory
2174     */
2175     void flag_inv(object*op, int flag){
2176     object *tmp;
2177     if(op->inv)
2178     for(tmp = op->inv; tmp != NULL; tmp = tmp->below){
2179     SET_FLAG(tmp, flag);
2180     flag_inv(tmp,flag);
2181     }
2182     }/*
2183     * desactivate recursively a flag on an object inventory
2184     */
2185     void unflag_inv(object*op, int flag){
2186     object *tmp;
2187     if(op->inv)
2188     for(tmp = op->inv; tmp != NULL; tmp = tmp->below){
2189     CLEAR_FLAG(tmp, flag);
2190     unflag_inv(tmp,flag);
2191     }
2192     }
2193    
2194     /*
2195     * set_cheat(object) sets the cheat flag (WAS_WIZ) in the object and in
2196     * all it's inventory (recursively).
2197     * If checksums are used, a player will get set_cheat called for
2198     * him/her-self and all object carried by a call to this function.
2199     */
2200    
2201     void set_cheat(object *op) {
2202     SET_FLAG(op, FLAG_WAS_WIZ);
2203     flag_inv(op, FLAG_WAS_WIZ);
2204     }
2205    
2206     /*
2207     * find_free_spot(object, map, x, y, start, stop) will search for
2208     * a spot at the given map and coordinates which will be able to contain
2209     * the given object. start and stop specifies how many squares
2210     * to search (see the freearr_x/y[] definition).
2211     * It returns a random choice among the alternatives found.
2212     * start and stop are where to start relative to the free_arr array (1,9
2213     * does all 4 immediate directions). This returns the index into the
2214     * array of the free spot, -1 if no spot available (dir 0 = x,y)
2215     * Note - this only checks to see if there is space for the head of the
2216     * object - if it is a multispace object, this should be called for all
2217     * pieces.
2218     * Note2: This function does correctly handle tiled maps, but does not
2219     * inform the caller. However, insert_ob_in_map will update as
2220     * necessary, so the caller shouldn't need to do any special work.
2221     * Note - updated to take an object instead of archetype - this is necessary
2222     * because arch_blocked (now ob_blocked) needs to know the movement type
2223     * to know if the space in question will block the object. We can't use
2224     * the archetype because that isn't correct if the monster has been
2225     * customized, changed states, etc.
2226     */
2227    
2228     int find_free_spot(object *ob, mapstruct *m,int x,int y,int start,int stop) {
2229     int i,index=0, flag;
2230     static int altern[SIZEOFFREE];
2231    
2232     for(i=start;i<stop;i++) {
2233     flag = ob_blocked(ob,m,x+freearr_x[i],y+freearr_y[i]);
2234     if(!flag)
2235     altern[index++]=i;
2236    
2237     /* Basically, if we find a wall on a space, we cut down the search size.
2238     * In this way, we won't return spaces that are on another side of a wall.
2239     * This mostly work, but it cuts down the search size in all directions -
2240     * if the space being examined only has a wall to the north and empty
2241     * spaces in all the other directions, this will reduce the search space
2242     * to only the spaces immediately surrounding the target area, and
2243     * won't look 2 spaces south of the target space.
2244     */
2245     else if ((flag & AB_NO_PASS) && maxfree[i]<stop)
2246     stop=maxfree[i];
2247     }
2248     if(!index) return -1;
2249     return altern[RANDOM()%index];
2250     }
2251    
2252     /*
2253     * find_first_free_spot(archetype, mapstruct, x, y) works like
2254     * find_free_spot(), but it will search max number of squares.
2255     * But it will return the first available spot, not a random choice.
2256     * Changed 0.93.2: Have it return -1 if there is no free spot available.
2257     */
2258    
2259     int find_first_free_spot(object *ob, mapstruct *m,int x,int y) {
2260     int i;
2261     for(i=0;i<SIZEOFFREE;i++) {
2262     if(!ob_blocked(ob,m,x+freearr_x[i],y+freearr_y[i]))
2263     return i;
2264     }
2265     return -1;
2266     }
2267    
2268     /*
2269     * The function permute(arr, begin, end) randomly reorders the array
2270     * arr[begin..end-1].
2271     */
2272     static void permute(int *arr, int begin, int end)
2273     {
2274     int i, j, tmp, len;
2275    
2276     len = end-begin;
2277     for(i = begin; i < end; i++)
2278     {
2279     j = begin+RANDOM()%len;
2280    
2281     tmp = arr[i];
2282     arr[i] = arr[j];
2283     arr[j] = tmp;
2284     }
2285     }
2286    
2287     /* new function to make monster searching more efficient, and effective!
2288     * This basically returns a randomized array (in the passed pointer) of
2289     * the spaces to find monsters. In this way, it won't always look for
2290     * monsters to the north first. However, the size of the array passed
2291     * covers all the spaces, so within that size, all the spaces within
2292     * the 3x3 area will be searched, just not in a predictable order.
2293     */
2294     void get_search_arr(int *search_arr)
2295     {
2296     int i;
2297    
2298     for(i = 0; i < SIZEOFFREE; i++)
2299     {
2300     search_arr[i] = i;
2301     }
2302    
2303     permute(search_arr, 1, SIZEOFFREE1+1);
2304     permute(search_arr, SIZEOFFREE1+1, SIZEOFFREE2+1);
2305     permute(search_arr, SIZEOFFREE2+1, SIZEOFFREE);
2306     }
2307    
2308     /*
2309     * find_dir(map, x, y, exclude) will search some close squares in the
2310     * given map at the given coordinates for live objects.
2311     * It will not considered the object given as exclude among possible
2312     * live objects.
2313     * It returns the direction toward the first/closest live object if finds
2314     * any, otherwise 0.
2315     * Perhaps incorrectly, but I'm making the assumption that exclude
2316     * is actually want is going to try and move there. We need this info
2317     * because we have to know what movement the thing looking to move
2318     * there is capable of.
2319     */
2320    
2321     int find_dir(mapstruct *m, int x, int y, object *exclude) {
2322     int i,max=SIZEOFFREE, mflags;
2323     sint16 nx, ny;
2324     object *tmp;
2325     mapstruct *mp;
2326     MoveType blocked, move_type;
2327    
2328     if (exclude && exclude->head) {
2329     exclude = exclude->head;
2330     move_type = exclude->move_type;
2331     } else {
2332     /* If we don't have anything, presume it can use all movement types. */
2333     move_type=MOVE_ALL;
2334     }
2335    
2336     for(i=1;i<max;i++) {
2337     mp = m;
2338     nx = x + freearr_x[i];
2339     ny = y + freearr_y[i];
2340    
2341     mflags = get_map_flags(m, &mp, nx, ny, &nx, &ny);
2342     if (mflags & P_OUT_OF_MAP) {
2343     max = maxfree[i];
2344     } else {
2345     blocked = GET_MAP_MOVE_BLOCK(mp, nx, ny);
2346    
2347     if ((move_type & blocked) == move_type) {
2348     max=maxfree[i];
2349     } else if (mflags & P_IS_ALIVE) {
2350     for (tmp=GET_MAP_OB(mp,nx,ny); tmp!= NULL; tmp=tmp->above) {
2351     if ((QUERY_FLAG(tmp,FLAG_MONSTER) || tmp->type==PLAYER) &&
2352     (tmp != exclude ||(tmp->head && tmp->head != exclude))) {
2353     break;
2354     }
2355     }
2356     if(tmp) {
2357     return freedir[i];
2358     }
2359     }
2360     }
2361     }
2362     return 0;
2363     }
2364    
2365     /*
2366     * distance(object 1, object 2) will return the square of the
2367     * distance between the two given objects.
2368     */
2369    
2370     int distance(object *ob1,object *ob2) {
2371     int i;
2372     i= (ob1->x - ob2->x)*(ob1->x - ob2->x)+
2373     (ob1->y - ob2->y)*(ob1->y - ob2->y);
2374     return i;
2375     }
2376    
2377     /*
2378     * find_dir_2(delta-x,delta-y) will return a direction in which
2379     * an object which has subtracted the x and y coordinates of another
2380     * object, needs to travel toward it.
2381     */
2382    
2383     int find_dir_2(int x, int y) {
2384     int q;
2385     if(!y)
2386     q= -300*x;
2387     else
2388     q=x*100/y;
2389     if(y>0) {
2390     if(q < -242)
2391     return 3 ;
2392     if (q < -41)
2393     return 2 ;
2394     if (q < 41)
2395     return 1 ;
2396     if (q < 242)
2397     return 8 ;
2398     return 7 ;
2399     }
2400     if (q < -242)
2401     return 7 ;
2402     if (q < -41)
2403     return 6 ;
2404     if (q < 41)
2405     return 5 ;
2406     if (q < 242)
2407     return 4 ;
2408     return 3 ;
2409     }
2410    
2411     /*
2412     * absdir(int): Returns a number between 1 and 8, which represent
2413     * the "absolute" direction of a number (it actually takes care of
2414     * "overflow" in previous calculations of a direction).
2415     */
2416    
2417     int absdir(int d) {
2418     while(d<1) d+=8;
2419     while(d>8) d-=8;
2420     return d;
2421     }
2422    
2423     /*
2424     * dirdiff(dir1, dir2) returns how many 45-degrees differences there is
2425     * between two directions (which are expected to be absolute (see absdir())
2426     */
2427    
2428     int dirdiff(int dir1, int dir2) {
2429     int d;
2430     d = abs(dir1 - dir2);
2431     if(d>4)
2432     d = 8 - d;
2433     return d;
2434     }
2435    
2436     /* peterm:
2437     * do LOS stuff for ball lightning. Go after the closest VISIBLE monster.
2438     * Basically, this is a table of directions, and what directions
2439     * one could go to go back to us. Eg, entry 15 below is 4, 14, 16.
2440     * This basically means that if direction is 15, then it could either go
2441     * direction 4, 14, or 16 to get back to where we are.
2442     * Moved from spell_util.c to object.c with the other related direction
2443     * functions.
2444     */
2445    
2446     int reduction_dir[SIZEOFFREE][3] = {
2447     {0,0,0}, /* 0 */
2448     {0,0,0}, /* 1 */
2449     {0,0,0}, /* 2 */
2450     {0,0,0}, /* 3 */
2451     {0,0,0}, /* 4 */
2452     {0,0,0}, /* 5 */
2453     {0,0,0}, /* 6 */
2454     {0,0,0}, /* 7 */
2455     {0,0,0}, /* 8 */
2456     {8,1,2}, /* 9 */
2457     {1,2,-1}, /* 10 */
2458     {2,10,12}, /* 11 */
2459     {2,3,-1}, /* 12 */
2460     {2,3,4}, /* 13 */
2461     {3,4,-1}, /* 14 */
2462     {4,14,16}, /* 15 */
2463     {5,4,-1}, /* 16 */
2464     {4,5,6}, /* 17 */
2465     {6,5,-1}, /* 18 */
2466     {6,20,18}, /* 19 */
2467     {7,6,-1}, /* 20 */
2468     {6,7,8}, /* 21 */
2469     {7,8,-1}, /* 22 */
2470     {8,22,24}, /* 23 */
2471     {8,1,-1}, /* 24 */
2472     {24,9,10}, /* 25 */
2473     {9,10,-1}, /* 26 */
2474     {10,11,-1}, /* 27 */
2475     {27,11,29}, /* 28 */
2476     {11,12,-1}, /* 29 */
2477     {12,13,-1}, /* 30 */
2478     {12,13,14}, /* 31 */
2479     {13,14,-1}, /* 32 */
2480     {14,15,-1}, /* 33 */
2481     {33,15,35}, /* 34 */
2482     {16,15,-1}, /* 35 */
2483     {17,16,-1}, /* 36 */
2484     {18,17,16}, /* 37 */
2485     {18,17,-1}, /* 38 */
2486     {18,19,-1}, /* 39 */
2487     {41,19,39}, /* 40 */
2488     {19,20,-1}, /* 41 */
2489     {20,21,-1}, /* 42 */
2490     {20,21,22}, /* 43 */
2491     {21,22,-1}, /* 44 */
2492     {23,22,-1}, /* 45 */
2493     {45,47,23}, /* 46 */
2494     {23,24,-1}, /* 47 */
2495     {24,9,-1}}; /* 48 */
2496    
2497     /* Recursive routine to step back and see if we can
2498     * find a path to that monster that we found. If not,
2499     * we don't bother going toward it. Returns 1 if we
2500     * can see a direct way to get it
2501     * Modified to be map tile aware -.MSW
2502     */
2503    
2504    
2505     int can_see_monsterP(mapstruct *m, int x, int y,int dir) {
2506     sint16 dx, dy;
2507     int mflags;
2508    
2509     if(dir<0) return 0; /* exit condition: invalid direction */
2510    
2511     dx = x + freearr_x[dir];
2512     dy = y + freearr_y[dir];
2513    
2514     mflags = get_map_flags(m, &m, dx, dy, &dx, &dy);
2515    
2516     /* This functional arguably was incorrect before - it was
2517     * checking for P_WALL - that was basically seeing if
2518     * we could move to the monster - this is being more
2519     * literal on if we can see it. To know if we can actually
2520     * move to the monster, we'd need the monster passed in or
2521     * at least its move type.
2522     */
2523     if (mflags & (P_OUT_OF_MAP | P_BLOCKSVIEW)) return 0;
2524    
2525     /* yes, can see. */
2526     if(dir < 9) return 1;
2527     return can_see_monsterP(m, x, y, reduction_dir[dir][0]) |
2528     can_see_monsterP(m,x,y, reduction_dir[dir][1]) |
2529     can_see_monsterP(m,x,y, reduction_dir[dir][2]);
2530     }
2531    
2532    
2533    
2534     /*
2535     * can_pick(picker, item): finds out if an object is possible to be
2536     * picked up by the picker. Returnes 1 if it can be
2537     * picked up, otherwise 0.
2538     *
2539     * Cf 0.91.3 - don't let WIZ's pick up anything - will likely cause
2540     * core dumps if they do.
2541     *
2542     * Add a check so we can't pick up invisible objects (0.93.8)
2543     */
2544    
2545     int can_pick(object *who,object *item) {
2546     return /*QUERY_FLAG(who,FLAG_WIZ)||*/
2547     (item->weight>0&&!QUERY_FLAG(item,FLAG_NO_PICK)&&
2548     !QUERY_FLAG(item,FLAG_ALIVE)&&!item->invisible &&
2549     (who->type==PLAYER||item->weight<who->weight/3));
2550     }
2551    
2552    
2553     /*
2554     * create clone from object to another
2555     */
2556     object *object_create_clone (object *asrc) {
2557     object *dst = NULL,*tmp,*src,*part,*prev, *item;
2558    
2559     if(!asrc) return NULL;
2560     src = asrc;
2561     if(src->head)
2562     src = src->head;
2563    
2564     prev = NULL;
2565     for(part = src; part; part = part->more) {
2566     tmp = get_object();
2567     copy_object(part,tmp);
2568     tmp->x -= src->x;
2569     tmp->y -= src->y;
2570     if(!part->head) {
2571     dst = tmp;
2572     tmp->head = NULL;
2573     } else {
2574     tmp->head = dst;
2575     }
2576     tmp->more = NULL;
2577     if(prev)
2578     prev->more = tmp;
2579     prev = tmp;
2580     }
2581     /*** copy inventory ***/
2582     for(item = src->inv; item; item = item->below) {
2583     (void) insert_ob_in_ob(object_create_clone(item),dst);
2584     }
2585    
2586     return dst;
2587     }
2588    
2589     /* return true if the object was destroyed, 0 otherwise */
2590     int was_destroyed (object *op, tag_t old_tag)
2591     {
2592     /* checking for FLAG_FREED isn't necessary, but makes this function more
2593     * robust */
2594     return op->count != old_tag || QUERY_FLAG (op, FLAG_FREED);
2595     }
2596    
2597     /* GROS - Creates an object using a string representing its content. */
2598     /* Basically, we save the content of the string to a temp file, then call */
2599     /* load_object on it. I admit it is a highly inefficient way to make things, */
2600     /* but it was simple to make and allows reusing the load_object function. */
2601     /* Remember not to use load_object_str in a time-critical situation. */
2602     /* Also remember that multiparts objects are not supported for now. */
2603    
2604     object* load_object_str(char *obstr)
2605     {
2606     object *op;
2607     FILE *tempfile;
2608     char filename[MAX_BUF];
2609     sprintf(filename,"%s/cfloadobstr2044",settings.tmpdir);
2610     tempfile=fopen(filename,"w");
2611     if (tempfile == NULL)
2612     {
2613     LOG(llevError,"Error - Unable to access load object temp file\n");
2614     return NULL;
2615     };
2616     fprintf(tempfile,obstr);
2617     fclose(tempfile);
2618    
2619     op=get_object();
2620    
2621     tempfile=fopen(filename,"r");
2622     if (tempfile == NULL)
2623     {
2624     LOG(llevError,"Error - Unable to read object temp file\n");
2625     return NULL;
2626     };
2627     load_object(tempfile,op,LO_NEWFILE,0);
2628     LOG(llevDebug," load str completed, object=%s\n",op->name);
2629     CLEAR_FLAG(op,FLAG_REMOVED);
2630     fclose(tempfile);
2631     return op;
2632     }
2633    
2634     /* This returns the first object in who's inventory that
2635     * has the same type and subtype match.
2636     * returns NULL if no match.
2637     */
2638     object *find_obj_by_type_subtype(object *who, int type, int subtype)
2639     {
2640     object *tmp;
2641    
2642     for (tmp=who->inv; tmp; tmp=tmp->below)
2643     if (tmp->type == type && tmp->subtype == subtype) return tmp;
2644    
2645     return NULL;
2646     }
2647    
2648     /* If ob has a field named key, return the link from the list,
2649     * otherwise return NULL.
2650     *
2651     * key must be a passed in shared string - otherwise, this won't
2652     * do the desired thing.
2653     */
2654     key_value * get_ob_key_link(object * ob, const char * key) {
2655     key_value * link;
2656    
2657     for (link = ob->key_values; link != NULL; link = link->next) {
2658     if (link->key == key) {
2659     return link;
2660     }
2661     }
2662    
2663     return NULL;
2664     }
2665    
2666     /*
2667     * Returns the value of op has an extra_field for key, or NULL.
2668     *
2669     * The argument doesn't need to be a shared string.
2670     *
2671     * The returned string is shared.
2672     */
2673     const char * get_ob_key_value(object * op, const char * const key) {
2674     key_value * link;
2675     const char * canonical_key;
2676    
2677     canonical_key = find_string(key);
2678    
2679     if (canonical_key == NULL) {
2680     /* 1. There being a field named key on any object
2681     * implies there'd be a shared string to find.
2682     * 2. Since there isn't, no object has this field.
2683     * 3. Therefore, *this* object doesn't have this field.
2684     */
2685     return NULL;
2686     }
2687    
2688     /* This is copied from get_ob_key_link() above -
2689     * only 4 lines, and saves the function call overhead.
2690     */
2691     for (link = op->key_values; link != NULL; link = link->next) {
2692     if (link->key == canonical_key) {
2693     return link->value;
2694     }
2695     }
2696     return NULL;
2697     }
2698    
2699    
2700     /*
2701     * Updates the canonical_key in op to value.
2702     *
2703     * canonical_key is a shared string (value doesn't have to be).
2704     *
2705     * Unless add_key is TRUE, it won't add fields, only change the value of existing
2706     * keys.
2707     *
2708     * Returns TRUE on success.
2709     */
2710     int set_ob_key_value_s(object * op, const char * canonical_key, const char * value, int add_key) {
2711     key_value * field = NULL, *last=NULL;
2712    
2713     LOG(llevDebug, "set_ob_value_s: '%s' '%s' %d\n", canonical_key, value, add_key);
2714    
2715     for (field=op->key_values; field != NULL; field=field->next) {
2716     if (field->key != canonical_key) {
2717     last = field;
2718     continue;
2719     }
2720    
2721     if (field->value) FREE_AND_CLEAR_STR(field->value);
2722     if (value)
2723     field->value = add_string(value);
2724     else {
2725     /* Basically, if the archetype has this key set,
2726     * we need to store the null value so when we save
2727     * it, we save the empty value so that when we load,
2728     * we get this value back again.
2729     */
2730     if (get_ob_key_link(&op->arch->clone, canonical_key))
2731     field->value = NULL;
2732     else {
2733     /* Delete this link */
2734     if (field->key) FREE_AND_CLEAR_STR(field->key);
2735     if (field->value) FREE_AND_CLEAR_STR(field->value);
2736     if (last) last->next = field->next;
2737     else op->key_values = field->next;
2738     free(field);
2739     }
2740     }
2741     return TRUE;
2742     }
2743     /* IF we get here, key doesn't exist */
2744    
2745     /* No field, we'll have to add it. */
2746    
2747     if (!add_key) {
2748     return FALSE;
2749     }
2750     /* There isn't any good reason to store a null
2751     * value in the key/value list. If the archetype has
2752     * this key, then we should also have it, so shouldn't
2753     * be here. If user wants to store empty strings,
2754     * should pass in ""
2755     */
2756     if (value == NULL) return TRUE;
2757    
2758     field = malloc(sizeof(key_value));
2759    
2760     field->key = add_refcount(canonical_key);
2761     field->value = add_string(value);
2762     /* Usual prepend-addition. */
2763     field->next = op->key_values;
2764     op->key_values = field;
2765    
2766     return TRUE;
2767     }
2768    
2769     /*
2770     * Updates the key in op to value.
2771     *
2772     * If add_key is FALSE, this will only update existing keys,
2773     * and not add new ones.
2774     * In general, should be little reason FALSE is ever passed in for add_key
2775     *
2776     * Returns TRUE on success.
2777     */
2778     int set_ob_key_value(object * op, const char * key, const char * value, int add_key) {
2779     const char * canonical_key = NULL;
2780     int floating_ref = FALSE;
2781     int ret;
2782    
2783     /* HACK This mess is to make sure set_ob_value() passes a shared string
2784     * to get_ob_key_link(), without leaving a leaked refcount.
2785     */
2786    
2787     canonical_key = find_string(key);
2788     if (canonical_key == NULL) {
2789     canonical_key = add_string(key);
2790     floating_ref = TRUE;
2791     }
2792    
2793     ret = set_ob_key_value_s(op, canonical_key, value, add_key);
2794    
2795     if (floating_ref) {
2796     free_string(canonical_key);
2797     }
2798    
2799     return ret;
2800     }