2008-06-14 00:55:20 +00:00
|
|
|
|
|
|
|
AddCSLuaFile( "cl_init.lua" )
|
|
|
|
AddCSLuaFile( "shared.lua" )
|
|
|
|
|
|
|
|
include('shared.lua')
|
|
|
|
|
|
|
|
ENT.WireDebugName = "Radio"
|
|
|
|
|
|
|
|
local MODEL = Model( "models/props_lab/binderblue.mdl" )
|
|
|
|
|
|
|
|
function ENT:Initialize()
|
|
|
|
self.Entity:PhysicsInit( SOLID_VPHYSICS )
|
|
|
|
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
|
|
|
|
self.Entity:SetSolid( SOLID_VPHYSICS )
|
|
|
|
|
|
|
|
self.Inputs = Wire_CreateInputs(self, { "Channel"})
|
|
|
|
self.Outputs = Wire_CreateOutputs(self, { "ERRORS!!!" })
|
|
|
|
|
|
|
|
self.Channel = 1
|
|
|
|
self.Transmitting = 0
|
2009-01-17 13:17:12 +00:00
|
|
|
self.ValuesTable = {}
|
2008-06-14 00:55:20 +00:00
|
|
|
|
|
|
|
Radio_Register(self)
|
2009-01-17 13:17:12 +00:00
|
|
|
Radio_TuneIn(self,self.Channel)
|
2008-06-14 00:55:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ENT:Setup(channel,values,secure)
|
|
|
|
channel = math.floor(tonumber(channel) or 0)
|
2009-01-17 13:17:12 +00:00
|
|
|
Radio_TuneOut(self,self.Channel)
|
2008-06-14 00:55:20 +00:00
|
|
|
self.Channel = channel
|
2009-01-17 13:17:12 +00:00
|
|
|
Radio_TuneIn(self,self.Channel)
|
2008-06-14 00:55:20 +00:00
|
|
|
self.Secure = secure
|
|
|
|
self.Old = false
|
|
|
|
if (tonumber(values) == nil) then
|
|
|
|
values = 4
|
|
|
|
self.Old = true
|
|
|
|
else
|
|
|
|
values = math.Round(values)
|
|
|
|
if (values > 20) then
|
|
|
|
values = 20
|
|
|
|
end
|
|
|
|
if (values < 1) then
|
|
|
|
values = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.Values = values
|
|
|
|
local onames = {}
|
|
|
|
if (self.Old == false) then
|
|
|
|
for i = 1,self.Values do
|
|
|
|
onames[i] = tostring(i) //without tostring() you kill the debugger.
|
|
|
|
end
|
|
|
|
else
|
|
|
|
onames = {"A","B","C","D"}
|
|
|
|
end
|
2009-01-17 13:17:12 +00:00
|
|
|
|
|
|
|
self.ValuesTable = {}
|
|
|
|
for i=1,values do
|
|
|
|
self.ValuesTable[i] = 0
|
|
|
|
end
|
2008-06-14 00:55:20 +00:00
|
|
|
|
|
|
|
Wire_AdjustOutputs(self,onames)
|
|
|
|
table.insert(onames,"Channel")
|
|
|
|
Wire_AdjustInputs(self,onames)
|
|
|
|
|
|
|
|
self:ReceiveRadio(Radio_Receive(self,self.Channel))
|
|
|
|
end
|
|
|
|
|
|
|
|
function ENT:TriggerInput(iname, value)
|
|
|
|
if (iname == "Channel") then
|
2009-01-02 08:34:50 +00:00
|
|
|
Radio_TuneOut(self,self.Channel)
|
2009-01-17 13:17:12 +00:00
|
|
|
Radio_TuneIn(self,value)
|
2009-01-02 08:34:50 +00:00
|
|
|
self.Channel = math.floor(value)
|
2009-01-17 13:17:12 +00:00
|
|
|
|
2009-01-02 08:34:50 +00:00
|
|
|
self:ReceiveRadio(Radio_Receive(self,self.Channel))
|
2009-01-17 13:17:12 +00:00
|
|
|
|
|
|
|
// for k,v in pairs(self.Inputs) do
|
|
|
|
// if k != "Channel" then self:Transmit(self.Channel, k, v.Value) end
|
|
|
|
// end
|
2008-06-14 00:55:20 +00:00
|
|
|
elseif (iname != nil && value != nil) then
|
2009-01-17 13:17:12 +00:00
|
|
|
self.ValuesTable[tonumber(iname)] = value
|
2009-01-02 08:34:50 +00:00
|
|
|
self.Inputs[iname].Value = value
|
|
|
|
self:Transmit(self.Channel,iname,value)
|
2008-06-14 00:55:20 +00:00
|
|
|
end
|
|
|
|
self:ShowOutput()
|
|
|
|
end
|
|
|
|
|
2009-01-09 08:40:59 +00:00
|
|
|
function ENT:ReadCell(Address)
|
2009-01-17 13:17:12 +00:00
|
|
|
// print("==================== read "..Address)
|
|
|
|
// print(self.ValuesTable[Address+1])
|
2009-01-09 08:40:59 +00:00
|
|
|
if (Address >= 0) && (Address < self.Values) then
|
2009-01-17 13:17:12 +00:00
|
|
|
return self.ValuesTable[Address+1]
|
2009-01-09 08:40:59 +00:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ENT:WriteCell(Address, value)
|
|
|
|
if (Address >= 0) && (Address < self.Values) then
|
2009-01-10 21:04:03 +00:00
|
|
|
self:Transmit(self.Channel, tostring(Address+1), value)
|
2009-01-09 08:40:59 +00:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-14 00:55:20 +00:00
|
|
|
function ENT:Transmit(channel,k,v)
|
|
|
|
Radio_Transmit(self,self.Channel,k,v)
|
2009-01-17 13:17:12 +00:00
|
|
|
self.ValuesTable[tonumber(k)] = v
|
2008-06-14 00:55:20 +00:00
|
|
|
end
|
|
|
|
|
2009-01-17 13:17:12 +00:00
|
|
|
function ENT:ReceiveRadio(values) //FIXME: horrible code, needs cleanup. it's only a temp fix...
|
2008-06-14 00:55:20 +00:00
|
|
|
if (values == nil) then return end
|
2009-01-17 13:17:12 +00:00
|
|
|
// print("recieve radio:")
|
|
|
|
//for k,o in pairs(values) do
|
|
|
|
for j=1,20 do
|
|
|
|
if (values[tostring(j)]) then
|
|
|
|
Wire_TriggerOutput(self,tostring(j),values[tostring(j)])
|
|
|
|
self.ValuesTable[j] = values[tostring(j)]
|
|
|
|
// Msg(j.." = "..values[tostring(j)].."\n")
|
2009-01-10 21:04:03 +00:00
|
|
|
end
|
2008-06-14 00:55:20 +00:00
|
|
|
end
|
|
|
|
self:ShowOutput()
|
|
|
|
end
|
|
|
|
function ENT:SReceiveRadio(k,v)
|
|
|
|
if (k == nil || v == nil) then return end
|
|
|
|
Wire_TriggerOutput(self,k,v)
|
2009-01-17 13:17:12 +00:00
|
|
|
self.ValuesTable[tonumber(k)] = v
|
2008-06-14 00:55:20 +00:00
|
|
|
self:ShowOutput()
|
|
|
|
end
|
|
|
|
function ENT:ShowOutput()
|
|
|
|
if (self.Old == true) then
|
|
|
|
self:SetOverlayText( "(Channel " .. self.Channel .. ") Transmit A: " .. (self.Inputs.A.Value or 0) .. " B: " .. (self.Inputs.B.Value or 0) .. " C: " .. (self.Inputs.C.Value or 0) .. " D: " .. (self.Inputs.D.Value or 0) .. "\nReceive A: " .. (self.Outputs.A.Value or 0) .. " B: " .. (self.Outputs.B.Value or 0) .. " C: " .. (self.Outputs.C.Value or 0) .. " D: " .. (self.Outputs.D.Value or 0) )
|
|
|
|
else
|
|
|
|
local overlay = "(Channel " .. self.Channel .. ") Transmit"
|
|
|
|
for i=1,self.Values do
|
|
|
|
if (k!= "Channel") then if (self.Outputs[tostring(i)] != nil) then overlay = overlay .. " " .. (tostring(i) or "Error") .. ":" .. math.Round((self.Inputs[tostring(i)].Value or 0)*1000)/1000 end end
|
|
|
|
end
|
|
|
|
overlay = overlay .. "\nReceive"
|
|
|
|
for i=1,self.Values do
|
|
|
|
if (self.Outputs[tostring(i)] != nil) then overlay = overlay .. " " .. (tostring(i) or "Error") .. ":" .. math.Round((self.Outputs[tostring(i)].Value or 0)*1000)/1000 end
|
|
|
|
end
|
|
|
|
if (self.Secure == true) then overlay = overlay .. "\nSecured" end
|
|
|
|
self:SetOverlayText( overlay )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ENT:OnRestore()
|
2009-01-17 13:17:12 +00:00
|
|
|
self.BaseClass.OnRestore(self)
|
2008-06-14 00:55:20 +00:00
|
|
|
Radio_Register(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ENT:OnRemove()
|
|
|
|
if (!self.Channel) then return end
|
2009-01-17 13:17:12 +00:00
|
|
|
Radio_TuneOut(self,self.Channel)
|
2008-06-14 00:55:20 +00:00
|
|
|
end
|