OAI_TOWN.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_TOWN.CPP
  21. //Description: AI - processing town
  22. #include <ALL.h>
  23. #include <OUNIT.h>
  24. #include <OF_INN.h>
  25. #include <OTOWN.h>
  26. #include <OREGIONS.h>
  27. #include <ONATION.h>
  28. //--------- Begin of function Nation::think_town --------//
  29. //
  30. void Nation::think_town()
  31. {
  32. optimize_town_race();
  33. }
  34. //---------- End of function Nation::think_town --------//
  35. //--------- Begin of function Nation::optimize_town_race --------//
  36. //
  37. // Optimize the distribution of different races in different towns.
  38. //
  39. void Nation::optimize_town_race()
  40. {
  41. RegionStat* regionStat = region_array.region_stat_array;
  42. for( int i=0 ; i<region_array.region_stat_count ; i++, regionStat++ )
  43. {
  44. if( regionStat->town_nation_count_array[nation_recno-1] > 0 )
  45. optimize_town_race_region( regionStat->region_id );
  46. }
  47. }
  48. //---------- End of function Nation::optimize_town_race --------//
  49. //--------- Begin of function Nation::optimize_town_race_region --------//
  50. //
  51. // Optimize the distribution of different races in different towns in
  52. // a single region.
  53. //
  54. void Nation::optimize_town_race_region(int regionId)
  55. {
  56. //---- reckon the minority jobless pop of each race ----//
  57. int racePopArray[MAX_RACE];
  58. memset( racePopArray, 0, sizeof(racePopArray) );
  59. int i, j, majorityRace;
  60. Town* townPtr;
  61. for( i=0 ; i<ai_town_count ; i++ )
  62. {
  63. townPtr = town_array[ ai_town_array[i] ];
  64. if( townPtr->region_id != regionId )
  65. continue;
  66. majorityRace = townPtr->majority_race();
  67. for( j=0 ; j<MAX_RACE ; j++ )
  68. {
  69. if( j+1 != majorityRace )
  70. racePopArray[j] += townPtr->jobless_race_pop_array[j];
  71. }
  72. }
  73. //--- locate for towns with minority being majority and those minority race can move to ---//
  74. Town* destTown;
  75. for( int raceId=0 ; raceId<MAX_RACE ; raceId++ )
  76. {
  77. if( racePopArray[raceId-1] == 0 ) // we don't have any minority of this race
  78. continue;
  79. destTown = NULL;
  80. for( i=0 ; i<ai_town_count ; i++ )
  81. {
  82. townPtr = town_array[ ai_town_array[i] ];
  83. if( townPtr->region_id != regionId )
  84. continue;
  85. if( !townPtr->is_base_town )
  86. continue;
  87. if( townPtr->majority_race() == raceId &&
  88. townPtr->population < MAX_TOWN_POPULATION )
  89. {
  90. destTown = townPtr;
  91. break;
  92. }
  93. }
  94. if( !destTown )
  95. continue;
  96. //---- if there is a suitable town for minority to move to ---//
  97. for( i=0 ; i<ai_town_count ; i++ )
  98. {
  99. townPtr = town_array[ ai_town_array[i] ];
  100. if( townPtr->region_id != regionId )
  101. continue;
  102. //---- move minority units from towns -----//
  103. int joblessCount = townPtr->jobless_race_pop_array[raceId-1];
  104. if( joblessCount > 0 &&
  105. townPtr->majority_race() != raceId )
  106. {
  107. int migrateCount = min(8, joblessCount); // migrate a maximum of 8 units at a time
  108. add_action( destTown->loc_x1, destTown->loc_y1,
  109. townPtr->loc_x1, townPtr->loc_y1, ACTION_AI_SETTLE_TO_OTHER_TOWN, 0, migrateCount);
  110. }
  111. }
  112. }
  113. }
  114. //---------- End of function Nation::optimize_town_race_region --------//