truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@1299: #include "debug.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" matthijs@1247: #include "npf.h" truelight@1313: #include "depot.h" truelight@0: truelight@1272: enum { truelight@1272: /* Max stations: 64000 (64 * 1000) */ truelight@1272: STATION_POOL_BLOCK_SIZE_BITS = 6, /* In bits, so (1 << 6) == 64 */ truelight@1272: STATION_POOL_MAX_BLOCKS = 1000, truelight@1284: truelight@1284: /* Max roadstops: 64000 (32 * 2000) */ truelight@1284: ROADSTOP_POOL_BLOCK_SIZE_BITS = 5, /* In bits, so (1 << 5) == 32 */ truelight@1284: ROADSTOP_POOL_MAX_BLOCKS = 2000, truelight@1272: }; truelight@1272: 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@1272: FOR_ALL_STATIONS_FROM(st, start_item) truelight@1272: st->index = start_item++; truelight@1272: } truelight@1272: 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@1284: FOR_ALL_ROADSTOPS_FROM(rs, start_item) truelight@1284: rs->index = start_item++; truelight@1284: } truelight@1284: truelight@1284: /* Initialize the station-pool and roadstop-pool */ truelight@1272: MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, 0, 0, NULL }; truelight@1284: MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, 0, 0, NULL }; truelight@1272: truelight@1272: truelight@0: // FIXME -- need to be embedded into Airport variable. Is dynamically truelight@0: // deducteable from graphics-tile array, so will not be needed tron@1417: const byte _airport_size_x[] = {4, 6, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; tron@1417: const byte _airport_size_y[] = {3, 6, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; truelight@0: truelight@0: void ShowAircraftDepotWindow(uint tile); truelight@0: extern void UpdateAirplanesOnNewStation(Station *st); truelight@0: truelight@0: static void MarkStationDirty(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->slot[0] = road_stop->slot[1] = INVALID_SLOT; celestar@1217: road_stop->next = NULL; celestar@1217: road_stop->prev = previous; celestar@1217: road_stop->station = index; celestar@1217: } celestar@1217: celestar@1217: RoadStop * GetPrimaryRoadStop(const Station *st, RoadStopType type) celestar@1217: { celestar@1217: switch (type) { celestar@1217: case RS_BUS: return st->bus_stops; celestar@1217: case RS_TRUCK: return st->truck_stops; celestar@1217: default: celestar@1217: NOT_REACHED(); celestar@1217: } celestar@1217: celestar@1217: return NULL; celestar@1217: } celestar@1217: celestar@1217: RoadStop * GetRoadStopByTile(TileIndex tile, RoadStopType type) celestar@1217: { celestar@1217: const Station *st = GetStation(_map2[tile]); celestar@1217: RoadStop *rs; celestar@1217: celestar@1217: for ( rs = GetPrimaryRoadStop(st, type); rs->xy != tile; rs = rs->next) celestar@1217: assert(rs->next != NULL); celestar@1217: celestar@1217: return rs; celestar@1217: } celestar@1217: celestar@1217: uint GetNumRoadStops(const Station *st, RoadStopType type) celestar@1217: { celestar@1217: int num = 0; celestar@1217: const RoadStop *rs; celestar@1217: celestar@1217: assert(st != NULL); celestar@1217: for ( rs = GetPrimaryRoadStop(st, type); rs != NULL; num++, rs = rs->next); celestar@1217: celestar@1217: return num; celestar@1217: } celestar@1217: truelight@1284: RoadStop *AllocateRoadStop( void ) celestar@1217: { truelight@1284: RoadStop *rs; truelight@1284: truelight@1284: FOR_ALL_ROADSTOPS(rs) { celestar@1217: if (!rs->used) { truelight@1284: uint index = rs->index; truelight@1284: truelight@1284: memset(rs, 0, sizeof(RoadStop)); 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 */ truelight@1284: if (AddBlockToPool(&_roadstop_pool)) truelight@1284: 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 truelight@703: radius that is available within the station */ truelight@703: static byte FindCatchmentRadius(Station *st) truelight@703: { truelight@703: byte 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; truelight@703: } truelight@703: } truelight@703: truelight@703: return ret; truelight@703: } truelight@703: truelight@0: #define CHECK_STATIONS_ERR ((Station*)-1) truelight@0: truelight@0: static Station *GetStationAround(uint tile, int w, int h, int closest_station) truelight@0: { truelight@0: // check around to see if there's any stations there truelight@0: BEGIN_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TILE_XY(1,1)) tron@1035: if (IsTileType(tile_cur, MP_STATION)) { truelight@0: int t; truelight@0: t = _map2[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: truelight@0: if (closest_station == -1) { 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: } truelight@0: END_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TILE_XY(1,1)) truelight@919: return (closest_station == -1) ? NULL : GetStation(closest_station); truelight@0: } truelight@0: truelight@1024: TileIndex GetStationTileForVehicle(const Vehicle *v, const Station *st) dominik@55: { dominik@55: switch (v->type) { dominik@55: case VEH_Train: return st->train_tile; dominik@55: case VEH_Aircraft: return st->airport_tile; dominik@55: case VEH_Ship: return st->dock_tile; celestar@1231: case VEH_Road: celestar@1231: if (v->cargo_type == CT_PASSENGERS) { celestar@1231: if (st->bus_stops != NULL) celestar@1231: return st->bus_stops->xy; celestar@1231: else celestar@1231: return 0; celestar@1231: } else { celestar@1231: if (st->truck_stops != NULL) celestar@1231: return st->truck_stops->xy; celestar@1231: else celestar@1231: return 0; celestar@1231: } dominik@55: default: dominik@55: assert(false); dominik@55: return 0; dominik@55: } dominik@55: } truelight@0: truelight@0: static bool CheckStationSpreadOut(Station *st, uint tile, int w, int h) truelight@0: { celestar@1551: StationID station_index = st->index; truelight@0: uint i; tron@926: uint x1 = TileX(tile); tron@926: uint y1 = TileY(tile); truelight@0: uint x2 = x1 + w - 1; truelight@0: uint y2 = y1 + h - 1; truelight@0: uint t; truelight@0: tron@863: for (i = 0; i != MapSize(); i++) { tron@1035: if (IsTileType(i, MP_STATION) && _map2[i] == station_index) { tron@926: t = TileX(i); truelight@0: if (t < x1) x1 = t; truelight@0: if (t > x2) x2 = t; truelight@0: tron@926: t = TileY(i); truelight@0: if (t < y1) y1 = t; truelight@0: if (t > y2) y2 = t; truelight@0: } truelight@0: } truelight@0: truelight@0: if (y2-y1 >= _patches.station_spread || x2-x1 >= _patches.station_spread) { truelight@0: _error_message = STR_306C_STATION_TOO_SPREAD_OUT; truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: tron@1093: static Station *AllocateStation(void) truelight@0: { truelight@1272: Station *st = NULL; truelight@0: truelight@919: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy == 0) { 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 */ truelight@1272: if (AddBlockToPool(&_station_pool)) truelight@1272: return AllocateStation(); truelight@1272: truelight@1272: _error_message = STR_3008_TOO_MANY_STATIONS_LOADING; truelight@1272: return NULL; truelight@0: } truelight@0: truelight@0: truelight@0: static int CountMapSquareAround(uint tile, byte type, byte min, byte max) { tron@909: static const TileIndexDiffC _count_square_table[] = { tron@909: {-3, -3}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, tron@909: {-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, tron@909: {-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, tron@909: {-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, tron@909: {-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, tron@909: {-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, tron@909: {-6, 1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0} truelight@193: }; tron@909: const TileIndexDiffC *p; truelight@0: int num = 0; truelight@0: tron@847: for (p = _count_square_table; p != endof(_count_square_table); ++p) { tron@909: tile = TILE_MASK(tile + ToTileIndexDiff(*p)); truelight@0: tron@1035: if (IsTileType(tile, type) && _map5[tile] >= min && _map5[tile] <= max) truelight@0: num++; truelight@0: } truelight@0: truelight@0: return num; truelight@0: } truelight@0: truelight@0: #define M(x) ((x) - STR_SV_STNAME) truelight@0: truelight@0: static bool GenerateStationName(Station *st, uint 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@0: if (s != st && s->xy != 0 && 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))) { truelight@0: if (CountMapSquareAround(tile, MP_INDUSTRY, 0, 6) >= 2 || tron@1472: CountMapSquareAround(tile, MP_INDUSTRY, 0x64, 0x73) >= 2 || tron@1472: CountMapSquareAround(tile, MP_INDUSTRY, 0x2F, 0x33) >= 2 || tron@1472: CountMapSquareAround(tile, MP_INDUSTRY, 0x48, 0x58) >= 2 || tron@1472: CountMapSquareAround(tile, MP_INDUSTRY, 0x5B, 0x63) >= 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 && tron@1472: CountMapSquareAround(tile, MP_WATER, 0, 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)) && ( tron@1472: CountMapSquareAround(tile, MP_TREES, 0, 255) >= 8 || tron@1472: CountMapSquareAround(tile, MP_INDUSTRY, 0x10, 0x11) >= 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@1424: static Station *GetClosestStationFromTile(TileIndex tile, uint threshold, byte owner) truelight@0: { tron@1424: Station* best_station = NULL; tron@1424: Station* st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { tron@1424: if (st->xy != 0 && (owner == OWNER_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: truelight@0: st->last_vehicle = INVALID_VEHICLE; 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; truelight@0: } darkvater@243: darkvater@243: _global_station_sort_dirty = true; // build a new station 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: { tron@926: Point pt = RemapCoords2(TileX(st->xy) * 16, TileY(st->xy) * 16); 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: { truelight@0: Station *st; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy != 0) truelight@193: 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@534: SetDParam(2, items >> 16); tron@534: SetDParam(1, items & 0xFFFF); tron@534: SetDParam(0, st->index); truelight@0: AddNewsItem(msg + ((items >> 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: truelight@0: memset(produced, 0, NUM_CARGO * sizeof(uint)); 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@1424: TileIndex tile = TILE_XY(xc, yc); tron@1424: tron@1214: gpc = _tile_type_procs[GetTileType(tile)]->get_produced_cargo_proc; truelight@0: if (gpc != NULL) { tron@1424: byte 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@1424: TileIndex tile = TILE_XY(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@1424: for (i = 0; i < lengthof(ac); ++i) tron@473: accepts[i] += ac[i]; truelight@0: } tron@1424: } tron@1424: } truelight@0: } truelight@0: tron@1423: typedef struct Rectangle { tron@1423: uint min_x; tron@1423: uint min_y; tron@1423: uint max_x; tron@1423: uint max_y; tron@1423: } Rectangle; tron@1423: tron@1423: static void MergePoint(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; darkvater@1507: 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 matthijs@1751: if (IsBuoy(st)) truelight@0: 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@1423: st->train_tile + TILE_XY(st->trainst_w - 1, st->trainst_h - 1) tron@1423: ); truelight@0: } celestar@1217: truelight@0: if (st->airport_tile != 0) { tron@1423: MergePoint(&rect, st->airport_tile); tron@1423: MergePoint(&rect, tron@1423: st->airport_tile + TILE_XY( tron@1423: _airport_size_x[st->airport_type] - 1, tron@1423: _airport_size_y[st->airport_type] - 1 tron@1423: ) 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: Celestar@568: if (_patches.modified_catchment) { truelight@703: rad = FindCatchmentRadius(st); Celestar@568: } else { Celestar@568: rad = 4; Celestar@568: } truelight@0: truelight@0: // And retrieve the acceptance. tron@1423: if (rect.max_x >= rect.min_x) { tron@1423: GetAcceptanceAroundTiles( tron@1423: accepts, tron@1423: TILE_XY(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: truelight@0: st->goods[i].waiting_acceptance = (st->goods[i].waiting_acceptance & ~0xF000) + (amt << 12); 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: 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. truelight@0: static void DeleteStationIfEmpty(Station *st) { truelight@0: if (st->facilities == 0) { truelight@0: st->delete_ctr = 0; darkvater@28: InvalidateWindow(WC_STATION_LIST, st->owner); darkvater@27: } truelight@0: } truelight@0: darkvater@977: static int32 ClearTile_Station(uint 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. truelight@0: int32 CheckFlatLandBelow(uint tile, uint w, uint h, uint flags, uint invalid_dirs, int *station) truelight@0: { truelight@0: int32 cost = 0, ret; truelight@0: truelight@0: uint 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) truelight@0: if (!EnsureNoVehicle(tile_cur)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: tileh = GetTileSlope(tile_cur, &z); truelight@0: truelight@0: // steep slopes are completely prohibited truelight@84: if (tileh & 0x10 || (((!_patches.ainew_active && _is_ai_player) || !_patches.build_on_slopes) && tileh != 0)) { truelight@0: _error_message = STR_0007_FLAT_LAND_REQUIRED; truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@0: flat_z = z; truelight@0: if (tileh) { truelight@0: // need to check so the entrance to the station is not pointing at a slope. truelight@0: if ((invalid_dirs&1 && !(tileh & 0xC) && (uint)w_cur == w) || truelight@0: (invalid_dirs&2 && !(tileh & 6) && h_cur == 1) || truelight@0: (invalid_dirs&4 && !(tileh & 3) && w_cur == 1) || truelight@0: (invalid_dirs&8 && !(tileh & 9) && (uint)h_cur == h)) { truelight@0: _error_message = STR_0007_FLAT_LAND_REQUIRED; truelight@0: return CMD_ERROR; truelight@0: } truelight@0: cost += _price.terraform; truelight@0: flat_z += 8; 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) { truelight@0: _error_message = STR_0007_FLAT_LAND_REQUIRED; truelight@0: return CMD_ERROR; truelight@0: } truelight@0: truelight@193: // if station is set, then we have special handling to allow building on top of already existing stations. truelight@0: // so station points to -1 if we can build on any station. or it points to a station if we're only allowed to build truelight@0: // on exactly that station. tron@1035: if (station != NULL && IsTileType(tile_cur, MP_STATION)) { Celestar@972: if (_map5[tile_cur] >= 8) { darkvater@977: _error_message = ClearTile_Station(tile_cur, DC_AUTO); // get error message truelight@0: return CMD_ERROR; truelight@0: } else { truelight@0: int st = _map2[tile_cur]; truelight@0: if (*station == -1) truelight@0: *station = st; truelight@0: else if (*station != st) { truelight@0: _error_message = STR_3006_ADJOINS_MORE_THAN_ONE_EXISTING; truelight@0: return CMD_ERROR; truelight@0: } truelight@0: } truelight@0: } else { truelight@0: ret = DoCommandByTile(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (ret == CMD_ERROR) return CMD_ERROR; 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: truelight@0: static bool CanExpandRailroadStation(Station *st, uint *fin, int direction) truelight@0: { truelight@0: uint curw = st->trainst_w, curh = st->trainst_h; truelight@0: uint 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; truelight@0: tile = TILE_XY(x,y); truelight@0: } else { truelight@0: // check so the direction is the same Celestar@1189: if ((_map5[st->train_tile] & 1) != direction) { 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 truelight@0: if (curw == w && st->train_tile == tile + TILE_XY(0, h)) { truelight@0: // above truelight@0: curh += h; truelight@0: } else if (curw == w && st->train_tile == tile - TILE_XY(0, curh)) { truelight@0: // below truelight@0: tile -= TILE_XY(0, curh); truelight@0: curh += h; truelight@0: } else if (curh == h && st->train_tile == tile + TILE_XY(w, 0)) { truelight@0: // to the left truelight@0: curw += w; truelight@0: } else if (curh == h && st->train_tile == tile - TILE_XY(curw, 0)) { truelight@0: // to the right truelight@0: tile -= TILE_XY(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: truelight@0: // stolen from TTDPatch tron@1477: static void GetStationLayout(byte *layout, int numtracks, int plat_len, StationSpec *spec) truelight@0: { tron@1472: if (spec != NULL && spec->lengths >= plat_len && tron@1472: spec->platforms[plat_len - 1] >= numtracks && tron@1472: spec->layouts[plat_len - 1][numtracks - 1]) { tron@449: /* Custom layout defined, follow it. */ tron@449: memcpy(layout, spec->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 { truelight@0: if (numtracks & 1) truelight@0: layout = CreateSingle(layout, plat_len); truelight@0: 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 Darkvater@1781: * @param x,y starting position of station dragging/placement Darkvater@1775: * @param p1 various bitstuffed elements Darkvater@1775: * - p1 = (bit 0) - orientation (p1 & 1) Darkvater@1775: * - p1 = (bit 8-15) - number of tracks (p1 >> 8) & 0xFF) Darkvater@1775: * - p1 = (bit 16-23) - platform length (p1 >> 16) & 0xFF) Darkvater@1775: * @param p2 various bitstuffed elements Darkvater@1775: * - p2 = (bit 0- 3) - railtype (p2 & 0xF) Darkvater@1775: * - p2 = (bit 4) - set for custom station (p2 & 0x10) Darkvater@1775: * - p2 = (bit 8-..) - custom station id (p2 >> 8) truelight@0: */ Darkvater@1775: int32 CmdBuildRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1775: Station *st; Darkvater@1775: TileIndex tile_org; Darkvater@1775: int w_org, h_org; truelight@0: int32 cost, ret; truelight@0: int est; truelight@0: int plat_len, numtracks; truelight@0: int direction; truelight@0: uint finalvalues[3]; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: Darkvater@1775: tile_org = TILE_FROM_XY(x, y); 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 */ Darkvater@1775: direction = p1 & 1; Darkvater@1775: numtracks = (p1 >> 8) & 0xFF; Darkvater@1775: plat_len = (p1 >> 16) & 0xFF; Darkvater@1775: /* w = length, h = num_tracks */ Darkvater@1775: if (direction) { Darkvater@1775: h_org = plat_len; Darkvater@1775: w_org = numtracks; Darkvater@1775: } else { Darkvater@1775: w_org = plat_len; Darkvater@1775: h_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) truelight@0: est = -1; 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 Darkvater@1775: if (CmdFailed(ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags&~DC_EXEC, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR; 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? Celestar@1189: if ((!_patches.ainew_active && _is_ai_player) || !_patches.join_stations) truelight@0: return_cmd_error(STR_3005_TOO_CLOSE_TO_ANOTHER_RAILROAD); Celestar@1189: if (!CanExpandRailroadStation(st, finalvalues, direction)) 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? truelight@0: if (!CheckStationSpreadOut(st, tile_org, w_org, h_org)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: } else { truelight@0: // Create a new station truelight@0: st = AllocateStation(); truelight@0: if (st == NULL) truelight@0: return CMD_ERROR; truelight@193: truelight@0: st->town = ClosestTownFromTile(tile_org, (uint)-1); truelight@0: if (_current_player < MAX_PLAYERS && flags&DC_EXEC) truelight@0: SETBIT(st->town->have_ratings, _current_player); truelight@0: truelight@0: if (!GenerateStationName(st, tile_org, 0)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: StationInitialize(st, tile_org); truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: int tile_delta; truelight@0: byte *layout_ptr; celestar@1551: StationID station_index = st->index; tron@1477: StationSpec *statspec; 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) Darkvater@1775: if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR; 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: truelight@0: tile_delta = direction ? TILE_XY(0,1) : TILE_XY(1,0); truelight@193: tron@449: statspec = (p2 & 0x10) != 0 ? GetCustomStation(STAT_CLASS_DFLT, p2 >> 8) : NULL; truelight@0: layout_ptr = alloca(numtracks * plat_len); tron@449: GetStationLayout(layout_ptr, numtracks, plat_len, statspec); truelight@193: truelight@0: do { Darkvater@1775: TileIndex tile = tile_org; truelight@0: int w = plat_len; truelight@0: do { truelight@0: truelight@193: ModifyTile(tile, truelight@0: MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT | tron@449: MP_MAP2 | MP_MAP5 | MP_MAP3LO | MP_MAP3HI, truelight@0: station_index, /* map2 parameter */ tron@449: p2 & 0xFF, /* map3lo parameter */ tron@449: p2 >> 8, /* map3hi parameter */ truelight@0: (*layout_ptr++) + direction /* map5 parameter */ truelight@0: ); truelight@0: truelight@0: tile += tile_delta; truelight@0: } while (--w); truelight@0: tile_org += tile_delta ^ TILE_XY(1,1); truelight@0: } while (--numtracks); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); truelight@0: InvalidateWindow(WC_STATION_LIST, st->owner); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static bool TileBelongsToRailStation(Station *st, uint tile) truelight@0: { tron@1035: return IsTileType(tile, MP_STATION) && _map2[tile] == st->index && _map5[tile] < 8; 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; truelight@0: uint 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 truelight@0: for(i=0; !TileBelongsToRailStation(st, tile + TILE_XY(0,i)) ;) truelight@0: // the left side is unused? truelight@0: if (++i==h) { tile += TILE_XY(1, 0); w--; goto restart; } truelight@0: truelight@0: // check the right side, x = constant, y changes truelight@0: for(i=0; !TileBelongsToRailStation(st, tile + TILE_XY(w-1,i)) ;) truelight@0: // the right side is unused? truelight@0: if (++i==h) { w--; goto restart; } truelight@0: truelight@0: // check the upper side, y = constant, x changes truelight@0: for(i=0; !TileBelongsToRailStation(st, tile + TILE_XY(i,0)) ;) truelight@0: // the left side is unused? truelight@0: if (++i==w) { tile += TILE_XY(0, 1); h--; goto restart; } truelight@0: truelight@0: // check the lower side, y = constant, x changes truelight@0: for(i=0; !TileBelongsToRailStation(st, tile + TILE_XY(i,h-1)) ;) truelight@0: // the left side is unused? truelight@0: if (++i==w) { h--; goto restart; } 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 Darkvater@1782: * @param x,y tile coordinates to remove Darkvater@1782: * @param p1 unused Darkvater@1782: * @param p2 unused Darkvater@1782: */ truelight@0: int32 CmdRemoveFromRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1782: TileIndex tile = TILE_FROM_XY(x, y); 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. tron@1035: if (!IsTileType(tile, MP_STATION) || _map5[tile] >= 8 || !_patches.nonuniform_stations) return CMD_ERROR; truelight@919: st = GetStation(_map2[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) { truelight@0: DoClearSquare(tile); 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); 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 truelight@0: uint GetStationPlatforms(Station *st, uint tile) truelight@0: { truelight@0: uint t; truelight@0: int dir,delta; truelight@0: int len; truelight@0: assert(TileBelongsToRailStation(st, tile)); truelight@0: truelight@0: len = 0; truelight@0: dir = _map5[tile]&1; truelight@0: delta = dir ? TILE_XY(0,1) : TILE_XY(1,0); truelight@0: truelight@0: // find starting tile.. truelight@0: t = tile; truelight@0: do { t -= delta; len++; } while (TileBelongsToRailStation(st, t) && (_map5[t]&1) == dir); truelight@0: truelight@0: // find ending tile truelight@0: t = tile; truelight@0: do { t += delta; len++; }while (TileBelongsToRailStation(st, t) && (_map5[t]&1) == dir); truelight@0: truelight@0: return len - 1; truelight@0: } truelight@0: truelight@0: tron@449: /* TODO: Custom classes! */ tron@449: /* Indexed by class, just STAT_CLASS_DFLT and STAT_CLASS_WAYP supported. */ tron@449: static int _statspec_highest_id[2] = {-1, -1}; tron@1477: static StationSpec _station_spec[2][256]; tron@1477: tron@1477: void SetCustomStation(byte local_stid, StationSpec *spec) darkvater@399: { tron@1477: StationClass sclass; darkvater@400: int stid = -1; darkvater@400: tron@449: assert(spec->sclass == STAT_CLASS_DFLT || spec->sclass == STAT_CLASS_WAYP); tron@449: sclass = spec->sclass - 1; darkvater@400: darkvater@400: if (spec->localidx != 0) { darkvater@400: /* Already allocated, try to resolve to global stid */ darkvater@400: int i; darkvater@400: tron@449: for (i = 0; i <= _statspec_highest_id[sclass]; i++) { tron@1472: if (_station_spec[sclass][i].grfid == spec->grfid && tron@1472: _station_spec[sclass][i].localidx == local_stid + 1) { darkvater@400: stid = i; darkvater@408: /* FIXME: Release original SpriteGroup to darkvater@408: * prevent leaks. But first we need to darkvater@408: * refcount the SpriteGroup. --pasky */ darkvater@400: break; darkvater@400: } darkvater@400: } darkvater@400: } darkvater@400: darkvater@400: if (stid == -1) { darkvater@400: /* Allocate new one. */ tron@449: if (_statspec_highest_id[sclass] >= 255) { darkvater@400: error("Too many custom stations allocated."); darkvater@400: return; darkvater@400: } tron@449: stid = ++_statspec_highest_id[sclass]; darkvater@400: spec->localidx = local_stid + 1; darkvater@400: } darkvater@400: tron@449: //debug("Registering station #%d of class %d", stid, sclass); tron@449: memcpy(&_station_spec[sclass][stid], spec, sizeof(*spec)); darkvater@399: } darkvater@399: tron@1477: StationSpec *GetCustomStation(StationClass sclass, byte stid) celestar@389: { tron@449: assert(sclass == STAT_CLASS_DFLT || sclass == STAT_CLASS_WAYP); tron@449: sclass--; tron@449: //debug("Asking for station #%d of class %d", stid, sclass); tron@449: if (stid > _statspec_highest_id[sclass]) celestar@389: return NULL; tron@449: return &_station_spec[sclass][stid]; darkvater@403: } darkvater@403: tron@1477: static RealSpriteGroup *ResolveStationSpriteGroup(SpriteGroup *spritegroup, Station *stat) darkvater@413: { darkvater@413: switch (spritegroup->type) { darkvater@413: case SGT_REAL: darkvater@413: return &spritegroup->g.real; darkvater@413: darkvater@413: case SGT_DETERMINISTIC: { tron@1477: DeterministicSpriteGroup *dsg = &spritegroup->g.determ; tron@1477: SpriteGroup *target; darkvater@413: int value = -1; darkvater@413: darkvater@413: if ((dsg->variable >> 6) == 0) { darkvater@413: /* General property */ darkvater@413: value = GetDeterministicSpriteValue(dsg->variable); darkvater@413: darkvater@413: } else { tron@433: if (stat == NULL) { tron@433: /* We are in a build dialog of something, tron@433: * and we are checking for something undefined. tron@433: * That means we should get the first target tron@433: * (NOT the default one). */ tron@433: if (dsg->num_ranges > 0) { tron@433: target = &dsg->ranges[0].group; tron@433: } else { tron@433: target = dsg->default_group; tron@433: } tron@433: return ResolveStationSpriteGroup(target, NULL); tron@433: } tron@433: darkvater@413: /* Station-specific property. */ darkvater@413: if (dsg->var_scope == VSG_SCOPE_PARENT) { darkvater@413: /* TODO: Town structure. */ darkvater@413: darkvater@413: } else /* VSG_SELF */ { tron@1472: if (dsg->variable == 0x40 || dsg->variable == 0x41) { darkvater@413: /* FIXME: This is ad hoc only darkvater@413: * for waypoints. */ darkvater@413: value = 0x01010000; darkvater@413: } else { darkvater@413: /* TODO: Only small fraction done. */ tron@426: // TTDPatch runs on little-endian arch; tron@426: // Variable is 0x70 + offset in the TTD's station structure darkvater@413: switch (dsg->variable - 0x70) { darkvater@413: case 0x80: tron@433: value = stat->facilities; darkvater@413: break; darkvater@413: case 0x81: tron@433: value = stat->airport_type; darkvater@413: break; darkvater@413: case 0x82: celestar@1217: value = stat->truck_stops->status; darkvater@413: break; darkvater@413: case 0x83: celestar@1217: value = stat->bus_stops->status; darkvater@413: break; darkvater@413: case 0x86: tron@433: value = stat->airport_flags & 0xFFFF; darkvater@413: break; darkvater@413: case 0x87: tron@433: value = stat->airport_flags & 0xFF; darkvater@413: break; darkvater@413: case 0x8A: tron@433: value = stat->build_date; darkvater@413: break; darkvater@413: } darkvater@413: } darkvater@413: } darkvater@413: } darkvater@413: darkvater@413: target = value != -1 ? EvalDeterministicSpriteGroup(dsg, value) : dsg->default_group; darkvater@413: return ResolveStationSpriteGroup(target, stat); darkvater@413: } darkvater@413: darkvater@413: default: tron@445: case SGT_RANDOMIZED: darkvater@413: error("I don't know how to handle random spritegroups yet!"); darkvater@413: return NULL; darkvater@413: } darkvater@413: } darkvater@413: tron@1477: uint32 GetCustomStationRelocation(StationSpec *spec, Station *stat, byte ctype) darkvater@403: { tron@1477: RealSpriteGroup *rsg; darkvater@408: darkvater@413: rsg = ResolveStationSpriteGroup(&spec->spritegroup[ctype], stat); darkvater@408: darkvater@408: if (rsg->sprites_per_set != 0) { darkvater@408: if (rsg->loading_count != 0) { darkvater@408: return rsg->loading[0]; tron@1012: } else if (rsg->loaded_count != 0) { darkvater@408: return rsg->loaded[0]; darkvater@408: } darkvater@403: } darkvater@408: darkvater@408: error("Custom station 0x%08x::0x%02x has no sprites associated.", tron@1472: spec->grfid, spec->localidx); dominik@452: /* This is what gets subscribed of dtss->image in newgrf.c, darkvater@408: * so it's probably kinda "default offset". Try to use it as darkvater@408: * emergency measure. */ darkvater@408: return 0x42D; celestar@389: } celestar@389: tron@1477: int GetCustomStationsCount(StationClass sclass) celestar@389: { tron@449: assert(sclass == STAT_CLASS_DFLT || sclass == STAT_CLASS_WAYP); tron@449: sclass--; tron@449: return _statspec_highest_id[sclass] + 1; celestar@389: } celestar@389: celestar@389: darkvater@149: static int32 RemoveRailroadStation(Station *st, TileIndex tile, uint32 flags) truelight@0: { truelight@0: int w,h; truelight@0: int32 cost; 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) darkvater@149: return DoCommandByTile(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: /* cost is area * constant */ truelight@0: cost = w*h*_price.remove_rail_station; 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; truelight@0: if (flags & DC_EXEC) truelight@0: DoClearSquare(tile); truelight@0: } truelight@0: tile += TILE_XY(1, 0); truelight@0: } while (--w); truelight@0: w = w_bak; truelight@0: tile = tile + TILE_XY(-w, 1); truelight@0: } while (--h); truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: st->train_tile = 0; truelight@0: st->facilities &= ~FACIL_TRAIN; truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: DeleteStationIfEmpty(st); truelight@0: } truelight@193: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: int32 DoConvertStationRail(uint tile, uint totype, bool exec) truelight@0: { Darkvater@1782: const Station *st = GetStation(_map2[tile]); truelight@0: if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR; truelight@0: truelight@0: // tile is not a railroad station? truelight@0: if (_map5[tile] >= 8) return CMD_ERROR; truelight@0: truelight@0: // tile is already of requested type? truelight@0: if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR; truelight@0: truelight@0: if (exec) { truelight@0: // change type. truelight@0: _map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: return _price.build_rail >> 1; truelight@0: } truelight@0: celestar@1217: void FindRoadStationSpot(bool truck_station, Station *st, RoadStop ***currstop, RoadStop **prev) celestar@1217: { celestar@1217: RoadStop **primary_stop; celestar@1217: celestar@1217: primary_stop = (truck_station) ? &st->truck_stops : &st->bus_stops; celestar@1217: celestar@1217: if (*primary_stop == NULL) { celestar@1217: //we have no station of the type yet, so write a "primary station" celestar@1217: //(the one at st->foo_stops) 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: Darkvater@1782: /** Build a bus station Darkvater@1782: * @param x,y coordinates to build bus station at Darkvater@1782: * @param p1 direction the busstop exit is pointing towards Darkvater@1782: * @param p2 0 for Bus stops, 1 for truck stops truelight@0: */ Darkvater@1782: int32 CmdBuildRoadStop(int x, int y, 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; Darkvater@1782: TileIndex tile; truelight@0: int32 cost; Darkvater@1782: bool type = !!p2; Darkvater@1782: Darkvater@1782: /* Saveguard the parameters */ Darkvater@1782: if (p1 > 3) return CMD_ERROR; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: tile = TILE_FROM_XY(x,y); truelight@0: truelight@0: if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) truelight@0: return CMD_ERROR; truelight@0: Darkvater@1782: cost = CheckFlatLandBelow(tile, 1, 1, flags, 1 << p1, NULL); celestar@1217: if (cost == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: truelight@0: st = GetStationAround(tile, 1, 1, -1); truelight@0: if (st == CHECK_STATIONS_ERR) truelight@0: 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); truelight@0: if (st!=NULL && st->facilities) 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(); celestar@1217: if (road_stop == NULL) celestar@1217: return_cmd_error( (type) ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS); celestar@1217: celestar@1217: if ( st != NULL && (GetNumRoadStops(st, RS_BUS) + GetNumRoadStops(st, RS_TRUCK) >= ROAD_STOP_LIMIT)) celestar@1217: return_cmd_error( (type) ? STR_3008B_TOO_MANY_TRUCK_STOPS : STR_3008A_TOO_MANY_BUS_STOPS); celestar@1217: 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: truelight@0: if (!CheckStationSpreadOut(st, tile, 1, 1)) truelight@0: return CMD_ERROR; truelight@0: celestar@1217: FindRoadStationSpot(type, st, &currstop, &prev); truelight@0: } else { truelight@0: Town *t; truelight@0: truelight@0: st = AllocateStation(); truelight@0: if (st == NULL) truelight@0: return CMD_ERROR; truelight@0: truelight@0: st->town = t = ClosestTownFromTile(tile, (uint)-1); truelight@0: celestar@1217: FindRoadStationSpot(type, st, &currstop, &prev); celestar@1217: truelight@0: if (_current_player < MAX_PLAYERS && 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 (!GenerateStationName(st, tile, 0)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: 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); celestar@1217: (*currstop)->type = type; 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: truelight@193: ModifyTile(tile, truelight@0: MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT | truelight@0: MP_MAP2 | MP_MAP5 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR, Darkvater@1782: st->index, /* map2 parameter */ Darkvater@1782: /* XXX - Truck stops have 0x43 _map5[] value + direction Darkvater@1782: * XXX - Bus stops have a _map5 value of 0x47 + direction */ Darkvater@1782: ((type) ? 0x43 : 0x47) + p1 /* map5 parameter */ truelight@0: ); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); 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@1217: bool is_truck = _map5[tile] < 0x47; truelight@0: truelight@0: if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) truelight@0: return CMD_ERROR; truelight@0: celestar@1217: 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: truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { Darkvater@1706: int i; truelight@0: DoClearSquare(tile); truelight@0: Darkvater@1706: /* Clear all vehicles destined for this station */ Darkvater@1706: for (i = 0; i != NUM_SLOTS; i++) { Darkvater@1706: if (cur_stop->slot[i] != INVALID_SLOT) { Darkvater@1706: Vehicle *v = GetVehicle(cur_stop->slot[i]); Darkvater@1706: ClearSlot(v, v->u.road.slot); Darkvater@1706: } Darkvater@1706: } Darkvater@1706: celestar@1217: cur_stop->used = false; celestar@1217: if (cur_stop->prev != NULL) //alter previous stop celestar@1217: cur_stop->prev->next = cur_stop->next; celestar@1217: celestar@1217: if (cur_stop->next != NULL) //alter next stop celestar@1217: cur_stop->next->prev = cur_stop->prev; celestar@1217: celestar@1217: //we only had one stop left celestar@1217: if (cur_stop->next == NULL && cur_stop->prev == NULL) { celestar@1217: 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: 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@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) truelight@0: static const byte _airport_map5_tiles_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) truelight@0: static const byte _airport_map5_tiles_town[] = { truelight@0: 31, 9, 33, 9, 9, 32, truelight@0: 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 truelight@0: static const byte _airport_map5_tiles_metropolitan[] = { truelight@0: 31, 9, 33, 9, 9, 32, truelight@0: 27, 36, 29, 34, 8, 10, truelight@0: 30, 11, 35, 13, 20, 21, truelight@0: 102, 8, 8, 8, 8, 28, truelight@0: 83, 84, 84, 84, 84, 83, truelight@0: 26, 23, 23, 23, 23, 26 truelight@0: }; truelight@0: truelight@0: // International Airport (large) - 2 runways truelight@0: static const byte _airport_map5_tiles_international[] = { truelight@0: 88, 89, 89, 89, 89, 89, 88, truelight@0: 51, 8, 8, 8, 8, 8, 32, truelight@0: 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, truelight@0: 87, 8, 8, 8, 8, 8, 90, truelight@0: 26, 23, 23, 23, 23, 23, 26 truelight@0: }; truelight@0: truelight@0: // Heliport truelight@0: static const byte _airport_map5_tiles_heliport[] = { truelight@0: 66, truelight@0: }; truelight@0: truelight@0: static const byte * const _airport_map5_tiles[] = { truelight@0: _airport_map5_tiles_country, // Country Airfield (small) truelight@0: _airport_map5_tiles_town, // City Airport (large) truelight@0: _airport_map5_tiles_heliport, // Heliport truelight@193: _airport_map5_tiles_metropolitan, // Metropolitain Airport (large) truelight@0: _airport_map5_tiles_international, // International Airport (xlarge) truelight@0: }; truelight@0: truelight@0: /* Place an Airport truelight@0: * p1 - airport type truelight@0: * p2 - unused truelight@0: */ truelight@0: int32 CmdBuildAirport(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: uint tile; truelight@0: Town *t; truelight@0: Station *st; truelight@0: int32 cost; truelight@0: int w,h; truelight@193: bool airport_upgrade = true; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: tile = TILE_FROM_XY(x,y); truelight@193: 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@0: if (st->xy != 0 && 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: truelight@0: w = _airport_size_x[p1]; truelight@0: h = _airport_size_y[p1]; truelight@0: truelight@0: cost = CheckFlatLandBelow(tile, w, h, flags, 0, NULL); truelight@0: if (cost == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: truelight@0: st = GetStationAround(tile, w, h, -1); truelight@0: if (st == CHECK_STATIONS_ERR) truelight@0: 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); 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: truelight@0: if (!CheckStationSpreadOut(st, tile, 1, 1)) truelight@0: 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(); truelight@0: if (st == NULL) truelight@0: return CMD_ERROR; truelight@0: tron@1265: st->town = t; truelight@0: truelight@0: if (_current_player < MAX_PLAYERS && 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) truelight@0: if (!GenerateStationName(st, tile, p1 == AT_HELIPORT ? 5 : 1)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: StationInitialize(st, tile); truelight@0: } truelight@0: truelight@0: cost += _price.build_airport * w * h; truelight@0: truelight@0: if (flags & DC_EXEC) { tron@701: const AirportFTAClass *afc = GetAirport(p1); tron@701: truelight@0: st->owner = _current_player; tron@701: if (_current_player == _local_player && afc->nof_depots != 0) { tron@909: _last_built_aircraft_depot_tile = tile + ToTileIndexDiff(afc->airport_depots[0]); truelight@0: } truelight@0: 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: truelight@0: /* if airport was demolished while planes were en-route to it, the positions can no longer truelight@0: be the same (v->u.air.pos), since different airports have different indexes. So update truelight@0: all planes en-route to this airport. Only update if truelight@0: 1. airport is upgraded truelight@0: 2. airport is added to existing station (unfortunately unavoideable) truelight@0: */ truelight@0: if (airport_upgrade) {UpdateAirplanesOnNewStation(st);} truelight@193: truelight@0: { truelight@0: const byte *b = _airport_map5_tiles[p1]; truelight@0: BEGIN_TILE_LOOP(tile_cur,w,h,tile) truelight@193: ModifyTile(tile_cur, truelight@0: MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT | truelight@0: MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAP5, truelight@0: st->index, *b++); truelight@0: END_TILE_LOOP(tile_cur,w,h,tile) truelight@0: } truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); 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: { truelight@0: uint tile; truelight@0: int w,h; truelight@0: int32 cost; 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: truelight@0: w = _airport_size_x[st->airport_type]; truelight@0: h = _airport_size_y[st->airport_type]; truelight@0: truelight@0: cost = w * h * _price.remove_airport; truelight@0: truelight@0: { truelight@0: BEGIN_TILE_LOOP(tile_cur,w,h,tile) truelight@0: if (!EnsureNoVehicle(tile_cur)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: DeleteAnimatedTile(tile_cur); truelight@0: DoClearSquare(tile_cur); truelight@0: } truelight@0: END_TILE_LOOP(tile_cur, w,h,tile) truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { tron@700: const AirportFTAClass *afc = GetAirport(st->airport_type); tron@700: uint i; tron@700: tron@700: for (i = 0; i < afc->nof_depots; ++i) tron@909: DeleteWindowById(WC_VEHICLE_DEPOT, tile + ToTileIndexDiff(afc->airport_depots[i])); darkvater@755: 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: truelight@193: /* Build a buoy truelight@0: * p1,p2 unused truelight@0: */ truelight@0: truelight@0: int32 CmdBuildBuoy(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: TileInfo ti; truelight@0: Station *st; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: truelight@0: if (ti.type != MP_WATER || ti.tileh != 0 || ti.map5 != 0 || ti.tile == 0) truelight@0: return_cmd_error(STR_304B_SITE_UNSUITABLE); truelight@0: truelight@0: st = AllocateStation(); truelight@0: if (st == NULL) truelight@0: return CMD_ERROR; truelight@0: truelight@0: st->town = ClosestTownFromTile(ti.tile, (uint)-1); truelight@0: st->sign.width_1 = 0; truelight@0: truelight@0: if (!GenerateStationName(st, ti.tile, 4)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: StationInitialize(st, ti.tile); truelight@0: st->dock_tile = ti.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: truelight@0: ModifyTile(ti.tile, truelight@0: MP_SETTYPE(MP_STATION) | truelight@0: MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5, truelight@0: st->index, /* map2 */ truelight@0: OWNER_NONE, /* map_owner */ truelight@0: 0x52 /* map5 */ truelight@0: ); truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@193: truelight@0: UpdateStationAcceptance(st, false); 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) { dominik@1078: if (order->type == OT_GOTO_STATION && order->station == 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: { truelight@0: uint tile; truelight@0: truelight@0: if (_current_player >= MAX_PLAYERS) { truelight@0: /* XXX: strange stuff */ truelight@0: return_cmd_error(INVALID_STRING_ID); truelight@0: } truelight@0: truelight@0: tile = st->dock_tile; truelight@0: dominik@1078: if (CheckShipsOnBuoy(st)) dominik@1078: return_cmd_error(STR_BUOY_IS_IN_USE); dominik@1078: truelight@0: if (!EnsureNoVehicle(tile)) truelight@0: 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: truelight@193: ModifyTile(tile, truelight@0: MP_SETTYPE(MP_WATER) | truelight@0: MP_MAP2_CLEAR | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | MP_MAPOWNER | MP_MAP5 | MP_MAP2_CLEAR, truelight@0: OWNER_WATER, /* map_owner */ truelight@0: 0 /* map5 */ truelight@0: ); 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: }; truelight@0: static const byte _dock_w_chk[4] = { 2,1,2,1 }; truelight@0: static const byte _dock_h_chk[4] = { 1,2,1,2 }; truelight@0: truelight@0: int32 CmdBuildDock(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: TileInfo ti; truelight@0: int direction; truelight@0: int32 cost; truelight@0: uint tile, tile_cur; truelight@0: Station *st; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@0: truelight@0: FindLandscapeHeight(&ti, x, y); truelight@0: truelight@0: if ((direction=0,ti.tileh) != 3 && truelight@0: (direction++,ti.tileh) != 9 && truelight@0: (direction++,ti.tileh) != 12 && truelight@0: (direction++,ti.tileh) != 6) truelight@0: return_cmd_error(STR_304B_SITE_UNSUITABLE); truelight@0: truelight@0: if (!EnsureNoVehicle(ti.tile)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: cost = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (cost == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: tron@900: tile_cur = (tile=ti.tile) + TileOffsByDir(direction); truelight@0: truelight@0: if (!EnsureNoVehicle(tile_cur)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: FindLandscapeHeightByTile(&ti, tile_cur); truelight@0: if (ti.tileh != 0 || ti.type != MP_WATER) truelight@0: return_cmd_error(STR_304B_SITE_UNSUITABLE); truelight@0: truelight@0: cost = DoCommandByTile(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR); truelight@0: if (cost == CMD_ERROR) truelight@0: return CMD_ERROR; truelight@0: tron@900: tile_cur = tile_cur + TileOffsByDir(direction); truelight@0: FindLandscapeHeightByTile(&ti, tile_cur); truelight@0: if (ti.tileh != 0 || ti.type != MP_WATER) truelight@0: return_cmd_error(STR_304B_SITE_UNSUITABLE); 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); truelight@0: if (st == CHECK_STATIONS_ERR) truelight@0: 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: truelight@0: if (!CheckStationSpreadOut(st, tile, 1, 1)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (st->dock_tile != 0) truelight@0: return_cmd_error(STR_304C_TOO_CLOSE_TO_ANOTHER_DOCK); truelight@0: } else { truelight@0: Town *t; truelight@0: truelight@0: st = AllocateStation(); truelight@0: if (st == NULL) truelight@0: return CMD_ERROR; truelight@0: truelight@0: st->town = t = ClosestTownFromTile(tile, (uint)-1); truelight@0: truelight@0: if (_current_player < MAX_PLAYERS && 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 (!GenerateStationName(st, tile, 3)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) truelight@0: 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: truelight@193: ModifyTile(tile, truelight@193: MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT | truelight@0: MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | truelight@0: MP_MAP5, truelight@0: st->index, truelight@0: direction + 0x4C); truelight@0: tron@900: ModifyTile(tile + TileOffsByDir(direction), truelight@193: MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT | truelight@0: MP_MAP2 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR | truelight@0: MP_MAP5, truelight@0: st->index, truelight@0: (direction&1) + 0x50); truelight@193: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); 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: { truelight@0: uint tile1, tile2; truelight@0: truelight@0: if (!CheckOwnership(st->owner)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: tile1 = st->dock_tile; tron@900: tile2 = tile1 + TileOffsByDir(_map5[tile1] - 0x4C); truelight@0: truelight@0: if (!EnsureNoVehicle(tile1)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (!EnsureNoVehicle(tile2)) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: DoClearSquare(tile1); truelight@0: truelight@0: // convert the water tile to water. truelight@0: ModifyTile(tile2, MP_SETTYPE(MP_WATER) | MP_MAPOWNER | MP_MAP5 | MP_MAP2_CLEAR | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR, OWNER_WATER, 0); 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: truelight@0: tron@449: extern uint16 _custom_sprites_base; truelight@0: truelight@0: static void DrawTile_Station(TileInfo *ti) truelight@0: { truelight@0: uint32 image_or_modificator; tron@449: uint32 image; truelight@0: const DrawTileSeqStruct *dtss; tron@449: const DrawTileSprites *t = NULL; tron@449: byte railtype = _map3_lo[ti->tile] & 0xF; tron@449: uint32 relocation = 0; truelight@0: truelight@0: { truelight@0: uint owner = _map_owner[ti->tile]; truelight@0: image_or_modificator = 0x315 << 16; /* NOTE: possible bug in ttd here? */ truelight@0: if (owner < MAX_PLAYERS) truelight@0: image_or_modificator = PLAYER_SPRITE_COLOR(owner); truelight@0: } truelight@0: truelight@0: // don't show foundation for docks (docks are between 76 (0x4C) and 81 (0x51)) truelight@0: if (ti->tileh != 0 && (ti->map5 < 0x4C || ti->map5 > 0x51)) truelight@0: DrawFoundation(ti, ti->tileh); truelight@0: tron@449: if (_map3_lo[ti->tile] & 0x10) { tron@449: // look for customization tron@1477: StationSpec *statspec = GetCustomStation(STAT_CLASS_DFLT, _map3_hi[ti->tile]); tron@449: tron@449: //debug("Cust-o-mized %p", statspec); tron@449: tron@449: if (statspec != NULL) { truelight@919: Station *st = GetStation(_map2[ti->tile]); tron@449: tron@449: relocation = GetCustomStationRelocation(statspec, st, 0); tron@449: //debug("Relocation %d", relocation); tron@449: t = &statspec->renderdata[ti->map5]; tron@449: } tron@449: } tron@449: tron@449: if (t == NULL) t = &_station_display_datas[ti->map5]; darkvater@384: darkvater@384: image = t->ground_sprite; truelight@0: if (image & 0x8000) truelight@0: image |= image_or_modificator; 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@457: image += railtype * ((image & 0x3FFF) < _custom_sprites_base ? TRACKTYPE_SPRITE_PITCH : 1); tron@449: DrawGroundSprite(image); truelight@0: darkvater@384: foreach_draw_tile_seq(dtss, t->seq) { tron@449: image = dtss->image + relocation; tron@457: // For custom sprites, there's no railtype-based pitching. tron@457: image += railtype * ((image & 0x3FFF) < _custom_sprites_base ? TRACKTYPE_SPRITE_PITCH : 0); darkvater@384: if (_display_opt & DO_TRANS_BUILDINGS) { tron@497: image = (image & 0x3FFF) | 0x03224000; tron@497: } else { truelight@703: if (image&0x8000) image |= image_or_modificator; darkvater@384: } darkvater@384: truelight@0: if ((byte)dtss->delta_z != 0x80) { truelight@0: AddSortableSpriteToDraw(image, ti->x + dtss->delta_x, ti->y + dtss->delta_y, dtss->width, dtss->height, dtss->unk, ti->z + dtss->delta_z); truelight@0: } else { truelight@0: AddChildSpriteScreen(image, dtss->delta_x, dtss->delta_y); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void StationPickerDrawSprite(int x, int y, int railtype, int image) truelight@0: { truelight@0: uint32 ormod, img; truelight@0: const DrawTileSeqStruct *dtss; darkvater@384: const DrawTileSprites *t; truelight@0: truelight@0: /* baseimage */ truelight@0: railtype *= TRACKTYPE_SPRITE_PITCH; 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; truelight@0: if (img & 0x8000) truelight@0: img |= ormod; tron@354: DrawSprite(img + railtype, 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); truelight@0: DrawSprite((dtss->image | ormod) + railtype, x + pt.x, y + pt.y); truelight@0: } truelight@0: } truelight@0: truelight@0: static uint GetSlopeZ_Station(TileInfo *ti) truelight@0: { truelight@0: uint z = ti->z; truelight@0: if (ti->tileh != 0) truelight@0: z += 8; truelight@0: return z; truelight@0: } truelight@0: dominik@39: static uint GetSlopeTileh_Station(TileInfo *ti) dominik@39: { dominik@39: return 0; dominik@39: } dominik@39: tron@473: static void GetAcceptedCargo_Station(uint tile, AcceptedCargo ac) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: static void GetTileDesc_Station(uint tile, TileDesc *td) truelight@0: { truelight@0: byte m5; truelight@0: StringID str; truelight@0: truelight@0: td->owner = _map_owner[tile]; truelight@919: td->build_date = GetStation(_map2[tile])->build_date; truelight@0: truelight@0: m5 = _map5[tile]; truelight@0: (str=STR_305E_RAILROAD_STATION, m5 < 8) || truelight@0: (str=STR_305F_AIRCRAFT_HANGAR, m5==32 || m5==45) || // hangars truelight@0: (str=STR_3060_AIRPORT, m5 < 0x43 || (m5 >= 83 && m5 <= 114)) || truelight@0: (str=STR_3061_TRUCK_LOADING_AREA, m5 < 0x47) || truelight@0: (str=STR_3062_BUS_STATION, m5 < 0x4B) || truelight@0: (str=STR_4807_OIL_RIG, m5 == 0x4B) || truelight@0: (str=STR_3063_SHIP_DOCK, m5 != 0x52) || truelight@0: (str=STR_3069_BUOY, true); truelight@0: td->str = str; truelight@0: } truelight@0: truelight@0: truelight@159: static uint32 GetTileTrackStatus_Station(uint tile, TransportType mode) { truelight@0: uint i = _map5[tile]; truelight@0: uint j = 0; truelight@0: tron@1032: switch (mode) { tron@1032: case TRANSPORT_RAIL: tron@1032: if (i < 8) { tron@1032: const byte tile_track_status_rail[8] = { 1, 2, 1, 2, 1, 2, 1, 2 }; tron@1032: j = tile_track_status_rail[i]; tron@1032: } tron@1032: j += (j << 8); 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@1032: // (0x3F, all tracks available) tron@1032: if (i == 0x52) j = 0x3F; tron@1032: j += (j << 8); tron@1032: break; tron@1032: tron@1032: default: tron@1032: break; truelight@0: } tron@1032: truelight@159: return j; truelight@0: } truelight@0: truelight@0: static void TileLoop_Station(uint tile) truelight@0: { truelight@0: //FIXME -- GetTileTrackStatus_Station -> animated stationtiles truelight@0: // hardcoded.....not good truelight@0: // 0x27 - large big airport (39) truelight@0: // 0x66 - radar metropolitan airport (102) truelight@0: // 0x5A - radar international airport (90) truelight@0: // 0x3A - flag small airport (58) truelight@0: if (_map5[tile] == 39 || _map5[tile] == 58 || _map5[tile] == 90 || _map5[tile] == 102) truelight@0: AddAnimatedTile(tile); truelight@193: truelight@0: // treat a bouy tile as water. truelight@0: else if (_map5[tile] == 0x52) truelight@0: TileLoop_Water(tile); dominik@43: dominik@43: // treat a oilrig (the station part) as water dominik@43: else if (_map5[tile] == 0x4B) dominik@43: TileLoop_Water(tile); dominik@43: truelight@0: } truelight@0: truelight@0: truelight@0: static void AnimateTile_Station(uint tile) truelight@0: { truelight@0: byte m5 = _map5[tile]; truelight@0: //FIXME -- AnimateTile_Station -> not nice code, lots of things double truelight@0: // again hardcoded...was a quick hack truelight@0: truelight@0: // turning radar / windsack on airport truelight@0: if (m5 >= 39 && m5 <= 50) { // turning radar (39 - 50) truelight@0: if (_tick_counter & 3) truelight@0: return; truelight@193: truelight@0: if (++m5 == 50+1) truelight@0: m5 = 39; truelight@0: truelight@0: _map5[tile] = m5; truelight@0: MarkTileDirtyByTile(tile); truelight@0: //added - begin truelight@0: } else if (m5 >= 90 && m5 <= 113) { // turning radar with ground under it (different fences) (90 - 101 | 102 - 113) truelight@0: if (_tick_counter & 3) truelight@0: return; truelight@193: truelight@0: m5++; truelight@193: truelight@0: if (m5 == 101+1) {m5 = 90;} // radar with fences in south truelight@0: else if (m5 == 113+1) {m5 = 102;} // radar with fences in north truelight@0: truelight@0: _map5[tile] = m5; truelight@193: MarkTileDirtyByTile(tile); truelight@0: //added - end truelight@0: } else if (m5 >= 0x3A && m5 <= 0x3D) { // windsack (58 - 61) truelight@0: if (_tick_counter & 1) truelight@0: return; truelight@0: truelight@0: if (++m5 == 0x3D+1) truelight@0: m5 = 0x3A; truelight@193: truelight@0: _map5[tile] = m5; truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: } truelight@0: truelight@0: static void ClickTile_Station(uint tile) truelight@0: { truelight@0: // 0x20 - hangar large airport (32) truelight@0: // 0x41 - hangar small airport (65) truelight@0: if (_map5[tile] == 32 || _map5[tile] == 65) { truelight@0: ShowAircraftDepotWindow(tile); truelight@0: } else { truelight@0: ShowStationViewWindow(_map2[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: truelight@0: static uint32 VehicleEnter_Station(Vehicle *v, uint tile, int x, int y) truelight@0: { celestar@1551: StationID station_id; truelight@0: byte dir; truelight@0: uint16 spd; truelight@0: truelight@0: if (v->type == VEH_Train) { bjarni@1067: if (IS_BYTE_INSIDE(_map5[tile], 0, 8) && v->subtype == TS_Front_Engine && tron@1685: !IsCompatibleTrainStationTile(tile + TileOffsByDir(v->direction >> 1), tile)) { truelight@193: truelight@0: station_id = _map2[tile]; tron@555: if ((!(v->current_order.flags & OF_NON_STOP) && !_patches.new_nonstop) || tron@555: (v->current_order.type == OT_GOTO_STATION && v->current_order.station == station_id)) { tron@555: 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) { truelight@0: x &= 0xF; truelight@0: y &= 0xF; truelight@193: truelight@0: dir = v->direction & 6; truelight@0: if (dir & 2) intswap(x,y); truelight@0: if (y == 8) { truelight@0: if (dir != 2 && dir != 4) { truelight@0: x = (~x)&0xF; truelight@0: } truelight@0: if (x == 12) return 2 | (station_id << 8); /* enter station */ truelight@0: if (x < 12) { truelight@0: v->vehstatus |= VS_TRAIN_SLOWING; truelight@0: spd = _enter_station_speedtable[x]; truelight@0: if (spd < v->cur_speed) truelight@0: v->cur_speed = spd; truelight@0: } truelight@0: } truelight@0: } truelight@193: } truelight@0: } truelight@0: } else if (v->type == VEH_Road) { truelight@0: if (v->u.road.state < 16 && (v->u.road.state&4)==0 && v->u.road.frame==0) { truelight@0: byte m5 = _map5[tile]; truelight@0: byte *b, bb,state; truelight@0: truelight@0: if (IS_BYTE_INSIDE(m5, 0x43, 0x4B)) { celestar@1217: RoadStop *rs = GetRoadStopByTile(tile, GetRoadStopType(tile)); celestar@1217: b = &rs->status; truelight@0: truelight@0: bb = *b; truelight@0: truelight@0: /* bb bits 1..0 describe current the two parking spots. truelight@0: 0 means occupied, 1 means free. */ truelight@0: truelight@0: // Station busy? truelight@0: if (bb & 0x80 || (bb&3) == 0) truelight@0: return 8; truelight@0: truelight@0: state = v->u.road.state + 32; truelight@0: if (bb & 1) { truelight@0: bb &= ~1; truelight@0: } else { truelight@0: bb &= ~2; truelight@0: state += 2; truelight@0: } celestar@1217: celestar@1217: bb |= 0x80; truelight@0: *b = bb; truelight@0: v->u.road.state = state; truelight@0: } truelight@0: } truelight@0: } truelight@193: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: static void DeleteStation(Station *st) truelight@0: { tron@555: Order order; celestar@1551: StationID index; truelight@0: st->xy = 0; truelight@0: truelight@0: DeleteName(st->string_id); truelight@0: MarkStationDirty(st); darkvater@243: _global_station_sort_dirty = true; // delete station, remove sign darkvater@65: InvalidateWindowClasses(WC_STATION_LIST); truelight@0: truelight@0: index = st->index; truelight@0: DeleteWindowById(WC_STATION_VIEW, index); truelight@1024: tron@555: order.type = OT_GOTO_STATION; tron@555: order.station = index; truelight@1024: DeleteDestinationFromVehicleOrder(order); truelight@1024: truelight@0: DeleteSubsidyWithStation(index); truelight@0: } truelight@0: tron@1093: void DeleteAllPlayerStations(void) truelight@0: { truelight@0: Station *st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy && st->owner < MAX_PLAYERS) truelight@193: DeleteStation(st); truelight@0: } truelight@0: } truelight@0: Darkvater@1664: static void CheckOrphanedSlots(const Station *st, RoadStopType rst) Darkvater@1664: { Darkvater@1664: RoadStop *rs; Darkvater@1664: int k; Darkvater@1664: Darkvater@1664: for (rs = GetPrimaryRoadStop(st, rst); rs != NULL; rs = rs->next) { Darkvater@1664: for (k = 0; k < NUM_SLOTS; k++) { Darkvater@1664: if (rs->slot[k] != INVALID_SLOT) { tron@1672: const Vehicle *v = GetVehicle(rs->slot[k]); tron@1672: tron@1672: if (v->type != VEH_Road || v->u.road.slot != rs) { tron@1672: DEBUG(ms, 0) ( tron@1672: "Multistop: Orphaned %s slot at 0x%X of station %d (don't panic)", tron@1672: (rst == RS_BUS) ? "bus" : "truck", rs->xy, st->index); Darkvater@1664: rs->slot[k] = INVALID_SLOT; Darkvater@1664: } Darkvater@1664: } Darkvater@1664: } Darkvater@1664: } Darkvater@1664: } Darkvater@1664: 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: truelight@0: if (st->facilities == 0) { truelight@0: if (++st->delete_ctr >= 8) truelight@0: DeleteStation(st); truelight@0: } celestar@1239: Darkvater@1664: // Here we saveguard against orphaned slots Darkvater@1664: CheckOrphanedSlots(st, RS_BUS); Darkvater@1664: CheckOrphanedSlots(st, RS_TRUCK); 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 byte _rating_boost[3] = { 0, 31, 63}; 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: truelight@0: { Darkvater@1457: if (st->owner != OWNER_NONE && !IS_HUMAN_PLAYER(st->owner)) truelight@0: rating += _rating_boost[_opt.diff.competitor_intelligence]; truelight@0: } truelight@0: Darkvater@1457: if (st->owner < MAX_PLAYERS && HASBIT(st->town->statues, st->owner)) truelight@0: rating += 26; truelight@0: truelight@0: { truelight@0: byte days = ge->days_since_pickup; truelight@0: if (st->last_vehicle != INVALID_VEHICLE && truelight@919: GetVehicle(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: { truelight@0: waiting = ge->waiting_acceptance & 0xFFF; 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: truelight@0: if (waiting_changed) truelight@0: ge->waiting_acceptance = (ge->waiting_acceptance & ~0xFFF) + waiting; truelight@0: } truelight@0: } truelight@0: } while (++ge != endof(st->goods)); truelight@193: truelight@0: index = st->index; truelight@0: truelight@0: if (waiting_changed) truelight@0: InvalidateWindow(WC_STATION_VIEW, index); truelight@0: else truelight@0: InvalidateWindowWidget(WC_STATION_VIEW, index, 5); 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: truelight@0: if (st->facilities == 0) truelight@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: truelight@0: if (b == 0) truelight@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: truelight@0: if (_game_mode == GM_EDITOR) truelight@0: return; truelight@0: truelight@0: i = _station_tick_ctr; truelight@1272: if (++_station_tick_ctr == GetStationPoolSize()) truelight@0: _station_tick_ctr = 0; truelight@0: truelight@919: st = GetStation(i); truelight@0: if (st->xy != 0) truelight@0: StationHandleBigTick(st); truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy != 0) truelight@0: StationHandleSmallTick(st); truelight@0: } truelight@0: truelight@0: } truelight@0: tron@1093: void StationMonthlyLoop(void) truelight@0: { truelight@0: } truelight@0: truelight@0: truelight@0: void ModifyStationRatingAround(TileIndex tile, byte owner, int amount, uint radius) truelight@0: { truelight@0: Station *st; truelight@0: GoodsEntry *ge; truelight@0: int i; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { tron@1245: if (st->xy != 0 && st->owner == owner && tron@1245: DistanceManhattan(tile, st->xy) <= radius) { truelight@0: ge = st->goods; truelight@0: for(i=0; i!=NUM_CARGO; i++,ge++) { 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: { truelight@193: st->goods[type].waiting_acceptance = truelight@193: (st->goods[type].waiting_acceptance & ~0xFFF) + truelight@0: min(0xFFF, (st->goods[type].waiting_acceptance & 0xFFF) + amount); 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); truelight@0: } truelight@0: truelight@0: int32 CmdRenameStation(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: StringID str,old_str; truelight@0: Station *st; truelight@0: tron@1774: if (!IsStationIndex(p1)) return CMD_ERROR; tron@1774: st = GetStation(p1); tron@1774: tron@1774: if (!IsValidStation(st) || !CheckOwnership(st->owner)) return CMD_ERROR; tron@1774: tron@1328: str = AllocateNameUnique((const char*)_decode_parameters, 6); truelight@0: if (str == 0) truelight@0: return CMD_ERROR; truelight@193: truelight@0: if (flags & DC_EXEC) { truelight@0: old_str = st->string_id; truelight@0: st->string_id = str; truelight@0: UpdateStationVirtCoord(st); truelight@0: DeleteName(old_str); darkvater@243: _station_sort_dirty[st->owner] = true; // rename a station truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: truelight@0: uint MoveGoodsToStation(uint tile, int w, int h, int type, uint amount) truelight@0: { truelight@0: Station *around_ptr[8]; celestar@1551: StationID around[8]; celestar@1551: StationID st_index; truelight@0: int i; truelight@0: Station *st; truelight@0: uint moved; truelight@0: uint best_rating, best_rating2; truelight@0: Station *st1, *st2; truelight@0: int t; Celestar@568: int rad=0; Celestar@568: int w_prod=0, h_prod=0; //width and height of the "producer" of the cargo Celestar@568: int x_min_prod, x_max_prod; //min and max coordinates of the producer Celestar@568: int y_min_prod, y_max_prod; //relative Celestar@568: int x_dist, y_dist; Celestar@568: int max_rad; Celestar@568: truelight@0: truelight@0: memset(around, 0xff, sizeof(around)); 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 { Celestar@568: w += 8; Celestar@568: h += 8; Celestar@568: max_rad = 4; Celestar@568: } Celestar@568: Celestar@568: BEGIN_TILE_LOOP(cur_tile, w, h, tile - TILE_XY(max_rad,max_rad)) truelight@0: cur_tile = TILE_MASK(cur_tile); tron@1035: if (IsTileType(cur_tile, MP_STATION)) { truelight@0: st_index = _map2[cur_tile]; truelight@0: for(i=0; i!=8; i++) { celestar@1527: if (around[i] == INVALID_STATION) { truelight@919: st = GetStation(st_index); matthijs@1751: if (!IsBuoy(st) && dominik@121: ( !st->town->exclusive_counter || (st->town->exclusivity == st->owner) ) && // check exclusive transport rights truelight@193: st->goods[type].rating != 0 && truelight@0: (!_patches.selectgoods || st->goods[type].last_speed) && // if last_speed is 0, no vehicle has been there. darkvater@22: ((st->facilities & (byte)~FACIL_BUS_STOP)!=0 || type==CT_PASSENGERS) && // if we have other fac. than a bus stop, or the cargo is passengers darkvater@22: ((st->facilities & (byte)~FACIL_TRUCK_STOP)!=0 || type!=CT_PASSENGERS)) { // if we have other fac. than a cargo bay or the cargo is not passengers Celestar@568: if (_patches.modified_catchment) { truelight@703: rad = FindCatchmentRadius(st); Celestar@568: x_min_prod = y_min_prod = 9; Celestar@568: x_max_prod = 8 + w_prod; Celestar@568: y_max_prod = 8 + h_prod; truelight@703: Celestar@568: x_dist = min(w_cur - x_min_prod, x_max_prod - w_cur); truelight@703: Celestar@568: if (w_cur < x_min_prod) { Celestar@568: x_dist = x_min_prod - w_cur; Celestar@568: } else { //save cycles Celestar@568: if (w_cur > x_max_prod) x_dist = w_cur - x_max_prod; Celestar@568: } truelight@703: Celestar@568: y_dist = min(h_cur - y_min_prod, y_max_prod - h_cur); Celestar@568: if (h_cur < y_min_prod) { Celestar@568: y_dist = y_min_prod - h_cur; Celestar@568: } else { Celestar@568: if (h_cur > y_max_prod) y_dist = h_cur - y_max_prod; Celestar@568: } truelight@703: Celestar@568: } else { Celestar@568: x_dist = y_dist = 0; Celestar@568: } truelight@703: Celestar@568: if ( !(x_dist > rad) && !(y_dist > rad) ) { Celestar@568: Celestar@568: around[i] = st_index; Celestar@568: around_ptr[i] = st; Celestar@568: } Celestar@568: } truelight@0: break; truelight@0: } else if (around[i] == st_index) truelight@0: break; truelight@0: } truelight@0: } Celestar@568: END_TILE_LOOP(cur_tile, w, h, tile - TILE_XY(max_rad, max_rad)) truelight@0: truelight@0: /* no stations around at all? */ celestar@1527: if (around[0] == INVALID_STATION) truelight@0: return 0; truelight@0: celestar@1527: if (around[1] == INVALID_STATION) { truelight@0: /* only one station around */ truelight@0: moved = (amount * around_ptr[0]->goods[type].rating >> 8) + 1; truelight@0: UpdateStationWaiting(around_ptr[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: celestar@1527: for( i = 0; i != 8 && around[i] != INVALID_STATION; i++) { truelight@0: if (around_ptr[i]->goods[type].rating >= best_rating) { truelight@0: best_rating2 = best_rating; truelight@0: st2 = st1; truelight@0: truelight@0: best_rating = around_ptr[i]->goods[type].rating; truelight@0: st1 = around_ptr[i]; truelight@0: } else if (around_ptr[i]->goods[type].rating >= best_rating2) { truelight@0: best_rating2 = around_ptr[i]->goods[type].rating; truelight@193: st2 = around_ptr[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) { truelight@0: moved = (t * best_rating >> 8) + 1; truelight@0: amount -= t; truelight@193: UpdateStationWaiting(st1, type, moved); truelight@0: } truelight@0: truelight@0: if (amount != 0) { truelight@0: moved += (amount = (amount * best_rating2 >> 8) + 1); truelight@0: UpdateStationWaiting(st2, type, amount); truelight@0: } truelight@0: truelight@0: return moved; truelight@0: } truelight@0: truelight@0: void BuildOilRig(uint tile) truelight@0: { truelight@0: Station *st; truelight@0: int j; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->xy == 0) { truelight@0: st->town = ClosestTownFromTile(tile, (uint)-1); truelight@0: st->sign.width_1 = 0; truelight@0: if (!GenerateStationName(st, tile, 2)) truelight@0: return; truelight@0: tron@1059: SetTileType(tile, MP_STATION); truelight@0: _map5[tile] = 0x4B; truelight@0: _map_owner[tile] = OWNER_NONE; truelight@0: _map3_lo[tile] = 0; truelight@0: _map3_hi[tile] = 0; truelight@0: _map2[tile] = st->index; truelight@0: darkvater@27: st->owner = OWNER_NONE; truelight@0: st->airport_flags = 0; truelight@0: st->airport_type = AT_OILRIG; truelight@0: st->xy = tile; celestar@1217: st->bus_stops = NULL; celestar@1217: st->truck_stops = NULL; truelight@0: st->airport_tile = tile; truelight@0: st->dock_tile = tile; truelight@0: st->train_tile = 0; 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->last_vehicle = INVALID_VEHICLE; truelight@0: st->facilities = FACIL_AIRPORT | FACIL_DOCK; truelight@71: st->build_date = _date; truelight@0: for(j=0; j!=NUM_CARGO; j++) { truelight@0: st->goods[j].waiting_acceptance = 0; truelight@0: st->goods[j].days_since_pickup = 0; truelight@1266: st->goods[j].enroute_from = INVALID_STATION; truelight@0: st->goods[j].rating = 175; truelight@0: st->goods[j].last_speed = 0; truelight@0: st->goods[j].last_age = 255; truelight@0: } truelight@0: truelight@0: UpdateStationVirtCoordDirty(st); truelight@0: UpdateStationAcceptance(st, false); truelight@193: return; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void DeleteOilRig(uint tile) truelight@0: { truelight@919: Station *st = GetStation(_map2[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: truelight@0: static void ChangeTileOwner_Station(uint tile, byte old_player, byte new_player) truelight@0: { truelight@0: if (_map_owner[tile] != old_player) truelight@0: return; truelight@0: truelight@0: if (new_player != 255) { truelight@919: Station *st = GetStation(_map2[tile]); truelight@0: _map_owner[tile] = new_player; truelight@0: st->owner = new_player; darkvater@243: _global_station_sort_dirty = true; // transfer ownership of station to another player darkvater@65: InvalidateWindowClasses(WC_STATION_LIST); truelight@0: } else { truelight@0: DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } truelight@0: } truelight@0: truelight@0: static int32 ClearTile_Station(uint tile, byte flags) { truelight@0: byte m5 = _map5[tile]; truelight@0: Station *st; truelight@0: truelight@0: if (flags & DC_AUTO) { truelight@0: if (m5 < 8) return_cmd_error(STR_300B_MUST_DEMOLISH_RAILROAD); truelight@0: if (m5 < 0x43 || (m5 >= 83 && m5 <= 114)) return_cmd_error(STR_300E_MUST_DEMOLISH_AIRPORT_FIRST); truelight@0: if (m5 < 0x47) return_cmd_error(STR_3047_MUST_DEMOLISH_TRUCK_STATION); truelight@0: if (m5 < 0x4B) return_cmd_error(STR_3046_MUST_DEMOLISH_BUS_STATION); truelight@0: if (m5 == 0x52) return_cmd_error(STR_306A_BUOY_IN_THE_WAY); truelight@0: if (m5 != 0x4B && m5 < 0x53) return_cmd_error(STR_304D_MUST_DEMOLISH_DOCK_FIRST); tron@534: SetDParam(0, STR_4807_OIL_RIG); truelight@0: return_cmd_error(STR_4800_IN_THE_WAY); truelight@0: } truelight@0: truelight@919: st = GetStation(_map2[tile]); truelight@0: truelight@0: if (m5 < 8) darkvater@149: return RemoveRailroadStation(st, tile, flags); truelight@0: truelight@0: // original airports < 67, new airports between 83 - 114 truelight@0: if (m5 < 0x43 || ( m5 >= 83 && m5 <= 114) ) truelight@0: return RemoveAirport(st, flags); truelight@193: truelight@0: if (m5 < 0x4B) celestar@1217: return RemoveRoadStop(st, flags, tile); truelight@0: truelight@0: if (m5 == 0x52) truelight@0: return RemoveBuoy(st, flags); truelight@0: truelight@0: if (m5 != 0x4B && m5 < 0x53) truelight@0: return RemoveDock(st, flags); truelight@0: truelight@0: return CMD_ERROR; truelight@0: truelight@0: } truelight@0: tron@1093: void InitializeStations(void) truelight@0: { truelight@1272: /* Clean the station pool and create 1 block in it */ truelight@1272: CleanPool(&_station_pool); truelight@1272: AddBlockToPool(&_station_pool); truelight@919: truelight@1284: /* Clean the roadstop pool and create 1 block in it */ truelight@1284: CleanPool(&_roadstop_pool); truelight@1284: AddBlockToPool(&_roadstop_pool); darkvater@243: truelight@0: _station_tick_ctr = 0; darkvater@243: darkvater@243: // set stations to be sorted on load of savegame darkvater@243: memset(_station_sort_dirty, true, sizeof(_station_sort_dirty)); darkvater@243: _global_station_sort_dirty = true; // load of savegame truelight@0: } truelight@0: truelight@0: truelight@0: const TileTypeProcs _tile_type_station_procs = { truelight@0: DrawTile_Station, /* draw_tile_proc */ truelight@0: GetSlopeZ_Station, /* get_slope_z_proc */ truelight@0: ClearTile_Station, /* clear_tile_proc */ truelight@0: GetAcceptedCargo_Station, /* get_accepted_cargo_proc */ truelight@0: GetTileDesc_Station, /* get_tile_desc_proc */ truelight@0: GetTileTrackStatus_Station, /* get_tile_track_status_proc */ truelight@0: ClickTile_Station, /* click_tile_proc */ truelight@0: AnimateTile_Station, /* animate_tile_proc */ truelight@0: TileLoop_Station, /* tile_loop_clear */ truelight@0: ChangeTileOwner_Station, /* change_tile_owner_clear */ truelight@0: NULL, /* get_produced_cargo_proc */ truelight@0: VehicleEnter_Station, /* vehicle_enter_tile_proc */ truelight@0: NULL, /* vehicle_leave_tile_proc */ dominik@39: GetSlopeTileh_Station, /* get_slope_tileh_proc */ truelight@0: }; truelight@0: celestar@1217: static const byte _roadstop_desc[] = { celestar@1217: SLE_VAR(RoadStop,xy, SLE_UINT32), celestar@1217: SLE_VAR(RoadStop,used, SLE_UINT8), celestar@1217: SLE_VAR(RoadStop,status, SLE_UINT8), truelight@1285: /* Index was saved in some versions, but this is not needed */ truelight@1285: SLE_CONDARR(NullStruct,null,SLE_FILE_U32 | SLE_VAR_NULL, 1, 0, 8), celestar@1217: SLE_VAR(RoadStop,station, SLE_UINT16), celestar@1217: SLE_VAR(RoadStop,type, SLE_UINT8), celestar@1217: celestar@1217: SLE_REF(RoadStop,next, REF_ROADSTOPS), celestar@1217: SLE_REF(RoadStop,prev, REF_ROADSTOPS), celestar@1217: truelight@1284: SLE_ARR(RoadStop,slot, SLE_UINT16, NUM_SLOTS), celestar@1217: celestar@1217: SLE_END() celestar@1217: }; truelight@0: truelight@0: static const byte _station_desc[] = { tron@1174: SLE_CONDVAR(Station, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Station, xy, SLE_UINT32, 6, 255), celestar@1217: SLE_CONDVAR(Station, bus_tile_obsolete, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), celestar@1217: SLE_CONDVAR(Station, lorry_tile_obsolete, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Station, train_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Station, train_tile, SLE_UINT32, 6, 255), tron@1174: SLE_CONDVAR(Station, airport_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Station, airport_tile, SLE_UINT32, 6, 255), tron@1174: SLE_CONDVAR(Station, dock_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Station, dock_tile, SLE_UINT32, 6, 255), truelight@0: SLE_REF(Station,town, REF_TOWN), truelight@0: SLE_VAR(Station,trainst_w, SLE_UINT8), truelight@0: SLE_CONDVAR(Station,trainst_h, SLE_UINT8, 2, 255), truelight@0: truelight@0: // alpha_order was stored here in savegame format 0 - 3 truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 1, 0, 3), truelight@0: truelight@0: SLE_VAR(Station,string_id, SLE_STRINGID), truelight@0: SLE_VAR(Station,had_vehicle_of_type,SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Station,time_since_load, SLE_UINT8), truelight@0: SLE_VAR(Station,time_since_unload, SLE_UINT8), truelight@0: SLE_VAR(Station,delete_ctr, SLE_UINT8), truelight@0: SLE_VAR(Station,owner, SLE_UINT8), truelight@0: SLE_VAR(Station,facilities, SLE_UINT8), truelight@0: SLE_VAR(Station,airport_type, SLE_UINT8), celestar@1217: celestar@1217: // truck/bus_stop_status was stored here in savegame format 0 - 6 celestar@1217: SLE_CONDVAR(Station,truck_stop_status_obsolete, SLE_UINT8, 0, 5), celestar@1217: 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 dominik@123: SLE_CONDVAR(Station,blocked_months_obsolete, SLE_UINT8, 0, 4), truelight@0: truelight@0: SLE_CONDVAR(Station,airport_flags, SLE_VAR_U32 | SLE_FILE_U16, 0, 2), truelight@0: SLE_CONDVAR(Station,airport_flags, SLE_UINT32, 3, 255), truelight@0: truelight@0: SLE_VAR(Station,last_vehicle, SLE_UINT16), truelight@0: truelight@0: SLE_CONDVAR(Station,class_id, SLE_UINT8, 3, 255), truelight@0: SLE_CONDVAR(Station,stat_id, SLE_UINT8, 3, 255), truelight@0: SLE_CONDVAR(Station,build_date, SLE_UINT16, 3, 255), truelight@0: celestar@1217: SLE_CONDREF(Station,bus_stops, REF_ROADSTOPS, 6, 255), celestar@1217: SLE_CONDREF(Station,truck_stops, REF_ROADSTOPS, 6, 255), celestar@1217: celestar@1217: // reserve extra space in savegame here. (currently 28 bytes) truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 32, 2, 255), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: static const byte _goods_desc[] = { truelight@0: SLE_VAR(GoodsEntry,waiting_acceptance,SLE_UINT16), truelight@0: SLE_VAR(GoodsEntry,days_since_pickup, SLE_UINT8), truelight@0: SLE_VAR(GoodsEntry,rating, SLE_UINT8), truelight@1266: SLE_CONDVAR(GoodsEntry,enroute_from, SLE_FILE_U8 | SLE_VAR_U16, 0, 6), truelight@1266: SLE_CONDVAR(GoodsEntry,enroute_from, SLE_UINT16, 7, 255), truelight@0: SLE_VAR(GoodsEntry,enroute_time, SLE_UINT8), truelight@0: SLE_VAR(GoodsEntry,last_speed, SLE_UINT8), truelight@0: SLE_VAR(GoodsEntry,last_age, SLE_UINT8), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: truelight@0: truelight@0: static void SaveLoad_STNS(Station *st) truelight@0: { truelight@0: int 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@1266: if (_sl.full_version < 0x700 && st->goods[i].enroute_from == 0xFF) truelight@1266: st->goods[i].enroute_from = 0xFFFF; truelight@1266: } 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@0: if (st->xy != 0) { truelight@0: SlSetArrayIndex(st->index); truelight@0: SlAutolength((AutolengthProc*)SaveLoad_STNS, st); truelight@0: } 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: truelight@1272: 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 truelight@0: if (st->train_tile && st->trainst_h == 0) { truelight@0: int w = st->trainst_w >> 4; truelight@0: int h = st->trainst_w & 0xF; truelight@0: if (_map5[st->train_tile]&1) intswap(w,h); truelight@0: st->trainst_w = w; truelight@0: st->trainst_h = h; truelight@0: } celestar@1217: celestar@1217: if (_sl.full_version < 0x600) { celestar@1217: /* Convert old bus and truck tile to new-ones */ 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 */ truelight@1272: if (_station_tick_ctr > GetStationPoolSize()) truelight@919: _station_tick_ctr = 0; truelight@0: } truelight@0: celestar@1217: static void Save_ROADSTOP( void ) celestar@1217: { truelight@1284: RoadStop *rs; truelight@1284: truelight@1284: FOR_ALL_ROADSTOPS(rs) { truelight@1284: if (rs->used) { truelight@1284: SlSetArrayIndex(rs->index); truelight@1284: SlObject(rs, _roadstop_desc); celestar@1217: } celestar@1217: } celestar@1217: } celestar@1217: celestar@1217: static void Load_ROADSTOP( void ) celestar@1217: { celestar@1217: int index; celestar@1217: truelight@1284: while ((index = SlIterateArray()) != -1) { truelight@1284: RoadStop *rs; truelight@1284: truelight@1284: 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@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: }; truelight@0: