# newgrf_industrytiles.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_industrytiles.cpp 19855 2010-05-18 21:30:56Z 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 "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   const Industry *ind = object->u.industry.ind;
    00122   assert(ind != NULL && IsValidTile(tile));
    00123   assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
    00124
    00125   return (object->scope == VSG_SCOPE_SELF) ?
    00126       (ind->index != INVALID_INDUSTRY ? GetIndustryRandomBits(tile) : 0) :
    00127       ind->random;
    00128 }
    00129
    00130 static uint32 IndustryTileGetTriggers(const ResolverObject *object)
    00131 {
    00132   const TileIndex tile = object->u.industry.tile;
    00133   const Industry *ind = object->u.industry.ind;
    00134   assert(ind != NULL && IsValidTile(tile));
    00135   assert(ind->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
    00136   if (ind->index == INVALID_INDUSTRY) return 0;
    00137   return (object->scope == VSG_SCOPE_SELF) ? GetIndustryTriggers(tile) : ind->random_triggers;
    00138 }
    00139
    00140 static void IndustryTileSetTriggers(const ResolverObject *object, int triggers)
    00141 {
    00142   const TileIndex tile = object->u.industry.tile;
    00143   Industry *ind = object->u.industry.ind;
    00144   assert(ind != NULL && ind->index != INVALID_INDUSTRY && IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
    00145
    00146   if (object->scope == VSG_SCOPE_SELF) {
    00147     SetIndustryTriggers(tile, triggers);
    00148   } else {
    00149     ind->random_triggers = triggers;
    00150   }
    00151 }
    00152
    00153 static void NewIndustryTileResolver(ResolverObject *res, IndustryGfx gfx, TileIndex tile, Industry *indus)
    00154 {
    00155   res->GetRandomBits = IndustryTileGetRandomBits;
    00156   res->GetTriggers   = IndustryTileGetTriggers;
    00157   res->SetTriggers   = IndustryTileSetTriggers;
    00158   res->GetVariable   = IndustryTileGetVariable;
    00159   res->ResolveReal   = IndustryTileResolveReal;
    00160
    00161   res->psa             = &indus->psa;
    00162   res->u.industry.tile = tile;
    00163   res->u.industry.ind  = indus;
    00164   res->u.industry.gfx  = gfx;
    00165   res->u.industry.type = indus->type;
    00166
    00167   res->callback        = CBID_NO_CALLBACK;
    00168   res->callback_param1 = 0;
    00169   res->callback_param2 = 0;
    00170   res->last_value      = 0;
    00171   res->trigger         = 0;
    00172   res->reseed          = 0;
    00173   res->count           = 0;
    00174
    00175   const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
    00176   res->grffile         = (its != NULL ? its->grf_prop.grffile : NULL);
    00177 }
    00178
    00179 static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, byte rnd_colour, byte stage, IndustryGfx gfx)
    00180 {
    00181   const DrawTileSprites *dts = group->dts;
    00182
    00183   SpriteID image = dts->ground.sprite;
    00184   PaletteID pal  = dts->ground.pal;
    00185
    00186   if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage;
    00187
    00188   if (GB(image, 0, SPRITE_WIDTH) != 0) {
    00189     /* If the ground sprite is the default flat water sprite, draw also canal/river borders
    00190      * Do not do this if the tile's WaterClass is 'land'. */
    00191     if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
    00192       DrawWaterClassGround(ti);
    00193     } else {
    00194       DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, GENERAL_SPRITE_COLOUR(rnd_colour)));
    00195     }
    00196   }
    00197
    00198   DrawNewGRFTileSeq(ti, dts, TO_INDUSTRIES, stage, GENERAL_SPRITE_COLOUR(rnd_colour));
    00199 }
    00200
    00201 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile)
    00202 {
    00203   ResolverObject object;
    00204   const SpriteGroup *group;
    00205
    00206   assert(industry != NULL && IsValidTile(tile));
    00207   assert(industry->index == INVALID_INDUSTRY || IsTileType(tile, MP_INDUSTRY));
    00208
    00209   NewIndustryTileResolver(&object, gfx_id, tile, industry);
    00210   object.callback = callback;
    00211   object.callback_param1 = param1;
    00212   object.callback_param2 = param2;
    00213
    00214   group = SpriteGroup::Resolve(GetIndustryTileSpec(gfx_id)->grf_prop.spritegroup, &object);
    00215   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
    00216
    00217   return group->GetCallbackResult();
    00218 }
    00219
    00220 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds)
    00221 {
    00222   const SpriteGroup *group;
    00223   ResolverObject object;
    00224
    00225   if (ti->tileh != SLOPE_FLAT) {
    00226     bool draw_old_one = true;
    00227     if (HasBit(inds->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
    00228       /* Called to determine the type (if any) of foundation to draw for industry tile */
    00229       uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, i, ti->tile);
    00230       draw_old_one = (callback_res != 0);
    00231     }
    00232
    00233     if (draw_old_one) DrawFoundation(ti, FOUNDATION_LEVELED);
    00234   }
    00235
    00236   NewIndustryTileResolver(&object, gfx, ti->tile, i);
    00237
    00238   group = SpriteGroup::Resolve(inds->grf_prop.spritegroup, &object);
    00239   if (group == NULL || group->type != SGT_TILELAYOUT) {
    00240     return false;
    00241   } else {
    00242     /* Limit the building stage to the number of stages supplied. */
    00243     const TileLayoutSpriteGroup *tlgroup = (const TileLayoutSpriteGroup *)group;
    00244     byte stage = GetIndustryConstructionStage(ti->tile);
    00245     stage = Clamp(stage - 4 + tlgroup->num_building_stages, 0, tlgroup->num_building_stages - 1);
    00246     IndustryDrawTileLayout(ti, tlgroup, i->random_colour, stage, gfx);
    00247     return true;
    00248   }
    00249 }
    00250
    00251 extern bool IsSlopeRefused(Slope current, Slope refused);
    00252
    00253 bool PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index)
    00254 {
    00255   Industry ind;
    00256   ind.index = INVALID_INDUSTRY;
    00257   ind.location.tile = ind_base_tile;
    00258   ind.location.w = 0;
    00259   ind.type = type;
    00260
    00261   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, itspec_index, gfx, &ind, ind_tile);
    00262   if (callback_res == CALLBACK_FAILED) {
    00263     return !IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused);
    00264   }
    00265   if (its->grf_prop.grffile->grf_version < 7) {
    00266     return callback_res != 0;
    00267   }
    00268   if (callback_res == 0x400) return true;
    00269
    00270   /* Copy some parameters from the registers to the error message text ref. stack */
    00271   SwitchToErrorRefStack();
    00272   PrepareTextRefStackUsage(4);
    00273   SwitchToNormalRefStack();
    00274
    00275   switch (callback_res) {
    00276     case 0x401: _error_message = STR_ERROR_SITE_UNSUITABLE;                 return false;
    00277     case 0x402: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST; return false;
    00278     case 0x403: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT;     return false;
    00279     default: _error_message = GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res); return false;
    00280   }
    00281 }
    00282
    00283 void AnimateNewIndustryTile(TileIndex tile)
    00284 {
    00285   Industry *ind = Industry::GetByTile(tile);
    00286   IndustryGfx gfx = GetIndustryGfx(tile);
    00287   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    00288   byte animation_speed = itspec->animation_speed;
    00289
    00290   if (HasBit(itspec->callback_mask, CBM_INDT_ANIM_SPEED)) {
    00291     uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIMATION_SPEED, 0, 0, gfx, ind, tile);
    00292     if (callback_res != CALLBACK_FAILED) animation_speed = Clamp(callback_res & 0xFF, 0, 16);
    00293   }
    00294
    00295   /* An animation speed of 2 means the animation frame changes 4 ticks, and
    00296    * increasing this value by one doubles the wait. 0 is the minimum value
    00297    * allowed for animation_speed, which corresponds to 30ms, and 16 is the
    00298    * maximum, corresponding to around 33 minutes. */
    00299   if ((_tick_counter % (1 << animation_speed)) != 0) return;
    00300
    00301   bool frame_set_by_callback = false;
    00302   byte frame = GetIndustryAnimationState(tile);
    00303   uint16 num_frames = GB(itspec->animation_info, 0, 8);
    00304
    00305   if (HasBit(itspec->callback_mask, CBM_INDT_ANIM_NEXT_FRAME)) {
    00306     uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_NEXT_FRAME,
    00307         (itspec->special_flags & INDTILE_SPECIAL_NEXTFRAME_RANDOMBITS) ? Random() : 0, 0, gfx, ind, tile);
    00308
    00309     if (callback_res != CALLBACK_FAILED) {
    00310       frame_set_by_callback = true;
    00311
    00312       switch (callback_res & 0xFF) {
    00313         case 0xFF:
    00314           DeleteAnimatedTile(tile);
    00315           break;
    00316         case 0xFE:
    00317           /* Carry on as normal. */
    00318           frame_set_by_callback = false;
    00319           break;
    00320         default:
    00321           frame = callback_res & 0xFF;
    00322           break;
    00323       }
    00324
    00325       /* If the lower 7 bits of the upper byte of the callback
    00326        * result are not empty, it is a sound effect. */
    00327       if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
    00328     }
    00329   }
    00330
    00331   if (!frame_set_by_callback) {
    00332     if (frame < num_frames) {
    00333       frame++;
    00334     } else if (frame == num_frames && GB(itspec->animation_info, 8, 8) == 1) {
    00335       /* This animation loops, so start again from the beginning */
    00336       frame = 0;
    00337     } else {
    00338       /* This animation doesn't loop, so stay here */
    00339       DeleteAnimatedTile(tile);
    00340     }
    00341   }
    00342
    00343   SetIndustryAnimationState(tile, frame);
    00344   MarkTileDirtyByTile(tile);
    00345 }
    00346
    00347 static void ChangeIndustryTileAnimationFrame(const IndustryTileSpec *itspec, TileIndex tile, IndustryAnimationTrigger iat, uint32 random_bits, IndustryGfx gfx, Industry *ind)
    00348 {
    00349   uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_ANIM_START_STOP, random_bits, iat, gfx, ind, tile);
    00350   if (callback_res == CALLBACK_FAILED) return;
    00351
    00352   switch (callback_res & 0xFF) {
    00353     case 0xFD: /* Do nothing. */         break;
    00354     case 0xFE: AddAnimatedTile(tile);    break;
    00355     case 0xFF: DeleteAnimatedTile(tile); break;
    00356     default:
    00357       SetIndustryAnimationState(tile, callback_res & 0xFF);
    00358       AddAnimatedTile(tile);
    00359       break;
    00360   }
    00361
    00362   /* If the lower 7 bits of the upper byte of the callback
    00363    * result are not empty, it is a sound effect. */
    00364   if (GB(callback_res, 8, 7) != 0) PlayTileSound(itspec->grf_prop.grffile, GB(callback_res, 8, 7), tile);
    00365 }
    00366
    00367 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random)
    00368 {
    00369   IndustryGfx gfx = GetIndustryGfx(tile);
    00370   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    00371
    00372   if (!HasBit(itspec->animation_triggers, iat)) return false;
    00373
    00374   Industry *ind = Industry::GetByTile(tile);
    00375   ChangeIndustryTileAnimationFrame(itspec, tile, iat, random, gfx, ind);
    00376   return true;
    00377 }
    00378
    00379 bool StartStopIndustryTileAnimation(const Industry *ind, IndustryAnimationTrigger iat)
    00380 {
    00381   bool ret = true;
    00382   uint32 random = Random();
    00383   TILE_AREA_LOOP(tile, ind->location) {
    00384     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
    00385       if (StartStopIndustryTileAnimation(tile, iat, random)) {
    00386         SB(random, 0, 16, Random());
    00387       } else {
    00388         ret = false;
    00389       }
    00390     }
    00391   }
    00392
    00393   return ret;
    00394 }
    00395
    00396 static void DoTriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger, Industry *ind)
    00397 {
    00398   ResolverObject object;
    00399
    00400   assert(IsValidTile(tile) && IsTileType(tile, MP_INDUSTRY));
    00401
    00402   IndustryGfx gfx = GetIndustryGfx(tile);
    00403   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    00404
    00405   if (itspec->grf_prop.spritegroup == NULL) return;
    00406
    00407   NewIndustryTileResolver(&object, gfx, tile, ind);
    00408
    00409   object.callback = CBID_RANDOM_TRIGGER;
    00410   object.trigger = trigger;
    00411
    00412   const SpriteGroup *group = SpriteGroup::Resolve(itspec->grf_prop.spritegroup, &object);
    00413   if (group == NULL) return;
    00414
    00415   byte new_random_bits = Random();
    00416   byte random_bits = GetIndustryRandomBits(tile);
    00417   random_bits &= ~object.reseed;
    00418   random_bits |= new_random_bits & object.reseed;
    00419   SetIndustryRandomBits(tile, random_bits);
    00420   MarkTileDirtyByTile(tile);
    00421 }
    00422
    00423 void TriggerIndustryTile(TileIndex tile, IndustryTileTrigger trigger)
    00424 {
    00425   DoTriggerIndustryTile(tile, trigger, Industry::GetByTile(tile));
    00426 }
    00427
    00428 void TriggerIndustry(Industry *ind, IndustryTileTrigger trigger)
    00429 {
    00430   TILE_AREA_LOOP(tile, ind->location) {
    00431     if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
    00432       DoTriggerIndustryTile(tile, trigger, ind);
    00433     }
    00434   }
    00435 }