2007-02-04 01:16:06 +00:00
|
|
|
|
|
|
|
AddCSLuaFile( "cl_init.lua" )
|
|
|
|
AddCSLuaFile( "shared.lua" )
|
|
|
|
|
|
|
|
include('shared.lua')
|
|
|
|
|
|
|
|
ENT.WireDebugName = "Gate"
|
2007-02-07 05:08:07 +00:00
|
|
|
ENT.OverlayDelay = 0
|
2007-02-04 01:16:06 +00:00
|
|
|
|
|
|
|
function ENT:Initialize()
|
|
|
|
self.Entity:PhysicsInit( SOLID_VPHYSICS )
|
|
|
|
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
|
|
|
|
self.Entity:SetSolid( SOLID_VPHYSICS )
|
|
|
|
|
|
|
|
self.Inputs = Wire_CreateInputs(self.Entity, { "A" })
|
|
|
|
self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" })
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-03-25 23:01:45 +00:00
|
|
|
function ENT:Setup( action, noclip )
|
2007-02-04 01:16:06 +00:00
|
|
|
if (action) then
|
|
|
|
self.WireDebugName = action.name
|
|
|
|
|
2007-04-26 05:35:36 +00:00
|
|
|
WireLib.AdjustSpecialInputs(self.Entity, action.inputs, action.inputtypes )
|
2007-02-04 01:16:06 +00:00
|
|
|
if (action.outputs) then
|
2007-04-26 05:35:36 +00:00
|
|
|
WireLib.AdjustSpecialOutputs(self.Entity, action.outputs, action.outputtypes)
|
2007-02-04 01:16:06 +00:00
|
|
|
else
|
2007-04-26 05:35:36 +00:00
|
|
|
//Wire_AdjustOutputs(self.Entity, { "Out" })
|
|
|
|
WireLib.AdjustSpecialOutputs(self.Entity, { "Out" }, action.outputtypes)
|
2007-02-04 01:16:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (action.reset) then
|
|
|
|
action.reset(self)
|
|
|
|
end
|
|
|
|
end
|
2007-03-25 23:01:45 +00:00
|
|
|
|
|
|
|
if (noclip) then
|
|
|
|
self.Entity:SetCollisionGroup( COLLISION_GROUP_WORLD )
|
|
|
|
end
|
|
|
|
|
2007-02-04 01:16:06 +00:00
|
|
|
self.Action = action
|
|
|
|
self.PrevValue = nil
|
2007-04-26 05:35:36 +00:00
|
|
|
|
|
|
|
//self.Action.inputtypes = self.Action.inputtypes or {}
|
|
|
|
|
2007-02-04 01:16:06 +00:00
|
|
|
self:CalcOutput()
|
|
|
|
self:ShowOutput()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-04-26 05:35:36 +00:00
|
|
|
function ENT:OnInputWireLink(iname, itype, src, oname, otype)
|
|
|
|
if (self.Action) and (self.Action.OnInputWireLink) then
|
|
|
|
self.Action.OnInputWireLink(self, iname, itype, src, oname, otype)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ENT:OnOutputWireLink(oname, otype, dst, iname, itype)
|
|
|
|
if (self.Action) and (self.Action.OnOutputWireLink) then
|
|
|
|
self.Action.OnOutputWireLink(self, oname, otype, dst, iname, itype)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-02-04 01:16:06 +00:00
|
|
|
function ENT:TriggerInput(iname, value, iter)
|
|
|
|
if (self.Action) and (not self.Action.timed) then
|
|
|
|
self:CalcOutput(iter)
|
|
|
|
self:ShowOutput()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ENT:Think()
|
|
|
|
self.BaseClass.Think(self)
|
|
|
|
|
|
|
|
if (self.Action) and (self.Action.timed) then
|
|
|
|
self:CalcOutput()
|
|
|
|
self:ShowOutput()
|
|
|
|
|
|
|
|
self.Entity:NextThink(CurTime()+0.02)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ENT:CalcOutput(iter)
|
|
|
|
if (self.Action) and (self.Action.output) then
|
|
|
|
if (self.Action.outputs) then
|
|
|
|
local result = { self.Action.output(self, unpack(self:GetActionInputs())) }
|
|
|
|
|
|
|
|
for k,v in ipairs(self.Action.outputs) do
|
|
|
|
Wire_TriggerOutput(self.Entity, v, result[k], iter)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local value = self.Action.output(self, unpack(self:GetActionInputs())) or 0
|
|
|
|
|
|
|
|
Wire_TriggerOutput(self.Entity, "Out", value, iter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ENT:ShowOutput()
|
|
|
|
local txt = ""
|
|
|
|
|
|
|
|
if (self.Action) then
|
|
|
|
txt = (self.Action.name or "No Name")
|
|
|
|
if (self.Action.label) then
|
2007-04-07 18:40:13 +00:00
|
|
|
txt = txt.."\n"..self.Action.label(self:GetActionOutputs(), unpack(self:GetActionInputs(Wire_EnableGateInputValues)))
|
2007-02-04 01:16:06 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
txt = "Invalid gate!"
|
|
|
|
end
|
|
|
|
|
|
|
|
self:SetOverlayText(txt)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ENT:OnRestore()
|
|
|
|
self.Action = GateActions[self.action]
|
|
|
|
|
|
|
|
self.BaseClass.OnRestore(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-02-07 05:08:07 +00:00
|
|
|
function ENT:GetActionInputs(as_names)
|
2007-02-04 01:16:06 +00:00
|
|
|
local Args = {}
|
|
|
|
|
|
|
|
if (self.Action.compact_inputs) then
|
|
|
|
for k,v in ipairs(self.Action.inputs) do
|
|
|
|
local input = self.Inputs[v]
|
|
|
|
if (not input) then
|
|
|
|
Msg("Missing input! ("..v..")")
|
|
|
|
return {}
|
|
|
|
end
|
2007-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
if (input.Src) and (input.Src:IsValid()) then
|
|
|
|
if (as_names) then
|
2007-02-08 06:39:35 +00:00
|
|
|
table.insert(Args, input.Src.WireName or input.Src.WireDebugName or v)
|
2007-02-04 01:16:06 +00:00
|
|
|
else
|
|
|
|
table.insert(Args, input.Value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
while (#Args < self.Action.compact_inputs) do
|
|
|
|
if (as_names) then
|
|
|
|
table.insert(Args, self.Action.inputs[#Args+1] or "*Not enough inputs*")
|
|
|
|
else
|
2007-04-26 05:35:36 +00:00
|
|
|
//table.insert( Args, WireLib.DT[ (self.Action.inputtypes[#Args+1] or "NORMAL") ].Zero )
|
|
|
|
table.insert( Args, WireLib.DT[ self.Inputs[ self.Action.inputs[#Args+1] ].Type ].Zero )
|
2007-02-07 05:08:07 +00:00
|
|
|
end
|
2007-02-04 01:16:06 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
for k,v in ipairs(self.Action.inputs) do
|
|
|
|
local input = self.Inputs[v]
|
|
|
|
if (not input) then
|
|
|
|
Msg("Missing input! ("..v..")")
|
|
|
|
return {}
|
|
|
|
end
|
2007-02-07 05:08:07 +00:00
|
|
|
|
|
|
|
if (as_names) then
|
|
|
|
if (input.Src) and (input.Src:IsValid()) then
|
2007-02-08 07:16:24 +00:00
|
|
|
Args[k] = input.Src.WireName or input.Src.WireDebugName or v
|
2007-02-04 01:16:06 +00:00
|
|
|
else
|
2007-02-07 05:08:07 +00:00
|
|
|
Args[k] = v
|
2007-02-04 01:16:06 +00:00
|
|
|
end
|
|
|
|
else
|
2007-02-07 05:08:07 +00:00
|
|
|
if (input.Src) and (input.Src:IsValid()) then
|
2007-04-26 05:35:36 +00:00
|
|
|
//Args[k] = ( input.Value or WireLib.DT[ (self.Action.inputtypes[k] or "NORMAL") ].Zero )
|
|
|
|
Args[k] = ( input.Value or WireLib.DT[ self.Inputs[v].Type ].Zero )
|
2007-02-07 05:08:07 +00:00
|
|
|
else
|
2007-04-26 05:35:36 +00:00
|
|
|
//Args[k] = WireLib.DT[ (self.Action.inputtypes[k] or "NORMAL") ].Zero
|
|
|
|
Args[k] = WireLib.DT[ self.Inputs[v].Type ].Zero
|
2007-02-07 05:08:07 +00:00
|
|
|
end
|
2007-02-04 01:16:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Args
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ENT:GetActionOutputs()
|
|
|
|
if (self.Action.outputs) then
|
|
|
|
local result = {}
|
|
|
|
for _,v in ipairs(self.Action.outputs) do
|
|
|
|
result[v] = self.Outputs[v].Value or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.Outputs.Out.Value or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-02-06 01:21:09 +00:00
|
|
|
function MakeWireGate(pl, Pos, Ang, Model, action, noclip, frozen, nocollide)
|
2007-02-04 01:16:06 +00:00
|
|
|
if ( !pl:CheckLimit( "wire_gates" ) ) then return nil end
|
|
|
|
|
|
|
|
local wire_gate = ents.Create( "gmod_wire_gate" )
|
|
|
|
wire_gate:SetPos( Pos )
|
|
|
|
wire_gate:SetAngles( Ang )
|
|
|
|
wire_gate:SetModel( Model )
|
|
|
|
wire_gate:Spawn()
|
|
|
|
wire_gate:Activate()
|
2007-03-25 23:01:45 +00:00
|
|
|
|
|
|
|
wire_gate:Setup( GateActions[action], noclip )
|
2007-02-04 01:16:06 +00:00
|
|
|
wire_gate:SetPlayer( pl )
|
|
|
|
|
2008-02-06 01:21:09 +00:00
|
|
|
if wire_gate:GetPhysicsObject():IsValid() then
|
|
|
|
local Phys = wire_gate:GetPhysicsObject()
|
|
|
|
if nocollide or noclip then
|
|
|
|
Phys:SetCollisionGroup(COLLISION_GROUP_WORLD)
|
|
|
|
end
|
|
|
|
Phys:EnableMotion(!frozen)
|
|
|
|
end
|
2007-02-04 01:16:06 +00:00
|
|
|
|
|
|
|
local ttable =
|
|
|
|
{
|
|
|
|
pl = pl,
|
2008-02-06 01:21:09 +00:00
|
|
|
action = action,
|
2007-03-25 23:01:45 +00:00
|
|
|
noclip = noclip,
|
2008-02-06 01:21:09 +00:00
|
|
|
nocollide = nocollide
|
2007-02-04 01:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table.Merge( wire_gate:GetTable(), ttable )
|
|
|
|
|
|
|
|
pl:AddCount( "wire_gates", wire_gate )
|
|
|
|
|
|
|
|
return wire_gate
|
|
|
|
end
|
2008-02-06 01:21:09 +00:00
|
|
|
duplicator.RegisterEntityClass("gmod_wire_gate", MakeWireGate, "Pos", "Ang", "Model", "action", "noclip", "frozen", "nocollide")
|