Add a timeout to a chunk check

This commit is contained in:
zontreck 2025-03-10 11:52:35 -07:00
parent c69e1de64b
commit cf7084c164
3 changed files with 33 additions and 10 deletions

View file

@ -52,7 +52,13 @@ public class RTPFactory
{ {
PlayerPosition PPos = data.LastPosition.GetPlayerPosition(); PlayerPosition PPos = data.LastPosition.GetPlayerPosition();
BlockPos check = data.LastPosition.GetBlockPos(); BlockPos check = data.LastPosition.GetBlockPos();
var BA = ServerUtilities.API.World.BlockAccessor;
check.Y = 1; check.Y = 1;
int height = BA.GetTerrainMapheightAt(check);
if (height >= 0 && height <= 20) return null;
check.Y = height + 1;
PPos.Y = height + 1;
return PPos;
int Y = 255; int Y = 255;
bool lastBlockAir = true; bool lastBlockAir = true;
@ -62,7 +68,6 @@ public class RTPFactory
for (Y = 255; Y > 1; Y--) for (Y = 255; Y > 1; Y--)
{ {
// Manually scan downwards // Manually scan downwards
var BA = ServerUtilities.API.World.BlockAccessor;
check.Y = Y; check.Y = Y;
var current = BA.GetBlock(check); var current = BA.GetBlock(check);
@ -115,6 +120,15 @@ public class RTPFactory
/// </summary> /// </summary>
internal static void HandleRTPChecking() internal static void HandleRTPChecking()
{ {
foreach (var chunk in ChunkChecks)
{
chunk.Wait--;
if (chunk.Wait <= 0)
{
ChunkChecks.Remove(chunk);
break;
}
}
// We want to now loop over the entire cache list. // We want to now loop over the entire cache list.
// We'll then generate a position to check // We'll then generate a position to check
// We'll also then check the loaded status of the chunk // We'll also then check the loaded status of the chunk
@ -131,6 +145,7 @@ public class RTPFactory
ServerUtilities.SendMessageTo(rtp.player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp-fail")); ServerUtilities.SendMessageTo(rtp.player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp-fail"));
// This check needs to be removed from the queue // This check needs to be removed from the queue
RTPCache.Remove(rtp); RTPCache.Remove(rtp);
HandleRTPChecking();
return; // We modified the list, so abort the loop. return; // We modified the list, so abort the loop.
} }
@ -141,7 +156,7 @@ public class RTPFactory
// Generate a new position // Generate a new position
var position = rtp.MakeNewPosition(worldSize); var position = rtp.MakeNewPosition(worldSize);
//ServerUtilities.SendMessageTo(rtp.player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp-progress")); ServerUtilities.SendMessageTo(rtp.player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp-progress", rtp.NumTriesRemaining));
// Generate a chunk load check object. // Generate a chunk load check object.
RTPChunk chunk = new RTPChunk(); RTPChunk chunk = new RTPChunk();
@ -176,6 +191,7 @@ public class RTPFactory
if (pos == null) if (pos == null)
{ {
// Let this get checked again // Let this get checked again
ServerUtilities.API.Logger.Notification("position null: resume search");
} }
else else
{ {
@ -184,6 +200,8 @@ public class RTPFactory
pos.Merge(data.player.Entity); pos.Merge(data.player.Entity);
ServerUtilities.SendMessageTo(data.player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp", GetDistance(new Vec2i(data.StartPosition.X, data.StartPosition.Z), new Vec2i(pos.X, pos.Z)))); ServerUtilities.SendMessageTo(data.player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp", GetDistance(new Vec2i(data.StartPosition.X, data.StartPosition.Z), new Vec2i(pos.X, pos.Z))));
ServerUtilities.API.Logger.Notification("position found");
} }
// Remove this check // Remove this check
@ -197,6 +215,7 @@ public class RTPChunk
public int ChunkZ; public int ChunkZ;
public int dim; public int dim;
public RTPData rtp; public RTPData rtp;
public int Wait = 5;
} }
public class RTPData public class RTPData
@ -218,7 +237,7 @@ public class RTPData
public RTPPosition MakeNewPosition(Vec3i mapSize) public RTPPosition MakeNewPosition(Vec3i mapSize)
{ {
NumTriesRemaining--; NumTriesRemaining--;
LastPosition = new RTPPosition((int)player.Entity.Pos.X, (int)player.Entity.Pos.Z, MaxDistance, player.Entity.Pos.Dimension, mapSize); LastPosition = new RTPPosition((int)player.Entity.Pos.X, (int)player.Entity.Pos.Z, MaxDistance, player.Entity.Pos.Dimension, mapSize, player);
return LastPosition; return LastPosition;
} }
@ -232,22 +251,26 @@ public class RTPPosition
public int dimension; public int dimension;
public RTPPosition(int x, int z, int maxDist, int dim, Vec3i mapSize) public RTPPosition(int x, int z, int maxDist, int dim, Vec3i mapSize, IServerPlayer player)
{ {
int worldx = mapSize.X / 2; int worldx = mapSize.X / 2;
int worldz = mapSize.Z / 2; int worldz = mapSize.Z / 2;
if (maxDist > worldx) maxDist = worldx / 2; if (maxDist > worldx)
{
ServerUtilities.SendMessageTo(player, Lang.Get($"{ServerUtilities.MOD_ID}:rtp-capped", maxDist, (worldx)));
maxDist = worldx / 2;
}
int minX = x - maxDist; int minX = x - maxDist;
int maxX = x + maxDist; int maxX = x + maxDist;
int minZ = z - maxDist; int minZ = z - maxDist;
int maxZ = z + maxDist; int maxZ = z + maxDist;
this.x = ServerUtilities.rng.Next(worldx - minX, worldx + maxX); this.x = ServerUtilities.rng.Next(minX, maxX);
this.y = 1; this.y = 1;
this.z = ServerUtilities.rng.Next(worldz - minZ, worldz + maxZ); this.z = ServerUtilities.rng.Next(minZ, maxZ);

View file

@ -34,7 +34,7 @@
"back": "You've been taken back to your last position", "back": "You've been taken back to your last position",
"rtp-search": "Searching for a random position...", "rtp-search": "Searching for a random position...",
"rtp-progress": "Still searching for a random position...", "rtp-progress": "Still searching for a random position... [{0}] tries remaining...",
"rtp": "You have been teleported [{0}] blocks away!", "rtp": "You have been teleported [{0}] blocks away!",
"rtp-found": "Found a valid landing position after {0} tries.", "rtp-found": "Found a valid landing position after {0} tries.",
"rtp-fail": "Giving up on RTP search. No valid position could be found. Try again later", "rtp-fail": "Giving up on RTP search. No valid position could be found. Try again later",

View file

@ -3,8 +3,8 @@
"modid": "ariasserverutils", "modid": "ariasserverutils",
"name": "Aria's Server Utilities", "name": "Aria's Server Utilities",
"authors": ["zontreck"], "authors": ["zontreck"],
"description": "A collection of server utilities\n\nBuild Date: 03-10-2025 @ 11:18 AM MST", "description": "A collection of server utilities\n\nBuild Date: 03-10-2025 @ 11:52 AM MST",
"version": "1.0.6-dev.16", "version": "1.0.6-dev.17",
"dependencies": { "dependencies": {
"game": "" "game": ""
} }