Now /click, /mark, and /place properly clamp positions outside the map.

This commit is contained in:
UnknownShadow200 2016-03-29 12:16:59 +11:00
parent 3e6b20b955
commit cf91d6d4df
7 changed files with 37 additions and 33 deletions

View File

@ -28,7 +28,8 @@ namespace MCGalaxy.Commands {
public override void Use(Player p, string message) {
if (p == null) { MessageInGameOnly(p); return; }
ushort[] click = p.lastClick;
Vec3U16 click = p.lastClick;
ushort value;
if (message.IndexOf(' ') != -1) {
string[] args = message.ToLower().Split(' ');
@ -36,28 +37,20 @@ namespace MCGalaxy.Commands {
for (int i = 0; i < 3; i++) {
if (args[i] == "x" || args[i] == "y" || args[i] == "z") {
click[i] = p.lastClick[i];
} else if (IsValid(p, i, args[i])) {
click[i] = ushort.Parse(args[i]);
// use the last value
} else if (ushort.TryParse(args[i], out value)) {
if (i == 0) click.X = value;
else if (i == 1) click.Y = value;
else click.Z = value;
} else {
Player.SendMessage(p, "\"" + args[i] + "\" was not valid"); return;
Player.SendMessage(p, "\"" + args[i] + "\" was not valid"); return;
}
}
}
p.lastCMD = "click";
p.ManualChange(click[0], click[1], click[2], 0, Block.rock);
Player.SendMessage(p, "Clicked &b(" + click[0] + ", " + click[1] + ", " + click[2] + ")");
}
bool IsValid(Player p, int axis, string message) {
ushort value;
if (!ushort.TryParse(message, out value)) return false;
if (value >= p.level.Width && axis == 0) return false;
else if (value >= p.level.Height && axis == 1) return false;
else if (value >= p.level.Length && axis == 2) return false;
return true;
click = Vec3U16.ClampToBounds(click.X, click.Y, click.Z, p.level);
p.ManualChange(click.X, click.Y, click.Z, 0, Block.rock);
Player.SendMessage(p, "Clicked &b(" + click.X + ", " + click.Y + ", " + click.Z + ")");
}
public override void Help(Player p) {

View File

@ -26,8 +26,7 @@ namespace MCGalaxy.Commands
public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
public CmdMark() { }
public override void Use(Player p, string message)
{
public override void Use(Player p, string message) {
if (p == null) { MessageInGameOnly(p); return; }
int x = (ushort)(p.pos[0] / 32);
@ -35,8 +34,8 @@ namespace MCGalaxy.Commands
int z = (ushort)(p.pos[2] / 32);
Command.all.Find("click").Use(p, x + " " + y + " " + z);
}
public override void Help(Player p)
{
public override void Help(Player p) {
Player.SendMessage(p, "/mark - Clicks where you are standing.");
Player.SendMessage(p, "Use this to place a marker at your position when making a selection or cuboid.");
}

View File

@ -58,10 +58,9 @@ namespace MCGalaxy.Commands {
if (type == Block.Zero) return;
if (!Block.canPlace(p, type)) { Player.SendMessage(p, "Cannot place that block type."); return; }
if (y >= p.level.Height) y = (ushort)(p.level.Height - 1);
p.level.UpdateBlock(p, x, y, z, type, extType);
Player.SendMessage(p, "A block was placed at (" + x + ", " + y + ", " + z + ").");
Vec3U16 P = Vec3U16.ClampToBounds(x, y, z, p.level);
p.level.UpdateBlock(p, P.X, P.Y, P.Z, type, extType);
Player.SendMessage(p, "A block was placed at (" + P.X + ", " + P.Y + ", " + P.Z + ").");
}
public override void Help(Player p) {

View File

@ -426,7 +426,6 @@
<Compile Include="Drawing\DrawOps\TriangleDrawOp.cs" />
<Compile Include="Drawing\DrawOps\UndoDrawOp.cs" />
<Compile Include="Drawing\DrawOps\WriteDrawOp.cs" />
<Compile Include="Drawing\Vectors.cs" />
<Compile Include="Economy\Awards.cs" />
<Compile Include="Economy\ZombieItems.cs" />
<Compile Include="Economy\Economy.cs" />
@ -615,6 +614,7 @@
<Compile Include="API\WebServer.cs" />
<Compile Include="util\FastList.cs" />
<Compile Include="util\SparseBitSet.cs" />
<Compile Include="util\Vectors.cs" />
<Compile Include="util\VolatileArray.cs" />
<EmbeddedResource Include="GUI\Eco\EcoLevelWindow.resx">
<DependentUpon>EcoLevelWindow.cs</DependentUpon>

View File

@ -71,7 +71,7 @@ namespace MCGalaxy {
bP.index = level.PosToInt(x, y, z);
bP.SetData(type, extType, false);
lastClick[0] = x; lastClick[1] = y; lastClick[2] = z;
lastClick.X = x; lastClick.Y = y; lastClick.Z = z;
if ( Blockchange != null ) {
if ( Blockchange.Method.ToString().IndexOf("AboutBlockchange") == -1 && !level.IsMuseum ) {
bP.flags |= 1;

View File

@ -265,7 +265,7 @@ namespace MCGalaxy {
public Level level = Server.mainLevel;
public bool Loading = true; //True if player is loading a map.
internal bool usingGoto = false;
public ushort[] lastClick = new ushort[] { 0, 0, 0 };
public Vec3U16 lastClick = Vec3U16.Zero;
public ushort[] beforeTeleportPos = new ushort[] { 0, 0, 0 };
public string beforeTeleportMap = "";
public ushort[] pos = new ushort[] { 0, 0, 0 };

View File

@ -17,11 +17,12 @@
*/
using System;
namespace MCGalaxy.Drawing {
namespace MCGalaxy {
public struct Vec3U16 {
public ushort X, Y, Z;
public static Vec3U16 Zero = new Vec3U16(0);
public static Vec3U16 MinVal = new Vec3U16(ushort.MinValue);
public static Vec3U16 MaxVal = new Vec3U16(ushort.MaxValue);
@ -75,6 +76,18 @@ namespace MCGalaxy.Drawing {
return new Vec3U16(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Min(a.Z, b.Z));
}
public static Vec3U16 ClampToBounds(ushort x, ushort y, ushort z, Level lvl) {
Vec3U16 P = new Vec3U16(x, y, z);
if (P.X >= 32768) P.X = 0;
if (P.Y >= 32768) P.Y = 0;
if (P.Z >= 32768) P.Z = 0;
if (P.X >= lvl.Width) P.X = (ushort)(lvl.Width - 1);
if (P.Y >= lvl.Height) P.Y = (ushort)(lvl.Height - 1);
if (P.Z >= lvl.Length) P.Z = (ushort)(lvl.Length - 1);
return P;
}
public static bool operator == (Vec3U16 a, Vec3U16 b) {
return a.X == b.X && a.Y == b.Y && a.Z == b.Z;
}
@ -95,8 +108,8 @@ namespace MCGalaxy.Drawing {
return X + "," + Y + "," + Z;
}
}
public struct Vec3S16 {
public struct Vec3S16 {
public short X, Y, Z;