author | darkvater |
Mon, 16 Aug 2004 22:15:44 +0000 | |
changeset 69 | f8c29cdb388e |
parent 65 | f9f866bc609c |
child 164 | 0cbdf3c9bde1 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "window.h" |
|
4 |
#include "gui.h" |
|
5 |
#include "station.h" |
|
6 |
#include "gfx.h" |
|
7 |
#include "player.h" |
|
8 |
#include "town.h" |
|
9 |
#include "command.h" |
|
10 |
||
11 |
static void StationsWndShowStationRating(int x, int y, int type, uint acceptance, int rating) |
|
12 |
{ |
|
13 |
static const byte _rating_colors[NUM_CARGO] = {152,32,15,174,208,194,191,55,184,10,191,48}; |
|
14 |
int color = _rating_colors[type]; |
|
15 |
uint w; |
|
16 |
||
17 |
if (acceptance > 575) |
|
18 |
acceptance = 575; |
|
19 |
||
20 |
acceptance = (acceptance + 7) >> 3; |
|
21 |
||
22 |
/* draw cargo */ |
|
23 |
if ( (w=acceptance>>3) != 0) { |
|
24 |
GfxFillRect(x, y, x+w-1, y+6, color); |
|
25 |
x += w; |
|
26 |
} |
|
27 |
||
28 |
if ( (w=acceptance&7) != 0) { |
|
29 |
if (w==7) w--; |
|
30 |
GfxFillRect(x, y+(w-1), x, y+6, color); |
|
31 |
} |
|
32 |
||
33 |
x -= (acceptance>>3); |
|
34 |
||
35 |
DrawString(x+1, y, _cargoc.names_short[type], 0x10); |
|
36 |
||
37 |
/* draw green/red ratings bar */ |
|
38 |
GfxFillRect(x+1, y+8, x+7, y+8, 0xB8); |
|
39 |
||
40 |
rating = (rating >> 5); |
|
41 |
||
42 |
if (rating != 0) { |
|
43 |
GfxFillRect(x+1, y+8, x+rating, y+8, 0xD0); |
|
44 |
} |
|
45 |
} |
|
46 |
||
47 |
// used to get a sorted list of the stations |
|
48 |
typedef struct StationSort { |
|
49 |
uint16 index; |
|
50 |
byte owner; |
|
51 |
} StationSort; |
|
52 |
||
53 |
static StationSort _station_sort[lengthof(_stations)]; |
|
54 |
static uint16 _num_station_sort[MAX_PLAYERS]; |
|
55 |
||
56 |
static char _bufcache[64]; |
|
57 |
static uint16 _last_station_idx; |
|
58 |
||
59 |
static int CDECL StationSorterByName(const void *a, const void *b) |
|
60 |
{ |
|
61 |
char buf1[64]; |
|
62 |
Station *st; |
|
63 |
StationSort *cmp1, *cmp2; |
|
64 |
cmp1 = (StationSort*)a; |
|
65 |
cmp2 = (StationSort*)b; |
|
66 |
||
67 |
// sort stations by owner, and inside owner by name |
|
68 |
if (cmp1->owner == cmp2->owner) { // if same owner, sort by name |
|
69 |
st = DEREF_STATION(cmp1->index); |
|
70 |
SET_DPARAM16(0, st->town->townnametype); |
|
71 |
SET_DPARAM32(1, st->town->townnameparts); |
|
72 |
GetString(buf1, st->string_id); |
|
73 |
||
74 |
if ( cmp2->index != _last_station_idx) { |
|
75 |
_last_station_idx = cmp2->index; |
|
76 |
st = DEREF_STATION(cmp2->index); |
|
77 |
SET_DPARAM16(0, st->town->townnametype); |
|
78 |
SET_DPARAM32(1, st->town->townnameparts); |
|
79 |
GetString(_bufcache, st->string_id); |
|
80 |
} |
|
81 |
||
82 |
return strcmp(buf1, _bufcache); // sort by name |
|
83 |
} |
|
84 |
return cmp1->owner - cmp2->owner; // sort by owner |
|
85 |
} |
|
86 |
||
87 |
static void MakeSortedStationList(Window *w) |
|
88 |
{ |
|
89 |
Station *st; |
|
90 |
uint16 n = 0; |
|
91 |
uint16 *i; |
|
92 |
// reset to 0 just to be sure |
|
93 |
for (i = _num_station_sort; i != endof(_num_station_sort); i++) {*i = 0;} |
|
94 |
||
95 |
FOR_ALL_STATIONS(st) { |
|
96 |
if(st->xy && st->owner != OWNER_NONE) { |
|
97 |
_station_sort[n].index = st->index; |
|
98 |
_station_sort[n++].owner = st->owner; |
|
99 |
_num_station_sort[st->owner]++; // add number of stations of player |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
// create cumulative station-ownage |
|
104 |
// stations are stored as a cummulative index, eg 25, 41, 43. This means |
|
105 |
// Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 |
|
106 |
for (i = &_num_station_sort[1]; i != endof(_num_station_sort); i++) {*i += *(i-1);} |
|
107 |
||
108 |
_last_station_idx = 255; // used for "cache" |
|
109 |
qsort(_station_sort, n, sizeof(_station_sort[0]), StationSorterByName); |
|
65
f9f866bc609c
(svn r66) -Fix Station list updated on station deletion/station rename
darkvater
parents:
0
diff
changeset
|
110 |
|
69 | 111 |
DEBUG(misc, 1) ("Resorting Stations list..."); |
0 | 112 |
} |
113 |
||
114 |
static void PlayerStationsWndProc(Window *w, WindowEvent *e) |
|
115 |
{ |
|
116 |
switch(e->event) { |
|
117 |
case WE_PAINT: { |
|
118 |
byte i; |
|
119 |
if (_station_sort_dirty) { |
|
120 |
_station_sort_dirty = false; |
|
121 |
MakeSortedStationList(w); |
|
122 |
} |
|
123 |
||
124 |
// stations are stored as a cummulative index, eg 25, 41, 43. This means |
|
125 |
// Player0: 25; Player1: (41-25) 16; Player2: (43-41) 2 stations |
|
126 |
i = (byte)(w->window_number == 0) ? 0 : _num_station_sort[w->window_number-1];; |
|
127 |
SetVScrollCount(w, _num_station_sort[w->window_number] - i); |
|
128 |
||
129 |
/* draw widgets, with player's name in the caption */ |
|
130 |
{ |
|
131 |
Player *p = DEREF_PLAYER(w->window_number); |
|
132 |
SET_DPARAM16(0, p->name_1); |
|
133 |
SET_DPARAM32(1, p->name_2); |
|
134 |
SET_DPARAM16(2, w->vscroll.count); |
|
135 |
DrawWindowWidgets(w); |
|
136 |
} |
|
137 |
||
138 |
{ |
|
139 |
byte p = 0; |
|
140 |
Station *st; |
|
141 |
int x,xb = 2; |
|
142 |
int y = 16; // offset from top of widget |
|
143 |
int j; |
|
144 |
||
145 |
if (w->vscroll.count == 0) { // player has no stations |
|
146 |
DrawString(xb, y, STR_304A_NONE, 0); |
|
147 |
return; |
|
148 |
} |
|
149 |
||
150 |
i += w->vscroll.pos; // offset from sorted station list of current player |
|
151 |
assert(i < _num_station_sort[w->window_number]); // at least one station must exist |
|
152 |
||
153 |
while (i < _num_station_sort[w->window_number]) { // do until max number of stations of owner |
|
154 |
st = DEREF_STATION(_station_sort[i].index); |
|
155 |
||
156 |
SET_DPARAM16(0, st->index); |
|
157 |
SET_DPARAM8(1, st->facilities); |
|
158 |
x = DrawString(xb, y, STR_3049_0, 0) + 5; |
|
159 |
||
160 |
// show cargo waiting and station ratings |
|
161 |
for(j=0; j!=NUM_CARGO; j++) { |
|
162 |
int acc = (st->goods[j].waiting_acceptance & 0xFFF); |
|
163 |
if (acc != 0) { |
|
164 |
StationsWndShowStationRating(x, y, j, acc, st->goods[j].rating); |
|
165 |
x += 10; |
|
166 |
} |
|
167 |
} |
|
168 |
y += 10; |
|
169 |
i++; // next station |
|
170 |
if (++p == 12) { break;} // max number of stations in 1 window |
|
171 |
} |
|
172 |
} |
|
173 |
} break; |
|
174 |
case WE_CLICK: { |
|
175 |
switch(e->click.widget) { |
|
176 |
case 2: { |
|
177 |
uint y = (e->click.pt.y - 15) / 10; |
|
178 |
uint16 p; |
|
179 |
||
180 |
if (!IS_INT_INSIDE(y, 0, 12)) { return;} |
|
181 |
||
182 |
// get the p-th OUR station from globally sorted list |
|
183 |
p = y + w->vscroll.pos; |
|
184 |
p += (w->window_number == 0) ? 0 : _num_station_sort[w->window_number-1]; // get offset of first station in list |
|
185 |
if (p < _num_station_sort[w->window_number]) { |
|
186 |
ScrollMainWindowToTile(DEREF_STATION(_station_sort[p].index)->xy); |
|
187 |
} |
|
188 |
} break; |
|
189 |
} |
|
190 |
} break; |
|
191 |
||
192 |
case WE_4: |
|
193 |
WP(w,plstations_d).refresh_counter++; |
|
194 |
if (WP(w,plstations_d).refresh_counter==5) { |
|
195 |
WP(w,plstations_d).refresh_counter = 0; |
|
196 |
SetWindowDirty(w); |
|
197 |
} |
|
198 |
break; |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
static const Widget _player_stations_widgets[] = { |
|
203 |
{ WWT_CLOSEBOX, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
|
204 |
{ WWT_CAPTION, 14, 11, 357, 0, 13, STR_3048_STATIONS, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
|
205 |
{ WWT_PANEL, 14, 0, 346, 14, 137, 0x0, STR_3057_STATION_NAMES_CLICK_ON}, |
|
206 |
{ WWT_SCROLLBAR, 14, 347, 357, 14, 137, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
|
207 |
{ WWT_LAST}, |
|
208 |
}; |
|
209 |
||
210 |
static const WindowDesc _player_stations_desc = { |
|
211 |
-1, -1, 358, 138, |
|
212 |
WC_STATION_LIST,0, |
|
213 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, |
|
214 |
_player_stations_widgets, |
|
215 |
PlayerStationsWndProc |
|
216 |
}; |
|
217 |
||
218 |
||
219 |
void ShowPlayerStations(int player) |
|
220 |
{ |
|
221 |
Window *w; |
|
222 |
||
223 |
w = AllocateWindowDescFront(&_player_stations_desc, player); |
|
224 |
if (w) { |
|
225 |
w->caption_color = (byte)w->window_number; |
|
226 |
w->vscroll.cap = 12; |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
static const Widget _station_view_expanded_widgets[] = { |
|
231 |
{ WWT_TEXTBTN, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
|
232 |
{ WWT_CAPTION, 14, 11, 248, 0, 13, STR_300A_0, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
|
233 |
{ WWT_IMGBTN, 14, 0, 237, 14, 65, 0x0, 0}, |
|
234 |
{ WWT_SCROLLBAR, 14, 238, 248, 14, 65, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
|
235 |
{ WWT_EMPTY, 0, 0, 0, 0, 0, 0x0, 0}, |
|
236 |
{ WWT_IMGBTN, 14, 0, 248, 66, 197, 0x0, 0}, |
|
237 |
{ WWT_PUSHTXTBTN, 14, 0, 82, 198, 209, STR_00E4_LOCATION, STR_3053_CENTER_MAIN_VIEW_ON_STATION}, |
|
238 |
{ WWT_PUSHTXTBTN, 14, 83, 165, 198, 209, STR_3033_ACCEPTS, STR_3056_SHOW_LIST_OF_ACCEPTED_CARGO}, |
|
239 |
{ WWT_PUSHTXTBTN, 14, 166, 248, 198, 209, STR_0130_RENAME, STR_3055_CHANGE_NAME_OF_STATION}, |
|
240 |
{ WWT_LAST}, |
|
241 |
}; |
|
242 |
||
243 |
static const Widget _station_view_widgets[] = { |
|
244 |
{ WWT_TEXTBTN, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
|
245 |
{ WWT_CAPTION, 14, 11, 248, 0, 13, STR_300A_0, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
|
246 |
{ WWT_IMGBTN, 14, 0, 237, 14, 65, 0x0, 0}, |
|
247 |
{ WWT_SCROLLBAR, 14, 238, 248, 14, 65, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
|
248 |
{ WWT_IMGBTN, 14, 0, 248, 66, 97, 0x0, 0}, |
|
249 |
{ WWT_EMPTY, 0, 0, 0, 0, 0, 0x0, 0}, |
|
250 |
{ WWT_PUSHTXTBTN, 14, 0, 82, 98, 109, STR_00E4_LOCATION, STR_3053_CENTER_MAIN_VIEW_ON_STATION}, |
|
251 |
{ WWT_PUSHTXTBTN, 14, 83, 165, 98, 109, STR_3032_RATINGS, STR_3054_SHOW_STATION_RATINGS}, |
|
252 |
{ WWT_PUSHTXTBTN, 14, 166, 248, 98, 109, STR_0130_RENAME, STR_3055_CHANGE_NAME_OF_STATION}, |
|
253 |
{ WWT_LAST}, |
|
254 |
}; |
|
255 |
||
256 |
static void DrawStationViewWindow(Window *w) |
|
257 |
{ |
|
258 |
Station *st; |
|
259 |
int i; |
|
260 |
int num; |
|
261 |
int x,y; |
|
262 |
int pos; |
|
263 |
StringID str; |
|
264 |
byte station_id; |
|
265 |
byte *b; |
|
266 |
||
267 |
||
268 |
station_id = (byte)w->window_number; |
|
269 |
||
270 |
st = DEREF_STATION(w->window_number); |
|
271 |
||
272 |
num = 1; |
|
273 |
for(i=0; i!=NUM_CARGO; i++) { |
|
274 |
if ((st->goods[i].waiting_acceptance & 0xFFF) != 0) { |
|
275 |
num++; |
|
276 |
if (st->goods[i].enroute_from != station_id) |
|
277 |
num++; |
|
278 |
} |
|
279 |
} |
|
280 |
SetVScrollCount(w, num); |
|
281 |
||
282 |
w->disabled_state = st->owner == _local_player ? 0 : (1 << 8); |
|
283 |
SET_DPARAM16(0, st->index); |
|
284 |
SET_DPARAM8(1, st->facilities); |
|
285 |
DrawWindowWidgets(w); |
|
286 |
||
287 |
x = 2; |
|
288 |
y = 15; |
|
289 |
pos = w->vscroll.pos; |
|
290 |
||
291 |
if (--pos < 0) { |
|
292 |
str = STR_00D0_NOTHING; |
|
293 |
for(i=0; i!=NUM_CARGO; i++) |
|
294 |
if (st->goods[i].waiting_acceptance & 0xFFF) |
|
295 |
str = STR_EMPTY; |
|
296 |
SET_DPARAM16(0, str); |
|
297 |
DrawString(x, y, STR_0008_WAITING, 0); |
|
298 |
y += 10; |
|
299 |
} |
|
300 |
||
301 |
i = 0; |
|
302 |
do { |
|
303 |
uint waiting = (st->goods[i].waiting_acceptance & 0xFFF); |
|
304 |
if (waiting == 0) |
|
305 |
continue; |
|
306 |
||
307 |
num = (waiting + 5) / 10; |
|
308 |
if (num != 0) { |
|
309 |
int cur_x = x; |
|
310 |
num = min(num, 23); |
|
311 |
do { |
|
312 |
DrawSprite(_cargoc.sprites[i], cur_x, y); |
|
313 |
cur_x += 10; |
|
314 |
} while (--num); |
|
315 |
} |
|
316 |
||
317 |
if ( st->goods[i].enroute_from == station_id) { |
|
318 |
if (--pos < 0) { |
|
319 |
SET_DPARAM16(1, waiting); |
|
320 |
SET_DPARAM16(0, _cargoc.names_long_s[i] + (waiting==1 ? 0 : 32)); |
|
321 |
DrawStringRightAligned(x + 234, y, STR_0009, 0); |
|
322 |
y += 10; |
|
323 |
} |
|
324 |
} else { |
|
325 |
/* enroute */ |
|
326 |
if (--pos < 0) { |
|
327 |
SET_DPARAM16(1, waiting); |
|
328 |
SET_DPARAM16(0, _cargoc.names_long_s[i] + (waiting==1 ? 0 : 32)); |
|
329 |
DrawStringRightAligned(x + 234, y, STR_000A_EN_ROUTE_FROM, 0); |
|
330 |
y += 10; |
|
331 |
} |
|
332 |
||
333 |
if (pos > -5 && --pos < 0) { |
|
334 |
SET_DPARAM16(0, st->goods[i].enroute_from); |
|
335 |
DrawStringRightAligned(x + 234, y, STR_000B, 0); |
|
336 |
y += 10; |
|
337 |
} |
|
338 |
} |
|
339 |
} while (pos > -5 && ++i != 12); |
|
340 |
||
341 |
if (w->widget == _station_view_widgets) { |
|
342 |
b = _userstring; |
|
343 |
b[0] = 0x81; |
|
344 |
b[1] = STR_000C_ACCEPTS; |
|
345 |
b[2] = STR_000C_ACCEPTS >> 8; |
|
346 |
b += 3; |
|
347 |
||
348 |
for(i=0; i!=NUM_CARGO; i++) { |
|
349 |
if (st->goods[i].waiting_acceptance & 0x8000) { |
|
350 |
b[0] = 0x81; |
|
351 |
WRITE_LE_UINT16(b+1, _cargoc.names_s[i]); |
|
352 |
WRITE_LE_UINT16(b+3, 0x202C); |
|
353 |
b += 5; |
|
354 |
} |
|
355 |
} |
|
356 |
||
357 |
if (b == (byte*)&_userstring[3]) { |
|
358 |
b[0] = 0x81; |
|
359 |
b[1] = STR_00D0_NOTHING; |
|
360 |
b[2] = STR_00D0_NOTHING >> 8; |
|
361 |
b[3] = 0; |
|
362 |
} else { |
|
363 |
b[-2] = 0; |
|
364 |
} |
|
365 |
||
366 |
DrawStringMultiLine(2, 67, STR_SPEC_USERSTRING, 245); |
|
367 |
} else { |
|
368 |
||
369 |
DrawString(2, 67, STR_3034_LOCAL_RATING_OF_TRANSPORT, 0); |
|
370 |
||
371 |
y = 77; |
|
372 |
for(i=0; i!=NUM_CARGO; i++) { |
|
373 |
if (st->goods[i].enroute_from != 0xFF) { |
|
374 |
SET_DPARAM16(0, _cargoc.names_s[i]); |
|
375 |
SET_DPARAM8(2, st->goods[i].rating * 101 >> 8); |
|
376 |
SET_DPARAM16(1, STR_3035_APPALLING + (st->goods[i].rating >> 5)); |
|
377 |
DrawString(8, y, STR_303D, 0); |
|
378 |
y += 10; |
|
379 |
} |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
||
385 |
static void StationViewWndProc(Window *w, WindowEvent *e) |
|
386 |
{ |
|
387 |
switch(e->event) { |
|
388 |
case WE_PAINT: |
|
389 |
DrawStationViewWindow(w); |
|
390 |
break; |
|
391 |
||
392 |
case WE_CLICK: |
|
393 |
switch(e->click.widget) { |
|
394 |
case 6: |
|
395 |
ScrollMainWindowToTile(DEREF_STATION(w->window_number)->xy); |
|
396 |
break; |
|
397 |
||
398 |
case 7: |
|
399 |
SetWindowDirty(w); |
|
400 |
||
401 |
/* toggle height/widget set */ |
|
402 |
w->height ^= (210 ^ 110); |
|
403 |
*(uint32*)&w->widget ^= (uint32)_station_view_expanded_widgets ^ (uint32)_station_view_widgets; |
|
404 |
||
405 |
SetWindowDirty(w); |
|
406 |
break; |
|
407 |
||
408 |
case 8: { |
|
409 |
Station *st = DEREF_STATION(w->window_number); |
|
410 |
SET_DPARAM16(0, st->town->townnametype); |
|
411 |
SET_DPARAM32(1, st->town->townnameparts); |
|
412 |
ShowQueryString(st->string_id, STR_3030_RENAME_STATION_LOADING, 31, 180, w->window_class, w->window_number); |
|
413 |
} break; |
|
414 |
} |
|
415 |
break; |
|
416 |
||
417 |
case WE_ON_EDIT_TEXT: { |
|
418 |
Station *st; |
|
419 |
byte *b = e->edittext.str; |
|
420 |
if (*b == 0) |
|
421 |
return; |
|
422 |
memcpy(_decode_parameters, b, 32); |
|
423 |
||
424 |
st = DEREF_STATION(w->window_number); |
|
425 |
DoCommandP(st->xy, w->window_number, 0, NULL, CMD_RENAME_STATION | CMD_MSG(STR_3031_CAN_T_RENAME_STATION)); |
|
426 |
} break; |
|
427 |
} |
|
428 |
} |
|
429 |
||
430 |
||
431 |
static const WindowDesc _station_view_desc = { |
|
432 |
-1, -1, 249, 110, |
|
433 |
WC_STATION_VIEW,0, |
|
434 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, |
|
435 |
_station_view_widgets, |
|
436 |
StationViewWndProc |
|
437 |
}; |
|
438 |
||
439 |
void ShowStationViewWindow(int station) |
|
440 |
{ |
|
441 |
Window *w; |
|
442 |
byte color; |
|
443 |
||
444 |
w = AllocateWindowDescFront(&_station_view_desc, station); |
|
445 |
if (w) { |
|
446 |
color = DEREF_STATION(w->window_number)->owner; |
|
447 |
if (color != 0x10) |
|
448 |
w->caption_color = color; |
|
449 |
w->vscroll.cap = 5; |
|
450 |
} |
|
451 |
} |