-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathgmod_wire_teleporter.lua
More file actions
338 lines (277 loc) · 11 KB
/
gmod_wire_teleporter.lua
File metadata and controls
338 lines (277 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
AddCSLuaFile()
DEFINE_BASECLASS( "base_wire_entity" )
ENT.PrintName = "Wire Teleporter"
ENT.WireDebugName = "Teleporter"
ENT.Author = "Divran"
if CLIENT then return end -- No more client
local cooldownCvar = CreateConVar("wire_teleporter_cooldown","1",{FCVAR_ARCHIVE,FCVAR_NOTIFY})
function ENT:Initialize()
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self.Jumping = false
self.TargetPos = self:GetPos()
self.TargetAng = self:GetAngles()
self.Entities = {}
self.LocalPos = {}
self.LocalAng = {}
self.LocalVel = {}
self.UseSounds = true
self.UseEffects = true
self.ClassSpecificActions = {
gmod_wire_hoverball = function( ent, oldpos, newpos ) ent:SetZTarget( newpos.z ) end,
gmod_toggleablehoverball = function( ent, oldpos, newpos ) ent:SetTargetZ( newpos.z ) end,
gmod_hoverball = function( ent, oldpos, newpos ) ent.dt.TargetZ = newpos.z end,
}
self:ShowOutput()
self.Inputs = Wire_CreateInputs( self, { "Jump (Activates the teleporter.\nIf nothing happens and an error noise is played, the destination does not have enough room to fit your contraption.)", "TargetPos [VECTOR]", "X", "Y", "Z", "TargetAngle [ANGLE]", "Sound" })
end
function ENT:TriggerInput(iname, value)
if (iname == "Jump") then
if (value ~= 0 and not self.Jumping) then
self:Jump(self.UseAngle or self.Inputs.TargetAngle.Src ~= nil)
self.UseAngle = false
end
elseif (iname == "TargetPos") then
self.TargetPos = value
elseif (iname == "X") then
self.TargetPos.x = value
elseif (iname == "Y") then
self.TargetPos.y = value
elseif (iname == "Z") then
self.TargetPos.z = value
elseif (iname == "TargetAngle") then
self.TargetAng = value
-- if the angle is set, we should use it for jumping
-- even if there's nothing connected to the angle wire.
-- otherwise, we can't use wirelink for angles.
self.UseAngle = true
elseif (iname == "Sound") then
self.UseSounds = value ~= 0
end
self:ShowOutput()
end
function ENT:ShowOutput()
self:SetOverlayText( "Target Position = " .. tostring(self.TargetPos) .. "\nTarget Angle = " .. tostring(self.TargetAng) .. "\nSounds = " .. (self.UseSounds and "Yes" or "No") .. "\nEffects = " .. (self.UseEffects and "Yes" or "No") )
end
function ENT:Jump( withangles )
--------------------------------------------------------------------
-- Check for errors
--------------------------------------------------------------------
-- Is already teleporting
if (self.Jumping) then
return
end
-- The target position is outside the world
if (not util.IsInWorld( self.TargetPos )) then
self:EmitSound("buttons/button8.wav")
return
end
-- The position or angle hasn't changed
if (self:GetPos() == self.TargetPos and self:GetAngles() == self.TargetAng) then
self:EmitSound("buttons/button8.wav")
return
end
--------------------------------------------------------------------
-- Find other entities
--------------------------------------------------------------------
-- Get the localized positions
local ents = constraint.GetAllConstrainedEntities( self )
-- If the teleporter is parented, and not constrained, then get the contraption of the parent instead
local val = next(ents) -- the first value of GetAllConstrainedEntities is always 'self', so we skip this value and check the next
if next(ents,val) == nil and IsValid(self:GetParent()) then
ents = constraint.GetAllConstrainedEntities( self:GetParent() )
end
-- Check world
self.Entities = {}
self.OtherEntities = {}
for _, ent in pairs( ents ) do
-- Calculate the position after teleport, without actually moving the entity
local pos = self:WorldToLocal( ent:GetPos() )
pos:Rotate( self.TargetAng )
pos = pos + self.TargetPos
local b = util.IsInWorld( pos )
if not b then -- If an entity will be outside the world after teleporting..
self:EmitSound("buttons/button8.wav")
return
elseif ent ~= self then -- if the entity is not equal to self
if self:CheckAllowed( ent ) then -- If the entity can be teleported
table.insert(self.Entities, ent)
else -- If the entity can't be teleported
self.OtherEntities[#self.OtherEntities+1] = ent
end
end
end
-- All error checking passed
self.Jumping = true
--------------------------------------------------------------------
-- Sound and visual effects
--------------------------------------------------------------------
if self.UseSounds then self:EmitSound("ambient/levels/citadel/weapon_disintegrate2.wav") end -- Starting sound
if self.UseEffects then
-- Effect out
local effectdata = EffectData()
effectdata:SetEntity( self )
local Dir = (self.TargetPos - self:GetPos())
Dir:Normalize()
effectdata:SetOrigin( self:GetPos() + Dir * math.Clamp( self:BoundingRadius() * 5, 180, 4092 ) )
util.Effect( "jump_out", effectdata, true, true )
DoPropSpawnedEffect( self )
for _, ent in pairs( ents ) do
-- Effect out
local effectdata = EffectData()
effectdata:SetEntity( ent )
effectdata:SetOrigin( self:GetPos() + Dir * math.Clamp( ent:BoundingRadius() * 5, 180, 4092 ) )
util.Effect( "jump_out", effectdata, true, true )
end
end
-- Call the next stage after a short time. This small delay is necessary for sounds and effects to work properly.
timer.Simple( 0.05, function()
if not IsValid( self ) then return end
self:Jump_Part2( withangles )
end )
end
function ENT:Jump_Part2( withangles )
local OldPos = self:GetPos()
--------------------------------------------------------------------
-- Other entities
--------------------------------------------------------------------
-- Save local positions, angles, and velocity
self.LocalPos = {}
self.LocalAng = {}
self.LocalVel = {}
for k, ent in pairs(self.Entities) do
if IsValid(ent) then
if (ent:GetPhysicsObjectCount() > 1) then -- Check for bones
local tbl = { Main = self:WorldToLocal( ent:GetPos() ) }
local tbl2 = { Main = self:WorldToLocal( ent:GetVelocity() + ent:GetPos() ) }
for i=0, ent:GetPhysicsObjectCount()-1 do
local b = ent:GetPhysicsObjectNum( i )
tbl[i] = ent:WorldToLocal( b:GetPos() )
tbl2[i] = ent:WorldToLocal( ent:GetPos() + b:GetVelocity() )
b:SetVelocity( b:GetVelocity() * -1 )
end
-- Save the localized position table
self.LocalPos[ent] = tbl
-- Save the localized velocity table
self.LocalVel[ent] = tbl2
else
-- Save the localized position
self.LocalPos[ent] = self:WorldToLocal( ent:GetPos() )
-- Save the localized velocity
self.LocalVel[ent] = self:WorldToLocal( ent:GetVelocity() + ent:GetPos() )
end
ent:SetVelocity( ent:GetVelocity() * -1 )
if withangles then
self.LocalAng[ent] = self:WorldToLocalAngles( ent:GetAngles() )
end
else
self.Entities[k] = nil
end
end
--------------------------------------------------------------------
-- The teleporter itself
--------------------------------------------------------------------
-- Save old parent and then unparent the teleporter (is restored after teleporting)
-- This prevents an issue that deletes the entire contraption
local parent = self:GetParent()
self:SetParent()
local oldvel = self:WorldToLocal( self:GetVelocity() + self:GetPos() ) -- Velocity
self:SetPos( self.TargetPos ) -- Position
if withangles then self:SetAngles( self.TargetAng ) end -- Angle
self:GetPhysicsObject():SetVelocity( self:LocalToWorld( oldvel ) - self:GetPos() ) -- Set new velocity
if self.UseSounds then self:EmitSound("npc/turret_floor/die.wav", 450, 70) end -- Sound
local Dir = (OldPos - self:GetPos()):GetNormalized()
if self.UseEffects then
-- Effect
effectdata = EffectData()
effectdata:SetEntity( self )
effectdata:SetOrigin( self:GetPos() + Dir * math.Clamp( self:BoundingRadius() * 5, 180, 4092 ) )
util.Effect( "jump_in", effectdata, true, true )
end
--------------------------------------------------------------------
-- Other entities
--------------------------------------------------------------------
for k, ent in pairs(self.Entities) do
if IsValid(ent) then
local oldPos = ent:GetPos() -- Remember old position
if withangles then ent:SetAngles( self:LocalToWorldAngles( self.LocalAng[ent] ) ) end -- Angles
if (ent:GetPhysicsObjectCount() > 1) then -- Check for bones
ent:SetPos( self:LocalToWorld( self.LocalPos[ent].Main ) ) -- Position
-- Set new velocity
local phys = ent:GetPhysicsObject()
if phys:IsValid() then
phys:SetVelocity( self:LocalToWorld( self.LocalVel[ent].Main ) - ent:GetPos() )
else
ent:SetVelocity( self:LocalToWorld( self.LocalVel[ent].Main ) )
end
for i=0, ent:GetPhysicsObjectCount()-1 do -- For each bone...
local b = ent:GetPhysicsObjectNum( i )
b:SetPos( ent:LocalToWorld(self.LocalPos[ent][i]) ) -- Position
b:SetVelocity( ent:LocalToWorld( self.LocalVel[ent][i] ) - ent:GetPos() ) -- Set new velocity
end
ent:GetPhysicsObject():Wake()
else -- If it doesn't have bones
ent:SetPos( self:LocalToWorld(self.LocalPos[ent]) ) -- Position
-- Set new velocity
local phys = ent:GetPhysicsObject()
if phys:IsValid() then
phys:SetVelocity( self:LocalToWorld( self.LocalVel[ent] ) - ent:GetPos() )
else
ent:SetVelocity( self:LocalToWorld( self.LocalVel[ent] ) )
end
ent:GetPhysicsObject():Wake()
end
if self.UseEffects then
-- Effect in
effectdata = EffectData()
effectdata:SetEntity( ent )
effectdata:SetOrigin( self:GetPos() + Dir * math.Clamp( ent:BoundingRadius() * 5, 180, 4092 ) )
util.Effect( "jump_in", effectdata, true, true )
DoPropSpawnedEffect( ent )
end
if self.ClassSpecificActions[ent:GetClass()] then -- Call function specific for this entity class
self.ClassSpecificActions[ent:GetClass()]( ent, oldPos, ent:GetPos() )
end
else
self.Entities[k] = nil
end
end
if self.UseEffects then
for _, ent in pairs( self.OtherEntities ) do -- Render the effect on all other entities in the contraption
-- Effect in
effectdata = EffectData()
effectdata:SetEntity( ent )
effectdata:SetOrigin( self:GetPos() + Dir * math.Clamp( ent:BoundingRadius() * 5, 180, 4092 ) )
util.Effect( "jump_in", effectdata, true, true )
DoPropSpawnedEffect( ent )
end
end
self:SetParent( parent ) -- restore parent
-- Cooldown - prevent teleporting for a time
timer.Create(
"teleporter_"..self:EntIndex(), -- name
cooldownCvar:GetFloat(), -- delay
1, -- nr of runs
function() -- function
if self:IsValid() then
self.Jumping = false
end
end
)
end
function ENT:CheckAllowed( e )
if (e:GetParent():EntIndex() ~= 0) then return false end
-- These shouldn't happen, ever, but they're here just to be safe
local c = e:GetClass()
if c == "Player" or c:find("npc_") then return false end
return true
end
function ENT:Setup(UseSounds, UseEffects)
self.UseSounds = UseSounds
self.UseEffects = UseEffects
self:ShowOutput()
end
duplicator.RegisterEntityClass("gmod_wire_hoverdrivecontroler", WireLib.MakeWireEnt, "Data", "UseSounds", "UseEffects" )
duplicator.RegisterEntityClass("gmod_wire_teleporter", WireLib.MakeWireEnt, "Data", "UseSounds", "UseEffects")
scripted_ents.Alias("gmod_wire_hoverdrivecontroler", "gmod_wire_teleporter")