# newgrf_canal.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_canal.cpp 24693 2012-11-10 20:46:39Z alberth $ */
    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 "newgrf_spritegroup.h"
    00015 #include "newgrf_canal.h"
    00016 #include "water_map.h"
    00017
    00019 WaterFeature _water_feature[CF_END];
    00020
    00022 struct CanalScopeResolver : public ScopeResolver {
    00023   TileIndex tile;
    00024
    00025   CanalScopeResolver(ResolverObject *ro, TileIndex tile);
    00026
    00027   /* virtual */ uint32 GetRandomBits() const;
    00028   /* virtual */ uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
    00029 };
    00030
    00032 struct CanalResolverObject : public ResolverObject {
    00033   CanalScopeResolver canal_scope;
    00034
    00035   CanalResolverObject(const GRFFile *grffile, TileIndex tile,
    00036       CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
    00037
    00038   /* virtual */ ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0)
    00039   {
    00040     switch (scope) {
    00041       case VSG_SCOPE_SELF: return &this->canal_scope;
    00042       default: return ResolverObject::GetScope(scope, relative);
    00043     }
    00044   }
    00045
    00046   /* virtual */ const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
    00047 };
    00048
    00049 /* virtual */ uint32 CanalScopeResolver::GetRandomBits() const
    00050 {
    00051   /* Return random bits only for water tiles, not station tiles */
    00052   return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
    00053 }
    00054
    00055 /* virtual */ uint32 CanalScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
    00056 {
    00057   switch (variable) {
    00058     /* Height of tile */
    00059     case 0x80: {
    00060       int z = GetTileZ(this->tile);
    00061       /* Return consistent height within locks */
    00062       if (IsTileType(this->tile, MP_WATER) && IsLock(this->tile) && GetLockPart(this->tile) == LOCK_PART_UPPER) z--;
    00063       return z;
    00064     }
    00065
    00066     /* Terrain type */
    00067     case 0x81: return GetTerrainType(this->tile);
    00068
    00069     /* Random data for river or canal tiles, otherwise zero */
    00070     case 0x83: return IsTileType(this->tile, MP_WATER) ? GetWaterTileRandomBits(this->tile) : 0;
    00071   }
    00072
    00073   DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
    00074
    00075   *available = false;
    00076   return UINT_MAX;
    00077 }
    00078
    00079
    00080 /* virtual */ const SpriteGroup *CanalResolverObject::ResolveReal(const RealSpriteGroup *group) const
    00081 {
    00082   if (group->num_loaded == 0) return NULL;
    00083
    00084   return group->loaded[0];
    00085 }
    00086
    00087 CanalScopeResolver::CanalScopeResolver(ResolverObject *ro, TileIndex tile) : ScopeResolver(ro)
    00088 {
    00089   this->tile = tile;
    00090 }
    00091
    00100 CanalResolverObject::CanalResolverObject(const GRFFile *grffile, TileIndex tile,
    00101     CallbackID callback, uint32 callback_param1, uint32 callback_param2)
    00102     : ResolverObject(grffile, callback, callback_param1, callback_param2), canal_scope(this, tile)
    00103 {
    00104 }
    00105
    00112 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
    00113 {
    00114   CanalResolverObject object(_water_feature[feature].grffile, tile);
    00115   const SpriteGroup *group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
    00116   if (group == NULL) return 0;
    00117
    00118   return group->GetResult();
    00119 }
    00120
    00130 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
    00131 {
    00132   CanalResolverObject object(_water_feature[feature].grffile, tile, callback, param1, param2);
    00133   const SpriteGroup *group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
    00134   if (group == NULL) return CALLBACK_FAILED;
    00135
    00136   return group->GetCallbackResult();
    00137 }
    00138
    00146 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
    00147 {
    00148   if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
    00149     uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
    00150     if (cb != CALLBACK_FAILED) return cur_offset + cb;
    00151   }
    00152   return cur_offset;
    00153 }