OpenTTD Source 20250205-master-gfd85ab1e2c
newgrf_object.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10#ifndef NEWGRF_OBJECT_H
11#define NEWGRF_OBJECT_H
12
13#include "newgrf_callbacks.h"
14#include "newgrf_spritegroup.h"
15#include "newgrf_town.h"
16#include "economy_func.h"
18#include "object_type.h"
20#include "newgrf_class.h"
21#include "newgrf_commons.h"
22
24enum class ObjectFlag : uint8_t {
25 OnlyInScenedit = 0,
26 CannotRemove = 1,
27 Autoremove = 2,
28 BuiltOnWater = 3,
29 ClearIncome = 4,
30 HasNoFoundation = 5,
31 Animation = 6,
32 OnlyInGame = 7,
33 Uses2CC = 8,
34 NotOnLand = 9,
35 DrawWater = 10,
36 AllowUnderBridge = 11,
37 AnimRandomBits = 12,
38 ScaleByWater = 13,
39};
41
42static const uint8_t OBJECT_SIZE_1X1 = 0x11;
43
44void ResetObjects();
45
47enum ObjectClassID : uint16_t {
49 OBJECT_CLASS_MAX = UINT16_MAX,
50 INVALID_OBJECT_CLASS = UINT16_MAX,
51};
54
55
60 /* 2 because of the "normal" and "buy" sprite stacks. */
64
66 uint8_t size;
73 uint8_t height;
74 uint8_t views;
76
81 bool IsEnabled() const { return this->views > 0; }
82
87 Money GetBuildCost() const { return GetPrice(PR_BUILD_OBJECT, this->build_cost_multiplier, this->grf_prop.grffile, 0); }
88
93 Money GetClearCost() const { return GetPrice(PR_CLEAR_OBJECT, this->clear_cost_multiplier, this->grf_prop.grffile, 0); }
94
95 bool IsEverAvailable() const;
96 bool WasEverAvailable() const;
97 bool IsAvailable() const;
98 uint Index() const;
99
100 static const std::vector<ObjectSpec> &Specs();
101 static size_t Count();
102 static const ObjectSpec *Get(ObjectType index);
103 static const ObjectSpec *GetByTile(TileIndex tile);
104
105 static void BindToClasses();
106};
107
110 struct Object *obj;
113 uint8_t view;
114
126
127 uint32_t GetRandomBits() const override;
128 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
129};
130
134 std::optional<TownScopeResolver> town_scope = std::nullopt;
135
136 ObjectResolverObject(const ObjectSpec *spec, Object *o, TileIndex tile, uint8_t view = 0,
137 CallbackID callback = CBID_NO_CALLBACK, uint32_t param1 = 0, uint32_t param2 = 0);
138
139 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
140 {
141 switch (scope) {
142 case VSG_SCOPE_SELF:
143 return &this->object_scope;
144
145 case VSG_SCOPE_PARENT: {
146 TownScopeResolver *tsr = this->GetTown();
147 if (tsr != nullptr) return tsr;
148 [[fallthrough]];
149 }
150
151 default:
152 return ResolverObject::GetScope(scope, relative);
153 }
154 }
155
156 GrfSpecFeature GetFeature() const override;
157 uint32_t GetDebugID() const override;
158
159private:
161};
162
165
166static const size_t OBJECT_SPRITE_GROUP_DEFAULT = 0;
167static const size_t OBJECT_SPRITE_GROUP_PURCHASE = 1;
168
169uint16_t GetObjectCallback(CallbackID callback, uint32_t param1, uint32_t param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8_t view = 0);
170
171void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec);
172void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8_t view);
176
177#endif /* NEWGRF_OBJECT_H */
Struct containing information relating to NewGRF classes for stations and airports.
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:966
Functions related to the economy.
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:61
GrfSpecFeature
Definition newgrf.h:68
Definitions related to NewGRF animation.
ObjectAnimationTrigger
Animation triggers for objects.
Callbacks that NewGRFs could implement.
CallbackID
List of implemented NewGRF callbacks.
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Header file for classes to be used by e.g.
This file simplyfies and embeds a common mechanism of loading/saving and mapping of grf entities.
static const uint8_t OBJECT_SIZE_1X1
The value of a NewGRF's size property when the object is 1x1 tiles: low nibble for X,...
void ResetObjects()
This function initialize the spec arrays of objects.
void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8_t view)
Draw representation of an object (tile) for GUI purposes.
void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
Trigger the update of animation on a whole object.
ObjectFlag
Various object behaviours.
@ AnimRandomBits
Object wants random bits in "next animation frame" callback.
@ Autoremove
Object get automatically removed (like "owned land").
@ CannotRemove
Object can not be removed.
@ AllowUnderBridge
Object can built under a bridge.
@ OnlyInScenedit
Object can only be constructed in the scenario editor.
@ BuiltOnWater
Object can be built on water (not required).
@ OnlyInGame
Object can only be built in game.
@ DrawWater
Object wants to be drawn on water.
@ HasNoFoundation
Do not display foundations when on a slope.
@ Uses2CC
Object wants 2CC colour mapping.
@ NotOnLand
Object can not be on land, implicitly sets ObjectFlag::BuiltOnWater.
@ ScaleByWater
Object count is roughly scaled by water amount at edges.
@ Animation
Object has animated tiles.
@ ClearIncome
When object is cleared a positive income is generated instead of a cost.
void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
Draw an object on the map.
uint16_t GetObjectCallback(CallbackID callback, uint32_t param1, uint32_t param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8_t view=0)
Perform a callback for an object.
void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
Trigger the update of animation on a single tile.
ObjectClassID
Class IDs for objects.
@ INVALID_OBJECT_CLASS
Class for the less fortunate.
@ OBJECT_CLASS_BEGIN
The lowest valid value.
@ OBJECT_CLASS_MAX
Maximum number of classes.
void AnimateNewObjectTile(TileIndex tile)
Handle the animation of the object tile.
Action 2 handling.
VarSpriteGroupScope
@ VSG_SCOPE_SELF
Resolved object itself.
@ VSG_SCOPE_PARENT
Related object of the resolved one.
Functions to handle the town part of NewGRF towns.
Types related to object tiles.
uint16_t ObjectType
Types of objects.
Definition object_type.h:14
@ Count
by amount of cargo
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Information about animation.
Data related to the handling of grf files.
const struct GRFFile * grffile
grf file that introduced this entity
A resolver object to be used with feature 0F spritegroups.
ObjectScopeResolver object_scope
The object scope resolver.
std::optional< TownScopeResolver > town_scope
The town scope resolver (created on the first call).
TownScopeResolver * GetTown()
Get the town resolver scope that belongs to this object resolver.
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Object scope resolver.
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Used by the resolver to get values for feature 0F deterministic spritegroups.
TileIndex tile
The tile related to the object.
uint8_t view
The view of the object.
uint32_t GetRandomBits() const override
Get a few random bits.
const ObjectSpec * spec
Specification of the object type.
ObjectScopeResolver(ResolverObject &ro, Object *obj, const ObjectSpec *spec, TileIndex tile, uint8_t view=0)
Constructor of an object scope resolver.
struct Object * obj
The object the callback is ran for.
Allow incrementing of ObjectClassID variables.
AnimationInfo animation
Information about the animation.
bool IsEnabled() const
Test if this object is enabled.
TimerGameCalendar::Date introduction_date
From when can this object be built.
LandscapeTypes climate
In which climates is this object available?
StringID name
The name for this object.
GRFFilePropsBase< 2 > grf_prop
Properties related the the grf file.
Money GetClearCost() const
Get the cost for clearing a structure of this type.
uint8_t size
The size of this objects; low nibble for X, high nibble for Y.
uint8_t generate_amount
Number of objects which are attempted to be generated per 256^2 map during world generation.
ObjectCallbackMasks callback_mask
Bitmask of requested/allowed callbacks.
ObjectFlags flags
Flags/settings related to the object.
uint8_t height
The height of this structure, in heightlevels; max MAX_TILE_HEIGHT.
uint8_t clear_cost_multiplier
Clear cost multiplier per tile.
Money GetBuildCost() const
Get the cost for building a structure of this type.
uint8_t views
The number of views.
TimerGameCalendar::Date end_of_life_date
When can't this object be built anymore.
uint8_t build_cost_multiplier
Build cost multiplier per tile.
An object, such as transmitter, on the map.
Definition object_base.h:23
Interface for SpriteGroup-s to access the gamestate.
CallbackID callback
Callback being resolved.
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
ResolverObject & ro
Surrounding resolver object.
Templated helper to make a type-safe 'typedef' representing a single POD value.
Tile information, used while rendering the tile.
Definition tile_cmd.h:43
Scope resolver for a town.
Definition newgrf_town.h:22
Definition of the game-calendar-timer.