2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file newgrf_cargo.cpp Implementation of NewGRF cargoes. */
|
|
|
|
|
2007-03-23 20:55:45 +00:00
|
|
|
#include "stdafx.h"
|
2007-03-26 08:43:14 +00:00
|
|
|
#include "debug.h"
|
2007-03-23 20:55:45 +00:00
|
|
|
#include "newgrf_spritegroup.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/** Resolver of cargo. */
|
2012-11-10 20:39:39 +00:00
|
|
|
struct CargoResolverObject : public ResolverObject {
|
2020-01-26 12:45:51 +00:00
|
|
|
const CargoSpec *cargospec;
|
|
|
|
|
2023-05-08 17:01:06 +00:00
|
|
|
CargoResolverObject(const CargoSpec *cs, CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0);
|
2007-03-23 20:55:45 +00:00
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature GetFeature() const override;
|
2023-05-08 17:01:06 +00:00
|
|
|
uint32_t GetDebugID() const override;
|
2012-11-10 20:39:39 +00:00
|
|
|
};
|
2007-03-23 20:55:45 +00:00
|
|
|
|
2020-01-26 12:45:51 +00:00
|
|
|
GrfSpecFeature CargoResolverObject::GetFeature() const
|
|
|
|
{
|
|
|
|
return GSF_CARGOES;
|
|
|
|
}
|
|
|
|
|
2023-05-08 17:01:06 +00:00
|
|
|
uint32_t CargoResolverObject::GetDebugID() const
|
2020-01-26 12:45:51 +00:00
|
|
|
{
|
|
|
|
return this->cargospec->label;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Constructor of the cargo resolver.
|
|
|
|
* @param cs Cargo being resolved.
|
|
|
|
* @param callback Callback ID.
|
|
|
|
* @param callback_param1 First parameter (var 10) of the callback.
|
|
|
|
* @param callback_param2 Second parameter (var 18) of the callback.
|
|
|
|
*/
|
2023-05-08 17:01:06 +00:00
|
|
|
CargoResolverObject::CargoResolverObject(const CargoSpec *cs, CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
|
2020-01-26 12:45:51 +00:00
|
|
|
: ResolverObject(cs->grffile, callback, callback_param1, callback_param2), cargospec(cs)
|
2007-03-23 20:55:45 +00:00
|
|
|
{
|
2014-03-03 20:02:31 +00:00
|
|
|
this->root_spritegroup = cs->group;
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Get the custom sprite for the given cargo type.
|
|
|
|
* @param cs Cargo being queried.
|
|
|
|
* @return Custom sprite to draw, or \c 0 if not available.
|
|
|
|
*/
|
2007-03-23 20:55:45 +00:00
|
|
|
SpriteID GetCustomCargoSprite(const CargoSpec *cs)
|
|
|
|
{
|
2012-11-10 20:39:39 +00:00
|
|
|
CargoResolverObject object(cs);
|
2014-03-03 20:02:31 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr) return 0;
|
2007-03-23 20:55:45 +00:00
|
|
|
|
2009-05-23 12:13:42 +00:00
|
|
|
return group->GetResult();
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-08 17:01:06 +00:00
|
|
|
uint16_t GetCargoCallback(CallbackID callback, uint32_t param1, uint32_t param2, const CargoSpec *cs)
|
2007-03-23 20:55:45 +00:00
|
|
|
{
|
2012-11-10 20:39:39 +00:00
|
|
|
CargoResolverObject object(cs, callback, param1, param2);
|
2014-03-03 20:02:31 +00:00
|
|
|
return object.ResolveCallback();
|
2007-03-23 20:55:45 +00:00
|
|
|
}
|
2007-04-13 19:32:18 +00:00
|
|
|
|
2012-02-07 22:46:26 +00:00
|
|
|
/**
|
|
|
|
* Translate a GRF-local cargo slot/bitnum into a CargoID.
|
|
|
|
* @param cargo GRF-local cargo slot/bitnum.
|
|
|
|
* @param grffile Originating GRF file.
|
|
|
|
* @param usebit Defines the meaning of \a cargo for GRF version < 7.
|
|
|
|
* If true, then \a cargo is a bitnum. If false, then \a cargo is a cargoslot.
|
|
|
|
* For GRF version >= 7 \a cargo is always a translated cargo bit.
|
2024-01-06 15:15:37 +00:00
|
|
|
* @return CargoID or INVALID_CARGO if the cargo is not available.
|
2012-02-07 22:46:26 +00:00
|
|
|
*/
|
2023-05-08 17:01:06 +00:00
|
|
|
CargoID GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
|
2007-04-13 19:32:18 +00:00
|
|
|
{
|
2012-02-08 18:11:49 +00:00
|
|
|
/* Pre-version 7 uses the 'climate dependent' ID in callbacks and properties, i.e. cargo is the cargo ID */
|
2021-01-10 14:10:03 +00:00
|
|
|
if (grffile->grf_version < 7 && !usebit) {
|
2024-01-06 15:15:37 +00:00
|
|
|
if (cargo >= CargoSpec::GetArraySize() || !CargoSpec::Get(cargo)->IsValid()) return INVALID_CARGO;
|
2021-01-10 14:10:03 +00:00
|
|
|
return cargo;
|
|
|
|
}
|
2012-02-08 18:11:49 +00:00
|
|
|
|
|
|
|
/* Other cases use (possibly translated) cargobits */
|
|
|
|
|
2023-10-20 18:18:31 +00:00
|
|
|
if (!grffile->cargo_list.empty()) {
|
2012-02-08 18:11:49 +00:00
|
|
|
/* ...and the cargo is in bounds, then get the cargo ID for
|
|
|
|
* the label */
|
2018-09-23 11:23:54 +00:00
|
|
|
if (cargo < grffile->cargo_list.size()) return GetCargoIDByLabel(grffile->cargo_list[cargo]);
|
2012-02-08 18:11:49 +00:00
|
|
|
} else {
|
2007-10-13 02:23:11 +00:00
|
|
|
/* Else the cargo value is a 'climate independent' 'bitnum' */
|
2012-02-07 22:46:26 +00:00
|
|
|
return GetCargoIDByBitnum(cargo);
|
2007-10-13 02:23:11 +00:00
|
|
|
}
|
2024-01-06 15:15:37 +00:00
|
|
|
return INVALID_CARGO;
|
2007-04-13 19:32:18 +00:00
|
|
|
}
|