src/openttd.cpp
branchgamebalance
changeset 9912 1ac8aac92385
parent 9911 0b8b245a2391
child 9913 e79cd19772dd
equal deleted inserted replaced
9911:0b8b245a2391 9912:1ac8aac92385
    62 #include "newgrf_config.h"
    62 #include "newgrf_config.h"
    63 #include "newgrf_house.h"
    63 #include "newgrf_house.h"
    64 #include "newgrf_commons.h"
    64 #include "newgrf_commons.h"
    65 #include "player_face.h"
    65 #include "player_face.h"
    66 #include "group.h"
    66 #include "group.h"
       
    67 #include "blitter/blitter.hpp"
    67 
    68 
    68 #include "bridge_map.h"
    69 #include "bridge_map.h"
    69 #include "clear_map.h"
    70 #include "clear_map.h"
    70 #include "rail_map.h"
    71 #include "rail_map.h"
    71 #include "road_map.h"
    72 #include "road_map.h"
    82 void ResetMusic();
    83 void ResetMusic();
    83 
    84 
    84 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
    85 extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
    85 extern Player* DoStartupNewPlayer(bool is_ai);
    86 extern Player* DoStartupNewPlayer(bool is_ai);
    86 extern void ShowOSErrorBox(const char *buf);
    87 extern void ShowOSErrorBox(const char *buf);
       
    88 
       
    89 const char *_default_blitter = "8bpp-optimzed";
    87 
    90 
    88 /* TODO: usrerror() for errors which are not of an internal nature but
    91 /* TODO: usrerror() for errors which are not of an internal nature but
    89  * caused by the user, i.e. missing files or fatal configuration errors.
    92  * caused by the user, i.e. missing files or fatal configuration errors.
    90  * Post-0.4.0 since Celestar doesn't want this in SVN before. --pasky */
    93  * Post-0.4.0 since Celestar doesn't want this in SVN before. --pasky */
    91 
    94 
   157 		"\n"
   160 		"\n"
   158 		"Command line options:\n"
   161 		"Command line options:\n"
   159 		"  -v drv              = Set video driver (see below)\n"
   162 		"  -v drv              = Set video driver (see below)\n"
   160 		"  -s drv              = Set sound driver (see below) (param bufsize,hz)\n"
   163 		"  -s drv              = Set sound driver (see below) (param bufsize,hz)\n"
   161 		"  -m drv              = Set music driver (see below)\n"
   164 		"  -m drv              = Set music driver (see below)\n"
       
   165 		"  -b drv              = Set the blitter to use (see below)\n"
   162 		"  -r res              = Set resolution (for instance 800x600)\n"
   166 		"  -r res              = Set resolution (for instance 800x600)\n"
   163 		"  -h                  = Display this help text\n"
   167 		"  -h                  = Display this help text\n"
   164 		"  -t year             = Set starting year\n"
   168 		"  -t year             = Set starting year\n"
   165 		"  -d [[fac=]lvl[,...]]= Debug mode\n"
   169 		"  -d [[fac=]lvl[,...]]= Debug mode\n"
   166 		"  -e                  = Start Editor\n"
   170 		"  -e                  = Start Editor\n"
   180 		"  -x                  = Do not automatically save to config file on exit\n",
   184 		"  -x                  = Do not automatically save to config file on exit\n",
   181 		lastof(buf)
   185 		lastof(buf)
   182 	);
   186 	);
   183 
   187 
   184 	p = GetDriverList(p, lastof(buf));
   188 	p = GetDriverList(p, lastof(buf));
       
   189 
       
   190 	/* List the blitters */
       
   191 	p = BlitterFactoryBase::GetBlittersInfo(p, lastof(buf));
   185 
   192 
   186 	/* ShowInfo put output to stderr, but version information should go
   193 	/* ShowInfo put output to stderr, but version information should go
   187 	 * to stdout; this is the only exception */
   194 	 * to stdout; this is the only exception */
   188 #if !defined(WIN32) && !defined(WIN64)
   195 #if !defined(WIN32) && !defined(WIN64)
   189 	printf("%s\n", buf);
   196 	printf("%s\n", buf);
   348 
   355 
   349 int ttd_main(int argc, char *argv[])
   356 int ttd_main(int argc, char *argv[])
   350 {
   357 {
   351 	int i;
   358 	int i;
   352 	const char *optformat;
   359 	const char *optformat;
   353 	char musicdriver[32], sounddriver[32], videodriver[32];
   360 	char musicdriver[32], sounddriver[32], videodriver[32], blitter[32];
   354 	int resolution[2] = {0, 0};
   361 	int resolution[2] = {0, 0};
   355 	Year startyear = INVALID_YEAR;
   362 	Year startyear = INVALID_YEAR;
   356 	uint generation_seed = GENERATE_NEW_SEED;
   363 	uint generation_seed = GENERATE_NEW_SEED;
   357 	bool save_config = true;
   364 	bool save_config = true;
   358 #if defined(ENABLE_NETWORK)
   365 #if defined(ENABLE_NETWORK)
   362 	char *debuglog_conn = NULL;
   369 	char *debuglog_conn = NULL;
   363 	char *dedicated_host = NULL;
   370 	char *dedicated_host = NULL;
   364 	uint16 dedicated_port = 0;
   371 	uint16 dedicated_port = 0;
   365 #endif /* ENABLE_NETWORK */
   372 #endif /* ENABLE_NETWORK */
   366 
   373 
   367 	musicdriver[0] = sounddriver[0] = videodriver[0] = '\0';
   374 	musicdriver[0] = sounddriver[0] = videodriver[0] = blitter[0] = '\0';
   368 
   375 
   369 	_game_mode = GM_MENU;
   376 	_game_mode = GM_MENU;
   370 	_switch_mode = SM_MENU;
   377 	_switch_mode = SM_MENU;
   371 	_switch_mode_errorstr = INVALID_STRING_ID;
   378 	_switch_mode_errorstr = INVALID_STRING_ID;
   372 	_dedicated_forks = false;
   379 	_dedicated_forks = false;
   374 
   381 
   375 	/* The last param of the following function means this:
   382 	/* The last param of the following function means this:
   376 	 *   a letter means: it accepts that param (e.g.: -h)
   383 	 *   a letter means: it accepts that param (e.g.: -h)
   377 	 *   a ':' behind it means: it need a param (e.g.: -m<driver>)
   384 	 *   a ':' behind it means: it need a param (e.g.: -m<driver>)
   378 	 *   a '::' behind it means: it can optional have a param (e.g.: -d<debug>) */
   385 	 *   a '::' behind it means: it can optional have a param (e.g.: -d<debug>) */
   379 	optformat = "m:s:v:hD::n::eit:d::r:g::G:c:xl:"
   386 	optformat = "m:s:v:b:hD::n::eit:d::r:g::G:c:xl:"
   380 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
   387 #if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
   381 		"f"
   388 		"f"
   382 #endif
   389 #endif
   383 	;
   390 	;
   384 
   391 
   387 	while ((i = MyGetOpt(&mgo)) != -1) {
   394 	while ((i = MyGetOpt(&mgo)) != -1) {
   388 		switch (i) {
   395 		switch (i) {
   389 		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
   396 		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
   390 		case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
   397 		case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
   391 		case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
   398 		case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
       
   399 		case 'b': ttd_strlcpy(blitter, mgo.opt, sizeof(blitter)); break;
   392 #if defined(ENABLE_NETWORK)
   400 #if defined(ENABLE_NETWORK)
   393 		case 'D':
   401 		case 'D':
   394 			strcpy(musicdriver, "null");
   402 			strcpy(musicdriver, "null");
   395 			strcpy(sounddriver, "null");
   403 			strcpy(sounddriver, "null");
   396 			strcpy(videodriver, "dedicated");
   404 			strcpy(videodriver, "dedicated");
   459 
   467 
   460 	/* override config? */
   468 	/* override config? */
   461 	if (!StrEmpty(musicdriver)) ttd_strlcpy(_ini_musicdriver, musicdriver, sizeof(_ini_musicdriver));
   469 	if (!StrEmpty(musicdriver)) ttd_strlcpy(_ini_musicdriver, musicdriver, sizeof(_ini_musicdriver));
   462 	if (!StrEmpty(sounddriver)) ttd_strlcpy(_ini_sounddriver, sounddriver, sizeof(_ini_sounddriver));
   470 	if (!StrEmpty(sounddriver)) ttd_strlcpy(_ini_sounddriver, sounddriver, sizeof(_ini_sounddriver));
   463 	if (!StrEmpty(videodriver)) ttd_strlcpy(_ini_videodriver, videodriver, sizeof(_ini_videodriver));
   471 	if (!StrEmpty(videodriver)) ttd_strlcpy(_ini_videodriver, videodriver, sizeof(_ini_videodriver));
       
   472 	if (StrEmpty(blitter)) ttd_strlcpy(blitter, _default_blitter, sizeof(blitter));
   464 	if (resolution[0] != 0) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; }
   473 	if (resolution[0] != 0) { _cur_resolution[0] = resolution[0]; _cur_resolution[1] = resolution[1]; }
   465 	if (startyear != INVALID_YEAR) _patches_newgame.starting_year = startyear;
   474 	if (startyear != INVALID_YEAR) _patches_newgame.starting_year = startyear;
   466 	if (generation_seed != GENERATE_NEW_SEED) _patches_newgame.generation_seed = generation_seed;
   475 	if (generation_seed != GENERATE_NEW_SEED) _patches_newgame.generation_seed = generation_seed;
   467 
   476 
   468 #if defined(ENABLE_NETWORK)
   477 #if defined(ENABLE_NETWORK)
   498 	InitWindowSystem();
   507 	InitWindowSystem();
   499 
   508 
   500 	/* Initialize game palette */
   509 	/* Initialize game palette */
   501 	GfxInitPalettes();
   510 	GfxInitPalettes();
   502 
   511 
       
   512 	DEBUG(misc, 1, "Loading blitter '%s'...", blitter);
       
   513 	if (BlitterFactoryBase::SelectBlitter(blitter) == NULL)
       
   514 		error("Failed to select requested blitter '%s'; does it exist?", blitter);
   503 	DEBUG(driver, 1, "Loading drivers...");
   515 	DEBUG(driver, 1, "Loading drivers...");
   504 	LoadDriver(SOUND_DRIVER, _ini_sounddriver);
   516 	LoadDriver(SOUND_DRIVER, _ini_sounddriver);
   505 	LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
   517 	LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
   506 	LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
   518 	LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
   507 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
   519 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
   989 {
  1001 {
   990 	if (_game_mode != GM_MENU) {
  1002 	if (_game_mode != GM_MENU) {
   991 		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
  1003 		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
   992 		assert(w);
  1004 		assert(w);
   993 
  1005 
   994 		WP(w,vp_d).scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
  1006 		WP(w,vp_d).dest_scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
   995 		WP(w,vp_d).scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
  1007 		WP(w,vp_d).dest_scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
   996 	}
  1008 	}
   997 }
  1009 }
   998 
  1010 
   999 static const int8 scrollamt[16][2] = {
  1011 static const int8 scrollamt[16][2] = {
  1000 	{ 0,  0},
  1012 	{ 0,  0},
  1282 
  1294 
  1283 	w = FindWindowById(WC_MAIN_WINDOW, 0);
  1295 	w = FindWindowById(WC_MAIN_WINDOW, 0);
  1284 
  1296 
  1285 	WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
  1297 	WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
  1286 	WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
  1298 	WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
       
  1299 	WP(w,vp_d).dest_scrollpos_x = _saved_scrollpos_x;
       
  1300 	WP(w,vp_d).dest_scrollpos_y = _saved_scrollpos_y;
  1287 
  1301 
  1288 	vp = w->viewport;
  1302 	vp = w->viewport;
  1289 	vp->zoom = _saved_scrollpos_zoom;
  1303 	vp->zoom = _saved_scrollpos_zoom;
  1290 	vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
  1304 	vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
  1291 	vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
  1305 	vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
  1442 		}
  1456 		}
  1443 	}
  1457 	}
  1444 
  1458 
  1445 	if (CheckSavegameVersion(61)) {
  1459 	if (CheckSavegameVersion(61)) {
  1446 		/* Added the RoadType */
  1460 		/* Added the RoadType */
       
  1461 		bool old_bridge = CheckSavegameVersion(42);
  1447 		for (TileIndex t = 0; t < map_size; t++) {
  1462 		for (TileIndex t = 0; t < map_size; t++) {
  1448 			switch(GetTileType(t)) {
  1463 			switch(GetTileType(t)) {
  1449 				case MP_STREET:
  1464 				case MP_STREET:
  1450 					SB(_m[t].m5, 6, 2, GB(_m[t].m5, 4, 2));
  1465 					SB(_m[t].m5, 6, 2, GB(_m[t].m5, 4, 2));
  1451 					switch (GetRoadTileType(t)) {
  1466 					switch (GetRoadTileType(t)) {
  1466 				case MP_STATION:
  1481 				case MP_STATION:
  1467 					if (IsRoadStop(t)) SetRoadTypes(t, ROADTYPES_ROAD);
  1482 					if (IsRoadStop(t)) SetRoadTypes(t, ROADTYPES_ROAD);
  1468 					break;
  1483 					break;
  1469 
  1484 
  1470 				case MP_TUNNELBRIDGE:
  1485 				case MP_TUNNELBRIDGE:
  1471 					if ((IsTunnel(t) ? GetTunnelTransportType(t) : GetBridgeTransportType(t)) == TRANSPORT_ROAD) {
  1486 					/* Middle part of "old" bridges */
       
  1487 					if (old_bridge && IsBridgeTile(t) && HASBIT(_m[t].m5, 6)) break;
       
  1488 					if ((IsTunnel(t) ? GetTunnelTransportType(t) : (old_bridge ? (TransportType)GB(_m[t].m5, 1, 2) : GetBridgeTransportType(t))) == TRANSPORT_ROAD) {
  1472 						SetRoadTypes(t, ROADTYPES_ROAD);
  1489 						SetRoadTypes(t, ROADTYPES_ROAD);
  1473 					}
  1490 					}
  1474 					break;
  1491 					break;
  1475 
  1492 
  1476 				default: break;
  1493 				default: break;
  1657 		for (TileIndex t = 0; t < map_size; t++) {
  1674 		for (TileIndex t = 0; t < map_size; t++) {
  1658 			switch (GetTileType(t)) {
  1675 			switch (GetTileType(t)) {
  1659 				case MP_RAILWAY:
  1676 				case MP_RAILWAY:
  1660 					if (HasSignals(t)) {
  1677 					if (HasSignals(t)) {
  1661 						/* convert PBS signals to combo-signals */
  1678 						/* convert PBS signals to combo-signals */
  1662 						if (HASBIT(_m[t].m2, 2)) SetSignalType(t, SIGTYPE_COMBO);
  1679 						if (HASBIT(_m[t].m2, 2)) SetSignalType(t, TRACK_X, SIGTYPE_COMBO);
  1663 
  1680 
  1664 						/* move the signal variant back */
  1681 						/* move the signal variant back */
  1665 						SetSignalVariant(t, HASBIT(_m[t].m2, 3) ? SIG_SEMAPHORE : SIG_ELECTRIC);
  1682 						SetSignalVariant(t, TRACK_X, HASBIT(_m[t].m2, 3) ? SIG_SEMAPHORE : SIG_ELECTRIC);
  1666 						CLRBIT(_m[t].m2, 3);
  1683 						CLRBIT(_m[t].m2, 3);
  1667 					}
  1684 					}
  1668 
  1685 
  1669 					/* Clear PBS reservation on track */
  1686 					/* Clear PBS reservation on track */
  1670 					if (!IsTileDepotType(t, TRANSPORT_RAIL)) {
  1687 					if (!IsTileDepotType(t, TRANSPORT_RAIL)) {
  1714 
  1731 
  1715 	YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
  1732 	YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK);
  1716 
  1733 
  1717 	if (CheckSavegameVersion(34)) FOR_ALL_PLAYERS(p) ResetPlayerLivery(p);
  1734 	if (CheckSavegameVersion(34)) FOR_ALL_PLAYERS(p) ResetPlayerLivery(p);
  1718 
  1735 
  1719 	FOR_ALL_PLAYERS(p) p->avail_railtypes = GetPlayerRailtypes(p->index);
  1736 	FOR_ALL_PLAYERS(p) {
       
  1737 		p->avail_railtypes = GetPlayerRailtypes(p->index);
       
  1738 		p->avail_roadtypes = GetPlayerRoadtypes(p->index);
       
  1739 	}
  1720 
  1740 
  1721 	if (!CheckSavegameVersion(27)) AfterLoadStations();
  1741 	if (!CheckSavegameVersion(27)) AfterLoadStations();
  1722 
  1742 
  1723 	{
  1743 	{
  1724 		/* Set up the engine count for all players */
  1744 		/* Set up the engine count for all players */
  1771 		}
  1791 		}
  1772 
  1792 
  1773 		FOR_ALL_INDUSTRIES(i) {
  1793 		FOR_ALL_INDUSTRIES(i) {
  1774 			uint j;
  1794 			uint j;
  1775 
  1795 
  1776 			if (i->type == IT_FARM || i->type == IT_FARM_2) {
  1796 			if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
  1777 				for (j = 0; j != 50; j++) PlantRandomFarmField(i);
  1797 				for (j = 0; j != 50; j++) PlantRandomFarmField(i);
  1778 			}
  1798 			}
  1779 		}
  1799 		}
  1780 	}
  1800 	}
  1781 
  1801 
  1991 
  2011 
  1992 		/* Same goes for number of towns, although no test is needed, just an increment */
  2012 		/* Same goes for number of towns, although no test is needed, just an increment */
  1993 		_opt.diff.number_towns++;
  2013 		_opt.diff.number_towns++;
  1994 	}
  2014 	}
  1995 
  2015 
       
  2016 	if (CheckSavegameVersion(64)) {
       
  2017 		/* copy the signal type/variant and move signal states bits */
       
  2018 		for (TileIndex t = 0; t < map_size; t++) {
       
  2019 			if (IsTileType(t, MP_RAILWAY) && HasSignals(t)) {
       
  2020 				SetSignalStates(t, GB(_m[t].m2, 4, 4));
       
  2021 				SetSignalVariant(t, INVALID_TRACK, GetSignalVariant(t, TRACK_X));
       
  2022 				SetSignalType(t, INVALID_TRACK, GetSignalType(t, TRACK_X));
       
  2023 				CLRBIT(_m[t].m2, 7);
       
  2024 			}
       
  2025 		}
       
  2026 	}
       
  2027 
  1996 	/* Recalculate */
  2028 	/* Recalculate */
  1997 	Group *g;
  2029 	Group *g;
  1998 	FOR_ALL_GROUPS(g) {
  2030 	FOR_ALL_GROUPS(g) {
  1999 		const Vehicle *v;
  2031 		const Vehicle *v;
  2000 		FOR_ALL_VEHICLES(v) {
  2032 		FOR_ALL_VEHICLES(v) {
  2004 
  2036 
  2005 			g->num_engines[v->engine_type]++;
  2037 			g->num_engines[v->engine_type]++;
  2006 		}
  2038 		}
  2007 	}
  2039 	}
  2008 
  2040 
  2009 	if (CheckSavegameVersion(62)) {
  2041 	if (CheckSavegameVersion(65)) {
  2010 		Town *t;
  2042 		Town *t;
  2011 		FOR_ALL_TOWNS(t) t->SetActivity(1);
  2043 		FOR_ALL_TOWNS(t) t->SetActivity(1);
  2012 	}
  2044 	}
  2013 
  2045 
  2014 	return true;
  2046 	return true;