Despawn bots on level unload.

This commit is contained in:
UnknownShadow200 2016-01-23 10:09:31 +11:00
parent 02b09db0d5
commit ceb6e5a1a5
5 changed files with 326 additions and 350 deletions

View File

@ -31,8 +31,9 @@ namespace MCGalaxy.Commands
if (message == "") { Help(p); return; } if (message == "") { Help(p); return; }
if (p != null) if (p != null)
{ {
if (!PlayerBot.ValidName(message)) { Player.SendMessage(p, "bot name " + message + " not valid!"); return; } if (!Player.ValidName(message)) { Player.SendMessage(p, "bot name " + message + " not valid!"); return; }
PlayerBot.playerbots.Add(new PlayerBot(message, p.level, p.pos[0], p.pos[1], p.pos[2], p.rot[0], 0)); PlayerBot bot = new PlayerBot(message, p.level, p.pos[0], p.pos[1], p.pos[2], p.rot[0], 0);
PlayerBot.Add(bot);
//who.SendMessage("You were summoned by " + p.color + p.name + "&e."); //who.SendMessage("You were summoned by " + p.color + p.name + "&e.");
return; return;
} }

View File

@ -31,32 +31,21 @@ namespace MCGalaxy.Commands
public override void Use(Player p, string message) public override void Use(Player p, string message)
{ {
if (message == "") { Help(p); return; } if (message == "") { Help(p); return; }
if (p == null) if (p == null) {
{ Player.SendMessage(p, "This command can only be used in-game!"); return;
Player.SendMessage(p, "This command can only be used in-game!");
return;
} }
try try
{ {
if (message.ToLower() == "all") if (message.ToLower() == "all")
{ {
for (int i = 0; i < PlayerBot.playerbots.Count; i++) PlayerBot.RemoveAllFromLevel(p.level);
{
if (PlayerBot.playerbots[i].level == p.level)
{
// PlayerBot.playerbots.Remove(PlayerBot.playerbots[i]);
PlayerBot Pb = PlayerBot.playerbots[i];
Pb.removeBot();
i--;
}
}
} }
else else
{ {
PlayerBot who = PlayerBot.Find(message); PlayerBot who = PlayerBot.Find(message);
if (who == null) { Player.SendMessage(p, "There is no bot " + who + "!"); return; } if (who == null) { Player.SendMessage(p, "There is no bot " + who + "!"); return; }
if (p.level != who.level) { Player.SendMessage(p, who.name + " is in a different level."); return; } if (p.level != who.level) { Player.SendMessage(p, who.name + " is in a different level."); return; }
who.removeBot(); PlayerBot.Remove(who);
Player.SendMessage(p, "Removed bot."); Player.SendMessage(p, "Removed bot.");
} }
} }

View File

@ -317,6 +317,7 @@ namespace MCGalaxy
try try
{ {
PlayerBot.RemoveAllFromLevel(this);
//physChecker.Stop(); //physChecker.Stop();
//physChecker.Dispose(); //physChecker.Dispose();
physThread.Abort(); physThread.Abort();

View File

@ -2835,7 +2835,7 @@ Next: continue;
} }
return lines; return lines;
} }
public static bool ValidName(string name, Player p = null) { public static bool ValidName(string name) {
string allowedchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._+"; string allowedchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._+";
return name.All(ch => allowedchars.IndexOf(ch) != -1); return name.All(ch => allowedchars.IndexOf(ch) != -1);
} }

View File

