Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: solve teleportation time overflow problem. #4163

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,16 @@ 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);
int time = 1;
Violet-Nonbloosom marked this conversation as resolved.
Show resolved Hide resolved

// 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;
Violet-Nonbloosom marked this conversation as resolved.
Show resolved Hide resolved
}

@ParametersAreNonnullByDefault
Expand Down