about summary refs log tree commit diff
path: root/src/engine/client/client.cpp
blob: e3bf2c32171a14f8e92d336b0862797b92859eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
#include <baselib/system.h>
#include <baselib/config.h>
#include <baselib/input.h>
#include <baselib/audio.h>
#include <baselib/stream/file.h>

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <engine/interface.h>

#include <engine/packet.h>
#include <engine/snapshot.h>
#include "ui.h"

#include <engine/compression.h>
#include <engine/network.h>
#include <engine/versions.h>
#include <engine/config.h>

#include <mastersrv/mastersrv.h>

using namespace baselib;

static int info_request_begin;
static int info_request_end;
static int snapshot_part;
static int64 local_start_time;
static int64 game_start_time;
static float latency = 0;
static int extra_polating = 0;
static int debug_font;
static float frametime = 0.0001f;
static net_client net;
static netaddr4 master_server;
static netaddr4 server_address;
static int window_must_refocus = 0;
static int snaploss = 0;

static int current_tick = 0;
static float intratick = 0;


// --- input wrappers ---
static int keyboard_state[2][input::last];
static int keyboard_current = 0;
static int keyboard_first = 1;

void inp_mouse_relative(int *x, int *y) { input::mouse_position(x, y); }
int inp_mouse_scroll() { return input::mouse_scroll(); }
int inp_key_pressed(int key) { return keyboard_state[keyboard_current][key]; }
int inp_key_was_pressed(int key) { return keyboard_state[keyboard_current^1][key]; }
int inp_key_down(int key) { return inp_key_pressed(key)&&!inp_key_was_pressed(key); }
int inp_button_pressed(int button) { return input::pressed(button); }

void inp_update()
{
	if(keyboard_first)
	{
		// make sure to reset
		keyboard_first = 0;
		inp_update();
	}
	
	keyboard_current = keyboard_current^1;
	for(int i = 0; i < input::last; i++)
		keyboard_state[keyboard_current][i] = input::pressed(i);
}

// --- input snapping ---
static int input_data[MAX_INPUT_SIZE] = {0};
static int input_data_size;
static int input_is_changed = 1;
void snap_input(void *data, int size)
{
	if(input_data_size != size || memcmp(input_data, data, size))
		input_is_changed = 1;
	mem_copy(input_data, data, size);
	input_data_size = size;
}

// -- snapshot handling ---
enum
{
	NUM_SNAPSHOT_TYPES=2,
};

struct snapshot_info
{
	snapshot_info *prev;
	snapshot_info *next;
	
	int tick;
	int64 recvtime;
	snapshot *snap;
};

static snapshot_info *first_snapshot = 0;
static snapshot_info *last_snapshot = 0;

static snapshot_info *client_snapshot_add(int tick, int64 time, void *data, int data_size)
{
	snapshot_info *holder = (snapshot_info*)mem_alloc(sizeof(snapshot_info) + data_size, 1);
	holder->tick = tick;
	holder->recvtime = time;
	holder->snap = (snapshot *)(holder+1);
	mem_copy(holder->snap, data, data_size);
	
	holder->next =0x0;
	holder->prev = last_snapshot;
	if(last_snapshot)
		last_snapshot->next = holder;
	else
		first_snapshot = holder;
	last_snapshot = holder;
	
	return holder;
}

static snapshot_info *client_snapshot_find(int tick)
{
	snapshot_info *current = first_snapshot;
	while(current)
	{
		if(current->tick == tick)
			return current;
		current = current->next;
	}
	
	return 0;
}

static void client_snapshot_purge_until(int tick)
{
	snapshot_info *current = first_snapshot;
	while(current)
	{
		snapshot_info *next = current->next;
		if(current->tick < tick)
			mem_free(current);
		else
			break;
		
		current = next;
		current->prev = 0;
		first_snapshot = current;
	}
	
	if(!first_snapshot)
		last_snapshot = 0;
}

static snapshot_info *snapshots[NUM_SNAPSHOT_TYPES];
static int recived_snapshots;
static char snapshot_incomming_data[MAX_SNAPSHOT_SIZE];

// ---

const void *snap_get_item(int snapid, int index, snap_item *item)
{
	dbg_assert(snapid >= 0 && snapid < NUM_SNAPSHOT_TYPES, "invalid snapid");
	snapshot::item *i = snapshots[snapid]->snap->get_item(index);
	item->type = i->type();
	item->id = i->id();
	return (void *)i->data();
}

