new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / gamemodes / sandbox / gamemode / commands.lua

  1.  
  2. include( 'prop_tools.lua' )
  3.  
  4. /*---------------------------------------------------------
  5.    Name: CCSpawn
  6.    Desc: Console Command for a player to spawn different items
  7. ---------------------------------------------------------*/
  8. function CCSpawn( player, command, arguments )
  9.  
  10.         if ( arguments[1] == nil ) then return end
  11.         if ( !gamemode.Call( "PlayerSpawnObject", player ) ) then return end
  12.         if ( !util.IsValidModel( arguments[1] ) ) then return end
  13.        
  14.         local iSkin = arguments[2] or 0
  15.  
  16.         if ( util.IsValidProp( arguments[1] ) ) then
  17.        
  18.                 GMODSpawnProp( player, arguments[1], iSkin )
  19.                 return
  20.        
  21.         end
  22.        
  23.         if ( util.IsValidRagdoll( arguments[1] ) ) then
  24.        
  25.                 GMODSpawnRagdoll( player, arguments[1], iSkin )
  26.                 return
  27.        
  28.         end
  29.  
  30.         // Not a ragdoll or prop.. must be an 'effect' - spawn it as one
  31.         GMODSpawnEffect( player, arguments[1], iSkin )
  32.        
  33. end
  34.  
  35. /*---------------------------------------------------------
  36.  
  37. ---------------------------------------------------------*/
  38. local function MakeRagdoll( Player, Pos, Ang, Model, PhysicsObjects, Data )
  39.  
  40.         if not gamemode.Call( "PlayerSpawnRagdoll", Player, Model ) then return end
  41.         local Ent = ents.Create( "prop_ragdoll" )
  42.                 duplicator.DoGeneric( Ent, Data )
  43.         Ent:Spawn()
  44.        
  45.         duplicator.DoGenericPhysics( Ent, Player, Data )
  46.         duplicator.DoFlex( Ent, Data.Flex, Data.FlexScale )
  47.        
  48.         Ent:Activate()
  49.  
  50.         gamemode.Call( "PlayerSpawnedRagdoll", Player, Model, Ent )
  51.         return Ent     
  52. end
  53.  
  54. // Register the "prop_ragdoll" class with the duplicator, (Args in brackets will be retreived for every bone)
  55. duplicator.RegisterEntityClass( "prop_ragdoll", MakeRagdoll, "Pos", "Ang", "Model", "PhysicsObjects", "Data" )
  56.  
  57. /*---------------------------------------------------------
  58.    Name: GMODSpawnRagdoll - player spawns a ragdoll
  59. ---------------------------------------------------------*/
  60. function GMODSpawnRagdoll( player, model, iSkin )
  61.  
  62.         if ( !gamemode.Call( "PlayerSpawnRagdoll", player, model ) ) then return end
  63.         local e = DoPlayerEntitySpawn( player, "prop_ragdoll", model, iSkin )
  64.         gamemode.Call( "PlayerSpawnedRagdoll", player, model, e )
  65.        
  66.         undo.Create("Ragdoll")
  67.                 undo.SetPlayer(player)
  68.                 undo.AddEntity(e)
  69.         undo.Finish( "Ragdoll ("..tostring(model)..")" )
  70.        
  71.         player:AddCleanup( "ragdolls", e )
  72.  
  73. end
  74.  
  75.  
  76. function MakeProp( Player, Pos, Ang, Model, PhysicsObjects, Data )
  77.  
  78.         // Uck.
  79.         Data.Pos = Pos
  80.         Data.Angle = Ang
  81.         Data.Model = Model
  82.  
  83.         // Make sure this is allowed
  84.         if ( !gamemode.Call( "PlayerSpawnProp", Player, Model ) ) then return end
  85.        
  86.         local Prop = ents.Create( "prop_physics" )
  87.                 duplicator.DoGeneric( Prop, Data )
  88.         Prop:Spawn()
  89.        
  90.         duplicator.DoGenericPhysics( Prop, Player, Data )
  91.         duplicator.DoFlex( Prop, Data.Flex, Data.FlexScale )
  92.        
  93.         // Tell the gamemode we just spawned something
  94.         gamemode.Call( "PlayerSpawnedProp", Player, Model, Prop )
  95.        
  96.         FixInvalidPhysicsObject( Prop )
  97.        
  98.         DoPropSpawnedEffect( Prop )
  99.        
  100.         return Prop
  101.        
  102. end
  103.  
  104. duplicator.RegisterEntityClass( "prop_physics", MakeProp, "Pos", "Ang", "Model", "PhysicsObjects", "Data" )
  105.  
  106. /*---------------------------------------------------------
  107.    Name: FixInvalidPhysicsObject
  108.                         Attempts to detect and correct the physics object
  109.                         on models such as the TF2 Turrets
  110. ---------------------------------------------------------*/
  111. function FixInvalidPhysicsObject( Prop )
  112.  
  113.         local PhysObj = Prop:GetPhysicsObject()
  114.         if ( !PhysObj ) then return end
  115.        
  116.         local min, max = PhysObj:GetAABB()
  117.         if  ( !min || !max ) then return end
  118.        
  119.         local PhysSize = (min - max):Length()
  120.         if ( PhysSize > 5 ) then return end
  121.        
  122.         local min = Prop:OBBMins()     
  123.         local max = Prop:OBBMaxs()
  124.         if  ( !min || !max ) then return end
  125.        
  126.         local ModelSize = (min - max):Length()
  127.         local Difference = math.abs( ModelSize - PhysSize )
  128.         if ( Difference < 10 ) then return end
  129.  
  130.         // This physics object is definitiely weird.
  131.         // Make a new one.
  132.        
  133.         Prop:PhysicsInitBox( min, max )
  134.         Prop:SetCollisionGroup( COLLISION_GROUP_DEBRIS )
  135.        
  136.         local PhysObj = Prop:GetPhysicsObject()
  137.         if ( !PhysObj ) then return end
  138.        
  139.         PhysObj:SetMass( 100 )
  140.         PhysObj:Wake()
  141.  
  142. end
  143.  
  144. /*---------------------------------------------------------
  145.    Name: CCSpawnProp - player spawns a prop
  146. ---------------------------------------------------------*/
  147. function GMODSpawnProp( player, model, iSkin )
  148.  
  149.         if ( !gamemode.Call( "PlayerSpawnProp", player, model ) ) then return end
  150.         local e = DoPlayerEntitySpawn( player, "prop_physics", model, iSkin )
  151.         gamemode.Call( "PlayerSpawnedProp", player, model, e )
  152.  
  153.         // This didn't work out - todo: Find a better way.
  154.         //timer.Simple( 0.01, CheckPropSolid, e, COLLISION_GROUP_NONE, COLLISION_GROUP_WORLD )
  155.        
  156.         FixInvalidPhysicsObject( e )
  157.        
  158.         DoPropSpawnedEffect( e )
  159.  
  160.         undo.Create("Prop")
  161.                 undo.SetPlayer(player)
  162.                 undo.AddEntity(e)
  163.         undo.Finish( "Prop ("..tostring(model)..")" )
  164.        
  165.         player:AddCleanup( "props", e )
  166.  
  167. end
  168.  
  169. /*---------------------------------------------------------
  170.    Name: GMODSpawnEffect
  171. ---------------------------------------------------------*/
  172. function GMODSpawnEffect( player, model, iSkin )
  173.  
  174.         if ( !gamemode.Call( "PlayerSpawnEffect", player, model ) ) then return end
  175.         local e = DoPlayerEntitySpawn( player, "prop_effect", model, iSkin )
  176.         gamemode.Call( "PlayerSpawnedEffect", player, model, e )
  177.        
  178.         undo.Create("Effect")
  179.                 undo.SetPlayer(player)
  180.                 undo.AddEntity(e)
  181.         undo.Finish( "Effect ("..tostring(model)..")" )
  182.        
  183.         player:AddCleanup( "effects", e )
  184.  
  185. end
  186.  
  187. /*---------------------------------------------------------
  188.    Name: DoPlayerEntitySpawn
  189.    Desc: Utility function for player entity spawning functions
  190. ---------------------------------------------------------*/
  191. function DoPlayerEntitySpawn( player, entity_name, model, iSkin )
  192.  
  193.         local vStart = player:GetShootPos()
  194.         local vForward = player:GetAimVector()
  195.  
  196.         local trace = {}
  197.         trace.start = vStart
  198.         trace.endpos = vStart + (vForward * 2048)
  199.         trace.filter = player
  200.        
  201.         local tr = util.TraceLine( trace )
  202.  
  203.         // PrintTable( tr )
  204.  
  205.         // Prevent spawning too close
  206.         //if ( !tr.Hit || tr.Fraction < 0.05 ) then
  207.         //      return
  208.         //end
  209.        
  210.         local ent = ents.Create( entity_name )
  211.         if ( !ent:IsValid() ) then return end
  212.  
  213.         local ang = player:EyeAngles()
  214.         ang.yaw = ang.yaw + 180 // Rotate it 180 degrees in my favour
  215.         ang.roll = 0
  216.         ang.pitch = 0
  217.        
  218.         if (entity_name == "prop_ragdoll") then
  219.                 ang.pitch = -90
  220.                 tr.HitPos = tr.HitPos
  221.         end
  222.        
  223.         ent:SetModel( model )
  224.         ent:SetSkin( iSkin )
  225.         ent:SetAngles( ang )
  226.         ent:SetPos( tr.HitPos )
  227.         ent:Spawn()
  228.         ent:Activate()
  229.  
  230.         // Attempt to move the object so it sits flush
  231.         // We could do a TraceEntity instead of doing all
  232.         // of this - but it feels off after the old way
  233.  
  234.         local vFlushPoint = tr.HitPos - ( tr.HitNormal * 512 )  // Find a point that is definitely out of the object in the direction of the floor
  235.                 vFlushPoint = ent:NearestPoint( vFlushPoint )                   // Find the nearest point inside the object to that point
  236.                 vFlushPoint = ent:GetPos() - vFlushPoint                                // Get the difference
  237.                 vFlushPoint = tr.HitPos + vFlushPoint                                   // Add it to our target pos
  238.                                                                                
  239.         if (entity_name != "prop_ragdoll") then
  240.        
  241.                 // Set new position
  242.                 ent:SetPos( vFlushPoint )
  243.                 player:SendLua( "achievements.SpawnedProp()" );
  244.        
  245.         else
  246.        
  247.                 // With ragdolls we need to move each physobject
  248.                 local VecOffset = vFlushPoint - ent:GetPos()
  249.                 for i=0, ent:GetPhysicsObjectCount()-1 do
  250.                         local phys = ent:GetPhysicsObjectNum( i )
  251.                         phys:SetPos( phys:GetPos() + VecOffset )
  252.                 end
  253.                
  254.                 player:SendLua( "achievements.SpawnedRagdoll()" );
  255.                
  256.         end
  257.  
  258.         return ent
  259.        
  260. end
  261.  
  262.  
  263. concommand.Add( "gm_spawn", CCSpawn )
  264.  
  265.  
  266. local function SetAdditionalEquipment( Player, NPC )
  267.  
  268.         local WeaponName = Player:GetInfo( "gmod_npc_weapon" )
  269.         if ( !WeaponName or WeaponName == "" or WeaponName == "none" ) then return end
  270.  
  271.         // Make sure the weapon is in the allowed list!
  272.         local WeaponList = list.Get( "NPCWeapons" )
  273.         if ( !WeaponList[ WeaponName ] ) then return end
  274.  
  275.         NPC:SetKeyValue( "additionalequipment", WeaponName )
  276.  
  277. end
  278.  
  279. local function InternalSpawnNPC( Player, Position, Normal, Class )
  280.  
  281.         local NPCList = list.Get( "NPC" )
  282.         local NPCData = NPCList[ Class ]
  283.        
  284.         // Don't let them spawn this entity if it isn't in our NPC Spawn list.
  285.         // We don't want them spawning any entity they like!
  286.         if ( !NPCData ) then
  287.                 Player:SendLua( "Derma_Message( \"Sorry! You can't spawn that NPC!\" )" );
  288.         return end
  289.        
  290.         local bDropToFloor = false
  291.                
  292.         //
  293.         // This NPC has to be spawned on a ceiling ( Barnacle )
  294.         //
  295.         if ( NPCData.OnCeiling && Vector( 0, 0, -1 ):Dot( Normal ) < 0.95 ) then
  296.                 return nil
  297.         end
  298.        
  299.         //
  300.         // This NPC has to be spawned on a floor ( Turrets )
  301.         //
  302.         if ( NPCData.OnFloor && Vector( 0, 0, 1 ):Dot( Normal ) < 0.95 ) then
  303.                 return nil
  304.         else
  305.                 bDropToFloor = true
  306.         end
  307.        
  308.        
  309.         //
  310.         // Offset the position
  311.         //
  312.         local Offset = NPCData.Offset or 32
  313.         Position = Position + Normal * Offset
  314.        
  315.        
  316.         // Create NPC
  317.         local NPC = ents.Create( NPCData.Class )
  318.         if ( !ValidEntity( NPC ) ) then return end
  319.  
  320.         NPC:SetPos( Position )
  321.        
  322.         // Rotate to face player (expected behaviour)
  323.         local Angles = Player:GetAngles()
  324.                 Angles.pitch = 0
  325.                 Angles.roll = 0
  326.                 Angles.yaw = Angles.yaw + 180
  327.  
  328.         if ( NPCData.Rotate ) then Angles = Angles + NPCData.Rotate end
  329.                
  330.         NPC:SetAngles( Angles )
  331.        
  332.         //
  333.         // This NPC has a special model we want to define
  334.         //
  335.         if ( NPCData.Model ) then
  336.                 NPC:SetModel( NPCData.Model )
  337.         end
  338.        
  339.         //
  340.         // Spawn Flags
  341.         //
  342.         local SpawnFlags = SF_NPC_FADE_CORPSE | SF_NPC_ALWAYSTHINK
  343.         if ( NPCData.SpawnFlags ) then SpawnFlags = SpawnFlags | NPCData.SpawnFlags end
  344.         if ( NPCData.TotalSpawnFlags ) then SpawnFlags = NPCData.TotalSpawnFlags end
  345.         NPC:SetKeyValue( "spawnflags", SpawnFlags )
  346.        
  347.         //
  348.         // Optional Key Values
  349.         //
  350.         if ( NPCData.KeyValues ) then
  351.                 for k, v in pairs( NPCData.KeyValues ) do
  352.                         NPC:SetKeyValue( k, v )
  353.                 end            
  354.         end
  355.        
  356.         //
  357.         // This NPC has a special skin we want to define
  358.         //
  359.         if ( NPCData.Skin ) then
  360.                 NPC:SetSkin( NPCData.Skin )
  361.         end
  362.        
  363.         //
  364.         // What weapon should this mother be carrying
  365.         //
  366.         SetAdditionalEquipment( Player, NPC )
  367.        
  368.        
  369.         NPC:Spawn()
  370.         NPC:Activate()
  371.        
  372.         if ( bDropToFloor && !NPCData.OnCeiling ) then
  373.                 NPC:DropToFloor()      
  374.         end
  375.        
  376.         return NPC
  377.        
  378. end
  379.  
  380. function CCSpawnNPC( player, command, arguments )
  381.  
  382.         local NPCClassName = arguments[1]
  383.         if ( !NPCClassName ) then return end
  384.        
  385.         local WeaponName = player:GetInfo( "gmod_npc_weapon" )
  386.  
  387.         // Give the gamemode an opportunity to deny spawning
  388.         if ( !gamemode.Call( "PlayerSpawnNPC", player, NPCClassName, WeaponName ) ) then return end
  389.        
  390.         local vStart = player:GetShootPos()
  391.         local vForward = player:GetAimVector()
  392.        
  393.         local trace = {}
  394.                 trace.start = vStart
  395.                 trace.endpos = vStart + vForward * 2048
  396.                 trace.filter = player
  397.  
  398.         local tr = util.TraceLine( trace )
  399.        
  400.         // Create the NPC is you can.
  401.         local SpawnedNPC = InternalSpawnNPC( player, tr.HitPos, tr.HitNormal, NPCClassName )
  402.         if ( !ValidEntity( SpawnedNPC ) ) then return end
  403.  
  404.         // Give the gamemode an opportunity to do whatever
  405.         gamemode.Call( "PlayerSpawnedNPC", player, SpawnedNPC )
  406.        
  407.         // See if we can find a nice name for this NPC..
  408.         local NPCList = list.Get( "NPC" )
  409.         local NiceName = nil
  410.         if ( NPCList[ NPCClassName ] ) then
  411.                 NiceName = NPCList[ NPCClassName ].Name
  412.         end
  413.  
  414.         // Add to undo list
  415.         undo.Create("NPC")
  416.                 undo.SetPlayer( player )
  417.                 undo.AddEntity( SpawnedNPC )
  418.                 if ( NiceName ) then
  419.                         undo.SetCustomUndoText( "Undone "..NiceName )
  420.                 end
  421.         undo.Finish( "NPC ("..tostring(arguments[1])..")" )
  422.        
  423.         // And cleanup
  424.         player:AddCleanup( "npcs", SpawnedNPC )
  425.        
  426.         player:SendLua( "achievements.SpawnedNPC()" );
  427.  
  428. end
  429.  
  430. concommand.Add( "gmod_spawnnpc", CCSpawnNPC )
  431.  
  432.  
  433. local function GenericNPCDuplicator( Player, Model, Class, Equipment, SpawnFlags, Data )
  434.  
  435.         if ( !gamemode.Call( "PlayerSpawnNPC", Player, Class, Equipment ) ) then return end
  436.  
  437.         local Entity = InternalSpawnNPC( Player, Data.Pos, Vector(0,0,1), Class, Equipment, SpawnFlags )
  438.        
  439.         if ( Entity && Entity:IsValid()) then
  440.  
  441.                 Entity:SetModel( Model )
  442.                 Entity:SetAngles( Data.Angle )
  443.                 gamemode.Call( "PlayerSpawnedNPC", Player, Entity )
  444.                 Player:AddCleanup( "npcs", Entity )
  445.                 table.Add( Entity:GetTable(), Data )
  446.                
  447.         end
  448.        
  449.         return Entity
  450.        
  451. end
  452.  
  453. // Huuuuuuuuhhhh
  454. duplicator.RegisterEntityClass( "npc_alyx", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  455. duplicator.RegisterEntityClass( "npc_antlion", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  456. duplicator.RegisterEntityClass( "npc_antlionguard", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  457. duplicator.RegisterEntityClass( "npc_barnacle", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  458. duplicator.RegisterEntityClass( "npc_barney", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  459. duplicator.RegisterEntityClass( "npc_breen", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  460. duplicator.RegisterEntityClass( "npc_combine_s", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  461. duplicator.RegisterEntityClass( "npc_combine_p", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  462. duplicator.RegisterEntityClass( "npc_combine_e", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  463. duplicator.RegisterEntityClass( "npc_crow", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  464. duplicator.RegisterEntityClass( "npc_cscanner", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  465. duplicator.RegisterEntityClass( "npc_dog", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  466. duplicator.RegisterEntityClass( "npc_eli", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  467. duplicator.RegisterEntityClass( "npc_fastzombie", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  468. duplicator.RegisterEntityClass( "npc_gman", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  469. duplicator.RegisterEntityClass( "npc_headcrab", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  470. duplicator.RegisterEntityClass( "npc_headcrab_black", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  471. duplicator.RegisterEntityClass( "npc_headcrab_fast", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  472. duplicator.RegisterEntityClass( "npc_kleiner", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  473. duplicator.RegisterEntityClass( "npc_manhack", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  474. duplicator.RegisterEntityClass( "npc_metropolice", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  475. duplicator.RegisterEntityClass( "npc_monk", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  476. duplicator.RegisterEntityClass( "npc_mossman", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  477. duplicator.RegisterEntityClass( "npc_pigeon", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  478. duplicator.RegisterEntityClass( "npc_rollermine", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  479. duplicator.RegisterEntityClass( "npc_seagull", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  480. duplicator.RegisterEntityClass( "npc_zombie", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  481. duplicator.RegisterEntityClass( "npc_zombie_torso", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  482. duplicator.RegisterEntityClass( "npc_citizen_rebel", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  483. duplicator.RegisterEntityClass( "npc_citizen", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  484. duplicator.RegisterEntityClass( "npc_citizen_dt", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  485. duplicator.RegisterEntityClass( "npc_citizen_medic", GenericNPCDuplicator, "Model", "Class", "Equipment", "SpawnFlags", "Data"  )
  486.  
  487.  
  488. /*---------------------------------------------------------
  489.    Name: CanPlayerSpawnSENT
  490. ---------------------------------------------------------*/
  491. local function CanPlayerSpawnSENT( player, EntityName )
  492.  
  493.         // Make sure this is a SWEP
  494.         local sent = scripted_ents.GetStored( EntityName )
  495.         if (sent == nil) then
  496.        
  497.                 // Is this in the SpawnableEntities list?
  498.                 local SpawnableEntities = list.Get( "SpawnableEntities" )
  499.                 if (!SpawnableEntities) then return false end
  500.                 local EntTable = SpawnableEntities[ EntityName ]
  501.                 if (!EntTable) then return false end
  502.                 if ( EntTable.AdminOnly && !player:IsAdmin() ) then return false end
  503.                
  504.                 return true
  505.        
  506.         end
  507.  
  508.         local sent = sent.t
  509.        
  510.         // We need a spawn function. The SENT can then spawn itself properly
  511.         if (!sent.SpawnFunction) then return false end
  512.        
  513.         // You're not allowed to spawn this unless you're an admin!
  514.         if ( !sent.Spawnable && !player:IsAdmin() ) then return false end
  515.         if ( sent.AdminOnly && !player:IsAdmin() ) then return false end
  516.        
  517.         return true
  518.        
  519. end
  520.  
  521. /*---------------------------------------------------------
  522.    Name: CCSpawnSENT
  523.    Desc: Console Command for a player to spawn different items
  524. ---------------------------------------------------------*/
  525.  
  526. function CCSpawnSENT( player, command, arguments )
  527.  
  528.         local EntityName = arguments[1]
  529.         if ( EntityName == nil ) then return end
  530.        
  531.         if ( !CanPlayerSpawnSENT( player, EntityName ) ) then return end
  532.        
  533.         // Ask the gamemode if it's ok to spawn this
  534.         if ( !gamemode.Call( "PlayerSpawnSENT", player, EntityName ) ) then return end
  535.        
  536.         local vStart = player:GetShootPos()
  537.         local vForward = player:GetAimVector()
  538.        
  539.         local trace = {}
  540.         trace.start = vStart
  541.         trace.endpos = vStart + (vForward * 2048)
  542.         trace.filter = player
  543.        
  544.         local tr = util.TraceLine( trace )
  545.        
  546.         local entity = nil
  547.         local PrintName = nil
  548.         local sent = scripted_ents.GetStored( EntityName )
  549.         if ( sent ) then
  550.        
  551.                 local sent = sent.t
  552.                 entity = sent:SpawnFunction( player, tr )
  553.                 PrintName = sent.PrintName
  554.        
  555.         else
  556.        
  557.                 // Spawn from list table
  558.                 local SpawnableEntities = list.Get( "SpawnableEntities" )
  559.                 if (!SpawnableEntities) then return end
  560.                 local EntTable = SpawnableEntities[ EntityName ]
  561.                 if (!EntTable) then return end
  562.                
  563.                 PrintName = EntTable.PrintName
  564.                
  565.                 local SpawnPos = tr.HitPos + tr.HitNormal * 16
  566.                 if ( EntTable.NormalOffset ) then SpawnPos = SpawnPos + tr.HitNormal * EntTable.NormalOffset end
  567.        
  568.                 entity = ents.Create( EntTable.ClassName )
  569.                         entity:SetPos( SpawnPos )
  570.                 entity:Spawn()
  571.                 entity:Activate()
  572.                
  573.                 if ( EntTable.DropToFloor ) then
  574.                         entity:DropToFloor()
  575.                 end
  576.        
  577.         end
  578.        
  579.  
  580.         if ( ValidEntity( entity ) ) then
  581.        
  582.                 gamemode.Call( "PlayerSpawnedSENT", player, entity )
  583.                
  584.                 undo.Create("SENT")
  585.                         undo.SetPlayer(player)
  586.                         undo.AddEntity(entity)
  587.                         if ( PrintName ) then
  588.                                 undo.SetCustomUndoText( "Undone "..PrintName )
  589.                         end
  590.                 undo.Finish( "Scripted Entity ("..tostring( EntityName )..")" )
  591.                
  592.                 player:AddCleanup( "sents", entity )           
  593.                 entity:SetVar( "Player", player )
  594.        
  595.         end
  596.        
  597.        
  598. end
  599.  
  600. concommand.Add( "gm_spawnsent", CCSpawnSENT )
  601.  
  602. /*---------------------------------------------------------
  603.         // Give a swep.. duh.
  604. ---------------------------------------------------------*/
  605. function CCGiveSWEP( player, command, arguments )
  606.  
  607.         if ( arguments[1] == nil ) then return end
  608.  
  609.         // Make sure this is a SWEP
  610.         local swep = weapons.GetStored( arguments[1] )
  611.         if (swep == nil) then return end
  612.        
  613.         // You're not allowed to spawn this!
  614.         if ( !swep.Spawnable && !player:IsAdmin() ) then
  615.                 return
  616.         end
  617.        
  618.         if ( !gamemode.Call( "PlayerGiveSWEP", player, arguments[1], swep ) ) then return end
  619.        
  620.         MsgAll( "Giving "..player:Nick().." a "..swep.Classname.."\n" )
  621.         player:Give( swep.Classname )
  622.        
  623.         // And switch to it
  624.         player:SelectWeapon( swep.Classname )
  625.        
  626. end
  627.  
  628. concommand.Add( "gm_giveswep", CCGiveSWEP )
  629.  
  630. /*---------------------------------------------------------
  631.         // Give a swep.. duh.
  632. ---------------------------------------------------------*/
  633. function CCSpawnSWEP( player, command, arguments )
  634.  
  635.         if ( arguments[1] == nil ) then return end
  636.  
  637.         // Make sure this is a SWEP
  638.         local swep = weapons.GetStored( arguments[1] )
  639.         if (swep == nil) then return end
  640.        
  641.         // You're not allowed to spawn this!
  642.         if ( !swep.Spawnable && !player:IsAdmin() ) then
  643.                 return
  644.         end
  645.        
  646.         if ( !gamemode.Call( "PlayerSpawnSWEP", player, arguments[1], swep ) ) then return end
  647.        
  648.         local tr = player:GetEyeTraceNoCursor()
  649.  
  650.         if ( !tr.Hit ) then return end
  651.        
  652.         local entity = ents.Create( swep.Classname )
  653.        
  654.         if ( ValidEntity( entity ) ) then
  655.        
  656.                 entity:SetPos( tr.HitPos + tr.HitNormal * 32 )
  657.                 entity:Spawn()
  658.        
  659.         end
  660.        
  661. end
  662.  
  663. concommand.Add( "gm_spawnswep", CCSpawnSWEP )
  664.  
  665.  
  666. local function MakeVehicle( Player, Pos, Ang, Model, Class, VName, VTable )
  667.  
  668.         if (!gamemode.Call( "PlayerSpawnVehicle", Player, Model, VName, VTable )) then return end
  669.        
  670.         local Ent = ents.Create( Class )
  671.         if (!Ent) then return NULL end
  672.        
  673.         Ent:SetModel( Model )
  674.        
  675.         // Fill in the keyvalues if we have them
  676.         if ( VTable && VTable.KeyValues ) then
  677.                 for k, v in pairs( VTable.KeyValues ) do
  678.                         Ent:SetKeyValue( k, v )
  679.                 end            
  680.         end
  681.                
  682.         Ent:SetAngles( Ang )
  683.         Ent:SetPos( Pos )
  684.                
  685.         Ent:Spawn()
  686.         Ent:Activate()
  687.        
  688.         Ent.VehicleName         = VName
  689.         Ent.VehicleTable        = VTable
  690.        
  691.         // We need to override the class in the case of the Jeep, because it
  692.         // actually uses a different class than is reported by GetClass
  693.         Ent.ClassOverride       = Class
  694.  
  695.         gamemode.Call( "PlayerSpawnedVehicle", Player, Ent )
  696.         return Ent     
  697.        
  698. end
  699.  
  700. duplicator.RegisterEntityClass( "prop_vehicle_jeep_old",                MakeVehicle, "Pos", "Ang", "Model", "Class", "VehicleName", "VehicleTable" )
  701. duplicator.RegisterEntityClass( "prop_vehicle_jeep",                    MakeVehicle, "Pos", "Ang", "Model", "Class", "VehicleName", "VehicleTable" )
  702. duplicator.RegisterEntityClass( "prop_vehicle_airboat",                 MakeVehicle, "Pos", "Ang", "Model", "Class", "VehicleName", "VehicleTable" )
  703. duplicator.RegisterEntityClass( "prop_vehicle_prisoner_pod",    MakeVehicle, "Pos", "Ang", "Model", "Class", "VehicleName", "VehicleTable" )
  704.  
  705.  
  706. /*---------------------------------------------------------
  707.    Name: CCSpawnVehicle
  708.    Desc: Player attempts to spawn vehicle
  709. ---------------------------------------------------------*/
  710. function CCSpawnVehicle( Player, command, arguments )
  711.  
  712.         if ( arguments[1] == nil ) then return end
  713.                
  714.         local vname = arguments[1]
  715.         local VehicleList = list.Get( "Vehicles" )
  716.         local vehicle = VehicleList[ vname ]
  717.        
  718.         // Not a valid vehicle to be spawning..
  719.         if ( !vehicle ) then return end
  720.        
  721.         local tr = Player:GetEyeTraceNoCursor()
  722.        
  723.         local Angles = Player:GetAngles()
  724.                 Angles.pitch = 0
  725.                 Angles.roll = 0
  726.                 Angles.yaw = Angles.yaw + 180
  727.        
  728.         local Ent = MakeVehicle( Player, tr.HitPos, Angles, vehicle.Model, vehicle.Class, vname, vehicle )
  729.         if ( !ValidEntity( Ent ) ) then return end
  730.        
  731.         if ( vehicle.Members ) then
  732.                 table.Merge( Ent, vehicle.Members )
  733.                 duplicator.StoreEntityModifier( Ent, "VehicleMemDupe", vehicle.Members );
  734.         end
  735.        
  736.         undo.Create( "Vehicle" )
  737.                 undo.SetPlayer( Player )
  738.                 undo.AddEntity( Ent )
  739.                 undo.SetCustomUndoText( "Undone "..vehicle.Name )
  740.         undo.Finish( "Vehicle ("..tostring( vehicle.Name )..")" )
  741.        
  742.         Player:AddCleanup( "vehicles", Ent )
  743.        
  744. end
  745.  
  746. concommand.Add( "gm_spawnvehicle", CCSpawnVehicle )
  747.  
  748.  
  749. local function VehicleMemDupe( Player, Entity, Data )
  750.  
  751.     table.Merge( Entity, Data );
  752.        
  753. end
  754. duplicator.RegisterEntityModifier( "VehicleMemDupe", VehicleMemDupe );
  755.