-
-
Notifications
You must be signed in to change notification settings - Fork 531
/
order.cpp
4304 lines (3956 loc) · 133 KB
/
order.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-2020 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
* Functions for setting the orders of a droid or group of droids.
*
*/
#include <string.h>
#include "lib/framework/frame.h"
#include "lib/framework/input.h"
#include "lib/framework/math_ext.h"
#include "lib/framework/object_list_iteration.h"
#include "lib/ivis_opengl/ivisdef.h"
#include "objects.h"
#include "order.h"
#include "action.h"
#include "map.h"
#include "formationdef.h"
#include "formation.h"
#include "projectile.h"
#include "effects.h" // for waypoint display
#include "lib/gamelib/gtime.h"
#include "lib/netplay/netplay.h"
#include "intorder.h"
#include "orderdef.h"
#include "transporter.h"
#include "qtscript.h"
#include "group.h"
#include "cmddroid.h"
#include "move.h"
#include "multiplay.h" //ajl
#include "random.h" // to balance the damaged units flow to repairs
#include "mission.h"
#include "hci.h"
#include "visibility.h"
#include "display.h"
#include "ai.h"
#include "warcam.h"
#include "lib/sound/audio_id.h"
#include "lib/sound/audio.h"
#include "fpath.h"
#include "display3d.h"
#include "console.h"
#include "mapgrid.h"
#include "random.h"
/** How long a droid runs after it fails do respond due to low moral. */
#define RUN_TIME 8000
/** How long a droid runs burning after it fails do respond due to low moral. */
#define RUN_BURN_TIME 10000
/** The distance a droid has in guard mode. */
#define DEFEND_MAXDIST (TILE_UNITS * 3)
/** The distance a droid has in guard mode.
* @todo seems to be used as equivalent to GUARD_MAXDIST.
*/
#define DEFEND_BASEDIST (TILE_UNITS * 3)
/** The distance a droid has in guard mode. Equivalent to GUARD_MAXDIST, but used for droids being on a command group. */
#define DEFEND_CMD_MAXDIST (TILE_UNITS * 8)
/** The distance a droid has in guard mode. Equivalent to GUARD_BASEDIST, but used for droids being on a command group. */
#define DEFEND_CMD_BASEDIST (TILE_UNITS * 5)
/** The maximum distance a constructor droid has in guard mode. */
#define CONSTRUCT_MAXDIST (TILE_UNITS * 8)
/** The maximum distance allowed to a droid to move out of the path on a patrol/scout. */
#define SCOUT_DIST (TILE_UNITS * 8)
/** The maximum distance allowed to a droid to move out of the path if already attacking a target on a patrol/scout. */
#define SCOUT_ATTACK_DIST (TILE_UNITS * 5)
static void orderClearDroidList(DROID *psDroid);
/** Whether an order effect has been displayed
* @todo better documentation required.
*/
static bool bOrderEffectDisplayed = false;
/** What the droid's action/order it is currently. This is used to debug purposes, jointly with showSAMPLES(). */
extern char DROIDDOING[512];
//////////////////////////////////////////////////////////////////
#define ASSERT_PLAYER_OR_RETURN(retVal, player) \
ASSERT_OR_RETURN(retVal, player < MAX_PLAYERS, "Invalid player: %" PRIu32 "", player);
//////////////////////////////////////////////////////////////////
struct RtrBestResult
{
RTR_DATA_TYPE type;
BASE_OBJECT *psObj;
RtrBestResult(RTR_DATA_TYPE type, BASE_OBJECT *psObj): type(type), psObj(psObj) {}
RtrBestResult(): type(RTR_TYPE_NO_RESULT), psObj(nullptr) {}
RtrBestResult(DROID_ORDER_DATA *psOrder): type(psOrder->rtrType), psObj(psOrder->psObj)
{
if (psObj->type == OBJ_STRUCTURE && ((STRUCTURE*)psObj)->pStructureType->type == REF_REPAIR_FACILITY) type = RTR_TYPE_REPAIR_FACILITY;
else if (psObj->type == OBJ_STRUCTURE) type = RTR_TYPE_HQ;
else type = RTR_TYPE_DROID;
}
};
static bool secondaryCheckDamageLevelDeselect(DROID *psDroid, SECONDARY_STATE repairState);
static RtrBestResult decideWhereToRepairAndBalance(DROID *psDroid);
/** This function checks if the droid is off range. If yes, it uses actionDroid() to make the droid to move to its target if its target is on range, or to move to its order position if not.
* @todo droid doesn't shoot while returning to the guard position.
*/
static void orderCheckGuardPosition(DROID *psDroid, SDWORD range)
{
if (psDroid->order.psObj != nullptr)
{
UDWORD x, y;
// repair droids always follow behind - don't want them jumping into the line of fire
if ((!(psDroid->droidType == DROID_REPAIR || psDroid->droidType == DROID_CYBORG_REPAIR))
&& psDroid->order.psObj->type == OBJ_DROID && orderStateLoc((DROID *)psDroid->order.psObj, DORDER_MOVE, &x, &y))
{
// got a moving droid - check against where the unit is going
psDroid->order.pos = Vector2i(x, y);
}
else
{
psDroid->order.pos = psDroid->order.psObj->pos.xy();
}
}
int xdiff = psDroid->pos.x - psDroid->order.pos.x;
int ydiff = psDroid->pos.y - psDroid->order.pos.y;
if (xdiff * xdiff + ydiff * ydiff > range * range)
{
if ((psDroid->sMove.Status != MOVEINACTIVE) &&
((psDroid->action == DACTION_MOVE) ||
(psDroid->action == DACTION_MOVEFIRE)))
{
xdiff = psDroid->sMove.destination.x - psDroid->order.pos.x;
ydiff = psDroid->sMove.destination.y - psDroid->order.pos.y;
if (xdiff * xdiff + ydiff * ydiff > range * range)
{
actionDroid(psDroid, DACTION_MOVE, psDroid->order.pos.x, psDroid->order.pos.y);
}
}
else
{
actionDroid(psDroid, DACTION_MOVE, psDroid->order.pos.x, psDroid->order.pos.y);
}
}
}
struct RSComparator
{
RSComparator(const STRUCTURE *self) : selfPosX(self->pos.x), selfPosY(self->pos.y), selfPlayer(self->player) {}
RSComparator(const DROID *self) : selfPosX(self->pos.x), selfPosY(self->pos.y), selfPlayer(self->player) {}
// "less" comparator for our priority queue
bool operator() (const DROID* l, const DROID* r) const
{
// Here, we know that both left and right Droids are:
// - either our own, or one of our allies
// - already within repair radius
// - not dead
// - don't have full HP
// always prefer our own units
const bool lown = l->player == selfPlayer;
const bool rown = r->player == selfPlayer;
if (lown && !rown) return true;
if (!lown && rown) return false;
// Next highest priority:
// Take any droid with orders to Return to Repair (DORDER_RTR),
// or that have been ordered to a specific repair facility (DORDER_RTR_SPECIFIED)
BASE_OBJECT *const lTarget = orderStateObj(l, DORDER_RTR);
BASE_OBJECT *const rTarget = orderStateObj(r, DORDER_RTR);
const bool lhighest = ((l->order.type == DORDER_RTR || l->order.type == DORDER_RTR_SPECIFIED));
const bool rhighest = ((r->order.type == DORDER_RTR || r->order.type == DORDER_RTR_SPECIFIED));
if (lhighest && !rhighest) return true;
if (!lhighest && rhighest) return false;
const auto distanceCheck = [this](const DROID* l, const DROID* r)
{
const auto ldist = (l->pos.x - selfPosX) * (l->pos.x - selfPosX) + (l->pos.y - selfPosY) * (l->pos.y - selfPosY);
const auto rdist = (r->pos.x - selfPosX) * (r->pos.x - selfPosX) + (r->pos.y - selfPosY) * (r->pos.y - selfPosY);
// debug(LOG_REPAIRS, "comparator called %i %i: (ldist %i >= %i rdist) %i", l->id, r->id, ldist, rdist, ldist >= rdist);
if (ldist != rdist)
{
return ldist > rdist;
}
// If the distances are the same, fallback to damage level check: prefer the one which is more damaged.
if (l->body != r->body)
{
return l->body > r->body;
}
// Fall back to comparing droid IDs, which should never be the same, as the last resort
// to preserve the consistency of comparator (e.g. strict weak ordering).
return l->id > r->id;
};
if (lhighest && rhighest)
{
// break the tie with distance check
return distanceCheck(l, r);
}
// Second highest priority:
// Help out another nearby repair facility
const bool lsecond = l->action == DACTION_WAITFORREPAIR && lTarget && lTarget->type == OBJ_STRUCTURE && ((STRUCTURE *)lTarget)->pStructureType && ((STRUCTURE *)lTarget)->pStructureType->type == REF_REPAIR_FACILITY;
const bool rsecond = r->action == DACTION_WAITFORREPAIR && rTarget && rTarget->type == OBJ_STRUCTURE && ((STRUCTURE *)rTarget)->pStructureType && ((STRUCTURE *)rTarget)->pStructureType->type == REF_REPAIR_FACILITY;
if (lsecond && !rsecond) return true;
if (!lsecond && rsecond) return false;
// at this point we don't really have a preference
// just repair closest.
// In case they have the same distance,
// fallback to the damage level check: prefer the most damaged.
return distanceCheck(l, r);
}
private:
const int selfPosX;
const int selfPosY;
const int selfPlayer;
};
/// @return return top priority droid to repair, or nullptr
static DROID* _findSomeoneToRepair(REPAIR_FACILITY *psRepairFac,
std::priority_queue<DROID*, std::vector<DROID*>, RSComparator> queue,
int x, int y, int radius, int player)
{
GridList gridList;
gridList = gridStartIterateRepairCandidates(x, y, radius, player);
for (GridIterator gi = gridList.begin(); gi != gridList.end(); ++gi)
{
DROID *psDroid = (DROID*) *gi;
if (psDroid->isDamaged())
{
queue.push(psDroid);
}
else if (psDroid->order.type == DORDER_RTR || psDroid->order.type == DORDER_RTR_SPECIFIED)
{
// we intentionally iterate over fully healthy droids too, in order to clear RTR order
psDroid->body = MIN(psDroid->originalBody, psDroid->body);
droidWasFullyRepaired(psDroid, psRepairFac);
}
}
if (!queue.empty())
{
debug(LOG_REPAIRS, "top was %i", queue.top()->id);
return queue.top();
}
return nullptr;
}
DROID *findSomeoneToRepair(const STRUCTURE *psStructure, int radius)
{
REPAIR_FACILITY *psRepairFac = &psStructure->pFunctionality->repairFacility;
RSComparator cmpr(psStructure);
std::priority_queue<DROID*, std::vector<DROID*>, RSComparator> queue(cmpr);
return _findSomeoneToRepair(psRepairFac, queue, psStructure->pos.x, psStructure->pos.y, radius, psStructure->player);
}
DROID *findSomeoneToRepair(const DROID *psTurret, int radius)
{
RSComparator cmpr(psTurret);
std::priority_queue<DROID*, std::vector<DROID*>, RSComparator> queue(cmpr);
return _findSomeoneToRepair(nullptr, queue, psTurret->pos.x, psTurret->pos.y, radius, psTurret->player);
}
/** This function checks if there are any structures to repair or help build in a given radius near the droid defined by REPAIR_RANGE if it is on hold, and REPAIR_MAXDIST if not on hold.
* It returns a damaged or incomplete structure if any was found or nullptr if none was found.
*/
static std::pair<STRUCTURE *, DROID_ACTION> checkForDamagedStruct(DROID *psDroid)
{
STRUCTURE *psFailedTarget = nullptr;
if (psDroid->action == DACTION_SULK)
{
psFailedTarget = (STRUCTURE *)psDroid->psActionTarget[0];
}
unsigned radius = ((psDroid->order.type == DORDER_HOLD) || (psDroid->order.type == DORDER_NONE && secondaryGetState(psDroid, DSO_HALTTYPE) == DSS_HALT_HOLD)) ? REPAIR_RANGE : REPAIR_MAXDIST;
unsigned bestDistanceSq = radius * radius;
std::pair<STRUCTURE *, DROID_ACTION> best = {nullptr, DACTION_NONE};
for (BASE_OBJECT *object : gridStartIterate(psDroid->pos.x, psDroid->pos.y, radius))
{
unsigned distanceSq = droidSqDist(psDroid, object); // droidSqDist returns -1 if unreachable, (unsigned)-1 is a big number.
STRUCTURE *structure = castStructure(object);
if (structure == nullptr || // Must be a structure.
structure == psFailedTarget || // Must not have just failed to reach it.
distanceSq > bestDistanceSq || // Must be as close as possible.
!visibleObject(psDroid, structure, false) || // Must be able to sense it.
!aiCheckAlliances(psDroid->player, structure->player) || // Must be a friendly structure.
checkDroidsDemolishing(structure)) // Must not be trying to get rid of it.
{
continue;
}
// Check for structures to repair.
if (structure->status == SS_BUILT && structure->isDamaged())
{
bestDistanceSq = distanceSq;
best = {structure, DACTION_REPAIR};
}
// Check for structures to help build.
else if (structure->status == SS_BEING_BUILT)
{
bestDistanceSq = distanceSq;
best = {structure, DACTION_BUILD};
}
}
return best;
}
static bool isRepairlikeAction(DROID_ACTION action)
{
switch (action)
{
case DACTION_BUILD:
case DACTION_BUILDWANDER:
case DACTION_DEMOLISH:
case DACTION_DROIDREPAIR:
case DACTION_MOVETOBUILD:
case DACTION_MOVETODEMOLISH:
case DACTION_MOVETODROIDREPAIR:
case DACTION_MOVETOREPAIR:
case DACTION_MOVETORESTORE:
case DACTION_REPAIR:
case DACTION_RESTORE:
return true;
default:
return false;
}
}
static bool tryDoRepairlikeAction(DROID *psDroid)
{
if (isRepairlikeAction(psDroid->action))
{
return true; // Already doing something.
}
unsigned radius;
switch (psDroid->droidType)
{
case DROID_REPAIR:
case DROID_CYBORG_REPAIR:
if (((psDroid->order.type == DORDER_HOLD) ||
(psDroid->order.type == DORDER_NONE && secondaryGetState(psDroid, DSO_HALTTYPE) == DSS_HALT_HOLD)))
{
radius = REPAIR_RANGE;
}
else
{
radius = REPAIR_MAXDIST;
}
//repair droids default to repairing droids within a given range
if (DROID *repairTarget = findSomeoneToRepair(psDroid, radius))
{
actionDroid(psDroid, DACTION_DROIDREPAIR, repairTarget);
}
break;
case DROID_CONSTRUCT:
case DROID_CYBORG_CONSTRUCT:
{
//construct droids default to repairing and helping structures within a given range
auto damaged = checkForDamagedStruct(psDroid);
if (damaged.second == DACTION_REPAIR)
{
actionDroid(psDroid, damaged.second, damaged.first);
}
else if (damaged.second == DACTION_BUILD)
{
psDroid->order.psStats = damaged.first->pStructureType;
psDroid->order.direction = damaged.first->rot.direction;
actionDroid(psDroid, damaged.second, damaged.first->pos.x, damaged.first->pos.y);
}
break;
}
default:
return false;
}
return true;
}
/** This function updates all the orders status, according with psdroid's current order and state.
* Returns false if all further droid processing should be shortcut for this frame (because, for example, the droid is a transporter that was moved to the off-world list)
*/
bool orderUpdateDroid(DROID *psDroid)
{
BASE_OBJECT *psObj = nullptr;
STRUCTURE *psStruct, *psWall;
SDWORD xdiff, ydiff;
bool bAttack;
SDWORD xoffset, yoffset;
if (psDroid == nullptr || isDead(psDroid))
{
return true;
}
const WEAPON_STATS *psWeapStats = psDroid->getWeaponStats(0);
// clear the target if it has died
if (psDroid->order.psObj && psDroid->order.psObj->died)
{
syncDebugObject(psDroid->order.psObj, '-');
setDroidTarget(psDroid, nullptr);
objTrace(psDroid->id, "Target dead");
}
//clear its base struct if its died
if (psDroid->psBaseStruct && psDroid->psBaseStruct->died)
{
syncDebugStructure(psDroid->psBaseStruct, '-');
setDroidBase(psDroid, nullptr);
objTrace(psDroid->id, "Base struct dead");
}
// check for died objects in the list
orderCheckList(psDroid);
switch (psDroid->order.type)
{
case DORDER_NONE:
case DORDER_HOLD:
// see if there are any orders queued up
if (orderDroidList(psDroid))
{
// started a new order, quit
break;
}
// if you are in a command group, default to guarding the commander
else if (hasCommander(psDroid) && psDroid->order.type != DORDER_HOLD
&& psDroid->order.psStats != structGetDemolishStat()) // stop the constructor auto repairing when it is about to demolish
{
orderDroidObj(psDroid, DORDER_GUARD, psDroid->psGroup->psCommander, ModeImmediate);
}
else if (psDroid->isTransporter() && !bMultiPlayer)
{
}
// default to guarding
else if (!tryDoRepairlikeAction(psDroid)
&& psDroid->order.type != DORDER_HOLD
&& psDroid->order.psStats != structGetDemolishStat()
&& secondaryGetState(psDroid, DSO_HALTTYPE) == DSS_HALT_GUARD
&& !psDroid->isVtol())
{
orderDroidLoc(psDroid, DORDER_GUARD, psDroid->pos.x, psDroid->pos.y, ModeImmediate);
}
break;
case DORDER_TRANSPORTRETURN:
if (psDroid->action == DACTION_NONE)
{
missionMoveTransporterOffWorld(psDroid);
/* clear order */
psDroid->order = DroidOrder(DORDER_NONE);
return false; // signal to caller to skip further processing this frame - droid was moved to a different list!
}
break;
case DORDER_TRANSPORTOUT:
if (psDroid->action == DACTION_NONE)
{
if (psDroid->player == selectedPlayer)
{
if (getDroidsToSafetyFlag())
{
//move droids in Transporter into holding list
moveDroidsToSafety(psDroid);
//we need the transporter to just sit off world for a while...
orderDroid(psDroid, DORDER_TRANSPORTIN, ModeImmediate);
/* set action transporter waits for timer */
actionDroid(psDroid, DACTION_TRANSPORTWAITTOFLYIN);
missionSetReinforcementTime(gameTime);
//don't do this until waited for the required time
//fly Transporter back to get some more droids
//orderDroidLoc( psDroid, DORDER_TRANSPORTIN,
//getLandingX(selectedPlayer), getLandingY(selectedPlayer));
}
else
{
/* clear order */
psDroid->order = DroidOrder(DORDER_NONE);
}
//the script can call startMission for this callback for offworld missions (if we want to change level)
triggerEvent(TRIGGER_TRANSPORTER_EXIT, psDroid);
psDroid->sMove.speed = 0; // Prevent radical movement vector when adjusting from home to away map exit and entry coordinates.
}
}
break;
case DORDER_TRANSPORTIN:
if ((psDroid->action == DACTION_NONE) &&
(psDroid->sMove.Status == MOVEINACTIVE))
{
/* clear order */
psDroid->order = DroidOrder(DORDER_NONE);
//FFS! You only wan't to do this if the droid being tracked IS the transporter! Not all the time!
// What about if your happily playing the game and tracking a droid, and re-enforcements come in!
// And suddenly BLAM!!!! It drops you out of camera mode for no apparent reason! TOTALY CONFUSING
// THE PLAYER!
//
// Just had to get that off my chest....end of rant.....
//
if (psDroid == getTrackingDroid()) // Thats better...
{
/* deselect transporter if have been tracking */
if (getWarCamStatus())
{
camToggleStatus();
}
}
DeSelectDroid(psDroid);
/*don't try the unload if moving droids to safety and still got some
droids left - wait until full and then launch again*/
if (psDroid->player == selectedPlayer && getDroidsToSafetyFlag() &&
missionDroidsRemaining(selectedPlayer))
{
resetTransporter();
triggerEvent(TRIGGER_TRANSPORTER_LANDED, psDroid);
}
else
{
unloadTransporter(psDroid, psDroid->pos.x, psDroid->pos.y, false);
}
}
break;
case DORDER_MOVE:
// Just wait for the action to finish then clear the order
if (psDroid->action == DACTION_NONE || psDroid->action == DACTION_ATTACK)
{
psDroid->order = DroidOrder(DORDER_NONE);
}
break;
case DORDER_RECOVER:
if (psDroid->order.psObj == nullptr)
{
psDroid->order = DroidOrder(DORDER_NONE);
}
else if (psDroid->action == DACTION_NONE)
{
// stopped moving, but still haven't got the artifact
actionDroid(psDroid, DACTION_MOVE, psDroid->order.psObj->pos.x, psDroid->order.psObj->pos.y);
}
break;
case DORDER_SCOUT:
case DORDER_PATROL:
// if there is an enemy around, attack it
if (psDroid->action == DACTION_MOVE || psDroid->action == DACTION_MOVEFIRE || (psDroid->action == DACTION_NONE && psDroid->isVtol()))
{
bool tooFarFromPath = false;
if (psDroid->isVtol() && psDroid->order.type == DORDER_PATROL)
{
// Don't stray too far from the patrol path - only attack if we're near it
// A fun algorithm to detect if we're near the path
Vector2i delta = psDroid->order.pos - psDroid->order.pos2;
if (delta == Vector2i(0, 0))
{
tooFarFromPath = false;
}
else if (abs(delta.x) >= abs(delta.y) &&
MIN(psDroid->order.pos.x, psDroid->order.pos2.x) - SCOUT_DIST <= psDroid->pos.x &&
psDroid->pos.x <= MAX(psDroid->order.pos.x, psDroid->order.pos2.x) + SCOUT_DIST)
{
tooFarFromPath = (abs((psDroid->pos.x - psDroid->order.pos.x) * delta.y / delta.x +
psDroid->order.pos.y - psDroid->pos.y) > SCOUT_DIST);
}
else if (abs(delta.x) <= abs(delta.y) &&
MIN(psDroid->order.pos.y, psDroid->order.pos2.y) - SCOUT_DIST <= psDroid->pos.y &&
psDroid->pos.y <= MAX(psDroid->order.pos.y, psDroid->order.pos2.y) + SCOUT_DIST)
{
tooFarFromPath = (abs((psDroid->pos.y - psDroid->order.pos.y) * delta.x / delta.y +
psDroid->order.pos.x - psDroid->pos.x) > SCOUT_DIST);
}
else
{
tooFarFromPath = true;
}
}
if (!tooFarFromPath)
{
// true if in condition to set actionDroid to attack/observe
bool attack = secondaryGetState(psDroid, DSO_ATTACK_LEVEL) == DSS_ALEV_ALWAYS &&
aiBestNearestTarget(psDroid, &psObj, 0, SCOUT_ATTACK_DIST) >= 0;
switch (psDroid->droidType)
{
case DROID_CONSTRUCT:
case DROID_CYBORG_CONSTRUCT:
case DROID_REPAIR:
case DROID_CYBORG_REPAIR:
tryDoRepairlikeAction(psDroid);
break;
case DROID_WEAPON:
case DROID_CYBORG:
case DROID_CYBORG_SUPER:
case DROID_PERSON:
case DROID_COMMAND:
if (attack)
{
actionDroid(psDroid, DACTION_ATTACK, psObj);
}
break;
case DROID_SENSOR:
if (!cbSensorDroid(psDroid) && attack)
{
actionDroid(psDroid, DACTION_OBSERVE, psObj);
}
break;
default:
actionDroid(psDroid, DACTION_NONE);
break;
}
}
}
if (psDroid->action == DACTION_NONE)
{
xdiff = psDroid->pos.x - psDroid->order.pos.x;
ydiff = psDroid->pos.y - psDroid->order.pos.y;
if (xdiff * xdiff + ydiff * ydiff < SCOUT_DIST * SCOUT_DIST)
{
if (psDroid->order.type == DORDER_PATROL)
{
// see if we have anything queued up
if (orderDroidList(psDroid))
{
// started a new order, quit
break;
}
if (psDroid->isVtol() && !vtolFull(psDroid) && (psDroid->secondaryOrder & DSS_ALEV_MASK) != DSS_ALEV_NEVER)
{
moveToRearm(psDroid);
break;
}
// head back to the other point
std::swap(psDroid->order.pos, psDroid->order.pos2);
actionDroid(psDroid, DACTION_MOVE, psDroid->order.pos.x, psDroid->order.pos.y);
}
else
{
psDroid->order = DroidOrder(DORDER_NONE);
}
}
else
{
actionDroid(psDroid, DACTION_MOVE, psDroid->order.pos.x, psDroid->order.pos.y);
}
}
else if (((psDroid->action == DACTION_ATTACK) ||
(psDroid->action == DACTION_VTOLATTACK) ||
(psDroid->action == DACTION_MOVETOATTACK) ||
(psDroid->action == DACTION_ROTATETOATTACK) ||
(psDroid->action == DACTION_OBSERVE) ||
(psDroid->action == DACTION_MOVETOOBSERVE)) &&
secondaryGetState(psDroid, DSO_HALTTYPE) != DSS_HALT_PURSUE)
{
// attacking something - see if the droid has gone too far, go up to twice the distance we want to go, so that we don't repeatedly turn back when the target is almost in range.
if (objPosDiffSq(psDroid->pos, Vector3i(psDroid->actionPos, 0)) > (SCOUT_ATTACK_DIST * 2 * SCOUT_ATTACK_DIST * 2))
{
actionDroid(psDroid, DACTION_RETURNTOPOS, psDroid->actionPos.x, psDroid->actionPos.y);
}
}
if (psDroid->order.type == DORDER_PATROL && psDroid->isVtol() && vtolEmpty(psDroid) && (psDroid->secondaryOrder & DSS_ALEV_MASK) != DSS_ALEV_NEVER)
{
moveToRearm(psDroid); // Completely empty (and we're not set to hold fire), don't bother patrolling.
break;
}
break;
case DORDER_CIRCLE:
// if there is an enemy around, attack it
if (psDroid->action == DACTION_MOVE &&
secondaryGetState(psDroid, DSO_ATTACK_LEVEL) == DSS_ALEV_ALWAYS &&
aiBestNearestTarget(psDroid, &psObj, 0, SCOUT_ATTACK_DIST) >= 0)
{
switch (psDroid->droidType)
{
case DROID_WEAPON:
case DROID_CYBORG:
case DROID_CYBORG_SUPER:
case DROID_PERSON:
case DROID_COMMAND:
actionDroid(psDroid, DACTION_ATTACK, psObj);
break;
case DROID_SENSOR:
actionDroid(psDroid, DACTION_OBSERVE, psObj);
break;
default:
actionDroid(psDroid, DACTION_NONE);
break;
}
}
else if (psDroid->action == DACTION_NONE || psDroid->action == DACTION_MOVE)
{
if (psDroid->action == DACTION_MOVE)
{
// see if we have anything queued up
if (orderDroidList(psDroid))
{
// started a new order, quit
break;
}
}
Vector2i edgeDiff = psDroid->pos.xy() - psDroid->actionPos;
if (psDroid->action != DACTION_MOVE || dot(edgeDiff, edgeDiff) <= TILE_UNITS * 4 * TILE_UNITS * 4)
{
//Watermelon:use orderX,orderY as local space origin and calculate droid direction in local space
Vector2i diff = psDroid->pos.xy() - psDroid->order.pos;
uint16_t angle = iAtan2(diff) - DEG(30);
do
{
xoffset = iSinR(angle, 1500);
yoffset = iCosR(angle, 1500);
angle -= DEG(10);
}
while (!worldOnMap(psDroid->order.pos.x + xoffset, psDroid->order.pos.y + yoffset)); // Don't try to fly off map.
actionDroid(psDroid, DACTION_MOVE, psDroid->order.pos.x + xoffset, psDroid->order.pos.y + yoffset);
}
if (psDroid->isVtol() && vtolEmpty(psDroid) && (psDroid->secondaryOrder & DSS_ALEV_MASK) != DSS_ALEV_NEVER)
{
moveToRearm(psDroid); // Completely empty (and we're not set to hold fire), don't bother circling.
break;
}
}
else if (((psDroid->action == DACTION_ATTACK) ||
(psDroid->action == DACTION_VTOLATTACK) ||
(psDroid->action == DACTION_MOVETOATTACK) ||
(psDroid->action == DACTION_ROTATETOATTACK) ||
(psDroid->action == DACTION_OBSERVE) ||
(psDroid->action == DACTION_MOVETOOBSERVE)) &&
secondaryGetState(psDroid, DSO_HALTTYPE) != DSS_HALT_PURSUE)
{
// attacking something - see if the droid has gone too far
xdiff = psDroid->pos.x - psDroid->actionPos.x;
ydiff = psDroid->pos.y - psDroid->actionPos.y;
if (xdiff * xdiff + ydiff * ydiff > 2000 * 2000)
{
// head back to the target location
actionDroid(psDroid, DACTION_RETURNTOPOS, psDroid->order.pos.x, psDroid->order.pos.y);
}
}
break;
case DORDER_HELPBUILD:
case DORDER_DEMOLISH:
case DORDER_OBSERVE:
case DORDER_REPAIR:
case DORDER_DROIDREPAIR:
case DORDER_RESTORE:
if (psDroid->action == DACTION_NONE || psDroid->order.psObj == nullptr)
{
psDroid->order = DroidOrder(DORDER_NONE);
actionDroid(psDroid, DACTION_NONE);
if (psDroid->player == selectedPlayer)
{
intRefreshScreen();
}
}
break;
case DORDER_REARM:
if (psDroid->order.psObj == nullptr || psDroid->psActionTarget[0] == nullptr)
{
// arm pad destroyed find another
psDroid->order = DroidOrder(DORDER_NONE);
moveToRearm(psDroid);
}
else if (psDroid->action == DACTION_NONE)
{
psDroid->order = DroidOrder(DORDER_NONE);
}
break;
case DORDER_ATTACK:
case DORDER_ATTACKTARGET:
if (psDroid->order.psObj == nullptr || psDroid->order.psObj->died)
{
// if vtol then return to rearm pad as long as there are no other
// orders queued up
if (psDroid->isVtol())
{
if (!orderDroidList(psDroid))
{
psDroid->order = DroidOrder(DORDER_NONE);
moveToRearm(psDroid);
}
}
else
{
psDroid->order = DroidOrder(DORDER_NONE);
actionDroid(psDroid, DACTION_NONE);
}
}
else if (((psDroid->action == DACTION_MOVE) ||
(psDroid->action == DACTION_MOVEFIRE)) &&
actionVisibleTarget(psDroid, psDroid->order.psObj, 0) && !psDroid->isVtol())
{
// moved near enough to attack change to attack action
actionDroid(psDroid, DACTION_ATTACK, psDroid->order.psObj);
}
else if ((psDroid->action == DACTION_MOVETOATTACK) &&
!psDroid->isVtol() &&
!actionVisibleTarget(psDroid, psDroid->order.psObj, 0) &&
secondaryGetState(psDroid, DSO_HALTTYPE) != DSS_HALT_HOLD)
{
// lost sight of the target while chasing it - change to a move action so
// that the unit will fire on other things while moving
actionDroid(psDroid, DACTION_MOVE, psDroid->order.psObj->pos.x, psDroid->order.psObj->pos.y);
}
else if (!psDroid->isVtol()
&& psDroid->order.psObj == psDroid->psActionTarget[0]
&& actionInRange(psDroid, psDroid->order.psObj, 0)
&& (psWall = visGetBlockingWall(psDroid, psDroid->order.psObj))
&& !aiCheckAlliances(psWall->player, psDroid->player)
// this is still wrong and should be unified with "action.cpp: actionUpdateDroid: case DACTION_ATTACK",
// because "case DACTION_ATTACK" actualy takes care of checking for all weapons (not only the first one)
// and for direct/indirect weapons.
&& psWeapStats && proj_Direct(psWeapStats)
)
{
// there is a wall in the way - attack that
actionDroid(psDroid, DACTION_ATTACK, psWall);
}
else if ((psDroid->action == DACTION_NONE) ||
(psDroid->action == DACTION_CLEARREARMPAD))
{
if ((psDroid->order.type == DORDER_ATTACKTARGET || psDroid->order.type == DORDER_ATTACK)
&& secondaryGetState(psDroid, DSO_HALTTYPE) == DSS_HALT_HOLD
&& !actionInRange(psDroid, psDroid->order.psObj, 0))
{
// target is not in range and DSS_HALT_HOLD: give up, don't move
psDroid->order = DroidOrder(DORDER_NONE);
}
else if (!psDroid->isVtol() || allVtolsRearmed(psDroid))
{
actionDroid(psDroid, DACTION_ATTACK, psDroid->order.psObj);
}
}
break;
case DORDER_BUILD:
if (psDroid->action == DACTION_BUILD &&
psDroid->order.psObj == nullptr)
{
psDroid->order = DroidOrder(DORDER_NONE);
actionDroid(psDroid, DACTION_NONE);
objTrace(psDroid->id, "Clearing build order since build target is gone");
}
else if (psDroid->action == DACTION_NONE)
{
psDroid->order = DroidOrder(DORDER_NONE);
objTrace(psDroid->id, "Clearing build order since build action is reset");
}
break;
case DORDER_EMBARK:
{
// only place it can be trapped - in multiPlayer can only put cyborgs onto a Cyborg Transporter
DROID *temp = (DROID *)psDroid->order.psObj; // NOTE: It is possible to have a NULL here
if (temp && temp->droidType == DROID_TRANSPORTER && !psDroid->isCyborg())
{
psDroid->order = DroidOrder(DORDER_NONE);
actionDroid(psDroid, DACTION_NONE);
if (psDroid->player == selectedPlayer)
{
audio_PlayBuildFailedOnce();
addConsoleMessage(_("We can't do that! We must be a Cyborg unit to use a Cyborg Transport!"), DEFAULT_JUSTIFY, selectedPlayer);
}
}
else
{
// don't want the droids to go into a formation for this order
if (psDroid->sMove.psFormation != nullptr)
{
formationLeave(psDroid->sMove.psFormation, psDroid);
psDroid->sMove.psFormation = nullptr;
}
// Wait for the action to finish then assign to Transporter (if not already flying)
if (psDroid->order.psObj == nullptr || transporterFlying((DROID *)psDroid->order.psObj))
{
psDroid->order = DroidOrder(DORDER_NONE);
actionDroid(psDroid, DACTION_NONE);
}
else if (abs((SDWORD)psDroid->pos.x - (SDWORD)psDroid->order.psObj->pos.x) < TILE_UNITS
&& abs((SDWORD)psDroid->pos.y - (SDWORD)psDroid->order.psObj->pos.y) < TILE_UNITS)
{
// save the target of current droid (the transporter)
DROID *transporter = (DROID *)psDroid->order.psObj;
// Make sure that it really is a valid droid
CHECK_DROID(transporter);
// order the droid to stop so moveUpdateDroid does not process this unit
orderDroid(psDroid, DORDER_STOP, ModeImmediate);
setDroidTarget(psDroid, nullptr);
psDroid->order.psObj = nullptr;
secondarySetState(psDroid, DSO_RETURN_TO_LOC, DSS_NONE);
moveReallyStopDroid(psDroid);
// Fire off embark event
transporterSetScriptCurrent(transporter);
triggerEvent(TRIGGER_TRANSPORTER_EMBARKED, transporter);
transporterSetScriptCurrent(nullptr);
/* We must add the droid to the transporter only *after*
* processing changing its orders (see above).
*/
transporterAddDroid(transporter, psDroid);
}
else if (psDroid->action == DACTION_NONE)
{
actionDroid(psDroid, DACTION_MOVE, psDroid->order.psObj->pos.x, psDroid->order.psObj->pos.y);
}
}
}
// Do we need to clear the secondary order "DSO_EMBARK" here? (PD)
break;
case DORDER_DISEMBARK:
//only valid in multiPlayer mode
if (bMultiPlayer)
{
//this order can only be given to Transporter droids
if (psDroid->isTransporter())
{
/*once the Transporter has reached its destination (and landed),
get all the units to disembark*/
if (psDroid->action != DACTION_MOVE && psDroid->action != DACTION_MOVEFIRE &&
psDroid->sMove.Status == MOVEINACTIVE && psDroid->sMove.iVertSpeed == 0)
{
unloadTransporter(psDroid, psDroid->pos.x, psDroid->pos.y, false);
//reset the transporter's order
psDroid->order = DroidOrder(DORDER_NONE);
}
}
}
break;
case DORDER_RTB:
// Just wait for the action to finish then clear the order
if (psDroid->action == DACTION_NONE)
{
psDroid->order = DroidOrder(DORDER_NONE);
secondarySetState(psDroid, DSO_RETURN_TO_LOC, DSS_NONE);
}
break;
case DORDER_RTR:
case DORDER_RTR_SPECIFIED:
// send them back to commander, no need to repair
if (!psDroid->isDamaged())
{
objTrace(psDroid->id, "was RTR, but we are full health");
droidWasFullyRepaired(psDroid, nullptr);
}
if (psDroid->order.psObj == nullptr)
{
objTrace(psDroid->id, "Our RTR target got lost. Let's try again.");
psDroid->order = DroidOrder(DORDER_NONE);
orderDroid(psDroid, DORDER_RTR, ModeImmediate);
}
else if (psDroid->action == DACTION_NONE)
{
/* get repair facility pointer */
psStruct = (STRUCTURE *)psDroid->order.psObj;
ASSERT(psStruct != nullptr, "orderUpdateUnit: invalid structure pointer");
if (objPosDiffSq(psDroid->pos, psDroid->order.psObj->pos) < REPAIR_RANGE * REPAIR_RANGE)
{
/* action droid to wait */
actionDroid(psDroid, DACTION_WAITFORREPAIR);
}
else