Change method of finding a safe position

This commit is contained in:
zontreck 2025-03-10 10:36:41 -07:00
parent 74d85c57e1
commit 6598eb44da
3 changed files with 15 additions and 49 deletions

View file

@ -49,53 +49,18 @@ public class RTPFactory
/// <returns>A safe position if able to be found</returns>
public static PlayerPosition GetSafePosition(RTPData data, RTPPosition position)
{
Random rng = ServerUtilities.rng;
IServerPlayer isp = data.player;
EntityPos vPos = isp.Entity.Pos;
BlockPos bPos = new BlockPos(isp.Entity.Pos.Dimension);
IServerWorldAccessor iswa = isp.Entity.World as IServerWorldAccessor;
PlayerPosition PPos = PlayerPosition.from(data.player.Entity);
BlockPos check = new(
x: position.x,
y: 1,
z: position.z,
dim: position.dimension
);
int tries = 10000;
PlayerPosition PPos = PlayerPosition.from(isp.Entity);
//PlayerPosition original = PlayerPosition.from(isp.Entity);
int height = ServerUtilities.API.World.BlockAccessor.GetTerrainMapheightAt(check);
// Generate random X and Z within max RTP distance
bPos.X = position.x;
bPos.Z = position.z;
bPos.Y = 255;
Block lastAboveLast = null;
Block lastBlock = null;
Block curBlock;
// Scan downwards to find a valid landing spot
for (int i = 255; i > 0; i--)
{
bPos.Y = i;
curBlock = iswa.BlockAccessor.GetBlock(bPos);
if (curBlock.MatterState == EnumMatterState.Solid)
{
if (lastBlock != null && lastBlock.BlockMaterial == EnumBlockMaterial.Air &&
lastAboveLast != null && lastAboveLast.BlockMaterial == EnumBlockMaterial.Air)
{
// Found a valid spot: curBlock is solid, lastBlock & lastAboveLast are gas (air)
PPos.X = bPos.X;
PPos.Y = bPos.Y + 1;
PPos.Z = bPos.Z;
ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:rtp-found", tries));
return PPos;
}
}
lastAboveLast = lastBlock;
lastBlock = curBlock;
}
return null; // Return null if no valid position is found after retries
PPos.Y = height + 1;
return PPos;
}
/// <summary>
@ -179,13 +144,14 @@ public class RTPFactory
if (pos == null)
{
// Let this get checked again
RTPCache.Remove(data);
}
else
{
// Found! Perform teleport and remove the RTP Check
RTPCache.Remove(data);
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))));
}
// Remove this check

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 searchinf for a random position...",
"rtp-progress": "Still searching for a random position...",
"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 @ 10:22 AM MST",
"version": "1.0.6-dev.12",
"description": "A collection of server utilities\n\nBuild Date: 03-10-2025 @ 10:36 AM MST",
"version": "1.0.6-dev.13",
"dependencies": {
"game": ""
}