Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to spawn prop_physics on npc death?  (Read 4765 times)

0 Members and 1 Guest are viewing this topic.

Offline Zadaki_L

  • *
  • +1% chance to make useful post
  • Zadaki_L has no influence.
How to spawn prop_physics on npc death?
« on: September 03, 2013, 11:28:11 PM »
Basically, I want to have the player hunt down and kill a terrorist leader, then take his mobile phone for intelligence. I figured for a mobile phone, I would use the OnPlayerPickup output to make the prop_physics disappear, similar to picking up ammo or weapons. But that part's easy. The hard part is getting the phone to spawn in the same place as the npc's ragdoll corpse.

I suppose I could have the npc in a location in which he can't move, then force the mobile phone to spawn when he dies using an "islived" label, but that seems lame. Or pretending that he set down the phone on a desk or something, but you have to kill him to get to the desk.

Is there a way to either pull the last location of the npc before he was killed and spawn the prop_physics in that location, or to somehow use the OnKilled output from the npc to force the ragdoll to spawn the prop_physics?


Any ideas would be appreciated, thanks!

Offline BIZ

  • 32-Bit Hobo
  • *
  • *
  • +0% chance to make useful post
  • BIZ hides in shadows.
    • BSNOOCH.COM
Re: How to spawn prop_physics on npc death?
« Reply #1 on: September 04, 2013, 07:26:50 AM »
yeah I'm contemplating similar concepts...Although I haven't tackled it yet. The answer I believe lies in the LUA code ;)

Offline Zadaki_L

  • *
  • +1% chance to make useful post
  • Zadaki_L has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #2 on: September 04, 2013, 08:53:30 PM »
yeah I'm contemplating similar concepts...Although I haven't tackled it yet. The answer I believe lies in the LUA code ;)

I've kind of hobbled through on the really basic LUA stuff. I may have to pay more attention. Off to the SMOD wiki again...

Some combination of HL2.HookKilledEvent, HL2.GetAbsOrigin (to find location of dead npc; either his prop_ragdoll [how, if the npc entity was killed?], or constantly monitor and use the last known location?), and HL2.SetAbsOrigin (to move the prop_physics from some off-map location to the location of the dead npc).


LeDerp: HL2.SetEntityParent ( <pEnt parent>, <pEnt child>, <attachment point> ) I can have the prop just attached to his belt. Then remove the attachment when he dies, or maybe it will fall off automatically.  I'll have to play with this one.

« Last Edit: September 04, 2013, 08:59:25 PM by Zadaki_L »

Offline Rook

  • *
  • +2% chance to make useful post
  • Rook has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #3 on: September 04, 2013, 09:03:42 PM »
Hi there! First-time post but I hope I can help anyways. I usually play around with Love2D and I've been having a lot of fun doing mapadd stuff so I looked at what you wanted and came up with a pair of tested answers you can use

Like BIZ hinted, the only way I could think of is through LUA, so I put this small script together just for you ;D

Code: [Select]
local propModel = 'models/props_c17/BriefCase001a.mdl'
local leaderModel = 'models/Combine_Super_Soldier.mdl'

function getLeaderDoll()
-- Favored method is to find a ragdoll with a model matching the leader's model
local doll = HL2.FindEntityByClass(nil, 'prop_ragdoll')
local leader = false
while doll do
local model = HL2.GetEntInfo(doll).ModelName
if model == leaderModel then
leader = doll -- We've found a match!
break
end
doll = HL2.FindEntityByClass(doll, 'prop_ragdoll')
end
return leader
end

function getLeaderDollAlt()
-- The alternative method is to just find the last created ragdoll
local doll = HL2.FindEntityByClass(nil, 'prop_ragdoll')
local leader = doll
while doll do
doll = HL2.FindEntityByClass(doll, 'prop_ragdoll')
leader = doll or leader
-- Is the new doll valid? Change our target to the new one
-- If nil (no more ragdolls found), use the previous doll
end
return leader
end

function spawnLeaderProp()
local leader = getLeaderDoll()
if leader then
local origin = HL2.GetEntityAbsOrigin(leader)
local prop = HL2.CreateEntity('prop_physics', origin, VECTORZERO)
if prop then
HL2.KeyValue(prop, 'targetname', 'leader_prop')
HL2.KeyValue(prop, 'model', propModel)
HL2.KeyValue(prop, 'spawnflags', '256') -- Enable OnPlayerUse output
HL2.KeyValue(prop, 'OnPlayerUse', 'prop_relay,Trigger,,0,-1')
HL2.SpawnEntity(prop)
end
else
_ALERT('Could not find leader doll. Prop spawn aborted.')
end
end

