diff options
| author | Magnus Auvinen <magnus.auvinen@gmail.com> | 2008-09-03 20:03:01 +0000 |
|---|---|---|
| committer | Magnus Auvinen <magnus.auvinen@gmail.com> | 2008-09-03 20:03:01 +0000 |
| commit | 53da3f0d40ff9eb171b3d8eaaeed148e9ddd2f8b (patch) | |
| tree | d9014120bd015f7e3fcc4f2cf997321ba0494b70 /src/engine/client | |
| parent | 37f3fa6c6efe231d5a172dbee6a50d63bf42b910 (diff) | |
| download | zcatch-53da3f0d40ff9eb171b3d8eaaeed148e9ddd2f8b.tar.gz zcatch-53da3f0d40ff9eb171b3d8eaaeed148e9ddd2f8b.zip | |
added favorites. no saving of them yet however
Diffstat (limited to 'src/engine/client')
| -rw-r--r-- | src/engine/client/ec_client.c | 32 | ||||
| -rw-r--r-- | src/engine/client/ec_gfx.c | 48 | ||||
| -rw-r--r-- | src/engine/client/ec_srvbrowse.c | 164 |
3 files changed, 186 insertions, 58 deletions
diff --git a/src/engine/client/ec_client.c b/src/engine/client/ec_client.c index 7445974f..bc76edfd 100644 --- a/src/engine/client/ec_client.c +++ b/src/engine/client/ec_client.c @@ -43,7 +43,7 @@ const int prediction_margin = 7; /* magic network prediction value */ NETCLIENT *net; /* TODO: ugly, fix me */ -extern void client_serverbrowse_set(NETADDR *addr, int request, SERVER_INFO *info); +extern void client_serverbrowse_set(NETADDR *addr, int request, int token, SERVER_INFO *info); static int snapshot_part; static int64 local_start_time; @@ -737,39 +737,21 @@ static void client_process_packet(NETCHUNK *packet) addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], addr.port); - client_serverbrowse_set(&addr, 1, &info); + client_serverbrowse_set(&addr, 1, -1, &info); } } { - int got_info_packet = 0; - - if(client_serverbrowse_lan()) - { - if(packet->data_size >= (int)sizeof(SERVERBROWSE_INFO_LAN) && - memcmp(packet->data, SERVERBROWSE_INFO_LAN, sizeof(SERVERBROWSE_INFO_LAN)) == 0) - { - got_info_packet = 1; - } - } - else - { - if(packet->data_size >= (int)sizeof(SERVERBROWSE_INFO) && - memcmp(packet->data, SERVERBROWSE_INFO, sizeof(SERVERBROWSE_INFO)) == 0) - { - got_info_packet = 1; - } - } - - if(got_info_packet) + if(packet->data_size >= (int)sizeof(SERVERBROWSE_INFO) && memcmp(packet->data, SERVERBROWSE_INFO, sizeof(SERVERBROWSE_INFO)) == 0) { /* we got ze info */ UNPACKER up; SERVER_INFO info = {0}; int i; - + int token = -1; + unpacker_reset(&up, (unsigned char*)packet->data+sizeof(SERVERBROWSE_INFO), packet->data_size-sizeof(SERVERBROWSE_INFO)); - + token = atol(unpacker_get_string(&up)); str_copy(info.version, unpacker_get_string(&up), sizeof(info.version)); str_copy(info.name, unpacker_get_string(&up), sizeof(info.name)); str_copy(info.map, unpacker_get_string(&up), sizeof(info.map)); @@ -792,7 +774,7 @@ static void client_process_packet(NETCHUNK *packet) { /* sort players */ qsort(info.players, info.num_players, sizeof(*info.players), player_score_comp); - client_serverbrowse_set(&packet->address, 0, &info); + client_serverbrowse_set(&packet->address, 0, token, &info); } } } diff --git a/src/engine/client/ec_gfx.c b/src/engine/client/ec_gfx.c index 279194ef..b44f2676 100644 --- a/src/engine/client/ec_gfx.c +++ b/src/engine/client/ec_gfx.c @@ -958,6 +958,7 @@ void gfx_text_set_cursor(TEXT_CURSOR *cursor, float x, float y, float font_size, cursor->line_count = 1; cursor->line_width = -1; cursor->flags = flags; + cursor->charcount = 0; } void gfx_text_ex(TEXT_CURSOR *cursor, const char *text, int length) @@ -999,7 +1000,7 @@ void gfx_text_ex(TEXT_CURSOR *cursor, const char *text, int length) if(cursor->flags&TEXTFLAG_RENDER) i = 0; - for (i = 0; i < 2; i++) + for(;i < 2; i++) { const unsigned char *current = (unsigned char *)text; int to_render = length; @@ -1022,8 +1023,9 @@ void gfx_text_ex(TEXT_CURSOR *cursor, const char *text, int length) while(to_render > 0) { + int new_line = 0; int this_batch = to_render; - if(cursor->line_width > 0) + if(cursor->line_width > 0 && !(cursor->flags&TEXTFLAG_STOP_AT_END)) { int wlen = word_length((char *)current); TEXT_CURSOR compare = *cursor; @@ -1033,12 +1035,27 @@ void gfx_text_ex(TEXT_CURSOR *cursor, const char *text, int length) compare.line_width = -1; gfx_text_ex(&compare, text, wlen); - if(compare.x-cursor->start_x > cursor->line_width) + if(compare.x-draw_x > cursor->line_width) { - draw_x = cursor->start_x; - draw_y += size; - draw_x = (int)(draw_x * fake_to_screen_x) / fake_to_screen_x; /* realign */ - draw_y = (int)(draw_y * fake_to_screen_y) / fake_to_screen_y; + /* word can't be fitted in one line, cut it */ + TEXT_CURSOR cutter = *cursor; + cutter.charcount = 0; + cutter.x = draw_x; + cutter.y = draw_y; + cutter.flags &= ~TEXTFLAG_RENDER; + cutter.flags |= TEXTFLAG_STOP_AT_END; + + gfx_text_ex(&cutter, (const char *)current, wlen); + wlen = cutter.charcount; + new_line = 1; + + if(wlen <= 3) /* if we can't place 3 chars of the word on this line, take the next */ + wlen = 0; + } + else if(compare.x-cursor->start_x > cursor->line_width) + { + new_line = 1; + wlen = 0; } this_batch = wlen; @@ -1075,9 +1092,26 @@ void gfx_text_ex(TEXT_CURSOR *cursor, const char *text, int length) } advance = x_advance + font_kerning(font, *current, *(current+1)); + + if(cursor->flags&TEXTFLAG_STOP_AT_END && draw_x+advance*size-cursor->start_x > cursor->line_width) + { + /* we hit the end of the line, no more to render or count */ + to_render = 0; + break; + } + draw_x += advance*size; + cursor->charcount++; current++; } + + if(new_line) + { + draw_x = cursor->start_x; + draw_y += size; + draw_x = (int)(draw_x * fake_to_screen_x) / fake_to_screen_x; /* realign */ + draw_y = (int)(draw_y * fake_to_screen_y) / fake_to_screen_y; + } } if(cursor->flags&TEXTFLAG_RENDER) diff --git a/src/engine/client/ec_srvbrowse.c b/src/engine/client/ec_srvbrowse.c index e9d8dd75..f9807174 100644 --- a/src/engine/client/ec_srvbrowse.c +++ b/src/engine/client/ec_srvbrowse.c @@ -35,6 +35,14 @@ static HEAP *serverlist_heap = 0; static SERVERENTRY **serverlist = 0; static int *sorted_serverlist = 0; +enum +{ + MAX_FAVORITES=256 +}; + +static NETADDR favorite_servers[MAX_FAVORITES]; +static int num_favorite_servers = 0; + static SERVERENTRY *serverlist_ip[256] = {0}; /* ip hash list */ static SERVERENTRY *first_req_server = 0; /* request list */ @@ -50,6 +58,9 @@ static int sorthash = 0; static char filterstring[64] = {0}; static char filtergametypestring[128] = {0}; +/* the token is to keep server refresh separated from each other */ +static int current_token = 1; + static int serverlist_lan = 1; static int64 broadcast_time = 0; @@ -267,32 +278,48 @@ static void client_serverbrowse_remove_request(SERVERENTRY *entry) } } -void client_serverbrowse_set(NETADDR *addr, int request, SERVER_INFO *info) +static SERVERENTRY *client_serverbrowse_find(NETADDR *addr) +{ + SERVERENTRY *entry = serverlist_ip[addr->ip[0]]; + + for(; entry; entry = entry->next_ip) + { + if(net_addr_comp(&entry->addr, addr) == 0) + return entry; + } + return (SERVERENTRY*)0; +} + +void client_serverbrowse_set(NETADDR *addr, int request, int token, SERVER_INFO *info) { int hash = addr->ip[0]; SERVERENTRY *entry = 0; + int i; + + if(token != -1 && token != current_token) + return; - entry = serverlist_ip[hash]; - while(entry) + entry = client_serverbrowse_find(addr); + if(entry) { - if(net_addr_comp(&entry->addr, addr) == 0) + /* update the server that we already have */ + if(!serverlist_lan) { - /* update the server that we already have */ - if(!serverlist_lan) + int fav = entry->info.favorite; + entry->info = *info; + entry->info.netaddr = *addr; + entry->info.favorite = fav; + + if(!request) { - entry->info = *info; - if(!request) - { - entry->info.latency = (time_get()-entry->request_time)*1000/time_freq(); - client_serverbrowse_remove_request(entry); - } - - entry->got_info = 1; - client_serverbrowse_sort(); + entry->info.latency = (time_get()-entry->request_time)*1000/time_freq(); + client_serverbrowse_remove_request(entry); } - return; + + entry->got_info = 1; + client_serverbrowse_sort(); } - entry = entry->next_ip; + return; } /* create new entry */ @@ -302,10 +329,18 @@ void client_serverbrowse_set(NETADDR *addr, int request, SERVER_INFO *info) /* set the info */ entry->addr = *addr; entry->info = *info; + entry->info.netaddr = *addr; if(serverlist_lan) entry->info.latency = (time_get()-broadcast_time)*1000/time_freq(); + /* check if it's a favorite */ + for(i = 0; i < num_favorite_servers; i++) + { + if(net_addr_comp(addr, &favorite_servers[i]) == 0) + entry->info.favorite = 1; + } + /* add to the hash list */ entry->next_ip = serverlist_ip[hash]; serverlist_ip[hash] = entry; @@ -342,7 +377,7 @@ void client_serverbrowse_set(NETADDR *addr, int request, SERVER_INFO *info) client_serverbrowse_sort(); } -void client_serverbrowse_refresh(int lan) +void client_serverbrowse_refresh(int type) { /* clear out everything */ if(serverlist_heap) @@ -355,13 +390,22 @@ void client_serverbrowse_refresh(int lan) last_req_server = 0; num_requests = 0; + /* next token */ + current_token = (current_token+1)&0xff; /* */ - serverlist_lan = lan; - - if(serverlist_lan) + serverlist_lan = 0; + if(type == BROWSETYPE_LAN) + serverlist_lan = 1; + + if(type == BROWSETYPE_LAN) { + unsigned char buffer[sizeof(SERVERBROWSE_GETINFO)+1]; NETCHUNK packet; + + mem_copy(buffer, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)); + buffer[sizeof(SERVERBROWSE_GETINFO)] = current_token; + packet.client_id = -1; mem_zero(&packet, sizeof(packet)); packet.address.ip[0] = 255; @@ -370,15 +414,15 @@ void client_serverbrowse_refresh(int lan) packet.address.ip[3] = 255; packet.address.port = 8303; packet.flags = NETSENDFLAG_CONNLESS; - packet.data_size = sizeof(SERVERBROWSE_GETINFO_LAN); - packet.data = SERVERBROWSE_GETINFO_LAN; + packet.data_size = sizeof(buffer); + packet.data = buffer; broadcast_time = time_get(); netclient_send(net, &packet); if(config.debug) dbg_msg("client", "broadcasting for servers"); } - else + else if(type == BROWSETYPE_INTERNET) { NETADDR addr; NETCHUNK p; @@ -405,10 +449,31 @@ void client_serverbrowse_refresh(int lan) if(config.debug) dbg_msg("client", "requesting server list"); } + else if(type == BROWSETYPE_FAVORITES) + { + int i; + + for(i = 0; i < num_favorite_servers; i++) + { + SERVER_INFO info = {0}; + NETADDR addr = favorite_servers[i]; + + info.latency = 999; + str_format(info.address, sizeof(info.address), "%d.%d.%d.%d:%d", + addr.ip[0], addr.ip[1], addr.ip[2], + addr.ip[3], addr.port); + str_format(info.name, sizeof(info.name), "\255%d.%d.%d.%d:%d", /* the \255 is to make sure that it's sorted last */ + addr.ip[0], addr.ip[1], addr.ip[2], + addr.ip[3], addr.port); + + client_serverbrowse_set(&addr, 1, current_token, &info); + } + } } static void client_serverbrowse_request(SERVERENTRY *entry) { + unsigned char buffer[sizeof(SERVERBROWSE_GETINFO)+1]; NETCHUNK p; if(config.debug) @@ -418,11 +483,14 @@ static void client_serverbrowse_request(SERVERENTRY *entry) entry->addr.ip[3], entry->addr.port); } + mem_copy(buffer, SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)); + buffer[sizeof(SERVERBROWSE_GETINFO)] = current_token; + p.client_id = -1; p.address = entry->addr; p.flags = NETSENDFLAG_CONNLESS; - p.data_size = sizeof(SERVERBROWSE_GETINFO); - p.data = SERVERBROWSE_GETINFO; + p.data_size = sizeof(buffer); + p.data = buffer; netclient_send(net, &p); entry->request_time = time_get(); } @@ -477,3 +545,47 @@ void client_serverbrowse_update() if(sorthash != client_serverbrowse_sorthash() || strcmp(filterstring, config.b_filter_string) != 0 || strcmp(filtergametypestring, config.b_filter_gametype) != 0) client_serverbrowse_sort(); } + + +void client_serverbrowse_addfavorite(NETADDR addr) +{ + int i; + SERVERENTRY *entry; + + if(num_favorite_servers == MAX_FAVORITES) + return; + + /* make sure that we don't already have the server in our list */ + for(i = 0; i < num_favorite_servers; i++) + { + if(net_addr_comp(&addr, &favorite_servers[i]) == 0) + return; + } + + /* add the server to the list */ + favorite_servers[num_favorite_servers++] = addr; + entry = client_serverbrowse_find(&addr); + if(entry) + entry->info.favorite = 1; +} + +void client_serverbrowse_removefavorite(NETADDR addr) +{ + int i; + SERVERENTRY *entry; + + for(i = 0; i < num_favorite_servers; i++) + { + if(net_addr_comp(&addr, &favorite_servers[i]) == 0) + { + mem_move(&favorite_servers[i], &favorite_servers[i+1], num_favorite_servers-(i+1)); + num_favorite_servers--; + + entry = client_serverbrowse_find(&addr); + if(entry) + entry->info.favorite = 0; + + return; + } + } +} |