Add setting for overriding attack speed for autoattack (#1512)

* Add setting for overriding attack speed for autoattack

* Change cooldown value type to float

* Fix minor grammar issue
This commit is contained in:
ReinforceZwei 2021-03-28 19:26:38 +08:00 committed by GitHub
parent 70d3b6175b
commit 53e2413204
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 7 deletions

View file

@ -15,14 +15,15 @@ namespace MinecraftClient.ChatBots
private int attackCooldown = 6;
private int attackCooldownCounter = 6;
private Double attackSpeed = 4;
private Double attackCooldownSecond;
private Double attackCooldownSeconds;
private bool overrideAttackSpeed = false;
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)
public AutoAttack(string mode, string priority, bool overrideAttackSpeed = false, double cooldownSeconds = 1)
{
if (mode == "single")
singleMode = true;
@ -35,6 +36,20 @@ namespace MinecraftClient.ChatBots
else if (priority == "health")
priorityDistance = false;
else LogToConsoleTranslated("bot.autoAttack.priority", priority);
if (overrideAttackSpeed)
{
if (cooldownSeconds <= 0)
{
LogToConsoleTranslated("bot.autoAttack.invalidcooldown");
}
else
{
this.overrideAttackSpeed = overrideAttackSpeed;
this.attackCooldownSeconds = cooldownSeconds;
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1);
}
}
}
public override void Initialize()
@ -137,6 +152,8 @@ namespace MinecraftClient.ChatBots
public override void OnPlayerProperty(Dictionary<string, double> prop)
{
if (overrideAttackSpeed)
return;
foreach (var attackSpeedKey in new[] { "generic.attackSpeed", "minecraft:generic.attack_speed" })
{
// adjust auto attack cooldown for maximum attack damage
@ -146,8 +163,8 @@ namespace MinecraftClient.ChatBots
{
serverTPS = GetServerTPS();
attackSpeed = prop[attackSpeedKey];
attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSecond / 0.1) + 1);
attackCooldownSeconds = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1);
}
}
}
@ -155,10 +172,12 @@ namespace MinecraftClient.ChatBots
public override void OnServerTpsUpdate(double tps)
{
if (overrideAttackSpeed)
return;
serverTPS = tps;
// re-calculate attack speed
attackCooldownSecond = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSecond / 0.1) + 1);
attackCooldownSeconds = 1 / attackSpeed * (serverTPS / 20.0); // server tps will affect the cooldown
attackCooldown = Convert.ToInt32(Math.Truncate(attackCooldownSeconds / 0.1) + 1);
}
/// <summary>

View file

@ -204,7 +204,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(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority)); }
if (Settings.AutoAttack_Enabled) { BotLoad(new ChatBots.AutoAttack(Settings.AutoAttack_Mode, Settings.AutoAttack_Priority, Settings.AutoAttack_OverrideAttackSpeed, Settings.AutoAttack_CooldownSeconds)); }
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()); }

View file

@ -138,6 +138,7 @@ matchesfile=matches.ini
enabled=false
mode=single # single or multi. single target one mob per attack. multi target all mobs in range per attack
priority=distance # health or distance. Only needed when using single mode
cooldownseconds=auto # How long to wait between each attack. Use auto to let MCC calculate it
[AutoFishing]
# Entity Handling NEED to be enabled first

View file

@ -338,6 +338,7 @@ cmd.useitem.use=Used an item
# AutoAttack
bot.autoAttack.mode=Unknown attack mode: {0}. Using single mode as default.
bot.autoAttack.priority=Unknown priority: {0}. Using distance priority as default.
bot.autoAttack.invalidcooldown=Attack cooldown value cannot be smaller than 0. Using auto as default
# AutoCraft
bot.autoCraft.cmd=Auto-crafting ChatBot command

View file

@ -173,6 +173,8 @@ namespace MinecraftClient
public static bool AutoAttack_Enabled = false;
public static string AutoAttack_Mode = "single";
public static string AutoAttack_Priority = "distance";
public static bool AutoAttack_OverrideAttackSpeed = false;
public static double AutoAttack_CooldownSeconds = 1;
//Auto Fishing
public static bool AutoFishing_Enabled = false;
@ -577,6 +579,17 @@ namespace MinecraftClient
case "enabled": AutoAttack_Enabled = str2bool(argValue); break;
case "mode": AutoAttack_Mode = argValue.ToLower(); break;
case "priority": AutoAttack_Priority = argValue.ToLower(); break;
case "cooldownseconds":
if (argValue.ToLower() == "auto")
{
AutoAttack_OverrideAttackSpeed = false;
}
else
{
AutoAttack_OverrideAttackSpeed = true;
AutoAttack_CooldownSeconds = str2float(argValue);
}
break;
}
break;
@ -726,6 +739,23 @@ namespace MinecraftClient
}
}
/// <summary>
/// Convert the specified string to a float number, defaulting to zero if invalid argument
/// </summary>
/// <param name="str">String to parse as a float number</param>
/// <returns>Float number</returns>
public static float str2float(string str)
{
float f;
if (float.TryParse(str.Trim(), out f))
return f;
else
{
ConsoleIO.WriteLogLine(Translations.Get("error.setting.str2int", str));
return 0;
}
}
/// <summary>
/// Convert the specified string to a boolean value, defaulting to false if invalid argument
/// </summary>