From 5e4a189511b4657d4072bd52f12d7e8d93b93d89 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 30 Jul 2016 16:54:18 +1000 Subject: [PATCH] Make bot movement not use 90% cpu on mono and also make it consistent movement speed regardless of position sending interval, partially addresses #205. --- Bots/Instructions.cs | 2 +- Bots/PlayerBot.cs | 36 ++++++++++++++---------------------- Bots/ScriptFile.cs | 2 +- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/Bots/Instructions.cs b/Bots/Instructions.cs index 86a529c28..19b90d5cb 100644 --- a/Bots/Instructions.cs +++ b/Bots/Instructions.cs @@ -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; } diff --git a/Bots/PlayerBot.cs b/Bots/PlayerBot.cs index 4befe05fd..d82fbfe92 100644 --- a/Bots/PlayerBot.cs +++ b/Bots/PlayerBot.cs @@ -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 @@ -81,9 +78,9 @@ namespace MCGalaxy { if (hunt) Instructions.DoHunt(this); } else { bool doNextInstruction = !DoInstruction(); - if (cur == Waypoints.Count) cur = 0; + if (cur == Waypoints.Count) cur = 0; - if (!doNextInstruction) return; + if (!doNextInstruction) return; DoInstruction(); if (cur == Waypoints.Count) cur = 0; } @@ -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)); @@ -144,7 +138,7 @@ namespace MCGalaxy { } else if (Block.Walkthrough(b1) && Block.Walkthrough(b2)) { pos[0] += (ushort)dx; // Stay on current level pos[2] += (ushort)dz; - } else if (Block.Walkthrough(b) && Block.Walkthrough(b1)) { + } else if (Block.Walkthrough(b) && Block.Walkthrough(b1)) { pos[0] += (ushort)dx; // Drop a level pos[1] -= (ushort)32; pos[2] += (ushort)dz; @@ -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(pl, name, out matches, Bots.Items, b => true, b => b.name, "bots"); } - #endregion public static void GlobalUpdatePosition() { PlayerBot[] bots = Bots.Items; diff --git a/Bots/ScriptFile.cs b/Bots/ScriptFile.cs index 30acb11b1..ba0e8df9b 100644 --- a/Bots/ScriptFile.cs +++ b/Bots/ScriptFile.cs @@ -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;