Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: a39943fdb1d890dc1ea13ccc7fdd7155a81afecb (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
/*
   client/targets.cc
   This file is part of the Osirion project and is distributed under 
   the terms of the GNU General Public License version 2 
*/

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

#include "audio/audio.h"
#include "audio/sources.h"
#include "client/input.h"
#include "client/view.h"
#include "client/video.h"
#include "core/application.h"
#include "core/entity.h"
#include "core/func.h"
#include "core/gameinterface.h"
#include "math/vector3f.h"
#include "render/gl.h"
#include "render/render.h"
#include "render/text.h"

namespace client {

namespace targets {

const float TARGETBOXRADIUS = 0.025f;
unsigned int current_target_id = 0;
unsigned int current_hover = 0;

core::Entity *current_target = 0;
core::Cvar *snd_engines = 0;


bool is_legal_target(core::Entity *entity)
{
	if (entity->id() == core::localcontrol()->id())
		return false;
	else
		return true;
}

core::Entity* current()
{
	return current_target;
}

unsigned int current_id()
{
	return current_target_id;
}

unsigned int hover()
{
	return current_hover;
}

void select_target(core::Entity *entity)
{
	current_target = entity;
	current_target_id = current_target->id();
	core::application()->notify_sound("ui/target");
}

void select_target(unsigned int id)
{
	// FIXME validate
	core::Entity * entity = core::Entity::find(id);
	if (entity)
		select_target(entity);
}

void func_target_next(std::string const &args)
{
	if (!core::localcontrol())
		return;

	if (!core::Entity::registry.size()) {
		current_target = 0;
		current_target_id = 0;
		return;
	}

	std::map<unsigned int, core::Entity *>::iterator it = core::Entity::registry.begin();
	if (!current_target_id) {

		// first entity
		it = core::Entity::registry.begin();
		while (!is_legal_target((*it).second) && it != core::Entity::registry.end())
			it++;

	} else {
		// current entity
		it = core::Entity::registry.find(current_target_id);

		// next legal entity
		if (it !=  core::Entity::registry.end())
			it++;
		if (it ==  core::Entity::registry.end()) {
			it =  core::Entity::registry.begin();
		}
		while (!is_legal_target((*it).second)) {
			it++;
			if (it == core::Entity::registry.end())
				it = core::Entity::registry.begin();

			if ((*it).first == current_target_id) {
				current_target = (*it).second;
				return;
			}
		}
	}

	if (it !=  core::Entity::registry.end()) {
		select_target((*it).second);
	} else {
		current_target = 0;
		current_target_id = 0;
	
	}
}

void func_target_prev(std::string const &args)
{
	if (!core::localcontrol())
		return;

	if (!core::Entity::registry.size()) {
		current_target = 0;
		current_target_id = 0;
		return;
	}

	std::map<unsigned int, core::Entity *>::reverse_iterator rit = core::Entity::registry.rbegin();
	if (!current_target_id) {
		// last entity
		rit = core::Entity::registry.rbegin();
		while (!is_legal_target((*rit).second) && rit != core::Entity::registry.rend())
			rit++;
	} else {
		// current entity
		while (rit != core::Entity::registry.rend() && ((*rit).first != current_target_id))
			++rit;

		// previous legal entity
		if (rit !=  core::Entity::registry.rend())
			++rit;
		if (rit ==  core::Entity::registry.rend()) {
			rit =  core::Entity::registry.rbegin();
		}
		while (!is_legal_target((*rit).second)) {
			++rit;
			if (rit == core::Entity::registry.rend())
				rit = core::Entity::registry.rbegin();

			if ((*rit).first == current_target_id) {
				current_target = (*rit).second;
				return;
			}
		}
	}

	if (rit != core::Entity::registry.rend()) {
		select_target((*rit).second);

	} else {
		current_target = 0;
		current_target_id = 0;
	}
}

void reset()
{
	current_target = 0;
	current_target_id = 0;
	current_hover = 0;
	
}

void init()
{
	snd_engines = core::Cvar::get("snd_engines", "1", core::Cvar::Archive);
	snd_engines->set_info("[bool] enable or disable engine sounds");

	core::Func *func = 0;

	func = core::Func::add("target_next", func_target_next);
	func->set_info("select next target");

	core::Func::add("target_prev", func_target_prev);
	func->set_info("select previous target");

	reset();
}

void shutdown()
{
	reset();

	core::Func::remove("target_next");
	core::Func::remove("target_prev");
}

void render_listener_sound()
{
	if (!(snd_engines && snd_engines->value()))
		return;

	math::Vector3f velocity(0, 0 ,0);
	if (core::localcontrol()) {
		velocity.assign(core::localcontrol()->state()->axis().forward() * core::localcontrol()->speed());
	}

	audio::update_listener(render::Camera::eye(), render::Camera::axis(), velocity);
}

void render_entity_sound(core::Entity *entity)
{
	if (!(entity->type() == core::Entity::Controlable))
		return;

	if (!(snd_engines && snd_engines->value())) {
		entity->state()->clearsound();
		return;
	}

	core::EntityControlable *entitycontrolable = (core::EntityControlable *) entity;
	core::ClientState *state = entity->state();

	if (entity->model() && state->detailvisible() && entitycontrolable->thrust() > 0 ) {

		float speed = entitycontrolable->speed();
		float pitch = 0.2f + entitycontrolable->thrust() * 0.8f;
		
		if (!state->state_enginesound) {
			if ((state->state_enginesound = audio::Sources::get()) > 0 ) {

				size_t enginesound = 0;
				if (entity->model())
					enginesound = entity->model()->enginesound();
				
				std::stringstream soundname;
				soundname << "engines/loop" << std::setfill('0') << std::setw(2) << enginesound;
				audio::loop(state->state_enginesound, soundname.str().c_str(), pitch, 0);
			}
		}

		if (state->state_enginesound) {
			audio::update_source(state->state_enginesound,
				state->location() - state->axis().forward() * entity->model()->maxbbox().y , state->axis().forward() * speed, pitch);
		}
	} else {
		entity->state()->clearsound();
	}
}

// render client targets
void frame()
{

	/* Notes
		http://en.wikipedia.org/wiki/Line-plane_intersection
		http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
	*/
	using math::Vector3f;

	render_listener_sound();

	current_target = 0;
	current_hover = 0;

	float z = -1;

	// mouse cursor location in 3d world space
	float x = (float)(input::mouse_x - video::width /2) / (float)video::width;
	float y = (float)(input::mouse_y - video::height /2) / (float)video::height / render::Camera::aspect();

	Vector3f cursor = render::Camera::eye() + render::Camera::axis().forward() * (render::Camera::frustum_front() + 0.001);
	cursor -= render::Camera::axis().left() * x;
	cursor -= render::Camera::axis().up() * y;

	math::Vector3f center = render::Camera::eye() + (render::Camera::axis().forward() * (render::Camera::frustum_front() +0.01f));
	for (core::Entity::Registry::iterator it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) {
		core::Entity *entity = (*it).second;
		
		// render entity sound
		render_entity_sound(entity);

		// find the current target
		if (entity->id() == current_target_id) {
			current_target = entity;
		}
		
		// check if the mouse is hovering the entity
		if (core::localcontrol() && entity->state()->visible()) {
			Vector3f v(entity->state()->location() - render::Camera::eye());
			v.normalize();

			if (is_legal_target(entity) && (math::dotproduct(render::Camera::axis().forward(), v) > 0.75 )) {

				// caculate the distance from entity location to the line [eye - cursor]
				float d = math::Vector3f::length(math::crossproduct( (cursor - render::Camera::eye()) , (render::Camera::eye() - entity->location()))) / math::Vector3f::length(cursor - render::Camera::eye());
	
				// if the cursor-beam hits the entity sphere
				if (d < entity->radius() * 0.5f) {
					float myz = math::distance(cursor, entity->location());
					if (z < 0 || myz < z) {
						current_hover = entity->id();
						z = myz;
					}
				}
			}
		}
	}
	
	if (!current_target) {
		current_target_id = 0;
	} else {
		current_target_id  = current_target->id();
	}
}

void draw_target()
{
	using math::Vector3f;

	if (!current_target)
		return;

	core::Entity *entity = current_target;

	// if target is in visible space
	Vector3f v(entity->state()->location() - render::Camera::eye());
	v.normalize();
	if (math::dotproduct(render::Camera::axis().forward(), v) > 0.75 ) {

		// find the intersection of the line [ eye - target ] in the frustum front plane
		Vector3f const &plane_normal = render::Camera::axis().forward();
		Vector3f const &plane_point = render::Camera::eye() + render::Camera::axis().forward() * (render::Camera::frustum_front() + 0.001);

		float d = (plane_normal.x * plane_point.x + plane_normal.y * plane_point.y + plane_normal.z * plane_point.z);
		float t = d - plane_normal.x * render::Camera::eye().x 
			- plane_normal.y * render::Camera::eye().y
			- plane_normal.z * render::Camera::eye().z;
		t /= plane_normal.x * (entity->state()->location().x - render::Camera::eye().x)
			+ plane_normal.y * (entity->state()->location().y - render::Camera::eye().y)
			+ plane_normal.z * (entity->state()->location().z - render::Camera::eye().z);

		Vector3f intersection = render::Camera::eye() + (entity->state()->location() - render::Camera::eye()) * t;

		// draw the target cursor in the frustum front plane, but in real world coordinates
		const float r = 0.05;
		render::gl::color(1, 1, 1, 1);
		render::gl::begin(render::gl::LineLoop);
		render::gl::vertex(intersection + render::Camera::axis().up() * r);
		render::gl::vertex(intersection + render::Camera::axis().left() * r);
		render::gl::vertex(intersection - render::Camera::axis().up() * r);
		render::gl::vertex(intersection - render::Camera::axis().left() * r);
		render::gl::end();
	}
}

}

}