OUNITAMT.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 : OUNITAMT.CPP
  21. //Description : Object UnitArray, trading code of caravans and ships for multiplayer game
  22. //Owner : Alex
  23. //------------------------------------------------------------------------------------------------------//
  24. // Parameters used for updating cargo info. of trade units in muliplayer game:
  25. //
  26. // 1) caravan
  27. // <short> mp_selected_caravan_recno[MAX_NATION]
  28. // <int> mp_selected_caravan_count
  29. // <char> mp_first_frame_to_select_caravan
  30. // <short> mp_pre_selected_caravan_recno
  31. //
  32. // 2) marine that can trade
  33. // <short> mp_selected_ship_recno[MAX_NATION]
  34. // <int> mp_selected_ship_count
  35. // <char> mp_first_frame_to_select_ship
  36. // <short> mp_pre_selected_ship_recno
  37. //
  38. // Functions used in multiplayer game
  39. //
  40. // 1) common
  41. // update_selected_trade_unit_info(...)
  42. //
  43. // 2) caravan
  44. // mp_mark_selected_caravan(...)
  45. // mp_get_selected_caravan_count(...)
  46. // mp_reset_selected_caravan_count(...)
  47. // mp_add_selected_caravan(...)
  48. // mp_is_selected_caravan(...)
  49. //
  50. // 3) marine that can trade
  51. // mp_mark_selected_ship(...)
  52. // mp_get_selected_ship_count(...)
  53. // mp_reset_selected_ship_count(...)
  54. // mp_add_selected_ship(...)
  55. // mp_is_selected_ship(...)
  56. //
  57. // In mulitplayer game, any order to the units from one player to others player must experience delay
  58. // of one frame. In order to update information of selected trade units in real time, the design used
  59. // in this part of programming is obviously different from other general multiplayer messages. The
  60. // documentation is only for caravans, since the method used for marine is absolutely equilvance.
  61. //
  62. // In this version of trading system, the player can only select one trade unit to view the unit detail
  63. // at any time and there are maximum 7 players in the game. Thus, an array of size MAX_NATION is used
  64. // to store the record number of trade units selected by each players. A count, mp_selected_caravan_count,
  65. // is used to count the number of selected units in the array, mp_selected_caravan_recno.
  66. //
  67. // The process that a player select a unit can be divided into three part, first frame to select the
  68. // unit, keep selecting the unit and unselect the unit. If the player selects a caravan, the flag
  69. // mp_first_frame_to_select_caravan set to TRUE, mp_pre_selected_caravan_recno stores the record number
  70. // of this unit. From now on, the message MSG_U_CARA_SELECTED for the unit keep on sending to other
  71. // players every frame until the unit is unselected. Since the flag mp_first_frame_to_select_caravan is
  72. // set to TRUE, this caravan information will not be shown in this frame.
  73. //
  74. // In the next frame, the flag mp_first_frame_to_select_caravan is set back to FLASE. The message
  75. // MSG_U_CARA_SELECTED sent in the previous frame reaches other players' channel. The message takes effect
  76. // to add a recno in the array mp_selected_caravan_recno. For each record number in the array, the system
  77. // update the caravan information. The process continue and the cargo information is updated in real-time
  78. // for the selected trade units.
  79. //
  80. //
  81. // Functions description:
  82. //
  83. // mp_mark_selected_caravan()
  84. // is called every frame to sent message to other players to notice them the recno of the trade unit selected
  85. //
  86. // mp_get_selected_caravan_count()
  87. // for debug only
  88. //
  89. // mp_reset_selected_caravan_count()
  90. // reset the array mp_selected_caravan_recno.
  91. //
  92. // mp_add_selected_caravan()
  93. // handle message sent by other players to add recno in the array mp_selected_caravan_recno
  94. //
  95. // mp_is_selected_caravan()
  96. // check whether the unit is selected, recno in the array mp_selected_caravan_recno
  97. //
  98. //------------------------------------------------------------------------------------------------------//
  99. #include <OUNIT.h>
  100. #include <OREMOTE.h>
  101. #include <OU_MARI.h>
  102. #include <ONATIONA.h>
  103. #include <OCONFIG.h>
  104. //------------ define static variables ------------//
  105. static short mp_selected_caravan_recno[MAX_NATION] = {0};
  106. static int mp_selected_caravan_count = 0;
  107. static short mp_selected_ship_recno[MAX_NATION] = {0};
  108. static int mp_selected_ship_count = 0;
  109. //--------- Begin of function UnitArray::mp_mark_selected_caravan ---------//
  110. void UnitArray::mp_mark_selected_caravan()
  111. {
  112. //---------- send remote messages for selected caravan and ship ------------//
  113. if(remote.is_enable())
  114. {
  115. if(selected_recno)
  116. {
  117. Unit *selectedUnitPtr = (Unit*)get_ptr(selected_recno);
  118. //### begin alex 19/9 ###//
  119. //if(selectedUnitPtr->unit_id==UNIT_CARAVAN && selectedUnitPtr->nation_recno==nation_array.player_recno)
  120. if(selectedUnitPtr->unit_id==UNIT_CARAVAN && ((nation_array.player_recno &&
  121. selectedUnitPtr->nation_recno==nation_array.player_recno) || config.show_ai_info))
  122. //#### end alex 19/9 ####//
  123. {
  124. //---------- send message for multiplayer ---------//
  125. short* shortPtr = (short*) remote.new_send_queue_msg(MSG_U_CARA_SELECTED, sizeof(short));
  126. shortPtr[0] = selectedUnitPtr->sprite_recno;
  127. if(selected_recno==mp_pre_selected_caravan_recno)
  128. mp_first_frame_to_select_caravan = 0;
  129. else
  130. {
  131. mp_pre_selected_caravan_recno = selected_recno;
  132. mp_first_frame_to_select_caravan = 1;
  133. }
  134. }
  135. else
  136. mp_pre_selected_caravan_recno = 0;
  137. }
  138. else
  139. mp_pre_selected_caravan_recno = 0;
  140. mp_reset_selected_caravan_count();
  141. }
  142. }
  143. //----------- End of function UnitArray::mp_mark_selected_caravan -----------//
  144. //--------- Begin of function UnitArray::mp_get_selected_caravan_count ---------//
  145. // function only for debug
  146. //
  147. int UnitArray::mp_get_selected_caravan_count()
  148. {
  149. //return selected_caravan_count;
  150. if(mp_selected_caravan_count)
  151. return mp_selected_caravan_recno[0];
  152. else
  153. return 0;
  154. }
  155. //----------- End of function UnitArray::mp_get_selected_caravan_count -----------//
  156. //--------- Begin of function UnitArray::mp_reset_selected_caravan_count ---------//
  157. void UnitArray::mp_reset_selected_caravan_count()
  158. {
  159. mp_selected_caravan_count = 0;
  160. }
  161. //----------- End of function Unit::mp_reset_selected_caravan_count -----------//
  162. //--------- Begin of function UnitArray::mp_add_selected_caravan ---------//
  163. // the specified recno is one of the caravans selected in the previous frame
  164. // by one of the players
  165. //
  166. void UnitArray::mp_add_selected_caravan(short unitRecno)
  167. {
  168. err_when(mp_selected_caravan_count>=MAX_NATION);
  169. mp_selected_caravan_recno[mp_selected_caravan_count++] = unitRecno;
  170. }
  171. //----------- End of function UnitArray::mp_add_selected_caravan -----------//
  172. //--------- Begin of function UnitArray::mp_is_selected_caravan ---------//
  173. // return 1 if the caravan is selected in the previous frame by one of the players
  174. // return 0 otherwise
  175. //
  176. int UnitArray::mp_is_selected_caravan(short unitRecno)
  177. {
  178. err_when(mp_selected_caravan_count>=MAX_NATION);
  179. for(int i=mp_selected_caravan_count-1; i>=0; --i)
  180. {
  181. if(mp_selected_caravan_recno[i] == unitRecno)
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. //----------- End of function UnitArray::mp_is_selected_caravan -----------//
  187. //--------- Begin of function UnitArray::mp_mark_selected_ship ---------//
  188. void UnitArray::mp_mark_selected_ship()
  189. {
  190. //---------- send remote messages for selected ship ------------//
  191. if(remote.is_enable())
  192. {
  193. if(selected_recno)
  194. {
  195. Unit *selectedUnitPtr = (Unit*)get_ptr(selected_recno);
  196. //### begin alex 19/9 ###//
  197. //if(unit_res[selectedUnitPtr->unit_id]->unit_class == UNIT_CLASS_SHIP &&
  198. // selectedUnitPtr->unit_id != UNIT_TRANSPORT && selectedUnitPtr->nation_recno == nation_array.player_recno)
  199. if(unit_res[selectedUnitPtr->unit_id]->unit_class == UNIT_CLASS_SHIP &&
  200. selectedUnitPtr->unit_id != UNIT_TRANSPORT && ((nation_array.player_recno &&
  201. selectedUnitPtr->nation_recno == nation_array.player_recno) || config.show_ai_info))
  202. //#### end alex 19/9 ####//
  203. {
  204. if(((UnitMarine*)selectedUnitPtr)->auto_mode)
  205. {
  206. //---------- send message for multiplayer ---------//
  207. short* shortPtr = (short*) remote.new_send_queue_msg(MSG_U_SHIP_SELECTED, sizeof(short));
  208. shortPtr[0] = selectedUnitPtr->sprite_recno;
  209. if(selected_recno==mp_pre_selected_ship_recno)
  210. mp_first_frame_to_select_ship = 0;
  211. else
  212. {
  213. mp_pre_selected_ship_recno = selected_recno;
  214. mp_first_frame_to_select_ship = 1;
  215. }
  216. }
  217. else
  218. mp_pre_selected_ship_recno = 0;
  219. }
  220. else
  221. mp_pre_selected_ship_recno = 0;
  222. }
  223. else
  224. mp_pre_selected_ship_recno = 0;
  225. mp_reset_selected_ship_count();
  226. }
  227. }
  228. //----------- End of function UnitArray::mp_mark_selected_ship -----------//
  229. //--------- Begin of function UnitArray::mp_get_selected_ship_count ---------//
  230. // function only for debug
  231. //
  232. int UnitArray::mp_get_selected_ship_count()
  233. {
  234. //return selected_ship_count;
  235. if(mp_selected_ship_count)
  236. return mp_selected_ship_recno[0];
  237. else
  238. return 0;
  239. }
  240. //----------- End of function UnitArray::mp_get_selected_ship_count -----------//
  241. //--------- Begin of function UnitArray::mp_reset_selected_ship_count ---------//
  242. void UnitArray::mp_reset_selected_ship_count()
  243. {
  244. mp_selected_ship_count = 0;
  245. }
  246. //----------- End of function Unit::mp_reset_selected_ship_count -----------//
  247. //--------- Begin of function UnitArray::mp_add_selected_ship ---------//
  248. // the specified recno is one of the ships selected in the previous frame
  249. // by one of the players
  250. //
  251. void UnitArray::mp_add_selected_ship(short unitRecno)
  252. {
  253. err_when(mp_selected_ship_count>=MAX_NATION);
  254. mp_selected_ship_recno[mp_selected_ship_count++] = unitRecno;
  255. }
  256. //----------- End of function UnitArray::mp_add_selected_ship -----------//
  257. //--------- Begin of function UnitArray::mp_is_selected_ship ---------//
  258. // return 1 if the ship is selected in the previous frame by one of the players
  259. // return 0 otherwise
  260. //
  261. int UnitArray::mp_is_selected_ship(short unitRecno)
  262. {
  263. err_when(mp_selected_ship_count>=MAX_NATION);
  264. for(int i=mp_selected_ship_count-1; i>=0; --i)
  265. {
  266. if(mp_selected_ship_recno[i] == unitRecno)
  267. return 1;
  268. }
  269. return 0;
  270. }
  271. //----------- End of function UnitArray::mp_is_selected_ship -----------//