Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 89ff343486bd70b669e8313af3228fa9943e95f5 (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
/*
   game/ship.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 
*/

// project headers
#include "auxiliary/functions.h"
#include "core/gameserver.h"
#include "game/game.h"
#include "game/ship.h"
#include "math/mathlib.h"

// C++ headers
#include <iostream>

using math::degrees360f;
using math::degrees180f;

namespace game {

const float 		MIN_DELTA = 10e-10;

Ship::Ship(core::Player *owner, ShipModel *shipmodel) :
	core::EntityControlable(owner, ship_enttype)
{
	entity_modelname = "ships/" + shipmodel->modelname();
	entity_name = shipmodel->name() + ": <^B" + owner->name() + "^N>";
	entity_label = shipmodel->label();

	entity_moduletypeid = ship_enttype;

	current_target_direction = 0.0f;
	current_target_pitch = 0.0f;;
	current_target_roll = 0.0f;

	entity_color = owner->color();
	entity_color_second = owner->color_second();

	ship_shipmodel = shipmodel;
	ship_jumpdrive = shipmodel->shipmodel_jumpdrive;
	ship_jumpdrive_activated = 0;
	ship_jumptargetzone = 0;

	ship_countdown = 0;

	ship_impulse = false;
}

Ship::~Ship() 
{

}

void Ship::impulse()
{
	if (ship_impulse) {
		ship_impulse = false;

	} else { 
		if (ship_jumpdrive_activated) {
			ship_jumpdrive_activated = 0;
			ship_jumptargetzone = 0;
			ship_countdown = 0;
		}
	
		ship_impulse = true;
	}
}

void Ship::jump(std::string const &args)
{
	if (!jumpdrive()) {
		core::server()->send(owner(), "This ship is not equiped with a hyperspace drive!");
		return;
	}
	
	if (ship_jumpdrive_activated) {
		core::server()->send(owner(), "Jump aborted, hyperspace drive deactivated.");
		ship_jumpdrive_activated = 0;
		ship_jumptargetzone = 0;
		ship_countdown = 0;
		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(owner(), "Usage: jump [^B" + helpstr + "^N]");
		return;
	}

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

	if (ship_jumptargetzone == zone()) {
		core::server()->send(owner(), "Already in the " + ship_jumptargetzone->name() + '.');
		ship_jumptargetzone = 0;
		return;
	}

	ship_jumpdrive_activated = core::server()->time();
	ship_countdown = 5;
	ship_impulse = false;

	std::stringstream msg("");
	msg << "Hyperspace drive activated. Jumping in " << ship_countdown << "...";
	core::server()->send(owner(), msg.str());

}

void Ship::frame(float seconds) 
{
	const float direction_change_speed = 2;
	float cosangle;		// cosine of an angle
	float angle;		// angle in radians	
	math::Vector3f n;	// normal of a plane

	float actual_maxspeed = ship_shipmodel->maxspeed();
	float actual_turnspeed = ship_shipmodel->turnspeed();
	float actual_acceleration = ship_shipmodel->acceleration();

	// jumpdrive
	// target axis
	math::Axis target_axis(entity_axis);

	if (ship_jumpdrive_activated) {

		if (ship_jumpdrive_activated + 1.0f <= core::server()->time()) {
			ship_countdown -= 1.0f;
			if (ship_countdown <= 0) {
				core::server()->send(owner(), "Jumping to '" + ship_jumptargetzone->name() + '\'');
				set_zone(ship_jumptargetzone);
				if (owner()->control() == (EntityControlable*) this)
					owner()->set_zone(ship_jumptargetzone);
				ship_jumpdrive_activated = 0;
				ship_jumptargetzone = 0;
				return;
			} else {
				std::stringstream msg("");
				msg << ship_countdown << "...";
				core::server()->send(owner(), msg.str());
				ship_jumpdrive_activated = core::server()->time();
			}
		}

		// control is disables while the jumpdrive is activated
		target_thrust = 0;
		target_pitch = 0;
		target_roll = 0;
		target_direction = 0;

	} else if (ship_impulse) {

		// clamp input values
		target_thrust = 1.0f;
		math::clamp(target_pitch, -1.0f, 1.0f);
		math::clamp(target_roll, -1.0f, 1.0f);
		math::clamp(target_direction, -1.0f, 1.0f);

		actual_maxspeed = Game::instance()->g_impulsespeed->value();
		actual_acceleration = Game::instance()->g_impulseacceleration->value();
		actual_turnspeed *= 0.5;

	} else {
		// clamp input values
		math::clamp(target_thrust, 0.0f, 1.0f);
		math::clamp(target_pitch, -1.0f, 1.0f);
		math::clamp(target_roll, -1.0f, 1.0f);
		math::clamp(target_direction, -1.0f, 1.0f);

		if (speed() > actual_maxspeed) {
			actual_acceleration = Game::instance()->g_impulseacceleration->value();
			actual_turnspeed *= 0.5;
		}
	}

	// update thrust
	if (entity_thrust < target_thrust) {
		entity_thrust += seconds  * 0.5f;
		if (entity_thrust > target_thrust)
			entity_thrust = target_thrust;
	} else if (entity_thrust > target_thrust) {
		entity_thrust -= seconds * 0.5f;
		if (entity_thrust < target_thrust)
			entity_thrust = target_thrust;
	}
	//math::clamp(entity_thrust, 0.0f, 1.0f);

	// update roll
	if (current_target_roll < target_roll) {
		current_target_roll += direction_change_speed * seconds;
		if (current_target_roll > target_roll)
			current_target_roll = target_roll;
	} else if (current_target_roll > target_roll) {
		current_target_roll -= direction_change_speed * seconds;
		if (current_target_roll < target_roll)
			current_target_roll = target_roll;
	}
	math::clamp(current_target_roll, -1.0f, 1.0f);

	if (fabs(current_target_roll) > MIN_DELTA) {	
		float roll_offset = seconds * current_target_roll;
		entity_axis.change_roll(actual_turnspeed * roll_offset);
	} else {
		current_target_roll = 0.0f;
	}

	// update target_axis direction	
	if (current_target_direction < target_direction) {
		current_target_direction += direction_change_speed * seconds;
		if (current_target_direction > target_direction) {
			current_target_direction = target_direction;
		}
	} else if (current_target_direction > target_direction) {
		current_target_direction -= direction_change_speed * seconds;
		if (current_target_direction < target_direction) {
			current_target_direction = target_direction;
		}
	}

	if (fabs(current_target_direction) > MIN_DELTA ) {
		math::clamp(current_target_direction, -1.0f, 1.0f);
		target_axis.change_direction(actual_turnspeed * current_target_direction);
	} else {
		current_target_direction = 0.0f;
	}

	if (current_target_pitch < target_pitch) {
		current_target_pitch += direction_change_speed * seconds;
		if (current_target_pitch > target_pitch)
			current_target_pitch = target_pitch;
	} else if (current_target_pitch > target_pitch) {
		current_target_pitch -= direction_change_speed * seconds;
		if (current_target_pitch < target_pitch)
			current_target_pitch = target_pitch;
	}
	
	if (fabs(current_target_pitch) > MIN_DELTA) {
		math::clamp(current_target_pitch, -1.0f, 1.0f);
		target_axis.change_pitch(actual_turnspeed * current_target_pitch);
	} else {
		current_target_pitch = 0.0f;
	}

	n.assign(math::crossproduct(entity_axis.forward(), target_axis.forward()));
	if (!(n.length() < MIN_DELTA)) {
		n.normalize();
		cosangle = math::dotproduct(entity_axis.forward(), target_axis.forward());
		angle = acos(cosangle) * seconds; // * 180.0f / M_PI;
		if (angle > MIN_DELTA)
			entity_axis.rotate(n, -angle);
	}

	// update speed
	if (entity_speed < entity_thrust * actual_maxspeed) {
		entity_speed += actual_acceleration * seconds;
		if (entity_speed > entity_thrust * actual_maxspeed) {
			entity_speed = entity_thrust * actual_maxspeed;
		}
	} else if(entity_speed > entity_thrust * actual_maxspeed) {
		entity_speed -= actual_acceleration * seconds;
		if (entity_speed < 0.0f) entity_speed = 0.0f;
	}

	if (entity_speed != 0.0f) {
		entity_location += entity_axis.forward() * entity_speed * seconds;
		entity_dirty = true;
	} else if ((current_target_pitch != 0.0f) || (current_target_direction != 0.0f) || (current_target_roll != 0.0f)) {
		entity_dirty = true;
	}
}

} // namespace game