OF_FACT.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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_FACT.CPP
  21. //Description : Firm Factory
  22. #include <OINFO.h>
  23. #include <OVGA.h>
  24. #include <OSTR.h>
  25. #include <OUNIT.h>
  26. #include <OGAME.h>
  27. #include <OFONT.h>
  28. #include <OMOUSE.h>
  29. #include <OBUTT3D.h>
  30. #include <ONATION.h>
  31. #include <ORAWRES.h>
  32. #include <ORACERES.h>
  33. #include <OTOWNRES.h>
  34. #include <OWORLD.h>
  35. #include <OF_MINE.h>
  36. #include <OF_MARK.h>
  37. #include <OF_FACT.h>
  38. #include <OREMOTE.h>
  39. #include <OSE.h>
  40. //-------- define constant ---------//
  41. #define DEFAULT_FACTORY_MAX_STOCK_QTY 500
  42. #define DEFAULT_FACTORY_MAX_RAW_STOCK_QTY 500
  43. //------- define static vars -------//
  44. static Button3D button_change_production;
  45. //--------- Begin of function FirmFactory::FirmFactory ---------//
  46. //
  47. FirmFactory::FirmFactory()
  48. {
  49. firm_skill_id = SKILL_MFT;
  50. product_raw_id = 1;
  51. cur_month_production = (float) 0;
  52. last_month_production = (float) 0;
  53. stock_qty = (float) 0;
  54. max_stock_qty = (float) DEFAULT_FACTORY_MAX_STOCK_QTY;
  55. raw_stock_qty = (float) 0;
  56. max_raw_stock_qty = (float) DEFAULT_FACTORY_MAX_RAW_STOCK_QTY;
  57. next_output_link_id = 0;
  58. next_output_firm_recno = 0;
  59. }
  60. //----------- End of function FirmFactory::FirmFactory -----------//
  61. //--------- Begin of function FirmFactory::~FirmFactory ---------//
  62. //
  63. FirmFactory::~FirmFactory()
  64. {
  65. }
  66. //----------- End of function FirmFactory::~FirmFactory -----------//
  67. //--------- Begin of function FirmFactory::init_derived ---------//
  68. //
  69. void FirmFactory::init_derived()
  70. {
  71. auto_set_product();
  72. }
  73. //----------- End of function FirmFactory::init_derived -----------//
  74. //------- Begin of function FirmFactory::auto_set_product --------//
  75. //
  76. void FirmFactory::auto_set_product()
  77. {
  78. //---- automatically set the factory product type -----//
  79. int i, j, k, rawId, firmDistance;
  80. Firm* firmPtr, *otherFirm;
  81. FirmMarket* firmMarket;
  82. int minDistance=0x7FFF;
  83. for( i=0 ; i<linked_firm_count ; i++ )
  84. {
  85. firmPtr = firm_array[linked_firm_array[i]];
  86. firmDistance = m.points_distance( firmPtr->center_x, firmPtr->center_y,
  87. center_x, center_y );
  88. //----------- if the firm is a mine ----------//
  89. if( firmPtr->firm_id == FIRM_MINE )
  90. {
  91. rawId = ((FirmMine*)firmPtr)->raw_id;
  92. if( !rawId )
  93. continue;
  94. //--- if this mine hasn't been used by any factories yet, then select it ---//
  95. for( j=firmPtr->linked_firm_count-1 ; j>=0 ; j-- )
  96. {
  97. if( !firmPtr->linked_firm_enable_array[j] )
  98. continue;
  99. otherFirm = firm_array[ firmPtr->linked_firm_array[j] ];
  100. if( otherFirm->firm_id == FIRM_FACTORY &&
  101. ((FirmFactory*)otherFirm)->product_raw_id == rawId )
  102. {
  103. break;
  104. }
  105. }
  106. if( j<0 )
  107. {
  108. product_raw_id = rawId;
  109. return;
  110. }
  111. //--------------------------------//
  112. if( firmDistance < minDistance )
  113. {
  114. product_raw_id = ((FirmMine*)firmPtr)->raw_id;
  115. minDistance = firmDistance;
  116. }
  117. }
  118. //----------- if the firm is a market place ----------//
  119. else if( firmPtr->firm_id == FIRM_MARKET )
  120. {
  121. firmMarket = (FirmMarket*) firmPtr;
  122. for( j=0 ; j<MAX_MARKET_GOODS ; j++ )
  123. {
  124. rawId = firmMarket->market_goods_array[j].raw_id;
  125. if( !rawId )
  126. continue;
  127. //--- if this raw material in this market hasn't been used by any factories yet, then select it ---//
  128. for( k=firmPtr->linked_firm_count-1 ; k>=0 ; k-- )
  129. {
  130. if( firmPtr->linked_firm_enable_array[k] != LINK_EE )
  131. continue;
  132. otherFirm = firm_array[ firmPtr->linked_firm_array[k] ];
  133. if( otherFirm->firm_id == FIRM_FACTORY &&
  134. ((FirmFactory*)otherFirm)->product_raw_id == rawId )
  135. {
  136. break;
  137. }
  138. }
  139. if( k<0 )
  140. {
  141. product_raw_id = rawId;
  142. return;
  143. }
  144. //-----------------------------------//
  145. if( firmDistance < minDistance )
  146. {
  147. product_raw_id = rawId;
  148. minDistance = firmDistance;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. //------- End of function FirmFactory::auto_set_product -------//
  155. //--------- Begin of function FirmFactory::put_info ---------//
  156. //
  157. void FirmFactory::put_info(int refreshFlag)
  158. {
  159. //---------- display info ------------//
  160. disp_basic_info(INFO_Y1, refreshFlag);
  161. if( !should_show_info() )
  162. return;
  163. disp_factory_info(INFO_Y1+54, refreshFlag);
  164. disp_worker_list(INFO_Y1+126, refreshFlag);
  165. disp_worker_info(INFO_Y1+190, refreshFlag);
  166. //------ display button -------//
  167. int x;
  168. if( own_firm() && refreshFlag==INFO_REPAINT )
  169. {
  170. button_change_production.paint( INFO_X1, INFO_Y1+248, 'A', "CHGPROD" );
  171. x = INFO_X1+BUTTON_ACTION_WIDTH;
  172. }
  173. else
  174. x = INFO_X1;
  175. //---------- display spy button ----------//
  176. disp_spy_button(x, INFO_Y1+248, refreshFlag);
  177. }
  178. //----------- End of function FirmFactory::put_info -----------//
  179. //--------- Begin of function FirmFactory::detect_info ---------//
  180. //
  181. void FirmFactory::detect_info()
  182. {
  183. //-------- detect basic info -----------//
  184. if( detect_basic_info() )
  185. return;
  186. //-------- detect workers ----------//
  187. if( detect_worker_list() ) // detect this when: it's the player's firm or the player has spies in this firm
  188. {
  189. disp_worker_list(INFO_Y1+126, INFO_UPDATE);
  190. disp_worker_info(INFO_Y1+190, INFO_UPDATE);
  191. }
  192. //-------- detect spy button ----------//
  193. detect_spy_button();
  194. if( !own_firm() )
  195. return;
  196. //---- detect change production button -----//
  197. if( button_change_production.detect() )
  198. {
  199. change_production();
  200. disp_factory_info(INFO_Y1+54, INFO_UPDATE);
  201. // ##### begin Gilbert 25/9 ######//
  202. se_ctrl.immediate_sound("TURN_ON");
  203. // ##### end Gilbert 25/9 ######//
  204. }
  205. }
  206. //----------- End of function FirmFactory::detect_info -----------//
  207. //--------- Begin of function FirmFactory::next_day ---------//
  208. //
  209. void FirmFactory::next_day()
  210. {
  211. //----- call next_day() of the base class -----//
  212. Firm::next_day();
  213. //----------- update population -------------//
  214. recruit_worker();
  215. //-------- train up the skill ------------//
  216. update_worker();
  217. //--------- daily manufacturing activities ---------//
  218. if( info.game_date%PROCESS_GOODS_INTERVAL == firm_recno%PROCESS_GOODS_INTERVAL )
  219. {
  220. input_raw();
  221. production();
  222. set_next_output_firm(); // set next output firm
  223. }
  224. }
  225. //----------- End of function FirmFactory::next_day -----------//
  226. //--------- Begin of function FirmFactory::next_month ---------//
  227. //
  228. void FirmFactory::next_month()
  229. {
  230. last_month_production = cur_month_production;
  231. cur_month_production = (float) 0;
  232. }
  233. //----------- End of function FirmFactory::next_month -----------//
  234. //------- Begin of function FirmFactory::draw -----------//
  235. //
  236. // Draw product stocks.
  237. //
  238. void FirmFactory::draw(int displayLayer)
  239. {
  240. Firm::draw(displayLayer);
  241. if( !should_show_info() )
  242. return;
  243. if( under_construction )
  244. return;
  245. if( product_raw_id && displayLayer == 1 )
  246. {
  247. int cargoCount = MAX_CARGO * (int)stock_qty / (int)max_stock_qty;
  248. draw_cargo( max(1,cargoCount), raw_res.small_product_icon(product_raw_id) );
  249. }
  250. }
  251. //--------- End of function FirmFactory::draw -----------//
  252. //--------- Begin of function FirmFactory::disp_factory_info ---------//
  253. //
  254. void FirmFactory::disp_factory_info(int dispY1, int refreshFlag)
  255. {
  256. //---------------- paint the panel --------------//
  257. if( refreshFlag == INFO_REPAINT )
  258. vga.d3_panel_up( INFO_X1, dispY1, INFO_X2, dispY1+70);
  259. //---------- display production info -------------//
  260. int x=INFO_X1+4, y=dispY1+4;
  261. vga_front.put_bitmap_trans( x+1, y+1, raw_res.small_product_icon(product_raw_id) );
  262. String str;
  263. str = translate.process("Producing ");
  264. #if(defined(FRENCH))
  265. char productName[20];
  266. strcpy(productName, raw_res[product_raw_id]->name);
  267. strcat(productName, " Products");
  268. str += translate.process(productName);
  269. #else
  270. str += raw_res[product_raw_id]->name;
  271. str += translate.process(" Products");
  272. #endif
  273. font_san.use_max_height(); // make sure the old text is replaced completely
  274. #if(defined(FRENCH) || defined(SPANISH))
  275. font_san.disp( x+15, y, str, INFO_X2-1);
  276. #else
  277. font_san.disp( x+20, y, str, INFO_X2-2);
  278. #endif
  279. font_san.use_std_height();
  280. y+=16;
  281. font_san.field( x, y, "Monthly Production", x+133, (int) production_30days(), 1, INFO_X2-2, refreshFlag, "FC_PROD" );
  282. y+=16;
  283. str = (int) raw_stock_qty;
  284. str += " / ";
  285. str += (int) max_raw_stock_qty;
  286. font_san.field( x, y, "Raw Material Stock", x+133, str, INFO_X2-2, refreshFlag, "FC_RAW" );
  287. y+=16;
  288. str = (int) stock_qty;
  289. str += " / ";
  290. str += (int) max_stock_qty;
  291. font_san.field( x, y, "Product Stock", x+133, str, INFO_X2-2, refreshFlag, "FC_PROD");
  292. }
  293. //----------- End of function FirmFactory::disp_factory_info -----------//
  294. //------ Begin of function FirmFactory::change_production -------//
  295. //
  296. void FirmFactory::change_production()
  297. {
  298. if( remote.is_enable() )
  299. {
  300. // packet structure : <firm recno> <product id>
  301. short *shortPtr = (short *)remote.new_send_queue_msg(MSG_F_FACTORY_CHG_PROD, 2*sizeof(short) );
  302. shortPtr[0] = firm_recno;
  303. shortPtr[1] = product_raw_id >= MAX_PRODUCT ? 1 : product_raw_id + 1;
  304. }
  305. else
  306. {
  307. // update RemoteMsg::factory_change_product
  308. if( ++product_raw_id > MAX_PRODUCT )
  309. product_raw_id = 1;
  310. set_production( product_raw_id==MAX_PRODUCT ? 1 : product_raw_id+1 );
  311. }
  312. }
  313. //--------- End of function FirmFactory::change_production --------//
  314. //------ Begin of function FirmFactory::set_production -------//
  315. //
  316. void FirmFactory::set_production(int newProductId)
  317. {
  318. product_raw_id = newProductId;
  319. stock_qty = (float) 0;
  320. max_stock_qty = (float) DEFAULT_FACTORY_MAX_STOCK_QTY;
  321. raw_stock_qty = (float) 0;
  322. max_raw_stock_qty = (float) DEFAULT_FACTORY_MAX_RAW_STOCK_QTY;
  323. }
  324. //--------- End of function FirmFactory::set_production --------//
  325. //--------- Begin of function FirmFactory::production ---------//
  326. //
  327. void FirmFactory::production()
  328. {
  329. //----- if stock capacity reached or reserve exhausted -----//
  330. if( stock_qty == max_stock_qty )
  331. return;
  332. err_when( stock_qty > max_stock_qty );
  333. //------- calculate the productivity of the workers -----------//
  334. calc_productivity();
  335. //------- generate revenue for the nation --------//
  336. float produceQty = (float) 20 * productivity / 100;
  337. produceQty = min( produceQty, max_stock_qty-stock_qty );
  338. manufacture(produceQty);
  339. }
  340. //----------- End of function FirmFactory::production -----------//
  341. //--------- Begin of function FirmFactory::input_raw ---------//
  342. //
  343. // Input raw materials from mines and market places.
  344. //
  345. void FirmFactory::input_raw()
  346. {
  347. //------ scan for a firm to input raw materials --------//
  348. int i, j;
  349. float inputQty;
  350. Firm* firmPtr;
  351. FirmMine* firmMine;
  352. FirmMarket* firmMarket;
  353. Nation* nationPtr = nation_array[nation_recno];
  354. MarketGoods* marketGoods;
  355. for( i=0 ; i<linked_firm_count ; i++ )
  356. {
  357. if( linked_firm_enable_array[i] != LINK_EE )
  358. continue;
  359. firmPtr = firm_array[linked_firm_array[i]];
  360. //----------- check if the firm is a mine ----------//
  361. if( firmPtr->firm_id != FIRM_MINE && firmPtr->firm_id != FIRM_MARKET )
  362. continue;
  363. //--------- if the firm is a mine ------------//
  364. if( firmPtr->firm_id == FIRM_MINE )
  365. {
  366. firmMine = (FirmMine*) firmPtr;
  367. if( firmMine->next_output_firm_recno == firm_recno &&
  368. firmMine->raw_id==product_raw_id && firmMine->stock_qty > 0 )
  369. {
  370. inputQty = min( firmMine->stock_qty, max_raw_stock_qty - raw_stock_qty );
  371. if( firmMine->nation_recno != nation_recno ) // make sure it has the cash to pay for the raw materials
  372. inputQty = min( inputQty, nationPtr->cash/RAW_PRICE );
  373. if( inputQty > 0 )
  374. {
  375. firmMine->stock_qty -= inputQty;
  376. raw_stock_qty += inputQty;
  377. err_when( raw_stock_qty > max_raw_stock_qty );
  378. //---- import from other nation -----//
  379. if( firmMine->nation_recno != nation_recno )
  380. nationPtr->import_goods(IMPORT_RAW, firmMine->nation_recno, inputQty*RAW_PRICE );
  381. }
  382. }
  383. }
  384. //------- if the firm is a market place --------//
  385. if( firmPtr->firm_id == FIRM_MARKET )
  386. {
  387. firmMarket = (FirmMarket*) firmPtr;
  388. if( firmMarket->next_output_firm_recno == firm_recno )
  389. {
  390. marketGoods = firmMarket->market_goods_array;
  391. for( j=0 ; j<MAX_MARKET_GOODS ; j++, marketGoods++ )
  392. {
  393. if( marketGoods->raw_id == product_raw_id &&
  394. marketGoods->stock_qty > 0 )
  395. {
  396. inputQty = min( marketGoods->stock_qty, max_raw_stock_qty - raw_stock_qty );
  397. if( firmMarket->nation_recno != nation_recno ) // make sure it has the cash to pay for the raw materials
  398. inputQty = min( inputQty, nationPtr->cash/RAW_PRICE );
  399. if( inputQty > 0 )
  400. {
  401. marketGoods->stock_qty -= inputQty;
  402. raw_stock_qty += inputQty;
  403. err_when( raw_stock_qty > max_raw_stock_qty );
  404. //---- import from other nation -----//
  405. if( firmMarket->nation_recno != nation_recno )
  406. nationPtr->import_goods(IMPORT_RAW, firmMarket->nation_recno, inputQty*RAW_PRICE );
  407. }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. }
  414. //----------- End of function FirmFactory::input_raw -----------//
  415. //--------- Begin of function FirmFactory::manufacture ---------//
  416. //
  417. // Input raw materials into the factory
  418. //
  419. // <float> maxMftQty - maximum qty this firm can manufacture in this call.
  420. //
  421. void FirmFactory::manufacture(float maxMftQty)
  422. {
  423. if( raw_stock_qty==0 )
  424. return;
  425. float inputQty;
  426. inputQty = min(raw_stock_qty, maxMftQty);
  427. inputQty = min(inputQty, max_stock_qty-stock_qty);
  428. if( inputQty <= 0 )
  429. return;
  430. raw_stock_qty -= inputQty;
  431. stock_qty += inputQty;
  432. cur_month_production += inputQty;
  433. }
  434. //----------- End of function FirmFactory::manufacture -----------//
  435. //------- Begin of function FirmFactory::set_next_output_firm ------//
  436. //
  437. // Set next_output_firm_recno, the recno of the linked firm
  438. // to which this factory is going to output products.
  439. //
  440. void FirmFactory::set_next_output_firm()
  441. {
  442. int i, firmRecno, firmId;
  443. for( i=0 ; i<linked_firm_count ; i++ ) // max tries
  444. {
  445. if( ++next_output_link_id > linked_firm_count ) // next firm in the link
  446. next_output_link_id = 1;
  447. if( linked_firm_enable_array[next_output_link_id-1] == LINK_EE )
  448. {
  449. firmRecno = linked_firm_array[next_output_link_id-1];
  450. firmId = firm_array[firmRecno]->firm_id;
  451. if( firmId==FIRM_MARKET )
  452. {
  453. next_output_firm_recno = firmRecno;
  454. return;
  455. }
  456. }
  457. }
  458. next_output_firm_recno = 0; // this mine has no linked output firms
  459. }
  460. //-------- End of function FirmFactory::set_next_output_firm ---------//