tron@2186: /* $Id$ */ tron@2186: belugas@6918: /** @file town_gui.cpp */ belugas@6918: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" truelight@0: #include "town.h" rubidium@8720: #include "viewport_func.h" rubidium@8720: #include "gfx_func.h" truelight@0: #include "gui.h" rubidium@8603: #include "window_gui.h" rubidium@8603: #include "textbuf_gui.h" rubidium@8612: #include "command_func.h" rubidium@8750: #include "player_func.h" rubidium@8750: #include "player_base.h" rubidium@8750: #include "player_gui.h" rubidium@5720: #include "network/network.h" tron@2159: #include "variables.h" rubidium@8610: #include "strings_func.h" rubidium@9219: #include "sound_func.h" rubidium@8612: #include "economy_func.h" rubidium@8626: #include "core/alloc_func.hpp" rubidium@8766: #include "settings_type.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: rubidium@9219: extern bool GenerateTowns(); rubidium@9219: static int _scengen_town_size = 1; // depress medium-sized towns per default rubidium@9219: rubidium@8153: enum TownAuthorityWidget { rubidium@8153: TWA_CLOSEBOX = 0, rubidium@8153: TWA_CAPTION, rubidium@8153: TWA_RATING_INFO, rubidium@8153: TWA_COMMAND_LIST, rubidium@8153: TWA_SCROLLBAR, rubidium@8153: TWA_ACTION_INFO, rubidium@8153: TWA_EXECUTE, rubidium@8153: }; rubidium@8153: truelight@0: static const Widget _town_authority_widgets[] = { rubidium@8153: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // TWA_CLOSEBOX rubidium@8153: { WWT_CAPTION, RESIZE_NONE, 13, 11, 316, 0, 13, STR_2022_LOCAL_AUTHORITY, STR_018C_WINDOW_TITLE_DRAG_THIS}, // TWA_CAPTION rubidium@8153: { WWT_PANEL, RESIZE_NONE, 13, 0, 316, 14, 105, 0x0, STR_NULL}, // TWA_RATING_INFO rubidium@8153: { WWT_PANEL, RESIZE_NONE, 13, 0, 304, 106, 157, 0x0, STR_2043_LIST_OF_THINGS_TO_DO_AT}, // TWA_COMMAND_LIST rubidium@8153: { WWT_SCROLLBAR, RESIZE_NONE, 13, 305, 316, 106, 157, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // TWA_SCROLLBAR rubidium@8153: { WWT_PANEL, RESIZE_NONE, 13, 0, 316, 158, 209, 0x0, STR_NULL}, // TWA_ACTION_INFO rubidium@8153: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 316, 210, 221, STR_2042_DO_IT, STR_2044_CARRY_OUT_THE_HIGHLIGHTED}, // TWA_EXECUTE darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: extern const byte _town_action_costs[8]; truelight@0: rubidium@8153: enum TownActions { rubidium@8153: TACT_NONE = 0x00, rubidium@8153: rubidium@8153: TACT_ADVERTISE_SMALL = 0x01, rubidium@8153: TACT_ADVERTISE_MEDIUM = 0x02, rubidium@8153: TACT_ADVERTISE_LARGE = 0x04, rubidium@8153: TACT_ROAD_REBUILD = 0x08, rubidium@8153: TACT_BUILD_STATUE = 0x10, rubidium@8153: TACT_FOUND_BUILDINGS = 0x20, rubidium@8153: TACT_BUY_RIGHTS = 0x40, rubidium@8153: TACT_BRIBE = 0x80, rubidium@8153: rubidium@8153: TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, rubidium@8153: TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS, rubidium@8153: TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE, rubidium@8153: TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS, rubidium@8153: }; rubidium@8153: rubidium@8153: DECLARE_ENUM_AS_BIT_SET(TownActions); rubidium@8153: Darkvater@1793: /** Get a list of available actions to do at a town. belugas@6918: * @param nump if not NULL add put the number of available actions in it Darkvater@1793: * @param pid the player that is querying the town belugas@6918: * @param t the town that is queried Darkvater@1793: * @return bitmasked value of enabled actions Darkvater@1793: */ Darkvater@1793: uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t) truelight@0: { Darkvater@1793: int num = 0; rubidium@8153: TownActions buttons = TACT_NONE; tron@2639: rubidium@8153: /* Spectators and unwanted have no options */ rubidium@8153: if (pid != PLAYER_SPECTATOR && !(_patches.bribe && t->unwanted[pid])) { truelight@0: belugas@6918: /* Things worth more than this are not shown */ rubidium@8153: Money avail = GetPlayer(pid)->player_money + _price.station_value * 200; rubidium@8153: Money ref = _price.build_industry >> 8; truelight@0: rubidium@8153: /* Check the action bits for validity and rubidium@8153: * if they are valid add them */ rubidium@8153: for (uint i = 0; i != lengthof(_town_action_costs); i++) { rubidium@8153: const TownActions cur = (TownActions)(1 << i); rubidium@8153: rubidium@8153: /* Is the player not able to bribe ? */ rubidium@8153: if (cur == TACT_BRIBE && (!_patches.bribe || t->ratings[pid] >= RATING_BRIBE_MAXIMUM)) rubidium@8153: continue; rubidium@8153: rubidium@8153: /* Is the player not able to buy exclusive rights ? */ rubidium@8153: if (cur == TACT_BUY_RIGHTS && !_patches.exclusive_rights) rubidium@8153: continue; rubidium@8153: rubidium@8153: /* Is the player not able to build a statue ? */ skidd13@8424: if (cur == TACT_BUILD_STATUE && HasBit(t->statues, pid)) rubidium@8153: continue; rubidium@8153: rubidium@8153: if (avail >= _town_action_costs[i] * ref) { rubidium@8153: buttons |= cur; truelight@0: num++; truelight@0: } truelight@0: } truelight@0: } truelight@0: Darkvater@1793: if (nump != NULL) *nump = num; truelight@0: return buttons; truelight@0: } truelight@0: skidd13@8478: /** skidd13@8478: * Get the position of the Nth set bit. skidd13@8478: * skidd13@8478: * If there is no Nth bit set return -1 skidd13@8478: * skidd13@8478: * @param bits The value to search in skidd13@8478: * @param n The Nth set bit from which we want to know the position skidd13@8478: * @return The position of the Nth set bit skidd13@8478: */ truelight@0: static int GetNthSetBit(uint32 bits, int n) truelight@0: { truelight@0: if (n >= 0) { skidd13@8501: uint i; skidd13@8501: FOR_EACH_SET_BIT(i, bits) { skidd13@8501: n--; skidd13@8478: if (n < 0) return i; skidd13@8478: } truelight@0: } truelight@0: return -1; truelight@0: } truelight@0: truelight@0: static void TownAuthorityWndProc(Window *w, WindowEvent *e) truelight@0: { Darkvater@1793: switch (e->event) { belugas@10173: case WE_PAINT: { belugas@10173: const Town *t = GetTown(w->window_number); belugas@10173: int numact; belugas@10173: uint buttons = GetMaskOfTownActions(&numact, _local_player, t); dominik@122: belugas@10173: SetVScrollCount(w, numact + 1); truelight@0: rubidium@10229: if (WP(w, def_d).data_1 != -1 && !HasBit(buttons, WP(w, def_d).data_1)) belugas@10173: WP(w, def_d).data_1 = -1; truelight@0: belugas@10173: w->SetWidgetDisabledState(6, WP(w, def_d).data_1 == -1); belugas@10173: belugas@10173: { belugas@10173: int y; belugas@10173: const Player *p; belugas@10173: int r; belugas@10173: StringID str; belugas@10173: belugas@10173: SetDParam(0, w->window_number); belugas@10173: DrawWindowWidgets(w); belugas@10173: belugas@10173: DrawString(2, 15, STR_2023_TRANSPORT_COMPANY_RATINGS, TC_FROMSTRING); belugas@10173: belugas@10173: /* Draw list of players */ belugas@10173: y = 25; belugas@10173: FOR_ALL_PLAYERS(p) { belugas@10173: if (p->is_active && (HasBit(t->have_ratings, p->index) || t->exclusivity == p->index)) { belugas@10173: DrawPlayerIcon(p->index, 2, y); belugas@10173: belugas@10173: SetDParam(0, p->index); belugas@10173: SetDParam(1, p->index); belugas@10173: belugas@10173: r = t->ratings[p->index]; belugas@10173: (str = STR_3035_APPALLING, r <= RATING_APPALLING) || // Apalling belugas@10173: (str++, r <= RATING_VERYPOOR) || // Very Poor belugas@10173: (str++, r <= RATING_POOR) || // Poor belugas@10173: (str++, r <= RATING_MEDIOCRE) || // Mediocore belugas@10173: (str++, r <= RATING_GOOD) || // Good belugas@10173: (str++, r <= RATING_VERYGOOD) || // Very Good belugas@10173: (str++, r <= RATING_EXCELLENT) || // Excellent belugas@10173: (str++, true); // Outstanding belugas@10173: belugas@10173: SetDParam(2, str); belugas@10173: if (t->exclusivity == p->index) { // red icon for player with exclusive rights belugas@10173: DrawSprite(SPR_BLOT, PALETTE_TO_RED, 18, y); belugas@10173: } belugas@10173: belugas@10173: DrawString(28, y, STR_2024, TC_FROMSTRING); belugas@10173: y += 10; peter1138@5919: } truelight@0: } truelight@0: } truelight@0: belugas@10173: /* Draw actions list */ belugas@10173: { belugas@10173: int y = 107, i; belugas@10173: int pos = w->vscroll.pos; truelight@0: belugas@10173: if (--pos < 0) { belugas@10173: DrawString(2, y, STR_2045_ACTIONS_AVAILABLE, TC_FROMSTRING); truelight@0: y += 10; truelight@0: } belugas@10173: belugas@10173: for (i = 0; buttons; i++, buttons >>= 1) { belugas@10173: if (pos <= -5) break; ///< Draw only the 5 fitting lines belugas@10173: belugas@10173: if ((buttons & 1) && --pos < 0) { belugas@10173: DrawString(3, y, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i, TC_ORANGE); belugas@10173: y += 10; belugas@10173: } belugas@10173: } truelight@0: } truelight@0: belugas@10173: { belugas@10173: int i = WP(w, def_d).data_1; truelight@0: belugas@10173: if (i != -1) { belugas@10173: SetDParam(1, (_price.build_industry >> 8) * _town_action_costs[i]); belugas@10173: SetDParam(0, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i); belugas@10173: DrawStringMultiLine(2, 159, STR_204D_INITIATE_A_SMALL_LOCAL + i, 313); belugas@9074: } truelight@0: } truelight@0: belugas@10173: } break; truelight@0: belugas@10173: case WE_DOUBLE_CLICK: belugas@10173: case WE_CLICK: belugas@10173: switch (e->we.click.widget) { belugas@10173: case TWA_COMMAND_LIST: { belugas@10173: const Town *t = GetTown(w->window_number); belugas@10173: int y = (e->we.click.pt.y - 0x6B) / 10; belugas@10173: belugas@10173: if (!IsInsideMM(y, 0, 5)) return; belugas@10173: belugas@10173: y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_player, t), y + w->vscroll.pos - 1); belugas@10173: if (y >= 0) { belugas@10173: WP(w, def_d).data_1 = y; belugas@10173: SetWindowDirty(w); belugas@10173: } belugas@10173: /* Fall through to clicking in case we are double-clicked */ belugas@10173: if (e->event != WE_DOUBLE_CLICK || y < 0) break; belugas@10173: } belugas@10173: belugas@10173: case TWA_EXECUTE: belugas@10173: DoCommandP(GetTown(w->window_number)->xy, w->window_number, WP(w, def_d).data_1, NULL, CMD_DO_TOWN_ACTION | CMD_MSG(STR_00B4_CAN_T_DO_THIS)); belugas@10173: break; belugas@10173: } belugas@10173: break; belugas@10173: belugas@10173: case WE_4: belugas@10173: SetWindowDirty(w); belugas@10173: break; truelight@0: } truelight@0: } truelight@0: truelight@0: static const WindowDesc _town_authority_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 317, 222, 317, 222, rubidium@6144: WC_TOWN_AUTHORITY, WC_NONE, truelight@0: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, truelight@0: _town_authority_widgets, truelight@0: TownAuthorityWndProc truelight@0: }; truelight@0: tron@410: static void ShowTownAuthorityWindow(uint town) truelight@0: { belugas@4171: Window *w = AllocateWindowDescFront(&_town_authority_desc, town); truelight@0: tron@2639: if (w != NULL) { truelight@0: w->vscroll.cap = 5; rubidium@8578: WP(w, def_d).data_1 = -1; truelight@0: } truelight@0: } truelight@0: belugas@10174: belugas@10174: enum TownViewWidget { belugas@10174: TVW_CAPTION = 1, belugas@10201: TVW_STICKY, belugas@10174: TVW_CENTERVIEW = 6, belugas@10174: TVW_SHOWAUTORITY, belugas@10174: TVW_CHANGENAME, belugas@10174: TVW_EXPAND, belugas@10174: TVW_DELETE, belugas@10174: }; belugas@10174: truelight@0: static void TownViewWndProc(Window *w, WindowEvent *e) truelight@0: { truelight@919: Town *t = GetTown(w->window_number); truelight@0: tron@2639: switch (e->event) { belugas@10201: case WE_CREATE: { belugas@10201: bool ingame = _game_mode != GM_EDITOR; belugas@10174: if (t->larger_town) w->widget[TVW_CAPTION].data = STR_CITY; belugas@10201: w->SetWidgetHiddenState(TVW_DELETE, ingame); // hide delete button on game mode belugas@10201: w->SetWidgetHiddenState(TVW_EXPAND, ingame); // hide expand button on game mode belugas@10201: w->SetWidgetHiddenState(TVW_SHOWAUTORITY, !ingame); // hide autority button on editor mode belugas@10201: belugas@10201: if (ingame) { belugas@10201: /* resize caption bar */ belugas@10201: w->widget[TVW_CAPTION].right = w->widget[TVW_STICKY].left -1; belugas@10201: /* move the rename from top on scenario to bottom in game */ belugas@10201: w->widget[TVW_CHANGENAME].top = w->widget[TVW_EXPAND].top; belugas@10201: w->widget[TVW_CHANGENAME].bottom = w->widget[TVW_EXPAND].bottom; belugas@10201: w->widget[TVW_CHANGENAME].right = w->widget[TVW_STICKY].right; belugas@10201: } belugas@10201: } break; truelight@0: belugas@10173: case WE_PAINT: belugas@10173: /* disable renaming town in network games if you are not the server */ belugas@10174: w->SetWidgetDisabledState(TVW_CHANGENAME, _networking && !_network_server); tron@2639: belugas@10173: SetDParam(0, t->index); belugas@10173: DrawWindowWidgets(w); tron@2639: belugas@10173: SetDParam(0, t->population); belugas@10173: SetDParam(1, t->num_houses); belugas@10173: DrawString(2, 107, STR_2006_POPULATION, TC_FROMSTRING); truelight@0: belugas@10173: SetDParam(0, t->act_pass); belugas@10173: SetDParam(1, t->max_pass); belugas@10173: DrawString(2, 117, STR_200D_PASSENGERS_LAST_MONTH_MAX, TC_FROMSTRING); belugas@10173: belugas@10173: SetDParam(0, t->act_mail); belugas@10173: SetDParam(1, t->max_mail); belugas@10173: DrawString(2, 127, STR_200E_MAIL_LAST_MONTH_MAX, TC_FROMSTRING); belugas@10173: belugas@10173: DrawWindowViewport(w); belugas@10173: break; belugas@10173: belugas@10173: case WE_CLICK: belugas@10173: switch (e->we.click.widget) { belugas@10174: case TVW_CENTERVIEW: /* scroll to location */ belugas@10173: ScrollMainWindowToTile(t->xy); belugas@10173: break; belugas@10173: belugas@10174: case TVW_SHOWAUTORITY: /* town authority */ belugas@10173: ShowTownAuthorityWindow(w->window_number); belugas@10173: break; belugas@10173: belugas@10174: case TVW_CHANGENAME: /* rename */ belugas@10173: SetDParam(0, w->window_number); belugas@10173: ShowQueryString(STR_TOWN, STR_2007_RENAME_TOWN, 31, 130, w, CS_ALPHANUMERAL); belugas@10173: break; belugas@10173: belugas@10174: case TVW_EXPAND: /* expand town - only available on Scenario editor */ belugas@10173: ExpandTown(t); belugas@10173: break; belugas@10173: belugas@10174: case TVW_DELETE: /* delete town - only available on Scenario editor */ belugas@10173: delete t; belugas@10173: break; belugas@10173: } break; belugas@10173: belugas@10173: case WE_ON_EDIT_TEXT: belugas@10173: if (e->we.edittext.str[0] != '\0') { belugas@10173: _cmd_text = e->we.edittext.str; belugas@10173: DoCommandP(0, w->window_number, 0, NULL, belugas@10173: CMD_RENAME_TOWN | CMD_MSG(STR_2008_CAN_T_RENAME_TOWN)); belugas@10173: } belugas@10173: break; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static const Widget _town_view_widgets[] = { rubidium@4344: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, belugas@10201: { WWT_CAPTION, RESIZE_NONE, 13, 11, 172, 0, 13, STR_2005, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@4344: { WWT_STICKYBOX, RESIZE_NONE, 13, 248, 259, 0, 13, 0x0, STR_STICKY_BUTTON}, Darkvater@4938: { WWT_PANEL, RESIZE_NONE, 13, 0, 259, 14, 105, 0x0, STR_NULL}, Darkvater@4939: { WWT_INSET, RESIZE_NONE, 13, 2, 257, 16, 103, 0x0, STR_NULL}, Darkvater@4938: { WWT_PANEL, RESIZE_NONE, 13, 0, 259, 106, 137, 0x0, STR_NULL}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 85, 138, 149, STR_00E4_LOCATION, STR_200B_CENTER_THE_MAIN_VIEW_ON}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 86, 171, 138, 149, STR_2020_LOCAL_AUTHORITY, STR_2021_SHOW_INFORMATION_ON_LOCAL}, belugas@10201: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 172, 247, 0, 13, STR_0130_RENAME, STR_200C_CHANGE_TOWN_NAME}, belugas@10201: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 86, 171, 138, 149, STR_023C_EXPAND, STR_023B_INCREASE_SIZE_OF_TOWN}, belugas@10201: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 172, 259, 138, 149, STR_0290_DELETE, STR_0291_DELETE_THIS_TOWN_COMPLETELY}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: static const WindowDesc _town_view_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 260, 150, 260, 150, rubidium@6144: WC_TOWN_VIEW, WC_NONE, darkvater@758: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON, truelight@0: _town_view_widgets, truelight@0: TownViewWndProc truelight@0: }; truelight@0: Darkvater@3349: void ShowTownViewWindow(TownID town) truelight@0: { truelight@0: Window *w; truelight@0: belugas@10201: w = AllocateWindowDescFront(&_town_view_desc, town); truelight@0: tron@2639: if (w != NULL) { truelight@0: w->flags4 |= WF_DISABLE_VP_SCROLL; rubidium@10257: InitializeWindowViewport(w, 3, 17, 254, 86, GetTown(town)->xy, ZOOM_LVL_TOWN); truelight@0: } truelight@0: } truelight@0: belugas@9074: enum TownDirectoryWidget { belugas@9074: TDW_SORTNAME = 3, belugas@9074: TDW_SORTPOPULATION, belugas@9074: TDW_CENTERTOWN, belugas@9074: }; truelight@0: static const Widget _town_directory_widgets[] = { rubidium@4344: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, rubidium@4344: { WWT_CAPTION, RESIZE_NONE, 13, 11, 195, 0, 13, STR_2000_TOWNS, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@4344: { WWT_STICKYBOX, RESIZE_NONE, 13, 196, 207, 0, 13, 0x0, STR_STICKY_BUTTON}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 98, 14, 25, STR_SORT_BY_NAME, STR_SORT_ORDER_TIP}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 99, 195, 14, 25, STR_SORT_BY_POPULATION, STR_SORT_ORDER_TIP}, Darkvater@4938: { WWT_PANEL, RESIZE_BOTTOM, 13, 0, 195, 26, 189, 0x0, STR_200A_TOWN_NAMES_CLICK_ON_NAME}, rubidium@4344: { WWT_SCROLLBAR, RESIZE_BOTTOM, 13, 196, 207, 14, 189, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, Darkvater@4938: { WWT_PANEL, RESIZE_TB, 13, 0, 195, 190, 201, 0x0, STR_NULL}, rubidium@4344: { WWT_RESIZEBOX, RESIZE_TB, 13, 196, 207, 190, 201, 0x0, STR_RESIZE_BUTTON}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: belugas@6918: /* used to get a sorted list of the towns */ truelight@0: static uint _num_town_sort; truelight@0: truelight@0: static char _bufcache[64]; tron@4277: static const Town* _last_town; truelight@0: darkvater@164: static int CDECL TownNameSorter(const void *a, const void *b) truelight@0: { tron@4277: const Town* ta = *(const Town**)a; tron@4277: const Town* tb = *(const Town**)b; truelight@0: char buf1[64]; truelight@0: int r; truelight@0: Darkvater@4416: SetDParam(0, ta->index); Darkvater@4912: GetString(buf1, STR_TOWN, lastof(buf1)); truelight@0: truelight@1260: /* If 'b' is the same town as in the last round, use the cached value truelight@1260: * We do this to speed stuff up ('b' is called with the same value a lot of tron@4277: * times after eachother) */ tron@4277: if (tb != _last_town) { tron@4277: _last_town = tb; Darkvater@4416: SetDParam(0, tb->index); Darkvater@4912: GetString(_bufcache, STR_TOWN, lastof(_bufcache)); truelight@0: } truelight@0: truelight@0: r = strcmp(buf1, _bufcache); truelight@0: if (_town_sort_order & 1) r = -r; truelight@0: return r; truelight@0: } truelight@0: darkvater@164: static int CDECL TownPopSorter(const void *a, const void *b) truelight@0: { tron@4277: const Town* ta = *(const Town**)a; tron@4277: const Town* tb = *(const Town**)b; darkvater@164: int r = ta->population - tb->population; truelight@0: if (_town_sort_order & 1) r = -r; truelight@0: return r; truelight@0: } truelight@0: rubidium@6573: static void MakeSortedTownList() truelight@0: { tron@2630: const Town* t; tron@2639: uint n = 0; truelight@919: truelight@919: /* Create array for sorting */ KUDr@5860: _town_sort = ReallocT(_town_sort, GetMaxTownIndex() + 1); truelight@919: truelight@4346: FOR_ALL_TOWNS(t) _town_sort[n++] = t; truelight@821: truelight@0: _num_town_sort = n; truelight@0: tron@4277: _last_town = NULL; // used for "cache" tron@4277: qsort((void*)_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter); darkvater@65: Darkvater@5568: DEBUG(misc, 3, "Resorting towns list"); truelight@0: } truelight@0: truelight@0: truelight@0: static void TownDirectoryWndProc(Window *w, WindowEvent *e) truelight@0: { tron@2639: switch (e->event) { belugas@10173: case WE_PAINT: { belugas@10173: if (_town_sort_dirty) { belugas@10173: _town_sort_dirty = false; belugas@10173: MakeSortedTownList(); darkvater@174: } truelight@0: belugas@10173: SetVScrollCount(w, _num_town_sort); belugas@9074: belugas@10173: DrawWindowWidgets(w); belugas@10173: DrawSortButtonState(w, (_town_sort_order <= 1) ? TDW_SORTNAME : TDW_SORTPOPULATION, _town_sort_order & 1 ? SBS_DOWN : SBS_UP); belugas@9074: belugas@10173: { belugas@10173: int n = 0; belugas@10173: uint16 i = w->vscroll.pos; belugas@10173: int y = 28; truelight@193: belugas@10173: while (i < _num_town_sort) { belugas@10173: const Town* t = _town_sort[i]; truelight@867: belugas@10173: assert(t->xy); belugas@10173: belugas@10173: SetDParam(0, t->index); belugas@10173: SetDParam(1, t->population); belugas@10173: DrawString(2, y, STR_2057, TC_FROMSTRING); belugas@10173: belugas@10173: y += 10; belugas@10173: i++; belugas@10173: if (++n == w->vscroll.cap) break; // max number of towns in 1 window belugas@10173: } belugas@10173: SetDParam(0, GetWorldPopulation()); belugas@10173: DrawString(3, w->height - 12 + 2, STR_TOWN_POPULATION, TC_FROMSTRING); belugas@10173: } belugas@10173: } break; belugas@10173: belugas@10173: case WE_CLICK: belugas@10173: switch (e->we.click.widget) { belugas@10173: case TDW_SORTNAME: /* Sort by Name ascending/descending */ belugas@10173: _town_sort_order = (_town_sort_order == 0) ? 1 : 0; belugas@10173: _town_sort_dirty = true; belugas@10173: SetWindowDirty(w); belugas@10173: break; belugas@10173: belugas@10173: case TDW_SORTPOPULATION: /* Sort by Population ascending/descending */ belugas@10173: _town_sort_order = (_town_sort_order == 2) ? 3 : 2; belugas@10173: _town_sort_dirty = true; belugas@10173: SetWindowDirty(w); belugas@10173: break; belugas@10173: belugas@10173: case TDW_CENTERTOWN: { /* Click on Town Matrix */ belugas@10173: const Town* t; belugas@10173: belugas@10173: uint16 id_v = (e->we.click.pt.y - 28) / 10; belugas@10173: belugas@10173: if (id_v >= w->vscroll.cap) return; // click out of bounds belugas@10173: belugas@10173: id_v += w->vscroll.pos; belugas@10173: belugas@10173: if (id_v >= _num_town_sort) return; // click out of town bounds belugas@10173: belugas@10173: t = _town_sort[id_v]; belugas@10173: assert(t->xy); belugas@10173: ScrollMainWindowToTile(t->xy); belugas@10173: } break; belugas@10173: } belugas@10173: break; belugas@10173: belugas@10173: case WE_4: belugas@10173: SetWindowDirty(w); belugas@10173: break; belugas@10173: belugas@10173: case WE_RESIZE: belugas@10173: w->vscroll.cap += e->we.sizing.diff.y / 10; belugas@10173: break; truelight@193: } truelight@0: } truelight@0: truelight@0: static const WindowDesc _town_directory_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 208, 202, 208, 202, rubidium@6144: WC_TOWN_DIRECTORY, WC_NONE, truelight@867: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, truelight@0: _town_directory_widgets, truelight@0: TownDirectoryWndProc truelight@0: }; truelight@0: truelight@0: rubidium@6573: void ShowTownDirectory() truelight@0: { belugas@4171: Window *w = AllocateWindowDescFront(&_town_directory_desc, 0); truelight@0: tron@2639: if (w != NULL) { truelight@0: w->vscroll.cap = 16; truelight@867: w->resize.step_height = 10; celestar@1080: w->resize.height = w->height - 10 * 6; // minimum of 10 items in the list, each item 10 high truelight@0: } truelight@0: } rubidium@9219: rubidium@9219: void CcBuildTown(bool success, TileIndex tile, uint32 p1, uint32 p2) rubidium@9219: { rubidium@9219: if (success) { rubidium@9219: SndPlayTileFx(SND_1F_SPLAT, tile); rubidium@9219: ResetObjectToPlace(); rubidium@9219: } rubidium@9219: } rubidium@9219: rubidium@9219: static void PlaceProc_Town(TileIndex tile) rubidium@9219: { rubidium@9219: uint32 size = min(_scengen_town_size, (int)TSM_CITY); rubidium@9219: uint32 mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED; rubidium@9219: DoCommandP(tile, size, mode, CcBuildTown, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE)); rubidium@9219: } rubidium@9219: belugas@10174: enum TownScenarioEditorWidget { belugas@10174: TSEW_NEWTOWN = 4, belugas@10174: TSEW_RANDOMTOWN, belugas@10174: TSEW_MANYRANDOMTOWNS, belugas@10174: TSEW_SMALLTOWN, belugas@10174: TSEW_MEDIUMTOWN, belugas@10174: TSEW_LARGETOWN, belugas@10174: TSEW_CITY, belugas@10174: }; belugas@10174: rubidium@9219: static const Widget _scen_edit_town_gen_widgets[] = { rubidium@9219: { WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, rubidium@9219: { WWT_CAPTION, RESIZE_NONE, 7, 11, 147, 0, 13, STR_0233_TOWN_GENERATION, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@9219: { WWT_STICKYBOX, RESIZE_NONE, 7, 148, 159, 0, 13, 0x0, STR_STICKY_BUTTON}, rubidium@9219: { WWT_PANEL, RESIZE_NONE, 7, 0, 159, 14, 94, 0x0, STR_NULL}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 16, 27, STR_0234_NEW_TOWN, STR_0235_CONSTRUCT_NEW_TOWN}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 29, 40, STR_023D_RANDOM_TOWN, STR_023E_BUILD_TOWN_IN_RANDOM_LOCATION}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 42, 53, STR_MANY_RANDOM_TOWNS, STR_RANDOM_TOWNS_TIP}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 53, 68, 79, STR_02A1_SMALL, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 54, 105, 68, 79, STR_02A2_MEDIUM, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 106, 157, 68, 79, STR_02A3_LARGE, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 81, 92, STR_SCENARIO_EDITOR_CITY, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_LABEL, RESIZE_NONE, 7, 0, 147, 54, 67, STR_02A5_TOWN_SIZE, STR_NULL}, rubidium@9219: { WIDGETS_END}, rubidium@9219: }; rubidium@9219: rubidium@9219: static void ScenEditTownGenWndProc(Window *w, WindowEvent *e) rubidium@9219: { rubidium@9219: switch (e->event) { belugas@10173: case WE_PAINT: belugas@10173: DrawWindowWidgets(w); rubidium@9219: break; rubidium@9219: belugas@10173: case WE_CREATE: belugas@10174: w->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN); rubidium@9219: break; rubidium@9219: belugas@10173: case WE_CLICK: belugas@10173: switch (e->we.click.widget) { belugas@10174: case TSEW_NEWTOWN: belugas@10174: HandlePlacePushButton(w, TSEW_NEWTOWN, SPR_CURSOR_TOWN, VHM_RECT, PlaceProc_Town); belugas@10173: break; belugas@10173: belugas@10174: case TSEW_RANDOMTOWN: { belugas@10173: Town *t; belugas@10173: uint size = min(_scengen_town_size, (int)TSM_CITY); belugas@10173: TownSizeMode mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED; belugas@10173: belugas@10174: w->HandleButtonClick(TSEW_RANDOMTOWN); belugas@10173: _generating_world = true; belugas@10173: t = CreateRandomTown(20, mode, size); belugas@10173: _generating_world = false; belugas@10173: belugas@10173: if (t == NULL) { belugas@10173: ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0); belugas@10173: } else { belugas@10173: ScrollMainWindowToTile(t->xy); belugas@10173: } belugas@10173: } break; belugas@10173: belugas@10174: case TSEW_MANYRANDOMTOWNS: belugas@10174: w->HandleButtonClick(TSEW_MANYRANDOMTOWNS); belugas@10173: belugas@10173: _generating_world = true; belugas@10173: if (!GenerateTowns()) ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0); belugas@10173: _generating_world = false; belugas@10173: break; belugas@10173: belugas@10174: case TSEW_SMALLTOWN: case TSEW_MEDIUMTOWN: case TSEW_LARGETOWN: case TSEW_CITY: belugas@10174: w->RaiseWidget(_scengen_town_size + TSEW_SMALLTOWN); belugas@10174: _scengen_town_size = e->we.click.widget - TSEW_SMALLTOWN; belugas@10174: w->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN); belugas@10173: SetWindowDirty(w); belugas@10173: break; belugas@10173: } break; belugas@10173: belugas@10173: case WE_TIMEOUT: belugas@10174: w->RaiseWidget(TSEW_RANDOMTOWN); belugas@10174: w->RaiseWidget(TSEW_MANYRANDOMTOWNS); belugas@10173: SetWindowDirty(w); rubidium@9219: break; rubidium@9219: belugas@10173: case WE_PLACE_OBJ: belugas@10173: _place_proc(e->we.place.tile); belugas@10173: break; belugas@10173: belugas@10173: case WE_ABORT_PLACE_OBJ: belugas@10173: w->RaiseButtons(); belugas@10174: w->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN); rubidium@9219: SetWindowDirty(w); rubidium@9219: break; rubidium@9219: } rubidium@9219: } rubidium@9219: rubidium@9219: static const WindowDesc _scen_edit_town_gen_desc = { rubidium@9219: WDP_AUTO, WDP_AUTO, 160, 95, 160, 95, rubidium@9219: WC_SCEN_TOWN_GEN, WC_NONE, rubidium@9219: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON, rubidium@9219: _scen_edit_town_gen_widgets, rubidium@9219: ScenEditTownGenWndProc, rubidium@9219: }; rubidium@9219: rubidium@9219: void ShowBuildTownWindow() rubidium@9219: { rubidium@9219: if (_game_mode != GM_EDITOR && !IsValidPlayer(_current_player)) return; rubidium@9219: AllocateWindowDescFront(&_scen_edit_town_gen_desc, 0); rubidium@9219: } rubidium@9219: