Skip to content

Commit b30164e

Browse files
Make /displace code much better.
1 parent 430a1f2 commit b30164e

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

fCraft/Commands/BuildingCommands.cs

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -771,59 +771,29 @@ private static void DisPlace(Player player, CommandReader cmd) {
771771
}
772772

773773
Block block;
774-
if (!cmd.NextBlock(player, false, out block)) return;
775-
int dis;
776-
if (!cmd.NextInt(out dis)) return;
774+
if (!cmd.NextBlock(player, false, out block)) return;
775+
int length;
776+
if (!cmd.NextInt(out length)) return;
777777

778-
dis = dis * 32;
779778
byte yaw = player.Position.R, pitch = player.Position.L;
780-
Vector3I Pos;
781-
782-
if (225 < pitch || pitch < 32) {
783-
if (225 <= yaw || yaw <= 32) {
784-
Pos = new Vector3I(player.Position.X/32, (player.Position.Y - dis)/32,
785-
(player.Position.Z - 32)/32);
786-
787-
} else if (33 <= yaw && yaw <= 96) {
788-
Pos = new Vector3I((player.Position.X + dis)/32, player.Position.Y/32,
789-
(player.Position.Z - 32) / 32);
790-
} else if (97 <= yaw && yaw <= 160) {
791-
Pos = new Vector3I(player.Position.X/32, (player.Position.Y + dis)/32,
792-
(player.Position.Z - 32) / 32);
793-
} else if (161 <= yaw && yaw <= 224) {
794-
Pos = new Vector3I((player.Position.X - dis)/32, player.Position.Y/32,
795-
(player.Position.Z - 32) / 32);
796-
} else {
797-
player.Message("Error occurred, please try again.");
798-
return;
799-
}
800-
} else {
801-
if (192 <= pitch && pitch <= 224) {
802-
Pos = new Vector3I(player.Position.X/32, player.Position.Y/32,
803-
(player.Position.Z - 32 + dis) / 32);
804-
} else if (33 <= pitch && pitch <= 65) {
805-
Pos = new Vector3I(player.Position.X/32, player.Position.Y/32,
806-
(player.Position.Z - 32 - dis) / 32);
807-
} else {
808-
player.Message("Error occurred, please try again.");
809-
return;
810-
}
811-
}
779+
Vector3I pos = player.Position.ToBlockCoordsRaw(); pos.Y--;
780+
Vector3I dir = Vector3I.FlatDirection(yaw, pitch);
781+
pos += dir * length;
812782

813-
Pos.X = Math.Min(player.WorldMap.Width - 1, Math.Max(0, Pos.X));
814-
Pos.Y = Math.Min(player.WorldMap.Length - 1, Math.Max(0, Pos.Y));
815-
Pos.Z = Math.Min(player.WorldMap.Height - 1, Math.Max(0, Pos.Z));
783+
pos.X = Math.Min(player.WorldMap.Width - 1, Math.Max(0, pos.X));
784+
pos.Y = Math.Min(player.WorldMap.Length - 1, Math.Max(0, pos.Y));
785+
pos.Z = Math.Min(player.WorldMap.Height - 1, Math.Max(0, pos.Z));
816786

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

822-
Player.RaisePlayerPlacedBlockEvent(player, player.WorldMap, Pos, player.WorldMap.GetBlock(Pos),
792+
Player.RaisePlayerPlacedBlockEvent(player, player.WorldMap, pos, player.WorldMap.GetBlock(pos),
823793
block, BlockChangeContext.Drawn);
824-
BlockUpdate blockUpdate = new BlockUpdate(null, Pos, block);
794+
BlockUpdate blockUpdate = new BlockUpdate(null, pos, block);
825795
player.World.Map.QueueUpdate(blockUpdate);
826-
player.Message("Block placed at {0} ({1} blocks away from you)", Pos, dis/32);
796+
player.Message("Block placed at {0} ({1} blocks away from you)", pos, length);
827797
}
828798

829799
#endregion

fCraft/System/Utils/Vector3I.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,26 @@ public static explicit operator Vector3F( Vector3I a ) {
303303
public Position ToPlayerCoords() {
304304
return new Position( X * 32 + 16, Y * 32 + 16, Z * 32 + 16 );
305305
}
306+
307+
public static Vector3I FlatDirection(byte yaw, byte pitch) {
308+
int dirX = 0, dirY = 0, dirZ = 0;
309+
if (pitch <= 32 || pitch >= 255) {
310+
if (yaw <= 32 || yaw >= 255) {
311+
dirY = -1;
312+
} else if (yaw >= 33 && yaw <= 96) {
313+
dirX = +1;
314+
} else if (yaw >= 97 && yaw <= 160) {
315+
dirY = +1;
316+
} else if (yaw >= 161 && yaw <= 224) {
317+
dirX = -1;
318+
}
319+
} else if (pitch >= 192 && pitch <= 224) {
320+
dirZ = +1;
321+
} else if (pitch >= 33 && pitch <= 65) {
322+
dirZ = -1;
323+
}
324+
return new Vector3I(dirX, dirY, dirZ);
325+
}
306326

307327
#endregion
308328
}

0 commit comments

Comments
 (0)