OSITE.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 : OSITE.CPP
  21. //Description : Object Site
  22. #include <OINFO.h>
  23. #include <OWORLD.h>
  24. #include <OTOWN.h>
  25. #include <OFIRM.h>
  26. #include <OUNIT.h>
  27. #include <ONEWS.h>
  28. #include <OGODRES.h>
  29. #include <ONATION.h>
  30. #include <OSITE.h>
  31. //-------------- Define constant -----------//
  32. #define EXIST_RAW_RESERVE_QTY (MAX_RAW_RESERVE_QTY / 20) // only sites with reserve qty >= 5% of MAX_RAW_RESERVE_QTY are counted as raw sites
  33. //--------- Begin of function SiteArray::SiteArray -------//
  34. SiteArray::SiteArray() : DynArrayB(sizeof(Site), 50, DEFAULT_REUSE_INTERVAL_DAYS)
  35. {
  36. }
  37. //----------- End of function SiteArray::SiteArray -------//
  38. //--------- Begin of function SiteArray::~SiteArray -------//
  39. SiteArray::~SiteArray()
  40. {
  41. deinit();
  42. }
  43. //----------- End of function SiteArray::~SiteArray -------//
  44. //--------- Begin of function SiteArray::init -------//
  45. void SiteArray::init()
  46. {
  47. untapped_raw_count = 0;
  48. std_raw_site_count = 0;
  49. }
  50. //----------- End of function SiteArray::init -------//
  51. //--------- Begin of function SiteArray::deinit -------//
  52. void SiteArray::deinit()
  53. {
  54. if( size()==0 )
  55. return;
  56. zap(); // zap the DynArrayB
  57. untapped_raw_count = 0;
  58. }
  59. //----------- End of function SiteArray::deinit -------//
  60. //--------- Begin of function SiteArray::add_site ---------//
  61. //
  62. // Add a raw item to the site array
  63. //
  64. // Return : 1 - the raw is added
  65. // 0 - duplicated, not added
  66. //
  67. int SiteArray::add_site(int xLoc, int yLoc, int siteType, int objectId, int reserveQty)
  68. {
  69. //----- linkin the raw and update raw attribute ----//
  70. Site site;
  71. linkin(&site);
  72. Site* sitePtr = (Site*) get(recno());
  73. sitePtr->init(recno(), siteType, xLoc, yLoc);
  74. sitePtr->object_id = objectId;
  75. sitePtr->reserve_qty = reserveQty;
  76. switch( siteType )
  77. {
  78. case SITE_RAW:
  79. untapped_raw_count++;
  80. break;
  81. case SITE_SCROLL:
  82. scroll_count++;
  83. break;
  84. case SITE_GOLD_COIN:
  85. gold_coin_count++;
  86. break;
  87. }
  88. return 1;
  89. }
  90. //----------- End of function SiteArray::add_site ----------//
  91. //--------- Begin of function SiteArray::del_site ----------//
  92. //
  93. // Delete a specified site.
  94. //
  95. // <int> siteRecno = the record no. of the site to be deleted
  96. //
  97. void SiteArray::del_site(int siteRecno)
  98. {
  99. err_if( siteRecno == 0 )
  100. err_now( "SiteArray::del_site" );
  101. Site* sitePtr = site_array[siteRecno];
  102. switch( sitePtr->site_type )
  103. {
  104. case SITE_RAW:
  105. untapped_raw_count--;
  106. break;
  107. case SITE_SCROLL:
  108. scroll_count--;
  109. break;
  110. case SITE_GOLD_COIN:
  111. gold_coin_count--;
  112. break;
  113. }
  114. //-------------------------------//
  115. sitePtr->deinit();
  116. linkout(siteRecno);
  117. if( siteRecno == site_array.selected_recno )
  118. site_array.selected_recno = 0;
  119. }
  120. //--------- End of function SiteArray::del_site ----------//
  121. //--------- Begin of function SiteArray::generate_raw_site ----------//
  122. //
  123. // Generate raw sites. This function is both called at the beginning
  124. // of a game and when existing raw sites are being used up.
  125. //
  126. // [int] rawGenCount - no. of raw sites to be generated.
  127. // (if this is not given, it will use the existing std_raw_site_count)
  128. //
  129. void SiteArray::generate_raw_site(int rawGenCount)
  130. {
  131. if( rawGenCount )
  132. std_raw_site_count = rawGenCount; // use this number for determing whether new sites should emerge in the future
  133. #define MAX_RAW_REGION 3 // maximum no. of regions that has raw sites
  134. #define SMALLEST_RAW_REGION 50 // only put raw on the region if its size is larger than this
  135. #define REGION_SIZE_PER_RAW 100
  136. //----- count the no. of existing raw sites -------//
  137. Site* sitePtr;
  138. int existRawSiteCount=0;
  139. int i;
  140. for( i=size() ; i>0 ; i-- )
  141. {
  142. if( site_array.is_deleted(i) )
  143. continue;
  144. sitePtr = site_array[i];
  145. if( sitePtr->site_type == SITE_RAW &&
  146. sitePtr->reserve_qty >= EXIST_RAW_RESERVE_QTY )
  147. {
  148. existRawSiteCount++;
  149. }
  150. }
  151. if( existRawSiteCount >= std_raw_site_count )
  152. return;
  153. //----- check which regions are valid for raw sites -----//
  154. int regionCount = min( MAX_RAW_REGION, region_array.region_info_count );
  155. int validRegionCount, totalValidSize=0;
  156. RegionInfo* regionInfo;
  157. for( validRegionCount=0 ; validRegionCount<regionCount ; validRegionCount++ )
  158. {
  159. regionInfo = region_array.get_sorted_region(validRegionCount+1);
  160. if( regionInfo->region_type != REGION_LAND )
  161. continue;
  162. if( regionInfo->region_size < SMALLEST_RAW_REGION )
  163. break;
  164. totalValidSize += regionInfo->region_size;
  165. }
  166. if( validRegionCount==0 ) // valid regions are those that are big enough to put raw sites
  167. return;
  168. //----- count the no. of existing raw sites in each ragion ------//
  169. int regionId;
  170. char regionRawCountArray[MAX_REGION];
  171. memset( regionRawCountArray, 0, sizeof(regionRawCountArray) );
  172. for( i=size() ; i>0 ; i-- )
  173. {
  174. if( site_array.is_deleted(i) )
  175. continue;
  176. sitePtr = site_array[i];
  177. if( sitePtr->site_type == SITE_RAW &&
  178. sitePtr->reserve_qty >= EXIST_RAW_RESERVE_QTY )
  179. {
  180. regionId = world.get_region_id(sitePtr->map_x_loc, sitePtr->map_y_loc);
  181. regionRawCountArray[regionId-1]++;
  182. }
  183. }
  184. //--------- generate raw sites now ----------//
  185. int avgValidSize = min( 10000, totalValidSize / std_raw_site_count );
  186. int j, createCount;
  187. err_when( validRegionCount > region_array.region_info_count || validRegionCount > MAX_RAW_REGION );
  188. for( int k=0 ; k<10 ; k++ ) // one loop may not be enough to generate all raw sites, have more loops to make sure all are generated
  189. {
  190. for( i=0 ; i<regionCount ; i++ )
  191. {
  192. regionInfo = region_array.get_sorted_region(i+1);
  193. if( regionInfo->region_type != REGION_LAND )
  194. continue;
  195. if( regionInfo->region_size < SMALLEST_RAW_REGION )
  196. break;
  197. createCount = regionInfo->region_size / avgValidSize;
  198. createCount = max(1, createCount);
  199. //--------- create now --------//
  200. for( j=regionRawCountArray[regionInfo->region_id-1] ; j<createCount ; j++ ) // if currently there are already some, don't create new ones
  201. {
  202. if( create_raw_site(regionInfo->region_id) )
  203. {
  204. if( ++existRawSiteCount == std_raw_site_count )
  205. return;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. //--------- End of function SiteArray::generate_raw_site ----------//
  212. //--------- Begin of function SiteArray::create_raw_site ----------//
  213. //
  214. // <int> regionId - if this parameter is given, the raw site
  215. // will be built on this region.
  216. // [int] townRecno - if this parameter is given, the raw site
  217. // will be built near this town.
  218. //
  219. int SiteArray::create_raw_site(int regionId, int townRecno)
  220. {
  221. //-------- count the no. of each raw material -------//
  222. Site* sitePtr;
  223. short rawCountArray[MAX_RAW];
  224. memset( rawCountArray, 0, sizeof(rawCountArray) );
  225. int i;
  226. for( i=size(); i>0 ; i-- )
  227. {
  228. if( site_array.is_deleted(i) )
  229. continue;
  230. sitePtr = site_array[i];
  231. if( sitePtr->site_type == SITE_RAW )
  232. {
  233. err_when( sitePtr->object_id < 1 || sitePtr->object_id > MAX_RAW );
  234. rawCountArray[ sitePtr->object_id-1 ]++;
  235. }
  236. }
  237. //---- find the minimum raw count ----//
  238. int minCount=0xFFFF;
  239. for( i=0 ; i<MAX_RAW ; i++ )
  240. {
  241. if( rawCountArray[i] < minCount )
  242. minCount = rawCountArray[i];
  243. }
  244. //----- pick a raw material type -----//
  245. int rawId = m.random(MAX_RAW)+1;
  246. for( i=0 ; i<MAX_RAW ; i++ )
  247. {
  248. if( ++rawId > MAX_RAW )
  249. rawId = 1;
  250. if( rawCountArray[rawId-1] == minCount ) // don't use this raw type unless it is one of the less available ones.
  251. break;
  252. }
  253. //--------- create the raw site now ------//
  254. int locX1, locY1, locX2, locY2;
  255. int maxTries;
  256. if( townRecno )
  257. {
  258. #define MAX_TOWN_SITE_DISTANCE 10
  259. Town* townPtr = town_array[townRecno];
  260. locX1 = townPtr->center_x - MAX_TOWN_SITE_DISTANCE;
  261. locX2 = townPtr->center_x + MAX_TOWN_SITE_DISTANCE;
  262. locY1 = townPtr->center_y - MAX_TOWN_SITE_DISTANCE;
  263. locY2 = townPtr->center_y + MAX_TOWN_SITE_DISTANCE;
  264. if(locX1<0)
  265. locX1 = 0;
  266. else if(locX2>=MAX_WORLD_X_LOC)
  267. locX2 = MAX_WORLD_X_LOC-1;
  268. if(locY1<0)
  269. locY1 = 0;
  270. else if(locY2>=MAX_WORLD_Y_LOC)
  271. locY2 = MAX_WORLD_Y_LOC-1;
  272. maxTries = (locX2-locX1+1)*(locY2-locY1+1);
  273. regionId = townPtr->region_id;
  274. }
  275. else
  276. {
  277. locX1 = 0;
  278. locY1 = 0;
  279. locX2 = MAX_WORLD_X_LOC-1;
  280. locY2 = MAX_WORLD_Y_LOC-1;
  281. maxTries = 10000;
  282. }
  283. //----- randomly locate a space to add the site -----//
  284. if( world.locate_space_random(locX1, locY1, locX2, locY2,
  285. 5, 5, maxTries, regionId, 1) ) // 5,5 are the size of the raw site, it must be large enough for a mine to build and 1 location for the edges. The last para 1 = site building mode
  286. {
  287. int reserveQty = MAX_RAW_RESERVE_QTY * (50 + m.random(50)) / 100;
  288. add_site( locX1+2, locY1+2, SITE_RAW, rawId, reserveQty ); // xLoc+1 & yLoc+1 as the located size is 3x3, the raw site is at the center of it
  289. return 1;
  290. }
  291. else
  292. {
  293. return 0;
  294. }
  295. }
  296. //--------- End of function SiteArray::create_raw_site ----------//
  297. //--------- Begin of function SiteArray::scan_site ----------//
  298. //
  299. // Scan for the a site that is closest the given location.
  300. //
  301. // <int> xLoc, yLoc - the location given.
  302. // [int] siteType - if given, only scan for this site (default: 0)
  303. //
  304. // return: <int> nearestSiteRecno - the recno of the raw materials
  305. // that is nearest to the given location.
  306. //
  307. int SiteArray::scan_site(int xLoc, int yLoc, int siteType)
  308. {
  309. Site* sitePtr;
  310. int siteDis, minDis=0x7FFFFFFF, nearestSiteRecno=0;
  311. for( int i=site_array.size() ; i>0 ; i-- )
  312. {
  313. if( site_array.is_deleted(i) )
  314. continue;
  315. sitePtr = site_array[i];
  316. if( siteType==0 || sitePtr->site_type==siteType )
  317. {
  318. siteDis = m.points_distance( xLoc, yLoc, sitePtr->map_x_loc, sitePtr->map_y_loc );
  319. if( siteDis < minDis )
  320. {
  321. minDis = siteDis;
  322. nearestSiteRecno = i;
  323. }
  324. }
  325. }
  326. return nearestSiteRecno;
  327. }
  328. //---------- End of function SiteArray::scan_site -----------//
  329. //--------- Begin of function SiteArray::next_day ----------//
  330. //
  331. void SiteArray::next_day()
  332. {
  333. if( info.game_date%30 == 0 )
  334. {
  335. generate_raw_site(); // check if we need to generate existing raw sites are being used up and if we need to generate new ones
  336. }
  337. //-- if there is any scroll or gold coins available, ask AI to get them --//
  338. if(scroll_count || gold_coin_count)
  339. {
  340. int aiGetSiteObject = (info.game_date%5 == 0);
  341. Site* sitePtr;
  342. Location *locPtr;
  343. for(int i=size(); i; i--)
  344. {
  345. if(is_deleted(i))
  346. continue;
  347. sitePtr = site_array[i];
  348. switch(sitePtr->site_type)
  349. {
  350. case SITE_SCROLL:
  351. case SITE_GOLD_COIN:
  352. locPtr = world.get_loc(sitePtr->map_x_loc, sitePtr->map_y_loc);
  353. //---- if the unit is standing on a scroll site -----//
  354. if(locPtr->has_unit(UNIT_LAND))
  355. {
  356. sitePtr->get_site_object( locPtr->unit_recno(UNIT_LAND) );
  357. }
  358. else if(aiGetSiteObject)
  359. {
  360. sitePtr->ai_get_site_object();
  361. }
  362. break;
  363. }
  364. }
  365. }
  366. //-------- debug testing --------//
  367. #ifdef DEBUG
  368. if( info.game_date%10 == 0 )
  369. {
  370. Site* sitePtr;
  371. Location* locPtr;
  372. for( int i=1 ; i<=size() ; i++ )
  373. {
  374. if( site_array.is_deleted(i) )
  375. continue;
  376. sitePtr = site_array[i];
  377. locPtr = world.get_loc( sitePtr->map_x_loc, sitePtr->map_y_loc );
  378. err_when( !locPtr->has_site() );
  379. err_when( locPtr->site_recno() != i );
  380. if( sitePtr->has_mine )
  381. {
  382. err_when( !locPtr->is_firm() );
  383. err_when( firm_array[locPtr->firm_recno()]->firm_id != FIRM_MINE );
  384. }
  385. else
  386. {
  387. err_when( locPtr->is_firm() || locPtr->is_town() );
  388. }
  389. }
  390. }
  391. #endif
  392. }
  393. //--------- End of function SiteArray::next_day ----------//
  394. //--------- Begin of function SiteArray::ai_get_site_object -------//
  395. //
  396. // Notify AI units to acquire scrolls or gold coins available on the
  397. // map.
  398. //
  399. void SiteArray::ai_get_site_object()
  400. {
  401. Site* sitePtr;
  402. for( int i=1 ; i<=size() ; i++ )
  403. {
  404. if( site_array.is_deleted(i) )
  405. continue;
  406. sitePtr = site_array[i];
  407. if( sitePtr->site_type == SITE_SCROLL ||
  408. sitePtr->site_type == SITE_GOLD_COIN )
  409. {
  410. sitePtr->ai_get_site_object();
  411. }
  412. }
  413. }
  414. //----------- End of function SiteArray::ai_get_site_object -------//
  415. //------- Begin of function Site::ai_get_site_object -------//
  416. //
  417. // Ask AI units around to get the object on this site.
  418. //
  419. int Site::ai_get_site_object()
  420. {
  421. #define NOTIFY_GET_RANGE 30 // only notify units within this range
  422. #define MAX_UNIT_TO_ORDER 5
  423. int xOffset, yOffset;
  424. int xLoc, yLoc;
  425. Location* locPtr;
  426. Unit* unitPtr;
  427. int unitOrderedCount=0;
  428. int siteRaceId = 0;
  429. if( site_type == SITE_SCROLL )
  430. siteRaceId = god_res[object_id]->race_id;
  431. for( int i=2 ; i<NOTIFY_GET_RANGE*NOTIFY_GET_RANGE ; i++ )
  432. {
  433. m.cal_move_around_a_point(i, NOTIFY_GET_RANGE, NOTIFY_GET_RANGE, xOffset, yOffset);
  434. xLoc = map_x_loc + xOffset;
  435. yLoc = map_y_loc + yOffset;
  436. xLoc = max(0, xLoc);
  437. xLoc = min(MAX_WORLD_X_LOC-1, xLoc);
  438. yLoc = max(0, yLoc);
  439. yLoc = min(MAX_WORLD_Y_LOC-1, yLoc);
  440. locPtr = world.get_loc(xLoc, yLoc);
  441. if( !locPtr->has_unit(UNIT_LAND) )
  442. continue;
  443. //------------------------------//
  444. int unitRecno = locPtr->unit_recno(UNIT_LAND);
  445. if( unit_array.is_deleted(unitRecno) )
  446. continue;
  447. unitPtr = unit_array[unitRecno];
  448. if( !unitPtr->race_id || !unitPtr->ai_unit || unitPtr->ai_action_id )
  449. continue;
  450. if( siteRaceId && siteRaceId != unitPtr->race_id )
  451. continue;
  452. unitPtr->move_to(map_x_loc, map_y_loc);
  453. //--- if the unit is just standing next to the site ---//
  454. if( abs(map_x_loc-xLoc)<=1 && abs(map_y_loc-yLoc)<=1 )
  455. {
  456. return 1;
  457. }
  458. else // order more than one unit to get the site at the same time
  459. {
  460. if( ++unitOrderedCount >= MAX_UNIT_TO_ORDER )
  461. return 1;
  462. }
  463. }
  464. return 0;
  465. }
  466. //-------- End of function Site::ai_get_site_object -------//
  467. //--------- Begin of function SiteArray::go_to_a_raw_site -------//
  468. //
  469. // Go to an untapped raw site.
  470. //
  471. void SiteArray::go_to_a_raw_site()
  472. {
  473. //----- try to locate an untapped raw site -----//
  474. Site* sitePtr;
  475. //### begin alex 22/10 ###//
  476. int arraySize = size();
  477. int i = selected_recno ? selected_recno : 0;
  478. //#### end alex 22/10 ####//
  479. //### begin alex 22/10 ###//
  480. //for( int i=1 ; i<=size() ; i++ )
  481. //{
  482. int j;
  483. for( j=1 ; j<=arraySize ; j++ )
  484. {
  485. if(++i > arraySize)
  486. i = 1;
  487. //#### end alex 22/10 ####//
  488. if( site_array.is_deleted(i) )
  489. continue;
  490. sitePtr = site_array[i];
  491. if( !sitePtr->has_mine )
  492. {
  493. if( world.get_loc(sitePtr->map_x_loc, sitePtr->map_y_loc)->explored() )
  494. {
  495. world.go_loc( sitePtr->map_x_loc, sitePtr->map_y_loc, 1 ); // 1-select the object on the location
  496. return;
  497. }
  498. }
  499. }
  500. //---- if no untapped raw sites left, jump to built mines ----//
  501. //### begin alex 22/10 ###//
  502. i = 1;
  503. if(firm_array.selected_recno)
  504. {
  505. //------- get the site_recno if a mine is selected ---------//
  506. Firm *firmPtr = firm_array[firm_array.selected_recno];
  507. if(firmPtr->firm_id==FIRM_MINE)
  508. {
  509. int x1 = firmPtr->loc_x1;
  510. int y1 = firmPtr->loc_y1;
  511. int x2 = firmPtr->loc_x2;
  512. int y2 = firmPtr->loc_y2;
  513. for(int count=1; count<=arraySize; ++count)
  514. {
  515. if(site_array.is_deleted(count))
  516. continue;
  517. sitePtr = site_array[count];
  518. if(sitePtr->map_x_loc>=x1 && sitePtr->map_x_loc<=x2 && sitePtr->map_y_loc>=y1 && sitePtr->map_y_loc<=y2)
  519. {
  520. i = count;
  521. break;
  522. }
  523. }
  524. }
  525. }
  526. //#### end alex 22/10 ####//
  527. //### begin alex 22/10 ###//
  528. //for( i=1 ; i<=size() ; i++ )
  529. //{
  530. for( j=1 ; j<=arraySize ; j++ )
  531. {
  532. if(++i > arraySize)
  533. i = 1;
  534. //#### end alex 22/10 ####//
  535. if( site_array.is_deleted(i) )
  536. continue;
  537. sitePtr = site_array[i];
  538. if( world.get_loc(sitePtr->map_x_loc, sitePtr->map_y_loc)->explored() )
  539. {
  540. world.go_loc( sitePtr->map_x_loc, sitePtr->map_y_loc, 1 ); // 1-select the object on the location
  541. return;
  542. }
  543. }
  544. }
  545. //----------- End of function SiteArray::go_to_a_raw_site -------//
  546. //--------- Begin of function Site::init ----------//
  547. //
  548. void Site::init(int siteRecno, int siteType, int xLoc, int yLoc)
  549. {
  550. site_recno = siteRecno;
  551. site_type = siteType;
  552. map_x_loc = xLoc;
  553. map_y_loc = yLoc;
  554. has_mine = 0;
  555. //------- set world's location --------//
  556. Location* locPtr = world.get_loc(xLoc, yLoc);
  557. locPtr->set_site(siteRecno);
  558. region_id = locPtr->region_id;
  559. }
  560. //---------- End of function Site::init -----------//
  561. //--------- Begin of function Site::deinit ----------//
  562. //
  563. void Site::deinit()
  564. {
  565. //------ reset world's location ---------//
  566. world.get_loc(map_x_loc, map_y_loc)->remove_site();
  567. }
  568. //---------- End of function Site::deinit -----------//
  569. //--------- Begin of function Site::get_site_object ----------//
  570. //
  571. // An unit takes the object from the site
  572. //
  573. // <int> unitRecno - recno of the unit that takes the object on the site.
  574. //
  575. int Site::get_site_object(int unitRecno)
  576. {
  577. Unit* unitPtr = unit_array[unitRecno];
  578. int objectTaken=0;
  579. if( !unitPtr->nation_recno )
  580. return 0;
  581. //----- if this is a scroll site ------//
  582. if( site_type == SITE_SCROLL )
  583. {
  584. if( god_res[object_id]->race_id == unitPtr->race_id )
  585. {
  586. god_res[object_id]->enable_know(unitPtr->nation_recno);
  587. objectTaken = 1;
  588. news_array.scroll_acquired(unitPtr->nation_recno, god_res[object_id]->race_id );
  589. }
  590. }
  591. //------ if there are gold coins on this site -----//
  592. if( site_type == SITE_GOLD_COIN )
  593. {
  594. nation_array[unitPtr->nation_recno]->add_income(INCOME_TREASURE, object_id);
  595. objectTaken = 1;
  596. if( unitPtr->nation_recno == nation_array.player_recno )
  597. news_array.monster_gold_acquired(object_id);
  598. }
  599. //---- if the object has been taken by the unit ----//
  600. if( objectTaken )
  601. {
  602. site_array.del_site(site_recno);
  603. return 1;
  604. }
  605. return 0;
  606. }
  607. //---------- End of function Site::get_site_object -----------//
  608. //------- Begin of function SiteArray::is_deleted -----//
  609. int SiteArray::is_deleted(int recNo)
  610. {
  611. Site* sitePtr = (Site*) get(recNo);
  612. return !sitePtr || sitePtr->site_type==0;
  613. }
  614. //--------- End of function SiteArray::is_deleted ----//
  615. #ifdef DEBUG
  616. //------- Begin of function SiteArray::operator[] -----//
  617. Site* SiteArray::operator[](int recNo)
  618. {
  619. Site* sitePtr = (Site*) get(recNo);
  620. if( !sitePtr || sitePtr->site_type==0 )
  621. err.run( "SiteArray[] is deleted" );
  622. return sitePtr;
  623. }
  624. //--------- End of function SiteArray::operator[] ----//
  625. //------- Begin of function SiteArray::operator() -----//
  626. Site* SiteArray::operator()()
  627. {
  628. Site* sitePtr = (Site*) get();
  629. if( !sitePtr || sitePtr->site_type==0 )
  630. err.run( "SiteArray[recno()] is deleted" );
  631. return sitePtr;
  632. }
  633. //--------- End of function SiteArray::operator() ----//
  634. #endif