2007-05-22 15:03:32 +00:00
|
|
|
#include <baselib/system.h>
|
2007-06-10 15:19:04 +00:00
|
|
|
#include <baselib/input.h>
|
2007-05-22 15:03:32 +00:00
|
|
|
#include <baselib/audio.h>
|
|
|
|
#include <baselib/stream/file.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2007-05-24 10:57:18 +00:00
|
|
|
#include <stdarg.h>
|
2007-05-27 00:47:07 +00:00
|
|
|
#include <stdlib.h>
|
2007-07-13 13:40:04 +00:00
|
|
|
#include <stdio.h>
|
2007-05-22 15:03:32 +00:00
|
|
|
#include <math.h>
|
2007-05-24 20:54:08 +00:00
|
|
|
#include <engine/interface.h>
|
2007-05-22 15:03:32 +00:00
|
|
|
|
2007-05-24 20:54:08 +00:00
|
|
|
#include <engine/packet.h>
|
|
|
|
#include <engine/snapshot.h>
|
2007-05-22 15:03:32 +00:00
|
|
|
#include "ui.h"
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
#include <engine/compression.h>
|
2007-05-22 15:03:32 +00:00
|
|
|
|
2007-05-24 20:54:08 +00:00
|
|
|
#include <engine/versions.h>
|
2007-06-01 11:17:10 +00:00
|
|
|
#include <engine/config.h>
|
2007-07-13 13:40:04 +00:00
|
|
|
#include <engine/network.h>
|
2007-05-22 18:18:41 +00:00
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
#include <mastersrv/mastersrv.h>
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
using namespace baselib;
|
|
|
|
|
|
|
|
// --- input wrappers ---
|
2007-06-10 15:19:04 +00:00
|
|
|
static int keyboard_state[2][input::last];
|
2007-05-22 15:03:32 +00:00
|
|
|
static int keyboard_current = 0;
|
|
|
|
static int keyboard_first = 1;
|
|
|
|
|
2007-06-10 15:19:04 +00:00
|
|
|
void inp_mouse_relative(int *x, int *y) { input::mouse_position(x, y); }
|
2007-05-22 15:03:32 +00:00
|
|
|
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); }
|
2007-06-10 15:19:04 +00:00
|
|
|
int inp_button_pressed(int button) { return input::pressed(button); }
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
void inp_update()
|
|
|
|
{
|
|
|
|
if(keyboard_first)
|
|
|
|
{
|
|
|
|
// make sure to reset
|
|
|
|
keyboard_first = 0;
|
|
|
|
inp_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
keyboard_current = keyboard_current^1;
|
2007-06-10 15:19:04 +00:00
|
|
|
for(int i = 0; i < input::last; i++)
|
|
|
|
keyboard_state[keyboard_current][i] = input::pressed(i);
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- input snapping ---
|
2007-07-13 13:40:04 +00:00
|
|
|
static int input_data[MAX_INPUT_SIZE] = {0};
|
2007-05-22 15:03:32 +00:00
|
|
|
static int input_data_size;
|
2007-05-24 10:57:18 +00:00
|
|
|
static int input_is_changed = 1;
|
2007-05-22 15:03:32 +00:00
|
|
|
void snap_input(void *data, int size)
|
|
|
|
{
|
2007-05-24 10:57:18 +00:00
|
|
|
if(input_data_size != size || memcmp(input_data, data, size))
|
|
|
|
input_is_changed = 1;
|
2007-05-22 15:03:32 +00:00
|
|
|
mem_copy(input_data, data, size);
|
|
|
|
input_data_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- snapshot handling ---
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SNAP_INCOMMING=2,
|
|
|
|
NUM_SNAPSHOT_TYPES=3,
|
|
|
|
};
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
static snapshot_storage snapshots_new;
|
|
|
|
static int current_tick;
|
2007-05-22 15:03:32 +00:00
|
|
|
static snapshot *snapshots[NUM_SNAPSHOT_TYPES];
|
|
|
|
static char snapshot_data[NUM_SNAPSHOT_TYPES][MAX_SNAPSHOT_SIZE];
|
|
|
|
static int recived_snapshots;
|
|
|
|
static int64 snapshot_start_time;
|
|
|
|
static int64 local_start_time;
|
|
|
|
|
|
|
|
float client_localtime()
|
|
|
|
{
|
|
|
|
return (time_get()-local_start_time)/(float)(time_freq());
|
|
|
|
}
|
|
|
|
|
|
|
|
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]->get_item(index);
|
|
|
|
item->type = i->type();
|
|
|
|
item->id = i->id();
|
2007-07-13 13:40:04 +00:00
|
|
|
return (void *)i->data();
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int snap_num_items(int snapid)
|
|
|
|
{
|
|
|
|
dbg_assert(snapid >= 0 && snapid < NUM_SNAPSHOT_TYPES, "invalid snapid");
|
|
|
|
return snapshots[snapid]->num_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void snap_init()
|
|
|
|
{
|
|
|
|
snapshots[SNAP_INCOMMING] = (snapshot*)snapshot_data[0];
|
|
|
|
snapshots[SNAP_CURRENT] = (snapshot*)snapshot_data[1];
|
|
|
|
snapshots[SNAP_PREV] = (snapshot*)snapshot_data[2];
|
|
|
|
mem_zero(snapshot_data, NUM_SNAPSHOT_TYPES*MAX_SNAPSHOT_SIZE);
|
|
|
|
recived_snapshots = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
float client_intratick()
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
|
|
|
return (time_get() - snapshot_start_time)/(float)(time_freq()/SERVER_TICK_SPEED);
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
int client_tick()
|
|
|
|
{
|
|
|
|
return current_tick;
|
|
|
|
}
|
|
|
|
|
2007-07-15 10:47:50 +00:00
|
|
|
int client_tickspeed()
|
|
|
|
{
|
|
|
|
return SERVER_TICK_SPEED;
|
|
|
|
}
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
void *snap_find_item(int snapid, int type, int id)
|
|
|
|
{
|
|
|
|
// TODO: linear search. should be fixed.
|
|
|
|
for(int i = 0; i < snapshots[snapid]->num_items; i++)
|
|
|
|
{
|
|
|
|
snapshot::item *itm = snapshots[snapid]->get_item(i);
|
|
|
|
if(itm->type() == type && itm->id() == id)
|
2007-07-13 13:40:04 +00:00
|
|
|
return (void *)itm->data();
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
int menu_loop(); // TODO: what is this?
|
|
|
|
static float frametime = 0.0001f;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
float client_frametime()
|
|
|
|
{
|
|
|
|
return frametime;
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
static net_client net;
|
2007-05-24 10:57:18 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
int client_send_msg()
|
2007-05-24 10:57:18 +00:00
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
const msg_info *info = msg_get_info();
|
|
|
|
NETPACKET packet;
|
2007-07-21 19:02:47 +00:00
|
|
|
mem_zero(&packet, sizeof(NETPACKET));
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
packet.client_id = 0;
|
|
|
|
packet.data = info->data;
|
|
|
|
packet.data_size = info->size;
|
|
|
|
|
|
|
|
if(info->flags&MSGFLAG_VITAL)
|
|
|
|
packet.flags = PACKETFLAG_VITAL;
|
2007-05-24 10:57:18 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
net.send(&packet);
|
|
|
|
return 0;
|
2007-05-24 10:57:18 +00:00
|
|
|
}
|
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
server_info infos[MAX_SERVERS];
|
|
|
|
int64 request_times[MAX_SERVERS];
|
|
|
|
netaddr4 addresses[MAX_SERVERS];
|
|
|
|
int num;
|
|
|
|
} servers;
|
|
|
|
|
|
|
|
static netaddr4 master_server;
|
|
|
|
|
|
|
|
int client_serverbrowse_getlist(server_info **serverlist)
|
|
|
|
{
|
|
|
|
*serverlist = servers.infos;
|
|
|
|
return servers.num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void client_serverbrowse_refresh()
|
|
|
|
{
|
|
|
|
dbg_msg("client", "requesting server list");
|
|
|
|
NETPACKET 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
STATE_OFFLINE,
|
|
|
|
STATE_CONNECTING,
|
|
|
|
STATE_LOADING,
|
|
|
|
STATE_ONLINE,
|
|
|
|
STATE_BROKEN,
|
|
|
|
STATE_QUIT,
|
|
|
|
};
|
|
|
|
|
|
|
|
static netaddr4 server_address;
|
|
|
|
|
|
|
|
static int state;
|
|
|
|
static int get_state() { return state; }
|
|
|
|
static void set_state(int s)
|
|
|
|
{
|
|
|
|
dbg_msg("game", "state change. last=%d current=%d", state, s);
|
|
|
|
state = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void client_connect(const char *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);
|
|
|
|
set_state(STATE_CONNECTING);
|
|
|
|
}
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
// --- client ---
|
2007-07-13 13:40:04 +00:00
|
|
|
// TODO: remove this class
|
2007-05-22 15:03:32 +00:00
|
|
|
class client
|
|
|
|
{
|
|
|
|
public:
|
2007-07-21 18:07:27 +00:00
|
|
|
int info_request_begin;
|
|
|
|
int info_request_end;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
int snapshot_part;
|
|
|
|
|
|
|
|
int debug_font; // TODO: rfemove this line
|
|
|
|
|
|
|
|
// data to hold three snapshots
|
|
|
|
// previous,
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
void send_info()
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
|
|
|
recived_snapshots = 0;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-07-14 13:09:42 +00:00
|
|
|
msg_pack_start_system(NETMSG_INFO, MSGFLAG_VITAL);
|
2007-07-13 13:40:04 +00:00
|
|
|
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();
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
void send_entergame()
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-14 13:09:42 +00:00
|
|
|
msg_pack_start_system(NETMSG_ENTERGAME, MSGFLAG_VITAL);
|
2007-07-13 13:40:04 +00:00
|
|
|
msg_pack_end();
|
|
|
|
client_send_msg();
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void send_error(const char *error)
|
|
|
|
{
|
2007-05-24 10:57:18 +00:00
|
|
|
/*
|
|
|
|
pack(NETMSG_CLIENT_ERROR, "s", error);
|
|
|
|
*/
|
2007-07-13 13:40:04 +00:00
|
|
|
/*
|
2007-05-22 15:03:32 +00:00
|
|
|
packet p(NETMSG_CLIENT_ERROR);
|
2007-05-24 10:57:18 +00:00
|
|
|
p.write_str(error);
|
2007-05-22 15:03:32 +00:00
|
|
|
send_packet(&p);
|
|
|
|
//send_packet(&p);
|
|
|
|
//send_packet(&p);
|
2007-07-13 13:40:04 +00:00
|
|
|
*/
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void send_input()
|
|
|
|
{
|
2007-07-14 13:09:42 +00:00
|
|
|
msg_pack_start_system(NETMSG_INPUT, 0);
|
2007-07-13 13:40:04 +00:00
|
|
|
msg_pack_int(input_data_size);
|
2007-05-22 15:03:32 +00:00
|
|
|
for(int i = 0; i < input_data_size/4; i++)
|
2007-07-13 13:40:04 +00:00
|
|
|
msg_pack_int(input_data[i]);
|
|
|
|
msg_pack_end();
|
|
|
|
client_send_msg();
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void disconnect()
|
|
|
|
{
|
|
|
|
send_error("disconnected");
|
|
|
|
set_state(STATE_OFFLINE);
|
|
|
|
map_unload();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool load_data()
|
|
|
|
{
|
2007-05-27 00:47:07 +00:00
|
|
|
debug_font = gfx_load_texture("data/debug_font.png");
|
2007-05-22 15:03:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
void debug_render()
|
|
|
|
{
|
|
|
|
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(¤t);
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[512];
|
|
|
|
sprintf(buffer, "send: %8d recv: %8d",
|
|
|
|
(current.send_bytes-prev.send_bytes)*10,
|
|
|
|
(current.recv_bytes-prev.recv_bytes)*10);
|
|
|
|
gfx_quads_text(10, 10, 16, buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
void render()
|
|
|
|
{
|
|
|
|
gfx_clear(0.0f,0.0f,0.0f);
|
|
|
|
|
|
|
|
// this should be moved around abit
|
2007-07-13 13:40:04 +00:00
|
|
|
// TODO: clean this shit up!
|
2007-05-22 15:03:32 +00:00
|
|
|
if(get_state() == STATE_ONLINE)
|
|
|
|
{
|
|
|
|
modc_render();
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
// debug render stuff
|
|
|
|
debug_render();
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
else if (get_state() != STATE_CONNECTING && get_state() != STATE_LOADING)
|
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
//netaddr4 server_address;
|
|
|
|
int status = modmenu_render();
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
if (status == -1)
|
|
|
|
set_state(STATE_QUIT);
|
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
else if (get_state() == STATE_CONNECTING || get_state() == STATE_LOADING)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
|
|
|
static int64 start = time_get();
|
|
|
|
static int tee_texture;
|
|
|
|
static int connecting_texture;
|
|
|
|
static bool inited = false;
|
|
|
|
|
|
|
|
if (!inited)
|
|
|
|
{
|
2007-05-27 00:47:07 +00:00
|
|
|
tee_texture = gfx_load_texture("data/gui_tee.png");
|
|
|
|
connecting_texture = gfx_load_texture("data/gui/connecting.png");
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
inited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx_mapscreen(0,0,400.0f,300.0f);
|
|
|
|
|
|
|
|
float t = (time_get() - start) / (double)time_freq();
|
|
|
|
|
|
|
|
float speed = 2*sin(t);
|
|
|
|
|
|
|
|
speed = 1.0f;
|
|
|
|
|
|
|
|
float x = 208 + sin(t*speed) * 32;
|
|
|
|
float w = sin(t*speed + 3.149) * 64;
|
|
|
|
|
|
|
|
ui_do_image(tee_texture, x, 95, w, 64);
|
|
|
|
ui_do_image(connecting_texture, 88, 150, 256, 64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
void run(const char *direct_connect_server)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-21 15:48:35 +00:00
|
|
|
local_start_time = time_get();
|
2007-05-22 15:03:32 +00:00
|
|
|
snapshot_part = 0;
|
2007-07-21 18:13:20 +00:00
|
|
|
info_request_begin = 0;
|
|
|
|
info_request_end = 0;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
// init graphics and sound
|
2007-07-21 16:33:01 +00:00
|
|
|
if(!gfx_init())
|
2007-05-22 15:03:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
snd_init(); // sound is allowed to fail
|
|
|
|
|
|
|
|
// load data
|
|
|
|
if(!load_data())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// init snapshotting
|
|
|
|
snap_init();
|
|
|
|
|
|
|
|
// init the mod
|
|
|
|
modc_init();
|
|
|
|
|
|
|
|
// init menu
|
|
|
|
modmenu_init();
|
|
|
|
|
|
|
|
// open socket
|
2007-07-21 18:07:27 +00:00
|
|
|
net.open(0, 0);
|
|
|
|
|
|
|
|
//
|
|
|
|
net_host_lookup(MASTERSERVER_ADDRESS, MASTERSERVER_PORT, &master_server);
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
// connect to the server if wanted
|
2007-07-21 18:07:27 +00:00
|
|
|
if(direct_connect_server)
|
|
|
|
client_connect(direct_connect_server);
|
|
|
|
|
2007-05-24 20:54:08 +00:00
|
|
|
//int64 inputs_per_second = 50;
|
|
|
|
//int64 time_per_input = time_freq()/inputs_per_second;
|
2007-05-22 15:03:32 +00:00
|
|
|
int64 game_starttime = time_get();
|
2007-05-24 10:57:18 +00:00
|
|
|
int64 last_input = game_starttime;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
int64 reporttime = time_get();
|
|
|
|
int64 reportinterval = time_freq()*1;
|
|
|
|
int frames = 0;
|
|
|
|
|
2007-06-10 15:19:04 +00:00
|
|
|
input::set_mouse_mode(input::mode_relative);
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
frames++;
|
|
|
|
int64 frame_start_time = time_get();
|
2007-05-24 10:57:18 +00:00
|
|
|
|
|
|
|
// send input
|
|
|
|
if(get_state() == STATE_ONLINE)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-05-24 10:57:18 +00:00
|
|
|
if(input_is_changed || time_get() > last_input+time_freq())
|
|
|
|
{
|
2007-05-22 15:03:32 +00:00
|
|
|
send_input();
|
2007-05-24 10:57:18 +00:00
|
|
|
input_is_changed = 0;
|
|
|
|
last_input = time_get();
|
|
|
|
}
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update input
|
|
|
|
inp_update();
|
|
|
|
|
|
|
|
//
|
2007-06-10 15:19:04 +00:00
|
|
|
if(input::pressed(input::f1))
|
|
|
|
input::set_mouse_mode(input::mode_absolute);
|
|
|
|
if(input::pressed(input::f2))
|
|
|
|
input::set_mouse_mode(input::mode_relative);
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-07-15 23:51:51 +00:00
|
|
|
// panic button
|
2007-07-13 13:40:04 +00:00
|
|
|
if(input::pressed(input::lctrl) && input::pressed('Q'))
|
|
|
|
break;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
// pump the network
|
|
|
|
pump_network();
|
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
// update the server browser
|
|
|
|
serverbrowse_update();
|
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
// render
|
|
|
|
render();
|
|
|
|
|
|
|
|
// swap the buffers
|
|
|
|
gfx_swap();
|
|
|
|
|
|
|
|
// check conditions
|
|
|
|
if(get_state() == STATE_BROKEN || get_state() == STATE_QUIT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// be nice
|
|
|
|
//thread_sleep(1);
|
|
|
|
|
|
|
|
if(reporttime < time_get())
|
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
dbg_msg("client/report", "fps=%.02f netstate=%d",
|
|
|
|
frames/(float)(reportinterval/time_freq()), net.state());
|
2007-05-22 15:03:32 +00:00
|
|
|
frames = 0;
|
|
|
|
reporttime += reportinterval;
|
|
|
|
}
|
|
|
|
|
2007-06-10 15:19:04 +00:00
|
|
|
if (input::pressed(input::esc))
|
2007-05-22 15:03:32 +00:00
|
|
|
if (get_state() == STATE_CONNECTING || get_state() == STATE_ONLINE)
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
// update frametime
|
|
|
|
frametime = (time_get()-frame_start_time)/(float)time_freq();
|
|
|
|
}
|
|
|
|
|
|
|
|
modc_shutdown();
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
modmenu_shutdown();
|
|
|
|
|
|
|
|
gfx_shutdown();
|
|
|
|
snd_shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void error(const char *msg)
|
|
|
|
{
|
|
|
|
dbg_msg("game", "error: %s", msg);
|
|
|
|
send_error(msg);
|
|
|
|
set_state(STATE_BROKEN);
|
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
|
|
|
|
void serverbrowse_request(int id)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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)
|
|
|
|
{
|
|
|
|
serverbrowse_request(info_request_end);
|
|
|
|
info_request_end++;
|
|
|
|
}
|
|
|
|
}
|
2007-05-22 15:03:32 +00:00
|
|
|
|
2007-07-13 13:40:04 +00:00
|
|
|
void process_packet(NETPACKET *packet)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
if(packet->client_id == -1)
|
2007-05-27 00:47:07 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
// connectionlesss
|
|
|
|
if(packet->data_size >= (int)sizeof(SERVERBROWSE_LIST) &&
|
|
|
|
memcmp(packet->data, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST)) == 0)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
// 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++)
|
2007-07-14 13:09:42 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
servers.infos[i].num_players = 0;
|
|
|
|
servers.infos[i].max_players = 0;
|
|
|
|
servers.infos[i].latency = 999;
|
|
|
|
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);
|
2007-07-14 13:09:42 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
for(int i = 0; i < servers.num; i++)
|
2007-07-14 13:09:42 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
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();
|
|
|
|
dbg_msg("client", "got server info");
|
|
|
|
break;
|
|
|
|
}
|
2007-07-14 13:09:42 +00:00
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
int sys;
|
|
|
|
int msg = msg_unpack_start(packet->data, packet->data_size, &sys);
|
|
|
|
if(sys)
|
2007-07-13 13:40:04 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
// system message
|
|
|
|
if(msg == NETMSG_MAP)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
const char *map = msg_unpack_string();
|
|
|
|
dbg_msg("client/network", "connection accepted, map=%s", map);
|
|
|
|
set_state(STATE_LOADING);
|
|
|
|
|
|
|
|
if(map_load(map))
|
|
|
|
{
|
|
|
|
modc_entergame();
|
|
|
|
send_entergame();
|
|
|
|
dbg_msg("client/network", "loading done");
|
|
|
|
// now we will wait for two snapshots
|
|
|
|
// to finish the connection
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error("failure to load map");
|
|
|
|
}
|
2007-07-14 13:09:42 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPSMALL || msg == NETMSG_SNAPEMPTY)
|
2007-07-14 13:09:42 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
//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_SNAP)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
num_parts = msg_unpack_int();
|
|
|
|
part = msg_unpack_int();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg != NETMSG_SNAPEMPTY)
|
|
|
|
part_size = msg_unpack_int();
|
|
|
|
|
|
|
|
if(snapshot_part == part)
|
|
|
|
{
|
|
|
|
// TODO: clean this up abit
|
|
|
|
const char *d = (const char *)msg_unpack_raw(part_size);
|
|
|
|
mem_copy((char*)snapshots[SNAP_INCOMMING] + part*MAX_SNAPSHOT_PACKSIZE, d, part_size);
|
|
|
|
snapshot_part++;
|
|
|
|
|
|
|
|
if(snapshot_part == num_parts)
|
2007-07-14 13:09:42 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
snapshot *tmp = snapshots[SNAP_PREV];
|
|
|
|
snapshots[SNAP_PREV] = snapshots[SNAP_CURRENT];
|
|
|
|
snapshots[SNAP_CURRENT] = tmp;
|
|
|
|
current_tick = game_tick;
|
|
|
|
|
|
|
|
// 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(snapshots[SNAP_INCOMMING], part_size, tmpbuffer);
|
|
|
|
int intsize = intpack_decompress(tmpbuffer, compsize, tmpbuffer2);
|
|
|
|
deltadata = tmpbuffer2;
|
|
|
|
deltasize = intsize;
|
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
// find snapshot that we should use as delta
|
|
|
|
static snapshot emptysnap;
|
|
|
|
emptysnap.data_size = 0;
|
|
|
|
emptysnap.num_items = 0;
|
|
|
|
|
|
|
|
snapshot *deltashot = &emptysnap;
|
|
|
|
int deltashot_size;
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
if(delta_tick >= 0)
|
2007-07-14 13:09:42 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
void *delta_data;
|
|
|
|
deltashot_size = snapshots_new.get(delta_tick, &delta_data);
|
|
|
|
if(deltashot_size >= 0)
|
|
|
|
{
|
|
|
|
deltashot = (snapshot *)delta_data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: handle this
|
|
|
|
dbg_msg("client", "error, couldn't find the delta snapshot");
|
|
|
|
}
|
2007-07-14 13:09:42 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
|
|
|
|
int snapsize = snapshot_unpack_delta(deltashot, (snapshot*)snapshots[SNAP_CURRENT], deltadata, deltasize);
|
|
|
|
//snapshot *shot = (snapshot *)snapshots[SNAP_CURRENT];
|
|
|
|
|
|
|
|
// purge old snapshots
|
|
|
|
snapshots_new.purge_until(delta_tick);
|
|
|
|
snapshots_new.purge_until(game_tick-50); // TODO: change this to server tickrate
|
|
|
|
|
|
|
|
// add new
|
|
|
|
snapshots_new.add(game_tick, snapsize, snapshots[SNAP_CURRENT]);
|
|
|
|
|
|
|
|
// apply snapshot, cycle pointers
|
|
|
|
recived_snapshots++;
|
|
|
|
snapshot_start_time = time_get();
|
|
|
|
|
|
|
|
// we got two snapshots until we see us self as connected
|
|
|
|
if(recived_snapshots == 2)
|
2007-07-14 13:09:42 +00:00
|
|
|
{
|
2007-07-21 18:07:27 +00:00
|
|
|
local_start_time = time_get();
|
|
|
|
set_state(STATE_ONLINE);
|
2007-07-14 13:09:42 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
|
|
|
|
if(recived_snapshots > 2)
|
|
|
|
modc_newsnapshot();
|
|
|
|
|
|
|
|
snapshot_part = 0;
|
|
|
|
|
|
|
|
// ack snapshot
|
|
|
|
msg_pack_start_system(NETMSG_SNAPACK, 0);
|
|
|
|
msg_pack_int(game_tick);
|
|
|
|
msg_pack_end();
|
|
|
|
client_send_msg();
|
2007-07-13 13:40:04 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_msg("client", "snapshot reset!");
|
2007-07-14 13:09:42 +00:00
|
|
|
snapshot_part = 0;
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
2007-07-14 13:09:42 +00:00
|
|
|
}
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
2007-07-21 18:07:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// game message
|
|
|
|
modc_message(msg);
|
|
|
|
}
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pump_network()
|
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
net.update();
|
|
|
|
|
|
|
|
// check for errors
|
|
|
|
if(get_state() != STATE_OFFLINE && net.state() == NETSTATE_OFFLINE)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
// TODO: add message to the user there
|
|
|
|
set_state(STATE_OFFLINE);
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-05-22 15:03:32 +00:00
|
|
|
//
|
2007-07-13 13:40:04 +00:00
|
|
|
if(get_state() == STATE_CONNECTING && net.state() == NETSTATE_ONLINE)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
// we switched to online
|
|
|
|
dbg_msg("client", "connected, sending info");
|
|
|
|
set_state(STATE_LOADING);
|
|
|
|
send_info();
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
2007-07-13 13:40:04 +00:00
|
|
|
|
|
|
|
// process packets
|
|
|
|
NETPACKET packet;
|
|
|
|
while(net.recv(&packet))
|
|
|
|
process_packet(&packet);
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-07-15 13:25:10 +00:00
|
|
|
int editor_main(int argc, char **argv);
|
|
|
|
|
2007-05-24 20:54:08 +00:00
|
|
|
int main(int argc, char **argv)
|
2007-05-22 15:03:32 +00:00
|
|
|
{
|
|
|
|
dbg_msg("client", "starting...");
|
2007-07-13 13:40:04 +00:00
|
|
|
|
2007-06-01 02:21:46 +00:00
|
|
|
config_reset();
|
|
|
|
config_load("teewars.cfg");
|
|
|
|
|
2007-07-21 18:07:27 +00:00
|
|
|
const char *direct_connect_server = 0x0;
|
2007-07-21 17:43:00 +00:00
|
|
|
snd_set_master_volume(config.volume / 255.0f);
|
2007-07-15 13:25:10 +00:00
|
|
|
bool editor = false;
|
2007-05-22 15:03:32 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2007-07-13 13:40:04 +00:00
|
|
|
// -c SERVER:PORT
|
2007-05-22 15:03:32 +00:00
|
|
|
i++;
|
2007-07-21 18:07:27 +00:00
|
|
|
direct_connect_server = argv[i];
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
else if(argv[i][0] == '-' && argv[i][1] == 'n' && argv[i][2] == 0 && argc - i > 1)
|
|
|
|
{
|
|
|
|
// -n NAME
|
|
|
|
i++;
|
2007-06-01 11:17:10 +00:00
|
|
|
config_set_player_name(&config, argv[i]);
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
else if(argv[i][0] == '-' && argv[i][1] == 'w' && argv[i][2] == 0)
|
|
|
|
{
|
|
|
|
// -w
|
2007-07-21 16:33:01 +00:00
|
|
|
config.fullscreen = 0;
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
2007-07-15 13:25:10 +00:00
|
|
|
else if(argv[i][0] == '-' && argv[i][1] == 'e' && argv[i][2] == 0)
|
|
|
|
{
|
|
|
|
editor = true;
|
|
|
|
}
|
2007-05-22 15:03:32 +00:00
|
|
|
}
|
|
|
|
|
2007-07-15 13:25:10 +00:00
|
|
|
if(editor)
|
|
|
|
editor_main(argc, argv);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// start the client
|
|
|
|
client c;
|
2007-07-21 18:07:27 +00:00
|
|
|
c.run(direct_connect_server);
|
2007-07-15 13:25:10 +00:00
|
|
|
}
|
2007-05-22 15:03:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|