And here's the TXT that goes with it
Code: [Select]
"precache" {
"model" "models/props_c17/BriefCase001a.mdl"
}
"entities" {
"npc_combine_s" {
"origin" "-260 -37 8" "angle" "0 135 0"
"keyvalues" {
"targetname" "Holt"
"squadname" "squad_Holt" // Console complains if not squadded
"model" "models/Combine_Super_Soldier.mdl"
"additionalequipment" "weapon_ar2"
}
}
"instant_trig" {
"keyvalues" {
"islived" "Holt"
"label" "leaderDeath"
}
}
"logic_relay" {
"keyvalues" {
"targetname" "prop_relay"
"spawnflags" "1"
"OnTrigger" "leader_prop,Kill,,0,-1"
"OnTrigger" "prop_sound,PlaySound,,0,-1"
"OnTrigger" "whatever_else,Activate,,0,-1"
}
}
"ambient_generic" {
"keyvalues" {
"targetname" "prop_sound"
"spawnflags" "48"
"health" "10"
"message" "Player.PickupWeapon"
"sourceEntityName" "!player"
}
}
}
"entities:leaderDeath" {
"lua" {
"callfunc" "spawnLeaderProp"
}
}

Be careful if you use the second way. instant_trigs only seem to update once or twice a second. If somebody else dies in that moment, the phone will be spawned on that person instead!

Hope this helps a great big bunch.  Lemme know if you want me to explain anything. Cheers! ;)

Edit
LeDerp: HL2.SetEntityParent ( <pEnt parent>, <pEnt child>, <attachment point> ) I can have the prop just attached to his belt. Then remove the attachment when he dies, or maybe it will fall off automatically.  I'll have to play with this one.
I don't think that'll work. When the parent dies, all the children get deleted. You won't have a prop to detach!
Quote from: Valve Wiki
If the Parent is Killed, all of its current Children are also removed from the game.
From here https://developer.valvesoftware.com/wiki/Entity_Hierarchy_%28parenting%29

Oh! And I did OnPlayerUse because OnPlayerPickup can be triggered by the Gravity Gun if my noggin's remembering alright. If that's something you want, just go with OnPlayerPickup.
« Last Edit: September 04, 2013, 09:15:05 PM by Rook »

Offline BIZ

  • 32-Bit Hobo
  • *
  • *
  • +0% chance to make useful post
  • BIZ hides in shadows.
    • BSNOOCH.COM
Re: How to spawn prop_physics on npc death?
« Reply #4 on: September 04, 2013, 10:08:20 PM »
cool...thanks for sharing! Will bookmark for when I get to a point where I need to fool with LUA.

Offline Zadaki_L

  • *
  • +1% chance to make useful post
  • Zadaki_L has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #5 on: September 04, 2013, 10:42:37 PM »
Sweet! That probably saved me a ton of time.  Thanks, Raven.

Offline Rook

  • *
  • +2% chance to make useful post
  • Rook has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #6 on: September 04, 2013, 11:35:07 PM »
Sure thing! Like I said, I've had a lot of fun making stuff like this. In fact, I hope to release a li'l pack of LUA scripts here soon once I'm done ironing the kinks out!   ;D

Offline BIZ

  • 32-Bit Hobo
  • *
  • *
  • +0% chance to make useful post
  • BIZ hides in shadows.
    • BSNOOCH.COM
Re: How to spawn prop_physics on npc death?
« Reply #7 on: September 04, 2013, 11:53:52 PM »
Did some quick testing here is my development...

You CAN parent a prop to an NPC and then detach it when it dies. This should leave a nice clean entity for us to spawn our physics prop at and then quickly delete attached prop. Would just need to do some testing on how fast the LUA calculates...

below is video and mapadd code. Paste the mapadd into d1_canals_01. I hope to see some progress from you boys :)

EDIT: I'm still trying to wrap my head around the LUA code...I'm sure there may be an easier way to do it. Let me know, thanks.

(Watch in HD to see the dev text)
http://youtu.be/SIxsAEFldAQ

http://youtu.be/PsgI1HJm2pk


