-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIBTLCAP.CPP
5265 lines (4499 loc) · 137 KB
/
AIBTLCAP.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
/* ========================================================================
Copyright (c) 1990,1996 Synergistic Software
All Rights Reserved
========================================================================
Filename: Avatar.cpp
Author: Greg Hightower
======================================================================== */
/* ------------------------------------------------------------------------
Includes
------------------------------------------------------------------------ */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if defined(_WINDOWS)
#include <windows.h>
#endif
#include "system.h"
#include "engine.h"
#include "ai_utils.h"
#include "avatar.hxx"
//#include "btlstr.h"
#include "aibtlcap.hxx"
#include "gamemap.hxx"
#include "gametype.hxx"
#include "gmenuenm.h"
#include "handle.hxx"
#include "main.hxx"
#include "game.h"
#include "panel.h"
#include "battle.hxx"
#include "battleui.hxx"
#include "scnmgr.hxx"
#include "sound.hxx"
#include "special.h"
#include "strenum.h"
#include "strmgr.h"
#include "sndvox.hxx"
#include "describe.hxx"
#include "playstat.hxx"
#include "regents.hxx"
#include "scene.hxx"
#ifdef _WINDOWS
#include "winsys\mulplay.hxx"
#include "winsys\messque.hxx"
#include "winsys\mono_c.h"
#ifdef OLD_SOUND
#include "winsys\sndutil.h"
#include "winsys\mciutil.h"
#include "winsys\musiutil.h"
#endif
#endif
/* ------------------------------------------------------------------------
Defines and Compile Flags
------------------------------------------------------------------------ */
#define MAX_TROOP_ROWS 3
#define MAX_TROOP_COLS 7
/* this is the definition of the battle field grid */
/* */
/* His Reserves */
/* ÚÄÄÄÄÂÄÄÄÄÂÄÄÄÄÂÄÄÄÄÂÄÄÄÄ¿ */
/* ³Away³ ³ ³ ³ ³ */
/* ³ 2,0³ 2,1³ 2,2³ 2,3³ 2,4³ */
/* ³Home³ ³ ³ ³ ³ */
/* ÀÄÄÄÄÁÄÄÄÄÁÄÄÄÄÁÄÄÄÄÁÄÄÄÄÙ */
/* ÚÄÄÄÄÂÄÄÄÄÂÄÄÄÄÂÄÄÄÄÂÄÄÄÄ¿ */
/* ³ ³ ³ ³ ³ ³ */
/* ³ 1,0³ 1,1³ 1,2³ 1,3³ 1,4³ */
/* ³ ³ ³ ³ ³ ³ */
/* ÀÄÄÄÄÁÄÄÄÄÁÄÄÄÄÁÄÄÄÄÁÄÄÄÄÙ */
/* ÚÄÄÄÄÂÄÄÄÄÂÄÄÄÄÂÄÄÄÄÂÄÄÄÄ¿ */
/* ³ ³ ³ ³ ³ ³ */
/* ³ 0,0³ 0,1³ 0,2³ 0,3³ 0,4³ */
/* ³ ³ ³ ³ ³ ³ */
/* ÀÄÄÄÄÁÄÄÄÄÁÄÄÄÄÁÄÄÄÄÁÄÄÄÄÙ */
/* My Reserves */
/* */
/* each grid is broken into 2 x and y sub points */
/* NOTE: look at the array below */
/* ------------------------------------------------------------------------
Macros
------------------------------------------------------------------------ */
#define BTL_KILL_DELAY 1
/* ------------------------------------------------------------------------
Prototypes
------------------------------------------------------------------------ */
void SendWin ( BOOL IWin );
void SendToReserves ( CAvatar *pAvatar );
void SendFallBack ( CAvatar *pAvatar, SHORT R, SHORT C );
void SendMagic ( CAvatar *pAvatar );
static void DisposeTroopList(CAvatar *pAvatar);
static void CheckDisconnect(void);
static void TransRowCol( SHORT * Row, SHORT * Column );
static void SendMove ( CAvatar *pAvatar );
static void SendArrive ( CAvatar *pAvatar );
static void SendResults ( CAvatar *pAvatar, SHORT points, SHORT shot );
static void SendRout ( CAvatar *pAvatar );
static int CheckArrive ( CAvatar *pAvatar );
static void CheckResults ( CAvatar *pAvatar );
static void CheckRout ( CAvatar *pAvatar );
static void CheckFallBack ( CAvatar *pAvatar );
static void CheckMove(CAvatar *pAvatar);
static void CheckReserves(CAvatar *pAvatar);
static void CheckWin ( void );
static void CheckMagic ( CAvatar *pAvatar );
static void CheckMulti ( CAvatar *pAvatar, CAvatar::AISTATUS Status );
static SHORT ResolveMissiles(CAvatar *pDefender);
static void MultiPrintf(const char *format, ...);
/* ------------------------------------------------------------------------
Global Variables
------------------------------------------------------------------------ */
DEFINE_MATRIX_DATA_S(POINT,BattleGridHome,GRID_MAX_ROWS,GRID_MAX_COLS) = {
{
{GRID1_X,GRID11_Y},
{GRID2_X,GRID11_Y},
{GRID3_X,GRID11_Y},
{GRID4_X,GRID11_Y},
{GRID5_X,GRID11_Y},
},
{
{GRID1_X,GRID21_Y},
{GRID2_X,GRID21_Y},
{GRID3_X,GRID21_Y},
{GRID4_X,GRID21_Y},
{GRID5_X,GRID21_Y},
},
{
{GRID1_X,GRID31_Y},
{GRID2_X,GRID31_Y},
{GRID3_X,GRID31_Y},
{GRID4_X,GRID31_Y},
{GRID5_X,GRID31_Y},
}
};
DEFINE_MATRIX_CLASS_S(POINT, BattleGridHome, GRID_MAX_ROWS, GRID_MAX_COLS);
DEFINE_MATRIX_DATA_S(POINT,BattleGridAway,GRID_MAX_ROWS,GRID_MAX_COLS) = {
{
{GRID1_X,GRID12_Y},
{GRID2_X,GRID12_Y},
{GRID3_X,GRID12_Y},
{GRID4_X,GRID12_Y},
{GRID5_X,GRID12_Y},
},
{
{GRID1_X,GRID22_Y},
{GRID2_X,GRID22_Y},
{GRID3_X,GRID22_Y},
{GRID4_X,GRID22_Y},
{GRID5_X,GRID22_Y},
},
{
{GRID1_X,GRID32_Y},
{GRID2_X,GRID32_Y},
{GRID3_X,GRID32_Y},
{GRID4_X,GRID32_Y},
{GRID5_X,GRID32_Y},
}
};
DEFINE_MATRIX_CLASS_S(POINT, BattleGridAway, GRID_MAX_ROWS, GRID_MAX_COLS);
// this is used only for turn undead spell
BOOL fBtlTurnUndead = FALSE;
// list of what avatar is in which grid
//GEH extern DECL_MATRIX_CLASS_S(GRID,WhosWhere,GRID_MAX_ROWS,GRID_MAX_COLS);
extern BOOL fTutorialFirstBattle; // from gamemap.cpp
extern BOOL fDoTutReserves ; // from battleui.cpp
extern BOOL fDoTutMove ; // from battleui.cpp
extern BOOL fDoTutEngage ; // from battleui.cpp
extern BOOL fDoTutArchers ; // from battleui.cpp
//---- If non-zero then we are fighting a remote player
extern DWORD dwBattleMultiId; //---- If non zero then we have an remote player
extern BOOL fBattleFreezeTime;
extern BOOL fBattleNextTurn;
extern LONG BattleTick;
extern BOOL fBtlTurnBased;
extern BOOL fUnitsStillMoving;
/* ========================================================================
Function - BattleCapt
Description - AI Proc to find nearest opponent, then release troops to melee
Returns - Current state
======================================================================== */
/* The battle capt's job is to move his troops from one point to another
* and engage the enemy. He will recieve movement commands from either
* his mouse click routine or scene commands. He is in charge of killing
* off his troops to keep them in line with his hit pts. He must coordinate
* with the opposing battle capt to work out whose charging, defending, etc.
*/
void CAvatar::BattleCapt (CAvatar *pAvatar, CAvatar::AISTATUS Status)
{
LONG i;
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
SHORT *pList;
SHORT level;
switch( Status )
{
case AI_INIT:
{
pAvatar->fBtlCap.mfInitVals(pAvatar->fBtlCap.TargetType, pAvatar->fBtlCap.TroopType);
pAvatar->LeftButtonFn = (PFVC)BATTLE_CAPTAIN_DATA::mfLeftButton;
pAvatar->RightButtonFn = (PFVC)BATTLE_CAPTAIN_DATA::mfRightButton;
pAvatar->fBtlCap.WavePlaying = FALSE;
switch (pAvatar->fBtlCap.UnitIcon)
{
// 3 hit points
case A_LVY_UNIT_ICON:
case A_KNT_UNIT_ICON:
case D_INF_UNIT_ICON:
case E_INF_UNIT_ICON:
case C_SKL_UNIT_ICON:
case A_EIN_UNIT_ICON:
case N_IRR_UNIT_ICON:
pAvatar->fBtlCap.TroopRows = 3;
pAvatar->fBtlCap.TroopCols = 7;
break;
// 2 hit points
case A_IRR_UNIT_ICON:
case A_CAV_UNIT_ICON:
case G_CAV_UNIT_ICON:
case G_BOW_UNIT_ICON:
case E_CAV_UNIT_ICON:
case E_BOW_UNIT_ICON:
case D_BOW_UNIT_ICON:
case A_PIK_UNIT_ICON:
case A_INF_UNIT_ICON:
case A_BOW_UNIT_ICON:
case G_INF_UNIT_ICON:
case M_CAV_UNIT_ICON:
case M_INF_UNIT_ICON:
case M_BOW_UNIT_ICON:
case M_IRR_UNIT_ICON:
case M_PIK_UNIT_ICON:
case N_INF_UNIT_ICON:
pAvatar->fBtlCap.TroopRows = 3;
pAvatar->fBtlCap.TroopCols = 5;
break;
// 1 hit point
case A_SCT_UNIT_ICON:
pAvatar->fBtlCap.TroopRows = 2;
pAvatar->fBtlCap.TroopCols = 5;
break;
// special cases
case C_SPD_UNIT_ICON:
case C_HEL_UNIT_ICON:
pAvatar->fBtlCap.TroopRows = 1;
pAvatar->fBtlCap.TroopCols = 6;
break;
case C_HPY_UNIT_ICON:
case C_WYV_UNIT_ICON:
pAvatar->fBtlCap.TroopRows = 1;
pAvatar->fBtlCap.TroopCols = 1;
default: // 2 hit points
pAvatar->fBtlCap.TroopRows = 3;
pAvatar->fBtlCap.TroopCols = 5;
break;
}
// about half as many points in ultra low memory mode
if (fRestrictAni == TRUE)
pAvatar->fBtlCap.TroopRows = (pAvatar->fBtlCap.TroopRows + 1) / 2;
pAvatar->fBtlCap.HitCount = 0;
pAvatar->fBtlCap.Healthy =
pAvatar->fBtlCap.HitPoints =
(SHORT)(pAvatar->fBtlCap.TroopRows*pAvatar->fBtlCap.TroopCols);
level = 0;
if (pAvatar->hPlayerStats != fERROR)
{
DumbAutoLockPtr<PLAYER_STATS const> const pPlayerStats(pAvatar->hPlayerStats);
SHORT SpellClass = pPlayerStats->mfGetSpellCastClass(SPELL_INFO::WIZARD);
if (SpellClass != CANT_CAST_SPELLS)
{
level = pPlayerStats->mfGetLevel(SpellClass);
level /= 3;
}
else
{
SpellClass = pPlayerStats->mfGetSpellCastClass(SPELL_INFO::PRIEST);
if (SpellClass != CANT_CAST_SPELLS)
{
level = pPlayerStats->mfGetLevel(SpellClass);
level -= 8;
if (level > 0)
{
level /= 2;
}
else
{
level = 0;
}
}
}
}
pAvatar->fBtlCap.SpellCount = level;
pAvatar->fBtlCap.MissileCount = BattleTick + BtlActionRate;
pAvatar->fBtlCap.MagicCount = BattleTick + BtlActionRate;
// place myself in the reserves
pAvatar->fBtlCap.Leader.hdlSlotList = fERROR;
pAvatar->fBtlCap.Leader.hdlPathDeltas = fERROR;
pAvatar->fBtlCap.FallBackCount = 0;
BattleMoveToReservesArea(pAvatar->hThis);
BattlePutInReserves(pAvatar->mfIsHomeTeam(), pAvatar->hThis);
// tally up the players
if(!pAvatar->mfIsHomeTeam())
gAwayUnits++;
// speed from reserves
pAvatar->fBtlCap.Movement =
pAvatar->fBtlCap.Rate = (SHORT)(BTLCAP_MARCH_RATE * 8);
// start the game paused
pAvatar->Status = AI_PAUSED;
if(!pAvatar->mfIsHomeTeam())
{
#ifdef _WINDOWS
if ( AMultiPlayer.IsMultiPlayerandRealm( pAvatar->Realm.HomeRealm ) )
{
CheckMove ( pAvatar );
}
else
#endif
{
// run the enemy ai once to get them setup on the field
pAvatar->fBtlCap.mfRunEnemyBtlCapAI(pAvatar);
}
}
// give all the character expierence points for just being brave
pAvatar->fBtlCap.EverOnBtlField = FALSE;
pAvatar->fBtlCap.TurnAction = TRUE;
pAvatar->fBtlCap.TurnMove = TRUE;
BattleExpPoints( pAvatar->UnitIndex, BTL_EXP_ENGAGE, 0, FALSE );
}
break;
case AI_RELEASE:
pAvatar->Status = AI_INIT;
DisposeTroopList(pAvatar);
break;
case AI_BEGIN_PAUSE:
// Tell everyone following or attacking me to stop and go away for now.
pAvatar->Status = AI_PAUSED;
pAvatar->mfStartAnimationOnce(STANDSEQ);
DisposeTroopList(pAvatar);
// take a powder myself
pAvatar->mfSetInvisible(TRUE);
break;
case AI_PAUSED:
// Just hang out here until called from the reserves
if( pAvatar->fBtlCap.Row == -1 ||
pAvatar->fBtlCap.Row == 3 )
{
if(!pAvatar->mfIsHomeTeam())
{
#ifdef _WINDOWS
if ( AMultiPlayer.IsMultiPlayerandRealm( pAvatar->Realm.HomeRealm ) )
{
CheckMove ( pAvatar );
}
else
#endif
{
pAvatar->fBtlCap.mfRunEnemyBtlCapAI(pAvatar);
}
}
break;
}
// else fall through
case AI_END_PAUSE:
// Tell everyone following me to continue on.
pAvatar->mfSetInvisible(FALSE);
pAvatar->Status = AI_SEARCH;
// time base stuff
pAvatar->fTimeLastMoved = SCENE_MGR::gTick;
pAvatar->fBtlCap.MissileCount = BattleTick + BtlActionRate;
pAvatar->fBtlCap.MagicCount = BattleTick + BtlActionRate;
// set up a action count for enemy AI's
pAvatar->fBtlCap.ActionCount =
BattleTick + // current time
2 * BtlActionRate + // ~10 seconds
(BtlActionRate * ((SpeedLevel+1)/2)) + // speed number of turns
random(BtlActionRate) // not everyone move at once
;
pAvatar->mfStartAnimationOnce(STANDSEQ);
pAvatar->Engaged = 0;
// now create my troops
pAvatar->CreateBattleTroops();
pAvatar->MoveBattleTroops();
break;
case AI_ATTACK:
{
LONG NewAngle;
if (fBattleFreezeTime)
return;
// begin the attack
// unless engaged, lets try always facing the camera
// so that our unitinfo box always shows my facing
if(pAvatar->hEnemy != fERROR)
{
CAvatar * const pEnemy = (CAvatar * const)BLKPTR(pAvatar->hEnemy);
if(!IsPointerGood(pEnemy))
{
pAvatar->hEnemy = fERROR;
pAvatar->Status = AI_SEARCH;
break;
}
else
{
NewAngle = AngleFromPoint(
pAvatar->mfX(),
pAvatar->mfY(),
pEnemy->mfX(),
pEnemy->mfY(),
RESOLUTION_8);
}
}
else
{
NewAngle = AngleFromPoint(
pAvatar->mfX(),
pAvatar->mfY(),
CAMERA_INT_VAL(camera.x),
CAMERA_INT_VAL(camera.y),
RESOLUTION_8);
}
pAvatar->mfRotateToward(pAvatar->mfAngle(), NewAngle);
pAvatar->mfStartAnimationOnce(STANDSEQ);
pAvatar->fBtlCap.Rate =
BTLCAP_MARCH_RATE *
BattleTroopInfo[pAvatar->fBtlCap.UnitIcon].Move ;
// try to reengage any of my troops
pAvatar->fBtlCap.mfMelee(pAvatar);
// attacks are resolved every battle action time
if(pAvatar->fBtlCap.ActionCount < BattleTick)
{
fUpdatePanels = TRUE;
pAvatar->fBtlCap.ActionCount =
BattleTick + (BtlActionRate * SpeedLevel);
switch(pAvatar->fBtlCap.UnitIcon)
{
case G_CAV_UNIT_ICON:
AddSndObj(SND_WOLF_SNARL1, SND_WOLF_SNARL_TOTAL, VOLUME_SEVENTY);
break;
case A_CAV_UNIT_ICON :
case A_KNT_UNIT_ICON :
AddSndObj(SND_HORSE_NOISE1, SND_HORSE_NOISE_TOTAL, VOLUME_SEVENTY);
break;
}
CheckDisconnect();
if ( dwBattleMultiId != kNoMultiId ) //@@@@
{
if ( pAvatar->mfIsHomeTeam() )
{
ResolveAttack(pAvatar->hThis, pAvatar->hEnemy, BTLCAP_MELEE);
}
}
else
{
ResolveAttack(pAvatar->hThis, pAvatar->hEnemy, BTLCAP_MELEE);
}
}
}
break;
case AI_SEARCH:
{
LONG tx = pAvatar->fBtlCap.TargetX;
LONG ty = pAvatar->fBtlCap.TargetY;
LONG oldX = pAvatar->mfX();
LONG oldY = pAvatar->mfY();
// test for shooting type actions
if(pAvatar->fBtlCap.Shoot)
{
if (fBattleFreezeTime)
return;
pAvatar->fBtlCap.mfShoot(pAvatar);
pAvatar->fBtlCap.Shoot = BTLCAP_NOSHOT;
}
else
if (tx != oldX || ty != oldY)
{
SendMove( pAvatar );
// NOTE: we started moving
fUpdatePanels = TRUE;
pAvatar->Status = AI_MOVING;
pAvatar->fTimeLastMoved = SCENE_MGR::gTick;
}
else
if( pAvatar->fBtlCap.TargetRow || pAvatar->fBtlCap.TargetCol )
{
BOOL MovedSideways = FALSE;
// NOTE: we started moving
fUpdatePanels = TRUE;
if(pAvatar->fBtlCap.TargetRow<0)
{
if (pAvatar->fBtlCap.mfMoveBack(0, pAvatar->hThis) == TRUE)
{
pAvatar->fBtlCap.TargetRow++;
}
else if (pAvatar->fBtlCap.TargetCol<0)
{
// Couldn't move back, did we want to try moving left?
pAvatar->fBtlCap.TargetCol++;
pAvatar->fBtlCap.mfMoveLeft(0, pAvatar->hThis);
MovedSideways = TRUE;
}
else if (pAvatar->fBtlCap.TargetCol>0)
{
// Couldn't move back, did we want to try moving right?
pAvatar->fBtlCap.TargetCol--;
pAvatar->fBtlCap.mfMoveRight(0, pAvatar->hThis);
MovedSideways = TRUE;
}
else
{
// Can't move, so decrement the requested count.
pAvatar->fBtlCap.TargetRow++;
}
}
else
if(pAvatar->fBtlCap.TargetRow>0)
{
if (pAvatar->fBtlCap.mfMoveAhead(0, pAvatar->hThis) == TRUE)
{
pAvatar->fBtlCap.TargetRow--;
}
else if (pAvatar->fBtlCap.TargetCol<0)
{
// Couldn't move ahead, did we want to try moving left?
pAvatar->fBtlCap.TargetCol++;
pAvatar->fBtlCap.mfMoveLeft(0, pAvatar->hThis);
MovedSideways = TRUE;
}
else if (pAvatar->fBtlCap.TargetCol>0)
{
// Couldn't move ahead, did we want to try moving right?
pAvatar->fBtlCap.TargetCol--;
pAvatar->fBtlCap.mfMoveRight(0, pAvatar->hThis);
MovedSideways = TRUE;
}
else
{
// Can't move, so decrement the requested count.
pAvatar->fBtlCap.TargetRow--;
}
}
// If we didn't movesideways because we were blocked from going forward or back.
if (MovedSideways == FALSE)
{
if(pAvatar->fBtlCap.TargetCol<0)
{
// Were we successfull or we aren't trying to move ahead or back.
if(pAvatar->fBtlCap.mfMoveLeft(0, pAvatar->hThis) == TRUE ||
pAvatar->fBtlCap.TargetRow == 0)
{
pAvatar->fBtlCap.TargetCol++;
}
}
else
if(pAvatar->fBtlCap.TargetCol>0)
{
// Were we successfull or we aren't trying to move ahead or back.
if (pAvatar->fBtlCap.mfMoveRight(0, pAvatar->hThis) == TRUE ||
pAvatar->fBtlCap.TargetRow == 0)
{
pAvatar->fBtlCap.TargetCol--;
}
}
}
SendMove( pAvatar );
}
else
{
if(pAvatar->mfIsHomeTeam())
pAvatar->mfRotateToward(pAvatar->mfAngle(), 0);
else
pAvatar->mfRotateToward(pAvatar->mfAngle(), 128);
pAvatar->mfStartAnimationOnce(STANDSEQ);
if (!fBattleFreezeTime)
pAvatar->fBtlCap.mfTryToEngageEnemyBtlCap(pAvatar);
if(pAvatar->fBtlCap.OnBtlField)
{
pAvatar->fBtlCap.Rate =
BTLCAP_MARCH_RATE *
BattleTroopInfo[pAvatar->fBtlCap.UnitIcon].Move ;
}
else
{
pAvatar->fBtlCap.Rate = (SHORT)(BTLCAP_MARCH_RATE * 8);
}
if(!pAvatar->mfIsHomeTeam() && pAvatar->Status == AI_SEARCH)
{
#ifdef _WINDOWS
if ( AMultiPlayer.IsMultiPlayerandRealm( pAvatar->Realm.HomeRealm ) )
{
CheckMove ( pAvatar );
}
else
#endif
{
pAvatar->fBtlCap.mfRunEnemyBtlCapAI(pAvatar);
}
}
}
// make our little duck follow us
pAvatar->MoveBattleTroops();
}
break;
case AI_MOVING:
{
LONG tx = pAvatar->fBtlCap.TargetX;
LONG ty = pAvatar->fBtlCap.TargetY;
LONG oldX = pAvatar->mfX();
LONG oldY = pAvatar->mfY();
if (tx != oldX || ty != oldY)
{
// -- signal that units are still on the move
if ( pAvatar->fBtlCap.OnBtlField )
fUnitsStillMoving = TRUE;
//---- If this isn't my guy then check for arrive message
if ( !pAvatar->mfIsHomeTeam() &&
CheckArrive ( pAvatar ) )
{
// NOTE: we stopped moving
fUpdatePanels = TRUE;
pAvatar->Status = AI_SEARCH;
}
else
{
pAvatar->fBtlCap.Movement =
pAvatar->fBtlCap.mfTerrainSpeed(pAvatar, pAvatar->fBtlCap.Rate)
* (SCENE_MGR::gTick - pAvatar->fTimeLastMoved);
pAvatar->FaceTo(tx, ty);
pAvatar->MoveToward( tx, ty, 0, pAvatar->fBtlCap.Movement);
pAvatar->mfStartAnimationOnce(MARCHSEQ);
}
}
else
{
if( fBtlTurnBased
&& ( pAvatar->fBtlCap.TargetRow || pAvatar->fBtlCap.TargetCol ))
{
pAvatar->Status = AI_SEARCH;
break;
}
// -- see if it is the next turn
if ( fBtlTurnBased && !fBattleNextTurn )
break;
//---- If this is my team tell other machine I made it
// GEH this is an attempt to limit the shoot and run tactics that
// seem too effective
// reset my shoot timer a little
if ( !fBtlTurnBased )
pAvatar->fBtlCap.MissileCount = BattleTick + (BtlActionRate/4);
if ( pAvatar->mfIsHomeTeam() )
{
SendArrive ( pAvatar );
}
// NOTE: we stopped moving
fUpdatePanels = TRUE;
// if I was sent to the reserves, pause myself next turn
if (pAvatar->fBtlCap.OnBtlField)
{
//GEH TUTORIAL
if (pAvatar->mfIsHomeTeam() &&
fTutorialFirstBattle &&
fDoTutMove )
{
fDoTutMove = FALSE;
//SystemHelp("TUTORIAL TEST", "Select a unit on the field and move them to one of the blue hilighted squares", -1, NULL, 0);
DescribeBtlMovement();
}
pAvatar->Status = AI_SEARCH;
}
else
pAvatar->Status = AI_BEGIN_PAUSE;
}
pAvatar->MoveBattleTroops();
pAvatar->fTimeLastMoved = SCENE_MGR::gTick;
}
break;
case AI_DEAD:
// pretend to be a lump of meat slowly approching air temperature
if (pAvatar->CurSequence != DEADSEQ &&
pAvatar->mfTestSequenceDone() )
{
// if the death was visible, send my horse off to the sidelines
if( pAvatar->mfIsVisible() )
{
// send a cav horse running off
if ((pAvatar->mfType() == LIGHT_CAVALRY ) ||
(pAvatar->mfType() == MERC_CAVALRY ) ||
(pAvatar->mfType() == LORES_A_CAV ) )
pAvatar->mfCreateHorse(GetNewId(), LORES_CAV_HORSE);
// send a night horse running off
if ((pAvatar->mfType() == KNIGHT ) ||
(pAvatar->mfType() == ELF_CAVALRY ) ||
(pAvatar->mfType() == LORES_A_KNT ) )
pAvatar->mfCreateHorse(GetNewId(), LORES_KNIGHT_HORSE);
}
pAvatar->mfPlayAnimationOnce(DEADSEQ);
// GEH pAvatar->SetAIFuncIndex(AI_FUNC_NULL);
}
if (pAvatar->mfGetArtSequence() != DEADSEQ &&
pAvatar->mfTestSequenceDone() )
pAvatar->mfPlayAnimationOnce(DEADSEQ);
// dont break!
return;
default:
break;
}
//---- Check for remote actions
CheckMulti ( pAvatar, Status );
/* -----------------------------------------------------------------
exec each frame pass...
----------------------------------------------------------------- */
// check for damage from a drive-by magic shooting
if(pAvatar->KillType != ARROWS && pAvatar->KillType != NO_THING)
{
CheckDisconnect();
if ( dwBattleMultiId != kNoMultiId )
{
//if ( pAvatar->mfIsHomeTeam() )
{
ResolveBattleMagic(pAvatar);
while(pAvatar->fBtlCap.HitCount > 0)
pAvatar->fBtlCap.mfKillTroop(pAvatar,pAvatar->KillType);
AddSndObj( SND_MALE_GROUP_DIE1, SND_MALE_GROUP_DIE_TOTAL, VOLUME_EIGHTY);
}
}
else
{
ResolveBattleMagic(pAvatar);
while(pAvatar->fBtlCap.HitCount > 0)
pAvatar->fBtlCap.mfKillTroop(pAvatar,pAvatar->KillType);
AddSndObj( SND_MALE_GROUP_DIE1, SND_MALE_GROUP_DIE_TOTAL, VOLUME_EIGHTY);
}
}
else
// check for arrow/missile damage
if(pAvatar->KillType == ARROWS)
{
//if(pAvatar->fBtlCap.Shot > BattleTick)
{
ResolveMissiles(pAvatar);
}
}
// clear outside kill types
pAvatar->KillType = NO_THING;
// do we need to resolve any damage?
if(pAvatar->fBtlCap.HitCount > 0)
{
while(pAvatar->fBtlCap.HitCount > 0)
pAvatar->fBtlCap.mfKillTroop(pAvatar,NO_THING);
AddSndObj( SND_MALE_GROUP_DIE1, SND_MALE_GROUP_DIE_TOTAL, VOLUME_EIGHTY);
}
if(pAvatar->fBtlCap.HitPoints <= 0) // I B DEAD
KillBattleCaptain(pAvatar);
}
/* ========================================================================
Function - BtlCapGenerate
Description - generate the list of battle captain avatars
Returns - TRUE for ok, FALSE or somekind of failure
======================================================================== */
BOOL BtlCapGenerate( SHORT UnitIndex )
{
BOOL ReturnVal = FALSE; // assume total failure!
CAvatar *pAvatar;
CHAR buffer[80];
SHORT X,Y;
SHORT UnitCount = 0;
while(UnitIndex != -1)
{
ReturnVal = TRUE; // got past first step
// no more then gUnitStackSize units may ever go to a single battle
if(++UnitCount > gUnitStackSize)
return TRUE;
if (units[UnitIndex].iHandle != fERROR)
{
pAvatar = (CAvatar *) BLKPTR(units[UnitIndex].iHandle);
pAvatar->SetAIFuncIndex(CAvatar::AI_FUNC_BATTLE_CAPTAIN);
pAvatar->mfInitVals();
// GEH now load the icon for this unit, note that we use
// the same name as the map level, just add a 'B' to the
// beginning of the name
sprintf(buffer,"%sB%s%s", ICON_DIR, GameIcons[units[UnitIndex].Icon], ICON_EXT);
pAvatar->fBtlCap.hIcon = GetResourceStd(buffer, FALSE);
pAvatar->fBtlCap.OfficerIcon = (SHORT) units[UnitIndex].Icon;
// if this is an officer of some type, inherite some
// appropriate body guards
if (units[UnitIndex].Icon >= FIRST_CHARACTER_TYPE)
{
THINGTYPE BodyGuardType;
SHORT IconType;
switch(units[UnitIndex].Icon)
{
case REG_ELORD1_UNIT_ICON:
case REG_ELORD2_UNIT_ICON:
case REG_ELADY1_UNIT_ICON:
case LET_ELORD1_UNIT_ICON:
case LET_ELORD2_UNIT_ICON:
case LET_ELADY1_UNIT_ICON:
BodyGuardType = ELF_ARCHER;
IconType = E_BOW_UNIT_ICON;
break;
case REG_DLORD1_UNIT_ICON:
case REG_DLORD2_UNIT_ICON:
case LET_DLORD1_UNIT_ICON:
case LET_DLORD2_UNIT_ICON:
BodyGuardType = DWARF_ARCHER;
IconType = D_BOW_UNIT_ICON;
break;
case REG_GLORD1_UNIT_ICON:
case REG_GLADY1_UNIT_ICON:
case LET_GLORD1_UNIT_ICON:
case LET_GLADY1_UNIT_ICON:
BodyGuardType = GOBLIN_ARCHER;
IconType = G_BOW_UNIT_ICON;
break;
case GORGON_UNIT_ICON:
BodyGuardType = T_SKELETON;
IconType = C_SKL_UNIT_ICON;
break;
case SPIDERKING_UNIT_ICON:
BodyGuardType = T_GIANT_SPIDER;
IconType = C_SPD_UNIT_ICON;
break;
case RHUOBHE_UNIT_ICON:
BodyGuardType = ELF_CAVALRY;
IconType = E_CAV_UNIT_ICON;
break;
default:
BodyGuardType = ELITE_INFANTRY;
IconType = A_EIN_UNIT_ICON;
}
pAvatar->fBtlCap.TroopType = BodyGuardType;
pAvatar->fBtlCap.TargetType = BodyGuardType;
pAvatar->fBtlCap.UnitIcon = IconType;
}
else
{
pAvatar->fBtlCap.TroopType = pAvatar->mfType();
pAvatar->fBtlCap.TargetType = pAvatar->mfType();
pAvatar->fBtlCap.UnitIcon = (SHORT) units[UnitIndex].Icon;
}
// start out in the reserves
if(pAvatar->mfIsHomeTeam())
{
Y = MY_RESERVES_Y;
}
else
{
Y = ENEMY_RESERVES_Y;
}
X = (SHORT)(1000 - random(2000));
pAvatar->mfSetInvisible(TRUE);
pAvatar->mfMoveTo(X, Y);
pAvatar->fBtlCap.UnitIndex = (SHORT) UnitIndex;
}
UnitIndex = (SHORT) units[UnitIndex].NextUnit;
}
return ReturnVal;
}
/* ========================================================================
Function - CreateBattleTroops
Description - Make some troops for a Battle Captain.
Returns - void
======================================================================== */
void CAvatar::CreateBattleTroops(void)
{
LONG i;
LONG k;
// Note : Don't lock the scene here, its already locked.
PTR_SCENE pCurrentScene = (PTR_SCENE) BLKPTR(SCENE_MGR::hCurrentScene);
const LONG X = mfX();
const LONG Y = mfY();
const LONG Z = mfZ();
SHORT *pList;
// init leader data
fBtlCap.Leader.mfInitVals();
fBtlCap.Leader.sCount = 0;
fBtlCap.Leader.sMaxFollowers = (SHORT)((fBtlCap.TroopRows*fBtlCap.TroopCols));
fBtlCap.Leader.hdlSlotList =
NewLockedBlock(sizeof(SHORT) * fBtlCap.Leader.sMaxFollowers);
//GEH SetBlockAttr(fBtlCap.Leader.hdlSlotList, LOCKED, LOCKED);
// No memory for now.
if (fBtlCap.Leader.hdlSlotList == fERROR)
return;
pList = (SHORT *)BLKPTR(fBtlCap.Leader.hdlSlotList);
memset(pList, -1, sizeof(SHORT) * fBtlCap.Leader.sMaxFollowers);
// create my troops and record their handles in my follower list
//for(k=0; k<fBtlCap.TroopRows; k++)