Changeset - r20480:93a50078f0bb
[Not reviewed]
master
0 1 0
rubidium - 11 years ago 2013-06-28 18:09:07
rubidium@openttd.org
(svn r25495) -Fix [FS#5553]: when addings bits to a (train) station, the train trying to stop there could overshoot the (new) stop location and not stop at all
1 file changed with 3 insertions and 6 deletions:
0 comments (0 inline, 0 general)
src/station_cmd.cpp
Show inline comments
 
@@ -2743,789 +2743,786 @@ static void DrawTile_Station(TileInfo *t
 
		/* Some stations are not owner by a company, namely oil rigs */
 
		palette = PALETTE_TO_GREY;
 
	}
 

	
 
	if (layout == NULL && (t == NULL || t->seq == NULL)) t = GetStationTileLayout(GetStationType(ti->tile), gfx);
 

	
 
	/* don't show foundation for docks */
 
	if (ti->tileh != SLOPE_FLAT && !IsDock(ti->tile)) {
 
		if (statspec != NULL && HasBit(statspec->flags, SSF_CUSTOM_FOUNDATIONS)) {
 
			/* Station has custom foundations.
 
			 * Check whether the foundation continues beyond the tile's upper sides. */
 
			uint edge_info = 0;
 
			int z;
 
			Slope slope = GetFoundationPixelSlope(ti->tile, &z);
 
			if (!HasFoundationNW(ti->tile, slope, z)) SetBit(edge_info, 0);
 
			if (!HasFoundationNE(ti->tile, slope, z)) SetBit(edge_info, 1);
 
			SpriteID image = GetCustomStationFoundationRelocation(statspec, st, ti->tile, tile_layout, edge_info);
 
			if (image == 0) goto draw_default_foundation;
 

	
 
			if (HasBit(statspec->flags, SSF_EXTENDED_FOUNDATIONS)) {
 
				/* Station provides extended foundations. */
 

	
 
				static const uint8 foundation_parts[] = {
 
					0, 0, 0, 0, // Invalid,  Invalid,   Invalid,   SLOPE_SW
 
					0, 1, 2, 3, // Invalid,  SLOPE_EW,  SLOPE_SE,  SLOPE_WSE
 
					0, 4, 5, 6, // Invalid,  SLOPE_NW,  SLOPE_NS,  SLOPE_NWS
 
					7, 8, 9     // SLOPE_NE, SLOPE_ENW, SLOPE_SEN
 
				};
 

	
 
				AddSortableSpriteToDraw(image + foundation_parts[ti->tileh], PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z);
 
			} else {
 
				/* Draw simple foundations, built up from 8 possible foundation sprites. */
 

	
 
				/* Each set bit represents one of the eight composite sprites to be drawn.
 
				 * 'Invalid' entries will not drawn but are included for completeness. */
 
				static const uint8 composite_foundation_parts[] = {
 
					/* Invalid  (00000000), Invalid   (11010001), Invalid   (11100100), SLOPE_SW  (11100000) */
 
					   0x00,                0xD1,                 0xE4,                 0xE0,
 
					/* Invalid  (11001010), SLOPE_EW  (11001001), SLOPE_SE  (11000100), SLOPE_WSE (11000000) */
 
					   0xCA,                0xC9,                 0xC4,                 0xC0,
 
					/* Invalid  (11010010), SLOPE_NW  (10010001), SLOPE_NS  (11100100), SLOPE_NWS (10100000) */
 
					   0xD2,                0x91,                 0xE4,                 0xA0,
 
					/* SLOPE_NE (01001010), SLOPE_ENW (00001001), SLOPE_SEN (01000100) */
 
					   0x4A,                0x09,                 0x44
 
				};
 

	
 
				uint8 parts = composite_foundation_parts[ti->tileh];
 

	
 
				/* If foundations continue beyond the tile's upper sides then
 
				 * mask out the last two pieces. */
 
				if (HasBit(edge_info, 0)) ClrBit(parts, 6);
 
				if (HasBit(edge_info, 1)) ClrBit(parts, 7);
 

	
 
				if (parts == 0) {
 
					/* We always have to draw at least one sprite to make sure there is a boundingbox and a sprite with the
 
					 * correct offset for the childsprites.
 
					 * So, draw the (completely empty) sprite of the default foundations. */
 
					goto draw_default_foundation;
 
				}
 

	
 
				StartSpriteCombine();
 
				for (int i = 0; i < 8; i++) {
 
					if (HasBit(parts, i)) {
 
						AddSortableSpriteToDraw(image + i, PAL_NONE, ti->x, ti->y, 16, 16, 7, ti->z);
 
					}
 
				}
 
				EndSpriteCombine();
 
			}
 

	
 
			OffsetGroundSprite(31, 1);
 
			ti->z += ApplyPixelFoundationToSlope(FOUNDATION_LEVELED, &ti->tileh);
 
		} else {
 
draw_default_foundation:
 
			DrawFoundation(ti, FOUNDATION_LEVELED);
 
		}
 
	}
 

	
 
	if (IsBuoy(ti->tile)) {
 
		DrawWaterClassGround(ti);
 
		SpriteID sprite = GetCanalSprite(CF_BUOY, ti->tile);
 
		if (sprite != 0) total_offset = sprite - SPR_IMG_BUOY;
 
	} else if (IsDock(ti->tile) || (IsOilRig(ti->tile) && IsTileOnWater(ti->tile))) {
 
		if (ti->tileh == SLOPE_FLAT) {
 
			DrawWaterClassGround(ti);
 
		} else {
 
			assert(IsDock(ti->tile));
 
			TileIndex water_tile = ti->tile + TileOffsByDiagDir(GetDockDirection(ti->tile));
 
			WaterClass wc = GetWaterClass(water_tile);
 
			if (wc == WATER_CLASS_SEA) {
 
				DrawShoreTile(ti->tileh);
 
			} else {
 
				DrawClearLandTile(ti, 3);
 
			}
 
		}
 
	} else {
 
		if (layout != NULL) {
 
			/* Sprite layout which needs preprocessing */
 
			bool separate_ground = HasBit(statspec->flags, SSF_SEPARATE_GROUND);
 
			uint32 var10_values = layout->PrepareLayout(total_offset, rti->fallback_railtype, 0, 0, separate_ground);
 
			uint8 var10;
 
			FOR_EACH_SET_BIT(var10, var10_values) {
 
				uint32 var10_relocation = GetCustomStationRelocation(statspec, st, ti->tile, var10);
 
				layout->ProcessRegisters(var10, var10_relocation, separate_ground);
 
			}
 
			tmp_rail_layout.seq = layout->GetLayout(&tmp_rail_layout.ground);
 
			t = &tmp_rail_layout;
 
			total_offset = 0;
 
		} else if (statspec != NULL) {
 
			/* Simple sprite layout */
 
			ground_relocation = relocation = GetCustomStationRelocation(statspec, st, ti->tile, 0);
 
			if (HasBit(statspec->flags, SSF_SEPARATE_GROUND)) {
 
				ground_relocation = GetCustomStationRelocation(statspec, st, ti->tile, 1);
 
			}
 
			ground_relocation += rti->fallback_railtype;
 
		}
 

	
 
		SpriteID image = t->ground.sprite;
 
		PaletteID pal  = t->ground.pal;
 
		RailTrackOffset overlay_offset;
 
		if (rti != NULL && rti->UsesOverlay() && SplitGroundSpriteForOverlay(ti, &image, &overlay_offset)) {
 
			SpriteID ground = GetCustomRailSprite(rti, ti->tile, RTSG_GROUND);
 
			DrawGroundSprite(image, PAL_NONE);
 
			DrawGroundSprite(ground + overlay_offset, PAL_NONE);
 

	
 
			if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasStationReservation(ti->tile)) {
 
				SpriteID overlay = GetCustomRailSprite(rti, ti->tile, RTSG_OVERLAY);
 
				DrawGroundSprite(overlay + overlay_offset, PALETTE_CRASH);
 
			}
 
		} else {
 
			image += HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? ground_relocation : total_offset;
 
			if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += ground_relocation;
 
			DrawGroundSprite(image, GroundSpritePaletteTransform(image, pal, palette));
 

	
 
			/* PBS debugging, draw reserved tracks darker */
 
			if (_game_mode != GM_MENU && _settings_client.gui.show_track_reservation && HasStationRail(ti->tile) && HasStationReservation(ti->tile)) {
 
				const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(ti->tile));
 
				DrawGroundSprite(GetRailStationAxis(ti->tile) == AXIS_X ? rti->base_sprites.single_x : rti->base_sprites.single_y, PALETTE_CRASH);
 
			}
 
		}
 
	}
 

	
 
	if (HasStationRail(ti->tile) && HasCatenaryDrawn(GetRailType(ti->tile))) DrawCatenary(ti);
 

	
 
	if (HasBit(roadtypes, ROADTYPE_TRAM)) {
 
		Axis axis = GetRoadStopDir(ti->tile) == DIAGDIR_NE ? AXIS_X : AXIS_Y;
 
		DrawGroundSprite((HasBit(roadtypes, ROADTYPE_ROAD) ? SPR_TRAMWAY_OVERLAY : SPR_TRAMWAY_TRAM) + (axis ^ 1), PAL_NONE);
 
		DrawTramCatenary(ti, axis == AXIS_X ? ROAD_X : ROAD_Y);
 
	}
 

	
 
	if (IsRailWaypoint(ti->tile)) {
 
		/* Don't offset the waypoint graphics; they're always the same. */
 
		total_offset = 0;
 
	}
 

	
 
	DrawRailTileSeq(ti, t, TO_BUILDINGS, total_offset, relocation, palette);
 
}
 

	
 