const void *snap_find_item(int snapid, int type, int id)
{
	// TODO: linear search. should be fixed.
	for(int i = 0; i < snapshots[snapid]->snap->num_items; i++)
	{
		snapshot::item *itm = snapshots[snapid]->snap->get_item(i);
		if(itm->type() == type && itm->id() == id)
			return (void *)itm->data();
	}
	return 0x0;
}

int snap_num_items(int snapid)
{
	dbg_assert(snapid >= 0 && snapid < NUM_SNAPSHOT_TYPES, "invalid snapid");
	return snapshots[snapid]->snap->num_items;
}

static void snap_init()
{
	snapshots[SNAP_CURRENT] = 0;
	snapshots[SNAP_PREV] = 0;
	recived_snapshots = 0;
	game_start_time = -1;
	
}

// ------ time functions ------
float client_intratick()
{
	return intratick;
}

int client_tick()
{
	return current_tick;
}

int client_tickspeed()
{
	return SERVER_TICK_SPEED;
}

float client_frametime()
{
	return frametime;
}

float client_localtime()
{
	return (time_get()-local_start_time)/(float)(time_freq());
}

int menu_loop(); // TODO: what is this?

// ----- send functions -----
int client_send_msg()
{
	const msg_info *info = msg_get_info();
	NETPACKET packet;
	mem_zero(&packet, sizeof(NETPACKET));
	
	packet.client_id = 0;
	packet.data = info->data;
	packet.data_size = info->size;

	if(info->flags&MSGFLAG_VITAL)	
		packet.flags = PACKETFLAG_VITAL;
	
	net.send(&packet);
	return 0;
}

static void client_send_info()
{
	recived_snapshots = 0;
	game_start_time = -1;

	msg_pack_start_system(NETMSG_INFO, MSGFLAG_VITAL);
	msg_pack_string(TEEWARS_NETVERSION_STRING, 64);
	msg_pack_string(config.player_name, 128);
	msg_pack_string(config.clan_name, 128);
	msg_pack_string(config.password, 128);
	msg_pack_string("myskin", 128);
	msg_pack_end();
	client_send_msg();
}

static void client_send_entergame()
{
	msg_pack_start_system(NETMSG_ENTERGAME, MSGFLAG_VITAL);
	msg_pack_end();
	client_send_msg();
}

static void client_send_error(const char *error)
{
	/*
		pack(NETMSG_CLIENT_ERROR, "s", error);
	*/
	/*
	packet p(NETMSG_CLIENT_ERROR);
	p.write_str(error);
	send_packet(&p);
	//send_packet(&p);
	//send_packet(&p);
	*/
}	

static void client_send_input()
{
	msg_pack_start_system(NETMSG_INPUT, 0);
	msg_pack_int(input_data_size);
	for(int i = 0; i < input_data_size/4; i++)
		msg_pack_int(input_data[i]);
	msg_pack_end();
	client_send_msg();
}


// ------ server browse ----
static struct 
{
	server_info infos[MAX_SERVERS];
	int64 request_times[MAX_SERVERS];
	netaddr4 addresses[MAX_SERVERS];
	int num;
} servers;
static int serverlist_lan = 1;

int client_serverbrowse_getlist(server_info **serverlist)
{
	*serverlist = servers.infos;
	return servers.num;
}

static void client_serverbrowse_init()
{
	servers.num = 0;
}

void client_serverbrowse_refresh(int lan)
{
	serverlist_lan = lan;
	
	if(serverlist_lan)
	{
		if(config.debug)
			dbg_msg("client", "broadcasting for servers");
		NETPACKET packet;
		packet.client_id = -1;
		mem_zero(&packet, sizeof(packet));
		packet.address.ip[0] = 0;
		packet.address.ip[1] = 0;
		packet.address.ip[2] = 0;
		packet.address.ip[3] = 0;
		packet.address.port = 8303;
		packet.flags = PACKETFLAG_CONNLESS;
		packet.data_size = sizeof(SERVERBROWSE_GETINFO);
		packet.data = SERVERBROWSE_GETINFO;
		net.send(&packet);	
		
		// reset the list
		servers.num = 0;		
	}
	else
	{
		if(config.debug)
			dbg_msg("client", "requesting server list");
		NETPACKET packet;
		mem_zero(&packet, sizeof(packet));
		packet.client_id = -1;
		packet.address = master_server;
		packet.flags = PACKETFLAG_CONNLESS;
		packet.data_size = sizeof(SERVERBROWSE_GETLIST);
		packet.data = SERVERBROWSE_GETLIST;
		net.send(&packet);	
		
		// reset the list
		servers.num = 0;
	}
}


