Skip to content

Commit

Permalink
Fix TP time overflow problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Violet-Nonbloosom committed Mar 21, 2024
1 parent d51c82e commit 8fe3084
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,15 @@ public int getTeleportationTime(int complexity, @Nonnull Location source, @Nonnu
return 100;
}

int speed = 50_000 + complexity * complexity;
int unsafeTime = Math.min(4 * distanceSquared(source, destination) / speed, 40);
long speed = 50_000L + (long)complexity * complexity;
int distance = 4 * distanceSquared(source, destination), time = 1;

// Fixes #3573 - Using Math.max is a safer way to ensure values > 0 than relying on addition.
return Math.max(1, unsafeTime);
// If speed is greater than distance, ultimate time cost must be 1 tick.
// Otherwise, speed WON'T overflow the range of int.
if (speed <= distance)
time = Math.min(distance / (int)speed, 40);

return time;
}

@ParametersAreNonnullByDefault
Expand Down

0 comments on commit 8fe3084

Please sign in to comment.