void StationPickerDrawSprite(int x, int y, StationType st, RailType railtype, RoadType roadtype, int image)
 
{
 
	int32 total_offset = 0;
 
	PaletteID pal = COMPANY_SPRITE_COLOUR(_local_company);
 
	const DrawTileSprites *t = GetStationTileLayout(st, image);
 
	const RailtypeInfo *rti = NULL;
 

	
 
	if (railtype != INVALID_RAILTYPE) {
 
		rti = GetRailTypeInfo(railtype);
 
		total_offset = rti->GetRailtypeSpriteOffset();
 
	}
 

	
 
	SpriteID img = t->ground.sprite;
 
	RailTrackOffset overlay_offset;
 
	if (rti != NULL && rti->UsesOverlay() && SplitGroundSpriteForOverlay(NULL, &img, &overlay_offset)) {
 
		SpriteID ground = GetCustomRailSprite(rti, INVALID_TILE, RTSG_GROUND);
 
		DrawSprite(img, PAL_NONE, x, y);
 
		DrawSprite(ground + overlay_offset, PAL_NONE, x, y);
 
	} else {
 
		DrawSprite(img + total_offset, HasBit(img, PALETTE_MODIFIER_COLOUR) ? pal : PAL_NONE, x, y);
 
	}
 

	
 
	if (roadtype == ROADTYPE_TRAM) {
 
		DrawSprite(SPR_TRAMWAY_TRAM + (t->ground.sprite == SPR_ROAD_PAVED_STRAIGHT_X ? 1 : 0), PAL_NONE, x, y);
 
	}
 

	
 
	/* Default waypoint has no railtype specific sprites */
 
	DrawRailTileSeqInGUI(x, y, t, st == STATION_WAYPOINT ? 0 : total_offset, 0, pal);
 
}
 

	
 
