From a993d31910b63a1f897e470842934e6ffefad32c Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 13 Nov 2009 22:25:09 +0000 Subject: added core::InfoType, refactored game::ShipModel as core::Info subclass, introduced core::Label --- src/game/base/commodity.cc | 4 ++ src/game/base/commodity.h | 2 + src/game/base/game.cc | 100 ++++++++++++++------------ src/game/base/ship.cc | 4 +- src/game/base/shipdealer.cc | 9 +-- src/game/base/shipmodel.cc | 168 +++++++++++--------------------------------- src/game/base/shipmodel.h | 105 ++++++++------------------- 7 files changed, 138 insertions(+), 254 deletions(-) (limited to 'src/game') diff --git a/src/game/base/commodity.cc b/src/game/base/commodity.cc index a9eb8c8..d41d370 100644 --- a/src/game/base/commodity.cc +++ b/src/game/base/commodity.cc @@ -4,6 +4,7 @@ the terms and conditions of the GNU General Public License version 2 */ +#include "base/game.h" #include "base/commodity.h" #include "auxiliary/functions.h" #include "sys/sys.h" @@ -13,8 +14,11 @@ namespace game /* ---- class Commodity -------------------------------------------- */ +core::InfoType *Commodity::commodity_infotype = 0; + Commodity::Commodity() : core::Info() { + set_type(commodity_infotype); } Commodity::~Commodity() diff --git a/src/game/base/commodity.h b/src/game/base/commodity.h index e226505..b889749 100644 --- a/src/game/base/commodity.h +++ b/src/game/base/commodity.h @@ -16,6 +16,8 @@ class Commodity : public core::Info { public: Commodity(); ~Commodity(); + + static core::InfoType *commodity_infotype; }; } // namespace game diff --git a/src/game/base/game.cc b/src/game/base/game.cc index fa02857..c20e42a 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -823,68 +823,73 @@ bool Game::load_menus(core::Entity *entity, const std::string &menufilename) // read commodities bool Game::load_commodities() { - filesystem::IniFile commoditiesini; - commoditiesini.open("commodities"); - if (!commoditiesini.is_open()) { - con_error << "Could not open " << commoditiesini.name() << "!" << std::endl; + // initialize commodities InfoType + Commodity::commodity_infotype = new core::InfoType("cargo"); + + filesystem::IniFile cargoini; + cargoini.open("cargo"); + if (!cargoini.is_open()) { + con_error << "Could not open " << cargoini.name() << "!" << std::endl; return false; } - con_print << "^BLoading commodities..." << std::endl; + con_print << "^BLoading cargo..." << std::endl; size_t count = 0; Commodity *commodity = 0; std::string str; - while (commoditiesini.getline()) { - if (commoditiesini.got_key()) { + while (cargoini.getline()) { + if (cargoini.got_key()) { - if (commoditiesini.section().compare("commodity") == 0) { - if (commoditiesini.got_key_string("label", str)) { - commodity->set_label(std::string("cargo/" + str)); + if (cargoini.section().compare("cargo") == 0) { + if (cargoini.got_key_string("label", str)) { + commodity->set_label(std::string(str)); count++; continue; - } else if (commoditiesini.got_key_string("name", str)) { - con_debug << " " << str << std::endl; + } else if (cargoini.got_key_string("name", str)) { commodity->set_name(str); continue; - } else if (commoditiesini.got_key_string("info", str)) { + } else if (cargoini.got_key_string("info", str)) { commodity->add_text(str); continue; - } else if (commoditiesini.got_key_string("model", str)) { + } else if (cargoini.got_key_string("model", str)) { commodity->set_modelname(str); continue; } else { - commoditiesini.unkown_key(); + cargoini.unkown_key(); } } - } else if (commoditiesini.got_section()) { + } else if (cargoini.got_section()) { - if (commoditiesini.got_section("commodity")) { + if (cargoini.got_section("cargo")) { commodity = new Commodity(); - } else if (commoditiesini.got_section()) { - commoditiesini.unknown_section(); + } else if (cargoini.got_section()) { + cargoini.unknown_section(); } } } - // add commodity infos - con_debug << " " << commoditiesini.name() << " " << count << " commodities" << std::endl; + // add cargo infos + con_debug << " " << cargoini.name() << " " << count << " cargo types" << std::endl; - commoditiesini.close(); + cargoini.close(); return true; } // read ship model specifications bool Game::load_ships() { + // initialize shipmodel InfoType + ShipModel::shipmodel_infotype = new core::InfoType("ship"); + using math::Vector3f; using math::Color; @@ -896,28 +901,29 @@ bool Game::load_ships() } con_print << "^BLoading ships..." << std::endl; - - unsigned int type_id = 0; + + size_t count = 0; ShipModel *shipmodel = 0; - std::string label; - std::string infostr; + std::string str; long l; float f; + bool b; while (shipsini.getline()) { if (shipsini.got_key()) { if (shipsini.section().compare("ship") == 0) { - if (shipsini.got_key_string("label", label)) { - aux::to_label(label); - shipmodel->shipmodel_label.assign(label); - ShipModel::add(shipmodel); + if (shipsini.got_key_string("label", str)) { + shipmodel->set_label(str); + count++; continue; - } else if (shipsini.got_key_string("name", shipmodel->shipmodel_name)) { + } else if (shipsini.got_key_string("name", str)) { + shipmodel->set_name(str); continue; - } else if (shipsini.got_key_string("info", infostr)) { - shipmodel->shipmodel_infotext.push_back(infostr); + } else if (shipsini.got_key_string("info", str)) { + shipmodel->add_text(str); continue; - } else if (shipsini.got_key_string("model", shipmodel->shipmodel_modelname)) { + } else if (shipsini.got_key_string("model", str)) { + shipmodel->set_modelname(str); continue; } else if (shipsini.got_key_long("price", l)) { shipmodel->set_price(l); @@ -925,7 +931,8 @@ bool Game::load_ships() } else if (shipsini.got_key_float("cargo", f)) { shipmodel->set_maxcargo(f); continue; - } else if (shipsini.got_key_bool("jumpdrive", shipmodel->shipmodel_jumpdrive)) { + } else if (shipsini.got_key_bool("jumpdrive", b)) { + shipmodel->set_jumpdrive(b); continue; } else if (shipsini.got_key_float("acceleration", f)) { shipmodel->set_acceleration(f); @@ -942,22 +949,29 @@ bool Game::load_ships() } } } else if (shipsini.got_section("ship")) { - if (shipmodel && !ShipModel::find(shipmodel)) delete shipmodel; - type_id++; - shipmodel = new ShipModel(type_id); - if (!Default::shipmodel) + // generate info for the last loaded ship model + if (shipmodel) { + shipmodel->generate_info(); + } + + // add a new shipmodel + shipmodel = new ShipModel(); + + if (!Default::shipmodel) { Default::shipmodel = shipmodel; + } } else if (shipsini.got_section()) { shipsini.unknown_section(); } } - if (shipmodel && !ShipModel::find(shipmodel)) delete shipmodel; + // generate info for the last loaded ship model + if (shipmodel) { + shipmodel->generate_info(); + } - // add shipmodel infos - ShipModel::generate_info(); - con_debug << " " << shipsini.name() << " " << ShipModel::registry.size() << " ship models" << std::endl; + con_debug << " " << shipsini.name() << " " << count << " ship types" << std::endl; shipsini.close(); diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index 2c42ca8..7cdf2ca 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -41,7 +41,7 @@ Ship::Ship(core::Player *owner, ShipModel *shipmodel) : get_color_second().assign(owner->color_second()); ship_shipmodel = shipmodel; - ship_jumpdrive = shipmodel->shipmodel_jumpdrive; + ship_jumpdrive = shipmodel->jumpdrive(); ship_impulsedrive_timer = 0; ship_jumpdrive_timer = 0; @@ -68,7 +68,7 @@ void Ship::func_impulse() if (entity_state == core::Entity::Impulse) { entity_state = core::Entity::Normal; target_thrust = 1.0f; - entity_thrust = 1.0f; + entity_thrust = 0.0f; } else if (entity_state == core::Entity::ImpulseInitiate) { entity_state = core::Entity::Normal; diff --git a/src/game/base/shipdealer.cc b/src/game/base/shipdealer.cc index 54d3e90..69b4543 100644 --- a/src/game/base/shipdealer.cc +++ b/src/game/base/shipdealer.cc @@ -83,13 +83,8 @@ void ShipDealer::func_buy(core::Player *player, const std::string &args) } if (!shipmodel) { - std::string helpstr; - for (ShipModel::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) { - if (helpstr.size()) - helpstr.append("^N|^B"); - helpstr.append((*smit).second->label()); - } - player->send("Usage: buy ship [^B" + helpstr + "^N]"); + ShipModel::list(); + player->send("Usage: buy ship model"); return; } diff --git a/src/game/base/shipmodel.cc b/src/game/base/shipmodel.cc index fe1c88c..9bd7573 100644 --- a/src/game/base/shipmodel.cc +++ b/src/game/base/shipmodel.cc @@ -8,169 +8,83 @@ #include "auxiliary/functions.h" #include "base/shipmodel.h" +#include "base/game.h" #include "sys/sys.h" namespace game { -// the ship model registry -ShipModel::Registry ShipModel::registry; +core::InfoType *ShipModel::shipmodel_infotype = 0; -ShipModel::ShipModel(const unsigned int type_id) +ShipModel::ShipModel() { + set_type(shipmodel_infotype); + //default specifications shipmodel_acceleration = 1.0f; // thruster acceleration in game untits/second^2 shipmodel_maxspeed = 3.0f; // maximum thruster speed in game units/second shipmodel_turnspeed = 45.0f; // 45 degrees per second - shipmodel_price = 0; // the ship is for free by default shipmodel_maxcargo = 0; - shipmodel_jumpdrive = false; - shipmodel_type_id = type_id; + shipmodel_jumpdrive = false; // no jumpdrive capability } ShipModel::~ShipModel() -{} - - -// clear the ship model registry -void ShipModel::clear() -{ - for (iterator smit = registry.begin(); smit != registry.end(); smit++) { - delete(*smit).second; - } - registry.clear(); -} - -void ShipModel::print() { - con_print << "label: ^B" << label() << " ^Nname: ^B" << name() << std::endl; - con_print << " price: ^B" << price() << std::endl; - con_print << " acceleration: ^B" << acceleration() << std::endl; - con_print << " turnspeed: ^B" << turnspeed() << std::endl; - con_print << " max speed: ^B" << maxspeed() << std::endl; - con_print << " cargo: ^B" << maxcargo() << std::endl; } void ShipModel::generate_info() { - for (iterator it = registry.begin(); it != registry.end(); it++) { - ShipModel *shipmodel = (*it).second; - core::Info *info = new core::Info("ship/" + shipmodel->label()); - - info->clear_text(); - info->set_name(shipmodel->name()); - info->set_modelname(shipmodel->modelname()); - - // info text form ships.ini - for (core::Info::Text::iterator tit = shipmodel->shipmodel_infotext.begin(); - tit != shipmodel->shipmodel_infotext.end(); tit++) { - - info->add_text((*tit)); - } - - info->add_text(""); - info->add_text("^BSpecifications:^N"); - std::stringstream str; - str << "price: ^B" << shipmodel->price() << " ^Ncredits"; - info->add_text(str.str()); - str.str(""); - - str << "cargo hold: ^B" << 0.1f * shipmodel->maxcargo() << " ^Nmetric tonnes"; - info->add_text(str.str()); + clear_text(); + + add_text(""); + add_text("^B" + name() + " specifications:^N"); + std::stringstream str; + str << "price: ^B" << price() << " ^Ncredits"; + add_text(str.str()); + str.str(""); + + str << "cargo hold: ^B" << 0.1f * maxcargo() << " ^Nmetric tonnes"; + add_text(str.str()); + str.str(""); + + str << "top speed: ^B" << 100.0f * maxspeed() << " ^Nmps"; + add_text(str.str()); + str.str(""); + + str << "response: ^B" << turnspeed() << " ^Ndps"; + add_text(str.str()); + str.str(""); + + str << "acceleration: ^B" << acceleration() << " ^Nstandard"; + add_text(str.str()); + str.str(""); + + if (jumpdrive()) { + str << "hyperspace jump drive capable"; + add_text(str.str()); str.str(""); - - str << "top speed: ^B" << 100.0f * shipmodel->maxspeed() << " ^Nmps"; - info->add_text(str.str()); - str.str(""); - - str << "response: ^B" << shipmodel->turnspeed() << " ^Ndps"; - info->add_text(str.str()); - str.str(""); - - str << "acceleration: ^B" << shipmodel->acceleration() << " ^Nstandard"; - info->add_text(str.str()); - str.str(""); - - if (shipmodel->shipmodel_jumpdrive) { - str << "hyperspace jump drive capable"; - info->add_text(str.str()); - str.str(""); - } } } void ShipModel::list() { - for (iterator smit = registry.begin(); smit != registry.end(); smit++) { - con_print << std::setw(24) << (*smit).second->label() - << " ^B" << (*smit).second->name() << "\n"; - } - con_print << registry.size() << " registered ship models\n"; + core::Info::list_class(shipmodel_infotype); } -ShipModel *ShipModel::find(ShipModel *shipmodel) +ShipModel *ShipModel::find(const unsigned int id) { - for (iterator smit = registry.begin(); smit != registry.end(); smit++) { - if ((*smit).second == shipmodel) - return shipmodel; - } - - return 0; + return (ShipModel *) core::Info::find(shipmodel_infotype, id); } -ShipModel *ShipModel::find(const std::string label) +ShipModel *ShipModel::find(const std::string & label) { - if (!label.size()) - return 0; - - std::map::iterator it = registry.find(label); - if (it == registry.end()) - return 0; - else - return (*it).second; + return (ShipModel *) core::Info::find(shipmodel_infotype, label); } -ShipModel *ShipModel::search(const std::string searchname) +ShipModel *ShipModel::search(const std::string searchstr) { - if (!searchname.size()) - return 0; - - std::string strsearchkey(aux::lowercase(searchname)); - if (strsearchkey.size() < 3) { - return 0; - } - - std::string label; - std::string name; - - for (iterator smit = registry.begin(); smit != registry.end(); smit++) { - ShipModel *shipmodel = (*smit).second; - - label.assign(shipmodel->label()); - if (label.size() && (label.find(strsearchkey) != std::string::npos)) { - return shipmodel; - } - - name.assign(aux::lowercase(shipmodel->name())); - if (name.size() && (name.find(strsearchkey) != std::string::npos)) { - return shipmodel; - } - } - - return 0; -} - -// add a ship model -void ShipModel::add(ShipModel *shipmodel) -{ - ShipModel *m = find(shipmodel->label()); - if (m) { - con_warn << "Duplicate ship model " << shipmodel->label() << "!\n"; - delete m; - } - - registry[shipmodel->label()] = shipmodel; + return (ShipModel *) core::Info::search(shipmodel_infotype, searchstr); } } diff --git a/src/game/base/shipmodel.h b/src/game/base/shipmodel.h index d3048b2..19b2a77 100644 --- a/src/game/base/shipmodel.h +++ b/src/game/base/shipmodel.h @@ -7,7 +7,6 @@ #ifndef __INCLUDED_BASE_SHIPMODEL_H__ #define __INCLUDED_BASE_SHIPMODEL_H__ -#include #include #include "core/info.h" @@ -15,15 +14,23 @@ namespace game { -/// ship model specifications -class ShipModel +/// ship model specification +class ShipModel : public core::Info { public: - ShipModel(const unsigned int type_id); - ~ShipModel(); + /// default constructor + ShipModel(); - void print(); + /// default destructor + virtual ~ShipModel(); + /* ---- inspectors ------------------------------------------------ */ + + /// indicates if this model can be equiped with a jump drive + inline const bool jumpdrive() const { + return shipmodel_jumpdrive; + } + /// acceleration inline const float acceleration() const { return shipmodel_acceleration; @@ -39,36 +46,13 @@ public: return shipmodel_turnspeed; } - /// label of the ship model - inline const std::string &label() const { - return shipmodel_label; - } - - /// name of the ship model - inline const std::string & name() const { - return shipmodel_name; - } - - /// name of the model of the ship - inline const std::string & modelname() const { - return shipmodel_modelname; - } - - /// type id - inline unsigned int type_id() { - return shipmodel_type_id; - } - - /// price of the ship - inline const long price() const { - return shipmodel_price; - } - /// size of the cargo hold inline const float maxcargo() const { return shipmodel_maxcargo; } + /* ---- mutators -------------------------------------------------- */ + /// set acceleration inline void set_acceleration(const float acceleration) { shipmodel_acceleration = acceleration; @@ -84,66 +68,37 @@ public: shipmodel_turnspeed = turnspeed; } - /// set price - inline void set_price(const long price) { - shipmodel_price = price; - } - /// set size of the cargo hold inline void set_maxcargo(const float maxcargo) { shipmodel_maxcargo = maxcargo; } - /// indicates of this model can be equiped with a jump drive - bool shipmodel_jumpdrive; - - std::string shipmodel_label; - std::string shipmodel_name; - std::string shipmodel_modelname; - - /// info text - core::Info::Text shipmodel_infotext; - - /* ---- static registry ------------------------------------ */ + /// set jumpdrive capability + inline void set_jumpdrive(const bool jumpdrive) { + shipmodel_jumpdrive = jumpdrive; + } - typedef std::map Registry; + void generate_info(); - typedef std::map::iterator iterator; - - /// find an exact match - static ShipModel *find(ShipModel *shipmodel); - - /// find an exact match - static ShipModel *find(const std::string label); - - /// search the registry - static ShipModel *search(const std::string label); - - /// the ship model registry - static std::map registry; + /* --- static registry functions ---------------------------------- */ + + static void list(); + + static ShipModel *find(const unsigned int id); - /// clear the ship model registry - static void clear(); + static ShipModel *find(const std::string & label); - /// list the ship model registry - static void list(); + static ShipModel *search(const std::string searchstr); - /// add a new ship model - static void add(ShipModel *shipmodel); - - /// generate info records - static void generate_info(); + static core::InfoType *shipmodel_infotype; private: - + bool shipmodel_jumpdrive; + float shipmodel_acceleration; float shipmodel_maxspeed; float shipmodel_turnspeed; float shipmodel_maxcargo; - - long shipmodel_price; - - unsigned int shipmodel_type_id; }; } -- cgit v1.2.3