Skip to content
Permalink
Browse files

Make /displace code much better.

  • Loading branch information...
UnknownShadow200 committed Dec 1, 2017
1 parent 430a1f2 commit b30164ecb4e03281563f49e4657f3e8d6ec46314
Showing with 33 additions and 43 deletions.
  1. +13 −43 fCraft/Commands/BuildingCommands.cs
  2. +20 −0 fCraft/System/Utils/Vector3I.cs
@@ -771,59 +771,29 @@ private static void WallsHandler(Player player, CommandReader cmd)
}

Block block;
if (!cmd.NextBlock(player, false, out block)) return;
int dis;
if (!cmd.NextInt(out dis)) return;
if (!cmd.NextBlock(player, false, out block)) return;
int length;
if (!cmd.NextInt(out length)) return;

dis = dis * 32;
byte yaw = player.Position.R, pitch = player.Position.L;
Vector3I Pos;

if (225 < pitch || pitch < 32) {
if (225 <= yaw || yaw <= 32) {
Pos = new Vector3I(player.Position.X/32, (player.Position.Y - dis)/32,
(player.Position.Z - 32)/32);

} else if (33 <= yaw && yaw <= 96) {
Pos = new Vector3I((player.Position.X + dis)/32, player.Position.Y/32,
(player.Position.Z - 32) / 32);
} else if (97 <= yaw && yaw <= 160) {
Pos = new Vector3I(player.Position.X/32, (player.Position.Y + dis)/32,
(player.Position.Z - 32) / 32);
} else if (161 <= yaw && yaw <= 224) {
Pos = new Vector3I((player.Position.X - dis)/32, player.Position.Y/32,
(player.Position.Z - 32) / 32);
} else {
player.Message("Error occurred, please try again.");
return;
}
} else {
if (192 <= pitch && pitch <= 224) {
Pos = new Vector3I(player.Position.X/32, player.Position.Y/32,
(player.Position.Z - 32 + dis) / 32);
} else if (33 <= pitch && pitch <= 65) {
Pos = new Vector3I(player.Position.X/32, player.Position.Y/32,
(player.Position.Z - 32 - dis) / 32);
} else {
player.Message("Error occurred, please try again.");
return;
}
}
Vector3I pos = player.Position.ToBlockCoordsRaw(); pos.Y--;
Vector3I dir = Vector3I.FlatDirection(yaw, pitch);
pos += dir * length;

Pos.X = Math.Min(player.WorldMap.Width - 1, Math.Max(0, Pos.X));
Pos.Y = Math.Min(player.WorldMap.Length - 1, Math.Max(0, Pos.Y));
Pos.Z = Math.Min(player.WorldMap.Height - 1, Math.Max(0, Pos.Z));
pos.X = Math.Min(player.WorldMap.Width - 1, Math.Max(0, pos.X));
pos.Y = Math.Min(player.WorldMap.Length - 1, Math.Max(0, pos.Y));
pos.Z = Math.Min(player.WorldMap.Height - 1, Math.Max(0, pos.Z));

if (player.CanPlace(player.World.Map, Pos, block, BlockChangeContext.Drawn) != CanPlaceResult.Allowed) {
if (player.CanPlace(player.World.Map, pos, block, BlockChangeContext.Drawn) != CanPlaceResult.Allowed) {
player.Message("&WYou are not allowed to build here");
return;
}

Player.RaisePlayerPlacedBlockEvent(player, player.WorldMap, Pos, player.WorldMap.GetBlock(Pos),
Player.RaisePlayerPlacedBlockEvent(player, player.WorldMap, pos, player.WorldMap.GetBlock(pos),
block, BlockChangeContext.Drawn);
BlockUpdate blockUpdate = new BlockUpdate(null, Pos, block);
BlockUpdate blockUpdate = new BlockUpdate(null, pos, block);
player.World.Map.QueueUpdate(blockUpdate);
player.Message("Block placed at {0} ({1} blocks away from you)", Pos, dis/32);
player.Message("Block placed at {0} ({1} blocks away from you)", pos, length);
}

#endregion
@@ -303,6 +303,26 @@ public struct Vector3I : IEquatable<Vector3I>, IComparable<Vector3I>, IComparabl
public Position ToPlayerCoords() {
return new Position( X * 32 + 16, Y * 32 + 16, Z * 32 + 16 );
}

public static Vector3I FlatDirection(byte yaw, byte pitch) {
int dirX = 0, dirY = 0, dirZ = 0;
if (pitch <= 32 || pitch >= 255) {
if (yaw <= 32 || yaw >= 255) {
dirY = -1;
} else if (yaw >= 33 && yaw <= 96) {
dirX = +1;
} else if (yaw >= 97 && yaw <= 160) {
dirY = +1;
} else if (yaw >= 161 && yaw <= 224) {
dirX = -1;
}
} else if (pitch >= 192 && pitch <= 224) {
dirZ = +1;
} else if (pitch >= 33 && pitch <= 65) {
dirZ = -1;
}
return new Vector3I(dirX, dirY, dirZ);
}

#endregion
}

0 comments on commit b30164e

Please sign in to comment.
You can’t perform that action at this time.