OU_MARI2.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 : OU_MARI2.CPP
  21. //Description : Marine Units AI functions
  22. #include <OINFO.h>
  23. #include <ONATION.h>
  24. #include <OF_HARB.h>
  25. #include <OU_MARI.h>
  26. //------- Begin of function UnitMarine::init_derived --------//
  27. void UnitMarine::init_derived()
  28. {
  29. last_load_goods_date = info.game_date;
  30. }
  31. //------- End of function UnitMarine::init_derived --------//
  32. //------- Begin of function UnitMarine::process_ai --------//
  33. //
  34. void UnitMarine::process_ai()
  35. {
  36. //-- Think about removing stops whose owner nation is at war with us. --//
  37. // AI does do any sea trade
  38. // if( info.game_date%30 == sprite_recno%30 )
  39. // think_del_stop();
  40. //---- Think about setting new trade route -------//
  41. if( info.game_date%15 == sprite_recno%15 )
  42. {
  43. if( stop_defined_num < 2 && is_visible() && is_ai_all_stop() )
  44. ai_sail_to_nearby_harbor();
  45. }
  46. //------ Think about resigning this caravan -------//
  47. if( info.game_date%60 == sprite_recno%60 )
  48. think_resign();
  49. }
  50. //------- End of function UnitMarine::process_ai --------//
  51. //--------- Begin of function UnitMarine::is_ai_all_stop --------//
  52. int UnitMarine::is_ai_all_stop()
  53. {
  54. if( cur_action != SPRITE_IDLE || ai_action_id )
  55. return 0;
  56. //---- if the ship is on the beach, it's action mode is always ACTION_SHIP_TO_BEACH, so we can't check it against ACTION_STOP ---//
  57. if( action_mode2 == ACTION_SHIP_TO_BEACH )
  58. {
  59. if( in_beach && (extra_move_in_beach==NO_EXTRA_MOVE || extra_move_in_beach==EXTRA_MOVE_FINISH) )
  60. return 1;
  61. }
  62. return action_mode==ACTION_STOP && action_mode2==ACTION_STOP;
  63. }
  64. //---------- End of function UnitMarine::is_ai_all_stop --------//
  65. //------- Begin of function UnitMarine::think_resign --------//
  66. int UnitMarine::think_resign()
  67. {
  68. //---- only resign when the ship has stopped ----//
  69. if( !is_ai_all_stop() )
  70. return 0;
  71. //--- retire this ship if we have better ship technology available ---//
  72. if( unit_id == UNIT_TRANSPORT )
  73. {
  74. if( unit_res[UNIT_CARAVEL]->get_nation_tech_level(nation_recno) > 0 ||
  75. unit_res[UNIT_GALLEON]->get_nation_tech_level(nation_recno) > 0 )
  76. {
  77. if( !nation_array[nation_recno]->ai_is_sea_travel_safe() )
  78. {
  79. resign(COMMAND_AI);
  80. return 1;
  81. }
  82. }
  83. }
  84. return 0;
  85. }
  86. //------- End of function UnitMarine::think_resign --------//
  87. //------- Begin of function UnitMarine::think_del_stop --------//
  88. //
  89. // Think about removing stops whose owner nation is at war with us.
  90. //
  91. void UnitMarine::think_del_stop()
  92. {
  93. if( !is_visible() ) // cannot del stop if the caravan is inside a market place.
  94. return;
  95. Nation* nationPtr = nation_array[nation_recno];
  96. for( int i=stop_defined_num ; i>0 ; i-- )
  97. {
  98. if( firm_array.is_deleted(stop_array[i-1].firm_recno) )
  99. {
  100. del_stop(i, COMMAND_AI);
  101. continue;
  102. }
  103. //----------------------------------------------//
  104. int nationRecno = firm_array[ stop_array[i-1].firm_recno ]->nation_recno;
  105. if( nationPtr->get_relation_status(nationRecno) == NATION_HOSTILE )
  106. {
  107. del_stop(i, COMMAND_AI);
  108. }
  109. }
  110. }
  111. //------- End of function UnitMarine::think_del_stop --------//
  112. //--------- Begin of function UnitMarine::ai_sail_to_nearby_harbor --------//
  113. void UnitMarine::ai_sail_to_nearby_harbor()
  114. {
  115. Nation *ownNation = nation_array[nation_recno];
  116. FirmHarbor *firmHarbor, *bestHarbor=NULL;
  117. int curRating, bestRating=0;
  118. int curXLoc=cur_x_loc(), curYLoc=cur_y_loc();
  119. int curRegionId=region_id();
  120. for( int i=0 ; i<ownNation->ai_harbor_count ; i++ )
  121. {
  122. firmHarbor = (FirmHarbor*) firm_array[ ownNation->ai_harbor_array[i] ];
  123. if( firmHarbor->sea_region_id != curRegionId )
  124. continue;
  125. curRating = world.distance_rating( curXLoc, curYLoc, firmHarbor->center_x, firmHarbor->center_y );
  126. curRating += (MAX_SHIP_IN_HARBOR - firmHarbor->ship_count) * 100;
  127. if( curRating > bestRating )
  128. {
  129. bestRating = curRating;
  130. bestHarbor = firmHarbor;
  131. }
  132. }
  133. if( bestHarbor )
  134. assign(bestHarbor->loc_x1, bestHarbor->loc_y1);
  135. }
  136. //---------- End of function UnitMarine::ai_sail_to_nearby_harbor --------//
  137. //--------- Begin of function UnitMarine::ai_ship_being_attacked --------//
  138. //
  139. // This function is called by Unit::hit_target() when the king is
  140. // under attack.
  141. //
  142. // <int> attackerUnitRecno - recno of the attacker unit.
  143. //
  144. void UnitMarine::ai_ship_being_attacked(int attackerUnitRecno)
  145. {
  146. Unit* attackerUnit = unit_array[attackerUnitRecno];
  147. if( attackerUnit->nation_recno == nation_recno ) // this can happen when the unit has just changed nation
  148. return;
  149. if( info.game_date%5 == sprite_recno%5 )
  150. {
  151. nation_array[nation_recno]->ai_sea_attack_target( attackerUnit->next_x_loc(), attackerUnit->next_y_loc() );
  152. }
  153. }
  154. //---------- End of function UnitMarine::ai_ship_being_attacked --------//