/
_base.py
184 lines (152 loc) · 6.04 KB
/
_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# ../weapons/_base.py
"""Provides simplified weapon functionality."""
# =============================================================================
# >> IMPORTS
# =============================================================================
# Source.Python Imports
# Entities
from entities.entity import Entity
# Weapons
from _weapons._entity import WeaponMixin
from weapons.manager import weapon_manager
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
# Add all the global variables to __all__
__all__ = ('Weapon',
)
# =============================================================================
# >> CLASSES
# =============================================================================
class Weapon(WeaponMixin, Entity):
"""Allows easy usage of the weapon's attributes."""
def __init__(self, index, caching=True):
"""Initialize the object.
:param int index:
A valid weapon index.
:param bool caching:
Whether to lookup the cache for an existing instance or not.
:raise ValueError:
Raised if the index is invalid.
"""
WeaponMixin.__init__(self, index)
Entity.__init__(self, index)
def _validate_clip(self):
"""Test if the weapon has a clip."""
if (
self.classname in weapon_manager and
weapon_manager[self.classname].clip is None
) or self._clip == -1:
raise ValueError('Weapon does not have a clip.')
def get_clip(self):
"""Return the amount of ammo in the weapon's clip."""
self._validate_clip()
return self._clip
def set_clip(self, value):
"""Set the amount of ammo in the weapon's clip."""
self._validate_clip()
self._clip = value
# Set the "clip" property methods
clip = property(
get_clip, set_clip,
doc="""Property to get/set the weapon's clip.""")
def _validate_ammo(self):
"""Test if the weapon has a valid ammoprop and an owner."""
if (
self.classname in weapon_manager and
weapon_manager[self.classname].ammoprop is None
) or self.ammoprop == -1:
raise ValueError(
'Unable to get ammoprop for {weapon}'.format(
weapon=self.classname
)
)
player = self.owner
if player is None:
raise ValueError('Unable to get the owner of the weapon.')
return player
def get_ammo(self):
"""Return the amount of ammo the player has for the weapon."""
player = self._validate_ammo()
return player.get_property_int(
'{base}{prop:03d}'.format(
base=weapon_manager.ammoprop,
prop=self.ammoprop,
)
)
def set_ammo(self, value):
"""Set the player's ammo property for the weapon."""
player = self._validate_ammo()
player.set_property_int(
'{base}{prop:03d}'.format(
base=weapon_manager.ammoprop,
prop=self.ammoprop,
),
value,
)
# Set the "ammo" property methods
ammo = property(
get_ammo, set_ammo,
doc="""Property to get/set the weapon's ammo.""")
def _validate_secondary_fire_clip(self):
"""Test if the weapon has a secondary fire clip."""
if self._secondary_fire_clip == -1:
raise ValueError('Weapon does not have a secondary fire clip.')
def get_secondary_fire_clip(self):
"""Return the amount of ammo in the weapon's secondary fire clip."""
self._validate_secondary_fire_clip()
return self._secondary_fire_clip
def set_secondary_fire_clip(self, value):
"""Set the amount of ammo in the weapon's secondary fire clip."""
self._validate_secondary_fire_clip()
self._secondary_fire_clip = value
# Set the "secondary_fire_clip" property methods
secondary_fire_clip = property(
get_secondary_fire_clip, set_secondary_fire_clip,
doc="""Property to get/set the weapon's secondary fire clip.""")
def _validate_secondary_fire_ammo(self):
"""Test if the weapon has a valid secondary fire ammoprop and an owner."""
if self.secondary_fire_ammoprop == -1:
raise ValueError(
'Unable to get secondary fire ammoprop for {0}'.format(
self.classname))
player = self.owner
if player is None:
raise ValueError('Unable to get the owner of the weapon.')
return player
def get_secondary_fire_ammo(self):
"""Return the secondary fire ammo the player has for the weapon."""
player = self._validate_secondary_fire_ammo()
return player.get_property_int(
'{base}{prop:03d}'.format(
base=weapon_manager.ammoprop,
prop=self.secondary_fire_ammoprop,
)
)
def set_secondary_fire_ammo(self, value):
"""Set the player's secondary fire ammo property for the weapon."""
player = self._validate_secondary_fire_ammo()
player.set_property_int(
'{base}{prop:03d}'.format(
base=weapon_manager.ammoprop,
prop=self.secondary_fire_ammoprop,
),
value,
)
# Set the "secondary_fire_ammo" property methods
secondary_fire_ammo = property(
get_secondary_fire_ammo, set_secondary_fire_ammo,
doc="""Property to get/set the weapon's secondary fire ammo.""")
@property
def weapon_name(self):
"""Return the full class name of the weapon."""
return self.classname
def remove(self):
"""Remove the weapon."""
# Avoid a cyclic import
from players.entity import Player
owner = self.owner
if owner is not None and owner.is_player():
player = Player(owner.index)
player.drop_weapon(self)
super().remove()