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;
972 if (ret.
Failed())
return ret;
981 if (ret.
Failed())
return ret;
985 uint roadbits_to_build =
CountBits(rts) * 2 - num_roadbits;
986 cost.
AddCost(_price[PR_BUILD_ROAD] * roadbits_to_build);
1019 static inline byte *CreateSingle(byte *layout,
int n)
1022 do *layout++ = 0;
while (--i);
1023 layout[((n - 1) >> 1) - n] = 2;
1027 static inline byte *CreateMulti(byte *layout,
int n, byte b)
1030 do *layout++ = b;
while (--i);
1033 layout[n - 1 - n] = 0;
1047 if (statspec != NULL && statspec->lengths >= plat_len &&
1048 statspec->platforms[plat_len - 1] >= numtracks &&
1049 statspec->layouts[plat_len - 1][numtracks - 1]) {
1051 memcpy(layout, statspec->layouts[plat_len - 1][numtracks - 1],
1052 plat_len * numtracks);
1056 if (plat_len == 1) {
1057 CreateSingle(layout, numtracks);
1059 if (numtracks & 1) layout = CreateSingle(layout, plat_len);
1062 while (--numtracks >= 0) {
1063 layout = CreateMulti(layout, plat_len, 4);
1064 layout = CreateMulti(layout, plat_len, 6);
1080 template <
class T, StringID error_message>
1083 assert(*st == NULL);
1084 bool check_surrounding =
true;
1087 if (existing_station != INVALID_STATION) {
1088 if (adjacent && existing_station != station_to_join) {
1095 *st = T::GetIfValid(existing_station);
1096 check_surrounding = (*st == NULL);
1101 if (adjacent) check_surrounding =
false;
1105 if (check_surrounding) {
1108 if (ret.
Failed())
return ret;
1112 if (*st == NULL && station_to_join != INVALID_STATION) *st = T::GetIfValid(station_to_join);
1128 return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_RAILWAY_STATION_FIRST>(existing_station, station_to_join, adjacent, ta, st);
1142 return FindJoiningBaseStation<Waypoint, STR_ERROR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
1189 RailType rt = Extract<RailType, 0, 4>(p1);
1190 Axis axis = Extract<Axis, 4, 1>(p1);
1191 byte numtracks =
GB(p1, 8, 8);
1192 byte plat_len =
GB(p1, 16, 8);
1193 bool adjacent =
HasBit(p1, 24);
1196 byte spec_index =
GB(p2, 8, 8);
1197 StationID station_to_join =
GB(p2, 16, 16);
1201 if (ret.
Failed())
return ret;
1208 if (plat_len == 0 || numtracks == 0)
return CMD_ERROR;
1219 bool reuse = (station_to_join != NEW_STATION);
1220 if (!reuse) station_to_join = INVALID_STATION;
1221 bool distant_join = (station_to_join != INVALID_STATION);
1228 TileArea new_location(tile_org, w_org, h_org);
1231 StationID est = INVALID_STATION;
1235 if (cost.
Failed())
return cost;
1237 cost.
AddCost((numtracks * _price[PR_BUILD_STATION_RAIL] + _price[PR_BUILD_STATION_RAIL_LENGTH]) * plat_len);
1242 if (ret.
Failed())
return ret;
1244 ret =
BuildStationPart(&st, flags, reuse, new_location, STATIONNAMING_RAIL);
1245 if (ret.
Failed())
return ret;
1249 if (ret.
Failed())
return ret;
1257 if (statspec != NULL) {
1272 if (flags & DC_EXEC) {
1275 byte numtracks_orig;
1281 st->
rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TRY);
1283 if (statspec != NULL) {
1292 layout_ptr =
AllocaM(byte, numtracks * plat_len);
1295 numtracks_orig = numtracks;
1303 byte layout = *layout_ptr++;
1308 *affected_vehicles.
Append() = v;
1333 if (statspec != NULL) {
1348 TriggerStationAnimation(st, tile,
SAT_BUILT);
1356 }
while (--numtracks);
1358 for (uint i = 0; i < affected_vehicles.
Length(); ++i) {
1366 update_reservation_area =
TileArea(tile_org, 1, numtracks_orig);
1368 update_reservation_area =
TileArea(tile_org, numtracks_orig, 1);
1382 platform_begin = next_tile;
1385 platform_end = next_tile;
1389 bool reservation =
false;
1390 for (
TileIndex t = platform_begin; !reservation && t <= platform_end; t += tile_offset) {
1412 static void MakeRailStationAreaSmaller(
BaseStation *st)
1419 if (ta.
w != 0 && ta.
h != 0) {
1493 if (ret.
Failed())
continue;
1496 T *st = T::GetByTile(tile);
1497 if (st == NULL)
continue;
1502 if (ret.
Failed())
continue;
1511 total_cost.
AddCost(-_price[PR_CLEAR_RAIL]);
1530 DoClearSquare(tile);
1536 st->rect.AfterRemoveTile(st, tile);
1542 affected_stations.
Include(st);
1548 if (quantity == 0)
return error.
Failed() ? error :
CommandCost(STR_ERROR_THERE_IS_NO_STATION);
1550 for (T **stp = affected_stations.
Begin(); stp != affected_stations.
End(); stp++) {
1556 MakeRailStationAreaSmaller(st);
1557 UpdateStationSignCoord(st);
1563 st->UpdateVirtCoord();
1568 total_cost.
AddCost(quantity * removal_cost);
1592 if (ret.
Failed())
return ret;
1595 for (
Station **stp = affected_stations.
Begin(); stp != affected_stations.
End(); stp++) {
1644 if (ret.
Failed())
return ret;
1650 assert(ta.
w != 0 && ta.
h != 0);
1656 if (st->TileBelongsToRailStation(tile)) {
1659 if (ret.
Failed())
return ret;
1714 if (*primary_stop == NULL) {
1716 return primary_stop;
1720 while (stop->
next != NULL) stop = stop->
next;
1738 return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_ROAD_STOP_FIRST>(existing_stop, station_to_join, adjacent, ta, st);
1759 bool type =
HasBit(p2, 0);
1760 bool is_drive_through =
HasBit(p2, 1);
1761 RoadTypes rts = Extract<RoadTypes, 2, 2>(p2);
1762 StationID station_to_join =
GB(p2, 16, 16);
1763 bool reuse = (station_to_join != NEW_STATION);
1764 if (!reuse) station_to_join = INVALID_STATION;
1765 bool distant_join = (station_to_join != INVALID_STATION);
1767 uint8 width = (uint8)
GB(p1, 0, 8);
1768 uint8 lenght = (uint8)
GB(p1, 8, 8);
1773 if (width == 0 || lenght == 0)
return CMD_ERROR;
1777 TileArea roadstop_area(tile, width, lenght);
1788 if (is_drive_through) {
1790 axis = Extract<Axis, 6, 1>(p2);
1794 ddir = Extract<DiagDirection, 6, 2>(p2);
1799 if (ret.
Failed())
return ret;
1803 StationID est = INVALID_STATION;
1804 ret =
CheckFlatLandRoadStop(roadstop_area, flags, is_drive_through ? 5 << axis : 1 << ddir, is_drive_through, type, axis, &est, rts);
1805 if (ret.
Failed())
return ret;
1810 if (ret.
Failed())
return ret;
1815 ret =
BuildStationPart(&st, flags, reuse, roadstop_area, STATIONNAMING_ROAD);
1816 if (ret.
Failed())
return ret;
1832 *currstop = road_stop;
1843 st->
rect.BeforeAddTile(cur_tile, StationRect::ADD_TRY);
1846 if (is_drive_through) {
1913 if (ret.
Failed())
return ret;
1928 assert(cur_stop != NULL);
1936 if (ret.
Failed())
return ret;
1940 if (*primary_stop == cur_stop) {
1942 *primary_stop = cur_stop->
next;
1944 if (*primary_stop == NULL) {
1950 while (pred->
next != cur_stop) pred = pred->
next;
1970 DoClearSquare(tile);
1978 FOR_ALL_ROADVEHICLES(v) {
1985 st->
rect.AfterRemoveTile(st, tile);
2017 uint8 width = (uint8)
GB(p1, 0, 8);
2018 uint8 height = (uint8)
GB(p1, 8, 8);
2019 bool keep_drive_through_roads = !
HasBit(p2, 1);
2022 if (width == 0 || height == 0)
return CMD_ERROR;
2028 TileArea roadstop_area(tile, width, height);
2031 CommandCost last_error(STR_ERROR_THERE_IS_NO_STATION);
2032 bool had_success =
false;
2078 return had_success ? cost : last_error;
2089 uint mindist = UINT_MAX;
2123 uint noise_reduction = distance / town_tolerance_distance;
2139 Town *t, *nearest = NULL;
2141 uint mindist = UINT_MAX - add;
2147 if (dist < mindist) {
2166 FOR_ALL_STATIONS(st) {
2191 StationID station_to_join =
GB(p2, 16, 16);
2192 bool reuse = (station_to_join != NEW_STATION);
2193 if (!reuse) station_to_join = INVALID_STATION;
2194 bool distant_join = (station_to_join != INVALID_STATION);
2195 byte airport_type =
GB(p1, 0, 8);
2196 byte layout =
GB(p1, 8, 8);
2203 if (ret.
Failed())
return ret;
2220 if (cost.
Failed())
return cost;
2228 StringID authority_refuse_message = STR_NULL;
2229 Town *authority_refuse_town = NULL;
2234 authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_NOISE;
2235 authority_refuse_town = nearest;
2241 FOR_ALL_STATIONS(st) {
2245 authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_AIRPORT;
2246 authority_refuse_town = t;
2250 if (authority_refuse_message != STR_NULL) {
2257 if (ret.
Failed())
return ret;
2263 if (ret.
Failed())
return ret;
2270 cost.
AddCost(_price[PR_BUILD_STATION_AIRPORT]);
2283 st->
rect.BeforeAddRect(tile, w, h, StationRect::ADD_TRY);
2295 AirportTileAnimationTrigger(st, iter,
AAT_BUILT);
2330 if (ret.
Failed())
return ret;
2354 if (!st->TileBelongsToAirport(tile_cur))
continue;
2357 if (ret.
Failed())
return ret;
2359 cost.
AddCost(_price[PR_CLEAR_STATION_AIRPORT]);
2361 if (flags & DC_EXEC) {
2364 DoClearSquare(tile_cur);
2369 if (flags & DC_EXEC) {
2419 if (ret.
Failed())
return ret;
2438 if ((v->
owner == company) == include_company) {
2440 FOR_VEHICLE_ORDERS(v, order) {
2456 static const byte _dock_w_chk[4] = { 2, 1, 2, 1 };
2457 static const byte _dock_h_chk[4] = { 1, 2, 1, 2 };
2470 StationID station_to_join =
GB(p2, 16, 16);
2471 bool reuse = (station_to_join != NEW_STATION);
2472 if (!reuse) station_to_join = INVALID_STATION;
2473 bool distant_join = (station_to_join != INVALID_STATION);
2485 if (ret.
Failed())
return ret;
2490 if (ret.
Failed())
return ret;
2504 if (ret.
Failed())
return ret;
2512 _dock_w_chk[direction], _dock_h_chk[direction]);
2517 if (ret.
Failed())
return ret;
2523 if (ret.
Failed())
return ret;
2531 st->
rect.BeforeAddRect(dock_area.
tile, dock_area.
w, dock_area.
h, StationRect::ADD_TRY);
2564 if (ret.
Failed())
return ret;
2573 if (ret.
Failed())
return ret;
2576 DoClearSquare(tile1);
2578 MakeWaterKeepingClass(tile2, st->
owner);
2580 st->
rect.AfterRemoveTile(st, tile1);
2581 st->
rect.AfterRemoveTile(st, tile2);
2618 return &_station_display_datas[st][gfx];
2634 case SPR_RAIL_TRACK_X:
2635 snow_desert =
false;
2636 *overlay_offset =
RTO_X;
2639 case SPR_RAIL_TRACK_Y:
2640 snow_desert =
false;
2641 *overlay_offset =
RTO_Y;
2644 case SPR_RAIL_TRACK_X_SNOW:
2646 *overlay_offset =
RTO_X;
2649 case SPR_RAIL_TRACK_Y_SNOW:
2651 *overlay_offset =
RTO_Y;
2674 *ground = snow_desert ? SPR_FLAT_SNOW_DESERT_TILE : SPR_FLAT_GRASS_TILE;
2678 static void DrawTile_Station(
TileInfo *ti)
2686 uint32 relocation = 0;
2687 uint32 ground_relocation = 0;
2690 uint tile_layout = 0;
2702 if (statspec != NULL) {
2739 case APT_RADAR_GRASS_FENCE_SW:
2742 case APT_GRASS_FENCE_NE_FLAG:
2745 case APT_RADAR_FENCE_SW:
2748 case APT_RADAR_FENCE_NE:
2751 case APT_GRASS_FENCE_NE_FLAG_2:
2761 palette = COMPANY_SPRITE_COLOUR(owner);
2764 palette = PALETTE_TO_GREY;
2767 if (layout == NULL && (t == NULL || t->
seq == NULL)) t = GetStationTileLayout(
GetStationType(ti->
tile), gfx);
2777 if (!HasFoundationNW(ti->
tile, slope, z))
SetBit(edge_info, 0);
2778 if (!HasFoundationNE(ti->
tile, slope, z))
SetBit(edge_info, 1);
2780 if (image == 0)
goto draw_default_foundation;
2785 static const uint8 foundation_parts[] = {
2798 static const uint8 composite_foundation_parts[] = {
2800 0x00, 0xD1, 0xE4, 0xE0,
2802 0xCA, 0xC9, 0xC4, 0xC0,
2804 0xD2, 0x91, 0xE4, 0xA0,
2809 uint8 parts = composite_foundation_parts[ti->
tileh];
2820 goto draw_default_foundation;
2824 for (
int i = 0; i < 8; i++) {
2835 draw_default_foundation:
2841 DrawWaterClassGround(ti);
2843 if (sprite != 0) total_offset = sprite - SPR_IMG_BUOY;
2846 DrawWaterClassGround(ti);
2852 DrawShoreTile(ti->
tileh);
2854 DrawClearLandTile(ti, 3);
2858 if (layout != NULL) {
2868 t = &tmp_rail_layout;
2870 }
else if (statspec != NULL) {
2922 int32 total_offset = 0;
2937 DrawSprite(ground + overlay_offset, PAL_NONE, x, y);
2950 static int GetSlopePixelZ_Station(
TileIndex tile, uint x, uint y)
2975 td->
owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
2976 td->
owner[i] = road_owner;
2980 td->
owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
2981 td->
owner[i] = tram_owner;
3024 default: NOT_REACHED();
3025 case STATION_RAIL: str = STR_LAI_STATION_DESCRIPTION_RAILROAD_STATION;
break;
3026 case STATION_AIRPORT:
3027 str = (
IsHangar(tile) ? STR_LAI_STATION_DESCRIPTION_AIRCRAFT_HANGAR : STR_LAI_STATION_DESCRIPTION_AIRPORT);
3029 case STATION_TRUCK: str = STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA;
break;
3030 case STATION_BUS: str = STR_LAI_STATION_DESCRIPTION_BUS_STATION;
break;
3031 case STATION_OILRIG: str = STR_INDUSTRY_NAME_OIL_RIG;
break;
3032 case STATION_DOCK: str = STR_LAI_STATION_DESCRIPTION_SHIP_DOCK;
break;
3033 case STATION_BUOY: str = STR_LAI_STATION_DESCRIPTION_BUOY;
break;
3034 case STATION_WAYPOINT: str = STR_LAI_STATION_DESCRIPTION_WAYPOINT;
break;
3083 static void TileLoop_Station(
TileIndex tile)
3088 case STATION_AIRPORT:
3096 case STATION_OILRIG:
3106 static void AnimateTile_Station(
TileIndex tile)
3109 AnimateStationTile(tile);
3114 AnimateAirportTile(tile);
3119 static bool ClickTile_Station(
TileIndex tile)
3163 }
else if (x < stop) {
3165 uint16 spd =
max(0, (stop - x) * 20 - 15);
3166 if (spd < v->cur_speed) v->
cur_speed = spd;
3195 if (cargoes == 0)
return;
3234 static inline void byte_inc_sat(byte *p)
3251 StationCargoAmountMap waiting_per_source;
3253 for (StationCargoAmountMap::iterator i(waiting_per_source.begin()); i != waiting_per_source.end(); ++i) {
3255 if (source_station == NULL)
continue;
3262 static void UpdateStationRating(
Station *st)
3264 bool waiting_changed =
false;
3266 byte_inc_sat(&st->time_since_load);
3267 byte_inc_sat(&st->time_since_unload);
3270 FOR_ALL_CARGOSPECS(cs) {
3286 waiting_changed =
true;
3305 uint waiting_avg = waiting / (num_dests + 1);
3316 uint32 var10 = (st->last_vehicle_type ==
VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10);
3320 rating =
GB(callback, 0, 14);
3323 if (
HasBit(callback, 14)) rating -= 0x4000;
3329 if (b >= 0) rating += b >> 2;
3332 if (st->last_vehicle_type ==
VEH_SHIP) waittime >>= 2;
3334 (rating += 25, waittime > 12) ||
3335 (rating += 25, waittime > 6) ||
3336 (rating += 45, waittime > 3) ||
3337 (rating += 35,
true);
3344 (rating += 10,
true);
3351 (rating += 10, age >= 2) ||
3352 (rating += 10, age >= 1) ||
3353 (rating += 13,
true);
3363 if (rating <= 64 && waiting_avg >= 100) {
3364 int dec = Random() & 0x1F;
3365 if (waiting_avg < 200) dec &= 7;
3366 waiting -= (dec + 1) * num_dests;
3367 waiting_changed =
true;
3371 if (rating <= 127 && waiting != 0) {
3372 uint32 r = Random();
3373 if (rating <= (
int)
GB(r, 0, 7)) {
3375 waiting =
max((
int)waiting - (
int)((
GB(r, 8, 2) - 1) * num_dests), 0);
3376 waiting_changed =
true;
3383 static const uint WAITING_CARGO_THRESHOLD = 1 << 12;
3384 static const uint WAITING_CARGO_CUT_FACTOR = 1 << 6;
3385 static const uint MAX_WAITING_CARGO = 1 << 15;
3387 if (waiting > WAITING_CARGO_THRESHOLD) {
3388 uint difference = waiting - WAITING_CARGO_THRESHOLD;
3389 waiting -= (difference / WAITING_CARGO_CUT_FACTOR);
3391 waiting =
min(waiting, MAX_WAITING_CARGO);
3392 waiting_changed =
true;
3397 if (waiting_changed && waiting < ge->cargo.AvailableCount()) {
3411 StationID index = st->
index;
3412 if (waiting_changed) {
3435 for (std::list<Vehicle *>::iterator it(st->loading_vehicles.begin()); it != st->loading_vehicles.end(); ++it) {
3436 for (
Vehicle *v = *it; v != NULL; v = v->
Next()) {
3457 if (lg == NULL)
continue;
3460 Edge edge = it->second;
3467 bool updated =
false;
3469 if (auto_distributed) {
3474 FOR_ALL_ORDER_LISTS(l) {
3475 bool found_from =
false;
3476 bool found_to =
false;
3478 if (!order->IsType(OT_GOTO_STATION) && !order->IsType(OT_IMPLICIT))
continue;
3479 if (order->GetDestination() == from->
index) {
3481 if (found_to)
break;
3482 }
else if (order->GetDestination() == to->
index) {
3484 if (found_from)
break;
3487 if (!found_to || !found_from)
continue;
3492 while (iter != vehicles.
End()) {
3503 *iter = next_shared;
3506 vehicles.
Erase(iter);
3509 if (iter == vehicles.
End()) iter = vehicles.
Begin();
3557 DEBUG(misc, 0,
"Can't allocate link graph");
3566 }
else if (ge2.
link_graph == INVALID_LINK_GRAPH) {
3585 (*lg)[ge1.
node].UpdateEdge(ge2.
node, capacity, usage, mode);
3597 for (
const Vehicle *v = front; v != NULL; v = v->
Next()) {
3612 static void StationHandleSmallTick(
BaseStation *st)
3623 void OnTick_Station()
3625 if (_game_mode == GM_EDITOR)
return;
3628 FOR_ALL_BASE_STATIONS(st) {
3629 StationHandleSmallTick(st);
3653 FOR_ALL_STATIONS(st) {
3663 void ModifyStationRatingAround(
TileIndex tile,
Owner owner,
int amount, uint radius)
3667 FOR_ALL_STATIONS(st) {
3668 if (st->
owner == owner &&
3693 if (amount == 0)
return 0;
3705 DEBUG(misc, 0,
"Can't allocate link graph");
3710 if (lg != NULL) (*lg)[ge.
node].UpdateSupply(amount);
3726 static bool IsUniqueStationName(
const char *name)
3730 FOR_ALL_STATIONS(st) {
3731 if (st->
name != NULL && strcmp(st->
name, name) == 0)
return false;
3752 if (ret.
Failed())
return ret;
3758 if (!IsUniqueStationName(text))
return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
3786 uint min_x = (x > max_rad) ? x - max_rad : 0;
3787 uint max_x = x + location.
w + max_rad;
3788 uint min_y = (y > max_rad) ? y - max_rad : 0;
3789 uint max_y = y + location.
h + max_rad;
3796 for (uint cy = min_y; cy < max_y; cy++) {
3797 for (uint cx = min_x; cx < max_x; cx++) {
3803 if (st == NULL)
continue;
3810 if (rad_x < -rad || rad_x >= rad + location.
w)
continue;
3811 if (rad_y < -rad || rad_y >= rad + location.
h)
continue;
3838 if (amount == 0)
return 0;
3842 uint best_rating1 = 0;
3843 uint best_rating2 = 0;
3845 for (
Station *
const *st_iter = all_stations->
Begin(); st_iter != all_stations->
End(); ++st_iter) {
3862 if (st1 == NULL || st->
goods[type].
rating >= best_rating1) {
3863 st2 = st1; best_rating2 = best_rating1; st1 = st; best_rating1 = st->
goods[type].
rating;
3864 }
else if (st2 == NULL || st->
goods[type].
rating >= best_rating2) {
3870 if (st1 == NULL)
return 0;
3874 amount *= best_rating1 + 1;
3878 return UpdateStationWaiting(st1, type, amount, source_type, source_id);
3882 assert(st1 != NULL);
3883 assert(st2 != NULL);
3884 assert(best_rating1 != 0 || best_rating2 != 0);
3891 uint worst_cargo = amount * best_rating2 / (best_rating1 + best_rating2);
3892 assert(worst_cargo <= (amount - worst_cargo));
3895 uint moved = UpdateStationWaiting(st1, type, amount - worst_cargo, source_type, source_id);
3898 return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);
3904 DEBUG(misc, 0,
"Can't allocate station for oilrig at 0x%X, reverting to oilrig only", tile);
3911 st->
string_id = GenerateStationName(st, tile, STATIONNAMING_OILRIG);
3924 st->
rect.BeforeAddTile(tile, StationRect::ADD_FORCE);
3942 st->
rect.AfterRemoveTile(st, tile);
3946 if (!st->
IsInUse())
delete st;
3978 case STATION_WAYPOINT:
4070 case STATION_WAYPOINT:
return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
4071 case STATION_AIRPORT:
return_cmd_error(STR_ERROR_MUST_DEMOLISH_AIRPORT_FIRST);
4076 case STATION_OILRIG:
4077 SetDParam(1, STR_INDUSTRY_NAME_OIL_RIG);
4096 case STATION_BUOY:
return RemoveBuoy(tile, flags);
4097 case STATION_DOCK:
return RemoveDock(tile, flags);
4112 case STATION_WAYPOINT:
4113 case STATION_RAIL: {
4120 case STATION_AIRPORT:
4148 for (SharesMap::const_iterator it = this->
shares.begin(); it != this->
shares.end(); ++it) {
4149 if (it->second == st) {
4150 return it->first - prev;
4167 assert(!this->
shares.empty());
4170 if (it->second != excluded && it->second != excluded2)
return it->second;
4175 uint end = it->first;
4176 uint begin = (it == this->
shares.begin() ? 0 : (--it)->first);
4177 uint interval = end - begin;
4178 if (interval >= this->
unrestricted)
return INVALID_STATION;
4181 SharesMap::const_iterator it2 = (rand < begin) ? this->
shares.upper_bound(rand) :
4182 this->
shares.upper_bound(rand + interval);
4184 if (it2->second != excluded && it2->second != excluded2)
return it2->second;
4189 uint end2 = it2->first;
4190 uint begin2 = (it2 == this->
shares.begin() ? 0 : (--it2)->first);
4191 uint interval2 = end2 - begin2;
4192 if (interval2 >= new_max)
return INVALID_STATION;
4193 new_max -= interval2;
4194 if (begin > begin2) {
4195 Swap(begin, begin2);
4197 Swap(interval, interval2);
4202 it3 = this->
shares.upper_bound(rand);
4203 }
else if (rand < begin2 - interval) {
4204 it3 = this->
shares.upper_bound(rand + interval);
4206 it3 = this->
shares.upper_bound(rand + interval + interval2);
4219 assert(!this->
shares.empty());
4220 SharesMap new_shares;
4222 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4223 new_shares[++i] = it->second;
4224 if (it->first == this->unrestricted) this->
unrestricted = i;
4226 this->
shares.swap(new_shares);
4240 assert(!this->
shares.empty());
4242 uint removed_shares = 0;
4243 uint added_shares = 0;
4244 uint last_share = 0;
4245 SharesMap new_shares;
4246 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4247 if (it->second == st) {
4249 uint share = it->first - last_share;
4250 if (flow == INT_MIN || (uint)(-flow) >= share) {
4251 removed_shares += share;
4252 if (it->first <= this->unrestricted) this->
unrestricted -= share;
4253 if (flow != INT_MIN) flow += share;
4254 last_share = it->first;
4257 removed_shares += (uint)(-flow);
4259 added_shares += (uint)(flow);
4261 if (it->first <= this->unrestricted) this->
unrestricted += flow;
4267 new_shares[it->first + added_shares - removed_shares] = it->second;
4268 last_share = it->first;
4271 new_shares[last_share + (uint)flow] = st;
4278 this->
shares.swap(new_shares);
4288 assert(!this->
shares.empty());
4290 uint last_share = 0;
4291 SharesMap new_shares;
4292 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4294 if (it->first > this->unrestricted)
return;
4295 if (it->second == st) {
4296 flow = it->first - last_share;
4299 new_shares[it->first] = it->second;
4302 new_shares[it->first - flow] = it->second;
4304 last_share = it->first;
4306 if (flow == 0)
return;
4307 new_shares[last_share + flow] = st;
4308 this->
shares.swap(new_shares);
4309 assert(!this->
shares.empty());
4319 assert(!this->
shares.empty());
4321 uint next_share = 0;
4323 for (SharesMap::reverse_iterator it(this->
shares.rbegin()); it != this->
shares.rend(); ++it) {
4324 if (it->first < this->unrestricted)
return;
4326 flow = next_share - it->first;
4330 if (it->first == this->unrestricted)
return;
4331 if (it->second == st) found =
true;
4333 next_share = it->first;
4335 if (flow == 0)
return;
4336 SharesMap new_shares;
4337 new_shares[flow] = st;
4338 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4339 if (it->second != st) {
4340 new_shares[flow + it->first] = it->second;
4345 this->
shares.swap(new_shares);
4346 assert(!this->
shares.empty());
4356 assert(runtime > 0);
4357 SharesMap new_shares;
4359 for (SharesMap::iterator i = this->
shares.begin(); i != this->
shares.end(); ++i) {
4360 share =
max(share + 1, i->first * 30 / runtime);
4361 new_shares[share] = i->second;
4364 this->
shares.swap(new_shares);
4375 FlowStatMap::iterator origin_it = this->find(origin);
4376 if (origin_it == this->end()) {
4377 this->insert(std::make_pair(origin,
FlowStat(via, flow)));
4379 origin_it->second.ChangeShare(via, flow);
4380 assert(!origin_it->second.GetShares()->empty());
4394 FlowStatMap::iterator prev_it = this->find(origin);
4395 if (prev_it == this->end()) {
4398 this->insert(std::make_pair(origin, fs));
4400 prev_it->second.ChangeShare(via, flow);
4401 prev_it->second.ChangeShare(INVALID_STATION, flow);
4402 assert(!prev_it->second.GetShares()->empty());
4412 for (FlowStatMap::iterator i = this->begin(); i != this->end(); ++i) {
4414 uint local = fs.
GetShare(INVALID_STATION);
4415 if (local > INT_MAX) {
4438 for (FlowStatMap::iterator f_it = this->begin(); f_it != this->end();) {
4442 ret.
Push(f_it->first);
4443 this->erase(f_it++);
4457 for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) {
4458 it->second.RestrictShare(via);
4468 for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) {
4469 it->second.ReleaseShare(via);
4480 for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) {
4481 ret += (--(i->second.GetShares()->end()))->first;
4494 for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) {
4495 ret += i->second.GetShare(via);
4507 FlowStatMap::const_iterator i = this->find(from);
4508 if (i == this->end())
return 0;
4509 return (--(i->second.GetShares()->end()))->first;
4520 FlowStatMap::const_iterator i = this->find(from);
4521 if (i == this->end())
return 0;
4522 return i->second.GetShare(via);
4527 GetSlopePixelZ_Station,
4530 GetTileDesc_Station,
4531 GetTileTrackStatus_Station,
4533 AnimateTile_Station,
4535 ChangeTileOwner_Station,
4537 VehicleEnter_Station,
4538 GetFoundation_Station,
4539 TerraformTile_Station,