Logitech Lua help needed

Viewed 643

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
1 Answers

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