halflife-photomode/dlls/explode.cpp

275 lines
6.4 KiB
C++
Raw Normal View History

/***
2013-08-30 13:34:05 -07:00
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
/*
===== explode.cpp ========================================================
Explosion-related code
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "decals.h"
#include "explode.h"
#include "weapons.h"
2013-08-30 13:34:05 -07:00
// Spark Shower
class CShower : public CBaseEntity
{
void Spawn() override;
void Think() override;
void Touch(CBaseEntity* pOther) override;
int ObjectCaps() override { return FCAP_DONT_SAVE; }
2013-08-30 13:34:05 -07:00
};
LINK_ENTITY_TO_CLASS(spark_shower, CShower);
2013-08-30 13:34:05 -07:00
void CShower::Spawn()
2013-08-30 13:34:05 -07:00
{
pev->velocity = RANDOM_FLOAT(200, 300) * pev->angles;
pev->velocity.x += RANDOM_FLOAT(-100.f, 100.f);
pev->velocity.y += RANDOM_FLOAT(-100.f, 100.f);
if (pev->velocity.z >= 0)
2013-08-30 13:34:05 -07:00
pev->velocity.z += 200;
else
pev->velocity.z -= 200;
pev->movetype = MOVETYPE_BOUNCE;
pev->gravity = 0.5;
pev->nextthink = gpGlobals->time + 0.1;
pev->solid = SOLID_NOT;
SET_MODEL(edict(), "models/grenade.mdl"); // Need a model, just use the grenade, we don't draw it anyway
UTIL_SetSize(pev, g_vecZero, g_vecZero);
2013-08-30 13:34:05 -07:00
pev->effects |= EF_NODRAW;
pev->speed = RANDOM_FLOAT(0.5, 1.5);
2013-08-30 13:34:05 -07:00
pev->angles = g_vecZero;
}
void CShower::Think()
2013-08-30 13:34:05 -07:00
{
UTIL_Sparks(pev->origin);
2013-08-30 13:34:05 -07:00
pev->speed -= 0.1;
if (pev->speed > 0)
2013-08-30 13:34:05 -07:00
pev->nextthink = gpGlobals->time + 0.1;
else
UTIL_Remove(this);
2013-08-30 13:34:05 -07:00
pev->flags &= ~FL_ONGROUND;
}
void CShower::Touch(CBaseEntity* pOther)
2013-08-30 13:34:05 -07:00
{
if ((pev->flags & FL_ONGROUND) != 0)
2013-08-30 13:34:05 -07:00
pev->velocity = pev->velocity * 0.1;
else
pev->velocity = pev->velocity * 0.6;
if ((pev->velocity.x * pev->velocity.x + pev->velocity.y * pev->velocity.y) < 10.0)
2013-08-30 13:34:05 -07:00
pev->speed = 0;
}
class CEnvExplosion : public CBaseMonster
{
public:
void Spawn() override;
void EXPORT Smoke();
bool KeyValue(KeyValueData* pkvd) override;
void Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value) override;
2013-08-30 13:34:05 -07:00
bool Save(CSave& save) override;
bool Restore(CRestore& restore) override;
static TYPEDESCRIPTION m_SaveData[];
2013-08-30 13:34:05 -07:00
int m_iMagnitude; // how large is the fireball? how much damage?
int m_spriteScale; // what's the exact fireball sprite scale?
2013-08-30 13:34:05 -07:00
};
TYPEDESCRIPTION CEnvExplosion::m_SaveData[] =
{
DEFINE_FIELD(CEnvExplosion, m_iMagnitude, FIELD_INTEGER),
DEFINE_FIELD(CEnvExplosion, m_spriteScale, FIELD_INTEGER),
2013-08-30 13:34:05 -07:00
};
IMPLEMENT_SAVERESTORE(CEnvExplosion, CBaseMonster);
LINK_ENTITY_TO_CLASS(env_explosion, CEnvExplosion);
2013-08-30 13:34:05 -07:00
bool CEnvExplosion::KeyValue(KeyValueData* pkvd)
2013-08-30 13:34:05 -07:00
{
if (FStrEq(pkvd->szKeyName, "iMagnitude"))
{
m_iMagnitude = atoi(pkvd->szValue);
return true;
2013-08-30 13:34:05 -07:00
}
return CBaseEntity::KeyValue(pkvd);
2013-08-30 13:34:05 -07:00
}
void CEnvExplosion::Spawn()
{
2013-08-30 13:34:05 -07:00
pev->solid = SOLID_NOT;
pev->effects = EF_NODRAW;
pev->movetype = MOVETYPE_NONE;
/*
if ( m_iMagnitude > 250 )
{
m_iMagnitude = 250;
}
*/
float flSpriteScale;
flSpriteScale = (m_iMagnitude - 50) * 0.6;
2013-08-30 13:34:05 -07:00
/*
if ( flSpriteScale > 50 )
{
flSpriteScale = 50;
}
*/
if (flSpriteScale < 10)
2013-08-30 13:34:05 -07:00
{
flSpriteScale = 10;
}
m_spriteScale = (int)flSpriteScale;
}
void CEnvExplosion::Use(CBaseEntity* pActivator, CBaseEntity* pCaller, USE_TYPE useType, float value)
{
2013-08-30 13:34:05 -07:00
TraceResult tr;
pev->model = iStringNull; //invisible
pev->solid = SOLID_NOT; // intangible
Vector vecSpot; // trace starts here!
2013-08-30 13:34:05 -07:00
vecSpot = pev->origin + Vector(0, 0, 8);
UTIL_TraceLine(vecSpot, vecSpot + Vector(0, 0, -40), ignore_monsters, ENT(pev), &tr);
2013-08-30 13:34:05 -07:00
// Pull out of the wall a bit
if (tr.flFraction != 1.0)
2013-08-30 13:34:05 -07:00
{
pev->origin = tr.vecEndPos + (tr.vecPlaneNormal * (m_iMagnitude - 24) * 0.6);
}
else
{
pev->origin = pev->origin;
}
// draw decal
if ((pev->spawnflags & SF_ENVEXPLOSION_NODECAL) == 0)
2013-08-30 13:34:05 -07:00
{
if (RANDOM_FLOAT(0, 1) < 0.5)
2013-08-30 13:34:05 -07:00
{
UTIL_DecalTrace(&tr, DECAL_SCORCH1);
2013-08-30 13:34:05 -07:00
}
else
{
UTIL_DecalTrace(&tr, DECAL_SCORCH2);
2013-08-30 13:34:05 -07:00
}
}
// draw fireball
if ((pev->spawnflags & SF_ENVEXPLOSION_NOFIREBALL) == 0)
2013-08-30 13:34:05 -07:00
{
MESSAGE_BEGIN(MSG_PAS, SVC_TEMPENTITY, pev->origin);
WRITE_BYTE(TE_EXPLOSION);
WRITE_COORD(pev->origin.x);
WRITE_COORD(pev->origin.y);
WRITE_COORD(pev->origin.z);
WRITE_SHORT(g_sModelIndexFireball);
WRITE_BYTE((byte)m_spriteScale); // scale * 10
WRITE_BYTE(15); // framerate
WRITE_BYTE(TE_EXPLFLAG_NONE);
2013-08-30 13:34:05 -07:00
MESSAGE_END();
}
else
{
MESSAGE_BEGIN(MSG_PAS, SVC_TEMPENTITY, pev->origin);
WRITE_BYTE(TE_EXPLOSION);
WRITE_COORD(pev->origin.x);
WRITE_COORD(pev->origin.y);
WRITE_COORD(pev->origin.z);
WRITE_SHORT(g_sModelIndexFireball);
WRITE_BYTE(0); // no sprite
WRITE_BYTE(15); // framerate
WRITE_BYTE(TE_EXPLFLAG_NONE);
2013-08-30 13:34:05 -07:00
MESSAGE_END();
}
// do damage
if ((pev->spawnflags & SF_ENVEXPLOSION_NODAMAGE) == 0)
2013-08-30 13:34:05 -07:00
{
RadiusDamage(pev, pev, m_iMagnitude, CLASS_NONE, DMG_BLAST);
2013-08-30 13:34:05 -07:00
}
SetThink(&CEnvExplosion::Smoke);
2013-08-30 13:34:05 -07:00
pev->nextthink = gpGlobals->time + 0.3;
// draw sparks
if ((pev->spawnflags & SF_ENVEXPLOSION_NOSPARKS) == 0)
2013-08-30 13:34:05 -07:00
{
int sparkCount = RANDOM_LONG(0, 3);
2013-08-30 13:34:05 -07:00
for (int i = 0; i < sparkCount; i++)
2013-08-30 13:34:05 -07:00
{
Create("spark_shower", pev->origin, tr.vecPlaneNormal, NULL);
2013-08-30 13:34:05 -07:00
}
}
}
void CEnvExplosion::Smoke()
2013-08-30 13:34:05 -07:00
{
if ((pev->spawnflags & SF_ENVEXPLOSION_NOSMOKE) == 0)
2013-08-30 13:34:05 -07:00
{
MESSAGE_BEGIN(MSG_PAS, SVC_TEMPENTITY, pev->origin);
WRITE_BYTE(TE_SMOKE);
WRITE_COORD(pev->origin.x);
WRITE_COORD(pev->origin.y);
WRITE_COORD(pev->origin.z);
WRITE_SHORT(g_sModelIndexSmoke);
WRITE_BYTE((byte)m_spriteScale); // scale * 10
WRITE_BYTE(12); // framerate
2013-08-30 13:34:05 -07:00
MESSAGE_END();
}
if ((pev->spawnflags & SF_ENVEXPLOSION_REPEATABLE) == 0)
2013-08-30 13:34:05 -07:00
{
UTIL_Remove(this);
2013-08-30 13:34:05 -07:00
}
}
// HACKHACK -- create one of these and fake a keyvalue to get the right explosion setup
void ExplosionCreate(const Vector& center, const Vector& angles, edict_t* pOwner, int magnitude, bool doDamage)
2013-08-30 13:34:05 -07:00
{
KeyValueData kvd;
char buf[128];
2013-08-30 13:34:05 -07:00
CBaseEntity* pExplosion = CBaseEntity::Create("env_explosion", center, angles, pOwner);
sprintf(buf, "%3d", magnitude);
2013-08-30 13:34:05 -07:00
kvd.szKeyName = "iMagnitude";
kvd.szValue = buf;
pExplosion->KeyValue(&kvd);
if (!doDamage)
2013-08-30 13:34:05 -07:00
pExplosion->pev->spawnflags |= SF_ENVEXPLOSION_NODAMAGE;
pExplosion->Spawn();
pExplosion->Use(NULL, NULL, USE_TOGGLE, 0);
2013-08-30 13:34:05 -07:00
}