src/train_cmd.cpp
changeset 9206 12d35b56014c
parent 9202 6c1679b46549
child 9208 f376b12acb1b
equal deleted inserted replaced
9205:eb91034aca64 9206:12d35b56014c
    50 
    50 
    51 #include "table/strings.h"
    51 #include "table/strings.h"
    52 #include "table/train_cmd.h"
    52 #include "table/train_cmd.h"
    53 
    53 
    54 static bool TrainCheckIfLineEnds(Vehicle *v);
    54 static bool TrainCheckIfLineEnds(Vehicle *v);
    55 static void TrainController(Vehicle *v, bool update_image);
    55 static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image);
    56 static TileIndex TrainApproachingCrossingTile(const Vehicle *v);
    56 static TileIndex TrainApproachingCrossingTile(const Vehicle *v);
    57 
    57 
    58 static const byte _vehicle_initial_x_fract[4] = {10, 8, 4,  8};
    58 static const byte _vehicle_initial_x_fract[4] = {10, 8, 4,  8};
    59 static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
    59 static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
    60 
    60 
  1733 		for (uint i = length - 3; i > 0; i--) last = last->Next();
  1733 		for (uint i = length - 3; i > 0; i--) last = last->Next();
  1734 
  1734 
  1735 		int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
  1735 		int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
  1736 		if (before) differential *= -1;
  1736 		if (before) differential *= -1;
  1737 
  1737 
  1738 		if (differential > 0) {
  1738 		/* do not update images now
  1739 			/* disconnect last car to make sure only this subset moves */
  1739 		 * negative differential will are handled in the second run */
  1740 			Vehicle *tempnext = last->Next();
  1740 		for (int i = 0; i < differential; i++) TrainController(first, last->Next(), false);
  1741 			last->SetNext(NULL);
       
  1742 
       
  1743 			/* do not update images now because the wagons are disconnected
       
  1744 			 * and that could cause problems with NewGRFs */
       
  1745 			for (int i = 0; i < differential; i++) TrainController(first, false);
       
  1746 
       
  1747 			last->SetNext(tempnext);
       
  1748 		}
       
  1749 
  1741 
  1750 		base = first;
  1742 		base = first;
  1751 		first = first->Next();
  1743 		first = first->Next();
  1752 		length -= 2;
  1744 		length -= 2;
  1753 	}
  1745 	}
  2991 		if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v;
  2983 		if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v;
  2992 	}
  2984 	}
  2993 	return NULL;
  2985 	return NULL;
  2994 }
  2986 }
  2995 
  2987 
  2996 static void TrainController(Vehicle *v, bool update_image)
  2988 static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image)
  2997 {
  2989 {
  2998 	Vehicle *prev;
  2990 	Vehicle *prev;
  2999 
  2991 
  3000 	/* For every vehicle after and including the given vehicle */
  2992 	/* For every vehicle after and including the given vehicle */
  3001 	for (prev = v->Previous(); v != NULL; prev = v, v = v->Next()) {
  2993 	for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) {
  3002 		DiagDirection enterdir = DIAGDIR_BEGIN;
  2994 		DiagDirection enterdir = DIAGDIR_BEGIN;
  3003 		bool update_signals_crossing = false; // will we update signals or crossing state?
  2995 		bool update_signals_crossing = false; // will we update signals or crossing state?
  3004 		BeginVehicleMove(v);
  2996 		BeginVehicleMove(v);
  3005 
  2997 
  3006 		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
  2998 		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
  3552 		if (v->cur_speed != 0) return;
  3544 		if (v->cur_speed != 0) return;
  3553 	} else {
  3545 	} else {
  3554 		TrainCheckIfLineEnds(v);
  3546 		TrainCheckIfLineEnds(v);
  3555 
  3547 
  3556 		do {
  3548 		do {
  3557 			TrainController(v, true);
  3549 			TrainController(v, NULL, true);
  3558 			CheckTrainCollision(v);
  3550 			CheckTrainCollision(v);
  3559 			if (v->cur_speed <= 0x100)
  3551 			if (v->cur_speed <= 0x100)
  3560 				break;
  3552 				break;
  3561 		} while (--j != 0);
  3553 		} while (--j != 0);
  3562 	}
  3554 	}