Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: fd1ee3f2921600da4e7fbc1499f270bbb20de697 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
   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 "sys/sys.h"
#include "filesystem/inifile.h"
#include "auxiliary/functions.h"
#include "core/func.h"
#include "base/game.h"
#include "base/cargo.h"
#include "base/cargopod.h"

namespace game
{

core::InfoType *Cargo::cargo_infotype = 0;

void func_list_cargo(const std::string &args)
{
	Cargo::list();
}

// loads cargo types from ini file
bool Cargo::init()
{

	// initialize cargo InfoType
	cargo_infotype = new core::InfoType("cargo");
	
	filesystem::IniFile cargoini;
	cargoini.open("ini/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(str);
					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.unknown_key();
				}
			}
			
		} else if (cargoini.got_section()) {
		
			if (cargoini.got_section("cargo")) {
				cargo = new Cargo();
				count++;

			} else if (cargoini.got_section()) {
				cargoini.unknown_section();
			}		
		}
	}
	
	// add cargo infos
	con_debug << "  " << cargoini.name() << " " << count << " cargo types" << std::endl;
	
	cargoini.close();
	
	core::Func *func = core::Func::add("list_cargo", func_list_cargo);
	func->set_info("list available cargo types");
	
	return true;
}

void Cargo::done()
{
	core::Func::remove("list_cargo");
}

/* ---- 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("^WCan not sell here");
		return;
	}
	
	if (!seller->owner()) 
		return;
	
	if (!buyer->inventory() || !seller->inventory()) {
		seller->owner()->send("^WCan 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("^WYou 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("^W" + 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("^WCan not buy here");
		return;
	}
	
	if (!buyer->owner()) 
		return;
	
	if (!buyer->inventory() || !seller->inventory()) {
		buyer->owner()->send("^WCan 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("^W" + 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");
}

// main 'eject cargo' function
void Cargo::eject(core::EntityControlable *ejector, const int amount)
{
	if (!ejector->inventory()) {
		return;
	}
	
	if (!amount) {
		return;
	}
	
	if ((ejector->state() == core::Entity::Jump) || (ejector->state() == core::Entity::JumpInitiate)) {
		if (ejector->owner()) {
			ejector->owner()->send("^WCan not eject while hyperspace jump drive is active");
		}
		return;
	}
	
	// find the cargo in the inventory
	core::Item *item = ejector->inventory()->find(this);	
	if (!item || !item->amount()) {
		if (ejector->owner()) {
			ejector->owner()->send("^WYou do not own any " + name());
		}
		return;	
	} else {
		assert(item->info() == this);
	}	
	
	int negotiated_amount = amount;
	if (negotiated_amount < 0) {
		negotiated_amount = item->amount();
	} else if (negotiated_amount > item->amount()) {
		if (ejector->owner()) {
			std::stringstream msgstr;
			msgstr << "^WYou only own " << item->amount() << " " << aux::plural("unit", negotiated_amount) << " of " << name();
			ejector->owner()->send(msgstr.str());
		}
		return;	
	}
	
	item->dec_amount(negotiated_amount);
	ejector->inventory()->set_dirty();
	
	if (ejector->state() == core::Entity::Docked) {
		std::stringstream msgstr;
		if (ejector->owner()) {
			msgstr << "^BDestroyed " << negotiated_amount << " " << aux::plural("unit", negotiated_amount) << " of " << name();
			ejector->owner()->send(msgstr.str());
			ejector->owner()->sound("game/eject");
		}
		return;
	}
	
	// create cargo pod	
	CargoPod *pod = new CargoPod();
	
	pod->set_color(ejector->color());
	pod->set_color_second(ejector->color_second());
	pod->set_location(ejector->location() + ejector->axis().up() * (ejector->radius() + pod->radius()));
	pod->set_axis(ejector->axis());
	pod->set_zone(ejector->zone());
	pod->set_info(item->info());
	
	// add loot to inventory
	pod->set_inventory(new core::Inventory());
	pod->inventory()->set_capacity(item->info()->volume() * negotiated_amount);
	
	core::Item *loot = new core::Item(item->info());
	loot->set_amount(negotiated_amount);
	
	pod->inventory()->add(loot);
	pod->inventory()->set_dirty();
	
	if (ejector->owner()) {
		std::stringstream msgstr;
		msgstr << "^BEjected " << negotiated_amount << " " << aux::plural("unit", negotiated_amount) << " of " << name();
		ejector->owner()->send(msgstr.str());
		ejector->owner()->sound("game/eject");
	}
	
	pod->reset();
}

void Cargo::list()
{
	core::Info::list(cargo_infotype);
}

} // namespace game