diff options
| author | Olle Rosenquist <phobos99@gmail.com> | 2007-07-22 11:53:15 +0000 |
|---|---|---|
| committer | Olle Rosenquist <phobos99@gmail.com> | 2007-07-22 11:53:15 +0000 |
| commit | 26dd1c20cee2bf814396cb941c6aea68e63d664f (patch) | |
| tree | 96b2ab24e5d5e59ba5b1108b6db090aa93e5c801 /src | |
| parent | 2165a728c25d804128933408cd8753c333be9ed3 (diff) | |
| download | zcatch-26dd1c20cee2bf814396cb941c6aea68e63d664f.tar.gz zcatch-26dd1c20cee2bf814396cb941c6aea68e63d664f.zip | |
Updated stuff
Diffstat (limited to 'src')
| -rw-r--r-- | src/engine/client/client.cpp | 920 | ||||
| -rw-r--r-- | src/engine/client/client.h | 48 | ||||
| -rw-r--r-- | src/engine/server/server.cpp | 2 | ||||
| -rw-r--r-- | src/game/client/game_client.cpp | 192 | ||||
| -rw-r--r-- | src/game/client/menu.h | 17 | ||||
| -rw-r--r-- | src/game/game.h | 6 | ||||
| -rw-r--r-- | src/game/game_variables.h | 2 | ||||
| -rw-r--r-- | src/game/server/game_server.cpp | 19 |
8 files changed, 681 insertions, 525 deletions
diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 7fb9413e..0dcab9a6 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -18,9 +18,10 @@ #include <engine/versions.h> #include <engine/config.h> -#include <engine/network.h> +//#include <engine/network.h> #include <mastersrv/mastersrv.h> +#include "client.h" using namespace baselib; @@ -274,567 +275,555 @@ void client_connect(const char *server_address_str) // --- client --- // TODO: remove this class -class client +void client::send_info() { -public: - int info_request_begin; - int info_request_end; - - int snapshot_part; + recived_snapshots = 0; - int debug_font; // TODO: rfemove this line + msg_pack_start_system(NETMSG_INFO, MSGFLAG_VITAL); + 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(); +} - // data to hold three snapshots - // previous, +void client::send_entergame() +{ + msg_pack_start_system(NETMSG_ENTERGAME, MSGFLAG_VITAL); + msg_pack_end(); + client_send_msg(); +} - void send_info() - { - recived_snapshots = 0; - - msg_pack_start_system(NETMSG_INFO, MSGFLAG_VITAL); - 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(); - } +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); + */ +} + +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(); +} - void send_entergame() - { - msg_pack_start_system(NETMSG_ENTERGAME, MSGFLAG_VITAL); - msg_pack_end(); - client_send_msg(); - } +void client::disconnect() +{ + send_error("disconnected"); + set_state(STATE_OFFLINE); + map_unload(); +} - void 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); - */ - } - - void 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(); - } +bool client::load_data() +{ + debug_font = gfx_load_texture("data/debug_font.png"); + return true; +} + +void client::debug_render() +{ + gfx_blend_normal(); + gfx_texture_set(debug_font); + gfx_mapscreen(0,0,gfx_screenwidth(),gfx_screenheight()); - void disconnect() + static NETSTATS prev, current; + static int64 last_snap = 0; + if(time_get()-last_snap > time_freq()/10) { - send_error("disconnected"); - set_state(STATE_OFFLINE); - map_unload(); + last_snap = time_get(); + prev = current; + net.stats(¤t); } - bool load_data() - { - debug_font = gfx_load_texture("data/debug_font.png"); - return true; - } + 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); - void debug_render() +} + +void client::render() +{ + gfx_clear(0.0f,0.0f,0.0f); + + // this should be moved around abit + // TODO: clean this shit up! + if(get_state() == STATE_ONLINE) { - gfx_blend_normal(); - gfx_texture_set(debug_font); - gfx_mapscreen(0,0,gfx_screenwidth(),gfx_screenheight()); + modc_render(); - 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); + // debug render stuff + debug_render(); } - - void render() + else if (get_state() != STATE_CONNECTING && get_state() != STATE_LOADING) { - gfx_clear(0.0f,0.0f,0.0f); + //netaddr4 server_address; + int status = modmenu_render(); + + if (status == -1) + set_state(STATE_QUIT); + } + else if (get_state() == STATE_CONNECTING || get_state() == STATE_LOADING) + { + static int64 start = time_get(); + static int tee_texture; + static int connecting_texture; + static bool inited = false; - // this should be moved around abit - // TODO: clean this shit up! - if(get_state() == STATE_ONLINE) + if (!inited) { - modc_render(); - - // debug render stuff - debug_render(); - - } - else if (get_state() != STATE_CONNECTING && get_state() != STATE_LOADING) - { - //netaddr4 server_address; - int status = modmenu_render(); - - if (status == -1) - set_state(STATE_QUIT); + tee_texture = gfx_load_texture("data/gui_tee.png"); + connecting_texture = gfx_load_texture("data/gui/connecting.png"); + + inited = true; } - else if (get_state() == STATE_CONNECTING || get_state() == STATE_LOADING) - { - static int64 start = time_get(); - static int tee_texture; - static int connecting_texture; - static bool inited = false; - - if (!inited) - { - tee_texture = gfx_load_texture("data/gui_tee.png"); - connecting_texture = gfx_load_texture("data/gui/connecting.png"); - - inited = true; - } - gfx_mapscreen(0,0,400.0f,300.0f); + gfx_mapscreen(0,0,400.0f,300.0f); - float t = (time_get() - start) / (double)time_freq(); + float t = (time_get() - start) / (double)time_freq(); - float speed = 2*sin(t); + float speed = 2*sin(t); - speed = 1.0f; + speed = 1.0f; - float x = 208 + sin(t*speed) * 32; - float w = sin(t*speed + 3.149) * 64; + 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); - } + ui_do_image(tee_texture, x, 95, w, 64); + ui_do_image(connecting_texture, 88, 150, 256, 64); } +} + +void client::run(const char *direct_connect_server) +{ + local_start_time = time_get(); + snapshot_part = 0; + info_request_begin = 0; + info_request_end = 0; - void 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; + client_serverbrowse_init(); + + // init graphics and sound + if(!gfx_init()) + return; - snd_init(); // sound is allowed to fail - - // load data - if(!load_data()) - return; + snd_init(); // sound is allowed to fail + + // load data + if(!load_data()) + return; - // init snapshotting - snap_init(); + // init snapshotting + snap_init(); + + // init the mod + modc_init(); + + // init menu + modmenu_init(); + + // open socket + net.open(0, 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); - // init the mod - modc_init(); + //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(); - // init menu - modmenu_init(); + // send input + if(get_state() == STATE_ONLINE) + { + if(input_is_changed || time_get() > last_input+time_freq()) + { + send_input(); + input_is_changed = 0; + last_input = time_get(); + } + } - // open socket - net.open(0, 0); + // update input + inp_update(); // - net_host_lookup(config.masterserver, MASTERSERVER_PORT, &master_server); + if(input::pressed(input::f1)) + input::set_mouse_mode(input::mode_absolute); + if(input::pressed(input::f2)) + input::set_mouse_mode(input::mode_relative); - // connect to the server if wanted - if(direct_connect_server) - client_connect(direct_connect_server); + // panic button + if(input::pressed(input::lctrl) && input::pressed('Q')) + break; - //int64 inputs_per_second = 50; - //int64 time_per_input = time_freq()/inputs_per_second; - int64 game_starttime = time_get(); - int64 last_input = game_starttime; + // pump the network + pump_network(); - int64 reporttime = time_get(); - int64 reportinterval = time_freq()*1; - int frames = 0; + // update the server browser + serverbrowse_update(); - input::set_mouse_mode(input::mode_relative); + // render + render(); - while (1) - { - frames++; - int64 frame_start_time = time_get(); - - // send input - if(get_state() == STATE_ONLINE) - { - if(input_is_changed || time_get() > last_input+time_freq()) - { - send_input(); - input_is_changed = 0; - last_input = time_get(); - } - } - - // update input - inp_update(); - - // - if(input::pressed(input::f1)) - input::set_mouse_mode(input::mode_absolute); - if(input::pressed(input::f2)) - input::set_mouse_mode(input::mode_relative); - - // panic button - if(input::pressed(input::lctrl) && input::pressed('Q')) - break; - - // pump the network - pump_network(); - - // update the server browser - serverbrowse_update(); - - // 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()) - { - dbg_msg("client/report", "fps=%.02f netstate=%d", - frames/(float)(reportinterval/time_freq()), net.state()); - frames = 0; - reporttime += reportinterval; - } - - if (input::pressed(input::esc)) - if (get_state() == STATE_CONNECTING || get_state() == STATE_ONLINE) - disconnect(); + // swap the buffers + gfx_swap(); + + // check conditions + if(get_state() == STATE_BROKEN || get_state() == STATE_QUIT) + break; - // update frametime - frametime = (time_get()-frame_start_time)/(float)time_freq(); + // be nice + thread_sleep(1); + + if(reporttime < time_get()) + { + dbg_msg("client/report", "fps=%.02f netstate=%d", + frames/(float)(reportinterval/time_freq()), net.state()); + frames = 0; + reporttime += reportinterval; } - modc_shutdown(); - disconnect(); + /*if (input::pressed(input::esc)) + if (get_state() == STATE_CONNECTING || get_state() == STATE_ONLINE) + disconnect();*/ - modmenu_shutdown(); - - gfx_shutdown(); - snd_shutdown(); + // update frametime + frametime = (time_get()-frame_start_time)/(float)time_freq(); } - void error(const char *msg) - { - dbg_msg("game", "error: %s", msg); - send_error(msg); - set_state(STATE_BROKEN); - } + modc_shutdown(); + disconnect(); + + modmenu_shutdown(); + + gfx_shutdown(); + snd_shutdown(); +} + +void client::error(const char *msg) +{ + dbg_msg("game", "error: %s", msg); + send_error(msg); + set_state(STATE_BROKEN); +} + +void client::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 client::serverbrowse_update() +{ + int64 timeout = time_freq(); + int64 now = time_get(); + int max_requests = 10; - void serverbrowse_request(int id) + // timeout old requests + while(info_request_begin < servers.num && info_request_begin < info_request_end) { - 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(); + if(now > servers.request_times[info_request_begin]+timeout) + info_request_begin++; + else + break; } - void serverbrowse_update() + // send new requests + while(info_request_end < servers.num && info_request_end-info_request_begin < max_requests) { - 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++; - } + serverbrowse_request(info_request_end); + info_request_end++; } +} - void process_packet(NETPACKET *packet) +void client::process_packet(NETPACKET *packet) +{ + if(packet->client_id == -1) { - if(packet->client_id == -1) + // connectionlesss + if(packet->data_size >= (int)sizeof(SERVERBROWSE_LIST) && + memcmp(packet->data, SERVERBROWSE_LIST, sizeof(SERVERBROWSE_LIST)) == 0) { - // 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); + // 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; + info_request_begin = 0; + info_request_end = 0; - for(int i = 0; i < servers.num; i++) + 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; + 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) { - servers.infos[i].num_players = 0; - servers.infos[i].max_players = 0; - servers.infos[i].latency = 999; + 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", - 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); + packet->address.ip[0], packet->address.ip[1], packet->address.ip[2], + packet->address.ip[3], packet->address.port); + + dbg_msg("client", "got server info"); + servers.num++; + } } - - if(packet->data_size >= (int)sizeof(SERVERBROWSE_INFO) && - memcmp(packet->data, SERVERBROWSE_INFO, sizeof(SERVERBROWSE_INFO)) == 0) + else { - // 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) + for(int i = 0; i < servers.num; i++) { - if(servers.num != MAX_SERVERS) + if(net_addr4_cmp(&servers.addresses[i], &packet->address) == 0) { - 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); - + servers.infos[i].latency = ((time_get() - servers.request_times[i])*1000)/time_freq(); dbg_msg("client", "got server info"); - servers.num++; - + 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); + 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 { - 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(); - dbg_msg("client", "got server info"); - break; - } - } + error("failure to load map"); } } - } - else - { - - int sys; - int msg = msg_unpack_start(packet->data, packet->data_size, &sys); - if(sys) + else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPSMALL || msg == NETMSG_SNAPEMPTY) { - // system message - if(msg == NETMSG_MAP) + //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) { - 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"); - } + num_parts = msg_unpack_int(); + part = msg_unpack_int(); } - else if(msg == NETMSG_SNAP || msg == NETMSG_SNAPSMALL || msg == NETMSG_SNAPEMPTY) + + if(msg != NETMSG_SNAPEMPTY) + part_size = msg_unpack_int(); + + if(snapshot_part == part) { - //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) - { - 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) { - // 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) + 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) { - 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; - } + int compsize = zerobit_decompress(snapshots[SNAP_INCOMMING], part_size, tmpbuffer); + int intsize = intpack_decompress(tmpbuffer, compsize, tmpbuffer2); + deltadata = tmpbuffer2; + deltasize = intsize; + } - // 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; + // 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; - if(delta_tick >= 0) + if(delta_tick >= 0) + { + void *delta_data; + deltashot_size = snapshots_new.get(delta_tick, 0, &delta_data); + if(deltashot_size >= 0) { - void *delta_data; - deltashot_size = snapshots_new.get(delta_tick, 0, &delta_data); - if(deltashot_size >= 0) - { - deltashot = (snapshot *)delta_data; - } - else - { - // TODO: handle this - dbg_msg("client", "error, couldn't find the delta snapshot"); - } + deltashot = (snapshot *)delta_data; } - - 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, time_get(), 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) + else { - local_start_time = time_get(); - set_state(STATE_ONLINE); + // TODO: handle this + dbg_msg("client", "error, couldn't find the delta snapshot"); } - - 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(); } - } - else - { - dbg_msg("client", "snapshot reset!"); + + 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, time_get(), 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) + { + local_start_time = time_get(); + set_state(STATE_ONLINE); + } + + 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(); } } - } - else - { - // game message - modc_message(msg); + else + { + dbg_msg("client", "snapshot reset!"); + snapshot_part = 0; + } } } - } - - void pump_network() - { - net.update(); - - // check for errors - if(get_state() != STATE_OFFLINE && net.state() == NETSTATE_OFFLINE) + else { - // TODO: add message to the user there - set_state(STATE_OFFLINE); + // game message + modc_message(msg); } + } +} - // - if(get_state() == STATE_CONNECTING && net.state() == NETSTATE_ONLINE) - { - // we switched to online - dbg_msg("client", "connected, sending info"); - set_state(STATE_LOADING); - send_info(); - } - - // process packets - NETPACKET packet; - while(net.recv(&packet)) - process_packet(&packet); - } -}; +void client::pump_network() +{ + net.update(); + + // check for errors + if(get_state() != STATE_OFFLINE && net.state() == NETSTATE_OFFLINE) + { + // TODO: add message to the user there + set_state(STATE_OFFLINE); + } + + // + if(get_state() == STATE_CONNECTING && net.state() == NETSTATE_ONLINE) + { + // we switched to online + dbg_msg("client", "connected, sending info"); + set_state(STATE_LOADING); + send_info(); + } + + // process packets + NETPACKET packet; + while(net.recv(&packet)) + process_packet(&packet); +} int editor_main(int argc, char **argv); +client main_client; + int main(int argc, char **argv) { dbg_msg("client", "starting..."); @@ -880,8 +869,7 @@ int main(int argc, char **argv) else { // start the client - client c; - c.run(direct_connect_server); + main_client.run(direct_connect_server); } return 0; } diff --git a/src/engine/client/client.h b/src/engine/client/client.h new file mode 100644 index 00000000..db9d142d --- /dev/null +++ b/src/engine/client/client.h @@ -0,0 +1,48 @@ +#ifndef __CLIENT_H +#define __CLIENT_H + +#include <engine/network.h> +// --- client --- +// TODO: remove this class +class client +{ +public: + int info_request_begin; + int info_request_end; + + int snapshot_part; + + int debug_font; // TODO: rfemove this line + + // data to hold three snapshots + // previous, + + void send_info(); + + void send_entergame(); + + void send_error(const char *error); + + void send_input(); + + void disconnect(); + + bool load_data(); + + void debug_render(); + + void render(); + + void run(const char *direct_connect_server); + + void error(const char *msg); + + void serverbrowse_request(int id); + + void serverbrowse_update(); + void process_packet(NETPACKET *packet); + + void pump_network(); +}; + +#endif \ No newline at end of file diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 71fb9b94..77a32962 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -249,7 +249,7 @@ public: reporttime += time_freq()*reportinterval; } totaltime += time_get()-t; - thread_sleep(1); + //thread_sleep(1); } mods_shutdown(); diff --git a/src/game/client/game_client.cpp b/src/game/client/game_client.cpp index 094f302a..511df061 100644 --- a/src/game/client/game_client.cpp +++ b/src/game/client/game_client.cpp @@ -4,11 +4,15 @@ #include <stdio.h> #include <string.h> #include <engine/config.h> +#include <engine/client/ui.h> +#include <engine/client/client.h> #include "../game.h" #include "mapres_image.h" #include "mapres_tilemap.h" #include "data.h" +#include "menu.h" +extern client main_client; using namespace baselib; data_container *data = 0x0; @@ -17,6 +21,11 @@ int charids[16] = {2,10,0,4,12,6,14,1,9,15,13,11,7,5,8,3}; static int gametype = GAMETYPE_DM; static int skinseed = 0; +static int menu_team = 0; +static int menu_quit = 0; + +static bool chat_active = false; +static bool menu_active = false; static vec2 mouse_pos; static vec2 local_player_pos; @@ -25,6 +34,7 @@ static obj_player *local_player; struct client_data { char name[64]; + int team; } client_datas[MAX_CLIENTS]; inline float frandom() { return rand()/(float)(RAND_MAX); } @@ -373,7 +383,6 @@ public: }; static projectile_particles proj_particles; -static bool chat_active = false; static char chat_input[512]; static unsigned chat_input_len; static const int chat_max_lines = 10; @@ -436,7 +445,10 @@ void modc_entergame() chat_reset(); for(int i = 0; i < MAX_CLIENTS; i++) + { client_datas[i].name[0] = 0; + client_datas[i].team = 0; + } for(int i = 0; i < killmsg_max; i++) killmsgs[i].tick = -100000; @@ -909,7 +921,7 @@ static void render_player(obj_player *prev, obj_player *player) draw_sprite(p.x, p.y, data->weapons[iw].visual_size); } - if (player->weapon == WEAPON_TYPE_GUN || player->weapon == WEAPON_TYPE_SHOTGUN) + if (player->weapon == WEAPON_GUN || player->weapon == WEAPON_SHOTGUN) { // check if we're firing stuff if (true)///prev->attackticks) @@ -987,54 +999,116 @@ void render_sun(float x, float y) gfx_quads_end(); } +void ingamemenu_render() +{ + if (!local_player) + return; + gfx_mapscreen(0, 0, 800, 600); + // Ingame menu - quit and change team (if tdm) + float mx,my; + int rx, ry; + inp_mouse_relative(&rx, &ry); + mouse_pos.x += rx; + mouse_pos.y += ry; + if(mouse_pos.x < 0) mouse_pos.x = 0; + if(mouse_pos.y < 0) mouse_pos.y = 0; + if(mouse_pos.x > gfx_screenwidth()) mouse_pos.x = gfx_screenwidth(); + if(mouse_pos.y > gfx_screenheight()) mouse_pos.y = gfx_screenheight(); + + // update the ui + mx = (mouse_pos.x/(float)gfx_screenwidth())*800.0f; + my = (mouse_pos.y/(float)gfx_screenheight())*600.0f; + + int buttons = 0; + if(inp_key_pressed(input::mouse_1)) buttons |= 1; + if(inp_key_pressed(input::mouse_2)) buttons |= 2; + if(inp_key_pressed(input::mouse_3)) buttons |= 4; + + ui_update(mx,my,mx*3.0f,my*3.0f,buttons); + + gfx_texture_set(cursor_texture); + gfx_quads_begin(); + gfx_quads_setcolor(1,1,1,1); + gfx_quads_drawTL(mx,my,24,24); + gfx_quads_end(); + + char buf[128]; + if (gametype == GAMETYPE_TDM) + { + // Switch team + ui_do_label(100,100,"Switch Team",40); + sprintf(buf,"Team: %s",local_player->team ? "A" : "B"); + if (ui_do_button(&menu_team, buf, 0, 30, 150, 170, 48, draw_teewars_button)) + { + msg_pack_start(MSG_SWITCHTEAM, MSGFLAG_VITAL); + msg_pack_end(); + client_send_msg(); + menu_active = false; + } + } + + if (ui_do_button(&menu_quit, "Quit", 0, 30, 350, 170, 48, draw_teewars_button)) + { + //if (get_state() == STATE_CONNECTING || get_state() == STATE_ONLINE) + main_client.disconnect(); + } +} + void modc_render() { animstate idlestate; anim_eval(&data->animations[ANIM_BASE], 0, &idlestate); anim_eval_add(&idlestate, &data->animations[ANIM_IDLE], 0, 1.0f); - - if(inp_key_down(input::enter)) + + if (inp_key_down(input::esc)) { - if(chat_active) - { - // send message - msg_pack_start(MSG_SAY, MSGFLAG_VITAL); - msg_pack_string(chat_input, 512); - msg_pack_end(); - client_send_msg(); - } - else - { - mem_zero(chat_input, sizeof(chat_input)); - chat_input_len = 0; - } - chat_active = !chat_active; + menu_active = !menu_active; } - - if(chat_active) + if (!menu_active) { - int c = input::last_char(); // TODO: bypasses the engine interface - int k = input::last_key(); // TODO: bypasses the engine interface - - if (c >= 32 && c < 255) + if(inp_key_down(input::enter)) { - if (chat_input_len < sizeof(chat_input) - 1) + if(chat_active) { - chat_input[chat_input_len] = c; - chat_input[chat_input_len+1] = 0; - chat_input_len++; + // send message + msg_pack_start(MSG_SAY, MSGFLAG_VITAL); + msg_pack_string(chat_input, 512); + msg_pack_end(); + client_send_msg(); } + else + { + mem_zero(chat_input, sizeof(chat_input)); + chat_input_len = 0; + } + chat_active = !chat_active; } - - if(k == input::backspace) + + if(chat_active) { - if(chat_input_len > 0) + int c = input::last_char(); // TODO: bypasses the engine interface + int k = input::last_key(); // TODO: bypasses the engine interface + + if (c >= 32 && c < 255) { - chat_input[chat_input_len-1] = 0; - chat_input_len--; + if (chat_input_len < sizeof(chat_input) - 1) + { + chat_input[chat_input_len] = c; + chat_input[chat_input_len+1] = 0; + chat_input_len++; + } } + + if(k == input::backspace) + { + if(chat_input_len > 0) + { + chat_input[chat_input_len-1] = 0; + chat_input_len--; + } + } + } - } input::clear_char(); // TODO: bypasses the engine interface @@ -1063,7 +1137,7 @@ void modc_render() input.target_y = (int)mouse_pos.y; //(int)(a*256.0f); input.activeweapon = -1; - if(!chat_active) + if(!chat_active && !menu_active) { input.left = inp_key_pressed(config.key_move_left); input.right = inp_key_pressed(config.key_move_right); @@ -1157,7 +1231,10 @@ void modc_render() { void *prev = snap_find_item(SNAP_PREV, item.type, item.id); if(prev) + { + client_datas[((obj_player *)data)->clientid].team = ((obj_player *)data)->team; render_player((obj_player *)prev, (obj_player *)data); + } } else if(item.type == OBJTYPE_PROJECTILE) { @@ -1188,9 +1265,12 @@ void modc_render() gfx_quads_begin(); // render cursor - select_sprite(data->weapons[local_player->weapon%data->num_weapons].sprite_cursor); - float cursorsize = 64; - draw_sprite(local_player_pos.x+mouse_pos.x, local_player_pos.y+mouse_pos.y, cursorsize); + if (!menu_active) + { + select_sprite(data->weapons[local_player->weapon%data->num_weapons].sprite_cursor); + float cursorsize = 64; + draw_sprite(local_player_pos.x+mouse_pos.x, local_player_pos.y+mouse_pos.y, cursorsize); + } // render ammo count // render gui stuff @@ -1252,22 +1332,26 @@ void modc_render() // render victim tee x -= 24.0f; - //int skin = gametype == GAMETYPE_TDM ? skinseed + player->team : player->clientid; - render_tee(&idlestate, killmsgs[r].victim, vec2(1,0), vec2(x, y+28)); + int skin = gametype == GAMETYPE_TDM ? skinseed + client_datas[killmsgs[r].victim].team : killmsgs[r].victim; + render_tee(&idlestate, skin, vec2(1,0), vec2(x, y+28)); x -= 32.0f; // render weapon x -= 44.0f; - gfx_texture_set(data->images[IMAGE_WEAPONS].id); - gfx_quads_begin(); - select_sprite(data->weapons[killmsgs[r].weapon].sprite_body); - draw_sprite(x, y+28, 96); - gfx_quads_end(); + if (killmsgs[r].weapon >= 0) + { + gfx_texture_set(data->images[IMAGE_WEAPONS].id); + gfx_quads_begin(); + select_sprite(data->weapons[killmsgs[r].weapon].sprite_body); + draw_sprite(x, y+28, 96); + gfx_quads_end(); + } x -= 52.0f; // render killer tee x -= 24.0f; - render_tee(&idlestate, killmsgs[r].killer, vec2(1,0), vec2(x, y+28)); + skin = gametype == GAMETYPE_TDM ? skinseed + client_datas[killmsgs[r].killer].team : killmsgs[r].killer; + render_tee(&idlestate, skin, vec2(1,0), vec2(x, y+28)); x -= 32.0f; // render killer name @@ -1277,6 +1361,12 @@ void modc_render() y += 44; } } + + if (menu_active) + { + ingamemenu_render(); + return; + } // render chat { @@ -1410,7 +1500,8 @@ void modc_render() // Team deathmatch gfx_blend_normal(); - float x = 50.0f; + float w = 650.0f; + float x = width-w-50.0f; float y = 150.0f; gfx_texture_set(-1); @@ -1483,7 +1574,12 @@ void modc_render() gfx_pretty_text(offsetx + x+128, offsets[player->team], 48, client_datas[player->clientid].name); int skin = skinseed + player->team; - render_tee(&idlestate, skin, vec2(1,0), vec2(offsetx + x+90, y+24)); + render_tee(&idlestate, skin, vec2(1,0), vec2(offsetx + x+90, offsets[player->team]+24)); + + sprintf(buf, "%4d", player->latency); + float tw = gfx_pretty_text_width(48.0f, buf); + gfx_pretty_text(offsetx + x + 220, y, 48, buf); + offsets[player->team] += 58.0f; } } diff --git a/src/game/client/menu.h b/src/game/client/menu.h new file mode 100644 index 00000000..0a8c1fe3 --- /dev/null +++ b/src/game/client/menu.h @@ -0,0 +1,17 @@ +#ifndef __MENU_H +#define __MENU_H + +extern int cursor_texture; + +void draw_image_button(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra); +void draw_single_part_button(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra); +void draw_menu_button(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra); +void draw_teewars_button(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra); +int ui_do_key_reader(void *id, float x, float y, float w, float h, int key); +int ui_do_combo_box(void *id, float x, float y, float w, char *lines, int line_count, int selected_index); +int ui_do_edit_box(void *id, float x, float y, float w, float h, char *str, int str_size); +int ui_do_check_box(void *id, float x, float y, float w, float h, int value); +int do_scroll_bar_horiz(void *id, float x, float y, float width, int steps, int last_index); +int do_scroll_bar_vert(void *id, float x, float y, float height, int steps, int last_index); + +#endif \ No newline at end of file diff --git a/src/game/game.h b/src/game/game.h index c9010aa0..3ed3fd05 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -49,6 +49,7 @@ enum MSG_CHAT, MSG_SETNAME, MSG_KILLMSG, + MSG_SWITCHTEAM, }; enum @@ -157,11 +158,6 @@ struct obj_player enum { - WEAPON_TYPE_GUN = 0, - WEAPON_TYPE_ROCKET = 1, - WEAPON_TYPE_SHOTGUN = 2, - WEAPON_TYPE_MELEE = 3, - WEAPON_NUMWEAPONS, //WEAPON_TYPE_SNIPER = 2, POWERUP_TYPE_HEALTH = 0, diff --git a/src/game/game_variables.h b/src/game/game_variables.h index 8775074e..04deceb6 100644 --- a/src/game/game_variables.h +++ b/src/game/game_variables.h @@ -4,5 +4,5 @@ MACRO_CONFIG_INT(key_jump, 32, 32, 512) MACRO_CONFIG_INT(key_fire, 384, 32, 512) MACRO_CONFIG_INT(key_hook, 385, 32, 512) -MACRO_CONFIG_INT(scorelimit, 20, 0, 1000) +MACRO_CONFIG_INT(scorelimit, 2, 0, 1000) MACRO_CONFIG_INT(timelimit, 0, 0, 1000) diff --git a/src/game/server/game_server.cpp b/src/game/server/game_server.cpp index a6884a58..6d026fb4 100644 --- a/src/game/server/game_server.cpp +++ b/src/game/server/game_server.cpp @@ -1640,6 +1640,13 @@ void mods_message(int msg, int client_id) msg_pack_end(); server_send_msg(-1); } + else if (msg == MSG_SWITCHTEAM) + { + // Switch team on given client and kill/respawn him + players[client_id].team = !players[client_id].team; + players[client_id].die(client_id, -1); + players[client_id].score--; + } } void mods_init() @@ -1662,19 +1669,19 @@ void mods_init() { case ITEM_WEAPON_GUN: type = POWERUP_TYPE_WEAPON; - subtype = WEAPON_TYPE_GUN; + subtype = WEAPON_GUN; break; case ITEM_WEAPON_SHOTGUN: type = POWERUP_TYPE_WEAPON; - subtype = WEAPON_TYPE_SHOTGUN; + subtype = WEAPON_SHOTGUN; break; case ITEM_WEAPON_ROCKET: type = POWERUP_TYPE_WEAPON; - subtype = WEAPON_TYPE_ROCKET; + subtype = WEAPON_ROCKET; break; case ITEM_WEAPON_HAMMER: type = POWERUP_TYPE_WEAPON; - subtype = WEAPON_TYPE_MELEE; + subtype = WEAPON_HAMMER; break; case ITEM_HEALTH: @@ -1684,6 +1691,10 @@ void mods_init() case ITEM_ARMOR: type = POWERUP_TYPE_ARMOR; break; + case ITEM_NINJA: + type = POWERUP_TYPE_NINJA; + subtype = WEAPON_NINJA; + break; }; powerup *ppower = new powerup(type, subtype); |