OF_MINE.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 : OF_MINE.CPP
  21. //Description : Firm Mine
  22. #include <OINFO.h>
  23. #include <OVGA.h>
  24. #include <OSTR.h>
  25. #include <OFONT.h>
  26. #include <ONEWS.h>
  27. #include <OUNIT.h>
  28. #include <ORACERES.h>
  29. #include <OGAME.h>
  30. #include <OWORLD.h>
  31. #include <ONATION.h>
  32. #include <OSITE.h>
  33. #include <OF_MINE.h>
  34. #include <OF_FACT.h>
  35. //--------- Begin of function FirmMine::FirmMine ---------//
  36. //
  37. FirmMine::FirmMine()
  38. {
  39. firm_skill_id = SKILL_MINING;
  40. cur_month_production = (float) 0;
  41. last_month_production = (float) 0;
  42. next_output_link_id = 0;
  43. next_output_firm_recno = 0;
  44. ai_should_build_factory_count = 0;
  45. }
  46. //----------- End of function FirmMine::FirmMine -----------//
  47. //--------- Begin of function FirmMine::~FirmMine ---------//
  48. //
  49. FirmMine::~FirmMine()
  50. {
  51. //------- update the site deposit reserve ------//
  52. if( site_recno )
  53. {
  54. site_array.untapped_raw_count++;
  55. if( reserve_qty==0 ) // if the reserve has been used up
  56. {
  57. site_array.del_site(site_recno);
  58. }
  59. else // restore the site
  60. {
  61. Site* sitePtr = site_array[site_recno];
  62. sitePtr->reserve_qty = (int) reserve_qty;
  63. sitePtr->has_mine = 0;
  64. }
  65. }
  66. //-------- decrease AI raw count --------//
  67. if( raw_id )
  68. {
  69. nation_array[nation_recno]->raw_count_array[raw_id-1]--;
  70. err_when( nation_array[nation_recno]->raw_count_array[raw_id-1] < 0 );
  71. }
  72. }
  73. //----------- End of function FirmMine::~FirmMine -----------//
  74. //--------- Begin of function FirmMine::init_derived ---------//
  75. //
  76. void FirmMine::init_derived()
  77. {
  78. //---- scan for raw site in this firm's building location ----//
  79. Location* locPtr = scan_raw_site();
  80. if( locPtr )
  81. {
  82. site_recno = locPtr->site_recno();
  83. raw_id = site_array[site_recno]->object_id;
  84. reserve_qty = (float) site_array[site_recno]->reserve_qty;
  85. site_array[site_recno]->has_mine = 1;
  86. site_array.untapped_raw_count--;
  87. err_when( site_array.untapped_raw_count < 0 );
  88. }
  89. else
  90. {
  91. site_recno = 0;
  92. raw_id = 0;
  93. reserve_qty = (float) 0;
  94. }
  95. stock_qty = (float) 0;
  96. max_stock_qty = (float) DEFAULT_MINE_MAX_STOCK_QTY;
  97. //-------- increase AI raw count --------//
  98. if( raw_id )
  99. nation_array[nation_recno]->raw_count_array[raw_id-1]++;
  100. }
  101. //----------- End of function FirmMine::init_derived -----------//
  102. //------- Begin of function FirmMine::change_nation ---------//
  103. //
  104. void FirmMine::change_nation(int newNationRecno)
  105. {
  106. if( raw_id )
  107. {
  108. nation_array[nation_recno]->raw_count_array[raw_id-1]--;
  109. err_when( nation_array[nation_recno]->raw_count_array[raw_id-1] < 0 );
  110. nation_array[newNationRecno]->raw_count_array[raw_id-1]++;
  111. }
  112. //-------- change the nation of this firm now ----------//
  113. Firm::change_nation(newNationRecno);
  114. }
  115. //-------- End of function FirmMine::change_nation ---------//
  116. //--------- Begin of function FirmMine::scan_raw_site ---------//
  117. //
  118. Location* FirmMine::scan_raw_site()
  119. {
  120. //---- scan for raw site in this firm's building location ----//
  121. int xLoc, yLoc;
  122. Location* locPtr;
  123. for( yLoc=loc_y1 ; yLoc<=loc_y2 ; yLoc++ )
  124. {
  125. for( xLoc=loc_x1 ; xLoc<=loc_x2 ; xLoc++ )
  126. {
  127. locPtr = world.get_loc(xLoc,yLoc);
  128. if( locPtr->has_site() &&
  129. site_array[locPtr->site_recno()]->site_type == SITE_RAW )
  130. {
  131. return locPtr;
  132. }
  133. }
  134. }
  135. return NULL;
  136. }
  137. //--------- End of function FirmMine::scan_raw_site ---------//
  138. //--------- Begin of function FirmMine::put_info ---------//
  139. //
  140. void FirmMine::put_info(int refreshFlag)
  141. {
  142. disp_basic_info(INFO_Y1, refreshFlag);
  143. if( !should_show_info() )
  144. return;
  145. disp_mine_info(INFO_Y1+52, refreshFlag);
  146. disp_worker_list(INFO_Y1+127, refreshFlag);
  147. disp_worker_info(INFO_Y1+191, refreshFlag);
  148. //---------- display spy button ----------//
  149. disp_spy_button(INFO_X1, INFO_Y1+249, refreshFlag);
  150. }
  151. //----------- End of function FirmMine::put_info -----------//
  152. //--------- Begin of function FirmMine::detect_info ---------//
  153. //
  154. void FirmMine::detect_info()
  155. {
  156. //-------- detect basic info -----------//
  157. if( detect_basic_info() )
  158. return;
  159. //----------- detect worker -----------//
  160. if( detect_worker_list() )
  161. {
  162. disp_mine_info(INFO_Y1+52, INFO_UPDATE);
  163. disp_worker_info(INFO_Y1+191, INFO_UPDATE);
  164. }
  165. //-------- detect spy button ----------//
  166. detect_spy_button();
  167. if( !own_firm() )
  168. return;
  169. }
  170. //----------- End of function FirmMine::detect_info -----------//
  171. //--------- Begin of function FirmMine::disp_mine_info ---------//
  172. //
  173. void FirmMine::disp_mine_info(int dispY1, int refreshFlag)
  174. {
  175. //---------------- paint the panel --------------//
  176. if( refreshFlag == INFO_REPAINT )
  177. vga.d3_panel_up( INFO_X1, dispY1, INFO_X2, dispY1+70);
  178. //------ if there is no natural resource on this location ------//
  179. if( !raw_id )
  180. {
  181. font_san.center_put( INFO_X1, dispY1, INFO_X2, dispY1+70, "No Natural Resources" );
  182. return;
  183. }
  184. //-------------- display mining info -----------//
  185. int x=INFO_X1+4, y=dispY1+4;
  186. raw_res.put_small_raw_icon( x+1, y+1, raw_id );
  187. String str;
  188. str = translate.process("Mining ");
  189. str += raw_res[raw_id]->name;
  190. font_san.disp( x+20, y, str, INFO_X2-2);
  191. y+=16;
  192. font_san.field( x, y, "Monthly Production", x+126, (int) production_30days(), 1, INFO_X2-2, refreshFlag, "MN_PROD");
  193. y+=16;
  194. str = (int) stock_qty;
  195. str += " / ";
  196. str += (int) max_stock_qty;
  197. font_san.field( x, y, "Mined Stock", x+126, str, INFO_X2-2, refreshFlag, "MN_STOCK");
  198. y+=16;
  199. font_san.field( x, y, "Untapped Reserve", x+126, (int) reserve_qty, 1, INFO_X2-2, refreshFlag, "MN_UNTAP");
  200. }
  201. //----------- End of function FirmMine::disp_mine_info -----------//
  202. //--------- Begin of function FirmMine::next_day ---------//
  203. //
  204. void FirmMine::next_day()
  205. {
  206. //----- call next_day() of the base class -----//
  207. Firm::next_day();
  208. //----------- update population -------------//
  209. recruit_worker();
  210. //-------- train up the skill ------------//
  211. update_worker();
  212. //---------------------------------------//
  213. if( info.game_date%PROCESS_GOODS_INTERVAL == firm_recno%PROCESS_GOODS_INTERVAL ) // produce raw materials once every 3 days
  214. {
  215. produce_raw();
  216. set_next_output_firm(); // set next output firm
  217. }
  218. }
  219. //----------- End of function FirmMine::next_day -----------//
  220. //--------- Begin of function FirmMine::next_month ---------//
  221. //
  222. void FirmMine::next_month()
  223. {
  224. last_month_production = cur_month_production;
  225. cur_month_production = (float) 0;
  226. }
  227. //----------- End of function FirmMine::next_month -----------//
  228. //------- Begin of function FirmMine::set_next_output_firm ------//
  229. //
  230. // Set next_output_firm_recno, the recno of the linked firm
  231. // to which this mine is going to output raw materials.
  232. //
  233. void FirmMine::set_next_output_firm()
  234. {
  235. int i, firmRecno, firmId;
  236. for( i=0 ; i<linked_firm_count ; i++ ) // max tries
  237. {
  238. if( ++next_output_link_id > linked_firm_count ) // next firm in the link
  239. next_output_link_id = 1;
  240. if( linked_firm_enable_array[next_output_link_id-1] == LINK_EE )
  241. {
  242. firmRecno = linked_firm_array[next_output_link_id-1];
  243. firmId = firm_array[firmRecno]->firm_id;
  244. if( firmId==FIRM_FACTORY || firmId==FIRM_MARKET )
  245. {
  246. next_output_firm_recno = firmRecno;
  247. return;
  248. }
  249. }
  250. }
  251. next_output_firm_recno = 0; // this mine has no linked output firms
  252. }
  253. //-------- End of function FirmMine::set_next_output_firm ---------//
  254. //--------- Begin of function FirmMine::produce_raw ---------//
  255. //
  256. // Produce raw materials.
  257. //
  258. void FirmMine::produce_raw()
  259. {
  260. //----- if stock capacity reached or reserve exhausted -----//
  261. if( stock_qty == max_stock_qty || reserve_qty==0 )
  262. return;
  263. err_when( reserve_qty < 0 );
  264. err_when( stock_qty > max_stock_qty );
  265. //------- calculate the productivity of the workers -----------//
  266. calc_productivity();
  267. //-------- mine raw materials -------//
  268. float produceQty = (float) 100 * productivity / 100;
  269. produceQty = min( produceQty, reserve_qty );
  270. produceQty = min( produceQty, max_stock_qty-stock_qty );
  271. reserve_qty -= produceQty;
  272. stock_qty += produceQty;
  273. cur_month_production += produceQty;
  274. site_array[site_recno]->reserve_qty = (int) reserve_qty; // update the reserve_qty in site_array
  275. err_when( reserve_qty < 0 );
  276. err_when( stock_qty > max_stock_qty );
  277. //---- add news if run out of raw deposit ----//
  278. if( reserve_qty == 0 )
  279. {
  280. site_array.untapped_raw_count++; // have to restore its first as del_site() will decrease uptapped_raw_count
  281. site_array.del_site(site_recno);
  282. site_recno = 0;
  283. if( nation_recno == nation_array.player_recno )
  284. news_array.raw_exhaust(raw_id, center_x, center_y);
  285. }
  286. }
  287. //----------- End of function FirmMine::produce_raw -----------//
  288. //------- Begin of function FirmMine::draw -----------//
  289. //
  290. // Draw raw materials stocks.
  291. //
  292. void FirmMine::draw(int displayLayer)
  293. {
  294. Firm::draw(displayLayer);
  295. if( !should_show_info() )
  296. return;
  297. if( under_construction )
  298. return;
  299. if( raw_id && displayLayer == 1)
  300. {
  301. int cargoCount = MAX_CARGO * (int)stock_qty / (int)max_stock_qty;
  302. draw_cargo( max(1,cargoCount), raw_res.small_raw_icon(raw_id) );
  303. }
  304. }
  305. //--------- End of function FirmMine::draw -----------//