2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2005-01-12 11:21:28 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-01-12 11:21:28 +00:00
|
|
|
#include "table/strings.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2005-07-21 19:36:43 +00:00
|
|
|
#include "player.h"
|
2005-01-12 11:21:28 +00:00
|
|
|
#include "signs.h"
|
|
|
|
#include "saveload.h"
|
|
|
|
#include "command.h"
|
2005-07-21 18:44:27 +00:00
|
|
|
#include "variables.h"
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
static Sign *_new_sign;
|
2005-12-24 20:51:21 +00:00
|
|
|
|
2005-02-04 14:45:32 +00:00
|
|
|
enum {
|
|
|
|
/* Max signs: 64000 (4 * 16000) */
|
|
|
|
SIGN_POOL_BLOCK_SIZE_BITS = 2, /* In bits, so (1 << 2) == 4 */
|
|
|
|
SIGN_POOL_MAX_BLOCKS = 16000,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called if a new block is added to the sign-pool
|
|
|
|
*/
|
|
|
|
static void SignPoolNewBlock(uint start_item)
|
|
|
|
{
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si;
|
2005-02-04 14:45:32 +00:00
|
|
|
|
2006-08-22 15:33:35 +00:00
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
2006-08-22 16:38:50 +00:00
|
|
|
for (si = GetSign(start_item); si != NULL; si = (si->index + 1 < GetSignPoolSize()) ? GetSign(si->index + 1) : NULL) si->index = start_item++;
|
2005-02-04 14:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the sign-pool */
|
2006-08-22 16:38:50 +00:00
|
|
|
MemoryPool _sign_pool = { "Signs", SIGN_POOL_MAX_BLOCKS, SIGN_POOL_BLOCK_SIZE_BITS, sizeof(Sign), &SignPoolNewBlock, NULL, 0, 0, NULL };
|
2005-02-04 14:45:32 +00:00
|
|
|
|
2005-01-12 11:21:28 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Update the coordinate of one sign
|
|
|
|
*
|
|
|
|
*/
|
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);
|
|
|
|
SetDParam(0, si->str);
|
|
|
|
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
|
|
|
*
|
|
|
|
*/
|
2005-01-22 20:23:18 +00:00
|
|
|
void UpdateAllSignVirtCoords(void)
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Marks the region of a sign as dirty
|
|
|
|
*
|
2006-08-22 16:38:50 +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 MarkSignDirty(Sign *si)
|
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,
|
|
|
|
si->sign.left + si->sign.width_1 * 4 + 12,
|
|
|
|
si->sign.top + 45);
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Allocates a new sign
|
|
|
|
*
|
|
|
|
* @return The pointer to the new sign, or NULL if there is no more free space
|
|
|
|
*/
|
2006-08-22 16:38:50 +00:00
|
|
|
static Sign *AllocateSign(void)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si;
|
2006-08-22 15:33:35 +00:00
|
|
|
|
|
|
|
/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
|
|
|
|
* TODO - This is just a temporary stage, this will be removed. */
|
2006-08-22 16:38:50 +00:00
|
|
|
for (si = GetSign(0); si != NULL; si = (si->index + 1 < GetSignPoolSize()) ? GetSign(si->index + 1) : NULL) {
|
|
|
|
if (!IsValidSign(si)) {
|
|
|
|
uint index = si->index;
|
2005-03-26 21:22:29 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
memset(si, 0, sizeof(Sign));
|
|
|
|
si->index = index;
|
2005-03-26 21:22:29 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
return si;
|
2005-03-26 21:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2005-02-04 14:45:32 +00:00
|
|
|
/* Check if we can add a block to the pool */
|
|
|
|
if (AddBlockToPool(&_sign_pool))
|
|
|
|
return AllocateSign();
|
|
|
|
|
2005-01-12 11:21:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Place a sign at the given coordinates. Ownership of sign has
|
|
|
|
* 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
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2006-04-10 07:15:58 +00:00
|
|
|
int32 CmdPlaceSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
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
|
|
|
|
|
|
|
/* Try to locate a new sign */
|
2006-08-22 16:38:50 +00:00
|
|
|
si = AllocateSign();
|
|
|
|
if (si == NULL) 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) {
|
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->str = STR_280A_SIGN;
|
|
|
|
si->x = x;
|
|
|
|
si->y = y;
|
|
|
|
si->owner = _current_player; // owner of the sign; just eyecandy
|
|
|
|
si->z = GetSlopeZ(x,y);
|
|
|
|
UpdateSignVirtCoords(si);
|
|
|
|
MarkSignDirty(si);
|
2005-03-26 21:22:29 +00:00
|
|
|
InvalidateWindow(WC_SIGN_LIST, 0);
|
|
|
|
_sign_sort_dirty = true;
|
2006-08-22 16:38:50 +00:00
|
|
|
_new_sign = si;
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 index of the sign to be renamed/removed
|
|
|
|
* @param p2 unused
|
2005-01-12 11:21:28 +00:00
|
|
|
*/
|
2006-04-10 07:15:58 +00:00
|
|
|
int32 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 */
|
2005-05-17 19:36:36 +00:00
|
|
|
if (_cmd_text[0] != '\0') {
|
2005-01-12 11:21:28 +00:00
|
|
|
/* Create the name */
|
2005-05-15 18:50:55 +00:00
|
|
|
StringID str = AllocateName(_cmd_text, 0);
|
2005-05-12 00:11:37 +00:00
|
|
|
if (str == 0) return CMD_ERROR;
|
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 */
|
2006-08-22 16:38:50 +00:00
|
|
|
DeleteName(si->str);
|
2005-01-12 11:21:28 +00:00
|
|
|
/* Assign the new one */
|
2006-08-22 16:38:50 +00:00
|
|
|
si->str = str;
|
|
|
|
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
|
|
|
} else {
|
|
|
|
/* Free the name, because we did not assign it yet */
|
|
|
|
DeleteName(str);
|
|
|
|
}
|
2005-05-12 00:11:37 +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
|
|
|
|
|
|
|
/* Delete the name */
|
2006-08-22 16:38:50 +00:00
|
|
|
DeleteName(si->str);
|
|
|
|
si->str = 0;
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Callback function that is called after a sign is placed
|
|
|
|
*
|
|
|
|
*/
|
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) {
|
2006-08-22 16:38:50 +00:00
|
|
|
ShowRenameSignWindow(_new_sign);
|
2005-01-12 11:21:28 +00:00
|
|
|
ResetObjectToPlace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* PlaceProc function, called when someone pressed the button if the
|
|
|
|
* sign-tool is selected
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
*/
|
2005-01-22 20:23:18 +00:00
|
|
|
void InitializeSigns(void)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
2005-02-04 14:45:32 +00:00
|
|
|
CleanPool(&_sign_pool);
|
|
|
|
AddBlockToPool(&_sign_pool);
|
2005-01-12 11:21:28 +00:00
|
|
|
}
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _sign_desc[] = {
|
2006-08-22 16:38:50 +00:00
|
|
|
SLE_VAR(Sign, str, SLE_UINT16),
|
|
|
|
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
|
|
|
|
*
|
|
|
|
*/
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Save_SIGN(void)
|
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
|
|
|
|
*
|
|
|
|
*/
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Load_SIGN(void)
|
2005-01-12 11:21:28 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2006-08-22 16:38:50 +00:00
|
|
|
Sign *si;
|
2005-02-04 14:45:32 +00:00
|
|
|
|
|
|
|
if (!AddBlockIfNeeded(&_sign_pool, index))
|
|
|
|
error("Signs: failed loading savegame: too many signs");
|
2005-01-12 11:21:28 +00:00
|
|
|
|
2006-08-22 16:38:50 +00:00
|
|
|
si = GetSign(index);
|
|
|
|
SlObject(si, _sign_desc);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const ChunkHandler _sign_chunk_handlers[] = {
|
|
|
|
{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
|
|
|
|
};
|