@ -14,19 +14,20 @@
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
namespace MCGalaxy using System.Timers;
{
public sealed class PlayerBot namespace MCGalaxy {
{
public sealed class PlayerBot {
public static List<PlayerBot> playerbots = new List<PlayerBot>(64); public static List<PlayerBot> playerbots = new List<PlayerBot>(64);
public bool hunt = false; public bool hunt = false, kill = false;
public bool kill = false;
public string AIName = ""; public string AIName = "";
public string name; public string name;
@ -46,51 +47,32 @@ namespace MCGalaxy
bool jumping = false; bool jumping = false;
int currentjump = 0; int currentjump = 0;
public System.Timers.Timer botTimer = new System.Timers.Timer(100); System.Timers.Timer botTimer = new System.Timers.Timer(100);
public System.Timers.Timer moveTimer = new System.Timers.Timer(100 / 24); System.Timers.Timer moveTimer = new System.Timers.Timer(100 / 24);
public System.Timers.Timer jumpTimer = new System.Timers.Timer(95); System.Timers.Timer jumpTimer = new System.Timers.Timer(95);
static readonly object botsLock = new object();
#region == constructors == #region == constructors ==
public PlayerBot(string n, Level l) public PlayerBot(string n, Level lvl, ushort x, ushort y, ushort z, byte rotx, byte roty) {
{
Server.s.Log("adding " + n + " bot");
name = n; name = n;
color = "&1"; color = "&1";
id = FreeId(); id = FreeId();
level = l; level = lvl;
ushort x = (ushort)((0.5 + level.spawnx) * 32); SetPos(x, y, z, rotx, roty);
ushort y = (ushort)((1 + level.spawny) * 32);
ushort z = (ushort)((0.5 + level.spawnz) * 32);
pos = new ushort[3] { x, y, z }; rot = new byte[2] { level.rotx, level.roty };
GlobalSpawn();
}
public PlayerBot(string n, Level l, ushort x, ushort y, ushort z, byte rotx, byte roty)
{
name = n;
color = "&1";
id = FreeId();
level = l; botTimer.Elapsed += BotTimerFunc;
pos = new ushort[3] { x, y, z }; rot = new byte[2] { rotx, roty }; botTimer.Start();
GlobalSpawn(); moveTimer.Elapsed += MoveTimerFunc;
moveTimer.Start();
foreach (Player p in Player.players)
{
if (p.level == level)
{
Player.SendMessage(p, color + name + Server.DefaultColor + ", the bot, has been added.");
}
} }
botTimer.Elapsed += delegate void BotTimerFunc(object sender, ElapsedEventArgs e) {
{
int currentNum, foundNum = (32 * 75); int currentNum, foundNum = (32 * 75);
Random rand = new Random(); Random rand = new Random();
ushort x = (ushort)Math.Round((decimal)pos[0] / 32);
x = (ushort)Math.Round((decimal)pos[0] / (decimal)32); ushort y = (ushort)((pos[1] - 33) / 32);
y = (ushort)((pos[1] - 33) / 32); ushort z = (ushort)Math.Round((decimal)pos[2] / 32);
z = (ushort)Math.Round((decimal)pos[2] / (decimal)32);
if (kill) if (kill)
{ {
@ -109,7 +91,7 @@ namespace MCGalaxy
} }
} }
if (Waypoints.Count < 1) if (Waypoints.Count == 0)
{ {
if (hunt) if (hunt)
Player.players.ForEach(delegate(Player p) Player.players.ForEach(delegate(Player p)
@ -251,7 +233,7 @@ namespace MCGalaxy
currentPoint = 0; currentPoint = 0;
return; return;
case "remove": case "remove":
removeBot(); PlayerBot.Remove(this);
return; return;
case "linkscript": case "linkscript":
if (File.Exists("bots/" + Waypoints[currentPoint].newscript)) if (File.Exists("bots/" + Waypoints[currentPoint].newscript))
@ -297,12 +279,9 @@ namespace MCGalaxy
else if (rot[1] > 250) rot[1] = 0; else if (rot[1] > 250) rot[1] = 0;
else rot[1] += 4; else rot[1] += 4;
} }
}; }
botTimer.Start(); void MoveTimerFunc(object sender, ElapsedEventArgs e) {
moveTimer.Elapsed += delegate
{
moveTimer.Interval = Server.updateTimer.Interval / movementSpeed; moveTimer.Interval = Server.updateTimer.Interval / movementSpeed;
if (!movement) return; if (!movement) return;
int newNum; Random rand = new Random(); int newNum; Random rand = new Random();
@ -312,9 +291,9 @@ namespace MCGalaxy
pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32)); pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32));
} }
x = (ushort)Math.Round((decimal)(pos[0] - 16) / (decimal)32); ushort x = (ushort)Math.Round((decimal)(pos[0] - 16) / 32);
y = (ushort)((pos[1] - 64) / 32); ushort y = (ushort)((pos[1] - 64) / 32);
z = (ushort)Math.Round((decimal)(pos[2] - 16) / (decimal)32); ushort z = (ushort)Math.Round((decimal)(pos[2] - 16) / 32);
byte b = Block.Convert(level.GetTile(x, y, z)); byte b = Block.Convert(level.GetTile(x, y, z));
byte b1, b2, b3;//, b4; byte b1, b2, b3;//, b4;
@ -372,48 +351,65 @@ namespace MCGalaxy
pos[1] = (ushort)(pos[1] + (Math.Sign(foundPos[1] - pos[1]))); pos[1] = (ushort)(pos[1] + (Math.Sign(foundPos[1] - pos[1])));
} }
}*/ }*/
};
moveTimer.Start();
} }
#endregion #endregion
#region ==Input == public void SetPos(ushort x, ushort y, ushort z, byte rotx, byte roty) {
public void SetPos(ushort x, ushort y, ushort z, byte rotx, byte roty)
{
pos = new ushort[3] { x, y, z }; pos = new ushort[3] { x, y, z };
rot = new byte[2] { rotx, roty }; rot = new byte[2] { rotx, roty };
} }
#endregion
public void removeBot() public static void Add(PlayerBot bot) {
{ lock (botsLock)
this.botTimer.Stop(); playerbots.Add(bot);
GlobalDie(); bot.GlobalSpawn();
PlayerBot.playerbots.Remove(this);
foreach (Player p in Player.players) {
if (p.level == bot.level)
Player.SendMessage(p, bot.color + bot.name + "%S, the bot, has been added.");
}
} }
public void GlobalSpawn() public static void Remove(PlayerBot bot) {
{ lock (botsLock)
Player.players.ForEach(delegate(Player p) //bots dont need to be informed of other bots here playerbots.Remove(bot);
{ bot.GlobalDespawn();
bot.botTimer.Stop();
bot.moveTimer.Stop();
bot.jumpTimer.Stop();
}
public static void RemoveAllFromLevel(Level lvl) {
lock (botsLock)
RemoveAll(lvl);
}
static void RemoveAll(Level lvl) {
for (int i = 0; i < PlayerBot.playerbots.Count; i++) {
PlayerBot bot = PlayerBot.playerbots[i];
if (PlayerBot.playerbots[i].level == lvl) {
Remove(bot);
i--;
}
}
}
public void GlobalSpawn() {
Player.players.ForEach(p => {
if (p.level == level) if (p.level == level)
p.SendSpawn(id, color + name, pos[0], pos[1], pos[2], rot[0], rot[1]); p.SendSpawn(id, color + name, pos[0], pos[1], pos[2], rot[0], rot[1]);
}); });
} }
public void GlobalDie() public void GlobalDespawn() {
{ Player.players.ForEach(p => {
Server.s.Log("removing " + name + " bot"); if (p.level == level)
Player.players.ForEach(delegate(Player p)
{
if (p.level != level) { return; }
p.SendDespawn(id); p.SendDespawn(id);
}); });
playerbots.Remove(this); //dont know if this is allowed really calling itself to kind of die
} }
public void Update() { public void Update() { }
}
void UpdatePosition() { void UpdatePosition() {
byte[] packet = NetUtils.GetPositionPacket(id, pos, oldpos, rot, oldrot, rot[1], true); byte[] packet = NetUtils.GetPositionPacket(id, pos, oldpos, rot, oldrot, rot[1], true);
@ -454,30 +450,19 @@ namespace MCGalaxy
} }
return matches == 1 ? match : null; return matches == 1 ? match : null;
} }
public static bool ValidName(string name)
{
string allowedchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890_";
foreach (char ch in name) { if (allowedchars.IndexOf(ch) == -1) { return false; } } return true;
}
#endregion #endregion
#region Global == public static void GlobalUpdatePosition() {
public static void GlobalUpdatePosition()
{
playerbots.ForEach( playerbots.ForEach(
delegate(PlayerBot b) { b.UpdatePosition(); } delegate(PlayerBot b) { b.UpdatePosition(); }
); );
} }
public static void GlobalUpdate() public static void GlobalUpdate() {
{ while (true) {
while (true)
{
Thread.Sleep(100); Thread.Sleep(100);
playerbots.ForEach(delegate(PlayerBot b) { b.Update(); }); playerbots.ForEach(delegate(PlayerBot b) { b.Update(); });
} }
} }
#endregion
} }
} }