newgrf_industrytiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_industrytiles.cpp 24900 2013-01-08 22:46:42Z planetmaker $ */
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) {
00216     return false;
00217   } else {
00218     /* Limit the building stage to the number of stages supplied. */
00219     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00220     byte stage = GetIndustryConstructionStage(ti->tile);
00221     IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00222     return true;
00223   }
00224 }
00225 
00226 extern bool IsSlopeRefused(Slope current, Slope refused);
00227 
00241 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)
00242 {
00243   Industry ind;
00244   ind.index = INVALID_INDUSTRY;
00245   ind.location.tile = ind_base_tile;
00246   ind.location.w = 0;
00247   ind.type = type;
00248   ind.random = initial_random_bits;
00249   ind.founder = founder;
00250 
00251   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
00252   if (callback_res == CALLBACK_FAILED) {
00253     if (!IsSlopeRefused(GetTileSlope(ind_tile), its->slopes_refused)) return CommandCost();
00254     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00255   }
00256   if (its->grf_prop.grffile->grf_version < 7) {
00257     if (callback_res != 0) return CommandCost();
00258     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00259   }
00260 
00261   return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
00262 }
00263 
00264 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00265 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, Industry *ind, TileIndex tile, int extra_data)
00266 {
00267   return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), ind, tile);
00268 }
00269 
00271 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, int, GetSimpleIndustryCallback> {
00272   static const CallbackID cb_animation_speed      = CBID_INDTILE_ANIMATION_SPEED;
00273   static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
00274 
00275   static const IndustryTileCallbackMask cbm_animation_speed      = CBM_INDT_ANIM_SPEED;
00276   static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
00277 };
00278 
00279 void AnimateNewIndustryTile(TileIndex tile)
00280 {
00281   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00282   if (itspec == NULL) return;
00283 
00284   IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
00285 }
00286 
00287 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00288 {
00289   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00290 
00291   if (!HasBit(itspec->animation.triggers, iat)) return false;
00292 
00293   IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
00294   return true;
00295 }
00296 
00297 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00298 {
00299   bool ret = true;
00300   uint32 random = Random();
00301   TILE_AREA_LOOP(tile, ind->location) {
00302     if (ind->TileBelongsToIndustry(tile)) {
00303       if (StartStopIndustryTileAnimation(tile, iat, random)) {
00304         SB(random, 0, 16, Random());
00305       } else {
00306         ret = false;
00307       }
00308     }
00309   }
00310 
00311   return ret;
00312 }
00313 
00321 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
00322 {
00323   assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00324 
00325   IndustryGfx gfx = GetIndustryGfx(tile);
00326   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00327 
00328   if (itspec->grf_prop.spritegroup[0] == NULL) return;
00329 
00330   IndustryTileResolverObject object(gfx, tile, ind, CBID_RANDOM_TRIGGER);
00331   object.trigger = trigger;
00332 
00333   const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], &object);
00334   if (group == NULL) return;
00335 
00336   byte new_random_bits = Random();
00337   byte random_bits = GetIndustryRandomBits(tile);
00338   random_bits &= ~object.reseed[VSG_SCOPE_SELF];
00339   random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
00340   SetIndustryRandomBits(tile, random_bits);
00341   MarkTileDirtyByTile(tile);
00342 
00343   reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
00344 }
00345 
00351 static void DoReseedIndustry(Industry *ind, uint32 reseed)
00352 {
00353   if (reseed == 0 || ind == NULL) return;
00354 
00355   uint16 random_bits = Random();
00356   ind->random &= reseed;
00357   ind->random |= random_bits & reseed;
00358 }
00359 
00365 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00366 {
00367   uint32 reseed_industry = 0;
00368   Industry *ind = Industry::GetByTile(tile);
00369   DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00370   DoReseedIndustry(ind, reseed_industry);
00371 }
00372 
00378 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00379 {
00380   uint32 reseed_industry = 0;
00381   TILE_AREA_LOOP(tile, ind->location) {
00382     if (ind->TileBelongsToIndustry(tile)) {
00383       DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00384     }
00385   }
00386   DoReseedIndustry(ind, reseed_industry);
00387 }
00388