static void client_serverbrowse_request(int id)
{
	if(config.debug)
	{
		dbg_msg("client", "requesting server info from %d.%d.%d.%d:%d",
			servers.addresses[id].ip[0], servers.addresses[id].ip[1], servers.addresses[id].ip[2],
			servers.addresses[id].ip[3], servers.addresses[id].port);
	}
	NETPACKET packet;
	packet.client_id = -1;
	packet.address = servers.addresses[id];
	packet.flags = PACKETFLAG_CONNLESS;
	packet.data_size = sizeof(SERVERBROWSE_GETINFO);
	packet.data = SERVERBROWSE_GETINFO;
	net.send(&packet);
	servers.request_times[id] = time_get();
}

static void client_serverbrowse_update()
{
	int64 timeout = time_freq();
	int64 now = time_get();
	int max_requests = 10;
	
	// timeout old requests
	while(info_request_begin < servers.num && info_request_begin < info_request_end)
	{
		if(now > servers.request_times[info_request_begin]+timeout)
			info_request_begin++;
		else
			break;
	}
	
	// send new requests
	while(info_request_end < servers.num && info_request_end-info_request_begin < max_requests)
	{
		client_serverbrowse_request(info_request_end);
		info_request_end++;
	}
}

// ------ state handling -----
static int state;
int client_state() { return state; }
static void client_set_state(int s)
{
	if(config.debug)
		dbg_msg("client", "state change. last=%d current=%d", state, s);
	int old = state;
	state = s;
	if(old != s)
		modc_statechange(state, old);
}


void client_connect(const char *server_address_str)
{
	dbg_msg("client", "connecting to '%s'", server_address_str);
	char buf[512];
	strncpy(buf, server_address_str, 512);
	
	const char *port_str = 0;
	for(int k = 0; buf[k]; k++)
	{
		if(buf[k] == ':')
		{
			port_str = &(buf[k+1]);
			buf[k] = 0;
			break;
		}
	}
	
	int port = 8303;
	if(port_str)
		port = atoi(port_str);
		
	if(net_host_lookup(buf, port, &server_address) != 0)
		dbg_msg("client", "could not find the address of %s, connecting to localhost", buf);
	
	net.connect(&server_address);
	client_set_state(CLIENTSTATE_CONNECTING);	
	current_tick = 0;
}

void client_disconnect()
{
	client_send_error("disconnected");
	net.disconnect("disconnected");
	client_set_state(CLIENTSTATE_OFFLINE);
	map_unload();
}

static bool client_load_data()
{
	debug_font = gfx_load_texture("data/debug_font.png");
	return true;
}

static void client_debug_render()
{
	if(!config.debug)
		return;
		
	gfx_blend_normal();
	gfx_texture_set(debug_font);
	gfx_mapscreen(0,0,gfx_screenwidth(),gfx_screenheight());
	
	static NETSTATS prev, current;
	static int64 last_snap = 0;
	if(time_get()-last_snap > time_freq()/10)
	{
		last_snap = time_get();
		prev = current;
		net.stats(&current);
	}
	
	static float frametime_avg = 0;
	frametime_avg = frametime_avg*0.9f + frametime*0.1f;
	char buffer[512];
	sprintf(buffer, "send: %6d recv: %6d snaploss: %4d latency: %4.0f %c gfxmem: %6dk fps: %3d",
		(current.send_bytes-prev.send_bytes)*10,
		(current.recv_bytes-prev.recv_bytes)*10,
		snaploss,
		latency*1000.0f, extra_polating?'E':' ',
		gfx_memory_usage()/1024,
		(int)(1.0f/frametime_avg));
	gfx_quads_text(2, 2, 16, buffer);
	
}

void client_quit()
{
	client_set_state(CLIENTSTATE_QUITING);
}

