2007-12-17 01:35:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file road.cpp Generic road related functions. */
|
|
|
|
|
2007-09-26 16:12:43 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "rail_map.h"
|
|
|
|
#include "road_map.h"
|
|
|
|
#include "water_map.h"
|
2008-01-09 21:05:03 +00:00
|
|
|
#include "genworld.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_base.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "engine_base.h"
|
2008-01-09 21:05:03 +00:00
|
|
|
#include "date_func.h"
|
2009-02-11 20:41:17 +00:00
|
|
|
#include "landscape.h"
|
2007-09-26 16:12:43 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-11-09 10:40:33 +00:00
|
|
|
/**
|
|
|
|
* Return if the tile is a valid tile for a crossing.
|
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param tile the current tile
|
2009-11-09 10:40:33 +00:00
|
|
|
* @param ax the axis of the road over the rail
|
|
|
|
* @return true if it is a valid tile
|
|
|
|
*/
|
|
|
|
static bool IsPossibleCrossing(const TileIndex tile, Axis ax)
|
2007-09-26 16:12:43 +00:00
|
|
|
{
|
|
|
|
return (IsTileType(tile, MP_RAILWAY) &&
|
2009-05-15 16:07:36 +00:00
|
|
|
GetRailTileType(tile) == RAIL_TILE_NORMAL &&
|
2009-02-11 20:41:17 +00:00
|
|
|
GetTrackBits(tile) == (ax == AXIS_X ? TRACK_BIT_Y : TRACK_BIT_X) &&
|
2011-11-04 10:23:48 +00:00
|
|
|
GetFoundationSlope(tile) == SLOPE_FLAT);
|
2007-09-26 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Clean up unnecessary RoadBits of a planed tile.
|
2011-01-18 23:09:43 +00:00
|
|
|
* @param tile current tile
|
|
|
|
* @param org_rb planed RoadBits
|
|
|
|
* @return optimised RoadBits
|
|
|
|
*/
|
2007-09-26 16:12:43 +00:00
|
|
|
RoadBits CleanUpRoadBits(const TileIndex tile, RoadBits org_rb)
|
|
|
|
{
|
2009-01-29 16:14:46 +00:00
|
|
|
if (!IsValidTile(tile)) return ROAD_NONE;
|
2007-09-26 16:12:43 +00:00
|
|
|
for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
|
|
|
|
const TileIndex neighbor_tile = TileAddByDiagDir(tile, dir);
|
|
|
|
|
|
|
|
/* Get the Roadbit pointing to the neighbor_tile */
|
|
|
|
const RoadBits target_rb = DiagDirToRoadBits(dir);
|
|
|
|
|
|
|
|
/* If the roadbit is in the current plan */
|
|
|
|
if (org_rb & target_rb) {
|
|
|
|
bool connective = false;
|
|
|
|
const RoadBits mirrored_rb = MirrorRoadBits(target_rb);
|
|
|
|
|
2015-10-30 17:19:01 +00:00
|
|
|
if (IsValidTile(neighbor_tile)) {
|
|
|
|
switch (GetTileType(neighbor_tile)) {
|
|
|
|
/* Always connective ones */
|
|
|
|
case MP_CLEAR: case MP_TREES:
|
|
|
|
connective = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* The conditionally connective ones */
|
|
|
|
case MP_TUNNELBRIDGE:
|
|
|
|
case MP_STATION:
|
|
|
|
case MP_ROAD:
|
|
|
|
if (IsNormalRoadTile(neighbor_tile)) {
|
|
|
|
/* Always connective */
|
|
|
|
connective = true;
|
|
|
|
} else {
|
|
|
|
const RoadBits neighbor_rb = GetAnyRoadBits(neighbor_tile, ROADTYPE_ROAD) | GetAnyRoadBits(neighbor_tile, ROADTYPE_TRAM);
|
|
|
|
|
|
|
|
/* Accept only connective tiles */
|
|
|
|
connective = (neighbor_rb & mirrored_rb) != ROAD_NONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_RAILWAY:
|
|
|
|
connective = IsPossibleCrossing(neighbor_tile, DiagDirToAxis(dir));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_WATER:
|
|
|
|
/* Check for real water tile */
|
|
|
|
connective = !IsWater(neighbor_tile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* The definitely not connective ones */
|
|
|
|
default: break;
|
2010-08-01 18:53:30 +00:00
|
|
|
}
|
2007-09-26 16:12:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* If the neighbor tile is inconnective, remove the planed road connection to it */
|
2007-09-26 16:12:43 +00:00
|
|
|
if (!connective) org_rb ^= target_rb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return org_rb;
|
|
|
|
}
|
2008-01-09 21:05:03 +00:00
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Finds out, whether given company has all given RoadTypes available
|
|
|
|
* @param company ID of company
|
|
|
|
* @param rts RoadTypes to test
|
|
|
|
* @return true if company has all requested RoadTypes available
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
bool HasRoadTypesAvail(const CompanyID company, const RoadTypes rts)
|
2008-01-09 21:05:03 +00:00
|
|
|
{
|
|
|
|
RoadTypes avail_roadtypes;
|
|
|
|
|
2011-12-19 21:02:33 +00:00
|
|
|
if (company == OWNER_DEITY || company == OWNER_TOWN || _game_mode == GM_EDITOR || _generating_world) {
|
2008-01-09 21:05:03 +00:00
|
|
|
avail_roadtypes = ROADTYPES_ROAD;
|
|
|
|
} else {
|
2009-05-18 16:21:28 +00:00
|
|
|
Company *c = Company::GetIfValid(company);
|
|
|
|
if (c == NULL) return false;
|
|
|
|
avail_roadtypes = (RoadTypes)c->avail_roadtypes | ROADTYPES_ROAD; // road is available for always for everybody
|
2008-01-09 21:05:03 +00:00
|
|
|
}
|
|
|
|
return (rts & ~avail_roadtypes) == 0;
|
|
|
|
}
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Validate functions for rail building.
|
|
|
|
* @param rt road type to check.
|
|
|
|
* @return true if the current company may build the road.
|
|
|
|
*/
|
2008-01-09 21:05:03 +00:00
|
|
|
bool ValParamRoadType(const RoadType rt)
|
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
return HasRoadTypesAvail(_current_company, RoadTypeToRoadTypes(rt));
|
2008-01-09 21:05:03 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Get the road types the given company can build.
|
|
|
|
* @param company the company to get the roadtypes for.
|
|
|
|
* @return the road types.
|
|
|
|
*/
|
2008-09-30 20:39:50 +00:00
|
|
|
RoadTypes GetCompanyRoadtypes(CompanyID company)
|
2008-01-09 21:05:03 +00:00
|
|
|
{
|
|
|
|
RoadTypes rt = ROADTYPES_NONE;
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
|
|
|
|
const EngineInfo *ei = &e->info;
|
2008-01-09 21:05:03 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (HasBit(ei->climates, _settings_game.game_creation.landscape) &&
|
2009-01-13 22:58:03 +00:00
|
|
|
(HasBit(e->company_avail, company) || _date >= e->intro_date + DAYS_IN_YEAR)) {
|
2008-01-09 21:05:03 +00:00
|
|
|
SetBit(rt, HasBit(ei->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rt;
|
|
|
|
}
|