mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-11-02 09:40:35 +00:00
415 lines
12 KiB
C
415 lines
12 KiB
C
/* $Id$ */
|
|
|
|
/** @file newgrf_station.c Functions for dealing with station classes and custom stations. */
|
|
|
|
#include "stdafx.h"
|
|
#include "openttd.h"
|
|
#include "variables.h"
|
|
#include "debug.h"
|
|
#include "sprite.h"
|
|
#include "table/strings.h"
|
|
#include "station.h"
|
|
#include "station_map.h"
|
|
#include "newgrf_station.h"
|
|
#include "newgrf_spritegroup.h"
|
|
|
|
static StationClass station_classes[STAT_CLASS_MAX];
|
|
|
|
/**
|
|
* Reset station classes to their default state.
|
|
* This includes initialising the Default and Waypoint classes with an empty
|
|
* entry, for standard stations and waypoints.
|
|
*/
|
|
void ResetStationClasses(void)
|
|
{
|
|
StationClassID i;
|
|
for (i = 0; i < STAT_CLASS_MAX; i++) {
|
|
station_classes[i].id = 0;
|
|
station_classes[i].name = STR_EMPTY;
|
|
station_classes[i].stations = 0;
|
|
|
|
free(station_classes[i].spec);
|
|
station_classes[i].spec = NULL;
|
|
}
|
|
|
|
// Set up initial data
|
|
station_classes[0].id = 'DFLT';
|
|
station_classes[0].name = STR_STAT_CLASS_DFLT;
|
|
station_classes[0].stations = 1;
|
|
station_classes[0].spec = malloc(sizeof(*station_classes[0].spec));
|
|
station_classes[0].spec[0] = NULL;
|
|
|
|
station_classes[1].id = 'WAYP';
|
|
station_classes[1].name = STR_STAT_CLASS_WAYP;
|
|
station_classes[1].stations = 1;
|
|
station_classes[1].spec = malloc(sizeof(*station_classes[1].spec));
|
|
station_classes[1].spec[0] = NULL;
|
|
}
|
|
|
|
/**
|
|
* Allocate a station class for the given class id.
|
|
* @param classid A 32 bit value identifying the class.
|
|
* @return Index into station_classes of allocated class.
|
|
*/
|
|
StationClassID AllocateStationClass(uint32 class)
|
|
{
|
|
StationClassID i;
|
|
|
|
for (i = 0; i < STAT_CLASS_MAX; i++) {
|
|
if (station_classes[i].id == class) {
|
|
// ClassID is already allocated, so reuse it.
|
|
return i;
|
|
} else if (station_classes[i].id == 0) {
|
|
// This class is empty, so allocate it to the ClassID.
|
|
station_classes[i].id = class;
|
|
return i;
|
|
}
|
|
}
|
|
|
|
DEBUG(grf, 2)("StationClassAllocate: Already allocated %d classes, using default.", STAT_CLASS_MAX);
|
|
return STAT_CLASS_DFLT;
|
|
}
|
|
|
|
/** Set the name of a custom station class */
|
|
void SetStationClassName(StationClassID sclass, StringID name)
|
|
{
|
|
assert(sclass < STAT_CLASS_MAX);
|
|
station_classes[sclass].name = name;
|
|
}
|
|
|
|
/** Retrieve the name of a custom station class */
|
|
StringID GetStationClassName(StationClassID sclass)
|
|
{
|
|
assert(sclass < STAT_CLASS_MAX);
|
|
return station_classes[sclass].name;
|
|
}
|
|
|
|
/** Build a list of station class name StringIDs to use in a dropdown list
|
|
* @return Pointer to a (static) array of StringIDs
|
|
*/
|
|
StringID *BuildStationClassDropdown(void)
|
|
{
|
|
/* Allow room for all station classes, plus a terminator entry */
|
|
static StringID names[STAT_CLASS_MAX + 1];
|
|
uint i;
|
|
|
|
/* Add each name */
|
|
for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++) {
|
|
names[i] = station_classes[i].name;
|
|
}
|
|
/* Terminate the list */
|
|
names[i] = INVALID_STRING_ID;
|
|
|
|
return names;
|
|
}
|
|
|
|
/**
|
|
* Get the number of station classes in use.
|
|
* @return Number of station classes.
|
|
*/
|
|
uint GetNumStationClasses(void)
|
|
{
|
|
uint i;
|
|
for (i = 0; i < STAT_CLASS_MAX && station_classes[i].id != 0; i++);
|
|
return i;
|
|
}
|
|
|
|
/**
|
|
* Return the number of stations for the given station class.
|
|
* @param sclass Index of the station class.
|
|
* @return Number of stations in the class.
|
|
*/
|
|
uint GetNumCustomStations(StationClassID sclass)
|
|
{
|
|
assert(sclass < STAT_CLASS_MAX);
|
|
return station_classes[sclass].stations;
|
|
}
|
|
|
|
/**
|
|
* Tie a station spec to its station class.
|
|
* @param spec The station spec.
|
|
*/
|
|
void SetCustomStationSpec(StationSpec *statspec)
|
|
{
|
|
StationClass *station_class;
|
|
int i;
|
|
|
|
assert(statspec->sclass < STAT_CLASS_MAX);
|
|
station_class = &station_classes[statspec->sclass];
|
|
|
|
i = station_class->stations++;
|
|
station_class->spec = realloc(station_class->spec, station_class->stations * sizeof(*station_class->spec));
|
|
|
|
station_class->spec[i] = statspec;
|
|
}
|
|
|
|
/**
|
|
* Retrieve a station spec from a class.
|
|
* @param sclass Index of the station class.
|
|
* @param station The station index with the class.
|
|
* @return The station spec.
|
|
*/
|
|
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
|
|
{
|
|
assert(sclass < STAT_CLASS_MAX);
|
|
if (station < station_classes[sclass].stations)
|
|
return station_classes[sclass].spec[station];
|
|
|
|
// If the custom station isn't defined any more, then the GRF file
|
|
// probably was not loaded.
|
|
return NULL;
|
|
}
|
|
|
|
|
|
/* Station Resolver Functions */
|
|
static uint32 StationGetRandomBits(const ResolverObject *object)
|
|
{
|
|
const Station *st = object->u.station.st;
|
|
const TileIndex tile = object->u.station.tile;
|
|
return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
|
|
}
|
|
|
|
|
|
static uint32 StationGetTriggers(const ResolverObject *object)
|
|
{
|
|
const Station *st = object->u.station.st;
|
|
return st == NULL ? 0 : st->waiting_triggers;
|
|
}
|
|
|
|
|
|
static void StationSetTriggers(const ResolverObject *object, int triggers)
|
|
{
|
|
Station *st = (Station*)object->u.station.st;
|
|
assert(st != NULL);
|
|
st->waiting_triggers = triggers;
|
|
}
|
|
|
|
|
|
static uint32 StationGetVariable(const ResolverObject *object, byte variable, byte parameter)
|
|
{
|
|
const Station *st = object->u.station.st;
|
|
|
|
if (st == NULL) {
|
|
/* Station does not exist, so we're in a purchase list */
|
|
switch (variable) {
|
|
case 0x40:
|
|
case 0x41:
|
|
case 0x46:
|
|
case 0x47:
|
|
case 0x49: return 0x2110000; /* Platforms, tracks & position */
|
|
case 0x42: return 0; /* Rail type (XXX Get current type from GUI?) */
|
|
case 0x43: return _current_player; /* Station owner */
|
|
case 0xFA: return _date; /* Build date */
|
|
default: return -1;
|
|
}
|
|
}
|
|
|
|
switch (variable) {
|
|
/* Calculated station variables */
|
|
case 0x43: return st->owner; /* Station owner */
|
|
case 0x44: return 0; /* PBS status */
|
|
case 0x48: { /* Accepted cargo types */
|
|
CargoID cargo_type;
|
|
uint32 value = 0;
|
|
|
|
for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
|
|
if (HASBIT(st->goods[cargo_type].waiting_acceptance, 15)) SETBIT(value, cargo_type);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/* Variables which use the parameter */
|
|
case 0x60: return GB(st->goods[parameter].waiting_acceptance, 0, 12);
|
|
case 0x61: return st->goods[parameter].days_since_pickup;
|
|
case 0x62: return st->goods[parameter].rating;
|
|
case 0x63: return st->goods[parameter].enroute_time;
|
|
case 0x64: return st->goods[parameter].last_speed | (st->goods[parameter].last_age << 8);
|
|
|
|
/* General station properties */
|
|
case 0x82: return 50;
|
|
case 0x84: return st->string_id;
|
|
case 0x86: return 0;
|
|
case 0x9A: return st->had_vehicle_of_type;
|
|
case 0xF0: return st->facilities;
|
|
case 0xF1: return st->airport_type;
|
|
case 0xF2: return st->truck_stops->status;
|
|
case 0xF3: return st->bus_stops->status;
|
|
case 0xF6: return st->airport_flags;
|
|
case 0xF7: return st->airport_flags & 0xFF;
|
|
case 0xFA: return st->build_date;
|
|
}
|
|
|
|
DEBUG(grf, 1)("Unhandled station property 0x%X", variable);
|
|
|
|
return -1;
|
|
}
|
|
|
|
|
|
static const SpriteGroup *StationResolveReal(const ResolverObject *object, const SpriteGroup *group)
|
|
{
|
|
const Station *st = object->u.station.st;
|
|
const StationSpec *statspec = object->u.station.statspec;
|
|
uint set;
|
|
|
|
uint cargo = 0;
|
|
CargoID cargo_type = CT_INVALID; /* XXX Pick the correct cargo type */
|
|
|
|
if (st == NULL || statspec->sclass == STAT_CLASS_WAYP) {
|
|
return group->g.real.loading[0];
|
|
}
|
|
|
|
if (cargo_type == CT_INVALID) {
|
|
for (cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
|
|
cargo += GB(st->goods[cargo_type].waiting_acceptance, 0, 12);
|
|
}
|
|
} else {
|
|
cargo = GB(st->goods[cargo_type].waiting_acceptance, 0, 12);
|
|
}
|
|
|
|
if (HASBIT(statspec->flags, 1)) cargo /= (st->trainst_w + st->trainst_h);
|
|
cargo = min(0xfff, cargo);
|
|
|
|
if (cargo > statspec->cargo_threshold) {
|
|
if (group->g.real.num_loaded > 0) {
|
|
set = ((cargo - statspec->cargo_threshold) * group->g.real.num_loaded) / (0xfff - statspec->cargo_threshold);
|
|
return group->g.real.loaded[set];
|
|
}
|
|
} else {
|
|
if (group->g.real.num_loading > 0) {
|
|
set = (cargo * group->g.real.num_loading) / statspec->cargo_threshold;
|
|
return group->g.real.loading[set];
|
|
}
|
|
}
|
|
|
|
return group->g.real.loading[0];
|
|
}
|
|
|
|
|
|
static void NewStationResolver(ResolverObject *res, const StationSpec *statspec, const Station *st, TileIndex tile)
|
|
{
|
|
res->GetRandomBits = StationGetRandomBits;
|
|
res->GetTriggers = StationGetTriggers;
|
|
res->SetTriggers = StationSetTriggers;
|
|
res->GetVariable = StationGetVariable;
|
|
res->ResolveReal = StationResolveReal;
|
|
|
|
res->u.station.st = st;
|
|
res->u.station.statspec = statspec;
|
|
res->u.station.tile = tile;
|
|
|
|
res->callback = 0;
|
|
res->callback_param1 = 0;
|
|
res->callback_param2 = 0;
|
|
res->last_value = 0;
|
|
res->trigger = 0;
|
|
res->reseed = 0;
|
|
}
|
|
|
|
|
|
SpriteID GetCustomStationRelocation(const StationSpec *statspec, const Station *st, TileIndex tile, CargoID ctype)
|
|
{
|
|
const SpriteGroup *group;
|
|
ResolverObject object;
|
|
|
|
NewStationResolver(&object, statspec, st, tile);
|
|
|
|
group = Resolve(statspec->spritegroup[ctype], &object);
|
|
if ((group == NULL || group->type != SGT_RESULT) && ctype != GC_DEFAULT) {
|
|
group = Resolve(statspec->spritegroup[GC_DEFAULT], &object);
|
|
}
|
|
|
|
if (group == NULL || group->type != SGT_RESULT) return 0;
|
|
|
|
return group->g.result.sprite;
|
|
}
|
|
|
|
|
|
/**
|
|
* Allocate a StationSpec to a Station. This is called once per build operation.
|
|
* @param spec StationSpec to allocate.
|
|
* @param st Station to allocate it to.
|
|
* @param exec Whether to actually allocate the spec.
|
|
* @return Index within the Station's spec list, or -1 if the allocation failed.
|
|
*/
|
|
int AllocateSpecToStation(const StationSpec *statspec, Station *st, bool exec)
|
|
{
|
|
uint i;
|
|
|
|
if (statspec == NULL) return 0;
|
|
|
|
/* Check if this spec has already been allocated */
|
|
for (i = 1; i < st->num_specs && i < 256; i++) {
|
|
if (st->speclist[i].spec == statspec) return i;
|
|
}
|
|
|
|
for (i = 1; i < st->num_specs && i < 256; i++) {
|
|
if (st->speclist[i].spec == NULL && st->speclist[i].grfid == 0) break;
|
|
}
|
|
|
|
if (i == 256) return -1;
|
|
|
|
if (exec) {
|
|
if (i >= st->num_specs) {
|
|
st->num_specs = i + 1;
|
|
st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist));
|
|
|
|
if (st->num_specs == 2) {
|
|
/* Initial allocation */
|
|
st->speclist[0].spec = NULL;
|
|
st->speclist[0].grfid = 0;
|
|
st->speclist[0].localidx = 0;
|
|
}
|
|
}
|
|
|
|
st->speclist[i].spec = statspec;
|
|
st->speclist[i].grfid = statspec->grfid;
|
|
st->speclist[i].localidx = statspec->localidx;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
/** Deallocate a StationSpec from a Station. Called when removing a single station tile.
|
|
* @param st Station to work with.
|
|
* @param specindex Index of the custom station within the Station's spec list.
|
|
* @return Indicates whether the StationSpec was deallocated.
|
|
*/
|
|
bool DeallocateSpecFromStation(Station *st, byte specindex)
|
|
{
|
|
bool freeable = true;
|
|
|
|
/* specindex of 0 (default) is never freeable */
|
|
if (specindex == 0) return false;
|
|
|
|
/* Check all tiles over the station to check if the specindex is still in use */
|
|
BEGIN_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile) {
|
|
if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st->index && IsRailwayStation(tile) && GetCustomStationSpecIndex(tile) == specindex) {
|
|
freeable = false;
|
|
break;
|
|
}
|
|
} END_TILE_LOOP(tile, st->trainst_w, st->trainst_h, st->train_tile)
|
|
|
|
if (freeable) {
|
|
/* This specindex is no longer in use, so deallocate it */
|
|
st->speclist[specindex].spec = NULL;
|
|
st->speclist[specindex].grfid = 0;
|
|
st->speclist[specindex].localidx = 0;
|
|
|
|
/* If this was the highest spec index, reallocate */
|
|
if (specindex == st->num_specs - 1) {
|
|
for (; st->speclist[st->num_specs - 1].grfid == 0 && st->num_specs > 1; st->num_specs--);
|
|
|
|
if (st->num_specs > 1) {
|
|
st->speclist = realloc(st->speclist, st->num_specs * sizeof(*st->speclist));
|
|
} else {
|
|
free(st->speclist);
|
|
st->num_specs = 0;
|
|
st->speclist = NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
return freeable;
|
|
}
|