OpenTTD Source: src/newgrf_object.h Source File

OpenTTD Source 15.0-beta1

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