src/openttd.cpp
branchnoai
changeset 9624 b71483f2330f
parent 9620 31e38d28a0af
child 9625 3301b1b3889c
equal deleted inserted replaced
9623:ee0173281563 9624:b71483f2330f
    60 #include "clear_map.h"
    60 #include "clear_map.h"
    61 #include "fontcache.h"
    61 #include "fontcache.h"
    62 #include "newgrf.h"
    62 #include "newgrf.h"
    63 #include "newgrf_config.h"
    63 #include "newgrf_config.h"
    64 #include "newgrf_house.h"
    64 #include "newgrf_house.h"
       
    65 #include "newgrf_commons.h"
    65 #include "player_face.h"
    66 #include "player_face.h"
       
    67 #include "group.h"
    66 
    68 
    67 #include "bridge_map.h"
    69 #include "bridge_map.h"
    68 #include "clear_map.h"
    70 #include "clear_map.h"
    69 #include "rail_map.h"
    71 #include "rail_map.h"
    70 #include "road_map.h"
    72 #include "road_map.h"
   298 	CleanPool(&_Industry_pool);
   300 	CleanPool(&_Industry_pool);
   299 	CleanPool(&_Station_pool);
   301 	CleanPool(&_Station_pool);
   300 	CleanPool(&_Vehicle_pool);
   302 	CleanPool(&_Vehicle_pool);
   301 	CleanPool(&_Sign_pool);
   303 	CleanPool(&_Sign_pool);
   302 	CleanPool(&_Order_pool);
   304 	CleanPool(&_Order_pool);
       
   305 	CleanPool(&_Group_pool);
   303 
   306 
   304 	free((void*)_town_sort);
   307 	free((void*)_town_sort);
   305 	free((void*)_industry_sort);
   308 	free((void*)_industry_sort);
   306 
   309 
   307 	free(_config_file);
   310 	free(_config_file);
   509 	DEBUG(driver, 1, "Loading drivers...");
   512 	DEBUG(driver, 1, "Loading drivers...");
   510 	LoadDriver(SOUND_DRIVER, _ini_sounddriver);
   513 	LoadDriver(SOUND_DRIVER, _ini_sounddriver);
   511 	LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
   514 	LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
   512 	LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
   515 	LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
   513 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
   516 	_savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
       
   517 	/* Initialize the zoom level of the screen to normal */
       
   518 	_screen.zoom = ZOOM_LVL_NORMAL;
   514 
   519 
   515 	/* restore saved music volume */
   520 	/* restore saved music volume */
   516 	_music_driver->set_volume(msf.music_vol);
   521 	_music_driver->set_volume(msf.music_vol);
   517 
   522 
   518 	NetworkStartUp(); // initialize network-core
   523 	NetworkStartUp(); // initialize network-core
   692 static void MakeNewGame(bool from_heightmap)
   697 static void MakeNewGame(bool from_heightmap)
   693 {
   698 {
   694 	_game_mode = GM_NORMAL;
   699 	_game_mode = GM_NORMAL;
   695 
   700 
   696 	ResetGRFConfig(true);
   701 	ResetGRFConfig(true);
   697 	ResetHouseIDMapping();
   702 	_house_mngr.ResetMapping();
   698 
   703 
   699 	GenerateWorldSetCallback(&MakeNewGameDone);
   704 	GenerateWorldSetCallback(&MakeNewGameDone);
   700 	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y);
   705 	GenerateWorld(from_heightmap ? GW_HEIGHTMAP : GW_NEWGAME, 1 << _patches.map_x, 1 << _patches.map_y);
   701 }
   706 }
   702 
   707 
   993 {
   998 {
   994 	if (_game_mode != GM_MENU) {
   999 	if (_game_mode != GM_MENU) {
   995 		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
  1000 		Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
   996 		assert(w);
  1001 		assert(w);
   997 
  1002 
   998 		WP(w,vp_d).scrollpos_x += x << w->viewport->zoom;
  1003 		WP(w,vp_d).scrollpos_x += ScaleByZoom(x, w->viewport->zoom);
   999 		WP(w,vp_d).scrollpos_y += y << w->viewport->zoom;
  1004 		WP(w,vp_d).scrollpos_y += ScaleByZoom(y, w->viewport->zoom);
  1000 	}
  1005 	}
  1001 }
  1006 }
  1002 
  1007 
  1003 static const int8 scrollamt[16][2] = {
  1008 static const int8 scrollamt[16][2] = {
  1004 	{ 0,  0},
  1009 	{ 0,  0},
  1110 	TileIndex tile;
  1115 	TileIndex tile;
  1111 
  1116 
  1112 	for (tile = 0; tile != MapSize(); tile++) {
  1117 	for (tile = 0; tile != MapSize(); tile++) {
  1113 		switch (GetTileType(tile)) {
  1118 		switch (GetTileType(tile)) {
  1114 			case MP_STREET:
  1119 			case MP_STREET:
  1115 				if (IsLevelCrossing(tile) && GetCrossingRoadOwner(tile) & 0x80) {
  1120 				if (GB(_m[tile].m5, 4, 2) == ROAD_TILE_CROSSING && HASBIT(_m[tile].m4, 7)) {
  1116 					SetCrossingRoadOwner(tile, OWNER_TOWN);
  1121 					_m[tile].m4 = OWNER_TOWN;
  1117 				}
  1122 				}
  1118 				/* FALLTHROUGH */
  1123 				/* FALLTHROUGH */
  1119 
  1124 
  1120 			case MP_TUNNELBRIDGE:
  1125 			case MP_TUNNELBRIDGE:
  1121 				if (GetTileOwner(tile) & 0x80) SetTileOwner(tile, OWNER_TOWN);
  1126 				if (GetTileOwner(tile) & 0x80) SetTileOwner(tile, OWNER_TOWN);
  1289 	WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
  1294 	WP(w,vp_d).scrollpos_x = _saved_scrollpos_x;
  1290 	WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
  1295 	WP(w,vp_d).scrollpos_y = _saved_scrollpos_y;
  1291 
  1296 
  1292 	vp = w->viewport;
  1297 	vp = w->viewport;
  1293 	vp->zoom = _saved_scrollpos_zoom;
  1298 	vp->zoom = _saved_scrollpos_zoom;
  1294 	vp->virtual_width = vp->width << vp->zoom;
  1299 	vp->virtual_width = ScaleByZoom(vp->width, vp->zoom);
  1295 	vp->virtual_height = vp->height << vp->zoom;
  1300 	vp->virtual_height = ScaleByZoom(vp->height, vp->zoom);
  1296 
  1301 
  1297 	/* in version 4.1 of the savegame, is_active was introduced to determine
  1302 	/* in version 4.1 of the savegame, is_active was introduced to determine
  1298 	 * if a player does exist, rather then checking name_1 */
  1303 	 * if a player does exist, rather then checking name_1 */
  1299 	if (CheckSavegameVersionOldStyle(4, 1)) CheckIsPlayerActive();
  1304 	if (CheckSavegameVersionOldStyle(4, 1)) CheckIsPlayerActive();
  1300 
  1305 
  1416 			p->engine_renew_months = _patches.autorenew_months;
  1421 			p->engine_renew_months = _patches.autorenew_months;
  1417 			p->engine_renew_money  = _patches.autorenew_money;
  1422 			p->engine_renew_money  = _patches.autorenew_money;
  1418 		}
  1423 		}
  1419 	}
  1424 	}
  1420 
  1425 
  1421 	if (CheckSavegameVersion(42)) {
       
  1422 		Vehicle* v;
       
  1423 
       
  1424 		for (TileIndex t = 0; t < map_size; t++) {
       
  1425 			if (MayHaveBridgeAbove(t)) ClearBridgeMiddle(t);
       
  1426 			if (IsBridgeTile(t)) {
       
  1427 				if (HASBIT(_m[t].m5, 6)) { // middle part
       
  1428 					Axis axis = (Axis)GB(_m[t].m5, 0, 1);
       
  1429 
       
  1430 					if (HASBIT(_m[t].m5, 5)) { // transport route under bridge?
       
  1431 						if (GB(_m[t].m5, 3, 2) == TRANSPORT_RAIL) {
       
  1432 							MakeRailNormal(
       
  1433 								t,
       
  1434 								GetTileOwner(t),
       
  1435 								axis == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X,
       
  1436 								GetRailType(t)
       
  1437 							);
       
  1438 						} else {
       
  1439 							TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, (uint)-1)->index : 0;
       
  1440 
       
  1441 							MakeRoadNormal(
       
  1442 								t,
       
  1443 								GetTileOwner(t),
       
  1444 								axis == AXIS_X ? ROAD_Y : ROAD_X,
       
  1445 								town
       
  1446 							);
       
  1447 						}
       
  1448 					} else {
       
  1449 						if (GB(_m[t].m5, 3, 2) == 0) {
       
  1450 							MakeClear(t, CLEAR_GRASS, 3);
       
  1451 						} else {
       
  1452 							MakeCanal(t, GetTileOwner(t));
       
  1453 						}
       
  1454 					}
       
  1455 					SetBridgeMiddle(t, axis);
       
  1456 				} else { // ramp
       
  1457 					Axis axis = (Axis)GB(_m[t].m5, 0, 1);
       
  1458 					uint north_south = GB(_m[t].m5, 5, 1);
       
  1459 					DiagDirection dir = ReverseDiagDir(XYNSToDiagDir(axis, north_south));
       
  1460 					TransportType type = (TransportType)GB(_m[t].m5, 1, 2);
       
  1461 
       
  1462 					_m[t].m5 = 1 << 7 | type << 2 | dir;
       
  1463 				}
       
  1464 			}
       
  1465 		}
       
  1466 
       
  1467 		FOR_ALL_VEHICLES(v) {
       
  1468 			if (v->type != VEH_TRAIN && v->type != VEH_ROAD) continue;
       
  1469 			if (IsBridgeTile(v->tile)) {
       
  1470 				DiagDirection dir = GetBridgeRampDirection(v->tile);
       
  1471 
       
  1472 				if (dir != DirToDiagDir(v->direction)) continue;
       
  1473 				switch (dir) {
       
  1474 					default: NOT_REACHED();
       
  1475 					case DIAGDIR_NE: if ((v->x_pos & 0xF) !=  0)            continue; break;
       
  1476 					case DIAGDIR_SE: if ((v->y_pos & 0xF) != TILE_SIZE - 1) continue; break;
       
  1477 					case DIAGDIR_SW: if ((v->x_pos & 0xF) != TILE_SIZE - 1) continue; break;
       
  1478 					case DIAGDIR_NW: if ((v->y_pos & 0xF) !=  0)            continue; break;
       
  1479 				}
       
  1480 			} else if (v->z_pos > GetSlopeZ(v->x_pos, v->y_pos)) {
       
  1481 				v->tile = GetNorthernBridgeEnd(v->tile);
       
  1482 			} else {
       
  1483 				continue;
       
  1484 			}
       
  1485 			if (v->type == VEH_TRAIN) {
       
  1486 				v->u.rail.track = TRACK_BIT_WORMHOLE;
       
  1487 			} else {
       
  1488 				v->u.road.state = RVSB_WORMHOLE;
       
  1489 			}
       
  1490 		}
       
  1491 	}
       
  1492 
       
  1493 	if (CheckSavegameVersion(48)) {
  1426 	if (CheckSavegameVersion(48)) {
  1494 		for (TileIndex t = 0; t < map_size; t++) {
  1427 		for (TileIndex t = 0; t < map_size; t++) {
  1495 			switch (GetTileType(t)) {
  1428 			switch (GetTileType(t)) {
  1496 				case MP_RAILWAY:
  1429 				case MP_RAILWAY:
  1497 					if (IsPlainRailTile(t)) {
  1430 					if (IsPlainRailTile(t)) {
  1516 				default: break;
  1449 				default: break;
  1517 			}
  1450 			}
  1518 		}
  1451 		}
  1519 	}
  1452 	}
  1520 
  1453 
       
  1454 	if (CheckSavegameVersion(61)) {
       
  1455 		/* Added the RoadType */
       
  1456 		for (TileIndex t = 0; t < map_size; t++) {
       
  1457 			switch(GetTileType(t)) {
       
  1458 				case MP_STREET:
       
  1459 					SB(_m[t].m5, 6, 2, GB(_m[t].m5, 4, 2));
       
  1460 					switch (GetRoadTileType(t)) {
       
  1461 						default: NOT_REACHED();
       
  1462 						case ROAD_TILE_NORMAL:
       
  1463 							SB(_m[t].m4, 0, 4, GB(_m[t].m5, 0, 4));
       
  1464 							SB(_m[t].m4, 4, 4, 0);
       
  1465 							SB(_m[t].m6, 2, 4, 0);
       
  1466 							break;
       
  1467 						case ROAD_TILE_CROSSING:
       
  1468 							SB(_m[t].m4, 5, 2, GB(_m[t].m5, 2, 2));
       
  1469 							break;
       
  1470 						case ROAD_TILE_DEPOT:    break;
       
  1471 					}
       
  1472 					SetRoadTypes(t, ROADTYPES_ROAD);
       
  1473 					break;
       
  1474 
       
  1475 				case MP_STATION:
       
  1476 					if (IsRoadStop(t)) SetRoadTypes(t, ROADTYPES_ROAD);
       
  1477 					break;
       
  1478 
       
  1479 				case MP_TUNNELBRIDGE:
       
  1480 					if ((IsTunnel(t) ? GetTunnelTransportType(t) : GetBridgeTransportType(t)) == TRANSPORT_ROAD) {
       
  1481 						SetRoadTypes(t, ROADTYPES_ROAD);
       
  1482 					}
       
  1483 					break;
       
  1484 
       
  1485 				default: break;
       
  1486 			}
       
  1487 		}
       
  1488 	}
       
  1489 
       
  1490 	if (CheckSavegameVersion(42)) {
       
  1491 		Vehicle* v;
       
  1492 
       
  1493 		for (TileIndex t = 0; t < map_size; t++) {
       
  1494 			if (MayHaveBridgeAbove(t)) ClearBridgeMiddle(t);
       
  1495 			if (IsBridgeTile(t)) {
       
  1496 				if (HASBIT(_m[t].m5, 6)) { // middle part
       
  1497 					Axis axis = (Axis)GB(_m[t].m5, 0, 1);
       
  1498 
       
  1499 					if (HASBIT(_m[t].m5, 5)) { // transport route under bridge?
       
  1500 						if (GB(_m[t].m5, 3, 2) == TRANSPORT_RAIL) {
       
  1501 							MakeRailNormal(
       
  1502 								t,
       
  1503 								GetTileOwner(t),
       
  1504 								axis == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X,
       
  1505 								GetRailType(t)
       
  1506 							);
       
  1507 						} else {
       
  1508 							TownID town = IsTileOwner(t, OWNER_TOWN) ? ClosestTownFromTile(t, (uint)-1)->index : 0;
       
  1509 
       
  1510 							MakeRoadNormal(
       
  1511 								t,
       
  1512 								axis == AXIS_X ? ROAD_Y : ROAD_X,
       
  1513 								ROADTYPES_ROAD,
       
  1514 								town,
       
  1515 								GetTileOwner(t), OWNER_NONE, OWNER_NONE
       
  1516 							);
       
  1517 						}
       
  1518 					} else {
       
  1519 						if (GB(_m[t].m5, 3, 2) == 0) {
       
  1520 							MakeClear(t, CLEAR_GRASS, 3);
       
  1521 						} else {
       
  1522 							MakeCanal(t, GetTileOwner(t));
       
  1523 						}
       
  1524 					}
       
  1525 					SetBridgeMiddle(t, axis);
       
  1526 				} else { // ramp
       
  1527 					Axis axis = (Axis)GB(_m[t].m5, 0, 1);
       
  1528 					uint north_south = GB(_m[t].m5, 5, 1);
       
  1529 					DiagDirection dir = ReverseDiagDir(XYNSToDiagDir(axis, north_south));
       
  1530 					TransportType type = (TransportType)GB(_m[t].m5, 1, 2);
       
  1531 
       
  1532 					_m[t].m5 = 1 << 7 | type << 2 | dir;
       
  1533 				}
       
  1534 			}
       
  1535 		}
       
  1536 
       
  1537 		FOR_ALL_VEHICLES(v) {
       
  1538 			if (v->type != VEH_TRAIN && v->type != VEH_ROAD) continue;
       
  1539 			if (IsBridgeTile(v->tile)) {
       
  1540 				DiagDirection dir = GetBridgeRampDirection(v->tile);
       
  1541 
       
  1542 				if (dir != DirToDiagDir(v->direction)) continue;
       
  1543 				switch (dir) {
       
  1544 					default: NOT_REACHED();
       
  1545 					case DIAGDIR_NE: if ((v->x_pos & 0xF) !=  0)            continue; break;
       
  1546 					case DIAGDIR_SE: if ((v->y_pos & 0xF) != TILE_SIZE - 1) continue; break;
       
  1547 					case DIAGDIR_SW: if ((v->x_pos & 0xF) != TILE_SIZE - 1) continue; break;
       
  1548 					case DIAGDIR_NW: if ((v->y_pos & 0xF) !=  0)            continue; break;
       
  1549 				}
       
  1550 			} else if (v->z_pos > GetSlopeZ(v->x_pos, v->y_pos)) {
       
  1551 				v->tile = GetNorthernBridgeEnd(v->tile);
       
  1552 			} else {
       
  1553 				continue;
       
  1554 			}
       
  1555 			if (v->type == VEH_TRAIN) {
       
  1556 				v->u.rail.track = TRACK_BIT_WORMHOLE;
       
  1557 			} else {
       
  1558 				v->u.road.state = RVSB_WORMHOLE;
       
  1559 			}
       
  1560 		}
       
  1561 	}
       
  1562 
  1521 	/* Elrails got added in rev 24 */
  1563 	/* Elrails got added in rev 24 */
  1522 	if (CheckSavegameVersion(24)) {
  1564 	if (CheckSavegameVersion(24)) {
  1523 		Vehicle *v;
  1565 		Vehicle *v;
  1524 		RailType min_rail = RAILTYPE_ELECTRIC;
  1566 		RailType min_rail = RAILTYPE_ELECTRIC;
  1525 
  1567 
  1774 
  1816 
  1775 	/* From version 53, the map array was changed for house tiles to allow
  1817 	/* From version 53, the map array was changed for house tiles to allow
  1776 	 * space for newhouses grf features. A new byte, m7, was also added. */
  1818 	 * space for newhouses grf features. A new byte, m7, was also added. */
  1777 	if (CheckSavegameVersion(53)) {
  1819 	if (CheckSavegameVersion(53)) {
  1778 		for (TileIndex t = 0; t < map_size; t++) {
  1820 		for (TileIndex t = 0; t < map_size; t++) {
  1779 			_me[t].m7 = 0;
       
  1780 
       
  1781 			if (IsTileType(t, MP_HOUSE)) {
  1821 			if (IsTileType(t, MP_HOUSE)) {
  1782 				if (GB(_m[t].m3, 6, 2) != TOWN_HOUSE_COMPLETED) {
  1822 				if (GB(_m[t].m3, 6, 2) != TOWN_HOUSE_COMPLETED) {
  1783 					/* Move the construction stage from m3[7..6] to m5[5..4].
  1823 					/* Move the construction stage from m3[7..6] to m5[5..4].
  1784 					 * The construction counter does not have to move. */
  1824 					 * The construction counter does not have to move. */
  1785 					SB(_m[t].m5, 3, 2, GB(_m[t].m3, 6, 2));
  1825 					SB(_m[t].m5, 3, 2, GB(_m[t].m3, 6, 2));
  1941 		FOR_ALL_VEHICLES(v) {
  1981 		FOR_ALL_VEHICLES(v) {
  1942 			if ((v->type != VEH_TRAIN || IsFrontEngine(v)) &&  // for all locs
  1982 			if ((v->type != VEH_TRAIN || IsFrontEngine(v)) &&  // for all locs
  1943 					!(v->vehstatus & (VS_STOPPED | VS_CRASHED)) && // not stopped or crashed
  1983 					!(v->vehstatus & (VS_STOPPED | VS_CRASHED)) && // not stopped or crashed
  1944 					v->current_order.type == OT_LOADING) {         // loading
  1984 					v->current_order.type == OT_LOADING) {         // loading
  1945 				GetStation(v->last_station_visited)->loading_vehicles.push_back(v);
  1985 				GetStation(v->last_station_visited)->loading_vehicles.push_back(v);
       
  1986 
       
  1987 				/* The loading finished flag is *only* set when actually completely
       
  1988 				 * finished. Because the vehicle is loading, it is not finished. */
       
  1989 				CLRBIT(v->vehicle_flags, VF_LOADING_FINISHED);
  1946 			}
  1990 			}
  1947 		}
  1991 		}
  1948 	}
  1992 	}
  1949 
  1993 
  1950 	if (CheckSavegameVersion(58)) {
  1994 	if (CheckSavegameVersion(58)) {
  1955 		}
  1999 		}
  1956 
  2000 
  1957 		/* Same goes for number of towns, although no test is needed, just an increment */
  2001 		/* Same goes for number of towns, although no test is needed, just an increment */
  1958 		_opt.diff.number_towns++;
  2002 		_opt.diff.number_towns++;
  1959 	}
  2003 	}
       
  2004 
       
  2005 	/* Recalculate */
       
  2006 	Group *g;
       
  2007 	FOR_ALL_GROUPS(g) {
       
  2008 		const Vehicle *v;
       
  2009 		FOR_ALL_VEHICLES(v) {
       
  2010 			if (!IsEngineCountable(v)) continue;
       
  2011 
       
  2012 			if (v->group_id != g->index || v->type != g->vehicle_type || v->owner != g->owner) continue;
       
  2013 
       
  2014 			g->num_engines[v->engine_type]++;
       
  2015 		}
       
  2016 	}
       
  2017 
  1960 
  2018 
  1961 	return true;
  2019 	return true;
  1962 }
  2020 }
  1963 
  2021 
  1964 /** Reload all NewGRF files during a running game. This is a cut-down
  2022 /** Reload all NewGRF files during a running game. This is a cut-down