const char *client_error_string()
{
	return net.error_string();
}

static void client_render()
{
	gfx_clear(0.0f,0.0f,0.0f);
	modc_render();
	client_debug_render();
}

static void client_error(const char *msg)
{
	dbg_msg("client", "error: %s", msg);
	client_send_error(msg);
	client_set_state(CLIENTSTATE_QUITING);
}

static void client_process_packet(NETPACKET *packet)
{
	if(packet->client_id == -1)
	{
		// connectionlesss
		if(packet->data_size >= (int)sizeof(SERVERBROWSE_LIST) &&
			memcmp(packet->data, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST)) == 0)
		{
			// server listing
			int size = packet->data_size-sizeof(SERVERBROWSE_LIST);
			mem_copy(servers.addresses, (char*)packet->data+sizeof(SERVERBROWSE_LIST), size);
			servers.num = size/sizeof(NETADDR4);

			info_request_begin = 0;
			info_request_end = 0;

			for(int i = 0; i < servers.num; i++)
			{
				servers.infos[i].num_players = 0;
				servers.infos[i].max_players = 0;
				servers.infos[i].latency = 999;
#if defined(CONF_ARCH_ENDIAN_BIG)
				const char *tmp = (const char *)&servers.addresses[i].port;
				servers.addresses[i].port = (tmp[1]<<8) | tmp[0];
#endif
				sprintf(servers.infos[i].address, "%d.%d.%d.%d:%d",
					servers.addresses[i].ip[0], servers.addresses[i].ip[1], servers.addresses[i].ip[2],
					servers.addresses[i].ip[3], servers.addresses[i].port);
				sprintf(servers.infos[i].name, "%d.%d.%d.%d:%d",
					servers.addresses[i].ip[0], servers.addresses[i].ip[1], servers.addresses[i].ip[2],
					servers.addresses[i].ip[3], servers.addresses[i].port);
			}
		}

		if(packet->data_size >= (int)sizeof(SERVERBROWSE_INFO) &&
			memcmp(packet->data, SERVERBROWSE_INFO, sizeof(SERVERBROWSE_INFO)) == 0)
		{
			// we got ze info
			data_unpacker unpacker;
			unpacker.reset((unsigned char*)packet->data+sizeof(SERVERBROWSE_INFO), packet->data_size-sizeof(SERVERBROWSE_INFO));
			
			if(serverlist_lan)
			{
				if(servers.num != MAX_SERVERS)
				{
					int i = servers.num;
					strncpy(servers.infos[i].name, unpacker.get_string(), 128);
					strncpy(servers.infos[i].map, unpacker.get_string(), 128);
					servers.infos[i].max_players = unpacker.get_int();
					servers.infos[i].num_players = unpacker.get_int();
					servers.infos[i].latency = 0;
					
					sprintf(servers.infos[i].address, "%d.%d.%d.%d:%d",
						packet->address.ip[0], packet->address.ip[1], packet->address.ip[2],
						packet->address.ip[3], packet->address.port);

					if(config.debug)
						dbg_msg("client", "got server info");
					servers.num++;
					
				}
			}
			else
			{
				for(int i = 0; i < servers.num; i++)
				{
					if(net_addr4_cmp(&servers.addresses[i], &packet->address) == 0)
					{
						strncpy(servers.infos[i].name, unpacker.get_string(), 128);
						strncpy(servers.infos[i].map, unpacker.get_string(), 128);
						servers.infos[i].max_players = unpacker.get_int();
						servers.infos[i].num_players = unpacker.get_int();
						servers.infos[i].latency = ((time_get() - servers.request_times[i])*1000)/time_freq();
						if(config.debug)
							dbg_msg("client", "got server info");
						break;
					}
				}
			}
		}
	}
	else
	{
		
		int sys;
		int msg = msg_unpack_start(packet->data, packet->data_size, &sys);
		if(sys)
		{
			// system message
			if(msg == NETMSG_MAP)
			{
				const char *map = msg_unpack_string();
				dbg_msg("client/network", "connection accepted, map=%s", map);
				client_set_state(CLIENTSTATE_LOADING);
				
				if(map_load(map))
				{
					modc_entergame();
					client_send_entergame();
					dbg_msg("client/network", "loading done");
					// now we will wait for two snapshots
					// to finish the connection
				}
				else
				{
					client_error("failure to load map");
				}
			}
			else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPEMPTY) //|| msg == NETMSG_SNAPSMALL || msg == NETMSG_SNAPEMPTY)
			{
				//dbg_msg("client/network", "got snapshot");
				int game_tick = msg_unpack_int();
				int delta_tick = game_tick-msg_unpack_int();
				int num_parts = 1;
				int part = 0;
				int part_size = 0;
				
				if(msg != NETMSG_SNAPEMPTY)
					part_size = msg_unpack_int();
				
				if(snapshot_part == part && game_tick > current_tick)
				{
					// TODO: clean this up abit
					const char *d = (const char *)msg_unpack_raw(part_size);
					mem_copy((char*)snapshot_incomming_data + part*MAX_SNAPSHOT_PACKSIZE, d, part_size);
					snapshot_part++;
				
					if(snapshot_part == num_parts)
					{
						snapshot_part = 0;
						
						// find snapshot that we should use as delta 
						static snapshot emptysnap;
						emptysnap.data_size = 0;
						emptysnap.num_items = 0;
						
						snapshot *deltashot = &emptysnap;
						
						// find delta
						if(delta_tick >= 0)
						{
							//void *delta_data;
							snapshot_info *delta_info = client_snapshot_find(delta_tick);
							//deltashot_size = snapshots_new.get(delta_tick, 0, &delta_data);
							if(delta_info)
								deltashot = delta_info->snap;
							else
							{
								// couldn't find the delta snapshots that the server used
								// to compress this snapshot. force the server to resync
								if(config.debug)
									dbg_msg("client", "error, couldn't find the delta snapshot");
								
								// ack snapshot
								msg_pack_start_system(NETMSG_SNAPACK, 0);
								msg_pack_int(-1);
								msg_pack_end();
								client_send_msg();
								return;
							}
						}

						// decompress snapshot
						void *deltadata = snapshot_empty_delta();
						int deltasize = sizeof(int)*3;

						unsigned char tmpbuffer[MAX_SNAPSHOT_SIZE];
						unsigned char tmpbuffer2[MAX_SNAPSHOT_SIZE];
						if(part_size)
						{
							int compsize = zerobit_decompress(snapshot_incomming_data, part_size, tmpbuffer);
							int intsize = intpack_decompress(tmpbuffer, compsize, tmpbuffer2);
							deltadata = tmpbuffer2;
							deltasize = intsize;
						}

						//dbg_msg("UNPACK", "%d unpacked with %d", game_tick, delta_tick);
						
						unsigned char tmpbuffer3[MAX_SNAPSHOT_SIZE];
						int snapsize = snapshot_unpack_delta(deltashot, (snapshot*)tmpbuffer3, deltadata, deltasize);

						// purge old snapshots				
						int purgetick = delta_tick;
						if(snapshots[SNAP_PREV] && snapshots[SNAP_PREV]->tick < purgetick)
							purgetick = snapshots[SNAP_PREV]->tick;
						if(snapshots[SNAP_CURRENT] && snapshots[SNAP_CURRENT]->tick < purgetick)
							purgetick = snapshots[SNAP_PREV]->tick;
						client_snapshot_purge_until(purgetick);
						//client_snapshot_purge_until(game_tick-50);
						
						// add new
						snapshot_info *snap = client_snapshot_add(game_tick, time_get(), tmpbuffer3, snapsize);
						
						//int ncrc = snapshot_crc((snapshot*)tmpbuffer3);
						//if(crc != ncrc)
						//	dbg_msg("client", "client snapshot crc failure %d %d", crc, ncrc);
						
						// apply snapshot, cycle pointers
						recived_snapshots++;
						

						if(current_tick > 0)
							snaploss += game_tick-current_tick-1;
						
						current_tick = game_tick;
						
						// we got two snapshots until we see us self as connected
						if(recived_snapshots <= 2)
						{
							snapshots[SNAP_PREV] = snapshots[SNAP_CURRENT];
							snapshots[SNAP_CURRENT] = snap;
						}
						
						if(recived_snapshots == 2)
						{
							local_start_time = time_get();
							client_set_state(CLIENTSTATE_ONLINE);
						}
						
						int64 now = time_get();
						int64 t = now - game_tick*time_freq()/50;
						if(game_start_time == -1 || t < game_start_time)
						{
							if(config.debug)
								dbg_msg("client", "adjusted time");
							game_start_time = t;
						}
						
						int64 wanted = game_start_time+(game_tick*time_freq())/50;
						float current_latency = (now-wanted)/(float)time_freq();
						latency = latency*0.95f+current_latency*0.05f;
						
						// ack snapshot
						msg_pack_start_system(NETMSG_SNAPACK, 0);
						msg_pack_int(game_tick);
						msg_pack_end();
						client_send_msg();
					}
				}
				else
				{
					dbg_msg("client", "snapshot reset!");
					snapshot_part = 0;
				}
			}
		}
		else
		{
			// game message
			modc_message(msg);
		}
	}
}


