(svn r2286) - CodeChange: paramcheck the next batch of commands.
authorDarkvater
Mon, 09 May 2005 16:37:40 +0000
changeset 1782 0ac16ff36293
parent 1781 92e08797c84b
child 1783 5ff88ba55601
(svn r2286) - CodeChange: paramcheck the next batch of commands.
- Fix (regression): fix up terraform land where every player can terraform land (towns, map generation), and player can terraform different corners; used for building tunnels
clear_cmd.c
command.c
rail_cmd.c
rail_gui.c
station_cmd.c
tunnelbridge_cmd.c
waypoint.c
--- a/clear_cmd.c	Mon May 09 13:26:15 2005 +0000
+++ b/clear_cmd.c	Mon May 09 16:37:40 2005 +0000
@@ -212,7 +212,7 @@
 
 /** Terraform land
  * @param x,y coordinates to terraform
- * @param p1 corners to terraform. Human user only north, towns more
+ * @param p1 corners to terraform.
  * @param p2 direction; eg up or down
  */
 int32 CmdTerraformLand(int x, int y, uint32 flags, uint32 p1, uint32 p2)
@@ -224,9 +224,6 @@
 	TerraformerHeightMod modheight_data[576];
 	TileIndex tile_table_data[625];
 
-	/* A normal user can only terraform one corner, the northern one; p1 & 8 */
-	if (_current_player < MAX_PLAYERS && p1 != 8) return CMD_ERROR;
-
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
 	_error_message = INVALID_STRING_ID;
@@ -380,31 +377,28 @@
 	return cost;
 }
 
-/* Purchase a land area
- * p1 = unused
- * p2 = unused
+/** Purchase a land area. Actually you only purchase one tile, so
+ * the name is a bit confusing ;p
+ * @param x,y the tile the player is purchasing
+ * @param p1 unused
+ * @param p2 unused
  */
