2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file signs.cpp Handling of signs. */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2005-01-12 11:21:28 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_func.h"
|
2008-03-31 07:25:49 +00:00
|
|
|
#include "signs_base.h"
|
|
|
|
#include "signs_func.h"
|
2005-01-12 11:21:28 +00:00
|
|
|
#include "saveload.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2005-07-21 18:44:27 +00:00
|
|
|
#include "variables.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "viewport_func.h"
|
2008-05-08 13:29:35 +00:00
|
|
|
#include "tilehighlight_func.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "zoom_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "map_func.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-04-06 23:49:45 +00:00
|
|
|
#include "oldpool_func.h"
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2007-07-20 18:35:33 +00:00
|
|
|
SignID _new_sign_id;
|
|
|
|
uint _total_signs;
|
2008-01-13 13:36:01 +00:00
|
|
|
bool _sign_sort_dirty;
|
2005-12-24 20:51:21 +00:00
|
|
|
|
2007-08-02 13:27:45 +00:00
|
|
|
/* Initialize the sign-pool */
|
|
|
|
DEFINE_OLD_POOL_GENERIC(Sign, Sign)
|
|
|
|
|
2008-01-12 19:58:06 +00:00
|
|
|
Sign::Sign(PlayerID owner)
|
2005-02-04 14:45:32 +00:00
|
|
|
{
|
2008-01-12 19:58:06 +00:00
|
|
|
this->owner = owner;
|
2007-08-02 13:27:45 +00:00
|
|
|
}
|
2005-02-04 14:45:32 +00:00
|
|
|
|
2007-08-02 13:27:45 +00:00
|
|
|
Sign::~Sign()
|
|
|
|
{
|
2008-01-12 19:58:06 +00:00
|
|
|
free(this->name);
|
|
|
|
this->owner = INVALID_PLAYER;
|
2007-08-02 13:27:45 +00:00
|
|
|
}
|
2005-02-04 14:45:32 +00:00
|
|
|
|
2005-01-12 11:21:28 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Update the coordinate of one sign
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param si Pointer to the Sign
|
2005-01-12 11:21:28 +00:00
|
|
|
*
|
|
|
|
*/
|
2006-08-22 16:38:50 +00:00
|
|
|
static void UpdateSignVirtCoords(Sign *si)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2006-08-22 16:38:50 +00:00
|
|
|
Point pt = RemapCoords(si->x, si->y, si->z);
|
2007-06-25 10:40:56 +00:00
|
|
|
SetDParam(0, si->index);
|
2006-08-22 16:38:50 +00:00
|
|
|
UpdateViewportSignPos(&si->sign, pt.x, pt.y - 6, STR_2806);
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2005-01-12 11:54:51 +00:00
|
|
|
* Update the coordinates of all signs
|
2005-01-12 11:21:28 +00:00
|
|
|
*
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void UpdateAllSignVirtCoords()
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si;
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
FOR_ALL_SIGNS(si) UpdateSignVirtCoords(si);
|
2005-01-12 11:21:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-09-09 10:13:17 +00:00
|
|
|
* Marks the region of a sign as dirty.
|
2005-01-12 11:21:28 +00:00
|
|
|
*
|
2007-09-09 10:13:17 +00:00
|
|
|
* This function marks the sign in all viewports as dirty for repaint.
|
2005-01-12 11:21:28 +00:00
|
|
|
*
|
2006-08-22 16:38:50 +00:00
|
|
|
* @param si Pointer to the Sign
|
2007-09-09 10:13:17 +00:00
|
|
|
* @ingroup dirty
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2006-08-22 16:38:50 +00:00
|
|
|
static void MarkSignDirty(Sign *si)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2007-05-26 12:19:54 +00:00
|
|
|
/* We use ZOOM_LVL_MAX here, as every viewport can have an other zoom,
|
|
|
|
* and there is no way for us to know which is the biggest. So make the
|
|
|
|
* biggest area dirty, and we are safe for sure. */
|
2005-01-12 11:21:28 +00:00
|
|
|
MarkAllViewportsDirty(
|
2006-08-22 16:38:50 +00:00
|
|
|
si->sign.left - 6,
|
|
|
|
si->sign.top - 3,
|
2007-05-26 12:19:54 +00:00
|
|
|
si->sign.left + ScaleByZoom(si->sign.width_1 + 12, ZOOM_LVL_MAX),
|
|
|
|
si->sign.top + ScaleByZoom(12, ZOOM_LVL_MAX));
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
2006-08-26 19:29:35 +00:00
|
|
|
/**
|
|
|
|
* Place a sign at the given coordinates. Ownership of sign has
|
2005-05-12 00:11:37 +00:00
|
|
|
* no effect whatsoever except for the colour the sign gets for easy recognition,
|
|
|
|
* but everybody is able to rename/remove it.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile to place sign at
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
|
|
|
/* Try to locate a new sign */
|
2008-04-23 20:56:08 +00:00
|
|
|
if (!Sign::CanAllocateItem()) return_cmd_error(STR_2808_TOO_MANY_SIGNS);
|
2005-01-12 11:21:28 +00:00
|
|
|
|
|
|
|
/* When we execute, really make the sign */
|
|
|
|
if (flags & DC_EXEC) {
|
2008-04-23 20:56:08 +00:00
|
|
|
Sign *si = new Sign(_current_player);
|
2006-04-10 07:15:58 +00:00
|
|
|
int x = TileX(tile) * TILE_SIZE;
|
|
|
|
int y = TileY(tile) * TILE_SIZE;
|
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
si->x = x;
|
|
|
|
si->y = y;
|
2007-04-18 22:10:36 +00:00
|
|
|
si->z = GetSlopeZ(x, y);
|
2006-08-22 16:38:50 +00:00
|
|
|
UpdateSignVirtCoords(si);
|
|
|
|
MarkSignDirty(si);
|
2005-03-26 21:22:29 +00:00
|
|
|
InvalidateWindow(WC_SIGN_LIST, 0);
|
|
|
|
_sign_sort_dirty = true;
|
2007-07-20 18:35:33 +00:00
|
|
|
_new_sign_id = si->index;
|
|
|
|
_total_signs++;
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Rename a sign. If the new name of the sign is empty, we assume
|
|
|
|
* the user wanted to delete it. So delete it. Ownership of signs
|
|
|
|
* has no meaning/effect whatsoever except for eyecandy
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 index of the sign to be renamed/removed
|
|
|
|
* @param p2 unused
|
2007-04-04 01:35:16 +00:00
|
|
|
* @return 0 if succesfull, otherwise CMD_ERROR
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidSignID(p1)) return CMD_ERROR;
|
2005-05-17 19:36:36 +00:00
|
|
|
|
|
|
|
/* If _cmd_text 0 means the new text for the sign is non-empty.
|
2005-05-12 00:11:37 +00:00
|
|
|
* So rename the sign. If it is empty, it has no name, so delete it */
|
2007-08-31 20:50:59 +00:00
|
|
|
if (!StrEmpty(_cmd_text)) {
|
2005-01-12 11:21:28 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si = GetSign(p1);
|
2005-01-12 11:21:28 +00:00
|
|
|
|
|
|
|
/* Delete the old name */
|
2008-01-12 19:58:06 +00:00
|
|
|
free(si->name);
|
2005-01-12 11:21:28 +00:00
|
|
|
/* Assign the new one */
|
2008-01-12 19:58:06 +00:00
|
|
|
si->name = strdup(_cmd_text);
|
2006-08-22 16:38:50 +00:00
|
|
|
si->owner = _current_player;
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* Update; mark sign dirty twice, because it can either becom longer, or shorter */
|
2006-08-22 16:38:50 +00:00
|
|
|
MarkSignDirty(si);
|
|
|
|
UpdateSignVirtCoords(si);
|
|
|
|
MarkSignDirty(si);
|
2005-03-26 21:22:29 +00:00
|
|
|
InvalidateWindow(WC_SIGN_LIST, 0);
|
|
|
|
_sign_sort_dirty = true;
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
2007-04-04 01:35:16 +00:00
|
|
|
} else { // Delete sign
|
2005-01-12 11:21:28 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si = GetSign(p1);
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
MarkSignDirty(si);
|
2007-08-02 13:27:45 +00:00
|
|
|
delete si;
|
2006-08-26 14:22:54 +00:00
|
|
|
|
2005-03-26 21:22:29 +00:00
|
|
|
InvalidateWindow(WC_SIGN_LIST, 0);
|
|
|
|
_sign_sort_dirty = true;
|
2007-07-20 18:35:33 +00:00
|
|
|
_total_signs--;
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function that is called after a sign is placed
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param success of the operation
|
|
|
|
* @param tile unused
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
void CcPlaceSign(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
|
|
|
if (success) {
|
2007-07-20 18:35:33 +00:00
|
|
|
ShowRenameSignWindow(GetSign(_new_sign_id));
|
2005-01-12 11:21:28 +00:00
|
|
|
ResetObjectToPlace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* PlaceProc function, called when someone pressed the button if the
|
|
|
|
* sign-tool is selected
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param tile on which to place the sign
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
void PlaceProc_Sign(TileIndex tile)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2005-12-24 10:16:34 +00:00
|
|
|
DoCommandP(tile, 0, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Initialize the signs
|
|
|
|
*
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeSigns()
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2007-07-20 18:35:33 +00:00
|
|
|
_total_signs = 0;
|
2007-08-02 13:27:45 +00:00
|
|
|
_Sign_pool.CleanPool();
|
|
|
|
_Sign_pool.AddBlockToPool();
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _sign_desc[] = {
|
2008-01-12 19:58:06 +00:00
|
|
|
SLE_CONDVAR(Sign, name, SLE_NAME, 0, 83),
|
|
|
|
SLE_CONDSTR(Sign, name, SLE_STR, 0, 84, SL_MAX_VERSION),
|
2006-08-22 16:38:50 +00:00
|
|
|
SLE_CONDVAR(Sign, x, SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
|
|
|
|
SLE_CONDVAR(Sign, y, SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
|
|
|
|
SLE_CONDVAR(Sign, x, SLE_INT32, 5, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Sign, y, SLE_INT32, 5, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Sign, owner, SLE_UINT8, 6, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Sign, z, SLE_UINT8),
|
2005-01-12 11:21:28 +00:00
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Save all signs
|
|
|
|
*
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Save_SIGN()
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si;
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
FOR_ALL_SIGNS(si) {
|
|
|
|
SlSetArrayIndex(si->index);
|
|
|
|
SlObject(si, _sign_desc);
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Load all signs
|
|
|
|
*
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Load_SIGN()
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2007-07-20 18:35:33 +00:00
|
|
|
_total_signs = 0;
|
2005-01-12 11:21:28 +00:00
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2007-08-02 13:27:45 +00:00
|
|
|
Sign *si = new (index) Sign();
|
2006-08-22 16:38:50 +00:00
|
|
|
SlObject(si, _sign_desc);
|
2007-07-20 18:35:33 +00:00
|
|
|
|
|
|
|
_total_signs++;
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
2005-03-26 21:22:29 +00:00
|
|
|
|
|
|
|
_sign_sort_dirty = true;
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const ChunkHandler _sign_chunk_handlers[] = {
|
2005-01-12 11:21:28 +00:00
|
|
|
{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
|
|
|
|
};
|