newgrf_industrytiles.cpp

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

Generated on Fri Dec 31 17:15:35 2010 for OpenTTD by  doxygen 1.6.1