Make bot movement not use 90% cpu on mono and also make it consistent movement speed regardless of position sending interval, partially addresses #205.

This commit is contained in:
UnknownShadow200 2016-07-30 16:54:18 +10:00
parent 036e758e38
commit 5e4a189511
3 changed files with 16 additions and 24 deletions

View File

@ -114,7 +114,7 @@ namespace MCGalaxy.Bots {
}
static bool DoSpeed(PlayerBot bot) {
bot.movementSpeed = (int)Math.Round(24m / 100m * bot.Waypoints[bot.cur].seconds);
bot.movementSpeed = (int)Math.Round(3m / 100m * bot.Waypoints[bot.cur].seconds);
if (bot.movementSpeed == 0) bot.movementSpeed = 1;
bot.NextInstruction(); return false;
}

View File

@ -47,12 +47,11 @@ namespace MCGalaxy {
public ushort[] pos = new ushort[3], oldpos = new ushort[3], foundPos = new ushort[3];
public byte[] rot = new byte[2], oldrot = new byte[2], foundRot = new byte[2];
public bool movement = false;
public int movementSpeed = 24;
public int movementSpeed = 3;
internal bool jumping = false;
internal int currentjump = 0;
System.Timers.Timer botTimer = new System.Timers.Timer(100);
System.Timers.Timer moveTimer = new System.Timers.Timer(100 / 24);
internal System.Timers.Timer jumpTimer = new System.Timers.Timer(95);
public PlayerBot(string n, Level lvl, ushort x, ushort y, ushort z, byte rotx, byte roty) {
@ -67,8 +66,6 @@ namespace MCGalaxy {
botTimer.Elapsed += BotTimerFunc;
botTimer.Start();
moveTimer.Elapsed += MoveTimerFunc;
moveTimer.Start();
}
#region Script handling
@ -112,10 +109,7 @@ namespace MCGalaxy {
if (cur == Waypoints.Count) cur = 0;
}
void MoveTimerFunc(object sender, ElapsedEventArgs e) {
moveTimer.Interval = Server.updateTimer.Interval / movementSpeed;
if (!movement) return;
void DoMove() {
if ((pos[1] - 19) % 32 != 0 && !jumping)
pos[1] = (ushort)((pos[1] + 19) - (pos[1] % 32));
@ -198,7 +192,6 @@ namespace MCGalaxy {
bot.GlobalDespawn();
bot.botTimer.Stop();
bot.moveTimer.Stop();
bot.jumpTimer.Stop();
if (save)
BotsFile.RemoveBot(bot);
@ -236,9 +229,12 @@ namespace MCGalaxy {
}
}
public void Update() { }
void UpdatePosition() {
if (movement) {
for (int i = 0; i < movementSpeed; i++)
DoMove();
}
byte[] packet = Entities.GetPositionPacket(this);
oldpos = pos; oldrot = rot;
if (packet == null) return;
@ -248,13 +244,10 @@ namespace MCGalaxy {
if (p.level == level) p.SendRaw(packet);
}
#region == Misc ==
static byte FreeId()
{
for (byte i = 127; i >= 64; i--)
{
foreach (PlayerBot b in playerbots)
{
static byte FreeId() {
for (byte i = 127; i >= 64; i--) {
foreach (PlayerBot b in playerbots) {
if (b.id == i) { goto Next; }
} return i;
Next: continue;
@ -281,7 +274,6 @@ namespace MCGalaxy {
return Extensions.FindMatches<PlayerBot>(pl, name, out matches, Bots.Items,
b => true, b => b.name, "bots");
}
#endregion
public static void GlobalUpdatePosition() {
PlayerBot[] bots = Bots.Items;

View File

@ -29,7 +29,7 @@ namespace MCGalaxy.Bots {
PlayerBot.Pos newPos = new PlayerBot.Pos();
try { bot.Waypoints.Clear(); } catch { }
bot.cur = 0; bot.countdown = 0; bot.movementSpeed = 12;
bot.cur = 0; bot.countdown = 0; bot.movementSpeed = 3;
foreach (string line in codes) {
if (line == "" || line[0] == '#') continue;