Skip to content

Commit

Permalink
Merge pull request #1073 from MattJeanes/optimise
Browse files Browse the repository at this point in the history
TARDIS state & optimisations to be included
  • Loading branch information
MattJeanes authored Jun 8, 2024
2 parents d544e93 + df86dfa commit 57967cb
Show file tree
Hide file tree
Showing 25 changed files with 1,222 additions and 1,070 deletions.
4 changes: 0 additions & 4 deletions lua/entities/gmod_tardis/modules/sh_flight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ function ENT:GetFlight()
return self:GetData("flight",false)
end

function ENT:IsTravelling()
return self:CallHook("IsTravelling")
end

ENT:AddHook("IsTravelling", "flight", function(self)
if self:GetFlight() then
return true
Expand Down
168 changes: 168 additions & 0 deletions lua/entities/gmod_tardis/modules/sh_state.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
-- TARDIS State

-- A list of supported states
TARDIS.States = {
["dead"] = true,

["off"] = true,
["idle"] = true,
["handbrake"] = true,
["travel"] = true,
["demat_abort"] = true,
["demat_fail"] = true,
["mat_fail"] = true,
["takeoff"] = true,
["parking"] = true,

["off_warning"] = true,
["idle_warning"] = true,
["handbrake_warning"] = true,
["travel_warning"] = true,
["demat_abort_warning"] = true,
["demat_fail_warning"] = true,
["mat_fail_warning"] = true,
["takeoff_warning"] = true,
["parking_warning"] = true,
}

-- We add all the hooks here to ensure the integrity of state logic
TARDIS.StateUpdateHooks = {
["PostInitialize"] = true,

["OnHealthDepleted"] = true,
["OnHealthChange"] = true,

["WarningToggled"] = true,
["PowerToggled"] = true,
["HandbrakeToggled"] = true,

["DematStart"] = true,
["MatStart"] = true,
["PreMatStart"] = true,
["StopDemat"] = true,
["StopMat"] = true,
["InterruptTeleport"] = true,
["DematInterrupted"] = true,
["DematFailed"] = true,
["DematFailStopped"] = true,
["MatFailed"] = true,
["MatFailStopped"] = true,

["TakeoffStateToggled"] = true,
["ParkingStateToggled"] = true,
["DematAbortStateToggled"] = true,

["FlightToggled"] = true,
}

-- Adding the hooks
for hook_name, hook_val in pairs(TARDIS.StateUpdateHooks) do
ENT:AddHook(hook_name, "state_update", function(self)
self:UpdateState()
end)
end

--

function ENT:SetState(state)
if not TARDIS.States[state] then
error("Attempted to apply unsupported state " .. state)
end
return self:SetData("state", state, true)
end

function ENT:GetState()
return self:GetData("state")
end

function ENT:SelectState()
local function select_warning(state)
return self:GetWarning() and (state .. "_warning") or state
end

if self:IsDead() then
return "dead", false
end

if not self:GetPower() then
return select_warning("off")
end

if self:IsTravelling() then
if self:GetData("failing-mat") then
return select_warning("mat_fail")
end
if self:GetData("state_takeoff") then
return select_warning("takeoff")
end
return select_warning("travel")
end

if self:GetData("failing-demat") then
return select_warning("demat_fail")
end

if self:GetData("state_demat_abort") then
return select_warning("demat_abort")
end

if self:GetHandbrake() then
return select_warning("handbrake")
end

if self:GetData("state_parking") then
return select_warning("parking")
end

return select_warning("idle")
end

function ENT:UpdateState()
return self:SetState(self:SelectState())
end

--
-- "Travelling" generalisation
--

function ENT:IsTravelling()
return self:CallHook("IsTravelling")
end

--
-- Custom states for visual effects with no functionality
--

local function ProcessTemporaryState(self, time, data_id, hook_name)
if time ~= 0 then
self:SetData(data_id, true)
self:CallHook(hook_name, true)
self:Timer(data_id .. "reset", time, function()
self:SetData(data_id, nil)
self:CallHook(hook_name, false)
end)
end
end

ENT:AddHook("DematStart", "state_takeoff", function(self)
ProcessTemporaryState(self, self.metadata.Timings.TakeOffState, "state_takeoff", "TakeoffStateToggled")
self:UpdateState()
end)

ENT:AddHook("StopMat", "state_parking", function(self)
ProcessTemporaryState(self, self.metadata.Timings.ParkingState, "state_parking", "ParkingStateToggled")
self:UpdateState()
end)

ENT:AddHook("DematInterrupted", "state_demat_abort", function(self)
ProcessTemporaryState(self, self.metadata.Timings.DematAbortState, "state_demat_abort", "DematAbortStateToggled")
self:UpdateState()
end)

-- fixing handbrake + stopmat conflict
ENT:AddHook("HandbrakeToggled", "state_parking", function(self, on)
if on and self:GetData("state_parking") then
self:SetData("state_parking", nil)
self:CancelTimer("state_parking_reset")
end
end)
6 changes: 6 additions & 0 deletions lua/entities/gmod_tardis/modules/teleport/sh_tp_failed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if SERVER then
local time = self.metadata.Timings.DematFail
self:Timer("failed-demat-stop", time, function()
self:SetData("failing-demat", false, true)
self:CallCommonHook("DematFailStopped")
self:CallClientCommonHook("DematFailStopped")
end)
end

Expand Down Expand Up @@ -42,10 +44,13 @@ if SERVER then
end

self:SetData("failing-mat", true, true)
self:CallCommonHook("MatFailed")
self:SendMessage("failed-mat")
local time = self.metadata.Timings.MatFail
self:Timer("failed-mat-stop", time, function()
self:SetData("failing-mat", false, true)
self:CallCommonHook("MatFailStopped")
self:CallClientCommonHook("MatFailStopped")
end)
if callback then callback(false) end
end
Expand Down Expand Up @@ -156,6 +161,7 @@ else -- CLIENT
end)

