2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-02-06 08:18:00 +00:00
|
|
|
#include "strings.h"
|
2005-02-13 11:18:02 +00:00
|
|
|
#include "table/sprites.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "viewport.h"
|
|
|
|
#include "gui.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "player.h"
|
2005-01-04 19:49:44 +00:00
|
|
|
#include "network.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static const Widget _town_authority_widgets[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
|
2005-01-03 19:45:18 +00:00
|
|
|
{ WWT_CAPTION, RESIZE_NONE, 13, 11, 316, 0, 13, STR_2022_LOCAL_AUTHORITY, STR_018C_WINDOW_TITLE_DRAG_THIS},
|
2006-08-22 14:38:37 +00:00
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 316, 14, 105, 0x0, STR_NULL},
|
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 306, 106, 157, 0x0, STR_2043_LIST_OF_THINGS_TO_DO_AT},
|
|
|
|
{ WWT_SCROLLBAR, RESIZE_NONE, 13, 305, 316, 106, 157, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
|
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 316, 158, 209, 0x0, STR_NULL},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 316, 210, 221, STR_2042_DO_IT, STR_2044_CARRY_OUT_THE_HIGHLIGHTED},
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern const byte _town_action_costs[8];
|
2006-04-29 14:31:16 +00:00
|
|
|
extern void DrawPlayerIcon(PlayerID pid, int x, int y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Get a list of available actions to do at a town.
|
|
|
|
* @param *nump if not NULL add put the number of available actions in it
|
|
|
|
* @param pid the player that is querying the town
|
|
|
|
* @param *t the town that is queried
|
|
|
|
* @return bitmasked value of enabled actions
|
|
|
|
*/
|
|
|
|
uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int32 avail, ref;
|
2005-05-12 00:11:37 +00:00
|
|
|
int num = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint avail_buttons = 0x7F; // by default all buttons except bribe are enabled.
|
2005-05-12 00:11:37 +00:00
|
|
|
uint buttons = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
if (pid != OWNER_SPECTATOR) {
|
2005-11-14 19:48:04 +00:00
|
|
|
uint i;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// bribe option enabled?
|
|
|
|
if (_patches.bribe) {
|
|
|
|
// if unwanted, disable everything.
|
2005-05-12 00:11:37 +00:00
|
|
|
if (t->unwanted[pid]) {
|
2004-08-09 17:04:08 +00:00
|
|
|
avail_buttons = 0;
|
2005-11-16 12:25:33 +00:00
|
|
|
} else if (t->ratings[pid] < RATING_BRIBE_MAXIMUM) {
|
|
|
|
SETBIT(avail_buttons, 7); // Allow bribing
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Things worth more than this are not shown
|
2005-06-21 16:28:17 +00:00
|
|
|
avail = GetPlayer(pid)->player_money + _price.station_value * 200;
|
2004-08-09 17:04:08 +00:00
|
|
|
ref = _price.build_industry >> 8;
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
for (i = 0; i != lengthof(_town_action_costs); i++, avail_buttons >>= 1) {
|
|
|
|
if (HASBIT(avail_buttons, 0) && avail >= _town_action_costs[i] * ref) {
|
2004-08-09 17:04:08 +00:00
|
|
|
SETBIT(buttons, i);
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* Disable build statue if already built */
|
|
|
|
if (HASBIT(t->statues, pid)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
CLRBIT(buttons, 4);
|
|
|
|
num--;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
if (nump != NULL) *nump = num;
|
2004-08-09 17:04:08 +00:00
|
|
|
return buttons;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetNthSetBit(uint32 bits, int n)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (n >= 0) {
|
|
|
|
do {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (bits & 1 && --n < 0) return i;
|
2004-08-09 17:04:08 +00:00
|
|
|
i++;
|
2005-11-14 19:48:04 +00:00
|
|
|
} while (bits >>= 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TownAuthorityWndProc(Window *w, WindowEvent *e)
|
|
|
|
{
|
2005-05-12 00:11:37 +00:00
|
|
|
switch (e->event) {
|
|
|
|
case WE_PAINT: {
|
|
|
|
const Town *t = GetTown(w->window_number);
|
|
|
|
int numact;
|
|
|
|
uint buttons = GetMaskOfTownActions(&numact, _local_player, t);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SetVScrollCount(w, numact + 1);
|
|
|
|
|
|
|
|
if (WP(w,def_d).data_1 != -1 && !HASBIT(buttons, WP(w,def_d).data_1))
|
|
|
|
WP(w,def_d).data_1 = -1;
|
|
|
|
|
|
|
|
w->disabled_state = (WP(w,def_d).data_1 == -1) ? (1 << 6) : 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
int y;
|
2006-07-26 03:33:12 +00:00
|
|
|
const Player *p;
|
2004-08-09 17:04:08 +00:00
|
|
|
int r;
|
|
|
|
StringID str;
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, w->window_number);
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawWindowWidgets(w);
|
|
|
|
|
|
|
|
DrawString(2, 15, STR_2023_TRANSPORT_COMPANY_RATINGS, 0);
|
2004-08-23 21:12:51 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Draw list of players
|
|
|
|
y = 25;
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (p->is_active && (HASBIT(t->have_ratings, p->index) || t->exclusivity == p->index)) {
|
2004-08-24 07:32:49 +00:00
|
|
|
DrawPlayerIcon(p->index, 2, y);
|
2004-08-23 21:04:39 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
|
|
|
SetDParam(2, GetPlayerNameString(p->index, 3));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
r = t->ratings[p->index];
|
2005-01-14 09:20:12 +00:00
|
|
|
(str = STR_3035_APPALLING, r <= RATING_APPALLING) || // Apalling
|
|
|
|
(str++, r <= RATING_VERYPOOR) || // Very Poor
|
|
|
|
(str++, r <= RATING_POOR) || // Poor
|
|
|
|
(str++, r <= RATING_MEDIOCRE) || // Mediocore
|
|
|
|
(str++, r <= RATING_GOOD) || // Good
|
|
|
|
(str++, r <= RATING_VERYGOOD) || // Very Good
|
|
|
|
(str++, r <= RATING_EXCELLENT) || // Excellent
|
2004-08-09 17:04:08 +00:00
|
|
|
(str++, true); // Outstanding
|
|
|
|
|
2005-07-15 14:53:44 +00:00
|
|
|
SetDParam(4, str);
|
2004-09-14 15:46:21 +00:00
|
|
|
if (t->exclusivity == p->index) // red icon for player with exclusive rights
|
2005-11-14 19:48:04 +00:00
|
|
|
DrawSprite(SPR_BLOT | PALETTE_TO_RED, 18, y);
|
2004-09-14 15:46:21 +00:00
|
|
|
|
|
|
|
DrawString(28, y, STR_2024, 0);
|
2005-11-14 19:48:04 +00:00
|
|
|
y += 10;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw actions list
|
|
|
|
{
|
|
|
|
int y = 107, i;
|
|
|
|
int pos = w->vscroll.pos;
|
|
|
|
|
|
|
|
if (--pos < 0) {
|
|
|
|
DrawString(2, y, STR_2045_ACTIONS_AVAILABLE, 0);
|
2005-11-14 19:48:04 +00:00
|
|
|
y += 10;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = 0; buttons; i++, buttons >>= 1) {
|
|
|
|
if (pos <= -5) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (buttons&1 && --pos < 0) {
|
|
|
|
DrawString(3, y, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i, 6);
|
|
|
|
y += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-23 21:12:51 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
int i = WP(w,def_d).data_1;
|
|
|
|
|
|
|
|
if (i != -1) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, (_price.build_industry >> 8) * _town_action_costs[i]);
|
|
|
|
SetDParam(0, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i);
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawStringMultiLine(2, 159, STR_204D_INITIATE_A_SMALL_LOCAL + i, 313);
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
} break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
case WE_CLICK:
|
2005-05-12 00:11:37 +00:00
|
|
|
switch (e->click.widget) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case 3: { /* listbox */
|
2005-05-12 00:11:37 +00:00
|
|
|
const Town *t = GetTown(w->window_number);
|
2004-08-09 17:04:08 +00:00
|
|
|
int y = (e->click.pt.y - 0x6B) / 10;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
|
|
|
if (!IS_INT_INSIDE(y, 0, 5)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_player, t), y + w->vscroll.pos - 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (y >= 0) {
|
|
|
|
WP(w,def_d).data_1 = y;
|
|
|
|
SetWindowDirty(w);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 6: { /* carry out the action */
|
2005-05-14 18:25:01 +00:00
|
|
|
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));
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WE_4:
|
|
|
|
SetWindowDirty(w);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const WindowDesc _town_authority_desc = {
|
|
|
|
-1, -1, 317, 222,
|
|
|
|
WC_TOWN_AUTHORITY,0,
|
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
|
|
|
|
_town_authority_widgets,
|
|
|
|
TownAuthorityWndProc
|
|
|
|
};
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void ShowTownAuthorityWindow(uint town)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-07-26 03:33:12 +00:00
|
|
|
Window *w = AllocateWindowDescFront(&_town_authority_desc, town);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
w->vscroll.cap = 5;
|
|
|
|
WP(w,def_d).data_1 = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TownViewWndProc(Window *w, WindowEvent *e)
|
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Town *t = GetTown(w->window_number);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
switch (e->event) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case WE_PAINT:
|
2005-01-04 19:49:44 +00:00
|
|
|
// disable renaming town in network games if you are not the server
|
|
|
|
if (_networking && !_network_server) SETBIT(w->disabled_state, 8);
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawWindowWidgets(w);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->population);
|
|
|
|
SetDParam(1, t->num_houses);
|
2005-11-14 19:48:04 +00:00
|
|
|
DrawString(2, 107, STR_2006_POPULATION, 0);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->act_pass);
|
|
|
|
SetDParam(1, t->max_pass);
|
2005-11-14 19:48:04 +00:00
|
|
|
DrawString(2, 117, STR_200D_PASSENGERS_LAST_MONTH_MAX, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->act_mail);
|
|
|
|
SetDParam(1, t->max_mail);
|
2005-11-14 19:48:04 +00:00
|
|
|
DrawString(2, 127, STR_200E_MAIL_LAST_MONTH_MAX, 0);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawWindowViewport(w);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WE_CLICK:
|
2005-11-14 19:48:04 +00:00
|
|
|
switch (e->click.widget) {
|
|
|
|
case 6: /* scroll to location */
|
|
|
|
ScrollMainWindowToTile(t->xy);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7: /* town authority */
|
|
|
|
ShowTownAuthorityWindow(w->window_number);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8: /* rename */
|
|
|
|
SetDParam(0, w->window_number);
|
2006-08-19 09:31:22 +00:00
|
|
|
ShowQueryString(STR_TOWN, STR_2007_RENAME_TOWN, 31, 130, w->window_class, w->window_number, CS_ALPHANUMERAL);
|
2005-11-14 19:48:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 9: /* expand town */
|
|
|
|
ExpandTown(t);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 10: /* delete town */
|
|
|
|
DeleteTown(t);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-10-22 06:39:32 +00:00
|
|
|
case WE_ON_EDIT_TEXT:
|
2005-05-15 18:50:55 +00:00
|
|
|
if (e->edittext.str[0] != '\0') {
|
|
|
|
_cmd_text = e->edittext.str;
|
|
|
|
DoCommandP(0, w->window_number, 0, NULL,
|
|
|
|
CMD_RENAME_TOWN | CMD_MSG(STR_2008_CAN_T_RENAME_TOWN));
|
|
|
|
}
|
2005-10-22 06:39:32 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const Widget _town_view_widgets[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
|
|
|
|
{ WWT_CAPTION, RESIZE_NONE, 13, 11, 247, 0, 13, STR_2005, STR_018C_WINDOW_TITLE_DRAG_THIS},
|
|
|
|
{ WWT_STICKYBOX, RESIZE_NONE, 13, 248, 259, 0, 13, 0x0, STR_STICKY_BUTTON},
|
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 259, 14, 105, 0x0, STR_NULL},
|
|
|
|
{ WWT_6, RESIZE_NONE, 13, 2, 257, 16, 103, 0x0, STR_NULL},
|
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 259, 106, 137, 0x0, STR_NULL},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 85, 138, 149, STR_00E4_LOCATION, STR_200B_CENTER_THE_MAIN_VIEW_ON},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 86, 171, 138, 149, STR_2020_LOCAL_AUTHORITY, STR_2021_SHOW_INFORMATION_ON_LOCAL},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 172, 259, 138, 149, STR_0130_RENAME, STR_200C_CHANGE_TOWN_NAME},
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const WindowDesc _town_view_desc = {
|
|
|
|
-1, -1, 260, 150,
|
|
|
|
WC_TOWN_VIEW,0,
|
2004-12-22 01:32:30 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
|
2004-08-09 17:04:08 +00:00
|
|
|
_town_view_widgets,
|
|
|
|
TownViewWndProc
|
|
|
|
};
|
|
|
|
|
|
|
|
static const Widget _town_view_scen_widgets[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
|
|
|
|
{ WWT_CAPTION, RESIZE_NONE, 13, 11, 172, 0, 13, STR_2005, STR_018C_WINDOW_TITLE_DRAG_THIS},
|
2005-01-03 19:45:18 +00:00
|
|
|
{ WWT_STICKYBOX, RESIZE_NONE, 13, 248, 259, 0, 13, 0x0, STR_STICKY_BUTTON},
|
2006-08-22 14:38:37 +00:00
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 259, 14, 105, 0x0, STR_NULL},
|
|
|
|
{ WWT_6, RESIZE_NONE, 13, 2, 257, 16, 103, 0x0, STR_NULL},
|
|
|
|
{ WWT_IMGBTN, RESIZE_NONE, 13, 0, 259, 106, 137, 0x0, STR_NULL},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 85, 138, 149, STR_00E4_LOCATION, STR_200B_CENTER_THE_MAIN_VIEW_ON},
|
|
|
|
{ WWT_EMPTY, RESIZE_NONE, 0, 0, 0, 0, 0, 0x0, STR_NULL},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 173, 247, 0, 13, STR_0130_RENAME, STR_200C_CHANGE_TOWN_NAME},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 86, 171, 138, 149, STR_023C_EXPAND, STR_023B_INCREASE_SIZE_OF_TOWN},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 172, 259, 138, 149, STR_0290_DELETE, STR_0291_DELETE_THIS_TOWN_COMPLETELY},
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const WindowDesc _town_view_scen_desc = {
|
|
|
|
-1, -1, 260, 150,
|
|
|
|
WC_TOWN_VIEW,0,
|
2004-12-22 01:32:30 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
|
2004-08-09 17:04:08 +00:00
|
|
|
_town_view_scen_widgets,
|
|
|
|
TownViewWndProc
|
|
|
|
};
|
|
|
|
|
2006-03-26 22:58:11 +00:00
|
|
|
void ShowTownViewWindow(TownID town)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Window *w;
|
|
|
|
|
|
|
|
if (_game_mode != GM_EDITOR) {
|
|
|
|
w = AllocateWindowDescFront(&_town_view_desc, town);
|
|
|
|
} else {
|
|
|
|
w = AllocateWindowDescFront(&_town_view_scen_desc, town);
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
w->flags4 |= WF_DISABLE_VP_SCROLL;
|
2005-11-14 19:48:04 +00:00
|
|
|
AssignWindowViewport(w, 3, 17, 0xFE, 0x56, GetTown(town)->xy, 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const Widget _town_directory_widgets[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
|
|
|
|
{ WWT_CAPTION, RESIZE_NONE, 13, 11, 195, 0, 13, STR_2000_TOWNS, STR_018C_WINDOW_TITLE_DRAG_THIS},
|
|
|
|
{ WWT_STICKYBOX, RESIZE_NONE, 13, 196, 207, 0, 13, 0x0, STR_STICKY_BUTTON},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 98, 14, 25, STR_SORT_BY_NAME, STR_SORT_ORDER_TIP},
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, 13, 99, 195, 14, 25, STR_SORT_BY_POPULATION, STR_SORT_ORDER_TIP},
|
|
|
|
{ WWT_IMGBTN, RESIZE_BOTTOM, 13, 0, 195, 26, 189, 0x0, STR_200A_TOWN_NAMES_CLICK_ON_NAME},
|
|
|
|
{ WWT_SCROLLBAR, RESIZE_BOTTOM, 13, 196, 207, 14, 189, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
|
|
|
|
{ WWT_PANEL, RESIZE_TB, 13, 0, 195, 190, 201, 0x0, STR_NULL},
|
|
|
|
{ WWT_RESIZEBOX, RESIZE_TB, 13, 196, 207, 190, 201, 0x0, STR_RESIZE_BUTTON},
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// used to get a sorted list of the towns
|
|
|
|
static uint _num_town_sort;
|
|
|
|
|
|
|
|
static char _bufcache[64];
|
2006-08-15 07:07:17 +00:00
|
|
|
static const Town* _last_town;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-06 18:15:13 +00:00
|
|
|
static int CDECL TownNameSorter(const void *a, const void *b)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-15 07:07:17 +00:00
|
|
|
const Town* ta = *(const Town**)a;
|
|
|
|
const Town* tb = *(const Town**)b;
|
2004-08-09 17:04:08 +00:00
|
|
|
char buf1[64];
|
|
|
|
int r;
|
|
|
|
|
2006-08-27 10:04:33 +00:00
|
|
|
SetDParam(0, ta->index);
|
|
|
|
GetString(buf1, STR_TOWN);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-01 18:32:01 +00:00
|
|
|
/* If 'b' is the same town as in the last round, use the cached value
|
|
|
|
* We do this to speed stuff up ('b' is called with the same value a lot of
|
2006-08-15 07:07:17 +00:00
|
|
|
* times after eachother) */
|
|
|
|
if (tb != _last_town) {
|
|
|
|
_last_town = tb;
|
2006-08-27 10:04:33 +00:00
|
|
|
SetDParam(0, tb->index);
|
|
|
|
GetString(_bufcache, STR_TOWN);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = strcmp(buf1, _bufcache);
|
|
|
|
if (_town_sort_order & 1) r = -r;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-09-06 18:15:13 +00:00
|
|
|
static int CDECL TownPopSorter(const void *a, const void *b)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-15 07:07:17 +00:00
|
|
|
const Town* ta = *(const Town**)a;
|
|
|
|
const Town* tb = *(const Town**)b;
|
2004-09-06 18:15:13 +00:00
|
|
|
int r = ta->population - tb->population;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_town_sort_order & 1) r = -r;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void MakeSortedTownList(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Town* t;
|
2005-11-14 19:48:04 +00:00
|
|
|
uint n = 0;
|
2005-01-06 22:31:58 +00:00
|
|
|
|
|
|
|
/* Create array for sorting */
|
2006-08-22 20:41:26 +00:00
|
|
|
_town_sort = realloc((void*)_town_sort, GetTownArraySize() * sizeof(_town_sort[0]));
|
2005-01-06 22:31:58 +00:00
|
|
|
if (_town_sort == NULL)
|
|
|
|
error("Could not allocate memory for the town-sorting-list");
|
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
FOR_ALL_TOWNS(t) _town_sort[n++] = t;
|
2004-12-28 17:50:17 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_num_town_sort = n;
|
|
|
|
|
2006-08-15 07:07:17 +00:00
|
|
|
_last_town = NULL; // used for "cache"
|
|
|
|
qsort((void*)_town_sort, n, sizeof(_town_sort[0]), _town_sort_order & 2 ? TownPopSorter : TownNameSorter);
|
2004-08-16 14:48:35 +00:00
|
|
|
|
2004-08-16 22:15:44 +00:00
|
|
|
DEBUG(misc, 1) ("Resorting Towns list...");
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void TownDirectoryWndProc(Window *w, WindowEvent *e)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
switch (e->event) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case WE_PAINT: {
|
|
|
|
if (_town_sort_dirty) {
|
|
|
|
_town_sort_dirty = false;
|
|
|
|
MakeSortedTownList();
|
|
|
|
}
|
|
|
|
|
2004-09-07 19:01:06 +00:00
|
|
|
SetVScrollCount(w, _num_town_sort);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
DrawWindowWidgets(w);
|
2005-09-10 07:38:03 +00:00
|
|
|
DoDrawString(_town_sort_order & 1 ? DOWNARROW : UPARROW, (_town_sort_order <= 1) ? 88 : 187, 15, 0x10);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-09-07 19:01:06 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
uint16 i = w->vscroll.pos;
|
|
|
|
int y = 28;
|
|
|
|
|
|
|
|
while (i < _num_town_sort) {
|
2006-08-15 07:07:17 +00:00
|
|
|
const Town* t = _town_sort[i];
|
2004-09-07 19:01:06 +00:00
|
|
|
|
|
|
|
assert(t->xy);
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
|
|
|
SetDParam(1, t->population);
|
2004-09-07 19:01:06 +00:00
|
|
|
DrawString(2, y, STR_2057, 0);
|
|
|
|
|
|
|
|
y += 10;
|
|
|
|
i++;
|
2005-10-23 13:04:44 +00:00
|
|
|
if (++n == w->vscroll.cap) break; // max number of towns in 1 window
|
2004-09-07 19:01:06 +00:00
|
|
|
}
|
2005-01-21 16:51:25 +00:00
|
|
|
SetDParam(0, GetWorldPopulation());
|
|
|
|
DrawString(3, w->height - 12 + 2, STR_TOWN_POPULATION, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case WE_CLICK:
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (e->click.widget) {
|
2004-12-22 01:32:30 +00:00
|
|
|
case 3: { /* Sort by Name ascending/descending */
|
2004-09-07 19:01:06 +00:00
|
|
|
_town_sort_order = (_town_sort_order == 0) ? 1 : 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
_town_sort_dirty = true;
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} break;
|
|
|
|
|
2004-12-22 01:32:30 +00:00
|
|
|
case 4: { /* Sort by Population ascending/descending */
|
2004-09-07 19:01:06 +00:00
|
|
|
_town_sort_order = (_town_sort_order == 2) ? 3 : 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
_town_sort_dirty = true;
|
|
|
|
SetWindowDirty(w);
|
|
|
|
} break;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-22 01:32:30 +00:00
|
|
|
case 5: { /* Click on Town Matrix */
|
2006-08-15 07:07:17 +00:00
|
|
|
const Town* t;
|
|
|
|
|
2004-09-07 19:01:06 +00:00
|
|
|
uint16 id_v = (e->click.pt.y - 28) / 10;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-23 13:04:44 +00:00
|
|
|
if (id_v >= w->vscroll.cap) return; // click out of bounds
|
2004-09-07 19:01:06 +00:00
|
|
|
|
|
|
|
id_v += w->vscroll.pos;
|
|
|
|
|
2005-10-23 13:04:44 +00:00
|
|
|
if (id_v >= _num_town_sort) return; // click out of town bounds
|
2004-09-07 19:01:06 +00:00
|
|
|
|
2006-08-15 07:07:17 +00:00
|
|
|
t = _town_sort[id_v];
|
|
|
|
assert(t->xy);
|
|
|
|
ScrollMainWindowToTile(t->xy);
|
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
case WE_4:
|
|
|
|
SetWindowDirty(w);
|
|
|
|
break;
|
2005-01-03 19:45:18 +00:00
|
|
|
|
|
|
|
case WE_RESIZE:
|
|
|
|
w->vscroll.cap += e->sizing.diff.y / 10;
|
|
|
|
break;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const WindowDesc _town_directory_desc = {
|
2005-01-21 16:51:25 +00:00
|
|
|
-1, -1, 208, 202,
|
2004-08-09 17:04:08 +00:00
|
|
|
WC_TOWN_DIRECTORY,0,
|
2005-01-03 19:45:18 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
|
2004-08-09 17:04:08 +00:00
|
|
|
_town_directory_widgets,
|
|
|
|
TownDirectoryWndProc
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void ShowTownDirectory(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-07-26 03:33:12 +00:00
|
|
|
Window *w = AllocateWindowDescFront(&_town_directory_desc, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (w != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
w->vscroll.cap = 16;
|
2005-01-03 19:45:18 +00:00
|
|
|
w->resize.step_height = 10;
|
2005-01-21 16:51:25 +00:00
|
|
|
w->resize.height = w->height - 10 * 6; // minimum of 10 items in the list, each item 10 high
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|