newgrf_industrytiles.cpp

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

Generated on Sun Nov 14 14:41:53 2010 for OpenTTD by  doxygen 1.6.1