Silence uninitialised variable warnings in SSE blitters
[openttd/fttd.git] / src / newgrf_industrytiles.cpp
blob8f266e424c5d95af07efa646299b4d4413c766bd
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file newgrf_industrytiles.cpp NewGRF handling of industry tiles. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "landscape.h"
15 #include "newgrf_industrytiles.h"
16 #include "newgrf_sound.h"
17 #include "industry.h"
18 #include "town.h"
19 #include "command_func.h"
20 #include "water.h"
21 #include "newgrf_animation_base.h"
22 #include "map/slope.h"
24 #include "table/strings.h"
26 /**
27 * Based on newhouses equivalent, but adapted for newindustries
28 * @param parameter from callback. It's in fact a pair of coordinates
29 * @param tile TileIndex from which the callback was initiated
30 * @param index of the industry been queried for
31 * @param signed_offsets Are the x and y offset encoded in parameter signed?
32 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
33 * @return a construction of bits obeying the newgrf format
35 uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets, bool grf_version8)
37 if (parameter != 0) tile = GetNearbyTile(parameter, tile, signed_offsets); // only perform if it is required
38 bool is_same_industry = (IsIndustryTile(tile) && GetIndustryIndex(tile) == index);
40 return GetNearbyTileInformation(tile, grf_version8) | (is_same_industry ? 1 : 0) << 8;
43 /**
44 * This is the position of the tile relative to the northernmost tile of the industry.
45 * Format: 00yxYYXX
46 * Variable Content
47 * x the x offset from the northernmost tile
48 * XX same, but stored in a byte instead of a nibble
49 * y the y offset from the northernmost tile
50 * YY same, but stored in a byte instead of a nibble
51 * @param tile TileIndex of the tile to evaluate
52 * @param ind_tile northernmost tile of the industry
54 uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
56 byte x = TileX(tile) - TileX(ind_tile);
57 byte y = TileY(tile) - TileY(ind_tile);
59 return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
62 /* virtual */ uint32 IndustryTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
64 switch (variable) {
65 /* Construction state of the tile: a value between 0 and 3 */
66 case 0x40: return (IsIndustryTile(this->tile)) ? GetIndustryConstructionStage(this->tile) : 0;
68 /* Terrain type */
69 case 0x41: return GetTerrainType(this->tile);
71 /* Current town zone of the tile in the nearest town */
72 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
74 /* Relative position */
75 case 0x43: return GetRelativePosition(this->tile, this->industry->location.tile);
77 /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
78 case 0x44: return IsIndustryTile(this->tile) ? GetAnimationFrame(this->tile) : 0;
80 /* Land info of nearby tiles */
81 case 0x60: return GetNearbyIndustryTileInformation(parameter, this->tile,
82 this->industry == NULL ? (IndustryID)INVALID_INDUSTRY : this->industry->index, true, this->ro.grffile->grf_version >= 8);
84 /* Animation stage of nearby tiles */
85 case 0x61: {
86 TileIndex tile = GetNearbyTile(parameter, this->tile);
87 if (IsIndustryTile(tile) && Industry::GetByTile(tile) == this->industry) {
88 return GetAnimationFrame(tile);
90 return UINT_MAX;
93 /* Get industry tile ID at offset */
94 case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->tile), this->industry, this->ro.grffile->grfid);
97 DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);
99 *available = false;
100 return UINT_MAX;
103 /* virtual */ uint32 IndustryTileScopeResolver::GetRandomBits() const
105 assert(this->industry != NULL && IsValidTile(this->tile));
106 assert(this->industry->index == INVALID_INDUSTRY || IsIndustryTile(this->tile));
108 return (this->industry->index != INVALID_INDUSTRY) ? GetIndustryRandomBits(this->tile) : 0;
111 /* virtual */ uint32 IndustryTileScopeResolver::GetTriggers() const
113 assert(this->industry != NULL && IsValidTile(this->tile));
114 assert(this->industry->index == INVALID_INDUSTRY || IsIndustryTile(this->tile));
115 if (this->industry->index == INVALID_INDUSTRY) return 0;
116 return GetIndustryTriggers(this->tile);
119 /* virtual */ void IndustryTileScopeResolver::SetTriggers(int triggers) const
121 assert(this->industry != NULL && this->industry->index != INVALID_INDUSTRY && IsValidTile(this->tile) && IsIndustryTile(this->tile));
122 SetIndustryTriggers(this->tile, triggers);
126 * Get the associated NewGRF file from the industry graphics.
127 * @param gfx Graphics to query.
128 * @return Grf file associated with the graphics, if any.
130 static const GRFFile *GetIndTileGrffile(IndustryGfx gfx)
132 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
133 return (its != NULL) ? its->grf_prop.grffile : NULL;
137 * Constructor of the industry tiles scope resolver.
138 * @param gfx Graphics of the industry.
139 * @param tile %Tile of the industry.
140 * @param indus %Industry owning the tile.
141 * @param callback Callback ID.
142 * @param callback_param1 First parameter (var 10) of the callback.
143 * @param callback_param2 Second parameter (var 18) of the callback.
145 IndustryTileResolverObject::IndustryTileResolverObject(IndustryGfx gfx, TileIndex tile, Industry *indus,
146 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
147 : ResolverObject(GetIndTileGrffile(gfx), callback, callback_param1, callback_param2),
148 indtile_scope(*this, indus, tile),
149 ind_scope(*this, tile, indus, indus->type)
154 * Constructor of the scope resolver for the industry tile.
155 * @param ro Surrounding resolver.
156 * @param industry %Industry owning the tile.
157 * @param tile %Tile of the industry.
159 IndustryTileScopeResolver::IndustryTileScopeResolver(ResolverObject &ro, Industry *industry, TileIndex tile) : ScopeResolver(ro)
161 this->industry = industry;
162 this->tile = tile;
165 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
167 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
169 SpriteID image = dts->ground.sprite;
170 PaletteID pal = dts->ground.pal;
172 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
173 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
175 if (GB(image, 0, SPRITE_WIDTH) != 0) {
176 /* If the ground sprite is the default flat water sprite, draw also canal/river borders
177 * Do not do this if the tile's WaterClass is 'land'. */
178 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
179 DrawWaterClassGround(ti);
180 } else {
181 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
185 DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
188 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
190 assert(industry != NULL && IsValidTile(tile));
191 assert(industry->index == INVALID_INDUSTRY || IsIndustryTile(tile));
193 IndustryTileResolverObject object(gfx_id, tile, industry, callback, param1, param2);
194 const SpriteGroup *group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], object);
195 if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
197 return group->GetCallbackResult();
200 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
202 if (ti->tileh != SLOPE_FLAT) {
203 bool draw_old_one = true;
204 if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
205 /* Called to determine the type (if any) of foundation to draw for industry tile */
206 uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
207 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(inds->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res);
210 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
213 IndustryTileResolverObject object(gfx, ti->tile, i);
215 const SpriteGroup *group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], object);
216 if (group == NULL || group->type != SGT_TILELAYOUT) return false;
218 /* Limit the building stage to the number of stages supplied. */
219 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
220 byte stage = GetIndustryConstructionStage(ti->tile);
221 IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
222 return true;
225 extern bool IsSlopeRefused(Slope current, Slope refused);
228 * Check the slope of a tile of a new industry.
229 * @param ind_base_tile Base tile of the industry.
230 * @param ind_tile Tile to check.
231 * @param its Tile specification.
232 * @param type Industry type.
233 * @param gfx Gfx of the tile.
234 * @param itspec_index Layout.
235 * @param initial_random_bits Random bits of industry after construction
236 * @param founder Industry founder
237 * @param creation_type The circumstances the industry is created under.
238 * @return Succeeded or failed command.
240 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)
242 Industry ind;
243 ind.index = INVALID_INDUSTRY;
244 ind.location.tile = ind_base_tile;
245 ind.location.w = 0;
246 ind.type = type;
247 ind.random = initial_random_bits;
248 ind.founder = founder;
250 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
251 if (callback_res == CALLBACK_FAILED) {
252 if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
253 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
255 if (its->grf_prop.grffile->grf_version < 7) {
256 if (callback_res != 0) return CommandCost();
257 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
260 return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
263 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
264 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile, int extra_data)
266 return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
269 /** Helper class for animation control. */
270 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback> {
271 static const CallbackID cb_animation_speed = CBID_INDTILE_ANIMATION_SPEED;
272 static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
274 static const IndustryTileCallbackMask cbm_animation_speed = CBM_INDT_ANIM_SPEED;
275 static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
278 void AnimateNewIndustryTile(TileIndex tile)
280 const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
281 if (itspec == NULL) return;
283 IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
286 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
288 const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
290 if (!HasBit(itspec->animation.triggers, iat)) return false;
292 IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
293 return true;
296 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
298 bool ret = true;
299 uint32 random = Random();
300 TILE_AREA_LOOP(tile, ind->location) {
301 if (ind->TileBelongsToIndustry(tile)) {
302 if (StartStopIndustryTileAnimation(tile, iat, random)) {
303 SB(random, 0, 16, Random());
304 } else {
305 ret = false;
310 return ret;
314 * Trigger random triggers for an industry tile and reseed its random bits.
315 * @param tile Industry tile to trigger.
316 * @param trigger Trigger to trigger.
317 * @param ind Industry of the tile.
318 * @param [in,out] reseed_industry Collects bits to reseed for the industry.
320 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
322 assert(IsValidTile(tile) && IsIndustryTile(tile));
324 IndustryGfx gfx = GetIndustryGfx(tile);
325 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
327 if (itspec->grf_prop.spritegroup[0] == NULL) return;
329 IndustryTileResolverObject object(gfx, tile, ind, CBID_RANDOM_TRIGGER);
330 object.trigger = trigger;
332 const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], object);
333 if (group == NULL) return;
335 byte new_random_bits = Random();
336 byte random_bits = GetIndustryRandomBits(tile);
337 random_bits &= ~object.reseed[VSG_SCOPE_SELF];
338 random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
339 SetIndustryRandomBits(tile, random_bits);
340 MarkTileDirtyByTile(tile);
342 reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
346 * Reseeds the random bits of an industry.
347 * @param ind Industry.
348 * @param reseed Bits to reseed.
350 static void DoReseedIndustry(Industry *ind, uint32 reseed)
352 if (reseed == 0 || ind == NULL) return;
354 uint16 random_bits = Random();
355 ind->random &= reseed;
356 ind->random |= random_bits & reseed;
360 * Trigger a random trigger for a single industry tile.
361 * @param tile Industry tile to trigger.
362 * @param trigger Trigger to trigger.
364 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
366 uint32 reseed_industry = 0;
367 Industry *ind = Industry::GetByTile(tile);
368 DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
369 DoReseedIndustry(ind, reseed_industry);
373 * Trigger a random trigger for all industry tiles.
374 * @param ind Industry to trigger.
375 * @param trigger Trigger to trigger.
377 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
379 uint32 reseed_industry = 0;
380 TILE_AREA_LOOP(tile, ind->location) {
381 if (ind->TileBelongsToIndustry(tile)) {
382 DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
385 DoReseedIndustry(ind, reseed_industry);