Changeset - r22:d13b1ad89539
[Not reviewed]
master
0 11 0
darkvater - 20 years ago 2004-08-11 22:07:08
darkvater@openttd.org
(svn r23) -Some omments on the code (blathijs)
11 files changed with 69 insertions and 10 deletions:
0 comments (0 inline, 0 general)
macros.h
Show inline comments
 
@@ -127,13 +127,20 @@ extern uint SafeTileAdd(uint x, int add,
 
#define CLRBITS(x,y) ((x) &= ~(y))
 

	
 
#define PLAYER_SPRITE_COLOR(owner) ((_player_colors[owner] << 16) + 0x3070000)
 
#define SPRITE_PALETTE(x) ((x) + 0x8000)
 

	
 
extern const byte _ffb_64[128];
 
/* Returns the position of the first bit that is not zero, counted from the
 
 * left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns
 
 * 0.
 
 */
 
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
 
/* Returns x with the first bit that is not zero, counted from the left, set
 
 * to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc.
 
 */
 
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64]
 

	
 
static INLINE int FindFirstBit2x64(int value)
 
{
 
	int i = 0;
 
	if ( (byte) value == 0) {
 
@@ -209,7 +216,9 @@ static INLINE void swap_tile(TileIndex *
 

	
 
static INLINE void WRITE_LE_UINT16(const void *b, uint16 x) {
 
	((byte*)b)[0] = (byte)x;
 
	((byte*)b)[1] = (byte)(x >> 8);
 
}
 

	
 
#define MAX_DETOUR 6
 

	
 
#endif /* MACROS_H */
pathfind.c
Show inline comments
 
@@ -184,12 +184,15 @@ continue_here:;
 
	} while (++i, bits>>=1);
 

	
 
}
 

	
 
static const int8 _get_tunlen_inc[5] = { -16, 0, 16, 0, -16 };
 

	
 
/* Returns the end tile and the length of a tunnel. The length does not
 
 * include the starting tile (entry), it does include the end tile (exit).
 
 */
 
FindLengthOfTunnelResult FindLengthOfTunnel(uint tile, int direction, byte type)
 
{
 
	FindLengthOfTunnelResult flotr;
 
	int x,y;
 
	byte z;
 

	
rail_cmd.c
Show inline comments
 
@@ -5,13 +5,13 @@
 
#include "command.h"
 
#include "pathfind.h"
 
#include "town.h"
 

	
 
void ShowTrainDepotWindow(uint tile);
 

	
 
enum {
 
enum { /* These values are bitmasks for the map5 byte */
 
	RAIL_TYPE_NORMAL = 0,
 
	RAIL_TYPE_SIGNALS = 0x40,
 
	RAIL_TYPE_SPECIAL = 0x80, // If this bit is set, then it's not a regular track.
 
	RAIL_TYPE_DEPOT = 0xC0,
 
	RAIL_TYPE_MASK = 0xC0,
 

	
 
@@ -37,12 +37,15 @@ enum {
 

	
 
/* Format of rail map5 byte.
 
 * 00 abcdef  => Normal rail
 
 * 01 abcdef  => Rail with signals
 
 * 10 ??????  => Unused
 
 * 11 ????dd  => Depot
 
 *
 
 * abcdef is a bitmask, which contains ones for all present tracks. Below the
 
 * value for each track is given. 
 
 */
 

	
 
/*         4
 
 *     ---------
 
 *    |\       /|
 
 *    | \    1/ |
 
@@ -74,14 +77,18 @@ enum RailMap2Lower4 {
 
	RAIL_GROUND_FENCE_HORIZ1 = 10,
 
	RAIL_GROUND_FENCE_HORIZ2 = 11,
 
	RAIL_GROUND_ICE_DESERT = 12,
 
};
 

	
 

	
 
/* MAP2 byte:    abcd???? => Signal On?
 
 * MAP3LO byte:  abcd???? => Signal Exists?
 
/* MAP2 byte:    abcd???? => Signal On? Same coding as map3lo
 
 * MAP3LO byte:  abcd???? => Signal Exists? 
 
 *				 a and b are for diagonals, upper and left,
 
 *				 one for each direction. (ie a == NE->SW, b ==
 
 *				 SW->NE, or v.v., I don't know. b and c are
 
 *				 similar for lower and right.
 
 * MAP2 byte:    ????abcd => Type of ground.
 
 * MAP3LO byte:  ????abcd => Type of rail.
 
 * MAP5:         00abcdef => rail
 
 *               01abcdef => rail w/ signals
 
 *               10uuuuuu => unused
 
 *               11uuuudd => rail depot   
 
@@ -1793,21 +1800,26 @@ static uint32 GetTileTrackStatus_Track(u
 
	m5 = _map5[tile];
 

	
 
	if (!(m5 & RAIL_TYPE_SPECIAL)) {
 
		ret = (m5 | (m5 << 8)) & 0x3F3F;
 
		if (!(m5 & RAIL_TYPE_SIGNALS)) {
 
			if ( (ret & 0xFF) == 3)
 
			/* Diagonal crossing? */
 
				ret |= 0x40;
 
		} else {
 
			/* has_signals */
 

	
 
			a = _map3_lo[tile];
 
			b = _map2[tile];
 

	
 
			b &= a;
 

	
 
			/* When signals are not present (in neither
 
			 * direction), we pretend them to be green. (So if
 
			 * signals are only one way, the other way will
 
			 * implicitely become `red' */
 
			if ((a & 0xC0) == 0) { b |= 0xC0; }
 
			if ((a & 0x30) == 0) { b |= 0x30; }
 

	
 
			if ( (b & 0x80) == 0)	ret |= 0x10070000;
 
			if ( (b & 0x40) == 0)	ret |= 0x7100000;
 
			if ( (b & 0x20) == 0)	ret |= 0x20080000;
road_cmd.c
Show inline comments
 
@@ -4,12 +4,16 @@
 
#include "viewport.h"
 
#include "command.h"
 
#include "player.h"
 
#include "town.h"
 
#include "gfx.h"
 

	
 
/* When true, GetTrackStatus for roads will treat roads under reconstruction
 
 * as normal roads instead of impassable. This is used when detecting whether
 
 * a road can be removed. This is of course ugly, but I don't know a better
 
 * solution just like that... */
 
static bool _road_special_gettrackstatus;
 

	
 
void RoadVehEnterDepot(Vehicle *v);
 

	
 

	
 
static bool HasTileRoadAt(uint tile, int i)
station_cmd.c
Show inline comments
 
@@ -2229,14 +2229,14 @@ uint MoveGoodsToStation(uint tile, int w
 
				if (around[i] == 0xFF) {
 
					st = DEREF_STATION(st_index);
 
					if ((st->had_vehicle_of_type & HVOT_BUOY) == 0 &&
 
							st->blocked_months == 0 &&
 
							st->goods[type].rating != 0 && 
 
							(!_patches.selectgoods || st->goods[type].last_speed) && // if last_speed is 0, no vehicle has been there.
 
							((st->facilities & (byte)~FACIL_BUS_STOP)!=0 || type==CT_PASSENGERS) &&
 
							((st->facilities & (byte)~FACIL_TRUCK_STOP)!=0 || type!=CT_PASSENGERS)) {
 
							((st->facilities & (byte)~FACIL_BUS_STOP)!=0 || type==CT_PASSENGERS) && // if we have other fac. than a bus stop, or the cargo is passengers
 
							((st->facilities & (byte)~FACIL_TRUCK_STOP)!=0 || type!=CT_PASSENGERS)) { // if we have other fac. than a cargo bay or the cargo is not passengers
 
						
 
						around[i] = st_index;
 
						around_ptr[i] = st;
 
					}
 
					break;
 
				} else if (around[i] == st_index)
train_cmd.c
Show inline comments
 
@@ -1918,12 +1918,18 @@ static int CountPassengersInTrain(Vehicl
 
	BEGIN_ENUM_WAGONS(v)
 
		if (v->cargo_type == 0) num += v->cargo_count;
 
	END_ENUM_WAGONS(v)
 
	return num;
 
}
 

	
 
/*
 
 * Checks whether the specified tried has a collision with another vehicle. If
 
 * so, destroys this vehicle, and the other vehicle if its subtype is 0 (?).
 
 * Reports the incident in a flashy news item, modifies station ratings and
 
 * plays a sound.
 
 */
 
static void CheckTrainCollision(Vehicle *v)
 
{
 
	TrainCollideChecker tcc;
 
	Vehicle *coll;
 
	int num;
 

	
 
@@ -1988,24 +1994,26 @@ static void TrainController(Vehicle *v)
 
	uint32 r, tracks,ts;
 
	int dir, i;
 
	byte chosen_dir;
 
	byte chosen_track;
 
	byte old_z;
 

	
 
	/* For every vehicle after and including the given vehicle */
 
	for(;;) {
 
		BeginVehicleMove(v);
 
		
 
		if (v->u.rail.track != 0x40) {
 
			/* Not inside tunnel */
 
			if (GetNewVehiclePos(v, &gp)) {
 
				/* Statying in the old tile */
 
				/* Staying in the old tile */
 
				if (v->u.rail.track == 0x80) {
 
					/* inside depot */
 
					gp.x = v->x_pos;
 
					gp.y = v->y_pos;
 
				} else {
 
					/* isnot inside depot */
 
					/* is not inside depot */
 
					r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
 
					if (r & 0x8)
 
						goto invalid_rail;
 
					if (r & 0x2) {
 
						TrainEnterStation(v, r >> 8);
 
						return;
 
@@ -2076,12 +2084,14 @@ static void TrainController(Vehicle *v)
 
					v->u.rail.track = chosen_track;
 
				}
 

	
 
				if (v->subtype == 0)
 
					TrainMovedChangeSignals(gp.new_tile, dir>>1);
 

	
 
				/* Signals can only change when the first
 
				 * (above) or the last vehicle moves. */
 
				if (v->next == NULL)
 
					TrainMovedChangeSignals(gp.old_tile, (dir>>1) ^ 2);
 
				
 
				if (prev == NULL) {
 
					AffectSpeedByDirChange(v, chosen_dir);
 
				}
 
@@ -2114,30 +2124,33 @@ common:;
 
		v->y_pos = gp.y;
 

	
 
		/* update the Z position of the vehicle */
 
		old_z = AfterSetTrainPos(v);
 
		
 
		if (prev == NULL) {
 
			/* This is the first vehicle in the train */
 
			AffectSpeedByZChange(v, old_z);
 
			CheckTrainCollision(v);
 
		}
 

	
 
next_vehicle:;
 
		/* continue with next vehicle */
 
next_vehicle:;
 
		prev = v;
 
		if ((v=v->next) == NULL)
 
			return;
 
	}
 

	
 
invalid_rail:
 
	/* We've reached end of line?? */
 
	if (prev != NULL) {
 
		error("!Disconnecting train");
 
	}
 
	goto reverse_train_direction;
 

	
 
red_light: {
 
	/* We're in front of a red signal ?? */
 
		/* find the first set bit in ts. need to do it in 2 steps, since
 
		 * FIND_FIRST_BIT only handles 6 bits at a time. */
 
		i = FindFirstBit2x64(ts);
 
		
 
		if (!(_map3_lo[gp.new_tile] & _signal_otherdir[i])) {
 
			v->cur_speed = 0;
 
@@ -2304,20 +2317,23 @@ static void TrainCheckIfLineEnds(Vehicle
 
				return;
 
	
 
	// depot?
 
	if (IS_TILETYPE(tile, MP_RAILWAY) && (_map5[tile] & 0xFC) == 0xC0)
 
		return;
 

	
 
	// determine the tracks on the next tile.
 
	/* Determine the non-diagonal direction in which we will exit this tile */
 
	t = v->direction >> 1;
 
	if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
 
		t = (t - 1) & 3;
 
	}
 
	/* Calculate next tile */
 
	tile += _tileoffs_by_dir[t];
 
	// determine the track status on the next tile.
 
	ts = GetTileTrackStatus(tile, 0) & _reachable_tracks[t];
 
	
 
	/* Calc position within the current tile ?? */
 
	x = v->x_pos & 0xF;
 
	y = v->y_pos & 0xF;
 
	
 
	switch(v->direction) {
 
	case 0:
 
		x = (~x) + (~y) + 24;
train_gui.c
Show inline comments
 
@@ -313,14 +313,16 @@ static void DrawTrainDepotWindow(Window 
 
				v->subtype == 0 &&
 
				v->tile == (TileIndex)tile &&
 
				v->u.rail.track == 0x80 && 
 
				--num < 0 && num >= -6) {
 

	
 
			DrawTrainImage(v, x+21, y, 10, w->hscroll.pos, WP(w,traindepot_d).sel);
 
			/* Draw the train number */
 
			SET_DPARAM16(0, v->unitnumber);
 
			DrawString(x, y, (v->max_age - 366 < v->age) ? STR_00E3 : STR_00E2, 0);
 
			/* Draw the pretty flag */
 
			DrawSprite(v->vehstatus&VS_STOPPED ? 0xC12 : 0xC13, x+15, y);
 

	
 
			y += 14;
 
		}
 
	}
 

	
ttd.h
Show inline comments
 
@@ -233,12 +233,16 @@ typedef void GetTileDescProc(uint tile, 
 
typedef uint32 GetTileTrackStatusProc(uint tile, int mode);
 
typedef void GetProducedCargoProc(uint tile, byte *b);
 
typedef void ClickTileProc(uint tile);
 
typedef void AnimateTileProc(uint tile);
 
typedef void TileLoopProc(uint tile);
 
typedef void ChangeTileOwnerProc(uint tile, byte old_player, byte new_player);
 
/* Return value has bit 0x2 set, when the vehicle enters a station. Then,
 
 * result << 8 contains the id of the station entered. If the return value has
 
 * bit 0x8 set, the vehicle could not and did not enter the tile. Are there
 
 * other bits that can be set? */
 
typedef uint32 VehicleEnterTileProc(Vehicle *v, uint tile, int x, int y);
 
typedef void VehicleLeaveTileProc(Vehicle *v, uint tile, int x, int y);
 

	
 
typedef struct {
 
	DrawTileProc *draw_tile_proc;
 
	GetSlopeZProc *get_slope_z_proc;
tunnelbridge_cmd.c
Show inline comments
 
@@ -1385,12 +1385,13 @@ static uint32 VehicleEnter_TunnelBridge(
 
					v->vehstatus |= VS_HIDDEN;
 
					return 4;
 
				}
 
			}
 

	
 
			if (dir == (vdir^2) && fc == _tunnel_fractcoord_3[dir] && z == 0) {
 
				/* We're at the tunnel exit ?? */
 
				v->tile = tile;
 
				v->u.rail.track = _exit_tunnel_track[dir];
 
				v->vehstatus &= ~VS_HIDDEN;
 
				return 4;
 
			}
 
		} else if (v->type == VEH_Road) {
 
@@ -1410,12 +1411,13 @@ static uint32 VehicleEnter_TunnelBridge(
 
				} else {
 
					return 0;
 
				}
 
			}
 

	
 
			if (dir == (vdir^2) && (
 
				/* We're at the tunnel exit ?? */
 
					fc == _tunnel_fractcoord_6[dir] ||
 
					fc == _tunnel_fractcoord_7[dir]) &&
 
					z == 0) {
 
				v->tile = tile;
 
				v->u.road.state = _road_exit_tunnel_state[dir];
 
				v->u.road.frame = _road_exit_tunnel_frame[dir];
vehicle.c
Show inline comments
 
@@ -1455,17 +1455,24 @@ byte GetDirectionTowards(Vehicle *v, int
 
	dirdiff = _new_direction_table[i] - dir;
 
	if (dirdiff == 0)
 
		return dir;
 
	return (dir+((dirdiff&7)<5?1:-1)) & 7;
 
}
 

	
 
/* Return value has bit 0x2 set, when the vehicle enters a station. Then,
 
 * result << 8 contains the id of the station entered. If the return value has
 
 * bit 0x8 set, the vehicle could not and did not enter the tile. Are there
 
 * other bits that can be set? */
 
uint32 VehicleEnterTile(Vehicle *v, uint tile, int x, int y)
 
{	
 
	uint old_tile = v->tile;
 
	uint32 result = _tile_type_procs[GET_TILETYPE(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
 
	
 
	/* When vehicle_enter_tile_proc returns 8, that apparently means that
 
	 * we cannot enter the tile at all. In that case, don't call
 
	 * leave_tile. */
 
	if (!(result & 8) && old_tile != tile) {
 
		VehicleLeaveTileProc *proc = _tile_type_procs[GET_TILETYPE(old_tile)]->vehicle_leave_tile_proc;
 
		if (proc != NULL)
 
			proc(v, old_tile, x, y);
 
	}
 
	return result;
vehicle.h
Show inline comments
 
@@ -82,13 +82,13 @@ struct WorldSprite {
 

	
 
	byte flags;				// draw flags
 
};
 

	
 
struct Vehicle {
 
	byte type;				// type, ie roadven,train,ship,aircraft,special
 
	byte subtype;			// subtype
 
	byte subtype;			// subtype (for trains, 0 == loco, 4 wagon ??)
 

	
 
	uint16 index;			// NOSAVE: Index in vehicle array
 
	uint16 next_in_chain_old; // Next vehicle index for chained vehicles
 

	
 
	Vehicle *next;		// next
 

	
0 comments (0 inline, 0 general)