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