… | |
… | |
65 | * - the archetype found or null if nothing was found. |
65 | * - the archetype found or null if nothing was found. |
66 | */ |
66 | */ |
67 | archetype * |
67 | archetype * |
68 | find_archetype_by_object_name (const char *name) |
68 | find_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 | */ |
88 | archetype * |
84 | archetype * |
89 | find_archetype_by_object_type_name (int type, const char *name) |
85 | find_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 | */ |
110 | object * |
101 | object * |
111 | get_archetype_by_skill_name (const char *skill, int type) |
102 | get_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 | */ |
130 | archetype * |
120 | archetype * |
131 | get_archetype_by_type_subtype (int type, int subtype) |
121 | get_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 | */ |
156 | object * |
142 | object * |
157 | get_archetype_by_object_name (const char *name) |
143 | get_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 | } |
… | |
… | |
195 | item_matched_string (object *pl, object *op, const char *name) |
179 | item_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 | */ |
318 | void |
302 | void |
319 | init_archetable (void) |
303 | init_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 | |
331 | void |
314 | void |
… | |
… | |
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 | |
353 | archetype::archetype () |
336 | archetype::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 | |
359 | archetype::~archetype () |
344 | archetype::~archetype () |
360 | { |
345 | { |
|
|
346 | //TODO: nuke ->more's |
|
|
347 | } |
|
|
348 | |
|
|
349 | archetype * |
|
|
350 | archetype::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 | */ |
367 | void |
399 | static bool |
368 | first_arch_pass (object_thawer & fp) |
400 | first_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 | |
|
|
447 | void |
427 | void |
448 | second_arch_pass (object_thawer & thawer) |
428 | second_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); |
… | |
… | |
492 | } |
474 | } |
493 | } |
475 | } |
494 | } |
476 | } |
495 | } |
477 | } |
496 | |
478 | |
497 | #ifdef DEBUG |
|
|
498 | void |
|
|
499 | check_generators (void) |
|
|
500 | { |
|
|
501 | archetype *at; |
|
|
502 | |
|
|
503 | for (at = first_archetype; at != NULL; at = at->next) |
|
|
504 | if (QUERY_FLAG (&at->clone, FLAG_GENERATOR) && at->clone.other_arch == NULL) |
|
|
505 | LOG (llevError, "Warning: %s is generator but lacks other_arch.\n", &at->name); |
|
|
506 | } |
|
|
507 | #endif |
|
|
508 | |
|
|
509 | /* |
479 | /* |
510 | * First initialises the archtype hash-table (init_archetable()). |
480 | * First initialises the archtype hash-table (init_archetable()). |
511 | * Reads and parses the archetype file (with the first and second-pass |
481 | * Reads and parses the archetype file (with the first and second-pass |
512 | * functions). |
482 | * functions). |
513 | * Then initialises treasures by calling load_treasures(). |
483 | * Then initialises treasures by calling load_treasures(). |
514 | */ |
484 | */ |
515 | |
|
|
516 | void |
485 | void |
517 | load_archetypes (void) |
486 | load_archetypes (void) |
518 | { |
487 | { |
519 | char filename[MAX_BUF]; |
488 | char filename[MAX_BUF]; |
520 | |
489 | |
521 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
490 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
522 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
491 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
523 | |
492 | |
524 | { |
493 | { |
525 | object_thawer |
|
|
526 | thawer (filename); |
494 | object_thawer f (filename); |
|
|
495 | |
|
|
496 | f.next (); |
527 | |
497 | |
528 | LOG (llevDebug, " arch-pass 1...\n"); |
498 | LOG (llevDebug, " arch-pass 1...\n"); |
529 | first_arch_pass (thawer); |
499 | if (!first_arch_pass (f)) |
|
|
500 | cleanup ("errors during first arch pass are fatal"); |
530 | LOG (llevDebug, " done\n"); |
501 | LOG (llevDebug, " done\n"); |
531 | } |
502 | } |
532 | |
503 | |
533 | init_archetable (); |
504 | init_archetable (); |
534 | warn_archetypes = 1; |
505 | warn_archetypes = 1; |
535 | |
506 | |
536 | { |
507 | { |
537 | object_thawer |
|
|
538 | thawer (filename); |
508 | object_thawer f (filename); |
539 | |
509 | |
540 | LOG (llevDebug, " loading treasure...\n"); |
510 | LOG (llevDebug, " loading treasure...\n"); |
541 | load_treasures (); |
511 | load_treasures (); |
542 | LOG (llevDebug, " done\n"); |
512 | LOG (llevDebug, " done\n"); |
543 | LOG (llevDebug, " arch-pass 2...\n"); |
513 | LOG (llevDebug, " arch-pass 2...\n"); |
544 | second_arch_pass (thawer); |
514 | second_arch_pass (f); |
545 | LOG (llevDebug, " done\n"); |
515 | LOG (llevDebug, " done\n"); |
546 | #ifdef DEBUG |
|
|
547 | check_generators (); |
|
|
548 | #endif |
|
|
549 | } |
516 | } |
550 | LOG (llevDebug, " done\n"); |
517 | LOG (llevDebug, " done\n"); |
551 | } |
518 | } |
552 | |
519 | |
553 | /* |
520 | /* |
… | |
… | |
555 | * This function returns NULL on failure. |
522 | * This function returns NULL on failure. |
556 | */ |
523 | */ |
557 | object * |
524 | object * |
558 | arch_to_object (archetype *at) |
525 | arch_to_object (archetype *at) |
559 | { |
526 | { |
560 | object *op; |
527 | if (!at) |
561 | |
|
|
562 | if (at == NULL) |
|
|
563 | { |
528 | { |
564 | if (warn_archetypes) |
529 | if (warn_archetypes) |
565 | LOG (llevError, "Couldn't find archetype.\n"); |
530 | LOG (llevError, "Couldn't find archetype.\n"); |
566 | |
531 | |
567 | return NULL; |
532 | return NULL; |
568 | } |
533 | } |
569 | |
534 | |
570 | op = at->clone.clone (); |
535 | object *op = at->clone.clone (); |
571 | op->arch = at; |
536 | op->arch = at; |
572 | op->instantiate (); |
537 | op->instantiate (); |
573 | return op; |
538 | return op; |
574 | } |
539 | } |
575 | |
540 | |