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/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2012-01-01 17:22:32 +00:00
|
|
|
* @file newgrf_commons.cpp Implementation of the class %OverrideManagerBase
|
|
|
|
* and its descendance, present and future.
|
2007-05-15 21:36:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2011-11-08 17:24:15 +00:00
|
|
|
#include "debug.h"
|
2007-06-11 04:52:51 +00:00
|
|
|
#include "landscape.h"
|
2009-10-04 17:16:41 +00:00
|
|
|
#include "house.h"
|
|
|
|
#include "industrytype.h"
|
2011-11-08 17:24:15 +00:00
|
|
|
#include "newgrf_config.h"
|
2010-07-11 17:11:14 +00:00
|
|
|
#include "clear_map.h"
|
2007-12-21 22:11:55 +00:00
|
|
|
#include "station_map.h"
|
2008-01-31 17:54:13 +00:00
|
|
|
#include "tree_map.h"
|
2010-07-11 17:11:14 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2010-08-28 17:30:55 +00:00
|
|
|
#include "newgrf_object.h"
|
2010-08-08 21:34:19 +00:00
|
|
|
#include "genworld.h"
|
2011-05-29 16:56:22 +00:00
|
|
|
#include "newgrf_spritegroup.h"
|
2011-07-11 16:32:19 +00:00
|
|
|
#include "newgrf_text.h"
|
2011-08-27 08:42:24 +00:00
|
|
|
#include "company_base.h"
|
2011-12-10 13:54:10 +00:00
|
|
|
#include "error.h"
|
2011-11-08 17:24:15 +00:00
|
|
|
#include "strings_func.h"
|
2011-07-11 16:32:19 +00:00
|
|
|
|
|
|
|
#include "table/strings.h"
|
2007-05-15 21:36:58 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Constructor of generic class
|
2007-05-15 21:36:58 +00:00
|
|
|
* @param offset end of original data for this entity. i.e: houses = 110
|
|
|
|
* @param maximum of entities this manager can deal with. i.e: houses = 512
|
|
|
|
* @param invalid is the ID used to identify an invalid entity id
|
|
|
|
*/
|
|
|
|
OverrideManagerBase::OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid)
|
|
|
|
{
|
|
|
|
max_offset = offset;
|
|
|
|
max_new_entities = maximum;
|
|
|
|
invalid_ID = invalid;
|
|
|
|
|
|
|
|
mapping_ID = CallocT<EntityIDMapping>(max_new_entities);
|
|
|
|
entity_overrides = MallocT<uint16>(max_offset);
|
2009-02-24 22:25:23 +00:00
|
|
|
for (size_t i = 0; i < max_offset; i++) entity_overrides[i] = invalid;
|
2007-11-12 20:40:58 +00:00
|
|
|
grfid_overrides = CallocT<uint32>(max_offset);
|
2007-05-15 21:36:58 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Destructor of the generic class.
|
2007-05-15 21:36:58 +00:00
|
|
|
* Frees allocated memory of constructor
|
|
|
|
*/
|
|
|
|
OverrideManagerBase::~OverrideManagerBase()
|
|
|
|
{
|
|
|
|
free(mapping_ID);
|
|
|
|
free(entity_overrides);
|
2007-11-12 20:40:58 +00:00
|
|
|
free(grfid_overrides);
|
2007-05-15 21:36:58 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Since the entity IDs defined by the GRF file does not necessarily correlate
|
2007-05-15 21:36:58 +00:00
|
|
|
* to those used by the game, the IDs used for overriding old entities must be
|
|
|
|
* translated when the entity spec is set.
|
2007-11-12 20:40:58 +00:00
|
|
|
* @param local_id ID in grf file
|
|
|
|
* @param grfid ID of the grf file
|
2007-05-15 21:36:58 +00:00
|
|
|
* @param entity_type original entity type
|
|
|
|
*/
|
2007-11-12 20:40:58 +00:00
|
|
|
void OverrideManagerBase::Add(uint8 local_id, uint32 grfid, uint entity_type)
|
2007-05-15 21:36:58 +00:00
|
|
|
{
|
|
|
|
assert(entity_type < max_offset);
|
2007-11-12 20:42:35 +00:00
|
|
|
/* An override can be set only once */
|
|
|
|
if (entity_overrides[entity_type] != invalid_ID) return;
|
2007-05-15 21:36:58 +00:00
|
|
|
entity_overrides[entity_type] = local_id;
|
2007-11-12 20:40:58 +00:00
|
|
|
grfid_overrides[entity_type] = grfid;
|
2007-05-15 21:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Resets the mapping, which is used while initializing game */
|
|
|
|
void OverrideManagerBase::ResetMapping()
|
|
|
|
{
|
|
|
|
memset(mapping_ID, 0, (max_new_entities - 1) * sizeof(EntityIDMapping));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resets the override, which is used while initializing game */
|
|
|
|
void OverrideManagerBase::ResetOverride()
|
|
|
|
{
|
|
|
|
for (uint16 i = 0; i < max_offset; i++) {
|
|
|
|
entity_overrides[i] = invalid_ID;
|
2007-11-12 20:40:58 +00:00
|
|
|
grfid_overrides[i] = 0;
|
2007-05-15 21:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Return the ID (if ever available) of a previously inserted entity.
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param grf_local_id ID of this entity within the grfID
|
2007-05-15 21:36:58 +00:00
|
|
|
* @param grfid ID of the grf file
|
|
|
|
* @return the ID of the candidate, of the Invalid flag item ID
|
|
|
|
*/
|
2010-08-11 18:50:23 +00:00
|
|
|
uint16 OverrideManagerBase::GetID(uint8 grf_local_id, uint32 grfid) const
|
2007-05-15 21:36:58 +00:00
|
|
|
{
|
|
|
|
const EntityIDMapping *map;
|
|
|
|
|
2007-07-12 07:53:53 +00:00
|
|
|
for (uint16 id = 0; id < max_new_entities; id++) {
|
2007-05-15 21:36:58 +00:00
|
|
|
map = &mapping_ID[id];
|
|
|
|
if (map->entity_id == grf_local_id && map->grfid == grfid) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
2007-11-12 20:57:03 +00:00
|
|
|
|
2007-05-15 21:36:58 +00:00
|
|
|
return invalid_ID;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Reserves a place in the mapping array for an entity to be installed
|
2007-05-15 21:36:58 +00:00
|
|
|
* @param grf_local_id is an arbitrary id given by the grf's author. Also known as setid
|
|
|
|
* @param grfid is the id of the grf file itself
|
|
|
|
* @param substitute_id is the original entity from which data is copied for the new one
|
|
|
|
* @return the proper usable slot id, or invalid marker if none is found
|
|
|
|
*/
|
|
|
|
uint16 OverrideManagerBase::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
|
|
|
|
{
|
|
|
|
uint16 id = this->GetID(grf_local_id, grfid);
|
|
|
|
EntityIDMapping *map;
|
|
|
|
|
|
|
|
/* Look to see if this entity has already been added. This is done
|
|
|
|
* separately from the loop below in case a GRF has been deleted, and there
|
|
|
|
* are any gaps in the array.
|
|
|
|
*/
|
|
|
|
if (id != invalid_ID) {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This entity hasn't been defined before, so give it an ID now. */
|
|
|
|
for (id = max_offset; id < max_new_entities; id++) {
|
|
|
|
map = &mapping_ID[id];
|
|
|
|
|
2007-09-26 02:15:00 +00:00
|
|
|
if (CheckValidNewID(id) && map->entity_id == 0 && map->grfid == 0) {
|
2007-05-15 21:36:58 +00:00
|
|
|
map->entity_id = grf_local_id;
|
|
|
|
map->grfid = grfid;
|
|
|
|
map->substitute_id = substitute_id;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return invalid_ID;
|
|
|
|
}
|
|
|
|
|
2011-06-12 20:42:23 +00:00
|
|
|
/**
|
|
|
|
* Gives the GRFID of the file the entity belongs to.
|
|
|
|
* @param entity_id ID of the entity being queried.
|
|
|
|
* @return GRFID.
|
|
|
|
*/
|
2011-06-13 06:28:43 +00:00
|
|
|
uint32 OverrideManagerBase::GetGRFID(uint16 entity_id) const
|
2011-06-12 20:42:23 +00:00
|
|
|
{
|
|
|
|
return mapping_ID[entity_id].grfid;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Gives the substitute of the entity, as specified by the grf file
|
2007-05-15 21:36:58 +00:00
|
|
|
* @param entity_id of the entity being queried
|
|
|
|
* @return mapped id
|
|
|
|
*/
|
2010-08-11 18:50:23 +00:00
|
|
|
uint16 OverrideManagerBase::GetSubstituteID(uint16 entity_id) const
|
2007-05-15 21:36:58 +00:00
|
|
|
{
|
|
|
|
return mapping_ID[entity_id].substitute_id;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Install the specs into the HouseSpecs array
|
2013-01-08 22:46:42 +00:00
|
|
|
* It will find itself the proper slot on which it will go
|
2007-05-15 21:36:58 +00:00
|
|
|
* @param hs HouseSpec read from the grf file, ready for inclusion
|
|
|
|
*/
|
|
|
|
void HouseOverrideManager::SetEntitySpec(const HouseSpec *hs)
|
|
|
|
{
|
2010-08-07 21:03:38 +00:00
|
|
|
HouseID house_id = this->AddEntityID(hs->grf_prop.local_id, hs->grf_prop.grffile->grfid, hs->grf_prop.subst_id);
|
2007-05-15 21:36:58 +00:00
|
|
|
|
|
|
|
if (house_id == invalid_ID) {
|
|
|
|
grfmsg(1, "House.SetEntitySpec: Too many houses allocated. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-26 14:02:27 +00:00
|
|
|
MemCpyT(HouseSpec::Get(house_id), hs);
|
2007-05-15 21:36:58 +00:00
|
|
|
|
|
|
|
/* Now add the overrides. */
|
|
|
|
for (int i = 0; i != max_offset; i++) {
|
2009-06-26 14:02:27 +00:00
|
|
|
HouseSpec *overridden_hs = HouseSpec::Get(i);
|
2007-05-15 21:36:58 +00:00
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
if (entity_overrides[i] != hs->grf_prop.local_id || grfid_overrides[i] != hs->grf_prop.grffile->grfid) continue;
|
2007-05-15 21:36:58 +00:00
|
|
|
|
2010-08-07 21:03:38 +00:00
|
|
|
overridden_hs->grf_prop.override = house_id;
|
2007-05-15 21:36:58 +00:00
|
|
|
entity_overrides[i] = invalid_ID;
|
2007-11-12 20:40:58 +00:00
|
|
|
grfid_overrides[i] = 0;
|
2007-05-15 21:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-05 01:49:08 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Return the ID (if ever available) of a previously inserted entity.
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param grf_local_id ID of this entity within the grfID
|
2008-02-08 16:56:52 +00:00
|
|
|
* @param grfid ID of the grf file
|
|
|
|
* @return the ID of the candidate, of the Invalid flag item ID
|
|
|
|
*/
|
2010-08-11 18:50:23 +00:00
|
|
|
uint16 IndustryOverrideManager::GetID(uint8 grf_local_id, uint32 grfid) const
|
2008-02-08 16:56:52 +00:00
|
|
|
{
|
|
|
|
uint16 id = OverrideManagerBase::GetID(grf_local_id, grfid);
|
|
|
|
if (id != invalid_ID) return id;
|
|
|
|
|
|
|
|
/* No mapping found, try the overrides */
|
|
|
|
for (id = 0; id < max_offset; id++) {
|
|
|
|
if (entity_overrides[id] == grf_local_id && grfid_overrides[id] == grfid) return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return invalid_ID;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Method to find an entity ID and to mark it as reserved for the Industry to be included.
|
2007-06-09 02:05:51 +00:00
|
|
|
* @param grf_local_id ID used by the grf file for pre-installation work (equivalent of TTDPatch's setid
|
|
|
|
* @param grfid ID of the current grf file
|
|
|
|
* @param substitute_id industry from which data has been copied
|
|
|
|
* @return a free entity id (slotid) if ever one has been found, or Invalid_ID marker otherwise
|
|
|
|
*/
|
|
|
|
uint16 IndustryOverrideManager::AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id)
|
|
|
|
{
|
|
|
|
/* This entity hasn't been defined before, so give it an ID now. */
|
|
|
|
for (uint16 id = 0; id < max_new_entities; id++) {
|
2013-01-08 22:46:42 +00:00
|
|
|
/* Skip overridden industries */
|
2007-11-12 20:57:03 +00:00
|
|
|
if (id < max_offset && entity_overrides[id] != invalid_ID) continue;
|
|
|
|
|
2007-06-09 02:05:51 +00:00
|
|
|
/* Get the real live industry */
|
|
|
|
const IndustrySpec *inds = GetIndustrySpec(id);
|
|
|
|
|
|
|
|
/* This industry must be one that is not available(enabled), mostly because of climate.
|
2019-04-10 21:07:06 +00:00
|
|
|
* And it must not already be used by a grf (grffile == nullptr).
|
2013-01-08 22:46:42 +00:00
|
|
|
* So reserve this slot here, as it is the chosen one */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (!inds->enabled && inds->grf_prop.grffile == nullptr) {
|
2007-06-09 02:05:51 +00:00
|
|
|
EntityIDMapping *map = &mapping_ID[id];
|
|
|
|
|
|
|
|
if (map->entity_id == 0 && map->grfid == 0) {
|
|
|
|
/* winning slot, mark it as been used */
|
|
|
|
map->entity_id = grf_local_id;
|
|
|
|
map->grfid = grfid;
|
|
|
|
map->substitute_id = substitute_id;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return invalid_ID;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2013-01-08 22:46:42 +00:00
|
|
|
* Method to install the new industry data in its proper slot
|
|
|
|
* The slot assignment is internal of this method, since it requires
|
2007-06-09 02:05:51 +00:00
|
|
|
* checking what is available
|
|
|
|
* @param inds Industryspec that comes from the grf decoding process
|
|
|
|
*/
|
2007-10-04 00:59:52 +00:00
|
|
|
void IndustryOverrideManager::SetEntitySpec(IndustrySpec *inds)
|
2007-06-09 02:05:51 +00:00
|
|
|
{
|
2010-08-14 09:04:37 +00:00
|
|
|
/* First step : We need to find if this industry is already specified in the savegame data. */
|
2007-06-09 02:05:51 +00:00
|
|
|
IndustryType ind_id = this->GetID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid);
|
|
|
|
|
2007-11-12 20:57:03 +00:00
|
|
|
if (ind_id == invalid_ID) {
|
|
|
|
/* Not found.
|
2013-01-08 22:46:42 +00:00
|
|
|
* Or it has already been overridden, so you've lost your place old boy.
|
2007-11-12 20:57:03 +00:00
|
|
|
* Or it is a simple substitute.
|
|
|
|
* We need to find a free available slot */
|
|
|
|
ind_id = this->AddEntityID(inds->grf_prop.local_id, inds->grf_prop.grffile->grfid, inds->grf_prop.subst_id);
|
2013-01-08 22:46:42 +00:00
|
|
|
inds->grf_prop.override = invalid_ID; // make sure it will not be detected as overridden
|
2007-06-09 02:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ind_id == invalid_ID) {
|
|
|
|
grfmsg(1, "Industry.SetEntitySpec: Too many industries allocated. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-14 09:04:37 +00:00
|
|
|
/* Now that we know we can use the given id, copy the spec to its final destination... */
|
2019-10-04 19:26:44 +00:00
|
|
|
_industry_specs[ind_id] = *inds;
|
2010-08-14 09:04:37 +00:00
|
|
|
/* ... and mark it as usable*/
|
2007-06-09 02:05:51 +00:00
|
|
|
_industry_specs[ind_id].enabled = true;
|
|
|
|
}
|
|
|
|
|
2007-06-21 17:09:10 +00:00
|
|
|
void IndustryTileOverrideManager::SetEntitySpec(const IndustryTileSpec *its)
|
|
|
|
{
|
|
|
|
IndustryGfx indt_id = this->AddEntityID(its->grf_prop.local_id, its->grf_prop.grffile->grfid, its->grf_prop.subst_id);
|
|
|
|
|
|
|
|
if (indt_id == invalid_ID) {
|
|
|
|
grfmsg(1, "IndustryTile.SetEntitySpec: Too many industry tiles allocated. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&_industry_tile_specs[indt_id], its, sizeof(*its));
|
|
|
|
|
|
|
|
/* Now add the overrides. */
|
|
|
|
for (int i = 0; i < max_offset; i++) {
|
|
|
|
IndustryTileSpec *overridden_its = &_industry_tile_specs[i];
|
|
|
|
|
2007-11-12 20:40:58 +00:00
|
|
|
if (entity_overrides[i] != its->grf_prop.local_id || grfid_overrides[i] != its->grf_prop.grffile->grfid) continue;
|
2007-06-21 17:09:10 +00:00
|
|
|
|
|
|
|
overridden_its->grf_prop.override = indt_id;
|
|
|
|
overridden_its->enabled = false;
|
|
|
|
entity_overrides[i] = invalid_ID;
|
2007-11-12 20:40:58 +00:00
|
|
|
grfid_overrides[i] = 0;
|
2007-06-21 17:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 17:30:55 +00:00
|
|
|
/**
|
|
|
|
* Method to install the new object data in its proper slot
|
2013-01-08 22:46:42 +00:00
|
|
|
* The slot assignment is internal of this method, since it requires
|
2010-08-28 17:30:55 +00:00
|
|
|
* checking what is available
|
|
|
|
* @param spec ObjectSpec that comes from the grf decoding process
|
|
|
|
*/
|
|
|
|
void ObjectOverrideManager::SetEntitySpec(ObjectSpec *spec)
|
|
|
|
{
|
|
|
|
/* First step : We need to find if this object is already specified in the savegame data. */
|
|
|
|
ObjectType type = this->GetID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid);
|
|
|
|
|
|
|
|
if (type == invalid_ID) {
|
|
|
|
/* Not found.
|
2013-01-08 22:46:42 +00:00
|
|
|
* Or it has already been overridden, so you've lost your place old boy.
|
2010-08-28 17:30:55 +00:00
|
|
|
* Or it is a simple substitute.
|
|
|
|
* We need to find a free available slot */
|
|
|
|
type = this->AddEntityID(spec->grf_prop.local_id, spec->grf_prop.grffile->grfid, OBJECT_TRANSMITTER);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == invalid_ID) {
|
|
|
|
grfmsg(1, "Object.SetEntitySpec: Too many objects allocated. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern ObjectSpec _object_specs[NUM_OBJECTS];
|
|
|
|
|
|
|
|
/* Now that we know we can use the given id, copy the spec to its final destination. */
|
|
|
|
memcpy(&_object_specs[type], spec, sizeof(*spec));
|
2010-08-28 18:21:09 +00:00
|
|
|
ObjectClass::Assign(&_object_specs[type]);
|
2010-08-28 17:30:55 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Function used by houses (and soon industries) to get information
|
2007-06-05 01:49:08 +00:00
|
|
|
* on type of "terrain" the tile it is queries sits on.
|
|
|
|
* @param tile TileIndex of the tile been queried
|
2011-05-02 16:14:23 +00:00
|
|
|
* @param context The context of the tile.
|
2007-06-05 01:49:08 +00:00
|
|
|
* @return value corresponding to the grf expected format:
|
2010-08-01 19:44:49 +00:00
|
|
|
* Terrain type: 0 normal, 1 desert, 2 rainforest, 4 on or above snowline
|
|
|
|
*/
|
2010-08-09 07:10:42 +00:00
|
|
|
uint32 GetTerrainType(TileIndex tile, TileContext context)
|
2007-06-05 01:49:08 +00:00
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.game_creation.landscape) {
|
2008-01-23 17:45:48 +00:00
|
|
|
case LT_TROPIC: return GetTropicZone(tile);
|
2010-07-11 17:11:14 +00:00
|
|
|
case LT_ARCTIC: {
|
|
|
|
bool has_snow;
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_CLEAR:
|
2010-08-08 21:34:19 +00:00
|
|
|
/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
|
|
|
|
if (_generating_world) goto genworld;
|
2010-07-14 20:26:01 +00:00
|
|
|
has_snow = IsSnowTile(tile) && GetClearDensity(tile) >= 2;
|
2010-07-11 17:11:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_RAILWAY: {
|
2010-08-08 21:34:19 +00:00
|
|
|
/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
|
|
|
|
if (_generating_world) goto genworld; // we do not care about foundations here
|
2010-07-11 17:11:14 +00:00
|
|
|
RailGroundType ground = GetRailGroundType(tile);
|
2010-08-09 10:59:30 +00:00
|
|
|
has_snow = (ground == RAIL_GROUND_ICE_DESERT || (context == TCX_UPPER_HALFTILE && ground == RAIL_GROUND_HALF_SNOW));
|
2010-07-11 17:11:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MP_ROAD:
|
2010-08-08 21:34:19 +00:00
|
|
|
/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
|
|
|
|
if (_generating_world) goto genworld; // we do not care about foundations here
|
2010-07-11 17:11:14 +00:00
|
|
|
has_snow = IsOnSnow(tile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_TREES: {
|
2010-08-08 21:34:19 +00:00
|
|
|
/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
|
|
|
|
if (_generating_world) goto genworld;
|
2010-07-11 17:11:14 +00:00
|
|
|
TreeGround ground = GetTreeGround(tile);
|
2010-07-14 20:26:01 +00:00
|
|
|
has_snow = (ground == TREE_GROUND_SNOW_DESERT || ground == TREE_GROUND_ROUGH_SNOW) && GetTreeDensity(tile) >= 2;
|
2010-07-11 17:11:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MP_TUNNELBRIDGE:
|
2010-08-09 10:59:30 +00:00
|
|
|
if (context == TCX_ON_BRIDGE) {
|
2011-11-04 10:25:58 +00:00
|
|
|
has_snow = (GetBridgeHeight(tile) > GetSnowLine());
|
2010-08-09 07:32:04 +00:00
|
|
|
} else {
|
|
|
|
/* During map generation the snowstate may not be valid yet, as the tileloop may not have run yet. */
|
|
|
|
if (_generating_world) goto genworld; // we do not care about foundations here
|
|
|
|
has_snow = HasTunnelBridgeSnowOrDesert(tile);
|
|
|
|
}
|
2010-07-11 17:11:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STATION:
|
|
|
|
case MP_HOUSE:
|
|
|
|
case MP_INDUSTRY:
|
2010-08-08 10:59:30 +00:00
|
|
|
case MP_OBJECT:
|
2010-07-11 17:11:14 +00:00
|
|
|
/* These tiles usually have a levelling foundation. So use max Z */
|
2011-11-04 10:25:58 +00:00
|
|
|
has_snow = (GetTileMaxZ(tile) > GetSnowLine());
|
2010-07-11 17:11:14 +00:00
|
|
|
break;
|
|
|
|
|
2010-07-19 23:51:22 +00:00
|
|
|
case MP_VOID:
|
2010-07-11 17:11:14 +00:00
|
|
|
case MP_WATER:
|
2010-08-08 21:34:19 +00:00
|
|
|
genworld:
|
2011-11-04 10:25:58 +00:00
|
|
|
has_snow = (GetTileZ(tile) > GetSnowLine());
|
2010-07-11 17:11:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
return has_snow ? 4 : 0;
|
|
|
|
}
|
2007-06-05 01:49:08 +00:00
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Get the tile at the given offset.
|
|
|
|
* @param parameter The NewGRF "encoded" offset.
|
|
|
|
* @param tile The tile to base the offset from.
|
|
|
|
* @param signed_offsets Whether the offsets are to be interpreted as signed or not.
|
2011-07-11 16:32:35 +00:00
|
|
|
* @param axis Axis of a railways station.
|
2011-05-02 16:14:23 +00:00
|
|
|
* @return The tile at the offset.
|
|
|
|
*/
|
2011-07-11 16:32:35 +00:00
|
|
|
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets, Axis axis)
|
2007-06-08 17:54:18 +00:00
|
|
|
{
|
|
|
|
int8 x = GB(parameter, 0, 4);
|
|
|
|
int8 y = GB(parameter, 4, 4);
|
|
|
|
|
2010-10-19 21:00:45 +00:00
|
|
|
if (signed_offsets && x >= 8) x -= 16;
|
|
|
|
if (signed_offsets && y >= 8) y -= 16;
|
2007-06-08 17:54:18 +00:00
|
|
|
|
2007-12-21 22:11:55 +00:00
|
|
|
/* Swap width and height depending on axis for railway stations */
|
2011-07-11 16:32:35 +00:00
|
|
|
if (axis == INVALID_AXIS && HasStationTileRail(tile)) axis = GetRailStationAxis(tile);
|
|
|
|
if (axis == AXIS_Y) Swap(x, y);
|
2007-12-21 22:11:55 +00:00
|
|
|
|
2009-02-25 21:29:50 +00:00
|
|
|
/* Make sure we never roam outside of the map, better wrap in that case */
|
2007-07-12 08:10:40 +00:00
|
|
|
return TILE_MASK(tile + TileDiffXY(x, y));
|
2007-06-08 17:54:18 +00:00
|
|
|
}
|
2008-01-31 17:46:08 +00:00
|
|
|
|
|
|
|
/**
|
2009-10-02 15:13:15 +00:00
|
|
|
* Common part of station var 0x67, house var 0x62, indtile var 0x60, industry var 0x62.
|
2008-01-31 17:46:08 +00:00
|
|
|
*
|
|
|
|
* @param tile the tile of interest.
|
2011-11-08 17:29:01 +00:00
|
|
|
* @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
|
2008-01-31 17:46:08 +00:00
|
|
|
* @return 0czzbbss: c = TileType; zz = TileZ; bb: 7-3 zero, 4-2 TerrainType, 1 water/shore, 0 zero; ss = TileSlope
|
|
|
|
*/
|
2011-11-08 17:29:01 +00:00
|
|
|
uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8)
|
2008-01-31 17:46:08 +00:00
|
|
|
{
|
|
|
|
TileType tile_type = GetTileType(tile);
|
|
|
|
|
2008-01-31 17:54:13 +00:00
|
|
|
/* Fake tile type for trees on shore */
|
|
|
|
if (IsTileType(tile, MP_TREES) && GetTreeGround(tile) == TREE_GROUND_SHORE) tile_type = MP_WATER;
|
|
|
|
|
2011-11-04 11:30:37 +00:00
|
|
|
int z;
|
2011-11-04 10:18:13 +00:00
|
|
|
Slope tileh = GetTilePixelSlope(tile, &z);
|
2011-07-21 17:17:48 +00:00
|
|
|
/* Return 0 if the tile is a land tile */
|
|
|
|
byte terrain_type = (HasTileWaterClass(tile) ? (GetWaterClass(tile) + 1) & 3 : 0) << 5 | GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
|
2011-11-08 17:29:01 +00:00
|
|
|
if (grf_version8) z /= TILE_HEIGHT;
|
|
|
|
return tile_type << 24 | Clamp(z, 0, 0xFF) << 16 | terrain_type << 8 | tileh;
|
2008-01-31 17:46:08 +00:00
|
|
|
}
|
2011-05-14 17:25:45 +00:00
|
|
|
|
2011-08-27 08:42:24 +00:00
|
|
|
/**
|
|
|
|
* Returns company information like in vehicle var 43 or station var 43.
|
|
|
|
* @param owner Owner of the object.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param l Livery of the object; nullptr to use default.
|
2011-08-27 08:42:24 +00:00
|
|
|
* @return NewGRF company information.
|
|
|
|
*/
|
|
|
|
uint32 GetCompanyInfo(CompanyID owner, const Livery *l)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (l == nullptr && Company::IsValidID(owner)) l = &Company::Get(owner)->livery[LS_DEFAULT];
|
|
|
|
return owner | (Company::IsValidAiID(owner) ? 0x10000 : 0) | (l != nullptr ? (l->colour1 << 24) | (l->colour2 << 28) : 0);
|
2011-08-27 08:42:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-11 16:32:19 +00:00
|
|
|
/**
|
|
|
|
* Get the error message from a shape/location/slope check callback result.
|
|
|
|
* @param cb_res Callback result to translate. If bit 10 is set this is a standard error message, otherwise a NewGRF provided string.
|
2014-01-12 18:00:19 +00:00
|
|
|
* @param grffile NewGRF to use to resolve a custom error message.
|
2011-07-11 16:32:19 +00:00
|
|
|
* @param default_error Error message to use for the generic error.
|
|
|
|
* @return CommandCost indicating success or the error message.
|
|
|
|
*/
|
2014-01-12 18:00:19 +00:00
|
|
|
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error)
|
2011-07-11 16:32:19 +00:00
|
|
|
{
|
|
|
|
CommandCost res;
|
2011-11-08 17:24:43 +00:00
|
|
|
|
|
|
|
if (cb_res < 0x400) {
|
2014-01-12 18:00:19 +00:00
|
|
|
res = CommandCost(GetGRFStringID(grffile->grfid, 0xD000 + cb_res));
|
2011-11-08 17:24:43 +00:00
|
|
|
} else {
|
|
|
|
switch (cb_res) {
|
|
|
|
case 0x400: return res; // No error.
|
|
|
|
|
|
|
|
default: // unknown reason -> default error
|
|
|
|
case 0x401: res = CommandCost(default_error); break;
|
|
|
|
|
|
|
|
case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
|
|
|
|
case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
|
|
|
|
case 0x404: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_ABOVE_SNOW_LINE); break;
|
|
|
|
case 0x405: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_BELOW_SNOW_LINE); break;
|
|
|
|
case 0x406: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_SEA); break;
|
|
|
|
case 0x407: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_CANAL); break;
|
|
|
|
case 0x408: res = CommandCost(STR_ERROR_CAN_T_BUILD_ON_RIVER); break;
|
|
|
|
}
|
2011-07-11 16:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy some parameters from the registers to the error message text ref. stack */
|
2014-01-12 18:00:39 +00:00
|
|
|
res.UseTextRefStack(grffile, 4);
|
2011-07-11 16:32:19 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-11-08 17:24:15 +00:00
|
|
|
/**
|
|
|
|
* Record that a NewGRF returned an unknown/invalid callback result.
|
|
|
|
* Also show an error to the user.
|
|
|
|
* @param grfid ID of the NewGRF causing the problem.
|
|
|
|
* @param cbid Callback causing the problem.
|
|
|
|
* @param cb_res Invalid result returned by the callback.
|
|
|
|
*/
|
|
|
|
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res)
|
|
|
|
{
|
|
|
|
GRFConfig *grfconfig = GetGRFConfig(grfid);
|
|
|
|
|
|
|
|
if (!HasBit(grfconfig->grf_bugs, GBUG_UNKNOWN_CB_RESULT)) {
|
|
|
|
SetBit(grfconfig->grf_bugs, GBUG_UNKNOWN_CB_RESULT);
|
|
|
|
SetDParamStr(0, grfconfig->GetName());
|
|
|
|
SetDParam(1, cbid);
|
|
|
|
SetDParam(2, cb_res);
|
|
|
|
ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT, WL_CRITICAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* debug output */
|
|
|
|
char buffer[512];
|
|
|
|
|
|
|
|
SetDParamStr(0, grfconfig->GetName());
|
|
|
|
GetString(buffer, STR_NEWGRF_BUGGY, lastof(buffer));
|
|
|
|
DEBUG(grf, 0, "%s", buffer + 3);
|
|
|
|
|
|
|
|
SetDParam(1, cbid);
|
|
|
|
SetDParam(2, cb_res);
|
|
|
|
GetString(buffer, STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT, lastof(buffer));
|
|
|
|
DEBUG(grf, 0, "%s", buffer + 3);
|
|
|
|
}
|
|
|
|
|
2011-11-08 17:26:49 +00:00
|
|
|
/**
|
|
|
|
* Converts a callback result into a boolean.
|
|
|
|
* For grf version < 8 the result is checked for zero or non-zero.
|
|
|
|
* For grf version >= 8 the callback result must be 0 or 1.
|
|
|
|
* @param grffile NewGRF returning the value.
|
|
|
|
* @param cbid Callback returning the value.
|
|
|
|
* @param cb_res Callback result.
|
|
|
|
* @return Boolean value. True if cb_res != 0.
|
|
|
|
*/
|
|
|
|
bool ConvertBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
|
|
|
|
{
|
|
|
|
assert(cb_res != CALLBACK_FAILED); // We do not know what to return
|
|
|
|
|
|
|
|
if (grffile->grf_version < 8) return cb_res != 0;
|
|
|
|
|
|
|
|
if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
|
|
|
|
return cb_res != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a callback result into a boolean.
|
|
|
|
* For grf version < 8 the first 8 bit of the result are checked for zero or non-zero.
|
|
|
|
* For grf version >= 8 the callback result must be 0 or 1.
|
|
|
|
* @param grffile NewGRF returning the value.
|
|
|
|
* @param cbid Callback returning the value.
|
|
|
|
* @param cb_res Callback result.
|
|
|
|
* @return Boolean value. True if cb_res != 0.
|
|
|
|
*/
|
|
|
|
bool Convert8bitBooleanCallback(const GRFFile *grffile, uint16 cbid, uint16 cb_res)
|
|
|
|
{
|
|
|
|
assert(cb_res != CALLBACK_FAILED); // We do not know what to return
|
|
|
|
|
|
|
|
if (grffile->grf_version < 8) return GB(cb_res, 0, 8) != 0;
|
|
|
|
|
|
|
|
if (cb_res > 1) ErrorUnknownCallbackResult(grffile->grfid, cbid, cb_res);
|
|
|
|
return cb_res != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-03 17:30:09 +00:00
|
|
|
/* static */ std::vector<DrawTileSeqStruct> NewGRFSpriteLayout::result_seq;
|
2011-05-29 16:56:22 +00:00
|
|
|
|
2011-05-14 17:25:45 +00:00
|
|
|
/**
|
|
|
|
* Clone the building sprites of a spritelayout.
|
|
|
|
* @param source The building sprites to copy.
|
|
|
|
*/
|
|
|
|
void NewGRFSpriteLayout::Clone(const DrawTileSeqStruct *source)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(this->seq == nullptr);
|
|
|
|
assert(source != nullptr);
|
2011-05-14 17:25:45 +00:00
|
|
|
|
|
|
|
size_t count = 1; // 1 for the terminator
|
|
|
|
const DrawTileSeqStruct *element;
|
|
|
|
foreach_draw_tile_seq(element, source) count++;
|
|
|
|
|
|
|
|
DrawTileSeqStruct *sprites = MallocT<DrawTileSeqStruct>(count);
|
|
|
|
MemCpyT(sprites, source, count);
|
|
|
|
this->seq = sprites;
|
|
|
|
}
|
|
|
|
|
2011-05-29 16:56:22 +00:00
|
|
|
/**
|
|
|
|
* Clone a spritelayout.
|
|
|
|
* @param source The spritelayout to copy.
|
|
|
|
*/
|
|
|
|
void NewGRFSpriteLayout::Clone(const NewGRFSpriteLayout *source)
|
|
|
|
{
|
|
|
|
this->Clone((const DrawTileSprites*)source);
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (source->registers != nullptr) {
|
2011-05-29 16:56:22 +00:00
|
|
|
size_t count = 1; // 1 for the ground sprite
|
|
|
|
const DrawTileSeqStruct *element;
|
|
|
|
foreach_draw_tile_seq(element, source->seq) count++;
|
|
|
|
|
|
|
|
TileLayoutRegisters *regs = MallocT<TileLayoutRegisters>(count);
|
|
|
|
MemCpyT(regs, source->registers, count);
|
|
|
|
this->registers = regs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 17:25:45 +00:00
|
|
|
/**
|
|
|
|
* Allocate a spritelayout for \a num_sprites building sprites.
|
|
|
|
* @param num_sprites Number of building sprites to allocate memory for. (not counting the terminator)
|
|
|
|
*/
|
|
|
|
void NewGRFSpriteLayout::Allocate(uint num_sprites)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(this->seq == nullptr);
|
2011-05-14 17:25:45 +00:00
|
|
|
|
|
|
|
DrawTileSeqStruct *sprites = CallocT<DrawTileSeqStruct>(num_sprites + 1);
|
|
|
|
sprites[num_sprites].MakeTerminator();
|
|
|
|
this->seq = sprites;
|
|
|
|
}
|
2011-05-29 16:56:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate memory for register modifiers.
|
|
|
|
*/
|
|
|
|
void NewGRFSpriteLayout::AllocateRegisters()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
assert(this->seq != nullptr);
|
|
|
|
assert(this->registers == nullptr);
|
2011-05-29 16:56:22 +00:00
|
|
|
|
|
|
|
size_t count = 1; // 1 for the ground sprite
|
|
|
|
const DrawTileSeqStruct *element;
|
|
|
|
foreach_draw_tile_seq(element, this->seq) count++;
|
|
|
|
|
|
|
|
this->registers = CallocT<TileLayoutRegisters>(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a sprite layout before resolving action-1-2-3 chains.
|
|
|
|
* Integrates offsets into the layout and determines which chains to resolve.
|
2019-09-29 20:27:32 +00:00
|
|
|
* @note The function uses statically allocated temporary storage, which is reused every time when calling the function.
|
2011-05-29 16:56:22 +00:00
|
|
|
* That means, you have to use the sprite layout before calling #PrepareLayout() the next time.
|
|
|
|
* @param orig_offset Offset to apply to non-action-1 sprites.
|
|
|
|
* @param newgrf_ground_offset Offset to apply to action-1 ground sprites.
|
|
|
|
* @param newgrf_offset Offset to apply to action-1 non-ground sprites.
|
2011-09-11 15:10:09 +00:00
|
|
|
* @param constr_stage Construction stage (0-3) to apply to all action-1 sprites.
|
2011-05-29 16:56:22 +00:00
|
|
|
* @param separate_ground Whether the ground sprite shall be resolved by a separate action-1-2-3 chain by default.
|
|
|
|
* @return Bitmask of values for variable 10 to resolve action-1-2-3 chains for.
|
|
|
|
*/
|
2011-09-11 15:10:09 +00:00
|
|
|
uint32 NewGRFSpriteLayout::PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const
|
2011-05-29 16:56:22 +00:00
|
|
|
{
|
2018-09-20 22:44:14 +00:00
|
|
|
result_seq.clear();
|
2011-05-29 16:56:22 +00:00
|
|
|
uint32 var10_values = 0;
|
|
|
|
|
|
|
|
/* Create a copy of the spritelayout, so we can modify some values.
|
|
|
|
* Also include the groundsprite into the sequence for easier processing. */
|
2020-12-14 23:22:04 +00:00
|
|
|
DrawTileSeqStruct *result = &result_seq.emplace_back();
|
2011-05-29 16:56:22 +00:00
|
|
|
result->image = ground;
|
|
|
|
result->delta_x = 0;
|
|
|
|
result->delta_y = 0;
|
2011-06-13 08:27:12 +00:00
|
|
|
result->delta_z = (int8)0x80;
|
2011-05-29 16:56:22 +00:00
|
|
|
|
|
|
|
const DrawTileSeqStruct *dtss;
|
|
|
|
foreach_draw_tile_seq(dtss, this->seq) {
|
2019-02-18 22:39:06 +00:00
|
|
|
result_seq.push_back(*dtss);
|
2011-05-29 16:56:22 +00:00
|
|
|
}
|
2020-12-14 23:22:04 +00:00
|
|
|
result_seq.emplace_back().MakeTerminator();
|
2011-05-29 16:56:22 +00:00
|
|
|
/* Determine the var10 values the action-1-2-3 chains needs to be resolved for,
|
|
|
|
* and apply the default sprite offsets (unless disabled). */
|
|
|
|
const TileLayoutRegisters *regs = this->registers;
|
|
|
|
bool ground = true;
|
2019-02-17 11:20:52 +00:00
|
|
|
foreach_draw_tile_seq(result, result_seq.data()) {
|
2011-05-29 16:56:22 +00:00
|
|
|
TileLayoutFlags flags = TLF_NOTHING;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (regs != nullptr) flags = regs->flags;
|
2011-05-29 16:56:22 +00:00
|
|
|
|
|
|
|
/* Record var10 value for the sprite */
|
|
|
|
if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_SPRITE_REG_FLAGS)) {
|
|
|
|
uint8 var10 = (flags & TLF_SPRITE_VAR10) ? regs->sprite_var10 : (ground && separate_ground ? 1 : 0);
|
|
|
|
SetBit(var10_values, var10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add default sprite offset, unless there is a custom one */
|
|
|
|
if (!(flags & TLF_SPRITE)) {
|
|
|
|
if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
|
|
|
|
result->image.sprite += ground ? newgrf_ground_offset : newgrf_offset;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (constr_stage > 0 && regs != nullptr) result->image.sprite += GetConstructionStageOffset(constr_stage, regs->max_sprite_offset);
|
2011-05-29 16:56:22 +00:00
|
|
|
} else {
|
|
|
|
result->image.sprite += orig_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Record var10 value for the palette */
|
|
|
|
if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_PALETTE_REG_FLAGS)) {
|
|
|
|
uint8 var10 = (flags & TLF_PALETTE_VAR10) ? regs->palette_var10 : (ground && separate_ground ? 1 : 0);
|
|
|
|
SetBit(var10_values, var10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add default palette offset, unless there is a custom one */
|
|
|
|
if (!(flags & TLF_PALETTE)) {
|
|
|
|
if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
|
|
|
|
result->image.sprite += ground ? newgrf_ground_offset : newgrf_offset;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (constr_stage > 0 && regs != nullptr) result->image.sprite += GetConstructionStageOffset(constr_stage, regs->max_palette_offset);
|
2011-05-29 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ground = false;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (regs != nullptr) regs++;
|
2011-05-29 16:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return var10_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the register modifiers and integrates them into the preprocessed sprite layout.
|
|
|
|
* @pre #PrepareLayout() needs calling first.
|
|
|
|
* @param resolved_var10 The value of var10 the action-1-2-3 chain was evaluated for.
|
|
|
|
* @param resolved_sprite Result sprite of the action-1-2-3 chain.
|
|
|
|
* @param separate_ground Whether the ground sprite is resolved by a separate action-1-2-3 chain.
|
|
|
|
* @return Resulting spritelayout after processing the registers.
|
|
|
|
*/
|
|
|
|
void NewGRFSpriteLayout::ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const
|
|
|
|
{
|
|
|
|
DrawTileSeqStruct *result;
|
|
|
|
const TileLayoutRegisters *regs = this->registers;
|
|
|
|
bool ground = true;
|
2019-02-17 11:20:52 +00:00
|
|
|
foreach_draw_tile_seq(result, result_seq.data()) {
|
2011-05-29 16:56:22 +00:00
|
|
|
TileLayoutFlags flags = TLF_NOTHING;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (regs != nullptr) flags = regs->flags;
|
2011-05-29 16:56:22 +00:00
|
|
|
|
|
|
|
/* Is the sprite or bounding box affected by an action-1-2-3 chain? */
|
|
|
|
if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_SPRITE_REG_FLAGS)) {
|
|
|
|
/* Does the var10 value apply to this sprite? */
|
|
|
|
uint8 var10 = (flags & TLF_SPRITE_VAR10) ? regs->sprite_var10 : (ground && separate_ground ? 1 : 0);
|
|
|
|
if (var10 == resolved_var10) {
|
|
|
|
/* Apply registers */
|
|
|
|
if ((flags & TLF_DODRAW) && GetRegister(regs->dodraw) == 0) {
|
|
|
|
result->image.sprite = 0;
|
|
|
|
} else {
|
2011-08-06 10:47:37 +00:00
|
|
|
if (HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE)) result->image.sprite += resolved_sprite;
|
2011-09-11 15:10:09 +00:00
|
|
|
if (flags & TLF_SPRITE) {
|
|
|
|
int16 offset = (int16)GetRegister(regs->sprite); // mask to 16 bits to avoid trouble
|
|
|
|
if (!HasBit(result->image.sprite, SPRITE_MODIFIER_CUSTOM_SPRITE) || (offset >= 0 && offset < regs->max_sprite_offset)) {
|
|
|
|
result->image.sprite += offset;
|
|
|
|
} else {
|
|
|
|
result->image.sprite = SPR_IMG_QUERY;
|
|
|
|
}
|
|
|
|
}
|
2011-08-06 10:47:37 +00:00
|
|
|
|
|
|
|
if (result->IsParentSprite()) {
|
|
|
|
if (flags & TLF_BB_XY_OFFSET) {
|
|
|
|
result->delta_x += (int32)GetRegister(regs->delta.parent[0]);
|
|
|
|
result->delta_y += (int32)GetRegister(regs->delta.parent[1]);
|
|
|
|
}
|
|
|
|
if (flags & TLF_BB_Z_OFFSET) result->delta_z += (int32)GetRegister(regs->delta.parent[2]);
|
|
|
|
} else {
|
|
|
|
if (flags & TLF_CHILD_X_OFFSET) result->delta_x += (int32)GetRegister(regs->delta.child[0]);
|
|
|
|
if (flags & TLF_CHILD_Y_OFFSET) result->delta_y += (int32)GetRegister(regs->delta.child[1]);
|
|
|
|
}
|
2011-05-29 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the palette affected by an action-1-2-3 chain? */
|
2011-08-06 10:47:37 +00:00
|
|
|
if (result->image.sprite != 0 && (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (flags & TLF_PALETTE_REG_FLAGS))) {
|
2011-05-29 16:56:22 +00:00
|
|
|
/* Does the var10 value apply to this sprite? */
|
|
|
|
uint8 var10 = (flags & TLF_PALETTE_VAR10) ? regs->palette_var10 : (ground && separate_ground ? 1 : 0);
|
|
|
|
if (var10 == resolved_var10) {
|
|
|
|
/* Apply registers */
|
|
|
|
if (HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) result->image.pal += resolved_sprite;
|
2011-09-11 15:10:09 +00:00
|
|
|
if (flags & TLF_PALETTE) {
|
|
|
|
int16 offset = (int16)GetRegister(regs->palette); // mask to 16 bits to avoid trouble
|
|
|
|
if (!HasBit(result->image.pal, SPRITE_MODIFIER_CUSTOM_SPRITE) || (offset >= 0 && offset < regs->max_palette_offset)) {
|
|
|
|
result->image.pal += offset;
|
|
|
|
} else {
|
|
|
|
result->image.sprite = SPR_IMG_QUERY;
|
|
|
|
result->image.pal = PAL_NONE;
|
|
|
|
}
|
|
|
|
}
|
2011-05-29 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ground = false;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (regs != nullptr) regs++;
|
2011-05-29 16:56:22 +00:00
|
|
|
}
|
|
|
|
}
|