[HL2:DM] Heartbeat

A place for requesting new Source.Python plugins to be made for your server.

Please request only one plugin per thread.
User avatar
Painkiller
Senior Member
Posts: 561
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

[HL2:DM] Heartbeat

Postby Painkiller » Mon Apr 13, 2020 8:59 am

Hello SourcePython Team and Community,

could somebody turn that into SP for me?


"es_heartbeat.txt"

Syntax: Select all

// ******************************
// Heartbeat V4.0 Beta
// for Valve Source Games.
// * Description:
// Can hear wounded players heartbeats.
// * Install instructions:
// <!-- m --><a class="postlink" href="http://forums.mattie.info/cs/forums/viewtopic.php?p=58954">http://forums.mattie.info/cs/forums/vie ... hp?p=58954</a><!-- m -->
// * Credits:
// Concept By: Murpher Written By: Venjax
// Edited Sounds by: Ace Rimmer - Thanks Bud!
// ENV_Blood code stolen from Einlazers - Thanks man!
// Special Thanks: MBchrono, Mattie!, Whore, & ljfsp
// * Requirements:
// EventScripts v1.2.1 or higher
// ******************************

block config
{
es_xsetinfo hbemit 1 // Set to 1 to hear others heartbeats. 0 to hear only your own.
es_xsetinfo slowhealth 60 // Set this variable for first beat
es_xsetinfo fasthealth 15 // Set this variable for second beat
es_xsetinfo hb_bleed 1 // Set to 1 to bleed during the fast heartbeat.
es_xsetinfo hb_bleedout 1 // Set this to amount of damage to lose after player starts to bleed.
}

// DO NOT EDIT BELOW THIS LINE DOING SO WILL VOID WARRANTY AND TECHNICAL SUPPORT. Smile

block load
{
es_xsetinfo mygame 0
es_getgame mygame
es_xsetinfo gmeinfo "CCSPlayer.baseclass.m_iHealth"
if ("Defeat" in server_var(mygame)) then es_xsetinfo gmeinfo "CDODPlayer.baseclass.m_iHealth"
if ("Deathmatch" in server_var(mygame)) then es_xsetinfo gmeinfo "CHL2MP_Player.baseclass.baseclass.m_iHealth"
es_xsetinfo myhealth 0
es_xsetinfo togvar 0
es_xsetinfo togvar2 0
es_xsetinfo stmid 0
es_xsetinfo ven_hb_ver "v4.0"
es_xmakepublic ven_hb_ver
es_xsetinfo hbexists 0
es_xsetinfo hb_shadox 0
es_xexists hbexists keygroup hbtoggle
if (server_var(hbexists) equalto 1) then es_xkeygroupdelete hbtoggle
es_xkeygroupload hbtoggle |heartbeat
es_xexists hbexists keygroup hbeaters
if (server_var(hbexists) equalto 1) then es_xkeygroupdelete hbeaters
es_xkeygroupcreate hbeaters
es_xloadevents addons/mattie_eventscripts.res
es_xdoblock heartbeat/config
if (server_var(hb_bleed) equalto 1) do
{
es_regcmd _hb_dobleed heartbeat/dobleed "internal command to add blood to heartbeat"
}
else do
{
es_regcmd _hb_dobleed heartbeat/nobleed "internal command for no blood"
}
}

event es_map_start
{
downloadable sound/hb/fastbeat.mp3
downloadable sound/hb/slowbeat.mp3
es_xdelayed 10 es_xdoblock heartbeat/beatstart
es_xkeygroupdelete hbtoggle
es_xkeygroupload hbtoggle |heartbeat
}

event round_start
{
es_xforeachkey hb_users in hbeaters "es_xdoblock heartbeat/shadox"
}

event player_spawn
{
es es_xexists hbexists key hbeaters event_var(userid)
if (server_var(hbexists) equalto 0) do
{
es es_xexists togvar2 key hbtoggle event_var(es_steamid)
if (server_var(togvar2) equalto 0) do
{
es es_xkeycreate hbeaters event_var(userid)
es es_xkeysetvalue hbeaters event_var(userid) hb 1
}
}
}

event player_death
{
es es_xexists hbexists key hbeaters event_var(userid)
if (server_var(hbexists) equalto 1) then es_xkeydelete hbeaters event_var(userid)
}

