ULX

Author Topic: Entity height/width in view angle?  (Read 9996 times)

0 Members and 1 Guest are viewing this topic.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6213
  • Karma: 394
  • Project Lead
Re: Entity height/width in view angle?
« Reply #15 on: July 15, 2006, 01:21:09 AM »
No problem, post whatever you need to.
Experiencing God's grace one day at a time.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Entity height/width in view angle?
« Reply #16 on: July 15, 2006, 01:44:22 PM »
This is the targeting for the 'Vision' part of a 'Predator' (mod?) I'm working on with Kronic.
I originally got the idea after meeting Kronic from FP online, who released a mod that among other things, has some Predator functions in it. (Stealth,jump, and a non-swep weapon)
 I'd been a beta tester for some of his mods features, and after seeing how popular the Predator portion of it was at the FP forums, decided to ask if he'd like it turned into LUA, giving he and I a chance to learn LUA, and at the same time, expanding its capability.
 Feel free to use/edit this in anything you want. I myself learn best.
Please note there will be more to the full vision portion, this just targets.
To execute, either place in LUA/Init, or, lua_openscript /where_ever_you_place_it/Pvision.lua
If you're on the server when you execute it, you need to rejoin, as it relies on 'eventPlayerActive' to set up some key values.

Currently its written to work totally on its own.
Once loaded, and you've joined the server, 'Ptarget' turns on, or off, the targeting.

Haven't tried all the materials yet.

I'm trying to learn _any_ tips to make it better/faster.
Code: [Select]
--targetmaterial = "gmod/wireframe"            --     "Wireframe"- fun, draws box with angle line, like 'do not let live'
--targetmaterial = "models/shadertest/shader3" --     "Water" - Useless, but fun to see. Listed here for amusement
--targetmaterial = "models/shadertest/shader4" --     "Jelly" - Extremely slow. Not recommended
--targetmaterial = "models/shadertest/shader5" --     "Stained Glass" Useless but fun, again, for amusement
--targetmaterial = "gmod/shiny"                --     "Shiny White"
--targetmaterial = "models/debug/debugwhite"   --     "Matte White"
--targetmaterial = "models/effects/comball_tape" --   "Combine Ball 2" -Shimmers, left to right, matalpha=~180
targetmaterial = "models/effects/splodearc_sheet"-- "Blue Glow" -- Nice material, bluish beam, fast updates
--targetmaterial = "models/props_combine/stasisshield_sheet" -- "Combine Stasis" - Looks great, Slow though
--targetmaterial = "models/props_combine/portalball001_sheet"-- "Combine Funnel" - ??
--targetmaterial = "models/props_combine/com_shield001a" -- "Combine Shield" - Falling blocks.
--targetmaterial = "models/props_c17/frostedglass_01a" -- "Frosted Glass"  - ??
--targetmaterial = "models/props_lab/Tank_Glass001"     -- "Tank Glass"     - ??
--targetmaterial = "models/props_combine/tprings_globe" -- "tprings_globe"  - ??
--targetmaterial = "gmod/white" - Standard block of color.
matalpha = 180  -- doesn't seem to have much effect on materials above, exception of gmod/white

targs     = {}
PredUser = {}
radius    = 4500
width = 18
height = 54
co = 1

function resetPred(predid)
HaltTimer(PredUser[predid].VisionTime)
hidetargets(predid)
PredUser[predid].visionswitch = 0
PredUser[predid] = {}
end

function checkpredator(predid)
    if (_PlayerInfo(predid,"connected")) then
                  return true
    else
        resetPred(predid)
     end
end


--Target code is based on that seen in Apocolypse Mod "redeemer" SWEP from FP forums, HEAVILY modified
--Which, may be based on the wiki 'findinsphere' page.
--BIG thanks go out to Megiddo(Former UGM, current ULX fame) for assisting with assiting in finding
--dynamic distance/width/height adjustment. I'm still looking for how to determine -any- Ents view properties,
--as the variables above, 18 and 54, only seem to fit humanoids best
--See http://www.ulyssesmod.net for ULX admin script/Ulib lua libraries by Megiddo
function findtargets(predid)
 if (checkpredator(predid)) then
   local startvector = _PlayerGetShootPos(predid)
    targs = _EntitiesFindInSphere(startvector, radius )
for i = 1,table.getn(targs) do
    if (string.find (_EntGetType(targs[i]),"npc_",1,true) ~= nil) or
       (_EntGetType(targs[i]) == "player") then
              if (predid ~= targs[i]) then
               local dist = vecLength( vecSub( startvector, _EntGetPos( targs[i] ) ) )
               local Xco = (width/dist)*co
               local Yco = (height/dist)*co
               _GModRect_Start( targetmaterial )
               _GModRect_SetPos( -(Xco/2), -(Yco/2), Xco, Yco )
               _GModRect_SetColor( 200, 0, 150, matalpha )
               _GModRect_SetTime( .5, 0, 0 )
               _GModRect_SetEntity( targs[i] )
               _GModRect_Send( predid, predid + (2*i)  )
           end -- if targ is self
         end -- if entype is npc or player
end -- for loop sphere table
  end -- if connected
 end  --function

-- There were originally 15 or more 'if (_EntGetType(targs[i]) == "npc_BLAHBLAH") or' comparisons above.
--Where BLAHBLAH was each npc individually from game.
--JamminR had hoped there was an easier way.
--There is. Thanks to string handling explanation http://www.lua.org/manual/5.1/manual.html#5.4
--    string.find (s, pattern [, init [, plain]])
--JamminR hasn't timed the two different ones, and the old code may work better for not tracking non necessary npc_ entitities (Vortigaunt beams)
--Even if not faster, at least its neater/smaller code.

function hidetargets( predid )
  for i = 1,table.getn(targs) do
        local targetchannel = predid + (2*i)
_GModRect_Hide(predid,targetchannel)
end
end

function DoShowTargets(predid)
if ( PredUser[predid].visionswitch == 0) then
PredUser[predid].visionswitch = 1
PredUser[predid].VisionTime = AddTimer( 1/30, 0, findtargets,predid )
else
PredUser[predid].visionswitch = 0
HaltTimer(PredUser[predid].VisionTime)
hidetargets(predid)
end
end

function eventPlayerActive ( name, predid, steamid )
PredUser[predid] = {}
PredUser[predid].visionswitch = 0
return
end

CONCOMMAND("Ptarget",DoShowTargets)

Known Bugs
-----------
-Doesn't adjust for non-humanoid NPCs. Antlions will still be tall rectangles. (No autoadjust for various NPC)
-Zooming in on any npc shrinks target (which personally, I don't mind)
-If a Vortigaunt starts to shoot one of its electrical beams at anything, apparently the beams are named npc_(something). this script picks it up and draws boxes around those too. Slowing it even more.
-Speed - More than a few NPCs, depending on hardware, and what material is being used as target, causes flashing of the target boxes, stuttering of the game itself.

Challenges (at least, for me in my lua inexperience) -
----------
-Speed. It tracks _everything_ in 360 sphere around the player. Gmod_rect only draws view in front.
I believe if a way were found to track the HUD, plus a few (10?) degress in all four view directions, this might help.
Implimenting a way to track only necessary parts of NPCs (See vortigaunt bug above) without slowing down even more.
- Relying on a timer instead of something else to update.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming