diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 3f0d5009..1f8d4117 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -570,7 +570,7 @@ namespace TShockAPI } else if (npcs.Count > 1) { - args.Player.SendMessage("More than one mob matched!", Color.Red); + args.Player.SendMessage(string.Format("More than one ({0}) mob matched!", npcs.Count), Color.Red); return; } else @@ -583,7 +583,12 @@ namespace TShockAPI if (npc.type >= 1 && npc.type < Main.maxNPCTypes) { for (int i = 0; i < amount; i++) - TSPlayer.Server.SpawnNPC(npc.type, npc.name, (int)args.Player.X, (int)args.Player.Y); + { + int spawnTileX; + int spawnTileY; + Tools.GetRandomClearTileWithInRange((int)args.Player.TileX, (int)args.Player.TileY, 50, 20, out spawnTileX, out spawnTileY); + TSPlayer.Server.SpawnNPC(npc.type, npc.name, spawnTileX, spawnTileY); + } Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", npc.name, amount)); } else diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 59194123..b605a165 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -195,9 +195,9 @@ namespace TShockAPI NetMessage.syncPlayers(); } - public int SpawnNPC(int type, string name, int x, int y) + public int SpawnNPC(int type, string name, int tileX, int tileY) { - int npcid = NPC.NewNPC(x, y, type, 0); + int npcid = NPC.NewNPC(tileX * 16, tileY * 16, type, 0); // This is for special slimes Main.npc[npcid].SetDefaults(name); return npcid; diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index c63c41f2..4e7bc924 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -33,6 +33,7 @@ namespace TShockAPI internal class Tools { + private static Random random = new Random(); private static List groups = new List(); /// @@ -163,6 +164,35 @@ namespace TShockAPI return found; } + public static void GetRandomClearTileWithInRange(int startTileX, int startTileY, int tileXRange, int tileYRange, out int tileX, out int tileY) + { + int j = 0; + do + { + if (j == 100) + { + tileX = startTileX; + tileY = startTileY; + break; + } + + tileX = startTileX + random.Next(tileXRange * -1, tileXRange); + tileY = startTileY + random.Next(tileYRange * -1, tileYRange); + j++; + } + while (TileValid(tileX, tileY) && !Tools.TileClear(tileX, tileY)); + } + + private static bool TileValid(int tileX, int tileY) + { + return tileX >= 0 && tileX <= Main.maxTilesX && tileY >= 0 && tileY <= Main.maxTilesY; + } + + private static bool TileClear(int tileX, int tileY) + { + return !Main.tile[tileX, tileY].active; + } + public static NPC GetNPCById(int id) { NPC npc = new NPC();