Skip to content

Commit 5dec3ba

Browse files
authored
New entity trigger_bomb_reset (#796)
* Init * Implementation * Add `trigger_bomb_reset` to `.fgd` * Set CC4 position on CC4::AttachToPlayer() * move to `addons/trigger_bomb_reset` * rename method & member * CTriggerBombReset: Add `SetUse()`
1 parent 7caf748 commit 5dec3ba

File tree

12 files changed

+149
-2
lines changed

12 files changed

+149
-2
lines changed

regamedll/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ set(GAMEDLL_SRCS
228228
"dlls/API/CSPlayerItem.cpp"
229229
"dlls/addons/item_airbox.cpp"
230230
"dlls/addons/point_command.cpp"
231+
"dlls/addons/trigger_bomb_reset.cpp"
231232
"dlls/addons/trigger_random.cpp"
232233
"dlls/addons/trigger_setorigin.cpp"
233234
"dlls/wpn_shared/wpn_ak47.cpp"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
*
3+
* This program is free software; you can redistribute it and/or modify it
4+
* under the terms of the GNU General Public License as published by the
5+
* Free Software Foundation; either version 2 of the License, or (at
6+
* your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful, but
9+
* WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program; if not, write to the Free Software Foundation,
15+
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16+
*
17+
*/
18+
19+
#include "precompiled.h"
20+
21+
LINK_ENTITY_TO_CLASS(trigger_bomb_reset, CTriggerBombReset, CCSTriggerBombReset)
22+
23+
void CTriggerBombReset::Spawn()
24+
{
25+
InitTrigger();
26+
SetTouch(&CTriggerBombReset::Touch);
27+
SetUse(&CTriggerBombReset::ToggleUse);
28+
}
29+
30+
void CTriggerBombReset::Touch(CBaseEntity *pOther)
31+
{
32+
CWeaponBox *pWeaponBox = dynamic_cast<CWeaponBox *>(pOther);
33+
34+
if (pWeaponBox && pWeaponBox->m_bIsBomb)
35+
{
36+
// If the bomb touches this trigger, tell it to reset to its last known valid position.
37+
pWeaponBox->ResetToLastValidPlayerHeldC4Position();
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
*
3+
* This program is free software; you can redistribute it and/or modify it
4+
* under the terms of the GNU General Public License as published by the
5+
* Free Software Foundation; either version 2 of the License, or (at
6+
* your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful, but
9+
* WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program; if not, write to the Free Software Foundation,
15+
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16+
*
17+
*/
18+
19+
#pragma once
20+
21+
#include "triggers.h"
22+
23+
class CTriggerBombReset: public CBaseTrigger
24+
{
25+
public:
26+
virtual void Spawn();
27+
virtual void Touch(CBaseEntity *pOther);
28+
};

regamedll/dlls/player.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8028,6 +8028,7 @@ CBaseEntity *EXT_FUNC CBasePlayer::__API_HOOK(DropPlayerItem)(const char *pszIte
80288028
pWeaponBox->m_bIsBomb = true;
80298029
pWeaponBox->SetThink(&CWeaponBox::BombThink);
80308030
pWeaponBox->pev->nextthink = gpGlobals->time + 1.0f;
8031+
pWeaponBox->SetLastValidHeldC4Position(((CC4 *)pWeapon)->GetLastValidHeldPosition());
80318032

80328033
if (TheCSBots())
80338034
{

regamedll/dlls/weapons.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,24 @@ void CWeaponBox::SetObjectCollisionBox()
21812181
pev->absmax = pev->origin + Vector(16, 16, 16);
21822182
}
21832183

2184+
void CWeaponBox::ResetToLastValidPlayerHeldC4Position()
2185+
{
2186+
if (pev->origin == m_vecLastValidPlayerHeldC4Position)
2187+
{
2188+
return;
2189+
}
2190+
2191+
Vector const vecResetPos = m_vecLastValidPlayerHeldC4Position + Vector(0.0f, 0.0f, 8.0f);
2192+
Vector const angResetAng = Vector(0.0f, RANDOM_FLOAT(0.0f, 360.0f), 0.0f);
2193+
2194+
// Teleport
2195+
pev->velocity = Vector(0.0f, 0.0f, 0.0f);
2196+
pev->movetype = MOVETYPE_NONE;
2197+
pev->flags |= FL_ONGROUND;
2198+
pev->angles = angResetAng;
2199+
UTIL_SetOrigin(pev, vecResetPos);
2200+
}
2201+
21842202
char *CArmoury::m_ItemModels[] = {
21852203
"models/w_mp5.mdl",
21862204
"models/w_tmp.mdl",

regamedll/dlls/weapons.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ class CWeaponBox: public CBaseEntity
483483
void SetModel_OrigFunc(const char *pszModelName);
484484
#endif
485485

486+
void SetLastValidHeldC4Position(const Vector &vecPositon) { m_vecLastValidPlayerHeldC4Position = vecPositon; }
487+
void ResetToLastValidPlayerHeldC4Position();
488+
486489
public:
487490
static TYPEDESCRIPTION m_SaveData[];
488491

@@ -491,6 +494,9 @@ class CWeaponBox: public CBaseEntity
491494
int m_rgAmmo[MAX_AMMO_SLOTS];
492495
int m_cAmmoTypes;
493496
bool m_bIsBomb;
497+
498+
private:
499+
Vector m_vecLastValidPlayerHeldC4Position;
494500
};
495501

496502

@@ -843,6 +849,7 @@ class CAWP: public CBasePlayerWeapon
843849

844850
const float C4_MAX_SPEED = 250.0f;
845851
const float C4_ARMING_ON_TIME = 3.0f;
852+
constexpr float WEAPON_C4_UPDATE_LAST_VALID_PLAYER_HELD_POSITION_INTERVAL = 0.2f;
846853

847854
enum c4_e
848855
{
@@ -862,6 +869,8 @@ class CC4: public CBasePlayerWeapon
862869
virtual int GetItemInfo(ItemInfo *p);
863870
virtual BOOL Deploy();
864871
virtual void Holster(int skiplocal);
872+
virtual void AttachToPlayer(CBasePlayer* pPlayer);
873+
virtual void Think();
865874
virtual float GetMaxSpeed();
866875
virtual int iItemSlot() { return C4_SLOT; }
867876
virtual void PrimaryAttack();
@@ -875,13 +884,16 @@ class CC4: public CBasePlayerWeapon
875884
#endif
876885
}
877886

887+
Vector GetLastValidHeldPosition() const { return m_vecLastValidPlayerHeldPosition; }
888+
878889
public:
879890
bool m_bStartedArming;
880891
bool m_bBombPlacedAnimation;
881892
float m_fArmedTime;
882893

883894
private:
884895
bool m_bHasShield;
896+
Vector m_vecLastValidPlayerHeldPosition;
885897
};
886898

887899

regamedll/dlls/wpn_shared/wpn_c4.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,37 @@ float CC4::GetMaxSpeed()
382382

383383
return C4_MAX_SPEED;
384384
}
385+
386+
void CC4::AttachToPlayer(CBasePlayer* pPlayer)
387+
{
388+
CBasePlayerWeapon::AttachToPlayer(pPlayer);
389+
390+
#ifdef REGAMEDLL_ADD
391+
SetThink(&CC4::Think);
392+
pev->nextthink = gpGlobals->time + WEAPON_C4_UPDATE_LAST_VALID_PLAYER_HELD_POSITION_INTERVAL;
393+
394+
if (pPlayer->IsPlayer() && pPlayer->IsAlive())
395+
{
396+
entvars_t* pevPlayer = pPlayer->pev;
397+
m_vecLastValidPlayerHeldPosition = pevPlayer->origin + pevPlayer->mins;
398+
}
399+
#endif
400+
}
401+
402+
void CC4::Think()
403+
{
404+
#ifdef REGAMEDLL_ADD
405+
pev->nextthink = gpGlobals->time + WEAPON_C4_UPDATE_LAST_VALID_PLAYER_HELD_POSITION_INTERVAL;
406+
407+
// If the bomb is held by an alive player standing on the ground, then we can use this
408+
// position as the last known valid position to respawn the bomb if it gets reset.
409+
410+
if (m_pPlayer && m_pPlayer->IsPlayer() && m_pPlayer->IsAlive() && (m_pPlayer->pev->flags & FL_ONGROUND))
411+
{
412+
entvars_t* pevPlayer = m_pPlayer->pev;
413+
m_vecLastValidPlayerHeldPosition = pevPlayer->origin + pevPlayer->mins;
414+
}
415+
#else
416+
CBasePlayerWeapon::Think();
417+
#endif
418+
}

regamedll/extra/Toolkit/GameDefinitionFile/regamedll-cs.fgd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,10 @@
22722272
]
22732273
]
22742274

2275+
@SolidClass base(Trigger) = trigger_bomb_reset : "Trigger bomb reset"
2276+
[
2277+
]
2278+
22752279
// Function entities
22762280
@SolidClass = func_bomb_target : "Bomb target zone"
22772281
[

regamedll/msvc/ReGameDLL.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Label="ProjectConfigurations">
44
<ProjectConfiguration Include="Debug Play|Win32">
@@ -25,6 +25,7 @@
2525
<ItemGroup>
2626
<ClCompile Include="..\dlls\addons\item_airbox.cpp" />
2727
<ClCompile Include="..\dlls\addons\point_command.cpp" />
28+
<ClCompile Include="..\dlls\addons\trigger_bomb_reset.cpp" />
2829
<ClCompile Include="..\dlls\addons\trigger_random.cpp" />
2930
<ClCompile Include="..\dlls\addons\trigger_setorigin.cpp" />
3031
<ClCompile Include="..\dlls\airtank.cpp" />
@@ -616,6 +617,7 @@
616617
<ClInclude Include="..\dlls\activity.h" />
617618
<ClInclude Include="..\dlls\addons\item_airbox.h" />
618619
<ClInclude Include="..\dlls\addons\point_command.h" />
620+
<ClInclude Include="..\dlls\addons\trigger_bomb_reset.h" />
619621
<ClInclude Include="..\dlls\addons\trigger_random.h" />
620622
<ClInclude Include="..\dlls\addons\trigger_setorigin.h" />
621623
<ClInclude Include="..\dlls\airtank.h" />

regamedll/msvc/ReGameDLL.vcxproj.filters

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
44
<Filter Include="engine">
@@ -549,6 +549,9 @@
549549
<ClCompile Include="..\dlls\addons\point_command.cpp">
550550
<Filter>dlls\addons</Filter>
551551
</ClCompile>
552+
<ClCompile Include="..\dlls\addons\trigger_bomb_reset.cpp">
553+
<Filter>dlls\addons</Filter>
554+
</ClCompile>
552555
<ClCompile Include="..\public\FileSystem.cpp">
553556
<Filter>public</Filter>
554557
</ClCompile>
@@ -1049,5 +1052,8 @@
10491052
<ClInclude Include="..\dlls\addons\point_command.h">
10501053
<Filter>dlls\addons</Filter>
10511054
</ClInclude>
1055+
<ClInclude Include="..\dlls\addons\trigger_bomb_reset.h">
1056+
<Filter>dlls\addons</Filter>
1057+
</ClInclude>
10521058
</ItemGroup>
10531059
</Project>

0 commit comments

Comments
 (0)