Files @ r15689:f98f51eeeb0c
Branch filter:

Location: cpp/openttd-patchpack/source/src/newgrf_airport.cpp - annotation

yexo
(svn r20365) -Codechange: allow multiple layouts for one airport statemachine, store the layout number
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r15534:64974155b7a6
r14618:a194334b4a3a
r14618:a194334b4a3a
r15688:5bb1558218af
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14851:b3915c863ff3
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r14727:9ddd9a15dd3b
r14618:a194334b4a3a
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14851:b3915c863ff3
r14851:b3915c863ff3
r14725:026b6e13b803
r14725:026b6e13b803
r14618:a194334b4a3a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14725:026b6e13b803
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14618:a194334b4a3a
r14618:a194334b4a3a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r14726:e42b5d6d3c6a
r15656:e21174b90f22
r14618:a194334b4a3a
r14618:a194334b4a3a
r14729:e9e99770f7ff
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14618:a194334b4a3a
r14725:026b6e13b803
r14725:026b6e13b803
r14725:026b6e13b803
r14725:026b6e13b803
r14725:026b6e13b803
r14725:026b6e13b803
r14725:026b6e13b803
r14725:026b6e13b803
r14850:537311950e1a
r14851:b3915c863ff3
r14851:b3915c863ff3
r14725:026b6e13b803
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14729:e9e99770f7ff
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14727:9ddd9a15dd3b
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r14851:b3915c863ff3
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15689:f98f51eeeb0c
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
r15688:5bb1558218af
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file newgrf_airport.cpp NewGRF handling of airports. */

#include "stdafx.h"
#include "debug.h"
#include "airport.h"
#include "newgrf_airport.h"
#include "date_func.h"
#include "settings_type.h"
#include "core/alloc_type.hpp"
#include "newgrf.h"
#include "newgrf_commons.h"
#include "newgrf_spritegroup.h"
#include "newgrf_text.h"
#include "station_base.h"
#include "table/strings.h"

static AirportClass _airport_classes[APC_MAX];

AirportOverrideManager _airport_mngr(NEW_AIRPORT_OFFSET, NUM_AIRPORTS, AT_INVALID);

AirportSpec AirportSpec::specs[NUM_AIRPORTS];

/**
 * Retrieve airport spec for the given airport. If an override is available
 *  it is returned.
 * @param type index of airport
 * @return A pointer to the corresponding AirportSpec
 */
/* static */ const AirportSpec *AirportSpec::Get(byte type)
{
	assert(type < lengthof(AirportSpec::specs));
	const AirportSpec *as = &AirportSpec::specs[type];
	if (type >= NEW_AIRPORT_OFFSET && !as->enabled) {
		byte subst_id = _airport_mngr.GetSubstituteID(type);
		if (subst_id == AT_INVALID) return as;
		as = &AirportSpec::specs[subst_id];
	}
	if (as->grf_prop.override != AT_INVALID) return &AirportSpec::specs[as->grf_prop.override];
	return as;
}

/**
 * Retrieve airport spec for the given airport. Even if an override is
 *  available the base spec is returned.
 * @param type index of airport
 * @return A pointer to the corresponding AirportSpec
 */
/* static */ AirportSpec *AirportSpec::GetWithoutOverride(byte type)
{
	assert(type < lengthof(AirportSpec::specs));
	return &AirportSpec::specs[type];
}

/** Check if this airport is available to build. */
bool AirportSpec::IsAvailable() const
{
	if (!this->enabled) return false;
	if (_cur_year < this->min_year) return false;
	if (_settings_game.station.never_expire_airports) return true;
	return _cur_year <= this->max_year;
}

/**
 * This function initialize the airportspec array.
 */
void AirportSpec::ResetAirports()
{
	extern const AirportSpec _origin_airport_specs[];
	memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs));
	memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NEW_AIRPORT_OFFSET);

	_airport_mngr.ResetOverride();
}

