OAI_DEFE.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : OAI_DEFE.CPP
  21. //Description: AI on defense
  22. #include <ALL.h>
  23. #include <OTALKRES.h>
  24. #include <ONATION.h>
  25. //----- Begin of function Nation::ai_defend -----//
  26. //
  27. // <int> attackerUnitRecno - unit recno of the attacker.
  28. //
  29. int Nation::ai_defend(int attackerUnitRecno)
  30. {
  31. //--- don't call for defense too frequently, only call once 7 days (since this function will be called every time our king/firm/town is attacked, so this filtering is necessary ---//
  32. if( info.game_date < ai_last_defend_action_date+7 )
  33. return 0;
  34. ai_last_defend_action_date = info.game_date;
  35. //---------- analyse the situation first -----------//
  36. Unit* attackerUnit = unit_array[attackerUnitRecno];
  37. err_when( attackerUnit->nation_recno == nation_recno );
  38. int attackerXLoc = attackerUnit->next_x_loc();
  39. int attackerYLoc = attackerUnit->next_y_loc();
  40. int hasWar;
  41. int enemyCombatLevel = mobile_defense_combat_level( attackerXLoc, attackerYLoc,
  42. attackerUnit->nation_recno, 0, hasWar ); // 0-don't return immediately even if there is war around this town
  43. //-- the value returned is enemy strength minus your own strength, so if it's positive, it means that your enemy is stronger than you, otherwise you're stronger than your enemy --//
  44. int attackCombatLevel = ai_attack_target(attackerXLoc, attackerYLoc, enemyCombatLevel, 1); // 1-defense mode
  45. //------ request military aid from allies ----//
  46. if( attackCombatLevel < enemyCombatLevel && attackerUnit->nation_recno )
  47. {
  48. ai_request_military_aid();
  49. }
  50. return 1;
  51. }
  52. //----- End of function Nation::ai_defend -----//
  53. //----- Begin of function Nation::ai_request_military_aid -----//
  54. //
  55. // Request allied nations to provide immediate military aid.
  56. //
  57. int Nation::ai_request_military_aid()
  58. {
  59. return 0; //**BUGHERE, multiplayer sync error, disabled temporarily
  60. for( int i=nation_array.size() ; i>0 ; i-- )
  61. {
  62. if( nation_array.is_deleted(i) )
  63. continue;
  64. if( get_relation(i)->status != NATION_ALLIANCE )
  65. continue;
  66. if( should_diplomacy_retry(TALK_REQUEST_MILITARY_AID, i) )
  67. {
  68. talk_res.ai_send_talk_msg(i, nation_recno, TALK_REQUEST_MILITARY_AID);
  69. return 1;
  70. }
  71. }
  72. return 0;
  73. }
  74. //----- End of function Nation::ai_request_military_aid -----//