File diff r7468:f5c9dc0e7bc9 → r7469:b4e494b560de
src/roadveh_cmd.cpp
Show inline comments
 
@@ -785,42 +785,35 @@ static void ProcessRoadVehOrder(Vehicle 
 
	}
 

	
 
	if (order->type  == v->current_order.type &&
 
			order->flags == v->current_order.flags &&
 
			order->dest  == v->current_order.dest) {
 
		return;
 
	}
 

	
 
	v->current_order = *order;
 

	
 
	switch (order->type) {
 
		case OT_GOTO_STATION: {
 
			const RoadStop* rs;
 

	
 
			if (order->dest == v->last_station_visited) {
 
				v->last_station_visited = INVALID_STATION;
 
			}
 

	
 
			rs = GetStation(order->dest)->GetPrimaryRoadStop(
 
				IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK
 
			);
 
			const RoadStop *rs = GetStation(order->dest)->GetPrimaryRoadStop(v);
 

	
 
			TileIndex dest = INVALID_TILE;
 
			if (rs != NULL) {
 
				uint mindist = MAX_UVALUE(uint);
 

	
 
				for (; rs != NULL; rs = rs->next) {
 
					/* The vehicle cannot go to this roadstop */
 
					if ((GetRoadTypes(rs->xy) & v->u.road.compatible_roadtypes) == ROADTYPES_NONE) continue;
 

	
 
				for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
 
					uint dist = DistanceManhattan(v->tile, rs->xy);
 

	
 
					if (dist < mindist) {
 
						mindist = dist;
 
						dest = rs->xy;
 
					}
 
				}
 
			}
 

	
 
			if (dest != INVALID_TILE) {
 
					v->dest_tile = dest;
 
			} else {
 
@@ -1145,26 +1138,27 @@ static Trackdir RoadFindPathToDest(Vehic
 

	
 
	uint32 r  = GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
 
	TrackdirBits signal    = (TrackdirBits)GB(r, 16, 16);
 
	TrackdirBits trackdirs = (TrackdirBits)GB(r,  0, 16);
 

	
 
	if (IsTileType(tile, MP_ROAD)) {
 
		if (GetRoadTileType(tile) == ROAD_TILE_DEPOT && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->u.road.compatible_roadtypes) == 0)) {
 
			/* Road depot owned by another player or with the wrong orientation */
 
			trackdirs = TRACKDIR_BIT_NONE;
 
		}
 
	} else if (IsTileType(tile, MP_STATION) && IsStandardRoadStopTile(tile)) {
 
		/* Standard road stop (drive-through stops are treated as normal road) */
 
		if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir) {
 
			/* different station owner or wrong orientation */
 

	
 
		if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir || RoadVehHasArticPart(v)) {
 
			/* different station owner or wrong orientation or the vehicle has articulated parts */
 
			trackdirs = TRACKDIR_BIT_NONE;
 
		} else {
 
			/* Our station */
 
			RoadStop::Type rstype = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK;
 

	
 
			if (GetRoadStopType(tile) != rstype) {
 
				/* Wrong station type */
 
				trackdirs = TRACKDIR_BIT_NONE;
 
			} else {
 
				/* Proper station type, check if there is free loading bay */
 
				if (!_patches.roadveh_queue &&  IsStandardRoadStopTile(tile) &&
 
						!GetRoadStopByTile(tile, rstype)->HasFreeBay()) {
 
@@ -1914,43 +1908,43 @@ void OnNewDay_RoadVeh(Vehicle *v)
 
	/* Current slot has expired */
 
	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) {
 
		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
 
			v->unitnumber, v->index, v->u.road.slot->xy);
 
		ClearSlot(v);
 
	}
 

	
 
	if (v->vehstatus & VS_STOPPED) return;
 

	
 
	/* update destination */
 
	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) {
 
		Station* st = GetStation(v->current_order.dest);
 
		RoadStop* rs = st->GetPrimaryRoadStop(IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK);
 
		RoadStop *rs = st->GetPrimaryRoadStop(v);
 
		RoadStop* best = NULL;
 

	
 
		if (rs != NULL) {
 
			/* We try to obtain a slot if:
 
			 * 1) we're reasonably close to the primary road stop
 
			 * or
 
			 * 2) we're somewhere close to the station rectangle (to make sure we do assign
 
			 *    slots even if the station and its road stops are incredibly spread out)
 
			 */
 
			if (DistanceManhattan(v->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(v->tile), TileY(v->tile), 2)) {
 
				uint dist, badness;
 
				uint minbadness = UINT_MAX;
 

	
 
				DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
 
					v->unitnumber, v->index, st->index, st->xy
 
				);
 
				/* Now we find the nearest road stop that has a free slot */
 
				for (; rs != NULL; rs = rs->next) {
 
				for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
 
					dist = RoadFindPathToStop(v, rs->xy);
 
					if (dist == UINT_MAX) {
 
						DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
 
						continue;
 
					}
 
					badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
 

	
 
					DEBUG(ms, 4, " stop 0x%X has %d vehicle%s waiting", rs->xy, rs->num_vehicles, rs->num_vehicles == 1 ? "":"s");
 
					DEBUG(ms, 4, " distance is %u", dist);
 
					DEBUG(ms, 4, " badness %u", badness);
 

	
 
					if (badness < minbadness) {