4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file newgrf_industrytiles.cpp NewGRF handling of industry tiles. */
14 #include "landscape.h"
15 #include "newgrf_industrytiles.h"
16 #include "newgrf_sound.h"
19 #include "command_func.h"
21 #include "newgrf_animation_base.h"
23 #include "table/strings.h"
25 #include "safeguards.h"
28 * Based on newhouses equivalent, but adapted for newindustries
29 * @param parameter from callback. It's in fact a pair of coordinates
30 * @param tile TileIndex from which the callback was initiated
31 * @param index of the industry been queried for
32 * @param signed_offsets Are the x and y offset encoded in parameter signed?
33 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
34 * @return a construction of bits obeying the newgrf format
36 uint32
GetNearbyIndustryTileInformation(byte parameter
, TileIndex tile
, IndustryID index
, bool signed_offsets
, bool grf_version8
)
38 if (parameter
!= 0) tile
= GetNearbyTile(parameter
, tile
, signed_offsets
); // only perform if it is required
39 bool is_same_industry
= (IsTileType(tile
, MP_INDUSTRY
) && GetIndustryIndex(tile
) == index
);
41 return GetNearbyTileInformation(tile
, grf_version8
) | (is_same_industry
? 1 : 0) << 8;
45 * This is the position of the tile relative to the northernmost tile of the industry.
48 * x the x offset from the northernmost tile
49 * XX same, but stored in a byte instead of a nibble
50 * y the y offset from the northernmost tile
51 * YY same, but stored in a byte instead of a nibble
52 * @param tile TileIndex of the tile to evaluate
53 * @param ind_tile northernmost tile of the industry
55 uint32
GetRelativePosition(TileIndex tile
, TileIndex ind_tile
)
57 byte x
= TileX(tile
) - TileX(ind_tile
);
58 byte y
= TileY(tile
) - TileY(ind_tile
);
60 return ((y
& 0xF) << 20) | ((x
& 0xF) << 16) | (y
<< 8) | x
;
63 /* virtual */ uint32
IndustryTileScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
66 /* Construction state of the tile: a value between 0 and 3 */
67 case 0x40: return (IsTileType(this->tile
, MP_INDUSTRY
)) ? GetIndustryConstructionStage(this->tile
) : 0;
70 case 0x41: return GetTerrainType(this->tile
);
72 /* Current town zone of the tile in the nearest town */
73 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile
, UINT_MAX
), this->tile
);
75 /* Relative position */
76 case 0x43: return GetRelativePosition(this->tile
, this->industry
->location
.tile
);
78 /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
79 case 0x44: return IsTileType(this->tile
, MP_INDUSTRY
) ? GetAnimationFrame(this->tile
) : 0;
81 /* Land info of nearby tiles */
82 case 0x60: return GetNearbyIndustryTileInformation(parameter
, this->tile
,
83 this->industry
== NULL
? (IndustryID
)INVALID_INDUSTRY
: this->industry
->index
, true, this->ro
.grffile
->grf_version
>= 8);
85 /* Animation stage of nearby tiles */
87 TileIndex tile
= GetNearbyTile(parameter
, this->tile
);
88 if (IsTileType(tile
, MP_INDUSTRY
) && Industry::GetByTile(tile
) == this->industry
) {
89 return GetAnimationFrame(tile
);
94 /* Get industry tile ID at offset */
95 case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter
, this->tile
), this->industry
, this->ro
.grffile
->grfid
);
98 DEBUG(grf
, 1, "Unhandled industry tile variable 0x%X", variable
);
104 /* virtual */ uint32
IndustryTileScopeResolver::GetRandomBits() const
106 assert(this->industry
!= NULL
&& IsValidTile(this->tile
));
107 assert(this->industry
->index
== INVALID_INDUSTRY
|| IsTileType(this->tile
, MP_INDUSTRY
));
109 return (this->industry
->index
!= INVALID_INDUSTRY
) ? GetIndustryRandomBits(this->tile
) : 0;
112 /* virtual */ uint32
IndustryTileScopeResolver::GetTriggers() const
114 assert(this->industry
!= NULL
&& IsValidTile(this->tile
));
115 assert(this->industry
->index
== INVALID_INDUSTRY
|| IsTileType(this->tile
, MP_INDUSTRY
));
116 if (this->industry
->index
== INVALID_INDUSTRY
) return 0;
117 return GetIndustryTriggers(this->tile
);
121 * Get the associated NewGRF file from the industry graphics.
122 * @param gfx Graphics to query.
123 * @return Grf file associated with the graphics, if any.
125 static const GRFFile
*GetIndTileGrffile(IndustryGfx gfx
)
127 const IndustryTileSpec
*its
= GetIndustryTileSpec(gfx
);
128 return (its
!= NULL
) ? its
->grf_prop
.grffile
: NULL
;
132 * Constructor of the industry tiles scope resolver.
133 * @param gfx Graphics of the industry.
134 * @param tile %Tile of the industry.
135 * @param indus %Industry owning the tile.
136 * @param callback Callback ID.
137 * @param callback_param1 First parameter (var 10) of the callback.
138 * @param callback_param2 Second parameter (var 18) of the callback.
140 IndustryTileResolverObject::IndustryTileResolverObject(IndustryGfx gfx
, TileIndex tile
, Industry
*indus
,
141 CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
142 : ResolverObject(GetIndTileGrffile(gfx
), callback
, callback_param1
, callback_param2
),
143 indtile_scope(*this, indus
, tile
),
144 ind_scope(*this, tile
, indus
, indus
->type
)
146 this->root_spritegroup
= GetIndustryTileSpec(gfx
)->grf_prop
.spritegroup
[0];
149 static void IndustryDrawTileLayout(const TileInfo
*ti
, const TileLayoutSpriteGroup
*group
, byte rnd_colour
, byte stage
, IndustryGfx gfx
)
151 const DrawTileSprites
*dts
= group
->ProcessRegisters(&stage
);
153 SpriteID image
= dts
->ground
.sprite
;
154 PaletteID pal
= dts
->ground
.pal
;
156 if (HasBit(image
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) image
+= stage
;
157 if (HasBit(pal
, SPRITE_MODIFIER_CUSTOM_SPRITE
)) pal
+= stage
;
159 if (GB(image
, 0, SPRITE_WIDTH
) != 0) {
160 /* If the ground sprite is the default flat water sprite, draw also canal/river borders
161 * Do not do this if the tile's WaterClass is 'land'. */
162 if (image
== SPR_FLAT_WATER_TILE
&& IsTileOnWater(ti
->tile
)) {
163 DrawWaterClassGround(ti
);
165 DrawGroundSprite(image
, GroundSpritePaletteTransform(image
, pal
, GENERAL_SPRITE_COLOUR(rnd_colour
)));
169 DrawOverlay(ti
, MP_INDUSTRY
);
171 DrawNewGRFTileSeq(ti
, dts
, TO_INDUSTRIES
, stage
, GENERAL_SPRITE_COLOUR(rnd_colour
));
174 uint16
GetIndustryTileCallback(CallbackID callback
, uint32 param1
, uint32 param2
, IndustryGfx gfx_id
, Industry
*industry
, TileIndex tile
)
176 assert(industry
!= NULL
&& IsValidTile(tile
));
177 assert(industry
->index
== INVALID_INDUSTRY
|| IsTileType(tile
, MP_INDUSTRY
));
179 IndustryTileResolverObject
object(gfx_id
, tile
, industry
, callback
, param1
, param2
);
180 return object
.ResolveCallback();
183 bool DrawNewIndustryTile(TileInfo
*ti
, Industry
*i
, IndustryGfx gfx
, const IndustryTileSpec
*inds
)
185 if (ti
->tileh
!= SLOPE_FLAT
) {
186 bool draw_old_one
= true;
187 if (HasBit(inds
->callback_mask
, CBM_INDT_DRAW_FOUNDATIONS
)) {
188 /* Called to determine the type (if any) of foundation to draw for industry tile */
189 uint32 callback_res
= GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS
, 0, 0, gfx
, i
, ti
->tile
);
190 if (callback_res
!= CALLBACK_FAILED
) draw_old_one
= ConvertBooleanCallback(inds
->grf_prop
.grffile
, CBID_INDTILE_DRAW_FOUNDATIONS
, callback_res
);
193 if (draw_old_one
) DrawFoundation(ti
, FOUNDATION_LEVELED
);
196 IndustryTileResolverObject
object(gfx
, ti
->tile
, i
);
198 const SpriteGroup
*group
= object
.Resolve();
199 if (group
== NULL
|| group
->type
!= SGT_TILELAYOUT
) return false;
201 /* Limit the building stage to the number of stages supplied. */
202 const TileLayoutSpriteGroup
*tlgroup
= (const TileLayoutSpriteGroup
*)group
;
203 byte stage
= GetIndustryConstructionStage(ti
->tile
);
204 IndustryDrawTileLayout(ti
, tlgroup
, i
->random_colour
, stage
, gfx
);
208 extern bool IsSlopeRefused(Slope current
, Slope refused
);
211 * Check the slope of a tile of a new industry.
212 * @param ind_base_tile Base tile of the industry.
213 * @param ind_tile Tile to check.
214 * @param its Tile specification.
215 * @param type Industry type.
216 * @param gfx Gfx of the tile.
217 * @param itspec_index Layout.
218 * @param initial_random_bits Random bits of industry after construction
219 * @param founder Industry founder
220 * @param creation_type The circumstances the industry is created under.
221 * @return Succeeded or failed command.
223 CommandCost
PerformIndustryTileSlopeCheck(TileIndex ind_base_tile
, TileIndex ind_tile
, const IndustryTileSpec
*its
, IndustryType type
, IndustryGfx gfx
, uint itspec_index
, uint16 initial_random_bits
, Owner founder
, IndustryAvailabilityCallType creation_type
)
226 ind
.index
= INVALID_INDUSTRY
;
227 ind
.location
.tile
= ind_base_tile
;
230 ind
.random
= initial_random_bits
;
231 ind
.founder
= founder
;
233 uint16 callback_res
= GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK
, 0, creation_type
<< 8 | itspec_index
, gfx
, &ind
, ind_tile
);
234 if (callback_res
== CALLBACK_FAILED
) {
235 if (!IsSlopeRefused(GetTileSlope(ind_tile
), its
->slopes_refused
)) return CommandCost();
236 return_cmd_error(STR_ERROR_SITE_UNSUITABLE
);
238 if (its
->grf_prop
.grffile
->grf_version
< 7) {
239 if (callback_res
!= 0) return CommandCost();
240 return_cmd_error(STR_ERROR_SITE_UNSUITABLE
);
243 return GetErrorMessageFromLocationCallbackResult(callback_res
, its
->grf_prop
.grffile
, STR_ERROR_SITE_UNSUITABLE
);
246 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
247 uint16
GetSimpleIndustryCallback(CallbackID callback
, uint32 param1
, uint32 param2
, const IndustryTileSpec
*spec
, Industry
*ind
, TileIndex tile
, int extra_data
)
249 return GetIndustryTileCallback(callback
, param1
, param2
, spec
- GetIndustryTileSpec(0), ind
, tile
);
252 /** Helper class for animation control. */
253 struct IndustryAnimationBase
: public AnimationBase
<IndustryAnimationBase
, IndustryTileSpec
, Industry
, int, GetSimpleIndustryCallback
> {
254 static const CallbackID cb_animation_speed
= CBID_INDTILE_ANIMATION_SPEED
;
255 static const CallbackID cb_animation_next_frame
= CBID_INDTILE_ANIM_NEXT_FRAME
;
257 static const IndustryTileCallbackMask cbm_animation_speed
= CBM_INDT_ANIM_SPEED
;
258 static const IndustryTileCallbackMask cbm_animation_next_frame
= CBM_INDT_ANIM_NEXT_FRAME
;
261 void AnimateNewIndustryTile(TileIndex tile
)
263 const IndustryTileSpec
*itspec
= GetIndustryTileSpec(GetIndustryGfx(tile
));
264 if (itspec
== NULL
) return;
266 IndustryAnimationBase::AnimateTile(itspec
, Industry::GetByTile(tile
), tile
, (itspec
->special_flags
& INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS
) != 0);
269 bool StartStopIndustryTileAnimation(TileIndex tile
, IndustryAnimationTrigger iat
, uint32 random
)
271 const IndustryTileSpec
*itspec
= GetIndustryTileSpec(GetIndustryGfx(tile
));
273 if (!HasBit(itspec
->animation
.triggers
, iat
)) return false;
275 IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP
, itspec
, Industry::GetByTile(tile
), tile
, random
, iat
);
279 bool StartStopIndustryTileAnimation(const Industry
*ind
, IndustryAnimationTrigger iat
)
282 uint32 random
= Random();
283 TILE_AREA_LOOP(tile
, ind
->location
) {
284 if (ind
->TileBelongsToIndustry(tile
)) {
285 if (StartStopIndustryTileAnimation(tile
, iat
, random
)) {
286 SB(random
, 0, 16, Random());
297 * Trigger random triggers for an industry tile and reseed its random bits.
298 * @param tile Industry tile to trigger.
299 * @param trigger Trigger to trigger.
300 * @param ind Industry of the tile.
301 * @param [in,out] reseed_industry Collects bits to reseed for the industry.
303 static void DoTriggerIndustryTile(TileIndex tile
, IndustryTileTrigger trigger
, Industry
*ind
, uint32
&reseed_industry
)
305 assert(IsValidTile(tile
) && IsTileType(tile
, MP_INDUSTRY
));
307 IndustryGfx gfx
= GetIndustryGfx(tile
);
308 const IndustryTileSpec
*itspec
= GetIndustryTileSpec(gfx
);
310 if (itspec
->grf_prop
.spritegroup
[0] == NULL
) return;
312 IndustryTileResolverObject
object(gfx
, tile
, ind
, CBID_RANDOM_TRIGGER
);
313 object
.waiting_triggers
= GetIndustryTriggers(tile
) | trigger
;
314 SetIndustryTriggers(tile
, object
.waiting_triggers
); // store now for var 5F
316 const SpriteGroup
*group
= object
.Resolve();
317 if (group
== NULL
) return;
319 /* Store remaining triggers. */
320 SetIndustryTriggers(tile
, object
.GetRemainingTriggers());
322 /* Rerandomise tile bits */
323 byte new_random_bits
= Random();
324 byte random_bits
= GetIndustryRandomBits(tile
);
325 random_bits
&= ~object
.reseed
[VSG_SCOPE_SELF
];
326 random_bits
|= new_random_bits
& object
.reseed
[VSG_SCOPE_SELF
];
327 SetIndustryRandomBits(tile
, random_bits
);
328 MarkTileDirtyByTile(tile
, ZOOM_LVL_DRAW_MAP
);
330 reseed_industry
|= object
.reseed
[VSG_SCOPE_PARENT
];
334 * Reseeds the random bits of an industry.
335 * @param ind Industry.
336 * @param reseed Bits to reseed.
338 static void DoReseedIndustry(Industry
*ind
, uint32 reseed
)
340 if (reseed
== 0 || ind
== NULL
) return;
342 uint16 random_bits
= Random();
343 ind
->random
&= reseed
;
344 ind
->random
|= random_bits
& reseed
;
348 * Trigger a random trigger for a single industry tile.
349 * @param tile Industry tile to trigger.
350 * @param trigger Trigger to trigger.
352 void TriggerIndustryTile(TileIndex tile
, IndustryTileTrigger trigger
)
354 uint32 reseed_industry
= 0;
355 Industry
*ind
= Industry::GetByTile(tile
);
356 DoTriggerIndustryTile(tile
, trigger
, ind
, reseed_industry
);
357 DoReseedIndustry(ind
, reseed_industry
);
361 * Trigger a random trigger for all industry tiles.
362 * @param ind Industry to trigger.
363 * @param trigger Trigger to trigger.
365 void TriggerIndustry(Industry
*ind
, IndustryTileTrigger trigger
)
367 uint32 reseed_industry
= 0;
368 TILE_AREA_LOOP(tile
, ind
->location
) {
369 if (ind
->TileBelongsToIndustry(tile
)) {
370 DoTriggerIndustryTile(tile
, trigger
, ind
, reseed_industry
);
373 DoReseedIndustry(ind
, reseed_industry
);