src/waypoint.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6743 cabfaa4a0295
--- a/src/waypoint.cpp	Sat Jun 02 19:59:29 2007 +0000
+++ b/src/waypoint.cpp	Sat Jul 14 19:42:58 2007 +0000
@@ -24,6 +24,8 @@
 #include "yapf/yapf.h"
 #include "date.h"
 #include "newgrf.h"
+#include "string.h"
+#include "strings.h"
 
 enum {
 	MAX_WAYPOINTS_PER_TOWN = 64,
@@ -201,7 +203,7 @@
  * @todo When checking for the tile slope,
  * distingush between "Flat land required" and "land sloped in wrong direction"
  */
-int32 CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
+CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	Waypoint *wp;
 	Slope tileh;
@@ -272,7 +274,7 @@
 		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
 	}
 
-	return _price.build_train_depot;
+	return CommandCost(_price.build_train_depot);
 }
 
 /**
@@ -295,7 +297,7 @@
  * @param justremove will indicate if it is removed from rail or if rails are removed too
  * @return cost of operation or error
  */
-int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
+CommandCost RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
 {
 	Waypoint *wp;
 
@@ -324,7 +326,7 @@
 		YapfNotifyTrackLayoutChange(tile, track);
 	}
 
-	return _price.remove_train_depot;
+	return CommandCost(_price.remove_train_depot);
 }
 
 /**
@@ -335,12 +337,26 @@
  * @param p2 unused
  * @return cost of operation or error
  */
-int32 CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
+CommandCost CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 	return RemoveTrainWaypoint(tile, flags, true);
 }
 
+static bool IsUniqueWaypointName(const char *name)
+{
+	const Waypoint *wp;
+	char buf[512];
+
+	FOR_ALL_WAYPOINTS(wp) {
+		SetDParam(0, wp->index);
+		GetString(buf, STR_WAYPOINT_RAW, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /**
  * Rename a waypoint.
  * @param tile unused
@@ -349,19 +365,23 @@
  * @param p2 unused
  * @return cost of operation or error
  */
-int32 CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
+CommandCost CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
 	Waypoint *wp;
 
 	if (!IsValidWaypointID(p1)) return CMD_ERROR;
 
-	if (_cmd_text[0] != '\0') {
-		StringID str = AllocateNameUnique(_cmd_text, 0);
+	wp = GetWaypoint(p1);
+	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
+
+	if (!StrEmpty(_cmd_text)) {
+		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+		StringID str = AllocateName(_cmd_text, 0);
 
 		if (str == 0) return CMD_ERROR;
 
 		if (flags & DC_EXEC) {
-			wp = GetWaypoint(p1);
 			if (wp->string != STR_NULL) DeleteName(wp->string);
 
 			wp->string = str;
@@ -374,7 +394,6 @@
 		}
 	} else {
 		if (flags & DC_EXEC) {
-			wp = GetWaypoint(p1);
 			if (wp->string != STR_NULL) DeleteName(wp->string);
 
 			MakeDefaultWaypointName(wp);
@@ -382,7 +401,7 @@
 			MarkWholeScreenDirty();
 		}
 	}
-	return 0;
+	return CommandCost();
 }
 
 /**