newgrf_canal.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00018 
00020 WaterFeature _water_feature[CF_END];
00021 
00022 
00023 /* Random bits and triggers are not supported for canals, so the following
00024  * three functions are stubs. */
00025 static uint32 CanalGetRandomBits(const ResolverObject *object)
00026 {
00027   /* Return random bits only for water tiles, not station tiles */
00028   return IsTileType(object->u.canal.tile, MP_WATER) ? GetWaterTileRandomBits(object->u.canal.tile) : 0;
00029 }
00030 
00031 
00032 static uint32 CanalGetTriggers(const ResolverObject *object)
00033 {
00034   return 0;
00035 }
00036 
00037 
00038 static void CanalSetTriggers(const ResolverObject *object, int triggers)
00039 {
00040   return;
00041 }
00042 
00043 
00044 static uint32 CanalGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00045 {
00046   TileIndex tile = object->u.canal.tile;
00047 
00048   switch (variable) {
00049     /* Height of tile */
00050     case 0x80: {
00051       int z = GetTileZ(tile);
00052       /* Return consistent height within locks */
00053       if (IsTileType(tile, MP_WATER) && IsLock(tile) && GetLockPart(tile) == LOCK_PART_UPPER) z--;
00054       return z;
00055     }
00056 
00057     /* Terrain type */
00058     case 0x81: return GetTerrainType(tile);
00059 
00060     /* Random data for river or canal tiles, otherwise zero */
00061     case 0x83: return IsTileType(tile, MP_WATER) ? GetWaterTileRandomBits(tile) : 0;
00062   }
00063 
00064   DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
00065 
00066   *available = false;
00067   return UINT_MAX;
00068 }
00069 
00070 
00071 static const SpriteGroup *CanalResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00072 {
00073   if (group->num_loaded == 0) return NULL;
00074 
00075   return group->loaded[0];
00076 }
00077 
00078 
00079 static void NewCanalResolver(ResolverObject *res, TileIndex tile, const GRFFile *grffile)
00080 {
00081   res->GetRandomBits = &CanalGetRandomBits;
00082   res->GetTriggers   = &CanalGetTriggers;
00083   res->SetTriggers   = &CanalSetTriggers;
00084   res->GetVariable   = &CanalGetVariable;
00085   res->ResolveReal   = &CanalResolveReal;
00086 
00087   res->u.canal.tile = tile;
00088 
00089   res->callback        = CBID_NO_CALLBACK;
00090   res->callback_param1 = 0;
00091   res->callback_param2 = 0;
00092   res->ResetState();
00093 
00094   res->grffile         = grffile;
00095 }
00096 
00097 
00104 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
00105 {
00106   ResolverObject object;
00107   const SpriteGroup *group;
00108 
00109   NewCanalResolver(&object, tile, _water_feature[feature].grffile);
00110 
00111   group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
00112   if (group == NULL) return 0;
00113 
00114   return group->GetResult();
00115 }
00116 
00126 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
00127 {
00128   ResolverObject object;
00129   const SpriteGroup *group;
00130 
00131   NewCanalResolver(&object, tile, _water_feature[feature].grffile);
00132 
00133   object.callback = callback;
00134   object.callback_param1 = param1;
00135   object.callback_param2 = param2;
00136 
00137   group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
00138   if (group == NULL) return CALLBACK_FAILED;
00139 
00140   return group->GetCallbackResult();
00141 }
00142 
00150 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
00151 {
00152   if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
00153     uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
00154     if (cb != CALLBACK_FAILED) return cur_offset + cb;
00155   }
00156   return cur_offset;
00157 }