(svn r4073) Add functions to make and test for (most) unmovable tiles
authortron
Thu, 23 Mar 2006 20:47:56 +0000
changeset 3310 a19f247b75b6
parent 3309 329e83a739a5
child 3311 138e38fa6fda
(svn r4073) Add functions to make and test for (most) unmovable tiles
clear_cmd.c
main_gui.c
town_cmd.c
unmovable_cmd.c
unmovable_map.h
--- a/clear_cmd.c	Thu Mar 23 17:49:50 2006 +0000
+++ b/clear_cmd.c	Thu Mar 23 20:47:56 2006 +0000
@@ -13,6 +13,7 @@
 #include "tunnel_map.h"
 #include "variables.h"
 #include "table/sprites.h"
+#include "unmovable_map.h"
 
 typedef struct TerraformerHeightMod {
 	TileIndex tile;
@@ -379,18 +380,16 @@
 
 	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
-	if (IsTileType(tile, MP_UNMOVABLE) && _m[tile].m5 == 3 &&
-			IsTileOwner(tile, _current_player))
+	if (IsOwnedLandTile(tile) && IsTileOwner(tile, _current_player)) {
 		return_cmd_error(STR_5807_YOU_ALREADY_OWN_IT);
+	}
 
 	cost = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 	if (CmdFailed(cost)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		ModifyTile(tile,
-			MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5,
-			3 /* map5 */
-			);
+		MakeOwnedLand(tile, _current_player);
+		MarkTileDirtyByTile(tile);
 	}
 
 	return cost + _price.purchase_land * 10;
@@ -435,7 +434,7 @@
 
 	tile = TileVirtXY(x, y);
 
-	if (!IsTileType(tile, MP_UNMOVABLE) || _m[tile].m5 != 3) return CMD_ERROR;
+	if (!IsOwnedLandTile(tile)) return CMD_ERROR;
 	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) return CMD_ERROR;
 
 
--- a/main_gui.c	Thu Mar 23 17:49:50 2006 +0000
+++ b/main_gui.c	Thu Mar 23 20:47:56 2006 +0000
@@ -27,6 +27,7 @@
 #include "waypoint.h"
 #include "variables.h"
 #include "train.h"
+#include "unmovable_map.h"
 
 #include "network_data.h"
 #include "network_client.h"
@@ -1207,7 +1208,8 @@
 		return;
 	}
 
-	ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAP5, 1);
+	MakeLighthouse(tile);
+	MarkTileDirtyByTile(tile);
 	SndPlayTileFx(SND_1F_SPLAT, tile);
 }
 
@@ -1217,7 +1219,8 @@
 		return;
 	}
 
-	ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAP5, 0);
+	MakeTransmitter(tile);
+	MarkTileDirtyByTile(tile);
 	SndPlayTileFx(SND_1F_SPLAT, tile);
 }
 
--- a/town_cmd.c	Thu Mar 23 17:49:50 2006 +0000
+++ b/town_cmd.c	Thu Mar 23 20:47:56 2006 +0000
@@ -21,6 +21,7 @@
 #include "saveload.h"
 #include "economy.h"
 #include "gui.h"
+#include "unmovable_map.h"
 #include "variables.h"
 
 enum {
@@ -1577,9 +1578,8 @@
 
 	if (CmdFailed(r)) return false;
 
-	ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5,
-		2 /* map5 */
-	);
+	MakeStatue(tile, _current_player);
+	MarkTileDirtyByTile(tile);
 
 	return true;
 }
--- a/unmovable_cmd.c	Thu Mar 23 17:49:50 2006 +0000
+++ b/unmovable_cmd.c	Thu Mar 23 20:47:56 2006 +0000
@@ -15,6 +15,7 @@
 #include "economy.h"
 #include "town.h"
 #include "sprite.h"
