OU_CARAS.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <OU_CARA.h>
  21. #include <OF_MINE.h>
  22. #include <OF_FACT.h>
  23. #include <OF_MARK.h>
  24. //--------- Begin of function TradeStop::pick_up_set_auto ---------//
  25. void TradeStop::pick_up_set_auto()
  26. {
  27. memset(pick_up_array, 0, sizeof(char)*MAX_PICK_UP_GOODS);
  28. pick_up_type = AUTO_PICK_UP;
  29. }
  30. //----------- End of function TradeStop::pick_up_set_auto -----------//
  31. //--------- Begin of function TradeStop::pick_up_set_none ---------//
  32. void TradeStop::pick_up_set_none()
  33. {
  34. memset(pick_up_array, 0, sizeof(char)*MAX_PICK_UP_GOODS);
  35. pick_up_type = NO_PICK_UP;
  36. }
  37. //----------- End of function TradeStop::pick_up_set_none -----------//
  38. //--------- Begin of function TradeStop::pick_up_toggle ---------//
  39. void TradeStop::pick_up_toggle(int pos)
  40. {
  41. char *posPtr = &pick_up_array[pos-1];
  42. if(*posPtr)
  43. {
  44. *posPtr = 0;
  45. char firmId = firm_array[firm_recno]->firm_id;
  46. if(firmId==FIRM_MARKET || firmId==FIRM_HARBOR)
  47. {
  48. int i, allZero;
  49. for(i=0, allZero=1; i<MAX_PICK_UP_GOODS; ++i)
  50. {
  51. if(pick_up_array[i])
  52. {
  53. allZero = 0;
  54. pick_up_type = i+1;
  55. break;
  56. }
  57. }
  58. if(allZero)
  59. pick_up_type = NO_PICK_UP;
  60. }
  61. else
  62. pick_up_type = NO_PICK_UP;
  63. }
  64. else
  65. {
  66. *posPtr = 1;
  67. pick_up_type = pos; // that means selective
  68. }
  69. }
  70. //----------- End of function TradeStop::pick_up_toggle -----------//
  71. //--------- Begin of function TradeStop::num_of_pick_up_goods ---------//
  72. int TradeStop::num_of_pick_up_goods(char *enableTable)
  73. {
  74. int num=0;
  75. //for(int i=0; i<MAX_PICK_UP_GOODS; i++)
  76. for(int i=1; i<=MAX_PICK_UP_GOODS; i++)
  77. {
  78. if(enableTable[i])
  79. num++;
  80. }
  81. return num;
  82. }
  83. //----------- End of function TradeStop::num_of_pick_up_goods -----------//
  84. //--------- Begin of function TradeStop::mp_pick_up_set_auto ---------//
  85. void TradeStop::mp_pick_up_set_auto(char *enableTable)
  86. {
  87. if(pick_up_type==AUTO_PICK_UP)
  88. return;
  89. //---------------------------------------------------------------------------//
  90. // 1) none -> auto, only if more than one kind of cargo can be selected
  91. // 2) selective -> auto, only if num of goods > 1
  92. //---------------------------------------------------------------------------//
  93. if(pick_up_type==NO_PICK_UP || num_of_pick_up_goods(enableTable)>1)
  94. {
  95. memset(pick_up_array, 0, sizeof(char)*MAX_PICK_UP_GOODS);
  96. pick_up_type = AUTO_PICK_UP;
  97. }
  98. }
  99. //----------- End of function TradeStop::mp_pick_up_set_auto -----------//
  100. //--------- Begin of function TradeStop::mp_pick_up_set_none ---------//
  101. void TradeStop::mp_pick_up_set_none(char *enableTable)
  102. {
  103. if(pick_up_type==NO_PICK_UP)
  104. return;
  105. //---------------------------------------------------------------------------//
  106. // 1) none -> none, only if more than one kind of cargo can be selected
  107. // 2) selective -> none, only if num of goods > 1
  108. //---------------------------------------------------------------------------//
  109. if(pick_up_type==AUTO_PICK_UP || num_of_pick_up_goods(enableTable)>1)
  110. {
  111. memset(pick_up_array, 0, sizeof(char)*MAX_PICK_UP_GOODS);
  112. pick_up_type = NO_PICK_UP;
  113. }
  114. }
  115. //----------- End of function TradeStop::mp_pick_up_set_none -----------//
  116. //--------- Begin of function CaravanStop::update_pick_up ---------//
  117. // enableFlag - represent which button will be displayed
  118. //
  119. int CaravanStop::update_pick_up(char *enableFlag)
  120. {
  121. #ifdef DBEUG
  122. if(pick_up_type == AUTO_PICK_UP || pick_up_type == NO_PICK_UP)
  123. {
  124. for(int di=0; di<MAX_PICK_UP_GOODS; ++di)
  125. err_when(pick_up_array[di]);
  126. }
  127. #endif
  128. err_when(firm_array.is_deleted(firm_recno));
  129. static char dummyBuffer[MAX_GOODS_SELECT_BUTTON];
  130. if(enableFlag==NULL)
  131. enableFlag = dummyBuffer;
  132. memset(enableFlag, 0, sizeof(char)*MAX_GOODS_SELECT_BUTTON);
  133. Firm *firmPtr = firm_array[firm_recno];
  134. MarketGoods *marketGoodsPtr;
  135. int goodsNum=0; // represent the number of cargo displayed in the menu for this stop
  136. int firstGoodsId = 0;
  137. int id, i, selectiveMode;
  138. switch(firmPtr->firm_id)
  139. {
  140. case FIRM_MINE:
  141. err_when(pick_up_type==AUTO_PICK_UP);
  142. id = ((FirmMine*)firmPtr)->raw_id+PICK_UP_RAW_FIRST-1;
  143. if(id)
  144. {
  145. goodsNum = enableFlag[id] = 1;
  146. if(!pick_up_array[id-1])
  147. pick_up_set_none(); // nothing can be taken if no cargo is matched
  148. }
  149. break;
  150. case FIRM_FACTORY:
  151. err_when(pick_up_type==AUTO_PICK_UP);
  152. id = ((FirmFactory*)firmPtr)->product_raw_id+PICK_UP_PRODUCT_FIRST-1;
  153. if(id)
  154. {
  155. goodsNum = enableFlag[id] = 1;
  156. if(!pick_up_array[id-1])
  157. pick_up_set_none(); // nothing can be taken if no cargo is matched
  158. }
  159. break;
  160. case FIRM_MARKET:
  161. marketGoodsPtr = ((FirmMarket*) firmPtr)->market_goods_array;
  162. selectiveMode = (pick_up_type!=AUTO_PICK_UP && pick_up_type!=NO_PICK_UP);
  163. for(i=1; i<=MAX_MARKET_GOODS; i++, marketGoodsPtr++)
  164. {
  165. if((id = marketGoodsPtr->raw_id)) // 1-3
  166. {
  167. goodsNum++;
  168. enableFlag[id] = 1;
  169. if(firstGoodsId==0)
  170. firstGoodsId = id;
  171. }
  172. else if((id = marketGoodsPtr->product_raw_id)) // 1-3
  173. {
  174. id += MAX_RAW; // 4-6
  175. goodsNum++;
  176. enableFlag[id] = 1;
  177. if(firstGoodsId==0)
  178. firstGoodsId = id;
  179. }
  180. }
  181. for(i=0; i<MAX_PICK_UP_GOODS; ++i)
  182. {
  183. if(pick_up_array[i])
  184. {
  185. if(enableFlag[i+1]==0)
  186. pick_up_array[i] = 0;
  187. }
  188. }
  189. break;
  190. default: err_here();
  191. break;
  192. }
  193. if(goodsNum==0 && pick_up_type!=NO_PICK_UP)
  194. pick_up_set_none();
  195. if(goodsNum==1 && pick_up_type==AUTO_PICK_UP)
  196. {
  197. pick_up_type = firstGoodsId; // change to selective
  198. pick_up_array[pick_up_type-1] = 1;
  199. }
  200. err_when(pick_up_type==AUTO_PICK_UP && goodsNum==0);
  201. #ifdef DEBUG
  202. int debugCount = 0;
  203. if(pick_up_type!=AUTO_PICK_UP && pick_up_type!=NO_PICK_UP)
  204. {
  205. for(int di=1; di<=MAX_PICK_UP_GOODS; ++di)
  206. {
  207. if(enableFlag[di])
  208. debugCount++;
  209. }
  210. err_when(debugCount==0);
  211. }
  212. #endif
  213. return goodsNum;
  214. }
  215. //----------- End of function CaravanStop::update_pick_up -----------//
  216. //--------- Begin of function CaravanStop::mp_pick_up_toggle ---------//
  217. void CaravanStop::mp_pick_up_toggle(int pos)
  218. {
  219. err_when(firm_array.is_deleted(firm_recno));
  220. //------------------------------------------------------------//
  221. // verify for cargo button
  222. // if not exist then return else call pick_up_toggle(int pos)
  223. //------------------------------------------------------------//
  224. Firm *firmPtr = firm_array[firm_recno];
  225. MarketGoods *marketGoodsPtr;
  226. int cargoExist = 0;
  227. int id, i;
  228. switch(firmPtr->firm_id)
  229. {
  230. case FIRM_MINE:
  231. id = ((FirmMine*)firmPtr)->raw_id+PICK_UP_RAW_FIRST-1;
  232. if(id==pos)
  233. cargoExist = 1;
  234. break;
  235. case FIRM_FACTORY:
  236. id = ((FirmFactory*)firmPtr)->product_raw_id+PICK_UP_PRODUCT_FIRST-1;
  237. if(id==pos)
  238. cargoExist = 1;
  239. break;
  240. case FIRM_MARKET:
  241. marketGoodsPtr = ((FirmMarket*) firmPtr)->market_goods_array;
  242. for(i=1; i<=MAX_MARKET_GOODS; i++, marketGoodsPtr++)
  243. {
  244. if((id = marketGoodsPtr->raw_id)) // 1-3
  245. {
  246. if(id==pos)
  247. {
  248. cargoExist = 1;
  249. break;
  250. }
  251. }
  252. else if((id = marketGoodsPtr->product_raw_id)) // 1-3
  253. {
  254. id += MAX_PRODUCT; // 4-6
  255. if(id==pos)
  256. {
  257. cargoExist = 1;
  258. break;
  259. }
  260. }
  261. }
  262. break;
  263. default: err_here();
  264. break;
  265. }
  266. if(cargoExist)
  267. pick_up_toggle(pos);
  268. }
  269. //----------- End of function CaravanStop::mp_pick_up_toggle -----------//