Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ba8702431212b5930dc9af2cea95eb47948241ef (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
404
405
/*
   base/shipmodel.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 <iomanip>
#include <assert.h>

#include "auxiliary/functions.h"
#include "base/shipmodel.h"
#include "base/game.h"
#include "base/template.h"
#include "sys/sys.h"

namespace game
{

core::InfoType *ShipModel::shipmodel_infotype = 0;

void func_list_ship(const std::string &args)
{
	ShipModel::list();
}

bool ShipModel::init()
{
	// initialize shipmodel InfoType
	ShipModel::shipmodel_infotype = new core::InfoType("ship");
	
	using math::Vector3f;
	using math::Color;

	filesystem::IniFile inifile;
	inifile.open("ini/ships");
	if (!inifile.is_open()) {
		con_error << "Could not open " << inifile.name() << "!" << std::endl;
		return false;
	}

	con_print << "^BLoading ships..." << std::endl;

	size_t shipmodel_count = 0;
	size_t template_count = 0;
	
	ShipModel *shipmodel = 0;
	Template *entitytemplate = 0;
	
	std::string str;
	long l;
	float f;
	bool b;

	while (inifile.getline()) {
		if (inifile.got_key()) {
			
			if (inifile.section().compare("template") == 0) {
				
				if (Template::got_template_key(inifile, entitytemplate)) {
					continue;
				} else {
					inifile.unknown_key();
				}
				
			} else if (inifile.section().compare("ship") == 0) {
				
				if (inifile.got_key_label("label", str)) {
					if (find(str)) {
						inifile.unknown_error("duplicate ship type '" + str + "'");
					}
					shipmodel->set_label(str);
					continue;
					
				} else if (inifile.got_key_string("name", str)) {
					shipmodel->set_name(str);
					continue;
				} else if (inifile.got_key_string("info", str)) {
					shipmodel->add_text(str);
					continue;
				} else if (inifile.got_key_string("model", str)) {
					shipmodel->set_modelname(str);
					continue;
				} else if (inifile.got_key_long("price", l)) {
					shipmodel->set_price(l);
					continue;
				} else if (inifile.got_key_float("cargo", f)) {
					shipmodel->set_maxcargo(f);
					continue;
				} else if (inifile.got_key_bool("jumpdrive", b)) {
					shipmodel->set_jumpdrive(b);
					continue;
				} else if (inifile.got_key_bool("dock", b)) {
					shipmodel->set_dockable(b);
					continue;
// 				} else if (inifile.got_key_float("maxspeed", f)) {
// 					shipmodel->set_maxspeed(f * 0.01f);
// 					continue;
				} else if (inifile.got_key_float("impulse", f)) {
					shipmodel->set_impulse_force(f);
					continue;
				} else if (inifile.got_key_float("thrust", f)) {
					shipmodel->set_thrust_force(f);
					continue;
				} else if (inifile.got_key_float("strafe", f)) {
					shipmodel->set_strafe_force(f);
					continue;
				} else if (inifile.got_key_float("turn", f)) {
					shipmodel->set_turn_force(f);
					continue;
				} else if (inifile.got_key_float("roll", f)) {
					shipmodel->set_roll_force(f);
					continue;
				} else if (inifile.got_key_float("mass", f)) {
					shipmodel->set_mass(f);
					continue;
				} else if (inifile.got_key_float("armor", f)) {
					shipmodel->set_maxarmor(f);
					continue;
				} else if (inifile.got_key_float("radius", f)) {
					shipmodel->set_radius(f);
					continue;
				} else if (inifile.got_key_string("damping", str)) {
					float linear, angular;
					std::istringstream sstr("str");
					if (sstr >> linear) {
						if (sstr >> angular) {
							shipmodel->set_linear_damping(linear);
							shipmodel->set_angular_damping(angular);
						} else {
							shipmodel->set_linear_damping(linear);
							shipmodel->set_angular_damping(linear);
						}
					} else {
						inifile.unknown_value();
					}
				} else if (inifile.got_key_label("template", str)) {
					Template *entitytemplate = Template::find(str);
					if (!entitytemplate) {
						inifile.unknown_error("unkown template '" + str + "'");
					} else {
						shipmodel->set_template(entitytemplate);
						if (entitytemplate->radius()) {
							shipmodel->set_radius(entitytemplate->radius());
						}
					}
				} else {
					inifile.unknown_key();
				}
			}
				
		} else if (inifile.got_section("template")) {
			
			template_count++;
			entitytemplate = new Template();
			
		} else if (inifile.got_section("ship")) {
			
			// generate info for the last loaded ship model
			if (shipmodel) {
				shipmodel->generate_info();
			}

			// add a new shipmodel
			shipmodel = new ShipModel();

			// the first ship model is set as default, game.ini can override this later
			if (!Default::shipmodel) {
				Default::shipmodel = shipmodel;
			}
			
			shipmodel_count++;

		} else if (inifile.got_section()) {
			inifile.unknown_section();
		}
	}

	// generate info for the last loaded ship model
	if (shipmodel) {
		shipmodel->generate_info();
	}

	con_debug << "  " << inifile.name() << " " << template_count << " entity templates" << std::endl;
	con_debug << "  " << inifile.name() << " " << shipmodel_count << " ship types" << std::endl;
	

	inifile.close();
	
	core::Func *func = core::Func::add("list_ship", func_list_ship);
	func->set_info("list available ship types");
	
	return true;
}

void ShipModel::done()
{
	core::Func::remove("list_ship");
}

ShipModel::ShipModel() : core::Info(shipmodel_infotype)
{
	//shipmodel_maxspeed = 0;
	
	//default specifications
	shipmodel_radius = 0.0f;
	shipmodel_mass = 0.0f;
	shipmodel_linear_damping = 0.8f;
	shipmodel_angular_damping = 0.8f;
	
	shipmodel_thrust_force = 0.8f;
	shipmodel_impulse_force = 4.0f;
	shipmodel_strafe_force = 0.1f;
	shipmodel_turn_force = 2.5f;
	shipmodel_roll_force = 1.0f;
	
	shipmodel_maxcargo = 0.0f;
	shipmodel_maxarmor = 100.0f;

	shipmodel_jumpdrive = false;	// no jumpdrive capability
	shipmodel_dockable = false;		// not dockable
	shipmodel_template = 0;
	
}

ShipModel::~ShipModel()
{
}

void ShipModel::generate_info()
{
	// default mass
	set_mass(radius() * 100.0f);

	if (text().size())
		add_line("");
	
	add_line("^BSpecifications:^N");
	std::stringstream str;
	str << "price:        ^B" << price() << " ^Ncredits";
	add_line(str.str());
	str.str("");

	str << "cargo hold:   ^B" << maxcargo() << " ^Ncubic meter";
	add_line(str.str());
	str.str("");

	str << "mass:         ^B" << mass() << " ^Nmetric tonnes";
	add_line(str.str());
	str.str("");
	
	str << "hull:         ^B" << maxarmor()  << "^N";
	add_line(str.str());
	str.str("");
	
	if (jumpdrive()) {
		add_line("^Bhyperspace jump drive");
	}
	
	if (dockable()) {
		add_line("^Bdockable");
	}
}

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

ShipModel *ShipModel::search(const std::string & searchstr)
{
	if (!searchstr.size()) {
		return 0;
	}
	
	return (ShipModel *) core::Info::search(shipmodel_infotype, searchstr);
}


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 if there's enough space available to transfer inventory
	if (player->control() && (maxcargo() < player->control()->inventory()->capacity_used())) {
		player->send("^WNot enough cargo space to transfer inventory!");
		return;
	}
	
	// check price
	if (price() > player->credits()) {
		player->send("^WCan not afford transaction!");
		return;
	}

	Ship * ship = new Ship(player, this);
	ship->set_zone(seller->zone());
	ship->get_location().assign(seller->location());
	ship->get_axis().assign(seller->axis());
	ship->get_axis().change_direction(180.0f);
	ship->set_dock(seller);
	//ship->reset(); // reset() is done by set_dock()
	
	// transfer inventory
	for (core::Inventory::Items::iterator it = player->control()->inventory()->items().begin(); it != player->control()->inventory()->items().end(); it++) {
		core::Item *item = new core::Item(*(*it));
		item->unset_flag(core::Item::Mounted);
		ship->inventory()->add(item);
	}	
	ship->inventory()->set_dirty();

	// remove old ship
	if (player->control()) {
		player->remove_asset(player->control());
	}

	// set control to new ship
	player->set_control(ship);
	player->set_view(seller);

	player->add_credits(-price());
		
	// 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");
	
}

void ShipModel::apply(core::Entity *entity) const
{
	// apply template settings if available
	if (model_template())
		model_template()->apply(entity);
	
	// apply entity settings
	if (label().size())
		entity->set_label(label());
	
	if (name().size())
		entity->set_name(name());
	
	if (modelname().size())
		entity->set_modelname(modelname());
	
	if (radius())
		entity->set_radius(radius());
}

void ShipModel::apply(Ship *ship) const
{
	// apply ship model settings
	apply (static_cast<core::Entity *>(ship));
	
	ship->set_info(this);
	
	ship->set_mass(mass());
	ship->set_impulse_force(impulse_force());
	ship->set_thrust_force(thrust_force());
	ship->set_strafe_force(strafe_force());
	ship->set_turn_force(turn_force());
	ship->set_roll_force(roll_force());
	ship->set_jumpdrive(jumpdrive());
	ship->set_maxarmor(maxarmor());
}

}