Skip to content

Commit f5f946e

Browse files
Fix /top and /setspawn random not moving you to right height
1 parent 6d31b59 commit f5f946e

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

fCraft/Commands/ModerationCommands.cs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,15 +1316,18 @@ static void TeleportToZone(Player player, CommandReader cmd) {
13161316
return;
13171317
}
13181318

1319-
Vector3I P = player.World.map.HighestFreeSpace(zone.Bounds.XCentre,
1320-
zone.Bounds.YCentre, zone.Bounds.ZCentre);
1321-
Position zPos = new Position(P.X * 32 + 16, P.Y * 32 + 16, P.Z * 32 + Player.CharacterHeight);
1319+
int x = zone.Bounds.XCentre, y = zone.Bounds.YCentre;
1320+
int z = player.WorldMap.HighestFreeSpace(x, y, zone.Bounds.ZCentre);
1321+
13221322
if (player.World != null) {
13231323
player.LastWorld = player.World;
13241324
player.LastPosition = player.Position;
13251325
}
13261326

1327-
player.TeleportTo((zPos));
1327+
Position pos = new Position(x, y, z);
1328+
pos.R = player.Position.R;
1329+
pos.L = player.Position.L;
1330+
player.TeleportTo(pos);
13281331
player.Message("Teleporting you to zone " + zone.ClassyName);
13291332
}
13301333

@@ -1333,20 +1336,18 @@ static void TeleportToRandom(Player player, CommandReader cmd) {
13331336
Random rand = new Random();
13341337
int x = rand.Next(0, player.WorldMap.Width);
13351338
int y = rand.Next(0, player.WorldMap.Length);
1336-
int z = (player.Position.Z - Player.CharacterHeight) / 32;
1337-
Vector3I P = player.WorldMap.HighestFreeSpace(x, y, z);
1339+
int z = player.Position.BlockFeetZ;
1340+
z = player.WorldMap.HighestFreeSpace(x, y, z);
13381341

13391342
if (player.World != null) {
13401343
player.LastWorld = player.World;
13411344
player.LastPosition = player.Position;
13421345
}
1343-
player.TeleportTo(new Position {
1344-
X = (P.X * 32 + 16),
1345-
Y = (P.Y * 32 + 16),
1346-
Z = (P.Z * 32 + Player.CharacterHeight),
1347-
R = player.Position.R,
1348-
L = player.Position.L
1349-
});
1346+
1347+
Position pos = new Position(x, y, z);
1348+
pos.R = player.Position.R;
1349+
pos.L = player.Position.L;
1350+
player.TeleportTo(pos);
13501351
player.Message("Teleported to: ({0}, {1}, {2})", x, y, z);
13511352
}
13521353

@@ -1376,13 +1377,11 @@ static void TeleportToCoords(Player player, CommandReader cmd) {
13761377
player.LastWorld = player.World;
13771378
player.LastPosition = player.Position;
13781379
}
1379-
player.TeleportTo(new Position {
1380-
X = (x*32 + 16),
1381-
Y = (y*32 + 16),
1382-
Z = (z*32 + Player.CharacterHeight),
1383-
R = (byte) rot,
1384-
L = (byte) lot
1385-
});
1380+
1381+
Position pos = new Position(x, y, z);
1382+
pos.R = (byte)rot;
1383+
pos.L = (byte)lot;
1384+
player.TeleportTo(pos);
13861385
} else {
13871386
CdTeleport.PrintUsage(player);
13881387
}
@@ -1552,16 +1551,12 @@ static void TeleportPHandler(Player player, CommandReader cmd) {
15521551
static void TopHandler(Player player, CommandReader cmd) {
15531552
int x = player.Position.BlockX;
15541553
int y = player.Position.BlockY;
1555-
int z = (player.Position.Z - Player.CharacterHeight) / 32;
1556-
Vector3I P = player.World.map.HighestFreeSpace(x, y, z);
1557-
1558-
player.TeleportTo(new Position {
1559-
X = (P.X * 32 + 16),
1560-
Y = (P.Y * 32 + 16),
1561-
Z = (P.Z * 32 + Player.CharacterHeight),
1562-
R = player.Position.R,
1563-
L = player.Position.L
1564-
});
1554+
int z = player.WorldMap.HighestFreeZ(x, y);
1555+
1556+
Position pos = new Position(x, y, z);
1557+
pos.R = player.Position.R;
1558+
pos.L = player.Position.L;
1559+
player.TeleportTo(pos);
15651560
player.Message("Teleported to top");
15661561
}
15671562

fCraft/Player/Position.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public string ToPlainString() {
9898
return X + "_" + Y + "_" + Z + "_" + R + "_" + L;
9999
}
100100

101+
public static Position FromFeet(int x, int y, int z) {
102+
return new Position(x * 32 + 16, y * 32 + 16, z * 32 + Player.CharacterHeight);
103+
}
104+
101105
public static Position FromString(string text) {
102106
Position pos = new Position();
103107

fCraft/World/Map.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,14 @@ public Position Spawn {
111111
public Position getSpawnIfRandom() {
112112
if (spawn == Position.RandomSpawn) {
113113
Random rnd = new Random();
114-
Vector3I P = HighestFreeSpace(rnd.Next(Width), rnd.Next(Length), Height);
115-
return new Position(P.X * 32, P.Y * 32, P.Z * 32, 0, 0);
114+
int x = rnd.Next(Width), y = rnd.Next(Length);
115+
return Position.FromFeet(x, y, HighestFreeZ(x, y));
116116
}
117117
return spawn;
118118
}
119119

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

125123
while (z < Height) {
126124
Block bFeet = GetBlock(x, y, z), bHead = GetBlock(x, y, z + 1);
@@ -130,7 +128,13 @@ public Vector3I HighestFreeSpace(int x, int y, int z) {
130128
if (freeFeet && freeHead) break;
131129
z++;
132130
}
133-
return new Vector3I(x, y, z);
131+
return z;
132+
}
133+
134+
public int HighestFreeZ(int x, int y) {
135+
int z = Bounds.ZMax;
136+
while (z > 0 && GetBlock(x, y, z) == Block.Air) z--;
137+
return z + 1;
134138
}
135139

136140
/// <summary> Resets spawn to the default location (top center of the map). </summary>

fCraft/Zone/SpecialZone.Move.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static bool HandleRespawn(Player p, Zone zone, ref bool deniedZone, Position new
6666
}
6767

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

0 commit comments

Comments
 (0)