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