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

@ -1,32 +1,33 @@
/* /*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS" software distributed under the Licenses are distributed on an "AS IS"
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,319 +47,297 @@ 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 = lvl;
SetPos(x, y, z, rotx, roty);
level = l; botTimer.Elapsed += BotTimerFunc;
ushort x = (ushort)((0.5 + level.spawnx) * 32); botTimer.Start();
ushort y = (ushort)((1 + level.spawny) * 32); moveTimer.Elapsed += MoveTimerFunc;
ushort z = (ushort)((0.5 + level.spawnz) * 32); moveTimer.Start();
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)
{ void BotTimerFunc(object sender, ElapsedEventArgs e) {
name = n; int currentNum, foundNum = (32 * 75);
color = "&1"; Random rand = new Random();
id = FreeId(); ushort x = (ushort)Math.Round((decimal)pos[0] / 32);
ushort y = (ushort)((pos[1] - 33) / 32);
ushort z = (ushort)Math.Round((decimal)pos[2] / 32);
level = l; if (kill)
pos = new ushort[3] { x, y, z }; rot = new byte[2] { rotx, roty };
GlobalSpawn();
foreach (Player p in Player.players)
{ {
if (p.level == level) foreach (Player p in Player.players)
{ {
Player.SendMessage(p, color + name + Server.DefaultColor + ", the bot, has been added."); if ((ushort)(p.pos[0] / 32) == x)
}
}
botTimer.Elapsed += delegate
{
int currentNum, foundNum = (32 * 75);
Random rand = new Random();
x = (ushort)Math.Round((decimal)pos[0] / (decimal)32);
y = (ushort)((pos[1] - 33) / 32);
z = (ushort)Math.Round((decimal)pos[2] / (decimal)32);
if (kill)
{
foreach (Player p in Player.players)
{ {
if ((ushort)(p.pos[0] / 32) == x) if (Math.Abs((ushort)(p.pos[1] / 32) - y) < 2)
{ {
if (Math.Abs((ushort)(p.pos[1] / 32) - y) < 2) if ((ushort)(p.pos[2] / 32) == z)
{ {
if ((ushort)(p.pos[2] / 32) == z) p.HandleDeath(Block.Zero);
{
p.HandleDeath(Block.Zero);
}
} }
} }
} }
} }
}
if (Waypoints.Count < 1) if (Waypoints.Count == 0)
{ {
if (hunt) if (hunt)
Player.players.ForEach(delegate(Player p) Player.players.ForEach(delegate(Player p)
{ {
if (p.level == level && !p.invincible) if (p.level == level && !p.invincible)
{ {
currentNum = Math.Abs(p.pos[0] - pos[0]) + Math.Abs(p.pos[1] - pos[1]) + Math.Abs(p.pos[2] - pos[2]); currentNum = Math.Abs(p.pos[0] - pos[0]) + Math.Abs(p.pos[1] - pos[1]) + Math.Abs(p.pos[2] - pos[2]);
if (currentNum < foundNum) if (currentNum < foundNum)
{ {
foundNum = currentNum; foundNum = currentNum;
foundPos = p.pos; foundPos = p.pos;
foundRot = p.rot; foundRot = p.rot;
movement = true; movement = true;
rot[1] = (byte)(255 - foundRot[1]); rot[1] = (byte)(255 - foundRot[1]);
if (foundRot[0] < 128) rot[0] = (byte)(foundRot[0] + 128); if (foundRot[0] < 128) rot[0] = (byte)(foundRot[0] + 128);
else rot[0] = (byte)(foundRot[0] - 128); else rot[0] = (byte)(foundRot[0] - 128);
} }
} }
}); });
} }
else else
{ {
bool skip = false; bool skip = false;
movement = false; movement = false;
retry: switch (Waypoints[currentPoint].type) retry: switch (Waypoints[currentPoint].type)
{ {
case "walk": case "walk":
foundPos[0] = Waypoints[currentPoint].x; foundPos[0] = Waypoints[currentPoint].x;
foundPos[1] = Waypoints[currentPoint].y; foundPos[1] = Waypoints[currentPoint].y;
foundPos[2] = Waypoints[currentPoint].z; foundPos[2] = Waypoints[currentPoint].z;
movement = true; movement = true;
if ((ushort)(pos[0] / 32) == (ushort)(Waypoints[currentPoint].x / 32)) if ((ushort)(pos[0] / 32) == (ushort)(Waypoints[currentPoint].x / 32))
{
if ((ushort)(pos[2] / 32) == (ushort)(Waypoints[currentPoint].z / 32))
{ {
if ((ushort)(pos[2] / 32) == (ushort)(Waypoints[currentPoint].z / 32)) rot[0] = Waypoints[currentPoint].rotx;
{ rot[1] = Waypoints[currentPoint].roty;
rot[0] = Waypoints[currentPoint].rotx; currentPoint++;
rot[1] = Waypoints[currentPoint].roty; movement = false;
currentPoint++;
movement = false;
if (currentPoint == Waypoints.Count) currentPoint = 0; if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; } if (!skip) { skip = true; goto retry; }
}
} }
break; }
case "teleport": break;
pos[0] = Waypoints[currentPoint].x; case "teleport":
pos[1] = Waypoints[currentPoint].y; pos[0] = Waypoints[currentPoint].x;
pos[2] = Waypoints[currentPoint].z; pos[1] = Waypoints[currentPoint].y;
rot[0] = Waypoints[currentPoint].rotx; pos[2] = Waypoints[currentPoint].z;
rot[1] = Waypoints[currentPoint].roty; rot[0] = Waypoints[currentPoint].rotx;
currentPoint++; rot[1] = Waypoints[currentPoint].roty;
if (currentPoint == Waypoints.Count) currentPoint = 0; currentPoint++;
return; if (currentPoint == Waypoints.Count) currentPoint = 0;
case "wait": return;
if (countdown != 0) case "wait":
if (countdown != 0)
{
countdown--;
if (countdown == 0)
{ {
countdown--; currentPoint++;
if (countdown == 0) if (currentPoint == Waypoints.Count) currentPoint = 0;
{ if (!skip) { skip = true; goto retry; }
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
}
} }
else }
{ else
countdown = Waypoints[currentPoint].seconds; {
} countdown = Waypoints[currentPoint].seconds;
return; }
case "nod": return;
if (countdown != 0) case "nod":
{ if (countdown != 0)
countdown--; {
countdown--;
if (nodUp) if (nodUp)
{ {
if (rot[1] > 32 && rot[1] < 128) nodUp = !nodUp; if (rot[1] > 32 && rot[1] < 128) nodUp = !nodUp;
else
{
if (rot[1] + (byte)Waypoints[currentPoint].rotspeed > 255) rot[1] = 0;
else rot[1] += (byte)Waypoints[currentPoint].rotspeed;
}
}
else else
{ {
if (rot[1] > 128 && rot[1] < 224) nodUp = !nodUp; if (rot[1] + (byte)Waypoints[currentPoint].rotspeed > 255) rot[1] = 0;
else else rot[1] += (byte)Waypoints[currentPoint].rotspeed;
{
if (rot[1] - (byte)Waypoints[currentPoint].rotspeed < 0) rot[1] = 255;
else rot[1] -= (byte)Waypoints[currentPoint].rotspeed;
}
}
if (countdown == 0)
{
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
} }
} }
else else
{ {
countdown = Waypoints[currentPoint].seconds; if (rot[1] > 128 && rot[1] < 224) nodUp = !nodUp;
} else
return;
case "spin":
if (countdown != 0)
{
countdown--;
if (rot[0] + (byte)Waypoints[currentPoint].rotspeed > 255) rot[0] = 0;
else if (rot[0] + (byte)Waypoints[currentPoint].rotspeed < 0) rot[0] = 255;
else rot[0] += (byte)Waypoints[currentPoint].rotspeed;
if (countdown == 0)
{ {
currentPoint++; if (rot[1] - (byte)Waypoints[currentPoint].rotspeed < 0) rot[1] = 255;
if (currentPoint == Waypoints.Count) currentPoint = 0; else rot[1] -= (byte)Waypoints[currentPoint].rotspeed;
if (!skip) { skip = true; goto retry; }
} }
} }
else
{
countdown = Waypoints[currentPoint].seconds;
}
return;
case "speed":
movementSpeed = (int)Math.Round((decimal)((decimal)24 / (decimal)100 * (decimal)Waypoints[currentPoint].seconds));
if (movementSpeed == 0) movementSpeed = 1;
currentPoint++; if (countdown == 0)
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
return;
case "reset":
currentPoint = 0;
return;
case "remove":
removeBot();
return;
case "linkscript":
if (File.Exists("bots/" + Waypoints[currentPoint].newscript))
{ {
Command.all.Find("botset").Use(null, this.name + " " + Waypoints[currentPoint].newscript); currentPoint++;
return; if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
} }
}
else
{
countdown = Waypoints[currentPoint].seconds;
}
return;
case "spin":
if (countdown != 0)
{
countdown--;
currentPoint++; if (rot[0] + (byte)Waypoints[currentPoint].rotspeed > 255) rot[0] = 0;
if (currentPoint == Waypoints.Count) currentPoint = 0; else if (rot[0] + (byte)Waypoints[currentPoint].rotspeed < 0) rot[0] = 255;
if (!skip) { skip = true; goto retry; } else rot[0] += (byte)Waypoints[currentPoint].rotspeed;
return;
case "jump": if (countdown == 0)
jumpTimer.Elapsed += delegate
{ {
currentjump++; currentPoint++;
switch (currentjump) if (currentPoint == Waypoints.Count) currentPoint = 0;
{ if (!skip) { skip = true; goto retry; }
case 1: }
}
else
{
countdown = Waypoints[currentPoint].seconds;
}
return;
case "speed":
movementSpeed = (int)Math.Round((decimal)((decimal)24 / (decimal)100 * (decimal)Waypoints[currentPoint].seconds));
if (movementSpeed == 0) movementSpeed = 1;
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
return;
case "reset":
currentPoint = 0;
return;
case "remove":
PlayerBot.Remove(this);
return;
case "linkscript":
if (File.Exists("bots/" + Waypoints[currentPoint].newscript))
{
Command.all.Find("botset").Use(null, this.name + " " + Waypoints[currentPoint].newscript);
return;
}
currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; }
return;
case "jump":
jumpTimer.Elapsed += delegate
{
currentjump++;
switch (currentjump)
{
case 1:
case 2: pos[1] += 24; break; case 2: pos[1] += 24; break;
case 3: break; case 3: break;
case 4: pos[1] -= 24; break; case 4: pos[1] -= 24; break;
case 5: pos[1] -= 24; jumping = false; currentjump = 0; jumpTimer.Stop(); break; case 5: pos[1] -= 24; jumping = false; currentjump = 0; jumpTimer.Stop(); break;
} }
}; };
jumpTimer.Start(); jumpTimer.Start();
currentPoint++; currentPoint++;
if (currentPoint == Waypoints.Count) currentPoint = 0; if (currentPoint == Waypoints.Count) currentPoint = 0;
if (!skip) { skip = true; goto retry; } if (!skip) { skip = true; goto retry; }
break; break;
}
if (currentPoint == Waypoints.Count) currentPoint = 0;
} }
if (!movement) if (currentPoint == Waypoints.Count) currentPoint = 0;
{ }
if (rot[0] < 245) rot[0] += 8;
else rot[0] = 0;
if (rot[1] > 32 && rot[1] < 64) rot[1] = 224; if (!movement)
else if (rot[1] > 250) rot[1] = 0;
else rot[1] += 4;
}
};
botTimer.Start();
moveTimer.Elapsed += delegate
{ {
moveTimer.Interval = Server.updateTimer.Interval / movementSpeed; if (rot[0] < 245) rot[0] += 8;
if (!movement) return; else rot[0] = 0;
int newNum; Random rand = new Random();
if ((pos[1] - 19) % 32 != 0 && !jumping) if (rot[1] > 32 && rot[1] < 64) rot[1] = 224;
{ else if (rot[1] > 250) rot[1] = 0;
pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32)); else rot[1] += 4;
} }
}
void MoveTimerFunc(object sender, ElapsedEventArgs e) {
moveTimer.Interval = Server.updateTimer.Interval / movementSpeed;
if (!movement) return;
int newNum; Random rand = new Random();
x = (ushort)Math.Round((decimal)(pos[0] - 16) / (decimal)32); if ((pos[1] - 19) % 32 != 0 && !jumping)
y = (ushort)((pos[1] - 64) / 32); {
z = (ushort)Math.Round((decimal)(pos[2] - 16) / (decimal)32); pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32));
}
byte b = Block.Convert(level.GetTile(x, y, z)); ushort x = (ushort)Math.Round((decimal)(pos[0] - 16) / 32);
byte b1, b2, b3;//, b4; ushort y = (ushort)((pos[1] - 64) / 32);
ushort z = (ushort)Math.Round((decimal)(pos[2] - 16) / 32);
if (Block.Walkthrough(b) && !jumping) byte b = Block.Convert(level.GetTile(x, y, z));
{ byte b1, b2, b3;//, b4;
pos[1] = (ushort)(pos[1] - 32);
}
y = (ushort)((pos[1] - 64) / 32); //Block below feet if (Block.Walkthrough(b) && !jumping)
{
pos[1] = (ushort)(pos[1] - 32);
}
newNum = level.PosToInt((ushort)(x + Math.Sign(foundPos[0] - pos[0])), y, (ushort)(z + Math.Sign(foundPos[2] - pos[2]))); y = (ushort)((pos[1] - 64) / 32); //Block below feet
b = Block.Convert(level.GetTile(newNum));
b1 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 1, 0)));
b2 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 2, 0)));
b3 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 3, 0)));
if (Block.Walkthrough(b2) && Block.Walkthrough(b3) && !Block.Walkthrough(b1)) newNum = level.PosToInt((ushort)(x + Math.Sign(foundPos[0] - pos[0])), y, (ushort)(z + Math.Sign(foundPos[2] - pos[2])));
{ //Get ready to go up step b = Block.Convert(level.GetTile(newNum));
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]); b1 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 1, 0)));
pos[1] += (ushort)32; b2 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 2, 0)));
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]); b3 = Block.Convert(level.GetTile(level.IntOffset(newNum, 0, 3, 0)));
}
else if (Block.Walkthrough(b1) && Block.Walkthrough(b2))
{ //Stay on current level
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
else if (Block.Walkthrough(b) && Block.Walkthrough(b1))
{ //Drop a level
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[1] -= (ushort)32;
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
x = (ushort)Math.Round((decimal)(pos[0] - 16) / (decimal)32); if (Block.Walkthrough(b2) && Block.Walkthrough(b3) && !Block.Walkthrough(b1))
y = (ushort)((pos[1] - 64) / 32); { //Get ready to go up step
z = (ushort)Math.Round((decimal)(pos[2] - 16) / (decimal)32); pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[1] += (ushort)32;
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
else if (Block.Walkthrough(b1) && Block.Walkthrough(b2))
{ //Stay on current level
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
else if (Block.Walkthrough(b) && Block.Walkthrough(b1))
{ //Drop a level
pos[0] += (ushort)Math.Sign(foundPos[0] - pos[0]);
pos[1] -= (ushort)32;
pos[2] += (ushort)Math.Sign(foundPos[2] - pos[2]);
}
b1 = Block.Convert(level.GetTile(x, (ushort)(y + 1), z)); x = (ushort)Math.Round((decimal)(pos[0] - 16) / (decimal)32);
b2 = Block.Convert(level.GetTile(x, (ushort)(y + 2), z)); y = (ushort)((pos[1] - 64) / 32);
b3 = Block.Convert(level.GetTile(x, y, z)); z = (ushort)Math.Round((decimal)(pos[2] - 16) / (decimal)32);
/* b1 = Block.Convert(level.GetTile(x, (ushort)(y + 1), z));
b2 = Block.Convert(level.GetTile(x, (ushort)(y + 2), z));
b3 = Block.Convert(level.GetTile(x, y, z));
/*
if ((ushort)(foundPos[1] / 32) > y) { if ((ushort)(foundPos[1] / 32) > y) {
if (b1 == Block.water || b1 == Block.waterstill || b1 == Block.lava || b1 == Block.lavastill) { if (b1 == Block.water || b1 == Block.waterstill || b1 == Block.lava || b1 == Block.lavastill) {
if (Block.Walkthrough(b2)) { if (Block.Walkthrough(b2)) {
@ -372,60 +351,77 @@ 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 static void Add(PlayerBot bot) {
public void removeBot() lock (botsLock)
{ playerbots.Add(bot);
this.botTimer.Stop(); bot.GlobalSpawn();
GlobalDie();
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();
if (p.level == level)
p.SendSpawn(id, color + name, pos[0], pos[1], pos[2], rot[0], rot[1]); 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 GlobalDie() public void GlobalSpawn() {
{ Player.players.ForEach(p => {
Server.s.Log("removing " + name + " bot"); if (p.level == level)
Player.players.ForEach(delegate(Player p) p.SendSpawn(id, color + name, pos[0], pos[1], pos[2], rot[0], rot[1]);
{ });
if (p.level != level) { return; }
p.SendDespawn(id);
});
playerbots.Remove(this); //dont know if this is allowed really calling itself to kind of die
} }
public void Update() { public void GlobalDespawn() {
Player.players.ForEach(p => {
if (p.level == level)
p.SendDespawn(id);
});
} }
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);
oldpos = pos; oldrot = rot; oldpos = pos; oldrot = rot;
if (packet == null) return; if (packet == null) return;
try { try {
foreach (Player p in Player.players) { foreach (Player p in Player.players) {
if (p.level == level) if (p.level == level)
p.SendRaw(packet); p.SendRaw(packet);
} }
} catch { } } catch { }
} }
#region == Misc == #region == Misc ==
@ -437,7 +433,7 @@ namespace MCGalaxy
{ {
if (b.id == i) { goto Next; } if (b.id == i) { goto Next; }
} return i; } return i;
Next: continue; Next: continue;
} }
return 0xFF; return 0xFF;
} }
@ -449,35 +445,24 @@ namespace MCGalaxy
foreach (PlayerBot pB in PlayerBot.playerbots) { foreach (PlayerBot pB in PlayerBot.playerbots) {
if (pB.name.ToLower() == name) return pB; if (pB.name.ToLower() == name) return pB;
if (pB.name.ToLower().Contains(name)) { if (pB.name.ToLower().Contains(name)) {
match = pB; matches++; match = pB; matches++;
} }
} }
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
} }
} }