Last active
January 12, 2021 12:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ../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