Page 1 of 3
Stripping weapons
Posted: Tue Nov 27, 2012 11:17 pm
by satoon101
This is a tutorial on how to strip weapons from a player.
This is how you would remove a specific weapon from a player's inventory:
Syntax: Select all
from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from entities.entity import BaseEntity
@Event
def player_say(GameEvent):
# Get the player's userid
userid = GameEvent.GetInt('userid')
# Get the player's index
index = index_from_userid(userid)
# Get the player's PlayerEntity instance
player = PlayerEntity(index)
# Get the player's secondary weapon's index
weapon_index = player.get_secondary()
# Get the weapon's BaseEntity instance
weapon = BaseEntity(weapon_index)
# Have the player drop the weapon
player.drop_weapon(weapon.pointer, True, True)
# Remove the weapon from the server
weapon.remove()
This is how you would remove all weapons except the knife from the player's inventory:
Syntax: Select all
from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from entities.entity import BaseEntity
@Event
def player_say(GameEvent):
# Get the player's userid
userid = GameEvent.GetInt('userid')
# Get the player's index
index = index_from_userid(userid)
# Get the player's PlayerEntity instance
player = PlayerEntity(index)
# Loop through all weapons in the player's inventory
for weapon_index in player.weapon_indexes(not_filters='knife'):
# Get the weapon's BaseEntity instance
weapon = BaseEntity(weapon_index)
# Have the player drop the weapon
player.drop_weapon(weapon.pointer, True, True)
# Remove the weapon from the server
weapon.remove()
Of course, this will only work on games that have the dynamic functions for DropWeapon and UTIL_Remove implemented.
If you do not remove the weapons, you can also use the drop_weapon functionality to cause players to drop their weapons (including grenades and even their knife).
Satoon
Posted: Sat Dec 01, 2012 11:36 pm
by Tuck
bit off topic but, Can we give weapons yet?
Posted: Sun Dec 02, 2012 1:28 am
by satoon101
GiveNamedItem is already implemented. The post that I created about that has obviously disappeared, since the site went down. It went something like this:
- Using Binutils directly:
Syntax: Select all
from os import name as os_name
from Source import Binutils
from events import Event
from players.helpers import pointer_from_userid
# Get the virtual machine instance
DynCallVM = Binutils.dcGetVM()
# Get the module's instance
module = Binutils.FindModuleData('csgo/bin/server')
# Is the server running Windows?
if os_name == 'nt':
# Set the calling convention
convention = Binutils.DC_CALL_C_X86_WIN32_THIS_MS
# Set the signature
sig = "\x55\x8B\xEC\x83\xEC\x28\x53\x8B\x5D\x08\x57\x8B\xF9\x85\xDB\x74\x2E"
# Get the address of the signature
address = Binutils.FindSignature(module, sig, len(sig))
# Is the server not running Windows?
else:
# Set the calling convention
convention = Binutils.DC_CALL_C_X86_WIN32_THIS_GNU
# Set the symbol
symbol = "_ZN9CCSPlayer13GiveNamedItemEPKcib"
# Get the address of the symbol
address = Binutils.FindSymbol(module, symbol)
@Event
def player_say(GameEvent):
# Get the player's userid
userid = GameEvent.GetInt('userid')
# Get the players pointer
pointer = pointer_from_userid(userid)
# Reset the virtual machine
Binutils.dcReset(DynCallVM)
# Set the calling convention
Binutils.dcMode(DynCallVM, convention)
# Push the player's pointer
Binutils.dcArgPointer(DynCallVM, pointer)
# Push the weapon to give the player
Binutils.dcArgString(DynCallVM, 'weapon_awp')
# Push the slot to put the weapon into
# Always use 0 for this, so it assigns the first available slot
# Note that this doesn't mean slot1, slot2, etc...
Binutils.dcArgInt(DynCallVM, 0)
# Push a bool value (not sure what this is for, is not in CS:S)
Binutils.dcArgBool(DynCallVM, True)
# Call the function
# The item's pointer is returned, so use dcCallPointer
weapon_pointer = Binutils.dcCallPointer(DynCallVM, address)
- Using Signature Dictionary:
Syntax: Select all
from dyncall.dictionary import SignatureDictionary
from events import Event
from players.helpers import pointer_from_userid
@Event
def player_say(GameEvent):
# Get the player's userid
userid = GameEvent.GetInt('userid')
# Get the player's pointer
pointer = pointer_from_userid(userid)
# Get the GiveNamedItem instance
instance = SignatureDictionary['GiveNamedItem']
# Call the function with the proper values
weapon_pointer = instance.call_function(pointer, 'weapon_awp', 0, True)
- Using PlayerEntity:
Syntax: Select all
from events import Event
from players.entity import PlayerEntity
from players.helpers import index_from_userid
@Event
def player_say(GameEvent):
# Get the player's userid
userid = GameEvent.GetInt('userid')
# Get the player's index
index = index_from_userid(userid)
# Get the player's PlayerEntity instance
player = PlayerEntity(index)
# Give the player the weapon
player.give_named_item('weapon_awp', 0, True)
Satoon
Posted: Sun Dec 02, 2012 10:03 am
by Tuck
weapon_pointer = SignatureDictionary['GiveNamedItem'](pointer, 'weapon_awp', 0, True)
Error: signature object is not callable
figured it by reading signature from libs
Syntax: Select all
addr = SignatureDictionary['GiveNamedItem']
addr.call_function(pointer, item, 0, True)
Posted: Sun Dec 02, 2012 1:52 pm
by satoon101
Yeah, sorry, just threw that example together real quick last night. Fixed in post above.
Satoon
Posted: Thu Dec 06, 2012 4:30 am
by CS4LIFE
Hi, I would like to make an addon to make player drop the weapon they are holding if I shoot their hands.
Is this possible in CS:S and CS:GO?
Posted: Thu Dec 06, 2012 7:39 am
by L'In20Cible
Hey CS4LIFE,
Try this:
Syntax: Select all
# ============================================================================
# >> IMPORTS
# ============================================================================
# Source Python Imports
# Entities
from entities.helpers import pointer_from_inthandle
# Events
from events import Event
# Players
from players.entity import PlayerEntity
from players.helpers import index_from_userid
# ============================================================================
# >> GAME EVENTS
# ============================================================================
@Event
def player_hurt(GameEvent):
'''Fire every time a player get hurts'''
# Is the player not damaged on left or right hand?
if GameEvent.GetInt('hitgroup') not in (4, 5):
# No need to go further
return
# Get the player's PlayerEntity instance
player_entity = PlayerEntity(
index_from_userid(GameEvent.GetInt('userid')))
# Is the player dying?
if GameEvent.GetInt('damage') >= player_entity.health:
# No need to go further
return
# Get the player's active weapon
m_hActiveWeapon = player_entity.active_weapon
# Is the weapon not valid?
if m_hActiveWeapon == -1:
# No need to go further
return
# Force the player to drop his active weapon
player_entity.drop_weapon(
pointer_from_inthandle(m_hActiveWeapon), True, True)
L'In20Cible
Posted: Tue Oct 08, 2013 10:06 am
by sicman_adrian
satoon101 wrote:This is a tutorial on how to strip weapons from a player.(snip)
Big bump but wanting to learn.
When trying to load example, with latest alpha build
sp load strip
[SP] Loading addon 'strip'...
[SP] Addon 'strip' was unable to be loaded.
Posted: Wed Oct 09, 2013 4:24 am
by arawra
Posted: Wed Oct 09, 2013 6:21 pm
by Ayuto
arawa, your link has nothing to do with adrian's problem.
Please go to ../cfg/source-python/core_settings.ini and "enable" tracebacks. You will probably get errors about undefined methods, because these scripts were made before the naming scheme change (from CamelCase to names_with_underscores).
Posted: Thu Oct 10, 2013 6:05 am
by sicman_adrian
Yea I had it in right folder arawra
Thanks Ayuto turns out I all packages are before the data_path change, downloading the latest ones from github.
I've updated and all loads fine but I am getting error on line 172 on entities\enitity.py int has no attribute for fset.
Posted: Mon May 05, 2014 10:00 pm
by arawra
Code: Select all
@SayCommand('!test')
def test(player, teamonly, CCommand):
myPlayer = PlayerEntity(index_from_playerinfo(player))
msg(myPlayer.get_primary())
def msg(the_message):
formatted_message = the_message
for players in PlayerGenerator():
i = index_from_playerinfo(players)
m = messages.SayText2(index=i, chat=1, message=formatted_message)
m.send(i)

