From 33b1ca969b2b84c3d39567452f748de022509e16 Mon Sep 17 00:00:00 2001 From: CoderCow Date: Mon, 1 Jul 2013 20:06:17 +0200 Subject: [PATCH] -Added "-d" parameter to "/region info". Use to display the given region's boundaries as wires. --- TShockAPI/Commands.cs | 51 +++++++++++++++++++++++++++++++++++++++---- TShockAPI/Utils.cs | 23 ++++++++++++++++++- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 89ae93fe..bc88fd07 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -24,7 +24,7 @@ using System.IO; using System.Linq; using System.Net; using System.Text; -using System.Threading; +using System.Threading; using TShockAPI.PluginUpdater; using Terraria; using TShockAPI.DB; @@ -2845,16 +2845,27 @@ namespace TShockAPI { if (args.Parameters.Count > 1) { + if (args.Parameters.Count > 4) + { + args.Player.SendErrorMessage("Invalid syntax! Proper syntax: /region info [region] [-d] [page]"); + break; + } + string regionName = args.Parameters[1]; + bool displayBoundaries = args.Parameters.Skip(2).Any( + p => p.Equals("-d", StringComparison.InvariantCultureIgnoreCase) + ); + Region region = TShock.Regions.GetRegionByName(regionName); if (region == null) { - args.Player.SendErrorMessage("Region {0} does not exist.", regionName); + args.Player.SendErrorMessage("Region \"{0}\" does not exist.", regionName); break; } + int pageNumberIndex = displayBoundaries ? 3 : 2; int pageNumber; - if (!PaginationTools.TryParsePageNumber(args.Parameters, 2, args.Player, out pageNumber)) + if (!PaginationTools.TryParsePageNumber(args.Parameters, pageNumberIndex, args.Player, out pageNumber)) break; List lines = new List @@ -2901,6 +2912,38 @@ namespace TShockAPI FooterFormat = "Type /region info {0} for more information." } ); + + if (displayBoundaries) + { + Rectangle regionArea = region.Area; + foreach (Point boundaryPoint in Utils.Instance.EnumerateRegionBoundaries(regionArea)) + { + // Preferring dotted lines as those should easily be distinguishable from actual wires. + if ((boundaryPoint.X + boundaryPoint.Y & 1) == 0) + { + // Could be improved by sending raw tile data to the client instead but not really + // worth the effort as chances are very low that overwriting the wire for a few + // nanoseconds will cause much trouble. + Tile tile = Main.tile[boundaryPoint.X, boundaryPoint.Y]; + bool oldWireState = tile.wire; + tile.wire = true; + + try { + args.Player.SendTileSquare(boundaryPoint.X, boundaryPoint.Y, 1); + } finally { + tile.wire = oldWireState; + } + } + } + + new Timer((dummy) => { + foreach (Point boundaryPoint in Utils.Instance.EnumerateRegionBoundaries(regionArea)) + if ((boundaryPoint.X + boundaryPoint.Y & 1) == 0) + args.Player.SendTileSquare(boundaryPoint.X, boundaryPoint.Y, 1); + }, + null, 5000, Timeout.Infinite + ); + } } else { @@ -3034,7 +3077,7 @@ namespace TShockAPI "remove [user] [region] - Removes a user from a region.", "allowg [group] [region] - Allows a user group to a region.", "removeg [group] [region] - Removes a user group from a region.", - "info [region] - Displays several information about the given region.", + "info [region] [-d] - Displays several information about the given region.", "protect [name] [true/false] - Sets whether the tiles inside the region are protected or not.", "z [name] [#] - Sets the z-order of the region.", }; diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 8687ec4b..f98cb284 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -22,7 +22,8 @@ using System.Linq; using System.Net; using System.Net.Sockets; using System.Security.Cryptography; -using System.Text; +using System.Text; +using TShockAPI.DB; using Terraria; namespace TShockAPI @@ -841,5 +842,25 @@ namespace TShockAPI } return new string(returnstr); } + + /// + /// Enumerates boundary points of the given region's rectangle. + /// + /// The region's area to enumerate through. + /// The enumerated boundary points. + public IEnumerable EnumerateRegionBoundaries(Rectangle regionArea) + { + for (int x = 0; x < regionArea.Width + 1; x++) + { + yield return new Point(regionArea.Left + x, regionArea.Top); + yield return new Point(regionArea.Left + x, regionArea.Bottom); + } + + for (int y = 1; y < regionArea.Height; y++) + { + yield return new Point(regionArea.Left, regionArea.Top + y); + yield return new Point(regionArea.Right, regionArea.Top + y); + } + } } }