forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.cpp
2317 lines (2005 loc) · 62.9 KB
/
move.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-2019 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
*/
/*
* Move.c
*
* Routines for moving units about the map
*
*/
#include "lib/framework/frame.h"
#include "lib/framework/trig.h"
#include "lib/framework/math_ext.h"
#include "lib/gamelib/gtime.h"
#include "lib/netplay/netplay.h"
#include "lib/sound/audio.h"
#include "lib/sound/audio_id.h"
#include "lib/ivis_opengl/ivisdef.h"
#include "console.h"
#include "move.h"
#include "objects.h"
#include "visibility.h"
#include "map.h"
#include "fpath.h"
#include "loop.h"
#include "geometry.h"
#include "action.h"
#include "order.h"
#include "astar.h"
#include "mapgrid.h"
#include "display.h" // needed for widgetsOn flag.
#include "effects.h"
#include "power.h"
#include "scores.h"
#include "multiplay.h"
#include "multigifts.h"
#include "random.h"
#include "mission.h"
#include "qtscript.h"
/* max and min vtol heights above terrain */
#define VTOL_HEIGHT_MIN 250
#define VTOL_HEIGHT_LEVEL 300
#define VTOL_HEIGHT_MAX 350
// Maximum size of an object for collision
#define OBJ_MAXRADIUS (TILE_UNITS * 4)
// how long a shuffle can propagate before they all stop
#define MOVE_SHUFFLETIME 10000
// Length of time a droid has to be stationery to be considered blocked
#define BLOCK_TIME 6000
#define SHUFFLE_BLOCK_TIME 2000
// How long a droid has to be stationary before stopping trying to move
#define BLOCK_PAUSETIME 1500
#define BLOCK_PAUSERELEASE 500
// How far a droid has to move before it is no longer 'stationary'
#define BLOCK_DIST 64
// How far a droid has to rotate before it is no longer 'stationary'
#define BLOCK_DIR 90
// How far out from an obstruction to start avoiding it
#define AVOID_DIST (TILE_UNITS*2)
// Speed to approach a final way point, if possible.
#define MIN_END_SPEED 60
// distance from final way point to start slowing
#define END_SPEED_RANGE (3 * TILE_UNITS)
// how long to pause after firing a FOM_NO weapon
#define FOM_MOVEPAUSE 1500
// distance to consider droids for a shuffle
#define SHUFFLE_DIST (3*TILE_UNITS/2)
// how far to move for a shuffle
#define SHUFFLE_MOVE (2*TILE_UNITS/2)
/// Extra precision added to movement calculations.
#define EXTRA_BITS 8
#define EXTRA_PRECISION (1 << EXTRA_BITS)
/* Function prototypes */
static void moveUpdatePersonModel(DROID *psDroid, SDWORD speed, uint16_t direction);
const char *moveDescription(MOVE_STATUS status)
{
switch (status)
{
case MOVEINACTIVE : return "Inactive";
case MOVENAVIGATE : return "Navigate";
case MOVETURN : return "Turn";
case MOVEPAUSE : return "Pause";
case MOVEPOINTTOPOINT : return "P2P";
case MOVETURNTOTARGET : return "Turn2target";
case MOVEHOVER : return "Hover";
case MOVEWAITROUTE : return "Waitroute";
case MOVESHUFFLE : return "Shuffle";
}
return "Error"; // satisfy compiler
}
/** Set a target location in world coordinates for a droid to move to
* @return true if the routing was successful, if false then the calling code
* should not try to route here again for a while
* @todo Document what "should not try to route here again for a while" means.
*/
static bool moveDroidToBase(DROID *psDroid, UDWORD x, UDWORD y, bool bFormation, FPATH_MOVETYPE moveType)
{
FPATH_RETVAL retVal = FPR_OK;
CHECK_DROID(psDroid);
// in multiPlayer make Transporter move like the vtols
if (isTransporter(psDroid) && game.maxPlayers == 0)
{
fpathSetDirectRoute(psDroid, x, y);
psDroid->sMove.Status = MOVENAVIGATE;
psDroid->sMove.pathIndex = 0;
return true;
}
// NOTE: While Vtols can fly, then can't go through things, like the transporter.
else if ((game.maxPlayers > 0 && isTransporter(psDroid)))
{
fpathSetDirectRoute(psDroid, x, y);
retVal = FPR_OK;
}
else
{
retVal = fpathDroidRoute(psDroid, x, y, moveType);
}
if (retVal == FPR_OK)
{
// bit of a hack this - john
// if astar doesn't have a complete route, it returns a route to the nearest clear tile.
// the location of the clear tile is in DestinationX,DestinationY.
// reset x,y to this position so the formation gets set up correctly
x = psDroid->sMove.destination.x;
y = psDroid->sMove.destination.y;
objTrace(psDroid->id, "unit %d: path ok - base Speed %u, speed %d, target(%u|%d, %u|%d)",
(int)psDroid->id, psDroid->baseSpeed, psDroid->sMove.speed, x, map_coord(x), y, map_coord(y));
psDroid->sMove.Status = MOVENAVIGATE;
psDroid->sMove.pathIndex = 0;
}
else if (retVal == FPR_WAIT)
{
// the route will be calculated by the path-finding thread
psDroid->sMove.Status = MOVEWAITROUTE;
psDroid->sMove.destination.x = x;
psDroid->sMove.destination.y = y;
}
else // if (retVal == FPR_FAILED)
{
objTrace(psDroid->id, "Path to (%d, %d) failed for droid %d", (int)x, (int)y, (int)psDroid->id);
psDroid->sMove.Status = MOVEINACTIVE;
actionDroid(psDroid, DACTION_SULK);
return (false);
}
CHECK_DROID(psDroid);
return true;
}
/** Move a droid to a location, joining a formation
* @see moveDroidToBase() for the parameter and return value specification
*/
bool moveDroidTo(DROID *psDroid, UDWORD x, UDWORD y, FPATH_MOVETYPE moveType)
{
return moveDroidToBase(psDroid, x, y, true, moveType);
}
/** Move a droid to a location, not joining a formation
* @see moveDroidToBase() for the parameter and return value specification
*/
bool moveDroidToNoFormation(DROID *psDroid, UDWORD x, UDWORD y, FPATH_MOVETYPE moveType)
{
ASSERT_OR_RETURN(false, x > 0 && y > 0, "Bad movement position");
return moveDroidToBase(psDroid, x, y, false, moveType);
}
/** Move a droid directly to a location.
* @note This is (or should be) used for VTOLs only.
*/
void moveDroidToDirect(DROID *psDroid, UDWORD x, UDWORD y)
{
ASSERT_OR_RETURN(, psDroid != nullptr && isVtolDroid(psDroid), "Only valid for a VTOL unit");
fpathSetDirectRoute(psDroid, x, y);
psDroid->sMove.Status = MOVENAVIGATE;
psDroid->sMove.pathIndex = 0;
}
/** Turn a droid towards a given location.
*/
void moveTurnDroid(DROID *psDroid, UDWORD x, UDWORD y)
{
uint16_t moveDir = calcDirection(psDroid->pos.x, psDroid->pos.y, x, y);
if (psDroid->rot.direction != moveDir)
{
psDroid->sMove.target.x = x;
psDroid->sMove.target.y = y;
psDroid->sMove.Status = MOVETURNTOTARGET;
}
}
// Tell a droid to move out the way for a shuffle
static void moveShuffleDroid(DROID *psDroid, Vector2i s)
{
SDWORD mx, my;
bool frontClear = true, leftClear = true, rightClear = true;
SDWORD lvx, lvy, rvx, rvy, svx, svy;
SDWORD shuffleMove;
ASSERT_OR_RETURN(, psDroid != nullptr, "Bad droid pointer");
CHECK_DROID(psDroid);
uint16_t shuffleDir = iAtan2(s);
int32_t shuffleMag = iHypot(s);
if (shuffleMag == 0)
{
return;
}
shuffleMove = SHUFFLE_MOVE;
// calculate the possible movement vectors
svx = s.x * shuffleMove / shuffleMag; // Straight in the direction of s.
svy = s.y * shuffleMove / shuffleMag;
lvx = -svy; // 90° to the... right?
lvy = svx;
rvx = svy; // 90° to the... left?
rvy = -svx;
// check for blocking tiles
if (fpathBlockingTile(map_coord((SDWORD)psDroid->pos.x + lvx),
map_coord((SDWORD)psDroid->pos.y + lvy), getPropulsionStats(psDroid)->propulsionType))
{
leftClear = false;
}
else if (fpathBlockingTile(map_coord((SDWORD)psDroid->pos.x + rvx),
map_coord((SDWORD)psDroid->pos.y + rvy), getPropulsionStats(psDroid)->propulsionType))
{
rightClear = false;
}
else if (fpathBlockingTile(map_coord((SDWORD)psDroid->pos.x + svx),
map_coord((SDWORD)psDroid->pos.y + svy), getPropulsionStats(psDroid)->propulsionType))
{
frontClear = false;
}
// find any droids that could block the shuffle
static GridList gridList; // static to avoid allocations.
gridList = gridStartIterate(psDroid->pos.x, psDroid->pos.y, SHUFFLE_DIST);
for (GridIterator gi = gridList.begin(); gi != gridList.end(); ++gi)
{
DROID *psCurr = castDroid(*gi);
if (psCurr == nullptr || psCurr->died || psCurr == psDroid)
{
continue;
}
uint16_t droidDir = iAtan2((psCurr->pos - psDroid->pos).xy());
int diff = angleDelta(shuffleDir - droidDir);
if (diff > -DEG(135) && diff < -DEG(45))
{
leftClear = false;
}
else if (diff > DEG(45) && diff < DEG(135))
{
rightClear = false;
}
}
// calculate a target
if (leftClear)
{
mx = lvx;
my = lvy;
}
else if (rightClear)
{
mx = rvx;
my = rvy;
}
else if (frontClear)
{
mx = svx;
my = svy;
}
else
{
// nowhere to shuffle to, quit
return;
}
// check the location for vtols
Vector2i tar = psDroid->pos.xy() + Vector2i(mx, my);
if (isVtolDroid(psDroid))
{
actionVTOLLandingPos(psDroid, &tar);
}
// set up the move state
if (psDroid->sMove.Status != MOVESHUFFLE)
{
psDroid->sMove.shuffleStart = gameTime;
}
psDroid->sMove.Status = MOVESHUFFLE;
psDroid->sMove.src = psDroid->pos.xy();
psDroid->sMove.target = tar;
psDroid->sMove.asPath.clear();
psDroid->sMove.pathIndex = 0;
CHECK_DROID(psDroid);
}
/** Stop a droid from moving.
*/
void moveStopDroid(DROID *psDroid)
{
CHECK_DROID(psDroid);
PROPULSION_STATS *psPropStats = asPropulsionStats + psDroid->asBits[COMP_PROPULSION];
ASSERT_OR_RETURN(, psPropStats != nullptr, "invalid propulsion stats pointer");
if (psPropStats->propulsionType == PROPULSION_TYPE_LIFT)
{
psDroid->sMove.Status = MOVEHOVER;
}
else
{
psDroid->sMove.Status = MOVEINACTIVE;
}
}
/** Stops a droid dead in its tracks.
* Doesn't allow for any little skidding bits.
* @param psDroid the droid to stop from moving
*/
void moveReallyStopDroid(DROID *psDroid)
{
CHECK_DROID(psDroid);
psDroid->sMove.Status = MOVEINACTIVE;
psDroid->sMove.speed = 0;
}
#define PITCH_LIMIT 150
/* Get pitch and roll from direction and tile data */
void updateDroidOrientation(DROID *psDroid)
{
int32_t hx0, hx1, hy0, hy1;
int newPitch, deltaPitch, pitchLimit;
int32_t dzdx, dzdy, dzdv, dzdw;
const int d = 20;
int32_t vX, vY;
if (psDroid->droidType == DROID_PERSON || cyborgDroid(psDroid) || isTransporter(psDroid)
|| isFlying(psDroid))
{
/* The ground doesn't affect the pitch/roll of these droids*/
return;
}
// Find the height of 4 points around the droid.
// hy0
// hx0 * hx1 (* = droid)
// hy1
hx1 = map_Height(psDroid->pos.x + d, psDroid->pos.y);
hx0 = map_Height(MAX(0, psDroid->pos.x - d), psDroid->pos.y);
hy1 = map_Height(psDroid->pos.x, psDroid->pos.y + d);
hy0 = map_Height(psDroid->pos.x, MAX(0, psDroid->pos.y - d));
//update height in case were in the bottom of a trough
psDroid->pos.z = MAX(psDroid->pos.z, (hx0 + hx1) / 2);
psDroid->pos.z = MAX(psDroid->pos.z, (hy0 + hy1) / 2);
// Vector of length 65536 pointing in direction droid is facing.
vX = iSin(psDroid->rot.direction);
vY = iCos(psDroid->rot.direction);
// Calculate pitch of ground.
dzdx = hx1 - hx0; // 2*d*∂z(x, y)/∂x of ground
dzdy = hy1 - hy0; // 2*d*∂z(x, y)/∂y of ground
dzdv = dzdx * vX + dzdy * vY; // 2*d*∂z(x, y)/∂v << 16 of ground, where v is the direction the droid is facing.
newPitch = iAtan2(dzdv, (2 * d) << 16); // pitch = atan(∂z(x, y)/∂v)/2π << 16
deltaPitch = angleDelta(newPitch - psDroid->rot.pitch);
// Limit the rate the front comes down to simulate momentum
pitchLimit = gameTimeAdjustedIncrement(DEG(PITCH_LIMIT));
deltaPitch = MAX(deltaPitch, -pitchLimit);
// Update pitch.
psDroid->rot.pitch += deltaPitch;
// Calculate and update roll of ground (not taking pitch into account, but good enough).
dzdw = dzdx * vY - dzdy * vX; // 2*d*∂z(x, y)/∂w << 16 of ground, where w is at right angles to the direction the droid is facing.
psDroid->rot.roll = iAtan2(dzdw, (2 * d) << 16); // pitch = atan(∂z(x, y)/∂w)/2π << 16
}
struct BLOCKING_CALLBACK_DATA
{
PROPULSION_TYPE propulsionType;
bool blocking;
Vector2i src;
Vector2i dst;
};
static bool moveBlockingTileCallback(Vector2i pos, int32_t dist, void *data_)
{
BLOCKING_CALLBACK_DATA *data = (BLOCKING_CALLBACK_DATA *)data_;
data->blocking |= pos != data->src && pos != data->dst && fpathBlockingTile(map_coord(pos.x), map_coord(pos.y), data->propulsionType);
return !data->blocking;
}
// Returns -1 - distance if the direct path to the waypoint is blocked, otherwise returns the distance to the waypoint.
static int32_t moveDirectPathToWaypoint(DROID *psDroid, unsigned positionIndex)
{
Vector2i src(psDroid->pos.xy());
Vector2i dst = psDroid->sMove.asPath[positionIndex];
Vector2i delta = dst - src;
int32_t dist = iHypot(delta);
BLOCKING_CALLBACK_DATA data;
data.propulsionType = getPropulsionStats(psDroid)->propulsionType;
data.blocking = false;
data.src = src;
data.dst = dst;
rayCast(src, dst, &moveBlockingTileCallback, &data);
return data.blocking ? -1 - dist : dist;
}
// Returns true if still able to find the path.
static bool moveBestTarget(DROID *psDroid)
{
int positionIndex = std::max(psDroid->sMove.pathIndex - 1, 0);
int32_t dist = moveDirectPathToWaypoint(psDroid, positionIndex);
if (dist >= 0)
{
// Look ahead in the path.
while (dist >= 0 && dist < TILE_UNITS * 5)
{
++positionIndex;
if (positionIndex >= (int)psDroid->sMove.asPath.size())
{
dist = -1;
break; // Reached end of path.
}
dist = moveDirectPathToWaypoint(psDroid, positionIndex);
}
if (dist < 0)
{
--positionIndex;
}
}
else
{
// Lost sight of path, backtrack.
while (dist < 0 && dist >= -TILE_UNITS * 7 && positionIndex > 0)
{
--positionIndex;
dist = moveDirectPathToWaypoint(psDroid, positionIndex);
}
if (dist < 0)
{
return false; // Couldn't find path, and backtracking didn't help.
}
}
psDroid->sMove.pathIndex = positionIndex + 1;
psDroid->sMove.src = psDroid->pos.xy();
psDroid->sMove.target = psDroid->sMove.asPath[positionIndex];
return true;
}
/* Get the next target point from the route */
static bool moveNextTarget(DROID *psDroid)
{
CHECK_DROID(psDroid);
// See if there is anything left in the move list
if (psDroid->sMove.pathIndex == (int)psDroid->sMove.asPath.size())
{
return false;
}
ASSERT_OR_RETURN(false, psDroid->sMove.pathIndex >= 0 && psDroid->sMove.pathIndex < (int)psDroid->sMove.asPath.size(), "psDroid->sMove.pathIndex out of bounds %d/%d.", psDroid->sMove.pathIndex, (int)psDroid->sMove.asPath.size());
if (psDroid->sMove.pathIndex == 0)
{
psDroid->sMove.src = psDroid->pos.xy();
}
else
{
psDroid->sMove.src = psDroid->sMove.asPath[psDroid->sMove.pathIndex - 1];
}
psDroid->sMove.target = psDroid->sMove.asPath[psDroid->sMove.pathIndex];
++psDroid->sMove.pathIndex;
CHECK_DROID(psDroid);
return true;
}
// Watermelon:fix these magic number...the collision radius should be based on pie imd radius not some static int's...
static int mvPersRad = 20, mvCybRad = 30, mvSmRad = 40, mvMedRad = 50, mvLgRad = 60;
// Get the radius of a base object for collision
static SDWORD moveObjRadius(const BASE_OBJECT *psObj)
{
switch (psObj->type)
{
case OBJ_DROID:
{
const DROID *psDroid = (const DROID *)psObj;
if (psDroid->droidType == DROID_PERSON)
{
return mvPersRad;
}
else if (cyborgDroid(psDroid))
{
return mvCybRad;
}
else
{
const BODY_STATS *psBdyStats = &asBodyStats[psDroid->asBits[COMP_BODY]];
switch (psBdyStats->size)
{
case SIZE_LIGHT:
return mvSmRad;
case SIZE_MEDIUM:
return mvMedRad;
case SIZE_HEAVY:
return mvLgRad;
case SIZE_SUPER_HEAVY:
return 130;
default:
return psDroid->sDisplay.imd->radius;
}
}
break;
}
case OBJ_STRUCTURE:
return psObj->sDisplay.imd->radius / 2;
case OBJ_FEATURE:
return psObj->sDisplay.imd->radius / 2;
default:
ASSERT(false, "unknown object type");
return 0;
}
}
// see if a Droid has run over a person
static void moveCheckSquished(DROID *psDroid, int32_t emx, int32_t emy)
{
int32_t rad, radSq, objR, xdiff, ydiff, distSq;
const int32_t droidR = moveObjRadius((BASE_OBJECT *)psDroid);
const int32_t mx = gameTimeAdjustedAverage(emx, EXTRA_PRECISION);
const int32_t my = gameTimeAdjustedAverage(emy, EXTRA_PRECISION);
static GridList gridList; // static to avoid allocations.
gridList = gridStartIterate(psDroid->pos.x, psDroid->pos.y, OBJ_MAXRADIUS);
for (GridIterator gi = gridList.begin(); gi != gridList.end(); ++gi)
{
BASE_OBJECT *psObj = *gi;
if (psObj->type != OBJ_DROID || ((DROID *)psObj)->droidType != DROID_PERSON)
{
// ignore everything but people
continue;
}
ASSERT_OR_RETURN(, psObj->type == OBJ_DROID && ((DROID *)psObj)->droidType == DROID_PERSON, "squished - eerk");
objR = moveObjRadius(psObj);
rad = droidR + objR;
radSq = rad * rad;
xdiff = psDroid->pos.x + mx - psObj->pos.x;
ydiff = psDroid->pos.y + my - psObj->pos.y;
distSq = xdiff * xdiff + ydiff * ydiff;
if (((2 * radSq) / 3) > distSq)
{
if ((psDroid->player != psObj->player) && !aiCheckAlliances(psDroid->player, psObj->player))
{
// run over a bloke - kill him
destroyDroid((DROID *)psObj, gameTime);
scoreUpdateVar(WD_BARBARIANS_MOWED_DOWN);
}
}
}
}
// See if the droid has been stopped long enough to give up on the move
static bool moveBlocked(DROID *psDroid)
{
SDWORD xdiff, ydiff, diffSq;
UDWORD blockTime;
if (psDroid->sMove.bumpTime == 0 || psDroid->sMove.bumpTime > gameTime)
{
// no bump - can't be blocked
return false;
}
// See if the block can be cancelled
if (abs(angleDelta(psDroid->rot.direction - psDroid->sMove.bumpDir)) > DEG(BLOCK_DIR))
{
// Move on, clear the bump
psDroid->sMove.bumpTime = 0;
psDroid->sMove.lastBump = 0;
return false;
}
xdiff = (SDWORD)psDroid->pos.x - (SDWORD)psDroid->sMove.bumpPos.x;
ydiff = (SDWORD)psDroid->pos.y - (SDWORD)psDroid->sMove.bumpPos.y;
diffSq = xdiff * xdiff + ydiff * ydiff;
if (diffSq > BLOCK_DIST * BLOCK_DIST)
{
// Move on, clear the bump
psDroid->sMove.bumpTime = 0;
psDroid->sMove.lastBump = 0;
return false;
}
if (psDroid->sMove.Status == MOVESHUFFLE)
{
blockTime = SHUFFLE_BLOCK_TIME;
}
else
{
blockTime = BLOCK_TIME;
}
if (gameTime - psDroid->sMove.bumpTime > blockTime)
{
// Stopped long enough - blocked
psDroid->sMove.bumpTime = 0;
psDroid->sMove.lastBump = 0;
if (!isHumanPlayer(psDroid->player) && bMultiPlayer)
{
psDroid->lastFrustratedTime = gameTime;
objTrace(psDroid->id, "FRUSTRATED");
}
else
{
objTrace(psDroid->id, "BLOCKED");
}
// if the unit cannot see the next way point - reroute it's got stuck
if ((bMultiPlayer || psDroid->player == selectedPlayer || psDroid->lastFrustratedTime == gameTime)
&& psDroid->sMove.pathIndex != (int)psDroid->sMove.asPath.size())
{
objTrace(psDroid->id, "Trying to reroute to (%d,%d)", psDroid->sMove.destination.x, psDroid->sMove.destination.y);
moveDroidTo(psDroid, psDroid->sMove.destination.x, psDroid->sMove.destination.y);
return false;
}
return true;
}
return false;
}
// Calculate the actual movement to slide around
static void moveCalcSlideVector(DROID *psDroid, int32_t objX, int32_t objY, int32_t *pMx, int32_t *pMy)
{
int32_t dirX, dirY, dirMagSq, dotRes;
const int32_t mx = *pMx;
const int32_t my = *pMy;
// Calculate the vector to the obstruction
const int32_t obstX = psDroid->pos.x - objX;
const int32_t obstY = psDroid->pos.y - objY;
// if the target dir is the same, don't need to slide
if (obstX * mx + obstY * my >= 0)
{
return;
}
// Choose the tangent vector to this on the same side as the target
dotRes = obstY * mx - obstX * my;
if (dotRes >= 0)
{
dirX = obstY;
dirY = -obstX;
}
else
{
dirX = -obstY;
dirY = obstX;
dotRes = -dotRes;
}
dirMagSq = MAX(1, dirX * dirX + dirY * dirY);
// Calculate the component of the movement in the direction of the tangent vector
*pMx = (int64_t)dirX * dotRes / dirMagSq;
*pMy = (int64_t)dirY * dotRes / dirMagSq;
}
static void moveOpenGates(DROID *psDroid, Vector2i tile)
{
// is the new tile a gate?
if (!worldOnMap(tile.x, tile.y))
{
return;
}
MAPTILE *psTile = mapTile(tile);
if (!isFlying(psDroid) && psTile && psTile->psObject && psTile->psObject->type == OBJ_STRUCTURE && aiCheckAlliances(psTile->psObject->player, psDroid->player))
{
requestOpenGate((STRUCTURE *)psTile->psObject); // If it's a friendly gate, open it. (It would be impolite to open an enemy gate.)
}
}
static void moveOpenGates(DROID *psDroid)
{
Vector2i pos = psDroid->pos.xy() + iSinCosR(psDroid->sMove.moveDir, psDroid->sMove.speed * SAS_OPEN_SPEED / GAME_TICKS_PER_SEC);
moveOpenGates(psDroid, map_coord(pos));
}
// see if a droid has run into a blocking tile
// TODO See if this function can be simplified.
static void moveCalcBlockingSlide(DROID *psDroid, int32_t *pmx, int32_t *pmy, uint16_t tarDir, uint16_t *pSlideDir)
{
PROPULSION_TYPE propulsion = getPropulsionStats(psDroid)->propulsionType;
SDWORD horizX, horizY, vertX, vertY;
uint16_t slideDir;
// calculate the new coords and see if they are on a different tile
const int32_t mx = gameTimeAdjustedAverage(*pmx, EXTRA_PRECISION);
const int32_t my = gameTimeAdjustedAverage(*pmy, EXTRA_PRECISION);
const int32_t tx = map_coord(psDroid->pos.x);
const int32_t ty = map_coord(psDroid->pos.y);
const int32_t nx = psDroid->pos.x + mx;
const int32_t ny = psDroid->pos.y + my;
const int32_t ntx = map_coord(nx);
const int32_t nty = map_coord(ny);
const int32_t blkCX = world_coord(ntx) + TILE_UNITS / 2;
const int32_t blkCY = world_coord(nty) + TILE_UNITS / 2;
CHECK_DROID(psDroid);
// is the new tile a gate?
moveOpenGates(psDroid, Vector2i(ntx, nty));
// is the new tile blocking?
if (!fpathBlockingTile(ntx, nty, propulsion))
{
// not blocking, don't change the move vector
return;
}
// if the droid is shuffling - just stop
if (psDroid->sMove.Status == MOVESHUFFLE)
{
objTrace(psDroid->id, "Was shuffling, now stopped");
psDroid->sMove.Status = MOVEINACTIVE;
}
// note the bump time and position if necessary
if (!isVtolDroid(psDroid) &&
psDroid->sMove.bumpTime == 0)
{
psDroid->sMove.bumpTime = gameTime;
psDroid->sMove.lastBump = 0;
psDroid->sMove.pauseTime = 0;
psDroid->sMove.bumpPos = psDroid->pos;
psDroid->sMove.bumpDir = psDroid->rot.direction;
}
if (tx != ntx && ty != nty)
{
// moved diagonally
// figure out where the other two possible blocking tiles are
horizX = mx < 0 ? ntx + 1 : ntx - 1;
horizY = nty;
vertX = ntx;
vertY = my < 0 ? nty + 1 : nty - 1;
if (fpathBlockingTile(horizX, horizY, propulsion) && fpathBlockingTile(vertX, vertY, propulsion))
{
// in a corner - choose an arbitrary slide
if (gameRand(2) == 0)
{
*pmx = 0;
*pmy = -*pmy;
}
else
{
*pmx = -*pmx;
*pmy = 0;
}
}
else if (fpathBlockingTile(horizX, horizY, propulsion))
{
*pmy = 0;
}
else if (fpathBlockingTile(vertX, vertY, propulsion))
{
*pmx = 0;
}
else
{
moveCalcSlideVector(psDroid, blkCX, blkCY, pmx, pmy);
}
}
else if (tx != ntx)
{
// moved horizontally - see which half of the tile were in
if ((psDroid->pos.y & TILE_MASK) > TILE_UNITS / 2)
{
// top half
if (fpathBlockingTile(ntx, nty + 1, propulsion))
{
*pmx = 0;
}
else
{
moveCalcSlideVector(psDroid, blkCX, blkCY, pmx, pmy);
}
}
else
{
// bottom half
if (fpathBlockingTile(ntx, nty - 1, propulsion))
{
*pmx = 0;
}
else
{
moveCalcSlideVector(psDroid, blkCX, blkCY, pmx, pmy);
}
}
}
else if (ty != nty)
{
// moved vertically
if ((psDroid->pos.x & TILE_MASK) > TILE_UNITS / 2)
{
// top half
if (fpathBlockingTile(ntx + 1, nty, propulsion))
{
*pmy = 0;
}
else
{
moveCalcSlideVector(psDroid, blkCX, blkCY, pmx, pmy);
}
}
else
{
// bottom half
if (fpathBlockingTile(ntx - 1, nty, propulsion))
{
*pmy = 0;
}
else
{
moveCalcSlideVector(psDroid, blkCX, blkCY, pmx, pmy);
}
}
}
else // if (tx == ntx && ty == nty)
{
// on a blocking tile - see if we need to jump off
int intx = psDroid->pos.x & TILE_MASK;
int inty = psDroid->pos.y & TILE_MASK;
bool bJumped = false;
int jumpx = psDroid->pos.x;
int jumpy = psDroid->pos.y;
if (intx < TILE_UNITS / 2)
{
if (inty < TILE_UNITS / 2)
{
// top left
if ((mx < 0) && fpathBlockingTile(tx - 1, ty, propulsion))
{
bJumped = true;
jumpy = (jumpy & ~TILE_MASK) - 1;
}
if ((my < 0) && fpathBlockingTile(tx, ty - 1, propulsion))
{
bJumped = true;
jumpx = (jumpx & ~TILE_MASK) - 1;
}
}
else
{
// bottom left
if ((mx < 0) && fpathBlockingTile(tx - 1, ty, propulsion))
{
bJumped = true;
jumpy = (jumpy & ~TILE_MASK) + TILE_UNITS;
}
if ((my >= 0) && fpathBlockingTile(tx, ty + 1, propulsion))
{
bJumped = true;
jumpx = (jumpx & ~TILE_MASK) - 1;
}
}
}
else
{
if (inty < TILE_UNITS / 2)
{
// top right
if ((mx >= 0) && fpathBlockingTile(tx + 1, ty, propulsion))
{
bJumped = true;
jumpy = (jumpy & ~TILE_MASK) - 1;
}
if ((my < 0) && fpathBlockingTile(tx, ty - 1, propulsion))
{
bJumped = true;
jumpx = (jumpx & ~TILE_MASK) + TILE_UNITS;
}
}
else
{
// bottom right
if ((mx >= 0) && fpathBlockingTile(tx + 1, ty, propulsion))
{
bJumped = true;
jumpy = (jumpy & ~TILE_MASK) + TILE_UNITS;
}
if ((my >= 0) && fpathBlockingTile(tx, ty + 1, propulsion))
{
bJumped = true;
jumpx = (jumpx & ~TILE_MASK) + TILE_UNITS;
}
}
}
if (bJumped)
{
psDroid->pos.x = MAX(0, jumpx);
psDroid->pos.y = MAX(0, jumpy);
*pmx = 0;
*pmy = 0;
}
else
{
moveCalcSlideVector(psDroid, blkCX, blkCY, pmx, pmy);
}
}
slideDir = iAtan2(*pmx, *pmy);
if (ntx != tx)
{
// hit a horizontal block
if ((tarDir < DEG(90) || tarDir > DEG(270)) &&
(slideDir >= DEG(90) && slideDir <= DEG(270)))
{
slideDir = tarDir;
}
else if ((tarDir >= DEG(90) && tarDir <= DEG(270)) &&
(slideDir < DEG(90) || slideDir > DEG(270)))
{
slideDir = tarDir;
}