1 /*
2  *   XFrisk - The classic board game for X
3  *   Copyright (C) 1993-1999 Elan Feingold (elan@aetherworks.com)
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *   $Id: riskgame.h,v 1.7 1999/12/26 19:09:04 morphy Exp $
20  *
21  *   $Log: riskgame.h,v $
22  *   Revision 1.7  1999/12/26 19:09:04  morphy
23  *   Doxygen comments
24  *
25  */
26 
27 /** \file
28  * Interface definitions for the RISK game distributed object.
29  *
30  * \bug Contains a LOT of hardcoded information
31  */
32 
33 #ifndef _RISK
34 #define _RISK
35 
36 #include <stdio.h>
37 
38 #include "network.h"
39 #include "types.h"
40 
41 /* Fields of the RiskGame object.  There MUST NOT be any gaps in the
42  * numeric sequence (because of how the replication works.
43  */
44 
45 #define PLR_ATTACKMODE       0   /* Remembers the attack mode of the player */
46 #define PLR_MSGDSTMODE       1   /* Remembers the last message destination */
47 #define PLR_DICEMODE         2   /* Remembers the last die mode */
48 
49 #define PLR_STATE            3   /* Either TRUE = Alive, FALSE = Dead */
50 #define PLR_CLIENT           4   /* The client of the player */
51 #define PLR_NUMCOUNTRIES     5   /* Number of territories the player owns */
52 #define PLR_NUMARMIES        6   /* Number of armies the player holds */
53 #define PLR_NUMCARDS         7   /* Number of cards the player holds */
54 #define PLR_NAME             8   /* Name of the player */
55 #define PLR_COLORSTRING      9   /* Name of the color of the player */
56 #define PLR_CARD            10   /* A card of the player */
57 #define PLR_SPECIES         11   /* What is the player */
58 #define PLR_ALLOCATION      12   /* State of allocation of player */
59 #define PLR_MISSION         13   /* Mission's type of player */
60 #define PLR_MISSION1        14   /* Mission's number1 of player */
61 #define PLR_MISSION2        15   /* Mission's number2 of player */
62 
63 #define CNT_NAME            16
64 #define CNT_CONTINENT       17
65 #define CNT_NUMARMIES       18
66 #define CNT_OWNER           19
67 #define CNT_ADJCOUNTRY      20
68 #define CNT_TEXTX           21
69 #define CNT_TEXTY           22
70 
71 #define CON_NAME            23
72 #define CON_VALUE           24
73 #define CON_NUMCOUNTRIES    25
74 
75 #define SPE_NAME            26
76 #define SPE_VERSION         27
77 #define SPE_DESCRIPTION     28
78 #define SPE_AUTHOR          29
79 #define SPE_CLIENT          30
80 #define SPE_ALLOCATION      31
81 
82 /* Port at which the clients can find the server */
83 #define RISK_PORT 5324
84 
85 /* Amount of time (in ms) for graphical notification */
86 #define NOTIFY_TIME 750
87 
88 /* Limits */
89 #define MAX_CLIENTS     32
90 #define MAX_CARDS       10
91 #define MAX_PLAYERS     12
92 #define MAX_COLORS     256
93 
94 /* This may be a static value (i.e. OPEN_MAX), or a dynamic value,
95  * as in OSF/1, accessed through sysconf().  I'm not sure that all
96  * systems have these, so we'll just guess.  I don't think this is
97  * exceptionally evil, since if we run out of descriptors, the socket
98  * or accept calls will fail.
99  */
100 
101 
102 #define MAX_DESCRIPTORS 128
103 
104 /* Numbers */
105 #define NUM_CONTINENTS     6
106 #define NUM_COUNTRIES     42
107 #define NUM_OTHERCOLORS    4 /* Dice*2, Player Turn, ColorEdit */
108 #define NUM_CARDS         (NUM_COUNTRIES+2)
109 #define NUM_FORTIFY_ARMIES 1
110 
111 /* Continents */
112 #define CNT_NORTHAMERICA  0
113 #define CNT_SOUTHAMERICA  1
114 #define CNT_AFRICA        2
115 #define CNT_AUSTRALIA     3
116 #define CNT_ASIA          4
117 #define CNT_EUROPE        5
118 
119 /* Missions */
120 #define NO_MISSION              0
121 #define CONQUIER_WORLD          1
122 #define CONQUIER_Nb_COUNTRY     2
123 #define CONQUIER_TWO_CONTINENTS 3
124 #define KILL_A_PLAYER           4
125 
126 /* Macros */
127 #ifdef MAX
128 #undef MAX
129 #endif
130 #define MAX(a, b) ((a)>(b)?(a):(b))
131 
132 #ifdef MIN
133 #undef MIN
134 #endif
135 #define MIN(a, b) ((a)>(b)?(b):(a))
136 
137 #ifndef TRUE
138 #define TRUE   1
139 #endif
140 #ifndef FALSE
141 #define FALSE  0
142 #endif
143 
144 /* States in risk state machine */
145 #define STATE_REGISTER  0
146 #define STATE_FORTIFY   1
147 #define STATE_PLACE     2
148 #define STATE_ATTACK    3
149 #define STATE_MOVE      4
150 
151 /* Defines for widgets */
152 #define ATTACK_ONE      0
153 #define ATTACK_TWO      1
154 #define ATTACK_THREE    2
155 #define ATTACK_AUTO     3
156 
157 #define ACTION_PLACE    0
158 #define ACTION_ATTACK   1
159 #define ACTION_DOORDIE  2
160 #define ACTION_MOVE     3
161 
162 /* The only known species at the beginning */
163 #define SPECIES_HUMAN    0
164 
165 /* A PERSISTANT object will maintain a copy of itself on secondary storage.
166  * Every time the object updates a field, it also updates the field on disk.
167  */
168 
169 #define MODE_PERSISTANT 8
170 
171 /* States in allocation for players and species */
172 #define ALLOC_NONE       0
173 #define ALLOC_INPROGRESS 1
174 #define ALLOC_COMPLETE   2
175 
176 /* States for players */
177 #define PLAYER_DEAD      0
178 #define PLAYER_ALIVE     1
179 
180 /* These functions support all of the functionality of the RiskGame structure,
181  * in order for it to work as a fully distributed object.  The functions
182  * are split into two catagories, those that receive input from a remote
183  * source (through a message), those that receive input from the local
184  * client, which is sent back to the server to broadcast to all the other
185  * clients, and those that return fields of the object to the local client.
186  * This file serves the functionality of a C++ object -- I would use GCC but
187  * its C++ side is not stable enough.  Hold your breath, though :)
188  */
189 
190 /* Constructor */
191 
192 void    RISK_InitObject(void (*ReplicateCallback)(Int32, void *, Int32, Int32),
193 			void (*PreViewCallback)(Int32, void *),
194 			void (*PostViewCallback)(Int32, void *),
195 			void (*FailureCallback)(CString, Int32),
196 			FILE *hDebug);
197 
198 #define MESS_INCOMING 0
199 #define MESS_OUTGOING 1
200 
201 /* For persistance */
202 
203 void     RISK_SaveObject(CString strFileName);
204 void     RISK_LoadObject(CString strFileName);
205 
206 
207 /* Utilities */
208 
209 Int32    RISK_GetNthLivePlayer(Int32 iIndex);
210 Int32    RISK_GetNthPlayer(Int32 iIndex);
211 Int32    RISK_GetNthPlayerAtClient(Int32 iClient, Int32 iIndex);
212 Int32    RISK_GetNumPlayersOfClient(Int32 iClient);
213 Int32    RISK_GetNumLivePlayersOfClient(Int32 iClient);
214 void     RISK_ObjectFailure(CString strReason, Int32 iCommLink);
215 
216 /* For the failure routine */
217 #define SERVER -1
218 
219 
220 /* Reliable communication */
221 
222 Int32    RISK_SendMessage(Int32 iDest, Int32 iMessType, void *pvMessage);
223 Int32    RISK_ReceiveMessage(Int32 iSource, Int32 *piMessType,
224 			     void **ppvMessage);
225 Int32    RISK_SendSyncMessage(Int32 iCommLink, Int32 iMessType,
226 			      void *pvMessage, Int32 iReturnMessType,
227 			      void (*CBK_MessageReceived)(Int32, void *));
228 
229 /* Methods for the distributed object. */
230 
231 void     RISK_SelectiveReplicate(Int32 iClientToServerSocket, Int32 iField,
232 				 Int32 iIndex1, Int32 iIndex2);
233 void     RISK_ResetObj(void);
234 void     RISK_ResetGame(void);
235 void     RISK_ProcessMessage(Int32 iMessType, void *pMessage);
236 
237 void     RISK_SetSpeciesOfPlayer(Int32 iPlayer, Int32 iSpecies);
238 void     RISK_SetAttackModeOfPlayer(Int32 iPlayer, Int32 iMode);
239 void     RISK_SetDiceModeOfPlayer(Int32 iPlayer, Int32 iMode);
240 void     RISK_SetMsgDstModeOfPlayer(Int32 iPlayer, Int32 iMode);
241 void     RISK_SetStateOfPlayer(Int32 iPlayer, Int32 iState);
242 void     RISK_SetClientOfPlayer(Int32 iPlayer, Int32 iClient);
243 void     RISK_SetAllocationStateOfPlayer(Int32 iPlayer, Int32 iState);
244 void     RISK_SetNumCountriesOfPlayer(Int32 iCountry, Int32 iNumCountries);
245 void     RISK_SetNumArmiesOfPlayer(Int32 iCountry, Int32 iNumArmies);
246 void     RISK_SetNumCardsOfPlayer(Int32 iPlayer, Int32 iNumCards);
247 void     RISK_SetNameOfPlayer(Int32 iPlayer, CString strName);
248 void     RISK_SetColorCStringOfPlayer(Int32 iPlayer, CString strColor);
249 void     RISK_SetCardOfPlayer(Int32 iPlayer, Int32 iCard, Int32 iValue);
250 void     RISK_SetMissionTypeOfPlayer(Int32 iPlayer, Int16 typ);
251 void     RISK_SetMissionNumberOfPlayer(Int32 iPlayer, Int32 n);
252 void     RISK_SetMissionContinent1OfPlayer(Int32 iPlayer, Int32 n);
253 void     RISK_SetMissionContinent2OfPlayer(Int32 iPlayer, Int32 n);
254 void     RISK_SetMissionMissionPlayerToKillOfPlayer(Int32 iPlayer, Int32 n);
255 void     RISK_SetMissionPlayerIsKilledOfPlayer(Int32 iPlayer, Flag boool);
256 void     RISK_SetNameOfCountry(Int32 iCountry, CString strName);
257 void     RISK_SetContinentOfCountry(Int32 iCountry, Int32 iContinent);
258 void     RISK_SetOwnerOfCountry(Int32 iCountry, Int32 iOwner);
259 void     RISK_SetNumArmiesOfCountry(Int32 iCountry, Int32 iNumArmies);
260 void     RISK_SetAdjCountryOfCountry(Int32 iCountry, Int32 iIndex,
261 				     Int32 OtherC);
262 
263 Int32    RISK_GetSpeciesOfPlayer(Int32 iPlayer);
264 Int32    RISK_GetDiceModeOfPlayer(Int32 iPlayer);
265 Int32    RISK_GetMsgDstModeOfPlayer(Int32 iPlayer);
266 Int32    RISK_GetAttackModeOfPlayer(Int32 iPlayer);
267 Int32    RISK_GetStateOfPlayer(Int32 iPlayer);
268 Int32    RISK_GetClientOfPlayer(Int32 iPlayer);
269 Int32    RISK_GetAllocationStateOfPlayer(Int32 iPlayer);
270 Int32    RISK_GetNumCountriesOfPlayer(Int32 iPlayer);
271 Int32    RISK_GetNumArmiesOfPlayer(Int32 iPlayer);
272 Int32    RISK_GetNumCardsOfPlayer(Int32 iPlayer);
273 CString  RISK_GetNameOfPlayer(Int32 iPlayer);
274 CString  RISK_GetColorCStringOfPlayer(Int32 iPlayer);
275 Int32    RISK_GetCardOfPlayer(Int32 iPlayer, Int32 iCard);
276 Int16    RISK_GetMissionTypeOfPlayer(Int32 iPlayer);
277 Int32    RISK_GetMissionNumberOfPlayer(Int32 iPlayer);
278 Int32    RISK_GetMissionContinent1OfPlayer(Int32 iPlayer);
279 Int32    RISK_GetMissionContinent2OfPlayer(Int32 iPlayer);
280 Int32    RISK_GetMissionPlayerToKillOfPlayer(Int32 iPlayer);
281 Flag     RISK_GetMissionIsPlayerKilledOfPlayer(Int32 iPlayer);
282 CString  RISK_GetNameOfCountry(Int32 iCountry);
283 Int32    RISK_GetContinentOfCountry(Int32 iCountry);
284 Int32    RISK_GetNumArmiesOfCountry(Int32 iCountry);
285 Int32    RISK_GetOwnerOfCountry(Int32 iCountry);
286 Int32    RISK_GetAdjCountryOfCountry(Int32 iCountry, Int32 iIndex);
287 Int32    RISK_GetTextXOfCountry(Int32 iCountry);
288 Int32    RISK_GetTextYOfCountry(Int32 iCountry);
289 Int32    RISK_GetValueOfContinent(Int32 iContinent);
290 CString  RISK_GetNameOfContinent(Int32 iContinent);
291 Int32    RISK_GetNumCountriesOfContinent(Int32 iContinent);
292 Int32    RISK_GetNumLivePlayers(void);
293 Int32    RISK_GetNumPlayers(void);
294 
295 /* The first (should-be-a) composite object in Frisk */
296 CString  RISK_GetNameOfSpecies(Int32 iHandle);
297 Int32    RISK_GetClientOfSpecies(Int32 iHandle);
298 CString  RISK_GetAuthorOfSpecies(Int32 iHandle);
299 CString  RISK_GetDescriptionOfSpecies(Int32 iHandle);
300 CString  RISK_GetVersionOfSpecies(Int32 iHandle);
301 Int32    RISK_GetAllocationStateOfSpecies(Int32 iHandle);
302 Int32    RISK_GetNumSpecies(void);
303 
304 void     RISK_SetNameOfSpecies(Int32 iHandle, CString strName);
305 void     RISK_SetClientOfSpecies(Int32 iHandle, Int32 iClient);
306 void     RISK_SetAuthorOfSpecies(Int32 iHandle, CString strAuthor);
307 void     RISK_SetVersionOfSpecies(Int32 iHandle, CString strVersion);
308 void     RISK_SetDescriptionOfSpecies(Int32 iHandle, CString strDescription);
309 void     RISK_SetAllocationStateOfSpecies(Int32 iHandle, Int32 iState);
310 void     RISK_SetNumSpecies(Int32 iNumSpecies);
311 
312 #endif
313