Skip to content

Commit 6bd2ee2

Browse files
committed
BugFix issue #11.
Updated weapons.entity.Weapon to use engine/game specific functionalities. Added missing ushort and char get/set property methods in Entity class.
1 parent fafd8ca commit 6bd2ee2

File tree

6 files changed

+244
-126
lines changed

6 files changed

+244
-126
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[property]
2+
3+
primary_ammo_count = m_iPrimaryReserveAmmoCount
4+
secondary_ammo_count = m_iSecondaryReserveAmmoCount
5+
ammoprop = m_iPrimaryAmmoType
6+
secondary_fire_ammoprop = m_iSecondaryAmmoType

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ def get_property_short(self, name):
389389
"""Return the short property."""
390390
return self._get_property(name, 'short')
391391

392+
def get_property_ushort(self, name):
393+
"""Return the ushort property."""
394+
return self._get_property(name, 'ushort')
395+
392396
def get_property_string(self, name):
393397
"""Return the string property."""
394398
return self._get_property(name, 'string_array')
@@ -397,6 +401,10 @@ def get_property_string_pointer(self, name):
397401
"""Return the string property."""
398402
return self._get_property(name, 'string_pointer')
399403

404+
def get_property_char(self, name):
405+
"""Return the char property."""
406+
return self._get_property(name, 'char')
407+
400408
def get_property_uchar(self, name):
401409
"""Return the uchar property."""
402410
return self._get_property(name, 'uchar')
@@ -468,6 +476,10 @@ def set_property_short(self, name, value):
468476
"""Set the short property."""
469477
self._set_property(name, 'short', value)
470478

479+
def set_property_ushort(self, name, value):
480+
"""Set the short property."""
481+
self._set_property(name, 'ushort', value)
482+
471483
def set_property_string(self, name, value):
472484
"""Set the string property."""
473485
self._set_property(name, 'string_array', value)
@@ -476,6 +488,10 @@ def set_property_string_pointer(self, name, value):
476488
"""Set the string property."""
477489
self._set_property(name, 'string_pointer', value)
478490

491+
def set_property_char(self, name, value):
492+
"""Set the char property."""
493+
self._set_property(name, 'char', value)
494+
479495
def set_property_uchar(self, name, value):
480496
"""Set the uchar property."""
481497
self._set_property(name, 'uchar', value)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
# ../players/_entity/csgo/__init__.py
2+
3+
"""Provides Orangebox specific Player based functionality."""
4+
5+
# =============================================================================
6+
# >> IMPORTS
7+
# =============================================================================
8+
# Source.Python Imports
9+
# Players
110
from players._entity import Player as _Player
211

312

