PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/kdegames-4.8.97/ksirk/ksirk/GameLogic/aiColsonPlayer.cpp

#
C++ | 2160 lines | 1649 code | 160 blank | 351 comment | 502 complexity | cf0449fb8437ec19b7974376ae490a4f MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. // kate: space-indent on; indent-width 2; replace-tabs on;
  2. /* This file is part of KsirK.
  3. Copyright (C) 2006-2007 Gael de Chalendar <kleag@free.fr>
  4. This file was initialy part of XFrisk
  5. Copyright (C) 1995 and later Jean-Claude Colson and Others <who@nowhere.org>
  6. KsirK is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation, version 2.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. /***********************************************************************
  19. *
  20. * 18-8-95 Created by Jean-Claude Colson.
  21. *
  22. * RISK game player.
  23. * $Id: aiColson.c,v 1.5 1999/11/07 15:57:29 tony Exp $
  24. *
  25. ***********************************************************************/
  26. #include "aiColsonPlayer.h"
  27. #include "onu.h"
  28. #include "goal.h"
  29. #include "continent.h"
  30. #include "gameautomaton.h"
  31. #include "kgamewin.h"
  32. #include "xfriskaiclient.h"
  33. #include "aiplayerio.h"
  34. #include "dice.h"
  35. #include <assert.h>
  36. namespace Ksirk
  37. {
  38. namespace GameLogic
  39. {
  40. AIColsonPlayer::AIColsonPlayer(
  41. const QString & nomPlayer, unsigned int nbArmies,
  42. Nationality * myNation, PlayersArray& players, ONU* world,
  43. GameAutomaton* game ) :
  44. AIPlayer(nomPlayer, nbArmies, myNation, players, world, game ),
  45. m_levelEnemy(0),
  46. m_initialized(false),
  47. Attack_SrcCountry(-1),
  48. Attack_DestCountry(-1),
  49. m_placeData(0)
  50. {
  51. kDebug();
  52. }
  53. AIColsonPlayer::~AIColsonPlayer()
  54. {
  55. delete m_placeData;
  56. }
  57. //////////////////////////////////////////////////////////
  58. // Virtual functions reimplemented from AIPlayer
  59. //////////////////////////////////////////////////////////
  60. QPair< const Country*, const Country* > AIColsonPlayer::chooseBelligerant()
  61. {
  62. kDebug();
  63. Fortify();
  64. Country* src = 0;
  65. Country* dest = 0;
  66. // Attack_SrcCountry = -1;
  67. // Attack_DestCountry = -1;
  68. // no attack was tempted
  69. if (!Attack())
  70. {
  71. Attack_SrcCountry = -1;
  72. Attack_DestCountry = -1;
  73. return qMakePair<const Country*, const Country*>(static_cast<Country*>(0), static_cast<Country*>(0));
  74. }
  75. if ( (Attack_SrcCountry>=0) && (Attack_SrcCountry<m_world->getCountries().size() ) )
  76. src = m_world->getCountries().at(Attack_SrcCountry);
  77. if ( (Attack_DestCountry>=0) && (Attack_DestCountry<m_world->getCountries().size() ) )
  78. dest = m_world->getCountries().at(Attack_DestCountry);
  79. // kDebug() << "chose belligerants " << src << " and " << dest;
  80. return qMakePair<const Country*, const Country*>(src,dest);
  81. }
  82. /**
  83. * Chooses the next action. Attack by default and if no attack is possible,
  84. * try to move armies and in the last resort, choose next player
  85. */
  86. void AIColsonPlayer::chooseAttackMoveArmiesOrNextPlayer()
  87. {
  88. kDebug();
  89. if (m_game->game()->haveAnimFighters() || m_game->game()->haveMovingArmies())
  90. {
  91. return;
  92. }
  93. if (!m_initialized)
  94. {
  95. finalize();
  96. }
  97. if (!attackAction())
  98. {
  99. if (!moveArmiesAction())
  100. {
  101. nextPlayerAction();
  102. }
  103. }
  104. // kDebug() <<"OUT AIColsonPlayer::chooseAttackMoveArmiesOrNextPlayer()";
  105. }
  106. /**
  107. * Chooses a country to receive a new army in dotation
  108. */
  109. Country* AIColsonPlayer::chooseReceivingCountry()
  110. {
  111. kDebug();
  112. if (m_placeData == 0)
  113. {
  114. if (!Place())
  115. {
  116. return 0;
  117. }
  118. }
  119. Country* res = m_placeData->dest;
  120. m_placeData->nb--;
  121. if (m_placeData->nb == 0)
  122. {
  123. delete m_placeData;
  124. m_placeData = 0;
  125. }
  126. return res;
  127. }
  128. /**
  129. * chooses to continue invasion with a certain amount of armies or to stop it
  130. */
  131. void AIColsonPlayer::chooseInvasionAction()
  132. {
  133. kDebug();
  134. kDebug() << " Attack_SrcCountry = " << Attack_SrcCountry;
  135. kDebug() << " Attack_DestCountry = " << Attack_DestCountry;
  136. if (Attack_SrcCountry == - 1 || Attack_DestCountry == -1)
  137. {
  138. stop();
  139. QByteArray buffer;
  140. QDataStream stream(&buffer, QIODevice::WriteOnly);
  141. QPointF point;
  142. stream << QString("actionInvasionFinished") << point;
  143. aiPlayerIO()->sendInput(stream,true);
  144. m_toMove = std::numeric_limits< unsigned int>::max();
  145. return;
  146. }
  147. if (m_toMove == std::numeric_limits< unsigned int>::max())
  148. {
  149. int nbEnemiesAdjacentToSrc = NbToEqualEnemyAdjacent(m_world->getCountries().at(Attack_SrcCountry));
  150. int nbEnemiesAdjacentToDest = NbToEqualEnemyAdjacent(m_world->getCountries().at(Attack_DestCountry));
  151. kDebug() << " nb on src = " << RISK_GetNumArmiesOfCountry(Attack_SrcCountry);
  152. kDebug() << " nb adj to src = " << nbEnemiesAdjacentToSrc;
  153. kDebug() << " nb adj to dest = " << nbEnemiesAdjacentToDest;
  154. int diff = nbEnemiesAdjacentToDest - nbEnemiesAdjacentToSrc;
  155. kDebug() << " diff = " << diff;
  156. m_toMove = (diff>RISK_GetNumArmiesOfCountry(Attack_SrcCountry)-1)?RISK_GetNumArmiesOfCountry(Attack_SrcCountry)-1:(diff<0?0:diff);
  157. kDebug() << " moves " << m_toMove;
  158. }
  159. QByteArray buffer;
  160. QDataStream stream(&buffer, QIODevice::WriteOnly);
  161. QPoint point;
  162. kDebug() << "Moves *****************" << m_toMove;
  163. if (m_toMove >= 10)
  164. {
  165. kDebug() << " choosing actionInvade10";
  166. stop();
  167. stream << QString("actionInvade10") << point;
  168. aiPlayerIO()->sendInput(stream,true);
  169. m_toMove -= 10;
  170. }
  171. else if (m_toMove >= 5)
  172. {
  173. kDebug() << " choosing actionInvade5";
  174. stop();
  175. stream << QString("actionInvade5") << point;
  176. aiPlayerIO()->sendInput(stream,true);
  177. m_toMove -= 5;
  178. }
  179. else if (m_toMove >= 1)
  180. {
  181. kDebug() << " choosing actionInvade1";
  182. stop();
  183. stream << QString("actionInvade1") << point;
  184. aiPlayerIO()->sendInput(stream,true);
  185. m_toMove--;
  186. }
  187. else
  188. {
  189. kDebug() << " choosing actionInvasionFinished";
  190. stop();
  191. stream << QString("actionInvasionFinished") << point;
  192. aiPlayerIO()->sendInput(stream,true);
  193. m_toMove = std::numeric_limits< unsigned int>::max();
  194. }
  195. }
  196. /**
  197. * makes all what is necessary to prepare and start the moving of armies
  198. */
  199. bool AIColsonPlayer::moveArmiesAction()
  200. {
  201. kDebug();
  202. bool res = Move();
  203. kDebug() << "Move got " << res;
  204. return res;
  205. }
  206. //////////////////////////////////////////////////////////
  207. // XFrisk AIColson functions
  208. //////////////////////////////////////////////////////////
  209. int AIColsonPlayer::getTotalArmiesOfPlayer(const Player* player)
  210. {
  211. int nb = 0;
  212. for (int i=0; i<m_world->getCountries().size(); i++)
  213. {
  214. if (m_world->getCountries().at(i)->owner() == player)
  215. {
  216. nb += m_world->getCountries().at(i)->nbArmies();
  217. }
  218. }
  219. return nb;
  220. }
  221. bool AIColsonPlayer::isContinentOfMission(const Player* player, const Continent* continent)
  222. {
  223. if (player->goal().type() != Goal::Continents)
  224. return false;
  225. return (player->goal().continents().contains(continent->name()));
  226. }
  227. bool AIColsonPlayer::isEnemyPlayer(const Player* player)
  228. {
  229. return m_isEnemyPlayer[player];
  230. }
  231. bool AIColsonPlayer::isFriendPlayer(const Player* player)
  232. {
  233. return (player==this);
  234. // return m_isEnemyPlayer[player] < m_levelEnemy;
  235. }
  236. int AIColsonPlayer::getNumEnemy()
  237. {
  238. kDebug();
  239. int nb = 0;
  240. PlayersArray::iterator it = m_game->playerList()->begin();
  241. PlayersArray::iterator it_end = m_game->playerList()->end();
  242. for (; it != it_end; it++)
  243. {
  244. if (m_isEnemyPlayer[((Player*)(*it))]>= m_levelEnemy)
  245. {
  246. nb++;
  247. }
  248. }
  249. if (m_levelEnemy == 1)
  250. nb--;
  251. return(nb);
  252. }
  253. bool AIColsonPlayer::isStrongerPlayer(const Player* player)
  254. {
  255. int nb;
  256. nb = getTotalArmiesOfPlayer(player);
  257. nb = nb + nb/5;
  258. PlayersArray::iterator it = m_game->playerList()->begin();
  259. PlayersArray::iterator it_end = m_game->playerList()->end();
  260. for (; it != it_end; it++)
  261. {
  262. if (getTotalArmiesOfPlayer(((Player*)(*it))) > nb)
  263. {
  264. return false;
  265. }
  266. }
  267. return true;
  268. }
  269. bool AIColsonPlayer::isSmallerPlayer(const Player* player)
  270. {
  271. int nb = 3 * getTotalArmiesOfPlayer(player);
  272. PlayersArray::iterator it = m_game->playerList()->begin();
  273. PlayersArray::iterator it_end = m_game->playerList()->end();
  274. for (; it != it_end; it++)
  275. {
  276. if (getTotalArmiesOfPlayer(((Player*)(*it))) > nb)
  277. {
  278. return false;
  279. }
  280. }
  281. return false;
  282. }
  283. bool AIColsonPlayer::isContinentOfPlayer(const Continent* continent, const Player* player)
  284. {
  285. if (continent == 0)
  286. {
  287. return false;
  288. }
  289. return (continent->owner() == player);
  290. }
  291. const Continent* AIColsonPlayer::computeChoiceOfContinent(void)
  292. {
  293. // kDebug();
  294. std::map< const KPlayer*, std::map <const Continent*, int > > piCount;
  295. std::map< const KPlayer*, std::map <const Continent*, bool > > maxContinent;
  296. std::map< const KPlayer*, std::map <const Continent*, bool > > posContinent;
  297. std::map<const KPlayer*, const Continent*> m_piContinent;
  298. QList<Continent*>::iterator continentsIt = (m_world->getContinents().begin());
  299. QList<Continent*>::iterator continentsIt_end = (m_world->getContinents().end());
  300. PlayersArray::iterator it = m_game->playerList()->begin();
  301. PlayersArray::iterator it_end = m_game->playerList()->end();
  302. for (; it != it_end; it++)
  303. {
  304. continentsIt = m_world->getContinents().begin();
  305. continentsIt_end = m_world->getContinents().end();
  306. for (; continentsIt != continentsIt_end; continentsIt++)
  307. {
  308. Continent *continent = *continentsIt;
  309. // kDebug() << " " << continent;
  310. if (continent == 0) continue;
  311. // kDebug() << " " << continent->name();
  312. piCount[((Player*)(*it))][continent] = 0;
  313. maxContinent[((Player*)(*it))][continent] = false;
  314. posContinent[((Player*)(*it))][continent] = false;
  315. }
  316. }
  317. for (int i=0; i<m_world->getCountries().size(); i++)
  318. {
  319. /* QList<Country*>::iterator countriesIt(m_world->getCountries().begin());
  320. QList<Country*>::iterator countriesIt_end(m_world->getCountries().end());
  321. for (; countriesIt != countriesIt_end; countriesIt++)
  322. {*/
  323. Country* country = m_world->getCountries().at(i);
  324. // kDebug() << "country " << country << ;
  325. // kDebug() << "country " << country->name().toUtf8().data();
  326. if ( country->owner() != 0 && country->continent() != 0)
  327. {
  328. piCount[country->owner()][country->continent()]++;
  329. // kDebug() << "piCount[" << country->owner()->name().toUtf8().data() << "][" << country->continent()->name().toUtf8().data() << "] ";
  330. // kDebug() << " is now " << piCount[country->owner()][country->continent()];
  331. }
  332. else
  333. {
  334. // kDebug() << "owner or continent of country " << country->name().toUtf8().data() << " is null...";
  335. }
  336. }
  337. it = m_game->playerList()->begin();
  338. it_end = m_game->playerList()->end();
  339. for (; it != it_end; it++)
  340. {
  341. int min = 10000;
  342. int max = 0;
  343. int bonus = 0;
  344. for (continentsIt = m_world->getContinents().begin();
  345. continentsIt!=continentsIt_end; continentsIt++)
  346. {
  347. const Continent* continent = *continentsIt;
  348. if (piCount[*it][continent] > 0)
  349. {
  350. if (min == 0)
  351. {
  352. if ( (piCount[*it][continent] == continent->getMembers().size())
  353. && (continent->getBonus()>bonus))
  354. {
  355. kDebug() << "pt2";
  356. max = piCount[*it][continent];
  357. bonus = continent->getBonus();
  358. maxContinent[*it][continent] = true;
  359. m_piContinent[*it] = continent;
  360. }
  361. }
  362. else if (piCount[*it][continent] == continent->getMembers().size())
  363. {
  364. min = 0;
  365. max = piCount[*it][continent];
  366. maxContinent[*it][continent] = true;
  367. m_piContinent[*it] = continent;
  368. }
  369. else if (piCount[*it][continent] > max)
  370. {
  371. QList<Continent*>::iterator jit(m_world->getContinents().begin());
  372. QList<Continent*>::iterator jit_end(m_world->getContinents().end());
  373. for (; jit!=jit_end; jit++)
  374. {
  375. Continent* j = *jit;
  376. maxContinent[*it][j] = false;
  377. if (piCount[*it][j] < max-1)
  378. posContinent[*it][j] = (piCount[*it][continent] < continent->getMembers().size()/2);
  379. else
  380. posContinent[*it][j] = true;
  381. }
  382. min = continent->getMembers().size() - piCount[*it][continent];
  383. max = piCount[*it][continent];
  384. bonus = continent->getBonus();
  385. maxContinent[*it][continent] = true;
  386. }
  387. else if ( (piCount[*it][continent] == max)
  388. && isContinentOfMission(dynamic_cast<Player*>(*it), continent))
  389. {
  390. min = continent->getMembers().size()
  391. - piCount[*it][continent];
  392. bonus = 2 * continent->getBonus();
  393. maxContinent[*it][continent] = true;
  394. }
  395. else if ( (piCount[*it][continent] == max)
  396. && (continent->getBonus() > bonus))
  397. {
  398. min = continent->getMembers().size() - piCount[*it][continent];
  399. bonus = continent->getBonus();
  400. maxContinent[*it][continent] = true;
  401. }
  402. else if (piCount[*it][continent] >= continent->getMembers().size()/2)
  403. {
  404. posContinent[*it][continent] = true;
  405. }
  406. }
  407. }
  408. }
  409. /* Search a continent with no conflict */
  410. const Continent* continent = 0;
  411. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  412. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  413. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  414. {
  415. const Continent* cont = *contIt;
  416. if (maxContinent[this][cont])
  417. {
  418. continent = cont;
  419. PlayersArray::iterator iit = m_game->playerList()->begin();
  420. PlayersArray::iterator iit_end = m_game->playerList()->end();
  421. for (;iit!=iit_end;iit++)
  422. {
  423. Player* i = static_cast<Player*>(*iit);
  424. if (i != this)
  425. {
  426. if ( m_piContinent[i] == cont)
  427. continent = *(m_world->getContinents().begin());
  428. else if ( (maxContinent[i][cont])
  429. && (piCount[i][cont] > piCount[this][cont]))
  430. continent = 0;
  431. }
  432. }
  433. }
  434. }
  435. if (isFriendPlayer(this) && (continent!=0))
  436. {
  437. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  438. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  439. #ifdef __GNUC__
  440. #warning continent can not be 0 in this code path - everything in this for loop is dead code
  441. #endif
  442. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  443. {
  444. const Continent* cont = *contIt;
  445. if (cont != continent)
  446. {
  447. maxContinent[this][cont] = false;
  448. posContinent[this][cont] = false;
  449. }
  450. }
  451. PlayersArray::iterator iit = m_game->playerList()->begin();
  452. PlayersArray::iterator iit_end = m_game->playerList()->end();
  453. for (;iit!=iit_end;iit++)
  454. {
  455. Player* i = static_cast<Player*>(*iit);
  456. posContinent[i][continent] = false;
  457. }
  458. return continent;
  459. }
  460. /* Search a continent with no computer conflict */
  461. continent = 0;
  462. contIt = m_world->getContinents().begin();
  463. contIt_end = m_world->getContinents().end();
  464. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  465. {
  466. const Continent* cont = *contIt;
  467. if (maxContinent[this][cont])
  468. {
  469. continent = cont;
  470. PlayersArray::iterator iit = m_game->playerList()->begin();
  471. PlayersArray::iterator iit_end = m_game->playerList()->end();
  472. for (;iit!=iit_end;iit++)
  473. {
  474. Player* i = static_cast<Player*>(*iit);
  475. if (isFriendPlayer(i) && (i != this))
  476. {
  477. if ( m_piContinent[i] == cont)
  478. continent = 0;
  479. else if ( (maxContinent[i][cont])
  480. && (piCount[i][cont] > piCount[this][cont]))
  481. continent = 0;
  482. }
  483. }
  484. }
  485. }
  486. if (continent!=0)
  487. {
  488. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  489. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  490. for (;contIt!=contIt_end;contIt++)
  491. {
  492. const Continent* cont = *contIt;
  493. //for (cont=0; cont!=m_world->getContinents().count(); cont++)
  494. if (cont != continent)
  495. {
  496. maxContinent[this][cont] = false;
  497. posContinent[this][cont] = false;
  498. }
  499. }
  500. PlayersArray::iterator iit = m_game->playerList()->begin();
  501. PlayersArray::iterator iit_end = m_game->playerList()->end();
  502. for (;iit!=iit_end;iit++)
  503. {
  504. Player* pi = static_cast<Player*>(*iit);
  505. posContinent[pi][continent] = false;
  506. }
  507. kDebug() << "pt5";
  508. return continent;
  509. }
  510. /* Search a possible continent with no conflict */
  511. continent = 0;
  512. contIt = m_world->getContinents().begin();
  513. contIt_end = m_world->getContinents().end();
  514. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  515. {
  516. const Continent* cont = *contIt;
  517. if (posContinent[this][cont])
  518. {
  519. continent = cont;
  520. PlayersArray::iterator iit = m_game->playerList()->begin();
  521. PlayersArray::iterator iit_end = m_game->playerList()->end();
  522. for (;iit!=iit_end;iit++)
  523. {
  524. Player* pi = static_cast<Player*>(*iit);
  525. if (isFriendPlayer(pi) && (pi != this))
  526. {
  527. if ( m_piContinent[pi] == cont)
  528. continent = 0;
  529. else if ( (maxContinent[pi][cont])
  530. && (piCount[pi][cont] > piCount[this][cont]))
  531. continent = 0;
  532. }
  533. }
  534. }
  535. }
  536. if (continent!=0)
  537. {
  538. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  539. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  540. for (;contIt!=contIt_end;contIt++)
  541. {
  542. const Continent* cont = *contIt;
  543. //for (cont=0; cont!=m_world->getContinents().count(); cont++)
  544. if (cont != continent)
  545. {
  546. maxContinent[this][cont] = false;
  547. posContinent[this][cont] = false;
  548. }
  549. }
  550. PlayersArray::iterator iit = m_game->playerList()->begin();
  551. PlayersArray::iterator iit_end = m_game->playerList()->end();
  552. for (;iit!=iit_end;iit++)
  553. {
  554. Player* pi = static_cast<Player*>(*iit);
  555. posContinent[pi][continent] = false;
  556. }
  557. kDebug() << "pt6";
  558. return continent;
  559. }
  560. /* Search a possible continent with no computer conflict */
  561. continent = 0;
  562. contIt = m_world->getContinents().begin();
  563. contIt_end = m_world->getContinents().end();
  564. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  565. {
  566. const Continent* cont = *contIt;
  567. if (posContinent[this][cont])
  568. {
  569. continent = cont;
  570. PlayersArray::iterator iit = m_game->playerList()->begin();
  571. PlayersArray::iterator iit_end = m_game->playerList()->end();
  572. for (;iit!=iit_end;iit++)
  573. {
  574. Player* pi = static_cast<Player*>(*iit);
  575. if (isFriendPlayer(pi) && (pi != this))
  576. {
  577. if ( m_piContinent[pi] == cont)
  578. continent = 0;
  579. else if ( (maxContinent[pi][cont])
  580. && (piCount[pi][cont] > piCount[this][cont]))
  581. continent = 0;
  582. }
  583. }
  584. }
  585. }
  586. if (continent!=0)
  587. {
  588. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  589. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  590. for (;contIt!=contIt_end;contIt++)
  591. {
  592. const Continent* cont = *contIt;
  593. if (cont != continent)
  594. {
  595. maxContinent[this][cont] = false;
  596. }
  597. }
  598. kDebug() << "pt7";
  599. return continent;
  600. }
  601. continent = 0;
  602. /* Search a continent */
  603. contIt = m_world->getContinents().begin();
  604. contIt_end = m_world->getContinents().end();
  605. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  606. {
  607. const Continent* cont = *contIt;
  608. if (piCount[this][cont]>0)
  609. {
  610. continent = cont;
  611. PlayersArray::iterator iit = m_game->playerList()->begin();
  612. PlayersArray::iterator iit_end = m_game->playerList()->end();
  613. for (;iit!=iit_end;iit++)
  614. {
  615. Player* pi = static_cast<Player*>(*iit);
  616. if (isFriendPlayer(pi) && (pi != this))
  617. continent = 0;
  618. }
  619. }
  620. }
  621. if (continent!=0)
  622. {
  623. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  624. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  625. for (;contIt!=contIt_end;contIt++)
  626. {
  627. const Continent* cont = *contIt;
  628. if (cont!=continent)
  629. {
  630. maxContinent[this][cont] = false;
  631. }
  632. }
  633. kDebug() << "pt8";
  634. return continent;
  635. }
  636. continent = 0;
  637. /* Search a continent */
  638. contIt = m_world->getContinents().begin();
  639. contIt_end = m_world->getContinents().end();
  640. for (;(contIt!=contIt_end) && (continent==0);contIt++)
  641. {
  642. const Continent* cont = *contIt;
  643. if (maxContinent[this][cont])
  644. continent = cont;
  645. }
  646. // kDebug() << "AIColsonPlayer::computeChoiceOfContinent for " << name().toUtf8().data()
  647. // << " is " << continent;
  648. return continent;
  649. }
  650. /**
  651. * Gets the continent already selected for this player
  652. * @param attack This is the number of countries of the selected continent
  653. * that have at least one adjacent enemy
  654. */
  655. const Continent* AIColsonPlayer::GetContinentToFortify(int *attack)
  656. {
  657. kDebug();
  658. const Continent* continent = computeChoiceOfContinent();
  659. kDebug() << " computeChoiceOfContinent got " << continent;
  660. *attack = 0;
  661. for (int i=0; i!=m_world->getCountries().size(); i++)
  662. {
  663. if ( (RISK_GetOwnerOfCountry(i) == this)
  664. && (RISK_GetContinentOfCountry(i) == continent)
  665. && GAME_IsEnemyAdjacent(i))
  666. {
  667. (*attack)++;
  668. }
  669. }
  670. kDebug() << "found " << continent;
  671. return(continent);
  672. }
  673. /**
  674. * @param attack Number of countries with adjacent ennemies in the selected
  675. * continent
  676. */
  677. const Continent* AIColsonPlayer::getContinentToConquier(int *attack)
  678. {
  679. kDebug();
  680. std::map<const Continent*, int> piCount;
  681. std::map<const Continent*, int> piOppo;
  682. std::map<const Continent*, int> piAttac;
  683. /* Init. */
  684. QList<Continent*>::iterator contIt(m_world->getContinents().begin());
  685. QList<Continent*>::iterator contIt_end(m_world->getContinents().end());
  686. for (;contIt!=contIt_end;contIt++)
  687. {
  688. const Continent* cont = *contIt;
  689. piCount[cont] = 0;
  690. piOppo[cont] = 0;
  691. piAttac[cont] = 0;
  692. }
  693. /* Count up how many countries the player has in each of the continents */
  694. QList<Country*>::iterator countriesIt(m_world->getCountries().begin());
  695. QList<Country*>::iterator countriesIt_end(m_world->getCountries().end());
  696. for (; countriesIt != countriesIt_end; countriesIt++)
  697. {
  698. Country* country = *countriesIt;
  699. if (country->owner() == this)
  700. {
  701. piCount[country->continent()]++;
  702. piOppo[country->continent()] -= country->nbArmies();
  703. if (country->hasAdjacentEnemy())
  704. piAttac[country->continent()]++;
  705. }
  706. else
  707. {
  708. if (isEnemyPlayer(country->owner()))
  709. piOppo[country->continent()] += country->nbArmies();
  710. else
  711. piOppo[country->continent()] += 2 * country->nbArmies();
  712. }
  713. }
  714. const Continent* continent = 0;
  715. int min = 10000;
  716. int max = 0;
  717. int bonus = 0;
  718. contIt = m_world->getContinents().begin();
  719. contIt_end = m_world->getContinents().end();
  720. for (; contIt != contIt_end; contIt++)
  721. {
  722. const Continent* cont = *contIt;
  723. kDebug() << " " << cont;
  724. if (cont != 0)
  725. kDebug() << " " << cont->name();
  726. if (isContinentOfMission(this, cont))
  727. {
  728. piOppo[cont] -= piOppo[cont]/3;
  729. }
  730. if (piCount[cont] < cont->getMembers().size())
  731. {
  732. if (piOppo[cont] < min)
  733. {
  734. continent = cont;
  735. min = piOppo[cont];
  736. max = piCount[cont];
  737. bonus = cont->getBonus();
  738. }
  739. }
  740. }
  741. *attack = piAttac[continent];
  742. return(continent);
  743. }
  744. int AIColsonPlayer::NbEnemyAdjacent(Country* iCountry)
  745. {
  746. std::map<Player*, int> NumEnemy;
  747. std::map<Player*, bool> fIsEnemy;
  748. bool fIAmStrong;
  749. Player* player = iCountry->owner();
  750. fIAmStrong = isStrongerPlayer(player);
  751. PlayersArray::iterator pit = m_game->playerList()->begin();
  752. PlayersArray::iterator pit_end = m_game->playerList()->end();
  753. for (; pit != pit_end; pit++)
  754. {
  755. fIsEnemy[(Player*)(*pit)] = false;
  756. }
  757. Player* iEnemy = 0;
  758. Country* destCountry;
  759. QList<Country*>::const_iterator it, it_end;
  760. it = iCountry->neighbours().constBegin();
  761. it_end = iCountry->neighbours().constEnd();
  762. for (;it != it_end; it++)
  763. {
  764. destCountry = *it;
  765. iEnemy = destCountry->owner();
  766. if (iEnemy != player)
  767. {
  768. if (NumEnemy[iEnemy] < destCountry->nbArmies())
  769. NumEnemy[iEnemy] = destCountry->nbArmies();
  770. fIsEnemy[iEnemy] = true;
  771. }
  772. }
  773. int max = 0;
  774. pit = m_game->playerList()->begin();
  775. pit_end = m_game->playerList()->end();
  776. for (; pit != pit_end; pit++)
  777. {
  778. Player* i = (Player*)(*pit);
  779. if ((i != player) && fIsEnemy[i])
  780. {
  781. int nb = NumEnemy[iEnemy];
  782. if (!fIAmStrong && isStrongerPlayer(i))
  783. nb = nb + 10;
  784. if (isEnemyPlayer(i))
  785. nb = nb + 5;
  786. if (nb > max)
  787. max = nb;
  788. }
  789. }
  790. return max;
  791. }
  792. int AIColsonPlayer::NbToAverageEnemyAdjacent(Country* iCountry)
  793. {
  794. int nbe = 0;
  795. int nb = 0;
  796. int i = 0;
  797. QList<Country*>::const_iterator neighbourIt, neighbourIt_end;
  798. neighbourIt = iCountry->neighbours().constBegin();
  799. neighbourIt_end = iCountry->neighbours().constEnd();
  800. for (;neighbourIt != neighbourIt_end; neighbourIt++)
  801. {
  802. Country* destCountry = *neighbourIt;
  803. Player* iEnemy = destCountry->owner();
  804. if (iEnemy != this)
  805. {
  806. nbe -= destCountry->nbArmies();
  807. nb++;
  808. }
  809. i++;
  810. }
  811. int nbi = iCountry->nbArmies();
  812. nbe = (nbe + nbi)/nb;
  813. // if ((nbi - nbe)< 10)
  814. // nbe = nbi - 10;
  815. kDebug() << "NbToAverageEnemyAdjacent of " << iCountry->name() << " is " << nbe;
  816. return nbe;
  817. }
  818. int AIColsonPlayer::NbToEqualEnemyAdjacent(Country* iCountry)
  819. {
  820. int nbe = 0;
  821. QList<Country*>::const_iterator neighbourIt, neighbourIt_end;
  822. neighbourIt = iCountry->neighbours().constBegin();
  823. neighbourIt_end = iCountry->neighbours().constEnd();
  824. for (;neighbourIt != neighbourIt_end; neighbourIt++)
  825. {
  826. Country* destCountry = *neighbourIt;
  827. Player* iEnemy = destCountry->owner();
  828. if (iEnemy != this)
  829. {
  830. nbe += destCountry->nbArmies();
  831. }
  832. }
  833. int nbi = iCountry->nbArmies();
  834. int nb = (nbe - nbi);
  835. kDebug() << "NbToEqualEnemyAdjacent of " << iCountry->name() << " is " << nb;
  836. return nb;
  837. }
  838. /************************************************************************/
  839. bool AIColsonPlayer::ComputerAttack(int destCountry, bool die, int dif)
  840. {
  841. kDebug() << destCountry;
  842. if (Attack_SrcCountry!=-1 && Attack_DestCountry!=-1
  843. && (RISK_GetOwnerOfCountry(Attack_SrcCountry) == this)
  844. && (RISK_GetOwnerOfCountry(Attack_DestCountry) != this)
  845. && (RISK_GetNumArmiesOfCountry(Attack_SrcCountry) > 1)
  846. && (die || ( RISK_GetNumArmiesOfCountry(Attack_SrcCountry)
  847. > RISK_GetNumArmiesOfCountry(Attack_DestCountry))))
  848. {
  849. kDebug() << " Retry attack";
  850. return true;
  851. }
  852. int srcCountry = -1;
  853. int max = RISK_GetNumArmiesOfCountry(destCountry) + dif;
  854. int i = 0;
  855. while ((i < 6) && (RISK_GetAdjCountryOfCountry(destCountry, i) != -1))
  856. {
  857. int iCountry = RISK_GetAdjCountryOfCountry(destCountry, i);
  858. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  859. && (RISK_GetNumArmiesOfCountry(iCountry) > max) )
  860. {
  861. max = RISK_GetNumArmiesOfCountry(iCountry);
  862. srcCountry = iCountry;
  863. }
  864. i++;
  865. }
  866. if (srcCountry == -1)
  867. {
  868. Attack_SrcCountry = -1;
  869. Attack_DestCountry = -1;
  870. return false;
  871. }
  872. kDebug() << " srcCountry = " << srcCountry;
  873. Attack_SrcCountry = srcCountry;
  874. Attack_DestCountry = destCountry;
  875. // AI_Attack (srcCountry, destCountry, ATTACK_DOORDIE, DICE_MAXIMUM, iMove);
  876. return true;
  877. }
  878. bool AIColsonPlayer::Fortify()
  879. {
  880. kDebug() << "1";
  881. int nb;
  882. const Continent* continent = GetContinentToFortify(&nb);
  883. kDebug() << "2";
  884. if (nb > 0)
  885. {
  886. nb = rand() % nb;
  887. for ( int iCountry = 0;
  888. iCountry < m_world->getCountries().size();
  889. iCountry++)
  890. {
  891. if ((RISK_GetOwnerOfCountry(iCountry) == this) &&
  892. (RISK_GetContinentOfCountry(iCountry) == continent) &&
  893. GAME_IsEnemyAdjacent(iCountry))
  894. {
  895. if (nb <= 0)
  896. {
  897. AI_Place (iCountry, 1);
  898. return true;
  899. }
  900. nb--;
  901. }
  902. }
  903. }
  904. kDebug() << "3";
  905. for ( int iCountry = 0;
  906. iCountry < m_world->getCountries().size();
  907. iCountry++)
  908. {
  909. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  910. && GAME_IsEnemyAdjacent(iCountry))
  911. {
  912. AI_Place (iCountry, 1);
  913. return true;
  914. }
  915. }
  916. kDebug() << "4";
  917. for ( int iCountry = 0;
  918. iCountry < m_world->getCountries().size();
  919. iCountry++)
  920. {
  921. if (RISK_GetOwnerOfCountry(iCountry) == this)
  922. {
  923. AI_Place (iCountry, 1);
  924. return true;
  925. }
  926. }
  927. return false;
  928. }
  929. bool AIColsonPlayer::Place()
  930. {
  931. kDebug();
  932. /* Try to destroy a weak enemy player.
  933. * NOTE this could be an error
  934. * if destroying this player is another player goal... */
  935. kDebug() << "1. Try to destroy a enemy player";
  936. for (int iCountry = 0; iCountry < m_world->getCountries().size(); iCountry++)
  937. {
  938. if ((RISK_GetOwnerOfCountry(iCountry) == this) &&
  939. GAME_IsEnemyAdjacent(iCountry))
  940. {
  941. int i = 0;
  942. while ((i < 6) && (RISK_GetAdjCountryOfCountry(iCountry, i) != -1))
  943. {
  944. int destCountry = RISK_GetAdjCountryOfCountry(iCountry, i);
  945. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  946. if ( (iEnemy != this) && isEnemyPlayer(iEnemy)
  947. && (getTotalArmiesOfPlayer(iEnemy)
  948. < ( RISK_GetNumArmiesOfPlayer(this)
  949. + RISK_GetNumArmiesOfCountry(iCountry) - 5))
  950. && (RISK_GetNumArmiesOfCountry(iCountry) < (2*RISK_GetNumArmiesOfCountry(destCountry))+3) )
  951. {
  952. kDebug() << " iCountry: " << RISK_GetNumArmiesOfCountry(iCountry)
  953. << " ; destCountry: " << RISK_GetNumArmiesOfCountry(destCountry);
  954. AI_Place (iCountry, 1);
  955. return true;
  956. }
  957. i++;
  958. }
  959. }
  960. }
  961. int nbCountriesWithAdjacentEnemies;
  962. const Continent* continent = GetContinentToFortify(&nbCountriesWithAdjacentEnemies);
  963. /* Try to conquier an entire continent, attack enemy */
  964. kDebug() << "2. Try to conquier an entire continent, attack enemy";
  965. for (int destCountry = 0;
  966. destCountry < m_world->getCountries().size();
  967. destCountry++)
  968. {
  969. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  970. if ( (iEnemy != this) && isEnemyPlayer(iEnemy)
  971. && (RISK_GetContinentOfCountry(destCountry) == continent))
  972. {
  973. int i = 0;
  974. while ((i < 6) && (RISK_GetAdjCountryOfCountry(destCountry, i) != -1))
  975. {
  976. int iCountry = RISK_GetAdjCountryOfCountry(destCountry, i);
  977. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  978. && (RISK_GetNumArmiesOfCountry(iCountry) < (1.5*RISK_GetNumArmiesOfCountry(destCountry))+3)
  979. )
  980. {
  981. AI_Place (iCountry, 1);
  982. return true;
  983. }
  984. i++;
  985. }
  986. }
  987. }
  988. /* Try to destroy a player */
  989. kDebug() << "3. Try to destroy a player";
  990. for (int iCountry = 0;
  991. iCountry < m_world->getCountries().size();
  992. iCountry++)
  993. {
  994. if ((RISK_GetOwnerOfCountry(iCountry) == this) &&
  995. GAME_IsEnemyAdjacent(iCountry))
  996. {
  997. int i = 0;
  998. while ((i < 6) && (RISK_GetAdjCountryOfCountry(iCountry, i) != -1))
  999. {
  1000. int destCountry = RISK_GetAdjCountryOfCountry(iCountry, i);
  1001. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1002. if ( (iEnemy != this)
  1003. && (getTotalArmiesOfPlayer(iEnemy)
  1004. < ( RISK_GetNumArmiesOfPlayer(this)
  1005. + RISK_GetNumArmiesOfCountry(iCountry) - 5))
  1006. && (RISK_GetNumArmiesOfCountry(iCountry) < (1.5*RISK_GetNumArmiesOfCountry(destCountry))+3)
  1007. )
  1008. {
  1009. AI_Place (iCountry, 1);
  1010. return true;
  1011. }
  1012. i++;
  1013. }
  1014. }
  1015. }
  1016. /* Try to conquier an entire continent */
  1017. kDebug() << "4. Try to conquier an entire continent";
  1018. for (int destCountry = 0;
  1019. destCountry < m_world->getCountries().size();
  1020. destCountry++)
  1021. {
  1022. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1023. if ( (RISK_GetContinentOfCountry(destCountry) == continent)
  1024. && (iEnemy != this))
  1025. {
  1026. int i = 0;
  1027. while ((i < 6) && (RISK_GetAdjCountryOfCountry(destCountry, i) != -1))
  1028. {
  1029. int iCountry = RISK_GetAdjCountryOfCountry(destCountry, i);
  1030. if (RISK_GetOwnerOfCountry(iCountry) == this
  1031. && (RISK_GetNumArmiesOfCountry(iCountry) < (1.5*RISK_GetNumArmiesOfCountry(destCountry))+3)
  1032. )
  1033. {
  1034. AI_Place (iCountry, 1);
  1035. return true;
  1036. }
  1037. i++;
  1038. }
  1039. }
  1040. }
  1041. /* Try to defend an entire continent */
  1042. kDebug() << "5. Try to defend an entire continent";
  1043. if ( (nbCountriesWithAdjacentEnemies > 0)
  1044. && (RISK_GetNumArmiesOfPlayer(this) > 0) )
  1045. {
  1046. int min = 0;
  1047. bool boool = false;
  1048. int destCountry = -1;
  1049. for (int iCountry = 0;
  1050. iCountry < m_world->getCountries().size();
  1051. iCountry++)
  1052. {
  1053. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  1054. && (RISK_GetContinentOfCountry(iCountry) == continent)
  1055. && GAME_IsEnemyAdjacent(iCountry))
  1056. {
  1057. int nb = NbToAverageEnemyAdjacent(m_world->getCountries().at(iCountry));
  1058. if (nb < min)
  1059. {
  1060. min = nb ;
  1061. destCountry = iCountry;
  1062. boool = true;
  1063. }
  1064. }
  1065. }
  1066. if (boool)
  1067. {
  1068. AI_Place (destCountry, 1);
  1069. return true;
  1070. }
  1071. }
  1072. kDebug() << "6. Try to conquier an entire continent, attack enemy";
  1073. continent = getContinentToConquier(&nbCountriesWithAdjacentEnemies);
  1074. /* Try to conquier an entire continent, attack enemy */
  1075. for (int iCountry = 0;
  1076. iCountry < m_world->getCountries().size();
  1077. iCountry++)
  1078. {
  1079. if ((RISK_GetOwnerOfCountry(iCountry) == this) &&
  1080. (RISK_GetContinentOfCountry(iCountry) == continent) &&
  1081. GAME_IsEnemyAdjacent(iCountry))
  1082. {
  1083. int i = 0;
  1084. while ((i < 6) && (RISK_GetAdjCountryOfCountry(iCountry, i) != -1))
  1085. {
  1086. int destCountry = RISK_GetAdjCountryOfCountry(iCountry, i);
  1087. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1088. if ( (iEnemy != this) && isEnemyPlayer(iEnemy)
  1089. && (RISK_GetContinentOfCountry(destCountry) == continent)
  1090. && (RISK_GetNumArmiesOfCountry(iCountry) < (1.5*RISK_GetNumArmiesOfCountry(destCountry))+3)
  1091. )
  1092. {
  1093. kDebug() << "iCountry " << RISK_GetNumArmiesOfCountry(iCountry)
  1094. << " ; destCountry "
  1095. << RISK_GetNumArmiesOfCountry(destCountry);
  1096. AI_Place (iCountry, 1);
  1097. return true;
  1098. }
  1099. i++;
  1100. }
  1101. }
  1102. }
  1103. kDebug() << "7. Try to conquier an entire continent";
  1104. /* Try to conquier an entire continent */
  1105. for (int iCountry = 0;
  1106. iCountry < m_world->getCountries().size();
  1107. iCountry++)
  1108. {
  1109. if ((RISK_GetOwnerOfCountry(iCountry) == this) &&
  1110. (RISK_GetContinentOfCountry(iCountry) == continent) &&
  1111. GAME_IsEnemyAdjacent(iCountry))
  1112. {
  1113. int i = 0;
  1114. while ((i < 6) && (RISK_GetAdjCountryOfCountry(iCountry, i) != -1))
  1115. {
  1116. int destCountry = RISK_GetAdjCountryOfCountry(iCountry, i);
  1117. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1118. if ( (iEnemy != this)
  1119. && (RISK_GetContinentOfCountry(destCountry) == continent)
  1120. && (RISK_GetNumArmiesOfCountry(iCountry) < (1.5*RISK_GetNumArmiesOfCountry(destCountry))+3)
  1121. )
  1122. {
  1123. AI_Place (iCountry, 1);
  1124. return true;
  1125. }
  1126. i++;
  1127. }
  1128. }
  1129. }
  1130. kDebug() << "8. Try to defend an entire continent";
  1131. /* Try to defend an entire continent */
  1132. if (nbCountriesWithAdjacentEnemies > 0)
  1133. {
  1134. int min = 2; // a la place de 1000
  1135. bool boool = false;
  1136. int destCountry = -1;
  1137. for (int iCountry = 0;
  1138. iCountry < m_world->getCountries().size();
  1139. iCountry++)
  1140. {
  1141. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  1142. && (RISK_GetContinentOfCountry(iCountry) == continent)
  1143. && GAME_IsEnemyAdjacent(iCountry))
  1144. {
  1145. int nb = NbToAverageEnemyAdjacent(m_world->getCountries().at(iCountry));
  1146. if (nb < min)
  1147. {
  1148. // min = RISK_GetNumArmiesOfCountry(iCountry);
  1149. min = nb;
  1150. destCountry = iCountry;
  1151. boool = true;
  1152. }
  1153. }
  1154. }
  1155. if (boool)
  1156. {
  1157. AI_Place (destCountry, 1);
  1158. return true;
  1159. }
  1160. }
  1161. kDebug() << "9. Try to prepare an enemy attack, find a lowest defence";
  1162. /* Try to prepare an enemy attack, find a lowest defence */
  1163. int selected = 0;
  1164. int myDefenceDelta = std::numeric_limits<int>::max();
  1165. bool boool9 = false;
  1166. for (int iCountry = 0;
  1167. iCountry < m_world->getCountries().size();
  1168. iCountry++)
  1169. {
  1170. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  1171. && GAME_IsEnemyAdjacent(iCountry))
  1172. {
  1173. int i = 0;
  1174. while ((i < 6) && (RISK_GetAdjCountryOfCountry(iCountry, i) != -1))
  1175. {
  1176. int destCountry = RISK_GetAdjCountryOfCountry(iCountry, i);
  1177. int delta = RISK_GetNumArmiesOfCountry(iCountry) - RISK_GetNumArmiesOfCountry(destCountry);
  1178. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1179. if ( (RISK_GetContinentOfCountry(destCountry) == continent)
  1180. && (iEnemy != this) && isEnemyPlayer(iEnemy)
  1181. && ( ( RISK_GetNumArmiesOfCountry(iCountry) <
  1182. (1.5*RISK_GetNumArmiesOfCountry(destCountry))+3 )
  1183. || (RISK_GetNumArmiesOfCountry(iCountry)==1) )
  1184. && (delta < myDefenceDelta ) )
  1185. {
  1186. selected = iCountry;
  1187. myDefenceDelta = delta;
  1188. boool9 = true;
  1189. }
  1190. i++;
  1191. }
  1192. }
  1193. }
  1194. if (boool9)
  1195. {
  1196. AI_Place (selected, 1);
  1197. return true;
  1198. }
  1199. kDebug() << "10. Try to prepare an enemy attack";
  1200. /* Try to prepare an enemy attack */
  1201. bool boool10 = false;
  1202. myDefenceDelta = std::numeric_limits<int>::max();
  1203. for (int iCountry = 0;
  1204. iCountry < m_world->getCountries().size();
  1205. iCountry++)
  1206. {
  1207. // kDebug() << " looking at iCountry " << iCountry;
  1208. // kDebug() << " owner " << RISK_GetOwnerOfCountry(iCountry);
  1209. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  1210. && GAME_IsEnemyAdjacent(iCountry))
  1211. {
  1212. int i = 0;
  1213. while ((i < 6) && (RISK_GetAdjCountryOfCountry(iCountry, i) != -1))
  1214. {
  1215. int destCountry = RISK_GetAdjCountryOfCountry(iCountry, i);
  1216. int delta = RISK_GetNumArmiesOfCountry(iCountry) - RISK_GetNumArmiesOfCountry(destCountry);
  1217. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1218. // kDebug() << " looking at destCountry " << destCountry;
  1219. // kDebug() << " iEnemy " << RISK_GetOwnerOfCountry(destCountry);
  1220. // kDebug() << " isEnemyPlayer? " << isEnemyPlayer(iEnemy);
  1221. // kDebug() << " delta / myDefenceDelta " << delta << " / " << myDefenceDelta;
  1222. if ((iEnemy != this) && isEnemyPlayer(iEnemy)
  1223. && (delta < myDefenceDelta ) )
  1224. {
  1225. // kDebug() << " GOT IT " << iCountry;
  1226. myDefenceDelta = delta;
  1227. boool10 = true;
  1228. selected = iCountry;
  1229. }
  1230. i++;
  1231. }
  1232. }
  1233. }
  1234. if (boool10)
  1235. {
  1236. AI_Place (selected, 1);
  1237. //AI_Place (iCountry, RISK_GetNumArmiesOfPlayer(this));
  1238. return true;
  1239. }
  1240. // cannot reach this line ???
  1241. // assert(false);
  1242. kDebug() << "11. Try to prepare an attack";
  1243. /* Try to prepare an attack */
  1244. for (int iCountry = 0;
  1245. iCountry < m_world->getCountries().size();
  1246. iCountry++)
  1247. {
  1248. if ( (RISK_GetOwnerOfCountry(iCountry) == this)
  1249. && GAME_IsEnemyAdjacent(iCountry))
  1250. {
  1251. AI_Place (iCountry, 1);
  1252. return true;
  1253. }
  1254. }
  1255. kDebug() << "12. Try to place";
  1256. /* Try to place */
  1257. for (int iCountry = 0;
  1258. iCountry < m_world->getCountries().size();
  1259. iCountry++)
  1260. {
  1261. if (RISK_GetOwnerOfCountry(iCountry) == this)
  1262. {
  1263. AI_Place (iCountry, 1);
  1264. return true;
  1265. }
  1266. }
  1267. return false;
  1268. }
  1269. /** @return KsirK change: true if an attack have been tempted; false otherwise */
  1270. bool AIColsonPlayer::AttackEnemy()
  1271. {
  1272. kDebug();
  1273. int nbCountriesWithAdjacentEnemies;
  1274. const Continent* continent = GetContinentToFortify(&nbCountriesWithAdjacentEnemies);
  1275. if (Attack_SrcCountry!=-1 && Attack_DestCountry != -1
  1276. && (RISK_GetOwnerOfCountry(Attack_SrcCountry)==this)
  1277. && (RISK_GetOwnerOfCountry(Attack_DestCountry)!=this)
  1278. && ComputerAttack (Attack_DestCountry, true,
  1279. (RISK_GetNumArmiesOfCountry(Attack_DestCountry) < 5)?1:
  1280. ((nbCountriesWithAdjacentEnemies > 4)?RISK_GetNumArmiesOfCountry(Attack_DestCountry):3)))
  1281. {
  1282. kDebug() << "Attack tempted again.";
  1283. return true;
  1284. }
  1285. kDebug() << "1 Try to conquier an entire continent, attack player of other species ";
  1286. /* Try to conquier an entire continent, attack player of other species */
  1287. for ( int destCountry = 0;
  1288. destCountry < m_world->getCountries().size();
  1289. destCountry++)
  1290. {
  1291. // kDebug() << "destCountry=" << destCountry << " / m_world->getCountries().size()=" << m_world->getCountries().size();
  1292. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1293. if ( (RISK_GetContinentOfCountry(destCountry) == continent)
  1294. && (iEnemy != this) && isEnemyPlayer(iEnemy))
  1295. {
  1296. if (ComputerAttack (destCountry, true,
  1297. (RISK_GetNumArmiesOfCountry(destCountry) < 5)?1:
  1298. ((nbCountriesWithAdjacentEnemies > 4)?RISK_GetNumArmiesOfCountry(destCountry):3)))
  1299. {
  1300. destCountry = 0;
  1301. kDebug() << "Attack tempted.";
  1302. return true;
  1303. }
  1304. }
  1305. }
  1306. kDebug() << "2 Try to conquier an entire continent: " << continent;
  1307. /* Try to conquier an entire continent */
  1308. for (int destCountry = 0; destCountry < m_world->getCountries().size(); destCountry++)
  1309. {
  1310. if ( (RISK_GetContinentOfCountry(destCountry) == continent)
  1311. && (RISK_GetOwnerOfCountry(destCountry) != this))
  1312. {
  1313. if (ComputerAttack (destCountry, true,
  1314. (RISK_GetNumArmiesOfCountry(destCountry) < 3)?1:50))
  1315. {
  1316. destCountry = 0;
  1317. kDebug() << "Attack tempted.";
  1318. return true;
  1319. }
  1320. }
  1321. }
  1322. kDebug() << "should abandon ?";
  1323. if ( !isContinentOfPlayer(continent, this)
  1324. && (m_numTurn[this] <= 2)
  1325. && (allPlayers.count() > m_world->getContinents().size()/2))
  1326. {
  1327. kDebug() << "No attack tried";
  1328. Attack_SrcCountry = -1;
  1329. Attack_DestCountry = -1;
  1330. return false;
  1331. }
  1332. kDebug() << "3 Try to destroy a human player";
  1333. /* Try to destroy a human player */
  1334. continent = getContinentToConquier(&nbCountriesWithAdjacentEnemies);
  1335. for (int destCountry = 0;
  1336. destCountry < m_world->getCountries().size();
  1337. destCountry++)
  1338. {
  1339. if ( (RISK_GetOwnerOfCountry(destCountry) != this)
  1340. && (!RISK_GetOwnerOfCountry(destCountry)->isAI())
  1341. && (RISK_GetNumCountriesOfPlayer(RISK_GetOwnerOfCountry(destCountry)) == 1))
  1342. {
  1343. if (ComputerAttack (destCountry, true,
  1344. (nbCountriesWithAdjacentEnemies > 2)?10:2))
  1345. {
  1346. kDebug() << "Attack tempted.";
  1347. return true;
  1348. }
  1349. }
  1350. }
  1351. kDebug() << "4 Try to destroy a enemy player";
  1352. /* Try to destroy a enemy player */
  1353. for (int destCountry = 0;
  1354. destCountry < m_world->getCountries().size();
  1355. destCountry++)
  1356. {
  1357. if ( isEnemyPlayer(RISK_GetOwnerOfCountry(destCountry))
  1358. && (RISK_GetNumCountriesOfPlayer(RISK_GetOwnerOfCountry(destCountry)) == 1))
  1359. {
  1360. if (ComputerAttack (destCountry, true,
  1361. (nbCountriesWithAdjacentEnemies > 2)?20:2))
  1362. {
  1363. kDebug() << "Attack tempted.";
  1364. return true;
  1365. }
  1366. }
  1367. }
  1368. kDebug() << "Try to destroy a player";
  1369. /* Try to destroy a player */
  1370. for (int destCountry = 0;
  1371. destCountry < m_world->getCountries().size();
  1372. destCountry++)
  1373. {
  1374. if ( (RISK_GetOwnerOfCountry(destCountry) != this)
  1375. && (RISK_GetNumCountriesOfPlayer(RISK_GetOwnerOfCountry(destCountry)) == 1))
  1376. {
  1377. if (ComputerAttack (destCountry, true,
  1378. (nbCountriesWithAdjacentEnemies> 2)?20:2))
  1379. {
  1380. kDebug() << "Attack tempted.";
  1381. return true;
  1382. }
  1383. }
  1384. }
  1385. kDebug() << "5 Try to conquier an entire continent, attack player of other species";
  1386. /* Try to conquier an entire continent, attack player of other species */
  1387. for (int destCountry = 0;
  1388. destCountry < m_world->getCountries().size();
  1389. destCountry++)
  1390. {
  1391. if ( (RISK_GetContinentOfCountry(destCountry) == continent)
  1392. && (RISK_GetOwnerOfCountry(destCountry) != this)
  1393. && isEnemyPlayer(RISK_GetOwnerOfCountry(destCountry)))
  1394. {
  1395. if (ComputerAttack (destCountry, true,
  1396. (RISK_GetNumArmiesOfCountry(destCountry) < 5)?1:
  1397. ((nbCountriesWithAdjacentEnemies > 4)?RISK_GetNumArmiesOfCountry(destCountry):3)))
  1398. {
  1399. destCountry = 0;
  1400. kDebug() << "Attack tempted.";
  1401. return true;
  1402. }
  1403. }
  1404. }
  1405. kDebug() << "6 Try to conquier an entire continent";
  1406. /* Try to conquier an entire continent */
  1407. for (int destCountry = 0;
  1408. destCountry < m_world->getCountries().size();
  1409. destCountry++)
  1410. {
  1411. if ( (RISK_GetContinentOfCountry(destCountry) == continent)
  1412. && (RISK_GetOwnerOfCountry(destCountry) != this))
  1413. {
  1414. if (ComputerAttack (destCountry, true,
  1415. (RISK_GetNumArmiesOfCountry(destCountry) < 3)?1:50))
  1416. {
  1417. destCountry = 0;
  1418. kDebug() << "Attack tempted.";
  1419. return true;
  1420. }
  1421. }
  1422. }
  1423. kDebug() << "7 Try to attack a stronger human player for a card";
  1424. /* Try to attack a stronger human player for a card */
  1425. for (int destCountry = 0;
  1426. destCountry < m_world->getCountries().size();
  1427. destCountry++)
  1428. {
  1429. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1430. if ( (iEnemy != this) && isEnemyPlayer(iEnemy)
  1431. && (!iEnemy->isAI())
  1432. && isStrongerPlayer(RISK_GetOwnerOfCountry(destCountry)))
  1433. {
  1434. if (ComputerAttack (destCountry, false,
  1435. (RISK_GetNumArmiesOfCountry(destCountry) < 3)?1:((nbCountriesWithAdjacentEnemies > 2)?10:2)))
  1436. {
  1437. kDebug() << "Attack tempted.";
  1438. return true;
  1439. }
  1440. }
  1441. }
  1442. kDebug() << "8 Try to attack an human player for a card";
  1443. /* Try to attack an human player for a card */
  1444. for (int destCountry = 0;
  1445. destCountry < m_world->getCountries().size();
  1446. destCountry++)
  1447. {
  1448. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1449. if ( (iEnemy != this) && isEnemyPlayer(iEnemy)
  1450. && (!iEnemy->isAI()))
  1451. {
  1452. if (ComputerAttack (destCountry, false,
  1453. (RISK_GetNumArmiesOfCountry(destCountry) < 3)?1:((nbCountriesWithAdjacentEnemies > 2)?10:2)))
  1454. {
  1455. kDebug() << "Attack tempted.";
  1456. return true;
  1457. }
  1458. }
  1459. }
  1460. kDebug() << "9 Try to attack enemy player for a card";
  1461. /* Try to attack enemy player for a card */
  1462. for (int destCountry = 0;
  1463. destCountry < m_world->getCountries().size();
  1464. destCountry++)
  1465. {
  1466. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1467. if ( (iEnemy != this) && isEnemyPlayer(iEnemy))
  1468. if (ComputerAttack (destCountry, false,
  1469. (RISK_GetNumArmiesOfCountry(destCountry) < 3)?1:((nbCountriesWithAdjacentEnemies > 2)?10:2)))
  1470. {
  1471. {
  1472. kDebug() << "Attack tempted.";
  1473. return true;
  1474. }
  1475. }
  1476. }
  1477. kDebug() << "10 Try to attack for a card, attack a stronger player";
  1478. /* Try to attack for a card, attack a stronger player */
  1479. for (int destCountry = 0;
  1480. destCountry < m_world->getCountries().size();
  1481. destCountry++)
  1482. {
  1483. Player* iEnemy = RISK_GetOwnerOfCountry(destCountry);
  1484. if ( (iEnemy != this)
  1485. && isStrongerPlayer(iEnemy))
  1486. {
  1487. if (ComputerAttack (destCountry, false,
  1488. (RISK_GetNumArmiesOfCountry(destCountry) < 2)?1:100))
  1489. {
  1490. kDebug() << "Attack tempted.";
  1491. return true;
  1492. }
  1493. }
  1494. }
  1495. kDebug() << "11 Try to attack for a card";
  1496. /* Try to attack for a card */
  1497. for (int destCountry = 0;
  1498. destCountry < m_world->getCountries().size();
  1499. destCountry++)
  1500. {
  1501. if (RISK_GetOwnerOfCountry(destCountry) != this)
  1502. {
  1503. if (ComputerAttack (destCountry, false,
  1504. RISK_GetNumArmiesOfCountry(destCountry)
  1505. // (RISK_GetNumArmiesOfCountry(destCountry) < 2)?1:100
  1506. ))
  1507. {
  1508. kDebug() << "Attack tempted.";
  1509. return true;
  1510. }
  1511. }
  1512. }
  1513. kDebug() << "No attack tried";
  1514. // No attack tried
  1515. Attack_SrcCountry = -1;
  1516. Attack_DestCountry = -1;
  1517. return false;
  1518. }
  1519. bool AIColsonPlayer::Attack()
  1520. {
  1521. kDebug();
  1522. int enemyAlive;
  1523. // if (RISK_GetAttackModeOfPlayer (player) != ACTION_DOORDIE)
  1524. // RISK_SetAttackModeOfPlayer (player, ACTION_DOORDIE);
  1525. // if (RISK_GetDiceModeOfPlayer (player) != ATTACK_AUTO)
  1526. // RISK_SetDiceModeOfPlayer (player, ATTACK_AUTO);
  1527. enemyAlive = getNumEnemy();
  1528. while ((enemyAli

Large files files are truncated, but you can click here to view the full file