author | truelight |
Fri, 10 Sep 2004 19:02:27 +0000 | |
changeset 193 | 0a7025304867 |
parent 185 | 646403e35006 |
child 206 | 7f8c26d8526b |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
||
3 |
#define VARDEF |
|
4 |
#include "ttd.h" |
|
5 |
#include "gfx.h" |
|
6 |
#include "gui.h" |
|
7 |
#include "station.h" |
|
8 |
#include "vehicle.h" |
|
9 |
#include "viewport.h" |
|
10 |
#include "window.h" |
|
11 |
#include "player.h" |
|
12 |
#include "command.h" |
|
13 |
#include "town.h" |
|
14 |
#include "industry.h" |
|
15 |
#include "news.h" |
|
16 |
#include "engine.h" |
|
17 |
#include "sound.h" |
|
18 |
#include "economy.h" |
|
19 |
#include "fileio.h" |
|
20 |
#include "hal.h" |
|
21 |
#include "airport.h" |
|
22 |
#include "saveload.h" |
|
139 | 23 |
#include "ai.h" |
126 | 24 |
#include "console.h" |
0 | 25 |
|
26 |
#include <stdarg.h> |
|
27 |
||
28 |
void GameLoop(); |
|
29 |
||
30 |
void IncreaseSpriteLRU(); |
|
31 |
void InitializeGame(); |
|
32 |
void GenerateWorld(int mode); |
|
33 |
void CallLandscapeTick(); |
|
34 |
void IncreaseDate(); |
|
35 |
void RunOtherPlayersLoop(); |
|
36 |
void DoPaletteAnimations(); |
|
37 |
void MusicLoop(); |
|
38 |
void ResetMusic(); |
|
39 |
void InitializeStations(); |
|
40 |
void DeleteAllPlayerStations(); |
|
41 |
||
42 |
extern void SetDifficultyLevel(int mode, GameOptions *gm_opt); |
|
43 |
extern void DoStartupNewPlayer(bool is_ai); |
|
44 |
extern void UpdateAllSignVirtCoords(); |
|
45 |
extern void ShowOSErrorBox(const char *buf); |
|
46 |
||
47 |
void redsq_debug(int tile); |
|
48 |
bool LoadSavegame(const char *filename); |
|
49 |
||
50 |
extern void HalGameLoop(); |
|
51 |
||
52 |
uint32 _pixels_redrawn; |
|
53 |
bool _dbg_screen_rect; |
|
54 |
bool disable_computer; |
|
55 |
||
56 |
void CDECL error(const char *s, ...) { |
|
57 |
va_list va; |
|
58 |
char buf[512]; |
|
59 |
va_start(va, s); |
|
60 |
vsprintf(buf, s, va); |
|
61 |
va_end(va); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
62 |
|
0 | 63 |
ShowOSErrorBox(buf); |
64 |
if (_video_driver) |
|
65 |
_video_driver->stop(); |
|
66 |
||
67 |
assert(0); |
|
68 |
exit(1); |
|
69 |
} |
|
70 |
||
71 |
void CDECL debug(const char *s, ...) |
|
72 |
{ |
|
73 |
va_list va; |
|
74 |
char buf[1024]; |
|
75 |
va_start(va, s); |
|
76 |
vsprintf(buf, s, va); |
|
77 |
va_end(va); |
|
139 | 78 |
fprintf(stderr, "dbg: %s\n", buf); |
126 | 79 |
IConsoleDebug((byte *) &buf); |
0 | 80 |
} |
81 |
||
82 |
void CDECL ShowInfoF(const char *str, ...) |
|
83 |
{ |
|
84 |
va_list va; |
|
85 |
char buf[1024]; |
|
86 |
va_start(va, str); |
|
87 |
vsprintf(buf, str, va); |
|
88 |
va_end(va); |
|
89 |
ShowInfo(buf); |
|
90 |
} |
|
91 |
||
92 |
char * CDECL str_fmt(const char *str, ...) |
|
93 |
{ |
|
94 |
char buf[4096]; |
|
95 |
va_list va; |
|
96 |
int len; |
|
97 |
char *p; |
|
98 |
||
99 |
va_start(va, str); |
|
100 |
len = vsprintf(buf, str, va); |
|
101 |
va_end(va); |
|
102 |
p = malloc(len + 1); |
|
103 |
if (p) |
|
104 |
memcpy(p, buf, len + 1); |
|
105 |
return p; |
|
106 |
} |
|
107 |
||
108 |
||
109 |
// NULL midi driver |
|
110 |
static char *NullMidiStart(char **parm) { return NULL; } |
|
111 |
static void NullMidiStop() {} |
|
112 |
static void NullMidiPlaySong(const char *filename) {} |
|
113 |
static void NullMidiStopSong() {} |
|
114 |
static bool NullMidiIsSongPlaying() { return true; } |
|
115 |
static void NullMidiSetVolume(byte vol) {} |
|
116 |
||
117 |
const HalMusicDriver _null_music_driver = { |
|
118 |
NullMidiStart, |
|
119 |
NullMidiStop, |
|
120 |
NullMidiPlaySong, |
|
121 |
NullMidiStopSong, |
|
122 |
NullMidiIsSongPlaying, |
|
123 |
NullMidiSetVolume, |
|
124 |
}; |
|
125 |
||
126 |
// NULL video driver |
|
127 |
static void *_null_video_mem; |
|
128 |
static const char *NullVideoStart(char **parm) { |
|
129 |
_screen.width = _screen.pitch = _cur_resolution[0]; |
|
130 |
_screen.height = _cur_resolution[1]; |
|
131 |
_null_video_mem = malloc(_cur_resolution[0]*_cur_resolution[1]); |
|
132 |
return NULL; |
|
133 |
} |
|
134 |
static void NullVideoStop() { free(_null_video_mem); } |
|
135 |
static void NullVideoMakeDirty(int left, int top, int width, int height) {} |
|
136 |
static int NullVideoMainLoop() { |
|
137 |
int i = 1000; |
|
138 |
do { |
|
139 |
GameLoop(); |
|
140 |
_screen.dst_ptr = _null_video_mem; |
|
141 |
UpdateWindows(); |
|
142 |
} while (--i); |
|
143 |
return ML_QUIT; |
|
144 |
} |
|
145 |
||
146 |
static bool NullVideoChangeRes(int w, int h) { return false; } |
|
147 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
148 |
|
0 | 149 |
const HalVideoDriver _null_video_driver = { |
150 |
NullVideoStart, |
|
151 |
NullVideoStop, |
|
152 |
NullVideoMakeDirty, |
|
153 |
NullVideoMainLoop, |
|
154 |
NullVideoChangeRes, |
|
155 |
}; |
|
156 |
||
157 |
// NULL sound driver |
|
158 |
static char *NullSoundStart(char **parm) { return NULL; } |
|
159 |
static void NullSoundStop() {} |
|
160 |
const HalSoundDriver _null_sound_driver = { |
|
161 |
NullSoundStart, |
|
162 |
NullSoundStop, |
|
163 |
}; |
|
164 |
||
165 |
enum { |
|
166 |
DF_PRIORITY_MASK = 0xf, |
|
167 |
}; |
|
168 |
||
169 |
typedef struct { |
|
170 |
const DriverDesc *descs; |
|
171 |
const char *name; |
|
172 |
void *var; |
|
173 |
} DriverClass; |
|
174 |
||
175 |
static DriverClass _driver_classes[] = { |
|
176 |
{_video_driver_descs, "video", &_video_driver}, |
|
177 |
{_sound_driver_descs, "sound", &_sound_driver}, |
|
178 |
{_music_driver_descs, "music", &_music_driver}, |
|
179 |
}; |
|
180 |
||
181 |
static const DriverDesc *GetDriverByName(const DriverDesc *dd, const char *name) |
|
182 |
{ |
|
183 |
do { |
|
184 |
if (!strcmp(dd->name, name)) |
|
185 |
return dd; |
|
186 |
} while ((++dd)->name); |
|
187 |
return NULL; |
|
188 |
} |
|
189 |
||
190 |
static const DriverDesc *ChooseDefaultDriver(const DriverDesc *dd) |
|
191 |
{ |
|
192 |
const DriverDesc *best = NULL; |
|
193 |
int best_pri = -1; |
|
194 |
do { |
|
195 |
if ((int)(dd->flags&DF_PRIORITY_MASK) > best_pri) { |
|
196 |
best_pri = dd->flags&DF_PRIORITY_MASK; |
|
197 |
best = dd; |
|
198 |
} |
|
199 |
} while ((++dd)->name); |
|
200 |
return best; |
|
201 |
} |
|
202 |
||
203 |
void ttd_strlcpy(char *dst, const char *src, size_t len) |
|
204 |
{ |
|
205 |
assert(len > 0); |
|
206 |
while (--len && *src) |
|
207 |
*dst++=*src++; |
|
208 |
*dst = 0; |
|
209 |
} |
|
210 |
||
211 |
char *strecpy(char *dst, const char *src) |
|
212 |
{ |
|
213 |
while ( (*dst++ = *src++) != 0) {} |
|
214 |
return dst - 1; |
|
215 |
} |
|
216 |
||
217 |
byte *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize) |
|
218 |
{ |
|
219 |
FILE *in; |
|
220 |
void *mem; |
|
221 |
size_t len; |
|
222 |
||
223 |
in = fopen(filename, "rb"); |
|
224 |
if (in == NULL) |
|
225 |
return NULL; |
|
226 |
||
227 |
fseek(in, 0, SEEK_END); |
|
228 |
len = ftell(in); |
|
229 |
fseek(in, 0, SEEK_SET); |
|
230 |
if (len > maxsize || (mem=(byte*)malloc(len + 1)) == NULL) { |
|
231 |
fclose(in); |
|
232 |
return NULL; |
|
233 |
} |
|
234 |
((byte*)mem)[len] = 0; |
|
235 |
if (fread(mem, len, 1, in) != 1) { |
|
236 |
fclose(in); |
|
237 |
free(mem); |
|
238 |
return NULL; |
|
239 |
} |
|
240 |
fclose(in); |
|
241 |
||
242 |
*lenp = len; |
|
243 |
return mem; |
|
244 |
} |
|
245 |
||
246 |
void LoadDriver(int driver, const char *name) |
|
247 |
{ |
|
248 |
const DriverClass *dc = &_driver_classes[driver]; |
|
249 |
const DriverDesc *dd; |
|
250 |
const void **var; |
|
251 |
const void *drv; |
|
252 |
const char *err; |
|
253 |
char *parm; |
|
254 |
char buffer[256]; |
|
255 |
char *parms[32]; |
|
256 |
||
257 |
parms[0] = NULL; |
|
258 |
||
259 |
if (!*name) { |
|
260 |
dd = ChooseDefaultDriver(dc->descs); |
|
261 |
} else { |
|
262 |
// Extract the driver name and put parameter list in parm |
|
263 |
ttd_strlcpy(buffer, name, sizeof(buffer)); |
|
264 |
parm = strchr(buffer, ':'); |
|
265 |
if (parm) { |
|
266 |
int np = 0; |
|
267 |
// Tokenize the parm. |
|
268 |
do { |
|
269 |
*parm++ = 0; |
|
270 |
if (np < lengthof(parms) - 1) |
|
271 |
parms[np++] = parm; |
|
272 |
while (*parm != 0 && *parm != ',') |
|
273 |
parm++; |
|
274 |
} while (*parm == ','); |
|
275 |
parms[np] = NULL; |
|
276 |
} |
|
277 |
dd = GetDriverByName(dc->descs, buffer); |
|
278 |
if (dd == NULL) |
|
279 |
error("No such %s driver: %s\n", dc->name, buffer); |
|
280 |
} |
|
281 |
var = dc->var; |
|
282 |
if (*var != NULL) ((HalCommonDriver*)*var)->stop(); |
|
283 |
*var = NULL; |
|
284 |
drv = dd->drv; |
|
285 |
if ((err=((HalCommonDriver*)drv)->start(parms)) != NULL) |
|
286 |
error("Unable to load driver %s(%s). The error was: %s\n", dd->name, dd->longname, err); |
|
287 |
*var = drv; |
|
288 |
} |
|
289 |
||
290 |
void PrintDriverList() |
|
291 |
{ |
|
292 |
} |
|
293 |
||
294 |
static void showhelp() |
|
295 |
{ |
|
296 |
char buf[4096], *p; |
|
297 |
const DriverClass *dc = _driver_classes; |
|
298 |
const DriverDesc *dd; |
|
299 |
int i; |
|
300 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
301 |
p = strecpy(buf, |
0 | 302 |
"Command line options:\n" |
303 |
" -v drv = Set video driver (see below)\n" |
|
304 |
" -s drv = Set sound driver (see below)\n" |
|
305 |
" -m drv = Set music driver (see below)\n" |
|
306 |
" -r res = Set resolution (for instance 800x600)\n" |
|
307 |
" -h = Display this help text\n" |
|
308 |
" -t date= Set starting date\n" |
|
309 |
" -d dbg = Debug mode\n" |
|
310 |
" -l lng = Select Language\n" |
|
311 |
" -e = Start Editor\n" |
|
312 |
" -g = Start new game immediately (can optionally take a game to load)\n" |
|
313 |
" -G seed= Set random seed\n" |
|
314 |
); |
|
315 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
316 |
for(i=0; i!=lengthof(_driver_classes); i++,dc++) { |
0 | 317 |
p += sprintf(p, "List of %s drivers:\n", dc->name); |
318 |
dd = dc->descs; |
|
319 |
do { |
|
320 |
p += sprintf(p, "%10s: %s\n", dd->name, dd->longname); |
|
321 |
} while ((++dd)->name); |
|
322 |
} |
|
323 |
||
324 |
ShowInfo(buf); |
|
325 |
} |
|
326 |
||
327 |
||
328 |
char *GetDriverParam(char **parm, const char *name) |
|
329 |
{ |
|
330 |
char *p; |
|
331 |
int len = strlen(name); |
|
332 |
while ((p = *parm++) != NULL) { |
|
333 |
if (!strncmp(p,name,len)) { |
|
334 |
if (p[len] == '=') return p + len + 1; |
|
335 |
if (p[len] == 0) return p + len; |
|
336 |
} |
|
337 |
} |
|
338 |
return NULL; |
|
339 |
} |
|
340 |
||
341 |
bool GetDriverParamBool(char **parm, const char *name) |
|
342 |
{ |
|
343 |
char *p = GetDriverParam(parm, name); |
|
344 |
return p != NULL; |
|
345 |
} |
|
346 |
||
347 |
int GetDriverParamInt(char **parm, const char *name, int def) |
|
348 |
{ |
|
349 |
char *p = GetDriverParam(parm, name); |
|
350 |
return p != NULL ? atoi(p) : def; |
|
351 |
} |
|
352 |
||
353 |
typedef struct { |
|
354 |
char *opt; |
|
355 |
int numleft; |
|
356 |
char **argv; |
|
357 |
const char *options; |
|
358 |
char *cont; |
|
359 |
} MyGetOptData; |
|
360 |
||
361 |
static void MyGetOptInit(MyGetOptData *md, int argc, char **argv, const char *options) |
|
362 |
{ |
|
363 |
md->cont = NULL; |
|
364 |
md->numleft = argc; |
|
365 |
md->argv = argv; |
|
366 |
md->options = options; |
|
367 |
} |
|
368 |
||
369 |
static int MyGetOpt(MyGetOptData *md) |
|
370 |
{ |
|
371 |
char *s,*r,*t; |
|
372 |
||
373 |
if ((s=md->cont) != NULL) |
|
374 |
goto md_continue_here; |
|
375 |
||
376 |
while(true) { |
|
377 |
if (--md->numleft < 0) |
|
378 |
return -1; |
|
379 |
||
380 |
s = *md->argv++; |
|
381 |
if (*s == '-') { |
|
382 |
md_continue_here:; |
|
383 |
s++; |
|
384 |
if (*s != 0) { |
|
385 |
// Found argument, try to locate it in options. |
|
386 |
if (*s == ':' || (r = strchr(md->options, *s)) == NULL) { |
|
387 |
// ERROR! |
|
388 |
return -2; |
|
389 |
} |
|
390 |
if (r[1] == ':') { |
|
391 |
// Item wants an argument. Check if the argument follows, or if it comes as a separate arg. |
|
392 |
if (!*(t = s + 1)) { |
|
393 |
// It comes as a separate arg. Check if out of args? |
|
394 |
if (--md->numleft < 0 || *(t = *md->argv) == '-') { |
|
395 |
// Check if item is optional? |
|
396 |
if (r[2] != ':') |
|
397 |
return -2; |
|
398 |
md->numleft++; |
|
399 |
t = NULL; |
|
400 |
} else { |
|
401 |
md->argv++; |
|
402 |
} |
|
403 |
} |
|
404 |
md->opt = t; |
|
405 |
md->cont = NULL; |
|
406 |
return *s; |
|
407 |
} |
|
408 |
md->opt = NULL; |
|
409 |
md->cont = s; |
|
410 |
return *s; |
|
411 |
} |
|
412 |
} else { |
|
413 |
// This is currently not supported. |
|
414 |
return -2; |
|
415 |
} |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
void SetDebugString(const char *s) |
|
420 |
{ |
|
421 |
int v; |
|
422 |
char *end; |
|
423 |
const char *t; |
|
424 |
int *p; |
|
425 |
||
426 |
// global debugging level? |
|
427 |
if (*s >= '0' && *s <= '9') { |
|
428 |
v = strtoul(s, &end, 0); |
|
429 |
s = end; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
430 |
|
0 | 431 |
_debug_spritecache_level = v; |
432 |
_debug_misc_level = v; |
|
65
f9f866bc609c
(svn r66) -Fix Station list updated on station deletion/station rename
darkvater
parents:
8
diff
changeset
|
433 |
_debug_grf_level = v; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
67
diff
changeset
|
434 |
_debug_ai_level = v; |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
172
diff
changeset
|
435 |
_debug_net_level = v; |
0 | 436 |
} |
437 |
||
438 |
// individual levels |
|
439 |
for(;;) { |
|
440 |
// skip delimiters |
|
441 |
while (*s == ' ' || *s == ',' || *s == '\t') s++; |
|
442 |
if (*s == 0) break; |
|
443 |
||
444 |
t = s; |
|
445 |
while (*s >= 'a' && *s <= 'z') s++; |
|
446 |
||
447 |
#define IS_LVL(x) (s - t == sizeof(x)-1 && !memcmp(t, x, sizeof(x)-1)) |
|
448 |
// check debugging levels |
|
449 |
if IS_LVL("misc") p = &_debug_misc_level; |
|
450 |
else if IS_LVL("spritecache") p = &_debug_spritecache_level; |
|
65
f9f866bc609c
(svn r66) -Fix Station list updated on station deletion/station rename
darkvater
parents:
8
diff
changeset
|
451 |
else if IS_LVL("grf") p = &_debug_grf_level; |
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
67
diff
changeset
|
452 |
else if IS_LVL("ai") p = &_debug_ai_level; |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
172
diff
changeset
|
453 |
else if IS_LVL("net") p = &_debug_net_level; |
0 | 454 |
else { |
455 |
ShowInfoF("Unknown debug level '%.*s'", s-t, t); |
|
456 |
return; |
|
457 |
} |
|
458 |
#undef IS_LVL |
|
459 |
if (*s == '=') s++; |
|
460 |
v = strtoul(s, &end, 0); |
|
461 |
s = end; |
|
462 |
if (p) *p = v; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
463 |
} |
0 | 464 |
} |
465 |
||
466 |
void ParseResolution(int res[2], char *s) |
|
467 |
{ |
|
468 |
char *t = strchr(s, 'x'); |
|
469 |
if (t == NULL) { |
|
470 |
ShowInfoF("Invalid resolution '%s'", s); |
|
471 |
return; |
|
472 |
} |
|
473 |
||
474 |
res[0] = (strtoul(s, NULL, 0) + 7) & ~7; |
|
475 |
res[1] = (strtoul(t+1, NULL, 0) + 7) & ~7; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
476 |
} |
0 | 477 |
|
478 |
int ttd_main(int argc, char* argv[]) |
|
479 |
{ |
|
480 |
MyGetOptData mgo; |
|
481 |
int i; |
|
482 |
int network = 0; |
|
483 |
char *network_conn = NULL; |
|
484 |
char *language = NULL; |
|
485 |
char musicdriver[32], sounddriver[32], videodriver[32]; |
|
486 |
int resolution[2] = {0,0}; |
|
487 |
uint startdate = -1; |
|
488 |
_ignore_wrong_grf = false; |
|
489 |
musicdriver[0] = sounddriver[0] = videodriver[0] = 0; |
|
105 | 490 |
_networking_override=false; |
0 | 491 |
|
492 |
_game_mode = GM_MENU; |
|
493 |
_switch_mode = SM_MENU; |
|
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
168
diff
changeset
|
494 |
_switch_mode_errorstr = INVALID_STRING_ID; |
0 | 495 |
|
496 |
MyGetOptInit(&mgo, argc-1, argv+1, "m:s:v:hn::l:eit:d::r:g::G:cp:"); |
|
497 |
while ((i = MyGetOpt(&mgo)) != -1) { |
|
498 |
switch(i) { |
|
499 |
case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break; |
|
500 |
case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break; |
|
501 |
case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break; |
|
1 | 502 |
case 'n': { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
503 |
network = 1; |
105 | 504 |
_networking_override=true; |
8
9ab81ef450f2
(svn r9) Fixed a couple of warnings and minor coding issues
dominik
parents:
1
diff
changeset
|
505 |
if (mgo.opt) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
506 |
network_conn = mgo.opt; |
1 | 507 |
network++; |
508 |
} |
|
509 |
else |
|
510 |
network_conn = NULL; |
|
511 |
} break; |
|
0 | 512 |
case 'r': ParseResolution(resolution, mgo.opt); break; |
513 |
case 'l': { |
|
514 |
language = mgo.opt; |
|
515 |
} break; |
|
516 |
case 't': { |
|
517 |
startdate = atoi(mgo.opt); |
|
518 |
} break; |
|
519 |
case 'd': { |
|
520 |
#if defined(WIN32) |
|
521 |
CreateConsole(); |
|
522 |
#endif |
|
523 |
if (mgo.opt) |
|
524 |
SetDebugString(mgo.opt); |
|
525 |
} break; |
|
526 |
case 'e': _switch_mode = SM_EDITOR; break; |
|
527 |
case 'i': _ignore_wrong_grf = true; break; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
528 |
case 'g': |
0 | 529 |
if (mgo.opt) { |
530 |
strcpy(_file_to_saveload.name, mgo.opt); |
|
531 |
_switch_mode = SM_LOAD; |
|
532 |
} else |
|
533 |
_switch_mode = SM_NEWGAME; |
|
534 |
break; |
|
535 |
case 'G': |
|
536 |
_random_seed_1 = atoi(mgo.opt); |
|
537 |
break; |
|
538 |
case 'p': { |
|
539 |
int i = atoi(mgo.opt); |
|
1 | 540 |
if (IS_INT_INSIDE(i, 0, MAX_PLAYERS)) _network_playas = i + 1; |
0 | 541 |
break; |
542 |
} |
|
543 |
case -2: |
|
544 |
case 'h': |
|
545 |
showhelp(); |
|
546 |
return 0; |
|
547 |
} |
|
548 |
} |
|
549 |
||
550 |
DeterminePaths(); |
|
551 |
LoadFromConfig(); |
|
552 |
||
553 |
// override config? |
|
554 |
if (musicdriver[0]) strcpy(_ini_musicdriver, musicdriver); |
|
555 |
if (sounddriver[0]) strcpy(_ini_sounddriver, sounddriver); |
|
556 |
if (videodriver[0]) strcpy(_ini_videodriver, videodriver); |
|
557 |
if (resolution[0]) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; } |
|
558 |
if (startdate != -1) _patches.starting_date = startdate; |
|
559 |
||
105 | 560 |
// initialize network-core |
561 |
NetworkCoreInit(); |
|
562 |
||
0 | 563 |
// enumerate language files |
564 |
InitializeLanguagePacks(); |
|
565 |
||
566 |
// initialize screenshot formats |
|
567 |
InitializeScreenshotFormats(); |
|
568 |
||
105 | 569 |
// initialize airport state machines |
570 |
InitializeAirports(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
571 |
|
0 | 572 |
// Sample catalogue |
573 |
DEBUG(misc, 1) ("Loading sound effects..."); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
574 |
MxInitialize(11025, "sample.cat"); |
0 | 575 |
|
576 |
// This must be done early, since functions use the InvalidateWindow* calls |
|
139 | 577 |
InitWindowSystem(); |
0 | 578 |
|
579 |
GfxLoadSprites(); |
|
580 |
LoadStringWidthTable(); |
|
581 |
||
582 |
DEBUG(misc, 1) ("Loading drivers..."); |
|
583 |
LoadDriver(SOUND_DRIVER, _ini_sounddriver); |
|
584 |
LoadDriver(MUSIC_DRIVER, _ini_musicdriver); |
|
585 |
LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads |
|
586 |
MusicLoop(); |
|
168
79f9ed5b23e6
(svn r169) -Fix: move around sort-widgets a bit so it looks more natural
darkvater
parents:
152
diff
changeset
|
587 |
_savegame_sort_order = 1; // default sorting of savegames is by date, newest first |
0 | 588 |
|
589 |
// Default difficulty level |
|
590 |
_opt_mod_ptr = &_new_opt; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
591 |
|
0 | 592 |
// ugly hack, if diff_level is 9, it means we got no setting from the config file, so we load the default settings. |
593 |
if (_opt_mod_ptr->diff_level == 9) |
|
594 |
SetDifficultyLevel(0, _opt_mod_ptr); |
|
595 |
||
105 | 596 |
if ((network) && (_network_available)) { |
597 |
NetworkLobbyInit(); |
|
598 |
if (network_conn!=NULL) { |
|
599 |
NetworkCoreConnectGame(network_conn,_network_server_port); |
|
600 |
} else { |
|
601 |
NetworkCoreConnectGame("auto",_network_server_port); |
|
602 |
} |
|
0 | 603 |
} |
139 | 604 |
|
605 |
// initialize the ingame console |
|
606 |
IConsoleInit(); |
|
0 | 607 |
|
608 |
while (_video_driver->main_loop() == ML_SWITCHDRIVER) {} |
|
139 | 609 |
|
610 |
IConsoleFree(); |
|
0 | 611 |
|
105 | 612 |
if (_network_available) { |
613 |
// shutdown network-core |
|
614 |
NetworkCoreShutdown(); |
|
615 |
} |
|
0 | 616 |
|
617 |
_video_driver->stop(); |
|
618 |
_music_driver->stop(); |
|
619 |
_sound_driver->stop(); |
|
620 |
||
621 |
SaveToConfig(); |
|
622 |
||
105 | 623 |
// uninitialize airport state machines |
624 |
UnInitializeAirports(); |
|
0 | 625 |
|
626 |
return 0; |
|
627 |
} |
|
628 |
||
629 |
static void ShowScreenshotResult(bool b) |
|
630 |
{ |
|
631 |
if (b) { |
|
632 |
SET_DPARAM16(0, STR_SPEC_SCREENSHOT_NAME); |
|
633 |
ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0); |
|
634 |
} else { |
|
635 |
ShowErrorMessage(INVALID_STRING_ID, STR_031C_SCREENSHOT_FAILED, 0, 0); |
|
636 |
} |
|
637 |
||
638 |
} |
|
639 |
||
640 |
void LoadIntroGame() |
|
641 |
{ |
|
642 |
char filename[256]; |
|
643 |
_game_mode = GM_MENU; |
|
644 |
_display_opt |= DO_TRANS_BUILDINGS; // don't make buildings transparent in intro |
|
645 |
||
646 |
_opt_mod_ptr = &_new_opt; |
|
647 |
GfxLoadSprites(); |
|
648 |
LoadStringWidthTable(); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
649 |
|
136
78ac8de2b2b8
(svn r137) Fix: console sometime didn't open (sign_de)
dominik
parents:
126
diff
changeset
|
650 |
// Setup main window |
0 | 651 |
InitWindowSystem(); |
652 |
SetupColorsAndInitialWindow(); |
|
653 |
||
654 |
// Generate a world. |
|
655 |
sprintf(filename, "%sopntitle.dat", _path.data_dir); |
|
656 |
if (SaveOrLoad(filename, SL_LOAD) != SL_OK) |
|
657 |
GenerateWorld(1); // if failed loading, make empty world. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
658 |
|
0 | 659 |
_opt.currency = _new_opt.currency; |
660 |
||
661 |
_pause = 0; |
|
662 |
_local_player = 0; |
|
663 |
MarkWholeScreenDirty(); |
|
664 |
||
665 |
// Play main theme |
|
666 |
if (_music_driver->is_song_playing()) ResetMusic(); |
|
667 |
} |
|
668 |
||
669 |
void MakeNewGame() |
|
670 |
{ |
|
671 |
_game_mode = GM_NORMAL; |
|
672 |
||
673 |
// Copy in game options |
|
674 |
_opt_mod_ptr = &_opt; |
|
675 |
memcpy(&_opt, &_new_opt, sizeof(_opt)); |
|
676 |
||
677 |
GfxLoadSprites(); |
|
678 |
||
136
78ac8de2b2b8
(svn r137) Fix: console sometime didn't open (sign_de)
dominik
parents:
126
diff
changeset
|
679 |
// Reinitialize windows |
0 | 680 |
InitWindowSystem(); |
681 |
LoadStringWidthTable(); |
|
682 |
||
683 |
SetupColorsAndInitialWindow(); |
|
684 |
||
685 |
// Randomize world |
|
686 |
GenerateWorld(0); |
|
687 |
||
688 |
// Create a single player |
|
689 |
DoStartupNewPlayer(false); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
690 |
|
0 | 691 |
_local_player = 0; |
692 |
||
693 |
MarkWholeScreenDirty(); |
|
694 |
} |
|
695 |
||
696 |
void MakeNewEditorWorld() |
|
697 |
{ |
|
698 |
_game_mode = GM_EDITOR; |
|
699 |
||
700 |
// Copy in game options |
|
701 |
_opt_mod_ptr = &_opt; |
|
702 |
memcpy(&_opt, &_new_opt, sizeof(_opt)); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
703 |
|
0 | 704 |
GfxLoadSprites(); |
705 |
||
136
78ac8de2b2b8
(svn r137) Fix: console sometime didn't open (sign_de)
dominik
parents:
126
diff
changeset
|
706 |
// Re-init the windowing system |
0 | 707 |
InitWindowSystem(); |
708 |
||
709 |
// Create toolbars |
|
710 |
SetupColorsAndInitialWindow(); |
|
711 |
||
712 |
// Startup the game system |
|
713 |
GenerateWorld(1); |
|
714 |
||
715 |
_local_player = OWNER_NONE; |
|
716 |
MarkWholeScreenDirty(); |
|
717 |
} |
|
718 |
||
116
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
115
diff
changeset
|
719 |
void StartupPlayers(); |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
115
diff
changeset
|
720 |
void StartupDisasters(); |
ca4332a9ec1d
(svn r117) Feature: Performance details window in company league menu (TrueLight)
dominik
parents:
115
diff
changeset
|
721 |
|
0 | 722 |
void StartScenario() |
723 |
{ |
|
724 |
_game_mode = GM_NORMAL; |
|
725 |
||
726 |
// invalid type |
|
727 |
if (_file_to_saveload.mode == SL_INVALID) { |
|
728 |
printf("Savegame is obsolete or invalid format: %s\n", _file_to_saveload.name); |
|
729 |
ShowErrorMessage(_error_message, STR_4009_GAME_LOAD_FAILED, 0, 0); |
|
730 |
_game_mode = GM_MENU; |
|
731 |
return; |
|
732 |
} |
|
733 |
||
734 |
// Copy in game options |
|
735 |
_opt_mod_ptr = &_opt; |
|
736 |
memcpy(&_opt, &_new_opt, sizeof(_opt)); |
|
737 |
||
738 |
GfxLoadSprites(); |
|
739 |
||
136
78ac8de2b2b8
(svn r137) Fix: console sometime didn't open (sign_de)
dominik
parents:
126
diff
changeset
|
740 |
// Reinitialize windows |
0 | 741 |
InitWindowSystem(); |
742 |
LoadStringWidthTable(); |
|
743 |
||
744 |
SetupColorsAndInitialWindow(); |
|
745 |
||
746 |
// Load game |
|
747 |
if (SaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode) != SL_OK) { |
|
748 |
LoadIntroGame(); |
|
749 |
ShowErrorMessage(_error_message, STR_4009_GAME_LOAD_FAILED, 0, 0); |
|
750 |
} |
|
751 |
||
115
3a3c64e7be82
(svn r116) Fix: [ 963056 ] Wrong trains you can buy with scenarios
dominik
parents:
105
diff
changeset
|
752 |
// Inititalize data |
3a3c64e7be82
(svn r116) Fix: [ 963056 ] Wrong trains you can buy with scenarios
dominik
parents:
105
diff
changeset
|
753 |
StartupPlayers(); |
3a3c64e7be82
(svn r116) Fix: [ 963056 ] Wrong trains you can buy with scenarios
dominik
parents:
105
diff
changeset
|
754 |
StartupEngines(); |
3a3c64e7be82
(svn r116) Fix: [ 963056 ] Wrong trains you can buy with scenarios
dominik
parents:
105
diff
changeset
|
755 |
StartupDisasters(); |
3a3c64e7be82
(svn r116) Fix: [ 963056 ] Wrong trains you can buy with scenarios
dominik
parents:
105
diff
changeset
|
756 |
|
139 | 757 |
// When starting a scenario, is it really a load.. |
758 |
// and in AfterLoad a player is started when it is |
|
759 |
// a scenario.. so we do not need it here. |
|
760 |
// DoStartupNewPlayer(false); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
761 |
|
0 | 762 |
_local_player = 0; |
763 |
||
764 |
MarkWholeScreenDirty(); |
|
765 |
} |
|
766 |
||
767 |
bool SafeSaveOrLoad(const char *filename, int mode, int newgm) |
|
768 |
{ |
|
769 |
byte ogm = _game_mode; |
|
770 |
int r; |
|
771 |
||
772 |
_game_mode = newgm; |
|
773 |
r = SaveOrLoad(filename, mode); |
|
774 |
if (r == SL_REINIT) { |
|
775 |
if (ogm == GM_MENU) |
|
776 |
LoadIntroGame(); |
|
777 |
else if (ogm == GM_EDITOR) |
|
778 |
MakeNewEditorWorld(); |
|
779 |
else |
|
780 |
MakeNewGame(); |
|
781 |
return false; |
|
782 |
} else if (r != SL_OK) { |
|
783 |
_game_mode = ogm; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
784 |
return false; |
0 | 785 |
} else |
786 |
return true; |
|
787 |
} |
|
788 |
||
789 |
static void SwitchMode(int new_mode) |
|
790 |
{ |
|
139 | 791 |
_in_state_game_loop = true; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
792 |
|
0 | 793 |
switch(new_mode) { |
794 |
case SM_EDITOR: // Switch to scenario editor |
|
795 |
MakeNewEditorWorld(); |
|
796 |
break; |
|
797 |
||
798 |
case SM_NEWGAME: |
|
105 | 799 |
if (_networking) { NetworkStartSync(true); } // UGLY HACK HACK HACK |
0 | 800 |
MakeNewGame(); |
801 |
break; |
|
802 |
||
803 |
normal_load: |
|
804 |
case SM_LOAD: { // Load game |
|
805 |
||
105 | 806 |
if (_networking) { NetworkStartSync(true); } // UGLY HACK HACK HACK |
0 | 807 |
|
808 |
_error_message = INVALID_STRING_ID; |
|
809 |
if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL)) { |
|
810 |
ShowErrorMessage(_error_message, STR_4009_GAME_LOAD_FAILED, 0, 0); |
|
811 |
} else { |
|
812 |
_opt_mod_ptr = &_opt; |
|
813 |
_local_player = 0; |
|
814 |
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // decrease pause counter (was increased from opening load dialog) |
|
815 |
} |
|
816 |
break; |
|
817 |
} |
|
818 |
||
819 |
case SM_LOAD_SCENARIO: { |
|
820 |
int i; |
|
821 |
||
822 |
if (_game_mode == GM_MENU) goto normal_load; |
|
823 |
||
824 |
if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR)) { |
|
825 |
_opt_mod_ptr = &_opt; |
|
826 |
||
827 |
_local_player = OWNER_NONE; |
|
828 |
_generating_world = true; |
|
829 |
// delete all players. |
|
830 |
for(i=0; i != MAX_PLAYERS; i++) { |
|
831 |
ChangeOwnershipOfPlayerItems(i, 0xff); |
|
832 |
_players[i].is_active = false; |
|
833 |
} |
|
834 |
_generating_world = false; |
|
835 |
// delete all stations owned by a player |
|
836 |
DeleteAllPlayerStations(); |
|
837 |
} else |
|
838 |
ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0); |
|
839 |
||
840 |
break; |
|
841 |
} |
|
842 |
||
843 |
||
844 |
case SM_MENU: // Switch to game menu |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
845 |
|
105 | 846 |
if ((_networking) && (!_networking_override)) NetworkCoreDisconnect(); |
847 |
_networking_override=false; |
|
848 |
||
0 | 849 |
LoadIntroGame(); |
850 |
break; |
|
851 |
||
852 |
case SM_SAVE: // Save game |
|
853 |
if (SaveOrLoad(_file_to_saveload.name, SL_SAVE) != SL_OK) |
|
854 |
ShowErrorMessage(INVALID_STRING_ID, STR_4007_GAME_SAVE_FAILED, 0, 0); |
|
855 |
else |
|
856 |
DeleteWindowById(WC_SAVELOAD, 0); |
|
857 |
break; |
|
858 |
||
859 |
case SM_GENRANDLAND: |
|
860 |
GenerateWorld(2); |
|
861 |
// XXX: set date |
|
862 |
_local_player = OWNER_NONE; |
|
863 |
MarkWholeScreenDirty(); |
|
864 |
break; |
|
865 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
866 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
867 |
if (_switch_mode_errorstr!=INVALID_STRING_ID) |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
168
diff
changeset
|
868 |
ShowErrorMessage(INVALID_STRING_ID,_switch_mode_errorstr,0,0); |
0 | 869 |
|
870 |
_in_state_game_loop = false; |
|
871 |
} |
|
872 |
||
873 |
||
874 |
// State controlling game loop. |
|
875 |
// The state must not be changed from anywhere |
|
876 |
// but here. |
|
877 |
// That check is enforced in DoCommand. |
|
878 |
void StateGameLoop() |
|
879 |
{ |
|
880 |
_in_state_game_loop = true; |
|
881 |
_frame_counter++; |
|
882 |
||
883 |
// store the random seed to be able to detect out of sync errors |
|
884 |
_sync_seed_1 = _random_seed_1; |
|
885 |
_sync_seed_2 = _random_seed_2; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
172
diff
changeset
|
886 |
if (_networking) disable_computer=true; |
0 | 887 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
888 |
if (_savedump_path[0] && (uint)_frame_counter >= _savedump_first && (uint)(_frame_counter -_savedump_first) % _savedump_freq == 0 ) { |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
889 |
char buf[100]; |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
890 |
sprintf(buf, "%s%.5d.sav", _savedump_path, _frame_counter); |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
891 |
SaveOrLoad(buf, SL_SAVE); |
0 | 892 |
if ((uint)_frame_counter >= _savedump_last) exit(1); |
893 |
} |
|
894 |
||
895 |
if (_game_mode == GM_EDITOR) { |
|
896 |
RunTileLoop(); |
|
897 |
CallVehicleTicks(); |
|
898 |
CallLandscapeTick(); |
|
899 |
CallWindowTickEvent(); |
|
900 |
NewsLoop(); |
|
901 |
} else { |
|
902 |
AnimateAnimatedTiles(); |
|
903 |
IncreaseDate(); |
|
904 |
RunTileLoop(); |
|
905 |
CallVehicleTicks(); |
|
906 |
CallLandscapeTick(); |
|
907 |
||
908 |
if (!disable_computer) |
|
909 |
RunOtherPlayersLoop(); |
|
910 |
||
911 |
CallWindowTickEvent(); |
|
912 |
NewsLoop(); |
|
913 |
} |
|
914 |
_in_state_game_loop = false; |
|
915 |
} |
|
916 |
||
917 |
static void DoAutosave() |
|
918 |
{ |
|
919 |
char buf[200]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
920 |
|
1 | 921 |
if (_patches.keep_all_autosave && _local_player != OWNER_SPECTATOR) { |
0 | 922 |
Player *p; |
923 |
char *s; |
|
924 |
sprintf(buf, "%s%s", _path.autosave_dir, PATHSEP); |
|
925 |
p = DEREF_PLAYER(_local_player); |
|
926 |
SET_DPARAM16(0, p->name_1); |
|
927 |
SET_DPARAM32(1, p->name_2); |
|
928 |
SET_DPARAM16(2, _date); |
|
929 |
s= (char*)GetString(buf + strlen(_path.autosave_dir) + strlen(PATHSEP) - 1, STR_4004); |
|
930 |
strcpy(s, ".sav"); |
|
931 |
} else { |
|
932 |
int n = _autosave_ctr; |
|
933 |
_autosave_ctr = (_autosave_ctr + 1) & 15; |
|
934 |
sprintf(buf, "%s%sautosave%d.sav", _path.autosave_dir, PATHSEP, n); |
|
935 |
} |
|
936 |
||
937 |
if (SaveOrLoad(buf, SL_SAVE) != SL_OK) |
|
938 |
ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0); |
|
939 |
} |
|
940 |
||
941 |
void GameLoop() |
|
942 |
{ |
|
943 |
int m; |
|
944 |
||
945 |
// autosave game? |
|
946 |
if (_do_autosave) { |
|
947 |
_do_autosave = false; |
|
948 |
DoAutosave(); |
|
949 |
RedrawAutosave(); |
|
950 |
} |
|
951 |
||
952 |
// make a screenshot? |
|
953 |
if ((m=_make_screenshot) != 0) { |
|
954 |
_make_screenshot = 0; |
|
955 |
switch(m) { |
|
956 |
case 1: // make small screenshot |
|
957 |
UndrawMouseCursor(); |
|
958 |
ShowScreenshotResult(MakeScreenshot()); |
|
959 |
break; |
|
960 |
case 2: // make large screenshot |
|
961 |
ShowScreenshotResult(MakeWorldScreenshot(-(TILE_X_MAX)*32, 0, TILE_X_MAX*32 + (TILE_X_MAX)*32, TILES_Y * 32, 0)); |
|
962 |
break; |
|
963 |
} |
|
964 |
} |
|
965 |
||
966 |
// switch game mode? |
|
967 |
if ((m=_switch_mode) != SM_NONE) { |
|
968 |
_switch_mode = SM_NONE; |
|
969 |
SwitchMode(m); |
|
970 |
} |
|
971 |
||
972 |
IncreaseSpriteLRU(); |
|
973 |
InteractiveRandom(); |
|
974 |
||
975 |
if (_scroller_click_timeout > 3) |
|
976 |
_scroller_click_timeout -= 3; |
|
977 |
else |
|
978 |
_scroller_click_timeout = 0; |
|
979 |
||
980 |
_caret_timer += 3; |
|
981 |
_timer_counter+=8; |
|
982 |
CursorTick(); |
|
983 |
||
105 | 984 |
// incomming packets |
985 |
NetworkCoreLoop(true); |
|
0 | 986 |
|
987 |
if (_networking_sync) { |
|
988 |
// make sure client's time is synched to the server by running frames quickly up to where the server is. |
|
989 |
if (!_networking_server) { |
|
990 |
while (_frame_counter < _frame_counter_srv) { |
|
991 |
StateGameLoop(); |
|
992 |
NetworkProcessCommands(); // need to process queue to make sure that packets get executed. |
|
993 |
} |
|
994 |
} |
|
995 |
// don't exceed the max count told by the server |
|
996 |
if (_frame_counter < _frame_counter_max) { |
|
997 |
StateGameLoop(); |
|
998 |
NetworkProcessCommands(); |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
172
diff
changeset
|
999 |
} |
0 | 1000 |
} else { |
1001 |
if (!_pause) |
|
1002 |
StateGameLoop(); |
|
1003 |
} |
|
1004 |
||
1005 |
if (!_pause && _display_opt&DO_FULL_ANIMATION) |
|
1006 |
DoPaletteAnimations(); |
|
1007 |
||
67
b2b551af3d9f
(svn r68) -Fix: [1009621] build in pause is now a cheat instead of a patch (truesatan)
darkvater
parents:
65
diff
changeset
|
1008 |
if (!_pause || _cheats.build_in_pause.value) |
0 | 1009 |
MoveAllTextEffects(); |
1010 |
||
1011 |
MouseLoop(); |
|
1012 |
||
1013 |
// send outgoing packets. |
|
105 | 1014 |
NetworkCoreLoop(false); |
1015 |
||
0 | 1016 |
|
1017 |
if (_game_mode != GM_MENU) |
|
1018 |
MusicLoop(); |
|
1019 |
} |
|
1020 |
||
1021 |
void BeforeSaveGame() |
|
1022 |
{ |
|
1023 |
Window *w = FindWindowById(WC_MAIN_WINDOW, 0); |
|
1024 |
||
1025 |
_saved_scrollpos_x = WP(w,vp_d).scrollpos_x; |
|
1026 |
_saved_scrollpos_y = WP(w,vp_d).scrollpos_y; |
|
1027 |
_saved_scrollpos_zoom = w->viewport->zoom; |
|
1028 |
} |
|
1029 |
||
1030 |
void ConvertTownOwner() |
|
1031 |
{ |
|
1032 |
uint tile; |
|
1033 |
||
1034 |
for(tile=0; tile!=TILES_X * TILES_Y; tile++) { |
|
1035 |
if (IS_TILETYPE(tile, MP_STREET)) { |
|
1036 |
if ((_map5[tile] & 0xF0) == 0x10 && _map3_lo[tile] & 0x80) |
|
1037 |
_map3_lo[tile] = OWNER_TOWN; |
|
1038 |
||
1039 |
if (_map_owner[tile] & 0x80) |
|
1040 |
_map_owner[tile] = OWNER_TOWN; |
|
1041 |
} else if (IS_TILETYPE(tile, MP_TUNNELBRIDGE)) { |
|
1042 |
if (_map_owner[tile] & 0x80) |
|
1043 |
_map_owner[tile] = OWNER_TOWN; |
|
1044 |
} |
|
1045 |
} |
|
1046 |
} |
|
1047 |
||
1048 |
// before savegame version 4, the name of the company determined if it existed |
|
1049 |
void CheckIsPlayerActive() |
|
1050 |
{ |
|
1051 |
Player *p; |
|
1052 |
FOR_ALL_PLAYERS(p) { |
|
1053 |
if (p->name_1 != 0) { |
|
1054 |
p->is_active = true; |
|
1055 |
} |
|
1056 |
} |
|
1057 |
} |
|
1058 |
||
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1059 |
// since savegame version 4.1, exclusive transport rights are stored at towns |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1060 |
void UpdateExclusiveRights() |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1061 |
{ |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1062 |
Town *t; |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1063 |
FOR_ALL_TOWNS(t) if (t->xy != 0) { |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1064 |
t->exclusivity=(byte)-1; |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1065 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
1066 |
|
123
d550772c61e6
(svn r124) Prepared code for removal of block_months variable in next major savegame version
dominik
parents:
121
diff
changeset
|
1067 |
/* FIXME old exclusive rights status is not being imported (stored in s->blocked_months_obsolete) |
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1068 |
could be implemented this way: |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1069 |
1.) Go through all stations |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1070 |
Build an array town_blocked[ town_id ][ player_id ] |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1071 |
that stores if at least one station in that town is blocked for a player |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1072 |
2.) Go through that array, if you find a town that is not blocked for |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1073 |
one player, but for all others, then give him exclusivity. |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1074 |
*/ |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1075 |
} |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1076 |
|
0 | 1077 |
extern void UpdateOldAircraft(); |
1078 |
||
1079 |
bool AfterLoadGame(uint version) |
|
1080 |
{ |
|
1081 |
Window *w; |
|
1082 |
ViewPort *vp; |
|
1083 |
||
1084 |
// in version 2.1 of the savegame, town owner was unified. |
|
1085 |
if (version <= 0x200) { |
|
1086 |
ConvertTownOwner(); |
|
1087 |
} |
|
1088 |
||
121
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1089 |
// from version 4.1 of the savegame, exclusive rights are stored at towns |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1090 |
if (version <= 0x400) { |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1091 |
UpdateExclusiveRights(); |
c2f18f4d8be1
(svn r122) Change: exclusive transport rights are now stored per town instead of per station
dominik
parents:
116
diff
changeset
|
1092 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
1093 |
|
0 | 1094 |
// convert road side to my format. |
1095 |
if (_opt.road_side) _opt.road_side = 1; |
|
1096 |
||
1097 |
// Load the sprites |
|
1098 |
GfxLoadSprites(); |
|
1099 |
||
1100 |
// Update current year |
|
1101 |
SetDate(_date); |
|
1102 |
||
1103 |
// reinit the landscape variables (landscape might have changed) |
|
1104 |
InitializeLandscapeVariables(true); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
1105 |
|
0 | 1106 |
// Update all vehicles |
1107 |
AfterLoadVehicles(); |
|
1108 |
// in version 2.2 of the savegame, we have new airports |
|
1109 |
if (version <= 0x201) { |
|
1110 |
UpdateOldAircraft(); |
|
1111 |
} |
|
1112 |
||
1113 |
UpdateAllStationVirtCoord(); |
|
1114 |
||
1115 |
// Setup town coords |
|
1116 |
AfterLoadTown(); |
|
1117 |
UpdateAllSignVirtCoords(); |
|
1118 |
||
1119 |
// make sure there is a town in the game |
|
1120 |
if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, (uint)-1)) |
|
1121 |
{ |
|
1122 |
_error_message = STR_NO_TOWN_IN_SCENARIO; |
|
1123 |
return false; |
|
1124 |
} |
|
1125 |
||
1126 |
// Initialize windows |
|
1127 |
InitWindowSystem(); |
|
1128 |
SetupColorsAndInitialWindow(); |
|
1129 |
||
1130 |
w = FindWindowById(WC_MAIN_WINDOW, 0); |
|
1131 |
||
1132 |
WP(w,vp_d).scrollpos_x = _saved_scrollpos_x; |
|
1133 |
WP(w,vp_d).scrollpos_y = _saved_scrollpos_y; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
1134 |
|
0 | 1135 |
vp = w->viewport; |
1136 |
vp->zoom = _saved_scrollpos_zoom; |
|
1137 |
vp->virtual_width = vp->width << vp->zoom; |
|
1138 |
vp->virtual_height = vp->height << vp->zoom; |
|
1139 |
||
1140 |
||
1141 |
// in version 4.0 of the savegame, is_active was introduced to determine |
|
1142 |
// if a player does exist, rather then checking name_1 |
|
1143 |
if (version <= 0x400) { |
|
1144 |
CheckIsPlayerActive(); |
|
1145 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
1146 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
185
diff
changeset
|
1147 |
// If Load Scenario / New (Scenario) Game is used, |
139 | 1148 |
// a player does not exist yet. So create one here. |
1149 |
if (!_players[0].is_active) |
|
1150 |
DoStartupNewPlayer(false); |
|
0 | 1151 |
|
152
c3964b43943e
(svn r153) -Feature: [1009710] Extra Viewport. In the minimap dropdown menu, open a new viewport to have a quick look at your favorite map-positions. Independent zoom and quick jump to/from viewport (Dribbel)
darkvater
parents:
139
diff
changeset
|
1152 |
DoZoomInOutWindow(ZOOM_NONE, w); // update button status |
0 | 1153 |
MarkWholeScreenDirty(); |
1154 |
||
1155 |
return true; |
|
1156 |
} |
|
1157 |
||
1158 |
||
1159 |
void DebugProc(int i) |
|
1160 |
{ |
|
1161 |
switch(i) { |
|
1162 |
case 0: |
|
1163 |
*(byte*)0 = 0; |
|
1164 |
break; |
|
1165 |
case 1: |
|
1166 |
DoCommandP(0, -10000000, 0, NULL, CMD_MONEY_CHEAT); |
|
1167 |
break; |
|
1168 |
case 2: |
|
1169 |
UpdateAllStationVirtCoord(); |
|
1170 |
break; |
|
1171 |
} |
|
1172 |
} |