Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2010-09-18 18:50:55 +0000
committerStijn Buys <ingar@osirion.org>2010-09-18 18:50:55 +0000
commit9c91a9767b570fdc3c3e19e1f452f9a8257f9999 (patch)
tree9ac10114a3378134ea19dac3d7f7639532c3bf5a /src/game/base/cargo.cc
parentfc4809e41bc5694231046eb2fd4c324c4daba13f (diff)
trade updates
Diffstat (limited to 'src/game/base/cargo.cc')
-rw-r--r--src/game/base/cargo.cc133
1 files changed, 131 insertions, 2 deletions
diff --git a/src/game/base/cargo.cc b/src/game/base/cargo.cc
index 258aecf..420d4d3 100644
--- a/src/game/base/cargo.cc
+++ b/src/game/base/cargo.cc
@@ -6,16 +6,86 @@
#include "base/game.h"
#include "base/cargo.h"
+#include "filesystem/inifile.h"
#include "auxiliary/functions.h"
#include "sys/sys.h"
namespace game
{
-/* ---- class Cargo -------------------------------------------- */
-
core::InfoType *Cargo::cargo_infotype = 0;
+// loads cargo types from ini file
+bool Cargo::init()
+{
+
+ // initialize commodities InfoType
+ Cargo::cargo_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 cargo..." << std::endl;
+
+ size_t count = 0;
+
+ Cargo *cargo = 0;
+ std::string str;
+ long l;
+
+ while (cargoini.getline()) {
+ if (cargoini.got_key()) {
+
+ if (cargoini.section().compare("cargo") == 0) {
+ if (cargoini.got_key_label("label", str)) {
+ cargo->set_label(std::string(str));
+ count++;
+ continue;
+
+ } else if (cargoini.got_key_string("name", str)) {
+ cargo->set_name(str);
+ continue;
+
+ } else if (cargoini.got_key_string("info", str)) {
+ cargo->add_text(str);
+ continue;
+
+ } else if (cargoini.got_key_string("model", str)) {
+ cargo->set_modelname(str);
+ continue;
+
+ } else if (cargoini.got_key_long("price", l)) {
+ cargo->set_price(l);
+ continue;
+ } else {
+ cargoini.unkown_key();
+ }
+ }
+
+ } else if (cargoini.got_section()) {
+
+ if (cargoini.got_section("cargo")) {
+ cargo = new Cargo();
+
+ } else if (cargoini.got_section()) {
+ cargoini.unknown_section();
+ }
+ }
+ }
+
+ // add cargo infos
+ con_debug << " " << cargoini.name() << " " << count << " cargo types" << std::endl;
+
+ cargoini.close();
+ return true;
+}
+
+/* ---- class Cargo -------------------------------------------- */
+
Cargo::Cargo() : core::Info(cargo_infotype)
{
}
@@ -33,6 +103,65 @@ Cargo *Cargo::find(const std::string & label)
return (Cargo *) core::Info::find(cargo_infotype, label);
}
+// main 'buy cargo' function
+void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int amount)
+{
+ if (!buyer || !seller)
+ return;
+
+ if (!buyer->owner())
+ return;
+
+ if (!buyer->inventory() || !seller->inventory()) {
+ return;
+ }
+
+ if (!amount) {
+ return;
+ }
+
+ core::Item *buyer_item = buyer->inventory()->find(this);
+ core::Item *seller_item = seller->inventory()->find(this);
+
+ if (!seller_item) {
+ if (buyer->owner()) {
+ buyer->owner()->send("^B" + seller->name() + " ^Bdoes not sell " + name());
+ }
+ return;
+ }
+
+ int negotiated_amount = amount;
+ int negotiated_price = seller_item->price();
+ long cash = buyer->owner()->credits();
+
+ // negative means 'as much as possible'
+ if (negotiated_amount < 0) {
+ negotiated_amount = cash / negotiated_price;
+ }
+
+ if (cash < negotiated_amount * negotiated_price) {
+ negotiated_amount = cash / negotiated_price;
+ }
+ if (!negotiated_amount) {
+ buyer->owner()->send("^WCan not afford transaction!");
+ return;
+ }
+
+ // TODO cargo size check
+
+ if (!buyer_item) {
+ buyer_item = new core::Item(this);
+ buyer->inventory()->add(buyer_item);
+ }
+ buyer_item->inc_amount(negotiated_amount);
+ buyer->owner()->set_credits(buyer->owner()->credits() - negotiated_price * negotiated_amount);
+
+ // send a cargo purchased message
+ std::stringstream msgstr;
+ msgstr << "^BPurchased " << negotiated_amount << " " << aux::plural("unit", negotiated_amount) << " of " << name() << " for " << negotiated_price * negotiated_amount << " credits";
+ buyer->owner()->send(msgstr.str());
+ buyer->owner()->sound("game/buy");
+}
} // namespace game