Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: aa78e41d3161952cb97d2f6d99afca232d1ba85c (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
/*
   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/ship.h"
#include "game/star.h"
#include "math/mathlib.h"
#include "sys/sys.h"

namespace game
{

ShipModel		*default_shipmodel;
/*----- 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->player_control = new Ship(player, default_shipmodel);
	player->control()->entity_color = player->color();
	
	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->player_control)
		return;

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

	if (player->control()) {
		// player has only ship for now
		player->player_control->die();
		player->player_control = 0;
	}

	player->player_dirty = true;
}

/// 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;

	ShipModel *shipmodel = 0;
	for (std::list<ShipModel *>::iterator smit = ShipModel::registry.begin(); smit != ShipModel::registry.end(); smit++) {
		if (shipname == (*smit)->modelname()) {
			shipmodel = (*smit);
			break;
		}

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

	if (shipmodel) {
		// player has only ship for now
		if (player->control()) {
			player->player_control->die();
			player->player_control = 0;
		}

		player->player_control = new Ship(player, shipmodel);
		player->control()->entity_color = player->color();

		core::server()->broadcast("^B" + player->name() + " ^Bpurchased " + aux::article(shipmodel->name()));
		player->player_dirty = true;
	} else {
		core::server()->send(player, "Usage: buy <" + helpstr + "^N>");
	}
}
/*----- Game ------------------------------------------------------ */

Game::Game() : core::Module("Project::OSiRiON")
{
}

Game::~Game()
{
}

void Game::init()
{
	using math::Vector3f;
	using math::Color;

	module_running = false;

	ShipModel::clear();

	// setup the game world
	filesystem::IniFile worldini;
	worldini.open("world");

	if (!worldini.is_open()) {
		con_error << "Could not open ini/world.ini!" << std::endl;
		abort();
		return;
	}

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

	float direction;
	float pitch;
	float roll;

	while (worldini.getline()) {
		if (worldini.got_key()) {
			if (worldini.section().compare("star") == 0) {
				if (worldini.got_key_string("name", star->entity_name))
					continue;
				else if (worldini.got_key_string("model", star->entity_modelname))
					 continue;
				else if (worldini.got_key_vector3f("location", star->entity_location ))
					continue;
				else if (worldini.got_key_color("color", star->entity_color))
					continue;
				else
					con_warn << worldini.name() << " unknown key '" << worldini.key() << "' at line " << worldini.line() << std::endl;

			} else if (worldini.section().compare("entity") == 0) {

				std::string shapename;

				if (worldini.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 << worldini.name() << " unknown shape '" << shapename << "' at line " << worldini.line() << std::endl;
					}
					continue;
				} else	if (worldini.got_key_string("name", entity->entity_name))
					continue;
				else if (worldini.got_key_string("model", entity->entity_modelname))
					continue;
				else if (worldini.got_key_angle("direction", direction)) {
					entity->axis().change_direction(direction);
					continue;
				} else if (worldini.got_key_angle("pitch", pitch)) {
					entity->axis().change_pitch(pitch);
					continue;
				} else if (worldini.got_key_angle("roll", roll)) {
					entity->axis().change_roll(roll);
					continue;
				} else if (worldini.got_key_angle("radius", entity->entity_radius))
					continue;
				else if (worldini.got_key_vector3f("location", entity->entity_location))
					continue;
				else if (worldini.got_key_color("color", entity->entity_color))
					continue;
				else
					con_warn << worldini.name() << " unknown key '" << worldini.key() << "' at line " << worldini.line() << std::endl;
			}
		} else if (worldini.got_section("star")) {
			star = new Star();

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

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

	// read ship model specifications
	// note: 
	// do not reuse the previous IniFile instance, some gcc versions do not like it
	filesystem::IniFile shipsini;
	shipsini.open("ships");
	if (!shipsini.is_open()) {
		con_error << "Could not open ini/ships.ini!" << std::endl;
		abort();
		return;
	}

	ShipModel *shipmodel = 0;
	default_shipmodel = 0;

	while (shipsini.getline()) {
		if (shipsini.got_key()) {
			if (shipsini.section().compare("ship") == 0) {

				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("default")) {
					default_shipmodel = shipmodel;
					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)) {
					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();

	if (!default_shipmodel) {
		con_error << "No default ship model in ini/ships.ini!\n";
		return;
	}

	// add engine game functions
	core::Func::add("buy", (core::GameFuncPtr) func_buy);
	core::Func::add("join", (core::GameFuncPtr) func_join);
	core::Func::add("spectate", (core::GameFuncPtr) func_spectate);

	// add engine core functions
	core::Func::add("list_ship", (core::FuncPtr) func_list_ship);

	// add engine game variables
	core::Cvar::set("g_testgamevariable", "1", core::Cvar::Game);
	core::Cvar::set("g_name", name().c_str(), core::Cvar::Game | core::Cvar::ReadOnly);
	
	// indicate the module is ready to run frames
	module_running = true;
}

void Game::shutdown()
{
	// game functions are automaticly removed

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

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

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

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

void Game::player_disconnect(core::Player *player)
{
	if (player->control()) {
		// player has only one ship for now
		player->control()->die();
		player->player_control = 0;
	}
}

} // namespace game