Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2011
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.84 KB | None | 0 0
  1. function gadget:GetInfo()
  2.     return {
  3.         name = "Dropship Unload Effect",
  4.         desc = "Shows a dropship that unloads the constructed unit",
  5.         author = "KDR_11k (David Becker)",
  6.         date = "2008-10-29",
  7.         license = "Public Domain",
  8.         layer = 1,
  9.         enabled = true
  10.     }
  11. end
  12.  
  13. if (gadgetHandler:IsSyncedCode()) then
  14.  
  15. --SYNCED
  16.  
  17. local LSspeedZ=.5
  18. local LSspeedY=-.95
  19.  
  20. local APPROACH=1
  21. local BRAKE=2
  22. local LAND=3
  23. local LANDED=4
  24. local RAISE=5
  25. local FLYOFF=6
  26.  
  27. local landFrames=100
  28.  
  29. local dropshipTypes, dropshipUnits = include("gamedata/LuaConfigs/dropship_defs.lua")
  30.  
  31. local function LandStraight(dropship,f)
  32.     local u=dropship.unit
  33.     local con=dropship.controls
  34.     if con.ls==APPROACH then
  35.         if f > con.brakeFrame then
  36.             con.ls=BRAKE
  37.         end
  38.     elseif con.ls == BRAKE then
  39.         if con.speed > 0.01 then
  40.             con.speed=con.speed - .3
  41.             Spring.MoveCtrl.SetRelativeVelocity(u,0,LSspeedY*con.speed,LSspeedZ*con.speed)
  42.         else
  43.             con.ls=LAND
  44.             con.speed=0
  45.             local headdiff = con.ang - con.thead
  46.             if headdiff > 3.14159 then
  47.                 headdiff = headdiff - 6.28318
  48.             elseif headdiff < -3.14159 then
  49.                 headdiff = headdiff + 6.28318
  50.             end
  51.             con.headdiff=headdiff/landFrames
  52.             con.landframe=f + landFrames
  53.             Spring.CallCOBScript(u,"Landing",0)
  54.             Spring.MoveCtrl.SetRelativeVelocity(u,0,-1,0)
  55.         end
  56.     elseif con.ls == LAND then
  57.         con.ang = con.ang - con.headdiff
  58.         Spring.MoveCtrl.SetRotation(u,0,con.ang,0)
  59.         if con.landframe<=f then
  60.             con.ls=LANDED
  61.             Spring.MoveCtrl.SetRelativeVelocity(u,0,0,0)
  62.         end
  63.     elseif con.ls == LANDED then
  64.         --nothing
  65.     else
  66.         local pos = dropship.pos
  67.         Spring.MoveCtrl.Enable(u)
  68.         local thead = (dropship.heading / 32756 * 3.14159)
  69.         local ang = math.random(-100000,100000)/100000 + thead
  70.         local dist = 1200
  71.         Spring.MoveCtrl.SetPosition(u, pos[1]-math.sin(ang)*dist, pos[2]+2400, pos[3]-math.cos(ang)*dist)
  72.         Spring.MoveCtrl.SetRotation(u,0,ang,0)
  73.         con.ang=ang
  74.         con.speed=dropship.speed
  75.         con.thead=thead
  76.         Spring.MoveCtrl.SetRelativeVelocity(u,0,LSspeedY*con.speed,LSspeedZ*con.speed)
  77.         con.brakeFrame=(dropship.frames+f)
  78.         con.ls=APPROACH
  79.     end
  80. end
  81.  
  82. local function TakeoffVert(dropship,f)
  83.     local u=dropship.unit
  84.     local con=dropship.controls
  85.     if con.ts==RAISE then
  86.         if con.forward then
  87.             Spring.MoveCtrl.SetRelativeVelocity(u,0,0 - LSspeedY*con.speed,0 - LSspeedZ*con.speed)
  88.             if con.speed >0 then
  89.                 con.forward=false
  90.             end
  91.         else
  92.             Spring.MoveCtrl.SetRelativeVelocity(u,0,con.speed,0)
  93.         end
  94.         if con.speed < 50 then
  95.             con.speed = con.speed +.4
  96.         else
  97.             con.ts=FLYOFF
  98.             con.flyoffFrame=f+100
  99.         end
  100.     elseif con.ts==FLYOFF then
  101.         if con.flyoffFrame < f then
  102.             Spring.DestroyUnit(u,false,true)
  103.         end
  104.     elseif con.ls then
  105.         if con.ls == LANDED or con.ls==LAND then
  106.             Spring.CallCOBScript(u,"Takeoff",0)
  107.         end
  108.         if con.speed > 0 then
  109.             con.speed = - con.speed
  110.             if con.ls == APPROACH or con.ls==BRAKING then
  111.                 con.forward=true
  112.             end
  113.         end
  114.         con.ls=nil
  115.         con.ts=RAISE
  116.     end
  117. end
  118.  
  119. local dropFunction = {
  120.     ["LandStraight"] = LandStraight,
  121.     ["TakeoffVert"] = TakeoffVert,
  122. }
  123. local useDropship={}
  124. local dropshipWait={}
  125. local dropship={}
  126.  
  127. function gadget:UnitCreated(u, ud, team, builder)
  128.     if builder then
  129.         if useDropship[ud] then
  130.             local bud = Spring.GetUnitDefID(builder)
  131.             local ds = useDropship[ud]
  132.             local x,y,z=Spring.GetUnitPosition(u)
  133.             local h = Spring.GetUnitHeading(u)
  134.             local approachTime = dropshipUnits[UnitDefs[ud].name].approachTime or ds.approachTime
  135.             local atBuild=1 - (approachTime / UnitDefs[ud].buildTime * UnitDefs[bud].buildSpeed) --Build completion at which the dropship will spawn
  136.             dropshipWait[u]={
  137.                 ds=ds,
  138.                 pos={x,y,z},
  139.                 heading=h,
  140.                 atBuild=atBuild,
  141.                 ud=ud,
  142.                 team=team,
  143.                 speed=dropshipUnits[UnitDefs[ud].name].speed,
  144.                 frames=dropshipUnits[UnitDefs[ud].name].frames,
  145.                 buildProgress=0,
  146.             }
  147.             Spring.SetUnitNoDraw(u,true)
  148.         end
  149.     end
  150. end
  151.  
  152. function gadget:AllowCommand(u,ud,team,cmd,param,opt)
  153.     if dropship[u] then
  154.         return false
  155.     end
  156.     return true
  157. end
  158.  
  159. local function EndDropshipWait(u)
  160.     Spring.SetUnitNoDraw(u,false)
  161.     local d = dropshipWait[u]
  162.     if d.dropship then
  163.         local dr = dropship[d.dropship]
  164.         if dr then
  165.             dr.controlFunc=dropFunction[dr.ds.startFunc]
  166.         end
  167.     end
  168.     dropshipWait[u]=nil
  169. end
  170.  
  171. function gadget:UnitFinished(u, ud, team, builder)
  172.     if dropshipWait[u] then
  173.         if dropshipUnits[UnitDefs[ud].name].landceg then
  174.             local x,y,z=Spring.GetUnitBasePosition(u)
  175.             Spring.SpawnCEG(dropshipUnits[UnitDefs[ud].name].landceg, x, y, z, 0, 1.0, 0,0,0)
  176.         end
  177.         EndDropshipWait(u)
  178.     end
  179. end
  180.  
  181. function gadget:UnitDestroyed(u,ud,team)
  182.     if dropshipWait[u] then
  183.         EndDropshipWait(u)
  184.     end
  185.     if dropship[u] then
  186.         dropship[u]=nil
  187.     end
  188. end
  189.  
  190. function gadget:GameFrame(f)
  191.     if f%16 < .01 then
  192.         for tu,d in pairs(dropshipWait) do
  193.             if not d.dropship then
  194.                 local _,_,_,_,b = Spring.GetUnitHealth(tu) --build progress
  195.                 dropshipWait[tu].buildProgress = b
  196.                 if b >= d.atBuild then
  197.                     local pos = d.pos
  198.                     nu = Spring.CreateUnit(d.ds.unitname,pos[1],pos[2],pos[3],0,d.team)
  199.                     local dr = {
  200.                         unit=nu,
  201.                         ds=d.ds,
  202.                         pos=pos,
  203.                         heading=d.heading,
  204.                         controlFunc=dropFunction[d.ds.landFunc],
  205.                         controls={},
  206.                         targetUnit=tu,
  207.                         speed=d.speed,
  208.                         frames=d.frames,
  209.                     }
  210.                     Spring.SetUnitNeutral(nu,true)
  211.                     Spring.SetUnitNoSelect(nu,true)
  212.                     dropship[nu]=dr
  213.                     --dr.controlFunc(dr,f)
  214.                     d.dropship=nu
  215.                 end
  216.             end
  217.         end
  218.     end
  219.     for u,d in pairs(dropship) do
  220.         d.controlFunc(d,f)
  221.     end
  222. end
  223.  
  224. function gadget:Initialize()
  225.     for unitname,d in pairs(dropshipUnits) do
  226.         if dropshipTypes[d.dropship] then
  227.             useDropship[d.id]=dropshipTypes[d.dropship]
  228.         elseif d.dropship then
  229.             Spring.Echo ("Unrecognized dropship type '"..d.dropship.."' on unit "..unitname)
  230.         end
  231.     end
  232.     _G.dropshipWait=dropshipWait
  233. end
  234.  
  235. else
  236.  
  237. --UNSYNCED
  238.  
  239. local rerolled=0
  240. local flicker=1
  241. local offset=0
  242.  
  243. function gadget:DrawWorld()
  244.     local ateam = Spring.GetLocalAllyTeamID()
  245.     offset=offset+.001
  246.     if rerolled > 5 then
  247.         if math.random(10)>8 then
  248.             flicker=true
  249.         else
  250.             flicker=false
  251.         end
  252.         rerolled=0
  253.     end
  254.     rerolled=rerolled+1
  255.     --gl.MultiTexGen(1,GL.S,GL.TEXTURE_GEN_MODE,GL.EYE_LINEAR)
  256.     gl.MultiTexGen(1,GL.T,GL.TEXTURE_GEN_MODE,GL.EYE_LINEAR)
  257.     gl.MultiTexGen(1,GL.T,GL.EYE_PLANE,0,.1,0,offset)
  258.     gl.Blending(GL.ONE,GL.ONE)
  259.     gl.DepthTest(GL.LEQUAL)
  260.     gl.Texture(1,":a:LuaRules/Images/interference.png")
  261.     for u,d in spairs(SYNCED.dropshipWait) do
  262.         local bp = math.min(d.buildProgress + 0.15,1)
  263.         if flicker then
  264.             gl.Color(bp*0.7,bp*0.7,bp*0.7,bp*0.7)
  265.         else
  266.             gl.Color(bp,bp,bp,bp)
  267.         end
  268.         local pos = d.pos
  269.         gl.Texture(0,"%"..d.ud..":0")
  270.         gl.Unit(u,true,0)
  271.     end
  272.     gl.Blending(GL.SRC_ALPHA,GL.ONE_MINUS_SRC_ALPHA)
  273.     gl.Color(1,1,1,1)
  274.     gl.Texture(0,false)
  275.     gl.Texture(1,false)
  276.     gl.MultiTexGen(1,GL.S,false)
  277.     gl.MultiTexGen(1,GL.T,false)
  278.     gl.DepthTest(false)
  279. end
  280.  
  281. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement