OREMOTE.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 : OREMOTE.CPP
  21. //Description : Object Remote
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #include <mmsystem.h>
  25. #include <ALL.h>
  26. #include <OBOX.h>
  27. #include <OVGA.h>
  28. #include <OSTR.h>
  29. #include <OFONT.h>
  30. #include <OSYS.h>
  31. #include <OWORLD.h>
  32. #include <OGAME.h>
  33. #include <OPOWER.h>
  34. #include <ONATION.h>
  35. #include <OREMOTE.h>
  36. #include <ODPLAY.h>
  37. #include <OIMMPLAY.h>
  38. #include <OERRCTRL.h>
  39. //---------- Define static variables ----------//
  40. static int connection_failed = 0; // connect_game_disconnect_handler() will set connection_failed to 0
  41. //---------- Define static functions ----------//
  42. static void connect_game_disconnect_handler(DWORD playerId);
  43. //--------- Begin of function Remote::Remote ----------//
  44. Remote::Remote()
  45. {
  46. //--------------------------------------------//
  47. packet_send_count = 0;
  48. packet_receive_count = 0;
  49. //--------------------------------------------//
  50. common_msg_buf = mem_add( COMMON_MSG_BUF_SIZE );
  51. mp_ptr = NULL;
  52. connectivity_mode = 0;
  53. // handle_vga_lock = 1;
  54. handle_vga_lock = 0; // lock vga front in MulitPlayerDP
  55. process_queue_flag = 0;
  56. nation_processing = 0;
  57. // ###### patch begin Gilbert 22/1 #######//
  58. sync_test_level = 0; // 0=disable, bit0= random seed, bit1=crc, bit7=error encountered
  59. // ###### patch end Gilbert 22/1 #######//
  60. }
  61. //--------- End of function Remote::Remote ----------//
  62. //--------- Begin of function Remote::~Remote ----------//
  63. Remote::~Remote()
  64. {
  65. deinit();
  66. mem_del( common_msg_buf );
  67. }
  68. //--------- End of function Remote::~Remote ----------//
  69. //--------- Begin of function Remote::init ----------//
  70. //
  71. // <int> connectivityMode - connectivity mode can be:
  72. // REMOTE_WSOCK_TCPIP, REMOTE_WSOCK_IPX,
  73. // REMOTE_MODEM, REMOTE_NULL_MODEM
  74. //
  75. void Remote::init(MultiPlayerType *mp)
  76. {
  77. if( connectivity_mode )
  78. deinit();
  79. connectivity_mode = 1;
  80. poll_msg_flag = 0;
  81. mp_ptr = mp;
  82. // ###### patch begin Gilbert 22/1 #######//
  83. sync_test_level = (m.is_file_exist("SYNC1.SYS") ? 1 : 0)
  84. | (m.is_file_exist("SYNC2.SYS") ? 2 : 0);
  85. // 0=disable, bit0= random seed, bit1=crc
  86. // ###### patch end Gilbert 22/1 #######//
  87. reset_process_frame_delay();
  88. set_alternating_send(1); // send every frame
  89. }
  90. //--------- End of function Remote::init ----------//
  91. //--------- Begin of function Remote::deinit ----------//
  92. void Remote::deinit()
  93. {
  94. if( connectivity_mode )
  95. {
  96. connectivity_mode = 0;
  97. }
  98. // ###### patch begin Gilbert 22/1 #######//
  99. sync_test_level = 0; // 0=disable, bit0= random seed, bit1=crc
  100. // ###### patch end Gilbert 22/1 #######//
  101. }
  102. //--------- End of function Remote::deinit ----------//
  103. //--------- Begin of function Remote::create_game ---------//
  104. //
  105. int Remote::create_game()
  106. {
  107. //--------- initialize session parameters ---------//
  108. is_host = TRUE;
  109. return 1;
  110. }
  111. //--------- End of function Remote::create_game ---------//
  112. //--------- Begin of function Remote::connect_game ---------//
  113. //
  114. int Remote::connect_game()
  115. {
  116. is_host = FALSE;
  117. return 1;
  118. }
  119. //--------- End of function Remote::connect_game ---------//
  120. //-------- Begin of function Remote::is_enable ---------//
  121. //
  122. int Remote::is_enable()
  123. {
  124. return connectivity_mode;
  125. }
  126. //--------- End of function Remote::is_enable ----------//
  127. /*
  128. //-------- Begin of function Remote::can_start_game ---------//
  129. //
  130. int Remote::can_start_game()
  131. {
  132. err_when(!connectivity_mode);
  133. return wsock_ptr->can_start_game();
  134. }
  135. //--------- End of function Remote::can_start_game ----------//
  136. */
  137. //-------- Begin of function Remote::number_of_opponent ---------//
  138. //
  139. int Remote::number_of_opponent()
  140. {
  141. err_when(!connectivity_mode);
  142. //return wsock_ptr->number_of_player;
  143. return mp_ptr->player_pool.size()-1;
  144. }
  145. //--------- End of function Remote::number_of_opponent ----------//
  146. //-------- Begin of function Remote::self_player_id ---------//
  147. //
  148. DWORD Remote::self_player_id()
  149. {
  150. err_when(!connectivity_mode);
  151. // return wsock_ptr->self_player_id;
  152. return mp_ptr->my_player_id;
  153. }
  154. //--------- End of function Remote::self_player_id ----------//
  155. /*
  156. //-------- Begin of function Remote::set_disconnect_handler ---------//
  157. //
  158. void Remote::set_disconnect_handler(DisconnectFP disconnectFP)
  159. {
  160. err_when(!connectivity_mode);
  161. wsock_ptr->set_disconnect_handler(disconnectFP);
  162. }
  163. //--------- End of function Remote::set_disconnect_handler ----------//
  164. */
  165. //-------- Begin of function Remote::start_game ---------//
  166. //
  167. void Remote::start_game()
  168. {
  169. err_when(!connectivity_mode);
  170. // wsock_ptr->start_game();
  171. }
  172. //--------- End of function Remote::start_game ----------//
  173. //----- Begin of function connect_game_disconnect_handler -----//
  174. //
  175. // Disconnection handler for Remote::connect_game()
  176. //
  177. static void connect_game_disconnect_handler(DWORD playerId)
  178. {
  179. connection_failed = 1;
  180. }
  181. //------ End of function connect_game_disconnect_handler ------//
  182. //-------- Begin of function Remote::reset_process_frame_delay ---------//
  183. void Remote::reset_process_frame_delay()
  184. {
  185. // process_frame_delay = 1;
  186. process_frame_delay = MAX_PROCESS_FRAME_DELAY;
  187. err_when( process_frame_delay > MAX_PROCESS_FRAME_DELAY );
  188. }
  189. //-------- End of function Remote::reset_process_frame_delay ---------//
  190. //-------- Begin of function Remote::get_process_frame_delay ---------//
  191. int Remote::get_process_frame_delay()
  192. {
  193. return process_frame_delay;
  194. }
  195. //-------- End of function Remote::get_process_frame_delay ---------//
  196. //-------- Begin of function Remote::set_process_frame_delay ---------//
  197. void Remote::set_process_frame_delay(int f)
  198. {
  199. // must not be called after init_receive_queue()
  200. if( f < 1 )
  201. f = 1;
  202. if( f > MAX_PROCESS_FRAME_DELAY )
  203. f = MAX_PROCESS_FRAME_DELAY;
  204. process_frame_delay = f;
  205. }
  206. //-------- End of function Remote::set_process_frame_delay ---------//
  207. //-------- Begin of function Remote::calc_process_frame_delay ---------//
  208. int Remote::calc_process_frame_delay(int milliSecond)
  209. {
  210. // expected : f = 1 + milliSecond / 100;
  211. // int f = 1 + (milliSecond+20)/100;
  212. int f = 1 + (milliSecond+40)/60;
  213. if( f < 1 )
  214. f = 1;
  215. if( f > MAX_PROCESS_FRAME_DELAY )
  216. f = MAX_PROCESS_FRAME_DELAY;
  217. return f;
  218. }
  219. //-------- End of function Remote::calc_process_frame_delay ---------//
  220. //-------- Begin of function Remote::set_alternating_send ---------//
  221. void Remote::set_alternating_send(int rate)
  222. {
  223. if( rate <= 0 || rate > 100 || (sync_test_level != 0 && rate != 1) )
  224. {
  225. err_here();
  226. // must not have any sync testing to enable alternating send
  227. // tell_random_seed and crc checking must send data every frame
  228. }
  229. else
  230. alternating_send_rate = rate;
  231. }
  232. //-------- End of function Remote::set_alternating_send ---------//
  233. //-------- Begin of function Remote::set_alternating_send ---------//
  234. int Remote::get_alternating_send()
  235. {
  236. return alternating_send_rate;
  237. }
  238. //-------- End of function Remote::set_alternating_send ---------//
  239. //-------- Begin of function Remote::has_send_frame ---------//
  240. int Remote::has_send_frame(int nationRecno, DWORD frameCount)
  241. {
  242. return ((frameCount + nationRecno) % alternating_send_rate) == 0;
  243. }
  244. //-------- End of function Remote::has_send_frame ---------//
  245. //-------- Begin of function Remote::next_send_frame ---------//
  246. // if has_send_frame is true, return frameCount
  247. // otherwise return the next frame which has_send_frame return true
  248. DWORD Remote::next_send_frame(int nationRecno, DWORD frameCount)
  249. {
  250. DWORD remainder = ((frameCount + nationRecno) % alternating_send_rate);
  251. if(remainder == 0)
  252. return frameCount;
  253. else
  254. return frameCount + alternating_send_rate - remainder;
  255. }
  256. //-------- End of function Remote::next_send_frame ---------//