Changeset - r1531:d65d15f6dd48
[Not reviewed]
master
0 1 0
tron - 20 years ago 2005-03-23 08:24:13
tron@openttd.org
(svn r2035) - Remove unneeded realloc()
- Use TileOffsByDir() instead of home brewed table
1 file changed with 6 insertions and 16 deletions:
0 comments (0 inline, 0 general)
train_cmd.c
Show inline comments
 
@@ -78,13 +78,13 @@ static bool TrainShouldStop(Vehicle *v, 
 
	return true;
 
}
 

	
 
//new acceleration
 
static int GetTrainAcceleration(Vehicle *v, bool mode)
 
{
 
	Vehicle *u = v;
 
	const Vehicle *u;
 
	int num = 0;	//number of vehicles, change this into the number of axles later
 
	int power = 0;
 
	int mass = 0;
 
	int max_speed = 2000;
 
	int area = 120;
 
	int friction = 35; //[1e-3]
 
@@ -93,31 +93,30 @@ static int GetTrainAcceleration(Vehicle 
 
	int resistance;
 
	int speed = v->cur_speed; //[mph]
 
	int force = 0x3FFFFFFF;
 
	int pos = 0;
 
	int lastpos = -1;
 
	int curvecount[2] = {0, 0};
 
	int *dist = NULL;
 
	int sum = 0;
 
	int numcurve = 0;
 
	int i;
 

	
 
	speed *= 10;
 
	speed /= 16;
 

	
 
	//first find the curve speed limit
 
	for (; u->next != NULL; u = u->next, pos++) {
 
	for (u = v; u->next != NULL; u = u->next, pos++) {
 
		int dir = u->direction;
 
		int ndir = u->next->direction;
 
		int i;
 

	
 
		for (i = 0; i < 2; i++) {
 
			if ( _curve_neighbours45[dir][i] == ndir) {
 
				curvecount[i]++;
 
				if (lastpos != -1) {
 
					dist = realloc(dist, sizeof(int) * ++numcurve);
 
					dist[numcurve - 1] = pos - lastpos;
 
					numcurve++;
 
					sum += pos - lastpos;
 
					if (pos - lastpos == 1) {
 
						max_speed = 88;
 
					}
 
				}
 
				lastpos = pos;
 
			}
 
@@ -127,17 +126,12 @@ static int GetTrainAcceleration(Vehicle 
 
		if (_curve_neighbours90[dir][0] == ndir ||
 
				_curve_neighbours90[dir][1] == ndir) {
 
			max_speed = 61;
 
		}
 
	}
 

	
 
	for (i = 0; i < numcurve; i++) sum += dist[i];
 

	
 
	free(dist);
 
	dist = NULL;
 

	
 
	if (numcurve > 0) sum /= numcurve;
 

	
 
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
 
		int total = curvecount[0] + curvecount[1];
 

	
 
		if (curvecount[0] == 1 && curvecount[1] == 1) {
 
@@ -147,25 +141,21 @@ static int GetTrainAcceleration(Vehicle 
 
		}
 
	}
 

	
 
	max_speed += (max_speed / 2) * v->u.rail.railtype;
 

	
 
	if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) {
 
		static const TileIndexDiffC _station_dir_from_vdir[] = {
 
			{0, 0}, {-1, 0}, {0, 0}, {0, 1}, {0, 0}, {1, 0}, {0, 0}, {0, -1}
 
		};
 

	
 
		if (TrainShouldStop(v, v->tile)) {
 
			int station_length = 0;
 
			TileIndex tile = v->tile;
 
			int delta_v;
 

	
 
			max_speed = 120;
 
			do {
 
				station_length++;
 
				tile = TILE_ADD(tile, ToTileIndexDiff(_station_dir_from_vdir[v->direction]));
 
				tile = TILE_ADD(tile, TileOffsByDir(v->direction / 2));
 
			} while (IsTileType(tile, MP_STATION));
 

	
 
			delta_v = v->cur_speed / (station_length + 1);
 
			if (v->max_speed > (v->cur_speed - delta_v))
 
				max_speed = v->cur_speed - (delta_v / 10);
 

	
0 comments (0 inline, 0 general)