2005-07-24 14:12:37 +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 station_cmd.cpp Handling of station tiles. */
|
2005-07-28 05:47:54 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-02-20 06:39:09 +00:00
|
|
|
#include "aircraft.h"
|
2006-12-27 12:38:02 +00:00
|
|
|
#include "bridge_map.h"
|
2007-02-24 09:42:39 +00:00
|
|
|
#include "cmd_helper.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "viewport_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2005-11-18 23:41:03 +00:00
|
|
|
#include "train.h"
|
2007-06-11 14:00:16 +00:00
|
|
|
#include "roadveh.h"
|
2009-08-30 11:47:41 +00:00
|
|
|
#include "industry.h"
|
2009-11-05 19:46:17 +00:00
|
|
|
#include "newgrf_cargo.h"
|
2010-04-24 13:39:11 +00:00
|
|
|
#include "newgrf_debug.h"
|
2006-05-04 20:00:50 +00:00
|
|
|
#include "newgrf_station.h"
|
2009-12-02 16:48:55 +00:00
|
|
|
#include "pathfinder/yapf/yapf_cache.h"
|
2007-12-18 20:38:16 +00:00
|
|
|
#include "road_internal.h" /* For drawing catenary/checking road removal */
|
2007-09-14 22:27:40 +00:00
|
|
|
#include "autoslope.h"
|
2007-11-24 08:45:04 +00:00
|
|
|
#include "water.h"
|
2007-12-05 17:08:10 +00:00
|
|
|
#include "station_gui.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-04-20 08:22:59 +00:00
|
|
|
#include "animated_tile_func.h"
|
2008-05-08 16:48:29 +00:00
|
|
|
#include "elrail_func.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "station_base.h"
|
2009-06-23 21:44:48 +00:00
|
|
|
#include "roadstop_base.h"
|
2010-02-07 22:22:54 +00:00
|
|
|
#include "newgrf_railtype.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_base.h"
|
|
|
|
#include "waypoint_func.h"
|
2009-12-01 23:56:04 +00:00
|
|
|
#include "pbs.h"
|
|
|
|
#include "debug.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/random_func.hpp"
|
|
|
|
#include "company_base.h"
|
2010-01-24 20:25:28 +00:00
|
|
|
#include "table/airporttile_ids.h"
|
2010-02-22 14:09:30 +00:00
|
|
|
#include "newgrf_airporttiles.h"
|
2010-08-18 18:52:16 +00:00
|
|
|
#include "order_backup.h"
|
2008-01-09 23:00:59 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2007-01-14 19:29:21 +00:00
|
|
|
|
2007-07-24 21:48:50 +00:00
|
|
|
/**
|
|
|
|
* Check whether the given tile is a hangar.
|
|
|
|
* @param t the tile to of whether it is a hangar.
|
|
|
|
* @pre IsTileType(t, MP_STATION)
|
|
|
|
* @return true if and only if the tile is a hangar.
|
|
|
|
*/
|
|
|
|
bool IsHangar(TileIndex t)
|
|
|
|
{
|
|
|
|
assert(IsTileType(t, MP_STATION));
|
|
|
|
|
2009-11-10 11:19:07 +00:00
|
|
|
/* If the tile isn't an airport there's no chance it's a hangar. */
|
|
|
|
if (!IsAirport(t)) return false;
|
|
|
|
|
2009-06-24 17:39:54 +00:00
|
|
|
const Station *st = Station::GetByTile(t);
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportSpec *as = st->airport.GetSpec();
|
2007-07-24 21:48:50 +00:00
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
for (uint i = 0; i < as->nof_depots; i++) {
|
2010-03-19 09:58:46 +00:00
|
|
|
if (st->airport.GetHangarTile(i) == t) return true;
|
2007-07-24 21:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-30 21:41:21 +00:00
|
|
|
/**
|
|
|
|
* Look for a station around the given tile area.
|
|
|
|
* @param ta the area to search over
|
|
|
|
* @param closest_station the closest station found so far
|
|
|
|
* @param st to 'return' the found station
|
2010-02-27 13:51:11 +00:00
|
|
|
* @return Succeeded command (if zero or one station found) or failed command (for two or more stations found).
|
2009-07-30 21:41:21 +00:00
|
|
|
*/
|
|
|
|
template <class T>
|
2010-02-27 13:51:11 +00:00
|
|
|
CommandCost GetStationAround(TileArea ta, StationID closest_station, T **st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-12-12 18:23:38 +00:00
|
|
|
ta.tile -= TileDiffXY(1, 1);
|
|
|
|
ta.w += 2;
|
|
|
|
ta.h += 2;
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* check around to see if there's any stations there */
|
2010-12-12 18:23:38 +00:00
|
|
|
TILE_AREA_LOOP(tile_cur, ta) {
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile_cur, MP_STATION)) {
|
2006-03-24 08:55:08 +00:00
|
|
|
StationID t = GetStationIndex(tile_cur);
|
2004-08-12 21:11:23 +00:00
|
|
|
|
2005-10-07 07:35:15 +00:00
|
|
|
if (closest_station == INVALID_STATION) {
|
2009-07-30 21:41:21 +00:00
|
|
|
if (T::IsValidID(t)) closest_station = t;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (closest_station != t) {
|
2010-02-27 13:51:11 +00:00
|
|
|
return_cmd_error(STR_ERROR_ADJOINS_MORE_THAN_ONE_EXISTING);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2009-07-30 21:41:21 +00:00
|
|
|
*st = (closest_station == INVALID_STATION) ? NULL : T::Get(closest_station);
|
2010-02-27 13:51:11 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-24 19:56:43 +00:00
|
|
|
/**
|
|
|
|
* Function to check whether the given tile matches some criterion.
|
|
|
|
* @param tile the tile to check
|
|
|
|
* @return true if it matches, false otherwise
|
|
|
|
*/
|
|
|
|
typedef bool (*CMSAMatcher)(TileIndex tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-11 22:09:21 +00:00
|
|
|
/**
|
|
|
|
* Counts the numbers of tiles matching a specific type in the area around
|
|
|
|
* @param tile the center tile of the 'count area'
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param cmp the comparator/matcher (@see CMSAMatcher)
|
|
|
|
* @return the number of matching tiles around
|
2006-04-11 22:09:21 +00:00
|
|
|
*/
|
2007-07-24 19:56:43 +00:00
|
|
|
static int CountMapSquareAround(TileIndex tile, CMSAMatcher cmp)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
int num = 0;
|
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
for (int dx = -3; dx <= 3; dx++) {
|
|
|
|
for (int dy = -3; dy <= 3; dy++) {
|
2009-02-25 21:29:50 +00:00
|
|
|
TileIndex t = TileAddWrap(tile, dx, dy);
|
|
|
|
if (t != INVALID_TILE && cmp(t)) num++;
|
2006-04-11 22:09:21 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2007-07-24 19:56:43 +00:00
|
|
|
/**
|
|
|
|
* Check whether the tile is a mine.
|
|
|
|
* @param tile the tile to investigate.
|
|
|
|
* @return true if and only if the tile is a mine
|
|
|
|
*/
|
|
|
|
static bool CMSAMine(TileIndex tile)
|
|
|
|
{
|
|
|
|
/* No industry */
|
|
|
|
if (!IsTileType(tile, MP_INDUSTRY)) return false;
|
|
|
|
|
2009-08-30 11:47:41 +00:00
|
|
|
const Industry *ind = Industry::GetByTile(tile);
|
2007-07-24 19:56:43 +00:00
|
|
|
|
|
|
|
/* No extractive industry */
|
2007-09-27 21:39:13 +00:00
|
|
|
if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_EXTRACTIVE) == 0) return false;
|
2007-07-24 19:56:43 +00:00
|
|
|
|
2007-09-27 21:39:13 +00:00
|
|
|
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
|
2009-04-11 17:09:38 +00:00
|
|
|
/* The industry extracts something non-liquid, i.e. no oil or plastic, so it is a mine.
|
|
|
|
* Also the production of passengers and mail is ignored. */
|
|
|
|
if (ind->produced_cargo[i] != CT_INVALID &&
|
2009-07-16 19:00:13 +00:00
|
|
|
(CargoSpec::Get(ind->produced_cargo[i])->classes & (CC_LIQUID | CC_PASSENGERS | CC_MAIL)) == 0) {
|
2009-04-11 17:09:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-07-24 19:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the tile is water.
|
|
|
|
* @param tile the tile to investigate.
|
|
|
|
* @return true if and only if the tile is a mine
|
|
|
|
*/
|
|
|
|
static bool CMSAWater(TileIndex tile)
|
|
|
|
{
|
|
|
|
return IsTileType(tile, MP_WATER) && IsWater(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the tile is a tree.
|
|
|
|
* @param tile the tile to investigate.
|
|
|
|
* @return true if and only if the tile is a mine
|
|
|
|
*/
|
|
|
|
static bool CMSATree(TileIndex tile)
|
|
|
|
{
|
|
|
|
return IsTileType(tile, MP_TREES);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the tile is a forest.
|
|
|
|
* @param tile the tile to investigate.
|
|
|
|
* @return true if and only if the tile is a mine
|
|
|
|
*/
|
|
|
|
static bool CMSAForest(TileIndex tile)
|
|
|
|
{
|
|
|
|
/* No industry */
|
|
|
|
if (!IsTileType(tile, MP_INDUSTRY)) return false;
|
|
|
|
|
2009-08-30 11:47:41 +00:00
|
|
|
const Industry *ind = Industry::GetByTile(tile);
|
2007-07-24 19:56:43 +00:00
|
|
|
|
|
|
|
/* No extractive industry */
|
2007-09-27 21:39:13 +00:00
|
|
|
if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_ORGANIC) == 0) return false;
|
2007-07-24 19:56:43 +00:00
|
|
|
|
2007-09-27 21:39:13 +00:00
|
|
|
for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
|
2007-07-24 19:56:43 +00:00
|
|
|
/* The industry produces wood. */
|
2009-07-16 19:00:13 +00:00
|
|
|
if (ind->produced_cargo[i] != CT_INVALID && CargoSpec::Get(ind->produced_cargo[i])->label == 'WOOD') return true;
|
2007-07-24 19:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#define M(x) ((x) - STR_SV_STNAME)
|
|
|
|
|
2007-10-31 18:53:49 +00:00
|
|
|
enum StationNaming {
|
2009-07-19 15:58:47 +00:00
|
|
|
STATIONNAMING_RAIL,
|
|
|
|
STATIONNAMING_ROAD,
|
2007-10-31 18:01:41 +00:00
|
|
|
STATIONNAMING_AIRPORT,
|
|
|
|
STATIONNAMING_OILRIG,
|
|
|
|
STATIONNAMING_DOCK,
|
|
|
|
STATIONNAMING_HELIPORT,
|
2007-10-31 18:53:49 +00:00
|
|
|
};
|
2007-10-31 18:01:41 +00:00
|
|
|
|
2008-11-19 23:55:34 +00:00
|
|
|
/** Information to handle station action 0 property 24 correctly */
|
|
|
|
struct StationNameInformation {
|
|
|
|
uint32 free_names; ///< Current bitset of free names (we can remove names).
|
|
|
|
bool *indtypes; ///< Array of bools telling whether an industry type has been found.
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a station action 0 property 24 station name, or reduce the
|
|
|
|
* free_names if needed.
|
|
|
|
* @param tile the tile to search
|
|
|
|
* @param user_data the StationNameInformation to base the search on
|
|
|
|
* @return true if the tile contains an industry that has not given
|
2010-04-12 14:12:47 +00:00
|
|
|
* its name to one of the other stations in town.
|
2008-11-19 23:55:34 +00:00
|
|
|
*/
|
|
|
|
static bool FindNearIndustryName(TileIndex tile, void *user_data)
|
|
|
|
{
|
|
|
|
/* All already found industry types */
|
|
|
|
StationNameInformation *sni = (StationNameInformation*)user_data;
|
|
|
|
if (!IsTileType(tile, MP_INDUSTRY)) return false;
|
|
|
|
|
|
|
|
/* If the station name is undefined it means that it doesn't name a station */
|
|
|
|
IndustryType indtype = GetIndustryType(tile);
|
|
|
|
if (GetIndustrySpec(indtype)->station_name == STR_UNDEFINED) return false;
|
|
|
|
|
|
|
|
/* In all cases if an industry that provides a name is found two of
|
|
|
|
* the standard names will be disabled. */
|
|
|
|
sni->free_names &= ~(1 << M(STR_SV_STNAME_OILFIELD) | 1 << M(STR_SV_STNAME_MINES));
|
|
|
|
return !sni->indtypes[indtype];
|
|
|
|
}
|
|
|
|
|
2009-07-19 15:58:47 +00:00
|
|
|
static StringID GenerateStationName(Station *st, TileIndex tile, StationNaming name_class)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
static const uint32 _gen_station_name_bits[] = {
|
2009-07-19 15:58:47 +00:00
|
|
|
0, // STATIONNAMING_RAIL
|
|
|
|
0, // STATIONNAMING_ROAD
|
|
|
|
1U << M(STR_SV_STNAME_AIRPORT), // STATIONNAMING_AIRPORT
|
|
|
|
1U << M(STR_SV_STNAME_OILFIELD), // STATIONNAMING_OILRIG
|
|
|
|
1U << M(STR_SV_STNAME_DOCKS), // STATIONNAMING_DOCK
|
|
|
|
1U << M(STR_SV_STNAME_HELIPORT), // STATIONNAMING_HELIPORT
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2008-04-17 20:03:28 +00:00
|
|
|
const Town *t = st->town;
|
|
|
|
uint32 free_names = UINT32_MAX;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-11-19 23:55:34 +00:00
|
|
|
bool indtypes[NUM_INDUSTRYTYPES];
|
|
|
|
memset(indtypes, 0, sizeof(indtypes));
|
|
|
|
|
2008-04-25 15:22:32 +00:00
|
|
|
const Station *s;
|
|
|
|
FOR_ALL_STATIONS(s) {
|
|
|
|
if (s != st && s->town == t) {
|
2008-11-19 23:55:34 +00:00
|
|
|
if (s->indtype != IT_INVALID) {
|
|
|
|
indtypes[s->indtype] = true;
|
|
|
|
continue;
|
|
|
|
}
|
2008-04-25 15:22:32 +00:00
|
|
|
uint str = M(s->string_id);
|
|
|
|
if (str <= 0x20) {
|
|
|
|
if (str == M(STR_SV_STNAME_FOREST)) {
|
|
|
|
str = M(STR_SV_STNAME_WOODS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-25 15:22:32 +00:00
|
|
|
ClrBit(free_names, str);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
TileIndex indtile = tile;
|
|
|
|
StationNameInformation sni = { free_names, indtypes };
|
|
|
|
if (CircularTileSearch(&indtile, 7, FindNearIndustryName, &sni)) {
|
|
|
|
/* An industry has been found nearby */
|
|
|
|
IndustryType indtype = GetIndustryType(indtile);
|
|
|
|
const IndustrySpec *indsp = GetIndustrySpec(indtype);
|
|
|
|
/* STR_NULL means it only disables oil rig/mines */
|
|
|
|
if (indsp->station_name != STR_NULL) {
|
|
|
|
st->indtype = indtype;
|
|
|
|
return STR_SV_STNAME_FALLBACK;
|
2008-11-19 23:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
/* Oil rigs/mines name could be marked not free by looking for a near by industry. */
|
|
|
|
free_names = sni.free_names;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* check default names */
|
2009-07-19 15:58:47 +00:00
|
|
|
uint32 tmp = free_names & _gen_station_name_bits[name_class];
|
2008-04-25 15:22:32 +00:00
|
|
|
if (tmp != 0) return STR_SV_STNAME + FindFirstBit(tmp);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* check mine? */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME_MINES))) {
|
2007-07-24 19:56:43 +00:00
|
|
|
if (CountMapSquareAround(tile, CMSAMine) >= 2) {
|
2008-04-25 15:22:32 +00:00
|
|
|
return STR_SV_STNAME_MINES;
|
2005-03-08 00:26:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check close enough to town to get central as name? */
|
2007-04-18 22:10:36 +00:00
|
|
|
if (DistanceMax(tile, t->xy) < 8) {
|
2008-04-25 15:22:32 +00:00
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME))) return STR_SV_STNAME;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-25 15:22:32 +00:00
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME_CENTRAL))) return STR_SV_STNAME_CENTRAL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check lakeside */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME_LAKESIDE)) &&
|
2005-03-09 19:09:04 +00:00
|
|
|
DistanceFromEdge(tile) < 20 &&
|
2007-07-24 19:56:43 +00:00
|
|
|
CountMapSquareAround(tile, CMSAWater) >= 5) {
|
2008-04-25 15:22:32 +00:00
|
|
|
return STR_SV_STNAME_LAKESIDE;
|
2005-03-08 00:26:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Check woods */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME_WOODS)) && (
|
2007-07-24 19:56:43 +00:00
|
|
|
CountMapSquareAround(tile, CMSATree) >= 8 ||
|
|
|
|
CountMapSquareAround(tile, CMSAForest) >= 2)
|
2005-03-09 19:09:04 +00:00
|
|
|
) {
|
2008-05-29 15:13:28 +00:00
|
|
|
return _settings_game.game_creation.landscape == LT_TROPIC ? STR_SV_STNAME_FOREST : STR_SV_STNAME_WOODS;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check elevation compared to town */
|
2008-04-25 15:22:32 +00:00
|
|
|
uint z = GetTileZ(tile);
|
|
|
|
uint z2 = GetTileZ(t->xy);
|
|
|
|
if (z < z2) {
|
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME_VALLEY))) return STR_SV_STNAME_VALLEY;
|
|
|
|
} else if (z > z2) {
|
|
|
|
if (HasBit(free_names, M(STR_SV_STNAME_HEIGHTS))) return STR_SV_STNAME_HEIGHTS;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check direction compared to town */
|
2008-04-25 15:22:32 +00:00
|
|
|
static const int8 _direction_and_table[] = {
|
|
|
|
~( (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
|
|
|
|
~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
|
|
|
|
~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
|
|
|
|
~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) ),
|
|
|
|
};
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-25 15:22:32 +00:00
|
|
|
free_names &= _direction_and_table[
|
|
|
|
(TileX(tile) < TileX(t->xy)) +
|
|
|
|
(TileY(tile) < TileY(t->xy)) * 2];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
tmp = free_names & ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 6) | (1 << 7) | (1 << 12) | (1 << 26) | (1 << 27) | (1 << 28) | (1 << 29) | (1 << 30));
|
2008-04-25 15:22:32 +00:00
|
|
|
return (tmp == 0) ? STR_SV_STNAME_FALLBACK : (STR_SV_STNAME + FindFirstBit(tmp));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
#undef M
|
|
|
|
|
2009-01-05 21:06:38 +00:00
|
|
|
/**
|
|
|
|
* Find the closest deleted station of the current company
|
|
|
|
* @param tile the tile to search from.
|
|
|
|
* @return the closest station or NULL if too far.
|
|
|
|
*/
|
|
|
|
static Station *GetClosestDeletedStation(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-17 15:59:33 +00:00
|
|
|
uint threshold = 8;
|
2008-04-17 20:03:28 +00:00
|
|
|
Station *best_station = NULL;
|
|
|
|
Station *st;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2009-07-25 10:39:58 +00:00
|
|
|
if (!st->IsInUse() && st->owner == _current_company) {
|
2005-03-05 18:44:26 +00:00
|
|
|
uint cur_dist = DistanceManhattan(tile, st->xy);
|
|
|
|
|
|
|
|
if (cur_dist < threshold) {
|
|
|
|
threshold = cur_dist;
|
|
|
|
best_station = st;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_station;
|
|
|
|
}
|
|
|
|
|
2009-07-21 11:11:05 +00:00
|
|
|
|
|
|
|
void Station::GetTileArea(TileArea *ta, StationType type) const
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case STATION_RAIL:
|
2009-07-25 08:54:19 +00:00
|
|
|
*ta = this->train_station;
|
2009-07-21 11:11:05 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case STATION_AIRPORT:
|
2010-02-22 14:17:07 +00:00
|
|
|
*ta = this->airport;
|
2009-07-21 16:20:02 +00:00
|
|
|
return;
|
2009-07-21 11:11:05 +00:00
|
|
|
|
|
|
|
case STATION_TRUCK:
|
2009-12-02 16:20:44 +00:00
|
|
|
*ta = this->truck_station;
|
|
|
|
return;
|
2009-07-21 11:11:05 +00:00
|
|
|
|
|
|
|
case STATION_BUS:
|
2009-12-02 16:20:44 +00:00
|
|
|
*ta = this->bus_station;
|
|
|
|
return;
|
2009-07-21 11:11:05 +00:00
|
|
|
|
|
|
|
case STATION_DOCK:
|
|
|
|
case STATION_OILRIG:
|
|
|
|
ta->tile = this->dock_tile;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
ta->w = 1;
|
|
|
|
ta->h = 1;
|
|
|
|
}
|
|
|
|
|
2009-07-07 16:43:58 +00:00
|
|
|
/**
|
2009-07-13 22:33:25 +00:00
|
|
|
* Update the virtual coords needed to draw the station sign.
|
2007-10-26 20:42:03 +00:00
|
|
|
*/
|
2009-07-07 16:43:58 +00:00
|
|
|
void Station::UpdateVirtCoord()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-07 16:43:58 +00:00
|
|
|
Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
pt.y -= 32;
|
2010-03-18 21:02:20 +00:00
|
|
|
if ((this->facilities & FACIL_AIRPORT) && this->airport.type == AT_OILRIG) pt.y -= 16;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-07 16:43:58 +00:00
|
|
|
SetDParam(0, this->index);
|
|
|
|
SetDParam(1, this->facilities);
|
2009-08-05 17:59:21 +00:00
|
|
|
this->sign.UpdatePosition(pt.x, pt.y, STR_VIEWPORT_STATION);
|
2009-08-22 20:28:30 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_STATION_VIEW, this->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/** Update the virtual coords needed to draw the station sign for all stations. */
|
2009-07-13 22:33:25 +00:00
|
|
|
void UpdateAllStationVirtCoords()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-18 08:41:58 +00:00
|
|
|
BaseStation *st;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2009-07-25 10:39:58 +00:00
|
|
|
FOR_ALL_BASE_STATIONS(st) {
|
2009-07-18 08:41:58 +00:00
|
|
|
st->UpdateVirtCoord();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Get a mask of the cargo types that the station accepts.
|
2007-10-26 20:42:03 +00:00
|
|
|
* @param st Station to query
|
|
|
|
* @return the expected mask
|
|
|
|
*/
|
2005-03-05 18:44:26 +00:00
|
|
|
static uint GetAcceptanceMask(const Station *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint mask = 0;
|
2005-03-05 18:44:26 +00:00
|
|
|
|
2007-03-21 13:19:01 +00:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(st->goods[i].acceptance_pickup, GoodsEntry::ACCEPTANCE)) mask |= 1 << i;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Items contains the two cargo names that are to be accepted or rejected.
|
2007-10-26 20:42:03 +00:00
|
|
|
* msg is the string id of the message to display.
|
|
|
|
*/
|
2007-01-16 11:13:00 +00:00
|
|
|
static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *cargo, StringID msg)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-16 11:13:00 +00:00
|
|
|
for (uint i = 0; i < num_items; i++) {
|
2009-07-16 19:00:13 +00:00
|
|
|
SetDParam(i + 1, CargoSpec::Get(cargo[i])->name);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-01-16 11:13:00 +00:00
|
|
|
|
|
|
|
SetDParam(0, st->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddNewsItem(msg, NS_ACCEPTANCE, NR_STATION, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
/**
|
2009-06-27 21:06:58 +00:00
|
|
|
* Get the cargo types being produced around the tile (in a rectangle).
|
2008-11-22 15:48:43 +00:00
|
|
|
* @param tile Northtile of area
|
|
|
|
* @param w X extent of the area
|
|
|
|
* @param h Y extent of the area
|
|
|
|
* @param rad Search radius in addition to the given area
|
2008-04-18 04:54:09 +00:00
|
|
|
*/
|
2009-06-27 21:06:58 +00:00
|
|
|
CargoArray GetProductionAroundTiles(TileIndex tile, int w, int h, int rad)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-06-27 21:06:58 +00:00
|
|
|
CargoArray produced;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
int x = TileX(tile);
|
|
|
|
int y = TileY(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* expand the region by rad tiles on each side
|
|
|
|
* while making sure that we remain inside the board. */
|
2007-02-18 11:27:09 +00:00
|
|
|
int x2 = min(x + w + rad, MapSizeX());
|
|
|
|
int x1 = max(x - rad, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
int y2 = min(y + h + rad, MapSizeY());
|
|
|
|
int y1 = max(y - rad, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(x1 < x2);
|
|
|
|
assert(y1 < y2);
|
|
|
|
assert(w > 0);
|
|
|
|
assert(h > 0);
|
|
|
|
|
2010-01-04 21:10:20 +00:00
|
|
|
TileArea ta(TileXY(x1, y1), TileXY(x2 - 1, y2 - 1));
|
|
|
|
|
|
|
|
/* Loop over all tiles to get the produced cargo of
|
|
|
|
* everything except industries */
|
|
|
|
TILE_AREA_LOOP(tile, ta) AddProducedCargo(tile, produced);
|
|
|
|
|
|
|
|
/* Loop over the industries. They produce cargo for
|
|
|
|
* anything that is within 'rad' from their bounding
|
|
|
|
* box. As such if you have e.g. a oil well the tile
|
|
|
|
* area loop might not hit an industry tile while
|
|
|
|
* the industry would produce cargo for the station.
|
|
|
|
*/
|
|
|
|
const Industry *i;
|
|
|
|
FOR_ALL_INDUSTRIES(i) {
|
|
|
|
if (!ta.Intersects(i->location)) continue;
|
|
|
|
|
|
|
|
for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
|
|
|
|
CargoID cargo = i->produced_cargo[j];
|
|
|
|
if (cargo != CT_INVALID) produced[cargo]++;
|
2005-03-05 18:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-27 21:06:58 +00:00
|
|
|
|
|
|
|
return produced;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
/**
|
2009-06-27 21:06:58 +00:00
|
|
|
* Get the acceptance of cargos around the tile in 1/8.
|
2008-11-22 15:48:43 +00:00
|
|
|
* @param tile Center of the search area
|
|
|
|
* @param w X extent of area
|
|
|
|
* @param h Y extent of area
|
|
|
|
* @param rad Search radius in addition to given area
|
2009-09-20 17:44:33 +00:00
|
|
|
* @param always_accepted bitmask of cargo accepted by houses and headquarters; can be NULL
|
2008-04-18 04:54:09 +00:00
|
|
|
*/
|
2009-09-20 17:44:33 +00:00
|
|
|
CargoArray GetAcceptanceAroundTiles(TileIndex tile, int w, int h, int rad, uint32 *always_accepted)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-06-27 21:06:58 +00:00
|
|
|
CargoArray acceptance;
|
2009-09-20 17:44:33 +00:00
|
|
|
if (always_accepted != NULL) *always_accepted = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
int x = TileX(tile);
|
|
|
|
int y = TileY(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* expand the region by rad tiles on each side
|
|
|
|
* while making sure that we remain inside the board. */
|
2007-02-18 11:27:09 +00:00
|
|
|
int x2 = min(x + w + rad, MapSizeX());
|
|
|
|
int y2 = min(y + h + rad, MapSizeY());
|
|
|
|
int x1 = max(x - rad, 0);
|
|
|
|
int y1 = max(y - rad, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
assert(x1 < x2);
|
|
|
|
assert(y1 < y2);
|
|
|
|
assert(w > 0);
|
|
|
|
assert(h > 0);
|
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
for (int yc = y1; yc != y2; yc++) {
|
|
|
|
for (int xc = x1; xc != x2; xc++) {
|
2005-06-25 16:44:57 +00:00
|
|
|
TileIndex tile = TileXY(xc, yc);
|
2009-09-20 17:44:33 +00:00
|
|
|
AddAcceptedCargo(tile, acceptance, always_accepted);
|
2005-03-05 18:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-27 21:06:58 +00:00
|
|
|
|
|
|
|
return acceptance;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Update the acceptance for a station.
|
2007-10-26 20:42:03 +00:00
|
|
|
* @param st Station to update
|
|
|
|
* @param show_msg controls whether to display a message that acceptance was changed.
|
|
|
|
*/
|
2009-09-07 07:39:08 +00:00
|
|
|
void UpdateStationAcceptance(Station *st, bool show_msg)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* old accepted goods types */
|
2007-02-18 11:27:09 +00:00
|
|
|
uint old_acc = GetAcceptanceMask(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* And retrieve the acceptance. */
|
2009-06-27 18:26:50 +00:00
|
|
|
CargoArray acceptance;
|
2009-04-18 11:08:10 +00:00
|
|
|
if (!st->rect.IsEmpty()) {
|
2009-06-27 21:06:58 +00:00
|
|
|
acceptance = GetAcceptanceAroundTiles(
|
2009-04-18 11:08:10 +00:00
|
|
|
TileXY(st->rect.left, st->rect.top),
|
|
|
|
st->rect.right - st->rect.left + 1,
|
|
|
|
st->rect.bottom - st->rect.top + 1,
|
2009-09-07 07:39:08 +00:00
|
|
|
st->GetCatchmentRadius(),
|
2009-09-20 17:44:33 +00:00
|
|
|
&st->always_accepted
|
2005-03-05 17:41:51 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* Adjust in case our station only accepts fewer kinds of goods */
|
2007-03-21 13:19:01 +00:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2009-06-27 18:26:50 +00:00
|
|
|
uint amt = min(acceptance[i], 15);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* Make sure the station can accept the goods type. */
|
2007-03-18 22:07:44 +00:00
|
|
|
bool is_passengers = IsCargoInClass(i, CC_PASSENGERS);
|
2009-07-26 09:25:00 +00:00
|
|
|
if ((!is_passengers && !(st->facilities & ~FACIL_BUS_STOP)) ||
|
|
|
|
(is_passengers && !(st->facilities & ~FACIL_TRUCK_STOP))) {
|
2004-08-09 17:04:08 +00:00
|
|
|
amt = 0;
|
2008-04-17 20:03:28 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-08-26 13:55:36 +00:00
|
|
|
SB(st->goods[i].acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, amt >= 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* Only show a message in case the acceptance was actually changed. */
|
2007-02-18 11:27:09 +00:00
|
|
|
uint new_acc = GetAcceptanceMask(st);
|
2008-04-17 20:03:28 +00:00
|
|
|
if (old_acc == new_acc) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* show a message to report that the acceptance was changed? */
|
2009-07-26 09:25:00 +00:00
|
|
|
if (show_msg && st->owner == _local_company && st->IsInUse()) {
|
2007-01-16 11:13:00 +00:00
|
|
|
/* List of accept and reject strings for different number of
|
|
|
|
* cargo types */
|
|
|
|
static const StringID accept_msg[] = {
|
2009-04-21 23:40:56 +00:00
|
|
|
STR_NEWS_STATION_NOW_ACCEPTS_CARGO,
|
|
|
|
STR_NEWS_STATION_NOW_ACCEPTS_CARGO_AND_CARGO,
|
2007-01-16 11:13:00 +00:00
|
|
|
};
|
|
|
|
static const StringID reject_msg[] = {
|
2009-04-21 23:40:56 +00:00
|
|
|
STR_NEWS_STATION_NO_LONGER_ACCEPTS_CARGO,
|
|
|
|
STR_NEWS_STATION_NO_LONGER_ACCEPTS_CARGO_OR_CARGO,
|
2007-01-16 11:13:00 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-16 11:13:00 +00:00
|
|
|
/* Array of accepted and rejected cargo types */
|
|
|
|
CargoID accepts[2] = { CT_INVALID, CT_INVALID };
|
|
|
|
CargoID rejects[2] = { CT_INVALID, CT_INVALID };
|
|
|
|
uint num_acc = 0;
|
|
|
|
uint num_rej = 0;
|
|
|
|
|
|
|
|
/* Test each cargo type to see if its acceptange has changed */
|
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(new_acc, i)) {
|
|
|
|
if (!HasBit(old_acc, i) && num_acc < lengthof(accepts)) {
|
2007-01-16 11:13:00 +00:00
|
|
|
/* New cargo is accepted */
|
|
|
|
accepts[num_acc++] = i;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(old_acc, i) && num_rej < lengthof(rejects)) {
|
2007-01-16 11:13:00 +00:00
|
|
|
/* Old cargo is no longer accepted */
|
|
|
|
rejects[num_rej++] = i;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-01-16 11:13:00 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-16 11:13:00 +00:00
|
|
|
/* Show news message if there are any changes */
|
|
|
|
if (num_acc > 0) ShowRejectOrAcceptNews(st, num_acc, accepts, accept_msg[num_acc - 1]);
|
|
|
|
if (num_rej > 0) ShowRejectOrAcceptNews(st, num_rej, rejects, reject_msg[num_rej - 1]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* redraw the station view since acceptance changed */
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_ACCEPTLIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 22:09:05 +00:00
|
|
|
static void UpdateStationSignCoord(BaseStation *st)
|
2006-11-16 16:18:00 +00:00
|
|
|
{
|
2007-02-18 11:27:09 +00:00
|
|
|
const StationRect *r = &st->rect;
|
2006-11-16 16:18:00 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
if (r->IsEmpty()) return; // no tiles belong to this station
|
2006-11-16 16:18:00 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* clamp sign coord to be inside the station rect */
|
2007-11-19 18:38:10 +00:00
|
|
|
st->xy = TileXY(ClampU(TileX(st->xy), r->left, r->right), ClampU(TileY(st->xy), r->top, r->bottom));
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2006-11-16 16:18:00 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* This is called right after a station was deleted.
|
2007-10-26 20:42:03 +00:00
|
|
|
* It checks if the whole station is free of substations, and if so, the station will be
|
|
|
|
* deleted after a little while.
|
|
|
|
* @param st Station
|
|
|
|
*/
|
2009-07-29 22:09:05 +00:00
|
|
|
static void DeleteStationIfEmpty(BaseStation *st)
|
2005-11-14 19:48:04 +00:00
|
|
|
{
|
2009-07-25 10:39:58 +00:00
|
|
|
if (!st->IsInUse()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
st->delete_ctr = 0;
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->owner, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-11-16 16:18:00 +00:00
|
|
|
/* station remains but it probably lost some parts - station sign should stay in the station boundaries */
|
|
|
|
UpdateStationSignCoord(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 22:06:54 +00:00
|
|
|
CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags);
|
2005-01-10 21:52:35 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given tile is buildable, flat and has a certain height.
|
2010-02-20 17:39:51 +00:00
|
|
|
* @param tile TileIndex to check.
|
|
|
|
* @param invalid_dirs Prohibited directions for slopes (set of #DiagDirection).
|
|
|
|
* @param allowed_z Height allowed for the tile. If allowed_z is negative, it will be set to the height of this tile.
|
2010-08-28 19:02:21 +00:00
|
|
|
* @param check_bridge Check for the existance of a bridge.
|
2010-02-20 17:39:51 +00:00
|
|
|
* @return The cost in case of success, or an error code if it failed.
|
|
|
|
*/
|
2010-08-28 19:02:21 +00:00
|
|
|
CommandCost CheckBuildableTile(TileIndex tile, uint invalid_dirs, int &allowed_z, bool check_bridge = true)
|
2010-02-20 17:39:51 +00:00
|
|
|
{
|
2010-08-28 19:02:21 +00:00
|
|
|
if (check_bridge && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) {
|
2010-02-20 17:39:51 +00:00
|
|
|
return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
|
|
|
}
|
|
|
|
|
2010-03-05 21:20:22 +00:00
|
|
|
CommandCost ret = EnsureNoVehicleOnGround(tile);
|
|
|
|
if (ret.Failed()) return ret;
|
2010-02-20 17:39:51 +00:00
|
|
|
|
|
|
|
uint z;
|
|
|
|
Slope tileh = GetTileSlope(tile, &z);
|
|
|
|
|
|
|
|
/* Prohibit building if
|
|
|
|
* 1) The tile is "steep" (i.e. stretches two height levels).
|
|
|
|
* 2) The tile is non-flat and the build_on_slopes switch is disabled.
|
|
|
|
*/
|
|
|
|
if (IsSteepSlope(tileh) ||
|
|
|
|
((!_settings_game.construction.build_on_slopes) && tileh != SLOPE_FLAT)) {
|
|
|
|
return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
|
|
|
int flat_z = z;
|
|
|
|
if (tileh != SLOPE_FLAT) {
|
|
|
|
/* Forbid building if the tile faces a slope in a invalid direction. */
|
|
|
|
if ((HasBit(invalid_dirs, DIAGDIR_NE) && !(tileh & SLOPE_NE)) ||
|
|
|
|
(HasBit(invalid_dirs, DIAGDIR_SE) && !(tileh & SLOPE_SE)) ||
|
|
|
|
(HasBit(invalid_dirs, DIAGDIR_SW) && !(tileh & SLOPE_SW)) ||
|
|
|
|
(HasBit(invalid_dirs, DIAGDIR_NW) && !(tileh & SLOPE_NW))) {
|
|
|
|
return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
|
|
|
|
}
|
|
|
|
cost.AddCost(_price[PR_BUILD_FOUNDATION]);
|
|
|
|
flat_z += TILE_HEIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The level of this tile must be equal to allowed_z. */
|
|
|
|
if (allowed_z < 0) {
|
|
|
|
/* First tile. */
|
|
|
|
allowed_z = flat_z;
|
|
|
|
} else if (allowed_z != flat_z) {
|
|
|
|
return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Tries to clear the given area.
|
2010-02-20 18:12:56 +00:00
|
|
|
* @param tile_area Area to check.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @return The cost in case of success, or an error code if it failed.
|
|
|
|
*/
|
|
|
|
CommandCost CheckFlatLand(TileArea tile_area, DoCommandFlag flags)
|
|
|
|
{
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
|
|
|
int allowed_z = -1;
|
|
|
|
|
|
|
|
TILE_AREA_LOOP(tile_cur, tile_area) {
|
|
|
|
CommandCost ret = CheckBuildableTile(tile_cur, 0, allowed_z);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
|
|
|
|
|
|
|
ret = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Checks if a rail station can be built at the given area.
|
2010-02-20 17:56:05 +00:00
|
|
|
* @param tile_area Area to check.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param invalid_dirs Prohibited directions (set of #DiagDirection).
|
|
|
|
* @param station StationID to be queried and returned if available.
|
|
|
|
* @param rt The rail type to check for (overbuilding rail stations over rail).
|
2010-03-10 20:45:23 +00:00
|
|
|
* @param affected_vehicles List of trains with PBS reservations on the tiles
|
2010-02-20 17:56:05 +00:00
|
|
|
* @return The cost in case of success, or an error code if it failed.
|
2007-10-26 20:42:03 +00:00
|
|
|
*/
|
2010-03-10 20:45:23 +00:00
|
|
|
static CommandCost CheckFlatLandRailStation(TileArea tile_area, DoCommandFlag flags, uint invalid_dirs, StationID *station, RailType rt, SmallVector<Train *, 4> &affected_vehicles)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
2005-02-07 10:41:45 +00:00
|
|
|
int allowed_z = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-20 17:56:05 +00:00
|
|
|
TILE_AREA_LOOP(tile_cur, tile_area) {
|
2010-02-20 17:39:51 +00:00
|
|
|
CommandCost ret = CheckBuildableTile(tile_cur, invalid_dirs, allowed_z);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* if station is set, then we have special handling to allow building on top of already existing stations.
|
|
|
|
* so station points to INVALID_STATION if we can build on any station.
|
|
|
|
* Or it points to a station if we're only allowed to build on exactly that station. */
|
2005-01-16 11:24:58 +00:00
|
|
|
if (station != NULL && IsTileType(tile_cur, MP_STATION)) {
|
2009-07-24 11:47:12 +00:00
|
|
|
if (!IsRailStation(tile_cur)) {
|
2006-03-12 12:19:25 +00:00
|
|
|
return ClearTile_Station(tile_cur, DC_AUTO); // get error message
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2006-03-24 08:55:08 +00:00
|
|
|
StationID st = GetStationIndex(tile_cur);
|
2005-10-07 07:35:15 +00:00
|
|
|
if (*station == INVALID_STATION) {
|
2004-08-09 17:04:08 +00:00
|
|
|
*station = st;
|
2005-10-07 07:35:15 +00:00
|
|
|
} else if (*station != st) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_ADJOINS_MORE_THAN_ONE_EXISTING);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-20 18:12:56 +00:00
|
|
|
} else {
|
2010-04-12 14:12:47 +00:00
|
|
|
/* Rail type is only valid when building a railway station; if station to
|
2009-09-07 18:35:38 +00:00
|
|
|
* build isn't a rail station it's INVALID_RAILTYPE. */
|
|
|
|
if (rt != INVALID_RAILTYPE &&
|
|
|
|
IsPlainRailTile(tile_cur) && !HasSignals(tile_cur) &&
|
2009-09-08 07:16:26 +00:00
|
|
|
HasPowerOnRail(GetRailType(tile_cur), rt)) {
|
2009-09-07 18:35:38 +00:00
|
|
|
/* Allow overbuilding if the tile:
|
|
|
|
* - has rail, but no signals
|
|
|
|
* - it has exactly one track
|
|
|
|
* - the track is in line with the station
|
|
|
|
* - the current rail type has power on the to-be-built type (e.g. convert normal rail to el rail)
|
|
|
|
*/
|
|
|
|
TrackBits tracks = GetTrackBits(tile_cur);
|
|
|
|
Track track = RemoveFirstTrack(&tracks);
|
|
|
|
Track expected_track = HasBit(invalid_dirs, DIAGDIR_NE) ? TRACK_X : TRACK_Y;
|
|
|
|
|
|
|
|
if (tracks == TRACK_BIT_NONE && track == expected_track) {
|
2010-03-10 20:45:23 +00:00
|
|
|
/* Check for trains having a reservation for this tile. */
|
|
|
|
if (HasBit(GetRailReservationTrackBits(tile_cur), track)) {
|
|
|
|
Train *v = GetTrainForReservation(tile_cur, track);
|
|
|
|
if (v != NULL) {
|
|
|
|
*affected_vehicles.Append() = v;
|
|
|
|
}
|
|
|
|
}
|
2009-09-07 18:35:38 +00:00
|
|
|
CommandCost ret = DoCommand(tile_cur, 0, track, flags, CMD_REMOVE_SINGLE_RAIL);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2009-09-07 18:35:38 +00:00
|
|
|
cost.AddCost(ret);
|
|
|
|
/* With flags & ~DC_EXEC CmdLandscapeClear would fail since the rail still exists */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-02-20 17:39:51 +00:00
|
|
|
ret = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2007-06-18 19:53:50 +00:00
|
|
|
cost.AddCost(ret);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Checks if a road stop can be built at the given tile.
|
2010-02-24 21:51:59 +00:00
|
|
|
* @param tile_area Area to check.
|
2010-02-20 18:12:56 +00:00
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param invalid_dirs Prohibited directions (set of DiagDirections).
|
2010-02-24 21:51:59 +00:00
|
|
|
* @param is_drive_through True if trying to build a drive-through station.
|
2010-02-24 21:55:03 +00:00
|
|
|
* @param is_truck_stop True when building a truck stop, false otherwise.
|
2010-02-24 21:47:06 +00:00
|
|
|
* @param axis Axis of a drive-through road stop.
|
2010-02-24 21:55:03 +00:00
|
|
|
* @param station StationID to be queried and returned if available.
|
2011-02-01 10:48:10 +00:00
|
|
|
* @param rts Road types to build.
|
2010-02-20 18:12:56 +00:00
|
|
|
* @return The cost in case of success, or an error code if it failed.
|
|
|
|
*/
|
2011-02-01 10:48:10 +00:00
|
|
|
static CommandCost CheckFlatLandRoadStop(TileArea tile_area, DoCommandFlag flags, uint invalid_dirs, bool is_drive_through, bool is_truck_stop, Axis axis, StationID *station, RoadTypes rts)
|
2010-02-20 18:12:56 +00:00
|
|
|
{
|
2010-02-24 21:51:59 +00:00
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
2010-02-20 18:12:56 +00:00
|
|
|
int allowed_z = -1;
|
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
TILE_AREA_LOOP(cur_tile, tile_area) {
|
|
|
|
CommandCost ret = CheckBuildableTile(cur_tile, invalid_dirs, allowed_z);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
2010-02-20 18:12:56 +00:00
|
|
|
|
2010-02-24 21:55:03 +00:00
|
|
|
/* If station is set, then we have special handling to allow building on top of already existing stations.
|
|
|
|
* Station points to INVALID_STATION if we can build on any station.
|
|
|
|
* Or it points to a station if we're only allowed to build on exactly that station. */
|
|
|
|
if (station != NULL && IsTileType(cur_tile, MP_STATION)) {
|
|
|
|
if (!IsRoadStop(cur_tile)) {
|
|
|
|
return ClearTile_Station(cur_tile, DC_AUTO); // Get error message.
|
|
|
|
} else {
|
|
|
|
if (is_truck_stop != IsTruckStop(cur_tile) ||
|
2011-02-01 10:43:25 +00:00
|
|
|
is_drive_through != IsDriveThroughStopTile(cur_tile)) {
|
2010-02-24 21:55:03 +00:00
|
|
|
return ClearTile_Station(cur_tile, DC_AUTO); // Get error message.
|
|
|
|
}
|
2010-02-24 22:40:43 +00:00
|
|
|
/* Drive-through station in the wrong direction. */
|
|
|
|
if (is_drive_through && IsDriveThroughStopTile(cur_tile) && DiagDirToAxis(GetRoadStopDir(cur_tile)) != axis){
|
|
|
|
return_cmd_error(STR_ERROR_DRIVE_THROUGH_DIRECTION);
|
|
|
|
}
|
2010-02-24 21:55:03 +00:00
|
|
|
StationID st = GetStationIndex(cur_tile);
|
|
|
|
if (*station == INVALID_STATION) {
|
|
|
|
*station = st;
|
|
|
|
} else if (*station != st) {
|
|
|
|
return_cmd_error(STR_ERROR_ADJOINS_MORE_THAN_ONE_EXISTING);
|
2010-02-24 21:51:59 +00:00
|
|
|
}
|
2010-02-20 18:28:21 +00:00
|
|
|
}
|
2010-02-24 21:55:03 +00:00
|
|
|
} else {
|
|
|
|
bool build_over_road = is_drive_through && IsNormalRoadTile(cur_tile);
|
|
|
|
/* Road bits in the wrong direction. */
|
2010-08-04 06:27:20 +00:00
|
|
|
RoadBits rb = IsNormalRoadTile(cur_tile) ? GetAllRoadBits(cur_tile) : ROAD_NONE;
|
2010-08-03 18:32:53 +00:00
|
|
|
if (build_over_road && (rb & (axis == AXIS_X ? ROAD_Y : ROAD_X)) != 0) {
|
|
|
|
/* Someone was pedantic and *NEEDED* three fracking different error messages. */
|
|
|
|
switch (CountBits(rb)) {
|
|
|
|
case 1:
|
|
|
|
return_cmd_error(STR_ERROR_DRIVE_THROUGH_DIRECTION);
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (rb == ROAD_X || rb == ROAD_Y) return_cmd_error(STR_ERROR_DRIVE_THROUGH_DIRECTION);
|
|
|
|
return_cmd_error(STR_ERROR_DRIVE_THROUGH_CORNER);
|
|
|
|
|
|
|
|
default: // 3 or 4
|
|
|
|
return_cmd_error(STR_ERROR_DRIVE_THROUGH_JUNCTION);
|
|
|
|
}
|
|
|
|
}
|
2010-02-24 21:55:03 +00:00
|
|
|
|
|
|
|
RoadTypes cur_rts = IsNormalRoadTile(cur_tile) ? GetRoadTypes(cur_tile) : ROADTYPES_NONE;
|
|
|
|
uint num_roadbits = 0;
|
|
|
|
if (build_over_road) {
|
|
|
|
/* There is a road, check if we can build road+tram stop over it. */
|
|
|
|
if (HasBit(cur_rts, ROADTYPE_ROAD)) {
|
|
|
|
Owner road_owner = GetRoadOwner(cur_tile, ROADTYPE_ROAD);
|
|
|
|
if (road_owner == OWNER_TOWN) {
|
|
|
|
if (!_settings_game.construction.road_stop_on_town_road) return_cmd_error(STR_ERROR_DRIVE_THROUGH_ON_TOWN_ROAD);
|
2010-03-13 17:11:28 +00:00
|
|
|
} else if (!_settings_game.construction.road_stop_on_competitor_road && road_owner != OWNER_NONE) {
|
|
|
|
CommandCost ret = CheckOwnership(road_owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2010-02-24 21:55:03 +00:00
|
|
|
}
|
|
|
|
num_roadbits += CountBits(GetRoadBits(cur_tile, ROADTYPE_ROAD));
|
|
|
|
}
|
2010-02-20 18:28:21 +00:00
|
|
|
|
2010-02-24 21:55:03 +00:00
|
|
|
/* There is a tram, check if we can build road+tram stop over it. */
|
|
|
|
if (HasBit(cur_rts, ROADTYPE_TRAM)) {
|
|
|
|
Owner tram_owner = GetRoadOwner(cur_tile, ROADTYPE_TRAM);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (!_settings_game.construction.road_stop_on_competitor_road && tram_owner != OWNER_NONE) {
|
|
|
|
CommandCost ret = CheckOwnership(tram_owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2010-02-24 21:55:03 +00:00
|
|
|
}
|
|
|
|
num_roadbits += CountBits(GetRoadBits(cur_tile, ROADTYPE_TRAM));
|
2010-02-24 21:51:59 +00:00
|
|
|
}
|
2010-02-24 21:55:03 +00:00
|
|
|
|
2011-02-01 10:48:10 +00:00
|
|
|
/* Take into account existing roadbits. */
|
2010-02-24 21:55:03 +00:00
|
|
|
rts |= cur_rts;
|
|
|
|
} else {
|
|
|
|
ret = DoCommand(cur_tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
2010-02-20 18:28:21 +00:00
|
|
|
}
|
2010-02-24 21:51:59 +00:00
|
|
|
|
2010-02-24 21:55:03 +00:00
|
|
|
uint roadbits_to_build = CountBits(rts) * 2 - num_roadbits;
|
|
|
|
cost.AddCost(_price[PR_BUILD_ROAD] * roadbits_to_build);
|
2010-02-20 18:28:21 +00:00
|
|
|
}
|
2010-02-20 18:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2009-07-24 17:05:17 +00:00
|
|
|
/**
|
|
|
|
* Check whether we can expand the rail part of the given station.
|
|
|
|
* @param st the station to expand
|
2009-07-28 13:35:50 +00:00
|
|
|
* @param new_ta the current (and if all is fine new) tile area of the rail part of the station
|
2009-07-24 17:05:17 +00:00
|
|
|
* @param axis the axis of the newly build rail
|
2010-02-27 14:17:33 +00:00
|
|
|
* @return Succeeded or failed command.
|
2009-07-24 17:05:17 +00:00
|
|
|
*/
|
2010-02-27 14:17:33 +00:00
|
|
|
CommandCost CanExpandRailStation(const BaseStation *st, TileArea &new_ta, Axis axis)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-28 13:35:50 +00:00
|
|
|
TileArea cur_ta = st->train_station;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.station.nonuniform_stations) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* determine new size of train station region.. */
|
2009-07-28 13:35:50 +00:00
|
|
|
int x = min(TileX(cur_ta.tile), TileX(new_ta.tile));
|
|
|
|
int y = min(TileY(cur_ta.tile), TileY(new_ta.tile));
|
|
|
|
new_ta.w = max(TileX(cur_ta.tile) + cur_ta.w, TileX(new_ta.tile) + new_ta.w) - x;
|
|
|
|
new_ta.h = max(TileY(cur_ta.tile) + cur_ta.h, TileY(new_ta.tile) + new_ta.h) - y;
|
2009-07-24 17:05:17 +00:00
|
|
|
new_ta.tile = TileXY(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2007-12-18 18:02:00 +00:00
|
|
|
/* do not allow modifying non-uniform stations,
|
|
|
|
* the uniform-stations code wouldn't handle it well */
|
2010-12-12 18:23:38 +00:00
|
|
|
TILE_AREA_LOOP(t, cur_ta) {
|
2007-12-18 18:02:00 +00:00
|
|
|
if (!st->TileBelongsToRailStation(t)) { // there may be adjoined station
|
2010-02-27 14:17:33 +00:00
|
|
|
return_cmd_error(STR_ERROR_NONUNIFORM_STATIONS_DISALLOWED);
|
2007-12-18 18:02:00 +00:00
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2007-12-18 18:02:00 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check so the orientation is the same */
|
2009-07-28 13:35:50 +00:00
|
|
|
if (GetRailStationAxis(cur_ta.tile) != axis) {
|
2010-02-27 14:17:33 +00:00
|
|
|
return_cmd_error(STR_ERROR_NONUNIFORM_STATIONS_DISALLOWED);
|
2005-01-27 09:43:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check if the new station adjoins the old station in either direction */
|
2009-07-28 13:35:50 +00:00
|
|
|
if (cur_ta.w == new_ta.w && cur_ta.tile == new_ta.tile + TileDiffXY(0, new_ta.h)) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* above */
|
2009-07-24 17:05:17 +00:00
|
|
|
new_ta.h += cur_ta.h;
|
2009-07-28 13:35:50 +00:00
|
|
|
} else if (cur_ta.w == new_ta.w && cur_ta.tile == new_ta.tile - TileDiffXY(0, cur_ta.h)) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* below */
|
2009-07-28 13:35:50 +00:00
|
|
|
new_ta.tile = cur_ta.tile;
|
|
|
|
new_ta.h += new_ta.h;
|
|
|
|
} else if (cur_ta.h == new_ta.h && cur_ta.tile == new_ta.tile + TileDiffXY(new_ta.w, 0)) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* to the left */
|
2009-07-24 17:05:17 +00:00
|
|
|
new_ta.w += cur_ta.w;
|
2009-07-28 13:35:50 +00:00
|
|
|
} else if (cur_ta.h == new_ta.h && cur_ta.tile == new_ta.tile - TileDiffXY(cur_ta.w, 0)) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* to the right */
|
2009-07-28 13:35:50 +00:00
|
|
|
new_ta.tile = cur_ta.tile;
|
2009-07-24 17:05:17 +00:00
|
|
|
new_ta.w += cur_ta.w;
|
2005-01-27 09:43:24 +00:00
|
|
|
} else {
|
2010-02-27 14:17:33 +00:00
|
|
|
return_cmd_error(STR_ERROR_NONUNIFORM_STATIONS_DISALLOWED);
|
2005-01-27 09:43:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-10-26 20:42:03 +00:00
|
|
|
/* make sure the final size is not too big. */
|
2009-07-24 17:05:17 +00:00
|
|
|
if (new_ta.w > _settings_game.station.station_spread || new_ta.h > _settings_game.station.station_spread) {
|
2010-02-27 14:17:33 +00:00
|
|
|
return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT);
|
2005-01-27 09:43:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-27 14:17:33 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline byte *CreateSingle(byte *layout, int n)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i = n;
|
|
|
|
do *layout++ = 0; while (--i);
|
2007-04-18 22:10:36 +00:00
|
|
|
layout[((n - 1) >> 1) - n] = 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2004-12-03 07:43:00 +00:00
|
|
|
static inline byte *CreateMulti(byte *layout, int n, byte b)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i = n;
|
|
|
|
do *layout++ = b; while (--i);
|
|
|
|
if (n > 4) {
|
2007-04-18 22:10:36 +00:00
|
|
|
layout[0 - n] = 0;
|
|
|
|
layout[n - 1 - n] = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
void GetStationLayout(byte *layout, int numtracks, int plat_len, const StationSpec *statspec)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-27 18:28:56 +00:00
|
|
|
if (statspec != NULL && statspec->lengths >= plat_len &&
|
|
|
|
statspec->platforms[plat_len - 1] >= numtracks &&
|
|
|
|
statspec->layouts[plat_len - 1][numtracks - 1]) {
|
2004-11-17 18:03:33 +00:00
|
|
|
/* Custom layout defined, follow it. */
|
2006-04-27 18:28:56 +00:00
|
|
|
memcpy(layout, statspec->layouts[plat_len - 1][numtracks - 1],
|
2005-03-09 19:09:04 +00:00
|
|
|
plat_len * numtracks);
|
2004-11-17 18:03:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (plat_len == 1) {
|
|
|
|
CreateSingle(layout, numtracks);
|
|
|
|
} else {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (numtracks & 1) layout = CreateSingle(layout, plat_len);
|
|
|
|
numtracks >>= 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
while (--numtracks >= 0) {
|
|
|
|
layout = CreateMulti(layout, plat_len, 4);
|
|
|
|
layout = CreateMulti(layout, plat_len, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 21:41:21 +00:00
|
|
|
/**
|
|
|
|
* Find a nearby station that joins this station.
|
2009-07-31 16:28:28 +00:00
|
|
|
* @tparam T the class to find a station for
|
2009-09-19 15:17:47 +00:00
|
|
|
* @tparam error_message the error message when building a station on top of others
|
2009-07-30 21:41:21 +00:00
|
|
|
* @param existing_station an existing station we build over
|
|
|
|
* @param station_to_join the station to join to
|
|
|
|
* @param adjacent whether adjacent stations are allowed
|
|
|
|
* @param ta the area of the newly build station
|
|
|
|
* @param st 'return' pointer for the found station
|
|
|
|
* @return command cost with the error or 'okay'
|
|
|
|
*/
|
|
|
|
template <class T, StringID error_message>
|
|
|
|
CommandCost FindJoiningBaseStation(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, T **st)
|
|
|
|
{
|
|
|
|
assert(*st == NULL);
|
|
|
|
bool check_surrounding = true;
|
|
|
|
|
|
|
|
if (_settings_game.station.adjacent_stations) {
|
|
|
|
if (existing_station != INVALID_STATION) {
|
|
|
|
if (adjacent && existing_station != station_to_join) {
|
|
|
|
/* You can't build an adjacent station over the top of one that
|
|
|
|
* already exists. */
|
|
|
|
return_cmd_error(error_message);
|
|
|
|
} else {
|
|
|
|
/* Extend the current station, and don't check whether it will
|
|
|
|
* be near any other stations. */
|
|
|
|
*st = T::GetIfValid(existing_station);
|
|
|
|
check_surrounding = (*st == NULL);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* There's no station here. Don't check the tiles surrounding this
|
|
|
|
* one if the company wanted to build an adjacent station. */
|
|
|
|
if (adjacent) check_surrounding = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_surrounding) {
|
|
|
|
/* Make sure there are no similar stations around us. */
|
2010-02-27 13:51:11 +00:00
|
|
|
CommandCost ret = GetStationAround(ta, existing_station, st);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-07-30 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 16:28:28 +00:00
|
|
|
/* Distant join */
|
|
|
|
if (*st == NULL && station_to_join != INVALID_STATION) *st = T::GetIfValid(station_to_join);
|
|
|
|
|
2010-02-14 16:31:35 +00:00
|
|
|
return CommandCost();
|
2009-07-30 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a nearby station that joins this station.
|
|
|
|
* @param existing_station an existing station we build over
|
|
|
|
* @param station_to_join the station to join to
|
|
|
|
* @param adjacent whether adjacent stations are allowed
|
|
|
|
* @param ta the area of the newly build station
|
|
|
|
* @param st 'return' pointer for the found station
|
|
|
|
* @return command cost with the error or 'okay'
|
|
|
|
*/
|
2009-11-09 10:40:33 +00:00
|
|
|
static CommandCost FindJoiningStation(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Station **st)
|
2009-07-30 21:41:21 +00:00
|
|
|
{
|
2009-08-05 17:59:21 +00:00
|
|
|
return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_RAILWAY_STATION_FIRST>(existing_station, station_to_join, adjacent, ta, st);
|
2009-07-30 21:41:21 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 22:06:54 +00:00
|
|
|
/**
|
|
|
|
* Find a nearby waypoint that joins this waypoint.
|
|
|
|
* @param existing_waypoint an existing waypoint we build over
|
|
|
|
* @param waypoint_to_join the waypoint to join to
|
|
|
|
* @param adjacent whether adjacent waypoints are allowed
|
|
|
|
* @param ta the area of the newly build waypoint
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param wp 'return' pointer for the found waypoint
|
2009-07-30 22:06:54 +00:00
|
|
|
* @return command cost with the error or 'okay'
|
|
|
|
*/
|
|
|
|
CommandCost FindJoiningWaypoint(StationID existing_waypoint, StationID waypoint_to_join, bool adjacent, TileArea ta, Waypoint **wp)
|
|
|
|
{
|
2009-08-05 17:59:21 +00:00
|
|
|
return FindJoiningBaseStation<Waypoint, STR_ERROR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
|
2009-07-30 22:06:54 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 18:52:16 +00:00
|
|
|
/**
|
2009-07-29 22:32:20 +00:00
|
|
|
* Build rail station
|
2009-07-28 18:52:16 +00:00
|
|
|
* @param tile_org northern most position of station dragging/placement
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-07 10:26:12 +00:00
|
|
|
* @param p1 various bitstuffed elements
|
2009-07-28 18:52:16 +00:00
|
|
|
* - p1 = (bit 0- 3) - railtype
|
2009-01-08 16:35:45 +00:00
|
|
|
* - p1 = (bit 4) - orientation (Axis)
|
2005-07-20 15:29:28 +00:00
|
|
|
* - p1 = (bit 8-15) - number of tracks
|
|
|
|
* - p1 = (bit 16-23) - platform length
|
2007-05-23 17:33:03 +00:00
|
|
|
* - p1 = (bit 24) - allow stations directly adjacent to other stations.
|
2005-05-07 10:26:12 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
2009-01-08 16:35:45 +00:00
|
|
|
* - p2 = (bit 0- 7) - custom station class
|
|
|
|
* - p2 = (bit 8-15) - custom station id
|
2009-07-28 18:52:16 +00:00
|
|
|
* - p2 = (bit 16-31) - station ID to join (NEW_STATION if build new one)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-07-29 22:32:20 +00:00
|
|
|
CommandCost CmdBuildRailStation(TileIndex tile_org, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-28 18:52:16 +00:00
|
|
|
/* Unpack parameters */
|
2010-04-17 13:31:41 +00:00
|
|
|
RailType rt = Extract<RailType, 0, 4>(p1);
|
2010-04-13 17:29:19 +00:00
|
|
|
Axis axis = Extract<Axis, 4, 1>(p1);
|
2009-07-28 18:52:16 +00:00
|
|
|
byte numtracks = GB(p1, 8, 8);
|
|
|
|
byte plat_len = GB(p1, 16, 8);
|
|
|
|
bool adjacent = HasBit(p1, 24);
|
|
|
|
|
2010-04-17 13:31:41 +00:00
|
|
|
StationClassID spec_class = Extract<StationClassID, 0, 8>(p2);
|
2009-07-28 18:52:16 +00:00
|
|
|
byte spec_index = GB(p2, 8, 8);
|
|
|
|
StationID station_to_join = GB(p2, 16, 16);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Does the authority allow this? */
|
2010-02-27 17:42:55 +00:00
|
|
|
CommandCost ret = CheckIfAuthorityAllowsNewStation(tile_org, flags);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
2009-07-28 18:52:16 +00:00
|
|
|
if (!ValParamRailtype(rt)) return CMD_ERROR;
|
2005-05-07 10:26:12 +00:00
|
|
|
|
2009-07-28 18:52:16 +00:00
|
|
|
/* Check if the given station class is valid */
|
2010-08-07 22:07:07 +00:00
|
|
|
if ((uint)spec_class >= StationClass::GetCount() || spec_class == STAT_CLASS_WAYP) return CMD_ERROR;
|
|
|
|
if (spec_index >= StationClass::GetCount(spec_class)) return CMD_ERROR;
|
2009-07-29 22:09:05 +00:00
|
|
|
if (plat_len == 0 || numtracks == 0) return CMD_ERROR;
|
2008-04-17 20:03:28 +00:00
|
|
|
|
|
|
|
int w_org, h_org;
|
2006-03-08 06:55:33 +00:00
|
|
|
if (axis == AXIS_X) {
|
2005-05-07 10:26:12 +00:00
|
|
|
w_org = plat_len;
|
|
|
|
h_org = numtracks;
|
2006-03-08 06:55:33 +00:00
|
|
|
} else {
|
|
|
|
h_org = plat_len;
|
|
|
|
w_org = numtracks;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-04-10 22:47:19 +00:00
|
|
|
bool reuse = (station_to_join != NEW_STATION);
|
|
|
|
if (!reuse) station_to_join = INVALID_STATION;
|
2009-01-08 16:35:45 +00:00
|
|
|
bool distant_join = (station_to_join != INVALID_STATION);
|
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (h_org > _settings_game.station.station_spread || w_org > _settings_game.station.station_spread) return CMD_ERROR;
|
2005-05-09 13:26:15 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* these values are those that will be stored in train_tile and station_platforms */
|
2009-07-25 08:54:19 +00:00
|
|
|
TileArea new_location(tile_org, w_org, h_org);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */
|
2007-02-18 11:27:09 +00:00
|
|
|
StationID est = INVALID_STATION;
|
2010-03-10 20:45:23 +00:00
|
|
|
SmallVector<Train *, 4> affected_vehicles;
|
2010-02-04 21:09:29 +00:00
|
|
|
/* Clear the land below the station. */
|
2010-03-10 20:45:23 +00:00
|
|
|
CommandCost cost = CheckFlatLandRailStation(TileArea(tile_org, w_org, h_org), flags, 5 << axis, _settings_game.station.nonuniform_stations ? &est : NULL, rt, affected_vehicles);
|
2010-02-04 21:09:29 +00:00
|
|
|
if (cost.Failed()) return cost;
|
|
|
|
/* Add construction expenses. */
|
|
|
|
cost.AddCost((numtracks * _price[PR_BUILD_STATION_RAIL] + _price[PR_BUILD_STATION_RAIL_LENGTH]) * plat_len);
|
2010-06-04 21:00:17 +00:00
|
|
|
cost.AddCost(numtracks * plat_len * RailBuildCost(rt));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-23 17:33:03 +00:00
|
|
|
Station *st = NULL;
|
2010-02-27 17:42:55 +00:00
|
|
|
ret = FindJoiningStation(est, station_to_join, adjacent, new_location, &st);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* See if there is a deleted station close to us. */
|
2009-04-10 22:47:19 +00:00
|
|
|
if (st == NULL && reuse) st = GetClosestDeletedStation(tile_org);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (st != NULL) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* Reuse an existing station. */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (st->owner != _current_company) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_STATION);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-25 08:54:19 +00:00
|
|
|
if (st->train_station.tile != INVALID_TILE) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check if we want to expanding an already existing station? */
|
2010-02-27 14:17:33 +00:00
|
|
|
if (!_settings_game.station.join_stations) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_RAILROAD);
|
|
|
|
|
|
|
|
CommandCost ret = CanExpandRailStation(st, new_location, axis);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* XXX can't we pack this in the "else" part of the if above? */
|
2010-03-06 13:23:33 +00:00
|
|
|
CommandCost ret = st->rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TEST);
|
|
|
|
if (ret.Failed()) return ret;
|
2006-08-28 18:53:03 +00:00
|
|
|
} else {
|
2007-01-14 19:18:50 +00:00
|
|
|
/* allocate and initialize new station */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!Station::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-04-23 22:16:41 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-04-28 23:34:23 +00:00
|
|
|
st = new Station(tile_org);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-24 18:30:41 +00:00
|
|
|
st->town = ClosestTownFromTile(tile_org, UINT_MAX);
|
2008-04-25 15:22:32 +00:00
|
|
|
st->string_id = GenerateStationName(st, tile_org, STATIONNAMING_RAIL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(_current_company)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(st->town->have_ratings, _current_company);
|
2008-04-23 22:16:41 +00:00
|
|
|
}
|
2007-01-14 19:18:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 07:17:00 +00:00
|
|
|
/* Check if we can allocate a custom stationspec to this station */
|
2010-08-07 22:07:07 +00:00
|
|
|
const StationSpec *statspec = StationClass::Get(spec_class, spec_index);
|
2009-02-10 04:23:37 +00:00
|
|
|
int specindex = AllocateSpecToStation(statspec, st, (flags & DC_EXEC) != 0);
|
2009-04-21 23:40:56 +00:00
|
|
|
if (specindex == -1) return_cmd_error(STR_ERROR_TOO_MANY_STATION_SPECS);
|
2006-04-19 07:17:00 +00:00
|
|
|
|
2006-05-04 20:00:50 +00:00
|
|
|
if (statspec != NULL) {
|
|
|
|
/* Perform NewStation checks */
|
|
|
|
|
|
|
|
/* Check if the station size is permitted */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(statspec->disallowed_platforms, numtracks - 1) || HasBit(statspec->disallowed_lengths, plat_len - 1)) {
|
2006-05-04 20:00:50 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the station is buildable */
|
2009-09-14 12:22:57 +00:00
|
|
|
if (HasBit(statspec->callback_mask, CBM_STATION_AVAIL) && GB(GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE), 0, 8) == 0) {
|
2006-05-04 20:00:50 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndexDiff tile_delta;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte *layout_ptr;
|
2006-05-06 22:30:36 +00:00
|
|
|
byte numtracks_orig;
|
2006-01-06 17:45:43 +00:00
|
|
|
Track track;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-25 08:54:19 +00:00
|
|
|
st->train_station = new_location;
|
2009-07-24 17:05:17 +00:00
|
|
|
st->AddFacility(FACIL_TRAIN, new_location.tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
st->rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TRY);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2008-04-19 23:19:12 +00:00
|
|
|
if (statspec != NULL) {
|
|
|
|
/* Include this station spec's animation trigger bitmask
|
|
|
|
* in the station's cached copy. */
|
2010-08-26 15:31:40 +00:00
|
|
|
st->cached_anim_triggers |= statspec->animation.triggers;
|
2008-04-19 23:19:12 +00:00
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
|
2006-07-22 08:59:52 +00:00
|
|
|
track = AxisToTrack(axis);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-06-10 21:59:22 +00:00
|
|
|
layout_ptr = AllocaM(byte, numtracks * plat_len);
|
2004-11-17 18:03:33 +00:00
|
|
|
GetStationLayout(layout_ptr, numtracks, plat_len, statspec);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-05-06 22:30:36 +00:00
|
|
|
numtracks_orig = numtracks;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2005-05-07 10:26:12 +00:00
|
|
|
TileIndex tile = tile_org;
|
2004-08-09 17:04:08 +00:00
|
|
|
int w = plat_len;
|
|
|
|
do {
|
2006-05-06 22:30:36 +00:00
|
|
|
byte layout = *layout_ptr++;
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(tile) && HasStationReservation(tile)) {
|
2008-08-08 13:53:06 +00:00
|
|
|
/* Check for trains having a reservation for this tile. */
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *v = GetTrainForReservation(tile, AxisToTrack(GetRailStationAxis(tile)));
|
2008-08-02 22:55:08 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
FreeTrainTrackReservation(v);
|
|
|
|
*affected_vehicles.Append() = v;
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false);
|
2009-11-29 19:20:39 +00:00
|
|
|
for (; v->Next() != NULL; v = v->Next()) { }
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), false);
|
2008-08-02 22:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-01 14:51:05 +00:00
|
|
|
/* Remove animation if overbuilding */
|
|
|
|
DeleteAnimatedTile(tile);
|
2010-01-26 23:03:47 +00:00
|
|
|
byte old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0;
|
2009-07-28 18:52:16 +00:00
|
|
|
MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, rt);
|
2009-01-10 14:01:17 +00:00
|
|
|
/* Free the spec if we overbuild something */
|
|
|
|
DeallocateSpecFromStation(st, old_specindex);
|
|
|
|
|
2006-04-19 07:17:00 +00:00
|
|
|
SetCustomStationSpecIndex(tile, specindex);
|
2006-05-03 21:25:49 +00:00
|
|
|
SetStationTileRandomBits(tile, GB(Random(), 0, 4));
|
2010-08-26 14:45:45 +00:00
|
|
|
SetAnimationFrame(tile, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-05-04 20:00:50 +00:00
|
|
|
if (statspec != NULL) {
|
2006-05-08 06:22:01 +00:00
|
|
|
/* Use a fixed axis for GetPlatformInfo as our platforms / numtracks are always the right way around */
|
|
|
|
uint32 platinfo = GetPlatformInfo(AXIS_X, 0, plat_len, numtracks_orig, plat_len - w, numtracks_orig - numtracks, false);
|
2007-11-11 20:24:32 +00:00
|
|
|
|
|
|
|
/* As the station is not yet completely finished, the station does not yet exist. */
|
|
|
|
uint16 callback = GetStationCallback(CBID_STATION_TILE_LAYOUT, platinfo, 0, statspec, NULL, tile);
|
2007-05-01 06:43:18 +00:00
|
|
|
if (callback != CALLBACK_FAILED && callback < 8) SetStationGfx(tile, (callback & ~1) + axis);
|
2008-04-19 23:19:12 +00:00
|
|
|
|
|
|
|
/* Trigger station animation -- after building? */
|
2010-08-26 15:31:40 +00:00
|
|
|
TriggerStationAnimation(st, tile, SAT_BUILT);
|
2006-05-04 20:00:50 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
tile += tile_delta;
|
|
|
|
} while (--w);
|
2008-09-30 20:39:50 +00:00
|
|
|
AddTrackToSignalBuffer(tile_org, track, _current_company);
|
2006-05-27 16:12:16 +00:00
|
|
|
YapfNotifyTrackLayoutChange(tile_org, track);
|
2005-06-25 16:44:57 +00:00
|
|
|
tile_org += tile_delta ^ TileDiffXY(1, 1); // perpendicular to tile_delta
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--numtracks);
|
|
|
|
|
2008-08-02 22:55:08 +00:00
|
|
|
for (uint i = 0; i < affected_vehicles.Length(); ++i) {
|
2008-08-08 13:53:06 +00:00
|
|
|
/* Restore reservations of trains. */
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *v = affected_vehicles[i];
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
|
2008-08-08 13:53:06 +00:00
|
|
|
TryPathReserve(v, true, true);
|
2009-11-29 19:20:39 +00:00
|
|
|
for (; v->Next() != NULL; v = v->Next()) { }
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true);
|
2008-08-02 22:55:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-08 09:35:39 +00:00
|
|
|
st->MarkTilesDirty(false);
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateStationAcceptance(st, false);
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2009-01-08 16:35:45 +00:00
|
|
|
InvalidateWindowData(WC_SELECT_STATION, 0, 0);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->owner, 0);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_TRAINS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2009-07-29 22:09:05 +00:00
|
|
|
static void MakeRailStationAreaSmaller(BaseStation *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-25 08:54:19 +00:00
|
|
|
TileArea ta = st->train_station;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
restart:
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* too small? */
|
2009-07-25 08:54:19 +00:00
|
|
|
if (ta.w != 0 && ta.h != 0) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check the left side, x = constant, y changes */
|
2009-07-25 08:54:19 +00:00
|
|
|
for (uint i = 0; !st->TileBelongsToRailStation(ta.tile + TileDiffXY(0, i));) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* the left side is unused? */
|
2009-07-25 08:54:19 +00:00
|
|
|
if (++i == ta.h) {
|
|
|
|
ta.tile += TileDiffXY(1, 0);
|
|
|
|
ta.w--;
|
2005-06-25 16:44:57 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check the right side, x = constant, y changes */
|
2009-07-25 08:54:19 +00:00
|
|
|
for (uint i = 0; !st->TileBelongsToRailStation(ta.tile + TileDiffXY(ta.w - 1, i));) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* the right side is unused? */
|
2009-07-25 08:54:19 +00:00
|
|
|
if (++i == ta.h) {
|
|
|
|
ta.w--;
|
2005-06-25 16:44:57 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check the upper side, y = constant, x changes */
|
2009-07-25 08:54:19 +00:00
|
|
|
for (uint i = 0; !st->TileBelongsToRailStation(ta.tile + TileDiffXY(i, 0));) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* the left side is unused? */
|
2009-07-25 08:54:19 +00:00
|
|
|
if (++i == ta.w) {
|
|
|
|
ta.tile += TileDiffXY(0, 1);
|
|
|
|
ta.h--;
|
2005-06-25 16:44:57 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* check the lower side, y = constant, x changes */
|
2009-07-25 08:54:19 +00:00
|
|
|
for (uint i = 0; !st->TileBelongsToRailStation(ta.tile + TileDiffXY(i, ta.h - 1));) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* the left side is unused? */
|
2009-07-25 08:54:19 +00:00
|
|
|
if (++i == ta.w) {
|
|
|
|
ta.h--;
|
2005-06-25 16:44:57 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-12-02 16:20:44 +00:00
|
|
|
ta.Clear();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-25 08:54:19 +00:00
|
|
|
st->train_station = ta;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 22:09:05 +00:00
|
|
|
/**
|
|
|
|
* Remove a number of tiles from any rail station within the area.
|
2010-11-20 12:52:06 +00:00
|
|
|
* @param ta the area to clear station tile from.
|
|
|
|
* @param affected_stations the stations affected.
|
|
|
|
* @param flags the command flags.
|
|
|
|
* @param removal_cost the cost for removing the tile, including the rail.
|
|
|
|
* @param keep_rail whether to keep the rail of the station.
|
|
|
|
* @tparam T the type of station to remove.
|
|
|
|
* @return the number of cleared tiles or an error.
|
2005-05-09 16:37:40 +00:00
|
|
|
*/
|
2009-07-29 22:09:05 +00:00
|
|
|
template <class T>
|
2009-09-08 10:12:13 +00:00
|
|
|
CommandCost RemoveFromRailBaseStation(TileArea ta, SmallVector<T *, 4> &affected_stations, DoCommandFlag flags, Money removal_cost, bool keep_rail)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-07 17:10:30 +00:00
|
|
|
/* Count of the number of tiles removed */
|
|
|
|
int quantity = 0;
|
2009-09-08 10:12:13 +00:00
|
|
|
CommandCost total_cost(EXPENSES_CONSTRUCTION);
|
2007-05-07 17:10:30 +00:00
|
|
|
|
|
|
|
/* Do the action for every tile into the area */
|
2010-01-04 18:30:10 +00:00
|
|
|
TILE_AREA_LOOP(tile, ta) {
|
2009-07-29 22:09:05 +00:00
|
|
|
/* Make sure the specified tile is a rail station */
|
|
|
|
if (!HasStationTileRail(tile)) continue;
|
2007-12-04 21:39:03 +00:00
|
|
|
|
|
|
|
/* If there is a vehicle on ground, do not allow to remove (flood) the tile */
|
2010-03-05 21:20:22 +00:00
|
|
|
CommandCost ret = EnsureNoVehicleOnGround(tile);
|
2010-03-20 17:58:24 +00:00
|
|
|
if (ret.Failed()) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-07 17:10:30 +00:00
|
|
|
/* Check ownership of station */
|
2009-07-29 22:09:05 +00:00
|
|
|
T *st = T::GetByTile(tile);
|
2009-08-02 14:54:42 +00:00
|
|
|
if (st == NULL) continue;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
if (_current_company != OWNER_WATER) {
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
2010-03-20 17:58:24 +00:00
|
|
|
if (ret.Failed()) continue;
|
2010-03-13 17:11:28 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-12-04 21:39:03 +00:00
|
|
|
/* Do not allow removing from stations if non-uniform stations are not enabled
|
|
|
|
* The check must be here to give correct error message
|
2008-04-18 04:54:09 +00:00
|
|
|
*/
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!_settings_game.station.nonuniform_stations) return_cmd_error(STR_ERROR_NONUNIFORM_STATIONS_DISALLOWED);
|
2007-12-04 21:39:03 +00:00
|
|
|
|
2007-05-07 17:10:30 +00:00
|
|
|
/* If we reached here, the tile is valid so increase the quantity of tiles we will remove */
|
|
|
|
quantity++;
|
|
|
|
|
2010-11-20 13:57:49 +00:00
|
|
|
if (keep_rail || IsStationTileBlocked(tile)) {
|
|
|
|
/* Don't refund the 'steel' of the track when we keep the
|
|
|
|
* rail, or when the tile didn't have any rail at all. */
|
|
|
|
total_cost.AddCost(-_price[PR_CLEAR_RAIL]);
|
|
|
|
}
|
|
|
|
|
2007-05-07 17:10:30 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-01-15 15:00:01 +00:00
|
|
|
/* read variables before the station tile is removed */
|
2009-07-29 21:41:45 +00:00
|
|
|
uint specindex = GetCustomStationSpecIndex(tile);
|
|
|
|
Track track = GetRailStationTrack(tile);
|
|
|
|
Owner owner = GetTileOwner(tile);
|
2009-07-29 22:09:05 +00:00
|
|
|
RailType rt = GetRailType(tile);
|
2009-05-22 22:22:46 +00:00
|
|
|
Train *v = NULL;
|
2008-08-02 22:55:08 +00:00
|
|
|
|
2009-07-29 21:41:45 +00:00
|
|
|
if (HasStationReservation(tile)) {
|
|
|
|
v = GetTrainForReservation(tile, track);
|
2008-08-08 13:53:06 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
/* Free train reservation. */
|
|
|
|
FreeTrainTrackReservation(v);
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), false);
|
2008-08-08 13:53:06 +00:00
|
|
|
Vehicle *temp = v;
|
2009-11-29 19:20:39 +00:00
|
|
|
for (; temp->Next() != NULL; temp = temp->Next()) { }
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(temp->tile)) SetRailStationPlatformReservation(temp->tile, TrackdirToExitdir(ReverseTrackdir(temp->GetVehicleTrackdir())), false);
|
2008-08-08 13:53:06 +00:00
|
|
|
}
|
2008-08-02 22:55:08 +00:00
|
|
|
}
|
2008-01-15 15:00:01 +00:00
|
|
|
|
2010-10-18 17:06:20 +00:00
|
|
|
bool build_rail = keep_rail && !IsStationTileBlocked(tile);
|
|
|
|
|
2009-10-17 08:12:39 +00:00
|
|
|
DoClearSquare(tile);
|
2010-04-24 13:39:11 +00:00
|
|
|
DeleteNewGRFInspectWindow(GSF_STATIONS, tile);
|
2010-10-18 17:06:20 +00:00
|
|
|
if (build_rail) MakeRailNormal(tile, owner, TrackToTrackBits(track), rt);
|
2009-10-17 08:12:39 +00:00
|
|
|
|
2010-03-13 01:12:07 +00:00
|
|
|
st->rect.AfterRemoveTile(st, tile);
|
2009-07-29 21:41:45 +00:00
|
|
|
AddTrackToSignalBuffer(tile, track, owner);
|
|
|
|
YapfNotifyTrackLayoutChange(tile, track);
|
2007-05-07 17:10:30 +00:00
|
|
|
|
|
|
|
DeallocateSpecFromStation(st, specindex);
|
|
|
|
|
2009-06-26 12:25:53 +00:00
|
|
|
affected_stations.Include(st);
|
2007-05-07 17:10:30 +00:00
|
|
|
|
2008-08-08 13:53:06 +00:00
|
|
|
if (v != NULL) {
|
|
|
|
/* Restore station reservation. */
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
|
2008-08-08 13:53:06 +00:00
|
|
|
TryPathReserve(v, true, true);
|
2009-11-29 19:20:39 +00:00
|
|
|
for (; v->Next() != NULL; v = v->Next()) { }
|
2009-07-24 11:47:12 +00:00
|
|
|
if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(ReverseTrackdir(v->GetVehicleTrackdir())), true);
|
2008-08-08 13:53:06 +00:00
|
|
|
}
|
2009-06-26 12:25:53 +00:00
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2008-08-02 22:55:08 +00:00
|
|
|
|
2010-07-10 20:17:05 +00:00
|
|
|
if (quantity == 0) return_cmd_error(STR_ERROR_THERE_IS_NO_STATION);
|
2009-07-29 21:41:45 +00:00
|
|
|
|
2009-07-29 22:09:05 +00:00
|
|
|
for (T **stp = affected_stations.Begin(); stp != affected_stations.End(); stp++) {
|
|
|
|
T *st = *stp;
|
2009-06-25 15:42:03 +00:00
|
|
|
|
2009-06-26 12:25:53 +00:00
|
|
|
/* now we need to make the "spanned" area of the railway station smaller
|
|
|
|
* if we deleted something at the edges.
|
|
|
|
* we also need to adjust train_tile. */
|
2009-07-24 11:47:12 +00:00
|
|
|
MakeRailStationAreaSmaller(st);
|
2009-06-26 12:25:53 +00:00
|
|
|
UpdateStationSignCoord(st);
|
|
|
|
|
|
|
|
/* if we deleted the whole station, delete the train facility. */
|
2009-07-25 08:54:19 +00:00
|
|
|
if (st->train_station.tile == INVALID_TILE) {
|
2009-06-26 12:25:53 +00:00
|
|
|
st->facilities &= ~FACIL_TRAIN;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_TRAINS);
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2009-06-26 12:25:53 +00:00
|
|
|
DeleteStationIfEmpty(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-07-29 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2009-09-08 10:12:13 +00:00
|
|
|
total_cost.AddCost(quantity * removal_cost);
|
|
|
|
return total_cost;
|
2009-07-29 22:09:05 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Remove a single tile from a rail station.
|
2009-07-29 22:09:05 +00:00
|
|
|
* This allows for custom-built station with holes and weird layouts
|
|
|
|
* @param start tile of station piece to remove
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @param p1 start_tile
|
2009-09-08 10:12:13 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = bit 0 - if set keep the rail
|
2009-07-29 22:09:05 +00:00
|
|
|
* @param text unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @return the cost of this operation or an error
|
2009-07-29 22:09:05 +00:00
|
|
|
*/
|
2009-07-29 22:32:20 +00:00
|
|
|
CommandCost CmdRemoveFromRailStation(TileIndex start, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2009-07-29 22:09:05 +00:00
|
|
|
{
|
|
|
|
TileIndex end = p1 == 0 ? start : p1;
|
|
|
|
if (start >= MapSize() || end >= MapSize()) return CMD_ERROR;
|
2009-06-26 12:25:53 +00:00
|
|
|
|
2009-07-29 22:09:05 +00:00
|
|
|
TileArea ta(start, end);
|
|
|
|
SmallVector<Station *, 4> affected_stations;
|
|
|
|
|
2009-11-07 22:47:54 +00:00
|
|
|
CommandCost ret = RemoveFromRailBaseStation(ta, affected_stations, flags, _price[PR_CLEAR_STATION_RAIL], HasBit(p2, 0));
|
2009-07-29 22:09:05 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
|
|
/* Do all station specific functions here. */
|
|
|
|
for (Station **stp = affected_stations.Begin(); stp != affected_stations.End(); stp++) {
|
|
|
|
Station *st = *stp;
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
if (st->train_station.tile == INVALID_TILE) SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_TRAINS);
|
2009-07-29 22:09:05 +00:00
|
|
|
st->MarkTilesDirty(false);
|
2009-06-26 12:25:53 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
|
|
|
}
|
2007-05-07 17:10:30 +00:00
|
|
|
|
2009-07-29 22:09:05 +00:00
|
|
|
/* Now apply the rail cost to the number that we deleted */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Remove a single tile from a waypoint.
|
2009-07-29 22:09:05 +00:00
|
|
|
* This allows for custom-built waypoint with holes and weird layouts
|
|
|
|
* @param start tile of waypoint piece to remove
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @param p1 start_tile
|
2009-09-08 10:12:13 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = bit 0 - if set keep the rail
|
2009-07-29 22:09:05 +00:00
|
|
|
* @param text unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @return the cost of this operation or an error
|
2009-07-29 22:09:05 +00:00
|
|
|
*/
|
2009-07-29 22:32:20 +00:00
|
|
|
CommandCost CmdRemoveFromRailWaypoint(TileIndex start, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2009-07-29 22:09:05 +00:00
|
|
|
{
|
|
|
|
TileIndex end = p1 == 0 ? start : p1;
|
|
|
|
if (start >= MapSize() || end >= MapSize()) return CMD_ERROR;
|
|
|
|
|
|
|
|
TileArea ta(start, end);
|
|
|
|
SmallVector<Waypoint *, 4> affected_stations;
|
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
return RemoveFromRailBaseStation(ta, affected_stations, flags, _price[PR_CLEAR_WAYPOINT_RAIL], HasBit(p2, 0));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 09:41:35 +00:00
|
|
|
|
2009-07-10 22:46:30 +00:00
|
|
|
/**
|
2010-11-20 14:15:02 +00:00
|
|
|
* Remove a rail station/waypoint
|
2009-07-29 22:13:12 +00:00
|
|
|
* @param st The station/waypoint to remove the rail part from
|
2009-07-10 22:46:30 +00:00
|
|
|
* @param flags operation to perform
|
2009-07-31 16:28:28 +00:00
|
|
|
* @tparam T the type of station to remove
|
2009-07-10 22:46:30 +00:00
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
2009-07-29 22:13:12 +00:00
|
|
|
template <class T>
|
|
|
|
CommandCost RemoveRailStation(T *st, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Current company owns the station? */
|
2010-03-13 17:11:28 +00:00
|
|
|
if (_current_company != OWNER_WATER) {
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* determine width and height of platforms */
|
2009-07-25 08:54:19 +00:00
|
|
|
TileArea ta = st->train_station;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-07-25 08:54:19 +00:00
|
|
|
assert(ta.w != 0 && ta.h != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
2004-08-09 17:04:08 +00:00
|
|
|
/* clear all areas of the station */
|
2010-01-04 18:30:10 +00:00
|
|
|
TILE_AREA_LOOP(tile, ta) {
|
2009-07-29 21:49:37 +00:00
|
|
|
/* for nonuniform stations, only remove tiles that are actually train station tiles */
|
|
|
|
if (!st->TileBelongsToRailStation(tile)) continue;
|
|
|
|
|
2010-03-05 21:20:22 +00:00
|
|
|
CommandCost ret = EnsureNoVehicleOnGround(tile);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-07-29 21:49:37 +00:00
|
|
|
|
2009-11-07 22:47:54 +00:00
|
|
|
cost.AddCost(_price[PR_CLEAR_STATION_RAIL]);
|
2009-07-29 21:49:37 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* read variables before the station tile is removed */
|
|
|
|
Track track = GetRailStationTrack(tile);
|
|
|
|
Owner owner = GetTileOwner(tile); // _current_company can be OWNER_WATER
|
|
|
|
Train *v = NULL;
|
|
|
|
if (HasStationReservation(tile)) {
|
|
|
|
v = GetTrainForReservation(tile, track);
|
|
|
|
if (v != NULL) FreeTrainTrackReservation(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-07-29 21:49:37 +00:00
|
|
|
DoClearSquare(tile);
|
2010-04-24 13:39:11 +00:00
|
|
|
DeleteNewGRFInspectWindow(GSF_STATIONS, tile);
|
2009-07-29 21:49:37 +00:00
|
|
|
AddTrackToSignalBuffer(tile, track, owner);
|
|
|
|
YapfNotifyTrackLayoutChange(tile, track);
|
|
|
|
if (v != NULL) TryPathReserve(v, true);
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2010-03-06 13:38:46 +00:00
|
|
|
st->rect.AfterRemoveRect(st, st->train_station);
|
2009-07-25 08:54:19 +00:00
|
|
|
|
2009-12-02 16:20:44 +00:00
|
|
|
st->train_station.Clear();
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
st->facilities &= ~FACIL_TRAIN;
|
|
|
|
|
2006-04-19 07:17:00 +00:00
|
|
|
free(st->speclist);
|
|
|
|
st->num_specs = 0;
|
|
|
|
st->speclist = NULL;
|
2008-04-19 23:19:12 +00:00
|
|
|
st->cached_anim_triggers = 0;
|
2006-04-19 07:17:00 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_TRAINS);
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2009-07-29 22:13:12 +00:00
|
|
|
/**
|
2010-11-20 14:15:02 +00:00
|
|
|
* Remove a rail station
|
2009-07-29 22:13:12 +00:00
|
|
|
* @param tile TileIndex been queried
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
2009-07-29 22:32:20 +00:00
|
|
|
static CommandCost RemoveRailStation(TileIndex tile, DoCommandFlag flags)
|
2009-07-29 22:13:12 +00:00
|
|
|
{
|
|
|
|
/* if there is flooding and non-uniform stations are enabled, remove platforms tile by tile */
|
|
|
|
if (_current_company == OWNER_WATER && _settings_game.station.nonuniform_stations) {
|
2009-07-29 22:32:20 +00:00
|
|
|
return DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_FROM_RAIL_STATION);
|
2009-07-29 22:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Station *st = Station::GetByTile(tile);
|
|
|
|
CommandCost cost = RemoveRailStation(st, flags);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) st->RecomputeIndustriesNear();
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a rail waypoint
|
|
|
|
* @param tile TileIndex been queried
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
2009-07-29 22:32:20 +00:00
|
|
|
static CommandCost RemoveRailWaypoint(TileIndex tile, DoCommandFlag flags)
|
2009-07-29 22:13:12 +00:00
|
|
|
{
|
|
|
|
/* if there is flooding and non-uniform stations are enabled, remove waypoints tile by tile */
|
|
|
|
if (_current_company == OWNER_WATER && _settings_game.station.nonuniform_stations) {
|
2009-07-29 22:32:20 +00:00
|
|
|
return DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_FROM_RAIL_WAYPOINT);
|
2009-07-29 22:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return RemoveRailStation(Waypoint::GetByTile(tile), flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-02 08:23:48 +00:00
|
|
|
/**
|
2008-03-31 00:06:17 +00:00
|
|
|
* @param truck_station Determines whether a stop is ROADSTOP_BUS or ROADSTOP_TRUCK
|
2007-04-18 18:00:33 +00:00
|
|
|
* @param st The Station to do the whole procedure for
|
2007-02-02 08:23:48 +00:00
|
|
|
* @return a pointer to where to link a new RoadStop*
|
2006-09-04 20:40:33 +00:00
|
|
|
*/
|
2008-04-17 20:03:28 +00:00
|
|
|
static RoadStop **FindRoadStopSpot(bool truck_station, Station *st)
|
2005-01-29 19:41:44 +00:00
|
|
|
{
|
2006-04-29 09:47:43 +00:00
|
|
|
RoadStop **primary_stop = (truck_station) ? &st->truck_stops : &st->bus_stops;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
|
|
|
if (*primary_stop == NULL) {
|
2007-04-18 18:00:33 +00:00
|
|
|
/* we have no roadstop of the type yet, so write a "primary stop" */
|
2007-02-02 08:23:48 +00:00
|
|
|
return primary_stop;
|
2005-01-29 19:41:44 +00:00
|
|
|
} else {
|
2007-04-18 18:00:33 +00:00
|
|
|
/* there are stops already, so append to the end of the list */
|
2007-02-02 08:23:48 +00:00
|
|
|
RoadStop *stop = *primary_stop;
|
|
|
|
while (stop->next != NULL) stop = stop->next;
|
|
|
|
return &stop->next;
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-24 21:55:03 +00:00
|
|
|
static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a nearby station that joins this road stop.
|
|
|
|
* @param existing_stop an existing road stop we build over
|
|
|
|
* @param station_to_join the station to join to
|
|
|
|
* @param adjacent whether adjacent stations are allowed
|
|
|
|
* @param ta the area of the newly build station
|
|
|
|
* @param st 'return' pointer for the found station
|
|
|
|
* @return command cost with the error or 'okay'
|
|
|
|
*/
|
|
|
|
static CommandCost FindJoiningRoadStop(StationID existing_stop, StationID station_to_join, bool adjacent, TileArea ta, Station **st)
|
|
|
|
{
|
|
|
|
return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_ROAD_STOP_FIRST>(existing_stop, station_to_join, adjacent, ta, st);
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build a bus or truck stop.
|
2010-02-24 21:51:59 +00:00
|
|
|
* @param tile Northernmost tile of the stop.
|
2010-02-24 21:45:23 +00:00
|
|
|
* @param flags Operation to perform.
|
2010-02-24 21:51:59 +00:00
|
|
|
* @param p1 bit 0..7: Width of the road stop.
|
|
|
|
* bit 8..15: Lenght of the road stop.
|
2010-02-24 21:45:23 +00:00
|
|
|
* @param p2 bit 0: 0 For bus stops, 1 for truck stops.
|
|
|
|
* bit 1: 0 For normal stops, 1 for drive-through.
|
|
|
|
* bit 2..3: The roadtypes.
|
|
|
|
* bit 5: Allow stations directly adjacent to other stations.
|
|
|
|
* bit 6..7: Entrance direction (DiagDirection).
|
|
|
|
* bit 16..31: Station ID to join (NEW_STATION if build new one).
|
|
|
|
* @param text Unused.
|
|
|
|
* @return The cost of this operation or an error.
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuildRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-11-19 21:02:30 +00:00
|
|
|
bool type = HasBit(p2, 0);
|
|
|
|
bool is_drive_through = HasBit(p2, 1);
|
2010-04-17 13:31:41 +00:00
|
|
|
RoadTypes rts = Extract<RoadTypes, 2, 2>(p2);
|
2009-01-08 16:35:45 +00:00
|
|
|
StationID station_to_join = GB(p2, 16, 16);
|
2009-04-10 22:47:19 +00:00
|
|
|
bool reuse = (station_to_join != NEW_STATION);
|
|
|
|
if (!reuse) station_to_join = INVALID_STATION;
|
2009-01-08 16:35:45 +00:00
|
|
|
bool distant_join = (station_to_join != INVALID_STATION);
|
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
uint8 width = (uint8)GB(p1, 0, 8);
|
|
|
|
uint8 lenght = (uint8)GB(p1, 8, 8);
|
|
|
|
|
|
|
|
/* Check if the requested road stop is too big */
|
|
|
|
if (width > _settings_game.station.station_spread || lenght > _settings_game.station.station_spread) return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT);
|
|
|
|
/* Check for incorrect width / lenght. */
|
|
|
|
if (width == 0 || lenght == 0) return CMD_ERROR;
|
|
|
|
/* Check if the first tile and the last tile are valid */
|
|
|
|
if (!IsValidTile(tile) || TileAddWrap(tile, width - 1, lenght - 1) == INVALID_TILE) return CMD_ERROR;
|
|
|
|
|
|
|
|
TileArea roadstop_area(tile, width, lenght);
|
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
|
2007-05-20 19:14:08 +00:00
|
|
|
|
2010-03-23 20:29:52 +00:00
|
|
|
if (!HasExactlyOneBit(rts) || !HasRoadTypesAvail(_current_company, rts)) return CMD_ERROR;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2007-05-24 08:52:28 +00:00
|
|
|
/* Trams only have drive through stops */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!is_drive_through && HasBit(rts, ROADTYPE_TRAM)) return CMD_ERROR;
|
2007-05-24 08:52:28 +00:00
|
|
|
|
2010-04-17 13:31:41 +00:00
|
|
|
DiagDirection ddir = Extract<DiagDirection, 6, 2>(p2);
|
2010-02-24 21:45:23 +00:00
|
|
|
|
|
|
|
/* Safeguard the parameters. */
|
|
|
|
if (!IsValidDiagDirection(ddir)) return CMD_ERROR;
|
|
|
|
/* If it is a drive-through stop, check for valid axis. */
|
|
|
|
if (is_drive_through && !IsValidAxis((Axis)ddir)) return CMD_ERROR;
|
2007-05-26 14:24:19 +00:00
|
|
|
|
2010-02-27 17:42:55 +00:00
|
|
|
CommandCost ret = CheckIfAuthorityAllowsNewStation(tile, flags);
|
|
|
|
if (ret.Failed()) return ret;
|
2007-05-26 14:24:19 +00:00
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
/* Total road stop cost. */
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION, roadstop_area.w * roadstop_area.h * _price[type ? PR_BUILD_STATION_TRUCK : PR_BUILD_STATION_BUS]);
|
2010-02-24 21:55:03 +00:00
|
|
|
StationID est = INVALID_STATION;
|
2010-02-27 17:42:55 +00:00
|
|
|
ret = CheckFlatLandRoadStop(roadstop_area, flags, is_drive_through ? 5 << ddir : 1 << ddir, is_drive_through, type, DiagDirToAxis(ddir), &est, rts);
|
2010-02-24 21:51:59 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-23 17:33:03 +00:00
|
|
|
Station *st = NULL;
|
2010-02-24 21:55:03 +00:00
|
|
|
ret = FindJoiningRoadStop(est, station_to_join, HasBit(p2, 5), roadstop_area, &st);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2009-01-05 21:06:38 +00:00
|
|
|
/* Find a deleted station close to us */
|
2009-04-10 22:47:19 +00:00
|
|
|
if (st == NULL && reuse) st = GetClosestDeletedStation(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
/* Check if this number of road stops can be allocated. */
|
|
|
|
if (!RoadStop::CanAllocateItem(roadstop_area.w * roadstop_area.h)) return_cmd_error(type ? STR_ERROR_TOO_MANY_TRUCK_STOPS : STR_ERROR_TOO_MANY_BUS_STOPS);
|
2007-01-25 01:29:24 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st != NULL) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (st->owner != _current_company) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_STATION);
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-03-06 13:23:33 +00:00
|
|
|
CommandCost ret = st->rect.BeforeAddRect(roadstop_area.tile, roadstop_area.w, roadstop_area.h, StationRect::ADD_TEST);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2007-01-14 19:18:50 +00:00
|
|
|
/* allocate and initialize new station */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!Station::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
2007-01-14 19:18:50 +00:00
|
|
|
|
2008-04-23 22:16:41 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-04-28 23:34:23 +00:00
|
|
|
st = new Station(tile);
|
2007-01-14 19:18:50 +00:00
|
|
|
|
2008-04-24 18:30:41 +00:00
|
|
|
st->town = ClosestTownFromTile(tile, UINT_MAX);
|
2008-04-25 15:22:32 +00:00
|
|
|
st->string_id = GenerateStationName(st, tile, STATIONNAMING_ROAD);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(_current_company)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(st->town->have_ratings, _current_company);
|
2008-04-23 22:16:41 +00:00
|
|
|
}
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2010-02-24 21:51:59 +00:00
|
|
|
/* Check every tile in the area. */
|
|
|
|
TILE_AREA_LOOP(cur_tile, roadstop_area) {
|
2011-02-01 10:48:10 +00:00
|
|
|
RoadTypes cur_rts = GetRoadTypes(cur_tile);
|
|
|
|
|
2010-02-24 21:55:03 +00:00
|
|
|
if (IsTileType(cur_tile, MP_STATION) && IsRoadStop(cur_tile)) {
|
|
|
|
RemoveRoadStop(cur_tile, flags);
|
|
|
|
}
|
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
RoadStop *road_stop = new RoadStop(cur_tile);
|
|
|
|
/* Insert into linked list of RoadStops. */
|
|
|
|
RoadStop **currstop = FindRoadStopSpot(type, st);
|
|
|
|
*currstop = road_stop;
|
|
|
|
|
|
|
|
if (type) {
|
|
|
|
st->truck_station.Add(cur_tile);
|
|
|
|
} else {
|
|
|
|
st->bus_station.Add(cur_tile);
|
|
|
|
}
|
2009-12-02 16:20:44 +00:00
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
/* Initialize an empty station. */
|
|
|
|
st->AddFacility((type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP, cur_tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
st->rect.BeforeAddTile(cur_tile, StationRect::ADD_TRY);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
RoadStopType rs_type = type ? ROADSTOP_TRUCK : ROADSTOP_BUS;
|
|
|
|
if (is_drive_through) {
|
2010-06-04 19:50:37 +00:00
|
|
|
Owner road_owner = HasBit(cur_rts, ROADTYPE_ROAD) ? GetRoadOwner(cur_tile, ROADTYPE_ROAD) : _current_company;
|
|
|
|
Owner tram_owner = HasBit(cur_rts, ROADTYPE_TRAM) ? GetRoadOwner(cur_tile, ROADTYPE_TRAM) : _current_company;
|
2011-02-01 10:48:10 +00:00
|
|
|
MakeDriveThroughRoadStop(cur_tile, st->owner, road_owner, tram_owner, st->index, rs_type, rts | cur_rts, DiagDirToAxis(ddir));
|
2010-02-24 21:51:59 +00:00
|
|
|
road_stop->MakeDriveThrough();
|
|
|
|
} else {
|
|
|
|
MakeRoadStop(cur_tile, st->owner, st->index, rs_type, rts, ddir);
|
|
|
|
}
|
2010-02-27 16:27:15 +00:00
|
|
|
|
|
|
|
MarkTileDirtyByTile(cur_tile);
|
2007-02-21 19:46:37 +00:00
|
|
|
}
|
2010-02-24 21:51:59 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-24 21:51:59 +00:00
|
|
|
if (st != NULL) {
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateStationAcceptance(st, false);
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2009-01-08 16:35:45 +00:00
|
|
|
InvalidateWindowData(WC_SELECT_STATION, 0, 0);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->owner, 0);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_ROADVEHS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2008-02-09 17:30:13 +00:00
|
|
|
|
2008-08-01 15:07:31 +00:00
|
|
|
static Vehicle *ClearRoadStopStatusEnum(Vehicle *v, void *)
|
2008-02-09 17:30:13 +00:00
|
|
|
{
|
2009-11-12 17:28:20 +00:00
|
|
|
if (v->type == VEH_ROAD) {
|
|
|
|
/* Okay... we are a road vehicle on a drive through road stop.
|
|
|
|
* But that road stop has just been removed, so we need to make
|
|
|
|
* sure we are in a valid state... however, vehicles can also
|
|
|
|
* turn on road stop tiles, so only clear the 'road stop' state
|
|
|
|
* bits and only when the state was 'in road stop', otherwise
|
|
|
|
* we'll end up clearing the turn around bits. */
|
|
|
|
RoadVehicle *rv = RoadVehicle::From(v);
|
|
|
|
if (HasBit(rv->state, RVS_IN_DT_ROAD_STOP)) rv->state &= RVSB_ROAD_STOP_TRACKDIR_MASK;
|
|
|
|
}
|
2008-02-09 17:30:13 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-10 22:46:30 +00:00
|
|
|
/**
|
|
|
|
* Remove a bus station/truck stop
|
2007-10-26 20:42:03 +00:00
|
|
|
* @param tile TileIndex been queried
|
2009-07-10 22:46:30 +00:00
|
|
|
* @param flags operation to perform
|
2007-10-26 20:42:03 +00:00
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
2009-07-10 22:46:30 +00:00
|
|
|
static CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-10 22:46:30 +00:00
|
|
|
Station *st = Station::GetByTile(tile);
|
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
if (_current_company != OWNER_WATER) {
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2006-02-01 06:32:03 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
bool is_truck = IsTruckStop(tile);
|
|
|
|
|
|
|
|
RoadStop **primary_stop;
|
|
|
|
RoadStop *cur_stop;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (is_truck) { // truck stop
|
2005-01-29 19:41:44 +00:00
|
|
|
primary_stop = &st->truck_stops;
|
2009-06-26 10:45:20 +00:00
|
|
|
cur_stop = RoadStop::GetByTile(tile, ROADSTOP_TRUCK);
|
2005-01-29 19:41:44 +00:00
|
|
|
} else {
|
|
|
|
primary_stop = &st->bus_stops;
|
2009-06-26 10:45:20 +00:00
|
|
|
cur_stop = RoadStop::GetByTile(tile, ROADSTOP_BUS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
assert(cur_stop != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-09 17:30:13 +00:00
|
|
|
/* don't do the check for drive-through road stops when company bankrupts */
|
|
|
|
if (IsDriveThroughStopTile(tile) && (flags & DC_BANKRUPT)) {
|
|
|
|
/* remove the 'going through road stop' status from all vehicles on that tile */
|
2008-09-07 11:23:10 +00:00
|
|
|
if (flags & DC_EXEC) FindVehicleOnPos(tile, NULL, &ClearRoadStopStatusEnum);
|
2008-02-09 17:30:13 +00:00
|
|
|
} else {
|
2010-03-05 21:20:22 +00:00
|
|
|
CommandCost ret = EnsureNoVehicleOnGround(tile);
|
|
|
|
if (ret.Failed()) return ret;
|
2008-02-09 17:30:13 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-01-28 21:54:40 +00:00
|
|
|
if (*primary_stop == cur_stop) {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* removed the first stop in the list */
|
2007-01-28 21:54:40 +00:00
|
|
|
*primary_stop = cur_stop->next;
|
2007-10-26 20:42:03 +00:00
|
|
|
/* removed the only stop? */
|
2007-01-28 21:54:40 +00:00
|
|
|
if (*primary_stop == NULL) {
|
|
|
|
st->facilities &= (is_truck ? ~FACIL_TRUCK_STOP : ~FACIL_BUS_STOP);
|
|
|
|
}
|
|
|
|
} else {
|
2007-10-26 20:42:03 +00:00
|
|
|
/* tell the predecessor in the list to skip this stop */
|
2007-01-28 21:54:40 +00:00
|
|
|
RoadStop *pred = *primary_stop;
|
|
|
|
while (pred->next != cur_stop) pred = pred->next;
|
|
|
|
pred->next = cur_stop->next;
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-04 20:52:19 +00:00
|
|
|
if (IsDriveThroughStopTile(tile)) {
|
|
|
|
/* Clears the tile for us */
|
|
|
|
cur_stop->ClearDriveThrough();
|
|
|
|
} else {
|
|
|
|
DoClearSquare(tile);
|
|
|
|
}
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_ROADVEHS);
|
2007-01-17 11:15:51 +00:00
|
|
|
delete cur_stop;
|
2008-10-07 18:46:12 +00:00
|
|
|
|
|
|
|
/* Make sure no vehicle is going to the old roadstop */
|
2009-05-26 22:45:48 +00:00
|
|
|
RoadVehicle *v;
|
|
|
|
FOR_ALL_ROADVEHICLES(v) {
|
|
|
|
if (v->First() == v && v->current_order.IsType(OT_GOTO_STATION) &&
|
2008-10-07 18:46:12 +00:00
|
|
|
v->dest_tile == tile) {
|
|
|
|
v->dest_tile = v->GetOrderStationLocation(st->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
st->rect.AfterRemoveTile(st, tile);
|
2006-08-26 19:14:02 +00:00
|
|
|
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteStationIfEmpty(st);
|
2009-12-02 16:20:44 +00:00
|
|
|
|
|
|
|
/* Update the tile area of the truck/bus stop */
|
|
|
|
if (is_truck) {
|
|
|
|
st->truck_station.Clear();
|
|
|
|
for (const RoadStop *rs = st->truck_stops; rs != NULL; rs = rs->next) st->truck_station.Add(rs->xy);
|
|
|
|
} else {
|
|
|
|
st->bus_station.Clear();
|
|
|
|
for (const RoadStop *rs = st->bus_stops; rs != NULL; rs = rs->next) st->bus_station.Add(rs->xy);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 22:47:54 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[is_truck ? PR_CLEAR_STATION_TRUCK : PR_CLEAR_STATION_BUS]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Remove bus or truck stops.
|
2010-02-24 21:53:16 +00:00
|
|
|
* @param tile Northernmost tile of the removal area.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 bit 0..7: Width of the removal area.
|
|
|
|
* bit 8..15: Height of the removal area.
|
|
|
|
* @param p2 bit 0: 0 For bus stops, 1 for truck stops.
|
|
|
|
* @param text Unused.
|
|
|
|
* @return The cost of this operation or an error.
|
2007-02-14 16:37:16 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRemoveRoadStop(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-02-14 16:37:16 +00:00
|
|
|
{
|
2010-02-24 21:53:16 +00:00
|
|
|
uint8 width = (uint8)GB(p1, 0, 8);
|
|
|
|
uint8 height = (uint8)GB(p1, 8, 8);
|
|
|
|
|
|
|
|
/* Check for incorrect width / height. */
|
|
|
|
if (width == 0 || height == 0) return CMD_ERROR;
|
|
|
|
/* Check if the first tile and the last tile are valid */
|
|
|
|
if (!IsValidTile(tile) || TileAddWrap(tile, width - 1, height - 1) == INVALID_TILE) return CMD_ERROR;
|
|
|
|
|
|
|
|
TileArea roadstop_area(tile, width, height);
|
|
|
|
|
|
|
|
int quantity = 0;
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
|
|
|
TILE_AREA_LOOP(cur_tile, roadstop_area) {
|
|
|
|
/* Make sure the specified tile is a road stop of the correct type */
|
|
|
|
if (!IsTileType(cur_tile, MP_STATION) || !IsRoadStop(cur_tile) || (uint32)GetRoadStopType(cur_tile) != GB(p2, 0, 1)) continue;
|
|
|
|
|
|
|
|
/* Save the stop info before it is removed */
|
|
|
|
bool is_drive_through = IsDriveThroughStopTile(cur_tile);
|
|
|
|
RoadTypes rts = GetRoadTypes(cur_tile);
|
|
|
|
RoadBits road_bits = IsDriveThroughStopTile(cur_tile) ?
|
|
|
|
((GetRoadStopDir(cur_tile) == DIAGDIR_NE) ? ROAD_X : ROAD_Y) :
|
|
|
|
DiagDirToRoadBits(GetRoadStopDir(cur_tile));
|
|
|
|
|
|
|
|
Owner road_owner = GetRoadOwner(cur_tile, ROADTYPE_ROAD);
|
|
|
|
Owner tram_owner = GetRoadOwner(cur_tile, ROADTYPE_TRAM);
|
|
|
|
CommandCost ret = RemoveRoadStop(cur_tile, flags);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
|
|
|
|
|
|
|
quantity++;
|
|
|
|
/* If the stop was a drive-through stop replace the road */
|
|
|
|
if ((flags & DC_EXEC) && is_drive_through) {
|
|
|
|
MakeRoadNormal(cur_tile, road_bits, rts, ClosestTownFromTile(cur_tile, UINT_MAX)->index,
|
|
|
|
road_owner, tram_owner);
|
|
|
|
}
|
2007-02-14 16:37:16 +00:00
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2010-07-10 20:17:05 +00:00
|
|
|
if (quantity == 0) return_cmd_error(STR_ERROR_THERE_IS_NO_STATION);
|
2010-02-24 21:53:16 +00:00
|
|
|
|
|
|
|
return cost;
|
2007-02-14 16:37:16 +00:00
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2009-01-26 21:09:17 +00:00
|
|
|
/**
|
|
|
|
* Computes the minimal distance from town's xy to any airport's tile.
|
2010-01-15 12:08:08 +00:00
|
|
|
* @param as airport's description
|
2009-01-26 21:09:17 +00:00
|
|
|
* @param town_tile town's tile (t->xy)
|
2010-02-22 14:17:07 +00:00
|
|
|
* @param airport_tile st->airport.tile
|
2009-01-26 21:09:17 +00:00
|
|
|
* @return minimal manhattan distance from town_tile to any airport's tile
|
|
|
|
*/
|
2010-01-15 12:08:08 +00:00
|
|
|
static uint GetMinimalAirportDistanceToTile(const AirportSpec *as, TileIndex town_tile, TileIndex airport_tile)
|
2009-01-26 21:09:17 +00:00
|
|
|
{
|
|
|
|
uint ttx = TileX(town_tile); // X, Y of town
|
|
|
|
uint tty = TileY(town_tile);
|
|
|
|
|
|
|
|
uint atx = TileX(airport_tile); // X, Y of northern airport corner
|
|
|
|
uint aty = TileY(airport_tile);
|
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
uint btx = TileX(airport_tile) + as->size_x - 1; // X, Y of southern corner
|
|
|
|
uint bty = TileY(airport_tile) + as->size_y - 1;
|
2009-01-26 21:09:17 +00:00
|
|
|
|
|
|
|
/* if ttx < atx, dx = atx - ttx
|
|
|
|
* if atx <= ttx <= btx, dx = 0
|
|
|
|
* else, dx = ttx - btx (similiar for dy) */
|
|
|
|
uint dx = ttx < atx ? atx - ttx : (ttx <= btx ? 0 : ttx - btx);
|
|
|
|
uint dy = tty < aty ? aty - tty : (tty <= bty ? 0 : tty - bty);
|
|
|
|
|
|
|
|
return dx + dy;
|
|
|
|
}
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Get a possible noise reduction factor based on distance from town center.
|
2008-05-24 02:54:47 +00:00
|
|
|
* The further you get, the less noise you generate.
|
|
|
|
* So all those folks at city council can now happily slee... work in their offices
|
2010-01-15 12:08:08 +00:00
|
|
|
* @param as airport information
|
2008-05-24 02:54:47 +00:00
|
|
|
* @param town_tile TileIndex of town's center, the one who will receive the airport's candidature
|
2008-07-20 15:50:41 +00:00
|
|
|
* @param tile TileIndex of northern tile of an airport (present or to-be-built), NOT the station tile
|
2008-05-24 02:54:47 +00:00
|
|
|
* @return the noise that will be generated, according to distance
|
|
|
|
*/
|
2010-01-15 12:08:08 +00:00
|
|
|
uint8 GetAirportNoiseLevelForTown(const AirportSpec *as, TileIndex town_tile, TileIndex tile)
|
2008-05-24 02:54:47 +00:00
|
|
|
{
|
|
|
|
/* 0 cannot be accounted, and 1 is the lowest that can be reduced from town.
|
|
|
|
* So no need to go any further*/
|
2010-01-15 12:08:08 +00:00
|
|
|
if (as->noise_level < 2) return as->noise_level;
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
uint distance = GetMinimalAirportDistanceToTile(as, town_tile, tile);
|
2008-05-24 02:54:47 +00:00
|
|
|
|
|
|
|
/* The steps for measuring noise reduction are based on the "magical" (and arbitrary) 8 base distance
|
|
|
|
* adding the town_council_tolerance 4 times, as a way to graduate, depending of the tolerance.
|
|
|
|
* Basically, it says that the less tolerant a town is, the bigger the distance before
|
|
|
|
* an actual decrease can be granted */
|
2008-05-29 15:13:28 +00:00
|
|
|
uint8 town_tolerance_distance = 8 + (_settings_game.difficulty.town_council_tolerance * 4);
|
2008-05-24 02:54:47 +00:00
|
|
|
|
|
|
|
/* now, we want to have the distance segmented using the distance judged bareable by town
|
|
|
|
* This will give us the coefficient of reduction the distance provides. */
|
2008-05-27 18:20:14 +00:00
|
|
|
uint noise_reduction = distance / town_tolerance_distance;
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2008-05-27 18:20:14 +00:00
|
|
|
/* If the noise reduction equals the airport noise itself, don't give it for free.
|
2008-05-24 02:54:47 +00:00
|
|
|
* Otherwise, simply reduce the airport's level. */
|
2010-01-15 12:08:08 +00:00
|
|
|
return noise_reduction >= as->noise_level ? 1 : as->noise_level - noise_reduction;
|
2008-05-24 02:54:47 +00:00
|
|
|
}
|
|
|
|
|
2009-01-26 21:09:17 +00:00
|
|
|
/**
|
|
|
|
* Finds the town nearest to given airport. Based on minimal manhattan distance to any airport's tile.
|
|
|
|
* If two towns have the same distance, town with lower index is returned.
|
2010-01-15 12:08:08 +00:00
|
|
|
* @param as airport's description
|
2010-02-22 14:17:07 +00:00
|
|
|
* @param airport_tile st->airport.tile
|
2009-01-26 21:09:17 +00:00
|
|
|
* @return nearest town to airport
|
|
|
|
*/
|
2010-01-15 12:08:08 +00:00
|
|
|
Town *AirportGetNearestTown(const AirportSpec *as, TileIndex airport_tile)
|
2009-01-26 21:09:17 +00:00
|
|
|
{
|
|
|
|
Town *t, *nearest = NULL;
|
2010-01-15 12:08:08 +00:00
|
|
|
uint add = as->size_x + as->size_y - 2; // GetMinimalAirportDistanceToTile can differ from DistanceManhattan by this much
|
2009-01-26 21:09:17 +00:00
|
|
|
uint mindist = UINT_MAX - add; // prevent overflow
|
|
|
|
FOR_ALL_TOWNS(t) {
|
|
|
|
if (DistanceManhattan(t->xy, airport_tile) < mindist + add) { // avoid calling GetMinimalAirportDistanceToTile too often
|
2010-01-15 12:08:08 +00:00
|
|
|
uint dist = GetMinimalAirportDistanceToTile(as, t->xy, airport_tile);
|
2009-01-26 21:09:17 +00:00
|
|
|
if (dist < mindist) {
|
|
|
|
nearest = t;
|
|
|
|
mindist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nearest;
|
|
|
|
}
|
|
|
|
|
2008-07-20 15:50:41 +00:00
|
|
|
|
|
|
|
/** Recalculate the noise generated by the airports of each town */
|
|
|
|
void UpdateAirportsNoise()
|
|
|
|
{
|
|
|
|
Town *t;
|
|
|
|
const Station *st;
|
|
|
|
|
|
|
|
FOR_ALL_TOWNS(t) t->noise_reached = 0;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2010-02-22 14:17:07 +00:00
|
|
|
if (st->airport.tile != INVALID_TILE) {
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportSpec *as = st->airport.GetSpec();
|
2010-02-22 14:17:07 +00:00
|
|
|
Town *nearest = AirportGetNearestTown(as, st->airport.tile);
|
|
|
|
nearest->noise_reached += GetAirportNoiseLevelForTown(as, nearest->xy, st->airport.tile);
|
2008-07-20 15:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Place an Airport.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile where airport will be built
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2010-03-06 16:07:45 +00:00
|
|
|
* @param p1
|
|
|
|
* - p1 = (bit 0- 7) - airport type, @see airport.h
|
|
|
|
* - p1 = (bit 8-15) - airport layout
|
2009-01-08 16:35:45 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = (bit 0) - allow airports directly adjacent to other airports.
|
2009-07-28 18:52:16 +00:00
|
|
|
* - p2 = (bit 16-31) - station ID to join (NEW_STATION if build new one)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuildAirport(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-08 16:35:45 +00:00
|
|
|
StationID station_to_join = GB(p2, 16, 16);
|
2009-04-10 22:47:19 +00:00
|
|
|
bool reuse = (station_to_join != NEW_STATION);
|
|
|
|
if (!reuse) station_to_join = INVALID_STATION;
|
2009-01-08 16:35:45 +00:00
|
|
|
bool distant_join = (station_to_join != INVALID_STATION);
|
2010-03-06 16:07:45 +00:00
|
|
|
byte airport_type = GB(p1, 0, 8);
|
|
|
|
byte layout = GB(p1, 8, 8);
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-06 16:07:45 +00:00
|
|
|
if (airport_type >= NUM_AIRPORTS) return CMD_ERROR;
|
2005-05-09 22:33:00 +00:00
|
|
|
|
2010-02-27 17:42:55 +00:00
|
|
|
CommandCost ret = CheckIfAuthorityAllowsNewStation(tile, flags);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-06-21 13:26:30 +00:00
|
|
|
/* Check if a valid, buildable airport was chosen for construction */
|
2010-03-06 16:07:45 +00:00
|
|
|
const AirportSpec *as = AirportSpec::Get(airport_type);
|
|
|
|
if (!as->IsAvailable() || layout >= as->num_table) return CMD_ERROR;
|
2009-06-21 13:26:30 +00:00
|
|
|
|
2010-08-05 12:00:09 +00:00
|
|
|
Direction rotation = as->rotation[layout];
|
2009-06-21 13:26:30 +00:00
|
|
|
Town *t = ClosestTownFromTile(tile, UINT_MAX);
|
2010-01-15 12:08:08 +00:00
|
|
|
int w = as->size_x;
|
|
|
|
int h = as->size_y;
|
2010-08-05 12:00:09 +00:00
|
|
|
if (rotation == DIR_E || rotation == DIR_W) Swap(w, h);
|
2008-05-02 02:06:57 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread) {
|
2010-02-27 14:03:06 +00:00
|
|
|
return_cmd_error(STR_ERROR_STATION_TOO_SPREAD_OUT);
|
2008-05-02 02:06:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-20 18:12:56 +00:00
|
|
|
CommandCost cost = CheckFlatLand(TileArea(tile, w, h), flags);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-10-30 17:51:07 +00:00
|
|
|
/* The noise level is the noise from the airport and reduce it to account for the distance to the town center. */
|
2010-01-15 12:08:08 +00:00
|
|
|
Town *nearest = AirportGetNearestTown(as, tile);
|
|
|
|
uint newnoise_level = GetAirportNoiseLevelForTown(as, nearest->xy, tile);
|
2008-05-24 02:54:47 +00:00
|
|
|
|
|
|
|
/* Check if local auth would allow a new airport */
|
2009-01-29 03:12:31 +00:00
|
|
|
StringID authority_refuse_message = STR_NULL;
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.economy.station_noise_level) {
|
2008-05-24 02:54:47 +00:00
|
|
|
/* do not allow to build a new airport if this raise the town noise over the maximum allowed by town */
|
2009-01-29 03:12:31 +00:00
|
|
|
if ((nearest->noise_reached + newnoise_level) > nearest->MaxTownNoise()) {
|
2009-08-05 17:59:21 +00:00
|
|
|
authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_NOISE;
|
2009-01-29 03:12:31 +00:00
|
|
|
}
|
2008-05-24 02:54:47 +00:00
|
|
|
} else {
|
|
|
|
uint num = 0;
|
|
|
|
const Station *st;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2010-03-18 21:02:20 +00:00
|
|
|
if (st->town == t && (st->facilities & FACIL_AIRPORT) && st->airport.type != AT_OILRIG) num++;
|
2008-05-24 02:54:47 +00:00
|
|
|
}
|
2009-01-29 03:12:31 +00:00
|
|
|
if (num >= 2) {
|
2009-04-21 23:40:56 +00:00
|
|
|
authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_AIRPORT;
|
2009-01-29 03:12:31 +00:00
|
|
|
}
|
2008-05-02 02:06:57 +00:00
|
|
|
}
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2009-01-29 03:12:31 +00:00
|
|
|
if (authority_refuse_message != STR_NULL) {
|
2008-05-02 02:06:57 +00:00
|
|
|
SetDParam(0, t->index);
|
2009-01-29 03:12:31 +00:00
|
|
|
return_cmd_error(authority_refuse_message);
|
2008-05-02 02:06:57 +00:00
|
|
|
}
|
2007-05-23 17:33:03 +00:00
|
|
|
|
2009-07-30 21:41:21 +00:00
|
|
|
Station *st = NULL;
|
2010-02-27 17:42:55 +00:00
|
|
|
ret = FindJoiningStation(INVALID_STATION, station_to_join, HasBit(p2, 0), TileArea(tile, w, h), &st);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-08 16:35:45 +00:00
|
|
|
/* Distant join */
|
2009-07-23 12:02:09 +00:00
|
|
|
if (st == NULL && distant_join) st = Station::GetIfValid(station_to_join);
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2009-01-05 21:06:38 +00:00
|
|
|
/* Find a deleted station close to us */
|
2009-04-10 22:47:19 +00:00
|
|
|
if (st == NULL && reuse) st = GetClosestDeletedStation(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (st != NULL) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (st->owner != _current_company) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_STATION);
|
2008-04-17 20:03:28 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-03-06 13:23:33 +00:00
|
|
|
CommandCost ret = st->rect.BeforeAddRect(tile, w, h, StationRect::ADD_TEST);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-22 14:17:07 +00:00
|
|
|
if (st->airport.tile != INVALID_TILE) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_AIRPORT);
|
2008-04-17 20:03:28 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2007-01-14 19:18:50 +00:00
|
|
|
/* allocate and initialize new station */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!Station::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-23 22:16:41 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-04-28 23:34:23 +00:00
|
|
|
st = new Station(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-24 18:17:53 +00:00
|
|
|
st->town = t;
|
2010-03-18 23:41:39 +00:00
|
|
|
st->string_id = GenerateStationName(st, tile, !(GetAirport(airport_type)->flags & AirportFTAClass::AIRPLANES) ? STATIONNAMING_HELIPORT : STATIONNAMING_AIRPORT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(_current_company)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(st->town->have_ratings, _current_company);
|
2008-04-23 22:16:41 +00:00
|
|
|
}
|
2007-02-17 15:03:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-03-18 00:41:16 +00:00
|
|
|
const AirportTileTable *it = as->table[layout];
|
|
|
|
do {
|
|
|
|
cost.AddCost(_price[PR_BUILD_STATION_AIRPORT]);
|
|
|
|
} while ((++it)->ti.x != -0x80);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-05-24 02:54:47 +00:00
|
|
|
/* Always add the noise, so there will be no need to recalculate when option toggles */
|
2009-01-26 21:09:17 +00:00
|
|
|
nearest->noise_reached += newnoise_level;
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2007-01-18 09:34:44 +00:00
|
|
|
st->AddFacility(FACIL_AIRPORT, tile);
|
2010-03-18 23:41:39 +00:00
|
|
|
st->airport.type = airport_type;
|
2010-08-05 11:59:07 +00:00
|
|
|
st->airport.layout = layout;
|
2010-03-18 21:02:20 +00:00
|
|
|
st->airport.flags = 0;
|
2010-08-05 12:00:09 +00:00
|
|
|
st->airport.rotation = rotation;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
st->rect.BeforeAddRect(tile, w, h, StationRect::ADD_TRY);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2010-03-18 00:41:16 +00:00
|
|
|
it = as->table[layout];
|
2010-01-15 12:08:08 +00:00
|
|
|
do {
|
|
|
|
TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
|
2010-08-05 12:06:53 +00:00
|
|
|
MakeAirport(cur_tile, st->owner, st->index, it->gfx, WATER_CLASS_INVALID);
|
2010-02-22 14:17:33 +00:00
|
|
|
SetStationTileRandomBits(cur_tile, GB(Random(), 0, 4));
|
2010-02-22 14:17:07 +00:00
|
|
|
st->airport.Add(cur_tile);
|
2010-02-22 14:16:57 +00:00
|
|
|
|
2010-08-26 15:31:40 +00:00
|
|
|
if (AirportTileSpec::Get(GetTranslatedAirportTileID(it->gfx))->animation.status != ANIM_STATUS_NO_ANIMATION) AddAnimatedTile(cur_tile);
|
2010-02-22 14:16:57 +00:00
|
|
|
} while ((++it)->ti.x != -0x80);
|
|
|
|
|
|
|
|
/* Only call the animation trigger after all tiles have been built */
|
2010-03-06 16:07:45 +00:00
|
|
|
it = as->table[layout];
|
2010-02-22 14:16:57 +00:00
|
|
|
do {
|
|
|
|
TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
|
|
|
|
AirportTileAnimationTrigger(st, cur_tile, AAT_BUILT);
|
2010-01-15 12:08:08 +00:00
|
|
|
} while ((++it)->ti.x != -0x80);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-17 21:50:58 +00:00
|
|
|
UpdateAirplanesOnNewStation(st);
|
2010-03-05 19:19:29 +00:00
|
|
|
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateStationAcceptance(st, false);
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2009-01-08 16:35:45 +00:00
|
|
|
InvalidateWindowData(WC_SELECT_STATION, 0, 0);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->owner, 0);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_PLANES);
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.economy.station_noise_level) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, st->town->index);
|
2008-05-24 02:54:47 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2009-07-10 22:46:30 +00:00
|
|
|
/**
|
|
|
|
* Remove an airport
|
|
|
|
* @param tile TileIndex been queried
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
|
|
|
static CommandCost RemoveAirport(TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-10 22:46:30 +00:00
|
|
|
Station *st = Station::GetByTile(tile);
|
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
if (_current_company != OWNER_WATER) {
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2008-04-17 20:03:28 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-02-22 14:17:07 +00:00
|
|
|
tile = st->airport.tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-18 12:32:50 +00:00
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
const Aircraft *a;
|
|
|
|
FOR_ALL_AIRCRAFT(a) {
|
2009-07-13 16:37:27 +00:00
|
|
|
if (!a->IsNormalAircraft()) continue;
|
2009-05-22 20:07:26 +00:00
|
|
|
if (a->targetairport == st->index && a->state != FLYING) return CMD_ERROR;
|
2007-03-29 13:52:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 14:17:07 +00:00
|
|
|
TILE_AREA_LOOP(tile_cur, st->airport) {
|
2010-01-18 12:32:50 +00:00
|
|
|
if (!st->TileBelongsToAirport(tile_cur)) continue;
|
|
|
|
|
2010-03-06 01:58:55 +00:00
|
|
|
CommandCost ret = EnsureNoVehicleOnGround(tile_cur);
|
2010-03-05 21:20:22 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2010-02-22 14:17:07 +00:00
|
|
|
|
2010-01-18 12:32:50 +00:00
|
|
|
cost.AddCost(_price[PR_CLEAR_STATION_AIRPORT]);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2010-08-18 20:48:38 +00:00
|
|
|
if (IsHangarTile(tile_cur)) OrderBackup::Reset(tile_cur, false);
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteAnimatedTile(tile_cur);
|
|
|
|
DoClearSquare(tile_cur);
|
2010-04-24 13:39:11 +00:00
|
|
|
DeleteNewGRFInspectWindow(GSF_AIRPORTTILES, tile_cur);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportSpec *as = st->airport.GetSpec();
|
2010-03-19 11:17:52 +00:00
|
|
|
for (uint i = 0; i < st->airport.GetNumHangars(); ++i) {
|
2006-02-18 14:41:24 +00:00
|
|
|
DeleteWindowById(
|
2010-03-19 09:58:46 +00:00
|
|
|
WC_VEHICLE_DEPOT, st->airport.GetHangarTile(i)
|
2006-02-18 14:41:24 +00:00
|
|
|
);
|
|
|
|
}
|
2004-12-21 23:27:58 +00:00
|
|
|
|
2010-10-30 17:51:07 +00:00
|
|
|
/* The noise level is the noise from the airport and reduce it to account for the distance to the town center.
|
2009-02-08 12:25:13 +00:00
|
|
|
* And as for construction, always remove it, even if the setting is not set, in order to avoid the
|
2008-05-24 02:54:47 +00:00
|
|
|
* need of recalculation */
|
2010-01-15 12:08:08 +00:00
|
|
|
Town *nearest = AirportGetNearestTown(as, tile);
|
|
|
|
nearest->noise_reached -= GetAirportNoiseLevelForTown(as, nearest->xy, tile);
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2010-03-06 13:38:46 +00:00
|
|
|
st->rect.AfterRemoveRect(st, st->airport);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2010-02-22 14:17:07 +00:00
|
|
|
st->airport.Clear();
|
2004-08-09 17:04:08 +00:00
|
|
|
st->facilities &= ~FACIL_AIRPORT;
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_PLANES);
|
2008-05-24 02:54:47 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.economy.station_noise_level) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_TOWN_VIEW, st->town->index);
|
2008-05-24 02:54:47 +00:00
|
|
|
}
|
|
|
|
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteStationIfEmpty(st);
|
2010-04-24 13:39:11 +00:00
|
|
|
DeleteNewGRFInspectWindow(GSF_AIRPORTS, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2007-12-19 23:35:14 +00:00
|
|
|
/**
|
2008-09-30 20:39:50 +00:00
|
|
|
* Tests whether the company's vehicles have this station in orders
|
2007-12-19 23:35:14 +00:00
|
|
|
* @param station station ID
|
2010-08-20 12:50:59 +00:00
|
|
|
* @param include_company If true only check vehicles of \a company, if false only check vehicles of other companies
|
|
|
|
* @param company company ID
|
2007-12-19 23:35:14 +00:00
|
|
|
*/
|
2010-08-20 12:50:59 +00:00
|
|
|
bool HasStationInUse(StationID station, bool include_company, CompanyID company)
|
2005-01-20 22:19:34 +00:00
|
|
|
{
|
|
|
|
const Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2010-08-20 12:50:59 +00:00
|
|
|
if ((v->owner == company) == include_company) {
|
2005-01-20 22:19:34 +00:00
|
|
|
const Order *order;
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
2009-07-22 08:59:57 +00:00
|
|
|
if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station) {
|
2005-01-20 22:19:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _dock_tileoffs_chkaround[] = {
|
|
|
|
{-1, 0},
|
|
|
|
{ 0, 0},
|
|
|
|
{ 0, 0},
|
|
|
|
{ 0, -1}
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2006-08-22 14:38:37 +00:00
|
|
|
static const byte _dock_w_chk[4] = { 2, 1, 2, 1 };
|
|
|
|
static const byte _dock_h_chk[4] = { 1, 2, 1, 2 };
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build a dock/haven.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile where dock will be built
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2007-05-23 17:33:03 +00:00
|
|
|
* @param p1 (bit 0) - allow docks directly adjacent to other docks.
|
2009-07-28 18:52:16 +00:00
|
|
|
* @param p2 bit 16-31: station ID to join (NEW_STATION if build new one)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-09 22:33:00 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuildDock(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-08 16:35:45 +00:00
|
|
|
StationID station_to_join = GB(p2, 16, 16);
|
2009-04-10 22:47:19 +00:00
|
|
|
bool reuse = (station_to_join != NEW_STATION);
|
|
|
|
if (!reuse) station_to_join = INVALID_STATION;
|
2009-01-08 16:35:45 +00:00
|
|
|
bool distant_join = (station_to_join != INVALID_STATION);
|
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (distant_join && (!_settings_game.station.distant_join_stations || !Station::IsValidID(station_to_join))) return CMD_ERROR;
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2008-01-25 15:47:58 +00:00
|
|
|
DiagDirection direction = GetInclinedSlopeDirection(GetTileSlope(tile, NULL));
|
2009-04-21 23:40:56 +00:00
|
|
|
if (direction == INVALID_DIAGDIR) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2008-01-25 15:47:58 +00:00
|
|
|
direction = ReverseDiagDir(direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-23 08:47:49 +00:00
|
|
|
/* Docks cannot be placed on rapids */
|
2010-11-21 18:39:12 +00:00
|
|
|
if (HasTileWaterGround(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2008-01-23 08:47:49 +00:00
|
|
|
|
2010-02-27 17:42:55 +00:00
|
|
|
CommandCost ret = CheckIfAuthorityAllowsNewStation(tile, flags);
|
|
|
|
if (ret.Failed()) return ret;
|
2006-09-18 18:02:33 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
2007-03-14 12:56:09 +00:00
|
|
|
|
2010-03-14 12:58:51 +00:00
|
|
|
ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
TileIndex tile_cur = tile + TileOffsByDiagDir(direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-04-23 13:48:16 +00:00
|
|
|
if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2006-02-21 07:41:54 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (MayHaveBridgeAbove(tile_cur) && IsBridgeAbove(tile_cur)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
2007-03-14 12:56:09 +00:00
|
|
|
|
2008-02-02 09:28:43 +00:00
|
|
|
/* Get the water class of the water tile before it is cleared.*/
|
|
|
|
WaterClass wc = GetWaterClass(tile_cur);
|
|
|
|
|
2010-03-14 12:58:51 +00:00
|
|
|
ret = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-09-05 23:21:41 +00:00
|
|
|
tile_cur += TileOffsByDiagDir(direction);
|
2006-04-23 13:48:16 +00:00
|
|
|
if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2006-02-21 07:41:54 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* middle */
|
2007-05-23 17:33:03 +00:00
|
|
|
Station *st = NULL;
|
2010-02-27 17:42:55 +00:00
|
|
|
ret = FindJoiningStation(INVALID_STATION, station_to_join, HasBit(p1, 0),
|
2009-07-30 21:41:21 +00:00
|
|
|
TileArea(tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]),
|
|
|
|
_dock_w_chk[direction], _dock_h_chk[direction]), &st);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-01-08 16:35:45 +00:00
|
|
|
/* Distant join */
|
2009-07-23 12:02:09 +00:00
|
|
|
if (st == NULL && distant_join) st = Station::GetIfValid(station_to_join);
|
2009-01-08 16:35:45 +00:00
|
|
|
|
2009-01-05 21:06:38 +00:00
|
|
|
/* Find a deleted station close to us */
|
2009-04-10 22:47:19 +00:00
|
|
|
if (st == NULL && reuse) st = GetClosestDeletedStation(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (st != NULL) {
|
2008-09-30 20:39:50 +00:00
|
|
|
if (st->owner != _current_company) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_STATION);
|
2008-04-17 20:03:28 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-03-06 13:23:33 +00:00
|
|
|
CommandCost ret = st->rect.BeforeAddRect(
|
2008-10-10 20:09:29 +00:00
|
|
|
tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]),
|
2010-03-06 13:23:33 +00:00
|
|
|
_dock_w_chk[direction], _dock_h_chk[direction], StationRect::ADD_TEST);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (st->dock_tile != INVALID_TILE) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_DOCK);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2008-04-23 22:16:41 +00:00
|
|
|
/* allocate and initialize new station */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!Station::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-23 22:16:41 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-04-28 23:34:23 +00:00
|
|
|
st = new Station(tile);
|
2007-01-14 19:18:50 +00:00
|
|
|
|
2008-04-24 18:30:41 +00:00
|
|
|
st->town = ClosestTownFromTile(tile, UINT_MAX);
|
2008-04-25 15:22:32 +00:00
|
|
|
st->string_id = GenerateStationName(st, tile, STATIONNAMING_DOCK);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(_current_company)) {
|
2008-09-30 20:39:50 +00:00
|
|
|
SetBit(st->town->have_ratings, _current_company);
|
2008-04-23 22:16:41 +00:00
|
|
|
}
|
2007-01-14 19:18:50 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
st->dock_tile = tile;
|
2007-01-18 09:34:44 +00:00
|
|
|
st->AddFacility(FACIL_DOCK, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-10-10 20:09:29 +00:00
|
|
|
st->rect.BeforeAddRect(
|
|
|
|
tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]),
|
|
|
|
_dock_w_chk[direction], _dock_h_chk[direction], StationRect::ADD_TRY);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2008-02-02 09:28:43 +00:00
|
|
|
MakeDock(tile, st->owner, st->index, direction, wc);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateStationAcceptance(st, false);
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2009-01-08 16:35:45 +00:00
|
|
|
InvalidateWindowData(WC_SELECT_STATION, 0, 0);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->owner, 0);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_SHIPS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-17 20:03:28 +00:00
|
|
|
|
2009-11-07 22:47:54 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_STATION_DOCK]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 22:46:30 +00:00
|
|
|
/**
|
|
|
|
* Remove a dock
|
|
|
|
* @param tile TileIndex been queried
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
|
|
|
static CommandCost RemoveDock(TileIndex tile, DoCommandFlag flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-10 22:46:30 +00:00
|
|
|
Station *st = Station::GetByTile(tile);
|
2010-03-13 17:11:28 +00:00
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
TileIndex tile1 = st->dock_tile;
|
|
|
|
TileIndex tile2 = tile1 + TileOffsByDiagDir(GetDockDirection(tile1));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
ret = EnsureNoVehicleOnGround(tile1);
|
2010-03-05 21:20:22 +00:00
|
|
|
if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile2);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
DoClearSquare(tile1);
|
2010-08-27 20:46:36 +00:00
|
|
|
MarkTileDirtyByTile(tile1);
|
2008-02-02 09:28:43 +00:00
|
|
|
MakeWaterKeepingClass(tile2, st->owner);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2007-01-14 23:02:12 +00:00
|
|
|
st->rect.AfterRemoveTile(st, tile1);
|
|
|
|
st->rect.AfterRemoveTile(st, tile2);
|
2006-12-27 23:11:43 +00:00
|
|
|
|
2008-12-26 18:01:15 +00:00
|
|
|
st->dock_tile = INVALID_TILE;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->facilities &= ~FACIL_DOCK;
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, st->index, SVW_SHIPS);
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
|
|
|
|
2009-11-07 22:47:54 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_STATION_DOCK]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "table/station_land.h"
|
|
|
|
|
2007-07-16 23:55:22 +00:00
|
|
|
const DrawTileSprites *GetStationTileLayout(StationType st, byte gfx)
|
2006-05-06 20:33:22 +00:00
|
|
|
{
|
2007-07-16 23:55:22 +00:00
|
|
|
return &_station_display_datas[st][gfx];
|
2006-05-06 20:33:22 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void DrawTile_Station(TileInfo *ti)
|
|
|
|
{
|
2004-11-17 18:03:33 +00:00
|
|
|
const DrawTileSprites *t = NULL;
|
2007-05-25 22:07:40 +00:00
|
|
|
RoadTypes roadtypes;
|
2007-12-31 11:13:51 +00:00
|
|
|
int32 total_offset;
|
|
|
|
int32 custom_ground_offset;
|
2010-02-07 22:22:54 +00:00
|
|
|
const RailtypeInfo *rti = NULL;
|
2010-01-21 13:34:05 +00:00
|
|
|
uint32 relocation = 0;
|
|
|
|
const BaseStation *st = NULL;
|
|
|
|
const StationSpec *statspec = NULL;
|
2007-12-31 11:13:51 +00:00
|
|
|
|
2009-07-24 11:47:12 +00:00
|
|
|
if (HasStationRail(ti->tile)) {
|
2010-02-07 22:22:54 +00:00
|
|
|
rti = GetRailTypeInfo(GetRailType(ti->tile));
|
2007-05-25 22:07:40 +00:00
|
|
|
roadtypes = ROADTYPES_NONE;
|
2007-12-31 11:13:51 +00:00
|
|
|
total_offset = rti->total_offset;
|
|
|
|
custom_ground_offset = rti->custom_ground_offset;
|
2010-01-21 13:34:05 +00:00
|
|
|
|
|
|
|
if (IsCustomStationSpecIndex(ti->tile)) {
|
|
|
|
/* look for customization */
|
|
|
|
st = BaseStation::GetByTile(ti->tile);
|
|
|
|
statspec = st->speclist[GetCustomStationSpecIndex(ti->tile)].spec;
|
|
|
|
|
|
|
|
if (statspec != NULL) {
|
|
|
|
uint tile = GetStationGfx(ti->tile);
|
|
|
|
|
|
|
|
relocation = GetCustomStationRelocation(statspec, st, ti->tile);
|
|
|
|
|
|
|
|
if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
|
|
|
|
uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0, 0, statspec, st, ti->tile);
|
|
|
|
if (callback != CALLBACK_FAILED) tile = (callback & ~1) + GetRailStationAxis(ti->tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure the chosen tile layout is valid for this custom station */
|
|
|
|
if (statspec->renderdata != NULL) {
|
|
|
|
t = &statspec->renderdata[tile < statspec->tiles ? tile : (uint)GetRailStationAxis(ti->tile)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-25 22:07:40 +00:00
|
|
|
} else {
|
2008-02-01 20:10:57 +00:00
|
|
|
roadtypes = IsRoadStop(ti->tile) ? GetRoadTypes(ti->tile) : ROADTYPES_NONE;
|
2007-12-31 11:13:51 +00:00
|
|
|
total_offset = 0;
|
|
|
|
custom_ground_offset = 0;
|
2007-05-25 22:07:40 +00:00
|
|
|
}
|
2010-01-24 20:25:28 +00:00
|
|
|
|
|
|
|
if (IsAirport(ti->tile)) {
|
2010-02-22 14:16:41 +00:00
|
|
|
StationGfx gfx = GetAirportGfx(ti->tile);
|
|
|
|
if (gfx >= NEW_AIRPORTTILE_OFFSET) {
|
|
|
|
const AirportTileSpec *ats = AirportTileSpec::Get(gfx);
|
2010-10-28 11:10:12 +00:00
|
|
|
if (ats->grf_prop.spritegroup[0] != NULL && DrawNewAirportTile(ti, Station::GetByTile(ti->tile), gfx, ats)) {
|
2010-02-22 14:16:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* No sprite group (or no valid one) found, meaning no graphics associated.
|
|
|
|
* Use the substitute one instead */
|
|
|
|
assert(ats->grf_prop.subst_id != INVALID_AIRPORTTILE);
|
|
|
|
gfx = ats->grf_prop.subst_id;
|
|
|
|
}
|
|
|
|
switch (gfx) {
|
2010-01-24 20:25:28 +00:00
|
|
|
case APT_RADAR_GRASS_FENCE_SW:
|
2010-08-26 14:45:45 +00:00
|
|
|
t = &_station_display_datas_airport_radar_grass_fence_sw[GetAnimationFrame(ti->tile)];
|
2010-01-24 20:25:28 +00:00
|
|
|
break;
|
|
|
|
case APT_GRASS_FENCE_NE_FLAG:
|
2010-08-26 14:45:45 +00:00
|
|
|
t = &_station_display_datas_airport_flag_grass_fence_ne[GetAnimationFrame(ti->tile)];
|
2010-01-24 20:25:28 +00:00
|
|
|
break;
|
|
|
|
case APT_RADAR_FENCE_SW:
|
2010-08-26 14:45:45 +00:00
|
|
|
t = &_station_display_datas_airport_radar_fence_sw[GetAnimationFrame(ti->tile)];
|
2010-01-24 20:25:28 +00:00
|
|
|
break;
|
|
|
|
case APT_RADAR_FENCE_NE:
|
2010-08-26 14:45:45 +00:00
|
|
|
t = &_station_display_datas_airport_radar_fence_ne[GetAnimationFrame(ti->tile)];
|
2010-01-24 20:25:28 +00:00
|
|
|
break;
|
|
|
|
case APT_GRASS_FENCE_NE_FLAG_2:
|
2010-08-26 14:45:45 +00:00
|
|
|
t = &_station_display_datas_airport_flag_grass_fence_ne_2[GetAnimationFrame(ti->tile)];
|
2010-01-24 20:25:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
Owner owner = GetTileOwner(ti->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID palette;
|
2009-05-17 01:00:56 +00:00
|
|
|
if (Company::IsValidID(owner)) {
|
2009-02-09 02:57:15 +00:00
|
|
|
palette = COMPANY_SPRITE_COLOUR(owner);
|
2006-08-05 17:00:09 +00:00
|
|
|
} else {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Some stations are not owner by a company, namely oil rigs */
|
2006-08-05 17:00:09 +00:00
|
|
|
palette = PALETTE_TO_GREY;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-05-18 20:49:53 +00:00
|
|
|
if (t == NULL || t->seq == NULL) t = GetStationTileLayout(GetStationType(ti->tile), GetStationGfx(ti->tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-03 22:44:57 +00:00
|
|
|
/* don't show foundation for docks */
|
|
|
|
if (ti->tileh != SLOPE_FLAT && !IsDock(ti->tile)) {
|
|
|
|
if (statspec != NULL && HasBit(statspec->flags, SSF_CUSTOM_FOUNDATIONS)) {
|
|
|
|
/* Station has custom foundations. */
|
|
|
|
SpriteID image = GetCustomStationFoundationRelocation(statspec, st, ti->tile);
|
|
|
|
|
|
|
|
if (HasBit(statspec->flags, SSF_EXTENDED_FOUNDATIONS)) {
|
|
|
|
/* Station provides extended foundations. */
|
|
|
|
|
|
|
|
static const uint8 foundation_parts[] = {
|
|
|
|
0, 0, 0, 0, // Invalid, Invalid, Invalid, SLOPE_SW
|
|
|
|
0, 1, 2, 3, // Invalid, SLOPE_EW, SLOPE_SE, SLOPE_WSE
|
|
|
|
0, 4, 5, 6, // Invalid, SLOPE_NW, SLOPE_NS, SLOPE_NWS
|
|
|
|
7, 8, 9 // SLOPE_NE, SLOPE_ENW, SLOPE_SEN
|
|
|
|
};
|
|
|
|
|
|
|
|
AddSortableSpriteToDraw(image + foundation_parts[ti->tileh], PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z);
|
|
|
|
} else {
|
|
|
|
/* Draw simple foundations, built up from 8 possible foundation sprites. */
|
|
|
|
|
|
|
|
/* Each set bit represents one of the eight composite sprites to be drawn.
|
|
|
|
* 'Invalid' entries will not drawn but are included for completeness. */
|
|
|
|
static const uint8 composite_foundation_parts[] = {
|
|
|
|
/* Invalid (00000000), Invalid (11010001), Invalid (11100100), SLOPE_SW (11100000) */
|
|
|
|
0x00, 0xD1, 0xE4, 0xE0,
|
|
|
|
/* Invalid (11001010), SLOPE_EW (11001001), SLOPE_SE (11000100), SLOPE_WSE (11000000) */
|
|
|
|
0xCA, 0xC9, 0xC4, 0xC0,
|
|
|
|
/* Invalid (11010010), SLOPE_NW (10010001), SLOPE_NS (11100100), SLOPE_NWS (10100000) */
|
|
|
|
0xD2, 0x91, 0xE4, 0xA0,
|
|
|
|
/* SLOPE_NE (01001010), SLOPE_ENW (00001001), SLOPE_SEN (01000100) */
|
|
|
|
0x4A, 0x09, 0x44
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8 parts = composite_foundation_parts[ti->tileh];
|
|
|
|
|
|
|
|
/* If foundations continue beyond the tile's upper sides then
|
|
|
|
* mask out the last two pieces. */
|
|
|
|
uint z;
|
|
|
|
Slope slope = GetFoundationSlope(ti->tile, &z);
|
|
|
|
if (!HasFoundationNW(ti->tile, slope, z)) ClrBit(parts, 6);
|
|
|
|
if (!HasFoundationNE(ti->tile, slope, z)) ClrBit(parts, 7);
|
|
|
|
|
2010-11-20 00:24:50 +00:00
|
|
|
if (parts == 0) {
|
|
|
|
/* We always have to draw at least one sprite to make sure there is a boundingbox and a sprite with the
|
|
|
|
* correct offset for the childsprites.
|
|
|
|
* So, draw the (completely empty) sprite of the default foundations. */
|
|
|
|
goto draw_default_foundation;
|
|
|
|
}
|
|
|
|
|
2010-01-06 20:23:06 +00:00
|
|
|
StartSpriteCombine();
|
2010-01-03 22:44:57 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
if (HasBit(parts, i)) {
|
|
|
|
AddSortableSpriteToDraw(image + i, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z);
|
|
|
|
}
|
|
|
|
}
|
2010-01-06 20:23:06 +00:00
|
|
|
EndSpriteCombine();
|
2010-01-03 22:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OffsetGroundSprite(31, 1);
|
|
|
|
ti->z += ApplyFoundationToSlope(FOUNDATION_LEVELED, &ti->tileh);
|
|
|
|
} else {
|
2010-11-20 00:24:50 +00:00
|
|
|
draw_default_foundation:
|
2010-01-03 22:44:57 +00:00
|
|
|
DrawFoundation(ti, FOUNDATION_LEVELED);
|
|
|
|
}
|
|
|
|
}
|
2008-02-06 16:12:23 +00:00
|
|
|
|
2010-09-05 13:18:54 +00:00
|
|
|
if (IsBuoy(ti->tile) || IsDock(ti->tile) || (IsOilRig(ti->tile) && IsTileOnWater(ti->tile))) {
|
2008-02-06 16:12:23 +00:00
|
|
|
if (ti->tileh == SLOPE_FLAT) {
|
|
|
|
DrawWaterClassGround(ti);
|
|
|
|
} else {
|
|
|
|
assert(IsDock(ti->tile));
|
|
|
|
TileIndex water_tile = ti->tile + TileOffsByDiagDir(GetDockDirection(ti->tile));
|
|
|
|
WaterClass wc = GetWaterClass(water_tile);
|
|
|
|
if (wc == WATER_CLASS_SEA) {
|
|
|
|
DrawShoreTile(ti->tileh);
|
|
|
|
} else {
|
|
|
|
DrawClearLandTile(ti, 3);
|
|
|
|
}
|
|
|
|
}
|
2006-05-07 10:58:53 +00:00
|
|
|
} else {
|
2008-02-15 18:40:42 +00:00
|
|
|
SpriteID image = t->ground.sprite;
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal = t->ground.pal;
|
2010-02-07 22:22:54 +00:00
|
|
|
if (rti != NULL && rti->UsesOverlay() && (image == SPR_RAIL_TRACK_X || image == SPR_RAIL_TRACK_Y)) {
|
|
|
|
SpriteID ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND);
|
|
|
|
DrawGroundSprite(SPR_FLAT_GRASS_TILE, PAL_NONE);
|
|
|
|
DrawGroundSprite(ground + (image == SPR_RAIL_TRACK_X ? RTO_X : RTO_Y), PAL_NONE);
|
|
|
|
|
|
|
|
if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasStationReservation(ti->tile)) {
|
|
|
|
SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
|
|
|
|
DrawGroundSprite(overlay + (image == SPR_RAIL_TRACK_X ? RTO_X : RTO_Y), PALETTE_CRASH);
|
|
|
|
}
|
2008-02-06 16:12:23 +00:00
|
|
|
} else {
|
2010-02-07 22:22:54 +00:00
|
|
|
if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
|
|
|
|
image += GetCustomStationGroundRelocation(statspec, st, ti->tile);
|
|
|
|
image += custom_ground_offset;
|
|
|
|
} else {
|
|
|
|
image += total_offset;
|
|
|
|
}
|
|
|
|
DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
|
2008-08-02 22:47:34 +00:00
|
|
|
|
2010-02-07 22:22:54 +00:00
|
|
|
/* PBS debugging, draw reserved tracks darker */
|
|
|
|
if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasStationRail(ti->tile) && HasStationReservation(ti->tile)) {
|
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
|
|
|
|
DrawGroundSprite(GetRailStationAxis(ti->tile) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
|
|
|
|
}
|
2008-08-02 22:47:34 +00:00
|
|
|
}
|
2006-05-07 10:58:53 +00:00
|
|
|
}
|
2004-11-17 18:03:33 +00:00
|
|
|
|
2009-07-24 11:47:12 +00:00
|
|
|
if (HasStationRail(ti->tile) && HasCatenaryDrawn(GetRailType(ti->tile)) && IsStationTileElectrifiable(ti->tile)) DrawCatenary(ti);
|
2006-03-29 16:30:26 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(roadtypes, ROADTYPE_TRAM)) {
|
2007-05-25 22:07:40 +00:00
|
|
|
Axis axis = GetRoadStopDir(ti->tile) == DIAGDIR_NE ? AXIS_X : AXIS_Y;
|
2007-11-19 21:02:30 +00:00
|
|
|
DrawGroundSprite((HasBit(roadtypes, ROADTYPE_ROAD) ? SPR_TRAMWAY_OVERLAY : SPR_TRAMWAY_TRAM) + (axis ^ 1), PAL_NONE);
|
2007-05-25 22:07:40 +00:00
|
|
|
DrawTramCatenary(ti, axis == AXIS_X ? ROAD_X : ROAD_Y);
|
|
|
|
}
|
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
if (IsRailWaypoint(ti->tile)) {
|
|
|
|
/* Don't offset the waypoint graphics; they're always the same. */
|
|
|
|
total_offset = 0;
|
|
|
|
}
|
|
|
|
|
2010-01-17 15:05:25 +00:00
|
|
|
DrawRailTileSeq(ti, t, TO_BUILDINGS, total_offset, relocation, palette);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-16 23:55:22 +00:00
|
|
|
void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-12-31 11:13:51 +00:00
|
|
|
int32 total_offset = 0;
|
2010-01-21 01:38:13 +00:00
|
|
|
PaletteID pal = COMPANY_SPRITE_COLOUR(_local_company);
|
2010-05-18 20:49:53 +00:00
|
|
|
const DrawTileSprites *t = GetStationTileLayout(st, image);
|
2010-02-07 22:22:54 +00:00
|
|
|
const RailtypeInfo *rti = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-12-31 11:13:51 +00:00
|
|
|
if (railtype != INVALID_RAILTYPE) {
|
2010-02-07 22:22:54 +00:00
|
|
|
rti = GetRailTypeInfo(railtype);
|
2007-12-31 11:13:51 +00:00
|
|
|
total_offset = rti->total_offset;
|
|
|
|
}
|
|
|
|
|
2008-02-15 18:40:42 +00:00
|
|
|
SpriteID img = t->ground.sprite;
|
2010-02-07 22:22:54 +00:00
|
|
|
if ((img == SPR_RAIL_TRACK_X || img == SPR_RAIL_TRACK_Y) && rti->UsesOverlay()) {
|
|
|
|
SpriteID ground = GetCustomRailSprite(rti, INVALID_TILE, RTSG_GROUND);
|
|
|
|
DrawSprite(SPR_FLAT_GRASS_TILE, PAL_NONE, x, y);
|
|
|
|
DrawSprite(ground + (img == SPR_RAIL_TRACK_X ? RTO_X : RTO_Y), PAL_NONE, x, y);
|
|
|
|
} else {
|
|
|
|
DrawSprite(img + total_offset, HasBit(img, PALETTE_MODIFIER_COLOUR) ? pal : PAL_NONE, x, y);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-25 22:07:40 +00:00
|
|
|
if (roadtype == ROADTYPE_TRAM) {
|
2008-02-15 18:40:42 +00:00
|
|
|
DrawSprite(SPR_TRAMWAY_TRAM + (t->ground.sprite == SPR_ROAD_PAVED_STRAIGHT_X ? 1 : 0), PAL_NONE, x, y);
|
2007-05-25 22:07:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 00:51:18 +00:00
|
|
|
/* Default waypoint has no railtype specific sprites */
|
2010-01-17 15:05:25 +00:00
|
|
|
DrawRailTileSeqInGUI(x, y, t, st == STATION_WAYPOINT ? 0 : total_offset, 0, pal);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-06 16:32:49 +00:00
|
|
|
static uint GetSlopeZ_Station(TileIndex tile, uint x, uint y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-06 16:32:49 +00:00
|
|
|
return GetTileMaxZ(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-26 16:51:10 +00:00
|
|
|
static Foundation GetFoundation_Station(TileIndex tile, Slope tileh)
|
2004-08-13 18:27:33 +00:00
|
|
|
{
|
2007-07-26 16:51:10 +00:00
|
|
|
return FlatteningFoundation(tileh);
|
2004-08-13 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void GetTileDesc_Station(TileIndex tile, TileDesc *td)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-21 22:15:39 +00:00
|
|
|
td->owner[0] = GetTileOwner(tile);
|
2009-03-02 22:57:47 +00:00
|
|
|
if (IsDriveThroughStopTile(tile)) {
|
|
|
|
Owner road_owner = INVALID_OWNER;
|
|
|
|
Owner tram_owner = INVALID_OWNER;
|
|
|
|
RoadTypes rts = GetRoadTypes(tile);
|
|
|
|
if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
|
|
|
|
if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
|
|
|
|
|
|
|
|
/* Is there a mix of owners? */
|
|
|
|
if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
|
|
|
|
(road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
|
|
|
|
uint i = 1;
|
|
|
|
if (road_owner != INVALID_OWNER) {
|
2009-04-21 23:40:56 +00:00
|
|
|
td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
|
2009-03-02 22:57:47 +00:00
|
|
|
td->owner[i] = road_owner;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (tram_owner != INVALID_OWNER) {
|
2009-04-21 23:40:56 +00:00
|
|
|
td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
|
2009-03-02 22:57:47 +00:00
|
|
|
td->owner[i] = tram_owner;
|
|
|
|
}
|
|
|
|
}
|
2008-05-21 22:15:39 +00:00
|
|
|
}
|
2009-07-22 08:59:57 +00:00
|
|
|
td->build_date = BaseStation::GetByTile(tile)->build_date;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-23 16:38:09 +00:00
|
|
|
if (HasStationTileRail(tile)) {
|
|
|
|
const StationSpec *spec = GetStationSpec(tile);
|
2008-07-25 22:37:34 +00:00
|
|
|
|
2010-01-23 16:38:09 +00:00
|
|
|
if (spec != NULL) {
|
2010-08-07 22:07:07 +00:00
|
|
|
td->station_class = StationClass::GetName(spec->cls_id);
|
2010-01-23 16:38:09 +00:00
|
|
|
td->station_name = spec->name;
|
2008-07-25 22:37:34 +00:00
|
|
|
|
2010-08-07 20:52:45 +00:00
|
|
|
if (spec->grf_prop.grffile != NULL) {
|
|
|
|
const GRFConfig *gc = GetGRFConfig(spec->grf_prop.grffile->grfid);
|
2010-02-28 20:28:08 +00:00
|
|
|
td->grf = gc->GetName();
|
2010-01-23 16:38:09 +00:00
|
|
|
}
|
2008-07-25 22:37:34 +00:00
|
|
|
}
|
2010-03-16 06:43:41 +00:00
|
|
|
|
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
|
|
|
|
td->rail_speed = rti->max_speed;
|
2008-07-25 22:37:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 14:17:17 +00:00
|
|
|
if (IsAirport(tile)) {
|
2010-09-01 23:14:15 +00:00
|
|
|
const AirportSpec *as = Station::GetByTile(tile)->airport.GetSpec();
|
|
|
|
td->airport_class = AirportClass::GetName(as->cls_id);
|
|
|
|
td->airport_name = as->name;
|
|
|
|
|
2010-08-26 16:53:06 +00:00
|
|
|
const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
|
2010-02-22 14:17:17 +00:00
|
|
|
td->airport_tile_name = ats->name;
|
|
|
|
|
2010-09-01 23:14:15 +00:00
|
|
|
if (as->grf_prop.grffile != NULL) {
|
|
|
|
const GRFConfig *gc = GetGRFConfig(as->grf_prop.grffile->grfid);
|
|
|
|
td->grf = gc->GetName();
|
|
|
|
} else if (ats->grf_prop.grffile != NULL) {
|
2010-02-22 14:17:17 +00:00
|
|
|
const GRFConfig *gc = GetGRFConfig(ats->grf_prop.grffile->grfid);
|
2010-02-28 20:28:08 +00:00
|
|
|
td->grf = gc->GetName();
|
2010-02-22 14:17:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
StringID str;
|
2006-03-26 14:41:39 +00:00
|
|
|
switch (GetStationType(tile)) {
|
|
|
|
default: NOT_REACHED();
|
2009-08-05 17:59:21 +00:00
|
|
|
case STATION_RAIL: str = STR_LAI_STATION_DESCRIPTION_RAILROAD_STATION; break;
|
2006-05-21 12:01:57 +00:00
|
|
|
case STATION_AIRPORT:
|
2009-08-05 17:59:21 +00:00
|
|
|
str = (IsHangar(tile) ? STR_LAI_STATION_DESCRIPTION_AIRCRAFT_HANGAR : STR_LAI_STATION_DESCRIPTION_AIRPORT);
|
2006-05-21 12:01:57 +00:00
|
|
|
break;
|
2009-08-05 17:59:21 +00:00
|
|
|
case STATION_TRUCK: str = STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA; break;
|
|
|
|
case STATION_BUS: str = STR_LAI_STATION_DESCRIPTION_BUS_STATION; break;
|
2009-07-22 08:59:57 +00:00
|
|
|
case STATION_OILRIG: str = STR_INDUSTRY_NAME_OIL_RIG; break;
|
2009-08-05 17:59:21 +00:00
|
|
|
case STATION_DOCK: str = STR_LAI_STATION_DESCRIPTION_SHIP_DOCK; break;
|
|
|
|
case STATION_BUOY: str = STR_LAI_STATION_DESCRIPTION_BUOY; break;
|
|
|
|
case STATION_WAYPOINT: str = STR_LAI_STATION_DESCRIPTION_WAYPOINT; break;
|
2006-03-26 14:41:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
td->str = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
static TrackStatus GetTileTrackStatus_Station(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2008-02-20 17:49:50 +00:00
|
|
|
TrackBits trackbits = TRACK_BIT_NONE;
|
|
|
|
|
2005-01-16 09:51:56 +00:00
|
|
|
switch (mode) {
|
|
|
|
case TRANSPORT_RAIL:
|
2009-07-24 11:47:12 +00:00
|
|
|
if (HasStationRail(tile) && !IsStationTileBlocked(tile)) {
|
2008-02-20 17:49:50 +00:00
|
|
|
trackbits = TrackToTrackBits(GetRailStationTrack(tile));
|
2005-01-16 09:51:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRANSPORT_WATER:
|
2007-10-26 20:42:03 +00:00
|
|
|
/* buoy is coded as a station, it is always on open water */
|
2007-02-02 20:20:56 +00:00
|
|
|
if (IsBuoy(tile)) {
|
2008-02-20 17:49:50 +00:00
|
|
|
trackbits = TRACK_BIT_ALL;
|
2007-10-26 20:42:03 +00:00
|
|
|
/* remove tracks that connect NE map edge */
|
2008-02-20 17:49:50 +00:00
|
|
|
if (TileX(tile) == 0) trackbits &= ~(TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT);
|
2007-10-26 20:42:03 +00:00
|
|
|
/* remove tracks that connect NW map edge */
|
2008-02-20 17:49:50 +00:00
|
|
|
if (TileY(tile) == 0) trackbits &= ~(TRACK_BIT_Y | TRACK_BIT_LEFT | TRACK_BIT_UPPER);
|
2007-02-02 20:20:56 +00:00
|
|
|
}
|
2005-01-16 09:51:56 +00:00
|
|
|
break;
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
case TRANSPORT_ROAD:
|
2008-02-18 16:11:31 +00:00
|
|
|
if ((GetRoadTypes(tile) & sub_mode) != 0 && IsRoadStop(tile)) {
|
|
|
|
DiagDirection dir = GetRoadStopDir(tile);
|
|
|
|
Axis axis = DiagDirToAxis(dir);
|
|
|
|
|
|
|
|
if (side != INVALID_DIAGDIR) {
|
2008-02-20 17:49:50 +00:00
|
|
|
if (axis != DiagDirToAxis(side) || (IsStandardRoadStopTile(tile) && dir != side)) break;
|
2008-02-18 16:11:31 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
trackbits = AxisToTrackBits(axis);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
break;
|
|
|
|
|
2005-01-16 09:51:56 +00:00
|
|
|
default:
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-16 09:51:56 +00:00
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
return CombineTrackStatus(TrackBitsToTrackdirBits(trackbits), TRACKDIR_BIT_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 12:29:37 +00:00
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void TileLoop_Station(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-10-26 20:42:03 +00:00
|
|
|
/* FIXME -- GetTileTrackStatus_Station -> animated stationtiles
|
|
|
|
* hardcoded.....not good */
|
2007-07-16 23:55:22 +00:00
|
|
|
switch (GetStationType(tile)) {
|
|
|
|
case STATION_AIRPORT:
|
2010-02-22 14:16:57 +00:00
|
|
|
AirportTileAnimationTrigger(Station::GetByTile(tile), tile, AAT_TILELOOP);
|
2005-11-16 12:29:37 +00:00
|
|
|
break;
|
2004-08-13 19:52:45 +00:00
|
|
|
|
2008-02-06 16:19:28 +00:00
|
|
|
case STATION_DOCK:
|
|
|
|
if (GetTileSlope(tile, NULL) != SLOPE_FLAT) break; // only handle water part
|
2010-08-01 20:52:11 +00:00
|
|
|
/* FALL THROUGH */
|
2007-07-16 23:55:22 +00:00
|
|
|
case STATION_OILRIG: //(station part)
|
|
|
|
case STATION_BUOY:
|
2005-11-16 12:29:37 +00:00
|
|
|
TileLoop_Water(tile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void AnimateTile_Station(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-24 11:47:12 +00:00
|
|
|
if (HasStationRail(tile)) {
|
2008-04-19 23:19:12 +00:00
|
|
|
AnimateStationTile(tile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-17 14:22:07 +00:00
|
|
|
if (IsAirport(tile)) {
|
2010-03-18 15:20:21 +00:00
|
|
|
AnimateAirportTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-18 14:41:24 +00:00
|
|
|
|
2009-01-02 22:42:05 +00:00
|
|
|
static bool ClickTile_Station(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-03-19 11:17:52 +00:00
|
|
|
const BaseStation *bst = BaseStation::GetByTile(tile);
|
2009-07-22 08:59:57 +00:00
|
|
|
|
2010-03-19 11:17:52 +00:00
|
|
|
if (bst->facilities & FACIL_WAYPOINT) {
|
|
|
|
ShowWaypointWindow(Waypoint::From(bst));
|
2009-07-22 08:59:57 +00:00
|
|
|
} else if (IsHangar(tile)) {
|
2010-03-19 11:17:52 +00:00
|
|
|
const Station *st = Station::From(bst);
|
|
|
|
ShowDepotWindow(st->airport.GetHangarTile(st->airport.GetHangarNum(tile)), VEH_AIRCRAFT);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2010-03-19 11:17:52 +00:00
|
|
|
ShowStationViewWindow(bst->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-01-02 22:42:05 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-12-21 22:50:51 +00:00
|
|
|
static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_TRAIN) {
|
2009-12-02 23:53:15 +00:00
|
|
|
StationID station_id = GetStationIndex(tile);
|
2008-04-13 16:54:19 +00:00
|
|
|
if (!v->current_order.ShouldStopAtStation(v, station_id)) return VETSB_CONTINUE;
|
2011-01-29 17:30:25 +00:00
|
|
|
if (!IsRailStation(tile) || !v->IsFrontEngine()) return VETSB_CONTINUE;
|
2009-04-12 14:11:14 +00:00
|
|
|
|
|
|
|
int station_ahead;
|
|
|
|
int station_length;
|
2009-06-06 16:54:22 +00:00
|
|
|
int stop = GetTrainStopLocation(station_id, tile, Train::From(v), &station_ahead, &station_length);
|
2009-04-12 14:11:14 +00:00
|
|
|
|
|
|
|
/* Stop whenever that amount of station ahead + the distance from the
|
|
|
|
* begin of the platform to the stop location is longer than the length
|
|
|
|
* of the platform. Station ahead 'includes' the current tile where the
|
|
|
|
* vehicle is on, so we need to substract that. */
|
2009-04-13 22:27:21 +00:00
|
|
|
if (!IsInsideBS(stop + station_ahead, station_length, TILE_SIZE)) return VETSB_CONTINUE;
|
2009-04-12 14:11:14 +00:00
|
|
|
|
|
|
|
DiagDirection dir = DirToDiagDir(v->direction);
|
|
|
|
|
|
|
|
x &= 0xF;
|
|
|
|
y &= 0xF;
|
|
|
|
|
|
|
|
if (DiagDirToAxis(dir) != AXIS_X) Swap(x, y);
|
|
|
|
if (y == TILE_SIZE / 2) {
|
|
|
|
if (dir != DIAGDIR_SE && dir != DIAGDIR_SW) x = TILE_SIZE - 1 - x;
|
|
|
|
stop &= TILE_SIZE - 1;
|
|
|
|
|
|
|
|
if (x == stop) return VETSB_ENTERED_STATION | (VehicleEnterTileStatus)(station_id << VETS_STATION_ID_OFFSET); // enter station
|
|
|
|
if (x < stop) {
|
|
|
|
uint16 spd;
|
|
|
|
|
|
|
|
v->vehstatus |= VS_TRAIN_SLOWING;
|
|
|
|
spd = max(0, (stop - x) * 20 - 15);
|
|
|
|
if (spd < v->cur_speed) v->cur_speed = spd;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-03-08 16:27:54 +00:00
|
|
|
} else if (v->type == VEH_ROAD) {
|
2009-06-06 16:54:22 +00:00
|
|
|
RoadVehicle *rv = RoadVehicle::From(v);
|
2009-05-22 20:22:20 +00:00
|
|
|
if (rv->state < RVSB_IN_ROAD_STOP && !IsReversingRoadTrackdir((Trackdir)rv->state) && rv->frame == 0) {
|
2011-01-19 18:44:13 +00:00
|
|
|
if (IsRoadStop(tile) && rv->IsFrontEngine()) {
|
2005-11-17 10:12:21 +00:00
|
|
|
/* Attempt to allocate a parking bay in a road stop */
|
2009-12-02 23:53:15 +00:00
|
|
|
return RoadStop::GetByTile(tile, GetRoadStopType(tile))->Enter(rv) ? VETSB_CONTINUE : VETSB_CANNOT_ENTER;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-13 10:26:53 +00:00
|
|
|
return VETSB_CONTINUE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-17 10:19:00 +00:00
|
|
|
/**
|
|
|
|
* This function is called for each station once every 250 ticks.
|
|
|
|
* Not all stations will get the tick at the same time.
|
|
|
|
* @param st the station receiving the tick.
|
2009-05-27 23:11:52 +00:00
|
|
|
* @return true if the station is still valid (wasn't deleted)
|
2009-05-17 10:19:00 +00:00
|
|
|
*/
|
2009-07-22 08:59:57 +00:00
|
|
|
static bool StationHandleBigTick(BaseStation *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-24 07:38:10 +00:00
|
|
|
if (!st->IsInUse() && ++st->delete_ctr >= 8) {
|
2009-05-27 23:11:52 +00:00
|
|
|
delete st;
|
|
|
|
return false;
|
|
|
|
}
|
2005-01-30 22:04:14 +00:00
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
if ((st->facilities & FACIL_WAYPOINT) == 0) UpdateStationAcceptance(Station::From(st), true);
|
|
|
|
|
2009-05-27 23:11:52 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-17 20:03:28 +00:00
|
|
|
static inline void byte_inc_sat(byte *p)
|
|
|
|
{
|
|
|
|
byte b = *p + 1;
|
|
|
|
if (b != 0) *p = b;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void UpdateStationRating(Station *st)
|
|
|
|
{
|
|
|
|
bool waiting_changed = false;
|
|
|
|
|
|
|
|
byte_inc_sat(&st->time_since_load);
|
|
|
|
byte_inc_sat(&st->time_since_unload);
|
|
|
|
|
2009-09-15 17:24:18 +00:00
|
|
|
const CargoSpec *cs;
|
|
|
|
FOR_ALL_CARGOSPECS(cs) {
|
|
|
|
GoodsEntry *ge = &st->goods[cs->Index()];
|
2007-03-08 20:50:27 +00:00
|
|
|
/* Slowly increase the rating back to his original level in the case we
|
|
|
|
* didn't deliver cargo yet to this station. This happens when a bribe
|
|
|
|
* failed while you didn't moved that cargo yet to a station. */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP) && ge->rating < INITIAL_STATION_RATING) {
|
2007-03-08 20:50:27 +00:00
|
|
|
ge->rating++;
|
2007-08-26 13:55:36 +00:00
|
|
|
}
|
|
|
|
|
2007-03-08 20:50:27 +00:00
|
|
|
/* Only change the rating if we are moving this cargo */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte_inc_sat(&ge->days_since_pickup);
|
|
|
|
|
2009-09-15 17:24:18 +00:00
|
|
|
bool skip = false;
|
2007-02-18 11:27:09 +00:00
|
|
|
int rating = 0;
|
2009-09-15 17:24:18 +00:00
|
|
|
uint waiting = ge->cargo.Count();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-15 17:24:18 +00:00
|
|
|
if (HasBit(cs->callback_mask, CBM_CARGO_STATION_RATING_CALC)) {
|
|
|
|
/* Perform custom station rating. If it succeeds the speed, days in transit and
|
|
|
|
* waiting cargo ratings must not be executed. */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-15 17:24:18 +00:00
|
|
|
/* NewGRFs expect last speed to be 0xFF when no vehicle has arrived yet. */
|
|
|
|
uint last_speed = ge->last_speed;
|
|
|
|
if (last_speed == 0) last_speed = 0xFF;
|
|
|
|
|
|
|
|
uint32 var18 = min(ge->days_since_pickup, 0xFF) | (min(waiting, 0xFFFF) << 8) | (min(last_speed, 0xFF) << 24);
|
|
|
|
/* Convert to the 'old' vehicle types */
|
|
|
|
uint32 var10 = (st->last_vehicle_type == VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10);
|
|
|
|
uint16 callback = GetCargoCallback(CBID_CARGO_STATION_RATING_CALC, var10, var18, cs);
|
|
|
|
if (callback != CALLBACK_FAILED) {
|
|
|
|
skip = true;
|
|
|
|
rating = GB(callback, 0, 14);
|
|
|
|
|
|
|
|
/* Simulate a 15 bit signed value */
|
|
|
|
if (HasBit(callback, 14)) rating -= 0x4000;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-15 17:24:18 +00:00
|
|
|
if (!skip) {
|
|
|
|
int b = ge->last_speed - 85;
|
|
|
|
if (b >= 0) rating += b >> 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
byte days = ge->days_since_pickup;
|
2007-10-26 20:42:03 +00:00
|
|
|
if (st->last_vehicle_type == VEH_SHIP) days >>= 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
(days > 21) ||
|
|
|
|
(rating += 25, days > 12) ||
|
|
|
|
(rating += 25, days > 6) ||
|
|
|
|
(rating += 45, days > 3) ||
|
|
|
|
(rating += 35, true);
|
2009-09-15 17:24:18 +00:00
|
|
|
|
|
|
|
(rating -= 90, waiting > 1500) ||
|
|
|
|
(rating += 55, waiting > 1000) ||
|
|
|
|
(rating += 35, waiting > 600) ||
|
|
|
|
(rating += 10, waiting > 300) ||
|
|
|
|
(rating += 20, waiting > 100) ||
|
|
|
|
(rating += 10, true);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-09-15 17:24:18 +00:00
|
|
|
if (Company::IsValidID(st->owner) && HasBit(st->town->statues, st->owner)) rating += 26;
|
|
|
|
|
|
|
|
byte age = ge->last_age;
|
|
|
|
(age >= 3) ||
|
|
|
|
(rating += 10, age >= 2) ||
|
|
|
|
(rating += 10, age >= 1) ||
|
|
|
|
(rating += 13, true);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
int or_ = ge->rating; // old rating
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* only modify rating in steps of -2, -1, 0, 1 or 2 */
|
2007-11-19 18:38:10 +00:00
|
|
|
ge->rating = rating = or_ + Clamp(Clamp(rating, 0, 255) - or_, -2, 2);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* if rating is <= 64 and more than 200 items waiting,
|
|
|
|
* remove some random amount of goods from the station */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (rating <= 64 && waiting >= 200) {
|
|
|
|
int dec = Random() & 0x1F;
|
|
|
|
if (waiting < 400) dec &= 7;
|
|
|
|
waiting -= dec + 1;
|
|
|
|
waiting_changed = true;
|
|
|
|
}
|
|
|
|
|
2007-10-26 20:42:03 +00:00
|
|
|
/* if rating is <= 127 and there are any items waiting, maybe remove some goods. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (rating <= 127 && waiting != 0) {
|
|
|
|
uint32 r = Random();
|
2007-01-14 18:38:40 +00:00
|
|
|
if (rating <= (int)GB(r, 0, 7)) {
|
2007-06-22 11:58:59 +00:00
|
|
|
/* Need to have int, otherwise it will just overflow etc. */
|
|
|
|
waiting = max((int)waiting - (int)GB(r, 8, 2) - 1, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
waiting_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-13 20:46:53 +00:00
|
|
|
/* At some point we really must cap the cargo. Previously this
|
|
|
|
* was a strict 4095, but now we'll have a less strict, but
|
|
|
|
* increasingly agressive truncation of the amount of cargo. */
|
|
|
|
static const uint WAITING_CARGO_THRESHOLD = 1 << 12;
|
|
|
|
static const uint WAITING_CARGO_CUT_FACTOR = 1 << 6;
|
|
|
|
static const uint MAX_WAITING_CARGO = 1 << 15;
|
|
|
|
|
|
|
|
if (waiting > WAITING_CARGO_THRESHOLD) {
|
|
|
|
uint difference = waiting - WAITING_CARGO_THRESHOLD;
|
|
|
|
waiting -= (difference / WAITING_CARGO_CUT_FACTOR);
|
|
|
|
|
|
|
|
waiting = min(waiting, MAX_WAITING_CARGO);
|
|
|
|
waiting_changed = true;
|
|
|
|
}
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
if (waiting_changed) ge->cargo.Truncate(waiting);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-15 17:24:18 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
StationID index = st->index;
|
2006-06-27 21:25:53 +00:00
|
|
|
if (waiting_changed) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_STATION_VIEW, index); // update whole window
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_STATION_VIEW, index, SVW_RATINGLIST); // update only ratings list
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called for every station each tick */
|
2009-07-22 08:59:57 +00:00
|
|
|
static void StationHandleSmallTick(BaseStation *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-24 07:38:10 +00:00
|
|
|
if ((st->facilities & FACIL_WAYPOINT) != 0 || !st->IsInUse()) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-18 11:27:09 +00:00
|
|
|
byte b = st->delete_ctr + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (b >= 185) b = 0;
|
|
|
|
st->delete_ctr = b;
|
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
if (b == 0) UpdateStationRating(Station::From(st));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void OnTick_Station()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_game_mode == GM_EDITOR) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
BaseStation *st;
|
|
|
|
FOR_ALL_BASE_STATIONS(st) {
|
2008-04-19 23:19:12 +00:00
|
|
|
StationHandleSmallTick(st);
|
|
|
|
|
|
|
|
/* Run 250 tick interval trigger for station animation.
|
|
|
|
* Station index is included so that triggers are not all done
|
|
|
|
* at the same time. */
|
|
|
|
if ((_tick_counter + st->index) % 250 == 0) {
|
2009-05-27 23:11:52 +00:00
|
|
|
/* Stop processing this station if it was deleted */
|
|
|
|
if (!StationHandleBigTick(st)) continue;
|
2010-08-26 15:31:40 +00:00
|
|
|
TriggerStationAnimation(st, st->xy, SAT_250_TICKS);
|
2010-08-26 17:08:37 +00:00
|
|
|
if (Station::IsExpected(st)) AirportAnimationTrigger(Station::From(st), AAT_STATION_250_TICKS);
|
2008-04-19 23:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void StationMonthlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-17 20:03:28 +00:00
|
|
|
/* not used */
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
void ModifyStationRatingAround(TileIndex tile, Owner owner, int amount, uint radius)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2006-08-22 15:33:35 +00:00
|
|
|
if (st->owner == owner &&
|
2005-01-31 07:23:15 +00:00
|
|
|
DistanceManhattan(tile, st->xy) <= radius) {
|
2007-03-21 13:19:01 +00:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2008-04-17 20:03:28 +00:00
|
|
|
GoodsEntry *ge = &st->goods[i];
|
2005-10-23 13:04:44 +00:00
|
|
|
|
2007-08-26 13:55:36 +00:00
|
|
|
if (ge->acceptance_pickup != 0) {
|
2007-11-19 18:38:10 +00:00
|
|
|
ge->rating = Clamp(ge->rating + amount, 0, 255);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-28 22:00:24 +00:00
|
|
|
static uint UpdateStationWaiting(Station *st, CargoID type, uint amount, SourceType source_type, SourceID source_id)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-11-11 09:12:01 +00:00
|
|
|
GoodsEntry &ge = st->goods[type];
|
|
|
|
amount += ge.amount_fract;
|
|
|
|
ge.amount_fract = GB(amount, 0, 8);
|
2010-09-28 22:00:24 +00:00
|
|
|
|
|
|
|
amount >>= 8;
|
|
|
|
/* No new "real" cargo item yet. */
|
|
|
|
if (amount == 0) return 0;
|
|
|
|
|
2010-11-11 09:12:01 +00:00
|
|
|
ge.cargo.Append(new CargoPacket(st->index, st->xy, amount, source_type, source_id));
|
2010-11-11 10:34:44 +00:00
|
|
|
|
|
|
|
if (!HasBit(ge.acceptance_pickup, GoodsEntry::PICKUP)) {
|
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->index);
|
|
|
|
SetBit(ge.acceptance_pickup, GoodsEntry::PICKUP);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-26 15:31:40 +00:00
|
|
|
TriggerStationAnimation(st, st->xy, SAT_NEW_CARGO, type);
|
2010-02-22 14:16:57 +00:00
|
|
|
AirportAnimationTrigger(st, AAT_STATION_NEW_CARGO, type);
|
2008-04-19 23:19:12 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_STATION_VIEW, st->index);
|
2007-06-08 09:35:39 +00:00
|
|
|
st->MarkTilesDirty(true);
|
2010-09-28 22:00:24 +00:00
|
|
|
return amount;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 20:53:25 +00:00
|
|
|
static bool IsUniqueStationName(const char *name)
|
|
|
|
{
|
|
|
|
const Station *st;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2009-01-10 15:54:07 +00:00
|
|
|
if (st->name != NULL && strcmp(st->name, name) == 0) return false;
|
2007-06-27 20:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Rename a station
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 station ID that is to be renamed
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRenameStation(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Station *st = Station::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (st == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(st->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2005-05-07 08:14:06 +00:00
|
|
|
|
2008-12-28 14:37:19 +00:00
|
|
|
bool reset = StrEmpty(text);
|
2008-09-15 19:02:50 +00:00
|
|
|
|
|
|
|
if (!reset) {
|
2010-12-05 22:25:36 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_STATION_NAME_CHARS) return CMD_ERROR;
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsUniqueStationName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
2008-09-15 19:02:50 +00:00
|
|
|
}
|
2007-06-27 20:53:25 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-01-12 19:58:06 +00:00
|
|
|
free(st->name);
|
2008-12-28 14:37:19 +00:00
|
|
|
st->name = reset ? NULL : strdup(text);
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2009-07-07 16:43:58 +00:00
|
|
|
st->UpdateVirtCoord();
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowData(WC_STATION_LIST, st->owner, 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
/**
|
2009-07-22 08:59:57 +00:00
|
|
|
* Find all stations around a rectangular producer (industry, house, headquarter, ...)
|
2008-04-17 20:03:28 +00:00
|
|
|
*
|
2010-01-04 18:16:32 +00:00
|
|
|
* @param location The location/area of the producer
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param stations The list to store the stations in
|
2008-04-17 20:03:28 +00:00
|
|
|
*/
|
2010-01-04 18:16:32 +00:00
|
|
|
void FindStationsAroundTiles(const TileArea &location, StationList *stations)
|
2007-11-15 22:20:33 +00:00
|
|
|
{
|
2008-11-22 15:48:43 +00:00
|
|
|
/* area to search = producer plus station catchment radius */
|
|
|
|
int max_rad = (_settings_game.station.modified_catchment ? MAX_CATCHMENT : CA_UNMODIFIED);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-04 18:16:32 +00:00
|
|
|
for (int dy = -max_rad; dy < location.h + max_rad; dy++) {
|
|
|
|
for (int dx = -max_rad; dx < location.w + max_rad; dx++) {
|
|
|
|
TileIndex cur_tile = TileAddWrap(location.tile, dx, dy);
|
2009-02-25 21:29:50 +00:00
|
|
|
if (cur_tile == INVALID_TILE || !IsTileType(cur_tile, MP_STATION)) continue;
|
2006-02-09 06:15:12 +00:00
|
|
|
|
2009-06-24 17:39:54 +00:00
|
|
|
Station *st = Station::GetByTile(cur_tile);
|
2009-07-22 08:59:57 +00:00
|
|
|
if (st == NULL) continue;
|
2006-02-09 06:24:53 +00:00
|
|
|
|
2009-02-25 21:29:50 +00:00
|
|
|
if (_settings_game.station.modified_catchment) {
|
|
|
|
int rad = st->GetCatchmentRadius();
|
2010-01-04 18:16:32 +00:00
|
|
|
if (dx < -rad || dx >= rad + location.w || dy < -rad || dy >= rad + location.h) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-11-15 22:20:33 +00:00
|
|
|
|
2009-02-25 21:29:50 +00:00
|
|
|
/* Insert the station in the set. This will fail if it has
|
|
|
|
* already been added.
|
|
|
|
*/
|
|
|
|
stations->Include(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-02-25 21:29:50 +00:00
|
|
|
}
|
2007-11-15 22:20:33 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-15 21:06:13 +00:00
|
|
|
/**
|
|
|
|
* Run a tile loop to find stations around a tile, on demand. Cache the result for further requests
|
|
|
|
* @return pointer to a StationList containing all stations found
|
|
|
|
*/
|
|
|
|
const StationList *StationFinder::GetStations()
|
|
|
|
{
|
|
|
|
if (this->tile != INVALID_TILE) {
|
2010-01-04 18:16:32 +00:00
|
|
|
FindStationsAroundTiles(*this, &this->stations);
|
2009-11-15 21:06:13 +00:00
|
|
|
this->tile = INVALID_TILE;
|
|
|
|
}
|
|
|
|
return &this->stations;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint MoveGoodsToStation(CargoID type, uint amount, SourceType source_type, SourceID source_id, const StationList *all_stations)
|
2007-11-15 22:20:33 +00:00
|
|
|
{
|
2009-05-10 15:42:59 +00:00
|
|
|
/* Return if nothing to do. Also the rounding below fails for 0. */
|
|
|
|
if (amount == 0) return 0;
|
|
|
|
|
2008-04-18 04:54:09 +00:00
|
|
|
Station *st1 = NULL; // Station with best rating
|
|
|
|
Station *st2 = NULL; // Second best station
|
|
|
|
uint best_rating1 = 0; // rating of st1
|
|
|
|
uint best_rating2 = 0; // rating of st2
|
2007-11-15 22:20:33 +00:00
|
|
|
|
2009-11-15 21:06:13 +00:00
|
|
|
for (Station * const *st_iter = all_stations->Begin(); st_iter != all_stations->End(); ++st_iter) {
|
2007-11-15 22:20:33 +00:00
|
|
|
Station *st = *st_iter;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
/* Is the station reserved exclusively for somebody else? */
|
|
|
|
if (st->town->exclusive_counter > 0 && st->town->exclusivity != st->owner) continue;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
if (st->goods[type].rating == 0) continue; // Lowest possible rating, better not to give cargo anymore
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.selectgoods && st->goods[type].last_speed == 0) continue; // Selectively servicing stations, and not this one
|
2007-11-15 22:20:33 +00:00
|
|
|
|
|
|
|
if (IsCargoInClass(type, CC_PASSENGERS)) {
|
|
|
|
if (st->facilities == FACIL_TRUCK_STOP) continue; // passengers are never served by just a truck stop
|
|
|
|
} else {
|
|
|
|
if (st->facilities == FACIL_BUS_STOP) continue; // non-passengers are never served by just a bus stop
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-11-15 22:20:33 +00:00
|
|
|
|
|
|
|
/* This station can be used, add it to st1/st2 */
|
|
|
|
if (st1 == NULL || st->goods[type].rating >= best_rating1) {
|
|
|
|
st2 = st1; best_rating2 = best_rating1; st1 = st; best_rating1 = st->goods[type].rating;
|
|
|
|
} else if (st2 == NULL || st->goods[type].rating >= best_rating2) {
|
|
|
|
st2 = st; best_rating2 = st->goods[type].rating;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no stations around at all? */
|
|
|
|
if (st1 == NULL) return 0;
|
|
|
|
|
2010-09-28 22:00:24 +00:00
|
|
|
/* From now we'll calculate with fractal cargo amounts.
|
|
|
|
* First determine how much cargo we really have. */
|
|
|
|
amount *= best_rating1 + 1;
|
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
if (st2 == NULL) {
|
|
|
|
/* only one station around */
|
2010-09-28 22:00:24 +00:00
|
|
|
return UpdateStationWaiting(st1, type, amount, source_type, source_id);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-11-15 22:20:33 +00:00
|
|
|
/* several stations around, the best two (highest rating) are in st1 and st2 */
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(st1 != NULL);
|
|
|
|
assert(st2 != NULL);
|
2007-11-15 22:20:33 +00:00
|
|
|
assert(best_rating1 != 0 || best_rating2 != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-09-28 22:00:24 +00:00
|
|
|
/* Then determine the amount the worst station gets. We do it this way as the
|
|
|
|
* best should get a bonus, which in this case is the rounding difference from
|
|
|
|
* this calculation. In reality that will mean the bonus will be pretty low.
|
|
|
|
* Nevertheless, the best station should always get the most cargo regardless
|
|
|
|
* of rounding issues. */
|
|
|
|
uint worst_cargo = amount * best_rating2 / (best_rating1 + best_rating2);
|
|
|
|
assert(worst_cargo <= (amount - worst_cargo));
|
|
|
|
|
|
|
|
/* And then send the cargo to the stations! */
|
|
|
|
uint moved = UpdateStationWaiting(st1, type, amount - worst_cargo, source_type, source_id);
|
|
|
|
/* These two UpdateStationWaiting's can't be in the statement as then the order
|
|
|
|
* of execution would be undefined and that could cause desyncs with callbacks. */
|
|
|
|
return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
void BuildOilRig(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-01-09 14:59:02 +00:00
|
|
|
if (!Station::CanAllocateItem()) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 0, "Can't allocate station for oilrig at 0x%X, reverting to oilrig only", tile);
|
2005-09-14 22:03:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-11-04 11:46:01 +00:00
|
|
|
|
2009-01-09 14:59:02 +00:00
|
|
|
Station *st = new Station(tile);
|
2008-04-24 18:30:41 +00:00
|
|
|
st->town = ClosestTownFromTile(tile, UINT_MAX);
|
2007-11-04 11:46:01 +00:00
|
|
|
|
2008-04-25 15:22:32 +00:00
|
|
|
st->string_id = GenerateStationName(st, tile, STATIONNAMING_OILRIG);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-07-26 16:14:10 +00:00
|
|
|
assert(IsTileType(tile, MP_INDUSTRY));
|
2009-07-01 14:51:05 +00:00
|
|
|
DeleteAnimatedTile(tile);
|
2008-07-26 16:14:10 +00:00
|
|
|
MakeOilrig(tile, st->index, GetWaterClass(tile));
|
2005-09-14 22:03:27 +00:00
|
|
|
|
|
|
|
st->owner = OWNER_NONE;
|
2010-03-18 21:02:20 +00:00
|
|
|
st->airport.type = AT_OILRIG;
|
2010-02-22 14:17:07 +00:00
|
|
|
st->airport.Add(tile);
|
2005-09-14 22:03:27 +00:00
|
|
|
st->dock_tile = tile;
|
|
|
|
st->facilities = FACIL_AIRPORT | FACIL_DOCK;
|
|
|
|
st->build_date = _date;
|
|
|
|
|
2008-10-12 10:22:13 +00:00
|
|
|
st->rect.BeforeAddTile(tile, StationRect::ADD_FORCE);
|
|
|
|
|
2007-03-21 13:19:01 +00:00
|
|
|
for (CargoID j = 0; j < NUM_CARGO; j++) {
|
2007-08-26 13:55:36 +00:00
|
|
|
st->goods[j].acceptance_pickup = 0;
|
2007-06-22 11:58:59 +00:00
|
|
|
st->goods[j].days_since_pickup = 255;
|
2007-03-08 20:50:27 +00:00
|
|
|
st->goods[j].rating = INITIAL_STATION_RATING;
|
2005-09-14 22:03:27 +00:00
|
|
|
st->goods[j].last_speed = 0;
|
|
|
|
st->goods[j].last_age = 255;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-09-14 22:03:27 +00:00
|
|
|
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2005-09-14 22:03:27 +00:00
|
|
|
UpdateStationAcceptance(st, false);
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
void DeleteOilRig(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-06-24 17:39:54 +00:00
|
|
|
Station *st = Station::GetByTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-07-26 16:14:10 +00:00
|
|
|
MakeWaterKeepingClass(tile, OWNER_NONE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-12-26 18:01:15 +00:00
|
|
|
st->dock_tile = INVALID_TILE;
|
2010-02-22 14:17:07 +00:00
|
|
|
st->airport.Clear();
|
2004-08-09 17:04:08 +00:00
|
|
|
st->facilities &= ~(FACIL_AIRPORT | FACIL_DOCK);
|
2010-03-18 21:02:20 +00:00
|
|
|
st->airport.flags = 0;
|
2008-10-12 10:22:13 +00:00
|
|
|
|
|
|
|
st->rect.AfterRemoveTile(st, tile);
|
|
|
|
|
2009-07-13 23:15:13 +00:00
|
|
|
st->UpdateVirtCoord();
|
2009-06-25 15:42:03 +00:00
|
|
|
st->RecomputeIndustriesNear();
|
2009-07-25 10:39:58 +00:00
|
|
|
if (!st->IsInUse()) delete st;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
static void ChangeTileOwner_Station(TileIndex tile, Owner old_owner, Owner new_owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-03-02 22:57:47 +00:00
|
|
|
if (IsDriveThroughStopTile(tile)) {
|
|
|
|
for (RoadType rt = ROADTYPE_ROAD; rt < ROADTYPE_END; rt++) {
|
|
|
|
/* Update all roadtypes, no matter if they are present */
|
|
|
|
if (GetRoadOwner(tile, rt) == old_owner) {
|
|
|
|
SetRoadOwner(tile, rt, new_owner == INVALID_OWNER ? OWNER_NONE : new_owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!IsTileOwner(tile, old_owner)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (new_owner != INVALID_OWNER) {
|
2009-02-26 14:07:42 +00:00
|
|
|
/* for buoys, owner of tile is owner of water, st->owner == OWNER_NONE */
|
2008-09-30 20:39:50 +00:00
|
|
|
SetTileOwner(tile, new_owner);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(WC_STATION_LIST, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2008-02-09 17:30:13 +00:00
|
|
|
if (IsDriveThroughStopTile(tile)) {
|
|
|
|
/* Remove the drive-through road stop */
|
2010-02-25 21:43:28 +00:00
|
|
|
DoCommand(tile, 1 | 1 << 8, (GetStationType(tile) == STATION_TRUCK) ? ROADSTOP_TRUCK : ROADSTOP_BUS, DC_EXEC | DC_BANKRUPT, CMD_REMOVE_ROAD_STOP);
|
2008-02-09 17:30:13 +00:00
|
|
|
assert(IsTileType(tile, MP_ROAD));
|
|
|
|
/* Change owner of tile and all roadtypes */
|
2008-09-30 20:39:50 +00:00
|
|
|
ChangeTileOwner(tile, old_owner, new_owner);
|
2007-02-23 00:17:46 +00:00
|
|
|
} else {
|
2008-02-09 15:07:31 +00:00
|
|
|
DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
|
2008-02-09 17:30:13 +00:00
|
|
|
/* Set tile owner of water under (now removed) buoy and dock to OWNER_NONE.
|
|
|
|
* Update owner of buoy if it was not removed (was in orders).
|
|
|
|
* Do not update when owned by OWNER_WATER (sea and rivers). */
|
2008-09-30 20:39:50 +00:00
|
|
|
if ((IsTileType(tile, MP_WATER) || IsBuoyTile(tile)) && IsTileOwner(tile, old_owner)) SetTileOwner(tile, OWNER_NONE);
|
2007-02-23 00:17:46 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-23 00:17:46 +00:00
|
|
|
/**
|
|
|
|
* Check if a drive-through road stop tile can be cleared.
|
|
|
|
* Road stops built on town-owned roads check the conditions
|
|
|
|
* that would allow clearing of the original road.
|
|
|
|
* @param tile road stop tile to check
|
2008-05-24 22:15:10 +00:00
|
|
|
* @param flags command flags
|
2007-02-23 00:17:46 +00:00
|
|
|
* @return true if the road can be cleared
|
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
static bool CanRemoveRoadWithStop(TileIndex tile, DoCommandFlag flags)
|
2007-02-23 00:17:46 +00:00
|
|
|
{
|
2009-12-04 19:16:52 +00:00
|
|
|
/* Yeah... water can always remove stops, right? */
|
|
|
|
if (_current_company == OWNER_WATER) return true;
|
|
|
|
|
2009-03-02 22:57:47 +00:00
|
|
|
Owner road_owner = _current_company;
|
|
|
|
Owner tram_owner = _current_company;
|
|
|
|
|
|
|
|
RoadTypes rts = GetRoadTypes(tile);
|
|
|
|
if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
|
|
|
|
if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
|
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
if ((road_owner != OWNER_TOWN && CheckOwnership(road_owner).Failed()) || CheckOwnership(tram_owner).Failed()) return false;
|
2007-02-23 00:17:46 +00:00
|
|
|
|
2010-03-13 15:53:44 +00:00
|
|
|
return road_owner != OWNER_TOWN || CheckAllowRemoveRoad(tile, GetAnyRoadBits(tile, ROADTYPE_ROAD), OWNER_TOWN, ROADTYPE_ROAD, flags).Succeeded();
|
2007-02-23 00:17:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 22:06:54 +00:00
|
|
|
CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_AUTO) {
|
2006-03-26 14:41:39 +00:00
|
|
|
switch (GetStationType(tile)) {
|
2009-07-18 10:01:31 +00:00
|
|
|
default: break;
|
2009-07-22 08:59:57 +00:00
|
|
|
case STATION_RAIL: return_cmd_error(STR_ERROR_MUST_DEMOLISH_RAILROAD);
|
|
|
|
case STATION_WAYPOINT: return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
|
|
|
|
case STATION_AIRPORT: return_cmd_error(STR_ERROR_MUST_DEMOLISH_AIRPORT_FIRST);
|
|
|
|
case STATION_TRUCK: return_cmd_error(HasTileRoadType(tile, ROADTYPE_TRAM) ? STR_ERROR_MUST_DEMOLISH_CARGO_TRAM_STATION_FIRST : STR_ERROR_MUST_DEMOLISH_TRUCK_STATION_FIRST);
|
|
|
|
case STATION_BUS: return_cmd_error(HasTileRoadType(tile, ROADTYPE_TRAM) ? STR_ERROR_MUST_DEMOLISH_PASSENGER_TRAM_STATION_FIRST : STR_ERROR_MUST_DEMOLISH_BUS_STATION_FIRST);
|
|
|
|
case STATION_BUOY: return_cmd_error(STR_ERROR_BUOY_IN_THE_WAY);
|
|
|
|
case STATION_DOCK: return_cmd_error(STR_ERROR_MUST_DEMOLISH_DOCK_FIRST);
|
2006-03-26 14:41:39 +00:00
|
|
|
case STATION_OILRIG:
|
2010-02-26 10:08:16 +00:00
|
|
|
SetDParam(1, STR_INDUSTRY_NAME_OIL_RIG);
|
2010-08-08 10:59:30 +00:00
|
|
|
return_cmd_error(STR_ERROR_GENERIC_OBJECT_IN_THE_WAY);
|
2006-03-26 14:41:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-26 14:41:39 +00:00
|
|
|
switch (GetStationType(tile)) {
|
2009-07-29 22:32:20 +00:00
|
|
|
case STATION_RAIL: return RemoveRailStation(tile, flags);
|
|
|
|
case STATION_WAYPOINT: return RemoveRailWaypoint(tile, flags);
|
2009-07-22 08:59:57 +00:00
|
|
|
case STATION_AIRPORT: return RemoveAirport(tile, flags);
|
2006-03-26 14:41:39 +00:00
|
|
|
case STATION_TRUCK:
|
2010-03-13 15:42:36 +00:00
|
|
|
if (IsDriveThroughStopTile(tile) && !CanRemoveRoadWithStop(tile, flags)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_MUST_DEMOLISH_TRUCK_STATION_FIRST);
|
2010-03-13 15:42:36 +00:00
|
|
|
}
|
2009-07-10 22:46:30 +00:00
|
|
|
return RemoveRoadStop(tile, flags);
|
2007-02-14 16:37:16 +00:00
|
|
|
case STATION_BUS:
|
2010-03-13 15:42:36 +00:00
|
|
|
if (IsDriveThroughStopTile(tile) && !CanRemoveRoadWithStop(tile, flags)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_MUST_DEMOLISH_BUS_STATION_FIRST);
|
2010-03-13 15:42:36 +00:00
|
|
|
}
|
2009-07-10 22:46:30 +00:00
|
|
|
return RemoveRoadStop(tile, flags);
|
2009-07-22 08:59:57 +00:00
|
|
|
case STATION_BUOY: return RemoveBuoy(tile, flags);
|
|
|
|
case STATION_DOCK: return RemoveDock(tile, flags);
|
2006-03-26 14:41:39 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:20:05 +00:00
|
|
|
static CommandCost TerraformTile_Station(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
|
2007-08-30 17:17:04 +00:00
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) {
|
2007-09-14 22:27:40 +00:00
|
|
|
/* TODO: If you implement newgrf callback 149 'land slope check', you have to decide what to do with it here.
|
|
|
|
* TTDP does not call it.
|
|
|
|
*/
|
|
|
|
if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
|
|
|
|
switch (GetStationType(tile)) {
|
2009-07-22 08:59:57 +00:00
|
|
|
case STATION_WAYPOINT:
|
2007-09-14 22:27:40 +00:00
|
|
|
case STATION_RAIL: {
|
|
|
|
DiagDirection direction = AxisToDiagDir(GetRailStationAxis(tile));
|
|
|
|
if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, direction)) break;
|
|
|
|
if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, ReverseDiagDir(direction))) break;
|
2009-11-24 22:15:42 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
|
2007-09-14 22:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case STATION_AIRPORT:
|
2009-11-24 22:15:42 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
|
2007-09-14 22:27:40 +00:00
|
|
|
|
|
|
|
case STATION_TRUCK:
|
|
|
|
case STATION_BUS: {
|
|
|
|
DiagDirection direction = GetRoadStopDir(tile);
|
|
|
|
if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, direction)) break;
|
|
|
|
if (IsDriveThroughStopTile(tile)) {
|
|
|
|
if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, ReverseDiagDir(direction))) break;
|
|
|
|
}
|
2009-11-24 22:15:42 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
|
2007-09-14 22:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-30 17:17:04 +00:00
|
|
|
return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
|
2006-05-06 21:46:26 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const TileTypeProcs _tile_type_station_procs = {
|
2009-03-15 00:32:18 +00:00
|
|
|
DrawTile_Station, // draw_tile_proc
|
|
|
|
GetSlopeZ_Station, // get_slope_z_proc
|
|
|
|
ClearTile_Station, // clear_tile_proc
|
2009-06-25 20:08:59 +00:00
|
|
|
NULL, // add_accepted_cargo_proc
|
2009-03-15 00:32:18 +00:00
|
|
|
GetTileDesc_Station, // get_tile_desc_proc
|
|
|
|
GetTileTrackStatus_Station, // get_tile_track_status_proc
|
|
|
|
ClickTile_Station, // click_tile_proc
|
|
|
|
AnimateTile_Station, // animate_tile_proc
|
|
|
|
TileLoop_Station, // tile_loop_clear
|
|
|
|
ChangeTileOwner_Station, // change_tile_owner_clear
|
2009-06-27 17:05:20 +00:00
|
|
|
NULL, // add_produced_cargo_proc
|
2009-03-15 00:32:18 +00:00
|
|
|
VehicleEnter_Station, // vehicle_enter_tile_proc
|
|
|
|
GetFoundation_Station, // get_foundation_proc
|
|
|
|
TerraformTile_Station, // terraform_tile_proc
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|