From bfa10f9990a8a045b03474d11af75984c12a856a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Thu, 25 Dec 2008 12:13:56 +0000 Subject: Improved list_zone, list_ships Ship price and cargo size --- src/game/base/game.cc | 44 +++++++++++++++++-------------- src/game/base/ship.cc | 8 ++---- src/game/base/shipdealer.cc | 36 ++++++++++--------------- src/game/base/shipmodel.cc | 52 +++++++++++++++++++++++++++++++----- src/game/base/shipmodel.h | 64 ++++++++++++++++++++++++++++++++++++--------- 5 files changed, 136 insertions(+), 68 deletions(-) (limited to 'src/game') diff --git a/src/game/base/game.cc b/src/game/base/game.cc index 48e1b05..c591ba4 100644 --- a/src/game/base/game.cc +++ b/src/game/base/game.cc @@ -47,7 +47,12 @@ core::Module *factory() // list the ship model registry void Game::func_list_ship(std::string const &args) { - ShipModel::list(); + ShipModel *shipmodel = ShipModel::search(args); + + if (shipmodel) + shipmodel->print(); + else + ShipModel::list(); } // a player joins the game @@ -233,19 +238,7 @@ void Game::func_goto(core::Player *player, const std::string &args) if (!player->control()) return; - core::Entity *dock = 0; - std::string label(args); - aux::to_label(label); - - core::Zone *zone = player->control()->zone(); - for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it ++) { - std::string str((*it)->label()); - aux::to_label(str); - if (str.find(label) != std::string::npos) { - dock = (*it); - } - } - + core::Entity *dock = player->control()->zone()->search_entity(args); if (dock) { player->control()->entity_location.assign(dock->location() + (dock->axis().forward() * (player->control()->radius()+dock->radius())*2.0f)); player->control()->entity_axis.assign(dock->axis()); @@ -752,6 +745,8 @@ bool Game::load_ships() ShipModel *shipmodel = 0; std::string label; bool b; + long l; + float f; while (shipsini.getline()) { if (shipsini.got_key()) { @@ -768,17 +763,26 @@ bool Game::load_ships() } else if (shipsini.got_key_bool("default", b)) { if (b) default_shipmodel = shipmodel; continue; + } else if (shipsini.got_key_long("price", l)) { + shipmodel->set_price(l); + continue; + } else if (shipsini.got_key_float("cargo", f)) { + shipmodel->set_maxcargo(f); + continue; } else if (shipsini.got_key_bool("jumpdrive", shipmodel->shipmodel_jumpdrive)) { continue; - } else if (shipsini.got_key_float("acceleration", shipmodel->shipmodel_acceleration)) { + } else if (shipsini.got_key_float("acceleration",f)) { + shipmodel->set_acceleration(f); continue; - } else if (shipsini.got_key_float("maxspeed", shipmodel->shipmodel_maxspeed)) { + } else if (shipsini.got_key_float("maxspeed", f)) { + shipmodel->set_maxspeed(f); continue; - } else if (shipsini.got_key_float("turnspeed", shipmodel->shipmodel_turnspeed)) { - math::clamp(shipmodel->shipmodel_turnspeed, 0.0f, 90.0f); + } else if (shipsini.got_key_float("turnspeed", f)) { + math::clamp(f, 0.0f, 90.0f); + shipmodel->set_turnspeed(f); continue; } else { - con_warn << shipsini.name() << " unknown key '" << shipsini.key() << "' at line " << shipsini.line() << std::endl; + shipsini.unkown_key(); } } } else if (shipsini.got_section("ship")) { @@ -788,7 +792,7 @@ bool Game::load_ships() default_shipmodel = shipmodel; } else if (shipsini.got_section()) { - con_warn << shipsini.name() << " unknown section '" << shipsini.section() << "' at line " << shipsini.line() << std::endl; + shipsini.unknown_section(); } } shipsini.close(); diff --git a/src/game/base/ship.cc b/src/game/base/ship.cc index d0bea4a..c48bf7a 100644 --- a/src/game/base/ship.cc +++ b/src/game/base/ship.cc @@ -122,18 +122,14 @@ void Ship::func_jump(std::string const &args) // devel mode provides instant jump access to arbitrary systems if (Game::g_devel->value() && (args.size())) { - core::Zone *jumptargetzone = 0; - std::string target(args); + core::Zone *jumptargetzone = core::Zone::search(args); - aux::to_lowercase(target); - jumptargetzone = core::Zone::find_zone(target); if (!jumptargetzone) { std::string helpstr; for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) { - core::Zone *zone = (*it).second; if (helpstr.size()) helpstr.append("^N|^B"); - helpstr.append(zone->label()); + helpstr.append((*it).second->label()); } owner()->send("Usage: jump [^B" + helpstr + "^N]"); return; diff --git a/src/game/base/shipdealer.cc b/src/game/base/shipdealer.cc index fb6415c..c1d3b10 100644 --- a/src/game/base/shipdealer.cc +++ b/src/game/base/shipdealer.cc @@ -67,41 +67,33 @@ ShipModel *ShipDealer::find(ShipModel *shipmodel) const void ShipDealer::func_buy(core::Player *player, const std::string &args) { core::Entity *dock = player->view(); - ShipModel *shipmodel = 0; + ShipDealer *shipdealer = 0; // find the ship model - std::string shipname; - std::string helpstr; - std::istringstream is(args); - is >> shipname; - aux::to_label(shipname); - - if (shipname.size()) { - for (ShipModel::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) { - if (shipname == (*smit).first) { - shipmodel = (*smit).second; - break; - } - - if (helpstr.size()) - helpstr.append("^N|^B"); - helpstr.append((*smit).first); - } - } - + ShipModel *shipmodel = ShipModel::find(args); if (!shipmodel) { if (!Game::g_devel->value()) { player->send("Cheats disabled"); return; - } else { + shipmodel = ShipModel::search(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 [^B" + helpstr + "^N]"); return; } } - /// find the ship dealer we're buying the ship from + // find the ship dealer we're buying the ship from if (player->view()) { if (player->view()->moduletype() == station_enttype) { shipdealer = static_cast(player->view())->shipdealer(); diff --git a/src/game/base/shipmodel.cc b/src/game/base/shipmodel.cc index b4b60f4..78dc125 100644 --- a/src/game/base/shipmodel.cc +++ b/src/game/base/shipmodel.cc @@ -4,8 +4,11 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "sys/sys.h" +#include + +#include "auxiliary/functions.h" #include "base/shipmodel.h" +#include "sys/sys.h" namespace game { @@ -18,6 +21,8 @@ ShipModel::ShipModel() 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; } @@ -35,15 +40,21 @@ void ShipModel::clear() registry.clear(); } +void ShipModel::print() +{ + con_print << "label: ^B" << label() << " ^Nname: ^B" << name() << 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 << " max cargo: ^B" << maxcargo() << std::endl; + con_print << " price: ^B" << price() << std::endl; +} + void ShipModel::list() { for (iterator smit = registry.begin(); smit != registry.end(); smit++) { - con_print << - " " << (*smit).second->label() << - " " << (*smit).second->name() << - " accel " << (*smit).second->acceleration() << - " max " << (*smit).second->maxspeed() << - " turn " << (*smit).second->turnspeed() << "\n"; + con_print << std::setw(24) << (*smit).second->label() + << " ^B" << (*smit).second->name() << "\n"; } con_print << registry.size() << " registered ship models\n"; } @@ -67,6 +78,33 @@ ShipModel *ShipModel::find(const std::string label) return (*it).second; } +ShipModel *ShipModel::search(const std::string searchname) +{ + 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 new ship model void ShipModel::add(ShipModel *shipmodel) { diff --git a/src/game/base/shipmodel.h b/src/game/base/shipmodel.h index 6452b8d..c194dc0 100644 --- a/src/game/base/shipmodel.h +++ b/src/game/base/shipmodel.h @@ -19,42 +19,70 @@ public: ShipModel(); ~ShipModel(); + void print(); + /// acceleration - inline float acceleration() const { return shipmodel_acceleration; } + inline const float acceleration() const { return shipmodel_acceleration; } /// maximum speed - inline float maxspeed() const { return shipmodel_maxspeed; } + inline const float maxspeed() const { return shipmodel_maxspeed; } /// turn speed in rotations per second - inline float turnspeed() const { return shipmodel_turnspeed; } + inline const float turnspeed() const { return shipmodel_turnspeed; } /// label of the ship model - inline std::string const &label() const { return shipmodel_label; } + inline const std::string &label() const { return shipmodel_label; } /// name of the ship model - inline std::string const & name() const { return shipmodel_name; } + inline const std::string & name() const { return shipmodel_name; } /// name of the model of the ship - inline std::string const & modelname() const { return shipmodel_modelname; } + inline const std::string & modelname() const { return shipmodel_modelname; } + + /// 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; } + + /// set acceleration + inline void set_acceleration(const float acceleration) { shipmodel_acceleration = acceleration; } + + /// set maximum speed + inline void set_maxspeed(const float maxspeed) { shipmodel_maxspeed = maxspeed; } + + /// set turn speed + inline void set_turnspeed(const float turnspeed) { 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; - float shipmodel_acceleration; - float shipmodel_maxspeed; - float shipmodel_turnspeed; std::string shipmodel_label; std::string shipmodel_name; std::string shipmodel_modelname; - /// indicates of this model can be equiped with a jump drive - bool shipmodel_jumpdrive; + /* ---- static registry ------------------------------------ */ + + 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; - typedef std::map::iterator iterator; - /// clear the ship model registry static void clear(); @@ -63,6 +91,16 @@ public: /// add a new ship model static void add(ShipModel *shipmodel); + +private: + + float shipmodel_acceleration; + float shipmodel_maxspeed; + float shipmodel_turnspeed; + float shipmodel_maxcargo; + + long shipmodel_price; + }; } -- cgit v1.2.3