Advertisement
Guest User

Untitled

a guest
Oct 20th, 2011
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.81 KB | None | 0 0
  1. --the amazing dropship factory by knorke
  2. local testing = false
  3. local pad = piece "bauplattform"
  4. local nano = piece "flare2"
  5. local geruest = piece "geruest"
  6. local radar = piece "radart2"
  7.  
  8. local transporter = piece "transporter"
  9. local antrieb = piece "antrieb"
  10. local turm = piece "turm"
  11. local waffe = piece "waffe"
  12.  
  13. local storedUnits = {}
  14. --storedUnits[1] = "dgheavytank"
  15. --storedUnits[2] = "dgbike"
  16. --storedUnits[3] = "dgheavytank"
  17.  
  18. local maxUnits = 8
  19. local unloadRadius = 90
  20. local busy = false
  21.  
  22. function script.Create(unitID) 
  23.     if (testing) then Spring.Echo ("spaceport here!") end
  24.     Hide (geruest)
  25.     Move (transporter, y_axis, 1000)
  26.     Move (pad, y_axis, -50) --move build pad underground but hovercrafts ignore it so that is useless
  27.     hideTransporter()
  28.     if (testing) then StartThread (loop) end
  29. end
  30.  
  31. function script.QueryBuildInfo() return pad end
  32.  
  33. --no nano bubbling
  34. --function script.QueryNanoPiece() return nano end
  35.  
  36. function script.Activate()
  37.     SetUnitValue(COB.YARD_OPEN, 1)
  38.     SetUnitValue(COB.INBUILDSTANCE, 1)
  39.     SetUnitValue(COB.BUGGER_OFF, 1)
  40.     return 1
  41. end
  42.  
  43. function script.Deactivate()   
  44.     SetUnitValue(COB.YARD_OPEN, 0)
  45.     SetUnitValue(COB.INBUILDSTANCE, 0)
  46.     SetUnitValue(COB.BUGGER_OFF, 0)
  47.     return 0
  48. end
  49.  
  50. function loop () --for testing
  51.     while (true) do
  52.         Spring.Echo ("que:" .. #Spring.GetFullBuildQueue(unitID) .. "  #storedUnits:" .. #storedUnits)
  53.         Sleep (1000)
  54.     end
  55. end
  56.  
  57. function script.StartBuilding()
  58.     bID = Spring.GetUnitIsBuilding (unitID)
  59.     hideUnitPieces (bID)
  60.     Spring.SetUnitNoSelect (bID, true)
  61.     --local x,y,z = Spring.GetUnitPosition (unitID)
  62.     --Spring.SetUnitPosition (bID, x,100,z)
  63. end
  64.  
  65. function script.StopBuilding() 
  66.     local _,  _,  _, _,  buildprogress = Spring.GetUnitHealth (bID)
  67.     if (buildprogress==1) then
  68.         storeUnit (unitName (bID))
  69.         Spring.DestroyUnit (bID,false,true)
  70.     end
  71.     if (not busy) then StartThread (shouldWeDeliver) end    --seems not to work without this. maybe buildque only gets cleared once StopBuilding returns?
  72. end
  73.  
  74. function shouldWeDeliver ()
  75.     Sleep (500)
  76.     if (#storedUnits >= maxUnits or (#Spring.GetFullBuildQueue(unitID)==0 and #storedUnits >0)) then deliverUnits () end   
  77. end
  78.  
  79. function unitName (uID)
  80.     if (not Spring.ValidUnitID (uID)) then return "invalid unitID in unitName()" end
  81.     local udID =Spring.GetUnitDefID(uID)
  82.     local uDef = UnitDefs [udID]
  83.     return uDef.name
  84. end
  85.  
  86. function deliverUnits ()
  87.     busy = true
  88.     while (#storedUnits >= maxUnits or (#Spring.GetFullBuildQueue(unitID)==0 and #storedUnits >0)) do
  89.         if (#storedUnits == 0) then return end
  90.         showTransporter()
  91.         Move (transporter, y_axis, 0, 100)
  92.         Spin (radar, y_axis, 2)
  93.         WaitForMove (transporter, y_axis)
  94.         local x,y,z = Spring.GetUnitPosition (unitID)
  95.         local teamID = Spring.GetUnitTeam (unitID)
  96.         if (testing) then Spring.Echo ("#storedUnits:" .. #storedUnits) end
  97.         local n = #storedUnits
  98.         if (n > maxUnits) then n = maxUnits end
  99.         local step = (math.pi*2)/n
  100.         for i=1, n do
  101.             dx = x + math.sin (step*i)*unloadRadius
  102.             dz = z + math.cos (step*i)*unloadRadius
  103.             local newUnit = Spring.CreateUnit (storedUnits[i], dx,y,dz, 0, teamID)
  104.             copyMoveOrders (unitID, newUnit)
  105.             if (testing) then Spring.Echo (i .. " deliver:" .. storedUnits[i]) end
  106.         end
  107.         local sUold = #storedUnits
  108.         for i=sUold,1,-1  do
  109.             if (i <= n) then
  110.                 storedUnits[i] = storedUnits[i+n]
  111.                 storedUnits[i+n] = nil
  112.             end
  113.         end
  114.         --storedUnits = {}     
  115.         Move (transporter, y_axis, 1000, 100)
  116.         WaitForMove (transporter, y_axis)
  117.         hideTransporter()
  118.         StopSpin (radar, y_axis,1)
  119.     end
  120.     busy = false
  121. end
  122.  
  123. function storeUnit (unitname)
  124.     storedUnits[#storedUnits+1] = unitname
  125. end
  126.  
  127. ---------------------------------
  128. function script.Killed(recentDamage, maxHealth)
  129.     if (busy) then explodeTransporter() end
  130.     return 0
  131. end
  132.  
  133. function explodeTransporter ()
  134.     Explode (transporter, SFX.FIRE)
  135.     Explode (waffe, SFX.FIRE)
  136.     Explode (turm, SFX.FIRE)
  137.     Explode (antrieb, SFX.SHATTER)
  138. end
  139.  
  140. function hideTransporter()
  141.     Hide (transporter)
  142.     Hide (antrieb)
  143.     Hide (turm)
  144.     Hide (waffe)   
  145. end
  146.  
  147. function showTransporter()
  148.     Show (transporter)
  149.     Show (antrieb)
  150.     Show (turm)
  151.     Show (waffe)
  152. end
  153.  
  154. --make a unit invisible by hiding all its pieces...lol
  155. function hideUnitPieces (uID)
  156.     local allpieces = Spring.GetUnitPieceList (uID)
  157.     local piece_n = table.getn (allpieces)
  158.     --Spring.Echo ("unit has " .. piece_n  .. " pieces")
  159.     for pID=1, piece_n,1  do       
  160.         --Spring.Echo ("pID:" .. pID)      
  161.         Spring.UnitScript.CallAsUnit(uID, Spring.UnitScript.Hide, pID)
  162.     end
  163. end
  164.  
  165. function copyMoveOrders (unitA, unitB)
  166.   local commands = Spring.GetUnitCommands(unitA)
  167.   for i = 1, #commands do
  168.     local cmd = commands[i]
  169.     if (cmd.id == CMD.MOVE  or cmd.id == CMD.FIGHT or cmd.id == CMD.PATROL) then    --only copy some commands
  170.         Spring.GiveOrderToUnit(unitB, cmd.id, cmd.params, cmd.options.coded)
  171.     end
  172.   end
  173. end
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement