Guest User

Battle Stance Gadget

a guest
Jul 17th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1.  
  2. function gadget:GetInfo()
  3.   return {
  4.     name      = "battlestance",
  5.     desc      = "Toggle between Defensive, Assault and Sprint",
  6.     author    = "Zealot",
  7.     date      = "13th July 2013",
  8.     license   = "GNU LGPL, v2.1 or later",
  9.     layer     = 101,  -------------------------What does this do?
  10.     enabled   = true  --  loaded by default?
  11.   }
  12. end
  13.  
  14.  
  15. --------------------------------------------------------------------------------
  16. --------------------------------------------------------------------------------
  17.  
  18. if (not gadgetHandler:IsSyncedCode()) then
  19.   return
  20. end
  21.  
  22.  
  23. local EditUnitCmdDesc = Spring.EditUnitCmdDesc
  24. local FindUnitCmdDesc = Spring.FindUnitCmdDesc
  25. local InsertUnitCmdDesc = Spring.InsertUnitCmdDesc
  26. local GiveOrderToUnit = Spring.GiveOrderToUnit
  27. local SetUnitNeutral = Spring.SetUnitNeutral
  28. local GetUnitMoveTypeData = Spring.GetUnitMoveTypeData
  29. local SetGroundMoveTypeData = Spring.MoveCtrl.SetGroundMoveTypeData
  30. local SetUnitCloak = Spring.SetUnitCloak
  31. local SetUnitArmored = Spring.SetUnitArmored
  32. local GetUnitCmdDescs = Spring.GetUnitCmdDescs
  33.  
  34.  
  35. -- constants
  36.  
  37. local ARMOUR_DEF = 0.2
  38. local ARMOUR_ASS = 1.3
  39.  
  40.  
  41.  
  42.  
  43. -----setup command
  44.  
  45.  
  46. -----The List of units who can change their stance. (units that get this gadget)
  47.        
  48.        
  49. local STANCE = {          
  50.     [UnitDefNames["armcv"].id] = true,
  51.     [UnitDefNames["corak"].id] = true,
  52.     [UnitDefNames["armpw"].id] = true,
  53.     [UnitDefNames["armwar"].id] = true,
  54.     [UnitDefNames["armrock"].id] = true,
  55.     [UnitDefNames["corstorm"].id] = true,
  56.     [UnitDefNames["corthud"].id] = true,
  57.     [UnitDefNames["armzeus"].id] = true,
  58.     [UnitDefNames["cornecro"].id] = true,
  59.     [UnitDefNames["corpyro"].id] = true,
  60.     [UnitDefNames["armsnipe"].id] = true,
  61.     [UnitDefNames["cormort"].id] = true,
  62.    
  63. }
  64.  
  65. local stanceList = {}
  66.  
  67. local CMD_STANCE = 34583
  68.  
  69.  
  70.             ----  Create the button
  71.  
  72. local stanceCmdDesc = {
  73.     id      = CMD_STANCE,
  74.     name    = "togglestance",
  75.     action  = "togglestance",
  76.     type    = CMDTYPE.ICON_MODE,
  77.     tooltip = 'Toggle between Defensive, Assault and Sprint.',
  78.     params  = {1, "Defensive", "Assault", "Sprint"}
  79. }
  80.  
  81.  
  82.             ---- Insert the button on the units who get it
  83.  
  84.  
  85. function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
  86.     if STANCE[unitDefID] then
  87.         InsertUnitCmdDesc(unitID, 502, stanceCmdDesc)
  88.         local ms = UnitDefs[unitDefID].speed
  89.         stanceList[unitID] = {orgspeed=ms}
  90.    
  91.   end
  92. end
  93.  
  94.  
  95.             ---- Remove the button when a unit dies (as it is no longer needed)
  96.  
  97. function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  98.   stanceList[unitID] = nil
  99. end
  100.  
  101.  
  102.  
  103. -----------enable the command
  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.       --you can't edit a single value in the params table for
  159.         --editUnitCmdDesc, so we generate a new table
  160.        
  161.         local updatedCmdParams = {
  162.             cmdParams[1],
  163.             stanceCmdDesc.params[2],
  164.             stanceCmdDesc.params[3],
  165.             stanceCmdDesc.params[4]
  166.         }
  167.         Spring.EditUnitCmdDesc(unitID, cmdDescID, { params = updatedCmdParams})
  168.         return false
  169.         end
  170.         return true
  171.     end
  172.   end
Advertisement
Add Comment
Please, Sign In to add comment