doom3-bfg/neo/d3xp/Trigger.cpp

1555 lines
32 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "../idlib/precompiled.h"
#pragma hdrstop
#include "Game_local.h"
/*
===============================================================================
idTrigger
2012-11-26 18:58:24 +00:00
===============================================================================
*/
const idEventDef EV_Enable( "enable", NULL );
const idEventDef EV_Disable( "disable", NULL );
CLASS_DECLARATION( idEntity, idTrigger )
EVENT( EV_Enable, idTrigger::Event_Enable )
EVENT( EV_Disable, idTrigger::Event_Disable )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger::DrawDebugInfo
================
*/
void idTrigger::DrawDebugInfo()
{
2012-11-26 18:58:24 +00:00
idMat3 axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
idVec3 up = axis[ 2 ] * 5.0f;
idBounds viewTextBounds( gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin() );
idBounds viewBounds( gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin() );
idBounds box( idVec3( -4.0f, -4.0f, -4.0f ), idVec3( 4.0f, 4.0f, 4.0f ) );
idEntity* ent;
idEntity* target;
2012-11-26 18:58:24 +00:00
int i;
bool show;
const function_t* func;
2012-11-26 18:58:24 +00:00
viewTextBounds.ExpandSelf( 128.0f );
viewBounds.ExpandSelf( 512.0f );
for( ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() )
{
if( ent->GetPhysics()->GetContents() & ( CONTENTS_TRIGGER | CONTENTS_FLASHLIGHT_TRIGGER ) )
{
2012-11-26 18:58:24 +00:00
show = viewBounds.IntersectsBounds( ent->GetPhysics()->GetAbsBounds() );
if( !show )
{
for( i = 0; i < ent->targets.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
target = ent->targets[ i ].GetEntity();
if( target != NULL && viewBounds.IntersectsBounds( target->GetPhysics()->GetAbsBounds() ) )
{
2012-11-26 18:58:24 +00:00
show = true;
break;
}
}
}
if( !show )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
gameRenderWorld->DebugBounds( colorOrange, ent->GetPhysics()->GetAbsBounds() );
if( viewTextBounds.IntersectsBounds( ent->GetPhysics()->GetAbsBounds() ) )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->DrawText( ent->name.c_str(), ent->GetPhysics()->GetAbsBounds().GetCenter(), 0.1f, colorWhite, axis, 1 );
gameRenderWorld->DrawText( ent->GetEntityDefName(), ent->GetPhysics()->GetAbsBounds().GetCenter() + up, 0.1f, colorWhite, axis, 1 );
if( ent->IsType( idTrigger::Type ) )
{
func = static_cast<idTrigger*>( ent )->GetScriptFunction();
}
else
{
2012-11-26 18:58:24 +00:00
func = NULL;
}
if( func )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->DrawText( va( "call script '%s'", func->Name() ), ent->GetPhysics()->GetAbsBounds().GetCenter() - up, 0.1f, colorWhite, axis, 1 );
}
}
for( i = 0; i < ent->targets.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
target = ent->targets[ i ].GetEntity();
if( target )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->DebugArrow( colorYellow, ent->GetPhysics()->GetAbsBounds().GetCenter(), target->GetPhysics()->GetOrigin(), 10, 0 );
gameRenderWorld->DebugBounds( colorGreen, box, target->GetPhysics()->GetOrigin() );
if( viewTextBounds.IntersectsBounds( target->GetPhysics()->GetAbsBounds() ) )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->DrawText( target->name.c_str(), target->GetPhysics()->GetAbsBounds().GetCenter(), 0.1f, colorWhite, axis, 1 );
}
}
}
}
}
}
/*
================
idTrigger::Enable
================
*/
void idTrigger::Enable()
{
2012-11-26 18:58:24 +00:00
GetPhysics()->SetContents( CONTENTS_TRIGGER );
GetPhysics()->EnableClip();
}
/*
================
idTrigger::Disable
================
*/
void idTrigger::Disable()
{
2012-11-26 18:58:24 +00:00
// we may be relinked if we're bound to another object, so clear the contents as well
GetPhysics()->SetContents( 0 );
GetPhysics()->DisableClip();
}
/*
================
idTrigger::CallScript
================
*/
void idTrigger::CallScript() const
{
idThread* thread;
if( scriptFunction )
{
2012-11-26 18:58:24 +00:00
thread = new idThread( scriptFunction );
thread->DelayedStart( 0 );
}
}
/*
================
idTrigger::GetScriptFunction
================
*/
const function_t* idTrigger::GetScriptFunction() const
{
2012-11-26 18:58:24 +00:00
return scriptFunction;
}
/*
================
idTrigger::Save
================
*/
void idTrigger::Save( idSaveGame* savefile ) const
{
if( scriptFunction )
{
2012-11-26 18:58:24 +00:00
savefile->WriteString( scriptFunction->Name() );
}
else
{
2012-11-26 18:58:24 +00:00
savefile->WriteString( "" );
}
}
/*
================
idTrigger::Restore
================
*/
void idTrigger::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
idStr funcname;
savefile->ReadString( funcname );
if( funcname.Length() )
{
2012-11-26 18:58:24 +00:00
scriptFunction = gameLocal.program.FindFunction( funcname );
if( scriptFunction == NULL )
{
gameLocal.Warning( "idTrigger_Multi '%s' at (%s) calls unknown function '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ), funcname.c_str() );
2012-11-26 18:58:24 +00:00
}
}
else
{
2012-11-26 18:58:24 +00:00
scriptFunction = NULL;
}
}
/*
================
idTrigger::Event_Enable
================
*/
void idTrigger::Event_Enable()
{
2012-11-26 18:58:24 +00:00
Enable();
}
/*
================
idTrigger::Event_Disable
================
*/
void idTrigger::Event_Disable()
{
2012-11-26 18:58:24 +00:00
Disable();
}
/*
================
idTrigger::idTrigger
================
*/
idTrigger::idTrigger()
{
2012-11-26 18:58:24 +00:00
scriptFunction = NULL;
}
/*
================
idTrigger::Spawn
================
*/
void idTrigger::Spawn()
{
2012-11-26 18:58:24 +00:00
GetPhysics()->SetContents( CONTENTS_TRIGGER );
2012-11-26 18:58:24 +00:00
idStr funcname = spawnArgs.GetString( "call", "" );
if( funcname.Length() )
{
2012-11-26 18:58:24 +00:00
scriptFunction = gameLocal.program.FindFunction( funcname );
if( scriptFunction == NULL )
{
gameLocal.Warning( "trigger '%s' at (%s) calls unknown function '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ), funcname.c_str() );
2012-11-26 18:58:24 +00:00
}
}
else
{
2012-11-26 18:58:24 +00:00
scriptFunction = NULL;
}
}
/*
===============================================================================
idTrigger_Multi
2012-11-26 18:58:24 +00:00
===============================================================================
*/
const idEventDef EV_TriggerAction( "<triggerAction>", "e" );
CLASS_DECLARATION( idTrigger, idTrigger_Multi )
EVENT( EV_Touch, idTrigger_Multi::Event_Touch )
EVENT( EV_Activate, idTrigger_Multi::Event_Trigger )
EVENT( EV_TriggerAction, idTrigger_Multi::Event_TriggerAction )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_Multi::idTrigger_Multi
================
*/
idTrigger_Multi::idTrigger_Multi()
{
2012-11-26 18:58:24 +00:00
wait = 0.0f;
random = 0.0f;
delay = 0.0f;
random_delay = 0.0f;
nextTriggerTime = 0;
removeItem = 0;
touchClient = false;
touchOther = false;
triggerFirst = false;
triggerWithSelf = false;
}
/*
================
idTrigger_Multi::Save
================
*/
void idTrigger_Multi::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
savefile->WriteFloat( wait );
savefile->WriteFloat( random );
savefile->WriteFloat( delay );
savefile->WriteFloat( random_delay );
savefile->WriteInt( nextTriggerTime );
savefile->WriteString( requires );
savefile->WriteInt( removeItem );
savefile->WriteBool( touchClient );
savefile->WriteBool( touchOther );
savefile->WriteBool( triggerFirst );
savefile->WriteBool( triggerWithSelf );
}
/*
================
idTrigger_Multi::Restore
================
*/
void idTrigger_Multi::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadFloat( wait );
savefile->ReadFloat( random );
savefile->ReadFloat( delay );
savefile->ReadFloat( random_delay );
savefile->ReadInt( nextTriggerTime );
savefile->ReadString( requires );
savefile->ReadInt( removeItem );
savefile->ReadBool( touchClient );
savefile->ReadBool( touchOther );
savefile->ReadBool( triggerFirst );
savefile->ReadBool( triggerWithSelf );
}
/*
================
idTrigger_Multi::Spawn
"wait" : Seconds between triggerings, 0.5 default, -1 = one time only.
"call" : Script function to call when triggered
"random" wait variance, default is 0
Variable sized repeatable trigger. Must be targeted at one or more entities.
so, the basic time between firing is a random time between
(wait - random) and (wait + random)
================
*/
void idTrigger_Multi::Spawn()
{
2012-11-26 18:58:24 +00:00
spawnArgs.GetFloat( "wait", "0.5", wait );
spawnArgs.GetFloat( "random", "0", random );
spawnArgs.GetFloat( "delay", "0", delay );
spawnArgs.GetFloat( "random_delay", "0", random_delay );
if( random && ( random >= wait ) && ( wait >= 0 ) )
{
2012-11-26 18:58:24 +00:00
random = wait - 1;
gameLocal.Warning( "idTrigger_Multi '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ) );
2012-11-26 18:58:24 +00:00
}
if( random_delay && ( random_delay >= delay ) && ( delay >= 0 ) )
{
2012-11-26 18:58:24 +00:00
random_delay = delay - 1;
gameLocal.Warning( "idTrigger_Multi '%s' at (%s) has random_delay >= delay", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ) );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
spawnArgs.GetString( "requires", "", requires );
spawnArgs.GetInt( "removeItem", "0", removeItem );
spawnArgs.GetBool( "triggerFirst", "0", triggerFirst );
spawnArgs.GetBool( "triggerWithSelf", "0", triggerWithSelf );
if( spawnArgs.GetBool( "anyTouch" ) )
{
2012-11-26 18:58:24 +00:00
touchClient = true;
touchOther = true;
}
else if( spawnArgs.GetBool( "noTouch" ) )
{
2012-11-26 18:58:24 +00:00
touchClient = false;
touchOther = false;
}
else if( spawnArgs.GetBool( "noClient" ) )
{
2012-11-26 18:58:24 +00:00
touchClient = false;
touchOther = true;
}
else
{
2012-11-26 18:58:24 +00:00
touchClient = true;
touchOther = false;
}
2012-11-26 18:58:24 +00:00
nextTriggerTime = 0;
if( spawnArgs.GetBool( "flashlight_trigger" ) )
{
2012-11-26 18:58:24 +00:00
GetPhysics()->SetContents( CONTENTS_FLASHLIGHT_TRIGGER );
}
else
{
2012-11-26 18:58:24 +00:00
GetPhysics()->SetContents( CONTENTS_TRIGGER );
}
}
/*
================
idTrigger_Multi::CheckFacing
================
*/
bool idTrigger_Multi::CheckFacing( idEntity* activator )
{
if( spawnArgs.GetBool( "facing" ) )
{
if( !activator->IsType( idPlayer::Type ) )
{
2012-11-26 18:58:24 +00:00
return true;
}
idPlayer* player = static_cast< idPlayer* >( activator );
2012-11-26 18:58:24 +00:00
float dot = player->viewAngles.ToForward() * GetPhysics()->GetAxis()[0];
float angle = RAD2DEG( idMath::ACos( dot ) );
if( angle > spawnArgs.GetFloat( "angleLimit", "30" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
return true;
}
/*
================
idTrigger_Multi::TriggerAction
================
*/
void idTrigger_Multi::TriggerAction( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
ActivateTargets( triggerWithSelf ? this : activator );
CallScript();
if( wait >= 0 )
{
2012-11-26 18:58:24 +00:00
nextTriggerTime = gameLocal.time + SEC2MS( wait + random * gameLocal.random.CRandomFloat() );
}
else
{
2012-11-26 18:58:24 +00:00
// we can't just remove (this) here, because this is a touch function
// called while looping through area links...
// If the player spawned inside the trigger, the player Spawn function called Think directly,
// allowing for multiple triggers on a trigger_once. Increasing the nextTriggerTime prevents it.
nextTriggerTime = gameLocal.time + 99999;
PostEventMS( &EV_Remove, 0 );
}
}
/*
================
idTrigger_Multi::Event_TriggerAction
================
*/
void idTrigger_Multi::Event_TriggerAction( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
TriggerAction( activator );
}
/*
================
idTrigger_Multi::Event_Trigger
the trigger was just activated
activated should be the entity that originated the activation sequence (ie. the original target)
activator should be set to the activator so it can be held through a delay
so wait for the delay time before firing
================
*/
void idTrigger_Multi::Event_Trigger( idEntity* activator )
{
if( nextTriggerTime > gameLocal.time )
{
2012-11-26 18:58:24 +00:00
// can't retrigger until the wait is over
return;
}
2012-11-26 18:58:24 +00:00
// see if this trigger requires an item
if( !gameLocal.RequirementMet( activator, requires, removeItem ) )
{
2012-11-26 18:58:24 +00:00
return;
}
if( !CheckFacing( activator ) )
{
2012-11-26 18:58:24 +00:00
return;
}
if( triggerFirst )
{
2012-11-26 18:58:24 +00:00
triggerFirst = false;
return;
}
2012-11-26 18:58:24 +00:00
// don't allow it to trigger twice in a single frame
nextTriggerTime = gameLocal.time + 1;
if( delay > 0 )
{
2012-11-26 18:58:24 +00:00
// don't allow it to trigger again until our delay has passed
nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
PostEventSec( &EV_TriggerAction, delay, activator );
}
else
{
2012-11-26 18:58:24 +00:00
TriggerAction( activator );
}
}
/*
================
idTrigger_Multi::Event_Touch
================
*/
void idTrigger_Multi::Event_Touch( idEntity* other, trace_t* trace )
{
if( common->IsClient() )
{
2012-11-26 18:58:24 +00:00
return;
}
if( triggerFirst )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
bool player = other->IsType( idPlayer::Type );
if( player )
{
if( !touchClient )
{
2012-11-26 18:58:24 +00:00
return;
}
if( static_cast< idPlayer* >( other )->spectating )
{
2012-11-26 18:58:24 +00:00
return;
}
}
else if( !touchOther )
{
2012-11-26 18:58:24 +00:00
return;
}
if( nextTriggerTime > gameLocal.time )
{
2012-11-26 18:58:24 +00:00
// can't retrigger until the wait is over
return;
}
2012-11-26 18:58:24 +00:00
// see if this trigger requires an item
if( !gameLocal.RequirementMet( other, requires, removeItem ) )
{
2012-11-26 18:58:24 +00:00
return;
}
if( !CheckFacing( other ) )
{
2012-11-26 18:58:24 +00:00
return;
}
if( spawnArgs.GetBool( "toggleTriggerFirst" ) )
{
2012-11-26 18:58:24 +00:00
triggerFirst = true;
}
2012-11-26 18:58:24 +00:00
nextTriggerTime = gameLocal.time + 1;
if( delay > 0 )
{
2012-11-26 18:58:24 +00:00
// don't allow it to trigger again until our delay has passed
nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
PostEventSec( &EV_TriggerAction, delay, other );
}
else
{
2012-11-26 18:58:24 +00:00
TriggerAction( other );
}
}
/*
===============================================================================
idTrigger_EntityName
2012-11-26 18:58:24 +00:00
===============================================================================
*/
CLASS_DECLARATION( idTrigger, idTrigger_EntityName )
EVENT( EV_Touch, idTrigger_EntityName::Event_Touch )
EVENT( EV_Activate, idTrigger_EntityName::Event_Trigger )
EVENT( EV_TriggerAction, idTrigger_EntityName::Event_TriggerAction )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_EntityName::idTrigger_EntityName
================
*/
idTrigger_EntityName::idTrigger_EntityName()
{
2012-11-26 18:58:24 +00:00
wait = 0.0f;
random = 0.0f;
delay = 0.0f;
random_delay = 0.0f;
nextTriggerTime = 0;
triggerFirst = false;
testPartialName = false;
}
/*
================
idTrigger_EntityName::Save
================
*/
void idTrigger_EntityName::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
savefile->WriteFloat( wait );
savefile->WriteFloat( random );
savefile->WriteFloat( delay );
savefile->WriteFloat( random_delay );
savefile->WriteInt( nextTriggerTime );
savefile->WriteBool( triggerFirst );
savefile->WriteString( entityName );
savefile->WriteBool( testPartialName );
}
/*
================
idTrigger_EntityName::Restore
================
*/
void idTrigger_EntityName::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadFloat( wait );
savefile->ReadFloat( random );
savefile->ReadFloat( delay );
savefile->ReadFloat( random_delay );
savefile->ReadInt( nextTriggerTime );
savefile->ReadBool( triggerFirst );
savefile->ReadString( entityName );
savefile->ReadBool( testPartialName );
}
/*
================
idTrigger_EntityName::Spawn
================
*/
void idTrigger_EntityName::Spawn()
{
2012-11-26 18:58:24 +00:00
spawnArgs.GetFloat( "wait", "0.5", wait );
spawnArgs.GetFloat( "random", "0", random );
spawnArgs.GetFloat( "delay", "0", delay );
spawnArgs.GetFloat( "random_delay", "0", random_delay );
if( random && ( random >= wait ) && ( wait >= 0 ) )
{
2012-11-26 18:58:24 +00:00
random = wait - 1;
gameLocal.Warning( "idTrigger_EntityName '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ) );
2012-11-26 18:58:24 +00:00
}
if( random_delay && ( random_delay >= delay ) && ( delay >= 0 ) )
{
2012-11-26 18:58:24 +00:00
random_delay = delay - 1;
gameLocal.Warning( "idTrigger_EntityName '%s' at (%s) has random_delay >= delay", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ) );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
spawnArgs.GetBool( "triggerFirst", "0", triggerFirst );
2012-11-26 18:58:24 +00:00
entityName = spawnArgs.GetString( "entityname" );
if( !entityName.Length() )
{
gameLocal.Error( "idTrigger_EntityName '%s' at (%s) doesn't have 'entityname' key specified", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ) );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
nextTriggerTime = 0;
if( !spawnArgs.GetBool( "noTouch" ) )
{
2012-11-26 18:58:24 +00:00
GetPhysics()->SetContents( CONTENTS_TRIGGER );
}
2012-11-26 18:58:24 +00:00
testPartialName = spawnArgs.GetBool( "testPartialName", testPartialName );
}
/*
================
idTrigger_EntityName::TriggerAction
================
*/
void idTrigger_EntityName::TriggerAction( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
ActivateTargets( activator );
CallScript();
if( wait >= 0 )
{
2012-11-26 18:58:24 +00:00
nextTriggerTime = gameLocal.time + SEC2MS( wait + random * gameLocal.random.CRandomFloat() );
}
else
{
2012-11-26 18:58:24 +00:00
// we can't just remove (this) here, because this is a touch function
// called while looping through area links...
nextTriggerTime = gameLocal.time + 1;
PostEventMS( &EV_Remove, 0 );
}
}
/*
================
idTrigger_EntityName::Event_TriggerAction
================
*/
void idTrigger_EntityName::Event_TriggerAction( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
TriggerAction( activator );
}
/*
================
idTrigger_EntityName::Event_Trigger
the trigger was just activated
activated should be the entity that originated the activation sequence (ie. the original target)
activator should be set to the activator so it can be held through a delay
so wait for the delay time before firing
================
*/
void idTrigger_EntityName::Event_Trigger( idEntity* activator )
{
if( nextTriggerTime > gameLocal.time )
{
2012-11-26 18:58:24 +00:00
// can't retrigger until the wait is over
return;
}
2012-11-26 18:58:24 +00:00
bool validEntity = false;
if( activator )
{
if( testPartialName )
{
if( activator->name.Find( entityName, false ) >= 0 )
{
2012-11-26 18:58:24 +00:00
validEntity = true;
}
}
if( activator->name == entityName )
{
2012-11-26 18:58:24 +00:00
validEntity = true;
}
}
if( !validEntity )
{
2012-11-26 18:58:24 +00:00
return;
}
if( triggerFirst )
{
2012-11-26 18:58:24 +00:00
triggerFirst = false;
return;
}
2012-11-26 18:58:24 +00:00
// don't allow it to trigger twice in a single frame
nextTriggerTime = gameLocal.time + 1;
if( delay > 0 )
{
2012-11-26 18:58:24 +00:00
// don't allow it to trigger again until our delay has passed
nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
PostEventSec( &EV_TriggerAction, delay, activator );
}
else
{
2012-11-26 18:58:24 +00:00
TriggerAction( activator );
}
}
/*
================
idTrigger_EntityName::Event_Touch
================
*/
void idTrigger_EntityName::Event_Touch( idEntity* other, trace_t* trace )
{
if( common->IsClient() )
{
2012-11-26 18:58:24 +00:00
return;
}
if( triggerFirst )
{
2012-11-26 18:58:24 +00:00
return;
}
if( nextTriggerTime > gameLocal.time )
{
2012-11-26 18:58:24 +00:00
// can't retrigger until the wait is over
return;
}
2012-11-26 18:58:24 +00:00
bool validEntity = false;
if( other )
{
if( testPartialName )
{
if( other->name.Find( entityName, false ) >= 0 )
{
2012-11-26 18:58:24 +00:00
validEntity = true;
}
}
if( other->name == entityName )
{
2012-11-26 18:58:24 +00:00
validEntity = true;
}
}
if( !validEntity )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
nextTriggerTime = gameLocal.time + 1;
if( delay > 0 )
{
2012-11-26 18:58:24 +00:00
// don't allow it to trigger again until our delay has passed
nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
PostEventSec( &EV_TriggerAction, delay, other );
}
else
{
2012-11-26 18:58:24 +00:00
TriggerAction( other );
}
}
/*
===============================================================================
idTrigger_Timer
2012-11-26 18:58:24 +00:00
===============================================================================
*/
const idEventDef EV_Timer( "<timer>", NULL );
CLASS_DECLARATION( idTrigger, idTrigger_Timer )
EVENT( EV_Timer, idTrigger_Timer::Event_Timer )
EVENT( EV_Activate, idTrigger_Timer::Event_Use )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_Timer::idTrigger_Timer
================
*/
idTrigger_Timer::idTrigger_Timer()
{
2012-11-26 18:58:24 +00:00
random = 0.0f;
wait = 0.0f;
on = false;
delay = 0.0f;
}
/*
================
idTrigger_Timer::Save
================
*/
void idTrigger_Timer::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
savefile->WriteFloat( random );
savefile->WriteFloat( wait );
savefile->WriteBool( on );
savefile->WriteFloat( delay );
savefile->WriteString( onName );
savefile->WriteString( offName );
}
/*
================
idTrigger_Timer::Restore
================
*/
void idTrigger_Timer::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadFloat( random );
savefile->ReadFloat( wait );
savefile->ReadBool( on );
savefile->ReadFloat( delay );
savefile->ReadString( onName );
savefile->ReadString( offName );
}
/*
================
idTrigger_Timer::Spawn
Repeatedly fires its targets.
Can be turned on or off by using.
================
*/
void idTrigger_Timer::Spawn()
{
2012-11-26 18:58:24 +00:00
spawnArgs.GetFloat( "random", "1", random );
spawnArgs.GetFloat( "wait", "1", wait );
spawnArgs.GetBool( "start_on", "0", on );
spawnArgs.GetFloat( "delay", "0", delay );
onName = spawnArgs.GetString( "onName" );
offName = spawnArgs.GetString( "offName" );
if( random >= wait && wait >= 0 )
{
2012-11-26 18:58:24 +00:00
random = wait - 0.001;
gameLocal.Warning( "idTrigger_Timer '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ) );
2012-11-26 18:58:24 +00:00
}
if( on )
{
2012-11-26 18:58:24 +00:00
PostEventSec( &EV_Timer, delay );
}
}
/*
================
idTrigger_Timer::Enable
================
*/
void idTrigger_Timer::Enable()
{
2012-11-26 18:58:24 +00:00
// if off, turn it on
if( !on )
{
2012-11-26 18:58:24 +00:00
on = true;
PostEventSec( &EV_Timer, delay );
}
}
/*
================
idTrigger_Timer::Disable
================
*/
void idTrigger_Timer::Disable()
{
2012-11-26 18:58:24 +00:00
// if on, turn it off
if( on )
{
2012-11-26 18:58:24 +00:00
on = false;
CancelEvents( &EV_Timer );
}
}
/*
================
idTrigger_Timer::Event_Timer
================
*/
void idTrigger_Timer::Event_Timer()
{
2012-11-26 18:58:24 +00:00
ActivateTargets( this );
2012-11-26 18:58:24 +00:00
// set time before next firing
if( wait >= 0.0f )
{
2012-11-26 18:58:24 +00:00
PostEventSec( &EV_Timer, wait + gameLocal.random.CRandomFloat() * random );
}
}
/*
================
idTrigger_Timer::Event_Use
================
*/
void idTrigger_Timer::Event_Use( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
// if on, turn it off
if( on )
{
if( offName.Length() && offName.Icmp( activator->GetName() ) )
{
2012-11-26 18:58:24 +00:00
return;
}
on = false;
CancelEvents( &EV_Timer );
}
else
{
2012-11-26 18:58:24 +00:00
// turn it on
if( onName.Length() && onName.Icmp( activator->GetName() ) )
{
2012-11-26 18:58:24 +00:00
return;
}
on = true;
PostEventSec( &EV_Timer, delay );
}
}
/*
===============================================================================
idTrigger_Count
2012-11-26 18:58:24 +00:00
===============================================================================
*/
CLASS_DECLARATION( idTrigger, idTrigger_Count )
EVENT( EV_Activate, idTrigger_Count::Event_Trigger )
EVENT( EV_TriggerAction, idTrigger_Count::Event_TriggerAction )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_Count::idTrigger_Count
================
*/
idTrigger_Count::idTrigger_Count()
{
2012-11-26 18:58:24 +00:00
goal = 0;
count = 0;
delay = 0.0f;
}
/*
================
idTrigger_Count::Save
================
*/
void idTrigger_Count::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
savefile->WriteInt( goal );
savefile->WriteInt( count );
savefile->WriteFloat( delay );
}
/*
================
idTrigger_Count::Restore
================
*/
void idTrigger_Count::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadInt( goal );
savefile->ReadInt( count );
savefile->ReadFloat( delay );
}
/*
================
idTrigger_Count::Spawn
================
*/
void idTrigger_Count::Spawn()
{
2012-11-26 18:58:24 +00:00
spawnArgs.GetInt( "count", "1", goal );
spawnArgs.GetFloat( "delay", "0", delay );
count = 0;
}
/*
================
idTrigger_Count::Event_Trigger
================
*/
void idTrigger_Count::Event_Trigger( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
// goal of -1 means trigger has been exhausted
if( goal >= 0 )
{
2012-11-26 18:58:24 +00:00
count++;
if( count >= goal )
{
if( spawnArgs.GetBool( "repeat" ) )
{
2012-11-26 18:58:24 +00:00
count = 0;
}
else
{
2012-11-26 18:58:24 +00:00
goal = -1;
}
PostEventSec( &EV_TriggerAction, delay, activator );
}
}
}
/*
================
idTrigger_Count::Event_TriggerAction
================
*/
void idTrigger_Count::Event_TriggerAction( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
ActivateTargets( activator );
CallScript();
if( goal == -1 )
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_Remove, 0 );
}
}
/*
===============================================================================
idTrigger_Hurt
2012-11-26 18:58:24 +00:00
===============================================================================
*/
CLASS_DECLARATION( idTrigger, idTrigger_Hurt )
EVENT( EV_Touch, idTrigger_Hurt::Event_Touch )
EVENT( EV_Activate, idTrigger_Hurt::Event_Toggle )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_Hurt::idTrigger_Hurt
================
*/
idTrigger_Hurt::idTrigger_Hurt()
{
2012-11-26 18:58:24 +00:00
on = false;
delay = 0.0f;
nextTime = 0;
}
/*
================
idTrigger_Hurt::Save
================
*/
void idTrigger_Hurt::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
savefile->WriteBool( on );
savefile->WriteFloat( delay );
savefile->WriteInt( nextTime );
}
/*
================
idTrigger_Hurt::Restore
================
*/
void idTrigger_Hurt::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadBool( on );
savefile->ReadFloat( delay );
savefile->ReadInt( nextTime );
}
/*
================
idTrigger_Hurt::Spawn
Damages activator
Can be turned on or off by using.
================
*/
void idTrigger_Hurt::Spawn()
{
2012-11-26 18:58:24 +00:00
spawnArgs.GetBool( "on", "1", on );
spawnArgs.GetFloat( "delay", "1.0", delay );
nextTime = gameLocal.time;
Enable();
}
/*
================
idTrigger_Hurt::Event_Touch
================
*/
void idTrigger_Hurt::Event_Touch( idEntity* other, trace_t* trace )
{
const char* damage;
if( common->IsClient() )
{
2012-11-26 18:58:24 +00:00
return;
}
if( on && other && gameLocal.time >= nextTime )
{
2012-11-26 18:58:24 +00:00
bool playerOnly = spawnArgs.GetBool( "playerOnly" );
if( playerOnly )
{
if( !other->IsType( idPlayer::Type ) )
{
2012-11-26 18:58:24 +00:00
return;
}
}
damage = spawnArgs.GetString( "def_damage", "damage_painTrigger" );
2012-11-26 18:58:24 +00:00
idVec3 dir = vec3_origin;
if( spawnArgs.GetBool( "kick_from_center", "0" ) )
{
2012-11-26 18:58:24 +00:00
dir = other->GetPhysics()->GetOrigin() - GetPhysics()->GetOrigin();
dir.Normalize();
}
other->Damage( NULL, NULL, dir, damage, 1.0f, INVALID_JOINT );
2012-11-26 18:58:24 +00:00
ActivateTargets( other );
CallScript();
2012-11-26 18:58:24 +00:00
nextTime = gameLocal.time + SEC2MS( delay );
}
}
/*
================
idTrigger_Hurt::Event_Toggle
================
*/
void idTrigger_Hurt::Event_Toggle( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
on = !on;
}
/*
===============================================================================
idTrigger_Fade
===============================================================================
*/
CLASS_DECLARATION( idTrigger, idTrigger_Fade )
EVENT( EV_Activate, idTrigger_Fade::Event_Trigger )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_Fade::Event_Trigger
================
*/
void idTrigger_Fade::Event_Trigger( idEntity* activator )
{
2012-11-26 18:58:24 +00:00
idVec4 fadeColor;
int fadeTime;
idPlayer* player;
2012-11-26 18:58:24 +00:00
player = gameLocal.GetLocalPlayer();
if( player )
{
2012-11-26 18:58:24 +00:00
fadeColor = spawnArgs.GetVec4( "fadeColor", "0, 0, 0, 1" );
fadeTime = SEC2MS( spawnArgs.GetFloat( "fadeTime", "0.5" ) );
player->playerView.Fade( fadeColor, fadeTime );
PostEventMS( &EV_ActivateTargets, fadeTime, activator );
}
}
/*
===============================================================================
idTrigger_Touch
2012-11-26 18:58:24 +00:00
===============================================================================
*/
CLASS_DECLARATION( idTrigger, idTrigger_Touch )
EVENT( EV_Activate, idTrigger_Touch::Event_Trigger )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idTrigger_Touch::idTrigger_Touch
================
*/
idTrigger_Touch::idTrigger_Touch()
{
2012-11-26 18:58:24 +00:00
clipModel = NULL;
}
/*
================
idTrigger_Touch::Spawn
================
*/
void idTrigger_Touch::Spawn()
{
2012-11-26 18:58:24 +00:00
// get the clip model
clipModel = new( TAG_THREAD ) idClipModel( GetPhysics()->GetClipModel() );
2012-11-26 18:58:24 +00:00
// remove the collision model from the physics object
GetPhysics()->SetClipModel( NULL, 1.0f );
if( spawnArgs.GetBool( "start_on" ) )
{
2012-11-26 18:58:24 +00:00
BecomeActive( TH_THINK );
}
}
/*
================
idTrigger_Touch::Save
================
*/
void idTrigger_Touch::Save( idSaveGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->WriteClipModel( clipModel );
}
/*
================
idTrigger_Touch::Restore
================
*/
void idTrigger_Touch::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadClipModel( clipModel );
}
/*
================
idTrigger_Touch::TouchEntities
================
*/
void idTrigger_Touch::TouchEntities()
{
2012-11-26 18:58:24 +00:00
int numClipModels, i;
idBounds bounds;
idClipModel* cm, *clipModelList[ MAX_GENTITIES ];
if( clipModel == NULL || scriptFunction == NULL )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
bounds.FromTransformedBounds( clipModel->GetBounds(), clipModel->GetOrigin(), clipModel->GetAxis() );
numClipModels = gameLocal.clip.ClipModelsTouchingBounds( bounds, -1, clipModelList, MAX_GENTITIES );
for( i = 0; i < numClipModels; i++ )
{
2012-11-26 18:58:24 +00:00
cm = clipModelList[ i ];
if( !cm->IsTraceModel() )
{
2012-11-26 18:58:24 +00:00
continue;
}
idEntity* entity = cm->GetEntity();
if( !entity )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( !gameLocal.clip.ContentsModel( cm->GetOrigin(), cm, cm->GetAxis(), -1,
clipModel->Handle(), clipModel->GetOrigin(), clipModel->GetAxis() ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
ActivateTargets( entity );
idThread* thread = new idThread();
2012-11-26 18:58:24 +00:00
thread->CallFunction( entity, scriptFunction, false );
thread->DelayedStart( 0 );
}
}
/*
================
idTrigger_Touch::Think
================
*/
void idTrigger_Touch::Think()
{
if( thinkFlags & TH_THINK )
{
2012-11-26 18:58:24 +00:00
TouchEntities();
}
idEntity::Think();
}
/*
================
idTrigger_Touch::Event_Trigger
================
*/
void idTrigger_Touch::Event_Trigger( idEntity* activator )
{
if( thinkFlags & TH_THINK )
{
2012-11-26 18:58:24 +00:00
BecomeInactive( TH_THINK );
}
else
{
2012-11-26 18:58:24 +00:00
BecomeActive( TH_THINK );
}
}
/*
================
idTrigger_Touch::Enable
================
*/
void idTrigger_Touch::Enable()
{
2012-11-26 18:58:24 +00:00
BecomeActive( TH_THINK );
}
/*
================
idTrigger_Touch::Disable
================
*/
void idTrigger_Touch::Disable()
{
2012-11-26 18:58:24 +00:00
BecomeInactive( TH_THINK );
}
/*
===============================================================================
idTrigger_Flag
===============================================================================
*/
CLASS_DECLARATION( idTrigger_Multi, idTrigger_Flag )
EVENT( EV_Touch, idTrigger_Flag::Event_Touch )
2012-11-26 18:58:24 +00:00
END_CLASS
idTrigger_Flag::idTrigger_Flag()
{
2012-11-26 18:58:24 +00:00
team = -1;
player = false;
eventFlag = NULL;
}
void idTrigger_Flag::Spawn()
{
2012-11-26 18:58:24 +00:00
team = spawnArgs.GetInt( "team", "0" );
player = spawnArgs.GetBool( "player", "0" );
2012-11-26 18:58:24 +00:00
idStr funcname = spawnArgs.GetString( "eventflag", "" );
if( funcname.Length() )
{
2012-11-26 18:58:24 +00:00
eventFlag = idEventDef::FindEvent( funcname );// gameLocal.program.FindFunction( funcname );//, &idItemTeam::Type );
if( eventFlag == NULL )
{
gameLocal.Warning( "trigger '%s' at (%s) event unknown '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString( 0 ), funcname.c_str() );
2012-11-26 18:58:24 +00:00
}
}
else
{
2012-11-26 18:58:24 +00:00
eventFlag = NULL;
}
2012-11-26 18:58:24 +00:00
idTrigger_Multi::Spawn();
}
void idTrigger_Flag::Event_Touch( idEntity* other, trace_t* trace )
{
idItemTeam* flag = NULL;
if( common->IsClient() )
{
2012-11-26 18:58:24 +00:00
return;
}
if( player )
{
if( !other->IsType( idPlayer::Type ) )
2012-11-26 18:58:24 +00:00
return;
idPlayer* player = static_cast<idPlayer*>( other );
if( player->carryingFlag == false )
2012-11-26 18:58:24 +00:00
return;
if( team != -1 && ( player->team != team || ( player->team != 0 && player->team != 1 ) ) )
2012-11-26 18:58:24 +00:00
return;
idItemTeam* flags[2];
2012-11-26 18:58:24 +00:00
flags[0] = gameLocal.mpGame.GetTeamFlag( 0 );
flags[1] = gameLocal.mpGame.GetTeamFlag( 1 );
2012-11-26 18:58:24 +00:00
int iFriend = 1 - player->team; // index to the flag player team wants
int iOpp = player->team; // index to the flag opp team wants
2012-11-26 18:58:24 +00:00
// flag is captured if :
// 1)flag is truely bound to the player
// 2)opponent flag has been return
if( flags[iFriend]->carried && !flags[iFriend]->dropped && //flags[iFriend]->IsBoundTo( player ) &&
!flags[iOpp]->carried && !flags[iOpp]->dropped )
2012-11-26 18:58:24 +00:00
flag = flags[iFriend];
else
return;
}
else
{
if( !other->IsType( idItemTeam::Type ) )
2012-11-26 18:58:24 +00:00
return;
idItemTeam* item = static_cast<idItemTeam*>( other );
if( item->team == team || team == -1 )
{
2012-11-26 18:58:24 +00:00
flag = item;
}
else
return;
}
if( flag )
{
switch( eventFlag->GetNumArgs() )
{
2012-11-26 18:58:24 +00:00
default :
case 0 :
flag->PostEventMS( eventFlag, 0 );
break;
2012-11-26 18:58:24 +00:00
case 1 :
flag->PostEventMS( eventFlag, 0, NULL );
break;
2012-11-26 18:58:24 +00:00
case 2 :
flag->PostEventMS( eventFlag, 0, NULL, NULL );
break;
2012-11-26 18:58:24 +00:00
}
/*
ServerSendEvent( eventFlag->GetEventNum(), NULL, true );
idThread *thread;
if ( scriptFlag ) {
thread = new idThread();
thread->CallFunction( flag, scriptFlag, false );
thread->DelayedStart( 0 );
}
*/
2012-11-26 18:58:24 +00:00
idTrigger_Multi::Event_Touch( other, trace );
}
}