2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2005-07-28 05:47:54 +00:00
|
|
|
/** @file station_cmd.c
|
|
|
|
*/
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-02-13 11:18:02 +00:00
|
|
|
#include "table/sprites.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "station.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "viewport.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "town.h"
|
|
|
|
#include "vehicle.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "saveload.h"
|
|
|
|
#include "economy.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "airport.h"
|
2004-11-14 16:42:08 +00:00
|
|
|
#include "sprite.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2005-07-04 14:58:55 +00:00
|
|
|
#include "pbs.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-03 17:22:35 +00:00
|
|
|
enum {
|
|
|
|
/* Max stations: 64000 (64 * 1000) */
|
|
|
|
STATION_POOL_BLOCK_SIZE_BITS = 6, /* In bits, so (1 << 6) == 64 */
|
|
|
|
STATION_POOL_MAX_BLOCKS = 1000,
|
2005-02-04 15:31:30 +00:00
|
|
|
|
|
|
|
/* Max roadstops: 64000 (32 * 2000) */
|
|
|
|
ROADSTOP_POOL_BLOCK_SIZE_BITS = 5, /* In bits, so (1 << 5) == 32 */
|
|
|
|
ROADSTOP_POOL_MAX_BLOCKS = 2000,
|
2005-02-03 17:22:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called if a new block is added to the station-pool
|
|
|
|
*/
|
|
|
|
static void StationPoolNewBlock(uint start_item)
|
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS_FROM(st, start_item)
|
|
|
|
st->index = start_item++;
|
|
|
|
}
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
/**
|
|
|
|
* Called if a new block is added to the roadstop-pool
|
|
|
|
*/
|
|
|
|
static void RoadStopPoolNewBlock(uint start_item)
|
|
|
|
{
|
|
|
|
RoadStop *rs;
|
|
|
|
|
|
|
|
FOR_ALL_ROADSTOPS_FROM(rs, start_item)
|
|
|
|
rs->index = start_item++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the station-pool and roadstop-pool */
|
2005-02-03 17:22:35 +00:00
|
|
|
MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, 0, 0, NULL };
|
2005-02-04 15:31:30 +00:00
|
|
|
MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, 0, 0, NULL };
|
2005-02-03 17:22:35 +00:00
|
|
|
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// FIXME -- need to be embedded into Airport variable. Is dynamically
|
|
|
|
// deducteable from graphics-tile array, so will not be needed
|
2005-03-03 19:54:46 +00:00
|
|
|
const byte _airport_size_x[] = {4, 6, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
|
|
|
|
const byte _airport_size_y[] = {3, 6, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
void ShowAircraftDepotWindow(TileIndex tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
extern void UpdateAirplanesOnNewStation(Station *st);
|
|
|
|
|
|
|
|
static void MarkStationDirty(Station *st)
|
|
|
|
{
|
|
|
|
if (st->sign.width_1 != 0) {
|
|
|
|
InvalidateWindowWidget(WC_STATION_VIEW, st->index, 1);
|
|
|
|
|
|
|
|
MarkAllViewportsDirty(
|
|
|
|
st->sign.left - 6,
|
|
|
|
st->sign.top,
|
|
|
|
st->sign.left + (st->sign.width_1 << 2) + 12,
|
|
|
|
st->sign.top + 48);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-25 10:40:58 +00:00
|
|
|
static void InitializeRoadStop(RoadStop *road_stop, RoadStop *previous, TileIndex tile, StationID index)
|
2005-01-29 19:41:44 +00:00
|
|
|
{
|
|
|
|
road_stop->xy = tile;
|
|
|
|
road_stop->used = true;
|
|
|
|
road_stop->status = 3; //stop is free
|
|
|
|
road_stop->slot[0] = road_stop->slot[1] = INVALID_SLOT;
|
|
|
|
road_stop->next = NULL;
|
|
|
|
road_stop->prev = previous;
|
|
|
|
road_stop->station = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
RoadStop * GetPrimaryRoadStop(const Station *st, RoadStopType type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case RS_BUS: return st->bus_stops;
|
|
|
|
case RS_TRUCK: return st->truck_stops;
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RoadStop * GetRoadStopByTile(TileIndex tile, RoadStopType type)
|
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
const Station *st = GetStation(_m[tile].m2);
|
2005-01-29 19:41:44 +00:00
|
|
|
RoadStop *rs;
|
|
|
|
|
|
|
|
for ( rs = GetPrimaryRoadStop(st, type); rs->xy != tile; rs = rs->next)
|
|
|
|
assert(rs->next != NULL);
|
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint GetNumRoadStops(const Station *st, RoadStopType type)
|
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
const RoadStop *rs;
|
|
|
|
|
|
|
|
assert(st != NULL);
|
|
|
|
for ( rs = GetPrimaryRoadStop(st, type); rs != NULL; num++, rs = rs->next);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
RoadStop *AllocateRoadStop( void )
|
2005-01-29 19:41:44 +00:00
|
|
|
{
|
2005-02-04 15:31:30 +00:00
|
|
|
RoadStop *rs;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
FOR_ALL_ROADSTOPS(rs) {
|
2005-01-29 19:41:44 +00:00
|
|
|
if (!rs->used) {
|
2005-02-04 15:31:30 +00:00
|
|
|
uint index = rs->index;
|
|
|
|
|
|
|
|
memset(rs, 0, sizeof(RoadStop));
|
|
|
|
rs->index = index;
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
/* Check if we can add a block to the pool */
|
|
|
|
if (AddBlockToPool(&_roadstop_pool))
|
|
|
|
return AllocateRoadStop();
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-18 13:38:27 +00:00
|
|
|
/* Calculate the radius of the station. Basicly it is the biggest
|
|
|
|
radius that is available within the station */
|
|
|
|
static byte FindCatchmentRadius(Station *st)
|
|
|
|
{
|
|
|
|
byte ret = 0;
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
if (st->bus_stops != NULL) ret = max(ret, CA_BUS);
|
|
|
|
if (st->truck_stops != NULL) ret = max(ret, CA_TRUCK);
|
2004-12-18 13:38:27 +00:00
|
|
|
if (st->train_tile) ret = max(ret, CA_TRAIN);
|
|
|
|
if (st->dock_tile) ret = max(ret, CA_DOCK);
|
|
|
|
|
|
|
|
if (st->airport_tile) {
|
|
|
|
switch (st->airport_type) {
|
|
|
|
case AT_OILRIG: ret = max(ret, CA_AIR_OILPAD); break;
|
|
|
|
case AT_SMALL: ret = max(ret, CA_AIR_SMALL); break;
|
|
|
|
case AT_HELIPORT: ret = max(ret, CA_AIR_HELIPORT); break;
|
|
|
|
case AT_LARGE: ret = max(ret, CA_AIR_LARGE); break;
|
|
|
|
case AT_METROPOLITAN: ret = max(ret, CA_AIR_METRO); break;
|
|
|
|
case AT_INTERNATIONAL: ret = max(ret, CA_AIR_INTER); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#define CHECK_STATIONS_ERR ((Station*)-1)
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static Station *GetStationAround(TileIndex tile, int w, int h, int closest_station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
// check around to see if there's any stations there
|
2005-06-25 16:44:57 +00:00
|
|
|
BEGIN_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TileDiffXY(1, 1))
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile_cur, MP_STATION)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int t;
|
2005-07-13 18:04:01 +00:00
|
|
|
t = _m[tile_cur].m2;
|
2004-08-12 21:11:23 +00:00
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Station *st = GetStation(t);
|
2004-08-12 21:11:23 +00:00
|
|
|
// you cannot take control of an oilrig!!
|
|
|
|
if (st->airport_type == AT_OILRIG && st->facilities == (FACIL_AIRPORT|FACIL_DOCK))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (closest_station == -1) {
|
|
|
|
closest_station = t;
|
|
|
|
} else if (closest_station != t) {
|
|
|
|
_error_message = STR_3006_ADJOINS_MORE_THAN_ONE_EXISTING;
|
|
|
|
return CHECK_STATIONS_ERR;
|
|
|
|
}
|
|
|
|
}
|
2005-06-25 16:44:57 +00:00
|
|
|
END_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TileDiffXY(1, 1))
|
2005-01-06 22:31:58 +00:00
|
|
|
return (closest_station == -1) ? NULL : GetStation(closest_station);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
TileIndex GetStationTileForVehicle(const Vehicle *v, const Station *st)
|
2004-08-15 13:21:18 +00:00
|
|
|
{
|
|
|
|
switch (v->type) {
|
|
|
|
case VEH_Train: return st->train_tile;
|
|
|
|
case VEH_Aircraft: return st->airport_tile;
|
|
|
|
case VEH_Ship: return st->dock_tile;
|
2005-01-30 17:46:57 +00:00
|
|
|
case VEH_Road:
|
|
|
|
if (v->cargo_type == CT_PASSENGERS) {
|
|
|
|
if (st->bus_stops != NULL)
|
|
|
|
return st->bus_stops->xy;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (st->truck_stops != NULL)
|
|
|
|
return st->truck_stops->xy;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2004-08-15 13:21:18 +00:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool CheckStationSpreadOut(Station *st, TileIndex tile, int w, int h)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-25 10:40:58 +00:00
|
|
|
StationID station_index = st->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint i;
|
2005-01-07 17:02:43 +00:00
|
|
|
uint x1 = TileX(tile);
|
|
|
|
uint y1 = TileY(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
uint x2 = x1 + w - 1;
|
|
|
|
uint y2 = y1 + h - 1;
|
|
|
|
uint t;
|
|
|
|
|
2005-01-03 18:59:58 +00:00
|
|
|
for (i = 0; i != MapSize(); i++) {
|
2005-07-13 18:04:01 +00:00
|
|
|
if (IsTileType(i, MP_STATION) && _m[i].m2 == station_index) {
|
2005-01-07 17:02:43 +00:00
|
|
|
t = TileX(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (t < x1) x1 = t;
|
|
|
|
if (t > x2) x2 = t;
|
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
t = TileY(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (t < y1) y1 = t;
|
|
|
|
if (t > y2) y2 = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y2-y1 >= _patches.station_spread || x2-x1 >= _patches.station_spread) {
|
|
|
|
_error_message = STR_306C_STATION_TOO_SPREAD_OUT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static Station *AllocateStation(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-03 17:22:35 +00:00
|
|
|
Station *st = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->xy == 0) {
|
2005-03-25 10:40:58 +00:00
|
|
|
StationID index = st->index;
|
2005-02-03 17:22:35 +00:00
|
|
|
|
|
|
|
memset(st, 0, sizeof(Station));
|
|
|
|
st->index = index;
|
|
|
|
|
|
|
|
return st;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-03 17:22:35 +00:00
|
|
|
/* Check if we can add a block to the pool */
|
|
|
|
if (AddBlockToPool(&_station_pool))
|
|
|
|
return AllocateStation();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-03 17:22:35 +00:00
|
|
|
_error_message = STR_3008_TOO_MANY_STATIONS_LOADING;
|
|
|
|
return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static int CountMapSquareAround(TileIndex tile, byte type, byte min, byte max)
|
|
|
|
{
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _count_square_table[] = {
|
|
|
|
{-3, -3}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
|
|
|
|
{-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
|
|
|
|
{-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
|
|
|
|
{-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
|
|
|
|
{-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
|
|
|
|
{-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
|
|
|
|
{-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}
|
2004-09-10 19:02:27 +00:00
|
|
|
};
|
2005-01-06 11:39:00 +00:00
|
|
|
const TileIndexDiffC *p;
|
2004-08-09 17:04:08 +00:00
|
|
|
int num = 0;
|
|
|
|
|
2005-01-03 08:33:04 +00:00
|
|
|
for (p = _count_square_table; p != endof(_count_square_table); ++p) {
|
2005-01-06 11:39:00 +00:00
|
|
|
tile = TILE_MASK(tile + ToTileIndexDiff(*p));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
if (IsTileType(tile, type) && _m[tile].m5 >= min && _m[tile].m5 <= max)
|
2004-08-09 17:04:08 +00:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define M(x) ((x) - STR_SV_STNAME)
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool GenerateStationName(Station *st, TileIndex tile, int flag)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
static const uint32 _gen_station_name_bits[] = {
|
2005-03-08 00:26:30 +00:00
|
|
|
0, /* 0 */
|
|
|
|
1 << M(STR_SV_STNAME_AIRPORT), /* 1 */
|
|
|
|
1 << M(STR_SV_STNAME_OILFIELD), /* 2 */
|
|
|
|
1 << M(STR_SV_STNAME_DOCKS), /* 3 */
|
|
|
|
0x1FF << M(STR_SV_STNAME_BUOY_1), /* 4 */
|
|
|
|
1 << M(STR_SV_STNAME_HELIPORT), /* 5 */
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Town *t = st->town;
|
|
|
|
uint32 free_names = (uint32)-1;
|
|
|
|
int found;
|
|
|
|
uint z,z2;
|
|
|
|
unsigned long tmp;
|
|
|
|
|
|
|
|
{
|
|
|
|
Station *s;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(s) {
|
|
|
|
if (s != st && s->xy != 0 && s->town==t) {
|
|
|
|
uint str = M(s->string_id);
|
|
|
|
if (str <= 0x20) {
|
|
|
|
if (str == M(STR_SV_STNAME_FOREST))
|
|
|
|
str = M(STR_SV_STNAME_WOODS);
|
|
|
|
CLRBIT(free_names, str);
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* check default names */
|
|
|
|
tmp = free_names & _gen_station_name_bits[flag];
|
|
|
|
if (tmp != 0) {
|
|
|
|
found = FindFirstBit(tmp);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check mine? */
|
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME_MINES))) {
|
|
|
|
if (CountMapSquareAround(tile, MP_INDUSTRY, 0, 6) >= 2 ||
|
2005-03-09 19:09:04 +00:00
|
|
|
CountMapSquareAround(tile, MP_INDUSTRY, 0x64, 0x73) >= 2 ||
|
|
|
|
CountMapSquareAround(tile, MP_INDUSTRY, 0x2F, 0x33) >= 2 ||
|
|
|
|
CountMapSquareAround(tile, MP_INDUSTRY, 0x48, 0x58) >= 2 ||
|
|
|
|
CountMapSquareAround(tile, MP_INDUSTRY, 0x5B, 0x63) >= 2) {
|
2005-03-08 00:26:30 +00:00
|
|
|
found = M(STR_SV_STNAME_MINES);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check close enough to town to get central as name? */
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceMax(tile,t->xy) < 8) {
|
2004-08-09 17:04:08 +00:00
|
|
|
found = M(STR_SV_STNAME);
|
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME))) goto done;
|
|
|
|
|
|
|
|
found = M(STR_SV_STNAME_CENTRAL);
|
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME_CENTRAL))) goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check lakeside */
|
2005-03-09 19:09:04 +00:00
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME_LAKESIDE)) &&
|
|
|
|
DistanceFromEdge(tile) < 20 &&
|
|
|
|
CountMapSquareAround(tile, MP_WATER, 0, 0) >= 5) {
|
2005-03-08 00:26:30 +00:00
|
|
|
found = M(STR_SV_STNAME_LAKESIDE);
|
|
|
|
goto done;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Check woods */
|
2005-03-09 19:09:04 +00:00
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME_WOODS)) && (
|
|
|
|
CountMapSquareAround(tile, MP_TREES, 0, 255) >= 8 ||
|
|
|
|
CountMapSquareAround(tile, MP_INDUSTRY, 0x10, 0x11) >= 2)
|
|
|
|
) {
|
|
|
|
found = _opt.landscape == LT_DESERT ?
|
|
|
|
M(STR_SV_STNAME_FOREST) : M(STR_SV_STNAME_WOODS);
|
2005-03-08 00:26:30 +00:00
|
|
|
goto done;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check elevation compared to town */
|
|
|
|
z = GetTileZ(tile);
|
|
|
|
z2 = GetTileZ(t->xy);
|
|
|
|
if (z < z2) {
|
|
|
|
found = M(STR_SV_STNAME_VALLEY);
|
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME_VALLEY))) goto done;
|
|
|
|
} else if (z > z2) {
|
|
|
|
found = M(STR_SV_STNAME_HEIGHTS);
|
|
|
|
if (HASBIT(free_names, M(STR_SV_STNAME_HEIGHTS))) goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check direction compared to town */
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
free_names &= _direction_and_table[
|
2005-01-07 17:02:43 +00:00
|
|
|
(TileX(tile) < TileX(t->xy)) +
|
|
|
|
(TileY(tile) < TileY(t->xy)) * 2];
|
2004-08-09 17:04:08 +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));
|
|
|
|
if (tmp == 0) {
|
|
|
|
_error_message = STR_3007_TOO_MANY_STATIONS_LOADING;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
found = FindFirstBit(tmp);
|
|
|
|
|
|
|
|
done:
|
|
|
|
st->string_id = found + STR_SV_STNAME;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#undef M
|
|
|
|
|
2005-03-05 18:44:26 +00:00
|
|
|
static Station *GetClosestStationFromTile(TileIndex tile, uint threshold, byte owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-05 18:44:26 +00:00
|
|
|
Station* best_station = NULL;
|
|
|
|
Station* st;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2005-03-05 18:44:26 +00:00
|
|
|
if (st->xy != 0 && (owner == OWNER_SPECTATOR || st->owner == owner)) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StationInitialize(Station *st, TileIndex tile)
|
|
|
|
{
|
|
|
|
GoodsEntry *ge;
|
|
|
|
|
|
|
|
st->xy = tile;
|
2005-01-29 19:41:44 +00:00
|
|
|
st->airport_tile = st->dock_tile = st->train_tile = 0;
|
|
|
|
st->bus_stops = st->truck_stops = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->had_vehicle_of_type = 0;
|
|
|
|
st->time_since_load = 255;
|
|
|
|
st->time_since_unload = 255;
|
|
|
|
st->delete_ctr = 0;
|
|
|
|
st->facilities = 0;
|
|
|
|
|
|
|
|
st->last_vehicle = INVALID_VEHICLE;
|
|
|
|
|
2005-03-05 18:44:26 +00:00
|
|
|
for (ge = st->goods; ge != endof(st->goods); ge++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ge->waiting_acceptance = 0;
|
|
|
|
ge->days_since_pickup = 0;
|
2005-02-02 16:16:43 +00:00
|
|
|
ge->enroute_from = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
ge->rating = 175;
|
|
|
|
ge->last_speed = 0;
|
|
|
|
ge->last_age = 0xFF;
|
2005-06-15 16:58:15 +00:00
|
|
|
ge->feeder_profit = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-14 14:19:53 +00:00
|
|
|
|
|
|
|
_global_station_sort_dirty = true; // build a new station
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the virtual coords needed to draw the station sign.
|
|
|
|
// st = Station to update for.
|
|
|
|
static void UpdateStationVirtCoord(Station *st)
|
|
|
|
{
|
2005-01-07 17:02:43 +00:00
|
|
|
Point pt = RemapCoords2(TileX(st->xy) * 16, TileY(st->xy) * 16);
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
pt.y -= 32;
|
2005-01-29 19:41:44 +00:00
|
|
|
if (st->facilities & FACIL_AIRPORT && st->airport_type == AT_OILRIG) pt.y -= 16;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
|
|
|
SetDParam(1, st->facilities);
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateViewportSignPos(&st->sign, pt.x, pt.y, STR_305C_0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the virtual coords needed to draw the station sign for all stations.
|
2005-01-22 20:23:18 +00:00
|
|
|
void UpdateAllStationVirtCoord(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0)
|
2004-09-10 19:02:27 +00:00
|
|
|
UpdateStationVirtCoord(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the station virt coords while making the modified parts dirty.
|
|
|
|
static void UpdateStationVirtCoordDirty(Station *st)
|
|
|
|
{
|
|
|
|
MarkStationDirty(st);
|
|
|
|
UpdateStationVirtCoord(st);
|
|
|
|
MarkStationDirty(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a mask of the cargo types that the station accepts.
|
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
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i != NUM_CARGO; i++) {
|
|
|
|
if (st->goods[i].waiting_acceptance & 0x8000) mask |= 1 << i;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Items contains the two cargo names that are to be accepted or rejected.
|
|
|
|
// msg is the string id of the message to display.
|
2005-03-05 18:44:26 +00:00
|
|
|
static void ShowRejectOrAcceptNews(const Station *st, uint32 items, StringID msg)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (items) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(2, items >> 16);
|
|
|
|
SetDParam(1, items & 0xFFFF);
|
|
|
|
SetDParam(0, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem(msg + ((items >> 16)?1:0), NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_TILE, NT_ACCEPTANCE, 0), st->xy, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of the cargo types being produced around the tile.
|
2005-03-05 18:44:26 +00:00
|
|
|
void GetProductionAroundTiles(AcceptedCargo produced, TileIndex tile,
|
|
|
|
int w, int h, int rad)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int x,y;
|
|
|
|
int x1,y1,x2,y2;
|
|
|
|
int xc,yc;
|
|
|
|
|
2005-07-19 21:49:35 +00:00
|
|
|
memset(produced, 0, sizeof(AcceptedCargo));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
x = TileX(tile);
|
|
|
|
y = TileY(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-05 18:44:26 +00:00
|
|
|
// expand the region by rad tiles on each side
|
2004-08-09 17:04:08 +00:00
|
|
|
// while making sure that we remain inside the board.
|
2005-01-03 12:56:22 +00:00
|
|
|
x2 = min(x + w + rad, MapSizeX());
|
2005-03-05 18:44:26 +00:00
|
|
|
x1 = max(x - rad, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-03 12:56:22 +00:00
|
|
|
y2 = min(y + h + rad, MapSizeY());
|
2005-03-05 18:44:26 +00:00
|
|
|
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);
|
|
|
|
|
2005-03-05 18:44:26 +00:00
|
|
|
for (yc = y1; yc != y2; yc++) {
|
|
|
|
for (xc = x1; xc != x2; xc++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!(IS_INSIDE_1D(xc, x, w) && IS_INSIDE_1D(yc, y, h))) {
|
|
|
|
GetProducedCargoProc *gpc;
|
2005-06-25 16:44:57 +00:00
|
|
|
TileIndex tile = TileXY(xc, yc);
|
2005-03-05 18:44:26 +00:00
|
|
|
|
2005-01-29 15:12:40 +00:00
|
|
|
gpc = _tile_type_procs[GetTileType(tile)]->get_produced_cargo_proc;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (gpc != NULL) {
|
2005-03-05 18:44:26 +00:00
|
|
|
byte cargos[2] = { CT_INVALID, CT_INVALID };
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
gpc(tile, cargos);
|
2005-03-05 18:44:26 +00:00
|
|
|
if (cargos[0] != CT_INVALID) {
|
2004-08-09 17:04:08 +00:00
|
|
|
produced[cargos[0]]++;
|
2005-03-05 18:44:26 +00:00
|
|
|
if (cargos[1] != CT_INVALID) {
|
2004-08-09 17:04:08 +00:00
|
|
|
produced[cargos[1]]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-05 18:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of the cargo types that are accepted around the tile.
|
2005-03-05 18:44:26 +00:00
|
|
|
void GetAcceptanceAroundTiles(AcceptedCargo accepts, TileIndex tile,
|
|
|
|
int w, int h, int rad)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int x,y;
|
|
|
|
int x1,y1,x2,y2;
|
|
|
|
int xc,yc;
|
|
|
|
|
2004-11-21 10:49:40 +00:00
|
|
|
memset(accepts, 0, sizeof(AcceptedCargo));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
x = TileX(tile);
|
|
|
|
y = TileY(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-05 18:44:26 +00:00
|
|
|
// expand the region by rad tiles on each side
|
2004-08-09 17:04:08 +00:00
|
|
|
// while making sure that we remain inside the board.
|
2005-01-03 12:56:22 +00:00
|
|
|
x2 = min(x + w + rad, MapSizeX());
|
|
|
|
y2 = min(y + h + rad, MapSizeY());
|
2005-03-05 18:44:26 +00:00
|
|
|
x1 = max(x - rad, 0);
|
|
|
|
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);
|
|
|
|
|
2005-03-05 18:44:26 +00:00
|
|
|
for (yc = y1; yc != y2; yc++) {
|
|
|
|
for (xc = x1; xc != x2; xc++) {
|
2005-06-25 16:44:57 +00:00
|
|
|
TileIndex tile = TileXY(xc, yc);
|
2005-03-05 18:44:26 +00:00
|
|
|
|
2005-01-16 11:24:58 +00:00
|
|
|
if (!IsTileType(tile, MP_STATION)) {
|
2004-11-21 10:49:40 +00:00
|
|
|
AcceptedCargo ac;
|
2005-03-05 18:44:26 +00:00
|
|
|
uint i;
|
2004-11-21 10:49:40 +00:00
|
|
|
|
|
|
|
GetAcceptedCargo(tile, ac);
|
2005-03-05 18:44:26 +00:00
|
|
|
for (i = 0; i < lengthof(ac); ++i)
|
2004-11-21 10:49:40 +00:00
|
|
|
accepts[i] += ac[i];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-03-05 18:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 17:41:51 +00:00
|
|
|
typedef struct Rectangle {
|
|
|
|
uint min_x;
|
|
|
|
uint min_y;
|
|
|
|
uint max_x;
|
|
|
|
uint max_y;
|
|
|
|
} Rectangle;
|
|
|
|
|
|
|
|
static void MergePoint(Rectangle* rect, TileIndex tile)
|
|
|
|
{
|
|
|
|
uint x = TileX(tile);
|
|
|
|
uint y = TileY(tile);
|
|
|
|
|
|
|
|
if (rect->min_x > x) rect->min_x = x;
|
|
|
|
if (rect->min_y > y) rect->min_y = y;
|
|
|
|
if (rect->max_x < x) rect->max_x = x;
|
|
|
|
if (rect->max_y < y) rect->max_y = y;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Update the acceptance for a station.
|
|
|
|
// show_msg controls whether to display a message that acceptance was changed.
|
|
|
|
static void UpdateStationAcceptance(Station *st, bool show_msg)
|
|
|
|
{
|
|
|
|
uint old_acc, new_acc;
|
2005-03-05 17:41:51 +00:00
|
|
|
const RoadStop *cur_rs;
|
2004-08-09 17:04:08 +00:00
|
|
|
int i;
|
2005-03-15 12:21:59 +00:00
|
|
|
Rectangle rect;
|
2005-03-05 17:41:51 +00:00
|
|
|
int rad;
|
|
|
|
AcceptedCargo accepts;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-15 12:21:59 +00:00
|
|
|
rect.min_x = MapSizeX();
|
|
|
|
rect.min_y = MapSizeY();
|
|
|
|
rect.max_x = rect.max_y = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
// Don't update acceptance for a buoy
|
2005-05-02 22:13:20 +00:00
|
|
|
if (IsBuoy(st))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* old accepted goods types */
|
|
|
|
old_acc = GetAcceptanceMask(st);
|
|
|
|
|
|
|
|
// Put all the tiles that span an area in the table.
|
|
|
|
if (st->train_tile != 0) {
|
2005-03-05 17:41:51 +00:00
|
|
|
MergePoint(&rect, st->train_tile);
|
|
|
|
MergePoint(&rect,
|
2005-06-25 16:44:57 +00:00
|
|
|
st->train_tile + TileDiffXY(st->trainst_w - 1, st->trainst_h - 1)
|
2005-03-05 17:41:51 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->airport_tile != 0) {
|
2005-03-05 17:41:51 +00:00
|
|
|
MergePoint(&rect, st->airport_tile);
|
|
|
|
MergePoint(&rect,
|
2005-06-25 16:44:57 +00:00
|
|
|
st->airport_tile + TileDiffXY(
|
2005-03-05 17:41:51 +00:00
|
|
|
_airport_size_x[st->airport_type] - 1,
|
|
|
|
_airport_size_y[st->airport_type] - 1
|
|
|
|
)
|
|
|
|
);
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 17:41:51 +00:00
|
|
|
if (st->dock_tile != 0) MergePoint(&rect, st->dock_tile);
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2005-03-05 17:41:51 +00:00
|
|
|
for (cur_rs = st->bus_stops; cur_rs != NULL; cur_rs = cur_rs->next) {
|
|
|
|
MergePoint(&rect, cur_rs->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-05 17:41:51 +00:00
|
|
|
for (cur_rs = st->truck_stops; cur_rs != NULL; cur_rs = cur_rs->next) {
|
|
|
|
MergePoint(&rect, cur_rs->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
if (_patches.modified_catchment) {
|
2004-12-18 13:38:27 +00:00
|
|
|
rad = FindCatchmentRadius(st);
|
2004-12-08 15:46:13 +00:00
|
|
|
} else {
|
|
|
|
rad = 4;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// And retrieve the acceptance.
|
2005-03-05 17:41:51 +00:00
|
|
|
if (rect.max_x >= rect.min_x) {
|
|
|
|
GetAcceptanceAroundTiles(
|
|
|
|
accepts,
|
2005-06-25 16:44:57 +00:00
|
|
|
TileXY(rect.min_x, rect.min_y),
|
2005-03-05 17:41:51 +00:00
|
|
|
rect.max_x - rect.min_x + 1,
|
|
|
|
rect.max_y - rect.min_y + 1,
|
|
|
|
rad
|
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
memset(accepts, 0, sizeof(accepts));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust in case our station only accepts fewer kinds of goods
|
2005-03-05 18:44:26 +00:00
|
|
|
for (i = 0; i != NUM_CARGO; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint amt = min(accepts[i], 15);
|
|
|
|
|
|
|
|
// Make sure the station can accept the goods type.
|
|
|
|
if ((i != CT_PASSENGERS && !(st->facilities & (byte)~FACIL_BUS_STOP)) ||
|
|
|
|
(i == CT_PASSENGERS && !(st->facilities & (byte)~FACIL_TRUCK_STOP)))
|
|
|
|
amt = 0;
|
|
|
|
|
|
|
|
st->goods[i].waiting_acceptance = (st->goods[i].waiting_acceptance & ~0xF000) + (amt << 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only show a message in case the acceptance was actually changed.
|
|
|
|
new_acc = GetAcceptanceMask(st);
|
|
|
|
if (old_acc == new_acc)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// show a message to report that the acceptance was changed?
|
|
|
|
if (show_msg && st->owner == _local_player && st->facilities) {
|
|
|
|
uint32 accept=0, reject=0; /* these contain two string ids each */
|
|
|
|
const StringID *str = _cargoc.names_s;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (new_acc & 1) {
|
2004-09-10 19:02:27 +00:00
|
|
|
if (!(old_acc & 1)) accept = (accept << 16) | *str;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
if (old_acc & 1) reject = (reject << 16) | *str;
|
|
|
|
}
|
|
|
|
} while (str++,(new_acc>>=1) != (old_acc>>=1));
|
|
|
|
|
|
|
|
ShowRejectOrAcceptNews(st, accept, STR_3040_NOW_ACCEPTS);
|
|
|
|
ShowRejectOrAcceptNews(st, reject, STR_303E_NO_LONGER_ACCEPTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// redraw the station view since acceptance changed
|
|
|
|
InvalidateWindowWidget(WC_STATION_VIEW, st->index, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called right after a station was deleted.
|
|
|
|
// It checks if the whole station is free of substations, and if so, the station will be
|
|
|
|
// deleted after a little while.
|
|
|
|
static void DeleteStationIfEmpty(Station *st) {
|
|
|
|
if (st->facilities == 0) {
|
|
|
|
st->delete_ctr = 0;
|
|
|
|
InvalidateWindow(WC_STATION_LIST, st->owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static int32 ClearTile_Station(TileIndex tile, byte flags);
|
2005-01-10 21:52:35 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Tries to clear the given area. Returns the cost in case of success.
|
|
|
|
// Or an error code if it failed.
|
2005-06-24 12:38:35 +00:00
|
|
|
int32 CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, int *station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int32 cost = 0, ret;
|
|
|
|
|
|
|
|
uint tileh;
|
2005-02-07 10:41:45 +00:00
|
|
|
uint z;
|
|
|
|
int allowed_z = -1;
|
|
|
|
int flat_z;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
BEGIN_TILE_LOOP(tile_cur, w, h, tile)
|
|
|
|
if (!EnsureNoVehicle(tile_cur))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
tileh = GetTileSlope(tile_cur, &z);
|
|
|
|
|
2005-07-16 23:47:37 +00:00
|
|
|
/* Prohibit building if
|
|
|
|
1) The tile is "steep" (i.e. stretches two height levels)
|
|
|
|
-OR-
|
|
|
|
2) The tile is non-flat if
|
|
|
|
a) the player building is an "old-school" AI
|
|
|
|
-OR-
|
|
|
|
b) the build_on_slopes switch is disabled
|
|
|
|
*/
|
|
|
|
if (IsSteepTileh(tileh) ||
|
|
|
|
(((!_patches.ainew_active && _is_ai_player) || !_patches.build_on_slopes)
|
|
|
|
&& tileh != 0)) {
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_0007_FLAT_LAND_REQUIRED;
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
flat_z = z;
|
|
|
|
if (tileh) {
|
|
|
|
// need to check so the entrance to the station is not pointing at a slope.
|
|
|
|
if ((invalid_dirs&1 && !(tileh & 0xC) && (uint)w_cur == w) ||
|
|
|
|
(invalid_dirs&2 && !(tileh & 6) && h_cur == 1) ||
|
|
|
|
(invalid_dirs&4 && !(tileh & 3) && w_cur == 1) ||
|
|
|
|
(invalid_dirs&8 && !(tileh & 9) && (uint)h_cur == h)) {
|
|
|
|
_error_message = STR_0007_FLAT_LAND_REQUIRED;
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
cost += _price.terraform;
|
|
|
|
flat_z += 8;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// get corresponding flat level and make sure that all parts of the station have the same level.
|
|
|
|
if (allowed_z == -1) {
|
|
|
|
// first tile
|
|
|
|
allowed_z = flat_z;
|
|
|
|
} else if (allowed_z != flat_z) {
|
|
|
|
_error_message = STR_0007_FLAT_LAND_REQUIRED;
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// if station is set, then we have special handling to allow building on top of already existing stations.
|
2004-08-09 17:04:08 +00:00
|
|
|
// so station points to -1 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)) {
|
2005-07-13 18:04:01 +00:00
|
|
|
if (_m[tile_cur].m5 >= 8) {
|
2005-01-10 21:52:35 +00:00
|
|
|
_error_message = ClearTile_Station(tile_cur, DC_AUTO); // get error message
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
} else {
|
2005-07-13 18:04:01 +00:00
|
|
|
int st = _m[tile_cur].m2;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (*station == -1)
|
|
|
|
*station = st;
|
|
|
|
else if (*station != st) {
|
|
|
|
_error_message = STR_3006_ADJOINS_MORE_THAN_ONE_EXISTING;
|
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = DoCommandByTile(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (ret == CMD_ERROR) return CMD_ERROR;
|
|
|
|
cost += ret;
|
|
|
|
}
|
|
|
|
END_TILE_LOOP(tile_cur, w, h, tile)
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CanExpandRailroadStation(Station *st, uint *fin, int direction)
|
|
|
|
{
|
|
|
|
uint curw = st->trainst_w, curh = st->trainst_h;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = fin[0];
|
2004-08-09 17:04:08 +00:00
|
|
|
uint w = fin[1];
|
|
|
|
uint h = fin[2];
|
|
|
|
|
|
|
|
if (_patches.nonuniform_stations) {
|
|
|
|
// determine new size of train station region..
|
2005-01-07 17:02:43 +00:00
|
|
|
int x = min(TileX(st->train_tile), TileX(tile));
|
|
|
|
int y = min(TileY(st->train_tile), TileY(tile));
|
|
|
|
curw = max(TileX(st->train_tile) + curw, TileX(tile) + w) - x;
|
|
|
|
curh = max(TileY(st->train_tile) + curh, TileY(tile) + h) - y;
|
2005-06-25 16:44:57 +00:00
|
|
|
tile = TileXY(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
// check so the direction is the same
|
2005-07-13 18:04:01 +00:00
|
|
|
if ((_m[st->train_tile].m5 & 1) != direction) {
|
2005-01-27 09:43:24 +00:00
|
|
|
_error_message = STR_306D_NONUNIFORM_STATIONS_DISALLOWED;
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// check if the new station adjoins the old station in either direction
|
2005-06-25 16:44:57 +00:00
|
|
|
if (curw == w && st->train_tile == tile + TileDiffXY(0, h)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// above
|
|
|
|
curh += h;
|
2005-06-25 16:44:57 +00:00
|
|
|
} else if (curw == w && st->train_tile == tile - TileDiffXY(0, curh)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// below
|
2005-06-25 16:44:57 +00:00
|
|
|
tile -= TileDiffXY(0, curh);
|
2004-08-09 17:04:08 +00:00
|
|
|
curh += h;
|
2005-06-25 16:44:57 +00:00
|
|
|
} else if (curh == h && st->train_tile == tile + TileDiffXY(w, 0)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// to the left
|
|
|
|
curw += w;
|
2005-06-25 16:44:57 +00:00
|
|
|
} else if (curh == h && st->train_tile == tile - TileDiffXY(curw, 0)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// to the right
|
2005-06-25 16:44:57 +00:00
|
|
|
tile -= TileDiffXY(curw, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
curw += w;
|
2005-01-27 09:43:24 +00:00
|
|
|
} else {
|
|
|
|
_error_message = STR_306D_NONUNIFORM_STATIONS_DISALLOWED;
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
2005-01-27 09:43:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
// make sure the final size is not too big.
|
2005-01-27 09:43:24 +00:00
|
|
|
if (curw > _patches.station_spread || curh > _patches.station_spread) {
|
|
|
|
_error_message = STR_306C_STATION_TOO_SPREAD_OUT;
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// now tile contains the new value for st->train_tile
|
|
|
|
// curw, curh contain the new value for width and height
|
|
|
|
fin[0] = tile;
|
|
|
|
fin[1] = curw;
|
|
|
|
fin[2] = curh;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
layout[((n-1) >> 1)-n] = 2;
|
|
|
|
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) {
|
|
|
|
layout[0-n] = 0;
|
|
|
|
layout[n-1-n] = 0;
|
|
|
|
}
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stolen from TTDPatch
|
2005-03-10 07:01:43 +00:00
|
|
|
static void GetStationLayout(byte *layout, int numtracks, int plat_len, StationSpec *spec)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-09 19:09:04 +00:00
|
|
|
if (spec != NULL && spec->lengths >= plat_len &&
|
|
|
|
spec->platforms[plat_len - 1] >= numtracks &&
|
|
|
|
spec->layouts[plat_len - 1][numtracks - 1]) {
|
2004-11-17 18:03:33 +00:00
|
|
|
/* Custom layout defined, follow it. */
|
|
|
|
memcpy(layout, spec->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 {
|
|
|
|
if (numtracks & 1)
|
|
|
|
layout = CreateSingle(layout, plat_len);
|
|
|
|
numtracks>>=1;
|
|
|
|
|
|
|
|
while (--numtracks >= 0) {
|
|
|
|
layout = CreateMulti(layout, plat_len, 4);
|
|
|
|
layout = CreateMulti(layout, plat_len, 6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-07 10:26:12 +00:00
|
|
|
/** Build railroad station
|
2005-05-09 13:26:15 +00:00
|
|
|
* @param x,y starting position of station dragging/placement
|
2005-05-07 10:26:12 +00:00
|
|
|
* @param p1 various bitstuffed elements
|
|
|
|
* - p1 = (bit 0) - orientation (p1 & 1)
|
2005-07-20 15:29:28 +00:00
|
|
|
* - p1 = (bit 8-15) - number of tracks
|
|
|
|
* - p1 = (bit 16-23) - platform length
|
2005-05-07 10:26:12 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = (bit 0- 3) - railtype (p2 & 0xF)
|
|
|
|
* - p2 = (bit 4) - set for custom station (p2 & 0x10)
|
|
|
|
* - p2 = (bit 8-..) - custom station id (p2 >> 8)
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2005-05-07 10:26:12 +00:00
|
|
|
int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
2005-05-07 10:26:12 +00:00
|
|
|
TileIndex tile_org;
|
|
|
|
int w_org, h_org;
|
|
|
|
int32 cost, ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
int est;
|
|
|
|
int plat_len, numtracks;
|
|
|
|
int direction;
|
|
|
|
uint finalvalues[3];
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
|
|
|
2005-06-25 06:15:43 +00:00
|
|
|
tile_org = TileVirtXY(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Does the authority allow this? */
|
2005-05-07 10:26:12 +00:00
|
|
|
if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile_org)) return CMD_ERROR;
|
|
|
|
if (!ValParamRailtype(p2 & 0xF)) return CMD_ERROR;
|
|
|
|
|
|
|
|
/* unpack parameters */
|
|
|
|
direction = p1 & 1;
|
2005-07-20 15:29:28 +00:00
|
|
|
numtracks = GB(p1, 8, 8);
|
|
|
|
plat_len = GB(p1, 16, 8);
|
2005-05-07 10:26:12 +00:00
|
|
|
/* w = length, h = num_tracks */
|
|
|
|
if (direction) {
|
|
|
|
h_org = plat_len;
|
|
|
|
w_org = numtracks;
|
|
|
|
} else {
|
|
|
|
w_org = plat_len;
|
|
|
|
h_org = numtracks;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-09 13:26:15 +00:00
|
|
|
if (h_org > _patches.station_spread || w_org > _patches.station_spread) return CMD_ERROR;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// these values are those that will be stored in train_tile and station_platforms
|
|
|
|
finalvalues[0] = tile_org;
|
|
|
|
finalvalues[1] = w_org;
|
|
|
|
finalvalues[2] = h_org;
|
|
|
|
|
|
|
|
// Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station)
|
|
|
|
est = -1;
|
2004-09-25 13:58:27 +00:00
|
|
|
// If DC_EXEC is in flag, do not want to pass it to CheckFlatLandBelow, because of a nice bug
|
|
|
|
// for detail info, see: https://sourceforge.net/tracker/index.php?func=detail&aid=1029064&group_id=103924&atid=636365
|
2005-05-07 10:26:12 +00:00
|
|
|
if (CmdFailed(ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags&~DC_EXEC, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
cost = ret + (numtracks * _price.train_station_track + _price.train_station_length) * plat_len;
|
|
|
|
|
|
|
|
// Make sure there are no similar stations around us.
|
|
|
|
st = GetStationAround(tile_org, w_org, h_org, est);
|
|
|
|
if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
|
|
|
|
|
|
|
|
// See if there is a deleted station close to us.
|
|
|
|
if (st == NULL) {
|
|
|
|
st = GetClosestStationFromTile(tile_org, 8, _current_player);
|
|
|
|
if (st != NULL && st->facilities) st = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st != NULL) {
|
|
|
|
// Reuse an existing station.
|
|
|
|
if (st->owner != OWNER_NONE && st->owner != _current_player)
|
|
|
|
return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
|
|
|
|
|
|
|
|
if (st->train_tile != 0) {
|
2004-08-20 09:32:32 +00:00
|
|
|
// check if we want to expanding an already existing station?
|
2005-01-27 09:43:24 +00:00
|
|
|
if ((!_patches.ainew_active && _is_ai_player) || !_patches.join_stations)
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_3005_TOO_CLOSE_TO_ANOTHER_RAILROAD);
|
2005-01-27 09:43:24 +00:00
|
|
|
if (!CanExpandRailroadStation(st, finalvalues, direction))
|
|
|
|
return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-27 09:43:24 +00:00
|
|
|
//XXX can't we pack this in the "else" part of the if above?
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!CheckStationSpreadOut(st, tile_org, w_org, h_org))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Create a new station
|
|
|
|
st = AllocateStation();
|
|
|
|
if (st == NULL)
|
|
|
|
return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
st->town = ClosestTownFromTile(tile_org, (uint)-1);
|
|
|
|
if (_current_player < MAX_PLAYERS && flags&DC_EXEC)
|
|
|
|
SETBIT(st->town->have_ratings, _current_player);
|
|
|
|
|
|
|
|
if (!GenerateStationName(st, tile_org, 0))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
StationInitialize(st, tile_org);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2005-03-25 10:40:58 +00:00
|
|
|
StationID station_index = st->index;
|
2005-03-10 07:01:43 +00:00
|
|
|
StationSpec *statspec;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-25 13:58:27 +00:00
|
|
|
// Now really clear the land below the station
|
|
|
|
// It should never return CMD_ERROR.. but you never know ;)
|
|
|
|
// (a bit strange function name for it, but it really does clear the land, when DC_EXEC is in flags)
|
2005-05-07 10:26:12 +00:00
|
|
|
if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
|
2004-09-25 13:58:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
st->train_tile = finalvalues[0];
|
|
|
|
if (!st->facilities) st->xy = finalvalues[0];
|
|
|
|
st->facilities |= FACIL_TRAIN;
|
|
|
|
st->owner = _current_player;
|
|
|
|
|
|
|
|
st->trainst_w = finalvalues[1];
|
|
|
|
st->trainst_h = finalvalues[2];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-17 08:40:09 +00:00
|
|
|
st->build_date = _date;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-06-25 16:44:57 +00:00
|
|
|
tile_delta = direction ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
statspec = (p2 & 0x10) != 0 ? GetCustomStation(STAT_CLASS_DFLT, p2 >> 8) : NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
layout_ptr = alloca(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
|
|
|
|
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 {
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
ModifyTile(tile,
|
2004-08-09 17:04:08 +00:00
|
|
|
MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT |
|
2004-11-17 18:03:33 +00:00
|
|
|
MP_MAP2 | MP_MAP5 | MP_MAP3LO | MP_MAP3HI,
|
2004-08-09 17:04:08 +00:00
|
|
|
station_index, /* map2 parameter */
|
2004-11-17 18:03:33 +00:00
|
|
|
p2 & 0xFF, /* map3lo parameter */
|
|
|
|
p2 >> 8, /* map3hi parameter */
|
2004-08-09 17:04:08 +00:00
|
|
|
(*layout_ptr++) + direction /* map5 parameter */
|
|
|
|
);
|
|
|
|
|
|
|
|
tile += tile_delta;
|
|
|
|
} while (--w);
|
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);
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
UpdateStationAcceptance(st, false);
|
|
|
|
InvalidateWindow(WC_STATION_LIST, st->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool TileBelongsToRailStation(Station *st, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
return IsTileType(tile, MP_STATION) && _m[tile].m2 == st->index && _m[tile].m5 < 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MakeRailwayStationAreaSmaller(Station *st)
|
|
|
|
{
|
|
|
|
uint w = st->trainst_w;
|
|
|
|
uint h = st->trainst_h;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = st->train_tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint i;
|
|
|
|
|
|
|
|
restart:
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// too small?
|
|
|
|
if (w != 0 && h != 0) {
|
|
|
|
// check the left side, x = constant, y changes
|
2005-06-25 16:44:57 +00:00
|
|
|
for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(0, i));) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// the left side is unused?
|
2005-06-25 16:44:57 +00:00
|
|
|
if (++i == h) {
|
|
|
|
tile += TileDiffXY(1, 0);
|
|
|
|
w--;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// check the right side, x = constant, y changes
|
2005-06-25 16:44:57 +00:00
|
|
|
for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(w - 1, i));) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// the right side is unused?
|
2005-06-25 16:44:57 +00:00
|
|
|
if (++i == h) {
|
|
|
|
w--;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// check the upper side, y = constant, x changes
|
2005-06-25 16:44:57 +00:00
|
|
|
for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(i, 0));) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// the left side is unused?
|
2005-06-25 16:44:57 +00:00
|
|
|
if (++i == w) {
|
|
|
|
tile += TileDiffXY(0, 1);
|
|
|
|
h--;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// check the lower side, y = constant, x changes
|
2005-06-25 16:44:57 +00:00
|
|
|
for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(i, h - 1));) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// the left side is unused?
|
2005-06-25 16:44:57 +00:00
|
|
|
if (++i == w) {
|
|
|
|
h--;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
tile = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->trainst_w = w;
|
|
|
|
st->trainst_h = h;
|
|
|
|
st->train_tile = tile;
|
|
|
|
}
|
|
|
|
|
2005-05-09 16:37:40 +00:00
|
|
|
/** Remove a single tile from a railroad station.
|
|
|
|
* This allows for custom-built station with holes and weird layouts
|
|
|
|
* @param x,y tile coordinates to remove
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdRemoveFromRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2005-06-25 06:15:43 +00:00
|
|
|
TileIndex tile = TileVirtXY(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
Station *st;
|
|
|
|
|
2005-01-04 17:11:03 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// make sure the specified tile belongs to the current player, and that it is a railroad station.
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!IsTileType(tile, MP_STATION) || _m[tile].m5 >= 8 || !_patches.nonuniform_stations) return CMD_ERROR;
|
|
|
|
st = GetStation(_m[tile].m2);
|
2004-09-03 17:57:27 +00:00
|
|
|
if (_current_player != OWNER_WATER && (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile))) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if we reached here, it means we can actually delete it. do that.
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
DoClearSquare(tile);
|
|
|
|
// 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.
|
|
|
|
MakeRailwayStationAreaSmaller(st);
|
|
|
|
|
|
|
|
// if we deleted the whole station, delete the train facility.
|
|
|
|
if (st->train_tile == 0) {
|
|
|
|
st->facilities &= ~FACIL_TRAIN;
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _price.remove_rail_station;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine the number of platforms for the station
|
2005-06-24 12:38:35 +00:00
|
|
|
uint GetStationPlatforms(Station *st, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint t;
|
|
|
|
int dir,delta;
|
|
|
|
int len;
|
|
|
|
assert(TileBelongsToRailStation(st, tile));
|
|
|
|
|
|
|
|
len = 0;
|
2005-07-13 18:04:01 +00:00
|
|
|
dir = _m[tile].m5&1;
|
2005-06-25 16:44:57 +00:00
|
|
|
delta = dir ? TileDiffXY(0, 1) : TileDiffXY(1, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// find starting tile..
|
|
|
|
t = tile;
|
2005-07-13 18:04:01 +00:00
|
|
|
do { t -= delta; len++; } while (TileBelongsToRailStation(st, t) && (_m[t].m5&1) == dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// find ending tile
|
|
|
|
t = tile;
|
2005-07-13 18:04:01 +00:00
|
|
|
do { t += delta; len++; }while (TileBelongsToRailStation(st, t) && (_m[t].m5&1) == dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return len - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
/* TODO: Custom classes! */
|
|
|
|
/* Indexed by class, just STAT_CLASS_DFLT and STAT_CLASS_WAYP supported. */
|
|
|
|
static int _statspec_highest_id[2] = {-1, -1};
|
2005-03-10 07:01:43 +00:00
|
|
|
static StationSpec _station_spec[2][256];
|
2004-11-14 09:07:15 +00:00
|
|
|
|
2005-03-10 07:01:43 +00:00
|
|
|
void SetCustomStation(byte local_stid, StationSpec *spec)
|
2004-11-14 09:07:15 +00:00
|
|
|
{
|
2005-03-10 07:01:43 +00:00
|
|
|
StationClass sclass;
|
2004-11-14 14:53:15 +00:00
|
|
|
int stid = -1;
|
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
assert(spec->sclass == STAT_CLASS_DFLT || spec->sclass == STAT_CLASS_WAYP);
|
|
|
|
sclass = spec->sclass - 1;
|
2004-11-14 14:53:15 +00:00
|
|
|
|
|
|
|
if (spec->localidx != 0) {
|
|
|
|
/* Already allocated, try to resolve to global stid */
|
|
|
|
int i;
|
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
for (i = 0; i <= _statspec_highest_id[sclass]; i++) {
|
2005-03-09 19:09:04 +00:00
|
|
|
if (_station_spec[sclass][i].grfid == spec->grfid &&
|
|
|
|
_station_spec[sclass][i].localidx == local_stid + 1) {
|
2004-11-14 14:53:15 +00:00
|
|
|
stid = i;
|
2004-11-14 18:18:28 +00:00
|
|
|
/* FIXME: Release original SpriteGroup to
|
|
|
|
* prevent leaks. But first we need to
|
|
|
|
* refcount the SpriteGroup. --pasky */
|
2004-11-14 14:53:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stid == -1) {
|
|
|
|
/* Allocate new one. */
|
2004-11-17 18:03:33 +00:00
|
|
|
if (_statspec_highest_id[sclass] >= 255) {
|
2004-11-14 14:53:15 +00:00
|
|
|
error("Too many custom stations allocated.");
|
|
|
|
return;
|
|
|
|
}
|
2004-11-17 18:03:33 +00:00
|
|
|
stid = ++_statspec_highest_id[sclass];
|
2004-11-14 14:53:15 +00:00
|
|
|
spec->localidx = local_stid + 1;
|
|
|
|
}
|
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
//debug("Registering station #%d of class %d", stid, sclass);
|
|
|
|
memcpy(&_station_spec[sclass][stid], spec, sizeof(*spec));
|
2004-11-14 09:07:15 +00:00
|
|
|
}
|
|
|
|
|
2005-03-10 07:01:43 +00:00
|
|
|
StationSpec *GetCustomStation(StationClass sclass, byte stid)
|
2004-11-14 09:07:15 +00:00
|
|
|
{
|
2004-11-17 18:03:33 +00:00
|
|
|
assert(sclass == STAT_CLASS_DFLT || sclass == STAT_CLASS_WAYP);
|
|
|
|
sclass--;
|
|
|
|
//debug("Asking for station #%d of class %d", stid, sclass);
|
|
|
|
if (stid > _statspec_highest_id[sclass])
|
2004-11-14 09:07:15 +00:00
|
|
|
return NULL;
|
2004-11-17 18:03:33 +00:00
|
|
|
return &_station_spec[sclass][stid];
|
2004-11-14 16:11:48 +00:00
|
|
|
}
|
|
|
|
|
2005-03-10 07:01:43 +00:00
|
|
|
static RealSpriteGroup *ResolveStationSpriteGroup(SpriteGroup *spritegroup, Station *stat)
|
2004-11-14 20:50:38 +00:00
|
|
|
{
|
|
|
|
switch (spritegroup->type) {
|
|
|
|
case SGT_REAL:
|
|
|
|
return &spritegroup->g.real;
|
|
|
|
|
|
|
|
case SGT_DETERMINISTIC: {
|
2005-03-10 07:01:43 +00:00
|
|
|
DeterministicSpriteGroup *dsg = &spritegroup->g.determ;
|
|
|
|
SpriteGroup *target;
|
2004-11-14 20:50:38 +00:00
|
|
|
int value = -1;
|
|
|
|
|
|
|
|
if ((dsg->variable >> 6) == 0) {
|
|
|
|
/* General property */
|
|
|
|
value = GetDeterministicSpriteValue(dsg->variable);
|
|
|
|
|
|
|
|
} else {
|
2004-11-15 20:52:11 +00:00
|
|
|
if (stat == NULL) {
|
|
|
|
/* We are in a build dialog of something,
|
|
|
|
* and we are checking for something undefined.
|
|
|
|
* That means we should get the first target
|
|
|
|
* (NOT the default one). */
|
|
|
|
if (dsg->num_ranges > 0) {
|
|
|
|
target = &dsg->ranges[0].group;
|
|
|
|
} else {
|
|
|
|
target = dsg->default_group;
|
|
|
|
}
|
|
|
|
return ResolveStationSpriteGroup(target, NULL);
|
|
|
|
}
|
|
|
|
|
2004-11-14 20:50:38 +00:00
|
|
|
/* Station-specific property. */
|
|
|
|
if (dsg->var_scope == VSG_SCOPE_PARENT) {
|
|
|
|
/* TODO: Town structure. */
|
|
|
|
|
|
|
|
} else /* VSG_SELF */ {
|
2005-03-09 19:09:04 +00:00
|
|
|
if (dsg->variable == 0x40 || dsg->variable == 0x41) {
|
2004-11-14 20:50:38 +00:00
|
|
|
/* FIXME: This is ad hoc only
|
|
|
|
* for waypoints. */
|
|
|
|
value = 0x01010000;
|
|
|
|
} else {
|
|
|
|
/* TODO: Only small fraction done. */
|
2004-11-15 11:54:35 +00:00
|
|
|
// TTDPatch runs on little-endian arch;
|
|
|
|
// Variable is 0x70 + offset in the TTD's station structure
|
2004-11-14 20:50:38 +00:00
|
|
|
switch (dsg->variable - 0x70) {
|
|
|
|
case 0x80:
|
2004-11-15 20:52:11 +00:00
|
|
|
value = stat->facilities;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
case 0x81:
|
2004-11-15 20:52:11 +00:00
|
|
|
value = stat->airport_type;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
case 0x82:
|
2005-01-29 19:41:44 +00:00
|
|
|
value = stat->truck_stops->status;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
case 0x83:
|
2005-01-29 19:41:44 +00:00
|
|
|
value = stat->bus_stops->status;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
case 0x86:
|
2004-11-15 20:52:11 +00:00
|
|
|
value = stat->airport_flags & 0xFFFF;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
case 0x87:
|
2004-11-15 20:52:11 +00:00
|
|
|
value = stat->airport_flags & 0xFF;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
case 0x8A:
|
2004-11-15 20:52:11 +00:00
|
|
|
value = stat->build_date;
|
2004-11-14 20:50:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
target = value != -1 ? EvalDeterministicSpriteGroup(dsg, value) : dsg->default_group;
|
|
|
|
return ResolveStationSpriteGroup(target, stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2004-11-17 08:52:47 +00:00
|
|
|
case SGT_RANDOMIZED:
|
2004-11-14 20:50:38 +00:00
|
|
|
error("I don't know how to handle random spritegroups yet!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-10 07:01:43 +00:00
|
|
|
uint32 GetCustomStationRelocation(StationSpec *spec, Station *stat, byte ctype)
|
2004-11-14 16:11:48 +00:00
|
|
|
{
|
2005-03-10 07:01:43 +00:00
|
|
|
RealSpriteGroup *rsg;
|
2004-11-14 18:18:28 +00:00
|
|
|
|
2004-11-14 20:50:38 +00:00
|
|
|
rsg = ResolveStationSpriteGroup(&spec->spritegroup[ctype], stat);
|
2004-11-14 18:18:28 +00:00
|
|
|
|
|
|
|
if (rsg->sprites_per_set != 0) {
|
|
|
|
if (rsg->loading_count != 0) {
|
|
|
|
return rsg->loading[0];
|
2005-01-14 21:58:48 +00:00
|
|
|
} else if (rsg->loaded_count != 0) {
|
2004-11-14 18:18:28 +00:00
|
|
|
return rsg->loaded[0];
|
|
|
|
}
|
2004-11-14 16:11:48 +00:00
|
|
|
}
|
2004-11-14 18:18:28 +00:00
|
|
|
|
|
|
|
error("Custom station 0x%08x::0x%02x has no sprites associated.",
|
2005-03-09 19:09:04 +00:00
|
|
|
spec->grfid, spec->localidx);
|
2004-11-17 18:49:55 +00:00
|
|
|
/* This is what gets subscribed of dtss->image in newgrf.c,
|
2004-11-14 18:18:28 +00:00
|
|
|
* so it's probably kinda "default offset". Try to use it as
|
|
|
|
* emergency measure. */
|
|
|
|
return 0x42D;
|
2004-11-14 09:07:15 +00:00
|
|
|
}
|
|
|
|
|
2005-03-10 07:01:43 +00:00
|
|
|
int GetCustomStationsCount(StationClass sclass)
|
2004-11-14 09:07:15 +00:00
|
|
|
{
|
2004-11-17 18:03:33 +00:00
|
|
|
assert(sclass == STAT_CLASS_DFLT || sclass == STAT_CLASS_WAYP);
|
|
|
|
sclass--;
|
|
|
|
return _statspec_highest_id[sclass] + 1;
|
2004-11-14 09:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-03 17:57:27 +00:00
|
|
|
static int32 RemoveRailroadStation(Station *st, TileIndex tile, uint32 flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int w,h;
|
|
|
|
int32 cost;
|
|
|
|
|
2004-09-03 17:57:27 +00:00
|
|
|
/* if there is flooding and non-uniform stations are enabled, remove platforms tile by tile */
|
|
|
|
if (_current_player == OWNER_WATER && _patches.nonuniform_stations)
|
|
|
|
return DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_REMOVE_FROM_RAILROAD_STATION);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Current player owns the station? */
|
|
|
|
if (_current_player != OWNER_WATER && !CheckOwnership(st->owner))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
/* determine width and height of platforms */
|
|
|
|
tile = st->train_tile;
|
|
|
|
w = st->trainst_w;
|
|
|
|
h = st->trainst_h;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(w != 0 && h != 0);
|
|
|
|
|
|
|
|
/* cost is area * constant */
|
|
|
|
cost = w*h*_price.remove_rail_station;
|
|
|
|
|
|
|
|
/* clear all areas of the station */
|
|
|
|
do {
|
|
|
|
int w_bak = w;
|
|
|
|
do {
|
|
|
|
// for nonuniform stations, only remove tiles that are actually train station tiles
|
|
|
|
if (TileBelongsToRailStation(st, tile)) {
|
|
|
|
if (!EnsureNoVehicle(tile))
|
|
|
|
return CMD_ERROR;
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
DoClearSquare(tile);
|
|
|
|
}
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(1, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--w);
|
|
|
|
w = w_bak;
|
2005-06-25 16:44:57 +00:00
|
|
|
tile += TileDiffXY(-w, 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--h);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
st->train_tile = 0;
|
|
|
|
st->facilities &= ~FACIL_TRAIN;
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
int32 DoConvertStationRail(TileIndex tile, uint totype, bool exec)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
const Station *st = GetStation(_m[tile].m2);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR;
|
|
|
|
|
|
|
|
// tile is not a railroad station?
|
2005-07-13 18:04:01 +00:00
|
|
|
if (_m[tile].m5 >= 8) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// tile is already of requested type?
|
2005-07-19 21:49:35 +00:00
|
|
|
if ((_m[tile].m3 & 0xFU) == totype) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (exec) {
|
|
|
|
// change type.
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m3 = (_m[tile].m3 & 0xF0) + totype;
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _price.build_rail >> 1;
|
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
void FindRoadStationSpot(bool truck_station, Station *st, RoadStop ***currstop, RoadStop **prev)
|
|
|
|
{
|
|
|
|
RoadStop **primary_stop;
|
|
|
|
|
|
|
|
primary_stop = (truck_station) ? &st->truck_stops : &st->bus_stops;
|
|
|
|
|
|
|
|
if (*primary_stop == NULL) {
|
|
|
|
//we have no station of the type yet, so write a "primary station"
|
|
|
|
//(the one at st->foo_stops)
|
|
|
|
*currstop = primary_stop;
|
|
|
|
} else {
|
|
|
|
//there are stops already, so append to the end of the list
|
|
|
|
*prev = *primary_stop;
|
|
|
|
*currstop = &(*primary_stop)->next;
|
|
|
|
while (**currstop != NULL) {
|
|
|
|
*prev = (*prev)->next;
|
|
|
|
*currstop = &(**currstop)->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-09 16:37:40 +00:00
|
|
|
/** Build a bus station
|
|
|
|
* @param x,y coordinates to build bus station at
|
2005-05-09 22:33:00 +00:00
|
|
|
* @param p1 busstop entrance direction (0 through 3), where 0 is NW, 1 is NE, etc.
|
2005-05-09 16:37:40 +00:00
|
|
|
* @param p2 0 for Bus stops, 1 for truck stops
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
2005-05-09 16:37:40 +00:00
|
|
|
int32 CmdBuildRoadStop(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-09 16:37:40 +00:00
|
|
|
Station *st;
|
2005-01-29 19:41:44 +00:00
|
|
|
RoadStop *road_stop;
|
|
|
|
RoadStop **currstop;
|
|
|
|
RoadStop *prev = NULL;
|
2005-05-09 16:37:40 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 cost;
|
2005-05-09 16:37:40 +00:00
|
|
|
bool type = !!p2;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2005-05-09 16:37:40 +00:00
|
|
|
/* Saveguard the parameters */
|
|
|
|
if (p1 > 3) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
|
|
|
2005-06-25 06:15:43 +00:00
|
|
|
tile = TileVirtXY(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-05-09 16:37:40 +00:00
|
|
|
cost = CheckFlatLandBelow(tile, 1, 1, flags, 1 << p1, NULL);
|
2005-01-29 19:41:44 +00:00
|
|
|
if (cost == CMD_ERROR)
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
st = GetStationAround(tile, 1, 1, -1);
|
|
|
|
if (st == CHECK_STATIONS_ERR)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
/* Find a station close to us */
|
|
|
|
if (st == NULL) {
|
|
|
|
st = GetClosestStationFromTile(tile, 8, _current_player);
|
|
|
|
if (st!=NULL && st->facilities) st = NULL;
|
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
//give us a road stop in the list, and check if something went wrong
|
2005-02-04 15:31:30 +00:00
|
|
|
road_stop = AllocateRoadStop();
|
2005-01-29 19:41:44 +00:00
|
|
|
if (road_stop == NULL)
|
|
|
|
return_cmd_error( (type) ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
|
|
|
|
|
|
|
|
if ( st != NULL && (GetNumRoadStops(st, RS_BUS) + GetNumRoadStops(st, RS_TRUCK) >= ROAD_STOP_LIMIT))
|
|
|
|
return_cmd_error( (type) ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st != NULL) {
|
|
|
|
if (st->owner != OWNER_NONE && st->owner != _current_player)
|
|
|
|
return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!CheckStationSpreadOut(st, tile, 1, 1))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
FindRoadStationSpot(type, st, &currstop, &prev);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
Town *t;
|
|
|
|
|
|
|
|
st = AllocateStation();
|
|
|
|
if (st == NULL)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
st->town = t = ClosestTownFromTile(tile, (uint)-1);
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
FindRoadStationSpot(type, st, &currstop, &prev);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_current_player < MAX_PLAYERS && flags&DC_EXEC)
|
|
|
|
SETBIT(t->have_ratings, _current_player);
|
|
|
|
|
|
|
|
st->sign.width_1 = 0;
|
|
|
|
|
|
|
|
if (!GenerateStationName(st, tile, 0))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
StationInitialize(st, tile);
|
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
cost += (type) ? _price.build_truck_station : _price.build_bus_station;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2005-01-29 19:41:44 +00:00
|
|
|
//point to the correct item in the _busstops or _truckstops array
|
|
|
|
*currstop = road_stop;
|
|
|
|
|
|
|
|
//initialize an empty station
|
|
|
|
InitializeRoadStop(road_stop, prev, tile, st->index);
|
|
|
|
(*currstop)->type = type;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!st->facilities) st->xy = tile;
|
2005-01-29 19:41:44 +00:00
|
|
|
st->facilities |= (type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->owner = _current_player;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-17 08:40:09 +00:00
|
|
|
st->build_date = _date;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
ModifyTile(tile,
|
2004-08-09 17:04:08 +00:00
|
|
|
MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT |
|
|
|
|
MP_MAP2 | MP_MAP5 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR,
|
2005-05-09 16:37:40 +00:00
|
|
|
st->index, /* map2 parameter */
|
2005-07-13 18:04:01 +00:00
|
|
|
/* XXX - Truck stops have 0x43 _m[].m5 value + direction
|
2005-05-09 16:37:40 +00:00
|
|
|
* XXX - Bus stops have a _map5 value of 0x47 + direction */
|
|
|
|
((type) ? 0x43 : 0x47) + p1 /* map5 parameter */
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
UpdateStationAcceptance(st, false);
|
|
|
|
InvalidateWindow(WC_STATION_LIST, st->owner);
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a bus station
|
2005-01-29 19:41:44 +00:00
|
|
|
static int32 RemoveRoadStop(Station *st, uint32 flags, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-29 19:41:44 +00:00
|
|
|
RoadStop **primary_stop;
|
|
|
|
RoadStop *cur_stop;
|
2005-07-13 18:04:01 +00:00
|
|
|
bool is_truck = _m[tile].m5 < 0x47;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (_current_player != OWNER_WATER && !CheckOwnership(st->owner))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
if (is_truck) { //truck stop
|
|
|
|
primary_stop = &st->truck_stops;
|
|
|
|
cur_stop = GetRoadStopByTile(tile, RS_TRUCK);
|
|
|
|
} else {
|
|
|
|
primary_stop = &st->bus_stops;
|
|
|
|
cur_stop = GetRoadStopByTile(tile, RS_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
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
if (!EnsureNoVehicle(tile))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2005-04-16 20:10:24 +00:00
|
|
|
int i;
|
2005-01-29 19:41:44 +00:00
|
|
|
DoClearSquare(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-04-16 20:10:24 +00:00
|
|
|
/* Clear all vehicles destined for this station */
|
|
|
|
for (i = 0; i != NUM_SLOTS; i++) {
|
|
|
|
if (cur_stop->slot[i] != INVALID_SLOT) {
|
|
|
|
Vehicle *v = GetVehicle(cur_stop->slot[i]);
|
|
|
|
ClearSlot(v, v->u.road.slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
cur_stop->used = false;
|
|
|
|
if (cur_stop->prev != NULL) //alter previous stop
|
|
|
|
cur_stop->prev->next = cur_stop->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
if (cur_stop->next != NULL) //alter next stop
|
|
|
|
cur_stop->next->prev = cur_stop->prev;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
//we only had one stop left
|
|
|
|
if (cur_stop->next == NULL && cur_stop->prev == NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
//so we remove ALL stops
|
|
|
|
*primary_stop = NULL;
|
|
|
|
st->facilities &= (is_truck) ? ~FACIL_TRUCK_STOP : ~FACIL_BUS_STOP;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
} else if (cur_stop == *primary_stop) {
|
|
|
|
//removed the first stop in the list
|
|
|
|
//need to set the primary element to the next stop
|
|
|
|
*primary_stop = (*primary_stop)->next;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
return (is_truck) ? _price.remove_truck_station : _price.remove_bus_station;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// FIXME -- need to move to its corresponding Airport variable
|
|
|
|
// Country Airfield (small)
|
|
|
|
static const byte _airport_map5_tiles_country[] = {
|
|
|
|
54, 53, 52, 65,
|
|
|
|
58, 57, 56, 55,
|
|
|
|
64, 63, 63, 62
|
|
|
|
};
|
|
|
|
|
|
|
|
// City Airport (large)
|
|
|
|
static const byte _airport_map5_tiles_town[] = {
|
|
|
|
31, 9, 33, 9, 9, 32,
|
|
|
|
27, 36, 29, 34, 8, 10,
|
|
|
|
30, 11, 35, 13, 20, 21,
|
|
|
|
51, 12, 14, 17, 19, 28,
|
|
|
|
38, 13, 15, 16, 18, 39,
|
|
|
|
26, 22, 23, 24, 25, 26
|
|
|
|
};
|
|
|
|
|
|
|
|
// Metropolitain Airport (large) - 2 runways
|
|
|
|
static const byte _airport_map5_tiles_metropolitan[] = {
|
|
|
|
31, 9, 33, 9, 9, 32,
|
|
|
|
27, 36, 29, 34, 8, 10,
|
|
|
|
30, 11, 35, 13, 20, 21,
|
|
|
|
102, 8, 8, 8, 8, 28,
|
|
|
|
83, 84, 84, 84, 84, 83,
|
|
|
|
26, 23, 23, 23, 23, 26
|
|
|
|
};
|
|
|
|
|
|
|
|
// International Airport (large) - 2 runways
|
|
|
|
static const byte _airport_map5_tiles_international[] = {
|
|
|
|
88, 89, 89, 89, 89, 89, 88,
|
|
|
|
51, 8, 8, 8, 8, 8, 32,
|
|
|
|
30, 8, 11, 27, 11, 8, 10,
|
|
|
|
32, 8, 11, 27, 11, 8, 114,
|
|
|
|
87, 8, 11, 85, 11, 8, 114,
|
|
|
|
87, 8, 8, 8, 8, 8, 90,
|
|
|
|
26, 23, 23, 23, 23, 23, 26
|
|
|
|
};
|
|
|
|
|
|
|
|
// Heliport
|
|
|
|
static const byte _airport_map5_tiles_heliport[] = {
|
|
|
|
66,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte * const _airport_map5_tiles[] = {
|
|
|
|
_airport_map5_tiles_country, // Country Airfield (small)
|
|
|
|
_airport_map5_tiles_town, // City Airport (large)
|
|
|
|
_airport_map5_tiles_heliport, // Heliport
|
2004-09-10 19:02:27 +00:00
|
|
|
_airport_map5_tiles_metropolitan, // Metropolitain Airport (large)
|
2004-08-09 17:04:08 +00:00
|
|
|
_airport_map5_tiles_international, // International Airport (xlarge)
|
|
|
|
};
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Place an Airport.
|
|
|
|
* @param x,y tile coordinates where airport will be built
|
|
|
|
* @param p1 airport type, @see airport.h
|
|
|
|
* @param p2 unused
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
|
|
|
int32 CmdBuildAirport(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2005-05-09 22:33:00 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
Town *t;
|
|
|
|
Station *st;
|
|
|
|
int32 cost;
|
2005-05-09 22:33:00 +00:00
|
|
|
int w, h;
|
2004-09-10 19:02:27 +00:00
|
|
|
bool airport_upgrade = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/* Check if a valid, buildable airport was chosen for construction */
|
2005-05-30 13:35:39 +00:00
|
|
|
if (p1 > lengthof(_airport_map5_tiles) || !HASBIT(GetValidAirports(), p1)) return CMD_ERROR;
|
2005-05-09 22:33:00 +00:00
|
|
|
|
2005-06-25 06:15:43 +00:00
|
|
|
tile = TileVirtXY(x, y);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
t = ClosestTownFromTile(tile, (uint)-1);
|
|
|
|
|
|
|
|
/* Check if local auth refuses a new airport */
|
|
|
|
{
|
|
|
|
uint num = 0;
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0 && st->town == t && st->facilities&FACIL_AIRPORT && st->airport_type != AT_OILRIG)
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
if (num >= 2) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_2035_LOCAL_AUTHORITY_REFUSES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w = _airport_size_x[p1];
|
|
|
|
h = _airport_size_y[p1];
|
|
|
|
|
|
|
|
cost = CheckFlatLandBelow(tile, w, h, flags, 0, NULL);
|
2005-05-11 00:00:27 +00:00
|
|
|
if (CmdFailed(cost)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
st = GetStationAround(tile, w, h, -1);
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Find a station close to us */
|
|
|
|
if (st == NULL) {
|
|
|
|
st = GetClosestStationFromTile(tile, 8, _current_player);
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st != NULL && st->facilities) st = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (st != NULL) {
|
|
|
|
if (st->owner != OWNER_NONE && st->owner != _current_player)
|
|
|
|
return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!CheckStationSpreadOut(st, tile, 1, 1))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (st->airport_tile != 0)
|
|
|
|
return_cmd_error(STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT);
|
|
|
|
} else {
|
|
|
|
airport_upgrade = false;
|
|
|
|
|
|
|
|
st = AllocateStation();
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st == NULL) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-02 15:45:53 +00:00
|
|
|
st->town = t;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (_current_player < MAX_PLAYERS && flags&DC_EXEC)
|
|
|
|
SETBIT(t->have_ratings, _current_player);
|
|
|
|
|
|
|
|
st->sign.width_1 = 0;
|
|
|
|
|
|
|
|
// if airport type equals Heliport then generate
|
|
|
|
// type 5 name, which is heliport, otherwise airport names (1)
|
2005-05-09 22:33:00 +00:00
|
|
|
if (!GenerateStationName(st, tile, (p1 == AT_HELIPORT) ? 5 : 1))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
StationInitialize(st, tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
cost += _price.build_airport * w * h;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2004-12-18 12:46:29 +00:00
|
|
|
const AirportFTAClass *afc = GetAirport(p1);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
st->owner = _current_player;
|
2004-12-18 12:46:29 +00:00
|
|
|
if (_current_player == _local_player && afc->nof_depots != 0) {
|
2005-01-06 11:39:00 +00:00
|
|
|
_last_built_aircraft_depot_tile = tile + ToTileIndexDiff(afc->airport_depots[0]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st->airport_tile = tile;
|
|
|
|
if (!st->facilities) st->xy = tile;
|
|
|
|
st->facilities |= FACIL_AIRPORT;
|
|
|
|
st->airport_type = (byte)p1;
|
|
|
|
st->airport_flags = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-17 08:40:09 +00:00
|
|
|
st->build_date = _date;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* if airport was demolished while planes were en-route to it, the positions can no longer
|
|
|
|
be the same (v->u.air.pos), since different airports have different indexes. So update
|
|
|
|
all planes en-route to this airport. Only update if
|
|
|
|
1. airport is upgraded
|
|
|
|
2. airport is added to existing station (unfortunately unavoideable)
|
|
|
|
*/
|
|
|
|
if (airport_upgrade) {UpdateAirplanesOnNewStation(st);}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
const byte *b = _airport_map5_tiles[p1];
|
|
|
|
BEGIN_TILE_LOOP(tile_cur,w,h,tile)
|
2004-09-10 19:02:27 +00:00
|
|
|
ModifyTile(tile_cur,
|
2004-08-09 17:04:08 +00:00
|
|
|
MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT |
|
|
|
|
MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAP5,
|
|
|
|
st->index, *b++);
|
|
|
|
END_TILE_LOOP(tile_cur,w,h,tile)
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
UpdateStationAcceptance(st, false);
|
|
|
|
InvalidateWindow(WC_STATION_LIST, st->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32 RemoveAirport(Station *st, uint32 flags)
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
int w,h;
|
|
|
|
int32 cost;
|
|
|
|
|
|
|
|
if (_current_player != OWNER_WATER && !CheckOwnership(st->owner))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
tile = st->airport_tile;
|
|
|
|
|
|
|
|
w = _airport_size_x[st->airport_type];
|
|
|
|
h = _airport_size_y[st->airport_type];
|
|
|
|
|
|
|
|
cost = w * h * _price.remove_airport;
|
|
|
|
|
|
|
|
{
|
|
|
|
BEGIN_TILE_LOOP(tile_cur,w,h,tile)
|
|
|
|
if (!EnsureNoVehicle(tile_cur))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
DeleteAnimatedTile(tile_cur);
|
|
|
|
DoClearSquare(tile_cur);
|
|
|
|
}
|
|
|
|
END_TILE_LOOP(tile_cur, w,h,tile)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2004-12-18 12:40:34 +00:00
|
|
|
const AirportFTAClass *afc = GetAirport(st->airport_type);
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i < afc->nof_depots; ++i)
|
2005-01-06 11:39:00 +00:00
|
|
|
DeleteWindowById(WC_VEHICLE_DEPOT, tile + ToTileIndexDiff(afc->airport_depots[i]));
|
2004-12-21 23:27:58 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
st->airport_tile = 0;
|
|
|
|
st->facilities &= ~FACIL_AIRPORT;
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Build a buoy.
|
|
|
|
* @param x,y tile coordinates of bouy construction
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2004-08-09 17:04:08 +00:00
|
|
|
*/
|
|
|
|
int32 CmdBuildBuoy(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
TileInfo ti;
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
|
|
|
|
|
|
FindLandscapeHeight(&ti, x, y);
|
|
|
|
|
|
|
|
if (ti.type != MP_WATER || ti.tileh != 0 || ti.map5 != 0 || ti.tile == 0)
|
|
|
|
return_cmd_error(STR_304B_SITE_UNSUITABLE);
|
|
|
|
|
|
|
|
st = AllocateStation();
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st == NULL) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
st->town = ClosestTownFromTile(ti.tile, (uint)-1);
|
|
|
|
st->sign.width_1 = 0;
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
if (!GenerateStationName(st, ti.tile, 4)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
StationInitialize(st, ti.tile);
|
|
|
|
st->dock_tile = ti.tile;
|
|
|
|
st->facilities |= FACIL_DOCK;
|
2005-05-02 22:13:20 +00:00
|
|
|
/* Buoys are marked in the Station struct by this flag. Yes, it is this
|
|
|
|
* braindead.. */
|
2004-08-09 17:04:08 +00:00
|
|
|
st->had_vehicle_of_type |= HVOT_BUOY;
|
|
|
|
st->owner = OWNER_NONE;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-17 08:40:09 +00:00
|
|
|
st->build_date = _date;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ModifyTile(ti.tile,
|
|
|
|
MP_SETTYPE(MP_STATION) |
|
|
|
|
MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5,
|
|
|
|
st->index, /* map2 */
|
|
|
|
OWNER_NONE, /* map_owner */
|
|
|
|
0x52 /* map5 */
|
|
|
|
);
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateStationAcceptance(st, false);
|
|
|
|
InvalidateWindow(WC_STATION_LIST, st->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _price.build_dock;
|
|
|
|
}
|
|
|
|
|
2005-01-20 22:19:34 +00:00
|
|
|
/* Checks if any ship is servicing the buoy specified. Returns yes or no */
|
|
|
|
static bool CheckShipsOnBuoy(Station *st)
|
|
|
|
{
|
|
|
|
const Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Ship) {
|
|
|
|
const Order *order;
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
if (order->type == OT_GOTO_STATION && order->station == st->index) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static int32 RemoveBuoy(Station *st, uint32 flags)
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (_current_player >= MAX_PLAYERS) {
|
|
|
|
/* XXX: strange stuff */
|
|
|
|
return_cmd_error(INVALID_STRING_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
tile = st->dock_tile;
|
|
|
|
|
2005-01-20 22:19:34 +00:00
|
|
|
if (CheckShipsOnBuoy(st))
|
|
|
|
return_cmd_error(STR_BUOY_IS_IN_USE);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!EnsureNoVehicle(tile))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
st->dock_tile = 0;
|
2005-05-02 22:13:20 +00:00
|
|
|
/* Buoys are marked in the Station struct by this flag. Yes, it is this
|
|
|
|
* braindead.. */
|
2004-08-09 17:04:08 +00:00
|
|
|
st->facilities &= ~FACIL_DOCK;
|
|
|
|
st->had_vehicle_of_type &= ~HVOT_BUOY;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
ModifyTile(tile,
|
2004-08-09 17:04:08 +00:00
|
|
|
MP_SETTYPE(MP_WATER) |
|
|
|
|
MP_MAP2_CLEAR | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5 | MP_MAP2_CLEAR,
|
|
|
|
OWNER_WATER, /* map_owner */
|
|
|
|
0 /* map5 */
|
|
|
|
);
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _price.remove_truck_station;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
static const byte _dock_w_chk[4] = { 2,1,2,1 };
|
|
|
|
static const byte _dock_h_chk[4] = { 1,2,1,2 };
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
/** Build a dock/haven.
|
|
|
|
* @param x,y tile coordinates where dock will be built
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdBuildDock(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
TileInfo ti;
|
|
|
|
int direction;
|
|
|
|
int32 cost;
|
2005-05-09 22:33:00 +00:00
|
|
|
TileIndex tile, tile_cur;
|
2004-08-09 17:04:08 +00:00
|
|
|
Station *st;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
|
|
|
|
|
|
|
|
FindLandscapeHeight(&ti, x, y);
|
|
|
|
|
|
|
|
if ((direction=0,ti.tileh) != 3 &&
|
|
|
|
(direction++,ti.tileh) != 9 &&
|
|
|
|
(direction++,ti.tileh) != 12 &&
|
|
|
|
(direction++,ti.tileh) != 6)
|
|
|
|
return_cmd_error(STR_304B_SITE_UNSUITABLE);
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
if (!EnsureNoVehicle(ti.tile)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
cost = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
2005-05-09 22:33:00 +00:00
|
|
|
if (CmdFailed(cost)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
tile_cur = (tile=ti.tile) + TileOffsByDir(direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
if (!EnsureNoVehicle(tile_cur)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FindLandscapeHeightByTile(&ti, tile_cur);
|
2005-05-09 22:33:00 +00:00
|
|
|
if (ti.tileh != 0 || ti.type != MP_WATER) return_cmd_error(STR_304B_SITE_UNSUITABLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
cost = DoCommandByTile(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
|
2005-05-09 22:33:00 +00:00
|
|
|
if (CmdFailed(cost)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
tile_cur = tile_cur + TileOffsByDir(direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
FindLandscapeHeightByTile(&ti, tile_cur);
|
|
|
|
if (ti.tileh != 0 || ti.type != MP_WATER)
|
|
|
|
return_cmd_error(STR_304B_SITE_UNSUITABLE);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* middle */
|
2005-01-06 11:39:00 +00:00
|
|
|
st = GetStationAround(
|
|
|
|
tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]),
|
2004-08-09 17:04:08 +00:00
|
|
|
_dock_w_chk[direction], _dock_h_chk[direction], -1);
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Find a station close to us */
|
|
|
|
if (st == NULL) {
|
|
|
|
st = GetClosestStationFromTile(tile, 8, _current_player);
|
|
|
|
if (st!=NULL && st->facilities) st = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st != NULL) {
|
|
|
|
if (st->owner != OWNER_NONE && st->owner != _current_player)
|
|
|
|
return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
if (!CheckStationSpreadOut(st, tile, 1, 1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st->dock_tile != 0) return_cmd_error(STR_304C_TOO_CLOSE_TO_ANOTHER_DOCK);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
Town *t;
|
|
|
|
|
|
|
|
st = AllocateStation();
|
2005-05-09 22:33:00 +00:00
|
|
|
if (st == NULL) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
st->town = t = ClosestTownFromTile(tile, (uint)-1);
|
|
|
|
|
|
|
|
if (_current_player < MAX_PLAYERS && flags&DC_EXEC)
|
|
|
|
SETBIT(t->have_ratings, _current_player);
|
|
|
|
|
|
|
|
st->sign.width_1 = 0;
|
|
|
|
|
2005-05-09 22:33:00 +00:00
|
|
|
if (!GenerateStationName(st, tile, 3)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
StationInitialize(st, tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
st->dock_tile = tile;
|
|
|
|
if (!st->facilities) st->xy = tile;
|
|
|
|
st->facilities |= FACIL_DOCK;
|
|
|
|
st->owner = _current_player;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-17 08:40:09 +00:00
|
|
|
st->build_date = _date;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
ModifyTile(tile,
|
|
|
|
MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT |
|
2004-08-09 17:04:08 +00:00
|
|
|
MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR |
|
|
|
|
MP_MAP5,
|
|
|
|
st->index,
|
|
|
|
direction + 0x4C);
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
ModifyTile(tile + TileOffsByDir(direction),
|
2004-09-10 19:02:27 +00:00
|
|
|
MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT |
|
2004-08-09 17:04:08 +00:00
|
|
|
MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR |
|
|
|
|
MP_MAP5,
|
|
|
|
st->index,
|
|
|
|
(direction&1) + 0x50);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
UpdateStationAcceptance(st, false);
|
|
|
|
InvalidateWindow(WC_STATION_LIST, st->owner);
|
|
|
|
}
|
|
|
|
return _price.build_dock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32 RemoveDock(Station *st, uint32 flags)
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile1;
|
|
|
|
TileIndex tile2;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!CheckOwnership(st->owner))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
tile1 = st->dock_tile;
|
2005-07-13 18:04:01 +00:00
|
|
|
tile2 = tile1 + TileOffsByDir(_m[tile1].m5 - 0x4C);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!EnsureNoVehicle(tile1))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (!EnsureNoVehicle(tile2))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
DoClearSquare(tile1);
|
|
|
|
|
|
|
|
// convert the water tile to water.
|
|
|
|
ModifyTile(tile2, MP_SETTYPE(MP_WATER) | MP_MAPOWNER | MP_MAP5 | MP_MAP2_CLEAR | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR, OWNER_WATER, 0);
|
|
|
|
|
|
|
|
st->dock_tile = 0;
|
|
|
|
st->facilities &= ~FACIL_DOCK;
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStationIfEmpty(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _price.remove_dock;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "table/station_land.h"
|
|
|
|
|
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
extern uint16 _custom_sprites_base;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void DrawTile_Station(TileInfo *ti)
|
|
|
|
{
|
|
|
|
uint32 image_or_modificator;
|
2004-11-17 18:03:33 +00:00
|
|
|
uint32 image;
|
2004-08-09 17:04:08 +00:00
|
|
|
const DrawTileSeqStruct *dtss;
|
2004-11-17 18:03:33 +00:00
|
|
|
const DrawTileSprites *t = NULL;
|
2005-07-13 18:04:01 +00:00
|
|
|
byte railtype = _m[ti->tile].m3 & 0xF;
|
2005-07-31 22:53:57 +00:00
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(railtype);
|
|
|
|
SpriteID offset;
|
2004-11-17 18:03:33 +00:00
|
|
|
uint32 relocation = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
{
|
2005-06-04 11:56:32 +00:00
|
|
|
uint owner = GetTileOwner(ti->tile);
|
2005-07-24 15:56:31 +00:00
|
|
|
image_or_modificator = PALETTE_TO_GREY; /* NOTE: possible bug in ttd here? */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (owner < MAX_PLAYERS)
|
|
|
|
image_or_modificator = PLAYER_SPRITE_COLOR(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't show foundation for docks (docks are between 76 (0x4C) and 81 (0x51))
|
|
|
|
if (ti->tileh != 0 && (ti->map5 < 0x4C || ti->map5 > 0x51))
|
|
|
|
DrawFoundation(ti, ti->tileh);
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
if (_m[ti->tile].m3 & 0x10) {
|
2004-11-17 18:03:33 +00:00
|
|
|
// look for customization
|
2005-07-13 18:04:01 +00:00
|
|
|
StationSpec *statspec = GetCustomStation(STAT_CLASS_DFLT, _m[ti->tile].m4);
|
2004-11-17 18:03:33 +00:00
|
|
|
|
|
|
|
//debug("Cust-o-mized %p", statspec);
|
|
|
|
|
|
|
|
if (statspec != NULL) {
|
2005-07-13 18:04:01 +00:00
|
|
|
Station *st = GetStation(_m[ti->tile].m2);
|
2004-11-17 18:03:33 +00:00
|
|
|
|
|
|
|
relocation = GetCustomStationRelocation(statspec, st, 0);
|
|
|
|
//debug("Relocation %d", relocation);
|
|
|
|
t = &statspec->renderdata[ti->map5];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t == NULL) t = &_station_display_datas[ti->map5];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-14 01:25:05 +00:00
|
|
|
image = t->ground_sprite;
|
2005-07-24 15:56:31 +00:00
|
|
|
if (image & PALETTE_MODIFIER_COLOR)
|
2004-08-09 17:04:08 +00:00
|
|
|
image |= image_or_modificator;
|
2004-11-17 18:03:33 +00:00
|
|
|
|
2005-07-04 14:58:55 +00:00
|
|
|
// For custom sprites, there's no railtype-based pitching.
|
2005-07-31 22:53:57 +00:00
|
|
|
offset = (image & SPRITE_MASK) < _custom_sprites_base ? rti->total_offset : railtype;
|
|
|
|
image += offset;
|
2005-07-04 14:58:55 +00:00
|
|
|
|
2004-11-17 18:03:33 +00:00
|
|
|
// station_land array has been increased from 82 elements to 114
|
|
|
|
// but this is something else. If AI builds station with 114 it looks all weird
|
|
|
|
DrawGroundSprite(image);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-04 14:58:55 +00:00
|
|
|
if (_debug_pbs_level >= 1) {
|
|
|
|
byte pbs = PBSTileReserved(ti->tile);
|
2005-07-31 22:53:57 +00:00
|
|
|
if (pbs & TRACK_BIT_DIAG1) DrawGroundSprite(rti->base_sprites.single_y | PALETTE_CRASH);
|
|
|
|
if (pbs & TRACK_BIT_DIAG2) DrawGroundSprite(rti->base_sprites.single_x | PALETTE_CRASH);
|
|
|
|
if (pbs & TRACK_BIT_UPPER) DrawGroundSprite(rti->base_sprites.single_n | PALETTE_CRASH);
|
|
|
|
if (pbs & TRACK_BIT_LOWER) DrawGroundSprite(rti->base_sprites.single_s | PALETTE_CRASH);
|
|
|
|
if (pbs & TRACK_BIT_LEFT) DrawGroundSprite(rti->base_sprites.single_w | PALETTE_CRASH);
|
|
|
|
if (pbs & TRACK_BIT_RIGHT) DrawGroundSprite(rti->base_sprites.single_e | PALETTE_CRASH);
|
2005-07-04 14:58:55 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 01:25:05 +00:00
|
|
|
foreach_draw_tile_seq(dtss, t->seq) {
|
2005-07-04 14:58:55 +00:00
|
|
|
image = dtss->image + relocation;
|
2005-07-31 22:53:57 +00:00
|
|
|
image += offset;
|
2004-11-14 01:25:05 +00:00
|
|
|
if (_display_opt & DO_TRANS_BUILDINGS) {
|
2005-07-20 22:05:13 +00:00
|
|
|
MAKE_TRANSPARENT(image);
|
2004-11-23 22:36:11 +00:00
|
|
|
} else {
|
2005-07-24 15:56:31 +00:00
|
|
|
if (image & PALETTE_MODIFIER_COLOR) image |= image_or_modificator;
|
2004-11-14 01:25:05 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-14 01:25:05 +00:00
|
|
|
if ((byte)dtss->delta_z != 0x80) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AddSortableSpriteToDraw(image, ti->x + dtss->delta_x, ti->y + dtss->delta_y, dtss->width, dtss->height, dtss->unk, ti->z + dtss->delta_z);
|
|
|
|
} else {
|
|
|
|
AddChildSpriteScreen(image, dtss->delta_x, dtss->delta_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StationPickerDrawSprite(int x, int y, int railtype, int image)
|
|
|
|
{
|
|
|
|
uint32 ormod, img;
|
|
|
|
const DrawTileSeqStruct *dtss;
|
2004-11-14 01:25:05 +00:00
|
|
|
const DrawTileSprites *t;
|
2005-07-31 22:53:57 +00:00
|
|
|
const RailtypeInfo *rti = GetRailTypeInfo(railtype);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ormod = PLAYER_SPRITE_COLOR(_local_player);
|
|
|
|
|
2004-11-14 01:25:05 +00:00
|
|
|
t = &_station_display_datas[image];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-14 01:25:05 +00:00
|
|
|
img = t->ground_sprite;
|
2005-07-24 15:56:31 +00:00
|
|
|
if (img & PALETTE_MODIFIER_COLOR)
|
2004-08-09 17:04:08 +00:00
|
|
|
img |= ormod;
|
2005-07-31 22:53:57 +00:00
|
|
|
DrawSprite(img + rti->total_offset, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-14 01:25:05 +00:00
|
|
|
foreach_draw_tile_seq(dtss, t->seq) {
|
2004-08-09 17:04:08 +00:00
|
|
|
Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
|
2005-07-31 22:53:57 +00:00
|
|
|
DrawSprite((dtss->image | ormod) + rti->total_offset, x + pt.x, y + pt.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint GetSlopeZ_Station(TileInfo *ti)
|
|
|
|
{
|
|
|
|
uint z = ti->z;
|
|
|
|
if (ti->tileh != 0)
|
|
|
|
z += 8;
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
2004-08-13 18:27:33 +00:00
|
|
|
static uint GetSlopeTileh_Station(TileInfo *ti)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void GetAcceptedCargo_Station(TileIndex tile, AcceptedCargo ac)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* not used */
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void GetTileDesc_Station(TileIndex tile, TileDesc *td)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte m5;
|
|
|
|
StringID str;
|
|
|
|
|
2005-06-04 11:56:32 +00:00
|
|
|
td->owner = GetTileOwner(tile);
|
2005-07-13 18:04:01 +00:00
|
|
|
td->build_date = GetStation(_m[tile].m2)->build_date;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
m5 = _m[tile].m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
(str=STR_305E_RAILROAD_STATION, m5 < 8) ||
|
|
|
|
(str=STR_305F_AIRCRAFT_HANGAR, m5==32 || m5==45) || // hangars
|
|
|
|
(str=STR_3060_AIRPORT, m5 < 0x43 || (m5 >= 83 && m5 <= 114)) ||
|
|
|
|
(str=STR_3061_TRUCK_LOADING_AREA, m5 < 0x47) ||
|
|
|
|
(str=STR_3062_BUS_STATION, m5 < 0x4B) ||
|
|
|
|
(str=STR_4807_OIL_RIG, m5 == 0x4B) ||
|
|
|
|
(str=STR_3063_SHIP_DOCK, m5 != 0x52) ||
|
|
|
|
(str=STR_3069_BUOY, true);
|
|
|
|
td->str = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static uint32 GetTileTrackStatus_Station(TileIndex tile, TransportType mode)
|
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
uint i = _m[tile].m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint j = 0;
|
|
|
|
|
2005-01-16 09:51:56 +00:00
|
|
|
switch (mode) {
|
|
|
|
case TRANSPORT_RAIL:
|
|
|
|
if (i < 8) {
|
|
|
|
const byte tile_track_status_rail[8] = { 1, 2, 1, 2, 1, 2, 1, 2 };
|
|
|
|
j = tile_track_status_rail[i];
|
|
|
|
}
|
|
|
|
j += (j << 8);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRANSPORT_WATER:
|
|
|
|
// buoy is coded as a station, it is always on open water
|
|
|
|
// (0x3F, all tracks available)
|
|
|
|
if (i == 0x52) j = 0x3F;
|
|
|
|
j += (j << 8);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-16 09:51:56 +00:00
|
|
|
|
2004-09-05 16:15:22 +00:00
|
|
|
return j;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void TileLoop_Station(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
//FIXME -- GetTileTrackStatus_Station -> animated stationtiles
|
|
|
|
// hardcoded.....not good
|
|
|
|
// 0x27 - large big airport (39)
|
|
|
|
// 0x66 - radar metropolitan airport (102)
|
|
|
|
// 0x5A - radar international airport (90)
|
|
|
|
// 0x3A - flag small airport (58)
|
2005-07-13 18:04:01 +00:00
|
|
|
if (_m[tile].m5 == 39 || _m[tile].m5 == 58 || _m[tile].m5 == 90 || _m[tile].m5 == 102)
|
2004-08-09 17:04:08 +00:00
|
|
|
AddAnimatedTile(tile);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// treat a bouy tile as water.
|
2005-07-13 18:04:01 +00:00
|
|
|
else if (_m[tile].m5 == 0x52)
|
2004-08-09 17:04:08 +00:00
|
|
|
TileLoop_Water(tile);
|
2004-08-13 19:52:45 +00:00
|
|
|
|
|
|
|
// treat a oilrig (the station part) as water
|
2005-07-13 18:04:01 +00:00
|
|
|
else if (_m[tile].m5 == 0x4B)
|
2004-08-13 19:52:45 +00:00
|
|
|
TileLoop_Water(tile);
|
|
|
|
|
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
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
byte m5 = _m[tile].m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
//FIXME -- AnimateTile_Station -> not nice code, lots of things double
|
|
|
|
// again hardcoded...was a quick hack
|
|
|
|
|
|
|
|
// turning radar / windsack on airport
|
|
|
|
if (m5 >= 39 && m5 <= 50) { // turning radar (39 - 50)
|
|
|
|
if (_tick_counter & 3)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (++m5 == 50+1)
|
|
|
|
m5 = 39;
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m5 = m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
//added - begin
|
|
|
|
} else if (m5 >= 90 && m5 <= 113) { // turning radar with ground under it (different fences) (90 - 101 | 102 - 113)
|
|
|
|
if (_tick_counter & 3)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
m5++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (m5 == 101+1) {m5 = 90;} // radar with fences in south
|
|
|
|
else if (m5 == 113+1) {m5 = 102;} // radar with fences in north
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m5 = m5;
|
2004-09-10 19:02:27 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
//added - end
|
|
|
|
} else if (m5 >= 0x3A && m5 <= 0x3D) { // windsack (58 - 61)
|
|
|
|
if (_tick_counter & 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (++m5 == 0x3D+1)
|
|
|
|
m5 = 0x3A;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m5 = m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void ClickTile_Station(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
// 0x20 - hangar large airport (32)
|
|
|
|
// 0x41 - hangar small airport (65)
|
2005-07-13 18:04:01 +00:00
|
|
|
if (_m[tile].m5 == 32 || _m[tile].m5 == 65) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowAircraftDepotWindow(tile);
|
|
|
|
} else {
|
2005-07-13 18:04:01 +00:00
|
|
|
ShowStationViewWindow(_m[tile].m2);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _enter_station_speedtable[12] = {
|
|
|
|
215, 195, 175, 155, 135, 115, 95, 75, 55, 35, 15, 0
|
|
|
|
};
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static uint32 VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-25 10:40:58 +00:00
|
|
|
StationID station_id;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte dir;
|
|
|
|
uint16 spd;
|
|
|
|
|
|
|
|
if (v->type == VEH_Train) {
|
2005-07-13 18:04:01 +00:00
|
|
|
if (IS_BYTE_INSIDE(_m[tile].m5, 0, 8) && v->subtype == TS_Front_Engine &&
|
2005-04-12 10:31:26 +00:00
|
|
|
!IsCompatibleTrainStationTile(tile + TileOffsByDir(v->direction >> 1), tile)) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
station_id = _m[tile].m2;
|
2004-12-05 12:43:04 +00:00
|
|
|
if ((!(v->current_order.flags & OF_NON_STOP) && !_patches.new_nonstop) ||
|
|
|
|
(v->current_order.type == OT_GOTO_STATION && v->current_order.station == station_id)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (!(_patches.new_nonstop && v->current_order.flags & OF_NON_STOP) &&
|
|
|
|
v->current_order.type != OT_LEAVESTATION &&
|
|
|
|
v->last_station_visited != station_id) {
|
2004-08-09 17:04:08 +00:00
|
|
|
x &= 0xF;
|
|
|
|
y &= 0xF;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
dir = v->direction & 6;
|
|
|
|
if (dir & 2) intswap(x,y);
|
|
|
|
if (y == 8) {
|
|
|
|
if (dir != 2 && dir != 4) {
|
|
|
|
x = (~x)&0xF;
|
|
|
|
}
|
|
|
|
if (x == 12) return 2 | (station_id << 8); /* enter station */
|
|
|
|
if (x < 12) {
|
|
|
|
v->vehstatus |= VS_TRAIN_SLOWING;
|
|
|
|
spd = _enter_station_speedtable[x];
|
|
|
|
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
|
|
|
}
|
|
|
|
} else if (v->type == VEH_Road) {
|
|
|
|
if (v->u.road.state < 16 && (v->u.road.state&4)==0 && v->u.road.frame==0) {
|
2005-07-13 18:04:01 +00:00
|
|
|
byte m5 = _m[tile].m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte *b, bb,state;
|
|
|
|
|
|
|
|
if (IS_BYTE_INSIDE(m5, 0x43, 0x4B)) {
|
2005-01-29 19:41:44 +00:00
|
|
|
RoadStop *rs = GetRoadStopByTile(tile, GetRoadStopType(tile));
|
|
|
|
b = &rs->status;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
bb = *b;
|
|
|
|
|
|
|
|
/* bb bits 1..0 describe current the two parking spots.
|
|
|
|
0 means occupied, 1 means free. */
|
|
|
|
|
|
|
|
// Station busy?
|
|
|
|
if (bb & 0x80 || (bb&3) == 0)
|
|
|
|
return 8;
|
|
|
|
|
|
|
|
state = v->u.road.state + 32;
|
|
|
|
if (bb & 1) {
|
|
|
|
bb &= ~1;
|
|
|
|
} else {
|
|
|
|
bb &= ~2;
|
|
|
|
state += 2;
|
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
|
|
|
bb |= 0x80;
|
2004-08-09 17:04:08 +00:00
|
|
|
*b = bb;
|
|
|
|
v->u.road.state = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-28 05:47:54 +00:00
|
|
|
/** Removes a station from the list.
|
|
|
|
* This is done by setting the .xy property to 0,
|
|
|
|
* and doing some maintenance, especially clearing vehicle orders.
|
|
|
|
* Aircraft-Hangar orders need special treatment here, as the hangars are
|
|
|
|
* actually part of a station (tiletype is STATION), but the order type
|
|
|
|
* is OT_GOTO_DEPOT.
|
|
|
|
* @param st Station to be deleted
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
static void DeleteStation(Station *st)
|
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
Order order;
|
2005-03-25 10:40:58 +00:00
|
|
|
StationID index;
|
2005-07-28 05:47:54 +00:00
|
|
|
Vehicle *v;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->xy = 0;
|
|
|
|
|
|
|
|
DeleteName(st->string_id);
|
|
|
|
MarkStationDirty(st);
|
2004-09-14 14:19:53 +00:00
|
|
|
_global_station_sort_dirty = true; // delete station, remove sign
|
2004-08-16 14:48:35 +00:00
|
|
|
InvalidateWindowClasses(WC_STATION_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
index = st->index;
|
|
|
|
DeleteWindowById(WC_STATION_VIEW, index);
|
2005-01-15 19:06:22 +00:00
|
|
|
|
2005-07-28 05:47:54 +00:00
|
|
|
//Now delete all orders that go to the station
|
2004-12-05 12:43:04 +00:00
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
|
order.station = index;
|
2005-01-15 19:06:22 +00:00
|
|
|
DeleteDestinationFromVehicleOrder(order);
|
|
|
|
|
2005-07-28 05:47:54 +00:00
|
|
|
//And do the same with aircraft that have the station as a hangar-stop
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
bool invalidate = false;
|
|
|
|
if (v->type == VEH_Aircraft) {
|
|
|
|
Order *order;
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
if (order->type == OT_GOTO_DEPOT && order->station == index) {
|
|
|
|
order->type = OT_DUMMY;
|
|
|
|
order->flags = 0;
|
|
|
|
invalidate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Orders for the vehicle have been changed, invalidate the window
|
|
|
|
if (invalidate)
|
|
|
|
InvalidateWindow(WC_VEHICLE_ORDERS, v->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Subsidies need removal as well
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteSubsidyWithStation(index);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void DeleteAllPlayerStations(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy && st->owner < MAX_PLAYERS)
|
2004-09-10 19:02:27 +00:00
|
|
|
DeleteStation(st);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-08 22:44:06 +00:00
|
|
|
static void CheckOrphanedSlots(const Station *st, RoadStopType rst)
|
|
|
|
{
|
|
|
|
RoadStop *rs;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
for (rs = GetPrimaryRoadStop(st, rst); rs != NULL; rs = rs->next) {
|
|
|
|
for (k = 0; k < NUM_SLOTS; k++) {
|
|
|
|
if (rs->slot[k] != INVALID_SLOT) {
|
2005-04-10 20:48:43 +00:00
|
|
|
const Vehicle *v = GetVehicle(rs->slot[k]);
|
|
|
|
|
|
|
|
if (v->type != VEH_Road || v->u.road.slot != rs) {
|
|
|
|
DEBUG(ms, 0) (
|
|
|
|
"Multistop: Orphaned %s slot at 0x%X of station %d (don't panic)",
|
|
|
|
(rst == RS_BUS) ? "bus" : "truck", rs->xy, st->index);
|
2005-04-08 22:44:06 +00:00
|
|
|
rs->slot[k] = INVALID_SLOT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* this function is called for one station each tick */
|
|
|
|
static void StationHandleBigTick(Station *st)
|
|
|
|
{
|
|
|
|
UpdateStationAcceptance(st, true);
|
|
|
|
|
|
|
|
if (st->facilities == 0) {
|
|
|
|
if (++st->delete_ctr >= 8)
|
|
|
|
DeleteStation(st);
|
|
|
|
}
|
2005-01-30 22:04:14 +00:00
|
|
|
|
2005-04-08 22:44:06 +00:00
|
|
|
// Here we saveguard against orphaned slots
|
|
|
|
CheckOrphanedSlots(st, RS_BUS);
|
|
|
|
CheckOrphanedSlots(st, RS_TRUCK);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +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)
|
|
|
|
{
|
|
|
|
GoodsEntry *ge;
|
2005-03-25 10:40:58 +00:00
|
|
|
int rating;
|
|
|
|
StationID index;
|
2004-08-09 17:04:08 +00:00
|
|
|
int waiting;
|
|
|
|
bool waiting_changed = false;
|
|
|
|
|
|
|
|
byte_inc_sat(&st->time_since_load);
|
|
|
|
byte_inc_sat(&st->time_since_unload);
|
|
|
|
|
|
|
|
ge = st->goods;
|
|
|
|
do {
|
2005-02-02 16:16:43 +00:00
|
|
|
if (ge->enroute_from != INVALID_STATION) {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte_inc_sat(&ge->enroute_time);
|
|
|
|
byte_inc_sat(&ge->days_since_pickup);
|
|
|
|
|
|
|
|
rating = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
int b = ge->last_speed;
|
|
|
|
if ((b-=85) >= 0)
|
|
|
|
rating += b >> 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
byte age = ge->last_age;
|
|
|
|
(age >= 3) ||
|
|
|
|
(rating += 10, age >= 2) ||
|
|
|
|
(rating += 10, age >= 1) ||
|
|
|
|
(rating += 13, true);
|
|
|
|
}
|
|
|
|
|
2005-03-08 17:32:34 +00:00
|
|
|
if (st->owner < MAX_PLAYERS && HASBIT(st->town->statues, st->owner))
|
2004-08-09 17:04:08 +00:00
|
|
|
rating += 26;
|
|
|
|
|
|
|
|
{
|
|
|
|
byte days = ge->days_since_pickup;
|
|
|
|
if (st->last_vehicle != INVALID_VEHICLE &&
|
2005-01-06 22:31:58 +00:00
|
|
|
GetVehicle(st->last_vehicle)->type == VEH_Ship)
|
2004-08-09 17:04:08 +00:00
|
|
|
days >>= 2;
|
|
|
|
(days > 21) ||
|
|
|
|
(rating += 25, days > 12) ||
|
|
|
|
(rating += 25, days > 6) ||
|
|
|
|
(rating += 45, days > 3) ||
|
|
|
|
(rating += 35, true);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
waiting = ge->waiting_acceptance & 0xFFF;
|
|
|
|
(rating -= 90, waiting > 1500) ||
|
|
|
|
(rating += 55, waiting > 1000) ||
|
|
|
|
(rating += 35, waiting > 600) ||
|
|
|
|
(rating += 10, waiting > 300) ||
|
|
|
|
(rating += 20, waiting > 100) ||
|
|
|
|
(rating += 10, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int or = ge->rating; // old rating
|
|
|
|
|
|
|
|
// only modify rating in steps of -2, -1, 0, 1 or 2
|
|
|
|
ge->rating = rating = or + clamp(clamp(rating, 0, 255) - or, -2, 2);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// if rating is <= 64 and more than 200 items waiting, remove some random amount of goods from the station
|
|
|
|
if (rating <= 64 && waiting >= 200) {
|
|
|
|
int dec = Random() & 0x1F;
|
|
|
|
if (waiting < 400) dec &= 7;
|
|
|
|
waiting -= dec + 1;
|
|
|
|
waiting_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if rating is <= 127 and there are any items waiting, maybe remove some goods.
|
|
|
|
if (rating <= 127 && waiting != 0) {
|
|
|
|
uint32 r = Random();
|
|
|
|
if ( (uint)rating <= (r & 0x7F) ) {
|
|
|
|
waiting = max(waiting - ((r >> 8)&3) - 1, 0);
|
|
|
|
waiting_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (waiting_changed)
|
|
|
|
ge->waiting_acceptance = (ge->waiting_acceptance & ~0xFFF) + waiting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (++ge != endof(st->goods));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
index = st->index;
|
|
|
|
|
|
|
|
if (waiting_changed)
|
|
|
|
InvalidateWindow(WC_STATION_VIEW, index);
|
|
|
|
else
|
|
|
|
InvalidateWindowWidget(WC_STATION_VIEW, index, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called for every station each tick */
|
|
|
|
static void StationHandleSmallTick(Station *st)
|
|
|
|
{
|
|
|
|
byte b;
|
|
|
|
|
|
|
|
if (st->facilities == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
b = st->delete_ctr + 1;
|
|
|
|
if (b >= 185) b = 0;
|
|
|
|
st->delete_ctr = b;
|
|
|
|
|
|
|
|
if (b == 0)
|
|
|
|
UpdateStationRating(st);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void OnTick_Station(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-03 17:22:35 +00:00
|
|
|
uint i;
|
2004-08-09 17:04:08 +00:00
|
|
|
Station *st;
|
|
|
|
|
|
|
|
if (_game_mode == GM_EDITOR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i = _station_tick_ctr;
|
2005-02-03 17:22:35 +00:00
|
|
|
if (++_station_tick_ctr == GetStationPoolSize())
|
2004-08-09 17:04:08 +00:00
|
|
|
_station_tick_ctr = 0;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->xy != 0)
|
|
|
|
StationHandleBigTick(st);
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0)
|
|
|
|
StationHandleSmallTick(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void StationMonthlyLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModifyStationRatingAround(TileIndex tile, byte owner, int amount, uint radius)
|
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
GoodsEntry *ge;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2005-01-31 07:23:15 +00:00
|
|
|
if (st->xy != 0 && st->owner == owner &&
|
|
|
|
DistanceManhattan(tile, st->xy) <= radius) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ge = st->goods;
|
|
|
|
for(i=0; i!=NUM_CARGO; i++,ge++) {
|
2005-02-02 16:16:43 +00:00
|
|
|
if (ge->enroute_from != INVALID_STATION) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ge->rating = clamp(ge->rating + amount, 0, 255);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateStationWaiting(Station *st, int type, uint amount)
|
|
|
|
{
|
2004-09-10 19:02:27 +00:00
|
|
|
st->goods[type].waiting_acceptance =
|
|
|
|
(st->goods[type].waiting_acceptance & ~0xFFF) +
|
2004-08-09 17:04:08 +00:00
|
|
|
min(0xFFF, (st->goods[type].waiting_acceptance & 0xFFF) + amount);
|
|
|
|
|
|
|
|
st->goods[type].enroute_time = 0;
|
|
|
|
st->goods[type].enroute_from = st->index;
|
|
|
|
InvalidateWindow(WC_STATION_VIEW, st->index);
|
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Rename a station
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 station ID that is to be renamed
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdRenameStation(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
StringID str,old_str;
|
|
|
|
Station *st;
|
|
|
|
|
2005-05-17 20:58:58 +00:00
|
|
|
if (!IsStationIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
|
2005-05-07 08:14:06 +00:00
|
|
|
st = GetStation(p1);
|
|
|
|
|
|
|
|
if (!IsValidStation(st) || !CheckOwnership(st->owner)) return CMD_ERROR;
|
|
|
|
|
2005-05-15 18:50:55 +00:00
|
|
|
str = AllocateNameUnique(_cmd_text, 6);
|
2005-05-11 00:00:27 +00:00
|
|
|
if (str == 0) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
old_str = st->string_id;
|
|
|
|
st->string_id = str;
|
|
|
|
UpdateStationVirtCoord(st);
|
|
|
|
DeleteName(old_str);
|
2004-09-14 14:19:53 +00:00
|
|
|
_station_sort_dirty[st->owner] = true; // rename a station
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkWholeScreenDirty();
|
|
|
|
} else {
|
|
|
|
DeleteName(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
uint MoveGoodsToStation(TileIndex tile, int w, int h, int type, uint amount)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *around_ptr[8];
|
2005-03-25 10:40:58 +00:00
|
|
|
StationID around[8];
|
|
|
|
StationID st_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
int i;
|
|
|
|
Station *st;
|
|
|
|
uint moved;
|
|
|
|
uint best_rating, best_rating2;
|
|
|
|
Station *st1, *st2;
|
|
|
|
int t;
|
2004-12-08 15:46:13 +00:00
|
|
|
int rad=0;
|
|
|
|
int w_prod=0, h_prod=0; //width and height of the "producer" of the cargo
|
|
|
|
int x_min_prod, x_max_prod; //min and max coordinates of the producer
|
|
|
|
int y_min_prod, y_max_prod; //relative
|
|
|
|
int x_dist, y_dist;
|
|
|
|
int max_rad;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
memset(around, 0xff, sizeof(around));
|
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
if (_patches.modified_catchment) {
|
|
|
|
w_prod = w;
|
|
|
|
h_prod = h;
|
|
|
|
w += 16;
|
|
|
|
h += 16;
|
|
|
|
max_rad = 8;
|
|
|
|
} else {
|
|
|
|
w += 8;
|
|
|
|
h += 8;
|
|
|
|
max_rad = 4;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-25 16:44:57 +00:00
|
|
|
BEGIN_TILE_LOOP(cur_tile, w, h, tile - TileDiffXY(max_rad, max_rad))
|
2004-08-09 17:04:08 +00:00
|
|
|
cur_tile = TILE_MASK(cur_tile);
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(cur_tile, MP_STATION)) {
|
2005-07-13 18:04:01 +00:00
|
|
|
st_index = _m[cur_tile].m2;
|
2004-08-09 17:04:08 +00:00
|
|
|
for(i=0; i!=8; i++) {
|
2005-03-19 23:17:59 +00:00
|
|
|
if (around[i] == INVALID_STATION) {
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(st_index);
|
2005-05-02 22:13:20 +00:00
|
|
|
if (!IsBuoy(st) &&
|
2004-08-23 21:04:39 +00:00
|
|
|
( !st->town->exclusive_counter || (st->town->exclusivity == st->owner) ) && // check exclusive transport rights
|
2004-09-10 19:02:27 +00:00
|
|
|
st->goods[type].rating != 0 &&
|
2004-08-09 17:04:08 +00:00
|
|
|
(!_patches.selectgoods || st->goods[type].last_speed) && // if last_speed is 0, no vehicle has been there.
|
2004-08-11 22:07:08 +00:00
|
|
|
((st->facilities & (byte)~FACIL_BUS_STOP)!=0 || type==CT_PASSENGERS) && // if we have other fac. than a bus stop, or the cargo is passengers
|
|
|
|
((st->facilities & (byte)~FACIL_TRUCK_STOP)!=0 || type!=CT_PASSENGERS)) { // if we have other fac. than a cargo bay or the cargo is not passengers
|
2004-12-08 15:46:13 +00:00
|
|
|
if (_patches.modified_catchment) {
|
2004-12-18 13:38:27 +00:00
|
|
|
rad = FindCatchmentRadius(st);
|
2004-12-08 15:46:13 +00:00
|
|
|
x_min_prod = y_min_prod = 9;
|
|
|
|
x_max_prod = 8 + w_prod;
|
|
|
|
y_max_prod = 8 + h_prod;
|
2004-12-18 13:38:27 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
x_dist = min(w_cur - x_min_prod, x_max_prod - w_cur);
|
2004-12-18 13:38:27 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
if (w_cur < x_min_prod) {
|
|
|
|
x_dist = x_min_prod - w_cur;
|
|
|
|
} else { //save cycles
|
|
|
|
if (w_cur > x_max_prod) x_dist = w_cur - x_max_prod;
|
|
|
|
}
|
2004-12-18 13:38:27 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
y_dist = min(h_cur - y_min_prod, y_max_prod - h_cur);
|
|
|
|
if (h_cur < y_min_prod) {
|
|
|
|
y_dist = y_min_prod - h_cur;
|
|
|
|
} else {
|
|
|
|
if (h_cur > y_max_prod) y_dist = h_cur - y_max_prod;
|
|
|
|
}
|
2004-12-18 13:38:27 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
} else {
|
|
|
|
x_dist = y_dist = 0;
|
|
|
|
}
|
2004-12-18 13:38:27 +00:00
|
|
|
|
2004-12-08 15:46:13 +00:00
|
|
|
if ( !(x_dist > rad) && !(y_dist > rad) ) {
|
|
|
|
|
|
|
|
around[i] = st_index;
|
|
|
|
around_ptr[i] = st;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
} else if (around[i] == st_index)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-06-25 16:44:57 +00:00
|
|
|
END_TILE_LOOP(cur_tile, w, h, tile - TileDiffXY(max_rad, max_rad))
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* no stations around at all? */
|
2005-03-19 23:17:59 +00:00
|
|
|
if (around[0] == INVALID_STATION)
|
2004-08-09 17:04:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2005-03-19 23:17:59 +00:00
|
|
|
if (around[1] == INVALID_STATION) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* only one station around */
|
|
|
|
moved = (amount * around_ptr[0]->goods[type].rating >> 8) + 1;
|
|
|
|
UpdateStationWaiting(around_ptr[0], type, moved);
|
|
|
|
return moved;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* several stations around, find the two with the highest rating */
|
|
|
|
st2 = st1 = NULL;
|
|
|
|
best_rating = best_rating2 = 0;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2005-03-19 23:17:59 +00:00
|
|
|
for( i = 0; i != 8 && around[i] != INVALID_STATION; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (around_ptr[i]->goods[type].rating >= best_rating) {
|
|
|
|
best_rating2 = best_rating;
|
|
|
|
st2 = st1;
|
|
|
|
|
|
|
|
best_rating = around_ptr[i]->goods[type].rating;
|
|
|
|
st1 = around_ptr[i];
|
|
|
|
} else if (around_ptr[i]->goods[type].rating >= best_rating2) {
|
|
|
|
best_rating2 = around_ptr[i]->goods[type].rating;
|
2004-09-10 19:02:27 +00:00
|
|
|
st2 = around_ptr[i];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(st1 != NULL);
|
|
|
|
assert(st2 != NULL);
|
|
|
|
assert(best_rating != 0 || best_rating2 != 0);
|
|
|
|
|
|
|
|
/* the 2nd highest one gets a penalty */
|
|
|
|
best_rating2 >>= 1;
|
|
|
|
|
|
|
|
/* amount given to station 1 */
|
|
|
|
t = (best_rating * (amount + 1)) / (best_rating + best_rating2);
|
|
|
|
|
|
|
|
moved = 0;
|
|
|
|
if (t != 0) {
|
|
|
|
moved = (t * best_rating >> 8) + 1;
|
|
|
|
amount -= t;
|
2004-09-10 19:02:27 +00:00
|
|
|
UpdateStationWaiting(st1, type, moved);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (amount != 0) {
|
|
|
|
moved += (amount = (amount * best_rating2 >> 8) + 1);
|
|
|
|
UpdateStationWaiting(st2, type, amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return moved;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
void BuildOilRig(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy == 0) {
|
|
|
|
st->town = ClosestTownFromTile(tile, (uint)-1);
|
|
|
|
st->sign.width_1 = 0;
|
|
|
|
if (!GenerateStationName(st, tile, 2))
|
|
|
|
return;
|
|
|
|
|
2005-01-18 18:41:56 +00:00
|
|
|
SetTileType(tile, MP_STATION);
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m5 = 0x4B;
|
2005-06-04 12:13:24 +00:00
|
|
|
SetTileOwner(tile, OWNER_NONE);
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m3 = 0;
|
|
|
|
_m[tile].m4 = 0;
|
|
|
|
_m[tile].m2 = st->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-12 20:24:28 +00:00
|
|
|
st->owner = OWNER_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->airport_flags = 0;
|
|
|
|
st->airport_type = AT_OILRIG;
|
|
|
|
st->xy = tile;
|
2005-01-29 19:41:44 +00:00
|
|
|
st->bus_stops = NULL;
|
|
|
|
st->truck_stops = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->airport_tile = tile;
|
|
|
|
st->dock_tile = tile;
|
|
|
|
st->train_tile = 0;
|
|
|
|
st->had_vehicle_of_type = 0;
|
|
|
|
st->time_since_load = 255;
|
|
|
|
st->time_since_unload = 255;
|
|
|
|
st->delete_ctr = 0;
|
|
|
|
st->last_vehicle = INVALID_VEHICLE;
|
|
|
|
st->facilities = FACIL_AIRPORT | FACIL_DOCK;
|
2004-08-17 08:40:09 +00:00
|
|
|
st->build_date = _date;
|
2004-08-09 17:04:08 +00:00
|
|
|
for(j=0; j!=NUM_CARGO; j++) {
|
|
|
|
st->goods[j].waiting_acceptance = 0;
|
|
|
|
st->goods[j].days_since_pickup = 0;
|
2005-02-02 16:16:43 +00:00
|
|
|
st->goods[j].enroute_from = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
st->goods[j].rating = 175;
|
|
|
|
st->goods[j].last_speed = 0;
|
|
|
|
st->goods[j].last_age = 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
UpdateStationAcceptance(st, false);
|
2004-09-10 19:02:27 +00:00
|
|
|
return;
|
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
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
Station *st = GetStation(_m[tile].m2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
DoClearSquare(tile);
|
|
|
|
|
|
|
|
st->dock_tile = 0;
|
|
|
|
st->airport_tile = 0;
|
|
|
|
st->facilities &= ~(FACIL_AIRPORT | FACIL_DOCK);
|
|
|
|
st->airport_flags = 0;
|
|
|
|
UpdateStationVirtCoordDirty(st);
|
|
|
|
DeleteStation(st);
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static void ChangeTileOwner_Station(TileIndex tile, byte old_player, byte new_player)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-04 11:56:32 +00:00
|
|
|
if (!IsTileOwner(tile, old_player)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (new_player != 255) {
|
2005-07-13 18:04:01 +00:00
|
|
|
Station *st = GetStation(_m[tile].m2);
|
2005-06-04 12:13:24 +00:00
|
|
|
SetTileOwner(tile, new_player);
|
2004-08-09 17:04:08 +00:00
|
|
|
st->owner = new_player;
|
2004-09-14 14:19:53 +00:00
|
|
|
_global_station_sort_dirty = true; // transfer ownership of station to another player
|
2004-08-16 14:48:35 +00:00
|
|
|
InvalidateWindowClasses(WC_STATION_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static int32 ClearTile_Station(TileIndex tile, byte flags)
|
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
byte m5 = _m[tile].m5;
|
2004-08-09 17:04:08 +00:00
|
|
|
Station *st;
|
|
|
|
|
|
|
|
if (flags & DC_AUTO) {
|
|
|
|
if (m5 < 8) return_cmd_error(STR_300B_MUST_DEMOLISH_RAILROAD);
|
|
|
|
if (m5 < 0x43 || (m5 >= 83 && m5 <= 114)) return_cmd_error(STR_300E_MUST_DEMOLISH_AIRPORT_FIRST);
|
|
|
|
if (m5 < 0x47) return_cmd_error(STR_3047_MUST_DEMOLISH_TRUCK_STATION);
|
|
|
|
if (m5 < 0x4B) return_cmd_error(STR_3046_MUST_DEMOLISH_BUS_STATION);
|
|
|
|
if (m5 == 0x52) return_cmd_error(STR_306A_BUOY_IN_THE_WAY);
|
|
|
|
if (m5 != 0x4B && m5 < 0x53) return_cmd_error(STR_304D_MUST_DEMOLISH_DOCK_FIRST);
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, STR_4807_OIL_RIG);
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_4800_IN_THE_WAY);
|
|
|
|
}
|
|
|
|
|
2005-07-13 18:04:01 +00:00
|
|
|
st = GetStation(_m[tile].m2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (m5 < 8)
|
2004-09-03 17:57:27 +00:00
|
|
|
return RemoveRailroadStation(st, tile, flags);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// original airports < 67, new airports between 83 - 114
|
|
|
|
if (m5 < 0x43 || ( m5 >= 83 && m5 <= 114) )
|
|
|
|
return RemoveAirport(st, flags);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (m5 < 0x4B)
|
2005-01-29 19:41:44 +00:00
|
|
|
return RemoveRoadStop(st, flags, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (m5 == 0x52)
|
|
|
|
return RemoveBuoy(st, flags);
|
|
|
|
|
|
|
|
if (m5 != 0x4B && m5 < 0x53)
|
|
|
|
return RemoveDock(st, flags);
|
|
|
|
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void InitializeStations(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-03 17:22:35 +00:00
|
|
|
/* Clean the station pool and create 1 block in it */
|
|
|
|
CleanPool(&_station_pool);
|
|
|
|
AddBlockToPool(&_station_pool);
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
/* Clean the roadstop pool and create 1 block in it */
|
|
|
|
CleanPool(&_roadstop_pool);
|
|
|
|
AddBlockToPool(&_roadstop_pool);
|
2004-09-14 14:19:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_station_tick_ctr = 0;
|
2004-09-14 14:19:53 +00:00
|
|
|
|
|
|
|
// set stations to be sorted on load of savegame
|
|
|
|
memset(_station_sort_dirty, true, sizeof(_station_sort_dirty));
|
|
|
|
_global_station_sort_dirty = true; // load of savegame
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const TileTypeProcs _tile_type_station_procs = {
|
|
|
|
DrawTile_Station, /* draw_tile_proc */
|
|
|
|
GetSlopeZ_Station, /* get_slope_z_proc */
|
|
|
|
ClearTile_Station, /* clear_tile_proc */
|
|
|
|
GetAcceptedCargo_Station, /* get_accepted_cargo_proc */
|
|
|
|
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 */
|
|
|
|
NULL, /* get_produced_cargo_proc */
|
|
|
|
VehicleEnter_Station, /* vehicle_enter_tile_proc */
|
|
|
|
NULL, /* vehicle_leave_tile_proc */
|
2004-08-13 18:27:33 +00:00
|
|
|
GetSlopeTileh_Station, /* get_slope_tileh_proc */
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _roadstop_desc[] = {
|
2005-01-29 19:41:44 +00:00
|
|
|
SLE_VAR(RoadStop,xy, SLE_UINT32),
|
|
|
|
SLE_VAR(RoadStop,used, SLE_UINT8),
|
|
|
|
SLE_VAR(RoadStop,status, SLE_UINT8),
|
2005-02-04 15:39:10 +00:00
|
|
|
/* Index was saved in some versions, but this is not needed */
|
|
|
|
SLE_CONDARR(NullStruct,null,SLE_FILE_U32 | SLE_VAR_NULL, 1, 0, 8),
|
2005-01-29 19:41:44 +00:00
|
|
|
SLE_VAR(RoadStop,station, SLE_UINT16),
|
|
|
|
SLE_VAR(RoadStop,type, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_REF(RoadStop,next, REF_ROADSTOPS),
|
|
|
|
SLE_REF(RoadStop,prev, REF_ROADSTOPS),
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
SLE_ARR(RoadStop,slot, SLE_UINT16, NUM_SLOTS),
|
2005-01-29 19:41:44 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _station_desc[] = {
|
2005-01-25 21:43:57 +00:00
|
|
|
SLE_CONDVAR(Station, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Station, xy, SLE_UINT32, 6, 255),
|
2005-01-29 19:41:44 +00:00
|
|
|
SLE_CONDVAR(Station, bus_tile_obsolete, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Station, lorry_tile_obsolete, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
2005-01-25 21:43:57 +00:00
|
|
|
SLE_CONDVAR(Station, train_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Station, train_tile, SLE_UINT32, 6, 255),
|
|
|
|
SLE_CONDVAR(Station, airport_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Station, airport_tile, SLE_UINT32, 6, 255),
|
|
|
|
SLE_CONDVAR(Station, dock_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Station, dock_tile, SLE_UINT32, 6, 255),
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_REF(Station,town, REF_TOWN),
|
|
|
|
SLE_VAR(Station,trainst_w, SLE_UINT8),
|
|
|
|
SLE_CONDVAR(Station,trainst_h, SLE_UINT8, 2, 255),
|
|
|
|
|
|
|
|
// alpha_order was stored here in savegame format 0 - 3
|
|
|
|
SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 1, 0, 3),
|
|
|
|
|
|
|
|
SLE_VAR(Station,string_id, SLE_STRINGID),
|
|
|
|
SLE_VAR(Station,had_vehicle_of_type,SLE_UINT16),
|
|
|
|
|
|
|
|
SLE_VAR(Station,time_since_load, SLE_UINT8),
|
|
|
|
SLE_VAR(Station,time_since_unload, SLE_UINT8),
|
|
|
|
SLE_VAR(Station,delete_ctr, SLE_UINT8),
|
|
|
|
SLE_VAR(Station,owner, SLE_UINT8),
|
|
|
|
SLE_VAR(Station,facilities, SLE_UINT8),
|
|
|
|
SLE_VAR(Station,airport_type, SLE_UINT8),
|
2005-01-29 19:41:44 +00:00
|
|
|
|
|
|
|
// truck/bus_stop_status was stored here in savegame format 0 - 6
|
|
|
|
SLE_CONDVAR(Station,truck_stop_status_obsolete, SLE_UINT8, 0, 5),
|
|
|
|
SLE_CONDVAR(Station,bus_stop_status_obsolete, SLE_UINT8, 0, 5),
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 21:29:25 +00:00
|
|
|
// blocked_months was stored here in savegame format 0 - 4.0
|
|
|
|
SLE_CONDVAR(Station,blocked_months_obsolete, SLE_UINT8, 0, 4),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SLE_CONDVAR(Station,airport_flags, SLE_VAR_U32 | SLE_FILE_U16, 0, 2),
|
|
|
|
SLE_CONDVAR(Station,airport_flags, SLE_UINT32, 3, 255),
|
|
|
|
|
|
|
|
SLE_VAR(Station,last_vehicle, SLE_UINT16),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Station,class_id, SLE_UINT8, 3, 255),
|
|
|
|
SLE_CONDVAR(Station,stat_id, SLE_UINT8, 3, 255),
|
|
|
|
SLE_CONDVAR(Station,build_date, SLE_UINT16, 3, 255),
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
SLE_CONDREF(Station,bus_stops, REF_ROADSTOPS, 6, 255),
|
|
|
|
SLE_CONDREF(Station,truck_stops, REF_ROADSTOPS, 6, 255),
|
|
|
|
|
|
|
|
// reserve extra space in savegame here. (currently 28 bytes)
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 32, 2, 255),
|
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _goods_desc[] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_VAR(GoodsEntry,waiting_acceptance,SLE_UINT16),
|
|
|
|
SLE_VAR(GoodsEntry,days_since_pickup, SLE_UINT8),
|
|
|
|
SLE_VAR(GoodsEntry,rating, SLE_UINT8),
|
2005-02-02 16:16:43 +00:00
|
|
|
SLE_CONDVAR(GoodsEntry,enroute_from, SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
|
|
|
|
SLE_CONDVAR(GoodsEntry,enroute_from, SLE_UINT16, 7, 255),
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_VAR(GoodsEntry,enroute_time, SLE_UINT8),
|
|
|
|
SLE_VAR(GoodsEntry,last_speed, SLE_UINT8),
|
|
|
|
SLE_VAR(GoodsEntry,last_age, SLE_UINT8),
|
2005-06-15 16:58:15 +00:00
|
|
|
SLE_CONDVAR(GoodsEntry,feeder_profit, SLE_INT32, 14, 255),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void SaveLoad_STNS(Station *st)
|
|
|
|
{
|
|
|
|
int i;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SlObject(st, _station_desc);
|
2005-02-02 16:16:43 +00:00
|
|
|
for (i = 0; i != NUM_CARGO; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
SlObject(&st->goods[i], _goods_desc);
|
2005-02-02 16:16:43 +00:00
|
|
|
|
|
|
|
/* In older versions, enroute_from had 0xFF as INVALID_STATION, is now 0xFFFF */
|
2005-08-06 17:40:21 +00:00
|
|
|
if (_sl_full_version < 0x700 && st->goods[i].enroute_from == 0xFF)
|
2005-02-02 16:16:43 +00:00
|
|
|
st->goods[i].enroute_from = 0xFFFF;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Save_STNS(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
2005-01-06 22:31:58 +00:00
|
|
|
// Write the stations
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0) {
|
|
|
|
SlSetArrayIndex(st->index);
|
|
|
|
SlAutolength((AutolengthProc*)SaveLoad_STNS, st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Load_STNS(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2005-02-03 17:22:35 +00:00
|
|
|
Station *st;
|
|
|
|
|
|
|
|
if (!AddBlockIfNeeded(&_station_pool, index))
|
|
|
|
error("Stations: failed loading savegame: too many stations");
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2005-02-03 17:22:35 +00:00
|
|
|
st = GetStation(index);
|
2004-08-09 17:04:08 +00:00
|
|
|
SaveLoad_STNS(st);
|
|
|
|
|
|
|
|
// this means it's an oldstyle savegame without support for nonuniform stations
|
|
|
|
if (st->train_tile && st->trainst_h == 0) {
|
|
|
|
int w = st->trainst_w >> 4;
|
|
|
|
int h = st->trainst_w & 0xF;
|
2005-07-13 18:04:01 +00:00
|
|
|
if (_m[st->train_tile].m5&1) intswap(w,h);
|
2004-08-09 17:04:08 +00:00
|
|
|
st->trainst_w = w;
|
|
|
|
st->trainst_h = h;
|
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2005-08-06 17:40:21 +00:00
|
|
|
if (_sl_full_version < 0x600) {
|
2005-01-29 19:41:44 +00:00
|
|
|
/* Convert old bus and truck tile to new-ones */
|
|
|
|
if (st->bus_tile_obsolete != 0) {
|
2005-02-04 15:31:30 +00:00
|
|
|
st->bus_stops = AllocateRoadStop();
|
|
|
|
if (st->bus_stops == NULL)
|
2005-01-29 19:41:44 +00:00
|
|
|
error("Station: too many busstations in savegame");
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
InitializeRoadStop(st->bus_stops, NULL, st->bus_tile_obsolete, st->index);
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
if (st->lorry_tile_obsolete != 0) {
|
2005-02-04 15:31:30 +00:00
|
|
|
st->truck_stops = AllocateRoadStop();
|
|
|
|
if (st->truck_stops == NULL)
|
2005-01-29 19:41:44 +00:00
|
|
|
error("Station: too many truckstations in savegame");
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
InitializeRoadStop(st->truck_stops, NULL, st->lorry_tile_obsolete, st->index);
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-06 22:31:58 +00:00
|
|
|
|
2005-03-09 19:09:04 +00:00
|
|
|
/* This is to ensure all pointers are within the limits of _stations_size */
|
2005-02-03 17:22:35 +00:00
|
|
|
if (_station_tick_ctr > GetStationPoolSize())
|
2005-01-06 22:31:58 +00:00
|
|
|
_station_tick_ctr = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 19:41:44 +00:00
|
|
|
static void Save_ROADSTOP( void )
|
|
|
|
{
|
2005-02-04 15:31:30 +00:00
|
|
|
RoadStop *rs;
|
2005-01-29 19:41:44 +00:00
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
FOR_ALL_ROADSTOPS(rs) {
|
|
|
|
if (rs->used) {
|
|
|
|
SlSetArrayIndex(rs->index);
|
|
|
|
SlObject(rs, _roadstop_desc);
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Load_ROADSTOP( void )
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
2005-02-04 15:31:30 +00:00
|
|
|
while ((index = SlIterateArray()) != -1) {
|
|
|
|
RoadStop *rs;
|
|
|
|
|
|
|
|
if (!AddBlockIfNeeded(&_roadstop_pool, index))
|
2005-02-06 10:24:57 +00:00
|
|
|
error("RoadStops: failed loading savegame: too many RoadStops");
|
2005-02-04 15:31:30 +00:00
|
|
|
|
|
|
|
rs = GetRoadStop(index);
|
|
|
|
SlObject(rs, _roadstop_desc);
|
|
|
|
}
|
2005-01-29 19:41:44 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
const ChunkHandler _station_chunk_handlers[] = {
|
2005-01-29 19:41:44 +00:00
|
|
|
{ 'STNS', Save_STNS, Load_STNS, CH_ARRAY },
|
|
|
|
{ 'ROAD', Save_ROADSTOP, Load_ROADSTOP, CH_ARRAY | CH_LAST},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|