forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroid.cpp
3728 lines (3160 loc) · 103 KB
/
droid.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
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2012 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file droid.c
*
* Droid method functions.
*
*/
#include "lib/framework/frame.h"
#include "lib/framework/math_ext.h"
#include "lib/framework/geometry.h"
#include "lib/framework/strres.h"
#include "lib/gamelib/gtime.h"
#include "lib/gamelib/animobj.h"
#include "lib/ivis_opengl/piematrix.h"
#include "lib/ivis_opengl/screen.h"
#include "lib/framework/fixedpoint.h"
#include "lib/script/script.h"
#include "lib/sound/audio.h"
#include "lib/sound/audio_id.h"
#include "lib/netplay/netplay.h"
#include "objects.h"
#include "loop.h"
#include "visibility.h"
#include "map.h"
#include "drive.h"
#include "droid.h"
#include "hci.h"
#include "game.h"
#include "power.h"
#include "miscimd.h"
#include "effects.h"
#include "feature.h"
#include "action.h"
#include "order.h"
#include "move.h"
#include "anim_id.h"
#include "geometry.h"
#include "display.h"
#include "console.h"
#include "component.h"
#include "function.h"
#include "lighting.h"
#include "multiplay.h"
#include "warcam.h"
#include "display3d.h"
#include "group.h"
#include "text.h"
#include "scripttabs.h"
#include "scriptcb.h"
#include "cmddroid.h"
#include "fpath.h"
#include "mapgrid.h"
#include "projectile.h"
#include "cluster.h"
#include "mission.h"
#include "levels.h"
#include "transporter.h"
#include "selection.h"
#include "difficulty.h"
#include "edit3d.h"
#include "scores.h"
#include "research.h"
#include "combat.h"
#include "scriptfuncs.h" //for ThreatInRange()
#include "template.h"
#include "qtscript.h"
#define DEFAULT_RECOIL_TIME (GAME_TICKS_PER_SEC/4)
#define DROID_DAMAGE_SPREAD (16 - rand()%32)
#define DROID_REPAIR_SPREAD (20 - rand()%40)
// store the experience of recently recycled droids
UWORD aDroidExperience[MAX_PLAYERS][MAX_RECYCLED_DROIDS];
UDWORD selectedGroup = UBYTE_MAX;
UDWORD selectedCommander = UBYTE_MAX;
/* default droid design template */
extern DROID_TEMPLATE sDefaultDesignTemplate;
/** Height the transporter hovers at above the terrain. */
#define TRANSPORTER_HOVER_HEIGHT 10
// the structure that was last hit
DROID *psLastDroidHit;
//determines the best IMD to draw for the droid - A TEMP MEASURE!
void groupConsoleInformOfSelection( UDWORD groupNumber );
void groupConsoleInformOfCreation( UDWORD groupNumber );
void groupConsoleInformOfCentering( UDWORD groupNumber );
void cancelBuild(DROID *psDroid)
{
if (orderDroidList(psDroid))
{
objTrace(psDroid->id, "Droid build order cancelled - changing to next order");
}
else
{
objTrace(psDroid->id, "Droid build order cancelled");
psDroid->action = DACTION_NONE;
psDroid->order = DroidOrder(DORDER_NONE);
setDroidActionTarget(psDroid, NULL, 0);
/* Notify scripts we just became idle */
psScrCBOrderDroid = psDroid;
psScrCBOrder = psDroid->order.type;
eventFireCallbackTrigger((TRIGGER_TYPE)CALL_DROID_REACH_LOCATION);
psScrCBOrderDroid = NULL;
psScrCBOrder = DORDER_NONE;
triggerEventDroidIdle(psDroid);
}
}
// initialise droid module
bool droidInit(void)
{
memset(aDroidExperience, 0, sizeof(UWORD) * MAX_PLAYERS * MAX_RECYCLED_DROIDS);
psLastDroidHit = NULL;
return true;
}
#define UNIT_LOST_DELAY (5*GAME_TICKS_PER_SEC)
/* Deals damage to a droid
* \param psDroid droid to deal damage to
* \param damage amount of damage to deal
* \param weaponClass the class of the weapon that deals the damage
* \param weaponSubClass the subclass of the weapon that deals the damage
* \param angle angle of impact (from the damage dealing projectile in relation to this droid)
* \return > 0 when the dealt damage destroys the droid, < 0 when the droid survives
*
* NOTE: This function will damage but _never_ destroy transports when in single player (campaign) mode
*/
int32_t droidDamage(DROID *psDroid, unsigned damage, WEAPON_CLASS weaponClass, WEAPON_SUBCLASS weaponSubClass, unsigned impactTime, bool isDamagePerSecond)
{
int32_t relativeDamage;
CHECK_DROID(psDroid);
// VTOLs on the ground take triple damage
if (isVtolDroid(psDroid) && psDroid->sMove.Status == MOVEINACTIVE)
{
damage *= 3;
}
relativeDamage = objDamage(psDroid, damage, psDroid->originalBody, weaponClass, weaponSubClass, isDamagePerSecond);
if (relativeDamage > 0)
{
// reset the attack level
if (secondaryGetState(psDroid, DSO_ATTACK_LEVEL) == DSS_ALEV_ATTACKED)
{
secondarySetState(psDroid, DSO_ATTACK_LEVEL, DSS_ALEV_ALWAYS);
}
// Now check for auto return on droid's secondary orders (i.e. return on medium/heavy damage)
secondaryCheckDamageLevel(psDroid);
if (!bMultiPlayer)
{
// Now check for scripted run-away based on health left
orderHealthCheck(psDroid);
}
CHECK_DROID(psDroid);
}
else if (relativeDamage < 0)
{
// HACK: Prevent transporters from being destroyed in single player
// FIXME: When we fix campaign scripts to use DROID_SUPERTRANSPORTER
if ((game.type == CAMPAIGN) && !bMultiPlayer && (psDroid->droidType == DROID_TRANSPORTER))
{
debug(LOG_ATTACK, "Transport(%d) saved from death--since it should never die (SP only)", psDroid->id);
psDroid->body = 1;
return 0;
}
// Droid destroyed
debug(LOG_ATTACK, "droid (%d): DESTROYED", psDroid->id);
// Deal with score increase/decrease and messages to the player
if( psDroid->player == selectedPlayer)
{
CONPRINTF(ConsoleString,(ConsoleString, _("Unit Lost!")));
scoreUpdateVar(WD_UNITS_LOST);
audio_QueueTrackMinDelayPos(ID_SOUND_UNIT_DESTROYED,UNIT_LOST_DELAY,
psDroid->pos.x, psDroid->pos.y, psDroid->pos.z );
}
else
{
scoreUpdateVar(WD_UNITS_KILLED);
}
// If this is droid is a person and was destroyed by flames,
// show it nicely by burning him/her to death.
if (psDroid->droidType == DROID_PERSON && weaponClass == WC_HEAT)
{
droidBurn(psDroid);
}
// Otherwise use the default destruction animation
else
{
debug(LOG_DEATH, "Droid %d (%p) is toast", (int)psDroid->id, psDroid);
// This should be sent even if multi messages are turned off, as the group message that was
// sent won't contain the destroyed droid
if (bMultiPlayer && !bMultiMessages)
{
bMultiMessages = true;
destroyDroid(psDroid, impactTime);
bMultiMessages = false;
}
else
{
destroyDroid(psDroid, impactTime);
}
}
}
return relativeDamage;
}
// Check that psVictimDroid is not referred to by any other object in the game. We can dump out some
// extra data in debug builds that help track down sources of dangling pointer errors.
#ifdef DEBUG
#define DROIDREF(func, line) "Illegal reference to droid from %s line %d", func, line
#else
#define DROIDREF(func, line) "Illegal reference to droid"
#endif
bool droidCheckReferences(DROID *psVictimDroid)
{
for (int plr = 0; plr < MAX_PLAYERS; plr++)
{
for (STRUCTURE *psStruct = apsStructLists[plr]; psStruct != NULL; psStruct = psStruct->psNext)
{
for (int i = 0; i < psStruct->numWeaps; i++)
{
ASSERT_OR_RETURN(false, (DROID *)psStruct->psTarget[i] != psVictimDroid, DROIDREF(psStruct->targetFunc[i], psStruct->targetLine[i]));
}
}
for (DROID *psDroid = apsDroidLists[plr]; psDroid != NULL; psDroid = psDroid->psNext)
{
if (psDroid->order.psObj == psVictimDroid)
{
debug(LOG_DEATH, "Death cleanup is postponed for this unit %p, (id %d name %s), droid %p (id %d name %s) still has orders concerning it!", psVictimDroid, psVictimDroid->id, psVictimDroid->aName,
psDroid, psDroid->id, psDroid->aName);
debug(LOG_DEATH, DROIDREF(psDroid->targetFunc, psDroid->targetLine));
return false;
}
for (int i = 0; i < psDroid->numWeaps; i++)
{
if (psDroid->psActionTarget[i] == psVictimDroid)
{
debug(LOG_DEATH, "Death cleanup is postponed for this unit %p, (id %d name %s), droid %p (id %d name %s) still has actions concerning it!", psVictimDroid, psVictimDroid->id, psVictimDroid->aName,
psDroid, psDroid->id, psDroid->aName);
debug(LOG_DEATH, DROIDREF(psDroid->actionTargetFunc[i], psDroid->actionTargetLine[i]));
return false;
}
}
}
}
return true;
}
#undef DROIDREF
DROID::DROID(uint32_t id, unsigned player)
: BASE_OBJECT(OBJ_DROID, id, player)
, droidType(DROID_ANY)
, psGroup(NULL)
, psGrpNext(NULL)
, secondaryOrder(DSS_ARANGE_DEFAULT | DSS_REPLEV_NEVER | DSS_ALEV_ALWAYS | DSS_HALT_GUARD)
, secondaryOrderPending(DSS_ARANGE_DEFAULT | DSS_REPLEV_NEVER | DSS_ALEV_ALWAYS | DSS_HALT_GUARD)
, secondaryOrderPendingCount(0)
, action(DACTION_NONE)
, actionPos(0, 0)
, psCurAnim(NULL)
{
order.type = DORDER_NONE;
order.pos = Vector2i(0, 0);
order.pos2 = Vector2i(0, 0);
order.direction = 0;
order.psObj = NULL;
order.psStats = NULL;
sMove.asPath = NULL;
sMove.Status = MOVEINACTIVE;
lastFrustratedTime = -UINT16_MAX; // make sure we do not start the game frustrated
listSize = 0;
listPendingBegin = 0;
iAudioID = NO_SOUND;
psCurAnim = NULL;
group = UBYTE_MAX;
psBaseStruct = NULL;
sDisplay.frameNumber = 0; // it was never drawn before
for (unsigned vPlayer = 0; vPlayer < MAX_PLAYERS; ++vPlayer)
{
visible[vPlayer] = hasSharedVision(vPlayer, player)? UINT8_MAX : 0;
}
memset(seenThisTick, 0, sizeof(seenThisTick));
died = 0;
burnStart = 0;
burnDamage = 0;
sDisplay.screenX = OFF_SCREEN;
sDisplay.screenY = OFF_SCREEN;
sDisplay.screenR = 0;
illumination = UBYTE_MAX;
resistance = ACTION_START_TIME; // init the resistance to indicate no EW performed on this droid
}
/* DROID::~DROID: release all resources associated with a droid -
* should only be called by objmem - use vanishDroid preferably
*/
DROID::~DROID()
{
DROID *psDroid = this;
DROID *psCurr, *psNext;
/* remove animation if present */
if (psDroid->psCurAnim != NULL)
{
animObj_Remove(psDroid->psCurAnim, psDroid->psCurAnim->psAnim->uwID);
psDroid->psCurAnim = NULL;
}
if (psDroid->droidType == DROID_TRANSPORTER || psDroid->droidType == DROID_SUPERTRANSPORTER)
{
if (psDroid->psGroup)
{
//free all droids associated with this Transporter
for (psCurr = psDroid->psGroup->psList; psCurr != NULL && psCurr != psDroid; psCurr = psNext)
{
psNext = psCurr->psGrpNext;
delete psCurr;
}
}
}
fpathRemoveDroidData(psDroid->id);
// leave the current group if any
if (psDroid->psGroup)
{
psDroid->psGroup->remove(psDroid);
}
// remove the droid from the cluster system
clustRemoveObject((BASE_OBJECT *)psDroid);
free(sMove.asPath);
}
// recycle a droid (retain it's experience and some of it's cost)
void recycleDroid(DROID *psDroid)
{
UDWORD numKills, minKills;
SDWORD i, cost, storeIndex;
Vector3i position;
CHECK_DROID(psDroid);
// store the droids kills
numKills = psDroid->experience/65536;
minKills = UWORD_MAX;
storeIndex = 0;
for(i=0; i<MAX_RECYCLED_DROIDS; i++)
{
if (aDroidExperience[psDroid->player][i] < (UWORD)minKills)
{
storeIndex = i;
minKills = aDroidExperience[psDroid->player][i];
}
}
aDroidExperience[psDroid->player][storeIndex] = (UWORD)numKills;
// return part of the cost of the droid
cost = calcDroidPower(psDroid);
cost = (cost/2) * psDroid->body / psDroid->originalBody;
addPower(psDroid->player, (UDWORD)cost);
// hide the droid
memset(psDroid->visible, 0, sizeof(psDroid->visible));
// stop any group moral checks
if (psDroid->psGroup)
{
psDroid->psGroup->remove(psDroid);
}
position.x = psDroid->pos.x; // Add an effect
position.z = psDroid->pos.y;
position.y = psDroid->pos.z;
vanishDroid(psDroid);
addEffect(&position, EFFECT_EXPLOSION, EXPLOSION_TYPE_DISCOVERY, false, NULL, false, gameTime - deltaGameTime + 1);
CHECK_DROID(psDroid);
}
bool removeDroidBase(DROID *psDel)
{
DROID *psCurr, *psNext;
STRUCTURE *psStruct;
CHECK_DROID(psDel);
if (isDead((BASE_OBJECT *)psDel))
{
// droid has already been killed, quit
return true;
}
syncDebugDroid(psDel, '#');
/* remove animation if present */
if (psDel->psCurAnim != NULL)
{
const bool bRet = animObj_Remove(psDel->psCurAnim, psDel->psCurAnim->psAnim->uwID);
ASSERT(bRet, "animObj_Remove failed");
psDel->psCurAnim = NULL;
}
//kill all the droids inside the transporter
if (psDel->droidType == DROID_TRANSPORTER || psDel->droidType == DROID_SUPERTRANSPORTER)
{
if (psDel->psGroup)
{
//free all droids associated with this Transporter
for (psCurr = psDel->psGroup->psList; psCurr != NULL && psCurr != psDel; psCurr = psNext)
{
psNext = psCurr->psGrpNext;
/* add droid to droid list then vanish it - hope this works! - GJ */
addDroid(psCurr, apsDroidLists);
vanishDroid(psCurr);
}
}
}
if (!bMultiPlayer) // The moral(e?) checks don't seem like something that belongs in real games.
{
// check moral
if (psDel->psGroup && psDel->psGroup->refCount > 1 && !bMultiPlayer)
{
DROID_GROUP *group = psDel->psGroup;
psDel->psGroup->remove(psDel);
psDel->psGroup = NULL;
orderGroupMoralCheck(group);
}
else
{
orderMoralCheck(psDel->player);
}
}
// leave the current group if any
if (psDel->psGroup)
{
psDel->psGroup->remove(psDel);
psDel->psGroup = NULL;
}
/* Put Deliv. Pts back into world when a command droid dies */
if (psDel->droidType == DROID_COMMAND)
{
for (psStruct = apsStructLists[psDel->player]; psStruct; psStruct=psStruct->psNext)
{
// alexl's stab at a right answer.
if (StructIsFactory(psStruct)
&& psStruct->pFunctionality->factory.psCommander == psDel)
{
assignFactoryCommandDroid(psStruct, NULL);
}
}
}
// Check to see if constructor droid currently trying to find a location to build
if (psDel->player == selectedPlayer && psDel->selected && isConstructionDroid(psDel))
{
// If currently trying to build, kill off the placement
if (tryingToGetLocation())
{
int numSelectedConstructors = 0;
for (DROID *psDroid = apsDroidLists[psDel->player]; psDroid != NULL; psDroid = psDroid->psNext)
{
numSelectedConstructors += psDroid->selected && isConstructionDroid(psDroid);
}
if (numSelectedConstructors <= 1) // If we were the last selected construction droid.
{
kill3DBuilding();
}
}
}
// remove the droid from the cluster systerm
clustRemoveObject((BASE_OBJECT *)psDel);
if (psDel->player == selectedPlayer)
{
intRefreshScreen();
}
killDroid(psDel);
return true;
}
static void removeDroidFX(DROID *psDel, unsigned impactTime)
{
Vector3i pos;
CHECK_DROID(psDel);
// only display anything if the droid is visible
if (!psDel->visible[selectedPlayer])
{
return;
}
if (psDel->droidType == DROID_PERSON && psDel->order.type != DORDER_RUNBURN)
{
/* blow person up into blood and guts */
compPersonToBits(psDel);
}
/* if baba and not running (on fire) then squish */
if (psDel->droidType == DROID_PERSON
&& psDel->order.type != DORDER_RUNBURN
&& psDel->visible[selectedPlayer])
{
// The babarian has been run over ...
audio_PlayStaticTrack(psDel->pos.x, psDel->pos.y, ID_SOUND_BARB_SQUISH);
}
else if (psDel->visible[selectedPlayer])
{
destroyFXDroid(psDel, impactTime);
pos.x = psDel->pos.x;
pos.z = psDel->pos.y;
pos.y = psDel->pos.z;
addEffect(&pos, EFFECT_DESTRUCTION, DESTRUCTION_TYPE_DROID, false, NULL, 0, impactTime);
audio_PlayStaticTrack( psDel->pos.x, psDel->pos.y, ID_SOUND_EXPLOSION );
}
}
bool destroyDroid(DROID *psDel, unsigned impactTime)
{
if (psDel->lastHitWeapon == WSC_LAS_SAT) // darken tile if lassat.
{
UDWORD width, breadth, mapX, mapY;
MAPTILE *psTile;
mapX = map_coord(psDel->pos.x);
mapY = map_coord(psDel->pos.y);
for (width = mapX-1; width <= mapX+1; width++)
{
for (breadth = mapY-1; breadth <= mapY+1; breadth++)
{
psTile = mapTile(width,breadth);
if(TEST_TILE_VISIBLE(selectedPlayer, psTile))
{
psTile->illumination /= 2;
}
}
}
}
removeDroidFX(psDel, impactTime);
removeDroidBase(psDel);
psDel->died = impactTime;
return true;
}
void vanishDroid(DROID *psDel)
{
removeDroidBase(psDel);
}
/* Remove a droid from the List so doesn't update or get drawn etc
TAKE CARE with removeDroid() - usually want droidRemove since it deal with cluster and grid code*/
//returns false if the droid wasn't removed - because it died!
bool droidRemove(DROID *psDroid, DROID *pList[MAX_PLAYERS])
{
CHECK_DROID(psDroid);
driveDroidKilled(psDroid); // Tell the driver system it's gone.
if (isDead((BASE_OBJECT *) psDroid))
{
// droid has already been killed, quit
return false;
}
// leave the current group if any - not if its a Transporter droid
if ((psDroid->droidType != DROID_TRANSPORTER && psDroid->droidType != DROID_SUPERTRANSPORTER) && psDroid->psGroup)
{
psDroid->psGroup->remove(psDroid);
psDroid->psGroup = NULL;
}
// reset the baseStruct
setDroidBase(psDroid, NULL);
// remove the droid from the cluster systerm
clustRemoveObject((BASE_OBJECT *)psDroid);
removeDroid(psDroid, pList);
if (psDroid->player == selectedPlayer)
{
intRefreshScreen();
}
return true;
}
static void droidFlameFallCallback( ANIM_OBJECT * psObj )
{
DROID *psDroid;
ASSERT_OR_RETURN( , psObj != NULL, "invalid anim object pointer");
psDroid = (DROID *) psObj->psParent;
ASSERT_OR_RETURN( , psDroid != NULL, "invalid Unit pointer");
psDroid->psCurAnim = NULL;
// This breaks synch, obviously. Animations are not synched, so changing game state as part of an animation is not completely ideal.
//debug(LOG_DEATH, "droidFlameFallCallback: Droid %d destroyed", (int)psDroid->id);
//destroyDroid( psDroid );
}
static void droidBurntCallback( ANIM_OBJECT * psObj )
{
DROID *psDroid;
ASSERT_OR_RETURN( , psObj != NULL, "invalid anim object pointer");
psDroid = (DROID *) psObj->psParent;
ASSERT_OR_RETURN( , psDroid != NULL, "invalid Unit pointer");
/* add falling anim */
psDroid->psCurAnim = animObj_Add((BASE_OBJECT *)psDroid, ID_ANIM_DROIDFLAMEFALL, 0, 1);
if (!psDroid->psCurAnim)
{
debug( LOG_ERROR, "couldn't add fall over anim");
return;
}
animObj_SetDoneFunc( psDroid->psCurAnim, droidFlameFallCallback );
}
void droidBurn(DROID *psDroid)
{
CHECK_DROID(psDroid);
if ( psDroid->droidType != DROID_PERSON )
{
ASSERT(LOG_ERROR, "can't burn anything except babarians currently!");
return;
}
if (psDroid->order.type != DORDER_RUNBURN)
{
/* set droid running */
orderDroid(psDroid, DORDER_RUNBURN, ModeImmediate);
}
/* if already burning return else remove currently-attached anim if present */
if ( psDroid->psCurAnim != NULL )
{
/* return if already burning */
if ( psDroid->psCurAnim->psAnim->uwID == ID_ANIM_DROIDBURN )
{
return;
}
else
{
const bool bRet = animObj_Remove(psDroid->psCurAnim, psDroid->psCurAnim->psAnim->uwID);
ASSERT(bRet, "animObj_Remove failed");
psDroid->psCurAnim = NULL;
}
}
/* add burning anim */
psDroid->psCurAnim = animObj_Add( (BASE_OBJECT *) psDroid,
ID_ANIM_DROIDBURN, 0, 3 );
if ( psDroid->psCurAnim == NULL )
{
debug( LOG_ERROR, "couldn't add burn anim" );
return;
}
/* set callback */
animObj_SetDoneFunc( psDroid->psCurAnim, droidBurntCallback );
/* add scream */
debug( LOG_NEVER, "baba burn" );
// NOTE: 3 types of screams are available ID_SOUND_BARB_SCREAM - ID_SOUND_BARB_SCREAM3
audio_PlayObjDynamicTrack( psDroid, ID_SOUND_BARB_SCREAM+(rand()%3), NULL );
}
void _syncDebugDroid(const char *function, DROID const *psDroid, char ch)
{
int list[] =
{
ch,
(int)psDroid->id,
psDroid->player,
psDroid->pos.x, psDroid->pos.y, psDroid->pos.z,
psDroid->rot.direction, psDroid->rot.pitch, psDroid->rot.roll,
(int)psDroid->order.type, psDroid->order.pos.x, psDroid->order.pos.y, psDroid->listSize,
(int)psDroid->action,
(int)psDroid->secondaryOrder,
(int)psDroid->body,
(int)psDroid->sMove.Status,
psDroid->sMove.speed, psDroid->sMove.moveDir,
psDroid->sMove.pathIndex, psDroid->sMove.numPoints,
psDroid->sMove.src.x, psDroid->sMove.src.y, psDroid->sMove.target.x, psDroid->sMove.target.y, psDroid->sMove.destination.x, psDroid->sMove.destination.y,
psDroid->sMove.bumpDir, (int)psDroid->sMove.bumpTime, psDroid->sMove.lastBump, psDroid->sMove.pauseTime, psDroid->sMove.bumpX, psDroid->sMove.bumpY, (int)psDroid->sMove.shuffleStart,
(int)psDroid->experience,
};
_syncDebugIntList(function, "%c droid%d = p%d;pos(%d,%d,%d),rot(%d,%d,%d),order%d(%d,%d)^%d,action%d,secondaryOrder%X,body%d,sMove(status%d,speed%d,moveDir%d,path%d/%d,src(%d,%d),target(%d,%d),destination(%d,%d),bump(%d,%d,%d,%d,(%d,%d),%d)),exp%u", list, ARRAY_SIZE(list));
}
/* The main update routine for all droids */
void droidUpdate(DROID *psDroid)
{
Vector3i dv;
UDWORD percentDamage, emissionInterval;
BASE_OBJECT *psBeingTargetted = NULL;
unsigned i;
CHECK_DROID(psDroid);
#ifdef DEBUG
// Check that we are (still) in the sensor list
if (psDroid->droidType == DROID_SENSOR)
{
BASE_OBJECT *psSensor;
for (psSensor = apsSensorList[0]; psSensor; psSensor = psSensor->psNextFunc)
{
if (psSensor == (BASE_OBJECT *)psDroid) break;
}
ASSERT(psSensor == (BASE_OBJECT *)psDroid, "%s(%p) not in sensor list!",
droidGetName(psDroid), psDroid);
}
#endif
syncDebugDroid(psDroid, '<');
// Save old droid position, update time.
psDroid->prevSpacetime = getSpacetime(psDroid);
psDroid->time = gameTime;
for (i = 0; i < MAX(1, psDroid->numWeaps); ++i)
{
psDroid->asWeaps[i].prevRot = psDroid->asWeaps[i].rot;
}
// update the cluster of the droid
if ((psDroid->id + gameTime)/2000 != (psDroid->id + gameTime - deltaGameTime)/2000)
{
clustUpdateObject((BASE_OBJECT *)psDroid);
}
// ai update droid
aiUpdateDroid(psDroid);
// Update the droids order. The droid may be killed here due to burn out.
orderUpdateDroid(psDroid);
if (isDead((BASE_OBJECT *)psDroid))
{
return; // FIXME: Workaround for babarians that were burned to death
}
// update the action of the droid
actionUpdateDroid(psDroid);
syncDebugDroid(psDroid, 'M');
// update the move system
moveUpdateDroid(psDroid);
/* Only add smoke if they're visible */
if((psDroid->visible[selectedPlayer]) && psDroid->droidType != DROID_PERSON)
{
// need to clip this value to prevent overflow condition
percentDamage = 100 - clip(PERCENT(psDroid->body, psDroid->originalBody), 0, 100);
// Is there any damage?
if(percentDamage>=25)
{
if(percentDamage>=100)
{
percentDamage = 99;
}
emissionInterval = CALC_DROID_SMOKE_INTERVAL(percentDamage);
int effectTime = std::max(gameTime - deltaGameTime + 1, psDroid->lastEmission + emissionInterval);
if (gameTime >= effectTime)
{
dv.x = psDroid->pos.x + DROID_DAMAGE_SPREAD;
dv.z = psDroid->pos.y + DROID_DAMAGE_SPREAD;
dv.y = psDroid->pos.z;
dv.y += (psDroid->sDisplay.imd->max.y * 2);
addEffect(&dv, EFFECT_SMOKE, SMOKE_TYPE_DRIFTING_SMALL, false, NULL, 0, effectTime);
psDroid->lastEmission = effectTime;
}
}
}
// -----------------
/* Are we a sensor droid or a command droid? Show where we target for selectedPlayer. */
if (psDroid->player == selectedPlayer && (psDroid->droidType == DROID_SENSOR || psDroid->droidType == DROID_COMMAND))
{
/* If we're attacking or sensing (observing), then... */
if ((psBeingTargetted = orderStateObj(psDroid, DORDER_ATTACK))
|| (psBeingTargetted = orderStateObj(psDroid, DORDER_OBSERVE)))
{
psBeingTargetted->bTargetted = true;
}
}
// -----------------
/* Update the fire damage data */
if (psDroid->burnStart != 0 && psDroid->burnStart != gameTime - deltaGameTime) // -deltaGameTime, since projectiles are updated after droids.
{
// The burnStart has been set, but is not from the previous tick, so we must be out of the fire.
psDroid->burnDamage = 0; // Reset burn damage done this tick.
if (psDroid->burnStart + BURN_TIME < gameTime)
{
// Finished burning.
psDroid->burnStart = 0;
}
else
{
// do burn damage
droidDamage(psDroid, BURN_DAMAGE, WC_HEAT, WSC_FLAME, gameTime - deltaGameTime/2 + 1, true);
}
}
// At this point, the droid may be dead due to burn damage.
if (isDead((BASE_OBJECT *)psDroid))
{
return;
}
calcDroidIllumination(psDroid);
// Check the resistance level of the droid
if ((psDroid->id + gameTime)/833 != (psDroid->id + gameTime - deltaGameTime)/833)
{
// Zero resistance means not currently been attacked - ignore these
if (psDroid->resistance && psDroid->resistance < droidResistance(psDroid))
{
// Increase over time if low
psDroid->resistance++;
}
}
syncDebugDroid(psDroid, '>');
CHECK_DROID(psDroid);
}
/* See if a droid is next to a structure */
static bool droidNextToStruct(DROID *psDroid, BASE_OBJECT *psStruct)
{
SDWORD minX, maxX, maxY, x,y;
CHECK_DROID(psDroid);
minX = map_coord(psDroid->pos.x) - 1;
y = map_coord(psDroid->pos.y) - 1;
maxX = minX + 2;
maxY = y + 2;
if (minX < 0)
{
minX = 0;
}
if (maxX >= (SDWORD)mapWidth)
{
maxX = (SDWORD)mapWidth;
}
if (y < 0)
{
y = 0;
}
if (maxY >= (SDWORD)mapHeight)
{
maxY = (SDWORD)mapHeight;
}
for(;y <= maxY; y++)
{
for(x = minX;x <= maxX; x++)
{
if (TileHasStructure(mapTile((UWORD)x,(UWORD)y)) &&
getTileStructure(x,y) == (STRUCTURE *)psStruct)
{
return true;
}
}
}
return false;
}
static bool
droidCheckBuildStillInProgress( void *psObj )
{
DROID *psDroid;
if ( psObj == NULL )
{
return false;
}
psDroid = (DROID*)psObj;
CHECK_DROID(psDroid);
if ( !psDroid->died && psDroid->action == DACTION_BUILD )
{
return true;
}
else
{
return false;
}
}
static bool
droidBuildStartAudioCallback( void *psObj )
{
DROID *psDroid;
psDroid = (DROID*)psObj;
if (psDroid == NULL)
{
return true;
}
if ( psDroid->visible[selectedPlayer] )
{
audio_PlayObjDynamicTrack( psDroid, ID_SOUND_CONSTRUCTION_LOOP, droidCheckBuildStillInProgress );
}
return true;
}
/* Set up a droid to build a structure - returns true if successful */
DroidStartBuild droidStartBuild(DROID *psDroid)
{
STRUCTURE *psStruct;
CHECK_DROID(psDroid);
/* See if we are starting a new structure */
if ((psDroid->order.psObj == NULL) &&
(psDroid->order.type == DORDER_BUILD ||
psDroid->order.type == DORDER_LINEBUILD))
{
STRUCTURE_STATS *psStructStat = psDroid->order.psStats;
STRUCTURE_LIMITS *structLimit = &asStructLimits[psDroid->player][psStructStat - asStructureStats];
//need to check structLimits have not been exceeded
if (structLimit->currentQuantity >= structLimit->limit)
{
intBuildFinished(psDroid);
cancelBuild(psDroid);
return DroidStartBuildFailed;
}
// Can't build on burning oil derricks.
if (psStructStat->type == REF_RESOURCE_EXTRACTOR && fireOnLocation(psDroid->order.pos.x,psDroid->order.pos.y))
{