ENT:OnMessage("failed-mat", function(self, data, ply)
self:CallCommonHook("MatFailed")
if TARDIS:GetSetting("teleport-sound") and TARDIS:GetSetting("sound") then
local ext = self.metadata.Exterior.Sounds.Teleport
local int = self.metadata.Interior.Sounds.Teleport
Expand Down
49 changes: 23 additions & 26 deletions lua/entities/gmod_tardis/modules/teleport/sh_tp_interrupt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ if SERVER then
self:SetData("teleport-interrupt-time", CurTime(), true)
self:SetData("teleport-interrupt-effects", true, true)
else
local time = self.metadata.Timings.DematInterrupt
self:SetData("demat-interrupted", true, true)
self:Timer("interrupt_demat", time, function()
self:SetData("demat-interrupted", nil, true)
end)
self:CallCommonHook("DematInterrupted")
self:CallClientCommonHook("DematInterrupted")
end
end

Expand Down Expand Up @@ -134,32 +131,32 @@ else
end

ENT:AddHook("Think", "tp_interrupt", function(self)
if self:GetData("teleport-interrupted", false) then
local timediff = CurTime() - self:GetData("teleport-interrupt-time")
if not self:GetData("teleport-interrupted", false) then return end

if timediff > 6 and timediff < 6.2 and self:GetData("teleport-interrupt-effects", false) then
self:SetData("teleport-interrupt-effects", false, true)
end
local timediff = CurTime() - self:GetData("teleport-interrupt-time")

local showeffects = (CLIENT and self:GetData("teleport-interrupt-effects", false)
and LocalPlayer():GetTardisData("exterior") == self
and (not LocalPlayer():GetTardisData("thirdperson"))
and TARDIS:GetSetting("breakdown-effects"))
if timediff > 6 and timediff < 6.2 and self:GetData("teleport-interrupt-effects", false) then
self:SetData("teleport-interrupt-effects", false, true)
end

if showeffects then
if math.Round(10 * CurTime()) % 2 == 0 then
self:InteriorSparks(1)
end
if timediff < 0.1 or (timediff > 2 and timediff < 2.1)
or (timediff > 2.6 and timediff < 2.7)
then
self:InteriorExplosion()
end
end
local showeffects = (CLIENT and self:GetData("teleport-interrupt-effects", false)
and LocalPlayer():GetTardisData("exterior") == self
and (not LocalPlayer():GetTardisData("thirdperson"))
and TARDIS:GetSetting("breakdown-effects"))

if timediff > 10 then
self:SetData("teleport-interrupted", false, true)
if showeffects then
if math.Round(10 * CurTime()) % 2 == 0 then
self:InteriorSparks(1)
end
if timediff < 0.1 or (timediff > 2 and timediff < 2.1)
or (timediff > 2.6 and timediff < 2.7)
then
self:InteriorExplosion()
end
end

if timediff > 10 then
self:SetData("teleport-interrupted", false, true)
end
end)

20 changes: 19 additions & 1 deletion lua/entities/gmod_tardis/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,22 @@ function ENT:LoadFolder(folder,addonly,noprefix)
end

ENT:LoadFolder("modules/libraries")
ENT:LoadFolder("modules")

if SERVER then
function ENT:CallClientHook(name, ...)
self:SendMessage("client_hook", {name, ...})
end
function ENT:CallClientCommonHook(name, ...)
self:SendMessage("client_common_hook", {name, ...})
end
else
ENT:OnMessage("client_hook", function(self, data, ply)
self:CallHook(unpack(data))
end)
ENT:OnMessage("client_common_hook", function(self, data, ply)
self:CallCommonHook(unpack(data))
end)
end


ENT:LoadFolder("modules")
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Round things (light sprites for old default interior)

if CLIENT then
function ENT:AddRoundThing(pos)
self.roundthings[pos]=util.GetPixelVisibleHandle()
end

ENT:AddHook("Initialize", "lights-roundthings", function(self)
if self.metadata.Interior.RoundThings then
self.roundthingmat=Material("sprites/light_ignorez")
self.roundthings={}
for k,v in pairs(self.metadata.Interior.RoundThings) do
self:AddRoundThing(v)
end
end
end)

local size=32
ENT:AddHook("Draw", "lights-roundthings", function(self)
if self.roundthings then
if self:CallHook("ShouldDrawLight")==false then return end
for k,v in pairs(self.roundthings) do
local pos = self:LocalToWorld(k)
local vis = util.PixelVisible(pos, 3, v)*255
if vis > 0 then
render.SetMaterial(self.roundthingmat)
render.DrawSprite(pos, size, size, Color(255,153,0, vis))
end
end
end
end)
end
Loading

0 comments on commit 57967cb

Please sign in to comment.