MAPADD
Code: [Select]
////////////////////////////////////
// MAP ARRANGE DATA
// HL2 - d1_canal_01
// BIZ - BSNOOCH.COM
// NPC drops attached prop
////////////////////////////////////

"precache" {
"model" "models/weapons/w_suitcase_passenger.mdl"
"model" "models/props_c17/suitcase_passenger_physics.mdl"
}

"entities" {

// Lets us run console commands via inputs
"point_servercommand" {
"keyvalues" {
"targetname" "con_cmd"
}
}

// Prop Attach Testing.
"instant_trig" {
"origin" "-164.97 -6845.02 604"
"keyvalues" {
"radius" "70"
"label" "proper-attacher"
}
}

// Testing spawn PhysObj on death via LUA. NON PLAYER TOUCHABLE
"instant_trig" {
"origin" "1604 -5597 1565"
"keyvalues" {
"radius" "1"
"label" "spawn-phys-via-lua"
}
}
}

// Prop Attachment testing...
"entities:proper-attacher" {

// fucked...NPC cannot move when attached :(
//"prop_physics" {
// "origin" "8.7 -5802.9 587.1"  "angle" "-0 77 0"
// "keyvalues" {
// "targetname" "clorox"
// "model" "models/props_junk/garbage_plasticbottle001a.mdl"
// }
//}

// This works better :)
"prop_dynamic" {
"origin" "8.7 -5802.9 587.1"  "angle" "0 0 0"
"keyvalues" {
"targetname" "clorox"
"model" "models/weapons/w_suitcase_passenger.mdl"
"solid" "0"
"DefaultAnim" "idle" // Use HLMV to find sequences
"RandomAnimation" "0" //no collisions
}
}

// Our traveling metrocop
"npc_metropolice" {
"origin" "-43.1 -5864.2 576.0"  "angle" "0 0 0"
"keyvalues" {
"targetname" "cleancop"
"squadname" "formula409"
"additionalequipment" "weapon_pistol"
"OnDeath" "clorox,clearparent,,0,1"
// When he dies make sure he drops some sweeeet loot.
"OnDeath" "con_cmd,command,instant_trig_run spawn-phys-via-lua,0,1"
"OnDeath" "clorox,turnoff,,0.01,1"
}
}

// Attach suitcase to his back
"event" {
"targetname" "clorox"
"action" "setparent"
"value" "cleancop"
"delaytime" "0.01"
}

"event" {
"targetname" "clorox"
"action" "setparentattachment"
"value" "manhack"
"delaytime" "0.02"
}
}

// Spawn PhysObj via LUA
"entities:spawn-phys-via-lua" {

"lua" {
"callfunc" "spawnTargetProp"
}
}

LUA
Code: [Select]
-- Test Prop Attach
-- credit: Rook
local propModel = 'models/props_c17/suitcase_passenger_physics.mdl'

function findClorox()
return HL2.FindEntityByName(nil, 'clorox')
end

function spawnTargetProp()
local found = findClorox()
if found then
local origin = HL2.GetEntityAbsOrigin(found)
local prop = HL2.CreateEntity('prop_physics', origin, VECTORZERO)
if prop then
HL2.KeyValue(prop, 'targetname', 'spawned_via_lua')
HL2.KeyValue(prop, 'model', propModel)
HL2.KeyValue(prop, 'spawnflags', '256') -- Enable OnPlayerUse output
HL2.KeyValue(prop, 'OnPlayerUse', 'prop_relay,Trigger,,0,-1')
HL2.SpawnEntity(prop)
end
else
_ALERT('Could not find Clorox. Prop spawn aborted.')
end
end
« Last Edit: September 05, 2013, 07:15:17 PM by BIZ »

Offline BIZ

  • 32-Bit Hobo
  • *
  • *
  • +0% chance to make useful post
  • BIZ hides in shadows.
    • BSNOOCH.COM
Re: How to spawn prop_physics on npc death?
« Reply #8 on: September 05, 2013, 06:48:19 AM »
updated my first post, thanks for the help. Saves me some time trying to force my way through it. Also a good intro for working with the LUA system. +rep

Sure thing! Like I said, I've had a lot of fun making stuff like this. In fact, I hope to release a li'l pack of LUA scripts here soon once I'm done ironing the kinks out!   ;D

