2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-02-06 13:41:02 +00:00
|
|
|
#include "string.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-23 15:16:57 +00:00
|
|
|
#include "driver.h"
|
2005-07-22 06:31:31 +00:00
|
|
|
#include "saveload.h"
|
2005-02-06 08:18:00 +00:00
|
|
|
#include "strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2006-03-05 10:19:33 +00:00
|
|
|
#include "void_map.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#define VARDEF
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2006-03-30 09:08:43 +00:00
|
|
|
#include "bridge_map.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-03-12 09:38:03 +00:00
|
|
|
#include "mixer.h"
|
2005-02-10 05:43:30 +00:00
|
|
|
#include "spritecache.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "gfx.h"
|
2005-08-14 18:10:18 +00:00
|
|
|
#include "gfxinit.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "gui.h"
|
|
|
|
#include "station.h"
|
2006-03-30 09:08:43 +00:00
|
|
|
#include "station_map.h"
|
2006-04-15 18:57:58 +00:00
|
|
|
#include "town_map.h"
|
2006-03-30 09:08:43 +00:00
|
|
|
#include "tunnel_map.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "vehicle.h"
|
|
|
|
#include "viewport.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "town.h"
|
|
|
|
#include "industry.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "sound.h"
|
|
|
|
#include "economy.h"
|
|
|
|
#include "fileio.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "airport.h"
|
2004-08-24 08:34:28 +00:00
|
|
|
#include "console.h"
|
2004-11-15 19:25:59 +00:00
|
|
|
#include "screenshot.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "network.h"
|
2005-01-12 11:21:28 +00:00
|
|
|
#include "signs.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2005-03-24 17:03:37 +00:00
|
|
|
#include "waypoint.h"
|
2005-09-07 15:10:11 +00:00
|
|
|
#include "ai/ai.h"
|
2006-01-18 15:05:01 +00:00
|
|
|
#include "train.h"
|
2006-05-27 16:12:16 +00:00
|
|
|
#include "yapf/yapf.h"
|
2006-05-22 10:08:23 +00:00
|
|
|
#include "settings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2005-07-13 19:51:31 +00:00
|
|
|
void GenerateWorld(int mode, uint size_x, uint size_y);
|
2005-01-22 20:23:18 +00:00
|
|
|
void CallLandscapeTick(void);
|
|
|
|
void IncreaseDate(void);
|
|
|
|
void DoPaletteAnimations(void);
|
|
|
|
void MusicLoop(void);
|
|
|
|
void ResetMusic(void);
|
|
|
|
void InitializeStations(void);
|
|
|
|
void DeleteAllPlayerStations(void);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
|
|
|
|
extern void DoStartupNewPlayer(bool is_ai);
|
|
|
|
extern void ShowOSErrorBox(const char *buf);
|
|
|
|
|
2005-03-06 13:55:40 +00:00
|
|
|
/* TODO: usrerror() for errors which are not of an internal nature but
|
|
|
|
* caused by the user, i.e. missing files or fatal configuration errors.
|
|
|
|
* Post-0.4.0 since Celestar doesn't want this in SVN before. --pasky */
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
void CDECL error(const char *s, ...)
|
2005-11-14 19:48:04 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
va_list va;
|
|
|
|
char buf[512];
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
va_start(va, s);
|
|
|
|
vsprintf(buf, s, va);
|
|
|
|
va_end(va);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowOSErrorBox(buf);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_video_driver != NULL) _video_driver->stop();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDECL ShowInfoF(const char *str, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char buf[1024];
|
|
|
|
va_start(va, str);
|
|
|
|
vsprintf(buf, str, va);
|
|
|
|
va_end(va);
|
|
|
|
ShowInfo(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-06 08:38:09 +00:00
|
|
|
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FILE *in;
|
2005-01-23 10:54:32 +00:00
|
|
|
byte *mem;
|
2004-08-09 17:04:08 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
in = fopen(filename, "rb");
|
2005-11-14 19:48:04 +00:00
|
|
|
if (in == NULL) return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
fseek(in, 0, SEEK_END);
|
|
|
|
len = ftell(in);
|
|
|
|
fseek(in, 0, SEEK_SET);
|
2005-01-23 10:54:32 +00:00
|
|
|
if (len > maxsize || (mem = malloc(len + 1)) == NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(in);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-01-23 10:54:32 +00:00
|
|
|
mem[len] = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (fread(mem, len, 1, in) != 1) {
|
|
|
|
fclose(in);
|
|
|
|
free(mem);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
|
|
|
|
*lenp = len;
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2006-01-06 22:52:31 +00:00
|
|
|
extern const char _openttd_revision[];
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void showhelp(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
char buf[4096], *p;
|
2006-01-06 22:52:31 +00:00
|
|
|
|
|
|
|
p = buf;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-01-07 10:15:46 +00:00
|
|
|
p += sprintf(p, "OpenTTD %s\n", _openttd_revision);
|
|
|
|
p += sprintf(p,
|
2006-01-06 22:52:31 +00:00
|
|
|
"\n"
|
|
|
|
"\n"
|
2004-08-09 17:04:08 +00:00
|
|
|
"Command line options:\n"
|
2004-12-04 17:54:56 +00:00
|
|
|
" -v drv = Set video driver (see below)\n"
|
|
|
|
" -s drv = Set sound driver (see below)\n"
|
|
|
|
" -m drv = Set music driver (see below)\n"
|
|
|
|
" -r res = Set resolution (for instance 800x600)\n"
|
|
|
|
" -h = Display this help text\n"
|
|
|
|
" -t date = Set starting date\n"
|
2005-03-06 16:08:19 +00:00
|
|
|
" -d [[fac=]lvl[,...]]= Debug mode\n"
|
2004-12-04 17:54:56 +00:00
|
|
|
" -e = Start Editor\n"
|
|
|
|
" -g [savegame] = Start new/save game immediately\n"
|
|
|
|
" -G seed = Set random seed\n"
|
|
|
|
" -n [ip#player:port] = Start networkgame\n"
|
|
|
|
" -D = Start dedicated server\n"
|
2006-06-27 21:25:53 +00:00
|
|
|
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
|
2004-12-18 14:19:21 +00:00
|
|
|
" -f = Fork into the background (dedicated only)\n"
|
2006-06-27 21:25:53 +00:00
|
|
|
#endif
|
2006-01-06 22:52:31 +00:00
|
|
|
" -i = Force to use the DOS palette\n"
|
|
|
|
" (use this if you see a lot of pink)\n"
|
|
|
|
" -c config_file = Use 'config_file' instead of 'openttd.cfg'\n"
|
|
|
|
"\n"
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
|
2006-01-07 10:15:46 +00:00
|
|
|
p = GetDriverList(p);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ShowInfo(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *opt;
|
|
|
|
int numleft;
|
|
|
|
char **argv;
|
|
|
|
const char *options;
|
|
|
|
char *cont;
|
|
|
|
} MyGetOptData;
|
|
|
|
|
|
|
|
static void MyGetOptInit(MyGetOptData *md, int argc, char **argv, const char *options)
|
|
|
|
{
|
|
|
|
md->cont = NULL;
|
|
|
|
md->numleft = argc;
|
|
|
|
md->argv = argv;
|
|
|
|
md->options = options;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int MyGetOpt(MyGetOptData *md)
|
|
|
|
{
|
|
|
|
char *s,*r,*t;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
s = md->cont;
|
|
|
|
if (s != NULL)
|
2004-08-09 17:04:08 +00:00
|
|
|
goto md_continue_here;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (;;) {
|
|
|
|
if (--md->numleft < 0) return -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
s = *md->argv++;
|
|
|
|
if (*s == '-') {
|
|
|
|
md_continue_here:;
|
|
|
|
s++;
|
|
|
|
if (*s != 0) {
|
|
|
|
// Found argument, try to locate it in options.
|
|
|
|
if (*s == ':' || (r = strchr(md->options, *s)) == NULL) {
|
|
|
|
// ERROR!
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (r[1] == ':') {
|
|
|
|
// Item wants an argument. Check if the argument follows, or if it comes as a separate arg.
|
|
|
|
if (!*(t = s + 1)) {
|
|
|
|
// It comes as a separate arg. Check if out of args?
|
|
|
|
if (--md->numleft < 0 || *(t = *md->argv) == '-') {
|
|
|
|
// Check if item is optional?
|
|
|
|
if (r[2] != ':')
|
|
|
|
return -2;
|
|
|
|
md->numleft++;
|
|
|
|
t = NULL;
|
|
|
|
} else {
|
|
|
|
md->argv++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
md->opt = t;
|
|
|
|
md->cont = NULL;
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
md->opt = NULL;
|
|
|
|
md->cont = s;
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is currently not supported.
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
static void ParseResolution(int res[2], const char *s)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
char *t = strchr(s, 'x');
|
|
|
|
if (t == NULL) {
|
|
|
|
ShowInfoF("Invalid resolution '%s'", s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-05 15:22:20 +00:00
|
|
|
res[0] = clamp(strtoul(s, NULL, 0), 64, MAX_SCREEN_WIDTH);
|
|
|
|
res[1] = clamp(strtoul(t + 1, NULL, 0), 64, MAX_SCREEN_HEIGHT);
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
static void InitializeDynamicVariables(void)
|
|
|
|
{
|
|
|
|
/* Dynamic stuff needs to be initialized somewhere... */
|
2005-02-02 17:30:29 +00:00
|
|
|
_station_sort = NULL;
|
|
|
|
_town_sort = NULL;
|
2005-01-06 22:31:58 +00:00
|
|
|
_industry_sort = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnInitializeDynamicVariables(void)
|
|
|
|
{
|
|
|
|
/* Dynamic stuff needs to be free'd somewhere... */
|
2005-02-01 18:32:01 +00:00
|
|
|
CleanPool(&_town_pool);
|
2005-02-02 17:30:29 +00:00
|
|
|
CleanPool(&_industry_pool);
|
2005-02-03 17:22:35 +00:00
|
|
|
CleanPool(&_station_pool);
|
2005-02-04 13:23:29 +00:00
|
|
|
CleanPool(&_vehicle_pool);
|
2005-02-04 14:45:32 +00:00
|
|
|
CleanPool(&_sign_pool);
|
2005-02-06 10:24:57 +00:00
|
|
|
CleanPool(&_order_pool);
|
2005-02-01 18:32:01 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
free(_station_sort);
|
|
|
|
free(_town_sort);
|
|
|
|
free(_industry_sort);
|
|
|
|
}
|
|
|
|
|
2005-03-09 19:48:20 +00:00
|
|
|
static void UnInitializeGame(void)
|
|
|
|
{
|
|
|
|
UnInitWindowSystem();
|
2005-03-10 21:44:17 +00:00
|
|
|
|
|
|
|
free(_config_file);
|
2005-03-09 19:48:20 +00:00
|
|
|
}
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void LoadIntroGame(void)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
|
|
|
char filename[256];
|
2005-03-12 21:21:47 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
_game_mode = GM_MENU;
|
2005-03-12 21:21:47 +00:00
|
|
|
CLRBITS(_display_opt, DO_TRANS_BUILDINGS); // don't make buildings transparent in intro
|
|
|
|
_opt_ptr = &_opt_newgame;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
GfxLoadSprites();
|
|
|
|
LoadStringWidthTable();
|
|
|
|
|
|
|
|
// Setup main window
|
2005-03-09 19:48:20 +00:00
|
|
|
ResetWindowSystem();
|
2004-12-04 17:54:56 +00:00
|
|
|
SetupColorsAndInitialWindow();
|
|
|
|
|
|
|
|
// Generate a world.
|
|
|
|
sprintf(filename, "%sopntitle.dat", _path.data_dir);
|
2004-12-07 17:16:57 +00:00
|
|
|
#if defined SECOND_DATA_DIR
|
2006-02-18 14:41:24 +00:00
|
|
|
if (SaveOrLoad(filename, SL_LOAD) != SL_OK) {
|
2004-12-07 17:16:57 +00:00
|
|
|
sprintf(filename, "%sopntitle.dat", _path.second_data_dir);
|
2006-02-18 14:41:24 +00:00
|
|
|
}
|
2004-12-07 17:16:57 +00:00
|
|
|
#endif
|
2006-02-18 14:41:24 +00:00
|
|
|
if (SaveOrLoad(filename, SL_LOAD) != SL_OK) {
|
|
|
|
GenerateWorld(GW_EMPTY, 64, 64); // if failed loading, make empty world.
|
2005-01-07 08:07:24 +00:00
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
_pause = 0;
|
|
|
|
_local_player = 0;
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
|
|
|
// Play main theme
|
|
|
|
if (_music_driver->is_song_playing()) ResetMusic();
|
|
|
|
}
|
|
|
|
|
2005-08-06 14:58:06 +00:00
|
|
|
#if defined(UNIX) && !defined(__MORPHOS__)
|
2004-12-23 00:24:44 +00:00
|
|
|
extern void DedicatedFork(void);
|
2005-08-06 14:58:06 +00:00
|
|
|
#endif
|
2004-12-18 14:19:21 +00:00
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
int ttd_main(int argc, char *argv[])
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
MyGetOptData mgo;
|
|
|
|
int i;
|
2005-01-14 19:44:42 +00:00
|
|
|
const char *optformat;
|
2004-11-20 11:15:31 +00:00
|
|
|
char musicdriver[16], sounddriver[16], videodriver[16];
|
2004-08-09 17:04:08 +00:00
|
|
|
int resolution[2] = {0,0};
|
|
|
|
uint startdate = -1;
|
2006-02-21 17:29:53 +00:00
|
|
|
|
|
|
|
bool dedicated = false;
|
|
|
|
bool network = false;
|
|
|
|
char *network_conn = NULL;
|
2005-02-23 09:13:12 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
musicdriver[0] = sounddriver[0] = videodriver[0] = 0;
|
|
|
|
|
|
|
|
_game_mode = GM_MENU;
|
|
|
|
_switch_mode = SM_MENU;
|
2004-09-06 22:46:02 +00:00
|
|
|
_switch_mode_errorstr = INVALID_STRING_ID;
|
2004-12-18 14:19:21 +00:00
|
|
|
_dedicated_forks = false;
|
2005-03-10 21:44:17 +00:00
|
|
|
_config_file = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// The last param of the following function means this:
|
|
|
|
// a letter means: it accepts that param (e.g.: -h)
|
|
|
|
// a ':' behind it means: it need a param (e.g.: -m<driver>)
|
|
|
|
// a '::' behind it means: it can optional have a param (e.g.: -d<debug>)
|
2006-06-27 21:25:53 +00:00
|
|
|
optformat = "bm:s:v:hDn::eit:d::r:g::G:c:"
|
|
|
|
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
|
|
|
|
"f"
|
|
|
|
#endif
|
|
|
|
;
|
2004-12-22 21:12:36 +00:00
|
|
|
|
|
|
|
MyGetOptInit(&mgo, argc-1, argv+1, optformat);
|
2004-08-09 17:04:08 +00:00
|
|
|
while ((i = MyGetOpt(&mgo)) != -1) {
|
2006-02-01 06:32:03 +00:00
|
|
|
switch (i) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
|
|
|
|
case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
|
|
|
|
case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
|
2006-02-21 17:29:53 +00:00
|
|
|
case 'D':
|
|
|
|
strcpy(musicdriver, "null");
|
|
|
|
strcpy(sounddriver, "null");
|
|
|
|
strcpy(videodriver, "dedicated");
|
|
|
|
dedicated = true;
|
|
|
|
break;
|
|
|
|
case 'f': _dedicated_forks = true; break;
|
|
|
|
case 'n':
|
|
|
|
network = true;
|
2006-02-21 17:48:40 +00:00
|
|
|
network_conn = mgo.opt; // optional IP parameter, NULL if unset
|
2006-02-21 17:29:53 +00:00
|
|
|
break;
|
2005-11-22 15:55:38 +00:00
|
|
|
case 'b': _ai.network_client = true; break;
|
2004-08-09 17:04:08 +00:00
|
|
|
case 'r': ParseResolution(resolution, mgo.opt); break;
|
2005-11-14 19:48:04 +00:00
|
|
|
case 't': startdate = atoi(mgo.opt); break;
|
2004-08-09 17:04:08 +00:00
|
|
|
case 'd': {
|
|
|
|
#if defined(WIN32)
|
|
|
|
CreateConsole();
|
|
|
|
#endif
|
2005-11-14 19:48:04 +00:00
|
|
|
if (mgo.opt != NULL) SetDebugString(mgo.opt);
|
2004-08-09 17:04:08 +00:00
|
|
|
} break;
|
|
|
|
case 'e': _switch_mode = SM_EDITOR; break;
|
2004-12-12 20:36:24 +00:00
|
|
|
case 'i': _use_dos_palette = true; break;
|
2004-09-10 19:02:27 +00:00
|
|
|
case 'g':
|
2005-11-14 19:48:04 +00:00
|
|
|
if (mgo.opt != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
strcpy(_file_to_saveload.name, mgo.opt);
|
|
|
|
_switch_mode = SM_LOAD;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
_switch_mode = SM_NEWGAME;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
2006-02-21 17:29:53 +00:00
|
|
|
case 'G': _random_seeds[0][0] = atoi(mgo.opt); break;
|
|
|
|
case 'c': _config_file = strdup(mgo.opt); break;
|
2004-08-09 17:04:08 +00:00
|
|
|
case -2:
|
2005-07-08 22:25:24 +00:00
|
|
|
case 'h':
|
2004-08-09 17:04:08 +00:00
|
|
|
showhelp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-22 15:55:38 +00:00
|
|
|
if (_ai.network_client && !network) {
|
|
|
|
_ai.network_client = false;
|
2006-02-21 17:29:53 +00:00
|
|
|
DEBUG(ai, 0) ("[AI] Can't enable network-AI, because '-n' is not used\n");
|
2005-11-22 15:55:38 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
DeterminePaths();
|
2005-01-10 01:14:26 +00:00
|
|
|
CheckExternalFiles();
|
2004-12-18 14:19:21 +00:00
|
|
|
|
2005-08-06 14:58:06 +00:00
|
|
|
#if defined(UNIX) && !defined(__MORPHOS__)
|
2004-12-18 14:19:21 +00:00
|
|
|
// We must fork here, or we'll end up without some resources we need (like sockets)
|
|
|
|
if (_dedicated_forks)
|
|
|
|
DedicatedFork();
|
|
|
|
#endif
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
LoadFromConfig();
|
2005-04-13 23:03:31 +00:00
|
|
|
CheckConfig();
|
2005-01-11 00:54:06 +00:00
|
|
|
LoadFromHighScore();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// override config?
|
2004-11-20 11:15:31 +00:00
|
|
|
if (musicdriver[0]) ttd_strlcpy(_ini_musicdriver, musicdriver, sizeof(_ini_musicdriver));
|
|
|
|
if (sounddriver[0]) ttd_strlcpy(_ini_sounddriver, sounddriver, sizeof(_ini_sounddriver));
|
|
|
|
if (videodriver[0]) ttd_strlcpy(_ini_videodriver, videodriver, sizeof(_ini_videodriver));
|
2004-08-09 17:04:08 +00:00
|
|
|
if (resolution[0]) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; }
|
2006-05-22 10:08:23 +00:00
|
|
|
if (startdate != (uint)-1) _patches_newgame.starting_date = startdate;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_dedicated_forks && !dedicated) _dedicated_forks = false;
|
2004-12-18 14:19:21 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// enumerate language files
|
|
|
|
InitializeLanguagePacks();
|
|
|
|
|
|
|
|
// initialize screenshot formats
|
|
|
|
InitializeScreenshotFormats();
|
|
|
|
|
2004-08-22 10:23:37 +00:00
|
|
|
// initialize airport state machines
|
|
|
|
InitializeAirports();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
/* initialize all variables that are allocated dynamically */
|
|
|
|
InitializeDynamicVariables();
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/* start the AI */
|
|
|
|
AI_Initialize();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Sample catalogue
|
|
|
|
DEBUG(misc, 1) ("Loading sound effects...");
|
2005-03-12 09:38:03 +00:00
|
|
|
MxInitialize(11025);
|
|
|
|
SoundInitialize("sample.cat");
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// This must be done early, since functions use the InvalidateWindow* calls
|
2004-08-25 10:17:39 +00:00
|
|
|
InitWindowSystem();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
GfxLoadSprites();
|
|
|
|
LoadStringWidthTable();
|
|
|
|
|
2006-02-21 17:29:53 +00:00
|
|
|
DEBUG(driver, 1) ("Loading drivers...");
|
2004-08-09 17:04:08 +00:00
|
|
|
LoadDriver(SOUND_DRIVER, _ini_sounddriver);
|
|
|
|
LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
|
|
|
|
LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
|
2005-10-17 19:57:00 +00:00
|
|
|
_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-27 21:05:32 +00:00
|
|
|
// restore saved music volume
|
|
|
|
_music_driver->set_volume(msf.music_vol);
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
// initialize network-core
|
|
|
|
NetworkStartUp();
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
_opt_ptr = &_opt_newgame;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
/* XXX - ugly hack, if diff_level is 9, it means we got no setting from the config file */
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_opt_newgame.diff_level == 9) SetDifficultyLevel(0, &_opt_newgame);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-25 10:17:39 +00:00
|
|
|
// initialize the ingame console
|
|
|
|
IConsoleInit();
|
2006-03-24 00:42:35 +00:00
|
|
|
_cursor.in_window = true;
|
2005-01-31 11:03:23 +00:00
|
|
|
InitializeGUI();
|
2004-12-13 22:13:02 +00:00
|
|
|
IConsoleCmdExec("exec scripts/autoexec.scr 0");
|
2004-12-14 20:27:00 +00:00
|
|
|
|
2006-01-06 21:57:37 +00:00
|
|
|
GenerateWorld(GW_EMPTY, 64, 64); // Make the viewport initialization happy
|
2005-02-02 18:28:08 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2006-02-01 06:32:03 +00:00
|
|
|
if (network && _network_available) {
|
2004-12-04 17:54:56 +00:00
|
|
|
if (network_conn != NULL) {
|
2005-02-06 22:25:27 +00:00
|
|
|
const char *port = NULL;
|
|
|
|
const char *player = NULL;
|
2004-12-04 17:54:56 +00:00
|
|
|
uint16 rport;
|
|
|
|
|
|
|
|
rport = NETWORK_DEFAULT_PORT;
|
|
|
|
|
|
|
|
ParseConnectionString(&player, &port, network_conn);
|
|
|
|
|
|
|
|
if (player != NULL) _network_playas = atoi(player);
|
|
|
|
if (port != NULL) rport = atoi(port);
|
|
|
|
|
|
|
|
LoadIntroGame();
|
|
|
|
_switch_mode = SM_NONE;
|
|
|
|
NetworkClientConnectGame(network_conn, rport);
|
2005-01-23 13:09:35 +00:00
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2005-07-29 16:40:29 +00:00
|
|
|
_video_driver->main_loop();
|
2004-08-25 10:17:39 +00:00
|
|
|
|
2005-08-05 09:15:41 +00:00
|
|
|
WaitTillSaved();
|
2004-08-25 10:17:39 +00:00
|
|
|
IConsoleFree();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2004-08-22 10:23:37 +00:00
|
|
|
if (_network_available) {
|
2004-12-04 17:54:56 +00:00
|
|
|
// Shut down the network and close any open connections
|
|
|
|
NetworkDisconnect();
|
|
|
|
NetworkUDPClose();
|
|
|
|
NetworkShutDown();
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_video_driver->stop();
|
|
|
|
_music_driver->stop();
|
|
|
|
_sound_driver->stop();
|
|
|
|
|
|
|
|
SaveToConfig();
|
2005-01-11 00:54:06 +00:00
|
|
|
SaveToHighScore();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-22 10:23:37 +00:00
|
|
|
// uninitialize airport state machines
|
|
|
|
UnInitializeAirports();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
/* uninitialize variables that are allocated dynamic */
|
|
|
|
UnInitializeDynamicVariables();
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
/* stop the AI */
|
|
|
|
AI_Uninitialize();
|
|
|
|
|
2005-01-16 12:14:52 +00:00
|
|
|
/* Close all and any open filehandles */
|
|
|
|
FioCloseAll();
|
2005-03-09 19:48:20 +00:00
|
|
|
UnInitializeGame();
|
2005-01-16 12:14:52 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-02 16:05:59 +00:00
|
|
|
/** Mutex so that only one thread can communicate with the main program
|
|
|
|
* at any given time */
|
|
|
|
static ThreadMsg _message = 0;
|
|
|
|
|
|
|
|
static inline void OTTD_ReleaseMutex(void) {_message = 0;}
|
|
|
|
static inline ThreadMsg OTTD_PollThreadEvent(void) {return _message;}
|
|
|
|
|
|
|
|
/** Called by running thread to execute some action in the main game.
|
|
|
|
* It will stall as long as the mutex is not freed (handled) by the game */
|
|
|
|
void OTTD_SendThreadMessage(ThreadMsg msg)
|
|
|
|
{
|
2005-09-03 11:33:55 +00:00
|
|
|
if (_exit_game) return;
|
2005-09-02 16:05:59 +00:00
|
|
|
while (_message != 0) CSleep(10);
|
|
|
|
|
|
|
|
_message = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Handle the user-messages sent to us
|
|
|
|
* @param message message sent
|
|
|
|
*/
|
2006-01-05 12:40:50 +00:00
|
|
|
static void ProcessSentMessage(ThreadMsg message)
|
2005-09-02 16:05:59 +00:00
|
|
|
{
|
|
|
|
switch (message) {
|
|
|
|
case MSG_OTTD_SAVETHREAD_START: SaveFileStart(); break;
|
|
|
|
case MSG_OTTD_SAVETHREAD_DONE: SaveFileDone(); break;
|
|
|
|
case MSG_OTTD_SAVETHREAD_ERROR: SaveFileError(); break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
OTTD_ReleaseMutex(); // release mutex so that other threads, messages can be handled
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void ShowScreenshotResult(bool b)
|
|
|
|
{
|
|
|
|
if (b) {
|
2005-07-14 09:43:59 +00:00
|
|
|
SetDParamStr(0, _screenshot_name);
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_031B_SCREENSHOT_SUCCESSFULLY, 0, 0);
|
|
|
|
} else {
|
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_031C_SCREENSHOT_FAILED, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void MakeNewGame(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_game_mode = GM_NORMAL;
|
|
|
|
|
|
|
|
// Copy in game options
|
2005-03-12 21:21:47 +00:00
|
|
|
_opt_ptr = &_opt;
|
2005-11-14 19:48:04 +00:00
|
|
|
memcpy(_opt_ptr, &_opt_newgame, sizeof(*_opt_ptr));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
GfxLoadSprites();
|
|
|
|
|
2004-08-25 08:55:53 +00:00
|
|
|
// Reinitialize windows
|
2005-03-09 19:48:20 +00:00
|
|
|
ResetWindowSystem();
|
2004-08-09 17:04:08 +00:00
|
|
|
LoadStringWidthTable();
|
|
|
|
|
|
|
|
SetupColorsAndInitialWindow();
|
|
|
|
|
|
|
|
// Randomize world
|
2006-01-06 21:57:37 +00:00
|
|
|
GenerateWorld(GW_NEWGAME, 1<<_patches.map_x, 1<<_patches.map_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// In a dedicated server, the server does not play
|
|
|
|
if (_network_dedicated) {
|
|
|
|
_local_player = OWNER_SPECTATOR;
|
2005-07-08 22:25:24 +00:00
|
|
|
} else {
|
2004-12-04 17:54:56 +00:00
|
|
|
// Create a single player
|
|
|
|
DoStartupNewPlayer(false);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
_local_player = 0;
|
2005-08-06 16:07:22 +00:00
|
|
|
_current_player = _local_player;
|
|
|
|
DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_REPLACE_VEHICLE);
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void MakeNewEditorWorld(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_game_mode = GM_EDITOR;
|
|
|
|
|
|
|
|
// Copy in game options
|
2005-03-12 21:21:47 +00:00
|
|
|
_opt_ptr = &_opt;
|
|
|
|
memcpy(_opt_ptr, &_opt_newgame, sizeof(GameOptions));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
GfxLoadSprites();
|
|
|
|
|
2004-08-25 08:55:53 +00:00
|
|
|
// Re-init the windowing system
|
2005-03-09 19:48:20 +00:00
|
|
|
ResetWindowSystem();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Create toolbars
|
|
|
|
SetupColorsAndInitialWindow();
|
|
|
|
|
|
|
|
// Startup the game system
|
2006-01-06 21:57:37 +00:00
|
|
|
GenerateWorld(GW_EMPTY, 1 << _patches.map_x, 1 << _patches.map_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_local_player = OWNER_NONE;
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void StartupPlayers(void);
|
|
|
|
void StartupDisasters(void);
|
2006-05-22 16:44:16 +00:00
|
|
|
extern void StartupEconomy(void);
|
2004-08-23 10:59:03 +00:00
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
/**
|
|
|
|
* Start Scenario starts a new game based on a scenario.
|
|
|
|
* Eg 'New Game' --> select a preset scenario
|
2005-03-13 11:47:04 +00:00
|
|
|
* This starts a scenario based on your current difficulty settings
|
2005-03-12 21:21:47 +00:00
|
|
|
*/
|
2005-01-22 22:47:58 +00:00
|
|
|
static void StartScenario(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_game_mode = GM_NORMAL;
|
|
|
|
|
|
|
|
// invalid type
|
|
|
|
if (_file_to_saveload.mode == SL_INVALID) {
|
|
|
|
printf("Savegame is obsolete or invalid format: %s\n", _file_to_saveload.name);
|
2005-12-13 21:21:57 +00:00
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
_game_mode = GM_MENU;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GfxLoadSprites();
|
|
|
|
|
2004-08-25 08:55:53 +00:00
|
|
|
// Reinitialize windows
|
2005-03-09 19:48:20 +00:00
|
|
|
ResetWindowSystem();
|
2004-08-09 17:04:08 +00:00
|
|
|
LoadStringWidthTable();
|
|
|
|
|
|
|
|
SetupColorsAndInitialWindow();
|
|
|
|
|
|
|
|
// Load game
|
|
|
|
if (SaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode) != SL_OK) {
|
|
|
|
LoadIntroGame();
|
2005-12-13 21:21:57 +00:00
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-13 11:47:04 +00:00
|
|
|
_opt_ptr = &_opt;
|
2006-02-06 09:18:04 +00:00
|
|
|
memcpy(&_opt_ptr->diff, &_opt_newgame.diff, sizeof(_opt_ptr->diff));
|
2005-03-13 11:47:04 +00:00
|
|
|
_opt.diff_level = _opt_newgame.diff_level;
|
2005-03-12 21:21:47 +00:00
|
|
|
|
2004-08-23 08:59:36 +00:00
|
|
|
// Inititalize data
|
2006-05-22 16:44:16 +00:00
|
|
|
StartupEconomy();
|
2004-08-23 08:59:36 +00:00
|
|
|
StartupPlayers();
|
|
|
|
StartupEngines();
|
|
|
|
StartupDisasters();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_local_player = 0;
|
2005-08-06 16:07:22 +00:00
|
|
|
_current_player = _local_player;
|
|
|
|
DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_REPLACE_VEHICLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
bool SafeSaveOrLoad(const char *filename, int mode, int newgm)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte ogm = _game_mode;
|
|
|
|
|
|
|
|
_game_mode = newgm;
|
2006-02-14 07:32:45 +00:00
|
|
|
switch (SaveOrLoad(filename, mode)) {
|
|
|
|
case SL_OK: return true;
|
|
|
|
|
|
|
|
case SL_REINIT:
|
|
|
|
switch (ogm) {
|
|
|
|
case GM_MENU: LoadIntroGame(); break;
|
|
|
|
case GM_EDITOR: MakeNewEditorWorld(); break;
|
|
|
|
default: MakeNewGame(); break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_game_mode = ogm;
|
|
|
|
return false;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
void SwitchMode(int new_mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
// If we are saving something, the network stays in his current state
|
|
|
|
if (new_mode != SM_SAVE) {
|
|
|
|
// If the network is active, make it not-active
|
|
|
|
if (_networking) {
|
|
|
|
if (_network_server && (new_mode == SM_LOAD || new_mode == SM_NEWGAME)) {
|
|
|
|
NetworkReboot();
|
|
|
|
NetworkUDPClose();
|
|
|
|
} else {
|
|
|
|
NetworkDisconnect();
|
|
|
|
NetworkUDPClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are a server, we restart the server
|
|
|
|
if (_is_network_server) {
|
|
|
|
// But not if we are going to the menu
|
|
|
|
if (new_mode != SM_MENU) {
|
|
|
|
NetworkServerStart();
|
|
|
|
} else {
|
|
|
|
// This client no longer wants to be a network-server
|
|
|
|
_is_network_server = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
switch (new_mode) {
|
|
|
|
case SM_EDITOR: /* Switch to scenario editor */
|
2004-08-09 17:04:08 +00:00
|
|
|
MakeNewEditorWorld();
|
|
|
|
break;
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_NEWGAME: /* New Game --> 'Random game' */
|
2004-12-13 17:58:53 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_network_server) {
|
2006-02-06 09:18:04 +00:00
|
|
|
snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "Random Map");
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-12-13 17:58:53 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-08-09 17:04:08 +00:00
|
|
|
MakeNewGame();
|
|
|
|
break;
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_START_SCENARIO: /* New Game --> Choose one of the preset scenarios */
|
2006-02-01 06:32:03 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
if (_network_server) {
|
2006-02-06 09:18:04 +00:00
|
|
|
snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "%s (Loaded scenario)", _file_to_saveload.title);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-12-04 17:54:56 +00:00
|
|
|
StartScenario();
|
|
|
|
break;
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_LOAD: { /* Load game, Play Scenario */
|
|
|
|
_opt_ptr = &_opt;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL)) {
|
2005-01-08 15:26:58 +00:00
|
|
|
LoadIntroGame();
|
2005-12-13 21:21:57 +00:00
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
_local_player = 0;
|
|
|
|
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); // decrease pause counter (was increased from opening load dialog)
|
2004-12-13 17:58:53 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_network_server) {
|
2006-02-06 09:18:04 +00:00
|
|
|
snprintf(_network_game_info.map_name, lengthof(_network_game_info.map_name), "%s (Loaded game)", _file_to_saveload.title);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-12-13 17:58:53 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_LOAD_SCENARIO: { /* Load scenario from scenario editor */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR)) {
|
2006-08-14 11:44:19 +00:00
|
|
|
Player *p;
|
2005-11-13 21:16:34 +00:00
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
_opt_ptr = &_opt;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_local_player = OWNER_NONE;
|
|
|
|
_generating_world = true;
|
2006-08-14 11:44:19 +00:00
|
|
|
/* Delete all players */
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (p->is_active) {
|
|
|
|
ChangeOwnershipOfPlayerItems(p->index, OWNER_SPECTATOR);
|
|
|
|
p->is_active = false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
_generating_world = false;
|
|
|
|
// delete all stations owned by a player
|
|
|
|
DeleteAllPlayerStations();
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_MENU: /* Switch to game intro menu */
|
2004-08-09 17:04:08 +00:00
|
|
|
LoadIntroGame();
|
|
|
|
break;
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_SAVE: /* Save game */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (SaveOrLoad(_file_to_saveload.name, SL_SAVE) != SL_OK) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_4007_GAME_SAVE_FAILED, 0, 0);
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_SAVELOAD, 0);
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
|
2005-03-12 21:21:47 +00:00
|
|
|
case SM_GENRANDLAND: /* Generate random land within scenario editor */
|
2006-02-01 06:32:03 +00:00
|
|
|
GenerateWorld(GW_RANDOM, 1 << _patches.map_x, 1 << _patches.map_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
// XXX: set date
|
|
|
|
_local_player = OWNER_NONE;
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
break;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (_switch_mode_errorstr != INVALID_STRING_ID) {
|
|
|
|
ShowErrorMessage(INVALID_STRING_ID, _switch_mode_errorstr, 0, 0);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// State controlling game loop.
|
|
|
|
// The state must not be changed from anywhere
|
|
|
|
// but here.
|
|
|
|
// That check is enforced in DoCommand.
|
2005-01-22 20:23:18 +00:00
|
|
|
void StateGameLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-09-12 15:29:37 +00:00
|
|
|
// dont execute the state loop during pause
|
|
|
|
if (_pause) return;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_game_mode == GM_EDITOR) {
|
|
|
|
RunTileLoop();
|
|
|
|
CallVehicleTicks();
|
|
|
|
CallLandscapeTick();
|
|
|
|
CallWindowTickEvent();
|
|
|
|
NewsLoop();
|
|
|
|
} else {
|
2004-09-11 19:34:11 +00:00
|
|
|
// All these actions has to be done from OWNER_NONE
|
|
|
|
// for multiplayer compatibility
|
2005-10-07 07:35:15 +00:00
|
|
|
PlayerID p = _current_player;
|
2004-09-11 19:34:11 +00:00
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
AnimateAnimatedTiles();
|
|
|
|
IncreaseDate();
|
|
|
|
RunTileLoop();
|
|
|
|
CallVehicleTicks();
|
|
|
|
CallLandscapeTick();
|
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
AI_RunGameLoop();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
CallWindowTickEvent();
|
|
|
|
NewsLoop();
|
2004-09-11 19:34:11 +00:00
|
|
|
_current_player = p;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void DoAutosave(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
char buf[200];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-10 14:14:00 +00:00
|
|
|
if (_patches.keep_all_autosave && _local_player != OWNER_SPECTATOR) {
|
2005-06-21 16:28:17 +00:00
|
|
|
const Player *p = GetPlayer(_local_player);
|
2004-08-09 17:04:08 +00:00
|
|
|
char *s;
|
|
|
|
sprintf(buf, "%s%s", _path.autosave_dir, PATHSEP);
|
2005-03-12 21:21:47 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
|
|
|
SetDParam(2, _date);
|
2005-11-15 09:05:16 +00:00
|
|
|
s = GetString(buf + strlen(_path.autosave_dir) + strlen(PATHSEP), STR_4004);
|
2004-08-09 17:04:08 +00:00
|
|
|
strcpy(s, ".sav");
|
2005-11-17 19:43:37 +00:00
|
|
|
} else { /* generate a savegame name and number according to _patches.max_num_autosaves */
|
|
|
|
sprintf(buf, "%s%sautosave%d.sav", _path.autosave_dir, PATHSEP, _autosave_ctr);
|
|
|
|
|
|
|
|
_autosave_ctr++;
|
|
|
|
if (_autosave_ctr >= _patches.max_num_autosaves) {
|
|
|
|
// we reached the limit for numbers of autosaves. We will start over
|
|
|
|
_autosave_ctr = 0;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-05 20:02:49 +00:00
|
|
|
DEBUG(misc, 2) ("Autosaving to %s", buf);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (SaveOrLoad(buf, SL_SAVE) != SL_OK)
|
|
|
|
ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
|
|
|
|
}
|
|
|
|
|
2005-02-22 14:52:20 +00:00
|
|
|
static void ScrollMainViewport(int x, int y)
|
|
|
|
{
|
|
|
|
if (_game_mode != GM_MENU) {
|
|
|
|
Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
|
|
|
|
assert(w);
|
|
|
|
|
|
|
|
WP(w,vp_d).scrollpos_x += x << w->viewport->zoom;
|
|
|
|
WP(w,vp_d).scrollpos_y += y << w->viewport->zoom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int8 scrollamt[16][2] = {
|
|
|
|
{ 0, 0},
|
|
|
|
{-2, 0}, // 1:left
|
|
|
|
{ 0,-2}, // 2:up
|
|
|
|
{-2,-1}, // 3:left + up
|
|
|
|
{ 2, 0}, // 4:right
|
|
|
|
{ 0, 0}, // 5:left + right
|
|
|
|
{ 2,-1}, // 6:right + up
|
|
|
|
{ 0,-2}, // 7:left + right + up = up
|
|
|
|
{ 0 ,2}, // 8:down
|
|
|
|
{-2 ,1}, // 9:down+left
|
|
|
|
{ 0, 0}, // 10:impossible
|
|
|
|
{-2, 0}, // 11:left + up + down = left
|
|
|
|
{ 2, 1}, // 12:down+right
|
|
|
|
{ 0, 2}, // 13:left + right + down = down
|
|
|
|
{ 0,-2}, // 14:left + right + up = up
|
|
|
|
{ 0, 0}, // 15:impossible
|
|
|
|
};
|
|
|
|
|
|
|
|
static void HandleKeyScrolling(void)
|
|
|
|
{
|
|
|
|
if (_dirkeys && !_no_scroll) {
|
|
|
|
int factor = _shift_pressed ? 50 : 10;
|
|
|
|
ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor);
|
|
|
|
}
|
|
|
|
}
|
2005-01-28 09:30:19 +00:00
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void GameLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-09-02 16:05:59 +00:00
|
|
|
ThreadMsg message;
|
|
|
|
|
|
|
|
if ((message = OTTD_PollThreadEvent()) != 0) ProcessSentMessage(message);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// autosave game?
|
|
|
|
if (_do_autosave) {
|
|
|
|
_do_autosave = false;
|
|
|
|
DoAutosave();
|
|
|
|
RedrawAutosave();
|
|
|
|
}
|
|
|
|
|
2005-01-28 09:30:19 +00:00
|
|
|
// handle scrolling of the main window
|
|
|
|
if (_dirkeys) HandleKeyScrolling();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// make a screenshot?
|
2006-07-28 21:51:00 +00:00
|
|
|
if (IsScreenshotRequested()) ShowScreenshotResult(MakeScreenshot());
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// switch game mode?
|
2006-02-14 07:32:45 +00:00
|
|
|
if (_switch_mode != SM_NONE) {
|
|
|
|
SwitchMode(_switch_mode);
|
2004-08-09 17:04:08 +00:00
|
|
|
_switch_mode = SM_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
IncreaseSpriteLRU();
|
|
|
|
InteractiveRandom();
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_scroller_click_timeout > 3) {
|
2004-08-09 17:04:08 +00:00
|
|
|
_scroller_click_timeout -= 3;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
_scroller_click_timeout = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_caret_timer += 3;
|
2005-11-14 19:48:04 +00:00
|
|
|
_timer_counter += 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
CursorTick();
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
// Check for UDP stuff
|
2006-07-13 17:53:32 +00:00
|
|
|
if (_network_available)
|
|
|
|
NetworkUDPGameLoop();
|
2004-12-04 17:54:56 +00:00
|
|
|
|
|
|
|
if (_networking) {
|
|
|
|
// Multiplayer
|
|
|
|
NetworkGameLoop();
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2004-12-04 17:54:56 +00:00
|
|
|
if (_network_reconnect > 0 && --_network_reconnect == 0) {
|
|
|
|
// This means that we want to reconnect to the last host
|
|
|
|
// We do this here, because it means that the network is really closed
|
|
|
|
NetworkClientConnectGame(_network_last_host, _network_last_port);
|
|
|
|
}
|
|
|
|
// Singleplayer
|
2004-09-12 15:29:37 +00:00
|
|
|
StateGameLoop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
#else
|
|
|
|
StateGameLoop();
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!_pause && _display_opt & DO_FULL_ANIMATION) DoPaletteAnimations();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!_pause || _cheats.build_in_pause.value) MoveAllTextEffects();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-26 04:16:39 +00:00
|
|
|
InputLoop();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
MusicLoop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void BeforeSaveGame(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-07-26 03:33:12 +00:00
|
|
|
const Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-11 00:54:06 +00:00
|
|
|
if (w != NULL) {
|
2005-11-15 09:00:02 +00:00
|
|
|
_saved_scrollpos_x = WP(w, const vp_d).scrollpos_x;
|
|
|
|
_saved_scrollpos_y = WP(w, const vp_d).scrollpos_y;
|
2005-01-11 00:54:06 +00:00
|
|
|
_saved_scrollpos_zoom = w->viewport->zoom;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void ConvertTownOwner(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-03 18:59:58 +00:00
|
|
|
for (tile = 0; tile != MapSize(); tile++) {
|
2006-06-10 08:37:41 +00:00
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_STREET:
|
|
|
|
if (IsLevelCrossing(tile) && GetCrossingRoadOwner(tile) & 0x80) {
|
|
|
|
SetCrossingRoadOwner(tile, OWNER_TOWN);
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case MP_TUNNELBRIDGE:
|
|
|
|
if (GetTileOwner(tile) & 0x80) SetTileOwner(tile, OWNER_TOWN);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
default: break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// before savegame version 4, the name of the company determined if it existed
|
2005-01-22 22:47:58 +00:00
|
|
|
static void CheckIsPlayerActive(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-07-26 03:33:12 +00:00
|
|
|
Player *p;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (p->name_1 != 0) p->is_active = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-23 21:04:39 +00:00
|
|
|
// since savegame version 4.1, exclusive transport rights are stored at towns
|
2005-01-22 22:47:58 +00:00
|
|
|
static void UpdateExclusiveRights(void)
|
2004-08-23 21:04:39 +00:00
|
|
|
{
|
2006-07-26 03:33:12 +00:00
|
|
|
Town *t;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
|
|
|
FOR_ALL_TOWNS(t) {
|
|
|
|
if (t->xy != 0) t->exclusivity = (byte)-1;
|
2004-08-23 21:04:39 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 21:29:25 +00:00
|
|
|
/* FIXME old exclusive rights status is not being imported (stored in s->blocked_months_obsolete)
|
2004-08-23 21:04:39 +00:00
|
|
|
could be implemented this way:
|
|
|
|
1.) Go through all stations
|
|
|
|
Build an array town_blocked[ town_id ][ player_id ]
|
|
|
|
that stores if at least one station in that town is blocked for a player
|
|
|
|
2.) Go through that array, if you find a town that is not blocked for
|
2005-07-08 22:25:24 +00:00
|
|
|
one player, but for all others, then give him exclusivity.
|
2004-08-23 21:04:39 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2005-12-14 06:51:19 +00:00
|
|
|
static const byte convert_currency[] = {
|
2004-12-22 16:54:55 +00:00
|
|
|
0, 1, 12, 8, 3,
|
|
|
|
10, 14, 19, 4, 5,
|
|
|
|
9, 11, 13, 6, 17,
|
|
|
|
16, 22, 21, 7, 15,
|
|
|
|
18, 2, 20, };
|
|
|
|
|
|
|
|
// since savegame version 4.2 the currencies are arranged differently
|
2005-01-22 22:47:58 +00:00
|
|
|
static void UpdateCurrencies(void)
|
2004-12-22 16:54:55 +00:00
|
|
|
{
|
2004-12-22 19:48:27 +00:00
|
|
|
_opt.currency = convert_currency[_opt.currency];
|
2004-12-22 16:54:55 +00:00
|
|
|
}
|
|
|
|
|
2005-01-26 18:17:50 +00:00
|
|
|
/* Up to revision 1413 the invisible tiles at the southern border have not been
|
|
|
|
* MP_VOID, even though they should have. This is fixed by this function
|
|
|
|
*/
|
2005-01-22 22:47:58 +00:00
|
|
|
static void UpdateVoidTiles(void)
|
2005-01-07 18:13:56 +00:00
|
|
|
{
|
2005-01-09 21:25:44 +00:00
|
|
|
uint i;
|
2005-01-26 18:17:50 +00:00
|
|
|
|
2006-02-24 19:52:26 +00:00
|
|
|
for (i = 0; i < MapMaxY(); ++i) MakeVoid(i * MapSizeX() + MapMaxX());
|
|
|
|
for (i = 0; i < MapSizeX(); ++i) MakeVoid(MapSizeX() * MapMaxY() + i);
|
2005-01-07 18:13:56 +00:00
|
|
|
}
|
|
|
|
|
2005-01-25 15:38:36 +00:00
|
|
|
// since savegame version 6.0 each sign has an "owner", signs without owner (from old games are set to 255)
|
|
|
|
static void UpdateSignOwner(void)
|
|
|
|
{
|
|
|
|
SignStruct *ss;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
|
|
|
FOR_ALL_SIGNS(ss) ss->owner = OWNER_NONE;
|
2005-01-25 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
extern void UpdateOldAircraft( void );
|
|
|
|
extern void UpdateOilRig( void );
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-25 07:29:02 +00:00
|
|
|
|
|
|
|
static inline RailType UpdateRailType(RailType rt, RailType min)
|
|
|
|
{
|
|
|
|
return rt >= min ? (RailType)(rt + 1): rt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-29 22:28:19 +00:00
|
|
|
bool AfterLoadGame(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Window *w;
|
|
|
|
ViewPort *vp;
|
2005-07-20 22:02:58 +00:00
|
|
|
Player *p;
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// in version 2.1 of the savegame, town owner was unified.
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(2, 1)) ConvertTownOwner();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-23 21:04:39 +00:00
|
|
|
// from version 4.1 of the savegame, exclusive rights are stored at towns
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(4, 1)) UpdateExclusiveRights();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-22 16:54:55 +00:00
|
|
|
// from version 4.2 of the savegame, currencies are in a different order
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(4, 2)) UpdateCurrencies();
|
2004-12-22 16:54:55 +00:00
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
// from version 6.1 of the savegame, signs have an "owner"
|
|
|
|
if (CheckSavegameVersionOldStyle(6, 1)) UpdateSignOwner();
|
2005-01-25 15:38:36 +00:00
|
|
|
|
2004-12-30 10:29:28 +00:00
|
|
|
/* In old version there seems to be a problem that water is owned by
|
|
|
|
OWNER_NONE, not OWNER_WATER.. I can't replicate it for the current
|
2005-11-22 19:33:29 +00:00
|
|
|
(4.3) version, so I just check when versions are older, and then
|
2004-12-30 10:29:28 +00:00
|
|
|
walk through the whole map.. */
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(4, 3)) {
|
2005-06-25 16:44:57 +00:00
|
|
|
TileIndex tile = TileXY(0, 0);
|
2004-12-30 10:29:28 +00:00
|
|
|
uint w = MapSizeX();
|
|
|
|
uint h = MapSizeY();
|
|
|
|
|
|
|
|
BEGIN_TILE_LOOP(tile_cur, w, h, tile)
|
2005-06-04 11:56:32 +00:00
|
|
|
if (IsTileType(tile_cur, MP_WATER) && GetTileOwner(tile_cur) >= MAX_PLAYERS)
|
2005-06-04 12:13:24 +00:00
|
|
|
SetTileOwner(tile_cur, OWNER_WATER);
|
2004-12-30 10:29:28 +00:00
|
|
|
END_TILE_LOOP(tile_cur, w, h, tile)
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// convert road side to my format.
|
|
|
|
if (_opt.road_side) _opt.road_side = 1;
|
|
|
|
|
|
|
|
// Load the sprites
|
|
|
|
GfxLoadSprites();
|
|
|
|
|
2006-01-04 20:18:39 +00:00
|
|
|
/* Connect front and rear engines of multiheaded trains and converts
|
|
|
|
* subtype to the new format */
|
|
|
|
if (CheckSavegameVersionOldStyle(17, 1)) ConvertOldMultiheadToNew();
|
|
|
|
|
2006-01-29 23:04:51 +00:00
|
|
|
/* Connect front and rear engines of multiheaded trains */
|
|
|
|
ConnectMultiheadedTrains();
|
2006-01-18 15:05:01 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Update current year
|
|
|
|
SetDate(_date);
|
|
|
|
|
|
|
|
// reinit the landscape variables (landscape might have changed)
|
|
|
|
InitializeLandscapeVariables(true);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Update all vehicles
|
|
|
|
AfterLoadVehicles();
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
// Update all waypoints
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersion(12)) FixOldWaypoints();
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
UpdateAllWaypointSigns();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// in version 2.2 of the savegame, we have new airports
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(2, 2)) UpdateOldAircraft();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
UpdateAllStationVirtCoord();
|
|
|
|
|
|
|
|
// Setup town coords
|
|
|
|
AfterLoadTown();
|
|
|
|
UpdateAllSignVirtCoords();
|
|
|
|
|
|
|
|
// make sure there is a town in the game
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_game_mode == GM_NORMAL && !ClosestTownFromTile(0, (uint)-1)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_NO_TOWN_IN_SCENARIO;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize windows
|
2005-03-09 19:48:20 +00:00
|
|
|
ResetWindowSystem();
|
2004-08-09 17:04:08 +00:00
|
|
|
SetupColorsAndInitialWindow();
|
|
|
|
|
|
|
|
w = FindWindowById(WC_MAIN_WINDOW, 0);
|
|
|
|
|
|
|
|
WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
|
|
|
|
WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
vp = w->viewport;
|
|
|
|
vp->zoom = _saved_scrollpos_zoom;
|
|
|
|
vp->virtual_width = vp->width << vp->zoom;
|
|
|
|
vp->virtual_height = vp->height << vp->zoom;
|
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
// in version 4.1 of the savegame, is_active was introduced to determine
|
2004-08-09 17:04:08 +00:00
|
|
|
// if a player does exist, rather then checking name_1
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(4, 1)) CheckIsPlayerActive();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
// the void tiles on the southern border used to belong to a wrong class (pre 4.3).
|
|
|
|
if (CheckSavegameVersionOldStyle(4, 3)) UpdateVoidTiles();
|
2005-01-07 18:13:56 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// If Load Scenario / New (Scenario) Game is used,
|
2004-08-25 10:17:39 +00:00
|
|
|
// a player does not exist yet. So create one here.
|
2004-12-04 17:54:56 +00:00
|
|
|
// 1 exeption: network-games. Those can have 0 players
|
|
|
|
// But this exeption is not true for network_servers!
|
|
|
|
if (!_players[0].is_active && (!_networking || (_networking && _network_server)))
|
2004-08-25 10:17:39 +00:00
|
|
|
DoStartupNewPlayer(false);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-03 19:59:05 +00:00
|
|
|
DoZoomInOutWindow(ZOOM_NONE, w); // update button status
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
// In 5.1, Oilrigs have been moved (again)
|
|
|
|
if (CheckSavegameVersionOldStyle(5, 1)) UpdateOilRig();
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
/* In version 6.1 we put the town index in the map-array. To do this, we need
|
|
|
|
* to use m2 (16bit big), so we need to clean m2, and that is where this is
|
|
|
|
* all about ;) */
|
|
|
|
if (CheckSavegameVersionOldStyle(6, 1)) {
|
2005-02-02 14:17:13 +00:00
|
|
|
BEGIN_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0) {
|
2006-06-08 18:31:54 +00:00
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_HOUSE:
|
|
|
|
_m[tile].m4 = _m[tile].m2;
|
|
|
|
SetTownIndex(tile, CalcClosestTownFromTile(tile, (uint)-1)->index);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STREET:
|
|
|
|
_m[tile].m4 |= (_m[tile].m2 << 4);
|
|
|
|
if (IsTileOwner(tile, OWNER_TOWN)) {
|
|
|
|
SetTownIndex(tile, CalcClosestTownFromTile(tile, (uint)-1)->index);
|
|
|
|
} else {
|
|
|
|
SetTownIndex(tile, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
2005-02-02 14:17:13 +00:00
|
|
|
}
|
|
|
|
} END_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0);
|
|
|
|
}
|
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
/* From version 9.0, we update the max passengers of a town (was sometimes negative
|
|
|
|
* before that. */
|
|
|
|
if (CheckSavegameVersion(9)) {
|
2005-02-17 10:56:19 +00:00
|
|
|
Town *t;
|
2005-11-14 19:48:04 +00:00
|
|
|
FOR_ALL_TOWNS(t) UpdateTownMaxPass(t);
|
2005-02-17 10:56:19 +00:00
|
|
|
}
|
|
|
|
|
2005-11-22 19:33:29 +00:00
|
|
|
/* From version 16.0, we included autorenew on engines, which are now saved, but
|
|
|
|
* of course, we do need to initialize them for older savegames. */
|
|
|
|
if (CheckSavegameVersion(16)) {
|
2005-08-06 16:07:22 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
2006-01-12 15:52:18 +00:00
|
|
|
p->engine_renew_list = NULL;
|
2005-08-06 16:07:22 +00:00
|
|
|
p->engine_renew = false;
|
|
|
|
p->engine_renew_months = -6;
|
|
|
|
p->engine_renew_money = 100000;
|
|
|
|
}
|
2005-08-10 21:16:38 +00:00
|
|
|
if (_local_player < MAX_PLAYERS) {
|
|
|
|
// Set the human controlled player to the patch settings
|
|
|
|
// Scenario editor do not have any companies
|
|
|
|
p = GetPlayer(_local_player);
|
|
|
|
p->engine_renew = _patches.autorenew;
|
|
|
|
p->engine_renew_months = _patches.autorenew_months;
|
|
|
|
p->engine_renew_money = _patches.autorenew_money;
|
|
|
|
}
|
2005-08-06 16:07:22 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 16:30:26 +00:00
|
|
|
/* Elrails got added in rev 24 */
|
|
|
|
if (CheckSavegameVersion(24)) {
|
2006-07-26 03:33:12 +00:00
|
|
|
Vehicle *v;
|
2006-03-29 16:30:26 +00:00
|
|
|
uint i;
|
|
|
|
TileIndex t;
|
2006-04-25 07:29:02 +00:00
|
|
|
RailType min_rail = RAILTYPE_ELECTRIC;
|
2006-03-29 16:30:26 +00:00
|
|
|
|
|
|
|
for (i = 0; i < lengthof(_engines); i++) {
|
2006-07-26 03:33:12 +00:00
|
|
|
Engine *e = GetEngine(i);
|
2006-03-29 16:30:26 +00:00
|
|
|
if (e->type == VEH_Train &&
|
|
|
|
(e->railtype != RAILTYPE_RAIL || RailVehInfo(i)->engclass == 2)) {
|
|
|
|
e->railtype++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Train) {
|
|
|
|
RailType rt = GetEngine(v->engine_type)->railtype;
|
|
|
|
|
|
|
|
v->u.rail.railtype = rt;
|
2006-04-25 07:29:02 +00:00
|
|
|
if (rt == RAILTYPE_ELECTRIC) min_rail = RAILTYPE_RAIL;
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* .. so we convert the entire map from normal to elrail (so maintain "fairness") */
|
|
|
|
for (t = 0; t < MapSize(); t++) {
|
|
|
|
switch (GetTileType(t)) {
|
|
|
|
case MP_RAILWAY:
|
2006-04-25 07:29:02 +00:00
|
|
|
SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
|
2006-03-29 16:30:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STREET:
|
2006-04-25 07:29:02 +00:00
|
|
|
if (IsLevelCrossing(t)) {
|
|
|
|
SetRailTypeCrossing(t, UpdateRailType(GetRailTypeCrossing(t), min_rail));
|
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STATION:
|
2006-06-27 21:25:53 +00:00
|
|
|
if (IsRailwayStation(t)) {
|
2006-04-25 07:29:02 +00:00
|
|
|
SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
|
|
|
|
}
|
2006-03-29 16:30:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_TUNNELBRIDGE:
|
2006-03-30 09:08:43 +00:00
|
|
|
if (IsTunnel(t)) {
|
|
|
|
if (GetTunnelTransportType(t) == TRANSPORT_RAIL) {
|
2006-04-25 07:29:02 +00:00
|
|
|
SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-03-30 09:08:43 +00:00
|
|
|
if (GetBridgeTransportType(t) == TRANSPORT_RAIL) {
|
2006-06-07 19:35:21 +00:00
|
|
|
if (IsBridgeRamp(t)) {
|
|
|
|
SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
|
|
|
|
} else {
|
|
|
|
SetRailTypeOnBridge(t, UpdateRailType(GetRailTypeOnBridge(t), min_rail));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IsBridgeMiddle(t) &&
|
|
|
|
IsTransportUnderBridge(t) &&
|
|
|
|
GetTransportTypeUnderBridge(t) == TRANSPORT_RAIL) {
|
2006-04-25 07:29:02 +00:00
|
|
|
SetRailType(t, UpdateRailType(GetRailType(t), min_rail));
|
2006-03-29 16:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Train && (IsFrontEngine(v) || IsFreeWagon(v))) TrainConsistChanged(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-12-29 12:42:59 +00:00
|
|
|
/* In version 16.1 of the savegame a player can decide if trains, which get
|
|
|
|
* replaced, shall keep their old length. In all prior versions, just default
|
|
|
|
* to false */
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersionOldStyle(16, 1)) {
|
2006-06-27 21:25:53 +00:00
|
|
|
FOR_ALL_PLAYERS(p) p->renew_keep_length = false;
|
2005-11-07 23:20:47 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 22:20:15 +00:00
|
|
|
/* In version 17, ground type is moved from m2 to m4 for depots and
|
|
|
|
* waypoints to make way for storing the index in m2. The custom graphics
|
|
|
|
* id which was stored in m4 is now saved as a grf/id reference in the
|
|
|
|
* waypoint struct. */
|
2005-11-22 19:33:29 +00:00
|
|
|
if (CheckSavegameVersion(17)) {
|
2005-11-16 22:20:15 +00:00
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
|
|
|
if (wp->xy != 0 && wp->deleted == 0) {
|
2006-04-27 18:28:56 +00:00
|
|
|
const StationSpec *statspec = NULL;
|
2005-11-16 22:20:15 +00:00
|
|
|
|
|
|
|
if (HASBIT(_m[wp->xy].m3, 4))
|
2006-04-27 18:28:56 +00:00
|
|
|
statspec = GetCustomStationSpec(STAT_CLASS_WAYP, _m[wp->xy].m4 + 1);
|
2005-11-16 22:20:15 +00:00
|
|
|
|
2006-04-27 18:28:56 +00:00
|
|
|
if (statspec != NULL) {
|
2005-11-16 22:20:15 +00:00
|
|
|
wp->stat_id = _m[wp->xy].m4 + 1;
|
2006-04-27 18:28:56 +00:00
|
|
|
wp->grfid = statspec->grfid;
|
|
|
|
wp->localidx = statspec->localidx;
|
2005-11-16 22:20:15 +00:00
|
|
|
} else {
|
|
|
|
// No custom graphics set, so set to default.
|
|
|
|
wp->stat_id = 0;
|
|
|
|
wp->grfid = 0;
|
|
|
|
wp->localidx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move ground type bits from m2 to m4.
|
|
|
|
_m[wp->xy].m4 = GB(_m[wp->xy].m2, 0, 4);
|
|
|
|
// Store waypoint index in the tile.
|
|
|
|
_m[wp->xy].m2 = wp->index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* As of version 17, we recalculate the custom graphic ID of waypoints
|
|
|
|
* from the GRF ID / station index. */
|
|
|
|
UpdateAllWaypointCustomGraphics();
|
|
|
|
}
|
|
|
|
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
/* From version 15, we moved a semaphore bit from bit 2 to bit 3 in m4, making
|
|
|
|
* room for PBS. Now in version 21 move it back :P. */
|
|
|
|
if (CheckSavegameVersion(21) && !CheckSavegameVersion(15)) {
|
|
|
|
BEGIN_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0) {
|
|
|
|
if (IsTileType(tile, MP_RAILWAY)) {
|
|
|
|
if (HasSignals(tile)) {
|
|
|
|
// convert PBS signals to combo-signals
|
2006-06-08 19:27:20 +00:00
|
|
|
if (HASBIT(_m[tile].m4, 2)) SetSignalType(tile, SIGTYPE_COMBO);
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
|
2006-06-08 19:27:20 +00:00
|
|
|
// move the signal variant back
|
|
|
|
SetSignalVariant(tile, HASBIT(_m[tile].m4, 3) ? SIG_SEMAPHORE : SIG_ELECTRIC);
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
CLRBIT(_m[tile].m4, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear PBS reservation on track
|
2006-06-27 21:25:53 +00:00
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) {
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
SB(_m[tile].m4, 4, 4, 0);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
CLRBIT(_m[tile].m3, 6);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear PBS reservation on crossing
|
|
|
|
if (IsTileType(tile, MP_STREET) && IsLevelCrossing(tile))
|
|
|
|
CLRBIT(_m[tile].m5, 0);
|
|
|
|
|
|
|
|
// Clear PBS reservation on station
|
2006-01-30 14:03:34 +00:00
|
|
|
if (IsTileType(tile, MP_STATION))
|
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
- The only change is that the nsignalsw.grf file is kept and that existing nightlies with PBS signals get those signals converted to combo-signals.
2006-01-29 18:57:26 +00:00
|
|
|
CLRBIT(_m[tile].m3, 6);
|
|
|
|
} END_TILE_LOOP(tile, MapSizeX(), MapSizeY(), 0);
|
|
|
|
}
|
|
|
|
|
2006-03-02 02:22:15 +00:00
|
|
|
if (CheckSavegameVersion(22)) UpdatePatches();
|
|
|
|
|
2006-04-03 14:25:32 +00:00
|
|
|
if (CheckSavegameVersion(25)) {
|
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Road) {
|
|
|
|
v->vehstatus &= ~0x40;
|
|
|
|
v->u.road.slot = NULL;
|
|
|
|
v->u.road.slot_age = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-18 08:50:17 +00:00
|
|
|
if (CheckSavegameVersion(26)) {
|
|
|
|
Station *st;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
st->last_vehicle_type = VEH_Invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
FOR_ALL_PLAYERS(p) p->avail_railtypes = GetPlayerRailtypes(p->index);
|
2005-07-20 22:02:58 +00:00
|
|
|
|
2006-05-06 21:46:26 +00:00
|
|
|
if (!CheckSavegameVersion(27)) AfterLoadStations();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|