# 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, byte parameter, bool *available)
    00045 {
    00046   TileIndex tile = object->u.canal.tile;
    00047
    00048   switch (variable) {
    00049     /* Height of tile */
    00050     case 0x80: {
    00051       uint z = GetTileZ(tile) / TILE_HEIGHT;
    00052       /* Return consistent height within locks */
    00053       if (IsTileType(tile, MP_WATER) && IsLock(tile) && GetSection(tile) == LOCK_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->last_value      = 0;
    00093   res->trigger         = 0;
    00094   res->reseed          = 0;
    00095   res->count           = 0;
    00096   res->grffile         = grffile;
    00097 }
    00098
    00099
    00106 SpriteID GetCanalSprite(CanalFeature feature, TileIndex tile)
    00107 {
    00108   ResolverObject object;
    00109   const SpriteGroup *group;
    00110
    00111   NewCanalResolver(&object, tile, _water_feature[feature].grffile);
    00112
    00113   group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
    00114   if (group == NULL) return 0;
    00115
    00116   return group->GetResult();
    00117 }
    00118
    00128 static uint16 GetCanalCallback(CallbackID callback, uint32 param1, uint32 param2, CanalFeature feature, TileIndex tile)
    00129 {
    00130   ResolverObject object;
    00131   const SpriteGroup *group;
    00132
    00133   NewCanalResolver(&object, tile, _water_feature[feature].grffile);
    00134
    00135   object.callback = callback;
    00136   object.callback_param1 = param1;
    00137   object.callback_param2 = param2;
    00138
    00139   group = SpriteGroup::Resolve(_water_feature[feature].group, &object);
    00140   if (group == NULL) return CALLBACK_FAILED;
    00141
    00142   return group->GetCallbackResult();
    00143 }
    00144
    00152 uint GetCanalSpriteOffset(CanalFeature feature, TileIndex tile, uint cur_offset)
    00153 {
    00154   if (HasBit(_water_feature[feature].callback_mask, CBM_CANAL_SPRITE_OFFSET)) {
    00155     uint16 cb = GetCanalCallback(CBID_CANALS_SPRITE_OFFSET, cur_offset, 0, feature, tile);
    00156     if (cb != CALLBACK_FAILED) return cur_offset + cb;
    00157   }
    00158   return cur_offset;
    00159 }