static int GetSlopePixelZ_Station(TileIndex tile, uint x, uint y)
 
{
 
	return GetTileMaxPixelZ(tile);
 
}
 

	
 
static Foundation GetFoundation_Station(TileIndex tile, Slope tileh)
 
{
 
	return FlatteningFoundation(tileh);
 
}
 

	
 
static void GetTileDesc_Station(TileIndex tile, TileDesc *td)
 
{
 
	td->owner[0] = GetTileOwner(tile);
 
	if (IsDriveThroughStopTile(tile)) {
 
		Owner road_owner = INVALID_OWNER;
 
		Owner tram_owner = INVALID_OWNER;
 
		RoadTypes rts = GetRoadTypes(tile);
 
		if (HasBit(rts, ROADTYPE_ROAD)) road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
 
		if (HasBit(rts, ROADTYPE_TRAM)) tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
 

	
 
		/* Is there a mix of owners? */
 
		if ((tram_owner != INVALID_OWNER && tram_owner != td->owner[0]) ||
 
				(road_owner != INVALID_OWNER && road_owner != td->owner[0])) {
 
			uint i = 1;
 
			if (road_owner != INVALID_OWNER) {
 
				td->owner_type[i] = STR_LAND_AREA_INFORMATION_ROAD_OWNER;
 
				td->owner[i] = road_owner;
 
				i++;
 
			}
 
			if (tram_owner != INVALID_OWNER) {
 
				td->owner_type[i] = STR_LAND_AREA_INFORMATION_TRAM_OWNER;
 
				td->owner[i] = tram_owner;
 
			}
 
		}
 
	}
 
	td->build_date = BaseStation::GetByTile(tile)->build_date;
 

	
 
	if (HasStationTileRail(tile)) {
 
		const StationSpec *spec = GetStationSpec(tile);
 

	
 
		if (spec != NULL) {
 
			td->station_class = StationClass::Get(spec->cls_id)->name;
 
			td->station_name  = spec->name;
 

	
 
			if (spec->grf_prop.grffile != NULL) {
 
				const GRFConfig *gc = GetGRFConfig(spec->grf_prop.grffile->grfid);
 
				td->grf = gc->GetName();
 
			}
 
		}
 

	
 
		const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(tile));
 
		td->rail_speed = rti->max_speed;
 
	}
 

	
 
	if (IsAirport(tile)) {
 
		const AirportSpec *as = Station::GetByTile(tile)->airport.GetSpec();
 
		td->airport_class = AirportClass::Get(as->cls_id)->name;
 
		td->airport_name = as->name;
 

	
 
		const AirportTileSpec *ats = AirportTileSpec::GetByTile(tile);
 
		td->airport_tile_name = ats->name;
 

	
 
		if (as->grf_prop.grffile != NULL) {
 
			const GRFConfig *gc = GetGRFConfig(as->grf_prop.grffile->grfid);
 
			td->grf = gc->GetName();
 
		} else if (ats->grf_prop.grffile != NULL) {
 
			const GRFConfig *gc = GetGRFConfig(ats->grf_prop.grffile->grfid);
 
			td->grf = gc->GetName();
 
		}
 
	}
 

	
 
	StringID str;
 
	switch (GetStationType(tile)) {
 
		default: NOT_REACHED();
 
		case STATION_RAIL:     str = STR_LAI_STATION_DESCRIPTION_RAILROAD_STATION; break;
 
		case STATION_AIRPORT:
 
			str = (IsHangar(tile) ? STR_LAI_STATION_DESCRIPTION_AIRCRAFT_HANGAR : STR_LAI_STATION_DESCRIPTION_AIRPORT);
 
			break;
 
		case STATION_TRUCK:    str = STR_LAI_STATION_DESCRIPTION_TRUCK_LOADING_AREA; break;
 
		case STATION_BUS:      str = STR_LAI_STATION_DESCRIPTION_BUS_STATION; break;
 
		case STATION_OILRIG:   str = STR_INDUSTRY_NAME_OIL_RIG; break;
 
		case STATION_DOCK:     str = STR_LAI_STATION_DESCRIPTION_SHIP_DOCK; break;
 
		case STATION_BUOY:     str = STR_LAI_STATION_DESCRIPTION_BUOY; break;
 
		case STATION_WAYPOINT: str = STR_LAI_STATION_DESCRIPTION_WAYPOINT; break;
 
	}
 
	td->str = str;
 
}
 

	
 

	
 
