Skip to content

Adds entity collisions functionalities. #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c6cbbd4
Added OnEntityCollision listener (#442).
jordanbriere Nov 28, 2021
0612bb2
Fixed some potential scoping issues when the entity is invalid.
jordanbriere Nov 28, 2021
034427a
Fixed various issues and added CollisionHash support.
jordanbriere Nov 29, 2021
9315171
Renamed CollisionHash.is_pair_in_hash to CollisionHash.has_pair.
jordanbriere Nov 29, 2021
9309701
Removed a redundant call.
jordanbriere Nov 29, 2021
07b8348
Various fixes and improvements.
jordanbriere Nov 30, 2021
ead0203
Specialized contents exclusion for CS:GO.
jordanbriere Nov 30, 2021
20319fe
Fixed collision detection of some projectiles on CS:GO.
jordanbriere Nov 30, 2021
cacbbd8
Ignores grate textures on CS:GO, unless they are traced against players.
jordanbriere Dec 1, 2021
2ffa2c2
Added ICollisionHash.
jordanbriere Dec 1, 2021
0348127
Removed some redundant code.
jordanbriere Dec 1, 2021
9bce139
Make sure entities added to collision hashes are valid, and cleaned u…
jordanbriere Dec 1, 2021
62bad57
Improved RTTI lookup on Linux.
jordanbriere Dec 1, 2021
268cfec
Added caching.
jordanbriere Dec 1, 2021
9c873df
Added collision hooks.
jordanbriere Dec 1, 2021
702d9ac
Some fixes and improvements.
jordanbriere Dec 2, 2021
62730cf
Added OnPlayerCollision.
jordanbriere Dec 3, 2021
fa85a5a
Solid masks are now resolved dynamically.
jordanbriere Dec 3, 2021
83aab51
Minor fixes and improvements.
jordanbriere Dec 5, 2021
7943b46
Fixed wrong offsets for BM:S (introduced into https://github.com/Sour…
jordanbriere Dec 5, 2021
a1f2d70
Updated the interface.
jordanbriere Dec 6, 2021
f12d4cb
Added CollisionSet.
jordanbriere Dec 7, 2021
dc08465
Added CollisionMap.
jordanbriere Dec 7, 2021
f04ba19
Updated CollisionHash.
jordanbriere Dec 9, 2021
07d4ab4
Minor fixes.
jordanbriere Dec 10, 2021
efd45a1
Added rules negation support.
jordanbriere Dec 11, 2021
0d58d99
Merge remote-tracking branch 'remotes/origin/master' into entities_co…
jordanbriere Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
offset_windows = 109
arguments = POINTER

# _ZNK11CBasePlayer25PhysicsSolidMaskForEntityEv
[[get_solid_mask]]
offset_linux = 180
offset_windows = 179
return_type = UINT


[input]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ srv_check = False
offset_linux = 105
offset_windows = 104
arguments = POINTER

# _ZNK11CBasePlayer25PhysicsSolidMaskForEntityEv
# https://gist.github.com/jordanbriere/679e6d09ebaa9830979b6f60e61d4b9c
[[get_solid_mask]]
offset_linux = 169
offset_windows = 168
return_type = UINT
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@
offset_linux = 103
offset_windows = 102
arguments = POINTER

# _ZNK11CBasePlayer25PhysicsSolidMaskForEntityEv
[[get_solid_mask]]
offset_linux = 173
offset_windows = 172
return_type = UINT
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@
offset_linux = 110
offset_windows = 109
arguments = POINTER

# _ZNK11CBasePlayer25PhysicsSolidMaskForEntityEv
[[get_solid_mask]]
offset_linux = 178
offset_windows = 177
return_type = UINT
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
offset_windows = 100
arguments = POINTER

# _ZNK11CBasePlayer25PhysicsSolidMaskForEntityEv
[[get_solid_mask]]
offset_linux = 168
offset_windows = 167
return_type = UINT


[input]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,27 @@ Called when a networked entity has been spawned.
pass


OnEntityCollision
-----------------

Called when a non-player entity is about to collide with another entity.

.. note::

This listener can be extremely noisy. Use :class:`entities.collisions.CollisionHash`,
:class:`entities.collisions.CollisionMap`, or :class:`entities.collisions.CollisionSet`
if you don't have dynamic conditions to test for.

.. code-block:: python

from listeners import OnEntityCollision

@OnEntityCollision
def on_entity_collision(entity, other):
# Disable weapons/projectiles collisions with everything except players
return not (entity.is_weapon() and not other.is_player())


OnLevelInit
-----------

Expand Down Expand Up @@ -406,6 +427,29 @@ Called when the button state of a player changed.
button or button combination has been pressed or released.


OnPlayerCollision
-----------------

Called when a player is about to collide with an entity.

.. note::

This listener can be extremely noisy. Use :class:`entities.collisions.CollisionHash`,
:class:`entities.collisions.CollisionMap`, or :class:`entities.collisions.CollisionSet`
if you don't have dynamic conditions to test for.

.. code-block:: python

from listeners import OnPlayerCollision

@OnPlayerCollision
def on_player_collision(player, entity):
# Disable teammates collisions
if not entity.is_player():
return
return player.team_index != entity.team_index


OnPlayerRunCommand
--------------------

Expand Down
64 changes: 64 additions & 0 deletions addons/source-python/packages/source-python/entities/collisions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# ../entities/collisions.py

"""Provides entity collisions functionality."""

# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
# Core
from core import AutoUnload
from core import WeakAutoUnload


# =============================================================================
# >> FORWARD IMPORTS
# =============================================================================
# Source.Python Imports
# Entities
from _entities._collisions import CollisionHash
from _entities._collisions import CollisionManager
from _entities._collisions import CollisionMap
from _entities._collisions import CollisionMode
from _entities._collisions import CollisionRules
from _entities._collisions import CollisionSet
from _entities._collisions import collision_manager


# =============================================================================
# >> ALL DECLARATION
# =============================================================================
__all__ = [
'CollisionHash',
'CollisionHook',
'CollisionManager',
'CollisionMap',
'CollisionMode',
'CollisionRules',
'CollisionSet',
'collision_manager',
]


# =============================================================================
# >> INITIALIZATION
# =============================================================================
# Inject WeakAutoUnload into CollisionRules's hierarchy.
if not issubclass(CollisionRules, WeakAutoUnload):
CollisionRules.__bases__ = (WeakAutoUnload,) + CollisionRules.__bases__


# =============================================================================
# >> CLASSES
# =============================================================================
class CollisionHook(AutoUnload):
"""Decorator used to create collision hooks that auto unload."""

def __init__(self, callback):
"""Registers the collision hook."""
self.callback = callback
collision_manager.register_hook(callback)

def _unload_instance(self):
"""Unregisters the collision hook."""
collision_manager.unregister_hook(self.callback)
18 changes: 18 additions & 0 deletions addons/source-python/packages/source-python/listeners/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@
from _listeners import on_networked_entity_spawned_listener_manager
from _listeners import on_entity_deleted_listener_manager
from _listeners import on_networked_entity_deleted_listener_manager
from _listeners import on_entity_collision_listener_manager
from _listeners import on_data_loaded_listener_manager
from _listeners import on_combiner_pre_cache_listener_manager
from _listeners import on_data_unloaded_listener_manager
from _listeners import on_query_cvar_value_finished_listener_manager
from _listeners import on_server_activate_listener_manager
from _listeners import on_tick_listener_manager
from _listeners import on_server_output_listener_manager
from _listeners import on_player_collision_listener_manager
from _listeners import on_player_run_command_listener_manager
from _listeners import on_player_post_run_command_listener_manager
from _listeners import on_button_state_changed_listener_manager
Expand Down Expand Up @@ -99,6 +101,7 @@
'OnNetworkedEntityCreated',
'OnEntityDeleted',
'OnNetworkedEntityDeleted',
'OnEntityCollision',
'OnEntityOutput',
'OnEntityOutputListenerManager',
'OnEntityPreSpawned',
Expand All @@ -110,6 +113,7 @@
'OnLevelEnd',
'OnNetworkidValidated',
'OnButtonStateChanged',
'OnPlayerCollision',
'OnPlayerRunCommand',
'OnPlayerPostRunCommand',
'OnPluginLoaded',
Expand Down Expand Up @@ -138,6 +142,7 @@
'on_networked_entity_created_listener_manager',
'on_entity_deleted_listener_manager',
'on_networked_entity_deleted_listener_manager',
'on_entity_collision_listener_manager',
'on_entity_output_listener_manager',
'on_entity_pre_spawned_listener_manager',
'on_networked_entity_pre_spawned_listener_manager',
Expand All @@ -156,6 +161,7 @@
'on_tick_listener_manager',
'on_version_update_listener_manager',
'on_server_output_listener_manager',
'on_player_collision_listener_manager',
'on_player_run_command_listener_manager',
'on_button_state_changed_listener_manager',
)
Expand Down Expand Up @@ -410,6 +416,12 @@ class OnNetworkedEntityDeleted(ListenerManagerDecorator):
manager = on_networked_entity_deleted_listener_manager


class OnEntityCollision(ListenerManagerDecorator):
"""Register/unregister a OnEntityCollision listener."""

manager = on_entity_collision_listener_manager


class OnDataLoaded(ListenerManagerDecorator):
"""Register/unregister a OnDataLoaded listener."""

Expand Down Expand Up @@ -491,6 +503,12 @@ class OnLevelEnd(ListenerManagerDecorator):
_level_initialized = False


class OnPlayerCollision(ListenerManagerDecorator):
"""Register/unregister a OnPlayerCollision listener."""

manager = on_player_collision_listener_manager


class OnPlayerRunCommand(ListenerManagerDecorator):
"""Register/unregister a run command listener."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# Configobj
from configobj import ConfigObj
from configobj import Section
# GC
from gc import collect
# Importlib
from importlib.util import find_spec
from importlib.util import spec_from_file_location
Expand Down Expand Up @@ -234,6 +236,7 @@ def unload(self, plugin_name):

self._remove_modules(plugin_name)
del self[plugin_name]
collect()
on_plugin_unloaded_manager.notify(plugin)

def reload(self, plugin_name):
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Set(SOURCEPYTHON_ENTITIES_MODULE_HEADERS
core/modules/entities/${SOURCE_ENGINE}/entities_props_wrap.h
core/modules/entities/${SOURCE_ENGINE}/entities_constants_wrap.h
core/modules/entities/entities_entity.h
core/modules/entities/entities_collisions.h
)

Set(SOURCEPYTHON_ENTITIES_MODULE_SOURCES
Expand All @@ -241,6 +242,8 @@ Set(SOURCEPYTHON_ENTITIES_MODULE_SOURCES
core/modules/entities/entities_props_wrap.cpp
core/modules/entities/entities_entity.cpp
core/modules/entities/entities_entity_wrap.cpp
core/modules/entities/entities_collisions.cpp
core/modules/entities/entities_collisions_wrap.cpp
)

# ------------------------------------------------------------------
Expand Down
Loading