13+
# =============================================================================
14+
# >> CLASSES
15+
# =============================================================================
416
class Player(_Player):
517
"""Class used to interact directly with players."""
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# ../weapons/_entity/__init__.py
2+
3+
"""Provides simplified weapon functionality."""
4+
5+
# =============================================================================
6+
# >> IMPORTS
7+
# =============================================================================
8+
# Source.Python Imports
9+
# Entities
10+
from entities.entity import Entity
11+
from entities.helpers import index_from_inthandle
12+
# Weapons
13+
from weapons.manager import weapon_manager
14+
15+
16+
# =============================================================================
17+
# >> GLOBAL VARIABLES
18+
# =============================================================================
19+
# Add all the global variables to __all__
20+
__all__ = ('Weapon',
21+
)
22+
23+
24+
# =============================================================================
25+
# >> CLASSES
26+
# =============================================================================
27+
class Weapon(Entity):
28+
"""Allows easy usage of the weapon's attributes."""
29+
30+
@property
31+
def current_owner(self):
32+
"""Return a Entity instance of the weapon's owner."""
33+
# Does no player currently own the weapon?
34+
if self.owner == -1:
35+
return None
36+
37+
# Return a Entity instance of the owner
38+
return Entity(index_from_inthandle(self.owner))
39+
40+
def get_clip(self):
41+
"""Return the amount of ammo in the weapon's clip."""
42+
return self.clip if self.clip != -1 else 0
43+
44+
def set_clip(self, value):
45+
"""Set the amount of ammo in the weapon's clip."""
46+
# Does the weapon have ammo?
47+
if self.clip != -1:
48+
49+
# Set the clip amount
50+
self.clip = value
51+
52+
def get_ammo(self):
53+
"""Return the amount of ammo the player has for the weapon."""
54+
# Do we have a valid ammoprop?
55+
if self.ammoprop == -1:
56+
raise ValueError(
57+
'Unable to get ammoprop for {0}'.format(self.classname))
58+
59+
# Get the owner of the weapon
60+
player = self.current_owner
61+
62+
# Does no one currently own the weapon?
63+
if player is None:
64+
return
65+
66+
# Return the ammo
67+
return player.get_property_ushort(
68+
weapon_manager.ammoprop + '%03d' % self.ammoprop)
69+
70+
def set_ammo(self, value):
71+
"""Set the player's ammo property for the weapon."""
72+
# Do we have a valid ammoprop?
73+
if self.ammoprop == -1:
74+
raise ValueError(
75+
'Unable to set ammoprop for {0}'.format(self.classname))
76+
77+
# Get the owner of the weapon
78+
player = self.current_owner
79+
80+
# Does no one currently own the weapon?
81+
if player is None:
82+
return
83+
84+
# Set the ammo
85+
player.set_property_ushort(
86+
weapon_manager.ammoprop + '%03d' % self.ammoprop, value)
87+
88+
# Set the "ammo" property methods
89+
ammo = property(
90+
get_ammo, set_ammo,
91+
doc="""Property to get/set the weapon's ammo.""")
92+
93+
def get_secondary_fire_clip(self):
94+
"""Return the amount of ammo in the weapon's secondary fire clip."""
95+
return (
96+
self.secondary_fire_clip if self.secondary_fire_clip != -1 else 0)
97+
98+
def set_secondary_fire_clip(self, value):
99+
"""Set the amount of ammo in the weapon's secondary fire clip."""
100+
# Does the weapon have secondary fire ammo?
101+
if self.secondary_fire_clip != -1:
102+
103+
# Set the secondary fire clip amount
104+
self.secondary_fire_clip = value
105+
106+
def get_secondary_fire_ammo(self):
107+
"""Return the secondary fire ammo the player has for the weapon."""
108+
# Do we have a valid ammoprop?
109+
if self.secondary_fire_ammoprop == -1:
110+
raise ValueError(
111+
'Unable to get secondary fire ammoprop for {0}'.format(
112+
self.classname))
113+
114+
# Get the owner of the weapon
115+
player = self.current_owner
116+
117+
# Does no one currently own the weapon?
118+
if player is None:
119+
return
120+
121+
# Return the ammo
122+
return player.get_property_ushort(
123+
weapon_manager.ammoprop + '%03d' % self.secondary_fire_ammoprop)
124+
125+
def set_secondary_fire_ammo(self, value):
126+
"""Set the player's secondary fire ammo property for the weapon."""
127+
# Do we have a valid ammoprop?
128+
if self.secondary_fire_ammoprop == -1:
129+
raise ValueError(
130+
'Unable to set secondary fire ammoprop for {0}'.format(
131+
self.classname))
132+
133+
# Get the owner of the weapon
134+
player = self.current_owner
135+
136+
# Does no one currently own the weapon?
137+
if player is None:
138+
return
139+
140+
# Set the ammo
141+
player.set_property_ushort(
142+
weapon_manager.ammoprop +
143+
'%03d' % self.secondary_fire_ammoprop, value)
144+
145+
# Set the "secondary_fire_ammo" property methods
146+
secondary_fire_ammo = property(
147+
get_secondary_fire_ammo, set_secondary_fire_ammo,
148+
doc="""Property to get/set the weapon's secondary fire ammo.""")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# ../weapons/_entity/__init__.py
2+
3+
"""Provides CS:GO specific weapon functionality."""
4+
5+
# =============================================================================
6+
# >> IMPORTS
7+
# =============================================================================
8+
# Weapons
9+
from weapons._entity import Weapon as _Weapon
10+
from weapons.manager import weapon_manager
11+
12+
13+
# =============================================================================
14+
# >> GLOBAL VARIABLES
15+
# =============================================================================
16+
_projectile_names = weapon_manager.projectiles.values()
17+
18+
19+
# =============================================================================
20+
# >> CLASSES
21+
# =============================================================================
22+
class Weapon(_Weapon):
23+
"""Allows easy usage of the weapon's attributes."""
24+
def get_ammo(self):
25+
"""Return the amount of ammo the player has for the weapon."""
26+
# Is the weapon a projectile?
27+
if weapon_manager[self.classname].basename in _projectile_names:
28+
return super(Weapon, self).get_ammo()
29+
30+
return self.primary_ammo_count
31+
32+
def set_ammo(self, value):
33+
"""Set the player's ammo property for the weapon."""
34+
# Is the weapon a projectile?
35+
if weapon_manager[self.classname].basename in _projectile_names:
36+
super(Weapon, self).set_ammo(value)
37+
return
38+
39+
self.primary_ammo_count = value
40+
41+
# Set the "ammo" property methods
42+
ammo = property(
43+
get_ammo, set_ammo,
44+
doc="""Property to get/set the weapon's ammo.""")

0 commit comments

Comments
 (0)