Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: ae2afb05adcdf6d75926f6b3912700e2b7aadb79 (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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
   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/buffers.h"
#include "audio/sources.h"
#include "auxiliary/functions.h"
#include "audio/sources.h"
#include "client/client.h"
#include "client/input.h"
#include "client/soundext.h"
#include "client/video.h"
#include "core/application.h"
#include "core/entity.h"
#include "core/func.h"
#include "core/gameinterface.h"
#include "core/range.h"
#include "math/axis.h"
#include "math/vector3f.h"
#include "render/draw.h"
#include "render/state.h"

namespace client
{

namespace targets
{

unsigned int current_target_id = 0;
unsigned int current_hover = 0;

const core::Entity *current_target = 0;
math::Vector3f cursor_aim;

bool is_valid_map_target(const core::Entity *entity)
{
	if (entity->serverside()) {
		return false;
	} else if (entity == core::localcontrol()) {
		return false;
	} else if (entity->has_flag(core::Entity::ShowOnMap)) {
		return true;
	} else 	if (entity == core::localplayer()->mission_target()) {
		return true;
	} else {
		return false;
	}
}

bool is_valid_hud_target(const core::Entity *entity)
{
	if (entity->serverside()) {
		return false;
	} else if (entity->type() == core::Entity::Projectile) {
		return false;
	} else if (entity == core::localcontrol()) {
		return false;
	} else if (entity->has_flag(core::Entity::ShowOnMap)) {
		return true;
	} else 	if (entity == core::localplayer()->mission_target()) {
		return true;
	} else 	if (entity == core::localplayer()->autopilot_target()) {
		return true;
	} else if (!ext_render(entity)) {
		return false;
	} else {
		return ext_render(entity)->visible();
	}
}

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

unsigned int current_id()
{
	return current_target_id;
}

unsigned int hover()
{
	return current_hover;
}

void set_target(const core::Entity *entity)
{
	current_target = entity;
	if (entity) {
		current_target_id = current_target->id();
		audio::play("ui/target");
	} else {
		current_target_id = 0;
	}
}

void set_target(unsigned int id)
{
	if (!core::localcontrol())
		return;
	core::Zone *zone = core::localcontrol()->zone();
	if (!zone)
		return;

	core::Entity *entity = zone->find_entity(id);

	if (entity && is_valid_hud_target(entity))
		set_target(entity);
}

void func_target_next(std::string const &args)
{
	if (!core::localcontrol())
		return;
	core::Zone *zone = core::localcontrol()->zone();
	if (!zone)
		return;

	if (!zone->content().size()) {
		current_target = 0;
		current_target_id = 0;
		return;
	}

	core::Zone::Content::iterator it = zone->content().begin();
	if (!current_target_id) {

		// first entity
		it = zone->content().begin();
		while ((it != zone->content().end()) && !is_valid_hud_target((*it))) {
			it++;
		}

	} else {
		// current entity
		while ((it != zone->content().end()) && ((*it)->id() != current_target_id)) {
			++it;
		}

		// next legal entity
		if (it !=  zone->content().end())
			it++;
		if (it ==  zone->content().end()) {
			it =  zone->content().begin();
		}
		while (!is_valid_hud_target((*it))) {
			it++;
			if (it == zone->content().end())
				it = zone->content().begin();

			if ((*it)->id() == current_target_id) {
				current_target = (*it);
				return;
			}
		}
	}

	if (it !=  zone->content().end() && is_valid_hud_target((*it))) {
		set_target((*it));
	} else {
		current_target = 0;
		current_target_id = 0;

	}
}

void func_target_prev(std::string const &args)
{
	if (!core::localcontrol())
		return;
	core::Zone *zone = core::localcontrol()->zone();
	if (!zone)
		return;

	if (!zone->content().size()) {
		current_target = 0;
		current_target_id = 0;
		return;
	}

	core::Zone::Content::reverse_iterator rit = zone->content().rbegin();
	if (!current_target_id) {
		// last entity
		rit = zone->content().rbegin();
		while ((rit != zone->content().rend()) && !is_valid_hud_target((*rit))) {
			++rit;
		}
	} else {
		// current entity
		while ((rit != zone->content().rend()) && ((*rit)->id() != current_target_id)) {
			++rit;
		}

		// previous legal entity
		if (rit !=  zone->content().rend())
			++rit;
		if (rit ==  zone->content().rend()) {
			rit =  zone->content().rbegin();
		}
		while (!is_valid_hud_target((*rit))) {
			++rit;
			if (rit == zone->content().rend())
				rit = zone->content().rbegin();

			if ((*rit)->id() == current_target_id) {
				current_target = (*rit);
				return;
			}
		}
	}

	if (rit != zone->content().rend() && is_valid_hud_target((*rit))) {
		set_target((*rit));
	} else {
		current_target = 0;
		current_target_id = 0;
	}
}

void func_target_controlable_next(std::string const &args)
{
	if (!core::localcontrol())
		return;
	core::Zone *zone = core::localcontrol()->zone();
	if (!zone)
		return;

	if (!zone->content().size()) {
		current_target = 0;
		current_target_id = 0;
		return;
	}

	core::Zone::Content::iterator it = zone->content().begin();
	
	if (!current_target_id) {
		// first entity
		it = zone->content().begin();
		while (
			(it != zone->content().end()) && (
				((*it)->type() != core::Entity::Controlable) ||  
				(math::distancesquared(core::localcontrol()->location(), (*it)->location()) > core::range::fxdistance * core::range::fxdistance) ||
				!is_valid_hud_target((*it))
			)
		) {
			++it;
		}

		if (it ==  zone->content().end()) {
			current_target = 0;
			current_target_id = 0;
		} else {
			set_target((*it));
		}
		
		return;
		
	} else {
		// current entity
		while (it != zone->content().end() && ((*it)->id() != current_target_id)) {
			++it;
		}

		// next legal entity
		if (it !=  zone->content().end())
			it++;
		if (it ==  zone->content().end()) {
			it =  zone->content().begin();
		}
		while (!(
				is_valid_hud_target((*it)) && 
				((*it)->type() == core::Entity::Controlable) &&  
				((static_cast<core::EntityControlable *>((*it))->owner() || (math::distancesquared(core::localcontrol()->location(), (*it)->location()) < core::range::fxdistance * core::range::fxdistance))) 
			)
		) {
			it++;
			if (it == zone->content().end())
				it = zone->content().begin();

			if ((*it)->id() == current_target_id) {
				current_target = (*it);
				return;
			}
		}
	}

	if ((it !=  zone->content().end()) && 
				is_valid_hud_target((*it)) &&
				((*it)->type() == core::Entity::Controlable) &&  
				((static_cast<core::EntityControlable *>((*it))->owner() || (math::distancesquared(core::localcontrol()->location(), (*it)->location()) < core::range::fxdistance * core::range::fxdistance))) 
	) {
		set_target((*it));
	} else {
		current_target = 0;
		current_target_id = 0;
	}
}

void func_target_controlable_prev(std::string const &args)
{
	if (!core::localcontrol())
		return;
	core::Zone *zone = core::localcontrol()->zone();
	if (!zone)
		return;

	if (!zone->content().size()) {
		current_target = 0;
		current_target_id = 0;
		return;
	}

	core::Zone::Content::reverse_iterator rit = zone->content().rbegin();
	if (!current_target_id) {
		// last entity
		rit = zone->content().rbegin();
		while (
			(rit != zone->content().rend()) && (
				((*rit)->type() != core::Entity::Controlable) ||  
				(math::distancesquared(core::localcontrol()->location(), (*rit)->location()) > core::range::fxdistance * core::range::fxdistance) ||
				!is_valid_hud_target((*rit))
			)
		) {
			++rit;
		}
		
		if (rit ==  zone->content().rend()) {
			current_target = 0;
			current_target_id = 0;
		} else {
			set_target((*rit));
		}
		
		return;
		
	} else {
		// current entity
		while (rit != zone->content().rend() && ((*rit)->id() != current_target_id)) {
			++rit;
		}

		// previous legal entity
		if (rit !=  zone->content().rend())
			++rit;
		if (rit ==  zone->content().rend()) {
			rit =  zone->content().rbegin();
		}
		while (!(
				is_valid_hud_target((*rit)) && 
				((*rit)->type() == core::Entity::Controlable) &&  
				((static_cast<core::EntityControlable *>((*rit))->owner() || (math::distancesquared(core::localcontrol()->location(), (*rit)->location()) < core::range::fxdistance * core::range::fxdistance))) 
			)
		) {				
			++rit;
			if (rit == zone->content().rend())
				rit = zone->content().rbegin();

			if ((*rit)->id() == current_target_id) {
				current_target = (*rit);
				return;
			}
		}
	}

	if ((rit !=  zone->content().rend()) && 
				is_valid_hud_target((*rit)) &&
				((*rit)->type() == core::Entity::Controlable) &&  
				((static_cast<core::EntityControlable *>((*rit))->owner() || (math::distancesquared(core::localcontrol()->location(), (*rit)->location()) < core::range::fxdistance * core::range::fxdistance))) 
	) {
		set_target((*rit));
	} else {
		current_target = 0;
		current_target_id = 0;		
	}
}

void func_target_none(std::string const &args)
{
	current_target = 0;
	current_target_id = 0;
}

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

	// this is essentialy the hover algorithm with the cursor in the center
	const core::Entity *new_target = 0;
	math::Vector3f center = render::camera().location() + render::camera().axis().forward() * (render::FRUSTUMFRONT + 0.001);
	float smallest_d = -1;

	for (core::Zone::Content::const_iterator it = core::localcontrol()->zone()->content().begin(); it != core::localcontrol()->zone()->content().end(); it++) {
		const core::Entity *entity = (*it);

		math::Vector3f v(entity->location() - render::camera().location());
		v.normalize();

		if (is_valid_hud_target(entity) && math::dotproduct(render::camera().axis().forward(), v) > 0.85) {

			// calculate the distance from entity location to the line [eye - cursor]
			float d = math::Vector3f::length(math::crossproduct((center - render::camera().location()) , (render::camera().location() - entity->location()))) / math::Vector3f::length(center - render::camera().location());

			// the entity closer to the center beam
			if (smallest_d < 0 || d < smallest_d) {
				new_target = entity;
				smallest_d = d;
			}
		}
	}

	if (new_target)
		set_target(new_target);
}

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

}

void init()
{
	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");

	core::Func::add("target_none", func_target_none);
	func->set_info("deselect current target");

	core::Func::add("target_center", func_target_center);
	func->set_info("select target near center");
	
	func = core::Func::add("target_controlable_next", func_target_controlable_next);
	func->set_info("select next controlable target");

	core::Func::add("target_controlable_prev", func_target_controlable_prev);
	func->set_info("select previous controlable target");

	reset();
}

void shutdown()
{
	reset();

	core::Func::remove("target_next");
	core::Func::remove("target_prev");
	core::Func::remove("target_none");
	core::Func::remove("target_center");
	core::Func::remove("target_controlable_next");
	core::Func::remove("target_controlable_prev");
}

const math::Vector3f & aim()
{
	return cursor_aim;
}

// render targets and sounds (in world coordinates)
void frame()
{
	core::Zone *zone = core::localplayer()->zone();
	if (!zone)
		return;

	/* 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 = 0;
	float y = 0;

	if ((input::joystick_lastmoved_time() > input::mouse_lastmoved_time()) && (render::camera().mode() == render::Camera::Cockpit || render::camera().mode() == render::Camera::Track)) {
		x = 0;
		y = 0;
	} else {
		x = (float)(input::mouse_position_x() - render::State::width() / 2) / (float)render::State::width();
		y = (float)(input::mouse_position_y() - render::State::height() / 2) / (float)render::State::height() / render::State::aspect();
	}

	cursor_aim.assign(render::camera().location() + render::camera().axis().forward() * (render::FRUSTUMFRONT + 0.001));
	cursor_aim -= render::camera().axis().left() * x;
	cursor_aim -= render::camera().axis().up() * y;
	
	float aim_distance = core::range::fxdistance;

	//math::Vector3f center = render::camera().location() + (render::camera().axis().forward() * (render::FRUSTUMFRONT + 0.001f));
	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
		core::Entity *entity = (*it);

		// render entity sound
		if ((entity->type() == core::Entity::Dynamic) || (entity->type() == core::Entity::Controlable) || (entity->type() == core::Entity::Projectile) || (entity->model() && entity->model()->sounds().size())) {
			render_entity_sound(entity);
		}

		// find the current target
		if (!core::localplayer()->view() && core::localcontrol() && (is_valid_hud_target(entity) || is_valid_map_target(entity))) {

			if (entity->id() == current_target_id) {
				current_target = entity;
			}

			// check if the mouse is hovering the entity
			Vector3f v(entity->location() - render::camera().location());
			v.normalize();

			if (math::dotproduct(render::camera().axis().forward(), v) > 0.75) {

				// calculate the distance from entity location to the line [eye - cursor]
				float d = math::Vector3f::length(math::crossproduct((cursor_aim - render::camera().location()) , (render::camera().location() - entity->location()))) / math::Vector3f::length(cursor_aim - render::camera().location());

				float r = entity->radius();

				if (ext_render(entity)->distance() > 512.0f)
					math::clamp(r, 8.0f, r);
				else if (ext_render(entity)->distance() > 256.0f)
					math::clamp(r, 4.0f, r);
				else if (ext_render(entity)->distance() > 128.0f)
					math::clamp(r, 2.0f, r);

				// if the cursor-beam hits the entity sphere
				if ((d < r) && ((current_target == entity) || (d < r * 0.75f))) {
					float myz = math::distance(cursor_aim, entity->location());
					if (z < 0 || myz < z) {
						current_hover = entity->id();
						// aim slightly behind target to prevent colliding projectiles
						aim_distance = entity->radius() + math::distance(render::camera().location(), entity->location());
						z = myz;
					}
				}
			}


		}
	}
	
	cursor_aim = render::camera().location() + (cursor_aim - render::camera().location()) * aim_distance / math::distance(render::camera().location(), cursor_aim);

	if (!current_target) {
		current_target_id = 0;
	} else {
		current_target_id  = current_target->id();
	}
	
	// set aim
	if (core::localcontrol()) {		
		core::localcontrol()->set_target_aim(cursor_aim);
	}

}

}

}