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();
BlockPos check = data.LastPosition.GetBlockPos();
var BA = ServerUtilities.API.World.BlockAccessor;
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;
bool lastBlockAir = true;
@ -62,7 +68,6 @@ public class RTPFactory
for (Y = 255; Y > 1; Y--)
{
// Manually scan downwards
var BA = ServerUtilities.API.World.BlockAccessor;
check.Y = Y;
var current = BA.GetBlock(check);
@ -115,6 +120,15 @@ public class RTPFactory
/// </summary>
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'll then generate a position to check
// 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"));
// This check needs to be removed from the queue
RTPCache.Remove(rtp);
HandleRTPChecking();
return; // We modified the list, so abort the loop.
}
@ -141,7 +156,7 @@ public class RTPFactory
// Generate a new position
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.
RTPChunk chunk = new RTPChunk();
@ -176,6 +191,7 @@ public class RTPFactory
if (pos == null)
{
// Let this get checked again
ServerUtilities.API.Logger.Notification("position null: resume search");
}
else
{
@ -184,6 +200,8 @@ public class RTPFactory
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.API.Logger.Notification("position found");
}
// Remove this check
@ -197,6 +215,7 @@ public class RTPChunk
public int ChunkZ;
public int dim;
public RTPData rtp;
public int Wait = 5;
}
public class RTPData
@ -218,7 +237,7 @@ public class RTPData
public RTPPosition MakeNewPosition(Vec3i mapSize)
{
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;
}
@ -232,22 +251,26 @@ public class RTPPosition
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 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 maxX = x + maxDist;
int minZ = 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.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",
"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-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",

View file

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