doom3-bfg/neo/d3xp/Projectile.cpp

3403 lines
83 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.
===========================================================================
*/
2012-11-26 18:58:24 +00:00
#include "../idlib/precompiled.h"
#pragma hdrstop
#include "Game_local.h"
/*
===============================================================================
idProjectile
===============================================================================
*/
idCVar g_projectileDebug( "g_projectileDebug", "0", CVAR_BOOL, "Debug projectiles" );
// This is used in MP to simulate frames to catchup a projectile's state. Similiar to how players work much lighter weight.
// This is used in MP to simulate frames to catchup a projectile's state. Similiar to how players work much lighter weight.
idArray< idProjectile::simulatedProjectile_t, idProjectile::MAX_SIMULATED_PROJECTILES > idProjectile::projectilesToSimulate;
static const int BFG_DAMAGE_FREQUENCY = 333;
static const float BOUNCE_SOUND_MIN_VELOCITY = 200.0f;
static const float BOUNCE_SOUND_MAX_VELOCITY = 400.0f;
const idEventDef EV_Explode( "<explode>", NULL );
const idEventDef EV_Fizzle( "<fizzle>", NULL );
const idEventDef EV_RadiusDamage( "<radiusdmg>", "e" );
const idEventDef EV_GetProjectileState( "getProjectileState", NULL, 'd' );
const idEventDef EV_CreateProjectile( "projectileCreateProjectile", "evv" );
const idEventDef EV_LaunchProjectile( "projectileLaunchProjectile", "vvv" );
const idEventDef EV_SetGravity( "setGravity", "f" );
CLASS_DECLARATION( idEntity, idProjectile )
EVENT( EV_Explode, idProjectile::Event_Explode )
EVENT( EV_Fizzle, idProjectile::Event_Fizzle )
EVENT( EV_Touch, idProjectile::Event_Touch )
EVENT( EV_RadiusDamage, idProjectile::Event_RadiusDamage )
EVENT( EV_GetProjectileState, idProjectile::Event_GetProjectileState )
EVENT( EV_CreateProjectile, idProjectile::Event_CreateProjectile )
EVENT( EV_LaunchProjectile, idProjectile::Event_LaunchProjectile )
EVENT( EV_SetGravity, idProjectile::Event_SetGravity )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idProjectile::idProjectile
================
*/
idProjectile::idProjectile() :
launchOrigin( 0.0f ),
launchAxis( mat3_identity )
{
2012-11-26 18:58:24 +00:00
owner = NULL;
lightDefHandle = -1;
thrust = 0.0f;
thrust_end = 0;
smokeFly = NULL;
smokeFlyTime = 0;
state = SPAWNED;
lightOffset = vec3_zero;
lightStartTime = 0;
lightEndTime = 0;
lightColor = vec3_zero;
damagePower = 1.0f;
launchedFromGrabber = false;
mTouchTriggers = false;
mNoExplodeDisappear = false;
memset( &projectileFlags, 0, sizeof( projectileFlags ) );
memset( &renderLight, 0, sizeof( renderLight ) );
2012-11-26 18:58:24 +00:00
// note: for net_instanthit projectiles, we will force this back to false at spawn time
fl.networkSync = true;
}
/*
================
idProjectile::Spawn
================
*/
void idProjectile::Spawn()
{
2012-11-26 18:58:24 +00:00
physicsObj.SetSelf( this );
physicsObj.SetClipModel( new( TAG_PHYSICS_CLIP_ENTITY ) idClipModel( GetPhysics()->GetClipModel() ), 1.0f );
2012-11-26 18:58:24 +00:00
physicsObj.SetContents( 0 );
physicsObj.SetClipMask( 0 );
physicsObj.PutToRest();
SetPhysics( &physicsObj );
mNoExplodeDisappear = spawnArgs.GetBool( "no_explode_disappear", mNoExplodeDisappear );
mTouchTriggers = spawnArgs.GetBool( "touch_triggers", mTouchTriggers );
}
/*
================
idProjectile::Save
================
*/
void idProjectile::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
owner.Save( savefile );
2012-11-26 18:58:24 +00:00
projectileFlags_s flags = projectileFlags;
LittleBitField( &flags, sizeof( flags ) );
savefile->Write( &flags, sizeof( flags ) );
2012-11-26 18:58:24 +00:00
savefile->WriteFloat( thrust );
savefile->WriteInt( thrust_end );
2012-11-26 18:58:24 +00:00
savefile->WriteRenderLight( renderLight );
savefile->WriteInt( ( int )lightDefHandle );
2012-11-26 18:58:24 +00:00
savefile->WriteVec3( lightOffset );
savefile->WriteInt( lightStartTime );
savefile->WriteInt( lightEndTime );
savefile->WriteVec3( lightColor );
2012-11-26 18:58:24 +00:00
savefile->WriteParticle( smokeFly );
savefile->WriteInt( smokeFlyTime );
2012-11-26 18:58:24 +00:00
savefile->WriteInt( originalTimeGroup );
savefile->WriteInt( ( int )state );
2012-11-26 18:58:24 +00:00
savefile->WriteFloat( damagePower );
2012-11-26 18:58:24 +00:00
savefile->WriteStaticObject( physicsObj );
savefile->WriteStaticObject( thruster );
}
/*
================
idProjectile::Restore
================
*/
void idProjectile::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
owner.Restore( savefile );
2012-11-26 18:58:24 +00:00
savefile->Read( &projectileFlags, sizeof( projectileFlags ) );
LittleBitField( &projectileFlags, sizeof( projectileFlags ) );
2012-11-26 18:58:24 +00:00
savefile->ReadFloat( thrust );
savefile->ReadInt( thrust_end );
2012-11-26 18:58:24 +00:00
savefile->ReadRenderLight( renderLight );
savefile->ReadInt( ( int& )lightDefHandle );
2012-11-26 18:58:24 +00:00
savefile->ReadVec3( lightOffset );
savefile->ReadInt( lightStartTime );
savefile->ReadInt( lightEndTime );
savefile->ReadVec3( lightColor );
2012-11-26 18:58:24 +00:00
savefile->ReadParticle( smokeFly );
savefile->ReadInt( smokeFlyTime );
2012-11-26 18:58:24 +00:00
savefile->ReadInt( originalTimeGroup );
savefile->ReadInt( ( int& )state );
2012-11-26 18:58:24 +00:00
savefile->ReadFloat( damagePower );
2012-11-26 18:58:24 +00:00
savefile->ReadStaticObject( physicsObj );
RestorePhysics( &physicsObj );
2012-11-26 18:58:24 +00:00
savefile->ReadStaticObject( thruster );
thruster.SetPhysics( &physicsObj );
if( smokeFly != NULL )
{
2012-11-26 18:58:24 +00:00
idVec3 dir;
dir = physicsObj.GetLinearVelocity();
dir.NormalizeFast();
gameLocal.smokeParticles->EmitSmoke( smokeFly, gameLocal.time, gameLocal.random.RandomFloat(), GetPhysics()->GetOrigin(), GetPhysics()->GetAxis(), timeGroup /*_D3XP*/ );
}
if( lightDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
lightDefHandle = gameRenderWorld->AddLightDef( &renderLight );
}
}
/*
================
idProjectile::GetOwner
================
*/
idEntity* idProjectile::GetOwner() const
{
2012-11-26 18:58:24 +00:00
return owner.GetEntity();
}
/*
================
idProjectile::Create
================
*/
void idProjectile::Create( idEntity* owner, const idVec3& start, const idVec3& dir )
{
2012-11-26 18:58:24 +00:00
idDict args;
idStr shaderName;
idVec3 light_color;
idVec3 light_offset;
idVec3 tmp;
idMat3 axis;
2012-11-26 18:58:24 +00:00
Unbind();
2012-11-26 18:58:24 +00:00
// align z-axis of model with the direction
axis = dir.ToMat3();
tmp = axis[2];
axis[2] = axis[0];
axis[0] = -tmp;
2012-11-26 18:58:24 +00:00
physicsObj.SetOrigin( start );
physicsObj.SetAxis( axis );
2012-11-26 18:58:24 +00:00
physicsObj.GetClipModel()->SetOwner( owner );
2012-11-26 18:58:24 +00:00
this->owner = owner;
2012-11-26 18:58:24 +00:00
memset( &renderLight, 0, sizeof( renderLight ) );
shaderName = spawnArgs.GetString( "mtr_light_shader" );
if( *( const char* )shaderName )
{
2012-11-26 18:58:24 +00:00
renderLight.shader = declManager->FindMaterial( shaderName, false );
renderLight.pointLight = true;
renderLight.lightRadius[0] =
renderLight.lightRadius[1] =
renderLight.lightRadius[2] = spawnArgs.GetFloat( "light_radius" );
2012-11-26 18:58:24 +00:00
#ifdef ID_PC
renderLight.lightRadius *= 1.5f;
renderLight.forceShadows = true;
#endif
spawnArgs.GetVector( "light_color", "1 1 1", light_color );
renderLight.shaderParms[0] = light_color[0];
renderLight.shaderParms[1] = light_color[1];
renderLight.shaderParms[2] = light_color[2];
renderLight.shaderParms[3] = 1.0f;
}
2012-11-26 18:58:24 +00:00
spawnArgs.GetVector( "light_offset", "0 0 0", lightOffset );
2012-11-26 18:58:24 +00:00
lightStartTime = 0;
lightEndTime = 0;
smokeFlyTime = 0;
2012-11-26 18:58:24 +00:00
damagePower = 1.0f;
if( spawnArgs.GetBool( "reset_time_offset", "0" ) )
{
2012-11-26 18:58:24 +00:00
renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
}
2012-11-26 18:58:24 +00:00
UpdateVisuals();
2012-11-26 18:58:24 +00:00
state = CREATED;
}
/*
=================
idProjectile::~idProjectile
=================
*/
idProjectile::~idProjectile()
{
2012-11-26 18:58:24 +00:00
StopSound( SND_CHANNEL_ANY, false );
FreeLightDef();
}
/*
=================
idProjectile::FreeLightDef
=================
*/
void idProjectile::FreeLightDef()
{
if( lightDefHandle != -1 )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->FreeLightDef( lightDefHandle );
lightDefHandle = -1;
}
}
/*
=================
idProjectile::Launch
=================
*/
void idProjectile::Launch( const idVec3& start, const idVec3& dir, const idVec3& pushVelocity, const float timeSinceFire, const float launchPower, const float dmgPower )
{
2012-11-26 18:58:24 +00:00
float fuse;
float startthrust;
float endthrust;
idVec3 velocity;
idAngles angular_velocity;
float linear_friction;
float angular_friction;
float contact_friction;
float bounce;
float mass;
float speed;
float gravity;
idVec3 gravVec;
idVec3 tmp;
idMat3 axis;
int thrust_start;
int contents;
int clipMask;
2012-11-26 18:58:24 +00:00
// allow characters to throw projectiles during cinematics, but not the player
if( owner.GetEntity() && !owner.GetEntity()->IsType( idPlayer::Type ) )
{
2012-11-26 18:58:24 +00:00
cinematic = owner.GetEntity()->cinematic;
}
else
{
2012-11-26 18:58:24 +00:00
cinematic = false;
}
2012-11-26 18:58:24 +00:00
thrust = spawnArgs.GetFloat( "thrust" );
startthrust = spawnArgs.GetFloat( "thrust_start" );
endthrust = spawnArgs.GetFloat( "thrust_end" );
2012-11-26 18:58:24 +00:00
spawnArgs.GetVector( "velocity", "0 0 0", velocity );
speed = velocity.Length() * launchPower;
2012-11-26 18:58:24 +00:00
damagePower = dmgPower;
2012-11-26 18:58:24 +00:00
spawnArgs.GetAngles( "angular_velocity", "0 0 0", angular_velocity );
2012-11-26 18:58:24 +00:00
linear_friction = spawnArgs.GetFloat( "linear_friction" );
angular_friction = spawnArgs.GetFloat( "angular_friction" );
contact_friction = spawnArgs.GetFloat( "contact_friction" );
bounce = spawnArgs.GetFloat( "bounce" );
mass = spawnArgs.GetFloat( "mass" );
gravity = spawnArgs.GetFloat( "gravity" );
fuse = spawnArgs.GetFloat( "fuse" );
2012-11-26 18:58:24 +00:00
projectileFlags.detonate_on_world = spawnArgs.GetBool( "detonate_on_world" );
projectileFlags.detonate_on_actor = spawnArgs.GetBool( "detonate_on_actor" );
projectileFlags.randomShaderSpin = spawnArgs.GetBool( "random_shader_spin" );
if( mass <= 0 )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "Invalid mass on '%s'\n", GetEntityDefName() );
}
2012-11-26 18:58:24 +00:00
thrust *= mass;
thrust_start = SEC2MS( startthrust ) + gameLocal.time;
thrust_end = SEC2MS( endthrust ) + gameLocal.time;
2012-11-26 18:58:24 +00:00
lightStartTime = 0;
lightEndTime = 0;
if( health )
{
2012-11-26 18:58:24 +00:00
fl.takedamage = true;
}
2012-11-26 18:58:24 +00:00
gravVec = gameLocal.GetGravity();
gravVec.NormalizeFast();
2012-11-26 18:58:24 +00:00
Unbind();
2012-11-26 18:58:24 +00:00
// align z-axis of model with the direction
axis = dir.ToMat3();
tmp = axis[2];
axis[2] = axis[0];
axis[0] = -tmp;
2012-11-26 18:58:24 +00:00
contents = 0;
clipMask = MASK_SHOT_RENDERMODEL;
if( spawnArgs.GetBool( "detonate_on_trigger" ) )
{
2012-11-26 18:58:24 +00:00
contents |= CONTENTS_TRIGGER;
}
if( !spawnArgs.GetBool( "no_contents" ) )
{
2012-11-26 18:58:24 +00:00
contents |= CONTENTS_PROJECTILE;
clipMask |= CONTENTS_PROJECTILE;
}
if( !idStr::Cmp( this->GetEntityDefName(), "projectile_helltime_killer" ) )
{
2012-11-26 18:58:24 +00:00
contents = CONTENTS_MOVEABLECLIP;
clipMask = CONTENTS_MOVEABLECLIP;
fuse = 10.0f;
}
2012-11-26 18:58:24 +00:00
// don't do tracers on client, we don't know origin and direction
if( spawnArgs.GetBool( "tracers" ) && gameLocal.random.RandomFloat() > 0.5f )
{
2012-11-26 18:58:24 +00:00
SetModel( spawnArgs.GetString( "model_tracer" ) );
projectileFlags.isTracer = true;
}
2012-11-26 18:58:24 +00:00
physicsObj.SetMass( mass );
physicsObj.SetFriction( linear_friction, angular_friction, contact_friction );
if( contact_friction == 0.0f )
{
2012-11-26 18:58:24 +00:00
physicsObj.NoContact();
}
physicsObj.SetBouncyness( bounce );
physicsObj.SetGravity( gravVec * gravity );
physicsObj.SetContents( contents );
physicsObj.SetClipMask( clipMask );
physicsObj.SetLinearVelocity( axis[ 2 ] * speed + pushVelocity );
physicsObj.SetAngularVelocity( angular_velocity.ToAngularVelocity() * axis );
physicsObj.SetOrigin( start );
physicsObj.SetAxis( axis );
2012-11-26 18:58:24 +00:00
launchOrigin = start;
launchAxis = axis;
2012-11-26 18:58:24 +00:00
thruster.SetPosition( &physicsObj, 0, idVec3( GetPhysics()->GetBounds()[ 0 ].x, 0, 0 ) );
if( !common->IsClient() || fl.skipReplication )
{
if( fuse <= 0 )
{
2012-11-26 18:58:24 +00:00
// run physics for 1 second
RunPhysics();
PostEventMS( &EV_Remove, spawnArgs.GetInt( "remove_time", "1500" ) );
}
else if( spawnArgs.GetBool( "detonate_on_fuse" ) )
{
2012-11-26 18:58:24 +00:00
fuse -= timeSinceFire;
if( fuse < 0.0f )
{
2012-11-26 18:58:24 +00:00
fuse = 0.0f;
}
PostEventSec( &EV_Explode, fuse );
}
else
{
2012-11-26 18:58:24 +00:00
fuse -= timeSinceFire;
if( fuse < 0.0f )
{
2012-11-26 18:58:24 +00:00
fuse = 0.0f;
}
PostEventSec( &EV_Fizzle, fuse );
}
}
if( projectileFlags.isTracer )
{
2012-11-26 18:58:24 +00:00
StartSound( "snd_tracer", SND_CHANNEL_BODY, 0, false, NULL );
}
else
{
2012-11-26 18:58:24 +00:00
StartSound( "snd_fly", SND_CHANNEL_BODY, 0, false, NULL );
}
2012-11-26 18:58:24 +00:00
smokeFlyTime = 0;
const char* smokeName = spawnArgs.GetString( "smoke_fly" );
if( *smokeName != '\0' )
{
smokeFly = static_cast<const idDeclParticle*>( declManager->FindType( DECL_PARTICLE, smokeName ) );
2012-11-26 18:58:24 +00:00
smokeFlyTime = gameLocal.time;
}
2012-11-26 18:58:24 +00:00
originalTimeGroup = timeGroup;
2012-11-26 18:58:24 +00:00
// used for the plasma bolts but may have other uses as well
if( projectileFlags.randomShaderSpin )
{
2012-11-26 18:58:24 +00:00
float f = gameLocal.random.RandomFloat();
f *= 0.5f;
renderEntity.shaderParms[SHADERPARM_DIVERSITY] = f;
}
2012-11-26 18:58:24 +00:00
UpdateVisuals();
2012-11-26 18:58:24 +00:00
state = LAUNCHED;
}
/*
================
idProjectile::Think
================
*/
void idProjectile::Think()
{
2012-11-26 18:58:24 +00:00
if( thinkFlags & TH_THINK )
{
if( thrust && ( gameLocal.time < thrust_end ) )
{
2012-11-26 18:58:24 +00:00
// evaluate force
thruster.SetForce( GetPhysics()->GetAxis()[ 0 ] * thrust );
thruster.Evaluate( gameLocal.time );
}
}
if( mTouchTriggers )
{
2012-11-26 18:58:24 +00:00
TouchTriggers();
}
// if the projectile owner is a player
if( owner.GetEntity() && owner.GetEntity()->IsType( idPlayer::Type ) )
{
idPlayer* player = static_cast<idPlayer*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
// Remove any projectiles spectators threw.
if( player != NULL && player->spectating )
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_Remove, 0 );
}
}
2012-11-26 18:58:24 +00:00
// run physics
RunPhysics();
2012-11-26 18:58:24 +00:00
DecayOriginAndAxisDelta();
2012-11-26 18:58:24 +00:00
Present();
2012-11-26 18:58:24 +00:00
AddParticlesAndLight();
}
/*
=================
idProjectile::AddParticlesAndLight
=================
*/
void idProjectile::AddParticlesAndLight()
{
2012-11-26 18:58:24 +00:00
// add the particles
if( smokeFly != NULL && smokeFlyTime && !IsHidden() )
{
2012-11-26 18:58:24 +00:00
idVec3 dir = -GetPhysics()->GetLinearVelocity();
dir.Normalize();
SetTimeState ts( originalTimeGroup );
if( !gameLocal.smokeParticles->EmitSmoke( smokeFly, smokeFlyTime, gameLocal.random.RandomFloat(), GetPhysics()->GetOrigin(), dir.ToMat3(), timeGroup /*_D3XP*/ ) )
{
2012-11-26 18:58:24 +00:00
smokeFlyTime = gameLocal.time;
}
}
2012-11-26 18:58:24 +00:00
// add the light
if( renderLight.lightRadius.x > 0.0f && g_projectileLights.GetBool() )
{
2012-11-26 18:58:24 +00:00
renderLight.origin = GetPhysics()->GetOrigin() + GetPhysics()->GetAxis() * lightOffset;
renderLight.axis = GetPhysics()->GetAxis();
if( ( lightDefHandle != -1 ) )
{
if( lightEndTime > 0 && gameLocal.time <= lightEndTime )
{
2012-11-26 18:58:24 +00:00
idVec3 color( 0, 0, 0 );
if( gameLocal.time < lightEndTime )
{
2012-11-26 18:58:24 +00:00
float frac = ( float )( gameLocal.time - lightStartTime ) / ( float )( lightEndTime - lightStartTime );
color.Lerp( lightColor, color, frac );
}
2012-11-26 18:58:24 +00:00
renderLight.shaderParms[SHADERPARM_RED] = color.x;
renderLight.shaderParms[SHADERPARM_GREEN] = color.y;
renderLight.shaderParms[SHADERPARM_BLUE] = color.z;
}
2012-11-26 18:58:24 +00:00
gameRenderWorld->UpdateLightDef( lightDefHandle, &renderLight );
}
else
{
2012-11-26 18:58:24 +00:00
lightDefHandle = gameRenderWorld->AddLightDef( &renderLight );
}
}
}
/*
=================
idProjectile::Collide
=================
*/
bool idProjectile::Collide( const trace_t& collision, const idVec3& velocity )
{
idEntity* ent;
idEntity* ignore;
const char* damageDefName;
2012-11-26 18:58:24 +00:00
idVec3 dir;
float push;
float damageScale;
if( state == EXPLODED || state == FIZZLED )
{
2012-11-26 18:58:24 +00:00
return true;
}
const bool isHitscan = spawnArgs.GetBool( "net_instanthit" );
2012-11-26 18:58:24 +00:00
// hanlde slow projectiles here.
if( common->IsClient() && !isHitscan )
{
2012-11-26 18:58:24 +00:00
// This is a replicated slow projectile, predict the explosion.
if( ClientPredictionCollide( this, spawnArgs, collision, velocity, !isHitscan ) )
{
2012-11-26 18:58:24 +00:00
Explode( collision, NULL );
return true;
}
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// remove projectile when a 'noimpact' surface is hit
if( ( collision.c.material != NULL ) && ( collision.c.material->GetSurfaceFlags() & SURF_NOIMPACT ) )
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_Remove, 0 );
common->DPrintf( "Projectile collision no impact\n" );
return true;
}
2012-11-26 18:58:24 +00:00
// get the entity the projectile collided with
ent = gameLocal.entities[ collision.c.entityNum ];
if( ent == owner.GetEntity() )
{
2012-11-26 18:58:24 +00:00
assert( 0 );
return true;
}
2012-11-26 18:58:24 +00:00
// just get rid of the projectile when it hits a player in noclip
if( ent->IsType( idPlayer::Type ) && static_cast<idPlayer*>( ent )->noclip )
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_Remove, 0 );
return true;
}
2012-11-26 18:58:24 +00:00
// direction of projectile
dir = velocity;
dir.Normalize();
2012-11-26 18:58:24 +00:00
// projectiles can apply an additional impulse next to the rigid body physics impulse
if( spawnArgs.GetFloat( "push", "0", push ) && push > 0.0f )
{
if( !common->IsClient() )
{
2012-11-26 18:58:24 +00:00
ent->ApplyImpulse( this, collision.c.id, collision.c.point, push * dir );
}
}
2012-11-26 18:58:24 +00:00
// MP: projectiles open doors
if( common->IsMultiplayer() && ent->IsType( idDoor::Type ) && !static_cast< idDoor* >( ent )->IsOpen() && !ent->spawnArgs.GetBool( "no_touch" ) )
{
if( !common->IsClient() )
{
2012-11-26 18:58:24 +00:00
ent->ProcessEvent( &EV_Activate , this );
}
}
if( ent->IsType( idActor::Type ) || ( ent->IsType( idAFAttachment::Type ) && static_cast<const idAFAttachment*>( ent )->GetBody()->IsType( idActor::Type ) ) )
{
if( !projectileFlags.detonate_on_actor )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else
{
if( !projectileFlags.detonate_on_world )
{
if( !StartSound( "snd_ricochet", SND_CHANNEL_ITEM, 0, true, NULL ) )
{
2012-11-26 18:58:24 +00:00
float len = velocity.Length();
if( len > BOUNCE_SOUND_MIN_VELOCITY )
{
2012-11-26 18:58:24 +00:00
SetSoundVolume( len > BOUNCE_SOUND_MAX_VELOCITY ? 1.0f : idMath::Sqrt( len - BOUNCE_SOUND_MIN_VELOCITY ) * ( 1.0f / idMath::Sqrt( BOUNCE_SOUND_MAX_VELOCITY - BOUNCE_SOUND_MIN_VELOCITY ) ) );
StartSound( "snd_bounce", SND_CHANNEL_ANY, 0, true, NULL );
}
}
return false;
}
}
2012-11-26 18:58:24 +00:00
SetOrigin( collision.endpos );
SetAxis( collision.endAxis );
2012-11-26 18:58:24 +00:00
// To see the explosion on the collision surface instead of
// at the muzzle, clear the deltas.
CreateDeltasFromOldOriginAndAxis( GetPhysics()->GetOrigin(), GetPhysics()->GetAxis() );
2012-11-26 18:58:24 +00:00
// unlink the clip model because we no longer need it
GetPhysics()->UnlinkClip();
2012-11-26 18:58:24 +00:00
damageDefName = spawnArgs.GetString( "def_damage" );
2012-11-26 18:58:24 +00:00
ignore = NULL;
2012-11-26 18:58:24 +00:00
// if the projectile causes a damage effect
if( spawnArgs.GetBool( "impact_damage_effect" ) )
{
2012-11-26 18:58:24 +00:00
// if the hit entity has a special damage effect
if( ent->spawnArgs.GetBool( "bleed" ) )
{
2012-11-26 18:58:24 +00:00
ent->AddDamageEffect( collision, velocity, damageDefName );
}
else
{
2012-11-26 18:58:24 +00:00
AddDefaultDamageEffect( collision, velocity );
}
}
2012-11-26 18:58:24 +00:00
// if the hit entity takes damage
if( ent->fl.takedamage )
{
if( damagePower )
{
2012-11-26 18:58:24 +00:00
damageScale = damagePower;
}
else
{
2012-11-26 18:58:24 +00:00
damageScale = 1.0f;
}
2012-11-26 18:58:24 +00:00
// if the projectile owner is a player
if( owner.GetEntity() && owner.GetEntity()->IsType( idPlayer::Type ) )
{
2012-11-26 18:58:24 +00:00
// if the projectile hit an actor
if( ent->IsType( idActor::Type ) )
{
idPlayer* player = static_cast<idPlayer*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
player->AddProjectileHits( 1 );
damageScale *= player->PowerUpModifier( PROJECTILE_DAMAGE );
}
}
if( damageDefName[0] != '\0' )
{
2012-11-26 18:58:24 +00:00
bool killedByImpact = true;
if( ent->health <= 0 )
{
2012-11-26 18:58:24 +00:00
killedByImpact = false;
}
2012-11-26 18:58:24 +00:00
// Only handle the server's own attacks here. Attacks by other players on the server occur through
// reliable messages.
if( !common->IsMultiplayer() || common->IsClient() || ( common->IsServer() && owner.GetEntityNum() == gameLocal.GetLocalClientNum() ) || ( common->IsServer() && !isHitscan ) )
{
2012-11-26 18:58:24 +00:00
ent->Damage( this, owner.GetEntity(), dir, damageDefName, damageScale, CLIPMODEL_ID_TO_JOINT_HANDLE( collision.c.id ) );
}
else
{
if( ent->IsType( idPlayer::Type ) == false && common->IsServer() )
{
2012-11-26 18:58:24 +00:00
ent->Damage( this, owner.GetEntity(), dir, damageDefName, damageScale, CLIPMODEL_ID_TO_JOINT_HANDLE( collision.c.id ) );
}
}
2012-11-26 18:58:24 +00:00
// Check if we are hitting an actor. and see if we killed him.
if( !common->IsClient() && ent->health <= 0 && killedByImpact )
{
if( owner.GetEntity() && owner.GetEntity()->IsType( idPlayer::Type ) )
{
if( ent->IsType( idActor::Type ) && ent != owner.GetEntity() )
{
idPlayer* player = static_cast<idPlayer*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
player->AddProjectileKills();
}
}
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
ignore = ent;
}
}
2012-11-26 18:58:24 +00:00
Explode( collision, ignore );
if( !common->IsClient() && owner.GetEntity() != NULL && owner.GetEntity()->IsType( idPlayer::Type ) )
{
idPlayer* player = static_cast<idPlayer*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
int kills = player->GetProjectileKills();
if( kills >= 2 && common->IsMultiplayer() && strstr( GetName(), "projectile_rocket" ) != 0 )
{
2012-11-26 18:58:24 +00:00
player->GetAchievementManager().EventCompletesAchievement( ACHIEVEMENT_MP_KILL_2_GUYS_IN_ROOM_WITH_BFG );
}
// projectile is done dealing damage.
2012-11-26 18:58:24 +00:00
player->ResetProjectileKills();
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
=================
idProjectile::DefaultDamageEffect
=================
*/
void idProjectile::DefaultDamageEffect( idEntity* soundEnt, const idDict& projectileDef, const trace_t& collision, const idVec3& velocity )
{
const char* decal, *sound, *typeName;
2012-11-26 18:58:24 +00:00
surfTypes_t materialType;
if( collision.c.material != NULL )
{
2012-11-26 18:58:24 +00:00
materialType = collision.c.material->GetSurfaceType();
}
else
{
2012-11-26 18:58:24 +00:00
materialType = SURFTYPE_METAL;
}
2012-11-26 18:58:24 +00:00
// get material type name
typeName = gameLocal.sufaceTypeNames[ materialType ];
2012-11-26 18:58:24 +00:00
// play impact sound
sound = projectileDef.GetString( va( "snd_%s", typeName ) );
if( *sound == '\0' )
{
2012-11-26 18:58:24 +00:00
sound = projectileDef.GetString( "snd_metal" );
}
if( *sound == '\0' )
{
2012-11-26 18:58:24 +00:00
sound = projectileDef.GetString( "snd_impact" );
}
if( *sound != '\0' )
{
2012-11-26 18:58:24 +00:00
soundEnt->StartSoundShader( declManager->FindSound( sound ), SND_CHANNEL_BODY, 0, false, NULL );
}
2012-11-26 18:58:24 +00:00
// project decal
decal = projectileDef.GetString( va( "mtr_detonate_%s", typeName ) );
if( *decal == '\0' )
{
2012-11-26 18:58:24 +00:00
decal = projectileDef.GetString( "mtr_detonate" );
}
if( *decal != '\0' )
{
2012-11-26 18:58:24 +00:00
gameLocal.ProjectDecal( collision.c.point, -collision.c.normal, 8.0f, true, projectileDef.GetFloat( "decal_size", "6.0" ), decal );
}
}
/*
=================
idProjectile::AddDefaultDamageEffect
=================
*/
void idProjectile::AddDefaultDamageEffect( const trace_t& collision, const idVec3& velocity )
{
2012-11-26 18:58:24 +00:00
DefaultDamageEffect( this, spawnArgs, collision, velocity );
if( common->IsServer() && fl.networkSync )
{
2012-11-26 18:58:24 +00:00
idBitMsg msg;
byte msgBuf[MAX_EVENT_PARAM_SIZE];
lobbyUserID_t excluding;
if( spawnArgs.GetBool( "net_instanthit" ) && owner.GetEntityNum() < MAX_PLAYERS )
{
2012-11-26 18:58:24 +00:00
excluding = gameLocal.lobbyUserIDs[owner.GetEntityNum()];
}
2012-11-26 18:58:24 +00:00
msg.InitWrite( msgBuf, sizeof( msgBuf ) );
msg.BeginWriting();
msg.WriteFloat( collision.c.point[0] );
msg.WriteFloat( collision.c.point[1] );
msg.WriteFloat( collision.c.point[2] );
msg.WriteDir( collision.c.normal, 24 );
msg.WriteLong( ( collision.c.material != NULL ) ? gameLocal.ServerRemapDecl( -1, DECL_MATERIAL, collision.c.material->Index() ) : -1 );
msg.WriteFloat( velocity[0], 5, 10 );
msg.WriteFloat( velocity[1], 5, 10 );
msg.WriteFloat( velocity[2], 5, 10 );
ServerSendEvent( EVENT_DAMAGE_EFFECT, &msg, false, excluding );
}
}
/*
================
idProjectile::Killed
================
*/
void idProjectile::Killed( idEntity* inflictor, idEntity* attacker, int damage, const idVec3& dir, int location )
{
if( spawnArgs.GetBool( "detonate_on_death" ) )
{
2012-11-26 18:58:24 +00:00
trace_t collision;
2012-11-26 18:58:24 +00:00
memset( &collision, 0, sizeof( collision ) );
collision.endAxis = GetPhysics()->GetAxis();
collision.endpos = GetPhysics()->GetOrigin();
collision.c.point = GetPhysics()->GetOrigin();
collision.c.normal.Set( 0, 0, 1 );
Explode( collision, NULL );
physicsObj.ClearContacts();
physicsObj.PutToRest();
}
else
{
2012-11-26 18:58:24 +00:00
Fizzle();
}
}
/*
================
idProjectile::Fizzle
================
*/
void idProjectile::Fizzle()
{
2012-11-26 18:58:24 +00:00
if( state == EXPLODED || state == FIZZLED )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
StopSound( SND_CHANNEL_BODY, false );
StartSound( "snd_fizzle", SND_CHANNEL_BODY, 0, false, NULL );
2012-11-26 18:58:24 +00:00
// fizzle FX
const char* psystem = spawnArgs.GetString( "smoke_fuse" );
if( psystem != NULL && *psystem != NULL )
{
2012-11-26 18:58:24 +00:00
//FIXME:SMOKE gameLocal.particles->SpawnParticles( GetPhysics()->GetOrigin(), vec3_origin, psystem );
}
2012-11-26 18:58:24 +00:00
// we need to work out how long the effects last and then remove them at that time
// for example, bullets have no real effects
if( smokeFly && smokeFlyTime )
{
2012-11-26 18:58:24 +00:00
smokeFlyTime = 0;
}
2012-11-26 18:58:24 +00:00
fl.takedamage = false;
physicsObj.SetContents( 0 );
physicsObj.GetClipModel()->Unlink();
physicsObj.PutToRest();
2012-11-26 18:58:24 +00:00
Hide();
FreeLightDef();
2012-11-26 18:58:24 +00:00
state = FIZZLED;
if( common->IsClient() && !fl.skipReplication )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
CancelEvents( &EV_Fizzle );
PostEventMS( &EV_Remove, spawnArgs.GetInt( "remove_time", "1500" ) );
}
/*
================
idProjectile::Event_RadiusDamage
================
*/
void idProjectile::Event_RadiusDamage( idEntity* ignore )
{
const char* splash_damage = spawnArgs.GetString( "def_splash_damage" );
if( splash_damage[0] != '\0' )
{
2012-11-26 18:58:24 +00:00
gameLocal.RadiusDamage( physicsObj.GetOrigin(), this, owner.GetEntity(), ignore, this, splash_damage, damagePower );
}
}
/*
================
idProjectile::Event_RadiusDamage
================
*/
void idProjectile::Event_GetProjectileState()
{
2012-11-26 18:58:24 +00:00
idThread::ReturnInt( state );
}
/*
================
idProjectile::Explode
================
*/
void idProjectile::Explode( const trace_t& collision, idEntity* ignore )
{
const char* fxname, *light_shader, *sndExplode;
2012-11-26 18:58:24 +00:00
float light_fadetime;
idVec3 normal;
int removeTime;
if( mNoExplodeDisappear )
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_Remove, 0 );
return;
}
if( state == EXPLODED || state == FIZZLED )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
// activate rumble for player
idPlayer* player = gameLocal.GetLocalPlayer();
2012-11-26 18:58:24 +00:00
const bool isHitscan = spawnArgs.GetBool( "net_instanthit" );
if( player != NULL && isHitscan == false )
{
2012-11-26 18:58:24 +00:00
// damage
const char* damageDefName = spawnArgs.GetString( "def_damage" );
const idDict* damageDef = gameLocal.FindEntityDefDict( damageDefName );
2012-11-26 18:58:24 +00:00
int damage;
if( damageDef != NULL )
{
2012-11-26 18:58:24 +00:00
damage = damageDef->GetInt( "damage" );
}
else
{
2012-11-26 18:58:24 +00:00
damage = 200;
}
float damageScale = idMath::ClampFloat( 0.25f, 1.0f, ( float )damage * ( 1.0f / 200.0f ) ); // 50...200 -> min...max rumble
2012-11-26 18:58:24 +00:00
// distance
float dist = ( GetPhysics()->GetOrigin() - player->GetPhysics()->GetOrigin() ).LengthFast();
float distScale = 1.0f - idMath::ClampFloat( 0.0f, 1.0f, ( dist * ( 1.0f / 4000.0f ) ) + 0.25f ); // 0...4000 -> max...min rumble
2012-11-26 18:58:24 +00:00
distScale *= damageScale; // apply damage scale here, weaker damage produces less rumble
2012-11-26 18:58:24 +00:00
// determine rumble
float highMag = distScale;
int highDuration = idMath::Ftoi( 300.0f * distScale );
float lowMag = distScale * 0.75f;
int lowDuration = idMath::Ftoi( 500.0f * distScale );
2012-11-26 18:58:24 +00:00
player->SetControllerShake( highMag, highDuration, lowMag, lowDuration );
}
2012-11-26 18:58:24 +00:00
// stop sound
StopSound( SND_CHANNEL_BODY2, false );
2012-11-26 18:58:24 +00:00
// play explode sound
switch( ( int ) damagePower )
{
case 2:
sndExplode = "snd_explode2";
break;
case 3:
sndExplode = "snd_explode3";
break;
case 4:
sndExplode = "snd_explode4";
break;
default:
sndExplode = "snd_explode";
break;
2012-11-26 18:58:24 +00:00
}
StartSound( sndExplode, SND_CHANNEL_BODY, 0, true, NULL );
2012-11-26 18:58:24 +00:00
// we need to work out how long the effects last and then remove them at that time
// for example, bullets have no real effects
if( smokeFly && smokeFlyTime )
{
2012-11-26 18:58:24 +00:00
smokeFlyTime = 0;
}
2012-11-26 18:58:24 +00:00
Hide();
FreeLightDef();
if( spawnArgs.GetVector( "detonation_axis", "", normal ) )
{
2012-11-26 18:58:24 +00:00
GetPhysics()->SetAxis( normal.ToMat3() );
}
GetPhysics()->SetOrigin( collision.endpos + 2.0f * collision.c.normal );
2012-11-26 18:58:24 +00:00
// default remove time
if( fl.skipReplication && !spawnArgs.GetBool( "net_instanthit" ) )
{
2012-11-26 18:58:24 +00:00
removeTime = spawnArgs.GetInt( "remove_time", "6000" );
}
else
{
2012-11-26 18:58:24 +00:00
removeTime = spawnArgs.GetInt( "remove_time", "1500" );
}
2012-11-26 18:58:24 +00:00
// change the model, usually to a PRT
fxname = NULL;
if( g_testParticle.GetInteger() == TEST_PARTICLE_IMPACT )
{
2012-11-26 18:58:24 +00:00
fxname = g_testParticleName.GetString();
}
else
{
2012-11-26 18:58:24 +00:00
fxname = spawnArgs.GetString( "model_detonate" );
}
2012-11-26 18:58:24 +00:00
int surfaceType = collision.c.material != NULL ? collision.c.material->GetSurfaceType() : SURFTYPE_METAL;
if( !( fxname != NULL && *fxname != NULL ) )
{
if( ( surfaceType == SURFTYPE_NONE ) || ( surfaceType == SURFTYPE_METAL ) || ( surfaceType == SURFTYPE_STONE ) )
{
2012-11-26 18:58:24 +00:00
fxname = spawnArgs.GetString( "model_smokespark" );
}
else if( surfaceType == SURFTYPE_RICOCHET )
{
2012-11-26 18:58:24 +00:00
fxname = spawnArgs.GetString( "model_ricochet" );
}
else
{
2012-11-26 18:58:24 +00:00
fxname = spawnArgs.GetString( "model_smoke" );
}
}
2012-11-26 18:58:24 +00:00
// If the explosion is in liquid, spawn a particle splash
idVec3 testOrg = GetPhysics()->GetOrigin();
int testC = gameLocal.clip.Contents( testOrg, NULL, mat3_identity, CONTENTS_WATER, this );
if( testC & CONTENTS_WATER )
{
idFuncEmitter* splashEnt;
2012-11-26 18:58:24 +00:00
idDict splashArgs;
2012-11-26 18:58:24 +00:00
splashArgs.Set( "model", "sludgebulletimpact.prt" );
splashArgs.Set( "start_off", "1" );
splashEnt = static_cast<idFuncEmitter*>( gameLocal.SpawnEntityType( idFuncEmitter::Type, &splashArgs ) );
2012-11-26 18:58:24 +00:00
splashEnt->GetPhysics()->SetOrigin( testOrg );
splashEnt->PostEventMS( &EV_Activate, 0, this );
splashEnt->PostEventMS( &EV_Remove, 1500 );
2012-11-26 18:58:24 +00:00
// HACK - if this is a chaingun bullet, don't do the normal effect
if( !idStr::Cmp( spawnArgs.GetString( "def_damage" ), "damage_bullet_chaingun" ) )
{
2012-11-26 18:58:24 +00:00
fxname = NULL;
}
}
if( fxname && *fxname )
{
2012-11-26 18:58:24 +00:00
SetModel( fxname );
renderEntity.shaderParms[SHADERPARM_RED] =
renderEntity.shaderParms[SHADERPARM_GREEN] =
renderEntity.shaderParms[SHADERPARM_BLUE] =
renderEntity.shaderParms[SHADERPARM_ALPHA] = 1.0f;
2012-11-26 18:58:24 +00:00
renderEntity.shaderParms[SHADERPARM_TIMEOFFSET] = -MS2SEC( gameLocal.time );
renderEntity.shaderParms[SHADERPARM_DIVERSITY] = gameLocal.random.CRandomFloat();
Show();
removeTime = ( removeTime > 3000 ) ? removeTime : 3000;
}
2012-11-26 18:58:24 +00:00
// explosion light
light_shader = spawnArgs.GetString( "mtr_explode_light_shader" );
if( gameLocal.mpGame.IsGametypeFlagBased() && gameLocal.serverInfo.GetBool( "si_midnight" ) )
2012-11-26 18:58:24 +00:00
{
light_shader = "lights/midnight_grenade";
}
if( *light_shader )
{
2012-11-26 18:58:24 +00:00
renderLight.shader = declManager->FindMaterial( light_shader, false );
renderLight.pointLight = true;
renderLight.lightRadius[0] =
renderLight.lightRadius[1] =
renderLight.lightRadius[2] = spawnArgs.GetFloat( "explode_light_radius" );
2012-11-26 18:58:24 +00:00
#ifdef ID_PC
renderLight.lightRadius *= 2.0f;
renderLight.forceShadows = true;
#endif
2012-11-26 18:58:24 +00:00
// Midnight ctf
if( gameLocal.mpGame.IsGametypeFlagBased() && gameLocal.serverInfo.GetBool( "si_midnight" ) )
{
2012-11-26 18:58:24 +00:00
renderLight.lightRadius[0] =
renderLight.lightRadius[1] =
renderLight.lightRadius[2] = spawnArgs.GetFloat( "explode_light_radius" ) * 2;
2012-11-26 18:58:24 +00:00
}
spawnArgs.GetVector( "explode_light_color", "1 1 1", lightColor );
renderLight.shaderParms[SHADERPARM_RED] = lightColor.x;
renderLight.shaderParms[SHADERPARM_GREEN] = lightColor.y;
renderLight.shaderParms[SHADERPARM_BLUE] = lightColor.z;
renderLight.shaderParms[SHADERPARM_ALPHA] = 1.0f;
renderLight.shaderParms[SHADERPARM_TIMEOFFSET] = -MS2SEC( gameLocal.time );
2012-11-26 18:58:24 +00:00
// Midnight ctf
if( gameLocal.mpGame.IsGametypeFlagBased() && gameLocal.serverInfo.GetBool( "si_midnight" ) )
{
2012-11-26 18:58:24 +00:00
light_fadetime = 3.0f;
}
else
{
2012-11-26 18:58:24 +00:00
light_fadetime = spawnArgs.GetFloat( "explode_light_fadetime", "0.5" );
}
2012-11-26 18:58:24 +00:00
lightStartTime = gameLocal.time;
lightEndTime = MSEC_ALIGN_TO_FRAME( gameLocal.time + SEC2MS( light_fadetime ) );
BecomeActive( TH_THINK );
}
2012-11-26 18:58:24 +00:00
fl.takedamage = false;
physicsObj.SetContents( 0 );
physicsObj.PutToRest();
2012-11-26 18:58:24 +00:00
state = EXPLODED;
if( common->IsClient() && !fl.skipReplication )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
// alert the ai
gameLocal.AlertAI( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
// bind the projectile to the impact entity if necesary
if( gameLocal.entities[collision.c.entityNum] && spawnArgs.GetBool( "bindOnImpact" ) )
{
2012-11-26 18:58:24 +00:00
Bind( gameLocal.entities[collision.c.entityNum], true );
}
2012-11-26 18:58:24 +00:00
// splash damage
if( !projectileFlags.noSplashDamage )
{
2012-11-26 18:58:24 +00:00
float delay = spawnArgs.GetFloat( "delay_splash" );
if( delay )
{
if( removeTime < delay * 1000 )
{
2012-11-26 18:58:24 +00:00
removeTime = ( delay + 0.10 ) * 1000;
}
PostEventSec( &EV_RadiusDamage, delay, ignore );
}
else
{
2012-11-26 18:58:24 +00:00
Event_RadiusDamage( ignore );
}
}
2012-11-26 18:58:24 +00:00
// spawn debris entities
int fxdebris = spawnArgs.GetInt( "debris_count" );
if( fxdebris )
{
const idDict* debris = gameLocal.FindEntityDefDict( "projectile_debris", false );
if( debris )
{
2012-11-26 18:58:24 +00:00
int amount = gameLocal.random.RandomInt( fxdebris );
for( int i = 0; i < amount; i++ )
{
idEntity* ent;
2012-11-26 18:58:24 +00:00
idVec3 dir;
dir.x = gameLocal.random.CRandomFloat() * 4.0f;
dir.y = gameLocal.random.CRandomFloat() * 4.0f;
dir.z = gameLocal.random.RandomFloat() * 8.0f;
dir.Normalize();
2012-11-26 18:58:24 +00:00
gameLocal.SpawnEntityDef( *debris, &ent, false );
if( ent == NULL || !ent->IsType( idDebris::Type ) )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "'projectile_debris' is not an idDebris" );
return;
}
idDebris* debris = static_cast<idDebris*>( ent );
2012-11-26 18:58:24 +00:00
debris->Create( owner.GetEntity(), physicsObj.GetOrigin(), dir.ToMat3() );
debris->Launch();
}
}
debris = gameLocal.FindEntityDefDict( "projectile_shrapnel", false );
if( debris )
{
2012-11-26 18:58:24 +00:00
int amount = gameLocal.random.RandomInt( fxdebris );
for( int i = 0; i < amount; i++ )
{
idEntity* ent;
2012-11-26 18:58:24 +00:00
idVec3 dir;
dir.x = gameLocal.random.CRandomFloat() * 8.0f;
dir.y = gameLocal.random.CRandomFloat() * 8.0f;
dir.z = gameLocal.random.RandomFloat() * 8.0f + 8.0f;
dir.Normalize();
2012-11-26 18:58:24 +00:00
gameLocal.SpawnEntityDef( *debris, &ent, false );
if( ent == NULL || !ent->IsType( idDebris::Type ) )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "'projectile_shrapnel' is not an idDebris" );
break;
}
idDebris* debris = static_cast<idDebris*>( ent );
2012-11-26 18:58:24 +00:00
debris->Create( owner.GetEntity(), physicsObj.GetOrigin(), dir.ToMat3() );
debris->Launch();
}
}
}
2012-11-26 18:58:24 +00:00
CancelEvents( &EV_Explode );
PostEventMS( &EV_Remove, removeTime );
}
/*
================
idProjectile::GetVelocity
================
*/
idVec3 idProjectile::GetVelocity( const idDict* projectile )
{
2012-11-26 18:58:24 +00:00
idVec3 velocity;
2012-11-26 18:58:24 +00:00
projectile->GetVector( "velocity", "0 0 0", velocity );
return velocity;
}
/*
================
idProjectile::GetGravity
================
*/
idVec3 idProjectile::GetGravity( const idDict* projectile )
{
2012-11-26 18:58:24 +00:00
float gravity;
2012-11-26 18:58:24 +00:00
gravity = projectile->GetFloat( "gravity" );
return idVec3( 0, 0, -gravity );
}
/*
================
idProjectile::Event_Explode
================
*/
void idProjectile::Event_Explode()
{
2012-11-26 18:58:24 +00:00
trace_t collision;
2012-11-26 18:58:24 +00:00
memset( &collision, 0, sizeof( collision ) );
collision.endAxis = GetPhysics()->GetAxis();
collision.endpos = GetPhysics()->GetOrigin();
collision.c.point = GetPhysics()->GetOrigin();
collision.c.normal.Set( 0, 0, 1 );
AddDefaultDamageEffect( collision, collision.c.normal );
Explode( collision, NULL );
}
/*
================
idProjectile::Event_Fizzle
================
*/
void idProjectile::Event_Fizzle()
{
2012-11-26 18:58:24 +00:00
Fizzle();
}
/*
================
idProjectile::Event_Touch
================
*/
void idProjectile::Event_Touch( idEntity* other, trace_t* trace )
{
if( common->IsClient() )
{
2012-11-26 18:58:24 +00:00
return;
}
if( IsHidden() )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
// Projectiles do not collide with flags
if( other->IsType( idItemTeam::Type ) )
{
2012-11-26 18:58:24 +00:00
return;
}
if( other != owner.GetEntity() )
{
2012-11-26 18:58:24 +00:00
trace_t collision;
2012-11-26 18:58:24 +00:00
memset( &collision, 0, sizeof( collision ) );
collision.endAxis = GetPhysics()->GetAxis();
collision.endpos = GetPhysics()->GetOrigin();
collision.c.point = GetPhysics()->GetOrigin();
collision.c.normal.Set( 0, 0, 1 );
AddDefaultDamageEffect( collision, collision.c.normal );
Explode( collision, NULL );
}
}
/*
================
idProjectile::CatchProjectile
================
*/
void idProjectile::CatchProjectile( idEntity* o, const char* reflectName )
{
idEntity* prevowner = owner.GetEntity();
2012-11-26 18:58:24 +00:00
owner = o;
physicsObj.GetClipModel()->SetOwner( o );
if( this->IsType( idGuidedProjectile::Type ) )
{
idGuidedProjectile* proj = static_cast<idGuidedProjectile*>( this );
2012-11-26 18:58:24 +00:00
proj->SetEnemy( prevowner );
}
2012-11-26 18:58:24 +00:00
idStr s = spawnArgs.GetString( "def_damage" );
s += reflectName;
const idDict* damageDef = gameLocal.FindEntityDefDict( s, false );
if( damageDef )
{
spawnArgs.Set( "def_damage", s );
2012-11-26 18:58:24 +00:00
}
}
/*
================
idProjectile::GetProjectileState
================
*/
int idProjectile::GetProjectileState()
{
2012-11-26 18:58:24 +00:00
return ( int )state;
2012-11-26 18:58:24 +00:00
}
/*
================
idProjectile::Event_CreateProjectile
================
*/
void idProjectile::Event_CreateProjectile( idEntity* owner, const idVec3& start, const idVec3& dir )
{
Create( owner, start, dir );
2012-11-26 18:58:24 +00:00
}
/*
================
idProjectile::Event_LaunchProjectile
================
*/
void idProjectile::Event_LaunchProjectile( const idVec3& start, const idVec3& dir, const idVec3& pushVelocity )
{
Launch( start, dir, pushVelocity );
2012-11-26 18:58:24 +00:00
}
/*
================
idProjectile::Event_SetGravity
================
*/
void idProjectile::Event_SetGravity( float gravity )
{
2012-11-26 18:58:24 +00:00
idVec3 gravVec;
2012-11-26 18:58:24 +00:00
gravVec = gameLocal.GetGravity();
gravVec.NormalizeFast();
physicsObj.SetGravity( gravVec * gravity );
2012-11-26 18:58:24 +00:00
}
/*
=================
idProjectile::ClientPredictionCollide
=================
*/
bool idProjectile::ClientPredictionCollide( idEntity* soundEnt, const idDict& projectileDef, const trace_t& collision, const idVec3& velocity, bool addDamageEffect )
{
idEntity* ent;
2012-11-26 18:58:24 +00:00
// remove projectile when a 'noimpact' surface is hit
if( collision.c.material && ( collision.c.material->GetSurfaceFlags() & SURF_NOIMPACT ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// get the entity the projectile collided with
ent = gameLocal.entities[ collision.c.entityNum ];
if( ent == NULL )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// don't do anything if hitting a noclip player
if( ent->IsType( idPlayer::Type ) && static_cast<idPlayer*>( ent )->noclip )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( ent->IsType( idActor::Type ) || ( ent->IsType( idAFAttachment::Type ) && static_cast<const idAFAttachment*>( ent )->GetBody()->IsType( idActor::Type ) ) )
{
if( !projectileDef.GetBool( "detonate_on_actor" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
else
{
if( !projectileDef.GetBool( "detonate_on_world" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
2012-11-26 18:58:24 +00:00
// if the projectile causes a damage effect
if( addDamageEffect && projectileDef.GetBool( "impact_damage_effect" ) )
{
2012-11-26 18:58:24 +00:00
// if the hit entity does not have a special damage effect
if( !ent->spawnArgs.GetBool( "bleed" ) )
{
2012-11-26 18:58:24 +00:00
// predict damage effect
DefaultDamageEffect( soundEnt, projectileDef, collision, velocity );
}
}
return true;
}
/*
================
idProjectile::ClientThink
================
*/
void idProjectile::ClientThink( const int curTime, const float fraction, const bool predict )
{
if( fl.skipReplication )
{
2012-11-26 18:58:24 +00:00
Think();
}
else
{
if( !renderEntity.hModel )
{
2012-11-26 18:58:24 +00:00
return;
}
InterpolatePhysicsOnly( fraction );
Present();
AddParticlesAndLight();
}
}
/*
================
idProjectile::ClientPredictionThink
================
*/
void idProjectile::ClientPredictionThink()
{
if( !renderEntity.hModel )
{
2012-11-26 18:58:24 +00:00
return;
}
Think();
}
/*
================
idProjectile::WriteToSnapshot
================
*/
void idProjectile::WriteToSnapshot( idBitMsg& msg ) const
{
2012-11-26 18:58:24 +00:00
msg.WriteBits( owner.GetSpawnId(), 32 );
msg.WriteBits( state, 3 );
msg.WriteBits( fl.hidden, 1 );
physicsObj.WriteToSnapshot( msg );
}
/*
================
idProjectile::ReadFromSnapshot
================
*/
void idProjectile::ReadFromSnapshot( const idBitMsg& msg )
{
2012-11-26 18:58:24 +00:00
projectileState_t newState;
2012-11-26 18:58:24 +00:00
owner.SetSpawnId( msg.ReadBits( 32 ) );
newState = ( projectileState_t ) msg.ReadBits( 3 );
if( msg.ReadBits( 1 ) )
{
2012-11-26 18:58:24 +00:00
Hide();
}
else
{
2012-11-26 18:58:24 +00:00
Show();
}
while( state != newState )
{
switch( state )
{
case SPAWNED:
{
2012-11-26 18:58:24 +00:00
Create( owner.GetEntity(), vec3_origin, idVec3( 1, 0, 0 ) );
break;
}
case CREATED:
{
2012-11-26 18:58:24 +00:00
// the right origin and direction are required if you want bullet traces
Launch( vec3_origin, idVec3( 1, 0, 0 ), vec3_origin );
break;
}
case LAUNCHED:
{
if( newState == FIZZLED )
{
2012-11-26 18:58:24 +00:00
Fizzle();
}
else
{
2012-11-26 18:58:24 +00:00
trace_t collision;
memset( &collision, 0, sizeof( collision ) );
collision.endAxis = GetPhysics()->GetAxis();
collision.endpos = GetPhysics()->GetOrigin();
collision.c.point = GetPhysics()->GetOrigin();
collision.c.normal.Set( 0, 0, 1 );
Explode( collision, NULL );
}
break;
}
case FIZZLED:
case EXPLODED:
{
2012-11-26 18:58:24 +00:00
StopSound( SND_CHANNEL_BODY2, false );
gameEdit->ParseSpawnArgsToRenderEntity( &spawnArgs, &renderEntity );
state = SPAWNED;
break;
}
}
}
2012-11-26 18:58:24 +00:00
physicsObj.ReadFromSnapshot( msg );
if( msg.HasChanged() )
{
2012-11-26 18:58:24 +00:00
UpdateVisuals();
}
}
/*
================
idProjectile::ClientReceiveEvent
================
*/
bool idProjectile::ClientReceiveEvent( int event, int time, const idBitMsg& msg )
{
2012-11-26 18:58:24 +00:00
trace_t collision;
idVec3 velocity;
switch( event )
{
case EVENT_DAMAGE_EFFECT:
{
2012-11-26 18:58:24 +00:00
memset( &collision, 0, sizeof( collision ) );
collision.c.point[0] = msg.ReadFloat();
collision.c.point[1] = msg.ReadFloat();
collision.c.point[2] = msg.ReadFloat();
collision.c.normal = msg.ReadDir( 24 );
int index = gameLocal.ClientRemapDecl( DECL_MATERIAL, msg.ReadLong() );
collision.c.material = ( index != -1 ) ? static_cast<const idMaterial*>( declManager->DeclByIndex( DECL_MATERIAL, index ) ) : NULL;
2012-11-26 18:58:24 +00:00
velocity[0] = msg.ReadFloat( 5, 10 );
velocity[1] = msg.ReadFloat( 5, 10 );
velocity[2] = msg.ReadFloat( 5, 10 );
DefaultDamageEffect( this, spawnArgs, collision, velocity );
return true;
}
default:
{
2012-11-26 18:58:24 +00:00
return idEntity::ClientReceiveEvent( event, time, msg );
}
}
}
/*
========================
idProjectile::QueueToSimulate
========================
*/
void idProjectile::QueueToSimulate( int startTime )
{
2012-11-26 18:58:24 +00:00
assert( common->IsMultiplayer() && common->IsServer() );
for( int i = 0; i < MAX_SIMULATED_PROJECTILES; i++ )
{
if( projectilesToSimulate[i].projectile == NULL )
{
2012-11-26 18:58:24 +00:00
projectilesToSimulate[i].projectile = this;
projectilesToSimulate[i].startTime = startTime;
if( g_projectileDebug.GetBool() )
{
2012-11-26 18:58:24 +00:00
int delta = gameLocal.GetServerGameTimeMs() - startTime;
idLib::Printf( "Simulating projectile %d. Approx %d delay.\n", GetEntityNumber(), delta );
2012-11-26 18:58:24 +00:00
}
return;
}
}
idLib::Warning( "Unable to simulate more projectiles this frame" );
2012-11-26 18:58:24 +00:00
}
/*
========================
idProjectile::SimulateProjectileFrame
========================
*/
void idProjectile::SimulateProjectileFrame( int msec, int endTime )
{
idVec3 oldOrigin = GetPhysics()->GetOrigin();
2012-11-26 18:58:24 +00:00
GetPhysics()->Evaluate( msec, endTime );
SetOrigin( GetPhysics()->GetOrigin() );
SetAxis( GetPhysics()->GetAxis() );
if( g_projectileDebug.GetBool() )
{
2012-11-26 18:58:24 +00:00
float delta = ( GetPhysics()->GetOrigin() - oldOrigin ).Length();
idLib::Printf( "Simulated projectile %d. Delta: %.2f \n", GetEntityNumber(), delta );
//clientGame->renderWorld->DebugLine( idColor::colorYellow, oldOrigin, GetPhysics()->GetOrigin(), 5000 );
}
}
/*
========================
idProjectile::PostSimulate
========================
*/
void idProjectile::PostSimulate( int endTime )
{
if( state == EXPLODED || state == FIZZLED )
{
2012-11-26 18:58:24 +00:00
// Already exploded. To see the explosion on the collision surface instead of
// at the muzzle, don't set the deltas to the launch origin and axis.
CreateDeltasFromOldOriginAndAxis( GetPhysics()->GetOrigin(), GetPhysics()->GetAxis() );
}
else
{
2012-11-26 18:58:24 +00:00
CreateDeltasFromOldOriginAndAxis( launchOrigin, launchAxis );
}
}
/*
===============================================================================
idGuidedProjectile
===============================================================================
*/
const idEventDef EV_SetEnemy( "setEnemy", "E" );
CLASS_DECLARATION( idProjectile, idGuidedProjectile )
EVENT( EV_SetEnemy, idGuidedProjectile::Event_SetEnemy )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idGuidedProjectile::idGuidedProjectile
================
*/
idGuidedProjectile::idGuidedProjectile()
{
2012-11-26 18:58:24 +00:00
enemy = NULL;
speed = 0.0f;
turn_max = 0.0f;
clamp_dist = 0.0f;
rndScale = ang_zero;
rndAng = ang_zero;
rndUpdateTime = 0;
angles = ang_zero;
burstMode = false;
burstDist = 0;
burstVelocity = 0.0f;
unGuided = false;
}
/*
=================
idGuidedProjectile::~idGuidedProjectile
=================
*/
idGuidedProjectile::~idGuidedProjectile()
{
2012-11-26 18:58:24 +00:00
}
/*
================
idGuidedProjectile::Spawn
================
*/
void idGuidedProjectile::Spawn()
{
2012-11-26 18:58:24 +00:00
}
/*
================
idGuidedProjectile::Save
================
*/
void idGuidedProjectile::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
enemy.Save( savefile );
savefile->WriteFloat( speed );
savefile->WriteAngles( rndScale );
savefile->WriteAngles( rndAng );
savefile->WriteInt( rndUpdateTime );
savefile->WriteFloat( turn_max );
savefile->WriteFloat( clamp_dist );
savefile->WriteAngles( angles );
savefile->WriteBool( burstMode );
savefile->WriteBool( unGuided );
savefile->WriteFloat( burstDist );
savefile->WriteFloat( burstVelocity );
}
/*
================
idGuidedProjectile::Restore
================
*/
void idGuidedProjectile::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
enemy.Restore( savefile );
savefile->ReadFloat( speed );
savefile->ReadAngles( rndScale );
savefile->ReadAngles( rndAng );
savefile->ReadInt( rndUpdateTime );
savefile->ReadFloat( turn_max );
savefile->ReadFloat( clamp_dist );
savefile->ReadAngles( angles );
savefile->ReadBool( burstMode );
savefile->ReadBool( unGuided );
savefile->ReadFloat( burstDist );
savefile->ReadFloat( burstVelocity );
}
/*
================
idGuidedProjectile::GetSeekPos
================
*/
void idGuidedProjectile::GetSeekPos( idVec3& out )
{
idEntity* enemyEnt = enemy.GetEntity();
if( enemyEnt )
{
if( enemyEnt->IsType( idActor::Type ) )
{
out = static_cast<idActor*>( enemyEnt )->GetEyePosition();
2012-11-26 18:58:24 +00:00
out.z -= 12.0f;
}
else
{
2012-11-26 18:58:24 +00:00
out = enemyEnt->GetPhysics()->GetOrigin();
}
}
else
{
2012-11-26 18:58:24 +00:00
out = GetPhysics()->GetOrigin() + physicsObj.GetLinearVelocity() * 2.0f;
}
}
/*
================
idGuidedProjectile::Think
================
*/
void idGuidedProjectile::Think()
{
2012-11-26 18:58:24 +00:00
idVec3 dir;
idVec3 seekPos;
idVec3 velocity;
idVec3 nose;
idVec3 tmp;
idMat3 axis;
idAngles dirAng;
idAngles diff;
float dist;
float frac;
int i;
if( state == LAUNCHED && !unGuided )
{
2012-11-26 18:58:24 +00:00
GetSeekPos( seekPos );
if( rndUpdateTime < gameLocal.time )
{
2012-11-26 18:58:24 +00:00
rndAng[ 0 ] = rndScale[ 0 ] * gameLocal.random.CRandomFloat();
rndAng[ 1 ] = rndScale[ 1 ] * gameLocal.random.CRandomFloat();
rndAng[ 2 ] = rndScale[ 2 ] * gameLocal.random.CRandomFloat();
rndUpdateTime = gameLocal.time + 200;
}
2012-11-26 18:58:24 +00:00
nose = physicsObj.GetOrigin() + 10.0f * physicsObj.GetAxis()[0];
2012-11-26 18:58:24 +00:00
dir = seekPos - nose;
dist = dir.Normalize();
dirAng = dir.ToAngles();
2012-11-26 18:58:24 +00:00
// make it more accurate as it gets closer
frac = dist / clamp_dist;
if( frac > 1.0f )
{
2012-11-26 18:58:24 +00:00
frac = 1.0f;
}
2012-11-26 18:58:24 +00:00
diff = dirAng - angles + rndAng * frac;
2012-11-26 18:58:24 +00:00
// clamp the to the max turn rate
diff.Normalize180();
for( i = 0; i < 3; i++ )
{
if( diff[ i ] > turn_max )
{
2012-11-26 18:58:24 +00:00
diff[ i ] = turn_max;
}
else if( diff[ i ] < -turn_max )
{
2012-11-26 18:58:24 +00:00
diff[ i ] = -turn_max;
}
}
angles += diff;
2012-11-26 18:58:24 +00:00
// make the visual model always points the dir we're traveling
dir = angles.ToForward();
velocity = dir * speed;
if( burstMode && dist < burstDist )
{
2012-11-26 18:58:24 +00:00
unGuided = true;
velocity *= burstVelocity;
}
2012-11-26 18:58:24 +00:00
physicsObj.SetLinearVelocity( velocity );
2012-11-26 18:58:24 +00:00
// align z-axis of model with the direction
axis = dir.ToMat3();
tmp = axis[2];
axis[2] = axis[0];
axis[0] = -tmp;
2012-11-26 18:58:24 +00:00
GetPhysics()->SetAxis( axis );
}
2012-11-26 18:58:24 +00:00
idProjectile::Think();
}
/*
=================
idGuidedProjectile::Launch
=================
*/
void idGuidedProjectile::Launch( const idVec3& start, const idVec3& dir, const idVec3& pushVelocity, const float timeSinceFire, const float launchPower, float dmgPower )
{
2012-11-26 18:58:24 +00:00
idProjectile::Launch( start, dir, pushVelocity, timeSinceFire, launchPower, dmgPower );
if( owner.GetEntity() )
{
if( owner.GetEntity()->IsType( idAI::Type ) )
{
enemy = static_cast<idAI*>( owner.GetEntity() )->GetEnemy();
}
else if( owner.GetEntity()->IsType( idPlayer::Type ) )
{
2012-11-26 18:58:24 +00:00
trace_t tr;
idPlayer* player = static_cast<idPlayer*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
idVec3 start = player->GetEyePosition();
idVec3 end = start + player->viewAxis[0] * 1000.0f;
gameLocal.clip.TracePoint( tr, start, end, MASK_SHOT_RENDERMODEL | CONTENTS_BODY, owner.GetEntity() );
if( tr.fraction < 1.0f )
{
2012-11-26 18:58:24 +00:00
enemy = gameLocal.GetTraceEntity( tr );
}
2012-11-26 18:58:24 +00:00
// ignore actors on the player's team
if( enemy.GetEntity() == NULL || !enemy.GetEntity()->IsType( idActor::Type ) || ( static_cast<idActor*>( enemy.GetEntity() )->team == player->team ) )
{
2012-11-26 18:58:24 +00:00
enemy = player->EnemyWithMostHealth();
}
}
}
const idVec3& vel = physicsObj.GetLinearVelocity();
2012-11-26 18:58:24 +00:00
angles = vel.ToAngles();
speed = vel.Length();
rndScale = spawnArgs.GetAngles( "random", "15 15 0" );
turn_max = spawnArgs.GetFloat( "turn_max", "180" ) / com_engineHz_latched;
clamp_dist = spawnArgs.GetFloat( "clamp_dist", "256" );
burstMode = spawnArgs.GetBool( "burstMode" );
unGuided = false;
burstDist = spawnArgs.GetFloat( "burstDist", "64" );
burstVelocity = spawnArgs.GetFloat( "burstVelocity", "1.25" );
UpdateVisuals();
}
void idGuidedProjectile::SetEnemy( idEntity* ent )
{
2012-11-26 18:58:24 +00:00
enemy = ent;
}
void idGuidedProjectile::Event_SetEnemy( idEntity* ent )
{
SetEnemy( ent );
2012-11-26 18:58:24 +00:00
}
/*
===============================================================================
idSoulCubeMissile
===============================================================================
*/
CLASS_DECLARATION( idGuidedProjectile, idSoulCubeMissile )
END_CLASS
/*
================
idSoulCubeMissile::Spawn()
================
*/
void idSoulCubeMissile::Spawn()
{
2012-11-26 18:58:24 +00:00
startingVelocity.Zero();
endingVelocity.Zero();
accelTime = 0.0f;
launchTime = 0;
killPhase = false;
returnPhase = false;
smokeKillTime = 0;
smokeKill = NULL;
}
/*
=================
idSoulCubeMissile::~idSoulCubeMissile
=================
*/
idSoulCubeMissile::~idSoulCubeMissile()
{
2012-11-26 18:58:24 +00:00
}
/*
================
idSoulCubeMissile::Save
================
*/
void idSoulCubeMissile::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
savefile->WriteVec3( startingVelocity );
savefile->WriteVec3( endingVelocity );
savefile->WriteFloat( accelTime );
savefile->WriteInt( launchTime );
savefile->WriteBool( killPhase );
savefile->WriteBool( returnPhase );
savefile->WriteVec3( destOrg );
2012-11-26 18:58:24 +00:00
savefile->WriteInt( orbitTime );
savefile->WriteVec3( orbitOrg );
savefile->WriteInt( smokeKillTime );
savefile->WriteParticle( smokeKill );
}
/*
================
idSoulCubeMissile::Restore
================
*/
void idSoulCubeMissile::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
savefile->ReadVec3( startingVelocity );
savefile->ReadVec3( endingVelocity );
savefile->ReadFloat( accelTime );
savefile->ReadInt( launchTime );
savefile->ReadBool( killPhase );
savefile->ReadBool( returnPhase );
savefile->ReadVec3( destOrg );
2012-11-26 18:58:24 +00:00
savefile->ReadInt( orbitTime );
savefile->ReadVec3( orbitOrg );
savefile->ReadInt( smokeKillTime );
savefile->ReadParticle( smokeKill );
}
/*
================
idSoulCubeMissile::KillTarget
================
*/
void idSoulCubeMissile::KillTarget( const idVec3& dir )
{
idEntity* ownerEnt;
const char* smokeName;
idActor* act;
2012-11-26 18:58:24 +00:00
ReturnToOwner();
if( enemy.GetEntity() && enemy.GetEntity()->IsType( idActor::Type ) )
{
2012-11-26 18:58:24 +00:00
act = static_cast<idActor*>( enemy.GetEntity() );
killPhase = true;
orbitOrg = act->GetPhysics()->GetAbsBounds().GetCenter();
orbitTime = gameLocal.time;
smokeKillTime = 0;
smokeName = spawnArgs.GetString( "smoke_kill" );
if( *smokeName != '\0' )
{
smokeKill = static_cast<const idDeclParticle*>( declManager->FindType( DECL_PARTICLE, smokeName ) );
2012-11-26 18:58:24 +00:00
smokeKillTime = gameLocal.time;
}
ownerEnt = owner.GetEntity();
if( ( act->health > 0 ) && ownerEnt != NULL && ownerEnt->IsType( idPlayer::Type ) && ( ownerEnt->health > 0 ) && !act->spawnArgs.GetBool( "boss" ) )
{
static_cast<idPlayer*>( ownerEnt )->GiveHealthPool( act->health );
2012-11-26 18:58:24 +00:00
}
act->Damage( this, owner.GetEntity(), dir, spawnArgs.GetString( "def_damage" ), 1.0f, INVALID_JOINT );
act->GetAFPhysics()->SetTimeScale( 0.25 );
StartSound( "snd_explode", SND_CHANNEL_BODY, 0, false, NULL );
}
}
/*
================
idSoulCubeMissile::Think
================
*/
void idSoulCubeMissile::Think()
{
2012-11-26 18:58:24 +00:00
float pct;
idVec3 seekPos;
idEntity* ownerEnt;
if( state == LAUNCHED )
{
if( killPhase )
{
2012-11-26 18:58:24 +00:00
// orbit the mob, cascading down
if( gameLocal.time < orbitTime + 1500 )
{
if( !gameLocal.smokeParticles->EmitSmoke( smokeKill, smokeKillTime, gameLocal.random.CRandomFloat(), orbitOrg, mat3_identity, timeGroup /*_D3XP*/ ) )
{
2012-11-26 18:58:24 +00:00
smokeKillTime = gameLocal.time;
}
}
}
else
{
if( accelTime && gameLocal.time < launchTime + accelTime * 1000 )
{
2012-11-26 18:58:24 +00:00
pct = ( gameLocal.time - launchTime ) / ( accelTime * 1000 );
speed = ( startingVelocity + ( startingVelocity + endingVelocity ) * pct ).Length();
}
}
2012-11-26 18:58:24 +00:00
idGuidedProjectile::Think();
GetSeekPos( seekPos );
if( ( seekPos - physicsObj.GetOrigin() ).Length() < 32.0f )
{
if( returnPhase )
{
2012-11-26 18:58:24 +00:00
StopSound( SND_CHANNEL_ANY, false );
StartSound( "snd_return", SND_CHANNEL_BODY2, 0, false, NULL );
Hide();
PostEventSec( &EV_Remove, 2.0f );
2012-11-26 18:58:24 +00:00
ownerEnt = owner.GetEntity();
if( ownerEnt != NULL && ownerEnt->IsType( idPlayer::Type ) )
{
static_cast<idPlayer*>( ownerEnt )->SetSoulCubeProjectile( NULL );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
state = FIZZLED;
}
else if( !killPhase )
{
2012-11-26 18:58:24 +00:00
KillTarget( physicsObj.GetAxis()[0] );
}
}
}
}
/*
================
idSoulCubeMissile::GetSeekPos
================
*/
void idSoulCubeMissile::GetSeekPos( idVec3& out )
{
if( returnPhase && owner.GetEntity() && owner.GetEntity()->IsType( idActor::Type ) )
{
idActor* act = static_cast<idActor*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
out = act->GetEyePosition();
return;
}
if( destOrg != vec3_zero )
{
2012-11-26 18:58:24 +00:00
out = destOrg;
return;
}
2012-11-26 18:58:24 +00:00
idGuidedProjectile::GetSeekPos( out );
}
/*
================
idSoulCubeMissile::Event_ReturnToOwner
================
*/
void idSoulCubeMissile::ReturnToOwner()
{
2012-11-26 18:58:24 +00:00
speed *= 0.65f;
killPhase = false;
returnPhase = true;
smokeFlyTime = 0;
}
/*
=================
idSoulCubeMissile::Launch
=================
*/
void idSoulCubeMissile::Launch( const idVec3& start, const idVec3& dir, const idVec3& pushVelocity, const float timeSinceFire, const float launchPower, float dmgPower )
{
2012-11-26 18:58:24 +00:00
idVec3 newStart;
idVec3 offs;
idEntity* ownerEnt;
2012-11-26 18:58:24 +00:00
// push it out a little
newStart = start + dir * spawnArgs.GetFloat( "launchDist" );
offs = spawnArgs.GetVector( "launchOffset", "0 0 -4" );
newStart += offs;
idGuidedProjectile::Launch( newStart, dir, pushVelocity, timeSinceFire, launchPower, dmgPower );
if( enemy.GetEntity() == NULL || !enemy.GetEntity()->IsType( idActor::Type ) )
{
2012-11-26 18:58:24 +00:00
destOrg = start + dir * 256.0f;
}
else
{
2012-11-26 18:58:24 +00:00
destOrg.Zero();
}
physicsObj.SetClipMask( 0 ); // never collide.. think routine will decide when to detonate
startingVelocity = spawnArgs.GetVector( "startingVelocity", "15 0 0" );
endingVelocity = spawnArgs.GetVector( "endingVelocity", "1500 0 0" );
accelTime = spawnArgs.GetFloat( "accelTime", "5" );
physicsObj.SetLinearVelocity( startingVelocity.Length() * physicsObj.GetAxis()[2] );
launchTime = gameLocal.time;
killPhase = false;
UpdateVisuals();
ownerEnt = owner.GetEntity();
if( ownerEnt != NULL && ownerEnt->IsType( idPlayer::Type ) )
{
static_cast<idPlayer*>( ownerEnt )->SetSoulCubeProjectile( this );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
}
/*
===============================================================================
idBFGProjectile
===============================================================================
*/
const idEventDef EV_RemoveBeams( "<removeBeams>", NULL );
CLASS_DECLARATION( idProjectile, idBFGProjectile )
EVENT( EV_RemoveBeams, idBFGProjectile::Event_RemoveBeams )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
=================
idBFGProjectile::idBFGProjectile
=================
*/
idBFGProjectile::idBFGProjectile()
{
2012-11-26 18:58:24 +00:00
memset( &secondModel, 0, sizeof( secondModel ) );
secondModelDefHandle = -1;
nextDamageTime = 0;
}
/*
=================
idBFGProjectile::~idBFGProjectile
=================
*/
idBFGProjectile::~idBFGProjectile()
{
2012-11-26 18:58:24 +00:00
FreeBeams();
if( secondModelDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->FreeEntityDef( secondModelDefHandle );
secondModelDefHandle = -1;
}
}
/*
================
idBFGProjectile::Spawn
================
*/
void idBFGProjectile::Spawn()
{
2012-11-26 18:58:24 +00:00
beamTargets.Clear();
memset( &secondModel, 0, sizeof( secondModel ) );
secondModelDefHandle = -1;
const char* temp = spawnArgs.GetString( "model_two" );
if( temp != NULL && *temp != NULL )
{
2012-11-26 18:58:24 +00:00
secondModel.hModel = renderModelManager->FindModel( temp );
secondModel.bounds = secondModel.hModel->Bounds( &secondModel );
secondModel.shaderParms[ SHADERPARM_RED ] =
secondModel.shaderParms[ SHADERPARM_GREEN ] =
secondModel.shaderParms[ SHADERPARM_BLUE ] =
secondModel.shaderParms[ SHADERPARM_ALPHA ] = 1.0f;
2012-11-26 18:58:24 +00:00
secondModel.noSelfShadow = true;
secondModel.noShadow = true;
}
nextDamageTime = 0;
damageFreq = NULL;
}
/*
================
idBFGProjectile::Save
================
*/
void idBFGProjectile::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
savefile->WriteInt( beamTargets.Num() );
for( i = 0; i < beamTargets.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
beamTargets[i].target.Save( savefile );
savefile->WriteRenderEntity( beamTargets[i].renderEntity );
savefile->WriteInt( beamTargets[i].modelDefHandle );
}
2012-11-26 18:58:24 +00:00
savefile->WriteRenderEntity( secondModel );
savefile->WriteInt( secondModelDefHandle );
savefile->WriteInt( nextDamageTime );
savefile->WriteString( damageFreq );
}
/*
================
idBFGProjectile::Restore
================
*/
void idBFGProjectile::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
int i, num;
2012-11-26 18:58:24 +00:00
savefile->ReadInt( num );
beamTargets.SetNum( num );
for( i = 0; i < num; i++ )
{
2012-11-26 18:58:24 +00:00
beamTargets[i].target.Restore( savefile );
savefile->ReadRenderEntity( beamTargets[i].renderEntity );
savefile->ReadInt( beamTargets[i].modelDefHandle );
if( beamTargets[i].modelDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
beamTargets[i].modelDefHandle = gameRenderWorld->AddEntityDef( &beamTargets[i].renderEntity );
}
}
2012-11-26 18:58:24 +00:00
savefile->ReadRenderEntity( secondModel );
savefile->ReadInt( secondModelDefHandle );
savefile->ReadInt( nextDamageTime );
savefile->ReadString( damageFreq );
if( secondModelDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
secondModelDefHandle = gameRenderWorld->AddEntityDef( &secondModel );
}
}
/*
=================
idBFGProjectile::FreeBeams
=================
*/
void idBFGProjectile::FreeBeams()
{
for( int i = 0; i < beamTargets.Num(); i++ )
{
if( beamTargets[i].modelDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->FreeEntityDef( beamTargets[i].modelDefHandle );
beamTargets[i].modelDefHandle = -1;
}
}
idPlayer* player = gameLocal.GetLocalPlayer();
if( player )
{
2012-11-26 18:58:24 +00:00
player->playerView.EnableBFGVision( false );
}
}
/*
================
idBFGProjectile::Think
================
*/
void idBFGProjectile::Think()
{
if( state == LAUNCHED )
{
2012-11-26 18:58:24 +00:00
// update beam targets
for( int i = 0; i < beamTargets.Num(); i++ )
{
if( beamTargets[i].target.GetEntity() == NULL )
{
2012-11-26 18:58:24 +00:00
continue;
}
idPlayer* player = ( beamTargets[i].target.GetEntity()->IsType( idPlayer::Type ) ) ? static_cast<idPlayer*>( beamTargets[i].target.GetEntity() ) : NULL;
2012-11-26 18:58:24 +00:00
// Major hack for end boss. :(
idAnimatedEntity* beamEnt;
2012-11-26 18:58:24 +00:00
idVec3 org;
bool forceDamage = false;
beamEnt = static_cast<idAnimatedEntity*>( beamTargets[i].target.GetEntity() );
if( !idStr::Cmp( beamEnt->GetEntityDefName(), "monster_boss_d3xp_maledict" ) )
{
2012-11-26 18:58:24 +00:00
SetTimeState ts( beamEnt->timeGroup );
idMat3 temp;
jointHandle_t bodyJoint;
2012-11-26 18:58:24 +00:00
bodyJoint = beamEnt->GetAnimator()->GetJointHandle( "Chest1" );
beamEnt->GetJointWorldTransform( bodyJoint, gameLocal.time, org, temp );
2012-11-26 18:58:24 +00:00
forceDamage = true;
}
else
{
2012-11-26 18:58:24 +00:00
org = beamEnt->GetPhysics()->GetAbsBounds().GetCenter();
}
beamTargets[i].renderEntity.origin = GetPhysics()->GetOrigin();
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_BEAM_END_X ] = org.x;
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_BEAM_END_Y ] = org.y;
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_BEAM_END_Z ] = org.z;
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_RED ] =
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_GREEN ] =
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_BLUE ] =
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_ALPHA ] = 1.0f;
if( gameLocal.time > nextDamageTime )
{
2012-11-26 18:58:24 +00:00
bool bfgVision = true;
if( damageFreq && *( const char* )damageFreq && beamTargets[i].target.GetEntity() && ( forceDamage || beamTargets[i].target.GetEntity()->CanDamage( GetPhysics()->GetOrigin(), org ) ) )
{
2012-11-26 18:58:24 +00:00
org = beamTargets[i].target.GetEntity()->GetPhysics()->GetOrigin() - GetPhysics()->GetOrigin();
org.Normalize();
beamTargets[i].target.GetEntity()->Damage( this, owner.GetEntity(), org, damageFreq, ( damagePower ) ? damagePower : 1.0f, INVALID_JOINT );
}
else
{
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_RED ] =
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_GREEN ] =
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_BLUE ] =
beamTargets[i].renderEntity.shaderParms[ SHADERPARM_ALPHA ] = 0.0f;
2012-11-26 18:58:24 +00:00
bfgVision = false;
}
if( player )
{
2012-11-26 18:58:24 +00:00
player->playerView.EnableBFGVision( bfgVision );
}
nextDamageTime = gameLocal.time + BFG_DAMAGE_FREQUENCY;
}
gameRenderWorld->UpdateEntityDef( beamTargets[i].modelDefHandle, &beamTargets[i].renderEntity );
}
if( secondModelDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
secondModel.origin = GetPhysics()->GetOrigin();
gameRenderWorld->UpdateEntityDef( secondModelDefHandle, &secondModel );
}
2012-11-26 18:58:24 +00:00
idAngles ang;
2012-11-26 18:58:24 +00:00
ang.pitch = ( gameLocal.time & 4095 ) * 360.0f / -4096.0f;
ang.yaw = ang.pitch;
ang.roll = 0.0f;
SetAngles( ang );
2012-11-26 18:58:24 +00:00
ang.pitch = ( gameLocal.time & 2047 ) * 360.0f / -2048.0f;
ang.yaw = ang.pitch;
ang.roll = 0.0f;
secondModel.axis = ang.ToMat3();
2012-11-26 18:58:24 +00:00
UpdateVisuals();
}
2012-11-26 18:58:24 +00:00
idProjectile::Think();
}
/*
=================
idBFGProjectile::Launch
=================
*/
void idBFGProjectile::Launch( const idVec3& start, const idVec3& dir, const idVec3& pushVelocity, const float timeSinceFire, const float power, const float dmgPower )
{
2012-11-26 18:58:24 +00:00
idProjectile::Launch( start, dir, pushVelocity, 0.0f, power, dmgPower );
2012-11-26 18:58:24 +00:00
// dmgPower * radius is the target acquisition area
// acquisition should make sure that monsters are not dormant
2012-11-26 18:58:24 +00:00
// which will cut down on hitting monsters not actively fighting
// but saves on the traces making sure they are visible
// damage is not applied until the projectile explodes
idEntity* ent;
idEntity* entityList[ MAX_GENTITIES ];
2012-11-26 18:58:24 +00:00
int numListedEntities;
idBounds bounds;
idVec3 damagePoint;
2012-11-26 18:58:24 +00:00
float radius;
spawnArgs.GetFloat( "damageRadius", "512", radius );
bounds = idBounds( GetPhysics()->GetOrigin() ).Expand( radius );
2012-11-26 18:58:24 +00:00
float beamWidth = spawnArgs.GetFloat( "beam_WidthFly" );
const char* skin = spawnArgs.GetString( "skin_beam" );
2012-11-26 18:58:24 +00:00
memset( &secondModel, 0, sizeof( secondModel ) );
secondModelDefHandle = -1;
const char* temp = spawnArgs.GetString( "model_two" );
if( temp != NULL && *temp != NULL )
{
2012-11-26 18:58:24 +00:00
secondModel.hModel = renderModelManager->FindModel( temp );
secondModel.bounds = secondModel.hModel->Bounds( &secondModel );
secondModel.shaderParms[ SHADERPARM_RED ] =
secondModel.shaderParms[ SHADERPARM_GREEN ] =
secondModel.shaderParms[ SHADERPARM_BLUE ] =
secondModel.shaderParms[ SHADERPARM_ALPHA ] = 1.0f;
2012-11-26 18:58:24 +00:00
secondModel.noSelfShadow = true;
secondModel.noShadow = true;
secondModel.origin = GetPhysics()->GetOrigin();
secondModel.axis = GetPhysics()->GetAxis();
secondModelDefHandle = gameRenderWorld->AddEntityDef( &secondModel );
}
2012-11-26 18:58:24 +00:00
idVec3 delta( 15.0f, 15.0f, 15.0f );
//physicsObj.SetAngularExtrapolation( extrapolation_t(EXTRAPOLATION_LINEAR|EXTRAPOLATION_NOSTOP), gameLocal.time, 0, physicsObj.GetAxis().ToAngles(), delta, ang_zero );
2012-11-26 18:58:24 +00:00
// get all entities touching the bounds
numListedEntities = gameLocal.clip.EntitiesTouchingBounds( bounds, CONTENTS_BODY, entityList, MAX_GENTITIES );
for( int e = 0; e < numListedEntities; e++ )
{
2012-11-26 18:58:24 +00:00
ent = entityList[ e ];
assert( ent );
if( ent == this || ent == owner.GetEntity() || ent->IsHidden() || !ent->IsActive() || !ent->fl.takedamage || ent->health <= 0 || !ent->IsType( idActor::Type ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( !ent->CanDamage( GetPhysics()->GetOrigin(), damagePoint ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( ent->IsType( idPlayer::Type ) )
{
idPlayer* player = static_cast<idPlayer*>( ent );
2012-11-26 18:58:24 +00:00
player->playerView.EnableBFGVision( true );
}
2012-11-26 18:58:24 +00:00
beamTarget_t bt;
memset( &bt.renderEntity, 0, sizeof( renderEntity_t ) );
bt.renderEntity.origin = GetPhysics()->GetOrigin();
bt.renderEntity.axis = GetPhysics()->GetAxis();
bt.renderEntity.shaderParms[ SHADERPARM_BEAM_WIDTH ] = beamWidth;
bt.renderEntity.shaderParms[ SHADERPARM_RED ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_GREEN ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_BLUE ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_ALPHA ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_DIVERSITY] = gameLocal.random.CRandomFloat() * 0.75;
bt.renderEntity.hModel = renderModelManager->FindModel( "_beam" );
bt.renderEntity.callback = NULL;
bt.renderEntity.numJoints = 0;
bt.renderEntity.joints = NULL;
bt.renderEntity.bounds.Clear();
bt.renderEntity.customSkin = declManager->FindSkin( skin );
bt.target = ent;
bt.modelDefHandle = gameRenderWorld->AddEntityDef( &bt.renderEntity );
beamTargets.Append( bt );
}
2012-11-26 18:58:24 +00:00
// Major hack for end boss. :(
idAnimatedEntity* maledict = static_cast<idAnimatedEntity*>( gameLocal.FindEntity( "monster_boss_d3xp_maledict_1" ) );
if( maledict )
{
2012-11-26 18:58:24 +00:00
SetTimeState ts( maledict->timeGroup );
2012-11-26 18:58:24 +00:00
idVec3 realPoint;
idMat3 temp;
float dist;
jointHandle_t bodyJoint;
2012-11-26 18:58:24 +00:00
bodyJoint = maledict->GetAnimator()->GetJointHandle( "Chest1" );
maledict->GetJointWorldTransform( bodyJoint, gameLocal.time, realPoint, temp );
2012-11-26 18:58:24 +00:00
dist = idVec3( realPoint - GetPhysics()->GetOrigin() ).Length();
if( dist < radius )
{
2012-11-26 18:58:24 +00:00
beamTarget_t bt;
memset( &bt.renderEntity, 0, sizeof( renderEntity_t ) );
bt.renderEntity.origin = GetPhysics()->GetOrigin();
bt.renderEntity.axis = GetPhysics()->GetAxis();
bt.renderEntity.shaderParms[ SHADERPARM_BEAM_WIDTH ] = beamWidth;
bt.renderEntity.shaderParms[ SHADERPARM_RED ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_GREEN ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_BLUE ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_ALPHA ] = 1.0f;
bt.renderEntity.shaderParms[ SHADERPARM_DIVERSITY] = gameLocal.random.CRandomFloat() * 0.75;
bt.renderEntity.hModel = renderModelManager->FindModel( "_beam" );
bt.renderEntity.callback = NULL;
bt.renderEntity.numJoints = 0;
bt.renderEntity.joints = NULL;
bt.renderEntity.bounds.Clear();
bt.renderEntity.customSkin = declManager->FindSkin( skin );
bt.target = maledict;
bt.modelDefHandle = gameRenderWorld->AddEntityDef( &bt.renderEntity );
beamTargets.Append( bt );
2012-11-26 18:58:24 +00:00
numListedEntities++;
}
}
if( numListedEntities )
{
2012-11-26 18:58:24 +00:00
StartSound( "snd_beam", SND_CHANNEL_BODY2, 0, false, NULL );
}
damageFreq = spawnArgs.GetString( "def_damageFreq" );
nextDamageTime = gameLocal.time + BFG_DAMAGE_FREQUENCY;
UpdateVisuals();
}
/*
================
idProjectile::Event_RemoveBeams
================
*/
void idBFGProjectile::Event_RemoveBeams()
{
2012-11-26 18:58:24 +00:00
FreeBeams();
UpdateVisuals();
}
/*
================
idProjectile::Explode
================
*/
void idBFGProjectile::Explode( const trace_t& collision, idEntity* ignore )
{
2012-11-26 18:58:24 +00:00
int i;
idVec3 dmgPoint;
idVec3 dir;
float beamWidth;
float damageScale;
const char* damage;
idPlayer* player;
idEntity* ownerEnt;
2012-11-26 18:58:24 +00:00
ownerEnt = owner.GetEntity();
if( ownerEnt != NULL && ownerEnt->IsType( idPlayer::Type ) )
{
player = static_cast< idPlayer* >( ownerEnt );
}
else
{
2012-11-26 18:58:24 +00:00
player = NULL;
}
2012-11-26 18:58:24 +00:00
beamWidth = spawnArgs.GetFloat( "beam_WidthExplode" );
damage = spawnArgs.GetString( "def_damage" );
for( i = 0; i < beamTargets.Num(); i++ )
{
if( ( beamTargets[i].target.GetEntity() == NULL ) || ( ownerEnt == NULL ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( !beamTargets[i].target.GetEntity()->CanDamage( GetPhysics()->GetOrigin(), dmgPoint ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
beamTargets[i].renderEntity.shaderParms[SHADERPARM_BEAM_WIDTH] = beamWidth;
2012-11-26 18:58:24 +00:00
// if the hit entity takes damage
if( damagePower )
{
2012-11-26 18:58:24 +00:00
damageScale = damagePower;
}
else
{
2012-11-26 18:58:24 +00:00
damageScale = 1.0f;
}
2012-11-26 18:58:24 +00:00
// if the projectile owner is a player
if( player )
{
2012-11-26 18:58:24 +00:00
// if the projectile hit an actor
if( beamTargets[i].target.GetEntity()->IsType( idActor::Type ) )
{
2012-11-26 18:58:24 +00:00
player->SetLastHitTime( gameLocal.time );
player->AddProjectileHits( 1 );
damageScale *= player->PowerUpModifier( PROJECTILE_DAMAGE );
}
}
if( damage[0] && ( beamTargets[i].target.GetEntity()->entityNumber > gameLocal.numClients - 1 ) )
{
2012-11-26 18:58:24 +00:00
dir = beamTargets[i].target.GetEntity()->GetPhysics()->GetOrigin() - GetPhysics()->GetOrigin();
dir.Normalize();
beamTargets[i].target.GetEntity()->Damage( this, ownerEnt, dir, damage, damageScale, ( collision.c.id < 0 ) ? CLIPMODEL_ID_TO_JOINT_HANDLE( collision.c.id ) : INVALID_JOINT );
}
}
if( secondModelDefHandle >= 0 )
{
2012-11-26 18:58:24 +00:00
gameRenderWorld->FreeEntityDef( secondModelDefHandle );
secondModelDefHandle = -1;
}
if( ignore == NULL )
{
2012-11-26 18:58:24 +00:00
projectileFlags.noSplashDamage = true;
}
if( !common->IsClient() || fl.skipReplication )
{
if( ignore != NULL )
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_RemoveBeams, 750 );
}
else
{
2012-11-26 18:58:24 +00:00
PostEventMS( &EV_RemoveBeams, 0 );
}
}
2012-11-26 18:58:24 +00:00
return idProjectile::Explode( collision, ignore );
}
/*
===============================================================================
idDebris
===============================================================================
*/
CLASS_DECLARATION( idEntity, idDebris )
EVENT( EV_Explode, idDebris::Event_Explode )
EVENT( EV_Fizzle, idDebris::Event_Fizzle )
END_CLASS
/*
================
idDebris::Spawn
================
*/
void idDebris::Spawn()
{
2012-11-26 18:58:24 +00:00
owner = NULL;
smokeFly = NULL;
smokeFlyTime = 0;
}
/*
================
idDebris::Create
================
*/
void idDebris::Create( idEntity* owner, const idVec3& start, const idMat3& axis )
{
2012-11-26 18:58:24 +00:00
Unbind();
GetPhysics()->SetOrigin( start );
GetPhysics()->SetAxis( axis );
GetPhysics()->SetContents( 0 );
this->owner = owner;
smokeFly = NULL;
smokeFlyTime = 0;
sndBounce = NULL;
noGrab = true;
UpdateVisuals();
}
/*
=================
idDebris::idDebris
=================
*/
idDebris::idDebris()
{
2012-11-26 18:58:24 +00:00
owner = NULL;
smokeFly = NULL;
smokeFlyTime = 0;
sndBounce = NULL;
}
/*
=================
idDebris::~idDebris
=================
*/
idDebris::~idDebris()
{
2012-11-26 18:58:24 +00:00
}
/*
=================
idDebris::Save
=================
*/
void idDebris::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
owner.Save( savefile );
2012-11-26 18:58:24 +00:00
savefile->WriteStaticObject( physicsObj );
2012-11-26 18:58:24 +00:00
savefile->WriteParticle( smokeFly );
savefile->WriteInt( smokeFlyTime );
savefile->WriteSoundShader( sndBounce );
}
/*
=================
idDebris::Restore
=================
*/
void idDebris::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
owner.Restore( savefile );
2012-11-26 18:58:24 +00:00
savefile->ReadStaticObject( physicsObj );
RestorePhysics( &physicsObj );
2012-11-26 18:58:24 +00:00
savefile->ReadParticle( smokeFly );
savefile->ReadInt( smokeFlyTime );
savefile->ReadSoundShader( sndBounce );
}
/*
=================
idDebris::Launch
=================
*/
void idDebris::Launch()
{
2012-11-26 18:58:24 +00:00
float fuse;
idVec3 velocity;
idAngles angular_velocity;
float linear_friction;
float angular_friction;
float contact_friction;
float bounce;
float mass;
float gravity;
idVec3 gravVec;
bool randomVelocity;
idMat3 axis;
2012-11-26 18:58:24 +00:00
renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
2012-11-26 18:58:24 +00:00
spawnArgs.GetVector( "velocity", "0 0 0", velocity );
spawnArgs.GetAngles( "angular_velocity", "0 0 0", angular_velocity );
linear_friction = spawnArgs.GetFloat( "linear_friction" );
angular_friction = spawnArgs.GetFloat( "angular_friction" );
contact_friction = spawnArgs.GetFloat( "contact_friction" );
bounce = spawnArgs.GetFloat( "bounce" );
mass = spawnArgs.GetFloat( "mass" );
gravity = spawnArgs.GetFloat( "gravity" );
fuse = spawnArgs.GetFloat( "fuse" );
randomVelocity = spawnArgs.GetBool( "random_velocity" );
if( mass <= 0 )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "Invalid mass on '%s'\n", GetEntityDefName() );
}
if( randomVelocity )
{
2012-11-26 18:58:24 +00:00
velocity.x *= gameLocal.random.RandomFloat() + 0.5f;
velocity.y *= gameLocal.random.RandomFloat() + 0.5f;
velocity.z *= gameLocal.random.RandomFloat() + 0.5f;
}
if( health )
{
2012-11-26 18:58:24 +00:00
fl.takedamage = true;
}
2012-11-26 18:58:24 +00:00
gravVec = gameLocal.GetGravity();
gravVec.NormalizeFast();
axis = GetPhysics()->GetAxis();
2012-11-26 18:58:24 +00:00
Unbind();
2012-11-26 18:58:24 +00:00
physicsObj.SetSelf( this );
2012-11-26 18:58:24 +00:00
// check if a clip model is set
const char* clipModelName;
2012-11-26 18:58:24 +00:00
idTraceModel trm;
spawnArgs.GetString( "clipmodel", "", &clipModelName );
if( !clipModelName[0] )
{
2012-11-26 18:58:24 +00:00
clipModelName = spawnArgs.GetString( "model" ); // use the visual model
}
2012-11-26 18:58:24 +00:00
// load the trace model
if( !collisionModelManager->TrmFromModel( clipModelName, trm ) )
{
2012-11-26 18:58:24 +00:00
// default to a box
physicsObj.SetClipBox( renderEntity.bounds, 1.0f );
}
else
{
physicsObj.SetClipModel( new( TAG_PHYSICS_CLIP_ENTITY ) idClipModel( trm ), 1.0f );
}
2012-11-26 18:58:24 +00:00
physicsObj.GetClipModel()->SetOwner( owner.GetEntity() );
physicsObj.SetMass( mass );
physicsObj.SetFriction( linear_friction, angular_friction, contact_friction );
if( contact_friction == 0.0f )
{
2012-11-26 18:58:24 +00:00
physicsObj.NoContact();
}
physicsObj.SetBouncyness( bounce );
physicsObj.SetGravity( gravVec * gravity );
physicsObj.SetContents( 0 );
physicsObj.SetClipMask( MASK_SOLID | CONTENTS_MOVEABLECLIP );
physicsObj.SetLinearVelocity( axis[ 0 ] * velocity[ 0 ] + axis[ 1 ] * velocity[ 1 ] + axis[ 2 ] * velocity[ 2 ] );
physicsObj.SetAngularVelocity( angular_velocity.ToAngularVelocity() * axis );
physicsObj.SetOrigin( GetPhysics()->GetOrigin() );
physicsObj.SetAxis( axis );
SetPhysics( &physicsObj );
if( !common->IsClient() )
{
if( fuse <= 0 )
{
2012-11-26 18:58:24 +00:00
// run physics for 1 second
RunPhysics();
PostEventMS( &EV_Remove, 0 );
}
else if( spawnArgs.GetBool( "detonate_on_fuse" ) )
{
if( fuse < 0.0f )
{
2012-11-26 18:58:24 +00:00
fuse = 0.0f;
}
RunPhysics();
PostEventSec( &EV_Explode, fuse );
}
else
{
if( fuse < 0.0f )
{
2012-11-26 18:58:24 +00:00
fuse = 0.0f;
}
PostEventSec( &EV_Fizzle, fuse );
}
}
2012-11-26 18:58:24 +00:00
StartSound( "snd_fly", SND_CHANNEL_BODY, 0, false, NULL );
2012-11-26 18:58:24 +00:00
smokeFly = NULL;
smokeFlyTime = 0;
const char* smokeName = spawnArgs.GetString( "smoke_fly" );
if( *smokeName != '\0' )
{
smokeFly = static_cast<const idDeclParticle*>( declManager->FindType( DECL_PARTICLE, smokeName ) );
2012-11-26 18:58:24 +00:00
smokeFlyTime = gameLocal.time;
gameLocal.smokeParticles->EmitSmoke( smokeFly, smokeFlyTime, gameLocal.random.CRandomFloat(), GetPhysics()->GetOrigin(), GetPhysics()->GetAxis(), timeGroup /*_D3XP*/ );
}
const char* sndName = spawnArgs.GetString( "snd_bounce" );
if( *sndName != '\0' )
{
2012-11-26 18:58:24 +00:00
sndBounce = declManager->FindSound( sndName );
}
2012-11-26 18:58:24 +00:00
UpdateVisuals();
}
/*
================
idDebris::Think
================
*/
void idDebris::Think()
{
2012-11-26 18:58:24 +00:00
// run physics
RunPhysics();
Present();
if( smokeFly && smokeFlyTime )
{
if( !gameLocal.smokeParticles->EmitSmoke( smokeFly, smokeFlyTime, gameLocal.random.CRandomFloat(), GetPhysics()->GetOrigin(), GetPhysics()->GetAxis(), timeGroup /*_D3XP*/ ) )
{
2012-11-26 18:58:24 +00:00
smokeFlyTime = 0;
}
}
}
/*
================
idDebris::Killed
================
*/
void idDebris::Killed( idEntity* inflictor, idEntity* attacker, int damage, const idVec3& dir, int location )
{
if( spawnArgs.GetBool( "detonate_on_death" ) )
{
2012-11-26 18:58:24 +00:00
Explode();
}
else
{
2012-11-26 18:58:24 +00:00
Fizzle();
}
}
/*
=================
idDebris::Collide
=================
*/
bool idDebris::Collide( const trace_t& collision, const idVec3& velocity )
{
if( sndBounce != NULL )
{
2012-11-26 18:58:24 +00:00
StartSoundShader( sndBounce, SND_CHANNEL_BODY, 0, false, NULL );
}
sndBounce = NULL;
return false;
}
/*
================
idDebris::Fizzle
================
*/
void idDebris::Fizzle()
{
if( IsHidden() )
{
2012-11-26 18:58:24 +00:00
// already exploded
return;
}
2012-11-26 18:58:24 +00:00
StopSound( SND_CHANNEL_ANY, false );
StartSound( "snd_fizzle", SND_CHANNEL_BODY, 0, false, NULL );
2012-11-26 18:58:24 +00:00
// fizzle FX
const char* smokeName = spawnArgs.GetString( "smoke_fuse" );
if( *smokeName != '\0' )
{
smokeFly = static_cast<const idDeclParticle*>( declManager->FindType( DECL_PARTICLE, smokeName ) );
2012-11-26 18:58:24 +00:00
smokeFlyTime = gameLocal.time;
gameLocal.smokeParticles->EmitSmoke( smokeFly, smokeFlyTime, gameLocal.random.CRandomFloat(), GetPhysics()->GetOrigin(), GetPhysics()->GetAxis(), timeGroup /*_D3XP*/ );
}
2012-11-26 18:58:24 +00:00
fl.takedamage = false;
physicsObj.SetContents( 0 );
physicsObj.PutToRest();
2012-11-26 18:58:24 +00:00
Hide();
if( common->IsClient() && !fl.skipReplication )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
CancelEvents( &EV_Fizzle );
PostEventMS( &EV_Remove, 0 );
}
/*
================
idDebris::Explode
================
*/
void idDebris::Explode()
{
if( IsHidden() )
{
2012-11-26 18:58:24 +00:00
// already exploded
return;
}
2012-11-26 18:58:24 +00:00
StopSound( SND_CHANNEL_ANY, false );
StartSound( "snd_explode", SND_CHANNEL_BODY, 0, false, NULL );
2012-11-26 18:58:24 +00:00
Hide();
2012-11-26 18:58:24 +00:00
// these must not be "live forever" particle systems
smokeFly = NULL;
smokeFlyTime = 0;
const char* smokeName = spawnArgs.GetString( "smoke_detonate" );
if( *smokeName != '\0' )
{
smokeFly = static_cast<const idDeclParticle*>( declManager->FindType( DECL_PARTICLE, smokeName ) );
2012-11-26 18:58:24 +00:00
smokeFlyTime = gameLocal.time;
gameLocal.smokeParticles->EmitSmoke( smokeFly, smokeFlyTime, gameLocal.random.CRandomFloat(), GetPhysics()->GetOrigin(), GetPhysics()->GetAxis(), timeGroup /*_D3XP*/ );
}
2012-11-26 18:58:24 +00:00
fl.takedamage = false;
physicsObj.SetContents( 0 );
physicsObj.PutToRest();
2012-11-26 18:58:24 +00:00
CancelEvents( &EV_Explode );
PostEventMS( &EV_Remove, 0 );
}
/*
================
idDebris::Event_Explode
================
*/
void idDebris::Event_Explode()
{
2012-11-26 18:58:24 +00:00
Explode();
}
/*
================
idDebris::Event_Fizzle
================
*/
void idDebris::Event_Fizzle()
{
2012-11-26 18:58:24 +00:00
Fizzle();
}
/*
===============================================================================
idHomingProjectile
===============================================================================
*/
CLASS_DECLARATION( idProjectile, idHomingProjectile )
EVENT( EV_SetEnemy, idHomingProjectile::Event_SetEnemy )
2012-11-26 18:58:24 +00:00
END_CLASS
/*
================
idHomingProjectile::idHomingProjectile
================
*/
idHomingProjectile::idHomingProjectile()
{
2012-11-26 18:58:24 +00:00
enemy = NULL;
speed = 0.0f;
turn_max = 0.0f;
clamp_dist = 0.0f;
rndScale = ang_zero;
rndAng = ang_zero;
angles = ang_zero;
burstMode = false;
burstDist = 0;
burstVelocity = 0.0f;
unGuided = false;
seekPos = vec3_origin;
}
/*
=================
idHomingProjectile::~idHomingProjectile
=================
*/
idHomingProjectile::~idHomingProjectile()
{
2012-11-26 18:58:24 +00:00
}
/*
================
idHomingProjectile::Spawn
================
*/
void idHomingProjectile::Spawn()
{
2012-11-26 18:58:24 +00:00
}
/*
================
idHomingProjectile::Save
================
*/
void idHomingProjectile::Save( idSaveGame* savefile ) const
{
2012-11-26 18:58:24 +00:00
enemy.Save( savefile );
savefile->WriteFloat( speed );
savefile->WriteAngles( rndScale );
savefile->WriteAngles( rndAng );
savefile->WriteFloat( turn_max );
savefile->WriteFloat( clamp_dist );
savefile->WriteAngles( angles );
savefile->WriteBool( burstMode );
savefile->WriteBool( unGuided );
savefile->WriteFloat( burstDist );
savefile->WriteFloat( burstVelocity );
savefile->WriteVec3( seekPos );
}
/*
================
idHomingProjectile::Restore
================
*/
void idHomingProjectile::Restore( idRestoreGame* savefile )
{
2012-11-26 18:58:24 +00:00
enemy.Restore( savefile );
savefile->ReadFloat( speed );
savefile->ReadAngles( rndScale );
savefile->ReadAngles( rndAng );
savefile->ReadFloat( turn_max );
savefile->ReadFloat( clamp_dist );
savefile->ReadAngles( angles );
savefile->ReadBool( burstMode );
savefile->ReadBool( unGuided );
savefile->ReadFloat( burstDist );
savefile->ReadFloat( burstVelocity );
savefile->ReadVec3( seekPos );
}
/*
================
idHomingProjectile::Think
================
*/
void idHomingProjectile::Think()
{
if( seekPos == vec3_zero )
{
2012-11-26 18:58:24 +00:00
// ai def uses a single def_projectile .. guardian has two projectile types so when seekPos is zero, just run regular projectile
idProjectile::Think();
return;
}
2012-11-26 18:58:24 +00:00
idVec3 dir;
idVec3 velocity;
idVec3 nose;
idVec3 tmp;
idMat3 axis;
idAngles dirAng;
idAngles diff;
float dist;
float frac;
int i;
2012-11-26 18:58:24 +00:00
nose = physicsObj.GetOrigin() + 10.0f * physicsObj.GetAxis()[0];
2012-11-26 18:58:24 +00:00
dir = seekPos - nose;
dist = dir.Normalize();
dirAng = dir.ToAngles();
2012-11-26 18:58:24 +00:00
// make it more accurate as it gets closer
frac = ( dist * 2.0f ) / clamp_dist;
if( frac > 1.0f )
{
2012-11-26 18:58:24 +00:00
frac = 1.0f;
}
2012-11-26 18:58:24 +00:00
diff = dirAng - angles * frac;
2012-11-26 18:58:24 +00:00
// clamp the to the max turn rate
diff.Normalize180();
for( i = 0; i < 3; i++ )
{
if( diff[ i ] > turn_max )
{
2012-11-26 18:58:24 +00:00
diff[ i ] = turn_max;
}
else if( diff[ i ] < -turn_max )
{
2012-11-26 18:58:24 +00:00
diff[ i ] = -turn_max;
}
}
angles += diff;
2012-11-26 18:58:24 +00:00
// make the visual model always points the dir we're traveling
dir = angles.ToForward();
velocity = dir * speed;
if( burstMode && dist < burstDist )
{
2012-11-26 18:58:24 +00:00
unGuided = true;
velocity *= burstVelocity;
}
2012-11-26 18:58:24 +00:00
physicsObj.SetLinearVelocity( velocity );
2012-11-26 18:58:24 +00:00
// align z-axis of model with the direction
axis = dir.ToMat3();
tmp = axis[2];
axis[2] = axis[0];
axis[0] = -tmp;
2012-11-26 18:58:24 +00:00
GetPhysics()->SetAxis( axis );
2012-11-26 18:58:24 +00:00
idProjectile::Think();
}
/*
=================
idHomingProjectile::Launch
=================
*/
void idHomingProjectile::Launch( const idVec3& start, const idVec3& dir, const idVec3& pushVelocity, const float timeSinceFire, const float launchPower, float dmgPower )
{
2012-11-26 18:58:24 +00:00
idProjectile::Launch( start, dir, pushVelocity, timeSinceFire, launchPower, dmgPower );
if( owner.GetEntity() )
{
if( owner.GetEntity()->IsType( idAI::Type ) )
{
enemy = static_cast<idAI*>( owner.GetEntity() )->GetEnemy();
}
else if( owner.GetEntity()->IsType( idPlayer::Type ) )
{
2012-11-26 18:58:24 +00:00
trace_t tr;
idPlayer* player = static_cast<idPlayer*>( owner.GetEntity() );
2012-11-26 18:58:24 +00:00
idVec3 start = player->GetEyePosition();
idVec3 end = start + player->viewAxis[0] * 1000.0f;
gameLocal.clip.TracePoint( tr, start, end, MASK_SHOT_RENDERMODEL | CONTENTS_BODY, owner.GetEntity() );
if( tr.fraction < 1.0f )
{
2012-11-26 18:58:24 +00:00
enemy = gameLocal.GetTraceEntity( tr );
}
2012-11-26 18:58:24 +00:00
// ignore actors on the player's team
if( enemy.GetEntity() == NULL || !enemy.GetEntity()->IsType( idActor::Type ) || ( static_cast<idActor*>( enemy.GetEntity() )->team == player->team ) )
{
2012-11-26 18:58:24 +00:00
enemy = player->EnemyWithMostHealth();
}
}
}
const idVec3& vel = physicsObj.GetLinearVelocity();
2012-11-26 18:58:24 +00:00
angles = vel.ToAngles();
speed = vel.Length();
rndScale = spawnArgs.GetAngles( "random", "15 15 0" );
turn_max = spawnArgs.GetFloat( "turn_max", "180" ) / com_engineHz_latched;
clamp_dist = spawnArgs.GetFloat( "clamp_dist", "256" );
burstMode = spawnArgs.GetBool( "burstMode" );
unGuided = false;
burstDist = spawnArgs.GetFloat( "burstDist", "64" );
burstVelocity = spawnArgs.GetFloat( "burstVelocity", "1.25" );
UpdateVisuals();
}
void idHomingProjectile::SetEnemy( idEntity* ent )
{
2012-11-26 18:58:24 +00:00
enemy = ent;
}
void idHomingProjectile::SetSeekPos( idVec3 pos )
{
2012-11-26 18:58:24 +00:00
seekPos = pos;
}
void idHomingProjectile::Event_SetEnemy( idEntity* ent )
{
SetEnemy( ent );
2012-11-26 18:58:24 +00:00
}