static TrackStatus GetTileTrackStatus_Station(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
 
{
 
	TrackBits trackbits = TRACK_BIT_NONE;
 

	
 
	switch (mode) {
 
		case TRANSPORT_RAIL:
 
			if (HasStationRail(tile) && !IsStationTileBlocked(tile)) {
 
				trackbits = TrackToTrackBits(GetRailStationTrack(tile));
 
			}
 
			break;
 

	
 
		case TRANSPORT_WATER:
 
			/* buoy is coded as a station, it is always on open water */
 
			if (IsBuoy(tile)) {
 
				trackbits = TRACK_BIT_ALL;
 
				/* remove tracks that connect NE map edge */
 
				if (TileX(tile) == 0) trackbits &= ~(TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT);
 
				/* remove tracks that connect NW map edge */
 
				if (TileY(tile) == 0) trackbits &= ~(TRACK_BIT_Y | TRACK_BIT_LEFT | TRACK_BIT_UPPER);
 
			}
 
			break;
 

	
 
		case TRANSPORT_ROAD:
 
			if ((GetRoadTypes(tile) & sub_mode) != 0 && IsRoadStop(tile)) {
 
				DiagDirection dir = GetRoadStopDir(tile);
 
				Axis axis = DiagDirToAxis(dir);
 

	
 
				if (side != INVALID_DIAGDIR) {
 
					if (axis != DiagDirToAxis(side) || (IsStandardRoadStopTile(tile) && dir != side)) break;
 
				}
 

	
 
				trackbits = AxisToTrackBits(axis);
 
			}
 
			break;
 

	
 
		default:
 
			break;
 
	}
 

	
 
	return CombineTrackStatus(TrackBitsToTrackdirBits(trackbits), TRACKDIR_BIT_NONE);
 
}
 

	
 

	
 
static void TileLoop_Station(TileIndex tile)
 
{
 
	/* FIXME -- GetTileTrackStatus_Station -> animated stationtiles
 
	 * hardcoded.....not good */
 
	switch (GetStationType(tile)) {
 
		case STATION_AIRPORT:
 
			AirportTileAnimationTrigger(Station::GetByTile(tile), tile, AAT_TILELOOP);
 
			break;
 

	
 
		case STATION_DOCK:
 
			if (GetTileSlope(tile) != SLOPE_FLAT) break; // only handle water part
 
			/* FALL THROUGH */
 
		case STATION_OILRIG: //(station part)
 
		case STATION_BUOY:
 
			TileLoop_Water(tile);
 
			break;
 

	
 
		default: break;
 
	}
 
}
 

	
 

	
 
static void AnimateTile_Station(TileIndex tile)
 
{
 
	if (HasStationRail(tile)) {
 
		AnimateStationTile(tile);
 
		return;
 
	}
 

	
 
	if (IsAirport(tile)) {
 
		AnimateAirportTile(tile);
 
	}
 
}
 

	
 

	
 
static bool ClickTile_Station(TileIndex tile)
 
{
 
	const BaseStation *bst = BaseStation::GetByTile(tile);
 

	
 
	if (bst->facilities & FACIL_WAYPOINT) {
 
		ShowWaypointWindow(Waypoint::From(bst));
 
	} else if (IsHangar(tile)) {
 
		const Station *st = Station::From(bst);
 
		ShowDepotWindow(st->airport.GetHangarTile(st->airport.GetHangarNum(tile)), VEH_AIRCRAFT);
 
	} else {
 
		ShowStationViewWindow(bst->index);
 
	}
 
	return true;
 
}
 

	
 
static VehicleEnterTileStatus VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	if (v->type == VEH_TRAIN) {
 
		StationID station_id = GetStationIndex(tile);
 
		if (!v->current_order.ShouldStopAtStation(v, station_id)) return VETSB_CONTINUE;
 
		if (!IsRailStation(tile) || !v->IsFrontEngine()) return VETSB_CONTINUE;
 

	
 
		int station_ahead;
 
		int station_length;
 
		int stop = GetTrainStopLocation(station_id, tile, Train::From(v), &station_ahead, &station_length);
 

	
 
		/* Stop whenever that amount of station ahead + the distance from the
 
		 * begin of the platform to the stop location is longer than the length
 
		 * of the platform. Station ahead 'includes' the current tile where the
 
		 * vehicle is on, so we need to subtract that. */
 
		if (!IsInsideBS(stop + station_ahead, station_length, TILE_SIZE)) return VETSB_CONTINUE;
 
		if (stop + station_ahead - (int)TILE_SIZE >= station_length) return VETSB_CONTINUE;
 

	
 
		DiagDirection dir = DirToDiagDir(v->direction);
 

	
 
		x &= 0xF;
 
		y &= 0xF;
 

	
 
		if (DiagDirToAxis(dir) != AXIS_X) Swap(x, y);
 
		if (y == TILE_SIZE / 2) {
 
			if (dir != DIAGDIR_SE && dir != DIAGDIR_SW) x = TILE_SIZE - 1 - x;
 
			stop &= TILE_SIZE - 1;
 

	
 
			if (x == stop) return VETSB_ENTERED_STATION | (VehicleEnterTileStatus)(station_id << VETS_STATION_ID_OFFSET); // enter station
 
			if (x < stop) {
 
				uint16 spd;
 
			if (x >= stop) return VETSB_ENTERED_STATION | (VehicleEnterTileStatus)(station_id << VETS_STATION_ID_OFFSET); // enter station
 

	
 
				v->vehstatus |= VS_TRAIN_SLOWING;
 
				spd = max(0, (stop - x) * 20 - 15);
 
			uint16 spd = max(0, (stop - x) * 20 - 15);
 
				if (spd < v->cur_speed) v->cur_speed = spd;
 
			}
 
		}
 
	} else if (v->type == VEH_ROAD) {
 
		RoadVehicle *rv = RoadVehicle::From(v);
 
		if (rv->state < RVSB_IN_ROAD_STOP && !IsReversingRoadTrackdir((Trackdir)rv->state) && rv->frame == 0) {
 
			if (IsRoadStop(tile) && rv->IsFrontEngine()) {
 
				/* Attempt to allocate a parking bay in a road stop */
 
				return RoadStop::GetByTile(tile, GetRoadStopType(tile))->Enter(rv) ? VETSB_CONTINUE : VETSB_CANNOT_ENTER;
 
			}
 
		}
 
	}
 

	
 
	return VETSB_CONTINUE;
 
}
 

	
 
