Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1207 lines
32 KiB

9 years ago
/***
*
* Copyright (c) 1996-2002, 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.
*
* This source code contains proprietary and confidential information of
* Valve LLC and its suppliers. Access to this code is restricted to
* persons who have executed a written SDK license with Valve. Any access,
* use or distribution of this code by or to any unlicensed person is illegal.
*
****/
/*
===== scripted.cpp ========================================================
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#ifndef ANIMATION_H
#include "animation.h"
#endif
#ifndef SAVERESTORE_H
#include "saverestore.h"
#endif
#include "schedule.h"
#include "scripted.h"
#include "defaultai.h"
/*
classname "scripted_sequence"
targetname "me" - there can be more than one with the same name, and they act in concert
target "the_entity_I_want_to_start_playing" or "class entity_classname" will pick the closest inactive scientist
play "name_of_sequence"
idle "name of idle sequence to play before starting"
donetrigger "whatever" - can be any other triggerable entity such as another sequence, train, door, or a special case like "die" or "remove"
moveto - if set the monster first moves to this nodes position
range # - only search this far to find the target
spawnflags - (stop if blocked, stop if player seen)
*/
//
// Cache user-entity-field values until spawn is called.
//
9 years ago
void CCineMonster::KeyValue( KeyValueData *pkvd )
9 years ago
{
9 years ago
if( FStrEq( pkvd->szKeyName, "m_iszIdle" ) )
9 years ago
{
m_iszIdle = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "m_iszPlay" ) )
9 years ago
{
m_iszPlay = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "m_iszEntity" ) )
9 years ago
{
m_iszEntity = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "m_fMoveTo" ) )
9 years ago
{
m_fMoveTo = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "m_flRepeat" ) )
9 years ago
{
m_flRepeat = atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "m_flRadius" ) )
9 years ago
{
m_flRadius = atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "m_iFinishSchedule" ) )
9 years ago
{
m_iFinishSchedule = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
{
CBaseMonster::KeyValue( pkvd );
}
}
TYPEDESCRIPTION CCineMonster::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CCineMonster, m_iszIdle, FIELD_STRING ),
DEFINE_FIELD( CCineMonster, m_iszPlay, FIELD_STRING ),
DEFINE_FIELD( CCineMonster, m_iszEntity, FIELD_STRING ),
DEFINE_FIELD( CCineMonster, m_fMoveTo, FIELD_INTEGER ),
DEFINE_FIELD( CCineMonster, m_flRepeat, FIELD_FLOAT ),
DEFINE_FIELD( CCineMonster, m_flRadius, FIELD_FLOAT ),
DEFINE_FIELD( CCineMonster, m_iDelay, FIELD_INTEGER ),
DEFINE_FIELD( CCineMonster, m_startTime, FIELD_TIME ),
9 years ago
DEFINE_FIELD( CCineMonster, m_saved_movetype, FIELD_INTEGER ),
DEFINE_FIELD( CCineMonster, m_saved_solid, FIELD_INTEGER ),
9 years ago
DEFINE_FIELD( CCineMonster, m_saved_effects, FIELD_INTEGER ),
DEFINE_FIELD( CCineMonster, m_iFinishSchedule, FIELD_INTEGER ),
DEFINE_FIELD( CCineMonster, m_interruptable, FIELD_BOOLEAN ),
};
IMPLEMENT_SAVERESTORE( CCineMonster, CBaseMonster )
9 years ago
LINK_ENTITY_TO_CLASS( scripted_sequence, CCineMonster )
9 years ago
#define CLASSNAME "scripted_sequence"
LINK_ENTITY_TO_CLASS( aiscripted_sequence, CCineAI )
9 years ago
9 years ago
void CCineMonster::Spawn( void )
9 years ago
{
// pev->solid = SOLID_TRIGGER;
9 years ago
// UTIL_SetSize( pev, Vector( -8, -8, -8 ), Vector( 8, 8, 8 ) );
9 years ago
pev->solid = SOLID_NOT;
// REMOVE: The old side-effect
#if 0
9 years ago
if( m_iszIdle )
9 years ago
m_fMoveTo = 4;
#endif
// if no targetname, start now
9 years ago
if( FStringNull( pev->targetname ) || !FStringNull( m_iszIdle ) )
9 years ago
{
SetThink( &CCineMonster::CineThink );
pev->nextthink = gpGlobals->time + 1.0;
// Wait to be used?
9 years ago
if( pev->targetname )
9 years ago
m_startTime = gpGlobals->time + 1E6;
}
9 years ago
if( pev->spawnflags & SF_SCRIPT_NOINTERRUPT )
9 years ago
m_interruptable = FALSE;
else
m_interruptable = TRUE;
}
//=========================================================
// FCanOverrideState - returns FALSE, scripted sequences
// cannot possess entities regardless of state.
//=========================================================
9 years ago
BOOL CCineMonster::FCanOverrideState( void )
9 years ago
{
9 years ago
if( pev->spawnflags & SF_SCRIPT_OVERRIDESTATE )
9 years ago
return TRUE;
return FALSE;
}
//=========================================================
// FCanOverrideState - returns true because scripted AI can
// possess entities regardless of their state.
//=========================================================
9 years ago
BOOL CCineAI::FCanOverrideState( void )
9 years ago
{
return TRUE;
}
//
// CineStart
//
9 years ago
void CCineMonster::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
// do I already know who I should use
9 years ago
CBaseEntity *pEntity = m_hTargetEnt;
CBaseMonster *pTarget = NULL;
9 years ago
9 years ago
if( pEntity )
9 years ago
pTarget = pEntity->MyMonsterPointer();
9 years ago
if( pTarget )
9 years ago
{
// am I already playing the script?
9 years ago
if( pTarget->m_scriptState == SCRIPT_PLAYING )
9 years ago
return;
m_startTime = gpGlobals->time + 0.05;
}
else
{
// if not, try finding them
SetThink( &CCineMonster::CineThink );
pev->nextthink = gpGlobals->time;
}
}
// This doesn't really make sense since only MOVETYPE_PUSH get 'Blocked' events
9 years ago
void CCineMonster::Blocked( CBaseEntity *pOther )
9 years ago
{
}
9 years ago
void CCineMonster::Touch( CBaseEntity *pOther )
9 years ago
{
/*
ALERT( at_aiconsole, "Cine Touch\n" );
9 years ago
if( m_pentTarget && OFFSET( pOther->pev ) == OFFSET( m_pentTarget ) )
9 years ago
{
9 years ago
CBaseMonster *pTarget = GetClassPtr( (CBaseMonster *)VARS( m_pentTarget ) );
9 years ago
pTarget->m_monsterState == MONSTERSTATE_SCRIPT;
}
*/
}
/*
entvars_t *pevOther = VARS( gpGlobals->other );
9 years ago
if( !FBitSet( pevOther->flags, FL_MONSTER ) )
{
// touched by a non-monster.
9 years ago
return;
}
pevOther->origin.z += 1;
9 years ago
if( FBitSet( pevOther->flags, FL_ONGROUND ) )
{
// clear the onground so physics don't bitch
9 years ago
pevOther->flags -= FL_ONGROUND;
}
// toss the monster!
pevOther->velocity = pev->movedir * pev->speed;
pevOther->velocity.z += m_flHeight;
pev->solid = SOLID_NOT;// kill the trigger for now !!!UNDONE
}
*/
//
// ********** Cinematic DIE **********
//
9 years ago
void CCineMonster::Die( void )
9 years ago
{
SetThink( &CBaseEntity::SUB_Remove );
}
//
// ********** Cinematic PAIN **********
//
9 years ago
void CCineMonster::Pain( void )
9 years ago
{
}
//
// ********** Cinematic Think **********
//
// find a viable entity
9 years ago
int CCineMonster::FindEntity( void )
9 years ago
{
edict_t *pentTarget;
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( m_iszEntity ) );
9 years ago
m_hTargetEnt = NULL;
9 years ago
CBaseMonster *pTarget = NULL;
9 years ago
9 years ago
while( !FNullEnt( pentTarget ) )
9 years ago
{
9 years ago
if( FBitSet( VARS( pentTarget )->flags, FL_MONSTER ) )
9 years ago
{
pTarget = GetMonsterPointer( pentTarget );
9 years ago
if( pTarget && pTarget->CanPlaySequence( FCanOverrideState(), SS_INTERRUPT_BY_NAME ) )
9 years ago
{
m_hTargetEnt = pTarget;
return TRUE;
}
9 years ago
ALERT( at_console, "Found %s, but can't play!\n", STRING( m_iszEntity ) );
9 years ago
}
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( pentTarget, STRING( m_iszEntity ) );
9 years ago
pTarget = NULL;
}
9 years ago
if( !pTarget )
9 years ago
{
CBaseEntity *pEntity = NULL;
9 years ago
while( ( pEntity = UTIL_FindEntityInSphere( pEntity, pev->origin, m_flRadius ) ) != NULL )
9 years ago
{
9 years ago
if( FClassnameIs( pEntity->pev, STRING( m_iszEntity ) ) )
9 years ago
{
9 years ago
if( FBitSet( pEntity->pev->flags, FL_MONSTER ) )
9 years ago
{
9 years ago
pTarget = pEntity->MyMonsterPointer();
if( pTarget && pTarget->CanPlaySequence( FCanOverrideState(), SS_INTERRUPT_IDLE ) )
9 years ago
{
m_hTargetEnt = pTarget;
return TRUE;
}
}
}
}
}
pTarget = NULL;
m_hTargetEnt = NULL;
return FALSE;
}
// make the entity enter a scripted sequence
9 years ago
void CCineMonster::PossessEntity( void )
9 years ago
{
9 years ago
CBaseEntity *pEntity = m_hTargetEnt;
CBaseMonster *pTarget = NULL;
if( pEntity )
9 years ago
pTarget = pEntity->MyMonsterPointer();
9 years ago
if( pTarget )
9 years ago
{
// FindEntity() just checked this!
#if 0
9 years ago
if( !pTarget->CanPlaySequence( FCanOverrideState() ) )
9 years ago
{
9 years ago
ALERT( at_aiconsole, "Can't possess entity %s\n", STRING( pTarget->pev->classname ) );
9 years ago
return;
}
#endif
pTarget->m_pGoalEnt = this;
pTarget->m_pCine = this;
pTarget->m_hTargetEnt = this;
m_saved_movetype = pTarget->pev->movetype;
m_saved_solid = pTarget->pev->solid;
m_saved_effects = pTarget->pev->effects;
pTarget->pev->effects |= pev->effects;
9 years ago
switch( m_fMoveTo )
9 years ago
{
case 0:
pTarget->m_scriptState = SCRIPT_WAIT;
break;
case 1:
pTarget->m_scriptState = SCRIPT_WALK_TO_MARK;
DelayStart( 1 );
break;
case 2:
pTarget->m_scriptState = SCRIPT_RUN_TO_MARK;
DelayStart( 1 );
break;
case 4:
UTIL_SetOrigin( pTarget->pev, pev->origin );
pTarget->pev->ideal_yaw = pev->angles.y;
pTarget->pev->avelocity = Vector( 0, 0, 0 );
pTarget->pev->velocity = Vector( 0, 0, 0 );
pTarget->pev->effects |= EF_NOINTERP;
pTarget->pev->angles.y = pev->angles.y;
pTarget->m_scriptState = SCRIPT_WAIT;
m_startTime = gpGlobals->time + 1E6;
// UNDONE: Add a flag to do this so people can fixup physics after teleporting monsters
// pTarget->pev->flags &= ~FL_ONGROUND;
break;
}
9 years ago
//ALERT( at_aiconsole, "\"%s\" found and used (INT: %s)\n", STRING( pTarget->pev->targetname ), FBitSet( pev->spawnflags, SF_SCRIPT_NOINTERRUPT )? "No" : "Yes" );
9 years ago
pTarget->m_IdealMonsterState = MONSTERSTATE_SCRIPT;
9 years ago
if( m_iszIdle )
9 years ago
{
StartSequence( pTarget, m_iszIdle, FALSE );
9 years ago
if( FStrEq( STRING( m_iszIdle ), STRING( m_iszPlay ) ) )
9 years ago
{
pTarget->pev->framerate = 0;
}
}
}
}
// make the entity carry out the scripted sequence instructions, but without
// destroying the monster's state.
9 years ago
void CCineAI::PossessEntity( void )
9 years ago
{
Schedule_t *pNewSchedule;
9 years ago
CBaseEntity *pEntity = m_hTargetEnt;
CBaseMonster *pTarget = NULL;
if( pEntity )
9 years ago
pTarget = pEntity->MyMonsterPointer();
9 years ago
if( pTarget )
9 years ago
{
9 years ago
if( !pTarget->CanPlaySequence( FCanOverrideState(), SS_INTERRUPT_AI ) )
9 years ago
{
9 years ago
ALERT( at_aiconsole, "(AI)Can't possess entity %s\n", STRING( pTarget->pev->classname ) );
9 years ago
return;
}
pTarget->m_pGoalEnt = this;
pTarget->m_pCine = this;
pTarget->m_hTargetEnt = this;
m_saved_movetype = pTarget->pev->movetype;
m_saved_solid = pTarget->pev->solid;
m_saved_effects = pTarget->pev->effects;
pTarget->pev->effects |= pev->effects;
9 years ago
switch( m_fMoveTo )
9 years ago
{
case 0:
case 5:
pTarget->m_scriptState = SCRIPT_WAIT;
break;
case 1:
pTarget->m_scriptState = SCRIPT_WALK_TO_MARK;
break;
case 2:
pTarget->m_scriptState = SCRIPT_RUN_TO_MARK;
break;
case 4:
// zap the monster instantly to the site of the script entity.
UTIL_SetOrigin( pTarget->pev, pev->origin );
pTarget->pev->ideal_yaw = pev->angles.y;
pTarget->pev->avelocity = Vector( 0, 0, 0 );
pTarget->pev->velocity = Vector( 0, 0, 0 );
pTarget->pev->effects |= EF_NOINTERP;
pTarget->pev->angles.y = pev->angles.y;
pTarget->m_scriptState = SCRIPT_WAIT;
m_startTime = gpGlobals->time + 1E6;
// UNDONE: Add a flag to do this so people can fixup physics after teleporting monsters
pTarget->pev->flags &= ~FL_ONGROUND;
break;
default:
9 years ago
ALERT( at_aiconsole, "aiscript: invalid Move To Position value!" );
9 years ago
break;
}
9 years ago
ALERT( at_aiconsole, "\"%s\" found and used\n", STRING( pTarget->pev->targetname ) );
pTarget->m_IdealMonsterState = MONSTERSTATE_SCRIPT;
/*
9 years ago
if( m_iszIdle )
9 years ago
{
StartSequence( pTarget, m_iszIdle, FALSE );
9 years ago
if( FStrEq( STRING( m_iszIdle ), STRING( m_iszPlay ) ) )
9 years ago
{
pTarget->pev->framerate = 0;
}
}
*/
// Already in a scripted state?
9 years ago
if( pTarget->m_MonsterState == MONSTERSTATE_SCRIPT )
9 years ago
{
pNewSchedule = pTarget->GetScheduleOfType( SCHED_AISCRIPT );
pTarget->ChangeSchedule( pNewSchedule );
}
}
}
9 years ago
void CCineMonster::CineThink( void )
9 years ago
{
9 years ago
if( FindEntity() )
9 years ago
{
9 years ago
PossessEntity();
9 years ago
ALERT( at_aiconsole, "script \"%s\" using monster \"%s\"\n", STRING( pev->targetname ), STRING( m_iszEntity ) );
}
else
{
9 years ago
CancelScript();
9 years ago
ALERT( at_aiconsole, "script \"%s\" can't find monster \"%s\"\n", STRING( pev->targetname ), STRING( m_iszEntity ) );
pev->nextthink = gpGlobals->time + 1.0;
}
}
// lookup a sequence name and setup the target monster to play it
9 years ago
BOOL CCineMonster::StartSequence( CBaseMonster *pTarget, int iszSeq, BOOL completeOnEmpty )
9 years ago
{
9 years ago
if( !iszSeq && completeOnEmpty )
9 years ago
{
SequenceDone( pTarget );
return FALSE;
}
pTarget->pev->sequence = pTarget->LookupSequence( STRING( iszSeq ) );
9 years ago
if( pTarget->pev->sequence == -1 )
9 years ago
{
9 years ago
ALERT( at_error, "%s: unknown scripted sequence \"%s\"\n", STRING( pTarget->pev->targetname ), STRING( iszSeq ) );
9 years ago
pTarget->pev->sequence = 0;
// return FALSE;
}
#if 0
char *s;
9 years ago
if( pev->spawnflags & SF_SCRIPT_NOINTERRUPT )
9 years ago
s = "No";
else
s = "Yes";
9 years ago
ALERT( at_console, "%s (%s): started \"%s\":INT:%s\n", STRING( pTarget->pev->targetname ), STRING( pTarget->pev->classname ), STRING( iszSeq ), s );
9 years ago
#endif
pTarget->pev->frame = 0;
pTarget->ResetSequenceInfo( );
return TRUE;
}
// lookup a sequence name and setup the target monster to play it
// overridden for CCineAI because it's ok for them to not have an animation sequence
// for the monster to play. For a regular Scripted Sequence, that situation is an error.
9 years ago
BOOL CCineAI::StartSequence( CBaseMonster *pTarget, int iszSeq, BOOL completeOnEmpty )
9 years ago
{
9 years ago
if( iszSeq == 0 && completeOnEmpty )
9 years ago
{
// no sequence was provided. Just let the monster proceed, however, we still have to fire any Sequence target
// and remove any non-repeatable CineAI entities here ( because there is code elsewhere that handles those tasks, but
// not until the animation sequence is finished. We have to manually take care of these things where there is no sequence.
SequenceDone ( pTarget );
return TRUE;
}
pTarget->pev->sequence = pTarget->LookupSequence( STRING( iszSeq ) );
9 years ago
if( pTarget->pev->sequence == -1 )
9 years ago
{
9 years ago
ALERT( at_error, "%s: unknown aiscripted sequence \"%s\"\n", STRING( pTarget->pev->targetname ), STRING( iszSeq ) );
9 years ago
pTarget->pev->sequence = 0;
// return FALSE;
}
pTarget->pev->frame = 0;
9 years ago
pTarget->ResetSequenceInfo();
9 years ago
return TRUE;
}
//=========================================================
// SequenceDone - called when a scripted sequence animation
// sequence is done playing ( or when an AI Scripted Sequence
// doesn't supply an animation sequence to play ). Expects
// the CBaseMonster pointer to the monster that the sequence
// possesses.
//=========================================================
9 years ago
void CCineMonster::SequenceDone( CBaseMonster *pMonster )
9 years ago
{
//ALERT( at_aiconsole, "Sequence %s finished\n", STRING( m_pCine->m_iszPlay ) );
9 years ago
if( !( pev->spawnflags & SF_SCRIPT_REPEATABLE ) )
9 years ago
{
SetThink( &CBaseEntity::SUB_Remove );
pev->nextthink = gpGlobals->time + 0.1;
}
9 years ago
// This is done so that another sequence can take over the monster when triggered by the first
9 years ago
pMonster->CineCleanup();
FixScriptMonsterSchedule( pMonster );
9 years ago
// This may cause a sequence to attempt to grab this guy NOW, so we have to clear him out
// of the existing sequence
SUB_UseTargets( NULL, USE_TOGGLE, 0 );
}
//=========================================================
// When a monster finishes a scripted sequence, we have to
// fix up its state and schedule for it to return to a
// normal AI monster.
//
// Scripted sequences just dirty the Schedule and drop the
// monster in Idle State.
//=========================================================
9 years ago
void CCineMonster::FixScriptMonsterSchedule( CBaseMonster *pMonster )
9 years ago
{
9 years ago
if( pMonster->m_IdealMonsterState != MONSTERSTATE_DEAD )
9 years ago
pMonster->m_IdealMonsterState = MONSTERSTATE_IDLE;
pMonster->ClearSchedule();
}
//=========================================================
// When a monster finishes a scripted sequence, we have to
// fix up its state and schedule for it to return to a
// normal AI monster.
//
// AI Scripted sequences will, depending on what the level
// designer selects:
//
// -Dirty the monster's schedule and drop out of the
// sequence in their current state.
//
// -Select a specific AMBUSH schedule, regardless of state.
//=========================================================
9 years ago
void CCineAI::FixScriptMonsterSchedule( CBaseMonster *pMonster )
9 years ago
{
switch ( m_iFinishSchedule )
{
case SCRIPT_FINISHSCHED_DEFAULT:
pMonster->ClearSchedule();
break;
case SCRIPT_FINISHSCHED_AMBUSH:
pMonster->ChangeSchedule( pMonster->GetScheduleOfType( SCHED_AMBUSH ) );
break;
default:
9 years ago
ALERT( at_aiconsole, "FixScriptMonsterSchedule - no case!\n" );
9 years ago
pMonster->ClearSchedule();
break;
}
}
9 years ago
BOOL CBaseMonster::ExitScriptedSequence()
9 years ago
{
9 years ago
if( pev->deadflag == DEAD_DYING )
9 years ago
{
// is this legal?
// BUGBUG -- This doesn't call Killed()
m_IdealMonsterState = MONSTERSTATE_DEAD;
return FALSE;
}
9 years ago
if( m_pCine )
9 years ago
{
9 years ago
m_pCine->CancelScript();
9 years ago
}
return TRUE;
}
void CCineMonster::AllowInterrupt( BOOL fAllow )
{
9 years ago
if( pev->spawnflags & SF_SCRIPT_NOINTERRUPT )
9 years ago
return;
m_interruptable = fAllow;
}
BOOL CCineMonster::CanInterrupt( void )
{
9 years ago
if( !m_interruptable )
9 years ago
return FALSE;
CBaseEntity *pTarget = m_hTargetEnt;
9 years ago
if( pTarget != NULL && pTarget->pev->deadflag == DEAD_NO )
9 years ago
return TRUE;
return FALSE;
}
int CCineMonster::IgnoreConditions( void )
9 years ago
{
9 years ago
if( CanInterrupt() )
9 years ago
return 0;
return SCRIPT_BREAK_CONDITIONS;
}
void ScriptEntityCancel( edict_t *pentCine )
{
// make sure they are a scripted_sequence
9 years ago
if( FClassnameIs( pentCine, CLASSNAME ) )
9 years ago
{
9 years ago
CCineMonster *pCineTarget = GetClassPtr( (CCineMonster *)VARS( pentCine ) );
9 years ago
// make sure they have a monster in mind for the script
9 years ago
CBaseEntity *pEntity = pCineTarget->m_hTargetEnt;
CBaseMonster *pTarget = NULL;
if( pEntity )
9 years ago
pTarget = pEntity->MyMonsterPointer();
9 years ago
if( pTarget )
9 years ago
{
// make sure their monster is actually playing a script
9 years ago
if( pTarget->m_MonsterState == MONSTERSTATE_SCRIPT )
9 years ago
{
// tell them do die
pTarget->m_scriptState = CCineMonster::SCRIPT_CLEANUP;
// do it now
9 years ago
pTarget->CineCleanup();
9 years ago
}
}
}
}
// find all the cinematic entities with my targetname and stop them from playing
9 years ago
void CCineMonster::CancelScript( void )
9 years ago
{
9 years ago
ALERT( at_aiconsole, "Cancelling script: %s\n", STRING( m_iszPlay ) );
9 years ago
if( !pev->targetname )
9 years ago
{
ScriptEntityCancel( edict() );
return;
}
9 years ago
edict_t *pentCineTarget = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( pev->targetname ) );
9 years ago
9 years ago
while( !FNullEnt( pentCineTarget ) )
9 years ago
{
ScriptEntityCancel( pentCineTarget );
9 years ago
pentCineTarget = FIND_ENTITY_BY_TARGETNAME( pentCineTarget, STRING( pev->targetname ) );
9 years ago
}
}
// find all the cinematic entities with my targetname and tell them to wait before starting
9 years ago
void CCineMonster::DelayStart( int state )
9 years ago
{
9 years ago
edict_t *pentCine = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( pev->targetname ) );
9 years ago
9 years ago
while( !FNullEnt( pentCine ) )
9 years ago
{
9 years ago
if( FClassnameIs( pentCine, "scripted_sequence" ) )
9 years ago
{
9 years ago
CCineMonster *pTarget = GetClassPtr( ( CCineMonster *)VARS( pentCine ) );
if( state )
9 years ago
{
pTarget->m_iDelay++;
}
else
{
pTarget->m_iDelay--;
if( pTarget->m_iDelay <= 0 )
9 years ago
pTarget->m_startTime = gpGlobals->time + 0.05;
}
}
9 years ago
pentCine = FIND_ENTITY_BY_TARGETNAME( pentCine, STRING( pev->targetname ) );
9 years ago
}
}
// Find an entity that I'm interested in and precache the sounds he'll need in the sequence.
9 years ago
void CCineMonster::Activate( void )
9 years ago
{
9 years ago
edict_t *pentTarget;
CBaseMonster *pTarget;
9 years ago
// The entity name could be a target name or a classname
// Check the targetname
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( m_iszEntity ) );
9 years ago
pTarget = NULL;
9 years ago
while( !pTarget && !FNullEnt( pentTarget ) )
9 years ago
{
9 years ago
if( FBitSet( VARS( pentTarget )->flags, FL_MONSTER ) )
9 years ago
{
pTarget = GetMonsterPointer( pentTarget );
}
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( pentTarget, STRING( m_iszEntity ) );
9 years ago
}
9 years ago
// If no entity with that targetname, check the classname
if ( !pTarget )
{
9 years ago
pentTarget = FIND_ENTITY_BY_CLASSNAME(NULL, STRING( m_iszEntity ) );
while( !pTarget && !FNullEnt( pentTarget ) )
9 years ago
{
pTarget = GetMonsterPointer( pentTarget );
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME(pentTarget, STRING( m_iszEntity ) );
9 years ago
}
}
9 years ago
// Found a compatible entity
if ( pTarget )
{
void *pmodel;
pmodel = GET_MODEL_PTR( pTarget->edict() );
9 years ago
if( pmodel )
9 years ago
{
// Look through the event list for stuff to precache
SequencePrecache( pmodel, STRING( m_iszIdle ) );
SequencePrecache( pmodel, STRING( m_iszPlay ) );
}
}
}
9 years ago
BOOL CBaseMonster::CineCleanup()
9 years ago
{
CCineMonster *pOldCine = m_pCine;
// am I linked to a cinematic?
9 years ago
if( m_pCine )
9 years ago
{
// okay, reset me to what it thought I was before
m_pCine->m_hTargetEnt = NULL;
pev->movetype = m_pCine->m_saved_movetype;
pev->solid = m_pCine->m_saved_solid;
pev->effects = m_pCine->m_saved_effects;
}
else
{
// arg, punt
pev->movetype = MOVETYPE_STEP;// this is evil
pev->solid = SOLID_SLIDEBOX;
}
m_pCine = NULL;
m_hTargetEnt = NULL;
m_pGoalEnt = NULL;
9 years ago
if( pev->deadflag == DEAD_DYING )
9 years ago
{
// last frame of death animation?
9 years ago
pev->health = 0;
pev->framerate = 0.0;
pev->solid = SOLID_NOT;
9 years ago
SetState( MONSTERSTATE_DEAD );
pev->deadflag = DEAD_DEAD;
9 years ago
UTIL_SetSize( pev, pev->mins, Vector( pev->maxs.x, pev->maxs.y, pev->mins.z + 2 ) );
9 years ago
9 years ago
if( pOldCine && FBitSet( pOldCine->pev->spawnflags, SF_SCRIPT_LEAVECORPSE ) )
9 years ago
{
SetUse( NULL ); // BUGBUG -- This doesn't call Killed()
SetThink( NULL ); // This will probably break some stuff
SetTouch( NULL );
}
else
SUB_StartFadeOut(); // SetThink( &SUB_DoNothing );
// This turns off animation & physics in case their origin ends up stuck in the world or something
StopAnimation();
pev->movetype = MOVETYPE_NONE;
pev->effects |= EF_NOINTERP; // Don't interpolate either, assume the corpse is positioned in its final resting place
return FALSE;
}
// If we actually played a sequence
9 years ago
if( pOldCine && pOldCine->m_iszPlay )
9 years ago
{
9 years ago
if( !( pOldCine->pev->spawnflags & SF_SCRIPT_NOSCRIPTMOVEMENT ) )
9 years ago
{
// reset position
Vector new_origin, new_angle;
GetBonePosition( 0, new_origin, new_angle );
// Figure out how far they have moved
// We can't really solve this problem because we can't query the movement of the origin relative
// to the sequence. We can get the root bone's position as we do here, but there are
// cases where the root bone is in a different relative position to the entity's origin
// before/after the sequence plays. So we are stuck doing this:
// !!!HACKHACK: Float the origin up and drop to floor because some sequences have
// irregular motion that can't be properly accounted for.
// UNDONE: THIS SHOULD ONLY HAPPEN IF WE ACTUALLY PLAYED THE SEQUENCE.
Vector oldOrigin = pev->origin;
// UNDONE: ugly hack. Don't move monster if they don't "seem" to move
// this really needs to be done with the AX,AY,etc. flags, but that aren't consistantly
// being set, so animations that really do move won't be caught.
9 years ago
if( ( oldOrigin - new_origin).Length2D() < 8.0 )
9 years ago
new_origin = oldOrigin;
pev->origin.x = new_origin.x;
pev->origin.y = new_origin.y;
pev->origin.z += 1;
pev->flags |= FL_ONGROUND;
9 years ago
int drop = DROP_TO_FLOOR( ENT( pev ) );
9 years ago
// Origin in solid? Set to org at the end of the sequence
9 years ago
if( drop < 0 )
9 years ago
pev->origin = oldOrigin;
9 years ago
else if( drop == 0 ) // Hanging in air?
9 years ago
{
pev->origin.z = new_origin.z;
pev->flags &= ~FL_ONGROUND;
}
// else entity hit floor, leave there
// pEntity->pev->origin.z = new_origin.z + 5.0; // damn, got to fix this
UTIL_SetOrigin( pev, pev->origin );
pev->effects |= EF_NOINTERP;
}
// We should have some animation to put these guys in, but for now it's idle.
// Due to NOINTERP above, there won't be any blending between this anim & the sequence
m_Activity = ACT_RESET;
}
// set them back into a normal state
pev->enemy = NULL;
9 years ago
if( pev->health > 0 )
9 years ago
m_IdealMonsterState = MONSTERSTATE_IDLE; // m_previousState;
else
{
// Dropping out because he got killed
// Can't call killed() no attacker and weirdness (late gibbing) may result
m_IdealMonsterState = MONSTERSTATE_DEAD;
SetConditions( bits_COND_LIGHT_DAMAGE );
pev->deadflag = DEAD_DYING;
FCheckAITrigger();
pev->deadflag = DEAD_NO;
}
// SetAnimation( m_MonsterState );
9 years ago
ClearBits( pev->spawnflags, SF_MONSTER_WAIT_FOR_SCRIPT );
9 years ago
return TRUE;
}
class CScriptedSentence : public CBaseToggle
{
public:
void Spawn( void );
void KeyValue( KeyValueData *pkvd );
void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
void EXPORT FindThink( void );
void EXPORT DelayThink( void );
9 years ago
int ObjectCaps( void ) { return ( CBaseToggle::ObjectCaps() & ~FCAP_ACROSS_TRANSITION ); }
9 years ago
9 years ago
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
9 years ago
9 years ago
static TYPEDESCRIPTION m_SaveData[];
9 years ago
CBaseMonster *FindEntity( void );
BOOL AcceptableSpeaker( CBaseMonster *pMonster );
BOOL StartSentence( CBaseMonster *pTarget );
private:
string_t m_iszSentence; // string index for idle animation
string_t m_iszEntity; // entity that is wanted for this sentence
9 years ago
float m_flRadius; // range to search
float m_flDuration; // How long the sentence lasts
float m_flRepeat; // repeat rate
float m_flAttenuation;
float m_flVolume;
BOOL m_active;
string_t m_iszListener; // name of entity to look at while talking
9 years ago
};
9 years ago
#define SF_SENTENCE_ONCE 0x0001
9 years ago
#define SF_SENTENCE_FOLLOWERS 0x0002 // only say if following player
#define SF_SENTENCE_INTERRUPT 0x0004 // force talking except when dead
#define SF_SENTENCE_CONCURRENT 0x0008 // allow other people to keep talking
TYPEDESCRIPTION CScriptedSentence::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CScriptedSentence, m_iszSentence, FIELD_STRING ),
DEFINE_FIELD( CScriptedSentence, m_iszEntity, FIELD_STRING ),
DEFINE_FIELD( CScriptedSentence, m_flRadius, FIELD_FLOAT ),
DEFINE_FIELD( CScriptedSentence, m_flDuration, FIELD_FLOAT ),
DEFINE_FIELD( CScriptedSentence, m_flRepeat, FIELD_FLOAT ),
DEFINE_FIELD( CScriptedSentence, m_flAttenuation, FIELD_FLOAT ),
DEFINE_FIELD( CScriptedSentence, m_flVolume, FIELD_FLOAT ),
DEFINE_FIELD( CScriptedSentence, m_active, FIELD_BOOLEAN ),
DEFINE_FIELD( CScriptedSentence, m_iszListener, FIELD_STRING ),
};
IMPLEMENT_SAVERESTORE( CScriptedSentence, CBaseToggle )
9 years ago
LINK_ENTITY_TO_CLASS( scripted_sentence, CScriptedSentence )
9 years ago
9 years ago
void CScriptedSentence::KeyValue( KeyValueData *pkvd )
9 years ago
{
9 years ago
if( FStrEq( pkvd->szKeyName, "sentence" ) )
9 years ago
{
m_iszSentence = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "entity" ) )
9 years ago
{
m_iszEntity = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "duration" ) )
9 years ago
{
m_flDuration = atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "radius" ) )
9 years ago
{
m_flRadius = atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "refire") )
9 years ago
{
m_flRepeat = atof( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "attenuation" ) )
9 years ago
{
pev->impulse = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "volume" ) )
9 years ago
{
m_flVolume = atof( pkvd->szValue ) * 0.1;
pkvd->fHandled = TRUE;
}
9 years ago
else if( FStrEq( pkvd->szKeyName, "listener" ) )
9 years ago
{
m_iszListener = ALLOC_STRING( pkvd->szValue );
pkvd->fHandled = TRUE;
}
else
CBaseToggle::KeyValue( pkvd );
}
9 years ago
void CScriptedSentence::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
9 years ago
if( !m_active )
9 years ago
return;
9 years ago
//ALERT( at_console, "Firing sentence: %s\n", STRING( m_iszSentence ) );
9 years ago
SetThink( &CScriptedSentence::FindThink );
pev->nextthink = gpGlobals->time;
}
9 years ago
void CScriptedSentence::Spawn( void )
9 years ago
{
pev->solid = SOLID_NOT;
9 years ago
m_active = TRUE;
// if no targetname, start now
9 years ago
if( !pev->targetname )
9 years ago
{
SetThink( &CScriptedSentence::FindThink );
pev->nextthink = gpGlobals->time + 1.0;
}
switch( pev->impulse )
{
9 years ago
case 1:
// Medium radius
9 years ago
m_flAttenuation = ATTN_STATIC;
break;
9 years ago
case 2:
// Large radius
9 years ago
m_flAttenuation = ATTN_NORM;
break;
9 years ago
case 3:
//EVERYWHERE
9 years ago
m_flAttenuation = ATTN_NONE;
break;
default:
9 years ago
case 0:
// Small radius
9 years ago
m_flAttenuation = ATTN_IDLE;
break;
}
pev->impulse = 0;
// No volume, use normal
9 years ago
if( m_flVolume <= 0 )
9 years ago
m_flVolume = 1.0;
}
9 years ago
void CScriptedSentence::FindThink( void )
9 years ago
{
CBaseMonster *pMonster = FindEntity();
9 years ago
if( pMonster )
9 years ago
{
StartSentence( pMonster );
9 years ago
if( pev->spawnflags & SF_SENTENCE_ONCE )
9 years ago
UTIL_Remove( this );
SetThink( &CScriptedSentence::DelayThink );
pev->nextthink = gpGlobals->time + m_flDuration + m_flRepeat;
m_active = FALSE;
9 years ago
//ALERT( at_console, "%s: found monster %s\n", STRING( m_iszSentence ), STRING( m_iszEntity ) );
9 years ago
}
else
{
9 years ago
//ALERT( at_console, "%s: can't find monster %s\n", STRING( m_iszSentence ), STRING( m_iszEntity ) );
9 years ago
pev->nextthink = gpGlobals->time + m_flRepeat + 0.5;
}
}
9 years ago
void CScriptedSentence::DelayThink( void )
9 years ago
{
m_active = TRUE;
9 years ago
if( !pev->targetname )
9 years ago
pev->nextthink = gpGlobals->time + 0.1;
SetThink( &CScriptedSentence::FindThink );
}
9 years ago
BOOL CScriptedSentence::AcceptableSpeaker( CBaseMonster *pMonster )
9 years ago
{
9 years ago
if( pMonster )
9 years ago
{
9 years ago
if( pev->spawnflags & SF_SENTENCE_FOLLOWERS )
9 years ago
{
if( pMonster->m_hTargetEnt == 0 || !FClassnameIs( pMonster->m_hTargetEnt->pev, "player" ) )
9 years ago
return FALSE;
}
BOOL override;
9 years ago
if( pev->spawnflags & SF_SENTENCE_INTERRUPT )
9 years ago
override = TRUE;
else
override = FALSE;
9 years ago
if( pMonster->CanPlaySentence( override ) )
9 years ago
return TRUE;
}
return FALSE;
}
9 years ago
CBaseMonster *CScriptedSentence::FindEntity( void )
9 years ago
{
edict_t *pentTarget;
CBaseMonster *pMonster;
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( NULL, STRING( m_iszEntity ) );
9 years ago
pMonster = NULL;
9 years ago
while( !FNullEnt( pentTarget ) )
9 years ago
{
pMonster = GetMonsterPointer( pentTarget );
9 years ago
if( pMonster != NULL )
9 years ago
{
9 years ago
if( AcceptableSpeaker( pMonster ) )
9 years ago
return pMonster;
9 years ago
//ALERT( at_console, "%s (%s), not acceptable\n", STRING( pMonster->pev->classname ), STRING( pMonster->pev->targetname ) );
9 years ago
}
9 years ago
pentTarget = FIND_ENTITY_BY_TARGETNAME( pentTarget, STRING( m_iszEntity ) );
9 years ago
}
9 years ago
9 years ago
CBaseEntity *pEntity = NULL;
9 years ago
while( ( pEntity = UTIL_FindEntityInSphere( pEntity, pev->origin, m_flRadius ) ) != NULL )
9 years ago
{
9 years ago
if( FClassnameIs( pEntity->pev, STRING( m_iszEntity ) ) )
9 years ago
{
9 years ago
if( FBitSet( pEntity->pev->flags, FL_MONSTER ) )
9 years ago
{
9 years ago
pMonster = pEntity->MyMonsterPointer();
if( AcceptableSpeaker( pMonster ) )
9 years ago
return pMonster;
}
}
}
return NULL;
}
9 years ago
BOOL CScriptedSentence::StartSentence( CBaseMonster *pTarget )
9 years ago
{
9 years ago
if( !pTarget )
9 years ago
{
9 years ago
ALERT( at_aiconsole, "Not Playing sentence %s\n", STRING( m_iszSentence ) );
return FALSE;
9 years ago
}
BOOL bConcurrent = FALSE;
9 years ago
if( !( pev->spawnflags & SF_SENTENCE_CONCURRENT ) )
9 years ago
bConcurrent = TRUE;
CBaseEntity *pListener = NULL;
9 years ago
if( !FStringNull( m_iszListener ) )
9 years ago
{
float radius = m_flRadius;
9 years ago
if( FStrEq( STRING( m_iszListener ), "player" ) )
9 years ago
radius = 4096; // Always find the player
pListener = UTIL_FindEntityGeneric( STRING( m_iszListener ), pTarget->pev->origin, radius );
}
9 years ago
pTarget->PlayScriptedSentence( STRING( m_iszSentence ), m_flDuration, m_flVolume, m_flAttenuation, bConcurrent, pListener );
ALERT( at_aiconsole, "Playing sentence %s (%.1f)\n", STRING( m_iszSentence ), m_flDuration );
9 years ago
SUB_UseTargets( NULL, USE_TOGGLE, 0 );
return TRUE;
}
//=========================================================
// Furniture - this is the cool comment I cut-and-pasted
//=========================================================
class CFurniture : public CBaseMonster
{
public:
9 years ago
void Spawn( void );
9 years ago
void Die( void );
9 years ago
int Classify( void );
virtual int ObjectCaps( void ) { return (CBaseMonster::ObjectCaps() & ~FCAP_ACROSS_TRANSITION); }
9 years ago
};
LINK_ENTITY_TO_CLASS( monster_furniture, CFurniture )
9 years ago
//=========================================================
// Furniture is killed
//=========================================================
9 years ago
void CFurniture::Die( void )
9 years ago
{
SetThink( &CBaseEntity::SUB_Remove );
pev->nextthink = gpGlobals->time;
}
//=========================================================
// This used to have something to do with bees flying, but
// now it only initializes moving furniture in scripted sequences
//=========================================================
9 years ago
void CFurniture::Spawn()
9 years ago
{
PRECACHE_MODEL( STRING( pev->model ) );
9 years ago
SET_MODEL( ENT( pev ), STRING( pev->model ) );
9 years ago
9 years ago
pev->movetype = MOVETYPE_NONE;
pev->solid = SOLID_BBOX;
pev->health = 80000;
9 years ago
pev->takedamage = DAMAGE_AIM;
9 years ago
pev->effects = 0;
pev->yaw_speed = 0;
pev->sequence = 0;
pev->frame = 0;
9 years ago
//pev->nextthink += 1.0;
9 years ago
//SetThink( &WalkMonsterDelay );
9 years ago
9 years ago
ResetSequenceInfo();
9 years ago
pev->frame = 0;
MonsterInit();
}
//=========================================================
// ID's Furniture as neutral (noone will attack it)
//=========================================================
9 years ago
int CFurniture::Classify( void )
9 years ago
{
9 years ago
return CLASS_NONE;
9 years ago
}