-
 int32 CmdPurchaseLandArea(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
-	uint tile;
+	TileIndex tile;
 	int32 cost;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
 	tile = TILE_FROM_XY(x,y);
 
-	if (!EnsureNoVehicle(tile))
-		return CMD_ERROR;
+	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
-	if (IsTileType(tile, MP_UNMOVABLE) &&
-			_map5[tile] == 3 &&
-			_map_owner[tile] == _current_player)
+	if (IsTileType(tile, MP_UNMOVABLE) && _map5[tile] == 3 && _map_owner[tile] == _current_player)
 		return_cmd_error(STR_5807_YOU_ALREADY_OWN_IT);
 
 	cost = DoCommandByTile(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
-	if (cost == CMD_ERROR)
-		return CMD_ERROR;
+	if (CmdFailed(cost)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
 		ModifyTile(tile,
@@ -439,9 +433,15 @@
 	return *price;
 }
 
+/** Sell a land area. Actually you only sell one tile, so
+ * the name is a bit confusing ;p
+ * @param x,y the tile the player is selling
+ * @param p1 unused
+ * @param p2 unused
+ */
 int32 CmdSellLandArea(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
-	uint tile;
+	TileIndex tile;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
@@ -450,13 +450,12 @@
 	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER)
 		return CMD_ERROR;
 
-	if (!EnsureNoVehicle(tile))
-		return CMD_ERROR;
+	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
 	if (flags & DC_EXEC)
 		DoClearSquare(tile);
 
-	return - _price.purchase_land*2;
+	return - _price.purchase_land * 2;
 }
 
 
--- a/command.c	Mon May 09 13:26:15 2005 +0000
+++ b/command.c	Mon May 09 16:37:40 2005 +0000
@@ -177,7 +177,6 @@
 	CmdBuildSingleSignal,					/* 8  */
 	CmdRemoveSingleSignal,				/* 9  */
 	CmdTerraformLand,							/* 10 */
-	/***************************************************/
 	CmdPurchaseLandArea,					/* 11 */
 	CmdSellLandArea,							/* 12 */
 	CmdBuildTunnel,								/* 13 */
@@ -190,6 +189,7 @@
 	NULL,													/* 20 */
 	CmdBuildRoadStop,							/* 21 */
 	NULL,													/* 22 */
+	/***************************************************/
 	CmdBuildLongRoad,							/* 23 */
 	CmdRemoveLongRoad,						/* 24 */
 	CmdBuildRoad,									/* 25 */
--- a/rail_cmd.c	Mon May 09 13:26:15 2005 +0000
+++ b/rail_cmd.c	Mon May 09 16:37:40 2005 +0000
@@ -971,15 +971,22 @@
 extern int32 DoConvertStreetRail(uint tile, uint totype, bool exec);
 extern int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec);
 
-// p1 = start tile
-// p2 = new railtype
+/** Convert one rail type to the other. You can convert normal rail to
+ * monorail/maglev easily or vice-versa.
+ * @param ex,ey end tile of rail conversion drag
+ * @param p1 start tile of drag
+ * @param p2 new railtype to convert to
+ */
 int32 CmdConvertRail(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
 {
 	int32 ret, cost, money;
-	int sx,sy,x,y;
+	int sx, sy, x, y;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
+	if (!ValParamRailtype(p2)) return CMD_ERROR;
+	if (p1 > MapSize()) return CMD_ERROR;
+
 	// make sure sx,sy are smaller than ex,ey
 	sx = TileX(p1) * 16;
 	sy = TileY(p1) * 16;
@@ -987,31 +994,31 @@
 	if (ey < sy) intswap(ey, sy);
 
 	money = GetAvailableMoneyForCommand();
-	ret = false;
 	cost = 0;
-	for(x=sx; x<=ex; x+=16) {
-		for(y=sy; y<=ey; y+=16) {
-			uint tile = TILE_FROM_XY(x,y);
-			DoConvertRailProc *p;
 
-			if (IsTileType(tile, MP_RAILWAY)) p = DoConvertRail;
-			else if (IsTileType(tile, MP_STATION)) p = DoConvertStationRail;
-			else if (IsTileType(tile, MP_STREET)) p = DoConvertStreetRail;
-			else if (IsTileType(tile, MP_TUNNELBRIDGE)) p = DoConvertTunnelBridgeRail;
+	for (x = sx; x <= ex; x += 16) {
+		for (y = sy; y <= ey; y += 16) {
+			TileIndex tile = TILE_FROM_XY(x,y);
+			DoConvertRailProc *proc;
+
+			if (IsTileType(tile, MP_RAILWAY)) proc = DoConvertRail;
+			else if (IsTileType(tile, MP_STATION)) proc = DoConvertStationRail;
+			else if (IsTileType(tile, MP_STREET)) proc = DoConvertStreetRail;
+			else if (IsTileType(tile, MP_TUNNELBRIDGE)) proc = DoConvertTunnelBridgeRail;
 			else continue;
 
-			ret = p(tile, p2, false);
-			if (ret == CMD_ERROR) continue;
+			ret = proc(tile, p2, false);
+			if (CmdFailed(ret)) continue;
 			cost += ret;
 
 			if (flags & DC_EXEC) {
 				if ( (money -= ret) < 0) { _additional_cash_required = ret; return cost - ret; }
-				p(tile, p2, true);
+				proc(tile, p2, true);
 			}
 		}
 	}
-	if (cost == 0) cost = CMD_ERROR;
-	return cost;
+
+	return (cost == 0) ? CMD_ERROR : cost;
 }
 
 static int32 RemoveTrainDepot(uint tile, uint32 flags)
--- a/rail_gui.c	Mon May 09 13:26:15 2005 +0000
+++ b/rail_gui.c	Mon May 09 16:37:40 2005 +0000
@@ -121,7 +121,7 @@
 static void PlaceRail_Waypoint(uint tile)
 {
 	if (!_remove_button_clicked) {
-		DoCommandP(tile, _waypoint_count > 0 ? (0x100 + _cur_waypoint_type) : 0, 0, CcPlaySound1E, CMD_BUILD_TRAIN_WAYPOINT | CMD_MSG(STR_CANT_BUILD_TRAIN_WAYPOINT));
+		DoCommandP(tile, (_waypoint_count > 0) ? (0x100 + _cur_waypoint_type) : 0, 0, CcPlaySound1E, CMD_BUILD_TRAIN_WAYPOINT | CMD_MSG(STR_CANT_BUILD_TRAIN_WAYPOINT));
 	} else {
 		DoCommandP(tile, 0, 0, CcPlaySound1E, CMD_REMOVE_TRAIN_WAYPOINT | CMD_MSG(STR_CANT_REMOVE_TRAIN_WAYPOINT));
 	}
--- a/station_cmd.c	Mon May 09 13:26:15 2005 +0000
+++ b/station_cmd.c	Mon May 09 16:37:40 2005 +0000
@@ -1116,10 +1116,15 @@
 	st->train_tile = tile;
 }
 
-// remove a single tile from a railroad station
+/** Remove a single tile from a railroad station.
+ * This allows for custom-built station with holes and weird layouts
+ * @param x,y tile coordinates to remove
+ * @param p1 unused
+ * @param p2 unused
+ */
 int32 CmdRemoveFromRailroadStation(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
-	uint tile = TILE_FROM_XY(x, y);
+	TileIndex tile = TILE_FROM_XY(x, y);
 	Station *st;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
@@ -1386,9 +1391,7 @@
 
 int32 DoConvertStationRail(uint tile, uint totype, bool exec)
 {
-	Station *st;
-
-	st = GetStation(_map2[tile]);
+	const Station *st = GetStation(_map2[tile]);
 	if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR;
 
 	// tile is not a railroad station?
@@ -1427,26 +1430,23 @@
 	}
 }
 
-/* Build a bus station
- * direction - direction of the stop exit
- * type - 0 for Bus stops, 1 for truck stops
+/** Build a bus station
+ * @param x,y coordinates to build bus station at
+ * @param p1 direction the busstop exit is pointing towards
+ * @param p2 0 for Bus stops, 1 for truck stops
  */
-
-int32 CmdBuildRoadStop(int x, int y, uint32 flags, uint32 direction, uint32 type)
+int32 CmdBuildRoadStop(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
+	Station *st;
 	RoadStop *road_stop;
 	RoadStop **currstop;
 	RoadStop *prev = NULL;
-	uint tile;
+	TileIndex tile;
 	int32 cost;
-	Station *st;
-	//Bus stops have a _map5 value of 0x47 + direction
-	//Truck stops have 0x43 + direction
-	byte gfxbase = (type) ? 0x43 : 0x47;
-
-	//saveguard the parameters
-	if (direction > 3 || type > 1)
-		return CMD_ERROR;
+	bool type = !!p2;
+
+	/* Saveguard the parameters */
+	if (p1 > 3) return CMD_ERROR;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
@@ -1455,7 +1455,7 @@
 	if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile))
 		return CMD_ERROR;
 
-	cost = CheckFlatLandBelow(tile, 1, 1, flags, 1 << direction, NULL);
+	cost = CheckFlatLandBelow(tile, 1, 1, flags, 1 << p1, NULL);
 	if (cost == CMD_ERROR)
 		return CMD_ERROR;
 
@@ -1526,8 +1526,10 @@
 		ModifyTile(tile,
 			MP_SETTYPE(MP_STATION) | MP_MAPOWNER_CURRENT |
 			MP_MAP2 | MP_MAP5 | MP_MAP3LO_CLEAR | MP_MAP3HI_CLEAR,
-			st->index,			/* map2 parameter */
-			gfxbase + direction       /* map5 parameter */
+			st->index,                       /* map2 parameter */
+			/* XXX - Truck stops have 0x43 _map5[] value + direction
+			 * XXX - Bus stops have a _map5 value of 0x47 + direction */
+			((type) ? 0x43 : 0x47) + p1 /* map5 parameter */
 		);
 
 		UpdateStationVirtCoordDirty(st);
--- a/tunnelbridge_cmd.c	Mon May 09 13:26:15 2005 +0000
+++ b/tunnelbridge_cmd.c	Mon May 09 16:37:40 2005 +0000
@@ -433,7 +433,7 @@
 
 static int32 DoBuildTunnel(int x, int y, int x2, int y2, uint32 flags, uint exc_tile)
 {
-	uint end_tile;
+	TileIndex end_tile;
 	int direction;
 	int32 cost, ret;
 	TileInfo ti;
@@ -472,8 +472,7 @@
 		if ( (direction ? 9U : 12U) != ti.tileh)
 			return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 		ret = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
-		if (ret == CMD_ERROR)
-			return CMD_ERROR;
+		if (CmdFailed(ret)) return CMD_ERROR;
 		cost += ret;
 	}
 	cost += _price.build_tunnel;
@@ -507,8 +506,7 @@
 			return_cmd_error(STR_1000_LAND_SLOPED_IN_WRONG_DIRECTION);
 
 		ret = DoCommandByTile(ti.tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
-		if (ret == CMD_ERROR)
-			return CMD_ERROR;
+		if (CmdFailed(ret)) return CMD_ERROR;
 		cost += ret;
 	}
 
@@ -533,10 +531,10 @@
 	return cost + _price.build_tunnel;
 }
 
-/* Build Tunnel
- * x,y - start tile coord
- * p1 - railtype
- * p2 - ptr to uint that recieves end tile
+/** Build Tunnel.
+ * @param x,y start tile coord of tunnel
+ * @param p1 railtype
+ * @param p2 unused (XXX - ptr to uint that recieves end tile; wtf?????)
  */
 int32 CmdBuildTunnel(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
@@ -545,10 +543,12 @@
 	uint z;
 	static const int8 _build_tunnel_coord_mod[4+1] = { -16, 0, 16, 0, -16 };
 	static const byte _build_tunnel_tileh[4] = {3, 9, 12, 6};
-	uint excavated_tile;
+	TileIndex excavated_tile;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
+	if (!ValParamRailtype(p1)) return CMD_ERROR;
+
 	_build_tunnel_railtype = (byte)(p1 & 0xFF);
 	_build_tunnel_bh = (byte)(p1 >> 8);
 
@@ -579,19 +579,14 @@
 		return CMD_ERROR;
 
 	if (ti.tileh != _build_tunnel_tileh[direction]) {
-		if (DoCommandByTile(ti.tile, ti.tileh & ~_build_tunnel_tileh[direction], 0, flags, CMD_TERRAFORM_LAND) == CMD_ERROR)
+		if (CmdFailed(DoCommandByTile(ti.tile, ti.tileh & ~_build_tunnel_tileh[direction], 0, flags, CMD_TERRAFORM_LAND)))
 			return_cmd_error(STR_5005_UNABLE_TO_EXCAVATE_LAND);
 		excavated_tile = 1;
 	}
 
-	if (flags & DC_EXEC && DoBuildTunnel(x,y,tiorg.x,tiorg.y,flags&~DC_EXEC,excavated_tile) == CMD_ERROR)
-		return CMD_ERROR;
-
-	return DoBuildTunnel(x,y,tiorg.x, tiorg.y,flags,excavated_tile);
+	return DoBuildTunnel(x, y, tiorg.x, tiorg.y, flags, excavated_tile);
 }
 
-static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3};
-
 TileIndex CheckTunnelBusy(TileIndex tile, uint *length)
 {
 	uint z = GetTileZ(tile);
@@ -625,8 +620,9 @@
 static int32 DoClearTunnel(uint tile, uint32 flags)
 {
 	Town *t;
-	uint endtile;
+	TileIndex endtile;
 	uint length;
+	static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3};
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
@@ -835,7 +831,7 @@
 
 int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec)
 {
-	uint endtile;
+	TileIndex endtile;
 	uint length;
 	Vehicle *v;
 
@@ -870,7 +866,7 @@
 		}
 		return _price.build_rail >> 1;
 	} else if ((_map5[tile]&0xC6) == 0x80) {
-		uint starttile;
+		TileIndex starttile;
 		int32 cost;
 		uint z = TilePixelHeight(tile);
 
--- a/waypoint.c	Mon May 09 13:26:15 2005 +0000
+++ b/waypoint.c	Mon May 09 16:37:40 2005 +0000
@@ -147,16 +147,24 @@
 	return best;
 }
 