/**
 
 * Run the watched cargo callback for all houses in the catchment area.
 
 * @param st Station.
 
 */
 
void TriggerWatchedCargoCallbacks(Station *st)
 
{
 
	/* Collect cargoes accepted since the last big tick. */
 
	uint cargoes = 0;
 
	for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
 
		if (HasBit(st->goods[cid].acceptance_pickup, GoodsEntry::GES_ACCEPTED_BIGTICK)) SetBit(cargoes, cid);
 
	}
 

	
 
	/* Anything to do? */
 
	if (cargoes == 0) return;
 

	
 
	/* Loop over all houses in the catchment. */
 
	Rect r = st->GetCatchmentRect();
 
	TileArea ta(TileXY(r.left, r.top), TileXY(r.right, r.bottom));
 
	TILE_AREA_LOOP(tile, ta) {
 
		if (IsTileType(tile, MP_HOUSE)) {
 
			WatchedCargoCallback(tile, cargoes);
 
		}
 
	}
 
}
 

	
 
/**
 
 * This function is called for each station once every 250 ticks.
 
 * Not all stations will get the tick at the same time.
 
 * @param st the station receiving the tick.
 
 * @return true if the station is still valid (wasn't deleted)
 
 */
 
static bool StationHandleBigTick(BaseStation *st)
 
{
 
	if (!st->IsInUse()) {
 
		if (++st->delete_ctr >= 8) delete st;
 
		return false;
 
	}
 

	
 
	if (Station::IsExpected(st)) {
 
		TriggerWatchedCargoCallbacks(Station::From(st));
 

	
 
		for (CargoID i = 0; i < NUM_CARGO; i++) {
 
			ClrBit(Station::From(st)->goods[i].acceptance_pickup, GoodsEntry::GES_ACCEPTED_BIGTICK);
 
		}
 
	}
 

	
 

	
 
	if ((st->facilities & FACIL_WAYPOINT) == 0) UpdateStationAcceptance(Station::From(st), true);
 

	
 
	return true;
 
}
 

	
 
static inline void byte_inc_sat(byte *p)
 
{
 
	byte b = *p + 1;
 
	if (b != 0) *p = b;
 
}
 

	
 
static void UpdateStationRating(Station *st)
 
