-
Notifications
You must be signed in to change notification settings - Fork 11
/
Opponent_Behaviour.cpp
2247 lines (1809 loc) · 65.7 KB
/
Opponent_Behaviour.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
/**************************************************************************
Opponent Behaviour.cpp - Functions relating to opponents's car behaviour
**************************************************************************/
/* ============= */
/* Include files */
/* ============= */
#include "dxstdafx.h"
#include <stdlib.h>
#include "StuntCarRacer.h"
#include "Opponent_Behaviour.h"
#include "Car_Behaviour.h"
#include "Track.h"
#include "3D_Engine.h"
/* ===== */
/* Debug */
/* ===== */
//#define TEST_AMIGA_RWP
//#define TEST_AMIGA_OPI
//#define TEST_AMIGA_MOTOS
//#define TEST_AMIGA_ROS
#define OPPONENT_SHADOW
//#define USE_OPP_CENTRE_POS
//#define CALC_FRONT_X_Z_FROM_SCRATCH
#if defined(DEBUG) || defined(_DEBUG)
extern FILE *out;
extern bool bTestKey;
#endif
/* ========= */
/* Constants */
/* ========= */
#ifdef linux
#undef FALSE
#undef TRUE
#endif
#define FALSE 0
#define TRUE 1
#define NUM_OPPONENTS (11)
#define NUM_X_SPANS (32)
#define LOCAL_Y_FACTOR 4
typedef enum
{
REAR_LEFT = 0,
REAR_RIGHT,
FRONT,
NUM_OPP_WHEEL_POSITIONS
} OppWheelPositionType;
/* =========== */
/* Global data */
/* =========== */
long opponentsID = NO_OPPONENT; // 0 to 10
long opponents_current_piece = 0; // use as opponents_road_section
bool player_close_to_opponent = FALSE;
bool opponent_behind_player = FALSE;
extern bool bSuperLeague;
extern unsigned char sections_car_can_be_put_on[]; // both array are used for opponents speed values computation
extern char Piece_Angle_And_Template[MAX_PIECES_PER_TRACK];
// SEB: The opponents_speed_values, that is pre-computed, is not used anymore and Opponents_Speed_Value function is used now
// Values for each piece of each track (Global because MoveDrawBridge() modifies the Draw Bridge values)
// NOTE: These are for the Standard league. Super league values are different
unsigned char opponents_speed_values[NUM_TRACKS][MAX_PIECES_PER_TRACK] =
{
{
/* Little Ramp data */
0x76,0x6c,0x62,0x58,0x7a,0x7a,0x70,0x66,0x5c,0x52,0x48,0x48,0x48,0x7a,0x7a,0x7a,
0x7a,0x7a,0x7a,0x7a,0x70,0x66,0x5c,0x52,0x48,0x48,0x48,0x48,0x78,0x6e,0x64,0x5a,
0x50,0x46,0x7a,0x70,0x66,0x5c,0x52,0x48,0x48,0x48,0x48,0x7c
},
{
/* Stepping Stones data */
0xf2,0xe8,0xde,0xd4,0x67,0x5d,0x53,0x49,0x3f,0x4b,0x41,0x41,0xc1,0xd2,0xc8,0xbe,
0xc7,0xbd,0xc5,0xbb,0xc4,0xba,0x55,0x4b,0x41,0x41,0x41,0x60,0x56,0x4c,0x42,0x7d,
0x7d,0x73,0x69,0x5f,0x55,0x4b,0x41,0x41,0x41,0xfd,0xfd,0xfd,0xf3,0x7d,0x7d,0x73,
0x69,0x5f,0x55,0x4b,0x41,0x41,0x41,0x7c
},
{
/* Hump Back data */
0x52,0x4d,0x77,0x77,0x77,0x6d,0x63,0x59,0x4f,0x45,0x45,0x45,0x77,0x77,0x77,0x77,
0x77,0x77,0x77,0x6d,0x63,0x59,0x4f,0x45,0x45,0x45,0x56,0x4c,0x77,0x77,0x6d,0x63,
0x59,0x4f,0x45,0x45,0x45,0x4f,0x61,0x57,0x4d,0x45,0x4f,0x45,0x45,0x63,0x59,0x4f,
0x45,0x45,0x45,0x66,0x5c
},
{
/* Big Ramp data */
0x7a,0x7a,0x7a,0x7a,0x7a,0x7a,0x70,0x66,0x5c,0x52,0x48,0x48,0x48,0x58,0x4e,0x4b,
0x69,0x5f,0x55,0x4b,0x46,0x66,0x5c,0x52,0x48,0x48,0x48,0x48,0x7e,0xf4,0xea,0xe0,
0xd6,0x7a,0x7a,0x70,0x66,0x5c,0x52,0x48,0x48,0x48,0x48,0x7c
},
{
/* Ski Jump data */
0x42,0xec,0xe2,0xd8,0x77,0x77,0x77,0x6d,0x63,0x59,0x4f,0x4f,0x4f,0x4f,0x63,0x59,
0x4f,0x4f,0x72,0x68,0x5e,0x54,0x4a,0x40,0x36,0x4f,0x4f,0x4f,0x6a,0xe0,0xd6,0xcc,
0xc2,0x63,0x59,0x4f,0xcf,0xcf,0xcf,0xc9,0x56,0x56,0x56,0x7e,0x7e,0x7e,0x7e,0x7e,
0x7e,0x74,0x6a,0x60,0x56,0x56,0x56,0x56,0x56,0x7e,0x7e,0x7e,0x7e,0x7e,0x7e,0x74,
0x6a,0x60,0x56,0x56,0x56,0x7e,0x7e,0x74,0x6a,0x60,0x56,0x56,0x5a,0x52
},
{
/* Draw Bridge data */
0x76,0x76,0x6c,0x62,0x69,0x5f,0x55,0x50,0x58,0x58,0x58,0x76,0x76,0x76,0x6c,0x62,
0x58,0x58,0x58,0x4d,0x43,0x76,0x76,0x76,0x76,0x76,0x6c,0x62,0x58,0x58,0x58,0x58,
0x58,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0xf8,0xee,0xe4,0x5a,0x50,0xc6,
0x76,0x76,0x76,0xbb,0xbb,0x76,0x76,0x76,0x6c,0x62,0xd8,0xd8,0xd8,0xe4,0xf6,0xec,
0xe2,0xd8,0x76,0x76,0x76,0x76,0x6c,0x62,0x58,0x58,0x58,0x58,0x58,0x7c
},
{
/* High Jump data */
0xe7,0xdd,0xd3,0x77,0x77,0x77,0x77,0x6d,0x63,0x59,0x4f,0x4f,0x4f,0x7a,0x7a,0x7a,
0x7a,0x7a,0x70,0x66,0x5c,0x52,0x52,0x55,0x59,0x4f,0x4f,0x77,0x77,0x77,0x77,0x6d,
0x63,0x59,0x4f,0x4f,0xcf,0xe7,0xdd,0xd3,0xce,0x77,0x77,0x77,0x77,0x6d,0x63,0x59,
0x4f,0x4f,0x4f,0x7c,0x41,0x41,0x41,0x7c
},
{
/* Roller Coaster data */
0x66,0x5c,0x52,0x48,0x3e,0x34,0x2a,0x29,0x6a,0x60,0x56,0x56,0x56,0x40,0x36,0x7e,
0x7e,0x7e,0x7e,0x7e,0x7e,0x74,0x6a,0x60,0x56,0x56,0x54,0x4a,0x7e,0x7e,0x7e,0x7e,
0x7e,0x7e,0x7e,0x74,0x6a,0x60,0x56,0x56,0x56,0x56,0x56,0x7e,0x7e,0x7e,0x7e,0x7e,
0x7e,0x74,0x6a,0x60,0x56,0x56,0x56,0x56,0x56,0x7e,0x7e,0x7e,0x7e,0x7e,0x7e,0x74,
0x6a,0x60,0x56,0x56,0x56,0x7e,0x7e,0x74,0x6a,0x60,0x56,0x56,0x5a,0x52
}
};
WCHAR *opponentNames[NUM_OPPONENTS] =
{
L"Hot Rod ",
L"Whizz Kid ",
L"Bad Guy ",
L"The Dodger ",
L"Big Ed ",
L"Max Boost ",
L"Dare Devil ",
L"High Flyer ",
L"Bully Boy ",
L"Jumping Jack",
L"Road Hog "
};
extern IDirectSoundBuffer8 *HitCarSoundBuffer;
/* =========== */
/* Static data */
/* =========== */
// Opponent attributes
#define OBSTRUCTS_PLAYER 2
#define WHEELIE 4
#define DRIVES_NEAR_EDGE 8
#define UNUSED4 16
#define PUSH_PLAYER 32
#define UNUSED6 64
static unsigned char opponent_attributes[NUM_OPPONENTS] =
{
// Hot Rod
PUSH_PLAYER|OBSTRUCTS_PLAYER,
// Whizz Kid
PUSH_PLAYER,
// Bad Guy
UNUSED6|PUSH_PLAYER|OBSTRUCTS_PLAYER,
// The Dodger
PUSH_PLAYER,
// Big Ed
PUSH_PLAYER|UNUSED4|DRIVES_NEAR_EDGE|WHEELIE|OBSTRUCTS_PLAYER,
// Max Boost
WHEELIE,
// Dare Devil
PUSH_PLAYER|UNUSED4,
// High Flyer
UNUSED4|WHEELIE,
// Bully Boy
UNUSED6|DRIVES_NEAR_EDGE|OBSTRUCTS_PLAYER,
// Jumping Jack
UNUSED4,
// Road Hog
DRIVES_NEAR_EDGE
};
// Values for each track
static unsigned char opp_track_speed_values[] = //DAT.1fe2c
{
// Standard league
0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,
0x41,0x3a,0x3e,0x41,0x48,0x51,0x48,0x4f,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // used when creating opponents.speed.values
0x48,0x41,0x45,0x48,0x4f,0x58,0x4f,0x56, // used when creating opponents.speed.values
// Super league
0x07,0x03,0x03,0x03,0x03,0x03,0x07,0x03,
0x66,0x57,0x57,0x59,0x59,0x69,0x62,0x64,
0x07,0x03,0x03,0x03,0x03,0x01,0x03,0x03, // used when creating opponents.speed.values
0x61,0x55,0x53,0x56,0x58,0x5b,0x5a,0x62 // used when creating opponents.speed.values
};
static long opponents_distance_into_section;
static long opponents_road_x_position;
// Three co-ordinates needed for opponent behaviour (as per original Amiga StuntCarRacer)
static COORD_3D opp_rear_left_road_pos;
static COORD_3D opp_rear_right_road_pos;
static long opp_front_road_pos_y; //X,Z not needed
// Additional co-ordinates needed for PC StuntCarRacer (for calculating opponent orientation)
static COORD_3D opp_front_left_road_pos;
static COORD_3D opp_front_right_road_pos;
#ifdef USE_OPP_CENTRE_POS
COORD_XZ opp_centre_road_pos;
#endif
static COORD_3D opp_shadow_rear_left;
static COORD_3D opp_shadow_rear_right;
static COORD_3D opp_shadow_front_left;
static COORD_3D opp_shadow_front_right;
// wheel heights
static long opp_actual_height[NUM_OPP_WHEEL_POSITIONS];
static long opp_smallest_difference;
static long opp_old_rear_left_difference;
static long opp_old_rear_right_difference;
static long opp_old_front_difference;
static long opp_new_rear_left_difference;
static long opp_new_rear_right_difference;
static long opp_new_front_difference;
static long opp_touching_road;
static long opp_y_acceleration[NUM_OPP_WHEEL_POSITIONS];
static long opp_y_speed[NUM_OPP_WHEEL_POSITIONS];
long opp_engine_power = 236; // (236 standard, 314 super)
static long opponents_engine_z_acceleration;
static long opponents_max_speed;
static long opponents_z_speed;
static bool opponents_required_z_speed_reached;
/* ===================== */
/* Function declarations */
/* ===================== */
static void ResetOpponent (void);
static void CalculateOpponentsRoadWheelPositions( void );
static void GetSurfaceCoords( long piece, long segment );
static long CalcSurfacePosition( long *next_segment, long distance, long z_shift );
static void CalculateOpponentsRoadWheelHeight( long sx, long sz, long *y_out );
static void OpponentMovement( void );
static void UpdateOpponentsActualWheelHeights( void );
static void CalculateWheelDifference( long road_height,
long actual_height,
long height_adjust,
long *old_difference_in_out,
long *new_difference_out,
long *touching_road);
static long LimitOpponentWheels( long max_difference, long wheel1, long wheel2 );
static void AverageWheelYSpeeds( long wheel1, long wheel2 );
static void RandomizeOpponentsSteering( void );
static void GetOpponentsEngineAcceleration( void );
static void AdjustOpponentsEngineAcceleration( void );
static void UpdateOpponentsZSpeed( void );
static void CalculateDistancesBetweenPlayers( void );
static void OpponentPlayerInteraction( void );
static void MoveOpponentToOneSide( void );
static void OpponentPushPlayer( void );
/* ======================================================================================= */
/* Function: ResetOpponent */
/* */
/* Description: Reset all opponent behaviour variables to their initial state */
/* ======================================================================================= */
static void ResetOpponent (void)
{
opponentsID = rand() % NUM_OPPONENTS;
// opponentsID = 9; // Jumping Jack
opp_old_rear_left_difference = 0;
opp_old_rear_right_difference = 0;
opp_old_front_difference = 0;
for (long i = 0; i < NUM_OPP_WHEEL_POSITIONS; i++)
{
opp_y_speed[i] = 0;
}
opponents_z_speed = 0;
opponents_required_z_speed_reached = FALSE;
player_close_to_opponent = FALSE;
opponent_behind_player = FALSE;
return;
}
/* ======================================================================================= */
/* Function: OpponentBehaviour */
/* */
/* Description: */
/* ======================================================================================= */
extern bool bNewGame;
extern long TrackID;
extern TRACK_PIECE Track[MAX_PIECES_PER_TRACK];
extern long Track_Map[NUM_TRACK_CUBES][NUM_TRACK_CUBES]; // [x][z]
extern long NumTrackPieces;
extern long PlayersStartPiece;
//#define USE_ROAD_Y
#define NEW_OPP_METHOD
// current surface co-ords
static long sx1, sy1, sz1, sx2, sy2, sz2, sx3, sy3, sz3, sx4, sy4, sz4;
void OpponentBehaviour (long *x,
long *y,
long *z,
float *x_angle,
float *y_angle,
float *z_angle,
bool bOpponentPaused)
{
long opponent_x, opponent_y, opponent_z;
float opponent_x_angle = 0.0f, opponent_y_angle = 0.0f, opponent_z_angle = 0.0f;
// reset opponent
if (bNewGame)
{
ResetOpponent();
opponents_current_piece = PlayersStartPiece;
opponents_distance_into_section = 0x400; // half way into section
opponents_road_x_position = 0x4c;
//temp opponents_road_x_position = 0x1c;
//temp opponents_road_x_position = 0xe4;
// initialise.opponent.data
CalculateOpponentsRoadWheelPositions();
// Position the opponent a random amount above the road
int r = rand();
r &= 0x7f;
r += 0x68;
opp_actual_height[REAR_LEFT] = opp_rear_left_road_pos.y + r;
opp_actual_height[REAR_RIGHT] = opp_rear_right_road_pos.y + r;
opp_actual_height[FRONT] = opp_front_road_pos_y + r;
// end initialise.opponent.data
// Set opponent_max_speed
long s = (long)rand() & (long)opp_track_speed_values[TrackID+(bSuperLeague?32:0)];
s += (long)opp_track_speed_values[TrackID+8+(bSuperLeague?32:0)];
opponents_max_speed = s;
//temp opponents_max_speed = 10;
bNewGame = FALSE;
}
CalculatePlayersRoadPosition();
if (!bOpponentPaused)
{
OpponentMovement();
CalculateDistancesBetweenPlayers();
OpponentPlayerInteraction();
}
else
CalculateDistancesBetweenPlayers();
CalculateOpponentsRoadWheelPositions();
//
// Calculate opponent's new centre point ...
//
/*
* Calculate opponent's x position
*/
opponent_x = (opp_front_left_road_pos.x + opp_front_right_road_pos.x + opp_rear_left_road_pos.x + opp_rear_right_road_pos.x) / 4;
opponent_x <<= LOG_PRECISION;
#ifdef USE_OPP_CENTRE_POS
// VALUE1 = (bTestKey ? 1 : 0);
VALUE2 = VALUE3 = 0;
if (bTestKey)
{
VALUE2 = opponent_x;
opponent_x = opp_centre_road_pos.x;
opponent_x <<= LOG_PRECISION;
VALUE3 = opponent_x;
}
#endif
/*
* Calculate opponent's y position
*/
#ifdef NEW_OPP_METHOD
long vis_rear_left_y, vis_rear_right_y, vis_front_y;
vis_rear_left_y = opp_rear_left_road_pos.y > opp_actual_height[REAR_LEFT] ? opp_rear_left_road_pos.y : opp_actual_height[REAR_LEFT];
vis_rear_right_y = opp_rear_right_road_pos.y > opp_actual_height[REAR_RIGHT] ? opp_rear_right_road_pos.y : opp_actual_height[REAR_RIGHT];
vis_front_y = opp_front_road_pos_y > opp_actual_height[FRONT] ? opp_front_road_pos_y : opp_actual_height[FRONT];
long rear_y = (vis_rear_left_y + vis_rear_right_y) / 2;
opponent_y = (rear_y + vis_front_y) / 2;
#else
long road_y = (opp_rear_left_road_pos.y + opp_rear_right_road_pos.y) / 2;
road_y = (road_y + opp_front_road_pos_y) / 2;
#ifdef USE_ROAD_Y
long rear_y = (opp_rear_left_road_pos.y + opp_rear_right_road_pos.y) / 2;
opponent_y = (rear_y + opp_front_road_pos_y) / 2;
#else
long rear_y = (opp_actual_height[REAR_LEFT] + opp_actual_height[REAR_RIGHT]) / 2;
opponent_y = (rear_y + opp_actual_height[FRONT]) / 2;
#endif
if (opponent_y < road_y) opponent_y = road_y;
#endif
// Raise the opponent slightly (to stop them sinking into road due to inaccurate heights)
opponent_y += 20;
opponent_y <<= (LOG_PRECISION-3);
/*
* Calculate opponent's z position
*/
opponent_z = (opp_front_left_road_pos.z + opp_front_right_road_pos.z + opp_rear_left_road_pos.z + opp_rear_right_road_pos.z) / 4;
opponent_z <<= LOG_PRECISION;
#ifdef USE_OPP_CENTRE_POS
if (bTestKey)
{
opponent_z = opp_centre_road_pos.z;
opponent_z <<= LOG_PRECISION;
}
#endif
//
// Calculate opponent's new angles
//
// Along car's x axis, only use y and z components
#ifdef USE_ROAD_Y
double yd = (double)(rear_y - opp_front_road_pos_y) / 2; // Note y is halved because of unit differences between y and x,z
#else
#ifdef NEW_OPP_METHOD
double yd = (double)(rear_y - vis_front_y) / 2; // Note y is halved because of unit differences between y and x,z
#else
double yd = (double)(rear_y - opp_actual_height[FRONT]) / 2; // Note y is halved because of unit differences between y and x,z
#endif
#endif
long rear_x = (opp_rear_left_road_pos.x + opp_rear_right_road_pos.x) / 2;
long rear_z = (opp_rear_left_road_pos.z + opp_rear_right_road_pos.z) / 2;
long front_x = (opp_front_left_road_pos.x + opp_front_right_road_pos.x) / 2;
long front_z = (opp_front_left_road_pos.z + opp_front_right_road_pos.z) / 2;
double xd = (double)(rear_x - front_x);
double zd = (double)(rear_z - front_z);
double carzd = sqrt((xd*xd) + (zd*zd));
opponent_x_angle = (float)atan2(yd, carzd);
// Along car's y axis, only use x and z components
xd = (double)(opp_rear_left_road_pos.x - opp_rear_right_road_pos.x);
zd = (double)(opp_rear_left_road_pos.z - opp_rear_right_road_pos.z);
// opponent_y_angle = (float)atan2(xd, zd) + D3DX_PI/2;
opponent_y_angle = (float)atan2(zd, -xd);
// Along car's z axis, only use x and y components
#ifdef USE_ROAD_Y
yd = (double)(opp_rear_left_road_pos.y - opp_rear_right_road_pos.y) / 2; // Note y is halved because of unit differences between y and x,z
#else
#ifdef NEW_OPP_METHOD
yd = (double)(vis_rear_left_y - vis_rear_right_y) / 2; // Note y is halved because of unit differences between y and x,z
#else
yd = (double)(opp_actual_height[REAR_LEFT] - opp_actual_height[REAR_RIGHT]) / 2; // Note y is halved because of unit differences between y and x,z
#endif
#endif
double carxd = sqrt((xd*xd) + (zd*zd));
opponent_z_angle = (float)atan2(-yd, carxd);
// output opponent values for use by functions that draw the world
*x = opponent_x;
*y = -(opponent_y * LOCAL_Y_FACTOR);
*z = opponent_z;
*x_angle = opponent_x_angle;
*y_angle = opponent_y_angle;
*z_angle = opponent_z_angle;
}
/* ======================================================================================= */
/* Function: CalculateOpponentsRoadWheelPositions */
/* */
/* Description: */
/* ======================================================================================= */
// current surface co-ords
//static long sx1, sy1, sz1, sx2, sy2, sz2, sx3, sy3, sz3, sx4, sy4, sz4;
static long B1bbbe[3] = {0,0,0}; // set by randomize.opponents.steering
// third value is opponents.random.steering.count
static long opponents_x_spans[NUM_X_SPANS] =
{27,27,27,27,27,26,26,26,25,25,25,24,23,23,22,21,20,19,18,17,15,14,11,9,7,7,7,7,7,7,7,7};
#ifdef OPPONENT_SHADOW
extern void RemoveShadowTriangles( void );
extern void StoreShadowTriangle( D3DXVECTOR3 v1, D3DXVECTOR3 v2, D3DXVECTOR3 v3, long other_colour );
#endif
// All three road heights tested against Amiga
static void CalculateOpponentsRoadWheelPositions( void )
{
long distance, segment, surface_position;
long piece = opponents_current_piece, next_segment;
long left_side_x, left_side_z, right_side_x, right_side_z;
bool draw_shadow = TRUE;
/*
* Rear wheels
*/
#ifdef TEST_AMIGA_RWP
if (GetRecordedAmigaWord(&piece))
++VALUE2;
GetRecordedAmigaWord(&opponents_distance_into_section);
GetRecordedAmigaWord(&opponents_road_x_position);
GetRecordedAmigaWord(&opp_actual_height[REAR_LEFT]);
GetRecordedAmigaWord(&opp_actual_height[REAR_RIGHT]);
GetRecordedAmigaWord(&opp_actual_height[FRONT]);
#endif
// Rear wheel position
distance = opponents_distance_into_section - 64;
if (distance < 0)
{
// DIRECTION DEPENDANT
// go to previous piece
piece--; if (piece < 0) piece = (NumTrackPieces - 1);
distance += (Track[piece].numSegments * 256);
}
#ifdef TEST_AMIGA_RWP
CompareRecordedAmigaWord("opponents.road.section.m64", &piece);
CompareRecordedAmigaWord("opponents.distance.into.section.minus64", &distance);
#endif
// Fetch 4 surface co-ords surrounding rear wheels (opponents.distance.into.section.minus64 / 256)
segment = distance >> 8;
GetSurfaceCoords(piece, segment);
// Don't draw opponent's shadow on black road segments
if (Track[piece].roadColour[segment] == SCR_BASE_COLOUR + 0)
draw_shadow = FALSE;
// Calculate segment left side x,z at opponents.distance.into.section.minus64
surface_position = CalcSurfacePosition(&next_segment, distance, B1bbbe[0]);
if (!next_segment)
{
left_side_x = sx2 + ((surface_position * (sx1-sx2)) >> 8);
left_side_z = sz2 + ((surface_position * (sz1-sz2)) >> 8);
}
else
{
// Use other end's value as base
// (Amiga StuntCarRacer does this, but not correct as should really use next segment's values)
left_side_x = sx1 + ((surface_position * (sx1-sx2)) >> 8);
left_side_z = sz1 + ((surface_position * (sz1-sz2)) >> 8);
}
// Calculate segment right side x,z at opponents.distance.into.section.minus64
surface_position = CalcSurfacePosition(&next_segment, distance, B1bbbe[2]);
if (!next_segment)
{
right_side_x = sx3 + ((surface_position * (sx4-sx3)) >> 8);
right_side_z = sz3 + ((surface_position * (sz4-sz3)) >> 8);
}
else
{
// Use other end's value as base
// (Amiga StuntCarRacer does this, but not correct as should really use next segment's values)
right_side_x = sx4 + ((surface_position * (sx4-sx3)) >> 8);
right_side_z = sz4 + ((surface_position * (sz4-sz3)) >> 8);
}
next_segment = FALSE;
long i = abs(opp_actual_height[REAR_LEFT] - opp_actual_height[REAR_RIGHT]) >> 4;
// Get half the x distance that the opponent's rear wheels span
if (i < 0) i = 0;
if (i >= NUM_X_SPANS) i = NUM_X_SPANS-1;
long opponents_x_span = opponents_x_spans[i];
// For calculating the opponent's shadow co-ordinates, the original StuntCarRacer spans are slightly
// too big, so need to be reduced to take into account the greater width of sloped segments
long xd = right_side_x - left_side_x;
long yd = (sy3 - sy2) / LOCAL_Y_FACTOR;
long zd = right_side_z - left_side_z;
long base_width = (long)(sqrt((double)((xd*xd) + (zd*zd))));
long slope_width = (long)(sqrt((double)((base_width*base_width) + (yd*yd))));
long opponents_shadow_x_span = (opponents_x_span * base_width) / slope_width;
// if (!bTestKey)
// opponents_shadow_x_span = opponents_x_span;
long sx, sz;
sz = distance & 0xff; // z position of rear wheels
// Calculate rear left road co-ordinate (as per Amiga opp.rear.left.road.height)
sx = opponents_road_x_position - opponents_x_span; // x position of rear left wheel
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_rear_left_road_pos.y);
opp_rear_left_road_pos.x = left_side_x + ((sx * xd) >> 8);
opp_rear_left_road_pos.z = left_side_z + ((sx * zd) >> 8);
#ifdef TEST_AMIGA_RWP
CompareRecordedAmigaWord("opp.rear.left.road.height", &opp_rear_left_road_pos.y);
#endif
// Calculate rear left shadow co-ordinate
sx = opponents_road_x_position - opponents_shadow_x_span;
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_shadow_rear_left.y);
opp_shadow_rear_left.x = left_side_x + ((sx * xd) >> 8);
opp_shadow_rear_left.z = left_side_z + ((sx * zd) >> 8);
// Calculate rear right road co-ordinate (as per Amiga opp.rear.right.road.height)
sx = opponents_road_x_position + opponents_x_span; // x position of rear right wheel
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_rear_right_road_pos.y);
opp_rear_right_road_pos.x = left_side_x + ((sx * xd) >> 8);
opp_rear_right_road_pos.z = left_side_z + ((sx * zd) >> 8);
#ifdef TEST_AMIGA_RWP
CompareRecordedAmigaWord("opp.rear.right.road.height", &opp_rear_right_road_pos.y);
#endif
// Calculate rear right shadow co-ordinate
sx = opponents_road_x_position + opponents_shadow_x_span;
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_shadow_rear_right.y);
opp_shadow_rear_right.x = left_side_x + ((sx * xd) >> 8);
opp_shadow_rear_right.z = left_side_z + ((sx * zd) >> 8);
long piece_x, piece_y, piece_z;
// Calculate position of piece's bottom front left corner, within world
piece_x = Track[piece].x << (LOG_CUBE_SIZE-LOG_PRECISION);
piece_y = Track[piece].y << (LOG_CUBE_SIZE-LOG_PRECISION);
piece_z = Track[piece].z << (LOG_CUBE_SIZE-LOG_PRECISION);
// Position rear road co-ordinates within world
opp_rear_left_road_pos.x += piece_x;
opp_rear_right_road_pos.x += piece_x;
opp_rear_left_road_pos.z += piece_z;
opp_rear_right_road_pos.z += piece_z;
// Position rear shadow co-ordinates within world
opp_shadow_rear_left.x += piece_x;
opp_shadow_rear_right.x += piece_x;
opp_shadow_rear_left.z += piece_z;
opp_shadow_rear_right.z += piece_z;
//****************
#ifdef USE_OPP_CENTRE_POS
long cdistance = opponents_distance_into_section;
// Fetch 4 surface co-ords surrounding centre point
segment = cdistance >> 8;
GetSurfaceCoords(opponents_current_piece, segment);
surface_position = cdistance & 0xff;
left_side_x = sx2 + ((surface_position * (sx1-sx2)) >> 8);
left_side_z = sz2 + ((surface_position * (sz1-sz2)) >> 8);
right_side_x = sx3 + ((surface_position * (sx4-sx3)) >> 8);
right_side_z = sz3 + ((surface_position * (sz4-sz3)) >> 8);
sx = opponents_road_x_position & 0xff; // x position of front wheel
// Calculate centre road x,z co-ordinates
opp_centre_road_pos.x = left_side_x + ((sx * (right_side_x-left_side_x)) >> 8);
opp_centre_road_pos.z = left_side_z + ((sx * (right_side_z-left_side_z)) >> 8);
// Calculate position of piece's bottom front left corner, within world
piece_x = Track[opponents_current_piece].x << (LOG_CUBE_SIZE-LOG_PRECISION);
piece_z = Track[opponents_current_piece].z << (LOG_CUBE_SIZE-LOG_PRECISION);
// Position centre road co-ordinates within world
opp_centre_road_pos.x += piece_x;
opp_centre_road_pos.z += piece_z;
#endif
//****************
/*
* Front wheels
*/
long diff, xdiff, zdiff;
// Calculate front left and right road x,z co-ordinates
diff = opp_rear_right_road_pos.x - opp_rear_left_road_pos.x;
xdiff = diff + (diff >> 1); // car length is 1.5 times width
diff = opp_rear_right_road_pos.z - opp_rear_left_road_pos.z;
zdiff = diff + (diff >> 1); // car length is 1.5 times width
opp_front_left_road_pos.x = opp_rear_left_road_pos.x - zdiff;
opp_front_left_road_pos.z = opp_rear_left_road_pos.z + xdiff;
opp_front_right_road_pos.x = opp_rear_right_road_pos.x - zdiff;
opp_front_right_road_pos.z = opp_rear_right_road_pos.z + xdiff;
/* Don't need to add piece_x,piece_z as already have world position (from rear wheels) */
// Calculate front left and right shadow x,z co-ordinates
diff = opp_shadow_rear_right.x - opp_shadow_rear_left.x;
xdiff = diff + (diff >> 1); // car length is 1.5 times width
diff = opp_shadow_rear_right.z - opp_shadow_rear_left.z;
zdiff = diff + (diff >> 1); // car length is 1.5 times width
opp_shadow_front_left.x = opp_shadow_rear_left.x - zdiff;
opp_shadow_front_left.z = opp_shadow_rear_left.z + xdiff;
opp_shadow_front_right.x = opp_shadow_rear_right.x - zdiff;
opp_shadow_front_right.z = opp_shadow_rear_right.z + xdiff;
/* Don't need to add piece_x,piece_z as already have world position (from rear wheels) */
// Add 128 to get z of opponent's front
distance += 128;
if (distance >= (Track[piece].numSegments * 256))
{
// DIRECTION DEPENDANT
distance -= (Track[piece].numSegments * 256);
// go to next piece
piece++; if (piece > (NumTrackPieces - 1)) piece = 0;
}
// Fetch 4 surface co-ords surrounding front wheels
segment = distance >> 8;
GetSurfaceCoords(piece, segment);
// Don't draw opponent's shadow on black road segments
if (Track[piece].roadColour[segment] == SCR_BASE_COLOUR + 0)
draw_shadow = FALSE;
/* temporary code
#ifdef TEST_AMIGA_RWP
long tsx, tsz, ty1, ty2, ty3, ty4;
GetRecordedAmigaWord(&tsx);
GetRecordedAmigaWord(&tsz);
GetRecordedAmigaWord(&ty1);
GetRecordedAmigaWord(&ty2);
GetRecordedAmigaWord(&ty3);
GetRecordedAmigaWord(&ty4);
#endif
*/
sz = distance & 0xff; // z position of front wheel
sx = opponents_road_x_position & 0xff; // x position of front wheel
// Calculate front road y co-ordinate (as per Amiga opp.front.road.height)
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_front_road_pos_y);
#ifdef TEST_AMIGA_RWP
CompareRecordedAmigaWord("opp.front.road.height", &opp_front_road_pos_y);
#endif
// Calculate front left road y co-ordinate
sx = opponents_road_x_position - opponents_x_span; // x position of front left wheel
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_front_left_road_pos.y);
// Calculate front right road y co-ordinate
sx = opponents_road_x_position + opponents_x_span; // x position of front right wheel
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_front_right_road_pos.y);
#ifdef CALC_FRONT_X_Z_FROM_SCRATCH
VALUE1 = VALUE2 = 0;
if (bTestKey)
{
VALUE1 = opp_front_right_road_pos.x;
surface_position = sz;
left_side_x = sx2 + ((surface_position * (sx1-sx2)) >> 8);
left_side_z = sz2 + ((surface_position * (sz1-sz2)) >> 8);
right_side_x = sx3 + ((surface_position * (sx4-sx3)) >> 8);
right_side_z = sz3 + ((surface_position * (sz4-sz3)) >> 8);
xd = right_side_x - left_side_x;
zd = right_side_z - left_side_z;
sx = opponents_road_x_position - opponents_x_span; // x position of front left wheel
opp_front_left_road_pos.x = left_side_x + ((sx * xd) >> 8);
opp_front_left_road_pos.z = left_side_z + ((sx * zd) >> 8);
sx = opponents_road_x_position + opponents_x_span; // x position of front right wheel
opp_front_right_road_pos.x = left_side_x + ((sx * xd) >> 8);
opp_front_right_road_pos.z = left_side_z + ((sx * zd) >> 8);
// Calculate position of piece's bottom front left corner, within world
piece_x = Track[piece].x << (LOG_CUBE_SIZE-LOG_PRECISION);
piece_y = Track[piece].y << (LOG_CUBE_SIZE-LOG_PRECISION);
piece_z = Track[piece].z << (LOG_CUBE_SIZE-LOG_PRECISION);
// Position front road co-ordinates within world
opp_front_left_road_pos.x += piece_x;
opp_front_left_road_pos.z += piece_z;
opp_front_right_road_pos.x += piece_x;
opp_front_right_road_pos.z += piece_z;
VALUE2 = opp_front_right_road_pos.x;
}
#endif
#ifdef OPPONENT_SHADOW
// Calculate front left shadow y co-ordinate
sx = opponents_road_x_position - opponents_shadow_x_span;
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_shadow_front_left.y);
// Calculate front right shadow y co-ordinate
sx = opponents_road_x_position + opponents_shadow_x_span;
CalculateOpponentsRoadWheelHeight(sx, sz, &opp_shadow_front_right.y);
// Y co-ordinates need to be divided by 4 for display, but they're
// already /2 because are in Amiga format (i.e. not * PC_FACTOR).
// Also add 7 to y so that shadow is slightly above road and isn't clipped as much
D3DXVECTOR3 v1, v2, v3, v4;
v2 = D3DXVECTOR3( (float)opp_shadow_rear_left.x, 7 + (float)opp_shadow_rear_left.y/2, (float)opp_shadow_rear_left.z );
v3 = D3DXVECTOR3( (float)opp_shadow_rear_right.x, 7 + (float)opp_shadow_rear_right.y/2, (float)opp_shadow_rear_right.z );
v1 = D3DXVECTOR3( (float)opp_shadow_front_left.x, 7 + (float)opp_shadow_front_left.y/2, (float)opp_shadow_front_left.z );
v4 = D3DXVECTOR3( (float)opp_shadow_front_right.x, 7 + (float)opp_shadow_front_right.y/2, (float)opp_shadow_front_right.z );
RemoveShadowTriangles();
if (draw_shadow)
{
StoreShadowTriangle(v2, v1, v3, 0);
StoreShadowTriangle(v1, v4, v3, 0);
}
#endif
// VALUE1 = opp_rear_left_road_pos.y;
// VALUE2 = opp_front_road_pos_y;
// VALUE3 = opp_rear_right_road_pos.y;
return;
}
static void GetSurfaceCoords( long piece, long segment )
{
if ((segment < 0) || (segment >= Track[piece].numSegments))
{
MessageBox(NULL, L"GetSurfaceCoords(opponent) segment out of range", L"Error", MB_OK);
#if defined(DEBUG) || defined(_DEBUG)
fprintf(out, "GetSurfaceCoords(opponent) piece %d, segment %d, numSegments %d\n", piece, segment, Track[piece].numSegments);
#endif
}
sx2 = Track[piece].coords[(segment*4)].x;
sy2 = Track[piece].coords[(segment*4)].y;
sz2 = Track[piece].coords[(segment*4)].z;
sx3 = Track[piece].coords[(segment*4)+1].x;
sy3 = Track[piece].coords[(segment*4)+1].y;
sz3 = Track[piece].coords[(segment*4)+1].z;
segment++;
sx1 = Track[piece].coords[(segment*4)].x;
sy1 = Track[piece].coords[(segment*4)].y;
sz1 = Track[piece].coords[(segment*4)].z;
sx4 = Track[piece].coords[(segment*4)+1].x;
sy4 = Track[piece].coords[(segment*4)+1].y;
sz4 = Track[piece].coords[(segment*4)+1].z;
return;
}
static long CalcSurfacePosition( long *next_segment, long distance, long z_shift )
{
long surface_position = distance & 0xff;
*next_segment = FALSE;
surface_position += z_shift;
if (surface_position >= 256)
{
*next_segment = TRUE;
surface_position &= 0xff;
}
return(surface_position);
}
static void CalculateOpponentsRoadWheelHeight( long sx, long sz, long *y_out )
{
long sya, syb, y;
// calculate height of surface at x,z position using linear interpolation
// i.e. calculate y at offset (sx, sz)
// given (sx1, sy1, sz1)
// (sx2, sy2, sz2)
// (sx3, sy3, sz3)
// (sx4, sy4, sz4)
// first do x interpolation
sya = sy1 + ((sx * (sy4-sy1)) >> 8);
syb = sy2 + ((sx * (sy3-sy2)) >> 8);
// now do z interpolation
y = (syb << 8) + (sz * (sya-syb));
*y_out = y >> 9;
return;
}
/* ======================================================================================= */
/* Function: OpponentMovement */
/* */
/* Description: */
/* ======================================================================================= */
extern bool drop_start_done;
// Tested against Amiga
static void OpponentMovement( void )
{
static long byte_count = 0;
if (!drop_start_done)
return;
UpdateOpponentsActualWheelHeights();
/*
//temp
opp_actual_height[REAR_LEFT] = opp_rear_left_road_pos.y;
opp_actual_height[REAR_RIGHT] = opp_rear_right_road_pos.y;
opp_actual_height[FRONT] = opp_front_road_pos_y;
*/
RandomizeOpponentsSteering();
GetOpponentsEngineAcceleration();
AdjustOpponentsEngineAcceleration();
#ifdef TEST_AMIGA_AOEA
long temp;
if (GetRecordedAmigaWord(&temp))
{
bool flag = temp & 0x80 ? TRUE : FALSE;
if (flag != opponents_required_z_speed_reached)
{
++VALUE1; // Count differences
fprintf(out, "%s different %d %d (VALUE2 %d)\n", "opponents.required.z.speed.reached", temp, opponents_required_z_speed_reached, VALUE2);
opponents_required_z_speed_reached = flag; // Use Amiga value when different
}
}
CompareRecordedAmigaWord("opponents.engine.z.acceleration", &opponents_engine_z_acceleration);
#endif
UpdateOpponentsZSpeed();
#ifdef TEST_AMIGA_OM
if (GetRecordedAmigaWord(&opponents_z_speed))
++VALUE2;
GetRecordedAmigaWord(&opponents_current_piece);
GetRecordedAmigaWord(&byte_count);
GetRecordedAmigaWord(&opponents_distance_into_section);
#endif
// TO DO: Tidy up
long value = (opponents_z_speed * (Track[opponents_current_piece].lengthReduction << 7)) << 1;
value >>= 16;
value *= REDUCTION;
value >>= 8;
value <<= 3;
long byte = value & 0xff;
value >>= 8;
byte_count += byte;
if (byte_count > 0xff)
{
++value;
byte_count &= 0xff;
}
opponents_distance_into_section += value;
if (opponents_distance_into_section >= (Track[opponents_current_piece].numSegments * 256))
{
// DIRECTION DEPENDANT
opponents_distance_into_section -= (Track[opponents_current_piece].numSegments * 256);
// go to next piece
opponents_current_piece++;
if (opponents_current_piece > (NumTrackPieces - 1)) opponents_current_piece = 0;
}
#ifdef TEST_AMIGA_OM
CompareRecordedAmigaWord("opponents.road.section", &opponents_current_piece);
CompareRecordedAmigaWord("opponents.distance.into.section", &opponents_distance_into_section);
#endif
}
/* ======================================================================================= */
/* Function: UpdateOpponentsActualWheelHeights */
/* */