• Print

Author Topic: Dodgeball v1.1  (Read 14761 times)

0 Members and 1 Guest are viewing this topic.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Dodgeball v1.1
« on: September 08, 2006, 03:44:38 pm »
Here's a little ditty I wrote for a lua request. While its original purpose was to just seek out and kill targets, I think it makes a fun dodgeball minigame. Here's the code:

Code: Lua
  1. --[[
  2. UDodge v1.1
  3. by Brett "Megiddo" Smith
  4.  
  5. This script will create a "pylon" that shoots full-sized combine balls at you.
  6.  
  7. Requirements:
  8. This script requires ULib version 1.1 or higher. You can download ULib from ulyssesmod.net
  9.  
  10. Usage:
  11. Just use the command "makepylon" in console, a pylon will appear in front of you and start shooting!
  12.  
  13. For more advanced users, the full command is makepylon [area_size] [ball_speed] [passes] [update_rate] [timeout] [ball_size] [charge]
  14. All arguments are optional
  15. area_size is how far away it will shoot at you from, default is 800
  16. ball_speed is how fast the ball moves, default is 1000. Caps at ~2000, don't set it higher or the AI will be screwy.
  17. passes is the accuracy with which the pylon will fire. Default is 2. Must be an integer. CPU usage increases with each pass, so use with caution.
  18. update_rate is how often it checks for enemies, default is 1/2
  19. timeout is how long it must wait between each fire before it can fire again, default is 1.5
  20. ball_size is how large the ball is, default is 15
  21. charge is the time between when it wants to shoot you and when it does (thus, charge time), default is 0
  22.  
  23. If you want to restrict access to the command, change the 'access' variable below to something like ACCESS_SLAY.
  24. If you want to be able to move the pylons, change pylons_movable to true.
  25.  
  26. Changelog:
  27. v1.1 *(09/12/06)*
  28.         [CHANGE] Now uses ULib v1.1
  29.         [ADD] ball_size parameter
  30.         [ADD] passes parameter
  31.         [ADD] Can now shoot at players in vehicles
  32.         [FIX] Deletion problem
  33.         [FIX] Doesn't target noclipped players
  34. v1.0 *(09/09/06)*
  35.         *Initial version.
  36. ]]
  37.  
  38. assert( _file.Exists( "lua/ULib/init.lua" ), "UDodge needs ULib to run!" )
  39. _OpenScript( "ULib/init.lua" )
  40. assert( ULib.ULIB_VERSION >= 1.1, "UDodge requires ULib version 1.1 or higher to run!" )
  41.  
  42. local access = ACCESS_ALL
  43. local pylons_movable = true
  44.  
  45. local offset = 50
  46. local offset_up = vector3( 0, 0, 20 )
  47.  
  48. local pylons = {}
  49. local maxplayers = _MaxPlayers()
  50. local vehicles -- This will let us know if a vehicle is occupied
  51. local vehicle_pos = {}
  52.  
  53. function updateVehicles()
  54.         vehicles = {} -- Regenerate every time.
  55.         for i=1, maxplayers do
  56.                 if _PlayerInfo( i, "connected" ) and ULib.isInVehicle( i ) then
  57.                         vehicles[ _EntGetParent( i ) ] = i
  58.                 end
  59.         end
  60. end
  61. AddTimer( 2.05, 0, updateVehicles )
  62.  
  63. local function enemyFire( cball, target, speed, passes, time )
  64.         if time > 0 then -- We want to wait for charging sound to end.
  65.                 AddTimer( time, 1, enemyFire, cball, target, speed, passes, 0 )
  66.                 return
  67.         end
  68.  
  69.         local targetpos = _EntGetPos( target )
  70.         targetpos = vecAdd( targetpos, offset_up )
  71.         local cballpos = _EntGetPos( cball )
  72.         local targetvel = _EntGetVelocity( target )
  73.         if vehicles[ target ] then -- If it's a vehicle, we handle velocity a special way
  74.                 local oldpos = vehicle_pos[ target ].pos
  75.                 local oldtime = vehicle_pos[ target ].time
  76.                 local newpos = targetpos
  77.                 local newtime = _CurTime()
  78.  
  79.                 -- M = (newpos-oldpos)/(newtime-oldtime)
  80.                 if newtime-oldtime > 0 then -- Nothing we can do, divide by 0.
  81.                         targetvel = vecSub( newpos, oldpos )
  82.                         targetvel = vecMul( targetvel, 1/(newtime-oldtime) )
  83.                 end
  84.         end
  85.        
  86.         -- Initialize loop parameters
  87.         local futurepos = targetpos
  88.  
  89.         for pass=0, passes do
  90.                 local dist = vecSub( futurepos, cballpos )
  91.                 local timetotarget = vecLength( dist ) / speed
  92.                 local distcovered = vecMul( targetvel, timetotarget )
  93.  
  94.                 futurepos = vecAdd( targetpos, distcovered )
  95.         end
  96.         local dir = vecNormalize( vecSub( futurepos, cballpos ) )
  97.  
  98.         ULib.play3DSound( "weapons/irifle/irifle_fire2.wav", cballpos )
  99.         ULib.applyAccel( cball, speed, dir )
  100. end
  101.  
  102.  
  103. local function enemyCheck( pylonid )
  104.         local pylon = pylons[ pylonid ]
  105.         if not _EntExists( pylon.entid ) or not _EntGetName( pylon.entid ) == "pylon" then -- Some error checking
  106.                 HaltTimer( pylon.timerid )
  107.                 pylons[ pylonid ] = nil -- Recycle so another pylon can take the place
  108.         end
  109.  
  110.         if pylon.timein > _CurTime() then return end -- Not past timeout
  111.  
  112.         local targets = {}
  113.         local pos = _EntGetPos( pylon.entid )
  114.         pos = vecAdd( pos, offset_up )
  115.         local ents = _EntitiesFindInSphere( pos, pylon.area_size )
  116.         for _, entid in ipairs( ents ) do
  117.                 if (entid > 0 and entid <= maxplayers and _PlayerInfo( entid, "alive" ) and
  118.                    _EntGetMoveType( entid ) ~= MOVETYPE_NOCLIP) or vehicles[ entid ] then -- Player or vehicle
  119.                         -- We're going to do a trace to make sure the weapon can see the player.
  120.                         local dir = vecSub( vecAdd( _EntGetPos( entid ), offset_up ), pos )
  121.                         _TraceLine( pos, dir, pylon.area_size, pylon.entid )
  122.                         if _TraceGetEnt() == entid then -- It can see them!
  123.                                 table.insert( targets, entid )
  124.                         end
  125.                 end
  126.         end
  127.  
  128.         if table.getn( targets ) > 0 then
  129.                 local entid = targets[ math.random( table.getn( targets ) ) ] -- Get a random target
  130.                 pylons[ pylonid ].timein = _CurTime() + pylon.timeout
  131.                 local off = vecNormalize( vecSub( _EntGetPos( entid ), pos ) )
  132.                 off = vecMul( off, offset )
  133.                 local spawnpos = vecAdd( pos, off )
  134.                 spawnpos = vecAdd( spawnpos, offset_up )
  135.                 ULib.play3DSound( "weapons/cguard/charging.wav", spawnpos )
  136.                
  137.                 if vehicles[ entid ] then -- If it's a vehicle
  138.                         vehicle_pos[ entid ] = { pos=vecAdd( _EntGetPos( entid ), offset_up ), time=_CurTime() } -- We have to do this because _EntGetVelocity doesn't work on vehicles.
  139.                 end
  140.                 ULib.makeCball( enemyFire, spawnpos, pylon.size, entid, pylon.speed, pylon.passes, pylon.charge )
  141.         end
  142.         return
  143. end
  144.  
  145. local function cc_makePylon( userid, args, argv, argc )
  146.         if not ULib.mainUcl:query( userid, access ) then
  147.                 ULib.tsay( userid, "You do not have access to this command." )
  148.                 return
  149.         end
  150.  
  151.         local area_size = tonumber( argv[ 1 ] ) or 800
  152.         local speed = tonumber( argv[ 2 ] ) or 1000
  153.         local passes = tonumber( argv[ 3 ] ) or 2
  154.         local update = tonumber( argv[ 4 ] ) or 1/2
  155.         local timeout = tonumber( argv[ 5 ] ) or 1.5
  156.         local size = tonumber( argv[ 6 ] ) or 15
  157.         local charge = tonumber( argv[ 7 ] ) or 0
  158.  
  159.         PlayerLookTrace( userid, 4096 )
  160.         local entid = ULib.makeProp( "models/props_c17/oildrum001.mdl", _TraceEndPos(), vector3( 1, 0, 0 ) )
  161.         _EntSetName( entid, "pylon" )
  162.         if not pylons_movable then
  163.                 _EntSetMoveType( entid, MOVETYPE_NONE )
  164.         else
  165.                 ULib.keepUpright( entid, 100 )
  166.         end
  167.  
  168.         local pylonid
  169.         for i=1, 512 do -- 512 limit on pylons
  170.                 if not pylons[ i ] then
  171.                         pylonid = i -- Take the first open slot
  172.                 end
  173.         end
  174.         local timerid = AddTimer( update, 0, enemyCheck, pylonid )
  175.         pylons[ pylonid ] = { entid=entid, passes=passes, area_size=area_size, timeout=timeout, speed=speed, timein=0, size=size, charge=charge, timerid=timerid }
  176.  
  177.         ULib.callOnDelete( entid, function()
  178.                 HaltTimer( timerid )
  179.                 pylons[ pylonid ] = nil -- Recycle so another pylon can take the place
  180.         end )
  181. end
  182. ULib.CONCOMMAND( "makepylon", cc_makePylon )

