Simplify /tree code somewhat

This commit is contained in:
UnknownShadow200 2018-05-30 15:28:31 +10:00
parent 7a23e04531
commit 350e1619e1
18 changed files with 87 additions and 137 deletions

View File

@ -21,18 +21,15 @@ using MCGalaxy.Drawing.Ops;
namespace MCGalaxy.Commands.Building {
public sealed class CmdBezier : DrawCmd {
public override string name { get { return "Bezier"; } }
protected override int MarksCount { get { return 3; } }
protected override string SelectionType { get { return "points"; } }
protected override string PlaceMessage { get { return "Place or break two blocks to determine the endpoints, then another for the control point"; } }
public override CommandAlias[] Aliases {
get { return new CommandAlias[] { new CommandAlias("Curve") }; }
}
protected override DrawOp GetDrawOp(DrawArgs dArgs) {
return new BezierDrawOp();
}
protected override int MarksCount { get { return 3; } }
protected override string SelectionType { get { return "points"; } }
protected override string PlaceMessage { get { return "Place or break two blocks to determine the endpoints, then another for the control point"; } }
protected override DrawOp GetDrawOp(DrawArgs dArgs) { return new BezierDrawOp(); }
public override void Help(Player p) {
Player.Message(p, "%T/Bezier <brush args>");

View File

@ -97,7 +97,7 @@ namespace MCGalaxy.Commands.Building {
void DoCopyMark(Player p, Vec3S32[] m, int i, object state, BlockID block) {
if (i == 2) {
CopyState copy = p.CopySlots[p.CurrentCopySlot];
CopyState copy = p.CurrentCopy;
copy.Offset.X = copy.OriginX - m[i].X;
copy.Offset.Y = copy.OriginY - m[i].Y;
copy.Offset.Z = copy.OriginZ - m[i].Z;
@ -139,7 +139,8 @@ namespace MCGalaxy.Commands.Building {
}
cState.CopySource = "level " + p.level.name;
p.SetCurrentCopy(cState);
p.CurrentCopy = cState;
if (cArgs.cut) {
DrawOp op = new CuboidDrawOp();
op.Flags = BlockDBFlags.Cut;
@ -172,11 +173,16 @@ namespace MCGalaxy.Commands.Building {
return;
}
CopyState cState = p.CurrentCopy;
if (cState == null) {
Player.Message(p, "You haven't copied anything yet"); return;
}
string path = "extra/savecopy/" + p.name + "/" + file + ".cpb";
using (FileStream fs = File.Create(path))
using(GZipStream gs = new GZipStream(fs, CompressionMode.Compress))
{
p.CopySlots[p.CurrentCopySlot].SaveTo(gs);
cState.SaveTo(gs);
}
Player.Message(p, "Saved copy as " + file);
}
@ -195,8 +201,9 @@ namespace MCGalaxy.Commands.Building {
} else {
state.LoadFromOld(gs, fs);
}
state.CopySource = "file " + file;
p.SetCurrentCopy(state);
p.CurrentCopy = state;
}
Player.Message(p, "Loaded copy from " + file);
}

View File

@ -40,11 +40,11 @@ namespace MCGalaxy.Commands.Building {
int i = 0;
if (!CommandParser.GetInt(p, message, "Slot number", ref i, 1, p.group.CopySlots)) return;
i--; p.CurrentCopySlot = i;
if (i >= p.CopySlots.Count || p.CopySlots[i] == null) {
Player.Message(p, "Selected copy slot {0} (unused)", i + 1);
p.CurrentCopySlot = i - 1;
if (p.CurrentCopy == null) {
Player.Message(p, "Selected copy slot {0} (unused)", i);
} else {
Player.Message(p, "Selected copy slot {0}: {1}", i + 1, p.CopySlots[i].Summary);
Player.Message(p, "Selected copy slot {0}: {1}", i, p.CurrentCopy.Summary);
}
}
}

View File

@ -63,21 +63,21 @@ namespace MCGalaxy.Commands.Building {
AdvDrawMeta meta = new AdvDrawMeta();
bool success = false;
string[] parts = dArgs.Message.SplitSpaces();
string[] args = dArgs.Message.SplitSpaces();
Player p = dArgs.Player;
if (op.UsesHeight) {
if (parts.Length < 3) {
Player.Message(p, "You need to provide the radius and the height for the {0}.", parts[0]);
if (args.Length < 3) {
Player.Message(p, "You need to provide the radius and the height for the {0}.", args[0]);
} else {
success = CommandParser.GetInt(p, parts[1], "radius", ref meta.radius, 0, 2000)
&& CommandParser.GetInt(p, parts[2], "height", ref meta.height, 0, 2000);
success = CommandParser.GetInt(p, args[1], "radius", ref meta.radius, 0, 2000)
&& CommandParser.GetInt(p, args[2], "height", ref meta.height, 0, 2000);
}
} else {
if (parts.Length < 2) {
Player.Message(p, "You need to provide the radius for the {0}.", parts[0]);
if (args.Length < 2) {
Player.Message(p, "You need to provide the radius for the {0}.", args[0]);
} else {
success = CommandParser.GetInt(p, parts[1], "radius", ref meta.radius, 0, 2000);
success = CommandParser.GetInt(p, args[1], "radius", ref meta.radius, 0, 2000);
}
}

View File

@ -36,14 +36,11 @@ namespace MCGalaxy.Commands.Building {
protected override string PlaceMessage { get { return "Place or break a block to mark the area you wish to fill."; } }
protected override DrawMode GetMode(string[] parts) {
if (parts[parts.Length - 1].CaselessEq("confirm")) {
string prev = parts.Length >= 2 ? parts[parts.Length - 2] : "";
return ParseFillMode(prev);
string msg = parts[parts.Length - 1];
if (msg.CaselessEq("confirm")) {
msg = parts.Length >= 2 ? parts[parts.Length - 2] : "";
}
return ParseFillMode(parts[parts.Length - 1]);
}
DrawMode ParseFillMode(string msg) {
if (msg == "normal") return DrawMode.solid;
if (msg == "up") return DrawMode.up;
if (msg == "down") return DrawMode.down;

View File

@ -34,7 +34,7 @@ namespace MCGalaxy.Commands.Building {
return op;
}
protected override void GetBrush(DrawArgs dArgs) { dArgs.BrushName = "normal"; }
protected override void GetBrush(DrawArgs dArgs) { dArgs.BrushName = "Normal"; }
public override void Help(Player p) {
Player.Message(p, "%T/Hollow");

View File

@ -35,7 +35,7 @@ namespace MCGalaxy.Commands.Building {
}
public override void Use(Player p, string message) {
if (p.CurrentCopySlot >= p.CopySlots.Count || p.CopySlots[p.CurrentCopySlot] == null) {
if (p.CurrentCopy == null) {
Player.Message(p, "You haven't copied anything yet"); return;
}
@ -48,7 +48,7 @@ namespace MCGalaxy.Commands.Building {
}
bool DoPaste(Player p, Vec3S32[] m, object state, BlockID block) {
CopyState cState = p.CopySlots[p.CurrentCopySlot];
CopyState cState = p.CurrentCopy;
m[0] += cState.Offset;
PasteDrawOp op = new PasteDrawOp();

View File

@ -24,7 +24,7 @@ namespace MCGalaxy.Commands.Building {
public override string name { get { return "Rainbow"; } }
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
protected override void GetBrush(DrawArgs dArgs) { dArgs.BrushName = "normal"; }
protected override void GetBrush(DrawArgs dArgs) { dArgs.BrushName = "Normal"; }
protected override DrawOp GetDrawOp(DrawArgs dArgs) {
string args = dArgs.Message;

View File

@ -30,9 +30,7 @@ namespace MCGalaxy.Commands.Building {
new CommandAlias("sph", null, "hollow"), new CommandAlias("Circle", null, "circle" ),
new CommandAlias("CircleH", null, "hollowcircle") }; }
}
protected override string PlaceMessage {
get { return "Place a block for the centre, then another for the radius."; }
}
protected override string PlaceMessage { get { return "Place a block for the centre, then another for the radius."; } }
protected override DrawMode GetMode(string[] parts) {
string msg = parts[parts.Length - 1];

View File

@ -31,11 +31,11 @@ namespace MCGalaxy.Commands.Building {
public override void Use(Player p, string message) {
if (message.Length == 0) message = "y";
if (p.CurrentCopySlot >= p.CopySlots.Count || p.CopySlots[p.CurrentCopySlot] == null) {
if (p.CurrentCopy == null) {
Player.Message(p, "You haven't copied anything yet"); return;
}
CopyState cState = p.CopySlots[p.CurrentCopySlot];
CopyState cState = p.CurrentCopy;
string opt = message.ToLower();
BlockDefinition[] defs = p.level.CustomBlockDefs;
@ -67,8 +67,8 @@ namespace MCGalaxy.Commands.Building {
}
newState.CopySource = cState.CopySource;
newState.CopyTime = cState.CopyTime;
p.SetCurrentCopy(newState);
newState.CopyTime = cState.CopyTime;
p.CurrentCopy = newState;
Player.Message(p, "Rotated copy {0} degrees around the {1} axis", angle, axis);
}
}

View File

@ -27,9 +27,7 @@ namespace MCGalaxy.Commands.Building {
public override CommandAlias[] Aliases {
get { return new[] { new CommandAlias("Donut"), new CommandAlias("Bagel") }; }
}
protected override string PlaceMessage {
get { return "Place a block for the centre, then another for the radius."; }
}
protected override string PlaceMessage { get { return "Place a block for the centre, then another for the radius."; } }
protected override void GetMarks(DrawArgs dArgs, ref Vec3S32[] m) {
int dx = m[0].X - m[1].X, dy = m[0].Y - m[1].Y, dz = m[0].Z - m[1].Z;

View File

@ -16,98 +16,56 @@
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Drawing.Brushes;
using MCGalaxy.Drawing.Ops;
using MCGalaxy.Generator.Foliage;
using MCGalaxy.Maths;
using BlockID = System.UInt16;
namespace MCGalaxy.Commands.Building {
public sealed class CmdTree : Command {
public sealed class CmdTree : DrawCmd {
public override string name { get { return "Tree"; } }
public override string type { get { return CommandTypes.Building; } }
public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Builder; } }
public override bool SuperUseable { get { return false; } }
public override void Use(Player p, string message) {
string[] parts = message.SplitSpaces(3);
protected override int MarksCount { get { return 1; } }
protected override string SelectionType { get { return "location"; } }
protected override string PlaceMessage { get { return "Select where you wish your tree to grow"; } }
protected override DrawOp GetDrawOp(DrawArgs dArgs) {
string[] args = dArgs.Message.SplitSpaces(3);
Tree tree = Tree.Find(args[0]);
if (tree == null) tree = new NormalTree();
DrawArgs dArgs = new DrawArgs();
dArgs.size = -1;
Tree tree = Tree.Find(parts[0]);
if (tree == null) {
dArgs.brushMsg = message;
tree = new NormalTree();
}
dArgs.tree = tree;
int size;
if (parts.Length > 1 && int.TryParse(parts[1], out size)) {
if (size < tree.MinSize) {
Player.Message(p, "Value must be {0} or above for {1} trees.", tree.MinSize, parts[0]); return;
}
if (size > tree.MaxSize) {
Player.Message(p, "Value must be {0} or below for {1} trees.", tree.MaxSize, parts[0]); return;
}
dArgs.size = size;
dArgs.brushMsg = message.Splice(2, 0); // type value/height brush
int size;
if (args.Length > 1 && int.TryParse(args[1], out size)) {
Player p = dArgs.Player;
string opt = args[0] + " tree size";
if (!CommandParser.GetInt(p, args[1], opt, ref size, tree.MinSize, tree.MaxSize)) return null;
} else {
dArgs.brushMsg = message.Splice(1, 0); // type brush
size = -1;
}
if (!CheckBrush(p, dArgs.brushMsg)) return;
Player.Message(p, "Select where you wish your tree to grow");
p.MakeSelection(1, "Selecting location for %STree", dArgs, DoTree);
}
static bool CheckBrush(Player p, string brushMsg) {
if (brushMsg.Length == 0) return true;
if (!p.group.CanExecute("Brush")) {
Player.Message(p, "You cannot use %T/Brush%S, so therefore cannot use %T/Tree%S with a brush."); return false;
}
return ParseBrush(brushMsg, p, Block.Air) != null;
}
bool DoTree(Player p, Vec3S32[] marks, object state, BlockID block) {
DrawArgs dArgs = (DrawArgs)state;
TreeDrawOp op = new TreeDrawOp();
op.Tree = dArgs.tree;
op.Size = dArgs.size;
Brush brush = null;
if (dArgs.brushMsg.Length > 0) brush = ParseBrush(dArgs.brushMsg, p, block);
DrawOpPerformer.Do(op, brush, p, marks);
return true;
op.Tree = tree; op.Size = size;
return op;
}
static Brush ParseBrush(string raw, Player p, BlockID block) {
string[] parts = raw.SplitSpaces(2);
BrushFactory brush = BrushFactory.Find(parts[0]);
if (brush == null) {
Player.Message(p, "No brush found with name \"{0}\".", parts[0]);
Player.Message(p, "Available brushes: " + BrushFactory.Available);
return null;
protected override void GetBrush(DrawArgs dArgs) {
TreeDrawOp op = (TreeDrawOp)dArgs.Op;
if (op.Size != -1) {
dArgs.BrushArgs = dArgs.Message.Splice(2, 0); // type, value/height, brush args
} else {
dArgs.BrushArgs = dArgs.Message.Splice(1, 0); // type, brush args
}
// use leaf blocks by default
if (dArgs.BrushName.CaselessEq("Normal") && dArgs.BrushArgs.Length == 0) {
dArgs.BrushArgs = Block.Leaves.ToString();
}
string brushArgs = parts.Length >= 2 ? parts[1].ToLower() : "";
BrushArgs args = new BrushArgs(p, brushArgs, block);
return brush.Construct(args);
}
class DrawArgs { public Tree tree; public string brushMsg; public int size; }
public override void Help(Player p) {
Player.Message(p, "%T/Tree [type] %H- Draws a tree.");
Player.Message(p, "%T/Tree [type] [size] %H- Draws a tree of given size.");
Player.Message(p, "%T/Tree [type] [brush name] <brush args>");
Player.Message(p, "%T/Tree [type] [size] [brush name] <brush args>");
Player.Message(p, "%T/Tree [type] <brush args> %H- Draws a tree.");
Player.Message(p, "%T/Tree [type] [size/height] <brush args>");
Player.Message(p, "%H Types: &f{0}", Tree.TreeTypes.Join(t => t.Key));
Player.Message(p, "%H For help about brushes, type %T/Help Brush%H.");
Player.Message(p, BrushHelpLine);
}
}
}
}

View File

@ -26,9 +26,7 @@ namespace MCGalaxy.Commands.Building {
protected override int MarksCount { get { return 3; } }
protected override string PlaceMessage { get { return "Place three blocks to determine the edges."; } }
protected override DrawOp GetDrawOp(DrawArgs dArgs) {
return new TriangleDrawOp();
}
protected override DrawOp GetDrawOp(DrawArgs dArgs) { return new TriangleDrawOp(); }
public override void Help(Player p) {
Player.Message(p, "%T/Triangle <brush args>");

View File

@ -100,7 +100,7 @@ namespace MCGalaxy.Drawing.Brushes {
};
public override Brush Construct(BrushArgs args) {
CopyState cState = args.Player.CopySlots[args.Player.CurrentCopySlot];
CopyState cState = args.Player.CurrentCopy;
if (cState == null) {
args.Player.SendMessage("You haven't copied anything yet.");
return null;

View File

@ -35,15 +35,12 @@ namespace MCGalaxy.Drawing.Ops {
public override string Name { get { return "Tree"; } }
public Random random = new Random();
public Tree Tree;
static Brush defBrush = new SolidBrush(Block.Leaves);
public Tree Tree;
public int Size = -1;
public override long BlocksAffected(Level lvl, Vec3S32[] marks) { return Tree.EstimateBlocksAffected(); }
public override void Perform(Vec3S32[] marks, Brush brush, DrawOpOutput output) {
if (brush == null) brush = defBrush;
Vec3U16 P = Clamp(marks[0]);
Level lvl = Level;
@ -54,7 +51,7 @@ namespace MCGalaxy.Drawing.Ops {
Tree.Generate(P.X, P.Y, P.Z, (xT, yT, zT, bT) =>
{
if (bT != Block.Leaves) {
output(Place(xT, yT, zT, (ushort)bT));
output(Place(xT, yT, zT, bT));
} else if (lvl.IsAirAt(xT, yT, zT)) {
leaves.Add(new Vec3U16(xT, yT, zT));
}

View File

@ -57,9 +57,7 @@ namespace MCGalaxy.Drawing.Ops {
}
pos = p1;
foreach (char c in Text) {
DrawLetter(Player, c, brush, output);
}
foreach (char c in Text) { DrawLetter(Player, c, brush, output); }
}
void DrawLetter(Player p, char c, Brush brush, DrawOpOutput output) {

View File

@ -48,7 +48,7 @@ namespace MCGalaxy {
public bool IsAfk, AutoAfk;
public bool cmdTimer;
public bool UsingWom;
public string BrushName = "normal", DefaultBrushArgs = "";
public string BrushName = "Normal", DefaultBrushArgs = "";
public Transform Transform = NoTransform.Instance;
public string afkMessage;
public bool disconnected, ClickToMark = true;
@ -160,6 +160,13 @@ namespace MCGalaxy {
public List<CopyState> CopySlots = new List<CopyState>();
public int CurrentCopySlot;
public CopyState CurrentCopy {
get { return CurrentCopySlot >= CopySlots.Count ? null : CopySlots[CurrentCopySlot]; }
set {
while (CurrentCopySlot >= CopySlots.Count) { CopySlots.Add(null); }
CopySlots[CurrentCopySlot] = value;
}
}
// BlockDefinitions
internal int gbStep = 0, lbStep = 0;

View File

@ -334,11 +334,6 @@ namespace MCGalaxy {
}
return true;
}
public void SetCurrentCopy(CopyState state) {
while (CurrentCopySlot >= CopySlots.Count) { CopySlots.Add(null); }
CopySlots[CurrentCopySlot] = state;
}
#endregion