static void client_pump_network()
{
	net.update();

	// check for errors		
	if(client_state() != CLIENTSTATE_OFFLINE && net.state() == NETSTATE_OFFLINE)
	{
		// TODO: add message to the user there
		client_set_state(CLIENTSTATE_OFFLINE);
		dbg_msg("client", "offline error='%s'", net.error_string());
	}

	//
	if(client_state() == CLIENTSTATE_CONNECTING && net.state() == NETSTATE_ONLINE)
	{
		// we switched to online
		dbg_msg("client", "connected, sending info");
		client_set_state(CLIENTSTATE_LOADING);
		client_send_info();
	}
	
	// process packets
	NETPACKET packet;
	while(net.recv(&packet))
		client_process_packet(&packet);
}

static void client_run(const char *direct_connect_server)
{
	local_start_time = time_get();
	snapshot_part = 0;
	info_request_begin = 0;
	info_request_end = 0;
	
	client_serverbrowse_init();
	
	// init graphics and sound
	if(!gfx_init())
		return;

	snd_init(); // sound is allowed to fail
	
	// load data
	if(!client_load_data())
		return;

	// init menu
	modmenu_init(); // TODO: remove
	
	// init snapshotting
	snap_init();
	
	// init the mod
	modc_init();
	
	// open socket
	NETADDR4 bindaddr;
	mem_zero(&bindaddr, sizeof(bindaddr));
	net.open(bindaddr, 0);
	
	//
	net_host_lookup(config.masterserver, MASTERSERVER_PORT, &master_server);

	// connect to the server if wanted
	if(direct_connect_server)
		client_connect(direct_connect_server);
		
	//int64 inputs_per_second = 50;
	//int64 time_per_input = time_freq()/inputs_per_second;
	int64 game_starttime = time_get();
	int64 last_input = game_starttime;
	
	int64 reporttime = time_get();
	int64 reportinterval = time_freq()*1;
	int frames = 0;
	
	input::set_mouse_mode(input::mode_relative);
	
	while (1)
	{	
		frames++;
		int64 frame_start_time = time_get();

		// switch snapshot
		if(recived_snapshots >= 3)
		{
			int64 now = time_get();
			while(1)
			{
				snapshot_info *cur = snapshots[SNAP_CURRENT];
				int64 tickstart = game_start_time + (cur->tick+1)*time_freq()/50;
				int64 t = tickstart;
				if(latency > 0)
					t += (int64)(time_freq()*(latency*1.1f));

				if(t < now)
				{
					snapshot_info *next = snapshots[SNAP_CURRENT]->next;
					if(next)
					{
						snapshots[SNAP_PREV] = snapshots[SNAP_CURRENT];
						snapshots[SNAP_CURRENT] = next;
						if(snapshots[SNAP_CURRENT] && snapshots[SNAP_PREV])
							modc_newsnapshot();
					}
					else
					{
						extra_polating = 1;
						break;
					}
				}
				else
				{
					extra_polating = 0;
					break;
				}
			}
			
			if(snapshots[SNAP_CURRENT] && snapshots[SNAP_PREV])
			{
				int64 curtick_start = game_start_time + (snapshots[SNAP_CURRENT]->tick+1)*time_freq()/50;
				if(latency > 0)
					curtick_start += (int64)(time_freq()*(latency*1.1f));
					
				int64 prevtick_start = game_start_time + (snapshots[SNAP_PREV]->tick+1)*time_freq()/50;
				if(latency > 0)
					prevtick_start += (int64)(time_freq()*(latency*1.1f));
					
				intratick = (now - prevtick_start) / (float)(curtick_start-prevtick_start);
			}
		}

		// send input
		if(client_state() == CLIENTSTATE_ONLINE)
		{
			if(config.stress&1 && client_localtime() > 10.0f)
				client_disconnect();
			
			if(input_is_changed || time_get() > last_input+time_freq())
			{
				client_send_input();
				input_is_changed = 0;
				last_input = time_get();
			}
		}
		
		if(client_state() == CLIENTSTATE_OFFLINE && config.stress && (frames%100) == 0)
			client_connect(config.cl_stress_server);
		
		// update input
		inp_update();
		
		// refocus
		if(!gfx_window_active())
		{
			if(window_must_refocus == 0)
			{
				input::set_mouse_mode(input::mode_absolute);
			}
			window_must_refocus = 1;
		}

		if(window_must_refocus && gfx_window_active())
		{
			if(window_must_refocus < 3)
			{
				input::set_mouse_mode(input::mode_absolute);
				window_must_refocus++;
			}

			if(inp_button_pressed(input::mouse_1))
			{
				input::set_mouse_mode(input::mode_relative);
				window_must_refocus = 0;
			}
		}

		// screenshot button
		if(inp_key_down(input::f10))
			gfx_screenshot();

		// panic button
		if(config.debug)
		{
			if(input::pressed(input::f1))
				input::set_mouse_mode(input::mode_absolute);
			if(input::pressed(input::f2))
				input::set_mouse_mode(input::mode_relative);

			if(input::pressed(input::lctrl) && input::pressed('Q'))
				break;
		
			if(input::pressed(input::f5))
			{
				// ack snapshot
				msg_pack_start_system(NETMSG_SNAPACK, 0);
				msg_pack_int(-1);
				msg_pack_end();
				client_send_msg();
			}
		}
			
		// pump the network
		client_pump_network();
		
		// update the server browser
		client_serverbrowse_update();
		
		// render
		if(config.stress)
		{
			if((frames%10) == 0)
			{
				client_render();
				gfx_swap();
			}
		}
		else
		{
			client_render();
			gfx_swap();
		}
		
		// check conditions
		if(client_state() == CLIENTSTATE_QUITING)
			break;

		// be nice
		if(config.cpu_throttle || !gfx_window_active())
			thread_sleep(1);
		
		if(reporttime < time_get())
		{
			if(config.debug)
			{
				dbg_msg("client/report", "fps=%.02f netstate=%d",
					frames/(float)(reportinterval/time_freq()), net.state());
			}
			frames = 0;
			reporttime += reportinterval;
		}
		
		// update frametime
		frametime = (time_get()-frame_start_time)/(float)time_freq();
	}
	
	modc_shutdown();
	client_disconnect();

	modmenu_shutdown(); // TODO: remove this
	
	gfx_shutdown();
	snd_shutdown();
}