UDodge v1.1
by Brett "Megiddo" Smith

This script will create a "pylon" that shoots full-sized combine balls at you.

Requirements:
This script requires ULib version 1.1 or higher. You can download ULib from ulyssesmod.net

Usage:
Just use the command "makepylon" in console, a pylon will appear in front of you and start shooting!

For more advanced users, the full command is makepylon [area_size] [ball_speed] [passes] [update_rate] [timeout] [ball_size] [charge]
All arguments are optional
area_size is how far away it will shoot at you from, default is 800
ball_speed is how fast the ball moves, default is 1000. Caps at ~2000, don't set it higher or the AI will be screwy.
passes is the accuracy with which the pylon will fire. Default is 2. Must be an integer. CPU usage increases with each pass, so use with caution.
update_rate is how often it checks for enemies, default is 1/2
timeout is how long it must wait between each fire before it can fire again, default is 1.5
ball_size is how large the ball is, default is 15
charge is the time between when it wants to shoot you and when it does (thus, charge time), default is 0

If you want to restrict access to the command, change the 'access' variable below to something like ACCESS_SLAY.
If you want to be able to move the pylons, change pylons_movable to true.

Download from: http://ulyssesmod.net/archive/UDodge-v1_1.zip
« Last Edit: September 12, 2006, 10:23:50 pm by Megiddo »
Experiencing God's grace one day at a time.

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: Dodgeball!
« Reply #1 on: September 08, 2006, 04:11:24 pm »
'
To make a 'pylon', just call makepylon in console. You can tweak its parameters by passing arguments it accepts the following (all optional): [area_size] [update_rate] [timeout] [ball_speed] [charge]

area_size is how far away it will shoot at you from, default is 800
update_rate is how often it checks for enemies, default is 1/2
timeout is how long it must wait between each fire before it can fire again, default is 1.5
ball_speed is how fast the ball moves, default is 1000
charge is the time between when it wants to shoot you and when it does (thus, charge time), default is 0'

Might wanna take that out of the code


Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Dodgeball!
« Reply #2 on: September 08, 2006, 04:45:07 pm »
Thanks  ;)
Experiencing God's grace one day at a time.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Dodgeball!
« Reply #3 on: September 09, 2006, 04:36:31 pm »
First post updated with better version.
Experiencing God's grace one day at a time.

Offline Golden-Death

  • Hero Member
  • *****
  • Posts: 751
  • Karma: 0
  • Honored Lua Scripter
    • BlueFire
Re: Dodgeball!
« Reply #4 on: September 10, 2006, 11:56:52 am »
* Golden-Death runs



Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Dodgeball!
« Reply #5 on: September 12, 2006, 10:15:51 pm »
Updated! v1.1!
Experiencing God's grace one day at a time.

  • Print