Skip to content

Commit

Permalink
Change: Cache depth of tile ship is on for speed calculation and NewG…
Browse files Browse the repository at this point in the history
…RF purposes

This is to avoid risks of desync if the depth changes while a ship is on a tile, and one client
considers the old depth and another client (which arrived just now) considers the new depth.
  • Loading branch information
nielsmh committed Jun 28, 2020
1 parent c80a1f0 commit a7bfef2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/newgrf_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
Ship *s = Ship::From(v);
switch (variable - 0x80) {
case 0x62: return s->state;
case 0x66: return GetEffectiveWaterDepth(s->tile);
case 0x66: return s->tile_depth;
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/saveload/vehicle_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
SLE_VAR(Ship, state, SLE_UINT8),
SLE_CONDDEQUE(Ship, path, SLE_UINT8, SLV_SHIP_PATH_CACHE, SL_MAX_VERSION),
SLE_CONDVAR(Ship, rotation, SLE_UINT8, SLV_SHIP_ROTATION, SL_MAX_VERSION),
SLE_CONDVAR(Ship, tile_depth, SLE_UINT8, SLV_WATER_DEPTH, SL_MAX_VERSION),

SLE_CONDNULL(16, SLV_2, SLV_144), // old reserved space

Expand Down
11 changes: 6 additions & 5 deletions src/ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ typedef std::deque<Trackdir> ShipPathCache;
* All ships have this type.
*/
struct Ship FINAL : public SpecializedVehicle<Ship, VEH_SHIP> {
TrackBits state; ///< The "track" the ship is following.
ShipPathCache path; ///< Cached path.
Direction rotation; ///< Visible direction.
int16 rotation_x_pos; ///< NOSAVE: X Position before rotation.
int16 rotation_y_pos; ///< NOSAVE: Y Position before rotation.
TrackBits state; ///< The "track" the ship is following.
ShipPathCache path; ///< Cached path.
Direction rotation; ///< Visible direction.
WaterDepth tile_depth; ///< Cached depth of current tile.
int16 rotation_x_pos; ///< NOSAVE: X Position before rotation.
int16 rotation_y_pos; ///< NOSAVE: Y Position before rotation.

/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
Ship() : SpecializedVehicleBase() {}
Expand Down
7 changes: 5 additions & 2 deletions src/ship_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,11 @@ void Ship::UpdateCache()
{
const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);

/* Update current water depth */
this->tile_depth = GetEffectiveWaterDepth(this->tile);

/* Get speed fraction for the current water type. Aqueducts are always canals. */
bool is_ocean = GetEffectiveWaterDepth(this->tile) >= WATER_DEPTH_DEEP;
bool is_ocean = this->tile_depth >= WATER_DEPTH_DEEP;
uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);

Expand Down Expand Up @@ -754,7 +757,7 @@ static void ShipController(Ship *v)
v->state = TrackToTrackBits(track);

/* Update ship cache when the water depth changes. */
WaterDepth old_depth = GetEffectiveWaterDepth(gp.old_tile);
WaterDepth old_depth = v->tile_depth;
WaterDepth new_depth = GetEffectiveWaterDepth(gp.new_tile);
if (old_depth != new_depth) v->UpdateCache();
}
Expand Down

0 comments on commit a7bfef2

Please sign in to comment.