So I'm pretty lost right now with this. Maybe its because I'm still sick, but I cant seem to figure this out:
Syntax: Select all
# =============================================================================
# >> IMPORTS
# =============================================================================
# Python
from collections import defaultdict
# Source.Python
from engines.precache import Model
from entities.entity import Entity
from entities.hooks import EntityCondition, EntityPreHook
from events import Event
from filters.weapons import WeaponClassIter
from listeners.tick import Delay
from memory import make_object
from players.entity import Player
from weapons.entity import Weapon
from weapons.manager import weapon_manager
# =============================================================================
# >> CONFIGURATION
# =============================================================================
# Set to the model to use for the dropped pack
pack_model = 'models/items/item_equipment_r2.mdl'
# Set to the number of seconds before the pack is removed
kill_time = 15
# =============================================================================
# >> END OF CONFIGURATION
# =============================================================================
# =============================================================================
# >> GLOBAL VARIABLES
# =============================================================================
ini_weapons = weapon_manager.ini['weapons']
# Get the weapons to not store in the pack
skip_weapons = [
weapon.name for weapon in
list(WeaponClassIter('melee')) + list(WeaponClassIter('tool'))
]
# Get the weapons that do not have a clip
ammo_only_weapons = [
weapon.name for weapon in weapon_manager.values() if weapon.clip is None
]
# Get the weapons that have a secondary fire
secondary_ammo_weapons = [
weapon_manager[weapon].name for weapon, values in
ini_weapons.items()
if values.get('secondary_fire_ammoprop') is not None
]
# Create a dictionary to store delays
pack_delays = dict()
# =============================================================================
# >> CLASSES
# =============================================================================
class _WeaponPacks(defaultdict):
def __delitem__(self, item):
"""Override to cancel the delay and remove the entity."""
if item not in self:
return
pack_delays[item]()
super().__delitem__(item)
weapon_packs = _WeaponPacks(dict)
# =============================================================================
# >> GAME EVENTS
# =============================================================================
@Event('player_death')
def _drop_pack(game_event):
# Loop through the victim's weapons
player = Player.from_userid(game_event['userid'])
player_weapons = {}
for weapon in player.weapons():
# Skip any weapons that should not be stored
if weapon.classname in skip_weapons:
continue
# Store the weapon with its ammo and secondary fire ammo
primary_ammo = weapon.ammo
if weapon.classname not in ammo_only_weapons:
primary_ammo += weapon.clip
secondary_ammo = 0
if weapon.classname in secondary_ammo_weapons:
secondary_ammo = weapon.secondary_fire_ammo
player_weapons[weapon.classname] = {
'ammo': primary_ammo,
'secondary': secondary_ammo,
}
# Were any weapons found on the victim?
if not player_weapons:
return
# Create the pack
entity = Entity.create('weapon_crossbow')
entity.model = Model(pack_model)
entity.spawn()
origin = player.origin
origin.z += 15
entity.teleport(origin)
weapon_packs[entity.index] = player_weapons
pack_delays[entity.index] = Delay(
delay=kill_time,
callback=_remove_entity,
args=(entity.index, ),
)
# =============================================================================
# >> ENTITY HOOKS
# =============================================================================
@EntityPreHook(EntityCondition.is_player, 'bump_weapon')
def _touch_pack(stack_data):
entity = make_object(Entity, stack_data[1])
# Is the entity a pack?
if entity.index not in weapon_packs:
return
# Verify the entity is still of the correct type
if entity.classname != 'prop_physics':
del weapon_packs[entity.index]
return
# Verify the entity is still of the correct model
if entity.model.path != pack_model:
del weapon_packs[entity.index]
return
# Verify the toucher is a player
player = make_object(Entity, stack_data[0])
if not player.is_player():
return
# Loop through all of the weapons in the pack
player = Player(player.index)
player_weapons = [weapon.classname for weapon in player.weapons()]
for weapon, values in weapon_packs.pop(entity.index).items():
# Give the player the weapon if they don't already own one
if weapon not in player_weapons:
weapon_entity = make_object(Weapon, player.give_named_item(weapon))
if weapon in ammo_only_weapons:
weapon_entity.ammo = values['ammo']
else:
value = values['ammo'] - weapon_entity.clip
if value >= 0:
weapon_entity.ammo = value
else:
weapon_entity.clip -= abs(value)
if values['secondary']:
weapon_entity.secondary_fire_ammo = values['secondary']
# Increase the player's ammo if they already own the weapon
else:
weapon_entity = player.get_weapon(weapon)
weapon_entity.ammo = min([
weapon_entity.ammo + values['ammo'],
weapon_manager[weapon].maxammo,
])
if values['secondary']:
value = ini_weapons[weapon_manager[weapon].name]
weapon_entity.secondary_fire_ammo = min([
weapon_entity.secondary_fire_ammo + values['secondary'],
value['secondary_fire_ammoprop'],
])
# Remove the pack
del weapon_packs[entity.index]
entity.call_input('Kill')
return False
# =============================================================================
# >> HELPER FUNCTIONS
# =============================================================================
def _remove_entity(index):
entity = Entity(index)
if entity.classname == 'prop_physics':
entity.remove()
So the problems here are:
- The weapon does not change its model (tried it with weapon_slam too, no luck either)
- On pickup it does not give you the weapons
- It still picks up the weapon even when I return False
I'm really sorry if I'm beeing stupid right now.
Edit: I just found one thing where it checks if its still prop_physics. That would propably be a cause for problems...
Edit 2: Okay, now I can collect the weapon stash and I do not pick up the weapon itself anymore. But the model is still not changing!