Advertisement
Guest User

GHD cl_main.lua

a guest
Jan 1st, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.30 KB | None | 0 0
  1. local GH = GravHull
  2. include('sh_codetools.lua')
  3. GH.SHIPCONTENTS = {}
  4. GH.GHOSTHULLS = {}
  5. ------------------------------------------------------------------------------------------
  6. -- Name: AddHullIds
  7. -- Desc: Called when a hull usermessage is recieved; waits until the entity indices are valid
  8. ------------------------------------------------------------------------------------------
  9. local function AddHullIds(tab,enti,shipi,ghosti,times)
  10.     local ent,ship,ghost = Entity(enti),Entity(shipi),Entity(ghosti)
  11.     if !(ValidEntity(ent) && ValidEntity(ship) && ValidEntity(ghost)) then
  12.         if (times < 50) then
  13.             timer.Simple(0.1,AddHullIds,tab,enti,shipi,ghosti,times+1)
  14.         end
  15.         return
  16.     end
  17.     tab[ent] = {S = ship, G = ghost}
  18.     ent.Ghost = ghost
  19.     ghost.Hull = ent
  20. end
  21. local draw_with_effect = kv_swap{
  22.     "player",
  23.     "npc_grenade_frag",
  24.     "crossbow_bolt",
  25.     "prop_combine_ball",
  26.     "rpg_missile",
  27.     "gmod_cameraprop",
  28.     "gmod_turret",
  29. }
  30. ------------------------------------------------------------------------------------------
  31. -- Name: AddPropIds
  32. -- Desc: Called when a contents usermessage is recieved; waits until the entity indices are valid
  33. ------------------------------------------------------------------------------------------
  34. local function AddPropIds(tab,enti,shipi,ghosti,times)
  35.     local ent,ship,ghost = Entity(enti),Entity(shipi),Entity(ghosti)
  36.     if !(ValidEntity(ent) && ValidEntity(ship) && ValidEntity(ghost)) then
  37.         if (times < 50) then
  38.             timer.Simple(0.1,AddPropIds,tab,enti,shipi,ghosti,times+1)
  39.         end
  40.         return
  41.     end
  42.     if (ent == LocalPlayer()) then
  43.         ent.RollCorrection = true
  44.     end
  45.     tab[ent] = {S = ship, G = ghost}
  46.     if ent == ship then ent.Ghost = ghost end
  47.     if (draw_with_effect[ent:GetClass()] or ent:IsVehicle() or (ent:GetMoveType() != MOVETYPE_NONE && (ent:GetBoneCount()||0) > 1)) then
  48.         local ed = EffectData()
  49.         ed:SetEntity(ent)
  50.         ent.OldColor = {ent:GetColor()}
  51.         ent:SetColor(0,0,0,0)
  52.         ent.WasHidden = true
  53.         util.Effect("player_rotate",ed)
  54.     end
  55. end
  56. ------------------------------------------------------------------------------------------
  57. -- Name: sl_ship_object
  58. -- Desc: The hull/contents usermessage, basically gives the client data about ships
  59. ------------------------------------------------------------------------------------------
  60. usermessage.Hook("sl_ship_object",function(um) --this is how the server tells us which entities are "contained"
  61.     local enti,yes,hull = um:ReadShort(),um:ReadBool(),um:ReadBool()
  62.     local tab = GH.SHIPCONTENTS
  63.     if hull then tab = GH.GHOSTHULLS end
  64.     if yes and hull then
  65.         local shipi,ghosti = um:ReadShort(),um:ReadShort()
  66.         AddHullIds(tab,enti,shipi,ghosti,0)
  67.     elseif yes then
  68.         local shipi,ghosti = um:ReadShort(),um:ReadShort()
  69.         AddPropIds(tab,enti,shipi,ghosti,0)
  70.     else
  71.         local ent = Entity(enti)
  72.         if ValidEntity(ent) then
  73.             if ent.RealColor or ent.WasHidden then
  74.                 ent:SetColor(unpack(ent.RealColor or {255,255,255,255}))
  75.                 if ({ent:GetColor()})[4] == 0 then ent:SetColor(255,255,255,255) end
  76.             end
  77.             if (ent == LocalPlayer()) then
  78.                 ent.RollCorrection = true
  79.             end
  80.             tab[ent] = nil
  81.         end
  82.     end
  83. end)
  84. ------------------------------------------------------------------------------------------
  85. -- Name: sl_ship_explosion
  86. -- Desc: The explosion usermessage, used
  87. ------------------------------------------------------------------------------------------
  88. usermessage.Hook("sl_ship_explosion",function(um)
  89.     local ed = EffectData()
  90.     local pos = um:ReadVector()
  91.     ed:SetStart(pos)
  92.     ed:SetOrigin(pos)
  93.     util.Effect("Explosion",ed)
  94.     /* DECALS DON'T WORK YET
  95.     local tr = util.RealTraceLine{ent:GetPos(), endpos = ent:GetPos() - Vector(0,0,ent:BoundingRadius()), filter = ent}
  96.     if (tr.Hit) then
  97.         util.Decal("Scorch",tr.HitPos+tr.HitNormal,tr.HitPos-tr.HitNormal)
  98.     end*/
  99. end)
  100. ------------------------------------------------------------------------------------------
  101. -- Name: sl_antiteleport_cl
  102. -- Desc: Clientside portion of the anti-teleport handshake
  103. ------------------------------------------------------------------------------------------
  104. usermessage.Hook("sl_antiteleport_cl",function(um) --recieved when the player's position is set by the server, used to avoid SetPos packet loss resulting in teleporting to the sky
  105.     local ppos = um:ReadVector()
  106.     if (LocalPlayer():GetRealPos():Distance(ppos) > 3000) then
  107.         RunConsoleCommand("sl_antiteleport")
  108.     end
  109. end)
  110. local RenderViewing = false
  111. /*hook.Add("PreDrawOpaqueRenderables","SLShipStart",function()
  112.     if RenderViewing then return end
  113.     local ply = LocalPlayer()
  114.     local data = GH.SHIPCONTENTS[ply]
  115.     if data then
  116.         local pos,ang = EyePos(),EyeAngles()
  117.         pos = data.S:LocalToWorld(data.G:WorldToLocal(pos))
  118.         ang = data.S:LocalToWorldAngles(data.G:WorldToLocalAngles(ang))
  119.         RenderViewing = true
  120.         render.RenderView{
  121.             origin = pos,
  122.             angles = ang,
  123.             x = 0, y = 0,
  124.             w = ScrW(),
  125.             h = ScrH(),
  126.             drawviewmodel = false
  127.         }
  128.         RenderViewing = false
  129.     end
  130. end)*/
  131. ------------------------------------------------------------------------------------------
  132. -- Name: SLBindPress
  133. -- Desc: Used for overriding binds so they send an extra console command making them work in ships
  134. ------------------------------------------------------------------------------------------
  135. hook.Add("PlayerBindPress","SLBindPress",function(ply,str,down)
  136.     if string.find(str,"+use") && GH.SHIPCONTENTS[ply] && !(ply:KeyDown(IN_ATTACK) and ValidEntity(ply:GetActiveWeapon()) and ply:GetActiveWeapon():GetClass()=="weapon_physgun") then
  137.         RunConsoleCommand("sl_external_use")
  138.     end
  139. end)
  140. local cvt = CreateClientConVar("ghd_cameramethod",0,true,false)
  141. ------------------------------------------------------------------------------------------
  142. -- Name: DoCalcView
  143. -- Desc: The magic calcview hook.
  144. ------------------------------------------------------------------------------------------
  145. GH.DoCalcView = function(ply,pos,ang,fov,nope)
  146.     local apply = false
  147.     local method = cvt:GetInt()
  148.     local view
  149.     if method == 0 then
  150.         if nope == "SLShipView" then return end
  151.         view = hook.Call("CalcView",GAMEMODE,ply,pos,ang,fov,"SLShipView")
  152.     elseif method == 1 then
  153.         view = {origin = pos, angles = ang}
  154.     end
  155.     xpcall(function()
  156.         if ply.RollCorrection then
  157.             local pa = ply:EyeAngles()
  158.             if pa.r > 0 then
  159.                 pa.r = pa.r * 0.9
  160.                 if (pa.r < 1) then pa.r = pa.r * 0.5 end
  161.                 if (pa.r < 0.1) then pa.r = 0 end
  162.             elseif pa.r < 0 then
  163.                 pa.r = pa.r * 0.9
  164.                 if (pa.r > -1) then pa.r = pa.r * 0.5 end
  165.                 if (pa.r > -0.1) then pa.r = 0 end
  166.             end
  167.             if pa.r == 0 then ply.RollCorrection = nil end
  168.             ply:SetEyeAngles(pa)
  169.             ply.LastCorrection = CurTime()
  170.         end
  171.         local data = GH.SHIPCONTENTS[ply]
  172.         if ply:InVehicle() && ValidEntity(ply:GetVehicle()) then
  173.             data = GH.SHIPCONTENTS[ply:GetVehicle()]
  174.         end
  175.         if ValidEntity(SLViewEnt) then
  176.             data = GH.SHIPCONTENTS[SLViewEnt]
  177.         end
  178.         if data && ValidEntity(data.S) && ValidEntity(data.G) then
  179.             local tpos,tang = WorldToLocal(view.origin,view.angles,data.G.RealPos or data.G:GetRealPos(),data.G.RealAng or data.G:GetRealAngles())
  180.             view.origin,view.angles = LocalToWorld(tpos,tang,data.S:GetPos(),data.S:GetAngles())
  181.             if view.vm_origin and view.vm_angles then
  182.                 local vmpos,vmang = WorldToLocal(view.vm_origin,view.vm_angles,data.G.RealPos or data.G:GetRealPos(),data.G.RealAng or data.G:GetRealAngles())
  183.                 view.vm_origin, view.vm_angles = LocalToWorld(vmpos,vmang,data.S:GetPos(),data.S:GetAngles())
  184.             end
  185.             apply = true
  186.         end
  187.     end,ErrorNoHalt)
  188.     if apply then
  189.         if method == 0 then
  190.             return view//GAMEMODE:CalcView(ply,pos,ang,fov)
  191.         elseif method == 1 then
  192.             return GAMEMODE:CalcView(ply,view.origin,view.angles,fov)
  193.         end
  194.     end
  195. end
  196. hook.Add("CalcView","SLShipView",GH.DoCalcView)
  197. local cvhook = "SLShipView"
  198. ------------------------------------------------------------------------------------------
  199. -- Name: ghd_fixcamera
  200. -- Desc: The magic calcview fixing function-- renames the hook in an attempt to claim priority.
  201. ------------------------------------------------------------------------------------------
  202. concommand.Add("ghd_fixcamera",function()
  203.     hook.Remove("CalcView",cvhook)
  204.     cvhook = string.char(math.random(32,122)).."SLShipView"
  205.     hook.Add("CalcView",cvhook,GH.DoCalcView)
  206.     Msg("GHD Camera Fix attempted, try now and run again if it doesn't work.")
  207. end)
  208. ------------------------------------------------------------------------------------------
  209. -- Name: SLRestoreRealPos
  210. -- Desc: Called after each frame to SetPos the objects back where they should be once rendered.
  211. ------------------------------------------------------------------------------------------
  212. local function SLRestoreRealPos(TABLE)
  213.     for ent,data in pairs(TABLE) do
  214.         if ValidEntity(ent) then
  215.             //if !(GH.SHIPCONTENTS[LocalPlayer()] and GH.SHIPCONTENTS[LocalPlayer()].S == data.S) then
  216.                 if data and ValidEntity(data.S) and ValidEntity(data.G) then
  217.                     if ent.RealPos then ent:SetPos(ent.RealPos) end
  218.                     if ent.RealAng then ent:SetAngles(ent.RealAng) end
  219.                     if ent.RealColor then ent:SetColor(unpack(ent.RealColor)) end
  220.                     if (ent.GetActiveWeapon and ValidEntity(ent:GetActiveWeapon())) then ent:GetActiveWeapon():SetColor(255,255,255,255) end
  221.                 end
  222.             //end
  223.         end
  224.     end
  225. end
  226. hook.Add("PostRenderVGUI","SLShipEnd",function()
  227.     xpcall(function()
  228.         SLRestoreRealPos(GH.SHIPCONTENTS)
  229.         SLRestoreRealPos(GH.GHOSTHULLS)
  230.     end,ErrorNoHalt)
  231. end)
  232. ------------------------------------------------------------------------------------------
  233. -- Name: SLShipContents
  234. -- Desc: Called before each frame to SetPos the objects to their transformed location.
  235. ------------------------------------------------------------------------------------------
  236. local function SLShipContents(TABLE,UseSG,cover)
  237.     for ent,data in pairs(TABLE) do
  238.         if ValidEntity(ent) then
  239.             if data and ValidEntity(data.S) and ValidEntity(UseSG and data.S.Ghost or data.G) then
  240.                 local G = (UseSG and data.S.Ghost or data.G)
  241.                 if draw_with_effect[ent:GetClass()] and ent != LocalPlayer() then
  242.                 /*
  243.                     local pos,ang,eye = ent:GetRealPos(),ent:GetRealAngles(),ent:EyeAngles()
  244.                     ent.RealPos = pos
  245.                     ent.RealAng = ang
  246.                     ent.RealEye = eye
  247.                     ent:SetPos(data.S:LocalToWorld(G:WorldToLocal(pos)))
  248.                     ent:SetAngles(data.S:LocalToWorldAngles(G:WorldToLocalAngles(ang)))*/
  249.                     //ent:SetEyeAngles(data.S:LocalToWorldAngles(G:WorldToLocalAngles(eye)))
  250.                     ent.RealColor = {ent:GetColor()}
  251.                     ent:SetColor(0,0,0,0)
  252.                     if (ent.GetActiveWeapon and ValidEntity(ent:GetActiveWeapon())) then ent:GetActiveWeapon():SetColor(0,0,0,0) end
  253.                 elseif ent != LocalPlayer() && (ent:GetMoveType() != MOVETYPE_NONE and ent:GetBoneCount()) == 1 && !ent.WasHidden then
  254.                     local pos,ang = ent:GetRealPos(),ent:GetRealAngles()
  255.                     ent.RealPos = pos
  256.                     ent.RealAng = ang
  257.                     if cover then
  258.                         ent:SetPos((G.RealPos or G:GetRealPos())-(RenderAngles():Forward()))
  259.                         ent:SetAngles(G:GetRealAngles())
  260.                     else
  261.                         local npos,nang = WorldToLocal(pos,ang, G.RealPos or G:GetRealPos(), G.RealAng or G:GetRealAngles())
  262.                         npos,nang = LocalToWorld(npos,nang, data.S:GetPos(), data.S:GetAngles())
  263.                         ent:SetPos(npos)
  264.                         ent:SetAngles(nang)
  265.                     end
  266.                 end
  267.             else
  268.                 TABLE[ent] = nil
  269.             end
  270.         else
  271.             TABLE[ent] = nil
  272.         end
  273.     end
  274. end
  275. hook.Add("RenderScene","SLShipDraw",function() --fake the positions of "contained" objects, unless the player is in the same place as the object
  276.     xpcall(function()
  277.         SLShipContents(GH.GHOSTHULLS,nil,true)
  278.         SLShipContents(GH.SHIPCONTENTS)
  279.     end,ErrorNoHalt)
  280. end)
  281. ------------------------------------------------------------------------------------------
  282. -- Name: sl_fake_tooltrace
  283. -- Desc: Sent by the server to fake the tool trace for interrupted CanTool messages
  284. ------------------------------------------------------------------------------------------
  285. usermessage.Hook("sl_fake_tooltrace",function(um)
  286.     local wep = um:ReadEntity()
  287.     local ent = um:ReadEntity()
  288.     local pos = um:ReadVector()
  289.     local nrm = um:ReadVector()
  290.     local bone = um:ReadShort()
  291.     wep:DoShootEffect(pos,nrm,ent,bone,true)
  292. end)
  293. local warpmat = Material("effects/water_warp01")
  294. local mats = {}
  295. local bmat = {}
  296. local fogs = {}
  297. local underwater = false
  298. local wasinship = false
  299. hook.Add("Think","SLWaterCheck",function()
  300.     local ply = LocalPlayer()
  301.     if ply:WaterLevel() == 3 then
  302.         if !underwater then
  303.             local tr = util.TraceLine{start = ply:GetPos(), endpos = ply:GetPos()+Vector(0,0,10000), mask = MASK_NPCSOLID_BRUSHONLY}
  304.             tr = util.TraceLine{start = tr.HitPos, endpos = tr.HitPos+Vector(0,0,-20000), mask = CONTENTS_WATER}
  305.             if !tr.HitTexture then return end
  306.             mats[tr.HitTexture] = mats[tr.HitTexture] or Material(tr.HitTexture)
  307.             local mtl = mats[tr.HitTexture]
  308.             local blw = mtl:GetMaterialString("$bottommaterial")
  309.             if blw then
  310.                 bmat[blw] = bmat[blw] or Material(blw)
  311.                 blw = bmat[blw]
  312.                 /*
  313.                 mtl:SetMaterialInt("$fogenable",1)
  314.                 blw:SetMaterialInt("$fogenable",1)
  315.                 mtl:SetMaterialInt("$fogstart",1)
  316.                 blw:SetMaterialInt("$fogstart",1)
  317.                 blw:SetMaterialInt("$fogend",1000000)
  318.                 mtl:SetMaterialInt("$fogend",1000000)
  319.                 blw:SetMaterialTexture("$normalmap",emptynrm)
  320.                 mtl:SetMaterialTexture("$normalmap",emptynrm)*/
  321.                 blw:SetMaterialString("$underwateroverlay","")
  322.             end
  323.         end
  324.         underwater = true
  325.     else
  326.         underwater = false
  327.     end
  328.     if GravHull.SHIPCONTENTS[ply] then
  329.         if !wasinship then
  330.             for k,v in pairs(bmat) do
  331.                 fogs[k.."s"] = v:GetMaterialInt("$fogstart")
  332.                 fogs[k.."e"] = v:GetMaterialInt("$fogend")
  333.                 if fogs[k.."s"] < 500 then
  334.                     v:SetMaterialInt("$fogstart",500)
  335.                 end
  336.                 if fogs[k.."e"] < 1000 then
  337.                     v:SetMaterialInt("$fogend",1000)
  338.                 end
  339.             end
  340.         end
  341.         wasinship = true
  342.     else
  343.         if wasinship then
  344.             for k,v in pairs(bmat) do
  345.                 v:SetMaterialInt("$fogstart",fogs[k.."s"])
  346.                 v:SetMaterialInt("$fogend",fogs[k.."e"])
  347.             end
  348.         end
  349.         wasinship = false
  350.     end
  351. end)
  352. hook.Add("PreDrawHUD","SLWaterOverlay",function()
  353.     for k,v in pairs(ents.FindByClass("class CLuaEffect")) do
  354.         if (v.DrawAgainInWater && (util.PointContents(v.Ent:GetPos()) & CONTENTS_WATER) != 0) then
  355.             v:Render()
  356.         end
  357.     end
  358.     if underwater then
  359.         render.UpdateRefractTexture()
  360.         render.SetMaterial(warpmat)
  361.         render.DrawScreenQuad()
  362.     end
  363. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement