peter1138@2625: /* $Id$ */ peter1138@2625: belugas@6443: /** @file newgrf_station.cpp Functions for dealing with station classes and custom stations. */ peter1138@2625: peter1138@2625: #include "stdafx.h" peter1138@2625: #include "openttd.h" peter1138@3743: #include "variables.h" rubidium@8615: #include "tile_cmd.h" maedhros@6669: #include "landscape.h" peter1138@2625: #include "debug.h" peter1138@2625: #include "sprite.h" peter1138@3764: #include "table/sprites.h" peter1138@3594: #include "table/strings.h" peter1138@2967: #include "station.h" peter1138@3587: #include "station_map.h" Darkvater@5568: #include "newgrf.h" peter1138@3752: #include "newgrf_callbacks.h" maedhros@7345: #include "newgrf_commons.h" peter1138@2963: #include "newgrf_station.h" peter1138@3743: #include "newgrf_spritegroup.h" peter1138@6439: #include "cargotype.h" peter1138@6680: #include "town_map.h" peter1138@6680: #include "newgrf_town.h" rubidium@8619: #include "gfx_func.h" rubidium@8636: #include "date_func.h" rubidium@8750: #include "player_func.h" peter1138@2625: peter1138@2625: static StationClass station_classes[STAT_CLASS_MAX]; peter1138@2625: peter1138@3786: enum { peter1138@3786: MAX_SPECLIST = 255, peter1138@3786: }; peter1138@3786: peter1138@2625: /** peter1138@2625: * Reset station classes to their default state. peter1138@2625: * This includes initialising the Default and Waypoint classes with an empty peter1138@2625: * entry, for standard stations and waypoints. peter1138@2625: */ rubidium@6573: void ResetStationClasses() peter1138@2625: { rubidium@5838: for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) { peter1138@2625: station_classes[i].id = 0; peter1138@3594: station_classes[i].name = STR_EMPTY; peter1138@2625: station_classes[i].stations = 0; peter1138@2625: peter1138@2625: free(station_classes[i].spec); peter1138@2625: station_classes[i].spec = NULL; peter1138@2625: } peter1138@2625: belugas@6674: /* Set up initial data */ peter1138@2625: station_classes[0].id = 'DFLT'; peter1138@3594: station_classes[0].name = STR_STAT_CLASS_DFLT; peter1138@2625: station_classes[0].stations = 1; KUDr@5860: station_classes[0].spec = MallocT(1); peter1138@2625: station_classes[0].spec[0] = NULL; peter1138@2625: peter1138@2625: station_classes[1].id = 'WAYP'; peter1138@3594: station_classes[1].name = STR_STAT_CLASS_WAYP; peter1138@2625: station_classes[1].stations = 1; KUDr@5860: station_classes[1].spec = MallocT(1); peter1138@2625: station_classes[1].spec[0] = NULL; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Allocate a station class for the given class id. belugas@6977: * @param cls A 32 bit value identifying the class. peter1138@2625: * @return Index into station_classes of allocated class. peter1138@2625: */ rubidium@5838: StationClassID AllocateStationClass(uint32 cls) peter1138@2625: { rubidium@5838: for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) { rubidium@5838: if (station_classes[i].id == cls) { belugas@6674: /* ClassID is already allocated, so reuse it. */ peter1138@2625: return i; peter1138@2625: } else if (station_classes[i].id == 0) { belugas@6674: /* This class is empty, so allocate it to the ClassID. */ rubidium@5838: station_classes[i].id = cls; peter1138@2625: return i; peter1138@2625: } peter1138@2625: } peter1138@2625: Darkvater@5568: grfmsg(2, "StationClassAllocate: already allocated %d classes, using default", STAT_CLASS_MAX); peter1138@2625: return STAT_CLASS_DFLT; peter1138@2625: } peter1138@2625: peter1138@3642: /** Set the name of a custom station class */ peter1138@3642: void SetStationClassName(StationClassID sclass, StringID name) peter1138@3642: { peter1138@3642: assert(sclass < STAT_CLASS_MAX); peter1138@3642: station_classes[sclass].name = name; peter1138@3642: } peter1138@3642: peter1138@3642: /** Retrieve the name of a custom station class */ peter1138@3642: StringID GetStationClassName(StationClassID sclass) peter1138@3642: { peter1138@3642: assert(sclass < STAT_CLASS_MAX); peter1138@3642: return station_classes[sclass].name; peter1138@3642: } peter1138@3642: peter1138@3642: /** Build a list of station class name StringIDs to use in a dropdown list peter1138@3642: * @return Pointer to a (static) array of StringIDs peter1138@3642: */ rubidium@6573: StringID *BuildStationClassDropdown() peter1138@3642: { peter1138@3642: /* Allow room for all station classes, plus a terminator entry */ peter1138@3642: static StringID names[STAT_CLASS_MAX + 1]; peter1138@3642: uint i; peter1138@3642: peter1138@3642: /* Add each name */ peter1138@3642: for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++) { peter1138@3642: names[i] = station_classes[i].name; peter1138@3642: } peter1138@3642: /* Terminate the list */ peter1138@3642: names[i] = INVALID_STRING_ID; peter1138@3642: peter1138@3642: return names; peter1138@3642: } peter1138@3642: peter1138@2625: /** peter1138@3587: * Get the number of station classes in use. peter1138@3587: * @return Number of station classes. peter1138@3587: */ rubidium@6573: uint GetNumStationClasses() peter1138@3587: { peter1138@3587: uint i; peter1138@3587: for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++); peter1138@3587: return i; peter1138@3587: } peter1138@3587: peter1138@3587: /** peter1138@2625: * Return the number of stations for the given station class. peter1138@2625: * @param sclass Index of the station class. peter1138@2625: * @return Number of stations in the class. peter1138@2625: */ peter1138@2625: uint GetNumCustomStations(StationClassID sclass) peter1138@2625: { peter1138@2625: assert(sclass < STAT_CLASS_MAX); peter1138@2625: return station_classes[sclass].stations; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Tie a station spec to its station class. belugas@6977: * @param statspec The station spec. peter1138@2625: */ belugas@3676: void SetCustomStationSpec(StationSpec *statspec) peter1138@2625: { peter1138@2625: StationClass *station_class; peter1138@2625: int i; peter1138@2625: peter1138@3780: /* If the station has already been allocated, don't reallocate it. */ peter1138@3780: if (statspec->allocated) return; peter1138@3780: belugas@3676: assert(statspec->sclass < STAT_CLASS_MAX); belugas@3676: station_class = &station_classes[statspec->sclass]; peter1138@2625: peter1138@2625: i = station_class->stations++; KUDr@5860: station_class->spec = ReallocT(station_class->spec, station_class->stations); peter1138@2625: belugas@3676: station_class->spec[i] = statspec; peter1138@3780: statspec->allocated = true; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Retrieve a station spec from a class. peter1138@2625: * @param sclass Index of the station class. peter1138@2625: * @param station The station index with the class. peter1138@2625: * @return The station spec. peter1138@2625: */ belugas@3676: const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station) peter1138@2625: { peter1138@2625: assert(sclass < STAT_CLASS_MAX); peter1138@2625: if (station < station_classes[sclass].stations) peter1138@2625: return station_classes[sclass].spec[station]; peter1138@2625: belugas@6674: /* If the custom station isn't defined any more, then the GRF file belugas@6674: * probably was not loaded. */ peter1138@2625: return NULL; peter1138@2625: } peter1138@2967: peter1138@2967: peter1138@3765: const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx) peter1138@3765: { peter1138@3765: uint j; peter1138@3765: rubidium@5838: for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) { peter1138@3765: for (j = 0; j < station_classes[i].stations; j++) { peter1138@3765: const StationSpec *statspec = station_classes[i].spec[j]; peter1138@3765: if (statspec == NULL) continue; peter1138@6947: if (statspec->grffile->grfid == grfid && statspec->localidx == localidx) return statspec; peter1138@3765: } peter1138@3765: } peter1138@3765: peter1138@3765: return NULL; peter1138@3765: } peter1138@3765: peter1138@3765: peter1138@3756: /* Evaluate a tile's position within a station, and return the result a bitstuffed format. peter1138@3756: * if not centred: .TNLcCpP, if centred: .TNL..CP peter1138@3756: * T = Tile layout number (GetStationGfx), N = Number of platforms, L = Length of platforms peter1138@3756: * C = Current platform number from start, c = from end peter1138@3756: * P = Position along platform from start, p = from end peter1138@3756: * if centred, C/P start from the centre and c/p are not available. peter1138@3756: */ peter1138@3756: uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred) peter1138@3756: { peter1138@3756: uint32 retval = 0; peter1138@3756: peter1138@3756: if (axis == AXIS_X) { tron@6432: Swap(platforms, length); tron@6432: Swap(x, y); peter1138@3756: } peter1138@3756: peter1138@3756: /* Limit our sizes to 4 bits */ peter1138@3756: platforms = min(15, platforms); peter1138@3756: length = min(15, length); peter1138@3756: x = min(15, x); peter1138@3756: y = min(15, y); peter1138@3756: if (centred) { peter1138@3756: x -= platforms / 2; peter1138@3756: y -= length / 2; peter1138@3790: SB(retval, 0, 4, y & 0xF); peter1138@3790: SB(retval, 4, 4, x & 0xF); peter1138@3756: } else { peter1138@3756: SB(retval, 0, 4, y); peter1138@3756: SB(retval, 4, 4, length - y - 1); peter1138@3756: SB(retval, 8, 4, x); peter1138@3756: SB(retval, 12, 4, platforms - x - 1); peter1138@3756: } peter1138@3756: SB(retval, 16, 4, length); peter1138@3756: SB(retval, 20, 4, platforms); peter1138@3756: SB(retval, 24, 4, tile); peter1138@3756: peter1138@3756: return retval; peter1138@3756: } peter1138@3756: peter1138@3756: peter1138@3756: /* Find the end of a railway station, from the tile, in the direction of delta. peter1138@3756: * If check_type is set, we stop if the custom station type changes. peter1138@3756: * If check_axis is set, we stop if the station direction changes. peter1138@3756: */ peter1138@3756: static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis) peter1138@3756: { peter1138@3756: bool waypoint; peter1138@3756: byte orig_type = 0; peter1138@3756: Axis orig_axis = AXIS_X; peter1138@3756: peter1138@3756: waypoint = IsTileType(tile, MP_RAILWAY); peter1138@3756: peter1138@3756: if (waypoint) { peter1138@3756: if (check_axis) orig_axis = GetWaypointAxis(tile); peter1138@3756: } else { peter1138@3756: if (check_type) orig_type = GetCustomStationSpecIndex(tile); peter1138@3756: if (check_axis) orig_axis = GetRailStationAxis(tile); peter1138@3756: } peter1138@3756: peter1138@3756: while (true) { peter1138@3756: TileIndex new_tile = TILE_ADD(tile, delta); peter1138@3756: peter1138@3756: if (waypoint) { peter1138@3756: if (!IsTileType(new_tile, MP_RAILWAY)) break; tron@4182: if (!IsRailWaypoint(new_tile)) break; peter1138@3756: if (check_axis && GetWaypointAxis(new_tile) != orig_axis) break; peter1138@3756: } else { peter1138@3756: if (!IsRailwayStationTile(new_tile)) break; peter1138@3756: if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break; peter1138@3756: if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break; peter1138@3756: } peter1138@3756: peter1138@3756: tile = new_tile; peter1138@3756: } peter1138@3756: return tile; peter1138@3756: } peter1138@3756: peter1138@3756: peter1138@3756: static uint32 GetPlatformInfoHelper(TileIndex tile, bool check_type, bool check_axis, bool centred) peter1138@3756: { peter1138@3756: int tx = TileX(tile); peter1138@3756: int ty = TileY(tile); peter1138@3756: int sx = TileX(FindRailStationEnd(tile, TileDiffXY(-1, 0), check_type, check_axis)); peter1138@3756: int sy = TileY(FindRailStationEnd(tile, TileDiffXY( 0, -1), check_type, check_axis)); peter1138@3756: int ex = TileX(FindRailStationEnd(tile, TileDiffXY( 1, 0), check_type, check_axis)) + 1; peter1138@3756: int ey = TileY(FindRailStationEnd(tile, TileDiffXY( 0, 1), check_type, check_axis)) + 1; peter1138@3756: Axis axis = IsTileType(tile, MP_RAILWAY) ? GetWaypointAxis(tile) : GetRailStationAxis(tile); peter1138@3756: peter1138@3756: tx -= sx; ex -= sx; peter1138@3756: ty -= sy; ey -= sy; peter1138@3756: peter1138@3756: return GetPlatformInfo(axis, IsTileType(tile, MP_RAILWAY) ? 2 : GetStationGfx(tile), ex, ey, tx, ty, centred); peter1138@3756: } peter1138@3756: peter1138@3756: peter1138@5660: static uint32 GetRailContinuationInfo(TileIndex tile) peter1138@5660: { peter1138@5660: /* Tile offsets and exit dirs for X axis */ peter1138@7653: static const Direction x_dir[8] = { DIR_SW, DIR_NE, DIR_SE, DIR_NW, DIR_S, DIR_E, DIR_W, DIR_N }; peter1138@7653: static const DiagDirection x_exits[8] = { DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SW, DIAGDIR_NE }; peter1138@5660: peter1138@5660: /* Tile offsets and exit dirs for Y axis */ peter1138@7653: static const Direction y_dir[8] = { DIR_SE, DIR_NW, DIR_SW, DIR_NE, DIR_S, DIR_W, DIR_E, DIR_N }; peter1138@7653: static const DiagDirection y_exits[8] = { DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_NW }; peter1138@5660: peter1138@5660: Axis axis = IsTileType(tile, MP_RAILWAY) ? GetWaypointAxis(tile) : GetRailStationAxis(tile); peter1138@5660: peter1138@5660: /* Choose appropriate lookup table to use */ peter1138@7653: const Direction *dir = axis == AXIS_X ? x_dir : y_dir; peter1138@7653: const DiagDirection *diagdir = axis == AXIS_X ? x_exits : y_exits; peter1138@5660: peter1138@5660: uint32 res = 0; peter1138@5660: uint i; peter1138@5660: peter1138@5660: for (i = 0; i < lengthof(x_dir); i++, dir++, diagdir++) { rubidium@7179: uint32 ts = GetTileTrackStatus(tile + TileOffsByDir(*dir), TRANSPORT_RAIL, 0); peter1138@5660: if (ts != 0) { peter1138@5660: /* If there is any track on the tile, set the bit in the second byte */ skidd13@8427: SetBit(res, i + 8); peter1138@5660: peter1138@5660: /* If any track reaches our exit direction, set the bit in the lower byte */ skidd13@8427: if (ts & DiagdirReachesTracks(*diagdir)) SetBit(res, i); peter1138@5660: } peter1138@5660: } peter1138@5660: peter1138@5660: return res; peter1138@5660: } peter1138@5660: peter1138@5660: peter1138@3743: /* Station Resolver Functions */ peter1138@3743: static uint32 StationGetRandomBits(const ResolverObject *object) peter1138@3743: { peter1138@3743: const Station *st = object->u.station.st; peter1138@3743: const TileIndex tile = object->u.station.tile; peter1138@3743: return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16); peter1138@3743: } peter1138@2967: peter1138@3743: peter1138@3743: static uint32 StationGetTriggers(const ResolverObject *object) peter1138@3743: { peter1138@3743: const Station *st = object->u.station.st; peter1138@3743: return st == NULL ? 0 : st->waiting_triggers; peter1138@3743: } peter1138@3743: peter1138@3743: peter1138@3743: static void StationSetTriggers(const ResolverObject *object, int triggers) peter1138@3743: { peter1138@3743: Station *st = (Station*)object->u.station.st; peter1138@3743: assert(st != NULL); peter1138@3743: st->waiting_triggers = triggers; peter1138@3743: } peter1138@3743: peter1138@7726: /** peter1138@7726: * Station variable cache peter1138@7726: * This caches 'expensive' station variable lookups which iterate over peter1138@7726: * several tiles that may be called multiple times per Resolve(). peter1138@7726: */ peter1138@7726: static struct { peter1138@7726: uint32 v40; peter1138@7726: uint32 v41; peter1138@7726: uint32 v45; peter1138@7726: uint32 v46; peter1138@7726: uint32 v47; peter1138@7726: uint32 v49; peter1138@7726: uint8 valid; peter1138@7726: } _svc; peter1138@3743: peter1138@3893: static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available) peter1138@3743: { peter1138@3743: const Station *st = object->u.station.st; peter1138@3756: TileIndex tile = object->u.station.tile; peter1138@3743: peter1138@6680: if (object->scope == VSG_SCOPE_PARENT) { peter1138@6680: /* Pass the request on to the town of the station */ peter1138@6680: Town *t; peter1138@6680: peter1138@6680: if (st != NULL) { peter1138@6680: t = st->town; peter1138@6680: } else if (tile != INVALID_TILE) { peter1138@6680: t = GetTownByTile(tile); peter1138@6680: } else { peter1138@6680: *available = false; peter1138@6680: return UINT_MAX; peter1138@6680: } peter1138@6680: peter1138@6680: return TownGetVariable(variable, parameter, available, t); peter1138@6680: } peter1138@6680: peter1138@3743: if (st == NULL) { peter1138@3743: /* Station does not exist, so we're in a purchase list */ peter1138@3743: switch (variable) { peter1138@3743: case 0x40: peter1138@3743: case 0x41: peter1138@3743: case 0x46: peter1138@3743: case 0x47: belugas@6674: case 0x49: return 0x2110000; // Platforms, tracks & position belugas@6674: case 0x42: return 0; // Rail type (XXX Get current type from GUI?) belugas@6674: case 0x43: return _current_player; // Station owner belugas@6674: case 0x44: return 2; // PBS status skidd13@8418: case 0xFA: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value peter1138@3743: } peter1138@3893: peter1138@3893: *available = false; rubidium@5838: return UINT_MAX; peter1138@3743: } peter1138@3743: peter1138@3743: switch (variable) { peter1138@3743: /* Calculated station variables */ peter1138@7726: case 0x40: skidd13@8427: if (!HasBit(_svc.valid, 0)) { _svc.v40 = GetPlatformInfoHelper(tile, false, false, false); SetBit(_svc.valid, 0); } peter1138@7726: return _svc.v40; peter1138@7726: peter1138@7726: case 0x41: skidd13@8427: if (!HasBit(_svc.valid, 1)) { _svc.v41 = GetPlatformInfoHelper(tile, true, false, false); SetBit(_svc.valid, 1); } peter1138@7726: return _svc.v41; peter1138@7726: maedhros@7345: case 0x42: return GetTerrainType(tile) | (GetRailType(tile) << 8); belugas@6674: case 0x43: return st->owner; // Station owner belugas@6674: case 0x44: return 2; // PBS status peter1138@7726: case 0x45: skidd13@8427: if (!HasBit(_svc.valid, 2)) { _svc.v45 = GetRailContinuationInfo(tile); SetBit(_svc.valid, 2); } peter1138@7726: return _svc.v45; peter1138@7726: peter1138@7726: case 0x46: skidd13@8427: if (!HasBit(_svc.valid, 3)) { _svc.v46 = GetPlatformInfoHelper(tile, false, false, true); SetBit(_svc.valid, 3); } peter1138@7726: return _svc.v46; peter1138@7726: peter1138@7726: case 0x47: skidd13@8427: if (!HasBit(_svc.valid, 4)) { _svc.v47 = GetPlatformInfoHelper(tile, true, false, true); SetBit(_svc.valid, 4); } peter1138@7726: return _svc.v47; peter1138@7726: belugas@6674: case 0x48: { // Accepted cargo types peter1138@3743: CargoID cargo_type; peter1138@3743: uint32 value = 0; peter1138@3743: peter1138@3743: for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) { skidd13@8427: if (HasBit(st->goods[cargo_type].acceptance_pickup, GoodsEntry::PICKUP)) SetBit(value, cargo_type); peter1138@2967: } peter1138@3743: return value; peter1138@2967: } peter1138@7726: case 0x49: skidd13@8427: if (!HasBit(_svc.valid, 5)) { _svc.v49 = GetPlatformInfoHelper(tile, false, true, false); SetBit(_svc.valid, 5); } peter1138@7726: return _svc.v49; peter1138@2967: peter1138@3743: /* Variables which use the parameter */ peter1138@6956: /* Variables 0x60 to 0x65 are handled separately below */ glx@8614: case 0x67: { // Land info of nearby tiles glx@8614: Axis axis = GetRailStationAxis(tile); glx@8614: glx@8614: if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required glx@8614: byte tile_type = GetTerrainType(tile) << 2 | (IsTileType(tile, MP_WATER) ? 1 : 0) << 1; glx@8614: glx@8614: uint z; glx@8614: Slope tileh = GetTileSlope(tile, &z); glx@8614: bool swap = (axis == AXIS_Y && HasBit(tileh, 0) != HasBit(tileh, 2)); glx@8614: return GetTileType(tile) << 24 | z << 16 | tile_type << 8 | (tileh ^ (swap ? 5 : 0)); glx@8614: } glx@8614: glx@8614: case 0x68: { // Station info of nearby tiles glx@8614: TileIndex nearby_tile = GetNearbyTile(parameter, tile); glx@8614: glx@8614: if (!IsRailwayStationTile(nearby_tile)) return 0xFFFFFFFF; glx@8614: glx@8614: uint32 grfid = st->speclist[GetCustomStationSpecIndex(tile)].grfid; glx@8614: bool perpendicular = GetRailStationAxis(tile) != GetRailStationAxis(nearby_tile); glx@8614: bool same_station = st->TileBelongsToRailStation(nearby_tile); glx@8614: uint32 res = GB(GetStationGfx(nearby_tile), 1, 2) << 12 | !!perpendicular << 11 | !!same_station << 10; glx@8614: glx@8614: if (IsCustomStationSpecIndex(nearby_tile)) { glx@8614: const StationSpecList ssl = GetStationByTile(nearby_tile)->speclist[GetCustomStationSpecIndex(nearby_tile)]; glx@8614: res |= 1 << (ssl.grfid != grfid ? 9 : 8) | ssl.localidx; glx@8614: } glx@8614: return res; glx@8614: } peter1138@3743: peter1138@3743: /* General station properties */ peter1138@3743: case 0x82: return 50; peter1138@3743: case 0x84: return st->string_id; peter1138@3743: case 0x86: return 0; peter1138@3746: case 0x8A: return st->had_vehicle_of_type; peter1138@3743: case 0xF0: return st->facilities; peter1138@3743: case 0xF1: return st->airport_type; peter1138@3743: case 0xF2: return st->truck_stops->status; peter1138@3743: case 0xF3: return st->bus_stops->status; peter1138@3743: case 0xF6: return st->airport_flags; peter1138@5172: case 0xF7: return GB(st->airport_flags, 8, 8); skidd13@8418: case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); peter1138@2967: } peter1138@3743: peter1138@6956: /* Handle cargo variables with parameter, 0x60 to 0x65 */ peter1138@6956: if (variable >= 0x60 && variable <= 0x65) { peter1138@6956: CargoID c = GetCargoTranslation(parameter, object->u.station.statspec->grffile); peter1138@6956: peter1138@6956: if (c == CT_INVALID) return 0; peter1138@6956: const GoodsEntry *ge = &st->goods[c]; peter1138@6956: peter1138@6956: switch (variable) { rubidium@7506: case 0x60: return min(ge->cargo.Count(), 4095); peter1138@6956: case 0x61: return ge->days_since_pickup; peter1138@6956: case 0x62: return ge->rating; rubidium@7506: case 0x63: return ge->cargo.DaysInTransit(); peter1138@6956: case 0x64: return ge->last_speed | (ge->last_age << 8); rubidium@7970: case 0x65: return GB(ge->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 3; peter1138@6956: } peter1138@6956: } peter1138@6956: peter1138@3746: /* Handle cargo variables (deprecated) */ peter1138@3746: if (variable >= 0x8C && variable <= 0xEC) { peter1138@3746: const GoodsEntry *g = &st->goods[GB(variable - 0x8C, 3, 4)]; peter1138@3746: switch (GB(variable - 0x8C, 0, 3)) { rubidium@7506: case 0: return g->cargo.Count(); rubidium@7970: case 1: return GB(min(g->cargo.Count(), 4095), 0, 4) | (GB(g->acceptance_pickup, GoodsEntry::ACCEPTANCE, 1) << 7); peter1138@3746: case 2: return g->days_since_pickup; peter1138@3746: case 3: return g->rating; rubidium@7506: case 4: return g->cargo.Source(); rubidium@7506: case 5: return g->cargo.DaysInTransit(); peter1138@3746: case 6: return g->last_speed; peter1138@3746: case 7: return g->last_age; peter1138@3746: } peter1138@3746: } peter1138@3746: Darkvater@5568: DEBUG(grf, 1, "Unhandled station property 0x%X", variable); peter1138@3743: peter1138@3893: *available = false; rubidium@5838: return UINT_MAX; peter1138@2967: } peter1138@2967: peter1138@2967: peter1138@3743: static const SpriteGroup *StationResolveReal(const ResolverObject *object, const SpriteGroup *group) peter1138@3743: { peter1138@3743: const Station *st = object->u.station.st; peter1138@3743: const StationSpec *statspec = object->u.station.statspec; peter1138@3743: uint set; peter1138@2967: peter1138@3743: uint cargo = 0; peter1138@4817: CargoID cargo_type = object->u.station.cargo_type; peter1138@3743: peter1138@3743: if (st == NULL || statspec->sclass == STAT_CLASS_WAYP) { peter1138@3743: return group->g.real.loading[0]; peter1138@3743: } peter1138@3743: peter1138@4817: switch (cargo_type) { peter1138@6474: case CT_INVALID: peter1138@6474: case CT_DEFAULT_NA: peter1138@6474: case CT_PURCHASE: peter1138@4817: cargo = 0; peter1138@4817: break; peter1138@4817: peter1138@6474: case CT_DEFAULT: peter1138@4817: for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) { rubidium@7506: cargo += st->goods[cargo_type].cargo.Count(); peter1138@4817: } peter1138@4817: break; peter1138@4817: peter1138@4817: default: rubidium@7506: cargo = st->goods[cargo_type].cargo.Count(); peter1138@4817: break; peter1138@3743: } peter1138@3743: skidd13@8424: if (HasBit(statspec->flags, 1)) cargo /= (st->trainst_w + st->trainst_h); peter1138@3743: cargo = min(0xfff, cargo); peter1138@3743: peter1138@3743: if (cargo > statspec->cargo_threshold) { peter1138@3749: if (group->g.real.num_loading > 0) { peter1138@3787: set = ((cargo - statspec->cargo_threshold) * group->g.real.num_loading) / (4096 - statspec->cargo_threshold); peter1138@3749: return group->g.real.loading[set]; peter1138@3743: } peter1138@3743: } else { peter1138@3749: if (group->g.real.num_loaded > 0) { peter1138@3749: set = (cargo * group->g.real.num_loaded) / (statspec->cargo_threshold + 1); peter1138@3749: return group->g.real.loaded[set]; peter1138@3743: } peter1138@3743: } peter1138@3743: peter1138@3743: return group->g.real.loading[0]; peter1138@3743: } peter1138@3743: peter1138@3743: peter1138@3743: static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, const Station *st, TileIndex tile) peter1138@3743: { peter1138@3743: res->GetRandomBits = StationGetRandomBits; peter1138@3743: res->GetTriggers = StationGetTriggers; peter1138@3743: res->SetTriggers = StationSetTriggers; peter1138@3743: res->GetVariable = StationGetVariable; peter1138@3743: res->ResolveReal = StationResolveReal; peter1138@3743: peter1138@3743: res->u.station.st = st; peter1138@3743: res->u.station.statspec = statspec; peter1138@3743: res->u.station.tile = tile; peter1138@3743: rubidium@7823: res->callback = CBID_NO_CALLBACK; peter1138@3743: res->callback_param1 = 0; peter1138@3743: res->callback_param2 = 0; peter1138@3743: res->last_value = 0; peter1138@3743: res->trigger = 0; peter1138@3743: res->reseed = 0; peter1138@3743: } peter1138@3743: tron@6100: static const SpriteGroup *ResolveStation(ResolverObject *object) peter1138@4817: { peter1138@4817: const SpriteGroup *group; peter1138@6474: CargoID ctype = CT_DEFAULT_NA; peter1138@4817: tron@6100: if (object->u.station.st == NULL) { peter1138@4817: /* No station, so we are in a purchase list */ peter1138@6474: ctype = CT_PURCHASE; peter1138@4817: } else { peter1138@4817: /* Pick the first cargo that we have waiting */ peter1138@6439: for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) { peter1138@6439: const CargoSpec *cs = GetCargo(cargo); peter1138@6474: if (cs->IsValid() && object->u.station.statspec->spritegroup[cargo] != NULL && rubidium@7506: !object->u.station.st->goods[cargo].cargo.Empty()) { peter1138@6474: ctype = cargo; peter1138@4817: break; peter1138@4817: } peter1138@4817: } peter1138@4817: } peter1138@4817: tron@6100: group = object->u.station.statspec->spritegroup[ctype]; peter1138@4817: if (group == NULL) { peter1138@6474: ctype = CT_DEFAULT; tron@6100: group = object->u.station.statspec->spritegroup[ctype]; peter1138@4817: } peter1138@4817: peter1138@4817: if (group == NULL) return NULL; peter1138@4817: peter1138@4817: /* Remember the cargo type we've picked */ peter1138@4817: object->u.station.cargo_type = ctype; peter1138@4817: peter1138@7726: /* Invalidate all cached vars */ peter1138@7726: _svc.valid = 0; peter1138@7726: peter1138@4817: return Resolve(group, object); peter1138@4817: } peter1138@3743: peter1138@3751: SpriteID GetCustomStationRelocation(const StationSpec *statspec, const Station *st, TileIndex tile) peter1138@3743: { peter1138@3743: const SpriteGroup *group; peter1138@3743: ResolverObject object; peter1138@3743: peter1138@3743: NewStationResolver(&object, statspec, st, tile); peter1138@3743: tron@6100: group = ResolveStation(&object); peter1138@3743: if (group == NULL || group->type != SGT_RESULT) return 0; peter1138@3775: return group->g.result.sprite - 0x42D; peter1138@3775: } peter1138@3775: peter1138@3775: peter1138@3775: SpriteID GetCustomStationGroundRelocation(const StationSpec *statspec, const Station *st, TileIndex tile) peter1138@3775: { peter1138@3775: const SpriteGroup *group; peter1138@3775: ResolverObject object; peter1138@3775: peter1138@3775: NewStationResolver(&object, statspec, st, tile); belugas@6674: object.callback_param1 = 1; // Indicate we are resolving the ground sprite peter1138@3775: tron@6100: group = ResolveStation(&object); peter1138@3775: if (group == NULL || group->type != SGT_RESULT) return 0; peter1138@3775: return group->g.result.sprite - 0x42D; peter1138@2967: } peter1138@3587: peter1138@3587: rubidium@7823: uint16 GetStationCallback(CallbackID callback, uint32 param1, uint32 param2, const StationSpec *statspec, const Station *st, TileIndex tile) peter1138@3752: { peter1138@3752: const SpriteGroup *group; peter1138@3752: ResolverObject object; peter1138@3752: peter1138@3752: NewStationResolver(&object, statspec, st, tile); peter1138@3752: peter1138@3752: object.callback = callback; peter1138@3752: object.callback_param1 = param1; peter1138@3752: object.callback_param2 = param2; peter1138@3752: tron@6100: group = ResolveStation(&object); peter1138@3752: if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED; peter1138@3752: return group->g.callback.result; peter1138@3752: } peter1138@3752: peter1138@3752: peter1138@3587: /** peter1138@3587: * Allocate a StationSpec to a Station. This is called once per build operation. belugas@6977: * @param statspec StationSpec to allocate. peter1138@3587: * @param st Station to allocate it to. peter1138@3587: * @param exec Whether to actually allocate the spec. peter1138@3587: * @return Index within the Station's spec list, or -1 if the allocation failed. peter1138@3587: */ belugas@3676: int AllocateSpecToStation(const StationSpec *statspec, Station *st, bool exec) peter1138@3587: { peter1138@3587: uint i; peter1138@3587: belugas@3676: if (statspec == NULL) return 0; peter1138@3587: peter1138@3587: /* Check if this spec has already been allocated */ peter1138@3786: for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) { belugas@3676: if (st->speclist[i].spec == statspec) return i; peter1138@3587: } peter1138@3587: peter1138@3786: for (i = 1; i < st->num_specs && i < MAX_SPECLIST; i++) { peter1138@3587: if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break; peter1138@3587: } peter1138@3587: peter1138@3786: if (i == MAX_SPECLIST) return -1; peter1138@3587: peter1138@3730: if (exec) { peter1138@3730: if (i >= st->num_specs) { peter1138@3730: st->num_specs = i + 1; KUDr@5860: st->speclist = ReallocT(st->speclist, st->num_specs); peter1138@3730: peter1138@3730: if (st->num_specs == 2) { peter1138@3730: /* Initial allocation */ peter1138@3730: st->speclist[0].spec = NULL; peter1138@3730: st->speclist[0].grfid = 0; peter1138@3730: st->speclist[0].localidx = 0; peter1138@3587: } peter1138@3730: } peter1138@3587: peter1138@3730: st->speclist[i].spec = statspec; peter1138@6947: st->speclist[i].grfid = statspec->grffile->grfid; peter1138@3730: st->speclist[i].localidx = statspec->localidx; peter1138@3587: } peter1138@3587: peter1138@3730: return i; peter1138@3587: } peter1138@3587: peter1138@3587: peter1138@3587: /** Deallocate a StationSpec from a Station. Called when removing a single station tile. peter1138@3587: * @param st Station to work with. peter1138@3587: * @param specindex Index of the custom station within the Station's spec list. peter1138@3587: * @return Indicates whether the StationSpec was deallocated. peter1138@3587: */ tron@4190: void DeallocateSpecFromStation(Station* st, byte specindex) peter1138@3587: { peter1138@3587: /* specindex of 0 (default) is never freeable */ tron@4190: if (specindex == 0) return; peter1138@3587: peter1138@3587: /* Check all tiles over the station to check if the specindex is still in use */ peter1138@3587: BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) { peter1138@3587: if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile) && GetCustomStationSpecIndex(tile) == specindex) { tron@4190: return; peter1138@3587: } peter1138@3587: } END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) peter1138@3587: tron@4190: /* This specindex is no longer in use, so deallocate it */ tron@4190: st->speclist[specindex].spec = NULL; tron@4190: st->speclist[specindex].grfid = 0; tron@4190: st->speclist[specindex].localidx = 0; peter1138@3587: tron@4190: /* If this was the highest spec index, reallocate */ tron@4190: if (specindex == st->num_specs - 1) { tron@4190: for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--); peter1138@3587: tron@4190: if (st->num_specs > 1) { KUDr@5860: st->speclist = ReallocT(st->speclist, st->num_specs); tron@4190: } else { tron@4190: free(st->speclist); tron@4190: st->num_specs = 0; tron@4190: st->speclist = NULL; peter1138@3587: } peter1138@3587: } peter1138@3587: } peter1138@3764: peter1138@3764: /** Draw representation of a station tile for GUI purposes. belugas@6977: * @param x Position x of image. belugas@6977: * @param y Position y of image. belugas@6977: * @param axis Axis. peter1138@3764: * @param railtype Rail type. peter1138@3764: * @param sclass, station Type of station. belugas@6977: * @param station station ID peter1138@3764: * @return True if the tile was drawn (allows for fallback to default graphic) peter1138@3764: */ peter1138@3764: bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID sclass, uint station) peter1138@3764: { peter1138@3764: const StationSpec *statspec; peter1138@3764: const DrawTileSprites *sprites; peter1138@3764: const DrawTileSeqStruct *seq; peter1138@3764: const RailtypeInfo *rti = GetRailTypeInfo(railtype); peter1138@3764: SpriteID relocation; peter1138@5919: SpriteID image; peter1138@5919: SpriteID pal = PLAYER_SPRITE_COLOR(_local_player); peter1138@3764: uint tile = 2; peter1138@3764: peter1138@3764: statspec = GetCustomStationSpec(sclass, station); peter1138@3764: if (statspec == NULL) return false; peter1138@3764: peter1138@3764: relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE); peter1138@3764: skidd13@8424: if (HasBit(statspec->callbackmask, CBM_STATION_SPRITE_LAYOUT)) { peter1138@3764: uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0x2110000, 0, statspec, NULL, INVALID_TILE); peter1138@3764: if (callback != CALLBACK_FAILED) tile = callback; peter1138@3764: } peter1138@3764: peter1138@3764: if (statspec->renderdata == NULL) { rubidium@7768: sprites = GetStationTileLayout(STATION_RAIL, tile + axis); peter1138@3764: } else { rubidium@5838: sprites = &statspec->renderdata[(tile < statspec->tiles) ? tile + axis : (uint)axis]; peter1138@3764: } peter1138@3764: peter1138@3764: image = sprites->ground_sprite; skidd13@8424: if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) { peter1138@3775: image += GetCustomStationGroundRelocation(statspec, NULL, INVALID_TILE); peter1138@3775: image += rti->custom_ground_offset; peter1138@3775: } else { peter1138@3775: image += rti->total_offset; peter1138@3775: } peter1138@3764: peter1138@5919: DrawSprite(image, PAL_NONE, x, y); peter1138@3764: peter1138@3764: foreach_draw_tile_seq(seq, sprites->seq) { peter1138@3764: Point pt; peter1138@3775: image = seq->image; skidd13@8424: if (HasBit(image, SPRITE_MODIFIER_USE_OFFSET)) { peter1138@3775: image += rti->total_offset; peter1138@3775: } else { peter1138@3775: image += relocation; peter1138@3775: } peter1138@3764: peter1138@3764: if ((byte)seq->delta_z != 0x80) { peter1138@3764: pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z); peter1138@5919: DrawSprite(image, pal, x + pt.x, y + pt.y); peter1138@3764: } peter1138@3764: } peter1138@3764: peter1138@3764: return true; peter1138@3764: } peter1138@3764: tron@4206: tron@4206: static const StationSpec* GetStationSpec(TileIndex t) tron@4206: { tron@4206: const Station* st; tron@4206: uint specindex; tron@4206: tron@4206: if (!IsCustomStationSpecIndex(t)) return NULL; tron@4206: tron@4206: st = GetStationByTile(t); tron@4206: specindex = GetCustomStationSpecIndex(t); tron@4206: return specindex < st->num_specs ? st->speclist[specindex].spec : NULL; tron@4206: } tron@4206: tron@4206: peter1138@3766: /* Check if a rail station tile is traversable. peter1138@3766: * XXX This could be cached (during build) in the map array to save on all the dereferencing */ peter1138@3766: bool IsStationTileBlocked(TileIndex tile) peter1138@3766: { tron@4206: const StationSpec* statspec = GetStationSpec(tile); peter1138@3766: skidd13@8424: return statspec != NULL && HasBit(statspec->blocked, GetStationGfx(tile)); peter1138@3766: } glx@3789: glx@3789: /* Check if a rail station tile is electrifiable. glx@3789: * XXX This could be cached (during build) in the map array to save on all the dereferencing */ glx@3789: bool IsStationTileElectrifiable(TileIndex tile) glx@3789: { tron@4206: const StationSpec* statspec = GetStationSpec(tile); glx@3789: tron@4206: return tron@4206: statspec == NULL || skidd13@8424: HasBit(statspec->pylons, GetStationGfx(tile)) || skidd13@8424: !HasBit(statspec->wires, GetStationGfx(tile)); glx@3789: }