OF_INN2.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 : OF_INN2.CPP
  21. //Description : Firm Inn - AI functions
  22. #include <ONATION.h>
  23. #include <OINFO.h>
  24. #include <OTOWN.h>
  25. #include <OUNIT.h>
  26. #include <OF_INN.h>
  27. #include <OF_CAMP.h>
  28. //--------- Begin of function FirmInn::process_ai ---------//
  29. void FirmInn::process_ai()
  30. {
  31. if( info.game_date%30==firm_recno%30 )
  32. {
  33. if( think_del() )
  34. return;
  35. }
  36. if( info.game_date%30==firm_recno%30 )
  37. {
  38. think_hire_spy();
  39. think_hire_general();
  40. }
  41. }
  42. //----------- End of function FirmInn::process_ai -----------//
  43. //------- Begin of function FirmInn::think_del -----------//
  44. //
  45. // Think about deleting this firm.
  46. //
  47. int FirmInn::think_del()
  48. {
  49. Nation* ownNation = nation_array[nation_recno];
  50. if( ownNation->cash < 500 + 500 * ownNation->pref_cash_reserve / 100 &&
  51. ownNation->profit_365days() < 0 )
  52. {
  53. ai_del_firm();
  54. return 1;
  55. }
  56. if( ownNation->ai_inn_count > ownNation->ai_supported_inn_count()+2 ) // if the current number of inns is more than the number the nation can support plus 2, then destroy the current one
  57. {
  58. ai_del_firm();
  59. return 1;
  60. }
  61. //-------- delete it if it is near no base town ------//
  62. Town* townPtr;
  63. for( int i=town_array.size() ; i>0 ; i-- )
  64. {
  65. if( town_array.is_deleted(i) )
  66. continue;
  67. townPtr = town_array[i];
  68. if( townPtr->nation_recno == nation_recno )
  69. {
  70. if( m.points_distance( townPtr->center_x, townPtr->center_y,
  71. center_x, center_y ) <= EFFECTIVE_FIRM_TOWN_DISTANCE )
  72. {
  73. return 0;
  74. }
  75. }
  76. }
  77. ai_del_firm();
  78. return 1;
  79. }
  80. //--------- End of function FirmInn::think_del -----------//
  81. //--------- Begin of function FirmInn::think_hire_spy ---------//
  82. int FirmInn::think_hire_spy()
  83. {
  84. Nation* ownNation = nation_array[nation_recno];
  85. if( !ownNation->ai_should_spend(ownNation->pref_spy/2) )
  86. return 0;
  87. //--------------------------------------------//
  88. InnUnit* innUnit = inn_unit_array;
  89. for( int i=0 ; i<inn_unit_count ; i++, innUnit++ )
  90. {
  91. if( innUnit->skill.skill_id != SKILL_SPYING )
  92. continue;
  93. int raceId = unit_res[innUnit->unit_id]->race_id;
  94. if( think_assign_spy_to(raceId, i+1) )
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. //----------- End of function FirmInn::think_hire_spy -----------//
  100. //-------- Begin of function FirmInn::think_assign_spy_to --------//
  101. //
  102. // Think about planting spies into independent towns and enemy towns.
  103. //
  104. int FirmInn::think_assign_spy_to(int raceId, int innUnitRecno)
  105. {
  106. Town *townPtr;
  107. for( int i=town_array.size() ; i>0 ; i-- )
  108. {
  109. if( town_array.is_deleted(i) )
  110. continue;
  111. townPtr = town_array[i];
  112. if( townPtr->majority_race() != raceId )
  113. continue;
  114. if( townPtr->region_id != region_id )
  115. continue;
  116. //---- think about assign spies to independent town to lower resistance ---//
  117. if( townPtr->nation_recno == 0 )
  118. {
  119. for( int j=0 ; j<MAX_RACE ; j++ )
  120. {
  121. //--- current resistance == target resistance if we don't have any spies in the town ---//
  122. if( townPtr->race_target_resistance_array[j][nation_recno-1] ==
  123. townPtr->race_resistance_array[j][nation_recno-1] )
  124. {
  125. int unitRecno = hire(innUnitRecno);
  126. nation_array[nation_recno]->add_action( townPtr->loc_x1, townPtr->loc_y1,
  127. -1, -1, ACTION_AI_ASSIGN_SPY, townPtr->nation_recno, 1, unitRecno );
  128. return 1;
  129. }
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. //-------- End of function FirmInn::think_assign_spy_to --------//
  136. //--------- Begin of function FirmInn::think_hire_general ---------//
  137. int FirmInn::think_hire_general()
  138. {
  139. Nation* ownNation = nation_array[nation_recno];
  140. if( !ownNation->ai_should_spend(ownNation->pref_military_development/2) )
  141. return 0;
  142. //--------------------------------------------//
  143. InnUnit* innUnit = inn_unit_array;
  144. for( int i=0 ; i<inn_unit_count ; i++, innUnit++ )
  145. {
  146. if( innUnit->skill.skill_id != SKILL_LEADING )
  147. continue;
  148. int raceId = unit_res[innUnit->unit_id]->race_id;
  149. if( think_assign_general_to(raceId, i+1) )
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. //----------- End of function FirmInn::think_hire_general -----------//
  155. //-------- Begin of function FirmInn::think_assign_general_to --------//
  156. //
  157. // Think about planting spies into independent towns and enemy towns.
  158. //
  159. int FirmInn::think_assign_general_to(int raceId, int innUnitRecno)
  160. {
  161. InnUnit* innUnit = inn_unit_array+innUnitRecno-1;
  162. Nation* ownNation = nation_array[nation_recno];
  163. int curRating, bestRating=10; // the new one needs to be at least 10 points better than the existing one
  164. FirmCamp *firmCamp, *bestCamp=NULL;
  165. //----- think about which camp to move to -----//
  166. for( int i=ownNation->ai_camp_count-1 ; i>=0 ; i-- )
  167. {
  168. firmCamp = (FirmCamp*) firm_array[ ownNation->ai_camp_array[i] ];
  169. if( firmCamp->region_id != region_id )
  170. continue;
  171. int curLeadership = firmCamp->cur_commander_leadership();
  172. int newLeadership = firmCamp->new_commander_leadership(raceId, innUnit->skill.skill_level);
  173. curRating = newLeadership - curLeadership;
  174. //-------------------------------------//
  175. if( curRating > bestRating )
  176. {
  177. //--- if there is already somebody being assigned to it ---//
  178. if( ownNation->is_action_exist(firmCamp->loc_x1, firmCamp->loc_y1,
  179. -1, -1, ACTION_AI_ASSIGN_OVERSEER, FIRM_CAMP) )
  180. {
  181. continue;
  182. }
  183. bestRating = curRating;
  184. bestCamp = firmCamp;
  185. }
  186. }
  187. if( !bestCamp )
  188. return 0;
  189. //--------------------------------------------//
  190. int unitRecno = hire(innUnitRecno);
  191. ownNation->add_action(bestCamp->loc_x1, bestCamp->loc_y1, -1, -1, ACTION_AI_ASSIGN_OVERSEER, FIRM_CAMP, 1, unitRecno);
  192. return 1;
  193. }
  194. //-------- End of function FirmInn::think_assign_general_to --------//