author | signde |
Mon, 13 Sep 2004 22:49:11 +0000 | |
changeset 239 | 23958632a582 |
parent 206 | 7f8c26d8526b |
child 256 | f7c6267731fb |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "vehicle.h" |
|
4 |
#include "gfx.h" |
|
5 |
#include "assert.h" |
|
6 |
#include "saveload.h" |
|
7 |
||
8 |
extern void StartupEconomy(); |
|
9 |
extern void InitNewsItemStructs(); |
|
10 |
||
11 |
byte _name_array[512][32]; |
|
12 |
||
13 |
static INLINE uint32 ROR(uint32 x, int n) |
|
14 |
{ |
|
15 |
return (x >> n) + (x << ((sizeof(x)*8)-n)); |
|
16 |
} |
|
17 |
||
18 |
||
19 |
uint32 Random() |
|
20 |
{ |
|
206 | 21 |
if (_current_player>=MAX_PLAYERS) { |
22 |
uint32 s = _random_seeds[0][0]; |
|
23 |
uint32 t = _random_seeds[0][1]; |
|
24 |
_random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7); |
|
25 |
return _random_seeds[0][1] = ROR(s, 3); |
|
26 |
} else { |
|
27 |
uint32 s = _player_seeds[_current_player][0]; |
|
28 |
uint32 t = _player_seeds[_current_player][1]; |
|
29 |
_player_seeds[_current_player][0] = s + ROR(t ^ 0x1234567F, 7); |
|
30 |
return _player_seeds[_current_player][1] = ROR(s, 3); |
|
31 |
} |
|
0 | 32 |
} |
33 |
||
34 |
uint RandomRange(uint max) |
|
35 |
{ |
|
36 |
return (uint16)Random() * max >> 16; |
|
37 |
} |
|
38 |
||
39 |
uint32 InteractiveRandom() |
|
40 |
{ |
|
206 | 41 |
uint32 t = _random_seeds[1][1]; |
42 |
uint32 s = _random_seeds[1][0]; |
|
43 |
_random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7); |
|
44 |
return _random_seeds[1][1] = ROR(s, 3); |
|
45 |
} |
|
46 |
||
239 | 47 |
uint InteractiveRandomRange(uint max) |
48 |
{ |
|
49 |
return (uint16)InteractiveRandom() * max >> 16; |
|
50 |
} |
|
51 |
||
206 | 52 |
void InitPlayerRandoms() |
53 |
{ |
|
54 |
int i; |
|
55 |
for (i=0; i<MAX_PLAYERS; i++) { |
|
56 |
_player_seeds[i][0]=InteractiveRandom(); |
|
57 |
_player_seeds[i][1]=InteractiveRandom(); |
|
58 |
} |
|
0 | 59 |
} |
60 |
||
61 |
void memswap(void *a, void *b, size_t size) { |
|
62 |
void *c = alloca(size); |
|
63 |
memcpy(c, a, size); |
|
64 |
memcpy(a, b, size); |
|
65 |
memcpy(b, c, size); |
|
66 |
} |
|
67 |
||
68 |
void SetDate(uint date) |
|
69 |
{ |
|
70 |
YearMonthDay ymd; |
|
71 |
ConvertDayToYMD(&ymd, _date = date); |
|
72 |
_cur_year = ymd.year; |
|
73 |
_cur_month = ymd.month; |
|
74 |
} |
|
75 |
||
76 |
void InitializeClearLand(); |
|
77 |
void InitializeRail(); |
|
78 |
void InitializeRailGui(); |
|
79 |
void InitializeRoad(); |
|
80 |
void InitializeRoadGui(); |
|
81 |
void InitializeAirportGui(); |
|
82 |
void InitializeDock(); |
|
83 |
void InitializeDockGui(); |
|
84 |
void InitializeIndustries(); |
|
85 |
void InitializeLandscape(); |
|
86 |
void InitializeTowns(); |
|
87 |
void InitializeTrees(); |
|
88 |
void InitializeStations(); |
|
89 |
void InitializeNameMgr(); |
|
90 |
void InitializePlayers(); |
|
91 |
void InitializeCheats(); |
|
92 |
||
93 |
void GenerateLandscape(); |
|
94 |
void GenerateClearTile(); |
|
95 |
||
96 |
void GenerateIndustries(); |
|
97 |
void GenerateUnmovables(); |
|
98 |
void GenerateTowns(); |
|
99 |
||
100 |
void StartupPlayers(); |
|
101 |
void StartupEngines(); |
|
102 |
void StartupDisasters(); |
|
103 |
void GenerateTrees(); |
|
104 |
||
105 |
void ConvertGroundTilesIntoWaterTiles(); |
|
106 |
||
107 |
void InitializeGame() |
|
108 |
{ |
|
109 |
SetObjectToPlace(1, 0, 0, 0); |
|
110 |
||
111 |
_pause = 0; |
|
112 |
_fast_forward = 0; |
|
113 |
_tick_counter = 0; |
|
114 |
_date_fract = 0; |
|
115 |
_cur_tileloop_tile = 0; |
|
116 |
_vehicle_id_ctr_day = 0; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
117 |
|
0 | 118 |
{ |
119 |
uint starting = ConvertIntDate(_patches.starting_date); |
|
120 |
if ( starting == (uint)-1) starting = 10958; |
|
121 |
SetDate(starting); |
|
122 |
} |
|
123 |
||
124 |
InitializeVehicles(); |
|
125 |
_backup_orders_tile = 0; |
|
126 |
||
127 |
InitNewsItemStructs(); |
|
128 |
InitializeLandscape(); |
|
129 |
InitializeClearLand(); |
|
130 |
InitializeRail(); |
|
131 |
InitializeRailGui(); |
|
132 |
InitializeRoad(); |
|
133 |
InitializeRoadGui(); |
|
134 |
InitializeAirportGui(); |
|
135 |
InitializeDock(); |
|
136 |
InitializeDockGui(); |
|
137 |
InitializeTowns(); |
|
138 |
InitializeTrees(); |
|
139 |
InitializeStations(); |
|
140 |
InitializeIndustries(); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
141 |
|
0 | 142 |
InitializeNameMgr(); |
164
0cbdf3c9bde1
(svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents:
116
diff
changeset
|
143 |
InitializeVehiclesGuiList(); |
0 | 144 |
InitializeTrains(); |
145 |
||
146 |
InitializePlayers(); |
|
147 |
InitializeCheats(); |
|
148 |
||
149 |
InitTextEffects(); |
|
150 |
InitializeAnimatedTiles(); |
|
151 |
||
152 |
InitializeLandscapeVariables(false); |
|
153 |
||
154 |
ResetObjectToPlace(); |
|
155 |
} |
|
156 |
||
157 |
void GenerateWorld(int mode) |
|
158 |
{ |
|
159 |
int i; |
|
160 |
||
161 |
_generating_world = true; |
|
162 |
InitializeGame(); |
|
163 |
SetObjectToPlace(1, 0, 0, 0); |
|
164 |
||
165 |
// Must start economy early because of the costs. |
|
166 |
StartupEconomy(); |
|
167 |
||
168 |
// Don't generate landscape items when in the scenario editor. |
|
169 |
if (mode == 1) { |
|
170 |
// empty world in scenario editor |
|
171 |
ConvertGroundTilesIntoWaterTiles(); |
|
172 |
} else { |
|
173 |
GenerateLandscape(); |
|
174 |
GenerateClearTile(); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
175 |
|
0 | 176 |
// only generate towns, tree and industries in newgame mode. |
177 |
if (mode == 0) { |
|
178 |
GenerateTowns(); |
|
179 |
GenerateTrees(); |
|
180 |
GenerateIndustries(); |
|
181 |
GenerateUnmovables(); |
|
182 |
} |
|
183 |
} |
|
184 |
||
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
185 |
// These are probably pointless when inside the scenario editor. |
0 | 186 |
StartupPlayers(); |
187 |
StartupEngines(); |
|
188 |
StartupDisasters(); |
|
189 |
_generating_world = false; |
|
190 |
||
191 |
// No need to run the tile loop in the scenario editor. |
|
192 |
if (mode != 1) { |
|
193 |
for(i=0x500; i!=0; i--) |
|
194 |
RunTileLoop(); |
|
195 |
} |
|
196 |
||
197 |
ResetObjectToPlace(); |
|
198 |
} |
|
199 |
||
200 |
void DeleteName(StringID id) |
|
201 |
{ |
|
202 |
if ((id & 0xF800) == 0x7800) { |
|
203 |
memset(_name_array[id & 0x1FF], 0, sizeof(_name_array[id & 0x1FF])); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
byte *GetName(int id, byte *buff) |
|
208 |
{ |
|
209 |
byte *b; |
|
210 |
||
211 |
if (id & 0x600) { |
|
212 |
if (id & 0x200) { |
|
213 |
if (id & 0x400) { |
|
214 |
GetParamInt32(); |
|
215 |
GetParamUint16(); |
|
216 |
} else { |
|
217 |
GetParamUint16(); |
|
218 |
} |
|
219 |
} else { |
|
220 |
GetParamInt32(); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
b = _name_array[(id&~0x600)]; |
|
225 |
while ((*buff++ = *b++) != 0); |
|
226 |
||
227 |
return buff - 1; |
|
228 |
} |
|
229 |
||
230 |
||
231 |
void InitializeCheats() |
|
232 |
{ |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
233 |
memset(&_cheats, 0, sizeof(Cheats)); |
0 | 234 |
} |
235 |
||
236 |
||
237 |
void InitializeNameMgr() |
|
238 |
{ |
|
239 |
memset(_name_array, 0, sizeof(_name_array)); |
|
240 |
} |
|
241 |
||
242 |
StringID AllocateName(const byte *name, byte skip) |
|
243 |
{ |
|
244 |
int free_item = -1; |
|
245 |
const byte *names; |
|
246 |
byte *dst; |
|
247 |
int i; |
|
248 |
||
249 |
names = &_name_array[0][0]; |
|
250 |
||
251 |
for(i=0; i!=512; i++,names+=sizeof(_name_array[0])) { |
|
252 |
if (names[0] == 0) { |
|
253 |
if (free_item == -1) |
|
254 |
free_item = i; |
|
255 |
} else { |
|
256 |
if (str_eq(names, name)) { |
|
257 |
_error_message = STR_0132_CHOSEN_NAME_IN_USE_ALREADY; |
|
258 |
return 0; |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
if (free_item < 0) { |
|
264 |
_error_message = STR_0131_TOO_MANY_NAMES_DEFINED; |
|
265 |
return 0; |
|
266 |
} |
|
267 |
||
268 |
dst=_name_array[free_item]; |
|
269 |
||
270 |
for(i=0; (dst[i] = name[i]) != 0 && ++i != 32; ) {} |
|
271 |
dst[31] = 0; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
272 |
|
0 | 273 |
return free_item | 0x7800 | (skip << 8); |
274 |
} |
|
275 |
||
276 |
const TileIndexDiff _tileoffs_by_dir[4] = { |
|
277 |
TILE_XY(-1, 0), |
|
278 |
TILE_XY(0, 1), |
|
279 |
TILE_XY(1, 0), |
|
280 |
TILE_XY(0, -1), |
|
281 |
}; |
|
282 |
||
283 |
||
284 |
#define M(a,b) ((a<<5)|b) |
|
285 |
static const uint16 _month_date_from_year_day[] = { |
|
286 |
M(0,1),M(0,2),M(0,3),M(0,4),M(0,5),M(0,6),M(0,7),M(0,8),M(0,9),M(0,10),M(0,11),M(0,12),M(0,13),M(0,14),M(0,15),M(0,16),M(0,17),M(0,18),M(0,19),M(0,20),M(0,21),M(0,22),M(0,23),M(0,24),M(0,25),M(0,26),M(0,27),M(0,28),M(0,29),M(0,30),M(0,31), |
|
287 |
M(1,1),M(1,2),M(1,3),M(1,4),M(1,5),M(1,6),M(1,7),M(1,8),M(1,9),M(1,10),M(1,11),M(1,12),M(1,13),M(1,14),M(1,15),M(1,16),M(1,17),M(1,18),M(1,19),M(1,20),M(1,21),M(1,22),M(1,23),M(1,24),M(1,25),M(1,26),M(1,27),M(1,28),M(1,29), |
|
288 |
M(2,1),M(2,2),M(2,3),M(2,4),M(2,5),M(2,6),M(2,7),M(2,8),M(2,9),M(2,10),M(2,11),M(2,12),M(2,13),M(2,14),M(2,15),M(2,16),M(2,17),M(2,18),M(2,19),M(2,20),M(2,21),M(2,22),M(2,23),M(2,24),M(2,25),M(2,26),M(2,27),M(2,28),M(2,29),M(2,30),M(2,31), |
|
289 |
M(3,1),M(3,2),M(3,3),M(3,4),M(3,5),M(3,6),M(3,7),M(3,8),M(3,9),M(3,10),M(3,11),M(3,12),M(3,13),M(3,14),M(3,15),M(3,16),M(3,17),M(3,18),M(3,19),M(3,20),M(3,21),M(3,22),M(3,23),M(3,24),M(3,25),M(3,26),M(3,27),M(3,28),M(3,29),M(3,30), |
|
290 |
M(4,1),M(4,2),M(4,3),M(4,4),M(4,5),M(4,6),M(4,7),M(4,8),M(4,9),M(4,10),M(4,11),M(4,12),M(4,13),M(4,14),M(4,15),M(4,16),M(4,17),M(4,18),M(4,19),M(4,20),M(4,21),M(4,22),M(4,23),M(4,24),M(4,25),M(4,26),M(4,27),M(4,28),M(4,29),M(4,30),M(4,31), |
|
291 |
M(5,1),M(5,2),M(5,3),M(5,4),M(5,5),M(5,6),M(5,7),M(5,8),M(5,9),M(5,10),M(5,11),M(5,12),M(5,13),M(5,14),M(5,15),M(5,16),M(5,17),M(5,18),M(5,19),M(5,20),M(5,21),M(5,22),M(5,23),M(5,24),M(5,25),M(5,26),M(5,27),M(5,28),M(5,29),M(5,30), |
|
292 |
M(6,1),M(6,2),M(6,3),M(6,4),M(6,5),M(6,6),M(6,7),M(6,8),M(6,9),M(6,10),M(6,11),M(6,12),M(6,13),M(6,14),M(6,15),M(6,16),M(6,17),M(6,18),M(6,19),M(6,20),M(6,21),M(6,22),M(6,23),M(6,24),M(6,25),M(6,26),M(6,27),M(6,28),M(6,29),M(6,30),M(6,31), |
|
293 |
M(7,1),M(7,2),M(7,3),M(7,4),M(7,5),M(7,6),M(7,7),M(7,8),M(7,9),M(7,10),M(7,11),M(7,12),M(7,13),M(7,14),M(7,15),M(7,16),M(7,17),M(7,18),M(7,19),M(7,20),M(7,21),M(7,22),M(7,23),M(7,24),M(7,25),M(7,26),M(7,27),M(7,28),M(7,29),M(7,30),M(7,31), |
|
294 |
M(8,1),M(8,2),M(8,3),M(8,4),M(8,5),M(8,6),M(8,7),M(8,8),M(8,9),M(8,10),M(8,11),M(8,12),M(8,13),M(8,14),M(8,15),M(8,16),M(8,17),M(8,18),M(8,19),M(8,20),M(8,21),M(8,22),M(8,23),M(8,24),M(8,25),M(8,26),M(8,27),M(8,28),M(8,29),M(8,30), |
|
295 |
M(9,1),M(9,2),M(9,3),M(9,4),M(9,5),M(9,6),M(9,7),M(9,8),M(9,9),M(9,10),M(9,11),M(9,12),M(9,13),M(9,14),M(9,15),M(9,16),M(9,17),M(9,18),M(9,19),M(9,20),M(9,21),M(9,22),M(9,23),M(9,24),M(9,25),M(9,26),M(9,27),M(9,28),M(9,29),M(9,30),M(9,31), |
|
296 |
M(10,1),M(10,2),M(10,3),M(10,4),M(10,5),M(10,6),M(10,7),M(10,8),M(10,9),M(10,10),M(10,11),M(10,12),M(10,13),M(10,14),M(10,15),M(10,16),M(10,17),M(10,18),M(10,19),M(10,20),M(10,21),M(10,22),M(10,23),M(10,24),M(10,25),M(10,26),M(10,27),M(10,28),M(10,29),M(10,30), |
|
297 |
M(11,1),M(11,2),M(11,3),M(11,4),M(11,5),M(11,6),M(11,7),M(11,8),M(11,9),M(11,10),M(11,11),M(11,12),M(11,13),M(11,14),M(11,15),M(11,16),M(11,17),M(11,18),M(11,19),M(11,20),M(11,21),M(11,22),M(11,23),M(11,24),M(11,25),M(11,26),M(11,27),M(11,28),M(11,29),M(11,30),M(11,31), |
|
298 |
}; |
|
299 |
#undef M |
|
300 |
||
301 |
enum { |
|
302 |
ACCUM_JAN = 0, |
|
303 |
ACCUM_FEB = ACCUM_JAN + 31, |
|
304 |
ACCUM_MAR = ACCUM_FEB + 29, |
|
305 |
ACCUM_APR = ACCUM_MAR + 31, |
|
306 |
ACCUM_MAY = ACCUM_APR + 30, |
|
307 |
ACCUM_JUN = ACCUM_MAY + 31, |
|
308 |
ACCUM_JUL = ACCUM_JUN + 30, |
|
309 |
ACCUM_AUG = ACCUM_JUL + 31, |
|
310 |
ACCUM_SEP = ACCUM_AUG + 31, |
|
311 |
ACCUM_OCT = ACCUM_SEP + 30, |
|
312 |
ACCUM_NOV = ACCUM_OCT + 31, |
|
313 |
ACCUM_DEC = ACCUM_NOV + 30, |
|
314 |
}; |
|
315 |
||
316 |
static const uint16 _accum_days_for_month[] = { |
|
317 |
ACCUM_JAN,ACCUM_FEB,ACCUM_MAR,ACCUM_APR, |
|
318 |
ACCUM_MAY,ACCUM_JUN,ACCUM_JUL,ACCUM_AUG, |
|
319 |
ACCUM_SEP,ACCUM_OCT,ACCUM_NOV,ACCUM_DEC, |
|
320 |
}; |
|
321 |
||
322 |
||
323 |
void ConvertDayToYMD(YearMonthDay *ymd, uint16 date) |
|
324 |
{ |
|
325 |
uint yr = date / (365+365+365+366); |
|
326 |
uint rem = date % (365+365+365+366); |
|
327 |
uint x; |
|
328 |
||
329 |
yr *= 4; |
|
330 |
||
331 |
if (rem >= 366) { |
|
332 |
rem--; |
|
333 |
do { |
|
334 |
rem -= 365; |
|
335 |
yr++; |
|
336 |
} while (rem >= 365); |
|
337 |
if (rem >= 31+28) rem++; |
|
338 |
} |
|
339 |
||
340 |
ymd->year = yr; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
341 |
|
0 | 342 |
x = _month_date_from_year_day[rem]; |
343 |
ymd->month = x >> 5; |
|
344 |
ymd->day = x & 0x1F; |
|
345 |
} |
|
346 |
||
347 |
// year is a number between 0..? |
|
348 |
// month is a number between 0..11 |
|
349 |
// day is a number between 1..31 |
|
350 |
uint ConvertYMDToDay(uint year, uint month, uint day) |
|
351 |
{ |
|
352 |
uint rem; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
353 |
|
0 | 354 |
// day in the year |
355 |
rem = _accum_days_for_month[month] + day - 1; |
|
356 |
||
357 |
// remove feb 29 from year 1,2,3 |
|
358 |
if (year & 3) rem += (year & 3) * 365 + (rem < 31+29); |
|
359 |
||
360 |
// base date. |
|
361 |
return (year >> 2) * (365+365+365+366) + rem; |
|
362 |
} |
|
363 |
||
364 |
// convert a date on the form |
|
365 |
// 1920 - 2090 |
|
366 |
// 192001 - 209012 |
|
367 |
// 19200101 - 20901231 |
|
368 |
// or if > 2090 and below 65536, treat it as a daycount |
|
369 |
// returns -1 if no conversion was possible |
|
370 |
uint ConvertIntDate(uint date) |
|
371 |
{ |
|
372 |
uint year, month = 0, day = 1; |
|
373 |
||
374 |
if (IS_INT_INSIDE(date, 1920, 2090 + 1)) { |
|
375 |
year = date - 1920; |
|
376 |
} else if (IS_INT_INSIDE(date, 192001, 209012+1)) { |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
377 |
month = date % 100 - 1; |
0 | 378 |
year = date / 100 - 1920; |
379 |
} else if (IS_INT_INSIDE(date, 19200101, 20901231+1)) { |
|
380 |
day = date % 100; date /= 100; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
381 |
month = date % 100 - 1; |
0 | 382 |
year = date / 100 - 1920; |
383 |
} else if (IS_INT_INSIDE(date, 2091, 65536)) |
|
384 |
return date; |
|
385 |
else |
|
386 |
return (uint)-1; |
|
387 |
||
388 |
// invalid ranges? |
|
389 |
if (month >= 12 || !IS_INT_INSIDE(day, 1, 31+1)) return (uint)-1; |
|
390 |
||
391 |
return ConvertYMDToDay(year, month, day); |
|
392 |
} |
|
393 |
||
394 |
typedef struct LandscapePredefVar { |
|
395 |
StringID names[NUM_CARGO]; |
|
396 |
byte weights[NUM_CARGO]; |
|
397 |
StringID sprites[NUM_CARGO]; |
|
398 |
||
399 |
uint16 initial_cargo_payment[NUM_CARGO]; |
|
400 |
byte transit_days_table_1[NUM_CARGO]; |
|
401 |
byte transit_days_table_2[NUM_CARGO]; |
|
402 |
||
403 |
byte railwagon_by_cargo[3][NUM_CARGO]; |
|
404 |
||
405 |
byte road_veh_by_cargo_start[NUM_CARGO]; |
|
406 |
byte road_veh_by_cargo_count[NUM_CARGO]; |
|
407 |
} LandscapePredefVar; |
|
408 |
||
409 |
#include "table/landscape_const.h" |
|
410 |
||
411 |
||
412 |
// Calculate constants that depend on the landscape type. |
|
413 |
void InitializeLandscapeVariables(bool only_constants) |
|
414 |
{ |
|
415 |
const LandscapePredefVar *lpd; |
|
416 |
int i; |
|
417 |
StringID str; |
|
418 |
||
419 |
lpd = &_landscape_predef_var[_opt.landscape]; |
|
420 |
||
421 |
memcpy(_cargoc.ai_railwagon, lpd->railwagon_by_cargo, sizeof(lpd->railwagon_by_cargo)); |
|
422 |
memcpy(_cargoc.ai_roadveh_start, lpd->road_veh_by_cargo_start,sizeof(lpd->road_veh_by_cargo_start)); |
|
423 |
memcpy(_cargoc.ai_roadveh_count, lpd->road_veh_by_cargo_count,sizeof(lpd->road_veh_by_cargo_count)); |
|
424 |
||
425 |
for(i=0; i!=NUM_CARGO; i++) { |
|
426 |
_cargoc.sprites[i] = lpd->sprites[i]; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
427 |
|
0 | 428 |
str = lpd->names[i]; |
429 |
_cargoc.names_s[i] = str; |
|
430 |
_cargoc.names_p[i] = (str += 0x20); |
|
431 |
_cargoc.names_long_s[i] = (str += 0x20); |
|
432 |
_cargoc.names_long_p[i] = (str += 0x20); |
|
433 |
_cargoc.names_short[i] = (str += 0x20); |
|
434 |
_cargoc.weights[i] = lpd->weights[i]; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
435 |
|
0 | 436 |
if (!only_constants) { |
437 |
_cargo_payment_rates[i] = lpd->initial_cargo_payment[i]; |
|
438 |
_cargo_payment_rates_frac[i] = 0; |
|
439 |
} |
|
440 |
||
441 |
_cargoc.transit_days_1[i] = lpd->transit_days_table_1[i]; |
|
442 |
_cargoc.transit_days_2[i] = lpd->transit_days_table_2[i]; |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
// distance in Manhattan metric |
|
447 |
uint GetTileDist(TileIndex xy1, TileIndex xy2) |
|
448 |
{ |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
449 |
return myabs(GET_TILE_X(xy1) - GET_TILE_X(xy2)) + |
0 | 450 |
myabs(GET_TILE_Y(xy1) - GET_TILE_Y(xy2)); |
451 |
} |
|
452 |
||
453 |
// maximum distance in x _or_ y |
|
454 |
uint GetTileDist1D(TileIndex xy1, TileIndex xy2) |
|
455 |
{ |
|
456 |
return max(myabs(GET_TILE_X(xy1) - GET_TILE_X(xy2)), |
|
457 |
myabs(GET_TILE_Y(xy1) - GET_TILE_Y(xy2))); |
|
458 |
} |
|
459 |
||
460 |
uint GetTileDist1Db(TileIndex xy1, TileIndex xy2) |
|
461 |
{ |
|
462 |
int a = myabs(GET_TILE_X(xy1) - GET_TILE_X(xy2)); |
|
463 |
int b = myabs(GET_TILE_Y(xy1) - GET_TILE_Y(xy2)); |
|
464 |
||
465 |
if (a > b) |
|
466 |
return a*2+b; |
|
467 |
else |
|
468 |
return b*2+a; |
|
469 |
} |
|
470 |
||
471 |
uint GetTileDistAdv(TileIndex xy1, TileIndex xy2) |
|
472 |
{ |
|
473 |
uint a = myabs(GET_TILE_X(xy1) - GET_TILE_X(xy2)); |
|
474 |
uint b = myabs(GET_TILE_Y(xy1) - GET_TILE_Y(xy2)); |
|
475 |
return a*a+b*b; |
|
476 |
} |
|
477 |
||
478 |
bool CheckDistanceFromEdge(TileIndex tile, uint distance) |
|
479 |
{ |
|
480 |
return IS_INT_INSIDE(GET_TILE_X(tile), distance, TILE_X_MAX + 1 - distance) && |
|
481 |
IS_INT_INSIDE(GET_TILE_Y(tile), distance, TILE_Y_MAX + 1 - distance); |
|
482 |
} |
|
483 |
||
484 |
void OnNewDay_Train(Vehicle *v); |
|
485 |
void OnNewDay_RoadVeh(Vehicle *v); |
|
486 |
void OnNewDay_Aircraft(Vehicle *v); |
|
487 |
void OnNewDay_Ship(Vehicle *v); |
|
488 |
void OnNewDay_EffectVehicle(Vehicle *v) { /* empty */ } |
|
489 |
void OnNewDay_DisasterVehicle(Vehicle *v); |
|
490 |
||
491 |
typedef void OnNewVehicleDayProc(Vehicle *v); |
|
492 |
||
493 |
static OnNewVehicleDayProc * _on_new_vehicle_day_proc[] = { |
|
494 |
OnNewDay_Train, |
|
495 |
OnNewDay_RoadVeh, |
|
496 |
OnNewDay_Ship, |
|
497 |
OnNewDay_Aircraft, |
|
498 |
OnNewDay_EffectVehicle, |
|
499 |
OnNewDay_DisasterVehicle, |
|
500 |
}; |
|
501 |
||
502 |
void EnginesDailyLoop(); |
|
503 |
void DisasterDailyLoop(); |
|
504 |
void PlayersMonthlyLoop(); |
|
505 |
void EnginesMonthlyLoop(); |
|
506 |
void TownsMonthlyLoop(); |
|
507 |
void IndustryMonthlyLoop(); |
|
508 |
void StationMonthlyLoop(); |
|
509 |
||
510 |
void PlayersYearlyLoop(); |
|
511 |
void TrainsYearlyLoop(); |
|
512 |
void RoadVehiclesYearlyLoop(); |
|
513 |
void AircraftYearlyLoop(); |
|
514 |
void ShipsYearlyLoop(); |
|
515 |
||
516 |
void CheckpointsDailyLoop(); |
|
517 |
||
518 |
||
519 |
static const uint16 _autosave_months[] = { |
|
520 |
0, // never |
|
521 |
0xFFF, // every month |
|
522 |
0x249, // every 3 months |
|
523 |
0x041, // every 6 months |
|
524 |
0x001, // every 12 months |
|
525 |
}; |
|
526 |
||
527 |
void IncreaseDate() |
|
528 |
{ |
|
529 |
int i,ctr,t; |
|
530 |
YearMonthDay ymd; |
|
531 |
||
532 |
if (_game_mode == GM_MENU) { |
|
533 |
_tick_counter++; |
|
534 |
return; |
|
535 |
} |
|
536 |
||
537 |
/*if the day changed, call the vehicle event but only update a part of the vehicles |
|
538 |
old max was i!= 12. But with that and a bigger number of vehicles (2560), per day only |
|
539 |
a part of it could be done, namely: function called max_size date_fract (uint16) / 885 x 12 ==> |
|
540 |
65536 / 885 = 74; 74x12 = 888. So max 888. Any vehicles above that were not _on_new_vehicle_day_proc'd |
|
541 |
eg. aged. |
|
542 |
So new code updates it for max vehicles. |
|
543 |
(NUM_VEHICLES / maximum number of times ctr is incremented before reset ) + 1 (to get last vehicles too) |
|
544 |
max size of _date_fract / 885 (added each tick) is number of times before ctr is reset. |
|
545 |
Calculation might look complicated, but compiler just replaces it with 35, so that's ok |
|
546 |
*/ |
|
547 |
||
548 |
ctr = _vehicle_id_ctr_day; |
|
549 |
for(i=0; i!=(NUM_VEHICLES / ((1<<sizeof(_date_fract)*8) / 885)) + 1 && ctr != lengthof(_vehicles); i++) { |
|
550 |
Vehicle *v = &_vehicles[ctr++]; |
|
551 |
if ((t=v->type) != 0) |
|
552 |
_on_new_vehicle_day_proc[t - 0x10](v); |
|
553 |
} |
|
554 |
_vehicle_id_ctr_day = ctr; |
|
555 |
||
556 |
/* increase day, and check if a new day is there? */ |
|
557 |
_tick_counter++; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
558 |
|
0 | 559 |
if ( (_date_fract += 885) >= 885) |
560 |
return; |
|
561 |
||
562 |
/* yeah, increse day counter and call various daily loops */ |
|
563 |
_date++; |
|
564 |
||
105 | 565 |
NetworkGameChangeDate(_date); |
566 |
||
0 | 567 |
_vehicle_id_ctr_day = 0; |
568 |
||
569 |
DisasterDailyLoop(); |
|
570 |
CheckpointsDailyLoop(); |
|
571 |
||
572 |
if (_game_mode != GM_MENU) { |
|
573 |
InvalidateWindowWidget(WC_STATUS_BAR, 0, 0); |
|
574 |
EnginesDailyLoop(); |
|
575 |
} |
|
576 |
||
577 |
/* check if we entered a new month? */ |
|
578 |
ConvertDayToYMD(&ymd, _date); |
|
579 |
if ((byte)ymd.month == _cur_month) |
|
580 |
return; |
|
581 |
_cur_month = ymd.month; |
|
582 |
||
206 | 583 |
// printf("Month %d, %X\n", ymd.month, _random_seeds[0][0]); |
0 | 584 |
|
585 |
/* yes, call various monthly loops */ |
|
586 |
if (_game_mode != GM_MENU) { |
|
587 |
if (HASBIT(_autosave_months[_opt.autosave], _cur_month)) { |
|
588 |
_do_autosave = true; |
|
589 |
RedrawAutosave(); |
|
590 |
} |
|
591 |
||
592 |
PlayersMonthlyLoop(); |
|
593 |
EnginesMonthlyLoop(); |
|
594 |
TownsMonthlyLoop(); |
|
595 |
IndustryMonthlyLoop(); |
|
596 |
StationMonthlyLoop(); |
|
597 |
} |
|
598 |
||
599 |
/* check if we entered a new year? */ |
|
600 |
if ((byte)ymd.year == _cur_year) |
|
601 |
return; |
|
602 |
_cur_year = ymd.year; |
|
603 |
||
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
604 |
/* yes, call various yearly loops */ |
0 | 605 |
|
606 |
PlayersYearlyLoop(); |
|
607 |
TrainsYearlyLoop(); |
|
608 |
RoadVehiclesYearlyLoop(); |
|
609 |
AircraftYearlyLoop(); |
|
610 |
ShipsYearlyLoop(); |
|
611 |
||
612 |
/* check if we reached 2090, that's the maximum year. */ |
|
613 |
if (_cur_year == 171) { |
|
614 |
_cur_year = 170; |
|
615 |
_date = 62093; |
|
616 |
} |
|
617 |
||
618 |
if (_patches.auto_euro) |
|
619 |
CheckSwitchToEuro(); |
|
620 |
||
621 |
/* XXX: check if year 2050 was reached */ |
|
622 |
} |
|
623 |
||
116
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
624 |
int FindFirstBit(uint32 value) |
0 | 625 |
{ |
116
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
626 |
// This is much faster then the one that was before here. |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
627 |
// Created by Darkvater.. blame him if it is wrong ;) |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
628 |
// Btw, the macro FINDFIRSTBIT is better to use when your value is |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
629 |
// not more then 128. |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
630 |
byte i = 0; |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
631 |
if (value & 0xffff0000) { value >>= 16; i += 16; } |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
632 |
if (value & 0x0000ff00) { value >>= 8; i += 8; } |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
633 |
if (value & 0x000000f0) { value >>= 4; i += 4; } |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
634 |
if (value & 0x0000000c) { value >>= 2; i += 2; } |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
105
diff
changeset
|
635 |
if (value & 0x00000002) { i += 1; } |
0 | 636 |
return i; |
637 |
} |
|
638 |
||
639 |
||
640 |
extern uint SafeTileAdd(uint tile, int add, const char *exp, const char *file, int line) |
|
641 |
{ |
|
642 |
int x = GET_TILE_X(tile) + (signed char)(add & 0xFF); |
|
643 |
int y = GET_TILE_Y(tile) + ((((0x8080 + add)>>8) & 0xFF) - 0x80); |
|
644 |
||
645 |
if (x < 0 || y < 0 || x >= TILES_X || y >= TILES_Y) { |
|
646 |
char buf[512]; |
|
647 |
||
648 |
sprintf(buf, "TILE_ADD(%s) when adding 0x%.4X and %d failed", exp, tile, add); |
|
649 |
#if !defined(_DEBUG) || !defined(_MSC_VER) |
|
650 |
printf("%s\n", buf); |
|
651 |
#else |
|
652 |
_assert(buf, (char*)file, line); |
|
653 |
#endif |
|
654 |
} |
|
655 |
||
656 |
assert(TILE_XY(x,y) == TILE_MASK(tile + add)); |
|
657 |
||
658 |
return TILE_XY(x,y); |
|
659 |
} |
|
660 |
||
661 |
static void Save_NAME() |
|
662 |
{ |
|
663 |
int i; |
|
664 |
byte *b = _name_array[0]; |
|
665 |
||
666 |
for(i=0; i!=lengthof(_name_array); i++,b+=sizeof(_name_array[0])) { |
|
667 |
if (*b) { |
|
668 |
SlSetArrayIndex(i); |
|
669 |
SlArray(b, strlen(b), SLE_UINT8); |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
670 |
} |
0 | 671 |
} |
672 |
} |
|
673 |
||
674 |
static void Load_NAME() |
|
675 |
{ |
|
676 |
int index; |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
677 |
|
0 | 678 |
while ((index = SlIterateArray()) != -1) { |
679 |
SlArray(_name_array[index],SlGetFieldLength(),SLE_UINT8); |
|
680 |
} |
|
681 |
} |
|
682 |
||
683 |
static const byte _game_opt_desc[] = { |
|
684 |
// added a new difficulty option (town attitude) in version 4 |
|
685 |
SLE_CONDARR(GameOptions,diff, SLE_FILE_I16 | SLE_VAR_I32, 17, 0, 3), |
|
686 |
SLE_CONDARR(GameOptions,diff, SLE_FILE_I16 | SLE_VAR_I32, 18, 4, 255), |
|
687 |
SLE_VAR(GameOptions,diff_level, SLE_UINT8), |
|
688 |
SLE_VAR(GameOptions,currency, SLE_UINT8), |
|
689 |
SLE_VAR(GameOptions,kilometers, SLE_UINT8), |
|
690 |
SLE_VAR(GameOptions,town_name, SLE_UINT8), |
|
691 |
SLE_VAR(GameOptions,landscape, SLE_UINT8), |
|
692 |
SLE_VAR(GameOptions,snow_line, SLE_UINT8), |
|
693 |
SLE_VAR(GameOptions,autosave, SLE_UINT8), |
|
694 |
SLE_VAR(GameOptions,road_side, SLE_UINT8), |
|
695 |
SLE_END() |
|
696 |
}; |
|
697 |
||
698 |
// Save load game options |
|
699 |
static void SaveLoad_OPTS() |
|
700 |
{ |
|
201
c40d343115f8
(svn r202) -Codechange: I missed some files with trailing spaces.. this should be
truelight
parents:
179
diff
changeset
|
701 |
SlObject(&_opt, _game_opt_desc); |
0 | 702 |
} |
703 |
||
704 |
||
705 |
static const SaveLoadGlobVarList _date_desc[] = { |
|
706 |
{&_date, SLE_UINT16, 0, 255}, |
|
707 |
{&_date_fract, SLE_UINT16, 0, 255}, |
|
708 |
{&_tick_counter, SLE_UINT16, 0, 255}, |
|
709 |
{&_vehicle_id_ctr_day, SLE_UINT16, 0, 255}, |
|
710 |
{&_age_cargo_skip_counter, SLE_UINT8, 0, 255}, |
|
711 |
{&_avail_aircraft, SLE_UINT8, 0, 255}, |
|
712 |
{&_cur_tileloop_tile, SLE_UINT16, 0, 255}, |
|
713 |
{&_disaster_delay, SLE_UINT16, 0, 255}, |
|
714 |
{&_station_tick_ctr, SLE_UINT16, 0, 255}, |
|
206 | 715 |
{&_random_seeds[0][0], SLE_UINT32, 0, 255}, |
716 |
{&_random_seeds[0][1], SLE_UINT32, 0, 255}, |
|
0 | 717 |
{&_cur_town_ctr, SLE_UINT8, 0, 255}, |
718 |
{&_cur_player_tick_index, SLE_FILE_U8 | SLE_VAR_UINT, 0, 255}, |
|
719 |
{&_next_competitor_start, SLE_FILE_U16 | SLE_VAR_UINT, 0, 255}, |
|
720 |
{&_trees_tick_ctr, SLE_UINT8, 0, 255}, |
|
721 |
{&_pause, SLE_UINT8, 4, 255}, |
|
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
722 |
{NULL, 0, 0, 0} |
0 | 723 |
}; |
724 |
||
725 |
// Save load date related variables as well as persistent tick counters |
|
726 |
// XXX: currently some unrelated stuff is just put here |
|
727 |
static void SaveLoad_DATE() |
|
728 |
{ |
|
729 |
SlGlobList(_date_desc); |
|
730 |
} |
|
731 |
||
732 |
||
733 |
static const SaveLoadGlobVarList _view_desc[] = { |
|
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
734 |
{&_saved_scrollpos_x, SLE_FILE_I16 | SLE_VAR_INT, 0, 255}, |
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
735 |
{&_saved_scrollpos_y, SLE_FILE_I16 | SLE_VAR_INT, 0, 255}, |
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
736 |
{&_saved_scrollpos_zoom, SLE_UINT8, 0, 255}, |
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
164
diff
changeset
|
737 |
{NULL, 0, 0, 0} |
0 | 738 |
}; |
739 |
||
740 |
static void SaveLoad_VIEW() |
|
741 |
{ |
|
742 |
SlGlobList(_view_desc); |
|
743 |
} |
|
744 |
||
745 |
static void SaveLoad_MAPT() { |
|
746 |
SlArray(_map_type_and_height, lengthof(_map_type_and_height), SLE_UINT8); |
|
747 |
} |
|
748 |
||
749 |
static void SaveLoad_MAP2() { |
|
750 |
SlArray(_map2, lengthof(_map2), SLE_UINT8); |
|
751 |
} |
|
752 |
||
753 |
static void SaveLoad_M3LO() { |
|
754 |
SlArray(_map3_lo, lengthof(_map3_lo), SLE_UINT8); |
|
755 |
} |
|
756 |
||
757 |
static void SaveLoad_M3HI() { |
|
758 |
SlArray(_map3_hi, lengthof(_map3_hi), SLE_UINT8); |
|
759 |
} |
|
760 |
||
761 |
static void SaveLoad_MAPO() { |
|
762 |
SlArray(_map_owner, lengthof(_map_owner), SLE_UINT8); |
|
763 |
} |
|
764 |
||
765 |
static void SaveLoad_MAP5() { |
|
766 |
SlArray(_map5, lengthof(_map5), SLE_UINT8); |
|
767 |
} |
|
768 |
||
769 |
static void SaveLoad_MAPE() { |
|
770 |
SlArray(_map_extra_bits, lengthof(_map_extra_bits), SLE_UINT8); |
|
771 |
} |
|
772 |
||
773 |
||
774 |
static void Save_CHTS() |
|
775 |
{ |
|
776 |
byte count = sizeof(_cheats)/sizeof(Cheat); |
|
777 |
Cheat* cht = (Cheat*) &_cheats; |
|
778 |
Cheat* cht_last = &cht[count]; |
|
779 |
||
780 |
SlSetLength(count*2); |
|
781 |
for(; cht != cht_last; cht++) { |
|
782 |
SlWriteByte(cht->been_used); |
|
783 |
SlWriteByte(cht->value); |
|
784 |
} |
|
785 |
} |
|
786 |
||
787 |
static void Load_CHTS() |
|
788 |
{ |
|
789 |
Cheat* cht = (Cheat*) &_cheats; |
|
790 |
||
791 |
uint count = SlGetFieldLength()/2; |
|
792 |
for(; count; count--, cht++) |
|
793 |
{ |
|
794 |
cht->been_used = (byte)SlReadByte(); |
|
795 |
cht->value = (byte)SlReadByte(); |
|
796 |
} |
|
797 |
} |
|
798 |
||
799 |
||
800 |
const ChunkHandler _misc_chunk_handlers[] = { |
|
801 |
{ 'MAPT', SaveLoad_MAPT, SaveLoad_MAPT, CH_RIFF }, |
|
802 |
{ 'MAP2', SaveLoad_MAP2, SaveLoad_MAP2, CH_RIFF }, |
|
803 |
{ 'M3LO', SaveLoad_M3LO, SaveLoad_M3LO, CH_RIFF }, |
|
804 |
{ 'M3HI', SaveLoad_M3HI, SaveLoad_M3HI, CH_RIFF }, |
|
805 |
{ 'MAPO', SaveLoad_MAPO, SaveLoad_MAPO, CH_RIFF }, |
|
806 |
{ 'MAP5', SaveLoad_MAP5, SaveLoad_MAP5, CH_RIFF }, |
|
807 |
{ 'MAPE', SaveLoad_MAPE, SaveLoad_MAPE, CH_RIFF }, |
|
808 |
||
809 |
{ 'NAME', Save_NAME, Load_NAME, CH_ARRAY}, |
|
810 |
{ 'DATE', SaveLoad_DATE, SaveLoad_DATE, CH_RIFF}, |
|
811 |
{ 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, CH_RIFF}, |
|
812 |
{ 'OPTS', SaveLoad_OPTS, SaveLoad_OPTS, CH_RIFF}, |
|
813 |
{ 'CHTS', Save_CHTS, Load_CHTS, CH_RIFF | CH_LAST} |
|
814 |
}; |