ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.c
Revision: 1.1
Committed: Fri Feb 3 07:11:30 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

# Content
1 /*
2 * static char *rcsid_arch_c =
3 * "$Id: arch.c,v 1.35 2005/12/05 23:34:03 akirschbaum Exp $";
4 */
5
6 /*
7 CrossFire, A Multiplayer game for X-windows
8
9 Copyright (C) 2002 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 #include <global.h>
30 #include <arch.h>
31 #include <funcpoint.h>
32 #include <loader.h>
33
34 /* IF set, does a little timing on the archetype load. */
35 #define TIME_ARCH_LOAD 0
36
37 static archetype *arch_table[ARCHTABLE];
38 int arch_cmp=0; /* How many strcmp's */
39 int arch_search=0; /* How many searches */
40 int arch_init; /* True if doing arch initialization */
41
42 /* The naming of these functions is really poor - they are all
43 * pretty much named '.._arch_...', but they may more may not
44 * return archetypes. Some make the arch_to_object call, and thus
45 * return an object. Perhaps those should be called 'archob' functions
46 * to denote they return an object derived from the archetype.
47 * MSW 2003-04-29
48 */
49
50 /**
51 * GROS - This function retrieves an archetype given the name that appears
52 * during the game (for example, "writing pen" instead of "stylus").
53 * It does not use the hashtable system, but browse the whole archlist each time.
54 * I suggest not to use it unless you really need it because of performance issue.
55 * It is currently used by scripting extensions (create-object).
56 * Params:
57 * - name: the name we're searching for (ex: "writing pen");
58 * Return value:
59 * - the archetype found or null if nothing was found.
60 */
61 archetype *find_archetype_by_object_name(const char *name) {
62 archetype *at;
63
64 if (name == NULL)
65 return (archetype *) NULL;
66
67 for(at = first_archetype;at!=NULL;at=at->next) {
68 if (!strcmp(at->clone.name, name))
69 return at;
70 }
71 return NULL;
72 }
73
74 /* This is a lot like the above function. Instead, we are trying to match
75 * the arch->skill values. type is the type of object to match
76 * against (eg, to only match against skills or only skill objects for example).
77 * If type is -1, ew don't match on type.
78 */
79 object *get_archetype_by_skill_name(const char *skill, int type) {
80 archetype *at;
81
82 if (skill == NULL)
83 return NULL;
84
85 for(at = first_archetype;at!=NULL;at=at->next) {
86 if ( ((type == -1) || (type == at->clone.type)) &&
87 (!strcmp(at->clone.skill, skill)))
88 return arch_to_object(at);
89 }
90 return NULL;
91 }
92
93 /* similiar to above - this returns the first archetype
94 * that matches both the type and subtype. type and subtype
95 * can be -1 to say ignore, but in this case, the match it does
96 * may not be very useful. This function is most useful when
97 * subtypes are known to be unique for a particular type
98 * (eg, skills)
99 */
100 archetype *get_archetype_by_type_subtype(int type, int subtype) {
101 archetype *at;
102
103 for(at = first_archetype;at!=NULL;at=at->next) {
104 if ( ((type == -1) || (type == at->clone.type)) &&
105 (subtype == -1 || subtype == at->clone.subtype))
106 return at;
107 }
108 return NULL;
109 }
110
111 /**
112 * GROS - this returns a new object given the name that appears during the game
113 * (for example, "writing pen" instead of "stylus").
114 * Params:
115 * - name: The name we're searching for (ex: "writing pen");
116 * Return value:
117 * - a corresponding object if found; a singularity object if not found.
118 * Note by MSW - it appears that it takes the full name and keeps
119 * shortening it until it finds a match. I re-wrote this so that it
120 * doesn't malloc it each time - not that this function is used much,
121 * but it otherwise had a big memory leak.
122 */
123 object *get_archetype_by_object_name(const char *name) {
124 archetype *at;
125 char tmpname[MAX_BUF];
126 int i;
127
128 strncpy(tmpname,name,MAX_BUF-1);
129 tmpname[MAX_BUF-1] = 0;
130 for(i=strlen(tmpname); i>0; i--) {
131 tmpname[i] = 0;
132 at = find_archetype_by_object_name(tmpname);
133 if (at !=NULL)
134 {
135 return arch_to_object(at);
136 }
137 }
138 return create_singularity(name);
139 }
140
141 /* GROS - find_best_weapon_used_match and item_matched_string moved there */
142 object *find_best_weapon_used_match(object *pl, const char *params)
143 {
144 object *tmp, *best=NULL;
145 int match_val=0,tmpmatch;
146
147 for (tmp=pl->inv; tmp; tmp=tmp->below) {
148 if (tmp->invisible) continue;
149 if ((tmpmatch=item_matched_string(pl, tmp, params))>match_val)
150 {
151 if ((QUERY_FLAG(tmp, FLAG_APPLIED))&&(tmp->type==WEAPON))
152 {
153 match_val=tmpmatch;
154 best=tmp;
155 };
156 }
157 }
158 return best;
159 }
160
161 /* This is a subset of the parse_id command. Basically, name can be
162 * a string seperated lists of things to match, with certain keywords.
163 * pl is the player (only needed to set count properly)
164 * op is the item we are trying to match. Calling function takes care
165 * of what action might need to be done and if it is valid
166 * (pickup, drop, etc.) Return NONZERO if we have a match. A higher
167 * value means a better match. 0 means no match.
168 *
169 * Brief outline of the procedure:
170 * We take apart the name variable into the individual components.
171 * cases for 'all' and unpaid are pretty obvious.
172 * Next, we check for a count (either specified in name, or in the
173 * player object.)
174 * If count is 1, make a quick check on the name.
175 * IF count is >1, we need to make plural name. Return if match.
176 * Last, make a check on the full name.
177 */
178 int item_matched_string(object *pl, object *op, const char *name)
179 {
180 char *cp, local_name[MAX_BUF];
181 int count,retval=0;
182 strcpy(local_name, name); /* strtok is destructive to name */
183
184 for (cp=strtok(local_name,","); cp; cp=strtok(NULL,",")) {
185 while (cp[0]==' ') ++cp; /* get rid of spaces */
186
187 /* LOG(llevDebug,"Trying to match %s\n", cp);*/
188 /* All is a very generic match - low match value */
189 if (!strcmp(cp,"all")) return 1;
190
191 /* unpaid is a little more specific */
192 if (!strcmp(cp,"unpaid") && QUERY_FLAG(op,FLAG_UNPAID)) return 2;
193 if (!strcmp(cp,"cursed") && QUERY_FLAG(op,FLAG_KNOWN_CURSED) &&
194 (QUERY_FLAG(op,FLAG_CURSED) ||QUERY_FLAG(op,FLAG_DAMNED)))
195 return 2;
196
197 if (!strcmp(cp,"unlocked") && !QUERY_FLAG(op, FLAG_INV_LOCKED))
198 return 2;
199
200 /* Allow for things like '100 arrows' */
201 if ((count=atoi(cp))!=0) {
202 cp=strchr(cp, ' ');
203 while (cp && cp[0]==' ') ++cp; /* get rid of spaces */
204 }
205 else {
206 if (pl->type==PLAYER)
207 count=pl->contr->count;
208 else
209 count = 0;
210 }
211
212 if (!cp || cp[0]=='\0' || count<0) return 0;
213
214
215 /* The code here should go from highest retval to lowest. That
216 * is because of the 'else' handling - we don't want to match on
217 * something and set a low retval, even though it may match a higher retcal
218 * later. So keep it in descending order here, so we try for the best
219 * match first, and work downward.
220 */
221 if (!strcasecmp(cp,query_name(op))) retval=20;
222 else if (!strcasecmp(cp,query_short_name(op))) retval=18;
223 else if (!strcasecmp(cp,query_base_name(op,0))) retval=16;
224 else if (!strcasecmp(cp,query_base_name(op,1))) retval=16;
225 else if (op->custom_name && !strcasecmp(cp,op->custom_name)) retval=15;
226 else if (!strncasecmp(cp,query_base_name(op,0),
227 strlen(cp))) retval=14;
228 else if (!strncasecmp(cp,query_base_name(op,1),
229 strlen(cp))) retval=14;
230
231 /* Do substring checks, so things like 'Str+1' will match.
232 * retval of these should perhaps be lower - they are lower
233 * then the specific strcasecmp aboves, but still higher than
234 * some other match criteria.
235 */
236 else if (strstr(query_base_name(op,1), cp)) retval = 12;
237 else if (strstr(query_base_name(op,0), cp)) retval = 12;
238 else if (strstr(query_short_name(op), cp)) retval = 12;
239
240 /* Check against plural/non plural based on count. */
241 else if (count>1 && !strcasecmp(cp,op->name_pl)) {
242 retval=6;
243 }
244 else if (count==1 && !strcasecmp(op->name,cp)) {
245 retval=6;
246 }
247 /* base name matched - not bad */
248 else if (strcasecmp(cp,op->name)==0 && !count) retval=4;
249 /* Check for partial custom name, but give a real low priority */
250 else if (op->custom_name && strstr(op->custom_name, cp)) retval = 3;
251
252 if (retval) {
253 if (pl->type == PLAYER)
254 pl->contr->count=count;
255 return retval;
256 }
257 }
258 return 0;
259 }
260
261 /*
262 * Initialises the internal linked list of archetypes (read from file).
263 * Then the global "empty_archetype" pointer is initialised.
264 * Then the blocksview[] array is initialised.
265 */
266
267 void init_archetypes(void) { /* called from add_player() and edit() */
268 if(first_archetype!=NULL) /* Only do this once */
269 return;
270 arch_init = 1;
271 load_archetypes();
272 arch_init = 0;
273 empty_archetype=find_archetype("empty_archetype");
274 /* init_blocksview();*/
275 }
276
277 /*
278 * Stores debug-information about how efficient the hashtable
279 * used for archetypes has been in the static errmsg array.
280 */
281
282 void arch_info(object *op) {
283 sprintf(errmsg,"%d searches and %d strcmp()'s",arch_search,arch_cmp);
284 new_draw_info(NDI_BLACK, 0, op,errmsg);
285 }
286
287 /*
288 * Initialise the hashtable used by the archetypes.
289 */
290
291 void clear_archetable(void) {
292 memset((void *) arch_table,0,ARCHTABLE*sizeof(archetype *));
293 }
294
295 /*
296 * An alternative way to init the hashtable which is slower, but _works_...
297 */
298
299 void init_archetable(void) {
300 archetype *at;
301 LOG(llevDebug," Setting up archetable...");
302 for(at=first_archetype;at!=NULL;at=(at->more==NULL)?at->next:at->more)
303 add_arch(at);
304 LOG(llevDebug,"done\n");
305 }
306
307 /*
308 * Dumps an archetype to debug-level output.
309 */
310
311 void dump_arch(archetype *at) {
312 dump_object(&at->clone);
313 }
314
315 /*
316 * Dumps _all_ archetypes to debug-level output.
317 * If you run crossfire with debug, and enter DM-mode, you can trigger
318 * this with the O key.
319 */
320
321 void dump_all_archetypes(void) {
322 archetype *at;
323 for(at=first_archetype;at!=NULL;at=(at->more==NULL)?at->next:at->more) {
324 dump_arch(at);
325 fprintf(logfile, "%s\n", errmsg);
326 }
327 }
328
329 void free_all_archs(void)
330 {
331 archetype *at, *next;
332 int i=0,f=0;
333
334 for (at=first_archetype; at!=NULL; at=next) {
335 if (at->more) next=at->more;
336 else next=at->next;
337 if (at->name) free_string(at->name);
338 if (at->clone.name) free_string(at->clone.name);
339 if (at->clone.name_pl) free_string(at->clone.name_pl);
340 if (at->clone.title) free_string(at->clone.title);
341 if (at->clone.race) free_string(at->clone.race);
342 if (at->clone.slaying) free_string(at->clone.slaying);
343 if (at->clone.msg) free_string(at->clone.msg);
344 free(at);
345 i++;
346 }
347 LOG(llevDebug,"Freed %d archetypes, %d faces\n", i, f);
348 }
349
350 /*
351 * Allocates, initialises and returns the pointer to an archetype structure.
352 */
353
354 archetype *get_archetype_struct(void) {
355 archetype *new;
356
357 new=(archetype *)CALLOC(1,sizeof(archetype));
358 if(new==NULL)
359 fatal(OUT_OF_MEMORY);
360 new->next=NULL;
361 new->name=NULL;
362 new->clone.other_arch=NULL;
363 new->clone.name=NULL;
364 new->clone.name_pl=NULL;
365 new->clone.title=NULL;
366 new->clone.race=NULL;
367 new->clone.slaying=NULL;
368 new->clone.msg=NULL;
369 clear_object(&new->clone); /* to initial state other also */
370 CLEAR_FLAG((&new->clone),FLAG_FREED); /* This shouldn't matter, since copy_object() */
371 SET_FLAG((&new->clone), FLAG_REMOVED); /* doesn't copy these flags... */
372 new->head=NULL;
373 new->more=NULL;
374 return new;
375 }
376
377 /*
378 * Reads/parses the archetype-file, and copies into a linked list
379 * of archetype-structures.
380 */
381 void first_arch_pass(FILE *fp) {
382 object *op;
383 archetype *at,*head=NULL,*last_more=NULL;
384 int i,first=2;
385
386 op=get_object();
387 op->arch=first_archetype=at=get_archetype_struct();
388
389 while((i=load_object(fp,op,first,0))) {
390 first=0;
391 copy_object(op,&at->clone);
392 at->clone.speed_left= (float) (-0.1);
393 /* copy the body_info to the body_used - this is only really
394 * need for monsters, but doesn't hurt to do it for everything.
395 * by doing so, when a monster is created, it has good starting
396 * values for the body_used info, so when items are created
397 * for it, they can be properly equipped.
398 */
399 memcpy(&at->clone.body_used, &op->body_info, sizeof(op->body_info));
400
401 switch(i) {
402 case LL_NORMAL: /* A new archetype, just link it with the previous */
403 if(last_more!=NULL)
404 last_more->next=at;
405 if(head!=NULL)
406 head->next=at;
407 head=last_more=at;
408 #if 0
409 if(!op->type)
410 LOG(llevDebug," WARNING: Archetype %s has no type info!\n", op->arch->name);
411 #endif
412 at->tail_x = 0;
413 at->tail_y = 0;
414 break;
415
416 case LL_MORE: /* Another part of the previous archetype, link it correctly */
417
418 at->head=head;
419 at->clone.head = &head->clone;
420 if(last_more!=NULL) {
421 last_more->more=at;
422 last_more->clone.more = &at->clone;
423 }
424 last_more=at;
425
426 /* If this multipart image is still composed of individual small
427 * images, don't set the tail_.. values. We can't use them anyways,
428 * and setting these to zero makes the map sending to the client much
429 * easier as just looking at the head, we know what to do.
430 */
431 if (at->clone.face != head->clone.face) {
432 head->tail_x = 0;
433 head->tail_y = 0;
434 } else {
435 if (at->clone.x > head->tail_x) head->tail_x = at->clone.x;
436 if (at->clone.y > head->tail_y) head->tail_y = at->clone.y;
437 }
438 break;
439
440 }
441
442 at=get_archetype_struct();
443 clear_object(op);
444 op->arch=at;
445 }
446 free(at);
447 free_object(op);
448 }
449
450 /*
451 * Reads the archetype file once more, and links all pointers between
452 * archetypes.
453 */
454
455 void second_arch_pass(FILE *fp) {
456 char buf[MAX_BUF],*variable=buf,*argument,*cp;
457 archetype *at=NULL,*other;
458
459 while(fgets(buf,MAX_BUF,fp)!=NULL) {
460 if(*buf=='#')
461 continue;
462 if((argument=strchr(buf,' '))!=NULL) {
463 *argument='\0',argument++;
464 cp = argument + strlen(argument)-1;
465 while (isspace(*cp)) {
466 *cp='\0';
467 cp--;
468 }
469 }
470 if(!strcmp("Object",variable)) {
471 if((at=find_archetype(argument))==NULL)
472 LOG(llevError,"Warning: failed to find arch %s\n",argument);
473 } else if(!strcmp("other_arch",variable)) {
474 if(at!=NULL&&at->clone.other_arch==NULL) {
475 if((other=find_archetype(argument))==NULL)
476 LOG(llevError,"Warning: failed to find other_arch %s\n",argument);
477 else if(at!=NULL)
478 at->clone.other_arch=other;
479 }
480 } else if(!strcmp("randomitems",variable)) {
481 if(at!=NULL) {
482 treasurelist *tl=find_treasurelist(argument);
483 if(tl==NULL)
484 LOG(llevError,"Failed to link treasure to arch (%s): %s\n",at->name, argument);
485 else
486 at->clone.randomitems=tl;
487 }
488 }
489 }
490 }
491
492 #ifdef DEBUG
493 void check_generators(void) {
494 archetype *at;
495 for(at=first_archetype;at!=NULL;at=at->next)
496 if(QUERY_FLAG(&at->clone,FLAG_GENERATOR)&&at->clone.other_arch==NULL)
497 LOG(llevError,"Warning: %s is generator but lacks other_arch.\n",
498 at->name);
499 }
500 #endif
501
502 /*
503 * First initialises the archtype hash-table (init_archetable()).
504 * Reads and parses the archetype file (with the first and second-pass
505 * functions).
506 * Then initialises treasures by calling load_treasures().
507 */
508
509 void load_archetypes(void) {
510 FILE *fp;
511 char filename[MAX_BUF];
512 int comp;
513 #if TIME_ARCH_LOAD
514 struct timeval tv1,tv2;
515 #endif
516
517 sprintf(filename,"%s/%s",settings.datadir,settings.archetypes);
518 LOG(llevDebug,"Reading archetypes from %s...\n",filename);
519 if((fp=open_and_uncompress(filename,0,&comp))==NULL) {
520 LOG(llevError," Can't open archetype file.\n");
521 return;
522 }
523 clear_archetable();
524 LOG(llevDebug," arch-pass 1...\n");
525 #if TIME_ARCH_LOAD
526 GETTIMEOFDAY(&tv1);
527 #endif
528 first_arch_pass(fp);
529 #if TIME_ARCH_LOAD
530 { int sec, usec;
531 GETTIMEOFDAY(&tv2);
532 sec = tv2.tv_sec - tv1.tv_sec;
533 usec = tv2.tv_usec - tv1.tv_usec;
534 if (usec<0) { usec +=1000000; sec--;}
535 LOG(llevDebug,"Load took %d.%06d seconds\n", sec, usec);
536 }
537 #endif
538
539 LOG(llevDebug," done\n");
540 init_archetable();
541 warn_archetypes=1;
542
543 /* do a close and reopen instead of a rewind - necessary in case the
544 * file has been compressed.
545 */
546 close_and_delete(fp, comp);
547 fp=open_and_uncompress(filename,0,&comp);
548
549 LOG(llevDebug," loading treasure...\n");
550 load_treasures();
551 LOG(llevDebug," done\n arch-pass 2...\n");
552 second_arch_pass(fp);
553 LOG(llevDebug," done\n");
554 #ifdef DEBUG
555 check_generators();
556 #endif
557 close_and_delete(fp, comp);
558 LOG(llevDebug," done\n");
559 }
560
561 /*
562 * Creates and returns a new object which is a copy of the given archetype.
563 * This function returns NULL on failure.
564 */
565
566 object *arch_to_object(archetype *at) {
567 object *op;
568 if(at==NULL) {
569 if(warn_archetypes)
570 LOG(llevError,"Couldn't find archetype.\n");
571 return NULL;
572 }
573 op=get_object();
574 copy_object(&at->clone,op);
575 op->arch=at;
576 return op;
577 }
578
579 /*
580 * Creates an object. This function is called by get_archetype()
581 * if it fails to find the appropriate archetype.
582 * Thus get_archetype() will be guaranteed to always return
583 * an object, and never NULL.
584 */
585
586 object *create_singularity(const char *name) {
587 object *op;
588 char buf[MAX_BUF];
589 sprintf(buf,"%s (%s)",ARCH_SINGULARITY,name);
590 op = get_object();
591 op->name = add_string(buf);
592 op->name_pl = add_string(buf);
593 SET_FLAG(op,FLAG_NO_PICK);
594 return op;
595 }
596
597 /*
598 * Finds which archetype matches the given name, and returns a new
599 * object containing a copy of the archetype.
600 */
601
602 object *get_archetype(const char *name) {
603 archetype *at;
604 at = find_archetype(name);
605 if (at == NULL)
606 return create_singularity(name);
607 return arch_to_object(at);
608 }
609
610 /*
611 * Hash-function used by the arch-hashtable.
612 */
613
614 unsigned long
615 hasharch(const char *str, int tablesize) {
616 unsigned long hash = 0;
617 int i = 0;
618 unsigned rot = 0;
619 const char *p;
620
621 for (p = str; i < MAXSTRING && *p; p++, i++) {
622 hash ^= (unsigned long) *p << rot;
623 rot += 2;
624 if (rot >= (sizeof(long) - sizeof(char)) * 8)
625 rot = 0;
626 }
627 return (hash % tablesize);
628 }
629
630 /*
631 * Finds, using the hashtable, which archetype matches the given name.
632 * returns a pointer to the found archetype, otherwise NULL.
633 */
634
635 archetype *find_archetype(const char *name) {
636 archetype *at;
637 unsigned long index;
638
639 if (name == NULL)
640 return (archetype *) NULL;
641
642 index=hasharch(name, ARCHTABLE);
643 arch_search++;
644 for(;;) {
645 at = arch_table[index];
646 if (at==NULL) {
647 if(warn_archetypes)
648 LOG(llevError,"Couldn't find archetype %s\n",name);
649 return NULL;
650 }
651 arch_cmp++;
652 if (!strcmp(at->name,name))
653 return at;
654 if(++index>=ARCHTABLE)
655 index=0;
656 }
657 }
658
659 /*
660 * Adds an archetype to the hashtable.
661 */
662
663 void add_arch(archetype *at) {
664 int index=hasharch(at->name, ARCHTABLE),org_index=index;
665 for(;;) {
666 if(arch_table[index]==NULL) {
667 arch_table[index]=at;
668 return;
669 }
670 if(++index==ARCHTABLE)
671 index=0;
672 if(index==org_index)
673 fatal(ARCHTABLE_TOO_SMALL);
674 }
675 }
676
677 /*
678 * Returns the first archetype using the given type.
679 * Used in treasure-generation.
680 */
681
682 archetype *type_to_archetype(int type) {
683 archetype *at;
684
685 for(at=first_archetype;at!=NULL;at=(at->more==NULL)?at->next:at->more)
686 if(at->clone.type==type)
687 return at;
688 return NULL;
689 }
690
691 /*
692 * Returns a new object copied from the first archetype matching
693 * the given type.
694 * Used in treasure-generation.
695 */
696
697 object *clone_arch(int type) {
698 archetype *at;
699 object *op=get_object();
700
701 if((at=type_to_archetype(type))==NULL) {
702 LOG(llevError,"Can't clone archetype %d\n",type);
703 free_object(op);
704 return NULL;
705 }
706 copy_object(&at->clone,op);
707 return op;
708 }
709
710 /*
711 * member: make instance from class
712 */
713
714 object *object_create_arch (archetype * at)
715 {
716 object *op, *prev = NULL, *head = NULL;
717
718 while (at) {
719 op = arch_to_object (at);
720 op->x = at->clone.x;
721 op->y = at->clone.y;
722 if (head)
723 op->head = head, prev->more = op;
724 if (!head)
725 head = op;
726 prev = op;
727 at = at->more;
728 }
729 return (head);
730 }
731
732 /*** end of arch.c ***/