src/waypoint.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6743 cabfaa4a0295
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
    22 #include "table/strings.h"
    22 #include "table/strings.h"
    23 #include "vehicle.h"
    23 #include "vehicle.h"
    24 #include "yapf/yapf.h"
    24 #include "yapf/yapf.h"
    25 #include "date.h"
    25 #include "date.h"
    26 #include "newgrf.h"
    26 #include "newgrf.h"
       
    27 #include "string.h"
       
    28 #include "strings.h"
    27 
    29 
    28 enum {
    30 enum {
    29 	MAX_WAYPOINTS_PER_TOWN = 64,
    31 	MAX_WAYPOINTS_PER_TOWN = 64,
    30 };
    32 };
    31 
    33 
   199  * @param p2 unused
   201  * @param p2 unused
   200  *
   202  *
   201  * @todo When checking for the tile slope,
   203  * @todo When checking for the tile slope,
   202  * distingush between "Flat land required" and "land sloped in wrong direction"
   204  * distingush between "Flat land required" and "land sloped in wrong direction"
   203  */
   205  */
   204 int32 CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   206 CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   205 {
   207 {
   206 	Waypoint *wp;
   208 	Waypoint *wp;
   207 	Slope tileh;
   209 	Slope tileh;
   208 	Axis axis;
   210 	Axis axis;
   209 
   211 
   270 		UpdateWaypointSign(wp);
   272 		UpdateWaypointSign(wp);
   271 		RedrawWaypointSign(wp);
   273 		RedrawWaypointSign(wp);
   272 		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
   274 		YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
   273 	}
   275 	}
   274 
   276 
   275 	return _price.build_train_depot;
   277 	return CommandCost(_price.build_train_depot);
   276 }
   278 }
   277 
   279 
   278 /**
   280 /**
   279  * Daily loop for waypoints
   281  * Daily loop for waypoints
   280  */
   282  */
   293  * @param tile from which to remove waypoint
   295  * @param tile from which to remove waypoint
   294  * @param flags type of operation
   296  * @param flags type of operation
   295  * @param justremove will indicate if it is removed from rail or if rails are removed too
   297  * @param justremove will indicate if it is removed from rail or if rails are removed too
   296  * @return cost of operation or error
   298  * @return cost of operation or error
   297  */
   299  */
   298 int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
   300 CommandCost RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
   299 {
   301 {
   300 	Waypoint *wp;
   302 	Waypoint *wp;
   301 
   303 
   302 	/* Make sure it's a waypoint */
   304 	/* Make sure it's a waypoint */
   303 	if (!IsTileType(tile, MP_RAILWAY) ||
   305 	if (!IsTileType(tile, MP_RAILWAY) ||
   322 			SetSignalsOnBothDir(tile, track);
   324 			SetSignalsOnBothDir(tile, track);
   323 		}
   325 		}
   324 		YapfNotifyTrackLayoutChange(tile, track);
   326 		YapfNotifyTrackLayoutChange(tile, track);
   325 	}
   327 	}
   326 
   328 
   327 	return _price.remove_train_depot;
   329 	return CommandCost(_price.remove_train_depot);
   328 }
   330 }
   329 
   331 
   330 /**
   332 /**
   331  * Delete a waypoint
   333  * Delete a waypoint
   332  * @param tile tile where waypoint is to be deleted
   334  * @param tile tile where waypoint is to be deleted
   333  * @param flags type of operation
   335  * @param flags type of operation
   334  * @param p1 unused
   336  * @param p1 unused
   335  * @param p2 unused
   337  * @param p2 unused
   336  * @return cost of operation or error
   338  * @return cost of operation or error
   337  */
   339  */
   338 int32 CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   340 CommandCost CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   339 {
   341 {
   340 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   342 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
   341 	return RemoveTrainWaypoint(tile, flags, true);
   343 	return RemoveTrainWaypoint(tile, flags, true);
       
   344 }
       
   345 
       
   346 static bool IsUniqueWaypointName(const char *name)
       
   347 {
       
   348 	const Waypoint *wp;
       
   349 	char buf[512];
       
   350 
       
   351 	FOR_ALL_WAYPOINTS(wp) {
       
   352 		SetDParam(0, wp->index);
       
   353 		GetString(buf, STR_WAYPOINT_RAW, lastof(buf));
       
   354 		if (strcmp(buf, name) == 0) return false;
       
   355 	}
       
   356 
       
   357 	return true;
   342 }
   358 }
   343 
   359 
   344 /**
   360 /**
   345  * Rename a waypoint.
   361  * Rename a waypoint.
   346  * @param tile unused
   362  * @param tile unused
   347  * @param flags type of operation
   363  * @param flags type of operation
   348  * @param p1 id of waypoint
   364  * @param p1 id of waypoint
   349  * @param p2 unused
   365  * @param p2 unused
   350  * @return cost of operation or error
   366  * @return cost of operation or error
   351  */
   367  */
   352 int32 CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   368 CommandCost CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
   353 {
   369 {
   354 	Waypoint *wp;
   370 	Waypoint *wp;
   355 
   371 
   356 	if (!IsValidWaypointID(p1)) return CMD_ERROR;
   372 	if (!IsValidWaypointID(p1)) return CMD_ERROR;
   357 
   373 
   358 	if (_cmd_text[0] != '\0') {
   374 	wp = GetWaypoint(p1);
   359 		StringID str = AllocateNameUnique(_cmd_text, 0);
   375 	if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
       
   376 
       
   377 	if (!StrEmpty(_cmd_text)) {
       
   378 		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
       
   379 
       
   380 		StringID str = AllocateName(_cmd_text, 0);
   360 
   381 
   361 		if (str == 0) return CMD_ERROR;
   382 		if (str == 0) return CMD_ERROR;
   362 
   383 
   363 		if (flags & DC_EXEC) {
   384 		if (flags & DC_EXEC) {
   364 			wp = GetWaypoint(p1);
       
   365 			if (wp->string != STR_NULL) DeleteName(wp->string);
   385 			if (wp->string != STR_NULL) DeleteName(wp->string);
   366 
   386 
   367 			wp->string = str;
   387 			wp->string = str;
   368 			wp->town_cn = 0;
   388 			wp->town_cn = 0;
   369 
   389 
   372 		} else {
   392 		} else {
   373 			DeleteName(str);
   393 			DeleteName(str);
   374 		}
   394 		}
   375 	} else {
   395 	} else {
   376 		if (flags & DC_EXEC) {
   396 		if (flags & DC_EXEC) {
   377 			wp = GetWaypoint(p1);
       
   378 			if (wp->string != STR_NULL) DeleteName(wp->string);
   397 			if (wp->string != STR_NULL) DeleteName(wp->string);
   379 
   398 
   380 			MakeDefaultWaypointName(wp);
   399 			MakeDefaultWaypointName(wp);
   381 			UpdateWaypointSign(wp);
   400 			UpdateWaypointSign(wp);
   382 			MarkWholeScreenDirty();
   401 			MarkWholeScreenDirty();
   383 		}
   402 		}
   384 	}
   403 	}
   385 	return 0;
   404 	return CommandCost();
   386 }
   405 }
   387 
   406 
   388 /**
   407 /**
   389  * This hacks together some dummy one-shot Station structure for a waypoint.
   408  * This hacks together some dummy one-shot Station structure for a waypoint.
   390  * @param tile on which to work
   409  * @param tile on which to work