1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game for X-windows |
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
9 | * it under the terms of the GNU General Public License as published by |
9 | * under the terms of the GNU General Public License as published by the Free |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * Software Foundation; either version 2 of the License, or (at your option) |
11 | * (at your option) any later version. |
11 | * any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, but |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
16 | * GNU General Public License for more details. |
16 | * for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License along |
19 | * along with this program; if not, write to the Free Software |
19 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | * |
21 | * |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | #include <cassert> |
25 | #include <cassert> |
26 | |
26 | |
27 | #include <global.h> |
27 | #include <global.h> |
… | |
… | |
50 | str_equal, |
50 | str_equal, |
51 | slice_allocator< std::pair<const char *const, arch_ptr> > |
51 | slice_allocator< std::pair<const char *const, arch_ptr> > |
52 | > HT; |
52 | > HT; |
53 | |
53 | |
54 | static HT ht (5000); |
54 | static HT ht (5000); |
55 | static std::vector<archetype *> archetypes; |
55 | archvec archetypes; |
56 | |
56 | |
57 | /** |
57 | /** |
58 | * GROS - This function retrieves an archetype given the name that appears |
58 | * GROS - This function retrieves an archetype given the name that appears |
59 | * during the game (for example, "writing pen" instead of "stylus"). |
59 | * during the game (for example, "writing pen" instead of "stylus"). |
60 | * It does not use the hashtable system, but browse the whole archlist each time. |
60 | * It does not use the hashtable system, but browse the whole archlist each time. |
… | |
… | |
275 | } |
275 | } |
276 | |
276 | |
277 | return 0; |
277 | return 0; |
278 | } |
278 | } |
279 | |
279 | |
280 | void |
|
|
281 | init_archetypes (void) |
|
|
282 | { |
|
|
283 | load_archetypes (); |
|
|
284 | |
|
|
285 | empty_archetype = archetype::find ("empty_archetype"); |
|
|
286 | } |
|
|
287 | |
|
|
288 | archetype::archetype () |
280 | archetype::archetype () |
289 | { |
281 | { |
290 | clone.arch = this; |
282 | clone.arch = this; |
291 | |
283 | |
292 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ |
284 | CLEAR_FLAG (&clone, FLAG_FREED); /* This shouldn't matter, since copy_to */ |
… | |
… | |
317 | // dire hack, need to rationalise |
309 | // dire hack, need to rationalise |
318 | void |
310 | void |
319 | overwrite (archetype *at, object *op) |
311 | overwrite (archetype *at, object *op) |
320 | { |
312 | { |
321 | at->clone = *op; |
313 | at->clone = *op; |
322 | |
|
|
323 | at->clone.arch = at; |
314 | at->clone.arch = at; |
324 | //TODO: inv et al. |
315 | |
|
|
316 | at->clone.inv = op->inv; op->inv = 0; |
|
|
317 | |
|
|
318 | op->destroy (); |
325 | } |
319 | } |
326 | |
320 | |
327 | archetype * |
321 | archetype * |
328 | archetype::read (object_thawer &f) |
322 | archetype::read (object_thawer &f) |
329 | { |
323 | { |
330 | assert (f.kw == KW_object); |
324 | assert (f.kw == KW_object); |
331 | |
325 | |
332 | archetype *head = get (f.get_str ()); |
326 | loading_arch = true; // hack to tell parse_kv et al. to behave |
333 | unlink (head); |
327 | |
|
|
328 | typedef std::pair<archetype *, object *> part; |
|
|
329 | std::vector<part> parts; |
|
|
330 | |
|
|
331 | coroapi::cede_to_tick_every (100); |
|
|
332 | |
|
|
333 | for (;;) |
|
|
334 | { |
334 | object *op = object::create (); |
335 | object *op = object::create (); |
335 | |
336 | archetype *at = get (f.get_str ()); |
336 | if (!op->parse_kv (f)) |
337 | f.get (op->name); |
337 | { |
|
|
338 | op->destroy (true); |
|
|
339 | // leak head |
|
|
340 | return 0; |
|
|
341 | } |
|
|
342 | |
|
|
343 | overwrite (head, op); |
|
|
344 | op->destroy (); |
|
|
345 | |
|
|
346 | for (archetype *prev = head; f.kw == KW_more; ) |
|
|
347 | { |
|
|
348 | f.next (); |
338 | f.next (); |
349 | assert (f.kw == KW_object); |
|
|
350 | |
339 | |
|
|
340 | #if 0 |
|
|
341 | if (f.kw == KW_inherit) |
|
|
342 | { |
351 | archetype *more = get (f.get_str ()); |
343 | if (archetype *at = find (f.get_str ())) |
352 | unlink (more); |
344 | *op = at->clone; |
353 | object *op = object::create (); |
345 | else |
|
|
346 | LOG (llevError, "archetype '%s' tries to inherit from non-existent archetype '%s'.\n", |
|
|
347 | &at->name, f.get_str ()); |
|
|
348 | |
|
|
349 | f.next (); |
|
|
350 | } |
|
|
351 | #endif |
354 | |
352 | |
355 | if (!op->parse_kv (f)) |
353 | if (!op->parse_kv (f)) |
|
|
354 | goto fail; |
|
|
355 | |
|
|
356 | op->post_load_check (); |
|
|
357 | |
|
|
358 | parts.push_back (std::make_pair (at, op)); |
|
|
359 | |
|
|
360 | if (f.kw != KW_more) |
|
|
361 | break; |
|
|
362 | |
|
|
363 | f.next (); |
|
|
364 | |
|
|
365 | if (f.kw != KW_object) |
356 | { |
366 | { |
357 | op->destroy (true); |
367 | f.parse_error ("more object"); |
358 | // leak head more* |
368 | goto fail; |
359 | return 0; |
|
|
360 | } |
369 | } |
361 | |
|
|
362 | overwrite (more, op); |
|
|
363 | op->destroy (); |
|
|
364 | |
|
|
365 | if (more->clone.x > head->tail_x) head->tail_x = more->clone.x; |
|
|
366 | if (more->clone.y > head->tail_y) head->tail_y = more->clone.y; |
|
|
367 | |
|
|
368 | more->head = head; |
|
|
369 | more->clone.head = &head->clone; |
|
|
370 | prev->more = more; |
|
|
371 | prev->clone.more = &more->clone; |
|
|
372 | |
|
|
373 | prev = more; |
|
|
374 | } |
370 | } |
375 | |
371 | |
376 | if (!head->next) |
372 | { |
|
|
373 | archetype *head = parts.front ().first; |
|
|
374 | |
|
|
375 | // check that all archetypes belong to the same object or are heads |
|
|
376 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
377 | { |
377 | { |
|
|
378 | archetype *at = p->first; |
|
|
379 | |
|
|
380 | if (at->head != head && at->head) |
|
|
381 | { |
|
|
382 | LOG (llevError, "%s: unable to overwrite foreign non-head archetype with non-head archetype\n", &at->archname); |
|
|
383 | goto fail; |
|
|
384 | } |
|
|
385 | |
|
|
386 | if (at->next && at != head) |
|
|
387 | { |
|
|
388 | LOG (llevError, "%s: unable to overwrite foreign head archetype with non-head archetype\n", &at->archname); |
|
|
389 | goto fail; |
|
|
390 | } |
|
|
391 | } |
|
|
392 | |
|
|
393 | // sever chain of existing head object |
|
|
394 | for (archetype *more, *at = head; at; at = more) |
|
|
395 | { |
|
|
396 | more = at->more; |
|
|
397 | |
|
|
398 | at->head = 0; |
|
|
399 | at->more = 0; |
|
|
400 | } |
|
|
401 | |
|
|
402 | // replace/update head |
|
|
403 | overwrite (head, parts.front ().second); |
|
|
404 | head->tail_x = 0; |
|
|
405 | head->tail_y = 0; |
|
|
406 | |
|
|
407 | // link into list of heads, if not already there |
|
|
408 | if (!head->linked) |
|
|
409 | { |
|
|
410 | head->linked = true; |
378 | head->next = first_archetype; |
411 | head->next = first_archetype; |
379 | first_archetype = head; |
412 | first_archetype = head; |
380 | } |
413 | } |
381 | |
414 | |
|
|
415 | // reassemble new chain |
|
|
416 | archetype *prev = head; |
|
|
417 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
|
|
418 | { |
|
|
419 | archetype *at = p->first; |
|
|
420 | overwrite (at, p->second); |
|
|
421 | |
|
|
422 | if (at->clone.x > head->tail_x) head->tail_x = at->clone.x; |
|
|
423 | if (at->clone.y > head->tail_y) head->tail_y = at->clone.y; |
|
|
424 | |
|
|
425 | at->head = head; |
|
|
426 | at->clone.head = &head->clone; |
|
|
427 | prev->more = at; |
|
|
428 | prev->clone.more = &at->clone; |
|
|
429 | |
|
|
430 | prev = at; |
|
|
431 | } |
|
|
432 | |
|
|
433 | loading_arch = false; |
382 | return head; |
434 | return head; |
383 | } |
435 | } |
384 | |
436 | |
385 | /* |
437 | fail: |
386 | * Reads/parses the archetype-file, and copies into a linked list |
438 | for (auto (p, parts.begin ()); p != parts.end (); ++p) |
387 | * of archetype-structures. |
439 | p->second->destroy (true); |
388 | */ |
|
|
389 | static bool |
|
|
390 | load_archetypes (object_thawer &f) |
|
|
391 | { |
|
|
392 | for (;;) |
|
|
393 | { |
|
|
394 | switch (f.kw) |
|
|
395 | { |
|
|
396 | case KW_object: |
|
|
397 | loading_arch = true; |
|
|
398 | if (!archetype::read (f)) |
|
|
399 | { |
|
|
400 | loading_arch = false; |
|
|
401 | return false; |
|
|
402 | } |
|
|
403 | |
440 | |
404 | loading_arch = false; |
441 | loading_arch = false; |
405 | continue; |
442 | return 0; |
406 | |
|
|
407 | case KW_EOF: |
|
|
408 | return true; |
|
|
409 | |
|
|
410 | default: |
|
|
411 | if (!f.parse_error ("archetypes file")) |
|
|
412 | return false; |
|
|
413 | } |
|
|
414 | |
|
|
415 | f.next (); |
|
|
416 | } |
|
|
417 | } |
443 | } |
418 | |
444 | |
419 | /* |
445 | /* |
420 | * First initialises the archtype hash-table (init_archetable()). |
446 | * Initialize global archtype pointers: |
421 | * Reads and parses the archetype file (with the first and second-pass |
|
|
422 | * functions). |
|
|
423 | */ |
447 | */ |
424 | void |
448 | void |
425 | load_archetypes (void) |
449 | init_archetype_pointers () |
426 | { |
450 | { |
427 | char filename[MAX_BUF]; |
451 | ring_arch = archetype::find ("ring"); |
428 | |
452 | amulet_arch = archetype::find ("amulet"); |
429 | sprintf (filename, "%s/%s", settings.datadir, settings.archetypes); |
453 | staff_arch = archetype::find ("staff"); |
430 | LOG (llevDebug, "Reading archetypes from %s:\n", filename); |
454 | crown_arch = archetype::find ("crown"); |
431 | |
455 | empty_archetype = archetype::find ("empty_archetype"); |
432 | object_thawer f (filename); |
|
|
433 | |
|
|
434 | f.next (); |
|
|
435 | |
|
|
436 | if (!load_archetypes (f)) |
|
|
437 | cleanup ("unable to load archetypes"); |
|
|
438 | |
|
|
439 | warn_archetypes = 1; |
|
|
440 | |
|
|
441 | load_treasures (); |
|
|
442 | } |
456 | } |
443 | |
457 | |
444 | /* |
458 | /* |
445 | * Creates and returns a new object which is a copy of the given archetype. |
459 | * Creates and returns a new object which is a copy of the given archetype. |
446 | * This function returns NULL on failure. |
460 | * This function returns NULL on failure. |
… | |
… | |
448 | object * |
462 | object * |
449 | arch_to_object (archetype *at) |
463 | arch_to_object (archetype *at) |
450 | { |
464 | { |
451 | if (!at) |
465 | if (!at) |
452 | { |
466 | { |
453 | if (warn_archetypes) |
|
|
454 | LOG (llevError, "Couldn't find archetype.\n"); |
467 | LOG (llevError, "Couldn't find archetype.\n"); |
455 | |
|
|
456 | return NULL; |
468 | return 0; |
457 | } |
469 | } |
458 | |
470 | |
459 | object *op = at->clone.clone (); |
471 | object *op = at->clone.clone (); |
460 | op->arch = at; |
472 | op->arch = at; |
461 | op->instantiate (); |
473 | op->instantiate (); |
|
|
474 | |
462 | return op; |
475 | return op; |
463 | } |
476 | } |
464 | |
477 | |
465 | /* |
478 | /* |
466 | * Creates an object. This function is called by get_archetype() |
479 | * Creates an object. This function is called by get_archetype() |
… | |
… | |
495 | |
508 | |
496 | return arch_to_object (at); |
509 | return arch_to_object (at); |
497 | } |
510 | } |
498 | |
511 | |
499 | /* |
512 | /* |
500 | * Hash-function used by the arch-hashtable. |
|
|
501 | */ |
|
|
502 | |
|
|
503 | unsigned long |
|
|
504 | hasharch (const char *str, int tablesize) |
|
|
505 | { |
|
|
506 | unsigned long hash = 0; |
|
|
507 | unsigned int i = 0; |
|
|
508 | const char *p; |
|
|
509 | |
|
|
510 | /* use the one-at-a-time hash function, which supposedly is |
|
|
511 | * better than the djb2-like one used by perl5.005, but |
|
|
512 | * certainly is better then the bug used here before. |
|
|
513 | * see http://burtleburtle.net/bob/hash/doobs.html |
|
|
514 | */ |
|
|
515 | for (p = str; i < MAXSTRING && *p; p++, i++) |
|
|
516 | { |
|
|
517 | hash += *p; |
|
|
518 | hash += hash << 10; |
|
|
519 | hash ^= hash >> 6; |
|
|
520 | } |
|
|
521 | |
|
|
522 | hash += hash << 3; |
|
|
523 | hash ^= hash >> 11; |
|
|
524 | hash += hash << 15; |
|
|
525 | |
|
|
526 | return hash % tablesize; |
|
|
527 | } |
|
|
528 | |
|
|
529 | /* |
|
|
530 | * Finds, using the hashtable, which archetype matches the given name. |
513 | * Finds, using the hashtable, which archetype matches the given name. |
531 | * returns a pointer to the found archetype, otherwise NULL. |
514 | * returns a pointer to the found archetype, otherwise NULL. |
532 | */ |
515 | */ |
533 | archetype * |
516 | archetype * |
534 | archetype::find (const char *name) |
517 | archetype::find (const char *name) |
535 | { |
518 | { |
536 | if (!name) |
519 | if (!name) |
537 | return 0; |
520 | return 0; |
538 | |
521 | |
539 | AUTODECL (i, ht.find (name)); |
522 | auto (i, ht.find (name)); |
540 | |
523 | |
541 | if (i == ht.end ()) |
524 | if (i == ht.end ()) |
542 | return 0; |
525 | return 0; |
543 | else |
526 | else |
544 | return i->second; |
527 | return i->second; |
545 | } |
528 | } |
546 | |
529 | |
547 | archetype * |
530 | archetype * |
548 | archetype::get (const char *name) |
531 | archetype::get (const char *name) |
549 | { |
532 | { |
|
|
533 | if (!name) |
|
|
534 | { |
|
|
535 | LOG (llevError, "null archetype requested\n"); |
|
|
536 | name = "(null)"; |
|
|
537 | } |
|
|
538 | |
550 | archetype *at = find (name); |
539 | archetype *at = find (name); |
551 | |
540 | |
552 | if (!at) |
541 | if (!at) |
553 | { |
542 | { |
554 | archetypes.push_back (at = new archetype); |
543 | archetypes.insert (at = new archetype); |
555 | at->name = name; |
544 | at->archname = at->clone.name = at->clone.name_pl = name; |
556 | at->hash_add (); |
545 | at->hash_add (); |
557 | } |
546 | } |
558 | |
547 | |
559 | return at; |
548 | return at; |
560 | } |
549 | } |
… | |
… | |
563 | * Adds an archetype to the hashtable. |
552 | * Adds an archetype to the hashtable. |
564 | */ |
553 | */ |
565 | void |
554 | void |
566 | archetype::hash_add () |
555 | archetype::hash_add () |
567 | { |
556 | { |
568 | ht.insert (std::make_pair (name, this)); |
557 | ht.insert (std::make_pair (archname, this)); |
569 | } |
558 | } |
570 | |
559 | |
571 | void |
560 | void |
572 | archetype::hash_del () |
561 | archetype::hash_del () |
573 | { |
562 | { |
574 | ht.erase (name); |
563 | ht.erase (archname); |
575 | } |
564 | } |
576 | |
565 | |
577 | /* |
566 | /* |
578 | * Returns the first archetype using the given type. |
567 | * Returns the first archetype using the given type. |
579 | * Used in treasure-generation. |
568 | * Used in treasure-generation. |