-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
1656 lines (1483 loc) · 49.8 KB
/
game.c
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
/*
* XFrisk - The classic board game for X
* Copyright (C) 1993-1999 Elan Feingold ([email protected])
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: game.c,v 1.13 2000/01/18 20:07:26 morphy Exp $
*
* $Log: game.c,v $
* Revision 1.13 2000/01/18 20:07:26 morphy
* Made middle button (drop all armies) obey DROP_ARMIES limitation
*
* Revision 1.12 2000/01/18 19:56:43 morphy
* Prevention of dropping all armies works now
*
* Revision 1.11 2000/01/18 09:25:47 tony
* oops, little braino in "dropping armies" code
*
* Revision 1.10 2000/01/17 21:09:03 tony
* small fix on dropping armies during fortification, optional number
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "registerPlayers.h"
#include "callbacks.h"
#include "utils.h"
#include "dice.h"
#include "game.h"
#include "debug.h"
#include "riskgame.h"
#include "types.h"
#include "colormap.h"
#include "client.h"
/* Globals */
static MsgNetMessage mess;
static Int32 iAttackDst=-1;
static Int32 iMoveDst=-1;
/*iAttackSrc is used by callbacks.c */
static Int32 iAttackSrc=-1;
Int32 iLastAttackSrc=-1, iLastAttackDst=-1;
Int32 iMoveSrc=-1;
Flag fGetsCard=FALSE;
Flag fCanExchange=TRUE;
/* max number of armies to allow when not using DROP_ALL */
#define DROP_ARMIES 3
/************************************************************************
* FUNCTION: GAME_AttackFrom
* HISTORY: 291099 Tdh
* PURPOSE: Return value of iAttackSrc
* NOTES: to make iAttackSrc private to game.c
*/
Int32 GAME_AttackFrom() {
return iAttackSrc;
}
/************************************************************************
* FUNCTION: GAME_SetAttackSrc
* HISTORY: 291099 Tdh
* PURPOSE: Set value of iAttackSrc
* NOTES: to make iAttackSrc private to game.c
*/
void GAME_SetAttackSrc(Int32 src) {
iAttackSrc = src;
}
/************************************************************************
* FUNCTION: GAME_CanAttack
* HISTORY:
* 03.04.94 ESF Created.
* PURPOSE: Check if this attack is possible
* NOTES:
************************************************************************/
Flag GAME_CanAttack(Int32 AttackSrc, Int32 iAttackDst)
{
Int32 i;
for (i=0; i!=6 && RISK_GetAdjCountryOfCountry(AttackSrc, i)!=-1; i++)
if (RISK_GetAdjCountryOfCountry(AttackSrc, i) == iAttackDst)
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_SetTurnArmiesOfPlayer
* HISTORY:
* 04.23.95 ESF Created.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_SetTurnArmiesOfPlayer(Int32 iCurrentPlayer)
{
Int32 iNumArmies=0;
/* Add the continent bonus */
iNumArmies += GAME_GetContinentBonus(iCurrentPlayer);
/* Add the standard "at-least-three-and-at-most-one-per-three-countries" */
iNumArmies += MAX(RISK_GetNumCountriesOfPlayer(iCurrentPlayer)/3, 3);
RISK_SetNumArmiesOfPlayer(iCurrentPlayer, iNumArmies);
}
/************************************************************************
* FUNCTION: GAME_MoveArmies
* HISTORY:
* 03.04.94 ESF Created.
* 03.18.94 ESF Added server update.
* 05.19.94 ESF Added verbose message across network.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_MoveArmies(Int32 iSrcCountry, Int32 iDstCountry, Int32 iNumArmies)
{
MsgMoveNotify msgMoveNotify;
char buf[256];
/* Notify the server */
msgMoveNotify.iSrcCountry = iSrcCountry;
msgMoveNotify.iDstCountry = iDstCountry;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_MOVENOTIFY, &msgMoveNotify);
/* send a verbose message */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s moved %d armies from %s to %s",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s déplace %d armée(s) de %s à %s",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
iNumArmies,
RISK_GetNameOfCountry(iSrcCountry),
RISK_GetNameOfCountry(iDstCountry));
mess.strMessage = buf;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_NETMESSAGE, &mess);
/* Update date structures */
RISK_SetNumArmiesOfCountry(iDstCountry,
RISK_GetNumArmiesOfCountry(iDstCountry) + iNumArmies);
RISK_SetNumArmiesOfCountry(iSrcCountry,
RISK_GetNumArmiesOfCountry(iSrcCountry) - iNumArmies);
}
/************************************************************************
* FUNCTION: GAME_PlaceArmies
* HISTORY:
* 03.04.94 ESF Stubbed.
* 05.19.94 ESF Coded and added verbose message across network.
* 10.02.94 ESF Added support for PLACE_ALL.
* 04.30.95 ESF Moved the dialog code from to PlaceClick.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_PlaceArmies(Int32 iCountry, Int32 iArmies)
{
MsgPlaceNotify msgPlaceNotify;
char buf[256];
/* Send a message to the server about this */
msgPlaceNotify.iCountry = iCountry;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_PLACENOTIFY, &msgPlaceNotify);
/* Send a verbose message */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s placing %d %s on %s",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s place %d %s en %s",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
iArmies,
iArmies == 1 ? "army" : "armies",
RISK_GetNameOfCountry(iCountry));
mess.strMessage = buf;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_NETMESSAGE, &mess);
/* Once we've placed, can't exchange cards anymore */
fCanExchange = FALSE;
/* Update the data structures */
RISK_SetNumArmiesOfPlayer(iCurrentPlayer,
RISK_GetNumArmiesOfPlayer(iCurrentPlayer)
- iArmies);
RISK_SetNumArmiesOfCountry(iCountry,
RISK_GetNumArmiesOfCountry(iCountry)+iArmies);
/* If we're out of armies, jump into attack mode */
if (!RISK_GetNumArmiesOfPlayer(iCurrentPlayer) && iState == STATE_PLACE)
{
iState = STATE_ATTACK;
UTIL_ServerEnterState(iState);
UTIL_DisplayActionCString(iState, iCurrentPlayer);
}
UTIL_DisplayActionCString(iState, iCurrentPlayer);
}
/************************************************************************
* FUNCTION: GAME_Attack
* HISTORY:
* 04.23.95 ESF Created.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_Attack(Int32 iSrcCountry, Int32 iDstCountry)
{
Int32 iActionState = RISK_GetAttackModeOfPlayer(iCurrentPlayer);
Int32 iDice = RISK_GetDiceModeOfPlayer(iCurrentPlayer);
/* Keep these so as to be able to repeat attack */
iLastAttackSrc = iSrcCountry;
iLastAttackDst = iDstCountry;
if (iActionState == ACTION_ATTACK)
{
if (GAME_ValidAttackSrc(iSrcCountry, TRUE) &&
GAME_ValidAttackDst(iSrcCountry, iDstCountry, TRUE) &&
GAME_ValidAttackDice(iDice, iSrcCountry))
{
GAME_DoAttack(iSrcCountry, iDstCountry, TRUE);
UTIL_DisplayActionCString(iState, iCurrentPlayer);
}
}
else if (iActionState == ACTION_DOORDIE)
{
Flag fNotify = TRUE;
while (GAME_ValidAttackSrc(iSrcCountry, FALSE) &&
GAME_ValidAttackDst(iSrcCountry, iDstCountry, FALSE) &&
GAME_ValidAttackDice(iDice, iSrcCountry))
{
GAME_DoAttack(iSrcCountry, iDstCountry, fNotify);
/* Only True the first time around */
fNotify = FALSE;
}
}
}
/************************************************************************
* FUNCTION: GAME_DoAttack
* HISTORY:
* 03.04.94 ESF Created.
* 03.06.94 ESF Added missing pieces.
* 03.07.94 ESF Fixed owner update bug & army subtraction bug.
* 03.18.94 ESF Added server notification.
* 03.28.94 ESF Added message when country is taken.
* 03.29.94 ESF Added more informative moving message.
* 04.02.94 ESF Fixed bug, darken country if wrong number of die.
* 04.02.94 ESF Only notify server if fCacheNotify == FALSE.
* 05.04.94 ESF Fixed bug, changed location of call to GAME_PlayerDied().
* 05.04.94 ESF Fixed bug, when end of game occurs.
* 05.19.94 ESF Added verbose notification when player takes country.
* 05.19.94 ESF Moved calculation of dice correctness to ValidDice.
* 11.06.94 ESF Cached results, so that Do-or-die runs quicker.
* 11.06.94 ESF Added attack notification.
* 01.17.95 ESF Changed fCacheNotify to fNotify.
* 07.09.95 JC Fixed a bug with ENDOFMISSION and FORCEEXCHANGECARDS.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_DoAttack(Int32 iSrcCountry, Int32 iDstCountry, Flag fNotify)
{
MsgAttackNotify msgAttackNotify;
Int32 iAttackDie, iDefendDie, iArmiesWon, iArmiesMove;
Int32 iSrc, iDst, iPlayerAttackDie;
char buf[256];
/* Cached values */
static Int32 iCachedSrcCountry=-1;
static Int32 iCachedDstCountry=-1;
iPlayerAttackDie = RISK_GetDiceModeOfPlayer(iCurrentPlayer);
/* If the dice have been selected automatically, calculate them */
if (iPlayerAttackDie != ATTACK_AUTO)
iAttackDie = iPlayerAttackDie+1;
else
iAttackDie = MIN(RISK_GetNumArmiesOfCountry(iSrcCountry)-1, 3);
iDefendDie = MIN(RISK_GetNumArmiesOfCountry(iDstCountry), 2);
/* Set the color of the dice if the attack has changed */
if (iCachedSrcCountry != iSrcCountry)
COLOR_CopyColor(COLOR_PlayerToColor(RISK_GetOwnerOfCountry(iSrcCountry)),
COLOR_DieToColor(0));
if (iCachedDstCountry != iDstCountry)
COLOR_CopyColor(COLOR_PlayerToColor(RISK_GetOwnerOfCountry(iDstCountry)),
COLOR_DieToColor(1));
/* Get the number of armies that the attacker won */
iArmiesWon = DICE_Attack(iAttackDie, iDefendDie,
RISK_GetOwnerOfCountry(iSrcCountry),
RISK_GetOwnerOfCountry(iDstCountry));
RISK_SetNumArmiesOfCountry(iDstCountry,
RISK_GetNumArmiesOfCountry(iDstCountry) - iArmiesWon);
RISK_SetNumArmiesOfCountry(iSrcCountry,
RISK_GetNumArmiesOfCountry(iSrcCountry) -
(MIN(iDefendDie, iAttackDie) - iArmiesWon));
/* Country was taken */
if (!RISK_GetNumArmiesOfCountry(iDstCountry))
{
/* The attacking player gets a card */
fGetsCard = TRUE;
/* Send a verbose message */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s captured %s from %s",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s capture %s à partir de %s",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iDstCountry),
RISK_GetNameOfCountry(iSrcCountry));
mess.strMessage = buf;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_NETMESSAGE, &mess);
/* Display informative message telling of victory */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s moving armies from %s to %s.",
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iSrcCountry),
RISK_GetNameOfCountry(iDstCountry));
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s conquière %s à partir de %s.",
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iDstCountry),
RISK_GetNameOfCountry(iSrcCountry));
#endif
UTIL_DisplayComment(buf);
/* Update data structures */
iSrc = RISK_GetOwnerOfCountry(iSrcCountry);
RISK_SetNumCountriesOfPlayer(iSrc, RISK_GetNumCountriesOfPlayer(iSrc)+1);
iDst = RISK_GetOwnerOfCountry(iDstCountry);
RISK_SetNumCountriesOfPlayer(iDst, RISK_GetNumCountriesOfPlayer(iDst)-1);
/* New owner */
RISK_SetOwnerOfCountry(iDstCountry, RISK_GetOwnerOfCountry(iSrcCountry));
/* See if this victory signalled a _MSG_ENDOFMISSION condition */
if (GAME_IsMissionAccomplied(iCurrentPlayer, iDst))
{
MsgEndOfMission msg;
msg.iWinner = iCurrentPlayer;
msg.iTyp = RISK_GetMissionTypeOfPlayer(iCurrentPlayer);
msg.iNum1 = RISK_GetMissionContinent1OfPlayer(iCurrentPlayer);
msg.iNum2 = RISK_GetMissionContinent2OfPlayer(iCurrentPlayer);
RISK_SetMissionTypeOfPlayer(iCurrentPlayer, NO_MISSION);
(void)RISK_SendSyncMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_ENDOFMISSION, &msg,
MSG_ENDOFMISSION, CBK_IncomingMessage);
/* If the winner stop the game ... */
if (iState == STATE_REGISTER)
return;
}
/* Move the desired number of armies if it isn't end of game */
if (RISK_GetNumCountriesOfPlayer(iDst) != 0 ||
RISK_GetNumLivePlayers() != 2)
{
iArmiesMove = UTIL_GetArmyNumber(iAttackDie,
RISK_GetNumArmiesOfCountry(iSrcCountry)-1,
FALSE);
RISK_SetNumArmiesOfCountry(iSrcCountry,
RISK_GetNumArmiesOfCountry(iSrcCountry) -
iArmiesMove);
RISK_SetNumArmiesOfCountry(iDstCountry,
RISK_GetNumArmiesOfCountry(iDstCountry) +
iArmiesMove);
}
/* See if this victory signalled a _DEADPLAYER or _ENDOFGAME condition */
if (RISK_GetNumCountriesOfPlayer(iDst) == 0)
GAME_PlayerDied(iDst);
}
else
{
/* If a new attack and not do-or-die, notify the server */
if (!(iCachedSrcCountry == iSrcCountry &&
iCachedDstCountry == iDstCountry))
{
/* Send a verbose message */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s is attacking from %s to %s",
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iSrcCountry),
RISK_GetNameOfCountry(iDstCountry));
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s attaque %s à partir de %s",
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iDstCountry),
RISK_GetNameOfCountry(iSrcCountry));
#endif
mess.strMessage = buf;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_NETMESSAGE, &mess);
UTIL_DisplayComment(buf);
}
}
/* If we are told to notify, then notify */
if (fNotify == TRUE)
{
msgAttackNotify.iSrcCountry = iSrcCountry;
msgAttackNotify.iDstCountry = iDstCountry;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_ATTACKNOTIFY, &msgAttackNotify);
}
/* Update cached values */
iCachedSrcCountry = iSrcCountry;
iCachedDstCountry = iDstCountry;
}
/************************************************************************
* FUNCTION: GAME_IsEnemyAdjacent
* HISTORY:
* 03.05.94 ESF Created.
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_IsEnemyAdjacent(Int32 iCountry)
{
Int32 i, iPlayer;
iPlayer = RISK_GetOwnerOfCountry(iCountry);
for (i=0; i!=6 && RISK_GetAdjCountryOfCountry(iCountry, i)!=-1; i++)
if (RISK_GetOwnerOfCountry(RISK_GetAdjCountryOfCountry(iCountry, i)) !=
iPlayer)
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_IsFrontierAdjacent
* HISTORY:
* 03.08.95 JC Created.
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_IsFrontierAdjacent(Int32 srcCountry, Int32 destCountry)
{
Int32 iPlayer, iContinent, iCountry, i;
iPlayer = RISK_GetOwnerOfCountry(srcCountry);
iContinent = RISK_GetContinentOfCountry(srcCountry);
if (RISK_GetContinentOfCountry(destCountry) != iContinent)
return GAME_IsEnemyAdjacent(destCountry);
for (i=0; i!=6 && RISK_GetAdjCountryOfCountry(destCountry, i)!=-1; i++)
{
iCountry = RISK_GetAdjCountryOfCountry(destCountry, i);
if (RISK_GetContinentOfCountry(iCountry) != iContinent)
return TRUE;
}
return FALSE;
}
/************************************************************************
* FUNCTION: FindEnemyAdjacent
* HISTORY:
* 11.08.95 JC Created.
* PURPOSE:
* NOTES:
************************************************************************/
Int32 FindEnemyAdjacent(Int32 iCountry, Int32 distance)
{
Int32 i, min, res, iPlayer, dest;
iPlayer = RISK_GetOwnerOfCountry(iCountry);
min = 100000;
for (i=0; i!=6 && RISK_GetAdjCountryOfCountry(iCountry, i)!=-1; i++)
{
dest = RISK_GetAdjCountryOfCountry(iCountry, i);
if (RISK_GetOwnerOfCountry(dest) == iPlayer)
{
if (distance <= 3)
{
res = FindEnemyAdjacent(dest, distance + 1);
if (res < min)
min = res;
}
}
else
min = 0;
}
return (min + 1);
}
/************************************************************************
* FUNCTION: GAME_FindEnemyAdjacent
* HISTORY:
* 11.08.95 JC Created.
* PURPOSE:
* NOTES:
************************************************************************/
Int32 GAME_FindEnemyAdjacent(Int32 iCountry)
{
Int32 i, min, res, iPlayer, dest, destCountry;
iPlayer = RISK_GetOwnerOfCountry(iCountry);
destCountry = -1;
min = 100000;
for (i=0; i!=6 && RISK_GetAdjCountryOfCountry(iCountry, i)!=-1; i++)
{
dest = RISK_GetAdjCountryOfCountry(iCountry, i);
if (RISK_GetOwnerOfCountry(dest) == iPlayer)
{
res = FindEnemyAdjacent(dest, 0);
if (res < min)
{
min = res;
destCountry = dest;
}
}
else
min = 0;
}
return (destCountry);
}
/************************************************************************
* FUNCTION: GAME_AttackClick
* HISTORY:
* 03.06.94 ESF Created.
* 04.02.94 ESF Added notification of server after Do-or-Die.
* 05.19.94 ESF Added verbose message across network.
* 05.19.94 ESF Fixed for fixed attack dice do-or-die.
* 07.14.94 ESF Fixed bug, bot checking attack dice on dest country.
* 10.15.94 ESF Added printing of "Attacking from foo to bar" locally.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_AttackClick(Int32 iCountry)
{
char buf[256];
if (iAttackSrc < 0)
{
if (GAME_ValidAttackSrc(iCountry, TRUE) &&
GAME_ValidAttackDice(RISK_GetDiceModeOfPlayer(iCurrentPlayer),
iCountry))
{
iAttackSrc = iCountry;
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s is attacking from %s to"
" <click on territory>",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s attaque à partir de %s ",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iAttackSrc));
UTIL_DisplayComment(buf);
UTIL_LightCountry(iAttackSrc);
}
}
else if (GAME_ValidAttackDst(iAttackSrc, iCountry, TRUE) &&
GAME_ValidAttackDice(RISK_GetDiceModeOfPlayer(iCurrentPlayer),
iAttackSrc))
{
iAttackDst = iCountry;
GAME_Attack(iAttackSrc, iAttackDst);
iAttackSrc = iAttackDst = -1;
UTIL_DisplayActionCString(iState, iCurrentPlayer);
}
}
/************************************************************************
* FUNCTION: GAME_ValidAttackDice
* HISTORY:
* 05.19.94 ESF Created.
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_ValidAttackDice(Int32 iAttackDice, Int32 iCountry)
{
char buf[256];
/* If the dice have been selected manually, check them */
if (iAttackDice != ATTACK_AUTO)
{
if (RISK_GetNumArmiesOfCountry(iCountry) <= iAttackDice+1)
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "You can't attack with %d dice, dummy.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "Attaque impossible avec %d dé(s).",
#endif
iAttackDice+1);
UTIL_DarkenCountry(iCountry);
UTIL_DisplayError(buf);
return FALSE;
}
}
return TRUE;
}
/************************************************************************
* FUNCTION: GAME_ValidAttackSrc
* HISTORY:
* 03.06.94 ESF Created.
* 03.07.94 ESF Added verbose option
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_ValidAttackSrc(Int32 iAttackSrc, Flag fVerbose)
{
char buf[256];
if (iAttackSrc < 0)
{
if (fVerbose)
#ifdef ENGLISH
UTIL_DisplayError("Where the hell are you trying to attack from?");
#endif
#ifdef FRENCH
UTIL_DisplayError("D'où voulez-vous attaquer?");
#endif
}
else if (iAttackSrc >= NUM_COUNTRIES)
{
if (fVerbose)
#ifdef ENGLISH
UTIL_DisplayError("You can't attack from the ocean!");
#endif
#ifdef FRENCH
UTIL_DisplayError("Attaque impossible à partir de l'océan!");
#endif
}
else if (RISK_GetOwnerOfCountry(iAttackSrc) != iCurrentPlayer)
{
if (fVerbose)
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "You can't attack from %s -- you don't own it.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "Attaque impossible à partir de %s.",
#endif
RISK_GetNameOfCountry(iAttackSrc));
UTIL_DisplayError(buf);
}
}
else if (!GAME_IsEnemyAdjacent(iAttackSrc))
{
if (fVerbose)
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "There are no enemy territories you can attack from %s.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "Pas de territoire à attaquer à partir de %s.",
#endif
RISK_GetNameOfCountry(iAttackSrc));
UTIL_DisplayError(buf);
}
}
else if (RISK_GetNumArmiesOfCountry(iAttackSrc) == 1)
{
if (fVerbose)
#ifdef ENGLISH
UTIL_DisplayError("You can't attack with 1 army.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Attaque impossible avec une seule armée.");
#endif
}
else
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_ValidAttackDst
* HISTORY:
* 03.06.94 ESF Created.
* 03.07.94 ESF Added verbose option
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_ValidAttackDst(Int32 iAttackSrc, Int32 iAttackDst, Flag fVerbose)
{
char buf[256];
if (iAttackDst < 0)
{
if (fVerbose)
#ifdef ENGLISH
UTIL_DisplayError("Where the hell are you trying to attack from?");
#endif
#ifdef FRENCH
UTIL_DisplayError("Où voulez-vous attaquer?");
#endif
}
else if (iAttackDst >= NUM_COUNTRIES)
{
if (fVerbose)
#ifdef ENGLISH
UTIL_DisplayError("You can't attack the ocean!");
#endif
#ifdef FRENCH
UTIL_DisplayError("Impossible d'attaquer l'océan!");
#endif
}
else
{
if (!GAME_CanAttack(iAttackSrc, iAttackDst))
{
if (fVerbose)
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "You can't attack %s from %s!",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "Attaque impossible de %s à partir de %s!",
#endif
RISK_GetNameOfCountry(iAttackDst),
RISK_GetNameOfCountry(iAttackSrc));
UTIL_DisplayError(buf);
}
}
else if (RISK_GetOwnerOfCountry(iAttackDst) == iCurrentPlayer)
{
if (fVerbose)
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "You can't attack %s -- you own it.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "Attaque impossible de son propre territoire (%s).",
#endif
RISK_GetNameOfCountry(iAttackDst));
UTIL_DisplayError(buf);
}
}
else
return TRUE;
}
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_PlaceClick
* HISTORY:
* 03.07.94 ESF Created.
* 03.16.94 ESF Added support for placing multiple armies.
* 05.19.94 ESF Factored out code to GAME_PlaceArmies.
* 04.30.95 ESF Brought the dialog code from PlaceArmies here.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_PlaceClick(Int32 iCountry, Int32 iPlaceType)
{
Int32 maxN;
/* Is the country picked the player's? */
if (GAME_ValidPlaceDst(iCountry)) {
Int32 iArmies;
maxN = RISK_GetNumArmiesOfPlayer(iCurrentPlayer);
#ifndef DROPALL
if ( iState == STATE_FORTIFY )
maxN = (maxN > DROP_ARMIES) ? DROP_ARMIES : maxN;
#endif
/* Depending on the type of placement, popup a dialog or not */
if (iPlaceType == PLACE_MULTIPLE)
{
iArmies = UTIL_GetArmyNumber(1, maxN, TRUE);
if (!iArmies)
return;
}
else if (iPlaceType == PLACE_ALL)
iArmies = maxN;
else
iArmies = 1;
GAME_PlaceArmies(iCountry, iArmies);
/* If we're fortifying, we're done. */
if (iState == STATE_FORTIFY)
GAME_EndTurn();
}
}
/************************************************************************
* FUNCTION: GAME_ValidPlaceDst
* HISTORY:
* 03.07.94 ESF Created.
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_ValidPlaceDst(Int32 iPlaceDst)
{
if (iPlaceDst < 0)
#ifdef ENGLISH
UTIL_DisplayError("What the hell country is that!");
#endif
#ifdef FRENCH
UTIL_DisplayError("What the hell country is that!");
#endif
else if (iPlaceDst >= NUM_COUNTRIES)
#ifdef ENGLISH
UTIL_DisplayError("Ahhh...That's the ocean, buddy...");
#endif
#ifdef FRENCH
UTIL_DisplayError("Ahhh...C'est l'océan, plouf...");
#endif
else if (RISK_GetOwnerOfCountry(iPlaceDst) != iCurrentPlayer)
#ifdef ENGLISH
UTIL_DisplayError("You cannot place armies on opponent's territories.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Donner des armées à un autre joueur?");
#endif
else
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_MoveClick
* HISTORY:
* 03.07.94 ESF Created.
* 03.29.94 ESF Added a message when iMoveDst is verified.
* 03.29.94 ESF Fixed lack of UTIL_DisplayComment() bug.
* 01.09.95 JC End turn only if iState asn't moved to STATE_REGISTER.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_MoveClick(Int32 iCountry)
{
Int32 iNumArmies;
char buf[256];
if (iMoveSrc < 0)
{
if (GAME_ValidMoveSrc(iCountry))
{
iMoveSrc = iCountry;
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s moving armies from %s to"
" <click on territory>",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s déplace des armées de %s en"
" <click on territory>",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iMoveSrc));
UTIL_DisplayComment(buf);
UTIL_LightCountry(iMoveSrc);
}
}
else if (GAME_ValidMoveDst(iMoveSrc, iCountry))
{
iMoveDst = iCountry;
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s moving armies from %s to %s.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s déplace des armées de %s en %s.",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNameOfCountry(iMoveSrc),
RISK_GetNameOfCountry(iMoveDst));
UTIL_DisplayComment(buf);
iNumArmies = UTIL_GetArmyNumber(1,
RISK_GetNumArmiesOfCountry(iMoveSrc)-1,
TRUE);
if (iNumArmies>0)
{
GAME_MoveArmies(iMoveSrc, iMoveDst, iNumArmies);
if (iState != STATE_REGISTER)
/* Turn over, man! */
GAME_EndTurn();
}
else
{
UTIL_DarkenCountry(iMoveSrc);
iMoveSrc = iMoveDst = -1;
}
}
}
/************************************************************************
* FUNCTION: GAME_ValidMoveSrc
* HISTORY:
* 03.07.94 ESF Created.
* 03.29.94 ESF fixed a small bug, inserted "else".
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_ValidMoveSrc(Int32 iMoveSrc)
{
if (iMoveSrc < 0)
#ifdef ENGLISH
UTIL_DisplayError("Where the hell are you trying to move from?");
#endif
#ifdef FRENCH
UTIL_DisplayError("Where the hell are you trying to move from?");
#endif
else if (iMoveSrc >= NUM_COUNTRIES)
#ifdef ENGLISH
UTIL_DisplayError("You can't move armies from the ocean.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Impossible de déplacer des armées à partir de l'océan.");
#endif
else if (RISK_GetOwnerOfCountry(iMoveSrc) != iCurrentPlayer)
#ifdef ENGLISH
UTIL_DisplayError("You cannot move armies from an opponent's"
" territories.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Impossible de prendre les armées d'un autre joueur.");
#endif
else if (RISK_GetNumArmiesOfCountry(iMoveSrc) == 1)
#ifdef ENGLISH
UTIL_DisplayError("You cannot move from territories that"
" have one army to begin with.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Impossible de déplacer la seule armée d'un territoire.");
#endif
else
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_ValidMoveDst
* HISTORY:
* 03.07.94 ESF Created.