ReGameDLL_CS/regamedll/dlls/util.cpp

2393 lines
57 KiB
C++
Raw Normal View History

#include "precompiled.h"
unsigned int glSeed;
CUtlVector<hash_item_t> stringsHashTable;
2015-06-30 12:46:07 +03:00
/*
* Globals initialization
*/
#ifndef HOOK_GAMEDLL
unsigned int seed_table[256] =
{
28985U, 27138U, 26457U, 9451U, 17764U, 10909U, 28790U, 8716U, 6361U, 4853U, 17798U, 21977U, 19643U, 20662U, 10834U, 20103,
27067U, 28634U, 18623U, 25849U, 8576U, 26234U, 23887U, 18228U, 32587U, 4836U, 3306U, 1811U, 3035U, 24559U, 18399U, 315,
26766U, 907U, 24102U, 12370U, 9674U, 2972U, 10472U, 16492U, 22683U, 11529U, 27968U, 30406U, 13213U, 2319U, 23620U, 16823,
10013U, 23772U, 21567U, 1251U, 19579U, 20313U, 18241U, 30130U, 8402U, 20807U, 27354U, 7169U, 21211U, 17293U, 5410U, 19223,
10255U, 22480U, 27388U, 9946U, 15628U, 24389U, 17308U, 2370U, 9530U, 31683U, 25927U, 23567U, 11694U, 26397U, 32602U, 15031,
18255U, 17582U, 1422U, 28835U, 23607U, 12597U, 20602U, 10138U, 5212U, 1252U, 10074U, 23166U, 19823U, 31667U, 5902U, 24630,
18948U, 14330U, 14950U, 8939U, 23540U, 21311U, 22428U, 22391U, 3583U, 29004U, 30498U, 18714U, 4278U, 2437U, 22430U, 3439,
28313U, 23161U, 25396U, 13471U, 19324U, 15287U, 2563U, 18901U, 13103U, 16867U, 9714U, 14322U, 15197U, 26889U, 19372U, 26241,
31925U, 14640U, 11497U, 8941U, 10056U, 6451U, 28656U, 10737U, 13874U, 17356U, 8281U, 25937U, 1661U, 4850U, 7448U, 12744,
21826U, 5477U, 10167U, 16705U, 26897U, 8839U, 30947U, 27978U, 27283U, 24685U, 32298U, 3525U, 12398U, 28726U, 9475U, 10208,
617U, 13467U, 22287U, 2376U, 6097U, 26312U, 2974U, 9114U, 21787U, 28010U, 4725U, 15387U, 3274U, 10762U, 31695U, 17320,
18324U, 12441U, 16801U, 27376U, 22464U, 7500U, 5666U, 18144U, 15314U, 31914U, 31627U, 6495U, 5226U, 31203U, 2331U, 4668,
12650U, 18275U, 351U, 7268U, 31319U, 30119U, 7600U, 2905U, 13826U, 11343U, 13053U, 15583U, 30055U, 31093U, 5067U, 761,
9685U, 11070U, 21369U, 27155U, 3663U, 26542U, 20169U, 12161U, 15411U, 30401U, 7580U, 31784U, 8985U, 29367U, 20989U, 14203,
29694U, 21167U, 10337U, 1706U, 28578U, 887U, 3373U, 19477U, 14382U, 675U, 7033U, 15111U, 26138U, 12252U, 30996U, 21409,
25678U, 18555U, 13256U, 23316U, 22407U, 16727U, 991U, 9236U, 5373U, 29402U, 6117U, 15241U, 27715U, 19291U, 19888U, 19847U
};
TYPEDESCRIPTION gEntvarsDescription[86] =
{
DEFINE_ENTITY_FIELD(classname, FIELD_STRING),
DEFINE_ENTITY_GLOBAL_FIELD(globalname, FIELD_STRING),
DEFINE_ENTITY_FIELD(origin, FIELD_POSITION_VECTOR),
DEFINE_ENTITY_FIELD(oldorigin, FIELD_POSITION_VECTOR),
DEFINE_ENTITY_FIELD(velocity, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(basevelocity, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(movedir, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(angles, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(avelocity, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(punchangle, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(v_angle, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(fixangle, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(idealpitch, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(pitch_speed, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(ideal_yaw, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(yaw_speed, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(modelindex, FIELD_INTEGER),
DEFINE_ENTITY_GLOBAL_FIELD(model, FIELD_MODELNAME),
DEFINE_ENTITY_FIELD(viewmodel, FIELD_MODELNAME),
DEFINE_ENTITY_FIELD(weaponmodel, FIELD_MODELNAME),
DEFINE_ENTITY_FIELD(absmin, FIELD_POSITION_VECTOR),
DEFINE_ENTITY_FIELD(absmax, FIELD_POSITION_VECTOR),
DEFINE_ENTITY_GLOBAL_FIELD(mins, FIELD_VECTOR),
DEFINE_ENTITY_GLOBAL_FIELD(maxs, FIELD_VECTOR),
DEFINE_ENTITY_GLOBAL_FIELD(size, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(ltime, FIELD_TIME),
DEFINE_ENTITY_FIELD(nextthink, FIELD_TIME),
DEFINE_ENTITY_FIELD(solid, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(movetype, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(skin, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(body, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(effects, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(gravity, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(friction, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(light_level, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(frame, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(scale, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(sequence, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(animtime, FIELD_TIME),
DEFINE_ENTITY_FIELD(framerate, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(controller, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(blending, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(rendermode, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(renderamt, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(rendercolor, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(renderfx, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(health, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(frags, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(weapons, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(takedamage, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(deadflag, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(view_ofs, FIELD_VECTOR),
DEFINE_ENTITY_FIELD(button, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(impulse, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(chain, FIELD_EDICT),
DEFINE_ENTITY_FIELD(dmg_inflictor, FIELD_EDICT),
DEFINE_ENTITY_FIELD(enemy, FIELD_EDICT),
DEFINE_ENTITY_FIELD(aiment, FIELD_EDICT),
DEFINE_ENTITY_FIELD(owner, FIELD_EDICT),
DEFINE_ENTITY_FIELD(groundentity, FIELD_EDICT),
DEFINE_ENTITY_FIELD(spawnflags, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(flags, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(colormap, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(team, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(max_health, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(teleport_time, FIELD_TIME),
DEFINE_ENTITY_FIELD(armortype, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(armorvalue, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(waterlevel, FIELD_INTEGER),
DEFINE_ENTITY_FIELD(watertype, FIELD_INTEGER),
DEFINE_ENTITY_GLOBAL_FIELD(target, FIELD_STRING),
DEFINE_ENTITY_GLOBAL_FIELD(targetname, FIELD_STRING),
DEFINE_ENTITY_FIELD(netname, FIELD_STRING),
DEFINE_ENTITY_FIELD(message, FIELD_STRING),
DEFINE_ENTITY_FIELD(dmg_take, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(dmg_save, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(dmg, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(dmgtime, FIELD_TIME),
DEFINE_ENTITY_FIELD(noise, FIELD_SOUNDNAME),
DEFINE_ENTITY_FIELD(noise1, FIELD_SOUNDNAME),
DEFINE_ENTITY_FIELD(noise2, FIELD_SOUNDNAME),
DEFINE_ENTITY_FIELD(noise3, FIELD_SOUNDNAME),
DEFINE_ENTITY_FIELD(speed, FIELD_FLOAT),
DEFINE_ENTITY_FIELD(air_finished, FIELD_TIME),
DEFINE_ENTITY_FIELD(pain_finished, FIELD_TIME),
DEFINE_ENTITY_FIELD(radsuit_finished, FIELD_TIME),
};
int g_groupmask = 0;
int g_groupop = 0;
const int gSizes[18] = { 4, 4, 4, 4, 4, 4, 4, 12, 12, 4, 4, 4, 4, 2, 1, 4, 4, 4 };
2016-01-25 20:02:57 +03:00
#endif // HOOK_GAMEDLL
float UTIL_WeaponTimeBase()
2015-12-09 01:39:54 +03:00
{
#ifdef CLIENT_WEAPONS
return 0.0;
#else
return gpGlobals->time;
#endif
2015-09-28 03:13:39 +03:00
}
unsigned int U_Random()
{
glSeed *= 69069;
glSeed += seed_table[glSeed & 0xFF] + 1;
return (glSeed & 0xFFFFFFF);
}
void U_Srand(unsigned int seed)
{
glSeed = seed_table[seed & 0xFF];
}
int UTIL_SharedRandomLong(unsigned int seed, int low, int high)
{
unsigned int range = high - low + 1;
U_Srand((unsigned int)(high + low + seed));
if (range != 1)
{
2015-06-30 12:46:07 +03:00
int rnum = U_Random();
int offset = rnum % range;
return (low + offset);
}
2015-08-20 13:35:01 +03:00
return low;
}
float UTIL_SharedRandomFloat(unsigned int seed, float low, float high)
{
unsigned int range = high - low;
U_Srand((unsigned int)seed + *(unsigned int *)&low + *(unsigned int *)&high);
U_Random();
U_Random();
if (range)
{
2015-06-30 12:46:07 +03:00
int tensixrand = U_Random() & 0xFFFFu;
float offset = float(tensixrand) / 0x10000u;
return (low + offset * range);
}
2015-08-20 13:35:01 +03:00
return low;
}
NOXREF void UTIL_ParametricRocket(entvars_t *pev, Vector p_vecOrigin, Vector vecAngles, edict_t *owner)
{
TraceResult tr;
Vector vecTravel;
float travelTime;
pev->startpos = p_vecOrigin;
UTIL_MakeVectors(vecAngles);
UTIL_TraceLine(pev->startpos, gpGlobals->v_forward * 8192.0f + pev->startpos, ignore_monsters, owner, &tr);
pev->endpos = tr.vecEndPos;
vecTravel = pev->endpos - pev->startpos;
if (pev->velocity.Length() > 0.0f)
travelTime = vecTravel.Length() / pev->velocity.Length();
else
travelTime = 0.0f;
2015-06-30 12:46:07 +03:00
pev->starttime = gpGlobals->time;
pev->impacttime = travelTime + gpGlobals->time;
}
void UTIL_SetGroupTrace(int groupmask, int op)
{
g_groupmask = groupmask;
g_groupop = op;
ENGINE_SETGROUPMASK(groupmask, op);
}
void UTIL_UnsetGroupTrace()
{
g_groupmask = 0;
g_groupop = 0;
ENGINE_SETGROUPMASK(0,0);
}
NOXREF UTIL_GroupTrace::UTIL_GroupTrace(int groupmask, int op)
{
m_oldgroupmask = g_groupmask;
m_oldgroupop = g_groupop;
g_groupmask = groupmask;
g_groupop = op;
ENGINE_SETGROUPMASK(groupmask, op);
}
NOXREF UTIL_GroupTrace::~UTIL_GroupTrace()
{
g_groupmask = m_oldgroupmask;
g_groupop = m_oldgroupop;
ENGINE_SETGROUPMASK(g_groupmask, g_groupop);
}
NOXREF BOOL UTIL_GetNextBestWeapon(CBasePlayer *pPlayer, CBasePlayerItem *pCurrentWeapon)
{
return g_pGameRules->GetNextBestWeapon(pPlayer,pCurrentWeapon);
}
NOXREF float UTIL_AngleMod(float a)
{
2015-06-30 12:46:07 +03:00
if (a < 0.0f)
a = a + 360.0f * (int(a / 360.0f) + 1);
2015-06-30 12:46:07 +03:00
else if (a >= 360.0f)
a = a - 360.0f * int(a / 360.0f);
return a;
}
NOXREF float UTIL_AngleDiff(float destAngle, float srcAngle)
{
2015-06-30 12:46:07 +03:00
float delta = destAngle - srcAngle;
if (destAngle > srcAngle)
{
if (delta >= 180.0f)
delta -= 360.0f;
}
else
{
if (delta <= -180.0f)
delta += 360.0f;
}
2016-01-25 20:02:57 +03:00
return delta;
}
Vector UTIL_VecToAngles(const Vector &vec)
{
float rgflVecOut[3];
VEC_TO_ANGLES(vec, rgflVecOut);
return Vector(rgflVecOut);
}
NOXREF void UTIL_MoveToOrigin(edict_t *pent, const Vector &vecGoal, float flDist, int iMoveType)
{
float rgfl[3];
vecGoal.CopyToArray(rgfl);
MOVE_TO_ORIGIN(pent, rgfl, flDist, iMoveType);
}
int UTIL_EntitiesInBox(CBaseEntity **pList, int listMax, const Vector &mins, const Vector &maxs, int flagMask)
{
edict_t *pEdict = INDEXENT(1);
CBaseEntity *pEntity;
int count = 0;
2015-09-16 23:19:21 +03:00
if (!pEdict)
return 0;
2016-01-25 20:02:57 +03:00
for (int i = 1; i < gpGlobals->maxEntities; ++i, ++pEdict)
{
if (pEdict->free)
continue;
if (flagMask && !(pEdict->v.flags & flagMask))
continue;
if (mins.x > pEdict->v.absmax.x
|| mins.y > pEdict->v.absmax.y
|| mins.z > pEdict->v.absmax.z
|| maxs.x < pEdict->v.absmin.x
|| maxs.y < pEdict->v.absmin.y
|| maxs.z < pEdict->v.absmin.z)
continue;
pEntity = CBaseEntity::Instance(pEdict);
if (!pEntity)
continue;
pList[count++] = pEntity;
if (count >= listMax)
break;
}
2015-08-20 13:35:01 +03:00
return count;
}
NOXREF int UTIL_MonstersInSphere(CBaseEntity ** pList, int listMax, const Vector &center, float radius)
{
edict_t *pEdict;
CBaseEntity *pEntity;
int count = 0;
float distance;
float delta;
float radiusSquared = radius * radius;
pEdict = INDEXENT(1);
if (!pEdict)
return count;
2016-01-25 20:02:57 +03:00
for (int i = 1; i < gpGlobals->maxEntities; ++i, ++pEdict)
{
if (pEdict->free)
continue;
2016-01-25 20:02:57 +03:00
if (!(pEdict->v.flags & (FL_CLIENT | FL_MONSTER)))
continue;
delta = center.x - pEdict->v.origin.x;
delta *= delta;
if (delta > radiusSquared)
continue;
distance = delta;
delta = center.y - pEdict->v.origin.y;
delta *= delta;
distance += delta;
if (distance > radiusSquared)
continue;
delta = center.z - (pEdict->v.absmin.z + pEdict->v.absmax.z) * 0.5;
delta *= delta;
distance += delta;
if (distance > radiusSquared)
continue;
pEntity = CBaseEntity::Instance(pEdict);
if (!pEntity)
continue;
pList[count] = pEntity;
count++;
if (count >= listMax)
return count;
}
2015-08-20 13:35:01 +03:00
return count;
}
CBaseEntity *UTIL_FindEntityInSphere(CBaseEntity *pStartEntity, const Vector &vecCenter, float flRadius)
{
edict_t *pentEntity;
2016-01-25 20:02:57 +03:00
if (pStartEntity != NULL)
pentEntity = pStartEntity->edict();
else
pentEntity = NULL;
pentEntity = FIND_ENTITY_IN_SPHERE(pentEntity, vecCenter, flRadius);
if (!FNullEnt(pentEntity))
2015-08-20 13:35:01 +03:00
{
return CBaseEntity::Instance(pentEntity);
2015-08-20 13:35:01 +03:00
}
return NULL;
}
CBaseEntity *UTIL_FindEntityByString_Old(CBaseEntity *pStartEntity, const char *szKeyword, const char *szValue)
{
edict_t *pentEntity;
2016-01-25 20:02:57 +03:00
if (pStartEntity != NULL)
pentEntity = pStartEntity->edict();
else
pentEntity = NULL;
pentEntity = FIND_ENTITY_BY_STRING(pentEntity, szKeyword, szValue);
if (!FNullEnt(pentEntity))
2015-08-20 13:35:01 +03:00
{
return CBaseEntity::Instance(pentEntity);
2015-08-20 13:35:01 +03:00
}
return NULL;
}
CBaseEntity *UTIL_FindEntityByString(CBaseEntity *pStartEntity, const char *szKeyword, const char *szValue)
{
edict_t *pentEntity;
int startEntityIndex;
if (pStartEntity)
pentEntity = pStartEntity->edict();
else
pentEntity = NULL;
startEntityIndex = ENTINDEX(pentEntity);
//it best each entity list
2015-06-30 12:46:07 +03:00
if (*szKeyword == 'c')
{
int hash;
hash_item_t *item;
int count;
hash = CaseInsensitiveHash(szValue, stringsHashTable.Count());
count = stringsHashTable.Count();
2015-08-20 13:35:01 +03:00
item = &stringsHashTable[ hash ];
2015-06-30 12:46:07 +03:00
2015-08-20 13:35:01 +03:00
if (!item->pev)
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
item->lastHash = NULL;
return NULL;
}
while (item->pev != NULL)
{
if (!Q_strcmp(STRING(item->pev->classname), szValue))
2015-06-30 12:46:07 +03:00
break;
hash = (hash + 1) % count;
2015-08-20 13:35:01 +03:00
item = &stringsHashTable[ hash ];
2015-06-30 12:46:07 +03:00
}
2015-08-20 13:35:01 +03:00
2015-06-30 12:46:07 +03:00
if (!item->pev)
{
item->lastHash = NULL;
return NULL;
}
2015-08-20 13:35:01 +03:00
if (pStartEntity != NULL)
2015-06-30 12:46:07 +03:00
{
if (item->lastHash && item->lastHash->pevIndex <= startEntityIndex)
item = item->lastHash;
2015-08-20 13:35:01 +03:00
if (item->pevIndex <= startEntityIndex)
2015-06-30 12:46:07 +03:00
{
2015-08-20 13:35:01 +03:00
while (item->pevIndex <= startEntityIndex)
{
if (!item->next)
break;
2015-06-30 12:46:07 +03:00
2015-08-20 13:35:01 +03:00
item = item->next;
}
2015-06-30 12:46:07 +03:00
2015-08-20 13:35:01 +03:00
if (item->pevIndex == startEntityIndex)
{
stringsHashTable[ hash ].lastHash = NULL;
return NULL;
}
2015-06-30 12:46:07 +03:00
}
}
2015-08-20 13:35:01 +03:00
stringsHashTable[ hash ].lastHash = item;
2015-06-30 12:46:07 +03:00
pentEntity = ENT(item->pev);
}
else
pentEntity = FIND_ENTITY_BY_STRING(pentEntity, szKeyword, szValue);
2015-06-30 12:46:07 +03:00
if (!FNullEnt(pentEntity))
2015-08-20 13:35:01 +03:00
{
2015-06-30 12:46:07 +03:00
return CBaseEntity::Instance(pentEntity);
2015-08-20 13:35:01 +03:00
}
return NULL;
}
CBaseEntity *UTIL_FindEntityByClassname(CBaseEntity *pStartEntity, const char *szName)
{
return UTIL_FindEntityByString(pStartEntity, "classname", szName);
}
CBaseEntity *UTIL_FindEntityByTargetname(CBaseEntity *pStartEntity, const char *szName)
{
return UTIL_FindEntityByString(pStartEntity, "targetname", szName);
}
CBaseEntity *UTIL_FindEntityGeneric(const char *szWhatever, const Vector &vecSrc, float flRadius)
{
CBaseEntity *pSearch = NULL;
2015-06-30 12:46:07 +03:00
CBaseEntity *pEntity = UTIL_FindEntityByTargetname(NULL, szWhatever);
2016-01-25 20:02:57 +03:00
if (pEntity != NULL)
2015-06-30 12:46:07 +03:00
return pEntity;
float flMaxDist2 = flRadius * flRadius;
while ((pSearch = UTIL_FindEntityByClassname(pSearch, szWhatever)) != NULL)
{
float flDist2 = (pSearch->pev->origin - vecSrc).Length();
flDist2 = flDist2 * flDist2;
if (flMaxDist2 > flDist2)
{
pEntity = pSearch;
flMaxDist2 = flDist2;
}
}
2016-01-25 20:02:57 +03:00
return pEntity;
}
2015-12-09 01:39:54 +03:00
CBaseEntity *EXT_FUNC UTIL_PlayerByIndex(int playerIndex)
{
2015-06-30 12:46:07 +03:00
CBaseEntity *pPlayer = NULL;
if (playerIndex > 0 && playerIndex <= gpGlobals->maxClients)
{
edict_t *pPlayerEdict = INDEXENT(playerIndex);
if (pPlayerEdict != NULL && !pPlayerEdict->free)
2015-06-30 12:46:07 +03:00
pPlayer = CBaseEntity::Instance(pPlayerEdict);
}
return pPlayer;
}
void UTIL_MakeVectors(const Vector &vecAngles)
{
MAKE_VECTORS(vecAngles);
}
void UTIL_MakeAimVectors(const Vector &vecAngles)
{
2015-06-30 12:46:07 +03:00
float rgflVec[3];
vecAngles.CopyToArray(rgflVec);
rgflVec[0] = -rgflVec[0];
MAKE_VECTORS(rgflVec);
}
void UTIL_MakeInvVectors(const Vector &vec, globalvars_t *pgv)
{
MAKE_VECTORS(vec);
pgv->v_right = pgv->v_right * -1;
SWAP(pgv->v_forward.y, pgv->v_right.x);
SWAP(pgv->v_forward.z, pgv->v_up.x);
SWAP(pgv->v_right.z, pgv->v_up.y);
}
void UTIL_EmitAmbientSound(edict_t *entity, const Vector &vecOrigin, const char *samp, float vol, float attenuation, int fFlags, int pitch)
{
float rgfl[3];
vecOrigin.CopyToArray(rgfl);
if (samp && *samp == '!')
{
char name[32];
if (SENTENCEG_Lookup(samp, name) >= 0)
EMIT_AMBIENT_SOUND(entity, rgfl, name, vol, attenuation, fFlags, pitch);
}
else
EMIT_AMBIENT_SOUND(entity, rgfl, samp, vol, attenuation, fFlags, pitch);
}
unsigned short FixedUnsigned16(float value, float scale)
{
int output = value * scale;
if (output < 0)
output = 0;
if (output > USHRT_MAX)
output = USHRT_MAX;
return (unsigned short)output;
}
short FixedSigned16(float value, float scale)
{
int output = value * scale;
if (output > SHRT_MAX)
output = SHRT_MAX;
if (output < SHRT_MIN)
output = SHRT_MIN;
return (short)output;
}
void UTIL_ScreenShake(const Vector &center, float amplitude, float frequency, float duration, float radius)
{
int i;
float localAmplitude;
ScreenShake shake;
shake.duration = FixedUnsigned16(duration, 1<<12);
shake.frequency = FixedUnsigned16(frequency, 1<<8);
2016-01-25 20:02:57 +03:00
for (i = 1; i <= gpGlobals->maxClients; ++i)
{
CBaseEntity *pPlayer = UTIL_PlayerByIndex(i);
if (!pPlayer || !(pPlayer->pev->flags & FL_ONGROUND))
continue;
localAmplitude = 0;
if (radius > 0)
{
Vector delta = center - pPlayer->pev->origin;
float distance = delta.Length();
if (distance < radius)
localAmplitude = amplitude;
}
else
localAmplitude = amplitude;
if (localAmplitude)
{
shake.amplitude = FixedUnsigned16(localAmplitude, 1<<12);
MESSAGE_BEGIN(MSG_ONE, gmsgShake, NULL, pPlayer->edict());
WRITE_SHORT(shake.amplitude);
WRITE_SHORT(shake.duration);
WRITE_SHORT(shake.frequency);
MESSAGE_END();
}
}
}
NOXREF void UTIL_ScreenShakeAll(const Vector &center, float amplitude, float frequency, float duration)
{
UTIL_ScreenShake(center, amplitude, frequency, duration, 0);
}
void UTIL_ScreenFadeBuild(ScreenFade &fade, const Vector &color, float fadeTime, float fadeHold, int alpha, int flags)
{
fade.duration = FixedUnsigned16(fadeTime, 1<<12);
fade.holdTime = FixedUnsigned16(fadeHold, 1<<12);
fade.r = int(color.x);
fade.g = int(color.y);
fade.b = int(color.z);
fade.a = alpha;
fade.fadeFlags = flags;
}
void UTIL_ScreenFadeWrite(const ScreenFade &fade, CBaseEntity *pEntity)
{
if (!pEntity || !pEntity->IsNetClient())
return;
MESSAGE_BEGIN(MSG_ONE, gmsgFade, NULL, pEntity->edict());
WRITE_SHORT(fade.duration);
WRITE_SHORT(fade.holdTime);
WRITE_SHORT(fade.fadeFlags);
WRITE_BYTE(fade.r);
WRITE_BYTE(fade.g);
WRITE_BYTE(fade.b);
WRITE_BYTE(fade.a);
MESSAGE_END();
}
void UTIL_ScreenFadeAll(const Vector &color, float fadeTime, float fadeHold, int alpha, int flags)
{
int i;
ScreenFade fade;
UTIL_ScreenFadeBuild(fade, color, fadeTime, fadeHold, alpha, flags);
2016-01-25 20:02:57 +03:00
for (i = 1; i <= gpGlobals->maxClients; ++i)
{
CBaseEntity *pPlayer = UTIL_PlayerByIndex(i);
UTIL_ScreenFadeWrite(fade, pPlayer);
}
}
void UTIL_ScreenFade(CBaseEntity *pEntity, const Vector &color, float fadeTime, float fadeHold, int alpha, int flags)
{
ScreenFade fade;
UTIL_ScreenFadeBuild(fade, color, fadeTime, fadeHold, alpha, flags);
UTIL_ScreenFadeWrite(fade, pEntity);
}
void UTIL_HudMessage(CBaseEntity *pEntity, const hudtextparms_t &textparms, const char *pMessage)
{
if (!pEntity || !pEntity->IsNetClient())
return;
MESSAGE_BEGIN(MSG_ONE, SVC_TEMPENTITY, NULL, pEntity->edict());
WRITE_BYTE(TE_TEXTMESSAGE);
WRITE_BYTE(textparms.channel & 0xFF);
WRITE_SHORT(FixedSigned16(textparms.x,1<<13));
WRITE_SHORT(FixedSigned16(textparms.y,1<<13));
WRITE_BYTE(textparms.effect);
WRITE_BYTE(textparms.r1);
WRITE_BYTE(textparms.g1);
WRITE_BYTE(textparms.b1);
WRITE_BYTE(textparms.a1);
WRITE_BYTE(textparms.r2);
WRITE_BYTE(textparms.g2);
WRITE_BYTE(textparms.b2);
WRITE_BYTE(textparms.a2);
WRITE_SHORT(FixedUnsigned16(textparms.fadeinTime,1<<8));
WRITE_SHORT(FixedUnsigned16(textparms.fadeoutTime,1<<8));
WRITE_SHORT(FixedUnsigned16(textparms.holdTime,1<<8));
if (textparms.effect == 2)
WRITE_SHORT(FixedUnsigned16(textparms.fxTime,1<<8));
if (!pMessage)
WRITE_STRING(" "); //TODO: oh yeah
else
{
if (Q_strlen(pMessage) >= 512)
{
char tmp[512];
Q_strncpy(tmp, pMessage, 511);
tmp[511] = 0;
WRITE_STRING(tmp);
}
else
WRITE_STRING(pMessage);
}
MESSAGE_END();
}
void UTIL_HudMessageAll(const hudtextparms_t &textparms, const char *pMessage)
{
2016-01-25 20:02:57 +03:00
for (int i = 1; i <= gpGlobals->maxClients; ++i)
{
CBaseEntity *pPlayer = UTIL_PlayerByIndex(i);
2016-01-25 20:02:57 +03:00
if (pPlayer != NULL)
UTIL_HudMessage(pPlayer, textparms, pMessage);
}
}
void UTIL_ClientPrintAll(int msg_dest, const char *msg_name, const char *param1, const char *param2, const char *param3, const char *param4)
{
MESSAGE_BEGIN(MSG_ALL, gmsgTextMsg);
WRITE_BYTE(msg_dest);
WRITE_STRING(msg_name);
if (param1)
WRITE_STRING(param1);
if (param2)
WRITE_STRING(param2);
if (param3)
WRITE_STRING(param3);
if (param4)
WRITE_STRING(param4);
MESSAGE_END();
}
void ClientPrint(entvars_t *client, int msg_dest, const char *msg_name, const char *param1, const char *param2, const char *param3, const char *param4)
{
MESSAGE_BEGIN(MSG_ONE, gmsgTextMsg, NULL, client);
WRITE_BYTE(msg_dest);
WRITE_STRING(msg_name);
if (param1)
WRITE_STRING(param1);
if (param2)
WRITE_STRING(param2);
if (param3)
WRITE_STRING(param3);
if (param4)
WRITE_STRING(param4);
MESSAGE_END();
}
NOXREF void UTIL_SayText(const char *pText, CBaseEntity *pEntity)
{
2015-06-30 12:46:07 +03:00
if (pEntity->IsNetClient())
{
MESSAGE_BEGIN(MSG_ONE, gmsgSayText, NULL, pEntity->edict());
WRITE_BYTE(pEntity->entindex());
WRITE_STRING(pText);
MESSAGE_END();
}
}
void UTIL_SayTextAll(const char *pText, CBaseEntity *pEntity)
{
2015-06-30 12:46:07 +03:00
MESSAGE_BEGIN(MSG_ALL, gmsgSayText);
WRITE_BYTE(pEntity->entindex());
WRITE_STRING(pText);
MESSAGE_END();
}
char *UTIL_dtos1(int d)
{
2015-06-30 12:46:07 +03:00
static char buf[8];
Q_sprintf(buf, "%d", d);
return buf;
}
char *UTIL_dtos2(int d)
{
2015-06-30 12:46:07 +03:00
static char buf[8];
Q_sprintf(buf, "%d", d);
return buf;
}
NOXREF char *UTIL_dtos3(int d)
{
2015-06-30 12:46:07 +03:00
static char buf[8];
Q_sprintf(buf, "%d", d);
return buf;
}
NOXREF char *UTIL_dtos4(int d)
{
2015-06-30 12:46:07 +03:00
static char buf[8];
Q_sprintf(buf, "%d", d);
return buf;
}
void UTIL_ShowMessageArgs(const char *pString, CBaseEntity *pPlayer, CUtlVector<char *> *args, bool isHint)
{
2016-01-25 20:02:57 +03:00
if (pPlayer != NULL && pPlayer->IsNetClient())
{
MESSAGE_BEGIN(MSG_ONE, gmsgHudTextArgs, NULL, pPlayer->pev);
WRITE_STRING(pString);
WRITE_BYTE(isHint);
WRITE_BYTE(args->Count());
2016-01-25 20:02:57 +03:00
for (int i = 0; i < args->Count(); ++i)
WRITE_STRING(args->Element(i));
MESSAGE_END();
}
else
{
MESSAGE_BEGIN(MSG_ONE, gmsgHudText, NULL, pPlayer->pev);
WRITE_STRING(pString);
WRITE_BYTE(isHint);
MESSAGE_END();
}
}
void UTIL_ShowMessage(const char *pString, CBaseEntity *pEntity, bool isHint)
{
if (!pEntity || !pEntity->IsNetClient())
return;
MESSAGE_BEGIN(MSG_ONE, gmsgHudText, NULL, pEntity->edict());
WRITE_STRING(pString);
WRITE_BYTE(int(isHint));
MESSAGE_END();
}
void UTIL_ShowMessageAll(const char *pString, bool isHint)
{
2016-01-25 20:02:57 +03:00
for (int i = 1; i <= gpGlobals->maxClients; ++i)
2015-06-30 12:46:07 +03:00
{
CBaseEntity *pPlayer = UTIL_PlayerByIndex(i);
2016-01-25 20:02:57 +03:00
if (pPlayer != NULL)
2015-06-30 12:46:07 +03:00
UTIL_ShowMessage(pString, pPlayer, isHint);
}
}
void UTIL_TraceLine(const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, edict_t *pentIgnore, TraceResult *ptr)
{
TRACE_LINE(vecStart, vecEnd, (igmon == ignore_monsters), pentIgnore, ptr);
}
// OVERLOAD
void UTIL_TraceLine(const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, IGNORE_GLASS ignoreGlass, edict_t *pentIgnore, TraceResult *ptr)
{
TRACE_LINE(vecStart, vecEnd, (igmon == ignore_monsters) | (ignoreGlass ? 0x100 : 0), pentIgnore, ptr);
}
void UTIL_TraceHull(const Vector &vecStart, const Vector &vecEnd, IGNORE_MONSTERS igmon, int hullNumber, edict_t *pentIgnore, TraceResult *ptr)
{
TRACE_HULL(vecStart, vecEnd, (igmon == ignore_monsters), hullNumber, pentIgnore, ptr);
}
void UTIL_TraceModel(const Vector &vecStart, const Vector &vecEnd, int hullNumber, edict_t *pentModel, TraceResult *ptr)
{
TRACE_MODEL(vecStart, vecEnd, hullNumber, pentModel, ptr);
}
NOXREF TraceResult UTIL_GetGlobalTrace()
{
2015-06-30 12:46:07 +03:00
TraceResult tr;
tr.flFraction = gpGlobals->trace_fraction;
tr.fInWater = int(gpGlobals->trace_inwater);
tr.fAllSolid = int(gpGlobals->trace_allsolid);
tr.fStartSolid = int(gpGlobals->trace_startsolid);
tr.fInOpen = int(gpGlobals->trace_inopen);
2015-06-30 12:46:07 +03:00
tr.vecEndPos = gpGlobals->trace_endpos;
tr.flPlaneDist = gpGlobals->trace_plane_dist;
tr.vecPlaneNormal = gpGlobals->trace_plane_normal;
tr.iHitgroup = gpGlobals->trace_hitgroup;
tr.pHit = gpGlobals->trace_ent;
return tr;
}
void UTIL_SetSize(entvars_t *pev, const Vector &vecMin, const Vector &vecMax)
{
SET_SIZE(ENT(pev), vecMin, vecMax);
}
float UTIL_VecToYaw(const Vector &vec)
{
return VEC_TO_YAW(vec);
}
void UTIL_SetOrigin(entvars_t *pev, const Vector &vecOrigin)
{
edict_t *ent = ENT(pev);
2016-01-25 20:02:57 +03:00
if (ent != NULL)
SET_ORIGIN(ent, vecOrigin);
}
NOXREF void UTIL_ParticleEffect(const Vector &vecOrigin, const Vector &vecDirection, ULONG ulColor, ULONG ulCount)
{
PARTICLE_EFFECT(vecOrigin, vecDirection, float(ulColor), float(ulCount));
}
float UTIL_Approach(float target, float value, float speed)
{
float delta = target - value;
if (delta > speed)
value += speed;
else if (delta < -speed)
value -= speed;
2015-09-16 23:19:21 +03:00
else
value = target;
2016-01-25 20:02:57 +03:00
return value;
}
2015-09-16 23:19:21 +03:00
float_precision UTIL_ApproachAngle(float target, float value, float speed)
{
target = UTIL_AngleMod(target);
value = UTIL_AngleMod(target);
2015-09-16 23:19:21 +03:00
float delta = target - value;
if (speed < 0.0f)
speed = -speed;
if (delta < -180.0f)
delta += 360.0f;
else if (delta > 180.0f)
delta -= 360.0f;
if (delta > speed)
value += speed;
else if (delta < -speed)
value -= speed;
2015-09-16 23:19:21 +03:00
else
value = target;
2016-01-25 20:02:57 +03:00
return value;
}
2015-09-16 23:19:21 +03:00
float_precision UTIL_AngleDistance(float next, float cur)
{
float_precision delta;
delta = next - cur;
if (delta < -180.0f)
delta += 360.0f;
else if (delta > 180.0f)
delta -= 360.0f;
return delta;
}
float UTIL_SplineFraction(float value, float scale)
{
float valueSquared = value * scale;
return 3.0f * (valueSquared * valueSquared) - (valueSquared * valueSquared * valueSquared + valueSquared * valueSquared * valueSquared);
}
char *UTIL_VarArgs(char *format, ...)
{
va_list argptr;
static char string[1024];
2015-06-30 12:46:07 +03:00
va_start(argptr, format);
vsprintf(string, format, argptr);
va_end(argptr);
return string;
}
NOXREF Vector UTIL_GetAimVector(edict_t *pent, float flSpeed)
{
2015-06-30 12:46:07 +03:00
Vector tmp;
GET_AIM_VECTOR(pent, flSpeed, tmp);
return tmp;
}
int UTIL_IsMasterTriggered(string_t sMaster, CBaseEntity *pActivator)
{
2015-06-30 12:46:07 +03:00
if (sMaster)
{
edict_t *pentTarget = FIND_ENTITY_BY_TARGETNAME(NULL, STRING(sMaster));
if (!FNullEnt(pentTarget))
{
CBaseEntity *pMaster = CBaseEntity::Instance(pentTarget);
if (pMaster && (pMaster->ObjectCaps() & FCAP_MASTER))
return pMaster->IsTriggered(pActivator);
}
ALERT(at_console, "Master was null or not a master!\n");
}
2015-08-20 13:35:01 +03:00
return 1;
}
BOOL UTIL_ShouldShowBlood(int color)
{
2015-06-30 12:46:07 +03:00
if (color != DONT_BLEED)
{
if (color == BLOOD_COLOR_RED)
{
if (CVAR_GET_FLOAT("violence_hblood") != 0.0f)
return TRUE;
}
else
{
if (CVAR_GET_FLOAT("violence_ablood") != 0.0f)
return TRUE;
}
}
2016-01-25 20:02:57 +03:00
return FALSE;
}
int UTIL_PointContents(const Vector &vec)
{
return POINT_CONTENTS(vec);
}
void UTIL_BloodStream(const Vector &origin, const Vector &direction, int color, int amount)
{
2015-06-30 12:46:07 +03:00
if (!UTIL_ShouldShowBlood(color))
return;
if (g_Language == LANGUAGE_GERMAN && color == BLOOD_COLOR_RED)
color = 0;
MESSAGE_BEGIN(MSG_PVS, SVC_TEMPENTITY, origin);
WRITE_BYTE(TE_BLOODSTREAM);
WRITE_COORD(origin.x);
WRITE_COORD(origin.y);
WRITE_COORD(origin.z);
WRITE_COORD(direction.x);
WRITE_COORD(direction.y);
WRITE_COORD(direction.z);
WRITE_BYTE(color);
2015-09-16 23:19:21 +03:00
WRITE_BYTE(Q_min(amount, 255));
MESSAGE_END();
}
void UTIL_BloodDrips(const Vector &origin, const Vector &direction, int color, int amount)
{
2015-06-30 12:46:07 +03:00
if (!UTIL_ShouldShowBlood(color))
return;
if (color == DONT_BLEED || amount == 0)
return;
if (g_Language == LANGUAGE_GERMAN && color == BLOOD_COLOR_RED)
color = 0;
if (g_pGameRules->IsMultiplayer())
amount *= 2;
if (amount > 255)
amount = 255;
MESSAGE_BEGIN(MSG_PVS, SVC_TEMPENTITY, origin);
WRITE_BYTE(TE_BLOODSPRITE);
WRITE_COORD(origin.x);
WRITE_COORD(origin.y);
WRITE_COORD(origin.z);
WRITE_SHORT(g_sModelIndexBloodSpray);
WRITE_SHORT(g_sModelIndexBloodDrop);
WRITE_BYTE(color);
2015-09-16 23:19:21 +03:00
WRITE_BYTE(Q_min(Q_max(3, amount / 10), 16));
MESSAGE_END();
}
Vector UTIL_RandomBloodVector()
{
2015-06-30 12:46:07 +03:00
Vector direction;
direction.x = RANDOM_FLOAT(-1, 1);
direction.y = RANDOM_FLOAT(-1, 1);
direction.z = RANDOM_FLOAT(0, 1);
return direction;
}
void UTIL_BloodDecalTrace(TraceResult *pTrace, int bloodColor)
{
2015-06-30 12:46:07 +03:00
if (UTIL_ShouldShowBlood(bloodColor))
{
if (bloodColor == BLOOD_COLOR_RED)
UTIL_DecalTrace(pTrace, DECAL_BLOOD1 + RANDOM_LONG(0, 5));
else
UTIL_DecalTrace(pTrace, DECAL_YBLOOD1 + RANDOM_LONG(0, 5));
}
}
void UTIL_DecalTrace(TraceResult *pTrace, int decalNumber)
{
short entityIndex;
int index;
int message;
2015-06-30 12:46:07 +03:00
if (decalNumber < 0)
return;
index = gDecals[decalNumber].index;
if (index < 0 || pTrace->flFraction == 1.0f)
return;
if (pTrace->pHit)
{
CBaseEntity *pEntity = CBaseEntity::Instance(pTrace->pHit);
if (pEntity && !pEntity->IsBSPModel())
return;
entityIndex = ENTINDEX(pTrace->pHit);
}
else
entityIndex = 0;
message = TE_DECAL;
if (entityIndex)
{
if (index > 255)
{
message = TE_DECALHIGH;
index -= 256;
}
}
else
{
message = TE_WORLDDECAL;
if (index > 255)
{
message = TE_WORLDDECALHIGH;
index -= 256;
}
}
2016-01-25 20:02:57 +03:00
2015-06-30 12:46:07 +03:00
MESSAGE_BEGIN(MSG_BROADCAST, SVC_TEMPENTITY);
WRITE_BYTE(message);
WRITE_COORD(pTrace->vecEndPos.x);
WRITE_COORD(pTrace->vecEndPos.y);
WRITE_COORD(pTrace->vecEndPos.z);
WRITE_BYTE(index);
if (entityIndex)
2015-06-30 12:46:07 +03:00
WRITE_SHORT(entityIndex);
MESSAGE_END();
}
void UTIL_PlayerDecalTrace(TraceResult *pTrace, int playernum, int decalNumber, BOOL bIsCustom)
{
2015-06-30 12:46:07 +03:00
int index;
if (!bIsCustom)
{
if (decalNumber < 0)
return;
index = gDecals[decalNumber].index;
if (index < 0)
return;
}
else
index = decalNumber;
if (pTrace->flFraction != 1.0f)
{
MESSAGE_BEGIN(MSG_BROADCAST, SVC_TEMPENTITY);
WRITE_BYTE(TE_PLAYERDECAL);
WRITE_BYTE(playernum);
WRITE_COORD(pTrace->vecEndPos.x);
WRITE_COORD(pTrace->vecEndPos.y);
WRITE_COORD(pTrace->vecEndPos.z);
WRITE_SHORT(int(ENTINDEX(pTrace->pHit)));
2015-06-30 12:46:07 +03:00
WRITE_BYTE(index);
MESSAGE_END();
}
}
void UTIL_GunshotDecalTrace(TraceResult *pTrace, int decalNumber, bool ClientOnly, entvars_t *pShooter)
{
2015-06-30 12:46:07 +03:00
if (decalNumber < 0)
return;
int index = gDecals[decalNumber].index;
if (index < 0 || pTrace->flFraction == 1.0f)
return;
if (ClientOnly)
MESSAGE_BEGIN(MSG_ONE, SVC_TEMPENTITY, pTrace->vecEndPos, pShooter);
else
MESSAGE_BEGIN(MSG_PAS, SVC_TEMPENTITY, pTrace->vecEndPos);
WRITE_BYTE(TE_GUNSHOTDECAL);
2015-08-20 13:35:01 +03:00
WRITE_COORD(pTrace->vecEndPos.x);
WRITE_COORD(pTrace->vecEndPos.y);
WRITE_COORD(pTrace->vecEndPos.z);
WRITE_SHORT(int(ENTINDEX(pTrace->pHit)));
2015-08-20 13:35:01 +03:00
WRITE_BYTE(index);
MESSAGE_END();
}
void UTIL_Sparks(const Vector &position)
{
2015-06-30 12:46:07 +03:00
MESSAGE_BEGIN(MSG_PVS, SVC_TEMPENTITY, position);
WRITE_BYTE(TE_SPARKS);
WRITE_COORD(position.x);
WRITE_COORD(position.y);
2015-06-30 12:46:07 +03:00
WRITE_COORD(position.z);
MESSAGE_END();
}
void UTIL_Ricochet(const Vector &position, float scale)
{
2015-06-30 12:46:07 +03:00
MESSAGE_BEGIN(MSG_PVS, SVC_TEMPENTITY, position);
WRITE_BYTE(TE_ARMOR_RICOCHET);
WRITE_COORD(position.x);
WRITE_COORD(position.y);
WRITE_COORD(position.z);
WRITE_BYTE(int(scale * 10.0f));
MESSAGE_END();
}
BOOL UTIL_TeamsMatch(const char *pTeamName1, const char *pTeamName2)
{
2015-06-30 12:46:07 +03:00
if (!g_pGameRules->IsTeamplay())
return TRUE;
if (*pTeamName1 != '\0' && *pTeamName2 != '\0')
{
if (!Q_stricmp(pTeamName1, pTeamName2))
return TRUE;
}
2016-01-25 20:02:57 +03:00
return FALSE;
}
void UTIL_StringToVector(float *pVector, const char *pString)
{
2015-06-30 12:46:07 +03:00
char *pstr;
char *pfront;
char tempString[128];
int j;
Q_strcpy(tempString, pString);
pstr = tempString;
pfront = tempString;
for (j = 0; j < 3; j++)
{
2015-08-20 13:35:01 +03:00
pVector[j] = Q_atof(pfront);
2015-06-30 12:46:07 +03:00
while (*pstr && *pstr != ' ')
pstr++;
if (!*pstr)
break;
pstr++;
pfront = pstr;
}
for (j++; j < 3; j++)
pVector[j] = 0;
}
void UTIL_StringToIntArray(int *pVector, int count, const char *pString)
2015-06-30 12:46:07 +03:00
{
char *pstr;
char *pfront;
char tempString[128];
int j;
Q_strcpy(tempString, pString);
pstr = tempString;
pfront = tempString;
for (j = 0; j < count; j++)
{
2015-08-20 13:35:01 +03:00
pVector[j] = Q_atoi(pfront);
2015-06-30 12:46:07 +03:00
while (*pstr && *pstr != ' ')
pstr++;
if (!*pstr)
break;
pstr++;
pfront = pstr;
}
for (j++; j < count; j++)
pVector[j] = 0;
}
Vector UTIL_ClampVectorToBox(const Vector &input, const Vector &clampSize)
{
2015-06-30 12:46:07 +03:00
Vector sourceVector = input;
if (sourceVector.x > clampSize.x)
sourceVector.x -= clampSize.x;
else if (sourceVector.x < -clampSize.x)
sourceVector.x += clampSize.x;
else
sourceVector.x = 0;
if (sourceVector.y > clampSize.y)
sourceVector.y -= clampSize.y;
else if (sourceVector.y < -clampSize.y)
sourceVector.y += clampSize.y;
else
sourceVector.y = 0;
if (sourceVector.z > clampSize.z)
sourceVector.z -= clampSize.z;
else if (sourceVector.z < -clampSize.z)
sourceVector.z += clampSize.z;
else
sourceVector.z = 0;
return sourceVector.Normalize();
}
float UTIL_WaterLevel(const Vector &position, float minz, float maxz)
{
Vector midUp;
float diff;
2015-06-30 12:46:07 +03:00
midUp = position;
midUp.z = minz;
if (UTIL_PointContents(midUp) != CONTENTS_WATER)
return minz;
midUp.z = maxz;
if (UTIL_PointContents(midUp) == CONTENTS_WATER)
return maxz;
diff = maxz - minz;
while (diff > 1)
{
midUp.z = minz + diff / 2;
if (UTIL_PointContents(midUp) == CONTENTS_WATER)
minz = midUp.z;
else
maxz = midUp.z;
diff = maxz - minz;
}
return midUp.z;
}
void UTIL_Bubbles(Vector mins, Vector maxs, int count)
{
2015-06-30 12:46:07 +03:00
Vector mid = (mins + maxs) * 0.5f;
float flHeight = UTIL_WaterLevel(mid, mid.z, mid.z + 1024.0f) - mins.z;
MESSAGE_BEGIN(MSG_PAS, SVC_TEMPENTITY, mid);
WRITE_BYTE(TE_BUBBLES);
WRITE_COORD(mins.x);
WRITE_COORD(mins.y);
WRITE_COORD(mins.z);
WRITE_COORD(maxs.x);
WRITE_COORD(maxs.y);
WRITE_COORD(maxs.z);
WRITE_COORD(flHeight);
WRITE_SHORT(g_sModelIndexBubbles);
WRITE_BYTE(count);
2015-06-30 12:46:07 +03:00
WRITE_COORD(8);
MESSAGE_END();
}
void UTIL_BubbleTrail(Vector from, Vector to, int count)
{
2015-06-30 12:46:07 +03:00
float flHeight = UTIL_WaterLevel(from, from.z, from.z + 256.0f) - from.z;
if (flHeight < 8.0f)
{
flHeight = UTIL_WaterLevel(to, to.z, to.z + 256.0f) - to.z;
if (flHeight < 8.0f)
return;
flHeight = flHeight + to.z - from.z;
}
if (count > 255)
count = 255;
MESSAGE_BEGIN(MSG_BROADCAST, SVC_TEMPENTITY);
WRITE_BYTE(TE_BUBBLETRAIL);
WRITE_COORD(from.x);
WRITE_COORD(from.y);
WRITE_COORD(from.z);
WRITE_COORD(to.x);
WRITE_COORD(to.y);
WRITE_COORD(to.z);
WRITE_COORD(flHeight);
WRITE_SHORT(g_sModelIndexBubbles);
WRITE_BYTE(count);
2015-06-30 12:46:07 +03:00
WRITE_COORD(8);
MESSAGE_END();
}
void UTIL_Remove(CBaseEntity *pEntity)
{
2016-01-25 20:02:57 +03:00
if (pEntity != NULL)
{
pEntity->UpdateOnRemove();
pEntity->pev->flags |= FL_KILLME;
pEntity->pev->targetname = 0;
}
}
NOXREF BOOL UTIL_IsValidEntity(edict_t *pent)
{
2015-06-30 12:46:07 +03:00
if (!pent || pent->free || (pent->v.flags & FL_KILLME))
return FALSE;
return TRUE;
}
void UTIL_PrecacheOther(const char *szClassname)
{
2015-06-30 12:46:07 +03:00
edict_t *pent = CREATE_NAMED_ENTITY(MAKE_STRING(szClassname));
if (FNullEnt(pent))
{
ALERT(at_console, "NULL Ent in UTIL_PrecacheOther\n");
return;
}
CBaseEntity *pEntity = CBaseEntity::Instance(VARS(pent));
if (pEntity != NULL)
2015-08-20 13:35:01 +03:00
{
2015-06-30 12:46:07 +03:00
pEntity->Precache();
2015-08-20 13:35:01 +03:00
}
2015-06-30 12:46:07 +03:00
REMOVE_ENTITY(pent);
}
void UTIL_LogPrintf(const char *fmt, ...)
{
2015-06-30 12:46:07 +03:00
va_list argptr;
static char string[1024];
va_start(argptr, fmt);
vsprintf(string, fmt, argptr);
va_end(argptr);
ALERT(at_logged, "%s", string);
}
NOXREF float UTIL_DotPoints(const Vector &vecSrc, const Vector &vecCheck, const Vector &vecDir)
{
2015-06-30 12:46:07 +03:00
Vector2D vec2LOS = (vecCheck - vecSrc).Make2D();
vec2LOS = vec2LOS.Normalize();
return DotProduct(vec2LOS, (vecDir.Make2D()));
}
void UTIL_StripToken(const char *pKey, char *pDest)
{
2015-06-30 12:46:07 +03:00
int i = 0;
while (pKey[i] && pKey[i] != '#')
{
pDest[i] = pKey[i];
2016-01-25 20:02:57 +03:00
++i;
2015-06-30 12:46:07 +03:00
}
2016-01-25 20:02:57 +03:00
pDest[i] = '\0';
}
CSaveRestoreBuffer::CSaveRestoreBuffer()
{
m_pdata = NULL;
}
CSaveRestoreBuffer::CSaveRestoreBuffer(SAVERESTOREDATA *pdata)
{
m_pdata = pdata;
}
CSaveRestoreBuffer::~CSaveRestoreBuffer()
{
;
}
int CSaveRestoreBuffer::EntityIndex(CBaseEntity *pEntity)
{
2015-06-30 12:46:07 +03:00
if (!pEntity)
return -1;
return EntityIndex(pEntity->pev);
}
int CSaveRestoreBuffer::EntityIndex(entvars_t *pevLookup)
{
2015-06-30 12:46:07 +03:00
if (!pevLookup)
return -1;
return EntityIndex(ENT(pevLookup));
}
int CSaveRestoreBuffer::EntityIndex(EOFFSET eoLookup)
{
return EntityIndex(ENT(eoLookup));
}
int CSaveRestoreBuffer::EntityIndex(edict_t *pentLookup)
{
2015-06-30 12:46:07 +03:00
if (!m_pdata || !pentLookup)
return -1;
2016-01-25 20:02:57 +03:00
for (int i = 0; i < m_pdata->tableCount; ++i)
2015-06-30 12:46:07 +03:00
{
ENTITYTABLE *pTable = &m_pdata->pTable[i];
if (pTable->pent == pentLookup)
return i;
}
2016-01-25 20:02:57 +03:00
return -1;
}
edict_t *CSaveRestoreBuffer::EntityFromIndex(int entityIndex)
{
2015-06-30 12:46:07 +03:00
if (!m_pdata || entityIndex < 0)
return NULL;
2016-01-25 20:02:57 +03:00
for (int i = 0; i < m_pdata->tableCount; ++i)
2015-06-30 12:46:07 +03:00
{
ENTITYTABLE *pTable = &m_pdata->pTable[i];
if (pTable->id == entityIndex)
return pTable->pent;
}
2016-01-25 20:02:57 +03:00
return NULL;
}
int CSaveRestoreBuffer::EntityFlagsSet(int entityIndex, int flags)
{
2015-06-30 12:46:07 +03:00
if (!m_pdata || entityIndex < 0)
return 0;
if (!m_pdata || entityIndex < 0 || entityIndex > m_pdata->tableCount)
return 0;
2015-08-20 13:35:01 +03:00
m_pdata->pTable[ entityIndex ].flags |= flags;
return m_pdata->pTable[ entityIndex ].flags;
}
void CSaveRestoreBuffer::BufferRewind(int size)
{
2015-06-30 12:46:07 +03:00
if (!m_pdata)
return;
if (m_pdata->size < size)
size = m_pdata->size;
m_pdata->pCurrentData -= size;
m_pdata->size -= size;
}
#ifndef _WIN32
extern "C"
{
inline unsigned _rotr(unsigned val, int shift)
{
register unsigned lobit;
register unsigned num = val;
shift &= 0x1f;
2015-09-16 23:19:21 +03:00
while (shift--)
{
lobit = num & 1;
num >>= 1;
if (lobit)
num |= 0x80000000;
}
2016-01-25 20:02:57 +03:00
return num;
}
}
2015-12-09 01:39:54 +03:00
#endif // _WIN32
unsigned int CSaveRestoreBuffer::HashString(const char *pszToken)
{
2015-06-30 12:46:07 +03:00
unsigned int hash = 0;
while (*pszToken)
hash = _rotr(hash, 4) ^ *pszToken++;
return hash;
}
unsigned short CSaveRestoreBuffer::TokenHash(const char *pszToken)
{
2015-06-30 12:46:07 +03:00
unsigned short hash = (unsigned short)(HashString(pszToken) % (unsigned)m_pdata->tokenCount);
2016-01-25 20:02:57 +03:00
for (int i = 0; i < m_pdata->tokenCount; ++i)
2015-06-30 12:46:07 +03:00
{
int index = hash + i;
if (index >= m_pdata->tokenCount)
index -= m_pdata->tokenCount;
if (!m_pdata->pTokens[index] || !Q_strcmp(pszToken, m_pdata->pTokens[index]))
{
m_pdata->pTokens[index] = (char *)pszToken;
return index;
}
}
2015-12-09 01:39:54 +03:00
2015-06-30 12:46:07 +03:00
ALERT(at_error, "CSaveRestoreBuffer :: TokenHash() is COMPLETELY FULL!");
return 0;
}
2015-12-09 01:39:54 +03:00
void CSave::WriteData(const char *pname, int size, const char *pdata)
{
BufferField(pname, size, pdata);
}
NOXREF void CSave::WriteShort(const char *pname, const short *data, int count)
{
BufferField(pname, sizeof(short) * count, (const char *)data);
}
2015-12-09 01:39:54 +03:00
void CSave::WriteInt(const char *pname, const int *data, int count)
{
BufferField(pname, sizeof(int) * count, (const char *)data);
}
2015-12-09 01:39:54 +03:00
void CSave::WriteFloat(const char *pname, const float *data, int count)
{
BufferField(pname, sizeof(float) * count, (const char *)data);
}
2015-12-09 01:39:54 +03:00
void CSave::WriteTime(const char *pname, const float *data, int count)
{
int i;
2015-06-30 12:46:07 +03:00
BufferHeader(pname, sizeof(float) * count);
2016-01-25 20:02:57 +03:00
for (i = 0; i < count; ++i)
2015-06-30 12:46:07 +03:00
{
float tmp = data[0];
if (m_pdata)
tmp -= m_pdata->time;
BufferData((const char *)&tmp, sizeof(float));
data++;
}
}
NOXREF void CSave::WriteString(const char *pname, const char *pdata)
{
BufferField(pname, Q_strlen(pdata) + 1, pdata);
}
2015-12-09 01:39:54 +03:00
void CSave::WriteString(const char *pname, const int *stringId, int count)
{
int i;
int size = 0;
2016-01-25 20:02:57 +03:00
for (i = 0; i < count; ++i)
size += Q_strlen(STRING(stringId[i])) + 1;
2015-06-30 12:46:07 +03:00
BufferHeader(pname, size);
2016-01-25 20:02:57 +03:00
for (i = 0; i < count; ++i)
2015-06-30 12:46:07 +03:00
{
const char *pString = STRING(stringId[i]);
BufferData(pString, Q_strlen(pString) + 1);
}
}
2015-12-09 01:39:54 +03:00
void CSave::WriteVector(const char *pname, const Vector &value)
{
WriteVector(pname, &value.x, 1);
}
2015-12-09 01:39:54 +03:00
void CSave::WriteVector(const char *pname, const float *value, int count)
{
2015-06-30 12:46:07 +03:00
BufferHeader(pname, sizeof(float) * 3 * count);
BufferData((const char *)value, sizeof(float) * 3 * count);
}
NOXREF void CSave::WritePositionVector(const char *pname, const Vector &value)
{
2015-06-30 12:46:07 +03:00
if (m_pdata && m_pdata->fUseLandmark)
{
Vector tmp = value - m_pdata->vecLandmarkOffset;
WriteVector(pname, tmp);
}
WriteVector(pname, value);
}
2015-12-09 01:39:54 +03:00
void CSave::WritePositionVector(const char *pname, const float *value, int count)
{
2015-06-30 12:46:07 +03:00
BufferHeader(pname, sizeof(float) * 3 * count);
2016-01-25 20:02:57 +03:00
for (int i = 0; i < count; ++i)
2015-06-30 12:46:07 +03:00
{
Vector tmp(value[0], value[1], value[2]);
if (m_pdata && m_pdata->fUseLandmark)
tmp = tmp - m_pdata->vecLandmarkOffset;
BufferData((const char *)&tmp.x, sizeof(float) * 3);
value += 3;
}
}
2015-12-09 01:39:54 +03:00
void CSave::WriteFunction(const char *pname, void **data, int count)
{
2015-06-30 12:46:07 +03:00
const char *functionName = NAME_FOR_FUNCTION((uint32)*data);
if (functionName)
BufferField(pname, Q_strlen(functionName) + 1, functionName);
2015-06-30 12:46:07 +03:00
else
ALERT(at_error, "Invalid function pointer in entity!");
}
void EntvarsKeyvalue(entvars_t *pev, KeyValueData *pkvd)
{
2016-01-25 20:02:57 +03:00
for (int i = 0; i < ARRAYSIZE(gEntvarsDescription); ++i)
2015-06-30 12:46:07 +03:00
{
TYPEDESCRIPTION *pField = &gEntvarsDescription[i];
if (!Q_stricmp(pField->fieldName, pkvd->szKeyName))
{
switch (pField->fieldType)
{
case FIELD_STRING:
case FIELD_MODELNAME:
case FIELD_SOUNDNAME:
*(string_t *)((char *)pev + pField->fieldOffset) = ALLOC_STRING(pkvd->szValue);
break;
case FIELD_FLOAT:
case FIELD_TIME:
2015-08-20 13:35:01 +03:00
*(float *)((char *)pev + pField->fieldOffset) = Q_atof(pkvd->szValue);
2015-06-30 12:46:07 +03:00
break;
case FIELD_INTEGER:
2015-08-20 13:35:01 +03:00
*(string_t *)((char *)pev + pField->fieldOffset) = Q_atoi(pkvd->szValue);
2015-06-30 12:46:07 +03:00
break;
case FIELD_VECTOR:
case FIELD_POSITION_VECTOR:
UTIL_StringToVector((float *)((char *)pev + pField->fieldOffset), pkvd->szValue);
break;
default:
case FIELD_EVARS:
case FIELD_CLASSPTR:
case FIELD_EDICT:
case FIELD_ENTITY:
case FIELD_POINTER:
ALERT(at_error, "Bad field in entity!!\n");
break;
}
pkvd->fHandled = TRUE;
break;
}
}
}
int CSave::WriteEntVars(const char *pname, entvars_t *pev)
{
return WriteFields(pname, pev, gEntvarsDescription, ARRAYSIZE(gEntvarsDescription));
}
int CSave::WriteFields(const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount)
{
2015-06-30 12:46:07 +03:00
int i;
int emptyCount = 0;
2016-01-25 20:02:57 +03:00
for (i = 0; i < fieldCount; ++i)
2015-06-30 12:46:07 +03:00
{
void *pOutputData = ((char *)pBaseData + pFields[i].fieldOffset);
if (DataEmpty((const char *)pOutputData, pFields[i].fieldSize * gSizes[pFields[i].fieldType]))
emptyCount++;
}
2015-08-20 13:35:01 +03:00
int entityArray[ MAX_ENTITY_ARRAY ];
2015-06-30 12:46:07 +03:00
int actualCount = fieldCount - emptyCount;
WriteInt(pname, &actualCount, 1);
2016-01-25 20:02:57 +03:00
for (i = 0; i < fieldCount; ++i)
2015-06-30 12:46:07 +03:00
{
TYPEDESCRIPTION *pTest = &pFields[i];
void *pOutputData = ((char *)pBaseData + pTest->fieldOffset);
if (DataEmpty((const char *)pOutputData, pTest->fieldSize * gSizes[pTest->fieldType]))
continue;
switch (pTest->fieldType)
{
case FIELD_FLOAT:
WriteFloat(pTest->fieldName, (float *)pOutputData, pTest->fieldSize);
break;
case FIELD_TIME:
WriteTime(pTest->fieldName, (float *)pOutputData, pTest->fieldSize);
break;
case FIELD_MODELNAME:
case FIELD_SOUNDNAME:
case FIELD_STRING:
WriteString(pTest->fieldName, (int *)pOutputData, pTest->fieldSize);
break;
case FIELD_CLASSPTR:
case FIELD_EVARS:
case FIELD_EDICT:
case FIELD_ENTITY:
case FIELD_EHANDLE:
{
2015-08-20 13:35:01 +03:00
if (pTest->fieldSize > MAX_ENTITY_ARRAY)
ALERT(at_error, "Can't save more than %d entities in an array!!!\n", MAX_ENTITY_ARRAY);
2015-06-30 12:46:07 +03:00
for (int j = 0; j < pTest->fieldSize; j++)
{
switch (pTest->fieldType)
{
case FIELD_EVARS:
entityArray[j] = EntityIndex(((entvars_t **)pOutputData)[j]);
break;
case FIELD_CLASSPTR:
entityArray[j] = EntityIndex(((CBaseEntity **)pOutputData)[j]);
break;
case FIELD_EDICT:
entityArray[j] = EntityIndex(((edict_t **)pOutputData)[j]);
break;
case FIELD_ENTITY:
entityArray[j] = EntityIndex(((EOFFSET *)pOutputData)[j]);
break;
case FIELD_EHANDLE:
entityArray[j] = EntityIndex((CBaseEntity *)(((EHANDLE *)pOutputData)[j]));
break;
default:
break;
}
}
WriteInt(pTest->fieldName, entityArray, pTest->fieldSize);
break;
}
case FIELD_POSITION_VECTOR:
WritePositionVector(pTest->fieldName, (float *)pOutputData, pTest->fieldSize);
break;
case FIELD_VECTOR:
WriteVector(pTest->fieldName, (float *)pOutputData, pTest->fieldSize);
break;
case FIELD_BOOLEAN:
case FIELD_INTEGER:
WriteInt(pTest->fieldName, (int *)pOutputData, pTest->fieldSize);
break;
case FIELD_SHORT:
WriteData(pTest->fieldName, 2 * pTest->fieldSize, ((char *)pOutputData));
break;
case FIELD_CHARACTER:
WriteData(pTest->fieldName, pTest->fieldSize, ((char *)pOutputData));
break;
case FIELD_POINTER:
WriteInt(pTest->fieldName, (int *)(char *)pOutputData, pTest->fieldSize);
break;
case FIELD_FUNCTION:
WriteFunction(pTest->fieldName, &pOutputData, pTest->fieldSize);
break;
default: ALERT(at_error, "Bad field type\n");
}
}
2016-01-25 20:02:57 +03:00
return 1;
}
NOXREF void CSave::BufferString(char *pdata, int len)
{
2015-06-30 12:46:07 +03:00
char c = 0;
BufferData(pdata, len);
BufferData(&c, 1);
}
2015-12-09 01:39:54 +03:00
int CSave::DataEmpty(const char *pdata, int size)
{
2016-01-25 20:02:57 +03:00
for (int i = 0; i < size; ++i)
2015-06-30 12:46:07 +03:00
{
if (pdata[i])
return 0;
}
2016-01-25 20:02:57 +03:00
return 1;
}
void CSave::BufferField(const char *pname, int size, const char *pdata)
2015-06-30 12:46:07 +03:00
{
BufferHeader(pname, size);
BufferData(pdata, size);
}
void CSave::BufferHeader(const char *pname, int size)
{
2015-06-30 12:46:07 +03:00
short hashvalue = TokenHash(pname);
2015-08-20 13:35:01 +03:00
if (size > (1 << (sizeof(short) * 8)))
2015-06-30 12:46:07 +03:00
ALERT(at_error, "CSave :: BufferHeader() size parameter exceeds 'short'!");
BufferData((const char *)&size, sizeof(short));
BufferData((const char *)&hashvalue, sizeof(short));
}
void CSave::BufferData(const char *pdata, int size)
{
2015-06-30 12:46:07 +03:00
if (!m_pdata)
return;
if (m_pdata->size + size > m_pdata->bufferSize)
{
ALERT(at_error, "Save/Restore overflow!");
m_pdata->size = m_pdata->bufferSize;
return;
}
Q_memcpy(m_pdata->pCurrentData, pdata, size);
m_pdata->pCurrentData += size;
m_pdata->size += size;
}
int CRestore::ReadField(void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount, int startField, int size, char *pName, void *pData)
{
2015-06-30 12:46:07 +03:00
float time = 0.0f;
Vector position = Vector(0, 0, 0);
if (m_pdata)
{
time = m_pdata->time;
if (m_pdata->fUseLandmark)
position = m_pdata->vecLandmarkOffset;
}
2016-01-25 20:02:57 +03:00
for (int i = 0; i < fieldCount; ++i)
2015-06-30 12:46:07 +03:00
{
int fieldNumber = (i + startField) % fieldCount;
TYPEDESCRIPTION *pTest = &pFields[fieldNumber];
if (!Q_stricmp(pTest->fieldName, pName))
{
if (!m_global || !(pTest->flags & FTYPEDESC_GLOBAL))
{
for (int j = 0; j < pTest->fieldSize; j++)
{
void *pOutputData = ((char *)pBaseData + pTest->fieldOffset + (j * gSizes[pTest->fieldType]));
void *pInputData = (char *)pData + j * gSizes[pTest->fieldType];
switch (pTest->fieldType)
{
case FIELD_TIME:
{
float timeData = *(float *)pInputData;
timeData += time;
*((float *)pOutputData) = timeData;
break;
}
case FIELD_FLOAT: *((float *)pOutputData) = *(float *)pInputData; break;
case FIELD_MODELNAME:
case FIELD_SOUNDNAME:
case FIELD_STRING:
{
char *pString = (char *)pData;
for (int stringCount = 0; stringCount < j; stringCount++)
{
while (*pString)
pString++;
pString++;
}
pInputData = pString;
if (!Q_strlen((char *)pInputData))
2015-06-30 12:46:07 +03:00
*((int *)pOutputData) = 0;
else
{
int string = ALLOC_STRING((char *)pInputData);
*((int *)pOutputData) = string;
if (!FStringNull(string) && m_precache)
{
if (pTest->fieldType == FIELD_MODELNAME)
PRECACHE_MODEL((char *)STRING(string));
else if (pTest->fieldType == FIELD_SOUNDNAME)
PRECACHE_SOUND((char *)STRING(string));
}
}
break;
}
case FIELD_EVARS:
{
int entityIndex = *(int *)pInputData;
edict_t *pent = EntityFromIndex(entityIndex);
if (pent)
*((entvars_t **)pOutputData) = VARS(pent);
else
*((entvars_t **)pOutputData) = NULL;
break;
}
case FIELD_CLASSPTR:
{
int entityIndex = *(int *)pInputData;
edict_t *pent = EntityFromIndex(entityIndex);
if (pent)
*((CBaseEntity **)pOutputData) = CBaseEntity::Instance(pent);
else
*((CBaseEntity **)pOutputData) = NULL;
break;
}
case FIELD_EDICT:
{
int entityIndex = *(int *)pInputData;
edict_t *pent = EntityFromIndex(entityIndex);
*((edict_t **)pOutputData) = pent;
break;
}
case FIELD_EHANDLE:
{
pOutputData = (char *)pOutputData + j * (sizeof(EHANDLE) - gSizes[pTest->fieldType]);
int entityIndex = *(int *)pInputData;
edict_t *pent = EntityFromIndex(entityIndex);
if (pent)
*((EHANDLE *)pOutputData) = CBaseEntity::Instance(pent);
else
*((EHANDLE *)pOutputData) = NULL;
break;
}
case FIELD_ENTITY:
{
int entityIndex = *(int *)pInputData;
edict_t *pent = EntityFromIndex(entityIndex);
if (pent)
*((EOFFSET *)pOutputData) = OFFSET(pent);
else
*((EOFFSET *)pOutputData) = 0;
break;
}
case FIELD_VECTOR:
{
((float *)pOutputData)[0] = ((float *)pInputData)[0];
((float *)pOutputData)[1] = ((float *)pInputData)[1];
((float *)pOutputData)[2] = ((float *)pInputData)[2];
break;
}
case FIELD_POSITION_VECTOR:
{
((float *)pOutputData)[0] = ((float *)pInputData)[0] + position.x;
((float *)pOutputData)[1] = ((float *)pInputData)[1] + position.y;
((float *)pOutputData)[2] = ((float *)pInputData)[2] + position.z;
break;
}
case FIELD_BOOLEAN:
case FIELD_INTEGER:
*((int *)pOutputData) = *(int *)pInputData;
break;
case FIELD_SHORT:
*((short *)pOutputData) = *(short *)pInputData;
break;
case FIELD_CHARACTER:
*((char *)pOutputData) = *(char *)pInputData;
break;
case FIELD_POINTER:
*((int *)pOutputData) = *(int *)pInputData;
break;
case FIELD_FUNCTION:
{
if (!Q_strlen((char *)pInputData))
2015-06-30 12:46:07 +03:00
*((int *)pOutputData) = 0;
else
*((int *)pOutputData) = FUNCTION_FROM_NAME((char *)pInputData);
break;
}
default:
ALERT(at_error, "Bad field type\n");
break;
}
}
}
return fieldNumber;
}
}
return -1;
}
int CRestore::ReadEntVars(const char *pname, entvars_t *pev)
{
return ReadFields(pname, pev, gEntvarsDescription, ARRAYSIZE(gEntvarsDescription));
}
int CRestore::ReadFields(const char *pname, void *pBaseData, TYPEDESCRIPTION *pFields, int fieldCount)
{
2015-06-30 12:46:07 +03:00
unsigned short i = ReadShort();
unsigned short token = ReadShort();
if (token != TokenHash(pname))
{
BufferRewind(2 * sizeof(short));
return 0;
}
int fileCount = ReadInt();
int lastField = 0;
2016-01-25 20:02:57 +03:00
for (i = 0; i < fieldCount; ++i)
2015-06-30 12:46:07 +03:00
{
if (!m_global || !(pFields[i].flags & FTYPEDESC_GLOBAL))
Q_memset(((char *)pBaseData + pFields[i].fieldOffset), 0, pFields[i].fieldSize * gSizes[pFields[i].fieldType]);
}
2016-01-25 20:02:57 +03:00
for (i = 0; i < fileCount; ++i)
2015-06-30 12:46:07 +03:00
{
HEADER header;
BufferReadHeader(&header);
lastField = ReadField(pBaseData, pFields, fieldCount, lastField, header.size, m_pdata->pTokens[header.token], header.pData);
lastField++;
}
return 1;
}
2015-12-09 01:39:54 +03:00
void CRestore::BufferReadHeader(HEADER *pheader)
{
2015-06-30 12:46:07 +03:00
pheader->size = ReadShort();
pheader->token = ReadShort();
pheader->pData = BufferPointer();
BufferSkipBytes(pheader->size);
}
short CRestore::ReadShort()
{
2015-06-30 12:46:07 +03:00
short tmp = 0;
BufferReadBytes((char *)&tmp, sizeof(short));
return tmp;
}
int CRestore::ReadInt()
{
2015-06-30 12:46:07 +03:00
int tmp = 0;
BufferReadBytes((char *)&tmp, sizeof(int));
return tmp;
}
NOXREF int CRestore::ReadNamedInt(const char *pName)
2015-06-30 12:46:07 +03:00
{
HEADER header;
BufferReadHeader(&header);
return ((int *)header.pData)[0];
}
NOXREF char *CRestore::ReadNamedString(const char *pName)
{
2015-06-30 12:46:07 +03:00
HEADER header;
BufferReadHeader(&header);
return (char *)header.pData;
}
char *CRestore::BufferPointer()
{
2015-06-30 12:46:07 +03:00
if (!m_pdata)
return NULL;
return m_pdata->pCurrentData;
}
void CRestore::BufferReadBytes(char *pOutput, int size)
2015-06-30 12:46:07 +03:00
{
if (!m_pdata || Empty())
return;
if ((m_pdata->size + size) > m_pdata->bufferSize)
{
ALERT(at_error, "Restore overflow!");
m_pdata->size = m_pdata->bufferSize;
return;
}
if (pOutput)
Q_memcpy(pOutput, m_pdata->pCurrentData, size);
m_pdata->pCurrentData += size;
m_pdata->size += size;
}
2015-12-09 01:39:54 +03:00
void CRestore::BufferSkipBytes(int bytes)
{
BufferReadBytes(NULL, bytes);
}
NOXREF int CRestore::BufferSkipZString()
{
2015-06-30 12:46:07 +03:00
if (!m_pdata)
return 0;
int maxLen = m_pdata->bufferSize - m_pdata->size;
int len = 0;
char *pszSearch = m_pdata->pCurrentData;
while (*pszSearch++ && len < maxLen)
len++;
len++;
BufferSkipBytes(len);
return len;
}
NOXREF int CRestore::BufferCheckZString(const char *string)
{
2015-06-30 12:46:07 +03:00
if (!m_pdata)
return 0;
int maxLen = m_pdata->bufferSize - m_pdata->size;
int len = Q_strlen(string);
2015-06-30 12:46:07 +03:00
if (len <= maxLen)
{
if (!Q_strncmp(string, m_pdata->pCurrentData, len))
2015-06-30 12:46:07 +03:00
return 1;
}
2016-01-25 20:02:57 +03:00
return 0;
}
char UTIL_TextureHit(TraceResult *ptr, Vector vecSrc, Vector vecEnd)
{
2015-06-30 12:46:07 +03:00
char chTextureType;
float rgfl1[3];
float rgfl2[3];
const char *pTextureName;
char szbuffer[64];
CBaseEntity *pEntity = CBaseEntity::Instance(ptr->pHit);
if (pEntity && pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE)
return CHAR_TEX_FLESH;
vecSrc.CopyToArray(rgfl1);
vecEnd.CopyToArray(rgfl2);
if (pEntity)
pTextureName = TRACE_TEXTURE(ENT(pEntity->pev), rgfl1, rgfl2);
else
pTextureName = TRACE_TEXTURE(ENT(0), rgfl1, rgfl2);
if (pTextureName)
{
if (*pTextureName == '-' || *pTextureName == '+')
pTextureName += 2;
if (*pTextureName == '{' || *pTextureName == '!' || *pTextureName == '~' || *pTextureName == ' ')
pTextureName++;
Q_strcpy(szbuffer, pTextureName);
2016-01-25 20:02:57 +03:00
szbuffer[16] = '\0';
2015-06-30 12:46:07 +03:00
chTextureType = TEXTURETYPE_Find(szbuffer);
}
else
2016-01-25 20:02:57 +03:00
chTextureType = '\0';
return chTextureType;
}
NOXREF int GetPlayerTeam(int index)
{
2016-01-25 20:02:57 +03:00
CBasePlayer *pPlayer = static_cast<CBasePlayer *>(UTIL_PlayerByIndex(index));
2015-09-16 23:19:21 +03:00
if (pPlayer != NULL)
{
return pPlayer->m_iTeam;
2015-09-16 23:19:21 +03:00
}
return 0;
}
bool UTIL_IsGame(const char *gameName)
{
#ifndef CSTRIKE
2015-09-16 23:19:21 +03:00
if (gameName != NULL)
{
static char gameDir[256];
GET_GAME_DIR(gameDir);
return (Q_stricmp(gameDir, gameName) == 0);
}
#endif
2015-09-16 23:19:21 +03:00
return false;
}
2015-09-16 23:19:21 +03:00
float_precision UTIL_GetPlayerGaitYaw(int playerIndex)
{
2016-01-25 20:02:57 +03:00
CBasePlayer *pPlayer = static_cast<CBasePlayer *>(UTIL_PlayerByIndex(playerIndex));
2015-09-16 23:19:21 +03:00
if (pPlayer != NULL)
{
2015-06-30 12:46:07 +03:00
return pPlayer->m_flGaityaw;
2015-09-16 23:19:21 +03:00
}
return 0;
}
2016-01-28 05:51:34 +03:00
int UTIL_ReadFlags(const char *c)
{
int flags = 0;
2016-01-28 05:51:34 +03:00
while (*c)
{
if (*c >= 'a' && *c <= 'z')
{
flags |= (1 << (*c - 'a'));
}
c++;
}
2016-01-28 05:51:34 +03:00
return flags;
}
// Determine whether bots can be used or not
bool UTIL_AreBotsAllowed()
{
if (g_engfuncs.pfnEngCheckParm == NULL)
return false;
if (g_bIsCzeroGame)
{
// If they pass in -nobots, don't allow bots. This is for people who host servers, to
// allow them to disallow bots to enforce CPU limits.
int nobots = ENG_CHECK_PARM("-nobots", NULL);
if (nobots)
{
return false;
}
return true;
}
#ifdef REGAMEDLL_ADD
// allow the using of bots for CS 1.6
int bots = ENG_CHECK_PARM("-bots", NULL);
if (bots)
{
return true;
}
#endif
return false;
}
bool UTIL_AreHostagesImprov()
{
if (g_bIsCzeroGame)
{
return true;
}
#ifdef REGAMEDLL_ADD
// someday in CS 1.6
// ...
#endif
return false;
}
void MAKE_STRING_CLASS(const char *str, entvars_t *pev)
{
if (!FStringNull(pev->classname))
{
RemoveEntityHashValue(pev, STRING(pev->classname), CLASSNAME);
}
pev->classname = MAKE_STRING(str);
AddEntityHashValue(pev, STRING(pev->classname), CLASSNAME);
}