/* game/game.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include "game/game.h" #include "game/ship.h" #include "game/star.h" #include "core/gameserver.h" #include "sys/sys.h" #include "math/mathlib.h" #include "filesystem/filesystem.h" #include "filesystem/inifile.h" namespace game { ShipModel *default_shipmodel; /*----- engine game functions ------------------------------------- */ // 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, default_shipmodel); player->control()->entity_color = player->color(); std::string message(player->name()); message.append(" joins the game."); core::server()->broadcast(message); player->player_dirty = true; } /// a player joins the spectators void func_spectate(core::Player *player, std::string const &args) { if (!player->player_control) return; std::string message(player->name()); message.append(" spectates."); core::server()->broadcast(message); if (player->control()) { // player has only ship for now player->player_control->die(); player->player_control = 0; } player->player_dirty = true; } /// a player buys a ship void func_buy(core::Player *player, std::string const &args) { std::string shipname; std::string helpstr; std::istringstream is(args); is >> shipname; 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 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 " + shipmodel->name()); player->player_dirty = true; } else { core::server()->send(player, "Usage: buy <" + helpstr + ">"); } } /*----- Game ------------------------------------------------------ */ Game::Game() : core::Module("Project::OSiRiON") { } Game::~Game() { } void Game::init() { using math::Vector3f; using math::Color; module_running = false; ShipModel::clear(); // setup the game world filesystem::IniFile f; f.open("world"); if (!f.is_open()) { return; } Star *star = 0; core::Entity *entity = 0; while (f.getline()) { if (f.got_key()) { if (f.section().compare("star") == 0) { if (f.got_key_string("name", star->entity_name)) continue; else if (f.got_key_string("model", star->entity_modelname)) continue; else if (f.got_key_vector3f("location", star->entity_location )) continue; else if (f.got_key_color("color", star->entity_color)) continue; else con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl; } else if (f.section().compare("entity") == 0) { std::string shapename; if (f.got_key_string("shape", shapename)) { if (shapename.compare("axis") == 0) { entity->entity_shape = core::Entity::Axis; } else if (shapename.compare("cube") == 0) { entity->entity_shape = core::Entity::Cube; } else if (shapename.compare("diamond") == 0) { entity->entity_shape = core::Entity::Diamond; } else if (shapename.compare("sphere") == 0) { entity->entity_shape = core::Entity::Sphere; } else { con_warn << f.name() << " unknown shape '" << shapename << "' at line " << f.line() << std::endl; } continue; } else if (f.got_key_string("name", entity->entity_name)) continue; else if (f.got_key_string("model", entity->entity_modelname)) continue; else if (f.got_key_angle("direction", entity->entity_direction)) continue; else if (f.got_key_angle("radius", entity->entity_radius)) continue; else if (f.got_key_vector3f("location", entity->entity_location)) continue; else if (f.got_key_color("color", entity->entity_color)) continue; else con_warn << f.name() << " unknown key '" << f.key() << "' at line " << f.line() << std::endl; } } else if (f.got_section("star")) { star = new Star(); } else if (f.got_section("entity")) { entity = new core::Entity(); } else if (f.got_section()) { con_warn << f.name() << " unknown section '" << f.section() << "' at line " << f.line() << std::endl; } } f.close(); /* // the green cube core::Entity *cube = new core::Entity(core::Entity::Solid & core::Entity::Static); cube->entity_shape = core::Entity::Cube; cube->entity_color = Color(0.0f, 0.8f, 0.0f); cube->entity_location = Vector3f(24.0f, 24.0f, 0.0f); cube->entity_name ="ccube: Borg cube green"; cube->entity_modelname = "ccube"; cube->entity_moduletypeid = cube_enttype; // the red cube cube = new core::Entity(core::Entity::Solid & core::Entity::Static); cube->entity_shape = core::Entity::Cube; cube->entity_color = Color(1.0f, 0.0f, 0.0f); cube->entity_location = Vector3f(16.0f, 16.0f, 0.0f); cube->entity_name = "cube: Borg cube red"; cube->entity_modelname = "cube"; cube->entity_moduletypeid = cube_enttype; // the yellow sphere core::Entity *sphere = new core::Entity(core::Entity::Solid & core::Entity::Static); sphere->entity_shape = core::Entity::Sphere; sphere->entity_color = Color(0.8f, 0.8f, 0.0f); sphere->entity_location = Vector3f(0.0f, 32.0f, 0.0f); sphere->entity_name ="sphere: The Sphere"; // the galactic origin core::Entity *axis = new core::Entity(core::Entity::Static); axis->entity_shape = core::Entity::Diamond; axis->entity_color = Color(1.0f, 1.0f, 0.0f); axis->entity_location = Vector3f(0, 0, 0); axis->entity_name = "axis: Origin"; */ // 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",shipmodel->shipmodel_name)) { continue; } else if (f.got_key_string("model", shipmodel->shipmodel_modelname)) { continue; } else if (f.got_key("default")) { default_shipmodel = shipmodel; continue; } else if (f.got_key_float("acceleration", shipmodel->shipmodel_acceleration)) { continue; } else if (f.got_key_float("maxspeed", shipmodel->shipmodel_maxspeed)) { continue; } else if (f.got_key_float("turnspeed", shipmodel->shipmodel_turnspeed)) { continue; } 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); // indicate the module is ready to run frames module_running = true; } void Game::shutdown() { // game functions are automaticly removed // remove engine core functions core::Func::remove("list_ship"); ShipModel::clear(); module_running = false; } void Game::frame(float seconds) { if (!running()) return; } void Game::player_connect(core::Player *player) { std::string args; func_spectate(player, args); } void Game::player_disconnect(core::Player *player) { if (player->control()) { // player has only one ship for now player->control()->die(); player->player_control = 0; } } } // namespace game