OUNITIND.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 : OUNITIND.CPP
  21. //Description : Object independent Unit AI
  22. #include <OSYS.h>
  23. #include <OSPY.h>
  24. #include <OREBEL.h>
  25. #include <OUNIT.h>
  26. #include <OCONFIG.h>
  27. #include <OF_CAMP.h>
  28. #include <ONATION.h>
  29. //--------- Begin of function Unit::think_independent_unit --------//
  30. //
  31. // Think about the action of an independent unit. It first tries to
  32. // settle to a town. If not successful, it will disband itself.
  33. //
  34. void Unit::think_independent_unit()
  35. {
  36. if( !is_ai_all_stop() )
  37. return;
  38. //--- don't process if it's a spy and the notify cloak flag is on ---//
  39. if( spy_recno )
  40. {
  41. //---------------------------------------------//
  42. //
  43. // If notify_cloaked_nation_flag is 0, the AI
  44. // won't control the unit.
  45. //
  46. // If notify_cloaked_nation_flag is 1, the AI
  47. // will control the unit. But not immediately,
  48. // it will do it once 5 days so the player can
  49. // have a chance to select the unit and set its
  50. // notify_cloaked_nation_flag back to 0 if the
  51. // player wants.
  52. //
  53. //---------------------------------------------//
  54. if( spy_array[spy_recno]->notify_cloaked_nation_flag==0 )
  55. return;
  56. if( info.game_date%5 != sprite_recno%5 )
  57. return;
  58. }
  59. //-------- if this is a rebel ----------//
  60. if( unit_mode == UNIT_MODE_REBEL )
  61. {
  62. Rebel* rebelPtr = rebel_array[unit_mode_para];
  63. //--- if the group this rebel belongs to already has a rebel town, assign to it now ---//
  64. if( rebelPtr->town_recno )
  65. {
  66. if( !town_array.is_deleted(rebelPtr->town_recno) )
  67. {
  68. Town* townPtr = town_array[rebelPtr->town_recno];
  69. err_when( townPtr->rebel_recno != rebelPtr->rebel_recno );
  70. assign(townPtr->loc_x1, townPtr->loc_y1);
  71. }
  72. return; // don't do anything if the town has been destroyed, Rebel::next_day() will take care of it.
  73. }
  74. }
  75. //---- look for towns to assign to -----//
  76. Town *townPtr, *bestTown=NULL;
  77. int regionId = world.get_region_id( next_x_loc(), next_y_loc() );
  78. int curRating, bestRating=0;
  79. int curXLoc = next_x_loc(), curYLoc = next_y_loc();
  80. for( int i=town_array.size() ; i>0 ; i-- )
  81. {
  82. if( town_array.is_deleted(i) )
  83. continue;
  84. townPtr = town_array[i];
  85. if( townPtr->nation_recno ||
  86. townPtr->population >= MAX_TOWN_POPULATION ||
  87. townPtr->region_id != regionId )
  88. {
  89. continue;
  90. }
  91. //-------------------------------------//
  92. curRating = world.distance_rating(curXLoc, curYLoc,
  93. townPtr->center_x, townPtr->center_y );
  94. curRating += 100 * townPtr->race_pop_array[race_id-1] / townPtr->population;
  95. //-------------------------------------//
  96. if( curRating > bestRating )
  97. {
  98. bestRating = curRating;
  99. bestTown = townPtr;
  100. }
  101. }
  102. if( bestTown )
  103. {
  104. err_when( unit_mode==UNIT_MODE_REBEL && rebel_array[unit_mode_para]->town_recno &&
  105. rebel_array[unit_mode_para]->town_recno != bestTown->town_recno );
  106. //--- drop its rebel identity and becomes a normal unit if he decides to settle to a town ---//
  107. if( unit_mode == UNIT_MODE_REBEL )
  108. rebel_array.drop_rebel_identity(sprite_recno);
  109. assign(bestTown->loc_x1, bestTown->loc_y1);
  110. }
  111. else
  112. resign(COMMAND_AI);
  113. }
  114. //---------- End of function Unit::think_independent_unit --------//