-/* Convert existing rail to waypoint */
+/** Convert existing rail to waypoint. Eg build a waypoint station over
+ * piece of rail
+ * @param x,y coordinates where waypoint will be built
+ * @param p1 graphics for waypoint type, bit 8 signifies custom waypoint gfx (& 0x100)
+ * @param p2 unused
+ */
 int32 CmdBuildTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
-	TileIndex tile = TILE_FROM_XY(x,y);
+	TileIndex tile = TILE_FROM_XY(x, y);
 	Waypoint *wp;
 	uint tileh;
 	uint dir;
 
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 
+	/* if custom gfx are used, make sure it is within bounds */
+	if ((int)p1 > 0x100 + GetCustomStationsCount(STAT_CLASS_WAYP)) return CMD_ERROR;
+
 	if (!IsTileType(tile, MP_RAILWAY) || ((dir = 0, _map5[tile] != 1) && (dir = 1, _map5[tile] != 2)))
 		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
 
@@ -175,8 +183,7 @@
 	wp = FindDeletedWaypointCloseTo(tile);
 	if (wp == NULL) {
 		wp = AllocateWaypoint();
-		if (wp == NULL)
-			return CMD_ERROR;
+		if (wp == NULL) return CMD_ERROR;
 
 		wp->town_index = 0;
 		wp->string = STR_NULL;
@@ -271,21 +278,30 @@
 	return _price.remove_train_depot;
 }
 
-/* Command call to remove a waypoint */
+/** Delete a waypoint
+ * @param x,y coordinates where waypoint is to be deleted
+ * @param p1 unused
+ * @param p2 unused
+ */
 int32 CmdRemoveTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
-	uint tile = TILE_FROM_XY(x,y);
+	TileIndex tile = TILE_FROM_XY(x,y);
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 	return RemoveTrainWaypoint(tile, flags, true);
 }
 
-/* Rename a waypoint
- * p1 = id of waypoint */
+/** Rename a waypoint.
+ * @param x,y unused
+ * @param p1 id of waypoint
+ * @param p2 unused
+ */
 int32 CmdRenameWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 {
 	Waypoint *wp;
 	StringID str;
 
+	if (!IsWaypointIndex(p1)) return CMD_ERROR;
+
 	if (_decode_parameters[0] != 0) {
 		str = AllocateNameUnique((const char*)_decode_parameters, 0);
 		if (str == 0)