/
ViewTactical.cpp
2045 lines (1753 loc) · 59.6 KB
/
ViewTactical.cpp
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ViewTactical.cpp
(c)2000 Palestar, Richard Lyle
*/
#include "Debug/Assert.h"
#include "Audio/Sound.h"
#include "System/Keyboard.h"
#include "Standard/Settings.h"
#include "Math/Helpers.h"
#include "Math/Quat.h"
#include "Display/PrimitiveSetTransform.h"
#include "Display/PrimitiveLineStrip.h"
#include "Display/PrimitiveTriangleFan.h"
#include "DarkSpace/VerbDistress.h"
#include "DarkSpace/NounBody.h"
#include "World/NounZoneTarget.h"
#include "DarkSpace/NounProjectile.h"
#include "DarkSpace/NounJumpGate.h"
#include "DarkSpace/VerbOrderShip.h"
#include "DarkSpace/Constants.h"
#include "DarkSpace/GadgetWeapon.h"
#include "DarkSpace/GadgetBeamWeapon.h"
#include "DarkSpace/GadgetShield.h"
#include "DarkSpace/VerbSelfDestruct.h"
#include "DarkSpace/Resource.h"
#include "DarkSpace/CargoEnhancement.h"
#include "DarkSpace/NounEnhancement.h"
#include "DarkSpace/VerbLoad.h"
#include "Interface/GameDocument.h"
#include "Interface/ViewGame.h"
#include "Interface/ButtonEnhancement.h"
#include "Interface/WindowEnhancement.h"
#include "Interface/ViewTactical.h"
//----------------------------------------------------------------------------
const float ZOOM_SCALE[] =
{
2.0f,
4.0f,
8.0f,
16.0f,
32.0f,
64.0f,
128.0f,
256.0f,
512.0f,
1024.0f,
2048.0f,
};
const int ZOOM_LEVELS = sizeof(ZOOM_SCALE) / sizeof(ZOOM_SCALE[0]);
const int DISTRESS_MESSAGE_DELAY = TICKS_PER_SECOND * 5;
const float PIX2 = PI * 2;
static Constant DROP_TARGET_RANGE( "DROP_TARGET_RANGE", 500.0f );
Matrix33 ViewTactical::sm_CameraFrame; // destination camera frame
Vector3 ViewTactical::sm_CameraPosition;
Vector3 ViewTactical::sm_AdjustPosition;
static Constant LEFT_TARGET_TIME( "LEFT_TARGET_TIME", 0.5f );
static Constant SET_HEADING_RATE( "SET_HEADING_RATE", PI / 3 );
static Constant SET_VELOCITY_RATE( "SET_VELOCITY_RATE", 10.0f );
static Constant SET_YAW_RATE( "SET_YAW_RATE", 2.0f );
static Constant TACTICAL_UPDATE_BUTTONS( "TACTICAL_UPDATE_BUTTONS", 2.0f );
static Constant TACTICAL_CONTACT_UPDATE( "TACTICAL_CONTACT_UPDATE", 1.0f );
static Constant TACTICAL_SCANNER_RANGE( "TACTICAL_SCANNER_RANGE", 5000.0f );
static Constant SHIELD_ARMOR_LAYER( "SHIELD_ARMOR_LAYER", 50000.0f );
// copied from NounPlanet.cpp
static Constant PLANET_SCANNER_RANGE( "PLANET_SCANNER_RANGE", 2000.0f );
//----------------------------------------------------------------------------
IMPLEMENT_FACTORY( ViewTactical, WindowView::View );
REGISTER_FACTORY_KEY( ViewTactical, 4096692533678357889 );
ViewTactical::ViewTactical() :
m_bLeftDown( false ),
m_fLeftDownTime( 0.0f ),
m_bRightDown( false ),
m_fRightDownTime( 0.0f )
{
#ifdef _DEBUG
Settings settings( "ClientD" );
#else
Settings settings( "Client" );
#endif
m_CursorPosition2 = PointInt( 0,0 );
sm_CameraFrame = Matrix33( true );
sm_CameraPosition = Vector3( 0.0f );
m_CameraYaw = PI / 4;
m_CameraPitch = PI / 4;
m_LastCameraMode = m_CameraMode = TARGET;
m_CameraZoom = 50.0f;
m_CameraZoomLevel = 3;
m_fYaw = m_fYawV = 0.0f;
YawStatePressed m_yawState = YawStatePressed::None;
m_SetHeading = m_SetHeadingV = 0.0f;
m_SetVelocity = m_SetVelocityV = 0.0f;
m_BeginControl = false;
m_UpdateControl = false;
m_fUpdateContacts = 0.0f;
m_TargetTime = 0.0f;
m_ScannerRange = TACTICAL_SCANNER_RANGE;
m_Adjust = false;
m_bYawControl = true;
m_bShowShipStatus = true;
m_bShowFriendlyStatus = true;
m_bShowTurnStatus = true;
m_bShowContacts = true;
m_SelfDestruct = false;
m_SelfDestructConfirm = false;
m_SelfDestructTime = 0;
m_fUpdateButtons = 0;
m_pDoc = NULL;
}
//----------------------------------------------------------------------------
void ViewTactical::onActivate()
{
//{{BEGIN_DATA_INIT
DATA_MAP( m_pDetectedStatus, WindowButton, "DetectedStatus" );
m_pButtonPointDefense = WidgetCast<WindowButton>( window()->findNode( "ButtonPointDefense" ) );
m_pButtonShields = WidgetCast<WindowButton>( window()->findNode( "ButtonShields" ) );
m_pHeadingText = WidgetCast<WindowText>( window()->findNode( "HeadingText" ) );
m_pHeadingBar = WidgetCast<SliderHUD>( window()->findNode( "HeadingBar" ) );
m_pCargoButtons = WidgetCast<NodeWindow>( window()->findNode( "CargoButtons" ) );
m_pGadgetLayout = WidgetCast<NodeWindow>( window()->findNode( "GadgetLayout" ) );
m_pVelocityText = WidgetCast<WindowText>( window()->findNode( "VelocityText" ) );
m_pVelocityBar = WidgetCast<SliderHUD>( window()->findNode( "VelocityBar" ) );
//END_DATA_INIT}}
// grab the enhancement buttons and hide them
#ifdef _DEBUG
Settings settings( "ClientD" );
#else
Settings settings( "Client" );
#endif
// load settings
m_bYawControl = settings.get("YawControl", 1 ) != 0;
m_bShowShipStatus = settings.get("ShowShipStatus", 1 ) != 0;
m_bShowFriendlyStatus = settings.get("ShowFriendlyStatus", 1 ) != 0;
m_bShowTurnStatus = settings.get("ShowTurnStatus", 1 ) != 0;
m_bShowContacts = settings.get("ShowContacts", 1 ) != 0;
// get some pointers
m_pDoc = WidgetCast<GameDocument>( document() );
if (! m_pDoc )
return;
NounShip * pShip = m_pDoc->ship();
if (! pShip )
return;
// set the times for various updates
m_UpdateChatter = 0.0f;
m_UseGadgetTick = 0;
m_bShowContacts = true;
m_Contacts.release();
m_nDistressTick = m_pDoc->tick() + DISTRESS_MESSAGE_DELAY;
const Color DARK_GREY( 80, 80, 80, 255 );
// setup the velocity bar
m_pVelocityBar->setBaseColor( DARK_GREY );
m_pVelocityBar->setBarColor( DARK_YELLOW );
m_pHeadingBar->setBaseColor( DARK_GREY );
m_pHeadingBar->setBarColor( DARK_GREEN );
m_fYaw = pShip->yaw();
m_fYawV = 0.0f;
m_SetHeading = pShip->heading();
m_SetHeadingV = 0.0f;
m_SetVelocity = pShip->velocity();
m_SetVelocityV = 0.0f;
m_Adjust = false;
m_SelfDestruct = false;
m_SelfDestructConfirm = false;
m_SelfDestructTime = 0;
// hide detected indicator on activation..
m_pDetectedStatus->setVisible( false );
}
void ViewTactical::onDeactivate()
{
#ifdef _DEBUG
Settings settings( "ClientD" );
#else
Settings settings( "Client" );
#endif
onHide();
// save settings
settings.put("YawControl", m_bYawControl );
settings.put("ShowShipStatus", m_bShowShipStatus );
settings.put("ShowFriendlyStatus", m_bShowFriendlyStatus );
settings.put("ShowTurnStatus", m_bShowTurnStatus );
settings.put("ShowContacts", m_bShowContacts );
m_CameraLock = NULL;
m_Orbiting = NULL;
m_Contacts.release();
m_OldContacts.release();
m_pSelfDestructMessage = NULL;
}
const float SCROLL_LOW_MARGIN = 0.05f;
const float SCROLL_HIGH_MARGIN = 0.95f;
const float CAMERA_DOLLY_RATE = 10.0f; // how fast does the camera scroll
const float MIN_CAMERA_PITCH = -PI / 16;
const float MAX_CAMERA_PITCH = PI / 2;
const float MIN_CAMERA_ZOOM = 10.0f;
const float MAX_CAMERA_ZOOM = 200.0f;
const float CONTACT_RADIUS = 5000.0f;
const float DECAY_RATE = 3.0f;
const int CONTROL_LAG = 5;
const float CAMERA_SNAP_TIME = 1.0f; // how long does it take for the camera to snap fully into the tactical frame/position
void ViewTactical::onUpdate( float t )
{
GameContext * pContext = m_pDoc->context();
ASSERT( pContext );
NounShip * pShip = m_pDoc->ship();
if (! pShip )
return;
Noun * pFocus = m_pDoc->focus();
if (! pFocus )
return;
Noun * pTarget = m_pDoc->target();
//----------------------------------------------------------------------------
float targetZoom = Max( pFocus->radius(), 1.0f) * ZOOM_SCALE[ m_CameraZoomLevel ];
if ( targetZoom > 6000 )
targetZoom = 6000;
ASSERT( !_isnan( targetZoom ) );
m_CameraZoom += (targetZoom - m_CameraZoom) * t;
ASSERT( !_isnan( m_CameraZoom ) );
// reset the lock time any time the current target changes
if ( m_CameraLock != pTarget )
{
m_CameraLock = pTarget;
m_CameraLockTime = 0.0f;
}
m_CameraLockTime += t;
if ( m_bLeftDown )
m_fLeftDownTime += t;
if ( m_bRightDown )
m_fRightDownTime += t;
// calculate the snap strength, 1.0f is full snap, 0.0f is no snap
float snap = Min( m_CameraLockTime, CAMERA_SNAP_TIME ) / CAMERA_SNAP_TIME;
// destination camera frame
switch( m_CameraMode )
{
case FREE: // free camera mode
{
Vector3 cameraTarget( pFocus->worldPosition() );
sm_CameraFrame = Matrix33( m_CameraPitch, m_CameraYaw, 0 );
sm_CameraPosition = cameraTarget - (sm_CameraFrame.k * m_CameraZoom);
m_CameraLock = NULL;
}
break;
case TARGET: // this mode keeps the focus and target object always in view, keeping the focus at the center of the screen
{
Vector3 focusPosition( pFocus->worldPosition() );
if ( pTarget != NULL && pTarget->zoneParent() != pFocus )
{
Vector3 targetPosition( pTarget->worldPosition() );
Vector3 targetDirection( targetPosition - focusPosition );
targetDirection.normalize();
if ( pTarget->parent() == pFocus || (WidgetCast<NounDropTarget>( pTarget ) && ((NounDropTarget *)pTarget)->planet() == pFocus) )
targetDirection = -targetDirection;
// get the yaw towards the target
float targetYaw = atan2( targetDirection.x, targetDirection.z );
float yawDelta = AngleDelta( NormalizeAngle( m_CameraYaw), targetYaw );
m_CameraYaw += yawDelta * snap;
float targetPitch = -asin( targetDirection.y ) + (PI / 16);
float pitchDelta = AngleDelta( NormalizeAngle( m_CameraPitch), targetPitch );
m_CameraPitch += pitchDelta * snap;
}
sm_CameraFrame = Matrix33( m_CameraPitch, m_CameraYaw, 0 );
sm_CameraPosition = focusPosition - (sm_CameraFrame.k * m_CameraZoom);
}
break;
case TACTICAL: // this camera mode keeps the focus and target objects always in view, the camera will zoom in and out as needed
{
Vector3 focusPosition( pFocus->worldPosition() );
if ( pTarget != NULL && pTarget != pFocus )
{
Vector3 targetPosition( pTarget->worldPosition() );
Vector3 targetDirection( targetPosition - focusPosition );
float viewingArea = m_CameraZoom * 0.5f;
float targetDistance = targetDirection.magnitude();
if ( targetDistance < 1.0f )
targetDistance = 1.0f;
else if ( targetDistance > viewingArea )
targetDistance = viewingArea;
// normalize the target direction
targetDirection.normalize();
// pitch camera almost overhead
float targetPitch = PI / 4;
float pitchDelta = AngleDelta( NormalizeAngle( m_CameraPitch), targetPitch );
m_CameraPitch += pitchDelta * snap;
// rotate the camera towards the target
float targetYaw = atan2( targetDirection.x, targetDirection.z );
float yawDelta = AngleDelta( NormalizeAngle( m_CameraYaw), targetYaw );
m_CameraYaw += yawDelta * snap;
sm_CameraFrame = Matrix33( m_CameraPitch, m_CameraYaw, 0 );
// position the camera between the target and the focus point
Vector3 midPoint( (focusPosition + ((targetDirection * targetDistance) * 0.5f)) - (sm_CameraFrame.k * m_CameraZoom) );
Vector3 deltaPosition( midPoint - sm_CameraPosition );
sm_CameraPosition += deltaPosition * snap;
}
else
{
float targetPitch = PI / 4;
float pitchDelta = AngleDelta( NormalizeAngle( m_CameraPitch), targetPitch );
m_CameraPitch += pitchDelta * t;
sm_CameraFrame = Matrix33( m_CameraPitch, m_CameraYaw, 0 );
Vector3 midPoint( focusPosition - (sm_CameraFrame.k * m_CameraZoom) );
Vector3 deltaPosition( midPoint - sm_CameraPosition );
sm_CameraPosition += deltaPosition * snap;
}
}
break;
case FOLLOW: // this camera mode follows the focus object
{
Matrix33 focusFrame( pFocus->frame() );
float targetYaw = atan2( focusFrame.k.x, focusFrame.k.z );
float yawDelta = AngleDelta( NormalizeAngle( m_CameraYaw), targetYaw );
m_CameraYaw += yawDelta * t;
float targetPitch = PI / 16;
float pitchDelta = AngleDelta( NormalizeAngle( m_CameraPitch), targetPitch );
m_CameraPitch += pitchDelta * t;
Vector3 cameraTarget( pFocus->worldPosition() );
sm_CameraFrame = Matrix33( m_CameraPitch, m_CameraYaw, 0 );
sm_CameraPosition = cameraTarget - (sm_CameraFrame.k * m_CameraZoom);
}
break;
}
if ( GameDocument::s_fCamera < CAMERA_SNAP_TIME )
{
// if ship is moving, keep it from jumping around while the camera is animating
if ( m_Adjust )
{
Vector3 position( pFocus->worldPosition() );
Vector3 adjust( position - sm_AdjustPosition );
sm_AdjustPosition = position;
GameDocument::s_vCamera += adjust;
}
else
{
sm_AdjustPosition = pFocus->worldPosition();
m_Adjust = true;
}
float d = GameDocument::s_fCamera / CAMERA_SNAP_TIME;
// move the camera
Vector3 deltaPosition( sm_CameraPosition - GameDocument::s_vCamera );
GameDocument::s_vCamera += deltaPosition * d;
// update the camera frame
GameDocument::s_mCamera.i += (sm_CameraFrame.i - GameDocument::s_mCamera.i) * d;
GameDocument::s_mCamera.j += (sm_CameraFrame.j - GameDocument::s_mCamera.j) * d;
GameDocument::s_mCamera.k += (sm_CameraFrame.k - GameDocument::s_mCamera.k) * d;
GameDocument::s_mCamera.orthoNormalizeXY();
}
else
{
// animation is over, snap directly to the desired frame/position
GameDocument::s_mCamera = sm_CameraFrame;
GameDocument::s_vCamera = sm_CameraPosition;
m_Adjust = false;
}
//----------------------------------------------------------------------------
if (! m_BeginControl )
{
// hide the velocity and heading bar when in jumping or under autopilot
if ( pShip->command() != NounShip::NOCOMMAND ||
(pShip->jumpDrive() != NULL && (pShip->jumpDrive()->engaged() || pShip->jumpDrive()->jumping())) )
{
// ship is under AI control, make controls match the AI
m_SetVelocity = pShip->velocity();
m_SetVelocityV = 0.0f;
m_SetHeading = pShip->heading();
m_SetHeadingV = 0.0f;
m_fYaw = pShip->yaw();
m_fYawV = 0.0f;
}
}
if ( m_bYawControl )
{
// ship is being control by yaw only
if ( m_fYawV != 0.0f )
{
m_fYaw += m_fYawV * t;
if ( m_fYaw > 1.0f )
{
m_fYaw = 1.0f;
m_fYawV = 0.0f;
m_UpdateControl = true;
}
else if ( m_fYaw < -1.0f )
{
m_fYaw = -1.0f;
m_fYawV = 0.0f;
m_UpdateControl = true;
}
}
}
else
{
// ship is being controlled by absolute heading
if ( m_SetHeadingV != 0.0f )
m_SetHeading = NormalizeAngle( m_SetHeading + (m_SetHeadingV * t) );
}
if ( m_SetVelocityV != 0.0f )
{
// velocity is being changed
m_SetVelocity += m_SetVelocityV * t;
if ( m_SetVelocity > pShip->maxVelocity() )
{
m_SetVelocity = pShip->maxVelocity();
m_SetVelocityV = 0.0f;
m_UpdateControl = true;
}
else if ( m_SetVelocity < 0.0f )
{
m_SetVelocity = 0.0f;
m_SetVelocityV = 0.0f;
m_UpdateControl = true;
}
}
if ( m_UpdateControl )
{
// clear any command, if the ship has one
if ( pShip->command() != NounShip::NOCOMMAND )
{
pShip->verbCommand( NounShip::NOCOMMAND, NULL );
// set the command locally now, otherwise the code above will change the m_SetVelocity values
pShip->setCommand( NounShip::NOCOMMAND, NULL );
}
if ( m_bYawControl )
pShip->controlYawVelocity( m_fYaw, m_SetVelocity );
else
pShip->controlHeadingVelocity( m_SetHeading, m_SetVelocity );
m_UpdateControl = false;
m_BeginControl = false;
}
if (! m_pVelocityBar->sliding() )
m_pVelocityBar->setPosition( m_SetVelocity / pShip->maxVelocity() );
float normalizedVelocity = Clamp( pShip->velocity(), 0.0f, pShip->maxVelocity() );
m_pVelocityBar->setPosition2( normalizedVelocity / pShip->maxVelocity() );
m_pVelocityText->setText( CharString().format( "Velocity: %.1f / %.1f", normalizedVelocity, m_SetVelocity) );
if ( m_bYawControl )
{
if (! m_pHeadingBar->sliding() )
m_pHeadingBar->setPosition( 0.5f + (m_fYaw * 0.5f) );
float fNormHeading = NormalizeAngle( pShip->heading() );
m_pHeadingBar->setPosition2( (fNormHeading + PI) / PIX2 );
m_pHeadingText->setText( CharString().format("Heading: %.0f / %.0f", RadToDeg( fNormHeading ), RadToDeg( pShip->yaw() * pShip->maxYaw() ) ) );
}
else
{
float fNormSetHeading = NormalizeAngle( m_SetHeading );
if (! m_pHeadingBar->sliding() )
m_pHeadingBar->setPosition( ((fNormSetHeading + PI) / PIX2 ) );
float fNormHeading = NormalizeAngle( pShip->heading() );
m_pHeadingBar->setPosition2( (fNormHeading + PI) / PIX2 );
m_pHeadingText->setText( CharString().format("Heading: %.0f / %.0f", RadToDeg( fNormHeading ), RadToDeg( fNormSetHeading ) ) );
}
// update chatter timer, the sound is actually played in render()
m_UpdateChatter += t;
// update the targeting animation
m_TargetTime += t;
if ( m_Orbiting.pointer() != pShip->orbiting() )
{
// update our orbiting pointer
m_Orbiting = pShip->orbiting();
if ( pShip->inOrbit() )
m_SetVelocity = m_SetVelocityV = 0.0f;
}
//----------------------------------------------------------------------------
// update the list of clickable objects
m_fUpdateContacts += t;
if ( m_fUpdateContacts > TACTICAL_CONTACT_UPDATE )
updateContacts();
// handle self-destruct
if ( m_SelfDestruct && m_SelfDestructConfirm && pContext->tick() >= m_SelfDestructTime)
{
Verb::Ref( new VerbSelfDestruct( m_pDoc->ship() ) );
m_SelfDestruct = m_SelfDestructConfirm = false;
}
m_fUpdateButtons += t;
if ( m_fUpdateButtons > TACTICAL_UPDATE_BUTTONS )
{
m_fUpdateButtons = 0;
// show shield/PD toggle button only if ship has shields/beam weapons
bool hasShields = false;
bool hasAutoDefense = false;
for(int i=0;i<pShip->childCount();i++)
{
if ( WidgetCast<GadgetShield>( pShip->child(i) ) )
hasShields = true;
else if ( WidgetCast<GadgetBeamWeapon>( pShip->child(i) ) && !hasAutoDefense )
hasAutoDefense = WidgetCast<GadgetBeamWeapon>( pShip->child(i) )->allowPointDefense();
}
m_pButtonShields->showWindow( hasShields );
m_pButtonPointDefense->showWindow( hasAutoDefense );
}
// update detection indicator.. button alpha is used to indicate how hidden our ship is, the closer to 1.0
// in alpha the better we are hidden, as we get closer to being seen the button alpha is faded to 0.0
if ( (pShip->detectionFlags() & ~(1 << pShip->factionId())) == 0 )
{
// we are currently undetected by any other faction than our own, show the button and set it's alpha
// based on how close we are to being detected by nearby contacts that we can currently detect.
float fBestDetection = 1.0f;
for(int i=0;i<pShip->contactCount();++i)
{
NounGame * pContact = WidgetCast<NounGame>( pShip->contact(i) );
if (! pContact || pShip->isFriend( pContact ) )
continue; // skip null and friendly contacts..
float fDistance = (pShip->worldPosition() - pContact->worldPosition()).magnitude();
float fDetectRange = 0.0f;
if ( WidgetCast<NounShip>( pContact ) )
{
NounShip * pEnemyShip = (NounShip *)pContact;
float fVisibleRange = (pShip->radius() * pShip->visibility()) * pEnemyShip->view();
float fSensorRange = pShip->signature() * pEnemyShip->sensor();
fDetectRange = Max( fVisibleRange, fSensorRange );
}
else if ( WidgetCast<NounPlanet>( pContact ) )
{
NounPlanet * pEnemyPlanet = (NounPlanet *)pContact;
float fVisibleRange = (pShip->radius() * pShip->visibility()) * pEnemyPlanet->view();
float fSensorRange = pShip->signature() * pEnemyPlanet->sensor();
fDetectRange = Max( fVisibleRange, fSensorRange );
if (fDetectRange > PLANET_SCANNER_RANGE )
fDetectRange = PLANET_SCANNER_RANGE; // planets have a hard max detection range, ships go all the way out to 5k
}
float fFadeDistance = fDetectRange * 0.1f; // we want the indicator to fade for the last 10% of the range
float fOuterRange = fDetectRange + fFadeDistance;
if ( fDistance < fOuterRange )
{
if ( fDistance > fDetectRange )
{
float fDetect = Clamp( (fDistance - fDetectRange) / fFadeDistance, 0.0f, 1.0f );
if ( fDetect < fBestDetection )
fBestDetection = fDetect;
}
else
fBestDetection = -1.0f; // stealth is 0, ship should be detected..
}
}
if ( fBestDetection >= 0.0f )
{
m_pDetectedStatus->setTip( CharString().format( "Stealth at %d%%", (int)(fBestDetection * 100) ) );
m_pDetectedStatus->setAlpha( (fBestDetection * 0.75f) + 0.25f );
m_pDetectedStatus->setVisible( true );
}
else
m_pDetectedStatus->setVisible( false ); // detected, hide the indicator..
}
else
{
// we are detected, hide the detected indicator button..
m_pDetectedStatus->setVisible( false );
}
}
const float CAMERA_PITCH_RATE = PI / 1024;
const float CAMERA_YAW_RATE = PI / 1024;
const float CAMERA_ZOOM_RATE = 250.0f;
const float ORBIT_MASS = 100.0f; // a body must be atleast this mass for a ship to orbit
static bool FOG_ENABLE = false;
bool ViewTactical::onMessage( const Message & msg )
{
GameContext * pContext = m_pDoc->context();
ASSERT( pContext );
if ( m_pDoc == NULL || m_pDoc->ship() == NULL )
return false;
//{{BEGIN_MSG_MAP
MESSAGE_MAP( WB_BUTTONUP, 2084942820, onButtonSelfDestruct);
MESSAGE_MAP( WB_BUTTONUP, 3106761400, onButtonSendSupply);
MESSAGE_MAP( WB_BUTTONUP, 2084942692, onButtonSendDistress);
MESSAGE_MAP( WB_BUTTONUP, 3979261547, onButtonShields);
MESSAGE_MAP( WB_BUTTONUP, 3962397129, onButtonPointDefense);
MESSAGE_MAP( WS_END_SLIDE, 3648580978, onHeadingBar);
MESSAGE_MAP( WS_BEGIN_SLIDE, 3648580978, onHeadingBar);
MESSAGE_MAP( WS_END_SLIDE, 3350053706, onVelocityBar);
MESSAGE_MAP( WS_BEGIN_SLIDE, 3350053706, onVelocityBar);
//END_MSG_MAP}}
switch( msg.message )
{
case HM_MOUSEWHEEL:
if ( int( msg.wparam ) > 0 )
m_CameraZoomLevel = Max( m_CameraZoomLevel - 1, 0 );
else
m_CameraZoomLevel = Min( m_CameraZoomLevel + 1, ZOOM_LEVELS - 1 );
return true;
case HM_SYSKEYDOWN:
switch( msg.wparam )
{
#ifdef _DEBUG
case 'J':
if ( m_pDoc->target() != NULL )
{
Vector3 newPosition( m_pDoc->target()->worldPosition() );
newPosition.x += m_pDoc->ship()->radius() + m_pDoc->target()->radius() + 75.0f;
m_pDoc->ship()->setPosition( newPosition );
}
return true;
case 'U': // damage gadgets
{
for(int i=0;i<m_pDoc->ship()->childCount();i++)
if ( WidgetCast<NounGadget>( m_pDoc->ship()->child(i) ) )
((NounGadget *)m_pDoc->ship()->child(i))->inflictDamage( 0, NULL, 10, DAMAGE_EMP, Vector3( 0.0f) );
}
return true;
#endif
case 'Y':
m_bYawControl = !m_bYawControl;
m_pDoc->pushChat( CharString().format("<color;ffffff>Helm: Yaw control %s...</color>", m_bYawControl ? "ENABLED" : "DISABLED" ) );
break;
case 'C':
m_bShowContacts = !m_bShowContacts;
return true;
case 'F':
FOG_ENABLE = !FOG_ENABLE;
return true;
case 'R':
m_pDoc->setTarget( m_pDoc->ship() );
return true;
}
break;
case HM_KEYDOWN:
switch( msg.wparam )
{
case 'X':
m_CameraZoomLevel = Min( m_CameraZoomLevel + 1, ZOOM_LEVELS - 1 );
return true;
case 'Z':
m_CameraZoomLevel = Max( m_CameraZoomLevel - 1, 0 );
return true;
case HK_TAB: // next camera view
nextCameraMode();
return true;
case 191: // "/" character on keyboard
if ( m_bShowShipStatus && m_bShowTurnStatus && m_bShowFriendlyStatus )
{
m_bShowShipStatus = false;
m_bShowFriendlyStatus = false;
m_bShowTurnStatus = false;
m_pDoc->pushChat( "<color;ffffff>Tactical: Tactical HUD, Helm Assistance, and ATIL disabled...</color>" );
}
else if ( m_bShowTurnStatus )
{
m_bShowFriendlyStatus = true;
m_pDoc->pushChat( "<color;ffffff>Tactical: Allied Tactical Information Link engaged...</color>" );
}
else if ( m_bShowShipStatus )
{
m_bShowTurnStatus = true;
m_pDoc->pushChat( "<color;ffffff>Tactical: Helm Assistance engaged...</color>" );
}
else
{
m_bShowShipStatus = true;
m_pDoc->pushChat( "<color;ffffff>Tactical: Tactical HUD engaged...</color>" );
}
return true;
case HK_UP:
case 'W':
if ( m_SetVelocity < m_pDoc->ship()->maxVelocity() )
{
m_BeginControl = true;
m_SetVelocityV = SET_VELOCITY_RATE;
}
return true;
case HK_DOWN:
case 'Q':
if ( m_SetVelocity > 0.0f )
{
m_BeginControl = true;
m_SetVelocityV = -SET_VELOCITY_RATE;
}
return true;
case 'S':
if ( m_bYawControl )
{
m_fYaw = m_fYawV = 0.0f;
m_BeginControl = true;
m_UpdateControl = true;
}
else
{
m_SetHeadingV = 0.0f;
m_SetHeading = m_pDoc->ship()->heading();
m_BeginControl = true;
m_UpdateControl = true;
}
return true;
case HK_LEFT:
if ( m_bYawControl )
{
if ( m_yawState != YawStatePressed::Left && m_yawState != YawStatePressed::Both )
{
if (m_yawState == YawStatePressed::Right)
m_yawState = YawStatePressed::Both;
else
m_yawState = YawStatePressed::Left;
m_BeginControl = true;
m_UpdateControl = true;
m_fYaw = -1.0f;
}
}
else
{
m_BeginControl = true;
m_SetHeadingV = -SET_HEADING_RATE;
}
return true;
case 'A':
if ( m_bYawControl )
{
if ( m_yawState != YawStatePressed::Left && m_yawState != YawStatePressed::Both )
{
m_BeginControl = true;
if (m_yawState == YawStatePressed::Right)
m_yawState = YawStatePressed::Both;
else
m_yawState = YawStatePressed::Left;
m_fYawV = -SET_YAW_RATE;
if ( m_fYaw > 0.0f )
m_fYaw = 0.0f;
}
}
else
{
m_BeginControl = true;
m_SetHeadingV = -SET_HEADING_RATE;
}
return true;
case HK_RIGHT:
if ( m_bYawControl )
{
if ( m_yawState != YawStatePressed::Right && m_yawState != YawStatePressed::Both )
{
if (m_yawState == YawStatePressed::Left)
m_yawState = YawStatePressed::Both;
else
m_yawState = YawStatePressed::Right;
m_BeginControl = true;
m_UpdateControl = true;
m_fYaw = 1.0f;
}
}
else
{
m_BeginControl = true;
m_SetHeadingV = SET_HEADING_RATE;
}
return true;
case 'D':
if ( m_bYawControl )
{
if ( m_yawState != YawStatePressed::Right && m_yawState != YawStatePressed::Both )
{
m_BeginControl = true;
if (m_yawState == YawStatePressed::Left)
m_yawState = YawStatePressed::Both;
else
m_yawState = YawStatePressed::Right;
m_fYawV = SET_YAW_RATE;
if ( m_fYaw < 0.0f )
m_fYaw = 0.0f;
}
}
else
{
m_BeginControl = true;
m_SetHeadingV = SET_HEADING_RATE;
}
return true;
case 219: // '[' decrease scanner range
if ( m_ScannerRange > 1000.0f )
m_ScannerRange -= 1000.0f;
return true;
case 221: // ']' increase scanner range
if ( m_ScannerRange < 100000.0f )
m_ScannerRange += 1000.0f;
return true;
case HK_SHIFT:
ViewGame::sm_pViewGame->updateAction( false );
break;
}
return false;
case HM_KEYUP:
switch( msg.wparam & 0xff )
{
case HK_SHIFT:
ViewGame::sm_pViewGame->updateAction( false );
return true;
case 'X':
case 'Z':
case HK_TAB:
return true;
case HK_UP:
case 'W':
case HK_DOWN:
case 'Q':
if ( m_SetVelocityV != 0.0f )
{
m_SetVelocityV = 0.0f;
m_UpdateControl = true;
}
return true;
case HK_LEFT:
if (m_yawState == YawStatePressed::Both) {
m_yawState = YawStatePressed::Right;
m_fYaw = 1.0f;
m_UpdateControl = true;
}
else if (m_yawState == YawStatePressed::Left) {
m_yawState = YawStatePressed::None;
m_fYaw = m_fYawV = 0.0f;
m_UpdateControl = true;
}
return true;
case HK_RIGHT:
if (m_yawState == YawStatePressed::Both) {
m_yawState = YawStatePressed::Left;
m_fYaw = -1.0f;
m_UpdateControl = true;
}
else if (m_yawState == YawStatePressed::Right) {
m_yawState = YawStatePressed::None;
m_fYaw = m_fYawV = 0.0f;
m_UpdateControl = true;
}
return true;
case 'A':
if (m_bYawControl) {
if (m_yawState == YawStatePressed::Both) {
m_yawState = YawStatePressed::Right;
m_fYawV = 1.0f;
m_UpdateControl = true;
}
else if (m_yawState == YawStatePressed::Left) {
m_yawState = YawStatePressed::None;
m_fYawV = 0.0f;
m_UpdateControl = true;
}
}
else
{
if ( m_SetHeadingV != 0.0f )
{
m_SetHeadingV = 0.0f;
m_UpdateControl = true;
}
}
return true;
case 'D':
if ( m_bYawControl )
{
if (m_yawState == YawStatePressed::Both) {
m_yawState = YawStatePressed::Left;
m_fYawV = -1.0f;
m_UpdateControl = true;
}
else if (m_yawState == YawStatePressed::Right) {
m_yawState = YawStatePressed::None;
m_fYawV = 0.0f;
m_UpdateControl = true;
}
}
else
{
if ( m_SetHeadingV != 0.0f )
{
m_SetHeadingV = 0.0f;
m_UpdateControl = true;
}
}
return true;
}
return false;
}
return false;
}
void ViewTactical::onDocumentUpdate()
{
// document data has changed, update this view if needed
}
void ViewTactical::onHide()
{
if ( m_bRightDown || m_bLeftDown )
{
setCursorState( POINTER );
m_bRightDown = m_bLeftDown = false;
}
//stop selfdestruct abuse
m_SelfDestruct = false;
m_SelfDestructConfirm = false;
m_SelfDestructTime = 0;
}
//----------------------------------------------------------------------------
const float FRONT_PLANE = 1.0f;
const float BACK_PLANE = 5000000.0f;