From e71d0e23831b0138c05c725511323239c95c48bc Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Tue, 18 Aug 2020 19:02:36 +0800 Subject: [PATCH] Improve AutoAttack (#1218) * Improve AutoAttack - New attack mode - Fix abnormally low attack damage * Add document for AutoCraft * Improve code style and user feedback * Correct spelling mistakes --- MinecraftClient/ChatBots/AutoAttack.cs | 72 ++++++++++++++++++++++---- MinecraftClient/McClient.cs | 2 +- MinecraftClient/Settings.cs | 6 +++ MinecraftClient/config/README.md | 64 ++++++++++++++++++++++- 4 files changed, 133 insertions(+), 11 deletions(-) diff --git a/MinecraftClient/ChatBots/AutoAttack.cs b/MinecraftClient/ChatBots/AutoAttack.cs index 559bc13a..39b5c724 100644 --- a/MinecraftClient/ChatBots/AutoAttack.cs +++ b/MinecraftClient/ChatBots/AutoAttack.cs @@ -19,6 +19,23 @@ namespace MinecraftClient.ChatBots private int attackRange = 4; private Double serverTPS; private float health = 100; + private bool singleMode = true; + private bool priorityDistance = true; + + public AutoAttack(string mode, string priority) + { + if (mode == "single") + singleMode = true; + else if (mode == "multi") + singleMode = false; + else LogToConsole("Unknown attack mode: " + mode + ". Using single mode as default."); + + if (priority == "distance") + priorityDistance = true; + else if (priority == "health") + priorityDistance = false; + else LogToConsole("Unknown priority: " + priority + ". Using distance priority as default."); + } public override void Initialize() { @@ -40,15 +57,52 @@ namespace MinecraftClient.ChatBots attackCooldownCounter = attackCooldown; if (entitiesToAttack.Count > 0) { - SendAnimation(Inventory.Hand.MainHand); // Arm animation - foreach (KeyValuePair entity in entitiesToAttack) + if (singleMode) { - // check that we are in range once again. - bool shouldAttack = handleEntity(entity.Value); - if (shouldAttack) + int priorityEntity = 0; + if (priorityDistance) // closest distance priority { - InteractEntity(entity.Key, 1); // hit the entity! + double distance = 5; + foreach (var entity in entitiesToAttack) + { + var tmp = GetCurrentLocation().Distance(entity.Value.Location); + if (tmp < distance) + { + priorityEntity = entity.Key; + distance = tmp; + } + } } + else // low health priority + { + float health = int.MaxValue; + foreach (var entity in entitiesToAttack) + { + if (entity.Value.Health < health) + { + priorityEntity = entity.Key; + health = entity.Value.Health; + } + } + } + // check entity distance and health again + if (shouldAttackEntity(entitiesToAttack[priorityEntity])) + { + InteractEntity(priorityEntity, 1); // hit the entity! + SendAnimation(Inventory.Hand.MainHand); // Arm animation + } + } + else + { + foreach (KeyValuePair entity in entitiesToAttack) + { + // check that we are in range once again. + if (shouldAttackEntity(entity.Value)) + { + InteractEntity(entity.Key, 1); // hit the entity! + } + } + SendAnimation(Inventory.Hand.MainHand); // Arm animation } } } @@ -60,7 +114,7 @@ namespace MinecraftClient.ChatBots public override void OnEntitySpawn(Entity entity) { - handleEntity(entity); + shouldAttackEntity(entity); } public override void OnEntityDespawn(Entity entity) @@ -73,7 +127,7 @@ namespace MinecraftClient.ChatBots public override void OnEntityMove(Entity entity) { - handleEntity(entity); + shouldAttackEntity(entity); } public override void OnHealthUpdate(float health, int food) @@ -113,7 +167,7 @@ namespace MinecraftClient.ChatBots /// /// The entity to handle /// If the entity should be attacked - public bool handleEntity(Entity entity) + public bool shouldAttackEntity(Entity entity) { if (!entity.Type.IsHostile() || entity.Health <= 0) return false; diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index dc284fba..4ec942ce 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -179,7 +179,7 @@ namespace MinecraftClient if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ExpandVars(Settings.ScriptScheduler_TasksFile))); } if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); } if (Settings.AutoRespond_Enabled) { BotLoad(new ChatBots.AutoRespond(Settings.AutoRespond_Matches)); } - if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack()); } + if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority)); } if (Settings.AutoFishing_Enabled) { BotLoad(new ChatBots.AutoFishing()); } if (Settings.AutoEat_Enabled) { BotLoad(new ChatBots.AutoEat(Settings.AutoEat_hungerThreshold)); } if (Settings.Mailer_Enabled) { BotLoad(new ChatBots.Mailer()); } diff --git a/MinecraftClient/Settings.cs b/MinecraftClient/Settings.cs index 013e9a21..00a8842c 100644 --- a/MinecraftClient/Settings.cs +++ b/MinecraftClient/Settings.cs @@ -155,6 +155,8 @@ namespace MinecraftClient //Auto Attack public static bool AutoAttack_Enabled = false; + public static string AutoAttack_Mode = "single"; + public static string AutoAttack_Priority = "distance"; //Auto Fishing public static bool AutoFishing_Enabled = false; @@ -498,6 +500,8 @@ namespace MinecraftClient switch (argName.ToLower()) { case "enabled": AutoAttack_Enabled = str2bool(argValue); break; + case "mode": AutoAttack_Mode = argValue.ToLower(); break; + case "priority": AutoAttack_Priority = argValue.ToLower(); break; } break; @@ -745,6 +749,8 @@ namespace MinecraftClient + "[AutoAttack]\r\n" + "# Entity Handling NEED to be enabled first\r\n" + "enabled=false\r\n" + + "mode=single # single or multi. single target one mob per attack. multi target all mobs in range per attack\r\n" + + "priority=distance # health or distance. Only needed when using single mode\r\n" + "\r\n" + "[AutoFishing]\r\n" + "# Entity Handling NEED to be enabled first\r\n" diff --git a/MinecraftClient/config/README.md b/MinecraftClient/config/README.md index eef76b79..e6a0ec27 100644 --- a/MinecraftClient/config/README.md +++ b/MinecraftClient/config/README.md @@ -61,7 +61,7 @@ In scripts and remote control, no slash is needed to perform the command. - quit or exit: disconnect from the server and close the application - reco [account] : disconnect and reconnect to the server - connect [account] : go to the given server and resume the script - - script