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