Skip to content

Commit

Permalink
trigger_entity_iterator
Browse files Browse the repository at this point in the history
- crash upon using delay_between_runs > 0
  • Loading branch information
Mikk155 committed Mar 28, 2024
1 parent 175e43f commit 397a9a0
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/game/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ target_include_directories(server PRIVATE
../shared/saverestore
../shared/utils
../../public)

target_compile_definitions(server PRIVATE
VALVE_DLL)

Expand Down Expand Up @@ -54,10 +54,10 @@ target_sources(server PRIVATE
vector.h
voice_gamemgr.cpp
voice_gamemgr.h

bot/BotSystem.cpp
bot/BotSystem.h

config/CommandWhitelist.cpp
config/CommandWhitelist.h
config/ConditionEvaluator.cpp
Expand All @@ -77,7 +77,7 @@ target_sources(server PRIVATE
config/sections/HudReplacementSection.h
config/sections/SpawnInventorySection.h
config/sections/SuitLightTypeSection.h

entities/animating.cpp
entities/animation.cpp
entities/animation.h
Expand Down Expand Up @@ -135,11 +135,13 @@ target_sources(server PRIVATE
entities/subs.cpp
entities/teleport_entities.cpp
entities/trains.h
entities/trigger_entity_iterator.cpp
entities/trigger_entity_iterator.h
entities/triggers.cpp
entities/world.cpp
entities/world.h
entities/xen.cpp

entities/ctf/CHUDIconTrigger.cpp
entities/ctf/CHUDIconTrigger.h
entities/ctf/ctf_goals.cpp
Expand All @@ -151,7 +153,7 @@ target_sources(server PRIVATE
entities/ctf/CTFDetect.h
entities/ctf/CTFSpawn.cpp
entities/ctf/CTFSpawn.h

entities/items/airtank.cpp
entities/items/CAmmoAll.cpp
entities/items/CAmmoGeneric.cpp
Expand Down Expand Up @@ -180,7 +182,7 @@ target_sources(server PRIVATE
entities/items/weapons/CSpore.cpp
entities/items/weapons/CSpore.h
entities/items/weapons/weapons.cpp

entities/rope/CElectrifiedWire.cpp
entities/rope/CElectrifiedWire.h
entities/rope/CRope.cpp
Expand All @@ -189,13 +191,13 @@ target_sources(server PRIVATE
entities/rope/CRopeSample.h
entities/rope/CRopeSegment.cpp
entities/rope/CRopeSegment.h

entities/NPCs/basemonster.h
entities/NPCs/combat.cpp
entities/NPCs/defaultai.cpp
entities/NPCs/defaultai.h
entities/NPCs/flyingmonster.cpp
entities/NPCs/flyingmonster.h
entities/NPCs/flyingmonster.h
entities/NPCs/h_ai.cpp
entities/NPCs/monstermaker.cpp
entities/NPCs/monsters.cpp
Expand All @@ -211,7 +213,7 @@ target_sources(server PRIVATE
entities/NPCs/squadmonster.h
entities/NPCs/talkmonster.cpp
entities/NPCs/talkmonster.h

entities/NPCs/aliens/aflock.cpp
entities/NPCs/aliens/agrunt.cpp
entities/NPCs/aliens/barnacle.cpp
Expand All @@ -229,7 +231,7 @@ target_sources(server PRIVATE
entities/NPCs/aliens/leech.cpp
entities/NPCs/aliens/nihilanth.cpp
entities/NPCs/aliens/tentacle.cpp

entities/NPCs/blackmesa/barney.cpp
entities/NPCs/blackmesa/barney.h
entities/NPCs/blackmesa/cleansuit_scientist.cpp
Expand All @@ -238,12 +240,12 @@ target_sources(server PRIVATE
entities/NPCs/blackmesa/rosenberg.cpp
entities/NPCs/blackmesa/scientist.cpp
entities/NPCs/blackmesa/scientist.h

entities/NPCs/blackops/blkop_apache.cpp
entities/NPCs/blackops/blkop_osprey.cpp
entities/NPCs/blackops/hassassin.cpp
entities/NPCs/blackops/male_assassin.cpp

entities/NPCs/military/apache.cpp
entities/NPCs/military/apache.h
entities/NPCs/military/COFSquadTalkMonster.cpp
Expand All @@ -261,14 +263,14 @@ target_sources(server PRIVATE
entities/NPCs/military/osprey.h
entities/NPCs/military/recruit.cpp
entities/NPCs/military/turret.cpp

entities/NPCs/other/genericmonster.cpp
entities/NPCs/other/gman.cpp
entities/NPCs/other/penguin_grenade.cpp
entities/NPCs/other/rat.cpp
entities/NPCs/other/roach.cpp
entities/NPCs/other/skeleton.cpp

entities/NPCs/racex/baby_voltigore.cpp
entities/NPCs/racex/geneworm.cpp
entities/NPCs/racex/pitdrone.cpp
Expand All @@ -277,13 +279,13 @@ target_sources(server PRIVATE
entities/NPCs/racex/shocktrooper.cpp
entities/NPCs/racex/voltigore.cpp
entities/NPCs/racex/voltigore.h

entities/NPCs/zombies/gonome.cpp
entities/NPCs/zombies/zombie.cpp
entities/NPCs/zombies/zombie.h
entities/NPCs/zombies/zombie_barney.cpp
entities/NPCs/zombies/zombie_soldier.cpp

gamerules/CGameRules.cpp
gamerules/CGameRules.h
gamerules/CHalfLifeCoopplay.cpp
Expand All @@ -303,12 +305,12 @@ target_sources(server PRIVATE
gamerules/PlayerInventory.cpp
gamerules/PlayerInventory.h
gamerules/SpawnInventorySystem.h

sound/SentencesSystem.cpp
sound/SentencesSystem.h
sound/ServerSoundSystem.cpp
sound/ServerSoundSystem.h

../../common/ClassData.h
../../common/com_model.h
../../common/common_types.h
Expand Down Expand Up @@ -341,7 +343,7 @@ target_sources(server PRIVATE
../../engine/progs.h
../../engine/shake.h
../../engine/studio.h

../../public/interface.cpp
../../public/interface.h
../../public/steam/steamtypes.h)
Expand Down
199 changes: 199 additions & 0 deletions src/game/server/entities/trigger_entity_iterator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/***
*
* Copyright (c) 1996-2001, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/

#include "cbase.h"
#include "trigger_entity_iterator.h"

BEGIN_DATAMAP( CTriggerEntityIterator )
DEFINE_FUNCTION( IteratorThink ),
DEFINE_FIELD( run_mode, FIELD_INTEGER ),
DEFINE_FIELD( name_filter, FIELD_STRING ),
DEFINE_FIELD( triggerstate, FIELD_INTEGER ),
DEFINE_FIELD( maximum_runs, FIELD_INTEGER ),
DEFINE_FIELD( current_runs, FIELD_INTEGER ),
DEFINE_FIELD( status_filter, FIELD_INTEGER ),
DEFINE_FIELD( classname_filter, FIELD_STRING ),
DEFINE_FIELD( trigger_after_run, FIELD_STRING ),
DEFINE_FIELD( delay_between_runs, FIELD_FLOAT ),
DEFINE_FIELD( delay_between_triggers, FIELD_FLOAT ),
END_DATAMAP();

LINK_ENTITY_TO_CLASS( trigger_entity_iterator, CTriggerEntityIterator );

void CTriggerEntityIterator :: Spawn()
{
pev->solid = SOLID_NOT;

if( FBitSet( pev->spawnflags, TEI_FLAGS::spawnflags::START_ON ) )
{
SetThink( &CTriggerEntityIterator::IteratorThink );
pev->nextthink = gpGlobals->time + 0.1;
}
}

void CTriggerEntityIterator :: IteratorThink()
{
if( run_mode == TEI_FLAGS::run_mode::TOGGLE && ( maximum_runs == 0 || current_runs < maximum_runs ) )
{
pev->nextthink = gpGlobals->time + delay_between_runs;

if( maximum_runs != 0 )
{
current_runs++;
}
}

IteratorFind();
}

bool CTriggerEntityIterator :: KeyValue( KeyValueData* pkvd )
{
if( FStrEq( pkvd->szKeyName, "maximum_runs" ) )
{
maximum_runs = atoi( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "name_filter" ) )
{
name_filter = ALLOC_STRING( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "classname_filter" ) )
{
classname_filter = ALLOC_STRING( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "status_filter" ) )
{
status_filter = atoi( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "triggerstate" ) )
{
triggerstate = atoi( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "run_mode" ) )
{
run_mode = atoi( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "maximum_runs" ) )
{
maximum_runs = atoi( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "delay_between_triggers" ) )
{
delay_between_triggers = atof( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "delay_between_runs" ) )
{
delay_between_runs = atof( pkvd->szValue );
}
else if( FStrEq( pkvd->szKeyName, "trigger_after_run" ) )
{
trigger_after_run = ALLOC_STRING( pkvd->szValue );
}
else
{
return CBaseEntity::KeyValue( pkvd );
}

return true;
}

void CTriggerEntityIterator :: Use( CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value )
{
if( run_mode == TEI_FLAGS::run_mode::TOGGLE )
{
if( &CTriggerEntityIterator::IteratorThink )
{
SetThink( &CTriggerEntityIterator::IteratorThink );
pev->nextthink = gpGlobals->time + 0.1;
}
else
{
SetThink( nullptr );
}
}
else
{
IteratorFind();
}
}

void CTriggerEntityIterator :: IteratorFind()
{
if( !FStringNull( name_filter ) )
{
for( auto pTarget : UTIL_FindEntitiesByTargetname( STRING( name_filter ) ) )
{
if( pTarget )
{
IteratorTrigger( pTarget );
}
}
}
else if( !FStringNull( classname_filter ) )
{
for( auto pTarget : UTIL_FindEntitiesByClassname( STRING( classname_filter ) ) )
{
if( pTarget )
{
IteratorTrigger( pTarget );
}
}
}

flNextDelayTrigger = 0.0;

if( !FStringNull( trigger_after_run ) )
{
FireTargets( STRING( trigger_after_run ), this, this, USE_TOGGLE, 0 );
}
}

void CTriggerEntityIterator :: IteratorTrigger( CBaseEntity* pTarget )
{
if(( !FStringNull( classname_filter ) && !pTarget->ClassnameIs( STRING( classname_filter ) ) )
or ( !FStringNull( name_filter ) && !FStrEq( STRING( pTarget->pev->targetname ), STRING( name_filter ) ) )
or ( status_filter == TEI_FLAGS::status_filter::ONLY_DEAD && pTarget->IsAlive() )
or ( status_filter == TEI_FLAGS::status_filter::ONLY_LIVING && !pTarget->IsAlive() )
) { return; }

USE_TYPE UseType = static_cast<USE_TYPE>( triggerstate );

/*
if( UseType == USE_UNSET )
{
UseType = pTarget->m_UseType;
}
*/

if( delay_between_triggers > 0.0 )
{
flNextDelayTrigger += delay_between_triggers;

// create a temp object to fire at a later time
CBaseDelay* pTemp = g_EntityDictionary->Create<CBaseDelay>("entity_iterator_delayed");

pTemp->pev->nextthink = gpGlobals->time + flNextDelayTrigger;

pTemp->SetThink(&CBaseDelay::DelayThink);

pTemp->pev->button = (int)UseType;
pTemp->pev->target = pev->target;
pTemp->m_hActivator = pTarget;

}
else
{
FireTargets( STRING( pev->target ), pTarget, this, UseType, 0 );
}
}
Loading

0 comments on commit 397a9a0

Please sign in to comment.