From 343896eeaa97009fb06096dc5bcc097bf1bd287d Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 8 Mar 2008 14:35:58 +0000 Subject: added ships.ini --- src/filesystem/inifile.cc | 2 +- src/game/Makefile.am | 4 +- src/game/game.cc | 120 ++++++++++++++++++++++++++++++++++++++++------ src/game/ship.cc | 33 +++++-------- src/game/ship.h | 14 ++---- src/game/shipmodel.cc | 57 ++++++++++++++++++++++ src/game/shipmodel.h | 59 +++++++++++++++++++++++ 7 files changed, 240 insertions(+), 49 deletions(-) create mode 100644 src/game/shipmodel.cc create mode 100644 src/game/shipmodel.h diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc index c76e06a..eccded4 100644 --- a/src/filesystem/inifile.cc +++ b/src/filesystem/inifile.cc @@ -23,7 +23,7 @@ void IniFile::open(std::string const & name) { section_current = ""; line_number = 0; - std::string inifile_name("ini/"); + inifile_name.assign("ini/"); inifile_name.append(name); inifile_name.append(".ini"); diff --git a/src/game/Makefile.am b/src/game/Makefile.am index a9685cc..e9f8d0b 100644 --- a/src/game/Makefile.am +++ b/src/game/Makefile.am @@ -2,7 +2,7 @@ INCLUDES = -I$(top_srcdir)/src METASOURCES = AUTO libgame_la_LDFLAGS = -avoid-version -libgame_la_SOURCES = game.cc ship.cc star.cc +libgame_la_SOURCES = game.cc ship.cc shipmodel.cc star.cc noinst_LTLIBRARIES = libgame.la -noinst_HEADERS = game.h ship.h star.h +noinst_HEADERS = game.h ship.h shipmodel.h star.h diff --git a/src/game/game.cc b/src/game/game.cc index f0fd190..290b80c 100644 --- a/src/game/game.cc +++ b/src/game/game.cc @@ -17,15 +17,22 @@ namespace game { +ShipModel *default_shipmodel; /*----- engine game functions ------------------------------------- */ -/// a player joins the game +// list the ship model registry +void func_list_ship(std::string const &args) +{ + ShipModel::list(); +} + +// a player joins the game void func_join(core::Player *player, std::string const &args) { if (player->control()) return; - player->player_control = new Ship(player, "vector"); + player->player_control = new Ship(player, default_shipmodel); player->control()->entity_color = player->color(); std::string message(player->name()); @@ -57,25 +64,38 @@ void func_spectate(core::Player *player, std::string const &args) /// a player buys a ship void func_buy(core::Player *player, std::string const &args) { - if (!player->control()) { - return; - } - + std::string shipname; + std::string helpstr; std::istringstream is(args); is >> shipname; - if ((shipname == "vector") || (shipname == "canasta")) { + + ShipModel *shipmodel = 0; + for (std::list::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) { + if (shipname == (*smit)->modelname()) { + shipmodel = (*smit); + break; + } + + if (helpstr.size()) + helpstr += '|'; + helpstr.append((*smit)->modelname()); + } + + if (shipmodel) { // player has only ship for now - player->player_control->die(); - player->player_control = 0; - - player->player_control = new Ship(player, shipname); + if (player->control()) { + player->player_control->die(); + player->player_control = 0; + } + + player->player_control = new Ship(player, shipmodel); player->control()->entity_color = player->color(); - core::server()->broadcast(player->name() + " purchased a " + shipname); + core::server()->broadcast(player->name() + " purchased a " + shipmodel->name()); player->player_dirty = true; } else { - core::server()->send(player, "Usage: buy "); + core::server()->send(player, "Usage: buy <" + helpstr + ">"); } } /*----- Game ------------------------------------------------------ */ @@ -95,6 +115,8 @@ void Game::init() module_running = false; + ShipModel::clear(); + // setup the game world filesystem::IniFile f; f.open("world"); @@ -242,11 +264,75 @@ void Game::init() alexandria->entity_modelname = "stations/alexandria"; */ + // read ship model specifications + f.open("ships"); + if (!f.is_open()) + return; + + ShipModel *shipmodel = 0; + default_shipmodel = 0; + + while (f.getline()) { + if (f.got_key()) { + if (f.section() == "ship") { + + if (f.got_key_string("name", tmp)) { + shipmodel->shipmodel_name = tmp; + + } else if (f.got_key_string("model", tmp)) { + shipmodel->shipmodel_modelname = tmp; + + } else if (f.got_key_string("default", tmp)) { + + default_shipmodel = shipmodel; + + } else if (f.got_key_string("acceleration", tmp)) { + std::istringstream is(tmp); + float a; + if (is >> a) { + shipmodel->shipmodel_acceleration = a; + } + + } else if (f.got_key_string("maxspeed", tmp)) { + std::istringstream is(tmp); + float ms; + if (is >> ms) { + shipmodel->shipmodel_maxspeed = ms; + } + + } else if (f.got_key_string("turnspeed", tmp)) { + + std::istringstream is(tmp); + float ts; + if (is >> ts) { + shipmodel->shipmodel_turnspeed = ts; + } + + } else + con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl; + } + } else if (f.got_section("ship")) { + shipmodel = new ShipModel(); + + } else if (f.got_section()) { + con_warn << f.name() << " unknown section '" << f.section() << "' at line " << f.line() << std::endl; + } + } + f.close(); + + if (!default_shipmodel) { + con_error << "No default ship model\n"; + return; + } + // add engine game functions core::Func::add("buy", (core::GameFuncPtr) func_buy); core::Func::add("join", (core::GameFuncPtr) func_join); core::Func::add("spectate", (core::GameFuncPtr) func_spectate); - + + // add engine core functions + core::Func::add("list_ship", (core::FuncPtr) func_list_ship); + // add engine game variables core::Cvar::set("g_borgcubes", "2", core::Cvar::Game); core::Cvar::set("g_name", name().c_str(), core::Cvar::Game | core::Cvar::ReadOnly); @@ -257,6 +343,12 @@ void Game::init() void Game::shutdown() { + // game functions are automaticly removed + + // remove engine core functions + core::Func::remove("list_ship"); + + ShipModel::clear(); module_running = false; } diff --git a/src/game/ship.cc b/src/game/ship.cc index 19cccbe..5edbd7d 100644 --- a/src/game/ship.cc +++ b/src/game/ship.cc @@ -17,25 +17,14 @@ using math::degrees180f; namespace game { -Ship::Ship(core::Player *owner, std::string const & model) : +Ship::Ship(core::Player *owner, ShipModel *shipmodel) : core::EntityControlable(owner, ship_enttype) { // entity properties entity_owner = owner; - - // ship model - if (model.size()) { - entity_modelname = "ships/" + model; - entity_name = model + ": <" + owner->name() + ">"; - } else { - entity_modelname = "ships/micron_vector"; - entity_name = "micron_vector: <" + owner->name() + ">"; - } - - // ship specs - acceleration = 1.5f; - max_speed = 4.0f; - turn_speed = 0.5f; + entity_modelname = "ships/" + shipmodel->modelname(); + entity_name = shipmodel->name() + ": <" + owner->name() + ">"; + ship_shipmodel = shipmodel; } Ship::~Ship() @@ -53,17 +42,17 @@ void Ship::frame(float seconds) // update direction float direction_offset = degrees180f(target_direction - entity_direction); - float d = turn_speed * seconds * direction_offset; + float d = ship_shipmodel->turnspeed() * seconds * direction_offset; entity_direction = degrees360f(entity_direction + d); // update speed - if (entity_speed < entity_thrust * max_speed) { - entity_speed += acceleration * seconds; - if (entity_speed > entity_thrust * max_speed) { - entity_speed = entity_thrust * max_speed; + if (entity_speed < entity_thrust * ship_shipmodel->maxspeed()) { + entity_speed += ship_shipmodel->acceleration() * seconds; + if (entity_speed > entity_thrust * ship_shipmodel->maxspeed()) { + entity_speed = entity_thrust * ship_shipmodel->maxspeed(); } - } else if(entity_speed > entity_thrust * max_speed) { - entity_speed -= acceleration * seconds; + } else if(entity_speed > entity_thrust * ship_shipmodel->maxspeed()) { + entity_speed -= ship_shipmodel->acceleration() * seconds; if (entity_speed < 0) entity_speed = 0; } diff --git a/src/game/ship.h b/src/game/ship.h index 88f42e5..633c722 100644 --- a/src/game/ship.h +++ b/src/game/ship.h @@ -9,6 +9,7 @@ #include "core/player.h" #include "core/entity.h" +#include "game/shipmodel.h" #include "math/vector3f.h" namespace game { @@ -17,21 +18,14 @@ namespace game { class Ship : public core::EntityControlable { public: - Ship(core::Player *owner, std::string const & model); + Ship(core::Player *owner, ShipModel *shipmodel); ~Ship(); /// update the ship state virtual void frame(float seconds); - /* -- Ship SPECS --*/ - /// acceleration - float acceleration; - - /// maximum speed - float max_speed; - - /// turn speed in rotations per second - float turn_speed; +private: + ShipModel *ship_shipmodel; }; } diff --git a/src/game/shipmodel.cc b/src/game/shipmodel.cc new file mode 100644 index 0000000..f37e5b2 --- /dev/null +++ b/src/game/shipmodel.cc @@ -0,0 +1,57 @@ +/* + game/shipmodel.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "sys/sys.h" +#include "game/shipmodel.h" + +namespace game { + +// the ship model registry +std::list ShipModel::registry; + +ShipModel::ShipModel() +{ + //default specifications + shipmodel_acceleration = 1.5f; + shipmodel_maxspeed = 4.0f; + shipmodel_turnspeed = 0.5f; + + add(this); +} + +ShipModel::~ShipModel() +{} + + +// clear the ship model registry +void ShipModel::clear() +{ + for (std::list< ShipModel *>::iterator smit=registry.begin(); smit != registry.end(); smit++) { + delete (*smit); + } + registry.clear(); +} + +void ShipModel::list() +{ + for (std::list< ShipModel *>::iterator smit=registry.begin(); smit != registry.end(); smit++) { + con_print << + " " << (*smit)->modelname() << + " " << (*smit)->name() << + " accel " << (*smit)->acceleration() << + " max " << (*smit)->maxspeed() << + " turn " << (*smit)->turnspeed() << "\n"; + } + con_print << registry.size() << " registered ship models\n"; +} + +// add a new ship model +void ShipModel::add(ShipModel *shipmodel) +{ + registry.push_back(shipmodel); +} + +} diff --git a/src/game/shipmodel.h b/src/game/shipmodel.h new file mode 100644 index 0000000..fd42c81 --- /dev/null +++ b/src/game/shipmodel.h @@ -0,0 +1,59 @@ +/* + game/shipmodel.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_GAME_SHIPMODEL_H__ +#define __INCLUDED_GAME_SHIPMODEL_H__ + +#include +#include + +namespace game { + +/// ship model specifications +class ShipModel +{ +public: + ShipModel(); + ~ShipModel(); + + /// acceleration + inline float acceleration() const { return shipmodel_acceleration; } + + /// maximum speed + inline float maxspeed() const { return shipmodel_maxspeed; } + + /// turn speed in rotations per second + inline float turnspeed() const { return shipmodel_turnspeed; } + + /// name of the model to use + inline std::string const & modelname() const { return shipmodel_modelname; } + + /// name of the ship model + inline std::string const & name() const { return shipmodel_name; } + + float shipmodel_acceleration; + float shipmodel_maxspeed; + float shipmodel_turnspeed; + std::string shipmodel_name; + std::string shipmodel_modelname; + + /// the ship model registry + static std::list registry; + + /// clear the ship model registry + static void clear(); + + /// list the ship model registry + static void list(); +private: + /// add a new ship model + static void add(ShipModel *shipmodel); +}; + +} + +#endif // __INCLUDED_GAME_SHIPMODEL_H__ + -- cgit v1.2.3