src/roadveh_cmd.cpp
changeset 8836 f6f1ea3d7e93
parent 8830 b288359ab100
child 8840 332412c2e9c1
equal deleted inserted replaced
8835:8fa962d90b63 8836:f6f1ea3d7e93
   474 	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
   474 	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
   475 
   475 
   476 	if (v->IsInDepot()) return CMD_ERROR;
   476 	if (v->IsInDepot()) return CMD_ERROR;
   477 
   477 
   478 	/* If the current orders are already goto-depot */
   478 	/* If the current orders are already goto-depot */
   479 	if (v->current_order.type == OT_GOTO_DEPOT) {
   479 	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
   480 		if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OF_HALT_IN_DEPOT)) {
   480 		if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OF_HALT_IN_DEPOT)) {
   481 			/* We called with a different DEPOT_SERVICE setting.
   481 			/* We called with a different DEPOT_SERVICE setting.
   482 			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
   482 			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
   483 			 * Note: the if is (true for requesting service == true for ordered to stop in depot) */
   483 			 * Note: the if is (true for requesting service == true for ordered to stop in depot) */
   484 			if (flags & DC_EXEC) {
   484 			if (flags & DC_EXEC) {
   494 			/* If the orders to 'goto depot' are in the orders list (forced servicing),
   494 			/* If the orders to 'goto depot' are in the orders list (forced servicing),
   495 			 * then skip to the next order; effectively cancelling this forced service */
   495 			 * then skip to the next order; effectively cancelling this forced service */
   496 			if (HasBit(v->current_order.flags, OF_PART_OF_ORDERS))
   496 			if (HasBit(v->current_order.flags, OF_PART_OF_ORDERS))
   497 				v->cur_order_index++;
   497 				v->cur_order_index++;
   498 
   498 
   499 			v->current_order.type = OT_DUMMY;
   499 			v->current_order.MakeDummy();
   500 			v->current_order.flags = 0;
       
   501 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
   500 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
   502 		}
   501 		}
   503 		return CommandCost();
   502 		return CommandCost();
   504 	}
   503 	}
   505 
   504 
   506 	dep = FindClosestRoadDepot(v);
   505 	dep = FindClosestRoadDepot(v);
   507 	if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
   506 	if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
   508 
   507 
   509 	if (flags & DC_EXEC) {
   508 	if (flags & DC_EXEC) {
   510 		if (v->current_order.type == OT_LOADING) v->LeaveStation();
   509 		if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
   511 
   510 
   512 		ClearSlot(v);
   511 		ClearSlot(v);
   513 		v->current_order.type = OT_GOTO_DEPOT;
   512 		v->current_order.MakeGoToDepot(dep->index, false);
   514 		v->current_order.flags = OFB_NON_STOP;
       
   515 		if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OF_HALT_IN_DEPOT);
   513 		if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OF_HALT_IN_DEPOT);
   516 		v->current_order.refit_cargo = CT_INVALID;
       
   517 		v->current_order.dest = dep->index;
       
   518 		v->dest_tile = dep->xy;
   514 		v->dest_tile = dep->xy;
   519 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
   515 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
   520 	}
   516 	}
   521 
   517 
   522 	return CommandCost();
   518 	return CommandCost();
  1755 		Station* st = GetStationByTile(v->tile);
  1751 		Station* st = GetStationByTile(v->tile);
  1756 
  1752 
  1757 		/* Vehicle is at the stop position (at a bay) in a road stop.
  1753 		/* Vehicle is at the stop position (at a bay) in a road stop.
  1758 		 * Note, if vehicle is loading/unloading it has already been handled,
  1754 		 * Note, if vehicle is loading/unloading it has already been handled,
  1759 		 * so if we get here the vehicle has just arrived or is just ready to leave. */
  1755 		 * so if we get here the vehicle has just arrived or is just ready to leave. */
  1760 		if (v->current_order.type != OT_LEAVESTATION &&
  1756 		if (!v->current_order.IsType(OT_LEAVESTATION) &&
  1761 				v->current_order.type != OT_GOTO_DEPOT) {
  1757 				!v->current_order.IsType(OT_GOTO_DEPOT)) {
  1762 			/* Vehicle has arrived at a bay in a road stop */
  1758 			/* Vehicle has arrived at a bay in a road stop */
  1763 
  1759 
  1764 			if (IsDriveThroughStopTile(v->tile)) {
  1760 			if (IsDriveThroughStopTile(v->tile)) {
  1765 				TileIndex next_tile = TILE_ADD(v->tile, TileOffsByDir(v->direction));
  1761 				TileIndex next_tile = TILE_ADD(v->tile, TileOffsByDir(v->direction));
  1766 				RoadStopType type = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK;
  1762 				RoadStopType type = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK;
  1793 
  1789 
  1794 			return false;
  1790 			return false;
  1795 		}
  1791 		}
  1796 
  1792 
  1797 		/* Vehicle is ready to leave a bay in a road stop */
  1793 		/* Vehicle is ready to leave a bay in a road stop */
  1798 		if (v->current_order.type != OT_GOTO_DEPOT) {
  1794 		if (!v->current_order.IsType(OT_GOTO_DEPOT)) {
  1799 			if (rs->IsEntranceBusy()) {
  1795 			if (rs->IsEntranceBusy()) {
  1800 				/* Road stop entrance is busy, so wait as there is nowhere else to go */
  1796 				/* Road stop entrance is busy, so wait as there is nowhere else to go */
  1801 				v->cur_speed = 0;
  1797 				v->cur_speed = 0;
  1802 				return false;
  1798 				return false;
  1803 			}
  1799 			}
  1819 				DEBUG(ms, 2, " current tile 0x%X is not destination tile 0x%X. Route problem", v->tile, v->dest_tile);
  1815 				DEBUG(ms, 2, " current tile 0x%X is not destination tile 0x%X. Route problem", v->tile, v->dest_tile);
  1820 			}
  1816 			}
  1821 			if (v->dest_tile != v->u.road.slot->xy) {
  1817 			if (v->dest_tile != v->u.road.slot->xy) {
  1822 				DEBUG(ms, 2, " stop tile 0x%X is not destination tile 0x%X. Multistop desync", v->u.road.slot->xy, v->dest_tile);
  1818 				DEBUG(ms, 2, " stop tile 0x%X is not destination tile 0x%X. Multistop desync", v->u.road.slot->xy, v->dest_tile);
  1823 			}
  1819 			}
  1824 			if (v->current_order.type != OT_GOTO_STATION) {
  1820 			if (!v->current_order.IsType(OT_GOTO_STATION)) {
  1825 				DEBUG(ms, 2, " current order type (%d) is not OT_GOTO_STATION", v->current_order.type);
  1821 				DEBUG(ms, 2, " current order type (%d) is not OT_GOTO_STATION", v->current_order.GetType());
  1826 			} else {
  1822 			} else {
  1827 				if (v->current_order.dest != st->index)
  1823 				if (v->current_order.dest != st->index)
  1828 					DEBUG(ms, 2, " current station %d is not target station in current_order.station (%d)",
  1824 					DEBUG(ms, 2, " current station %d is not target station in current_order.station (%d)",
  1829 							st->index, v->current_order.dest);
  1825 							st->index, v->current_order.dest);
  1830 			}
  1826 			}
  1882 	if (v->vehstatus & VS_STOPPED) return;
  1878 	if (v->vehstatus & VS_STOPPED) return;
  1883 
  1879 
  1884 	ProcessOrders(v);
  1880 	ProcessOrders(v);
  1885 	v->HandleLoading();
  1881 	v->HandleLoading();
  1886 
  1882 
  1887 	if (v->current_order.type == OT_LOADING) return;
  1883 	if (v->current_order.IsType(OT_LOADING)) return;
  1888 
  1884 
  1889 	if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return;
  1885 	if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return;
  1890 
  1886 
  1891 	/* Check if vehicle needs to proceed, return if it doesn't */
  1887 	/* Check if vehicle needs to proceed, return if it doesn't */
  1892 	if (!RoadVehAccelerate(v)) return;
  1888 	if (!RoadVehAccelerate(v)) return;
  1923 
  1919 
  1924 	/* XXX If we already have a depot order, WHY do we search over and over? */
  1920 	/* XXX If we already have a depot order, WHY do we search over and over? */
  1925 	const Depot *depot = FindClosestRoadDepot(v);
  1921 	const Depot *depot = FindClosestRoadDepot(v);
  1926 
  1922 
  1927 	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
  1923 	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
  1928 		if (v->current_order.type == OT_GOTO_DEPOT) {
  1924 		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
  1929 			v->current_order.type = OT_DUMMY;
  1925 			v->current_order.MakeDummy();
  1930 			v->current_order.flags = 0;
       
  1931 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
  1926 			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
  1932 		}
  1927 		}
  1933 		return;
  1928 		return;
  1934 	}
  1929 	}
  1935 
  1930 
  1936 	if (v->current_order.type == OT_GOTO_DEPOT &&
  1931 	if (v->current_order.IsType(OT_GOTO_DEPOT) &&
  1937 			v->current_order.flags & OFB_NON_STOP &&
  1932 			v->current_order.flags & OFB_NON_STOP &&
  1938 			!Chance16(1, 20)) {
  1933 			!Chance16(1, 20)) {
  1939 		return;
  1934 		return;
  1940 	}
  1935 	}
  1941 
  1936 
  1942 	if (v->current_order.type == OT_LOADING) v->LeaveStation();
  1937 	if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
  1943 	ClearSlot(v);
  1938 	ClearSlot(v);
  1944 
  1939 
  1945 	v->current_order.type = OT_GOTO_DEPOT;
  1940 	v->current_order.MakeGoToDepot(depot->index, false);
  1946 	v->current_order.flags = OFB_NON_STOP;
       
  1947 	v->current_order.dest = depot->index;
       
  1948 	v->dest_tile = depot->xy;
  1941 	v->dest_tile = depot->xy;
  1949 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
  1942 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
  1950 }
  1943 }
  1951 
  1944 
  1952 void RoadVehicle::OnNewDay()
  1945 void RoadVehicle::OnNewDay()
  1960 	CheckIfRoadVehNeedsService(this);
  1953 	CheckIfRoadVehNeedsService(this);
  1961 
  1954 
  1962 	CheckOrders(this);
  1955 	CheckOrders(this);
  1963 
  1956 
  1964 	/* Current slot has expired */
  1957 	/* Current slot has expired */
  1965 	if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot != NULL && this->u.road.slot_age-- == 0) {
  1958 	if (this->current_order.IsType(OT_GOTO_STATION) && this->u.road.slot != NULL && this->u.road.slot_age-- == 0) {
  1966 		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
  1959 		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
  1967 			this->unitnumber, this->index, this->u.road.slot->xy);
  1960 			this->unitnumber, this->index, this->u.road.slot->xy);
  1968 		ClearSlot(this);
  1961 		ClearSlot(this);
  1969 	}
  1962 	}
  1970 
  1963 
  1971 	/* update destination */
  1964 	/* update destination */
  1972 	if (!(this->vehstatus & VS_STOPPED) && this->current_order.type == OT_GOTO_STATION && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
  1965 	if (!(this->vehstatus & VS_STOPPED) && this->current_order.IsType(OT_GOTO_STATION) && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
  1973 		Station *st = GetStation(this->current_order.dest);
  1966 		Station *st = GetStation(this->current_order.dest);
  1974 		RoadStop *rs = st->GetPrimaryRoadStop(this);
  1967 		RoadStop *rs = st->GetPrimaryRoadStop(this);
  1975 		RoadStop *best = NULL;
  1968 		RoadStop *best = NULL;
  1976 
  1969 
  1977 		if (rs != NULL) {
  1970 		if (rs != NULL) {