Skip to content

Commit

Permalink
Added beta teleport code
Browse files Browse the repository at this point in the history
Also added some hooks 'wp-shouldrender' and 'wp-shouldtp' as well as
ENT.TPHook.
  • Loading branch information
MattJeanes committed Aug 30, 2014
1 parent d89c9ab commit bf2225e
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 77 deletions.
9 changes: 8 additions & 1 deletion lua/autorun/worldportals_init.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@

worldportals = {}
--because worldportals is too long
wp = {}

-- Load required files
include( "worldportals/utils_sh.lua" )

if SERVER then

include( "worldportals/render_sv.lua" )
include( "worldportals/teleport_sv.lua" )

AddCSLuaFile( "worldportals/utils_sh.lua" )
AddCSLuaFile( "worldportals/render_cl.lua" )
--AddCSLuaFile( "worldportals/teleport_cl.lua" )

else

include( "worldportals/render_cl.lua" )
include( "worldportals/teleport_cl.lua" )

end
16 changes: 6 additions & 10 deletions lua/entities/linked_portal_door/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ AccessorFunc( ENT, "texture", "Texture" )
AccessorFunc( ENT, "shouldDrawaNextFrame", "ShouldDrawNextFrame" )


--Draw world portals
--overridedepthenable for drawing quad?
-- Draw world portals
function ENT:Draw()

--self:DrawModel()
if wp.drawing then return end

if worldportals.drawing then return end

--portal is rendering, so start rendering the view from it next frame
self:SetShouldDrawNextFrame( true )

render.ClearStencil()
Expand All @@ -28,17 +24,17 @@ function ENT:Draw()
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_ALWAYS )
render.SetStencilReferenceValue( 1 )

render.SetMaterial( worldportals.matDummy )
render.SetMaterial( wp.matDummy )
render.SetColorModulation( 1, 1, 1 )

render.DrawQuadEasy( self:GetPos(), self:GetForward(), self:GetWidth(), self:GetHeight(), Color( 255, 255, 255, 255), self:GetAngles().roll )
render.DrawQuadEasy( self:GetPos() -( self:GetForward() *5), self:GetForward(), self:GetWidth(), self:GetHeight(), Color( 255, 255, 255, 255), self:GetAngles().roll )

render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL )
render.SetStencilPassOperation( STENCILOPERATION_REPLACE )
render.SetStencilReferenceValue( 1 )

worldportals.matView:SetTexture( "$basetexture", self:GetTexture() )
render.SetMaterial( worldportals.matView )
wp.matView:SetTexture( "$basetexture", self:GetTexture() )
render.SetMaterial( wp.matView )
render.DrawScreenQuad()

render.SetStencilEnable( false )
Expand Down
40 changes: 40 additions & 0 deletions lua/entities/linked_portal_door/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,43 @@ function ENT:KeyValue( key, value )
self:SetAngles( Angle( unpack(args) ) )
end
end

-- Teleportation
function ENT:Touch( ent )

local vel_norm = ent:GetVelocity():GetNormalized()

-- Object is moving towards the portal
if vel_norm:Dot( self:GetForward() ) < 0 then

local projected_distance = wp.DistanceToPlane( ent:EyePos() + ent:GetVelocity() * engine.TickInterval(), self:GetPos(), self:GetForward() )

if projected_distance < 0 and hook.Call("wp-shouldtp",GAMEMODE,self,ent)~=false then

local new_pos = wp.TransformPortalPos( ent:GetPos() + ent:GetVelocity() * engine.TickInterval(), self, self:GetExit() )
local old_velocity = ent:GetVelocity()
local new_velocity = old_velocity + Vector(0,0,0) --stupid pointers
local new_angle = wp.TransformPortalAngle( ent:GetAngles(), self, self:GetExit() )

new_velocity = wp.TransformPortalVector( new_velocity, self, self:GetExit() )

ent:SetPos( new_pos )
if ent:IsPlayer() then
ent:SetEyeAngles( new_angle )
ent:SetVelocity( new_velocity -old_velocity )
wp.AlertPlayerOnTeleport( ent )
else
ent:SetAngles( new_angle )

local phys = ent:GetPhysicsObject()
if phys then phys:SetVelocity( new_velocity ) end
end

ent:ForcePlayerDrop()

if self.TPHook then
self:TPHook(ent)
end
end
end
end
25 changes: 7 additions & 18 deletions lua/entities/linked_portal_door/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ ENT.AdminOnly = false
ENT.Editable = false


