Page 1 of 1

Dropping Knife

Posted: Wed Jan 13, 2016 3:19 pm
by decompile
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

Posted: Wed Jan 13, 2016 3:38 pm
by satoon101

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.

Posted: Wed Jan 13, 2016 3:41 pm
by iPlayer
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.

Posted: Wed Jan 13, 2016 3:43 pm
by satoon101
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

Posted: Wed Jan 13, 2016 3:51 pm
by iPlayer
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.

Posted: Wed Jan 13, 2016 3:52 pm
by decompile
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

Posted: Wed Jan 13, 2016 3:54 pm
by iPlayer
If you want to kill it, just add this line right after 'drop_weapon' one:

Syntax: Select all

weapon.remove()

Posted: Wed Jan 13, 2016 3:57 pm
by decompile
Dang, SourcePython makes everything easier! Thank you both!!

Posted: Wed Jan 13, 2016 4:10 pm
by iPlayer
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.

Posted: Wed Jan 13, 2016 4:32 pm
by Doldol
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.)

Posted: Wed Jan 13, 2016 8:13 pm
by decompile
What do you mean under nade? he_grenade or the smoke_grenade and whatever grenades there are

Re: Dropping Knife

Posted: Sat Apr 01, 2017 4:44 pm
by decompile
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

Re: Dropping Knife

Posted: Sun Apr 02, 2017 3:16 pm
by satoon101
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.