Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 626a1b352f559ccce55f65df3d277f1fdd1e92d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
   base/cargo.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 <assert.h>
#include <cmath>

#include "base/game.h"
#include "base/cargo.h"
#include "filesystem/inifile.h"
#include "auxiliary/functions.h"
#include "sys/sys.h"

namespace game
{

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;
	float f;
	
	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 if (cargoini.got_key_float("volume", f)) {
					cargo->set_volume(f);
					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)
{
	set_volume(1);
}

Cargo::~Cargo()
{
}

Cargo *Cargo::find(const std::string & label)
{
	if (!label.size()) {
		return 0;
	}
	
	return (Cargo *) core::Info::find(cargo_infotype, label);
}

// main 'sell cargo' function
void Cargo::sell(core::EntityControlable *seller, core::Entity *buyer, const int amount)
{
	if (!buyer || !seller)
		return;
	
	// can only sell at planets and stations
	if ((buyer->moduletype() != station_enttype) && (buyer->moduletype() != planet_enttype)) {
		seller->owner()->send("^BCan not sell here");
		return;
	}
	
	if (!seller->owner()) 
		return;
	
	if (!buyer->inventory() || !seller->inventory()) {
		seller->owner()->send("^BCan not sell here");
		return;
	}
	
	if (!amount) {
		return;
	}
	
	// seller is the player
	core::Item *seller_item = seller->inventory()->find(this);
	if (!seller_item) {
		if (seller->owner()) {
			seller->owner()->send("^BYou do not own any " + name());
		}
		return;	
	}
	
	// buyer is the station or planer
	core::Item *buyer_item = buyer->inventory()->find(this);
	if (!buyer_item) {
		seller->owner()->send("^B" + buyer->name() + " ^Bdoes not buy " + name());
		return;
	}

	int negotiated_amount = amount;
	if (negotiated_amount < 0) {
		negotiated_amount = seller_item->amount();
	} else if (negotiated_amount > seller_item->amount()) {
		negotiated_amount = seller_item->amount();
	}
		
	int negotiated_price = buyer_item->price();
	
	seller_item->dec_amount(negotiated_amount);
	seller->owner()->set_credits(seller->owner()->credits() + negotiated_price * negotiated_amount);
	seller->owner()->set_dirty();
	seller->inventory()->set_dirty();
	
	if (buyer_item->amount() >= 0) {
		buyer_item->inc_amount(negotiated_amount);
		buyer->inventory()->set_dirty();
	}
	
	// send a cargo purchased message
	std::stringstream msgstr;
	msgstr << "^BSold " << negotiated_amount << " " << aux::plural("unit", negotiated_amount) << " of " << name() << " for " << negotiated_price * negotiated_amount << " credits";
	seller->owner()->send(msgstr.str());
	seller->owner()->sound("game/buy");
	
}

// main 'buy cargo' function
void Cargo::buy(core::EntityControlable *buyer, core::Entity *seller, const int amount)
{
	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 (!buyer->inventory() || !seller->inventory()) {
		buyer->owner()->send("^BCan not buy here");
		return;
	}
	
	if (!amount) {
		return;
	}

	// seller is the station or planet
	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;	
	} else {
		assert(seller_item->info() == this);
	}
	
	int negotiated_amount = amount;
	int negotiated_price = seller_item->price();
	long cash = buyer->owner()->credits();
	
	// check if the player has enough cash
	if (negotiated_price > 0) {
		
		// negative amount means 'as much as possible'
		if (negotiated_amount < 0) {
			negotiated_amount = cash / negotiated_price;
		}
		
		// maximum amount the player can afford
		if (cash < negotiated_amount * negotiated_price) {
			negotiated_amount = cash / negotiated_price;
		}
		
		if (negotiated_amount < 1) {
			buyer->owner()->send("^WCan not afford transaction!");
			return;
		}
	}
	
	// check cargo size - ignore zero volume cargo
	if (volume() > 0 ) {
		
		// maximum cargo size
		if (negotiated_amount * volume() > buyer->inventory()->capacity_available()) {
			negotiated_amount = (int)floorf(buyer->inventory()->capacity_available() / volume());
		}
		
		if (negotiated_amount < 1) {
			buyer->owner()->send("^WNot enough cargo space available!");
			return;
		}
	}
	
	if (negotiated_amount <= 0) {
		// unlimited amount of zero-cost cargo
		buyer->owner()->send("^WNo unlimited amounts of zero-cost cargo available!");
		return;
	}
	
	// if amount is set to -1. the base has a limitless supply
	
	if (seller_item->amount() == 0) {
		buyer->owner()->send("^WCargo not available!");
		return;
		
	} else if (seller_item->amount() > 0) {
		
		if (negotiated_amount  > seller_item->amount()) {
			negotiated_amount = seller_item->amount();
		}
		
		seller_item->dec_amount(negotiated_amount);
		seller->inventory()->set_dirty();
	}
	
	// buyer is the player
	core::Item *buyer_item = buyer->inventory()->find(this);
	if (!buyer_item) {
		buyer_item = new core::Item(this);
		buyer->inventory()->add(buyer_item);
	} else {
		assert(buyer_item->info() == this);	
	}	
	buyer_item->inc_amount(negotiated_amount);
	buyer->owner()->set_credits(buyer->owner()->credits() - negotiated_price * negotiated_amount);
	buyer->owner()->set_dirty();
	buyer->inventory()->set_dirty();
	
	// 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