OTOWNA.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 : OTOWNA.CPP
  21. //Description : Object Town Array
  22. #include <stdlib.h>
  23. #include <OBOX.h>
  24. #include <OWORLD.h>
  25. #include <OINFO.h>
  26. #include <OCONFIG.h>
  27. #include <OSYS.h>
  28. #include <OTOWN.h>
  29. #include <OF_MARK.h>
  30. #include <OF_MONS.h>
  31. #include <ONATION.h>
  32. #include <OGAME.h>
  33. #ifdef DEBUG
  34. #include <OFONT.h>
  35. //### begin alex 20/9 ###//
  36. static unsigned long last_town_ai_profile_time = 0L;
  37. static unsigned long town_ai_profile_time = 0L;
  38. static unsigned long last_town_profile_time = 0L;
  39. static unsigned long town_profile_time = 0L;
  40. //#### end alex 20/9 ####//
  41. #endif
  42. //--------- Begin of function TownArray::TownArray ----------//
  43. TownArray::TownArray() : DynArrayB(sizeof(Town*), 10, DEFAULT_REUSE_INTERVAL_DAYS)
  44. {
  45. }
  46. //--------- End of function TownArray::TownArary ----------//
  47. //------- Begin of function TownArray::~TownArray ----------//
  48. //
  49. TownArray::~TownArray()
  50. {
  51. deinit();
  52. }
  53. //--------- End of function TownArray::~TownArray ----------//
  54. //--------- Begin of function TownArray::init ----------//
  55. //
  56. void TownArray::init()
  57. {
  58. memset( race_wander_pop_array, 0, sizeof(race_wander_pop_array) );
  59. }
  60. //---------- End of function TownArray::init ----------//
  61. //--------- Begin of function TownArray::deinit ----------//
  62. //
  63. void TownArray::deinit()
  64. {
  65. //----- delete Town objects ------//
  66. if( size() > 0 )
  67. {
  68. Town* townPtr;
  69. for( int i=1 ; i<=size() ; i++ )
  70. {
  71. townPtr = (Town*) get_ptr(i);
  72. if( townPtr )
  73. delete townPtr;
  74. }
  75. zap();
  76. }
  77. }
  78. //---------- End of function TownArray::deinit ----------//
  79. //----- Begin of function TownArray::create_town -------//
  80. //
  81. // Create a blank town object and link it into town_array.
  82. //
  83. Town* TownArray::create_town()
  84. {
  85. Town* townPtr;
  86. townPtr = new Town;
  87. linkin(&townPtr);
  88. return townPtr;
  89. }
  90. //------- End of function TownArray::create_town -------//
  91. //----- Begin of function TownArray::add_town -------//
  92. //
  93. // <int> nationRecno - the nation recno
  94. // <int> raceId - the race of the majority of the town poulation
  95. // <int> xLoc, yLoc - location of the town
  96. //
  97. int TownArray::add_town(int nationRecno, int raceId, int xLoc, int yLoc)
  98. {
  99. Town* townPtr;
  100. townPtr = new Town;
  101. linkin(&townPtr);
  102. townPtr->town_recno = recno();
  103. townPtr->init(nationRecno, raceId, xLoc, yLoc);
  104. nation_array.update_statistic(); // update largest_town_recno
  105. return recno();
  106. }
  107. //------- End of function TownArray::add_town -------//
  108. //----- Begin of function TownArray::del_town -------//
  109. //
  110. void TownArray::del_town(int townRecno)
  111. {
  112. Town* townPtr = operator[](townRecno);
  113. townPtr->deinit();
  114. delete townPtr;
  115. linkout(townRecno);
  116. nation_array.update_statistic();
  117. }
  118. //------- End of function TownArray::del_town -------//
  119. //--------- Begin of function TownArray::process ---------//
  120. //
  121. // Process all town in town_array for action and movement for next frame
  122. //
  123. void TownArray::process()
  124. {
  125. //----- call Town::next_day --------//
  126. int i;
  127. Town *townPtr;
  128. for( i=size() ; i>0 ; i-- )
  129. {
  130. townPtr = (Town*) get_ptr(i);
  131. if( !townPtr )
  132. continue;
  133. //------- if all the population are gone --------//
  134. if( townPtr->population==0 )
  135. {
  136. del_town(i);
  137. continue;
  138. }
  139. //-------------------------------//
  140. err_when(town_array.is_deleted(i));
  141. err_when( townPtr->town_recno==0 );
  142. if( i%FRAMES_PER_DAY == int(sys.frame_count%FRAMES_PER_DAY) ) // only process each firm once per day
  143. {
  144. err_when( townPtr->town_recno==0 );
  145. if( townPtr->nation_recno==0 )
  146. {
  147. townPtr->think_independent_town();
  148. }
  149. else
  150. {
  151. #ifdef DEBUG
  152. if(!config.disable_ai_flag && townPtr->ai_town)
  153. #else
  154. if( townPtr->ai_town )
  155. #endif
  156. {
  157. #ifdef DEBUG
  158. unsigned long profileStartTime = m.get_time();
  159. #endif
  160. townPtr->process_ai();
  161. #ifdef DEBUG
  162. town_profile_time += m.get_time() - profileStartTime;
  163. #endif
  164. }
  165. }
  166. if( town_array.is_deleted(i) )
  167. continue;
  168. err_when( townPtr->town_recno==0 );
  169. //### begin alex 20/9 ###//
  170. #ifdef DEBUG
  171. unsigned long profileStartTime = m.get_time();
  172. #endif
  173. townPtr->next_day();
  174. #ifdef DEBUG
  175. town_profile_time += m.get_time() - profileStartTime;
  176. #endif
  177. //#### end alex 20/9 ####//
  178. }
  179. }
  180. //------ distribute demand -------//
  181. if( sys.day_frame_count==0 && info.game_date%15==0 ) // distribute demand every 15 days
  182. distribute_demand();
  183. //------ create new independent town -----//
  184. if( info.game_date%30==0 && sys.frame_count%FRAMES_PER_DAY==0 )
  185. think_new_independent_town();
  186. }
  187. //----------- End of function TownArray::process ---------//
  188. //----- Begin of function TownArray::think_new_independent_town -----//
  189. //
  190. // Think about creating new independent towns.
  191. //
  192. void TownArray::think_new_independent_town()
  193. {
  194. if( m.random(3) != 0 ) // 1/3 chance
  195. return;
  196. //---- count the number of independent towns ----//
  197. Town* townPtr;
  198. int independentTownCount=0, allTotalPop=0;
  199. int i;
  200. for( i=town_array.size() ; i>0 ; i-- )
  201. {
  202. if( town_array.is_deleted(i) )
  203. continue;
  204. townPtr = town_array[i];
  205. allTotalPop += townPtr->population;
  206. if( townPtr->nation_recno == 0 )
  207. independentTownCount++;
  208. }
  209. if( independentTownCount >= 10 ) // only when the no. of independent town is less than 10
  210. return;
  211. //--- if the total population of all nations combined > 1000, then no new independent town will emerge ---//
  212. if( allTotalPop > 1000 )
  213. return;
  214. //--- add 1 to 2 wanderer per month per race ---//
  215. for( i=0 ; i<MAX_RACE ; i++ )
  216. {
  217. race_wander_pop_array[i] += 2+m.random(5);
  218. }
  219. //----- check if there are enough wanderers to set up a new town ---//
  220. int raceId = m.random(MAX_RACE)+1;
  221. for( i=0 ; i<MAX_RACE ; i++ )
  222. {
  223. if( ++raceId > MAX_RACE )
  224. raceId = 1;
  225. if( race_wander_pop_array[raceId-1] >= 10 ) // one of the race must have at least 10 people
  226. break;
  227. }
  228. if( i==MAX_RACE )
  229. return;
  230. //------- locate for a space to build the town ------//
  231. int xLoc, yLoc;
  232. if( !think_town_loc(MAX_WORLD_X_LOC*MAX_WORLD_Y_LOC/4, xLoc, yLoc) )
  233. return;
  234. //--------------- create town ---------------//
  235. int townRecno = town_array.add_town(0, raceId, xLoc, yLoc);
  236. int maxTownPop = 20 + m.random(10);
  237. int addPop, townResistance;
  238. int loopCount=0;
  239. townPtr = town_array[townRecno];
  240. while(1)
  241. {
  242. err_when( loopCount++ > 100 );
  243. addPop = race_wander_pop_array[raceId-1];
  244. addPop = min(maxTownPop-townPtr->population, addPop);
  245. townResistance = independent_town_resistance();
  246. townPtr->init_pop( raceId, addPop, townResistance, 0, 1 ); // 0-the add pop do not have jobs, 1-first init
  247. race_wander_pop_array[raceId-1] -= addPop;
  248. err_when( race_wander_pop_array[raceId-1] < 0 );
  249. if( townPtr->population >= maxTownPop )
  250. break;
  251. //---- next race to be added to the independent town ----//
  252. raceId = m.random(MAX_RACE)+1;
  253. for( i=0 ; i<MAX_RACE ; i++ )
  254. {
  255. if( ++raceId > MAX_RACE )
  256. raceId = 1;
  257. if( race_wander_pop_array[raceId-1] >= 5 )
  258. break;
  259. }
  260. if( i==MAX_RACE ) // no suitable race
  261. break;
  262. }
  263. //---------- set town layout -----------//
  264. townPtr->auto_set_layout();
  265. // if( sys.debug_session )
  266. // box.msg( "A new independent town has emerged." );
  267. }
  268. //------ End of function TownArray::think_new_independent_town -----//
  269. //----- Begin of function TownArray::independent_town_resistance ------//
  270. //
  271. // Return a random resistance value for new independent town.
  272. //
  273. int TownArray::independent_town_resistance()
  274. {
  275. switch(config.independent_town_resistance)
  276. {
  277. case OPTION_LOW:
  278. return 40 + m.random(20);
  279. break;
  280. case OPTION_MODERATE:
  281. return 50 + m.random(30);
  282. break;
  283. case OPTION_HIGH:
  284. return 60 + m.random(40);
  285. break;
  286. default:
  287. err_here();
  288. return 60 + m.random(40);
  289. }
  290. }
  291. //----- End of function TownArray::independent_town_resistance ------//
  292. //-------- Begin of function TownArray::think_town_loc --------//
  293. //
  294. // Locate for an area of free space
  295. //
  296. // <int> maxTries = maximum no. of tries
  297. // <int&> xLoc = for returning result
  298. // <int&> yLoc = for returning result
  299. //
  300. // return : <int> 1 - free space found
  301. // 0 - free space found
  302. //
  303. int TownArray::think_town_loc(int maxTries, int& xLoc, int& yLoc)
  304. {
  305. #define MIN_INTER_TOWN_DISTANCE 16
  306. #define BUILD_TOWN_LOC_WIDTH 16
  307. #define BUILD_TOWN_LOC_HEIGHT 16
  308. int i, x, y, canBuildFlag, townRecno, firmRecno;
  309. Location* locPtr;
  310. Town* townPtr;
  311. Firm* firmPtr;
  312. for( i=0 ; i<maxTries ; i++ )
  313. {
  314. xLoc = m.random(MAX_WORLD_X_LOC-BUILD_TOWN_LOC_WIDTH);
  315. yLoc = 2+m.random(MAX_WORLD_Y_LOC-BUILD_TOWN_LOC_HEIGHT-2); // do not build on the upper most location as the flag will go beyond the view area
  316. canBuildFlag=1;
  317. //---------- check if the area is all free ----------//
  318. for( y=yLoc ; y<yLoc+BUILD_TOWN_LOC_HEIGHT ; y++ )
  319. {
  320. locPtr = world.get_loc(xLoc, y);
  321. for( x=xLoc ; x<xLoc+BUILD_TOWN_LOC_WIDTH ; x++, locPtr++ )
  322. {
  323. if( !locPtr->can_build_town() )
  324. {
  325. canBuildFlag=0;
  326. break;
  327. }
  328. }
  329. }
  330. if( !canBuildFlag )
  331. continue;
  332. //-------- check if it's too close to other towns --------//
  333. for( townRecno=town_array.size() ; townRecno>0 ; townRecno-- )
  334. {
  335. if( town_array.is_deleted(townRecno) )
  336. continue;
  337. townPtr = town_array[townRecno];
  338. if( m.points_distance(xLoc+1, yLoc+1, townPtr->center_x, // xLoc+1 and yLoc+1 to take the center location of the town
  339. townPtr->center_y) < MIN_INTER_TOWN_DISTANCE )
  340. {
  341. break;
  342. }
  343. }
  344. if( townRecno > 0 ) // if it's too close to other towns
  345. continue;
  346. //-------- check if it's too close to monster firms --------//
  347. for( firmRecno=firm_array.size() ; firmRecno>0 ; firmRecno-- )
  348. {
  349. if( firm_array.is_deleted(firmRecno) )
  350. continue;
  351. firmPtr = firm_array[firmRecno];
  352. if( m.points_distance(xLoc+1, yLoc+1, firmPtr->center_x,
  353. firmPtr->center_y) < MONSTER_ATTACK_NEIGHBOR_RANGE )
  354. {
  355. break;
  356. }
  357. }
  358. if( firmRecno > 0 ) // if it's too close to monster firms
  359. continue;
  360. //----------------------------------------//
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. //--------- End of function TownArray::think_town_loc ---------//
  366. //--------- Begin of function TownArray::draw ---------//
  367. //
  368. // Draw town towns on the zoom map.
  369. //
  370. void TownArray::draw()
  371. {
  372. int i;
  373. Town *townPtr;
  374. for( i=size() ; i>0 ; i-- )
  375. {
  376. townPtr = (Town*) get_ptr(i);
  377. if( townPtr )
  378. townPtr->draw();
  379. }
  380. }
  381. //----------- End of function TownArray::draw ---------//
  382. //--------- Begin of function TownArray::draw_dot ---------//
  383. //
  384. // Draw tiny dots on map window representing the location of the town town.
  385. //
  386. void TownArray::draw_dot()
  387. {
  388. char* vgaBufPtr = vga_back.buf_ptr();
  389. char* writePtr;
  390. int i, x, y;
  391. Town* townPtr;
  392. TownLayout* townLayout;
  393. char nationColor;
  394. char* nationColorArray = nation_array.nation_color_array;
  395. int vgaBufPitch = vga_back.buf_pitch();
  396. // ##### begin Gilbert 16/8 #######//
  397. const unsigned int excitedColorCount = 4;
  398. char excitedColorArray[MAX_NATION+1][excitedColorCount];
  399. for( i = 0; i <= MAX_NATION; ++i )
  400. {
  401. if( i == 0 || !nation_array.is_deleted(i) )
  402. {
  403. char *remapTable = game.get_color_remap_table(i, 0);
  404. excitedColorArray[i][0] = remapTable[0xe0];
  405. excitedColorArray[i][1] = remapTable[0xe1];
  406. excitedColorArray[i][2] = remapTable[0xe2];
  407. excitedColorArray[i][3] = remapTable[0xe3];
  408. }
  409. else
  410. {
  411. excitedColorArray[i][0] =
  412. excitedColorArray[i][1] =
  413. excitedColorArray[i][2] =
  414. excitedColorArray[i][3] = (char) V_WHITE;
  415. }
  416. }
  417. // ##### end Gilbert 16/8 #######//
  418. for(i=1; i <=size() ; i++)
  419. {
  420. townPtr = (Town*) get_ptr(i);
  421. if( !townPtr)
  422. continue;
  423. nationColor = info.game_date - townPtr->last_being_attacked_date > 2 ?
  424. nationColorArray[townPtr->nation_recno] :
  425. excitedColorArray[townPtr->nation_recno][sys.frame_count % excitedColorCount];
  426. townLayout = town_res.get_layout(townPtr->layout_id);
  427. writePtr = vgaBufPtr + (MAP_Y1+townPtr->loc_y1)*vgaBufPitch + (MAP_X1+townPtr->loc_x1);
  428. char shadowColor = (char) VGA_GRAY;
  429. for( y=STD_TOWN_LOC_HEIGHT ; y>0 ; y--, writePtr+=vgaBufPitch-STD_TOWN_LOC_WIDTH )
  430. {
  431. for( x=STD_TOWN_LOC_WIDTH ; x>0 ; x--, writePtr++ )
  432. {
  433. if( *writePtr != UNEXPLORED_COLOR )
  434. *writePtr = nationColor;
  435. }
  436. if( *(writePtr+vgaBufPitch) != UNEXPLORED_COLOR)
  437. *(writePtr+vgaBufPitch) = shadowColor;
  438. }
  439. for( x = STD_TOWN_LOC_WIDTH ; x>0; x--)
  440. {
  441. if( *(++writePtr) != UNEXPLORED_COLOR )
  442. *writePtr = shadowColor;
  443. }
  444. }
  445. }
  446. //----------- End of function TownArray::draw_dot -----------//
  447. //--------- Begin of function TownArray::draw_profile ---------//
  448. void TownArray::draw_profile()
  449. {
  450. #ifdef DEBUG
  451. static unsigned long lastDrawTime = m.get_time();
  452. //### begin alex 20/9 ###//
  453. if(m.get_time() >= lastDrawTime + 1000)
  454. {
  455. last_town_ai_profile_time = town_ai_profile_time;
  456. town_ai_profile_time = 0L;
  457. last_town_profile_time = town_profile_time;
  458. town_profile_time = 0L;
  459. lastDrawTime = m.get_time();
  460. }
  461. String str;
  462. str = "Town : ";
  463. font_news.disp( ZOOM_X1+10, ZOOM_Y1+90, str, MAP_X2);
  464. str = "";
  465. str += last_town_ai_profile_time;
  466. font_news.disp( ZOOM_X1+60, ZOOM_Y1+90, str, MAP_X2);
  467. str = "";
  468. str += last_town_profile_time;
  469. font_news.disp( ZOOM_X1+100, ZOOM_Y1+90, str, MAP_X2);
  470. //#### end alex 20/9 ####//
  471. #endif
  472. }
  473. //----------- End of function TownArray::draw_profile -----------//
  474. //--------- Begin of function TownArray::find_nearest_town --------//
  475. //
  476. // Find the town that is nearest to the firm.
  477. //
  478. // <int> xLoc, yLoc = the x and y location of the town
  479. // [int] nationRecno = nation recno of the town town
  480. // (default: 0, any nation)
  481. //
  482. // return : <int> town town recno of where the firm is nearest to
  483. //
  484. int TownArray::find_nearest_town(int xLoc, int yLoc, int nationRecno)
  485. {
  486. int i, curDistance, bestTownRecno=0, minDistance=0x7FFFFFFF;
  487. Town* townPtr;
  488. for( i=size() ; i>0 ; i-- )
  489. {
  490. if( town_array.is_deleted(i) )
  491. continue;
  492. townPtr = town_array[i];
  493. curDistance = m.points_distance( xLoc, yLoc, townPtr->center_x, townPtr->center_y );
  494. if( nationRecno && townPtr->nation_recno != nationRecno )
  495. continue;
  496. //--- when the firm is outside the town, find the town nearest to the firm ---//
  497. if( curDistance < minDistance )
  498. {
  499. minDistance = curDistance;
  500. bestTownRecno = i;
  501. }
  502. }
  503. return bestTownRecno;
  504. }
  505. //----------- End of function TownArray::find_nearest_town ---------//
  506. //--------- Begin of function TownArray::settle --------//
  507. //
  508. // A unit settles on a given location and form a town town.
  509. //
  510. // return: <int> 1 - settled and a town town is formed successfully.
  511. // 0 - settling failed. too close to a town of another nation.
  512. //
  513. int TownArray::settle(int unitRecno, int xLoc, int yLoc)
  514. {
  515. if(!world.can_build_town(xLoc, yLoc, unitRecno))
  516. return 0;
  517. //--------- get the nearest town town ------------//
  518. Unit* unitPtr = unit_array[unitRecno];
  519. char nationRecno = unitPtr->nation_recno;
  520. //----- it's far enough to form another town --------//
  521. int townRecno = town_array.add_town( nationRecno, unitPtr->race_id, xLoc, yLoc );
  522. //---------- init town population ----------//
  523. Town* townPtr = town_array[townRecno];
  524. //----------------------------------------------------//
  525. // if the settle unit is standing in the town area
  526. // cargo_recno of that location is unchange in town_array.add_town
  527. // so update the location now
  528. //----------------------------------------------------//
  529. short uXLoc = unitPtr->next_x_loc();
  530. short uYLoc = unitPtr->next_y_loc();
  531. Location *locPtr = world.get_loc(uXLoc, uYLoc);
  532. townPtr->assign_unit(unitRecno);
  533. if( uXLoc>=townPtr->loc_x1 && uXLoc<=townPtr->loc_x2 && uYLoc>=townPtr->loc_y1 && uYLoc<=townPtr->loc_y2 )
  534. locPtr->set_town(townPtr->town_recno);
  535. #ifdef DEBUG
  536. // make sure cargo recno is set to town's
  537. for( uYLoc = townPtr->loc_y1; uYLoc <= townPtr->loc_y2; ++uYLoc)
  538. {
  539. for( uXLoc = townPtr->loc_x1; uXLoc <= townPtr->loc_x2; ++uXLoc)
  540. {
  541. err_when( world.get_loc(uXLoc, uYLoc)->town_recno() != townPtr->town_recno );
  542. }
  543. }
  544. #endif
  545. townPtr->update_target_loyalty();
  546. //--------- hide the unit from the map ----------//
  547. return 1;
  548. }
  549. //----------- End of function TownArray::settle ---------//
  550. //--------- Begin of function TownArray::distribute_demand --------//
  551. //
  552. void TownArray::distribute_demand()
  553. {
  554. //--- reset market place demand ----//
  555. int i, j;
  556. FirmMarket* firmMarket;
  557. for( i=firm_array.size() ; i>0 ; i-- )
  558. {
  559. if( firm_array.is_deleted(i) )
  560. continue;
  561. firmMarket = (FirmMarket*) firm_array[i];
  562. if( firmMarket->firm_id == FIRM_MARKET )
  563. {
  564. for( j=0 ; j<MAX_MARKET_GOODS ; j++ )
  565. {
  566. firmMarket->market_goods_array[j].month_demand = (float) 0;
  567. }
  568. }
  569. }
  570. //-------- distribute demand -------//
  571. for( i=size() ; i>0 ; i-- )
  572. {
  573. if( town_array.is_deleted(i) )
  574. continue;
  575. town_array[i]->distribute_demand();
  576. }
  577. }
  578. //----------- End of function TownArray::distribute_demand ---------//
  579. //------- Begin of function TownArray::stop_attack_nation -----//
  580. void TownArray::stop_attack_nation(short nationRecno)
  581. {
  582. Town* townPtr;
  583. for(int i=size(); i>0; --i)
  584. {
  585. townPtr = (Town*) get_ptr(i);
  586. if(townPtr)
  587. townPtr->reset_hostile_nation(nationRecno);
  588. }
  589. }
  590. //----------- End of function TownArray::stop_attack_nation ---------//
  591. //------- Begin of function TownArray::is_deleted -----//
  592. int TownArray::is_deleted(int recNo)
  593. {
  594. Town *townPtr = (Town*) get_ptr(recNo);
  595. if( !townPtr )
  596. return 1;
  597. if( townPtr->population==0 )
  598. return 1;
  599. return 0;
  600. }
  601. //--------- End of function TownArray::is_deleted ----//
  602. #ifdef DEBUG
  603. //------- Begin of function TownArray::operator[] -----//
  604. Town* TownArray::operator[](int recNo)
  605. {
  606. Town* townPtr = (Town*) get_ptr(recNo);
  607. if( !townPtr )
  608. err.run( "TownArray[] is deleted" );
  609. return townPtr;
  610. }
  611. //--------- End of function TownArray::operator[] ----//
  612. #endif