-
-
Save adamcunnington/55fcecaf314c3da1631b to your computer and use it in GitHub Desktop.
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
# <path to game directory>/addons/eventscripts/_engines/python/Lib/esutils/ | |
# tools.py | |
# by Adam Cunnington | |
"""Provide pythonic, intuitive helper functions (and wrappers) to be commonly | |
used by both libraries and modules. | |
""" | |
import itertools | |
import operator | |
import es | |
__all__ = ( | |
"GAME_DIR", | |
"ToolsError", | |
"Filter", | |
"format_verbose_name", | |
"get_colour", | |
"set_colour", | |
) | |
GAME_DIR = es.ServerVar("eventscripts_gamedir") | |
class ToolsError(Exception): | |
"""General error encountered relating to esutils.tools""" | |
class Filter(object): | |
"""Create a filter object that can be further filtered by the object's | |
attribute values. | |
""" | |
def __init__(self, original_objects, current_objects=None, attribute=""): | |
"""Instantiate a Filter object. | |
Arguments: | |
original_objects - the original set of objects which all other related | |
filter objects are filtered from. | |
current_objects (Keyword Default: None) - the current set of objects | |
that have not been filtered out. | |
attribute (Keyword Default: "") - the name of the attribute for | |
arithmetical / logical comparison operators to use. | |
""" | |
self._original_objects = set(original_objects) | |
if current_objects is None: | |
self._current_objects = original_objects | |
else: | |
self._current_objects = current_objects | |
self._attribute = attribute | |
def __contains__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.contains, value), | |
self._attribute) | |
def __eq__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.eq, value), self._attribute) | |
def __ge__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.ge, value), self._attribute) | |
def __getattr__(self, name): | |
return Filter(self._original_objects, self._current_objects, name) | |
def __gt__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.gt, value), self._attribute) | |
def __inv__(self): | |
return Filter(self._original_objects, self._original_objects - | |
self._current_objects, self._attribute) | |
def __iter__(self): | |
return self._current_objects.__iter__() | |
def __le__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.le, value), self._attribute) | |
def __lt__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.lt, value), self._attribute) | |
def __ne__(self, value): | |
return Filter(self._original_objects, | |
self._get_objects(operator.ne, value), self._attribute) | |
def _get_objects(self, operation, value): | |
for current_object in self._current_objects: | |
if operation(getattr(current_object, self._attribute), value): | |
yield current_object | |
def filter(self, *args): | |
"""Return a new filter object based on several comparison operations. | |
Arguments: | |
args - the comparisons to determine which objects should be filtered | |
out in order to leave a set of objects for the new filter object. | |
""" | |
return Filter(self._original_objects, set(itertools.chain(*args))) | |
def format_verbose_name(basename): | |
"""Format verbose name from basename by replacing underscores with spaces | |
and then titling. Return the result. | |
Arguments: | |
basename - the basename to format the verbose name from. | |
""" | |
return basename.replace("_", " ").title() | |
def get_colour(index): | |
"""Calculate and return a 4 part tuple (r, b, g, a) representing the | |
user's colour. | |
Arguments: | |
index - the player's index. | |
""" | |
colour = es.getindexprop(index, "CBaseEntity.m_clrRender") | |
return (colour & 0xff, (colour & 0xff00) >> 8, | |
(colour & 0xff0000) >> 16, (colour & 0xff000000) >> 24) | |
def set_colour(index, red, blue, green, alpha=255): | |
"""Set a player's model colour. | |
Arguments: | |
index - the player's index. | |
value - a 4 part tuple (r, b, g, a) representing the colour to set the | |
user to. | |
""" | |
es.setindexprop(index, "CBaseEntity.m_nRenderMode", | |
es.getindexprop(index, "CBaseEntity.m_nRenderMode") | 1) | |
es.setindexprop(index, "CBaseEntity.m_nRenderFX", | |
es.getindexprop(index, "CBaseEntity.m_nRenderFX") | 256) | |
colour = red + (green << 8) + (blue << 16) + (alpha << 24) | |
if colour >= 2 ** 31: | |
colour -= 2 ** 32 | |
es.setindexprop(index, "CBaseEntity.m_clrRender", colour) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment