OpenTTD: src/newgrf_industrytiles.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_industrytiles.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_industrytiles.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
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 */
9
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
23 #include "table/strings.h"
24
25 #include "safeguards.h"
26
36 uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets, bool grf_version8)
37 {
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);
40
41 return GetNearbyTileInformation(tile, grf_version8) | (is_same_industry ? 1 : 0) << 8;
42 }
43
55 uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
56 {
57 byte x = TileX(tile) - TileX(ind_tile);
58 byte y = TileY(tile) - TileY(ind_tile);
59
60 return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
61 }
62
63 /* virtual */ uint32 IndustryTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
64 {
65 switch (variable) {
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;
68
69 /* Terrain type */
70 case 0x41: return GetTerrainType(this->tile);
71
72 /* Current town zone of the tile in the nearest town */
73 case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
74
75 /* Relative position */
76 case 0x43: return GetRelativePosition(this->tile, this->industry->location.tile);
77
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;
80
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);
84
85 /* Animation stage of nearby tiles */
86 case 0x61: {
87 TileIndex tile = GetNearbyTile(parameter, this->tile);
88 if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == this->industry) {
89 return GetAnimationFrame(tile);
90 }
91 return UINT_MAX;
92 }
93
94 /* Get industry tile ID at offset */
95 case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->tile), this->industry, this->ro.grffile->grfid);
96 }
97
98 DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);
99
100 *available = false;
101 return UINT_MAX;
102 }
103
104 /* virtual */ uint32 IndustryTileScopeResolver::GetRandomBits() const
105 {
106 assert(this->industry != NULL && IsValidTile(this->tile));
107 assert(this->industry->index == INVALID_INDUSTRY || IsTileType(this->tile, MP_INDUSTRY));
108
109 return (this->industry->index != INVALID_INDUSTRY) ? GetIndustryRandomBits(this->tile) : 0;
110 }
111
112 /* virtual */ uint32 IndustryTileScopeResolver::GetTriggers() const
113 {
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);
118 }
119
120 /* virtual */ void IndustryTileScopeResolver::SetTriggers(int triggers) const
121 {
122 assert(this->industry != NULL && this->industry->index != INVALID_INDUSTRY && IsValidTile(this->tile) && IsTileType(this->tile, MP_INDUSTRY));
123 SetIndustryTriggers(this->tile, triggers);
124 }
125
131 static const GRFFile *GetIndTileGrffile(IndustryGfx gfx)
132 {
133 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
134 return (its != NULL) ? its->grf_prop.grffile : NULL;
135 }
136
146 IndustryTileResolverObject::IndustryTileResolverObject(IndustryGfx gfx, TileIndex tile, Industry *indus,
147 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
148 : ResolverObject(GetIndTileGrffile(gfx), callback, callback_param1, callback_param2),
149 indtile_scope(*this, indus, tile),
150 ind_scope(*this, tile, indus, indus->type)
151 {
152 this->root_spritegroup = GetIndustryTileSpec(gfx)->grf_prop.spritegroup[0];
153 }
154
161 IndustryTileScopeResolver::IndustryTileScopeResolver(ResolverObject &ro, Industry *industry, TileIndex tile) : ScopeResolver(ro)
162 {
163 this->industry = industry;
164 this->tile = tile;
165 }
166
167 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
168 {
169 const DrawTileSprites *dts = group->ProcessRegisters(&stage);
170
171 SpriteID image = dts->ground.sprite;
172 PaletteID pal = dts->ground.pal;
173
174 if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
175 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
176
177 if (GB(image, 0, SPRITE_WIDTH) != 0) {
178 /* If the ground sprite is the default flat water sprite, draw also canal/river borders
179 * Do not do this if the tile's WaterClass is 'land'. */
180 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
181 DrawWaterClassGround(ti);
182 } else {
183 DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
184 }
185 }
186
187 DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
188 }
189
190 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
191 {
192 assert(industry != NULL && IsValidTile(tile));
193 assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
194
195 IndustryTileResolverObject object(gfx_id, tile, industry, callback, param1, param2);
196 return object.ResolveCallback();
197 }
198
199 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
200 {
201 if (ti->tileh != SLOPE_FLAT) {
202 bool draw_old_one = true;
203 if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
204 /* Called to determine the type (if any) of foundation to draw for industry tile */
205 uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
206 if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(inds->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res);
207 }
208
209 if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
210 }
211
212 IndustryTileResolverObject object(gfx, ti->tile, i);
213
214 const SpriteGroup *group = object.Resolve();
215 if (group == NULL || group->type != SGT_TILELAYOUT) return false;
216
217 /* Limit the building stage to the number of stages supplied. */
218 const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
219 byte stage = GetIndustryConstructionStage(ti->tile);
220 IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
221 return true;
222 }
223
224 extern bool IsSlopeRefused(Slope current, Slope refused);
225
239 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)
240 {
241 Industry ind;
242 ind.index = INVALID_INDUSTRY;
243 ind.location.tile = ind_base_tile;
244 ind.location.w = 0;
245 ind.type = type;
246 ind.random = initial_random_bits;
247 ind.founder = founder;
248
249 uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
250 if (callback_res == CALLBACK_FAILED) {
251 if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
252 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
253 }
254 if (its->grf_prop.grffile->grf_version < 7) {
255 if (callback_res != 0) return CommandCost();
256 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
257 }
258
259 return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile, STR_ERROR_SITE_UNSUITABLE);
260 }
261
262 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
263 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile, int extra_data)
264 {
265 return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
266 }
267
269 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback> {
270 static const CallbackID cb_animation_speed = CBID_INDTILE_ANIMATION_SPEED;
271 static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
272
273 static const IndustryTileCallbackMask cbm_animation_speed = CBM_INDT_ANIM_SPEED;
274 static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
275 };
276
277 void AnimateNewIndustryTile(TileIndex tile)
278 {
279 const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
280 if (itspec == NULL) return;
281
282 IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
283 }
284
285 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
286 {
287 const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
288
289 if (!HasBit(itspec->animation.triggers, iat)) return false;
290
291 IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
292 return true;
293 }
294
295 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
296 {
297 bool ret = true;
298 uint32 random = Random();
299 TILE_AREA_LOOP(tile, ind->location) {
300 if (ind->TileBelongsToIndustry(tile)) {
301 if (StartStopIndustryTileAnimation(tile, iat, random)) {
302 SB(random, 0, 16, Random());
303 } else {
304 ret = false;
305 }
306 }
307 }
308
309 return ret;
310 }
311
319 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
320 {
321 assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
322
323 IndustryGfx gfx = GetIndustryGfx(tile);
324 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
325
326 if (itspec->grf_prop.spritegroup[0] == NULL) return;
327
328 IndustryTileResolverObject object(gfx, tile, ind, CBID_RANDOM_TRIGGER);
329 object.trigger = trigger;
330
331 const SpriteGroup *group = object.Resolve();
332 if (group == NULL) return;
333
334 byte new_random_bits = Random();
335 byte random_bits = GetIndustryRandomBits(tile);
336 random_bits &= ~object.reseed[VSG_SCOPE_SELF];
337 random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
338 SetIndustryRandomBits(tile, random_bits);
339 MarkTileDirtyByTile(tile);
340
341 reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
342 }
343
349 static void DoReseedIndustry(Industry *ind, uint32 reseed)
350 {
351 if (reseed == 0 || ind == NULL) return;
352
353 uint16 random_bits = Random();
354 ind->random &= reseed;
355 ind->random |= random_bits & reseed;
356 }
357
363 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
364 {
365 uint32 reseed_industry = 0;
366 Industry *ind = Industry::GetByTile(tile);
367 DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
368 DoReseedIndustry(ind, reseed_industry);
369 }
370
376 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
377 {
378 uint32 reseed_industry = 0;
379 TILE_AREA_LOOP(tile, ind->location) {
380 if (ind->TileBelongsToIndustry(tile)) {
381 DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
382 }
383 }
384 DoReseedIndustry(ind, reseed_industry);
385 }
386