Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/auxiliary/functions.cc5
-rw-r--r--src/core/descriptions.cc2
-rw-r--r--src/core/descriptions.h6
-rw-r--r--src/game/base/Makefile.am4
-rw-r--r--src/game/base/game.cc376
-rw-r--r--src/game/base/game.h2
-rw-r--r--src/game/base/planet.cc12
-rw-r--r--src/game/base/planet.h14
-rw-r--r--src/game/base/shipdealer.cc144
-rw-r--r--src/game/base/shipdealer.h44
-rw-r--r--src/game/base/shipmodel.cc63
-rw-r--r--src/game/base/shipmodel.h5
-rw-r--r--src/game/base/station.cc13
-rw-r--r--src/game/base/station.h12
14 files changed, 271 insertions, 431 deletions
diff --git a/src/auxiliary/functions.cc b/src/auxiliary/functions.cc
index 5dbc012..47f7fb2 100644
--- a/src/auxiliary/functions.cc
+++ b/src/auxiliary/functions.cc
@@ -135,10 +135,11 @@ const std::string lowercase(const std::string &text)
void trim(std::string &text)
{
- while (text.size() && text[0] == ' ') {
+ // remove spaces and tabs
+ while (text.size() && ((text[0] == ' ') || (text[0] == 9))) {
text.erase(0, 1);
}
- while (text.size() && text[text.size()-1] == ' ') {
+ while (text.size() && ((text[text.size()-1] == ' ') || (text[text.size()-1] == 9))) {
text.erase(text.size() - 1, 1);
}
}
diff --git a/src/core/descriptions.cc b/src/core/descriptions.cc
index 26a5858..2cfab99 100644
--- a/src/core/descriptions.cc
+++ b/src/core/descriptions.cc
@@ -40,7 +40,7 @@ void ButtonDescription::set_alignment(Align align)
button_align = align;
}
-void ButtonDescription::set_info(Info *info)
+void ButtonDescription::set_info(const Info *info)
{
button_info = info;
}
diff --git a/src/core/descriptions.h b/src/core/descriptions.h
index 05e92af..b1b0901 100644
--- a/src/core/descriptions.h
+++ b/src/core/descriptions.h
@@ -58,7 +58,7 @@ public:
}
/// button info record
- inline Info *info() {
+ inline const Info *info() const {
return button_info;
}
@@ -74,14 +74,14 @@ public:
void set_alignment(Align align);
/// set info record
- void set_info(Info *info);
+ void set_info(const Info *info);
private:
std::string button_text;
CommandType button_commandtype;
std::string button_command;
Align button_align;
- Info *button_info;
+ const Info *button_info;
};
/// description of an entity menu
diff --git a/src/game/base/Makefile.am b/src/game/base/Makefile.am
index f7fd52e..b86d14a 100644
--- a/src/game/base/Makefile.am
+++ b/src/game/base/Makefile.am
@@ -3,7 +3,7 @@ METASOURCES = AUTO
libbase_la_LDFLAGS = -avoid-version
noinst_LTLIBRARIES = libbase.la
libbase_la_SOURCES = collision.cc cargo.cc game.cc jumppoint.cc navpoint.cc physics.cc \
- planet.cc racetrack.cc ship.cc shipdealer.cc shipmodel.cc star.cc station.cc
+ planet.cc racetrack.cc ship.cc shipmodel.cc star.cc station.cc
noinst_HEADERS = game.h collision.h cargo.h jumppoint.h navpoint.h physics.h planet.h \
- racetrack.h ship.h shipdealer.h shipmodel.h star.h station.h
+ racetrack.h ship.h shipmodel.h star.h station.h
diff --git a/src/game/base/game.cc b/src/game/base/game.cc
index 803657d..fcecf58 100644
--- a/src/game/base/game.cc
+++ b/src/game/base/game.cc
@@ -261,7 +261,7 @@ void Game::func_give(core::Player *player, const std::string &args)
} else {
player->set_credits(0);
}
-
+ player->set_dirty();
player->sound("game/buy");
} else {
@@ -335,7 +335,12 @@ void Game::func_buy(core::Player *player, const std::string &args)
amount = 0;
if (typestr.compare("ship") == 0) {
- ShipDealer::func_buy(player, labelstr);
+ ShipModel *shipmodel = ShipModel::find(labelstr);
+ if (shipmodel) {
+ shipmodel->buy(player->control(), player->view());
+ } else {
+ player->send("Unkown ship type '" + labelstr + "'");
+ }
} else if (typestr.compare("cargo") == 0) {
Cargo *cargo = Cargo::find(labelstr);
if (cargo) {
@@ -672,16 +677,20 @@ bool Game::load_zone(core::Zone *zone)
size_t count = 0;
- Station *station = 0;
- Planet *planet = 0;
- Star *star = 0;
- NavPoint *navpoint = 0;
- JumpPoint *jumppoint = 0;
- RaceTrack *racetrack = 0;
- CheckPoint *checkpoint = 0;
- core::Entity *entity = 0;
+ core::Entity *entity = 0;
+ core::Inventory *inventory = 0;
+ core::Item *item = 0;
+
+ Station *station = 0;
+ Planet *planet = 0;
+ Star *star = 0;
+ NavPoint *navpoint = 0;
+ JumpPoint *jumppoint = 0;
+ RaceTrack *racetrack = 0;
+ CheckPoint *checkpoint = 0;
bool b;
+ long l;
std::string strval;
@@ -696,41 +705,49 @@ bool Game::load_zone(core::Zone *zone)
} else if (zoneini.got_section("star")) {
star = new Star();
+ entity = star;
star->set_zone(zone);
count ++;
} else if (zoneini.got_section("navpoint")) {
navpoint = new NavPoint();
+ entity = navpoint;
navpoint->set_zone(zone);
count ++;
} else if (zoneini.got_section("jumpgate")) {
jumppoint = new JumpGate();
+ entity = jumppoint;
jumppoint->set_zone(zone);
count ++;
} else if (zoneini.got_section("jumppoint")) {
jumppoint = new JumpPoint();
+ entity = jumppoint;
jumppoint->set_zone(zone);
count ++;
} else if (zoneini.got_section("racetrack")) {
racetrack = new RaceTrack();
+ entity = racetrack;
racetrack->set_zone(zone);
} else if (zoneini.got_section("checkpoint")) {
checkpoint = new CheckPoint(racetrack);
+ entity = checkpoint;
if (!racetrack) {
- con_warn << zoneini.name() << " checkpoint without racetrack at line " << zoneini.line() << std::endl;
+ zoneini.unknown_error("checkpoint without racetrack");
}
} else if (zoneini.got_section("planet")) {
planet = new Planet();
+ entity = planet;
planet->set_zone(zone);
count ++;
} else if (zoneini.got_section("station")) {
station = new Station();
+ entity = station;
station->set_zone(zone);
count ++;
@@ -739,7 +756,41 @@ bool Game::load_zone(core::Zone *zone)
entity->set_flag(core::Entity::Static);
entity->set_zone(zone);
count ++;
-
+
+ } else if (zoneini.got_section("cargo")) {
+ // new cargo trading definition for the current base
+ item = 0;
+ inventory = 0;
+
+ if (!entity) {
+ zoneini.unknown_error("cargo definition without entity");
+ } else if ((entity->moduletype() != planet_enttype) && (entity->moduletype() != station_enttype)) {
+ zoneini.unknown_error("cargo definition for invalid entity type");
+ } else {
+ inventory = entity->inventory();
+ if (!inventory) {
+ inventory = new core::Inventory();
+ entity->set_inventory(inventory);
+ }
+ }
+
+ } else if (zoneini.got_section("ship")) {
+ // new ship trading definition for the current base
+ item = 0;
+ inventory = 0;
+
+ if (!entity) {
+ zoneini.unknown_error("ship definition without entity");
+ } else if ((entity->moduletype() != planet_enttype) && (entity->moduletype() != station_enttype)) {
+ zoneini.unknown_error("ship definition for invalid entity type");
+ } else {
+ inventory = entity->inventory();
+ if (!inventory) {
+ inventory = new core::Inventory();
+ entity->set_inventory(inventory);
+ }
+ }
+
} else {
zoneini.unknown_section();
}
@@ -841,8 +892,68 @@ bool Game::load_zone(core::Zone *zone)
} else {
zoneini.unkown_key();
}
- }
-
+
+ } else if (zoneini.in_section("cargo")) {
+ // cargo definition for a station or planet
+ if (!entity || !inventory) {
+ continue;
+ }
+
+ if (zoneini.got_key_label("label", strval)) {
+ Cargo *cargo = Cargo::find(strval);
+ if (cargo) {
+ item = inventory->find(cargo);
+ if (!item) {
+ item = new core::Item(cargo);
+ item->set_amount(-1);
+ item->set_price(cargo->price());
+ inventory->add(item);
+ }
+ } else {
+ zoneini.unknown_error("unkown cargo type '" + strval + "'");
+ }
+
+ } else if (zoneini.got_key_long("price", l)) {
+ if (item) {
+ item->set_price(l);
+ }
+ } else if (zoneini.got_key_long("amount", l)) {
+ if (item) {
+ item->set_amount(l);
+ }
+ } else {
+ zoneini.unkown_key();
+ }
+
+ } else if (zoneini.in_section("ship")) {
+ // ship definition for a station or planet
+ if (!entity || !inventory) {
+ continue;
+ }
+
+ if (zoneini.got_key_label("label", strval)) {
+ ShipModel *shipmodel= ShipModel::find(strval);
+ if (shipmodel) {
+ item = inventory->find(shipmodel);
+ if (!item) {
+ item = new core::Item(shipmodel);
+ item->set_amount(-1);
+ item->set_price(shipmodel->price());
+ inventory->add(item);
+ }
+ } else {
+ zoneini.unknown_error("unkown ship type '" + strval + "'");
+ }
+
+ } else if (zoneini.got_key_long("price", l)) {
+ if (item) {
+ item->set_price(l);
+ }
+
+ } else {
+ zoneini.unkown_key();
+ }
+ }
}
}
zoneini.close();
@@ -874,204 +985,103 @@ bool Game::validate_zone(core::Zone *zone)
station->init_physics(0.0f);
}
- if ((entity->flags() & core::Entity::Dockable) == core::Entity::Dockable)
- load_menus(entity, "zones/" + zone->label() + "/" + entity->label());
+ if ((entity->flags() & core::Entity::Dockable) == core::Entity::Dockable) {
+ generate_entity_menus(entity);
+ }
}
}
return true;
}
-bool Game::load_menus(core::Entity *entity, const std::string &menufilename)
+bool Game::generate_entity_menus(core::Entity *entity)
{
using core::MenuDescription;
using core::ButtonDescription;
if ((entity->moduletype() != planet_enttype) && (entity->moduletype() != station_enttype)) {
+ //con_warn << "Can not generate menus for entity '" << entity->label() << "'" << std::endl;
+ // not dockable
return false;
}
-
- filesystem::IniFile inifile;
-
- std::string strval;
- long l;
-
+
MenuDescription *menu_dealer = 0;
ButtonDescription *button = 0;
- ShipDealer *shipdealer = 0;
- core::Inventory *inventory = 0;
- core::Item *item = 0;
-
- inifile.open(menufilename);
-
- if (inifile.is_open()) {
- while (inifile.getline()) {
-
- if (inifile.got_section()) {
-
- item = 0;
-
- if (inifile.got_section("dealer")) {
- // TODO replace [dealer] section with individual [ship] sections
- // dealer menu
- if (!menu_dealer) {
- menu_dealer = new MenuDescription();
- menu_dealer->set_label("ships");
- menu_dealer->set_text("Ships");
-
- shipdealer = new ShipDealer();
- if (entity->moduletype() == planet_enttype) {
- static_cast<Planet *>(entity)->set_shipdealer(shipdealer);
- } else if (entity->moduletype() == station_enttype) {
- static_cast<Station *>(entity)->set_shipdealer(shipdealer);
- }
- }
-
- } else if (inifile.got_section("cargo")) {
-
- } else if (inifile.got_section("ship")) {
-
- } else {
- inifile.unknown_section();
- }
-
- } else if (inifile.got_key()) {
-
- if (inifile.in_section("dealer")) {
-
- if (inifile.got_key_string("ship", strval)) {
- aux::to_label(strval);
- ShipModel *model = shipdealer->add(strval);
- if (model) {
- button = new ButtonDescription();
- button->set_text("buy " + model->name());
- std::ostringstream str("");
- str << "buy " << model->id();
- button->set_command(str.str() , ButtonDescription::CommandMenu);
- button->set_info(model);
- button->set_alignment(ButtonDescription::Left);
- menu_dealer->add_button(button);
- } else {
- std::string msg("unknown ship type '");
- msg.append(strval);
- msg.append("'");
- inifile.unknown_error(msg);
- }
- } else {
- inifile.unkown_key();
- }
-
- } else if (inifile.in_section("cargo")) {
-
- if (inifile.got_key_label("label", strval)) {
- Cargo *cargo = Cargo::find(strval);
- if (cargo) {
- if (!inventory) {
- inventory = new core::Inventory();
- }
- item = inventory->find(cargo);
- if (!item) {
- item = new core::Item(cargo);
- item->set_amount(-1);
- item->set_price(cargo->price());
- inventory->add(item);
- }
- } else {
- std::string msg("unkown cargo type '");
- msg.append(strval);
- msg.append("'");
- inifile.unknown_error(msg);
- }
-
- } else if (inifile.got_key_long("price", l)) {
- if (item) {
- item->set_price(l);
- }
- } else if (inifile.got_key_long("amount", l)) {
- if (item) {
- item->set_amount(l);
- }
- } else {
- inifile.unkown_key();
- }
-
- } else if (inifile.in_section("ship")) {
-
- if (inifile.got_key_label("label", strval)) {
- ShipModel *shipmodel = ShipModel::find(strval);
- if (shipmodel) {
- if (!inventory) {
- inventory = new core::Inventory();
- }
- item = inventory->find(shipmodel);
- if (!item) {
- item = new core::Item(shipmodel);
- item->set_price(shipmodel->price());
- item->set_amount(-1);
- inventory->add(item);
- }
- } else {
- std::string msg("unknown ship type '");
- msg.append(strval);
- msg.append("'");
- inifile.unknown_error(msg);
- }
- } else if (inifile.got_key_long("price", l)) {
- if (item) {
- item->set_price(l);
- }
- } else {
- inifile.unkown_key();
- }
- }
- }
- }
-
- }
-
- if (inventory)
- entity->set_inventory(inventory);
-
+ // dockable entity
+ // add main menu
MenuDescription *menu_main = new MenuDescription();
menu_main->set_label("main");
menu_main->set_text("Launch area");
entity->add_menu(menu_main);
+ // add launch button
button = new ButtonDescription();
button->set_text("Launch");
button->set_command("launch", ButtonDescription::CommandGame);
button->set_alignment(ButtonDescription::Center);
menu_main->add_button(button);
- button = new ButtonDescription();
- button->set_text("Trade");
- button->set_command("trade cargo", ButtonDescription::CommandMenu);
- button->set_alignment(ButtonDescription::Center);
- menu_main->add_button(button);
-
- if (menu_dealer) {
- button = new ButtonDescription();
- button->set_text("Return");
- button->set_command("main", ButtonDescription::CommandMenu);
- button->set_alignment(ButtonDescription::Center);
- menu_dealer->add_button(button);
-
- entity->add_menu(menu_dealer);
-
- button = new ButtonDescription();
- button->set_text("Ships");
- button->set_command("ships", ButtonDescription::CommandMenu);
- button->set_alignment(ButtonDescription::Center);
- menu_main->add_button(button);
- }
+ // add trade menus
+ if (entity->inventory()) {
+ size_t nbcargo = 0;
+ size_t nbships = 0;
+
+ for (core::Inventory::Items::const_iterator it = entity->inventory()->items().begin(); it != entity->inventory()->items().end(); it++) {
+ core::Item *item = (*it);
+
+ if (item->info()->type() == Cargo::cargo_infotype) {
+
+ nbcargo++;
+
+ } else if (item->info()->type() == ShipModel::shipmodel_infotype) {
+ if (!menu_dealer) {
+ menu_dealer = new MenuDescription();
+ menu_dealer->set_label("ships");
+ menu_dealer->set_text("Ships");
+ }
+
+ button = new ButtonDescription();
+ button->set_text("buy " + item->info()->name());
+
+ std::ostringstream str("");
+ str << "buy " << item->info()->id();
+ button->set_command(str.str() , ButtonDescription::CommandMenu);
+ button->set_info(item->info());
+ button->set_alignment(ButtonDescription::Left);
+
+ menu_dealer->add_button(button);
+ nbships++;
+ }
+ }
+
+ if (nbcargo > 0) {
+ con_debug << " " << entity->label() << " " << nbcargo << " cargo " << aux::plural("type", nbcargo) << std::endl;
+ button = new ButtonDescription();
+ button->set_text("Trade");
+ button->set_command("trade cargo", ButtonDescription::CommandMenu);
+ button->set_alignment(ButtonDescription::Center);
+ menu_main->add_button(button);
+ }
- if (inifile.is_open()) {
- size_t n = entity->menus().size();
- con_debug << " " << inifile.name() << " " << n << " " << aux::plural("menu", n) << std::endl;
- inifile.close();
+ if (nbships > 0) {
+ con_debug << " " << entity->label() << " " << nbcargo << " ship " << aux::plural("type", nbcargo) << std::endl;
+ button = new ButtonDescription();
+ button->set_text("Return");
+ button->set_command("main", ButtonDescription::CommandMenu);
+ button->set_alignment(ButtonDescription::Center);
+ menu_dealer->add_button(button);
+
+ entity->add_menu(menu_dealer);
+
+ button = new ButtonDescription();
+ button->set_text("Ships");
+ button->set_command("ships", ButtonDescription::CommandMenu);
+ button->set_alignment(ButtonDescription::Center);
+ menu_main->add_button(button);
+ }
}
-
+
return true;
}
diff --git a/src/game/base/game.h b/src/game/base/game.h
index ce37fa6..1aaba51 100644
--- a/src/game/base/game.h
+++ b/src/game/base/game.h
@@ -87,7 +87,7 @@ private:
bool validate_zone(core::Zone *zone);
- bool load_menus(core::Entity *entity, const std::string &menufilename);
+ bool generate_entity_menus(core::Entity *entity);
bool load_commodities();
diff --git a/src/game/base/planet.cc b/src/game/base/planet.cc
index ca213af..ad1aa47 100644
--- a/src/game/base/planet.cc
+++ b/src/game/base/planet.cc
@@ -23,25 +23,13 @@ Planet::Planet() : core::EntityGlobe()
entity_moduletypeid = planet_enttype;
entity_rotationspeed = 1.0f;
-
- planet_shipdealer = 0;
}
Planet::~Planet()
{
- if (planet_shipdealer)
- delete planet_shipdealer;
-}
-void Planet::set_shipdealer(ShipDealer *shipdealer)
-{
- if (planet_shipdealer)
- delete planet_shipdealer;
-
- planet_shipdealer = shipdealer;
}
-
void Planet::dock(core::Entity *entity)
{
if (entity->moduletype() != ship_enttype)
diff --git a/src/game/base/planet.h b/src/game/base/planet.h
index efc5622..d2b18a8 100644
--- a/src/game/base/planet.h
+++ b/src/game/base/planet.h
@@ -7,7 +7,6 @@
#ifndef __INCLUDED_BASE_PLANET_H__
#define __INCLUDED_BASE_PLANET_H__
-#include "base/shipdealer.h"
#include "core/entity.h"
#include "math/mathlib.h"
@@ -22,20 +21,9 @@ class Planet : public core::EntityGlobe
public:
Planet();
virtual ~Planet();
-
- inline ShipDealer *shipdealer() {
- return planet_shipdealer;
- }
-
- void set_shipdealer(ShipDealer *shipdealer);
-
+
/// entity received a docking request
virtual void dock(core::Entity *entity);
-
-
-private:
- ShipDealer *planet_shipdealer;
-
};
/// FIXME
diff --git a/src/game/base/shipdealer.cc b/src/game/base/shipdealer.cc
deleted file mode 100644
index e6df18f..0000000
--- a/src/game/base/shipdealer.cc
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- base/shipdealer.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 "auxiliary/functions.h"
-#include "base/game.h"
-#include "base/planet.h"
-#include "base/shipdealer.h"
-#include "base/station.h"
-#include "sys/sys.h"
-
-namespace game
-{
-
-ShipDealer::ShipDealer()
-{
-}
-
-ShipDealer::~ShipDealer()
-{
- dealer_models.clear();
-}
-
-ShipModel *ShipDealer::add(const std::string &modelname)
-{
- ShipModel *model = ShipModel::find(modelname);
-
- if (!model) {
- con_warn << "Ship model '" + modelname + "' not found" << std::endl;
- return 0;
- }
-
- for (Models::iterator it = dealer_models.begin(); it != dealer_models.end(); it++) {
- if ((*it) == model) {
- con_warn << "Ship dealer already has " + model->name() << std::endl;
- return 0;
- }
- }
- dealer_models.push_back(model);
- return model;
-}
-
-ShipModel *ShipDealer::find(const std::string &modelname) const
-{
- for (Models::const_iterator it = dealer_models.begin(); it != dealer_models.end(); it++) {
- if ((*it)->label().compare(modelname) == 0) {
- return (*it);
- }
- }
-
- return 0;
-}
-
-ShipModel *ShipDealer::find(ShipModel *shipmodel) const
-{
- for (Models::const_iterator it = dealer_models.begin(); it != dealer_models.end(); it++) {
- if ((*it) == shipmodel) {
- return (*it);
- }
- }
-
- return 0;
-}
-
-// a player buys a ship
-void ShipDealer::func_buy(core::Player *player, const std::string &args)
-{
- core::Entity *dock = player->view();
- ShipDealer *shipdealer = 0;
-
- std::istringstream is(args);
- std::string labelstr;
-
- if (!(is >> labelstr)) {
- player->send("Usage: buy ship [string]");
- }
-
- // find the ship model
- ShipModel *shipmodel = ShipModel::find(labelstr);
- if (!shipmodel) {
- player->send("Unkown ship type '" + labelstr + "'");
- return;
- }
-
- // find the ship dealer we're buying the ship from
- if (player->view()) {
- if (player->view()->moduletype() == station_enttype) {
- shipdealer = static_cast<Station *>(player->view())->shipdealer();
- } else if (player->view()->moduletype() == planet_enttype) {
- shipdealer = static_cast<Planet *>(player->view())->shipdealer();
- }
- }
-
- if (!shipdealer) {
- player->send("No ship dealer available");
- return;
- }
-
- if (!shipdealer->find(shipmodel)) {
- player->send("Ship dealer does not sell the " + shipmodel->name());
- return;
- }
-
- // check price
- if (shipmodel->price() > player->credits()) {
- std::stringstream msgstr;
- msgstr << "You require " << (shipmodel->price() - player->credits()) << " additional credits to buy the " << shipmodel->name();
- player->send(msgstr.str());
- return;
- }
-
- player->add_credits(-shipmodel->price());
-
- // player has only ship for now
- if (player->control()) {
- player->remove_asset(player->control());
- }
-
- Ship * ship = new Ship(player, shipmodel);
- if (dock) {
- ship->set_zone(dock->zone());
- ship->get_location().assign(dock->location());
- ship->set_state(core::Entity::Docked);
- ship->get_axis().assign(dock->axis());
- ship->get_axis().change_direction(180.0f);
- player->set_control(ship);
- player->set_view(dock);
- } else {
- ship->set_zone(player->zone());
- player->set_control(ship);
- }
-
- // send the ship purchased message
- std::stringstream msgstr;
- msgstr << "^BPurchased " << aux::article(shipmodel->name()) << " for " << shipmodel->price() << " credits";
-
- player->send(msgstr.str());
- player->sound("game/buy-ship");
-}
-
-
-}
diff --git a/src/game/base/shipdealer.h b/src/game/base/shipdealer.h
deleted file mode 100644
index 1d93f03..0000000
--- a/src/game/base/shipdealer.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- base/shipdealer.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_BASE_SHIPDEALER_H__
-#define __INCLUDED_BASE_SHIPDEALER_H__
-
-#include <list>
-
-#include "base/shipmodel.h"
-#include "core/entity.h"
-
-namespace game
-{
-
-class ShipDealer
-{
-public:
- typedef std::list<ShipModel *> Models;
-
- ShipDealer();
- ~ShipDealer();
-
- /// add a ship model to the dealer list
- ShipModel *add(const std::string &modelname);
-
- /// find a ship model in the dealer list
- ShipModel *find(const std::string &modelname) const;
-
- /// find a ship model in the dealer list
- ShipModel *find(ShipModel *shipmodel) const;
-
- static void func_buy(core::Player *player, const std::string &args);
-
-private:
- Models dealer_models;
-};
-
-}
-
-#endif // __INCLUDED_BASE_SHIPDEALER_H__
-
diff --git a/src/game/base/shipmodel.cc b/src/game/base/shipmodel.cc
index e52aab2..6ff6c43 100644
--- a/src/game/base/shipmodel.cc
+++ b/src/game/base/shipmodel.cc
@@ -5,6 +5,7 @@
*/
#include <iomanip>
+#include <assert.h>
#include "auxiliary/functions.h"
#include "base/shipmodel.h"
@@ -190,4 +191,66 @@ void ShipModel::list()
core::Info::list(shipmodel_infotype);
}
+// a player buys a ship
+void ShipModel::buy(core::EntityControlable *buyer, core::Entity *seller)
+{
+ if (!buyer || !seller)
+ return;
+
+ // can only buy at planets and stations
+ if ((seller->moduletype() != station_enttype) && (seller->moduletype() != planet_enttype)) {
+ buyer->owner()->send("^BCan not buy here");
+ return;
+ }
+
+ if (!buyer->owner())
+ return;
+
+ if (!seller->inventory()) {
+ buyer->owner()->send("^BCan not buy here");
+ return;
+ }
+
+ core::Player *player = buyer->owner();
+
+ // seller is the station or planet
+ core::Item *seller_item = seller->inventory()->find(this);
+ if (!seller_item) {
+ if (player) {
+ player->send("^B" + seller->name() + " ^Bdoes not sell " + name());
+ }
+ return;
+ } else {
+ assert(seller_item->info() == this);
+ }
+
+ // check price
+ if (price() > player->credits()) {
+ player->send("^WCan not afford transaction!");
+ return;
+ }
+
+ player->add_credits(-price());
+
+ // player has only ship for now
+ if (player->control()) {
+ player->remove_asset(player->control());
+ }
+
+ Ship * ship = new Ship(player, this);
+ ship->set_zone(seller->zone());
+ ship->get_location().assign(seller->location());
+ ship->set_state(core::Entity::Docked);
+ ship->get_axis().assign(seller->axis());
+ ship->get_axis().change_direction(180.0f);
+ player->set_control(ship);
+ player->set_view(seller);
+
+ // send the ship purchased message
+ std::stringstream msgstr;
+ msgstr << "^BPurchased " << aux::article(name()) << " for " << price() << " credits";
+ player->send(msgstr.str());
+ player->sound("game/buy-ship");
+}
+
}
diff --git a/src/game/base/shipmodel.h b/src/game/base/shipmodel.h
index ef7ff15..a7c8500 100644
--- a/src/game/base/shipmodel.h
+++ b/src/game/base/shipmodel.h
@@ -10,6 +10,7 @@
#include <string>
#include "core/info.h"
+#include "core/entity.h"
namespace game
{
@@ -90,11 +91,13 @@ public:
void generate_info();
+ void buy(core::EntityControlable *buyer, core::Entity *seller);
+
/* --- static registry functions ---------------------------------- */
static ShipModel *find(const std::string & label);
- static ShipModel *search(const std::string & searchstr);
+ static ShipModel *search(const std::string & searchstr);
static bool init();
diff --git a/src/game/base/station.cc b/src/game/base/station.cc
index 83897cd..a093c0e 100644
--- a/src/game/base/station.cc
+++ b/src/game/base/station.cc
@@ -16,26 +16,13 @@ Station::Station() : Entity(), PhysicsBody(this)
entity_moduletypeid = station_enttype;
set_flag(core::Entity::Dockable);
set_flag(core::Entity::ShowOnMap);
- station_shipdealer = 0;
}
Station::~Station()
{
- if (station_shipdealer)
- delete station_shipdealer;
-
shutdown_physics();
}
-void Station::set_shipdealer(ShipDealer *shipdealer)
-{
- if (station_shipdealer)
- delete station_shipdealer;
-
- station_shipdealer = shipdealer;
-}
-
-
void Station::dock(core::Entity *entity)
{
if (entity->moduletype() != ship_enttype)
diff --git a/src/game/base/station.h b/src/game/base/station.h
index ad627dd..ea7fdbd 100644
--- a/src/game/base/station.h
+++ b/src/game/base/station.h
@@ -7,7 +7,6 @@
#ifndef __INCLUDED_BASE_STATION_H__
#define __INCLUDED_BASE_STATION_H__
-#include "base/shipdealer.h"
#include "base/physics.h"
namespace game
@@ -19,19 +18,8 @@ public:
Station();
virtual ~Station();
- inline ShipDealer *shipdealer() {
- return station_shipdealer;
- }
-
- void set_shipdealer(ShipDealer *shipdealer);
-
/// entity received a docking request
virtual void dock(core::Entity *entity);
-
-
-private:
- ShipDealer *station_shipdealer;
-
};
}