Guest User

unit script for unit that switches weapons

a guest
Mar 21st, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.16 KB | None | 0 0
  1. local torso = piece "torso"
  2. local rFoot = piece "rFoot"
  3. local lFoot = piece "lFoot"
  4. local turret = piece "lHand"
  5. local rHand = piece "rHand"
  6. local head = piece "head"
  7. local flare = piece "flare"
  8.  
  9. local SIG_WALK = 1
  10. local SIG_AIM = 2
  11. local SIG_AIM_2 = 4
  12. local SIG_AIM_3 = 8
  13.  
  14. local RESTORE_DELAY = Spring.UnitScript.GetLongestReloadTime(unitID)*2
  15.  
  16. local leg_movespeed = 20
  17. local leg_movedistance = 10
  18. local waterGunE = false
  19.  
  20. local hulaHoopP = false
  21. local magicWandP = false
  22. local waterGunP = false
  23. local bbGloveP = false
  24. local capeP = false
  25. local swimTrunksP = false
  26. local runningShoesP = false
  27.  
  28. local waterGunAllowed = false
  29. local waterGunVal = 0
  30. local activeWeapon = 1
  31. local AP = 12
  32.  
  33. local ename1
  34. local eid1
  35. local wID
  36.  
  37. local unitName
  38. local unitDefID
  39.  
  40. --FLARE POSITIONING --
  41. local fguny = 8
  42. local fgunz = 39
  43.  
  44. function script.Create()
  45.  
  46.     Sleep(50)
  47.    
  48.     Spring.SetUnitRulesParam(unitID, "bbGlove", 0, {public = true})
  49.     Spring.SetUnitRulesParam(unitID, "cape", 0, {public = true})
  50.     Spring.SetUnitRulesParam(unitID, "hulaHoop", 0, {public = true})
  51.     Spring.SetUnitRulesParam(unitID, "magicWand", 0, {public = true})
  52.     Spring.SetUnitRulesParam(unitID, "swimTrunks", 0, {public = true})
  53.     Spring.SetUnitRulesParam(unitID, "waterGun", 0, {public = true})
  54.     Spring.SetUnitRulesParam(unitID, "runningShoes", 0, {public = true})
  55.    
  56.     local ux, uy, uz = Spring.GetUnitPosition(unitID)
  57.     local idleTransports = Spring.GetUnitsInCylinder(ux, uy, 2000)
  58.  
  59.     local enemyID = Spring.ENEMY_UNITS
  60.     local enemyunits = Spring.GetUnitsInCylinder(ux, uy, 2000, enemyID)
  61.    
  62.     eid1 = idleTransports[2]
  63.    
  64.     unitDefID = Spring.GetUnitDefID(unitID)
  65.     unitName = UnitDefs[unitDefID].name
  66.     if(eid1 ~= nil) then
  67.         local enemyID1 = Spring.GetUnitDefID(eid1)
  68.         ename1 = UnitDefs[enemyID1].name
  69.         if(ename1 == "Jenny") then
  70.             AP = AP-2
  71.         end
  72.     end
  73.     Spring.SetUnitRulesParam(unitID, "maxAP", AP, {public = true})
  74. end
  75.  
  76. local function Equip()
  77.         if waterGunAllowed == true then
  78.             Spring.UnitScript.Move( flare, y_axis, fguny, 999)
  79.             Spring.UnitScript.Move( flare, z_axis, fgunz, 999)
  80.         end
  81.        
  82. end
  83.  
  84. local function walk()
  85.     Signal(SIG_WALK)
  86.     SetSignalMask(SIG_WALK)
  87.     while (true) do
  88.         Move(lFoot, y_axis, leg_movedistance, leg_movespeed)
  89.         Move(rFoot, y_axis, 0, leg_movespeed)
  90.         WaitForMove(lFoot, y_axis)
  91.         WaitForMove(rFoot, y_axis)
  92.         Sleep(50)
  93.         Move(lFoot, y_axis, 0, leg_movespeed)
  94.         Move(rFoot, y_axis, leg_movedistance, leg_movespeed)
  95.         WaitForMove(lFoot, y_axis)
  96.         WaitForMove(rFoot, y_axis)
  97.        
  98.         local ucx, ucy, ucz = Spring.GetUnitPosition(unitID)
  99.         if(ename1 == "dana") then
  100.             local dcx, dcy, dcz = Spring.GetUnitPosition(eid1)
  101.             local distance = math.sqrt((dcx-ucx)^2 + (dcz-ucz)^2)
  102.             --Spring.Echo(distance)
  103.             if(distance < 200) then
  104.                 --Spring.Echo("STOPPPPP!!!")
  105.                 Spring.GiveOrderToUnit(unitID, CMD.STOP, {}, {})
  106.                 script.StopMoving()
  107.             end
  108.         end
  109.        
  110.         if(ename1 == "timmy") then
  111.             local ecx, ecy, ecz = Spring.GetUnitPosition(eid1)
  112.             local distance = math.sqrt((ecx-ucx)^2 + (ecz-ucz)^2)
  113.            
  114.             if(distance < 200) then
  115.                
  116.                 Spring.SetUnitCloak(eid1, false)
  117.                
  118.             end
  119.         end
  120.         Sleep(50)
  121.     end
  122. end
  123.  
  124. local function legs_down()
  125.         Move(lFoot, y_axis, 0, leg_movespeed)
  126.         Move(rFoot, y_axis, 0, leg_movespeed)
  127. end
  128.  
  129. local function RestoreAfterDelay(unitID)
  130.    
  131.     Sleep(RESTORE_DELAY)
  132.     if(waterGunAllowed == true) then
  133.     Turn(turret, y_axis, 0, math.rad(90))
  134.    
  135.     Turn(torso, y_axis, 0, math.rad(90))
  136.     Turn(head, y_axis, 0, math.rad(90))
  137.     Turn(rHand, y_axis, 0, math.rad(90))
  138.     Turn(rFoot, y_axis, 0, math.rad(90))
  139.     Turn(lFoot, y_axis, 0, math.rad(90))
  140.     else
  141.     Move(lHand, z_axis, 0, 20)
  142.     end
  143. end
  144.  
  145. function script.QueryWeapon3()
  146.     return rHand
  147.  
  148. end
  149.  
  150. function script.AimFromWeapon3()
  151.    
  152.         return rHand
  153.  
  154. end
  155.  
  156. function script.AimWeapon3(heading, pitch)
  157.    
  158.     Spring.Echo("aim weapon 2!")
  159.     Signal(SIG_AIM_3)
  160.     SetSignalMask(SIG_AIM_3)
  161.    
  162.    
  163.    
  164.    
  165.     Move(rHand, z_axis, 15, 20)
  166.     StartThread(RestoreAfterDelay)
  167.     return true
  168.    
  169. end
  170.  
  171. function script.FireWeapon3()
  172.     Spring.Echo("fire2!")
  173. end
  174.  
  175. function script.QueryWeapon1()
  176.    
  177.     return flare
  178.    
  179. end
  180.  
  181. function script.AimFromWeapon1()
  182.    
  183.     return flare
  184.    
  185. end
  186.  
  187. function SwitchWeapon()
  188.     if (waterGunP == true) then
  189.         weaponChosen = 0
  190.     elseif (magicWandP == true) then
  191.         weaponChosen = 1
  192.     end
  193. end
  194.        
  195.  
  196. function script.AimWeapon1(heading, pitch)--(weaponID, heading, pitch)
  197.    
  198.     Spring.Echo("heyyyyy")
  199.    
  200.     Signal(SIG_AIM_2)
  201.     SetSignalMask(SIG_AIM_2)
  202.     while(weaponChosen) do
  203.         Sleep(100)
  204.     end
  205.     Spring.Echo("aiming when I shouldn't")
  206.    
  207.     Turn(turret, y_axis, heading, math.rad(90))
  208.     --Turn(gun, x_axis, -pitch, math.rad(90))
  209.     --Turn(waterGunVal, x_axis, -pitch, math.rad(90))
  210.     Turn(torso, y_axis, heading, math.rad(90))
  211.     Turn(head, y_axis, heading, math.rad(90))
  212.     Turn(rHand, y_axis, heading, math.rad(90))
  213.     Turn(rFoot, y_axis, heading, math.rad(90))
  214.     Turn(lFoot, y_axis, heading, math.rad(90))
  215.     WaitForTurn(turret, y_axis)
  216.     --WaitForTurn(waterGunVal, x_axis)
  217.     --WaitForTurn(gun, x_axis)
  218.     WaitForTurn(head, y_axis)
  219.     WaitForTurn(rHand, y_axis)
  220.     WaitForTurn(rFoot, y_axis)
  221.     WaitForTurn(lFoot, y_axis)
  222.     StartThread(RestoreAfterDelay)
  223.  
  224.     StartThread(RestoreAfterDelay)
  225.     return true
  226.  
  227. end
  228.  
  229. function script.FireWeapon1()
  230.     Spring.Echo("fire!")
  231. end
  232.  
  233.  
  234. function script.HitByWeapon(x, z, weaponDefID, damage)
  235.     wname = WeaponDefs[weaponDefID].name
  236.     i, j = string.find(wname, "fist")
  237.     if(hulaHoopP == true) then
  238.             newDamage = damage - (0.75*damage)
  239.             return newDamage
  240.     end
  241. end
  242.  
  243. function script.StartMoving()
  244.     StartThread(walk)
  245. end
  246.  
  247.  
  248. function script.StopMoving()
  249.     Signal(SIG_WALK)
  250.     legs_down()
  251. end
  252.  
  253. function script.TransportDrop(passengerID)
  254. end
  255.  
  256. function script.TransportPickup (passengerID)
  257.     Spring.UnitScript.AttachUnit(turret, passengerID)
  258.     local unitDefID = Spring.GetUnitDefID(passengerID)
  259.     wID = passengerID
  260.     local name = UnitDefs[unitDefID].name
  261.     if (name == "wwatergun") then
  262.         waterGunAllowed = true
  263.         Equip()
  264.         waterGunP = true
  265.         Spring.SetUnitRulesParam(unitID, "waterGun", 1, {public = true})
  266.     end
  267.     if(name == "whulahoop") then
  268.         hulaHoopP = true
  269.         -- CHANGE THE UNIT'S HEALTH --
  270.         local _, currentMaxHealth, _, _, _= Spring.GetUnitHealth(unitID)
  271.         currentMaxHealth = currentMaxHealth+20
  272.         Spring.SetUnitMaxHealth(unitID, currentMaxHealth)
  273.         Spring.SetUnitHealth(unitID, currentMaxHealth)
  274.        
  275.         Spring.SetUnitRulesParam(unitID, "hulaHoop", 1, {public = true})
  276.     end
  277.     if(name == "wbbglove") then
  278.         bbGloveP = true
  279.         Spring.SetUnitRulesParam(unitID, "bbGlove", 1, {public = true})
  280.         Spring.UnitScript.SetPieceVisibility(rHand, false)
  281.     end
  282.     if(name == "wmagicwand") then
  283.         magicWandP = true
  284.         Spring.SetUnitRulesParam(unitID, "magicWand", 1, {public = true})
  285.     end
  286.     if(name == "wcape") then
  287.         capeP = true
  288.         Spring.SetUnitRulesParam(unitID, "cape", 1, {public = true})
  289.         UnitDefs[unitName].cruisealt = 4.0
  290.     end
  291.     if(name == "wswimTrunks") then
  292.         swimTrunksP = true
  293.         Spring.SetUnitRulesParam(unitID, "swimTrunks", 1, {public = true})
  294.     end
  295.     if(name == "wrunningShoes") then
  296.         runningShoesP = true
  297.         Spring.SetUnitRulesParam(unitID, "runningShoes", 1, {public = true})
  298.     end
  299.     SwitchWeapon()
  300. end
  301.  
  302. function script.Killed(recentDamage, maxHealth)
  303.    
  304.     --return 0
  305. end
Advertisement
Add Comment
Please, Sign In to add comment