newgrf_industrytiles.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "viewport_func.h"
00015 #include "landscape.h"
00016 #include "newgrf.h"
00017 #include "newgrf_industrytiles.h"
00018 #include "newgrf_sound.h"
00019 #include "newgrf_text.h"
00020 #include "industry.h"
00021 #include "town.h"
00022 #include "command_func.h"
00023 #include "water.h"
00024 #include "sprite.h"
00025 #include "newgrf_animation_base.h"
00026 
00027 #include "table/strings.h"
00028 
00038 uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets, bool grf_version8)
00039 {
00040   if (parameter != 0) tile = GetNearbyTile(parameter, tile, signed_offsets); // only perform if it is required
00041   bool is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
00042 
00043   return GetNearbyTileInformation(tile, grf_version8) | (is_same_industry ? 1 : 0) << 8;
00044 }
00045 
00057 uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
00058 {
00059   byte x = TileX(tile) - TileX(ind_tile);
00060   byte y = TileY(tile) - TileY(ind_tile);
00061 
00062   return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
00063 }
00064 
00065 static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00066 {
00067   const Industry *inds = object->u.industry.ind;
00068   TileIndex tile       = object->u.industry.tile;
00069 
00070   if (object->scope == VSG_SCOPE_PARENT) {
00071     return IndustryGetVariable(object, variable, parameter, available);
00072   }
00073 
00074   switch (variable) {
00075     /* Construction state of the tile: a value between 0 and 3 */
00076     case 0x40: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
00077 
00078     /* Terrain type */
00079     case 0x41: return GetTerrainType(tile);
00080 
00081     /* Current town zone of the tile in the nearest town */
00082     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
00083 
00084     /* Relative position */
00085     case 0x43: return GetRelativePosition(tile, inds->location.tile);
00086 
00087     /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
00088     case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetAnimationFrame(tile) : 0;
00089 
00090     /* Land info of nearby tiles */
00091     case 0x60: return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index, true, object->grffile->grf_version >= 8);
00092 
00093     /* Animation stage of nearby tiles */
00094     case 0x61:
00095       tile = GetNearbyTile(parameter, tile);
00096       if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == inds) {
00097         return GetAnimationFrame(tile);
00098       }
00099       return UINT_MAX;
00100 
00101     /* Get industry tile ID at offset */
00102     case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds, object->grffile->grfid);
00103   }
00104 
00105   DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);
00106 
00107   *available = false;
00108   return UINT_MAX;
00109 }
00110 
00111 static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00112 {
00113   /* IndustryTile do not have 'real' groups.  Or do they?? */
00114   return NULL;
00115 }
00116 
00117 static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
00118 {
00119   const TileIndex tile = object->u.industry.tile;
00120   const Industry *ind = object->u.industry.ind;
00121   assert(ind != NULL && IsValidTile(tile));
00122   assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00123 
00124   return (object->scope == VSG_SCOPE_SELF) ?
00125       (ind->index != INVALID_INDUSTRY ? GetIndustryRandomBits(tile) : 0) :
00126       ind->random;
00127 }
00128 
00129 static uint32 IndustryTileGetTriggers(const ResolverObject *object)
00130 {
00131   const TileIndex tile = object->u.industry.tile;
00132   const Industry *ind = object->u.industry.ind;
00133   assert(ind != NULL && IsValidTile(tile));
00134   assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00135   if (ind->index == INVALID_INDUSTRY) return 0;
00136   return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : ind->random_triggers;
00137 }
00138 
00139 static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
00140 {
00141   const TileIndex tile = object->u.industry.tile;
00142   Industry *ind = object->u.industry.ind;
00143   assert(ind != NULL && ind->index != INVALID_INDUSTRY && IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00144 
00145   if (object->scope == VSG_SCOPE_SELF) {
00146     SetIndustryTriggers(tile, triggers);
00147   } else {
00148     ind->random_triggers = triggers;
00149   }
00150 }
00151 
00158 void IndustryTileStorePSA(ResolverObject *object, uint pos, int32 value)
00159 {
00160   Industry *ind = object->u.industry.ind;
00161   if (object->scope != VSG_SCOPE_PARENT || ind->index == INVALID_INDUSTRY) return;
00162 
00163   if (ind->psa == NULL) {
00164     /* There is no need to create a storage if the value is zero. */
00165     if (value == 0) return;
00166 
00167     /* Create storage on first modification. */
00168     const IndustrySpec *indsp = GetIndustrySpec(ind->type);
00169     uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
00170     assert(PersistentStorage::CanAllocateItem());
00171     ind->psa = new PersistentStorage(grfid);
00172   }
00173 
00174   ind->psa->StoreValue(pos, value);
00175 }
00176 
00177 static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
00178 {
00179   res->GetRandomBits = IndustryTileGetRandomBits;
00180   res->GetTriggers   = IndustryTileGetTriggers;
00181   res->SetTriggers   = IndustryTileSetTriggers;
00182   res->GetVariable   = IndustryTileGetVariable;
00183   res->ResolveReal   = IndustryTileResolveReal;
00184   res->StorePSA      = IndustryTileStorePSA;
00185 
00186   res->u.industry.tile = tile;
00187   res->u.industry.ind  = indus;
00188   res->u.industry.gfx  = gfx;
00189   res->u.industry.type = indus->type;
00190 
00191   res->callback        = CBID_NO_CALLBACK;
00192   res->callback_param1 = 0;
00193   res->callback_param2 = 0;
00194   res->ResetState();
00195 
00196   const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
00197   res->grffile         = (its != NULL ? its->grf_prop.grffile : NULL);
00198 }
00199 
00200 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
00201 {
00202   const DrawTileSprites *dts = group->ProcessRegisters(&stage);
00203 
00204   SpriteID image = dts->ground.sprite;
00205   PaletteID pal  = dts->ground.pal;
00206 
00207   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00208   if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
00209 
00210   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00211     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00212      * Do not do this if the tile's WaterClass is 'land'. */
00213     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00214       DrawWaterClassGround(ti);
00215     } else {
00216       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00217     }
00218   }
00219 
00220   DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
00221 }
00222 
00223 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00224 {
00225   ResolverObject object;
00226   const SpriteGroup *group;
00227 
00228   assert(industry != NULL && IsValidTile(tile));
00229   assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00230 
00231   NewIndustryTileResolver(&object, gfx_id, tile, industry);
00232   object.callback = callback;
00233   object.callback_param1 = param1;
00234   object.callback_param2 = param2;
00235 
00236   group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], &object);
00237   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00238 
00239   return group->GetCallbackResult();
00240 }
00241 
00242 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00243 {
00244   const SpriteGroup *group;
00245   ResolverObject object;
00246 
00247   if (ti->tileh != SLOPE_FLAT) {
00248     bool draw_old_one = true;
00249     if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00250       /* Called to determine the type (if any) of foundation to draw for industry tile */
00251       uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00252       if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(inds->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res);
00253     }
00254 
00255     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00256   }
00257 
00258   NewIndustryTileResolver(&object, gfx, ti->tile, i);
00259 
00260   group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], &object);
00261   if (group == NULL || group->type != SGT_TILELAYOUT) {
00262     return false;
00263   } else {
00264     /* Limit the building stage to the number of stages supplied. */
00265     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00266     byte stage = GetIndustryConstructionStage(ti->tile);
00267     IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00268     return true;
00269   }
00270 }
00271 
00272 extern bool IsSlopeRefused(Slope current, Slope refused);
00273 
00287 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)
00288 {
00289   Industry ind;
00290   ind.index = INVALID_INDUSTRY;
00291   ind.location.tile = ind_base_tile;
00292   ind.location.w = 0;
00293   ind.type = type;
00294   ind.random = initial_random_bits;
00295   ind.founder = founder;
00296 
00297   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
00298   if (callback_res == CALLBACK_FAILED) {
00299     if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
00300     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00301   }
00302   if (its->grf_prop.grffile->grf_version < 7) {
00303     if (callback_res != 0) return CommandCost();
00304     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00305   }
00306 
00307   return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
00308 }
00309 
00310 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00311 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile, int extra_data)
00312 {
00313   return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
00314 }
00315 
00317 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback> {
00318   static const CallbackID cb_animation_speed      = CBID_INDTILE_ANIMATION_SPEED;
00319   static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
00320 
00321   static const IndustryTileCallbackMask cbm_animation_speed      = CBM_INDT_ANIM_SPEED;
00322   static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
00323 };
00324 
00325 void AnimateNewIndustryTile(TileIndex tile)
00326 {
00327   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00328   if (itspec == NULL) return;
00329 
00330   IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
00331 }
00332 
00333 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00334 {
00335   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00336 
00337   if (!HasBit(itspec->animation.triggers, iat)) return false;
00338 
00339   IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
00340   return true;
00341 }
00342 
00343 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00344 {
00345   bool ret = true;
00346   uint32 random = Random();
00347   TILE_AREA_LOOP(tile, ind->location) {
00348     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00349       if (StartStopIndustryTileAnimation(tile, iat, random)) {
00350         SB(random, 0, 16, Random());
00351       } else {
00352         ret = false;
00353       }
00354     }
00355   }
00356 
00357   return ret;
00358 }
00359 
00367 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
00368 {
00369   ResolverObject object;
00370 
00371   assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00372 
00373   IndustryGfx gfx = GetIndustryGfx(tile);
00374   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00375 
00376   if (itspec->grf_prop.spritegroup[0] == NULL) return;
00377 
00378   NewIndustryTileResolver(&object, gfx, tile, ind);
00379 
00380   object.callback = CBID_RANDOM_TRIGGER;
00381   object.trigger = trigger;
00382 
00383   const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], &object);
00384   if (group == NULL) return;
00385 
00386   byte new_random_bits = Random();
00387   byte random_bits = GetIndustryRandomBits(tile);
00388   random_bits &= ~object.reseed[VSG_SCOPE_SELF];
00389   random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
00390   SetIndustryRandomBits(tile, random_bits);
00391   MarkTileDirtyByTile(tile);
00392 
00393   reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
00394 }
00395 
00401 static void DoReseedIndustry(Industry *ind, uint32 reseed)
00402 {
00403   if (reseed == 0 || ind == NULL) return;
00404 
00405   uint16 random_bits = Random();
00406   ind->random &= reseed;
00407   ind->random |= random_bits & reseed;
00408 }
00409 
00415 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00416 {
00417   uint32 reseed_industry = 0;
00418   Industry *ind = Industry::GetByTile(tile);
00419   DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00420   DoReseedIndustry(ind, reseed_industry);
00421 }
00422 
00428 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00429 {
00430   uint32 reseed_industry = 0;
00431   TILE_AREA_LOOP(tile, ind->location) {
00432     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00433       DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00434     }
00435   }
00436   DoReseedIndustry(ind, reseed_industry);
00437 }
00438 
00444 void GetIndustryTileResolver(ResolverObject *ro, uint index)
00445 {
00446   NewIndustryTileResolver(ro, GetIndustryGfx(index), index, Industry::GetByTile(index));
00447 }