Looking forward to your contributions. LUA documentation/examples is one area where SMOD knowledge is sorely lacking...

Offline Counter-Life™

  • *
  • *
  • +17% chance to make useful post
  • Counter-Life™ is a rising star!Counter-Life™ is a rising star!Counter-Life™ is a rising star!Counter-Life™ is a rising star!Counter-Life™ is a rising star!Counter-Life™ is a rising star!
    • Youtube Channel
Re: How to spawn prop_physics on npc death?
« Reply #9 on: September 05, 2013, 12:12:43 PM »
Someone port some TF2 hats and attach them to NPCs xD

Offline Pepsifan04

  • *
  • *
  • +12% chance to make useful post
  • Pepsifan04 might someday be someone...Pepsifan04 might someday be someone...Pepsifan04 might someday be someone...Pepsifan04 might someday be someone...
Re: How to spawn prop_physics on npc death?
« Reply #10 on: September 05, 2013, 12:24:40 PM »
Someone port some TF2 hats and attach them to NPCs xD
and make hat prop OnPlayerUse so player can wear it :D
Officially Unofficial BSNOOCH Discord:
https://discordapp.com/invite/eBqezeR

Offline Rook

  • *
  • +2% chance to make useful post
  • Rook has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #11 on: September 05, 2013, 06:24:26 PM »
Very cool to see that you can clear the parent before child deletion begins. Had no idea. Thanks, Mister BIZ!  ;)

You've admitted you're not intimately familiar with LUA. Do you mind if I nitpick your code?

The original Find function's aim was to look through the entity lump for a unique non-named ragdoll (either by model or time of creation). Your new aim seems to be looking for a named prop. Since that's the case, you can now trim this

Code: [Select]
function findClorox()
local target = HL2.FindEntityByName(nil, 'clorox')
local found = false
while target do
local name = HL2.GetEntInfo(target).Name
if name == targetName then
found = target -- We've found a match!
break
end
target = HL2.FindEntityByName(target, 'clorox')
end
return found
end

allll the way down to one line of execution:

Code: [Select]
function findClorox()
return HL2.FindEntityByName(nil, 'clorox')
end

Hell of a lot more readable, ain't it? Not even worth breaking off into its own function at that point!  ;D

Edit
Oh, before I forget again! Also meant to say that instant_trig_run is cheat-protected and you can't usually just put "sv_cheats 1" into the _servercommand. It'll just ignore it. You can get around that with "incrementvar sv_cheats 1 1 1" though.  :D
« Last Edit: September 05, 2013, 06:38:46 PM by Rook »

Offline BIZ

  • 32-Bit Hobo
  • *
  • *
  • +0% chance to make useful post
  • BIZ hides in shadows.
    • BSNOOCH.COM
Re: How to spawn prop_physics on npc death?
« Reply #12 on: September 05, 2013, 07:13:35 PM »
yeah thanks for that...I could tell there was an easier way, but had no idea how to approach it.

why would you not run smod with sv_cheats 1??  :P


EDIT - also wanted to mention that perhaps a physics prop could be attached if SetParentAttachmentMaintainOffset were used. I haven't done any testing but my theory as to why the NPC can't move when a prop_physics is attached is because of vphysics. Maybe if the propr wasn't inside the NPC's bbox it could work?!
« Last Edit: September 05, 2013, 07:22:01 PM by BIZ »

Offline Rook

  • *
  • +2% chance to make useful post
  • Rook has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #13 on: September 05, 2013, 07:19:57 PM »
why wouldn't you run smod with sv_cheats 1??  :P

The only way to ride, true! ;) Didn't know if Zadaki was in the habit, though.

Offline Zadaki_L

  • *
  • +1% chance to make useful post
  • Zadaki_L has no influence.
Re: How to spawn prop_physics on npc death?
« Reply #14 on: September 05, 2013, 10:41:25 PM »
why wouldn't you run smod with sv_cheats 1??  :P

The only way to ride, true! ;) Didn't know if Zadaki was in the habit, though.

I think I turned it on long, long ago and actually put it in the config.cfg or whichever .cfg that was.

Also, you guys are way smarter than I am. I remember FORTRAN. I remember looking at photocopies of punchcards to write code so my programs would be backwards-compatible. But this stuff... I keep wanting to put "implicit none" at the front of each one.
« Last Edit: September 05, 2013, 10:44:30 PM by Zadaki_L »