/*---------------------------------------------------------
Name: CCSpawn
Desc: Console Command for a player to spawn different items
---------------------------------------------------------*/
function CCSpawn( player, command, arguments
)
if ( arguments[1] == nil ) then return end
if ( !gamemode.Call( "PlayerSpawnObject", player
) ) then return end
local iSkin = arguments[2] or 0
return
end
return
end
// Not a ragdoll or prop.. must be an 'effect' - spawn it as one
end
/*---------------------------------------------------------
---------------------------------------------------------*/
local function MakeRagdoll
( Player, Pos, Ang,
Model, PhysicsObjects, Data
)
return Ent
end
// Register the "prop_ragdoll" class with the duplicator, (Args in brackets will be retreived for every bone)
/*---------------------------------------------------------
Name: GMODSpawnRagdoll - player spawns a ragdoll
---------------------------------------------------------*/
if ( !gamemode.Call( "PlayerSpawnRagdoll", player, model
) ) then return end
end
// Uck.
Data.Pos = Pos
// Make sure this is allowed
// Tell the gamemode we just spawned something
return Prop
end
/*---------------------------------------------------------
Name: FixInvalidPhysicsObject
Attempts to detect and correct the physics object
on models such as the TF2 Turrets
---------------------------------------------------------*/
if ( !PhysObj ) then return end
if ( !min || !max ) then return end
local PhysSize
= (min - max
):
Length()
if ( PhysSize > 5 ) then return end
if ( !min || !max ) then return end
local ModelSize
= (min - max
):
Length()
local Difference
= math.abs( ModelSize - PhysSize
)
if ( Difference < 10 ) then return end
// This physics object is definitiely weird.
// Make a new one.
if ( !PhysObj ) then return end
end
/*---------------------------------------------------------
Name: CCSpawnProp - player spawns a prop
---------------------------------------------------------*/
if ( !gamemode.Call( "PlayerSpawnProp", player, model
) ) then return end
// This didn't work out - todo: Find a better way.
//timer.Simple( 0.01, CheckPropSolid, e, COLLISION_GROUP_NONE, COLLISION_GROUP_WORLD )
end
/*---------------------------------------------------------
Name: GMODSpawnEffect
---------------------------------------------------------*/
if ( !gamemode.Call( "PlayerSpawnEffect", player, model
) ) then return end
end
/*---------------------------------------------------------
Name: DoPlayerEntitySpawn
Desc: Utility function for player entity spawning functions
---------------------------------------------------------*/
local trace = {}
trace.start = vStart
trace.endpos = vStart + (vForward * 2048)
trace.filter = player
// PrintTable( tr )
// Prevent spawning too close
//if ( !tr.Hit || tr.Fraction < 0.05 ) then
// return
//end
if ( !ent:
IsValid() ) then return end
ang.yaw = ang.yaw + 180 // Rotate it 180 degrees in my favour
ang.roll = 0
ang.pitch = 0
if (entity_name == "prop_ragdoll") then
ang.pitch = -90
tr.HitPos = tr.HitPos
end
// Attempt to move the object so it sits flush
// We could do a TraceEntity instead of doing all
// of this - but it feels off after the old way
local vFlushPoint = tr.HitPos - ( tr.HitNormal * 512 ) // Find a point that is definitely out of the object in the direction of the floor
vFlushPoint
= ent:
NearestPoint( vFlushPoint
) // Find the nearest point inside the object to that point
vFlushPoint
= ent:
GetPos() - vFlushPoint
// Get the difference
vFlushPoint = tr.HitPos + vFlushPoint // Add it to our target pos
if (entity_name != "prop_ragdoll") then
// Set new position
player:
SendLua( "achievements.SpawnedProp()" );
else
// With ragdolls we need to move each physobject
local VecOffset
= vFlushPoint - ent:
GetPos()
end
player:
SendLua( "achievements.SpawnedRagdoll()" );
end
return ent
end
local function SetAdditionalEquipment
( Player, NPC
)
if ( !WeaponName or WeaponName == "" or WeaponName == "none" ) then return end
// Make sure the weapon is in the allowed list!
local WeaponList
= list.Get( "NPCWeapons" )
if ( !WeaponList[ WeaponName ] ) then return end
end
local function InternalSpawnNPC
( Player, Position, Normal, Class
)
local NPCData = NPCList[ Class ]
// Don't let them spawn this entity if it isn't in our NPC Spawn list.
// We don't want them spawning any entity they like!
if ( !NPCData ) then
Player:
SendLua( "Derma_Message( \"Sorry! You can't spawn that NPC!\" )" );
return end
local bDropToFloor = false
//
// This NPC has to be spawned on a ceiling ( Barnacle )
//
if ( NPCData.OnCeiling
&& Vector( 0,
0, -
1 ):
Dot( Normal
) < 0.95 ) then
return nil
end
//
// This NPC has to be spawned on a floor ( Turrets )
//
if ( NPCData.OnFloor
&& Vector( 0,
0,
1 ):
Dot( Normal
) < 0.95 ) then
return nil
else
bDropToFloor = true
end
//
// Offset the position
//
local Offset = NPCData.Offset or 32
Position = Position + Normal * Offset
// Create NPC
// Rotate to face player (expected behaviour)
Angles.pitch = 0
Angles.roll = 0
Angles.yaw = Angles.yaw + 180
if ( NPCData.
Rotate ) then Angles
= Angles + NPCData.
Rotate end
//
// This NPC has a special model we want to define
//
if ( NPCData.
Model ) then
end
//
// Spawn Flags
//
local SpawnFlags = SF_NPC_FADE_CORPSE | SF_NPC_ALWAYSTHINK
if ( NPCData.SpawnFlags ) then SpawnFlags = SpawnFlags | NPCData.SpawnFlags end
if ( NPCData.TotalSpawnFlags ) then SpawnFlags = NPCData.TotalSpawnFlags end
//
// Optional Key Values
//
if ( NPCData.KeyValues ) then
for k, v
in pairs( NPCData.KeyValues
) do
end
end
//
// This NPC has a special skin we want to define
//
if ( NPCData.Skin ) then
end
//
// What weapon should this mother be carrying
//
SetAdditionalEquipment
( Player, NPC
)
if ( bDropToFloor && !NPCData.OnCeiling ) then
end
return NPC
end
local NPCClassName = arguments[1]
if ( !NPCClassName ) then return end
local WeaponName
= player:
GetInfo( "gmod_npc_weapon" )
// Give the gamemode an opportunity to deny spawning
if ( !gamemode.Call( "PlayerSpawnNPC", player, NPCClassName, WeaponName
) ) then return end
local trace = {}
trace.start = vStart
trace.endpos = vStart + vForward * 2048
trace.filter = player
// Create the NPC is you can.
local SpawnedNPC = InternalSpawnNPC( player, tr.HitPos, tr.HitNormal, NPCClassName )
// Give the gamemode an opportunity to do whatever
// See if we can find a nice name for this NPC..
local NiceName = nil
if ( NPCList[ NPCClassName ] ) then
NiceName
= NPCList
[ NPCClassName
].
Name
end
// Add to undo list
if ( NiceName ) then
end
// And cleanup
player:
SendLua( "achievements.SpawnedNPC()" );
end
local function GenericNPCDuplicator
( Player,
Model, Class, Equipment, SpawnFlags, Data
)
local Entity = InternalSpawnNPC
( Player, Data.Pos,
Vector(0,
0,
1), Class, Equipment, SpawnFlags
)
end
end
// Huuuuuuuuhhhh
/*---------------------------------------------------------
Name: CanPlayerSpawnSENT
---------------------------------------------------------*/
local function CanPlayerSpawnSENT( player, EntityName )
// Make sure this is a SWEP
if (sent == nil) then
// Is this in the SpawnableEntities list?
local SpawnableEntities
= list.Get( "SpawnableEntities" )
if (!SpawnableEntities) then return false end
local EntTable = SpawnableEntities[ EntityName ]
if (!EntTable) then return false end
if ( EntTable.AdminOnly
&& !player:
IsAdmin() ) then return false end
return true
end
local sent = sent.t
// We need a spawn function. The SENT can then spawn itself properly
if (!sent.SpawnFunction) then return false end
// You're not allowed to spawn this unless you're an admin!
if ( !sent.Spawnable
&& !player:
IsAdmin() ) then return false end
if ( sent.AdminOnly
&& !player:
IsAdmin() ) then return false end
return true
end
/*---------------------------------------------------------
Name: CCSpawnSENT
Desc: Console Command for a player to spawn different items
---------------------------------------------------------*/
local EntityName = arguments[1]
if ( EntityName == nil ) then return end
if ( !CanPlayerSpawnSENT( player, EntityName ) ) then return end
// Ask the gamemode if it's ok to spawn this
if ( !gamemode.Call( "PlayerSpawnSENT", player, EntityName
) ) then return end
local trace = {}
trace.start = vStart
trace.endpos = vStart + (vForward * 2048)
trace.filter = player
local entity = nil
local PrintName = nil
if ( sent ) then
local sent = sent.t
entity = sent:SpawnFunction( player, tr )
PrintName = sent.PrintName
else
// Spawn from list table
local SpawnableEntities
= list.Get( "SpawnableEntities" )
if (!SpawnableEntities) then return end
local EntTable = SpawnableEntities[ EntityName ]
if (!EntTable) then return end
PrintName = EntTable.PrintName
local SpawnPos = tr.HitPos + tr.HitNormal * 16
if ( EntTable.NormalOffset ) then SpawnPos = SpawnPos + tr.HitNormal * EntTable.NormalOffset end
end
end
if ( PrintName ) then
end
entity:
SetVar( "Player", player
)
end
end
/*---------------------------------------------------------
// Give a swep.. duh.
---------------------------------------------------------*/
if ( arguments[1] == nil ) then return end
// Make sure this is a SWEP
if (swep == nil) then return end
// You're not allowed to spawn this!
if ( !swep.Spawnable
&& !player:
IsAdmin() ) then
return
end
if ( !gamemode.Call( "PlayerGiveSWEP", player, arguments
[1], swep
) ) then return end
MsgAll( "Giving "..player:
Nick()..
" a "..swep.Classname..
"\n" )
player:
Give( swep.Classname
)
// And switch to it
end
/*---------------------------------------------------------
// Give a swep.. duh.
---------------------------------------------------------*/
if ( arguments[1] == nil ) then return end
// Make sure this is a SWEP
if (swep == nil) then return end
// You're not allowed to spawn this!
if ( !swep.Spawnable
&& !player:
IsAdmin() ) then
return
end
if ( !gamemode.Call( "PlayerSpawnSWEP", player, arguments
[1], swep
) ) then return end
if ( !tr.Hit ) then return end
entity:
SetPos( tr.HitPos + tr.HitNormal
* 32 )
end
end
local function MakeVehicle
( Player, Pos, Ang,
Model, Class, VName, VTable
)
if (!Ent) then return NULL end
// Fill in the keyvalues if we have them
if ( VTable && VTable.KeyValues ) then
for k, v
in pairs( VTable.KeyValues
) do
end
end
Ent.VehicleName = VName
Ent.VehicleTable = VTable
// We need to override the class in the case of the Jeep, because it
// actually uses a different class than is reported by GetClass
Ent.ClassOverride = Class
return Ent
end
/*---------------------------------------------------------
Name: CCSpawnVehicle
Desc: Player attempts to spawn vehicle
---------------------------------------------------------*/
if ( arguments[1] == nil ) then return end
local vname = arguments[1]
local VehicleList
= list.Get( "Vehicles" )
local vehicle = VehicleList[ vname ]
// Not a valid vehicle to be spawning..
if ( !vehicle ) then return end
Angles.pitch = 0
Angles.roll = 0
Angles.yaw = Angles.yaw + 180
local Ent
= MakeVehicle
( Player, tr.HitPos, Angles, vehicle.
Model, vehicle.Class, vname, vehicle
)
if ( vehicle.Members ) then
end
end
end