--ENT.Model = Model("models/props/cs_office/microwave.mdl")

function ENT:Initialize()

local mins = Vector(-1, -self:GetWidth() /2, -self:GetHeight() /2)
local maxs = -mins
local mins = Vector( 0, -self:GetWidth() /2, -self:GetHeight() /2 )
local maxs = Vector( 10, self:GetWidth() /2, self:GetHeight() /2)

if CLIENT then

Expand All @@ -24,26 +22,17 @@ function ENT:Initialize()

else

self:SetTrigger(true)
self:SetTrigger( true )

end

--self:SetModel(self.Model)

--self:PhysicsInit(SOLID_VPHYSICS)
--self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetMoveType(MOVETYPE_NONE)
self:SetSolid(SOLID_OBB)
self:SetNotSolid(true)
self:SetCollisionBounds(mins, maxs)
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_OBB )
self:SetNotSolid( true )
self:SetCollisionBounds( mins, maxs )

self:EnableCustomCollisions(true)
self:DrawShadow( false )

--local phys = self:GetPhysicsObject()
--phys:EnableMotion( true )
--phys:Wake()

end


Expand Down
71 changes: 24 additions & 47 deletions lua/worldportals/render_cl.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

--Setup variables
worldportals.matView = CreateMaterial(
-- Setup variables
wp.matView = CreateMaterial(
"UnlitGeneric",
"GMODScreenspace",
{
Expand All @@ -9,22 +9,15 @@ worldportals.matView = CreateMaterial(
["$vertexalpha"] = "1",
}
)
worldportals.matDummy = Material( "debug/white" )
wp.matDummy = Material( "debug/white" )

wp.portals = {}
wp.drawing = true --default portals to not draw

--POTENTIAL IMPROVEMENT
--add portals when they're created
--remove portals when they're destroyed
--this would be slightly better than just finding all of them every frame
--hook.Add( "OnEntityCreated", "WorldPortalRenderHook", function( ent )

local portals
worldportals.drawing = true --default portals to not draw

--Start drawing the portals
--This prevents the game from crashing when loaded for the first time
-- Start drawing the portals
-- This prevents the game from crashing when loaded for the first time
hook.Add( "PostRender", "WorldPortals_StartRender", function()
worldportals.drawing = false
wp.drawing = false
hook.Remove( "PostRender", "WorldPortals_StartRender" )
end )

Expand All @@ -40,8 +33,10 @@ local function ShouldRender( portal, exitPortal, plyOrigin )
if not portal:GetShouldDrawNextFrame() then return false end

--don't render if the player is behind the portal
local vec = plyOrigin - portal:GetPos()
if ( portal:GetForward():Dot( vec ) < 0 ) then return false end
local behind = wp.IsBehind( plyOrigin, portal:GetPos(), portal:GetForward() )
if behind then return false end

if hook.Call("wp-shouldrender", GAMEMODE, portal, exitPortal, plyOrigin)==false then return false end

portal:SetShouldDrawNextFrame( false )

Expand All @@ -51,18 +46,18 @@ end


-- Render views from the portals
hook.Add( "RenderScene", "WorldPortals_Render", function( plyOrigin, plyAngles )
hook.Add( "RenderScene", "WorldPortals_Render", function( plyOrigin, plyAngle )

portals = ents.FindByClass( "linked_portal_door" )
wp.portals = ents.FindByClass( "linked_portal_door" )

if ( not portals ) then return end
if ( worldportals.drawing ) then return end
if ( not wp.portals ) then return end
if ( wp.drawing ) then return end

--Disable phys gun glow and beam
-- Disable phys gun glow and beam
local oldWepColor = LocalPlayer():GetWeaponColor()
LocalPlayer():SetWeaponColor( Vector(0, 0, 0) )

for _, portal in pairs( portals ) do
for _, portal in pairs( wp.portals ) do

local exitPortal = portal:GetExit()

Expand All @@ -77,29 +72,24 @@ hook.Add( "RenderScene", "WorldPortals_Render", function( plyOrigin, plyAngles )
render.EnableClipping(true)
render.PushCustomClipPlane( exitPortal:GetForward(), exitPortal:GetForward():Dot(exitPortal:GetPos() - (exitPortal:GetForward() *0.5) ) )

local localOrigin = portal:WorldToLocal( plyOrigin )
local localAngles = portal:WorldToLocalAngles( plyAngles )

localOrigin:Rotate( Angle(0, 180, 0) )
localAngles:RotateAroundAxis( Vector(0, 0, 1), 180)
local camOrigin = wp.TransformPortalPos( plyOrigin, portal, exitPortal )
local camAngle = wp.TransformPortalAngle( plyAngle, portal, exitPortal )

local camOrigin = exitPortal:LocalToWorld( localOrigin )
local camAngles = exitPortal:LocalToWorldAngles( localAngles )

worldportals.drawing = true
wp.drawing = true
render.RenderView( {
x = 0,
y = 0,
w = ScrW(),
h = ScrH(),
origin = camOrigin,
angles = camAngles,
angles = camAngle,
drawpostprocess = true,
drawhud = false,
drawmonitors = false,
drawviewmodel = false,
--zfar = 1500
} )
worldportals.drawing = false
wp.drawing = false

render.PopCustomClipPlane()
render.EnableClipping(false)
Expand All @@ -108,16 +98,3 @@ hook.Add( "RenderScene", "WorldPortals_Render", function( plyOrigin, plyAngles )

LocalPlayer():SetWeaponColor( oldWepColor )
end )

-- Set it to draw the player while rendering portals
-- Calling Start3D to fix this is incredibly hacky
hook.Add( "PostDrawEffects", "WorldPortals_PlayerDrawFix", function()
cam.Start3D( EyePos(), EyeAngles() )
cam.End3D()
end)

hook.Add( "ShouldDrawLocalPlayer", "WorldPortals_PlayerDraw", function()
if worldportals.drawing then
return true
end
end)
1 change: 0 additions & 1 deletion lua/worldportals/render_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ local function PairWithExits()
for _, portal in ipairs( ents.FindByClass( "linked_portal_door" ) ) do
if not IsValid( portal:GetExit() ) then
portal:SetExit( ents.FindByName( portal:GetPartnerName() )[1] )
print("fixed exit; new exit:", portal:GetExit())
end
end
end
Expand Down
71 changes: 71 additions & 0 deletions lua/worldportals/teleport_cl.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

-- Client side tetleportation prediction
-- This is somewhat specific to rooftops_lost and can be improved a lot

--should really create a custom move type based off of the source engine move type in order
--to have properly predicted teleportation

--[[
local last_tele = 0
hook.Add( "Tick", "WorldPortals_Teleportation", function()
hook.Remove( "CalcView", "WorldPortals_PredictTeleView" )
local ply = LocalPlayer()
for _, portal in pairs( wp.portals ) do
local distance = ply:GetPos():Distance( portal:GetPos() )
if distance < 150 then
print("asdf")
local mins = portal:GetPos() + Vector( -32, -portal:GetWidth() /2, -portal:GetHeight() /2 ) --would normally be 0
local maxs = portal:GetPos() + Vector( 32, portal:GetWidth() /2, portal:GetHeight() /2) --would normally be 10
local ply_pos = LocalPlayer():GetPos() + Vector( 0, 0, 36)
if ply_pos:WithinAABox( mins, maxs ) then
--print("asddd1", ent, LocalPlayer() )
--if ent ~= ply then continue end
print("asddd")
local vel_norm = ply:GetVelocity():GetNormalized()
-- Object is moving towards the portal
if vel_norm:Dot( portal:GetForward() ) < 0 then --and SysTime() - last_tele > 0.5 then
print("ddddd")
local projected_distance = wp.DistanceToPlane( ply:EyePos() + ply:GetVelocity() *engine.TickInterval(), portal:GetPos(), portal:GetForward() )
--if projected_distance < 0 then
print("ffffff")
hook.Add( "CalcView", "WorldPortals_PredictTeleView", function( ply, pos, angle, fov )
local camOrigin = wp.TransformPortalPos( pos, portal, portal:GetExit() )
local camAngle = wp.TransformPortalAngle( angle, portal, portal:GetExit() )
print("qqqqq")
return {
origin = camOrigin, ---( angle:Forward()*100 ), --camorigin
angles = camAngle,
fov = fov
}
end )
--end
end
end
end
end
end )
net.Receive( "WorldPortals_TeleportAlert", function()
hook.Remove( "CalcView", "WorldPortals_PredictTeleView" )
end )
]]--
9 changes: 9 additions & 0 deletions lua/worldportals/teleport_sv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

-- Add message name to pool
util.AddNetworkString( "WorldPortals_TeleportAlert" )

-- Let player know that server has acknowledged and completed the teleport
function wp.AlertPlayerOnTeleport( ply )
net.Start( "WorldPortals_TeleportAlert" )
net.Send( ply )
end
Loading

0 comments on commit bf2225e

Please sign in to comment.