{
 
	bool waiting_changed = false;
 

	
 
	byte_inc_sat(&st->time_since_load);
 
	byte_inc_sat(&st->time_since_unload);
 

	
 
	const CargoSpec *cs;
 
	FOR_ALL_CARGOSPECS(cs) {
 
		GoodsEntry *ge = &st->goods[cs->Index()];
 
		/* Slowly increase the rating back to his original level in the case we
 
		 *  didn't deliver cargo yet to this station. This happens when a bribe
 
		 *  failed while you didn't moved that cargo yet to a station. */
 
		if (!ge->HasRating() && ge->rating < INITIAL_STATION_RATING) {
 
			ge->rating++;
 
		}
 

	
 
		/* Only change the rating if we are moving this cargo */
 
		if (ge->HasRating()) {
 
			byte_inc_sat(&ge->time_since_pickup);
 

	
 
			bool skip = false;
 
			int rating = 0;
 
			uint waiting = ge->cargo.TotalCount();
 

	
 
			/* num_dests is at least 1 if there is any cargo as
 
			 * INVALID_STATION is also a destination.
 
			 */
 
			uint num_dests = (uint)ge->cargo.Packets()->MapSize();
 

	
 
			/* Average amount of cargo per next hop, but prefer solitary stations
 
			 * with only one or two next hops. They are allowed to have more
 
			 * cargo waiting per next hop.
 
			 * With manual cargo distribution waiting_avg = waiting / 2 as then
 
			 * INVALID_STATION is the only destination.
 
			 */
 
			uint waiting_avg = waiting / (num_dests + 1);
 

	
 
			if (HasBit(cs->callback_mask, CBM_CARGO_STATION_RATING_CALC)) {
 
				/* Perform custom station rating. If it succeeds the speed, days in transit and
 
				 * waiting cargo ratings must not be executed. */
 

	
 
				/* NewGRFs expect last speed to be 0xFF when no vehicle has arrived yet. */
 
				uint last_speed = ge->HasVehicleEverTriedLoading() ? ge->last_speed : 0xFF;
 

	
 
				uint32 var18 = min(ge->time_since_pickup, 0xFF) | (min(ge->max_waiting_cargo, 0xFFFF) << 8) | (min(last_speed, 0xFF) << 24);
 
				/* Convert to the 'old' vehicle types */
 
				uint32 var10 = (st->last_vehicle_type == VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10);
 
				uint16 callback = GetCargoCallback(CBID_CARGO_STATION_RATING_CALC, var10, var18, cs);
 
				if (callback != CALLBACK_FAILED) {
 
					skip = true;
 
					rating = GB(callback, 0, 14);
 

	
 
					/* Simulate a 15 bit signed value */
 
					if (HasBit(callback, 14)) rating -= 0x4000;
 
				}
 
			}
 

	
 
			if (!skip) {
 
				int b = ge->last_speed - 85;
 
				if (b >= 0) rating += b >> 2;
 

	
 
				byte waittime = ge->time_since_pickup;
 
				if (st->last_vehicle_type == VEH_SHIP) waittime >>= 2;
 
				(waittime > 21) ||
 
				(rating += 25, waittime > 12) ||
 
				(rating += 25, waittime > 6) ||
 
				(rating += 45, waittime > 3) ||
 
				(rating += 35, true);
 

	
 
				(rating -= 90, ge->max_waiting_cargo > 1500) ||
 
				(rating += 55, ge->max_waiting_cargo > 1000) ||
 
				(rating += 35, ge->max_waiting_cargo > 600) ||
 
				(rating += 10, ge->max_waiting_cargo > 300) ||
 
				(rating += 20, ge->max_waiting_cargo > 100) ||
 
				(rating += 10, true);
 
			}
 

	
 
			if (Company::IsValidID(st->owner) && HasBit(st->town->statues, st->owner)) rating += 26;
 

	
 
			byte age = ge->last_age;
 
			(age >= 3) ||
 
			(rating += 10, age >= 2) ||
 
			(rating += 10, age >= 1) ||
 
			(rating += 13, true);
 

	
 
			{
 
				int or_ = ge->rating; // old rating
 

	
 
				/* only modify rating in steps of -2, -1, 0, 1 or 2 */
 
				ge->rating = rating = or_ + Clamp(Clamp(rating, 0, 255) - or_, -2, 2);
 

	
 
				/* if rating is <= 64 and more than 100 items waiting on average per destination,
 
				 * remove some random amount of goods from the station */
 
				if (rating <= 64 && waiting_avg >= 100) {
 
					int dec = Random() & 0x1F;
 
					if (waiting_avg < 200) dec &= 7;
 
					waiting -= (dec + 1) * num_dests;
 
					waiting_changed = true;
 
				}
 

	
 
				/* if rating is <= 127 and there are any items waiting, maybe remove some goods. */
 
				if (rating <= 127 && waiting != 0) {
 
					uint32 r = Random();
 
					if (rating <= (int)GB(r, 0, 7)) {
 
						/* Need to have int, otherwise it will just overflow etc. */
 
						waiting = max((int)waiting - (int)((GB(r, 8, 2) - 1) * num_dests), 0);
 
						waiting_changed = true;
 
					}
 
				}
 

	
 
				/* At some point we really must cap the cargo. Previously this
 
				 * was a strict 4095, but now we'll have a less strict, but
 
				 * increasingly aggressive truncation of the amount of cargo. */
 
				static const uint WAITING_CARGO_THRESHOLD  = 1 << 12;
 
				static const uint WAITING_CARGO_CUT_FACTOR = 1 <<  6;
 
				static const uint MAX_WAITING_CARGO        = 1 << 15;
 

	
 
				if (waiting > WAITING_CARGO_THRESHOLD) {
 
					uint difference = waiting - WAITING_CARGO_THRESHOLD;
 
					waiting -= (difference / WAITING_CARGO_CUT_FACTOR);
 

	
 
					waiting = min(waiting, MAX_WAITING_CARGO);
 
					waiting_changed = true;
 
				}
 

	
 
				/* We can't truncate cargo that's already reserved for loading.
 
				 * Thus StoredCount() here. */
 
				if (waiting_changed && waiting < ge->cargo.AvailableCount()) {
 
					/* Feed back the exact own waiting cargo at this station for the
 
					 * next rating calculation. */
 
					ge->max_waiting_cargo = 0;
 

	
 
					/* If truncating also punish the source stations' ratings to
 
					 * decrease the flow of incoming cargo. */
 

	
 
					StationCargoAmountMap waiting_per_source;
 
					ge->cargo.Truncate(ge->cargo.AvailableCount() - waiting, &waiting_per_source);
 
					for (StationCargoAmountMap::iterator i(waiting_per_source.begin()); i != waiting_per_source.end(); ++i) {
 
						Station *source_station = Station::GetIfValid(i->first);
 
						if (source_station == NULL) continue;
 

	
 
						GoodsEntry &source_ge = source_station->goods[cs->Index()];
 
						source_ge.max_waiting_cargo = max(source_ge.max_waiting_cargo, i->second);
 
					}
 
				} else {
 
					/* If the average number per next hop is low, be more forgiving. */
 
					ge->max_waiting_cargo = waiting_avg;
 
				}
 
			}
 
		}
 
	}
 

	
 
	StationID index = st->index;
 
	if (waiting_changed) {
 
		SetWindowDirty(WC_STATION_VIEW, index); // update whole window
 
	} else {
 
		SetWindowWidgetDirty(WC_STATION_VIEW, index, WID_SV_ACCEPT_RATING_LIST); // update only ratings list
 
	}
 
}
 

	
 
/**
 
 * Reroute cargo of type c at station st or in any vehicles unloading there.
 
 * Make sure the cargo's new next hop is neither "avoid" nor "avoid2".
 
 * @param st Station to be rerouted at.
 
 * @param c Type of cargo.
 
 * @param avoid Original next hop of cargo, avoid this.
 
 * @param avoid2 Another station to be avoided when rerouting.
 
 */
 
void RerouteCargo(Station *st, CargoID c, StationID avoid, StationID avoid2)
 
{
 
	GoodsEntry &ge = st->goods[c];
 

	
 
	/* Reroute cargo in station. */
 
	ge.cargo.Reroute(UINT_MAX, &ge.cargo, avoid, avoid2, &ge);
 

	
 
	/* Reroute cargo staged to be transfered. */
 
	for (std::list<Vehicle *>::iterator it(st->loading_vehicles.begin()); it != st->loading_vehicles.end(); ++it) {
 
		for (Vehicle *v = *it; v != NULL; v = v->Next()) {
 
			if (v->cargo_type != c) continue;
 
			v->cargo.Reroute(UINT_MAX, &v->cargo, avoid, avoid2, &ge);
 
		}
 
	}
 
}
 

	
 
/**
 
 * Check all next hops of cargo packets in this station for existance of a
 
 * a valid link they may use to travel on. Reroute any cargo not having a valid
 
 * link and remove timed out links found like this from the linkgraph. We're
 
 * not all links here as that is expensive and useless. A link no one is using
 
 * doesn't hurt either.
 
 * @param from Station to check.
 
 */
 
void DeleteStaleLinks(Station *from)
 
