File diff r2624:1fa34b183518 → r2625:27ad3b929a5b
waypoint.c
Show inline comments
 
@@ -133,98 +133,98 @@ void MakeDefaultWaypointName(Waypoint *w
 
}
 

	
 
/* Find a deleted waypoint close to a tile. */
 
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
 
{
 
	Waypoint *wp, *best = NULL;
 
	uint thres = 8, cur_dist;
 

	
 
	FOR_ALL_WAYPOINTS(wp) {
 
		if (wp->deleted && wp->xy) {
 
			cur_dist = DistanceManhattan(tile, wp->xy);
 
			if (cur_dist < thres) {
 
				thres = cur_dist;
 
				best = wp;
 
			}
 
		}
 
	}
 

	
 
	return best;
 
}
 

	
 
/** 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 p1 graphics for waypoint type, 0 indicates standard graphics
 
 * @param p2 unused
 
 *
 
 * @todo When checking for the tile slope,
 
 * distingush between "Flat land required" and "land sloped in wrong direction"
 
 */
 
int32 CmdBuildTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	TileIndex tile = TileVirtXY(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 (p1 > 0x100 + (uint)GetCustomStationsCount(STAT_CLASS_WAYP)) return CMD_ERROR;
 
	if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
 

	
 
	if (!IsTileType(tile, MP_RAILWAY) || ((dir = 0, _m[tile].m5 != 1) && (dir = 1, _m[tile].m5 != 2)))
 
		return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
 

	
 
	if (!CheckTileOwnership(tile))
 
		return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
	tileh = GetTileSlope(tile, NULL);
 
	if (tileh != 0) {
 
		if (!_patches.build_on_slopes || IsSteepTileh(tileh) || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir)))
 
			return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
 
	}
 

	
 
	/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
 
	wp = FindDeletedWaypointCloseTo(tile);
 
	if (wp == NULL) {
 
		wp = AllocateWaypoint();
 
		if (wp == NULL) return CMD_ERROR;
 

	
 
		wp->town_index = 0;
 
		wp->string = STR_NULL;
 
		wp->town_cn = 0;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		bool reserved = PBSTileReserved(tile) != 0;
 
		ModifyTile(tile, MP_MAP5, RAIL_TYPE_WAYPOINT | dir);
 
		if (--p1 & 0x100) { // waypoint type 0 uses default graphics
 
		if (p1 > 0) { // waypoint type 0 uses default graphics
 
			// custom graphics
 
			_m[tile].m3 |= 16;
 
			_m[tile].m4 = p1 & 0xff;
 
			_m[tile].m4 = (p1 - 1) & 0xff;
 
		}
 
		if (reserved) {
 
			PBSReserveTrack(tile, dir);
 
		} else {
 
			PBSClearTrack(tile, dir);
 
		}
 

	
 
		wp->deleted = 0;
 
		wp->xy = tile;
 
		wp->build_date = _date;
 

	
 
		if (wp->town_index == STR_NULL)
 
			MakeDefaultWaypointName(wp);
 

	
 
		UpdateWaypointSign(wp);
 
		RedrawWaypointSign(wp);
 
	}
 

	
 
	return _price.build_train_depot;
 
}
 

	
 
/* Internal handler to delete a waypoint */
 
static void DoDeleteWaypoint(Waypoint *wp)
 
{
 
@@ -366,56 +366,56 @@ Station *ComposeWaypointStation(TileInde
 
	stat.class_id = 6;
 
	stat.stat_id = wp->stat_id;
 

	
 
	return &stat;
 
}
 

	
 
extern uint16 _custom_sprites_base;
 

	
 

	
 
/* Draw a waypoint */
 
void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype)
 
{
 
	const StationSpec *stat;
 
	uint32 relocation;
 
	const DrawTileSprites *cust;
 
	DrawTileSeqStruct const *seq;
 
	const RailtypeInfo *rti = GetRailTypeInfo(railtype);
 
	uint32 ormod, img;
 

	
 
	ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player));
 

	
 
	x += 33;
 
	y += 17;
 

	
 
	/* draw default waypoint graphics of ID 0 */
 
	if (stat_id == 0) {
 
	stat = GetCustomStation(STAT_CLASS_WAYP, stat_id);
 
	if (stat == NULL) {
 
		// stat is NULL for default waypoints and when waypoint graphics are
 
		// not loaded.
 
		DrawDefaultWaypointSprite(x, y, railtype);
 
		return;
 
	}
 

	
 
	stat = GetCustomStation(STAT_CLASS_WAYP, stat_id - 1);
 
	assert(stat);
 
	relocation = GetCustomStationRelocation(stat, NULL, 1);
 
	// emulate station tile - open with building
 
	// add 1 to get the other direction
 
	cust = &stat->renderdata[2];
 

	
 
	img = cust->ground_sprite;
 
	img += (img < _custom_sprites_base) ? rti->total_offset : railtype;
 

	
 
	if (img & PALETTE_MODIFIER_COLOR) img = (img & SPRITE_MASK);
 
	DrawSprite(img, x, y);
 

	
 
	foreach_draw_tile_seq(seq, cust->seq) {
 
		Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z);
 
		uint32 image = seq->image + relocation;
 
		DrawSprite((image & SPRITE_MASK) | ormod, x + pt.x, y + pt.y);
 
	}
 
}
 

	
 
/* Fix savegames which stored waypoints in their old format */
 
void FixOldWaypoints(void)
 
{
 
	Waypoint *wp;
 

	
 
	/* Convert the old 'town_or_string', to 'string' / 'town' / 'town_cn' */