celestar@5642: /* $Id$ */ celestar@5642: celestar@5642: #ifdef ENABLE_NETWORK celestar@5642: #include "../stdafx.h" celestar@5642: #include "../openttd.h" celestar@5642: #include "../string.h" celestar@5642: #include "../strings.h" celestar@5642: #include "../table/sprites.h" celestar@5642: #include "network.h" celestar@5642: #include "../date.h" celestar@5642: celestar@5642: #include "../fios.h" celestar@5642: #include "../table/strings.h" celestar@5642: #include "../functions.h" celestar@5642: #include "network_data.h" celestar@5642: #include "network_client.h" celestar@5642: #include "network_gui.h" celestar@5642: #include "network_gamelist.h" celestar@5642: #include "../window.h" celestar@5642: #include "../gui.h" celestar@5642: #include "../gfx.h" celestar@5642: #include "../command.h" celestar@5642: #include "../variables.h" celestar@5642: #include "network_server.h" celestar@5642: #include "network_udp.h" celestar@5642: #include "../settings.h" celestar@5642: #include "../string.h" celestar@5642: #include "../town.h" celestar@5642: #include "../newgrf.h" celestar@5642: celestar@5642: #define BGC 5 celestar@5642: #define BTC 15 celestar@5642: celestar@5642: typedef struct network_d { celestar@5642: PlayerID company; // select company in network lobby celestar@5642: byte field; // select text-field in start-server and game-listing celestar@5642: NetworkGameList *server; // selected server in lobby and game-listing celestar@5642: FiosItem *map; // selected map in start-server celestar@5642: } network_d; celestar@5642: assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(network_d)); celestar@5642: celestar@5642: typedef struct network_ql_d { celestar@5642: network_d n; // see above; general stuff celestar@5642: querystr_d q; // text-input in start-server and game-listing celestar@5642: NetworkGameList **sort_list; // list of games (sorted) celestar@5642: list_d l; // accompanying list-administration celestar@5642: } network_ql_d; celestar@5642: assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(network_ql_d)); celestar@5642: celestar@5642: /* Global to remember sorting after window has been closed */ celestar@5642: static Listing _ng_sorting; celestar@5642: celestar@5642: static char _edit_str_buf[150]; celestar@5642: static bool _chat_tab_completion_active; celestar@5642: celestar@5642: static void ShowNetworkStartServerWindow(void); celestar@5642: static void ShowNetworkLobbyWindow(NetworkGameList *ngl); celestar@5642: extern void SwitchMode(int new_mode); celestar@5642: celestar@5642: static const StringID _connection_types_dropdown[] = { celestar@5642: STR_NETWORK_LAN_INTERNET, celestar@5642: STR_NETWORK_INTERNET_ADVERTISE, celestar@5642: INVALID_STRING_ID celestar@5642: }; celestar@5642: celestar@5642: static const StringID _lan_internet_types_dropdown[] = { celestar@5642: STR_NETWORK_LAN, celestar@5642: STR_NETWORK_INTERNET, celestar@5642: INVALID_STRING_ID celestar@5642: }; celestar@5642: celestar@5642: static const StringID _players_dropdown[] = { celestar@5642: STR_NETWORK_0_PLAYERS, celestar@5642: STR_NETWORK_1_PLAYERS, celestar@5642: STR_NETWORK_2_PLAYERS, celestar@5642: STR_NETWORK_3_PLAYERS, celestar@5642: STR_NETWORK_4_PLAYERS, celestar@5642: STR_NETWORK_5_PLAYERS, celestar@5642: STR_NETWORK_6_PLAYERS, celestar@5642: STR_NETWORK_7_PLAYERS, celestar@5642: STR_NETWORK_8_PLAYERS, celestar@5642: STR_NETWORK_9_PLAYERS, celestar@5642: STR_NETWORK_10_PLAYERS, celestar@5642: INVALID_STRING_ID celestar@5642: }; celestar@5642: celestar@5642: static const StringID _language_dropdown[] = { celestar@5642: STR_NETWORK_LANG_ANY, celestar@5642: STR_NETWORK_LANG_ENGLISH, celestar@5642: STR_NETWORK_LANG_GERMAN, celestar@5642: STR_NETWORK_LANG_FRENCH, celestar@5642: INVALID_STRING_ID celestar@5642: }; celestar@5642: celestar@5642: enum { celestar@5642: NET_PRC__OFFSET_TOP_WIDGET = 54, celestar@5642: NET_PRC__OFFSET_TOP_WIDGET_COMPANY = 52, celestar@5642: NET_PRC__SIZE_OF_ROW = 14, celestar@5642: }; celestar@5642: celestar@5642: /** Update the network new window because a new server is celestar@5642: * found on the network. celestar@5642: * @param unselect unselect the currently selected item */ celestar@5642: void UpdateNetworkGameWindow(bool unselect) celestar@5642: { celestar@5642: SendWindowMessage(WC_NETWORK_WINDOW, 0, unselect, 0, 0); celestar@5642: } celestar@5642: celestar@5642: static bool _internal_sort_order; // Used for Qsort order-flipping celestar@5642: typedef int CDECL NGameNameSortFunction(const void*, const void*); celestar@5642: celestar@5642: /** Qsort function to sort by name. */ celestar@5642: static int CDECL NGameNameSorter(const void *a, const void *b) celestar@5642: { celestar@5642: const NetworkGameList *cmp1 = *(const NetworkGameList**)a; celestar@5642: const NetworkGameList *cmp2 = *(const NetworkGameList**)b; celestar@5642: int r = strcasecmp(cmp1->info.server_name, cmp2->info.server_name); celestar@5642: celestar@5642: return _internal_sort_order ? -r : r; celestar@5642: } celestar@5642: celestar@5642: /** Qsort function to sort by the amount of clients online on a celestar@5642: * server. If the two servers have the same amount, the one with the celestar@5642: * higher maximum is preferred. */ celestar@5642: static int CDECL NGameClientSorter(const void *a, const void *b) celestar@5642: { celestar@5642: const NetworkGameList *cmp1 = *(const NetworkGameList**)a; celestar@5642: const NetworkGameList *cmp2 = *(const NetworkGameList**)b; celestar@5642: /* Reverse as per default we are interested in most-clients first */ celestar@5642: int r = cmp1->info.clients_on - cmp2->info.clients_on; celestar@5642: celestar@5642: if (r == 0) r = cmp1->info.clients_max - cmp2->info.clients_max; celestar@5642: if (r == 0) r = strcasecmp(cmp1->info.server_name, cmp2->info.server_name); celestar@5642: celestar@5642: return _internal_sort_order ? -r : r; celestar@5642: } celestar@5642: celestar@5642: /** Qsort function to sort by joinability. If both servers are the celestar@5642: * same, prefer the non-passworded server first. */ celestar@5642: static int CDECL NGameAllowedSorter(const void *a, const void *b) celestar@5642: { celestar@5642: const NetworkGameList *cmp1 = *(const NetworkGameList**)a; celestar@5642: const NetworkGameList *cmp2 = *(const NetworkGameList**)b; celestar@5642: /* Reverse default as we are interested in compatible clients first */ celestar@5642: int r = cmp2->info.compatible - cmp1->info.compatible; celestar@5642: celestar@5642: if (r == 0) r = cmp1->info.use_password - cmp2->info.use_password; celestar@5642: if (r == 0) r = strcasecmp(cmp1->info.server_name, cmp2->info.server_name); celestar@5642: celestar@5642: return _internal_sort_order ? -r : r; celestar@5642: } celestar@5642: celestar@5642: /** (Re)build the network game list as its amount has changed because celestar@5642: * an item has been added or deleted for example celestar@5642: * @param ngl list_d struct that contains all necessary information for sorting */ celestar@5642: static void BuildNetworkGameList(network_ql_d *nqld) celestar@5642: { celestar@5642: NetworkGameList *ngl_temp; celestar@5642: uint n = 0; celestar@5642: celestar@5642: if (!(nqld->l.flags & VL_REBUILD)) return; celestar@5642: celestar@5642: /* Count the number of games in the list */ celestar@5642: for (ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) n++; celestar@5642: if (n == 0) return; celestar@5642: celestar@5642: /* Create temporary array of games to use for listing */ celestar@5642: free(nqld->sort_list); celestar@5642: nqld->sort_list = malloc(n * sizeof(nqld->sort_list[0])); celestar@5642: if (nqld->sort_list == NULL) error("Could not allocate memory for the network-game-sorting-list"); celestar@5642: nqld->l.list_length = n; celestar@5642: celestar@5642: for (n = 0, ngl_temp = _network_game_list; ngl_temp != NULL; ngl_temp = ngl_temp->next) { celestar@5642: nqld->sort_list[n++] = ngl_temp; celestar@5642: } celestar@5642: celestar@5642: /* Force resort */ celestar@5642: nqld->l.flags &= ~VL_REBUILD; celestar@5642: nqld->l.flags |= VL_RESORT; celestar@5642: } celestar@5642: celestar@5642: static void SortNetworkGameList(network_ql_d *nqld) celestar@5642: { celestar@5642: static NGameNameSortFunction * const ngame_sorter[] = { celestar@5642: &NGameNameSorter, celestar@5642: &NGameClientSorter, celestar@5642: &NGameAllowedSorter celestar@5642: }; celestar@5642: celestar@5642: NetworkGameList *item; celestar@5642: uint i; celestar@5642: celestar@5642: if (!(nqld->l.flags & VL_RESORT)) return; celestar@5642: if (nqld->l.list_length == 0) return; celestar@5642: celestar@5642: _internal_sort_order = !!(nqld->l.flags & VL_DESC); celestar@5642: qsort(nqld->sort_list, nqld->l.list_length, sizeof(nqld->sort_list[0]), ngame_sorter[nqld->l.sort_type]); celestar@5642: celestar@5642: /* After sorting ngl->sort_list contains the sorted items. Put these back celestar@5642: * into the original list. Basically nothing has changed, we are only celestar@5642: * shuffling the ->next pointers */ celestar@5642: _network_game_list = nqld->sort_list[0]; celestar@5642: for (item = _network_game_list, i = 1; i != nqld->l.list_length; i++) { celestar@5642: item->next = nqld->sort_list[i]; celestar@5642: item = item->next; celestar@5642: } celestar@5642: item->next = NULL; celestar@5642: celestar@5642: nqld->l.flags &= ~VL_RESORT; celestar@5642: } celestar@5642: celestar@5642: /* Uses network_ql_d (network_d, querystr_d and list_d) WP macro */ celestar@5642: static void NetworkGameWindowWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: network_d *nd = &WP(w, network_ql_d).n; celestar@5642: list_d *ld = &WP(w, network_ql_d).l; celestar@5642: celestar@5642: switch (e->event) { celestar@5642: case WE_CREATE: /* Focus input box */ celestar@5642: nd->field = 3; celestar@5642: nd->server = NULL; celestar@5642: celestar@5642: WP(w, network_ql_d).sort_list = NULL; celestar@5642: ld->flags = VL_REBUILD | (_ng_sorting.order << (VL_DESC - 1)); celestar@5642: ld->sort_type = _ng_sorting.criteria; celestar@5642: break; celestar@5642: celestar@5642: case WE_PAINT: { celestar@5642: const NetworkGameList *sel = nd->server; celestar@5642: const char *arrow = (ld->flags & VL_DESC) ? DOWNARROW : UPARROW; celestar@5642: celestar@5642: if (ld->flags & VL_REBUILD) { celestar@5642: BuildNetworkGameList(&WP(w, network_ql_d)); celestar@5642: SetVScrollCount(w, ld->list_length); celestar@5642: } celestar@5642: if (ld->flags & VL_RESORT) SortNetworkGameList(&WP(w, network_ql_d)); celestar@5642: celestar@5642: SetWindowWidgetDisabledState(w, 17, sel == NULL); celestar@5642: /* Join Button disabling conditions */ celestar@5642: SetWindowWidgetDisabledState(w, 16, sel == NULL || // no Selected Server celestar@5642: !sel->online || // Server offline celestar@5642: sel->info.clients_on >= sel->info.clients_max || // Server full celestar@5642: !sel->info.compatible); // Revision mismatch celestar@5642: celestar@5642: SetWindowWidgetHiddenState(w, 18, sel == NULL || celestar@5642: !sel->online || celestar@5642: sel->info.grfconfig == NULL); celestar@5642: celestar@5642: SetDParam(0, 0x00); celestar@5642: SetDParam(7, _lan_internet_types_dropdown[_network_lan_internet]); celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: DrawEditBox(w, &WP(w, network_ql_d).q, 3); celestar@5642: celestar@5642: DrawString(9, 23, STR_NETWORK_CONNECTION, 2); celestar@5642: DrawString(210, 23, STR_NETWORK_PLAYER_NAME, 2); celestar@5642: celestar@5642: /* Sort based on widgets: name, clients, compatibility */ celestar@5642: switch (ld->sort_type) { celestar@5642: case 6 - 6: DoDrawString(arrow, w->widget[6].right - 10, 42, 0x10); break; celestar@5642: case 7 - 6: DoDrawString(arrow, w->widget[7].right - 10, 42, 0x10); break; celestar@5642: case 8 - 6: DoDrawString(arrow, w->widget[8].right - 10, 42, 0x10); break; celestar@5642: } celestar@5642: celestar@5642: { // draw list of games celestar@5642: uint16 y = NET_PRC__OFFSET_TOP_WIDGET + 3; celestar@5642: int32 n = 0; celestar@5642: int32 pos = w->vscroll.pos; celestar@5642: uint max_name_width = w->widget[6].right - w->widget[6].left - 5; celestar@5642: const NetworkGameList *cur_item = _network_game_list; celestar@5642: celestar@5642: while (pos > 0 && cur_item != NULL) { celestar@5642: pos--; celestar@5642: cur_item = cur_item->next; celestar@5642: } celestar@5642: celestar@5642: while (cur_item != NULL) { celestar@5642: // show highlighted item with a different colour celestar@5642: if (cur_item == sel) GfxFillRect(w->widget[6].left + 1, y - 2, w->widget[8].right - 1, y + 9, 10); celestar@5642: celestar@5642: SetDParamStr(0, cur_item->info.server_name); celestar@5642: DrawStringTruncated(w->widget[6].left + 5, y, STR_02BD, 16, max_name_width); celestar@5642: celestar@5642: SetDParam(0, cur_item->info.clients_on); celestar@5642: SetDParam(1, cur_item->info.clients_max); celestar@5642: SetDParam(2, cur_item->info.companies_on); celestar@5642: SetDParam(3, cur_item->info.companies_max); celestar@5642: DrawStringCentered(210, y, STR_NETWORK_GENERAL_ONLINE, 2); celestar@5642: celestar@5642: // only draw icons if the server is online celestar@5642: if (cur_item->online) { celestar@5642: // draw a lock if the server is password protected. celestar@5642: if (cur_item->info.use_password) DrawSprite(SPR_LOCK, w->widget[8].left + 5, y - 1); celestar@5642: celestar@5642: // draw red or green icon, depending on compatibility with server. celestar@5642: DrawSprite(SPR_BLOT | (cur_item->info.compatible ? PALETTE_TO_GREEN : (cur_item->info.version_compatible ? PALETTE_TO_YELLOW : PALETTE_TO_RED)), w->widget[8].left + 15, y); celestar@5642: celestar@5642: // draw flag according to server language celestar@5642: DrawSprite(SPR_FLAGS_BASE + cur_item->info.server_lang, w->widget[8].left + 25, y); celestar@5642: } celestar@5642: celestar@5642: cur_item = cur_item->next; celestar@5642: y += NET_PRC__SIZE_OF_ROW; celestar@5642: if (++n == w->vscroll.cap) break; // max number of games in the window celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: /* Draw the right menu */ celestar@5642: GfxFillRect(311, 43, 539, 92, 157); celestar@5642: if (sel == NULL) { celestar@5642: DrawStringCentered(425, 58, STR_NETWORK_GAME_INFO, 0); celestar@5642: } else if (!sel->online) { celestar@5642: SetDParamStr(0, sel->info.server_name); celestar@5642: DrawStringCentered(425, 68, STR_ORANGE, 0); // game name celestar@5642: celestar@5642: DrawStringCentered(425, 132, STR_NETWORK_SERVER_OFFLINE, 0); // server offline celestar@5642: } else { // show game info celestar@5642: uint16 y = 100; celestar@5642: const uint16 x = w->widget[15].left + 5; celestar@5642: celestar@5642: DrawStringCentered(425, 48, STR_NETWORK_GAME_INFO, 0); celestar@5642: celestar@5642: celestar@5642: SetDParamStr(0, sel->info.server_name); celestar@5642: DrawStringCenteredTruncated(w->widget[15].left, w->widget[15].right, 62, STR_ORANGE, 16); // game name celestar@5642: celestar@5642: SetDParamStr(0, sel->info.map_name); celestar@5642: DrawStringCenteredTruncated(w->widget[15].left, w->widget[15].right, 74, STR_02BD, 16); // map name celestar@5642: celestar@5642: SetDParam(0, sel->info.clients_on); celestar@5642: SetDParam(1, sel->info.clients_max); celestar@5642: SetDParam(2, sel->info.companies_on); celestar@5642: SetDParam(3, sel->info.companies_max); celestar@5642: DrawString(x, y, STR_NETWORK_CLIENTS, 2); celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, _language_dropdown[sel->info.server_lang]); celestar@5642: DrawString(x, y, STR_NETWORK_LANGUAGE, 2); // server language celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, STR_TEMPERATE_LANDSCAPE + sel->info.map_set); celestar@5642: DrawString(x, y, STR_NETWORK_TILESET, 2); // tileset celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, sel->info.map_width); celestar@5642: SetDParam(1, sel->info.map_height); celestar@5642: DrawString(x, y, STR_NETWORK_MAP_SIZE, 2); // map size celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParamStr(0, sel->info.server_revision); celestar@5642: DrawString(x, y, STR_NETWORK_SERVER_VERSION, 2); // server version celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParamStr(0, sel->info.hostname); celestar@5642: SetDParam(1, sel->port); celestar@5642: DrawString(x, y, STR_NETWORK_SERVER_ADDRESS, 2); // server address celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, sel->info.start_date); celestar@5642: DrawString(x, y, STR_NETWORK_START_DATE, 2); // start date celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, sel->info.game_date); celestar@5642: DrawString(x, y, STR_NETWORK_CURRENT_DATE, 2); // current date celestar@5642: y += 10; celestar@5642: celestar@5642: y += 2; celestar@5642: celestar@5642: if (!sel->info.compatible) { celestar@5642: DrawStringCentered(425, y, sel->info.version_compatible ? STR_NETWORK_GRF_MISMATCH : STR_NETWORK_VERSION_MISMATCH, 0); // server mismatch celestar@5642: } else if (sel->info.clients_on == sel->info.clients_max) { celestar@5642: // Show: server full, when clients_on == clients_max celestar@5642: DrawStringCentered(425, y, STR_NETWORK_SERVER_FULL, 0); // server full celestar@5642: } else if (sel->info.use_password) { celestar@5642: DrawStringCentered(425, y, STR_NETWORK_PASSWORD, 0); // password warning celestar@5642: } celestar@5642: celestar@5642: y += 10; celestar@5642: } celestar@5642: } break; celestar@5642: celestar@5642: case WE_CLICK: celestar@5642: nd->field = e->we.click.widget; celestar@5642: switch (e->we.click.widget) { celestar@5642: case 0: case 14: /* Close 'X' | Cancel button */ celestar@5642: DeleteWindowById(WC_NETWORK_WINDOW, 0); celestar@5642: break; celestar@5642: case 4: case 5: celestar@5642: ShowDropDownMenu(w, _lan_internet_types_dropdown, _network_lan_internet, 5, 0, 0); // do it for widget 5 celestar@5642: break; celestar@5642: case 6: /* Sort by name */ celestar@5642: case 7: /* Sort by connected clients */ celestar@5642: case 8: /* Connectivity (green dot) */ celestar@5642: if (ld->sort_type == e->we.click.widget - 6) ld->flags ^= VL_DESC; celestar@5642: ld->flags |= VL_RESORT; celestar@5642: ld->sort_type = e->we.click.widget - 6; celestar@5642: celestar@5642: _ng_sorting.order = !!(ld->flags & VL_DESC); celestar@5642: _ng_sorting.criteria = ld->sort_type; celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: case 9: { /* Matrix to show networkgames */ celestar@5642: NetworkGameList *cur_item; celestar@5642: uint32 id_v = (e->we.click.pt.y - NET_PRC__OFFSET_TOP_WIDGET) / NET_PRC__SIZE_OF_ROW; celestar@5642: celestar@5642: if (id_v >= w->vscroll.cap) return; // click out of bounds celestar@5642: id_v += w->vscroll.pos; celestar@5642: celestar@5642: cur_item = _network_game_list; celestar@5642: for (; id_v > 0 && cur_item != NULL; id_v--) cur_item = cur_item->next; celestar@5642: celestar@5642: nd->server = cur_item; celestar@5642: SetWindowDirty(w); celestar@5642: } break; celestar@5642: case 11: /* Find server automatically */ celestar@5642: switch (_network_lan_internet) { celestar@5642: case 0: NetworkUDPSearchGame(); break; celestar@5642: case 1: NetworkUDPQueryMasterServer(); break; celestar@5642: } celestar@5642: break; celestar@5642: case 12: { // Add a server celestar@5642: ShowQueryString( celestar@5642: BindCString(_network_default_ip), celestar@5642: STR_NETWORK_ENTER_IP, celestar@5642: 31 | 0x1000, // maximum number of characters OR celestar@5642: 250, // characters up to this width pixels, whichever is satisfied first celestar@5642: w, CS_ALPHANUMERAL); celestar@5642: } break; celestar@5642: case 13: /* Start server */ celestar@5642: ShowNetworkStartServerWindow(); celestar@5642: break; celestar@5642: case 16: /* Join Game */ celestar@5642: if (nd->server != NULL) { celestar@5642: snprintf(_network_last_host, sizeof(_network_last_host), "%s", inet_ntoa(*(struct in_addr *)&nd->server->ip)); celestar@5642: _network_last_port = nd->server->port; celestar@5642: ShowNetworkLobbyWindow(nd->server); celestar@5642: } celestar@5642: break; celestar@5642: case 17: // Refresh celestar@5642: if (nd->server != NULL) celestar@5642: NetworkQueryServer(nd->server->info.hostname, nd->server->port, true); celestar@5642: break; celestar@5642: case 18: // NewGRF Settings celestar@5642: if (nd->server != NULL) ShowNewGRFSettings(false, false, false, &nd->server->info.grfconfig); celestar@5642: break; celestar@5642: celestar@5642: } break; celestar@5642: celestar@5642: case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ celestar@5642: switch (e->we.dropdown.button) { celestar@5642: case 5: celestar@5642: _network_lan_internet = e->we.dropdown.index; celestar@5642: break; celestar@5642: } celestar@5642: celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: celestar@5642: case WE_MOUSELOOP: celestar@5642: if (nd->field == 3) HandleEditBox(w, &WP(w, network_ql_d).q, 3); celestar@5642: break; celestar@5642: celestar@5642: case WE_MESSAGE: celestar@5642: if (e->we.message.msg != 0) nd->server = NULL; celestar@5642: ld->flags |= VL_REBUILD; celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: celestar@5642: case WE_KEYPRESS: celestar@5642: if (nd->field != 3) { celestar@5642: if (nd->server != NULL) { celestar@5642: if (e->we.keypress.keycode == WKC_DELETE) { /* Press 'delete' to remove servers */ celestar@5642: NetworkGameListRemoveItem(nd->server); celestar@5642: NetworkRebuildHostList(); celestar@5642: nd->server = NULL; celestar@5642: } celestar@5642: } celestar@5642: break; celestar@5642: } celestar@5642: celestar@5642: if (HandleEditBoxKey(w, &WP(w, network_ql_d).q, 3, e) == 1) break; // enter pressed celestar@5642: celestar@5642: // The name is only allowed when it starts with a letter! celestar@5642: if (_edit_str_buf[0] != '\0' && _edit_str_buf[0] != ' ') { celestar@5642: ttd_strlcpy(_network_player_name, _edit_str_buf, lengthof(_network_player_name)); celestar@5642: } else { celestar@5642: ttd_strlcpy(_network_player_name, "Player", lengthof(_network_player_name)); celestar@5642: } celestar@5642: celestar@5642: break; celestar@5642: celestar@5642: case WE_ON_EDIT_TEXT: celestar@5642: NetworkAddServer(e->we.edittext.str); celestar@5642: NetworkRebuildHostList(); celestar@5642: break; celestar@5642: celestar@5642: case WE_DESTROY: /* Nicely clean up the sort-list */ celestar@5642: free(WP(w, network_ql_d).sort_list); celestar@5642: break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: static const Widget _network_game_window_widgets[] = { celestar@5642: { WWT_CLOSEBOX, RESIZE_NONE, BGC, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, celestar@5642: { WWT_CAPTION, RESIZE_NONE, BGC, 11, 549, 0, 13, STR_NETWORK_MULTIPLAYER, STR_NULL}, celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 0, 549, 14, 263, 0x0, STR_NULL}, celestar@5642: celestar@5642: /* LEFT SIDE */ celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 310, 461, 22, 33, 0x0, STR_NETWORK_ENTER_NAME_TIP}, celestar@5642: celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 90, 181, 22, 33, STR_NETWORK_COMBO1, STR_NETWORK_CONNECTION_TIP}, celestar@5642: { WWT_TEXTBTN, RESIZE_NONE, BGC, 170, 180, 23, 32, STR_0225, STR_NETWORK_CONNECTION_TIP}, celestar@5642: celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 10, 170, 42, 53, STR_NETWORK_GAME_NAME, STR_NETWORK_GAME_NAME_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 171, 250, 42, 53, STR_NETWORK_CLIENTS_CAPTION, STR_NETWORK_CLIENTS_CAPTION_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 251, 290, 42, 53, STR_EMPTY, STR_NETWORK_INFO_ICONS_TIP}, celestar@5642: celestar@5642: { WWT_MATRIX, RESIZE_NONE, BGC, 10, 290, 54, 236, (13 << 8) + 1, STR_NETWORK_CLICK_GAME_TO_SELECT}, celestar@5642: { WWT_SCROLLBAR, RESIZE_NONE, BGC, 291, 302, 42, 236, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, celestar@5642: celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 30, 130, 246, 257, STR_NETWORK_FIND_SERVER, STR_NETWORK_FIND_SERVER_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 180, 280, 246, 257, STR_NETWORK_ADD_SERVER, STR_NETWORK_ADD_SERVER_TIP}, celestar@5642: celestar@5642: /* RIGHT SIDE */ celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 315, 415, 246, 257, STR_NETWORK_START_SERVER, STR_NETWORK_START_SERVER_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 430, 535, 246, 257, STR_012E_CANCEL, STR_NULL}, celestar@5642: celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 310, 540, 42, 236, 0x0, STR_NULL}, celestar@5642: celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 315, 415, 215, 226, STR_NETWORK_JOIN_GAME, STR_NULL}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 430, 535, 215, 226, STR_NETWORK_REFRESH, STR_NETWORK_REFRESH_TIP}, celestar@5642: celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 430, 535, 197, 208, STR_NEWGRF_SETTINGS_BUTTON, STR_NULL}, celestar@5642: celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static const WindowDesc _network_game_window_desc = { celestar@5642: WDP_CENTER, WDP_CENTER, 550, 264, celestar@5642: WC_NETWORK_WINDOW,0, celestar@5642: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, celestar@5642: _network_game_window_widgets, celestar@5642: NetworkGameWindowWndProc, celestar@5642: }; celestar@5642: celestar@5642: void ShowNetworkGameWindow(void) celestar@5642: { celestar@5642: static bool first = true; celestar@5642: Window *w; celestar@5642: DeleteWindowById(WC_NETWORK_WINDOW, 0); celestar@5642: celestar@5642: /* Only show once */ celestar@5642: if (first) { celestar@5642: char* const *srv; celestar@5642: celestar@5642: first = false; celestar@5642: // add all servers from the config file to our list celestar@5642: for (srv = &_network_host_list[0]; srv != endof(_network_host_list) && *srv != NULL; srv++) { celestar@5642: NetworkAddServer(*srv); celestar@5642: } celestar@5642: celestar@5642: _ng_sorting.criteria = 2; // sort default by collectivity (green-dots on top) celestar@5642: _ng_sorting.order = 0; // sort ascending by default celestar@5642: } celestar@5642: celestar@5642: w = AllocateWindowDesc(&_network_game_window_desc); celestar@5642: if (w != NULL) { celestar@5642: querystr_d *querystr = &WP(w, network_ql_d).q; celestar@5642: celestar@5642: ttd_strlcpy(_edit_str_buf, _network_player_name, lengthof(_edit_str_buf)); celestar@5642: w->vscroll.cap = 13; celestar@5642: celestar@5642: querystr->afilter = CS_ALPHANUMERAL; celestar@5642: InitializeTextBuffer(&querystr->text, _edit_str_buf, lengthof(_edit_str_buf), 120); celestar@5642: celestar@5642: UpdateNetworkGameWindow(true); celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: enum { celestar@5642: NSSWND_START = 64, celestar@5642: NSSWND_ROWSIZE = 12 celestar@5642: }; celestar@5642: celestar@5642: /* Uses network_ql_d (network_d, querystr_d and list_d) WP macro */ celestar@5642: static void NetworkStartServerWindowWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: network_d *nd = &WP(w, network_ql_d).n; celestar@5642: celestar@5642: switch (e->event) { celestar@5642: case WE_CREATE: /* focus input box */ celestar@5642: nd->field = 3; celestar@5642: _network_game_info.use_password = (_network_server_password[0] != '\0'); celestar@5642: break; celestar@5642: celestar@5642: case WE_PAINT: { celestar@5642: int y = NSSWND_START, pos; celestar@5642: const FiosItem *item; celestar@5642: celestar@5642: SetDParam( 7, _connection_types_dropdown[_network_advertise]); celestar@5642: SetDParam( 9, _players_dropdown[_network_game_info.clients_max]); celestar@5642: SetDParam(11, _players_dropdown[_network_game_info.companies_max]); celestar@5642: SetDParam(13, _players_dropdown[_network_game_info.spectators_max]); celestar@5642: SetDParam(15, _language_dropdown[_network_game_info.server_lang]); celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: GfxFillRect(11, 63, 258, 215, 0xD7); celestar@5642: DrawEditBox(w, &WP(w, network_ql_d).q, 3); celestar@5642: celestar@5642: DrawString(10, 22, STR_NETWORK_NEW_GAME_NAME, 2); celestar@5642: celestar@5642: DrawString(10, 43, STR_NETWORK_SELECT_MAP, 2); celestar@5642: celestar@5642: DrawString(280, 63, STR_NETWORK_CONNECTION, 2); celestar@5642: DrawString(280, 95, STR_NETWORK_NUMBER_OF_CLIENTS, 2); celestar@5642: DrawString(280, 127, STR_NETWORK_NUMBER_OF_COMPANIES, 2); celestar@5642: DrawString(280, 159, STR_NETWORK_NUMBER_OF_SPECTATORS, 2); celestar@5642: DrawString(280, 191, STR_NETWORK_LANGUAGE_SPOKEN, 2); celestar@5642: celestar@5642: if (_network_game_info.use_password) DoDrawString("*", 408, 23, 3); celestar@5642: celestar@5642: // draw list of maps celestar@5642: pos = w->vscroll.pos; celestar@5642: while (pos < _fios_num + 1) { celestar@5642: item = _fios_list + pos - 1; celestar@5642: if (item == nd->map || (pos == 0 && nd->map == NULL)) celestar@5642: GfxFillRect(11, y - 1, 258, y + 10, 155); // show highlighted item with a different colour celestar@5642: celestar@5642: if (pos == 0) { celestar@5642: DrawString(14, y, STR_4010_GENERATE_RANDOM_NEW_GAME, 9); celestar@5642: } else { celestar@5642: DoDrawString(item->title, 14, y, _fios_colors[item->type] ); celestar@5642: } celestar@5642: pos++; celestar@5642: y += NSSWND_ROWSIZE; celestar@5642: celestar@5642: if (y >= w->vscroll.cap * NSSWND_ROWSIZE + NSSWND_START) break; celestar@5642: } celestar@5642: } break; celestar@5642: celestar@5642: case WE_CLICK: celestar@5642: nd->field = e->we.click.widget; celestar@5642: switch (e->we.click.widget) { celestar@5642: case 0: /* Close 'X' */ celestar@5642: case 19: /* Cancel button */ celestar@5642: ShowNetworkGameWindow(); celestar@5642: break; celestar@5642: celestar@5642: case 4: /* Set password button */ celestar@5642: ShowQueryString(BindCString(_network_server_password), STR_NETWORK_SET_PASSWORD, 20, 250, w, CS_ALPHANUMERAL); celestar@5642: break; celestar@5642: celestar@5642: case 5: { /* Select map */ celestar@5642: int y = (e->we.click.pt.y - NSSWND_START) / NSSWND_ROWSIZE; celestar@5642: celestar@5642: y += w->vscroll.pos; celestar@5642: if (y >= w->vscroll.count) return; celestar@5642: celestar@5642: nd->map = (y == 0) ? NULL : _fios_list + y - 1; celestar@5642: SetWindowDirty(w); celestar@5642: } break; celestar@5642: case 7: case 8: /* Connection type */ celestar@5642: ShowDropDownMenu(w, _connection_types_dropdown, _network_advertise, 8, 0, 0); // do it for widget 8 celestar@5642: break; celestar@5642: case 9: case 10: /* Number of Players (hide 0 and 1 players) */ celestar@5642: ShowDropDownMenu(w, _players_dropdown, _network_game_info.clients_max, 10, 0, 3); celestar@5642: break; celestar@5642: case 11: case 12: /* Number of Companies (hide 0, 9 and 10 companies; max is 8) */ celestar@5642: ShowDropDownMenu(w, _players_dropdown, _network_game_info.companies_max, 12, 0, 1537); celestar@5642: break; celestar@5642: case 13: case 14: /* Number of Spectators */ celestar@5642: ShowDropDownMenu(w, _players_dropdown, _network_game_info.spectators_max, 14, 0, 0); celestar@5642: break; celestar@5642: case 15: case 16: /* Language */ celestar@5642: ShowDropDownMenu(w, _language_dropdown, _network_game_info.server_lang, 16, 0, 0); celestar@5642: break; celestar@5642: case 17: /* Start game */ celestar@5642: _is_network_server = true; celestar@5642: celestar@5642: if (nd->map == NULL) { // start random new game celestar@5642: ShowGenerateLandscape(); celestar@5642: } else { // load a scenario celestar@5642: char *name = FiosBrowseTo(nd->map); celestar@5642: if (name != NULL) { celestar@5642: SetFiosType(nd->map->type); celestar@5642: ttd_strlcpy(_file_to_saveload.name, name, sizeof(_file_to_saveload.name)); celestar@5642: ttd_strlcpy(_file_to_saveload.title, nd->map->title, sizeof(_file_to_saveload.title)); celestar@5642: celestar@5642: DeleteWindow(w); celestar@5642: SwitchMode(SM_START_SCENARIO); celestar@5642: } celestar@5642: } celestar@5642: break; celestar@5642: case 18: /* Load game */ celestar@5642: _is_network_server = true; celestar@5642: /* XXX - WC_NETWORK_WINDOW should stay, but if it stays, it gets celestar@5642: * copied all the elements of 'load game' and upon closing that, it segfaults */ celestar@5642: DeleteWindowById(WC_NETWORK_WINDOW, 0); celestar@5642: ShowSaveLoadDialog(SLD_LOAD_GAME); celestar@5642: break; celestar@5642: } celestar@5642: break; celestar@5642: celestar@5642: case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ celestar@5642: switch (e->we.dropdown.button) { celestar@5642: case 8: _network_advertise = (e->we.dropdown.index != 0); break; celestar@5642: case 10: _network_game_info.clients_max = e->we.dropdown.index; break; celestar@5642: case 12: _network_game_info.companies_max = e->we.dropdown.index; break; celestar@5642: case 14: _network_game_info.spectators_max = e->we.dropdown.index; break; celestar@5642: case 16: _network_game_info.server_lang = e->we.dropdown.index; break; celestar@5642: } celestar@5642: celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: celestar@5642: case WE_MOUSELOOP: celestar@5642: if (nd->field == 3) HandleEditBox(w, &WP(w, network_ql_d).q, 3); celestar@5642: break; celestar@5642: celestar@5642: case WE_KEYPRESS: celestar@5642: if (nd->field == 3) { celestar@5642: if (HandleEditBoxKey(w, &WP(w, network_ql_d).q, 3, e) == 1) break; // enter pressed celestar@5642: celestar@5642: ttd_strlcpy(_network_server_name, WP(w, network_ql_d).q.text.buf, sizeof(_network_server_name)); celestar@5642: UpdateTextBufferSize(&WP(w, network_ql_d).q.text); celestar@5642: } celestar@5642: break; celestar@5642: celestar@5642: case WE_ON_EDIT_TEXT: { celestar@5642: ttd_strlcpy(_network_server_password, e->we.edittext.str, lengthof(_network_server_password)); celestar@5642: _network_game_info.use_password = (_network_server_password[0] != '\0'); celestar@5642: SetWindowDirty(w); celestar@5642: } break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: static const Widget _network_start_server_window_widgets[] = { celestar@5642: { WWT_CLOSEBOX, RESIZE_NONE, BGC, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, celestar@5642: { WWT_CAPTION, RESIZE_NONE, BGC, 11, 419, 0, 13, STR_NETWORK_START_GAME_WINDOW, STR_NULL}, celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 0, 419, 14, 243, 0x0, STR_NULL}, celestar@5642: celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 100, 272, 22, 33, 0x0, STR_NETWORK_NEW_GAME_NAME_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 285, 405, 22, 33, STR_NETWORK_SET_PASSWORD, STR_NETWORK_PASSWORD_TIP}, celestar@5642: celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 10, 271, 62, 216, 0x0, STR_NETWORK_SELECT_MAP_TIP}, celestar@5642: { WWT_SCROLLBAR, RESIZE_NONE, BGC, 259, 270, 63, 215, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, celestar@5642: /* Combo boxes to control Connection Type / Max Clients / Max Companies / Max Observers / Language */ celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 280, 410, 77, 88, STR_NETWORK_COMBO1, STR_NETWORK_CONNECTION_TIP}, celestar@5642: { WWT_TEXTBTN, RESIZE_NONE, BGC, 399, 409, 78, 87, STR_0225, STR_NETWORK_CONNECTION_TIP}, celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 280, 410, 109, 120, STR_NETWORK_COMBO2, STR_NETWORK_NUMBER_OF_CLIENTS_TIP}, celestar@5642: { WWT_TEXTBTN, RESIZE_NONE, BGC, 399, 409, 110, 119, STR_0225, STR_NETWORK_NUMBER_OF_CLIENTS_TIP}, celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 280, 410, 141, 152, STR_NETWORK_COMBO3, STR_NETWORK_NUMBER_OF_COMPANIES_TIP}, celestar@5642: { WWT_TEXTBTN, RESIZE_NONE, BGC, 399, 409, 142, 151, STR_0225, STR_NETWORK_NUMBER_OF_COMPANIES_TIP}, celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 280, 410, 173, 184, STR_NETWORK_COMBO4, STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, celestar@5642: { WWT_TEXTBTN, RESIZE_NONE, BGC, 399, 409, 174, 183, STR_0225, STR_NETWORK_NUMBER_OF_SPECTATORS_TIP}, celestar@5642: { WWT_INSET, RESIZE_NONE, BGC, 280, 410, 205, 216, STR_NETWORK_COMBO5, STR_NETWORK_LANGUAGE_TIP}, celestar@5642: { WWT_TEXTBTN, RESIZE_NONE, BGC, 399, 409, 206, 215, STR_0225, STR_NETWORK_LANGUAGE_TIP}, celestar@5642: celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 40, 140, 224, 235, STR_NETWORK_START_GAME, STR_NETWORK_START_GAME_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 150, 250, 224, 235, STR_NETWORK_LOAD_GAME, STR_NETWORK_LOAD_GAME_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 260, 360, 224, 235, STR_012E_CANCEL, STR_NULL}, celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static const WindowDesc _network_start_server_window_desc = { celestar@5642: WDP_CENTER, WDP_CENTER, 420, 244, celestar@5642: WC_NETWORK_WINDOW,0, celestar@5642: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, celestar@5642: _network_start_server_window_widgets, celestar@5642: NetworkStartServerWindowWndProc, celestar@5642: }; celestar@5642: celestar@5642: static void ShowNetworkStartServerWindow(void) celestar@5642: { celestar@5642: Window *w; celestar@5642: DeleteWindowById(WC_NETWORK_WINDOW, 0); celestar@5642: celestar@5642: w = AllocateWindowDesc(&_network_start_server_window_desc); celestar@5642: ttd_strlcpy(_edit_str_buf, _network_server_name, lengthof(_edit_str_buf)); celestar@5642: celestar@5642: _saveload_mode = SLD_NEW_GAME; celestar@5642: BuildFileList(); celestar@5642: w->vscroll.cap = 12; celestar@5642: w->vscroll.count = _fios_num+1; celestar@5642: celestar@5642: WP(w, network_ql_d).q.afilter = CS_ALPHANUMERAL; celestar@5642: InitializeTextBuffer(&WP(w, network_ql_d).q.text, _edit_str_buf, lengthof(_edit_str_buf), 160); celestar@5642: } celestar@5642: celestar@5642: static byte NetworkLobbyFindCompanyIndex(byte pos) celestar@5642: { celestar@5642: byte i; celestar@5642: celestar@5642: /* Scroll through all _network_player_info and get the 'pos' item celestar@5642: that is not empty */ celestar@5642: for (i = 0; i < MAX_PLAYERS; i++) { celestar@5642: if (_network_player_info[i].company_name[0] != '\0') { celestar@5642: if (pos-- == 0) return i; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: return 0; celestar@5642: } celestar@5642: celestar@5642: /* uses network_d WP macro */ celestar@5642: static void NetworkLobbyWindowWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: network_d *nd = &WP(w, network_d); celestar@5642: celestar@5642: switch (e->event) { celestar@5642: case WE_CREATE: celestar@5642: nd->company = (byte)-1; celestar@5642: break; celestar@5642: celestar@5642: case WE_PAINT: { celestar@5642: const NetworkGameInfo *gi = &nd->server->info; celestar@5642: int y = NET_PRC__OFFSET_TOP_WIDGET_COMPANY, pos; celestar@5642: celestar@5642: SetWindowWidgetDisabledState(w, 7, nd->company == (byte)-1); celestar@5642: SetWindowWidgetDisabledState(w, 8, gi->companies_on >= gi->companies_max); celestar@5642: /* You can not join a server as spectator when it has no companies active.. celestar@5642: * it causes some nasty crashes */ celestar@5642: SetWindowWidgetDisabledState(w, 9, gi->spectators_on >= gi->spectators_max || celestar@5642: gi->companies_on == 0); celestar@5642: celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: SetDParamStr(0, gi->server_name); celestar@5642: DrawString(10, 22, STR_NETWORK_PREPARE_TO_JOIN, 2); celestar@5642: celestar@5642: /* Draw company list */ celestar@5642: pos = w->vscroll.pos; celestar@5642: while (pos < gi->companies_on) { celestar@5642: byte company = NetworkLobbyFindCompanyIndex(pos); celestar@5642: bool income = false; celestar@5642: if (nd->company == company) celestar@5642: GfxFillRect(11, y - 1, 154, y + 10, 10); // show highlighted item with a different colour celestar@5642: celestar@5642: DoDrawStringTruncated(_network_player_info[company].company_name, 13, y, 16, 135 - 13); celestar@5642: if (_network_player_info[company].use_password != 0) DrawSprite(SPR_LOCK, 135, y); celestar@5642: celestar@5642: /* If the company's income was positive puts a green dot else a red dot */ celestar@5642: if (_network_player_info[company].income >= 0) income = true; celestar@5642: DrawSprite(SPR_BLOT | (income ? PALETTE_TO_GREEN : PALETTE_TO_RED), 145, y); celestar@5642: celestar@5642: pos++; celestar@5642: y += NET_PRC__SIZE_OF_ROW; celestar@5642: if (pos >= w->vscroll.cap) break; celestar@5642: } celestar@5642: celestar@5642: /* Draw info about selected company when it is selected in the left window */ celestar@5642: GfxFillRect(174, 39, 403, 75, 157); celestar@5642: DrawStringCentered(290, 50, STR_NETWORK_COMPANY_INFO, 0); celestar@5642: if (nd->company != (byte)-1) { celestar@5642: const uint x = 183; celestar@5642: const uint trunc_width = w->widget[6].right - x; celestar@5642: y = 80; celestar@5642: celestar@5642: SetDParam(0, nd->server->info.clients_on); celestar@5642: SetDParam(1, nd->server->info.clients_max); celestar@5642: SetDParam(2, nd->server->info.companies_on); celestar@5642: SetDParam(3, nd->server->info.companies_max); celestar@5642: DrawString(x, y, STR_NETWORK_CLIENTS, 2); celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParamStr(0, _network_player_info[nd->company].company_name); celestar@5642: DrawStringTruncated(x, y, STR_NETWORK_COMPANY_NAME, 2, trunc_width); celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, _network_player_info[nd->company].inaugurated_year); celestar@5642: DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, 2); // inauguration year celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam64(0, _network_player_info[nd->company].company_value); celestar@5642: DrawString(x, y, STR_NETWORK_VALUE, 2); // company value celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam64(0, _network_player_info[nd->company].money); celestar@5642: DrawString(x, y, STR_NETWORK_CURRENT_BALANCE, 2); // current balance celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam64(0, _network_player_info[nd->company].income); celestar@5642: DrawString(x, y, STR_NETWORK_LAST_YEARS_INCOME, 2); // last year's income celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, _network_player_info[nd->company].performance); celestar@5642: DrawString(x, y, STR_NETWORK_PERFORMANCE, 2); // performance celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, _network_player_info[nd->company].num_vehicle[0]); celestar@5642: SetDParam(1, _network_player_info[nd->company].num_vehicle[1]); celestar@5642: SetDParam(2, _network_player_info[nd->company].num_vehicle[2]); celestar@5642: SetDParam(3, _network_player_info[nd->company].num_vehicle[3]); celestar@5642: SetDParam(4, _network_player_info[nd->company].num_vehicle[4]); celestar@5642: DrawString(x, y, STR_NETWORK_VEHICLES, 2); // vehicles celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParam(0, _network_player_info[nd->company].num_station[0]); celestar@5642: SetDParam(1, _network_player_info[nd->company].num_station[1]); celestar@5642: SetDParam(2, _network_player_info[nd->company].num_station[2]); celestar@5642: SetDParam(3, _network_player_info[nd->company].num_station[3]); celestar@5642: SetDParam(4, _network_player_info[nd->company].num_station[4]); celestar@5642: DrawString(x, y, STR_NETWORK_STATIONS, 2); // stations celestar@5642: y += 10; celestar@5642: celestar@5642: SetDParamStr(0, _network_player_info[nd->company].players); celestar@5642: DrawStringTruncated(x, y, STR_NETWORK_PLAYERS, 2, trunc_width); // players celestar@5642: } celestar@5642: } break; celestar@5642: celestar@5642: case WE_CLICK: celestar@5642: switch (e->we.click.widget) { celestar@5642: case 0: case 11: /* Close 'X' | Cancel button */ celestar@5642: ShowNetworkGameWindow(); celestar@5642: break; celestar@5642: case 4: { /* Company list */ celestar@5642: uint32 id_v = (e->we.click.pt.y - NET_PRC__OFFSET_TOP_WIDGET_COMPANY) / NET_PRC__SIZE_OF_ROW; celestar@5642: celestar@5642: if (id_v >= w->vscroll.cap) return; celestar@5642: celestar@5642: id_v += w->vscroll.pos; celestar@5642: nd->company = (id_v >= nd->server->info.companies_on) ? (byte)-1 : NetworkLobbyFindCompanyIndex(id_v); celestar@5642: SetWindowDirty(w); celestar@5642: } break; celestar@5642: case 7: /* Join company */ celestar@5642: if (nd->company != (byte)-1) { celestar@5642: _network_playas = nd->company; celestar@5642: NetworkClientConnectGame(_network_last_host, _network_last_port); celestar@5642: } celestar@5642: break; celestar@5642: case 8: /* New company */ celestar@5642: _network_playas = PLAYER_NEW_COMPANY; celestar@5642: NetworkClientConnectGame(_network_last_host, _network_last_port); celestar@5642: break; celestar@5642: case 9: /* Spectate game */ celestar@5642: _network_playas = PLAYER_SPECTATOR; celestar@5642: NetworkClientConnectGame(_network_last_host, _network_last_port); celestar@5642: break; celestar@5642: case 10: /* Refresh */ celestar@5642: NetworkQueryServer(_network_last_host, _network_last_port, false); // company info celestar@5642: NetworkUDPQueryServer(_network_last_host, _network_last_port); // general data celestar@5642: break; celestar@5642: } break; celestar@5642: celestar@5642: case WE_MESSAGE: celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: static const Widget _network_lobby_window_widgets[] = { celestar@5642: { WWT_CLOSEBOX, RESIZE_NONE, BGC, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW }, celestar@5642: { WWT_CAPTION, RESIZE_NONE, BGC, 11, 419, 0, 13, STR_NETWORK_GAME_LOBBY, STR_NULL}, celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 0, 419, 14, 234, 0x0, STR_NULL}, celestar@5642: celestar@5642: // company list celestar@5642: { WWT_PANEL, RESIZE_NONE, BTC, 10, 155, 38, 49, 0x0, STR_NULL}, celestar@5642: { WWT_MATRIX, RESIZE_NONE, BGC, 10, 155, 50, 190, (10 << 8) + 1, STR_NETWORK_COMPANY_LIST_TIP}, celestar@5642: { WWT_SCROLLBAR, RESIZE_NONE, BGC, 156, 167, 38, 190, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, celestar@5642: celestar@5642: // company/player info celestar@5642: { WWT_PANEL, RESIZE_NONE, BGC, 173, 404, 38, 190, 0x0, STR_NULL}, celestar@5642: celestar@5642: // buttons celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 10, 151, 200, 211, STR_NETWORK_JOIN_COMPANY, STR_NETWORK_JOIN_COMPANY_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 10, 151, 215, 226, STR_NETWORK_NEW_COMPANY, STR_NETWORK_NEW_COMPANY_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 158, 268, 200, 211, STR_NETWORK_SPECTATE_GAME, STR_NETWORK_SPECTATE_GAME_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 158, 268, 215, 226, STR_NETWORK_REFRESH, STR_NETWORK_REFRESH_TIP}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 278, 388, 200, 211, STR_012E_CANCEL, STR_NULL}, celestar@5642: celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static const WindowDesc _network_lobby_window_desc = { celestar@5642: WDP_CENTER, WDP_CENTER, 420, 235, celestar@5642: WC_NETWORK_WINDOW,0, celestar@5642: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, celestar@5642: _network_lobby_window_widgets, celestar@5642: NetworkLobbyWindowWndProc, celestar@5642: }; celestar@5642: celestar@5642: /* Show the networklobbywindow with the selected server celestar@5642: * @param ngl Selected game pointer which is passed to the new window */ celestar@5642: static void ShowNetworkLobbyWindow(NetworkGameList *ngl) celestar@5642: { celestar@5642: Window *w; celestar@5642: DeleteWindowById(WC_NETWORK_WINDOW, 0); celestar@5642: celestar@5642: NetworkQueryServer(_network_last_host, _network_last_port, false); // company info celestar@5642: NetworkUDPQueryServer(_network_last_host, _network_last_port); // general data celestar@5642: celestar@5642: w = AllocateWindowDesc(&_network_lobby_window_desc); celestar@5642: if (w != NULL) { celestar@5642: WP(w, network_ql_d).n.server = ngl; celestar@5642: strcpy(_edit_str_buf, ""); celestar@5642: w->vscroll.cap = 10; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: // The window below gives information about the connected clients celestar@5642: // and also makes able to give money to them, kick them (if server) celestar@5642: // and stuff like that. celestar@5642: celestar@5642: extern void DrawPlayerIcon(PlayerID pid, int x, int y); celestar@5642: celestar@5642: // Every action must be of this form celestar@5642: typedef void ClientList_Action_Proc(byte client_no); celestar@5642: celestar@5642: // Max 10 actions per client celestar@5642: #define MAX_CLIENTLIST_ACTION 10 celestar@5642: celestar@5642: // Some standard bullshit.. defines variables ;) celestar@5642: static void ClientListWndProc(Window *w, WindowEvent *e); celestar@5642: static void ClientListPopupWndProc(Window *w, WindowEvent *e); celestar@5642: static byte _selected_clientlist_item = 255; celestar@5642: static byte _selected_clientlist_y = 0; celestar@5642: static char _clientlist_action[MAX_CLIENTLIST_ACTION][50]; celestar@5642: static ClientList_Action_Proc *_clientlist_proc[MAX_CLIENTLIST_ACTION]; celestar@5642: celestar@5642: enum { celestar@5642: CLNWND_OFFSET = 16, celestar@5642: CLNWND_ROWSIZE = 10 celestar@5642: }; celestar@5642: celestar@5642: static const Widget _client_list_widgets[] = { celestar@5642: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, celestar@5642: { WWT_CAPTION, RESIZE_NONE, 14, 11, 249, 0, 13, STR_NETWORK_CLIENT_LIST, STR_018C_WINDOW_TITLE_DRAG_THIS}, celestar@5642: celestar@5642: { WWT_PANEL, RESIZE_NONE, 14, 0, 249, 14, 14 + CLNWND_ROWSIZE + 1, 0x0, STR_NULL}, celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static const Widget _client_list_popup_widgets[] = { celestar@5642: { WWT_PANEL, RESIZE_NONE, 14, 0, 99, 0, 0, 0, STR_NULL}, celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static WindowDesc _client_list_desc = { celestar@5642: WDP_AUTO, WDP_AUTO, 250, 1, celestar@5642: WC_CLIENT_LIST,0, celestar@5642: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, celestar@5642: _client_list_widgets, celestar@5642: ClientListWndProc celestar@5642: }; celestar@5642: celestar@5642: // Finds the Xth client-info that is active celestar@5642: static const NetworkClientInfo *NetworkFindClientInfo(byte client_no) celestar@5642: { celestar@5642: const NetworkClientInfo *ci; celestar@5642: celestar@5642: FOR_ALL_ACTIVE_CLIENT_INFOS(ci) { celestar@5642: if (client_no == 0) return ci; celestar@5642: client_no--; celestar@5642: } celestar@5642: celestar@5642: return NULL; celestar@5642: } celestar@5642: celestar@5642: // Here we start to define the options out of the menu celestar@5642: static void ClientList_Kick(byte client_no) celestar@5642: { celestar@5642: if (client_no < MAX_PLAYERS) celestar@5642: SEND_COMMAND(PACKET_SERVER_ERROR)(DEREF_CLIENT(client_no), NETWORK_ERROR_KICKED); celestar@5642: } celestar@5642: celestar@5642: static void ClientList_Ban(byte client_no) celestar@5642: { celestar@5642: uint i; celestar@5642: uint32 ip = NetworkFindClientInfo(client_no)->client_ip; celestar@5642: celestar@5642: for (i = 0; i < lengthof(_network_ban_list); i++) { celestar@5642: if (_network_ban_list[i] == NULL) { celestar@5642: _network_ban_list[i] = strdup(inet_ntoa(*(struct in_addr *)&ip)); celestar@5642: break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: if (client_no < MAX_PLAYERS) celestar@5642: SEND_COMMAND(PACKET_SERVER_ERROR)(DEREF_CLIENT(client_no), NETWORK_ERROR_KICKED); celestar@5642: } celestar@5642: celestar@5642: static void ClientList_GiveMoney(byte client_no) celestar@5642: { celestar@5642: if (NetworkFindClientInfo(client_no) != NULL) celestar@5642: ShowNetworkGiveMoneyWindow(NetworkFindClientInfo(client_no)->client_playas); celestar@5642: } celestar@5642: celestar@5642: static void ClientList_SpeakToClient(byte client_no) celestar@5642: { celestar@5642: if (NetworkFindClientInfo(client_no) != NULL) celestar@5642: ShowNetworkChatQueryWindow(DESTTYPE_CLIENT, NetworkFindClientInfo(client_no)->client_index); celestar@5642: } celestar@5642: celestar@5642: static void ClientList_SpeakToCompany(byte client_no) celestar@5642: { celestar@5642: if (NetworkFindClientInfo(client_no) != NULL) celestar@5642: ShowNetworkChatQueryWindow(DESTTYPE_TEAM, NetworkFindClientInfo(client_no)->client_playas); celestar@5642: } celestar@5642: celestar@5642: static void ClientList_SpeakToAll(byte client_no) celestar@5642: { celestar@5642: ShowNetworkChatQueryWindow(DESTTYPE_BROADCAST, 0); celestar@5642: } celestar@5642: celestar@5642: static void ClientList_None(byte client_no) celestar@5642: { celestar@5642: // No action ;) celestar@5642: } celestar@5642: celestar@5642: celestar@5642: celestar@5642: // Help, a action is clicked! What do we do? celestar@5642: static void HandleClientListPopupClick(byte index, byte clientno) { celestar@5642: // A click on the Popup of the ClientList.. handle the command celestar@5642: if (index < MAX_CLIENTLIST_ACTION && _clientlist_proc[index] != NULL) { celestar@5642: _clientlist_proc[index](clientno); celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: // Finds the amount of clients and set the height correct celestar@5642: static bool CheckClientListHeight(Window *w) celestar@5642: { celestar@5642: int num = 0; celestar@5642: const NetworkClientInfo *ci; celestar@5642: celestar@5642: // Should be replaced with a loop through all clients celestar@5642: FOR_ALL_ACTIVE_CLIENT_INFOS(ci) { celestar@5642: num++; celestar@5642: } celestar@5642: celestar@5642: num *= CLNWND_ROWSIZE; celestar@5642: celestar@5642: // If height is changed celestar@5642: if (w->height != CLNWND_OFFSET + num + 1) { celestar@5642: // XXX - magic unfortunately; (num + 2) has to be one bigger than heigh (num + 1) celestar@5642: SetWindowDirty(w); celestar@5642: w->widget[2].bottom = w->widget[2].top + num + 2; celestar@5642: w->height = CLNWND_OFFSET + num + 1; celestar@5642: SetWindowDirty(w); celestar@5642: return false; celestar@5642: } celestar@5642: return true; celestar@5642: } celestar@5642: celestar@5642: // Finds the amount of actions in the popup and set the height correct celestar@5642: static uint ClientListPopupHeigth(void) { celestar@5642: int i, num = 0; celestar@5642: celestar@5642: // Find the amount of actions celestar@5642: for (i = 0; i < MAX_CLIENTLIST_ACTION; i++) { celestar@5642: if (_clientlist_action[i][0] == '\0') continue; celestar@5642: if (_clientlist_proc[i] == NULL) continue; celestar@5642: num++; celestar@5642: } celestar@5642: celestar@5642: num *= CLNWND_ROWSIZE; celestar@5642: celestar@5642: return num + 1; celestar@5642: } celestar@5642: celestar@5642: // Show the popup (action list) celestar@5642: static Window *PopupClientList(Window *w, int client_no, int x, int y) celestar@5642: { celestar@5642: int i, h; celestar@5642: const NetworkClientInfo *ci; celestar@5642: DeleteWindowById(WC_TOOLBAR_MENU, 0); celestar@5642: celestar@5642: // Clean the current actions celestar@5642: for (i = 0; i < MAX_CLIENTLIST_ACTION; i++) { celestar@5642: _clientlist_action[i][0] = '\0'; celestar@5642: _clientlist_proc[i] = NULL; celestar@5642: } celestar@5642: celestar@5642: // Fill the actions this client has celestar@5642: // Watch is, max 50 chars long! celestar@5642: celestar@5642: ci = NetworkFindClientInfo(client_no); celestar@5642: if (ci == NULL) return NULL; celestar@5642: celestar@5642: i = 0; celestar@5642: if (_network_own_client_index != ci->client_index) { celestar@5642: GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_CLIENT, lastof(_clientlist_action[i])); celestar@5642: _clientlist_proc[i++] = &ClientList_SpeakToClient; celestar@5642: } celestar@5642: celestar@5642: if (IsValidPlayer(ci->client_playas) || ci->client_playas == PLAYER_SPECTATOR) { celestar@5642: GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_COMPANY, lastof(_clientlist_action[i])); celestar@5642: _clientlist_proc[i++] = &ClientList_SpeakToCompany; celestar@5642: } celestar@5642: GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_SPEAK_TO_ALL, lastof(_clientlist_action[i])); celestar@5642: _clientlist_proc[i++] = &ClientList_SpeakToAll; celestar@5642: celestar@5642: if (_network_own_client_index != ci->client_index) { celestar@5642: /* We are no spectator and the player we want to give money to is no spectator */ celestar@5642: if (IsValidPlayer(_network_playas) && IsValidPlayer(ci->client_playas)) { celestar@5642: GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_GIVE_MONEY, lastof(_clientlist_action[i])); celestar@5642: _clientlist_proc[i++] = &ClientList_GiveMoney; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: // A server can kick clients (but not himself) celestar@5642: if (_network_server && _network_own_client_index != ci->client_index) { celestar@5642: GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_KICK, lastof(_clientlist_action[i])); celestar@5642: _clientlist_proc[i++] = &ClientList_Kick; celestar@5642: celestar@5642: sprintf(_clientlist_action[i],"Ban"); // XXX GetString? celestar@5642: _clientlist_proc[i++] = &ClientList_Ban; celestar@5642: } celestar@5642: celestar@5642: if (i == 0) { celestar@5642: GetString(_clientlist_action[i], STR_NETWORK_CLIENTLIST_NONE, lastof(_clientlist_action[i])); celestar@5642: _clientlist_proc[i++] = &ClientList_None; celestar@5642: } celestar@5642: celestar@5642: /* Calculate the height */ celestar@5642: h = ClientListPopupHeigth(); celestar@5642: celestar@5642: // Allocate the popup celestar@5642: w = AllocateWindow(x, y, 150, h + 1, ClientListPopupWndProc, WC_TOOLBAR_MENU, _client_list_popup_widgets); celestar@5642: w->widget[0].bottom = w->widget[0].top + h; celestar@5642: w->widget[0].right = w->widget[0].left + 150; celestar@5642: celestar@5642: w->flags4 &= ~WF_WHITE_BORDER_MASK; celestar@5642: WP(w,menu_d).item_count = 0; celestar@5642: // Save our client celestar@5642: WP(w,menu_d).main_button = client_no; celestar@5642: WP(w,menu_d).sel_index = 0; celestar@5642: // We are a popup celestar@5642: _popup_menu_active = true; celestar@5642: celestar@5642: return w; celestar@5642: } celestar@5642: celestar@5642: /** Main handle for the client popup list celestar@5642: * uses menu_d WP macro */ celestar@5642: static void ClientListPopupWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: switch (e->event) { celestar@5642: case WE_PAINT: { celestar@5642: int i, y, sel; celestar@5642: byte colour; celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: // Draw the actions celestar@5642: sel = WP(w,menu_d).sel_index; celestar@5642: y = 1; celestar@5642: for (i = 0; i < MAX_CLIENTLIST_ACTION; i++, y += CLNWND_ROWSIZE) { celestar@5642: if (_clientlist_action[i][0] == '\0') continue; celestar@5642: if (_clientlist_proc[i] == NULL) continue; celestar@5642: celestar@5642: if (sel-- == 0) { // Selected item, highlight it celestar@5642: GfxFillRect(1, y, 150 - 2, y + CLNWND_ROWSIZE - 1, 0); celestar@5642: colour = 0xC; celestar@5642: } else { celestar@5642: colour = 0x10; celestar@5642: } celestar@5642: celestar@5642: DoDrawString(_clientlist_action[i], 4, y, colour); celestar@5642: } celestar@5642: } break; celestar@5642: celestar@5642: case WE_POPUPMENU_SELECT: { celestar@5642: // We selected an action celestar@5642: int index = (e->we.popupmenu.pt.y - w->top) / CLNWND_ROWSIZE; celestar@5642: celestar@5642: if (index >= 0 && e->we.popupmenu.pt.y >= w->top) celestar@5642: HandleClientListPopupClick(index, WP(w,menu_d).main_button); celestar@5642: celestar@5642: DeleteWindowById(WC_TOOLBAR_MENU, 0); celestar@5642: } break; celestar@5642: celestar@5642: case WE_POPUPMENU_OVER: { celestar@5642: // Our mouse hoovers over an action? Select it! celestar@5642: int index = (e->we.popupmenu.pt.y - w->top) / CLNWND_ROWSIZE; celestar@5642: celestar@5642: if (index == -1 || index == WP(w,menu_d).sel_index) return; celestar@5642: celestar@5642: WP(w,menu_d).sel_index = index; celestar@5642: SetWindowDirty(w); celestar@5642: } break; celestar@5642: celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: // Main handle for clientlist celestar@5642: static void ClientListWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: switch (e->event) { celestar@5642: case WE_PAINT: { celestar@5642: NetworkClientInfo *ci; celestar@5642: int y, i = 0; celestar@5642: byte colour; celestar@5642: celestar@5642: // Check if we need to reset the height celestar@5642: if (!CheckClientListHeight(w)) break; celestar@5642: celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: y = CLNWND_OFFSET; celestar@5642: celestar@5642: FOR_ALL_ACTIVE_CLIENT_INFOS(ci) { celestar@5642: if (_selected_clientlist_item == i++) { // Selected item, highlight it celestar@5642: GfxFillRect(1, y, 248, y + CLNWND_ROWSIZE - 1, 0); celestar@5642: colour = 0xC; celestar@5642: } else { celestar@5642: colour = 0x10; celestar@5642: } celestar@5642: celestar@5642: if (ci->client_index == NETWORK_SERVER_INDEX) { celestar@5642: DrawString(4, y, STR_NETWORK_SERVER, colour); celestar@5642: } else { celestar@5642: DrawString(4, y, STR_NETWORK_CLIENT, colour); celestar@5642: } celestar@5642: celestar@5642: // Filter out spectators celestar@5642: if (IsValidPlayer(ci->client_playas)) DrawPlayerIcon(ci->client_playas, 64, y + 1); celestar@5642: celestar@5642: DoDrawString(ci->client_name, 81, y, colour); celestar@5642: celestar@5642: y += CLNWND_ROWSIZE; celestar@5642: } celestar@5642: } break; celestar@5642: celestar@5642: case WE_CLICK: celestar@5642: // Show the popup with option celestar@5642: if (_selected_clientlist_item != 255) { celestar@5642: PopupClientList(w, _selected_clientlist_item, e->we.click.pt.x + w->left, e->we.click.pt.y + w->top); celestar@5642: } celestar@5642: celestar@5642: break; celestar@5642: celestar@5642: case WE_MOUSEOVER: celestar@5642: // -1 means we left the current window celestar@5642: if (e->we.mouseover.pt.y == -1) { celestar@5642: _selected_clientlist_y = 0; celestar@5642: _selected_clientlist_item = 255; celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: } celestar@5642: // It did not change.. no update! celestar@5642: if (e->we.mouseover.pt.y == _selected_clientlist_y) break; celestar@5642: celestar@5642: // Find the new selected item (if any) celestar@5642: _selected_clientlist_y = e->we.mouseover.pt.y; celestar@5642: if (e->we.mouseover.pt.y > CLNWND_OFFSET) { celestar@5642: _selected_clientlist_item = (e->we.mouseover.pt.y - CLNWND_OFFSET) / CLNWND_ROWSIZE; celestar@5642: } else { celestar@5642: _selected_clientlist_item = 255; celestar@5642: } celestar@5642: celestar@5642: // Repaint celestar@5642: SetWindowDirty(w); celestar@5642: break; celestar@5642: celestar@5642: case WE_DESTROY: case WE_CREATE: celestar@5642: // When created or destroyed, data is reset celestar@5642: _selected_clientlist_item = 255; celestar@5642: _selected_clientlist_y = 0; celestar@5642: break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: void ShowClientList(void) celestar@5642: { celestar@5642: AllocateWindowDescFront(&_client_list_desc, 0); celestar@5642: } celestar@5642: celestar@5642: celestar@5642: static NetworkPasswordType pw_type; celestar@5642: celestar@5642: celestar@5642: void ShowNetworkNeedPassword(NetworkPasswordType npt) celestar@5642: { celestar@5642: StringID caption; celestar@5642: celestar@5642: pw_type = npt; celestar@5642: switch (npt) { celestar@5642: default: NOT_REACHED(); celestar@5642: case NETWORK_GAME_PASSWORD: caption = STR_NETWORK_NEED_GAME_PASSWORD_CAPTION; break; celestar@5642: case NETWORK_COMPANY_PASSWORD: caption = STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION; break; celestar@5642: } celestar@5642: ShowQueryString(STR_EMPTY, caption, 20, 180, FindWindowById(WC_NETWORK_STATUS_WINDOW, 0), CS_ALPHANUMERAL); celestar@5642: } celestar@5642: celestar@5642: celestar@5642: static void NetworkJoinStatusWindowWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: switch (e->event) { celestar@5642: case WE_PAINT: { celestar@5642: uint8 progress; // used for progress bar celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: DrawStringCentered(125, 35, STR_NETWORK_CONNECTING_1 + _network_join_status, 14); celestar@5642: switch (_network_join_status) { celestar@5642: case NETWORK_JOIN_STATUS_CONNECTING: case NETWORK_JOIN_STATUS_AUTHORIZING: celestar@5642: case NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO: celestar@5642: progress = 10; // first two stages 10% celestar@5642: break; celestar@5642: case NETWORK_JOIN_STATUS_WAITING: celestar@5642: SetDParam(0, _network_join_waiting); celestar@5642: DrawStringCentered(125, 46, STR_NETWORK_CONNECTING_WAITING, 14); celestar@5642: progress = 15; // third stage is 15% celestar@5642: break; celestar@5642: case NETWORK_JOIN_STATUS_DOWNLOADING: celestar@5642: SetDParam(0, _network_join_kbytes); celestar@5642: SetDParam(1, _network_join_kbytes_total); celestar@5642: DrawStringCentered(125, 46, STR_NETWORK_CONNECTING_DOWNLOADING, 14); celestar@5642: /* Fallthrough */ celestar@5642: default: /* Waiting is 15%, so the resting receivement of map is maximum 70% */ celestar@5642: progress = 15 + _network_join_kbytes * (100 - 15) / _network_join_kbytes_total; celestar@5642: } celestar@5642: celestar@5642: /* Draw nice progress bar :) */ celestar@5642: DrawFrameRect(20, 18, (int)((w->width - 20) * progress / 100), 28, 10, 0); celestar@5642: } break; celestar@5642: celestar@5642: case WE_CLICK: celestar@5642: switch (e->we.click.widget) { celestar@5642: case 2: /* Disconnect button */ celestar@5642: NetworkDisconnect(); celestar@5642: DeleteWindow(w); celestar@5642: SwitchMode(SM_MENU); celestar@5642: ShowNetworkGameWindow(); celestar@5642: break; celestar@5642: } celestar@5642: break; celestar@5642: celestar@5642: /* If the server asks for a password, we need to fill it in */ celestar@5642: case WE_ON_EDIT_TEXT_CANCEL: celestar@5642: NetworkDisconnect(); celestar@5642: ShowNetworkGameWindow(); celestar@5642: break; celestar@5642: celestar@5642: case WE_ON_EDIT_TEXT: celestar@5642: SEND_COMMAND(PACKET_CLIENT_PASSWORD)(pw_type, e->we.edittext.str); celestar@5642: break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: static const Widget _network_join_status_window_widget[] = { celestar@5642: { WWT_CAPTION, RESIZE_NONE, 14, 0, 249, 0, 13, STR_NETWORK_CONNECTING, STR_018C_WINDOW_TITLE_DRAG_THIS}, celestar@5642: { WWT_PANEL, RESIZE_NONE, 14, 0, 249, 14, 84, 0x0, STR_NULL}, celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, BTC, 75, 175, 69, 80, STR_NETWORK_DISCONNECT, STR_NULL}, celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static const WindowDesc _network_join_status_window_desc = { celestar@5642: WDP_CENTER, WDP_CENTER, 250, 85, celestar@5642: WC_NETWORK_STATUS_WINDOW, 0, celestar@5642: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_MODAL, celestar@5642: _network_join_status_window_widget, celestar@5642: NetworkJoinStatusWindowWndProc, celestar@5642: }; celestar@5642: celestar@5642: void ShowJoinStatusWindow(void) celestar@5642: { celestar@5642: Window *w; celestar@5642: DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); celestar@5642: w = AllocateWindowDesc(&_network_join_status_window_desc); celestar@5642: /* Parent the status window to the lobby */ celestar@5642: if (w != NULL) w->parent = FindWindowById(WC_NETWORK_WINDOW, 0); celestar@5642: } celestar@5642: celestar@5642: static void SendChat(const char *buf, DestType type, byte dest) celestar@5642: { celestar@5642: if (buf[0] == '\0') return; celestar@5642: if (!_network_server) { celestar@5642: SEND_COMMAND(PACKET_CLIENT_CHAT)(NETWORK_ACTION_CHAT + type, type, dest, buf); celestar@5642: } else { celestar@5642: NetworkServer_HandleChat(NETWORK_ACTION_CHAT + type, type, dest, buf, NETWORK_SERVER_INDEX); celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: /** celestar@5642: * Find the next item of the list of things that can be auto-completed. celestar@5642: * @param item The current indexed item to return. This function can, and most celestar@5642: * likely will, alter item, to skip empty items in the arrays. celestar@5642: * @return Returns the char that matched to the index. celestar@5642: */ celestar@5642: static const char *ChatTabCompletionNextItem(uint *item) celestar@5642: { celestar@5642: static char chat_tab_temp_buffer[64]; celestar@5642: celestar@5642: /* First, try clients */ celestar@5642: if (*item < MAX_CLIENT_INFO) { celestar@5642: /* Skip inactive clients */ celestar@5642: while (_network_client_info[*item].client_index == NETWORK_EMPTY_INDEX && *item < MAX_CLIENT_INFO) (*item)++; celestar@5642: if (*item < MAX_CLIENT_INFO) return _network_client_info[*item].client_name; celestar@5642: } celestar@5642: celestar@5642: /* Then, try townnames */ celestar@5642: /* Not that the following assumes all town indices are adjacent, ie no celestar@5642: * towns have been deleted. */ celestar@5642: if (*item <= (uint)MAX_CLIENT_INFO + GetMaxTownIndex()) { celestar@5642: const Town *t; celestar@5642: celestar@5642: FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_INFO) { celestar@5642: /* Get the town-name via the string-system */ celestar@5642: SetDParam(0, t->townnameparts); celestar@5642: GetString(chat_tab_temp_buffer, t->townnametype, lastof(chat_tab_temp_buffer)); celestar@5642: return &chat_tab_temp_buffer[0]; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: return NULL; celestar@5642: } celestar@5642: celestar@5642: /** celestar@5642: * Find what text to complete. It scans for a space from the left and marks celestar@5642: * the word right from that as to complete. It also writes a \0 at the celestar@5642: * position of the space (if any). If nothing found, buf is returned. celestar@5642: */ celestar@5642: static char *ChatTabCompletionFindText(char *buf) celestar@5642: { celestar@5642: char *p = strrchr(buf, ' '); celestar@5642: if (p == NULL) return buf; celestar@5642: celestar@5642: *p = '\0'; celestar@5642: return p + 1; celestar@5642: } celestar@5642: celestar@5642: /** celestar@5642: * See if we can auto-complete the current text of the user. celestar@5642: */ celestar@5642: static void ChatTabCompletion(Window *w) celestar@5642: { celestar@5642: static char _chat_tab_completion_buf[lengthof(_edit_str_buf)]; celestar@5642: Textbuf *tb = &WP(w, querystr_d).text; celestar@5642: uint len, tb_len; celestar@5642: uint item; celestar@5642: char *tb_buf, *pre_buf; celestar@5642: const char *cur_name; celestar@5642: bool second_scan = false; celestar@5642: celestar@5642: item = 0; celestar@5642: celestar@5642: /* Copy the buffer so we can modify it without damaging the real data */ celestar@5642: pre_buf = (_chat_tab_completion_active) ? strdup(_chat_tab_completion_buf) : strdup(tb->buf); celestar@5642: celestar@5642: tb_buf = ChatTabCompletionFindText(pre_buf); celestar@5642: tb_len = strlen(tb_buf); celestar@5642: celestar@5642: while ((cur_name = ChatTabCompletionNextItem(&item)) != NULL) { celestar@5642: item++; celestar@5642: celestar@5642: if (_chat_tab_completion_active) { celestar@5642: /* We are pressing TAB again on the same name, is there an other name celestar@5642: * that starts with this? */ celestar@5642: if (!second_scan) { celestar@5642: uint offset; celestar@5642: uint length; celestar@5642: celestar@5642: /* If we are completing at the begin of the line, skip the ': ' we added */ celestar@5642: if (tb_buf == pre_buf) { celestar@5642: offset = 0; celestar@5642: length = tb->length - 2; celestar@5642: } else { celestar@5642: /* Else, find the place we are completing at */ celestar@5642: offset = strlen(pre_buf) + 1; celestar@5642: length = tb->length - offset; celestar@5642: } celestar@5642: celestar@5642: /* Compare if we have a match */ celestar@5642: if (strlen(cur_name) == length && strncmp(cur_name, tb->buf + offset, length) == 0) second_scan = true; celestar@5642: celestar@5642: continue; celestar@5642: } celestar@5642: celestar@5642: /* Now any match we make on _chat_tab_completion_buf after this, is perfect */ celestar@5642: } celestar@5642: celestar@5642: len = strlen(cur_name); celestar@5642: if (tb_len < len && strncasecmp(cur_name, tb_buf, tb_len) == 0) { celestar@5642: /* Save the data it was before completion */ celestar@5642: if (!second_scan) snprintf(_chat_tab_completion_buf, lengthof(_chat_tab_completion_buf), "%s", tb->buf); celestar@5642: _chat_tab_completion_active = true; celestar@5642: celestar@5642: /* Change to the found name. Add ': ' if we are at the start of the line (pretty) */ celestar@5642: if (pre_buf == tb_buf) { celestar@5642: snprintf(tb->buf, lengthof(_edit_str_buf), "%s: ", cur_name); celestar@5642: } else { celestar@5642: snprintf(tb->buf, lengthof(_edit_str_buf), "%s %s", pre_buf, cur_name); celestar@5642: } celestar@5642: celestar@5642: /* Update the textbuffer */ celestar@5642: UpdateTextBufferSize(&WP(w, querystr_d).text); celestar@5642: celestar@5642: SetWindowDirty(w); celestar@5642: free(pre_buf); celestar@5642: return; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: if (second_scan) { celestar@5642: /* We walked all posibilities, and the user presses tab again.. revert to original text */ celestar@5642: strcpy(tb->buf, _chat_tab_completion_buf); celestar@5642: _chat_tab_completion_active = false; celestar@5642: celestar@5642: /* Update the textbuffer */ celestar@5642: UpdateTextBufferSize(&WP(w, querystr_d).text); celestar@5642: celestar@5642: SetWindowDirty(w); celestar@5642: } celestar@5642: free(pre_buf); celestar@5642: } celestar@5642: celestar@5642: /* uses querystr_d WP macro celestar@5642: * uses querystr_d->caption to store celestar@5642: * - type of chat message (Private/Team/All) in bytes 0-7 celestar@5642: * - destination of chat message in the case of Team/Private in bytes 8-15 */ celestar@5642: static void ChatWindowWndProc(Window *w, WindowEvent *e) celestar@5642: { celestar@5642: switch (e->event) { celestar@5642: case WE_CREATE: celestar@5642: SendWindowMessage(WC_NEWS_WINDOW, 0, WE_CREATE, w->height, 0); celestar@5642: SETBIT(_no_scroll, SCROLL_CHAT); // do not scroll the game with the arrow-keys celestar@5642: break; celestar@5642: celestar@5642: case WE_PAINT: { celestar@5642: static const StringID chat_captions[] = { celestar@5642: STR_NETWORK_CHAT_ALL_CAPTION, celestar@5642: STR_NETWORK_CHAT_COMPANY_CAPTION, celestar@5642: STR_NETWORK_CHAT_CLIENT_CAPTION celestar@5642: }; celestar@5642: StringID msg; celestar@5642: celestar@5642: DrawWindowWidgets(w); celestar@5642: celestar@5642: assert(GB(WP(w, querystr_d).caption, 0, 8) < lengthof(chat_captions)); celestar@5642: msg = chat_captions[GB(WP(w, querystr_d).caption, 0, 8)]; celestar@5642: DrawStringRightAligned(w->widget[2].left - 2, w->widget[2].top + 1, msg, 16); celestar@5642: DrawEditBox(w, &WP(w, querystr_d), 2); celestar@5642: } break; celestar@5642: celestar@5642: case WE_CLICK: celestar@5642: switch (e->we.click.widget) { celestar@5642: case 3: { /* Send */ celestar@5642: DestType type = GB(WP(w, querystr_d).caption, 0, 8); celestar@5642: byte dest = GB(WP(w, querystr_d).caption, 8, 8); celestar@5642: SendChat(WP(w, querystr_d).text.buf, type, dest); celestar@5642: } /* FALLTHROUGH */ celestar@5642: case 0: /* Cancel */ DeleteWindow(w); break; celestar@5642: } celestar@5642: break; celestar@5642: celestar@5642: case WE_MOUSELOOP: celestar@5642: HandleEditBox(w, &WP(w, querystr_d), 2); celestar@5642: break; celestar@5642: celestar@5642: case WE_KEYPRESS: celestar@5642: if (e->we.keypress.keycode == WKC_TAB) { celestar@5642: ChatTabCompletion(w); celestar@5642: } else { celestar@5642: _chat_tab_completion_active = false; celestar@5642: switch (HandleEditBoxKey(w, &WP(w, querystr_d), 2, e)) { celestar@5642: case 1: { /* Return */ celestar@5642: DestType type = GB(WP(w, querystr_d).caption, 0, 8); celestar@5642: byte dest = GB(WP(w, querystr_d).caption, 8, 8); celestar@5642: SendChat(WP(w, querystr_d).text.buf, type, dest); celestar@5642: } /* FALLTHROUGH */ celestar@5642: case 2: /* Escape */ DeleteWindow(w); break; celestar@5642: } celestar@5642: } celestar@5642: break; celestar@5642: celestar@5642: case WE_DESTROY: celestar@5642: SendWindowMessage(WC_NEWS_WINDOW, 0, WE_DESTROY, 0, 0); celestar@5642: CLRBIT(_no_scroll, SCROLL_CHAT); celestar@5642: break; celestar@5642: } celestar@5642: } celestar@5642: celestar@5642: static const Widget _chat_window_widgets[] = { celestar@5642: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, celestar@5642: { WWT_PANEL, RESIZE_NONE, 14, 11, 639, 0, 13, 0x0, STR_NULL}, // background celestar@5642: { WWT_PANEL, RESIZE_NONE, 14, 75, 577, 1, 12, 0x0, STR_NULL}, // text box celestar@5642: { WWT_PUSHTXTBTN, RESIZE_NONE, 14, 578, 639, 1, 12, STR_NETWORK_SEND, STR_NULL}, // send button celestar@5642: { WIDGETS_END}, celestar@5642: }; celestar@5642: celestar@5642: static const WindowDesc _chat_window_desc = { celestar@5642: WDP_CENTER, -26, 640, 14, // x, y, width, height celestar@5642: WC_SEND_NETWORK_MSG,0, celestar@5642: WDF_STD_TOOLTIPS | WDF_DEF_WIDGET, celestar@5642: _chat_window_widgets, celestar@5642: ChatWindowWndProc celestar@5642: }; celestar@5642: celestar@5642: void ShowNetworkChatQueryWindow(DestType type, byte dest) celestar@5642: { celestar@5642: Window *w; celestar@5642: celestar@5642: DeleteWindowById(WC_SEND_NETWORK_MSG, 0); celestar@5642: celestar@5642: _edit_str_buf[0] = '\0'; celestar@5642: _chat_tab_completion_active = false; celestar@5642: celestar@5642: w = AllocateWindowDesc(&_chat_window_desc); celestar@5642: celestar@5642: LowerWindowWidget(w, 2); celestar@5642: WP(w, querystr_d).caption = GB(type, 0, 8) | (dest << 8); // Misuse of caption celestar@5642: WP(w, querystr_d).afilter = CS_ALPHANUMERAL; celestar@5642: InitializeTextBuffer(&WP(w, querystr_d).text, _edit_str_buf, lengthof(_edit_str_buf), 0); celestar@5642: } celestar@5642: celestar@5642: #endif /* ENABLE_NETWORK */