signs_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: signs_cmd.cpp 23835 2012-01-22 13:54:02Z frosch $ */
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 "landscape.h"
00014 #include "company_func.h"
00015 #include "signs_base.h"
00016 #include "signs_func.h"
00017 #include "command_func.h"
00018 #include "tilehighlight_func.h"
00019 #include "window_func.h"
00020 #include "string_func.h"
00021 
00022 #include "table/strings.h"
00023 
00025 SignID _new_sign_id;
00026 
00038 CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00039 {
00040   /* Try to locate a new sign */
00041   if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS);
00042 
00043   /* Check sign text length if any */
00044   if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
00045 
00046   /* When we execute, really make the sign */
00047   if (flags & DC_EXEC) {
00048     Sign *si = new Sign(_game_mode == GM_EDITOR ? OWNER_DEITY : _current_company);
00049     int x = TileX(tile) * TILE_SIZE;
00050     int y = TileY(tile) * TILE_SIZE;
00051 
00052     si->x = x;
00053     si->y = y;
00054     si->z = GetSlopePixelZ(x, y);
00055     if (!StrEmpty(text)) {
00056       si->name = strdup(text);
00057     }
00058     si->UpdateVirtCoord();
00059     InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00060     _new_sign_id = si->index;
00061   }
00062 
00063   return CommandCost();
00064 }
00065 
00077 CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00078 {
00079   Sign *si = Sign::GetIfValid(p1);
00080   if (si == NULL) return CMD_ERROR;
00081   if (si->owner == OWNER_DEITY && _current_company != OWNER_DEITY && _game_mode != GM_EDITOR) return CMD_ERROR;
00082 
00083   /* Rename the signs when empty, otherwise remove it */
00084   if (!StrEmpty(text)) {
00085     if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR;
00086 
00087     if (flags & DC_EXEC) {
00088       /* Delete the old name */
00089       free(si->name);
00090       /* Assign the new one */
00091       si->name = strdup(text);
00092       if (_game_mode != GM_EDITOR) si->owner = _current_company;
00093 
00094       si->UpdateVirtCoord();
00095       InvalidateWindowData(WC_SIGN_LIST, 0, 1);
00096     }
00097   } else { // Delete sign
00098     if (flags & DC_EXEC) {
00099       si->sign.MarkDirty();
00100       delete si;
00101 
00102       InvalidateWindowData(WC_SIGN_LIST, 0, 0);
00103     }
00104   }
00105 
00106   return CommandCost();
00107 }
00108 
00116 void CcPlaceSign(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00117 {
00118   if (result.Failed()) return;
00119 
00120   ShowRenameSignWindow(Sign::Get(_new_sign_id));
00121   ResetObjectToPlace();
00122 }
00123 
00130 void PlaceProc_Sign(TileIndex tile)
00131 {
00132   DoCommandP(tile, 0, 0, CMD_PLACE_SIGN | CMD_MSG(STR_ERROR_CAN_T_PLACE_SIGN_HERE), CcPlaceSign);
00133 }