forked from electronicarts/CnC_Tiberian_Dawn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUNIT.CPP
4051 lines (3648 loc) · 173 KB
/
UNIT.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
/*
** Command & Conquer(tm)
** Copyright 2025 Electronic Arts Inc.
**
** 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/* $Header: F:\projects\c&c\vcs\code\unit.cpv 2.17 16 Oct 1995 16:48:28 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 : August 16, 1995 [JLB] *
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* Recoil_Adjust -- Adjust pixel values in direction specified. *
* Turret_Adjust -- Turret adjustment routine for MLRS and MSAM units. *
* 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::As_Target -- Returns the unit as a target value. *
* UnitClass::Blocking_Object -- Determines how a object blocks a unit *
* UnitClass::Can_Enter_Building -- Determines building entry legality. *
* UnitClass::Can_Fire -- Determines if this unit can fire. *
* UnitClass::Can_Player_Move -- Determines if the player is legally allowed to move it. *
* UnitClass::Click_With -- Handles player map clicking while this unit is selected. *
* 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::Enter_Idle_Mode -- Unit enters idle mode state. *
* UnitClass::Find_LZ -- Maintenance function for transport units. *
* 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::Harvesting -- Harvests tiberium at the current location. *
* UnitClass::Init -- Clears all units for scenario preparation. *
* UnitClass::Limbo -- Prepares vehicle and then limbos it. *
* UnitClass::Look -- Perform map revelation from a unit's position. *
* UnitClass::Mission_Attack -- Handles the mission attack logic. *
* UnitClass::Mission_Guard -- Special guard mission override processor. *
* 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_Unload -- Handles unloading cargo. *
* UnitClass::Overlap_List -- Determines overlap list for units. *
* UnitClass::Per_Cell_Process -- Performs operations necessary on a per cell basis. *
* UnitClass::Pip_Count -- Fetchs 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::Remap_Table -- Fetches the remap table to use for this object. *
* UnitClass::Response_Attack -- Voice feedback when ordering the unit to attack a target. *
* UnitClass::Response_Move -- Voice feedback when ordering the unit to move. *
* UnitClass::Response_Select -- Voice feedback when selecting the unit. *
* UnitClass::Scatter -- Causes the unit to travel to a nearby safe cell. *
* UnitClass::Set_Speed -- Initiate unit movement physics. *
* UnitClass::Sort_Y -- Give Y coordinate sort value for unit. *
* UnitClass::Start_Driver -- Starts driving and reserves destination cell. *
* UnitClass::Stop_Driver -- Handles removing occupation bits when driving stops. *
* UnitClass::Stun -- Stuns the unit in preparation for unit removal. *
* UnitClass::Take_Damage -- Inflicts damage points on a unit. *
* UnitClass::Target_Coord -- The coordinate to use when targeting this unit. *
* UnitClass::Try_To_Deploy -- The unit attempts to "deploy" at current location. *
* UnitClass::UnitClass -- Constructor for units. *
* UnitClass::Unlimbo -- Removes unit from stasis. *
* UnitClass::Unload_Hovercraft_Process -- Handles unloading hovercraft transport. *
* UnitClass::Validate -- validates unit pointer. *
* UnitClass::What_Action -- Determines what action would occur if clicked on object. *
* UnitClass::What_Am_I -- Returns with the RTTI type this object is. *
* UnitClass::Write_INI -- Writes all the units out to an INI file. *
* 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"
/*
** This contains the value of the Virtual Function Table Pointer
*/
void * UnitClass::VTable;
/***********************************************************************************************
* UnitClass::Validate -- validates unit pointer. *
* *
* INPUT: *
* none. *
* *
* OUTPUT: *
* 1 = ok, 0 = error *
* *
* WARNINGS: *
* none. *
* *
* HISTORY: *
* 08/09/1995 BRR : Created. *
*=============================================================================================*/
#ifdef CHEAT_KEYS
int UnitClass::Validate(void) const
{
int num;
num = Units.ID(this);
if (num < 0 || num >= UNIT_MAX) {
Validate_Error("UNIT");
return (0);
}
else
return (1);
}
#else
#define Validate()
#endif
/***********************************************************************************************
* 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 = Facing_To_32(dir);
x += _adjust[index].X;
y += _adjust[index].Y;
}
/***********************************************************************************************
* Turret_Adjust -- Turret adjustment routine for MLRS and MSAM units. *
* *
* This routine adjusts the pixel coordinates specified to account for the displacement of *
* the turret on the MLRS and MSAM vehicles. *
* *
* INPUT: dir -- The direction of the body of the vehicle. *
* *
* x,y -- References to the turret center pixel position. These will be modified as *
* necessary. *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/08/1995 JLB : Created. *
*=============================================================================================*/
void Turret_Adjust(DirType dir, int &x, int &y)
{
static struct {
signed char X,Y;
} _adjust[32] = {
{1,2}, // N
{-1,1},
{-2,0},
{-3,0},
{-3,1}, // NW
{-4,-1},
{-4,-1},
{-5,-2},
{-5,-3}, // W
{-5,-3},
{-3,-3},
{-3,-4},
{-3,-4}, // SW
{-3,-5},
{-2,-5},
{-1,-5},
{0,-5}, // S
{1,-6},
{2,-5},
{3,-5},
{4,-5}, // SE
{6,-4},
{6,-3},
{6,-3},
{6,-3}, // E
{5,-1},
{5,-1},
{4,0},
{3,0}, // NE
{2,0},
{2,1},
{1,2}
};
int index = Facing_To_32(dir);
x += _adjust[index].X;
y += _adjust[index].Y;
}
/***********************************************************************************************
* UnitClass::As_Target -- Returns the unit as a target value. *
* *
* This routine will convert the unit into a target value that is then returned. Target *
* values are typically used for navigation and other computer uses. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with target value of unit. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 09/19/1994 JLB : Created. *
*=============================================================================================*/
TARGET UnitClass::As_Target(void) const
{
Validate();
return(Build_Target(KIND_UNIT, Units.ID(this)));
}
#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
{
Validate();
mono->Set_Cursor(0, 0);
mono->Print(
"ÚName:ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂMission:ÄÄÄÂTarCom:ÂNavCom:ÂRadio:ÂCoord:ÄÄÂHeadTo:ÄÂSt:Ä¿\n"
"³ ³ ³ ³ ³ ³ ³ ³ ³\n"
"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂNÂYÂHealth:ÄÂBody:ÂTurret:ÂSpeed:ÂPath:ÁÄÄÄÄÄÄÂCargo:ÄÄÄÄÁÄÄÄÄ´\n"
"³Active........³ ³ ³ ³ ³ ³ ³ ³ ³\n"
"³Limbo.........³ ³ ÃÄÄÄÄÄÄÄÄÁÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´\n"
"³Owned.........³ ³ ³Last Message: ³\n"
"³Discovered....³ ³ ÃTimer:ÂArm:ÂTrack:ÂTiberium:ÂFlash:ÂStage:ÂTeam:ÄÄÄÄÂArch:´\n"
"³Selected......³ ³ ³ ³ ³ ³ ³ ³ ³ ³ ³\n"
"³Teathered.....³ ³ ÃÄÄÄÄÄÄÁÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÙ\n"
"³Locked on Map.³ ³ ³ \n"
"³Turret Locked.³ ³ ³ \n"
"³Is A Loaner...³ ³ ³ \n"
"³Deploying.....³ ³ ³ \n"
"³Rotating......³ ³ ³ \n"
"³Firing........³ ³ ³ \n"
"³Driving.......³ ³ ³ \n"
"³To Look.......³ ³ ³ \n"
"³Recoiling.....³ ³ ³ \n"
"³To Display....³ ³ ³ \n"
"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÁÄÙ \n");
mono->Set_Cursor(1, 1);mono->Printf("%s:%s", House->Class->IniName, Class->IniName);
CargoClass::Debug_Dump(mono);
MissionClass::Debug_Dump(mono);
TarComClass::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
{
Validate();
if (IsTethered && *this == UNIT_HOVER) {
return(Coord_Add(Coord, 0xFF800000L));
}
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)
{
Validate();
/*
** Act on new orders if the unit is at a good position to do so.
*/
if (!IsDriving && Is_Door_Closed() /*Mission != MISSION_UNLOAD*/) {
Commence();
}
TarComClass::AI();
/*
** Delete this unit if it finds itself off the edge of the map and it is in
** guard or other static mission mode.
*/
if (!Team && Mission == MISSION_GUARD && !Map.In_Radar(Coord_Cell(Coord))) {
Stun();
delete this;
return;
}
/*
** Rocket launchers will reload every so often.
*/
if (*this == UNIT_MSAM && Ammo < Class->MaxAmmo) {
if (IsDriving) {
Reload = Reload + 1;
} else {
if (Reload.Expired()) {
Ammo++;
if (Ammo < Class->MaxAmmo) {
Reload = TICKS_PER_SECOND*30;
}
Mark(MARK_CHANGE);
}
}
}
/*
** Hover landers always are flagged to redraw since they don't record themselves
** on the map in the normal fashion.
*/
if (*this == UNIT_HOVER) {
// Mark_For_Redraw();
//if (IsDown) Mono_Printf("*");
Mark(MARK_CHANGE);
}
/*
** If this is a vehicle that heals itself (e.g., Mammoth Tank), then it will perform
** the heal logic here.
*/
if (*this == UNIT_HTANK && (Frame % 16) == 0 && Health_Ratio() < 0x0080) {
Strength++;
Mark(MARK_CHANGE);
}
if (*this == UNIT_VICE && Map[Coord_Cell(Coord)].Land_Type() == LAND_TIBERIUM && Health_Ratio() < 0x0100 && (Frame % 16) == 0) {
Strength++;
Mark(MARK_CHANGE);
}
/*
** 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->IsTransporter) {
/*
** 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 (!IsDriving && Is_Door_Closed()/*&& Mission != MISSION_UNLOAD*/) {
Commence();
}
/*
** Handle recoil recovery here.
*/
if (IsInRecoilState) {
IsInRecoilState = false;
Mark(MARK_CHANGE);
}
/*
** For animating objects such as Visceroids, max-out their animation
** stages here
*/
if (Class->IsAnimating) {
if (!Fetch_Rate()) Set_Rate(2);
StageClass::Graphic_Logic();
if (Fetch_Stage() >= Get_Build_Frame_Count(Class->Get_Image_Data())-1) {
Set_Stage(0);
}
}
/*
** for Jurassic objects, animate them if they're walking
*/
if (Class->IsPieceOfEight && Special.IsJurassic && AreThingiesEnabled) {
// Only animate if they're walking
if (IsDriving || IsFiring) {
if (!Fetch_Rate()) {
Set_Rate(Options.Normalize_Delay(2));
Set_Stage(0);
}
StageClass::Graphic_Logic();
if (Fetch_Stage() >= ( (IsDriving || *this == UNIT_TREX || *this == UNIT_RAPT) ? 8 : 12) ) {
Set_Stage(0);
if (IsFiring) {
Set_Rate(0);
IsFiring = false;
}
}
}
}
/*
** A cloaked object that is carrying the flag will always shimmer.
*/
if (Cloak == CLOAKED && Flagged != HOUSE_NONE) {
Do_Shimmer();
}
}
/***********************************************************************************************
* UnitClass::Can_Fire -- Determines if this unit can fire. *
* *
* INPUT: target -- The target that is desired to fire upon. *
* *
* which -- The weapon (primary=0, secondary=1) to fire. *
* *
* OUTPUT: Returns with the fire error number if it cannot fire or else FIRE_OK. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 05/08/1995 JLB : Created. *
*=============================================================================================*/
FireErrorType UnitClass::Can_Fire(TARGET target, int which) const
{
Validate();
FireErrorType cf;
cf = TarComClass::Can_Fire(target, which);
if (cf == FIRE_OK) {
/*
** If it's a dinosaur, when it's OK to fire we should start the firing
** animation, but wait for the proper attack stage before starting the
** bullet, so things won't die prematurely.
*/
if (Class->IsFireAnim) {
if (!IsFiring) {
UnitClass *nonconst;
nonconst = (UnitClass *)this;
nonconst->Set_Rate(Options.Normalize_Delay(2));
nonconst->Set_Stage(0);
IsFiring = true;
cf = FIRE_BUSY;
} else {
if (Fetch_Stage() < 4) cf = FIRE_BUSY;
}
}
}
return(cf);
}
/***********************************************************************************************
* 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, long & param)
{
Validate();
switch (message) {
/*
** Asks if the passenger can load on this transport.
*/
case RADIO_CAN_LOAD:
if (Class->IsTransporter && How_Many() < Class->Max_Passengers() && from && House->Class->House == from->Owner()) {
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:
TarComClass::Receive_Message(from, message, param);
if (!IsRotating && PrimaryFacing != DIR_SW) {
Do_Turn(DIR_SW);
} else {
if (!IsDriving) {
Force_Track(BACKUP_INTO_REFINERY, Adjacent_Cell(Center_Coord(), FACING_N));
Set_Speed(128);
}
}
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 (Class->IsTransporter && *this == UNIT_APC && How_Many() < Class->Max_Passengers()) {
TarComClass::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 = (long)::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.
*/
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) {
if (Is_Door_Open()) {
param = (long)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;
/*
** 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);
}
TarComClass::Receive_Message(from, message, param);
return(RADIO_ROGER);
}
return(TarComClass::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)
{
Validate();
/*
** All units must start out facing one of the 8 major directions.
*/
dir = Facing_Dir(Dir_Facing(dir));
if (TarComClass::Unlimbo(coord, 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);
/*
** A gun boat and a hover craft are allowed to exit the map thus we
** flag them so they can. This undoes the work of Techno::Unlimbo which
** stole their IsALoaner flag.
*/
if (*this == UNIT_GUNBOAT || *this == UNIT_HOVER) {
IsALoaner = true;
}
/*
** Start the gunboat animating when it is unlimboed.
*/
if (*this == UNIT_GUNBOAT) {
Set_Rate(2);
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)
{
Validate();
ResultType res = RESULT_NONE;
/*
** Special case: If this is a laser attacking a gunboat, then the gunboat is ALWAYS toasted.
*/
if (*this == UNIT_GUNBOAT && warhead == WARHEAD_LASER) {
damage = Strength*3;
}
/*
** Remember if this object was selected. If it was and it gets destroyed and it has
** passengers that pop out, then the passengers will inherit the select state.
*/
bool select = (IsSelected && IsOwnedByPlayer);
/*
** In order for a this to be damaged, it must either be a unit
** with a crew or a sandworm.
*/
res = TarComClass::Take_Damage(damage, distance, warhead, source);
if (res == RESULT_DESTROYED) {
Death_Announcement(source);
if (Class->Explosion != ANIM_NONE) {
AnimType anim = Class->Explosion;
/*
** SSM launchers will really explode big if they are carrying
** missiles at the time of the explosion.
*/
if (*this == UNIT_MSAM && Ammo) {
anim = ANIM_NAPALM3;
}
if (*this == UNIT_TRIC || *this == UNIT_TREX || *this == UNIT_RAPT || *this == UNIT_STEG) {
Sound_Effect(VOC_DINODIE1, Coord);
}
new AnimClass(anim, Coord);
/*
** When the flame tank blows up, it really blows up. It is
** equivalent to a napalm strike.
*/
if (Class->Explosion == ANIM_NAPALM3) {
Explosion_Damage(Coord, 200, source, WARHEAD_FIRE);
}
/*
** Very strong units that have an explosion will also rock the
** screen when they are destroyed.
*/
if (Class->MaxStrength > 400) {
Shake_Screen(3);
}
}
/*
** Possibly have the crew member run away.
*/
Mark(MARK_UP);
if (Class->IsCrew && !Class->IsTransporter) {
if (Random_Pick(0, 1) == 0) {
InfantryClass * i = 0;
if (Class->Primary == WEAPON_NONE) {
i = new InfantryClass(INFANTRY_C1, House->Class->House);
i->IsTechnician = true;
} else {
i = new InfantryClass(INFANTRY_E1, House->Class->House);
}
if (i) {
if (i->Unlimbo(Coord, DIR_N)) {
i->Strength = Random_Pick(5, (int)i->Class->MaxStrength/2);
i->Scatter(0, true);
if (!House->IsHuman) {
i->Assign_Mission(MISSION_HUNT);
} else {
i->Assign_Mission(MISSION_GUARD);
}
if (select) i->Select();
} else {
delete i;
}
}
}
} else {
if (*this != UNIT_HOVER) {
while (Is_Something_Attached()) {
FootClass * object = Detach_Object();
if (!object) break; // How can this happen?
/*
** Only infantry can run from a destroyed vehicle. Even then, it is not a sure
** thing.
*/
if (object->Is_Infantry() && object->Unlimbo(Coord, DIR_N)) {
// object->Strength = Random_Pick(5, (int)((InfantryClass*)object)->Class->MaxStrength/2);
object->Scatter(0, true);
if (select) object->Select();
} else {
object->Record_The_Kill(source);
delete object;
}
}
} else {
Kill_Cargo(source);
}
}
/*
** When the mobile head quarters blows up, the entire side blows up.
*/
if (*this == UNIT_MHQ) {
House->Flag_To_Die();
}
/*
** Finally, delete the vehicle.
*/
delete this;
} else {
/*
** When damaged and below half strength, start smoking if
** it isn't already smoking.
*/
if (Health_Ratio() < 0x0080 && !IsAnimAttached && *this != UNIT_VICE && *this != UNIT_STEG && *this != UNIT_TREX && *this != UNIT_TRIC && *this != UNIT_RAPT) {
AnimClass * anim = new AnimClass(ANIM_SMOKE_M, Coord_Add(Coord, XYP_Coord(0, -8)));
if (anim) anim->Attach_To(this);
}
/*
** If at half damage, then start smoking or burning as appropriate.
*/
if (res == RESULT_HALF) {
if (*this == UNIT_GUNBOAT) {
AnimClass * anim = new AnimClass(ANIM_ON_FIRE_MED, Coord_Add(Coord, XYP_Coord(Random_Pick(0, 16)-8, -2)));
if (anim) anim->Attach_To(this);
}
}
/*
** Try to crush anyone that fires on this unit if possible. The harvester
** typically is the only one that will qualify here.
*/
if (!Team && source && !IsTethered && !House->Is_Ally(source) && (!House->IsHuman || Special.IsSmartDefense)) {
/*
** Try to crush the attacker if it can be crushed by this unit and this unit is
** not equipped with a flame type weapon. If this unit has a weapon and the target
** is not very close, then fire on it instead. In easy mode, they never run over the
** player. In hard mode, they always do. In normal mode, they only overrun past
** mission #8.
*/
if ((Class->Primary == WEAPON_NONE || (Distance(source) < 0x0180 && BulletTypeClass::As_Reference(Weapons[Class->Primary].Fires).Warhead != WARHEAD_FIRE)) &&
(GameToPlay != GAME_NORMAL ||
*this != UNIT_HARVESTER ||
BuildLevel > 8 ||
Special.IsDifficult) &&
!Special.IsEasy &&
Class->IsCrusher && source->Is_Techno() && ((TechnoTypeClass const &)source->Class_Of()).IsCrushable) {
Assign_Destination(source->As_Target());
Assign_Mission(MISSION_MOVE);
} else {
/*
** Try to return to base if possible.
*/
if (*this == UNIT_HARVESTER && Pip_Count() && Health_Ratio() < 0x0080) {
/*
** Find nearby refinery and head to it?
*/
BuildingClass * building = Find_Docking_Bay(STRUCT_REFINERY, false);
/*
** Since the refinery said it was ok to load, establish radio
** contact with the refinery and then await docking orders.
*/
if (building && Transmit_Message(RADIO_HELLO, building) == RADIO_ROGER) {
Assign_Mission(MISSION_ENTER);
}
}
}
}
/*
** Computer controlled harvester will radio for help if they are attacked.
*/