57 #include "table/strings.h"
102 if (!T::IsValidID(t))
continue;
104 if (closest_station == INVALID_STATION) {
106 }
else if (closest_station != t) {
111 *st = (closest_station == INVALID_STATION) ? NULL : T::Get(closest_station);
132 for (
int dx = -3; dx <= 3; dx++) {
133 for (
int dy = -3; dy <= 3; dy++) {
189 #define M(x) ((x) - STR_SV_STNAME)
194 STATIONNAMING_AIRPORT,
195 STATIONNAMING_OILRIG,
197 STATIONNAMING_HELIPORT,
222 if (
GetIndustrySpec(indtype)->station_name == STR_UNDEFINED)
return false;
226 sni->
free_names &= ~(1 << M(STR_SV_STNAME_OILFIELD) | 1 << M(STR_SV_STNAME_MINES));
232 static const uint32 _gen_station_name_bits[] = {
235 1U << M(STR_SV_STNAME_AIRPORT),
236 1U << M(STR_SV_STNAME_OILFIELD),
237 1U << M(STR_SV_STNAME_DOCKS),
238 1U << M(STR_SV_STNAME_HELIPORT),
242 uint32 free_names = UINT32_MAX;
245 memset(indtypes, 0,
sizeof(indtypes));
248 FOR_ALL_STATIONS(s) {
249 if (s != st && s->
town == t) {
250 if (s->
indtype != IT_INVALID) {
253 if (name != STR_UNDEFINED) {
264 if (str == M(STR_SV_STNAME_FOREST)) {
265 str = M(STR_SV_STNAME_WOODS);
281 return STR_SV_STNAME_FALLBACK;
289 uint32 tmp = free_names & _gen_station_name_bits[name_class];
293 if (
HasBit(free_names, M(STR_SV_STNAME_MINES))) {
295 return STR_SV_STNAME_MINES;
301 if (
HasBit(free_names, M(STR_SV_STNAME)))
return STR_SV_STNAME;
303 if (
HasBit(free_names, M(STR_SV_STNAME_CENTRAL)))
return STR_SV_STNAME_CENTRAL;
307 if (
HasBit(free_names, M(STR_SV_STNAME_LAKESIDE)) &&
310 return STR_SV_STNAME_LAKESIDE;
314 if (
HasBit(free_names, M(STR_SV_STNAME_WOODS)) && (
325 if (
HasBit(free_names, M(STR_SV_STNAME_VALLEY)))
return STR_SV_STNAME_VALLEY;
327 if (
HasBit(free_names, M(STR_SV_STNAME_HEIGHTS)))
return STR_SV_STNAME_HEIGHTS;
331 static const int8 _direction_and_table[] = {
332 ~( (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
333 ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
334 ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_EAST)) | (1 << M(STR_SV_STNAME_NORTH)) ),
335 ~( (1 << M(STR_SV_STNAME_SOUTH)) | (1 << M(STR_SV_STNAME_WEST)) | (1 << M(STR_SV_STNAME_EAST)) ),
338 free_names &= _direction_and_table[
342 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));
343 return (tmp == 0) ? STR_SV_STNAME_FALLBACK : (STR_SV_STNAME +
FindFirstBit(tmp));
358 FOR_ALL_STATIONS(st) {
362 if (cur_dist < threshold) {
363 threshold = cur_dist;
380 case STATION_AIRPORT:
397 default: NOT_REACHED();
411 pt.y -= 32 * ZOOM_LVL_BASE;
426 FOR_ALL_BASE_STATIONS(st) {
452 for (uint i = 0; i < num_items; i++) {
477 int x1 =
max(x - rad, 0);
480 int y1 =
max(y - rad, 0);
500 FOR_ALL_INDUSTRIES(i) {
523 if (always_accepted != NULL) *always_accepted = 0;
532 int x1 =
max(x - rad, 0);
533 int y1 =
max(y - rad, 0);
540 for (
int yc = y1; yc != y2; yc++) {
541 for (
int xc = x1; xc != x2; xc++) {
543 AddAcceptedCargo(tile, acceptance, always_accepted);
562 if (!st->
rect.IsEmpty()) {
574 uint amt = acceptance[i];
592 if (old_acc == new_acc)
return;
598 static const StringID accept_msg[] = {
599 STR_NEWS_STATION_NOW_ACCEPTS_CARGO,
600 STR_NEWS_STATION_NOW_ACCEPTS_CARGO_AND_CARGO,
602 static const StringID reject_msg[] = {
603 STR_NEWS_STATION_NO_LONGER_ACCEPTS_CARGO,
604 STR_NEWS_STATION_NO_LONGER_ACCEPTS_CARGO_OR_CARGO,
618 accepts[num_acc++] = i;
623 rejects[num_rej++] = i;
637 static void UpdateStationSignCoord(
BaseStation *st)
641 if (r->IsEmpty())
return;
675 CommandCost ret = (*st)->rect.BeforeAddRect(area.
tile, area.
w, area.
h, StationRect::ADD_TEST);
676 if (ret.
Failed())
return ret;
685 (*st)->string_id = GenerateStationName(*st, area.
tile, name_class);
708 UpdateStationSignCoord(st);
729 if (ret.
Failed())
return ret;
752 cost.
AddCost(_price[PR_BUILD_FOUNDATION]);
759 }
else if (allowed_z != flat_z) {
779 if (ret.
Failed())
return ret;
783 if (ret.
Failed())
return ret;
804 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)
808 uint invalid_dirs = 5 << axis;
815 if (ret.
Failed())
return ret;
821 if (ret.
Failed())
return ret;
832 if (*station == INVALID_STATION) {
834 }
else if (*station != st) {
859 *affected_vehicles.
Append() = v;
863 if (ret.
Failed())
return ret;
870 if (ret.
Failed())
return ret;
897 if (ret.
Failed())
return ret;
916 if (*station == INVALID_STATION) {
918 }
else if (*station != st) {
942 uint num_roadbits = 0;
943 if (build_over_road) {
951 if (ret.
Failed())
return ret;
961 if (ret.
Failed())
return ret;
970 if (ret.
Failed())
return ret;
974 uint roadbits_to_build =
CountBits(rts) * 2 - num_roadbits;
975 cost.
AddCost(_price[PR_BUILD_ROAD] * roadbits_to_build);
1008 static inline byte *CreateSingle(byte *layout,
int n)
1011 do *layout++ = 0;
while (--i);
1012 layout[((n - 1) >> 1) - n] = 2;
1016 static inline byte *CreateMulti(byte *layout,
int n, byte b)
1019 do *layout++ = b;
while (--i);
1022 layout[n - 1 - n] = 0;
1036 if (statspec != NULL && statspec->lengths >= plat_len &&
1037 statspec->platforms[plat_len - 1] >= numtracks &&
1038 statspec->layouts[plat_len - 1][numtracks - 1]) {
1040 memcpy(layout, statspec->layouts[plat_len - 1][numtracks - 1],
1041 plat_len * numtracks);
1045 if (plat_len == 1) {
1046 CreateSingle(layout, numtracks);
1048 if (numtracks & 1) layout = CreateSingle(layout, plat_len);
1051 while (--numtracks >= 0) {
1052 layout = CreateMulti(layout, plat_len, 4);
1053 layout = CreateMulti(layout, plat_len, 6);
1069 template <
class T, StringID error_message>
1072 assert(*st == NULL);
1073 bool check_surrounding =
true;
1076 if (existing_station != INVALID_STATION) {
1077 if (adjacent && existing_station != station_to_join) {
1084 *st = T::GetIfValid(existing_station);
1085 check_surrounding = (*st == NULL);
1090 if (adjacent) check_surrounding =
false;
1094 if (check_surrounding) {
1097 if (ret.
Failed())
return ret;
1101 if (*st == NULL && station_to_join != INVALID_STATION) *st = T::GetIfValid(station_to_join);
1117 return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_RAILWAY_STATION_FIRST>(existing_station, station_to_join, adjacent, ta, st);
1131 return FindJoiningBaseStation<Waypoint, STR_ERROR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
1154 RailType rt = Extract<RailType, 0, 4>(p1);
1155 Axis axis = Extract<Axis, 4, 1>(p1);
1156 byte numtracks =
GB(p1, 8, 8);
1157 byte plat_len =
GB(p1, 16, 8);
1158 bool adjacent =
HasBit(p1, 24);
1161 byte spec_index =
GB(p2, 8, 8);
1162 StationID station_to_join =
GB(p2, 16, 16);
1166 if (ret.
Failed())
return ret;
1173 if (plat_len == 0 || numtracks == 0)
return CMD_ERROR;
1184 bool reuse = (station_to_join != NEW_STATION);
1185 if (!reuse) station_to_join = INVALID_STATION;
1186 bool distant_join = (station_to_join != INVALID_STATION);
1193 TileArea new_location(tile_org, w_org, h_org);
1196 StationID est = INVALID_STATION;
1200 if (cost.
Failed())
return cost;
1202 cost.
AddCost((numtracks * _price[PR_BUILD_STATION_RAIL] + _price[PR_BUILD_STATION_RAIL_LENGTH]) * plat_len);
1207 if (ret.
Failed())
return ret;
1209 ret =
BuildStationPart(&st, flags, reuse, new_location, STATIONNAMING_RAIL);
1210 if (ret.
Failed())
return ret;
1214 if (ret.
Failed())
return ret;
1222 if (statspec != NULL) {
1237 if (flags & DC_EXEC) {
1240 byte numtracks_orig;
1246 st->
rect.BeforeAddRect(tile_org, w_org, h_org, StationRect::ADD_TRY);
1248 if (statspec != NULL) {
1257 layout_ptr =
AllocaM(byte, numtracks * plat_len);
1260 numtracks_orig = numtracks;
1268 byte layout = *layout_ptr++;
1274 *affected_vehicles.
Append() = v;
1276 for (; v->
Next() != NULL; v = v->
Next()) { }
1301 if (statspec != NULL) {
1316 TriggerStationAnimation(st, tile,
SAT_BUILT);
1324 }
while (--numtracks);
1326 for (uint i = 0; i < affected_vehicles.
Length(); ++i) {
1328 Train *v = affected_vehicles[i];
1331 for (; v->
Next() != NULL; v = v->
Next()) { }
1338 update_reservation_area =
TileArea(tile_org, 1, numtracks_orig);
1340 update_reservation_area =
TileArea(tile_org, numtracks_orig, 1);
1354 platform_begin = next_tile;
1357 platform_end = next_tile;
1361 bool reservation =
false;
1362 for (
TileIndex t = platform_begin; !reservation && t <= platform_end; t += tile_offset) {
1384 static void MakeRailStationAreaSmaller(
BaseStation *st)
1391 if (ta.
w != 0 && ta.
h != 0) {
1465 if (ret.
Failed())
continue;
1468 T *st = T::GetByTile(tile);
1469 if (st == NULL)
continue;
1474 if (ret.
Failed())
continue;
1483 total_cost.
AddCost(-_price[PR_CLEAR_RAIL]);
1501 for (; temp->
Next() != NULL; temp = temp->
Next()) { }
1509 DoClearSquare(tile);
1515 st->rect.AfterRemoveTile(st, tile);
1521 affected_stations.
Include(st);
1527 for (; v->
Next() != NULL; v = v->
Next()) { }
1533 if (quantity == 0)
return error.
Failed() ? error :
CommandCost(STR_ERROR_THERE_IS_NO_STATION);
1535 for (T **stp = affected_stations.
Begin(); stp != affected_stations.
End(); stp++) {
1541 MakeRailStationAreaSmaller(st);
1542 UpdateStationSignCoord(st);
1548 st->UpdateVirtCoord();
1553 total_cost.
AddCost(quantity * removal_cost);
1577 if (ret.
Failed())
return ret;
1580 for (
Station **stp = affected_stations.
Begin(); stp != affected_stations.
End(); stp++) {
1628 if (ret.
Failed())
return ret;
1634 assert(ta.
w != 0 && ta.
h != 0);
1640 if (!st->TileBelongsToRailStation(tile))
continue;
1643 if (ret.
Failed())
return ret;
1645 cost.
AddCost(_price[PR_CLEAR_STATION_RAIL]);
1657 DoClearSquare(tile);
1666 st->rect.AfterRemoveRect(st, st->train_station);
1668 st->train_station.Clear();
1674 st->speclist = NULL;
1675 st->cached_anim_triggers = 0;
1679 st->UpdateVirtCoord();
1733 if (*primary_stop == NULL) {
1735 return primary_stop;
1739 while (stop->
next != NULL) stop = stop->
next;
1757 return FindJoiningBaseStation<Station, STR_ERROR_MUST_REMOVE_ROAD_STOP_FIRST>(existing_stop, station_to_join, adjacent, ta, st);
1778 bool type =
HasBit(p2, 0);
1779 bool is_drive_through =
HasBit(p2, 1);
1780 RoadTypes rts = Extract<RoadTypes, 2, 2>(p2);
1781 StationID station_to_join =
GB(p2, 16, 16);
1782 bool reuse = (station_to_join != NEW_STATION);
1783 if (!reuse) station_to_join = INVALID_STATION;
1784 bool distant_join = (station_to_join != INVALID_STATION);
1786 uint8 width = (uint8)
GB(p1, 0, 8);
1787 uint8 lenght = (uint8)
GB(p1, 8, 8);
1792 if (width == 0 || lenght == 0)
return CMD_ERROR;
1796 TileArea roadstop_area(tile, width, lenght);
1807 if (is_drive_through) {
1809 axis = Extract<Axis, 6, 1>(p2);
1813 ddir = Extract<DiagDirection, 6, 2>(p2);
1818 if (ret.
Failed())
return ret;
1822 StationID est = INVALID_STATION;
1823 ret =
CheckFlatLandRoadStop(roadstop_area, flags, is_drive_through ? 5 << axis : 1 << ddir, is_drive_through, type, axis, &est, rts);
1824 if (ret.
Failed())
return ret;
1829 if (ret.
Failed())
return ret;
1834 ret =
BuildStationPart(&st, flags, reuse, roadstop_area, STATIONNAMING_ROAD);
1835 if (ret.
Failed())
return ret;
1851 *currstop = road_stop;
1862 st->
rect.BeforeAddTile(cur_tile, StationRect::ADD_TRY);
1865 if (is_drive_through) {
1932 if (ret.
Failed())
return ret;
1947 assert(cur_stop != NULL);
1955 if (ret.
Failed())
return ret;
1959 if (*primary_stop == cur_stop) {
1961 *primary_stop = cur_stop->
next;
1963 if (*primary_stop == NULL) {
1969 while (pred->
next != cur_stop) pred = pred->
next;
1988 DoClearSquare(tile);
1996 FOR_ALL_ROADVEHICLES(v) {
2003 st->
rect.AfterRemoveTile(st, tile);
2034 uint8 width = (uint8)
GB(p1, 0, 8);
2035 uint8 height = (uint8)
GB(p1, 8, 8);
2038 if (width == 0 || height == 0)
return CMD_ERROR;
2042 TileArea roadstop_area(tile, width, height);
2060 if (ret.
Failed())
return ret;
2065 if ((flags &
DC_EXEC) && is_drive_through) {
2067 road_owner, tram_owner);
2094 uint mindist = UINT_MAX;
2128 uint noise_reduction = distance / town_tolerance_distance;
2144 Town *t, *nearest = NULL;
2146 uint mindist = UINT_MAX - add;
2152 if (dist < mindist) {
2171 FOR_ALL_STATIONS(st) {
2196 StationID station_to_join =
GB(p2, 16, 16);
2197 bool reuse = (station_to_join != NEW_STATION);
2198 if (!reuse) station_to_join = INVALID_STATION;
2199 bool distant_join = (station_to_join != INVALID_STATION);
2200 byte airport_type =
GB(p1, 0, 8);
2201 byte layout =
GB(p1, 8, 8);
2208 if (ret.
Failed())
return ret;
2225 if (cost.
Failed())
return cost;
2233 StringID authority_refuse_message = STR_NULL;
2234 Town *authority_refuse_town = NULL;
2239 authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_NOISE;
2240 authority_refuse_town = nearest;
2246 FOR_ALL_STATIONS(st) {
2250 authority_refuse_message = STR_ERROR_LOCAL_AUTHORITY_REFUSES_AIRPORT;
2251 authority_refuse_town = t;
2255 if (authority_refuse_message != STR_NULL) {
2262 if (ret.
Failed())
return ret;
2268 if (ret.
Failed())
return ret;
2275 cost.
AddCost(_price[PR_BUILD_STATION_AIRPORT]);
2288 st->
rect.BeforeAddRect(tile, w, h, StationRect::ADD_TRY);
2300 AirportTileAnimationTrigger(st, iter,
AAT_BUILT);
2335 if (ret.
Failed())
return ret;
2359 if (!st->TileBelongsToAirport(tile_cur))
continue;
2362 if (ret.
Failed())
return ret;
2364 cost.
AddCost(_price[PR_CLEAR_STATION_AIRPORT]);
2366 if (flags & DC_EXEC) {
2369 DoClearSquare(tile_cur);
2374 if (flags & DC_EXEC) {
2424 if (ret.
Failed())
return ret;
2443 if ((v->
owner == company) == include_company) {
2445 FOR_VEHICLE_ORDERS(v, order) {
2461 static const byte _dock_w_chk[4] = { 2, 1, 2, 1 };
2462 static const byte _dock_h_chk[4] = { 1, 2, 1, 2 };
2475 StationID station_to_join =
GB(p2, 16, 16);
2476 bool reuse = (station_to_join != NEW_STATION);
2477 if (!reuse) station_to_join = INVALID_STATION;
2478 bool distant_join = (station_to_join != INVALID_STATION);
2490 if (ret.
Failed())
return ret;
2495 if (ret.
Failed())
return ret;
2509 if (ret.
Failed())
return ret;
2517 _dock_w_chk[direction], _dock_h_chk[direction]);
2522 if (ret.
Failed())
return ret;
2528 if (ret.
Failed())
return ret;
2536 st->
rect.BeforeAddRect(dock_area.
tile, dock_area.
w, dock_area.
h, StationRect::ADD_TRY);
2569 if (ret.
Failed())
return ret;
2578 if (ret.
Failed())
return ret;
2581 DoClearSquare(tile1);
2583 MakeWaterKeepingClass(tile2, st->
owner);
2585 st->
rect.AfterRemoveTile(st, tile1);
2586 st->
rect.AfterRemoveTile(st, tile2);
2623 return &_station_display_datas[st][gfx];
2639 case SPR_RAIL_TRACK_X:
2640 snow_desert =
false;
2641 *overlay_offset =
RTO_X;
2644 case SPR_RAIL_TRACK_Y:
2645 snow_desert =
false;
2646 *overlay_offset =
RTO_Y;
2649 case SPR_RAIL_TRACK_X_SNOW:
2651 *overlay_offset =
RTO_X;
2654 case SPR_RAIL_TRACK_Y_SNOW:
2656 *overlay_offset =
RTO_Y;
2679 *ground = snow_desert ? SPR_FLAT_SNOW_DESERT_TILE : SPR_FLAT_GRASS_TILE;
2683 static void DrawTile_Station(
TileInfo *ti)
2691 uint32 relocation = 0;
2692 uint32 ground_relocation = 0;
2695 uint tile_layout = 0;
2707 if (statspec != NULL) {
2744 case APT_RADAR_GRASS_FENCE_SW:
2747 case APT_GRASS_FENCE_NE_FLAG:
2750 case APT_RADAR_FENCE_SW:
2753 case APT_RADAR_FENCE_NE:
2756 case APT_GRASS_FENCE_NE_FLAG_2:
2766 palette = COMPANY_SPRITE_COLOUR(owner);
2769 palette = PALETTE_TO_GREY;
2772 if (layout == NULL && (t == NULL || t->
seq == NULL)) t = GetStationTileLayout(
GetStationType(ti->
tile), gfx);
2782 if (!HasFoundationNW(ti->
tile, slope, z))
SetBit(edge_info, 0);
2783 if (!HasFoundationNE(ti->
tile, slope, z))
SetBit(edge_info, 1);
2785 if (image == 0)
goto draw_default_foundation;
2790 static const uint8 foundation_parts[] = {
2803 static const uint8 composite_foundation_parts[] = {
2805 0x00, 0xD1, 0xE4, 0xE0,
2807 0xCA, 0xC9, 0xC4, 0xC0,
2809 0xD2, 0x91, 0xE4, 0xA0,
2814 uint8 parts = composite_foundation_parts[ti->
tileh];
2825 goto draw_default_foundation;
2829 for (
int i = 0; i < 8; i++) {
2840 draw_default_foundation:
2846 DrawWaterClassGround(ti);
2848 if (sprite != 0) total_offset = sprite - SPR_IMG_BUOY;
2851 DrawWaterClassGround(ti);
2857 DrawShoreTile(ti->
tileh);
2859 DrawClearLandTile(ti, 3);
2863 if (layout != NULL) {
2873 t = &tmp_rail_layout;
2875 }
else if (statspec != NULL) {
2927 int32 total_offset = 0;
2942 DrawSprite(ground + overlay_offset, PAL_NONE, x, y);
2955 static int GetSlopePixelZ_Station(
TileIndex tile, uint x, uint y)
2980 td->
owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
2981 td->
owner[i] = road_owner;
2985 td->
owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
2986 td->
owner[i] = tram_owner;
3028 default: NOT_REACHED();
3029 case STATION_RAIL: str = STR_LAI_STATION_DESCRIPTION_RAILROAD_STATION;
break;
3030 case STATION_AIRPORT:
3031 str = (
IsHangar(tile) ? STR_LAI_STATION_DESCRIPTION_AIRCRAFT_HANGAR : STR_LAI_STATION_DESCRIPTION_AIRPORT);
3033 case STATION_TRUCK: str = STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA;
break;
3034 case STATION_BUS: str = STR_LAI_STATION_DESCRIPTION_BUS_STATION;
break;
3035 case STATION_OILRIG: str = STR_INDUSTRY_NAME_OIL_RIG;
break;
3036 case STATION_DOCK: str = STR_LAI_STATION_DESCRIPTION_SHIP_DOCK;
break;
3037 case STATION_BUOY: str = STR_LAI_STATION_DESCRIPTION_BUOY;
break;
3038 case STATION_WAYPOINT: str = STR_LAI_STATION_DESCRIPTION_WAYPOINT;
break;
3087 static void TileLoop_Station(
TileIndex tile)
3092 case STATION_AIRPORT:
3099 case STATION_OILRIG:
3109 static void AnimateTile_Station(
TileIndex tile)
3112 AnimateStationTile(tile);
3117 AnimateAirportTile(tile);
3122 static bool ClickTile_Station(
TileIndex tile)
3166 }
else if (x < stop) {
3168 uint16 spd =
max(0, (stop - x) * 20 - 15);
3169 if (spd < v->cur_speed) v->
cur_speed = spd;
3198 if (cargoes == 0)
return;
3237 static inline void byte_inc_sat(byte *p)
3254 StationCargoAmountMap waiting_per_source;
3256 for (StationCargoAmountMap::iterator i(waiting_per_source.begin()); i != waiting_per_source.end(); ++i) {
3258 if (source_station == NULL)
continue;
3265 static void UpdateStationRating(
Station *st)
3267 bool waiting_changed =
false;
3269 byte_inc_sat(&st->time_since_load);
3270 byte_inc_sat(&st->time_since_unload);
3273 FOR_ALL_CARGOSPECS(cs) {
3289 waiting_changed =
true;
3308 uint waiting_avg = waiting / (num_dests + 1);
3319 uint32 var10 = (st->last_vehicle_type ==
VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10);
3323 rating =
GB(callback, 0, 14);
3326 if (
HasBit(callback, 14)) rating -= 0x4000;
3332 if (b >= 0) rating += b >> 2;
3335 if (st->last_vehicle_type ==
VEH_SHIP) waittime >>= 2;
3337 (rating += 25, waittime > 12) ||
3338 (rating += 25, waittime > 6) ||
3339 (rating += 45, waittime > 3) ||
3340 (rating += 35,
true);
3347 (rating += 10,
true);
3354 (rating += 10, age >= 2) ||
3355 (rating += 10, age >= 1) ||
3356 (rating += 13,
true);
3366 if (rating <= 64 && waiting_avg >= 100) {
3367 int dec = Random() & 0x1F;
3368 if (waiting_avg < 200) dec &= 7;
3369 waiting -= (dec + 1) * num_dests;
3370 waiting_changed =
true;
3374 if (rating <= 127 && waiting != 0) {
3375 uint32 r = Random();
3376 if (rating <= (
int)
GB(r, 0, 7)) {
3378 waiting =
max((
int)waiting - (
int)((
GB(r, 8, 2) - 1) * num_dests), 0);
3379 waiting_changed =
true;
3386 static const uint WAITING_CARGO_THRESHOLD = 1 << 12;
3387 static const uint WAITING_CARGO_CUT_FACTOR = 1 << 6;
3388 static const uint MAX_WAITING_CARGO = 1 << 15;
3390 if (waiting > WAITING_CARGO_THRESHOLD) {
3391 uint difference = waiting - WAITING_CARGO_THRESHOLD;
3392 waiting -= (difference / WAITING_CARGO_CUT_FACTOR);
3394 waiting =
min(waiting, MAX_WAITING_CARGO);
3395 waiting_changed =
true;
3400 if (waiting_changed && waiting < ge->cargo.AvailableCount()) {
3414 StationID index = st->
index;
3415 if (waiting_changed) {
3438 for (std::list<Vehicle *>::iterator it(st->loading_vehicles.begin()); it != st->loading_vehicles.end(); ++it) {
3439 for (
Vehicle *v = *it; v != NULL; v = v->
Next()) {
3459 if (lg == NULL)
continue;
3462 Edge edge = it->second;
3471 bool updated =
false;
3473 FOR_ALL_ORDER_LISTS(l) {
3474 bool found_from =
false;
3475 bool found_to =
false;
3477 if (!order->IsType(OT_GOTO_STATION) && !order->IsType(OT_IMPLICIT))
continue;
3478 if (order->GetDestination() == from->
index) {
3480 if (found_to)
break;
3481 }
else if (order->GetDestination() == to->
index) {
3483 if (found_from)
break;
3486 if (!found_to || !found_from)
continue;
3543 DEBUG(misc, 0,
"Can't allocate link graph");
3552 }
else if (ge2.
link_graph == INVALID_LINK_GRAPH) {
3571 (*lg)[ge1.
node].UpdateEdge(ge2.
node, capacity, usage, mode);
3583 for (
const Vehicle *v = front; v != NULL; v = v->
Next()) {
3598 static void StationHandleSmallTick(
BaseStation *st)
3609 void OnTick_Station()
3611 if (_game_mode == GM_EDITOR)
return;
3614 FOR_ALL_BASE_STATIONS(st) {
3615 StationHandleSmallTick(st);
3639 FOR_ALL_STATIONS(st) {
3649 void ModifyStationRatingAround(
TileIndex tile,
Owner owner,
int amount, uint radius)
3653 FOR_ALL_STATIONS(st) {
3654 if (st->
owner == owner &&
3679 if (amount == 0)
return 0;
3691 DEBUG(misc, 0,
"Can't allocate link graph");
3696 if (lg != NULL) (*lg)[ge.
node].UpdateSupply(amount);
3712 static bool IsUniqueStationName(
const char *name)
3716 FOR_ALL_STATIONS(st) {
3717 if (st->
name != NULL && strcmp(st->
name, name) == 0)
return false;
3738 if (ret.
Failed())
return ret;
3744 if (!IsUniqueStationName(text))
return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
3772 uint min_x = (x > max_rad) ? x - max_rad : 0;
3773 uint max_x = x + location.
w + max_rad;
3774 uint min_y = (y > max_rad) ? y - max_rad : 0;
3775 uint max_y = y + location.
h + max_rad;
3782 for (uint cy = min_y; cy < max_y; cy++) {
3783 for (uint cx = min_x; cx < max_x; cx++) {
3789 if (st == NULL)
continue;
3796 if (rad_x < -rad || rad_x >= rad + location.
w)
continue;
3797 if (rad_y < -rad || rad_y >= rad + location.
h)
continue;
3824 if (amount == 0)
return 0;
3828 uint best_rating1 = 0;
3829 uint best_rating2 = 0;
3831 for (
Station *
const *st_iter = all_stations->
Begin(); st_iter != all_stations->
End(); ++st_iter) {
3848 if (st1 == NULL || st->
goods[type].
rating >= best_rating1) {
3849 st2 = st1; best_rating2 = best_rating1; st1 = st; best_rating1 = st->
goods[type].
rating;
3850 }
else if (st2 == NULL || st->
goods[type].
rating >= best_rating2) {
3856 if (st1 == NULL)
return 0;
3860 amount *= best_rating1 + 1;
3864 return UpdateStationWaiting(st1, type, amount, source_type, source_id);
3868 assert(st1 != NULL);
3869 assert(st2 != NULL);
3870 assert(best_rating1 != 0 || best_rating2 != 0);
3877 uint worst_cargo = amount * best_rating2 / (best_rating1 + best_rating2);
3878 assert(worst_cargo <= (amount - worst_cargo));
3881 uint moved = UpdateStationWaiting(st1, type, amount - worst_cargo, source_type, source_id);
3884 return moved + UpdateStationWaiting(st2, type, worst_cargo, source_type, source_id);
3890 DEBUG(misc, 0,
"Can't allocate station for oilrig at 0x%X, reverting to oilrig only", tile);
3897 st->
string_id = GenerateStationName(st, tile, STATIONNAMING_OILRIG);
3910 st->
rect.BeforeAddTile(tile, StationRect::ADD_FORCE);
3928 st->
rect.AfterRemoveTile(st, tile);
3932 if (!st->
IsInUse())
delete st;
3964 case STATION_WAYPOINT:
4056 case STATION_WAYPOINT:
return_cmd_error(STR_ERROR_BUILDING_MUST_BE_DEMOLISHED);
4057 case STATION_AIRPORT:
return_cmd_error(STR_ERROR_MUST_DEMOLISH_AIRPORT_FIRST);
4062 case STATION_OILRIG:
4063 SetDParam(1, STR_INDUSTRY_NAME_OIL_RIG);
4082 case STATION_BUOY:
return RemoveBuoy(tile, flags);
4083 case STATION_DOCK:
return RemoveDock(tile, flags);
4098 case STATION_WAYPOINT:
4099 case STATION_RAIL: {
4106 case STATION_AIRPORT:
4134 for (SharesMap::const_iterator it = this->
shares.begin(); it != this->
shares.end(); ++it) {
4135 if (it->second == st) {
4136 return it->first - prev;
4153 assert(!this->
shares.empty());
4156 if (it->second != excluded && it->second != excluded2)
return it->second;
4161 uint end = it->first;
4162 uint begin = (it == this->
shares.begin() ? 0 : (--it)->first);
4163 uint interval = end - begin;
4164 if (interval >= this->
unrestricted)
return INVALID_STATION;
4167 SharesMap::const_iterator it2 = (rand < begin) ? this->
shares.upper_bound(rand) :
4168 this->
shares.upper_bound(rand + interval);
4170 if (it2->second != excluded && it2->second != excluded2)
return it2->second;
4175 uint end2 = it2->first;
4176 uint begin2 = (it2 == this->
shares.begin() ? 0 : (--it2)->first);
4177 uint interval2 = end2 - begin2;
4178 if (interval2 >= new_max)
return INVALID_STATION;
4179 new_max -= interval2;
4180 if (begin > begin2) {
4181 Swap(begin, begin2);
4183 Swap(interval, interval2);
4188 it3 = this->
shares.upper_bound(rand);
4189 }
else if (rand < begin2 - interval) {
4190 it3 = this->
shares.upper_bound(rand + interval);
4192 it3 = this->
shares.upper_bound(rand + interval + interval2);
4205 assert(!this->
shares.empty());
4206 SharesMap new_shares;
4208 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4209 new_shares[++i] = it->second;
4210 if (it->first == this->unrestricted) this->
unrestricted = i;
4212 this->
shares.swap(new_shares);
4226 assert(!this->
shares.empty());
4228 uint removed_shares = 0;
4229 uint added_shares = 0;
4230 uint last_share = 0;
4231 SharesMap new_shares;
4232 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4233 if (it->second == st) {
4235 uint share = it->first - last_share;
4236 if (flow == INT_MIN || (uint)(-flow) >= share) {
4237 removed_shares += share;
4238 if (it->first <= this->unrestricted) this->
unrestricted -= share;
4239 if (flow != INT_MIN) flow += share;
4240 last_share = it->first;
4243 removed_shares += (uint)(-flow);
4245 added_shares += (uint)(flow);
4247 if (it->first <= this->unrestricted) this->
unrestricted += flow;
4253 new_shares[it->first + added_shares - removed_shares] = it->second;
4254 last_share = it->first;
4257 new_shares[last_share + (uint)flow] = st;
4264 this->
shares.swap(new_shares);
4274 assert(!this->
shares.empty());
4276 uint last_share = 0;
4277 SharesMap new_shares;
4278 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4280 if (it->first > this->unrestricted)
return;
4281 if (it->second == st) {
4282 flow = it->first - last_share;
4285 new_shares[it->first] = it->second;
4288 new_shares[it->first - flow] = it->second;
4290 last_share = it->first;
4292 if (flow == 0)
return;
4293 new_shares[last_share + flow] = st;
4294 this->
shares.swap(new_shares);
4295 assert(!this->
shares.empty());
4305 assert(!this->
shares.empty());
4307 uint next_share = 0;
4309 for (SharesMap::reverse_iterator it(this->
shares.rbegin()); it != this->
shares.rend(); ++it) {
4310 if (it->first < this->unrestricted)
return;
4312 flow = next_share - it->first;
4316 if (it->first == this->unrestricted)
return;
4317 if (it->second == st) found =
true;
4319 next_share = it->first;
4321 if (flow == 0)
return;
4322 SharesMap new_shares;
4323 new_shares[flow] = st;
4324 for (SharesMap::iterator it(this->
shares.begin()); it != this->
shares.end(); ++it) {
4325 if (it->second != st) {
4326 new_shares[flow + it->first] = it->second;
4331 this->
shares.swap(new_shares);
4332 assert(!this->
shares.empty());
4342 assert(runtime > 0);
4343 SharesMap new_shares;
4345 for (SharesMap::iterator i = this->
shares.begin(); i != this->
shares.end(); ++i) {
4346 share =
max(share + 1, i->first * 30 / runtime);
4347 new_shares[share] = i->second;
4350 this->
shares.swap(new_shares);
4361 FlowStatMap::iterator origin_it = this->find(origin);
4362 if (origin_it == this->end()) {
4363 this->insert(std::make_pair(origin,
FlowStat(via, flow)));
4365 origin_it->second.ChangeShare(via, flow);
4366 assert(!origin_it->second.GetShares()->empty());
4380 FlowStatMap::iterator prev_it = this->find(origin);
4381 if (prev_it == this->end()) {
4384 this->insert(std::make_pair(origin, fs));
4386 prev_it->second.ChangeShare(via, flow);
4387 prev_it->second.ChangeShare(INVALID_STATION, flow);
4388 assert(!prev_it->second.GetShares()->empty());
4398 for (FlowStatMap::iterator i = this->begin(); i != this->end(); ++i) {
4400 uint local = fs.
GetShare(INVALID_STATION);
4401 if (local > INT_MAX) {
4424 for (FlowStatMap::iterator f_it = this->begin(); f_it != this->end();) {
4428 ret.
Push(f_it->first);
4429 this->erase(f_it++);
4443 for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) {
4444 it->second.RestrictShare(via);
4454 for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) {
4455 it->second.ReleaseShare(via);
4466 for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) {
4467 ret += (--(i->second.GetShares()->end()))->first;
4480 for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) {
4481 ret += i->second.GetShare(via);
4493 FlowStatMap::const_iterator i = this->find(from);
4494 if (i == this->end())
return 0;
4495 return (--(i->second.GetShares()->end()))->first;
4506 FlowStatMap::const_iterator i = this->find(from);
4507 if (i == this->end())
return 0;
4508 return i->second.GetShare(via);
4513 GetSlopePixelZ_Station,
4516 GetTileDesc_Station,
4517 GetTileTrackStatus_Station,
4519 AnimateTile_Station,
4521 ChangeTileOwner_Station,
4523 VehicleEnter_Station,
4524 GetFoundation_Station,
4525 TerraformTile_Station,