Skip to content

Commit

Permalink
Use new vscript events system
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikusch committed Nov 10, 2024
1 parent e426c3c commit 6e4675c
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 110 deletions.
60 changes: 10 additions & 50 deletions scripts/vscripts/chaos.nut
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,20 @@ IncludeScript("chaos_util")
const CHAOS_SCOPE_PREFIX = "CHAOS_"
const CHAOS_LOG_PREFIX = "[TF2 Chaos VScript] "

::Chaos_GameEventCallbacks <- {}
::Chaos_ScriptHookCallbacks <- {}

::__RunGameEventCallbacks <- function(event, params)
{
__RunEventCallbacks(event, params, "Chaos_OnGameEvent_", "Chaos_GameEventCallbacks", false)

if ("GameEventCallbacks" in getroottable())
__RunEventCallbacks(event, params, "OnGameEvent_", "GameEventCallbacks", true)
}

::__RunScriptHookCallbacks <- function(event, params)
{
__RunEventCallbacks(event, params, "Chaos_OnScriptHook_", "Chaos_ScriptHookCallbacks", false)

if ("ScriptHookCallbacks" in getroottable())
__RunEventCallbacks(event, params, "OnScriptHook_", "ScriptHookCallbacks", true)
}

::Chaos_CollectEventCallbacks <- function(scope)
{
__CollectEventCallbacks(scope, "Chaos_OnGameEvent_", "Chaos_GameEventCallbacks", ::RegisterScriptGameEventListener)
__CollectEventCallbacks(scope, "Chaos_OnScriptHook_", "Chaos_ScriptHookCallbacks", ::RegisterScriptHookListener)
}

ChaosEffectScopes <- {}

function Chaos_StartEffect(name, duration)
{
local scope_name = CHAOS_SCOPE_PREFIX + name
if (scope_name in ChaosEffectScopes)
if (scope_name in ROOT)
{
printf(CHAOS_LOG_PREFIX + "Attempted to start effect '%s' that is already started, restarting...\n", name)
Chaos_EndEffect(name)
}

getroottable()[scope_name] <- {}
local scope = getroottable()[scope_name]
ROOT[scope_name] <- {}
local scope = ROOT[scope_name]

IncludeScript("chaos/effects/" + name.tolower(), scope)
__CollectGameEventCallbacks(scope)

scope.Chaos_EffectName <- CHAOS_SCOPE_PREFIX + name

Expand All @@ -58,7 +32,7 @@ function Chaos_StartEffect(name, duration)
printf(CHAOS_LOG_PREFIX + "Starting effect '%s'\n", name)

if (duration > 0)
ChaosEffectScopes[scope_name] <- scope
ROOT[scope_name] <- scope
}
else
{
Expand All @@ -71,10 +45,10 @@ function Chaos_StartEffect(name, duration)
function Chaos_UpdateEffect(name)
{
local scope_name = CHAOS_SCOPE_PREFIX + name
if (!(scope_name in ChaosEffectScopes))
if (!(scope_name in ROOT))
return

local scope = ChaosEffectScopes[scope_name]
local scope = ROOT[scope_name]
if (scope == null)
return

Expand All @@ -89,13 +63,13 @@ function Chaos_EndEffect(name)
printf(CHAOS_LOG_PREFIX + "Stopping effect '%s'\n", name)

local scope_name = CHAOS_SCOPE_PREFIX + name
if (!(scope_name in ChaosEffectScopes))
if (!(scope_name in ROOT))
{
printf(CHAOS_LOG_PREFIX + "Effect '%s' not found in scope list!\n", name)
return false
}

local scope = ChaosEffectScopes[scope_name]
local scope = ROOT[scope_name]
if (scope == null)
{
printf(CHAOS_LOG_PREFIX + "Effect '%s' scope was deleted early!\n", name)
Expand All @@ -105,21 +79,7 @@ function Chaos_EndEffect(name)
if ("ChaosEffect_OnEnd" in scope)
scope.ChaosEffect_OnEnd()

foreach (event, scopes in Chaos_GameEventCallbacks)
{
local index = scopes.find(scope)
if (index != null)
scopes.remove(index)
}

foreach (hook, scopes in Chaos_ScriptHookCallbacks)
{
local index = scopes.find(scope)
if (index != null)
scopes.remove(index)
}

delete ChaosEffectScopes[scope_name]
delete ROOT[scope_name]

return true
}
8 changes: 3 additions & 5 deletions scripts/vscripts/chaos/effects/callouts.nut
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Chaos_OnGameEvent_player_spawn(params)
function OnGameEvent_player_spawn(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand Down Expand Up @@ -26,7 +26,7 @@ function Chaos_OnGameEvent_player_spawn(params)
}
}

function Chaos_OnGameEvent_player_death(params)
function OnGameEvent_player_death(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand All @@ -50,6 +50,4 @@ function Chaos_OnGameEvent_player_death(params)
EntFireByHandle(other, "SpeakResponseConcept", "TLK_MVM_DEFENDER_DIED", -1, null, null)
EntFireByHandle(other, "ClearContext", null, -1, null, null)
}
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/chipdamage.nut
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
function Chaos_OnScriptHook_OnTakeDamage(params)
function OnScriptHook_OnTakeDamage(params)
{
// Only modify for CBaseCombatCharacter
if (!NetProps.HasProp(params.const_entity, "m_flNextAttack"))
return

params.damage = 1
}

Chaos_CollectEventCallbacks(this)
}
8 changes: 3 additions & 5 deletions scripts/vscripts/chaos/effects/eyecamera.nut
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function RemoveViewControl(player)
EntFireByHandle(viewcontrol, "Kill", null, -1, null, null)
}

