ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/arch.C
(Generate patch)

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.38 by root, Fri Feb 2 21:50:43 2007 UTC vs.
Revision 1.44 by pippijn, Thu Mar 1 11:14:45 2007 UTC

65 * - the archetype found or null if nothing was found. 65 * - the archetype found or null if nothing was found.
66 */ 66 */
67archetype * 67archetype *
68find_archetype_by_object_name (const char *name) 68find_archetype_by_object_name (const char *name)
69{ 69{
70 archetype *at; 70 shstr_cmp name_cmp (name);
71 71
72 if (name == NULL)
73 return (archetype *) NULL;
74
75 for (at = first_archetype; at != NULL; at = at->next) 72 for (archetype *at = first_archetype; at; at = at->next)
76 { 73 if (at->clone.name == name_cmp)
77 if (!strcmp (at->clone.name, name))
78 return at; 74 return at;
79 } 75
80 return NULL; 76 return 0;
81} 77}
82 78
83/** 79/**
84 * This function retrieves an archetype by type and name that appears during 80 * This function retrieves an archetype by type and name that appears during
85 * the game. It is basically the same as find_archetype_by_object_name() 81 * the game. It is basically the same as find_archetype_by_object_name()
86 * except that it considers only items of the given type. 82 * except that it considers only items of the given type.
87 */ 83 */
88archetype * 84archetype *
89find_archetype_by_object_type_name (int type, const char *name) 85find_archetype_by_object_type_name (int type, const char *name)
90{ 86{
91 archetype *at; 87 shstr_cmp name_cmp (name);
92 88
93 if (name == NULL)
94 return NULL;
95
96 for (at = first_archetype; at != NULL; at = at->next) 89 for (archetype *at = first_archetype; at; at = at->next)
97 { 90 if (at->clone.name == name_cmp && at->clone.type == type)
98 if (at->clone.type == type && strcmp (at->clone.name, name) == 0)
99 return at; 91 return at;
100 }
101 92
102 return NULL; 93 return 0;
103} 94}
104 95
105/* This is a lot like the above function. Instead, we are trying to match 96/* This is a lot like the above function. Instead, we are trying to match
106 * the arch->skill values. type is the type of object to match 97 * the arch->skill values. type is the type of object to match
107 * against (eg, to only match against skills or only skill objects for example). 98 * against (eg, to only match against skills or only skill objects for example).
108 * If type is -1, ew don't match on type. 99 * If type is -1, ew don't match on type.
109 */ 100 */
110object * 101object *
111get_archetype_by_skill_name (const char *skill, int type) 102get_archetype_by_skill_name (const char *skill, int type)
112{ 103{
113 archetype *at; 104 shstr_cmp skill_cmp (skill);
114 105
115 if (skill)
116 for (at = first_archetype; at; at = at->next) 106 for (archetype *at = first_archetype; at; at = at->next)
117 if (((type == -1) || (type == at->clone.type)) && (!strcmp (at->clone.skill, skill))) 107 if (at->clone.skill == skill_cmp && (type == -1 || type == at->clone.type))
118 return arch_to_object (at); 108 return arch_to_object (at);
119 109
120 return 0; 110 return 0;
121} 111}
122 112
123/* similiar to above - this returns the first archetype 113/* similiar to above - this returns the first archetype
128 * (eg, skills) 118 * (eg, skills)
129 */ 119 */
130archetype * 120archetype *
131get_archetype_by_type_subtype (int type, int subtype) 121get_archetype_by_type_subtype (int type, int subtype)
132{ 122{
133 archetype *
134 at;
135
136 for (at = first_archetype; at != NULL; at = at->next) 123 for (archetype *at = first_archetype; at; at = at->next)
137 {
138 if (((type == -1) || (type == at->clone.type)) && (subtype == -1 || subtype == at->clone.subtype)) 124 if ((type == -1 || type == at->clone.type) && (subtype == -1 || subtype == at->clone.subtype))
139 return at; 125 return at;
140 } 126
141 return NULL; 127 return 0;
142} 128}
143 129
144/** 130/**
145 * GROS - this returns a new object given the name that appears during the game 131 * GROS - this returns a new object given the name that appears during the game
146 * (for example, "writing pen" instead of "stylus"). 132 * (for example, "writing pen" instead of "stylus").
154 * but it otherwise had a big memory leak. 140 * but it otherwise had a big memory leak.
155 */ 141 */
156object * 142object *
157get_archetype_by_object_name (const char *name) 143get_archetype_by_object_name (const char *name)
158{ 144{
159 archetype *at;
160 char tmpname[MAX_BUF]; 145 char tmpname[MAX_BUF];
161 int i; 146 int i;
162 147
163 assign (tmpname, name); 148 assign (tmpname, name);
164 149
165 for (i = strlen (tmpname); i > 0; i--) 150 for (i = strlen (tmpname); i > 0; i--)
166 { 151 {
167 tmpname[i] = 0; 152 tmpname[i] = 0;
153
168 at = find_archetype_by_object_name (tmpname); 154 if (archetype *at = find_archetype_by_object_name (tmpname))
169
170 if (at)
171 return arch_to_object (at); 155 return arch_to_object (at);
172 } 156 }
173 157
174 return create_singularity (name); 158 return create_singularity (name);
175} 159}
195item_matched_string (object *pl, object *op, const char *name) 179item_matched_string (object *pl, object *op, const char *name)
196{ 180{
197 char *cp, local_name[MAX_BUF]; 181 char *cp, local_name[MAX_BUF];
198 int count, retval = 0; 182 int count, retval = 0;
199 183
200 strcpy (local_name, name); /* strtok is destructive to name */ 184 assign (local_name, name); /* strtok is destructive to name */
201 185
202 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ",")) 186 for (cp = strtok (local_name, ","); cp; cp = strtok (NULL, ","))
203 { 187 {
204 while (cp[0] == ' ') 188 while (cp[0] == ' ')
205 ++cp; /* get rid of spaces */ 189 ++cp; /* get rid of spaces */
316 * An alternative way to init the hashtable which is slower, but _works_... 300 * An alternative way to init the hashtable which is slower, but _works_...
317 */ 301 */
318void 302void
319init_archetable (void) 303init_archetable (void)
320{ 304{
321 archetype *at;
322
323 LOG (llevDebug, " Setting up archetable...\n"); 305 LOG (llevDebug, " Setting up archetable...\n");
324 306
325 for (at = first_archetype; at; at = at->more ? at->more : at->next) 307 for (archetype *at = first_archetype; at; at = at->next)
308 for (archetype *bt = at; bt; bt = bt->more)
326 at->hash_add (); 309 bt->hash_add ();
327 310
328 LOG (llevDebug, "done\n"); 311 LOG (llevDebug, "done\n");
329} 312}
330 313
331void 314void
350 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f); 333 LOG (llevDebug, "Freed %d archetypes, %d faces\n", i, f);
351} 334}
352 335
353archetype::archetype () 336archetype::archetype ()
354{ 337{
338 clone.arch = this;
339
355 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ 340 CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */
356 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */ 341 SET_FLAG (&clone, FLAG_REMOVED); /* doesn't copy these flags... */
357} 342}
358 343
359archetype::~archetype () 344archetype::~archetype ()
360{ 345{
346 //TODO: nuke ->more's
347}
348
349archetype *
350archetype::read (object_thawer &f)
351{
352 assert (f.kw == KW_object);
353
354 archetype *head = new archetype;
355 f.get (head->name);
356
357 if (!head->clone.parse_kv (f))
358 {
359 delete head;
360 return 0;
361 }
362
363 for (archetype *prev = head; f.kw == KW_more; )
364 {
365 f.next ();
366
367 assert (f.kw == KW_object);
368
369 archetype *more = new archetype;
370 f.get (more->name);
371
372 if (!more->clone.parse_kv (f))
373 {
374 delete head;
375 return 0;
376 }
377
378 if (more->clone.x > head->tail_x) head->tail_x = more->clone.x;
379 if (more->clone.y > head->tail_y) head->tail_y = more->clone.y;
380
381 more->head = head;
382 more->clone.head = &head->clone;
383 prev->more = more;
384 prev->clone.more = &more->clone;
385
386 prev = more;
387 }
388
389 head->next = first_archetype;
390 first_archetype = head;
391
392 return head;
361} 393}
362 394
363/* 395/*
364 * Reads/parses the archetype-file, and copies into a linked list 396 * Reads/parses the archetype-file, and copies into a linked list
365 * of archetype-structures. 397 * of archetype-structures.
366 */ 398 */
367void 399static bool
368first_arch_pass (object_thawer & fp) 400first_arch_pass (object_thawer &f)
369{ 401{
370 archetype *head = 0, *last_more = 0; 402 for (;;)
371
372 archetype *at = new archetype;
373 at->clone.arch = first_archetype = at;
374
375 while (int i = load_object (fp, &at->clone, 0))
376 { 403 {
377 at->clone.speed_left = (float) (-0.1);
378 /* copy the body_info to the body_used - this is only really
379 * need for monsters, but doesn't hurt to do it for everything.
380 * by doing so, when a monster is created, it has good starting
381 * values for the body_used info, so when items are created
382 * for it, they can be properly equipped.
383 */
384 memcpy (&at->clone.body_used, &at->clone.body_info, sizeof (at->clone.body_info));
385
386 switch (i) 404 switch (f.kw)
387 {
388 case LL_NORMAL: /* A new archetype, just link it with the previous */
389 if (last_more != NULL)
390 last_more->next = at;
391 if (head != NULL)
392 head->next = at;
393 head = last_more = at;
394#if 0
395 if (!op->type)
396 LOG (llevDebug, " WARNING: Archetype %s has no type info!\n", op->arch->name);
397#endif
398 at->tail_x = 0;
399 at->tail_y = 0;
400 break;
401
402 case LL_MORE: /* Another part of the previous archetype, link it correctly */
403
404 at->head = head;
405 at->clone.head = &head->clone;
406 if (last_more != NULL)
407 {
408 last_more->more = at;
409 last_more->clone.more = &at->clone;
410 }
411 last_more = at;
412
413 /* If this multipart image is still composed of individual small
414 * images, don't set the tail_.. values. We can't use them anyways,
415 * and setting these to zero makes the map sending to the client much
416 * easier as just looking at the head, we know what to do.
417 */
418 if (at->clone.face != head->clone.face)
419 {
420 head->tail_x = 0;
421 head->tail_y = 0;
422 }
423 else
424 {
425 if (at->clone.x > head->tail_x)
426 head->tail_x = at->clone.x;
427 if (at->clone.y > head->tail_y)
428 head->tail_y = at->clone.y;
429 }
430 break;
431
432 } 405 {
406 case KW_object:
407 if (!archetype::read (f))
408 return false;
409 continue;
433 410
434 at = new archetype; 411 case KW_EOF:
412 return true;
435 413
436 at->clone.arch = at; 414 default:
437 } 415 if (!f.parse_error ("archetypes file"))
416 return false;
417 }
438 418
439 delete at; 419 f.next ();
420 }
440} 421}
441 422
442/* 423/*
443 * Reads the archetype file once more, and links all pointers between 424 * Reads the archetype file once more, and links all pointers between
444 * archetypes. 425 * archetypes.
445 */ 426 */
446
447void 427void
448second_arch_pass (object_thawer & thawer) 428second_arch_pass (object_thawer & thawer)
449{ 429{
450 char buf[MAX_BUF], *variable = buf, *argument, *cp; 430 char buf[MAX_BUF], *variable = buf, *argument, *cp;
451 archetype *at = NULL, *other; 431 archetype *at = NULL, *other;
452 432
453 while (fgets (buf, MAX_BUF, thawer) != NULL) 433 while (fgets (buf, MAX_BUF, thawer) != NULL)
454 { 434 {
455 if (*buf == '#') 435 if (*buf == '#')
456 continue; 436 continue;
437
457 if ((argument = strchr (buf, ' ')) != NULL) 438 if ((argument = strchr (buf, ' ')) != NULL)
458 { 439 {
459 *argument = '\0', argument++; 440 *argument = '\0', argument++;
460 cp = argument + strlen (argument) - 1; 441 cp = argument + strlen (argument) - 1;
461 while (isspace (*cp)) 442 while (isspace (*cp))
462 { 443 {
463 *cp = '\0'; 444 *cp = '\0';
464 cp--; 445 cp--;
465 } 446 }
466 } 447 }
448
467 if (!strcmp ("Object", variable)) 449 if (!strcmp ("object", variable))
468 { 450 {
469 if ((at = archetype::find (argument)) == NULL) 451 if ((at = archetype::find (argument)) == NULL)
470 LOG (llevError, "Warning: failed to find arch %s\n", argument); 452 LOG (llevError, "Warning: failed to find arch %s\n", argument);
471 } 453 }
472 else if (!strcmp ("other_arch", variable)) 454 else if (!strcmp ("other_arch", variable))
479 at->clone.other_arch = other; 461 at->clone.other_arch = other;
480 } 462 }
481 } 463 }
482 else if (!strcmp ("randomitems", variable)) 464 else if (!strcmp ("randomitems", variable))
483 { 465 {
484 if (at != NULL) 466 if (at)
485 { 467 {
486 treasurelist *tl = find_treasurelist (argument); 468 treasurelist *tl = find_treasurelist (argument);
487 469
488 if (tl == NULL) 470 if (tl == NULL)
489 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 471 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
520 502
521 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 503 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
522 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 504 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
523 505
524 { 506 {
525 object_thawer
526 thawer (filename); 507 object_thawer f (filename);
508
509 f.next ();
527 510
528 LOG (llevDebug, " arch-pass 1...\n"); 511 LOG (llevDebug, " arch-pass 1...\n");
529 first_arch_pass (thawer); 512 if (!first_arch_pass (f))
513 cleanup ("errors during first arch pass are fatal");
530 LOG (llevDebug, " done\n"); 514 LOG (llevDebug, " done\n");
531 } 515 }
532 516
533 init_archetable (); 517 init_archetable ();
534 warn_archetypes = 1; 518 warn_archetypes = 1;
535 519
536 { 520 {
537 object_thawer
538 thawer (filename); 521 object_thawer f (filename);
539 522
540 LOG (llevDebug, " loading treasure...\n"); 523 LOG (llevDebug, " loading treasure...\n");
541 load_treasures (); 524 load_treasures ();
542 LOG (llevDebug, " done\n"); 525 LOG (llevDebug, " done\n");
543 LOG (llevDebug, " arch-pass 2...\n"); 526 LOG (llevDebug, " arch-pass 2...\n");
544 second_arch_pass (thawer); 527 second_arch_pass (f);
545 LOG (llevDebug, " done\n"); 528 LOG (llevDebug, " done\n");
546#ifdef DEBUG 529#ifdef DEBUG
547 check_generators (); 530 check_generators ();
548#endif 531#endif
549 } 532 }
555 * This function returns NULL on failure. 538 * This function returns NULL on failure.
556 */ 539 */
557object * 540object *
558arch_to_object (archetype *at) 541arch_to_object (archetype *at)
559{ 542{
560 object *op; 543 if (!at)
561
562 if (at == NULL)
563 { 544 {
564 if (warn_archetypes) 545 if (warn_archetypes)
565 LOG (llevError, "Couldn't find archetype.\n"); 546 LOG (llevError, "Couldn't find archetype.\n");
566 547
567 return NULL; 548 return NULL;
568 } 549 }
569 550
570 op = at->clone.clone (); 551 object *op = at->clone.clone ();
571 op->arch = at; 552 op->arch = at;
572 op->instantiate (); 553 op->instantiate ();
573 return op; 554 return op;
574} 555}
575 556

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines