src/train_cmd.cpp
changeset 8312 8b4944b3b5a3
parent 8305 6191852ae2a2
child 8317 539038de35cb
equal deleted inserted replaced
8311:033318a20764 8312:8b4944b3b5a3
  2815 	if (v->type != VEH_TRAIN) return NULL;
  2815 	if (v->type != VEH_TRAIN) return NULL;
  2816 
  2816 
  2817 	/* get first vehicle now to make most usual checks faster */
  2817 	/* get first vehicle now to make most usual checks faster */
  2818 	Vehicle *coll = v->First();
  2818 	Vehicle *coll = v->First();
  2819 
  2819 
  2820 	/* can't collide with own wagons && can't crash in depot && not too far */
  2820 	/* can't collide with own wagons && can't crash in depot && the same height level */
  2821 	if (coll != tcc->v && v->u.rail.track != TRACK_BIT_DEPOT &&
  2821 	if (coll != tcc->v && v->u.rail.track != TRACK_BIT_DEPOT && abs(v->z_pos - tcc->v->z_pos) < 6) {
  2822 			abs(v->z_pos - tcc->v->z_pos) < 6 &&
  2822 		int x_diff = v->x_pos - tcc->v->x_pos;
  2823 			abs(v->x_pos - tcc->v->x_pos) < 6 &&
  2823 		int y_diff = v->y_pos - tcc->v->y_pos;
  2824 			abs(v->y_pos - tcc->v->y_pos) < 6 ) {
  2824 
  2825 
  2825 		/* needed to disable possible crash of competitor train in station by building diagonal track at its end */
  2826 		/* two drivers + passengers killed in train tcc->v (if it was not crashed already) */
  2826 		if (x_diff * x_diff + y_diff * y_diff > 25) return NULL;
       
  2827 
  2827 		if (!(tcc->v->vehstatus & VS_CRASHED)) {
  2828 		if (!(tcc->v->vehstatus & VS_CRASHED)) {
       
  2829 			/* two drivers + passengers killed in train tcc->v (if it was not crashed already) */
  2828 			tcc->num += 2 + CountPassengersInTrain(tcc->v);
  2830 			tcc->num += 2 + CountPassengersInTrain(tcc->v);
  2829 			SetVehicleCrashed(tcc->v);
  2831 			SetVehicleCrashed(tcc->v);
  2830 		}
  2832 		}
  2831 
  2833 
  2832 		if (!(coll->vehstatus & VS_CRASHED)) {
  2834 		if (!(coll->vehstatus & VS_CRASHED)) {
  3244 		}
  3246 		}
  3245 	}
  3247 	}
  3246 }
  3248 }
  3247 
  3249 
  3248 /** Maximum speeds for train that is broken down or approaching line end */
  3250 /** Maximum speeds for train that is broken down or approaching line end */
  3249 static const byte _breakdown_speeds[16] = {
  3251 static const uint16 _breakdown_speeds[16] = {
  3250 	225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
  3252 	225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
  3251 };
  3253 };
  3252 
  3254 
  3253 
  3255 
  3254 /**
  3256 /**
  3262 {
  3264 {
  3263 	/* Calc position within the current tile */
  3265 	/* Calc position within the current tile */
  3264 	uint x = v->x_pos & 0xF;
  3266 	uint x = v->x_pos & 0xF;
  3265 	uint y = v->y_pos & 0xF;
  3267 	uint y = v->y_pos & 0xF;
  3266 
  3268 
       
  3269 	/* for diagonal directions, 'x' will be 0..15 -
       
  3270 	 * for other directions, it will be 1, 3, 5, ..., 15 */
  3267 	switch (v->direction) {
  3271 	switch (v->direction) {
  3268 		case DIR_N : x = ~x + ~y + 24; break;
  3272 		case DIR_N : x = ~x + ~y + 25; break;
  3269 		case DIR_NW: x = y;            /* FALLTHROUGH */
  3273 		case DIR_NW: x = y;            /* FALLTHROUGH */
  3270 		case DIR_NE: x = ~x + 16;      break;
  3274 		case DIR_NE: x = ~x + 16;      break;
  3271 		case DIR_E : x = ~x + y + 8;   break;
  3275 		case DIR_E : x = ~x + y + 9;   break;
  3272 		case DIR_SE: x = y;            break;
  3276 		case DIR_SE: x = y;            break;
  3273 		case DIR_S : x = x + y - 8;    break;
  3277 		case DIR_S : x = x + y - 7;    break;
  3274 		case DIR_W : x = ~y + x + 8;   break;
  3278 		case DIR_W : x = ~y + x + 9;   break;
  3275 		default: break;
  3279 		default: break;
  3276 	}
  3280 	}
  3277 
  3281 
  3278 	/* do not reverse when approaching red signal */
  3282 	/* do not reverse when approaching red signal */
  3279 	if (!signal && x + 4 >= TILE_SIZE) {
  3283 	if (!signal && x + 4 >= TILE_SIZE) {
  3284 	}
  3288 	}
  3285 
  3289 
  3286 	/* slow down */
  3290 	/* slow down */
  3287 	v->vehstatus |= VS_TRAIN_SLOWING;
  3291 	v->vehstatus |= VS_TRAIN_SLOWING;
  3288 	uint16 break_speed = _breakdown_speeds[x & 0xF];
  3292 	uint16 break_speed = _breakdown_speeds[x & 0xF];
  3289 	if (!(v->direction & 1)) break_speed >>= 1;
       
  3290 	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
  3293 	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
  3291 
  3294 
  3292 	return true;
  3295 	return true;
  3293 }
  3296 }
  3294 
  3297