Randomized spawn point for mobs (range 50x20 tiles in all directions)

This commit is contained in:
ricky 2011-06-17 13:11:28 +10:00
parent b290802e6a
commit 8fa4687f33
3 changed files with 39 additions and 4 deletions

View file

@ -570,7 +570,7 @@ namespace TShockAPI
} }
else if (npcs.Count > 1) 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; return;
} }
else else
@ -583,7 +583,12 @@ namespace TShockAPI
if (npc.type >= 1 && npc.type < Main.maxNPCTypes) if (npc.type >= 1 && npc.type < Main.maxNPCTypes)
{ {
for (int i = 0; i < amount; i++) 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)); Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", npc.name, amount));
} }
else else

View file

@ -195,9 +195,9 @@ namespace TShockAPI
NetMessage.syncPlayers(); 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 // This is for special slimes
Main.npc[npcid].SetDefaults(name); Main.npc[npcid].SetDefaults(name);
return npcid; return npcid;

View file

@ -33,6 +33,7 @@ namespace TShockAPI
internal class Tools internal class Tools
{ {
private static Random random = new Random();
private static List<Group> groups = new List<Group>(); private static List<Group> groups = new List<Group>();
/// <summary> /// <summary>
@ -163,6 +164,35 @@ namespace TShockAPI
return found; 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) public static NPC GetNPCById(int id)
{ {
NPC npc = new NPC(); NPC npc = new NPC();