signs.cpp

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

Generated on Wed Nov 19 19:01:40 2008 for openttd by  doxygen 1.5.6