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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
|
/*
* 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: 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 dplace %d arme(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 conquire %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'ocan!");
#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 arme.");
#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'ocan!");
#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'ocan, 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 armes 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 dplace des armes 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 dplace des armes 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 dplacer des armes partir de l'ocan.");
#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 armes 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 dplacer la seule arme d'un territoire.");
#endif
else
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_ValidMoveDst
* HISTORY:
* 03.07.94 ESF Created.
* PURPOSE:
* NOTES: used to get iMoveDst passed, which is a global static in game.c
************************************************************************/
Flag GAME_ValidMoveDst(Int32 iMoveSrc, Int32 iMoveDst)
{
char buf[256];
if (iMoveDst < 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 (iMoveDst >= NUM_COUNTRIES)
#ifdef ENGLISH
UTIL_DisplayError("You can't move armies into the ocean -- they'd drown.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Impossible de placer des armes dans l'ocan.");
#endif
else if (RISK_GetOwnerOfCountry(iMoveDst) != iCurrentPlayer)
#ifdef ENGLISH
UTIL_DisplayError("You cannot move armies to an opponent's"
" territories.");
#endif
#ifdef FRENCH
UTIL_DisplayError("Impossible de placer des armes sur un territoire ennemi.");
#endif
else if (!GAME_CanAttack(iMoveSrc, iMoveDst))
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "Armies can't get to %s from %s",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "Dplacement impossible de %s en %s",
#endif
RISK_GetNameOfCountry(iMoveDst),
RISK_GetNameOfCountry(iMoveSrc));
UTIL_DisplayError(buf);
}
else
return TRUE;
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_GetContinentBonus
* HISTORY:
* 03.16.94 ESF Created.
* 01.09.95 ESF Changed an if..else to an assertion.
* PURPOSE:
* NOTES:
************************************************************************/
Int32 GAME_GetContinentBonus(Int32 iPlayer)
{
Int32 piCount[NUM_CONTINENTS];
Int32 i, iArmies=0;
/* Init. */
for (i=0; i!=NUM_CONTINENTS; i++)
piCount[i] = 0;
/* Count up how many countries the player has in each of the continents */
for (i=0; i!=NUM_COUNTRIES; i++)
if (RISK_GetOwnerOfCountry(i) == iPlayer)
{
/* Sanity check */
D_Assert(RISK_GetContinentOfCountry(i) >= 0 &&
RISK_GetContinentOfCountry(i) < NUM_CONTINENTS,
"Country has messed up number of armies.");
piCount[RISK_GetContinentOfCountry(i)]++;
}
/* If the player has the total number of countries, give him or her bonus */
for (i=0; i!=NUM_CONTINENTS; i++)
if (RISK_GetNumCountriesOfContinent(i) == piCount[i])
iArmies += RISK_GetValueOfContinent(i);
return(iArmies);
}
/************************************************************************
* FUNCTION: GAME_PlayerDied
* HISTORY:
* 03.28.94 ESF Created.
* 03.29.94 ESF Fixed off-by-one bug in end of game detection.
* 04.11.94 ESF Added iKillerPlayer parameter.
* 05.04.94 ESF Fixed bugs and exhanced.
* 05.12.94 ESF Fixed bug, reset dead player's cards to 0.
* 09.31.94 ESF Changed because of new ENDOFGAME contents.
* 10.02.94 ESF Changed to set number of live players == 0.
* 10.30.94 ESF Fixed bug, shouldn't be setting iNumLivePlayers to 0.
* 01.09.95 ESF Changed to be more verbose.
* 30.08.95 JC Talk the server: "force exchange of cards?".
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_PlayerDied(Int32 iDeadPlayer)
{
MsgMessagePacket msg;
char buf[256];
/* Notify everybody */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s was destroyed by %s (who gained %d card%s).",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s est dtruit par %s (qui gagne %d carte%s).",
#endif
RISK_GetNameOfPlayer(iDeadPlayer),
RISK_GetNameOfPlayer(iCurrentPlayer),
RISK_GetNumCardsOfPlayer(iDeadPlayer),
(RISK_GetNumCardsOfPlayer(iDeadPlayer)>1)?"s":"");
msg.strMessage = buf;
msg.iFrom = FROM_SERVER;
msg.iTo = DST_ALLPLAYERS;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_MESSAGEPACKET, &msg);
/* Kill the player */
RISK_SetStateOfPlayer(iDeadPlayer, PLAYER_DEAD);
/* Is there only one player left? If so, end of game... */
if (RISK_GetNumLivePlayers() == 1)
{
MsgVictory msgVictory;
msgVictory.iWinner = iCurrentPlayer;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_VICTORY, &msgVictory);
}
else
{
MsgForceExchangeCards msg;
Int32 i, n, m;
/* Give the killer player all of the cards */
n = RISK_GetNumCardsOfPlayer(iCurrentPlayer);
m = RISK_GetNumCardsOfPlayer(iDeadPlayer);
/* Let the player know how many cards he or she got. */
if (m>0)
{
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s got %d card%s from %s.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s prend %d carte%s de %s.",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
m, m==1?"":"s", RISK_GetNameOfPlayer(iDeadPlayer));
mess.strMessage = buf;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_NETMESSAGE, &mess);
}
for (i=0; i!=m; i++)
RISK_SetCardOfPlayer(iCurrentPlayer, n+i,
RISK_GetCardOfPlayer(iDeadPlayer, i));
RISK_SetNumCardsOfPlayer(iCurrentPlayer, n+m);
RISK_SetNumCardsOfPlayer(iDeadPlayer, 0);
if (m>0)
{
/* Force the player to exchange if he or she possesses
* too many cards.
*/
msg.iPlayer = iCurrentPlayer;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_FORCEEXCHANGECARDS, &msg);
}
}
}
/************************************************************************
* FUNCTION: GAME_MissionToStr
* HISTORY:
* 29.08.95 JC Created (copy of GAME_ShowMission).
* PURPOSE: Put in strScratch a description of the mission
* NOTES:
************************************************************************/
void GAME_MissionToStr(Int32 iPlayer, Int16 iTyp, Int32 iNum1, Int32 iNum2, Flag fVict, char *buf, size_t bufsize)
{
UNUSED(iPlayer);
#ifdef FRENCH
switch (iTyp)
{
case NO_MISSION:
snprintf(buf, bufsize, "Plus de mission -> Conquerir le monde");
break;
case CONQUIER_WORLD:
if (fVict)
snprintf(buf, bufsize, "Mission accomplie: le monde est conquit");
else
snprintf(buf, bufsize, "Mission: Conquerir le monde");
break;
case CONQUIER_Nb_COUNTRY:
if (fVict)
snprintf(buf, bufsize, "Mission accomplie: %d territoires conquis",
iNum1);
else
snprintf(buf, bufsize, "Mission: Conquerir %d territoires",
iNum1);
break;
case CONQUIER_TWO_CONTINENTS:
if (fVict)
snprintf(buf, bufsize, "Mission accomplie: Conquerir %s et %s",
RISK_GetNameOfContinent(iNum1),
RISK_GetNameOfContinent(iNum2));
else
snprintf(buf, bufsize, "Mission: Conquerir %s et %s",
RISK_GetNameOfContinent(iNum1),
RISK_GetNameOfContinent(iNum2));
break;
case KILL_A_PLAYER:
if (fVict || iNum2)
snprintf(buf, bufsize, "Mission accomplie: %s est mort",
RISK_GetNameOfPlayer(iNum1));
else if (RISK_GetNumCountriesOfPlayer(iNum1) <= 0)
snprintf(buf, bufsize, "Mission impossible: Tuer %s qui est dj mort",
RISK_GetNameOfPlayer(iNum1));
else
snprintf(buf, bufsize, "Mission: Tuer %s",
RISK_GetNameOfPlayer(iNum1));
break;
}
#endif
#ifdef ENGLISH
switch (iTyp)
{
case NO_MISSION:
snprintf(buf, bufsize, "Additional mission -> Conquer the world");
break;
case CONQUIER_WORLD:
if (fVict)
snprintf(buf, bufsize, "Mission accomplished: the world is conquered");
else
snprintf(buf, bufsize, "Mission: Conquer the world");
break;
case CONQUIER_Nb_COUNTRY:
if (fVict)
snprintf(buf, bufsize, "Mission accomplished: %d countries conquered",
iNum1);
else
snprintf(buf, bufsize, "Mission: Conquer %d countries",
iNum1);
break;
case CONQUIER_TWO_CONTINENTS:
if (fVict)
snprintf(buf, bufsize, "Mission accomplished: Conquer %s and %s",
RISK_GetNameOfContinent(iNum1),
RISK_GetNameOfContinent(iNum2));
else
snprintf(buf, bufsize, "Mission: Conquer %s and %s",
RISK_GetNameOfContinent(iNum1),
RISK_GetNameOfContinent(iNum2));
break;
case KILL_A_PLAYER:
if (fVict || iNum2)
snprintf(buf, bufsize, "Mission accomplished: %s is dead",
RISK_GetNameOfPlayer(iNum1));
else if (RISK_GetNumCountriesOfPlayer(iNum1) <= 0)
snprintf(buf, bufsize, "Mission impossible: kill %s who's already dead",
RISK_GetNameOfPlayer(iNum1));
else
snprintf(buf, bufsize, "Mission: Kill %s",
RISK_GetNameOfPlayer(iNum1));
break;
}
#endif
}
/************************************************************************
* FUNCTION: GAME_MissionAccomplied
* HISTORY:
* 25.08.95 JC Created from different pieces.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_MissionAccomplied(Int32 iWinner, Int16 iTyp, Int32 iNum1, Int32 iNum2)
{
Char str[256];
Int32 iChoice;
char buf[256];
GAME_MissionToStr(iWinner, iTyp, iNum1, iNum2, TRUE, buf, sizeof(buf));
if (RISK_GetClientOfPlayer(iWinner) != CLNT_GetThisClientID())
{
/* Player that has won is non-local */
#ifdef ENGLISH
snprintf(str, sizeof(str),
"%s has accomplied his mission!\n%s\n",
RISK_GetNameOfPlayer(iCurrentPlayer),
buf);
(void)UTIL_PopupDialog("End of Mission", str, 2, "Cool!",
"So What?", NULL);
#endif
#ifdef FRENCH
snprintf(str, sizeof(str),
"%s a termin sa mission!\n%s\n",
RISK_GetNameOfPlayer(iCurrentPlayer),
buf);
(void)UTIL_PopupDialog("Mission termine", str, 2, "Cool!",
"Et alors?", NULL);
#endif
return;
}
/* Winning player is local, congratulate him or her */
#ifdef ENGLISH
(void)UTIL_PopupDialog(RISK_GetNameOfPlayer(iCurrentPlayer),
"You WON!!!", 2, "Cool!",
"So What?", NULL);
#endif
#ifdef FRENCH
(void)UTIL_PopupDialog(RISK_GetNameOfPlayer(iCurrentPlayer),
"Victoire!!!", 2, "Cool!",
"Et alors?", NULL);
#endif
/* See if the player wants to continue or play again */
#ifdef ENGLISH
if (RISK_GetNumLivePlayers () > 1)
iChoice = UTIL_PopupDialog("Game over", "Play again?",
3, "Yes", "No", "Continue");
else
iChoice = UTIL_PopupDialog("Game over", "Play again?",
2, "Yes", "No", NULL);
#endif
#ifdef FRENCH
if (RISK_GetNumLivePlayers () > 1)
iChoice = UTIL_PopupDialog("Partie termine", "Jouer une autre partie?",
3, "Oui", "Non", "Continuer");
else
iChoice = UTIL_PopupDialog("Partie termine", "Jouer une autre partie?",
2, "Oui", "Non", NULL);
#endif
if (iChoice == QUERY_NO)
{
/* Leave the game */
UTIL_ExitProgram(0);
}
if (iChoice == QUERY_CANCEL)
return;
iState = STATE_REGISTER;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_ENDOFGAME, NULL);
}
/************************************************************************
* FUNCTION: GAME_Victory
* HISTORY:
* 28.08.95 JC Created from different pieces.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_Victory(Int32 iWinner)
{
Int32 iChoice;
char buf[256];
if (RISK_GetClientOfPlayer(iWinner) != CLNT_GetThisClientID())
{
/* Player that has won is non-local */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s has won!", RISK_GetNameOfPlayer(iCurrentPlayer));
(void)UTIL_PopupDialog("End of Game", buf, 2, "Cool!",
"So What?", NULL);
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s a gagn!", RISK_GetNameOfPlayer(iCurrentPlayer));
(void)UTIL_PopupDialog("Fin du jeu", buf, 2, "Cool!",
"Et alors?", NULL);
#endif
return;
}
/* Winning player is local, congratulate him or her */
#ifdef ENGLISH
(void)UTIL_PopupDialog(RISK_GetNameOfPlayer(iCurrentPlayer),
"You WON!!!", 2, "Cool!",
"So What?", NULL);
#endif
#ifdef FRENCH
(void)UTIL_PopupDialog(RISK_GetNameOfPlayer(iCurrentPlayer),
"Victoire!!!", 2, "Cool!",
"Et alors?", NULL);
#endif
/* See if the player wants to continue or play again */
#ifdef ENGLISH
iChoice = UTIL_PopupDialog("Game over", "Play again?",
2, "Yes", "No", NULL);
#endif
#ifdef FRENCH
iChoice = UTIL_PopupDialog("Partie termine", "Jouer une autre partie?",
2, "Oui", "Non", NULL);
#endif
if (iChoice == QUERY_NO)
{
/* Leave the game */
UTIL_ExitProgram(0);
}
iState = STATE_REGISTER;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_ENDOFGAME, NULL);
}
/************************************************************************
* FUNCTION: GAME_GameOverMan
* HISTORY:
* 03.28.94 ESF Created.
* 05.10.94 ESF Completed.
* 05.12.94 ESF Added more functionality.
* 01.05.95 DFE Fixed "Free Card" bug -- gave 1st person free card.
* 02.13.95 ESF Updated to reflect new registration interface.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_GameOverMan(void)
{
/* There will be another game */
fGameStarted = FALSE;
/* Since EndTurn isn't called, need to reset this so that first player
* doesn't get a free card next game.
*/
fGetsCard = FALSE;
/* The state will be one of registering */
if (iState != STATE_REGISTER)
{
iState = STATE_REGISTER;
UTIL_ServerEnterState(iState);
}
UTIL_DisplayError("");
UTIL_DisplayComment("");
/* Set the colors to be of the initial setup */
COLOR_SetWorldColors();
/* Erase the dice roll */
DICE_Hide();
/* Get the new players */
REG_PopupDialog();
}
/************************************************************************
* FUNCTION: GAME_ExchangeCards
* HISTORY:
* 03.29.94 ESF Created.
* 04.23.95 ESF Fixed (terrible) card take-away bug.
* 05.13.95 ESF Fixed (horrible) country bonus bug -- thanks Alan!
* 22.08.95 JC Fixed a bug with computerized player
* (piCards not ordered).
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_ExchangeCards(Int32 *piCards)
{
Int32 i, j, k, piOldCards[3];
MsgExchangeCards msgCards;
MsgNetMessage msgNetMessage;
char buf[256];
/* only 3 cards because we're handling selected cards */
/* Save a copy and transform array indices into card indices */
for (i=0; i!=3; i++)
{
piOldCards[i] = piCards[i];
piCards[i] = RISK_GetCardOfPlayer(iCurrentPlayer, piCards[i]);
}
/* Send the cards to the server -- this will generate an _REPLYPACKET */
for (i=0; i!=3; i++)
msgCards.piCards[i] = piCards[i];
(void)RISK_SendSyncMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_EXCHANGECARDS, &msgCards,
MSG_REPLYPACKET, CBK_IncomingMessage);
/* Update the date structures (bonus + countries on cards) */
RISK_SetNumArmiesOfPlayer(iCurrentPlayer,
RISK_GetNumArmiesOfPlayer(iCurrentPlayer)+iReply);
/* See if any of the cards match countries that the player has */
for (i=0; i!=3; i++)
if (piCards[i] < NUM_COUNTRIES && /* Not a joker! */
RISK_GetOwnerOfCountry(piCards[i]) == iCurrentPlayer)
RISK_SetNumArmiesOfCountry(piCards[i],
RISK_GetNumArmiesOfCountry(piCards[i])+2);
/* Take the cards away from the player */
for (i=0; i!=3; i++)
{
for (j=piOldCards[i]; j<MAX_CARDS-1; j++)
RISK_SetCardOfPlayer(iCurrentPlayer, j,
RISK_GetCardOfPlayer(iCurrentPlayer, j+1));
/* Now that we've moved all the cards down, we have to move the
* pointers to the selected cards down as well. This was a bug.
*/
for (k=i+1; k<3; k++)
if (piOldCards[k]>piOldCards[i])
piOldCards[k] = piOldCards[k]-1;
}
RISK_SetNumCardsOfPlayer(iCurrentPlayer,
RISK_GetNumCardsOfPlayer(iCurrentPlayer)-3);
/* Display a message */
#ifdef ENGLISH
snprintf(buf, sizeof(buf), "%s got %d armies from exchanging cards.",
#endif
#ifdef FRENCH
snprintf(buf, sizeof(buf), "%s gagne %d armes en changeant des cartes.",
#endif
RISK_GetNameOfPlayer(iCurrentPlayer),
iReply);
UTIL_DisplayError(buf);
msgNetMessage.strMessage = buf;
(void)RISK_SendMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()), MSG_NETMESSAGE,
&msgNetMessage);
}
/************************************************************************
* FUNCTION: GAME_EndTurn
* HISTORY:
* 03.17.94 ESF Created.
* 03.28.94 ESF Added card code, do we need Sync on the call?
* 03.28.94 ESF Moved iAttack/iDefend stuff here.
* 03.29.94 ESF Added iMove stuff.
* 04.11.94 ESF Added clear dice call.
* 04.11.94 ESF Move message call to end.
* 02.20.95 ESF Moved to game.c from utils.c
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_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);
}
/* Darken the country if the player was attacking or moving */
if (iAttackSrc >= 0)
UTIL_DarkenCountry(iAttackSrc);
if (iMoveSrc >= 0)
UTIL_DarkenCountry(iMoveSrc);
/* Clear the dice box */
DICE_Hide();
/* Init. variables for next turn */
fCanExchange = TRUE;
iAttackSrc = iAttackDst = iLastAttackSrc = iLastAttackDst = -1;
iMoveSrc = iMoveDst = -1;
/* Notify the server of the end-of-turn condition and wait for a response */
(void)RISK_SendSyncMessage(CLNT_GetCommLinkOfClient(CLNT_GetThisClientID()),
MSG_ENDTURN, NULL,
MSG_TURNNOTIFY, CBK_IncomingMessage);
}
/************************************************************************
* FUNCTION: GAME_IsMissionAccomplied
* HISTORY:
* 16.08.95 JC Created.
* 23.08.95 JC added iKilledPlayer.
* PURPOSE:
* NOTES:
************************************************************************/
Flag GAME_IsMissionAccomplied(Int32 iPlayer, Int32 iKilledPlayer)
{
Int32 piCount [NUM_CONTINENTS];
Int32 i, cont, n1, n2;
switch (RISK_GetMissionTypeOfPlayer(iPlayer))
{
case CONQUIER_WORLD:
if (RISK_GetNumCountriesOfPlayer(iPlayer) >= NUM_COUNTRIES)
return TRUE;
break;
case CONQUIER_Nb_COUNTRY:
n1 = RISK_GetMissionNumberOfPlayer(iPlayer);
if (RISK_GetNumCountriesOfPlayer(iPlayer) >= n1)
return TRUE;
break;
case CONQUIER_TWO_CONTINENTS:
for (cont=0; cont<NUM_CONTINENTS; cont++)
piCount[cont] = 0;
for (i=0; i<NUM_COUNTRIES; i++)
if (RISK_GetOwnerOfCountry(i) == iPlayer)
piCount[RISK_GetContinentOfCountry(i)]++;
n1 = RISK_GetMissionContinent1OfPlayer(iPlayer);
n2 = RISK_GetMissionContinent2OfPlayer(iPlayer);
if ( (piCount[n1] == RISK_GetNumCountriesOfContinent(n1))
&& (piCount[n2] == RISK_GetNumCountriesOfContinent(n2)))
return TRUE;
break;
case KILL_A_PLAYER:
n1 = RISK_GetMissionPlayerToKillOfPlayer(iPlayer);
if ( (RISK_GetNumCountriesOfPlayer(n1) <= 0)
&& (n1 == iKilledPlayer))
{
RISK_SetMissionPlayerIsKilledOfPlayer(iPlayer, TRUE);
return TRUE;
}
else if (RISK_GetMissionIsPlayerKilledOfPlayer(iPlayer))
return TRUE;
break;
}
return FALSE;
}
/************************************************************************
* FUNCTION: GAME_ShowMission
* HISTORY:
* 16.08.95 JC Created.
* PURPOSE:
* NOTES:
************************************************************************/
void GAME_ShowMission(Int32 iPlayer)
{
char buf[256];
GAME_MissionToStr(iPlayer, RISK_GetMissionTypeOfPlayer(iPlayer),
RISK_GetMissionContinent1OfPlayer(iPlayer),
RISK_GetMissionContinent2OfPlayer(iPlayer),
GAME_IsMissionAccomplied(iPlayer, -1),
buf, sizeof(buf));
UTIL_DisplayError(buf);
}
|