Skip to content

Commit

Permalink
Merge pull request #1 from ipsvn/grenade-types
Browse files Browse the repository at this point in the history
Added support for other grenade types
  • Loading branch information
zwolof committed Jan 30, 2024
2 parents 17f2f8b + a66ce35 commit c60c7d5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 25 deletions.
121 changes: 100 additions & 21 deletions Managers/GrenadeManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Utils;
using ExecutesPlugin.Enums;
Expand Down Expand Up @@ -55,26 +57,103 @@ public void SetupGrenades(Scenario scenario)
}
}

public void ThrowGrenade(Grenade grenade)
{
switch(grenade.Type)
{
case EGrenade.Smoke:
GrenadeFunctions.CSmokeGrenadeProjectile_CreateFunc.Invoke(
grenade.Position!.Handle,
grenade.Angle!.Handle,
grenade.Velocity!.Handle,
grenade.Velocity.Handle,
IntPtr.Zero,
45,
(int)grenade.Team
);
break;

default:
break;
}
public void ThrowGrenade(Grenade grenade)
{
CBaseCSGrenadeProjectile? createdGrenade = null;
switch (grenade.Type)
{
case EGrenade.Smoke:
{
createdGrenade = GrenadeFunctions.CSmokeGrenadeProjectile_CreateFunc.Invoke(
grenade.Position!.Handle,
grenade.Angle!.Handle,
grenade.Velocity!.Handle,
grenade.Velocity.Handle,
IntPtr.Zero,
45,
(int)grenade.Team
);
break;
}
case EGrenade.Flashbang:
{
createdGrenade = Utilities.CreateEntityByName<CFlashbangProjectile>("flashbang_projectile");
if (createdGrenade == null)
{
return;
}

createdGrenade.DispatchSpawn();
break;
}
case EGrenade.Molotov:
case EGrenade.Incendiary:
{
var molotov = Utilities.CreateEntityByName<CMolotovProjectile>("molotov_projectile");
if (molotov == null)
{
return;
}

molotov.Damage = 200;
molotov.DmgRadius = 300;

molotov.DispatchSpawn();
molotov.AcceptInput("InitializeSpawnFromWorld");

if (grenade.Type == EGrenade.Molotov)
{
molotov.SetModel("weapons/models/grenade/molotov/weapon_molotov.vmdl");
}
else if (grenade.Type == EGrenade.Incendiary)
{
// have to set IsIncGrenade after InitializeSpawnFromWorld as it forces it to false
molotov.IsIncGrenade = true;
molotov.SetModel("weapons/models/grenade/incendiary/weapon_incendiarygrenade.vmdl");
}

createdGrenade = molotov;
break;
}
case EGrenade.HighExplosive:
{
createdGrenade = Utilities.CreateEntityByName<CHEGrenadeProjectile>("hegrenade_projectile");
if (createdGrenade == null)
{
return;
}

createdGrenade.Damage = 100;
createdGrenade.DmgRadius = createdGrenade.Damage * 3.5f;
createdGrenade.DispatchSpawn();
createdGrenade.AcceptInput("InitializeSpawnFromWorld");
break;
}
default:
Console.WriteLine($"[Executes] Unimplemented Grenade type {grenade.Type}");
break;
}

if (createdGrenade != null && createdGrenade.DesignerName != "smokegrenade_projectile")
{
createdGrenade.Teleport(grenade.Position!, grenade.Angle!, grenade.Velocity!);

createdGrenade.InitialPosition.X = grenade.Position!.X;
createdGrenade.InitialPosition.Y = grenade.Position!.Y;
createdGrenade.InitialPosition.Z = grenade.Position!.Z;

createdGrenade.InitialVelocity.X = grenade.Velocity!.X;
createdGrenade.InitialVelocity.Y = grenade.Velocity!.Y;
createdGrenade.InitialVelocity.Z = grenade.Velocity!.Z;

createdGrenade.AngVelocity.X = grenade.Velocity!.X;
createdGrenade.AngVelocity.Y = grenade.Velocity!.Y;
createdGrenade.AngVelocity.Z = grenade.Velocity!.Z;

createdGrenade.TeamNum = (byte)grenade.Team;
}

Console.WriteLine($"[Executes] Threw grenade {grenade.Name}");
}
}
}
}
}
10 changes: 6 additions & 4 deletions Memory.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;

namespace ExecutesPlugin.Memory
{
public static class GrenadeFunctions
{
public static MemoryFunctionWithReturn<IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, int> CSmokeGrenadeProjectile_CreateFunc = new(
public static MemoryFunctionWithReturn<IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, IntPtr, int, CSmokeGrenadeProjectile> CSmokeGrenadeProjectile_CreateFunc = new(
Environment.OSVersion.Platform == PlatformID.Unix
? @"\x55\x4c\x89\xc1\x48\x89\xe5\x41\x57\x41\x56\x49\x89\xd6\x48\x89\xf2\x48\x89\xfe\x41\x55\x45\x89\xcd\x41\x54\x4d\x89\xc4\x53\x48\x83\xec\x28\x48\x89\x7d\xb8\x48"
: @"\x48\x89\x5C\x24\x2A\x48\x89\x6C\x24\x2A\x48\x89\x74\x24\x2A\x57\x41\x56\x41\x57\x48\x83\xEC\x50\x4C\x8B\xB4\x24"
);
? @"\x55\x4C\x89\xC1\x48\x89\xE5\x41\x57\x41\x56\x49\x89\xD6"
: @"\x48\x89\x5C\x24\x2A\x48\x89\x6C\x24\x2A\x48\x89\x74\x24\x2A\x57\x41\x56\x41\x57\x48\x83\xEC\x50\x4C\x8B\xB4\x24"
);

public static MemoryFunctionWithReturn<string, float, float, int> EmitSound = new(@"\x55\x48\x89\xe5\x41\x57\x41\x56\x41\x55\x41\x54\x53\x48\x81\xec\xa8\x00\x00\x00\x48\x85\xd2\x74\x08\x80\x3a\x00\x48\x89\xd3\x75\x1f\x49\xbc\x00\x00\x00\x00\xff\xff\xff\xff");
}
}

0 comments on commit c60c7d5

Please sign in to comment.