+#include "unmovable_map.h"
 #include "variables.h"
 
 /** Destroy a HQ.
@@ -184,7 +185,7 @@
 
 static uint GetSlopeZ_Unmovable(const TileInfo* ti)
 {
-	if (_m[ti->tile].m5 == 3) {
+	if (IsOwnedLand(ti->tile)) {
 		return ti->z + GetPartialZ(ti->x & 0xF, ti->y & 0xF, ti->tileh);
 	} else {
 		return ti->z + (ti->tileh == 0 ? 0 : 8);
@@ -193,20 +194,19 @@
 
 static uint GetSlopeTileh_Unmovable(const TileInfo *ti)
 {
-	return _m[ti->tile].m5 == 3 ? ti->tileh : 0;
+	return IsOwnedLand(ti->tile) ? ti->tileh : 0;
 }
 
 static int32 ClearTile_Unmovable(TileIndex tile, byte flags)
 {
-	byte m5 = _m[tile].m5;
-
-	if (m5 & 0x80) {
+	if (_m[tile].m5 & 0x80) {
 		if (_current_player == OWNER_WATER) return DestroyCompanyHQ(tile, DC_EXEC);
 		return_cmd_error(STR_5804_COMPANY_HEADQUARTERS_IN);
 	}
 
-	if (m5 == 3) // company owned land
+	if (IsOwnedLand(tile)) {
 		return DoCommandByTile(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
+	}
 
 	// checks if you're allowed to remove unmovable things
 	if (_game_mode != GM_EDITOR && _current_player != OWNER_WATER && ((flags & DC_AUTO || !_cheats.magic_bulldozer.value)) )
@@ -245,19 +245,16 @@
 	ac[CT_MAIL] = max(1, level / 2);
 }
 
-static const StringID _unmovable_tile_str[] = {
-	STR_5803_COMPANY_HEADQUARTERS,
-	STR_5801_TRANSMITTER,
-	STR_5802_LIGHTHOUSE,
-	STR_2016_STATUE,
-	STR_5805_COMPANY_OWNED_LAND,
-};
 
 static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
 {
-	int i = _m[tile].m5;
-	if (i & 0x80) i = -1;
-	td->str = _unmovable_tile_str[i + 1];
+	switch (GetUnmovableType(tile)) {
+		case UNMOVABLE_TRANSMITTER: td->str = STR_5801_TRANSMITTER; break;
+		case UNMOVABLE_LIGHTHOUSE:  td->str = STR_5802_LIGHTHOUSE; break;
+		case UNMOVABLE_STATUE:      td->str = STR_2016_STATUE; break;
+		case UNMOVABLE_OWNED_LAND:  td->str = STR_5805_COMPANY_OWNED_LAND; break;
+		default:                    td->str = STR_5803_COMPANY_HEADQUARTERS; break;
+	}
 	td->owner = GetTileOwner(tile);
 }
 
@@ -321,9 +318,7 @@
 	TileIndex tile_s = tile - TileDiffXY(4, 4);
 
 	BEGIN_TILE_LOOP(tile, 9, 9, tile_s)
-		// already a radio tower here?
-		if (IsTileType(tile, MP_UNMOVABLE) && _m[tile].m5 == 0)
-			return false;
+		if (IsTransmitterTile(tile)) return false;
 	END_TILE_LOOP(tile, 9, 9, tile_s)
 	return true;
 }
@@ -345,9 +340,7 @@
 		tile = RandomTile();
 		if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h >= 32) {
 			if (!checkRadioTowerNearby(tile)) continue;
-			SetTileType(tile, MP_UNMOVABLE);
-			SetTileOwner(tile, OWNER_NONE);
-			_m[tile].m5 = 0;
+			MakeTransmitter(tile);
 			if (--j == 0) break;
 		}
 	} while (--i);
@@ -380,9 +373,7 @@
 
 		assert(tile == TILE_MASK(tile));
 
-		SetTileType(tile, MP_UNMOVABLE);
-		SetTileOwner(tile, OWNER_NONE);
-		_m[tile].m5 = 1;
+		MakeLighthouse(tile);
 	} while (--i);
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unmovable_map.h	Thu Mar 23 20:47:56 2006 +0000
@@ -0,0 +1,65 @@
+/* $Id$ */
+
+typedef enum UnmovableType {
+	UNMOVABLE_TRANSMITTER = 0,
+	UNMOVABLE_LIGHTHOUSE  = 1,
+	UNMOVABLE_STATUE      = 2,
+	UNMOVABLE_OWNED_LAND  = 3
+} UnmovableType;
+
+
+static inline UnmovableType GetUnmovableType(TileIndex t)
+{
+	return _m[t].m5;
+}
+
+
+static inline bool IsTransmitterTile(TileIndex t)
+{
+	return
+		IsTileType(t, MP_UNMOVABLE) &&
+		GetUnmovableType(t) == UNMOVABLE_TRANSMITTER;
+}
+
+
+static inline bool IsOwnedLand(TileIndex t)
+{
+	return GetUnmovableType(t) == UNMOVABLE_OWNED_LAND;
+}
+
+static inline bool IsOwnedLandTile(TileIndex t)
+{
+	return IsTileType(t, MP_UNMOVABLE) && IsOwnedLand(t);
+}
+
+
+static inline void MakeUnmovable(TileIndex t, UnmovableType u, Owner o)
+{
+	SetTileType(t, MP_UNMOVABLE);
+	SetTileOwner(t, o);
+	_m[t].m2 = 0;
+	_m[t].m3 = 0;
+	_m[t].m4 = 0;
+	_m[t].m5 = u;
+}
+
+
+static inline void MakeTransmitter(TileIndex t)
+{
+	MakeUnmovable(t, UNMOVABLE_TRANSMITTER, OWNER_NONE);
+}
+
+static inline void MakeLighthouse(TileIndex t)
+{
+	MakeUnmovable(t, UNMOVABLE_LIGHTHOUSE, OWNER_NONE);
+}
+
+static inline void MakeStatue(TileIndex t, Owner o)
+{
+	MakeUnmovable(t, UNMOVABLE_STATUE, o);
+}
+
+static inline void MakeOwnedLand(TileIndex t, Owner o)
+{
+	MakeUnmovable(t, UNMOVABLE_OWNED_LAND, o);
+}