/**
 * Allocate an airport class for the given class id
 * @param cls A 32 bit value identifying the class
 * @return Index into _airport_classes of allocated class
 */
AirportClassID AllocateAirportClass(uint32 cls)
{
	for (AirportClassID i = APC_BEGIN; i < APC_MAX; i++) {
		if (_airport_classes[i].id == cls) {
			/* ClassID is already allocated, so reuse it. */
			return i;
		} else if (_airport_classes[i].id == 0) {
			/* This class is empty, so allocate it to the ClassID. */
			_airport_classes[i].id = cls;
			return i;
		}
	}

	grfmsg(2, "AllocateAirportClass: already allocated %d classes, using small airports class", APC_MAX);
	return APC_SMALL;
}

/**
 * Set the name of an airport class
 * @param id The id of the class to change the name from
 * @param name The new name for the class
 */
void SetAirportClassName(AirportClassID id, StringID name)
{
	assert(id < APC_MAX);
	_airport_classes[id].name = name;
}

/**
 * Retrieve the name of an airport class
 * @param id The id of the airport class to get the name from
 * @return The name of the given class
 */
StringID GetAirportClassName(AirportClassID id)
{
	assert(id < APC_MAX);
	return _airport_classes[id].name;
}

/**
 * Get the number of airport classes in use
 * @return The number of airport classes
 */
uint GetNumAirportClasses()
{
	uint i;
	for (i = APC_BEGIN; i < APC_MAX && _airport_classes[i].id != 0; i++) {}
	return i;
}

/**
 * Return the number of airports for the given airport class.
 * @param id Index of the airport class.
 * @return Number of airports in the class.
 */
uint GetNumAirportsInClass(AirportClassID id)
{
	assert(id < APC_MAX);
	return _airport_classes[id].airports;
}

/**
 * Tie an airport spec to its airport class.
 * @param statspec The airport spec.
 */
static void BindAirportSpecToClass(AirportSpec *as)
{
	assert(as->aclass < APC_MAX);
	AirportClass *airport_class = &_airport_classes[as->aclass];

	int i = airport_class->airports++;
	airport_class->spec = ReallocT(airport_class->spec, airport_class->airports);

	airport_class->spec[i] = as;
}

/**
 * Tie all airportspecs to their class.
 */
void BindAirportSpecs()
{
	for (int i = 0; i < NUM_AIRPORTS; i++) {
		AirportSpec *as = AirportSpec::GetWithoutOverride(i);
		if (as->enabled) BindAirportSpecToClass(as);
	}
}


/**
 * Retrieve an airport spec from a class.
 * @param aclass Index of the airport class.
 * @param airport The airport index with the class.
 * @return The station spec.
 */
const AirportSpec *GetAirportSpecFromClass(AirportClassID aclass, uint airport)
{
	assert(aclass < APC_MAX);
	assert(airport < _airport_classes[aclass].airports);

	return _airport_classes[aclass].spec[airport];
}

/**
 * Reset airport classes to their default state.
 * This includes initialising the defaults classes with an empty
 * entry, for standard airports.
 */
void ResetAirportClasses()
{
	for (AirportClassID i = APC_BEGIN; i < APC_MAX; i++) {
		_airport_classes[i].id = 0;
		_airport_classes[i].name = STR_EMPTY;
		_airport_classes[i].airports = 0;

		free(_airport_classes[i].spec);
		_airport_classes[i].spec = NULL;
	}

	/* Set up initial data */
	AirportClassID id = AllocateAirportClass('SMAL');
	SetAirportClassName(id, STR_AIRPORT_CLASS_SMALL);

	id = AllocateAirportClass('LARG');
	SetAirportClassName(id, STR_AIRPORT_CLASS_LARGE);

	id = AllocateAirportClass('HUB_');
	SetAirportClassName(id, STR_AIRPORT_CLASS_HUB);

	id = AllocateAirportClass('HELI');
	SetAirportClassName(id, STR_AIRPORT_CLASS_HELIPORTS);
}

