Skip to content

Commit

Permalink
add sd cancel, move over to ontakedamage
Browse files Browse the repository at this point in the history
  • Loading branch information
destoer committed Nov 24, 2023
1 parent 61d350c commit 6d1fe7b
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 29 deletions.
36 changes: 32 additions & 4 deletions src/Jail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using CounterStrikeSharp.API.Modules.Utils;
using CounterStrikeSharp.API.Modules.Entities.Constants;
using CSTimer = CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;

// main plugin file, controls central hooking
// defers to warden, lr and sd
Expand Down Expand Up @@ -103,6 +104,7 @@ void register_commands()

// reg sd commands
AddCommand("sd","start and sd",sd.sd_cmd);
AddCommand("cancel_sd","cancel an sd",sd.cancel_sd_cmd);

// debug
if(Debug.enable)
Expand All @@ -128,13 +130,20 @@ void register_hook()
RegisterEventHandler<EventTeamchangePending>(OnSwitchTeam);
RegisterEventHandler<EventMapTransition>(OnMapChange);
RegisterEventHandler<EventPlayerDeath>(OnPlayerDeath);
RegisterEventHandler<EventPlayerHurt>(OnPlayerHurt);
RegisterEventHandler<EventItemEquip>(OnItemEquip);
RegisterEventHandler<EventGrenadeThrown>(OnGrenadeThrown);
RegisterEventHandler<EventPlayerHurt>(OnPlayerHurt);
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Hook(OnTakeDamage,HookMode.Pre);


// TODO: need to hook weapon drop
}

public override void Unload(bool hotReload)
{
VirtualFunctions.CBaseEntity_TakeDamageOldFunc.Unhook(OnTakeDamage,HookMode.Pre);
}

HookResult OnGrenadeThrown(EventGrenadeThrown @event, GameEventInfo info)
{
CCSPlayerController? player = @event.Userid;
Expand Down Expand Up @@ -172,14 +181,33 @@ HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)

if(player != null && player.is_valid())
{
lr.take_damage(player,attacker,damage,health,hitgroup);
warden.take_damage(player,attacker,damage,health);
sd.take_damage(player,attacker,damage,health,hitgroup);
lr.player_hurt(player,attacker,damage,health,hitgroup);
warden.player_hurt(player,attacker,damage,health);
sd.player_hurt(player,attacker,damage,health,hitgroup);
}

return HookResult.Continue;
}

HookResult OnTakeDamage(DynamicHook handle)
{
CEntityInstance victim = handle.GetParam<CEntityInstance>(0);
CTakeDamageInfo damage_info = handle.GetParam<CTakeDamageInfo>(1);

CHandle<CBaseEntity> dealer = damage_info.Attacker;

// get player and attacker
CCSPlayerController? player = victim.player();
CCSPlayerController? attacker = dealer.player();



sd.take_damage(player,attacker,ref damage_info.Damage);
lr.take_damage(player,attacker,ref damage_info.Damage);

return HookResult.Continue;
}

HookResult OnMapChange(EventMapTransition @event, GameEventInfo info)
{
warden.map_start();
Expand Down
4 changes: 1 addition & 3 deletions src/LastRequest/Dodgeball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ public override void init_player(CCSPlayerController player)
}
}

public override bool take_damage(int damage, int health, int hitgroup)
public override void player_hurt(int damage, int health, int hitgroup)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(player_slot);

if(player != null && player.is_valid_alive())
{
player.PlayerPawn.Value.CommitSuicide(true, true);
}

return true;
}

public override void grenade_thrown()
Expand Down
9 changes: 7 additions & 2 deletions src/LastRequest/HeadshotOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public override void init_player(CCSPlayerController player)
player.GiveNamedItem("weapon_deagle");
}

public override bool take_damage(int health,int damage, int hitgroup)
public override void player_hurt(int health,int damage, int hitgroup)
{
return hitgroup == Lib.HITGROUP_HEAD;
// dont allow damage when its not to head
if(hitgroup != Lib.HITGROUP_HEAD)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(player_slot);
Lib.restore_hp(player,damage,health);
}
}
}
9 changes: 7 additions & 2 deletions src/LastRequest/LRBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ public void activate()
public virtual void weapon_fire(String name) {}

public virtual void ent_created(CEntityInstance entity) {}

public virtual bool take_damage(int health,int damage, int hitgroup)
public virtual bool take_damage()
{
return !restrict_damage;
}

public virtual void player_hurt(int health,int damage, int hitgroup)
{

}

public virtual bool weapon_drop(String name)
{
return !restrict_drop;
Expand Down
38 changes: 31 additions & 7 deletions src/LastRequest/LastRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,29 @@ bool is_pair(CCSPlayerController? v1, CCSPlayerController? v2)
return lr1.slot == lr2.slot;
}

public void take_damage(CCSPlayerController? player, CCSPlayerController? attacker, int damage,int health, int hitgroup)


public void player_hurt(CCSPlayerController? player, CCSPlayerController? attacker, int damage,int health, int hitgroup)
{
// check no damage restrict
LRBase? lr = find_lr(player);

// no lr
if(lr == null)
{
return;
}

// not a pair
if(!is_pair(player,attacker))
{
return;
}

lr.player_hurt(damage,health,hitgroup);
}

