# newgrf_station.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
    00033 {
    00034   /* Set up initial data */
    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) { /* Nothing */ }
    00076         for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(tile, start - delta); start -= delta) { /* Nothing */ }
    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   if (centred) {
    00114     x -= platforms / 2;
    00115     y -= length / 2;
    00116     x = Clamp(x, -8, 7);
    00117     y = Clamp(y, -8, 7);
    00118     SB(retval,  0, 4, y & 0xF);
    00119     SB(retval,  4, 4, x & 0xF);
    00120   } else {
    00121     SB(retval,  0, 4, min(15, y));
    00122     SB(retval,  4, 4, min(15, length - y - 1));
    00123     SB(retval,  8, 4, min(15, x));
    00124     SB(retval, 12, 4, min(15, platforms - x - 1));
    00125   }
    00126   SB(retval, 16, 4, min(15, length));
    00127   SB(retval, 20, 4, min(15, platforms));
    00128   SB(retval, 24, 4, tile);
    00129
    00130   return retval;
    00131 }
    00132
    00133
    00142 static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
    00143 {
    00144   byte orig_type = 0;
    00145   Axis orig_axis = AXIS_X;
    00146   StationID sid = GetStationIndex(tile);
    00147
    00148   if (check_type) orig_type = GetCustomStationSpecIndex(tile);
    00149   if (check_axis) orig_axis = GetRailStationAxis(tile);
    00150
    00151   for (;;) {
    00152     TileIndex new_tile = TILE_ADD(tile, delta);
    00153
    00154     if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
    00155     if (!HasStationRail(new_tile)) break;
    00156     if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
    00157     if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
    00158
    00159     tile = new_tile;
    00160   }
    00161   return tile;
    00162 }
    00163
    00164
    00165 static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
    00166 {
    00167   int tx = TileX(tile);
    00168   int ty = TileY(tile);
    00169   int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1,  0), check_type, check_axis));
    00170   int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
    00171   int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1,  0), check_type, check_axis)) + 1;
    00172   int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0,  1), check_type, check_axis)) + 1;
    00173
    00174   tx -= sx; ex -= sx;
    00175   ty -= sy; ey -= sy;
    00176
    00177   return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
    00178 }
    00179
    00180
    00181 static uint32 GetRailContinuationInfo(TileIndex tile)
    00182 {
    00183   /* Tile offsets and exit dirs for X axis */
    00184   static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
    00185   static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
    00186
    00187   /* Tile offsets and exit dirs for Y axis */
    00188   static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
    00189   static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
    00190
    00191   Axis axis = GetRailStationAxis(tile);
    00192
    00193   /* Choose appropriate lookup table to use */
    00194   const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
    00195   const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
    00196
    00197   uint32 res = 0;
    00198   uint i;
    00199
    00200   for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
    00201     TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
    00202     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
    00203     if (trackbits != TRACK_BIT_NONE) {
    00204       /* If there is any track on the tile, set the bit in the second byte */
    00205       SetBit(res, i + 8);
    00206
    00207       /* With tunnels and bridges the tile has tracks, but they are not necessarily connected
    00208        * with the next tile because the ramp is not going in the right direction. */
    00209       if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
    00210         continue;
    00211       }
    00212
    00213       /* If any track reaches our exit direction, set the bit in the lower byte */
    00214       if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
    00215     }
    00216   }
    00217
    00218   return res;
    00219 }
    00220
    00221
    00222 /* Station Resolver Functions */
    00223 static uint32 StationGetRandomBits(const ResolverObject *object)
    00224 {
    00225   const BaseStation *st = object->u.station.st;
    00226   const TileIndex tile = object->u.station.tile;
    00227   return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
    00228 }
    00229
    00230
    00231 static uint32 StationGetTriggers(const ResolverObject *object)
    00232 {
    00233   const BaseStation *st = object->u.station.st;
    00234   return st == NULL ? 0 : st->waiting_triggers;
    00235 }
    00236
    00237
    00238 static void StationSetTriggers(const ResolverObject *object, int triggers)
    00239 {
    00240   BaseStation *st = const_cast<BaseStation *>(object->u.station.st);
    00241   assert(st != NULL);
    00242   st->waiting_triggers = triggers;
    00243 }
    00244
    00250 static struct {
    00251   uint32 v40;
    00252   uint32 v41;
    00253   uint32 v45;
    00254   uint32 v46;
    00255   uint32 v47;
    00256   uint32 v49;
    00257   uint8 valid;
    00258 } _svc;
    00259
    00260 static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00261 {
    00262   const BaseStation *st = object->u.station.st;
    00263   TileIndex tile = object->u.station.tile;
    00264
    00265   if (object->scope == VSG_SCOPE_PARENT) {
    00266     /* Pass the request on to the town of the station */
    00267     const Town *t;
    00268
    00269     if (st != NULL) {
    00270       t = st->town;
    00271     } else if (tile != INVALID_TILE) {
    00272       t = ClosestTownFromTile(tile, UINT_MAX);
    00273     } else {
    00274       *available = false;
    00275       return UINT_MAX;
    00276     }
    00277
    00278     return TownGetVariable(variable, parameter, available, t);
    00279   }
    00280
    00281   if (st == NULL) {
    00282     /* Station does not exist, so we're in a purchase list */
    00283     switch (variable) {
    00284       case 0x40:
    00285       case 0x41:
    00286       case 0x46:
    00287       case 0x47:
    00288       case 0x49: return 0x2110000;        // Platforms, tracks & position
    00289       case 0x42: return 0;                // Rail type (XXX Get current type from GUI?)
    00290       case 0x43: return _current_company; // Station owner
    00291       case 0x44: return 2;                // PBS status
    00292       case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
    00293     }
    00294
    00295     *available = false;
    00296     return UINT_MAX;
    00297   }
    00298
    00299   switch (variable) {
    00300     /* Calculated station variables */
    00301     case 0x40:
    00302       if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); }
    00303       return _svc.v40;
    00304
    00305     case 0x41:
    00306       if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true,  false, false); SetBit(_svc.valid, 1); }
    00307       return _svc.v41;
    00308
    00309     case 0x42: return GetTerrainType(tile) | (GetReverseRailTypeTranslation(GetRailType(tile), object->u.station.statspec->grf_prop.grffile) << 8);
    00310     case 0x43: return st->owner; // Station owner
    00311     case 0x44: return HasStationReservation(tile) ? 7 : 4; // PBS status
    00312     case 0x45:
    00313       if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); }
    00314       return _svc.v45;
    00315
    00316     case 0x46:
    00317       if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); }
    00318       return _svc.v46;
    00319
    00320     case 0x47:
    00321       if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true,  false, true); SetBit(_svc.valid, 4); }
    00322       return _svc.v47;
    00323
    00324     case 0x49:
    00325       if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(tile, false, true, false); SetBit(_svc.valid, 5); }
    00326       return _svc.v49;
    00327
    00328     case 0x4A: // Animation frame of tile
    00329       return GetAnimationFrame(tile);
    00330
    00331     /* Variables which use the parameter */
    00332     /* Variables 0x60 to 0x65 and 0x69 are handled separately below */
    00333     case 0x66: // Animation frame of nearby tile
    00334       if (parameter != 0) tile = GetNearbyTile(parameter, tile);
    00335       return st->TileBelongsToRailStation(tile) ? GetAnimationFrame(tile) : UINT_MAX;
    00336
    00337     case 0x67: { // Land info of nearby tile
    00338       Axis axis = GetRailStationAxis(tile);
    00339
    00340       if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
    00341
    00342       Slope tileh = GetTileSlope(tile, NULL);
    00343       bool swap = (axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
    00344
    00345       return GetNearbyTileInformation(tile) ^ (swap ? SLOPE_EW : 0);
    00346     }
    00347
    00348     case 0x68: { // Station info of nearby tiles
    00349       TileIndex nearby_tile = GetNearbyTile(parameter, tile);
    00350
    00351       if (!HasStationTileRail(nearby_tile)) return 0xFFFFFFFF;
    00352
    00353       uint32 grfid = st->speclist[GetCustomStationSpecIndex(tile)].grfid;
    00354       bool perpendicular = GetRailStationAxis(tile) != GetRailStationAxis(nearby_tile);
    00355       bool same_station = st->TileBelongsToRailStation(nearby_tile);
    00356       uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
    00357
    00358       if (IsCustomStationSpecIndex(nearby_tile)) {
    00359         const StationSpecList ssl = BaseStation::GetByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
    00360         res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
    00361       }
    00362       return res;
    00363     }
    00364
    00365     /* General station variables */
    00366     case 0x82: return 50;
    00367     case 0x84: return st->string_id;
    00368     case 0x86: return 0;
    00369     case 0xF0: return st->facilities;
    00370     case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
    00371   }
    00372
    00373   return st->GetNewGRFVariable(object, variable, parameter, available);
    00374 }
    00375
    00376 uint32 Station::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
    00377 {
    00378   switch (variable) {
    00379     case 0x48: { // Accepted cargo types
    00380       CargoID cargo_type;
    00381       uint32 value = 0;
    00382
    00383       for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
    00384         if (HasBit(this->goods[cargo_type].acceptance_pickup, GoodsEntry::GES_PICKUP)) SetBit(value, cargo_type);
    00385       }
    00386       return value;
    00387     }
    00388
    00389     case 0x8A: return this->had_vehicle_of_type;
    00390     case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE;
    00391     case 0xF2: return (this->truck_stops != NULL) ? this->truck_stops->status : 0;
    00392     case 0xF3: return (this->bus_stops != NULL)   ? this->bus_stops->status   : 0;
    00393     case 0xF6: return this->airport.flags;
    00394     case 0xF7: return GB(this->airport.flags, 8, 8);
    00395   }
    00396
    00397   /* Handle cargo variables with parameter, 0x60 to 0x65 and 0x69 */
    00398   if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) {
    00399     CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grf_prop.grffile);
    00400
    00401     if (c == CT_INVALID) return 0;
    00402     const GoodsEntry *ge = &this->goods[c];
    00403
    00404     switch (variable) {
    00405       case 0x60: return min(ge->cargo.Count(), 4095);
    00406       case 0x61: return ge->days_since_pickup;
    00407       case 0x62: return ge->rating;
    00408       case 0x63: return ge->cargo.DaysInTransit();
    00409       case 0x64: return ge->last_speed | (ge->last_age << 8);
    00410       case 0x65: return GB(ge->acceptance_pickup, GoodsEntry::GES_ACCEPTANCE, 1) << 3;
    00411       case 0x69: return GB(ge->acceptance_pickup, GoodsEntry::GES_EVER_ACCEPTED, 4);
    00412     }
    00413   }
    00414
    00415   /* Handle cargo variables (deprecated) */
    00416   if (variable >= 0x8C && variable <= 0xEC) {
    00417     const GoodsEntry *g = &this->goods[GB(variable - 0x8C, 3, 4)];
    00418     switch (GB(variable - 0x8C, 0, 3)) {
    00419       case 0: return g->cargo.Count();
    00420       case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (GB(g->acceptance_pickup, GoodsEntry::GES_ACCEPTANCE, 1) << 7);
    00421       case 2: return g->days_since_pickup;
    00422       case 3: return g->rating;
    00423       case 4: return g->cargo.Source();
    00424       case 5: return g->cargo.DaysInTransit();
    00425       case 6: return g->last_speed;
    00426       case 7: return g->last_age;
    00427     }
    00428   }
    00429
    00430   DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
    00431
    00432   *available = false;
    00433   return UINT_MAX;
    00434 }
    00435
    00436 uint32 Waypoint::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
    00437 {
    00438   switch (variable) {
    00439     case 0x48: return 0; // Accepted cargo types
    00440     case 0x8A: return HVOT_WAYPOINT;
    00441     case 0xF1: return 0; // airport type
    00442     case 0xF2: return 0; // truck stop status
    00443     case 0xF3: return 0; // bus stop status
    00444     case 0xF6: return 0; // airport flags
    00445     case 0xF7: return 0; // airport flags cont.
    00446   }
    00447
    00448   /* Handle cargo variables with parameter, 0x60 to 0x65 */
    00449   if (variable >= 0x60 && variable <= 0x65) {
    00450     return 0;
    00451   }
    00452
    00453   /* Handle cargo variables (deprecated) */
    00454   if (variable >= 0x8C && variable <= 0xEC) {
    00455     switch (GB(variable - 0x8C, 0, 3)) {
    00456       case 3: return INITIAL_STATION_RATING;
    00457       case 4: return INVALID_STATION;
    00458       default: return 0;
    00459     }
    00460   }
    00461
    00462   DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
    00463
    00464   *available = false;
    00465   return UINT_MAX;
    00466 }
    00467
    00468 static const SpriteGroup *StationResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00469 {
    00470   const BaseStation *bst = object->u.station.st;
    00471   const StationSpec *statspec = object->u.station.statspec;
    00472   uint set;
    00473
    00474   uint cargo = 0;
    00475   CargoID cargo_type = object->u.station.cargo_type;
    00476
    00477   if (bst == NULL || statspec->cls_id == STAT_CLASS_WAYP) {
    00478     return group->loading[0];
    00479   }
    00480
    00481   const Station *st = Station::From(bst);
    00482
    00483   switch (cargo_type) {
    00484     case CT_INVALID:
    00485     case CT_DEFAULT_NA:
    00486     case CT_PURCHASE:
    00487       cargo = 0;
    00488       break;
    00489
    00490     case CT_DEFAULT:
    00491       for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
    00492         cargo += st->goods[cargo_type].cargo.Count();
    00493       }
    00494       break;
    00495
    00496     default:
    00497       cargo = st->goods[cargo_type].cargo.Count();
    00498       break;
    00499   }
    00500
    00501   if (HasBit(statspec->flags, SSF_DIV_BY_STATION_SIZE)) cargo /= (st->train_station.w + st->train_station.h);
    00502   cargo = min(0xfff, cargo);
    00503
    00504   if (cargo > statspec->cargo_threshold) {
    00505     if (group->num_loading > 0) {
    00506       set = ((cargo - statspec->cargo_threshold) * group->num_loading) / (4096 - statspec->cargo_threshold);
    00507       return group->loading[set];
    00508     }
    00509   } else {
    00510     if (group->num_loaded > 0) {
    00511       set = (cargo * group->num_loaded) / (statspec->cargo_threshold + 1);
    00512       return group->loaded[set];
    00513     }
    00514   }
    00515
    00516   return group->loading[0];
    00517 }
    00518
    00519
    00520 static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, const BaseStation *st, TileIndex tile)
    00521 {
    00522   res->GetRandomBits = StationGetRandomBits;
    00523   res->GetTriggers   = StationGetTriggers;
    00524   res->SetTriggers   = StationSetTriggers;
    00525   res->GetVariable   = StationGetVariable;
    00526   res->ResolveReal   = StationResolveReal;
    00527
    00528   res->u.station.st       = st;
    00529   res->u.station.statspec = statspec;
    00530   res->u.station.tile     = tile;
    00531
    00532   res->callback        = CBID_NO_CALLBACK;
    00533   res->callback_param1 = 0;
    00534   res->callback_param2 = 0;
    00535   res->last_value      = 0;
    00536   res->trigger         = 0;
    00537   res->reseed          = 0;
    00538   res->count           = 0;
    00539   res->grffile         = (statspec != NULL ? statspec->grf_prop.grffile : NULL);
    00540
    00541   /* Invalidate all cached vars */
    00542   _svc.valid = 0;
    00543 }
    00544
    00545 static const SpriteGroup *ResolveStation(ResolverObject *object)
    00546 {
    00547   const SpriteGroup *group;
    00548   CargoID ctype = CT_DEFAULT_NA;
    00549
    00550   if (object->u.station.st == NULL) {
    00551     /* No station, so we are in a purchase list */
    00552     ctype = CT_PURCHASE;
    00553   } else if (Station::IsExpected(object->u.station.st)) {
    00554     const Station *st = Station::From(object->u.station.st);
    00555     /* Pick the first cargo that we have waiting */
    00556     const CargoSpec *cs;
    00557     FOR_ALL_CARGOSPECS(cs) {
    00558       if (object->u.station.statspec->grf_prop.spritegroup[cs->Index()] != NULL &&
    00559           !st->goods[cs->Index()].cargo.Empty()) {
    00560         ctype = cs->Index();
    00561         break;
    00562       }
    00563     }
    00564   }
    00565
    00566   group = object->u.station.statspec->grf_prop.spritegroup[ctype];
    00567   if (group == NULL) {
    00568     ctype = CT_DEFAULT;
    00569     group = object->u.station.statspec->grf_prop.spritegroup[ctype];
    00570   }
    00571
    00572   if (group == NULL) return NULL;
    00573
    00574   /* Remember the cargo type we've picked */
    00575   object->u.station.cargo_type = ctype;
    00576
    00577   return SpriteGroup::Resolve(group, object);
    00578 }
    00579
    00588 SpriteID GetCustomStationRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile, uint32 var10)
    00589 {
    00590   const SpriteGroup *group;
    00591   ResolverObject object;
    00592
    00593   NewStationResolver(&object, statspec, st, tile);
    00594   object.callback_param1 = var10;
    00595
    00596   group = ResolveStation(&object);
    00597   if (group == NULL || group->type != SGT_RESULT) return 0;
    00598   return group->GetResult() - 0x42D;
    00599 }
    00600
    00610 SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, const BaseStation *st, TileIndex tile, uint layout, uint edge_info)
    00611 {
    00612   const SpriteGroup *group;
    00613   ResolverObject object;
    00614
    00615   NewStationResolver(&object, statspec, st, tile);
    00616   object.callback_param1 = 2; // Indicate we are resolving the foundation sprites
    00617   object.callback_param2 = layout | (edge_info << 16);
    00618
    00619   ClearRegister(0x100);
    00620   group = ResolveStation(&object);
    00621   if (group == NULL || group->type != SGT_RESULT) return 0;
    00622   return group->GetResult() + GetRegister(0x100);
    00623 }
    00624
    00625
    00626 uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, const BaseStation *st, TileIndex tile)
    00627 {
    00628   const SpriteGroup *group;
    00629   ResolverObject object;
    00630
    00631   NewStationResolver(&object, statspec, st, tile);
    00632
    00633   object.callback        = callback;
    00634   object.callback_param1 = param1;
    00635   object.callback_param2 = param2;
    00636
    00637   group = ResolveStation(&object);
    00638   if (group == NULL) return CALLBACK_FAILED;
    00639   return group->GetCallbackResult();
    00640 }
    00641
    00642
    00650 int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
    00651 {
    00652   uint i;
    00653
    00654   if (statspec == NULL || st == NULL) return 0;
    00655
    00656   for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
    00657     if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
    00658   }
    00659
    00660   if (i == MAX_SPECLIST) {
    00661     /* As final effort when the spec list is already full...
    00662      * try to find the same spec and return that one. This might
    00663      * result in slighty "wrong" (as per specs) looking stations,
    00664      * but it's fairly unlikely that one reaches the limit anyways.
    00665      */
    00666     for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
    00667       if (st->speclist[i].spec == statspec) return i;
    00668     }
    00669
    00670     return -1;
    00671   }
    00672
    00673   if (exec) {
    00674     if (i >= st->num_specs) {
    00675       st->num_specs = i + 1;
    00676       st->speclist = ReallocT(st->speclist, st->num_specs);
    00677
    00678       if (st->num_specs == 2) {
    00679         /* Initial allocation */
    00680         st->speclist[0].spec     = NULL;
    00681         st->speclist[0].grfid    = 0;
    00682         st->speclist[0].localidx = 0;
    00683       }
    00684     }
    00685
    00686     st->speclist[i].spec     = statspec;
    00687     st->speclist[i].grfid    = statspec->grf_prop.grffile->grfid;
    00688     st->speclist[i].localidx = statspec->grf_prop.local_id;
    00689   }
    00690
    00691   return i;
    00692 }
    00693
    00694
    00701 void DeallocateSpecFromStation(BaseStation *st, byte specindex)
    00702 {
    00703   /* specindex of 0 (default) is never freeable */
    00704   if (specindex == 0) return;
    00705
    00706   ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE);
    00707   /* Check all tiles over the station to check if the specindex is still in use */
    00708   TILE_AREA_LOOP(tile, area) {
    00709     if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
    00710       return;
    00711     }
    00712   }
    00713
    00714   /* This specindex is no longer in use, so deallocate it */
    00715   st->speclist[specindex].spec     = NULL;
    00716   st->speclist[specindex].grfid    = 0;
    00717   st->speclist[specindex].localidx = 0;
    00718
    00719   /* If this was the highest spec index, reallocate */
    00720   if (specindex == st->num_specs - 1) {
    00721     for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--) {}
    00722
    00723     if (st->num_specs > 1) {
    00724       st->speclist = ReallocT(st->speclist, st->num_specs);
    00725     } else {
    00726       free(st->speclist);
    00727       st->num_specs = 0;
    00728       st->speclist  = NULL;
    00729       st->cached_anim_triggers = 0;
    00730       return;
    00731     }
    00732   }
    00733
    00734   StationUpdateAnimTriggers(st);
    00735 }
    00736
    00747 bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
    00748 {
    00749   const StationSpec *statspec;
    00750   const DrawTileSprites *sprites = NULL;
    00751   const RailtypeInfo *rti = GetRailTypeInfo(railtype);
    00752   PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
    00753   uint tile = 2;
    00754
    00755   statspec = StationClass::Get(sclass, station);
    00756   if (statspec == NULL) return false;
    00757
    00758   if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
    00759     uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
    00760     if (callback != CALLBACK_FAILED) tile = callback;
    00761   }
    00762
    00763   uint32 total_offset = rti->GetRailtypeSpriteOffset();
    00764   uint32 relocation = 0;
    00765   uint32 ground_relocation = 0;
    00766   const NewGRFSpriteLayout *layout = NULL;
    00767   DrawTileSprites tmp_rail_layout;
    00768
    00769   if (statspec->renderdata == NULL) {
    00770     sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
    00771   } else {
    00772     layout = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
    00773     if (!layout->NeedsPreprocessing()) {
    00774       sprites = layout;
    00775       layout = NULL;
    00776     }
    00777   }
    00778
    00779   if (layout != NULL) {
    00780     /* Sprite layout which needs preprocessing */
    00781     bool separate_ground = HasBit(statspec->flags, SSF_SEPARATE_GROUND);
    00782     uint32 var10_values = layout->PrepareLayout(total_offset, rti->fallback_railtype, 0, separate_ground);
    00783     uint8 var10;
    00784     FOR_EACH_SET_BIT(var10, var10_values) {
    00785       uint32 var10_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, var10);
    00786       layout->ProcessRegisters(var10, var10_relocation, separate_ground);
    00787     }
    00788
    00789     tmp_rail_layout.seq = layout->GetLayout(&tmp_rail_layout.ground);
    00790     sprites = &tmp_rail_layout;
    00791     total_offset = 0;
    00792   } else {
    00793     /* Simple sprite layout */
    00794     ground_relocation = relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 0);
    00795     if (HasBit(sprites->ground.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
    00796       ground_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 1);
    00797     }
    00798     ground_relocation += rti->fallback_railtype;
    00799   }
    00800
    00801   SpriteID image = sprites->ground.sprite;
    00802   PaletteID pal = sprites->ground.pal;
    00803   image += HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? ground_relocation : total_offset;
    00804   if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += ground_relocation;
    00805
    00806   DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
    00807
    00808   DrawRailTileSeqInGUI(x, y, sprites, total_offset, relocation, palette);
    00809
    00810   return true;
    00811 }
    00812
    00813
    00814 const StationSpec *GetStationSpec(TileIndex t)
    00815 {
    00816   if (!IsCustomStationSpecIndex(t)) return NULL;
    00817
    00818   const BaseStation *st = BaseStation::GetByTile(t);
    00819   uint specindex = GetCustomStationSpecIndex(t);
    00820   return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
    00821 }
    00822
    00823
    00830 bool IsStationTileBlocked(TileIndex tile)
    00831 {
    00832   const StationSpec *statspec = GetStationSpec(tile);
    00833
    00834   return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile));
    00835 }
    00836
    00843 bool IsStationTileElectrifiable(TileIndex tile)
    00844 {
    00845   const StationSpec *statspec = GetStationSpec(tile);
    00846
    00847   return statspec == NULL ||
    00848       HasBit(statspec->pylons, GetStationGfx(tile)) ||
    00849       !HasBit(statspec->wires, GetStationGfx(tile));
    00850 }
    00851
    00853 struct StationAnimationBase : public AnimationBase<StationAnimationBase, StationSpec, BaseStation, GetStationCallback> {
    00854   static const CallbackID cb_animation_speed      = CBID_STATION_ANIMATION_SPEED;
    00855   static const CallbackID cb_animation_next_frame = CBID_STATION_ANIM_NEXT_FRAME;
    00856
    00857   static const StationCallbackMask cbm_animation_speed      = CBM_STATION_ANIMATION_SPEED;
    00858   static const StationCallbackMask cbm_animation_next_frame = CBM_STATION_ANIMATION_NEXT_FRAME;
    00859 };
    00860
    00861 void AnimateStationTile(TileIndex tile)
    00862 {
    00863   const StationSpec *ss = GetStationSpec(tile);
    00864   if (ss == NULL) return;
    00865
    00866   StationAnimationBase::AnimateTile(ss, BaseStation::GetByTile(tile), tile, HasBit(ss->flags, SSF_CB141_RANDOM_BITS));
    00867 }
    00868
    00869 void TriggerStationAnimation(const BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type)
    00870 {
    00871   /* List of coverage areas for each animation trigger */
    00872   static const TriggerArea tas[] = {
    00873     TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
    00874   };
    00875
    00876   /* Get Station if it wasn't supplied */
    00877   if (st == NULL) st = BaseStation::GetByTile(tile);
    00878
    00879   /* Check the cached animation trigger bitmask to see if we need
    00880    * to bother with any further processing. */
    00881   if (!HasBit(st->cached_anim_triggers, trigger)) return;
    00882
    00883   uint16 random_bits = Random();
    00884   ETileArea area = ETileArea(st, tile, tas[trigger]);
    00885
    00886   /* Check all tiles over the station to check if the specindex is still in use */
    00887   TILE_AREA_LOOP(tile, area) {
    00888     if (st->TileBelongsToRailStation(tile)) {
    00889       const StationSpec *ss = GetStationSpec(tile);
    00890       if (ss != NULL && HasBit(ss->animation.triggers, trigger)) {
    00891         CargoID cargo;
    00892         if (cargo_type == CT_INVALID) {
    00893           cargo = CT_INVALID;
    00894         } else {
    00895           cargo = GetReverseCargoTranslation(cargo_type, ss->grf_prop.grffile);
    00896         }
    00897         StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, tile, (random_bits << 16) | Random(), (uint8)trigger | (cargo << 8));
    00898       }
    00899     }
    00900   }
    00901 }
    00902
    00907 void StationUpdateAnimTriggers(BaseStation *st)
    00908 {
    00909   st->cached_anim_triggers = 0;
    00910
    00911   /* Combine animation trigger bitmask for all station specs
    00912    * of this station. */
    00913   for (uint i = 0; i < st->num_specs; i++) {
    00914     const StationSpec *ss = st->speclist[i].spec;
    00915     if (ss != NULL) st->cached_anim_triggers |= ss->animation.triggers;
    00916   }
    00917 }
    00918
    00924 void GetStationResolver(ResolverObject *ro, uint index)
    00925 {
    00926   NewStationResolver(ro, GetStationSpec(index), Station::GetByTile(index), index);
    00927 }