From f77a029bbca4a92ee9ceea4efcabc2cdbbf2c617 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 17 May 2016 18:25:23 +1000 Subject: [PATCH] More modularising of botai code. --- Bots/Instructions.cs | 102 ++++++++++++++++++++++++++++++++++--------- Bots/PlayerBot.cs | 92 +++++++++++++++++++------------------- Bots/ScriptFile.cs | 2 +- 3 files changed, 128 insertions(+), 68 deletions(-) diff --git a/Bots/Instructions.cs b/Bots/Instructions.cs index 2f3a7b6b1..55d51ae95 100644 --- a/Bots/Instructions.cs +++ b/Bots/Instructions.cs @@ -28,18 +28,17 @@ namespace MCGalaxy.Bots { }; static bool DoWalk(PlayerBot bot) { - bot.foundPos[0] = bot.Waypoints[bot.currentPoint].x; - bot.foundPos[1] = bot.Waypoints[bot.currentPoint].y; - bot.foundPos[2] = bot.Waypoints[bot.currentPoint].z; + bot.foundPos[0] = bot.Waypoints[bot.cur].x; + bot.foundPos[1] = bot.Waypoints[bot.cur].y; + bot.foundPos[2] = bot.Waypoints[bot.cur].z; bot.movement = true; - if ((ushort)(bot.pos[0] / 32) == (ushort)(bot.Waypoints[bot.currentPoint].x / 32)) { - if ((ushort)(bot.pos[2] / 32) == (ushort)(bot.Waypoints[bot.currentPoint].z / 32)) { - bot.rot[0] = bot.Waypoints[bot.currentPoint].rotx; - bot.rot[1] = bot.Waypoints[bot.currentPoint].roty; + if ((ushort)(bot.pos[0] / 32) == (ushort)(bot.Waypoints[bot.cur].x / 32)) { + if ((ushort)(bot.pos[2] / 32) == (ushort)(bot.Waypoints[bot.cur].z / 32)) { + bot.rot[0] = bot.Waypoints[bot.cur].rotx; + bot.rot[1] = bot.Waypoints[bot.cur].roty; bot.movement = false; - bot.NextInstruction(); - return false; + bot.NextInstruction(); return false; } } bot.AdvanceRotation(); @@ -47,26 +46,89 @@ namespace MCGalaxy.Bots { } static bool DoTeleport(PlayerBot bot) { - bot.pos[0] = bot.Waypoints[bot.currentPoint].x; - bot.pos[1] = bot.Waypoints[bot.currentPoint].y; - bot.pos[2] = bot.Waypoints[bot.currentPoint].z; - bot.rot[0] = bot.Waypoints[bot.currentPoint].rotx; - bot.rot[1] = bot.Waypoints[bot.currentPoint].roty; + bot.pos[0] = bot.Waypoints[bot.cur].x; + bot.pos[1] = bot.Waypoints[bot.cur].y; + bot.pos[2] = bot.Waypoints[bot.cur].z; + bot.rot[0] = bot.Waypoints[bot.cur].rotx; + bot.rot[1] = bot.Waypoints[bot.cur].roty; bot.NextInstruction(); return true; } static bool DoWait(PlayerBot bot) { - if (bot.countdown != 0) { - bot.countdown--; - if (bot.countdown == 0) { - bot.NextInstruction(); - return false; + if (bot.countdown == 0) { + bot.countdown = bot.Waypoints[bot.cur].seconds; + return true; + } + + bot.countdown--; + if (bot.countdown == 0) { bot.NextInstruction(); return false; } + return true; + } + + static bool DoNod(PlayerBot bot) { + if (bot.countdown == 0) { + bot.countdown = bot.Waypoints[bot.cur].seconds; + return true; + } + bot.countdown--; + + byte speed = (byte)bot.Waypoints[bot.cur].rotspeed; + if (bot.nodUp) { + if (bot.rot[1] > 32 && bot.rot[1] < 128) { + bot.nodUp = !bot.nodUp; + } else { + if (bot.rot[1] + speed > 255) bot.rot[1] = 0; + else bot.rot[1] += speed; } } else { - bot.countdown = bot.Waypoints[bot.currentPoint].seconds; + if (bot.rot[1] > 128 && bot.rot[1] < 224) { + bot.nodUp = !bot.nodUp; + } else { + if (bot.rot[1] - speed < 0) bot.rot[1] = 255; + else bot.rot[1] -= speed; + } } + + if (bot.countdown == 0) { bot.NextInstruction(); return false; } return true; } + + static bool DoSpin(PlayerBot bot) { + if (bot.countdown == 0) { + bot.countdown = bot.Waypoints[bot.cur].seconds; + return true; + } + bot.countdown--; + + byte speed = (byte)bot.Waypoints[bot.cur].rotspeed; + if (bot.rot[0] + speed > 255) bot.rot[0] = 0; + else if (bot.rot[0] + speed < 0) bot.rot[0] = 255; + else bot.rot[0] += speed; + + if (bot.countdown == 0) { bot.NextInstruction(); return false; } + return true; + } + + static bool DoSpeed(PlayerBot bot) { + bot.movementSpeed = (int)Math.Round(24m / 100m * bot.Waypoints[bot.cur].seconds); + if (bot.movementSpeed == 0) bot.movementSpeed = 1; + bot.NextInstruction(); return true; + } + + static bool DoJump(PlayerBot bot) { + bot.jumpTimer.Elapsed += delegate { + bot.currentjump++; + switch (bot.currentjump) { + case 1: + case 2: bot.pos[1] += 24; break; + case 3: break; + case 4: bot.pos[1] -= 24; break; + case 5: bot.pos[1] -= 24; bot.jumping = false; bot.currentjump = 0; bot.jumpTimer.Stop(); break; + } + }; + bot.jumpTimer.Start(); + bot.NextInstruction(); return true; + } } } diff --git a/Bots/PlayerBot.cs b/Bots/PlayerBot.cs index 88531fded..0378e1491 100644 --- a/Bots/PlayerBot.cs +++ b/Bots/PlayerBot.cs @@ -38,7 +38,7 @@ namespace MCGalaxy { public byte id; public string color; public Level level; - public int currentPoint = 0; + public int cur = 0; public int countdown = 0; public bool nodUp = false; public List Waypoints = new List(); @@ -48,12 +48,12 @@ namespace MCGalaxy { public byte[] rot = new byte[2], oldrot = new byte[2], foundRot = new byte[2]; public bool movement = false; public int movementSpeed = 24; - bool jumping = false; - int currentjump = 0; + 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); - System.Timers.Timer jumpTimer = new System.Timers.Timer(95); + 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) { name = n; @@ -86,8 +86,7 @@ namespace MCGalaxy { bool skip = false; retry: - switch (Waypoints[currentPoint].type) - { + switch (Waypoints[cur].type) { case "walk": if (!DoWalk(ref skip)) goto retry; break; @@ -107,33 +106,32 @@ namespace MCGalaxy { if (!DoSpeed(ref skip)) goto retry; return; case "reset": - currentPoint = 0; + cur = 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); + if (File.Exists("bots/" + Waypoints[cur].newscript)) { + Command.all.Find("botset").Use(null, name + " " + Waypoints[cur].newscript); return; } - currentPoint++; - if (currentPoint == Waypoints.Count) currentPoint = 0; + cur++; + if (cur == Waypoints.Count) cur = 0; if (!skip) { skip = true; goto retry; } return; case "jump": if (!DoJump(ref skip)) goto retry; break; } - - if (currentPoint == Waypoints.Count) currentPoint = 0; + if (cur == Waypoints.Count) cur = 0; } AdvanceRotation(); } public void AdvanceRotation() { - if (!movement && Waypoints.Count > 0) { + if (!movement && Waypoints.Count > 0) { if (rot[0] < 245) rot[0] += 8; else rot[0] = 0; @@ -144,21 +142,21 @@ namespace MCGalaxy { } public void NextInstruction() { - currentPoint++; - if (currentPoint == Waypoints.Count) - currentPoint = 0; + cur++; + if (cur == Waypoints.Count) + cur = 0; } bool DoWalk(ref bool skip) { - foundPos[0] = Waypoints[currentPoint].x; - foundPos[1] = Waypoints[currentPoint].y; - foundPos[2] = Waypoints[currentPoint].z; + foundPos[0] = Waypoints[cur].x; + foundPos[1] = Waypoints[cur].y; + foundPos[2] = Waypoints[cur].z; movement = true; - if ((ushort)(pos[0] / 32) == (ushort)(Waypoints[currentPoint].x / 32)) { - if ((ushort)(pos[2] / 32) == (ushort)(Waypoints[currentPoint].z / 32)) { - rot[0] = Waypoints[currentPoint].rotx; - rot[1] = Waypoints[currentPoint].roty; + if ((ushort)(pos[0] / 32) == (ushort)(Waypoints[cur].x / 32)) { + if ((ushort)(pos[2] / 32) == (ushort)(Waypoints[cur].z / 32)) { + rot[0] = Waypoints[cur].rotx; + rot[1] = Waypoints[cur].roty; movement = false; NextInstruction(); if (!skip) { skip = true; return false; } @@ -168,11 +166,11 @@ namespace MCGalaxy { } void DoTeleport() { - pos[0] = Waypoints[currentPoint].x; - pos[1] = Waypoints[currentPoint].y; - pos[2] = Waypoints[currentPoint].z; - rot[0] = Waypoints[currentPoint].rotx; - rot[1] = Waypoints[currentPoint].roty; + pos[0] = Waypoints[cur].x; + pos[1] = Waypoints[cur].y; + pos[2] = Waypoints[cur].z; + rot[0] = Waypoints[cur].rotx; + rot[1] = Waypoints[cur].roty; NextInstruction(); } @@ -180,11 +178,11 @@ namespace MCGalaxy { if (countdown != 0) { countdown--; if (countdown == 0) { - NextInstruction(); + NextInstruction(); if (!skip) { skip = true; return false; } } } else { - countdown = Waypoints[currentPoint].seconds; + countdown = Waypoints[cur].seconds; } return true; } @@ -197,24 +195,24 @@ namespace MCGalaxy { 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; + if (rot[1] + (byte)Waypoints[cur].rotspeed > 255) rot[1] = 0; + else rot[1] += (byte)Waypoints[cur].rotspeed; } } else { if (rot[1] > 128 && rot[1] < 224) nodUp = !nodUp; else { - if (rot[1] - (byte)Waypoints[currentPoint].rotspeed < 0) rot[1] = 255; - else rot[1] -= (byte)Waypoints[currentPoint].rotspeed; + if (rot[1] - (byte)Waypoints[cur].rotspeed < 0) rot[1] = 255; + else rot[1] -= (byte)Waypoints[cur].rotspeed; } } if (countdown == 0) { - NextInstruction(); + NextInstruction(); if (!skip) { skip = true; return false; } } } else { - countdown = Waypoints[currentPoint].seconds; + countdown = Waypoints[cur].seconds; } return true; } @@ -223,22 +221,22 @@ namespace MCGalaxy { 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 (rot[0] + (byte)Waypoints[cur].rotspeed > 255) rot[0] = 0; + else if (rot[0] + (byte)Waypoints[cur].rotspeed < 0) rot[0] = 255; + else rot[0] += (byte)Waypoints[cur].rotspeed; if (countdown == 0) { - NextInstruction(); + NextInstruction(); if (!skip) { skip = true; return false; } } } else { - countdown = Waypoints[currentPoint].seconds; + countdown = Waypoints[cur].seconds; } return true; } bool DoSpeed(ref bool skip) { - movementSpeed = (int)Math.Round(24m / 100m * Waypoints[currentPoint].seconds); + movementSpeed = (int)Math.Round(24m / 100m * Waypoints[cur].seconds); if (movementSpeed == 0) movementSpeed = 1; NextInstruction(); @@ -258,7 +256,7 @@ namespace MCGalaxy { case 5: pos[1] -= 24; jumping = false; currentjump = 0; jumpTimer.Stop(); break; } }; - + jumpTimer.Start(); NextInstruction(); if (!skip) { skip = true; return false; } @@ -399,7 +397,7 @@ namespace MCGalaxy { } public static void UnloadFromLevel(Level lvl) { - BotsFile.UnloadBots(lvl); + BotsFile.UnloadBots(lvl); RemoveAll(lvl, false); } @@ -464,14 +462,14 @@ namespace MCGalaxy { foreach (PlayerBot bot in bots) { if (bot.name.ToLower() == name) return bot; if (bot.name.ToLower().Contains(name)) { - match = bot; matches++; + match = bot; matches++; } } return matches == 1 ? match : null; } public static PlayerBot FindOrShowMatches(Player pl, string name) { - int matches = 0; + int matches = 0; return Extensions.FindOrShowMatches(pl, name, out matches, Bots.Items, b => true, b => b.name, "bots"); } diff --git a/Bots/ScriptFile.cs b/Bots/ScriptFile.cs index 8fd896299..e382da524 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.currentPoint = 0; bot.countdown = 0; bot.movementSpeed = 12; + bot.cur = 0; bot.countdown = 0; bot.movementSpeed = 12; foreach (string line in codes) { if (line == "" || line[0] == '#') continue;