author | tron |
Fri, 05 Nov 2004 23:12:33 +0000 | |
changeset 337 | 66647f97e7c0 |
parent 206 | 7f8c26d8526b |
child 507 | 8aa8100b0b22 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "player.h" |
|
4 |
#include "town.h" |
|
5 |
#include "vehicle.h" |
|
6 |
#include "station.h" |
|
7 |
#include "gfx.h" |
|
8 |
#include "news.h" |
|
9 |
#include "saveload.h" |
|
10 |
#include "command.h" |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
23
diff
changeset
|
11 |
#include "ai.h" |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
206
diff
changeset
|
12 |
#include "sound.h" |
0 | 13 |
|
14 |
extern void StartupEconomy(); |
|
15 |
||
16 |
static const SpriteID cheeks_table[4] = { |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
17 |
0x325, 0x326, |
0 | 18 |
0x390, 0x3B0, |
19 |
}; |
|
20 |
||
21 |
static const SpriteID mouth_table[3] = { |
|
22 |
0x34C, 0x34D, 0x34F |
|
23 |
}; |
|
24 |
||
25 |
void DrawPlayerFace(uint32 face, int color, int x, int y) |
|
26 |
{ |
|
27 |
byte flag = 0; |
|
28 |
||
29 |
if ( (int32)face < 0) |
|
30 |
flag |= 1; |
|
31 |
if ((((((face >> 7) ^ face) >> 7) ^ face) & 0x8080000) == 0x8000000) |
|
32 |
flag |= 2; |
|
33 |
||
34 |
/* draw the gradient */ |
|
35 |
DrawSprite( (color<<16) + 0x0307836A, x, y); |
|
36 |
||
37 |
/* draw the cheeks */ |
|
38 |
DrawSprite(cheeks_table[flag&3], x, y); |
|
39 |
||
40 |
/* draw the chin */ |
|
41 |
/* FIXME: real code uses -2 in zoomlevel 1 */ |
|
42 |
{ |
|
43 |
uint val = (face >> 4) & 3; |
|
44 |
if (!(flag & 2)) { |
|
45 |
DrawSprite(0x327 + (flag&1?0:val), x, y); |
|
46 |
} else { |
|
47 |
DrawSprite((flag&1?0x3B1:0x391) + (val>>1), x, y); |
|
48 |
} |
|
49 |
} |
|
50 |
/* draw the eyes */ |
|
51 |
{ |
|
52 |
uint val1 = (face >> 6)&15; |
|
53 |
uint val2 = (face >> 20)&7; |
|
54 |
uint32 high = 0x314<<16; |
|
55 |
||
56 |
if (val2 >= 6) { |
|
57 |
high = 0x30F<<16; |
|
58 |
if (val2 != 6) |
|
59 |
high = 0x30D<<16; |
|
60 |
} |
|
61 |
||
62 |
if (!(flag & 2)) { |
|
63 |
if (!(flag & 1)) { |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
64 |
DrawSprite(high+((val1 * 12 >> 4) + 0x832B), x, y); |
0 | 65 |
} else { |
66 |
DrawSprite(high+(val1 + 0x8337), x, y); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
67 |
} |
0 | 68 |
} else { |
69 |
if (!(flag & 1)) { |
|
70 |
DrawSprite(high+((val1 * 11 >> 4) + 0x839A), x, y); |
|
71 |
} else { |
|
72 |
DrawSprite(high+(val1 + 0x83B8), x, y); |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
/* draw the mouth */ |
|
78 |
{ |
|
79 |
uint val = (face >> 10) & 63; |
|
80 |
uint val2; |
|
81 |
||
82 |
if (!(flag&1)) { |
|
83 |
val2 = ((val&0xF) * 15 >> 4); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
84 |
|
0 | 85 |
if (val2 < 3) { |
86 |
DrawSprite((flag&2 ? 0x397 : 0x367) + val2, x, y); |
|
87 |
/* skip the rest */ |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
88 |
goto skip_mouth; |
0 | 89 |
} |
90 |
||
91 |
val2 -= 3; |
|
92 |
if (flag & 2) { |
|
93 |
if (val2 > 8) val2 = 0; |
|
94 |
val2 += 0x3A5 - 0x35B; |
|
95 |
} |
|
96 |
DrawSprite(val2 + 0x35B, x, y); |
|
97 |
} else if (!(flag&2)) { |
|
98 |
DrawSprite(((val&0xF) * 10 >> 4) + 0x351, x, y); |
|
99 |
} else { |
|
100 |
DrawSprite(((val&0xF) * 9 >> 4) + 0x3C8, x, y); |
|
101 |
} |
|
102 |
||
103 |
val >>= 3; |
|
104 |
||
105 |
if (!(flag&2)) { |
|
106 |
if (!(flag&1)) { |
|
107 |
DrawSprite(0x349 + val, x, y); |
|
108 |
} else { |
|
109 |
DrawSprite( mouth_table[(val*3>>3)], x, y); |
|
110 |
} |
|
111 |
} else { |
|
112 |
if (!(flag&1)) { |
|
113 |
DrawSprite(0x393 + (val&3), x, y); |
|
114 |
} else { |
|
115 |
DrawSprite(0x3B3 + (val*5>>3), x, y); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
skip_mouth:; |
|
120 |
} |
|
121 |
||
122 |
||
123 |
/* draw the hair */ |
|
124 |
{ |
|
125 |
uint val = (face >> 16) & 15; |
|
126 |
if (!(flag&2)) { |
|
127 |
if (!(flag&1)) { |
|
128 |
DrawSprite(0x382 + (val*9>>4), x, y); |
|
129 |
} else { |
|
130 |
DrawSprite(0x38B + (val*5>>4), x, y); |
|
131 |
} |
|
132 |
} else { |
|
133 |
if (!(flag&1)) { |
|
134 |
DrawSprite(0x3D4 + (val*5>>4), x, y); |
|
135 |
} else { |
|
136 |
DrawSprite(0x3D9 + (val*5>>4), x, y); |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
/* draw the tie */ |
|
142 |
{ |
|
143 |
uint val = (face >> 20) & 0xFF; |
|
144 |
||
145 |
if (!(flag&1)) { |
|
146 |
DrawSprite(0x36B + ((val&3)*3>>2), x, y); |
|
147 |
DrawSprite(0x36E + ((val>>2)&3), x, y); |
|
148 |
DrawSprite(0x372 + ((val>>4)*6>>4), x, y); |
|
149 |
} else { |
|
150 |
DrawSprite(0x378 + ((val&3)*3>>2), x, y); |
|
151 |
DrawSprite(0x37B + ((val>>2)&3), x, y); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
152 |
|
0 | 153 |
val >>= 4; |
154 |
if (val < 3) { |
|
155 |
DrawSprite((flag&2 ? 0x3D1 : 0x37F) + val, x, y); |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
/* draw the glasses */ |
|
161 |
{ |
|
162 |
uint val = (face >> 28) & 7; |
|
163 |
||
164 |
if (!(flag&2)) { |
|
165 |
if (val<=1) { |
|
166 |
DrawSprite(0x347 + val, x, y); |
|
167 |
} |
|
168 |
} else { |
|
169 |
if (val<=1) { |
|
170 |
DrawSprite(0x3AE + val, x, y); |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
void InvalidatePlayerWindows(Player *p) |
|
177 |
{ |
|
178 |
uint pid = p->index; |
|
179 |
if ( (byte)pid == _local_player) |
|
180 |
InvalidateWindow(WC_STATUS_BAR, 0); |
|
181 |
||
182 |
InvalidateWindow(WC_FINANCES, pid); |
|
183 |
} |
|
184 |
||
185 |
bool CheckPlayerHasMoney(int32 cost) |
|
186 |
{ |
|
187 |
if (cost > 0) { |
|
188 |
uint pid = _current_player; |
|
189 |
if (pid < MAX_PLAYERS && cost > DEREF_PLAYER(pid)->player_money) { |
|
190 |
SET_DPARAM32(0, cost); |
|
191 |
_error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES; |
|
192 |
return false; |
|
193 |
} |
|
194 |
} |
|
195 |
return true; |
|
196 |
} |
|
197 |
||
198 |
static void SubtractMoneyFromAnyPlayer(Player *p, int32 cost) |
|
199 |
{ |
|
200 |
p->money64 -= cost; |
|
201 |
UpdatePlayerMoney32(p); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
202 |
|
0 | 203 |
p->yearly_expenses[0][_yearly_expenses_type] += cost; |
204 |
||
205 |
if ( ( 1 << _yearly_expenses_type ) & (1<<7|1<<8|1<<9|1<<10)) |
|
206 |
p->cur_economy.income -= cost; |
|
207 |
else if (( 1 << _yearly_expenses_type ) & (1<<2|1<<3|1<<4|1<<5|1<<6|1<<11)) |
|
208 |
p->cur_economy.expenses -= cost; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
209 |
|
0 | 210 |
InvalidatePlayerWindows(p); |
211 |
} |
|
212 |
||
213 |
void SubtractMoneyFromPlayer(int32 cost) |
|
214 |
{ |
|
215 |
uint pid = _current_player; |
|
216 |
if (pid < MAX_PLAYERS) |
|
217 |
SubtractMoneyFromAnyPlayer(DEREF_PLAYER(pid), cost); |
|
218 |
} |
|
219 |
||
220 |
void SubtractMoneyFromPlayerFract(byte player, int32 cost) |
|
221 |
{ |
|
222 |
Player *p = DEREF_PLAYER(player); |
|
223 |
byte m = p->player_money_fraction; |
|
224 |
p->player_money_fraction = m - (byte)cost; |
|
225 |
cost >>= 8; |
|
226 |
if (p->player_money_fraction > m) |
|
227 |
cost++; |
|
228 |
if (cost != 0) |
|
229 |
SubtractMoneyFromAnyPlayer(p, cost); |
|
230 |
} |
|
231 |
||
232 |
// the player_money field is kept as it is, but money64 contains the actual amount of money. |
|
233 |
void UpdatePlayerMoney32(Player *p) |
|
234 |
{ |
|
235 |
if (p->money64 < -2000000000) |
|
236 |
p->player_money = -2000000000; |
|
237 |
else if (p->money64 > 2000000000) |
|
238 |
p->player_money = 2000000000; |
|
239 |
else |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
240 |
p->player_money = (int32)p->money64; |
0 | 241 |
} |
242 |
||
243 |
void GetNameOfOwner(byte owner, uint tile) |
|
244 |
{ |
|
245 |
SET_DPARAM8(2, owner); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
246 |
|
0 | 247 |
if (owner != OWNER_TOWN) { |
248 |
if (owner >= 8) |
|
249 |
SET_DPARAM16(0, STR_0150_SOMEONE); |
|
250 |
else { |
|
251 |
Player *p = DEREF_PLAYER(owner); |
|
252 |
SET_DPARAM16(0, p->name_1); |
|
253 |
SET_DPARAM32(1, p->name_2); |
|
254 |
} |
|
255 |
} else { |
|
256 |
Town *t = ClosestTownFromTile(tile, (uint)-1); |
|
257 |
SET_DPARAM16(0, t->townnametype); |
|
258 |
SET_DPARAM32(1, t->townnameparts); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
||
263 |
bool CheckOwnership(byte owner) |
|
264 |
{ |
|
265 |
assert(owner <= OWNER_WATER); |
|
266 |
||
267 |
if (owner == _current_player) |
|
268 |
return true; |
|
269 |
_error_message = STR_013B_OWNED_BY; |
|
270 |
GetNameOfOwner(owner, 0); |
|
271 |
return false; |
|
272 |
} |
|
273 |
||
274 |
bool CheckTileOwnership(uint tile) |
|
275 |
{ |
|
276 |
byte owner = _map_owner[tile]; |
|
277 |
assert(owner <= OWNER_WATER); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
278 |
if (owner == _current_player) |
0 | 279 |
return true; |
280 |
_error_message = STR_013B_OWNED_BY; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
281 |
|
0 | 282 |
// no need to get the name of the owner unless we're the local player (saves some time) |
283 |
if (_current_player == _local_player) |
|
284 |
GetNameOfOwner(owner, tile); |
|
285 |
return false; |
|
286 |
} |
|
287 |
||
288 |
static void GenerateCompanyName(Player *p) |
|
289 |
{ |
|
290 |
uint tile; |
|
291 |
Town *t; |
|
292 |
StringID str; |
|
293 |
Player *pp; |
|
294 |
uint32 strp; |
|
295 |
char buffer[100]; |
|
296 |
||
297 |
if (p->name_1 != STR_SV_UNNAMED) |
|
298 |
return; |
|
299 |
||
300 |
tile = p->last_build_coordinate; |
|
301 |
if (tile == 0) |
|
302 |
return; |
|
303 |
||
304 |
t = ClosestTownFromTile(tile, (uint)-1); |
|
305 |
||
306 |
if (IS_INT_INSIDE(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST+1)) { |
|
307 |
str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START; |
|
308 |
strp = t->townnameparts; |
|
309 |
||
310 |
verify_name:; |
|
311 |
// No player must have this name already |
|
312 |
FOR_ALL_PLAYERS(pp) { |
|
313 |
if (pp->name_1 == str && pp->name_2 == strp) |
|
314 |
goto bad_town_name; |
|
315 |
} |
|
316 |
||
317 |
GetString(buffer, str); |
|
318 |
if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 150) |
|
319 |
goto bad_town_name; |
|
320 |
||
321 |
set_name:; |
|
322 |
p->name_1 = str; |
|
323 |
p->name_2 = strp; |
|
324 |
||
325 |
MarkWholeScreenDirty(); |
|
326 |
||
327 |
if (!IS_HUMAN_PLAYER(p->index)) { |
|
328 |
SET_DPARAM16(0, t->index); |
|
329 |
AddNewsItem(p->index + (4 << 4), NEWS_FLAGS(NM_CALLBACK, NF_TILE, NT_COMPANY_INFO, DNC_BANKRUPCY), p->last_build_coordinate, 0); |
|
330 |
} |
|
331 |
return; |
|
332 |
} |
|
333 |
bad_town_name:; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
334 |
|
0 | 335 |
if (p->president_name_1 == SPECSTR_PRESIDENT_NAME) { |
336 |
str = SPECSTR_ANDCO_NAME; |
|
337 |
strp = p->president_name_2; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
338 |
goto set_name; |
0 | 339 |
} else { |
340 |
str = SPECSTR_ANDCO_NAME; |
|
341 |
strp = Random(); |
|
342 |
goto verify_name; |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
#define COLOR_SWAP(i,j) do { byte t=colors[i];colors[i]=colors[j];colors[j]=t; } while(0) |
|
347 |
||
348 |
static const byte _color_sort[16] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1}; |
|
349 |
static const byte _color_similar_1[16] = {8, 6, 255, 12, 255, 0, 1, 1, 0, 13, 11, 10, 3, 9, 15, 14}; |
|
350 |
static const byte _color_similar_2[16] = {5, 7, 255, 255, 255, 8, 7, 6, 5, 12, 255, 255, 9, 255, 255, 255}; |
|
351 |
||
352 |
static byte GeneratePlayerColor() |
|
353 |
{ |
|
354 |
byte colors[16], pcolor, t2; |
|
355 |
int i,j,n; |
|
356 |
uint32 r; |
|
357 |
Player *p; |
|
358 |
||
359 |
// Initialize array |
|
360 |
for(i=0; i!=16; i++) |
|
361 |
colors[i] = i; |
|
362 |
||
363 |
// And randomize it |
|
364 |
n = 100; |
|
365 |
do { |
|
366 |
r = Random(); |
|
367 |
COLOR_SWAP(r & 0xF, (r >> 4) & 0xF); |
|
368 |
} while (--n); |
|
369 |
||
370 |
// Bubble sort it according to the values in table 1 |
|
371 |
i = 16; |
|
372 |
do { |
|
373 |
for(j=0; j!=15; j++) { |
|
374 |
if (_color_sort[colors[j]] < _color_sort[colors[j+1]]) { |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
375 |
COLOR_SWAP(j,j+1); |
0 | 376 |
} |
377 |
} |
|
378 |
} while (--i); |
|
379 |
||
380 |
// Move the colors that look similar to each player's color to the side |
|
381 |
FOR_ALL_PLAYERS(p) if (p->is_active) { |
|
382 |
pcolor = p->player_color; |
|
383 |
for(i=0; i!=16; i++) if (colors[i] == pcolor) { |
|
384 |
colors[i] = 0xFF; |
|
385 |
||
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
386 |
t2 = _color_similar_1[pcolor]; |
0 | 387 |
if (t2 == 0xFF) break; |
388 |
for(i=0; i!=15; i++) { |
|
389 |
if (colors[i] == t2) { |
|
390 |
do COLOR_SWAP(i,i+1); while (++i != 15); |
|
391 |
break; |
|
392 |
} |
|
393 |
} |
|
394 |
||
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
395 |
t2 = _color_similar_2[pcolor]; |
0 | 396 |
if (t2 == 0xFF) break; |
397 |
for(i=0; i!=15; i++) { |
|
398 |
if (colors[i] == t2) { |
|
399 |
do COLOR_SWAP(i,i+1); while (++i != 15); |
|
400 |
break; |
|
401 |
} |
|
402 |
} |
|
403 |
break; |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
// Return the first available color |
|
408 |
i = 0; |
|
409 |
for(;;) { |
|
410 |
if (colors[i] != 0xFF) |
|
411 |
return colors[i]; |
|
412 |
i++; |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
static void GeneratePresidentName(Player *p) |
|
417 |
{ |
|
418 |
Player *pp; |
|
419 |
char buffer[100], buffer2[40]; |
|
420 |
||
421 |
for(;;) { |
|
422 |
restart:; |
|
423 |
||
424 |
p->president_name_2 = Random(); |
|
425 |
p->president_name_1 = SPECSTR_PRESIDENT_NAME; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
426 |
|
0 | 427 |
SET_DPARAM32(0, p->president_name_2); |
428 |
GetString(buffer, p->president_name_1); |
|
429 |
if (strlen(buffer) >= 32 || GetStringWidth(buffer) >= 94) |
|
430 |
continue; |
|
431 |
||
432 |
FOR_ALL_PLAYERS(pp) { |
|
433 |
if (pp->is_active && p != pp) { |
|
434 |
SET_DPARAM32(0, pp->president_name_2); |
|
435 |
GetString(buffer2, pp->president_name_1); |
|
436 |
if (str_eq(buffer2, buffer)) |
|
437 |
goto restart; |
|
438 |
} |
|
439 |
} |
|
440 |
return; |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
extern int GetPlayerMaxRailtype(int p); |
|
445 |
||
446 |
static Player *AllocatePlayer() |
|
447 |
{ |
|
448 |
Player *p; |
|
449 |
// Find a free slot |
|
450 |
FOR_ALL_PLAYERS(p) { |
|
451 |
if (!p->is_active) { |
|
452 |
int i = p->index; |
|
453 |
memset(p, 0, sizeof(Player)); |
|
454 |
p->index = i; |
|
455 |
return p; |
|
456 |
} |
|
457 |
} |
|
458 |
return NULL; |
|
459 |
} |
|
460 |
||
461 |
Player *DoStartupNewPlayer(bool is_ai) |
|
462 |
{ |
|
463 |
Player *p; |
|
464 |
int index; |
|
465 |
||
466 |
p = AllocatePlayer(); |
|
467 |
if (p == NULL) return NULL; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
468 |
|
0 | 469 |
index = p->index; |
470 |
||
471 |
// Make a color |
|
472 |
p->player_color = GeneratePlayerColor(); |
|
473 |
_player_colors[index] = p->player_color; |
|
474 |
p->name_1 = STR_SV_UNNAMED; |
|
475 |
p->is_active = true; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
476 |
|
0 | 477 |
p->money64 = p->player_money = p->current_loan = 100000; |
478 |
||
479 |
p->is_ai = is_ai; |
|
480 |
p->ai.state = 5; /* AIS_WANT_NEW_ROUTE */ |
|
481 |
p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = 0xFF; |
|
482 |
||
483 |
p->max_railtype = GetPlayerMaxRailtype(index); |
|
484 |
p->inaugurated_year = _cur_year; |
|
485 |
p->face = Random(); |
|
486 |
||
487 |
GeneratePresidentName(p); |
|
488 |
||
489 |
InvalidateWindow(WC_GRAPH_LEGEND, 0); |
|
490 |
InvalidateWindow(WC_TOOLBAR_MENU, 0); |
|
491 |
||
492 |
return p; |
|
493 |
} |
|
494 |
||
495 |
void StartupPlayers() |
|
496 |
{ |
|
23 | 497 |
// The AI starts like in the setting with +2 month max |
165
f81fa8c27236
(svn r166) -Codechange: change 74 for constant DAY_TICKS
darkvater
parents:
84
diff
changeset
|
498 |
_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1; |
0 | 499 |
} |
500 |
||
501 |
static void MaybeStartNewPlayer() |
|
502 |
{ |
|
503 |
uint n; |
|
504 |
Player *p; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
505 |
|
0 | 506 |
// count number of competitors |
507 |
n = 0; |
|
508 |
for(p=_players; p!=endof(_players); p++) |
|
509 |
if (p->is_active && p->is_ai) |
|
510 |
n++; |
|
511 |
||
512 |
// when there's a lot of computers in game, the probability that a new one starts is lower |
|
23 | 513 |
if (n < (uint)_opt.diff.max_no_competitors && n < RandomRange(_opt.diff.max_no_competitors + 2)) |
0 | 514 |
DoStartupNewPlayer(true); |
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
515 |
|
23 | 516 |
// The next AI starts like the difficulty setting said, with +2 month max |
165
f81fa8c27236
(svn r166) -Codechange: change 74 for constant DAY_TICKS
darkvater
parents:
84
diff
changeset
|
517 |
_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1; |
0 | 518 |
} |
519 |
||
520 |
void InitializePlayers() |
|
521 |
{ |
|
522 |
int i; |
|
523 |
memset(_players, 0, sizeof(_players)); |
|
524 |
for(i = 0; i != MAX_PLAYERS; i++) |
|
525 |
_players[i].index=i; |
|
526 |
_cur_player_tick_index = 0; |
|
527 |
} |
|
528 |
||
529 |
void OnTick_Players() |
|
530 |
{ |
|
531 |
Player *p; |
|
532 |
||
533 |
if (_game_mode == GM_EDITOR) |
|
534 |
return; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
535 |
|
0 | 536 |
p = DEREF_PLAYER(_cur_player_tick_index); |
537 |
_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS; |
|
538 |
if (p->name_1 != 0) GenerateCompanyName(p); |
|
539 |
||
540 |
if (_game_mode != GM_MENU && !--_next_competitor_start) { |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
541 |
MaybeStartNewPlayer(); |
0 | 542 |
} |
543 |
} |
|
544 |
||
545 |
void RunOtherPlayersLoop() |
|
546 |
{ |
|
547 |
Player *p; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
548 |
|
0 | 549 |
_is_ai_player = true; |
550 |
||
551 |
FOR_ALL_PLAYERS(p) { |
|
206 | 552 |
if (p->is_active && p->is_ai) { |
0 | 553 |
_current_player = p->index; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
23
diff
changeset
|
554 |
if (_patches.ainew_active) |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
23
diff
changeset
|
555 |
AiNewDoGameLoop(p); |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
23
diff
changeset
|
556 |
else |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
23
diff
changeset
|
557 |
AiDoGameLoop(p); |
0 | 558 |
} |
559 |
} |
|
560 |
||
561 |
_is_ai_player = false; |
|
206 | 562 |
_current_player = OWNER_NONE; |
0 | 563 |
} |
564 |
||
2
104b2984cd3e
(svn r3) -[1005611] Player Window patch: When looking in dropdownlist for player stuff, it will say
darkvater
parents:
1
diff
changeset
|
565 |
// index is the next parameter in _decode_parameters to set up |
104b2984cd3e
(svn r3) -[1005611] Player Window patch: When looking in dropdownlist for player stuff, it will say
darkvater
parents:
1
diff
changeset
|
566 |
StringID GetPlayerNameString(byte player, byte index) |
0 | 567 |
{ |
2
104b2984cd3e
(svn r3) -[1005611] Player Window patch: When looking in dropdownlist for player stuff, it will say
darkvater
parents:
1
diff
changeset
|
568 |
if (IS_HUMAN_PLAYER(player) && player < MAX_PLAYERS) { |
104b2984cd3e
(svn r3) -[1005611] Player Window patch: When looking in dropdownlist for player stuff, it will say
darkvater
parents:
1
diff
changeset
|
569 |
SET_DPARAM16(index, player+1); |
104b2984cd3e
(svn r3) -[1005611] Player Window patch: When looking in dropdownlist for player stuff, it will say
darkvater
parents:
1
diff
changeset
|
570 |
return STR_7002_PLAYER; |
104b2984cd3e
(svn r3) -[1005611] Player Window patch: When looking in dropdownlist for player stuff, it will say
darkvater
parents:
1
diff
changeset
|
571 |
} |
0 | 572 |
return STR_EMPTY; |
573 |
} |
|
574 |
||
575 |
extern void ShowPlayerFinances(int player); |
|
576 |
||
577 |
void PlayersYearlyLoop() |
|
578 |
{ |
|
579 |
Player *p; |
|
580 |
||
581 |
// Copy statistics |
|
582 |
FOR_ALL_PLAYERS(p) { |
|
583 |
if (p->is_active) { |
|
584 |
memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0])); |
|
585 |
memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0])); |
|
586 |
InvalidateWindow(WC_FINANCES, p->index); |
|
587 |
} |
|
588 |
} |
|
589 |
||
1 | 590 |
if (_patches.show_finances && _local_player != OWNER_SPECTATOR) { |
0 | 591 |
ShowPlayerFinances(_local_player); |
592 |
p = DEREF_PLAYER(_local_player); |
|
593 |
if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) { |
|
594 |
SndPlayFx(0x28); |
|
595 |
} else { |
|
596 |
SndPlayFx(0x27); |
|
597 |
} |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
void DeletePlayerWindows(int pi) |
|
602 |
{ |
|
603 |
DeleteWindowById(WC_COMPANY, pi); |
|
604 |
DeleteWindowById(WC_FINANCES, pi); |
|
605 |
DeleteWindowById(WC_STATION_LIST, pi); |
|
606 |
DeleteWindowById(WC_TRAINS_LIST, pi); |
|
607 |
DeleteWindowById(WC_ROADVEH_LIST, pi); |
|
608 |
DeleteWindowById(WC_SHIPS_LIST, pi); |
|
609 |
DeleteWindowById(WC_AIRCRAFT_LIST, pi); |
|
610 |
DeleteWindowById(WC_BUY_COMPANY, pi); |
|
611 |
} |
|
612 |
||
613 |
static void DeletePlayerStuff(int pi) |
|
614 |
{ |
|
615 |
Player *p; |
|
616 |
||
617 |
DeletePlayerWindows(pi); |
|
618 |
p = DEREF_PLAYER(pi); |
|
619 |
DeleteName(p->name_1); |
|
620 |
DeleteName(p->president_name_1); |
|
621 |
p->name_1 = 0; |
|
622 |
p->president_name_1 = 0; |
|
623 |
} |
|
624 |
||
625 |
// functionality. |
|
626 |
// 0 - make new player |
|
627 |
// 1 - make new AI player |
|
628 |
// 2 - delete player (p1 >> 8) & 0xFF |
|
629 |
// 3 - join player (p1 >> 8) & 0xFF with (p1 >> 16) & 0xFF |
|
630 |
int32 CmdPlayerCtrl(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
631 |
{ |
|
632 |
int pi; |
|
633 |
Player *p; |
|
634 |
||
635 |
if (!(flags & DC_EXEC)) |
|
636 |
return 0; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
637 |
|
0 | 638 |
switch(p1 & 0xff) { |
639 |
case 0: // make new player |
|
640 |
p = DoStartupNewPlayer(false); |
|
1 | 641 |
if (_local_player == OWNER_SPECTATOR && p != NULL) { |
0 | 642 |
_local_player = p->index; |
643 |
MarkWholeScreenDirty(); |
|
644 |
} |
|
645 |
break; |
|
646 |
case 1: // make new ai player |
|
647 |
DoStartupNewPlayer(true); |
|
648 |
break; |
|
649 |
case 2: // delete player |
|
650 |
pi = (byte)(p1 >> 8); |
|
651 |
ChangeOwnershipOfPlayerItems(pi, 255); |
|
652 |
DeletePlayerStuff(pi); |
|
653 |
break; |
|
654 |
||
655 |
case 3: // join player |
|
656 |
pi = (byte)(p1 >> 8); |
|
657 |
ChangeOwnershipOfPlayerItems(pi, (byte)(p1 >> 16)); |
|
658 |
DeletePlayerStuff(pi); |
|
659 |
break; |
|
660 |
} |
|
661 |
||
662 |
||
663 |
return 0; |
|
664 |
} |
|
665 |
||
666 |
// Save/load of players |
|
667 |
static const byte _player_desc[] = { |
|
668 |
SLE_VAR(Player,name_2, SLE_UINT32), |
|
669 |
SLE_VAR(Player,name_1, SLE_STRINGID), |
|
670 |
||
671 |
SLE_VAR(Player,president_name_1,SLE_UINT16), |
|
672 |
SLE_VAR(Player,president_name_2,SLE_UINT32), |
|
673 |
||
674 |
SLE_VAR(Player,face, SLE_UINT32), |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
675 |
|
0 | 676 |
// money was changed to a 64 bit field in savegame version 1. |
677 |
SLE_CONDVAR(Player,money64, SLE_VAR_I64 | SLE_FILE_I32, 0, 0), |
|
678 |
SLE_CONDVAR(Player,money64, SLE_INT64, 1, 255), |
|
679 |
||
680 |
SLE_VAR(Player,current_loan, SLE_INT32), |
|
681 |
||
682 |
SLE_VAR(Player,player_color, SLE_UINT8), |
|
683 |
SLE_VAR(Player,player_money_fraction,SLE_UINT8), |
|
684 |
SLE_VAR(Player,max_railtype, SLE_UINT8), |
|
685 |
SLE_VAR(Player,block_preview, SLE_UINT8), |
|
686 |
||
687 |
SLE_VAR(Player,cargo_types, SLE_UINT16), |
|
688 |
SLE_VAR(Player,location_of_house,SLE_UINT16), |
|
689 |
SLE_VAR(Player,last_build_coordinate,SLE_UINT16), |
|
690 |
SLE_VAR(Player,inaugurated_year,SLE_UINT8), |
|
691 |
||
692 |
SLE_ARR(Player,share_owners, SLE_UINT8, 4), |
|
693 |
||
694 |
SLE_VAR(Player,num_valid_stat_ent,SLE_UINT8), |
|
695 |
||
696 |
SLE_VAR(Player,quarters_of_bankrupcy,SLE_UINT8), |
|
697 |
SLE_VAR(Player,bankrupt_asked, SLE_UINT8), |
|
698 |
SLE_VAR(Player,bankrupt_timeout,SLE_INT16), |
|
699 |
SLE_VAR(Player,bankrupt_value, SLE_INT32), |
|
700 |
||
701 |
// yearly expenses was changed to 64-bit in savegame version 2. |
|
702 |
SLE_CONDARR(Player,yearly_expenses, SLE_FILE_I32|SLE_VAR_I64, 3*13, 0, 1), |
|
703 |
SLE_CONDARR(Player,yearly_expenses, SLE_INT64, 3*13, 2, 255), |
|
704 |
||
705 |
SLE_CONDVAR(Player,is_ai, SLE_UINT8, 2, 255), |
|
706 |
SLE_CONDVAR(Player,is_active, SLE_UINT8, 4, 255), |
|
707 |
||
708 |
// reserve extra space in savegame here. (currently 64 bytes) |
|
709 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 8, 2, 255), |
|
710 |
||
711 |
SLE_END() |
|
712 |
}; |
|
713 |
||
714 |
static const byte _player_economy_desc[] = { |
|
715 |
// these were changed to 64-bit in savegame format 2 |
|
716 |
SLE_CONDVAR(PlayerEconomyEntry,income, SLE_INT32, 0, 1), |
|
717 |
SLE_CONDVAR(PlayerEconomyEntry,expenses, SLE_INT32, 0, 1), |
|
718 |
SLE_CONDVAR(PlayerEconomyEntry,company_value, SLE_INT32, 0, 1), |
|
719 |
SLE_CONDVAR(PlayerEconomyEntry,income, SLE_FILE_I64 | SLE_VAR_I32, 2, 255), |
|
720 |
SLE_CONDVAR(PlayerEconomyEntry,expenses,SLE_FILE_I64 | SLE_VAR_I32, 2, 255), |
|
721 |
SLE_CONDVAR(PlayerEconomyEntry,company_value,SLE_FILE_I64 | SLE_VAR_I32, 2, 255), |
|
722 |
||
723 |
SLE_VAR(PlayerEconomyEntry,delivered_cargo, SLE_INT32), |
|
724 |
SLE_VAR(PlayerEconomyEntry,performance_history, SLE_INT32), |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
725 |
|
0 | 726 |
SLE_END() |
727 |
}; |
|
728 |
||
729 |
static const byte _player_ai_desc[] = { |
|
730 |
SLE_VAR(PlayerAI,state, SLE_UINT8), |
|
731 |
SLE_VAR(PlayerAI,tick, SLE_UINT8), |
|
732 |
SLE_VAR(PlayerAI,state_counter, SLE_UINT16), |
|
733 |
SLE_VAR(PlayerAI,timeout_counter, SLE_UINT16), |
|
734 |
||
735 |
SLE_VAR(PlayerAI,state_mode, SLE_UINT8), |
|
736 |
SLE_VAR(PlayerAI,banned_tile_count, SLE_UINT8), |
|
737 |
SLE_VAR(PlayerAI,railtype_to_use, SLE_UINT8), |
|
738 |
||
739 |
SLE_VAR(PlayerAI,cargo_type, SLE_UINT8), |
|
740 |
SLE_VAR(PlayerAI,num_wagons, SLE_UINT8), |
|
741 |
SLE_VAR(PlayerAI,build_kind, SLE_UINT8), |
|
742 |
SLE_VAR(PlayerAI,num_build_rec, SLE_UINT8), |
|
743 |
SLE_VAR(PlayerAI,num_loco_to_build, SLE_UINT8), |
|
744 |
SLE_VAR(PlayerAI,num_want_fullload, SLE_UINT8), |
|
745 |
||
746 |
SLE_VAR(PlayerAI,route_type_mask, SLE_UINT8), |
|
747 |
||
748 |
SLE_VAR(PlayerAI,start_tile_a, SLE_UINT16), |
|
749 |
SLE_VAR(PlayerAI,cur_tile_a, SLE_UINT16), |
|
750 |
SLE_VAR(PlayerAI,start_dir_a, SLE_UINT8), |
|
751 |
SLE_VAR(PlayerAI,cur_dir_a, SLE_UINT8), |
|
752 |
||
753 |
SLE_VAR(PlayerAI,start_tile_b, SLE_UINT16), |
|
754 |
SLE_VAR(PlayerAI,cur_tile_b, SLE_UINT16), |
|
755 |
SLE_VAR(PlayerAI,start_dir_b, SLE_UINT8), |
|
756 |
SLE_VAR(PlayerAI,cur_dir_b, SLE_UINT8), |
|
757 |
||
758 |
SLE_REF(PlayerAI,cur_veh, REF_VEHICLE), |
|
759 |
||
760 |
SLE_ARR(PlayerAI,wagon_list, SLE_UINT16, 9), |
|
761 |
SLE_ARR(PlayerAI,order_list_blocks, SLE_UINT8, 20), |
|
762 |
SLE_ARR(PlayerAI,banned_tiles, SLE_UINT16, 16), |
|
763 |
||
764 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 8, 2, 255), |
|
765 |
SLE_END() |
|
766 |
}; |
|
767 |
||
768 |
static const byte _player_ai_build_rec_desc[] = { |
|
769 |
SLE_VAR(AiBuildRec,spec_tile, SLE_UINT16), |
|
770 |
SLE_VAR(AiBuildRec,use_tile, SLE_UINT16), |
|
771 |
SLE_VAR(AiBuildRec,rand_rng, SLE_UINT8), |
|
772 |
SLE_VAR(AiBuildRec,cur_building_rule,SLE_UINT8), |
|
773 |
SLE_VAR(AiBuildRec,unk6, SLE_UINT8), |
|
774 |
SLE_VAR(AiBuildRec,unk7, SLE_UINT8), |
|
775 |
SLE_VAR(AiBuildRec,buildcmd_a, SLE_UINT8), |
|
776 |
SLE_VAR(AiBuildRec,buildcmd_b, SLE_UINT8), |
|
777 |
SLE_VAR(AiBuildRec,direction, SLE_UINT8), |
|
778 |
SLE_VAR(AiBuildRec,cargo, SLE_UINT8), |
|
779 |
SLE_END() |
|
780 |
}; |
|
781 |
||
782 |
static void SaveLoad_PLYR(Player *p) { |
|
783 |
int i; |
|
784 |
||
785 |
SlObject(p, _player_desc); |
|
786 |
||
787 |
// Write AI? |
|
788 |
if (!IS_HUMAN_PLAYER(p->index)) { |
|
789 |
SlObject(&p->ai, _player_ai_desc); |
|
790 |
for(i=0; i!=p->ai.num_build_rec; i++) |
|
791 |
SlObject(&p->ai.src + i, _player_ai_build_rec_desc); |
|
792 |
} |
|
793 |
||
794 |
// Write economy |
|
795 |
SlObject(&p->cur_economy, _player_economy_desc); |
|
796 |
||
797 |
// Write old economy entries. |
|
798 |
{ |
|
799 |
PlayerEconomyEntry *pe; |
|
800 |
for(i=p->num_valid_stat_ent,pe=p->old_economy; i!=0; i--,pe++) |
|
801 |
SlObject(pe, _player_economy_desc); |
|
802 |
} |
|
803 |
} |
|
804 |
||
805 |
static void Save_PLYR() |
|
806 |
{ |
|
807 |
Player *p; |
|
808 |
FOR_ALL_PLAYERS(p) { |
|
809 |
if (p->is_active) { |
|
810 |
SlSetArrayIndex(p->index); |
|
811 |
SlAutolength((AutolengthProc*)SaveLoad_PLYR, p); |
|
812 |
} |
|
813 |
} |
|
814 |
} |
|
815 |
||
816 |
static void Load_PLYR() |
|
817 |
{ |
|
818 |
int index; |
|
819 |
while ((index = SlIterateArray()) != -1) { |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
165
diff
changeset
|
820 |
Player *p = DEREF_PLAYER(index); |
0 | 821 |
p->is_ai = (index != 0); |
822 |
SaveLoad_PLYR(p); |
|
823 |
_player_colors[index] = p->player_color; |
|
824 |
UpdatePlayerMoney32(p); |
|
825 |
} |
|
826 |
} |
|
827 |
||
828 |
const ChunkHandler _player_chunk_handlers[] = { |
|
829 |
{ 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST}, |
|
830 |
}; |
|
831 |