{
 
	for (CargoID c = 0; c < NUM_CARGO; ++c) {
 
		GoodsEntry &ge = from->goods[c];
 
		LinkGraph *lg = LinkGraph::GetIfValid(ge.link_graph);
 
		if (lg == NULL) continue;
 
		Node node = (*lg)[ge.node];
 
		for (EdgeIterator it(node.Begin()); it != node.End();) {
 
			Edge edge = it->second;
 
			Station *to = Station::Get((*lg)[it->first].Station());
 
			assert(to->goods[c].node == it->first);
 
			++it; // Do that before removing the node. Anything else may crash.
 
			assert(_date >= edge.LastUpdate());
 
			if ((uint)(_date - edge.LastUpdate()) > LinkGraph::MIN_TIMEOUT_DISTANCE +
 
					(DistanceManhattan(from->xy, to->xy) >> 2)) {
 
				node.RemoveEdge(to->goods[c].node);
 
				ge.flows.DeleteFlows(to->index);
 
				RerouteCargo(from, c, to->index, from->index);
 
			}
 
		}
 
		assert(_date >= lg->LastCompression());
 
		if ((uint)(_date - lg->LastCompression()) > LinkGraph::COMPRESSION_INTERVAL) {
 
			lg->Compress();
 
		}
 
	}
 
}
 

	
 
/**
 
 * Increase capacity for a link stat given by station cargo and next hop.
 
 * @param st Station to get the link stats from.
 
 * @param cargo Cargo to increase stat for.
 
 * @param next_station_id Station the consist will be travelling to next.
 
 * @param capacity Capacity to add to link stat.
 
 * @param usage Usage to add to link stat. If UINT_MAX refresh the link instead of increasing.
 
 */
 
void IncreaseStats(Station *st, CargoID cargo, StationID next_station_id, uint capacity, uint usage)
 
{
 
	GoodsEntry &ge1 = st->goods[cargo];
 
	Station *st2 = Station::Get(next_station_id);
 
	GoodsEntry &ge2 = st2->goods[cargo];
 
	LinkGraph *lg = NULL;
 
	if (ge1.link_graph == INVALID_LINK_GRAPH) {
 
		if (ge2.link_graph == INVALID_LINK_GRAPH) {
 
			if (LinkGraph::CanAllocateItem()) {
 
				lg = new LinkGraph(cargo);
 
				LinkGraphSchedule::Instance()->Queue(lg);
 
				ge2.link_graph = lg->index;
 
				ge2.node = lg->AddNode(st2);
 
			} else {
 
				DEBUG(misc, 0, "Can't allocate link graph");
 
			}
 
		} else {
 
			lg = LinkGraph::Get(ge2.link_graph);
 
		}
 
		if (lg) {
 
			ge1.link_graph = lg->index;
 
			ge1.node = lg->AddNode(st);
 
		}
 
	} else if (ge2.link_graph == INVALID_LINK_GRAPH) {
 
		lg = LinkGraph::Get(ge1.link_graph);
 
		ge2.link_graph = lg->index;
 
		ge2.node = lg->AddNode(st2);
 
	} else {
 
		lg = LinkGraph::Get(ge1.link_graph);
 
		if (ge1.link_graph != ge2.link_graph) {
 
			LinkGraph *lg2 = LinkGraph::Get(ge2.link_graph);
 
			if (lg->Size() < lg2->Size()) {
 
				LinkGraphSchedule::Instance()->Unqueue(lg);
 
				lg2->Merge(lg); // Updates GoodsEntries of lg
 
				lg = lg2;
 
			} else {
 
				LinkGraphSchedule::Instance()->Unqueue(lg2);
 
				lg->Merge(lg2); // Updates GoodsEntries of lg2
 
			}
 
		}
 
	}
 
	if (lg != NULL) {
 
		(*lg)[ge1.node].UpdateEdge(ge2.node, capacity, usage);
 
	}
 
}
 

	
 
/**
 
 * Increase capacity for all link stats associated with vehicles in the given consist.
 
 * @param st Station to get the link stats from.
 
 * @param front First vehicle in the consist.
 
 * @param next_station_id Station the consist will be travelling to next.
 
 */
 
void IncreaseStats(Station *st, const Vehicle *front, StationID next_station_id)
 
{
 
	for (const Vehicle *v = front; v != NULL; v = v->Next()) {
 
		if (v->refit_cap > 0) {
 
			/* The cargo count can indeed be higher than the refit_cap if
 
			 * wagons have been auto-replaced and subsequently auto-
 
			 * refitted to a higher capacity. The cargo gets redistributed
 
			 * among the wagons in that case.
 
			 * As usage is not such an important figure anyway we just
 
			 * ignore the additional cargo then.*/
 
			IncreaseStats(st, v->cargo_type, next_station_id, v->refit_cap,
 
				min(v->refit_cap, v->cargo.StoredCount()));
 
		}
 
	}
 
}
 

	
 
/* called for every station each tick */
 
static void StationHandleSmallTick(BaseStation *st)
 
{
 
	if ((st->facilities & FACIL_WAYPOINT) != 0 || !st->IsInUse()) return;
 

	
 
	byte b = st->delete_ctr + 1;
 
	if (b >= STATION_RATING_TICKS) b = 0;
 
	st->delete_ctr = b;
 

	
 
	if (b == 0) UpdateStationRating(Station::From(st));
 
}
 

	
 
void OnTick_Station()
 
{
 
	if (_game_mode == GM_EDITOR) return;
 

	
 
	BaseStation *st;
0 comments (0 inline, 0 general)