function Chaos_OnGameEvent_player_spawn(params)
function OnGameEvent_player_spawn(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand All @@ -80,7 +80,7 @@ function Chaos_OnGameEvent_player_spawn(params)
player.GetScriptScope().viewcontrol <- CreateViewControl(player)
}

function Chaos_OnGameEvent_player_death(params)
function OnGameEvent_player_death(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand All @@ -90,6 +90,4 @@ function Chaos_OnGameEvent_player_death(params)
return

RemoveViewControl(player)
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/homingprojectiles.nut
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function ChaosEffect_OnEnd()
projectile.SetLocalAngles(move_ang)
}

function Chaos_OnScriptHook_OnTakeDamage(params)
function OnScriptHook_OnTakeDamage(params)
{
if (params.const_entity == worldspawn)
return
Expand All @@ -156,6 +156,4 @@ function Chaos_OnScriptHook_OnTakeDamage(params)
return

EntFireByHandle(params.inflictor, "Kill", null, 0.5, null, null)
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/noclip.nut
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ function ChaosEffect_OnEnd()
}
}

function Chaos_OnGameEvent_player_spawn(params)
function OnGameEvent_player_spawn(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
return

EntFireByHandle(player, "RunScriptCode", "self.SetMoveType(MOVETYPE_NOCLIP, MOVECOLLIDE_DEFAULT)", -1, null, null)
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/paintitems.nut
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ function GetRandomColor()
return RandomInt(0, 0x1000000)
}

function Chaos_OnGameEvent_post_inventory_application(params)
function OnGameEvent_post_inventory_application(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
return

ApplyRandomPaintToItems(player)
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/removewearables.nut
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ function RemoveAllWearables(player)
}
}

function Chaos_OnGameEvent_post_inventory_application(params)
function OnGameEvent_post_inventory_application(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
return

RemoveAllWearables(player)
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/thirdperson.nut
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ function ChaosEffect_OnEnd()
}
}

function Chaos_OnGameEvent_player_spawn(params)
function OnGameEvent_player_spawn(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
return

EntFireByHandle(player, "RunScriptCode", "self.SetForcedTauntCam(1)", -1, null, null)
}

Chaos_CollectEventCallbacks(this)
}
8 changes: 3 additions & 5 deletions scripts/vscripts/chaos/effects/tiltedcamera.nut
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function ChaosEffect_OnEnd()
}
}

function Chaos_OnGameEvent_player_spawn(params)
function OnGameEvent_player_spawn(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand All @@ -31,7 +31,7 @@ function Chaos_OnGameEvent_player_spawn(params)
SetEyeAngles(player, 90)
}

function Chaos_OnGameEvent_player_teleported(params)
function OnGameEvent_player_teleported(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand All @@ -45,6 +45,4 @@ function SetEyeAngles(player, z)
local angles = player.EyeAngles()
angles.z = z
player.SnapEyeAngles(angles)
}

Chaos_CollectEventCallbacks(this)
}
6 changes: 2 additions & 4 deletions scripts/vscripts/chaos/effects/uncontrollablerecoil.nut
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function ChaosEffect_Update()
return -1
}

function Chaos_OnGameEvent_player_spawn(params)
function OnGameEvent_player_spawn(params)
{
local player = GetPlayerFromUserID(params.userid)
if (player == null)
Expand All @@ -58,6 +58,4 @@ function Chaos_OnGameEvent_player_spawn(params)

player.ValidateScriptScope()
player.GetScriptScope().prevLastFireTime <- NetProps.GetPropFloat(weapon, "LocalActiveTFWeaponData.m_flLastFireTime")
}

Chaos_CollectEventCallbacks(this)
}
17 changes: 0 additions & 17 deletions scripts/vscripts/chaos_util.nut
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ if (!("ConstantNamingConvention" in ROOT))
ROOT[k] <- v
}

// m_lifeState values
const LIFE_ALIVE = 0
const LIFE_DYING = 1
const LIFE_DEAD = 2
const LIFE_RESPAWNABLE = 3
const LIFE_DISCARDBODY = 4

// settings for m_takedamage
const DAMAGE_NO = 0
const DAMAGE_EVENTS_ONLY = 1
Expand Down Expand Up @@ -50,16 +43,6 @@ const FLT_MAX = 0x7F7FFFFF
::worldspawn <- Entities.FindByClassname(null, "worldspawn")
::gamerules <- Entities.FindByClassname(null, "tf_gamerules")

CTFPlayer.IsAlive <- function()
{
return NetProps.GetPropInt(this, "m_lifeState") == LIFE_ALIVE
}

CTFBot.IsAlive <- function()
{
return NetProps.GetPropInt(this, "m_lifeState") == LIFE_ALIVE
}

::GetEnemyTeam <- function(team)
{
if (team == TF_TEAM_RED)
Expand Down

0 comments on commit 6e4675c

Please sign in to comment.