event player_disconnect
{
es es_xexists hbexists key hbeaters event_var(userid)
if (server_var(hbexists) equalto 1) then es_xkeydelete hbeaters event_var(userid)
}

block beatstart
{
es_xforeachkey hb_users in hbeaters "es_xdoblock heartbeat/beat"
es_xdelayed 1.1 es_xdoblock heartbeat/beatstart
}

block beat
{
es es_xgetplayerprop myhealth server_var(hb_users) server_var(gmeinfo)
if (server_var(myhealth) <= server_var(slowhealth)) do
{
if (server_var(myhealth) > server_var(fasthealth)) do
{
if (server_var(hbemit) equalto 1) do
{
es_emitsound player server_var(hb_users) hb/slowbeat.mp3 1.0 1.0
}
else do
{
es es_xcexec server_var(hb_users) playgamesound hb/slowbeat.mp3
}
}
else do
{
if (server_var(myhealth) > 1) do
{
if (server_var(hbemit) equalto 1) do
{
es_emitsound player server_var(hb_users) hb/fastbeat.mp3 1.0 1.0
_hb_dobleed
}
else do
{
es es_xcexec server_var(hb_users) playgamesound hb/fastbeat.mp3
_hb_dobleed
}
}
}
}
}

event player_say
{
if (event_var(text) equalto "!heartbeat") do
{
es es_xexists togvar key hbtoggle event_var(es_steamid)
if (server_var(togvar) equalto 0) do
{
es es_xtell event_var(userid) [Heartbeat] You will not hear your own heartbeat.
if (server_var(hbemit) notequalto 0) do
{
es es_xtell event_var(userid) [Heartbeat] You WILL still hear other's heartbeat.
}
es es_xkeycreate hbtoggle event_var(es_steamid)
es es_xkeysetvalue hbtoggle event_var(es_steamid) nobeat 1
es es_xexists hbexists key hbeaters event_var(userid)
if (server_var(hbexists) equalto 1) then es_xkeydelete hbeaters event_var(userid)
}
else do
{
es es_xtell event_var(userid) [Heartbeat] You will now hear your own heartbeat again.
es es_xkeydelete hbtoggle event_var(es_steamid)
es es_xkeycreate hbeaters event_var(userid)
es es_xkeysetvalue hbeaters event_var(userid) hb 1
}
es_xkeygroupsave hbtoggle |heartbeat
}
}

event unload
{
es_xkeygroupsave hbtoggle |heartbeat
es_xkeygroupdelete hbtoggle
es_xkeygroupdelete hbeaters
}

block dobleed
{
es es_xfire server_var(hb_users) env_blood kill
es es_xgive server_var(hb_users) env_blood
es es_xfire server_var(hb_users) env_blood addoutput "spawnflags 13"
es es_xfire server_var(hb_users) env_blood addoutput "amount 10000"
es es_xfire server_var(hb_users) env_blood EmitBlood
if (server_var(hb_bleedout) greaterthan 0) do
{
es es_xmath myhealth - server_var(hb_bleedout)
es es_xsetplayerprop server_var(hb_users) server_var(gmeinfo) server_var(myhealth)
}
}

block nobleed
{
//Nothing here!
}

block shadox
{
es es_xgetuserid hb_shadox server_var(hb_users)
if (server_var(hb_shadox) equalto 0) then es_xkeydelete hbeaters server_var(hb_users)
}



"es_hbtoggle_db.txt"

Code: Select all

"hbtoggle"
{
   "STEAM_0:1:13527267"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:12379922"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:5062330"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:5031723"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:73619277"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:11412613"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:5065710"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:12887093"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:33597831"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:80209958"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:22541232"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:18699109"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:39824176"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:14786612"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:8063502"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:29324209"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:5238382"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:37116066"
   {
      "nobeat"      "1"
   }
   "STEAM_0:1:16245200"
   {
      "nobeat"      "1"
   }
   "STEAM_0:0:22011549"
   {
      "nobeat"      "1"
   }
   "[U:1:35751098]"
   {
      "nobeat"      "1"
   }
   "[U:1:185003008]"
   {
      "nobeat"      "1"
   }
   "[U:1:395801539]"
   {
      "nobeat"      "1"
   }
   "[U:1:63022354]"
   {
      "nobeat"      "1"
   }
   "[U:1:155082594]"
   {
      "nobeat"      "1"
   }
   "[U:1:190107688]"
   {
      "nobeat"      "1"
   }
   "[U:1:79614141]"
   {
      "nobeat"      "1"
   }
   "[U:1:201432531]"
   {
      "nobeat"      "1"
   }
   "[U:1:851189843]"
   {
      "nobeat"      "1"
   }
   "[U:1:228203257]"
   {
      "nobeat"      "1"
   }
}


