Skip to content

Commit

Permalink
Fix: Equalise the number of frames needed for road vehicles to traver…
Browse files Browse the repository at this point in the history
…se different radius curves
  • Loading branch information
SamuXarick committed Jan 25, 2021
1 parent 67fa5a2 commit f72f17b
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 24 deletions.
256 changes: 256 additions & 0 deletions src/saveload/afterload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3127,6 +3127,262 @@ bool AfterLoadGame()
}
}

if (IsSavegameVersionBefore(SLV_ROADVEH_MOVEMENT_DATA)) {
/* Equalise the number of frames needed for road vehicles to traverse different radius curves.
* Convert old 'x' and 'y' positions to reflect the newer movement data.
* Then, fix articulated road vehicles. Some curves were shorter than other curves.
* Now they have the same length, but that means that trailing articulated parts will
* take longer to go through the curve than the parts in front which already left the curve.
* So, make articulated parts catch up. */
std::vector<uint> skip_frames;
for (RoadVehicle *v : RoadVehicle::Iterate()) {
if (!v->IsFrontEngine()) continue;
bool changed = false;
skip_frames.clear();
TileIndex prev_tile = v->tile;
uint prev_tile_skip = 0;
uint cur_skip = 0;
for (RoadVehicle *u = v; u != nullptr; u = u->Next()) {
if (u->tile != prev_tile) {
prev_tile_skip = cur_skip;
prev_tile = u->tile;
} else {
cur_skip = prev_tile_skip;
}

uint &this_skip = skip_frames.emplace_back(prev_tile_skip);

switch ((u->state + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->overtaking) {
case 2: // Going SE -> E -> NE, in the inner curve.
switch (u->frame) {
case 2:
if (u->direction == DIR_E) {
u->direction = DIR_SE;
u->y_pos++;
u->frame++;
changed = true;
}
break;

case 3:
case 4:
case 5:
case 6:
case 7:
u->x_pos++;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 10: // Going SW -> W -> NW, in the outer curve.
switch (u->frame) {
case 5:
if (u->direction == DIR_W) {
u->direction = DIR_SW;
u->x_pos++;
u->frame++;
changed = true;
}
break;

case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
u->y_pos++;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 12: // Going NE -> N -> NW, in the inner curve.
switch (u->frame) {
case 2:
if (u->direction == DIR_N) {
u->direction = DIR_NE;
u->x_pos--;
u->frame++;
changed = true;
}
break;

case 3:
case 4:
case 5:
case 6:
case 7:
u->y_pos++;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 13: // Going NW -> N -> NE, in the outer curve.
switch (u->frame) {
case 5:
if (u->direction == DIR_N) {
u->direction = DIR_NW;
u->y_pos--;
u->frame++;
changed = true;
}
break;

case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
u->x_pos++;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 18: // Going SE -> E -> NE, in the outer curve.
switch (u->frame) {
case 5:
if (u->direction == DIR_E) {
u->direction = DIR_SE;
u->y_pos++;
u->frame++;
changed = true;
}
break;

case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
u->x_pos++;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 20: // Going SE -> S -> SW, in the inner curve.
switch (u->frame) {
case 1:
if (u->direction == DIR_S) {
u->direction = DIR_SE;
u->y_pos++;
u->frame++;
changed = true;
}
break;

case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
u->x_pos--;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 21: // Going SW -> S -> SE, in the outer curve.
switch (u->frame) {
case 4:
if (u->direction == DIR_S) {
u->direction = DIR_SW;
u->x_pos++;
u->frame++;
changed = true;
}
break;

case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
u->y_pos--;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;

case 26: // Going SW -> W -> NW, in the inner curve.
switch (u->frame) {
case 2:
if (u->direction == DIR_W) {
u->direction = DIR_SW;
u->x_pos++;
u->frame++;
changed = true;
}
break;

case 3:
case 4:
case 5:
case 6:
case 7:
u->y_pos++;
changed = true;
break;
}
cur_skip++;
this_skip = cur_skip;
break;
}
if (changed) {
u->UpdateInclination(false, true);
changed = false;
}
}
while (cur_skip > skip_frames[0]) {
RoadVehicle *u = v;
RoadVehicle *prev = nullptr;
for (uint sf : skip_frames) {
extern bool IndividualRoadVehicleController(RoadVehicle * v, const RoadVehicle * prev);
if (sf >= cur_skip) IndividualRoadVehicleController(u, prev);

prev = u;
u = u->Next();
}
cur_skip--;
}
}
}

/* Compute station catchment areas. This is needed here in case UpdateStationAcceptance is called below. */
Station::RecomputeCatchmentForAll();

Expand Down
2 changes: 2 additions & 0 deletions src/saveload/saveload.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ enum SaveLoadVersion : uint16 {
SLV_VEH_MOTION_COUNTER, ///< 288 PR#8591 Desync safe motion counter
SLV_INDUSTRY_TEXT, ///< 289 PR#8576 Additional GS text for industries.

SLV_ROADVEH_MOVEMENT_DATA, ///< 290 PR#8609 Road vehicle movement data frame changes.

SL_MAX_VERSION, ///< Highest possible saveload version
};

Expand Down
56 changes: 32 additions & 24 deletions src/table/roadveh_movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ static const RoadDriveEntry _roadveh_drive_data_2[] = {
{5, 0},
{5, 1},
{5, 2},
{4, 3},
{3, 4},
{5, 3},
{4, 4},
{3, 5},
{2, 5},
{1, 5},
{0, 5},
Expand Down Expand Up @@ -165,9 +166,10 @@ static const RoadDriveEntry _roadveh_drive_data_10[] = {
{3, 9},
{4, 9},
{5, 9},
{6, 8},
{7, 7},
{8, 6},
{6, 9},
{7, 8},
{8, 7},
{9, 6},
{9, 5},
{9, 4},
{9, 3},
Expand All @@ -192,9 +194,10 @@ static const RoadDriveEntry _roadveh_drive_data_12[] = {
{15, 5},
{14, 5},
{13, 5},
{12, 4},
{11, 3},
{10, 2},
{12, 5},
{11, 4},
{10, 3},
{ 9, 2},
{ 9, 1},
{ 9, 0},
{RDE_NEXT_TILE | DIAGDIR_NW, 0}
Expand All @@ -206,10 +209,11 @@ static const RoadDriveEntry _roadveh_drive_data_13[] = {
{9, 12},
{9, 11},
{9, 10},
{8, 9},
{7, 8},
{6, 7},
{5, 6},
{9, 9},
{8, 8},
{7, 7},
{6, 6},
{5, 5},
{4, 5},
{3, 5},
{2, 5},
Expand Down Expand Up @@ -276,9 +280,10 @@ static const RoadDriveEntry _roadveh_drive_data_18[] = {
{9, 3},
{9, 4},
{9, 5},
{8, 6},
{7, 7},
{6, 8},
{9, 6},
{8, 7},
{7, 8},
{6, 9},
{5, 9},
{4, 9},
{3, 9},
Expand All @@ -302,9 +307,10 @@ static const RoadDriveEntry _roadveh_drive_data_19[] = {
static const RoadDriveEntry _roadveh_drive_data_20[] = {
{ 9, 0},
{ 9, 1},
{10, 2},
{11, 3},
{12, 4},
{ 9, 2},
{10, 3},
{11, 4},
{12, 5},
{13, 5},
{14, 5},
{15, 5},
Expand All @@ -316,10 +322,11 @@ static const RoadDriveEntry _roadveh_drive_data_21[] = {
{2, 5},
{3, 5},
{4, 5},
{5, 6},
{6, 7},
{7, 8},
{8, 9},
{5, 5},
{6, 6},
{7, 7},
{8, 8},
{9, 9},
{9, 10},
{9, 11},
{9, 12},
Expand Down Expand Up @@ -384,8 +391,9 @@ static const RoadDriveEntry _roadveh_drive_data_26[] = {
{0, 5},
{1, 5},
{2, 5},
{3, 4},
{4, 3},
{3, 5},
{4, 4},
{5, 3},
{5, 2},
{5, 1},
{5, 0},
Expand Down

0 comments on commit f72f17b

Please sign in to comment.