src/train_cmd.cpp
changeset 9775 22e256c3bf46
parent 9774 190734cb1113
child 9808 a914bcdca6fb
equal deleted inserted replaced
9774:190734cb1113 9775:22e256c3bf46
  1690 /**
  1690 /**
  1691  * Check if the vehicle is a train
  1691  * Check if the vehicle is a train
  1692  * @param v vehicle on tile
  1692  * @param v vehicle on tile
  1693  * @return v if it is a train, NULL otherwise
  1693  * @return v if it is a train, NULL otherwise
  1694  */
  1694  */
  1695 static void *TrainOnTileEnum(Vehicle *v, void *)
  1695 static Vehicle *TrainOnTileEnum(Vehicle *v, void *)
  1696 {
  1696 {
  1697 	return (v->type == VEH_TRAIN) ? v : NULL;
  1697 	return (v->type == VEH_TRAIN) ? v : NULL;
  1698 }
  1698 }
  1699 
  1699 
  1700 
  1700 
  1702  * Checks if a train is approaching a rail-road crossing
  1702  * Checks if a train is approaching a rail-road crossing
  1703  * @param v vehicle on tile
  1703  * @param v vehicle on tile
  1704  * @param data tile with crossing we are testing
  1704  * @param data tile with crossing we are testing
  1705  * @return v if it is approaching a crossing, NULL otherwise
  1705  * @return v if it is approaching a crossing, NULL otherwise
  1706  */
  1706  */
  1707 static void *TrainApproachingCrossingEnum(Vehicle *v, void *data)
  1707 static Vehicle *TrainApproachingCrossingEnum(Vehicle *v, void *data)
  1708 {
  1708 {
  1709 	/* not a train || not front engine || crashed */
  1709 	/* not a train || not front engine || crashed */
  1710 	if (v->type != VEH_TRAIN || !IsFrontEngine(v) || v->vehstatus & VS_CRASHED) return NULL;
  1710 	if (v->type != VEH_TRAIN || !IsFrontEngine(v) || v->vehstatus & VS_CRASHED) return NULL;
  1711 
  1711 
  1712 	TileIndex tile = *(TileIndex*)data;
  1712 	TileIndex tile = *(TileIndex*)data;
  1728 	assert(IsLevelCrossingTile(tile));
  1728 	assert(IsLevelCrossingTile(tile));
  1729 
  1729 
  1730 	DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
  1730 	DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
  1731 	TileIndex tile_from = tile + TileOffsByDiagDir(dir);
  1731 	TileIndex tile_from = tile + TileOffsByDiagDir(dir);
  1732 
  1732 
  1733 	Vehicle *v = (Vehicle*)VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
  1733 	Vehicle *v = VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
  1734 
  1734 
  1735 	if (v != NULL) return v;
  1735 	if (v != NULL) return v;
  1736 
  1736 
  1737 	dir = ReverseDiagDir(dir);
  1737 	dir = ReverseDiagDir(dir);
  1738 	tile_from = tile + TileOffsByDiagDir(dir);
  1738 	tile_from = tile + TileOffsByDiagDir(dir);
  1739 
  1739 
  1740 	return (Vehicle*)VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
  1740 	return VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum);
  1741 }
  1741 }
  1742 
  1742 
  1743 
  1743 
  1744 /**
  1744 /**
  1745  * Sets correct crossing state
  1745  * Sets correct crossing state
  1750 void UpdateLevelCrossing(TileIndex tile, bool sound)
  1750 void UpdateLevelCrossing(TileIndex tile, bool sound)
  1751 {
  1751 {
  1752 	assert(IsLevelCrossingTile(tile));
  1752 	assert(IsLevelCrossingTile(tile));
  1753 
  1753 
  1754 	/* train on crossing || train approaching crossing */
  1754 	/* train on crossing || train approaching crossing */
  1755 	bool new_state = VehicleFromPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile);
  1755 	bool new_state = VehicleFromPos(tile, NULL, &TrainOnTileEnum) != NULL || TrainApproachingCrossing(tile);
  1756 
  1756 
  1757 	if (new_state != IsCrossingBarred(tile)) {
  1757 	if (new_state != IsCrossingBarred(tile)) {
  1758 		if (new_state && sound) {
  1758 		if (new_state && sound) {
  1759 			SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
  1759 			SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
  1760 		}
  1760 		}
  2894 struct TrainCollideChecker {
  2894 struct TrainCollideChecker {
  2895 	Vehicle *v;  ///< vehicle we are testing for collision
  2895 	Vehicle *v;  ///< vehicle we are testing for collision
  2896 	uint num;    ///< number of dead if train collided
  2896 	uint num;    ///< number of dead if train collided
  2897 };
  2897 };
  2898 
  2898 
  2899 static void *FindTrainCollideEnum(Vehicle *v, void *data)
  2899 static Vehicle *FindTrainCollideEnum(Vehicle *v, void *data)
  2900 {
  2900 {
  2901 	TrainCollideChecker *tcc = (TrainCollideChecker*)data;
  2901 	TrainCollideChecker *tcc = (TrainCollideChecker*)data;
  2902 
  2902 
  2903 	if (v->type != VEH_TRAIN) return NULL;
  2903 	if (v->type != VEH_TRAIN) return NULL;
  2904 
  2904 
  2966 
  2966 
  2967 	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
  2967 	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
  2968 	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
  2968 	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
  2969 }
  2969 }
  2970 
  2970 
  2971 static void *CheckVehicleAtSignal(Vehicle *v, void *data)
  2971 static Vehicle *CheckVehicleAtSignal(Vehicle *v, void *data)
  2972 {
  2972 {
  2973 	DiagDirection exitdir = *(DiagDirection *)data;
  2973 	DiagDirection exitdir = *(DiagDirection *)data;
  2974 
  2974 
  2975 	/* front engine of a train, not inside wormhole or depot, not crashed */
  2975 	/* front engine of a train, not inside wormhole or depot, not crashed */
  2976 	if (v->type == VEH_TRAIN && IsFrontEngine(v) && (v->u.rail.track & TRACK_BIT_MASK) != 0 && !(v->vehstatus & VS_CRASHED)) {
  2976 	if (v->type == VEH_TRAIN && IsFrontEngine(v) && (v->u.rail.track & TRACK_BIT_MASK) != 0 && !(v->vehstatus & VS_CRASHED)) {