Thanks in Advance
User avatar
Painkiller
Senior Member
Posts: 561
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Heartbeat

Postby Painkiller » Wed May 13, 2020 6:46 am

Is there already an attempt to do this?
User avatar
Painkiller
Senior Member
Posts: 561
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Heartbeat

Postby Painkiller » Mon May 18, 2020 7:03 am

Hello, who would be so kind and rewrite this plugin?
User avatar
VinciT
Senior Member
Posts: 175
Joined: Thu Dec 18, 2014 2:41 am

Re: [HL2:DM] Heartbeat

Postby VinciT » Fri Jun 12, 2020 9:01 pm

Sorry it took so long, give this a try:

Syntax: Select all

# ../heartbeat/heartbeat.py
# Heartbeat SFX provided by MrSnooze.

# Python
import json

# Source.Python
from commands import CommandReturn
from commands.say import SayCommand
from engines.sound import Sound
from entities.entity import Entity
from events import Event
from filters.recipients import RecipientFilter
from listeners import OnClientDisconnect
from listeners.tick import GameThread, RepeatStatus, Delay
from mathlib import Vector
from messages import SayText2
from paths import PLUGIN_DATA_PATH
from players.dictionary import PlayerDictionary
from players.entity import Player
from stringtables.downloads import Downloadables


# Emit the heartbeat sound to other players? (0 - no, 1 - yes)
EMIT = 1
# Health below which the slow heartbeat starts.
HEALTH_SLOW = 60
# Health at and below which the player's heartbeat will become fast.
HEALTH_FAST = 15
# Should the player bleed while their heartbeat is fast?
BLEED = 1
# Amount of damage caused by bleeding.
BLEED_DAMAGE = 1


downloads = Downloadables()
# Make sure the players download the heartbeat sounds.
downloads.add_directory('sound/hb')


SOUND_SLOW = Sound('hb/slowbeat.wav', attenuation=2.2)
SOUND_FAST = Sound('hb/fastbeat.wav', attenuation=2.2)


CHAT_MESSAGE = {
False: SayText2(
'\x071af716[Heartbeat] You will now hear your own heartbeat again.'),
True: SayText2(
'\x07fd2b01[Heartbeat] You will not hear your own heartbeat.' +
('\n\x071af716[Heartbeat] You will still hear other\'s heartbeat.'
if EMIT else '')
)
}


class Database:
"""Simple class used for loading and saving player data.

Args:
path (str): Path to the file from which to load player data or save it.
"""

def __init__(self, path):
self.path = path
self.player_data = set()
self.save_queued = False

# Attempt to load player data.
try:
with open(self.path, 'r') as file:
self.player_data = set(json.load(file)['disabled'])
except FileNotFoundError:
pass

def queue_save(self):
"""Saves the player data after a short delay. This is done to avoid
constant saving of the file to disk."""
# Has the saving of data already been queued?
if self.save_queued:
# No need to go further.
return

self.save_queued = True
Delay(60, self.save)

def save(self):
"""Starts a new GameThread in order to avoid freezing the server and
saves the data to a file."""
thread = GameThread(target=self._save())
thread.start()

def _save(self):
prepared_data = {}
# Convert the set to a dictionary.
prepared_data['disabled'] = list(self.player_data)

with open(self.path, 'w', encoding='utf-8') as file:
# Save the dictionary to a file.
json.dump(prepared_data, file, indent=4)

self.save_queued = False


db = Database(PLUGIN_DATA_PATH / 'heartbeat' / 'player_data.json')


class PlayerHB(Player):
"""Modified Player class.

Args:
index (int): A valid player index.
caching (bool): Check for a cached instance?

Attributes:
beat_think (Repeat): Instance of Repeat() used for looping the
`_beat_think()` function.
blood (Entity): Instance of the 'env_blood' entity.
muted_heartbeat (bool): Has the player decided to mute their own
heartbeat for themselves?
"""

