Dropping Knife

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Dropping Knife

Postby decompile » Wed Jan 13, 2016 3:19 pm

Hey, i tried to use the search function but didnt gave me what i was looking for.

Is there anyway to drop knife, he smokes etc. with the clientside drop button?

Sorry for the bad english, im on my phone
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Jan 13, 2016 3:38 pm

Syntax: Select all

from commands.client import ClientCommand
from entities.helpers import index_from_inthandle
from filters.weapons import WeaponClassIter
from mathlib import NULL_VECTOR
from players.entity import Player
from weapons.entity import Weapon


knife_weapons = [x.name for x in WeaponClassIter('knife')]
nade_weapons = [x.name for x in WeaponClassIter('grenade')]
drop_weapons = knife_weapons + nade_weapons


@ClientCommand('drop')
def drop_weapon(command, index):
player = Player(index)
weapon = Weapon(index_from_inthandle(player.active_weapon))
if weapon.classname in drop_weapons:
player.drop_weapon(weapon.pointer, NULL_VECTOR, NULL_VECTOR)


You can use any vector for the last 2 arguments in drop_weapon. Getting the weapon will be slightly easier once we finish the player_weapons_update branch and merge those changes.
Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Wed Jan 13, 2016 3:41 pm

Wouldn't it be better with

Syntax: Select all

try:
weapon = Weapon(index_from_inthandle(player.active_weapon))
except (ValueError, OverflowError):
return

?

Also, you can use BaseEntity instead of Weapon for this purpose, I believe it would be faster

By the way, NULL_VECTOR throws it very far away from the player.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Jan 13, 2016 3:43 pm

Meh, minor details. Again, that will be easier once the other branch is merged. In the other branch, active_weapon is a property instead of using data, and it will do all that on its own to return the Weapon instance.


iPlayer wrote:By the way, NULL_VECTOR throws it very far away from the player.


No it doesn't, quite the opposite in fact, since the second vector is the velocity:
https://github.com/alliedmodders/hl2sdk/blob/98fe5b5a34b3721fe4d60ec7ba3a28ade3512560/game/server/hl2mp/hl2mp_player.h#L74
Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Wed Jan 13, 2016 3:51 pm

satoon101 wrote:No it doesn't, quite the opposite in fact as one of the vectors is the velocity.


Well, then maybe our NULL_VECTOR then gets summed with some other vectors? Because for me (cstrike) it always throws the item in the direction of (1, -1, 1). Right, backwards and up according to Hammer axises.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Postby decompile » Wed Jan 13, 2016 3:52 pm

Hey thank you first!

I want players to drop any weapon they have, I think CS:S atleast refuses to drop knife and grenades. So when you have a knife as active weapon you just need to press "G" (Default) to drop it as usual.

I just found a code from SP times, but couldnt find something similiar:

You can probably do that with ClientCommand('drop')

Syntax: Select all

// drop any weapon
public Action :D ropItem(client, const String:command[], argc)
{
new weaponIndex = GetEntPropEnt(client, Prop_Data, "m_hActiveWeapon");
// Allow ghosts to drop all weapons and allow players if the cvar allows them to
if(GetConVarBool(g_hAllowKnifeDrop) || IsFakeClient(client))
{
if(weaponIndex != -1)
{
CS_DropWeapon(client, weaponIndex, true, false);
}

return Plugin_Handled;
}

return Plugin_Continue;
}


Would be also great if it might kill any weapon which has been dropped. (Not only for this plugin, in total)

Is there an event when a weapon has been dropped?

Syntax: Select all

// kill weapon and weapon attachments on drop
public Action:CS_OnCSWeaponDrop(client, weaponIndex)
{
if(weaponIndex != -1)
{
AcceptEntityInput(weaponIndex, "KillHierarchy");
AcceptEntityInput(weaponIndex, "Kill");
}
}


EDIT: oh well I see my browser didnt showed the code until I restarted it.. weird Sorry but ye ignore the first code part. :p
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Wed Jan 13, 2016 3:54 pm

If you want to kill it, just add this line right after 'drop_weapon' one:

Syntax: Select all

weapon.remove()
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Postby decompile » Wed Jan 13, 2016 3:57 pm

Dang, SourcePython makes everything easier! Thank you both!!
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Wed Jan 13, 2016 4:10 pm

Nevermind, something's going really weird during my testing.
Velocity vector doesn't seem to do anything for me, and I still don't know what makes the item fly that far.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Postby Doldol » Wed Jan 13, 2016 4:32 pm

Because the first example included nades:

You have to be careful when dropping nades, you'll keep the nade if the pin is pulled on it or it's being thrown, and you'll also drop it, basically duplicating the items.

This is old, but you need something like this:

Syntax: Select all

SEQUENCE_THROWN = 5
SEQUENCE_PINOUT = 1
BLOCKED_SEQ = (SEQUENCE_THROWN, SEQUENCE_PINOUT)

if Entity(index_from_inthandle(m_hActiveWeapon)).get_property_int("m_nSequence") not in BLOCKED_SEQ: # save to drop the nade


Additionally when you have more than one nade you also need to handle the ammo (I don't remember what specifically is needed.)
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Postby decompile » Wed Jan 13, 2016 8:13 pm

What do you mean under nade? he_grenade or the smoke_grenade and whatever grenades there are
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Dropping Knife

Postby decompile » Sat Apr 01, 2017 4:44 pm

Weirdly never tested the dropping knife since I added it back then.
Right now it removes the weapon, but clientside you still see the knife equipped, any help?

Syntax: Select all

@ClientCommand('drop')
def drop_knife(command, index):
player = Player(index)
weapon = player.active_weapon
if weapon.classname.startswith('weapon_knife'):
player.drop_weapon(weapon.pointer, NULL_VECTOR, NULL_VECTOR)


Edit:

It bugs cause of:

Syntax: Select all

@EntityPreHook(EntityCondition.is_player, 'drop_weapon')
def pre_drop_weapon(args):
weapon_ptr = args[1]
if not weapon_ptr:
return

weapon = memory.make_object(BaseEntity, weapon_ptr)
weapon.remove()
return False
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Dropping Knife

Postby satoon101 » Sun Apr 02, 2017 3:16 pm

I'm not sure where you found that drop_weapon pre-hook, but I imagine you would rather that be a post-hook. Removing the weapon before it has been "dropped" will have some strange side-effects, like what you are seeing.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 38 guests