OREGION.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 : OREGION.CPP
  21. // Description : Class RegionArray , info on regions
  22. #include <stdlib.h>
  23. #include <OINFO.h>
  24. #include <OREGION.h>
  25. #include <OREGIONS.h>
  26. //--------- Define static function ---------//
  27. static int sort_region_function( const void *a, const void *b );
  28. // --------- Begin of function RegionArray::RegionArray -------//
  29. RegionArray::RegionArray()
  30. {
  31. init_flag = 0;
  32. }
  33. // --------- End of function RegionArray::RegionArray -------//
  34. // --------- Begin of function RegionArray::~RegionArray -------//
  35. RegionArray::~RegionArray()
  36. {
  37. deinit();
  38. }
  39. // --------- End of function RegionArray::~RegionArray -------//
  40. // --------- Begin of function RegionArray::init -------//
  41. void RegionArray::init(int maxRegion)
  42. {
  43. deinit();
  44. region_info_count = maxRegion;
  45. int connectBit;
  46. if( maxRegion > 0)
  47. {
  48. // --------- allocate memory for RegionInfo --------//
  49. region_info_array = (RegionInfo *)mem_add( sizeof(RegionInfo) * maxRegion);
  50. memset(region_info_array, 0, sizeof(RegionInfo) * maxRegion );
  51. // ---- calculate the no. of bit required to store connection ----//
  52. connectBit = (maxRegion-1) * (maxRegion) /2;
  53. // region 1 needs 0 bit
  54. // region 2 needs 1 bit
  55. // region 3 needs 2 bits
  56. // region 4 needs 3 bits...
  57. }
  58. else
  59. {
  60. region_info_array = NULL;
  61. connectBit = 0;
  62. }
  63. if( connectBit > 0)
  64. {
  65. connect_bits = (unsigned char *) mem_add( (connectBit + 7) / 8 );
  66. memset(connect_bits, 0, (connectBit + 7) /8 );
  67. }
  68. else
  69. {
  70. connect_bits = NULL;
  71. }
  72. //------ initialize adj_offset_bit and area -------//
  73. int j = 0;
  74. for(int i=0 ; i<maxRegion ; i++)
  75. {
  76. region_info_array[i].region_id = i+1;
  77. region_info_array[i].adj_offset_bit = j;
  78. region_info_array[i].region_size = 0;
  79. j += i; // j += regionId-1;
  80. }
  81. err_when(j != connectBit );
  82. //------------------------------------------------//
  83. init_flag = 1;
  84. }
  85. // --------- End of function RegionArray::init -------//
  86. // --------- Begin of function RegionArray::deinit -------//
  87. void RegionArray::deinit()
  88. {
  89. if( init_flag )
  90. {
  91. if(region_info_array)
  92. mem_del( region_info_array );
  93. if(region_stat_array)
  94. mem_del( region_stat_array );
  95. if(connect_bits)
  96. mem_del( connect_bits );
  97. init_flag = 0;
  98. }
  99. }
  100. // --------- End of function RegionArray::deinit -------//
  101. //--------- Begin of function RegionArray::next_day -------//
  102. void RegionArray::next_day()
  103. {
  104. if( info.game_date%7 == 0 )
  105. update_region_stat();
  106. }
  107. //--------- End of function RegionArray::next_day -------//
  108. //--------- Begin of function RegionArray::set_region -------//
  109. void RegionArray::set_region(int reg, RegionType regType)
  110. {
  111. err_when( reg <= 0 || reg > region_info_count);
  112. region_info_array[reg-1].region_type = regType;
  113. }
  114. //--------- End of function RegionArray::set_region -------//
  115. //--------- Begin of function RegionArray::inc_size -------//
  116. void RegionArray::inc_size(int reg)
  117. {
  118. err_when( reg <= 0 || reg > region_info_count);
  119. region_info_array[reg-1].region_size++;
  120. }
  121. //--------- End of function RegionArray::inc_size -------//
  122. //--------- Begin of function RegionArray::set_adjacent -------//
  123. void RegionArray::set_adjacent(int reg1, int reg2)
  124. {
  125. err_when( reg1 < 0 || reg1 > region_info_count);
  126. err_when( reg2 < 0 || reg2 > region_info_count);
  127. if( reg1 == 0 || reg2 == 0)
  128. return;
  129. int bitOffset;
  130. if( reg1 == reg2 )
  131. return;
  132. if( reg1 > reg2)
  133. {
  134. bitOffset = region_info_array[reg1 -1].adj_offset_bit + (reg2 -1);
  135. }
  136. else
  137. {
  138. bitOffset = region_info_array[reg2 -1].adj_offset_bit + (reg1 -1);
  139. }
  140. connect_bits[bitOffset / 8] |= 1 << (bitOffset % 8);
  141. }
  142. //--------- End of function RegionArray::set_adjacent -------//
  143. //--------- Begin of function RegionArray::is_adjacent -------//
  144. int RegionArray::is_adjacent(int reg1, int reg2)
  145. {
  146. err_when( reg1 <= 0 || reg1 > region_info_count);
  147. err_when( reg2 <= 0 || reg2 > region_info_count);
  148. int bitOffset;
  149. if( reg1 == reg2 )
  150. return TRUE;
  151. if( reg1 > reg2 )
  152. {
  153. bitOffset = region_info_array[reg1 -1].adj_offset_bit + (reg2 -1);
  154. }
  155. else
  156. {
  157. bitOffset = region_info_array[reg2 -1].adj_offset_bit + (reg1 -1);
  158. }
  159. return connect_bits[bitOffset / 8] & (1 << (bitOffset % 8));
  160. }
  161. //--------- End of function RegionArray::is_adjacent -------//
  162. //--------- Begin of function RegionArray::sort_region -------//
  163. void RegionArray::sort_region()
  164. {
  165. //--- initialize the region_sorted_array first ----//
  166. for( int i=0 ; i<region_info_count ; i++ )
  167. region_sorted_array[i] = i+1;
  168. //----------- sort it now -----------//
  169. qsort( region_sorted_array, region_info_count, sizeof(region_sorted_array[0]), sort_region_function );
  170. }
  171. //--------- End of function RegionArray::sort_region -------//
  172. //------ Begin of function sort_region_function ------//
  173. //
  174. static int sort_region_function( const void *a, const void *b )
  175. {
  176. return region_array[*((BYTE*)b)]->region_size - region_array[*((BYTE*)a)]->region_size;
  177. }
  178. //------- End of function sort_region_function ------//
  179. #ifdef DEBUG
  180. //--------- Begin of function RegionArray::get_sorted_region -------//
  181. RegionInfo* RegionArray::get_sorted_region(int recNo)
  182. {
  183. err_when( recNo<1 || recNo>region_info_count );
  184. return operator[]( region_sorted_array[recNo-1] );
  185. }
  186. //--------- End of function RegionArray::get_sorted_region -------//
  187. //--------- Begin of function RegionArray::get_region_stat -------//
  188. RegionStat* RegionArray::get_region_stat(int regionId)
  189. {
  190. err_when( regionId<1 || regionId>region_info_count );
  191. int regionStatId = region_info_array[regionId-1].region_stat_id;
  192. err_when( regionStatId<1 || regionStatId>region_stat_count );
  193. return region_stat_array+regionStatId-1;
  194. }
  195. //--------- End of function RegionArray::get_region_stat -------//
  196. //--------- Begin of function RegionArray::get_region_stat2 -------//
  197. RegionStat* RegionArray::get_region_stat2(int regionStatId)
  198. {
  199. err_when( regionStatId<1 || regionStatId>region_stat_count );
  200. return region_stat_array+regionStatId-1;
  201. }
  202. //--------- End of function RegionArray::get_region_stat2 -------//
  203. // --------- Begin of function RegionArray::region_type -------//
  204. RegionType RegionArray::region_type(int region)
  205. {
  206. err_when(region <= 0 || region > region_info_count);
  207. return region_info_array[region-1].region_type;
  208. }
  209. // --------- End of function RegionArray::region_type -------//
  210. //--------- Begin of function RegionArray::operator[] -------//
  211. RegionInfo *RegionArray::operator[](int region)
  212. {
  213. err_when(region <= 0 || region > region_info_count);
  214. return region_info_array+region-1;
  215. }
  216. //--------- End of function RegionArray::operator[] -------//
  217. #endif