train_cmd.c
changeset 1531 4ee3c51e2b32
parent 1530 caa16c506a22
child 1542 2ca6d1624e6d
equal deleted inserted replaced
1530:caa16c506a22 1531:4ee3c51e2b32
    79 }
    79 }
    80 
    80 
    81 //new acceleration
    81 //new acceleration
    82 static int GetTrainAcceleration(Vehicle *v, bool mode)
    82 static int GetTrainAcceleration(Vehicle *v, bool mode)
    83 {
    83 {
    84 	Vehicle *u = v;
    84 	const Vehicle *u;
    85 	int num = 0;	//number of vehicles, change this into the number of axles later
    85 	int num = 0;	//number of vehicles, change this into the number of axles later
    86 	int power = 0;
    86 	int power = 0;
    87 	int mass = 0;
    87 	int mass = 0;
    88 	int max_speed = 2000;
    88 	int max_speed = 2000;
    89 	int area = 120;
    89 	int area = 120;
    94 	int speed = v->cur_speed; //[mph]
    94 	int speed = v->cur_speed; //[mph]
    95 	int force = 0x3FFFFFFF;
    95 	int force = 0x3FFFFFFF;
    96 	int pos = 0;
    96 	int pos = 0;
    97 	int lastpos = -1;
    97 	int lastpos = -1;
    98 	int curvecount[2] = {0, 0};
    98 	int curvecount[2] = {0, 0};
    99 	int *dist = NULL;
       
   100 	int sum = 0;
    99 	int sum = 0;
   101 	int numcurve = 0;
   100 	int numcurve = 0;
   102 	int i;
       
   103 
   101 
   104 	speed *= 10;
   102 	speed *= 10;
   105 	speed /= 16;
   103 	speed /= 16;
   106 
   104 
   107 	//first find the curve speed limit
   105 	//first find the curve speed limit
   108 	for (; u->next != NULL; u = u->next, pos++) {
   106 	for (u = v; u->next != NULL; u = u->next, pos++) {
   109 		int dir = u->direction;
   107 		int dir = u->direction;
   110 		int ndir = u->next->direction;
   108 		int ndir = u->next->direction;
       
   109 		int i;
   111 
   110 
   112 		for (i = 0; i < 2; i++) {
   111 		for (i = 0; i < 2; i++) {
   113 			if ( _curve_neighbours45[dir][i] == ndir) {
   112 			if ( _curve_neighbours45[dir][i] == ndir) {
   114 				curvecount[i]++;
   113 				curvecount[i]++;
   115 				if (lastpos != -1) {
   114 				if (lastpos != -1) {
   116 					dist = realloc(dist, sizeof(int) * ++numcurve);
   115 					numcurve++;
   117 					dist[numcurve - 1] = pos - lastpos;
   116 					sum += pos - lastpos;
   118 					if (pos - lastpos == 1) {
   117 					if (pos - lastpos == 1) {
   119 						max_speed = 88;
   118 						max_speed = 88;
   120 					}
   119 					}
   121 				}
   120 				}
   122 				lastpos = pos;
   121 				lastpos = pos;
   128 				_curve_neighbours90[dir][1] == ndir) {
   127 				_curve_neighbours90[dir][1] == ndir) {
   129 			max_speed = 61;
   128 			max_speed = 61;
   130 		}
   129 		}
   131 	}
   130 	}
   132 
   131 
   133 	for (i = 0; i < numcurve; i++) sum += dist[i];
       
   134 
       
   135 	free(dist);
       
   136 	dist = NULL;
       
   137 
       
   138 	if (numcurve > 0) sum /= numcurve;
   132 	if (numcurve > 0) sum /= numcurve;
   139 
   133 
   140 	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
   134 	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
   141 		int total = curvecount[0] + curvecount[1];
   135 		int total = curvecount[0] + curvecount[1];
   142 
   136 
   148 	}
   142 	}
   149 
   143 
   150 	max_speed += (max_speed / 2) * v->u.rail.railtype;
   144 	max_speed += (max_speed / 2) * v->u.rail.railtype;
   151 
   145 
   152 	if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) {
   146 	if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) {
   153 		static const TileIndexDiffC _station_dir_from_vdir[] = {
       
   154 			{0, 0}, {-1, 0}, {0, 0}, {0, 1}, {0, 0}, {1, 0}, {0, 0}, {0, -1}
       
   155 		};
       
   156 
       
   157 		if (TrainShouldStop(v, v->tile)) {
   147 		if (TrainShouldStop(v, v->tile)) {
   158 			int station_length = 0;
   148 			int station_length = 0;
   159 			TileIndex tile = v->tile;
   149 			TileIndex tile = v->tile;
   160 			int delta_v;
   150 			int delta_v;
   161 
   151 
   162 			max_speed = 120;
   152 			max_speed = 120;
   163 			do {
   153 			do {
   164 				station_length++;
   154 				station_length++;
   165 				tile = TILE_ADD(tile, ToTileIndexDiff(_station_dir_from_vdir[v->direction]));
   155 				tile = TILE_ADD(tile, TileOffsByDir(v->direction / 2));
   166 			} while (IsTileType(tile, MP_STATION));
   156 			} while (IsTileType(tile, MP_STATION));
   167 
   157 
   168 			delta_v = v->cur_speed / (station_length + 1);
   158 			delta_v = v->cur_speed / (station_length + 1);
   169 			if (v->max_speed > (v->cur_speed - delta_v))
   159 			if (v->max_speed > (v->cur_speed - delta_v))
   170 				max_speed = v->cur_speed - (delta_v / 10);
   160 				max_speed = v->cur_speed - (delta_v / 10);