def __init__(self, index, caching=True):
"""Initializes the object."""
super().__init__(index, caching)

self.beat_think = self.repeat(self._beat_think)
self.blood = None
self.muted_heartbeat = (
True if self.steamid in db.player_data else False)

def enable_heartbeat(self):
"""Starts looping the `_beat_think()' function."""
if self.beat_think.status is not RepeatStatus.RUNNING:
# Is the player missing an 'env_blood' entity?
if self.blood is None:
# Create and store one for future use.
self.blood = Entity.create('env_blood')
# How far from the player should the decals be applied to walls
# and props?
self.blood.set_key_value_int('amount', 150)
# Set certain spawn flags for the entity.
# 1: Random Direction
# 4: On Player
# 8: Spray decals
self.blood.spawn_flags = 1 + 4 + 8
self.blood.spawn()

self.beat_think.start(interval=1.1, execute_on_start=True)

def _beat_think(self):
# Has the player healed above the heartbeat threshold?
if self.health > HEALTH_SLOW:
# Let's stop the loop.
self.beat_think.stop()

# Get a list of players that should hear the heartbeat.
recipients = list(RecipientFilter()) if EMIT else [self.index,]

# Does the player not want to hear their own heartbeat?
if self.muted_heartbeat:
recipients.remove(self.index)

sound = SOUND_SLOW
# Should the heartbeat be fast?
if self.health <= HEALTH_FAST:
sound = SOUND_FAST

# Is bleeding enabled? (BLEED set to 1)
if BLEED:
self.bleed()

sound.play(recipients)

def bleed(self):
"""Creates a blood sprite and decal at the player's current origin."""
# Adjust the 'env_blood' origin according to player's current height.
self.blood.origin = self.origin + Vector(0, 0, self.maxs.z * 0.65)
self.blood.call_input('EmitBlood')

# Should bleeding cause damage?
if BLEED_DAMAGE:
self.take_damage(BLEED_DAMAGE)

def on_death(self):
"""Called when the player dies."""
self.beat_think.stop()

try:
self.blood.remove()
except AttributeError:
pass

self.blood = None

def on_disconnect(self):
"""Called when the player leaves the server."""
try:
self.blood.remove()
except AttributeError:
pass


player_instances = PlayerDictionary(PlayerHB)


@Event('player_hurt')
def player_hurt(event):
if event['health'] <= HEALTH_SLOW:
player_instances.from_userid(event['userid']).enable_heartbeat()


@Event('player_death')
def player_death(event):
player_instances.from_userid(event['userid']).on_death()


@OnClientDisconnect
def player_disconnect(index):
player_instances[index].on_disconnect()


@SayCommand(['!heartbeat', '/heartbeat', '!hb', '/hb'])
def heartbeat_cmd(command, index, team_only=False):
player = player_instances[index]
# Toggle the player's own heartbeat.
player.muted_heartbeat = not player.muted_heartbeat

# Attempt to remove the player's steamid from the set.
try:
db.player_data.remove(player.steamid)
except KeyError:
# The steamid wasn't found - add it to the set.
db.player_data.add(player.steamid)

# Send the player the correct chat message.
CHAT_MESSAGE[player.muted_heartbeat].send(index)
# Queue the database to save the set to a file.
db.queue_save()
# Block the player's message from appearing. (e.g. player_name: !heartbeat)
return CommandReturn.BLOCK
Attachments
heartbeat_additional_files.zip
(251.44 KiB) Downloaded 13 times
ImageImageImageImageImage
User avatar
Painkiller
Senior Member
Posts: 561
Joined: Sun Mar 01, 2015 8:09 am
Location: Germany
Contact:

Re: [HL2:DM] Heartbeat

Postby Painkiller » Sun Jun 28, 2020 7:32 am

Hello, I just saw it a minute ago. I wasn't shown the post.

Sorry for the late notice.

I've tried it now. The bleeding works so far but I can't hear the sounds.
There are also no mistakes.
Is an automatic downloader built into the plugin ?

Return to “Plugin Requests”

Who is online

Users browsing this forum: No registered users and 1 guest