Stripping weapons

Post Python examples to help other users.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Stripping weapons

Postby satoon101 » Tue Nov 27, 2012 11:17 pm

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
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Sat Dec 01, 2012 11:36 pm

bit off topic but, Can we give weapons yet?
-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Dec 02, 2012 1:28 am

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
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Sun Dec 02, 2012 10:03 am

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)
-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sun Dec 02, 2012 1:52 pm

Yeah, sorry, just threw that example together real quick last night. Fixed in post above.

Satoon
CS4LIFE
Junior Member
Posts: 1
Joined: Thu Dec 06, 2012 4:15 am

Postby CS4LIFE » Thu Dec 06, 2012 4:30 am

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?
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Thu Dec 06, 2012 7:39 am

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
sicman_adrian
Junior Member
Posts: 3
Joined: Tue Oct 08, 2013 10:03 am

Postby sicman_adrian » Tue Oct 08, 2013 10:06 am

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.
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Wed Oct 09, 2013 4:24 am

User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Wed Oct 09, 2013 6:21 pm

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).
sicman_adrian
Junior Member
Posts: 3
Joined: Tue Oct 08, 2013 10:03 am

Postby sicman_adrian » Thu Oct 10, 2013 6:05 am

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.
arawra
Senior Member
Posts: 190
Joined: Fri Jun 21, 2013 6:51 am

Postby arawra » Mon May 05, 2014 10:00 pm

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)

Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon May 05, 2014 10:30 pm

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))
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Tue May 06, 2014 5:49 pm

Was give_named_item() removed/moved from PlayerEntity?

Is there a equivalent for it yet?
-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue May 06, 2014 6:05 pm

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.
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Tue May 06, 2014 7:49 pm

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
-Tuck
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Tue May 06, 2014 10:41 pm

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'))
DJiSer
Junior Member
Posts: 20
Joined: Sat May 10, 2014 2:44 pm

Postby DJiSer » Tue Jul 29, 2014 2:58 pm

Are there any ways to strip/drop player weapon for now?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Aug 04, 2014 2:44 am

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.
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Thu Sep 25, 2014 10:59 am

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

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 16 guests