Posted: Mon May 05, 2014 10:30 pm
by L'In20Cible
Weapon functionalities are not updated yet. Also, I'd rather suggest you using
PlayerIter instead of
PlayerGenerator.
Syntax: Select all
from filters.players import PlayerIter
for index in PlayerIter(return_types='index'):
print('A player was found at index {0}!'.format(index))
Posted: Tue May 06, 2014 5:49 pm
by Tuck
Was give_named_item() removed/moved from PlayerEntity?
Is there a equivalent for it yet?
Posted: Tue May 06, 2014 6:05 pm
by satoon101
No, give_named_item still exists for both cstrike and csgo. Though, the signature might have changed for csgo. Eventually, that will use a virtual function (like it currently does on cstrike) on csgo as well.
Posted: Tue May 06, 2014 7:49 pm
by Tuck
satoon101 wrote:No, give_named_item still exists for both cstrike and csgo. Though, the signature might have changed for csgo. Eventually, that will use a virtual function (like it currently does on cstrike) on csgo as well.
Would somebody be able to get the new signature would be greatly appriciated
Posted: Tue May 06, 2014 10:41 pm
by L'In20Cible
No need of a signature.
Syntax: Select all
from entities.helpers import edict_from_index
from players.entity import PlayerEntity
from players.helpers import index_from_userid
from tools import ServerTools
def give_item(player, item):
'''Give an item to the given player.'''
# Ghosts can't hold a weapon...
if player.pl.deadflag:
return
# Create the weapon entity...
index = ServerTools.create_entity(item)
# If the given item was invalid, raise an error...
if not index:
raise ValueError('"{0}" is not a valid item.'.format(item))
# Move the weapon to the player...
edict_from_index(index).set_key_value_vector('origin', player.m_vecOrigin)
# Finally, spawn it...
ServerTools.spawn_entity(index)
Usage example:
Syntax: Select all
from events import Event
@Event
def player_say(game_event):
give_item(PlayerEntity(index_from_userid(game_event.get_int('userid'))),
game_event.get_string('text'))
Posted: Tue Jul 29, 2014 2:58 pm
by DJiSer
Are there any ways to strip/drop player weapon for now?
Posted: Mon Aug 04, 2014 2:44 am
by satoon101
Sorry, I keep forgetting to reply to your post. The code in the first post should work with minor changes (ie GameEvent.GetInt should be game_event.get_int), however my tests show that dropping seems broken for the current released version. I have been working on an update to the BaseEntity system. Once it is finished and has been committed to the repository, I will try to remember to update the code above and verify that it works.
Posted: Thu Sep 25, 2014 10:59 am
by 8guawong
Syntax: Select all
for weapon_index in player.weapon_indexes():
weapon = BaseEntity(weapon_index)
player.drop_weapon(weapon.pointer, True, True)
can drop the weapon but weapon.remove() is not working