-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathriskgame.c
1860 lines (1615 loc) · 53.8 KB
/
riskgame.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 (elan@aetherworks.com)
*
* 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: riskgame.c,v 1.16 2000/01/23 20:10:09 tony Exp $
*/
/** \file
* "World" used by server as well as client
*/
/*
*
* $Log: riskgame.c,v $
* Revision 1.16 2000/01/23 20:10:09 tony
* oops, needed another commit for doxygen :-)
*
* Revision 1.14 2000/01/09 16:06:23 tony
* oops, wrong doxygen tags
*
* Revision 1.13 2000/01/04 21:41:53 tony
* removed redundant stuff for jokers
*
* Revision 1.12 1999/12/14 19:10:50 tony
* got rid of this annoying message:
* RISK_GetNthPlayerAtClient got 0, numplayers returned 2. to fix!!
* lets hope the assertion is just wrong
*
* Revision 1.11 1999/11/28 14:28:47 tony
* nothing special
*
* Revision 1.10 1999/11/27 19:19:07 tony
* oops :-) only 40 in MessageNames
*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "callbacks.h"
#include "network.h"
#include "utils.h"
#include "riskgame.h"
#include "debug.h"
#include "language.h"
/* Private variables */
static FILE *hLogFile = (FILE *)NULL;
/* Private functions */
static Int32 _RISK_GetMsgType(Int32 iField);
static Int32 _RISK_GetIntValue(Int32 iField, Int32 iIndex1, Int32 iIndex2);
static CString _RISK_GetStrValue(Int32 iField, Int32 iIndex1, Int32 iIndex2);
static void *_RISK_BuildMsg(Int32 iMessType, Int32 iField,
Int32 iIndex1, Int32 iIndex2,
CString strNewValue, Int32 iNewValue);
static void _RISK_Replicate(Int32 iField, Int32 iIndex1, Int32 iIndex2,
CString strNewValue, Int32 iNewValue);
/****************************************/
typedef struct _ContinentObject
{
CString strName;
Int32 iValue;
Int32 iNumCountries;
} ContinentObject;
/****************************************/
typedef struct _CountryObject
{
CString strName; /* Name of the country */
Int32 iContinent; /* The continent it forms part of */
Int32 iNumArmies; /* Number of armies on the country */
Int32 iTextX, iTextY; /* Where to write the text */
Int32 piOwner; /* The player it belongs to */
Int32 piAdjCountries[6]; /* All attackable countries */
} CountryObject;
/****************************************/
typedef struct _PlayerObject
{
Int32 iAllocationState;
Int32 iAttackMode, iDiceMode, iMsgDstMode;
Int32 iSpecies;
Flag iState;
Int32 iClient;
CString strName, strColor;
Int32 iCountriesOwned, iNumArmies, iNumCards;
Int32 piCards[MAX_CARDS];
Int16 typOfMission;
Int32 mission1, mission2;
} PlayerObject;
/****************************************/
typedef struct _SpeciesObject
{
Int32 iAllocationState;
Int32 iClient;
CString strName;
CString strAuthor;
CString strVersion;
CString strDescription;
struct _SpeciesObject *next;
} SpeciesObject;
#ifdef LOGGING
static char * MessageNames[] = {
"MSG_NOMESSAGE",
"MSG_OLDREGISTERCLIENT",
"unused 0x02",
"MSG_EXCHANGECARDS",
"MSG_REQUESTCARD",
"MSG_CARDPACKET",
"MSG_CARDPACKET",
"MSG_SENDMESSAGE",
"MSG_SENDMESSAGE",
"MSG_EXIT",
"MSG_STARTGAME",
"MSG_TURNNOTIFY",
"MSG_ENDTURN",
"MSG_CLIENTIDENT",
"MSG_ENTERSTATE",
"unused 0x0F"
"MSG_ENDOFGAME",
"MSG_OBJSTRUPDATE",
"MSG_OBJINTUPDATE",
"PLR_MISSION",
"unused 0x14",
"unused 0x15",
"MSG_DEREGISTERCLIENT",
"unused 0x17",
"unused 0x18",
"MSG_ALLOCPLAYER"
"unused 0x1a",
"unused 0x1b",
"unused 0x1c",
"unused 0x1d",
"unused 0x1e",
"unused 0x1d",
"MSG_FREEPLAYER"
"MSG_NETMESSAGE"
"MSG_NETPOPUP"
"MSG_POPUPREGISTERBOX"
"MSG_DICEROLL"
"MSG_PLACENOTIFY"
"MSG_ATTACKNOTIFY",
"MSG_MOVENOTIFY",
"MSG_POLLCLIENTS",
"unused 0x29",
"MSG_HELLO",
"MSG_VERSION",
"MSG_ENDOFMISSION",
"MSG_VICTORY",
"MSG_VICTORY"
};
#endif
/****************************************/
typedef struct _Game
{
void (*ReplicateCallback)(Int32, void *, Int32, Int32);
void (*FailureCallback)(CString, Int32);
void (*PreViewCallback)(Int32, void *);
void (*PostViewCallback)(Int32, void *);
/* The databases */
SpeciesObject pSpecies;
ContinentObject pContinents[NUM_CONTINENTS];
PlayerObject pPlayers[MAX_PLAYERS];
CountryObject pCountries[NUM_COUNTRIES];
} Game;
/* The object (eventually do this in a loop?) */
static Game RiskGame =
{
NULL, NULL, NULL, NULL,
{
ALLOC_COMPLETE,
-1,HUMAN,UNKNOWN,
"0.001",
PINK_CREATURE,
(SpeciesObject *)NULL
},
{
{ NORTH_AMERICA, 5, 9 },
{ SOUTH_AMERICA, 2, 4 },
{ AFRICA, 3, 6 },
{ AUSTRALIA, 2, 4 },
{ ASIA, 7, 12 },
{ EUROPE, 5, 7 },
},
{
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
{ ALLOC_NONE, 1, 3, 0, 0, PLAYER_ALIVE, -1, NULL, NULL, 0, 0, 0,
{0,0,0,0,0,0,0,0,0,0}, NO_MISSION, -1, -1 },
},
{
{ GREENLAND, CNT_NORTHAMERICA, 0, 264, 32, -1, /* 0 */
{1, 5, 10, 11, -1, -1} },
{ ICELAND, CNT_EUROPE, 0, 342, 51, -1,
{0, 7, 14, -1, -1, -1} },
{ SIBERIA, CNT_ASIA, 0, 622, 91, -1,
{3, 6, 13, 21, 22, -1} },
{ URAL, CNT_ASIA, 0, 557, 106, -1,
{2, 8, 15, 22, -1, -1} },
{ ALASKA, CNT_NORTHAMERICA, 0, 13, 61, -1,
{5, 9, 12, -1, -1, -1} },
{ NORTH_WEST, CNT_NORTHAMERICA, 0, 61, 62, -1, /* 5 */
{0, 4, 11, 12, -1, -1} },
{ IRKUTSK, CNT_ASIA, 0, 700, 71, -1,
{2, 9, 13, -1, -1, -1} },
{ SCANDINAVIA, CNT_EUROPE, 0, 387, 85, -1,
{1, 8, 14, 16, -1, -1} },
{ UKRAINE, CNT_EUROPE, 0, 450, 103, -1,
{3, 7, 15, 16, 20, 24}},
{ KAMCHATKA, CNT_ASIA, 0, 731, 121, -1,
{4, 6, 13, 21, 23, -1} },
{ QUEBEC, CNT_NORTHAMERICA, 0, 181, 95, -1, /* 10 */
{0, 11, 18, -1, -1, -1} },
{ ONTARIO, CNT_NORTHAMERICA, 0, 116, 97, -1,
{0, 5, 10, 12, 17, 18} },
{ ALBERTA, CNT_NORTHAMERICA, 0, 55, 90, -1,
{4, 5, 11, 17, -1, -1} },
{ YAKUTSK, CNT_ASIA, 0, 690, 111, -1,
{2, 6, 9, 21, -1, -1} },
{ GREAT_BRITAIN, CNT_EUROPE, 0, 338, 110, -1,
{1, 7, 16, 19, -1, -1} },
{ AFGHANISTAN, CNT_ASIA, 0, 534, 141, -1, /* 15 */
{3, 8, 22, 24, 28, -1} },
{ NORTHERN_EUROPE, CNT_EUROPE, 0, 386, 113, -1,
{7, 8, 14, 19, 20, -1} },
{ WEST_STATES, CNT_NORTHAMERICA, 0, 62, 126, -1,
{11, 12, 18, 25, -1, -1} },
{ EAST_STATES, CNT_NORTHAMERICA, 0, 126, 145, -1,
{10, 11, 17, 25, -1, -1} },
{ WEST_EUROPE, CNT_EUROPE, 0, 338, 148, -1,
{14, 16, 20, 26, -1, -1} },
{ SOUTH_EUROPE, CNT_EUROPE, 0, 416, 134, -1, /* 20 */
{8, 16, 19, 24, 26, 27} },
{ MONGOLIA, CNT_ASIA, 0, 685, 139, -1,
{2, 9, 13, 22, 23, -1} },
{ CHINA, CNT_ASIA, 0, 632, 163, -1,
{2, 3, 15, 21, 28, 29} },
{ JAPAN, CNT_ASIA, 0, 735, 166, -1,
{9, 21, -1, -1, -1, -1} },
{ MIDDLE_EAST, CNT_ASIA, 0, 477, 193, -1,
{8, 15, 20, 27, 28, 30} },
{ CENTRAL_AMERICA, CNT_NORTHAMERICA, 0, 87, 165, -1, /* 25 */
{17, 18, 32, -1, -1, -1}},
{ NORTH_AFRICA, CNT_AFRICA, 0, 339, 214, -1,
{19, 20, 27, 30, 33, 35} },
{ EGYPT, CNT_AFRICA, 0, 407, 194, -1,
{20, 24, 26, 30, -1, -1} },
{ INDIA, CNT_ASIA, 0, 572, 194, -1,
{15, 22, 24, 29, -1, -1} },
{ SIAM, CNT_ASIA, 0, 637, 207, -1,
{22, 28, 31, -1, -1, -1} },
{ EAST_AFRICA, CNT_AFRICA, 0, 437, 231, -1, /* 30 */
{24, 26, 27, 35, 37, 40} },
{ INDONESIA, CNT_AUSTRALIA, 0, 672, 262, -1,
{29, 36, 38, -1, -1, -1} },
{ VENEZUELA, CNT_SOUTHAMERICA, 0, 188, 230, -1,
{25, 33, 34, -1, -1, -1} },
{ BRASIL, CNT_SOUTHAMERICA, 0, 233, 277, -1,
{26, 32, 34, 41, -1, -1} },
{ PERU, CNT_SOUTHAMERICA, 0, 194, 294, -1,
{32, 33, 41, -1, -1, -1}},
{ CONGO, CNT_AFRICA, 0, 410, 271, -1, /* 35 */
{26, 30, 37, -1, -1, -1} },
{ NEW_GUINEA, CNT_AUSTRALIA, 0, 751, 276, -1,
{31, 38, 39, -1, -1, -1} },
{ SOUTH_AFRICA, CNT_AFRICA, 0, 416, 326, -1,
{30, 35, 40, -1, -1, -1} },
{ WESTERN_AUSTRALIA, CNT_AUSTRALIA, 0, 700, 335, -1,
{31, 36, 39, -1, -1, -1} },
{ EASTERN_AUSTRALIA, CNT_AUSTRALIA, 0, 756, 343, -1,
{36, 38, -1, -1, -1, -1} },
{ MADAGASCAR, CNT_AFRICA, 0, 479, 321, -1, /* 40 */
{30, 37, -1, -1, -1, -1} },
{ ARGENTINA, CNT_SOUTHAMERICA, 0, 189, 352, -1,
{33, 34, -1, -1, -1, -1} },
}
};
/* Species related private function */
SpeciesObject *_RISK_GetSpecies(Int32 iHandle);
/**
* Init callbacks
*
* \b History:
* \tag 07.17.94 ESF Created.
* \tag 08.13.94 ESF Rewrote.
* \b Notes:
* \parThis function is called by both client and server.
*/
void RISK_InitObject(void (*ReplicateCallback)(Int32, void *, Int32, Int32),
void (*PreViewCallback)(Int32, void *),
void (*PostViewCallback)(Int32, void *),
void (*FailureCallback)(CString, Int32),
FILE *hDebug)
{
RiskGame.ReplicateCallback = ReplicateCallback;
RiskGame.FailureCallback = FailureCallback;
RiskGame.PreViewCallback = PreViewCallback;
RiskGame.PostViewCallback = PostViewCallback;
/* The message callback has to be set -- it handles replication. */
D_Assert(RiskGame.ReplicateCallback, "ReplicateCallback needs to be set!");
/* Save this */
hLogFile = hDebug;
}
/************************************************************************
* FUNCTION: _RISK_Replicate
* HISTORY:
* 05.03.94 ESF Created.
* 06.24.94 ESF Fixed memory leak.
* 07.16.94 ESF Added assert.
* 08.16.94 ESF Changed so that both can get callback (Server/Client).
* 08.18.94 ESF Changed so that callback handles actual replication.
* PURPOSE:
* NOTES:
************************************************************************/
void _RISK_Replicate(Int32 iField, Int32 iIndex1, Int32 iIndex2,
CString strNewValue, Int32 iNewValue)
{
void *pvMess;
Int32 iMessType;
/* Get the message and the message type */
iMessType = _RISK_GetMsgType(iField);
pvMess = _RISK_BuildMsg(iMessType, iField,
iIndex1, iIndex2,
strNewValue, iNewValue);
D_Assert(pvMess, "Got back NULL message!");
/* Call the callback for messages (before changing dist. obj.) */
if (RiskGame.PreViewCallback)
RiskGame.PreViewCallback(iMessType, pvMess);
/* Actually set the value (sort of roundabout :) Everything
* to set the message follows the same code path now, so
* it may be a bit roundabout but at least it's consistant.
*/
RISK_ProcessMessage(iMessType, pvMess);
/* Call the callback for messages (after changing dist. obj.) */
if (RiskGame.PostViewCallback)
RiskGame.PostViewCallback(iMessType, pvMess);
/* Call the message handler. If there wasn't one, then
* we would have a problem, since it handles the actual replication.
* This involves sending a message to the server, or broadcasting
* a message out, depending on whether a client or server has
* registered this callback. The last parameter doesn't matter in
* this case, because it's an outgoing message (the last parameter
* is the message source).
*/
RiskGame.ReplicateCallback(iMessType, pvMess, MESS_OUTGOING, -1);
/* Delete the memory the message was taking */
MEM_Free(pvMess);
}
/************************************************************************
* FUNCTION: RISK_SelectiveReplicate
* HISTORY:
* 05.06.94 ESF Created.
* 05.10.94 ESF Added needed check for existence of callback.
* 07.16.94 ESF Fixed loop bug, added assertions.
* 08.28.94 ESF Changed to take indices, instead of ranges.
* 01.01.95 ESF Fixed to remove a memory leak.
* PURPOSE:
* Sends data to a client, for purposes of data synchronicity.
* NOTES:
************************************************************************/
void RISK_SelectiveReplicate(Int32 iSocket, Int32 iField,
Int32 iIndex1, Int32 iIndex2)
{
void *pvMess;
Int32 iValue = 0, iMessType;
CString strValue = NULL;
/* Type of the message */
iMessType = _RISK_GetMsgType(iField);
/* Get the value... */
if (iMessType == MSG_OBJINTUPDATE)
iValue = _RISK_GetIntValue(iField, iIndex1, iIndex2);
else
strValue = _RISK_GetStrValue(iField, iIndex1, iIndex2);
/* And build the message... */
pvMess = _RISK_BuildMsg(iMessType, iField,
iIndex1, iIndex2,
strValue, iValue);
(void)RISK_SendMessage(iSocket, iMessType, pvMess);
/* Delete the memory the message took up */
MEM_Free(pvMess);
}
/************************************************************************
* FUNCTION: RISK_ResetObj
* HISTORY:
* 05.02.94 ESF Created.
* PURPOSE:
* NOTES:
************************************************************************/
void RISK_ResetObj(void)
{
Int32 i, iPlayer;
/* Init the needed fields of the players */
for (i=0; i<RISK_GetNumPlayers(); i++)
{
iPlayer = RISK_GetNthPlayer(i);
RISK_SetAttackModeOfPlayer(iPlayer, 1);
RISK_SetStateOfPlayer(iPlayer, PLAYER_ALIVE);
RISK_SetClientOfPlayer(iPlayer, -1);
RISK_SetNumCountriesOfPlayer(iPlayer, 0);
RISK_SetNumArmiesOfPlayer(iPlayer, 0);
RISK_SetNumCardsOfPlayer(iPlayer, 0);
RISK_SetAllocationStateOfPlayer(iPlayer, ALLOC_NONE);
RISK_SetMissionTypeOfPlayer(iPlayer, NO_MISSION);
}
/* Init the needed fields of the countries */
for (i=0; i!=NUM_COUNTRIES; i++)
{
RISK_SetOwnerOfCountry(i, -1);
RISK_SetNumArmiesOfCountry(i, 0);
}
}
/************************************************************************
* FUNCTION: RISK_ResetGame
* HISTORY:
* 05.12.94 ESF Created.
* PURPOSE:
* New game, keep players.
* NOTES:
************************************************************************/
void RISK_ResetGame(void)
{
Int32 i, iPlayer;
/* Init the needed fields of the players */
for (i=0; i<RISK_GetNumPlayers(); i++)
{
iPlayer = RISK_GetNthPlayer(i);
RISK_SetStateOfPlayer(iPlayer, PLAYER_ALIVE);
RISK_SetNumCountriesOfPlayer(iPlayer, 0);
RISK_SetNumCardsOfPlayer(iPlayer, 0);
RISK_SetNumArmiesOfPlayer(iPlayer, 0);
RISK_SetMissionTypeOfPlayer(iPlayer, NO_MISSION);
}
/* Init the needed fields of the countries */
for (i=0; i!=NUM_COUNTRIES; i++)
{
RISK_SetOwnerOfCountry(i, -1);
RISK_SetNumArmiesOfCountry(i, 0);
}
}
/************************************************************************
* FUNCTION: RISK_ProcessMessage
* HISTORY:
* 05.02.94 ESF Created.
* 05.10.94 ESF Added needed check for existence of callback.
* 02.15.95 ESF Moved the callback code to _after_ the update.
* PURPOSE:
* NOTES:
************************************************************************/
void RISK_ProcessMessage(Int32 iMessType, void *pvMess)
{
Int32 iIndex1, iIndex2, iValue, iField;
CString strValue;
MsgObjIntUpdate *pIntMess = (MsgObjIntUpdate *)pvMess;
MsgObjStrUpdate *pStrMess = (MsgObjStrUpdate *)pvMess;
D_Assert(pvMess, "pvMess is NULL!");
if (iMessType == MSG_OBJINTUPDATE)
{
iField = pIntMess->iField;
iIndex1 = pIntMess->iIndex1;
iIndex2 = pIntMess->iIndex2;
iValue = pIntMess->iNewValue;
switch (iField)
{
case PLR_ATTACKMODE:
RiskGame.pPlayers[iIndex1].iAttackMode = iValue;
break;
case PLR_DICEMODE:
RiskGame.pPlayers[iIndex1].iDiceMode = iValue;
break;
case PLR_MSGDSTMODE:
RiskGame.pPlayers[iIndex1].iMsgDstMode = iValue;
break;
case PLR_STATE:
RiskGame.pPlayers[iIndex1].iState = iValue;
break;
case PLR_CLIENT:
RiskGame.pPlayers[iIndex1].iClient = iValue;
break;
case PLR_NUMCOUNTRIES:
RiskGame.pPlayers[iIndex1].iCountriesOwned = iValue;
break;
case PLR_NUMARMIES:
RiskGame.pPlayers[iIndex1].iNumArmies = iValue;
break;
case PLR_NUMCARDS:
RiskGame.pPlayers[iIndex1].iNumCards = iValue;
break;
case PLR_SPECIES:
RiskGame.pPlayers[iIndex1].iSpecies = iValue;
break;
case PLR_ALLOCATION:
RiskGame.pPlayers[iIndex1].iAllocationState = iValue;
break;
case PLR_CARD: /* TdH: one of the places where greenlandbug might be caught */
RiskGame.pPlayers[iIndex1].piCards[iIndex2] = iValue;
break;
case PLR_MISSION:
RiskGame.pPlayers[iIndex1].typOfMission = iValue;
break;
case PLR_MISSION1:
RiskGame.pPlayers[iIndex1].mission1 = iValue;
break;
case PLR_MISSION2:
RiskGame.pPlayers[iIndex1].mission2 = iValue;
break;
case CNT_NUMARMIES:
RiskGame.pCountries[iIndex1].iNumArmies = iValue;
break;
case CNT_OWNER:
RiskGame.pCountries[iIndex1].piOwner = iValue;
break;
case SPE_CLIENT:
{
SpeciesObject *s = _RISK_GetSpecies(iIndex1);
s->iClient = iValue;
}
break;
case SPE_ALLOCATION:
{
SpeciesObject *s = _RISK_GetSpecies(iIndex1);
s->iAllocationState = iValue;
}
break;
default:
D_Assert(FALSE, "Badly build MSG_OBJINTUPDATE!");
}
}
else
{
iField = pStrMess->iField;
iIndex1 = pStrMess->iIndex1;
iIndex2 = pStrMess->iIndex2;
strValue = pStrMess->strNewValue;
switch (iField)
{
case PLR_NAME:
if (RiskGame.pPlayers[iIndex1].strName != NULL)
MEM_Free(RiskGame.pPlayers[iIndex1].strName);
if (strValue)
{
RiskGame.pPlayers[iIndex1].strName =
(CString)MEM_Alloc(strlen(strValue)+1);
strcpy(RiskGame.pPlayers[iIndex1].strName, strValue);
}
else
RiskGame.pPlayers[iIndex1].strName = NULL;
break;
case PLR_COLORSTRING:
if (RiskGame.pPlayers[iIndex1].strColor != NULL)
MEM_Free(RiskGame.pPlayers[iIndex1].strColor);
if (strValue)
{
RiskGame.pPlayers[iIndex1].strColor =
(CString)MEM_Alloc(strlen(strValue)+1);
strcpy(RiskGame.pPlayers[iIndex1].strColor, strValue);
}
else
RiskGame.pPlayers[iIndex1].strColor = NULL;
break;
case SPE_NAME:
{
SpeciesObject *s = _RISK_GetSpecies(iIndex1);
if (s->strName != NULL)
MEM_Free(s->strName);
if (strValue)
{
s->strName = (CString)MEM_Alloc(strlen(strValue)+1);
strcpy(s->strName, strValue);
}
else
s->strName = NULL;
}
break;
case SPE_VERSION:
{
SpeciesObject *s = _RISK_GetSpecies(iIndex1);
if (s->strVersion != NULL)
MEM_Free(s->strVersion);
if (strValue)
{
s->strVersion = (CString)MEM_Alloc(strlen(strValue)+1);
strcpy(s->strVersion, strValue);
}
else
s->strVersion = NULL;
}
break;
case SPE_DESCRIPTION:
{
SpeciesObject *s = _RISK_GetSpecies(iIndex1);
if (s->strDescription != NULL)
MEM_Free(s->strDescription);
if (strValue)
{
s->strDescription = (CString)MEM_Alloc(strlen(strValue)+1);
strcpy(s->strDescription, strValue);
}
else
s->strDescription = NULL;
}
break;
case SPE_AUTHOR:
{
SpeciesObject *s = _RISK_GetSpecies(iIndex1);
if (s->strAuthor != NULL)
MEM_Free(s->strAuthor);
if (strValue)
{
s->strAuthor = (CString)MEM_Alloc(strlen(strValue)+1);
strcpy(s->strAuthor, strValue);
}
else
s->strAuthor = strValue;
}
break;
default:
D_Assert(FALSE, "Badly build MSG_OBJSTRUPDATE!");
}
}
}
/***************************************/
void RISK_SetAttackModeOfPlayer(Int32 iPlayer, Int32 iMode)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_ATTACKMODE, iPlayer, 0, NULL, iMode);
}
/***************************************/
void RISK_SetDiceModeOfPlayer(Int32 iPlayer, Int32 iMode)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_DICEMODE, iPlayer, 0, NULL, iMode);
}
/***************************************/
void RISK_SetMsgDstModeOfPlayer(Int32 iPlayer, Int32 iMode)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MSGDSTMODE, iPlayer, 0, NULL, iMode);
}
/***************************************/
void RISK_SetStateOfPlayer(Int32 iPlayer, Int32 iState)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_STATE, iPlayer, 0, NULL, iState);
}
/***************************************/
void RISK_SetClientOfPlayer(Int32 iPlayer, Int32 iClient)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_CLIENT, iPlayer, 0, NULL, iClient);
}
/***************************************/
void RISK_SetNumCountriesOfPlayer(Int32 iPlayer, Int32 iNumCountries)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_NUMCOUNTRIES, iPlayer, 0, NULL, iNumCountries);
}
/***************************************/
void RISK_SetNumArmiesOfPlayer(Int32 iPlayer, Int32 iNumArmies)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_NUMARMIES, iPlayer, 0, NULL, iNumArmies);
}
/***************************************/
void RISK_SetNumCardsOfPlayer(Int32 iPlayer, Int32 iNumCards)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_NUMCARDS, iPlayer, 0, NULL, iNumCards);
}
/***************************************/
void RISK_SetNameOfPlayer(Int32 iPlayer, CString strName)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_NAME, iPlayer, 0, strName, 0);
}
/***************************************/
void RISK_SetColorCStringOfPlayer(Int32 iPlayer, CString strColor)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_COLORSTRING, iPlayer, 0, strColor, 0);
}
/***************************************/
void RISK_SetCardOfPlayer(Int32 iPlayer, Int32 iCard, Int32 iValue)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_CARD, iPlayer, iCard, NULL, iValue);
}
/***************************************/
void RISK_SetMissionTypeOfPlayer(Int32 iPlayer, Int16 typ)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MISSION , iPlayer, 0, NULL, typ);
}
/***************************************/
void RISK_SetMissionNumberOfPlayer(Int32 iPlayer, Int32 n)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MISSION1 , iPlayer, 0, NULL, n);
}
/***************************************/
void RISK_SetMissionContinent1OfPlayer(Int32 iPlayer, Int32 n)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MISSION1 , iPlayer, 0, NULL, n);
}
/***************************************/
void RISK_SetMissionContinent2OfPlayer(Int32 iPlayer, Int32 n)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MISSION2 , iPlayer, 0, NULL, n);
}
/***************************************/
void RISK_SetMissionMissionPlayerToKillOfPlayer(Int32 iPlayer, Int32 n)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MISSION1 , iPlayer, 0, NULL, n);
}
/***************************************/
void RISK_SetMissionPlayerIsKilledOfPlayer(Int32 iPlayer, Flag boool)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_MISSION2 , iPlayer, 0, NULL, boool);
}
/***************************************/
void RISK_SetSpeciesOfPlayer(Int32 iPlayer, Int32 iSpecies)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_SPECIES, iPlayer, 0, NULL, iSpecies);
}
/***************************************/
void RISK_SetAllocationStateOfPlayer(Int32 iPlayer, Int32 iState)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
_RISK_Replicate(PLR_ALLOCATION, iPlayer, 0, NULL, iState);
}
/***************************************/
void RISK_SetNumArmiesOfCountry(Int32 iCountry, Int32 iNumArmies)
{
D_Assert(iCountry>=0 && iCountry<NUM_COUNTRIES, "Bogus country!");
_RISK_Replicate(CNT_NUMARMIES, iCountry, 0, NULL, iNumArmies);
}
/***************************************/
void RISK_SetOwnerOfCountry(Int32 iCountry, Int32 iOwner)
{
D_Assert(iCountry>=0 && iCountry<NUM_COUNTRIES, "Bogus country!");
_RISK_Replicate(CNT_OWNER, iCountry, 0, NULL, iOwner);
}
/***************************************/
void RISK_SetClientOfSpecies(Int32 iHandle, Int32 iClient)
{
_RISK_Replicate(SPE_CLIENT, iHandle, 0, NULL, iClient);
}
/***************************************/
void RISK_SetNameOfSpecies(Int32 iHandle, CString strName)
{
_RISK_Replicate(SPE_NAME, iHandle, 0, strName, 0);
}
/***************************************/
void RISK_SetAllocationStateOfSpecies(Int32 iHandle, Int32 iState)
{
_RISK_Replicate(SPE_ALLOCATION, iHandle, 0, NULL, iState);
}
/***************************************/
void RISK_SetAuthorOfSpecies(Int32 iHandle, CString strAuthor)
{
_RISK_Replicate(SPE_AUTHOR, iHandle, 0, strAuthor, 0);
}
/***************************************/
void RISK_SetVersionOfSpecies(Int32 iHandle, CString strVersion)
{
_RISK_Replicate(SPE_VERSION, iHandle, 0, strVersion, 0);
}
/***************************************/
void RISK_SetDescriptionOfSpecies(Int32 iHandle, CString strDescription)
{
_RISK_Replicate(SPE_DESCRIPTION, iHandle, 0, strDescription, 0);
}
/**********************************************************************
* PURPOSE: determine number of initialized players
* NOTES:
*********************************************************************/
Int32 RISK_GetNumPlayers(void)
{
Int32 i, iCount;
for (i=iCount=0; i < MAX_PLAYERS; i++)
if (RISK_GetAllocationStateOfPlayer(i) == ALLOC_COMPLETE)
iCount++;
return iCount;
}
/***************************************/
Int32 RISK_GetAttackModeOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iAttackMode;
}
/***************************************/
Int32 RISK_GetMsgDstModeOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iMsgDstMode;
}
/***************************************/
Int32 RISK_GetDiceModeOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iDiceMode;
}
/***************************************/
Int32 RISK_GetStateOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iState;
}
/***************************************/
Int32 RISK_GetClientOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iClient;
}
/***************************************/
Int32 RISK_GetAllocationStateOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iAllocationState;
}
/***************************************/
Int32 RISK_GetNumCountriesOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iCountriesOwned;
}
/***************************************/
Int32 RISK_GetNumArmiesOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iNumArmies;
}
/***************************************/
Int32 RISK_GetNumCardsOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iNumCards;
}
/***************************************/
Int32 RISK_GetSpeciesOfPlayer(Int32 iPlayer)
{
D_Assert(iPlayer>=0 && iPlayer<MAX_PLAYERS, "Bogus player!");
return RiskGame.pPlayers[iPlayer].iSpecies;
}
/***************************************/
Int32 RISK_GetValueOfContinent(Int32 iContinent)
{
D_Assert(iContinent>=0 && iContinent<NUM_CONTINENTS, "Bogus continent!");
return RiskGame.pContinents[iContinent].iValue;
}
/***************************************/
CString RISK_GetNameOfContinent(Int32 iContinent)
{
D_Assert(iContinent>=0 && iContinent<NUM_CONTINENTS, "Bogus continent!");