00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "station_base.h"
00015 #include "waypoint_base.h"
00016 #include "roadstop_base.h"
00017 #include "newgrf_cargo.h"
00018 #include "newgrf_station.h"
00019 #include "newgrf_spritegroup.h"
00020 #include "newgrf_sound.h"
00021 #include "newgrf_railtype.h"
00022 #include "town.h"
00023 #include "newgrf_town.h"
00024 #include "company_func.h"
00025 #include "tunnelbridge_map.h"
00026 #include "newgrf_animation_base.h"
00027 #include "newgrf_class_func.h"
00028
00029
00030 template <typename Tspec, typename Tid, Tid Tmax>
00031 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00032 {
00033
00034 classes[0].global_id = 'DFLT';
00035 classes[0].name = STR_STATION_CLASS_DFLT;
00036 classes[0].Insert(NULL);
00037
00038 classes[1].global_id = 'WAYP';
00039 classes[1].name = STR_STATION_CLASS_WAYP;
00040 classes[1].Insert(NULL);
00041 }
00042
00043 template <typename Tspec, typename Tid, Tid Tmax>
00044 bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
00045 {
00046 return true;
00047 }
00048
00049 INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX)
00050
00051 static const uint NUM_STATIONSSPECS_PER_STATION = 255;
00052
00053 enum TriggerArea {
00054 TA_TILE,
00055 TA_PLATFORM,
00056 TA_WHOLE,
00057 };
00058
00059 struct ETileArea : TileArea {
00060 ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
00061 {
00062 switch (ta) {
00063 default: NOT_REACHED();
00064
00065 case TA_TILE:
00066 this->tile = tile;
00067 this->w = 1;
00068 this->h = 1;
00069 break;
00070
00071 case TA_PLATFORM: {
00072 TileIndex start, end;
00073 Axis axis = GetRailStationAxis(tile);
00074 TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
00075
00076 for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(end + delta, tile); end += delta) { }
00077 for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(start - delta, tile); start -= delta) { }
00078
00079 this->tile = start;
00080 this->w = TileX(end) - TileX(start) + 1;
00081 this->h = TileY(end) - TileY(start) + 1;
00082 break;
00083 }
00084
00085 case TA_WHOLE:
00086 st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT);
00087 break;
00088 }
00089 }
00090 };
00091
00092
00105 uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
00106 {
00107 uint32 retval = 0;
00108
00109 if (axis == AXIS_X) {
00110 Swap(platforms, length);
00111 Swap(x, y);
00112 }
00113
00114 if (centred) {
00115 x -= platforms / 2;
00116 y -= length / 2;
00117 x = Clamp(x, -8, 7);
00118 y = Clamp(y, -8, 7);
00119 SB(retval, 0, 4, y & 0xF);
00120 SB(retval, 4, 4, x & 0xF);
00121 } else {
00122 SB(retval, 0, 4, min(15, y));
00123 SB(retval, 4, 4, min(15, length - y - 1));
00124 SB(retval, 8, 4, min(15, x));
00125 SB(retval, 12, 4, min(15, platforms - x - 1));
00126 }
00127 SB(retval, 16, 4, min(15, length));
00128 SB(retval, 20, 4, min(15, platforms));
00129 SB(retval, 24, 4, tile);
00130
00131 return retval;
00132 }
00133
00134
00143 static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
00144 {
00145 byte orig_type = 0;
00146 Axis orig_axis = AXIS_X;
00147 StationID sid = GetStationIndex(tile);
00148
00149 if (check_type) orig_type = GetCustomStationSpecIndex(tile);
00150 if (check_axis) orig_axis = GetRailStationAxis(tile);
00151
00152 for (;;) {
00153 TileIndex new_tile = TILE_ADD(tile, delta);
00154
00155 if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
00156 if (!HasStationRail(new_tile)) break;
00157 if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
00158 if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
00159
00160 tile = new_tile;
00161 }
00162 return tile;
00163 }
00164
00165
00166 static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
00167 {
00168 int tx = TileX(tile);
00169 int ty = TileY(tile);
00170 int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1, 0), check_type, check_axis));
00171 int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
00172 int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1, 0), check_type, check_axis)) + 1;
00173 int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0, 1), check_type, check_axis)) + 1;
00174
00175 tx -= sx; ex -= sx;
00176 ty -= sy; ey -= sy;
00177
00178 return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
00179 }
00180
00181
00182 static uint32 GetRailContinuationInfo(TileIndex tile)
00183 {
00184
00185 static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
00186 static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
00187
00188
00189 static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
00190 static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
00191
00192 Axis axis = GetRailStationAxis(tile);
00193
00194
00195 const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
00196 const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
00197
00198 uint32 res = 0;
00199 uint i;
00200
00201 for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
00202 TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
00203 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
00204 if (trackbits != TRACK_BIT_NONE) {
00205
00206 SetBit(res, i + 8);
00207
00208
00209
00210 if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
00211 continue;
00212 }
00213
00214
00215 if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
00216 }
00217 }
00218
00219 return res;
00220 }
00221
00222
00223
00224 uint32 StationScopeResolver::GetRandomBits() const
00225 {
00226 return (this->st == NULL ? 0 : this->st->random_bits) | (this->tile == INVALID_TILE ? 0 : GetStationTileRandomBits(this->tile) << 16);
00227 }
00228
00229
00230 uint32 StationScopeResolver::GetTriggers() const
00231 {
00232 return this->st == NULL ? 0 : this->st->waiting_triggers;
00233 }
00234
00235
00236 void StationScopeResolver::SetTriggers(int triggers) const
00237 {
00238 BaseStation *st = const_cast<BaseStation *>(this->st);
00239 assert(st != NULL);
00240 st->waiting_triggers = triggers;
00241 }
00242
00248 static struct {
00249 uint32 v40;
00250 uint32 v41;
00251 uint32 v45;
00252 uint32 v46;
00253 uint32 v47;
00254 uint32 v49;
00255 uint8 valid;
00256 } _svc;
00257
00263 TownScopeResolver *StationResolverObject::GetTown()
00264 {
00265 if (this->town_scope == NULL) {
00266 Town *t = NULL;
00267 if (this->station_scope.st != NULL) {
00268 t = this->station_scope.st->town;
00269 } else if (this->station_scope.tile != INVALID_TILE) {
00270 t = ClosestTownFromTile(this->station_scope.tile, UINT_MAX);
00271 }
00272 if (t == NULL) return NULL;
00273 this->town_scope = new TownScopeResolver(*this, t, this->station_scope.st == NULL);
00274 }
00275 return this->town_scope;
00276 }
00277
00278 uint32 StationScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00279 {
00280 if (this->st == NULL) {
00281
00282 switch (variable) {
00283 case 0x40:
00284 case 0x41:
00285 case 0x46:
00286 case 0x47:
00287 case 0x49: return 0x2110000;
00288 case 0x42: return 0;
00289 case 0x43: return GetCompanyInfo(_current_company);
00290 case 0x44: return 2;
00291 case 0x67:
00292 if (this->axis != INVALID_AXIS && this->tile != INVALID_TILE) {
00293 TileIndex tile = this->tile;
00294 if (parameter != 0) tile = GetNearbyTile(parameter, tile, true, this->axis);
00295
00296 Slope tileh = GetTileSlope(tile);
00297 bool swap = (this->axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
00298
00299 return GetNearbyTileInformation(tile, this->ro.grffile->grf_version >= 8) ^ (swap ? SLOPE_EW : 0);
00300 }
00301 break;
00302
00303 case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00304 }
00305
00306 *available = false;
00307 return UINT_MAX;
00308 }
00309
00310 switch (variable) {
00311
00312 case 0x40:
00313 if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(this->tile, false, false, false); SetBit(_svc.valid, 0); }
00314 return _svc.v40;
00315
00316 case 0x41:
00317 if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(this->tile, true, false, false); SetBit(_svc.valid, 1); }
00318 return _svc.v41;
00319
00320 case 0x42: return GetTerrainType(this->tile) | (GetReverseRailTypeTranslation(GetRailType(this->tile), this->statspec->grf_prop.grffile) << 8);
00321 case 0x43: return GetCompanyInfo(this->st->owner);
00322 case 0x44: return HasStationReservation(this->tile) ? 7 : 4;
00323 case 0x45:
00324 if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(this->tile); SetBit(_svc.valid, 2); }
00325 return _svc.v45;
00326
00327 case 0x46:
00328 if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(this->tile, false, false, true); SetBit(_svc.valid, 3); }
00329 return _svc.v46;
00330
00331 case 0x47:
00332 if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(this->tile, true, false, true); SetBit(_svc.valid, 4); }
00333 return _svc.v47;
00334
00335 case 0x49:
00336 if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(this->tile, false, true, false); SetBit(_svc.valid, 5); }
00337 return _svc.v49;
00338
00339 case 0x4A:
00340 return GetAnimationFrame(this->tile);
00341
00342
00343
00344 case 0x66: {
00345 TileIndex tile = this->tile;
00346 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00347 return this->st->TileBelongsToRailStation(tile) ? GetAnimationFrame(tile) : UINT_MAX;
00348 }
00349
00350 case 0x67: {
00351 Axis axis = GetRailStationAxis(this->tile);
00352 TileIndex tile = this->tile;
00353 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00354
00355 Slope tileh = GetTileSlope(tile);
00356 bool swap = (axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
00357
00358 return GetNearbyTileInformation(tile, this->ro.grffile->grf_version >= 8) ^ (swap ? SLOPE_EW : 0);
00359 }
00360
00361 case 0x68: {
00362 TileIndex nearby_tile = GetNearbyTile(parameter, this->tile);
00363
00364 if (!HasStationTileRail(nearby_tile)) return 0xFFFFFFFF;
00365
00366 uint32 grfid = this->st->speclist[GetCustomStationSpecIndex(this->tile)].grfid;
00367 bool perpendicular = GetRailStationAxis(this->tile) != GetRailStationAxis(nearby_tile);
00368 bool same_station = this->st->TileBelongsToRailStation(nearby_tile);
00369 uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
00370
00371 if (IsCustomStationSpecIndex(nearby_tile)) {
00372 const StationSpecList ssl = BaseStation::GetByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
00373 res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
00374 }
00375 return res;
00376 }
00377
00378
00379 case 0x82: return 50;
00380 case 0x84: return this->st->string_id;
00381 case 0x86: return 0;
00382 case 0xF0: return this->st->facilities;
00383 case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00384 }
00385
00386 return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);
00387 }
00388
00389 uint32 Station::GetNewGRFVariable(const ResolverObject &object, byte variable, byte parameter, bool *available) const
00390 {
00391 switch (variable) {
00392 case 0x48: {
00393 CargoID cargo_type;
00394 uint32 value = 0;
00395
00396 for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00397 if (HasBit(this->goods[cargo_type].status, GoodsEntry::GES_ACCEPTANCE)) SetBit(value, cargo_type);
00398 }
00399 return value;
00400 }
00401
00402 case 0x8A: return this->had_vehicle_of_type;
00403 case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE;
00404 case 0xF2: return (this->truck_stops != NULL) ? this->truck_stops->status : 0;
00405 case 0xF3: return (this->bus_stops != NULL) ? this->bus_stops->status : 0;
00406 case 0xF6: return this->airport.flags;
00407 case 0xF7: return GB(this->airport.flags, 8, 8);
00408 }
00409
00410
00411 if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) {
00412 CargoID c = GetCargoTranslation(parameter, object.grffile);
00413
00414 if (c == CT_INVALID) {
00415 switch (variable) {
00416 case 0x62: return 0xFFFFFFFF;
00417 case 0x64: return 0xFF00;
00418 default: return 0;
00419 }
00420 }
00421 const GoodsEntry *ge = &this->goods[c];
00422
00423 switch (variable) {
00424 case 0x60: return min(ge->cargo.TotalCount(), 4095);
00425 case 0x61: return ge->HasVehicleEverTriedLoading() ? ge->time_since_pickup : 0;
00426 case 0x62: return ge->HasRating() ? ge->rating : 0xFFFFFFFF;
00427 case 0x63: return ge->cargo.DaysInTransit();
00428 case 0x64: return ge->HasVehicleEverTriedLoading() ? ge->last_speed | (ge->last_age << 8) : 0xFF00;
00429 case 0x65: return GB(ge->status, GoodsEntry::GES_ACCEPTANCE, 1) << 3;
00430 case 0x69: {
00431 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED + 1 == (int)GoodsEntry::GES_LAST_MONTH);
00432 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED + 2 == (int)GoodsEntry::GES_CURRENT_MONTH);
00433 assert_compile((int)GoodsEntry::GES_EVER_ACCEPTED + 3 == (int)GoodsEntry::GES_ACCEPTED_BIGTICK);
00434 return GB(ge->status, GoodsEntry::GES_EVER_ACCEPTED, 4);
00435 }
00436 }
00437 }
00438
00439
00440 if (variable >= 0x8C && variable <= 0xEC) {
00441 const GoodsEntry *g = &this->goods[GB(variable - 0x8C, 3, 4)];
00442 switch (GB(variable - 0x8C, 0, 3)) {
00443 case 0: return g->cargo.TotalCount();
00444 case 1: return GB(min(g->cargo.TotalCount(), 4095), 0, 4) | (GB(g->status, GoodsEntry::GES_ACCEPTANCE, 1) << 7);
00445 case 2: return g->time_since_pickup;
00446 case 3: return g->rating;
00447 case 4: return g->cargo.Source();
00448 case 5: return g->cargo.DaysInTransit();
00449 case 6: return g->last_speed;
00450 case 7: return g->last_age;
00451 }
00452 }
00453
00454 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
00455
00456 *available = false;
00457 return UINT_MAX;
00458 }
00459
00460 uint32 Waypoint::GetNewGRFVariable(const ResolverObject &object, byte variable, byte parameter, bool *available) const
00461 {
00462 switch (variable) {
00463 case 0x48: return 0;
00464 case 0x8A: return HVOT_WAYPOINT;
00465 case 0xF1: return 0;
00466 case 0xF2: return 0;
00467 case 0xF3: return 0;
00468 case 0xF6: return 0;
00469 case 0xF7: return 0;
00470 }
00471
00472
00473 if (variable >= 0x60 && variable <= 0x65) {
00474 return 0;
00475 }
00476
00477
00478 if (variable >= 0x8C && variable <= 0xEC) {
00479 switch (GB(variable - 0x8C, 0, 3)) {
00480 case 3: return INITIAL_STATION_RATING;
00481 case 4: return INVALID_STATION;
00482 default: return 0;
00483 }
00484 }
00485
00486 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
00487
00488 *available = false;
00489 return UINT_MAX;
00490 }
00491
00492 const SpriteGroup *StationResolverObject::ResolveReal(const RealSpriteGroup *group) const
00493 {
00494 if (this->station_scope.st == NULL || this->station_scope.statspec->cls_id == STAT_CLASS_WAYP) {
00495 return group->loading[0];
00496 }
00497
00498 uint cargo = 0;
00499 const Station *st = Station::From(this->station_scope.st);
00500
00501 switch (this->station_scope.cargo_type) {
00502 case CT_INVALID:
00503 case CT_DEFAULT_NA:
00504 case CT_PURCHASE:
00505 cargo = 0;
00506 break;
00507
00508 case CT_DEFAULT:
00509 for (CargoID cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00510 cargo += st->goods[cargo_type].cargo.TotalCount();
00511 }
00512 break;
00513
00514 default:
00515 cargo = st->goods[this->station_scope.cargo_type].cargo.TotalCount();
00516 break;
00517 }
00518
00519 if (HasBit(this->station_scope.statspec->flags, SSF_DIV_BY_STATION_SIZE)) cargo /= (st->train_station.w + st->train_station.h);
00520 cargo = min(0xfff, cargo);
00521
00522 if (cargo > this->station_scope.statspec->cargo_threshold) {
00523 if (group->num_loading > 0) {
00524 uint set = ((cargo - this->station_scope.statspec->cargo_threshold) * group->num_loading) / (4096 - this->station_scope.statspec->cargo_threshold);
00525 return group->loading[set];
00526 }
00527 } else {
00528 if (group->num_loaded > 0) {
00529 uint set = (cargo * group->num_loaded) / (this->station_scope.statspec->cargo_threshold + 1);
00530 return group->loaded[set];
00531 }
00532 }
00533
00534 return group->loading[0];
00535 }
00536
00546 StationResolverObject::StationResolverObject(const StationSpec *statspec, BaseStation *st, TileIndex tile,
00547 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
00548 : ResolverObject((statspec != NULL ? statspec->grf_prop.grffile : NULL), callback, callback_param1, callback_param2),
00549 station_scope(*this, statspec, st, tile), town_scope(NULL)
00550 {
00551
00552 _svc.valid = 0;
00553
00554 CargoID ctype = CT_DEFAULT_NA;
00555
00556 if (this->station_scope.st == NULL) {
00557
00558 ctype = CT_PURCHASE;
00559 } else if (Station::IsExpected(this->station_scope.st)) {
00560 const Station *st = Station::From(this->station_scope.st);
00561
00562 const CargoSpec *cs;
00563 FOR_ALL_CARGOSPECS(cs) {
00564 if (this->station_scope.statspec->grf_prop.spritegroup[cs->Index()] != NULL &&
00565 st->goods[cs->Index()].cargo.TotalCount() > 0) {
00566 ctype = cs->Index();
00567 break;
00568 }
00569 }
00570 }
00571
00572 if (this->station_scope.statspec->grf_prop.spritegroup[ctype] == NULL) {
00573 ctype = CT_DEFAULT;
00574 }
00575
00576
00577 this->station_scope.cargo_type = ctype;
00578 this->root_spritegroup = this->station_scope.statspec->grf_prop.spritegroup[this->station_scope.cargo_type];
00579 }
00580
00581 StationResolverObject::~StationResolverObject()
00582 {
00583 delete this->town_scope;
00584 }
00585
00593 StationScopeResolver::StationScopeResolver(ResolverObject &ro, const StationSpec *statspec, BaseStation *st, TileIndex tile)
00594 : ScopeResolver(ro)
00595 {
00596 this->tile = tile;
00597 this->st = st;
00598 this->statspec = statspec;
00599 this->cargo_type = CT_INVALID;
00600 this->axis = INVALID_AXIS;
00601 }
00602
00611 SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32 var10)
00612 {
00613 StationResolverObject object(statspec, st, tile, CBID_NO_CALLBACK, var10);
00614 const SpriteGroup *group = object.Resolve();
00615 if (group == NULL || group->type != SGT_RESULT) return 0;
00616 return group->GetResult() - 0x42D;
00617 }
00618
00628 SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info)
00629 {
00630
00631 StationResolverObject object(statspec, st, tile, CBID_NO_CALLBACK, 2, layout | (edge_info << 16));
00632
00633 const SpriteGroup *group = object.Resolve();
00634 if (group == NULL || group->type != SGT_RESULT) return 0;
00635
00636
00637 return group->GetResult() + GetRegister(0x100);
00638 }
00639
00640
00641 uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, BaseStation *st, TileIndex tile)
00642 {
00643 StationResolverObject object(statspec, st, tile, callback, param1, param2);
00644 return object.ResolveCallback();
00645 }
00646
00657 CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, byte plat_len, byte numtracks)
00658 {
00659 TileIndexDiff diff = cur_tile - north_tile;
00660 Slope slope = GetTileSlope(cur_tile);
00661
00662 StationResolverObject object(statspec, NULL, cur_tile, CBID_STATION_LAND_SLOPE_CHECK,
00663 (slope << 4) | (slope ^ (axis == AXIS_Y && HasBit(slope, CORNER_W) != HasBit(slope, CORNER_E) ? SLOPE_EW : 0)),
00664 (numtracks << 24) | (plat_len << 16) | (axis == AXIS_Y ? TileX(diff) << 8 | TileY(diff) : TileY(diff) << 8 | TileX(diff)));
00665 object.station_scope.axis = axis;
00666
00667 uint16 cb_res = object.ResolveCallback();
00668
00669
00670 if (cb_res == CALLBACK_FAILED) return CommandCost();
00671
00672
00673 if (statspec->grf_prop.grffile->grf_version < 8) ToggleBit(cb_res, 10);
00674 return GetErrorMessageFromLocationCallbackResult(cb_res, statspec->grf_prop.grffile, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
00675 }
00676
00677
00685 int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
00686 {
00687 uint i;
00688
00689 if (statspec == NULL || st == NULL) return 0;
00690
00691 for (i = 1; i < st->num_specs && i < NUM_STATIONSSPECS_PER_STATION; i++) {
00692 if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
00693 }
00694
00695 if (i == NUM_STATIONSSPECS_PER_STATION) {
00696
00697
00698
00699
00700
00701 for (i = 1; i < st->num_specs && i < NUM_STATIONSSPECS_PER_STATION; i++) {
00702 if (st->speclist[i].spec == statspec) return i;
00703 }
00704
00705 return -1;
00706 }
00707
00708 if (exec) {
00709 if (i >= st->num_specs) {
00710 st->num_specs = i + 1;
00711 st->speclist = ReallocT(st->speclist, st->num_specs);
00712
00713 if (st->num_specs == 2) {
00714
00715 st->speclist[0].spec = NULL;
00716 st->speclist[0].grfid = 0;
00717 st->speclist[0].localidx = 0;
00718 }
00719 }
00720
00721 st->speclist[i].spec = statspec;
00722 st->speclist[i].grfid = statspec->grf_prop.grffile->grfid;
00723 st->speclist[i].localidx = statspec->grf_prop.local_id;
00724
00725 StationUpdateCachedTriggers(st);
00726 }
00727
00728 return i;
00729 }
00730
00731
00738 void DeallocateSpecFromStation(BaseStation *st, byte specindex)
00739 {
00740
00741 if (specindex == 0) return;
00742
00743 ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE);
00744
00745 TILE_AREA_LOOP(tile, area) {
00746 if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
00747 return;
00748 }
00749 }
00750
00751
00752 st->speclist[specindex].spec = NULL;
00753 st->speclist[specindex].grfid = 0;
00754 st->speclist[specindex].localidx = 0;
00755
00756
00757 if (specindex == st->num_specs - 1) {
00758 for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--) {}
00759
00760 if (st->num_specs > 1) {
00761 st->speclist = ReallocT(st->speclist, st->num_specs);
00762 } else {
00763 free(st->speclist);
00764 st->num_specs = 0;
00765 st->speclist = NULL;
00766 st->cached_anim_triggers = 0;
00767 st->cached_cargo_triggers = 0;
00768 return;
00769 }
00770 }
00771
00772 StationUpdateCachedTriggers(st);
00773 }
00774
00785 bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
00786 {
00787 const DrawTileSprites *sprites = NULL;
00788 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
00789 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
00790 uint tile = 2;
00791
00792 const StationSpec *statspec = StationClass::Get(sclass)->GetSpec(station);
00793 if (statspec == NULL) return false;
00794
00795 if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
00796 uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
00797 if (callback != CALLBACK_FAILED) tile = callback;
00798 }
00799
00800 uint32 total_offset = rti->GetRailtypeSpriteOffset();
00801 uint32 relocation = 0;
00802 uint32 ground_relocation = 0;
00803 const NewGRFSpriteLayout *layout = NULL;
00804 DrawTileSprites tmp_rail_layout;
00805
00806 if (statspec->renderdata == NULL) {
00807 sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
00808 } else {
00809 layout = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
00810 if (!layout->NeedsPreprocessing()) {
00811 sprites = layout;
00812 layout = NULL;
00813 }
00814 }
00815
00816 if (layout != NULL) {
00817
00818 bool separate_ground = HasBit(statspec->flags, SSF_SEPARATE_GROUND);
00819 uint32 var10_values = layout->PrepareLayout(total_offset, rti->fallback_railtype, 0, 0, separate_ground);
00820 uint8 var10;
00821 FOR_EACH_SET_BIT(var10, var10_values) {
00822 uint32 var10_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, var10);
00823 layout->ProcessRegisters(var10, var10_relocation, separate_ground);
00824 }
00825
00826 tmp_rail_layout.seq = layout->GetLayout(&tmp_rail_layout.ground);
00827 sprites = &tmp_rail_layout;
00828 total_offset = 0;
00829 } else {
00830
00831 ground_relocation = relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 0);
00832 if (HasBit(sprites->ground.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
00833 ground_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 1);
00834 }
00835 ground_relocation += rti->fallback_railtype;
00836 }
00837
00838 SpriteID image = sprites->ground.sprite;
00839 PaletteID pal = sprites->ground.pal;
00840 RailTrackOffset overlay_offset;
00841 if (rti->UsesOverlay() && SplitGroundSpriteForOverlay(NULL, &image, &overlay_offset)) {
00842 SpriteID ground = GetCustomRailSprite(rti, INVALID_TILE, RTSG_GROUND);
00843 DrawSprite(image, PAL_NONE, x, y);
00844 DrawSprite(ground + overlay_offset, PAL_NONE, x, y);
00845 } else {
00846 image += HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? ground_relocation : total_offset;
00847 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += ground_relocation;
00848 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00849 }
00850
00851 DrawRailTileSeqInGUI(x, y, sprites, total_offset, relocation, palette);
00852
00853 return true;
00854 }
00855
00856
00857 const StationSpec *GetStationSpec(TileIndex t)
00858 {
00859 if (!IsCustomStationSpecIndex(t)) return NULL;
00860
00861 const BaseStation *st = BaseStation::GetByTile(t);
00862 uint specindex = GetCustomStationSpecIndex(t);
00863 return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
00864 }
00865
00866
00873 bool IsStationTileBlocked(TileIndex tile)
00874 {
00875 const StationSpec *statspec = GetStationSpec(tile);
00876
00877 return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile));
00878 }
00879
00886 bool CanStationTileHavePylons(TileIndex tile)
00887 {
00888 const StationSpec *statspec = GetStationSpec(tile);
00889 uint gfx = GetStationGfx(tile);
00890
00891 return statspec != NULL ? HasBit(statspec->pylons, gfx) : gfx < 4;
00892 }
00893
00900 bool CanStationTileHaveWires(TileIndex tile)
00901 {
00902 const StationSpec *statspec = GetStationSpec(tile);
00903 return statspec == NULL || !HasBit(statspec->wires, GetStationGfx(tile));
00904 }
00905
00907 uint16 GetAnimStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, BaseStation *st, TileIndex tile, int extra_data)
00908 {
00909 return GetStationCallback(callback, param1, param2, statspec, st, tile);
00910 }
00911
00913 struct StationAnimationBase : public AnimationBase<StationAnimationBase, StationSpec, BaseStation, int, GetAnimStationCallback> {
00914 static const CallbackID cb_animation_speed = CBID_STATION_ANIMATION_SPEED;
00915 static const CallbackID cb_animation_next_frame = CBID_STATION_ANIM_NEXT_FRAME;
00916
00917 static const StationCallbackMask cbm_animation_speed = CBM_STATION_ANIMATION_SPEED;
00918 static const StationCallbackMask cbm_animation_next_frame = CBM_STATION_ANIMATION_NEXT_FRAME;
00919 };
00920
00921 void AnimateStationTile(TileIndex tile)
00922 {
00923 const StationSpec *ss = GetStationSpec(tile);
00924 if (ss == NULL) return;
00925
00926 StationAnimationBase::AnimateTile(ss, BaseStation::GetByTile(tile), tile, HasBit(ss->flags, SSF_CB141_RANDOM_BITS));
00927 }
00928
00929 void TriggerStationAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type)
00930 {
00931
00932 static const TriggerArea tas[] = {
00933 TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
00934 };
00935
00936
00937 if (st == NULL) st = BaseStation::GetByTile(tile);
00938
00939
00940
00941 if (!HasBit(st->cached_anim_triggers, trigger)) return;
00942
00943 uint16 random_bits = Random();
00944 ETileArea area = ETileArea(st, tile, tas[trigger]);
00945
00946
00947 TILE_AREA_LOOP(tile, area) {
00948 if (st->TileBelongsToRailStation(tile)) {
00949 const StationSpec *ss = GetStationSpec(tile);
00950 if (ss != NULL && HasBit(ss->animation.triggers, trigger)) {
00951 CargoID cargo;
00952 if (cargo_type == CT_INVALID) {
00953 cargo = CT_INVALID;
00954 } else {
00955 cargo = ss->grf_prop.grffile->cargo_map[cargo_type];
00956 }
00957 StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, tile, (random_bits << 16) | Random(), (uint8)trigger | (cargo << 8));
00958 }
00959 }
00960 }
00961 }
00962
00970 void TriggerStationRandomisation(Station *st, TileIndex tile, StationRandomTrigger trigger, CargoID cargo_type)
00971 {
00972
00973 static const TriggerArea tas[] = {
00974 TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM
00975 };
00976
00977
00978 if (st == NULL) st = Station::GetByTile(tile);
00979
00980
00981
00982 if (st->cached_cargo_triggers == 0) return;
00983 if (cargo_type != CT_INVALID && !HasBit(st->cached_cargo_triggers, cargo_type)) return;
00984
00985 uint32 whole_reseed = 0;
00986 ETileArea area = ETileArea(st, tile, tas[trigger]);
00987
00988 uint32 empty_mask = 0;
00989 if (trigger == SRT_CARGO_TAKEN) {
00990
00991 for (CargoID i = 0; i < NUM_CARGO; i++) {
00992 if (st->goods[i].cargo.TotalCount() == 0) {
00993 SetBit(empty_mask, i);
00994 }
00995 }
00996 }
00997
00998
00999 uint8 trigger_bit = 1 << trigger;
01000
01001
01002 TILE_AREA_LOOP(tile, area) {
01003 if (st->TileBelongsToRailStation(tile)) {
01004 const StationSpec *ss = GetStationSpec(tile);
01005 if (ss == NULL) continue;
01006
01007
01008
01009 if (trigger == SRT_CARGO_TAKEN) {
01010 if ((ss->cargo_triggers & ~empty_mask) != 0) continue;
01011 }
01012
01013 if (cargo_type == CT_INVALID || HasBit(ss->cargo_triggers, cargo_type)) {
01014 StationResolverObject object(ss, st, tile, CBID_RANDOM_TRIGGER, 0);
01015 object.trigger = trigger_bit;
01016
01017 const SpriteGroup *group = object.Resolve();
01018 if (group == NULL) continue;
01019
01020 uint32 reseed = object.GetReseedSum();
01021 if (reseed != 0) {
01022 whole_reseed |= reseed;
01023 reseed >>= 16;
01024
01025
01026 uint8 random_bits = GetStationTileRandomBits(tile);
01027 random_bits &= ~reseed;
01028 random_bits |= Random() & reseed;
01029 SetStationTileRandomBits(tile, random_bits);
01030
01031 MarkTileDirtyByTile(tile);
01032 }
01033 }
01034 }
01035 }
01036
01037
01038 if ((whole_reseed & 0xFFFF) != 0) {
01039 st->random_bits &= ~whole_reseed;
01040 st->random_bits |= Random() & whole_reseed;
01041 }
01042 }
01043
01048 void StationUpdateCachedTriggers(BaseStation *st)
01049 {
01050 st->cached_anim_triggers = 0;
01051 st->cached_cargo_triggers = 0;
01052
01053
01054
01055 for (uint i = 0; i < st->num_specs; i++) {
01056 const StationSpec *ss = st->speclist[i].spec;
01057 if (ss != NULL) {
01058 st->cached_anim_triggers |= ss->animation.triggers;
01059 st->cached_cargo_triggers |= ss->cargo_triggers;
01060 }
01061 }
01062 }
01063