Changeset - r16110:0eb4e685b16c
[Not reviewed]
master
0 5 0
rubidium - 14 years ago 2010-09-16 16:31:57
rubidium@openttd.org
(svn r20816) -Codechange [FS#3835]: make waypoint default names work like depots, stations and vehicles (Krille)
5 files changed with 20 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/depot_base.h
Show inline comments
 
@@ -14,25 +14,25 @@
 

	
 
#include "depot_map.h"
 
#include "core/pool_type.hpp"
 

	
 
typedef Pool<Depot, DepotID, 64, 64000> DepotPool;
 
extern DepotPool _depot_pool;
 

	
 
struct Depot : DepotPool::PoolItem<&_depot_pool> {
 
	Town *town;
 
	char *name;
 

	
 
	TileIndex xy;
 
	uint16 town_cn;    ///< The Nth depot for this town (consecutive number)
 
	uint16 town_cn;    ///< The N-1th depot for this town (consecutive number)
 
	Date build_date;   ///< Date of construction
 

	
 
	Depot(TileIndex xy = INVALID_TILE) : xy(xy) {}
 
	~Depot();
 

	
 
	static FORCEINLINE Depot *GetByTile(TileIndex tile)
 
	{
 
		return Depot::Get(GetDepotIndex(tile));
 
	}
 

	
 
	/**
 
	 * Is the "type" of depot the same as the given depot,
src/saveload/afterload.cpp
Show inline comments
 
@@ -2286,24 +2286,37 @@ bool AfterLoadGame()
 
			Owner owner = GetTileOwner(o->location.tile);
 
			o->colour = (owner == OWNER_NONE) ? Random() & 0xF : Company::Get(owner)->livery->colour1;
 
		}
 
	}
 

	
 
	if (CheckSavegameVersion(149)) {
 
		for (TileIndex t = 0; t < map_size; t++) {
 
			if (!IsTileType(t, MP_STATION)) continue;
 
			if (!IsBuoy(t) && !IsOilRig(t) && !(IsDock(t) && GetTileSlope(t, NULL) == SLOPE_FLAT)) {
 
				SetWaterClass(t, WATER_CLASS_INVALID);
 
			}
 
		}
 

	
 
		/* Waypoints with custom name may have a non-unique town_cn,
 
		 * renumber those. First set all affected waypoints to the
 
		 * highest possible number to get them numbered in the
 
		 * order they have in the pool. */
 
		Waypoint *wp;
 
		FOR_ALL_WAYPOINTS(wp) {
 
			if (wp->name != NULL) wp->town_cn = UINT16_MAX;
 
		}
 

	
 
		FOR_ALL_WAYPOINTS(wp) {
 
			if (wp->name != NULL) MakeDefaultName(wp);
 
		}
 
	}
 

	
 
	/* Road stops is 'only' updating some caches */
 
	AfterLoadRoadStops();
 
	AfterLoadLabelMaps();
 

	
 
	GamelogPrintDebug(1);
 

	
 
	InitializeWindowsAndCaches();
 
	/* Restore the signals */
 
	ResetSignalHandlers();
 
	return true;
src/town.h
Show inline comments
 
@@ -231,52 +231,52 @@ DECLARE_ENUM_AS_BIT_SET(TownActions)
 

	
 
extern const byte _town_action_costs[TACT_COUNT];
 
extern TownID _new_town_id;
 

	
 
/**
 
 * Set the default name for a depot/waypoint
 
 * @tparam T The type/class to make a default name for
 
 * @param obj The object/instance we want to find the name for
 
 */
 
template <class T>
 
void MakeDefaultName(T *obj)
 
{
 
	/* We only want to set names if it hasn't been set before. */
 
	assert(obj->name == NULL);
 
	/* We only want to set names if it hasn't been set before, or when we're calling from afterload. */
 
	assert(obj->name == NULL || obj->town_cn == UINT16_MAX);
 

	
 
	obj->town = ClosestTownFromTile(obj->xy, UINT_MAX);
 

	
 
	/* Find first unused number belonging to this town. This can never fail,
 
	 * as long as there can be at most 65535 waypoints/depots in total.
 
	 *
 
	 * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at
 
	 * most 'n * (1 + ceil(m / 32))' steps (n - number of waypoints in pool,
 
	 * m - number of waypoints near this town).
 
	 * Usually, it needs only 'n' steps.
 
	 *
 
	 * If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
 
	 * but this way it is faster */
 

	
 
	uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
 
	uint32 next = 0; // first number in the bitmap
 
	uint32 idx  = 0; // index where we will stop
 
	uint32 cid  = 0; // current index, goes to T::GetPoolSize()-1, then wraps to 0
 

	
 
	do {
 
		T *lobj = T::GetIfValid(cid);
 

	
 
		/* check only valid waypoints... */
 
		if (lobj != NULL && obj != lobj) {
 
			/* only objects with 'generic' name within the same city and with the same type*/
 
			if (lobj->name == NULL && lobj->town == obj->town && lobj->IsOfType(obj)) {
 
			/* only objects within the same city and with the same type */
 
			if (lobj->town == obj->town && lobj->IsOfType(obj)) {
 
				/* if lobj->town_cn < next, uint will overflow to '+inf' */
 
				uint i = (uint)lobj->town_cn - next;
 

	
 
				if (i < 32) {
 
					SetBit(used, i); // update bitmap
 
					if (i == 0) {
 
						/* shift bitmap while the lowest bit is '1';
 
						 * increase the base of the bitmap too */
 
						do {
 
							used >>= 1;
 
							next++;
 
						} while (HasBit(used, 0));
src/waypoint_base.h
Show inline comments
 
@@ -6,25 +6,25 @@
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file waypoint_base.h Base of waypoints. */
 

	
 
#ifndef WAYPOINT_H
 
#define WAYPOINT_H
 

	
 
#include "base_station_base.h"
 

	
 
struct Waypoint : SpecializedStation<Waypoint, true> {
 
	uint16 town_cn;    ///< The Nth waypoint for this town (consecutive number)
 
	uint16 town_cn;    ///< The N-1th waypoint for this town (consecutive number)
 

	
 
	Waypoint(TileIndex tile = INVALID_TILE) : SpecializedStation<Waypoint, true>(tile) { }
 
	~Waypoint();
 

	
 
	void UpdateVirtCoord();
 

	
 
	/* virtual */ FORCEINLINE bool TileBelongsToRailStation(TileIndex tile) const
 
	{
 
		return IsRailWaypointTile(tile) && GetStationIndex(tile) == this->index;
 
	}
 

	
 
	/* virtual */ uint32 GetNewGRFVariable(const struct ResolverObject *object, byte variable, byte parameter, bool *available) const;
src/waypoint_cmd.cpp
Show inline comments
 
@@ -397,24 +397,18 @@ CommandCost CmdRenameWaypoint(TileIndex 
 
		if (ret.Failed()) return ret;
 
	}
 

	
 
	bool reset = StrEmpty(text);
 

	
 
	if (!reset) {
 
		if (strlen(text) >= MAX_LENGTH_STATION_NAME_BYTES) return CMD_ERROR;
 
		if (!IsUniqueWaypointName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		free(wp->name);
 

	
 
		if (reset) {
 
			wp->name = NULL;
 
			MakeDefaultName(wp);
 
		} else {
 
			wp->name = strdup(text);
 
		}
 
		wp->name = reset ? NULL : strdup(text);
 

	
 
		wp->UpdateVirtCoord();
 
	}
 
	return CommandCost();
 
}
0 comments (0 inline, 0 general)