Skip to content

Commit d739f6e

Browse files
committed
Entity transmission alternative.
1 parent a7bff6a commit d739f6e

File tree

5 files changed

+1662
-444
lines changed

5 files changed

+1662
-444
lines changed

addons/source-python/packages/source-python/entities/transmit.py

100644100755
Lines changed: 2 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -23,154 +23,18 @@
2323
# ============================================================================
2424
# Source.Python
2525
# Entities
26-
from _entities._transmit import BaseTransmitCriteria
27-
from _entities._transmit import BaseTransmitFilter
28-
from _entities._transmit import TransmitManager
29-
from _entities._transmit import TransmitStates
30-
from _entities._transmit import TransmitTarget
31-
from _entities._transmit import TransmitType
3226
from _entities._transmit import transmit_manager
3327

3428

3529
# ============================================================================
3630
# >> ALL DECLARATION
3731
# ============================================================================
38-
__all__ = [
39-
'BaseTransmitCriteria',
40-
'BaseTransmitFilter',
41-
'TransmitCriteria',
42-
'TransmitFilter',
43-
'TransmitManager',
44-
'TransmitStates',
45-
'TransmitTarget',
46-
'TransmitType',
32+
__all__ = (
4733
'transmit_manager',
48-
]
34+
)
4935

5036

5137
# ============================================================================
5238
# >> CLASSES
5339
# ============================================================================
54-
class TransmitFilter(AutoUnload, BaseTransmitFilter):
55-
"""Decorator class used to register entity transmission filter."""
5640

57-
def __call__(self, callback):
58-
"""Register the given callback and initialize the filter.
59-
60-
:param function callback:
61-
The callback to register.
62-
"""
63-
self.callback = callback
64-
self.initialize()
65-
return self
66-
67-
def initialize(self):
68-
"""Initializes the filter."""
69-
transmit_manager.register_filter(self)
70-
71-
def _unload_instance(self):
72-
"""Unregister ourself as filter."""
73-
transmit_manager.unregister_filter(self)
74-
75-
76-
class TransmitCriteria(AutoUnload, BaseTransmitCriteria):
77-
"""Class used to narrow down entity transmission filtering."""
78-
79-
def __init__(self, conditions, target=TransmitTarget.ENTITY):
80-
"""Initialize the criteria.
81-
82-
:param function/iterable conditions:
83-
Conditions that an entity have to meet in order to match this
84-
criteria.
85-
:param TransmitTarget target:
86-
Whether the criteria is to be checked against the entity being
87-
transmitted or the receiving player.
88-
"""
89-
super().__init__(target)
90-
91-
# Validate and store the given conditions
92-
if callable(conditions):
93-
self.conditions = (conditions,)
94-
else:
95-
for condition in conditions:
96-
if not callable(condition):
97-
raise ValueError(f'"{condition}" is not callable.')
98-
self.conditions = conditions
99-
100-
# Let's test existing entities
101-
for entity in EntityIter():
102-
self[entity.index] = self.test_entity(entity)
103-
104-
# Register our internal listeners
105-
on_entity_created_listener_manager.register_listener(
106-
self._on_entity_created)
107-
on_entity_deleted_listener_manager.register_listener(
108-
self._on_entity_deleted)
109-
110-
def test_entity(self, entity):
111-
"""Test whether the entity matches all the conditions.
112-
113-
:param Entity entity::
114-
The entity to test against all conditions.
115-
116-
:rtype:
117-
bool
118-
"""
119-
for condition in self.conditions:
120-
if not condition(entity):
121-
return False
122-
123-
return True
124-
125-
def _on_entity_created(self, base_entity):
126-
"""Called when an entity is created.
127-
128-
:param BaseEntity base_entity:
129-
The entity that was created.
130-
"""
131-
# Try to grab an index or exit if the entity isn't networked
132-
try:
133-
entity = Entity(base_entity.index)
134-
except ValueError:
135-
return
136-
137-
# Test the entity and set the result
138-
self[entity.index] = self.test_entity(entity)
139-
140-
def _on_entity_deleted(self, base_entity):
141-
"""Called when an entity is being deleted.
142-
143-
:param BaseEntity base_entity:
144-
The entity that is being deleted.
145-
"""
146-
# Try to grab an index or exit if the entity isn't networked
147-
try:
148-
entity_index = base_entity.index
149-
except ValueError:
150-
return
151-
152-
# Mark the entity as no longer matching
153-
del self[entity_index]
154-
155-
def _unload_instance(self):
156-
"""Unregister our internal listeners."""
157-
self.listener.unregister_listener(self._on_entity_created)
158-
on_entity_deleted_listener_manager.unregister_listener(
159-
self._on_entity_deleted)
160-
161-
162-
# For convenience, register some commonly used criterias
163-
TransmitCriteria.is_player = TransmitCriteria(
164-
EntityCondition.is_player)
165-
TransmitCriteria.is_not_player = TransmitCriteria(
166-
EntityCondition.is_not_player)
167-
TransmitCriteria.is_human_player = TransmitCriteria(
168-
EntityCondition.is_human_player)
169-
TransmitCriteria.is_bot_player = TransmitCriteria(
170-
EntityCondition.is_bot_player)
171-
TransmitCriteria.equals_entity_classname = (
172-
lambda *classnames: TransmitCriteria(
173-
EntityCondition.equals_entity_classname(*classnames)))
174-
TransmitCriteria.equals_entity_classname = (
175-
lambda *classnames: TransmitCriteria(
176-
EntityCondition.equals_entity_classname(*classnames)))

0 commit comments

Comments
 (0)