Skip to content

Commit

Permalink
Fix /top and /setspawn random not moving you to right height
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Sep 1, 2018
1 parent 6d31b59 commit f5f946e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
55 changes: 25 additions & 30 deletions fCraft/Commands/ModerationCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,15 +1316,18 @@ static void UnfreezeHandler(Player player, CommandReader cmd)
return;
}

Vector3I P = player.World.map.HighestFreeSpace(zone.Bounds.XCentre,
zone.Bounds.YCentre, zone.Bounds.ZCentre);
Position zPos = new Position(P.X * 32 + 16, P.Y * 32 + 16, P.Z * 32 + Player.CharacterHeight);
int x = zone.Bounds.XCentre, y = zone.Bounds.YCentre;
int z = player.WorldMap.HighestFreeSpace(x, y, zone.Bounds.ZCentre);

if (player.World != null) {
player.LastWorld = player.World;
player.LastPosition = player.Position;
}

player.TeleportTo((zPos));
Position pos = new Position(x, y, z);
pos.R = player.Position.R;
pos.L = player.Position.L;
player.TeleportTo(pos);
player.Message("Teleporting you to zone " + zone.ClassyName);
}

Expand All @@ -1333,20 +1336,18 @@ static void UnfreezeHandler(Player player, CommandReader cmd)
Random rand = new Random();
int x = rand.Next(0, player.WorldMap.Width);
int y = rand.Next(0, player.WorldMap.Length);
int z = (player.Position.Z - Player.CharacterHeight) / 32;
Vector3I P = player.WorldMap.HighestFreeSpace(x, y, z);
int z = player.Position.BlockFeetZ;
z = player.WorldMap.HighestFreeSpace(x, y, z);

if (player.World != null) {
player.LastWorld = player.World;
player.LastPosition = player.Position;
}
player.TeleportTo(new Position {
X = (P.X * 32 + 16),
Y = (P.Y * 32 + 16),
Z = (P.Z * 32 + Player.CharacterHeight),
R = player.Position.R,
L = player.Position.L
});

Position pos = new Position(x, y, z);
pos.R = player.Position.R;
pos.L = player.Position.L;
player.TeleportTo(pos);
player.Message("Teleported to: ({0}, {1}, {2})", x, y, z);
}

Expand Down Expand Up @@ -1376,13 +1377,11 @@ static void UnfreezeHandler(Player player, CommandReader cmd)
player.LastWorld = player.World;
player.LastPosition = player.Position;
}
player.TeleportTo(new Position {
X = (x*32 + 16),
Y = (y*32 + 16),
Z = (z*32 + Player.CharacterHeight),
R = (byte) rot,
L = (byte) lot
});

Position pos = new Position(x, y, z);
pos.R = (byte)rot;
pos.L = (byte)lot;
player.TeleportTo(pos);
} else {
CdTeleport.PrintUsage(player);
}
Expand Down Expand Up @@ -1552,16 +1551,12 @@ static void UnfreezeHandler(Player player, CommandReader cmd)
static void TopHandler(Player player, CommandReader cmd) {
int x = player.Position.BlockX;
int y = player.Position.BlockY;
int z = (player.Position.Z - Player.CharacterHeight) / 32;
Vector3I P = player.World.map.HighestFreeSpace(x, y, z);

player.TeleportTo(new Position {
X = (P.X * 32 + 16),
Y = (P.Y * 32 + 16),
Z = (P.Z * 32 + Player.CharacterHeight),
R = player.Position.R,
L = player.Position.L
});
int z = player.WorldMap.HighestFreeZ(x, y);

Position pos = new Position(x, y, z);
pos.R = player.Position.R;
pos.L = player.Position.L;
player.TeleportTo(pos);
player.Message("Teleported to top");
}

Expand Down
4 changes: 4 additions & 0 deletions fCraft/Player/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public struct Position : IEquatable<Position> {
return X + "_" + Y + "_" + Z + "_" + R + "_" + L;
}

public static Position FromFeet(int x, int y, int z) {
return new Position(x * 32 + 16, y * 32 + 16, z * 32 + Player.CharacterHeight);
}

public static Position FromString(string text) {
Position pos = new Position();

Expand Down
18 changes: 11 additions & 7 deletions fCraft/World/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,14 @@ static void DefineFallbackBlocks()
public Position getSpawnIfRandom() {
if (spawn == Position.RandomSpawn) {
Random rnd = new Random();
Vector3I P = HighestFreeSpace(rnd.Next(Width), rnd.Next(Length), Height);
return new Position(P.X * 32, P.Y * 32, P.Z * 32, 0, 0);
int x = rnd.Next(Width), y = rnd.Next(Length);
return Position.FromFeet(x, y, HighestFreeZ(x, y));
}
return spawn;
}

public Vector3I HighestFreeSpace(int x, int y, int z) {
while (z > 0 && GetBlock(x, y, z) != Block.Air) {
z--;
}
public int HighestFreeSpace(int x, int y, int z) {
while (z > 0 && GetBlock(x, y, z) != Block.Air) z--;

while (z < Height) {
Block bFeet = GetBlock(x, y, z), bHead = GetBlock(x, y, z + 1);
Expand All @@ -130,7 +128,13 @@ static void DefineFallbackBlocks()
if (freeFeet && freeHead) break;
z++;
}
return new Vector3I(x, y, z);
return z;
}

public int HighestFreeZ(int x, int y) {
int z = Bounds.ZMax;
while (z > 0 && GetBlock(x, y, z) == Block.Air) z--;
return z + 1;
}

/// <summary> Resets spawn to the default location (top center of the map). </summary>
Expand Down
2 changes: 1 addition & 1 deletion fCraft/Zone/SpecialZone.Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static partial class SpecialZone {
}

static bool HandleCheckpoint(Player p, Zone zone, ref bool deniedZone, Position newPos) {
Position centre = new Position(zone.Bounds.XCentre * 32 + 16, zone.Bounds.YCentre * 32 + 16, zone.Bounds.ZCentre * 32 + Player.CharacterHeight);
Position centre = Position.FromFeet(zone.Bounds.XCentre, zone.Bounds.YCentre, zone.Bounds.ZCentre);
if (p.CheckPoint == centre) return false;
if (zone.Bounds.Contains(newPos.BlockX, newPos.BlockY, (newPos.Z - 32) / 32)) {
SendZoneMessage(p, "&aCheckPoint &Sreached! This is now your respawn point.");
Expand Down

0 comments on commit f5f946e

Please sign in to comment.