newgrf_industrytiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_industrytiles.cpp 26388 2014-03-03 20:02:31Z frosch $ */
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 /* virtual */ uint32 IndustryTileScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00062 {
00063   switch (variable) {
00064     /* Construction state of the tile: a value between 0 and 3 */
00065     case 0x40: return (IsTileType(this->tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(this->tile) : 0;
00066 
00067     /* Terrain type */
00068     case 0x41: return GetTerrainType(this->tile);
00069 
00070     /* Current town zone of the tile in the nearest town */
00071     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(this->tile, UINT_MAX), this->tile);
00072 
00073     /* Relative position */
00074     case 0x43: return GetRelativePosition(this->tile, this->industry->location.tile);
00075 
00076     /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
00077     case 0x44: return IsTileType(this->tile, MP_INDUSTRY) ? GetAnimationFrame(this->tile) : 0;
00078 
00079     /* Land info of nearby tiles */
00080     case 0x60: return GetNearbyIndustryTileInformation(parameter, this->tile,
00081         this->industry == NULL ? (IndustryID)INVALID_INDUSTRY : this->industry->index, true, this->ro.grffile->grf_version >= 8);
00082 
00083     /* Animation stage of nearby tiles */
00084     case 0x61: {
00085       TileIndex tile = GetNearbyTile(parameter, this->tile);
00086       if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == this->industry) {
00087         return GetAnimationFrame(tile);
00088       }
00089       return UINT_MAX;
00090     }
00091 
00092     /* Get industry tile ID at offset */
00093     case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->tile), this->industry, this->ro.grffile->grfid);
00094   }
00095 
00096   DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);
00097 
00098   *available = false;
00099   return UINT_MAX;
00100 }
00101 
00102 /* virtual */ uint32 IndustryTileScopeResolver::GetRandomBits() const
00103 {
00104   assert(this->industry != NULL && IsValidTile(this->tile));
00105   assert(this->industry->index == INVALID_INDUSTRY || IsTileType(this->tile, MP_INDUSTRY));
00106 
00107   return (this->industry->index != INVALID_INDUSTRY) ? GetIndustryRandomBits(this->tile) : 0;
00108 }
00109 
00110 /* virtual */ uint32 IndustryTileScopeResolver::GetTriggers() const
00111 {
00112   assert(this->industry != NULL && IsValidTile(this->tile));
00113   assert(this->industry->index == INVALID_INDUSTRY || IsTileType(this->tile, MP_INDUSTRY));
00114   if (this->industry->index == INVALID_INDUSTRY) return 0;
00115   return GetIndustryTriggers(this->tile);
00116 }
00117 
00118 /* virtual */ void IndustryTileScopeResolver::SetTriggers(int triggers) const
00119 {
00120   assert(this->industry != NULL && this->industry->index != INVALID_INDUSTRY && IsValidTile(this->tile) && IsTileType(this->tile, MP_INDUSTRY));
00121   SetIndustryTriggers(this->tile, triggers);
00122 }
00123 
00129 static const GRFFile *GetIndTileGrffile(IndustryGfx gfx)
00130 {
00131   const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
00132   return (its != NULL) ? its->grf_prop.grffile : NULL;
00133 }
00134 
00144 IndustryTileResolverObject::IndustryTileResolverObject(IndustryGfx gfx, TileIndex tile, Industry *indus,
00145       CallbackID callback, uint32 callback_param1, uint32 callback_param2)
00146   : ResolverObject(GetIndTileGrffile(gfx), callback, callback_param1, callback_param2),
00147   indtile_scope(*this, indus, tile),
00148   ind_scope(*this, tile, indus, indus->type)
00149 {
00150   this->root_spritegroup = GetIndustryTileSpec(gfx)->grf_prop.spritegroup[0];
00151 }
00152 
00159 IndustryTileScopeResolver::IndustryTileScopeResolver(ResolverObject &ro, Industry *industry, TileIndex tile) : ScopeResolver(ro)
00160 {
00161   this->industry = industry;
00162   this->tile = tile;
00163 }
00164 
00165 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
00166 {
00167   const DrawTileSprites *dts = group->ProcessRegisters(&stage);
00168 
00169   SpriteID image = dts->ground.sprite;
00170   PaletteID pal  = dts->ground.pal;
00171 
00172   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00173   if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
00174 
00175   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00176     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00177      * Do not do this if the tile's WaterClass is 'land'. */
00178     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00179       DrawWaterClassGround(ti);
00180     } else {
00181       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00182     }
00183   }
00184 
00185   DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
00186 }
00187 
00188 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00189 {
00190   assert(industry != NULL && IsValidTile(tile));
00191   assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00192 
00193   IndustryTileResolverObject object(gfx_id, tile, industry, callback, param1, param2);
00194   return object.ResolveCallback();
00195 }
00196 
00197 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00198 {
00199   if (ti->tileh != SLOPE_FLAT) {
00200     bool draw_old_one = true;
00201     if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00202       /* Called to determine the type (if any) of foundation to draw for industry tile */
00203       uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00204       if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(inds->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res);
00205     }
00206 
00207     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00208   }
00209 
00210   IndustryTileResolverObject object(gfx, ti->tile, i);
00211 
00212   const SpriteGroup *group = object.Resolve();
00213   if (group == NULL || group->type != SGT_TILELAYOUT) return false;
00214 
00215   /* Limit the building stage to the number of stages supplied. */
00216   const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00217   byte stage = GetIndustryConstructionStage(ti->tile);
00218   IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00219   return true;
00220 }
00221 
00222 extern bool IsSlopeRefused(Slope current, Slope refused);
00223 
00237 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)
00238 {
00239   Industry ind;
00240   ind.index = INVALID_INDUSTRY;
00241   ind.location.tile = ind_base_tile;
00242   ind.location.w = 0;
00243   ind.type = type;
00244   ind.random = initial_random_bits;
00245   ind.founder = founder;
00246 
00247   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
00248   if (callback_res == CALLBACK_FAILED) {
00249     if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
00250     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00251   }
00252   if (its->grf_prop.grffile->grf_version < 7) {
00253     if (callback_res != 0) return CommandCost();
00254     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00255   }
00256 
00257   return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile, STR_ERROR_SITE_UNSUITABLE);
00258 }
00259 
00260 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00261 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile, int extra_data)
00262 {
00263   return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
00264 }
00265 
00267 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback> {
00268   static const CallbackID cb_animation_speed      = CBID_INDTILE_ANIMATION_SPEED;
00269   static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
00270 
00271   static const IndustryTileCallbackMask cbm_animation_speed      = CBM_INDT_ANIM_SPEED;
00272   static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
00273 };
00274 
00275 void AnimateNewIndustryTile(TileIndex tile)
00276 {
00277   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00278   if (itspec == NULL) return;
00279 
00280   IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
00281 }
00282 
00283 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00284 {
00285   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00286 
00287   if (!HasBit(itspec->animation.triggers, iat)) return false;
00288 
00289   IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
00290   return true;
00291 }
00292 
00293 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00294 {
00295   bool ret = true;
00296   uint32 random = Random();
00297   TILE_AREA_LOOP(tile, ind->location) {
00298     if (ind->TileBelongsToIndustry(tile)) {
00299       if (StartStopIndustryTileAnimation(tile, iat, random)) {
00300         SB(random, 0, 16, Random());
00301       } else {
00302         ret = false;
00303       }
00304     }
00305   }
00306 
00307   return ret;
00308 }
00309 
00317 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
00318 {
00319   assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00320 
00321   IndustryGfx gfx = GetIndustryGfx(tile);
00322   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00323 
00324   if (itspec->grf_prop.spritegroup[0] == NULL) return;
00325 
00326   IndustryTileResolverObject object(gfx, tile, ind, CBID_RANDOM_TRIGGER);
00327   object.trigger = trigger;
00328 
00329   const SpriteGroup *group = object.Resolve();
00330   if (group == NULL) return;
00331 
00332   byte new_random_bits = Random();
00333   byte random_bits = GetIndustryRandomBits(tile);
00334   random_bits &= ~object.reseed[VSG_SCOPE_SELF];
00335   random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
00336   SetIndustryRandomBits(tile, random_bits);
00337   MarkTileDirtyByTile(tile);
00338 
00339   reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
00340 }
00341 
00347 static void DoReseedIndustry(Industry *ind, uint32 reseed)
00348 {
00349   if (reseed == 0 || ind == NULL) return;
00350 
00351   uint16 random_bits = Random();
00352   ind->random &= reseed;
00353   ind->random |= random_bits & reseed;
00354 }
00355 
00361 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00362 {
00363   uint32 reseed_industry = 0;
00364   Industry *ind = Industry::GetByTile(tile);
00365   DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00366   DoReseedIndustry(ind, reseed_industry);
00367 }
00368 
00374 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00375 {
00376   uint32 reseed_industry = 0;
00377   TILE_AREA_LOOP(tile, ind->location) {
00378     if (ind->TileBelongsToIndustry(tile)) {
00379       DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00380     }
00381   }
00382   DoReseedIndustry(ind, reseed_industry);
00383 }
00384