newgrf_industrytiles.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_industrytiles.cpp 19053 2010-02-07 12:04:44Z 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 "variables.h"
00014 #include "debug.h"
00015 #include "viewport_func.h"
00016 #include "landscape.h"
00017 #include "newgrf.h"
00018 #include "newgrf_commons.h"
00019 #include "newgrf_industries.h"
00020 #include "newgrf_industrytiles.h"
00021 #include "newgrf_sound.h"
00022 #include "newgrf_text.h"
00023 #include "industry.h"
00024 #include "functions.h"
00025 #include "town.h"
00026 #include "command_func.h"
00027 #include "animated_tile_func.h"
00028 #include "water.h"
00029 #include "sprite.h"
00030 
00031 #include "table/strings.h"
00032 
00040 uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index)
00041 {
00042   if (parameter != 0) tile = GetNearbyTile(parameter, tile); // only perform if it is required
00043   bool is_same_industry = (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == index);
00044 
00045   return GetNearbyTileInformation(tile) | (is_same_industry ? 1 : 0) << 8;
00046 }
00047 
00058 static uint32 GetRelativePosition(TileIndex tile, TileIndex ind_tile)
00059 {
00060   byte x = TileX(tile) - TileX(ind_tile);
00061   byte y = TileY(tile) - TileY(ind_tile);
00062 
00063   return ((y & 0xF) << 20) | ((x & 0xF) << 16) | (y << 8) | x;
00064 }
00065 
00066 static uint32 IndustryTileGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00067 {
00068   const Industry *inds = object->u.industry.ind;
00069   TileIndex tile       = object->u.industry.tile;
00070 
00071   if (object->scope == VSG_SCOPE_PARENT) {
00072     return IndustryGetVariable(object, variable, parameter, available);
00073   }
00074 
00075   switch (variable) {
00076     /* Construction state of the tile: a value between 0 and 3 */
00077     case 0x40: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryConstructionStage(tile) : 0;
00078 
00079     /* Terrain type */
00080     case 0x41: return GetTerrainType(tile);
00081 
00082     /* Current town zone of the tile in the nearest town */
00083     case 0x42: return GetTownRadiusGroup(ClosestTownFromTile(tile, UINT_MAX), tile);
00084 
00085     /* Relative position */
00086     case 0x43: return GetRelativePosition(tile, inds->location.tile);
00087 
00088     /* Animation frame. Like house variable 46 but can contain anything 0..FF. */
00089     case 0x44: return (IsTileType(tile, MP_INDUSTRY)) ? GetIndustryAnimationState(tile) : 0;
00090 
00091     /* Land info of nearby tiles */
00092     case 0x60: return GetNearbyIndustryTileInformation(parameter, tile, inds == NULL ? (IndustryID)INVALID_INDUSTRY : inds->index);
00093 
00094     /* Animation stage of nearby tiles */
00095     case 0x61:
00096       tile = GetNearbyTile(parameter, tile);
00097       if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == inds) {
00098         return GetIndustryAnimationState(tile);
00099       }
00100       return UINT_MAX;
00101 
00102     /* Get industry tile ID at offset */
00103     case 0x62: return GetIndustryIDAtOffset(GetNearbyTile(parameter, tile), inds, object->grffile->grfid);
00104   }
00105 
00106   DEBUG(grf, 1, "Unhandled industry tile property 0x%X", variable);
00107 
00108   *available = false;
00109   return UINT_MAX;
00110 }
00111 
00112 static const SpriteGroup *IndustryTileResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00113 {
00114   /* IndustryTile do not have 'real' groups.  Or do they?? */
00115   return NULL;
00116 }
00117 
00118 static uint32 IndustryTileGetRandomBits(const ResolverObject *object)
00119 {
00120   const TileIndex tile = object->u.industry.tile;
00121   if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
00122   return (object->scope == VSG_SCOPE_SELF) ? GetIndustryRandomBits(tile) : Industry::GetByTile(tile)->random;
00123 }
00124 
00125 static uint32 IndustryTileGetTriggers(const ResolverObject *object)
00126 {
00127   const TileIndex tile = object->u.industry.tile;
00128   if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return 0;
00129   return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : Industry::GetByTile(tile)->random_triggers;
00130 }
00131 
00132 static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
00133 {
00134   const TileIndex tile = object->u.industry.tile;
00135   if (tile == INVALID_TILE || !IsTileType(tile, MP_INDUSTRY)) return;
00136 
00137   if (object->scope == VSG_SCOPE_SELF) {
00138     SetIndustryTriggers(tile, triggers);
00139   } else {
00140     Industry::GetByTile(tile)->random_triggers = triggers;
00141   }
00142 }
00143 
00144 static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
00145 {
00146   res->GetRandomBits = IndustryTileGetRandomBits;
00147   res->GetTriggers   = IndustryTileGetTriggers;
00148   res->SetTriggers   = IndustryTileSetTriggers;
00149   res->GetVariable   = IndustryTileGetVariable;
00150   res->ResolveReal   = IndustryTileResolveReal;
00151 
00152   res->psa             = &indus->psa;
00153   res->u.industry.tile = tile;
00154   res->u.industry.ind  = indus;
00155   res->u.industry.gfx  = gfx;
00156   res->u.industry.type = indus->type;
00157 
00158   res->callback        = CBID_NO_CALLBACK;
00159   res->callback_param1 = 0;
00160   res->callback_param2 = 0;
00161   res->last_value      = 0;
00162   res->trigger         = 0;
00163   res->reseed          = 0;
00164   res->count           = 0;
00165 
00166   const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
00167   res->grffile         = (its != NULL ? its->grf_prop.grffile : NULL);
00168 }
00169 
00170 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
00171 {
00172   const DrawTileSprites *dts = group->dts;
00173 
00174   SpriteID image = dts->ground.sprite;
00175   PaletteID pal  = dts->ground.pal;
00176 
00177   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
00178 
00179   if (GB(image, 0, SPRITE_WIDTH) != 0) {
00180     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
00181      * Do not do this if the tile's WaterClass is 'land'. */
00182     if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
00183       DrawWaterClassGround(ti);
00184     } else {
00185       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
00186     }
00187   }
00188 
00189   DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
00190 }
00191 
00192 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
00193 {
00194   ResolverObject object;
00195   const SpriteGroup *group;
00196 
00197   NewIndustryTileResolver(&object, gfx_id, tile, industry);
00198   object.callback = callback;
00199   object.callback_param1 = param1;
00200   object.callback_param2 = param2;
00201 
00202   group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
00203   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
00204 
00205   return group->GetCallbackResult();
00206 }
00207 
00208 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
00209 {
00210   const SpriteGroup *group;
00211   ResolverObject object;
00212 
00213   if (ti->tileh != SLOPE_FLAT) {
00214     bool draw_old_one = true;
00215     if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00216       /* Called to determine the type (if any) of foundation to draw for industry tile */
00217       uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
00218       draw_old_one = (callback_res != 0);
00219     }
00220 
00221     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
00222   }
00223 
00224   NewIndustryTileResolver(&object, gfx, ti->tile, i);
00225 
00226   group = SpriteGroup::Resolve(inds->grf_prop.spritegroup, &object);
00227   if (group == NULL || group->type != SGT_TILELAYOUT) {
00228     return false;
00229   } else {
00230     /* Limit the building stage to the number of stages supplied. */
00231     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
00232     byte stage = GetIndustryConstructionStage(ti->tile);
00233     stage = Clamp(stage - 4 + tlgroup->num_building_stages, 0, tlgroup->num_building_stages - 1);
00234     IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
00235     return true;
00236   }
00237 }
00238 
00239 extern bool IsSlopeRefused(Slope current, Slope refused);
00240 
00241 bool PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index)
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 
00249   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, itspec_index, gfx, &ind, ind_tile);
00250   if (callback_res == CALLBACK_FAILED) {
00251     return !IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused);
00252   }
00253   if (its->grf_prop.grffile->grf_version < 7) {
00254     return callback_res != 0;
00255   }
00256   if (callback_res == 0x400) return true;
00257 
00258   /* Copy some parameters from the registers to the error message text ref. stack */
00259   SwitchToErrorRefStack();
00260   PrepareTextRefStackUsage(4);
00261   SwitchToNormalRefStack();
00262 
00263   switch (callback_res) {
00264     case 0x401: _error_message = STR_ERROR_SITE_UNSUITABLE;                 return false;
00265     case 0x402: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST; return false;
00266     case 0x403: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT;     return false;
00267     default: _error_message = GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res); return false;
00268   }
00269 }
00270 
00271 void AnimateNewIndustryTile(TileIndex tile)
00272 {
00273   Industry *ind = Industry::GetByTile(tile);
00274   IndustryGfx gfx = GetIndustryGfx(tile);
00275   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00276   byte animation_speed = itspec->animation_speed;
00277 
00278   if (HasBit(itspec->callback_mask, CBM_INDT_ANIM_SPEED)) {
00279     uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
00280     if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
00281   }
00282 
00283   /* An animation speed of 2 means the animation frame changes 4 ticks, and
00284    * increasing this value by one doubles the wait. 0 is the minimum value
00285    * allowed for animation_speed, which corresponds to 30ms, and 16 is the
00286    * maximum, corresponding to around 33 minutes. */
00287   if ((_tick_counter % (1 << animation_speed)) != 0) return;
00288 
00289   bool frame_set_by_callback = false;
00290   byte frame = GetIndustryAnimationState(tile);
00291   uint16 num_frames = GB(itspec->animation_info, 0, 8);
00292 
00293   if (HasBit(itspec->callback_mask, CBM_INDT_ANIM_NEXT_FRAME)) {
00294     uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME,
00295         (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) ? Random() : 0, 0, gfx, ind, tile);
00296 
00297     if (callback_res != CALLBACK_FAILED) {
00298       frame_set_by_callback = true;
00299 
00300       switch (callback_res & 0xFF) {
00301         case 0xFF:
00302           DeleteAnimatedTile(tile);
00303           break;
00304         case 0xFE:
00305           /* Carry on as normal. */
00306           frame_set_by_callback = false;
00307           break;
00308         default:
00309           frame = callback_res & 0xFF;
00310           break;
00311       }
00312 
00313       /* If the lower 7 bits of the upper byte of the callback
00314        * result are not empty, it is a sound effect. */
00315       if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
00316     }
00317   }
00318 
00319   if (!frame_set_by_callback) {
00320     if (frame < num_frames) {
00321       frame++;
00322     } else if (frame == num_frames && GB(itspec->animation_info, 8, 8) == 1) {
00323       /* This animation loops, so start again from the beginning */
00324       frame = 0;
00325     } else {
00326       /* This animation doesn't loop, so stay here */
00327       DeleteAnimatedTile(tile);
00328     }
00329   }
00330 
00331   SetIndustryAnimationState(tile, frame);
00332   MarkTileDirtyByTile(tile);
00333 }
00334 
00335 static void ChangeIndustryTileAnimationFrame(const IndustryTileSpec *itspec, TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
00336 {
00337   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_START_STOP, random_bits, iat, gfx, ind, tile);
00338   if (callback_res == CALLBACK_FAILED) return;
00339 
00340   switch (callback_res & 0xFF) {
00341     case 0xFD: /* Do nothing. */         break;
00342     case 0xFE: AddAnimatedTile(tile);    break;
00343     case 0xFF: DeleteAnimatedTile(tile); break;
00344     default:
00345       SetIndustryAnimationState(tile, callback_res & 0xFF);
00346       AddAnimatedTile(tile);
00347       break;
00348   }
00349 
00350   /* If the lower 7 bits of the upper byte of the callback
00351    * result are not empty, it is a sound effect. */
00352   if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
00353 }
00354 
00355 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
00356 {
00357   IndustryGfx gfx = GetIndustryGfx(tile);
00358   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00359 
00360   if (!HasBit(itspec->animation_triggers, iat)) return false;
00361 
00362   Industry *ind = Industry::GetByTile(tile);
00363   ChangeIndustryTileAnimationFrame(itspec, tile, iat, random, gfx, ind);
00364   return true;
00365 }
00366 
00367 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
00368 {
00369   bool ret = true;
00370   uint32 random = Random();
00371   TILE_AREA_LOOP(tile, ind->location) {
00372     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00373       if (StartStopIndustryTileAnimation(tile, iat, random)) {
00374         SB(random, 0, 16, Random());
00375       } else {
00376         ret = false;
00377       }
00378     }
00379   }
00380 
00381   return ret;
00382 }
00383 
00384 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
00385 {
00386   ResolverObject object;
00387 
00388   IndustryGfx gfx = GetIndustryGfx(tile);
00389   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00390 
00391   if (itspec->grf_prop.spritegroup == NULL) return;
00392 
00393   NewIndustryTileResolver(&object, gfx, tile, ind);
00394 
00395   object.callback = CBID_RANDOM_TRIGGER;
00396   object.trigger = trigger;
00397 
00398   const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup, &object);
00399   if (group == NULL) return;
00400 
00401   byte new_random_bits = Random();
00402   byte random_bits = GetIndustryRandomBits(tile);
00403   random_bits &= ~object.reseed;
00404   random_bits |= new_random_bits & object.reseed;
00405   SetIndustryRandomBits(tile, random_bits);
00406   MarkTileDirtyByTile(tile);
00407 }
00408 
00409 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
00410 {
00411   DoTriggerIndustryTile(tile, trigger, Industry::GetByTile(tile));
00412 }
00413 
00414 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
00415 {
00416   TILE_AREA_LOOP(tile, ind->location) {
00417     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
00418       DoTriggerIndustryTile(tile, trigger, ind);
00419     }
00420   }
00421 }

Generated on Wed Mar 3 23:32:23 2010 for OpenTTD by  doxygen 1.6.1