tron@2186: /* $Id$ */ tron@2186: rubidium@4549: /** @file station_cmd.c */ celestar@2213: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" celestar@5573: #include "bridge_map.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" tron@3315: #include "station_map.h" tron@1363: #include "table/sprites.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@0: #include "station.h" truelight@0: #include "gfx.h" truelight@0: #include "window.h" truelight@0: #include "viewport.h" truelight@0: #include "command.h" truelight@0: #include "town.h" truelight@0: #include "vehicle.h" truelight@0: #include "news.h" truelight@0: #include "saveload.h" truelight@0: #include "economy.h" truelight@0: #include "player.h" truelight@0: #include "airport.h" darkvater@405: #include "sprite.h" truelight@1313: #include "depot.h" bjarni@2676: #include "train.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@4261: #include "date.h" truelight@0: celestar@5592: typedef enum StationRectModes celestar@5592: { celestar@5592: RECT_MODE_TEST = 0, celestar@5592: RECT_MODE_TRY, celestar@5592: RECT_MODE_FORCE celestar@5592: } StationRectMode; celestar@5592: KUDr@5583: static void StationRect_Init(Station *st); KUDr@5583: static bool StationRect_IsEmpty(Station *st); celestar@5592: static bool StationRect_BeforeAddTile(Station *st, TileIndex tile, StationRectMode mode); celestar@5592: static bool StationRect_BeforeAddRect(Station *st, TileIndex tile, int w, int h, StationRectMode mode); KUDr@5583: static bool StationRect_AfterRemoveTile(Station *st, TileIndex tile); KUDr@5587: static bool StationRect_AfterRemoveRect(Station *st, TileIndex tile, int w, int h); KUDr@5583: KUDr@5583: truelight@1272: /** truelight@1272: * Called if a new block is added to the station-pool truelight@1272: */ truelight@1272: static void StationPoolNewBlock(uint start_item) truelight@1272: { truelight@1272: Station *st; truelight@1272: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4348: * TODO - This is just a temporary stage, this will be removed. */ tron@4980: for (st = GetStation(start_item); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) st->index = start_item++; truelight@1272: } truelight@1272: peter1138@3587: static void StationPoolCleanBlock(uint start_item, uint end_item) peter1138@3587: { peter1138@3587: uint i; peter1138@3587: peter1138@3587: for (i = start_item; i <= end_item; i++) { peter1138@3587: Station *st = GetStation(i); peter1138@3587: free(st->speclist); peter1138@3587: st->speclist = NULL; peter1138@3587: } peter1138@3587: } peter1138@3587: truelight@1284: /** truelight@1284: * Called if a new block is added to the roadstop-pool truelight@1284: */ truelight@1284: static void RoadStopPoolNewBlock(uint start_item) truelight@1284: { truelight@1284: RoadStop *rs; truelight@1284: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4981: for (rs = GetRoadStop(start_item); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) rs->index = start_item++; truelight@1284: } truelight@1284: matthijs@5216: DEFINE_OLD_POOL(Station, Station, StationPoolNewBlock, StationPoolCleanBlock) matthijs@5216: DEFINE_OLD_POOL(RoadStop, RoadStop, RoadStopPoolNewBlock, NULL) truelight@1272: truelight@1272: truelight@0: extern void UpdateAirplanesOnNewStation(Station *st); truelight@0: peter1138@4904: static bool TileBelongsToRailStation(const Station *st, TileIndex tile) peter1138@4904: { peter1138@4904: return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile); peter1138@4904: } peter1138@4904: peter1138@5220: void MarkStationTilesDirty(const Station *st) peter1138@4904: { peter1138@4904: TileIndex tile = st->train_tile; Darkvater@4910: int w, h; peter1138@4904: peter1138@4904: // XXX No station is recorded as 0, not INVALID_TILE... peter1138@4904: if (tile == 0) return; peter1138@4904: peter1138@4904: for (h = 0; h < st->trainst_h; h++) { peter1138@4904: for (w = 0; w < st->trainst_w; w++) { peter1138@4904: if (TileBelongsToRailStation(st, tile)) { peter1138@4904: MarkTileDirtyByTile(tile); peter1138@4904: } peter1138@4904: tile += TileDiffXY(1, 0); peter1138@4904: } peter1138@4904: tile += TileDiffXY(-w, 1); peter1138@4904: } peter1138@4904: } peter1138@4904: tron@2549: static void MarkStationDirty(const Station* st) truelight@0: { truelight@0: if (st->sign.width_1 != 0) { truelight@0: InvalidateWindowWidget(WC_STATION_VIEW, st->index, 1); truelight@0: truelight@0: MarkAllViewportsDirty( truelight@0: st->sign.left - 6, truelight@0: st->sign.top, truelight@0: st->sign.left + (st->sign.width_1 << 2) + 12, truelight@0: st->sign.top + 48); truelight@0: } truelight@0: } truelight@0: celestar@1551: static void InitializeRoadStop(RoadStop *road_stop, RoadStop *previous, TileIndex tile, StationID index) celestar@1217: { celestar@1217: road_stop->xy = tile; celestar@1217: road_stop->used = true; celestar@1217: road_stop->status = 3; //stop is free celestar@1217: road_stop->next = NULL; celestar@1217: road_stop->prev = previous; celestar@1217: road_stop->station = index; celestar@3431: road_stop->num_vehicles = 0; celestar@1217: } celestar@1217: tron@2639: RoadStop* GetPrimaryRoadStop(const Station* st, RoadStopType type) celestar@1217: { celestar@1217: switch (type) { tron@2631: case RS_BUS: return st->bus_stops; celestar@1217: case RS_TRUCK: return st->truck_stops; tron@2631: default: NOT_REACHED(); celestar@1217: } celestar@1217: celestar@1217: return NULL; celestar@1217: } celestar@1217: tron@2639: RoadStop* GetRoadStopByTile(TileIndex tile, RoadStopType type) celestar@1217: { tron@3315: const Station* st = GetStationByTile(tile); tron@2639: RoadStop* rs; tron@2639: tron@2639: for (rs = GetPrimaryRoadStop(st, type); rs->xy != tile; rs = rs->next) { celestar@1217: assert(rs->next != NULL); tron@2639: } celestar@1217: celestar@1217: return rs; celestar@1217: } celestar@1217: truelight@4399: uint GetNumRoadStopsInStation(const Station* st, RoadStopType type) celestar@1217: { tron@2549: uint num = 0; celestar@1217: const RoadStop *rs; celestar@1217: celestar@1217: assert(st != NULL); tron@2549: for (rs = GetPrimaryRoadStop(st, type); rs != NULL; rs = rs->next) num++; celestar@1217: celestar@1217: return num; celestar@1217: } celestar@1217: tron@2549: RoadStop *AllocateRoadStop(void) celestar@1217: { truelight@1284: RoadStop *rs; truelight@1284: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4981: for (rs = GetRoadStop(0); rs != NULL; rs = (rs->index + 1U < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1U) : NULL) { truelight@4346: if (!IsValidRoadStop(rs)) { truelight@4397: RoadStopID index = rs->index; truelight@1284: tron@2549: memset(rs, 0, sizeof(*rs)); truelight@1284: rs->index = index; truelight@1284: celestar@1217: return rs; celestar@1217: } celestar@1217: } celestar@1217: truelight@1284: /* Check if we can add a block to the pool */ tron@4981: if (AddBlockToPool(&_RoadStop_pool)) return AllocateRoadStop(); truelight@1284: celestar@1217: return NULL; celestar@1217: } celestar@1217: truelight@703: /* Calculate the radius of the station. Basicly it is the biggest rubidium@4549: * radius that is available within the station */ tron@2643: static uint FindCatchmentRadius(const Station* st) truelight@703: { tron@2643: uint ret = 0; truelight@703: celestar@1217: if (st->bus_stops != NULL) ret = max(ret, CA_BUS); celestar@1217: if (st->truck_stops != NULL) ret = max(ret, CA_TRUCK); truelight@703: if (st->train_tile) ret = max(ret, CA_TRAIN); truelight@703: if (st->dock_tile) ret = max(ret, CA_DOCK); truelight@703: truelight@703: if (st->airport_tile) { truelight@703: switch (st->airport_type) { truelight@703: case AT_OILRIG: ret = max(ret, CA_AIR_OILPAD); break; truelight@703: case AT_SMALL: ret = max(ret, CA_AIR_SMALL); break; truelight@703: case AT_HELIPORT: ret = max(ret, CA_AIR_HELIPORT); break; truelight@703: case AT_LARGE: ret = max(ret, CA_AIR_LARGE); break; truelight@703: case AT_METROPOLITAN: ret = max(ret, CA_AIR_METRO); break; truelight@703: case AT_INTERNATIONAL: ret = max(ret, CA_AIR_INTER); break; richk@4059: case AT_COMMUTER: ret = max(ret, CA_AIR_COMMUTER); break; richk@4059: case AT_HELIDEPOT: ret = max(ret, CA_AIR_HELIDEPOT); break; richk@4059: case AT_INTERCON: ret = max(ret, CA_AIR_INTERCON); break; richk@4059: case AT_HELISTATION: ret = max(ret, CA_AIR_HELISTATION); break; truelight@703: } truelight@703: } truelight@703: truelight@703: return ret; truelight@703: } truelight@703: truelight@0: #define CHECK_STATIONS_ERR ((Station*)-1) truelight@0: tron@2498: static Station* GetStationAround(TileIndex tile, int w, int h, StationID closest_station) truelight@0: { truelight@0: // 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: { truelight@919: Station *st = GetStation(t); darkvater@28: // you cannot take control of an oilrig!! darkvater@28: if (st->airport_type == AT_OILRIG && st->facilities == (FACIL_AIRPORT|FACIL_DOCK)) darkvater@28: continue; darkvater@28: } 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: tron@1093: static Station *AllocateStation(void) truelight@0: { truelight@1272: Station *st = NULL; truelight@0: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4980: for (st = GetStation(0); st != NULL; st = (st->index + 1U < GetStationPoolSize()) ? GetStation(st->index + 1U) : NULL) { truelight@4346: if (!IsValidStation(st)) { celestar@1551: StationID index = st->index; truelight@1272: truelight@1272: memset(st, 0, sizeof(Station)); truelight@1272: st->index = index; truelight@1272: truelight@1272: return st; truelight@0: } truelight@0: } truelight@0: truelight@1272: /* Check if we can add a block to the pool */ tron@4980: if (AddBlockToPool(&_Station_pool)) return AllocateStation(); truelight@1272: truelight@1272: _error_message = STR_3008_TOO_MANY_STATIONS_LOADING; truelight@1272: return NULL; truelight@0: } truelight@0: 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@3515: * @result the noumber of matching tiles around belugas@3515: */ belugas@3515: static int CountMapSquareAround(TileIndex tile, TileType type, IndustryType industry) tron@1977: { belugas@3515: TileIndex cur_tile; belugas@3515: int dx, dy; truelight@0: int num = 0; truelight@0: belugas@3515: for (dx = -3; dx <= 3; dx++) { belugas@3515: for (dy = -3; dy <= 3; dy++) { glx@3682: cur_tile = TILE_MASK(tile + TileDiffXY(dx, dy)); belugas@3515: belugas@3515: if (IsTileType(cur_tile, type)) { belugas@3515: switch (type) { belugas@3515: case MP_INDUSTRY: belugas@3515: if (GetIndustryType(cur_tile) == industry) belugas@3515: num++; belugas@3515: break; belugas@3515: belugas@3515: case MP_WATER: belugas@3515: if (!IsWater(cur_tile)) belugas@3515: break; belugas@3515: /* FALL THROUGH WHEN WATER TILE */ belugas@3515: case MP_TREES: belugas@3515: num++; belugas@3515: break; belugas@3515: belugas@3515: default: belugas@3515: break; belugas@3515: } belugas@3515: } belugas@3515: } truelight@0: } truelight@0: truelight@0: return num; truelight@0: } truelight@0: truelight@0: #define M(x) ((x) - STR_SV_STNAME) truelight@0: tron@1977: static bool 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: truelight@0: Town *t = st->town; truelight@0: uint32 free_names = (uint32)-1; truelight@0: int found; truelight@0: uint z,z2; truelight@0: unsigned long tmp; truelight@0: truelight@0: { truelight@0: Station *s; truelight@0: truelight@0: FOR_ALL_STATIONS(s) { truelight@4346: if (s != st && s->town==t) { truelight@0: uint str = M(s->string_id); truelight@0: if (str <= 0x20) { truelight@0: if (str == M(STR_SV_STNAME_FOREST)) truelight@0: str = M(STR_SV_STNAME_WOODS); truelight@0: CLRBIT(free_names, str); truelight@0: } truelight@0: } truelight@193: } truelight@0: } truelight@193: truelight@0: /* check default names */ truelight@0: tmp = free_names & _gen_station_name_bits[flag]; truelight@0: if (tmp != 0) { truelight@0: found = FindFirstBit(tmp); truelight@0: goto done; truelight@0: } truelight@0: truelight@0: /* check mine? */ truelight@0: if (HASBIT(free_names, M(STR_SV_STNAME_MINES))) { belugas@3515: if (CountMapSquareAround(tile, MP_INDUSTRY, IT_COAL_MINE) >= 2 || belugas@3515: CountMapSquareAround(tile, MP_INDUSTRY, IT_IRON_MINE) >= 2 || belugas@3515: CountMapSquareAround(tile, MP_INDUSTRY, IT_COPPER_MINE) >= 2 || belugas@3515: CountMapSquareAround(tile, MP_INDUSTRY, IT_GOLD_MINE) >= 2 || belugas@3515: CountMapSquareAround(tile, MP_INDUSTRY, IT_DIAMOND_MINE) >= 2) { pasky@1454: found = M(STR_SV_STNAME_MINES); pasky@1454: goto done; pasky@1454: } truelight@0: } truelight@0: truelight@0: /* check close enough to town to get central as name? */ tron@1245: if (DistanceMax(tile,t->xy) < 8) { truelight@0: found = M(STR_SV_STNAME); truelight@0: if (HASBIT(free_names, M(STR_SV_STNAME))) goto done; truelight@0: truelight@0: found = M(STR_SV_STNAME_CENTRAL); truelight@0: if (HASBIT(free_names, M(STR_SV_STNAME_CENTRAL))) goto done; truelight@0: } truelight@0: truelight@0: /* Check lakeside */ tron@1472: if (HASBIT(free_names, M(STR_SV_STNAME_LAKESIDE)) && tron@1472: DistanceFromEdge(tile) < 20 && belugas@3515: CountMapSquareAround(tile, MP_WATER, 0) >= 5) { pasky@1454: found = M(STR_SV_STNAME_LAKESIDE); pasky@1454: goto done; pasky@1454: } truelight@0: truelight@0: /* Check woods */ tron@1472: if (HASBIT(free_names, M(STR_SV_STNAME_WOODS)) && ( belugas@3515: CountMapSquareAround(tile, MP_TREES, 0) >= 8 || belugas@3515: CountMapSquareAround(tile, MP_INDUSTRY, IT_FOREST) >= 2) tron@1472: ) { tron@1472: found = _opt.landscape == LT_DESERT ? tron@1472: M(STR_SV_STNAME_FOREST) : M(STR_SV_STNAME_WOODS); pasky@1454: goto done; truelight@0: } truelight@0: truelight@0: /* check elevation compared to town */ truelight@0: z = GetTileZ(tile); truelight@0: z2 = GetTileZ(t->xy); truelight@0: if (z < z2) { truelight@0: found = M(STR_SV_STNAME_VALLEY); truelight@0: if (HASBIT(free_names, M(STR_SV_STNAME_VALLEY))) goto done; truelight@0: } else if (z > z2) { truelight@0: found = M(STR_SV_STNAME_HEIGHTS); truelight@0: if (HASBIT(free_names, M(STR_SV_STNAME_HEIGHTS))) goto done; truelight@0: } truelight@0: truelight@0: /* check direction compared to town */ truelight@0: { truelight@0: static const int8 _direction_and_table[] = { truelight@0: ~( (1<xy)) + tron@926: (TileY(tile) < TileY(t->xy)) * 2]; truelight@0: } truelight@0: truelight@0: 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)); truelight@0: if (tmp == 0) { truelight@0: _error_message = STR_3007_TOO_MANY_STATIONS_LOADING; truelight@0: return false; truelight@0: } truelight@0: found = FindFirstBit(tmp); truelight@0: truelight@0: done: truelight@0: st->string_id = found + STR_SV_STNAME; truelight@0: return true; truelight@0: } truelight@0: #undef M truelight@0: tron@2498: static Station* GetClosestStationFromTile(TileIndex tile, uint threshold, PlayerID owner) truelight@0: { tron@1424: Station* best_station = NULL; tron@1424: Station* st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { Darkvater@4848: if ((owner == PLAYER_SPECTATOR || st->owner == owner)) { 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: truelight@0: static void StationInitialize(Station *st, TileIndex tile) truelight@0: { truelight@0: GoodsEntry *ge; truelight@0: truelight@0: st->xy = tile; celestar@1217: st->airport_tile = st->dock_tile = st->train_tile = 0; celestar@1217: st->bus_stops = st->truck_stops = NULL; truelight@0: st->had_vehicle_of_type = 0; truelight@0: st->time_since_load = 255; truelight@0: st->time_since_unload = 255; truelight@0: st->delete_ctr = 0; truelight@0: st->facilities = 0; truelight@0: celestar@3580: st->last_vehicle_type = VEH_Invalid; truelight@0: tron@1424: for (ge = st->goods; ge != endof(st->goods); ge++) { truelight@0: ge->waiting_acceptance = 0; truelight@0: ge->days_since_pickup = 0; truelight@1266: ge->enroute_from = INVALID_STATION; truelight@0: ge->rating = 175; truelight@0: ge->last_speed = 0; truelight@0: ge->last_age = 0xFF; celestar@1935: ge->feeder_profit = 0; truelight@0: } darkvater@243: peter1138@3687: st->random_bits = Random(); peter1138@3687: st->waiting_triggers = 0; peter1138@3687: KUDr@5583: StationRect_Init(st); truelight@0: } truelight@0: truelight@0: // Update the virtual coords needed to draw the station sign. truelight@0: // st = Station to update for. 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; celestar@1217: if (st->facilities & FACIL_AIRPORT && st->airport_type == AT_OILRIG) 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: truelight@0: // Update the virtual coords needed to draw the station sign for all stations. tron@1093: void UpdateAllStationVirtCoord(void) truelight@0: { tron@2639: Station* st; tron@2639: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: UpdateStationVirtCoord(st); truelight@0: } truelight@0: } truelight@0: truelight@0: // Update the station virt coords while making the modified parts dirty. truelight@0: static void UpdateStationVirtCoordDirty(Station *st) truelight@0: { truelight@0: MarkStationDirty(st); truelight@0: UpdateStationVirtCoord(st); truelight@0: MarkStationDirty(st); truelight@0: } truelight@0: truelight@0: // Get a mask of the cargo types that the station accepts. tron@1424: static uint GetAcceptanceMask(const Station *st) truelight@0: { truelight@0: uint mask = 0; tron@1424: uint i; tron@1424: tron@1424: for (i = 0; i != NUM_CARGO; i++) { tron@1424: if (st->goods[i].waiting_acceptance & 0x8000) mask |= 1 << i; truelight@0: } truelight@0: return mask; truelight@0: } truelight@0: truelight@0: // Items contains the two cargo names that are to be accepted or rejected. truelight@0: // msg is the string id of the message to display. tron@1424: static void ShowRejectOrAcceptNews(const Station *st, uint32 items, StringID msg) truelight@0: { truelight@0: if (items) { tron@2549: SetDParam(2, GB(items, 16, 16)); tron@2549: SetDParam(1, GB(items, 0, 16)); tron@534: SetDParam(0, st->index); Darkvater@4873: AddNewsItem(msg + (GB(items, 16, 16) ? 1 : 0), NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_TILE, NT_ACCEPTANCE, 0), st->xy, 0); truelight@0: } truelight@0: } truelight@0: truelight@0: // Get a list of the cargo types being produced around the tile. tron@1424: void GetProductionAroundTiles(AcceptedCargo produced, TileIndex tile, tron@1424: int w, int h, int rad) truelight@0: { truelight@0: int x,y; truelight@0: int x1,y1,x2,y2; truelight@0: int xc,yc; truelight@0: tron@2133: memset(produced, 0, sizeof(AcceptedCargo)); truelight@0: tron@926: x = TileX(tile); tron@926: y = TileY(tile); truelight@0: tron@1424: // expand the region by rad tiles on each side truelight@0: // while making sure that we remain inside the board. tron@856: x2 = min(x + w + rad, MapSizeX()); tron@1424: x1 = max(x - rad, 0); Celestar@568: tron@856: y2 = min(y + h + rad, MapSizeY()); tron@1424: 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@1424: for (yc = y1; yc != y2; yc++) { tron@1424: for (xc = x1; xc != x2; xc++) { truelight@0: if (!(IS_INSIDE_1D(xc, x, w) && IS_INSIDE_1D(yc, y, h))) { truelight@0: GetProducedCargoProc *gpc; tron@1981: TileIndex tile = TileXY(xc, yc); tron@1424: tron@1214: gpc = _tile_type_procs[GetTileType(tile)]->get_produced_cargo_proc; truelight@0: if (gpc != NULL) { Darkvater@3344: CargoID cargos[2] = { CT_INVALID, CT_INVALID }; tron@1424: truelight@0: gpc(tile, cargos); tron@1424: if (cargos[0] != CT_INVALID) { truelight@0: produced[cargos[0]]++; tron@1424: if (cargos[1] != CT_INVALID) { truelight@0: produced[cargos[1]]++; truelight@0: } truelight@0: } truelight@0: } truelight@0: } tron@1424: } tron@1424: } truelight@0: } truelight@0: truelight@0: // Get a list of the cargo types that are accepted around the tile. tron@1424: void GetAcceptanceAroundTiles(AcceptedCargo accepts, TileIndex tile, tron@1424: int w, int h, int rad) truelight@0: { truelight@0: int x,y; truelight@0: int x1,y1,x2,y2; truelight@0: int xc,yc; tron@473: tron@473: memset(accepts, 0, sizeof(AcceptedCargo)); truelight@0: tron@926: x = TileX(tile); tron@926: y = TileY(tile); truelight@0: tron@1424: // expand the region by rad tiles on each side truelight@0: // while making sure that we remain inside the board. tron@856: x2 = min(x + w + rad, MapSizeX()); tron@856: y2 = min(y + h + rad, MapSizeY()); tron@1424: x1 = max(x - rad, 0); tron@1424: 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@1424: for (yc = y1; yc != y2; yc++) { tron@1424: for (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@1424: uint i; tron@473: tron@473: GetAcceptedCargo(tile, ac); tron@2639: for (i = 0; i < lengthof(ac); ++i) accepts[i] += ac[i]; truelight@0: } tron@1424: } tron@1424: } truelight@0: } truelight@0: truelight@2793: typedef struct ottd_Rectangle { tron@1423: uint min_x; tron@1423: uint min_y; tron@1423: uint max_x; tron@1423: uint max_y; truelight@2793: } ottd_Rectangle; truelight@2793: KUDr@5103: static inline void MergePoint(ottd_Rectangle* rect, TileIndex tile) tron@1423: { tron@1423: uint x = TileX(tile); tron@1423: uint y = TileY(tile); tron@1423: tron@1423: if (rect->min_x > x) rect->min_x = x; tron@1423: if (rect->min_y > y) rect->min_y = y; tron@1423: if (rect->max_x < x) rect->max_x = x; tron@1423: if (rect->max_y < y) rect->max_y = y; tron@1423: } tron@1423: truelight@0: // Update the acceptance for a station. truelight@0: // show_msg controls whether to display a message that acceptance was changed. truelight@0: static void UpdateStationAcceptance(Station *st, bool show_msg) truelight@0: { truelight@0: uint old_acc, new_acc; tron@1423: const RoadStop *cur_rs; truelight@0: int i; truelight@2793: ottd_Rectangle rect; tron@1423: int rad; tron@1423: AcceptedCargo accepts; truelight@0: darkvater@1507: rect.min_x = MapSizeX(); darkvater@1507: rect.min_y = MapSizeY(); darkvater@1507: rect.max_x = rect.max_y = 0; truelight@0: // Don't update acceptance for a buoy tron@2639: if (IsBuoy(st)) return; truelight@0: truelight@0: /* old accepted goods types */ truelight@0: old_acc = GetAcceptanceMask(st); truelight@0: truelight@0: // 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); tron@1423: MergePoint(&rect, tron@1981: st->train_tile + TileDiffXY(st->trainst_w - 1, st->trainst_h - 1) tron@1423: ); truelight@0: } celestar@1217: truelight@0: if (st->airport_tile != 0) { tron@3876: const AirportFTAClass* afc = GetAirport(st->airport_type); tron@3876: tron@1423: MergePoint(&rect, st->airport_tile); tron@1423: MergePoint(&rect, tron@3876: st->airport_tile + TileDiffXY(afc->size_x - 1, afc->size_y - 1) tron@1423: ); celestar@1217: } celestar@1217: tron@1423: if (st->dock_tile != 0) MergePoint(&rect, st->dock_tile); tron@1423: tron@1423: for (cur_rs = st->bus_stops; cur_rs != NULL; cur_rs = cur_rs->next) { tron@1423: MergePoint(&rect, cur_rs->xy); celestar@1217: } truelight@0: tron@1423: for (cur_rs = st->truck_stops; cur_rs != NULL; cur_rs = cur_rs->next) { tron@1423: MergePoint(&rect, cur_rs->xy); truelight@0: } celestar@1217: tron@2631: rad = (_patches.modified_catchment) ? FindCatchmentRadius(st) : 4; truelight@0: truelight@0: // And retrieve the acceptance. tron@1423: if (rect.max_x >= rect.min_x) { tron@1423: GetAcceptanceAroundTiles( tron@1423: accepts, tron@1981: TileXY(rect.min_x, rect.min_y), tron@1423: rect.max_x - rect.min_x + 1, tron@1423: rect.max_y - rect.min_y + 1, tron@1423: rad tron@1423: ); truelight@0: } else { truelight@0: memset(accepts, 0, sizeof(accepts)); truelight@0: } truelight@0: truelight@0: // Adjust in case our station only accepts fewer kinds of goods tron@1424: for (i = 0; i != NUM_CARGO; i++) { truelight@0: uint amt = min(accepts[i], 15); truelight@0: truelight@0: // Make sure the station can accept the goods type. truelight@0: if ((i != CT_PASSENGERS && !(st->facilities & (byte)~FACIL_BUS_STOP)) || truelight@0: (i == CT_PASSENGERS && !(st->facilities & (byte)~FACIL_TRUCK_STOP))) truelight@0: amt = 0; truelight@0: tron@2504: SB(st->goods[i].waiting_acceptance, 12, 4, amt); truelight@0: } truelight@0: truelight@0: // Only show a message in case the acceptance was actually changed. truelight@0: new_acc = GetAcceptanceMask(st); truelight@0: if (old_acc == new_acc) truelight@0: return; truelight@193: truelight@0: // show a message to report that the acceptance was changed? truelight@0: if (show_msg && st->owner == _local_player && st->facilities) { truelight@0: uint32 accept=0, reject=0; /* these contain two string ids each */ truelight@0: const StringID *str = _cargoc.names_s; truelight@0: truelight@0: do { truelight@0: if (new_acc & 1) { truelight@193: if (!(old_acc & 1)) accept = (accept << 16) | *str; truelight@0: } else { truelight@0: if (old_acc & 1) reject = (reject << 16) | *str; truelight@0: } truelight@0: } while (str++,(new_acc>>=1) != (old_acc>>=1)); truelight@0: truelight@0: ShowRejectOrAcceptNews(st, accept, STR_3040_NOW_ACCEPTS); truelight@0: ShowRejectOrAcceptNews(st, reject, STR_303E_NO_LONGER_ACCEPTS); truelight@0: } truelight@0: truelight@0: // redraw the station view since acceptance changed truelight@0: InvalidateWindowWidget(WC_STATION_VIEW, st->index, 4); truelight@0: } truelight@0: KUDr@5098: static void UpdateStationSignCoord(Station *st) KUDr@5098: { KUDr@5587: Rect *r = &st->rect; KUDr@5583: KUDr@5583: if (StationRect_IsEmpty(st)) return; // no tiles belong to this station KUDr@5583: KUDr@5583: // clamp sign coord to be inside the station rect KUDr@5583: 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: truelight@0: // This is called right after a station was deleted. truelight@0: // It checks if the whole station is free of substations, and if so, the station will be truelight@0: // deleted after a little while. tron@2639: static void DeleteStationIfEmpty(Station* st) tron@2639: { truelight@0: if (st->facilities == 0) { truelight@0: st->delete_ctr = 0; celestar@3812: RebuildStationLists(); darkvater@28: InvalidateWindow(WC_STATION_LIST, st->owner); 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: tron@1977: static int32 ClearTile_Station(TileIndex tile, byte flags); darkvater@977: truelight@0: // Tries to clear the given area. Returns the cost in case of success. truelight@0: // Or an error code if it failed. tron@2498: int32 CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, StationID* station) truelight@0: { truelight@0: int32 cost = 0, ret; truelight@0: tron@3636: Slope tileh; tron@1335: uint z; tron@1335: int allowed_z = -1; tron@1335: int flat_z; truelight@0: truelight@0: 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: tron@2639: if (!EnsureNoVehicle(tile_cur)) return CMD_ERROR; truelight@0: truelight@0: 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) || tron@3636: ((_is_old_ai_player || !_patches.build_on_slopes) && tileh != SLOPE_FLAT)) { tron@3183: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: truelight@0: flat_z = z; tron@3636: if (tileh != SLOPE_FLAT) { truelight@0: // need to check so the entrance to the station is not pointing at a slope. tron@3636: if ((invalid_dirs&1 && !(tileh & SLOPE_NE) && (uint)w_cur == w) || rubidium@4434: (invalid_dirs&2 && !(tileh & SLOPE_SE) && h_cur == 1) || tron@3636: (invalid_dirs&4 && !(tileh & SLOPE_SW) && w_cur == 1) || tron@3636: (invalid_dirs&8 && !(tileh & SLOPE_NW) && (uint)h_cur == h)) { tron@3183: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: cost += _price.terraform; tron@3645: flat_z += TILE_HEIGHT; truelight@0: } truelight@193: truelight@0: // get corresponding flat level and make sure that all parts of the station have the same level. truelight@0: if (allowed_z == -1) { truelight@0: // first tile truelight@0: allowed_z = flat_z; truelight@0: } else if (allowed_z != flat_z) { tron@3183: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@0: } truelight@0: truelight@193: // if station is set, then we have special handling to allow building on top of already existing stations. tron@2498: // 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 truelight@0: // on exactly that station. tron@1035: if (station != NULL && IsTileType(tile_cur, MP_STATION)) { celestar@3334: if (!IsRailwayStation(tile_cur)) { tron@3183: return ClearTile_Station(tile_cur, DC_AUTO); // get error message truelight@0: } else { tron@3315: StationID st = GetStationIndex(tile_cur); tron@2498: if (*station == INVALID_STATION) { truelight@0: *station = st; tron@2498: } else if (*station != st) { tron@3183: return_cmd_error(STR_3006_ADJOINS_MORE_THAN_ONE_EXISTING); truelight@0: } truelight@0: } truelight@0: } else { tron@3491: ret = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR); tron@3183: if (CmdFailed(ret)) return ret; truelight@0: cost += ret; truelight@0: } truelight@0: END_TILE_LOOP(tile_cur, w, h, tile) truelight@0: truelight@0: return cost; truelight@0: } truelight@0: tron@3157: static bool CanExpandRailroadStation(Station* st, uint* fin, Axis axis) truelight@0: { truelight@0: uint curw = st->trainst_w, curh = st->trainst_h; tron@1977: TileIndex tile = fin[0]; truelight@0: uint w = fin[1]; truelight@0: uint h = fin[2]; truelight@0: truelight@0: if (_patches.nonuniform_stations) { truelight@0: // 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 { tron@3157: // check so the orientation is the same celestar@3334: if (GetRailStationAxis(st->train_tile) != axis) { Celestar@1189: _error_message = STR_306D_NONUNIFORM_STATIONS_DISALLOWED; Celestar@1189: return false; Celestar@1189: } truelight@0: truelight@0: // check if the new station adjoins the old station in either direction tron@1981: if (curw == w && st->train_tile == tile + TileDiffXY(0, h)) { truelight@0: // above truelight@0: curh += h; tron@1981: } else if (curw == w && st->train_tile == tile - TileDiffXY(0, curh)) { truelight@0: // below tron@1981: tile -= TileDiffXY(0, curh); truelight@0: curh += h; tron@1981: } else if (curh == h && st->train_tile == tile + TileDiffXY(w, 0)) { truelight@0: // to the left truelight@0: curw += w; tron@1981: } else if (curh == h && st->train_tile == tile - TileDiffXY(curw, 0)) { truelight@0: // to the right tron@1981: tile -= TileDiffXY(curw, 0); truelight@0: curw += w; Celestar@1189: } else { Celestar@1189: _error_message = STR_306D_NONUNIFORM_STATIONS_DISALLOWED; truelight@0: return false; Celestar@1189: } truelight@0: } truelight@0: // make sure the final size is not too big. Celestar@1189: if (curw > _patches.station_spread || curh > _patches.station_spread) { Celestar@1189: _error_message = STR_306C_STATION_TOO_SPREAD_OUT; Celestar@1189: return false; Celestar@1189: } truelight@0: truelight@0: // now tile contains the new value for st->train_tile truelight@0: // 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); truelight@0: 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) { truelight@0: layout[0-n] = 0; truelight@0: 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 Darkvater@1775: * @param p1 various bitstuffed elements Darkvater@1775: * - p1 = (bit 0) - orientation (p1 & 1) tron@2140: * - p1 = (bit 8-15) - number of tracks tron@2140: * - p1 = (bit 16-23) - platform length 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: */ tron@3491: int32 CmdBuildRailroadStation(TileIndex tile_org, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1775: Station *st; Darkvater@1775: int w_org, h_org; truelight@0: int32 cost, ret; tron@2498: StationID est; truelight@0: int plat_len, numtracks; tron@3157: Axis axis; truelight@0: uint finalvalues[3]; peter1138@3587: const StationSpec *statspec; peter1138@3587: int specindex; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: /* Does the authority allow this? */ Darkvater@1775: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile_org)) return CMD_ERROR; Darkvater@1775: if (!ValParamRailtype(p2 & 0xF)) return CMD_ERROR; Darkvater@1775: Darkvater@1775: /* unpack parameters */ tron@3157: axis = p1 & 1; tron@2140: numtracks = GB(p1, 8, 8); tron@2140: plat_len = GB(p1, 16, 8); Darkvater@1775: /* w = length, h = num_tracks */ 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: Darkvater@1781: if (h_org > _patches.station_spread || w_org > _patches.station_spread) return CMD_ERROR; Darkvater@1781: truelight@0: // these values are those that will be stored in train_tile and station_platforms truelight@0: finalvalues[0] = tile_org; truelight@0: finalvalues[1] = w_org; truelight@0: finalvalues[2] = h_org; truelight@0: truelight@0: // Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) tron@2498: est = INVALID_STATION; truelight@313: // If DC_EXEC is in flag, do not want to pass it to CheckFlatLandBelow, because of a nice bug truelight@313: // for detail info, see: https://sourceforge.net/tracker/index.php?func=detail&aid=1029064&group_id=103924&atid=636365 tron@3183: ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags & ~DC_EXEC, 5 << axis, _patches.nonuniform_stations ? &est : NULL); tron@3183: if (CmdFailed(ret)) return ret; truelight@0: cost = ret + (numtracks * _price.train_station_track + _price.train_station_length) * plat_len; truelight@0: truelight@0: // Make sure there are no similar stations around us. truelight@0: st = GetStationAround(tile_org, w_org, h_org, est); truelight@0: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; truelight@0: truelight@0: // See if there is a deleted station close to us. truelight@0: if (st == NULL) { truelight@0: st = GetClosestStationFromTile(tile_org, 8, _current_player); truelight@0: if (st != NULL && st->facilities) st = NULL; truelight@0: } truelight@0: truelight@0: if (st != NULL) { truelight@0: // Reuse an existing station. truelight@0: if (st->owner != OWNER_NONE && 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) { truelight@84: // check if we want to expanding an already existing station? truelight@2422: if (_is_old_ai_player || !_patches.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: Celestar@1189: //XXX can't we pack this in the "else" part of the if above? celestar@5592: if (!StationRect_BeforeAddRect(st, tile_org, w_org, h_org, RECT_MODE_TEST)) return CMD_ERROR; rubidium@4434: } else { truelight@0: // Create a new station truelight@0: st = AllocateStation(); tron@2639: if (st == NULL) return CMD_ERROR; truelight@193: truelight@0: st->town = ClosestTownFromTile(tile_org, (uint)-1); Darkvater@4850: if (IsValidPlayer(_current_player) && (flags & DC_EXEC)) truelight@0: SETBIT(st->town->have_ratings, _current_player); truelight@0: tron@2639: if (!GenerateStationName(st, tile_org, 0)) return CMD_ERROR; tron@2639: tron@2639: if (flags & DC_EXEC) StationInitialize(st, tile_org); truelight@0: } truelight@0: peter1138@3587: /* Check if the given station class is valid */ peter1138@3587: if (GB(p2, 8, 8) >= STAT_CLASS_MAX) return CMD_ERROR; peter1138@3587: peter1138@3587: /* Check if we can allocate a custom stationspec to this station */ belugas@3676: statspec = GetCustomStationSpec(GB(p2, 8, 8), GB(p2, 16, 8)); peter1138@3587: 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 */ peter1138@3754: 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 */ peter1138@3754: if (HASBIT(statspec->callbackmask, CBM_STATION_AVAIL) && GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE) == 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: truelight@313: // Now really clear the land below the station truelight@313: // It should never return CMD_ERROR.. but you never know ;) truelight@313: // (a bit strange function name for it, but it really does clear the land, when DC_EXEC is in flags) tron@3183: ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << axis, _patches.nonuniform_stations ? &est : NULL); tron@3183: if (CmdFailed(ret)) return ret; truelight@313: truelight@0: st->train_tile = finalvalues[0]; truelight@0: if (!st->facilities) st->xy = finalvalues[0]; truelight@0: st->facilities |= FACIL_TRAIN; truelight@0: st->owner = _current_player; truelight@0: truelight@0: st->trainst_w = finalvalues[1]; truelight@0: st->trainst_h = finalvalues[2]; truelight@193: truelight@71: st->build_date = _date; truelight@193: celestar@5592: StationRect_BeforeAddRect(st, tile_org, w_org, h_org, RECT_MODE_TRY); KUDr@5583: tron@3157: tile_delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); tron@4158: track = AxisToTrack(axis); truelight@193: truelight@0: layout_ptr = alloca(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++; peter1138@3769: MakeRailStation(tile, st->owner, st->index, axis, layout, GB(p2, 0, 4)); peter1138@3587: SetCustomStationSpecIndex(tile, specindex); peter1138@3742: SetStationTileRandomBits(tile, GB(Random(), 0, 4)); 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); peter1138@3769: uint16 callback = GetStationCallback(CBID_STATION_TILE_LAYOUT, platinfo, 0, statspec, st, tile); peter1138@3784: if (callback != CALLBACK_FAILED && callback < 8) SetStationGfx(tile, callback + axis); peter1138@3754: } peter1138@3754: truelight@0: tile += tile_delta; truelight@0: } while (--w); peter1138@2824: SetSignalsOnBothDir(tile_org, track); 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: peter1138@4904: MarkStationTilesDirty(st); truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); celestar@3812: RebuildStationLists(); truelight@0: InvalidateWindow(WC_STATION_LIST, st->owner); 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: uint i; truelight@0: truelight@0: restart: truelight@193: truelight@0: // too small? truelight@0: if (w != 0 && h != 0) { truelight@0: // check the left side, x = constant, y changes tron@1981: for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(0, i));) { truelight@0: // 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: truelight@0: // check the right side, x = constant, y changes tron@1981: for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(w - 1, i));) { truelight@0: // the right side is unused? tron@1981: if (++i == h) { tron@1981: w--; tron@1981: goto restart; tron@1981: } tron@1981: } truelight@0: truelight@0: // check the upper side, y = constant, x changes tron@1981: for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(i, 0));) { truelight@0: // 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: truelight@0: // check the lower side, y = constant, x changes tron@1981: for (i = 0; !TileBelongsToRailStation(st, tile + TileDiffXY(i, h - 1));) { truelight@0: // 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 Darkvater@1782: * @param p1 unused Darkvater@1782: * @param p2 unused Darkvater@1782: */ tron@3491: int32 CmdRemoveFromRailroadStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Station *st; truelight@0: darkvater@889: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); darkvater@889: truelight@0: // make sure the specified tile belongs to the current player, and that it is a railroad station. celestar@3334: if (!IsTileType(tile, MP_STATION) || !IsRailwayStation(tile) || !_patches.nonuniform_stations) return CMD_ERROR; tron@3315: st = GetStationByTile(tile); darkvater@149: if (_current_player != OWNER_WATER && (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile))) return CMD_ERROR; truelight@0: truelight@0: // if we reached here, it means we can actually delete it. do that. truelight@0: if (flags & DC_EXEC) { peter1138@3587: uint specindex = GetCustomStationSpecIndex(tile); celestar@3334: Track track = GetRailStationTrack(tile); truelight@0: DoClearSquare(tile); KUDr@5583: StationRect_AfterRemoveTile(st, tile); peter1138@2824: SetSignalsOnBothDir(tile, track); KUDr@3900: YapfNotifyTrackLayoutChange(tile, track); peter1138@3587: peter1138@3587: DeallocateSpecFromStation(st, specindex); peter1138@3587: truelight@0: // now we need to make the "spanned" area of the railway station smaller if we deleted something at the edges. truelight@0: // we also need to adjust train_tile. truelight@0: MakeRailwayStationAreaSmaller(st); peter1138@4904: MarkStationTilesDirty(st); KUDr@5098: UpdateStationSignCoord(st); truelight@0: truelight@0: // if we deleted the whole station, delete the train facility. truelight@0: if (st->train_tile == 0) { truelight@0: st->facilities &= ~FACIL_TRAIN; truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: } truelight@0: return _price.remove_rail_station; truelight@0: } truelight@0: truelight@0: // determine the number of platforms for the station Darkvater@2436: uint GetStationPlatforms(const Station *st, TileIndex tile) truelight@0: { tron@2498: TileIndex t; tron@2498: TileIndexDiff delta; celestar@3334: Axis axis; tron@2639: uint len; truelight@0: assert(TileBelongsToRailStation(st, tile)); truelight@0: truelight@0: len = 0; celestar@3334: axis = GetRailStationAxis(tile); celestar@3334: delta = (axis == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1)); truelight@0: truelight@0: // find starting tile.. truelight@0: t = tile; tron@2639: do { tron@2639: t -= delta; tron@2639: len++; celestar@3334: } while (TileBelongsToRailStation(st, t) && GetRailStationAxis(t) == axis); truelight@0: truelight@0: // find ending tile truelight@0: t = tile; tron@2639: do { tron@2639: t += delta; tron@2639: len++; celestar@3334: } while (TileBelongsToRailStation(st, t) && GetRailStationAxis(t) == axis); truelight@0: truelight@0: return len - 1; truelight@0: } truelight@0: celestar@3928: /** Determines the REMAINING length of a platform, starting at (and including) rubidium@4549: * the given tile. rubidium@4549: * @param tile the tile from which to start searching. Must be a railway station tile rubidium@4549: * @param dir The direction in which to search. rubidium@4549: * @return The platform length rubidium@4549: */ celestar@3928: uint GetPlatformLength(TileIndex tile, DiagDirection dir) celestar@3928: { celestar@3928: TileIndex start_tile = tile; celestar@3928: uint length = 0; celestar@3928: assert(IsRailwayStationTile(tile)); celestar@3928: assert(dir < DIAGDIR_END); celestar@3928: celestar@3928: do { celestar@3928: length ++; Darkvater@4559: tile += TileOffsByDiagDir(dir); celestar@3928: } while (IsCompatibleTrainStationTile(tile, start_tile)); celestar@3928: celestar@3928: return length; celestar@3928: } celestar@3928: celestar@3928: darkvater@149: static int32 RemoveRailroadStation(Station *st, TileIndex tile, uint32 flags) truelight@0: { truelight@0: int w,h; KUDr@5550: int32 cost = 0; truelight@0: darkvater@149: /* if there is flooding and non-uniform stations are enabled, remove platforms tile by tile */ darkvater@149: if (_current_player == OWNER_WATER && _patches.nonuniform_stations) tron@3491: return DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_FROM_RAILROAD_STATION); darkvater@149: truelight@0: /* Current player owns the station? */ truelight@0: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: /* determine width and height of platforms */ truelight@0: tile = st->train_tile; truelight@0: w = st->trainst_w; truelight@0: h = st->trainst_h; truelight@193: truelight@0: assert(w != 0 && h != 0); truelight@0: truelight@0: /* clear all areas of the station */ truelight@0: do { truelight@0: int w_bak = w; truelight@0: do { truelight@0: // for nonuniform stations, only remove tiles that are actually train station tiles truelight@0: if (TileBelongsToRailStation(st, tile)) { truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; KUDr@5550: cost += _price.remove_rail_station; peter1138@2824: if (flags & DC_EXEC) { celestar@3334: Track track = GetRailStationTrack(tile); truelight@0: DoClearSquare(tile); peter1138@2824: SetSignalsOnBothDir(tile, track); 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@5583: StationRect_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; peter1138@3587: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@193: truelight@0: return cost; truelight@0: } truelight@0: Darkvater@3435: int32 DoConvertStationRail(TileIndex tile, RailType totype, bool exec) truelight@0: { tron@3315: const Station* st = GetStationByTile(tile); tron@3315: truelight@0: if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR; truelight@0: truelight@0: // tile is not a railroad station? celestar@3334: if (!IsRailwayStation(tile)) return CMD_ERROR; truelight@0: tron@3242: if (GetRailType(tile) == totype) return CMD_ERROR; truelight@0: KUDr@5116: // 'hidden' elrails can't be downgraded to normal rail when elrails are disabled KUDr@5116: if (_patches.disable_elrails && totype == RAILTYPE_RAIL && GetRailType(tile) == RAILTYPE_ELECTRIC) return CMD_ERROR; KUDr@5116: truelight@0: if (exec) { tron@3242: SetRailType(tile, totype); truelight@0: MarkTileDirtyByTile(tile); KUDr@3900: YapfNotifyTrackLayoutChange(tile, GetRailStationTrack(tile)); truelight@0: } truelight@0: truelight@0: return _price.build_rail >> 1; truelight@0: } truelight@0: celestar@3691: /** Heavy wizardry used to add a roadstop to a station. rubidium@4549: * To understand the function, lets first look at what is passed around, rubidium@4549: * especially the last two parameters. CmdBuildRoadStop allocates a road rubidium@4549: * stop and needs to put that stop into the linked list of road stops. rubidium@4549: * It (CmdBuildRoadStop) has a **currstop pointer which points to element rubidium@4549: * in the linked list of stops (each element in this list being a pointer rubidium@4549: * in itself, hence the double pointer). We (FindRoadStopSpot) need to rubidium@4549: * modify this pointer (**currstop) thus we need to pass by reference, rubidium@4549: * obtaining a triple pointer (***currstop). When finished, **currstop rubidium@4549: * in CmdBuildRoadStop will contain the address of the pointer which will rubidium@4549: * then point into the global roadstop array. *prev (in CmdBuildRoadStop) rubidium@4549: * is the pointer tino the global roadstop array which has *currstop in rubidium@4549: * its ->next element. rubidium@4549: * @param[in] truck_station Determines whether a stop is RS_BUS or RS_TRUCK rubidium@4549: * @param[in] station The station to do the whole procedure for rubidium@4549: * @param[out] currstop See the detailed function description rubidium@4549: * @param prev See the detailed function description rubidium@4549: */ celestar@3691: static void FindRoadStopSpot(bool truck_station, Station* st, RoadStop*** currstop, RoadStop** prev) celestar@1217: { celestar@3691: RoadStop **primary_stop = (truck_station) ? &st->truck_stops : &st->bus_stops; celestar@3691: assert(*prev == NULL); celestar@1217: celestar@1217: if (*primary_stop == NULL) { celestar@3691: //we have no roadstop of the type yet, so write a "primary stop" celestar@1217: *currstop = primary_stop; celestar@1217: } else { celestar@1217: //there are stops already, so append to the end of the list celestar@1217: *prev = *primary_stop; celestar@1217: *currstop = &(*primary_stop)->next; celestar@1217: while (**currstop != NULL) { celestar@1217: *prev = (*prev)->next; celestar@1217: *currstop = &(**currstop)->next; celestar@1217: } celestar@1217: } celestar@1217: } celestar@1217: celestar@3691: /** Build a bus or truck stop celestar@3691: * @param tile tile to build the stop at tron@3333: * @param p1 entrance direction (DiagDirection) Darkvater@1782: * @param p2 0 for Bus stops, 1 for truck stops truelight@0: */ tron@3491: int32 CmdBuildRoadStop(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1782: Station *st; celestar@1217: RoadStop *road_stop; celestar@1217: RoadStop **currstop; celestar@1217: RoadStop *prev = NULL; truelight@0: int32 cost; tron@3183: int32 ret; Darkvater@1782: bool type = !!p2; Darkvater@1782: Darkvater@1782: /* Saveguard the parameters */ matthijs@3699: if (!IsValidDiagDirection(p1)) return CMD_ERROR; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) truelight@0: return CMD_ERROR; truelight@0: tron@3183: ret = CheckFlatLandBelow(tile, 1, 1, flags, 1 << p1, NULL); tron@3183: if (CmdFailed(ret)) return ret; tron@3183: cost = ret; truelight@0: truelight@0: st = GetStationAround(tile, 1, 1, -1); tron@2639: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; truelight@0: truelight@0: /* Find a station close to us */ truelight@0: if (st == NULL) { truelight@0: st = GetClosestStationFromTile(tile, 8, _current_player); tron@2989: if (st != NULL && st->facilities != 0) st = NULL; truelight@0: } truelight@0: celestar@1217: //give us a road stop in the list, and check if something went wrong truelight@1284: road_stop = AllocateRoadStop(); tron@2951: if (road_stop == NULL) { tron@2951: return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS); tron@2951: } tron@2951: tron@2989: if (st != NULL && truelight@4399: GetNumRoadStopsInStation(st, RS_BUS) + GetNumRoadStopsInStation(st, RS_TRUCK) >= ROAD_STOP_LIMIT) { tron@2951: return_cmd_error(type ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS); tron@2951: } celestar@1217: truelight@0: if (st != NULL) { tron@2951: if (st->owner != OWNER_NONE && st->owner != _current_player) { truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); tron@2951: } tron@2951: celestar@5592: if (!StationRect_BeforeAddTile(st, tile, RECT_MODE_TEST)) return CMD_ERROR; truelight@0: celestar@3691: FindRoadStopSpot(type, st, &currstop, &prev); truelight@0: } else { truelight@0: Town *t; truelight@0: truelight@0: st = AllocateStation(); tron@2639: if (st == NULL) return CMD_ERROR; truelight@0: truelight@0: st->town = t = ClosestTownFromTile(tile, (uint)-1); truelight@0: celestar@3691: FindRoadStopSpot(type, st, &currstop, &prev); celestar@1217: Darkvater@4850: if (IsValidPlayer(_current_player) && (flags & DC_EXEC)) { truelight@0: SETBIT(t->have_ratings, _current_player); tron@2951: } truelight@0: truelight@0: st->sign.width_1 = 0; truelight@0: tron@2639: if (!GenerateStationName(st, tile, 0)) return CMD_ERROR; tron@2639: tron@2639: if (flags & DC_EXEC) StationInitialize(st, tile); truelight@0: } truelight@0: celestar@1217: cost += (type) ? _price.build_truck_station : _price.build_bus_station; truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@1217: //point to the correct item in the _busstops or _truckstops array celestar@1217: *currstop = road_stop; celestar@1217: celestar@1217: //initialize an empty station celestar@1217: InitializeRoadStop(road_stop, prev, tile, st->index); truelight@0: if (!st->facilities) st->xy = tile; celestar@1217: st->facilities |= (type) ? FACIL_TRUCK_STOP : FACIL_BUS_STOP; truelight@0: st->owner = _current_player; truelight@193: truelight@71: st->build_date = _date; truelight@0: celestar@5592: StationRect_BeforeAddTile(st, tile, RECT_MODE_TRY); KUDr@5583: celestar@3334: MakeRoadStop(tile, st->owner, st->index, type, p1); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); celestar@3812: RebuildStationLists(); truelight@0: InvalidateWindow(WC_STATION_LIST, st->owner); truelight@0: } truelight@0: return cost; truelight@0: } truelight@0: truelight@0: // Remove a bus station celestar@1217: static int32 RemoveRoadStop(Station *st, uint32 flags, TileIndex tile) truelight@0: { celestar@1217: RoadStop **primary_stop; celestar@1217: RoadStop *cur_stop; celestar@3334: bool is_truck = IsTruckStop(tile); truelight@0: tron@2951: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) { truelight@0: return CMD_ERROR; tron@2951: } truelight@0: tron@2639: if (is_truck) { // truck stop celestar@1217: primary_stop = &st->truck_stops; celestar@1217: cur_stop = GetRoadStopByTile(tile, RS_TRUCK); celestar@1217: } else { celestar@1217: primary_stop = &st->bus_stops; celestar@1217: cur_stop = GetRoadStopByTile(tile, RS_BUS); celestar@1217: } celestar@1217: celestar@1217: assert(cur_stop != NULL); truelight@193: tron@2951: if (!EnsureNoVehicle(tile)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@1217: //we only had one stop left celestar@1217: if (cur_stop->next == NULL && cur_stop->prev == NULL) { celestar@1217: //so we remove ALL stops celestar@1217: *primary_stop = NULL; celestar@1217: st->facilities &= (is_truck) ? ~FACIL_TRUCK_STOP : ~FACIL_BUS_STOP; celestar@1217: } else if (cur_stop == *primary_stop) { celestar@1217: //removed the first stop in the list celestar@1217: //need to set the primary element to the next stop celestar@1217: *primary_stop = (*primary_stop)->next; celestar@1217: } truelight@0: truelight@4398: DeleteRoadStop(cur_stop); truelight@4398: DoClearSquare(tile); KUDr@5583: StationRect_AfterRemoveTile(st, tile); truelight@4398: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: celestar@1217: return (is_truck) ? _price.remove_truck_station : _price.remove_bus_station; truelight@0: } truelight@0: truelight@0: truelight@0: truelight@0: // FIXME -- need to move to its corresponding Airport variable truelight@0: // Country Airfield (small) celestar@3429: static const byte _airport_sections_country[] = { truelight@0: 54, 53, 52, 65, truelight@0: 58, 57, 56, 55, truelight@0: 64, 63, 63, 62 truelight@0: }; truelight@0: truelight@0: // City Airport (large) celestar@3429: static const byte _airport_sections_town[] = { tron@2549: 31, 9, 33, 9, 9, 32, tron@2549: 27, 36, 29, 34, 8, 10, truelight@0: 30, 11, 35, 13, 20, 21, truelight@0: 51, 12, 14, 17, 19, 28, truelight@0: 38, 13, 15, 16, 18, 39, truelight@0: 26, 22, 23, 24, 25, 26 truelight@0: }; truelight@0: truelight@0: // Metropolitain Airport (large) - 2 runways celestar@3429: static const byte _airport_sections_metropolitan[] = { tron@2549: 31, 9, 33, 9, 9, 32, tron@2549: 27, 36, 29, 34, 8, 10, tron@2549: 30, 11, 35, 13, 20, 21, tron@2549: 102, 8, 8, 8, 8, 28, tron@2549: 83, 84, 84, 84, 84, 83, tron@2549: 26, 23, 23, 23, 23, 26 truelight@0: }; truelight@0: truelight@0: // International Airport (large) - 2 runways celestar@3429: static const byte _airport_sections_international[] = { belugas@3554: 88, 89, 89, 89, 89, 89, 88, belugas@3554: 51, 8, 8, 8, 8, 8, 32, tron@2549: 30, 8, 11, 27, 11, 8, 10, truelight@0: 32, 8, 11, 27, 11, 8, 114, truelight@0: 87, 8, 11, 85, 11, 8, 114, tron@2549: 87, 8, 8, 8, 8, 8, 90, tron@2549: 26, 23, 23, 23, 23, 23, 26 truelight@0: }; truelight@0: richk@4059: // Intercontinental Airport (vlarge) - 4 runways richk@4059: static const byte _airport_sections_intercontinental[] = { tron@4077: 102, 120, 89, 89, 89, 89, 89, 89, 118, tron@4077: 120, 22, 22, 22, 22, 22, 22, 119, 117, tron@4077: 87, 54, 87, 8, 8, 8, 8, 51, 117, tron@4077: 87, 162, 87, 85, 116, 116, 8, 9, 10, richk@4059: 87, 8, 8, 11, 31, 11, 8, 160, 32, richk@4059: 32, 160, 8, 11, 27, 11, 8, 8, 10, richk@4059: 87, 8, 8, 11, 30, 11, 8, 8, 10, richk@4066: 87, 142, 8, 11, 29, 11, 10, 163, 10, richk@4066: 87, 164, 87, 8, 8, 8, 10, 37, 117, tron@4077: 87, 120, 89, 89, 89, 89, 89, 89, 119, tron@4077: 121, 22, 22, 22, 22, 22, 22, 119, 37 richk@4059: }; richk@4059: richk@4059: richk@4059: // Commuter Airfield (small) richk@4059: static const byte _airport_sections_commuter[] = { richk@4059: 85, 30, 115, 115, 32, richk@4059: 87, 8, 8, 8, 10, richk@4059: 87, 11, 11, 11, 10, richk@4059: 26, 23, 23, 23, 26 richk@4059: }; richk@4059: truelight@0: // Heliport celestar@3429: static const byte _airport_sections_heliport[] = { truelight@0: 66, truelight@0: }; truelight@0: richk@4059: // Helidepot richk@4059: static const byte _airport_sections_helidepot[] = { richk@4059: 124, 32, richk@4059: 122, 123 richk@4059: }; richk@4059: richk@4059: // Helistation richk@4059: static const byte _airport_sections_helistation[] = { richk@4059: 32, 134, 159, 158, richk@4059: 161, 142, 142, 157 richk@4059: }; richk@4059: celestar@3471: static const byte * const _airport_sections[] = { richk@4059: _airport_sections_country, // Country Airfield (small) richk@4059: _airport_sections_town, // City Airport (large) richk@4059: _airport_sections_heliport, // Heliport richk@4059: _airport_sections_metropolitan, // Metropolitain Airport (large) richk@4059: _airport_sections_international, // International Airport (xlarge) richk@4059: _airport_sections_commuter, // Commuter Airport (small) richk@4059: _airport_sections_helidepot, // Helidepot richk@4059: _airport_sections_intercontinental, // Intercontinental Airport (xxlarge) richk@4059: _airport_sections_helistation // Helistation truelight@0: }; truelight@0: Darkvater@1784: /** Place an Airport. tron@3491: * @param tile tile where airport will be built Darkvater@1784: * @param p1 airport type, @see airport.h Darkvater@1784: * @param p2 unused truelight@0: */ tron@3491: int32 CmdBuildAirport(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Town *t; truelight@0: Station *st; truelight@0: int32 cost; tron@3183: int32 ret; Darkvater@1784: int w, h; truelight@193: bool airport_upgrade = true; tron@3876: const AirportFTAClass* afc; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: Darkvater@1784: /* Check if a valid, buildable airport was chosen for construction */ celestar@3471: if (p1 > lengthof(_airport_sections) || !HASBIT(GetValidAirports(), p1)) return CMD_ERROR; Darkvater@1784: truelight@0: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: t = ClosestTownFromTile(tile, (uint)-1); truelight@0: truelight@0: /* Check if local auth refuses a new airport */ truelight@0: { truelight@0: uint num = 0; truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: if (st->town == t && st->facilities&FACIL_AIRPORT && st->airport_type != AT_OILRIG) truelight@0: num++; truelight@0: } truelight@0: if (num >= 2) { tron@534: SetDParam(0, t->index); truelight@0: return_cmd_error(STR_2035_LOCAL_AUTHORITY_REFUSES); truelight@0: } truelight@0: } truelight@0: tron@3876: afc = GetAirport(p1); tron@3876: w = afc->size_x; tron@3876: h = afc->size_y; truelight@0: tron@3183: ret = CheckFlatLandBelow(tile, w, h, flags, 0, NULL); tron@3183: if (CmdFailed(ret)) return ret; tron@3183: cost = ret; truelight@0: truelight@0: st = GetStationAround(tile, w, h, -1); Darkvater@1784: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; truelight@0: truelight@0: /* Find a station close to us */ truelight@0: if (st == NULL) { truelight@0: st = GetClosestStationFromTile(tile, 8, _current_player); Darkvater@1784: if (st != NULL && st->facilities) st = NULL; truelight@0: } truelight@0: truelight@0: if (st != NULL) { truelight@0: if (st->owner != OWNER_NONE && st->owner != _current_player) truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); truelight@193: celestar@5592: if (!StationRect_BeforeAddRect(st, tile, w, h, RECT_MODE_TEST)) return CMD_ERROR; truelight@0: truelight@0: if (st->airport_tile != 0) truelight@0: return_cmd_error(STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT); truelight@0: } else { truelight@0: airport_upgrade = false; truelight@0: truelight@0: st = AllocateStation(); Darkvater@1784: if (st == NULL) return CMD_ERROR; truelight@0: tron@1265: st->town = t; truelight@0: Darkvater@4850: if (IsValidPlayer(_current_player) && (flags & DC_EXEC)) truelight@0: SETBIT(t->have_ratings, _current_player); truelight@0: truelight@0: st->sign.width_1 = 0; truelight@0: truelight@0: // if airport type equals Heliport then generate truelight@0: // type 5 name, which is heliport, otherwise airport names (1) richk@4059: if (!GenerateStationName(st, tile, (p1 == AT_HELIPORT)||(p1 == AT_HELIDEPOT)||(p1 == AT_HELISTATION) ? 5 : 1)) truelight@0: return CMD_ERROR; truelight@0: tron@2951: if (flags & DC_EXEC) StationInitialize(st, tile); truelight@0: } truelight@0: truelight@0: cost += _price.build_airport * w * h; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: st->owner = _current_player; truelight@0: st->airport_tile = tile; truelight@0: if (!st->facilities) st->xy = tile; truelight@0: st->facilities |= FACIL_AIRPORT; truelight@0: st->airport_type = (byte)p1; truelight@0: st->airport_flags = 0; truelight@193: truelight@71: st->build_date = _date; truelight@0: celestar@5592: StationRect_BeforeAddRect(st, tile, w, h, RECT_MODE_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: */ tron@2549: if (airport_upgrade) UpdateAirplanesOnNewStation(st); truelight@193: truelight@0: { celestar@3471: const byte *b = _airport_sections[p1]; tron@2549: tron@3033: BEGIN_TILE_LOOP(tile_cur, w, h, tile) { celestar@3334: MakeAirport(tile_cur, st->owner, st->index, *b++); tron@3033: } END_TILE_LOOP(tile_cur, w, h, tile) truelight@0: } truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); celestar@3812: RebuildStationLists(); truelight@0: InvalidateWindow(WC_STATION_LIST, st->owner); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static int32 RemoveAirport(Station *st, uint32 flags) truelight@0: { tron@1977: TileIndex tile; truelight@0: int w,h; truelight@0: int32 cost; tron@3876: const AirportFTAClass* afc; truelight@0: truelight@0: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: tile = st->airport_tile; truelight@0: tron@3876: afc = GetAirport(st->airport_type); tron@3876: w = afc->size_x; tron@3876: h = afc->size_y; truelight@0: truelight@0: cost = w * h * _price.remove_airport; truelight@0: tron@3033: BEGIN_TILE_LOOP(tile_cur, w, h, tile) { tron@3033: if (!EnsureNoVehicle(tile_cur)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: DeleteAnimatedTile(tile_cur); truelight@0: DoClearSquare(tile_cur); truelight@0: } tron@3033: } END_TILE_LOOP(tile_cur, w,h,tile) truelight@0: truelight@0: if (flags & DC_EXEC) { tron@700: uint i; tron@700: tron@3033: for (i = 0; i < afc->nof_depots; ++i) { tron@3033: DeleteWindowById( tron@3033: WC_VEHICLE_DEPOT, tile + ToTileIndexDiff(afc->airport_depots[i]) tron@3033: ); tron@3033: } darkvater@755: KUDr@5583: StationRect_AfterRemoveRect(st, tile, w, h); KUDr@5583: truelight@0: st->airport_tile = 0; truelight@0: st->facilities &= ~FACIL_AIRPORT; truelight@0: 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 Darkvater@1784: * @param p1 unused Darkvater@1784: * @param p2 unused truelight@0: */ tron@3491: int32 CmdBuildBuoy(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Station *st; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: celestar@3374: if (!IsClearWaterTile(tile) || tile == 0) return_cmd_error(STR_304B_SITE_UNSUITABLE); truelight@0: truelight@0: st = AllocateStation(); Darkvater@1784: if (st == NULL) return CMD_ERROR; truelight@0: tron@3055: st->town = ClosestTownFromTile(tile, (uint)-1); truelight@0: st->sign.width_1 = 0; truelight@0: tron@3055: if (!GenerateStationName(st, tile, 4)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { tron@3055: StationInitialize(st, tile); 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: celestar@3334: MakeBuoy(tile, st->index); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); celestar@3812: RebuildStationLists(); truelight@0: InvalidateWindow(WC_STATION_LIST, st->owner); truelight@0: } truelight@0: truelight@0: return _price.build_dock; truelight@0: } truelight@0: dominik@1078: /* Checks if any ship is servicing the buoy specified. Returns yes or no */ dominik@1078: static bool CheckShipsOnBuoy(Station *st) dominik@1078: { dominik@1078: const Vehicle *v; dominik@1078: FOR_ALL_VEHICLES(v) { dominik@1078: if (v->type == VEH_Ship) { dominik@1078: const Order *order; dominik@1078: FOR_VEHICLE_ORDERS(v, order) { tron@4527: if (order->type == OT_GOTO_STATION && order->dest == st->index) { dominik@1078: return true; dominik@1078: } dominik@1078: } dominik@1078: } dominik@1078: } dominik@1078: return false; dominik@1078: } dominik@1078: truelight@0: static int32 RemoveBuoy(Station *st, uint32 flags) truelight@0: { tron@1977: TileIndex tile; truelight@0: Darkvater@4850: /* XXX: strange stuff */ Darkvater@4850: if (!IsValidPlayer(_current_player)) return_cmd_error(INVALID_STRING_ID); truelight@0: truelight@0: tile = st->dock_tile; truelight@0: tron@2639: if (CheckShipsOnBuoy(st)) return_cmd_error(STR_BUOY_IS_IN_USE); tron@2639: if (!EnsureNoVehicle(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: tron@3111: MakeWater(tile); tron@3111: MarkTileDirtyByTile(tile); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: truelight@0: return _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 Darkvater@1784: * @param p1 unused Darkvater@1784: * @param p2 unused Darkvater@1784: */ tron@3491: int32 CmdBuildDock(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@3055: TileIndex tile_cur; tron@3157: DiagDirection direction; truelight@0: int32 cost; truelight@0: Station *st; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: tron@3055: switch (GetTileSlope(tile, NULL)) { tron@3636: case SLOPE_SW: direction = DIAGDIR_NE; break; tron@3636: case SLOPE_SE: direction = DIAGDIR_NW; break; tron@3636: case SLOPE_NW: direction = DIAGDIR_SE; break; tron@3636: case SLOPE_NE: direction = DIAGDIR_SW; break; tron@3055: default: return_cmd_error(STR_304B_SITE_UNSUITABLE); tron@3055: } tron@3055: peter1138@4619: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) return CMD_ERROR; peter1138@4619: tron@3055: if (!EnsureNoVehicle(tile)) return CMD_ERROR; tron@3055: tron@3491: cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); Darkvater@1784: if (CmdFailed(cost)) return CMD_ERROR; truelight@0: Darkvater@4559: tile_cur = tile + TileOffsByDiagDir(direction); truelight@0: Darkvater@1784: if (!EnsureNoVehicle(tile_cur)) return CMD_ERROR; 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: tron@3491: cost = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR); Darkvater@1784: if (CmdFailed(cost)) 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 */ tron@909: st = GetStationAround( tron@909: tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]), truelight@0: _dock_w_chk[direction], _dock_h_chk[direction], -1); Darkvater@1784: if (st == CHECK_STATIONS_ERR) return CMD_ERROR; truelight@193: truelight@0: /* Find a station close to us */ truelight@0: if (st == NULL) { truelight@0: st = GetClosestStationFromTile(tile, 8, _current_player); truelight@0: if (st!=NULL && st->facilities) st = NULL; truelight@0: } truelight@0: truelight@0: if (st != NULL) { truelight@0: if (st->owner != OWNER_NONE && st->owner != _current_player) truelight@0: return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION); truelight@193: celestar@5592: if (!StationRect_BeforeAddRect(st, tile, _dock_w_chk[direction], _dock_h_chk[direction], RECT_MODE_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 { truelight@0: Town *t; truelight@0: truelight@0: st = AllocateStation(); Darkvater@1784: if (st == NULL) return CMD_ERROR; truelight@0: truelight@0: st->town = t = ClosestTownFromTile(tile, (uint)-1); truelight@0: Darkvater@4850: if (IsValidPlayer(_current_player) && (flags & DC_EXEC)) truelight@0: SETBIT(t->have_ratings, _current_player); truelight@0: truelight@0: st->sign.width_1 = 0; truelight@0: Darkvater@1784: if (!GenerateStationName(st, tile, 3)) return CMD_ERROR; truelight@0: tron@2639: if (flags & DC_EXEC) StationInitialize(st, tile); truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: st->dock_tile = tile; truelight@0: if (!st->facilities) st->xy = tile; truelight@0: st->facilities |= FACIL_DOCK; truelight@0: st->owner = _current_player; truelight@193: truelight@71: st->build_date = _date; truelight@0: celestar@5592: StationRect_BeforeAddRect(st, tile, _dock_w_chk[direction], _dock_h_chk[direction], RECT_MODE_TRY); KUDr@5583: celestar@3334: MakeDock(tile, st->owner, st->index, direction); truelight@193: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); celestar@3812: RebuildStationLists(); truelight@0: InvalidateWindow(WC_STATION_LIST, st->owner); truelight@0: } truelight@0: return _price.build_dock; truelight@0: } truelight@0: truelight@0: static int32 RemoveDock(Station *st, uint32 flags) truelight@0: { tron@1977: TileIndex tile1; tron@1977: TileIndex tile2; truelight@0: tron@2639: if (!CheckOwnership(st->owner)) return CMD_ERROR; truelight@0: truelight@0: tile1 = st->dock_tile; Darkvater@4559: tile2 = tile1 + TileOffsByDiagDir(GetDockDirection(tile1)); truelight@0: tron@2639: if (!EnsureNoVehicle(tile1)) return CMD_ERROR; tron@2639: if (!EnsureNoVehicle(tile2)) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: DoClearSquare(tile1); tron@3111: MakeWater(tile2); KUDr@5583: KUDr@5583: StationRect_AfterRemoveTile(st, tile1); KUDr@5583: StationRect_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: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@0: truelight@0: return _price.remove_dock; truelight@0: } truelight@0: truelight@0: #include "table/station_land.h" truelight@0: peter1138@3763: const DrawTileSprites *GetStationTileLayout(byte gfx) peter1138@3763: { peter1138@3763: return &_station_display_datas[gfx]; peter1138@3763: } truelight@0: truelight@0: static void DrawTile_Station(TileInfo *ti) truelight@0: { tron@449: uint32 image; truelight@0: const DrawTileSeqStruct *dtss; tron@449: const DrawTileSprites *t = NULL; tron@3242: RailType railtype = GetRailType(ti->tile); celestar@2254: const RailtypeInfo *rti = GetRailTypeInfo(railtype); tron@449: uint32 relocation = 0; peter1138@3775: const Station *st = NULL; peter1138@3775: const StationSpec *statspec = NULL; tron@4227: PlayerID owner = GetTileOwner(ti->tile); tron@4227: uint32 palette; tron@4227: Darkvater@4850: if (IsValidPlayer(owner)) { tron@4227: palette = PLAYER_SPRITE_COLOR(owner); tron@4227: } else { tron@4227: // Some stations are not owner by a player, namely oil rigs tron@4227: palette = PALETTE_TO_GREY; truelight@0: } truelight@0: celestar@3334: // don't show foundation for docks tron@3636: if (ti->tileh != SLOPE_FLAT && !IsDock(ti->tile)) truelight@0: DrawFoundation(ti, ti->tileh); truelight@0: peter1138@3568: if (IsCustomStationSpecIndex(ti->tile)) { tron@449: // look for customization peter1138@3775: st = GetStationByTile(ti->tile); peter1138@3775: statspec = st->speclist[GetCustomStationSpecIndex(ti->tile)].spec; tron@449: tron@449: //debug("Cust-o-mized %p", statspec); 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: peter1138@3754: if (HASBIT(statspec->callbackmask, CBM_CUSTOM_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) { peter1138@3754: t = &statspec->renderdata[tile < statspec->tiles ? tile : GetRailStationAxis(ti->tile)]; peter1138@3754: } tron@449: } tron@449: } tron@449: peter1138@3737: if (t == NULL || t->seq == NULL) t = &_station_display_datas[GetStationGfx(ti->tile)]; darkvater@384: darkvater@384: image = t->ground_sprite; peter1138@3775: if (HASBIT(image, 31)) { peter1138@3775: CLRBIT(image, 31); peter1138@3775: image += GetCustomStationGroundRelocation(statspec, st, ti->tile); peter1138@3775: image += rti->custom_ground_offset; peter1138@3775: } else { peter1138@3775: image += rti->total_offset; peter1138@3775: } tron@4227: if (image & PALETTE_MODIFIER_COLOR) image |= palette; tron@449: tron@449: // station_land array has been increased from 82 elements to 114 tron@449: // but this is something else. If AI builds station with 114 it looks all weird tron@449: DrawGroundSprite(image); truelight@0: glx@3789: if (GetRailType(ti->tile) == RAILTYPE_ELECTRIC && IsStationTileElectrifiable(ti->tile)) DrawCatenary(ti); celestar@3355: darkvater@384: foreach_draw_tile_seq(dtss, t->seq) { peter1138@3775: image = dtss->image; peter1138@3775: if (HASBIT(image, 30)) { peter1138@3775: CLRBIT(image, 30); peter1138@3775: image += rti->total_offset; peter1138@3775: } else { peter1138@3775: image += relocation; peter1138@3775: } peter1138@3775: darkvater@384: if (_display_opt & DO_TRANS_BUILDINGS) { celestar@2148: MAKE_TRANSPARENT(image); tron@4227: } else if (image & PALETTE_MODIFIER_COLOR) { tron@4227: image |= palette; darkvater@384: } darkvater@384: truelight@0: if ((byte)dtss->delta_z != 0x80) { tron@4230: AddSortableSpriteToDraw( tron@4230: image, tron@4230: ti->x + dtss->delta_x, ti->y + dtss->delta_y, tron@4230: dtss->size_x, dtss->size_y, tron@4230: dtss->size_z, ti->z + dtss->delta_z tron@4230: ); truelight@0: } else { truelight@0: AddChildSpriteScreen(image, dtss->delta_x, dtss->delta_y); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@2520: void StationPickerDrawSprite(int x, int y, RailType railtype, int image) truelight@0: { truelight@0: uint32 ormod, img; truelight@0: const DrawTileSeqStruct *dtss; darkvater@384: const DrawTileSprites *t; celestar@2254: const RailtypeInfo *rti = GetRailTypeInfo(railtype); truelight@0: truelight@0: ormod = PLAYER_SPRITE_COLOR(_local_player); truelight@0: darkvater@384: t = &_station_display_datas[image]; darkvater@384: darkvater@384: img = t->ground_sprite; tron@2639: if (img & PALETTE_MODIFIER_COLOR) img |= ormod; celestar@2254: DrawSprite(img + rti->total_offset, x, y); truelight@0: darkvater@384: foreach_draw_tile_seq(dtss, t->seq) { truelight@0: Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z); celestar@2254: DrawSprite((dtss->image | ormod) + rti->total_offset, 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: tron@3636: static Slope GetSlopeTileh_Station(TileIndex tile, Slope tileh) dominik@39: { tron@3636: return SLOPE_FLAT; 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: { truelight@0: StringID str; truelight@0: tron@1901: td->owner = GetTileOwner(tile); tron@3315: td->build_date = GetStationByTile(tile)->build_date; tron@2049: 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: tron@1977: static uint32 GetTileTrackStatus_Station(TileIndex tile, TransportType mode) tron@1977: { tron@1032: switch (mode) { tron@1032: case TRANSPORT_RAIL: tron@4077: if (IsRailwayStation(tile) && !IsStationTileBlocked(tile)) { celestar@3334: return TrackToTrackBits(GetRailStationTrack(tile)) * 0x101; tron@1032: } tron@1032: break; tron@1032: tron@1032: case TRANSPORT_WATER: tron@1032: // buoy is coded as a station, it is always on open water tron@4077: if (IsBuoy_(tile)) return TRACK_BIT_ALL * 0x101; tron@1032: break; tron@1032: KUDr@3900: case TRANSPORT_ROAD: tron@4077: if (IsRoadStopTile(tile)) { tron@4158: return AxisToTrackBits(DiagDirToAxis(GetRoadStopDir(tile))) * 0x101; tron@4077: } KUDr@3900: break; KUDr@3900: tron@1032: default: tron@1032: break; truelight@0: } tron@1032: celestar@3334: return 0; truelight@0: } truelight@0: tron@2660: tron@1977: static void TileLoop_Station(TileIndex tile) truelight@0: { tron@2660: // FIXME -- GetTileTrackStatus_Station -> animated stationtiles tron@2660: // hardcoded.....not good belugas@3540: switch (GetStationGfx(tile)) { belugas@3545: case GFX_RADAR_LARGE_FIRST: belugas@3545: case GFX_WINDSACK_FIRST : // for small airport belugas@3554: case GFX_RADAR_INTERNATIONAL_FIRST: belugas@3554: case GFX_RADAR_METROPOLITAN_FIRST: richk@4059: case GFX_RADAR_DISTRICTWE_FIRST: // radar district W-E airport richk@4066: case GFX_WINDSACK_INTERCON_FIRST : // for intercontinental airport tron@2660: AddAnimatedTile(tile); tron@2660: break; tron@2660: belugas@3545: case GFX_OILRIG_BASE: //(station part) belugas@3545: case GFX_BUOY_BASE: 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: { tron@4467: typedef struct AnimData { tron@4467: StationGfx from; // first sprite tron@4467: StationGfx to; // last sprite tron@4467: byte delay; tron@4467: } AnimData; 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: belugas@3545: StationGfx gfx = GetStationGfx(tile); tron@4467: const AnimData* i; tron@4467: tron@4467: for (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)) { bjarni@4638: ShowDepotWindow(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: tron@1977: static uint32 VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y) truelight@0: { truelight@0: if (v->type == VEH_Train) { celestar@3334: if (IsRailwayStation(tile) && IsFrontEngine(v) && Darkvater@4559: !IsCompatibleTrainStationTile(tile + TileOffsByDiagDir(DirToDiagDir(v->direction)), tile)) { tron@3315: StationID station_id = GetStationIndex(tile); tron@2989: tron@555: if ((!(v->current_order.flags & OF_NON_STOP) && !_patches.new_nonstop) || tron@4527: (v->current_order.type == OT_GOTO_STATION && v->current_order.dest == station_id)) { tron@555: if (!(_patches.new_nonstop && v->current_order.flags & OF_NON_STOP) && tron@555: v->current_order.type != OT_LEAVESTATION && tron@555: v->last_station_visited != station_id) { tron@3482: DiagDirection dir = DirToDiagDir(v->direction); tron@2989: truelight@0: x &= 0xF; truelight@0: y &= 0xF; truelight@193: tron@3482: if (DiagDirToAxis(dir) != AXIS_X) intswap(x, y); tron@3645: if (y == TILE_SIZE / 2) { tron@3645: if (dir != DIAGDIR_SE && dir != DIAGDIR_SW) x = TILE_SIZE - 1 - x; truelight@0: if (x == 12) return 2 | (station_id << 8); /* enter station */ truelight@0: if (x < 12) { tron@2639: uint16 spd; tron@2639: truelight@0: v->vehstatus |= VS_TRAIN_SLOWING; truelight@0: spd = _enter_station_speedtable[x]; tron@2639: if (spd < v->cur_speed) v->cur_speed = spd; truelight@0: } truelight@0: } truelight@0: } truelight@193: } truelight@0: } truelight@0: } else if (v->type == VEH_Road) { peter1138@2671: if (v->u.road.state < 16 && !HASBIT(v->u.road.state, 2) && v->u.road.frame == 0) { celestar@3334: if (IsRoadStop(tile)) { peter1138@2671: /* Attempt to allocate a parking bay in a road stop */ celestar@1217: RoadStop *rs = GetRoadStopByTile(tile, GetRoadStopType(tile)); peter1138@2671: peter1138@2671: /* rs->status bits 0 and 1 describe current the two parking spots. peter1138@2671: * 0 means occupied, 1 means free. */ peter1138@2671: peter1138@2671: // Check if station is busy or if there are no free bays. peter1138@2671: if (HASBIT(rs->status, 7) || GB(rs->status, 0, 2) == 0) truelight@0: return 8; truelight@0: peter1138@2671: v->u.road.state += 32; peter1138@2671: peter1138@2671: // if the first bay is free, allocate that, else the second bay must be free. peter1138@2671: if (HASBIT(rs->status, 0)) { peter1138@2671: CLRBIT(rs->status, 0); truelight@0: } else { peter1138@2671: CLRBIT(rs->status, 1); peter1138@2671: v->u.road.state += 2; truelight@0: } celestar@1217: peter1138@2671: // mark the station as busy peter1138@2671: SETBIT(rs->status, 7); truelight@0: } truelight@0: } truelight@0: } truelight@193: truelight@0: return 0; truelight@0: } truelight@0: truelight@4398: /** truelight@4398: * Cleanup a RoadStop. Make sure no vehicles try to go to this roadstop. truelight@4398: */ truelight@4398: void DestroyRoadStop(RoadStop* rs) truelight@4398: { truelight@4398: Vehicle *v; truelight@4398: truelight@4398: /* Clear the slot assignment of all vehicles heading for this road stop */ truelight@4398: if (rs->num_vehicles != 0) { truelight@4398: FOR_ALL_VEHICLES(v) { truelight@4398: if (v->type == VEH_Road && v->u.road.slot == rs) { truelight@4398: ClearSlot(v); truelight@4398: } truelight@4398: } truelight@4398: } truelight@4398: assert(rs->num_vehicles == 0); truelight@4398: truelight@4398: if (rs->prev != NULL) rs->prev->next = rs->next; truelight@4398: if (rs->next != NULL) rs->next->prev = rs->prev; truelight@4398: } truelight@4398: truelight@4398: /** rubidium@4549: * Clean up a station by clearing vehicle orders and invalidating windows. rubidium@4549: * Aircraft-Hangar orders need special treatment here, as the hangars are rubidium@4549: * actually part of a station (tiletype is STATION), but the order type rubidium@4549: * is OT_GOTO_DEPOT. rubidium@4549: * @param st Station to be deleted rubidium@4549: */ truelight@4398: void DestroyStation(Station *st) truelight@0: { celestar@1551: StationID index; truelight@4398: truelight@4398: index = st->index; truelight@0: truelight@0: DeleteName(st->string_id); truelight@0: MarkStationDirty(st); celestar@3812: RebuildStationLists(); darkvater@65: InvalidateWindowClasses(WC_STATION_LIST); truelight@0: truelight@0: DeleteWindowById(WC_STATION_VIEW, index); truelight@1024: truelight@4351: /* Now delete all orders that go to the station */ tron@4527: RemoveOrderFromAllVehicles(OT_GOTO_STATION, index); truelight@1024: celestar@2213: //Subsidies need removal as well truelight@0: DeleteSubsidyWithStation(index); truelight@4398: truelight@4398: free(st->speclist); truelight@0: } truelight@0: tron@1093: void DeleteAllPlayerStations(void) truelight@0: { truelight@0: Station *st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { Darkvater@4850: if (IsValidPlayer(st->owner)) DeleteStation(st); truelight@0: } 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: tron@2639: if (st->facilities == 0 && ++st->delete_ctr >= 8) DeleteStation(st); celestar@1239: truelight@0: } truelight@0: tron@500: static inline void byte_inc_sat(byte *p) { byte b = *p + 1; if (b != 0) *p = b; } truelight@0: truelight@0: static void UpdateStationRating(Station *st) truelight@0: { truelight@0: GoodsEntry *ge; celestar@1551: int rating; celestar@1551: StationID index; truelight@0: int waiting; 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: truelight@0: ge = st->goods; truelight@0: do { truelight@1266: if (ge->enroute_from != INVALID_STATION) { truelight@0: byte_inc_sat(&ge->enroute_time); truelight@0: byte_inc_sat(&ge->days_since_pickup); truelight@0: truelight@0: rating = 0; truelight@0: truelight@0: { truelight@0: int b = ge->last_speed; truelight@0: if ((b-=85) >= 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: Darkvater@4850: if (IsValidPlayer(st->owner) && HASBIT(st->town->statues, st->owner)) rating += 26; truelight@0: truelight@0: { truelight@0: byte days = ge->days_since_pickup; celestar@3580: if (st->last_vehicle_type == VEH_Ship) truelight@0: 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: truelight@0: { tron@2504: waiting = GB(ge->waiting_acceptance, 0, 12); truelight@0: (rating -= 90, waiting > 1500) || truelight@0: (rating += 55, waiting > 1000) || truelight@0: (rating += 35, waiting > 600) || truelight@0: (rating += 10, waiting > 300) || truelight@0: (rating += 20, waiting > 100) || truelight@0: (rating += 10, true); truelight@0: } truelight@0: truelight@0: { truelight@0: int or = ge->rating; // old rating truelight@0: truelight@0: // only modify rating in steps of -2, -1, 0, 1 or 2 truelight@0: ge->rating = rating = or + clamp(clamp(rating, 0, 255) - or, -2, 2); truelight@193: truelight@0: // if rating is <= 64 and more than 200 items waiting, 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: truelight@0: // 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(); truelight@0: if ( (uint)rating <= (r & 0x7F) ) { truelight@0: waiting = max(waiting - ((r >> 8)&3) - 1, 0); truelight@0: waiting_changed = true; truelight@0: } truelight@0: } truelight@0: tron@2504: if (waiting_changed) SB(ge->waiting_acceptance, 0, 12, waiting); truelight@0: } truelight@0: } truelight@0: } while (++ge != endof(st->goods)); truelight@193: truelight@0: index = st->index; truelight@0: tron@4077: if (waiting_changed) { truelight@0: InvalidateWindow(WC_STATION_VIEW, index); tron@4077: } else { truelight@0: InvalidateWindowWidget(WC_STATION_VIEW, index, 5); tron@4077: } truelight@0: } truelight@0: truelight@0: /* called for every station each tick */ truelight@0: static void StationHandleSmallTick(Station *st) truelight@0: { truelight@0: byte b; truelight@0: tron@2639: if (st->facilities == 0) return; truelight@0: truelight@0: 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: tron@1093: void OnTick_Station(void) truelight@0: { truelight@1272: uint i; truelight@0: Station *st; truelight@0: tron@2639: if (_game_mode == GM_EDITOR) return; truelight@0: truelight@0: 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: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: StationHandleSmallTick(st); truelight@0: } truelight@0: } truelight@0: tron@1093: void StationMonthlyLoop(void) truelight@0: { 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) { tron@2549: uint i; tron@2549: tron@2549: for (i = 0; i != NUM_CARGO; i++) { tron@2549: GoodsEntry* ge = &st->goods[i]; tron@2549: truelight@1266: if (ge->enroute_from != INVALID_STATION) { truelight@0: ge->rating = clamp(ge->rating + amount, 0, 255); truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void UpdateStationWaiting(Station *st, int type, uint amount) truelight@0: { tron@2504: SB(st->goods[type].waiting_acceptance, 0, 12, tron@2504: min(0xFFF, GB(st->goods[type].waiting_acceptance, 0, 12) + amount) tron@2504: ); truelight@0: truelight@0: st->goods[type].enroute_time = 0; truelight@0: st->goods[type].enroute_from = st->index; truelight@0: InvalidateWindow(WC_STATION_VIEW, st->index); peter1138@5220: MarkStationTilesDirty(st); truelight@0: } truelight@0: Darkvater@1786: /** Rename a station tron@3491: * @param tile unused Darkvater@1786: * @param p1 station ID that is to be renamed Darkvater@1786: * @param p2 unused Darkvater@1786: */ tron@3491: int32 CmdRenameStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { tron@2639: StringID str; truelight@0: Station *st; truelight@0: truelight@4352: if (!IsValidStationID(p1) || _cmd_text[0] == '\0') return CMD_ERROR; tron@1774: st = GetStation(p1); tron@1774: truelight@4352: if (!CheckOwnership(st->owner)) return CMD_ERROR; tron@1774: tron@1820: str = AllocateNameUnique(_cmd_text, 6); Darkvater@1786: if (str == 0) return CMD_ERROR; truelight@193: truelight@0: if (flags & DC_EXEC) { tron@2639: StringID old_str = st->string_id; tron@2639: truelight@0: st->string_id = str; truelight@0: UpdateStationVirtCoord(st); truelight@0: DeleteName(old_str); celestar@3812: ResortStationLists(); truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: tron@1977: uint MoveGoodsToStation(TileIndex tile, int w, int h, int type, uint amount) truelight@0: { tron@3002: Station* around[8]; tron@3002: uint i; truelight@0: uint moved; truelight@0: uint best_rating, best_rating2; truelight@0: Station *st1, *st2; tron@3000: uint t; tron@3000: int rad = 0; tron@3000: int w_prod; //width and height of the "producer" of the cargo tron@3000: int h_prod; Celestar@568: int max_rad; Celestar@568: tron@3002: for (i = 0; i < lengthof(around); i++) around[i] = NULL; truelight@0: Celestar@568: if (_patches.modified_catchment) { Celestar@568: w_prod = w; Celestar@568: h_prod = h; Celestar@568: w += 16; Celestar@568: h += 16; Celestar@568: max_rad = 8; Celestar@568: } else { tron@3000: w_prod = 0; tron@3000: h_prod = 0; tron@3000: w += 8; tron@3000: h += 8; Celestar@568: max_rad = 4; Celestar@568: } Celestar@568: tron@1981: BEGIN_TILE_LOOP(cur_tile, w, h, tile - TileDiffXY(max_rad, max_rad)) tron@3002: Station* st; tron@3000: truelight@0: cur_tile = TILE_MASK(cur_tile); tron@3000: if (!IsTileType(cur_tile, MP_STATION)) continue; tron@3000: tron@3315: st = GetStationByTile(cur_tile); tron@3002: tron@3002: for (i = 0; i != lengthof(around); i++) { tron@3002: if (around[i] == NULL) { tron@3000: if (!IsBuoy(st) && tron@3000: (st->town->exclusive_counter == 0 || st->town->exclusivity == st->owner) && // check exclusive transport rights tron@3000: st->goods[type].rating != 0 && tron@3000: (!_patches.selectgoods || st->goods[type].last_speed > 0) && // if last_speed is 0, no vehicle has been there. tron@3000: ((st->facilities & ~FACIL_BUS_STOP) != 0 || type == CT_PASSENGERS) && // if we have other fac. than a bus stop, or the cargo is passengers tron@3000: ((st->facilities & ~FACIL_TRUCK_STOP) != 0 || type != CT_PASSENGERS)) { // if we have other fac. than a cargo bay or the cargo is not passengers tron@3001: int x_dist; tron@3001: int y_dist; tron@3001: tron@3001: if (_patches.modified_catchment) { tron@3001: // min and max coordinates of the producer relative tron@3001: const int x_min_prod = 9; tron@3001: const int x_max_prod = 8 + w_prod; tron@3001: const int y_min_prod = 9; tron@3001: const int y_max_prod = 8 + h_prod; tron@3001: tron@3001: rad = FindCatchmentRadius(st); tron@3001: tron@3001: x_dist = min(w_cur - x_min_prod, x_max_prod - w_cur); tron@3001: if (w_cur < x_min_prod) { tron@3001: x_dist = x_min_prod - w_cur; tron@3001: } else if (w_cur > x_max_prod) { tron@3001: x_dist = w_cur - x_max_prod; tron@3000: } tron@3001: tron@3001: y_dist = min(h_cur - y_min_prod, y_max_prod - h_cur); tron@3001: if (h_cur < y_min_prod) { tron@3001: y_dist = y_min_prod - h_cur; tron@3001: } else if (h_cur > y_max_prod) { tron@3001: y_dist = h_cur - y_max_prod; tron@3001: } tron@3001: } else { tron@3001: x_dist = 0; tron@3001: y_dist = 0; tron@3001: } tron@3001: tron@3002: if (x_dist <= rad && y_dist <= rad) around[i] = st; tron@3001: } tron@3000: break; tron@3002: } else if (around[i] == st) { tron@3000: break; truelight@0: } truelight@0: } tron@1981: END_TILE_LOOP(cur_tile, w, h, tile - TileDiffXY(max_rad, max_rad)) truelight@0: truelight@0: /* no stations around at all? */ tron@3002: if (around[0] == NULL) return 0; tron@3002: tron@3002: if (around[1] == NULL) { truelight@0: /* only one station around */ tron@3002: moved = (amount * around[0]->goods[type].rating >> 8) + 1; tron@3002: UpdateStationWaiting(around[0], type, moved); truelight@0: return moved; truelight@0: } truelight@0: truelight@0: /* several stations around, find the two with the highest rating */ truelight@0: st2 = st1 = NULL; truelight@0: best_rating = best_rating2 = 0; celestar@1217: tron@3002: for (i = 0; i != lengthof(around) && around[i] != NULL; i++) { tron@3002: if (around[i]->goods[type].rating >= best_rating) { truelight@0: best_rating2 = best_rating; truelight@0: st2 = st1; truelight@0: tron@3002: best_rating = around[i]->goods[type].rating; tron@3002: st1 = around[i]; tron@3002: } else if (around[i]->goods[type].rating >= best_rating2) { tron@3002: best_rating2 = around[i]->goods[type].rating; tron@3002: st2 = around[i]; truelight@0: } truelight@0: } truelight@193: truelight@0: assert(st1 != NULL); truelight@0: assert(st2 != NULL); truelight@0: assert(best_rating != 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 */ truelight@0: t = (best_rating * (amount + 1)) / (best_rating + best_rating2); truelight@0: truelight@0: moved = 0; truelight@0: if (t != 0) { tron@3000: moved = t * best_rating / 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: { tron@2639: uint j; Darkvater@2427: Station *st = AllocateStation(); 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: if (!GenerateStationName(st, tile, 2)) { Darkvater@5568: DEBUG(misc, 0, "Can't allocate station-name for oilrig at 0x%X, reverting to oilrig only", tile); Darkvater@2427: return; Darkvater@2427: } Darkvater@2427: Darkvater@2427: st->town = ClosestTownFromTile(tile, (uint)-1); Darkvater@2427: st->sign.width_1 = 0; Darkvater@2427: celestar@3334: MakeOilrig(tile, st->index); Darkvater@2427: Darkvater@2427: st->owner = OWNER_NONE; Darkvater@2427: st->airport_flags = 0; Darkvater@2427: st->airport_type = AT_OILRIG; 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; celestar@3580: st->last_vehicle_type = VEH_Invalid; Darkvater@2427: st->facilities = FACIL_AIRPORT | FACIL_DOCK; Darkvater@2427: st->build_date = _date; Darkvater@2427: Darkvater@2427: for (j = 0; j != NUM_CARGO; j++) { Darkvater@2427: st->goods[j].waiting_acceptance = 0; Darkvater@2427: st->goods[j].days_since_pickup = 0; Darkvater@2427: st->goods[j].enroute_from = INVALID_STATION; Darkvater@2427: st->goods[j].rating = 175; 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: { tron@3315: Station* st = GetStationByTile(tile); truelight@0: truelight@0: DoClearSquare(tile); truelight@0: truelight@0: st->dock_tile = 0; truelight@0: st->airport_tile = 0; truelight@0: st->facilities &= ~(FACIL_AIRPORT | FACIL_DOCK); truelight@0: st->airport_flags = 0; truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStation(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) { tron@3315: Station* st = GetStationByTile(tile); tron@3315: tron@1902: SetTileOwner(tile, new_player); truelight@0: st->owner = new_player; celestar@3812: RebuildStationLists(); darkvater@65: InvalidateWindowClasses(WC_STATION_LIST); truelight@0: } else { tron@3491: DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } truelight@0: } truelight@0: tron@1977: static int32 ClearTile_Station(TileIndex tile, byte flags) tron@1977: { truelight@0: Station *st; truelight@0: 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); celestar@3334: case STATION_TRUCK: return_cmd_error(STR_3047_MUST_DEMOLISH_TRUCK_STATION); celestar@3334: case STATION_BUS: return_cmd_error(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@3315: 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: celestar@3334: case STATION_BUS: 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: tron@1093: void InitializeStations(void) truelight@0: { truelight@1272: /* Clean the station pool and create 1 block in it */ tron@4980: CleanPool(&_Station_pool); tron@4980: AddBlockToPool(&_Station_pool); truelight@919: truelight@1284: /* Clean the roadstop pool and create 1 block in it */ tron@4981: CleanPool(&_RoadStop_pool); tron@4981: AddBlockToPool(&_RoadStop_pool); darkvater@243: truelight@0: _station_tick_ctr = 0; darkvater@243: truelight@0: } truelight@0: truelight@0: peter1138@3765: void AfterLoadStations(void) peter1138@3765: { peter1138@3765: Station *st; peter1138@3765: uint i; KUDr@5583: TileIndex tile; peter1138@3765: peter1138@3765: /* Update the speclists of all stations to point to the currently loaded custom stations. */ peter1138@3765: FOR_ALL_STATIONS(st) { peter1138@3765: for (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: } peter1138@3765: } KUDr@5583: KUDr@5583: for (tile = 0; tile < MapSize(); tile++) { KUDr@5583: if (GetTileType(tile) != MP_STATION) continue; KUDr@5583: st = GetStationByTile(tile); celestar@5592: StationRect_BeforeAddTile(st, tile, RECT_MODE_FORCE); KUDr@5583: } peter1138@3765: } peter1138@3765: peter1138@3765: truelight@0: 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 */ belugas@3554: GetSlopeTileh_Station, /* get_slope_tileh_proc */ truelight@0: }; truelight@0: Darkvater@1881: static const SaveLoad _roadstop_desc[] = { celestar@1217: SLE_VAR(RoadStop,xy, SLE_UINT32), KUDr@3873: SLE_VAR(RoadStop,used, SLE_BOOL), celestar@1217: 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), celestar@1217: SLE_VAR(RoadStop,station, SLE_UINT16), tron@3479: SLE_CONDNULL(1, 0, 25), celestar@1217: celestar@1217: SLE_REF(RoadStop,next, REF_ROADSTOPS), celestar@1217: SLE_REF(RoadStop,prev, REF_ROADSTOPS), 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), rubidium@4344: SLE_CONDVAR(Station, bus_tile_obsolete, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Station, lorry_tile_obsolete, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), 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: truelight@0: // alpha_order was stored here in savegame format 0 - 3 Darkvater@3222: SLE_CONDNULL(1, 0, 3), truelight@0: rubidium@4344: SLE_VAR(Station, string_id, SLE_STRINGID), 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: celestar@1217: // truck/bus_stop_status was stored here in savegame format 0 - 6 rubidium@4344: SLE_CONDVAR(Station, truck_stop_status_obsolete, SLE_UINT8, 0, 5), rubidium@4344: SLE_CONDVAR(Station, bus_stop_status_obsolete, SLE_UINT8, 0, 5), truelight@193: dominik@123: // blocked_months was stored here in savegame format 0 - 4.0 rubidium@4344: SLE_CONDVAR(Station, blocked_months_obsolete, SLE_UINT8, 0, 4), rubidium@4344: rubidium@4344: SLE_CONDVAR(Station, airport_flags, SLE_VAR_U32 | SLE_FILE_U16, 0, 2), rubidium@4344: SLE_CONDVAR(Station, airport_flags, SLE_UINT32, 3, SL_MAX_VERSION), belugas@3554: celestar@3580: SLE_CONDNULL(2, 0, 25), /* Ex last-vehicle */ rubidium@4344: SLE_CONDVAR(Station, last_vehicle_type, SLE_UINT8, 26, SL_MAX_VERSION), belugas@3554: peter1138@3574: // Was custom station class and id peter1138@3574: SLE_CONDNULL(2, 3, 25), 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), peter1138@3687: Darkvater@3222: // 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: Darkvater@1881: static const SaveLoad _goods_desc[] = { rubidium@4344: SLE_VAR(GoodsEntry, waiting_acceptance, SLE_UINT16), rubidium@4344: SLE_VAR(GoodsEntry, days_since_pickup, SLE_UINT8), rubidium@4344: SLE_VAR(GoodsEntry, rating, SLE_UINT8), rubidium@4344: SLE_CONDVAR(GoodsEntry, enroute_from, SLE_FILE_U8 | SLE_VAR_U16, 0, 6), rubidium@4344: SLE_CONDVAR(GoodsEntry, enroute_from, SLE_UINT16, 7, SL_MAX_VERSION), rubidium@4344: SLE_VAR(GoodsEntry, enroute_time, SLE_UINT8), rubidium@4344: SLE_VAR(GoodsEntry, last_speed, SLE_UINT8), rubidium@4344: SLE_VAR(GoodsEntry, last_age, SLE_UINT8), rubidium@4344: SLE_CONDVAR(GoodsEntry, feeder_profit, SLE_INT32, 14, SL_MAX_VERSION), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: 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: truelight@0: truelight@0: static void SaveLoad_STNS(Station *st) truelight@0: { peter1138@3765: uint i; celestar@1217: truelight@0: SlObject(st, _station_desc); truelight@1266: for (i = 0; i != NUM_CARGO; i++) { truelight@0: SlObject(&st->goods[i], _goods_desc); truelight@1266: truelight@1266: /* In older versions, enroute_from had 0xFF as INVALID_STATION, is now 0xFFFF */ truelight@2685: if (CheckSavegameVersion(7) && st->goods[i].enroute_from == 0xFF) { tron@2469: st->goods[i].enroute_from = INVALID_STATION; tron@2639: } truelight@1266: } peter1138@3765: peter1138@3765: if (st->num_specs != 0) { peter1138@3765: /* Allocate speclist memory when loading a game */ peter1138@3765: if (st->speclist == NULL) st->speclist = calloc(st->num_specs, sizeof(*st->speclist)); peter1138@3765: for (i = 0; i < st->num_specs; i++) SlObject(&st->speclist[i], _station_speclist_desc); peter1138@3765: } truelight@0: } truelight@0: tron@1093: static void Save_STNS(void) truelight@0: { truelight@0: Station *st; truelight@919: // 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: tron@1093: static void Load_STNS(void) truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { truelight@1272: Station *st; truelight@1272: tron@4980: if (!AddBlockIfNeeded(&_Station_pool, index)) truelight@1272: error("Stations: failed loading savegame: too many stations"); truelight@1272: truelight@1272: st = GetStation(index); truelight@0: SaveLoad_STNS(st); truelight@0: truelight@0: // 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: celestar@3334: if (GetRailStationAxis(st->train_tile) == AXIS_Y) uintswap(w, h); truelight@0: st->trainst_w = w; truelight@0: st->trainst_h = h; truelight@0: } celestar@1217: truelight@2685: /* In older versions, we had just 1 tile for a bus/lorry, now we have more.. truelight@2685: * convert, if needed */ truelight@2685: if (CheckSavegameVersion(6)) { celestar@1217: if (st->bus_tile_obsolete != 0) { truelight@1284: st->bus_stops = AllocateRoadStop(); truelight@1284: if (st->bus_stops == NULL) celestar@1217: error("Station: too many busstations in savegame"); celestar@1217: truelight@1284: InitializeRoadStop(st->bus_stops, NULL, st->bus_tile_obsolete, st->index); celestar@1217: } celestar@1217: if (st->lorry_tile_obsolete != 0) { truelight@1284: st->truck_stops = AllocateRoadStop(); truelight@1284: if (st->truck_stops == NULL) celestar@1217: error("Station: too many truckstations in savegame"); celestar@1217: truelight@1284: InitializeRoadStop(st->truck_stops, NULL, st->lorry_tile_obsolete, st->index); celestar@1217: } celestar@1217: } 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: tron@2549: static void Save_ROADSTOP(void) 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: tron@2549: static void Load_ROADSTOP(void) celestar@1217: { celestar@1217: int index; celestar@3475: Vehicle *v; celestar@1217: truelight@1284: while ((index = SlIterateArray()) != -1) { truelight@1284: RoadStop *rs; truelight@1284: tron@4981: if (!AddBlockIfNeeded(&_RoadStop_pool, index)) truelight@1314: error("RoadStops: failed loading savegame: too many RoadStops"); truelight@1284: truelight@1284: rs = GetRoadStop(index); truelight@1284: SlObject(rs, _roadstop_desc); truelight@1284: } celestar@3475: celestar@3475: FOR_ALL_VEHICLES(v) { tron@3480: if (v->type == VEH_Road && v->u.road.slot != NULL) v->u.road.slot->num_vehicles++; celestar@3475: } celestar@1217: } celestar@1217: truelight@0: 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: }; KUDr@5583: KUDr@5583: KUDr@5587: static inline bool PtInRectXY(Rect *r, int x, int y) KUDr@5587: { KUDr@5587: return (r->left <= x && x <= r->right && r->top <= y && y <= r->bottom); KUDr@5587: } KUDr@5583: KUDr@5583: static void StationRect_Init(Station *st) KUDr@5583: { KUDr@5587: Rect *r = &st->rect; KUDr@5583: r->left = r->top = r->right = r->bottom = 0; KUDr@5583: } KUDr@5583: KUDr@5583: static bool StationRect_IsEmpty(Station *st) KUDr@5583: { KUDr@5583: return (st->rect.left == 0 || st->rect.left > st->rect.right || st->rect.top > st->rect.bottom); KUDr@5583: } KUDr@5583: celestar@5592: static bool StationRect_BeforeAddTile(Station *st, TileIndex tile, StationRectMode mode) KUDr@5583: { KUDr@5587: Rect *r = &st->rect; KUDr@5587: int x = TileX(tile); KUDr@5587: int y = TileY(tile); KUDr@5583: if (StationRect_IsEmpty(st)) { KUDr@5583: // we are adding the first station tile KUDr@5583: r->left = r->right = x; KUDr@5583: r->top = r->bottom = y; KUDr@5587: } else if (!PtInRectXY(r, x, y)) { KUDr@5583: // current rect is not empty and new point is outside this rect KUDr@5583: // make new spread-out rectangle KUDr@5587: Rect new_rect = {min(x, r->left), min(y, r->top), max(x, r->right), max(y, r->bottom)}; KUDr@5583: // check new rect dimensions against preset max KUDr@5587: int w = new_rect.right - new_rect.left + 1; KUDr@5587: int h = new_rect.bottom - new_rect.top + 1; celestar@5592: if (mode != RECT_MODE_FORCE && (w > _patches.station_spread || h > _patches.station_spread)) { celestar@5592: assert(mode != RECT_MODE_TRY); KUDr@5583: _error_message = STR_306C_STATION_TOO_SPREAD_OUT; KUDr@5583: return false; KUDr@5583: } KUDr@5583: // spread-out ok, return true celestar@5592: if (mode != RECT_MODE_TEST) { KUDr@5583: // we should update the station rect KUDr@5583: *r = new_rect; KUDr@5583: } KUDr@5583: } else { KUDr@5583: ; // new point is inside the rect, we don't need to do anything KUDr@5583: } KUDr@5583: return true; KUDr@5583: } KUDr@5583: celestar@5592: static bool StationRect_BeforeAddRect(Station *st, TileIndex tile, int w, int h, StationRectMode mode) KUDr@5583: { celestar@5592: return StationRect_BeforeAddTile(st, tile, mode) && StationRect_BeforeAddTile(st, TILE_ADDXY(tile, w - 1, h - 1), mode); KUDr@5583: } KUDr@5583: KUDr@5587: static inline bool ScanRectForStationTiles(StationID st_id, int left, int top, int right, int bottom) KUDr@5583: { KUDr@5583: TileIndex top_left = TileXY(left, top); KUDr@5587: int width = right - left + 1; KUDr@5587: int height = bottom - top + 1; KUDr@5583: BEGIN_TILE_LOOP(tile, width, height, top_left) KUDr@5583: if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true; KUDr@5583: END_TILE_LOOP(tile, width, height, top_left); KUDr@5583: return false; KUDr@5583: } KUDr@5583: KUDr@5583: static bool StationRect_AfterRemoveTile(Station *st, TileIndex tile) KUDr@5583: { KUDr@5587: Rect *r = &st->rect; KUDr@5587: int x = TileX(tile); KUDr@5587: int y = TileY(tile); KUDr@5583: bool reduce_x, reduce_y; KUDr@5583: KUDr@5583: // look if removed tile was on the bounding rect edge KUDr@5583: // and try to reduce the rect by this edge KUDr@5583: // do it until we have empty rect or nothing to do KUDr@5583: for (;;) { KUDr@5583: // check if removed tile is on rect edge KUDr@5583: bool left_edge = (x == r->left); KUDr@5583: bool right_edge = (x == r->right); KUDr@5583: bool top_edge = (y == r->top); KUDr@5583: bool bottom_edge = (y == r->bottom); KUDr@5583: // can we reduce the rect in either direction? KUDr@5583: reduce_x = ((left_edge || right_edge) && !ScanRectForStationTiles(st->index, x, r->top, x, r->bottom)); KUDr@5583: reduce_y = ((top_edge || bottom_edge) && !ScanRectForStationTiles(st->index, r->left, y, r->right, y)); KUDr@5583: if (!(reduce_x || reduce_y)) break; // nothing to do (can't reduce) KUDr@5583: if (reduce_x) { KUDr@5583: // reduce horizontally KUDr@5583: if (left_edge) { KUDr@5583: // move left edge right KUDr@5583: r->left = x = x + 1; KUDr@5583: } else { KUDr@5583: // move right edge left KUDr@5583: r->right = x = x - 1; KUDr@5583: } KUDr@5583: } KUDr@5583: if (reduce_y) { KUDr@5583: // reduce vertically KUDr@5583: if (top_edge) { KUDr@5583: // move top edge down KUDr@5583: r->top = y = y + 1; KUDr@5583: } else { KUDr@5583: // move bottom edge up KUDr@5583: r->bottom = y = y - 1; KUDr@5583: } KUDr@5583: } KUDr@5583: if (r->left > r->right || r->top > r->bottom) { KUDr@5583: // can't continue, if the remaining rectangle is empty KUDr@5583: StationRect_Init(st); KUDr@5583: return true; // empty remaining rect KUDr@5583: } KUDr@5583: } KUDr@5583: return false; // non-empty remaining rect KUDr@5583: } KUDr@5583: KUDr@5587: static bool StationRect_AfterRemoveRect(Station *st, TileIndex tile, int w, int h) KUDr@5583: { KUDr@5583: bool empty; KUDr@5587: assert(PtInRectXY(&st->rect, TileX(tile), TileY(tile))); KUDr@5587: assert(PtInRectXY(&st->rect, TileX(tile) + w - 1, TileY(tile) + h - 1)); KUDr@5583: empty = StationRect_AfterRemoveTile(st, tile); KUDr@5583: if (w != 1 || h != 1) empty = empty || StationRect_AfterRemoveTile(st, TILE_ADDXY(tile, w - 1, h - 1)); KUDr@5583: return empty; KUDr@5583: }