Skip to content

Instantly share code, notes, and snippets.

@vinci6k
Last active January 12, 2021 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinci6k/62906c2f570dfab1f9b647d3b613f020 to your computer and use it in GitHub Desktop.
Save vinci6k/62906c2f570dfab1f9b647d3b613f020 to your computer and use it in GitHub Desktop.
Control the rate at which aux (suit) power drains during certain activities in Half Life 2: Deathmatch with Source.Python.
# ../aux_drain/aux_drain.py
# Python
from enum import IntEnum
# Source.Python
from core import PLATFORM
from memory import Convention, DataType, find_binary
from memory.hooks import PreHook
from memory.manager import CustomType, TypeManager, Type
class Device(IntEnum):
"""Suit usage bits."""
SPRINT = 1
FLASHLIGHT = 2
BREATH = 4
# Dictionary used to set the rate at which aux drains for specific things.
# Default values: 25 for SPRINT, 6.7 for BREATH.
aux_drain_rate = {
Device.SPRINT: 0,
Device.BREATH: 0
}
server_binary = find_binary('server')
manager = TypeManager()
class SuitPowerDevice(CustomType, metaclass=manager):
"""Reconstructed 'CSuitPowerDevice' class."""
device_id = manager.instance_attribute(Type.INT, 0)
drain_rate = manager.instance_attribute(Type.FLOAT, 4)
if PLATFORM == 'windows':
identifier = b'\x55\x8B\xEC\x51\x53\x56\x8B\x75\x08\x8B\xD9\x57\x8D\xBB'
else:
identifier = '_ZN11CHL2_Player19SuitPower_AddDeviceERK16CSuitPowerDevice'
# More information about the function: https://git.io/JTUY8
SuitPower_AddDevice = server_binary[identifier].make_function(
Convention.THISCALL,
(DataType.POINTER, DataType.POINTER),
DataType.BOOL
)
@PreHook(SuitPower_AddDevice)
def suit_power_add_device_pre(stack_data):
"""Called when the player starts running or goes underwater."""
device = SuitPowerDevice._obj(stack_data[1])
try:
# Try to get the drain rate for this device.
drain_rate = aux_drain_rate[device.device_id]
except KeyError:
return
device.drain_rate = drain_rate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment