Guest User

keypresscode

a guest
Jul 18th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.70 KB | None | 0 0
  1. function gadget:GetInfo()
  2.   return {
  3.     name      = "battlestance",
  4.     desc      = "Toggle between Defensive, Assault and Sprint",
  5.     author    = "Zealot",
  6.     date      = "13th July 2013",
  7.     license   = "GNU LGPL, v2.1 or later",
  8.     layer     = 101,  -------------------------What does this do?
  9.     enabled   = true  --  loaded by default?
  10.   }
  11. end
  12.  
  13.  
  14. --------------------------------------------------------------------------------
  15. --------------------------------------------------------------------------------
  16.  
  17. if (not gadgetHandler:IsSyncedCode()) then
  18.   return
  19. end
  20.  
  21.  
  22. local EditUnitCmdDesc = Spring.EditUnitCmdDesc
  23. local FindUnitCmdDesc = Spring.FindUnitCmdDesc
  24. local InsertUnitCmdDesc = Spring.InsertUnitCmdDesc
  25. local GiveOrderToUnit = Spring.GiveOrderToUnit
  26. local SetUnitNeutral = Spring.SetUnitNeutral
  27. local GetUnitMoveTypeData = Spring.GetUnitMoveTypeData
  28. local SetGroundMoveTypeData = Spring.MoveCtrl.SetGroundMoveTypeData
  29. local SetUnitCloak = Spring.SetUnitCloak
  30. local SetUnitArmored = Spring.SetUnitArmored
  31. local GetUnitCmdDescs = Spring.GetUnitCmdDescs
  32.  
  33.  
  34. -- constants
  35.  
  36. local ARMOUR_DEF = 0.2
  37. local ARMOUR_ASS = 1.3
  38.  
  39.  
  40.  
  41.  
  42. -----setup command
  43.  
  44.  
  45. -----The List of units who can change their stance. (units that get this gadget)
  46.        
  47.        
  48. local STANCE = {          
  49.     [UnitDefNames["armcv"].id] = true,
  50.     [UnitDefNames["corak"].id] = true,
  51.     [UnitDefNames["armpw"].id] = true,
  52.     [UnitDefNames["armwar"].id] = true,
  53.     [UnitDefNames["armrock"].id] = true,
  54.     [UnitDefNames["corstorm"].id] = true,
  55.     [UnitDefNames["corthud"].id] = true,
  56.     [UnitDefNames["armzeus"].id] = true,
  57.     [UnitDefNames["cornecro"].id] = true,
  58.     [UnitDefNames["corpyro"].id] = true,
  59.     [UnitDefNames["armsnipe"].id] = true,
  60.     [UnitDefNames["cormort"].id] = true,
  61.    
  62. }
  63.  
  64. local stanceList = {}
  65.  
  66. local CMD_STANCE = 34583
  67.  
  68.  
  69.             ----  Create the button
  70.  
  71. local stanceCmdDesc = {
  72.     id      = CMD_STANCE,
  73.     name    = "togglestance",
  74.     action  = "togglestance",
  75.     type    = CMDTYPE.ICON_MODE,
  76.     tooltip = 'Toggle between Defensive, Assault and Sprint.',
  77.     params  = {1, "Defensive", "Assault", "Sprint"}
  78. }
  79.  
  80.  
  81.             ---- Insert the button on the units who get it
  82.  
  83.  
  84. function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
  85.     if STANCE[unitDefID] then
  86.         InsertUnitCmdDesc(unitID, 502, stanceCmdDesc)
  87.         local ms = UnitDefs[unitDefID].speed
  88.         stanceList[unitID] = {orgspeed=ms}
  89.    
  90.   end
  91. end
  92.  
  93.  
  94.             ---- Remove the button when a unit dies (as it is no longer needed)
  95.  
  96. function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  97.   stanceList[unitID] = nil
  98. end
  99.  
  100.  
  101.  
  102. -----------enable the command
  103.  
  104.  
  105.  
  106.  
  107.  
  108. function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
  109. if STANCE[unitDefID] then
  110.     if cmdID == CMD_STANCE then
  111.             local cmdDescID = FindUnitCmdDesc(unitID, CMD_STANCE)
  112.             --Spring.Echo("cmdparams[1]=" .. tostring(cmdParams[1]))
  113.             if not cmdDescID then return false end
  114.        
  115.            
  116.              if  cmdParams[1] == 1 then  --Assault stance
  117.                
  118.                     --units decloak range to 250
  119.                     SetUnitCloak(unitID, true, 250)
  120.                    
  121.                      --unit receives normal damage as in unitdef
  122.                     SetUnitArmored(unitID, false)  
  123.                    
  124.                     --unit moves at normal speed as in unitdef
  125.                     SetGroundMoveTypeData(unitID, {
  126.                     maxSpeed=stanceList[unitID].orgspeed * 1})  
  127.                    
  128.                
  129.        
  130.             elseif
  131.                      cmdParams[1] == 2 then    --Sprint stance
  132.                    
  133.                     --set units decloak range to 750
  134.                     SetUnitCloak(unitID, true, 750)    
  135.                    
  136.                     --multiply damage received by ARMOUR_ASS amount (defined above)
  137.                     SetUnitArmored(unitID, true, ARMOUR_ASS)  
  138.                     --unit moves at 1.6x unitdef speed
  139.                     SetGroundMoveTypeData(unitID, {maxSpeed=stanceList[unitID].orgspeed * 1.6 })
  140.                    
  141.                      
  142.                  
  143.             elseif
  144.                      cmdParams[1] == 0 then   -- Defensive stance
  145.                
  146.                
  147.                     --set units decloak range to 125
  148.                     SetUnitCloak(unitID, true, 125)  
  149.                    
  150.                     --multiply damage received by ARMOUR_DEF amount (defined above)
  151.                     SetUnitArmored(unitID, true, ARMOUR_DEF)
  152.                    
  153.                     --unit moves at 1/3 unitdef speed
  154.                     SetGroundMoveTypeData(unitID, {maxSpeed=stanceList[unitID].orgspeed / 3 })
  155.                    
  156.             end
  157.            
  158.        
  159.       --you can't edit a single value in the params table for
  160.         --editUnitCmdDesc, so we generate a new table
  161.        
  162.         local updatedCmdParams = {
  163.             cmdParams[1],
  164.             stanceCmdDesc.params[2],
  165.             stanceCmdDesc.params[3],
  166.             stanceCmdDesc.params[4]
  167.         }
  168.         Spring.EditUnitCmdDesc(unitID, cmdDescID, { params = updatedCmdParams})
  169.         return false
  170.         end
  171.         return true
  172.     end
  173.    
  174. end
  175.  
  176.  
  177.  
  178.    -------------------------------         
  179.    ---------------Should I put code here?-----------------     
  180.    ------------------------
  181.    ----------------Keypress callin:
  182.    ----------KeyPress() --> "key, mods, isRepeat", where key = keymap, mods = SHIFT, etc.
  183.  ----------------------------------
  184.  
  185.   function Gadget:KeyPress(y,false--[[is this correct? how to write no mods ie not shift or ctrl]], false --[[what does this mean?]])
  186.     if unitselected --[[what is the callin for this]] then   -----select
  187.         if STANCE[unitDefID] then  ---if the selected unit is one of the units in the list of units that have the cmd desc button.
  188.             local cmdDescID = FindUnitCmdDesc(unitID, CMD_STANCE)
  189.             cmdDescID.cmdParams[1] = 1 -----set the button state to Assault
  190.         end
  191.     end
  192.    -----------------------------------         
  193.    
  194.  
  195.    
  196.   end
Advertisement
Add Comment
Please, Sign In to add comment