OpenTTD Source: src/newgrf_object.h Source File

OpenTTD Source 13.2.1

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 "date_type.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 {
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
43 static const uint8 OBJECT_SIZE_1X1 = 0x11;
44
45 void ResetObjects();
46
48 enum ObjectClassID {
49 OBJECT_CLASS_BEGIN = 0,
50 OBJECT_CLASS_MAX = 0xFF,
51 INVALID_OBJECT_CLASS = 0xFF,
52 };
54 DECLARE_POSTFIX_INCREMENT(ObjectClassID)
55
56
60 struct ObjectSpec {
61 /* 2 because of the "normal" and "buy" sprite stacks. */
62 GRFFilePropsBase<2> grf_prop;
63 ObjectClassID cls_id;
64 StringID name;
65
66 uint8 climate;
67 uint8 size;
68 uint8 build_cost_multiplier;
69 uint8 clear_cost_multiplier;
70 Date introduction_date;
71 Date end_of_life_date;
72 ObjectFlags flags;
73 AnimationInfo animation;
74 uint16 callback_mask;
75 uint8 height;
76 uint8 views;
77 uint8 generate_amount;
78 bool enabled;
79
84 Money GetBuildCost() const { return GetPrice(PR_BUILD_OBJECT, this->build_cost_multiplier, this->grf_prop.grffile, 0); }
85
90 Money GetClearCost() const { return GetPrice(PR_CLEAR_OBJECT, this->clear_cost_multiplier, this->grf_prop.grffile, 0); }
91
92 bool IsEverAvailable() const;
93 bool WasEverAvailable() const;
94 bool IsAvailable() const;
95 uint Index() const;
96
97 static const ObjectSpec *Get(ObjectType index);
98 static const ObjectSpec *GetByTile(TileIndex tile);
99 };
100
102 struct ObjectScopeResolver : public ScopeResolver {
103 struct Object *obj;
104 const ObjectSpec *spec;
105 TileIndex tile;
106 uint8 view;
107
115 ObjectScopeResolver(ResolverObject &ro, Object *obj, const ObjectSpec *spec, TileIndex tile, uint8 view = 0)
116 : ScopeResolver(ro), obj(obj), spec(spec), tile(tile), view(view)
117 {
118 }
119
120 uint32 GetRandomBits() const override;
121 uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override;
122 };
123
125 struct ObjectResolverObject : public ResolverObject {
126 ObjectScopeResolver object_scope;
127 TownScopeResolver *town_scope;
128
129 ObjectResolverObject(const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0,
130 CallbackID callback = CBID_NO_CALLBACK, uint32 param1 = 0, uint32 param2 = 0);
131 ~ObjectResolverObject();
132
133 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0) override
134 {
135 switch (scope) {
136 case VSG_SCOPE_SELF:
137 return &this->object_scope;
138
139 case VSG_SCOPE_PARENT: {
140 TownScopeResolver *tsr = this->GetTown();
141 if (tsr != nullptr) return tsr;
142 FALLTHROUGH;
143 }
144
145 default:
146 return ResolverObject::GetScope(scope, relative);
147 }
148 }
149
150 GrfSpecFeature GetFeature() const override;
151 uint32 GetDebugID() const override;
152
153 private:
154 TownScopeResolver *GetTown();
155 };
156
158 typedef NewGRFClass<ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX> ObjectClass;
159
161 static const CargoID CT_PURCHASE_OBJECT = 1;
162
163 uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view = 0);
164
165 void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec);
166 void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view);
167 void AnimateNewObjectTile(TileIndex tile);
168 void TriggerObjectTileAnimation(Object *o, TileIndex tile, ObjectAnimationTrigger trigger, const ObjectSpec *spec);
169 void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec);
170
171 #endif /* NEWGRF_OBJECT_H */
OBJECT_CLASS_BEGIN
@ OBJECT_CLASS_BEGIN
The lowest valid value.
Definition: newgrf_object.h:49
OBJECT_FLAG_BUILT_ON_WATER
@ OBJECT_FLAG_BUILT_ON_WATER
Object can be built on water (not required).
Definition: newgrf_object.h:29
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:540
ObjectResolverObject
A resolver object to be used with feature 0F spritegroups.
Definition: newgrf_object.h:125
OBJECT_FLAG_ANIMATION
@ OBJECT_FLAG_ANIMATION
Object has animated tiles.
Definition: newgrf_object.h:32
VarSpriteGroupScope
VarSpriteGroupScope
Definition: newgrf_spritegroup.h:97
OBJECT_FLAG_ONLY_IN_SCENEDIT
@ OBJECT_FLAG_ONLY_IN_SCENEDIT
Object can only be constructed in the scenario editor.
Definition: newgrf_object.h:26
GetPrice
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition: economy.cpp:961
OBJECT_FLAG_2CC_COLOUR
@ OBJECT_FLAG_2CC_COLOUR
Object wants 2CC colour mapping.
Definition: newgrf_object.h:34
ObjectSpec
Allow incrementing of ObjectClassID variables.
Definition: newgrf_object.h:60
ObjectSpec::grf_prop
GRFFilePropsBase< 2 > grf_prop
Properties related the the grf file.
Definition: newgrf_object.h:62
ObjectFlags
ObjectFlags
Various object behaviours.
Definition: newgrf_object.h:24
newgrf_commons.h
ObjectResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0) override
Get a resolver for the scope.
Definition: newgrf_object.h:133
OBJECT_FLAG_NONE
@ OBJECT_FLAG_NONE
Just nothing.
Definition: newgrf_object.h:25
ObjectScopeResolver::ObjectScopeResolver
ObjectScopeResolver(ResolverObject &ro, Object *obj, const ObjectSpec *spec, TileIndex tile, uint8 view=0)
Constructor of an object scope resolver.
Definition: newgrf_object.h:115
ObjectSpec::build_cost_multiplier
uint8 build_cost_multiplier
Build cost multiplier per tile.
Definition: newgrf_object.h:68
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:42
ObjectResolverObject::ObjectResolverObject
ObjectResolverObject(const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view=0, CallbackID callback=CBID_NO_CALLBACK, uint32 param1=0, uint32 param2=0)
Constructor of the object resolver.
Definition: newgrf_object.cpp:353
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
INVALID_OBJECT_CLASS
@ INVALID_OBJECT_CLASS
Class for the less fortunate.
Definition: newgrf_object.h:51
ObjectAnimationTrigger
ObjectAnimationTrigger
Animation triggers for objects.
Definition: newgrf_animation_type.h:56
ObjectResolverObject::GetDebugID
uint32 GetDebugID() const override
Get an identifier for the item being resolved.
Definition: newgrf_object.cpp:392
ObjectScopeResolver::obj
struct Object * obj
The object the callback is ran for.
Definition: newgrf_object.h:103
ResolverObject
Interface for SpriteGroup-s to access the gamestate.
Definition: newgrf_spritegroup.h:307
AnimationInfo
Information about animation.
Definition: newgrf_animation_type.h:18
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
ObjectSpec::end_of_life_date
Date end_of_life_date
When can't this object be built anymore.
Definition: newgrf_object.h:71
newgrf_callbacks.h
economy_func.h
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition: newgrf_callbacks.h:20
VSG_SCOPE_PARENT
@ VSG_SCOPE_PARENT
Related object of the resolved one.
Definition: newgrf_spritegroup.h:101
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition: newgrf_spritegroup.h:288
ObjectSpec::size
uint8 size
The size of this objects; low nibble for X, high nibble for Y.
Definition: newgrf_object.h:67
ObjectSpec::callback_mask
uint16 callback_mask
Bitmask of requested/allowed callbacks.
Definition: newgrf_object.h:74
ObjectResolverObject::town_scope
TownScopeResolver * town_scope
The town scope resolver (created on the first call).
Definition: newgrf_object.h:127
ObjectSpec::GetClearCost
Money GetClearCost() const
Get the cost for clearing a structure of this type.
Definition: newgrf_object.h:90
ObjectScopeResolver::tile
TileIndex tile
The tile related to the object.
Definition: newgrf_object.h:105
ObjectResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition: newgrf_object.cpp:387
ObjectSpec::animation
AnimationInfo animation
Information about the animation.
Definition: newgrf_object.h:73
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition: newgrf_spritegroup.h:100
ObjectSpec::introduction_date
Date introduction_date
From when can this object be built.
Definition: newgrf_object.h:70
DECLARE_POSTFIX_INCREMENT
#define DECLARE_POSTFIX_INCREMENT(enum_type)
Some enums need to have allowed incrementing (i.e.
Definition: enum_type.hpp:14
OBJECT_FLAG_CANNOT_REMOVE
@ OBJECT_FLAG_CANNOT_REMOVE
Object can not be removed.
Definition: newgrf_object.h:27
ObjectClassID
ObjectClassID
Class IDs for objects.
Definition: newgrf_object.h:48
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition: newgrf_callbacks.h:22
ObjectType
uint16 ObjectType
Types of objects.
Definition: object_type.h:14
TriggerObjectAnimation
void TriggerObjectAnimation(Object *o, ObjectAnimationTrigger trigger, const ObjectSpec *spec)
Trigger the update of animation on a whole object.
Definition: newgrf_object.cpp:553
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
Object
An object, such as transmitter, on the map.
Definition: object_base.h:23
NewGRFClass
Struct containing information relating to NewGRF classes for stations and airports.
Definition: newgrf_class.h:19
ObjectSpec::height
uint8 height
The height of this structure, in heightlevels; max MAX_TILE_HEIGHT.
Definition: newgrf_object.h:75
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
Definition: newgrf_spritegroup.cpp:139
DrawNewObjectTileInGUI
void DrawNewObjectTileInGUI(int x, int y, const ObjectSpec *spec, uint8 view)
Draw representation of an object (tile) for GUI purposes.
Definition: newgrf_object.cpp:464
TownScopeResolver
Scope resolver for a town.
Definition: newgrf_town.h:22
ObjectSpec::enabled
bool enabled
Is this spec enabled?
Definition: newgrf_object.h:78
DrawNewObjectTile
void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec)
Draw an object on the map.
Definition: newgrf_object.cpp:446
ObjectScopeResolver::view
uint8 view
The view of the object.
Definition: newgrf_object.h:106
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
ObjectSpec::name
StringID name
The name for this object.
Definition: newgrf_object.h:64
date_type.h
newgrf_spritegroup.h
AnimateNewObjectTile
void AnimateNewObjectTile(TileIndex tile)
Handle the animation of the object tile.
Definition: newgrf_object.cpp:525
GetObjectCallback
uint16 GetObjectCallback(CallbackID callback, uint32 param1, uint32 param2, const ObjectSpec *spec, Object *o, TileIndex tile, uint8 view=0)
Perform a callback for an object.
Definition: newgrf_object.cpp:408
ObjectSpec::clear_cost_multiplier
uint8 clear_cost_multiplier
Clear cost multiplier per tile.
Definition: newgrf_object.h:69
OBJECT_FLAG_AUTOREMOVE
@ OBJECT_FLAG_AUTOREMOVE
Object get automatically removed (like "owned land").
Definition: newgrf_object.h:28
newgrf_town.h
OBJECT_CLASS_MAX
@ OBJECT_CLASS_MAX
Maximum number of classes.
Definition: newgrf_object.h:50
GrfSpecFeature
GrfSpecFeature
Definition: newgrf.h:66
ObjectSpec::generate_amount
uint8 generate_amount
Number of objects which are attempted to be generated per 256^2 map during world generation.
Definition: newgrf_object.h:77
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
ResetObjects
void ResetObjects()
This function initialize the spec arrays of objects.
Definition: newgrf_object.cpp:94
ObjectSpec::views
uint8 views
The number of views.
Definition: newgrf_object.h:76
ObjectClass
NewGRFClass< ObjectSpec, ObjectClassID, OBJECT_CLASS_MAX > ObjectClass
Struct containing information relating to object classes.
Definition: newgrf_object.h:158
newgrf_class.h
ObjectScopeResolver::GetVariable
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override
Used by the resolver to get values for feature 0F deterministic spritegroups.
Definition: newgrf_object.cpp:233
OBJECT_FLAG_ALLOW_UNDER_BRIDGE
@ OBJECT_FLAG_ALLOW_UNDER_BRIDGE
Object can built under a bridge.
Definition: newgrf_object.h:37
ObjectResolverObject::GetTown
TownScopeResolver * GetTown()
Get the town resolver scope that belongs to this object resolver.
Definition: newgrf_object.cpp:372
OBJECT_FLAG_ANIM_RANDOM_BITS
@ OBJECT_FLAG_ANIM_RANDOM_BITS
Object wants random bits in "next animation frame" callback.
Definition: newgrf_object.h:38
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition: newgrf_spritegroup.h:325
ObjectScopeResolver
Object scope resolver.
Definition: newgrf_object.h:102
DECLARE_ENUM_AS_BIT_SET
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Definition: company_manager_face.h:29
OBJECT_FLAG_DRAW_WATER
@ OBJECT_FLAG_DRAW_WATER
Object wants to be drawn on water.
Definition: newgrf_object.h:36
ObjectScopeResolver::spec
const ObjectSpec * spec
Specification of the object type.
Definition: newgrf_object.h:104
CT_PURCHASE_OBJECT
static const CargoID CT_PURCHASE_OBJECT
Mapping of purchase for objects.
Definition: newgrf_object.h:161
OverflowSafeInt< int64 >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
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_SCALE_BY_WATER
@ OBJECT_FLAG_SCALE_BY_WATER
Object count is roughly scaled by water amount at edges.
Definition: newgrf_object.h:39
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition: newgrf_spritegroup.h:289
ObjectSpec::GetBuildCost
Money GetBuildCost() const
Get the cost for building a structure of this type.
Definition: newgrf_object.h:84
GRFFilePropsBase::grffile
const struct GRFFile * grffile
grf file that introduced this entity
Definition: newgrf_commons.h:320
OBJECT_SIZE_1X1
static const uint8 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
ObjectSpec::climate
uint8 climate
In which climates is this object available?
Definition: newgrf_object.h:66
ObjectScopeResolver::GetRandomBits
uint32 GetRandomBits() const override
Get a few random bits.
Definition: newgrf_object.cpp:131
newgrf_animation_type.h
GRFFilePropsBase< 2 >
object_type.h
ObjectSpec::cls_id
ObjectClassID cls_id
The class to which this spec belongs.
Definition: newgrf_object.h:63
ObjectResolverObject::object_scope
ObjectScopeResolver object_scope
The object scope resolver.
Definition: newgrf_object.h:126
OBJECT_FLAG_ONLY_IN_GAME
@ OBJECT_FLAG_ONLY_IN_GAME
Object can only be built in game.
Definition: newgrf_object.h:33
ObjectSpec::flags
ObjectFlags flags
Flags/settings related to the object.
Definition: newgrf_object.h:72