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/>.
8 /** @file newgrf_object.h Functions related to NewGRF objects. */
10 #ifndef NEWGRF_OBJECT_H
11 #define NEWGRF_OBJECT_H
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"
23 /** Various object behaviours. */
24 enum ObjectFlags
: uint16_t {
25 OBJECT_FLAG_NONE
= 0, ///< Just nothing.
26 OBJECT_FLAG_ONLY_IN_SCENEDIT
= 1 << 0, ///< Object can only be constructed in the scenario editor.
27 OBJECT_FLAG_CANNOT_REMOVE
= 1 << 1, ///< Object can not be removed.
28 OBJECT_FLAG_AUTOREMOVE
= 1 << 2, ///< Object get automatically removed (like "owned land").
29 OBJECT_FLAG_BUILT_ON_WATER
= 1 << 3, ///< Object can be built on water (not required).
30 OBJECT_FLAG_CLEAR_INCOME
= 1 << 4, ///< When object is cleared a positive income is generated instead of a cost.
31 OBJECT_FLAG_HAS_NO_FOUNDATION
= 1 << 5, ///< Do not display foundations when on a slope.
32 OBJECT_FLAG_ANIMATION
= 1 << 6, ///< Object has animated tiles.
33 OBJECT_FLAG_ONLY_IN_GAME
= 1 << 7, ///< Object can only be built in game.
34 OBJECT_FLAG_2CC_COLOUR
= 1 << 8, ///< Object wants 2CC colour mapping.
35 OBJECT_FLAG_NOT_ON_LAND
= 1 << 9, ///< Object can not be on land, implicitly sets #OBJECT_FLAG_BUILT_ON_WATER.
36 OBJECT_FLAG_DRAW_WATER
= 1 << 10, ///< Object wants to be drawn on water.
37 OBJECT_FLAG_ALLOW_UNDER_BRIDGE
= 1 << 11, ///< Object can built under a bridge.
38 OBJECT_FLAG_ANIM_RANDOM_BITS
= 1 << 12, ///< Object wants random bits in "next animation frame" callback.
39 OBJECT_FLAG_SCALE_BY_WATER
= 1 << 13, ///< Object count is roughly scaled by water amount at edges.
41 DECLARE_ENUM_AS_BIT_SET(ObjectFlags
)
43 static const uint8_t OBJECT_SIZE_1X1
= 0x11; ///< The value of a NewGRF's size property when the object is 1x1 tiles: low nibble for X, high nibble for Y.
47 /** Class IDs for objects. */
48 enum ObjectClassID
: uint8_t {
49 OBJECT_CLASS_BEGIN
= 0, ///< The lowest valid value
50 OBJECT_CLASS_MAX
= 0xFF, ///< Maximum number of classes.
51 INVALID_OBJECT_CLASS
= 0xFF, ///< Class for the less fortunate.
53 /** Allow incrementing of ObjectClassID variables */
54 DECLARE_POSTFIX_INCREMENT(ObjectClassID
)
56 /** An object that isn't use for transport, industries or houses.
57 * @note If you change this struct, adopt the initialization of
58 * default objects in table/object_land.h
61 /* 2 because of the "normal" and "buy" sprite stacks. */
62 GRFFilePropsBase
<2> grf_prop
; ///< Properties related the the grf file
63 AnimationInfo animation
; ///< Information about the animation.
64 ObjectClassID cls_id
; ///< The class to which this spec belongs.
65 StringID name
; ///< The name for this object.
67 uint8_t climate
; ///< In which climates is this object available?
68 uint8_t size
; ///< The size of this objects; low nibble for X, high nibble for Y.
69 uint8_t build_cost_multiplier
; ///< Build cost multiplier per tile.
70 uint8_t clear_cost_multiplier
; ///< Clear cost multiplier per tile.
71 TimerGameCalendar::Date introduction_date
; ///< From when can this object be built.
72 TimerGameCalendar::Date end_of_life_date
; ///< When can't this object be built anymore.
73 ObjectFlags flags
; ///< Flags/settings related to the object.
74 uint16_t callback_mask
; ///< Bitmask of requested/allowed callbacks.
75 uint8_t height
; ///< The height of this structure, in heightlevels; max MAX_TILE_HEIGHT.
76 uint8_t views
; ///< The number of views.
77 uint8_t generate_amount
; ///< Number of objects which are attempted to be generated per 256^2 map during world generation.
80 * Test if this object is enabled.
81 * @return True iff this object is enabled.
83 bool IsEnabled() const { return this->views
> 0; }
86 * Get the cost for building a structure of this type.
87 * @return The cost for building.
89 Money
GetBuildCost() const { return GetPrice(PR_BUILD_OBJECT
, this->build_cost_multiplier
, this->grf_prop
.grffile
, 0); }
92 * Get the cost for clearing a structure of this type.
93 * @return The cost for clearing.
95 Money
GetClearCost() const { return GetPrice(PR_CLEAR_OBJECT
, this->clear_cost_multiplier
, this->grf_prop
.grffile
, 0); }
97 bool IsEverAvailable() const;
98 bool WasEverAvailable() const;
99 bool IsAvailable() const;
102 static const std::vector
<ObjectSpec
> &Specs();
103 static size_t Count();
104 static const ObjectSpec
*Get(ObjectType index
);
105 static const ObjectSpec
*GetByTile(TileIndex tile
);
107 static void BindToClasses();
110 /** Object scope resolver. */
111 struct ObjectScopeResolver
: public ScopeResolver
{
112 struct Object
*obj
; ///< The object the callback is ran for.
113 const ObjectSpec
*spec
; ///< Specification of the object type.
114 TileIndex tile
; ///< The tile related to the object.
115 uint8_t view
; ///< The view of the object.
118 * Constructor of an object scope resolver.
119 * @param ro Surrounding resolver.
120 * @param obj Object being resolved.
121 * @param tile %Tile of the object.
122 * @param view View of the object.
124 ObjectScopeResolver(ResolverObject
&ro
, Object
*obj
, const ObjectSpec
*spec
, TileIndex tile
, uint8_t view
= 0)
125 : ScopeResolver(ro
), obj(obj
), spec(spec
), tile(tile
), view(view
)
129 uint32_t GetRandomBits() const override
;
130 uint32_t GetVariable(byte variable
, [[maybe_unused
]] uint32_t parameter
, bool *available
) const override
;
133 /** A resolver object to be used with feature 0F spritegroups. */
134 struct ObjectResolverObject
: public ResolverObject
{
135 ObjectScopeResolver object_scope
; ///< The object scope resolver.
136 TownScopeResolver
*town_scope
; ///< The town scope resolver (created on the first call).
138 ObjectResolverObject(const ObjectSpec
*spec
, Object
*o
, TileIndex tile
, uint8_t view
= 0,
139 CallbackID callback
= CBID_NO_CALLBACK
, uint32_t param1
= 0, uint32_t param2
= 0);
140 ~ObjectResolverObject();
142 ScopeResolver
*GetScope(VarSpriteGroupScope scope
= VSG_SCOPE_SELF
, byte relative
= 0) override
146 return &this->object_scope
;
148 case VSG_SCOPE_PARENT
: {
149 TownScopeResolver
*tsr
= this->GetTown();
150 if (tsr
!= nullptr) return tsr
;
155 return ResolverObject::GetScope(scope
, relative
);
159 GrfSpecFeature
GetFeature() const override
;
160 uint32_t GetDebugID() const override
;
163 TownScopeResolver
*GetTown();
166 /** Struct containing information relating to object classes. */
167 typedef NewGRFClass
<ObjectSpec
, ObjectClassID
, OBJECT_CLASS_MAX
> ObjectClass
;
169 static const size_t OBJECT_SPRITE_GROUP_DEFAULT
= 0;
170 static const size_t OBJECT_SPRITE_GROUP_PURCHASE
= 1;
172 uint16_t GetObjectCallback(CallbackID callback
, uint32_t param1
, uint32_t param2
, const ObjectSpec
*spec
, Object
*o
, TileIndex tile
, uint8_t view
= 0);
174 void DrawNewObjectTile(TileInfo
*ti
, const ObjectSpec
*spec
);
175 void DrawNewObjectTileInGUI(int x
, int y
, const ObjectSpec
*spec
, uint8_t view
);
176 void AnimateNewObjectTile(TileIndex tile
);
177 void TriggerObjectTileAnimation(Object
*o
, TileIndex tile
, ObjectAnimationTrigger trigger
, const ObjectSpec
*spec
);
178 void TriggerObjectAnimation(Object
*o
, ObjectAnimationTrigger trigger
, const ObjectSpec
*spec
);
180 #endif /* NEWGRF_OBJECT_H */