src/train_cmd.cpp
changeset 8827 730524764a69
parent 8794 4a3ee468d97a
child 8830 b288359ab100
equal deleted inserted replaced
8826:8fbb67bcf292 8827:730524764a69
     9 #include "tile_cmd.h"
     9 #include "tile_cmd.h"
    10 #include "landscape.h"
    10 #include "landscape.h"
    11 #include "gui.h"
    11 #include "gui.h"
    12 #include "station_map.h"
    12 #include "station_map.h"
    13 #include "tunnel_map.h"
    13 #include "tunnel_map.h"
    14 #include "timetable.h"
       
    15 #include "articulated_vehicles.h"
    14 #include "articulated_vehicles.h"
    16 #include "command_func.h"
    15 #include "command_func.h"
    17 #include "pathfind.h"
    16 #include "pathfind.h"
    18 #include "npf.h"
    17 #include "npf.h"
    19 #include "station_base.h"
    18 #include "station_base.h"
  2614 	}
  2613 	}
  2615 
  2614 
  2616 	return reverse_best != 0;
  2615 	return reverse_best != 0;
  2617 }
  2616 }
  2618 
  2617 
  2619 static bool ProcessTrainOrder(Vehicle *v)
  2618 TileIndex Train::GetOrderStationLocation(StationID station)
  2620 {
  2619 {
  2621 	switch (v->current_order.type) {
  2620 	return GetStation(station)->xy;
  2622 		case OT_GOTO_DEPOT:
       
  2623 			if (!(v->current_order.flags & OFB_PART_OF_ORDERS)) return false;
       
  2624 			if ((v->current_order.flags & OFB_SERVICE_IF_NEEDED) &&
       
  2625 					!VehicleNeedsService(v)) {
       
  2626 				UpdateVehicleTimetable(v, true);
       
  2627 				v->cur_order_index++;
       
  2628 			}
       
  2629 			break;
       
  2630 
       
  2631 		case OT_LOADING:
       
  2632 		case OT_LEAVESTATION:
       
  2633 			return false;
       
  2634 
       
  2635 		default: break;
       
  2636 	}
       
  2637 
       
  2638 	/**
       
  2639 	 * Reversing because of order change is allowed only just after leaving a
       
  2640 	 * station (and the difficulty setting to allowed, of course)
       
  2641 	 * this can be detected because only after OT_LEAVESTATION, current_order
       
  2642 	 * will be reset to nothing. (That also happens if no order, but in that case
       
  2643 	 * it won't hit the point in code where may_reverse is checked)
       
  2644 	 */
       
  2645 	bool may_reverse = v->current_order.type == OT_NOTHING;
       
  2646 
       
  2647 	/* check if we've reached the waypoint? */
       
  2648 	if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) {
       
  2649 		UpdateVehicleTimetable(v, true);
       
  2650 		v->cur_order_index++;
       
  2651 	}
       
  2652 
       
  2653 	/* check if we've reached a non-stop station while TTDPatch nonstop is enabled.. */
       
  2654 	if (_patches.new_nonstop &&
       
  2655 			v->current_order.flags & OFB_NON_STOP &&
       
  2656 			IsTileType(v->tile, MP_STATION) &&
       
  2657 			v->current_order.dest == GetStationIndex(v->tile)) {
       
  2658 		UpdateVehicleTimetable(v, true);
       
  2659 		v->cur_order_index++;
       
  2660 	}
       
  2661 
       
  2662 	/* Get the current order */
       
  2663 	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
       
  2664 
       
  2665 	const Order *order = GetVehicleOrder(v, v->cur_order_index);
       
  2666 
       
  2667 	/* If no order, do nothing. */
       
  2668 	if (order == NULL) {
       
  2669 		v->current_order.Free();
       
  2670 		v->dest_tile = 0;
       
  2671 		return false;
       
  2672 	}
       
  2673 
       
  2674 	/* If it is unchanged, keep it. */
       
  2675 	if (order->type  == v->current_order.type &&
       
  2676 			order->flags == v->current_order.flags &&
       
  2677 			order->dest  == v->current_order.dest)
       
  2678 		return false;
       
  2679 
       
  2680 	/* Otherwise set it, and determine the destination tile. */
       
  2681 	v->current_order = *order;
       
  2682 
       
  2683 	v->dest_tile = 0;
       
  2684 
       
  2685 	InvalidateVehicleOrder(v);
       
  2686 
       
  2687 	switch (order->type) {
       
  2688 		case OT_GOTO_STATION:
       
  2689 			if (order->dest == v->last_station_visited)
       
  2690 				v->last_station_visited = INVALID_STATION;
       
  2691 			v->dest_tile = GetStation(order->dest)->xy;
       
  2692 			break;
       
  2693 
       
  2694 		case OT_GOTO_DEPOT:
       
  2695 			v->dest_tile = GetDepot(order->dest)->xy;
       
  2696 			break;
       
  2697 
       
  2698 		case OT_GOTO_WAYPOINT:
       
  2699 			v->dest_tile = GetWaypoint(order->dest)->xy;
       
  2700 			break;
       
  2701 
       
  2702 		default:
       
  2703 			return false;
       
  2704 	}
       
  2705 
       
  2706 	return may_reverse && CheckReverseTrain(v);
       
  2707 }
  2621 }
  2708 
  2622 
  2709 void Train::MarkDirty()
  2623 void Train::MarkDirty()
  2710 {
  2624 {
  2711 	Vehicle *v = this;
  2625 	Vehicle *v = this;
  3563 	}
  3477 	}
  3564 
  3478 
  3565 	/* exit if train is stopped */
  3479 	/* exit if train is stopped */
  3566 	if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return;
  3480 	if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return;
  3567 
  3481 
  3568 	if (ProcessTrainOrder(v)) {
  3482 	if (ProcessOrders(v) && CheckReverseTrain(v)) {
  3569 		v->load_unload_time_rem = 0;
  3483 		v->load_unload_time_rem = 0;
  3570 		v->cur_speed = 0;
  3484 		v->cur_speed = 0;
  3571 		v->subspeed = 0;
  3485 		v->subspeed = 0;
  3572 		ReverseTrainDirection(v);
  3486 		ReverseTrainDirection(v);
  3573 		return;
  3487 		return;