• Print

Author Topic: Restrict Certain Props  (Read 7857 times)

0 Members and 1 Guest are viewing this topic.

Offline Thud13

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Restrict Certain Props
« on: February 19, 2007, 12:25:29 pm »
Was just curious- people on my server have the new hobby of crashing my server with explosive barrels. Its annoying, and I can't do anything about it.

And I was wondering if this feature was already built in:

Is it possible to create a way to limit the amount of explosive barrels that can be created? 10 Barrels max IMO would be a good number.

If it doesn't have this feature built in, could anyone point me in the right direction to code up something simple? Its really becoming an issue fast.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Restrict Certain Props
« Reply #1 on: February 19, 2007, 12:53:00 pm »
Here's a very basic one..

Code: Lua
  1. local models = {}
  2.  
  3. function restrictProp( prop, number )
  4.         models[ prop ] = number
  5. end
  6.  
  7. local function blockProps( ply, mdl )
  8.         if not models[ mdl ] then return end -- Not blocked
  9.         ply.mdlCount = ply.mdlCount or {}
  10.         ply.mdlCount[ mdl ] = ply.mdlCount[ mdl ] or 0
  11.         if ply.mdlCount[ mdl ] >= models[ mdl ] then ply:PrintMessage( 3, "You're over your limit for this prop!" ) return false end
  12.         ply.mdlCount[ mdl ] = ply.mdlCount[ mdl ] + 1
  13. end
  14. hook.Add( "PlayerSpawnProp", "blockProps", blockProps )
  15. restrictProp( "models/props_c17/oildrum001_explosive.mdl", 10 )
  16.  

Doh, just realized this code won't free the numbers as the prop is deleted, but the gmod wiki is down right now so I can't look up the hook. I'll fix it when it's back up.
« Last Edit: February 19, 2007, 12:55:58 pm by Megiddo »
Experiencing God's grace one day at a time.

Offline Thud13

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Restrict Certain Props
« Reply #2 on: February 19, 2007, 02:47:26 pm »
You guys are really awesome. Thank you for being so kind.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Restrict Certain Props
« Reply #3 on: February 22, 2007, 12:49:19 am »
Okay, here's the final version of this quick hack.

Code: Lua
  1. local models = {}
  2.  
  3. function restrictProp( prop, number )
  4.         models[ prop ] = number
  5. end
  6.  
  7. local function blockProps( ply, mdl )
  8.         if not models[ mdl ] then return end -- Not blocked
  9.         ply.mdlCount = ply.mdlCount or {}
  10.         ply.mdlCount[ mdl ] = ply.mdlCount[ mdl ] or 0
  11.         if ply.mdlCount[ mdl ] >= models[ mdl ] then
  12.                 ply:PrintMessage( 3, "You have reached the limit for this prop (" .. models[ mdl ] .. ")" )
  13.                 return false
  14.         end
  15.         ply.mdlCount[ mdl ] = ply.mdlCount[ mdl ] + 1
  16. end
  17. hook.Add( "PlayerSpawnProp", "block.blockProps", blockProps )
  18.  
  19. local function recordProps( ply, mdl, ent )
  20.         if not models[ mdl ] then return end -- Not blocked
  21.         ent.blockOwner = ply
  22. end
  23. hook.Add( "PlayerSpawnedProp", "block.recordProps", recordProps )
  24.  
  25. local function propRemoved( ent )
  26.         if ent.blockOwner then
  27.                 local mdl = ent:GetModel()
  28.                 local ply = ent.blockOwner
  29.                 ply.mdlCount[ mdl ] = ply.mdlCount[ mdl ] - 1
  30.         end
  31. end
  32. hook.Add( "EntityRemoved", "block.entRemoved", propRemoved )
  33.  
  34. local function tool( ply, tr, toolmode )
  35.         if toolmode == "magnetise" and tr.Entity.blockOwner then
  36.                 ply:PrintMessage( 3, "You cannot magnetize this prop, sorry." )
  37.                 return false
  38.         end
  39. end
  40. hook.Add( "CanTool", "ULXToolCheck", tool )
  41.  
  42. restrictProp( "models/props_c17/oildrum001_explosive.mdl", 10 )

Just call the restrictProp() function with what you want. Note that a really easy way to get around this protection is to rejoin... but hey, we're going for simple here.
Experiencing God's grace one day at a time.

Offline Dr.Roxor

  • Newbie
  • *
  • Posts: 10
  • Karma: 2
Re: Restrict Certain Props
« Reply #4 on: March 01, 2007, 11:37:43 am »
I'm trying to call the restrictProp() function in multiple lines for various props like this:

Code: [Select]
...
restrictProp( "models/props_c17/oildrum001_explosive.mdl", 5 )
restrictProp( "addons/phx/models/props_phx/amraam.mdl", 2 )
restrictProp( "addons/phx/models/props_phx/torpedo.mdl", 2 )

But I must not be able to "see" the addons/phx/models/props_phx folder, because this restriction for the phx models is not working. The restriction on the oildrum works however. Can anyone help me on this? I have tried various ways without success. Thanks for any tips.

EDIT: Nevermind I figured it out. The problem was in my path. Below is the correct way, noting the extra forwardslash as being what solved it for me:

Code: [Select]
...
restrictProp( "models/props_c17/oildrum001_explosive.mdl", 5 )
restrictProp( "addons/phx/models//props_phx/amraam.mdl", 2 )
restrictProp( "addons/phx/models//props_phx/torpedo.mdl", 2 )
« Last Edit: March 01, 2007, 11:52:28 am by Dr.Roxor »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Restrict Certain Props
« Reply #5 on: March 01, 2007, 04:22:35 pm »
Hmm odd, I'd think that you'd drop the addons/phx/ part entirely.
Experiencing God's grace one day at a time.

Offline Dr.Roxor

  • Newbie
  • *
  • Posts: 10
  • Karma: 2
Re: Restrict Certain Props
« Reply #6 on: March 01, 2007, 06:50:03 pm »
Perhaps it works both ways. I know it is working as shown in my snippet above.

  • Print