2010-08-27 22:21:23 +00:00
|
|
|
/* $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_object.cpp Handling of object NewGRFs. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2010-08-28 18:49:39 +00:00
|
|
|
#include "company_base.h"
|
|
|
|
#include "company_func.h"
|
|
|
|
#include "debug.h"
|
2013-10-17 21:37:25 +00:00
|
|
|
#include "genworld.h"
|
2010-08-28 17:32:30 +00:00
|
|
|
#include "newgrf_class_func.h"
|
2010-08-27 22:21:23 +00:00
|
|
|
#include "newgrf_object.h"
|
2010-08-28 18:51:47 +00:00
|
|
|
#include "newgrf_sound.h"
|
2010-08-28 18:49:39 +00:00
|
|
|
#include "object_base.h"
|
2010-08-27 22:21:23 +00:00
|
|
|
#include "object_map.h"
|
2012-01-03 20:26:05 +00:00
|
|
|
#include "tile_cmd.h"
|
2010-08-28 18:49:39 +00:00
|
|
|
#include "town.h"
|
|
|
|
#include "water.h"
|
2010-08-28 18:51:47 +00:00
|
|
|
#include "newgrf_animation_base.h"
|
2010-08-27 22:21:23 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-08-28 17:30:55 +00:00
|
|
|
/** The override manager for our objects. */
|
|
|
|
ObjectOverrideManager _object_mngr(NEW_OBJECT_OFFSET, NUM_OBJECTS, INVALID_OBJECT_TYPE);
|
|
|
|
|
2010-08-28 17:29:12 +00:00
|
|
|
extern const ObjectSpec _original_objects[NEW_OBJECT_OFFSET];
|
|
|
|
/** All the object specifications. */
|
2010-08-28 17:30:55 +00:00
|
|
|
ObjectSpec _object_specs[NUM_OBJECTS];
|
2010-08-27 22:21:23 +00:00
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Get the specification associated with a specific ObjectType.
|
|
|
|
* @param index The object type to fetch.
|
|
|
|
* @return The specification.
|
|
|
|
*/
|
2010-08-27 22:21:23 +00:00
|
|
|
/* static */ const ObjectSpec *ObjectSpec::Get(ObjectType index)
|
|
|
|
{
|
2010-08-28 17:29:12 +00:00
|
|
|
assert(index < NUM_OBJECTS);
|
|
|
|
return &_object_specs[index];
|
2010-08-27 22:21:23 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Get the specification associated with a tile.
|
|
|
|
* @param tile The tile to fetch the data for.
|
|
|
|
* @return The specification.
|
|
|
|
*/
|
2010-08-27 22:21:23 +00:00
|
|
|
/* static */ const ObjectSpec *ObjectSpec::GetByTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
return ObjectSpec::Get(GetObjectType(tile));
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:27:45 +00:00
|
|
|
/**
|
|
|
|
* Check whether the object might be available at some point in this game with the current game mode.
|
|
|
|
* @return true if it might be available.
|
|
|
|
*/
|
|
|
|
bool ObjectSpec::IsEverAvailable() const
|
|
|
|
{
|
|
|
|
return this->enabled && HasBit(this->climate, _settings_game.game_creation.landscape) &&
|
2013-10-17 21:37:25 +00:00
|
|
|
(this->flags & ((_game_mode != GM_EDITOR && !_generating_world) ? OBJECT_FLAG_ONLY_IN_SCENEDIT : OBJECT_FLAG_ONLY_IN_GAME)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the object was available at some point in the past or present in this game with the current game mode.
|
|
|
|
* @return true if it was ever or is available.
|
|
|
|
*/
|
|
|
|
bool ObjectSpec::WasEverAvailable() const
|
|
|
|
{
|
|
|
|
return this->IsEverAvailable() && _date > this->introduction_date;
|
2012-04-22 16:27:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Check whether the object is available at this time.
|
|
|
|
* @return true if it is available.
|
|
|
|
*/
|
2010-08-28 17:36:28 +00:00
|
|
|
bool ObjectSpec::IsAvailable() const
|
|
|
|
{
|
2013-10-17 21:37:25 +00:00
|
|
|
return this->WasEverAvailable() &&
|
2012-04-22 16:27:45 +00:00
|
|
|
(_date < this->end_of_life_date || this->end_of_life_date < this->introduction_date + 365);
|
2010-08-28 17:36:28 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the index of this spec.
|
|
|
|
* @return The index.
|
|
|
|
*/
|
2010-08-28 17:38:07 +00:00
|
|
|
uint ObjectSpec::Index() const
|
|
|
|
{
|
|
|
|
return this - _object_specs;
|
|
|
|
}
|
|
|
|
|
2010-08-28 17:29:12 +00:00
|
|
|
/** This function initialize the spec arrays of objects. */
|
|
|
|
void ResetObjects()
|
|
|
|
{
|
|
|
|
/* Clean the pool. */
|
2018-05-13 17:34:57 +00:00
|
|
|
for (uint16 i = 0; i < NUM_OBJECTS; i++) {
|
|
|
|
_object_specs[i] = {};
|
|
|
|
}
|
2010-08-28 17:29:12 +00:00
|
|
|
|
|
|
|
/* And add our originals. */
|
|
|
|
MemCpyT(_object_specs, _original_objects, lengthof(_original_objects));
|
2010-08-28 19:45:56 +00:00
|
|
|
|
|
|
|
for (uint16 i = 0; i < lengthof(_original_objects); i++) {
|
|
|
|
_object_specs[i].grf_prop.local_id = i;
|
|
|
|
}
|
2010-08-28 17:29:12 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 17:32:30 +00:00
|
|
|
template <typename Tspec, typename Tid, Tid Tmax>
|
|
|
|
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
|
|
|
|
{
|
|
|
|
ObjectClassID cls = ObjectClass::Allocate('LTHS');
|
2012-04-22 16:27:55 +00:00
|
|
|
ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS;
|
2010-08-28 17:32:30 +00:00
|
|
|
_object_specs[OBJECT_LIGHTHOUSE].cls_id = cls;
|
|
|
|
ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]);
|
|
|
|
|
|
|
|
cls = ObjectClass::Allocate('TRNS');
|
2012-04-22 16:27:55 +00:00
|
|
|
ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS;
|
2010-08-28 17:32:30 +00:00
|
|
|
_object_specs[OBJECT_TRANSMITTER].cls_id = cls;
|
|
|
|
ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]);
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:28:27 +00:00
|
|
|
template <typename Tspec, typename Tid, Tid Tmax>
|
|
|
|
bool NewGRFClass<Tspec, Tid, Tmax>::IsUIAvailable(uint index) const
|
|
|
|
{
|
|
|
|
return this->GetSpec(index)->IsEverAvailable();
|
|
|
|
}
|
|
|
|
|
2010-08-28 17:32:30 +00:00
|
|
|
INSTANTIATE_NEWGRF_CLASS_METHODS(ObjectClass, ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX)
|
2010-08-28 18:49:39 +00:00
|
|
|
|
2012-11-10 20:40:46 +00:00
|
|
|
/* virtual */ uint32 ObjectScopeResolver::GetRandomBits() const
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
2012-11-10 20:40:46 +00:00
|
|
|
return IsValidTile(this->tile) && IsTileType(this->tile, MP_OBJECT) ? GetObjectRandomBits(this->tile) : 0;
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-26 21:51:24 +00:00
|
|
|
* Make an analysis of a tile and get the object type.
|
2010-08-28 18:49:39 +00:00
|
|
|
* @param tile TileIndex of the tile to query
|
|
|
|
* @param cur_grfid GRFID of the current callback chain
|
|
|
|
* @return value encoded as per NFO specs
|
|
|
|
*/
|
2010-10-26 21:51:24 +00:00
|
|
|
static uint32 GetObjectIDAtOffset(TileIndex tile, uint32 cur_grfid)
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
2010-10-26 21:51:24 +00:00
|
|
|
if (!IsTileType(tile, MP_OBJECT)) {
|
2010-08-28 18:49:39 +00:00
|
|
|
return 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2014-02-07 22:55:33 +00:00
|
|
|
const Object *o = Object::GetByTile(tile);
|
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(o->type);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
2011-08-18 14:08:51 +00:00
|
|
|
/* Default objects have no associated NewGRF file */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec->grf_prop.grffile == nullptr) {
|
2011-08-18 14:08:51 +00:00
|
|
|
return 0xFFFE; // Defined in another grf file
|
|
|
|
}
|
|
|
|
|
2010-08-28 18:49:39 +00:00
|
|
|
if (spec->grf_prop.grffile->grfid == cur_grfid) { // same object, same grf ?
|
2014-02-07 22:55:33 +00:00
|
|
|
return spec->grf_prop.local_id | o->view << 16;
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0xFFFE; // Defined in another grf file
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on newhouses equivalent, but adapted for newobjects
|
|
|
|
* @param parameter from callback. It's in fact a pair of coordinates
|
|
|
|
* @param tile TileIndex from which the callback was initiated
|
|
|
|
* @param index of the object been queried for
|
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.
|
2010-08-28 18:49:39 +00:00
|
|
|
* @return a construction of bits obeying the newgrf format
|
|
|
|
*/
|
2011-11-08 17:29:01 +00:00
|
|
|
static uint32 GetNearbyObjectTileInformation(byte parameter, TileIndex tile, ObjectID index, bool grf_version8)
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
|
|
|
if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
|
|
|
|
bool is_same_object = (IsTileType(tile, MP_OBJECT) && GetObjectIndex(tile) == index);
|
|
|
|
|
2011-11-08 17:29:01 +00:00
|
|
|
return GetNearbyTileInformation(tile, grf_version8) | (is_same_object ? 1 : 0) << 8;
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the closest object of a given type.
|
|
|
|
* @param tile The tile to start searching from.
|
|
|
|
* @param type The type of the object to search for.
|
|
|
|
* @param current The current object (to ignore).
|
|
|
|
* @return The distance to the closest object.
|
|
|
|
*/
|
|
|
|
static uint32 GetClosestObject(TileIndex tile, ObjectType type, const Object *current)
|
|
|
|
{
|
|
|
|
uint32 best_dist = UINT32_MAX;
|
|
|
|
const Object *o;
|
|
|
|
FOR_ALL_OBJECTS(o) {
|
2013-10-12 16:30:42 +00:00
|
|
|
if (o->type != type || o == current) continue;
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
best_dist = min(best_dist, DistanceManhattan(tile, o->location.tile));
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of var 65
|
|
|
|
* @param local_id Parameter given to the callback, which is the set id, or the local id, in our terminology.
|
|
|
|
* @param grfid The object's GRFID.
|
|
|
|
* @param tile The tile to look from.
|
|
|
|
* @param current Object for which the inquiry is made
|
|
|
|
* @return The formatted answer to the callback : rr(reserved) cc(count) dddd(manhattan distance of closest sister)
|
|
|
|
*/
|
|
|
|
static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid, TileIndex tile, const Object *current)
|
|
|
|
{
|
|
|
|
uint32 grf_id = GetRegister(0x100); // Get the GRFID of the definition to look for in register 100h
|
|
|
|
uint32 idx;
|
|
|
|
|
|
|
|
/* Determine what will be the object type to look for */
|
|
|
|
switch (grf_id) {
|
|
|
|
case 0: // this is a default object type
|
|
|
|
idx = local_id;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xFFFFFFFF: // current grf
|
|
|
|
grf_id = grfid;
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
default: // use the grfid specified in register 100h
|
|
|
|
idx = _object_mngr.GetID(local_id, grf_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the object type is invalid, there is none and the closest is far away. */
|
|
|
|
if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
|
|
|
|
|
|
|
|
return Object::GetTypeCount(idx) << 16 | min(GetClosestObject(tile, idx, current), 0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Used by the resolver to get values for feature 0F deterministic spritegroups. */
|
2012-11-10 20:40:46 +00:00
|
|
|
/* virtual */ uint32 ObjectScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
|
|
|
/* We get the town from the object, or we calculate the closest
|
|
|
|
* town if we need to when there's no object. */
|
2019-04-10 21:07:06 +00:00
|
|
|
const Town *t = nullptr;
|
2010-08-28 18:49:39 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->obj == nullptr) {
|
2010-08-28 18:49:39 +00:00
|
|
|
switch (variable) {
|
|
|
|
/* Allow these when there's no object. */
|
|
|
|
case 0x41:
|
|
|
|
case 0x60:
|
|
|
|
case 0x61:
|
|
|
|
case 0x62:
|
|
|
|
case 0x64:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Allow these, but find the closest town. */
|
|
|
|
case 0x45:
|
|
|
|
case 0x46:
|
2012-11-10 20:40:46 +00:00
|
|
|
if (!IsValidTile(this->tile)) goto unhandled;
|
|
|
|
t = ClosestTownFromTile(this->tile, UINT_MAX);
|
2010-08-28 18:49:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Construction date */
|
|
|
|
case 0x42: return _date;
|
|
|
|
|
|
|
|
/* Object founder information */
|
|
|
|
case 0x44: return _current_company;
|
|
|
|
|
2010-12-10 21:32:52 +00:00
|
|
|
/* Object view */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x48: return this->view;
|
2010-12-10 21:32:52 +00:00
|
|
|
|
2010-08-28 18:49:39 +00:00
|
|
|
/*
|
|
|
|
* Disallow the rest:
|
|
|
|
* 0x40: Relative position is passed as parameter during construction.
|
|
|
|
* 0x43: Animation counter is only for actual tiles.
|
2010-11-19 19:25:35 +00:00
|
|
|
* 0x47: Object colour is only valid when its built.
|
2010-08-28 18:49:39 +00:00
|
|
|
* 0x63: Animation counter of nearby tile, see above.
|
|
|
|
*/
|
|
|
|
default:
|
2010-10-08 21:07:54 +00:00
|
|
|
goto unhandled;
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
2010-10-08 21:07:54 +00:00
|
|
|
|
|
|
|
/* If there's an invalid tile, then we don't have enough information at all. */
|
2012-11-10 20:40:46 +00:00
|
|
|
if (!IsValidTile(this->tile)) goto unhandled;
|
2010-08-28 18:49:39 +00:00
|
|
|
} else {
|
2012-11-10 20:40:46 +00:00
|
|
|
t = this->obj->town;
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (variable) {
|
|
|
|
/* Relative position. */
|
|
|
|
case 0x40: {
|
2012-11-10 20:40:46 +00:00
|
|
|
uint offset = this->tile - this->obj->location.tile;
|
2010-08-28 18:49:39 +00:00
|
|
|
uint offset_x = TileX(offset);
|
|
|
|
uint offset_y = TileY(offset);
|
|
|
|
return offset_y << 20 | offset_x << 16 | offset_y << 8 | offset_x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tile information. */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x41: return GetTileSlope(this->tile) << 8 | GetTerrainType(this->tile);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Construction date */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x42: return this->obj->build_date;
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Animation counter */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x43: return GetAnimationFrame(this->tile);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Object founder information */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x44: return GetTileOwner(this->tile);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Get town zone and Manhattan distance of closest town */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceManhattan(this->tile, t->xy), 0xFFFF);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Get square of Euclidian distance of closes town */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x46: return GetTownRadiusGroup(t, this->tile) << 16 | min(DistanceSquare(this->tile, t->xy), 0xFFFF);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
2010-11-19 19:25:35 +00:00
|
|
|
/* Object colour */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x47: return this->obj->colour;
|
2010-11-19 19:25:35 +00:00
|
|
|
|
2010-12-10 21:32:52 +00:00
|
|
|
/* Object view */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x48: return this->obj->view;
|
2010-12-10 21:32:52 +00:00
|
|
|
|
2010-08-28 18:49:39 +00:00
|
|
|
/* Get object ID at offset param */
|
2013-11-24 14:41:19 +00:00
|
|
|
case 0x60: return GetObjectIDAtOffset(GetNearbyTile(parameter, this->tile), this->ro.grffile->grfid);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Get random tile bits at offset param */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x61: {
|
|
|
|
TileIndex tile = GetNearbyTile(parameter, this->tile);
|
|
|
|
return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetObjectRandomBits(tile) : 0;
|
|
|
|
}
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Land info of nearby tiles */
|
2019-04-10 21:07:06 +00:00
|
|
|
case 0x62: return GetNearbyObjectTileInformation(parameter, this->tile, this->obj == nullptr ? INVALID_OBJECT : this->obj->index, this->ro.grffile->grf_version >= 8);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Animation counter of nearby tile */
|
2012-11-10 20:40:46 +00:00
|
|
|
case 0x63: {
|
|
|
|
TileIndex tile = GetNearbyTile(parameter, this->tile);
|
|
|
|
return (IsTileType(tile, MP_OBJECT) && Object::GetByTile(tile) == this->obj) ? GetAnimationFrame(tile) : 0;
|
|
|
|
}
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
/* Count of object, distance of closest instance */
|
2013-11-24 14:41:19 +00:00
|
|
|
case 0x64: return GetCountAndDistanceOfClosestInstance(parameter, this->ro.grffile->grfid, this->tile, this->obj);
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 21:07:54 +00:00
|
|
|
unhandled:
|
2010-11-15 16:43:46 +00:00
|
|
|
DEBUG(grf, 1, "Unhandled object variable 0x%X", variable);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
*available = false;
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:46:39 +00:00
|
|
|
/**
|
|
|
|
* Constructor of the object resolver.
|
|
|
|
* @param obj Object being resolved.
|
|
|
|
* @param tile %Tile of the object.
|
|
|
|
* @param view View of the object.
|
|
|
|
* @param callback Callback ID.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param param1 First parameter (var 10) of the callback.
|
|
|
|
* @param param2 Second parameter (var 18) of the callback.
|
2012-11-10 20:46:39 +00:00
|
|
|
*/
|
2012-11-10 20:40:46 +00:00
|
|
|
ObjectResolverObject::ObjectResolverObject(const ObjectSpec *spec, Object *obj, TileIndex tile, uint8 view,
|
|
|
|
CallbackID callback, uint32 param1, uint32 param2)
|
2013-11-24 14:41:19 +00:00
|
|
|
: ResolverObject(spec->grf_prop.grffile, callback, param1, param2), object_scope(*this, obj, tile, view)
|
2011-06-12 20:50:03 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
this->town_scope = nullptr;
|
|
|
|
this->root_spritegroup = (obj == nullptr && spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT] != nullptr) ?
|
2014-03-03 20:02:31 +00:00
|
|
|
spec->grf_prop.spritegroup[CT_PURCHASE_OBJECT] : spec->grf_prop.spritegroup[0];
|
2012-11-10 20:40:46 +00:00
|
|
|
}
|
2011-06-12 20:50:03 +00:00
|
|
|
|
2012-11-10 20:40:46 +00:00
|
|
|
ObjectResolverObject::~ObjectResolverObject()
|
|
|
|
{
|
|
|
|
delete this->town_scope;
|
2011-06-12 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 18:49:39 +00:00
|
|
|
/**
|
2012-11-10 20:40:46 +00:00
|
|
|
* Get the town resolver scope that belongs to this object resolver.
|
|
|
|
* On the first call, the town scope is created (if possible).
|
|
|
|
* @return Town scope, if available.
|
2010-08-28 18:49:39 +00:00
|
|
|
*/
|
2012-11-10 20:40:46 +00:00
|
|
|
TownScopeResolver *ObjectResolverObject::GetTown()
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->town_scope == nullptr) {
|
2012-11-10 20:40:46 +00:00
|
|
|
Town *t;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (this->object_scope.obj != nullptr) {
|
2012-11-10 20:40:46 +00:00
|
|
|
t = this->object_scope.obj->town;
|
|
|
|
} else {
|
|
|
|
t = ClosestTownFromTile(this->object_scope.tile, UINT_MAX);
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
if (t == nullptr) return nullptr;
|
|
|
|
this->town_scope = new TownScopeResolver(*this, t, this->object_scope.obj == nullptr);
|
2012-11-10 20:40:46 +00:00
|
|
|
}
|
|
|
|
return this->town_scope;
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a callback for an object.
|
|
|
|
* @param callback The callback to perform.
|
|
|
|
* @param param1 The first parameter to pass to the NewGRF.
|
|
|
|
* @param param2 The second parameter to pass to the NewGRF.
|
|
|
|
* @param spec The specification of the object / the entry point.
|
|
|
|
* @param o The object to call the callback for.
|
|
|
|
* @param tile The tile the callback is called for.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param view The view of the object (only used when o == nullptr).
|
2010-08-28 18:49:39 +00:00
|
|
|
* @return The result of the callback.
|
|
|
|
*/
|
2011-06-12 20:36:33 +00:00
|
|
|
uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view)
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
2012-11-10 20:40:46 +00:00
|
|
|
ObjectResolverObject object(spec, o, tile, view, callback, param1, param2);
|
2014-03-03 20:02:31 +00:00
|
|
|
return object.ResolveCallback();
|
2010-08-28 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw an group of sprites on the map.
|
|
|
|
* @param ti Information about the tile to draw on.
|
|
|
|
* @param group The group of sprites to draw.
|
|
|
|
* @param spec Object spec to draw.
|
|
|
|
*/
|
|
|
|
static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
const DrawTileSprites *dts = group->ProcessRegisters(nullptr);
|
2010-08-28 18:49:39 +00:00
|
|
|
PaletteID palette = ((spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour;
|
|
|
|
|
|
|
|
SpriteID image = dts->ground.sprite;
|
|
|
|
PaletteID pal = dts->ground.pal;
|
|
|
|
|
|
|
|
if (GB(image, 0, SPRITE_WIDTH) != 0) {
|
|
|
|
/* If the ground sprite is the default flat water sprite, draw also canal/river borders
|
|
|
|
* Do not do this if the tile's WaterClass is 'land'. */
|
|
|
|
if ((image == SPR_FLAT_WATER_TILE || spec->flags & OBJECT_FLAG_DRAW_WATER) && IsTileOnWater(ti->tile)) {
|
|
|
|
DrawWaterClassGround(ti);
|
|
|
|
} else {
|
|
|
|
DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawNewGRFTileSeq(ti, dts, TO_STRUCTURES, 0, palette);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw an object on the map.
|
|
|
|
* @param ti Information about the tile to draw on.
|
|
|
|
* @param spec Object spec to draw.
|
|
|
|
*/
|
|
|
|
void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
|
|
|
|
{
|
2011-06-12 20:36:33 +00:00
|
|
|
Object *o = Object::GetByTile(ti->tile);
|
2012-11-10 20:40:46 +00:00
|
|
|
ObjectResolverObject object(spec, o, ti->tile);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
2014-03-03 20:02:31 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr || group->type != SGT_TILELAYOUT) return;
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
DrawTileLayout(ti, (const TileLayoutSpriteGroup *)group, spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw representation of an object (tile) for GUI purposes.
|
|
|
|
* @param x Position x of image.
|
|
|
|
* @param y Position y of image.
|
|
|
|
* @param spec Object spec to draw.
|
2010-12-10 21:32:52 +00:00
|
|
|
* @param view The object's view.
|
2010-08-28 18:49:39 +00:00
|
|
|
*/
|
2010-12-10 21:32:52 +00:00
|
|
|
void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
|
2010-08-28 18:49:39 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
ObjectResolverObject object(spec, nullptr, INVALID_TILE, view);
|
2014-03-03 20:02:31 +00:00
|
|
|
const SpriteGroup *group = object.Resolve();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (group == nullptr || group->type != SGT_TILELAYOUT) return;
|
2010-08-28 18:49:39 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
const DrawTileSprites *dts = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(nullptr);
|
2010-08-28 18:49:39 +00:00
|
|
|
|
|
|
|
PaletteID palette;
|
|
|
|
if (Company::IsValidID(_local_company)) {
|
|
|
|
/* Get the colours of our company! */
|
|
|
|
if (spec->flags & OBJECT_FLAG_2CC_COLOUR) {
|
|
|
|
const Livery *l = Company::Get(_local_company)->livery;
|
|
|
|
palette = SPR_2CCMAP_BASE + l->colour1 + l->colour2 * 16;
|
|
|
|
} else {
|
|
|
|
palette = COMPANY_SPRITE_COLOUR(_local_company);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* There's no company, so just take the base palette. */
|
|
|
|
palette = (spec->flags & OBJECT_FLAG_2CC_COLOUR) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpriteID image = dts->ground.sprite;
|
|
|
|
PaletteID pal = dts->ground.pal;
|
|
|
|
|
|
|
|
if (GB(image, 0, SPRITE_WIDTH) != 0) {
|
|
|
|
DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawNewGRFTileSeqInGUI(x, y, dts, 0, palette);
|
|
|
|
}
|
2010-08-28 18:51:47 +00:00
|
|
|
|
2010-12-10 21:32:52 +00:00
|
|
|
/**
|
|
|
|
* Perform a callback for an object.
|
|
|
|
* @param callback The callback to perform.
|
|
|
|
* @param param1 The first parameter to pass to the NewGRF.
|
|
|
|
* @param param2 The second parameter to pass to the NewGRF.
|
|
|
|
* @param spec The specification of the object / the entry point.
|
|
|
|
* @param o The object to call the callback for.
|
|
|
|
* @param tile The tile the callback is called for.
|
2011-10-31 22:31:35 +00:00
|
|
|
* @param extra_data Ignored.
|
2010-12-10 21:32:52 +00:00
|
|
|
* @return The result of the callback.
|
|
|
|
*/
|
2011-10-31 22:31:35 +00:00
|
|
|
uint16 StubGetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, int extra_data)
|
2010-12-10 21:32:52 +00:00
|
|
|
{
|
|
|
|
return GetObjectCallback(callback, param1, param2, spec, o, tile);
|
|
|
|
}
|
|
|
|
|
2010-08-28 18:51:47 +00:00
|
|
|
/** Helper class for animation control. */
|
2011-10-31 22:31:35 +00:00
|
|
|
struct ObjectAnimationBase : public AnimationBase<ObjectAnimationBase, ObjectSpec, Object, int, StubGetObjectCallback> {
|
2010-08-28 18:51:47 +00:00
|
|
|
static const CallbackID cb_animation_speed = CBID_OBJECT_ANIMATION_SPEED;
|
|
|
|
static const CallbackID cb_animation_next_frame = CBID_OBJECT_ANIMATION_NEXT_FRAME;
|
|
|
|
|
|
|
|
static const ObjectCallbackMask cbm_animation_speed = CBM_OBJ_ANIMATION_SPEED;
|
|
|
|
static const ObjectCallbackMask cbm_animation_next_frame = CBM_OBJ_ANIMATION_NEXT_FRAME;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the animation of the object tile.
|
|
|
|
* @param tile The tile to animate.
|
|
|
|
*/
|
|
|
|
void AnimateNewObjectTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
const ObjectSpec *spec = ObjectSpec::GetByTile(tile);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec == nullptr || !(spec->flags & OBJECT_FLAG_ANIMATION)) return;
|
2010-08-28 18:51:47 +00:00
|
|
|
|
|
|
|
ObjectAnimationBase::AnimateTile(spec, Object::GetByTile(tile), tile, (spec->flags & OBJECT_FLAG_ANIM_RANDOM_BITS) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the update of animation on a single tile.
|
|
|
|
* @param o The object that got triggered.
|
|
|
|
* @param tile The location of the triggered tile.
|
|
|
|
* @param trigger The trigger that is triggered.
|
|
|
|
* @param spec The spec associated with the object.
|
|
|
|
*/
|
2011-06-12 20:32:52 +00:00
|
|
|
void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
|
2010-08-28 18:51:47 +00:00
|
|
|
{
|
|
|
|
if (!HasBit(spec->animation.triggers, trigger)) return;
|
|
|
|
|
|
|
|
ObjectAnimationBase::ChangeAnimationFrame(CBID_OBJECT_ANIMATION_START_STOP, spec, o, tile, Random(), trigger);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger the update of animation on a whole object.
|
|
|
|
* @param o The object that got triggered.
|
|
|
|
* @param trigger The trigger that is triggered.
|
|
|
|
* @param spec The spec associated with the object.
|
|
|
|
*/
|
2011-06-12 20:32:52 +00:00
|
|
|
void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
|
2010-08-28 18:51:47 +00:00
|
|
|
{
|
|
|
|
if (!HasBit(spec->animation.triggers, trigger)) return;
|
|
|
|
|
|
|
|
TILE_AREA_LOOP(tile, o->location) {
|
|
|
|
TriggerObjectTileAnimation(o, tile, trigger, spec);
|
|
|
|
}
|
|
|
|
}
|