Facepunch / garrysmod-requests

Feature requests for Garry's Mod
84 stars 24 forks source link

Add Player/NPC:EquipWeapon and DetachWeapon #1235

Closed Kefta closed 4 years ago

Kefta commented 5 years ago

These are the core events that add and remove weapons from the player's inventory. Lua access to them would allow for custom weapon drops without having to recreate ents, direct trading of weapons between players and NPCs, and an alternative give concommand for weapons that wouldn't rely on pickup rules.

Equip: https://github.com/VSES/SourceEngine2007/blob/43a5c90a5ada1e69ca044595383be67f40b33c61/src_main/game/server/basecombatcharacter.cpp#L1981 EquipAmmoOnly (would be better as a parameter in the Lua version): https://github.com/VSES/SourceEngine2007/blob/43a5c90a5ada1e69ca044595383be67f40b33c61/src_main/game/server/basecombatcharacter.cpp#L2083 Drop: https://github.com/VSES/SourceEngine2007/blob/43a5c90a5ada1e69ca044595383be67f40b33c61/src_main/game/server/basecombatcharacter.cpp#L1813

Jvs34 commented 5 years ago

Bumping to ping @robotboy655 to fullfil the request, but also to point out that just binding Weapon_Equip itself is a lil bit unsafe, it doesn't have the necessary checks that BumpWeapon does to check for duplicate weapon and the sort.

So in my opinion, if these were bound to Lua, they should have the checks inside of the Lua binds themselves.

And of course preventing shit like equipping a weapon that someone is still holding and etc.

Kefta commented 5 years ago

The checks should probably just go in Weapon_Equip itself for safety.

robotboy655 commented 5 years ago

Added NPC:DropWeapon and its arguments as well as backported the arguments to the player DropWeapon functions.

https://wiki.garrysmod.com/page/NPC/DropWeapon

Alf21 commented 4 years ago

The workaround described in https://github.com/Facepunch/garrysmod-issues/issues/4049 wouldn't work in our scenario because of the GM:PlayerCanPickupWeapon, which is called automatically for the weapons that are in a defined range. If you wanna (like we do) pick up a weapon out of this range (e.g. while looking at them), this is not a valid "solution". I thought about a workaround (teleporting this weapon to the player, ...), but any way seems to be ugly / provisionally. The only way currently is to decrease the range :/

TimGoll commented 4 years ago

For anyone interested, we've implemented our pickup in a really hacky way, since robotboy said we have to use the GM:PlayerCanPickupWeapon hook.

First, there is the problem that the hook only tries to pickup entities in a really small area, our pickup systems works in a bigger radius. Therefore we teleport the weapons to the playerposition. But since teleporting weapons to the feet can glitch them in the ground / in ground near entities, we had to teleport them to chest height.

This created another problem: Our pickupsystem features an autodrop to fastswitch weapons. To minimize the collisions the physics of the weapon that is picked up is disabled. Also it is rendered transparently to eliminate the problem that there is a weapon in your face.

Additionally we added a feature to disable autopickup. All these problems created a really big system that is quite hacky but robust. It supports addons that return false in the PlayerCanPickupWeapon hook, supports autopickup, supports direct weapon switch and much more. All of it is wrapped in a PLAYER:PickupWeapon(wep) function.

In case you try to achieve something similar, feel free to take a look at our implementation:

The pickup function and an extended give function: https://github.com/TTT-2/TTT2/blob/master/gamemodes/terrortown/gamemode/server/sv_player_ext.lua#L1056

Some internal pickup functions: https://github.com/TTT-2/TTT2/blob/master/gamemodes/terrortown/gamemode/server/sv_weapon_pickup.lua

The modified pickup hook: https://github.com/TTT-2/TTT2/blob/master/gamemodes/terrortown/gamemode/server/sv_weaponry.lua#L31

Some modifications to the equip Hook: https://github.com/TTT-2/TTT2/blob/master/gamemodes/terrortown/gamemode/server/sv_weaponry.lua#L656

robotboy655 commented 4 years ago

So why would you want a binding of Weapon_Equip over BumpWeapon?

Kefta commented 4 years ago

Because of this: https://github.com/VSES/SourceEngine2007/blob/43a5c90a5ada1e69ca044595383be67f40b33c61/se2007/game/server/hl2mp/hl2mp_player.cpp#L864-L868

robotboy655 commented 4 years ago

Added Player.PickupWeapon( wep, onlyAmmo ) = bool success

robotboy655 commented 4 years ago

Added NPC:PickupWeapon( wep ) = bool

And with that this is fulfilled. NPC:PickupWeapon does not have an ammo only argument and will drop NPC's current weapon.

Kefta commented 4 years ago

Why is the NPC version lacking an ammoonly arg?

robotboy655 commented 4 years ago

Because it's completely useless since NPCs have infinite ammo?

Kefta commented 4 years ago

But NPCs are CBaseCombatCharacters which all have an ammo table, none of whose functions are binded to Lua for NPCs (GetAmmoCount and the like). Where are they given infinite ammo?

Kefta commented 4 years ago

Also, it doesn't make sense for PickupWeapon to drop the NPCs current weapon, they can hold multiple weapons: https://github.com/Facepunch/garrysmod-requests/issues/1237. Seems like we're missing a lot of CBaseCombatCharacter bindings for NPCs.

robotboy655 commented 4 years ago

Have you ever seen a HL2 NPC run out of ammo?

https://github.com/ValveSoftware/source-sdk-2013/blob/0d8dceea4310fde5706b3ce1c70609d72a38efdf/sp/src/game/server/ai_basenpc.cpp#L8386

They don't consume ammo.

Kefta commented 4 years ago

That's only if you use reload events, they consume ammo with custom SWEPs.