halflife-photomode/dlls/items.cpp

340 lines
7.1 KiB
C++
Raw Normal View History

/***
2013-08-30 13:34:05 -07:00
*
* Copyright (c) 1996-2001, 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.
*
****/
/*
===== items.cpp ========================================================
functions governing the selection/use of weapons for players
*/
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "weapons.h"
#include "player.h"
#include "skill.h"
#include "items.h"
#include "gamerules.h"
#include "UserMessages.h"
2013-08-30 13:34:05 -07:00
class CWorldItem : public CBaseEntity
{
public:
bool KeyValue(KeyValueData* pkvd) override;
void Spawn() override;
int m_iType;
2013-08-30 13:34:05 -07:00
};
LINK_ENTITY_TO_CLASS(world_items, CWorldItem);
bool CWorldItem::KeyValue(KeyValueData* pkvd)
2013-08-30 13:34:05 -07:00
{
if (FStrEq(pkvd->szKeyName, "type"))
{
m_iType = 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 CWorldItem::Spawn()
2013-08-30 13:34:05 -07:00
{
CBaseEntity* pEntity = NULL;
2013-08-30 13:34:05 -07:00
switch (m_iType)
2013-08-30 13:34:05 -07:00
{
case 44: // ITEM_BATTERY:
pEntity = CBaseEntity::Create("item_battery", pev->origin, pev->angles);
2013-08-30 13:34:05 -07:00
break;
case 42: // ITEM_ANTIDOTE:
pEntity = CBaseEntity::Create("item_antidote", pev->origin, pev->angles);
2013-08-30 13:34:05 -07:00
break;
case 43: // ITEM_SECURITY:
pEntity = CBaseEntity::Create("item_security", pev->origin, pev->angles);
2013-08-30 13:34:05 -07:00
break;
case 45: // ITEM_SUIT:
pEntity = CBaseEntity::Create("item_suit", pev->origin, pev->angles);
2013-08-30 13:34:05 -07:00
break;
}
if (!pEntity)
{
ALERT(at_console, "unable to create world_item %d\n", m_iType);
2013-08-30 13:34:05 -07:00
}
else
{
pEntity->pev->target = pev->target;
pEntity->pev->targetname = pev->targetname;
pEntity->pev->spawnflags = pev->spawnflags;
}
REMOVE_ENTITY(edict());
}
void CItem::Spawn()
2013-08-30 13:34:05 -07:00
{
pev->movetype = MOVETYPE_TOSS;
pev->solid = SOLID_TRIGGER;
UTIL_SetOrigin(pev, pev->origin);
2013-08-30 13:34:05 -07:00
UTIL_SetSize(pev, Vector(-16, -16, 0), Vector(16, 16, 16));
SetTouch(&CItem::ItemTouch);
if (DROP_TO_FLOOR(ENT(pev)) == 0)
{
ALERT(at_error, "Item %s fell out of level at %f,%f,%f", STRING(pev->classname), pev->origin.x, pev->origin.y, pev->origin.z);
UTIL_Remove(this);
2013-08-30 13:34:05 -07:00
return;
}
}
void CItem::ItemTouch(CBaseEntity* pOther)
2013-08-30 13:34:05 -07:00
{
// if it's not a player, ignore
if (!pOther->IsPlayer())
2013-08-30 13:34:05 -07:00
{
return;
}
CBasePlayer* pPlayer = (CBasePlayer*)pOther;
2013-08-30 13:34:05 -07:00
// ok, a player is touching this item, but can he have it?
if (!g_pGameRules->CanHaveItem(pPlayer, this))
2013-08-30 13:34:05 -07:00
{
// no? Ignore the touch.
return;
}
if (MyTouch(pPlayer))
2013-08-30 13:34:05 -07:00
{
SUB_UseTargets(pOther, USE_TOGGLE, 0);
SetTouch(NULL);
// player grabbed the item.
g_pGameRules->PlayerGotItem(pPlayer, this);
if (g_pGameRules->ItemShouldRespawn(this) == GR_ITEM_RESPAWN_YES)
2013-08-30 13:34:05 -07:00
{
Respawn();
2013-08-30 13:34:05 -07:00
}
else
{
UTIL_Remove(this);
2013-08-30 13:34:05 -07:00
}
}
else if (gEvilImpulse101)
{
UTIL_Remove(this);
2013-08-30 13:34:05 -07:00
}
}
CBaseEntity* CItem::Respawn()
2013-08-30 13:34:05 -07:00
{
SetTouch(NULL);
2013-08-30 13:34:05 -07:00
pev->effects |= EF_NODRAW;
UTIL_SetOrigin(pev, g_pGameRules->VecItemRespawnSpot(this)); // blip to whereever you should respawn.
2013-08-30 13:34:05 -07:00
SetThink(&CItem::Materialize);
pev->nextthink = g_pGameRules->FlItemRespawnTime(this);
2013-08-30 13:34:05 -07:00
return this;
}
void CItem::Materialize()
2013-08-30 13:34:05 -07:00
{
if ((pev->effects & EF_NODRAW) != 0)
2013-08-30 13:34:05 -07:00
{
// changing from invisible state to visible.
EMIT_SOUND_DYN(ENT(pev), CHAN_WEAPON, "items/suitchargeok1.wav", 1, ATTN_NORM, 0, 150);
2013-08-30 13:34:05 -07:00
pev->effects &= ~EF_NODRAW;
pev->effects |= EF_MUZZLEFLASH;
}
SetTouch(&CItem::ItemTouch);
2013-08-30 13:34:05 -07:00
}
#define SF_SUIT_SHORTLOGON 0x0001
2013-08-30 13:34:05 -07:00
class CItemSuit : public CItem
{
void Spawn() override
{
Precache();
2013-08-30 13:34:05 -07:00
SET_MODEL(ENT(pev), "models/w_suit.mdl");
CItem::Spawn();
2013-08-30 13:34:05 -07:00
}
void Precache() override
2013-08-30 13:34:05 -07:00
{
PRECACHE_MODEL("models/w_suit.mdl");
2013-08-30 13:34:05 -07:00
}
bool MyTouch(CBasePlayer* pPlayer) override
2013-08-30 13:34:05 -07:00
{
if (pPlayer->HasSuit())
2021-11-19 13:43:33 +01:00
return false;
2013-08-30 13:34:05 -07:00
if ((pev->spawnflags & SF_SUIT_SHORTLOGON) != 0)
EMIT_SOUND_SUIT(pPlayer->edict(), "!HEV_A0"); // short version of suit logon,
2013-08-30 13:34:05 -07:00
else
EMIT_SOUND_SUIT(pPlayer->edict(), "!HEV_AAx"); // long version of suit logon
2013-08-30 13:34:05 -07:00
pPlayer->SetHasSuit(true);
2021-11-19 13:45:16 +01:00
return true;
2013-08-30 13:34:05 -07:00
}
};
LINK_ENTITY_TO_CLASS(item_suit, CItemSuit);
class CItemBattery : public CItem
{
void Spawn() override
{
Precache();
2013-08-30 13:34:05 -07:00
SET_MODEL(ENT(pev), "models/w_battery.mdl");
CItem::Spawn();
2013-08-30 13:34:05 -07:00
}
void Precache() override
2013-08-30 13:34:05 -07:00
{
PRECACHE_MODEL("models/w_battery.mdl");
PRECACHE_SOUND("items/gunpickup2.wav");
2013-08-30 13:34:05 -07:00
}
bool MyTouch(CBasePlayer* pPlayer) override
2013-08-30 13:34:05 -07:00
{
if (pPlayer->pev->deadflag != DEAD_NO)
2013-08-30 13:34:05 -07:00
{
2021-11-19 13:43:33 +01:00
return false;
2013-08-30 13:34:05 -07:00
}
if ((pPlayer->pev->armorvalue < MAX_NORMAL_BATTERY) &&
pPlayer->HasSuit())
2013-08-30 13:34:05 -07:00
{
int pct;
char szcharge[64];
pPlayer->pev->armorvalue += gSkillData.batteryCapacity;
2018-09-03 00:01:23 +02:00
pPlayer->pev->armorvalue = V_min(pPlayer->pev->armorvalue, MAX_NORMAL_BATTERY);
2013-08-30 13:34:05 -07:00
EMIT_SOUND(pPlayer->edict(), CHAN_ITEM, "items/gunpickup2.wav", 1, ATTN_NORM);
2013-08-30 13:34:05 -07:00
MESSAGE_BEGIN(MSG_ONE, gmsgItemPickup, NULL, pPlayer->pev);
WRITE_STRING(STRING(pev->classname));
2013-08-30 13:34:05 -07:00
MESSAGE_END();
2013-08-30 13:34:05 -07:00
// Suit reports new power level
// For some reason this wasn't working in release build -- round it.
pct = (int)((float)(pPlayer->pev->armorvalue * 100.0) * (1.0 / MAX_NORMAL_BATTERY) + 0.5);
2013-08-30 13:34:05 -07:00
pct = (pct / 5);
if (pct > 0)
pct--;
sprintf(szcharge, "!HEV_%1dP", pct);
2013-08-30 13:34:05 -07:00
//EMIT_SOUND_SUIT(ENT(pev), szcharge);
2021-11-19 13:43:33 +01:00
pPlayer->SetSuitUpdate(szcharge, false, SUIT_NEXT_IN_30SEC);
return true;
2013-08-30 13:34:05 -07:00
}
2021-11-19 13:43:33 +01:00
return false;
2013-08-30 13:34:05 -07:00
}
};
LINK_ENTITY_TO_CLASS(item_battery, CItemBattery);
class CItemAntidote : public CItem
{
void Spawn() override
{
Precache();
2013-08-30 13:34:05 -07:00
SET_MODEL(ENT(pev), "models/w_antidote.mdl");
CItem::Spawn();
2013-08-30 13:34:05 -07:00
}
void Precache() override
2013-08-30 13:34:05 -07:00
{
PRECACHE_MODEL("models/w_antidote.mdl");
2013-08-30 13:34:05 -07:00
}
bool MyTouch(CBasePlayer* pPlayer) override
2013-08-30 13:34:05 -07:00
{
2021-11-19 13:43:33 +01:00
pPlayer->SetSuitUpdate("!HEV_DET4", false, SUIT_NEXT_IN_1MIN);
2013-08-30 13:34:05 -07:00
pPlayer->m_rgItems[ITEM_ANTIDOTE] += 1;
2021-11-19 13:45:16 +01:00
return true;
2013-08-30 13:34:05 -07:00
}
};
LINK_ENTITY_TO_CLASS(item_antidote, CItemAntidote);
class CItemSecurity : public CItem
{
void Spawn() override
{
Precache();
2013-08-30 13:34:05 -07:00
SET_MODEL(ENT(pev), "models/w_security.mdl");
CItem::Spawn();
2013-08-30 13:34:05 -07:00
}
void Precache() override
2013-08-30 13:34:05 -07:00
{
PRECACHE_MODEL("models/w_security.mdl");
2013-08-30 13:34:05 -07:00
}
bool MyTouch(CBasePlayer* pPlayer) override
2013-08-30 13:34:05 -07:00
{
pPlayer->m_rgItems[ITEM_SECURITY] += 1;
2021-11-19 13:45:16 +01:00
return true;
2013-08-30 13:34:05 -07:00
}
};
LINK_ENTITY_TO_CLASS(item_security, CItemSecurity);
class CItemLongJump : public CItem
{
void Spawn() override
{
Precache();
2013-08-30 13:34:05 -07:00
SET_MODEL(ENT(pev), "models/w_longjump.mdl");
CItem::Spawn();
2013-08-30 13:34:05 -07:00
}
void Precache() override
2013-08-30 13:34:05 -07:00
{
PRECACHE_MODEL("models/w_longjump.mdl");
2013-08-30 13:34:05 -07:00
}
bool MyTouch(CBasePlayer* pPlayer) override
2013-08-30 13:34:05 -07:00
{
if (pPlayer->m_fLongJump)
2013-08-30 13:34:05 -07:00
{
2021-11-19 13:43:33 +01:00
return false;
2013-08-30 13:34:05 -07:00
}
if (pPlayer->HasSuit())
2013-08-30 13:34:05 -07:00
{
pPlayer->m_fLongJump = true; // player now has longjump module
2013-08-30 13:34:05 -07:00
g_engfuncs.pfnSetPhysicsKeyValue(pPlayer->edict(), "slj", "1");
2013-08-30 13:34:05 -07:00
MESSAGE_BEGIN(MSG_ONE, gmsgItemPickup, NULL, pPlayer->pev);
WRITE_STRING(STRING(pev->classname));
2013-08-30 13:34:05 -07:00
MESSAGE_END();
EMIT_SOUND_SUIT(pPlayer->edict(), "!HEV_A1"); // Play the longjump sound UNDONE: Kelly? correct sound?
return true;
2013-08-30 13:34:05 -07:00
}
2021-11-19 13:43:33 +01:00
return false;
2013-08-30 13:34:05 -07:00
}
};
LINK_ENTITY_TO_CLASS(item_longjump, CItemLongJump);