int editor_main(int argc, char **argv);

//client main_client;

int main(int argc, char **argv)
{
	dbg_msg("client", "starting...");
	
	config_reset();

#ifdef CONF_PLATFORM_MACOSX
	const char *config_filename = "~/.teewars";
#else
	const char *config_filename = "default.cfg";
#endif

	for(int i = 1; i < argc; i++)
	{
		if(argv[i][0] == '-' && argv[i][1] == 'f' && argv[i][2] == 0 && argc - i > 1)
		{
			config_filename = argv[i+1];
			i++;
		}
	}

	config_load(config_filename);

	const char *direct_connect_server = 0x0;
	snd_set_master_volume(config.volume / 255.0f);
	bool editor = false;

	// init network, need to be done first so we can do lookups
	net_init();

	// parse arguments
	for(int i = 1; i < argc; i++)
	{
		if(argv[i][0] == '-' && argv[i][1] == 'c' && argv[i][2] == 0 && argc - i > 1)
		{
			// -c SERVER:PORT
			i++;
			direct_connect_server = argv[i];
		}
		else if(argv[i][0] == '-' && argv[i][1] == 'e' && argv[i][2] == 0)
		{
			editor = true;
		}
		else
			config_set(argv[i]);
	}
	
	if(editor)
		editor_main(argc, argv);
	else
	{
		// start the client
		client_run(direct_connect_server);
	}
	return 0;
}