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

Comparing deliantra/server/common/loader.C (file contents):
Revision 1.50 by root, Fri Jan 19 18:06:51 2007 UTC vs.
Revision 1.58 by root, Thu Feb 15 21:07:48 2007 UTC

24 24
25/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects. 25/* Eneq(@csd.uu.se): Added weight-modifiers in environment of objects.
26 sub/add_weight will transcend the environment updating the carrying 26 sub/add_weight will transcend the environment updating the carrying
27 variable. */ 27 variable. */
28 28
29
30#include <global.h> 29#include <global.h>
31#include <loader.h> 30#include <loader.h>
32#include <sproto.h> 31#include <sproto.h>
33 32
34// future resource loader base class 33/////////////////////////////////////////////////////////////////////////////
35struct loader_base
36{
37 const char *filename;
38 virtual const char *type () const = 0;
39
40 virtual archetype *get_arch (const char *name);
41 virtual void put_arch (archetype *arch);
42
43 virtual object *get_object (const char *name);
44 virtual void put_object (object *op);
45
46 virtual player *get_player ();
47 virtual void put_player (player *pl);
48
49 virtual region *get_region (const char *name);
50 virtual void put_region (region *region);
51
52 virtual facetile *get_face (const char *name);
53 virtual void put_face (facetile *face);
54
55 virtual treasurelist *get_treasure (const char *name, bool one = false);
56 virtual void put_treasure (treasurelist *treasure);
57
58 virtual animation *get_animation (const char *name);
59 virtual void put_animation (animation *anim);
60};
61
62// future generic resource loader
63// handles generic stuff valid in most files, such as
64// animations, treasures, faces and so on
65struct loader_generic : loader_base
66{
67 const char *type () const = 0;
68
69 region *get_region ();
70 void put_region (region *region);
71
72 facetile *get_face (const char *name);
73 void put_face (facetile *face);
74
75 treasurelist *get_treasure (const char *name, bool one = false);
76 void put_treasure (treasurelist *treasure);
77
78 animation *get_animation (const char *name);
79 void put_animation (animation *anim);
80};
81
82// the base class warns about and skips everything
83archetype *
84loader_base::get_arch (const char *name)
85{
86 LOG (llevError, "%s: found archetype definition '%s', which is not allowed in files of type %s.\n",
87 filename, name, type ());
88
89 return new archetype;
90}
91
92object *
93loader_base::get_object (const char *name)
94{
95 LOG (llevError, "%s: found object definition '%s', which is not allowed in files of type %s.\n",
96 filename, name, type ());
97
98 return object::create ();
99}
100
101player *
102loader_base::get_player ()
103{
104 LOG (llevError, "%s: found player definition, which is not allowed in files of type %s.\n",
105 filename, type ());
106
107 return player::create ();
108}
109
110region *
111loader_base::get_region (const char *name)
112{
113 LOG (llevError, "%s: found region definition '%s', which is not allowed in files of type %s.\n",
114 filename, name, type ());
115
116 return new region;
117}
118
119facetile *
120loader_base::get_face (const char *name)
121{
122 LOG (llevError, "%s: found face definition '%s', which is not allowed in files of type %s.\n",
123 filename, name, type ());
124
125 return new facetile;
126}
127
128treasurelist *
129loader_base::get_treasure (const char *name, bool one)
130{
131 LOG (llevError, "%s: found treasure definition '%s', which is not allowed in files of type %s.\n",
132 filename, name, type ());
133
134 return new treasurelist;//D
135}
136
137animation *
138loader_base::get_animation (const char *name)
139{
140 LOG (llevError, "%s: found animation definition '%s', which is not allowed in files of type %s.\n",
141 filename, name, type ());
142
143 return new animation;
144}
145
146void
147loader_base::put_arch (archetype *arch)
148{
149 delete arch;
150}
151
152void
153loader_base::put_object (object *op)
154{
155 op->destroy ();
156}
157
158void
159loader_base::put_player (player *pl)
160{
161 delete pl;
162}
163
164void
165loader_base::put_region (region *region)
166{
167 delete region;
168}
169
170void
171loader_base::put_face (facetile *face)
172{
173 delete face;
174}
175
176void
177loader_base::put_treasure (treasurelist *treasure)
178{
179 delete treasure;
180}
181
182void
183loader_base::put_animation (animation *anim)
184{
185 delete anim;
186}
187 34
188/* Maps the MOVE_* values to names */ 35/* Maps the MOVE_* values to names */
189static const char *const move_name[] = { "walk", "fly_low", "fly_high", "swim", "boat", NULL }; 36static const char *const move_name[] = { "walk", "fly_low", "fly_high", "swim", "boat", NULL };
190 37
191/* This table is only necessary to convert objects that existed before the 38/* This table is only necessary to convert objects that existed before the
416 * to check for objects to make sure there are no common errors. 263 * to check for objects to make sure there are no common errors.
417 */ 264 */
418static void 265static void
419check_loaded_object (object *op) 266check_loaded_object (object *op)
420{ 267{
421 int ip;
422
423 /* We do some specialised handling to handle legacy cases of name_pl. 268 /* We do some specialised handling to handle legacy cases of name_pl.
424 * If the object doesn't have a name_pl, we just use the object name - 269 * If the object doesn't have a name_pl, we just use the object name -
425 * this isn't perfect (things won't be properly pluralised), but works to 270 * this isn't perfect (things won't be properly pluralised), but works to
426 * that degree (5 heart is still quite understandable). But the case we 271 * that degree (5 heart is still quite understandable). But the case we
427 * also have to catch is if this object is not using the normal name for 272 * also have to catch is if this object is not using the normal name for
428 * the object. In that case, we also want to use the loaded name. 273 * the object. In that case, we also want to use the loaded name.
429 * Otherwise, what happens is that the the plural name will lose 274 * Otherwise, what happens is that the the plural name will lose
430 * information (appear as just 'hearts' and not 'goblins heart') 275 * information (appear as just 'hearts' and not 'goblins heart')
431 */ 276 */
432 if (op->arch && op->name != op->arch->clone.name && op->name_pl == op->arch->clone.name_pl) 277 if (op->arch && op->name != op->arch->clone.name && op->name_pl == op->arch->clone.name_pl)
433 op->name_pl = NULL; 278 op->name_pl = 0;
434 279
435 if (!op->name_pl) 280 if (!op->name_pl)
436 op->name_pl = op->name; 281 op->name_pl = op->name;
437 282
438 /* objects now have a materialname. try to patch it in */ 283 /* objects now have a materialname. try to patch it in */
441 if (op->map != NULL) 286 if (op->map != NULL)
442 set_materialname (op, op->map->difficulty, NULL); 287 set_materialname (op, op->map->difficulty, NULL);
443 else 288 else
444 set_materialname (op, 5, NULL); 289 set_materialname (op, 5, NULL);
445 } 290 }
291
446 /* only do these when program is first run - a bit 292 /* only do these when program is first run - a bit
447 * excessive to do this at every run - most of this is 293 * excessive to do this at every run - most of this is
448 * really just to catch any errors - program will still run, but 294 * really just to catch any errors - program will still run, but
449 * not in the ideal fashion. 295 * not in the ideal fashion.
450 */ 296 */
451 if ((op->type == WEAPON || op->type == BOW) && arch_init) 297 if ((op->type == WEAPON || op->type == BOW) && arch_init)
452 { 298 {
453 if (!op->skill) 299 if (!op->skill)
454 {
455 LOG (llevError, "Weapon %s lacks a skill.\n", op->debug_desc ()); 300 LOG (llevError, "Weapon %s lacks a skill.\n", op->debug_desc ());
456 }
457 else if ((!strcmp (op->skill, "one handed weapons") && op->body_info[1] != -1) || 301 else if ((!strcmp (op->skill, "one handed weapons") && op->body_info[1] != -1) ||
458 (!strcmp (op->skill, "two handed weapons") && op->body_info[1] != -2)) 302 (!strcmp (op->skill, "two handed weapons") && op->body_info[1] != -2))
459 {
460 LOG (llevError, "weapon %s arm usage does not match skill: %d, %s\n", op->debug_desc (), op->body_info[1], &op->skill); 303 LOG (llevError, "weapon %s arm usage does not match skill: %d, %s\n", op->debug_desc (), op->body_info[1], &op->skill);
461 }
462 } 304 }
463 305
464 /* We changed last_heal to gen_sp_armour, which is what it 306 /* We changed last_heal to gen_sp_armour, which is what it
465 * really does for many objects. Need to catch any in maps 307 * really does for many objects. Need to catch any in maps
466 * that may have an old value. 308 * that may have an old value.
467 */ 309 */
468 if ((op->type == WEAPON) || 310 if (op->type == WEAPON
469 (op->type == ARMOUR) || (op->type == HELMET) || 311 || op->type == ARMOUR || op->type == HELMET
470 (op->type == SHIELD) || (op->type == RING) || 312 || op->type == SHIELD || op->type == RING
471 (op->type == BOOTS) || (op->type == GLOVES) || 313 || op->type == BOOTS || op->type == GLOVES
472 (op->type == AMULET) || (op->type == GIRDLE) || (op->type == BRACERS) || (op->type == CLOAK)) 314 || op->type == AMULET || op->type == GIRDLE
315 || op->type == BRACERS || op->type == CLOAK)
473 { 316 {
474 if (op->last_heal) 317 if (op->last_heal)
475 { 318 {
476 LOG (llevDebug, "Object %s still has last_heal set, not gen_sp_armour\n", op->debug_desc ()); 319 LOG (llevDebug, "Object %s still has last_heal set, not gen_sp_armour\n", op->debug_desc ());
477 op->gen_sp_armour = op->last_heal; 320 op->gen_sp_armour = op->last_heal;
478 op->last_heal = 0; 321 op->last_heal = 0;
479 } 322 }
480 323
481 ip = calc_item_power (op, 0); 324 int ip = calc_item_power (op, 0);
325
482 /* Legacy objects from before item power was in the game */ 326 /* Legacy objects from before item power was in the game */
483 if (!op->item_power && ip) 327 if (!op->item_power && ip)
484 { 328 {
485 if (ip > 3) 329 if (ip > 3)
486 {
487 LOG (llevDebug, "Object %s had no item power, using %d\n", op->debug_desc (), ip); 330 LOG (llevDebug, "Object %s had no item power, using %d\n", op->debug_desc (), ip);
488 } 331
489 op->item_power = ip; 332 op->item_power = ip;
490 } 333 }
334
491 /* Check for possibly bogus values. Has to meet both these criteria - 335 /* Check for possibly bogus values. Has to meet both these criteria -
492 * something that has item_power 1 is probably just fine if our calculated 336 * something that has item_power 1 is probably just fine if our calculated
493 * value is 1 or 2 - these values are small enough that hard to be precise. 337 * value is 1 or 2 - these values are small enough that hard to be precise.
494 * similarly, it item_power is 0, the first check will always pass, 338 * similarly, it item_power is 0, the first check will always pass,
495 * but not the second one. 339 * but not the second one.
496 */ 340 */
497 if (ip > 2 * op->item_power && ip > (op->item_power + 3)) 341 if (ip > 2 * op->item_power && ip > (op->item_power + 3))
498 {
499 LOG (llevDebug, "Object %s seems to have too low item power? %d > %d\n", op->debug_desc (), ip, op->item_power); 342 LOG (llevDebug, "Object %s seems to have too low item power? %d > %d\n", op->debug_desc (), ip, op->item_power);
500 }
501
502 } 343 }
344
503 /* Old spellcasting object - need to load in the appropiate object */ 345 /* Old spellcasting object - need to load in the appropiate object */
504 if ((op->type == ROD || op->type == WAND || op->type == SCROLL || op->type == HORN || op->type == FIREWALL || 346 if ((op->type == ROD || op->type == WAND || op->type == SCROLL || op->type == HORN || op->type == FIREWALL ||
505 /* POTIONS and ALTARS don't always cast spells, but if they do, update them */ 347 /* POTIONS and ALTARS don't always cast spells, but if they do, update them */
506 ((op->type == POTION || op->type == ALTAR) && op->stats.sp)) && !op->inv && !arch_init) 348 ((op->type == POTION || op->type == ALTAR) && op->stats.sp)) && !op->inv && !arch_init)
507 { 349 {
508 object *tmp;
509
510 /* Fireall is bizarre in that spell type was stored in dam. Rest are 'normal' 350 /* Fireall is bizarre in that spell type was stored in dam. Rest are 'normal'
511 * in that spell was stored in sp. 351 * in that spell was stored in sp.
512 */ 352 */
513 tmp = get_archetype (spell_mapping[op->type == FIREWALL ? op->stats.dam : op->stats.sp]); 353 object *tmp = get_archetype (spell_mapping[op->type == FIREWALL ? op->stats.dam : op->stats.sp]);
514 insert_ob_in_ob (tmp, op); 354 insert_ob_in_ob (tmp, op);
515 op->randomitems = NULL; /* So another spell isn't created for this object */ 355 op->randomitems = NULL; /* So another spell isn't created for this object */
516 } 356 }
357
517 /* spellbooks & runes use slaying. But not to arch name, but to spell name */ 358 /* spellbooks & runes use slaying. But not to arch name, but to spell name */
518
519 if ((op->type == SPELLBOOK || op->type == RUNE) && op->slaying && !op->inv && !arch_init) 359 if ((op->type == SPELLBOOK || op->type == RUNE) && op->slaying && !op->inv && !arch_init)
520 { 360 {
521 object *tmp;
522
523 tmp = get_archetype_by_object_name (op->slaying); 361 object *tmp = get_archetype_by_object_name (op->slaying);
524 insert_ob_in_ob (tmp, op); 362 insert_ob_in_ob (tmp, op);
525 op->randomitems = NULL; /* So another spell isn't created for this object */ 363 op->randomitems = NULL; /* So another spell isn't created for this object */
526 /* without this, value is all screwed up */ 364 /* without this, value is all screwed up */
527 op->value = op->arch->clone.value * op->inv->value; 365 op->value = op->arch->clone.value * op->inv->value;
528 } 366 }
564 { 402 {
565 op->move_type = MOVE_ALL; 403 op->move_type = MOVE_ALL;
566 op->stats.maxhp = 0; 404 op->stats.maxhp = 0;
567 } 405 }
568 else 406 else
569 {
570 op->move_type = MOVE_WALK; 407 op->move_type = MOVE_WALK;
571 }
572 } 408 }
573 } 409 }
574 410
575} 411}
576 412
650 bool ismore = 0; 486 bool ismore = 0;
651 object *op_inv = op->inv; 487 object *op_inv = op->inv;
652 488
653 for (;;) 489 for (;;)
654 { 490 {
655 keyword kw = thawer.get_kv (); 491 switch (thawer.get_kv ())
656
657 switch (kw)
658 { 492 {
659 case KW_Object: // uppercase alias 493 case KW_Object: // uppercase alias
660 case KW_object: 494 case KW_object:
661 thawer.get (op->name); 495 thawer.get (op->name);
662 496
713 tmp = arch_to_object (arch); 547 tmp = arch_to_object (arch);
714 else 548 else
715 { 549 {
716 tmp = object::create (); 550 tmp = object::create ();
717 /* record the name of the broken object */ 551 /* record the name of the broken object */
552 //TODO: but do not log???
718 tmp->name = str; 553 tmp->name = str;
719 } 554 }
720 555
721 // decativate the object we, as we are still going 556 // decativate the object we, as we are still going
722 // to read the speed value. Objects should be activated 557 // to read the speed value. Objects should be activated
780 op->other_arch = archetype::find (thawer.get_str ()); 615 op->other_arch = archetype::find (thawer.get_str ());
781 break; 616 break;
782 617
783 case KW_animation: 618 case KW_animation:
784 { 619 {
620 CLEAR_FLAG (op, FLAG_ANIMATE);
621 op->animation_id = 0;
622
785 const char *str = thawer.get_str (); 623 const char *str = thawer.get_str ();
786
787 if (!str)
788 {
789 op->animation_id = 0;
790 CLEAR_FLAG (op, FLAG_ANIMATE);
791 }
792 else if ((op->animation_id = find_animation (str))) 624 if (str && (op->animation_id = find_animation (str)))
793 SET_FLAG (op, FLAG_ANIMATE); 625 SET_FLAG (op, FLAG_ANIMATE);
794 } 626 }
795
796 break; 627 break;
797 628
798 case KW_last_heal: thawer.get (op->last_heal); break; 629 case KW_last_heal: thawer.get (op->last_heal); break;
799 case KW_last_sp: thawer.get (op->last_sp); break; 630 case KW_last_sp: thawer.get (op->last_sp); break;
800 case KW_last_grace: thawer.get (op->last_grace); break; 631 case KW_last_grace: thawer.get (op->last_grace); break;
865 case KW_path_denied: thawer.get (op->path_denied); break; 696 case KW_path_denied: thawer.get (op->path_denied); break;
866 case KW_invisible: thawer.get (op->invisible); break; 697 case KW_invisible: thawer.get (op->invisible); break;
867 case KW_magic: thawer.get (op->magic); break; 698 case KW_magic: thawer.get (op->magic); break;
868 case KW_state: thawer.get (op->state); break; 699 case KW_state: thawer.get (op->state); break;
869 case KW_move_slow_penalty: thawer.get (op->move_slow_penalty); break; 700 case KW_move_slow_penalty: thawer.get (op->move_slow_penalty); break;
870 case KW_material: thawer.get (op->material); break; 701 case KW_material: thawer.get (op->materials); break; //TODO: nuke
871 case KW_materialname: thawer.get (op->materialname); break; 702 case KW_materialname: thawer.get (op->materialname); break;
872 703
873 /* These are the new values */ 704 /* These are the new values */
874 case KW_move_block: set_move (op->move_block, thawer.get_str ()); break; 705 case KW_move_block: set_move (op->move_block, thawer.get_str ()); break;
875 case KW_move_allow: set_move (op->move_allow, thawer.get_str ()); break; 706 case KW_move_allow: set_move (op->move_allow, thawer.get_str ()); break;
1137 add_button_link (op, op->map, thawer.get_sint32 ()); 968 add_button_link (op, op->map, thawer.get_sint32 ());
1138 break; 969 break;
1139 970
1140 case KW_randomitems: 971 case KW_randomitems:
1141 op->randomitems = find_treasurelist (thawer.get_str ()); 972 op->randomitems = find_treasurelist (thawer.get_str ());
973 //if (!op->randomitems)
974 // LOG (llevError, "%s uses unknown randomitems '%s'.\n", op->debug_desc (), thawer.get_str ());
1142 break; 975 break;
1143 976
1144 case KW_msg: 977 case KW_msg:
1145 thawer.get_ml (KW_endmsg, op->msg); 978 thawer.get_ml (KW_endmsg, op->msg);
1146 //TODO: allow longer messages 979 //TODO: allow longer messages
1184 1017
1185 case KW_EOF: 1018 case KW_EOF:
1186 return LL_EOF; 1019 return LL_EOF;
1187 1020
1188 case KW_ERROR: 1021 case KW_ERROR:
1189 set_ob_key_value (op, thawer.last_keyword, thawer.last_value, true); 1022 set_ob_key_value (op, thawer.kw_str, thawer.value, true);
1190 //fprintf (stderr, "addkv(%s,%s)\n", thawer.last_keyword, thawer.last_value);//D 1023 //fprintf (stderr, "addkv(%s,%s)\n", thawer.kw_str, thawer.value);//D
1191 break; 1024 break;
1192 1025
1193 default: 1026 default:
1194 LOG (llevError, "UNSUPPORTED KEYWORD IN MAP: \"%s\", bug in normaliser. skipping.\n", keyword_str[kw]); 1027 if (!thawer.parse_error ("object", op->name))
1028 return false;
1195 break; 1029 break;
1196 } 1030 }
1197 } 1031 }
1198} 1032}
1199 1033
1533 f.put (resist_save[i], op->resist[i]); 1367 f.put (resist_save[i], op->resist[i]);
1534 1368
1535 CMP_OUT (path_attuned); 1369 CMP_OUT (path_attuned);
1536 CMP_OUT (path_repelled); 1370 CMP_OUT (path_repelled);
1537 CMP_OUT (path_denied); 1371 CMP_OUT (path_denied);
1538 CMP_OUT (material); 1372 CMP_OUT2 (material, materials);//TODO: nuke
1539 CMP_OUT (materialname); 1373 CMP_OUT (materialname);
1540 CMP_OUT (value); 1374 CMP_OUT (value);
1541 CMP_OUT (carrying); 1375 CMP_OUT (carrying);
1542 CMP_OUT (weight); 1376 CMP_OUT (weight);
1543 CMP_OUT (invisible); 1377 CMP_OUT (invisible);
1620 1454
1621 fp.put (op); 1455 fp.put (op);
1622 fp.put (KW_end); 1456 fp.put (KW_end);
1623} 1457}
1624 1458
1459/////////////////////////////////////////////////////////////////////////////
1460
1461bool load_resource_file (const char *filename)
1462{
1463 object_thawer f (filename);
1464
1465 f.next_kv ();
1466
1467 for (;;)
1468 {
1469 switch (f.kw)
1470 {
1471 case KW_region:
1472 if (!region::load (f))
1473 return false;
1474 break;
1475
1476 case KW_EOF:
1477 return true;
1478
1479 default:
1480 if (!f.parse_error ("resource file"))
1481 return false;
1482 }
1483
1484 f.next_kv ();
1485 }
1486}
1487

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines