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