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 
21 //Filename    : OREMOTE.CPP
22 //Description : Object Remote
23 
24 #include <ALL.h>
25 #include <OBOX.h>
26 #include <OVGA.h>
27 #include <OSTR.h>
28 #include <OFONT.h>
29 #include <OSYS.h>
30 #include <OWORLD.h>
31 #include <OGAME.h>
32 #include <OPOWER.h>
33 #include <ONATION.h>
34 #include <OREMOTE.h>
35 #include <multiplayer.h>
36 #include <OERRCTRL.h>
37 #include <ReplayFile.h>
38 #include <FilePath.h>
39 #include <ConfigAdv.h>
40 
41 //--------- Begin of function Remote::Remote ----------//
42 
Remote()43 Remote::Remote()
44 {
45 	//--------------------------------------------//
46 
47 	packet_send_count    = 0;
48 	packet_receive_count = 0;
49 
50 	//--------------------------------------------//
51 
52 	common_msg_buf    = mem_add( COMMON_MSG_BUF_SIZE );
53 
54 	mp_ptr				= NULL;
55 	connectivity_mode = MODE_DISABLED;
56 	//	handle_vga_lock   = 1;
57 	handle_vga_lock   = 0;			// lock vga front in MulitPlayerDP
58 	process_queue_flag = 0;
59 	nation_processing = 0;
60 	// ###### patch begin Gilbert 22/1 #######//
61 	sync_test_level = 0;			// 0=disable, bit0= random seed, bit1=crc, bit7=error encountered
62 	// ###### patch end Gilbert 22/1 #######//
63 }
64 //--------- End of function Remote::Remote ----------//
65 
66 
67 //--------- Begin of function Remote::~Remote ----------//
68 
~Remote()69 Remote::~Remote()
70 {
71 	deinit();
72 
73 	mem_del( common_msg_buf  );
74 }
75 //--------- End of function Remote::~Remote ----------//
76 
77 
78 //--------- Begin of function Remote::init ----------//
79 //
80 // <int> connectivityMode - connectivity mode can be:
81 //									 REMOTE_WSOCK_TCPIP, REMOTE_WSOCK_IPX,
82 //									 REMOTE_MODEM, REMOTE_NULL_MODEM
83 //
init(MultiPlayer * mp)84 void Remote::init(MultiPlayer *mp)
85 {
86 	if( connectivity_mode )
87 		deinit();
88 
89 	connectivity_mode = MODE_MP_ENABLED;
90 	poll_msg_flag = 0;
91 	mp_ptr = mp;
92 
93 	// ###### patch begin Gilbert 22/1 #######//
94 	// 0=disable, bit0= random seed, bit1=crc
95 	sync_test_level = 0;
96 	if( config_adv.remote_compare_random_seed || misc.is_file_exist("SYNC1.SYS") )
97 		sync_test_level |= 1;
98 	if( config_adv.remote_compare_object_crc || misc.is_file_exist("SYNC2.SYS") )
99 		sync_test_level |= 2;
100 	// ###### patch end Gilbert 22/1 #######//
101 
102 	reset_process_frame_delay();
103 
104 	set_alternating_send(1);		// send every frame
105 }
106 //--------- End of function Remote::init ----------//
107 
108 
109 //--------- Begin of function Remote::init_replay_load ----------//
110 // Can be called independently.
init_replay_load(char * full_path,NewNationPara * mpGame,int * playerCount)111 int Remote::init_replay_load(char *full_path, NewNationPara *mpGame, int *playerCount)
112 {
113 	if( connectivity_mode )
114 		deinit();
115 
116 	if (!replay.open_read(full_path, mpGame, playerCount))
117 		return 0;
118 
119 	// 0=disable, bit0= random seed, bit1=crc
120 	sync_test_level = 0;
121 	if( config_adv.remote_compare_random_seed || misc.is_file_exist("SYNC1.SYS") )
122 		sync_test_level |= 1;
123 	if( config_adv.remote_compare_object_crc || misc.is_file_exist("SYNC2.SYS") )
124 		sync_test_level |= 2;
125 
126 	remote.connectivity_mode = Remote::MODE_REPLAY;
127 
128 	return 1;
129 }
130 //--------- End of function Remote::init_replay_load ----------//
131 
132 
133 //--------- Begin of function Remote::init_replay_save ----------//
134 // Call this after init(). Uses a default filename.
init_replay_save(NewNationPara * mpGame,int playerCount)135 void Remote::init_replay_save(NewNationPara *mpGame, int playerCount)
136 {
137 	err_when( connectivity_mode != MODE_MP_ENABLED );
138 
139 	FilePath full_path(sys.dir_config);
140 	full_path += "NONAME.RPL";
141 	if( !full_path.error_flag )
142 		replay.open_write(full_path, mpGame, playerCount);
143 }
144 //--------- End of function Remote::init_replay_save ----------//
145 
146 
147 //--------- Begin of function Remote::deinit ----------//
148 
deinit()149 void Remote::deinit()
150 {
151 	if( connectivity_mode )
152 	{
153 		connectivity_mode = MODE_DISABLED;
154 	}
155 	// ###### patch begin Gilbert 22/1 #######//
156 	sync_test_level = 0;			// 0=disable, bit0= random seed, bit1=crc
157 	// ###### patch end Gilbert 22/1 #######//
158 	replay.close();
159 }
160 //--------- End of function Remote::deinit ----------//
161 
162 
163 //--------- Begin of function Remote::create_game ---------//
164 //
create_game()165 int Remote::create_game()
166 {
167 	//--------- initialize session parameters ---------//
168 
169 	is_host = 1;
170 
171 	return 1;
172 }
173 //--------- End of function Remote::create_game ---------//
174 
175 
176 //--------- Begin of function Remote::connect_game ---------//
177 //
connect_game()178 int Remote::connect_game()
179 {
180 	is_host = 0;
181 	return 1;
182 }
183 //--------- End of function Remote::connect_game ---------//
184 
185 
186 //-------- Begin of function Remote::is_enable ---------//
187 //
is_enable()188 int Remote::is_enable()
189 {
190 	return connectivity_mode == MODE_MP_ENABLED;
191 }
192 //--------- End of function Remote::is_enable ----------//
193 
194 
195 //-------- Begin of function Remote::is_replay ---------//
196 //
is_replay()197 int Remote::is_replay()
198 {
199 	return connectivity_mode == MODE_REPLAY;
200 }
201 //--------- End of function Remote::is_replay ----------//
202 
203 
204 //-------- Begin of function Remote::is_replay_end ---------//
205 //
is_replay_end()206 int Remote::is_replay_end()
207 {
208 	return connectivity_mode == MODE_REPLAY_END;
209 }
210 //--------- End of function Remote::is_replay_end ----------//
211 
212 
213 /*
214 //-------- Begin of function Remote::can_start_game ---------//
215 //
216 int Remote::can_start_game()
217 {
218 	err_when(connectivity_mode != MODE_MP_ENABLED);
219 
220 	return wsock_ptr->can_start_game();
221 }
222 //--------- End of function Remote::can_start_game ----------//
223 */
224 
225 //-------- Begin of function Remote::number_of_opponent ---------//
226 //
number_of_opponent()227 int Remote::number_of_opponent()
228 {
229 	err_when(connectivity_mode != MODE_MP_ENABLED);
230 
231 	//return wsock_ptr->number_of_player;
232 	return mp_ptr->get_player_count()-1;
233 }
234 //--------- End of function Remote::number_of_opponent ----------//
235 
236 
237 //-------- Begin of function Remote::self_player_id ---------//
238 //
self_player_id()239 PID_TYPE Remote::self_player_id()
240 {
241 	err_when(connectivity_mode != MODE_MP_ENABLED);
242 
243 	// return wsock_ptr->self_player_id;
244 	return mp_ptr->get_my_player_id();
245 }
246 //--------- End of function Remote::self_player_id ----------//
247 
248 /*
249 //-------- Begin of function Remote::set_disconnect_handler ---------//
250 //
251 void Remote::set_disconnect_handler(DisconnectFP disconnectFP)
252 {
253 	err_when(connectivity_mode != MODE_MP_ENABLED);
254 
255 	wsock_ptr->set_disconnect_handler(disconnectFP);
256 }
257 //--------- End of function Remote::set_disconnect_handler ----------//
258 */
259 
260 
261 //-------- Begin of function Remote::start_game ---------//
262 //
start_game()263 void Remote::start_game()
264 {
265 	err_when(connectivity_mode != MODE_MP_ENABLED);
266 
267 	// wsock_ptr->start_game();
268 }
269 //--------- End of function Remote::start_game ----------//
270 
271 
272 //-------- Begin of function Remote::reset_process_frame_delay ---------//
reset_process_frame_delay()273 void Remote::reset_process_frame_delay()
274 {
275 	// process_frame_delay = 1;
276 	process_frame_delay = MAX_PROCESS_FRAME_DELAY;
277 	err_when( process_frame_delay > MAX_PROCESS_FRAME_DELAY );
278 }
279 //-------- End of function Remote::reset_process_frame_delay ---------//
280 
281 
282 //-------- Begin of function Remote::get_process_frame_delay ---------//
get_process_frame_delay()283 int Remote::get_process_frame_delay()
284 {
285 	return process_frame_delay;
286 }
287 //-------- End of function Remote::get_process_frame_delay ---------//
288 
289 
290 //-------- Begin of function Remote::set_process_frame_delay ---------//
set_process_frame_delay(int f)291 void Remote::set_process_frame_delay(int f)
292 {
293 	// must not be called after init_receive_queue()
294 
295 	if( f < 1 )
296 		f = 1;
297 	if( f > MAX_PROCESS_FRAME_DELAY )
298 		f = MAX_PROCESS_FRAME_DELAY;
299 	process_frame_delay = f;
300 }
301 //-------- End of function Remote::set_process_frame_delay ---------//
302 
303 
304 //-------- Begin of function Remote::calc_process_frame_delay ---------//
calc_process_frame_delay(int milliSecond)305 int Remote::calc_process_frame_delay(int milliSecond)
306 {
307 	// expected : f = 1 + milliSecond / 100;
308 	// int f = 1 + (milliSecond+20)/100;
309 	int f = 1 + (milliSecond+40)/60;
310 	if( f < 1 )
311 		f = 1;
312 	if( f > MAX_PROCESS_FRAME_DELAY )
313 		f = MAX_PROCESS_FRAME_DELAY;
314 	return f;
315 }
316 //-------- End of function Remote::calc_process_frame_delay ---------//
317 
318 
319 //-------- Begin of function Remote::set_alternating_send ---------//
set_alternating_send(int rate)320 void Remote::set_alternating_send(int rate)
321 {
322 	if( rate <= 0 || rate > 100 || (sync_test_level != 0 && rate != 1) )
323 	{
324 		err_here();
325 		// must not have any sync testing to enable alternating send
326 		// tell_random_seed and crc checking must send data every frame
327 	}
328 	else
329 		alternating_send_rate = rate;
330 }
331 //-------- End of function Remote::set_alternating_send ---------//
332 
333 
334 //-------- Begin of function Remote::set_alternating_send ---------//
get_alternating_send()335 int Remote::get_alternating_send()
336 {
337 	return alternating_send_rate;
338 }
339 //-------- End of function Remote::set_alternating_send ---------//
340 
341 
342 //-------- Begin of function Remote::has_send_frame ---------//
has_send_frame(int nationRecno,uint32_t frameCount)343 int Remote::has_send_frame(int nationRecno, uint32_t frameCount)
344 {
345 	return ((frameCount + nationRecno) % alternating_send_rate) == 0;
346 }
347 //-------- End of function Remote::has_send_frame ---------//
348 
349 
350 //-------- Begin of function Remote::next_send_frame ---------//
351 // if has_send_frame is true, return frameCount
352 // otherwise return the next frame which has_send_frame return true
next_send_frame(int nationRecno,uint32_t frameCount)353 uint32_t Remote::next_send_frame(int nationRecno, uint32_t frameCount)
354 {
355 	uint32_t remainder = ((frameCount + nationRecno) % alternating_send_rate);
356 	if(remainder == 0)
357 		return frameCount;
358 	else
359 		return frameCount + alternating_send_rate - remainder;
360 }
361 //-------- End of function Remote::next_send_frame ---------//
362