Skip to content

Commit

Permalink
Autoreload checkpoints, add data driven vehicle checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed Jun 23, 2024
1 parent f793e98 commit 08e2ed4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
6 changes: 6 additions & 0 deletions entities/entities/lambda_checkpoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,20 @@ function ENT:Think()
end
end

-- TODO: Remove all instances, use SetRenderPos
function ENT:SetVisiblePos(pos)
self:SetNWVector("VisiblePos", pos)
end

ENT.SetRenderPos = ENT.SetVisiblePos

-- TODO: Remove all instances, use GetRenderPos
function ENT:GetVisiblePos()
return self:GetNWVector("VisiblePos", self:GetPos())
end

ENT.GetRenderPos = ENT.GetVisiblePos

function ENT:SetDynamicCheckpoint(dynamic)
self:SetNWBool("DynamicCheckpoint", dynamic)
end
Expand Down
16 changes: 16 additions & 0 deletions gamemode/gametypes/hl2ep2/mapscripts/ep2_outland_07.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ MAPSCRIPT.GlobalStates = {
}

MAPSCRIPT.Checkpoints = {
{
Pos = Vector(-9988, -10067, 129),
Ang = Angle(0, 0, 0),
RenderPos = Vector(-9846, -10569, 114),
Trigger = {
Pos = Vector(-10506, -11740, 151),
Mins = Vector(-500, -50, 0),
Maxs = Vector(1500, 50, 200)
},
Vehicle = {
Pos = Vector(-9846, -10604, 118.5),
Ang = Angle(0, -100, 0),
}
},
}

function MAPSCRIPT:PostInit()
Expand Down Expand Up @@ -96,6 +110,8 @@ function MAPSCRIPT:PostInit()

-- Resize the trigger and make it wait for all players.
ents.WaitForEntityByName("trigger_alyx_start_advisor_scene", function(ent)
ent:Remove()
local ent = ents.Create("trigger_once")

Check warning on line 114 in gamemode/gametypes/hl2ep2/mapscripts/ep2_outland_07.lua

View workflow job for this annotation

GitHub Actions / Lint / lint

"Shadowing"

Variable 'ent' shadows existing binding, defined at line 112, column 75
ent:SetupTrigger(
Vector(-9600, -9708, 125),
Angle(0, 0, 0),
Expand Down
5 changes: 2 additions & 3 deletions gamemode/sh_vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ if SERVER then
end

function GM:SpawnVehicleAtSpot(vehicle)
local pos = util.StringToType(vehicle["origin"], "Vector")
if self.VehicleCheckpoint ~= nil then pos = self.VehicleCheckpoint.Pos end
local pos = self:GetVehicleSpawnPos()
-- Check if there is already one.
local nearbyEnts = ents.FindInBox(pos + VEHICLE_SPAWN_MINS, pos + VEHICLE_SPAWN_MAXS)
for _, v in pairs(nearbyEnts) do
Expand All @@ -442,7 +441,7 @@ if SERVER then
-- The box is somewhat big, we should deal with players standing directly in the way.
end

DbgPrint("Spawning vehicle...")
DbgPrint("Spawning vehicle at spot: " .. tostring(pos))
local newVehicle = ents.CreateFromData(vehicle)
if self.VehicleCheckpoint ~= nil then
newVehicle:SetPos(self.VehicleCheckpoint.Pos)
Expand Down
4 changes: 4 additions & 0 deletions gamemode/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ function GM:OnReloaded()
self:SetGameType(lambda_gametype:GetString())
self:InitSettings()
self:InitializeDifficulty()

if SERVER then
self:ReloadCheckpoints()
end
end

function GM:Tick()
Expand Down
51 changes: 50 additions & 1 deletion gamemode/sv_checkpoints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function GM:ResetCheckpoints()
self.CurrentCheckpoint = nil
self.CurrentCheckpointPos = nil
self.NextCheckpointTest = nil
self.CheckpointInstances = {}
end

function GM:CreatePlayerCheckpoint(data)
Expand Down Expand Up @@ -112,6 +113,16 @@ local function CreateCheckpointWithTrigger(data)
end
end

local vehicleData = data.Vehicle
if vehicleData ~= nil then
if vehicleData.Pos == nil then
error("Missing Vehicle.Pos in data")
end
if vehicleData.Ang == nil then
error("Missing Vehicle.Ang in data")
end
end

local triggerData = data.Trigger

if triggerData == nil then
Expand All @@ -131,12 +142,20 @@ local function CreateCheckpointWithTrigger(data)
end

local checkpoint = GAMEMODE:CreateCheckpoint(data.Pos, data.Ang)
if data.RenderPos ~= nil then
checkpoint:SetRenderPos(data.RenderPos)
end

local checkpointTrigger = ents.Create("trigger_once")
checkpointTrigger:SetupTrigger(triggerData.Pos, triggerData.Ang or Angle(0, 0, 0), triggerData.Mins, triggerData.Maxs)

checkpointTrigger.OnTrigger = function(_, activator)
GAMEMODE:SetPlayerCheckpoint(checkpoint, activator)

if vehicleData ~= nil then
GAMEMODE:SetVehicleCheckpoint(vehicleData.Pos, vehicleData.Ang)
end

local mapscript = GAMEMODE:GetMapScript()
if mapscript ~= nil and mapscript.DefaultLoadout ~= nil then
local loadoutWeapons = mapscript.DefaultLoadout.Weapons
Expand All @@ -160,11 +179,41 @@ local function CreateCheckpointWithTrigger(data)
end
end
end

local checkpointData = {
Checkpoint = checkpoint,
Trigger = checkpointTrigger
}

return checkpointData
end

function GM:CreateCheckpointsFromData(data)
for _, cp in pairs(data) do
CreateCheckpointWithTrigger(cp)
local instance = CreateCheckpointWithTrigger(cp)
if instance ~= nil then
table.insert(self.CheckpointInstances, instance)
end
end
end

function GM:ReloadCheckpoints()
local mapScript = self:GetMapScript()
if mapScript == nil then return end

-- Remove old ones.
self.CheckpointInstances = self.CheckpointInstances or {}
for _, v in pairs(self.CheckpointInstances) do
if IsValid(v.Trigger) then
v.Trigger:Remove()
end
if IsValid(v.Checkpoint) then
v.Checkpoint:Remove()
end
end

if mapScript.Checkpoints ~= nil then
self:CreateCheckpointsFromData(mapScript.Checkpoints)
end
end

Expand Down

0 comments on commit 08e2ed4

Please sign in to comment.