Created
May 22, 2021 23:17
-
-
Save MajsterTynek/3fcc5f5607b073102ff27fa7909f7d3a to your computer and use it in GitHub Desktop.
HorseAutoGallop - requires AdvancedMacros 7.10.0wd_pre136 from WD_fix fork build. (Find it in AM's #creations or `gradlew build` it yourself from my repo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- bind to keypress MOSE:4 | |
-- halts movement till sound comes back | |
-- prevents kicking caused by outrunning server | |
-- basically waits for getting sound packets back | |
local player, horse, last_gallop, action, x, y, z, a, b, c, hi, lo, last | |
local unpack, format, sqrt = table.unpack, string.format, math.sqrt | |
local waitTick, runOnMC, getPlayer = waitTick, runOnMC, getPlayer | |
local logdel, gal = logdel, 420780262 | |
local notify = hud2D.actionbar | |
player = getPlayer() | |
horse = player.entityRiding | |
if not horse then | |
logdel(gal,"&4[G] Gallop has no horse") | |
return | |
end | |
if gallop_loop then | |
gallop_loop = false | |
logdel(gal,"&6[G] Gallop utility disabled") | |
else | |
gallop_loop = true | |
logdel(gal,"&6[G] Gallop utility enabled") | |
if velo_inlo then return end | |
end | |
hi, lo = 0, math.huge | |
local minmax = function(d) | |
if hi < d then hi = d end | |
if lo > d then lo = d end | |
return d | |
end | |
local distance = function(snd, hors) | |
local a, b, c = unpack(snd.pos) | |
local x, y, z = unpack(hors.pos) | |
a, b = x - a, z - c -- horizontal | |
-- log("dst: ", math.abs(a), " ", math.abs(b)) | |
return math.sqrt(a*a + b*b) | |
end | |
local update = function() | |
-- check if we are still a rider | |
player = getPlayer() | |
horse = player.entityRiding | |
if not horse then | |
gallop_loop = false | |
logdel(gal+1,"&4[G] Gallop has no horse") | |
return | |
end | |
-- initialise movement | |
if not action then | |
action = true | |
forward(-1) | |
end | |
-- check first sound | |
local gallop = gallop | |
if not gallop then | |
-- velocity not reached | |
-- or loop not initialised | |
return | |
end | |
-- display debug info in actionbar | |
local d = distance(gallop, horse) -- current distance | |
local d_, hi_ = format("%.3f",d), format("%.3f",hi) | |
notify("&6Distance: "..format("%.3f",last).. " &7["..hi_.."]") | |
-- notify("&6Distance: "..d_.." &7["..hi_.."]") -- too frequent | |
if last_gallop then -- if loop is ready | |
-- update distance range | |
if gallop ~= last_gallop then | |
last = minmax(distance(last_gallop,gallop)) | |
end | |
-- check for latest sound update | |
if gallop == last_gallop and hi ~= 0 then | |
-- no sound update was there | |
--if d > 1.4142 * hi then | |
--if d > 2.236 * hi then | |
--if d > 13 then | |
if d > 16 then -- this value is tweakable | |
logdel(gal+1,"&4[G] LAGSPIKE? Reached: "..d_.."m") | |
notify("&4Distance: "..d_.." &7["..hi_.."]") | |
playSound("pop.wav").loop(1) | |
action = false | |
gallop = false | |
_G.gallop = false | |
gallop_await = true | |
last_gallop = false | |
forward(0) | |
end | |
return | |
end | |
end | |
last_gallop = gallop | |
end | |
-- state machine | |
last = 0 | |
action = false | |
gallop = false | |
_G.gallop = false | |
gallop_await = false | |
last_gallop = false | |
-- execution loop | |
local wasWaiting = false | |
while gallop_loop do | |
-- check if we are still a rider | |
player = getPlayer() | |
horse = player.entityRiding | |
if not horse then | |
gallop_loop = false | |
logdel(gal+1,"&4[G] Gallop has no horse") | |
break | |
end | |
if not gallop_await then | |
if wasWaiting then | |
sleep(1000) | |
wasWaiting = false | |
end | |
runOnMC(update) | |
else | |
wasWaiting = gallop_await | |
end | |
waitTick() | |
end | |
forward(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- bind to keypress MOUSE:3 | |
-- autowalks again if you unpress it by accident | |
local player = getPlayer() | |
local horse = player.entityRiding | |
if horse then | |
forward(-1) | |
logdel(420780264) -- delete message by unique ID, if exists | |
return | |
else | |
logdel(420780264,"&4You are not riding anything!") | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- bind to event Sound | |
-- other script part uses global variable | |
-- `gallop` sourced from this execution | |
local _, _, name, sound, control = ... | |
-- sound.category == "neutral" and | |
if name == "minecraft:entity.horse.gallop" then | |
gallop = sound -- table describing sound | |
gallop.time = os.millis() -- current time | |
gallop_await = false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment