--- deliantra/server/common/loader.C 2007/02/19 19:10:31 1.62 +++ deliantra/server/common/loader.C 2007/05/19 00:08:11 1.83 @@ -1,5 +1,5 @@ /* - * CrossFire, A Multiplayer game for X-windows + * CrossFire, A Multiplayer game * * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team * Copyright (C) 2002 Mark Wedel & Crossfire Development Team @@ -32,9 +32,6 @@ ///////////////////////////////////////////////////////////////////////////// -/* Maps the MOVE_* values to names */ -static const char *const move_name[] = { "walk", "fly_low", "fly_high", "swim", "boat", NULL }; - /* This table is only necessary to convert objects that existed before the * spell object conversion to the new object. It was not practical * to go through every mapping looking for every potion, rod, wand, etc @@ -255,16 +252,72 @@ NULL }; -extern int arch_init; +extern bool loading_arch; /* This function checks the object after it has been loaded (when we * get the 'end' in the input stream). This function can be used to * deal with legacy objects where fields may have changed. It can also be used * to check for objects to make sure there are no common errors. */ -static void -check_loaded_object (object *op) +void +object::post_load_check () { + if (type >= NUM_TYPES) + { + LOG (llevError, "%s: type out of range, resetting to 0.\n", debug_desc ()); + type = 0; + } + + switch (type) + { + case BOW: + case WAND: + case ROD: + case HORN: + if (slot [body_range].info != -1) + { + LOG (llevError, "%s: body_range %d != -1\n", debug_desc (), slot [body_range].info); + slot [body_range].info = -1; + } + break; + + case WEAPON: + if (slot [body_combat].info != -1) + { + LOG (llevError, "%s: body_combat %d != -1\n", debug_desc (), slot [body_combat].info); + slot [body_combat].info = -1; + } + break; + + case SHIELD: + if (slot [body_shield].info != -1) + { + LOG (llevError, "%s: body_shield %d != -1\n", debug_desc (), slot [body_shield].info); + slot [body_shield].info = -1; + } + break; + + case PLAYER: + if (slot [body_shield].info != 1) + { + LOG (llevError, "%s: body_shield %d != 1\n", debug_desc (), slot [body_shield].info); + slot [body_shield].info = 1; + } + + if (slot [body_combat].info != 1) + { + LOG (llevError, "%s: body_combat %d != 1\n", debug_desc (), slot [body_combat].info); + slot [body_combat].info = 1; + } + + if (slot [body_range].info != 1) + { + LOG (llevError, "%s: body_range %d != 1\n", debug_desc (), slot [body_range].info); + slot [body_range].info = 1; + } + break; + } + /* We do some specialised handling to handle legacy cases of name_pl. * If the object doesn't have a name_pl, we just use the object name - * this isn't perfect (things won't be properly pluralised), but works to @@ -274,62 +327,57 @@ * Otherwise, what happens is that the the plural name will lose * information (appear as just 'hearts' and not 'goblins heart') */ - if (op->arch && op->name != op->arch->clone.name && op->name_pl == op->arch->clone.name_pl) - op->name_pl = 0; + if (arch && name != arch->clone.name && name_pl == arch->clone.name_pl) + name_pl = 0; - if (!op->name_pl) - op->name_pl = op->name; + if (!name_pl) + name_pl = name; /* objects now have a materialname. try to patch it in */ - if (!(op->is_weapon () && op->level > 0)) - { - if (op->map != NULL) - set_materialname (op, op->map->difficulty, NULL); - else - set_materialname (op, 5, NULL); - } + if (!(is_weapon () && level > 0)) + set_materialname (this, map ? map->difficulty : 5, 0); /* only do these when program is first run - a bit * excessive to do this at every run - most of this is * really just to catch any errors - program will still run, but * not in the ideal fashion. */ - if ((op->type == WEAPON || op->type == BOW) && arch_init) + if (loading_arch && (type == WEAPON || type == BOW || type == ROD || type == HORN || type == WAND)) { - if (!op->skill) - LOG (llevError, "Weapon %s lacks a skill.\n", op->debug_desc ()); - else if ((!strcmp (op->skill, "one handed weapons") && op->body_info[1] != -1) || - (!strcmp (op->skill, "two handed weapons") && op->body_info[1] != -2)) - LOG (llevError, "weapon %s arm usage does not match skill: %d, %s\n", op->debug_desc (), op->body_info[1], &op->skill); + if (!skill) + LOG (llevError, "Weapon %s lacks a skill.\n", debug_desc ()); + else if ((!strcmp (skill, "one handed weapons") && slot[body_arm].info != -1) || + (!strcmp (skill, "two handed weapons") && slot[body_arm].info != -2)) + LOG (llevError, "weapon %s arm usage does not match skill: %d, %s\n", debug_desc (), slot[body_arm].info, &skill); } /* We changed last_heal to gen_sp_armour, which is what it * really does for many objects. Need to catch any in maps * that may have an old value. */ - if (op->type == WEAPON - || op->type == ARMOUR || op->type == HELMET - || op->type == SHIELD || op->type == RING - || op->type == BOOTS || op->type == GLOVES - || op->type == AMULET || op->type == GIRDLE - || op->type == BRACERS || op->type == CLOAK) + if (type == WEAPON + || type == ARMOUR || type == HELMET + || type == SHIELD || type == RING + || type == BOOTS || type == GLOVES + || type == AMULET || type == GIRDLE + || type == BRACERS || type == CLOAK) { - if (op->last_heal) + if (last_heal) { - LOG (llevDebug, "Object %s still has last_heal set, not gen_sp_armour\n", op->debug_desc ()); - op->gen_sp_armour = op->last_heal; - op->last_heal = 0; + LOG (llevDebug, "Object %s still has last_heal set, not gen_sp_armour\n", debug_desc ()); + gen_sp_armour = last_heal; + last_heal = 0; } - int ip = calc_item_power (op, 0); + int ip = calc_item_power (this, 0); /* Legacy objects from before item power was in the game */ - if (!op->item_power && ip) + if (!item_power && ip) { if (ip > 3) - LOG (llevDebug, "Object %s had no item power, using %d\n", op->debug_desc (), ip); + LOG (llevDebug, "Object %s had no item power, using %d\n", debug_desc (), ip); - op->item_power = ip; + item_power = ip; } /* Check for possibly bogus values. Has to meet both these criteria - @@ -338,54 +386,54 @@ * similarly, it item_power is 0, the first check will always pass, * but not the second one. */ - if (ip > 2 * op->item_power && ip > (op->item_power + 3)) - LOG (llevDebug, "Object %s seems to have too low item power? %d > %d\n", op->debug_desc (), ip, op->item_power); + if (ip > 2 * item_power && ip > (item_power + 3)) + LOG (llevDebug, "Object %s seems to have too low item power? %d > %d\n", debug_desc (), ip, item_power); } /* Old spellcasting object - need to load in the appropiate object */ - if ((op->type == ROD || op->type == WAND || op->type == SCROLL || op->type == HORN || op->type == FIREWALL || + if ((type == ROD || type == WAND || type == SCROLL || type == HORN || type == FIREWALL || /* POTIONS and ALTARS don't always cast spells, but if they do, update them */ - ((op->type == POTION || op->type == ALTAR) && op->stats.sp)) && !op->inv && !arch_init) + ((type == POTION || type == ALTAR) && stats.sp)) && !inv && !loading_arch) { /* Fireall is bizarre in that spell type was stored in dam. Rest are 'normal' * in that spell was stored in sp. */ - object *tmp = get_archetype (spell_mapping[op->type == FIREWALL ? op->stats.dam : op->stats.sp]); - insert_ob_in_ob (tmp, op); - op->randomitems = NULL; /* So another spell isn't created for this object */ + object *tmp = get_archetype (spell_mapping[type == FIREWALL ? stats.dam : stats.sp]); + insert_ob_in_ob (tmp, this); + randomitems = NULL; /* So another spell isn't created for this object */ } /* spellbooks & runes use slaying. But not to arch name, but to spell name */ - if ((op->type == SPELLBOOK || op->type == RUNE) && op->slaying && !op->inv && !arch_init) + if ((type == SPELLBOOK || type == RUNE) && slaying && !inv && !loading_arch) { - object *tmp = get_archetype_by_object_name (op->slaying); - insert_ob_in_ob (tmp, op); - op->randomitems = NULL; /* So another spell isn't created for this object */ + object *tmp = get_archetype_by_object_name (slaying); + insert_ob_in_ob (tmp, this); + randomitems = NULL; /* So another spell isn't created for this object */ /* without this, value is all screwed up */ - op->value = op->arch->clone.value * op->inv->value; + value = arch->clone.value * inv->value; } - if (QUERY_FLAG (op, FLAG_MONSTER)) + if (QUERY_FLAG (this, FLAG_MONSTER)) { - if (op->stats.hp > op->stats.maxhp) + if (stats.hp > stats.maxhp) { - LOG (llevDebug, "Monster %s has hp set higher than maxhp (%d>%d)\n", op->debug_desc (), op->stats.hp, op->stats.maxhp); - op->stats.maxhp = op->stats.hp; + LOG (llevDebug, "Monster %s has hp set higher than maxhp (%d>%d)\n", debug_desc (), stats.hp, stats.maxhp); + stats.maxhp = stats.hp; } /* The archs just need to be updated for this */ - if (op->move_type == 0) - op->move_type = MOVE_WALK; + if (move_type == 0) + move_type = MOVE_WALK; } - if ((QUERY_FLAG (op, FLAG_GENERATOR) && QUERY_FLAG (op, FLAG_CONTENT_ON_GEN)) || op->type == CREATOR || op->type == CONVERTER) + if ((QUERY_FLAG (this, FLAG_GENERATOR) && QUERY_FLAG (this, FLAG_CONTENT_ON_GEN)) || type == CREATOR || type == CONVERTER) { /* Object will duplicate it's content as part of the * generation process. To do this, we must flag inventory * so it remains unevaluated concerning the randomitems and * the living (a demonlord shouldn't cast from inside generator!) */ - flag_inv (op, FLAG_IS_A_TEMPLATE); + flag_inv (this, FLAG_IS_A_TEMPLATE); } /* Handle player movers. We use move_type for player movers @@ -394,25 +442,38 @@ * on them). If move_type is set, presume person knows what they * are doing, otherwise, set move_type based on maxhp value. */ - if (op->type == PLAYERMOVER) + if (type == PLAYERMOVER) { - if (!op->move_type) + if (!move_type) { - if (op->stats.maxhp) + if (stats.maxhp) { - op->move_type = MOVE_ALL; - op->stats.maxhp = 0; + move_type = MOVE_ALL; + stats.maxhp = 0; } else - op->move_type = MOVE_WALK; + move_type = MOVE_WALK; } } - } static void -set_move (MoveType & mt, const char *str) +set_move (MoveType &mt, const char *str) { + static const struct flagstr { + const char *name; + MoveType flags; + } move_flags[] = { + { "walk" , MOVE_WALK }, + { "flying" , MOVE_FLY_LOW | MOVE_FLY_HIGH }, + { "fly_low" , MOVE_FLY_LOW }, + { "fly_high", MOVE_FLY_HIGH }, + { "swim" , MOVE_SWIM }, + { "boat" , MOVE_BOAT }, + { "ship" , MOVE_SHIP }, + { "all" , MOVE_ALL }, + }; + if (!str) { mt = 0; @@ -429,48 +490,30 @@ for (str = strtok ((char *) str, " "); str; str = strtok (0, " ")) { - if (!strcasecmp (str, "all")) - mt |= MOVE_ALL; - else - { - int i, negate = 0; + bool negate = 0; - if (*str == '-') - { - negate = 1; - str++; - } - - for (i = 0; move_name[i]; i++) - { - if (!strcasecmp (move_name[i], str)) - { - if (negate) - mt &= ~(1 << i); - else - mt |= (1 << i); - - break; - } - } + if (*str == '-') + { + negate = 1; + str++; + } - if (!move_name[i]) + for (const flagstr *f = move_flags; f < move_flags + sizeof (move_flags) / sizeof (move_flags [0]); ++f) + { + if (!strcmp (f->name, str)) { - /* fly is a special case - covers both fly_low and - * fly_high - since it doesn't match to a specific - * single bit, have to special case it. - */ - if (!strcasecmp (str, "flying")) - { - if (negate) - mt &= ~MOVE_FLYING; - else - mt |= MOVE_FLYING; - } + if (negate) + mt &= ~f->flags; else - LOG (llevDebug, "common/loader.l: set_move - unknown move string '%s'\n", str); + mt |= f->flags; + + goto next; } } + + LOG (llevDebug, "common/loader.C: set_move - unknown move string '%s'\n", str); + +next: ; } } @@ -483,13 +526,6 @@ bool object::parse_kv (object_thawer &f) { - assert (f.kw == KW_object || f.kw == KW_arch); - - if (f.kw == KW_object) - f.get (name); // preset name from object name - - f.next (); - object *op_inv = inv; for (;;) @@ -561,7 +597,13 @@ continue; case KW_other_arch: - other_arch = archetype::find (f.get_str ()); + other_arch = + loading_arch + ? archetype::get (f.get_str ()) + : archetype::find (f.get_str ()); + + if (!other_arch) + LOG (llevError, "%s uses unknown other_arch '%s'.\n", debug_desc (), f.get_str ()); break; case KW_animation: @@ -597,7 +639,7 @@ break; case KW_face: - face = &new_faces[FindFace (f.get_str (), 0)]; + face = face_find (f.get_str ()); break; case KW_x: f.get (x); break; @@ -734,22 +776,6 @@ break; - case KW_wiz: - GET_FLAG (this, FLAG_WIZ); - //TODO: move to check_object - if (QUERY_FLAG (this, FLAG_WIZ)) - { - SET_FLAG (this, FLAG_WAS_WIZ); - SET_FLAG (this, FLAG_WIZPASS); - SET_FLAG (this, FLAG_WIZCAST); - } - else - { - CLEAR_FLAG (this, FLAG_WIZPASS); - CLEAR_FLAG (this, FLAG_WIZCAST); - } - break; - case KW_friendly: if (f.get_sint32 ()) if (type != PLAYER) @@ -790,7 +816,6 @@ case KW_no_magic: GET_FLAG (this, FLAG_NO_MAGIC); break; case KW_no_drop: GET_FLAG (this, FLAG_NO_DROP); break; case KW_random_movement: GET_FLAG (this, FLAG_RANDOM_MOVE); break; - case KW_was_wiz: GET_FLAG (this, FLAG_WAS_WIZ); break; case KW_no_fix_player: GET_FLAG (this, FLAG_NO_FIX_PLAYER); break; case KW_is_lightable: GET_FLAG (this, FLAG_IS_LIGHTABLE); break; case KW_tear_down: GET_FLAG (this, FLAG_TEAR_DOWN); break; @@ -843,6 +868,8 @@ case KW_berserk: GET_FLAG (this, FLAG_BERSERK); break; case KW_is_buildable: GET_FLAG (this, FLAG_IS_BUILDABLE); break; case KW_destroy_on_death: GET_FLAG (this, FLAG_DESTROY_ON_DEATH); break; + case KW_treasure_env: GET_FLAG (this, FLAG_TREASURE_ENV); break; + case KW_precious: GET_FLAG (this, FLAG_PRECIOUS); break; case KW_armour: f.get (resist[ATNR_PHYSICAL]); break; case KW_resist_physical: f.get (resist[ATNR_PHYSICAL]); break; @@ -888,7 +915,7 @@ case KW_tooltype: f.get (tooltype); break; case KW_casting_time: f.get (casting_time); break; case KW_elevation: f.get (elevation); break; - case KW_smoothlevel: f.get (smoothlevel); break; + case KW_smoothlevel: f.get (smoothlevel); smoothlevel = clamp (smoothlevel, 0, 255); break; case KW_client_type: f.get (client_type); break; case KW_duration: f.get (duration); break; case KW_range: f.get (range); break; @@ -898,18 +925,20 @@ //TODO: mechanism to ensure that KW_xxx is consecutive needed from include/preprocess //TODO: parse from other include files - case KW_body_range: f.get (body_info[0]); break; - case KW_body_arm: f.get (body_info[1]); break; - case KW_body_torso: f.get (body_info[2]); break; - case KW_body_head: f.get (body_info[3]); break; - case KW_body_neck: f.get (body_info[4]); break; - case KW_body_skill: f.get (body_info[5]); break; - case KW_body_finger: f.get (body_info[6]); break; - case KW_body_shoulder: f.get (body_info[7]); break; - case KW_body_foot: f.get (body_info[8]); break; - case KW_body_hand: f.get (body_info[9]); break; - case KW_body_wrist: f.get (body_info[10]); break; - case KW_body_waist: f.get (body_info[11]); break; + case KW_body_range: slot[body_range] .info = f.get_sint32 (); break; + case KW_body_shield: slot[body_shield] .info = f.get_sint32 (); break; + case KW_body_combat: slot[body_combat] .info = f.get_sint32 (); break; + case KW_body_arm: slot[body_arm] .info = f.get_sint32 (); break; + case KW_body_torso: slot[body_torso] .info = f.get_sint32 (); break; + case KW_body_head: slot[body_head] .info = f.get_sint32 (); break; + case KW_body_neck: slot[body_neck] .info = f.get_sint32 (); break; + case KW_body_skill: slot[body_skill] .info = f.get_sint32 (); break; + case KW_body_finger: slot[body_finger] .info = f.get_sint32 (); break; + case KW_body_shoulder: slot[body_shoulder].info = f.get_sint32 (); break; + case KW_body_foot: slot[body_foot] .info = f.get_sint32 (); break; + case KW_body_hand: slot[body_hand] .info = f.get_sint32 (); break; + case KW_body_wrist: slot[body_wrist] .info = f.get_sint32 (); break; + case KW_body_waist: slot[body_waist] .info = f.get_sint32 (); break; case KW_can_apply: break; @@ -918,9 +947,18 @@ break; case KW_randomitems: - randomitems = find_treasurelist (f.get_str ()); - //if (!randomitems) - // LOG (llevError, "%s uses unknown randomitems '%s'.\n", debug_desc (), f.get_str ()); + if (f.get_str ()) + { + randomitems = + loading_arch + ? treasurelist::get (f.get_str ()) + : treasurelist::find (f.get_str ()); + + if (!randomitems) + LOG (llevError, "%s uses unknown randomitems '%s'.\n", debug_desc (), f.get_str ()); + } + else + randomitems = 0; break; case KW_msg: @@ -949,9 +987,7 @@ break; case KW_end: - check_loaded_object (this); - - if (!arch_init) + if (!loading_arch) instantiate (); f.next (); @@ -987,20 +1023,22 @@ assert (arch); //D maybe use exception handling of sorts? + f.next (); + object *op = object::create (); op->map = map; - op->arch = arch; arch->clone.copy_to (op); // copy_to activates, this should be fixed properly op->deactivate (); if (!op->parse_kv (f)) { - delete op; + op->destroy (true); return 0; } + op->post_load_check (); return op; } @@ -1015,7 +1053,7 @@ { object_thawer f (buf, (AV *)0); - f.kw = KW_arch; // special hack so that parse_kv skips + f.next (); return op->parse_kv (f); } @@ -1087,10 +1125,10 @@ */ static const keyword flag_names [NUM_FLAGS] = { KW_alive, - KW_wiz, KW_NULL, KW_NULL, - KW_was_wiz, + KW_NULL, + KW_NULL, KW_applied, KW_unpaid, KW_can_use_shield, @@ -1106,7 +1144,7 @@ KW_generator, KW_is_thrown, KW_auto_apply, - KW_NULL, // was KW_treasure + KW_treasure_env, KW_player_sold, /* 20 */ KW_see_invisible, @@ -1177,7 +1215,7 @@ /* 80 */ KW_has_ready_scroll, KW_can_use_rod, - KW_NULL, + KW_precious, KW_can_use_horn, KW_make_invisible, KW_inv_locked, @@ -1253,7 +1291,8 @@ f.put (KW_lore, KW_endlore, op->lore); CMP_OUT (other_arch); - CMP_OUT (face); + + if (op->face != tmp->face) f.put (KW_face, op->face ? &faces [op->face] : 0); if (op->animation_id != tmp->animation_id) if (op->animation_id) @@ -1360,10 +1399,10 @@ if (flag_names [i] && op->flag [i] != tmp->flag [i]) f.put (flag_names [i], op->flag [i] ? "1" : "0"); - /* Save body locations */ + // save body locations for (i = 0; i < NUM_BODY_LOCATIONS; i++) - if (op->body_info[i] != tmp->body_info[i]) - f.put (body_locations[i].save_name, op->body_info[i]); + if (op->slot[i].info != tmp->slot[i].info) + f.put (body_locations[i].save_name, op->slot[i].info); } /* @@ -1396,10 +1435,15 @@ ///////////////////////////////////////////////////////////////////////////// +// generic resource file load, +// currently supports: region, treasures, archetypes bool load_resource_file (const char *filename) { object_thawer f (filename); + bool success = false; + bool seen_arch = false; + f.next (); for (;;) @@ -1408,18 +1452,38 @@ { case KW_region: if (!region::read (f)) - return false; + goto finish; + break; + + case KW_treasure: + case KW_treasureone: + if (!treasurelist::read (f)) + goto finish; + break; + + case KW_object: + seen_arch = true; + if (!archetype::read (f)) + goto finish; break; case KW_EOF: - return true; + success = true; + goto finish; default: if (!f.parse_error ("resource file")) - return false; - } + goto finish; - f.next (); + f.next (); + break; + } } + +finish: + if (seen_arch) + init_archetype_pointers (); + + return success; }