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.h"
00027 #include "newgrf_animation_base.h"
00028 #include "newgrf_class_func.h"
00029
00030
00031 template <typename Tspec, typename Tid, Tid Tmax>
00032 void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
00033 {
00034
00035 classes[0].global_id = 'DFLT';
00036 classes[0].name = STR_STATION_CLASS_DFLT;
00037 classes[0].count = 1;
00038 classes[0].spec = MallocT<StationSpec*>(1);
00039 classes[0].spec[0] = NULL;
00040
00041 classes[1].global_id = 'WAYP';
00042 classes[1].name = STR_STATION_CLASS_WAYP;
00043 classes[1].count = 1;
00044 classes[1].spec = MallocT<StationSpec*>(1);
00045 classes[1].spec[0] = NULL;
00046 }
00047
00048 INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX)
00049
00050 static const uint MAX_SPECLIST = 255;
00051
00052 enum TriggerArea {
00053 TA_TILE,
00054 TA_PLATFORM,
00055 TA_WHOLE,
00056 };
00057
00058 struct ETileArea : TileArea {
00059 ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
00060 {
00061 switch (ta) {
00062 default: NOT_REACHED();
00063
00064 case TA_TILE:
00065 this->tile = tile;
00066 this->w = 1;
00067 this->h = 1;
00068 break;
00069
00070 case TA_PLATFORM: {
00071 TileIndex start, end;
00072 Axis axis = GetRailStationAxis(tile);
00073 TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
00074
00075 for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(tile, end + delta); end += delta) { }
00076 for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(tile, start - delta); start -= delta) { }
00077
00078 this->tile = start;
00079 this->w = TileX(end) - TileX(start) + 1;
00080 this->h = TileY(end) - TileY(start) + 1;
00081 break;
00082 }
00083
00084 case TA_WHOLE:
00085 st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT);
00086 break;
00087 }
00088 }
00089 };
00090
00091
00104 uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
00105 {
00106 uint32 retval = 0;
00107
00108 if (axis == AXIS_X) {
00109 Swap(platforms, length);
00110 Swap(x, y);
00111 }
00112
00113
00114 platforms = min(15, platforms);
00115 length = min(15, length);
00116 x = min(15, x);
00117 y = min(15, y);
00118 if (centred) {
00119 x -= platforms / 2;
00120 y -= length / 2;
00121 SB(retval, 0, 4, y & 0xF);
00122 SB(retval, 4, 4, x & 0xF);
00123 } else {
00124 SB(retval, 0, 4, y);
00125 SB(retval, 4, 4, length - y - 1);
00126 SB(retval, 8, 4, x);
00127 SB(retval, 12, 4, platforms - x - 1);
00128 }
00129 SB(retval, 16, 4, length);
00130 SB(retval, 20, 4, platforms);
00131 SB(retval, 24, 4, tile);
00132
00133 return retval;
00134 }
00135
00136
00145 static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
00146 {
00147 byte orig_type = 0;
00148 Axis orig_axis = AXIS_X;
00149 StationID sid = GetStationIndex(tile);
00150
00151 if (check_type) orig_type = GetCustomStationSpecIndex(tile);
00152 if (check_axis) orig_axis = GetRailStationAxis(tile);
00153
00154 while (true) {
00155 TileIndex new_tile = TILE_ADD(tile, delta);
00156
00157 if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
00158 if (!HasStationRail(new_tile)) break;
00159 if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
00160 if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
00161
00162 tile = new_tile;
00163 }
00164 return tile;
00165 }
00166
00167
00168 static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
00169 {
00170 int tx = TileX(tile);
00171 int ty = TileY(tile);
00172 int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1, 0), check_type, check_axis));
00173 int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
00174 int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1, 0), check_type, check_axis)) + 1;
00175 int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0, 1), check_type, check_axis)) + 1;
00176
00177 tx -= sx; ex -= sx;
00178 ty -= sy; ey -= sy;
00179
00180 return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
00181 }
00182
00183
00184 static uint32 GetRailContinuationInfo(TileIndex tile)
00185 {
00186
00187 static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
00188 static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
00189
00190
00191 static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
00192 static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
00193
00194 Axis axis = GetRailStationAxis(tile);
00195
00196
00197 const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
00198 const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
00199
00200 uint32 res = 0;
00201 uint i;
00202
00203 for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
00204 TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
00205 TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
00206 if (trackbits != TRACK_BIT_NONE) {
00207
00208 SetBit(res, i + 8);
00209
00210
00211
00212 if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
00213 continue;
00214 }
00215
00216
00217 if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
00218 }
00219 }
00220
00221 return res;
00222 }
00223
00224
00225
00226 static uint32 StationGetRandomBits(const ResolverObject *object)
00227 {
00228 const BaseStation *st = object->u.station.st;
00229 const TileIndex tile = object->u.station.tile;
00230 return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
00231 }
00232
00233
00234 static uint32 StationGetTriggers(const ResolverObject *object)
00235 {
00236 const BaseStation *st = object->u.station.st;
00237 return st == NULL ? 0 : st->waiting_triggers;
00238 }
00239
00240
00241 static void StationSetTriggers(const ResolverObject *object, int triggers)
00242 {
00243 BaseStation *st = const_cast<BaseStation *>(object->u.station.st);
00244 assert(st != NULL);
00245 st->waiting_triggers = triggers;
00246 }
00247
00253 static struct {
00254 uint32 v40;
00255 uint32 v41;
00256 uint32 v45;
00257 uint32 v46;
00258 uint32 v47;
00259 uint32 v49;
00260 uint8 valid;
00261 } _svc;
00262
00263 static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00264 {
00265 const BaseStation *st = object->u.station.st;
00266 TileIndex tile = object->u.station.tile;
00267
00268 if (object->scope == VSG_SCOPE_PARENT) {
00269
00270 const Town *t;
00271
00272 if (st != NULL) {
00273 t = st->town;
00274 } else if (tile != INVALID_TILE) {
00275 t = ClosestTownFromTile(tile, UINT_MAX);
00276 } else {
00277 *available = false;
00278 return UINT_MAX;
00279 }
00280
00281 return TownGetVariable(variable, parameter, available, t);
00282 }
00283
00284 if (st == NULL) {
00285
00286 switch (variable) {
00287 case 0x40:
00288 case 0x41:
00289 case 0x46:
00290 case 0x47:
00291 case 0x49: return 0x2110000;
00292 case 0x42: return 0;
00293 case 0x43: return _current_company;
00294 case 0x44: return 2;
00295 case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00296 }
00297
00298 *available = false;
00299 return UINT_MAX;
00300 }
00301
00302 switch (variable) {
00303
00304 case 0x40:
00305 if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); }
00306 return _svc.v40;
00307
00308 case 0x41:
00309 if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true, false, false); SetBit(_svc.valid, 1); }
00310 return _svc.v41;
00311
00312 case 0x42: return GetTerrainType(tile) | (GetReverseRailTypeTranslation(GetRailType(tile), object->u.station.statspec->grf_prop.grffile) << 8);
00313 case 0x43: return st->owner;
00314 case 0x44: return HasStationReservation(tile) ? 7 : 4;
00315 case 0x45:
00316 if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); }
00317 return _svc.v45;
00318
00319 case 0x46:
00320 if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); }
00321 return _svc.v46;
00322
00323 case 0x47:
00324 if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true, false, true); SetBit(_svc.valid, 4); }
00325 return _svc.v47;
00326
00327 case 0x49:
00328 if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(tile, false, true, false); SetBit(_svc.valid, 5); }
00329 return _svc.v49;
00330
00331 case 0x4A:
00332 return GetAnimationFrame(tile);
00333
00334
00335
00336 case 0x66:
00337 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00338 return st->TileBelongsToRailStation(tile) ? GetAnimationFrame(tile) : UINT_MAX;
00339
00340 case 0x67: {
00341 Axis axis = GetRailStationAxis(tile);
00342
00343 if (parameter != 0) tile = GetNearbyTile(parameter, tile);
00344
00345 Slope tileh = GetTileSlope(tile, NULL);
00346 bool swap = (axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
00347
00348 return GetNearbyTileInformation(tile) ^ (swap ? SLOPE_EW : 0);
00349 }
00350
00351 case 0x68: {
00352 TileIndex nearby_tile = GetNearbyTile(parameter, tile);
00353
00354 if (!HasStationTileRail(nearby_tile)) return 0xFFFFFFFF;
00355
00356 uint32 grfid = st->speclist[GetCustomStationSpecIndex(tile)].grfid;
00357 bool perpendicular = GetRailStationAxis(tile) != GetRailStationAxis(nearby_tile);
00358 bool same_station = st->TileBelongsToRailStation(nearby_tile);
00359 uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
00360
00361 if (IsCustomStationSpecIndex(nearby_tile)) {
00362 const StationSpecList ssl = BaseStation::GetByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
00363 res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
00364 }
00365 return res;
00366 }
00367
00368
00369 case 0x82: return 50;
00370 case 0x84: return st->string_id;
00371 case 0x86: return 0;
00372 case 0xF0: return st->facilities;
00373 case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00374 }
00375
00376 return st->GetNewGRFVariable(object, variable, parameter, available);
00377 }
00378
00379 uint32 Station::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
00380 {
00381 switch (variable) {
00382 case 0x48: {
00383 CargoID cargo_type;
00384 uint32 value = 0;
00385
00386 for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00387 if (HasBit(this->goods[cargo_type].acceptance_pickup, GoodsEntry::PICKUP)) SetBit(value, cargo_type);
00388 }
00389 return value;
00390 }
00391
00392 case 0x8A: return this->had_vehicle_of_type;
00393 case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE;
00394 case 0xF2: return (this->truck_stops != NULL) ? this->truck_stops->status : 0;
00395 case 0xF3: return (this->bus_stops != NULL) ? this->bus_stops->status : 0;
00396 case 0xF6: return this->airport.flags;
00397 case 0xF7: return GB(this->airport.flags, 8, 8);
00398 }
00399
00400
00401 if (variable >= 0x60 && variable <= 0x65) {
00402 CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grf_prop.grffile);
00403
00404 if (c == CT_INVALID) return 0;
00405 const GoodsEntry *ge = &this->goods[c];
00406
00407 switch (variable) {
00408 case 0x60: return min(ge->cargo.Count(), 4095);
00409 case 0x61: return ge->days_since_pickup;
00410 case 0x62: return ge->rating;
00411 case 0x63: return ge->cargo.DaysInTransit();
00412 case 0x64: return ge->last_speed | (ge->last_age << 8);
00413 case 0x65: return GB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 3;
00414 }
00415 }
00416
00417
00418 if (variable >= 0x8C && variable <= 0xEC) {
00419 const GoodsEntry *g = &this->goods[GB(variable - 0x8C, 3, 4)];
00420 switch (GB(variable - 0x8C, 0, 3)) {
00421 case 0: return g->cargo.Count();
00422 case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (GB(g->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 7);
00423 case 2: return g->days_since_pickup;
00424 case 3: return g->rating;
00425 case 4: return g->cargo.Source();
00426 case 5: return g->cargo.DaysInTransit();
00427 case 6: return g->last_speed;
00428 case 7: return g->last_age;
00429 }
00430 }
00431
00432 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
00433
00434 *available = false;
00435 return UINT_MAX;
00436 }
00437
00438 uint32 Waypoint::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
00439 {
00440 switch (variable) {
00441 case 0x48: return 0;
00442 case 0x8A: return HVOT_WAYPOINT;
00443 case 0xF1: return 0;
00444 case 0xF2: return 0;
00445 case 0xF3: return 0;
00446 case 0xF6: return 0;
00447 case 0xF7: return 0;
00448 }
00449
00450
00451 if (variable >= 0x60 && variable <= 0x65) {
00452 return 0;
00453 }
00454
00455
00456 if (variable >= 0x8C && variable <= 0xEC) {
00457 switch (GB(variable - 0x8C, 0, 3)) {
00458 case 3: return INITIAL_STATION_RATING;
00459 case 4: return INVALID_STATION;
00460 default: return 0;
00461 }
00462 }
00463
00464 DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
00465
00466 *available = false;
00467 return UINT_MAX;
00468 }
00469
00470 static const SpriteGroup *StationResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00471 {
00472 const BaseStation *bst = object->u.station.st;
00473 const StationSpec *statspec = object->u.station.statspec;
00474 uint set;
00475
00476 uint cargo = 0;
00477 CargoID cargo_type = object->u.station.cargo_type;
00478
00479 if (bst == NULL || statspec->cls_id == STAT_CLASS_WAYP) {
00480 return group->loading[0];
00481 }
00482
00483 const Station *st = Station::From(bst);
00484
00485 switch (cargo_type) {
00486 case CT_INVALID:
00487 case CT_DEFAULT_NA:
00488 case CT_PURCHASE:
00489 cargo = 0;
00490 break;
00491
00492 case CT_DEFAULT:
00493 for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00494 cargo += st->goods[cargo_type].cargo.Count();
00495 }
00496 break;
00497
00498 default:
00499 cargo = st->goods[cargo_type].cargo.Count();
00500 break;
00501 }
00502
00503 if (HasBit(statspec->flags, SSF_DIV_BY_STATION_SIZE)) cargo /= (st->train_station.w + st->train_station.h);
00504 cargo = min(0xfff, cargo);
00505
00506 if (cargo > statspec->cargo_threshold) {
00507 if (group->num_loading > 0) {
00508 set = ((cargo - statspec->cargo_threshold) * group->num_loading) / (4096 - statspec->cargo_threshold);
00509 return group->loading[set];
00510 }
00511 } else {
00512 if (group->num_loaded > 0) {
00513 set = (cargo * group->num_loaded) / (statspec->cargo_threshold + 1);
00514 return group->loaded[set];
00515 }
00516 }
00517
00518 return group->loading[0];
00519 }
00520
00521
00522 static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00523 {
00524 res->GetRandomBits = StationGetRandomBits;
00525 res->GetTriggers = StationGetTriggers;
00526 res->SetTriggers = StationSetTriggers;
00527 res->GetVariable = StationGetVariable;
00528 res->ResolveReal = StationResolveReal;
00529
00530 res->u.station.st = st;
00531 res->u.station.statspec = statspec;
00532 res->u.station.tile = tile;
00533
00534 res->callback = CBID_NO_CALLBACK;
00535 res->callback_param1 = 0;
00536 res->callback_param2 = 0;
00537 res->last_value = 0;
00538 res->trigger = 0;
00539 res->reseed = 0;
00540 res->count = 0;
00541 res->grffile = (statspec != NULL ? statspec->grf_prop.grffile : NULL);
00542 }
00543
00544 static const SpriteGroup *ResolveStation(ResolverObject *object)
00545 {
00546 const SpriteGroup *group;
00547 CargoID ctype = CT_DEFAULT_NA;
00548
00549 if (object->u.station.st == NULL) {
00550
00551 ctype = CT_PURCHASE;
00552 } else if (Station::IsExpected(object->u.station.st)) {
00553 const Station *st = Station::From(object->u.station.st);
00554
00555 const CargoSpec *cs;
00556 FOR_ALL_CARGOSPECS(cs) {
00557 if (object->u.station.statspec->grf_prop.spritegroup[cs->Index()] != NULL &&
00558 !st->goods[cs->Index()].cargo.Empty()) {
00559 ctype = cs->Index();
00560 break;
00561 }
00562 }
00563 }
00564
00565 group = object->u.station.statspec->grf_prop.spritegroup[ctype];
00566 if (group == NULL) {
00567 ctype = CT_DEFAULT;
00568 group = object->u.station.statspec->grf_prop.spritegroup[ctype];
00569 }
00570
00571 if (group == NULL) return NULL;
00572
00573
00574 object->u.station.cargo_type = ctype;
00575
00576
00577 _svc.valid = 0;
00578
00579 return SpriteGroup::Resolve(group, object);
00580 }
00581
00582 SpriteID GetCustomStationRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00583 {
00584 const SpriteGroup *group;
00585 ResolverObject object;
00586
00587 NewStationResolver(&object, statspec, st, tile);
00588
00589 group = ResolveStation(&object);
00590 if (group == NULL || group->type != SGT_RESULT) return 0;
00591 return group->GetResult() - 0x42D;
00592 }
00593
00594
00595 SpriteID GetCustomStationGroundRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00596 {
00597 const SpriteGroup *group;
00598 ResolverObject object;
00599
00600 NewStationResolver(&object, statspec, st, tile);
00601 if (HasBit(statspec->flags, SSF_SEPARATE_GROUND)) {
00602 object.callback_param1 = 1;
00603 }
00604
00605 group = ResolveStation(&object);
00606 if (group == NULL || group->type != SGT_RESULT) return 0;
00607 return group->GetResult() - 0x42D;
00608 }
00609
00610
00611 SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00612 {
00613 const SpriteGroup *group;
00614 ResolverObject object;
00615
00616 NewStationResolver(&object, statspec, st, tile);
00617 object.callback_param1 = 2;
00618
00619 group = ResolveStation(&object);
00620 if (group == NULL || group->type != SGT_RESULT) return 0;
00621 return group->GetResult() + GetRegister(0x100);
00622 }
00623
00624
00625 uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, const BaseStation *st, TileIndex tile)
00626 {
00627 const SpriteGroup *group;
00628 ResolverObject object;
00629
00630 NewStationResolver(&object, statspec, st, tile);
00631
00632 object.callback = callback;
00633 object.callback_param1 = param1;
00634 object.callback_param2 = param2;
00635
00636 group = ResolveStation(&object);
00637 if (group == NULL) return CALLBACK_FAILED;
00638 return group->GetCallbackResult();
00639 }
00640
00641
00649 int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
00650 {
00651 uint i;
00652
00653 if (statspec == NULL || st == NULL) return 0;
00654
00655 for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
00656 if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
00657 }
00658
00659 if (i == MAX_SPECLIST) {
00660
00661
00662
00663
00664
00665 for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
00666 if (st->speclist[i].spec == statspec) return i;
00667 }
00668
00669 return -1;
00670 }
00671
00672 if (exec) {
00673 if (i >= st->num_specs) {
00674 st->num_specs = i + 1;
00675 st->speclist = ReallocT(st->speclist, st->num_specs);
00676
00677 if (st->num_specs == 2) {
00678
00679 st->speclist[0].spec = NULL;
00680 st->speclist[0].grfid = 0;
00681 st->speclist[0].localidx = 0;
00682 }
00683 }
00684
00685 st->speclist[i].spec = statspec;
00686 st->speclist[i].grfid = statspec->grf_prop.grffile->grfid;
00687 st->speclist[i].localidx = statspec->grf_prop.local_id;
00688 }
00689
00690 return i;
00691 }
00692
00693
00700 void DeallocateSpecFromStation(BaseStation *st, byte specindex)
00701 {
00702
00703 if (specindex == 0) return;
00704
00705 ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE);
00706
00707 TILE_AREA_LOOP(tile, area) {
00708 if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
00709 return;
00710 }
00711 }
00712
00713
00714 st->speclist[specindex].spec = NULL;
00715 st->speclist[specindex].grfid = 0;
00716 st->speclist[specindex].localidx = 0;
00717
00718
00719 if (specindex == st->num_specs - 1) {
00720 for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--) {}
00721
00722 if (st->num_specs > 1) {
00723 st->speclist = ReallocT(st->speclist, st->num_specs);
00724 } else {
00725 free(st->speclist);
00726 st->num_specs = 0;
00727 st->speclist = NULL;
00728 st->cached_anim_triggers = 0;
00729 return;
00730 }
00731 }
00732
00733 StationUpdateAnimTriggers(st);
00734 }
00735
00746 bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
00747 {
00748 const StationSpec *statspec;
00749 const DrawTileSprites *sprites;
00750 const RailtypeInfo *rti = GetRailTypeInfo(railtype);
00751 PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
00752 uint tile = 2;
00753
00754 statspec = StationClass::Get(sclass, station);
00755 if (statspec == NULL) return false;
00756
00757 uint relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE);
00758
00759 if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
00760 uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
00761 if (callback != CALLBACK_FAILED) tile = callback;
00762 }
00763
00764 if (statspec->renderdata == NULL) {
00765 sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
00766 } else {
00767 sprites = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
00768 }
00769
00770 SpriteID image = sprites->ground.sprite;
00771 PaletteID pal = sprites->ground.pal;
00772 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
00773 image += GetCustomStationGroundRelocation(statspec, NULL, INVALID_TILE);
00774 image += rti->custom_ground_offset;
00775 } else {
00776 image += rti->total_offset;
00777 }
00778
00779 DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
00780
00781 DrawRailTileSeqInGUI(x, y, sprites, rti->total_offset, relocation, palette);
00782
00783 return true;
00784 }
00785
00786
00787 const StationSpec *GetStationSpec(TileIndex t)
00788 {
00789 if (!IsCustomStationSpecIndex(t)) return NULL;
00790
00791 const BaseStation *st = BaseStation::GetByTile(t);
00792 uint specindex = GetCustomStationSpecIndex(t);
00793 return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
00794 }
00795
00796
00803 bool IsStationTileBlocked(TileIndex tile)
00804 {
00805 const StationSpec *statspec = GetStationSpec(tile);
00806
00807 return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile));
00808 }
00809
00816 bool IsStationTileElectrifiable(TileIndex tile)
00817 {
00818 const StationSpec *statspec = GetStationSpec(tile);
00819
00820 return statspec == NULL ||
00821 HasBit(statspec->pylons, GetStationGfx(tile)) ||
00822 !HasBit(statspec->wires, GetStationGfx(tile));
00823 }
00824
00826 struct StationAnimationBase : public AnimationBase<StationAnimationBase, StationSpec, BaseStation, GetStationCallback> {
00827 static const CallbackID cb_animation_speed = CBID_STATION_ANIMATION_SPEED;
00828 static const CallbackID cb_animation_next_frame = CBID_STATION_ANIM_NEXT_FRAME;
00829
00830 static const StationCallbackMask cbm_animation_speed = CBM_STATION_ANIMATION_SPEED;
00831 static const StationCallbackMask cbm_animation_next_frame = CBM_STATION_ANIMATION_NEXT_FRAME;
00832 };
00833
00834 void AnimateStationTile(TileIndex tile)
00835 {
00836 const StationSpec *ss = GetStationSpec(tile);
00837 if (ss == NULL) return;
00838
00839 StationAnimationBase::AnimateTile(ss, BaseStation::GetByTile(tile), tile, HasBit(ss->flags, SSF_CB141_RANDOM_BITS));
00840 }
00841
00842 void TriggerStationAnimation(const BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type)
00843 {
00844
00845 static const TriggerArea tas[] = {
00846 TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
00847 };
00848
00849
00850 if (st == NULL) st = BaseStation::GetByTile(tile);
00851
00852
00853
00854 if (!HasBit(st->cached_anim_triggers, trigger)) return;
00855
00856 uint16 random_bits = Random();
00857 ETileArea area = ETileArea(st, tile, tas[trigger]);
00858
00859
00860 TILE_AREA_LOOP(tile, area) {
00861 if (st->TileBelongsToRailStation(tile)) {
00862 const StationSpec *ss = GetStationSpec(tile);
00863 if (ss != NULL && HasBit(ss->animation.triggers, trigger)) {
00864 CargoID cargo;
00865 if (cargo_type == CT_INVALID) {
00866 cargo = CT_INVALID;
00867 } else {
00868 cargo = GetReverseCargoTranslation(cargo_type, ss->grf_prop.grffile);
00869 }
00870 StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, tile, (random_bits << 16) | Random(), (uint8)trigger | (cargo << 8));
00871 }
00872 }
00873 }
00874 }
00875
00880 void StationUpdateAnimTriggers(BaseStation *st)
00881 {
00882 st->cached_anim_triggers = 0;
00883
00884
00885
00886 for (uint i = 0; i < st->num_specs; i++) {
00887 const StationSpec *ss = st->speclist[i].spec;
00888 if (ss != NULL) st->cached_anim_triggers |= ss->animation.triggers;
00889 }
00890 }
00891
00897 void GetStationResolver(ResolverObject *ro, uint index)
00898 {
00899 NewStationResolver(ro, GetStationSpec(index), Station::GetByTile(index), index);
00900 }