truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" truelight@0: #include "town.h" truelight@0: #include "window.h" truelight@0: #include "gfx.h" truelight@0: #include "viewport.h" truelight@0: #include "gui.h" truelight@0: #include "command.h" truelight@0: #include "player.h" truelight@0: truelight@0: static const Widget _town_authority_widgets[] = { darkvater@176: { WWT_TEXTBTN, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, truelight@0: { WWT_CAPTION, 13, 11, 316, 0, 13, STR_2022_LOCAL_AUTHORITY, STR_018C_WINDOW_TITLE_DRAG_THIS}, darkvater@176: { WWT_IMGBTN, 13, 0, 316, 14, 105, 0x0, STR_NULL}, darkvater@176: { WWT_IMGBTN, 13, 0, 305, 106, 157, 0x0, STR_2043_LIST_OF_THINGS_TO_DO_AT}, darkvater@176: { WWT_SCROLLBAR, 13, 306, 316, 106, 157, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, darkvater@176: { WWT_IMGBTN, 13, 0, 316, 158, 209, 0x0, STR_NULL}, darkvater@176: { WWT_PUSHTXTBTN, 13, 0, 316, 210, 221, STR_2042_DO_IT, STR_2044_CARRY_OUT_THE_HIGHLIGHTED}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: extern const byte _town_action_costs[8]; truelight@0: extern void DrawPlayerIcon(int p, int x, int y); truelight@0: truelight@0: static uint GetMaskOfTownActions(int *nump, Town *t) truelight@0: { truelight@0: int32 avail, ref; truelight@0: int i, num; truelight@0: uint avail_buttons = 0x7F; // by default all buttons except bribe are enabled. truelight@0: uint buttons; truelight@0: darkvater@1: if (_local_player != OWNER_SPECTATOR) { truelight@0: // bribe option enabled? truelight@0: if (_patches.bribe) { truelight@0: // if unwanted, disable everything. truelight@0: if (t->unwanted[_local_player]) { truelight@0: avail_buttons = 0; truelight@0: } else if (t->ratings[_local_player] < 600) truelight@0: avail_buttons |= (1 << 7); // only bribe if less than excellent truelight@0: } truelight@0: truelight@0: // Things worth more than this are not shown truelight@0: avail = DEREF_PLAYER(_local_player)->player_money + _price.station_value * 200; truelight@0: ref = _price.build_industry >> 8; truelight@0: truelight@0: for(i=0,buttons=0,num=0; i != lengthof(_town_action_costs); i++,avail_buttons>>=1) { truelight@0: if (avail_buttons&1 && avail >= _town_action_costs[i] * ref) { truelight@0: SETBIT(buttons, i); truelight@0: num++; truelight@0: } truelight@0: } truelight@0: truelight@0: // Disable build statue if already built truelight@0: if(HASBIT(t->statues, _local_player)) truelight@0: { truelight@0: CLRBIT(buttons, 4); truelight@0: num--; truelight@0: } truelight@0: truelight@0: } else { truelight@0: // no actions available for spectator truelight@0: buttons = 0; truelight@0: num = 0; truelight@0: } truelight@0: truelight@0: if (nump) *nump = num; truelight@0: return buttons; truelight@0: } truelight@0: truelight@0: static int GetNthSetBit(uint32 bits, int n) truelight@0: { truelight@0: int i = 0; truelight@0: if (n >= 0) { truelight@0: do { truelight@0: if (bits&1 && --n < 0) return i; truelight@0: i++; truelight@0: } while (bits>>=1); truelight@0: } truelight@0: return -1; truelight@0: } truelight@0: truelight@0: static void TownAuthorityWndProc(Window *w, WindowEvent *e) truelight@0: { truelight@0: uint buttons; truelight@0: int numact; truelight@0: Town *t = DEREF_TOWN(w->window_number); truelight@193: truelight@0: switch(e->event) { truelight@0: case WE_PAINT: truelight@0: buttons = GetMaskOfTownActions(&numact, t); truelight@0: SetVScrollCount(w, numact + 1); truelight@0: truelight@0: if (WP(w,def_d).data_1 != -1 && !HASBIT(buttons, WP(w,def_d).data_1)) truelight@0: WP(w,def_d).data_1 = -1; truelight@0: truelight@0: w->disabled_state = (WP(w,def_d).data_1 == -1) ? (1 << 6) : 0; truelight@0: truelight@0: { truelight@0: int y; truelight@0: Player *p; truelight@0: int r; truelight@0: StringID str; truelight@0: truelight@0: SET_DPARAM16(0, w->window_number); truelight@0: DrawWindowWidgets(w); truelight@0: truelight@0: DrawString(2, 15, STR_2023_TRANSPORT_COMPANY_RATINGS, 0); dominik@122: truelight@0: // Draw list of players truelight@0: y = 25; truelight@0: FOR_ALL_PLAYERS(p) { dominik@121: if (p->is_active && (HASBIT(t->have_ratings, p->index) || t->exclusivity==p->index)) { dominik@125: DrawPlayerIcon(p->index, 2, y); dominik@121: truelight@0: SET_DPARAM16(0, p->name_1); truelight@0: SET_DPARAM32(1, p->name_2); darkvater@2: SET_DPARAM16(2, GetPlayerNameString(p->index, 3)); truelight@0: truelight@0: r = t->ratings[p->index]; truelight@0: (str = STR_3035_APPALLING, r <= -400) || // Apalling truelight@0: (str++, r <= -200) || // Very Poor truelight@0: (str++, r <= 0) || // Poor truelight@0: (str++, r <= 200) || // Mediocore truelight@0: (str++, r <= 400) || // Good truelight@0: (str++, r <= 600) || // Very Good truelight@0: (str++, r <= 800) || // Excellent truelight@0: (str++, true); // Outstanding truelight@0: darkvater@16: /* WARNING ugly hack! darkvater@16: GetPlayerNameString sets up (Player #) if the player is human in an extra DPARAM16 darkvater@16: It seems that if player is non-human, nothing is set up, so param is 0. GetString doesn't like truelight@193: that because there is another param after it. darkvater@16: So we'll just shift the rating one back if player is AI and all is fine darkvater@16: */ darkvater@16: SET_DPARAM16((IS_HUMAN_PLAYER(p->index) ? 4 : 3), str); darkvater@246: if (t->exclusivity == p->index) // red icon for player with exclusive rights darkvater@246: DrawSprite((SPR_OPENTTD_BASE + 10) | 0x30b8000, 18, y); darkvater@246: darkvater@246: DrawString(28, y, STR_2024, 0); truelight@0: y+=10; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: // Draw actions list truelight@0: { truelight@0: int y = 107, i; truelight@0: int pos = w->vscroll.pos; truelight@0: truelight@0: if (--pos < 0) { truelight@0: DrawString(2, y, STR_2045_ACTIONS_AVAILABLE, 0); truelight@0: y+=10; truelight@0: } truelight@0: for(i=0; buttons; i++,buttons>>=1) { truelight@0: if (pos <= -5) truelight@0: break; truelight@0: truelight@0: if (buttons&1 && --pos < 0) { truelight@0: DrawString(3, y, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i, 6); truelight@0: y += 10; truelight@0: } truelight@0: } truelight@0: } dominik@122: truelight@0: { truelight@0: int i; truelight@0: if ((i=WP(w,def_d).data_1) != -1) { truelight@0: SET_DPARAM32(1, (_price.build_industry >> 8) * _town_action_costs[i]); truelight@0: SET_DPARAM16(0, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i); truelight@0: DrawStringMultiLine(2, 159, STR_204D_INITIATE_A_SMALL_LOCAL + i, 313); truelight@0: } truelight@0: } truelight@193: truelight@0: break; truelight@0: truelight@0: case WE_CLICK: truelight@0: switch(e->click.widget) { truelight@0: case 3: { /* listbox */ truelight@0: int y = (e->click.pt.y - 0x6B) / 10; truelight@0: if (!IS_INT_INSIDE(y, 0, 5)) truelight@0: return; truelight@0: truelight@0: y = GetNthSetBit(GetMaskOfTownActions(NULL, t), y + w->vscroll.pos - 1); truelight@0: if (y >= 0) { truelight@0: WP(w,def_d).data_1 = y; truelight@0: SetWindowDirty(w); truelight@0: } truelight@0: break; truelight@0: } truelight@0: truelight@0: case 6: { /* carry out the action */ truelight@0: DoCommandP(t->xy, w->window_number, WP(w,def_d).data_1, NULL, CMD_DO_TOWN_ACTION | CMD_MSG(STR_2054_CAN_T_DO_THIS)); truelight@0: break; truelight@0: } truelight@0: } truelight@0: break; truelight@0: truelight@0: case WE_4: truelight@0: SetWindowDirty(w); truelight@0: break; truelight@0: } truelight@0: } truelight@0: truelight@0: static const WindowDesc _town_authority_desc = { truelight@0: -1, -1, 317, 222, truelight@0: WC_TOWN_AUTHORITY,0, 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: { truelight@0: Window *w; truelight@0: truelight@0: w = AllocateWindowDescFront(&_town_authority_desc, town); truelight@0: if (w) { truelight@0: w->vscroll.cap = 5; truelight@0: WP(w,def_d).data_1 = -1; truelight@0: } truelight@0: } truelight@0: truelight@0: static void TownViewWndProc(Window *w, WindowEvent *e) truelight@0: { truelight@0: Town *t = DEREF_TOWN(w->window_number); truelight@0: truelight@0: switch(e->event) { truelight@0: case WE_PAINT: truelight@0: SET_DPARAM16(0, t->index); truelight@0: DrawWindowWidgets(w); truelight@193: truelight@0: SET_DPARAM32(0, t->population); truelight@0: SET_DPARAM32(1, t->num_houses); truelight@0: DrawString(2,107,STR_2006_POPULATION,0); truelight@193: truelight@0: SET_DPARAM16(0, t->act_pass); truelight@0: SET_DPARAM16(1, t->max_pass); truelight@0: DrawString(2,117,STR_200D_PASSENGERS_LAST_MONTH_MAX,0); truelight@0: truelight@0: SET_DPARAM16(0, t->act_mail); truelight@0: SET_DPARAM16(1, t->max_mail); truelight@0: DrawString(2,127,STR_200E_MAIL_LAST_MONTH_MAX,0); truelight@193: truelight@0: DrawWindowViewport(w); truelight@0: break; truelight@0: truelight@0: case WE_CLICK: truelight@0: switch(e->click.widget) { truelight@0: case 5: /* scroll to location */ truelight@0: ScrollMainWindowToTile(t->xy); truelight@0: break; truelight@0: case 6: /* town authority */ truelight@0: ShowTownAuthorityWindow(w->window_number); truelight@0: break; truelight@0: case 7: /* rename */ truelight@0: SET_DPARAM32(0, t->townnameparts); truelight@0: ShowQueryString(t->townnametype, STR_2007_RENAME_TOWN, 31, 130, w->window_class, w->window_number); truelight@0: break; truelight@0: case 8: /* expand town */ truelight@0: ExpandTown(t); truelight@0: break; truelight@0: case 9: /* delete town */ truelight@0: DeleteTown(t); truelight@0: break; truelight@0: } truelight@0: break; truelight@0: truelight@0: case WE_ON_EDIT_TEXT: { truelight@0: byte *b = e->edittext.str; truelight@0: if (*b == 0) truelight@0: return; truelight@0: memcpy(_decode_parameters, b, 32); truelight@0: DoCommandP(0, w->window_number, 0, NULL, CMD_RENAME_TOWN | CMD_MSG(STR_2008_CAN_T_RENAME_TOWN)); truelight@0: } break; truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static const Widget _town_view_widgets[] = { truelight@0: { WWT_TEXTBTN, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, truelight@0: { WWT_CAPTION, 13, 11, 259, 0, 13, STR_2005, STR_018C_WINDOW_TITLE_DRAG_THIS}, darkvater@176: { WWT_IMGBTN, 13, 0, 259, 14, 105, 0x0, STR_NULL}, darkvater@176: { WWT_6, 13, 2, 257, 16, 103, 0x0, STR_NULL}, darkvater@176: { WWT_IMGBTN, 13, 0, 259, 106, 137, 0x0, STR_NULL}, darkvater@176: { WWT_PUSHTXTBTN, 13, 0, 85, 138, 149, STR_00E4_LOCATION, STR_200B_CENTER_THE_MAIN_VIEW_ON}, darkvater@176: { WWT_PUSHTXTBTN, 13, 86, 171, 138, 149, STR_2020_LOCAL_AUTHORITY,STR_2021_SHOW_INFORMATION_ON_LOCAL}, darkvater@176: { WWT_PUSHTXTBTN, 13, 172, 259, 138, 149, STR_0130_RENAME, STR_200C_CHANGE_TOWN_NAME}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: static const WindowDesc _town_view_desc = { truelight@0: -1, -1, 260, 150, truelight@0: WC_TOWN_VIEW,0, truelight@0: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, truelight@0: _town_view_widgets, truelight@0: TownViewWndProc truelight@0: }; truelight@0: truelight@0: static const Widget _town_view_scen_widgets[] = { darkvater@176: { WWT_TEXTBTN, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, darkvater@176: { WWT_CAPTION, 13, 11, 184, 0, 13, STR_2005, STR_018C_WINDOW_TITLE_DRAG_THIS}, darkvater@176: { WWT_IMGBTN, 13, 0, 259, 14, 105, 0x0, STR_NULL}, darkvater@176: { WWT_6, 13, 2, 257, 16, 103, 0x0, STR_NULL}, darkvater@176: { WWT_IMGBTN, 13, 0, 259, 106, 137, 0x0, STR_NULL}, darkvater@176: { WWT_PUSHTXTBTN, 13, 0, 85, 138, 149, STR_00E4_LOCATION, STR_200B_CENTER_THE_MAIN_VIEW_ON}, darkvater@176: { WWT_EMPTY, 0, 0, 0, 0, 0, 0x0, STR_NULL}, darkvater@176: { WWT_PUSHTXTBTN, 13, 185, 259, 0, 13, STR_0130_RENAME, STR_200C_CHANGE_TOWN_NAME}, darkvater@176: { WWT_PUSHTXTBTN, 13, 86, 171, 138, 149, STR_023C_EXPAND, STR_023B_INCREASE_SIZE_OF_TOWN}, darkvater@176: { WWT_PUSHTXTBTN, 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_scen_desc = { truelight@0: -1, -1, 260, 150, truelight@0: WC_TOWN_VIEW,0, truelight@0: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, truelight@0: _town_view_scen_widgets, truelight@0: TownViewWndProc truelight@0: }; truelight@0: truelight@0: void ShowTownViewWindow(uint town) truelight@0: { truelight@0: Window *w; truelight@0: Town *t; truelight@0: truelight@0: if (_game_mode != GM_EDITOR) { truelight@0: w = AllocateWindowDescFront(&_town_view_desc, town); truelight@0: } else { truelight@0: w = AllocateWindowDescFront(&_town_view_scen_desc, town); truelight@0: } truelight@0: truelight@0: if (w) { truelight@0: w->flags4 |= WF_DISABLE_VP_SCROLL; truelight@0: t = DEREF_TOWN(w->window_number); truelight@0: AssignWindowViewport(w, 3, 17, 0xFE, 0x56, t->xy, 1); truelight@0: } truelight@0: } truelight@0: truelight@0: static const Widget _town_directory_widgets[] = { truelight@0: { WWT_TEXTBTN, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, truelight@0: { WWT_CAPTION, 13, 11, 207, 0, 13, STR_2000_TOWNS, STR_018C_WINDOW_TITLE_DRAG_THIS}, truelight@0: { WWT_PUSHTXTBTN, 13, 0, 98, 14, 25, STR_SORT_BY_NAME, STR_SORT_TIP}, truelight@0: { WWT_PUSHTXTBTN, 13, 99, 196, 14, 25, STR_SORT_BY_POPULATION,STR_SORT_TIP}, truelight@0: { WWT_IMGBTN, 13, 0, 196, 26, 189, 0x0, STR_200A_TOWN_NAMES_CLICK_ON_NAME}, truelight@0: { WWT_SCROLLBAR, 13, 197, 207, 14, 189, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: truelight@0: // used to get a sorted list of the towns truelight@0: static byte _town_sort[lengthof(_towns)]; truelight@0: static uint _num_town_sort; truelight@0: truelight@0: static char _bufcache[64]; truelight@0: static byte _last_town_idx; truelight@0: darkvater@164: static int CDECL TownNameSorter(const void *a, const void *b) truelight@0: { truelight@0: char buf1[64]; darkvater@222: const Town *t; truelight@0: byte val; truelight@0: int r; truelight@0: darkvater@222: t = DEREF_TOWN(*(const byte*)a); truelight@0: SET_DPARAM32(0, t->townnameparts); truelight@0: GetString(buf1, t->townnametype); truelight@0: darkvater@222: if ( (val=*(const byte*)b) != _last_town_idx) { truelight@0: _last_town_idx = val; truelight@0: t = DEREF_TOWN(val); truelight@0: SET_DPARAM32(0, t->townnameparts); truelight@0: GetString(_bufcache, t->townnametype); 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: { darkvater@222: const Town *ta = DEREF_TOWN(*(const byte*)a); darkvater@222: const Town *tb = DEREF_TOWN(*(const byte*)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: truelight@0: static void MakeSortedTownList() truelight@0: { truelight@0: Town *t; truelight@0: int n = 0; truelight@0: FOR_ALL_TOWNS(t) if(t->xy) _town_sort[n++] = t->index; truelight@0: _num_town_sort = n; truelight@0: darkvater@174: _last_town_idx = 0; // used for "cache" darkvater@164: qsort(_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter); darkvater@65: darkvater@69: DEBUG(misc, 1) ("Resorting Towns list..."); truelight@0: } truelight@0: truelight@0: truelight@0: static void TownDirectoryWndProc(Window *w, WindowEvent *e) truelight@0: { truelight@0: switch(e->event) { truelight@0: case WE_PAINT: { truelight@0: truelight@0: if (_town_sort_dirty) { truelight@0: _town_sort_dirty = false; truelight@0: MakeSortedTownList(); truelight@0: } truelight@0: darkvater@174: SetVScrollCount(w, _num_town_sort); truelight@0: truelight@0: DrawWindowWidgets(w); darkvater@174: DoDrawString(_town_sort_order & 1 ? "\xAA" : "\xA0", (_town_sort_order <= 1) ? 88 : 187, 15, 0x10); truelight@193: darkvater@174: { darkvater@174: Town *t; darkvater@174: int n = 0; darkvater@174: uint16 i = w->vscroll.pos; darkvater@174: int y = 28; truelight@0: darkvater@174: while (i < _num_town_sort) { darkvater@174: t = DEREF_TOWN(_town_sort[i]); darkvater@174: darkvater@174: assert(t->xy); darkvater@174: darkvater@174: SET_DPARAM16(0, t->index); darkvater@174: SET_DPARAM32(1, t->population); darkvater@174: DrawString(2, y, STR_2057, 0); darkvater@174: darkvater@174: y += 10; darkvater@174: i++; darkvater@174: if (++n == w->vscroll.cap) { break;} // max number of towns in 1 window darkvater@174: } truelight@0: } truelight@0: } break; truelight@0: truelight@0: case WE_CLICK: truelight@0: switch(e->click.widget) { darkvater@174: case 2: { /* Sort by Name ascending/descending */ darkvater@174: _town_sort_order = (_town_sort_order == 0) ? 1 : 0; truelight@0: _town_sort_dirty = true; truelight@0: SetWindowDirty(w); truelight@0: } break; truelight@0: darkvater@174: case 3: { /* Sort by Population ascending/descending */ darkvater@174: _town_sort_order = (_town_sort_order == 2) ? 3 : 2; truelight@0: _town_sort_dirty = true; truelight@0: SetWindowDirty(w); truelight@0: } break; truelight@193: darkvater@174: case 4: { /* Click on Town Matrix */ darkvater@174: uint16 id_v = (e->click.pt.y - 28) / 10; truelight@0: darkvater@174: if (id_v >= w->vscroll.cap) { return;} // click out of bounds darkvater@174: darkvater@174: id_v += w->vscroll.pos; darkvater@174: darkvater@174: if (id_v >= _num_town_sort) { return;} // click out of town bounds darkvater@174: darkvater@174: { darkvater@174: Town *t = DEREF_TOWN(_town_sort[id_v]); darkvater@174: assert(t->xy); truelight@193: truelight@0: ScrollMainWindowToTile(t->xy); truelight@0: } darkvater@174: } break; truelight@0: } truelight@0: break; truelight@193: truelight@0: case WE_4: truelight@0: SetWindowDirty(w); truelight@0: break; truelight@193: } truelight@0: } truelight@0: truelight@0: static const WindowDesc _town_directory_desc = { truelight@0: -1, -1, 208, 190, truelight@0: WC_TOWN_DIRECTORY,0, truelight@0: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, truelight@0: _town_directory_widgets, truelight@0: TownDirectoryWndProc truelight@0: }; truelight@0: truelight@0: truelight@0: void ShowTownDirectory() truelight@0: { truelight@0: Window *w; truelight@0: truelight@0: w = AllocateWindowDescFront(&_town_directory_desc, 0); truelight@0: if (w) { truelight@0: w->vscroll.cap = 16; truelight@0: } truelight@0: } truelight@0: truelight@0: