about summary refs log tree commit diff
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/client/client.c66
-rw-r--r--src/engine/interface.h1
-rw-r--r--src/engine/map.c9
-rw-r--r--src/engine/server/server.c58
4 files changed, 83 insertions, 51 deletions
diff --git a/src/engine/client/client.c b/src/engine/client/client.c
index 87a3481f..1fce3833 100644
--- a/src/engine/client/client.c
+++ b/src/engine/client/client.c
@@ -231,14 +231,6 @@ int snap_num_items(int snapid)
 	return snapshots[snapid]->snap->num_items;
 }
 
-static void snap_init()
-{
-	snapshots[SNAP_CURRENT] = 0;
-	snapshots[SNAP_PREV] = 0;
-	recived_snapshots = 0;
-	current_predtick = 0;
-}
-
 // ------ time functions ------
 float client_intratick() { return intratick; }
 float client_intrapredtick() { return intrapredtick; }
@@ -268,8 +260,6 @@ int client_send_msg()
 
 static void client_send_info()
 {
-	recived_snapshots = 0;
-
 	msg_pack_start_system(NETMSG_INFO, MSGFLAG_VITAL);
 	msg_pack_string(modc_net_version(), 128);
 	msg_pack_string(config.player_name, 128);
@@ -472,6 +462,23 @@ static void client_set_state(int s)
 		modc_statechange(state, old);
 }
 
+/* called when the map is loaded and we should init for a new round */
+static void client_on_enter_game()
+{
+	// reset input
+	int i;
+	for(i = 0; i < 200; i++)
+		inputs[i].tick = -1;
+	current_input = 0;
+
+	// reset snapshots
+	snapshots[SNAP_CURRENT] = 0;
+	snapshots[SNAP_PREV] = 0;
+	snapstorage_purge_all(&snapshot_storage);
+	recived_snapshots = 0;
+	current_predtick = 0;
+	current_recv_tick = 0;
+}
 
 void client_connect(const char *server_address_str)
 {
@@ -500,14 +507,6 @@ void client_connect(const char *server_address_str)
 	
 	netclient_connect(net, &server_address);
 	client_set_state(CLIENTSTATE_CONNECTING);	
-	
-	current_recv_tick = 0;
-	
-	// reset input
-	int i;
-	for(i = 0; i < 200; i++)
-		inputs[i].tick = -1;
-	current_input = 0;
 }
 
 void client_disconnect()
@@ -692,13 +691,15 @@ static void client_process_packet(NETPACKET *packet)
 					dbg_msg("client/network", "loading done");
 					// now we will wait for two snapshots
 					// to finish the connection
+					
+					client_on_enter_game();
 				}
 				else
 				{
 					client_error("failure to load map");
 				}
 			}
-			else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPEMPTY) //|| msg == NETMSG_SNAPSMALL || msg == NETMSG_SNAPEMPTY)
+			else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPEMPTY)
 			{
 				//dbg_msg("client/network", "got snapshot");
 				int game_tick = msg_unpack_int();
@@ -791,12 +792,13 @@ static void client_process_packet(NETPACKET *packet)
 						if(msg != NETMSG_SNAPEMPTY && snapshot_crc((SNAPSHOT*)tmpbuffer3) != crc)
 						{
 							if(config.debug)
-								dbg_msg("client", "snapshot crc error");
+								dbg_msg("client", "snapshot crc error %d", snapcrcerrors);
 							snapcrcerrors++;
-							if(snapcrcerrors > 25)
+							if(snapcrcerrors > 10)
 							{
 								// to many errors, send reset
 								ack_game_tick = -1;
+								client_send_input();
 								snapcrcerrors = 0;
 							}
 							return;
@@ -845,27 +847,8 @@ static void client_process_packet(NETPACKET *packet)
 						}
 						
 						st_update(&game_time, (game_tick-2)*time_freq()/50);
-						//client_send_input();
-
-						//st_update(&predicted_time, game_tick*time_freq());
-
-						/*
-						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();
-						snapshot_latency = snapshot_latency*0.95f+current_latency*0.05f;
-						*/
 						
 						// ack snapshot
-						//dbg_msg("snap!", "%d", game_tick);
 						ack_game_tick = game_tick;
 					}
 				}
@@ -932,9 +915,6 @@ static void client_run(const char *direct_connect_server)
 	// init menu
 	modmenu_init(); // TODO: remove
 	
-	// init snapshotting
-	snap_init();
-	
 	// init the mod
 	modc_init();
 	dbg_msg("client", "version %s", modc_net_version());
diff --git a/src/engine/interface.h b/src/engine/interface.h
index 97a4b684..b2412de7 100644
--- a/src/engine/interface.h
+++ b/src/engine/interface.h
@@ -805,6 +805,7 @@ void snap_free_id(int id);
 
 /* other */
 void map_unload_data(int index);
+void map_set(void *m);
 
 #ifdef __cplusplus
 }
diff --git a/src/engine/map.c b/src/engine/map.c
index 53ade00a..dfb8b691 100644
--- a/src/engine/map.c
+++ b/src/engine/map.c
@@ -1,7 +1,7 @@
 #include <stdio.h>
 #include "datafile.h"
 
-static DATAFILE *map;
+static DATAFILE *map = 0;
 
 void *map_get_data(int index)
 {
@@ -51,3 +51,10 @@ int map_load(const char *mapname)
 	map = datafile_load(buf);
 	return map != 0;
 }
+
+void map_set(void *m)
+{
+	if(map)
+		map_unload();
+	map = (DATAFILE*)m;
+}
diff --git a/src/engine/server/server.c b/src/engine/server/server.c
index 57f315f7..db911819 100644
--- a/src/engine/server/server.c
+++ b/src/engine/server/server.c
@@ -15,6 +15,7 @@
 #include <engine/network.h>
 #include <engine/config.h>
 #include <engine/packer.h>
+#include <engine/datafile.h>
 
 #include <mastersrv/mastersrv.h>
 
@@ -26,7 +27,8 @@ static int current_tick = 0;
 static int64 lastheartbeat;
 static NETADDR4 master_server;
 
-static int biggest_snapshot;
+static char current_map[64];
+
 
 void *snap_new_item(int type, int id, int size)
 {
@@ -304,9 +306,6 @@ static void server_do_snap()
 				int compsize = zerobit_compress(intdata, intsize, compdata);
 				snapshot_size = compsize;
 
-				if(snapshot_size > biggest_snapshot)
-					biggest_snapshot = snapshot_size;
-
 				const int max_size = MAX_SNAPSHOT_PACKSIZE;
 				int numpackets = (snapshot_size+max_size-1)/max_size;
 				(void)numpackets;
@@ -565,17 +564,29 @@ static void server_pump_network()
 	}
 }
 
+static int server_load_map(const char *mapname)
+{
+	DATAFILE *df;
+	char buf[512];
+	sprintf(buf, "data/maps/%s.map", mapname);
+	df = datafile_load(buf);
+	if(!df)
+		return 0;
+		
+	strcpy(current_map, mapname);
+	map_set(df);
+	return 1;
+}
+
 
 static int server_run()
 {
-	biggest_snapshot = 0;
-
 	net_init(); /* For Windows compatibility. */
 	
 	snap_init_id();
 
 	/* load map */
-	if(!map_load(config.sv_map))
+	if(!server_load_map(config.sv_map))
 	{
 		dbg_msg("server", "failed to load map. mapname='%s'", config.sv_map);
 		return -1;
@@ -632,6 +643,39 @@ static int server_run()
 
 	while(1)
 	{
+		/* load new map TODO: don't poll this */
+		if(strcmp(config.sv_map, current_map) != 0)
+		{
+			/* load map */
+			if(server_load_map(config.sv_map))
+			{
+				int c;
+				
+				/* new map loaded */
+				mods_shutdown();
+				
+				for(c = 0; c < MAX_CLIENTS; c++)
+				{
+					if(clients[c].state == SRVCLIENT_STATE_EMPTY)
+						continue;
+					
+					server_send_map(c);
+					clients[c].state = SRVCLIENT_STATE_CONNECTING;
+					clients[c].last_acked_snapshot = -1;
+					snapstorage_purge_all(&clients[c].snapshots);
+				}
+				
+				mods_init();
+				game_start_time = time_get();
+				current_tick = 0;
+			}
+			else
+			{
+				dbg_msg("server", "failed to load map. mapname='%s'", config.sv_map);
+				config_set_sv_map(&config, current_map);
+			}
+		}
+		
 		int64 t = time_get();
 		if(t > server_tick_start_time(current_tick+1))
 		{