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.43 by root, Fri Feb 16 19:43:40 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 assert (("pippijn sagt es gibt keine tiles mehr", head->clone.face == more->clone.face));
379
380 if (more->clone.x > head->tail_x) head->tail_x = more->clone.x;
381 if (more->clone.y > head->tail_y) head->tail_y = more->clone.y;
382
383 more->head = head;
384 more->clone.head = &head->clone;
385 prev->more = more;
386 prev->clone.more = &more->clone;
387
388 prev = more;
389 }
390
391 head->next = first_archetype;
392 first_archetype = head;
393
394 return head;
361} 395}
362 396
363/* 397/*
364 * Reads/parses the archetype-file, and copies into a linked list 398 * Reads/parses the archetype-file, and copies into a linked list
365 * of archetype-structures. 399 * of archetype-structures.
366 */ 400 */
367void 401static bool
368first_arch_pass (object_thawer & fp) 402first_arch_pass (object_thawer &f)
369{ 403{
370 archetype *head = 0, *last_more = 0; 404 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 { 405 {
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) 406 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 } 407 {
408 case KW_object:
409 if (!archetype::read (f))
410 return false;
411 continue;
433 412
434 at = new archetype; 413 case KW_EOF:
414 return true;
435 415
436 at->clone.arch = at; 416 default:
437 } 417 if (!f.parse_error ("archetypes file"))
418 return false;
419 }
438 420
439 delete at; 421 f.next ();
422 }
440} 423}
441 424
442/* 425/*
443 * Reads the archetype file once more, and links all pointers between 426 * Reads the archetype file once more, and links all pointers between
444 * archetypes. 427 * archetypes.
445 */ 428 */
446
447void 429void
448second_arch_pass (object_thawer & thawer) 430second_arch_pass (object_thawer & thawer)
449{ 431{
450 char buf[MAX_BUF], *variable = buf, *argument, *cp; 432 char buf[MAX_BUF], *variable = buf, *argument, *cp;
451 archetype *at = NULL, *other; 433 archetype *at = NULL, *other;
452 434
453 while (fgets (buf, MAX_BUF, thawer) != NULL) 435 while (fgets (buf, MAX_BUF, thawer) != NULL)
454 { 436 {
455 if (*buf == '#') 437 if (*buf == '#')
456 continue; 438 continue;
439
457 if ((argument = strchr (buf, ' ')) != NULL) 440 if ((argument = strchr (buf, ' ')) != NULL)
458 { 441 {
459 *argument = '\0', argument++; 442 *argument = '\0', argument++;
460 cp = argument + strlen (argument) - 1; 443 cp = argument + strlen (argument) - 1;
461 while (isspace (*cp)) 444 while (isspace (*cp))
462 { 445 {
463 *cp = '\0'; 446 *cp = '\0';
464 cp--; 447 cp--;
465 } 448 }
466 } 449 }
450
467 if (!strcmp ("Object", variable)) 451 if (!strcmp ("object", variable))
468 { 452 {
469 if ((at = archetype::find (argument)) == NULL) 453 if ((at = archetype::find (argument)) == NULL)
470 LOG (llevError, "Warning: failed to find arch %s\n", argument); 454 LOG (llevError, "Warning: failed to find arch %s\n", argument);
471 } 455 }
472 else if (!strcmp ("other_arch", variable)) 456 else if (!strcmp ("other_arch", variable))
479 at->clone.other_arch = other; 463 at->clone.other_arch = other;
480 } 464 }
481 } 465 }
482 else if (!strcmp ("randomitems", variable)) 466 else if (!strcmp ("randomitems", variable))
483 { 467 {
484 if (at != NULL) 468 if (at)
485 { 469 {
486 treasurelist *tl = find_treasurelist (argument); 470 treasurelist *tl = find_treasurelist (argument);
487 471
488 if (tl == NULL) 472 if (tl == NULL)
489 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument); 473 LOG (llevError, "Failed to link treasure to arch (%s): %s\n", &at->name, argument);
520 504
521 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); 505 sprintf (filename, "%s/%s", settings.datadir, settings.archetypes);
522 LOG (llevDebug, "Reading archetypes from %s:\n", filename); 506 LOG (llevDebug, "Reading archetypes from %s:\n", filename);
523 507
524 { 508 {
525 object_thawer
526 thawer (filename); 509 object_thawer f (filename);
510
511 f.next ();
527 512
528 LOG (llevDebug, " arch-pass 1...\n"); 513 LOG (llevDebug, " arch-pass 1...\n");
529 first_arch_pass (thawer); 514 if (!first_arch_pass (f))
515 cleanup ("errors during first arch pass are fatal");
530 LOG (llevDebug, " done\n"); 516 LOG (llevDebug, " done\n");
531 } 517 }
532 518
533 init_archetable (); 519 init_archetable ();
534 warn_archetypes = 1; 520 warn_archetypes = 1;
535 521
536 { 522 {
537 object_thawer
538 thawer (filename); 523 object_thawer f (filename);
539 524
540 LOG (llevDebug, " loading treasure...\n"); 525 LOG (llevDebug, " loading treasure...\n");
541 load_treasures (); 526 load_treasures ();
542 LOG (llevDebug, " done\n"); 527 LOG (llevDebug, " done\n");
543 LOG (llevDebug, " arch-pass 2...\n"); 528 LOG (llevDebug, " arch-pass 2...\n");
544 second_arch_pass (thawer); 529 second_arch_pass (f);
545 LOG (llevDebug, " done\n"); 530 LOG (llevDebug, " done\n");
546#ifdef DEBUG 531#ifdef DEBUG
547 check_generators (); 532 check_generators ();
548#endif 533#endif
549 } 534 }
555 * This function returns NULL on failure. 540 * This function returns NULL on failure.
556 */ 541 */
557object * 542object *
558arch_to_object (archetype *at) 543arch_to_object (archetype *at)
559{ 544{
560 object *op; 545 if (!at)
561
562 if (at == NULL)
563 { 546 {
564 if (warn_archetypes) 547 if (warn_archetypes)
565 LOG (llevError, "Couldn't find archetype.\n"); 548 LOG (llevError, "Couldn't find archetype.\n");
566 549
567 return NULL; 550 return NULL;
568 } 551 }
569 552
570 op = at->clone.clone (); 553 object *op = at->clone.clone ();
571 op->arch = at; 554 op->arch = at;
572 op->instantiate (); 555 op->instantiate ();
573 return op; 556 return op;
574} 557}
575 558

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines