File: aiController.c

package info (click to toggle)
xfrisk 1.2-10
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,344 kB
  • sloc: ansic: 16,942; makefile: 272; sh: 29
file content (454 lines) | stat: -rw-r--r-- 12,542 bytes parent folder | download | duplicates (7)
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
/*
 *   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: aiController.c,v 1.4 1999/11/13 21:58:30 morphy Exp $
 */

#include "aiController.h"
#include "aiClient.h"
#include "client.h"
#include "riskgame.h"
#include "game.h"
#include "debug.h"

/* Prototypes */
Int32  CLNT_GetCommLinkOfClient(Int32 iThisClient);

/* Externs */
extern void  *(*__AI_Callback)(void *, Int32, void *);
extern void  *pvContext[MAX_PLAYERS];
extern Int32  iState, iCurrentPlayer;
extern Flag   fGameStarted;

/* Globals */
Flag          fPlayerFortified;


/************************************************************************ 
 *  FUNCTION: AI_Init
 *  HISTORY: 
 *     04.23.95  ESF  Created.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
void AI_Init(void)
{
  fPlayerFortified=FALSE;
  fGameStarted = FALSE;
  fGetsCard=FALSE;
}


/************************************************************************ 
 *  FUNCTION: AI_CheckCards
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *     28.08.95  JC   If the computerized must exchange cards then do.
                      (copy of ExchangeCards)
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
void AI_CheckCards(void)
{
  Int32 i, j, nb, typ, piCards[4], nbCards[4], piCardValues[MAX_CARDS];
  Flag  fOptimal;

  if (RISK_GetNumCardsOfPlayer (iCurrentPlayer) <= 4)
    return;
#ifdef ENGLISH
  printf("AI: Error -- Checking cards.\n");
#endif
#ifdef FRENCH
  printf("IA: Erreur -- Vrification des cartes.\n");
#endif

  nb = RISK_GetNumCardsOfPlayer(iCurrentPlayer);
  do
    {
      piCards[0]=piCards[1]=piCards[2]=piCards[3]=-1;
      nbCards[0]=nbCards[1]=nbCards[2]=nbCards[3]=0;
      fOptimal = FALSE;
      for (i=0; i<nb; i++)
        {
          piCardValues[i] = RISK_GetCardOfPlayer(iCurrentPlayer, i);
          /* Set the type of the card */
          if (piCardValues[i]<NUM_COUNTRIES)
            {
              typ = piCardValues[i] % 3;
              nbCards[typ]++;
              if (RISK_GetOwnerOfCountry(piCardValues[i]) == iCurrentPlayer)
                  piCards[typ] = i;
              else if (piCards[typ] == -1)
                  piCards[typ] = i;
            }
          else  /* Joker */
            {
	      piCards[3] = i;
	      nbCards[3]++;
	    }
        }
      if ((nbCards[0]>0)&&(nbCards[1]>0)&&(nbCards[2]>0))
        {
          AI_ExchangeCards(piCards);
          fOptimal = TRUE;
        }
      else if (nb >= 5)
        {
          if ((nbCards[0]>0)&&(nbCards[1]>0)&&(nbCards[3]>0))
              piCards[2] = piCards[3];
          else if ((nbCards[0]>0)&&(nbCards[3]>0)&&(nbCards[2]>0))
              piCards[1] = piCards[3];
          else if ((nbCards[3]>0)&&(nbCards[1]>0)&&(nbCards[2]>0))
              piCards[0] = piCards[3];
          else if (nbCards[0]>=3)
            {
              j = 0;
              for (i=0; i<nb; i++)
                  if ((piCardValues[i] % 3) == 0)
                      piCards[j++]=i;
            }
          else if (nbCards[1]>=3)
            {
              j = 0;
              for (i=0; i<nb; i++)
                  if ((piCardValues[i] % 3) == 1)
                      piCards[j++]=i;
            }
          else if (nbCards[2]>=3)
            {
              j = 0;
              for (i=0; i<nb; i++)
                  if ((piCardValues[i] % 3) == 2)
                      piCards[j++]=i;
            }
          else
            {
              piCards[0] = 0;
              piCards[1] = 1;
              piCards[2] = 2;
            }
          AI_ExchangeCards(piCards);
        }
      nb = RISK_GetNumCardsOfPlayer(iCurrentPlayer);
    }
  while ((fOptimal && (nb >= 3)) || (nb >= 5));
}


/************************************************************************ 
 *  FUNCTION: AI_CheckFortification
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
void AI_CheckFortification(void)
{
  /* If the player hasn't fortified yet, do it */
  if (!fPlayerFortified)
    {
      Int32 i;

#ifdef ENGLISH
      printf("AI: Error -- Checking fortification.\n");  
#endif
#ifdef FRENCH
      printf("IA: Erreur -- Vrification de la fortification.\n");  
#endif

      /* Place an army in the first country we find */
      for (i=0; i!=NUM_COUNTRIES; i++)
	if (RISK_GetOwnerOfCountry(i) == iCurrentPlayer)
	  {
	    AI_Place(i, 1);
	    return;
	  }

      D_Assert(FALSE, "Player didn't own any countries??");
    }
}


/************************************************************************ 
 *  FUNCTION: AI_CheckPlacement
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
void AI_CheckPlacement(void)
{
  if (RISK_GetNumArmiesOfPlayer(iCurrentPlayer) != 0)
    {
      Int32 i;

#ifdef ENGLISH
      printf("AI: Error -- Checking placement.\n");  
#endif
#ifdef FRENCH
      printf("IA: Erreur -- Vrification du placement.\n");  
#endif
      /* Dump the armies on the first country */
      for (i=0; i!=NUM_COUNTRIES; i++)
	if (RISK_GetOwnerOfCountry(i) == iCurrentPlayer)
	  {
	    AI_Place(i, RISK_GetNumArmiesOfPlayer(iCurrentPlayer));
	    return;
	  }

      D_Assert(FALSE, "Player didn't own any countries??");
    }
}


/************************************************************************ 
 *  FUNCTION: AI_Place
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *     21.08.95  JC   AI_CheckCards if game is started.
 *     28.08.95  JC   No AI_CheckCards.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
Flag AI_Place(Int32 iCountry, Int32 iNumArmies)
{
  if (GAME_ValidPlaceDst(iCountry) && 
      iNumArmies <= RISK_GetNumArmiesOfPlayer(iCurrentPlayer) &&
      ((iState == STATE_FORTIFY && iNumArmies == 1) || 
       iState != STATE_FORTIFY))
    {
      GAME_PlaceArmies(iCountry, iNumArmies);
      
      /* Player has fortified if in fortification stage */
      fPlayerFortified = TRUE;

      return TRUE;
    }
  else
    {
#ifdef ENGLISH
      printf("AI: Error -- illegal place (%d, %d)\n", iCountry, iNumArmies);
#endif
#ifdef FRENCH
      printf("IA: Erreur -- placement illgal (%d, %d)\n", iCountry, iNumArmies);
#endif
      return FALSE;
    }
}


/************************************************************************/

Int32 iMoveMode;


/************************************************************************ 
 *  FUNCTION: AI_Attack
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *     23.08.95  JC   Corrected a bug if iSrcCountry or iDstCountry are 
 *                    totally invalid.
 *                    If ARMIES_MOVE_MANUAL, conserve NumArmiesOfPlayer.
 *     30.08.95  JC   Do the move immediatly. 
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
Flag AI_Attack(Int32 iSrcCountry, Int32 iDstCountry, Int32 iAttack, 
	       Int32 iDice, Int32 iMoveSelectMode)
{
  Int32 iAttackMode, iDiceMode;
  char  *srcName, *dstName;

  iMoveMode = iMoveSelectMode;
  /* Set the modes for the attack */
  switch (iDice)
    {
    case DICE_ONE:
      iDiceMode   = ATTACK_ONE;
      break;
    case DICE_TWO:
      iDiceMode   = ATTACK_TWO;
      break;
    case DICE_THREE:
      iDiceMode   = ATTACK_THREE;
      break;
    case DICE_MAXIMUM:
      iDiceMode   = ATTACK_AUTO;
      break;
    default:
#ifdef ENGLISH
      printf("AI: Error -- choosing dice mode for attack.\n");
#endif
#ifdef FRENCH
      printf("IA: Erreur -- choix du mode des ds pour combattre.\n");
#endif
      return FALSE;
    }

  switch (iAttack)
    {
    case ATTACK_ONCE:
      iAttackMode = ACTION_ATTACK;
      break;
    case ATTACK_DOORDIE:
      iAttackMode = ACTION_DOORDIE;
      break;
    default:
#ifdef ENGLISH
      printf("AI: Error -- choosing attack mode for attack.\n");
#endif
#ifdef FRENCH
      printf("IA: Erreur -- choix du mode d'attaque pour combattre.\n");
#endif
      return FALSE;
    }

  RISK_SetAttackModeOfPlayer(iCurrentPlayer, iAttackMode);
  RISK_SetDiceModeOfPlayer(iCurrentPlayer, iDiceMode);

  /* BUG -- return true if victory? */

  if (GAME_ValidAttackSrc(iSrcCountry, TRUE) &&
      GAME_ValidAttackDst(iSrcCountry, iDstCountry, TRUE) &&
      GAME_ValidAttackDice(iDiceMode, iSrcCountry))
    {
      GAME_Attack(iSrcCountry, iDstCountry);
      return TRUE;
    }
  else
    {
      if ((iSrcCountry>=0) && (iSrcCountry<NUM_COUNTRIES))
          srcName = RISK_GetNameOfCountry(iSrcCountry);
      else
          srcName = "";
      if ((iDstCountry>=0) && (iDstCountry<NUM_COUNTRIES))
          dstName = RISK_GetNameOfCountry(iDstCountry);
      else
          dstName = "";
#ifdef ENGLISH
      printf("AI: Error -- illegal attack (%s --> %s)\n", 
#endif
#ifdef FRENCH
      printf("IA: Erreur -- illgall attaque (%s --> %s)\n", 
#endif
	     srcName, dstName);
      return FALSE;
    }
}


/************************************************************************ 
 *  FUNCTION: AI_Move
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *     23.08.95  JC   Don't check Cards and placement
 *                    if state <> STATE_MOVE.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
Flag AI_Move(Int32 iSrcCountry, Int32 iDstCountry, Int32 iNumArmies)
{
  if (GAME_ValidMoveSrc(iSrcCountry) &&
      GAME_ValidMoveDst(iSrcCountry, iDstCountry) &&
      RISK_GetNumArmiesOfCountry(iSrcCountry) >= iNumArmies+1)
    {
      GAME_MoveArmies(iSrcCountry, iDstCountry, iNumArmies);
      return TRUE;
    }
  else
    {
#ifdef ENGLISH
      printf("AI: Error -- illegal move (%s --> %s)\n", 
#endif
#ifdef FRENCH
      printf("IA: Erreur -- mouvement illgal (%s --> %s)\n", 
#endif
	     RISK_GetNameOfCountry(iSrcCountry), 
	     RISK_GetNameOfCountry(iDstCountry));
      return FALSE;
    }
}


/************************************************************************ 
 *  FUNCTION: AI_ExchangeCards
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
Flag AI_ExchangeCards(Int32 *piCards)
{
  GAME_ExchangeCards(piCards);
  return TRUE;
}


/************************************************************************ 
 *  FUNCTION: AI_SendMessage
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
Flag AI_SendMessage(Int32 iMessDest, CString strMessage)
{
  MsgMessagePacket mess;

  mess.strMessage = strMessage;
  mess.iFrom      = iCurrentPlayer;
  mess.iTo        = iMessDest;
  (void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
			 MSG_MESSAGEPACKET, &mess);
  return TRUE;
}


/************************************************************************ 
 *  FUNCTION: AI_EndTurn
 *  HISTORY: 
 *     03.31.95  ESF  Created.
 *     21.08.95  JC   Added fGetsCard.
 *     23.08.95  JC   Added test the mission.
 *  PURPOSE: 
 *  NOTES: 
 ************************************************************************/
Flag AI_EndTurn(void)
{
  /* Get the player a card if he or she needs one */
  if (fGetsCard)
    {
      MsgRequestCard msg;

      fGetsCard = FALSE;
      msg.iPlayer = iCurrentPlayer;
      (void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()), 
			     MSG_REQUESTCARD, &msg);
    }

  if (fGameStarted)
      fCanExchange = TRUE;
  else
      fPlayerFortified = FALSE;

  /* Actually end the turn */
  (void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
			 MSG_ENDTURN, NULL);

  return TRUE;
}