Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 2f5490f737c8144b6246114800d333fae1e7c454 (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
   game/game.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include <vector>

#include "auxiliary/functions.h"
#include "core/gameserver.h"
#include "filesystem/filesystem.h"
#include "filesystem/inifile.h"
#include "game/game.h"
#include "game/planet.h"
#include "game/ship.h"
#include "game/star.h"
#include "math/mathlib.h"
#include "sys/sys.h"

namespace game
{



ShipModel		*default_shipmodel = 0;
core::Zone		*default_zone = 0;

/*----- engine game functions ------------------------------------- */

/// list the ship model registry
void func_list_ship(std::string const &args)
{
	ShipModel::list();
}

/// a player joins the game
void func_join(core::Player *player, std::string const &args)
{
	if (player->control())
		return;

	player->set_zone(default_zone);
	Ship *ship = new Ship(player, default_shipmodel);
	ship->set_zone(default_zone);
	player->set_control(ship);

	core::server()->send_sound(player, "game/buy-ship");
	
	std::string message("^B");
	message.append(player->name());
	message.append("^B joins the game.");
	core::server()->broadcast(message);

	player->player_dirty = true;
}

/// a player joins the spectators
void func_spectate(core::Player *player, std::string const &args)
{
	if (!player->control())
		return;

	std::string message("^B");
	message.append(player->name());
	message.append("^B spectates.");
	core::server()->broadcast(message);

	if (player->control()) {
		player->remove_asset(player->control());
	}
}

/// a player buys a ship
void func_buy(core::Player *player, std::string const &args)
{
	
	std::string shipname;
	std::string helpstr;
	std::istringstream is(args);
	is >> shipname;
	aux::to_lowercase(shipname);

	ShipModel *shipmodel = 0;
	for (ShipModel::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) {
		if (shipname == (*smit).first) {
			shipmodel = (*smit).second;
			break;
		}

		if (helpstr.size())
			helpstr.append("^N|^B");
		helpstr.append((*smit).second->label());
	}

	if (shipmodel) {
		// player has only ship for now
		if (player->control()) {
			player->remove_asset(player->control());
		}

		Ship * ship = new Ship(player, shipmodel);
		if (player->zone()) {
			ship->set_zone(player->zone());
		} else {
			ship->set_zone(default_zone);
		}
		player->set_control(ship);
		
		core::server()->broadcast("^B" + player->name() + " ^Bpurchased " + aux::article(shipmodel->name()));
		core::server()->send_sound(player, "game/buy-ship");

	} else {
		core::server()->send(player, "Usage: buy [^B" + helpstr + "^N]");
	}
}

/// a player sends standard hails
void func_hail(core::Player *player, std::string const &args)
{
	std::string  target;
	std::istringstream is(args);
	if (!(is >> target)) {
		core::server()->send(player, "Usage: hail [player]");
		return;
	}

	core::Player *targetplayer = core::server()->find_player(target);
	if (!targetplayer) {
		core::server()->send(player, "^BPlayer " + target + "^B not found.");
		return;
	}

	core::server()->send(player, "^BYou hail " + targetplayer->name() + "^B.");
	core::server()->send_sound(player, "com/hail");

	core::server()->send(targetplayer, "^B" + player->name() + "^B hails you!");
	core::server()->send_sound(targetplayer, "com/hail");
}

/// a player actives the jumpdrive on his ship
void func_jump(core::Player *player, std::string const &args)
{
	if (!player->control())
		return;

	if (!player->control()->moduletype() == ship_enttype)
		return;

	Ship * ship = static_cast<Ship *>(player->control());

	if (!ship->jumpdrive()) {
		core::server()->send(player, "Your ship is not equiped with a jumpdrive");
		return;
	}
	std::string target;
	std::istringstream is(args);
	if (!(is >> target)) {
		std::string helpstr;
		for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) {
			core::Zone *zone = (*it).second;
			if (helpstr.size())
				helpstr.append("^N|^B");
			helpstr.append(zone->label());
		}

		core::server()->send(player, "Usage: jump [^B" + helpstr + "^N]");
		return;
	}

	aux::to_lowercase(target);
	core::Zone *zone = core::Zone::find_zone(target);
	if (!zone) {
		core::server()->send(player, "Unknown system '" + target + '\'');
		return;
	}

	if (zone == player->control()->zone())
		return;

	core::server()->send(player, "Jumping to '" + zone->name() + '\'');

	player->control()->set_zone(zone);
	player->set_zone(zone);
}

/* ---- The Game class --------------------------------------------- */

Game 			*Game::game_instance = 0;

Game::Game() : core::Module("Project::OSiRiON")
{
	game_instance = this;
	g_impulsespeed = 0;
}

Game::~Game()
{
	game_instance = 0;
}

void Game::init()
{
	module_running = false;

	ShipModel::clear();

	if (!load_world()) {
		abort();
		return;
	}
		
	if (!load_ships()) {
		abort();
		return;
	}

	// add engine game functions
	core::Func *func = 0;
	func = core::Func::add("buy", (core::GameFuncPtr) func_buy);
	func->set_info("buy a ship");
	func = core::Func::add("jump", (core::GameFuncPtr) func_jump);
	func->set_info("make a hyperspace jump");
	func = core::Func::add("join", (core::GameFuncPtr) func_join);
	func->set_info("join the game");
	func = core::Func::add("hail", (core::GameFuncPtr) func_hail);
	func->set_info("send a standard hail");
	func = core::Func::add("spectate", (core::GameFuncPtr) func_spectate);
	func->set_info("leave the game and spectate");

	// add engine core functions
	func = core::Func::add("list_ship", (core::FuncPtr) func_list_ship);
	func->set_info("list ship statistics");

	g_impulsespeed = core::Cvar::get("g_impulsespeed", "15", core::Cvar::Game | core::Cvar::Archive);
	g_impulsespeed->set_info("[float] speed of the impulse drive");
	
	// indicate the module is ready to run frames
	module_running = true;
}

void Game::shutdown()
{
	g_impulsespeed = 0;
	// game functions are automaticly removed

	// remove engine core functions
	core::Func::remove("list_ship");

	ShipModel::clear();
	module_running = false;
}

bool Game::load_world()
{
	std::string inifilename("world");

	filesystem::IniFile worldini;
	worldini.open(inifilename);

	if (!worldini.is_open()) {
		con_error << "Could not open " << worldini.name() << std::endl;
		return false;
	}

	core::Zone *zone = 0;
	std::string label;

	while (worldini.getline()) {

		if (worldini.got_section()) {

			if (worldini.got_section("world")) {
				continue;
			} else {
				con_warn << worldini.name() << "  unknown section '" << worldini.section() << "' at line " << worldini.line() << std::endl;
			}

		} else if (worldini.section().compare("world") == 0 ) {
			if (worldini.got_key_string("zone", label)) {
				aux::to_label(label);
				zone = new core::Zone(label);
				core::Zone::add(zone);
			}
		}
	}
	worldini.close();

	if (!core::Zone::registry().size()) {
		con_error << "No zones found!" << std::endl;
		return false;
	}

	con_debug << "  " << worldini.name() << " " << core::Zone::registry().size() << " zones" << std::endl;

	for (core::Zone::Registry::iterator it = core::Zone::registry().begin(); it != core::Zone::registry().end(); it++) {
		if (!load_zone((*it).second)) {
			return false;
		}
	}

	if (!default_zone) {
		con_error << "No default zone found!" << std::endl;
		return false;
	}

	return true;
}

bool Game::load_zone(core::Zone *zone)
{
	using math::Vector3f;
	using math::Color;

	std::string inifilename("zones/");
	inifilename.append(zone->label());

	filesystem::IniFile zoneini;
	zoneini.open(inifilename);

	if (!zoneini.is_open()) {
		con_error << "Could not open " << zoneini.name() << std::endl;
		return false;
	}

	size_t count = 0;

	Planet *planet = 0;
	Star *star = 0;
	core::Entity *entity = 0;

	float direction;
	float pitch;
	float roll;

	bool b;

	std::string strval;

	// set th default sky
	zone->set_sky("sky");

	while (zoneini.getline()) {
		if (zoneini.got_key()) {
			if (zoneini.section().compare("zone") == 0) {
				if (zoneini.got_key_string("name", strval)) {
					zone->set_name(strval);
					continue;
				} else if (zoneini.got_key_string("sky", strval)) {
					zone->set_sky(strval);
					continue;
				} else if (zoneini.got_key_bool("default", b)) {
					if (b) default_zone = zone;
					continue;
				} else {
					con_warn << zoneini.name() << " unknown key '" << zoneini.key() << "' at line " << zoneini.line() << std::endl;
				}
			} else if (zoneini.section().compare("star") == 0) {
				if (zoneini.got_key_string("label", strval)) {
					aux::to_label(strval);
					star->entity_label.assign(strval);
					continue;
				 } else if (zoneini.got_key_string("name", star->entity_name))
					continue;
				else if (zoneini.got_key_vector3f("location", star->entity_location ))
					continue;
				else if (zoneini.got_key_color("color", star->entity_color))
					continue;
				else if (zoneini.got_key_angle("radius", star->entity_radius))
					continue;
				else
					con_warn << zoneini.name() << " unknown key '" << zoneini.key() << "' at line " << zoneini.line() << std::endl;

			} else if (zoneini.section().compare("planet") == 0) {
				if (zoneini.got_key_string("label", strval)) {
					aux::to_label(strval);
					planet->entity_label.assign(strval);
					continue;
				} else if (zoneini.got_key_string("name", planet->entity_name))
					continue;
				else if (zoneini.got_key_string("texture", planet->entity_texture))
					 continue;
				else if (zoneini.got_key_vector3f("location", planet->entity_location ))
					continue;
				else if (zoneini.got_key_color("color", planet->entity_color))
					continue;
				else if (zoneini.got_key_angle("radius", planet->entity_radius))
					continue;
				else
					con_warn << zoneini.name() << " unknown key '" << zoneini.key() << "' at line " << zoneini.line() << std::endl;

			} else if (zoneini.section().compare("entity") == 0) {
				std::string shapename;
				if (zoneini.got_key_string("shape", shapename)) {
					if (shapename.compare("axis") == 0) {
						entity->entity_shape = core::Entity::Axis;
					} else if (shapename.compare("cube") == 0) {
						entity->entity_shape = core::Entity::Cube;
					} else if (shapename.compare("diamond") == 0) {
						entity->entity_shape = core::Entity::Diamond;
					} else if (shapename.compare("sphere") == 0) {
						entity->entity_shape = core::Entity::Sphere;
					} else {
						con_warn << zoneini.name() << " unknown shape '" << shapename << "' at line " << zoneini.line() << std::endl;
					}
					continue;
				} else if (zoneini.got_key_string("label", strval)) {
					aux::to_label(strval);
					entity->entity_label.assign(strval);		
					continue;
				} else if (zoneini.got_key_string("name", entity->entity_name)) {
					continue;
				} else if (zoneini.got_key_string("model", entity->entity_modelname)) {
					continue;
				} else if (zoneini.got_key_angle("direction", direction)) {
					entity->axis().change_direction(direction);
					continue;
				} else if (zoneini.got_key_angle("pitch", pitch)) {
					entity->axis().change_pitch(pitch);
					continue;
				} else if (zoneini.got_key_angle("roll", roll)) {
					entity->axis().change_roll(roll);
					continue;
				} else if (zoneini.got_key_angle("radius", entity->entity_radius)) {
					continue;
				} else if (zoneini.got_key_vector3f("location", entity->entity_location)) {
					continue;
				} else if (zoneini.got_key_color("color", entity->entity_color)) {
					continue;
				} else if (zoneini.got_key_color("colorsecond", entity->entity_color_second)) {
					continue;
				} else {
					con_warn << zoneini.name() << " unknown key '" << zoneini.key() << "' at line " << zoneini.line() << std::endl;
				}
			}
		} else if (zoneini.got_section("zone")) {
			continue;

		} else if (zoneini.got_section("star")) {
			star = new Star();
			star->set_zone(zone);
			count ++;

		} else if (zoneini.got_section("planet")) {
			planet = new Planet();
			planet->set_zone(zone);
			count ++;

		} else if (zoneini.got_section("entity")) {
			entity = new core::Entity();
			entity->entity_flags += core::Entity::Static;
			entity->set_zone(zone);
			count ++;

		} else if (zoneini.got_section()) {
			con_warn << zoneini.name() << " unknown section '" << zoneini.section() << "' at line " << zoneini.line() << std::endl;
		}
	}
	zoneini.close();

	con_debug << "  " << zoneini.name() << " " << zone->content().size() << " entities" << std::endl;

	return true;
}

// read ship model specifications
bool Game::load_ships()
{
	using math::Vector3f;
	using math::Color;

	default_shipmodel = 0;

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

	ShipModel *shipmodel = 0;
	std::string label;
	bool b;

	while (shipsini.getline()) {
		if (shipsini.got_key()) {
			if (shipsini.section().compare("ship") == 0) {
				if (shipsini.got_key_string("label", label)) {
					aux::to_label(label);
					shipmodel->shipmodel_label.assign(label);
					ShipModel::add(shipmodel);
					continue;
				} else if (shipsini.got_key_string("name",shipmodel->shipmodel_name)) {
					 continue;
				} else if (shipsini.got_key_string("model", shipmodel->shipmodel_modelname)) {
					continue;
				} else if (shipsini.got_key_bool("default", b)) {
					if (b) default_shipmodel = shipmodel;
					continue;
				} else if (shipsini.got_key_bool("jumpdrive", shipmodel->shipmodel_jumpdrive)) {
					continue;
				} else if (shipsini.got_key_float("acceleration", shipmodel->shipmodel_acceleration)) {
					continue;
				} else if (shipsini.got_key_float("maxspeed", shipmodel->shipmodel_maxspeed)) {
					continue;
				} else if (shipsini.got_key_float("turnspeed", shipmodel->shipmodel_turnspeed)) {
					math::clamp(shipmodel->shipmodel_turnspeed, 0.0f, 90.0f);
					continue;
				} else {
					con_warn << shipsini.name() << " unknown key '" << shipsini.key() << "' at line " << shipsini.line() << std::endl;
				}
			}
		} else if (shipsini.got_section("ship")) {
			shipmodel = new ShipModel();

			if (!default_shipmodel)
				default_shipmodel = shipmodel; 

		} else if (shipsini.got_section()) {
			con_warn << shipsini.name() << " unknown section '" << shipsini.section() << "' at line " << shipsini.line() << std::endl;
		}
	}
	shipsini.close();

	con_debug << "  " << shipsini.name() << " " << ShipModel::registry.size() << " ship models" << std::endl;

	if (!default_shipmodel) {
		con_error << "No default ship model found!\n";
		return false;
	}

	return true;
}

void Game::frame(float seconds)
{
	if (!running())
		return;
}

void Game::player_connect(core::Player *player)
{
	std::string args;
	player->set_zone(default_zone);
	func_spectate(player, args);
}

void Game::player_disconnect(core::Player *player)
{
	player->remove_asset(player->control());
}

} // namespace game