-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathunit.cpp
More file actions
5360 lines (4854 loc) · 248 KB
/
Copy pathunit.cpp
File metadata and controls
5360 lines (4854 loc) · 248 KB
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
//
// Copyright 2020 Electronic Arts Inc.
//
// TiberianDawn.DLL and RedAlert.dll and corresponding source code 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 3 of the License, or (at your option) any later version.
// TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
// in the hope that it will be useful, but with permitted additional restrictions
// under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
// distributed with this program. You should have received a copy of the
// GNU General Public License along with permitted additional restrictions
// with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
/* $Header: /CounterStrike/UNIT.CPP 1 3/03/97 10:26a Joe_bostic $ */
/***********************************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* File Name : UNIT.CPP *
* *
* Programmer : Joe L. Bostic *
* *
* Start Date : September 10, 1993 *
* *
* Last Update : November 3, 1996 [JLB] *
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* Recoil_Adjust -- Adjust pixel values in direction specified. *
* UnitClass::AI -- AI processing for the unit. *
* UnitClass::APC_Close_Door -- Closes an APC door. *
* UnitClass::APC_Open_Door -- Opens an APC door. *
* UnitClass::Active_Click_With -- Intercepts the active click to see if deployment is possib*
* UnitClass::Active_Click_With -- Performs specified action on specified cell. *
* UnitClass::Approach_Target -- Handles approaching the target in order to attack it. *
* UnitClass::Assign_Destination -- Assign a destination to a unit. *
* UnitClass::Blocking_Object -- Determines how a object blocks a unit *
* UnitClass::Can_Enter_Cell -- Determines cell entry legality. *
* UnitClass::Can_Fire -- Determines if turret can fire upon target. *
* UnitClass::Click_With -- Handles player map clicking while this unit is selected. *
* UnitClass::Credit_Load -- Fetch the full credit value of cargo carried. *
* UnitClass::Crew_Type -- Fetches the kind of crew that this object produces. *
* UnitClass::Debug_Dump -- Displays the status of the unit to the mono monitor. *
* UnitClass::Desired_Load_Dir -- Determines the best cell and facing for loading. *
* UnitClass::Draw_It -- Draws a unit object. *
* UnitClass::Edge_Of_World_AI -- Check for falling off the edge of the world. *
* UnitClass::Enter_Idle_Mode -- Unit enters idle mode state. *
* UnitClass::Fire_Direction -- Determines the direction of firing. *
* UnitClass::Firing_AI -- Handle firing logic for this unit. *
* UnitClass::Flag_Attach -- Attaches a house flag to this unit. *
* UnitClass::Flag_Remove -- Removes the house flag from this unit. *
* UnitClass::Goto_Clear_Spot -- Finds a clear spot to deploy. *
* UnitClass::Goto_Tiberium -- Search for and head toward nearest available Tiberium patch. *
* UnitClass::Greatest_Threat -- Fetches the greatest threat for this unit. *
* UnitClass::Harvesting -- Harvests tiberium at the current location. *
* UnitClass::Init -- Clears all units for scenario preparation. *
* UnitClass::Limbo -- Limbo this unit. *
* UnitClass::Mission_Guard -- Special guard mission override processor. *
* UnitClass::Mission_Guard_Area -- Guard area logic for units. *
* UnitClass::Mission_Harvest -- Handles the harvesting process used by harvesters. *
* UnitClass::Mission_Hunt -- This is the AI process for aggressive enemy units. *
* UnitClass::Mission_Move -- Handles special move mission overrides. *
* UnitClass::Mission_Repair -- Handles finding and proceeding on a repair mission. *
* UnitClass::Mission_Unload -- Handles unloading cargo. *
* UnitClass::Offload_Tiberium_Bail -- Offloads one Tiberium quantum from the object. *
* UnitClass::Ok_To_Move -- Queries whether the vehicle can move. *
* UnitClass::Overlap_List -- Determines overlap list for units. *
* UnitClass::Overrun_Square -- Handles vehicle overrun of a cell. *
* UnitClass::Per_Cell_Process -- Performs operations necessary on a per cell basis. *
* UnitClass::Pip_Count -- Fetches the number of pips to display on unit. *
* UnitClass::Random_Animate -- Handles random idle animation for the unit. *
* UnitClass::Read_INI -- Reads units from scenario INI file. *
* UnitClass::Receive_Message -- Handles receiving a radio message. *
* UnitClass::Reload_AI -- Perform reload logic for this unit. *
* UnitClass::Rotation_AI -- Process any turret or body rotation. *
* UnitClass::Scatter -- Causes the unit to scatter to a nearby location. *
* UnitClass::Set_Speed -- Initiate unit movement physics. *
* UnitClass::Shape_Number -- Fetch the shape number to use for this unit. *
* UnitClass::Should_Crush_It -- Determines if this unit should crush an object. *
* UnitClass::Sort_Y -- Give Y coordinate sort value for unit. *
* UnitClass::Start_Driver -- Starts driving and reserves destination cell. *
* UnitClass::Take_Damage -- Inflicts damage points on a unit. *
* UnitClass::Tiberium_Check -- Search for and head toward nearest available Tiberium patch. *
* UnitClass::Tiberium_Load -- Determine the Tiberium load as a percentage. *
* UnitClass::Try_To_Deploy -- The unit attempts to "deploy" at current location. *
* UnitClass::UnitClass -- Constructor for units. *
* UnitClass::Unlimbo -- Removes unit from stasis. *
* UnitClass::What_Action -- Determines action to perform on specified cell. *
* UnitClass::What_Action -- Determines what action would occur if clicked on object. *
* UnitClass::Write_INI -- Store the units to the INI database. *
* UnitClass::delete -- Deletion operator for units. *
* UnitClass::new -- Allocate a unit slot and adjust access arrays. *
* UnitClass::~UnitClass -- Destructor for unit objects. *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "function.h"
#include "keyframe.h"
#include "common/miscasm.h"
extern void Logic_Switch_Player_Context(ObjectClass* object);
extern void Logic_Switch_Player_Context(HouseClass* object);
extern void On_Special_Weapon_Targetting(const HouseClass* player_ptr, SpecialWeaponType weapon_type);
#ifdef REMASTER_BUILD
extern bool Is_Legacy_Render_Enabled(void);
#else
#define Is_Legacy_Render_Enabled() true
#endif
static int _GapShroudXTable[] = {-1, 0, 1, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0,
1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -1, 0, 1};
static int _GapShroudYTable[] = {-3, -3, -3, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3};
/***********************************************************************************************
* Recoil_Adjust -- Adjust pixel values in direction specified. *
* *
* This is a helper routine that modifies the pixel coordinates provided according to the *
* direction specified. The effect is the simulate recoil effects by moving an object 'back'*
* one pixel. Since the pixels moved depend on facing, this routine handles the pixel *
* adjustment quickly. *
* *
* INPUT: dir -- The direction to base the recoil on. *
* *
* x,y -- References to the pixel coordinates that will be adjusted. *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/08/1995 JLB : Created. *
*=============================================================================================*/
void Recoil_Adjust(DirType dir, int& x, int& y)
{
static struct
{
signed char X, Y;
} _adjust[32] = {{0, 1}, // N
{0, 1}, {0, 1}, {-1, 1}, {-1, 1}, // NE
{-1, 1}, {-1, 0}, {-1, 0}, {-1, 0}, // E
{-1, 0}, {-1, -1}, {-1, -1}, {-1, -1}, // SE
{-1, -1}, {-1, -1}, {0, -1}, {0, -1}, // S
{0, -1}, {0, -1}, {1, -1}, {1, -1}, // SW
{1, -1}, {1, 0}, {1, 0}, {1, 0}, // W
{1, 0}, {1, 1}, {1, 1}, {1, 1}, // NW
{1, 1}, {0, 1}, {0, 1}};
int index = Dir_To_32(dir);
x += _adjust[index].X;
y += _adjust[index].Y;
}
/***********************************************************************************************
* UnitClass::new -- Allocate a unit slot and adjust access arrays. *
* *
* This routine will allocate a unit from the available unit pool and *
* fixup all the access lists to match. It will allocate a unit slot *
* from within the range allowed for the specified unit type. If no *
* slot was found, then it will fail. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with a pointer to the allocated unit. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 04/11/1994 JLB : Created. *
* 04/21/1994 JLB : Converted to operator new. *
*=============================================================================================*/
void* UnitClass::operator new(size_t) noexcept
{
void* ptr = Units.Alloc();
if (ptr != NULL) {
((UnitClass*)ptr)->Set_Active();
}
return (ptr);
}
/***********************************************************************************************
* UnitClass::delete -- Deletion operator for units. *
* *
* This removes the unit from the local allocation system. Since this *
* is a fixed block of memory, not much has to be done to delete the *
* unit. Merely marking it as inactive is enough. *
* *
* INPUT: ptr -- Pointer to the unit to delete. *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 04/21/1994 JLB : Created. *
*=============================================================================================*/
void UnitClass::operator delete(void* ptr)
{
if (ptr != NULL) {
((UnitClass*)ptr)->IsActive = false;
}
Units.Free((UnitClass*)ptr);
}
/***********************************************************************************************
* UnitClass::~UnitClass -- Destructor for unit objects. *
* *
* This destructor will lower the unit count for the owning house as well as inform any *
* other units in communication, that this unit is about to leave reality. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 08/15/1994 JLB : Created. *
*=============================================================================================*/
UnitClass::~UnitClass(void)
{
if (GameActive && Class.Is_Valid()) {
/*
** Remove this member from any team it may be associated with. This must occur at the
** top most level of the inheritance hierarchy because it may call virtual functions.
*/
if (Team.Is_Valid()) {
Team->Remove(this);
Team = NULL;
}
House->Tracking_Remove(this);
/*
** If there are any cargo members, delete them.
*/
while (Is_Something_Attached()) {
delete Detach_Object();
}
Limbo();
}
ID = -1;
}
/***********************************************************************************************
* UnitClass::UnitClass -- Constructor for units. *
* *
* This constructor for units will initialize the unit into the game *
* system. It will be placed in all necessary tracking lists. The initial condition will *
* be in a state of limbo. *
* *
* INPUT: classid -- The type of unit to create. *
* *
* house -- The house owner of this unit. *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 04/21/1994 JLB : Created. *
*=============================================================================================*/
UnitClass::UnitClass(UnitType classid, HousesType house)
: DriveClass(RTTI_UNIT, Units.ID(this), house)
, Class(UnitTypes.Ptr((int)classid))
, Flagged(HOUSE_NONE)
, IsDumping(false)
, Gems(0)
, Gold(0)
, Tiberium(0)
, IsToScatter(false)
, ShroudBits(0xFFFFFFFFUL)
, ShroudCenter(0)
, Reload(0)
, SecondaryFacing(PrimaryFacing)
, TiberiumUnloadRefinery(TARGET_NONE)
{
Reload = 0;
House->Tracking_Add(this);
Ammo = Class->MaxAmmo;
IsCloakable = Class->IsCloakable;
if (Class->IsAnimating)
Set_Rate(Options.Normalize_Delay(3));
/*
** For two shooters, clear out the second shot flag -- it will be set the first time
** the object fires. For non two shooters, set the flag since it will never be cleared
** and the second shot flag tells the system that normal rearm times apply -- this is
** what is desired for non two shooters.
*/
IsSecondShot = !Class->Is_Two_Shooter();
Strength = Class->MaxStrength;
/*
** Keep count of the number of units created.
*/
// if (Session.Type == GAME_INTERNET) {
// House->UnitTotals->Increment_Unit_Total((int)classid);
// }
}
#ifdef CHEAT_KEYS
/***********************************************************************************************
* UnitClass::Debug_Dump -- Displays the status of the unit to the mono monitor. *
* *
* This displays the current status of the unit class to the mono monitor. By this display *
* bugs may be tracked down or prevented. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 06/02/1994 JLB : Created. *
*=============================================================================================*/
void UnitClass::Debug_Dump(MonoClass* mono) const
{
assert(Units.ID(this) == ID);
assert(IsActive);
mono->Set_Cursor(0, 0);
mono->Print(Text_String(TXT_DEBUG_VEHICLE));
mono->Set_Cursor(47, 5);
mono->Printf("%02X:%02X", SecondaryFacing.Current(), SecondaryFacing.Desired());
mono->Set_Cursor(1, 11);
mono->Printf("%03", Gems);
mono->Set_Cursor(7, 11);
mono->Printf("%03", Gold);
mono->Fill_Attrib(66, 13, 12, 1, IsDumping ? MonoClass::INVERSE : MonoClass::NORMAL);
DriveClass::Debug_Dump(mono);
}
#endif
/***********************************************************************************************
* UnitClass::Sort_Y -- Give Y coordinate sort value for unit. *
* *
* This routine is used by the rendering system in order to sort the *
* game objects in a back to front order. This is now the correct *
* overlap effect is achieved. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with a coordinate value that can be used for sorting. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/17/1994 JLB : Created. *
*=============================================================================================*/
COORDINATE UnitClass::Sort_Y(void) const
{
assert(Units.ID(this) == ID);
assert(IsActive);
return (Coord_Add(Coord, 0x00800000L));
}
/***********************************************************************************************
* UnitClass::AI -- AI processing for the unit. *
* *
* This routine will perform the AI processing necessary for the unit. These are non- *
* graphic related operations. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/31/1994 JLB : Created. *
*=============================================================================================*/
void UnitClass::AI(void)
{
assert(Units.ID(this) == ID);
assert(IsActive);
/*
** Act on new orders if the unit is at a good position to do so.
*/
if (Height == 0 && !IsDumping && !IsDriving && Is_Door_Closed() /*&& Mission != MISSION_UNLOAD*/) {
// if (MissionQueue == MISSION_NONE) Enter_Idle_Mode();
Commence();
}
DriveClass::AI();
if (!IsActive || Height > 0) {
return;
}
/*
** Hack check to ensure that a harvester won't harvest if it is not harvesting.
*/
if (Mission != MISSION_HARVEST) {
IsHarvesting = false;
}
/*
** Handle combat logic for this unit. It will determine if it has a target and
** if so, if conditions are favorable for firing. When conditions permit, the
** unit will fire upon its target.
*/
Firing_AI();
#ifdef FIXIT_CSII // checked - ajw 9/28/98
if (!IsActive) {
return;
}
#endif
/*
** Turret rotation processing. Handles rotating radar dish
** as well as conventional turrets if present. If no turret present, but
** it decides that the body should face its target, then body rotation
** would occur by this process as well.
*/
Rotation_AI();
/*
** Scatter units off buildings in guard modes.
*/
if (!IsTethered && !IsFiring && !IsDriving && !IsRotating
&& (Mission == MISSION_GUARD || Mission == MISSION_GUARD_AREA) && MissionQueue == MISSION_NONE
&& Map[Coord].Cell_Building() != NULL) {
Scatter(0, true, true);
}
/*
** Delete this unit if it finds itself off the edge of the map and it is in
** guard or other static mission mode.
*/
if (Edge_Of_World_AI()) {
return;
}
/*
** Units will reload every so often if they are under the burden of
** being required to reload between shots.
*/
Reload_AI();
/*
** Transporters require special logic handled here since there isn't a MISSION_WAIT_FOR_PASSENGERS
** mission that they can follow. Passenger loading is merely a part of their normal operation.
*/
if (Class->Max_Passengers() > 0) {
/*
** Double check that there is a passenger that is trying to load or unload.
** If not, then close the door.
*/
if (!Is_Door_Closed() && Mission != MISSION_UNLOAD && Transmit_Message(RADIO_TRYING_TO_LOAD) != RADIO_ROGER) {
APC_Close_Door();
}
}
/*
** Don't start a new mission unless the vehicle is in the center of
** a cell (not driving) and the door (if any) is closed.
*/
if (!IsDumping && !IsDriving && Is_Door_Closed() /*&& Mission != MISSION_UNLOAD*/) {
Commence();
}
/*
** A cloaked object that is carrying the flag will always shimmer.
*/
if (Cloak == CLOAKED && Flagged != HOUSE_NONE) {
Do_Shimmer();
}
/*
** Mobile gap generators regenerate their gap every so often (just in case).
*/
if (Class->IsGapper && !IsDriving && (Frame % TICKS_PER_SECOND) == 0) {
Shroud_Regen();
}
}
/***********************************************************************************************
* UnitClass::Rotation_AI -- Process any turret or body rotation. *
* *
* This routine will handle the rotation logic for the unit's turret (if it has one) as *
* well as its normal body shape. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 07/30/1996 JLB : Created. *
*=============================================================================================*/
void UnitClass::Rotation_AI(void)
{
if (Target_Legal(TarCom) && !IsRotating) {
DirType dir = Direction(TarCom);
if (Class->IsTurretEquipped) {
SecondaryFacing.Set_Desired(dir);
} else {
/*
** Non turret equipped vehicles will rotate their body to face the target only
** if the vehicle isn't currently moving or facing the correct direction. This
** applies only to tracked vehicles. Wheeled vehicles never rotate to face the
** target, since they aren't maneuverable enough.
*/
if ((Class->Speed == SPEED_TRACK /* || *this == UNIT_BIKE */) && !Target_Legal(NavCom) && !IsDriving
&& PrimaryFacing.Difference(dir)) {
PrimaryFacing.Set_Desired(dir);
}
}
}
if (Class->IsRadarEquipped) {
Mark(MARK_CHANGE_REDRAW);
SecondaryFacing.Set((DirType)(SecondaryFacing.Current() + 8));
Mark(MARK_CHANGE_REDRAW);
} else {
IsRotating = false;
if (Class->IsTurretEquipped) {
if (IsTurretLockedDown) {
SecondaryFacing.Set_Desired(PrimaryFacing.Current());
}
if (SecondaryFacing.Is_Rotating()) {
Mark(MARK_CHANGE_REDRAW);
if (SecondaryFacing.Rotation_Adjust(Class->ROT + 1)) {
Mark(MARK_CHANGE_REDRAW);
}
/*
** If no further rotation is necessary, flag that the rotation
** has stopped.
*/
if (!Class->IsRadarEquipped) {
IsRotating = SecondaryFacing.Is_Rotating();
}
} else {
if (!IsTurretLockedDown && !Target_Legal(TarCom)) {
if (!Target_Legal(NavCom)) {
SecondaryFacing.Set_Desired(PrimaryFacing.Current());
} else {
SecondaryFacing.Set_Desired(Direction(NavCom));
}
}
}
}
}
}
/***********************************************************************************************
* UnitClass::Edge_Of_World_AI -- Check for falling off the edge of the world. *
* *
* When a unit leaves the map it will be eliminated. This routine checks for this case *
* and eliminates the unit accordingly. *
* *
* INPUT: none *
* *
* OUTPUT: bool; Was the unit eliminated by this routine? *
* *
* WARNINGS: Be sure to check for the return value and if 'true' abort any further processing*
* of the unit since it is dead. Only call this routine once per unit per *
* game logic loop. *
* *
* HISTORY: *
* 07/30/1996 JLB : Created. *
*=============================================================================================*/
bool UnitClass::Edge_Of_World_AI(void)
{
if (Mission == MISSION_GUARD && !Map.In_Radar(Coord_Cell(Coord)) && IsLocked) {
if (Team.Is_Valid())
Team->IsLeaveMap = true;
Stun();
delete this;
return (true);
}
return (false);
}
/***********************************************************************************************
* UnitClass::Reload_AI -- Perform reload logic for this unit. *
* *
* Some units require special reload logic. The V2 rocket launcher in particular. Perform *
* this reload logic with this routine. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: Only call this routine once per unit per game logic loop. *
* *
* HISTORY: *
* 07/30/1996 JLB : Created. *
*=============================================================================================*/
void UnitClass::Reload_AI(void)
{
if (*this == UNIT_V2_LAUNCHER && Ammo < Class->MaxAmmo) {
if (IsDriving) {
Reload = Reload + 1;
} else {
if (Reload == 0) {
Ammo++;
if (Ammo < Class->MaxAmmo) {
Reload = TICKS_PER_SECOND * 30;
}
Mark(MARK_CHANGE);
}
}
}
}
/***********************************************************************************************
* UnitClass::Firing_AI -- Handle firing logic for this unit. *
* *
* This routine wil check for and perform any firing logic required of this unit. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: This should be called only once per unit per game logic loop. *
* *
* HISTORY: *
* 07/30/1996 JLB : Created. *
*=============================================================================================*/
void UnitClass::Firing_AI(void)
{
if (Target_Legal(TarCom) && Class->PrimaryWeapon != NULL) {
/*
** Determine which weapon can fire. First check for the primary weapon. If that weapon
** cannot fire, then check any secondary weapon. If neither weapon can fire, then the
** failure code returned is that from the primary weapon.
*/
int primary = What_Weapon_Should_I_Use(TarCom);
FireErrorType ok = Can_Fire(TarCom, primary);
switch (ok) {
case FIRE_OK:
if (!((UnitClass*)this)->Class->IsFireAnim) {
Mark(MARK_OVERLAP_UP);
IsFiring = false;
Mark(MARK_OVERLAP_DOWN);
}
Fire_At(TarCom, primary);
break;
case FIRE_FACING:
#ifdef FIXIT_CSII // checked - ajw 9/28/98
if (Class->IsLockTurret || Class->Type == UNIT_DEMOTRUCK) {
#else
if (Class->IsLockTurret) {
#endif
if (!Target_Legal(NavCom) && !IsDriving) {
PrimaryFacing.Set_Desired(Direction(TarCom));
SecondaryFacing.Set_Desired(PrimaryFacing.Desired());
}
} else {
SecondaryFacing.Set_Desired(Direction(TarCom));
}
break;
case FIRE_CLOAKED:
Mark(MARK_OVERLAP_UP);
IsFiring = false;
Mark(MARK_OVERLAP_DOWN);
Do_Uncloak();
break;
}
}
}
/***********************************************************************************************
* UnitClass::Receive_Message -- Handles receiving a radio message. *
* *
* This is the handler function for when a unit receives a radio *
* message. Typical use of this is when a unit unloads from a hover *
* class so that clearing of the transport is successful. *
* *
* INPUT: from -- Pointer to the originator of the message. *
* *
* message -- The radio message received. *
* *
* param -- Reference to an optional parameter the might be needed to return *
* information back to the originator of the message. *
* *
* OUTPUT: Returns with the radio message response. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/22/1994 JLB : Created. *
*=============================================================================================*/
RadioMessageType UnitClass::Receive_Message(RadioClass* from, RadioMessageType message, int& param)
{
assert(Units.ID(this) == ID);
assert(IsActive);
switch (message) {
/*
** Checks to see if this object is in need of service depot processing.
*/
case RADIO_NEED_REPAIR:
if (!IsDriving && !Target_Legal(NavCom)
&& (Health_Ratio() >= 1 && (*this != UNIT_MINELAYER || Ammo >= Class->MaxAmmo)))
return (RADIO_NEGATIVE);
break;
// return(RADIO_ROGER);
/*
** Asks if the passenger can load on this transport.
*/
case RADIO_CAN_LOAD:
if (Class->Max_Passengers() == 0 || from == NULL || !House->Is_Ally(from->Owner()))
return (RADIO_STATIC);
if (How_Many() < Class->Max_Passengers()) {
return (RADIO_ROGER);
}
return (RADIO_NEGATIVE);
/*
** The refinery has told this harvester that it should begin the backup procedure
** so that proper unloading may take place.
*/
case RADIO_BACKUP_NOW:
DriveClass::Receive_Message(from, message, param);
if (!IsRotating && PrimaryFacing != DIR_W) {
Do_Turn(DIR_W);
} else {
if (!IsDriving) {
TechnoClass* whom = Contact_With_Whom();
if (IsTethered && whom != NULL) {
if (whom->What_Am_I() == RTTI_BUILDING && Mission == MISSION_ENTER) {
if (Transmit_Message(RADIO_IM_IN, whom) == RADIO_ROGER) {
Transmit_Message(RADIO_UNLOADED, whom);
}
}
}
}
}
return (RADIO_ROGER);
/*
** This message is sent by the passenger when it determines that it has
** entered the transport.
*/
case RADIO_IM_IN:
if (How_Many() == Class->Max_Passengers()) {
APC_Close_Door();
}
return (RADIO_ATTACH);
/*
** Docking maintenance message received. Check to see if new orders should be given
** to the impatient unit.
*/
case RADIO_DOCKING:
/*
** If this transport is moving, then always abort the docking request.
*/
if (IsDriving || Target_Legal(NavCom)) {
return (RADIO_NEGATIVE);
}
/*
** Check for the case of a docking message arriving from a unit that does not
** have formal radio contact established. This might be a unit that is standing
** by. If this transport is free to proceed with normal docking operation, then
** establish formal contact now. If the transport is completely full, then break
** off contact. In all other cases, just tell the pending unit to stand by.
*/
if (Contact_With_Whom() != from) {
/*
** Can't ever load up so tell the passenger to bug off.
*/
if (How_Many() >= Class->Max_Passengers()) {
return (RADIO_NEGATIVE);
}
/*
** Establish contact and let the loading process proceed normally.
*/
if (!In_Radio_Contact()) {
Transmit_Message(RADIO_HELLO, from);
} else {
/*
** This causes the potential passenger to think that all is ok and to
** hold on for a bit.
*/
return (RADIO_ROGER);
}
}
if (Class->Max_Passengers() > 0 && How_Many() < Class->Max_Passengers()) {
DriveClass::Receive_Message(from, message, param);
if (!IsDriving && !IsRotating && !IsTethered) {
/*
** If the potential passenger needs someplace to go, then figure out a good
** spot and tell it to go.
*/
if (Transmit_Message(RADIO_NEED_TO_MOVE, from) == RADIO_ROGER) {
CELL cell;
DirType dir = Desired_Load_Dir(from, cell);
/*
** If no adjacent free cells are detected, then passenger loading
** cannot occur. Break radio contact.
*/
if (cell == 0) {
Transmit_Message(RADIO_OVER_OUT, from);
} else {
param = ::As_Target(cell);
Do_Turn(dir);
/*
** If it is now facing the correct direction, then open the
** transport doors. Close the doors if the transport is or needs
** to rotate.
*/
#ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
if (*this == UNIT_APC || *this == UNIT_PHASE) {
#else
if (*this == UNIT_APC) {
#endif
if (IsRotating) {
if (!Is_Door_Closed()) {
APC_Close_Door();
}
} else {
if (!Is_Door_Open()) {
APC_Open_Door();
}
}
}
/*
** Tell the potential passenger where it should go. If the passenger is
** already at the staging location, then tell it to move onto the transport
** directly.
*/
if (Transmit_Message(RADIO_MOVE_HERE, param, from) == RADIO_YEA_NOW_WHAT) {
#ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
if ((*this != UNIT_APC && *this != UNIT_PHASE) || Is_Door_Open()) {
#else
if (*this != UNIT_APC || Is_Door_Open()) {
#endif
param = As_Target();
Transmit_Message(RADIO_TETHER);
if (Transmit_Message(RADIO_MOVE_HERE, param, from) != RADIO_ROGER) {
Transmit_Message(RADIO_OVER_OUT, from);
} else {
Contact_With_Whom()->Unselect();
}
}
}
}
}
}
return (RADIO_ROGER);
}
break;
/*
** Something bad has happened to the object in contact with. Abort any coordinated
** activity with this object. Basically, ... run away! Run away!
*/
case RADIO_RUN_AWAY:
if (Class->IsToHarvest && In_Radio_Contact() && Mission == MISSION_ENTER) {
TechnoClass* contact = Contact_With_Whom();
if (contact->What_Am_I() == RTTI_BUILDING && *((BuildingClass*)contact) == STRUCT_REFINERY) {
// Slight hack; set a target so the harvest mission knows to skip to finding home state
Assign_Mission(MISSION_HARVEST);
TarCom = As_Target();
return (RADIO_ROGER);
}
}
return (DriveClass::Receive_Message(from, message, param));
/*
** When this message is received, it means that the other object
** has already turned its radio off. Turn this radio off as well.
*/
case RADIO_OVER_OUT:
if (Mission == MISSION_RETURN) {
Assign_Mission(MISSION_GUARD);
}
DriveClass::Receive_Message(from, message, param);
return (RADIO_ROGER);
}
return (DriveClass::Receive_Message(from, message, param));
}
/***********************************************************************************************
* UnitClass::Unlimbo -- Removes unit from stasis. *
* *
* This routine will place a unit into the game and out of its limbo *
* state. This occurs whenever a unit is unloaded from a transport. *
* *
* INPUT: coord -- The coordinate to make the unit appear. *
* *
* dir -- The initial facing to impart upon the unit. *
* *
* OUTPUT: bool; Was the unit unlimboed successfully? If the desired *
* coordinate is illegal, then this might very well return *
* false. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/22/1994 JLB : Created. *
*=============================================================================================*/
bool UnitClass::Unlimbo(COORDINATE coord, DirType dir)
{
assert(Units.ID(this) == ID);
assert(IsActive);
/*
** All units must start out facing one of the 8 major directions.
*/
dir = Facing_Dir(Dir_Facing(dir));
if (DriveClass::Unlimbo(coord, dir)) {
SecondaryFacing = dir;
/*
** Ensure that the owning house knows about the
** new object.
*/
House->UScan |= (1L << Class->Type);
House->ActiveUScan |= (1L << Class->Type);
/*
** If it starts off the edge of the map, then it already starts cloaked.
*/
if (IsCloakable && !IsLocked)
Cloak = CLOAKED;
/*
** Units default to no special animation.
*/
Set_Rate(0);
Set_Stage(0);
return (true);
}
return (false);
}
/***********************************************************************************************
* UnitClass::Take_Damage -- Inflicts damage points on a unit. *
* *
* This routine will inflict the specified number of damage points on *
* the given unit. If the unit is destroyed, then this routine will *
* remove the unit cleanly from the game. The return value indicates *
* whether the unit was destroyed. This will allow appropriate death *
* animation or whatever. *
* *
* INPUT: damage-- The number of damage points to inflict. *
* *
* distance -- The distance from the damage center point to the object's center point.*
* *
* warhead--The type of damage to inflict. *
* *
* source -- Who is responsible for this damage? *
* *
* OUTPUT: Returns the result of the damage process. This can range from RESULT_NONE up to *
* RESULT_DESTROYED. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/30/1991 JLB : Created. *
* 07/12/1991 JLB : Script initiated by unit destruction. *
* 04/15/1994 JLB : Converted to member function. *
* 04/16/1994 JLB : Warhead modifier. *
* 06/03/1994 JLB : Added the source of the damage target value. *
* 06/20/1994 JLB : Source is a base class pointer. *
* 11/22/1994 JLB : Shares base damage handler for techno objects. *
* 06/30/1995 JLB : Lasers do maximum damage against gunboat. *
* 08/16/1995 JLB : Harvester crushing doesn't occur on early missions. *
*=============================================================================================*/
ResultType UnitClass::Take_Damage(int& damage, int distance, WarheadType warhead, TechnoClass* source, bool forced)
{
assert(Units.ID(this) == ID);
assert(IsActive);