newgrf_industrytiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_industrytiles.cpp 26085 2013-11-24 14:41:19Z 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 }
00151 
00158 IndustryTileScopeResolver::IndustryTileScopeResolver(ResolverObject &ro, Industry *industry, TileIndex tile) : ScopeResolver(ro)
00159 {
00160   this->industry = industry;
00161   this->tile = tile;
00162 }
00163 
00164 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
00165 {
00166   const DrawTileSprites *dts = group->ProcessRegisters(&stage);
00167 
00168   SpriteID image = dts->ground.sprite;
00169   PaletteID pal  = dts->ground.pal;
00170 
00171   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00172   if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage;
00173 
00174   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00175     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00176      * Do not do this if the tile's WaterClass is 'land'. */
00177     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00178       DrawWaterClassGround(ti);
00179     } else {
00180       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00181     }
00182   }
00183 
00184   DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
00185 }
00186 
00187 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00188 {
00189   assert(industry != NULL && IsValidTile(tile));
00190   assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00191 
00192   IndustryTileResolverObject object(gfx_id, tile, industry, callback, param1, param2);
00193   const SpriteGroup *group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], object);
00194   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00195 
00196   return group->GetCallbackResult();
00197 }
00198 
00199 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00200 {
00201   if (ti->tileh != SLOPE_FLAT) {
00202     bool draw_old_one = true;
00203     if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00204       /* Called to determine the type (if any) of foundation to draw for industry tile */
00205       uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00206       if (callback_res != CALLBACK_FAILED) draw_old_one = ConvertBooleanCallback(inds->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res);
00207     }
00208 
00209     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00210   }
00211 
00212   IndustryTileResolverObject object(gfx, ti->tile, i);
00213 
00214   const SpriteGroup *group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], object);
00215   if (group == NULL || group->type != SGT_TILELAYOUT) return false;
00216 
00217   /* Limit the building stage to the number of stages supplied. */
00218   const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00219   byte stage = GetIndustryConstructionStage(ti->tile);
00220   IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00221   return true;
00222 }
00223 
00224 extern bool IsSlopeRefused(Slope current, Slope refused);
00225 
00239 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)
00240 {
00241   Industry ind;
00242   ind.index = INVALID_INDUSTRY;
00243   ind.location.tile = ind_base_tile;
00244   ind.location.w = 0;
00245   ind.type = type;
00246   ind.random = initial_random_bits;
00247   ind.founder = founder;
00248 
00249   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
00250   if (callback_res == CALLBACK_FAILED) {
00251     if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
00252     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00253   }
00254   if (its->grf_prop.grffile->grf_version < 7) {
00255     if (callback_res != 0) return CommandCost();
00256     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00257   }
00258 
00259   return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
00260 }
00261 
00262 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00263 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile, int extra_data)
00264 {
00265   return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
00266 }
00267 
00269 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback> {
00270   static const CallbackID cb_animation_speed      = CBID_INDTILE_ANIMATION_SPEED;
00271   static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
00272 
00273   static const IndustryTileCallbackMask cbm_animation_speed      = CBM_INDT_ANIM_SPEED;
00274   static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
00275 };
00276 
00277 void AnimateNewIndustryTile(TileIndex tile)
00278 {
00279   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00280   if (itspec == NULL) return;
00281 
00282   IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
00283 }
00284 
00285 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00286 {
00287   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00288 
00289   if (!HasBit(itspec->animation.triggers, iat)) return false;
00290 
00291   IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
00292   return true;
00293 }
00294 
00295 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00296 {
00297   bool ret = true;
00298   uint32 random = Random();
00299   TILE_AREA_LOOP(tile, ind->location) {
00300     if (ind->TileBelongsToIndustry(tile)) {
00301       if (StartStopIndustryTileAnimation(tile, iat, random)) {
00302         SB(random, 0, 16, Random());
00303       } else {
00304         ret = false;
00305       }
00306     }
00307   }
00308 
00309   return ret;
00310 }
00311 
00319 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
00320 {
00321   assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00322 
00323   IndustryGfx gfx = GetIndustryGfx(tile);
00324   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00325 
00326   if (itspec->grf_prop.spritegroup[0] == NULL) return;
00327 
00328   IndustryTileResolverObject object(gfx, tile, ind, CBID_RANDOM_TRIGGER);
00329   object.trigger = trigger;
00330 
00331   const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], object);
00332   if (group == NULL) return;
00333 
00334   byte new_random_bits = Random();
00335   byte random_bits = GetIndustryRandomBits(tile);
00336   random_bits &= ~object.reseed[VSG_SCOPE_SELF];
00337   random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
00338   SetIndustryRandomBits(tile, random_bits);
00339   MarkTileDirtyByTile(tile);
00340 
00341   reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
00342 }
00343 
00349 static void DoReseedIndustry(Industry *ind, uint32 reseed)
00350 {
00351   if (reseed == 0 || ind == NULL) return;
00352 
00353   uint16 random_bits = Random();
00354   ind->random &= reseed;
00355   ind->random |= random_bits & reseed;
00356 }
00357 
00363 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00364 {
00365   uint32 reseed_industry = 0;
00366   Industry *ind = Industry::GetByTile(tile);
00367   DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00368   DoReseedIndustry(ind, reseed_industry);
00369 }
00370 
00376 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00377 {
00378   uint32 reseed_industry = 0;
00379   TILE_AREA_LOOP(tile, ind->location) {
00380     if (ind->TileBelongsToIndustry(tile)) {
00381       DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00382     }
00383   }
00384   DoReseedIndustry(ind, reseed_industry);
00385 }
00386