newgrf_canal.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_canal.cpp 21198 2010-11-15 16:43:46Z 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 "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, byte parameter, bool *available)
00045 {
00046   TileIndex tile = object->u.canal.tile;
00047 
00048   switch (variable) {
00049     /* Height of tile */
00050     case 0x80: return GetTileZ(tile) / TILE_HEIGHT;
00051 
00052     /* Terrain type */
00053     case 0x81: return GetTerrainType(tile);
00054 
00055     /* Random data for river or canal tiles, otherwise zero */
00056     case 0x83: return GetWaterTileRandomBits(tile);
00057   }
00058 
00059   DEBUG(grf, 1, "Unhandled canal variable 0x%02X", variable);
00060 
00061   *available = false;
00062   return UINT_MAX;
00063 }
00064 
00065 
00066 static const SpriteGroup *CanalResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00067 {
00068   if (group->num_loaded == 0) return NULL;
00069 
00070   return group->loaded[0];
00071 }
00072 
00073 
00074 static void NewCanalResolver(ResolverObject *res, TileIndex tile, const GRFFile *grffile)
00075 {
00076   res->GetRandomBits = &CanalGetRandomBits;
00077   res->GetTriggers   = &CanalGetTriggers;
00078   res->SetTriggers   = &CanalSetTriggers;
00079   res->GetVariable   = &CanalGetVariable;
00080   res->ResolveReal   = &CanalResolveReal;
00081 
00082   res->u.canal.tile = tile;
00083 
00084   res->callback        = CBID_NO_CALLBACK;
00085   res->callback_param1 = 0;
00086   res->callback_param2 = 0;
00087   res->last_value      = 0;
00088   res->trigger         = 0;
00089   res->reseed          = 0;
00090   res->count           = 0;
00091   res->grffile         = grffile;
00092 }
00093 
00094 
00095 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
00096 {
00097   ResolverObject object;
00098   const SpriteGroup *group;
00099 
00100   NewCanalResolver(&object, tile, _water_feature[feature].grffile);
00101 
00102   group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
00103   if (group == NULL) return 0;
00104 
00105   return group->GetResult();
00106 }
00107 
00117 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
00118 {
00119   ResolverObject object;
00120   const SpriteGroup *group;
00121 
00122   NewCanalResolver(&object, tile, _water_feature[feature].grffile);
00123 
00124   object.callback = callback;
00125   object.callback_param1 = param1;
00126   object.callback_param2 = param2;
00127 
00128   group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
00129   if (group == NULL) return CALLBACK_FAILED;
00130 
00131   return group->GetCallbackResult();
00132 }
00133 
00141 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
00142 {
00143   if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
00144     uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
00145     if (cb != CALLBACK_FAILED) return cur_offset + cb;
00146   }
00147   return cur_offset;
00148 }

Generated on Fri Dec 31 17:15:34 2010 for OpenTTD by  doxygen 1.6.1