OFIRMA.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 : OFIRMA.CPP
  21. //Description : Object Firm Array
  22. #include <OVGA.h>
  23. #include <OWORLD.h>
  24. #include <OPOWER.h>
  25. #include <ONATION.h>
  26. #include <OSYS.h>
  27. #include <OGAME.h>
  28. #include <OTOWN.h>
  29. #include <OINFO.h>
  30. #include <OFIRMRES.h>
  31. #include <OFIRMALL.h>
  32. #include <OSERES.h>
  33. #include <OLOG.h>
  34. //### begin alex 22/9 ###//
  35. #ifdef DEBUG
  36. #include <OFONT.h>
  37. static unsigned long last_firm_ai_profile_time = 0L;
  38. static unsigned long firm_ai_profile_time = 0L;
  39. static unsigned long last_firm_profile_time = 0L;
  40. static unsigned long firm_profile_time = 0L;
  41. #endif
  42. //#### end alex 22/9 ####//
  43. //----------- Note ----------------//
  44. //
  45. // go(), start(), end(), fwd(), bkwd() will affect
  46. // the current record no. pointer which will be used by other function
  47. //
  48. // To process all elements in the array, get(recNo) should be used.
  49. //
  50. // Because the sizeof different derived Firm objects are quite largely
  51. // different, it is better to link in pointer instead of the object
  52. // body.
  53. //
  54. // The DynArrayB contain the pointer to Firm objects.
  55. //
  56. //---------------------------------//
  57. //--------- Begin of function FirmArray::FirmArray ---------//
  58. //
  59. FirmArray::FirmArray() : DynArrayB(sizeof(Firm*), 50, DEFAULT_REUSE_INTERVAL_DAYS)
  60. {
  61. }
  62. //----------- End of function FirmArray::FirmArray ---------//
  63. //--------- Begin of function FirmArray::~FirmArray ---------//
  64. //
  65. FirmArray::~FirmArray()
  66. {
  67. deinit();
  68. }
  69. //----------- End of function FirmArray::~FirmArray ---------//
  70. //--------- Begin of function FirmArray::init ---------//
  71. void FirmArray::init()
  72. {
  73. process_recno = 1;
  74. }
  75. //---------- End of function FirmArray::init ---------//
  76. //--------- Begin of function FirmArray::deinit ---------//
  77. //
  78. // All firms should be deleted when the system terminated
  79. //
  80. // Chain : delete nation --> delete firm --> delete job --> delete item
  81. //
  82. // Some data is corrupted when the some firms still exist when
  83. // the system terminated
  84. //
  85. void FirmArray::deinit()
  86. {
  87. if( size()==0 )
  88. return;
  89. //----------------------------------//
  90. int i;
  91. for( i=1 ; i<=size() ; i++ )
  92. {
  93. if( !firm_array.is_deleted(i) )
  94. firm_array.del_firm(i);
  95. }
  96. //----------------------------------//
  97. zap(); // zap the DynArrayB
  98. }
  99. //----------- End of function FirmArray::deinit ---------//
  100. //--------- Begin of function FirmArray::build_firm ---------//
  101. //
  102. // build_firm() will be called by Nation and FirmRes when setting up
  103. // new firm.
  104. //
  105. // <int> xLoc = the x location of the firm to be built
  106. // <int> yLoc = the y location of the firm to be built
  107. // <int> nationRecno = the nation which builds this firm
  108. // <int> firmId = firm type id.
  109. // [char*] buildCode = the build code of the firm, no need to give if the firm just have one build type
  110. // [short] builderRecno = recno of the builder unit
  111. //
  112. // Return : <int> the record no. of the newly added firm
  113. //
  114. int FirmArray::build_firm(int xLoc, int yLoc, int nationRecno, int firmId, char* buildCode, short builderRecno)
  115. {
  116. if( !world.can_build_firm(xLoc, yLoc, firmId) )
  117. return 0;
  118. //--------- check if there is enough cash ----------//
  119. if( nationRecno )
  120. {
  121. FirmInfo* firmInfo = firm_res[firmId];
  122. Nation* nationPtr = nation_array[nationRecno];
  123. if( nationRecno )
  124. {
  125. if( nationPtr->cash < firmInfo->setup_cost )
  126. return 0;
  127. }
  128. }
  129. //---------- create and build the firm -------------//
  130. int firmRecno = create_firm(firmId);
  131. firm_array[firmRecno]->init( xLoc, yLoc, nationRecno, firmId, buildCode, builderRecno);
  132. // Firm::init() will set world matrix, it will use Firm::firm_recno to set the location cargo
  133. //------ pay the land cost to the nation that owns the land ------//
  134. if( nationRecno )
  135. {
  136. Nation* nationPtr = nation_array[nationRecno];
  137. nationPtr->add_expense( EXPENSE_FIRM, (float)firm_res[firmId]->setup_cost); // setup cost of the firm
  138. }
  139. return firmRecno;
  140. }
  141. //---------- End of function FirmArray::build_firm ---------//
  142. //--------- Begin of function FirmArray::create_firm ---------//
  143. //
  144. // create_firm() will be called directly by :
  145. //
  146. // 1. FirmArray::build_firm() for setting up a new firm
  147. // 2. FirmArray::read_file() when loading game.
  148. //
  149. // <int> firmId = firm type id
  150. //
  151. // Return : <int> the record no. of the newly added firm
  152. //
  153. int FirmArray::create_firm(int firmId)
  154. {
  155. Firm* firmPtr;
  156. switch(firmId)
  157. {
  158. case FIRM_BASE:
  159. firmPtr = new FirmBase;
  160. break;
  161. case FIRM_CAMP:
  162. firmPtr = new FirmCamp;
  163. break;
  164. case FIRM_FACTORY:
  165. firmPtr = new FirmFactory;
  166. break;
  167. case FIRM_INN:
  168. firmPtr = new FirmInn;
  169. break;
  170. case FIRM_MARKET:
  171. firmPtr = new FirmMarket;
  172. break;
  173. case FIRM_MINE:
  174. firmPtr = new FirmMine;
  175. break;
  176. case FIRM_RESEARCH:
  177. firmPtr = new FirmResearch;
  178. break;
  179. case FIRM_WAR_FACTORY:
  180. firmPtr = new FirmWar;
  181. break;
  182. case FIRM_HARBOR:
  183. firmPtr = new FirmHarbor;
  184. break;
  185. case FIRM_MONSTER:
  186. firmPtr = new FirmMonster;
  187. break;
  188. default:
  189. err_now("FirmArray::create_firm()");
  190. }
  191. //----------------------------------------//
  192. linkin(&firmPtr);
  193. firmPtr->firm_recno = recno();
  194. return firmPtr->firm_recno;
  195. }
  196. //----------- End of function FirmArray::create_firm ---------//
  197. //--------- Begin of function FirmArray::firm_class_size ---------//
  198. //
  199. // Return the size of the specified class.
  200. // This function will be called by FirmArray::write_file()
  201. //
  202. // <int> id = the id of the job
  203. //
  204. int FirmArray::firm_class_size(int id)
  205. {
  206. switch(id)
  207. {
  208. case FIRM_BASE:
  209. return sizeof(FirmBase);
  210. case FIRM_CAMP:
  211. return sizeof(FirmCamp);
  212. case FIRM_FACTORY:
  213. return sizeof(FirmFactory);
  214. case FIRM_INN:
  215. return sizeof(FirmInn);
  216. case FIRM_MARKET:
  217. return sizeof(FirmMarket);
  218. case FIRM_MINE:
  219. return sizeof(FirmMine);
  220. case FIRM_RESEARCH:
  221. return sizeof(FirmResearch);
  222. case FIRM_WAR_FACTORY:
  223. return sizeof(FirmWar);
  224. case FIRM_HARBOR:
  225. return sizeof(FirmHarbor);
  226. case FIRM_MONSTER:
  227. return sizeof(FirmMonster);
  228. default:
  229. err_now( "FirmArray::firm_class_size" );
  230. }
  231. return 0;
  232. }
  233. //----------- End of function FirmArray::firm_class_size ---------//
  234. //--------- Begin of function FirmArray::del_firm ---------//
  235. //
  236. // Warning : After calling this function, the recno() is still
  237. // pointing to the deleted record.
  238. // So go() to a new record to prevent running NULL object
  239. //
  240. // <int> recNo = the no. of the record to be deleted
  241. // (default : current record no.)
  242. //
  243. void FirmArray::del_firm(int recNo)
  244. {
  245. Firm* firmPtr = firm_array[recNo];
  246. int xLoc = firmPtr->center_x;
  247. int yLoc = firmPtr->center_y;
  248. firmPtr->deinit(); // we must call deinit() first
  249. delete firmPtr;
  250. linkout(recNo);
  251. }
  252. //----------- End of function FirmArray::del_firm ---------//
  253. //--------- Begin of function FirmArray::process ---------//
  254. //
  255. // Process all firm in firm_array for action and movement for next frame
  256. //
  257. // Return : 1 - all firm in the FirmArray has been processed
  258. // 0 - only some has been processed, not all
  259. //
  260. int FirmArray::process()
  261. {
  262. int i;
  263. Firm *firmPtr;
  264. //----- each time process some firm only ------//
  265. for( i=1 ; i<=size() ; i++ )
  266. {
  267. firmPtr = (Firm*) get_ptr(i);
  268. if( !firmPtr ) // the firm has been deleted
  269. continue;
  270. err_when(firmPtr->firm_recno!=i);
  271. //-------- system yield ---------//
  272. if( i%20==1 )
  273. sys.yield();
  274. #if defined(DEBUG) && defined(ENABLE_LOG)
  275. String logStr;
  276. logStr = "begin process firm ";
  277. logStr += firmPtr->firm_recno;
  278. logStr += " nation=";
  279. logStr += firmPtr->nation_recno;
  280. LOG_MSG(logStr);
  281. #endif
  282. if(i==50)
  283. {
  284. FirmMarket *mPtr = (FirmMarket*) firmPtr;
  285. MarketGoods *marketGoods = mPtr->market_goods_array;
  286. marketGoods++;
  287. if(marketGoods->stock_qty)
  288. int debug = 0;
  289. }
  290. //-------- process visibility -----------//
  291. if( firmPtr->nation_recno == nation_array.player_recno ||
  292. ( firmPtr->nation_recno &&
  293. nation_array[firmPtr->nation_recno]->is_allied_with_player ) )
  294. {
  295. world.visit( firmPtr->loc_x1, firmPtr->loc_y1, firmPtr->loc_x2, firmPtr->loc_y2, EXPLORE_RANGE-1 );
  296. }
  297. //--------- process and process_ai firms ----------//
  298. if( firmPtr->under_construction )
  299. {
  300. LOG_MSG(" process_construction");
  301. firmPtr->process_construction();
  302. LOG_MSG(m.get_random_seed() );
  303. }
  304. else
  305. {
  306. if( i%FRAMES_PER_DAY == int(sys.frame_count%FRAMES_PER_DAY) ) // only process each firm once per day
  307. {
  308. //### begin alex 22/9 ###//
  309. #ifdef DEBUG
  310. unsigned long profileStartTime = m.get_time();
  311. #endif
  312. //#### end alex 22/9 ####//
  313. LOG_MSG(" next_day");
  314. firmPtr->next_day();
  315. LOG_MSG(m.get_random_seed() );
  316. //### begin alex 22/9 ###//
  317. #ifdef DEBUG
  318. firm_profile_time += m.get_time() - profileStartTime;
  319. #endif
  320. //#### end alex 22/9 ####//
  321. //-- if the hit points drop to zero, the firm should be deleted --//
  322. if( firmPtr->hit_points <=0 )
  323. {
  324. se_res.sound( firmPtr->center_x, firmPtr->center_y, 1, 'F', firmPtr->firm_id, "DEST" );
  325. del_firm( firmPtr->firm_recno );
  326. continue;
  327. }
  328. //--------- process AI ------------//
  329. #ifdef DEBUG
  330. if(config.disable_ai_flag==0 && firmPtr->firm_ai)
  331. #else
  332. if( firmPtr->firm_ai )
  333. #endif
  334. {
  335. LOG_MSG(" process_common_ai");
  336. firmPtr->process_common_ai();
  337. LOG_MSG(m.get_random_seed() );
  338. //### begin alex 22/9 ###//
  339. #ifdef DEBUG
  340. unsigned long profileAiStartTime = m.get_time();
  341. #endif
  342. //#### end alex 22/9 ####//
  343. LOG_MSG(" process_ai");
  344. firmPtr->process_ai();
  345. LOG_MSG(m.get_random_seed());
  346. //### begin alex 22/9 ###//
  347. #ifdef DEBUG
  348. firm_ai_profile_time += m.get_time() - profileAiStartTime;
  349. #endif
  350. //#### end alex 22/9 ####//
  351. if( is_deleted(i) ) // the firm may have been deleted in process_ai()
  352. continue;
  353. }
  354. //--- think about having other nations capturing this firm ----//
  355. if( info.game_date%60==i%60 ) // this is not limited to ai firms only, it is called on all firms as AI can capture other player's firm
  356. firmPtr->think_capture();
  357. }
  358. }
  359. //-------- process animation ---------//
  360. LOG_MSG(" process_animation");
  361. firmPtr->process_animation();
  362. LOG_MSG( m.get_random_seed() );
  363. }
  364. return 0;
  365. }
  366. //----------- End of function FirmArray::process ---------//
  367. //--------- Begin of function FirmArray::next_month ---------//
  368. //
  369. void FirmArray::next_month()
  370. {
  371. int i;
  372. Firm* firmPtr;
  373. LOG_MSG("begin FirmArray::next_month");
  374. LOG_MSG(m.get_random_seed() );
  375. for(i=1; i <=size() ; i++)
  376. {
  377. firmPtr = (Firm*)get_ptr(i);
  378. if( firmPtr && !firmPtr->under_construction )
  379. {
  380. LOG_MSG("Firm next_month");
  381. LOG_MSG( i );
  382. firmPtr->next_month();
  383. LOG_MSG(m.get_random_seed() );
  384. }
  385. }
  386. LOG_MSG("end FirmArray::next_month");
  387. LOG_MSG(m.get_random_seed() );
  388. }
  389. //----------- End of function FirmArray::next_month -----------//
  390. //--------- Begin of function FirmArray::next_year ---------//
  391. //
  392. void FirmArray::next_year()
  393. {
  394. int i;
  395. Firm* firmPtr;
  396. LOG_MSG("begin FirmArray::next_year");
  397. LOG_MSG(m.get_random_seed() );
  398. for(i=1; i <=size() ; i++)
  399. {
  400. firmPtr = (Firm*)get_ptr(i);
  401. if( firmPtr && !firmPtr->under_construction )
  402. {
  403. LOG_MSG("Firm next_month");
  404. LOG_MSG( i );
  405. firmPtr->next_year();
  406. LOG_MSG(m.get_random_seed() );
  407. }
  408. }
  409. LOG_MSG("end FirmArray::next_year");
  410. LOG_MSG(m.get_random_seed() );
  411. }
  412. //----------- End of function FirmArray::next_year -----------//
  413. //--------- Begin of function FirmArray::draw_dot ---------//
  414. //
  415. // Draw tiny dots on map window representing the location of the firm
  416. //
  417. void FirmArray::draw_dot()
  418. {
  419. char* vgaBufPtr = vga_back.buf_ptr();
  420. char* writePtr;
  421. int i, x, y;
  422. Firm* firmPtr;
  423. FirmBuild* firmBuild;
  424. char* nationColorArray = nation_array.nation_color_array;
  425. char nationColor;
  426. int vgaBufPitch = vga_back.buf_pitch();
  427. const unsigned int excitedColorCount = 4;
  428. char excitedColorArray[MAX_NATION+1][excitedColorCount];
  429. for( i = 0; i <= MAX_NATION; ++i )
  430. {
  431. if( i == 0 || !nation_array.is_deleted(i) )
  432. {
  433. char *remapTable = game.get_color_remap_table(i, 0);
  434. excitedColorArray[i][0] = remapTable[0xe0];
  435. excitedColorArray[i][1] = remapTable[0xe1];
  436. excitedColorArray[i][2] = remapTable[0xe2];
  437. excitedColorArray[i][3] = remapTable[0xe3];
  438. }
  439. else
  440. {
  441. excitedColorArray[i][0] =
  442. excitedColorArray[i][1] =
  443. excitedColorArray[i][2] =
  444. excitedColorArray[i][3] = (char) V_WHITE;
  445. }
  446. }
  447. for(i=1; i <=size() ; i++)
  448. {
  449. firmPtr = (Firm*)get_ptr(i);
  450. if( !firmPtr )
  451. continue;
  452. firmBuild = firm_res.get_build(firmPtr->firm_build_id);
  453. writePtr = vgaBufPtr + (MAP_Y1+firmPtr->loc_y1)*vgaBufPitch + (MAP_X1+firmPtr->loc_x1);
  454. nationColor = info.game_date - firmPtr->last_attacked_date > 2 ?
  455. nationColorArray[firmPtr->nation_recno] :
  456. excitedColorArray[firmPtr->nation_recno][sys.frame_count % excitedColorCount];
  457. char shadowColor = (char) VGA_GRAY;
  458. // ###### begin Gilbert 17/10 #######//
  459. int firmWidth = firmBuild->loc_width;
  460. int firmHeight = firmBuild->loc_height;
  461. if( firmPtr->nation_recno == 0 &&
  462. firmWidth == STD_TOWN_LOC_WIDTH && firmHeight == STD_TOWN_LOC_HEIGHT)
  463. {
  464. --firmWidth; // all monster are all 4x4, may mixed with independent town on mini-map
  465. --firmHeight;
  466. }
  467. for( y=firmHeight ; y>0 ; y--, writePtr+=vgaBufPitch-firmWidth )
  468. {
  469. for( x=firmWidth ; x>0 ; x--, writePtr++ )
  470. {
  471. if( *writePtr != UNEXPLORED_COLOR )
  472. *writePtr = nationColor;
  473. }
  474. if( *(writePtr+vgaBufPitch) != UNEXPLORED_COLOR)
  475. *(writePtr+vgaBufPitch) = shadowColor;
  476. }
  477. for( x = firmWidth; x>0; x--)
  478. {
  479. if( *(++writePtr) != UNEXPLORED_COLOR )
  480. *writePtr = shadowColor;
  481. }
  482. // ###### end Gilbert 17/10 #######//
  483. }
  484. }
  485. //----------- End of function FirmArray::draw_dot -----------//
  486. //### begin alex 22/9 ###//
  487. //--------- Begin of function FirmArray::draw_profile ---------//
  488. void FirmArray::draw_profile()
  489. {
  490. #ifdef DEBUG
  491. static unsigned long lastDrawTime = m.get_time();
  492. if(m.get_time() >= lastDrawTime + 1000)
  493. {
  494. last_firm_profile_time = firm_profile_time;
  495. lastDrawTime = m.get_time();
  496. firm_profile_time = 0L;
  497. }
  498. String str;
  499. str = "Firm : ";
  500. font_news.disp( ZOOM_X1+10, ZOOM_Y1+60, str, MAP_X2);
  501. str = "";
  502. str += last_firm_ai_profile_time;
  503. font_news.disp( ZOOM_X1+60, ZOOM_Y1+60, str, MAP_X2);
  504. str = "";
  505. str += last_firm_profile_time;
  506. font_news.disp( ZOOM_X1+100, ZOOM_Y1+60, str, MAP_X2);
  507. #endif
  508. }
  509. //----------- End of function FirmArray::draw_profile -----------//
  510. //#### end alex 22/9 ####//
  511. /*
  512. //--------- Begin of function FirmArray::skip ---------//
  513. //
  514. // Skip to prev/next firms within the firm filter.
  515. //
  516. // <int> skipDirection = -1 - backward one record
  517. // 1 - forward one record
  518. //
  519. void FirmArray::skip(int skipDirection)
  520. {
  521. int firmRecno=recno();
  522. Firm *firmPtr;
  523. while(1)
  524. {
  525. firmRecno += ( skipDirection>0 ? 1 : -1 );
  526. //------- loop to front/back --------//
  527. if( firmRecno > size() )
  528. firmRecno = firmRecno - size();
  529. if( firmRecno < 1 )
  530. firmRecno = firmRecno + size();
  531. //----- if no firm under current firm filter ----//
  532. if( firmRecno == recno() ) // after one loop, there is still no match
  533. return;
  534. //--------- if it is deleted --------//
  535. if( is_deleted(firmRecno) )
  536. continue;
  537. //------ if it is not in firm filter -------//
  538. if( !firm_array[firmRecno]->draw_map_loc(2) ) //only draw when the firm is in filter
  539. continue;
  540. //--------- go to the firm ----------//
  541. world.go_firm(firmRecno);
  542. return;
  543. }
  544. }
  545. //---------- End of function FirmArray::skip ----------//
  546. */
  547. #ifdef DEBUG
  548. //------- Begin of function FirmArray::operator() -----//
  549. Firm* FirmArray::operator()()
  550. {
  551. Firm* firmPtr = (Firm*) get_ptr(); // if recno()==0, get_ptr() returns NULL
  552. err_if( !firmPtr )
  553. err_now( "FirmArray[recno()] is deleted" );
  554. return firmPtr;
  555. }
  556. //--------- End of function FirmArray::operator() ----//
  557. //------- Begin of function FirmArray::operator[] -----//
  558. Firm* FirmArray::operator[](int recNo)
  559. {
  560. Firm* firmPtr = (Firm*) get_ptr(recNo);
  561. if( !firmPtr )
  562. err.run( "FirmArray[] is deleted" );
  563. return firmPtr;
  564. }
  565. //--------- End of function FirmArray::operator[] ----//
  566. #endif