tron@2186: /* $Id$ */ tron@2186: richk@10724: /** @file station_cmd.cpp Handling of station tiles. */ celestar@2213: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@6413: #include "aircraft.h" celestar@5573: #include "bridge_map.h" tron@6460: #include "cmd_helper.h" tron@1299: #include "debug.h" rubidium@6872: #include "tile_cmd.h" richk@6719: #include "landscape.h" tron@3315: #include "station_map.h" rubidium@6872: #include "viewport_func.h" rubidium@6872: #include "command_func.h" truelight@0: #include "town.h" richk@10184: #include "news_func.h" truelight@0: #include "saveload.h" truelight@0: #include "airport.h" darkvater@405: #include "sprite.h" bjarni@2676: #include "train.h" richk@6720: #include "roadveh.h" tron@3111: #include "water_map.h" belugas@3515: #include "industry_map.h" peter1138@3754: #include "newgrf_callbacks.h" peter1138@3754: #include "newgrf_station.h" KUDr@3900: #include "yapf/yapf.h" rubidium@6872: #include "road_type.h" rubidium@6872: #include "road_internal.h" /* For drawing catenary/checking road removal */ peter1138@6417: #include "cargotype.h" rubidium@6872: #include "variables.h" rubidium@6870: #include "autoslope.h" richk@6722: #include "newgrf_fsmports.h" rubidium@6871: #include "transparency.h" rubidium@6871: #include "water.h" rubidium@6872: #include "station_gui.h" rubidium@6872: #include "strings_func.h" rubidium@6872: #include "functions.h" rubidium@6872: #include "window_func.h" rubidium@6872: #include "date_func.h" rubidium@6872: #include "vehicle_func.h" rubidium@6872: #include "string_func.h" rubidium@6872: #include "signal_func.h" richk@10184: #include "oldpool_func.h" richk@10274: #include "animated_tile_func.h" richk@10724: #include "elrail_func.h" rubidium@6872: rubidium@6872: #include "table/sprites.h" rubidium@6872: #include "table/strings.h" KUDr@5917: richk@6743: DEFINE_OLD_POOL_GENERIC(Station, Station) richk@6743: DEFINE_OLD_POOL_GENERIC(RoadStop, RoadStop) richk@6743: peter1138@3587: truelight@1284: /** richk@6743: * Check whether the given tile is a hangar. richk@6743: * @param t the tile to of whether it is a hangar. richk@6743: * @pre IsTileType(t, MP_STATION) richk@6743: * @return true if and only if the tile is a hangar. truelight@1284: */ richk@6743: bool IsHangar(TileIndex t) truelight@1284: { richk@6743: assert(IsTileType(t, MP_STATION)); richk@6743: richk@6743: Station *st = GetStationByTile(t); richk@6863: FSMDepots *hangar_list; richk@6743: byte num_depots; richk@6743: if (IsCustomFSMportsSpecIndex(t)) { rubidium@6853: hangar_list = st->fsmportsspeclist[1].spec->portFSM->depots; richk@6856: num_depots = st->fsmportsspeclist[1].spec->portFSM->num_depots; richk@6743: } else { rubidium@6853: hangar_list = st->Airport()->depots; richk@6856: num_depots = st->Airport()->num_depots; tron@5969: } richk@6743: for (uint i = 0; i < num_depots; i++) { richk@6863: TileIndexDiffC depot = RotateFSMPosition(hangar_list[i].tile_pos, richk@6746: st->fsmportsspeclist[1].spec->size_x[st->FSMport_layout_set], richk@6746: st->fsmportsspeclist[1].spec->size_y[st->FSMport_layout_set], richk@6746: st->FSMport_orientation); richk@6746: if (st->airport_tile + ToTileIndexDiff(depot) == t) { richk@6743: return true; richk@6743: } richk@6743: } richk@6743: return false; truelight@1284: } truelight@1284: richk@6863: richk@6863: FSMDepots GetKeyDepotByTile(TileIndex t) richk@6863: { richk@6863: Station *st = GetStationByTile(t); richk@6863: richk@6863: FSMDepots *hangar_list; richk@6863: byte num_depots; richk@6863: uint i; richk@6863: richk@6863: if (IsCustomFSMportsSpecIndex(t)) { richk@6863: hangar_list = st->fsmportsspeclist[1].spec->portFSM->depots; richk@6863: num_depots = st->fsmportsspeclist[1].spec->portFSM->num_depots; richk@6863: } else { richk@6863: hangar_list = st->Airport()->depots; richk@6863: num_depots = st->Airport()->num_depots; richk@6863: } richk@6863: for (i = 0; i < num_depots; i++) { richk@6863: TileIndexDiffC depot = RotateFSMPosition(hangar_list[i].tile_pos, richk@6863: st->fsmportsspeclist[1].spec->size_x[st->FSMport_layout_set], richk@6863: st->fsmportsspeclist[1].spec->size_y[st->FSMport_layout_set], richk@6863: st->FSMport_orientation); richk@6863: if (st->airport_tile + ToTileIndexDiff(depot) == t) { richk@6863: break; richk@6863: } richk@6863: } richk@6863: return hangar_list[i]; richk@6863: } richk@6863: richk@6863: /** richk@6863: * goes from a tile location, looks up the depot, finds the location of the key part of the depot in the state machine richk@6863: * returns the tile location of that key part richk@6863: * @param t: the tile to start from richk@6863: * @pre IsHangar(t) richk@6863: * @return tile of key part of hangar richk@6863: */ richk@6863: TileIndex GetKeyDepotTile(TileIndex t) richk@6863: { richk@6863: Station *st = GetStationByTile(t); richk@6863: const FSMPortMovingData *md = st->fsmportsspeclist[1].spec->portFSM->MovingData(GetKeyDepotByTile(t).FSMposition); richk@6863: TileIndexDiffC keydepot; richk@6863: keydepot.x = md->x / TILE_SIZE; richk@6863: keydepot.y = md->y / TILE_SIZE; richk@6863: keydepot = RotateFSMPosition(keydepot, richk@6863: st->fsmportsspeclist[1].spec->size_x[st->FSMport_layout_set], richk@6863: st->fsmportsspeclist[1].spec->size_y[st->FSMport_layout_set], richk@6863: st->FSMport_orientation); richk@6863: return st->airport_tile + ToTileIndexDiff(keydepot); richk@6863: } richk@6863: richk@10242: RoadStop *GetRoadStopByTile(TileIndex tile, RoadStopType type) celestar@1217: { richk@10242: const Station *st = GetStationByTile(tile); tron@6395: tron@6395: for (RoadStop *rs = st->GetPrimaryRoadStop(type);; rs = rs->next) { tron@6395: if (rs->xy == tile) return rs; celestar@1217: assert(rs->next != NULL); tron@2639: } celestar@1217: } celestar@1217: tron@6164: richk@10242: static uint GetNumRoadStopsInStation(const Station *st, RoadStopType type) celestar@1217: { tron@2549: uint num = 0; celestar@1217: celestar@1217: assert(st != NULL); tron@6395: for (const RoadStop *rs = st->GetPrimaryRoadStop(type); rs != NULL; rs = rs->next) { tron@6395: num++; tron@6395: } celestar@1217: celestar@1217: return num; celestar@1217: } celestar@1217: celestar@1217: rubidium@6871: /** Calculate the radius of the station. Basicly it is the biggest rubidium@6871: * radius that is available within the station rubidium@6871: * @param st Station to query rubidium@6871: * @return the so calculated radius rubidium@6871: */ richk@10242: static uint FindCatchmentRadius(const Station *st) truelight@703: { tron@6367: uint ret = CA_NONE; tron@6367: tron@6367: if (st->bus_stops != NULL) ret = max(ret, CA_BUS); tron@6367: if (st->truck_stops != NULL) ret = max(ret, CA_TRUCK); tron@6367: if (st->train_tile != 0) ret = max(ret, CA_TRAIN); tron@6367: if (st->dock_tile != 0) ret = max(ret, CA_DOCK); tron@6367: if (st->airport_tile) ret = max(ret, st->Airport()->catchment); truelight@703: truelight@703: return ret; truelight@703: } truelight@703: truelight@0: #define CHECK_STATIONS_ERR ((Station*)-1) truelight@0: richk@10242: static Station *GetStationAround(TileIndex tile, int w, int h, StationID closest_station) truelight@0: { richk@6719: /* check around to see if there's any stations there */ tron@1981: BEGIN_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TileDiffXY(1, 1)) tron@1035: if (IsTileType(tile_cur, MP_STATION)) { tron@3315: StationID t = GetStationIndex(tile_cur); darkvater@28: tron@2498: if (closest_station == INVALID_STATION) { truelight@0: closest_station = t; truelight@0: } else if (closest_station != t) { truelight@0: _error_message = STR_3006_ADJOINS_MORE_THAN_ONE_EXISTING; truelight@0: return CHECK_STATIONS_ERR; truelight@0: } truelight@0: } tron@1981: END_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TileDiffXY(1, 1)) tron@2498: return (closest_station == INVALID_STATION) ? NULL : GetStation(closest_station); truelight@0: } truelight@0: richk@6743: /** richk@6743: * Function to check whether the given tile matches some criterion. richk@6743: * @param tile the tile to check richk@6743: * @return true if it matches, false otherwise richk@6743: */ richk@6743: typedef bool (*CMSAMatcher)(TileIndex tile); truelight@0: belugas@3515: /** belugas@3515: * Counts the numbers of tiles matching a specific type in the area around belugas@3515: * @param tile the center tile of the 'count area' belugas@3515: * @param type the type of tile searched for belugas@3515: * @param industry when type == MP_INDUSTRY, the type of the industry, belugas@3515: * in all other cases this parameter is ignored belugas@6527: * @return the result the noumber of matching tiles around belugas@3515: */ richk@6743: static int CountMapSquareAround(TileIndex tile, CMSAMatcher cmp) tron@1977: { truelight@0: int num = 0; truelight@0: tron@6395: for (int dx = -3; dx <= 3; dx++) { tron@6395: for (int dy = -3; dy <= 3; dy++) { richk@6743: if (cmp(TILE_MASK(tile + TileDiffXY(dx, dy)))) num++; belugas@3515: } truelight@0: } truelight@0: truelight@0: return num; truelight@0: } truelight@0: richk@6743: /** richk@6743: * Check whether the tile is a mine. richk@6743: * @param tile the tile to investigate. richk@6743: * @return true if and only if the tile is a mine richk@6743: */ richk@6743: static bool CMSAMine(TileIndex tile) richk@6743: { richk@6743: /* No industry */ richk@6743: if (!IsTileType(tile, MP_INDUSTRY)) return false; richk@6743: rubidium@6870: const Industry *ind = GetIndustryByTile(tile); richk@6743: richk@6743: /* No extractive industry */ rubidium@6870: if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_EXTRACTIVE) == 0) return false; rubidium@6870: rubidium@6870: for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { richk@6743: /* The industry extracts something non-liquid, i.e. no oil or plastic, so it is a mine */ rubidium@6870: if (ind->produced_cargo[i] != CT_INVALID && (GetCargo(ind->produced_cargo[i])->classes & CC_LIQUID) == 0) return true; richk@6743: } richk@6743: richk@6743: return false; richk@6743: } richk@6743: richk@6743: /** richk@6743: * Check whether the tile is water. richk@6743: * @param tile the tile to investigate. richk@6743: * @return true if and only if the tile is a mine richk@6743: */ richk@6743: static bool CMSAWater(TileIndex tile) richk@6743: { richk@6743: return IsTileType(tile, MP_WATER) && IsWater(tile); richk@6743: } richk@6743: richk@6743: /** richk@6743: * Check whether the tile is a tree. richk@6743: * @param tile the tile to investigate. richk@6743: * @return true if and only if the tile is a mine richk@6743: */ richk@6743: static bool CMSATree(TileIndex tile) richk@6743: { richk@6743: return IsTileType(tile, MP_TREES); richk@6743: } richk@6743: richk@6743: /** richk@6743: * Check whether the tile is a forest. richk@6743: * @param tile the tile to investigate. richk@6743: * @return true if and only if the tile is a mine richk@6743: */ richk@6743: static bool CMSAForest(TileIndex tile) richk@6743: { richk@6743: /* No industry */ richk@6743: if (!IsTileType(tile, MP_INDUSTRY)) return false; richk@6743: rubidium@6870: const Industry *ind = GetIndustryByTile(tile); richk@6743: richk@6743: /* No extractive industry */ rubidium@6870: if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_ORGANIC) == 0) return false; rubidium@6870: rubidium@6870: for (uint i = 0; i < lengthof(ind->produced_cargo); i++) { richk@6743: /* The industry produces wood. */ rubidium@6870: if (ind->produced_cargo[i] != CT_INVALID && GetCargo(ind->produced_cargo[i])->label == 'WOOD') return true; richk@6743: } richk@6743: richk@6743: return false; richk@6743: } richk@6743: truelight@0: #define M(x) ((x) - STR_SV_STNAME) truelight@0: rubidium@6871: enum StationNaming { rubidium@6871: STATIONNAMING_RAIL = 0, rubidium@6871: STATIONNAMING_ROAD = 0, rubidium@6871: STATIONNAMING_AIRPORT, rubidium@6871: STATIONNAMING_OILRIG, rubidium@6871: STATIONNAMING_DOCK, rubidium@6871: STATIONNAMING_BUOY, rubidium@6871: STATIONNAMING_HELIPORT, rubidium@6871: }; rubidium@6871: richk@10724: static StringID GenerateStationName(Station *st, TileIndex tile, int flag) truelight@0: { truelight@0: static const uint32 _gen_station_name_bits[] = { pasky@1454: 0, /* 0 */ pasky@1454: 1 << M(STR_SV_STNAME_AIRPORT), /* 1 */ pasky@1454: 1 << M(STR_SV_STNAME_OILFIELD), /* 2 */ pasky@1454: 1 << M(STR_SV_STNAME_DOCKS), /* 3 */ pasky@1454: 0x1FF << M(STR_SV_STNAME_BUOY_1), /* 4 */ pasky@1454: 1 << M(STR_SV_STNAME_HELIPORT), /* 5 */ truelight@0: }; truelight@0: richk@10242: const Town *t = st->town; richk@10242: uint32 free_names = UINT32_MAX; truelight@0: richk@10724: const Station *s; richk@10724: FOR_ALL_STATIONS(s) { richk@10724: if (s != st && s->town == t) { richk@10724: uint str = M(s->string_id); richk@10724: if (str <= 0x20) { richk@10724: if (str == M(STR_SV_STNAME_FOREST)) { richk@10724: str = M(STR_SV_STNAME_WOODS); truelight@0: } richk@10724: ClrBit(free_names, str); truelight@0: } truelight@193: } truelight@0: } truelight@193: truelight@0: /* check default names */ richk@10242: uint32 tmp = free_names & _gen_station_name_bits[flag]; richk@10724: if (tmp != 0) return STR_SV_STNAME + FindFirstBit(tmp); truelight@0: truelight@0: /* check mine? */ rubidium@6871: if (HasBit(free_names, M(STR_SV_STNAME_MINES))) { richk@6743: if (CountMapSquareAround(tile, CMSAMine) >= 2) { richk@10724: return STR_SV_STNAME_MINES; pasky@1454: } truelight@0: } truelight@0: truelight@0: /* check close enough to town to get central as name? */ richk@6719: if (DistanceMax(tile, t->xy) < 8) { richk@10724: if (HasBit(free_names, M(STR_SV_STNAME))) return STR_SV_STNAME; richk@10724: richk@10724: if (HasBit(free_names, M(STR_SV_STNAME_CENTRAL))) return STR_SV_STNAME_CENTRAL; truelight@0: } truelight@0: truelight@0: /* Check lakeside */ rubidium@6871: if (HasBit(free_names, M(STR_SV_STNAME_LAKESIDE)) && tron@1472: DistanceFromEdge(tile) < 20 && richk@6743: CountMapSquareAround(tile, CMSAWater) >= 5) { richk@10724: return STR_SV_STNAME_LAKESIDE; pasky@1454: } truelight@0: truelight@0: /* Check woods */ rubidium@6871: if (HasBit(free_names, M(STR_SV_STNAME_WOODS)) && ( richk@6743: CountMapSquareAround(tile, CMSATree) >= 8 || richk@6743: CountMapSquareAround(tile, CMSAForest) >= 2) tron@1472: ) { richk@10991: return _settings_game.game_creation.landscape == LT_TROPIC ? STR_SV_STNAME_FOREST : STR_SV_STNAME_WOODS; truelight@0: } truelight@0: truelight@0: /* check elevation compared to town */ richk@10724: uint z = GetTileZ(tile); richk@10724: uint z2 = GetTileZ(t->xy); richk@10724: if (z < z2) { richk@10724: if (HasBit(free_names, M(STR_SV_STNAME_VALLEY))) return STR_SV_STNAME_VALLEY; richk@10724: } else if (z > z2) { richk@10724: if (HasBit(free_names, M(STR_SV_STNAME_HEIGHTS))) return STR_SV_STNAME_HEIGHTS; truelight@0: } truelight@0: truelight@0: /* check direction compared to town */ richk@10724: static const int8 _direction_and_table[] = { richk@10724: ~( (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ), richk@10724: ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_NORTH)) ), richk@10724: ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ), richk@10724: ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) ), richk@10724: }; richk@10724: richk@10724: free_names &= _direction_and_table[ richk@10724: (TileX(tile) < TileX(t->xy)) + richk@10724: (TileY(tile) < TileY(t->xy)) * 2]; truelight@0: richk@6719: 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)); richk@10724: return (tmp == 0) ? STR_SV_STNAME_FALLBACK : (STR_SV_STNAME + FindFirstBit(tmp)); truelight@0: } truelight@0: #undef M truelight@0: richk@10242: static Station *GetClosestStationFromTile(TileIndex tile) truelight@0: { tron@6378: uint threshold = 8; richk@10242: Station *best_station = NULL; richk@10242: Station *st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { tron@6378: if (st->facilities == 0 && st->owner == _current_player) { tron@1424: uint cur_dist = DistanceManhattan(tile, st->xy); tron@1424: tron@1424: if (cur_dist < threshold) { tron@1424: threshold = cur_dist; tron@1424: best_station = st; tron@1424: } truelight@0: } truelight@0: } truelight@0: truelight@0: return best_station; truelight@0: } truelight@0: rubidium@6871: /** Update the virtual coords needed to draw the station sign. rubidium@6871: * @param st Station to update for. rubidium@6871: */ truelight@0: static void UpdateStationVirtCoord(Station *st) truelight@0: { celestar@3422: Point pt = RemapCoords2(TileX(st->xy) * TILE_SIZE, TileY(st->xy) * TILE_SIZE); celestar@1217: truelight@0: pt.y -= 32; rubidium@6854: if (st->facilities & FACIL_AIRPORT && st->Airport() == AirportFTAClass::oil_rig) pt.y -= 16; truelight@0: tron@534: SetDParam(0, st->index); tron@534: SetDParam(1, st->facilities); truelight@0: UpdateViewportSignPos(&st->sign, pt.x, pt.y, STR_305C_0); truelight@0: } truelight@0: rubidium@6871: /** Update the virtual coords needed to draw the station sign for all stations. */ rubidium@6573: void UpdateAllStationVirtCoord() truelight@0: { richk@10242: Station *st; tron@2639: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: UpdateStationVirtCoord(st); truelight@0: } truelight@0: } truelight@0: rubidium@6870: /** rubidium@6870: * Update the station virt coords while making the modified parts dirty. rubidium@6870: * rubidium@6870: * This function updates the virt coords and mark the modified parts as dirty rubidium@6870: * rubidium@6870: * @param st The station to update the virt coords rubidium@6870: * @ingroup dirty rubidium@6870: */ truelight@0: static void UpdateStationVirtCoordDirty(Station *st) truelight@0: { KUDr@5916: st->MarkDirty(); truelight@0: UpdateStationVirtCoord(st); KUDr@5916: st->MarkDirty(); truelight@0: } truelight@0: rubidium@6871: /** Get a mask of the cargo types that the station accepts. rubidium@6871: * @param st Station to query rubidium@6871: * @return the expected mask rubidium@6871: */ tron@1424: static uint GetAcceptanceMask(const Station *st) truelight@0: { truelight@0: uint mask = 0; tron@6395: peter1138@6676: for (CargoID i = 0; i < NUM_CARGO; i++) { rubidium@6871: if (HasBit(st->goods[i].acceptance_pickup, GoodsEntry::ACCEPTANCE)) mask |= 1 << i; truelight@0: } truelight@0: return mask; truelight@0: } truelight@0: rubidium@6871: /** Items contains the two cargo names that are to be accepted or rejected. rubidium@6871: * msg is the string id of the message to display. rubidium@6871: */ peter1138@5941: static void ShowRejectOrAcceptNews(const Station *st, uint num_items, CargoID *cargo, StringID msg) truelight@0: { peter1138@5941: for (uint i = 0; i < num_items; i++) { peter1138@6417: SetDParam(i + 1, GetCargo(cargo[i])->name); truelight@0: } peter1138@5941: peter1138@5941: SetDParam(0, st->index); richk@10724: AddNewsItem(msg, NS_ACCEPTANCE, st->xy, 0); truelight@0: } truelight@0: rubidium@6871: /** richk@10242: * Get a list of the cargo types being produced around the tile (in a rectangle). richk@10242: * @param produced: Destination array of produced cargo richk@10242: * @param tile: Center of the search area richk@10242: * @param w: Width of the center richk@10242: * @param h: Height of the center richk@10242: * @param rad: Radius of the search area richk@10242: */ tron@1424: void GetProductionAroundTiles(AcceptedCargo produced, TileIndex tile, tron@1424: int w, int h, int rad) truelight@0: { tron@2133: memset(produced, 0, sizeof(AcceptedCargo)); truelight@0: tron@6395: int x = TileX(tile); tron@6395: int y = TileY(tile); truelight@0: rubidium@6871: /* expand the region by rad tiles on each side rubidium@6871: * while making sure that we remain inside the board. */ tron@6395: int x2 = min(x + w + rad, MapSizeX()); tron@6395: int x1 = max(x - rad, 0); tron@6395: tron@6395: int y2 = min(y + h + rad, MapSizeY()); tron@6395: int y1 = max(y - rad, 0); truelight@0: truelight@0: assert(x1 < x2); truelight@0: assert(y1 < y2); truelight@0: assert(w > 0); truelight@0: assert(h > 0); truelight@0: tron@6395: for (int yc = y1; yc != y2; yc++) { tron@6395: for (int xc = x1; xc != x2; xc++) { richk@10184: TileIndex tile = TileXY(xc, yc); richk@10184: richk@10184: if (!IsTileType(tile, MP_STATION)) { tron@6395: GetProducedCargoProc *gpc = _tile_type_procs[GetTileType(tile)]->get_produced_cargo_proc; truelight@0: if (gpc != NULL) { richk@10184: CargoID cargos[256]; // Required for CBID_HOUSE_PRODUCE_CARGO. richk@10184: memset(cargos, CT_INVALID, 256); tron@1424: truelight@0: gpc(tile, cargos); richk@6878: for (uint i = 0; i < lengthof(cargos); ++i) { richk@6878: if (cargos[i] != CT_INVALID) produced[cargos[i]]++; truelight@0: } truelight@0: } truelight@0: } tron@1424: } tron@1424: } truelight@0: } truelight@0: rubidium@6871: /** richk@10242: * Get a list of the cargo types that are accepted around the tile. richk@10242: * @param accepts: Destination array of accepted cargo richk@10242: * @param tile: Center of the search area richk@10242: * @param w: Width of the center richk@10242: * @param h: Height of the center richk@10242: * @param rad: Radius of the rectangular search area richk@10242: */ tron@1424: void GetAcceptanceAroundTiles(AcceptedCargo accepts, TileIndex tile, tron@1424: int w, int h, int rad) truelight@0: { tron@473: memset(accepts, 0, sizeof(AcceptedCargo)); truelight@0: tron@6395: int x = TileX(tile); tron@6395: int y = TileY(tile); truelight@0: rubidium@6871: /* expand the region by rad tiles on each side rubidium@6871: * while making sure that we remain inside the board. */ tron@6395: int x2 = min(x + w + rad, MapSizeX()); tron@6395: int y2 = min(y + h + rad, MapSizeY()); tron@6395: int x1 = max(x - rad, 0); tron@6395: int y1 = max(y - rad, 0); truelight@0: truelight@0: assert(x1 < x2); truelight@0: assert(y1 < y2); truelight@0: assert(w > 0); truelight@0: assert(h > 0); truelight@0: tron@6395: for (int yc = y1; yc != y2; yc++) { tron@6395: for (int xc = x1; xc != x2; xc++) { tron@1981: TileIndex tile = TileXY(xc, yc); tron@1424: tron@1035: if (!IsTileType(tile, MP_STATION)) { tron@473: AcceptedCargo ac; tron@473: tron@473: GetAcceptedCargo(tile, ac); tron@6395: for (uint i = 0; i < lengthof(ac); ++i) accepts[i] += ac[i]; truelight@0: } tron@1424: } tron@1424: } truelight@0: } truelight@0: richk@10724: static inline void MergePoint(Rect *rect, TileIndex tile) tron@1423: { richk@10724: int x = TileX(tile); richk@10724: int y = TileY(tile); richk@10724: richk@10724: if (rect->left > x) rect->left = x; richk@10724: if (rect->bottom > y) rect->bottom = y; richk@10724: if (rect->right < x) rect->right = x; richk@10724: if (rect->top < y) rect->top = y; tron@1423: } tron@1423: rubidium@6871: /** Update the acceptance for a station. rubidium@6871: * @param st Station to update rubidium@6871: * @param show_msg controls whether to display a message that acceptance was changed. rubidium@6871: */ truelight@0: static void UpdateStationAcceptance(Station *st, bool show_msg) truelight@0: { rubidium@6871: /* Don't update acceptance for a buoy */ tron@6395: if (st->IsBuoy()) return; tron@6395: richk@10724: Rect rect; richk@10724: rect.left = MapSizeX(); richk@10724: rect.bottom = MapSizeY(); richk@10724: rect.right = 0; richk@10724: rect.top = 0; truelight@0: truelight@0: /* old accepted goods types */ tron@6395: uint old_acc = GetAcceptanceMask(st); truelight@0: rubidium@6871: /* Put all the tiles that span an area in the table. */ truelight@0: if (st->train_tile != 0) { tron@1423: MergePoint(&rect, st->train_tile); richk@10242: MergePoint(&rect, st->train_tile + TileDiffXY(st->trainst_w - 1, st->trainst_h - 1)); truelight@0: } celestar@1217: truelight@0: if (st->airport_tile != 0) { richk@10242: const AirportFTAClass *afc = st->Airport(); tron@3876: tron@1423: MergePoint(&rect, st->airport_tile); richk@10242: MergePoint(&rect, st->airport_tile + TileDiffXY(afc->size_x - 1, afc->size_y - 1)); celestar@1217: } celestar@1217: tron@1423: if (st->dock_tile != 0) MergePoint(&rect, st->dock_tile); tron@1423: tron@6395: for (const RoadStop *rs = st->bus_stops; rs != NULL; rs = rs->next) { tron@6395: MergePoint(&rect, rs->xy); celestar@1217: } truelight@0: tron@6395: for (const RoadStop *rs = st->truck_stops; rs != NULL; rs = rs->next) { tron@6395: MergePoint(&rect, rs->xy); truelight@0: } celestar@1217: rubidium@6871: /* And retrieve the acceptance. */ tron@6395: AcceptedCargo accepts; richk@10724: if (rect.right >= rect.left) { tron@1423: GetAcceptanceAroundTiles( tron@1423: accepts, richk@10724: TileXY(rect.left, rect.bottom), richk@10724: rect.right - rect.left + 1, richk@10724: rect.top - rect.bottom + 1, richk@10991: _settings_game.station.modified_catchment ? FindCatchmentRadius(st) : (uint)CA_UNMODIFIED tron@1423: ); truelight@0: } else { truelight@0: memset(accepts, 0, sizeof(accepts)); truelight@0: } truelight@0: rubidium@6871: /* Adjust in case our station only accepts fewer kinds of goods */ peter1138@6676: for (CargoID i = 0; i < NUM_CARGO; i++) { truelight@0: uint amt = min(accepts[i], 15); truelight@0: rubidium@6871: /* Make sure the station can accept the goods type. */ peter1138@6655: bool is_passengers = IsCargoInClass(i, CC_PASSENGERS); peter1138@6655: if ((!is_passengers && !(st->facilities & (byte)~FACIL_BUS_STOP)) || richk@10242: (is_passengers && !(st->facilities & (byte)~FACIL_TRUCK_STOP))) { truelight@0: amt = 0; richk@10242: } truelight@0: rubidium@6868: SB(st->goods[i].acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, amt >= 8); truelight@0: } truelight@0: rubidium@6871: /* Only show a message in case the acceptance was actually changed. */ tron@6395: uint new_acc = GetAcceptanceMask(st); richk@10242: if (old_acc == new_acc) return; truelight@193: rubidium@6871: /* show a message to report that the acceptance was changed? */ truelight@0: if (show_msg && st->owner == _local_player && st->facilities) { peter1138@5941: /* List of accept and reject strings for different number of peter1138@5941: * cargo types */ peter1138@5941: static const StringID accept_msg[] = { peter1138@5941: STR_3040_NOW_ACCEPTS, peter1138@5941: STR_3041_NOW_ACCEPTS_AND, peter1138@5941: }; peter1138@5941: static const StringID reject_msg[] = { peter1138@5941: STR_303E_NO_LONGER_ACCEPTS, peter1138@5941: STR_303F_NO_LONGER_ACCEPTS_OR, peter1138@5941: }; peter1138@5941: peter1138@5941: /* Array of accepted and rejected cargo types */ peter1138@5941: CargoID accepts[2] = { CT_INVALID, CT_INVALID }; peter1138@5941: CargoID rejects[2] = { CT_INVALID, CT_INVALID }; peter1138@5941: uint num_acc = 0; peter1138@5941: uint num_rej = 0; peter1138@5941: peter1138@5941: /* Test each cargo type to see if its acceptange has changed */ peter1138@5941: for (CargoID i = 0; i < NUM_CARGO; i++) { rubidium@6871: if (HasBit(new_acc, i)) { rubidium@6871: if (!HasBit(old_acc, i) && num_acc < lengthof(accepts)) { peter1138@5941: /* New cargo is accepted */ peter1138@5941: accepts[num_acc++] = i; peter1138@5941: } truelight@0: } else { rubidium@6871: if (HasBit(old_acc, i) && num_rej < lengthof(rejects)) { peter1138@5941: /* Old cargo is no longer accepted */ peter1138@5941: rejects[num_rej++] = i; peter1138@5941: } truelight@0: } peter1138@5941: } peter1138@5941: peter1138@5941: /* Show news message if there are any changes */ peter1138@5941: if (num_acc > 0) ShowRejectOrAcceptNews(st, num_acc, accepts, accept_msg[num_acc - 1]); peter1138@5941: if (num_rej > 0) ShowRejectOrAcceptNews(st, num_rej, rejects, reject_msg[num_rej - 1]); truelight@0: } truelight@0: rubidium@6871: /* redraw the station view since acceptance changed */ rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_ACCEPTLIST); truelight@0: } truelight@0: KUDr@5098: static void UpdateStationSignCoord(Station *st) KUDr@5098: { tron@6395: const StationRect *r = &st->rect; KUDr@5927: rubidium@6871: if (r->IsEmpty()) return; /* no tiles belong to this station */ rubidium@6871: rubidium@6871: /* clamp sign coord to be inside the station rect */ rubidium@6871: st->xy = TileXY(ClampU(TileX(st->xy), r->left, r->right), ClampU(TileY(st->xy), r->top, r->bottom)); KUDr@5098: UpdateStationVirtCoordDirty(st); KUDr@5098: } KUDr@5098: rubidium@6871: /** This is called right after a station was deleted. rubidium@6871: * It checks if the whole station is free of substations, and if so, the station will be rubidium@6871: * deleted after a little while. rubidium@6871: * @param st Station rubidium@6871: */ rubidium@6871: static void DeleteStationIfEmpty(Station *st) tron@2639: { truelight@0: if (st->facilities == 0) { truelight@0: st->delete_ctr = 0; richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 0); darkvater@27: } KUDr@5098: /* station remains but it probably lost some parts - station sign should stay in the station boundaries */ KUDr@5098: UpdateStationSignCoord(st); truelight@0: } truelight@0: richk@6720: static CommandCost ClearTile_Station(TileIndex tile, byte flags); darkvater@977: rubidium@6871: /** Tries to clear the given area. rubidium@6871: * @param tile TileIndex to start check rubidium@6871: * @param w width of search area rubidium@6871: * @param h height of search area rubidium@6871: * @param flags operation to perform rubidium@6877: * @param invalid_dirs prohibited directions (set of DiagDirections) rubidium@6871: * @param station StationID to be queried and returned if available rubidium@6871: * @param check_clear if clearing tile should be performed (in wich case, cost will be added) rubidium@6871: * @return the cost in case of success, or an error code if it failed. rubidium@6871: */ richk@6804: CommandCost CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint32 invalid_dirs, StationID* station, const FSMportsSpec *fsmportspec, bool check_clear = true) truelight@0: { rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION); tron@1335: int allowed_z = -1; richk@6804: byte layout_set = GB(invalid_dirs, 0, 3); richk@6804: byte *b = NULL; richk@6804: if (fsmportspec != NULL) { richk@6804: b = fsmportspec->layouts[layout_set]; richk@6804: b++; //skip orientation richk@6804: b++; //skip minipic richk@6804: } richk@6718: bool do_tile_test; tron@6395: tron@6395: BEGIN_TILE_LOOP(tile_cur, w, h, tile) { celestar@5573: if (MayHaveBridgeAbove(tile_cur) && IsBridgeAbove(tile_cur)) { celestar@5573: return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: } celestar@5573: richk@6720: if (!EnsureNoVehicleOnGround(tile_cur)) return CMD_ERROR; truelight@0: tron@6395: uint z; tron@6395: Slope tileh = GetTileSlope(tile_cur, &z); truelight@0: celestar@2085: /* Prohibit building if rubidium@4549: * 1) The tile is "steep" (i.e. stretches two height levels) rubidium@4549: * -OR- rubidium@4549: * 2) The tile is non-flat if rubidium@4549: * a) the player building is an "old-school" AI rubidium@4549: * -OR- rubidium@4549: * b) the build_on_slopes switch is disabled rubidium@4549: */ tron@3636: if (IsSteepSlope(tileh) || richk@10991: ((_is_old_ai_player || !_settings_game.construction.build_on_slopes) && tileh != SLOPE_FLAT)) { tron@3183: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: tron@6395: int flat_z = z; tron@3636: if (tileh != SLOPE_FLAT) { rubidium@6877: /* need to check so the entrance to the station is not pointing at a slope. rubidium@6877: * This must be valid for all station tiles, as the user can remove single station tiles. */ rubidium@6877: if ((HasBit(invalid_dirs, DIAGDIR_NE) && !(tileh & SLOPE_NE)) || rubidium@6877: (HasBit(invalid_dirs, DIAGDIR_SE) && !(tileh & SLOPE_SE)) || rubidium@6877: (HasBit(invalid_dirs, DIAGDIR_SW) && !(tileh & SLOPE_SW)) || rubidium@6877: (HasBit(invalid_dirs, DIAGDIR_NW) && !(tileh & SLOPE_NW))) { tron@3183: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } richk@6720: cost.AddCost(_price.terraform); tron@3645: flat_z += TILE_HEIGHT; truelight@0: } truelight@193: richk@6718: // do tile tests if it is not an airport or airport tile is 255 richk@6718: do_tile_test = true; richk@6804: if (fsmportspec != NULL) { richk@6718: if ((byte)*b++ == (byte)255) { richk@6718: do_tile_test = false; richk@6718: } richk@6718: }; richk@6718: rubidium@6871: /* get corresponding flat level and make sure that all parts of the station have the same level. */ truelight@0: if (allowed_z == -1) { rubidium@6871: /* first tile */ truelight@0: allowed_z = flat_z; richk@6718: } else if (do_tile_test && (allowed_z != flat_z)) { richk@6770: /* test here whether build on height permitted. for FSM ports with layout masks, this is not an error richk@6770: * land checking is done in airport placement richk@6770: */ richk@6804: if (fsmportspec->layout_mask == NULL) return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: richk@6718: if (do_tile_test) { richk@6718: // if station is set, then we have special handling to allow building on top of already existing stations. richk@6718: // so station points to INVALID_STATION if we can build on any station. or it points to a station if we're only allowed to build richk@6718: // on exactly that station. richk@6718: if (station != NULL && IsTileType(tile_cur, MP_STATION)) { richk@6718: if (!IsRailwayStation(tile_cur)) { richk@6718: return ClearTile_Station(tile_cur, DC_AUTO); // get error message richk@6718: } else { richk@6718: StationID st = GetStationIndex(tile_cur); richk@6718: if (*station == INVALID_STATION) { richk@6718: *station = st; richk@6718: } else if (*station != st) { richk@6718: return_cmd_error(STR_3006_ADJOINS_MORE_THAN_ONE_EXISTING); richk@6718: } richk@6718: } richk@6720: } else if (check_clear) { rubidium@6871: bool water_tile = IsWaterTile(tile_cur); richk@6720: CommandCost ret = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR); richk@6718: if (CmdFailed(ret)) return ret; rubidium@6799: if (!water_tile) cost.AddCost(ret); truelight@0: } truelight@0: } tron@6395: } END_TILE_LOOP(tile_cur, w, h, tile) truelight@0: truelight@0: return cost; truelight@0: } truelight@0: richk@6770: CommandCost CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, StationID* station, bool check_clear = true) richk@6770: { richk@6804: return CheckFlatLandBelow(tile, w, h, flags, (uint32)invalid_dirs, station, NULL, check_clear); richk@6770: } richk@6770: richk@10242: static bool CanExpandRailroadStation(const Station *st, uint *fin, Axis axis) truelight@0: { tron@6395: uint curw = st->trainst_w; tron@6395: uint curh = st->trainst_h; tron@1977: TileIndex tile = fin[0]; truelight@0: uint w = fin[1]; truelight@0: uint h = fin[2]; truelight@0: richk@10991: if (_settings_game.station.nonuniform_stations) { rubidium@6871: /* determine new size of train station region.. */ tron@926: int x = min(TileX(st->train_tile), TileX(tile)); tron@926: int y = min(TileY(st->train_tile), TileY(tile)); tron@926: curw = max(TileX(st->train_tile) + curw, TileX(tile) + w) - x; tron@926: curh = max(TileY(st->train_tile) + curh, TileY(tile) + h) - y; tron@1981: tile = TileXY(x, y); truelight@0: } else { rubidium@6872: /* do not allow modifying non-uniform stations, rubidium@6872: * the uniform-stations code wouldn't handle it well */ rubidium@6872: BEGIN_TILE_LOOP(t, st->trainst_w, st->trainst_h, st->train_tile) rubidium@6872: if (!st->TileBelongsToRailStation(t)) { // there may be adjoined station rubidium@6877: _error_message = STR_NONUNIFORM_STATIONS_DISALLOWED; rubidium@6872: return false; rubidium@6872: } rubidium@6872: END_TILE_LOOP(t, st->trainst_w, st->trainst_h, st->train_tile) rubidium@6872: rubidium@6871: /* check so the orientation is the same */ celestar@3334: if (GetRailStationAxis(st->train_tile) != axis) { rubidium@6877: _error_message = STR_NONUNIFORM_STATIONS_DISALLOWED; Celestar@1189: return false; Celestar@1189: } truelight@0: rubidium@6871: /* check if the new station adjoins the old station in either direction */ tron@1981: if (curw == w && st->train_tile == tile + TileDiffXY(0, h)) { rubidium@6871: /* above */ truelight@0: curh += h; tron@1981: } else if (curw == w && st->train_tile == tile - TileDiffXY(0, curh)) { rubidium@6871: /* below */ tron@1981: tile -= TileDiffXY(0, curh); truelight@0: curh += h; tron@1981: } else if (curh == h && st->train_tile == tile + TileDiffXY(w, 0)) { rubidium@6871: /* to the left */ truelight@0: curw += w; tron@1981: } else if (curh == h && st->train_tile == tile - TileDiffXY(curw, 0)) { rubidium@6871: /* to the right */ tron@1981: tile -= TileDiffXY(curw, 0); truelight@0: curw += w; Celestar@1189: } else { rubidium@6877: _error_message = STR_NONUNIFORM_STATIONS_DISALLOWED; truelight@0: return false; Celestar@1189: } truelight@0: } rubidium@6871: /* make sure the final size is not too big. */ richk@10991: if (curw > _settings_game.station.station_spread || curh > _settings_game.station.station_spread) { Celestar@1189: _error_message = STR_306C_STATION_TOO_SPREAD_OUT; Celestar@1189: return false; Celestar@1189: } truelight@0: rubidium@6871: /* now tile contains the new value for st->train_tile rubidium@6871: * curw, curh contain the new value for width and height */ truelight@0: fin[0] = tile; truelight@0: fin[1] = curw; truelight@0: fin[2] = curh; truelight@0: return true; truelight@0: } truelight@0: tron@536: static inline byte *CreateSingle(byte *layout, int n) truelight@0: { truelight@0: int i = n; truelight@0: do *layout++ = 0; while (--i); richk@6719: layout[((n - 1) >> 1) - n] = 2; truelight@0: return layout; truelight@0: } truelight@0: tron@536: static inline byte *CreateMulti(byte *layout, int n, byte b) truelight@0: { truelight@0: int i = n; truelight@0: do *layout++ = b; while (--i); truelight@0: if (n > 4) { richk@6719: layout[0 - n] = 0; richk@6719: layout[n - 1 - n] = 0; truelight@0: } truelight@0: return layout; truelight@0: } truelight@0: belugas@3676: static void GetStationLayout(byte *layout, int numtracks, int plat_len, const StationSpec *statspec) truelight@0: { belugas@3676: if (statspec != NULL && statspec->lengths >= plat_len && belugas@3676: statspec->platforms[plat_len - 1] >= numtracks && belugas@3676: statspec->layouts[plat_len - 1][numtracks - 1]) { tron@449: /* Custom layout defined, follow it. */ belugas@3676: memcpy(layout, statspec->layouts[plat_len - 1][numtracks - 1], tron@1472: plat_len * numtracks); tron@449: return; tron@449: } tron@449: truelight@0: if (plat_len == 1) { truelight@0: CreateSingle(layout, numtracks); truelight@0: } else { tron@2639: if (numtracks & 1) layout = CreateSingle(layout, plat_len); tron@2639: numtracks >>= 1; truelight@0: truelight@0: while (--numtracks >= 0) { truelight@0: layout = CreateMulti(layout, plat_len, 4); truelight@0: layout = CreateMulti(layout, plat_len, 6); truelight@0: } truelight@0: } truelight@0: } truelight@0: Darkvater@1775: /** Build railroad station tron@3491: * @param tile_org starting position of station dragging/placement richk@6719: * @param flags operation to perform Darkvater@1775: * @param p1 various bitstuffed elements tron@6460: * - p1 = (bit 0) - orientation (Axis) tron@2140: * - p1 = (bit 8-15) - number of tracks tron@2140: * - p1 = (bit 16-23) - platform length richk@6719: * - p1 = (bit 24) - allow stations directly adjacent to other stations. Darkvater@1775: * @param p2 various bitstuffed elements Darkvater@1775: * - p2 = (bit 0- 3) - railtype (p2 & 0xF) peter1138@3587: * - p2 = (bit 8-15) - custom station class peter1138@3587: * - p2 = (bit 16-23) - custom station id truelight@0: */ richk@6720: CommandCost CmdBuildRailroadStation(TileIndex tile_org, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: /* Does the authority allow this? */ Darkvater@1775: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile_org)) return CMD_ERROR; rubidium@6872: if (!ValParamRailtype((RailType)(p2 & 0xF))) return CMD_ERROR; Darkvater@1775: Darkvater@1775: /* unpack parameters */ tron@6460: Axis axis = Extract(p1); tron@6395: uint numtracks = GB(p1, 8, 8); tron@6395: uint plat_len = GB(p1, 16, 8); richk@10242: richk@10242: int w_org, h_org; tron@3157: if (axis == AXIS_X) { tron@3157: w_org = plat_len; tron@3157: h_org = numtracks; tron@3157: } else { Darkvater@1775: h_org = plat_len; Darkvater@1775: w_org = numtracks; truelight@0: } truelight@0: richk@10991: if (h_org > _settings_game.station.station_spread || w_org > _settings_game.station.station_spread) return CMD_ERROR; Darkvater@1781: rubidium@6871: /* these values are those that will be stored in train_tile and station_platforms */ tron@6395: uint finalvalues[3]; truelight@0: finalvalues[0] = tile_org; truelight@0: finalvalues[1] = w_org; truelight@0: finalvalues[2] = h_org; truelight@0: rubidium@6871: /* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */ tron@6395: StationID est = INVALID_STATION; rubidium@6871: /* If DC_EXEC is in flag, do not want to pass it to CheckFlatLandBelow, because of a nice bug rubidium@6871: * for detail info, see: rubidium@6871: * https://sourceforge.net/tracker/index.php?func=detail&aid=1029064&group_id=103924&atid=636365 */ richk@10991: CommandCost ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags & ~DC_EXEC, 5 << axis, _settings_game.station.nonuniform_stations ? &est : NULL); tron@3183: if (CmdFailed(ret)) return ret; rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION, ret.GetCost() + (numtracks * _price.train_station_track + _price.train_station_length) * plat_len); truelight@0: richk@6719: Station *st = NULL; richk@6719: bool check_surrounding = true; richk@6719: richk@10991: if (_settings_game.station.adjacent_stations) { richk@6719: if (est != INVALID_STATION) { rubidium@6871: if (HasBit(p1, 24)) { richk@6719: /* You can't build an adjacent station over the top of one that richk@6719: * already exists. */ richk@6719: return_cmd_error(STR_MUST_REMOVE_RAILWAY_STATION_FIRST); richk@6719: } else { richk@6719: /* Extend the current station, and don't check whether it will richk@6719: * be near any other stations. */ richk@6719: st = GetStation(est); richk@6719: check_surrounding = false; richk@6719: } richk@6719: } else { richk@6719: /* There's no station here. Don't check the tiles surrounding this richk@6719: * one if the player wanted to build an adjacent station. */ rubidium@6871: if (HasBit(p1, 24)) check_surrounding = false; richk@6719: } richk@6719: } richk@6719: richk@6719: if (check_surrounding) { rubidium@6871: /* Make sure there are no similar stations around us. */ richk@6719: st = GetStationAround(tile_org, w_org, h_org, est); richk@6719: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; richk@6719: } truelight@0: rubidium@6871: /* See if there is a deleted station close to us. */ tron@6378: if (st == NULL) st = GetClosestStationFromTile(tile_org); truelight@0: truelight@0: if (st != NULL) { rubidium@6871: /* Reuse an existing station. */ tron@6390: if (st->owner != _current_player) truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); truelight@0: truelight@0: if (st->train_tile != 0) { rubidium@6871: /* check if we want to expanding an already existing station? */ richk@10991: if (_is_old_ai_player || !_settings_game.station.join_stations) truelight@0: return_cmd_error(STR_3005_TOO_CLOSE_TO_ANOTHER_RAILROAD); tron@3157: if (!CanExpandRailroadStation(st, finalvalues, axis)) Celestar@1189: return CMD_ERROR; truelight@0: } truelight@0: rubidium@6871: /* XXX can't we pack this in the "else" part of the if above? */ KUDr@5927: if (!st->rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TEST)) return CMD_ERROR; rubidium@4434: } else { KUDr@5916: /* allocate and initialize new station */ richk@10724: if (!Station::CanAllocateItem()) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING); richk@10724: richk@10724: if (flags & DC_EXEC) { richk@10724: st = new Station(tile_org); richk@10724: richk@10724: st->town = ClosestTownFromTile(tile_org, UINT_MAX); richk@10724: st->string_id = GenerateStationName(st, tile_org, STATIONNAMING_RAIL); richk@10724: richk@10724: if (IsValidPlayer(_current_player)) { richk@10724: SetBit(st->town->have_ratings, _current_player); richk@10724: } KUDr@5916: } truelight@0: } truelight@0: peter1138@3587: /* Check if the given station class is valid */ rubidium@6872: if (GB(p2, 8, 8) >= GetNumStationClasses()) return CMD_ERROR; peter1138@3587: peter1138@3587: /* Check if we can allocate a custom stationspec to this station */ tron@6395: const StationSpec *statspec = GetCustomStationSpec((StationClassID)GB(p2, 8, 8), GB(p2, 16, 8)); tron@6395: int specindex = AllocateSpecToStation(statspec, st, flags & DC_EXEC); peter1138@3587: if (specindex == -1) return CMD_ERROR; peter1138@3587: peter1138@3754: if (statspec != NULL) { peter1138@3754: /* Perform NewStation checks */ peter1138@3754: peter1138@3754: /* Check if the station size is permitted */ rubidium@6871: if (HasBit(statspec->disallowed_platforms, numtracks - 1) || HasBit(statspec->disallowed_lengths, plat_len - 1)) { peter1138@3754: return CMD_ERROR; peter1138@3754: } peter1138@3754: peter1138@3754: /* Check if the station is buildable */ richk@10724: if (HasBit(statspec->callbackmask, CBM_STATION_AVAIL) && GB(GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE), 0, 8) == 0) { peter1138@3754: return CMD_ERROR; peter1138@3754: } peter1138@3754: } peter1138@3754: truelight@0: if (flags & DC_EXEC) { tron@1977: TileIndexDiff tile_delta; truelight@0: byte *layout_ptr; peter1138@3769: byte numtracks_orig; peter1138@2824: Track track; truelight@0: rubidium@6871: /* Now really clear the land below the station rubidium@6871: * It should never return CMD_ERROR.. but you never know ;) rubidium@6871: * (a bit strange function name for it, but it really does clear the land, when DC_EXEC is in flags) */ richk@10991: ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << axis, _settings_game.station.nonuniform_stations ? &est : NULL); tron@3183: if (CmdFailed(ret)) return ret; truelight@313: truelight@0: st->train_tile = finalvalues[0]; KUDr@5972: st->AddFacility(FACIL_TRAIN, finalvalues[0]); truelight@0: truelight@0: st->trainst_w = finalvalues[1]; truelight@0: st->trainst_h = finalvalues[2]; truelight@193: KUDr@5927: st->rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TRY); KUDr@5583: richk@10274: if (statspec != NULL) { richk@10274: /* Include this station spec's animation trigger bitmask richk@10274: * in the station's cached copy. */ richk@10274: st->cached_anim_triggers |= statspec->anim_triggers; richk@10274: } richk@10274: tron@3157: tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); tron@4158: track = AxisToTrack(axis); truelight@193: richk@10994: layout_ptr = AllocaM(byte, numtracks * plat_len); tron@449: GetStationLayout(layout_ptr, numtracks, plat_len, statspec); truelight@193: peter1138@3769: numtracks_orig = numtracks; peter1138@3769: truelight@0: do { Darkvater@1775: TileIndex tile = tile_org; truelight@0: int w = plat_len; truelight@0: do { peter1138@3769: byte layout = *layout_ptr++; richk@6719: MakeRailStation(tile, st->owner, st->index, axis, layout & ~1, (RailType)GB(p2, 0, 4)); peter1138@3587: SetCustomStationSpecIndex(tile, specindex); peter1138@3742: SetStationTileRandomBits(tile, GB(Random(), 0, 4)); richk@10274: SetStationAnimationFrame(tile, 0); truelight@0: peter1138@3754: if (statspec != NULL) { peter1138@3784: /* Use a fixed axis for GetPlatformInfo as our platforms / numtracks are always the right way around */ peter1138@3784: uint32 platinfo = GetPlatformInfo(AXIS_X, 0, plat_len, numtracks_orig, plat_len - w, numtracks_orig - numtracks, false); rubidium@6871: rubidium@6871: /* As the station is not yet completely finished, the station does not yet exist. */ rubidium@6871: uint16 callback = GetStationCallback(CBID_STATION_TILE_LAYOUT, platinfo, 0, statspec, NULL, tile); richk@6719: if (callback != CALLBACK_FAILED && callback < 8) SetStationGfx(tile, (callback & ~1) + axis); richk@10274: richk@10274: /* Trigger station animation -- after building? */ richk@10274: StationAnimationTrigger(st, tile, STAT_ANIM_BUILT); peter1138@3754: } peter1138@3754: truelight@0: tile += tile_delta; truelight@0: } while (--w); rubidium@6872: AddTrackToSignalBuffer(tile_org, track, _current_player); KUDr@3900: YapfNotifyTrackLayoutChange(tile_org, track); tron@1981: tile_org += tile_delta ^ TileDiffXY(1, 1); // perpendicular to tile_delta truelight@0: } while (--numtracks); truelight@0: richk@6720: st->MarkTilesDirty(false); truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 0); rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_TRAINS); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static void MakeRailwayStationAreaSmaller(Station *st) truelight@0: { truelight@0: uint w = st->trainst_w; truelight@0: uint h = st->trainst_h; tron@1977: TileIndex tile = st->train_tile; truelight@0: truelight@0: restart: truelight@193: rubidium@6871: /* too small? */ truelight@0: if (w != 0 && h != 0) { rubidium@6871: /* check the left side, x = constant, y changes */ tron@6395: for (uint i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(0, i));) { rubidium@6871: /* the left side is unused? */ tron@1981: if (++i == h) { tron@1981: tile += TileDiffXY(1, 0); tron@1981: w--; tron@1981: goto restart; tron@1981: } tron@1981: } truelight@0: rubidium@6871: /* check the right side, x = constant, y changes */ tron@6395: for (uint i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(w - 1, i));) { rubidium@6871: /* the right side is unused? */ tron@1981: if (++i == h) { tron@1981: w--; tron@1981: goto restart; tron@1981: } tron@1981: } truelight@0: rubidium@6871: /* check the upper side, y = constant, x changes */ tron@6395: for (uint i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(i, 0));) { rubidium@6871: /* the left side is unused? */ tron@1981: if (++i == w) { tron@1981: tile += TileDiffXY(0, 1); tron@1981: h--; tron@1981: goto restart; tron@1981: } tron@1981: } truelight@0: rubidium@6871: /* check the lower side, y = constant, x changes */ tron@6395: for (uint i = 0; !st->TileBelongsToRailStation(tile + TileDiffXY(i, h - 1));) { rubidium@6871: /* the left side is unused? */ tron@1981: if (++i == w) { tron@1981: h--; tron@1981: goto restart; tron@1981: } tron@1981: } truelight@0: } else { truelight@0: tile = 0; truelight@0: } truelight@0: truelight@0: st->trainst_w = w; truelight@0: st->trainst_h = h; truelight@0: st->train_tile = tile; truelight@0: } truelight@0: Darkvater@1782: /** Remove a single tile from a railroad station. Darkvater@1782: * This allows for custom-built station with holes and weird layouts tron@3491: * @param tile tile of station piece to remove richk@6719: * @param flags operation to perform richk@6719: * @param p1 start_tile Darkvater@1782: * @param p2 unused Darkvater@1782: */ richk@6720: CommandCost CmdRemoveFromRailroadStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { richk@6719: TileIndex start = p1 == 0 ? tile : p1; richk@6719: richk@6719: /* Count of the number of tiles removed */ richk@6719: int quantity = 0; richk@6719: richk@6719: if (tile >= MapSize() || start >= MapSize()) return CMD_ERROR; richk@6719: richk@6719: /* make sure sx,sy are smaller than ex,ey */ richk@6719: int ex = TileX(tile); richk@6719: int ey = TileY(tile); richk@6719: int sx = TileX(start); richk@6719: int sy = TileY(start); richk@6719: if (ex < sx) Swap(ex, sx); richk@6719: if (ey < sy) Swap(ey, sy); richk@6719: tile = TileXY(sx, sy); richk@6719: richk@6719: int size_x = ex - sx + 1; richk@6719: int size_y = ey - sy + 1; richk@6719: richk@6719: /* Do the action for every tile into the area */ richk@6719: BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) { rubidium@6872: /* Make sure the specified tile is a railroad station */ rubidium@6872: if (!IsTileType(tile2, MP_STATION) || !IsRailwayStation(tile2)) { rubidium@6872: continue; rubidium@6872: } rubidium@6872: rubidium@6872: /* If there is a vehicle on ground, do not allow to remove (flood) the tile */ rubidium@6872: if (!EnsureNoVehicleOnGround(tile2)) { richk@6719: continue; truelight@0: } richk@6719: richk@6719: /* Check ownership of station */ richk@6719: Station *st = GetStationByTile(tile2); rubidium@6872: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) { richk@6719: continue; richk@6719: } richk@6719: rubidium@6872: /* Do not allow removing from stations if non-uniform stations are not enabled rubidium@6872: * The check must be here to give correct error message richk@10242: */ richk@10991: if (!_settings_game.station.nonuniform_stations) return_cmd_error(STR_NONUNIFORM_STATIONS_DISALLOWED); rubidium@6872: richk@6719: /* If we reached here, the tile is valid so increase the quantity of tiles we will remove */ richk@6719: quantity++; richk@6719: richk@6719: if (flags & DC_EXEC) { rubidium@6872: /* read variables before the station tile is removed */ richk@6719: uint specindex = GetCustomStationSpecIndex(tile2); richk@6719: Track track = GetRailStationTrack(tile2); rubidium@6872: Owner owner = GetTileOwner(tile2); rubidium@6872: richk@6719: DoClearSquare(tile2); richk@6719: st->rect.AfterRemoveTile(st, tile2); rubidium@6872: AddTrackToSignalBuffer(tile2, track, owner); richk@6719: YapfNotifyTrackLayoutChange(tile2, track); richk@6719: richk@6719: DeallocateSpecFromStation(st, specindex); richk@6719: rubidium@6871: /* now we need to make the "spanned" area of the railway station smaller rubidium@6871: * if we deleted something at the edges. rubidium@6871: * we also need to adjust train_tile. */ richk@6719: MakeRailwayStationAreaSmaller(st); richk@6720: st->MarkTilesDirty(false); richk@6719: UpdateStationSignCoord(st); richk@6719: rubidium@6871: /* if we deleted the whole station, delete the train facility. */ richk@6719: if (st->train_tile == 0) { richk@6719: st->facilities &= ~FACIL_TRAIN; rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_TRAINS); richk@6719: UpdateStationVirtCoordDirty(st); richk@6719: DeleteStationIfEmpty(st); richk@6719: } richk@6719: } richk@6719: } END_TILE_LOOP(tile2, size_x, size_y, tile) richk@6719: richk@6719: /* If we've not removed any tiles, give an error */ richk@6719: if (quantity == 0) return CMD_ERROR; richk@6719: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_rail_station * quantity); truelight@0: } truelight@0: celestar@3928: richk@6720: static CommandCost RemoveRailroadStation(Station *st, TileIndex tile, uint32 flags) truelight@0: { darkvater@149: /* if there is flooding and non-uniform stations are enabled, remove platforms tile by tile */ richk@10991: if (_current_player == OWNER_WATER && _settings_game.station.nonuniform_stations) { tron@3491: return DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_FROM_RAILROAD_STATION); richk@10242: } darkvater@149: truelight@0: /* Current player owns the station? */ richk@10242: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) return CMD_ERROR; truelight@0: truelight@0: /* determine width and height of platforms */ truelight@0: tile = st->train_tile; tron@6395: int w = st->trainst_w; tron@6395: int h = st->trainst_h; truelight@193: truelight@0: assert(w != 0 && h != 0); truelight@0: rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION); truelight@0: /* clear all areas of the station */ truelight@0: do { truelight@0: int w_bak = w; truelight@0: do { rubidium@6872: /* for nonuniform stations, only remove tiles that are actually train station tiles */ KUDr@5916: if (st->TileBelongsToRailStation(tile)) { rubidium@6871: if (!EnsureNoVehicleOnGround(tile)) truelight@0: return CMD_ERROR; richk@6720: cost.AddCost(_price.remove_rail_station); peter1138@2824: if (flags & DC_EXEC) { rubidium@6872: /* read variables before the station tile is removed */ celestar@3334: Track track = GetRailStationTrack(tile); rubidium@6872: Owner owner = GetTileOwner(tile); // _current_player can be OWNER_WATER truelight@0: DoClearSquare(tile); rubidium@6872: AddTrackToSignalBuffer(tile, track, owner); KUDr@3900: YapfNotifyTrackLayoutChange(tile, track); peter1138@2824: } truelight@0: } tron@1981: tile += TileDiffXY(1, 0); truelight@0: } while (--w); truelight@0: w = w_bak; tron@1981: tile += TileDiffXY(-w, 1); truelight@0: } while (--h); truelight@0: truelight@0: if (flags & DC_EXEC) { KUDr@5927: st->rect.AfterRemoveRect(st, st->train_tile, st->trainst_w, st->trainst_h); KUDr@5583: truelight@0: st->train_tile = 0; KUDr@5583: st->trainst_w = st->trainst_h = 0; truelight@0: st->facilities &= ~FACIL_TRAIN; truelight@0: peter1138@3587: free(st->speclist); peter1138@3587: st->num_specs = 0; peter1138@3587: st->speclist = NULL; richk@10274: st->cached_anim_triggers = 0; peter1138@3587: rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_TRAINS); truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@193: truelight@0: return cost; truelight@0: } truelight@0: celestar@6694: /** richk@10184: * @param truck_station Determines whether a stop is ROADSTOP_BUS or ROADSTOP_TRUCK richk@6719: * @param st The Station to do the whole procedure for tron@6157: * @return a pointer to where to link a new RoadStop* rubidium@4549: */ richk@10242: static RoadStop **FindRoadStopSpot(bool truck_station, Station *st) celestar@1217: { celestar@3691: RoadStop **primary_stop = (truck_station) ? &st->truck_stops : &st->bus_stops; celestar@1217: celestar@1217: if (*primary_stop == NULL) { richk@6719: /* we have no roadstop of the type yet, so write a "primary stop" */ tron@6157: return primary_stop; celestar@1217: } else { richk@6719: /* there are stops already, so append to the end of the list */ tron@6157: RoadStop *stop = *primary_stop; tron@6157: while (stop->next != NULL) stop = stop->next; tron@6157: return &stop->next; celestar@1217: } celestar@1217: } celestar@1217: celestar@3691: /** Build a bus or truck stop celestar@3691: * @param tile tile to build the stop at richk@6719: * @param flags operation to perform tron@3333: * @param p1 entrance direction (DiagDirection) rubidium@6338: * @param p2 bit 0: 0 for Bus stops, 1 for truck stops rubidium@6338: * bit 1: 0 for normal, 1 for drive-through richk@6719: * bit 2..4: the roadtypes richk@6719: * bit 5: allow stations directly adjacent to other stations. truelight@0: */ richk@6720: CommandCost CmdBuildRoadStop(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@6871: bool type = HasBit(p2, 0); rubidium@6871: bool is_drive_through = HasBit(p2, 1); richk@6878: bool build_over_road = is_drive_through && IsNormalRoadTile(tile); richk@10731: bool town_owned_road = false; richk@6719: RoadTypes rts = (RoadTypes)GB(p2, 2, 3); richk@6719: rubidium@6871: if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR; richk@6719: richk@6719: /* Trams only have drive through stops */ rubidium@6871: if (!is_drive_through && HasBit(rts, ROADTYPE_TRAM)) return CMD_ERROR; Darkvater@1782: Darkvater@1782: /* Saveguard the parameters */ rubidium@5838: if (!IsValidDiagDirection((DiagDirection)p1)) return CMD_ERROR; rubidium@6338: /* If it is a drive-through stop check for valid axis */ rubidium@6338: if (is_drive_through && !IsValidAxis((Axis)p1)) return CMD_ERROR; rubidium@6344: /* Road bits in the wrong direction */ richk@6719: if (build_over_road && (GetAllRoadBits(tile) & ((Axis)p1 == AXIS_X ? ROAD_Y : ROAD_X)) != 0) return_cmd_error(STR_DRIVE_THROUGH_ERROR_DIRECTION); truelight@0: richk@6719: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) return CMD_ERROR; richk@6719: richk@6719: /* Not allowed to build over this road */ richk@6719: if (build_over_road) { rubidium@6872: RoadTypes cur_rts = GetRoadTypes(tile); rubidium@6872: rubidium@6872: /* there is a road, check if we can build road+tram stop over it */ rubidium@6872: if (HasBit(cur_rts, ROADTYPE_ROAD)) { rubidium@6872: Owner road_owner = GetRoadOwner(tile, ROADTYPE_ROAD); richk@10731: if (road_owner == OWNER_TOWN) { richk@10731: town_owned_road = true; richk@10991: if (!_settings_game.construction.road_stop_on_town_road) return_cmd_error(STR_DRIVE_THROUGH_ERROR_ON_TOWN_ROAD); richk@10731: } else { richk@10731: if (road_owner != OWNER_NONE && !CheckOwnership(road_owner)) return CMD_ERROR; richk@10731: } rubidium@6872: } rubidium@6872: rubidium@6872: /* there is a tram, check if we can build road+tram stop over it */ rubidium@6872: if (HasBit(cur_rts, ROADTYPE_TRAM)) { rubidium@6872: Owner tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM); rubidium@6872: if (tram_owner != OWNER_NONE && !CheckOwnership(tram_owner)) return CMD_ERROR; rubidium@6872: } rubidium@6872: richk@6719: /* Don't allow building the roadstop when vehicles are already driving on it */ richk@6719: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; richk@6719: richk@6719: /* Do not remove roadtypes! */ richk@6719: rts |= cur_rts; richk@6719: } richk@10242: CommandCost cost = CheckFlatLandBelow(tile, 1, 1, flags, is_drive_through ? 5 << p1 : 1 << p1, NULL, !build_over_road); richk@6719: if (CmdFailed(cost)) return cost; richk@6719: richk@6719: Station *st = NULL; richk@6719: richk@10991: if (!_settings_game.station.adjacent_stations || !HasBit(p2, 5)) { richk@6719: st = GetStationAround(tile, 1, 1, INVALID_STATION); richk@6719: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; richk@6719: } truelight@0: truelight@0: /* Find a station close to us */ tron@6378: if (st == NULL) st = GetClosestStationFromTile(tile); truelight@0: rubidium@6871: /* give us a road stop in the list, and check if something went wrong */ richk@10724: if (!RoadStop::CanAllocateItem()) return_cmd_error(type ? STR_TOO_MANY_TRUCK_STOPS : STR_TOO_MANY_BUS_STOPS); glx@6084: tron@2989: if (st != NULL && richk@10184: GetNumRoadStopsInStation(st, ROADSTOP_BUS) + GetNumRoadStopsInStation(st, ROADSTOP_TRUCK) >= RoadStop::LIMIT) { rubidium@6877: return_cmd_error(type ? STR_TOO_MANY_TRUCK_STOPS : STR_TOO_MANY_BUS_STOPS); tron@2951: } celestar@1217: truelight@0: if (st != NULL) { tron@6390: if (st->owner != _current_player) { truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); tron@2951: } tron@2951: KUDr@5927: if (!st->rect.BeforeAddTile(tile, StationRect::ADD_TEST)) return CMD_ERROR; truelight@0: } else { KUDr@5916: /* allocate and initialize new station */ richk@10724: if (!Station::CanAllocateItem()) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING); richk@10724: richk@10724: if (flags & DC_EXEC) { richk@10724: st = new Station(tile); richk@10724: richk@10724: st->town = ClosestTownFromTile(tile, UINT_MAX); richk@10724: st->string_id = GenerateStationName(st, tile, STATIONNAMING_ROAD); richk@10724: richk@10724: if (IsValidPlayer(_current_player)) { richk@10724: SetBit(st->town->have_ratings, _current_player); richk@10724: } richk@10724: st->sign.width_1 = 0; tron@2951: } truelight@0: } truelight@0: richk@6720: cost.AddCost((type) ? _price.build_truck_station : _price.build_bus_station); truelight@0: truelight@0: if (flags & DC_EXEC) { richk@10724: RoadStop *road_stop = new RoadStop(tile); rubidium@6871: /* Insert into linked list of RoadStops */ tron@6157: RoadStop **currstop = FindRoadStopSpot(type, st); celestar@1217: *currstop = road_stop; celestar@1217: rubidium@6871: /*initialize an empty station */ KUDr@5972: st->AddFacility((type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP, tile); truelight@0: KUDr@5927: st->rect.BeforeAddTile(tile, StationRect::ADD_TRY); KUDr@5583: richk@10184: RoadStopType rs_type = type ? ROADSTOP_TRUCK : ROADSTOP_BUS; tron@6424: if (is_drive_through) { richk@6719: MakeDriveThroughRoadStop(tile, st->owner, st->index, rs_type, rts, (Axis)p1, town_owned_road); tron@6424: } else { richk@6719: MakeRoadStop(tile, st->owner, st->index, rs_type, rts, (DiagDirection)p1); tron@6424: } truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 0); rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_ROADVEHS); truelight@0: } truelight@0: return cost; truelight@0: } truelight@0: richk@6878: richk@6878: static void *ClearRoadStopStatusEnum(Vehicle *v, void *) richk@6878: { richk@6878: if (v->type == VEH_ROAD) ClrBit(v->u.road.state, RVS_IN_DT_ROAD_STOP); richk@6878: richk@6878: return NULL; richk@6878: } richk@6878: richk@6878: rubidium@6871: /** Remove a bus station rubidium@6871: * @param st Station to remove rubidium@6871: * @param flags operation to perform rubidium@6871: * @param tile TileIndex been queried rubidium@6871: * @return cost or failure of operation rubidium@6871: */ richk@6720: static CommandCost RemoveRoadStop(Station *st, uint32 flags, TileIndex tile) truelight@0: { tron@2951: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) { truelight@0: return CMD_ERROR; tron@2951: } truelight@0: tron@6395: bool is_truck = IsTruckStop(tile); tron@6395: tron@6395: RoadStop **primary_stop; tron@6395: RoadStop *cur_stop; tron@2639: if (is_truck) { // truck stop celestar@1217: primary_stop = &st->truck_stops; richk@10184: cur_stop = GetRoadStopByTile(tile, ROADSTOP_TRUCK); celestar@1217: } else { celestar@1217: primary_stop = &st->bus_stops; richk@10184: cur_stop = GetRoadStopByTile(tile, ROADSTOP_BUS); celestar@1217: } celestar@1217: celestar@1217: assert(cur_stop != NULL); truelight@193: richk@6878: /* don't do the check for drive-through road stops when company bankrupts */ richk@6878: if (IsDriveThroughStopTile(tile) && (flags & DC_BANKRUPT)) { richk@6878: /* remove the 'going through road stop' status from all vehicles on that tile */ richk@6878: if (flags & DC_EXEC) VehicleFromPos(tile, NULL, &ClearRoadStopStatusEnum); richk@6878: } else { richk@6878: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; richk@6878: } truelight@0: truelight@0: if (flags & DC_EXEC) { tron@6118: if (*primary_stop == cur_stop) { rubidium@6871: /* removed the first stop in the list */ tron@6118: *primary_stop = cur_stop->next; rubidium@6871: /* removed the only stop? */ tron@6118: if (*primary_stop == NULL) { tron@6118: st->facilities &= (is_truck ? ~FACIL_TRUCK_STOP : ~FACIL_BUS_STOP); tron@6118: } tron@6118: } else { rubidium@6871: /* tell the predecessor in the list to skip this stop */ tron@6118: RoadStop *pred = *primary_stop; tron@6118: while (pred->next != cur_stop) pred = pred->next; tron@6118: pred->next = cur_stop->next; celestar@1217: } truelight@0: rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_ROADVEHS); celestar@5959: delete cur_stop; truelight@4398: DoClearSquare(tile); KUDr@5927: st->rect.AfterRemoveTile(st, tile); truelight@4398: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, (is_truck) ? _price.remove_truck_station : _price.remove_bus_station); truelight@0: } truelight@0: rubidium@6338: /** Remove a bus or truck stop rubidium@6338: * @param tile tile to remove the stop from richk@6719: * @param flags operation to perform rubidium@6338: * @param p1 not used rubidium@6338: * @param p2 bit 0: 0 for Bus stops, 1 for truck stops rubidium@6338: */ richk@6720: CommandCost CmdRemoveRoadStop(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) rubidium@6338: { rubidium@6338: /* Make sure the specified tile is a road stop of the correct type */ richk@10994: if (!IsTileType(tile, MP_STATION) || !IsRoadStop(tile) || (uint32)GetRoadStopType(tile) != GB(p2, 0, 1)) return CMD_ERROR; tron@6395: Station *st = GetStationByTile(tile); rubidium@6338: /* Save the stop info before it is removed */ tron@6395: bool is_drive_through = IsDriveThroughStopTile(tile); richk@6719: RoadTypes rts = GetRoadTypes(tile); richk@6719: RoadBits road_bits = IsDriveThroughStopTile(tile) ? richk@6719: ((GetRoadStopDir(tile) == DIAGDIR_NE) ? ROAD_X : ROAD_Y) : richk@6719: DiagDirToRoadBits(GetRoadStopDir(tile)); tron@6395: bool is_towns_road = is_drive_through && GetStopBuiltOnTownRoad(tile); tron@6395: richk@6720: CommandCost ret = RemoveRoadStop(st, flags, tile); rubidium@6338: rubidium@6338: /* If the stop was a drive-through stop replace the road */ richk@6720: if ((flags & DC_EXEC) && CmdSucceeded(ret) && is_drive_through) { richk@6719: /* Rebuild the drive throuhg road stop. As a road stop can only be richk@6719: * removed by the owner of the roadstop, _current_player is the richk@6719: * owner of the road stop. */ richk@10724: MakeRoadNormal(tile, road_bits, rts, is_towns_road ? ClosestTownFromTile(tile, UINT_MAX)->index : 0, richk@6719: is_towns_road ? OWNER_TOWN : _current_player, _current_player, _current_player); rubidium@6338: } rubidium@6338: rubidium@6338: return ret; rubidium@6338: } truelight@0: richk@10731: /** Recalculate the noise generated by the airports of each town */ richk@10731: void UpdateAirportsNoise() richk@10731: { richk@10731: Town *t; richk@10731: const Station *st; richk@10731: richk@10731: FOR_ALL_TOWNS(t) t->noise_reached = 0; richk@10731: richk@10731: FOR_ALL_STATIONS(st) { richk@10731: if (IsAirport(st->xy)) { richk@10731: st->town->noise_reached += GetAirportNoiseLevelForTown(st->Airport(), st->town->xy, st->xy); richk@10731: } richk@10731: } richk@10731: } richk@10731: richk@10731: /** Get a possible noise reduction factor based on distance from town center. richk@10731: * The further you get, the less noise you generate. richk@10731: * So all those folks at city council can now happily slee... work in their offices richk@10731: * @param afc AirportFTAClass pointer of the class being proposed richk@10731: * @param town_tile TileIndex of town's center, the one who will receive the airport's candidature richk@10731: * @param tile TileIndex where the new airport might be built richk@10731: * @return the noise that will be generated, according to distance richk@10731: */ richk@10731: uint8 GetAirportNoiseLevelForTown(const AirportFTAClass *afc, TileIndex town_tile, TileIndex tile) richk@10731: { richk@10731: struct TileIndexDistance { richk@10731: TileIndex index; richk@10731: uint distance; richk@10731: }; richk@10731: richk@10731: uint distance; richk@10731: richk@10731: /* 0 cannot be accounted, and 1 is the lowest that can be reduced from town. richk@10731: * So no need to go any further*/ richk@10731: if (afc->noise_level < 2) return afc->noise_level; richk@10731: richk@10731: /* Find the airport-to-be's closest corner to the town */ richk@10731: if (afc->size_x == 1 && afc->size_y == 1) { richk@10731: distance = DistanceManhattan(town_tile, tile); // ont tile, one corner, it's THE corner richk@10731: } else { richk@10731: /* calculate manhattan distance to town of each side of the airport */ richk@10731: TileIndexDistance dist[4]; ///< Array that will contain all 4 corners in TileIndex and distance richk@10731: uint min_tile = UINT_MAX; ///< Sentinel value richk@10731: uint min_indice = 4; richk@10731: richk@10731: /* Based on the size of the airport, establish location of each corner*/ richk@10731: dist[0].index = tile; // north tile richk@10731: dist[1].index = TileAddWrap(tile, afc->size_x - 1, afc->size_y - 1); // south tile richk@10731: dist[2].index = TileAddWrap(tile, afc->size_x - 1, 0); // west tile richk@10731: dist[3].index = TileAddWrap(tile, 0, afc->size_y - 1); //east tile richk@10731: richk@10731: /* now, go and find the smallest one, thus the closest to town */ richk@10731: for (uint i = 0; i < 4; i++) { richk@10731: dist[i].distance = DistanceManhattan(town_tile, dist[i].index); richk@10731: richk@10731: if (dist[i].distance < min_tile) { richk@10731: min_tile = dist[i].distance; // here's a new candidate richk@10731: min_indice = i; richk@10731: } richk@10731: } richk@10731: richk@10731: distance = dist[min_indice].distance; richk@10731: } richk@10731: richk@10731: /* The steps for measuring noise reduction are based on the "magical" (and arbitrary) 8 base distance richk@10731: * adding the town_council_tolerance 4 times, as a way to graduate, depending of the tolerance. richk@10731: * Basically, it says that the less tolerant a town is, the bigger the distance before richk@10731: * an actual decrease can be granted */ richk@10991: uint8 town_tolerance_distance = 8 + (_settings_game.difficulty.town_council_tolerance * 4); richk@10731: richk@10731: /* now, we want to have the distance segmented using the distance judged bareable by town richk@10731: * This will give us the coefficient of reduction the distance provides. */ richk@10991: uint noise_reduction = distance / town_tolerance_distance; richk@10991: richk@10991: /* If the noise reduction equals the airport noise itself, don't give it for free. richk@10731: * Otherwise, simply reduce the airport's level. */ richk@10991: return noise_reduction >= afc->noise_level ? 1 : afc->noise_level - noise_reduction; richk@10731: } richk@10731: Darkvater@1784: /** Place an Airport. tron@3491: * @param tile tile where airport will be built richk@6719: * @param flags operation to perform richk@6722: * @param p1 various bitstuffed elements richk@6745: * - p1 = (bit 0-2) - layout_set for newgrf richk@6722: * - p1 = (bit 8-15) - length richk@6722: * - p1 = (bit 16-23) - width richk@6722: * - p1 = (bit 24) - allow stations directly adjacent to other stations. richk@6722: * @param p2 various bitstuffed elements richk@6722: * - p2 = (bit 8-15) - custom fsmport class richk@6722: * - p2 = (bit 16-23) - custom fsmport id truelight@0: */ richk@6720: CommandCost CmdBuildAirport(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@193: bool airport_upgrade = true; richk@6745: byte layout_set = GB(p1, 0, 3); richk@6804: const uint32 airport_type_for_flat_check = (GB(p2, 8, 8) << 3) | layout_set; truelight@0: Darkvater@1784: /* Check if a valid, buildable airport was chosen for construction */ rubidium@6871: //if (p1 > lengthof(_airport_sections) || !HasBit(GetValidAirports(), p1)) return CMD_ERROR; Darkvater@1784: richk@10242: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) { truelight@0: return CMD_ERROR; richk@10242: } richk@10242: richk@10242: Town *t = ClosestTownFromTile(tile, UINT_MAX); richk@6719: Station *st = NULL; richk@6719: richk@6722: /* Check if the given station class is valid */ richk@6722: if (GB(p2, 8, 8) >= FSMPORTS_CLASS_MAX) return CMD_ERROR; richk@6722: richk@10731: /* Check if we can allocate a custom stationspec to this station */ richk@10731: const FSMportsSpec *fsmportsspec = GetCustomFSMportsSpec((FSMportsClassID)GB(p2, 8, 8), GB(p2, 16, 8)); richk@10731: if (fsmportsspec == NULL) return CMD_ERROR; richk@10731: richk@10731: /* Go get the final noise level, that is base noise minus factor from distance to town center */ richk@10731: uint newnoise_level = GetAirportNoiseLevelForTown(fsmportsspec->portFSM, t->xy, tile); richk@10731: richk@10731: /* Check if local auth would allow a new airport */ richk@10731: bool authority_refused; richk@10731: richk@10991: if (_settings_game.economy.station_noise_level) { richk@10731: /* do not allow to build a new airport if this raise the town noise over the maximum allowed by town */ richk@10731: authority_refused = (t->noise_reached + newnoise_level) > t->MaxTownNoise(); richk@10731: } else { richk@10731: uint num = 0; richk@10731: const Station *st; richk@10731: FOR_ALL_STATIONS(st) { richk@10731: if (st->town == t && st->facilities & FACIL_AIRPORT && st->Airport() != AirportFTAClass::oil_rig) num++; richk@10731: } richk@10731: authority_refused = (num >= 2); richk@10724: } richk@10731: richk@10731: if (authority_refused) { richk@10724: SetDParam(0, t->index); richk@10724: return_cmd_error(STR_2035_LOCAL_AUTHORITY_REFUSES); richk@10724: } rubidium@6802: rubidium@6802: /* Perform NewStation checks */ rubidium@6802: byte w = fsmportsspec->size_x[layout_set]; rubidium@6802: byte h = fsmportsspec->size_y[layout_set]; rubidium@6802: richk@10991: if (w > _settings_game.station.station_spread || h > _settings_game.station.station_spread) { richk@10724: _error_message = STR_306C_STATION_TOO_SPREAD_OUT; richk@10724: return CMD_ERROR; richk@10724: } richk@10724: rubidium@6802: /* Check if the station is buildable */ rubidium@6871: if (HasBit(fsmportsspec->callbackmask, CBM_STATION_AVAIL) && GetFSMportsCallback(CBID_STATION_AVAILABILITY, 0, 0, fsmportsspec, NULL, INVALID_TILE) == 0) { rubidium@6802: return CMD_ERROR; richk@6722: } richk@6722: richk@10991: if (!_settings_game.station.adjacent_stations || !HasBit(p1, 24)) { richk@6719: st = GetStationAround(tile, w, h, INVALID_STATION); richk@6719: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; richk@10724: } else { richk@10724: st = NULL; richk@6719: } truelight@0: truelight@0: /* Find a station close to us */ tron@6378: if (st == NULL) st = GetClosestStationFromTile(tile); truelight@0: truelight@0: if (st != NULL) { richk@10242: if (st->owner != _current_player) { truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); richk@10242: } truelight@193: KUDr@5927: if (!st->rect.BeforeAddRect(tile, w, h, StationRect::ADD_TEST)) return CMD_ERROR; truelight@0: richk@10242: if (st->airport_tile != 0) { truelight@0: return_cmd_error(STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT); richk@10242: } truelight@0: } else { truelight@0: airport_upgrade = false; truelight@0: KUDr@5916: /* allocate and initialize new station */ richk@10724: if (!Station::CanAllocateItem()) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING); richk@10724: richk@10724: if (flags & DC_EXEC) { richk@10724: st = new Station(tile); richk@10724: richk@10724: st->town = t; richk@10724: st->string_id = GenerateStationName(st, tile, !(fsmportsspec->flags & AirportFTAClass::AIRPLANES) ? STATIONNAMING_HELIPORT : STATIONNAMING_AIRPORT); richk@10724: richk@10724: if (IsValidPlayer(_current_player)) { richk@10724: SetBit(st->town->have_ratings, _current_player); richk@10724: } richk@10724: st->sign.width_1 = 0; KUDr@5916: } truelight@0: truelight@0: } truelight@0: richk@6770: flags |= DC_NO_WATER; rubidium@6802: richk@6770: /* do FSM layout mask checks richk@6770: * These allow placement on water, and irregular heights within layout. richk@6770: * If building on water is allowed for one tile, permit it for whole airport. This is not the problem it appears. richk@6770: * This routine does all the other tile validations, so although CMD_NO_WATER is cleared, it has already verified that richk@6770: * the tiles have the correct land/water statuses. richk@6770: */ richk@6770: if (fsmportsspec->layout_mask != NULL) { rubidium@6802: const byte *mask_ptr = fsmportsspec->layout_mask[layout_set] + 2; // width*length flagbytes. Also skip orientation and "filler" byte richk@6770: BEGIN_TILE_LOOP(tile_cur, w, h, tile) { rubidium@6801: byte mask = *mask_ptr++; richk@6770: if (mask & 0x80) { richk@6770: /* do water tests */ rubidium@6871: if (IsWaterTile(tile_cur)) { rubidium@6802: flags &= ~DC_NO_WATER; richk@10724: if (flags & DC_EXEC) st->FSMport_flood_protected = true; richk@6770: } else { richk@6770: _error_message = STR_304B_SITE_UNSUITABLE; richk@6770: return CMD_ERROR; richk@6770: } rubidium@6871: } else if (IsWaterTile(tile_cur)) { rubidium@6802: /* tile should be land, but is water */ rubidium@6802: _error_message = STR_3807_CAN_T_BUILD_ON_WATER; rubidium@6802: return CMD_ERROR; richk@6770: } richk@6770: //TODO: add height comparison & validation code here richk@6770: } END_TILE_LOOP(tile_cur, w, h, tile) richk@6770: } richk@6770: richk@6804: CommandCost cost = CheckFlatLandBelow(tile, w, h, flags, airport_type_for_flat_check, NULL, fsmportsspec, true); rubidium@6802: if (CmdFailed(cost)) return cost; richk@6722: richk@6720: cost.AddCost(_price.build_airport * w * h); truelight@0: truelight@0: if (flags & DC_EXEC) { richk@10731: /* Always add the noise, so there will be no need to recalculate when option toggles */ richk@10731: st->town->noise_reached += newnoise_level; richk@10731: truelight@0: st->airport_tile = tile; KUDr@5972: st->AddFacility(FACIL_AIRPORT, tile); richk@6739: st->airport_flags.ResetAll(); richk@6745: st->FSMport_layout_set = layout_set; truelight@193: KUDr@5927: st->rect.BeforeAddRect(tile, w, h, StationRect::ADD_TRY); KUDr@5583: tron@2639: /* if airport was demolished while planes were en-route to it, the tron@2639: * positions can no longer be the same (v->u.air.pos), since different tron@2639: * airports have different indexes. So update all planes en-route to this tron@2639: * airport. Only update if tron@2639: * 1. airport is upgraded tron@2639: * 2. airport is added to existing station (unfortunately unavoideable) tron@2639: */ richk@10346: if (airport_upgrade) { richk@10346: UpdateAirplanesOnNewStation(st); richk@10346: DeallocateSpecFromFSMports(st, 1); richk@10346: } truelight@193: rubidium@6802: int fsmportspecindex = AllocateFSMportsSpecToStation(fsmportsspec, st, flags & DC_EXEC); rubidium@6802: if (fsmportspecindex == -1) return CMD_ERROR; rubidium@6802: rubidium@6802: const byte *layout_ptr = fsmportsspec->layouts[layout_set]; rubidium@6802: st->FSMport_orientation = *layout_ptr++; //orientation rubidium@6802: layout_ptr++; //skip minipic ID rubidium@6802: richk@6867: byte partial_snow_desert; richk@6867: rubidium@6802: BEGIN_TILE_LOOP(tile_cur, w, h, tile) { richk@6867: partial_snow_desert = 0x00; rubidium@6802: byte layout = *layout_ptr++; richk@10991: if ((GetTropicZone(tile_cur) == TROPICZONE_DESERT) || ((GetTileZ(tile_cur) >= GetSnowLine() && _settings_game.game_creation.landscape == LT_ARCTIC))) richk@6867: partial_snow_desert = 1; rubidium@6802: MakeAirport(tile_cur, st->owner, st->index, layout); richk@6804: if (layout != 255) richk@6867: { richk@6867: if (partial_snow_desert != 0) richk@6867: SetTropicZone(tile_cur, TROPICZONE_DESERT); richk@6804: SetCustomFSMportsSpecIndex(tile_cur, fsmportspecindex); //set top bit of m6 to indicate an fsmportsspec richk@6867: } rubidium@6802: } END_TILE_LOOP(tile_cur, w, h, tile) truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 0); rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_PLANES); richk@10731: richk@10991: if (_settings_game.economy.station_noise_level) { richk@10731: InvalidateWindow(WC_TOWN_VIEW, st->town->index); richk@10731: } truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: richk@6720: static CommandCost RemoveAirport(Station *st, uint32 flags) truelight@0: { richk@10242: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) { richk@10242: return CMD_ERROR; richk@10242: } truelight@0: tron@6395: TileIndex tile = st->airport_tile; rubidium@6802: byte layout_set = st->FSMport_layout_set; rubidium@6802: rubidium@6802: const FSMportsSpec *fsmportsspec = st->fsmportsspeclist[GetCustomStationSpecIndex(tile)].spec; rubidium@6802: int w = fsmportsspec->size_x[layout_set]; rubidium@6802: int h = fsmportsspec->size_y[layout_set]; tron@6395: rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION, w * h * _price.remove_airport); truelight@0: richk@10242: const Vehicle *v; richk@6719: FOR_ALL_VEHICLES(v) { richk@6719: if (!(v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) continue; richk@6719: richk@6719: if (v->u.air.targetairport == st->index && v->u.air.state != FLYING) return CMD_ERROR; richk@6719: } richk@6719: rubidium@6802: const byte *layout_ptr = fsmportsspec->layouts[layout_set] + 2; // skip orientation and minipic ID rubidium@6801: const byte *mask_ptr; richk@6729: rubidium@6802: if (fsmportsspec->layout_mask != NULL) { rubidium@6802: mask_ptr = fsmportsspec->layout_mask[layout_set] + 2; // width*length flagbytes. Also skip plus orientation byte and 'filler' byte richk@6729: } else { rubidium@6799: mask_ptr = NULL; richk@6729: } richk@6729: rubidium@6802: BEGIN_TILE_LOOP(tile_cur, w, h, tile) rubidium@6802: byte layout = *layout_ptr++; rubidium@6802: if ((byte)layout != (byte)255) { rubidium@6870: if (!EnsureNoVehicleOnGround(tile_cur)) return CMD_ERROR; rubidium@6802: rubidium@6802: if (flags & DC_EXEC) { rubidium@6802: DeleteAnimatedTile(tile_cur); rubidium@6802: DoClearSquare(tile_cur); rubidium@6802: rubidium@6802: /* It was a water tile */ rubidium@6802: if (mask_ptr != NULL && *mask_ptr & 0x80) MakeWater(tile_cur); richk@6718: } rubidium@6802: } rubidium@6802: if (mask_ptr != NULL) mask_ptr++; rubidium@6802: rubidium@6802: END_TILE_LOOP(tile_cur, w,h,tile) truelight@0: truelight@0: if (flags & DC_EXEC) { richk@6856: for (uint i = 0; i < fsmportsspec->portFSM->num_depots; ++i) { rubidium@6802: DeleteWindowById( richk@6863: WC_VEHICLE_DEPOT, tile + ToTileIndexDiff(fsmportsspec->portFSM->depots[i].tile_pos) rubidium@6802: ); tron@3033: } darkvater@755: richk@10731: /* Go get the final noise level, that is base noise minus factor from distance to town center. richk@10731: * And as for construction, always remove it, even if the patch is not set, in order to avoid the richk@10731: * need of recalculation */ richk@10731: st->town->noise_reached -= GetAirportNoiseLevelForTown(st->Airport(), st->town->xy, tile); richk@10731: KUDr@5927: st->rect.AfterRemoveRect(st, tile, w, h); KUDr@5583: truelight@0: st->airport_tile = 0; truelight@0: st->facilities &= ~FACIL_AIRPORT; truelight@0: rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_PLANES); richk@10731: richk@10991: if (_settings_game.economy.station_noise_level) { richk@10731: InvalidateWindow(WC_TOWN_VIEW, st->town->index); richk@10731: } richk@10731: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: Darkvater@1784: /** Build a buoy. tron@3491: * @param tile tile where to place the bouy richk@6719: * @param flags operation to perform Darkvater@1784: * @param p1 unused Darkvater@1784: * @param p2 unused truelight@0: */ richk@6720: CommandCost CmdBuildBuoy(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@6871: if (!IsWaterTile(tile) || tile == 0) return_cmd_error(STR_304B_SITE_UNSUITABLE); maedhros@6632: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); truelight@0: rubidium@6877: if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return_cmd_error(STR_304B_SITE_UNSUITABLE); rubidium@6877: KUDr@5916: /* allocate and initialize new station */ richk@10724: if (!Station::CanAllocateItem()) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING); truelight@0: truelight@0: if (flags & DC_EXEC) { richk@10724: Station *st = new Station(tile); richk@10724: richk@10724: st->town = ClosestTownFromTile(tile, UINT_MAX); richk@10724: st->string_id = GenerateStationName(st, tile, STATIONNAMING_BUOY); richk@10724: richk@10724: if (IsValidPlayer(_current_player)) { richk@10724: SetBit(st->town->have_ratings, _current_player); richk@10724: } richk@10724: st->sign.width_1 = 0; tron@3055: st->dock_tile = tile; truelight@0: st->facilities |= FACIL_DOCK; matthijs@1751: /* Buoys are marked in the Station struct by this flag. Yes, it is this matthijs@1751: * braindead.. */ truelight@0: st->had_vehicle_of_type |= HVOT_BUOY; truelight@0: st->owner = OWNER_NONE; truelight@193: truelight@71: st->build_date = _date; truelight@0: rubidium@6877: MakeBuoy(tile, st->index, GetWaterClass(tile)); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 0); rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS); truelight@0: } truelight@0: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_dock); truelight@0: } truelight@0: rubidium@6872: /** rubidium@6872: * Tests whether the player's vehicles have this station in orders rubidium@6872: * When player == INVALID_PLAYER, then check all vehicles rubidium@6872: * @param station station ID rubidium@6872: * @param player player ID, INVALID_PLAYER to disable the check rubidium@6872: */ rubidium@6872: bool HasStationInUse(StationID station, PlayerID player) dominik@1078: { dominik@1078: const Vehicle *v; dominik@1078: FOR_ALL_VEHICLES(v) { rubidium@6872: if (player == INVALID_PLAYER || v->owner == player) { dominik@1078: const Order *order; dominik@1078: FOR_VEHICLE_ORDERS(v, order) { richk@10184: if (order->IsType(OT_GOTO_STATION) && order->GetDestination() == station) { dominik@1078: return true; dominik@1078: } dominik@1078: } dominik@1078: } dominik@1078: } dominik@1078: return false; dominik@1078: } dominik@1078: richk@6720: static CommandCost RemoveBuoy(Station *st, uint32 flags) truelight@0: { Darkvater@4850: /* XXX: strange stuff */ richk@10242: if (!IsValidPlayer(_current_player)) return_cmd_error(INVALID_STRING_ID); truelight@0: tron@6395: TileIndex tile = st->dock_tile; truelight@0: rubidium@6872: if (HasStationInUse(st->index, INVALID_PLAYER)) return_cmd_error(STR_BUOY_IS_IN_USE); richk@6878: /* remove the buoy if there is a ship on tile when company goes bankrupt... */ richk@6878: if (!(flags & DC_BANKRUPT) && !EnsureNoVehicleOnGround(tile)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: st->dock_tile = 0; matthijs@1751: /* Buoys are marked in the Station struct by this flag. Yes, it is this matthijs@1751: * braindead.. */ truelight@0: st->facilities &= ~FACIL_DOCK; truelight@0: st->had_vehicle_of_type &= ~HVOT_BUOY; truelight@0: rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS); rubidium@6872: rubidium@6204: /* We have to set the water tile's state to the same state as before the rubidium@6204: * buoy was placed. Otherwise one could plant a buoy on a canal edge, rubidium@6204: * remove it and flood the land (if the canal edge is at level 0) */ rubidium@6877: MakeWaterKeepingClass(tile, GetTileOwner(tile)); tron@3111: MarkTileDirtyByTile(tile); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_truck_station); truelight@0: } truelight@0: tron@909: static const TileIndexDiffC _dock_tileoffs_chkaround[] = { tron@909: {-1, 0}, tron@909: { 0, 0}, tron@909: { 0, 0}, tron@909: { 0, -1} truelight@0: }; rubidium@4344: static const byte _dock_w_chk[4] = { 2, 1, 2, 1 }; rubidium@4344: static const byte _dock_h_chk[4] = { 1, 2, 1, 2 }; truelight@0: Darkvater@1784: /** Build a dock/haven. tron@3491: * @param tile tile where dock will be built richk@6719: * @param flags operation to perform richk@6719: * @param p1 (bit 0) - allow docks directly adjacent to other docks. Darkvater@1784: * @param p2 unused Darkvater@1784: */ richk@6720: CommandCost CmdBuildDock(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { rubidium@6877: DiagDirection direction = GetInclinedSlopeDirection(GetTileSlope(tile, NULL)); rubidium@6877: if (direction == INVALID_DIAGDIR) return_cmd_error(STR_304B_SITE_UNSUITABLE); rubidium@6877: direction = ReverseDiagDir(direction); rubidium@6877: rubidium@6877: /* Docks cannot be placed on rapids */ rubidium@6877: if (IsWaterTile(tile)) return_cmd_error(STR_304B_SITE_UNSUITABLE); tron@3055: peter1138@4619: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) return CMD_ERROR; peter1138@4619: maedhros@6632: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); maedhros@6632: richk@10242: if (CmdFailed(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR))) return CMD_ERROR; truelight@0: tron@6395: TileIndex tile_cur = tile + TileOffsByDiagDir(direction); truelight@0: tron@3636: if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) { tron@3055: return_cmd_error(STR_304B_SITE_UNSUITABLE); tron@3055: } truelight@0: maedhros@6632: if (MayHaveBridgeAbove(tile_cur) && IsBridgeAbove(tile_cur)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); maedhros@6632: rubidium@6877: /* Get the water class of the water tile before it is cleared.*/ rubidium@6877: WaterClass wc = GetWaterClass(tile_cur); rubidium@6877: richk@10242: if (CmdFailed(DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR))) return CMD_ERROR; truelight@0: Darkvater@4559: tile_cur += TileOffsByDiagDir(direction); tron@3636: if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) { truelight@0: return_cmd_error(STR_304B_SITE_UNSUITABLE); tron@3055: } truelight@193: truelight@0: /* middle */ richk@6719: Station *st = NULL; richk@6719: richk@10991: if (!_settings_game.station.adjacent_stations || !HasBit(p1, 0)) { richk@6719: st = GetStationAround( richk@6719: tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]), richk@6719: _dock_w_chk[direction], _dock_h_chk[direction], INVALID_STATION); richk@6719: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; richk@6719: } truelight@193: truelight@0: /* Find a station close to us */ tron@6378: if (st == NULL) st = GetClosestStationFromTile(tile); truelight@0: truelight@0: if (st != NULL) { richk@10242: if (st->owner != _current_player) { truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); richk@10242: } truelight@193: KUDr@5927: if (!st->rect.BeforeAddRect(tile, _dock_w_chk[direction], _dock_h_chk[direction], StationRect::ADD_TEST)) return CMD_ERROR; Darkvater@1784: Darkvater@1784: if (st->dock_tile != 0) return_cmd_error(STR_304C_TOO_CLOSE_TO_ANOTHER_DOCK); truelight@0: } else { KUDr@5916: /* allocate and initialize new station */ richk@10724: /* allocate and initialize new station */ richk@10724: if (!Station::CanAllocateItem()) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING); richk@10724: richk@10724: if (flags & DC_EXEC) { richk@10724: st = new Station(tile); richk@10724: richk@10724: st->town = ClosestTownFromTile(tile, UINT_MAX); richk@10724: st->string_id = GenerateStationName(st, tile, STATIONNAMING_DOCK); richk@10724: richk@10724: if (IsValidPlayer(_current_player)) { richk@10724: SetBit(st->town->have_ratings, _current_player); richk@10724: } KUDr@5916: } truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: st->dock_tile = tile; KUDr@5972: st->AddFacility(FACIL_DOCK, tile); truelight@0: KUDr@5927: st->rect.BeforeAddRect(tile, _dock_w_chk[direction], _dock_h_chk[direction], StationRect::ADD_TRY); KUDr@5583: rubidium@6877: MakeDock(tile, st->owner, st->index, direction, wc); truelight@193: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 0); rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS); truelight@0: } richk@10242: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_dock); truelight@0: } truelight@0: richk@6720: static CommandCost RemoveDock(Station *st, uint32 flags) truelight@0: { tron@2639: if (!CheckOwnership(st->owner)) return CMD_ERROR; truelight@0: tron@6395: TileIndex tile1 = st->dock_tile; tron@6395: TileIndex tile2 = tile1 + TileOffsByDiagDir(GetDockDirection(tile1)); truelight@0: rubidium@6871: if (!EnsureNoVehicleOnGround(tile1)) return CMD_ERROR; rubidium@6871: if (!EnsureNoVehicleOnGround(tile2)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: DoClearSquare(tile1); rubidium@6877: MakeWaterKeepingClass(tile2, st->owner); KUDr@5583: KUDr@5927: st->rect.AfterRemoveTile(st, tile1); KUDr@5927: st->rect.AfterRemoveTile(st, tile2); KUDr@5583: tron@3111: MarkTileDirtyByTile(tile2); truelight@0: truelight@0: st->dock_tile = 0; truelight@0: st->facilities &= ~FACIL_DOCK; truelight@0: rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS); truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_dock); truelight@0: } truelight@0: truelight@0: #include "table/station_land.h" truelight@0: rubidium@6730: const DrawTileSprites *GetStationTileLayout(StationType st, byte gfx) peter1138@3763: { rubidium@6730: return &_station_display_datas[st][gfx]; peter1138@3763: } truelight@0: truelight@0: static void DrawTile_Station(TileInfo *ti) truelight@0: { tron@449: const DrawTileSprites *t = NULL; richk@6719: RoadTypes roadtypes; rubidium@6872: int32 total_offset; rubidium@6872: int32 custom_ground_offset; rubidium@6872: richk@6719: if (IsRailwayStation(ti->tile)) { rubidium@6872: const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile)); richk@6719: roadtypes = ROADTYPES_NONE; rubidium@6872: total_offset = rti->total_offset; rubidium@6872: custom_ground_offset = rti->custom_ground_offset; richk@6719: } else { rubidium@6877: roadtypes = IsRoadStop(ti->tile) ? GetRoadTypes(ti->tile) : ROADTYPES_NONE; rubidium@6872: total_offset = 0; rubidium@6872: custom_ground_offset = 0; richk@6719: } tron@449: uint32 relocation = 0; peter1138@3775: const Station *st = NULL; peter1138@3775: const StationSpec *statspec = NULL; richk@6722: const FSMportsSpec *fsmportspec = NULL; richk@6722: bool FSMport = false; tron@4227: PlayerID owner = GetTileOwner(ti->tile); tron@6395: peter1138@5919: SpriteID palette; Darkvater@4850: if (IsValidPlayer(owner)) { tron@4227: palette = PLAYER_SPRITE_COLOR(owner); tron@4227: } else { rubidium@6871: /* Some stations are not owner by a player, namely oil rigs */ tron@4227: palette = PALETTE_TO_GREY; truelight@0: } truelight@0: rubidium@6871: /* don't show foundation for docks */ tron@3636: if (ti->tileh != SLOPE_FLAT && !IsDock(ti->tile)) richk@6743: DrawFoundation(ti, FOUNDATION_LEVELED); truelight@0: peter1138@3568: if (IsCustomStationSpecIndex(ti->tile)) { rubidium@6871: /* look for customization */ peter1138@3775: st = GetStationByTile(ti->tile); richk@6722: int specindex = GetCustomStationSpecIndex(ti->tile); richk@6722: FSMport = IsCustomFSMportsSpecIndex(ti->tile); richk@6722: if (FSMport) { richk@6722: fsmportspec = st->fsmportsspeclist[specindex].spec; richk@6722: } else { richk@6722: statspec = st->speclist[specindex].spec; richk@6722: } tron@449: tron@449: if (statspec != NULL) { peter1138@3576: uint tile = GetStationGfx(ti->tile); tron@449: peter1138@3751: relocation = GetCustomStationRelocation(statspec, st, ti->tile); peter1138@3576: rubidium@6871: if (HasBit(statspec->callbackmask, CBM_STATION_SPRITE_LAYOUT)) { peter1138@3754: uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0, 0, statspec, st, ti->tile); peter1138@4886: if (callback != CALLBACK_FAILED) tile = (callback & ~1) + GetRailStationAxis(ti->tile); peter1138@3754: } peter1138@3754: peter1138@3576: /* Ensure the chosen tile layout is valid for this custom station */ peter1138@3754: if (statspec->renderdata != NULL) { rubidium@5838: t = &statspec->renderdata[tile < statspec->tiles ? tile : (uint)GetRailStationAxis(ti->tile)]; peter1138@3754: } tron@449: } richk@6722: if (fsmportspec != NULL) { richk@6722: uint tile = GetStationGfx(ti->tile); richk@6722: richk@6722: relocation = GetCustomFSMportsRelocation(fsmportspec, st, ti->tile); richk@6722: richk@6722: //if (HASBIT(fsmportspec->callbackmask, CBM_CUSTOM_LAYOUT)) { richk@6722: // uint16 callback = GetFSMportsCallback(CBID_FSMPORTS_SPRITE_LAYOUT, 0, 0, fsmportspec, st, ti->tile); richk@6722: // if (callback != CALLBACK_FAILED) tile = (callback & ~1) + GetRailStationAxis(ti->tile); richk@6722: //} richk@6722: richk@6722: /* Ensure the chosen tile layout is valid for this custom station */ richk@6722: if (fsmportspec->renderdata != NULL) { richk@6773: t = &fsmportspec->renderdata[tile < fsmportspec->tiles ? tile : 0]; richk@6722: } richk@6722: } tron@449: } tron@449: rubidium@6730: if (t == NULL || t->seq == NULL) t = &_station_display_datas[GetStationType(ti->tile)][GetStationGfx(ti->tile)]; darkvater@384: richk@6878: richk@6878: if (IsBuoy(ti->tile) || IsDock(ti->tile)) { richk@6878: if (ti->tileh == SLOPE_FLAT) { richk@6878: DrawWaterClassGround(ti); richk@6878: } else { richk@6878: assert(IsDock(ti->tile)); richk@6878: TileIndex water_tile = ti->tile + TileOffsByDiagDir(GetDockDirection(ti->tile)); richk@6878: WaterClass wc = GetWaterClass(water_tile); richk@6878: if (wc == WATER_CLASS_SEA) { richk@6878: DrawShoreTile(ti->tileh); richk@6878: } else { richk@6878: DrawClearLandTile(ti, 3); richk@6878: } richk@6878: } richk@6878: } richk@6878: SpriteID image = t->ground.sprite; rubidium@6871: if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) { richk@6722: image += (FSMport)? GetCustomFSMportsGroundRelocation(fsmportspec, st, ti->tile) : GetCustomStationGroundRelocation(statspec, st, ti->tile); rubidium@6872: image += custom_ground_offset; peter1138@3775: } else { richk@6878: SpriteID image = t->ground.sprite; richk@6878: if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) { richk@6878: image += GetCustomStationGroundRelocation(statspec, st, ti->tile); richk@6878: image += custom_ground_offset; richk@6878: } else { richk@6878: image += total_offset; richk@6878: } richk@6878: DrawGroundSprite(image, HasBit(image, PALETTE_MODIFIER_COLOR) ? palette : PAL_NONE); peter1138@3775: } tron@449: richk@6867: // if ground tile 0x0F8D, but tile is desert/snow, then choose desert/snow graphic richk@6867: if ((image == 0x0F8D) && (GetTropicZone(ti->tile) == TROPICZONE_DESERT)) richk@6867: image = 0x11C6; // snow tile (arctic) or desert tile (tropical) richk@6867: rubidium@6871: /* station_land array has been increased from 82 elements to 114 rubidium@6871: * but this is something else. If AI builds station with 114 it looks all weird */ rubidium@6871: DrawGroundSprite(image, HasBit(image, PALETTE_MODIFIER_COLOR) ? palette : PAL_NONE); truelight@0: richk@10724: if (IsRailwayStation(ti->tile) && HasCatenaryDrawn(GetRailType(ti->tile)) && IsStationTileElectrifiable(ti->tile)) DrawCatenary(ti); celestar@3355: rubidium@6871: if (HasBit(roadtypes, ROADTYPE_TRAM)) { richk@6719: Axis axis = GetRoadStopDir(ti->tile) == DIAGDIR_NE ? AXIS_X : AXIS_Y; rubidium@6871: DrawGroundSprite((HasBit(roadtypes, ROADTYPE_ROAD) ? SPR_TRAMWAY_OVERLAY : SPR_TRAMWAY_TRAM) + (axis ^ 1), PAL_NONE); richk@6719: DrawTramCatenary(ti, axis == AXIS_X ? ROAD_X : ROAD_Y); richk@6719: } richk@6719: richk@10184: /* End now if buildings are invisible */ richk@10184: if (IsInvisibilitySet(TO_BUILDINGS)) return; richk@10184: tron@6395: const DrawTileSeqStruct *dtss; darkvater@384: foreach_draw_tile_seq(dtss, t->seq) { richk@6878: SpriteID image = dtss->image.sprite; rubidium@6871: if (relocation == 0 || HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) { rubidium@6872: image += total_offset; peter1138@3775: } else { peter1138@3775: image += relocation; peter1138@3775: } peter1138@3775: tron@6395: SpriteID pal; richk@10210: if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOR)) { richk@10210: if (dtss->image.pal > 0) { richk@10210: pal = dtss->image.pal; richk@10210: } else { richk@10210: pal = palette; richk@10210: } peter1138@5919: } else { richk@10210: pal = PAL_NONE; darkvater@384: } darkvater@384: truelight@0: if ((byte)dtss->delta_z != 0x80) { tron@4230: AddSortableSpriteToDraw( peter1138@5919: image, pal, tron@4230: ti->x + dtss->delta_x, ti->y + dtss->delta_y, tron@4230: dtss->size_x, dtss->size_y, richk@6743: dtss->size_z, ti->z + dtss->delta_z, rubidium@6872: !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(TO_BUILDINGS) tron@4230: ); truelight@0: } else { rubidium@6871: AddChildSpriteScreen(image, pal, dtss->delta_x, dtss->delta_y, IsTransparencySet(TO_BUILDINGS)); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@6730: void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image) truelight@0: { rubidium@6872: int32 total_offset = 0; tron@6395: SpriteID pal = PLAYER_SPRITE_COLOR(_local_player); rubidium@6730: const DrawTileSprites *t = &_station_display_datas[st][image]; tron@6395: rubidium@6872: if (railtype != INVALID_RAILTYPE) { rubidium@6872: const RailtypeInfo *rti = GetRailTypeInfo(railtype); rubidium@6872: total_offset = rti->total_offset; rubidium@6872: } rubidium@6872: richk@6878: SpriteID img = t->ground.sprite; rubidium@6872: DrawSprite(img + total_offset, HasBit(img, PALETTE_MODIFIER_COLOR) ? pal : PAL_NONE, x, y); truelight@0: richk@6719: if (roadtype == ROADTYPE_TRAM) { richk@6878: DrawSprite(SPR_TRAMWAY_TRAM + (t->ground.sprite == SPR_ROAD_PAVED_STRAIGHT_X ? 1 : 0), PAL_NONE, x, y); richk@6719: } richk@6719: tron@6395: const DrawTileSeqStruct *dtss; darkvater@384: foreach_draw_tile_seq(dtss, t->seq) { truelight@0: Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z); richk@6878: DrawSprite(dtss->image.sprite + total_offset, pal, x + pt.x, y + pt.y); truelight@0: } truelight@0: } truelight@0: tron@4231: static uint GetSlopeZ_Station(TileIndex tile, uint x, uint y) truelight@0: { tron@4231: return GetTileMaxZ(tile); truelight@0: } truelight@0: richk@6743: static Foundation GetFoundation_Station(TileIndex tile, Slope tileh) dominik@39: { richk@6743: return FlatteningFoundation(tileh); dominik@39: } dominik@39: tron@1977: static void GetAcceptedCargo_Station(TileIndex tile, AcceptedCargo ac) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: tron@1977: static void GetTileDesc_Station(TileIndex tile, TileDesc *td) truelight@0: { richk@10731: td->owner[0] = GetTileOwner(tile); richk@10731: if (IsDriveThroughStopTile(tile) && HasTileRoadType(tile, ROADTYPE_ROAD) && GetStopBuiltOnTownRoad(tile)) { richk@10731: /* Display a second owner */ richk@10731: td->owner_type[1] = STR_ROAD_OWNER; richk@10731: td->owner[1] = OWNER_TOWN; richk@10731: } tron@3315: td->build_date = GetStationByTile(tile)->build_date; tron@2049: tron@6395: StringID str; celestar@3334: switch (GetStationType(tile)) { celestar@3334: default: NOT_REACHED(); celestar@3334: case STATION_RAIL: str = STR_305E_RAILROAD_STATION; break; tron@3882: case STATION_AIRPORT: tron@3882: str = (IsHangar(tile) ? STR_305F_AIRCRAFT_HANGAR : STR_3060_AIRPORT); tron@3882: break; celestar@3334: case STATION_TRUCK: str = STR_3061_TRUCK_LOADING_AREA; break; celestar@3334: case STATION_BUS: str = STR_3062_BUS_STATION; break; celestar@3334: case STATION_OILRIG: str = STR_4807_OIL_RIG; break; celestar@3334: case STATION_DOCK: str = STR_3063_SHIP_DOCK; break; celestar@3334: case STATION_BUOY: str = STR_3069_BUOY; break; celestar@3334: } truelight@0: td->str = str; truelight@0: } truelight@0: truelight@0: richk@6878: static TrackStatus GetTileTrackStatus_Station(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) tron@1977: { richk@6878: TrackBits trackbits = TRACK_BIT_NONE; richk@6878: tron@1032: switch (mode) { tron@1032: case TRANSPORT_RAIL: tron@4077: if (IsRailwayStation(tile) && !IsStationTileBlocked(tile)) { richk@6878: trackbits = TrackToTrackBits(GetRailStationTrack(tile)); tron@1032: } tron@1032: break; tron@1032: tron@1032: case TRANSPORT_WATER: rubidium@6871: /* buoy is coded as a station, it is always on open water */ KUDr@6165: if (IsBuoy(tile)) { richk@6878: trackbits = TRACK_BIT_ALL; rubidium@6871: /* remove tracks that connect NE map edge */ richk@6878: if (TileX(tile) == 0) trackbits &= ~(TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT); rubidium@6871: /* remove tracks that connect NW map edge */ richk@6878: if (TileY(tile) == 0) trackbits &= ~(TRACK_BIT_Y | TRACK_BIT_LEFT | TRACK_BIT_UPPER); KUDr@6165: } tron@1032: break; tron@1032: KUDr@3900: case TRANSPORT_ROAD: richk@6878: if ((GetRoadTypes(tile) & sub_mode) != 0 && IsRoadStop(tile)) { richk@6878: DiagDirection dir = GetRoadStopDir(tile); richk@6878: Axis axis = DiagDirToAxis(dir); richk@6878: richk@6878: if (side != INVALID_DIAGDIR) { richk@6878: if (axis != DiagDirToAxis(side) || (IsStandardRoadStopTile(tile) && dir != side)) break; richk@6878: } richk@6878: richk@6878: trackbits = AxisToTrackBits(axis); tron@4077: } KUDr@3900: break; KUDr@3900: tron@1032: default: tron@1032: break; truelight@0: } tron@1032: richk@6878: return CombineTrackStatus(TrackBitsToTrackdirBits(trackbits), TRACKDIR_BIT_NONE); truelight@0: } truelight@0: tron@2660: tron@1977: static void TileLoop_Station(TileIndex tile) truelight@0: { rubidium@6871: /* FIXME -- GetTileTrackStatus_Station -> animated stationtiles rubidium@6871: * hardcoded.....not good */ rubidium@6730: switch (GetStationType(tile)) { rubidium@6730: case STATION_AIRPORT: richk@6773: //TODO: animated tiles from newgrf richk@6773: //switch (GetStationGfx(tile)) { richk@6773: // case GFX_RADAR_LARGE_FIRST: richk@6773: // case GFX_WINDSACK_FIRST : // for small airport richk@6773: // case GFX_RADAR_INTERNATIONAL_FIRST: richk@6773: // case GFX_RADAR_METROPOLITAN_FIRST: richk@6773: // case GFX_RADAR_DISTRICTWE_FIRST: // radar district W-E airport richk@6773: // case GFX_WINDSACK_INTERCON_FIRST : // for intercontinental airport richk@6773: // AddAnimatedTile(tile); richk@6773: // break; richk@6773: //} tron@2660: break; tron@2660: richk@6878: case STATION_DOCK: richk@6878: if (GetTileSlope(tile, NULL) != SLOPE_FLAT) break; // only handle water part richk@6878: /* FALL THROUGH */ rubidium@6730: case STATION_OILRIG: //(station part) rubidium@6730: case STATION_BUOY: tron@2660: TileLoop_Water(tile); tron@2660: break; tron@2660: tron@2660: default: break; tron@2660: } truelight@0: } truelight@0: truelight@0: tron@1977: static void AnimateTile_Station(TileIndex tile) truelight@0: { rubidium@6574: struct AnimData { tron@4467: StationGfx from; // first sprite tron@4467: StationGfx to; // last sprite tron@4467: byte delay; rubidium@6574: }; tron@4467: tron@4467: static const AnimData data[] = { tron@4467: { GFX_RADAR_LARGE_FIRST, GFX_RADAR_LARGE_LAST, 3 }, tron@4467: { GFX_WINDSACK_FIRST, GFX_WINDSACK_LAST, 1 }, tron@4467: { GFX_RADAR_INTERNATIONAL_FIRST, GFX_RADAR_INTERNATIONAL_LAST, 3 }, tron@4467: { GFX_RADAR_METROPOLITAN_FIRST, GFX_RADAR_METROPOLITAN_LAST, 3 }, tron@4467: { GFX_RADAR_DISTRICTWE_FIRST, GFX_RADAR_DISTRICTWE_LAST, 3 }, tron@4467: { GFX_WINDSACK_INTERCON_FIRST, GFX_WINDSACK_INTERCON_LAST, 1 } tron@4467: }; tron@4467: richk@10274: if (IsRailwayStation(tile)) { richk@10274: AnimateStationTile(tile); richk@10274: return; richk@10274: } richk@10274: belugas@3545: StationGfx gfx = GetStationGfx(tile); tron@6395: tron@6395: for (const AnimData *i = data; i != endof(data); i++) { tron@4467: if (i->from <= gfx && gfx <= i->to) { tron@4467: if ((_tick_counter & i->delay) == 0) { tron@4467: SetStationGfx(tile, gfx < i->to ? gfx + 1 : i->from); tron@4467: MarkTileDirtyByTile(tile); tron@4467: } tron@4467: break; belugas@3545: } truelight@0: } truelight@0: } truelight@0: tron@3033: tron@1977: static void ClickTile_Station(TileIndex tile) truelight@0: { celestar@3334: if (IsHangar(tile)) { richk@6863: //find the main tile for this depot richk@6863: ShowDepotWindow(GetKeyDepotTile(tile), VEH_AIRCRAFT); truelight@0: } else { tron@3315: ShowStationViewWindow(GetStationIndex(tile)); truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _enter_station_speedtable[12] = { truelight@0: 215, 195, 175, 155, 135, 115, 95, 75, 55, 35, 15, 0 truelight@0: }; truelight@0: rubidium@6872: static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y) truelight@0: { richk@10184: StationID station_id = GetStationIndex(tile); richk@10184: rubidium@6585: if (v->type == VEH_TRAIN) { richk@10210: if (!v->current_order.ShouldStopAtStation(v, station_id)) return VETSB_CONTINUE; celestar@3334: if (IsRailwayStation(tile) && IsFrontEngine(v) && Darkvater@4559: !IsCompatibleTrainStationTile(tile + TileOffsByDiagDir(DirToDiagDir(v->direction)), tile)) { richk@10184: DiagDirection dir = DirToDiagDir(v->direction); richk@10184: richk@10184: x &= 0xF; richk@10184: y &= 0xF; richk@10184: richk@10184: if (DiagDirToAxis(dir) != AXIS_X) Swap(x, y); richk@10184: if (y == TILE_SIZE / 2) { richk@10184: if (dir != DIAGDIR_SE && dir != DIAGDIR_SW) x = TILE_SIZE - 1 - x; richk@10184: if (x == 12) return VETSB_ENTERED_STATION | (VehicleEnterTileStatus)(station_id << VETS_STATION_ID_OFFSET); /* enter station */ richk@10184: if (x < 12) { richk@10184: uint16 spd; richk@10184: richk@10184: v->vehstatus |= VS_TRAIN_SLOWING; richk@10184: spd = _enter_station_speedtable[x]; richk@10184: if (spd < v->cur_speed) v->cur_speed = spd; truelight@0: } truelight@193: } truelight@0: } rubidium@6585: } else if (v->type == VEH_ROAD) { rubidium@6402: if (v->u.road.state < RVSB_IN_ROAD_STOP && !IsReversingRoadTrackdir((Trackdir)v->u.road.state) && v->u.road.frame == 0) { richk@6720: if (IsRoadStop(tile) && IsRoadVehFront(v)) { peter1138@2671: /* Attempt to allocate a parking bay in a road stop */ celestar@1217: RoadStop *rs = GetRoadStopByTile(tile, GetRoadStopType(tile)); peter1138@2671: rubidium@6338: if (IsDriveThroughStopTile(tile)) { richk@10210: if (!v->current_order.ShouldStopAtStation(v, station_id)) return VETSB_CONTINUE; richk@10210: rubidium@6338: /* Vehicles entering a drive-through stop from the 'normal' side use first bay (bay 0). */ rubidium@6338: byte side = ((DirToDiagDir(v->direction) == ReverseDiagDir(GetRoadStopDir(tile))) == (v->u.road.overtaking == 0)) ? 0 : 1; rubidium@6338: rubidium@6338: if (!rs->IsFreeBay(side)) return VETSB_CANNOT_ENTER; rubidium@6338: rubidium@6338: /* Check if the vehicle is stopping at this road stop */ richk@10184: if (GetRoadStopType(tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK) && richk@10184: v->current_order.GetDestination() == GetStationIndex(tile)) { rubidium@6871: SetBit(v->u.road.state, RVS_IS_STOPPING); rubidium@6338: rs->AllocateDriveThroughBay(side); rubidium@6338: } rubidium@6338: rubidium@6338: /* Indicate if vehicle is using second bay. */ rubidium@6871: if (side == 1) SetBit(v->u.road.state, RVS_USING_SECOND_BAY); rubidium@6338: /* Indicate a drive-through stop */ rubidium@6871: SetBit(v->u.road.state, RVS_IN_DT_ROAD_STOP); rubidium@6338: return VETSB_CONTINUE; rubidium@6338: } rubidium@6338: rubidium@6338: /* For normal (non drive-through) road stops */ rubidium@6868: /* Check if station is busy or if there are no free bays or whether it is a articulated vehicle. */ rubidium@6868: if (rs->IsEntranceBusy() || !rs->HasFreeBay() || RoadVehHasArticPart(v)) return VETSB_CANNOT_ENTER; truelight@0: rubidium@6871: SetBit(v->u.road.state, RVS_IN_ROAD_STOP); peter1138@2671: rubidium@6316: /* Allocate a bay and update the road state */ rubidium@6316: uint bay_nr = rs->AllocateBay(); rubidium@6326: SB(v->u.road.state, RVS_USING_SECOND_BAY, 1, bay_nr); rubidium@6316: rubidium@6316: /* Mark the station entrace as busy */ rubidium@6316: rs->SetEntranceBusy(true); truelight@0: } truelight@0: } truelight@0: } truelight@193: rubidium@6317: return VETSB_CONTINUE; truelight@0: } truelight@0: truelight@0: /* this function is called for one station each tick */ truelight@0: static void StationHandleBigTick(Station *st) truelight@0: { truelight@0: UpdateStationAcceptance(st, true); truelight@0: KUDr@5916: if (st->facilities == 0 && ++st->delete_ctr >= 8) delete st; celestar@1239: truelight@0: } truelight@0: richk@10242: static inline void byte_inc_sat(byte *p) richk@10242: { richk@10242: byte b = *p + 1; richk@10242: if (b != 0) *p = b; richk@10242: } truelight@0: truelight@0: static void UpdateStationRating(Station *st) truelight@0: { truelight@0: bool waiting_changed = false; truelight@0: truelight@0: byte_inc_sat(&st->time_since_load); truelight@0: byte_inc_sat(&st->time_since_unload); truelight@0: tron@6395: GoodsEntry *ge = st->goods; truelight@0: do { truelight@6587: /* Slowly increase the rating back to his original level in the case we truelight@6587: * didn't deliver cargo yet to this station. This happens when a bribe truelight@6587: * failed while you didn't moved that cargo yet to a station. */ rubidium@6871: if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP) && ge->rating < INITIAL_STATION_RATING) { truelight@6587: ge->rating++; rubidium@6868: } rubidium@6868: truelight@6587: /* Only change the rating if we are moving this cargo */ rubidium@6871: if (HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) { truelight@0: byte_inc_sat(&ge->days_since_pickup); truelight@0: tron@6395: int rating = 0; truelight@0: truelight@0: { richk@10724: int b = ge->last_speed - 85; richk@10724: if (b >= 0) truelight@0: rating += b >> 2; truelight@0: } truelight@0: truelight@0: { truelight@0: byte age = ge->last_age; truelight@0: (age >= 3) || truelight@0: (rating += 10, age >= 2) || truelight@0: (rating += 10, age >= 1) || truelight@0: (rating += 13, true); truelight@0: } truelight@0: rubidium@6871: if (IsValidPlayer(st->owner) && HasBit(st->town->statues, st->owner)) rating += 26; truelight@0: truelight@0: { truelight@0: byte days = ge->days_since_pickup; rubidium@6871: if (st->last_vehicle_type == VEH_SHIP) days >>= 2; truelight@0: (days > 21) || truelight@0: (rating += 25, days > 12) || truelight@0: (rating += 25, days > 6) || truelight@0: (rating += 45, days > 3) || truelight@0: (rating += 35, true); truelight@0: } truelight@193: richk@6720: uint waiting = ge->cargo.Count(); tron@6395: (rating -= 90, waiting > 1500) || tron@6395: (rating += 55, waiting > 1000) || tron@6395: (rating += 35, waiting > 600) || tron@6395: (rating += 10, waiting > 300) || tron@6395: (rating += 20, waiting > 100) || tron@6395: (rating += 10, true); truelight@0: truelight@0: { rubidium@5838: int or_ = ge->rating; // old rating truelight@0: rubidium@6871: /* only modify rating in steps of -2, -1, 0, 1 or 2 */ rubidium@6871: ge->rating = rating = or_ + Clamp(Clamp(rating, 0, 255) - or_, -2, 2); rubidium@6871: rubidium@6871: /* if rating is <= 64 and more than 200 items waiting, rubidium@6871: * remove some random amount of goods from the station */ truelight@0: if (rating <= 64 && waiting >= 200) { truelight@0: int dec = Random() & 0x1F; truelight@0: if (waiting < 400) dec &= 7; truelight@0: waiting -= dec + 1; truelight@0: waiting_changed = true; truelight@0: } truelight@0: rubidium@6871: /* if rating is <= 127 and there are any items waiting, maybe remove some goods. */ truelight@0: if (rating <= 127 && waiting != 0) { truelight@0: uint32 r = Random(); peter1138@5911: if (rating <= (int)GB(r, 0, 7)) { richk@6720: /* Need to have int, otherwise it will just overflow etc. */ richk@6720: waiting = max((int)waiting - (int)GB(r, 8, 2) - 1, 0); truelight@0: waiting_changed = true; truelight@0: } truelight@0: } truelight@0: richk@6720: /* At some point we really must cap the cargo. Previously this richk@6720: * was a strict 4095, but now we'll have a less strict, but richk@6720: * increasingly agressive truncation of the amount of cargo. */ richk@6720: static const uint WAITING_CARGO_THRESHOLD = 1 << 12; richk@6720: static const uint WAITING_CARGO_CUT_FACTOR = 1 << 6; richk@6720: static const uint MAX_WAITING_CARGO = 1 << 15; richk@6720: richk@6720: if (waiting > WAITING_CARGO_THRESHOLD) { richk@6720: uint difference = waiting - WAITING_CARGO_THRESHOLD; richk@6720: waiting -= (difference / WAITING_CARGO_CUT_FACTOR); richk@6720: richk@6720: waiting = min(waiting, MAX_WAITING_CARGO); richk@6720: waiting_changed = true; richk@6720: } richk@6720: richk@6720: if (waiting_changed) ge->cargo.Truncate(waiting); truelight@0: } truelight@0: } truelight@0: } while (++ge != endof(st->goods)); truelight@193: tron@6395: StationID index = st->index; tron@4077: if (waiting_changed) { rubidium@6872: InvalidateWindow(WC_STATION_VIEW, index); // update whole window tron@4077: } else { rubidium@6872: InvalidateWindowWidget(WC_STATION_VIEW, index, SVW_RATINGLIST); // update only ratings list tron@4077: } truelight@0: } truelight@0: truelight@0: /* called for every station each tick */ truelight@0: static void StationHandleSmallTick(Station *st) truelight@0: { tron@2639: if (st->facilities == 0) return; truelight@0: tron@6395: byte b = st->delete_ctr + 1; truelight@0: if (b >= 185) b = 0; truelight@0: st->delete_ctr = b; truelight@0: tron@2639: if (b == 0) UpdateStationRating(st); truelight@0: } truelight@0: rubidium@6573: void OnTick_Station() truelight@0: { tron@2639: if (_game_mode == GM_EDITOR) return; truelight@0: tron@6395: uint i = _station_tick_ctr; matthijs@5247: if (++_station_tick_ctr > GetMaxStationIndex()) _station_tick_ctr = 0; truelight@0: truelight@4352: if (IsValidStationID(i)) StationHandleBigTick(GetStation(i)); truelight@0: tron@6395: Station *st; richk@10274: FOR_ALL_STATIONS(st) { richk@10274: StationHandleSmallTick(st); richk@10274: richk@10274: /* Run 250 tick interval trigger for station animation. richk@10274: * Station index is included so that triggers are not all done richk@10274: * at the same time. */ richk@10274: if ((_tick_counter + st->index) % 250 == 0) { richk@10274: StationAnimationTrigger(st, st->xy, STAT_ANIM_250_TICKS); richk@10274: } richk@10274: } truelight@0: } truelight@0: rubidium@6573: void StationMonthlyLoop() truelight@0: { richk@10242: /* not used */ truelight@0: } truelight@0: truelight@0: tron@2498: void ModifyStationRatingAround(TileIndex tile, PlayerID owner, int amount, uint radius) truelight@0: { truelight@0: Station *st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: if (st->owner == owner && tron@1245: DistanceManhattan(tile, st->xy) <= radius) { peter1138@6676: for (CargoID i = 0; i < NUM_CARGO; i++) { richk@10242: GoodsEntry *ge = &st->goods[i]; tron@2549: rubidium@6868: if (ge->acceptance_pickup != 0) { rubidium@6871: ge->rating = Clamp(ge->rating + amount, 0, 255); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: peter1138@6676: static void UpdateStationWaiting(Station *st, CargoID type, uint amount) truelight@0: { richk@6720: st->goods[type].cargo.Append(new CargoPacket(st->index, amount)); rubidium@6871: SetBit(st->goods[type].acceptance_pickup, GoodsEntry::PICKUP); richk@6720: richk@10274: StationAnimationTrigger(st, st->xy, STAT_ANIM_NEW_CARGO, type); richk@10274: truelight@0: InvalidateWindow(WC_STATION_VIEW, st->index); richk@6720: st->MarkTilesDirty(true); richk@6720: } richk@6720: richk@6720: static bool IsUniqueStationName(const char *name) richk@6720: { richk@6720: const Station *st; richk@6720: char buf[512]; richk@6720: richk@6720: FOR_ALL_STATIONS(st) { richk@6720: SetDParam(0, st->index); richk@6720: GetString(buf, STR_STATION, lastof(buf)); richk@6720: if (strcmp(buf, name) == 0) return false; richk@6720: } richk@6720: richk@6720: return true; truelight@0: } truelight@0: Darkvater@1786: /** Rename a station tron@3491: * @param tile unused richk@6719: * @param flags operation to perform Darkvater@1786: * @param p1 station ID that is to be renamed Darkvater@1786: * @param p2 unused Darkvater@1786: */ richk@6720: CommandCost CmdRenameStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { richk@6720: if (!IsValidStationID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR; tron@6395: Station *st = GetStation(p1); tron@1774: truelight@4352: if (!CheckOwnership(st->owner)) return CMD_ERROR; tron@1774: richk@6720: if (!IsUniqueStationName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); richk@6720: truelight@0: if (flags & DC_EXEC) { rubidium@6872: free(st->name); rubidium@6872: st->name = strdup(_cmd_text); rubidium@6872: truelight@0: UpdateStationVirtCoord(st); richk@10731: InvalidateWindowData(WC_STATION_LIST, st->owner, 1); truelight@0: MarkWholeScreenDirty(); truelight@0: } truelight@0: richk@6720: return CommandCost(); truelight@0: } truelight@0: rubidium@6871: /** richk@10242: * Find all (non-buoy) stations around an industry tile richk@10242: * richk@10242: * @param tile: Center tile to search from richk@10242: * @param w: Width of the center richk@10242: * @param h: Height of the center richk@10242: * richk@10242: * @return: Set of found stations richk@10242: */ rubidium@6871: StationSet FindStationsAroundIndustryTile(TileIndex tile, int w, int h) truelight@0: { rubidium@6871: StationSet station_set; rubidium@6871: rubidium@6871: int w_prod; // width and height of the "producer" of the cargo tron@3000: int h_prod; Celestar@568: int max_rad; richk@10991: if (_settings_game.station.modified_catchment) { Celestar@568: w_prod = w; Celestar@568: h_prod = h; rubidium@6868: w += 2 * MAX_CATCHMENT; rubidium@6868: h += 2 * MAX_CATCHMENT; rubidium@6868: max_rad = MAX_CATCHMENT; Celestar@568: } else { tron@3000: w_prod = 0; tron@3000: h_prod = 0; tron@3000: w += 8; tron@3000: h += 8; richk@6878: max_rad = CA_UNMODIFIED; Celestar@568: } Celestar@568: tron@1981: BEGIN_TILE_LOOP(cur_tile, w, h, tile - TileDiffXY(max_rad, max_rad)) truelight@0: cur_tile = TILE_MASK(cur_tile); tron@3000: if (!IsTileType(cur_tile, MP_STATION)) continue; tron@3000: tron@6395: Station *st = GetStationByTile(cur_tile); tron@6395: rubidium@6871: if (st->IsBuoy()) continue; // bouys don't accept cargo rubidium@6871: rubidium@6871: richk@10991: if (_settings_game.station.modified_catchment) { rubidium@6871: /* min and max coordinates of the producer relative */ rubidium@6871: const int x_min_prod = max_rad + 1; rubidium@6871: const int x_max_prod = max_rad + w_prod; rubidium@6871: const int y_min_prod = max_rad + 1; rubidium@6871: const int y_max_prod = max_rad + h_prod; rubidium@6871: rubidium@6871: int rad = FindCatchmentRadius(st); rubidium@6871: rubidium@6871: int x_dist = min(w_cur - x_min_prod, x_max_prod - w_cur); rubidium@6871: if (w_cur < x_min_prod) { rubidium@6871: x_dist = x_min_prod - w_cur; rubidium@6871: } else if (w_cur > x_max_prod) { rubidium@6871: x_dist = w_cur - x_max_prod; truelight@0: } rubidium@6871: rubidium@6871: if (x_dist > rad) continue; rubidium@6871: rubidium@6871: int y_dist = min(h_cur - y_min_prod, y_max_prod - h_cur); rubidium@6871: if (h_cur < y_min_prod) { rubidium@6871: y_dist = y_min_prod - h_cur; rubidium@6871: } else if (h_cur > y_max_prod) { rubidium@6871: y_dist = h_cur - y_max_prod; rubidium@6871: } rubidium@6871: rubidium@6871: if (y_dist > rad) continue; truelight@0: } rubidium@6871: rubidium@6871: /* Insert the station in the set. This will fail if it has rubidium@6871: * already been added. rubidium@6871: */ rubidium@6871: station_set.insert(st); rubidium@6871: tron@1981: END_TILE_LOOP(cur_tile, w, h, tile - TileDiffXY(max_rad, max_rad)) truelight@0: rubidium@6871: return station_set; rubidium@6871: } rubidium@6871: rubidium@6871: uint MoveGoodsToStation(TileIndex tile, int w, int h, CargoID type, uint amount) rubidium@6871: { richk@10242: Station *st1 = NULL; // Station with best rating richk@10242: Station *st2 = NULL; // Second best station richk@10242: uint best_rating1 = 0; // rating of st1 richk@10242: uint best_rating2 = 0; // rating of st2 rubidium@6871: rubidium@6871: StationSet all_stations = FindStationsAroundIndustryTile(tile, w, h); rubidium@6871: for (StationSet::iterator st_iter = all_stations.begin(); st_iter != all_stations.end(); ++st_iter) { rubidium@6871: Station *st = *st_iter; rubidium@6871: rubidium@6871: /* Is the station reserved exclusively for somebody else? */ rubidium@6871: if (st->town->exclusive_counter > 0 && st->town->exclusivity != st->owner) continue; rubidium@6871: rubidium@6871: if (st->goods[type].rating == 0) continue; // Lowest possible rating, better not to give cargo anymore rubidium@6871: richk@10991: if (_settings_game.order.selectgoods && st->goods[type].last_speed == 0) continue; // Selectively servicing stations, and not this one rubidium@6871: rubidium@6871: if (IsCargoInClass(type, CC_PASSENGERS)) { rubidium@6871: if (st->facilities == FACIL_TRUCK_STOP) continue; // passengers are never served by just a truck stop rubidium@6871: } else { rubidium@6871: if (st->facilities == FACIL_BUS_STOP) continue; // non-passengers are never served by just a bus stop rubidium@6871: } rubidium@6871: rubidium@6871: /* This station can be used, add it to st1/st2 */ rubidium@6871: if (st1 == NULL || st->goods[type].rating >= best_rating1) { rubidium@6871: st2 = st1; best_rating2 = best_rating1; st1 = st; best_rating1 = st->goods[type].rating; rubidium@6871: } else if (st2 == NULL || st->goods[type].rating >= best_rating2) { rubidium@6871: st2 = st; best_rating2 = st->goods[type].rating; rubidium@6871: } rubidium@6871: } rubidium@6871: truelight@0: /* no stations around at all? */ rubidium@6871: if (st1 == NULL) return 0; rubidium@6871: rubidium@6871: if (st2 == NULL) { truelight@0: /* only one station around */ rubidium@6871: uint moved = amount * best_rating1 / 256 + 1; rubidium@6871: UpdateStationWaiting(st1, type, moved); truelight@0: return moved; truelight@0: } truelight@0: rubidium@6871: /* several stations around, the best two (highest rating) are in st1 and st2 */ truelight@0: assert(st1 != NULL); truelight@0: assert(st2 != NULL); rubidium@6871: assert(best_rating1 != 0 || best_rating2 != 0); truelight@0: truelight@0: /* the 2nd highest one gets a penalty */ truelight@0: best_rating2 >>= 1; truelight@0: truelight@0: /* amount given to station 1 */ rubidium@6871: uint t = (best_rating1 * (amount + 1)) / (best_rating1 + best_rating2); tron@6395: tron@6395: uint moved = 0; truelight@0: if (t != 0) { rubidium@6871: moved = t * best_rating1 / 256 + 1; truelight@0: amount -= t; truelight@193: UpdateStationWaiting(st1, type, moved); truelight@0: } truelight@0: truelight@0: if (amount != 0) { tron@3000: amount = amount * best_rating2 / 256 + 1; tron@3000: moved += amount; truelight@0: UpdateStationWaiting(st2, type, amount); truelight@0: } truelight@0: truelight@0: return moved; truelight@0: } truelight@0: tron@1977: void BuildOilRig(TileIndex tile) truelight@0: { richk@10724: Station *st = new Station(tile); Darkvater@2427: Darkvater@2427: if (st == NULL) { Darkvater@5568: DEBUG(misc, 0, "Can't allocate station for oilrig at 0x%X, reverting to oilrig only", tile); Darkvater@2427: return; truelight@0: } Darkvater@2427: richk@10724: st->town = ClosestTownFromTile(tile, UINT_MAX); Darkvater@2427: st->sign.width_1 = 0; Darkvater@2427: richk@10724: st->string_id = GenerateStationName(st, tile, STATIONNAMING_OILRIG); rubidium@6871: celestar@3334: MakeOilrig(tile, st->index); Darkvater@2427: Darkvater@2427: st->owner = OWNER_NONE; richk@6739: st->airport_flags.ResetAll(); richk@6793: richk@6793: //add assignment of fsmportspec, and paint tile of North corner as fsmport rubidium@6854: int fsmportspecindex = AllocateFSMportsSpecToStation(AirportFTAClass::oil_rig->spec, st, true); richk@6793: SetCustomFSMportsSpecIndex(tile, fsmportspecindex); richk@6793: Darkvater@2427: st->xy = tile; Darkvater@2427: st->bus_stops = NULL; Darkvater@2427: st->truck_stops = NULL; Darkvater@2427: st->airport_tile = tile; Darkvater@2427: st->dock_tile = tile; Darkvater@2427: st->train_tile = 0; Darkvater@2427: st->had_vehicle_of_type = 0; Darkvater@2427: st->time_since_load = 255; Darkvater@2427: st->time_since_unload = 255; Darkvater@2427: st->delete_ctr = 0; rubidium@6585: st->last_vehicle_type = VEH_INVALID; Darkvater@2427: st->facilities = FACIL_AIRPORT | FACIL_DOCK; Darkvater@2427: st->build_date = _date; Darkvater@2427: peter1138@6676: for (CargoID j = 0; j < NUM_CARGO; j++) { rubidium@6868: st->goods[j].acceptance_pickup = 0; richk@6720: st->goods[j].days_since_pickup = 255; truelight@6587: st->goods[j].rating = INITIAL_STATION_RATING; Darkvater@2427: st->goods[j].last_speed = 0; Darkvater@2427: st->goods[j].last_age = 255; Darkvater@2427: } Darkvater@2427: Darkvater@2427: UpdateStationVirtCoordDirty(st); Darkvater@2427: UpdateStationAcceptance(st, false); truelight@0: } truelight@0: tron@1977: void DeleteOilRig(TileIndex tile) truelight@0: { richk@10242: Station *st = GetStationByTile(tile); truelight@0: tron@6138: MakeWater(tile); truelight@0: truelight@0: st->dock_tile = 0; truelight@0: st->airport_tile = 0; truelight@0: st->facilities &= ~(FACIL_AIRPORT | FACIL_DOCK); richk@6739: st->airport_flags.ResetAll(); truelight@0: UpdateStationVirtCoordDirty(st); KUDr@5916: if (st->facilities == 0) delete st; truelight@0: } truelight@0: Darkvater@2436: static void ChangeTileOwner_Station(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { tron@1901: if (!IsTileOwner(tile, old_player)) return; truelight@0: Darkvater@4848: if (new_player != PLAYER_SPECTATOR) { richk@10242: Station *st = GetStationByTile(tile); tron@3315: tron@1902: SetTileOwner(tile, new_player); richk@6878: if (!IsBuoy(tile)) st->owner = new_player; // do not set st->owner for buoys richk@10731: InvalidateWindowClassesData(WC_STATION_LIST, 0); truelight@0: } else { richk@6878: if (IsDriveThroughStopTile(tile)) { richk@6878: /* Remove the drive-through road stop */ richk@10184: DoCommand(tile, 0, (GetStationType(tile) == STATION_TRUCK) ? ROADSTOP_TRUCK : ROADSTOP_BUS, DC_EXEC | DC_BANKRUPT, CMD_REMOVE_ROAD_STOP); richk@6878: assert(IsTileType(tile, MP_ROAD)); richk@6878: /* Change owner of tile and all roadtypes */ richk@6878: ChangeTileOwner(tile, old_player, new_player); rubidium@6442: } else { richk@6878: DoCommand(tile, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR); richk@6878: /* Set tile owner of water under (now removed) buoy and dock to OWNER_NONE. richk@6878: * Update owner of buoy if it was not removed (was in orders). richk@6878: * Do not update when owned by OWNER_WATER (sea and rivers). */ richk@6878: if ((IsTileType(tile, MP_WATER) || IsBuoyTile(tile)) && IsTileOwner(tile, old_player)) SetTileOwner(tile, OWNER_NONE); rubidium@6442: } truelight@0: } truelight@0: } truelight@0: rubidium@6442: /** rubidium@6442: * Check if a drive-through road stop tile can be cleared. rubidium@6442: * Road stops built on town-owned roads check the conditions rubidium@6442: * that would allow clearing of the original road. rubidium@6442: * @param tile road stop tile to check richk@10731: * @param flags command flags rubidium@6442: * @return true if the road can be cleared rubidium@6442: */ richk@10731: static bool CanRemoveRoadWithStop(TileIndex tile, uint32 flags) rubidium@6442: { rubidium@6442: /* The road can always be cleared if it was not a town-owned road */ rubidium@6442: if (!GetStopBuiltOnTownRoad(tile)) return true; rubidium@6442: richk@10731: return CheckAllowRemoveRoad(tile, GetAnyRoadBits(tile, ROADTYPE_ROAD), OWNER_TOWN, ROADTYPE_ROAD, flags); rubidium@6442: } rubidium@6442: richk@6720: static CommandCost ClearTile_Station(TileIndex tile, byte flags) tron@1977: { truelight@0: if (flags & DC_AUTO) { celestar@3334: switch (GetStationType(tile)) { celestar@3334: case STATION_RAIL: return_cmd_error(STR_300B_MUST_DEMOLISH_RAILROAD); celestar@3334: case STATION_AIRPORT: return_cmd_error(STR_300E_MUST_DEMOLISH_AIRPORT_FIRST); richk@6878: case STATION_TRUCK: return_cmd_error(HasTileRoadType(tile, ROADTYPE_TRAM) ? STR_MUST_DEMOLISH_CARGO_TRAM_STATION : STR_3047_MUST_DEMOLISH_TRUCK_STATION); richk@6878: case STATION_BUS: return_cmd_error(HasTileRoadType(tile, ROADTYPE_TRAM) ? STR_MUST_DEMOLISH_PASSENGER_TRAM_STATION : STR_3046_MUST_DEMOLISH_BUS_STATION); celestar@3334: case STATION_BUOY: return_cmd_error(STR_306A_BUOY_IN_THE_WAY); celestar@3334: case STATION_DOCK: return_cmd_error(STR_304D_MUST_DEMOLISH_DOCK_FIRST); celestar@3334: case STATION_OILRIG: celestar@3334: SetDParam(0, STR_4807_OIL_RIG); celestar@3334: return_cmd_error(STR_4800_IN_THE_WAY); celestar@3334: } truelight@0: } truelight@0: tron@6395: Station *st = GetStationByTile(tile); truelight@0: celestar@3334: switch (GetStationType(tile)) { celestar@3334: case STATION_RAIL: return RemoveRailroadStation(st, tile, flags); celestar@3334: case STATION_AIRPORT: return RemoveAirport(st, flags); celestar@3334: case STATION_TRUCK: richk@10731: if (IsDriveThroughStopTile(tile) && !CanRemoveRoadWithStop(tile, flags)) rubidium@6338: return_cmd_error(STR_3047_MUST_DEMOLISH_TRUCK_STATION); rubidium@6338: return RemoveRoadStop(st, flags, tile); rubidium@6338: case STATION_BUS: richk@10731: if (IsDriveThroughStopTile(tile) && !CanRemoveRoadWithStop(tile, flags)) rubidium@6338: return_cmd_error(STR_3046_MUST_DEMOLISH_BUS_STATION); rubidium@6338: return RemoveRoadStop(st, flags, tile); celestar@3334: case STATION_BUOY: return RemoveBuoy(st, flags); celestar@3334: case STATION_DOCK: return RemoveDock(st, flags); celestar@3334: default: break; celestar@3334: } truelight@0: truelight@0: return CMD_ERROR; truelight@0: } truelight@0: rubidium@6573: void InitializeStations() truelight@0: { truelight@1272: /* Clean the station pool and create 1 block in it */ richk@6743: _Station_pool.CleanPool(); richk@6743: _Station_pool.AddBlockToPool(); truelight@919: truelight@1284: /* Clean the roadstop pool and create 1 block in it */ richk@6743: _RoadStop_pool.CleanPool(); richk@6743: _RoadStop_pool.AddBlockToPool(); darkvater@243: truelight@0: _station_tick_ctr = 0; rubidium@6758: /* Check whether our &fsmblocks == &fsmblocks.blocks "hack" works */ rubidium@6758: FSMblockmap::AssertOnWrongBlockOffset(); truelight@0: } truelight@0: truelight@0: rubidium@6573: void AfterLoadStations() peter1138@3765: { tron@6395: /* Update the speclists of all stations to point to the currently loaded custom stations. */ peter1138@3765: Station *st; peter1138@3765: FOR_ALL_STATIONS(st) { tron@6395: for (uint i = 0; i < st->num_specs; i++) { peter1138@3765: if (st->speclist[i].grfid == 0) continue; peter1138@3765: peter1138@3765: st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx); peter1138@3765: } richk@6720: richk@6733: for (uint i = 0; i < st->num_fsmportsspecs; i++) { richk@6733: if (st->fsmportsspeclist[i].grfid == 0) continue; richk@6733: richk@6733: st->fsmportsspeclist[i].spec = GetCustomFSMportsSpecByGrf(st->fsmportsspeclist[i].grfid, st->fsmportsspeclist[i].localidx); richk@6733: } richk@6733: richk@6720: for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache(); richk@10274: richk@10274: StationUpdateAnimTriggers(st); peter1138@3765: } peter1138@3765: } peter1138@3765: rubidium@6868: static CommandCost TerraformTile_Station(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@6868: { richk@10991: if (_settings_game.construction.build_on_slopes && AutoslopeEnabled()) { rubidium@6870: /* TODO: If you implement newgrf callback 149 'land slope check', you have to decide what to do with it here. rubidium@6870: * TTDP does not call it. rubidium@6870: */ rubidium@6870: if (!IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) { rubidium@6870: switch (GetStationType(tile)) { rubidium@6870: case STATION_RAIL: { rubidium@6870: DiagDirection direction = AxisToDiagDir(GetRailStationAxis(tile)); rubidium@6870: if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, direction)) break; rubidium@6870: if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, ReverseDiagDir(direction))) break; rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@6870: } rubidium@6870: rubidium@6870: case STATION_AIRPORT: rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@6870: rubidium@6870: case STATION_TRUCK: rubidium@6870: case STATION_BUS: { rubidium@6870: DiagDirection direction = GetRoadStopDir(tile); rubidium@6870: if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, direction)) break; rubidium@6870: if (IsDriveThroughStopTile(tile)) { rubidium@6870: if (!AutoslopeCheckForEntranceEdge(tile, z_new, tileh_new, ReverseDiagDir(direction))) break; rubidium@6870: } rubidium@6872: return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@6870: } rubidium@6870: rubidium@6870: default: break; rubidium@6870: } rubidium@6870: } rubidium@6870: } rubidium@6868: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@6868: } rubidium@6868: peter1138@3765: rubidium@5838: extern const TileTypeProcs _tile_type_station_procs = { belugas@3554: DrawTile_Station, /* draw_tile_proc */ belugas@3554: GetSlopeZ_Station, /* get_slope_z_proc */ belugas@3554: ClearTile_Station, /* clear_tile_proc */ belugas@3554: GetAcceptedCargo_Station, /* get_accepted_cargo_proc */ belugas@3554: GetTileDesc_Station, /* get_tile_desc_proc */ belugas@3554: GetTileTrackStatus_Station, /* get_tile_track_status_proc */ belugas@3554: ClickTile_Station, /* click_tile_proc */ belugas@3554: AnimateTile_Station, /* animate_tile_proc */ belugas@3554: TileLoop_Station, /* tile_loop_clear */ belugas@3554: ChangeTileOwner_Station, /* change_tile_owner_clear */ belugas@3554: NULL, /* get_produced_cargo_proc */ belugas@3554: VehicleEnter_Station, /* vehicle_enter_tile_proc */ richk@6743: GetFoundation_Station, /* get_foundation_proc */ rubidium@6868: TerraformTile_Station, /* terraform_tile_proc */ truelight@0: }; truelight@0: Darkvater@1881: static const SaveLoad _roadstop_desc[] = { richk@10242: SLE_VAR(RoadStop, xy, SLE_UINT32), tron@5969: SLE_CONDNULL(1, 0, 44), richk@10242: SLE_VAR(RoadStop, status, SLE_UINT8), truelight@1285: /* Index was saved in some versions, but this is not needed */ Darkvater@3222: SLE_CONDNULL(4, 0, 8), tron@5967: SLE_CONDNULL(2, 0, 44), tron@3479: SLE_CONDNULL(1, 0, 25), celestar@1217: richk@10242: SLE_REF(RoadStop, next, REF_ROADSTOPS), tron@6118: SLE_CONDNULL(2, 0, 44), celestar@1217: celestar@3431: SLE_CONDNULL(4, 0, 24), celestar@3475: SLE_CONDNULL(1, 25, 25), celestar@1217: celestar@1217: SLE_END() celestar@1217: }; truelight@0: Darkvater@1881: static const SaveLoad _station_desc[] = { rubidium@4344: SLE_CONDVAR(Station, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Station, xy, SLE_UINT32, 6, SL_MAX_VERSION), richk@10242: SLE_CONDNULL(4, 0, 5), ///< bus/lorry tile rubidium@4344: SLE_CONDVAR(Station, train_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Station, train_tile, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Station, airport_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Station, airport_tile, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Station, dock_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Station, dock_tile, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@4344: SLE_REF(Station, town, REF_TOWN), rubidium@4344: SLE_VAR(Station, trainst_w, SLE_UINT8), rubidium@4344: SLE_CONDVAR(Station, trainst_h, SLE_UINT8, 2, SL_MAX_VERSION), truelight@0: richk@10242: SLE_CONDNULL(1, 0, 3), ///< alpha_order truelight@0: rubidium@4344: SLE_VAR(Station, string_id, SLE_STRINGID), rubidium@6872: SLE_CONDSTR(Station, name, SLE_STR, 0, 84, SL_MAX_VERSION), rubidium@4344: SLE_VAR(Station, had_vehicle_of_type, SLE_UINT16), rubidium@4344: rubidium@4344: SLE_VAR(Station, time_since_load, SLE_UINT8), rubidium@4344: SLE_VAR(Station, time_since_unload, SLE_UINT8), rubidium@4344: SLE_VAR(Station, delete_ctr, SLE_UINT8), rubidium@4344: SLE_VAR(Station, owner, SLE_UINT8), rubidium@4344: SLE_VAR(Station, facilities, SLE_UINT8), rubidium@4344: SLE_VAR(Station, airport_type, SLE_UINT8), celestar@1217: richk@10242: SLE_CONDNULL(2, 0, 5), ///< Truck/bus stop status richk@10242: SLE_CONDNULL(1, 0, 4), ///< Blocked months rubidium@4344: celestar@6321: SLE_CONDVAR(Station, airport_flags, SLE_VAR_U64 | SLE_FILE_U16, 0, 2), celestar@6321: SLE_CONDVAR(Station, airport_flags, SLE_VAR_U64 | SLE_FILE_U32, 3, 45), rubidium@6868: SLE_CONDVAR(Station, airport_flags, SLE_UINT64, 46, NEWGRF_AIRPORTS_SAVEGAME - 1), rubidium@6868: SLE_CONDARR(Station, airport_flags, SLE_UINT64, 2, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), belugas@3554: richk@10242: SLE_CONDNULL(2, 0, 25), ///< last-vehicle rubidium@4344: SLE_CONDVAR(Station, last_vehicle_type, SLE_UINT8, 26, SL_MAX_VERSION), belugas@3554: richk@10242: SLE_CONDNULL(2, 3, 25), ///< custom station class and id rubidium@4344: SLE_CONDVAR(Station, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30), rubidium@4344: SLE_CONDVAR(Station, build_date, SLE_INT32, 31, SL_MAX_VERSION), rubidium@4344: rubidium@4344: SLE_CONDREF(Station, bus_stops, REF_ROADSTOPS, 6, SL_MAX_VERSION), rubidium@4344: SLE_CONDREF(Station, truck_stops, REF_ROADSTOPS, 6, SL_MAX_VERSION), celestar@1217: peter1138@3687: /* Used by newstations for graphic variations */ rubidium@4344: SLE_CONDVAR(Station, random_bits, SLE_UINT16, 27, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Station, waiting_triggers, SLE_UINT8, 27, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Station, num_specs, SLE_UINT8, 27, SL_MAX_VERSION), rubidium@6868: SLE_CONDVAR(Station, num_fsmportsspecs, SLE_UINT8, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), rubidium@6868: SLE_CONDVAR(Station, FSMport_layout_set, SLE_UINT8, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), rubidium@6868: SLE_CONDVAR(Station, FSMport_orientation, SLE_UINT8, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), rubidium@6868: SLE_CONDVAR(Station, FSMport_flood_protected, SLE_BOOL, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), peter1138@3687: richk@6719: SLE_CONDLST(Station, loading_vehicles, REF_VEHICLE, 57, SL_MAX_VERSION), richk@6719: rubidium@6871: /* reserve extra space in savegame here. (currently 32 bytes) */ Darkvater@3222: SLE_CONDNULL(32, 2, SL_MAX_VERSION), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: richk@6720: static uint16 _waiting_acceptance; richk@6720: static uint16 _cargo_source; richk@6720: static uint32 _cargo_source_xy; richk@6720: static uint16 _cargo_days; richk@6720: static Money _cargo_feeder_share; richk@6720: peter1138@3765: static const SaveLoad _station_speclist_desc[] = { peter1138@3765: SLE_CONDVAR(StationSpecList, grfid, SLE_UINT32, 27, SL_MAX_VERSION), peter1138@3765: SLE_CONDVAR(StationSpecList, localidx, SLE_UINT8, 27, SL_MAX_VERSION), peter1138@3765: peter1138@3765: SLE_END() peter1138@3765: }; peter1138@3765: richk@6733: static const SaveLoad _fsmports_speclist_desc[] = { rubidium@6868: SLE_CONDVAR(FSMportsSpecList, grfid, SLE_UINT32, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), rubidium@6868: SLE_CONDVAR(FSMportsSpecList, localidx, SLE_UINT8, NEWGRF_AIRPORTS_SAVEGAME, SL_MAX_VERSION), richk@6733: richk@6733: SLE_END() richk@6733: }; richk@6733: truelight@0: rubidium@6868: void SaveLoad_STNS(Station *st) truelight@0: { rubidium@6868: static const SaveLoad _goods_desc[] = { rubidium@6868: SLEG_CONDVAR( _waiting_acceptance, SLE_UINT16, 0, 67), rubidium@6868: SLE_CONDVAR(GoodsEntry, acceptance_pickup, SLE_UINT8, 68, SL_MAX_VERSION), rubidium@6868: SLE_CONDNULL(2, 51, 67), rubidium@6868: SLE_VAR(GoodsEntry, days_since_pickup, SLE_UINT8), rubidium@6868: SLE_VAR(GoodsEntry, rating, SLE_UINT8), rubidium@6868: SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6), rubidium@6868: SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67), rubidium@6868: SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67), rubidium@6868: SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67), rubidium@6868: SLE_VAR(GoodsEntry, last_speed, SLE_UINT8), rubidium@6868: SLE_VAR(GoodsEntry, last_age, SLE_UINT8), rubidium@6868: SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64), rubidium@6868: SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67), rubidium@6868: SLE_CONDLST(GoodsEntry, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION), rubidium@6868: rubidium@6868: SLE_END() rubidium@6868: }; rubidium@6868: rubidium@6868: truelight@0: SlObject(st, _station_desc); richk@6719: richk@6720: _waiting_acceptance = 0; richk@6720: richk@6719: uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO; richk@6719: for (CargoID i = 0; i < num_cargo; i++) { richk@6720: GoodsEntry *ge = &st->goods[i]; richk@6720: SlObject(ge, _goods_desc); richk@6720: if (CheckSavegameVersion(68)) { rubidium@6871: SB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, HasBit(_waiting_acceptance, 15)); richk@6720: if (GB(_waiting_acceptance, 0, 12) != 0) { richk@6720: /* Don't construct the packet with station here, because that'll fail with old savegames */ richk@6720: CargoPacket *cp = new CargoPacket(); richk@6720: /* In old versions, enroute_from used 0xFF as INVALID_STATION */ richk@6720: cp->source = (CheckSavegameVersion(7) && _cargo_source == 0xFF) ? INVALID_STATION : _cargo_source; richk@6720: cp->count = GB(_waiting_acceptance, 0, 12); richk@6720: cp->days_in_transit = _cargo_days; richk@6720: cp->feeder_share = _cargo_feeder_share; richk@6720: cp->source_xy = _cargo_source_xy; richk@6720: cp->days_in_transit = _cargo_days; richk@6720: cp->feeder_share = _cargo_feeder_share; rubidium@6868: SB(ge->acceptance_pickup, GoodsEntry::PICKUP, 1, 1); richk@6720: ge->cargo.Append(cp); richk@6720: } richk@6720: } truelight@1266: } peter1138@3765: peter1138@3765: if (st->num_specs != 0) { peter1138@3765: /* Allocate speclist memory when loading a game */ KUDr@5860: if (st->speclist == NULL) st->speclist = CallocT(st->num_specs); tron@6395: for (uint i = 0; i < st->num_specs; i++) { tron@6395: SlObject(&st->speclist[i], _station_speclist_desc); tron@6395: } peter1138@3765: } richk@6733: richk@6733: if (st->num_fsmportsspecs != 0) { richk@6733: /* Allocate fsmspeclist memory when loading a game */ richk@6733: if (st->fsmportsspeclist == NULL) st->fsmportsspeclist = CallocT(st->num_fsmportsspecs); richk@6733: for (uint i = 0; i < st->num_fsmportsspecs; i++) { richk@6733: SlObject(&st->fsmportsspeclist[i], _fsmports_speclist_desc); richk@6733: } richk@6733: } truelight@0: } truelight@0: rubidium@6573: static void Save_STNS() truelight@0: { truelight@0: Station *st; rubidium@6871: /* Write the stations */ truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: SlSetArrayIndex(st->index); truelight@4346: SlAutolength((AutolengthProc*)SaveLoad_STNS, st); truelight@0: } truelight@0: } truelight@0: rubidium@6573: static void Load_STNS() truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { KUDr@5916: Station *st = new (index) Station(); KUDr@5916: truelight@0: SaveLoad_STNS(st); truelight@0: rubidium@6871: /* this means it's an oldstyle savegame without support for nonuniform stations */ tron@2639: if (st->train_tile != 0 && st->trainst_h == 0) { tron@3033: uint w = GB(st->trainst_w, 4, 4); tron@3033: uint h = GB(st->trainst_w, 0, 4); tron@3033: tron@6432: if (GetRailStationAxis(st->train_tile) != AXIS_X) Swap(w, h); truelight@0: st->trainst_w = w; truelight@0: st->trainst_h = h; truelight@0: } truelight@0: } truelight@919: tron@1472: /* This is to ensure all pointers are within the limits of _stations_size */ matthijs@5247: if (_station_tick_ctr > GetMaxStationIndex()) _station_tick_ctr = 0; truelight@0: } truelight@0: rubidium@6573: static void Save_ROADSTOP() celestar@1217: { truelight@1284: RoadStop *rs; truelight@1284: truelight@1284: FOR_ALL_ROADSTOPS(rs) { truelight@4346: SlSetArrayIndex(rs->index); truelight@4346: SlObject(rs, _roadstop_desc); celestar@1217: } celestar@1217: } celestar@1217: rubidium@6573: static void Load_ROADSTOP() celestar@1217: { celestar@1217: int index; celestar@1217: truelight@1284: while ((index = SlIterateArray()) != -1) { tron@5967: RoadStop *rs = new (index) RoadStop(INVALID_TILE); celestar@5959: truelight@1284: SlObject(rs, _roadstop_desc); truelight@1284: } celestar@1217: } celestar@1217: rubidium@5838: extern const ChunkHandler _station_chunk_handlers[] = { celestar@1217: { 'STNS', Save_STNS, Load_STNS, CH_ARRAY }, celestar@1217: { 'ROAD', Save_ROADSTOP, Load_ROADSTOP, CH_ARRAY | CH_LAST}, truelight@0: };