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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* PARSEC - Radar Display Code
*
* $Author: uberlinuxguy $ - $Date: 2004/09/26 03:43:37 $
*
* Orginally written by:
* Copyright (c) Markus Hadwiger <msh@parsec.org> 1996-1999
* Copyright (c) Andreas Varga <sid@parsec.org> 1999
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// C library
#include <ctype.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// compilation flags/debug support
#include "config.h"
#include "debug.h"
// general definitions
#include "general.h"
#include "objstruc.h"
// global externals
#include "globals.h"
// subsystem headers
#include "aud_defs.h"
// drawing subsystem
#include "d_bmap.h"
#include "d_misc.h"
// mathematics header
#include "utl_math.h"
// local module header
#include "h_radar.h"
// proprietary module headers
#include "con_arg.h"
#include "con_aux.h"
#include "con_com.h"
#include "con_main.h"
#include "e_color.h"
#include "e_supp.h"
#include "h_cockpt.h"
#include "obj_ctrl.h"
#include "obj_cust.h"
// epsilon for null vector ----------------------------------------------------
//
#define EPS_VECLEN 1e-7
// colors of radar objects ----------------------------------------------------
//
#define TARGET_RADAR_OBJ_COL 255 // white
#define NORMAL_RADAR_OBJ_COL1 80 // red
#define NORMAL_RADAR_OBJ_COL2 87
#define NORMAL_RADAR_OBJ_COL3 91
#define MISSILE_RADAR_OBJ_COL 167 // yellow
#define STARGATE_RADAR_OBJ_COL 225 // blue
#define PLANET_RADAR_OBJ_COL 102 // green
#define TELEPORTER_RADAR_OBJ_COL 102 // green // FIXME: use this for now
// generic string paste area --------------------------------------------------
//
#define PASTE_STR_LEN 255
static char paste_str[ PASTE_STR_LEN + 1 ];
// geometry of radar ----------------------------------------------------------
//
int hud_radar_w;
int hud_radar_h;
int hud_radar_x;
int hud_radar_y;
int hud_radar_bitm_x;
int hud_radar_bitm_y;
// check vector length for zero -----------------------------------------------
//
INLINE
int LenZero( hprec_t len )
{
ASSERT( len >= 0 );
if ( len < EPS_VECLEN ) {
MSGOUT( "radar: diminishing vector encountered." );
return TRUE;
}
return FALSE;
}
// area of radar frame --------------------------------------------------------
//
static int area_x;
static int area_y;
static int area_width;
static int area_height;
// draw radar object after calculating its position ---------------------------
//
PRIVATE
void CalcDrawRadarObj( GenObject *objpo, int col )
{
ASSERT( objpo != NULL );
// fetch ship's position
float ox = GEOMV_TO_FLOAT( objpo->CurrentXmatrx[ 0 ][ 3 ] );
float oy = GEOMV_TO_FLOAT( objpo->CurrentXmatrx[ 1 ][ 3 ] );
float oz = GEOMV_TO_FLOAT( objpo->CurrentXmatrx[ 2 ][ 3 ] );
// square components
hprec_t ox2 = ox * ox;
hprec_t oy2 = oy * oy;
hprec_t oz2 = oz * oz;
// length of vector
hprec_t len = sqrt( ox2 + oy2 + oz2 );
if ( LenZero( len ) )
return;
// length of projection onto (xz)-plane
hprec_t lxz = sqrt( ox2 + oz2 );
if ( LenZero( lxz ) )
return;
// yaw of projected vector (theta)
hprec_t yaw = asin( ox / lxz );
// bring range to [-PI..0] and [0..+PI]
if ( oz < 0 )
yaw = ( ox < 0 ) ? -HPREC_PI - yaw : HPREC_PI - yaw;
// x axis is sin( yaw/2 ) weighted with cos( y-axis ):
// combined left/right and front/back info in sine scale
// [ formula: x = sin( theta/2 ) * cos( phi ) ]
hprec_t rx = sin( yaw / 2 ) * ( lxz / len );
// y axis is sin( pitch ):
// above/below info in sine scale
// [ formula: y = sin( phi ) ]
hprec_t ry = oy / len;
// draw actual radar object dot
int radarx = (int)( rx * area_width );
int radary = (int)( ry * area_height );
D_DrawRadarObj( area_x + radarx , area_y + radary, COLINDX_TO_VISUAL( col ) );
}
// the range of the elite radar -----------------------------------------------
//
#define ELITE_RADAR_RANGE_MIN 4096.0
#define ELITE_RADAR_RANGE_MAX 65535.0
static int radar_range = (int)ELITE_RADAR_RANGE_MIN;
// draw radar object after calculating its position ---------------------------
//
PRIVATE
void CalcDrawEliteRadarObj( GenObject *objpo, int col )
{
ASSERT( objpo != NULL );
// fetch ship's position
float ox = GEOMV_TO_FLOAT( objpo->CurrentXmatrx[ 0 ][ 3 ] );
float oy = GEOMV_TO_FLOAT( objpo->CurrentXmatrx[ 1 ][ 3 ] );
float oz = GEOMV_TO_FLOAT( objpo->CurrentXmatrx[ 2 ][ 3 ] );
int radar_width = 200;
int radar_width_2 = 200 / 2;
int radar_height = 32;
int radar_height_2 = 32 / 2;
int radar_centerx = Screen_XOfs + 2;
int radar_centery = Screen_Height - 100 + 68;
if ( AUX_DRAW_COCKPIT && AUX_DRAW_COCKPIT_RADAR ) {
radar_centerx = (int)(Screen_Width - 0.4953f * Scaled_Screen_Width + 1);
radar_centery = (int)(Screen_Height - 0.1187f * Scaled_Screen_Height);
radar_width = (int)(130 * scale_tab[ cp_scale ]);
radar_width_2 = (int)(( 130 / 2 ) * scale_tab[ cp_scale ]);
radar_height = (int)(32 * scale_tab[ cp_scale ]);
radar_height_2 = (int)(( 32 / 2 ) * scale_tab[ cp_scale ]);
}
// square components
hprec_t ox2 = ox * ox;
hprec_t oy2 = oy * oy;
hprec_t oz2 = oz * oz;
// length of vector
hprec_t len = sqrt( ox2 + oy2 + oz2 );
if ( LenZero( len ) )
return;
// don't draw objects that are too far away
if ( len > radar_range )
return;
hprec_t rx = ( ox / radar_range ) * radar_width_2;
hprec_t ry = ( -oz / radar_range ) * radar_height_2;
hprec_t rz = ( -oy / radar_range ) * ( Screen_Height - radar_centery - 3 );
int radar_x = (int)(radar_centerx + rx)
;
int radar_y = (int)(radar_centery + ry);
int height = (int)rz;
// draw actual radar object
visual_t xlatcol = COLINDX_TO_VISUAL( col );
if ( height > 0 ) {
D_DrawVertBar( radar_x, radar_y - height, xlatcol, height + 1 );
D_DrawHorzBar( radar_x, radar_y - height, xlatcol, 2 );
} else if ( height < 0 ) {
D_DrawVertBar( radar_x, radar_y, xlatcol, -height + 1 );
D_DrawHorzBar( radar_x, radar_y - height, xlatcol, 2 );
} else {
D_DrawHorzBar( radar_x, radar_y, xlatcol, 2 );
}
}
// scan ship objects for radar ------------------------------------------------
//
INLINE
void RadarScanShipObjects()
{
GenObject *targetobj = NULL;
for ( GenObject *walkobjs = FetchFirstShip(); walkobjs; ) {
// set color for normal objects
int rocolor = NORMAL_RADAR_OBJ_COL2;
if ( AUX_ENABLE_DEPTHCUED_RADAR_DOTS && !AUX_ENABLE_ELITE_RADAR ) {
Vertex3 shippos;
FetchTVector( walkobjs->ObjPosition, &shippos );
Vertex3 myshippos;
FetchTVector( MyShip->ObjPosition, &myshippos );
Vector3 dist;
VECSUB( &dist, &shippos, &myshippos );
geomv_t vx = dist.X;
geomv_t vy = dist.Y;
geomv_t vz = dist.Z;
ABS_GEOMV( vx );
ABS_GEOMV( vy );
ABS_GEOMV( vz );
int distance = 0;
if ( ( vx > GEOMV_VANISHING ) ||
( vy > GEOMV_VANISHING ) ||
( vz > GEOMV_VANISHING ) ) {
distance = GEOMV_TO_INT( VctLenX( &dist ) );
}
if ( distance > 4096 ) {
rocolor = NORMAL_RADAR_OBJ_COL3;
} else if ( distance > 2048 ) {
rocolor = NORMAL_RADAR_OBJ_COL2;
} else {
rocolor = NORMAL_RADAR_OBJ_COL1;
}
}
// ensure that the target object is drawn last
if ( walkobjs->HostObjNumber == TargetObjNumber ) {
if ( targetobj == NULL ) {
targetobj = walkobjs;
if ( ( walkobjs = walkobjs->NextObj ) == NULL ) {
walkobjs = targetobj;
}
continue;
}
rocolor = TARGET_RADAR_OBJ_COL;
}
// depict ship's position on radar
if ( AUX_ENABLE_ELITE_RADAR ) {
CalcDrawEliteRadarObj( walkobjs, rocolor );
} else {
CalcDrawRadarObj( walkobjs, rocolor );
}
if ( walkobjs == targetobj ) {
break;
}
if ( ( walkobjs = walkobjs->NextObj ) == NULL ) {
walkobjs = targetobj;
}
}
}
// scan missile objects for radar ---------------------------------------------
//
INLINE
void RadarScanMissileObjects()
{
GenObject *walkobjs = FetchFirstMissile();
for ( ; walkobjs; walkobjs = walkobjs->NextObj ) {
// set color for missile objects
int rocolor = MISSILE_RADAR_OBJ_COL;
// depict missile's position on radar
if ( AUX_ENABLE_ELITE_RADAR ) {
CalcDrawEliteRadarObj( walkobjs, rocolor );
} else {
CalcDrawRadarObj( walkobjs, rocolor );
}
}
}
// scan stargate objects for radar --------------------------------------------
//
INLINE
void RadarScanStargateObjects()
{
GenObject *walkobjs = FetchFirstCustom();
for ( ; walkobjs; walkobjs = walkobjs->NextObj ) {
// get type id of the custom stargate
static dword stargate_typeid = TYPE_ID_INVALID;
if ( stargate_typeid == TYPE_ID_INVALID ) {
stargate_typeid = OBJ_FetchCustomTypeId( "stargate" );
}
// we only want to walk stargates
if ( walkobjs->ObjectType != stargate_typeid ) {
continue;
}
// set color for stargate objects
int rocolor = STARGATE_RADAR_OBJ_COL;
// depict stargate's position on radar
if ( AUX_ENABLE_ELITE_RADAR ) {
CalcDrawEliteRadarObj( walkobjs, rocolor );
} else {
CalcDrawRadarObj( walkobjs, rocolor );
}
}
}
// scan teleporter objects for radar ------------------------------------------
//
INLINE
void RadarScanTeleporterObjects()
{
GenObject *walkobjs = FetchFirstCustom();
for ( ; walkobjs; walkobjs = walkobjs->NextObj ) {
// get type id of the custom stargate
static dword teleporter_typeid = TYPE_ID_INVALID;
if ( teleporter_typeid == TYPE_ID_INVALID ) {
teleporter_typeid = OBJ_FetchCustomTypeId( "teleporter" );
}
// we only want to walk stargates
if ( walkobjs->ObjectType != teleporter_typeid ) {
continue;
}
// set color for stargate objects
int rocolor = TELEPORTER_RADAR_OBJ_COL;
// depict stargate's position on radar
if ( AUX_ENABLE_ELITE_RADAR ) {
CalcDrawEliteRadarObj( walkobjs, rocolor );
} else {
CalcDrawRadarObj( walkobjs, rocolor );
}
}
}
// scan planet objects for radar -----------------------------------------------
//
INLINE
void RadarScanPlanetObjects()
{
GenObject *walkobjs = FetchFirstCustom();
for ( ; walkobjs; walkobjs = walkobjs->NextObj ) {
// get type id of the custom planet
static dword planet_typeid = TYPE_ID_INVALID;
if ( planet_typeid == TYPE_ID_INVALID ) {
planet_typeid = OBJ_FetchCustomTypeId( "planet" );
}
// we only want to walk planets
if ( walkobjs->ObjectType != planet_typeid ) {
continue;
}
// set color for planet objects
int rocolor = PLANET_RADAR_OBJ_COL;
// depict planet's position on radar
if ( AUX_ENABLE_ELITE_RADAR ) {
CalcDrawEliteRadarObj( walkobjs, rocolor );
} else {
CalcDrawRadarObj( walkobjs, rocolor );
}
}
}
// draw radar display (contents): dots denoting spacecrafts -------------------
//
void HUD_DrawHUDRadar()
{
if ( !AUX_ENABLE_ELITE_RADAR ) {
// make sure dots are positioned in/scaled to frame
if ( AUX_DRAW_COCKPIT && AUX_DRAW_COCKPIT_RADAR ) {
float center_x = 322.0f / 640.0f; // offset from left edge of screen
float center_y = 65.0f / 480.0f; // offset from top of texture
float width = 43.0f / 640.0f;
area_x = (int)(center_x * Screen_Width);
area_y = (int)(Screen_Height - ( cockpitinfo[ RADAR ].ypos - center_y ) * Scaled_Screen_Height);
area_width = (int)(width * Scaled_Screen_Width);
area_height = area_width;
} else {
area_x = hud_radar_x;
area_y = hud_radar_y - BitmapInfo[ BM_RADAR ].height/2;
area_width = BitmapInfo[ BM_RADAR ].width/2 - hud_radar_w;
area_height = BitmapInfo[ BM_RADAR ].height/2 - hud_radar_h;
}
}
// ship dots
RadarScanShipObjects();
// missile dots
if ( AUX_DRAW_MISSILES_ON_RADAR ) {
RadarScanMissileObjects();
}
// stargate dots
if ( AUX_DRAW_STARGATES_ON_RADAR ) {
RadarScanStargateObjects();
}
// planet dots
if ( AUX_DRAW_PLANETS_ON_RADAR ) {
RadarScanPlanetObjects();
}
// telepoter dots
if ( AUX_DRAW_TELEPORTERS_ON_RADAR ) {
RadarScanTeleporterObjects();
}
}
// draw radar frame (bitmap, no contents) -------------------------------------
//
void HUD_DrawHUDRadarFrame()
{
// no bitmap frame if new radar enabled
if ( AUX_DRAW_COCKPIT && AUX_DRAW_COCKPIT_RADAR )
return;
// standard radar is default
int radarid = BM_RADAR;
// check for alternate radar
if ( AUX_ENABLE_ELITE_RADAR ) {
// elite-style radar
static int eliteid = -1;
if ( eliteid == -1 ) {
eliteid = FetchBitmapId( BM_NAME_RADAR2 );
if ( eliteid == -1 ) {
return;
}
}
radarid = eliteid;
}
int drawx = hud_radar_bitm_x - BitmapInfo[ radarid ].width/2;
int drawy = hud_radar_bitm_y - BitmapInfo[ radarid ].height;
// draw radar circle
D_PutTrBitmap( BitmapInfo[ radarid ].bitmappointer,
BitmapInfo[ radarid ].width,
BitmapInfo[ radarid ].height,
drawx, drawy );
}
// change range of elite radar ------------------------------------------------
//
PRIVATE
int Cmd_RADAR_RANGE( char *paramstr )
{
//NOTE:
//CONCOM:
// range_command ::= 'radar.range' [<range>]
ASSERT( paramstr != NULL );
HANDLE_COMMAND_DOMAIN_SEP( paramstr );
int range = radar_range;
char *scan;
if ( (scan = QueryIntArgumentEx( paramstr, "%d", &range )) ) {
// determine if delta modification (++/--)
int delta = 0;
if ( ( scan[ 0 ] == '+' ) && ( scan[ 1 ] == '+' ) )
delta = 1;
else if ( ( scan[ 0 ] == '-' ) && ( scan[ 1 ] == '-' ) )
delta = -1;
if ( delta != 0 )
scan += 2;
if ( *scan != 0 ) {
char *errpart;
long sval = strtol( scan, &errpart, 10 );
if ( *errpart == 0 ) {
if ( delta != 0 )
sval = range + sval * delta;
if ( sval >= ELITE_RADAR_RANGE_MIN && sval <= ELITE_RADAR_RANGE_MAX ) {
radar_range = sval;
} else {
//NOTE:
// if delta modification was used, don't print a range_error
// but just beep to give the user some feedback...
if ( delta != 0 )
AUD_Select2();
else
CON_AddLine( range_error );
}
} else {
CON_AddLine( invalid_arg );
}
} else {
CON_AddLine( invalid_arg );
}
}
return TRUE;
}
// module registration function -----------------------------------------------
//
REGISTER_MODULE( H_RADAR )
{
user_command_s regcom;
memset( ®com, 0, sizeof( user_command_s ) );
// register "radar.range" command
regcom.command = "radar.range";
regcom.numparams = 1;
regcom.execute = Cmd_RADAR_RANGE;
regcom.statedump = NULL;
CON_RegisterUserCommand( ®com );
}
|