signs.cpp

Go to the documentation of this file.
00001 /* $Id: signs.cpp 14385 2008-09-22 19:57:31Z rubidium $ */
00002 
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "landscape.h"
00008 #include "player_func.h"
00009 #include "signs.h"
00010 #include "saveload.h"
00011 #include "command_func.h"
00012 #include "variables.h"
00013 #include "strings_func.h"
00014 #include "viewport_func.h"
00015 #include "zoom_func.h"
00016 #include "functions.h"
00017 #include "window_func.h"
00018 #include "map_func.h"
00019 #include "string_func.h"
00020 
00021 #include "table/strings.h"
00022 
00023 SignID _new_sign_id;
00024 uint _total_signs;
00025 bool _sign_sort_dirty;
00026 
00027 /* Initialize the sign-pool */
00028 DEFINE_OLD_POOL_GENERIC(Sign, Sign)
00029 
00030 Sign::Sign(PlayerID owner)
00031 {
00032   this->owner = owner;
00033 }
00034 
00035 Sign::~Sign()
00036 {
00037   free(this->name);
00038 
00039   if (CleaningPool()) return;
00040 
00041   DeleteRenameSignWindow(this->index);
00042   this->owner = INVALID_PLAYER;
00043 }
00044 
00051 static void UpdateSignVirtCoords(Sign *si)
00052 {
00053   Point pt = RemapCoords(si->x, si->y, si->z);
00054   SetDParam(0, si->index);
00055   UpdateViewportSignPos(&si->sign, pt.x, pt.y - 6, STR_2806);
00056 }
00057 
00063 void UpdateAllSignVirtCoords()
00064 {
00065   Sign *si;
00066 
00067   FOR_ALL_SIGNS(si) UpdateSignVirtCoords(si);
00068 
00069 }
00070 
00079 static void MarkSignDirty(Sign *si)
00080 {
00081   /* We use ZOOM_LVL_MAX here, as every viewport can have an other zoom,
00082     *  and there is no way for us to know which is the biggest. So make the
00083     *  biggest area dirty, and we are safe for sure. */
00084   MarkAllViewportsDirty(
00085     si->sign.left - 6,
00086     si->sign.top  - 3,
00087     si->sign.left + ScaleByZoom(si->sign.width_1 + 12, ZOOM_LVL_MAX),
00088     si->sign.top  + ScaleByZoom(12, ZOOM_LVL_MAX));
00089 }
00090 
00100 CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
00101 {
00102   /* Try to locate a new sign */
00103   if (!Sign::CanAllocateItem()) return_cmd_error(STR_2808_TOO_MANY_SIGNS);
00104 
00105   /* When we execute, really make the sign */
00106   if (flags & DC_EXEC) {
00107     Sign *si = new Sign(_current_player);
00108     int x = TileX(tile) * TILE_SIZE;
00109     int y = TileY(tile) * TILE_SIZE;
00110 
00111     si->x = x;
00112     si->y = y;
00113     si->z = GetSlopeZ(x, y);
00114     UpdateSignVirtCoords(si);
00115     MarkSignDirty(si);
00116     InvalidateWindow(WC_SIGN_LIST, 0);
00117     _sign_sort_dirty = true;
00118     _new_sign_id = si->index;
00119     _total_signs++;
00120   }
00121 
00122   return CommandCost();
00123 }
00124 
00134 CommandCost CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
00135 {
00136   if (!IsValidSignID(p1)) return CMD_ERROR;
00137 
00138   /* If _cmd_text 0 means the new text for the sign is non-empty.
00139    * So rename the sign. If it is empty, it has no name, so delete it */
00140   if (!StrEmpty(_cmd_text)) {
00141     if (flags & DC_EXEC) {
00142       Sign *si = GetSign(p1);
00143 
00144       /* Delete the old name */
00145       free(si->name);
00146       /* Assign the new one */
00147       si->name = strdup(_cmd_text);
00148       si->owner = _current_player;
00149 
00150       /* Update; mark sign dirty twice, because it can either becom longer, or shorter */
00151       MarkSignDirty(si);
00152       UpdateSignVirtCoords(si);
00153       MarkSignDirty(si);
00154       InvalidateWindow(WC_SIGN_LIST, 0);
00155       _sign_sort_dirty = true;
00156     }
00157   } else { // Delete sign
00158     if (flags & DC_EXEC) {
00159       Sign *si = GetSign(p1);
00160 
00161       MarkSignDirty(si);
00162       delete si;
00163 
00164       InvalidateWindow(WC_SIGN_LIST, 0);
00165       _sign_sort_dirty = true;
00166       _total_signs--;
00167     }
00168   }
00169 
00170   return CommandCost();
00171 }
00172 
00180 void CcPlaceSign(bool success, TileIndex tile, uint32 p1, uint32 p2)
00181 {
00182   if (success) {
00183     ShowRenameSignWindow(GetSign(_new_sign_id));
00184     ResetObjectToPlace();
00185   }
00186 }
00187 
00194 void PlaceProc_Sign(TileIndex tile)
00195 {
00196   DoCommandP(tile, 0, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
00197 }
00198 
00204 void InitializeSigns()
00205 {
00206   _total_signs = 0;
00207   _Sign_pool.CleanPool();
00208   _Sign_pool.AddBlockToPool();
00209 }
00210 
00211 static const SaveLoad _sign_desc[] = {
00212   SLE_CONDVAR(Sign, name,  SLE_NAME,                   0, 83),
00213   SLE_CONDSTR(Sign, name,  SLE_STR, 0,                84, SL_MAX_VERSION),
00214   SLE_CONDVAR(Sign, x,     SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
00215   SLE_CONDVAR(Sign, y,     SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
00216   SLE_CONDVAR(Sign, x,     SLE_INT32,                  5, SL_MAX_VERSION),
00217   SLE_CONDVAR(Sign, y,     SLE_INT32,                  5, SL_MAX_VERSION),
00218   SLE_CONDVAR(Sign, owner, SLE_UINT8,                  6, SL_MAX_VERSION),
00219       SLE_VAR(Sign, z,     SLE_UINT8),
00220   SLE_END()
00221 };
00222 
00228 static void Save_SIGN()
00229 {
00230   Sign *si;
00231 
00232   FOR_ALL_SIGNS(si) {
00233     SlSetArrayIndex(si->index);
00234     SlObject(si, _sign_desc);
00235   }
00236 }
00237 
00243 static void Load_SIGN()
00244 {
00245   _total_signs = 0;
00246   int index;
00247   while ((index = SlIterateArray()) != -1) {
00248     Sign *si = new (index) Sign();
00249     SlObject(si, _sign_desc);
00250 
00251     _total_signs++;
00252   }
00253 
00254   _sign_sort_dirty = true;
00255 }
00256 
00257 extern const ChunkHandler _sign_chunk_handlers[] = {
00258   { 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
00259 };

Generated on Mon Sep 22 20:34:18 2008 for openttd by  doxygen 1.5.6