void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
{
	byte airport_id = this->AddEntityID(as->grf_prop.local_id, as->grf_prop.grffile->grfid, as->grf_prop.subst_id);

	if (airport_id == invalid_ID) {
		grfmsg(1, "Airport.SetEntitySpec: Too many airports allocated. Ignoring.");
		return;
	}

	memcpy(AirportSpec::GetWithoutOverride(airport_id), as, sizeof(*as));

	/* Now add the overrides. */
	for (int i = 0; i < max_offset; i++) {
		AirportSpec *overridden_as = AirportSpec::GetWithoutOverride(i);

		if (entity_overrides[i] != as->grf_prop.local_id || grfid_overrides[i] != as->grf_prop.grffile->grfid) continue;

		overridden_as->grf_prop.override = airport_id;
		overridden_as->enabled = false;
		entity_overrides[i] = invalid_ID;
		grfid_overrides[i] = 0;
	}
}

uint32 AirportGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
{
	const Station *st = object->u.airport.st;
	byte layout       = object->u.airport.layout;

	if (object->scope == VSG_SCOPE_PARENT) {
		DEBUG(grf, 1, "Parent scope for airports unavailable");
		*available = false;
		return UINT_MAX;
	}

	switch (variable) {
		case 0x40: return layout;
	}

	if (st == NULL) {
		*available = false;
		return UINT_MAX;
	}

	switch (variable) {
		case 0xF0: return st->facilities;
		case 0xFA: return Clamp(st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
	}

	return st->GetNewGRFVariable(object, variable, parameter, available);
}

static const SpriteGroup *AirportResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
{
	/* Airport action 2s should always have only 1 "loaded" state, but some
	 * times things don't follow the spec... */
	if (group->num_loaded > 0) return group->loaded[0];
	if (group->num_loading > 0) return group->loading[0];

	return NULL;
}

static uint32 AirportGetRandomBits(const ResolverObject *object)
{
	const Station *st = object->u.airport.st;
	const TileIndex tile = object->u.airport.tile;
	return (st == NULL ? 0 : st->random_bits) | (tile == INVALID_TILE ? 0 : GetStationTileRandomBits(tile) << 16);
}

static uint32 AirportGetTriggers(const ResolverObject *object)
{
	return 0;
}

static void AirportSetTriggers(const ResolverObject *object, int triggers)
{
}

static void NewAirportResolver(ResolverObject *res, TileIndex tile, Station *st, byte airport_id, byte layout)
{
	res->GetRandomBits = AirportGetRandomBits;
	res->GetTriggers   = AirportGetTriggers;
	res->SetTriggers   = AirportSetTriggers;
	res->GetVariable   = AirportGetVariable;
	res->ResolveReal   = AirportResolveReal;

	res->psa                  = NULL;
	res->u.airport.st         = st;
	res->u.airport.airport_id = airport_id;
	res->u.airport.layout     = layout;
	res->u.airport.tile       = tile;

	res->callback        = CBID_NO_CALLBACK;
	res->callback_param1 = 0;
	res->callback_param2 = 0;
	res->last_value      = 0;
	res->trigger         = 0;
	res->reseed          = 0;
	res->count           = 0;

	const AirportSpec *as = AirportSpec::Get(airport_id);
	res->grffile = (as != NULL ? as->grf_prop.grffile : NULL);
}

uint16 GetAirportCallback(CallbackID callback, uint32 param1, uint32 param2, Station *st, TileIndex tile)
{
	ResolverObject object;

	NewAirportResolver(&object, tile, st, st->airport.type, st->airport.layout);
	object.callback = callback;
	object.callback_param1 = param1;
	object.callback_param2 = param2;

	const SpriteGroup *group = SpriteGroup::Resolve(st->airport.GetSpec()->grf_prop.spritegroup, &object);
	if (group == NULL) return CALLBACK_FAILED;

	return group->GetCallbackResult();
}