public void take_damage(CCSPlayerController? player, CCSPlayerController? attacker, ref float damage)
{
// neither player is in lr we dont care
if(!in_lr(player) && !in_lr(attacker))
Expand All @@ -313,20 +335,22 @@ public void take_damage(CCSPlayerController? player, CCSPlayerController? attack
// not a pair restore hp
if(!is_pair(player,attacker))
{
Lib.restore_hp(player,damage,health);
damage = 0.0f;
return;
}

// check no damage restrict
LRBase? lr = find_lr(player);

if(lr != null)
if(lr == null)
{
if(!lr.take_damage(damage,health,hitgroup))
{
Lib.restore_hp(player,damage,health);
}
return;
}

if(!lr.take_damage())
{
damage = 0.0f;
}
}

public void weapon_equip(CCSPlayerController? player,String name)
Expand Down
48 changes: 48 additions & 0 deletions src/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ static public bool is_generic_admin(this CCSPlayerController? player)

static public void draw_laser(CCSPlayerController? player)
{
/*
CEnvBeam? laser = Utilities.CreateEntityByName<CEnvBeam>("env_beam");
CEnvBeam? end = Utilities.CreateEntityByName<CEnvBeam>("env_beam");
Expand All @@ -180,6 +181,53 @@ static public void draw_laser(CCSPlayerController? player)
laser.Teleport(player.PlayerPawn.Value.AbsOrigin, player.PlayerPawn.Value.AbsRotation, player.PlayerPawn.Value.AbsVelocity);
laser.DispatchSpawn();
*/
}

static public CCSPlayerController? player(this CEntityInstance? instance)
{
if(instance == null)
{
return null;
}

// grab the pawn index
CEntityIndex? ent_index = instance.EntityIndex;

if(ent_index == null)
{
return null;
}

int player_index = (int)ent_index.Value.Value;

// grab player controller from pawn
CCSPlayerPawn? player_pawn = Utilities.GetEntityFromIndex<CCSPlayerPawn>(player_index);

// pawn valid
if(player_pawn == null || !player_pawn.IsValid)
{
return null;
}

// controller valid
if(player_pawn.OriginalController == null || !player_pawn.OriginalController.IsValid)
{
return null;
}

// any further validity is up to the caller
return player_pawn.OriginalController.Value;
}

static public CCSPlayerController? player(this CHandle<CBaseEntity> handle)
{
if(handle.IsValid && handle.Value.IsValid)
{
return handle.Value.player();
}

return null;
}

static public void mute(this CCSPlayerController? player)
Expand Down
38 changes: 30 additions & 8 deletions src/SpecialDay/SpecialDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public void end_sd(bool forced = false)
JailPlugin.end_event();
active_sd.end_common();
active_sd = null;
}

// restore all players if from a cancel
if(forced)
{

}
// restore all players if from a cancel
if(forced)
{
Lib.announce(SPECIALDAY_PREFIX,"Special day cancelled");
}
}
}

public void round_end()
Expand All @@ -53,6 +53,12 @@ public void setup_sd(CCSPlayerController? player, ChatMenuOption option)
return;
}

if(active_sd != null)
{
player.announce(SPECIALDAY_PREFIX,"You cannot call two SD's at once");
return;
}

String name = option.Text;

switch(name)
Expand Down Expand Up @@ -111,7 +117,7 @@ public void start_sd()
}
}

public void take_damage(CCSPlayerController? player,CCSPlayerController? attacker, int damage,int health, int hitgroup)
public void take_damage(CCSPlayerController? player, CCSPlayerController? attacker, ref float damage)
{
if(active_sd == null || player == null || !player.is_valid())
{
Expand All @@ -120,8 +126,24 @@ public void take_damage(CCSPlayerController? player,CCSPlayerController? attacke

if(active_sd.restrict_damage)
{
Lib.restore_hp(player,damage,health);
damage = 0.0f;
}
}

public void player_hurt(CCSPlayerController? player,CCSPlayerController? attacker, int damage,int health, int hitgroup)
{
if(active_sd == null || player == null || !player.is_valid())
{
return;
}


}

[RequiresPermissions("@css/generic")]
public void cancel_sd_cmd(CCSPlayerController? player,CommandInfo command)
{
end_sd(true);
}

[RequiresPermissions("@css/generic")]
Expand Down
2 changes: 1 addition & 1 deletion src/Warden/JailPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void rebel_weapon_fire(CCSPlayerController? player, String weapon)
}
}

public void take_damage(CCSPlayerController? player, CCSPlayerController? attacker, int health, int damage)
public void player_hurt(CCSPlayerController? player, CCSPlayerController? attacker, int health, int damage)
{
if(player == null || attacker == null || !player.is_valid() || !attacker.is_valid())
{
Expand Down
4 changes: 2 additions & 2 deletions src/Warden/Warden.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,13 @@ public void death(CCSPlayerController? player, CCSPlayerController? killer)
}


public void take_damage(CCSPlayerController? player, CCSPlayerController? attacker, int damage,int health)
public void player_hurt(CCSPlayerController? player, CCSPlayerController? attacker, int damage,int health)
{
var jail_player = jail_player_from_player(player);

if(jail_player != null)
{
jail_player.take_damage(player,attacker,damage, health);
jail_player.player_hurt(player,attacker,damage, health);
}
}

Expand Down

0 comments on commit 6d1fe7b

Please sign in to comment.