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.H
22 //Description : Header file of object Remote
23 
24 #ifndef __OREMOTE_H
25 #define __OREMOTE_H
26 
27 #include <MPTYPES.h>
28 #include <OREMOTEQ.h>
29 #include <ReplayFile.h>
30 
31 //---------- Define message id. ---------//
32 
33 enum { FIRST_REMOTE_MSG_ID = 0x25D3 };
34 
35 enum { MSG_QUEUE_HEADER=FIRST_REMOTE_MSG_ID,
36 		 MSG_QUEUE_TRAILER,
37 		 MSG_NEW_NATION,
38 		 MSG_UPDATE_GAME_SETTING,
39 		 MSG_START_GAME,
40 		 MSG_NEXT_FRAME,
41 		 MSG_REQUEST_RESEND,
42 		 MSG_TELL_SEND_TIME,
43 		 MSG_SET_SPEED,
44 		 MSG_TELL_RANDOM_SEED,
45 		 MSG_REQUEST_SAVE,
46 		 MSG_PLAYER_QUIT,
47 
48 		 MSG_UNIT_STOP,
49 		 MSG_UNIT_MOVE,
50 		 MSG_UNIT_SET_FORCE_MOVE,
51 		 MSG_UNIT_ATTACK,
52 		 MSG_UNIT_ASSIGN,
53 		 MSG_UNIT_CHANGE_NATION,
54 		 MSG_UNIT_BUILD_FIRM,
55 		 MSG_UNIT_BURN,
56 		 MSG_UNITS_SETTLE,
57 		 MSG_UNIT_SET_GUARD,
58 		 MSG_UNIT_SET_RANK,
59 		 MSG_UNIT_DISMOUNT,
60 		 MSG_UNIT_REWARD,
61 		 MSG_UNITS_TRANSFORM,
62 		 MSG_UNIT_RESIGN,
63 		 MSG_UNITS_ASSIGN_TO_SHIP,
64 		 MSG_UNITS_SHIP_TO_BEACH,
65 		 MSG_UNIT_SUCCEED_KING,
66 		 MSG_UNITS_RETURN_CAMP,
67 		 MSG_U_CARA_CHANGE_GOODS,
68 		 MSG_U_CARA_SET_STOP,
69 		 MSG_U_CARA_DEL_STOP,
70 		 MSG_U_CARA_SELECTED,
71 		 MSG_U_SHIP_UNLOAD_UNIT,
72 		 MSG_U_SHIP_UNLOAD_ALL_UNITS,
73 		 MSG_U_SHIP_CHANGE_GOODS,
74 		 MSG_U_SHIP_SET_STOP,
75 		 MSG_U_SHIP_DEL_STOP,
76 		 MSG_U_SHIP_CHANGE_MODE,
77 		 MSG_U_SHIP_SELECTED,
78 		 MSG_U_GOD_CAST,
79 		 MSG_UNIT_SPY_NATION,
80 		 MSG_UNIT_SPY_NOTIFY_CLOAKED_NATION,
81 		 MSG_UNIT_CHANGE_AGGRESSIVE_MODE,
82 		 MSG_SPY_CHANGE_NOTIFY_FLAG,
83 
84 		 //##### trevor 15/10 ######//
85 		 MSG_SPY_ASSASSINATE,
86 		 //##### trevor 15/10 ######//
87 
88 		 //### begin alex 14/10 ###//
89 		 MSG_UNIT_ADD_WAY_POINT,
90 		 //#### end alex 14/10 ####//
91 
92 		 MSG_FIRM_SELL,
93 		 MSG_FIRM_CANCEL,
94 		 MSG_FIRM_DESTRUCT,
95 		 MSG_FIRM_SET_REPAIR,
96 		 MSG_FIRM_TRAIN_LEVEL,
97 		 MSG_FIRM_MOBL_WORKER,
98 		 MSG_FIRM_MOBL_ALL_WORKERS,
99 		 MSG_FIRM_MOBL_OVERSEER,
100 		 MSG_FIRM_MOBL_BUILDER,
101 		 MSG_FIRM_TOGGLE_LINK_FIRM,
102 		 MSG_FIRM_TOGGLE_LINK_TOWN,
103 		 MSG_FIRM_PULL_TOWN_PEOPLE,
104 		 MSG_FIRM_SET_WORKER_HOME,
105 		 MSG_FIRM_BRIBE,
106 		 MSG_FIRM_CAPTURE,
107 
108 		//### trevor 2/10 ###//
109 		 MSG_FIRM_REWARD,
110 		 MSG_F_CAMP_PATROL,
111 		 MSG_F_CAMP_TOGGLE_PATROL,
112 		 MSG_F_INN_HIRE,
113 		//### trevor 2/10 ###//
114 		 MSG_F_MARKET_SCRAP,
115 		 MSG_F_MARKET_HIRE_CARA,
116 		 MSG_F_RESEARCH_START,
117 		 MSG_F_WAR_BUILD_WEAPON,
118 		 MSG_F_WAR_CANCEL_WEAPON,
119 		 MSG_F_WAR_SKIP_WEAPON,
120 		 MSG_F_HARBOR_BUILD_SHIP,
121 		 MSG_F_HARBOR_SAIL_SHIP,
122 		 MSG_F_HARBOR_SKIP_SHIP,
123 		 MSG_F_FACTORY_CHG_PROD,
124 		 MSG_F_BASE_MOBL_PRAYER,
125 		 MSG_F_BASE_INVOKE_GOD,
126 
127 		 MSG_TOWN_RECRUIT,
128 		 MSG_TOWN_SKIP_RECRUIT,
129 		 MSG_TOWN_MIGRATE,
130 		 MSG_TOWN_COLLECT_TAX,
131 		 MSG_TOWN_REWARD,
132 		 MSG_TOWN_TOGGLE_LINK_FIRM,
133 		 MSG_TOWN_TOGGLE_LINK_TOWN,
134 		 MSG_TOWN_AUTO_TAX,
135 		 MSG_TOWN_AUTO_GRANT,
136 		 MSG_TOWN_GRANT_INDEPENDENT,
137 
138 		 MSG_WALL_BUILD,
139 		 MSG_WALL_DESTRUCT,
140 
141 		 MSG_SPY_CYCLE_ACTION,
142 		 MSG_SPY_LEAVE_TOWN,
143 		 MSG_SPY_LEAVE_FIRM,
144 		 MSG_SPY_CAPTURE_FIRM,
145 		 MSG_SPY_DROP_IDENTITY,
146 		 MSG_SPY_REWARD,
147 		 MSG_SPY_EXPOSED,
148 
149 		 MSG_SEND_TALK_MSG,			// for diplomacy
150 		 MSG_REPLY_TALK_MSG,
151 		 MSG_NATION_CONTACT,
152 		 MSG_NATION_SET_SHOULD_ATTACK,
153 		 MSG_CHAT,
154 
155 		 MSG_COMPARE_NATION,
156 		 MSG_COMPARE_UNIT,
157 		 MSG_COMPARE_FIRM,
158 		 MSG_COMPARE_TOWN,
159 		 MSG_COMPARE_BULLET,
160 		 MSG_COMPARE_REBEL,
161 		 MSG_COMPARE_SPY,
162 		 MSG_COMPARE_TALK,
163 
164 		 MSG_U_CARA_COPY_ROUTE,
165 
166 		 LAST_REMOTE_MSG_ID			// keep this item last
167 	  };
168 
169 enum { REMOTE_MSG_TYPE_COUNT= LAST_REMOTE_MSG_ID - FIRST_REMOTE_MSG_ID };
170 
171 
172 
173 //--------- Define struct RemoteMsg ---------//
174 
175 struct RemoteMsg
176 {
177 public:
178 	uint32_t	id;
179 	char  data_buf[1];
180 
181 public:
182 	void	process_msg();
183 
184 	//------ remote message processing functions ------//
185 
186 	void	queue_header();
187 	void	queue_trailer();
188 	void 	new_nation();
189 	void	update_game_setting();
190 	void	start_game();
191 	void	next_frame();
192 	void  request_resend();
193 	void	tell_send_time();
194 	void  set_speed();
195 	void	tell_random_seed();
196 	void	request_save_game();
197 	void	player_quit();
198 
199 	void	unit_stop();
200 	void	unit_move();
201 	void	unit_set_force_move();
202 	void	unit_attack();
203 	void	unit_assign();
204 	void  unit_change_nation();
205 
206 	void	unit_build_firm();
207 	void	unit_burn();
208 	void	units_settle();
209 	void	unit_set_guard();
210 	void	unit_set_rank();
211 	void	unit_dismount();
212 	void	unit_reward();
213 	void	units_transform();
214 	void	unit_resign();
215 	void	units_assign_to_ship();
216 	void	units_ship_to_beach();
217 	void	unit_succeed_king();
218 	void	units_return_camp();
219 	void	caravan_change_goods();
220 	void	caravan_set_stop();
221 	void	caravan_del_stop();
222 	void	caravan_selected();
223 	void	ship_unload_unit();
224 	void	ship_unload_all_units();
225 	void	ship_change_goods();
226 	void	ship_set_stop();
227 	void	ship_del_stop();
228 	void	ship_change_mode();
229 	void	ship_selected();
230 	void	god_cast();
231 	void	change_spy_nation();
232 	void	notify_cloaked_nation();
233 	void  unit_change_aggressive_mode();
234 	void  spy_change_notify_flag();
235 
236 	//##### trevor 15/10 #####//
237 	void  spy_assassinate();
238 	//##### trevor 15/10 #####//
239 
240 	//### begin alex 14/10 ###//
241 	void	unit_add_way_point();
242 	//#### end alex 14/10 ####//
243 
244 	void	firm_sell();
245 	void	firm_cancel();				// cancel construction
246 	void	firm_destruct();
247 	void	firm_set_repair();
248 	void	firm_train_level();
249 	void	mobilize_worker();
250 	void	mobilize_all_workers();
251 	void	mobilize_overseer();
252 	void	mobilize_builder();
253 	void	firm_toggle_link_firm();
254 	void	firm_toggle_link_town();
255 	void	firm_pull_town_people();
256 	void	firm_set_worker_home();
257 	void	firm_bribe();
258 	void	firm_capture();
259 	void	camp_patrol();
260 	void	toggle_camp_patrol();
261 	void	firm_reward();
262 	void	inn_hire();
263 	void	market_scrap();
264 	void	market_hire_caravan();
265 	void	research_start();
266 	void	build_weapon();
267 	void	cancel_weapon();
268 	void	skip_build_weapon();
269 	void	build_ship();
270 	void	sail_ship();
271 	void	skip_build_ship();
272 	void	factory_change_product();
273 	void	base_mobilize_prayer();
274 	void	invoke_god();
275 
276 	void	town_recruit();
277 	void	town_skip_recruit();
278 	void	town_migrate();
279 	void	town_collect_tax();
280 	void	town_reward();
281 	void	town_toggle_link_firm();
282 	void	town_toggle_link_town();
283 	void	town_auto_tax();
284 	void	town_auto_grant();
285 	void	town_grant_independent();
286 
287 	void	wall_build();
288 	void	wall_destruct();
289 
290 	void	spy_cycle_action();
291 	void	spy_leave_town();
292 	void	spy_leave_firm();
293 	void	spy_capture_firm();
294 	void	spy_drop_identity();
295 	void	spy_reward();
296 	void	spy_exposed();
297 
298 	void	send_talk_msg();
299 	void	reply_talk_msg();
300 	void	nation_contact();
301 	void	nation_set_should_attack();
302 
303 	 //##### trevor 30/9 #######//
304 	void	chat();
305 	 //##### trevor 30/9 #######//
306 
307 	void	compare_remote_object();
308 
309 	void	caravan_copy_route();
310 };
311 
312 //----------- Define class Remote -----------//
313 
314 class MultiPlayer;
315 
316 class Remote
317 {
318 public:
319 	enum { COMMON_MSG_BUF_SIZE	    	 = 1024,			// Remote allocates a common RemoteMsg object with a data buffer of this size.
320 			 SEND_QUEUE_BUF_SIZE		  	 = 1024, 		// The default queue buffer size
321 			 SEND_QUEUE_BUF_INC_SIZE 	 = 1024,			// If the queue is full, expand with this size
322 			 RECEIVE_QUEUE_BUF_SIZE		 = 8192, 		// The default queue buffer size
323 			 RECEIVE_QUEUE_BUF_INC_SIZE = 2048,			// If the queue is full, expand with this size
324 //			 MAX_PROCESS_FRAME_DELAY = 5,					// process player action 1 frame later
325 			 MAX_PROCESS_FRAME_DELAY = 8,					// process player action 1 frame later
326 			 SEND_QUEUE_BACKUP = MAX_PROCESS_FRAME_DELAY+4,
327 			 RECEIVE_QUEUE_BACKUP = (MAX_PROCESS_FRAME_DELAY+1)*2,
328 		  };
329 
330 	enum { MODE_DISABLED = 0, MODE_MP_ENABLED, MODE_REPLAY, MODE_REPLAY_END };
331 
332 public:
333 	char				is_host;
334 	char				handle_vga_lock;
335 	int				connectivity_mode;
336 	int				poll_msg_flag;
337 	//	Wsock*			wsock_ptr;
338 	MultiPlayer *mp_ptr;
339 
340 	//--------- send queue -----------//
341 
342 	RemoteQueue		send_queue[SEND_QUEUE_BACKUP];		// 0 for the latest, other for backup
343 	uint32_t	send_frame_count[SEND_QUEUE_BACKUP];
344 /*
345 	char*				send_queue_buf;
346 	char*				send_queue_ptr;
347 	int				send_queue_buf_size;
348 	int			   send_queued_size;
349 
350 	//------- send backup queue --------//
351 
352 	char*				backup_queue_buf;			// backup copy of the send_queue_buf
353 	int				backup_queued_size;		// for handling resend request
354 	DWORD         	backup_frame_count;
355 
356 	char*				backup2_queue_buf;		// backup copy of the send_queue_buf
357 	int				backup2_queued_size;		// for handling resend request
358 	DWORD         	backup2_frame_count;
359 */
360 	//------- receive queue ---------//
361 
362 	RemoteQueue		receive_queue[RECEIVE_QUEUE_BACKUP];
363 	uint32_t	receive_frame_count[RECEIVE_QUEUE_BACKUP];
364 /*
365 	char*				receive_queue_buf;
366 	char*				receive_queue_ptr;
367 	int				receive_queue_buf_size;
368 	int			   receive_queued_size;
369 
370 	char*				receive2_queue_buf;
371 	char*				receive2_queue_ptr;
372 	int				receive2_queue_buf_size;
373 	int			   receive2_queued_size;
374 */
375 	char				process_queue_flag;
376 
377 	//-------------------------------//
378 
379 	int				packet_send_count;
380 	int				packet_receive_count;
381 
382 	//-------------------------------//
383 	short				nation_processing;		// used in process_receive_queue
384 
385 	char				save_file_name[FilePath::MAX_FILE_PATH];
386 
387 	char				*common_msg_buf;
388 	// ###### patch begin Gilbert 22/1 #######//
389 	char				sync_test_level;			// 0=disable, bit0= random seed, bit1=crc
390 	// ###### patch end Gilbert 22/1 #######//
391 	int				process_frame_delay;
392 
393 	// --------- alternating send frame --------//
394 	int				alternating_send_rate;	// 1=every frame, 2=send one frame per two frames...
395 
396 	ReplayFile			replay;
397 
398 public:
399 	Remote();
400 	~Remote();
401 
402 	void			init(MultiPlayer *mp);
403 	int 			init_replay_load(char *full_path, NewNationPara *mpGame, int *playerCount);
404 	void			init_replay_save(NewNationPara *mpGame, int playerCount);
405 	void			deinit();
406 
407 	void			init_start_mp();
408 	int				is_enable();
409 	int			is_replay();
410 	int			is_replay_end();
411 	// int			can_start_game();
412 	int				number_of_opponent();
413 	PID_TYPE    	self_player_id();
414 	// void			set_disconnect_handler(DisconnectFP disconnectFP);
415 
416 	int				create_game();
417 	int				connect_game();
418 	void			start_game();
419 
420 	void 			send_msg(RemoteMsg* remoteMsgPtr, uint32_t receiverId=0);
421 	void 			send_free_msg(RemoteMsg* remoteMsgPtr, uint32_t receiverId=0);
422 
423 	RemoteMsg* 		new_msg(uint32_t msgId, int dataSize);
424 	void 			free_msg(RemoteMsg* remoteMsgPtr);
425 
426 	char* 	 		new_send_queue_msg(uint32_t msgId, int msgSize);
427 	int				send_queue_now(uint32_t receiverId=0);
428 	int				send_backup_now(uint32_t receiverId, uint32_t requestFrameCount);
429 	void 			append_send_to_receive();
430 	void			copy_send_to_backup();
431 	// int			poll_msg(UINT message, UINT wParam, LONG lParam);
432 	int				poll_msg();
433 	void			enable_poll_msg();
434 	void			disable_poll_msg();
435 
436 
437 	void 			process_receive_queue();
438 	void 			process_specific_msg(uint32_t msgId);
439 
440 	void			init_send_queue(uint32_t,short);
441 	void			init_receive_queue(uint32_t);
442 
443 	void			enable_process_queue();
444 	void			disable_process_queue();
445 
446 	void			reset_process_frame_delay();
447 	int				get_process_frame_delay();
448 	void			set_process_frame_delay(int);
449 	int				calc_process_frame_delay(int milliSecond);
450 
451 	// ------- alternating send frame -------//
452 	void			set_alternating_send(int rate);
453 	int				get_alternating_send();
454 	int				has_send_frame(int nationRecno, uint32_t frameCount);
455 	uint32_t		next_send_frame(int nationRecno, uint32_t frameCount);
456 };
457 
458 extern Remote remote;
459 
460 //------------------------------------//
461 
462 #endif
463