57 #include "table/strings.h"
109 if (!T::IsValidID(t))
continue;
111 if (closest_station == INVALID_STATION) {
113 }
else if (closest_station != t) {
118 *st = (closest_station == INVALID_STATION) ? NULL : T::Get(closest_station);
139 for (
int dx = -3; dx <= 3; dx++) {
140 for (
int dy = -3; dy <= 3; dy++) {
196 #define M(x) ((x) - STR_SV_STNAME)
201 STATIONNAMING_AIRPORT,
202 STATIONNAMING_OILRIG,
204 STATIONNAMING_HELIPORT,
229 if (
GetIndustrySpec(indtype)->station_name == STR_UNDEFINED)
return false;
233 sni->
free_names &= ~(1 << M(STR_SV_STNAME_OILFIELD) | 1 << M(STR_SV_STNAME_MINES));
239 static const uint32 _gen_station_name_bits[] = {
242 1U << M(STR_SV_STNAME_AIRPORT),
243 1U << M(STR_SV_STNAME_OILFIELD),
244 1U << M(STR_SV_STNAME_DOCKS),
245 1U << M(STR_SV_STNAME_HELIPORT),
249 uint32 free_names = UINT32_MAX;
252 memset(indtypes, 0,
sizeof(indtypes));
255 FOR_ALL_STATIONS(s) {
256 if (s != st && s->
town == t) {
257 if (s->
indtype != IT_INVALID) {
260 if (name != STR_UNDEFINED) {
271 if (str == M(STR_SV_STNAME_FOREST)) {
272 str = M(STR_SV_STNAME_WOODS);
288 return STR_SV_STNAME_FALLBACK;
296 uint32 tmp = free_names & _gen_station_name_bits[name_class];
300 if (
HasBit(free_names, M(STR_SV_STNAME_MINES))) {
302 return STR_SV_STNAME_MINES;
308 if (
HasBit(free_names, M(STR_SV_STNAME)))
return STR_SV_STNAME;
310 if (
HasBit(free_names, M(STR_SV_STNAME_CENTRAL)))
return STR_SV_STNAME_CENTRAL;
314 if (
HasBit(free_names, M(STR_SV_STNAME_LAKESIDE)) &&
317 return STR_SV_STNAME_LAKESIDE;
321 if (
HasBit(free_names, M(STR_SV_STNAME_WOODS)) && (
332 if (
HasBit(free_names, M(STR_SV_STNAME_VALLEY)))
return STR_SV_STNAME_VALLEY;
334 if (
HasBit(free_names, M(STR_SV_STNAME_HEIGHTS)))
return STR_SV_STNAME_HEIGHTS;
338 static const int8 _direction_and_table[] = {
339 ~( (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
340 ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
341 ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
342 ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) ),
345 free_names &= _direction_and_table[
349 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));
350 return (tmp == 0) ? STR_SV_STNAME_FALLBACK : (STR_SV_STNAME +
FindFirstBit(tmp));
365 FOR_ALL_STATIONS(st) {
369 if (cur_dist < threshold) {
370 threshold = cur_dist;
387 case STATION_AIRPORT:
404 default: NOT_REACHED();
418 pt.y -= 32 * ZOOM_LVL_BASE;
433 FOR_ALL_BASE_STATIONS(st) {
459 for (uint i = 0; i < num_items; i++) {
484 int x1 =
max(x - rad, 0);
487 int y1 =
max(y - rad, 0);
507 FOR_ALL_INDUSTRIES(i) {
530 if (always_accepted != NULL) *always_accepted = 0;
539 int x1 =
max(x - rad, 0);
540 int y1 =
max(y - rad, 0);
547 for (
int yc = y1; yc != y2; yc++) {
548 for (
int xc = x1; xc != x2; xc++) {
550 AddAcceptedCargo(tile, acceptance, always_accepted);
569 if (!st->
rect.IsEmpty()) {
581 uint amt = acceptance[i];
599 if (old_acc == new_acc)
return;
605 static const StringID accept_msg[] = {
606 STR_NEWS_STATION_NOW_ACCEPTS_CARGO,
607 STR_NEWS_STATION_NOW_ACCEPTS_CARGO_AND_CARGO,
609 static const StringID reject_msg[] = {
610 STR_NEWS_STATION_NO_LONGER_ACCEPTS_CARGO,
611 STR_NEWS_STATION_NO_LONGER_ACCEPTS_CARGO_OR_CARGO,
625 accepts[num_acc++] = i;
630 rejects[num_rej++] = i;
644 static void UpdateStationSignCoord(
BaseStation *st)
648 if (r->IsEmpty())
return;
682 CommandCost ret = (*st)->rect.BeforeAddRect(area.
tile, area.
w, area.
h, StationRect::ADD_TEST);
683 if (ret.
Failed())
return ret;
692 (*st)->string_id = GenerateStationName(*st, area.
tile, name_class);
715 UpdateStationSignCoord(st);
736 if (ret.
Failed())
return ret;
759 cost.
AddCost(_price[PR_BUILD_FOUNDATION]);
766 }
else if (allowed_z != flat_z) {
786 if (ret.
Failed())
return ret;
790 if (ret.
Failed())
return ret;
811 static CommandCost CheckFlatLandRailStation(
TileArea tile_area,
DoCommandFlag flags,
Axis axis, StationID *station,
RailType rt,
SmallVector<Train *, 4> &affected_vehicles,
StationClassID spec_class, byte spec_index, byte plat_len, byte numtracks)
815 uint invalid_dirs = 5 << axis;
822 if (ret.
Failed())
return ret;
828 if (ret.
Failed())
return ret;
839 if (*station == INVALID_STATION) {
841 }
else if (*station != st) {
866 *affected_vehicles.
Append() = v;
870 if (ret.
Failed())
return ret;
877 if (ret.
Failed())
return ret;
904 if (ret.
Failed())
return ret;
923 if (*station == INVALID_STATION) {
925 }
else if (*station != st) {
949 uint num_roadbits = 0;
950 if (build_over_road) {
958 if (ret.
Failed())
return ret;
968 if (ret.
Failed())
return ret;
977 if (ret.
Failed())
return ret;
981 uint roadbits_to_build =
CountBits(rts) * 2 - num_roadbits;
982 cost.
AddCost(_price[PR_BUILD_ROAD] * roadbits_to_build);
1015 static inline byte *CreateSingle(byte *layout,
int n)
1018 do *layout++ = 0;
while (--i);
1019 layout[((n - 1) >> 1) - n] = 2;
1023 static inline byte *CreateMulti(byte *layout,
int n, byte b)
1026 do *layout++ = b;
while (--i);
1029 layout[n - 1 - n] = 0;
1043 if (statspec != NULL && statspec->lengths >= plat_len &&
1044 statspec->platforms[plat_len - 1] >= numtracks &&
1045 statspec->layouts[plat_len - 1][numtracks - 1]) {
1047 memcpy(layout, statspec->layouts[plat_len - 1][numtracks - 1],
1048 plat_len * numtracks);
1052 if (plat_len == 1) {
1053 CreateSingle(layout, numtracks);
1055 if (numtracks & 1) layout = CreateSingle(layout, plat_len);
1058 while (--numtracks >= 0) {
1059 layout = CreateMulti(layout, plat_len, 4);
1060 layout = CreateMulti(layout, plat_len, 6);
1076 template <
class T, StringID error_message>
1079 assert(*st == NULL);
1080 bool check_surrounding =
true;
1083 if (existing_station != INVALID_STATION) {
1084 if (adjacent && existing_station != station_to_join) {
1091 *st = T::GetIfValid(existing_station);
1092 check_surrounding = (*st == NULL);
1097 if (adjacent) check_surrounding =
false;
1101 if (check_surrounding) {
1104 if (ret.
Failed())
return ret;
1108 if (*st == NULL && station_to_join != INVALID_STATION) *st = T::GetIfValid(station_to_join);
1124 return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_RAILWAY_STATION_FIRST>(existing_station, station_to_join, adjacent, ta, st);
1138 return FindJoiningBaseStation<Waypoint, STR_ERROR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
1161 RailType rt = Extract<RailType, 0, 4>(p1);
1162 Axis axis = Extract<Axis, 4, 1>(p1);
1163 byte numtracks =
GB(p1, 8, 8);
1164 byte plat_len =
GB(p1, 16, 8);
1165 bool adjacent =
HasBit(p1, 24);
1168 byte spec_index =
GB(p2, 8, 8);
1169 StationID station_to_join =
GB(p2, 16, 16);
1173 if (ret.
Failed())
return ret;
1180 if (plat_len == 0 || numtracks == 0)
return CMD_ERROR;
1191 bool reuse = (station_to_join != NEW_STATION);
1192 if (!reuse) station_to_join = INVALID_STATION;
1193 bool distant_join = (station_to_join != INVALID_STATION);
1200 TileArea new_location(tile_org, w_org, h_org);
1203 StationID est = INVALID_STATION;
1207 if (cost.
Failed())
return cost;
1209 cost.
AddCost((numtracks * _price[PR_BUILD_STATION_RAIL] + _price[PR_BUILD_STATION_RAIL_LENGTH]) * plat_len);
1214 if (ret.
Failed())
return ret;
1216 ret =
BuildStationPart(&st, flags, reuse, new_location, STATIONNAMING_RAIL);
1217 if (ret.
Failed())
return ret;
1221 if (ret.
Failed())
return ret;
1229 if (statspec != NULL) {
1244 if (flags & DC_EXEC) {
1247 byte numtracks_orig;
1253 st->
rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TRY);
1255 if (statspec != NULL) {
1264 layout_ptr =
AllocaM(byte, numtracks * plat_len);
1267 numtracks_orig = numtracks;
1275 byte layout = *layout_ptr++;
1281 *affected_vehicles.
Append() = v;
1283 for (; v->
Next() != NULL; v = v->
Next()) { }
1308 if (statspec != NULL) {
1323 TriggerStationAnimation(st, tile,
SAT_BUILT);
1331 }
while (--numtracks);
1333 for (uint i = 0; i < affected_vehicles.
Length(); ++i) {
1335 Train *v = affected_vehicles[i];
1338 for (; v->
Next() != NULL; v = v->
Next()) { }
1345 update_reservation_area =
TileArea(tile_org, 1, numtracks_orig);
1347 update_reservation_area =
TileArea(tile_org, numtracks_orig, 1);
1361 platform_begin = next_tile;
1364 platform_end = next_tile;
1368 bool reservation =
false;
1369 for (
TileIndex t = platform_begin; !reservation && t <= platform_end; t += tile_offset) {
1391 static void MakeRailStationAreaSmaller(
BaseStation *st)
1398 if (ta.
w != 0 && ta.
h != 0) {
1472 if (ret.
Failed())
continue;
1475 T *st = T::GetByTile(tile);
1476 if (st == NULL)
continue;
1481 if (ret.
Failed())
continue;
1490 total_cost.
AddCost(-_price[PR_CLEAR_RAIL]);
1508 for (; temp->
Next() != NULL; temp = temp->
Next()) { }
1516 DoClearSquare(tile);
1522 st->rect.AfterRemoveTile(st, tile);
1528 affected_stations.
Include(st);
1534 for (; v->
Next() != NULL; v = v->
Next()) { }
1540 if (quantity == 0)
return error.
Failed() ? error :
CommandCost(STR_ERROR_THERE_IS_NO_STATION);
1542 for (T **stp = affected_stations.
Begin(); stp != affected_stations.
End(); stp++) {
1548 MakeRailStationAreaSmaller(st);
1549 UpdateStationSignCoord(st);
1555 st->UpdateVirtCoord();
1560 total_cost.
AddCost(quantity * removal_cost);
1584 if (ret.
Failed())
return ret;
1587 for (
Station **stp = affected_stations.
Begin(); stp != affected_stations.
End(); stp++) {
1636 if (ret.
Failed())
return ret;
1642 assert(ta.
w != 0 && ta.
h != 0);
1648 if (st->TileBelongsToRailStation(tile)) {
1651 if (ret.
Failed())
return ret;
1706 if (*primary_stop == NULL) {
1708 return primary_stop;
1712 while (stop->
next != NULL) stop = stop->
next;
1730 return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_ROAD_STOP_FIRST>(existing_stop, station_to_join, adjacent, ta, st);
1751 bool type =
HasBit(p2, 0);
1752 bool is_drive_through =
HasBit(p2, 1);
1753 RoadTypes rts = Extract<RoadTypes, 2, 2>(p2);
1754 StationID station_to_join =
GB(p2, 16, 16);
1755 bool reuse = (station_to_join != NEW_STATION);
1756 if (!reuse) station_to_join = INVALID_STATION;
1757 bool distant_join = (station_to_join != INVALID_STATION);
1759 uint8 width = (uint8)
GB(p1, 0, 8);
1760 uint8 lenght = (uint8)
GB(p1, 8, 8);
1765 if (width == 0 || lenght == 0)
return CMD_ERROR;
1769 TileArea roadstop_area(tile, width, lenght);
1780 if (is_drive_through) {
1782 axis = Extract<Axis, 6, 1>(p2);
1786 ddir = Extract<DiagDirection, 6, 2>(p2);
1791 if (ret.
Failed())
return ret;
1795 StationID est = INVALID_STATION;
1796 ret =
CheckFlatLandRoadStop(roadstop_area, flags, is_drive_through ? 5 << axis : 1 << ddir, is_drive_through, type, axis, &est, rts);
1797 if (ret.
Failed())
return ret;
1802 if (ret.
Failed())
return ret;
1807 ret =
BuildStationPart(&st, flags, reuse, roadstop_area, STATIONNAMING_ROAD);
1808 if (ret.
Failed())
return ret;
1824 *currstop = road_stop;
1835 st->
rect.BeforeAddTile(cur_tile, StationRect::ADD_TRY);
1838 if (is_drive_through) {
1905 if (ret.
Failed())
return ret;
1920 assert(cur_stop != NULL);
1928 if (ret.
Failed())
return ret;
1932 if (*primary_stop == cur_stop) {
1934 *primary_stop = cur_stop->
next;
1936 if (*primary_stop == NULL) {
1942 while (pred->
next != cur_stop) pred = pred->
next;
1961 DoClearSquare(tile);
1969 FOR_ALL_ROADVEHICLES(v) {
1976 st->
rect.AfterRemoveTile(st, tile);
2007 uint8 width = (uint8)
GB(p1, 0, 8);
2008 uint8 height = (uint8)
GB(p1, 8, 8);
2011 if (width == 0 || height == 0)
return CMD_ERROR;
2015 TileArea roadstop_area(tile, width, height);
2018 CommandCost last_error(STR_ERROR_THERE_IS_NO_STATION);
2019 bool had_success =
false;
2043 if ((flags &
DC_EXEC) && is_drive_through) {
2045 road_owner, tram_owner);
2059 return had_success ? cost : last_error;
2070 uint mindist = UINT_MAX;
2104 uint noise_reduction = distance / town_tolerance_distance;
2120 Town *t, *nearest = NULL;
2122 uint mindist = UINT_MAX - add;
2128 if (dist < mindist) {
2147 FOR_ALL_STATIONS(st) {
2172 StationID station_to_join =
GB(p2, 16, 16);
2173 bool reuse = (station_to_join != NEW_STATION);
2174 if (!reuse) station_to_join = INVALID_STATION;
2175 bool distant_join = (station_to_join != INVALID_STATION);
2176 byte airport_type =
GB(p1, 0, 8);
2177 byte layout =
GB(p1, 8, 8);
2184 if (ret.
Failed())
return ret;
2201 if (cost.
Failed())
return cost;
2209 StringID authority_refuse_message = STR_NULL;
2210 Town *authority_refuse_town = NULL;
2215 authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_NOISE;
2216 authority_refuse_town = nearest;
2222 FOR_ALL_STATIONS(st) {
2226 authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_AIRPORT;
2227 authority_refuse_town = t;
2231 if (authority_refuse_message != STR_NULL) {
2238 if (ret.
Failed())
return ret;
2244 if (ret.
Failed())
return ret;
2251 cost.
AddCost(_price[PR_BUILD_STATION_AIRPORT]);
2264 st->
rect.BeforeAddRect(tile, w, h, StationRect::ADD_TRY);
2276 AirportTileAnimationTrigger(st, iter,
AAT_BUILT);
2311 if (ret.
Failed())
return ret;
2335 if (!st->TileBelongsToAirport(tile_cur))
continue;
2338 if (ret.
Failed())
return ret;
2340 cost.
AddCost(_price[PR_CLEAR_STATION_AIRPORT]);
2342 if (flags & DC_EXEC) {
2345 DoClearSquare(tile_cur);
2350 if (flags & DC_EXEC) {
2400 if (ret.
Failed())
return ret;
2419 if ((v->
owner == company) == include_company) {
2421 FOR_VEHICLE_ORDERS(v, order) {
2437 static const byte _dock_w_chk[4] = { 2, 1, 2, 1 };
2438 static const byte _dock_h_chk[4] = { 1, 2, 1, 2 };
2451 StationID station_to_join =
GB(p2, 16, 16);
2452 bool reuse = (station_to_join != NEW_STATION);
2453 if (!reuse) station_to_join = INVALID_STATION;
2454 bool distant_join = (station_to_join != INVALID_STATION);
2466 if (ret.
Failed())
return ret;
2471 if (ret.
Failed())
return ret;
2485 if (ret.
Failed())
return ret;
2493 _dock_w_chk[direction], _dock_h_chk[direction]);
2498 if (ret.
Failed())
return ret;
2504 if (ret.
Failed())
return ret;
2512 st->
rect.BeforeAddRect(dock_area.
tile, dock_area.
w, dock_area.
h, StationRect::ADD_TRY);
2545 if (ret.
Failed())
return ret;
2554 if (ret.
Failed())
return ret;
2557 DoClearSquare(tile1);
2559 MakeWaterKeepingClass(tile2, st->
owner);
2561 st->
rect.AfterRemoveTile(st, tile1);
2562 st->
rect.AfterRemoveTile(st, tile2);
2599 return &_station_display_datas[st][gfx];
2615 case SPR_RAIL_TRACK_X:
2616 snow_desert =
false;
2617 *overlay_offset =
RTO_X;
2620 case SPR_RAIL_TRACK_Y:
2621 snow_desert =
false;
2622 *overlay_offset =
RTO_Y;
2625 case SPR_RAIL_TRACK_X_SNOW:
2627 *overlay_offset =
RTO_X;
2630 case SPR_RAIL_TRACK_Y_SNOW:
2632 *overlay_offset =
RTO_Y;
2655 *ground = snow_desert ? SPR_FLAT_SNOW_DESERT_TILE : SPR_FLAT_GRASS_TILE;
2659 static void DrawTile_Station(
TileInfo *ti)
2667 uint32 relocation = 0;
2668 uint32 ground_relocation = 0;
2671 uint tile_layout = 0;
2683 if (statspec != NULL) {
2720 case APT_RADAR_GRASS_FENCE_SW:
2723 case APT_GRASS_FENCE_NE_FLAG:
2726 case APT_RADAR_FENCE_SW:
2729 case APT_RADAR_FENCE_NE:
2732 case APT_GRASS_FENCE_NE_FLAG_2:
2742 palette = COMPANY_SPRITE_COLOUR(owner);
2745 palette = PALETTE_TO_GREY;
2748 if (layout == NULL && (t == NULL || t->
seq == NULL)) t = GetStationTileLayout(
GetStationType(ti->
tile), gfx);
2758 if (!HasFoundationNW(ti->
tile, slope, z))
SetBit(edge_info, 0);
2759 if (!HasFoundationNE(ti->
tile, slope, z))
SetBit(edge_info, 1);
2761 if (image == 0)
goto draw_default_foundation;
2766 static const uint8 foundation_parts[] = {
2779 static const uint8 composite_foundation_parts[] = {
2781 0x00, 0xD1, 0xE4, 0xE0,
2783 0xCA, 0xC9, 0xC4, 0xC0,
2785 0xD2, 0x91, 0xE4, 0xA0,
2790 uint8 parts = composite_foundation_parts[ti->
tileh];
2801 goto draw_default_foundation;
2805 for (
int i = 0; i < 8; i++) {
2816 draw_default_foundation:
2822 DrawWaterClassGround(ti);
2824 if (sprite != 0) total_offset = sprite - SPR_IMG_BUOY;
2827 DrawWaterClassGround(ti);
2833 DrawShoreTile(ti->
tileh);
2835 DrawClearLandTile(ti, 3);
2839 if (layout != NULL) {
2849 t = &tmp_rail_layout;
2851 }
else if (statspec != NULL) {
2903 int32 total_offset = 0;
2918 DrawSprite(ground + overlay_offset, PAL_NONE, x, y);
2931 static int GetSlopePixelZ_Station(
TileIndex tile, uint x, uint y)
2956 td->
owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
2957 td->
owner[i] = road_owner;
2961 td->
owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
2962 td->
owner[i] = tram_owner;
3004 default: NOT_REACHED();
3005 case STATION_RAIL: str = STR_LAI_STATION_DESCRIPTION_RAILROAD_STATION;
break;
3006 case STATION_AIRPORT:
3007 str = (
IsHangar(tile) ? STR_LAI_STATION_DESCRIPTION_AIRCRAFT_HANGAR : STR_LAI_STATION_DESCRIPTION_AIRPORT);
3009 case STATION_TRUCK: str = STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA;
break;
3010 case STATION_BUS: str = STR_LAI_STATION_DESCRIPTION_BUS_STATION;
break;
3011 case STATION_OILRIG: str = STR_INDUSTRY_NAME_OIL_RIG;
break;
3012 case STATION_DOCK: str = STR_LAI_STATION_DESCRIPTION_SHIP_DOCK;
break;
3013 case STATION_BUOY: str = STR_LAI_STATION_DESCRIPTION_BUOY;
break;
3014 case STATION_WAYPOINT: str = STR_LAI_STATION_DESCRIPTION_WAYPOINT;
break;
3063 static void TileLoop_Station(
TileIndex tile)
3068 case STATION_AIRPORT:
3075 case STATION_OILRIG:
3085 static void AnimateTile_Station(
TileIndex tile)
3088 AnimateStationTile(tile);
3093 AnimateAirportTile(tile);
3098 static bool ClickTile_Station(
TileIndex tile)
3142 }
else if (x < stop) {
3144 uint16 spd =
max(0, (stop - x) * 20 - 15);
3145 if (spd < v->cur_speed) v->
cur_speed = spd;
3174 if (cargoes == 0)
return;
3213 static inline void byte_inc_sat(byte *p)
3230 StationCargoAmountMap waiting_per_source;
3232 for (StationCargoAmountMap::iterator i(waiting_per_source.begin()); i != waiting_per_source.end(); ++i) {
3234 if (source_station == NULL)
continue;
3241 static void UpdateStationRating(
Station *st)
3243 bool waiting_changed =
false;
3245 byte_inc_sat(&st->time_since_load);
3246 byte_inc_sat(&st->time_since_unload);
3249 FOR_ALL_CARGOSPECS(cs) {
3265 waiting_changed =
true;
3284 uint waiting_avg = waiting / (num_dests + 1);
3295 uint32 var10 = (st->last_vehicle_type ==
VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10);
3299 rating =
GB(callback, 0, 14);
3302 if (
HasBit(callback, 14)) rating -= 0x4000;
3308 if (b >= 0) rating += b >> 2;
3311 if (st->last_vehicle_type ==
VEH_SHIP) waittime >>= 2;
3313 (rating += 25, waittime > 12) ||
3314 (rating += 25, waittime > 6) ||
3315 (rating += 45, waittime > 3) ||
3316 (rating += 35,
true);
3323 (rating += 10,
true);
3330 (rating += 10, age >= 2) ||
3331 (rating += 10, age >= 1) ||
3332 (rating += 13,
true);
3342 if (rating <= 64 && waiting_avg >= 100) {
3343 int dec = Random() & 0x1F;
3344 if (waiting_avg < 200) dec &= 7;
3345 waiting -= (dec + 1) * num_dests;
3346 waiting_changed =
true;
3350 if (rating <= 127 && waiting != 0) {
3351 uint32 r = Random();
3352 if (rating <= (
int)
GB(r, 0, 7)) {
3354 waiting =
max((
int)waiting - (
int)((
GB(r, 8, 2) - 1) * num_dests), 0);
3355 waiting_changed =
true;
3362 static const uint WAITING_CARGO_THRESHOLD = 1 << 12;
3363 static const uint WAITING_CARGO_CUT_FACTOR = 1 << 6;
3364 static const uint MAX_WAITING_CARGO = 1 << 15;
3366 if (waiting > WAITING_CARGO_THRESHOLD) {
3367 uint difference = waiting - WAITING_CARGO_THRESHOLD;
3368 waiting -= (difference / WAITING_CARGO_CUT_FACTOR);
3370 waiting =
min(waiting, MAX_WAITING_CARGO);
3371 waiting_changed =
true;
3376 if (waiting_changed && waiting < ge->cargo.AvailableCount()) {
3390 StationID index = st->
index;
3391 if (waiting_changed) {
3414 for (std::list<Vehicle *>::iterator it(st->loading_vehicles.begin()); it != st->loading_vehicles.end(); ++it) {
3415 for (
Vehicle *v = *it; v != NULL; v = v->
Next()) {
3435 if (lg == NULL)
continue;
3438 Edge edge = it->second;
3447 bool updated =
false;
3449 FOR_ALL_ORDER_LISTS(l) {
3450 bool found_from =
false;
3451 bool found_to =
false;
3453 if (!order->IsType(OT_GOTO_STATION) && !order->IsType(OT_IMPLICIT))
continue;
3454 if (order->GetDestination() == from->
index) {
3456 if (found_to)
break;
3457 }
else if (order->GetDestination() == to->
index) {
3459 if (found_from)
break;
3462 if (!found_to || !found_from)
continue;
3519 DEBUG(misc, 0,
"Can't allocate link graph");
3528 }
else if (ge2.
link_graph == INVALID_LINK_GRAPH) {
3547 (*lg)[ge1.
node].UpdateEdge(ge2.
node, capacity, usage, mode);
3559 for (
const Vehicle *v = front; v != NULL; v = v->
Next()) {
3574 static void StationHandleSmallTick(
BaseStation *st)
3585 void OnTick_Station()
3587 if (_game_mode == GM_EDITOR)
return;
3590 FOR_ALL_BASE_STATIONS(st) {
3591 StationHandleSmallTick(st);
3615 FOR_ALL_STATIONS(st) {
3625 void ModifyStationRatingAround(
TileIndex tile,
Owner owner,
int amount, uint radius)
3629 FOR_ALL_STATIONS(st) {
3630 if (st->
owner == owner &&
3655 if (amount == 0)
return 0;
3667 DEBUG(misc, 0,
"Can't allocate link graph");
3672 if (lg != NULL) (*lg)[ge.
node].UpdateSupply(amount);
3688 static bool IsUniqueStationName(
const char *name)
3692 FOR_ALL_STATIONS(st) {
3693 if (st->
name != NULL && strcmp(st->
name, name) == 0)
return false;
3714 if (ret.
Failed())
return ret;
3720 if (!IsUniqueStationName(text))
return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
3748 uint min_x = (x > max_rad) ? x - max_rad : 0;
3749 uint max_x = x + location.
w + max_rad;
3750 uint min_y = (y > max_rad) ? y - max_rad : 0;
3751 uint max_y = y + location.
h + max_rad;
3758 for (uint cy = min_y; cy < max_y; cy++) {
3759 for (uint cx = min_x; cx < max_x; cx++) {
3765 if (st == NULL)
continue;
3772 if (rad_x < -rad || rad_x >= rad + location.
w)
continue;
3773 if (rad_y < -rad || rad_y >= rad + location.
h)
continue;
3800 if (amount == 0)
return 0;
3804 uint best_rating1 = 0;
3805 uint best_rating2 = 0;
3807 for (
Station *
const *st_iter = all_stations->
Begin(); st_iter != all_stations->
End(); ++st_iter) {
3824 if (st1 == NULL || st->
goods[type].
rating >= best_rating1) {
3825 st2 = st1; best_rating2 = best_rating1; st1 = st; best_rating1 = st->
goods[type].
rating;
3826 }
else if (st2 == NULL || st->
goods[type].
rating >= best_rating2) {
3832 if (st1 == NULL)
return 0;
3836 amount *= best_rating1 + 1;
3840 return UpdateStationWaiting(st1, type, amount, source_type, source_id);
3844 assert(st1 != NULL);
3845 assert(st2 != NULL);
3846 assert(best_rating1 != 0 || best_rating2 != 0);
3853 uint worst_cargo = amount * best_rating2 / (best_rating1 + best_rating2);
3854 assert(worst_cargo <= (amount - worst_cargo));
3857 uint moved = UpdateStationWaiting(st1, type, amount - worst_cargo, source_type, source_id);
3860 return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);
3866 DEBUG(misc, 0,
"Can't allocate station for oilrig at 0x%X, reverting to oilrig only", tile);
3873 st->
string_id = GenerateStationName(st, tile, STATIONNAMING_OILRIG);
3886 st->
rect.BeforeAddTile(tile, StationRect::ADD_FORCE);
3904 st->
rect.AfterRemoveTile(st, tile);
3908 if (!st->
IsInUse())
delete st;
3940 case STATION_WAYPOINT:
4032 case STATION_WAYPOINT:
return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
4033 case STATION_AIRPORT:
return_cmd_error(STR_ERROR_MUST_DEMOLISH_AIRPORT_FIRST);
4038 case STATION_OILRIG:
4039 SetDParam(1, STR_INDUSTRY_NAME_OIL_RIG);
4058 case STATION_BUOY:
return RemoveBuoy(tile, flags);
4059 case STATION_DOCK:
return RemoveDock(tile, flags);
4074 case STATION_WAYPOINT:
4075 case STATION_RAIL: {
4082 case STATION_AIRPORT:
4110 for (SharesMap::const_iterator it = this->
shares.begin(); it != this->
shares.end(); ++it) {
4111 if (it->second == st) {
4112 return it->first - prev;
4129 assert(!this->
shares.empty());
4132 if (it->second != excluded && it->second != excluded2)
return it->second;
4137 uint end = it->first;
4138 uint begin = (it == this->
shares.begin() ? 0 : (--it)->first);
4139 uint interval = end - begin;
4140 if (interval >= this->
unrestricted)
return INVALID_STATION;
4143 SharesMap::const_iterator it2 = (rand < begin) ? this->
shares.upper_bound(rand) :
4144 this->
shares.upper_bound(rand + interval);
4146 if (it2->second != excluded && it2->second != excluded2)
return it2->second;
4151 uint end2 = it2->first;
4152 uint begin2 = (it2 == this->
shares.begin() ? 0 : (--it2)->first);
4153 uint interval2 = end2 - begin2;
4154 if (interval2 >= new_max)
return INVALID_STATION;
4155 new_max -= interval2;
4156 if (begin > begin2) {
4157 Swap(begin, begin2);
4159 Swap(interval, interval2);
4164 it3 = this->
shares.upper_bound(rand);
4165 }
else if (rand < begin2 - interval) {
4166 it3 = this->
shares.upper_bound(rand + interval);
4168 it3 = this->
shares.upper_bound(rand + interval + interval2);
4181 assert(!this->
shares.empty());
4182 SharesMap new_shares;
4184 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4185 new_shares[++i] = it->second;
4186 if (it->first == this->unrestricted) this->
unrestricted = i;
4188 this->
shares.swap(new_shares);
4202 assert(!this->
shares.empty());
4204 uint removed_shares = 0;
4205 uint added_shares = 0;
4206 uint last_share = 0;
4207 SharesMap new_shares;
4208 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4209 if (it->second == st) {
4211 uint share = it->first - last_share;
4212 if (flow == INT_MIN || (uint)(-flow) >= share) {
4213 removed_shares += share;
4214 if (it->first <= this->unrestricted) this->
unrestricted -= share;
4215 if (flow != INT_MIN) flow += share;
4216 last_share = it->first;
4219 removed_shares += (uint)(-flow);
4221 added_shares += (uint)(flow);
4223 if (it->first <= this->unrestricted) this->
unrestricted += flow;
4229 new_shares[it->first + added_shares - removed_shares] = it->second;
4230 last_share = it->first;
4233 new_shares[last_share + (uint)flow] = st;
4240 this->
shares.swap(new_shares);
4250 assert(!this->
shares.empty());
4252 uint last_share = 0;
4253 SharesMap new_shares;
4254 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4256 if (it->first > this->unrestricted)
return;
4257 if (it->second == st) {
4258 flow = it->first - last_share;
4261 new_shares[it->first] = it->second;
4264 new_shares[it->first - flow] = it->second;
4266 last_share = it->first;
4268 if (flow == 0)
return;
4269 new_shares[last_share + flow] = st;
4270 this->
shares.swap(new_shares);
4271 assert(!this->
shares.empty());
4281 assert(!this->
shares.empty());
4283 uint next_share = 0;
4285 for (SharesMap::reverse_iterator it(this->
shares.rbegin()); it != this->
shares.rend(); ++it) {
4286 if (it->first < this->unrestricted)
return;
4288 flow = next_share - it->first;
4292 if (it->first == this->unrestricted)
return;
4293 if (it->second == st) found =
true;
4295 next_share = it->first;
4297 if (flow == 0)
return;
4298 SharesMap new_shares;
4299 new_shares[flow] = st;
4300 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4301 if (it->second != st) {
4302 new_shares[flow + it->first] = it->second;
4307 this->
shares.swap(new_shares);
4308 assert(!this->
shares.empty());
4318 assert(runtime > 0);
4319 SharesMap new_shares;
4321 for (SharesMap::iterator i = this->
shares.begin(); i != this->
shares.end(); ++i) {
4322 share =
max(share + 1, i->first * 30 / runtime);
4323 new_shares[share] = i->second;
4326 this->
shares.swap(new_shares);
4337 FlowStatMap::iterator origin_it = this->find(origin);
4338 if (origin_it == this->end()) {
4339 this->insert(std::make_pair(origin,
FlowStat(via, flow)));
4341 origin_it->second.ChangeShare(via, flow);
4342 assert(!origin_it->second.GetShares()->empty());
4356 FlowStatMap::iterator prev_it = this->find(origin);
4357 if (prev_it == this->end()) {
4360 this->insert(std::make_pair(origin, fs));
4362 prev_it->second.ChangeShare(via, flow);
4363 prev_it->second.ChangeShare(INVALID_STATION, flow);
4364 assert(!prev_it->second.GetShares()->empty());
4374 for (FlowStatMap::iterator i = this->begin(); i != this->end(); ++i) {
4376 uint local = fs.
GetShare(INVALID_STATION);
4377 if (local > INT_MAX) {
4400 for (FlowStatMap::iterator f_it = this->begin(); f_it != this->end();) {
4404 ret.
Push(f_it->first);
4405 this->erase(f_it++);
4419 for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) {
4420 it->second.RestrictShare(via);
4430 for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) {
4431 it->second.ReleaseShare(via);
4442 for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) {
4443 ret += (--(i->second.GetShares()->end()))->first;
4456 for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) {
4457 ret += i->second.GetShare(via);
4469 FlowStatMap::const_iterator i = this->find(from);
4470 if (i == this->end())
return 0;
4471 return (--(i->second.GetShares()->end()))->first;
4482 FlowStatMap::const_iterator i = this->find(from);
4483 if (i == this->end())
return 0;
4484 return i->second.GetShare(via);
4489 GetSlopePixelZ_Station,
4492 GetTileDesc_Station,
4493 GetTileTrackStatus_Station,
4495 AnimateTile_Station,
4497 ChangeTileOwner_Station,
4499 VehicleEnter_Station,
4500 GetFoundation_Station,
4501 TerraformTile_Station,