From 871be21e83502a909620a5bcfcd09e3257ed0518 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 13 Nov 2010 14:25:39 +0000 Subject: corrected a bug where ship names got erased, made core::Entity::info() const, added core::Info::find() to get non-const *core::Info pointers --- src/core/entity.cc | 10 +------ src/core/entity.h | 6 ++-- src/core/info.cc | 12 ++++++++ src/core/info.h | 25 +++++++++++++---- src/core/parser.cc | 9 ++++-- src/game/base/jumppoint.cc | 16 +++++++---- src/game/base/ship.cc | 49 ++++++++++++++++----------------- src/game/base/ship.h | 4 +-- src/game/base/shipmodel.cc | 35 +++++++++++++++++++++--- src/game/base/template.cc | 68 ++++++++++++++++++++++++++++++---------------- src/game/base/template.h | 5 ++++ 11 files changed, 158 insertions(+), 81 deletions(-) diff --git a/src/core/entity.cc b/src/core/entity.cc index 35164dc..013e66d 100644 --- a/src/core/entity.cc +++ b/src/core/entity.cc @@ -266,13 +266,9 @@ void Entity::clear_updates() } } -void Entity::set_info(Info *info) +void Entity::set_info(const Info *info) { entity_info = info; - if (entity_info) { - entity_info->set_model(model()); - entity_info->set_name(name()); - } } void Entity::set_inventory(Inventory *inventory) @@ -318,10 +314,6 @@ void Entity::set_model(model::Model *model) if (entity_model) { entity_modelname.assign(entity_model->name()); } - - if (entity_info) { - entity_info->set_model(entity_model); - } } void Entity::set_modelname(const std::string &modelname) diff --git a/src/core/entity.h b/src/core/entity.h index 1996e10..5c34f97 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -194,7 +194,7 @@ public: } /// entity info - inline Info *info() const { + inline const Info *info() const { return entity_info; } @@ -328,7 +328,7 @@ public: */ void set_inventory(Inventory *inventory); - void set_info(Info *info); + void set_info(const Info *info); /// set the timestamp when the entity was last alive void set_keepalive(unsigned long timestamp) { @@ -492,7 +492,7 @@ private: Menus entity_menus; Inventory* entity_inventory; - Info* entity_info; + const Info* entity_info; Extension* entity_extension[4]; diff --git a/src/core/info.cc b/src/core/info.cc index cc1720e..2ff6669 100644 --- a/src/core/info.cc +++ b/src/core/info.cc @@ -243,6 +243,18 @@ void Info::print() const Info::Registry Info::info_registry; +Info *Info::find(const Info *info) +{ + if (!info) + return 0; + + for (Registry::iterator it = info_registry.begin(); it != info_registry.end(); it++) { + if (info == (*it)); + return *it; + } + return 0; +} + Info *Info::find(unsigned int id) { if (!id) diff --git a/src/core/info.h b/src/core/info.h index b8154d7..d32ccab 100644 --- a/src/core/info.h +++ b/src/core/info.h @@ -191,23 +191,38 @@ private: /* ---- static info registry --------------------------------------- */ public: - /// info registry type definition + /** + * @brief info registry type definition + */ typedef std::vector Registry; - /// the info registry + /** + * @brief the info registry + */ static inline Registry & registry() { return info_registry; } - /// search the info registry for a record with the specified id + /** + * @brief search the info registry for a record with the specified id + */ static Info *find(const unsigned int id); - /// search the info registry for a labeled item + /** + * @brief search the info registry for a labeled item + */ static Info *find(const char * label); - /// search the info registry for a label + /** + * @brief search the info registry for a label + */ static Info *find(const std::string & label); + /** + * @brief search the info register for a pointer + */ + static Info *find(const Info *info); + /// search the info registry for a record with a matching label or name static Info *search(const std::string & searchstr); diff --git a/src/core/parser.cc b/src/core/parser.cc index 953cd50..22b2fc3 100644 --- a/src/core/parser.cc +++ b/src/core/parser.cc @@ -47,10 +47,13 @@ bool Parser::got_entity_key(filesystem::IniFile &inifile, core::Entity *entity) } } else if (inifile.got_key_string("info", strval)) { - if (!entity->info()) { - entity->set_info(new Info(Entity::infotype(), entity->label().c_str())); + + Info *info = Info::find(entity->info()); + if (!info) { + info = new Info(Entity::infotype(), entity->label().c_str()); + entity->set_info(info); } - entity->info()->add_text(strval); + info->add_text(strval); return true; } else if (inifile.got_key_label("label", strval)) { diff --git a/src/game/base/jumppoint.cc b/src/game/base/jumppoint.cc index 8af39b0..e52873e 100644 --- a/src/game/base/jumppoint.cc +++ b/src/game/base/jumppoint.cc @@ -79,11 +79,14 @@ void JumpPoint::validate() } set_name(name); - if (!info()) { - set_info(new core::Info(core::Entity::infotype(), label().c_str())); + // info is const, core::Info::find returns a non-const pointer + core::Info *entity_info = core::Info::find(info()); + if (!entity_info) { + entity_info = new core::Info(core::Entity::infotype(), label().c_str()); + set_info(entity_info); } - info()->clear_text(); - info()->add_text("Jumppoint to the " + target()->zone()->name()); + entity_info->clear_text(); + entity_info->add_text("Jumppoint to the " + target()->zone()->name()); con_debug << " " << label() << " jumppoint to " << target()->zone()->label() << ":" << target()->label() << std::endl; @@ -119,8 +122,9 @@ void JumpGate::validate() } set_name(name); - info()->clear_text(); - info()->add_text("Jumpgate to the " + target()->zone()->name()); + core::Info *entity_info = core::Info::find(info()); + entity_info->clear_text(); + entity_info->add_text("Jumpgate to the " + target()->zone()->name()); if (target()) { set_flag(core::Entity::Dockable); diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index 726729a..f88afd1 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -24,27 +24,27 @@ namespace game const float MIN_DELTA = 0.000001f; -Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable() +Ship::Ship(core::Player *owner, const ShipModel *shipmodel) : core::EntityControlable() { assert(shipmodel); + ship_shipmodel = shipmodel; entity_moduletypeid = ship_enttype; - set_radius(0); // apply template settings - if (shipmodel->model_template()) { - shipmodel->model_template()->apply(this); + if (ship_shipmodel->model_template()) { + ship_shipmodel->model_template()->apply(this); } // apply ship model settings // shipmodel overrides template model and radius - if (shipmodel->modelname().size()) { - set_modelname(shipmodel->modelname()); - } - if (shipmodel->radius()) { - set_radius(shipmodel->radius()); + if (ship_shipmodel->modelname().size()) { + set_modelname(ship_shipmodel->modelname()); } + if (ship_shipmodel->radius()) { + set_radius(ship_shipmodel->radius()); + } if (!radius()) { if (model()) { set_radius(model()->radius()); @@ -53,17 +53,17 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable( if (!radius()) { set_radius(0.5f); } - - ship_shipmodel = shipmodel; - set_info(shipmodel); - set_name(shipmodel->name()); - ship_jumpdrive = shipmodel->jumpdrive(); + + set_name(ship_shipmodel->name()); + set_info(ship_shipmodel); + + ship_jumpdrive = ship_shipmodel->jumpdrive(); ship_impulsedrive_timer = 0; ship_jumpdrive_timer = 0; ship_jumpdepart = 0; if (owner) { - // this ship is owned by a player + // this ship is owned by a player, // player colors override template colors set_owner(owner); get_color().assign(owner->color()); @@ -74,13 +74,12 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable( // add an inventory set_inventory(new core::Inventory()); - inventory()->set_capacity(shipmodel->maxcargo()); + inventory()->set_capacity(ship_shipmodel->maxcargo()); } else { - set_name(shipmodel->name()); - set_label(shipmodel->label()); + set_label(ship_shipmodel->label()); } - if (shipmodel->dockable()) { + if (ship_shipmodel->dockable()) { using core::MenuDescription; using core::ButtonDescription; @@ -98,12 +97,12 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable( set_flag(core::Entity::Dockable); } - set_mass(shipmodel->mass()); - set_impulse_force(shipmodel->impulse_force()); - set_thrust_force(shipmodel->thrust_force()); - set_strafe_force(shipmodel->strafe_force()); - set_turn_force(shipmodel->turn_force()); - set_roll_force(shipmodel->roll_force()); + set_mass(ship_shipmodel->mass()); + set_impulse_force(ship_shipmodel->impulse_force()); + set_thrust_force(ship_shipmodel->thrust_force()); + set_strafe_force(ship_shipmodel->strafe_force()); + set_turn_force(ship_shipmodel->turn_force()); + set_roll_force(ship_shipmodel->roll_force()); reset(); diff --git a/src/game/base/ship.h b/src/game/base/ship.h index 9da45f4..ade7ff7 100644 --- a/src/game/base/ship.h +++ b/src/game/base/ship.h @@ -20,7 +20,7 @@ namespace game class Ship : public core::EntityControlable { public: - Ship(core::Player *owner, ShipModel *shipmodel); + Ship(core::Player *owner, const ShipModel *shipmodel); ~Ship(); /// shipmodel @@ -111,7 +111,7 @@ public: private: JumpPoint *find_closest_jumppoint(); - ShipModel *ship_shipmodel; + const ShipModel *ship_shipmodel; float current_target_direction; float current_target_pitch; diff --git a/src/game/base/shipmodel.cc b/src/game/base/shipmodel.cc index 4c67e72..d9a9ed7 100644 --- a/src/game/base/shipmodel.cc +++ b/src/game/base/shipmodel.cc @@ -40,8 +40,12 @@ bool ShipModel::init() con_print << "^BLoading ships..." << std::endl; - size_t count = 0; + size_t shipmodel_count = 0; + size_t template_count = 0; + ShipModel *shipmodel = 0; + Template *entitytemplate = 0; + std::string str; long l; float f; @@ -49,11 +53,24 @@ bool ShipModel::init() while (inifile.getline()) { if (inifile.got_key()) { - if (inifile.section().compare("ship") == 0) { + + if (inifile.section().compare("template") == 0) { + + if (Template::got_template_key(inifile, entitytemplate)) { + continue; + } else { + inifile.unkown_key(); + } + + } else if (inifile.section().compare("ship") == 0) { + if (inifile.got_key_label("label", str)) { + if (find(str)) { + inifile.unknown_error("duplicate ship type '" + str + "'"); + } shipmodel->set_label(str); - count++; continue; + } else if (inifile.got_key_string("name", str)) { shipmodel->set_name(str); continue; @@ -110,6 +127,12 @@ bool ShipModel::init() inifile.unkown_key(); } } + + } else if (inifile.got_section("template")) { + + template_count++; + entitytemplate = new Template(); + } else if (inifile.got_section("ship")) { // generate info for the last loaded ship model @@ -124,6 +147,8 @@ bool ShipModel::init() if (!Default::shipmodel) { Default::shipmodel = shipmodel; } + + shipmodel_count++; } else if (inifile.got_section()) { inifile.unknown_section(); @@ -135,7 +160,9 @@ bool ShipModel::init() shipmodel->generate_info(); } - con_debug << " " << inifile.name() << " " << count << " ship types" << std::endl; + con_debug << " " << inifile.name() << " " << template_count << " entity templates" << std::endl; + con_debug << " " << inifile.name() << " " << shipmodel_count << " ship types" << std::endl; + inifile.close(); diff --git a/src/game/base/template.cc b/src/game/base/template.cc index f937e22..7327c31 100644 --- a/src/game/base/template.cc +++ b/src/game/base/template.cc @@ -33,6 +33,47 @@ void Template::list() core::Info::list(template_infotype); } +bool Template::got_template_key(filesystem::IniFile &inifile, Template *entitytemplate) +{ + std::string strvalue; + math::Color colorvalue; + float floatvalue; + + if (inifile.got_key_label("label", strvalue)) { + if (find(strvalue)) { + inifile.unknown_error("duplicate template '" + strvalue + "'"); + } + entitytemplate->set_label(strvalue); + return true; + + } else if (inifile.got_key_string("name", strvalue)) { + entitytemplate->set_name(strvalue); + return true; + + } else if (inifile.got_key_string("model", strvalue)) { + entitytemplate->set_modelname(strvalue); + return true; + + } else if (inifile.got_key_string("info", strvalue)) { + entitytemplate->add_text(strvalue); + return true; + + } else if (inifile.got_key_color("color", colorvalue)) { + entitytemplate->set_color(colorvalue); + return true; + + } else if (inifile.got_key_color("colorsecond", colorvalue)) { + entitytemplate->set_color_second(colorvalue); + return true; + + } else if (inifile.got_key_float("radius", floatvalue)) { + entitytemplate->set_radius(floatvalue); + return true; + } else { + return false; + } +} + bool Template::init() { // initialize template InfoType @@ -52,10 +93,7 @@ bool Template::init() size_t count = 0; Template *entitytemplate = 0; - std::string strvalue; - math::Color colorvalue; - float floatvalue; - + while (inifile.getline()) { if (inifile.got_section()) { @@ -74,26 +112,8 @@ bool Template::init() if (inifile.in_section("template")) { - if (inifile.got_key_label("label", strvalue)) { - entitytemplate->set_label(strvalue); - - } else if (inifile.got_key_string("name", strvalue)) { - entitytemplate->set_name(strvalue); - - } else if (inifile.got_key_string("model", strvalue)) { - entitytemplate->set_modelname(strvalue); - - } else if (inifile.got_key_string("info", strvalue)) { - entitytemplate->add_text(strvalue); - - } else if (inifile.got_key_color("color", colorvalue)) { - entitytemplate->set_color(colorvalue); - - } else if (inifile.got_key_color("colorsecond", colorvalue)) { - entitytemplate->set_color_second(colorvalue); - - } else if (inifile.got_key_float("radius", floatvalue)) { - entitytemplate->set_radius(floatvalue); + if (got_template_key(inifile, entitytemplate)) { + continue; } else { inifile.unkown_key(); } diff --git a/src/game/base/template.h b/src/game/base/template.h index 5780e9c..7934a7f 100644 --- a/src/game/base/template.h +++ b/src/game/base/template.h @@ -50,6 +50,11 @@ public: * @brief apply a template to an entity */ void apply(core::Entity *entity) const; + + /** + * @brief read template keys from an ini file + */ + static bool got_template_key(filesystem::IniFile &inifile, Template *entitytemplate); protected: -- cgit v1.2.3