Logitech Lua help needed - lua

software = Logitech G hub
UPDATE
Okay! Thanks to some help this is where I'm at, Everything's working very nicely and as it should but now I face a new task and that is adding variations of each table for the slight changes to recoil when using scopes.
I could simple add more gun modes and make the changes required but I'd really like to make a way of using a key to toggle whether they are used or not, An idea I have floating around my head is for F9 and F10 to toggle between the scopes and when the toggle is off it will work with the code I already have. however I'm not sure this is possible, to my understanding I could use the lock keys (capslock, scrolllock, numlock) to make this work. initially I had the idea to have a toggle key that modifies the timing of the recoil sequence but unfortunately this is not an accurate solution.
What I'd like to do now is create 2/3 toggles that allow gun modes to switch between variable tables without having to add more guns
any help is greatly appreciated and that goes for any help I've received so far too
thanks
---GUN MODES---
local GUN1_Mouse = 8
local GUN1_Keyboard = nil
local GUN2_Mouse = 7
local GUN2_Keyboard = nil
local GUN3_Mouse = 9
local GUN3_Keyboard = nil
local GUN4_Mouse = nil
local GUN4_Keyboard = nil
local GUN5_Mouse = 5
local GUN5_Keyboard = nil
local GUN6_Mouse = nil
local CUSTOM_Keyboard = nil
local GUN7_Mouse = 4
local GUN7_Keyboard = nil
---RECOIL TABLES---
local recoil_table = {}
-------------------------------------
recoil_table["GUN1"] = {
0, 6, 133,
-1, 6, 133,
-1, 6, 133,
-4, 6, 133,
-3, 6, 133,
-3, 6, 133,
-3, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
-4, 6, 133,
}
recoil_table["GUN1_crouch"] = {
0, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-3, 3, 133,
-3, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
}
-------------------------------------
recoil_table["GUN2"] = {
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
0, 5.5, 120,
}
recoil_table["GUN2_crouch"] = {
0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
-0, 2.8, 120,
}
-------------------------------------
recoil_table["GUN3"] = {
0, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-1, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-3, 3, 133,
-3, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
-2, 3, 133,
}
recoil_table["GUN3_crouch"] = {
0, 2.5, 125,
0, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.8, 2.5, 125,
0.9, 2.5, 125,
1, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.7, 2.5, 125,
0.8, 2.5, 125,
0.9, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
1, 2.5, 125,
}
-------------------------------------
recoil_table["GUN4"] = {
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
0, 3.4, 100,
}
recoil_table["GUN4_crouch"] = {
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
0, 1.7, 100,
}
-------------------------------------
recoil_table["GUN5"] = {
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
0, 2.4, 129,
}
recoil_table["GUN5_crouch"] = {
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
0, 1.2, 129,
}
-------------------------------------
recoil_table["GUN6"] = {
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
0, 3, 100,
}
recoil_table["GUN6_crouch"] = {
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
0, 1.5, 100,
}
-------------------------------------
recoil_table["GUN7"] = {
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
0, 4.4, 120,
}
-------------------------------------
recoil_table["GUN7_crouch"] = {
0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
-0, 2.2, 120,
}
---------------------------------------------------
local weapon
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN1_Mouse) or (event == "G_PRESSED" and arg == GUN1_Keyboard) then
weapon = weapon ~= "GUN1" and "GUN1"
if not weapon then
OutputLogMessage("OFF-GUN1\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN1\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN2_Mouse) or (event == "G_PRESSED" and arg == GUN2_Keyboard) then
weapon = weapon ~= "GUN2" and "GUN2"
if not weapon then
OutputLogMessage("OFF-GUN2\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN2\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN3_Mouse) or (event == "G_PRESSED" and arg == GUN3_Keyboard) then
weapon = weapon ~= "GUN3" and "GUN3"
if not weapon then
OutputLogMessage("OFF-GUN3\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN3\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN4_Mouse) or (event == "G_PRESSED" and arg == GUN4_Keyboard) then
weapon = weapon ~= "GUN4" and "GUN4"
if not weapon then
OutputLogMessage("OFF-GUN4\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN4\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN5_Mouse) or (event == "G_PRESSED" and arg == GUN5_Keyboard) then
weapon = weapon ~= "GUN5" and "GUN5"
if not weapon then
OutputLogMessage("OFF-GUN5\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN5\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN6_Mouse) or (event == "G_PRESSED" and arg == GUN6_Keyboard) then
weapon = weapon ~= "GUN6" and "GUN6"
if not weapon then
OutputLogMessage("OFF-GUN6\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN6\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == GUN7_Mouse) or (event == "G_PRESSED" and arg == GUN7_Keyboard) then
weapon = weapon ~= "GUN7" and "GUN7"
if not weapon then
OutputLogMessage("OFF-GUN7\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-GUN7\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
---------------------------------------------------
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and weapon and IsMouseButtonPressed(3) then
local recoil_sequence = recoil_table[weapon..(IsModifierPressed("lctrl") and "_crouch" or "")] or {}
local x, y, tm = 0, 0, GetRunningTime()
for j = 3, #recoil_sequence, 3 do
local dx_10ms = recoil_sequence[j-2]
local dy_10ms = recoil_sequence[j-1]
local duration = recoil_sequence[j]
repeat
Sleep(10)
if not IsMouseButtonPressed(1) then break end
local dt = math.min(GetRunningTime() - tm, duration)
duration, tm = duration - dt, tm + dt
x, y = x + dt/10 * dx_10ms, y + dt/10 * dy_10ms
local int_x, int_y = math.floor(x), math.floor(y)
x, y = x - int_x, y - int_y
MoveMouseRelative(int_x, int_y)
until duration == 0
if not IsMouseButtonPressed(1) then break end
end
end
end

You should use function GetRunningTime() to check if it is time to change recoil direction.
---GUN MODES---
local AK47_Mouse = 8
local AK47_Keyboard = nil
------------- for adding another weapon -----------
local AnotherWeapon_Mouse = 7
local AnotherWeapon_Keyboard = nil
---------------------------------------------------
local SpeedX2Modifier_Mouse = nil
local SpeedX2Modifier_Keyboard = 1 -- key G1
local SpeedX2 = false
---RECOIL TABLES---
local recoil_table = {}
recoil_table["AK47"] = {
-- down for 1 second
0, 0.5, 1000, -- delta_x_per_10ms, delta_y_per_10ms, duration_in_ms
-- diagonal for 1 second
-2, 4, 1000,
-- it stops moving after 2 seconds (even if LMB is still pressed)
}
recoil_table["AK47_crouch"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
recoil_table["AK47_reddot"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
------------- for adding another weapon -----------
recoil_table["AnotherWeapon"] = {
-- down for 1 second
0, 0.5, 1000, -- delta_x_per_10ms, delta_y_per_10ms, duration_in_ms
-- diagonal for 1 second
-2, 4, 1000,
-- it stops moving after 2 seconds (even if LMB is still pressed)
}
recoil_table["AnotherWeapon_crouch"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
recoil_table["AnotherWeapon_reddot"] = {
-- down forever (until LMB is released)
0, 2, math.huge, -- math.huge means "very big number of ms"
}
---------------------------------------------------
local weapon
function OnEvent(event, arg)
if event == "PROFILE_ACTIVATED" then
EnablePrimaryMouseButtonEvents(true)
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == AK47_Mouse) or (event == "G_PRESSED" and arg == AK47_Keyboard) then
weapon = weapon ~= "AK47" and "AK47"
if not weapon then
OutputLogMessage("OFF-Macro-AK47\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-NoRecoil-AK47\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
------------- for adding another weapon -----------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == AnotherWeapon_Mouse) or (event == "G_PRESSED" and arg == AnotherWeapon_Keyboard) then
weapon = weapon ~= "AnotherWeapon" and "AnotherWeapon"
if not weapon then
OutputLogMessage("OFF-Macro-AnotherWeapon\n")
if IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
else
OutputLogMessage("ON-NoRecoil-AnotherWeapon\n")
if not IsKeyLockOn("numlock") then
PressAndReleaseKey("numlock")
end
end
---------------------------------------------------
elseif (event == "MOUSE_BUTTON_PRESSED" and arg == SpeedX2Modifier_Mouse) or (event == "G_PRESSED" and arg == SpeedX2Modifier_Keyboard) then
SpeedX2 = not SpeedX2
if not SpeedX2 then
OutputLogMessage("OFF-SpeedX2\n")
else
OutputLogMessage("ON-SpeedX2\n")
end
elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and weapon and IsMouseButtonPressed(3) then
local recoil_sequence = recoil_table[weapon..(
IsKeyLockOn("capslock") and "_crouch" or
IsKeyLockOn("scrolllock") and "_reddot" or "")] or {}
local x, y, tm = 0, 0, GetRunningTime()
for j = 3, #recoil_sequence, 3 do
local dx_10ms = recoil_sequence[j-2]
local dy_10ms = recoil_sequence[j-1]
if SpeedX2 then
dx_10ms = dx_10ms * 2
dy_10ms = dy_10ms * 2
end
local duration = recoil_sequence[j]
repeat
Sleep(10)
if not IsMouseButtonPressed(1) then break end
local dt = math.min(GetRunningTime() - tm, duration)
duration, tm = duration - dt, tm + dt
x, y = x + dt/10 * dx_10ms, y + dt/10 * dy_10ms
local int_x, int_y = math.floor(x), math.floor(y)
x, y = x - int_x, y - int_y
MoveMouseRelative(int_x, int_y)
until duration == 0
if not IsMouseButtonPressed(1) then break end
end
end
end

Related

Metadict in ffmpeg does not have rotate information

My aim is to extract frames out of a video but many times the frames are inverted. This is happening because "VideoCapture" in cv2 does read the rotate flag data present in the video metadata. I am running it on mac and installed "ffmpeg" package (5.0.1) using brew package manager. This is my code to check rotation present in a video using ffmpeg.
import ffmpeg
import cv2
import pprint
pp = pprint.PrettyPrinter(depth=4)
def check_rotation(path_video_file):
# this returns meta-data of the video file in form of a dictionary
meta_dict = ffmpeg.probe(path_video_file)
pp.pprint(meta_dict)
# from the dictionary, meta_dict['streams'][0]['tags']['rotate'] is the key
# we are looking for
rotateCode = None
if int(meta_dict['streams'][0]['tags']['rotate']) == 90:
rotateCode = cv2.ROTATE_90_CLOCKWISE
elif int(meta_dict['streams'][0]['tags']['rotate']) == 180:
rotateCode = cv2.ROTATE_180
elif int(meta_dict['streams'][0]['tags']['rotate']) == 270:
rotateCode = cv2.ROTATE_90_COUNTERCLOCKWISE
return rotateCode
Here the meta_dict is supposed to have information about the rotation but it is not there.
printing out meta_dict gives:
{'format': {'bit_rate': '18498544',
'duration': '3.584000',
'filename': 'video.mp4',
'format_long_name': 'QuickTime / MOV',
'format_name': 'mov,mp4,m4a,3gp,3g2,mj2',
'nb_programs': 0,
'nb_streams': 2,
'probe_score': 100,
'size': '8287348',
'start_time': '0.000000',
'tags': {'com.android.version': '11',
'compatible_brands': 'isommp42',
'creation_time': '2021-12-08T10:56:57.000000Z',
'major_brand': 'mp42',
'minor_version': '0'}},
'streams': [{'avg_frame_rate': '990000/34997',
'bit_rate': '18804651',
'bits_per_raw_sample': '8',
'chroma_location': 'left',
'closed_captions': 0,
'codec_long_name': 'H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10',
'codec_name': 'h264',
'codec_tag': '0x31637661',
'codec_tag_string': 'avc1',
'codec_type': 'video',
'coded_height': 1080,
'coded_width': 1920,
'color_primaries': 'bt470bg',
'color_range': 'pc',
'color_space': 'bt470bg',
'color_transfer': 'smpte170m',
'display_aspect_ratio': '16:9',
'disposition': {'attached_pic': 0,
'captions': 0,
'clean_effects': 0,
'comment': 0,
'default': 1,
'dependent': 0,
'descriptions': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'metadata': 0,
'original': 0,
'still_image': 0,
'timed_thumbnails': 0,
'visual_impaired': 0},
'duration': '3.499700',
'duration_ts': 314973,
'extradata_size': 35,
'field_order': 'progressive',
'film_grain': 0,
'has_b_frames': 0,
'height': 1080,
'id': '0x1',
'index': 0,
'is_avc': 'true',
'level': 40,
'nal_length_size': '4',
'nb_frames': '99',
'pix_fmt': 'yuvj420p',
'profile': 'High',
'r_frame_rate': '120/1',
'refs': 1,
'sample_aspect_ratio': '1:1',
'side_data_list': [{...}],
'start_pts': 3168,
'start_time': '0.035200',
'tags': {'creation_time': '2021-12-08T10:56:57.000000Z',
'handler_name': 'VideoHandle',
'language': 'eng',
'vendor_id': '[0][0][0][0]'},
'time_base': '1/90000',
'width': 1920},
{'avg_frame_rate': '0/0',
'bit_rate': '128986',
'bits_per_sample': 0,
'channel_layout': 'stereo',
'channels': 2,
'codec_long_name': 'AAC (Advanced Audio Coding)',
'codec_name': 'aac',
'codec_tag': '0x6134706d',
'codec_tag_string': 'mp4a',
'codec_type': 'audio',
'disposition': {'attached_pic': 0,
'captions': 0,
'clean_effects': 0,
'comment': 0,
'default': 1,
'dependent': 0,
'descriptions': 0,
'dub': 0,
'forced': 0,
'hearing_impaired': 0,
'karaoke': 0,
'lyrics': 0,
'metadata': 0,
'original': 0,
'still_image': 0,
'timed_thumbnails': 0,
'visual_impaired': 0},
'duration': '3.584000',
'duration_ts': 172032,
'extradata_size': 2,
'id': '0x2',
'index': 1,
'nb_frames': '168',
'profile': 'LC',
'r_frame_rate': '0/0',
'sample_fmt': 'fltp',
'sample_rate': '48000',
'start_pts': 0,
'start_time': '0.000000',
'tags': {'creation_time': '2021-12-08T10:56:57.000000Z',
'handler_name': 'SoundHandle',
'language': 'eng',
'vendor_id': '[0][0][0][0]'},
'time_base': '1/48000'}]}
Can someone please help me to find if rotation is present in a video?

Implementing WeightedRandomSampler on imbalanced data set: RuntimeError: invalid multinomial distribution

I am trying to implement a weighted sampler for a very imbalanced data set. There are 182 different classes. Here is an array of the bin counts per class:
array([69487, 5770, 5753, 138, 4308, 10, 1161, 29, 5611,
350, 7, 183, 218, 4, 3, 3872, 5, 950,
33, 3, 443, 16, 20, 330, 4353, 186, 19,
122, 546, 6, 44, 6, 3561, 2186, 3, 48,
8440, 338, 9, 610, 74, 236, 160, 449, 72,
6, 37, 1729, 2255, 1392, 12, 1, 3426, 513,
44, 3, 28, 12, 9, 27, 5, 75, 15,
3, 21, 549, 7, 25, 871, 240, 128, 28,
253, 62, 55, 12, 8, 57, 16, 99, 6,
5, 150, 7, 110, 8, 2, 1296, 70, 1927,
470, 1, 1, 511, 2, 620, 946, 36, 19,
21, 39, 6, 101, 15, 7, 1, 90, 29,
40, 14, 1, 4, 330, 1099, 1248, 1146, 7414,
934, 156, 80, 755, 3, 6, 6, 9, 21,
70, 219, 3, 3, 15, 15, 12, 69, 21,
15, 3, 101, 9, 9, 11, 6, 32, 6,
32, 4422, 16282, 12408, 2959, 3352, 146, 1329, 1300,
3795, 90, 1109, 120, 48, 23, 9, 1, 6,
2, 1, 11, 5, 27, 3, 7, 1, 3,
70, 1598, 254, 90, 20, 120, 380, 230, 180,
10, 10])
In some classes, instances are as low as 1. I am trying to implement a Weighted random sampler from torch for this dataset. However, as the class imbalance is so large, when I calculate weights using
count_occr = np.bincount(dataset.y)
lbl_weights = 1. / count_occr
weights = np.array(lbl_weights)
weights = torch.from_numpy(weights)
sampler = WeightedRandomSampler(weights.type('torch.DoubleTensor'), len(weights*2))
I get two error messages:
RuntimeWarning: divide by zero encountered in true_divide
and
RuntimeError: invalid multinomial distribution (encountering probability entry = infinity or NaN)
Does anyone have a work around for this ? I was considering multiplying the lbl_weights by some scalar however I am not sure if this is a viable option.

how to convert 8-bit unsigned int data to signed?

I am getting List of 8-bit unsigned int from a mic source for each sample rate which looks like this
[61, 251, 199, 251, 56, 252, 138, 252, 211, 252, 18, 253, 91, 253, 194, 253, 25, 254, 54, 254, 19, 254, 190, 253, 80, 253, 249, 252, 233, 252, 46, 253, 180, 253, 54, 254, 136, 254, 157, 254, 110, 254, 38, 254, 208, 253, 117, 253, 68, 253, 57, 253, 83, 253, 163, 253, 20, 254, 151, 254, 51, 255, 215, 255, 105, 0, 207, 0, 246, 0, 249, 0, 10, 1, 64, 1, 162, 1, 4, 2, 64, 2, 97, 2, 111, 2, 110, 2, 89, 2, 40, 2, 241, 1, 199, 1, 178, 1, 192, 1, 241, 1, 45, 2, 77, 2, 70, 2, 45, 2, 36, 2, 83, 2, 176, 2, 21, 3, 121, 3, 229, 3, 87, 4, 185, 4, 225, 4, 197, 4, 129, 4, 26, 4, 150, 3, 7, 3, 128, 2, 55, 2, 65, 2, 134, 2, 223, 2, 25, 3, 41, 3, 28, 3, 255, 2, 234, 2, 240, 2, 25, 3, 62, 3, 92, 3, 146, 3, 219, 3, 65, 4, 149, 4, 164, 4, 130, 4, 51, 4, 195, 3, 69, 3, 164, 2, 244, 1, 75, 1, 187, 0, 81, 0, 240, 255, 135, 255, 19, 255, 155, 254, 64, 254, 22, 254, 58, 254, 146, 254, 217, 254, 248, 254, 215, 254, 144, 254, 92, 254, 84, 254, 141, 254, 229, 254, 39, 255, 96, 255, 170, 255, 248, 255, 69, 0, 117, 0, 128, 0, 137, 0, 131, 0,
so how can I convert this into signed decimal value or someone can guide me to the right path
That depends on what the bytes mean.
Looking at the bytes, every other byte is either very low or very high. That suggests to me that the bytes are really little-endian signed 16-bit values.
In that case, you just need to view them as such. If we assume that the platform is little-endian (most are), you can just do:
List<int> list = ...;
Uint8List bytes = Uint8List.fromList(list); //
Int16List words = Int16List.sublistView(bytes);
Then the words list contains signed 16-bit numbers.
(If the list is already a Uint8List, you can skip the first step.)
If that's not what the bytes mean, you'll have to figure out what they do mean.
Dart int type provide a method to convert from signed to unsigned and from unsigned to signed.
For example:
int a = 16;
int b = 239;
print(a.toSigned(5).toString()); // Print -16
print(b.toSigned(5).toString()); // Print 15
the toSigned method parameter indicate the bit order of the sign bit.
You can get more information here: https://api.flutter.dev/flutter/dart-core/int/toSigned.html
A toUnsigned method exixts too: https://api.flutter.dev/flutter/dart-core/int/toUnsigned.html

Elixir: what is the binary printed in the repl after module definition?

I wrote a simple module in the repl:
iex(26)> defmodule Adder do
...(26)> def add(a \\ 0, b \\ 0) do
...(26)> a+b
...(26)> end
...(26)> end
{:module, Adder,
<<70, 79, 82, 49, 0, 0, 4, 224, 66, 69, 65, 77, 65, 116, 111, 109, 0, 0, 0, 106, 0, 0, 0, 12, 12, 69, 108, 105, 120, 105, 114, 46, 65, 100, 100, 101, 114, 8, 95, 95, 105, 110, 102, 111, 95, 95, 4, 100, 111, 99, ...>>,
{:add, 2}}
What is the binary printed out after the module definition?
That's the BEAM bytecode output as a raw binary. It's used by the compiler to persist the compiled module to a Elixir.Adder.beam file when compiling via mix. Modules defined in iex aren't ever persisted to disk, but you can think of it as confirmation that your module was successfully compiled in memory.

gawk: change 5th element on every other line

I want to add a line break (\n) in front of the 5th element on every other line:
2, 0, 0, 0, 2
4, 0, 0, 0, 4
6, 0, 0, 0, 6
8, 0, 0, 0, 8
... in order to get:
2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8
What I have so far in gawk doesn't work:
gawk '{if (NR % 2) {$5=\n$5; print} else print}'
You could say:
awk '{NR%2 || $5="\\n"$5 }1' filename
Note that you'll need to escape the \ in order to get a literal \.
For your input, it'd produce:
2, 0, 0, 0, 2
4, 0, 0, 0, \n4
6, 0, 0, 0, 6
8, 0, 0, 0, \n8
Alternatively, (as pointed out by #WilliamPursell), you could say:
awk '!(NR%2) {$5="\\n"$5 }1' filename

Resources