newgrf_industrytiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_industrytiles.cpp 22838 2011-08-25 13:24:32Z rubidium $ */
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 
00037 uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets)
00038 {
00039   if (parameter != 0) tile = GetNearbyTile(parameter, tile, signed_offsets); // only perform if it is required
00040   bool is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
00041 
00042   return GetNearbyTileInformation(tile) | (is_same_industry ? 1 : 0) << 8;
00043 }
00044 
00056 uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
00057 {
00058   byte x = TileX(tile) - TileX(ind_tile);
00059   byte y = TileY(tile) - TileY(ind_tile);
00060 
00061   return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
00062 }
00063 
00064 static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00065 {
00066   const Industry *inds = object->u.industry.ind;
00067   TileIndex tile       = object->u.industry.tile;
00068 
00069   if (object->scope == VSG_SCOPE_PARENT) {
00070     return IndustryGetVariable(object, variable, parameter, available);
00071   }
00072 
00073   switch (variable) {
00074     /* Construction state of the tile: a value between 0 and 3 */
00075     case 0x40: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
00076 
00077     /* Terrain type */
00078     case 0x41: return GetTerrainType(tile);
00079 
00080     /* Current town zone of the tile in the nearest town */
00081     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
00082 
00083     /* Relative position */
00084     case 0x43: return GetRelativePosition(tile, inds->location.tile);
00085 
00086     /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
00087     case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetAnimationFrame(tile) : 0;
00088 
00089     /* Land info of nearby tiles */
00090     case 0x60: return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index);
00091 
00092     /* Animation stage of nearby tiles */
00093     case 0x61:
00094       tile = GetNearbyTile(parameter, tile);
00095       if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == inds) {
00096         return GetAnimationFrame(tile);
00097       }
00098       return UINT_MAX;
00099 
00100     /* Get industry tile ID at offset */
00101     case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds, object->grffile->grfid);
00102   }
00103 
00104   DEBUG(grf, 1, "Unhandled industry tile variable 0x%X", variable);
00105 
00106   *available = false;
00107   return UINT_MAX;
00108 }
00109 
00110 static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00111 {
00112   /* IndustryTile do not have 'real' groups.  Or do they?? */
00113   return NULL;
00114 }
00115 
00116 static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
00117 {
00118   const TileIndex tile = object->u.industry.tile;
00119   const Industry *ind = object->u.industry.ind;
00120   assert(ind != NULL && IsValidTile(tile));
00121   assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00122 
00123   return (object->scope == VSG_SCOPE_SELF) ?
00124       (ind->index != INVALID_INDUSTRY ? GetIndustryRandomBits(tile) : 0) :
00125       ind->random;
00126 }
00127 
00128 static uint32 IndustryTileGetTriggers(const ResolverObject *object)
00129 {
00130   const TileIndex tile = object->u.industry.tile;
00131   const Industry *ind = object->u.industry.ind;
00132   assert(ind != NULL && IsValidTile(tile));
00133   assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00134   if (ind->index == INVALID_INDUSTRY) return 0;
00135   return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : ind->random_triggers;
00136 }
00137 
00138 static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
00139 {
00140   const TileIndex tile = object->u.industry.tile;
00141   Industry *ind = object->u.industry.ind;
00142   assert(ind != NULL && ind->index != INVALID_INDUSTRY && IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00143 
00144   if (object->scope == VSG_SCOPE_SELF) {
00145     SetIndustryTriggers(tile, triggers);
00146   } else {
00147     ind->random_triggers = triggers;
00148   }
00149 }
00150 
00151 static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
00152 {
00153   res->GetRandomBits = IndustryTileGetRandomBits;
00154   res->GetTriggers   = IndustryTileGetTriggers;
00155   res->SetTriggers   = IndustryTileSetTriggers;
00156   res->GetVariable   = IndustryTileGetVariable;
00157   res->ResolveReal   = IndustryTileResolveReal;
00158 
00159   res->psa             = &indus->psa;
00160   res->u.industry.tile = tile;
00161   res->u.industry.ind  = indus;
00162   res->u.industry.gfx  = gfx;
00163   res->u.industry.type = indus->type;
00164 
00165   res->callback        = CBID_NO_CALLBACK;
00166   res->callback_param1 = 0;
00167   res->callback_param2 = 0;
00168   res->ResetState();
00169 
00170   const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
00171   res->grffile         = (its != NULL ? its->grf_prop.grffile : NULL);
00172 }
00173 
00174 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
00175 {
00176   const DrawTileSprites *dts = group->dts;
00177 
00178   SpriteID image = dts->ground.sprite;
00179   PaletteID pal  = dts->ground.pal;
00180 
00181   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00182 
00183   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00184     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00185      * Do not do this if the tile's WaterClass is 'land'. */
00186     if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00187       DrawWaterClassGround(ti);
00188     } else {
00189       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00190     }
00191   }
00192 
00193   DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
00194 }
00195 
00196 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00197 {
00198   ResolverObject object;
00199   const SpriteGroup *group;
00200 
00201   assert(industry != NULL && IsValidTile(tile));
00202   assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
00203 
00204   NewIndustryTileResolver(&object, gfx_id, tile, industry);
00205   object.callback = callback;
00206   object.callback_param1 = param1;
00207   object.callback_param2 = param2;
00208 
00209   group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup[0], &object);
00210   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00211 
00212   return group->GetCallbackResult();
00213 }
00214 
00215 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00216 {
00217   const SpriteGroup *group;
00218   ResolverObject object;
00219 
00220   if (ti->tileh != SLOPE_FLAT) {
00221     bool draw_old_one = true;
00222     if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00223       /* Called to determine the type (if any) of foundation to draw for industry tile */
00224       uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00225       draw_old_one = (callback_res != 0);
00226     }
00227 
00228     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00229   }
00230 
00231   NewIndustryTileResolver(&object, gfx, ti->tile, i);
00232 
00233   group = SpriteGroup::Resolve(inds->grf_prop.spritegroup[0], &object);
00234   if (group == NULL || group->type != SGT_TILELAYOUT) {
00235     return false;
00236   } else {
00237     /* Limit the building stage to the number of stages supplied. */
00238     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00239     byte stage = GetIndustryConstructionStage(ti->tile);
00240     stage = tlgroup->GetConstructionStageOffset(stage);
00241     IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00242     return true;
00243   }
00244 }
00245 
00246 extern bool IsSlopeRefused(Slope current, Slope refused);
00247 
00261 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)
00262 {
00263   Industry ind;
00264   ind.index = INVALID_INDUSTRY;
00265   ind.location.tile = ind_base_tile;
00266   ind.location.w = 0;
00267   ind.type = type;
00268   ind.random = initial_random_bits;
00269   ind.founder = founder;
00270 
00271   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, creation_type << 8 | itspec_index, gfx, &ind, ind_tile);
00272   if (callback_res == CALLBACK_FAILED) {
00273     if (!IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused)) return CommandCost();
00274     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00275   }
00276   if (its->grf_prop.grffile->grf_version < 7) {
00277     if (callback_res != 0) return CommandCost();
00278     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
00279   }
00280   if (callback_res == 0x400) return CommandCost();
00281 
00282   CommandCost res;
00283   switch (callback_res) {
00284     case 0x401: res = CommandCost(STR_ERROR_SITE_UNSUITABLE); break;
00285     case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
00286     case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
00287     default:    res = CommandCost(GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res)); break;
00288   }
00289 
00290   /* Copy some parameters from the registers to the error message text ref. stack */
00291   res.UseTextRefStack(4);
00292 
00293   return res;
00294 }
00295 
00296 /* Simple wrapper for GetHouseCallback to keep the animation unified. */
00297 uint16 GetSimpleIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, const IndustryTileSpec *spec, const Industry *ind, TileIndex tile)
00298 {
00299   return GetIndustryTileCallback(callback, param1, param2, spec - GetIndustryTileSpec(0), const_cast<Industry *>(ind), tile);
00300 }
00301 
00303 struct IndustryAnimationBase : public AnimationBase<IndustryAnimationBase, IndustryTileSpec, Industry, GetSimpleIndustryCallback> {
00304   static const CallbackID cb_animation_speed      = CBID_INDTILE_ANIMATION_SPEED;
00305   static const CallbackID cb_animation_next_frame = CBID_INDTILE_ANIM_NEXT_FRAME;
00306 
00307   static const IndustryTileCallbackMask cbm_animation_speed      = CBM_INDT_ANIM_SPEED;
00308   static const IndustryTileCallbackMask cbm_animation_next_frame = CBM_INDT_ANIM_NEXT_FRAME;
00309 };
00310 
00311 void AnimateNewIndustryTile(TileIndex tile)
00312 {
00313   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00314   if (itspec == NULL) return;
00315 
00316   IndustryAnimationBase::AnimateTile(itspec, Industry::GetByTile(tile), tile, (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) != 0);
00317 }
00318 
00319 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00320 {
00321   const IndustryTileSpec *itspec = GetIndustryTileSpec(GetIndustryGfx(tile));
00322 
00323   if (!HasBit(itspec->animation.triggers, iat)) return false;
00324 
00325   IndustryAnimationBase::ChangeAnimationFrame(CBID_INDTILE_ANIM_START_STOP, itspec, Industry::GetByTile(tile), tile, random, iat);
00326   return true;
00327 }
00328 
00329 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00330 {
00331   bool ret = true;
00332   uint32 random = Random();
00333   TILE_AREA_LOOP(tile, ind->location) {
00334     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00335       if (StartStopIndustryTileAnimation(tile, iat, random)) {
00336         SB(random, 0, 16, Random());
00337       } else {
00338         ret = false;
00339       }
00340     }
00341   }
00342 
00343   return ret;
00344 }
00345 
00353 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind, uint32 &reseed_industry)
00354 {
00355   ResolverObject object;
00356 
00357   assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
00358 
00359   IndustryGfx gfx = GetIndustryGfx(tile);
00360   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00361 
00362   if (itspec->grf_prop.spritegroup[0] == NULL) return;
00363 
00364   NewIndustryTileResolver(&object, gfx, tile, ind);
00365 
00366   object.callback = CBID_RANDOM_TRIGGER;
00367   object.trigger = trigger;
00368 
00369   const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup[0], &object);
00370   if (group == NULL) return;
00371 
00372   byte new_random_bits = Random();
00373   byte random_bits = GetIndustryRandomBits(tile);
00374   random_bits &= ~object.reseed[VSG_SCOPE_SELF];
00375   random_bits |= new_random_bits & object.reseed[VSG_SCOPE_SELF];
00376   SetIndustryRandomBits(tile, random_bits);
00377   MarkTileDirtyByTile(tile);
00378 
00379   reseed_industry |= object.reseed[VSG_SCOPE_PARENT];
00380 }
00381 
00387 static void DoReseedIndustry(Industry *ind, uint32 reseed)
00388 {
00389   if (reseed == 0 || ind == NULL) return;
00390 
00391   uint16 random_bits = Random();
00392   ind->random &= reseed;
00393   ind->random |= random_bits & reseed;
00394 }
00395 
00401 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00402 {
00403   uint32 reseed_industry = 0;
00404   Industry *ind = Industry::GetByTile(tile);
00405   DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00406   DoReseedIndustry(ind, reseed_industry);
00407 }
00408 
00414 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00415 {
00416   uint32 reseed_industry = 0;
00417   TILE_AREA_LOOP(tile, ind->location) {
00418     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00419       DoTriggerIndustryTile(tile, trigger, ind, reseed_industry);
00420     }
00421   }
00422   DoReseedIndustry(ind, reseed_industry);
00423 }
00424 
00430 void GetIndustryTileResolver(ResolverObject *ro, uint index)
00431 {
00432   NewIndustryTileResolver(ro, GetIndustryGfx(index), index, Industry::GetByTile(index));
00433 }