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_industrytiles.cpp NewGRF handling of industry tiles. */
12 #include "landscape.h"
13 #include "newgrf_industrytiles.h"
14 #include "newgrf_sound.h"
17 #include "command_func.h"
19 #include "newgrf_animation_base.h"
21 #include "table/strings.h"
23 #include "safeguards.h"
26 * Based on newhouses equivalent, but adapted for newindustries
27 * @param parameter from callback. It's in fact a pair of coordinates
28 * @param tile TileIndex from which the callback was initiated
29 * @param index of the industry been queried for
30 * @param signed_offsets Are the x and y offset encoded in parameter signed?
31 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
32 * @return a construction of bits obeying the newgrf format
34 uint32
GetNearbyIndustryTileInformation(byte parameter
, TileIndex tile
, IndustryID index
, bool signed_offsets
, bool grf_version8
)
36 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
, signed_offsets
); // only perform if it is required
37 bool is_same_industry
= (IsTileType(tile
, MP_INDUSTRY
) && GetIndustryIndex(tile
) == index
);
39 return GetNearbyTileInformation(tile
, grf_version8
) | (is_same_industry
? 1 : 0) << 8;
43 * This is the position of the tile relative to the northernmost tile of the industry.
46 * x the x offset from the northernmost tile
47 * XX same, but stored in a byte instead of a nibble
48 * y the y offset from the northernmost tile
49 * YY same, but stored in a byte instead of a nibble
50 * @param tile TileIndex of the tile to evaluate
51 * @param ind_tile northernmost tile of the industry
53 uint32
GetRelativePosition(TileIndex tile
, TileIndex ind_tile
)
55 byte x
= TileX(tile
) - TileX(ind_tile
);
56 byte y
= TileY(tile
) - TileY(ind_tile
);
58 return ((y
& 0xF) << 20) | ((x
& 0xF) << 16) | (y
<< 8) | x
;
61 /* virtual */ uint32
IndustryTileScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
64 /* Construction state of the tile: a value between 0 and 3 */
65 case 0x40: return (IsTileType(this->tile
, MP_INDUSTRY
)) ? GetIndustryConstructionStage(this->tile
) : 0;
68 case 0x41: return GetTerrainType(this->tile
);
70 /* Current town zone of the tile in the nearest town */
71 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile
, UINT_MAX
), this->tile
);
73 /* Relative position */
74 case 0x43: return GetRelativePosition(this->tile
, this->industry
->location
.tile
);
76 /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
77 case 0x44: return IsTileType(this->tile
, MP_INDUSTRY
) ? GetAnimationFrame(this->tile
) : 0;
79 /* Land info of nearby tiles */
80 case 0x60: return GetNearbyIndustryTileInformation(parameter
, this->tile
,
81 this->industry
== nullptr ? (IndustryID
)INVALID_INDUSTRY
: this->industry
->index
, true, this->ro
.grffile
->grf_version
>= 8);
83 /* Animation stage of nearby tiles */
85 TileIndex tile
= GetNearbyTile(parameter
, this->tile
);
86 if (IsTileType(tile
, MP_INDUSTRY
) && Industry::GetByTile(tile
) == this->industry
) {
87 return GetAnimationFrame(tile
);
92 /* Get industry tile ID at offset */
93 case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter
, this->tile
), this->industry
, this->ro
.grffile
->grfid
);
96 Debug(grf
, 1, "Unhandled industry tile variable 0x{:X}", variable
);
102 /* virtual */ uint32
IndustryTileScopeResolver::GetRandomBits() const
104 assert(this->industry
!= nullptr && IsValidTile(this->tile
));
105 assert(this->industry
->index
== INVALID_INDUSTRY
|| IsTileType(this->tile
, MP_INDUSTRY
));
107 return (this->industry
->index
!= INVALID_INDUSTRY
) ? GetIndustryRandomBits(this->tile
) : 0;
110 /* virtual */ uint32
IndustryTileScopeResolver::GetTriggers() const
112 assert(this->industry
!= nullptr && IsValidTile(this->tile
));
113 assert(this->industry
->index
== INVALID_INDUSTRY
|| IsTileType(this->tile
, MP_INDUSTRY
));
114 if (this->industry
->index
== INVALID_INDUSTRY
) return 0;
115 return GetIndustryTriggers(this->tile
);
119 * Get the associated NewGRF file from the industry graphics.
120 * @param gfx Graphics to query.
121 * @return Grf file associated with the graphics, if any.
123 static const GRFFile
*GetIndTileGrffile(IndustryGfx gfx
)
125 const IndustryTileSpec
*its
= GetIndustryTileSpec(gfx
);
126 return (its
!= nullptr) ? its
->grf_prop
.grffile
: nullptr;
130 * Constructor of the industry tiles scope resolver.
131 * @param gfx Graphics of the industry.
132 * @param tile %Tile of the industry.
133 * @param indus %Industry owning the tile.
134 * @param callback Callback ID.
135 * @param callback_param1 First parameter (var 10) of the callback.
136 * @param callback_param2 Second parameter (var 18) of the callback.
138 IndustryTileResolverObject::IndustryTileResolverObject(IndustryGfx gfx
, TileIndex tile
, Industry
*indus
,
139 CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
140 : ResolverObject(GetIndTileGrffile(gfx
), callback
, callback_param1
, callback_param2
),
141 indtile_scope(*this, indus
, tile
),
142 ind_scope(*this, tile
, indus
, indus
->type
),
145 this->root_spritegroup
= GetIndustryTileSpec(gfx
)->grf_prop
.spritegroup
[0];
148 GrfSpecFeature
IndustryTileResolverObject::GetFeature() const
150 return GSF_INDUSTRYTILES
;
153 uint32
IndustryTileResolverObject::GetDebugID() const
155 return GetIndustryTileSpec(gfx
)->grf_prop
.local_id
;
158 static void IndustryDrawTileLayout(const TileInfo
*ti
, const TileLayoutSpriteGroup
*group
, byte rnd_colour
, byte stage
, IndustryGfx gfx
)
160 const DrawTileSprites
*dts
= group
->ProcessRegisters(&stage
);
162 SpriteID image
= dts
->ground
.sprite
;
163 PaletteID pal
= dts
->ground
.pal
;
165 if (HasBit(image
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) image
+= stage
;
166 if (HasBit(pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) pal
+= stage
;
168 if (GB(image
, 0, SPRITE_WIDTH
) != 0) {
169 /* If the ground sprite is the default flat water sprite, draw also canal/river borders
170 * Do not do this if the tile's WaterClass is 'land'. */
171 if (image
== SPR_FLAT_WATER_TILE
&& IsTileOnWater(ti
->tile
)) {
172 DrawWaterClassGround(ti
);
174 DrawGroundSprite(image
, GroundSpritePaletteTransform(image
, pal
, GENERAL_SPRITE_COLOUR(rnd_colour
)));
178 DrawNewGRFTileSeq(ti
, dts
, TO_INDUSTRIES
, stage
, GENERAL_SPRITE_COLOUR(rnd_colour
));
181 uint16
GetIndustryTileCallback(CallbackID callback
, uint32 param1
, uint32 param2
, IndustryGfx gfx_id
, Industry
*industry
, TileIndex tile
)
183 assert(industry
!= nullptr && IsValidTile(tile
));
184 assert(industry
->index
== INVALID_INDUSTRY
|| IsTileType(tile
, MP_INDUSTRY
));
186 IndustryTileResolverObject
object(gfx_id
, tile
, industry
, callback
, param1
, param2
);
187 return object
.ResolveCallback();
190 bool DrawNewIndustryTile(TileInfo
*ti
, Industry
*i
, IndustryGfx gfx
, const IndustryTileSpec
*inds
)
192 if (ti
->tileh
!= SLOPE_FLAT
) {
193 bool draw_old_one
= true;
194 if (HasBit(inds
->callback_mask
, CBM_INDT_DRAW_FOUNDATIONS
)) {
195 /* Called to determine the type (if any) of foundation to draw for industry tile */
196 uint32 callback_res
= GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS
, 0, 0, gfx
, i
, ti
->tile
);
197 if (callback_res
!= CALLBACK_FAILED
) draw_old_one
= ConvertBooleanCallback(inds
->grf_prop
.grffile
, CBID_INDTILE_DRAW_FOUNDATIONS
, callback_res
);
200 if (draw_old_one
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
203 IndustryTileResolverObject
object(gfx
, ti
->tile
, i
);
205 const SpriteGroup
*group
= object
.Resolve();
206 if (group
== nullptr || group
->type
!= SGT_TILELAYOUT
) return false;
208 /* Limit the building stage to the number of stages supplied. */
209 const TileLayoutSpriteGroup
*tlgroup
= (const TileLayoutSpriteGroup
*)group
;
210 byte stage
= GetIndustryConstructionStage(ti
->tile
);
211 IndustryDrawTileLayout(ti
, tlgroup
, i
->random_colour
, stage
, gfx
);
215 extern bool IsSlopeRefused(Slope current
, Slope refused
);
218 * Check the slope of a tile of a new industry.
219 * @param ind_base_tile Base tile of the industry.
220 * @param ind_tile Tile to check.
221 * @param its Tile specification.
222 * @param type Industry type.
223 * @param gfx Gfx of the tile.
224 * @param layout_index Layout.
225 * @param initial_random_bits Random bits of industry after construction
226 * @param founder Industry founder
227 * @param creation_type The circumstances the industry is created under.
228 * @return Succeeded or failed command.
230 CommandCost
PerformIndustryTileSlopeCheck(TileIndex ind_base_tile
, TileIndex ind_tile
, const IndustryTileSpec
*its
, IndustryType type
, IndustryGfx gfx
, size_t layout_index
, uint16 initial_random_bits
, Owner founder
, IndustryAvailabilityCallType creation_type
)
233 ind
.index
= INVALID_INDUSTRY
;
234 ind
.location
.tile
= ind_base_tile
;
237 ind
.random
= initial_random_bits
;
238 ind
.founder
= founder
;
240 uint16 callback_res
= GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK
, 0, creation_type
<< 8 | (uint32
)layout_index
, gfx
, &ind
, ind_tile
);
241 if (callback_res
== CALLBACK_FAILED
) {
242 if (!IsSlopeRefused(GetTileSlope(ind_tile
), its
->slopes_refused
)) return CommandCost();
243 return_cmd_error(STR_ERROR_SITE_UNSUITABLE
);
245 if (its
->grf_prop
.grffile
->grf_version
< 7) {
246 if (callback_res
!= 0) return CommandCost();
247 return_cmd_error(STR_ERROR_SITE_UNSUITABLE
);
250 return GetErrorMessageFromLocationCallbackResult(callback_res
, its
->grf_prop
.grffile
, STR_ERROR_SITE_UNSUITABLE
);
253 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
254 uint16
GetSimpleIndustryCallback(CallbackID callback
, uint32 param1
, uint32 param2
, const IndustryTileSpec
*spec
, Industry
*ind
, TileIndex tile
, int extra_data
)
256 return GetIndustryTileCallback(callback
, param1
, param2
, spec
- GetIndustryTileSpec(0), ind
, tile
);
259 /** Helper class for animation control. */
260 struct IndustryAnimationBase
: public AnimationBase
<IndustryAnimationBase
, IndustryTileSpec
, Industry
, int, GetSimpleIndustryCallback
> {
261 static const CallbackID cb_animation_speed
= CBID_INDTILE_ANIMATION_SPEED
;
262 static const CallbackID cb_animation_next_frame
= CBID_INDTILE_ANIM_NEXT_FRAME
;
264 static const IndustryTileCallbackMask cbm_animation_speed
= CBM_INDT_ANIM_SPEED
;
265 static const IndustryTileCallbackMask cbm_animation_next_frame
= CBM_INDT_ANIM_NEXT_FRAME
;
268 void AnimateNewIndustryTile(TileIndex tile
)
270 const IndustryTileSpec
*itspec
= GetIndustryTileSpec(GetIndustryGfx(tile
));
271 if (itspec
== nullptr) return;
273 IndustryAnimationBase::AnimateTile(itspec
, Industry::GetByTile(tile
), tile
, (itspec
->special_flags
& INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS
) != 0);
276 bool StartStopIndustryTileAnimation(TileIndex tile
, IndustryAnimationTrigger iat
, uint32 random
)
278 const IndustryTileSpec
*itspec
= GetIndustryTileSpec(GetIndustryGfx(tile
));
280 if (!HasBit(itspec
->animation
.triggers
, iat
)) return false;
282 IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP
, itspec
, Industry::GetByTile(tile
), tile
, random
, iat
);
286 bool StartStopIndustryTileAnimation(const Industry
*ind
, IndustryAnimationTrigger iat
)
289 uint32 random
= Random();
290 for (TileIndex tile
: ind
->location
) {
291 if (ind
->TileBelongsToIndustry(tile
)) {
292 if (StartStopIndustryTileAnimation(tile
, iat
, random
)) {
293 SB(random
, 0, 16, Random());
304 * Trigger random triggers for an industry tile and reseed its random bits.
305 * @param tile Industry tile to trigger.
306 * @param trigger Trigger to trigger.
307 * @param ind Industry of the tile.
308 * @param[in,out] reseed_industry Collects bits to reseed for the industry.
310 static void DoTriggerIndustryTile(TileIndex tile
, IndustryTileTrigger trigger
, Industry
*ind
, uint32
&reseed_industry
)
312 assert(IsValidTile(tile
) && IsTileType(tile
, MP_INDUSTRY
));
314 IndustryGfx gfx
= GetIndustryGfx(tile
);
315 const IndustryTileSpec
*itspec
= GetIndustryTileSpec(gfx
);
317 if (itspec
->grf_prop
.spritegroup
[0] == nullptr) return;
319 IndustryTileResolverObject
object(gfx
, tile
, ind
, CBID_RANDOM_TRIGGER
);
320 object
.waiting_triggers
= GetIndustryTriggers(tile
) | trigger
;
321 SetIndustryTriggers(tile
, object
.waiting_triggers
); // store now for var 5F
323 const SpriteGroup
*group
= object
.Resolve();
324 if (group
== nullptr) return;
326 /* Store remaining triggers. */
327 SetIndustryTriggers(tile
, object
.GetRemainingTriggers());
329 /* Rerandomise tile bits */
330 byte new_random_bits
= Random();
331 byte random_bits
= GetIndustryRandomBits(tile
);
332 random_bits
&= ~object
.reseed
[VSG_SCOPE_SELF
];
333 random_bits
|= new_random_bits
& object
.reseed
[VSG_SCOPE_SELF
];
334 SetIndustryRandomBits(tile
, random_bits
);
335 MarkTileDirtyByTile(tile
);
337 reseed_industry
|= object
.reseed
[VSG_SCOPE_PARENT
];
341 * Reseeds the random bits of an industry.
342 * @param ind Industry.
343 * @param reseed Bits to reseed.
345 static void DoReseedIndustry(Industry
*ind
, uint32 reseed
)
347 if (reseed
== 0 || ind
== nullptr) return;
349 uint16 random_bits
= Random();
350 ind
->random
&= reseed
;
351 ind
->random
|= random_bits
& reseed
;
355 * Trigger a random trigger for a single industry tile.
356 * @param tile Industry tile to trigger.
357 * @param trigger Trigger to trigger.
359 void TriggerIndustryTile(TileIndex tile
, IndustryTileTrigger trigger
)
361 uint32 reseed_industry
= 0;
362 Industry
*ind
= Industry::GetByTile(tile
);
363 DoTriggerIndustryTile(tile
, trigger
, ind
, reseed_industry
);
364 DoReseedIndustry(ind
, reseed_industry
);
368 * Trigger a random trigger for all industry tiles.
369 * @param ind Industry to trigger.
370 * @param trigger Trigger to trigger.
372 void TriggerIndustry(Industry
*ind
, IndustryTileTrigger trigger
)
374 uint32 reseed_industry
= 0;
375 for (TileIndex tile
: ind
->location
) {
376 if (ind
->TileBelongsToIndustry(tile
)) {
377 DoTriggerIndustryTile(tile
, trigger
, ind
, reseed_industry
);
380 DoReseedIndustry(ind
, reseed_industry
);