# 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_animation_base.h"
    00027 #include "newgrf_class_func.h"
    00028
    00029
    00030 template <typename Tspec, typename Tid, Tid Tmax>
    00031 /* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
    00032 {
    00033   /* Set up initial data */
    00034   classes[0].global_id = 'DFLT';
    00035   classes[0].name = STR_STATION_CLASS_DFLT;
    00036   classes[0].count = 1;
    00037   classes[0].spec = MallocT<StationSpec*>(1);
    00038   classes[0].spec[0] = NULL;
    00039
    00040   classes[1].global_id = 'WAYP';
    00041   classes[1].name = STR_STATION_CLASS_WAYP;
    00042   classes[1].count = 1;
    00043   classes[1].spec = MallocT<StationSpec*>(1);
    00044   classes[1].spec[0] = NULL;
    00045 }
    00046
    00047 INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX)
    00048
    00049 static const uint MAX_SPECLIST = 255;
    00050
    00051 enum TriggerArea {
    00052   TA_TILE,
    00053   TA_PLATFORM,
    00054   TA_WHOLE,
    00055 };
    00056
    00057 struct ETileArea : TileArea {
    00058   ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta)
    00059   {
    00060     switch (ta) {
    00061       default: NOT_REACHED();
    00062
    00063       case TA_TILE:
    00064         this->tile = tile;
    00065         this->w    = 1;
    00066         this->h    = 1;
    00067         break;
    00068
    00069       case TA_PLATFORM: {
    00070         TileIndex start, end;
    00071         Axis axis = GetRailStationAxis(tile);
    00072         TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis));
    00073
    00074         for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(tile, end + delta); end += delta) { /* Nothing */ }
    00075         for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(tile, start - delta); start -= delta) { /* Nothing */ }
    00076
    00077         this->tile = start;
    00078         this->w = TileX(end) - TileX(start) + 1;
    00079         this->h = TileY(end) - TileY(start) + 1;
    00080         break;
    00081       }
    00082
    00083       case TA_WHOLE:
    00084         st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT);
    00085         break;
    00086     }
    00087   }
    00088 };
    00089
    00090
    00103 uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred)
    00104 {
    00105   uint32 retval = 0;
    00106
    00107   if (axis == AXIS_X) {
    00108     Swap(platforms, length);
    00109     Swap(x, y);
    00110   }
    00111
    00112   if (centred) {
    00113     x -= platforms / 2;
    00114     y -= length / 2;
    00115     x = Clamp(x, -8, 7);
    00116     y = Clamp(y, -8, 7);
    00117     SB(retval,  0, 4, y & 0xF);
    00118     SB(retval,  4, 4, x & 0xF);
    00119   } else {
    00120     SB(retval,  0, 4, min(15, y));
    00121     SB(retval,  4, 4, min(15, length - y - 1));
    00122     SB(retval,  8, 4, min(15, x));
    00123     SB(retval, 12, 4, min(15, platforms - x - 1));
    00124   }
    00125   SB(retval, 16, 4, min(15, length));
    00126   SB(retval, 20, 4, min(15, platforms));
    00127   SB(retval, 24, 4, tile);
    00128
    00129   return retval;
    00130 }
    00131
    00132
    00141 static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis)
    00142 {
    00143   byte orig_type = 0;
    00144   Axis orig_axis = AXIS_X;
    00145   StationID sid = GetStationIndex(tile);
    00146
    00147   if (check_type) orig_type = GetCustomStationSpecIndex(tile);
    00148   if (check_axis) orig_axis = GetRailStationAxis(tile);
    00149
    00150   for (;;) {
    00151     TileIndex new_tile = TILE_ADD(tile, delta);
    00152
    00153     if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break;
    00154     if (!HasStationRail(new_tile)) break;
    00155     if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break;
    00156     if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break;
    00157
    00158     tile = new_tile;
    00159   }
    00160   return tile;
    00161 }
    00162
    00163
    00164 static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred)
    00165 {
    00166   int tx = TileX(tile);
    00167   int ty = TileY(tile);
    00168   int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1,  0), check_type, check_axis));
    00169   int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis));
    00170   int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1,  0), check_type, check_axis)) + 1;
    00171   int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0,  1), check_type, check_axis)) + 1;
    00172
    00173   tx -= sx; ex -= sx;
    00174   ty -= sy; ey -= sy;
    00175
    00176   return GetPlatformInfo(GetRailStationAxis(tile), GetStationGfx(tile), ex, ey, tx, ty, centred);
    00177 }
    00178
    00179
    00180 static uint32 GetRailContinuationInfo(TileIndex tile)
    00181 {
    00182   /* Tile offsets and exit dirs for X axis */
    00183   static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N };
    00184   static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE };
    00185
    00186   /* Tile offsets and exit dirs for Y axis */
    00187   static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N };
    00188   static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW };
    00189
    00190   Axis axis = GetRailStationAxis(tile);
    00191
    00192   /* Choose appropriate lookup table to use */
    00193   const Direction *dir = axis == AXIS_X ? x_dir : y_dir;
    00194   const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits;
    00195
    00196   uint32 res = 0;
    00197   uint i;
    00198
    00199   for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) {
    00200     TileIndex neighbour_tile = tile + TileOffsByDir(*dir);
    00201     TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(neighbour_tile, TRANSPORT_RAIL, 0));
    00202     if (trackbits != TRACK_BIT_NONE) {
    00203       /* If there is any track on the tile, set the bit in the second byte */
    00204       SetBit(res, i + 8);
    00205
    00206       /* With tunnels and bridges the tile has tracks, but they are not necessarily connected
    00207        * with the next tile because the ramp is not going in the right direction. */
    00208       if (IsTileType(neighbour_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(neighbour_tile) != *diagdir) {
    00209         continue;
    00210       }
    00211
    00212       /* If any track reaches our exit direction, set the bit in the lower byte */
    00213       if (trackbits & DiagdirReachesTracks(*diagdir)) SetBit(res, i);
    00214     }
    00215   }
    00216
    00217   return res;
    00218 }
    00219
    00220
    00221 /* Station Resolver Functions */
    00222 static uint32 StationGetRandomBits(const ResolverObject *object)
    00223 {
    00224   const BaseStation *st = object->u.station.st;
    00225   const TileIndex tile = object->u.station.tile;
    00226   return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
    00227 }
    00228
    00229
    00230 static uint32 StationGetTriggers(const ResolverObject *object)
    00231 {
    00232   const BaseStation *st = object->u.station.st;
    00233   return st == NULL ? 0 : st->waiting_triggers;
    00234 }
    00235
    00236
    00237 static void StationSetTriggers(const ResolverObject *object, int triggers)
    00238 {
    00239   BaseStation *st = const_cast<BaseStation *>(object->u.station.st);
    00240   assert(st != NULL);
    00241   st->waiting_triggers = triggers;
    00242 }
    00243
    00249 static struct {
    00250   uint32 v40;
    00251   uint32 v41;
    00252   uint32 v45;
    00253   uint32 v46;
    00254   uint32 v47;
    00255   uint32 v49;
    00256   uint8 valid;
    00257 } _svc;
    00258
    00259 static uint32 StationGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
    00260 {
    00261   const BaseStation *st = object->u.station.st;
    00262   TileIndex tile = object->u.station.tile;
    00263
    00264   if (object->scope == VSG_SCOPE_PARENT) {
    00265     /* Pass the request on to the town of the station */
    00266     Town *t;
    00267
    00268     if (st != NULL) {
    00269       t = st->town;
    00270     } else if (tile != INVALID_TILE) {
    00271       t = ClosestTownFromTile(tile, UINT_MAX);
    00272     } else {
    00273       *available = false;
    00274       return UINT_MAX;
    00275     }
    00276
    00277     return TownGetVariable(variable, parameter, available, t, object->grffile);
    00278   }
    00279
    00280   if (st == NULL) {
    00281     /* Station does not exist, so we're in a purchase list or the land slope check callback. */
    00282     switch (variable) {
    00283       case 0x40:
    00284       case 0x41:
    00285       case 0x46:
    00286       case 0x47:
    00287       case 0x49: return 0x2110000;        // Platforms, tracks & position
    00288       case 0x42: return 0;                // Rail type (XXX Get current type from GUI?)
    00289       case 0x43: return GetCompanyInfo(_current_company); // Station owner
    00290       case 0x44: return 2;                // PBS status
    00291       case 0x67: // Land info of nearby tile
    00292         if (object->u.station.axis != INVALID_AXIS && tile != INVALID_TILE) {
    00293           if (parameter != 0) tile = GetNearbyTile(parameter, tile, true, object->u.station.axis); // only perform if it is required
    00294
    00295           Slope tileh = GetTileSlope(tile);
    00296           bool swap = (object->u.station.axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
    00297
    00298           return GetNearbyTileInformation(tile, object->grffile->grf_version >= 8) ^ (swap ? SLOPE_EW : 0);
    00299         }
    00300         break;
    00301
    00302       case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value
    00303     }
    00304
    00305     *available = false;
    00306     return UINT_MAX;
    00307   }
    00308
    00309   switch (variable) {
    00310     /* Calculated station variables */
    00311     case 0x40:
    00312       if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); }
    00313       return _svc.v40;
    00314
    00315     case 0x41:
    00316       if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true,  false, false); SetBit(_svc.valid, 1); }
    00317       return _svc.v41;
    00318
    00319     case 0x42: return GetTerrainType(tile) | (GetReverseRailTypeTranslation(GetRailType(tile), object->u.station.statspec->grf_prop.grffile) << 8);
    00320     case 0x43: return GetCompanyInfo(st->owner); // Station owner
    00321     case 0x44: return HasStationReservation(tile) ? 7 : 4; // PBS status
    00322     case 0x45:
    00323       if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); }
    00324       return _svc.v45;
    00325
    00326     case 0x46:
    00327       if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); }
    00328       return _svc.v46;
    00329
    00330     case 0x47:
    00331       if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true,  false, true); SetBit(_svc.valid, 4); }
    00332       return _svc.v47;
    00333
    00334     case 0x49:
    00335       if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(tile, false, true, false); SetBit(_svc.valid, 5); }
    00336       return _svc.v49;
    00337
    00338     case 0x4A: // Animation frame of tile
    00339       return GetAnimationFrame(tile);
    00340
    00341     /* Variables which use the parameter */
    00342     /* Variables 0x60 to 0x65 and 0x69 are handled separately below */
    00343     case 0x66: // Animation frame of nearby tile
    00344       if (parameter != 0) tile = GetNearbyTile(parameter, tile);
    00345       return st->TileBelongsToRailStation(tile) ? GetAnimationFrame(tile) : UINT_MAX;
    00346
    00347     case 0x67: { // Land info of nearby tile
    00348       Axis axis = GetRailStationAxis(tile);
    00349
    00350       if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
    00351
    00352       Slope tileh = GetTileSlope(tile);
    00353       bool swap = (axis == AXIS_Y && HasBit(tileh, CORNER_W) != HasBit(tileh, CORNER_E));
    00354
    00355       return GetNearbyTileInformation(tile, object->grffile->grf_version >= 8) ^ (swap ? SLOPE_EW : 0);
    00356     }
    00357
    00358     case 0x68: { // Station info of nearby tiles
    00359       TileIndex nearby_tile = GetNearbyTile(parameter, tile);
    00360
    00361       if (!HasStationTileRail(nearby_tile)) return 0xFFFFFFFF;
    00362
    00363       uint32 grfid = st->speclist[GetCustomStationSpecIndex(tile)].grfid;
    00364       bool perpendicular = GetRailStationAxis(tile) != GetRailStationAxis(nearby_tile);
    00365       bool same_station = st->TileBelongsToRailStation(nearby_tile);
    00366       uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10;
    00367
    00368       if (IsCustomStationSpecIndex(nearby_tile)) {
    00369         const StationSpecList ssl = BaseStation::GetByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)];
    00370         res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx;
    00371       }
    00372       return res;
    00373     }
    00374
    00375     /* General station variables */
    00376     case 0x82: return 50;
    00377     case 0x84: return st->string_id;
    00378     case 0x86: return 0;
    00379     case 0xF0: return st->facilities;
    00380     case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
    00381   }
    00382
    00383   return st->GetNewGRFVariable(object, variable, parameter, available);
    00384 }
    00385
    00386 uint32 Station::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
    00387 {
    00388   switch (variable) {
    00389     case 0x48: { // Accepted cargo types
    00390       CargoID cargo_type;
    00391       uint32 value = 0;
    00392
    00393       for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
    00394         if (HasBit(this->goods[cargo_type].acceptance_pickup, GoodsEntry::GES_PICKUP)) SetBit(value, cargo_type);
    00395       }
    00396       return value;
    00397     }
    00398
    00399     case 0x8A: return this->had_vehicle_of_type;
    00400     case 0xF1: return (this->airport.tile != INVALID_TILE) ? this->airport.GetSpec()->ttd_airport_type : ATP_TTDP_LARGE;
    00401     case 0xF2: return (this->truck_stops != NULL) ? this->truck_stops->status : 0;
    00402     case 0xF3: return (this->bus_stops != NULL)   ? this->bus_stops->status   : 0;
    00403     case 0xF6: return this->airport.flags;
    00404     case 0xF7: return GB(this->airport.flags, 8, 8);
    00405   }
    00406
    00407   /* Handle cargo variables with parameter, 0x60 to 0x65 and 0x69 */
    00408   if ((variable >= 0x60 && variable <= 0x65) || variable == 0x69) {
    00409     CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grf_prop.grffile);
    00410
    00411     if (c == CT_INVALID) return 0;
    00412     const GoodsEntry *ge = &this->goods[c];
    00413
    00414     switch (variable) {
    00415       case 0x60: return min(ge->cargo.Count(), 4095);
    00416       case 0x61: return ge->days_since_pickup;
    00417       case 0x62: return ge->rating;
    00418       case 0x63: return ge->cargo.DaysInTransit();
    00419       case 0x64: return ge->last_speed | (ge->last_age << 8);
    00420       case 0x65: return GB(ge->acceptance_pickup, GoodsEntry::GES_ACCEPTANCE, 1) << 3;
    00421       case 0x69: return GB(ge->acceptance_pickup, GoodsEntry::GES_EVER_ACCEPTED, 4);
    00422     }
    00423   }
    00424
    00425   /* Handle cargo variables (deprecated) */
    00426   if (variable >= 0x8C && variable <= 0xEC) {
    00427     const GoodsEntry *g = &this->goods[GB(variable - 0x8C, 3, 4)];
    00428     switch (GB(variable - 0x8C, 0, 3)) {
    00429       case 0: return g->cargo.Count();
    00430       case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (GB(g->acceptance_pickup, GoodsEntry::GES_ACCEPTANCE, 1) << 7);
    00431       case 2: return g->days_since_pickup;
    00432       case 3: return g->rating;
    00433       case 4: return g->cargo.Source();
    00434       case 5: return g->cargo.DaysInTransit();
    00435       case 6: return g->last_speed;
    00436       case 7: return g->last_age;
    00437     }
    00438   }
    00439
    00440   DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
    00441
    00442   *available = false;
    00443   return UINT_MAX;
    00444 }
    00445
    00446 uint32 Waypoint::GetNewGRFVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) const
    00447 {
    00448   switch (variable) {
    00449     case 0x48: return 0; // Accepted cargo types
    00450     case 0x8A: return HVOT_WAYPOINT;
    00451     case 0xF1: return 0; // airport type
    00452     case 0xF2: return 0; // truck stop status
    00453     case 0xF3: return 0; // bus stop status
    00454     case 0xF6: return 0; // airport flags
    00455     case 0xF7: return 0; // airport flags cont.
    00456   }
    00457
    00458   /* Handle cargo variables with parameter, 0x60 to 0x65 */
    00459   if (variable >= 0x60 && variable <= 0x65) {
    00460     return 0;
    00461   }
    00462
    00463   /* Handle cargo variables (deprecated) */
    00464   if (variable >= 0x8C && variable <= 0xEC) {
    00465     switch (GB(variable - 0x8C, 0, 3)) {
    00466       case 3: return INITIAL_STATION_RATING;
    00467       case 4: return INVALID_STATION;
    00468       default: return 0;
    00469     }
    00470   }
    00471
    00472   DEBUG(grf, 1, "Unhandled station variable 0x%X", variable);
    00473
    00474   *available = false;
    00475   return UINT_MAX;
    00476 }
    00477
    00478 static const SpriteGroup *StationResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00479 {
    00480   const BaseStation *bst = object->u.station.st;
    00481   const StationSpec *statspec = object->u.station.statspec;
    00482   uint set;
    00483
    00484   uint cargo = 0;
    00485   CargoID cargo_type = object->u.station.cargo_type;
    00486
    00487   if (bst == NULL || statspec->cls_id == STAT_CLASS_WAYP) {
    00488     return group->loading[0];
    00489   }
    00490
    00491   const Station *st = Station::From(bst);
    00492
    00493   switch (cargo_type) {
    00494     case CT_INVALID:
    00495     case CT_DEFAULT_NA:
    00496     case CT_PURCHASE:
    00497       cargo = 0;
    00498       break;
    00499
    00500     case CT_DEFAULT:
    00501       for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
    00502         cargo += st->goods[cargo_type].cargo.Count();
    00503       }
    00504       break;
    00505
    00506     default:
    00507       cargo = st->goods[cargo_type].cargo.Count();
    00508       break;
    00509   }
    00510
    00511   if (HasBit(statspec->flags, SSF_DIV_BY_STATION_SIZE)) cargo /= (st->train_station.w + st->train_station.h);
    00512   cargo = min(0xfff, cargo);
    00513
    00514   if (cargo > statspec->cargo_threshold) {
    00515     if (group->num_loading > 0) {
    00516       set = ((cargo - statspec->cargo_threshold) * group->num_loading) / (4096 - statspec->cargo_threshold);
    00517       return group->loading[set];
    00518     }
    00519   } else {
    00520     if (group->num_loaded > 0) {
    00521       set = (cargo * group->num_loaded) / (statspec->cargo_threshold + 1);
    00522       return group->loaded[set];
    00523     }
    00524   }
    00525
    00526   return group->loading[0];
    00527 }
    00528
    00529
    00536 void StationStorePSA(ResolverObject *object, uint pos, int32 value)
    00537 {
    00538   /* Stations have no persistent storage. */
    00539   BaseStation *st = object->u.station.st;
    00540   if (object->scope != VSG_SCOPE_PARENT || st == NULL) return;
    00541
    00542   TownStorePSA(st->town, object->grffile, pos, value);
    00543 }
    00544
    00545 static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, BaseStation *st, TileIndex tile)
    00546 {
    00547   res->GetRandomBits = StationGetRandomBits;
    00548   res->GetTriggers   = StationGetTriggers;
    00549   res->SetTriggers   = StationSetTriggers;
    00550   res->GetVariable   = StationGetVariable;
    00551   res->ResolveReal   = StationResolveReal;
    00552   res->StorePSA      = StationStorePSA;
    00553
    00554   res->u.station.st       = st;
    00555   res->u.station.statspec = statspec;
    00556   res->u.station.tile     = tile;
    00557   res->u.station.axis     = INVALID_AXIS;
    00558
    00559   res->callback        = CBID_NO_CALLBACK;
    00560   res->callback_param1 = 0;
    00561   res->callback_param2 = 0;
    00562   res->ResetState();
    00563
    00564   res->grffile         = (statspec != NULL ? statspec->grf_prop.grffile : NULL);
    00565
    00566   /* Invalidate all cached vars */
    00567   _svc.valid = 0;
    00568 }
    00569
    00570 static const SpriteGroup *ResolveStation(ResolverObject *object)
    00571 {
    00572   const SpriteGroup *group;
    00573   CargoID ctype = CT_DEFAULT_NA;
    00574
    00575   if (object->u.station.st == NULL) {
    00576     /* No station, so we are in a purchase list */
    00577     ctype = CT_PURCHASE;
    00578   } else if (Station::IsExpected(object->u.station.st)) {
    00579     const Station *st = Station::From(object->u.station.st);
    00580     /* Pick the first cargo that we have waiting */
    00581     const CargoSpec *cs;
    00582     FOR_ALL_CARGOSPECS(cs) {
    00583       if (object->u.station.statspec->grf_prop.spritegroup[cs->Index()] != NULL &&
    00584           !st->goods[cs->Index()].cargo.Empty()) {
    00585         ctype = cs->Index();
    00586         break;
    00587       }
    00588     }
    00589   }
    00590
    00591   group = object->u.station.statspec->grf_prop.spritegroup[ctype];
    00592   if (group == NULL) {
    00593     ctype = CT_DEFAULT;
    00594     group = object->u.station.statspec->grf_prop.spritegroup[ctype];
    00595   }
    00596
    00597   if (group == NULL) return NULL;
    00598
    00599   /* Remember the cargo type we've picked */
    00600   object->u.station.cargo_type = ctype;
    00601
    00602   return SpriteGroup::Resolve(group, object);
    00603 }
    00604
    00613 SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32 var10)
    00614 {
    00615   const SpriteGroup *group;
    00616   ResolverObject object;
    00617
    00618   NewStationResolver(&object, statspec, st, tile);
    00619   object.callback_param1 = var10;
    00620
    00621   group = ResolveStation(&object);
    00622   if (group == NULL || group->type != SGT_RESULT) return 0;
    00623   return group->GetResult() - 0x42D;
    00624 }
    00625
    00635 SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info)
    00636 {
    00637   const SpriteGroup *group;
    00638   ResolverObject object;
    00639
    00640   NewStationResolver(&object, statspec, st, tile);
    00641   object.callback_param1 = 2; // Indicate we are resolving the foundation sprites
    00642   object.callback_param2 = layout | (edge_info << 16);
    00643
    00644   ClearRegister(0x100);
    00645   group = ResolveStation(&object);
    00646   if (group == NULL || group->type != SGT_RESULT) return 0;
    00647   return group->GetResult() + GetRegister(0x100);
    00648 }
    00649
    00650
    00651 uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, BaseStation *st, TileIndex tile)
    00652 {
    00653   const SpriteGroup *group;
    00654   ResolverObject object;
    00655
    00656   NewStationResolver(&object, statspec, st, tile);
    00657
    00658   object.callback        = callback;
    00659   object.callback_param1 = param1;
    00660   object.callback_param2 = param2;
    00661
    00662   group = ResolveStation(&object);
    00663   if (group == NULL) return CALLBACK_FAILED;
    00664   return group->GetCallbackResult();
    00665 }
    00666
    00677 CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, byte plat_len, byte numtracks)
    00678 {
    00679   TileIndexDiff diff = cur_tile - north_tile;
    00680   Slope slope = GetTileSlope(cur_tile);
    00681
    00682   ResolverObject object;
    00683   NewStationResolver(&object, statspec, NULL, cur_tile);
    00684
    00685   object.callback        = CBID_STATION_LAND_SLOPE_CHECK;
    00686   object.callback_param1 = slope << 4 | (slope ^ (axis == AXIS_Y && HasBit(slope, CORNER_W) != HasBit(slope, CORNER_E) ? SLOPE_EW : 0));
    00687   object.callback_param2 = numtracks << 24 | plat_len << 16 | (axis == AXIS_Y ? TileX(diff) << 8 | TileY(diff) : TileY(diff) << 8 | TileX(diff));
    00688   object.u.station.axis  = axis;
    00689
    00690   const SpriteGroup *group = ResolveStation(&object);
    00691   uint16 cb_res = group != NULL ? group->GetCallbackResult() : CALLBACK_FAILED;
    00692
    00693   /* Failed callback means success. */
    00694   if (cb_res == CALLBACK_FAILED) return CommandCost();
    00695
    00696   /* The meaning of bit 10 is inverted for a grf version < 8. */
    00697   if (statspec->grf_prop.grffile->grf_version < 8) ToggleBit(cb_res, 10);
    00698   return GetErrorMessageFromLocationCallbackResult(cb_res, statspec->grf_prop.grffile->grfid, STR_ERROR_LAND_SLOPED_IN_WRONG_DIRECTION);
    00699 }
    00700
    00701
    00709 int AllocateSpecToStation(const StationSpec *statspec, BaseStation *st, bool exec)
    00710 {
    00711   uint i;
    00712
    00713   if (statspec == NULL || st == NULL) return 0;
    00714
    00715   for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
    00716     if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
    00717   }
    00718
    00719   if (i == MAX_SPECLIST) {
    00720     /* As final effort when the spec list is already full...
    00721      * try to find the same spec and return that one. This might
    00722      * result in slighty "wrong" (as per specs) looking stations,
    00723      * but it's fairly unlikely that one reaches the limit anyways.
    00724      */
    00725     for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) {
    00726       if (st->speclist[i].spec == statspec) return i;
    00727     }
    00728
    00729     return -1;
    00730   }
    00731
    00732   if (exec) {
    00733     if (i >= st->num_specs) {
    00734       st->num_specs = i + 1;
    00735       st->speclist = ReallocT(st->speclist, st->num_specs);
    00736
    00737       if (st->num_specs == 2) {
    00738         /* Initial allocation */
    00739         st->speclist[0].spec     = NULL;
    00740         st->speclist[0].grfid    = 0;
    00741         st->speclist[0].localidx = 0;
    00742       }
    00743     }
    00744
    00745     st->speclist[i].spec     = statspec;
    00746     st->speclist[i].grfid    = statspec->grf_prop.grffile->grfid;
    00747     st->speclist[i].localidx = statspec->grf_prop.local_id;
    00748   }
    00749
    00750   return i;
    00751 }
    00752
    00753
    00760 void DeallocateSpecFromStation(BaseStation *st, byte specindex)
    00761 {
    00762   /* specindex of 0 (default) is never freeable */
    00763   if (specindex == 0) return;
    00764
    00765   ETileArea area = ETileArea(st, INVALID_TILE, TA_WHOLE);
    00766   /* Check all tiles over the station to check if the specindex is still in use */
    00767   TILE_AREA_LOOP(tile, area) {
    00768     if (st->TileBelongsToRailStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
    00769       return;
    00770     }
    00771   }
    00772
    00773   /* This specindex is no longer in use, so deallocate it */
    00774   st->speclist[specindex].spec     = NULL;
    00775   st->speclist[specindex].grfid    = 0;
    00776   st->speclist[specindex].localidx = 0;
    00777
    00778   /* If this was the highest spec index, reallocate */
    00779   if (specindex == st->num_specs - 1) {
    00780     for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--) {}
    00781
    00782     if (st->num_specs > 1) {
    00783       st->speclist = ReallocT(st->speclist, st->num_specs);
    00784     } else {
    00785       free(st->speclist);
    00786       st->num_specs = 0;
    00787       st->speclist  = NULL;
    00788       st->cached_anim_triggers = 0;
    00789       return;
    00790     }
    00791   }
    00792
    00793   StationUpdateAnimTriggers(st);
    00794 }
    00795
    00806 bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station)
    00807 {
    00808   const StationSpec *statspec;
    00809   const DrawTileSprites *sprites = NULL;
    00810   const RailtypeInfo *rti = GetRailTypeInfo(railtype);
    00811   PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
    00812   uint tile = 2;
    00813
    00814   statspec = StationClass::Get(sclass, station);
    00815   if (statspec == NULL) return false;
    00816
    00817   if (HasBit(statspec->callback_mask, CBM_STATION_SPRITE_LAYOUT)) {
    00818     uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE);
    00819     if (callback != CALLBACK_FAILED) tile = callback;
    00820   }
    00821
    00822   uint32 total_offset = rti->GetRailtypeSpriteOffset();
    00823   uint32 relocation = 0;
    00824   uint32 ground_relocation = 0;
    00825   const NewGRFSpriteLayout *layout = NULL;
    00826   DrawTileSprites tmp_rail_layout;
    00827
    00828   if (statspec->renderdata == NULL) {
    00829     sprites = GetStationTileLayout(STATION_RAIL, tile + axis);
    00830   } else {
    00831     layout = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis];
    00832     if (!layout->NeedsPreprocessing()) {
    00833       sprites = layout;
    00834       layout = NULL;
    00835     }
    00836   }
    00837
    00838   if (layout != NULL) {
    00839     /* Sprite layout which needs preprocessing */
    00840     bool separate_ground = HasBit(statspec->flags, SSF_SEPARATE_GROUND);
    00841     uint32 var10_values = layout->PrepareLayout(total_offset, rti->fallback_railtype, 0, 0, separate_ground);
    00842     uint8 var10;
    00843     FOR_EACH_SET_BIT(var10, var10_values) {
    00844       uint32 var10_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, var10);
    00845       layout->ProcessRegisters(var10, var10_relocation, separate_ground);
    00846     }
    00847
    00848     tmp_rail_layout.seq = layout->GetLayout(&tmp_rail_layout.ground);
    00849     sprites = &tmp_rail_layout;
    00850     total_offset = 0;
    00851   } else {
    00852     /* Simple sprite layout */
    00853     ground_relocation = relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 0);
    00854     if (HasBit(sprites->ground.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
    00855       ground_relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE, 1);
    00856     }
    00857     ground_relocation += rti->fallback_railtype;
    00858   }
    00859
    00860   SpriteID image = sprites->ground.sprite;
    00861   PaletteID pal = sprites->ground.pal;
    00862   image += HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? ground_relocation : total_offset;
    00863   if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += ground_relocation;
    00864
    00865   DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
    00866
    00867   DrawRailTileSeqInGUI(x, y, sprites, total_offset, relocation, palette);
    00868
    00869   return true;
    00870 }
    00871
    00872
    00873 const StationSpec *GetStationSpec(TileIndex t)
    00874 {
    00875   if (!IsCustomStationSpecIndex(t)) return NULL;
    00876
    00877   const BaseStation *st = BaseStation::GetByTile(t);
    00878   uint specindex = GetCustomStationSpecIndex(t);
    00879   return specindex < st->num_specs ? st->speclist[specindex].spec : NULL;
    00880 }
    00881
    00882
    00889 bool IsStationTileBlocked(TileIndex tile)
    00890 {
    00891   const StationSpec *statspec = GetStationSpec(tile);
    00892
    00893   return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile));
    00894 }
    00895
    00902 bool IsStationTileElectrifiable(TileIndex tile)
    00903 {
    00904   const StationSpec *statspec = GetStationSpec(tile);
    00905
    00906   return statspec == NULL ||
    00907       HasBit(statspec->pylons, GetStationGfx(tile)) ||
    00908       !HasBit(statspec->wires, GetStationGfx(tile));
    00909 }
    00910
    00912 uint16 GetAnimStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, BaseStation *st, TileIndex tile, int extra_data)
    00913 {
    00914   return GetStationCallback(callback, param1, param2, statspec, st, tile);
    00915 }
    00916
    00918 struct StationAnimationBase : public AnimationBase<StationAnimationBase, StationSpec, BaseStation, int, GetAnimStationCallback> {
    00919   static const CallbackID cb_animation_speed      = CBID_STATION_ANIMATION_SPEED;
    00920   static const CallbackID cb_animation_next_frame = CBID_STATION_ANIM_NEXT_FRAME;
    00921
    00922   static const StationCallbackMask cbm_animation_speed      = CBM_STATION_ANIMATION_SPEED;
    00923   static const StationCallbackMask cbm_animation_next_frame = CBM_STATION_ANIMATION_NEXT_FRAME;
    00924 };
    00925
    00926 void AnimateStationTile(TileIndex tile)
    00927 {
    00928   const StationSpec *ss = GetStationSpec(tile);
    00929   if (ss == NULL) return;
    00930
    00931   StationAnimationBase::AnimateTile(ss, BaseStation::GetByTile(tile), tile, HasBit(ss->flags, SSF_CB141_RANDOM_BITS));
    00932 }
    00933
    00934 void TriggerStationAnimation(BaseStation *st, TileIndex tile, StationAnimationTrigger trigger, CargoID cargo_type)
    00935 {
    00936   /* List of coverage areas for each animation trigger */
    00937   static const TriggerArea tas[] = {
    00938     TA_TILE, TA_WHOLE, TA_WHOLE, TA_PLATFORM, TA_PLATFORM, TA_PLATFORM, TA_WHOLE
    00939   };
    00940
    00941   /* Get Station if it wasn't supplied */
    00942   if (st == NULL) st = BaseStation::GetByTile(tile);
    00943
    00944   /* Check the cached animation trigger bitmask to see if we need
    00945    * to bother with any further processing. */
    00946   if (!HasBit(st->cached_anim_triggers, trigger)) return;
    00947
    00948   uint16 random_bits = Random();
    00949   ETileArea area = ETileArea(st, tile, tas[trigger]);
    00950
    00951   /* Check all tiles over the station to check if the specindex is still in use */
    00952   TILE_AREA_LOOP(tile, area) {
    00953     if (st->TileBelongsToRailStation(tile)) {
    00954       const StationSpec *ss = GetStationSpec(tile);
    00955       if (ss != NULL && HasBit(ss->animation.triggers, trigger)) {
    00956         CargoID cargo;
    00957         if (cargo_type == CT_INVALID) {
    00958           cargo = CT_INVALID;
    00959         } else {
    00960           cargo = GetReverseCargoTranslation(cargo_type, ss->grf_prop.grffile);
    00961         }
    00962         StationAnimationBase::ChangeAnimationFrame(CBID_STATION_ANIM_START_STOP, ss, st, tile, (random_bits << 16) | Random(), (uint8)trigger | (cargo << 8));
    00963       }
    00964     }
    00965   }
    00966 }
    00967
    00972 void StationUpdateAnimTriggers(BaseStation *st)
    00973 {
    00974   st->cached_anim_triggers = 0;
    00975
    00976   /* Combine animation trigger bitmask for all station specs
    00977    * of this station. */
    00978   for (uint i = 0; i < st->num_specs; i++) {
    00979     const StationSpec *ss = st->speclist[i].spec;
    00980     if (ss != NULL) st->cached_anim_triggers |= ss->animation.triggers;
    00981   }
    00982 }
    00983
    00989 void GetStationResolver(ResolverObject *ro, uint index)
    00990 {
    00991   NewStationResolver(ro, GetStationSpec(index), Station::GetByTile(index), index);
    00992 }