Results 1 to 9 of 9

Thread: Lua in SourceOP

  1. #1
    Drunken F00l's Avatar



    Join Date
    Dec 11, 2004
    Last Online
    Jun 11, 2019
    Posts
    5,874
    Threads
    182
    Reputation
    SourceOP Thread


        
    Steam: 76561197968459473 
    Steam join date: Aug 23, 2004
    Steam Level: 56
    Profile Status: Public


    Lua in SourceOP


    So I've finally started implementing some SourceOP stuff in Lua. What this means is that you will be able to customize or add commands and other things in SourceOP. Here's an example of e_kill.
    Code:
    function drawkillbeam( startpos, endpos )
      local beamindex = util.PrecacheModel("effects/laser1.vmt");
      effects.Beam(startpos, endpos, beamindex, 0, 0, 0, 0.2, 5, 5, 0, 5, math.random(200,255), math.random(0,15), math.random(0,15), 255, 20)
      effects.Beam(startpos, endpos, beamindex, 0, 0, 0, 0.2, 5, 5, 0, 1, math.random(200,255), math.random(100,146), math.random(32,96), 255, 20)
    end
    
    // specifies entities that are not valid for killing
    function validkillent( classname )
      if(string.sub(classname, 1, 12) == "prop_vehicle") then
        return false
      end
      return true
    end
    
    function ekill( playerid, command, arguments )
      local pPlayer = player.GetByID(playerid)
      if(pPlayer) then
        if(pPlayer:IsAdmin(1024, "e_kill")) then
          if(!pPlayer:IsEntMoving()) then
            local ent, endpos = pPlayer:FindEntityForward(MASK_ALL)
            if(ent) then
              local classname = ent:GetClassname()
              if(validkillent(classname)) then
                drawkillbeam(pPlayer:GetAbsOrigin(), endpos)
                if(classname != "player") then
                  // kill the entity with explosion sound
                  ent:EmitSound("^weapons/explode" .. math.random(3,5) .. ".wav", 60, math.random(90,110))
                  ent:Kill()
                else
                  // slay the player
                  local pDeadPlayer = player.GetByID(ent:EntIndex())
                  pDeadPlayer:Kill()
                end
              end
            end
          else
            pPlayer:SayText("You are currently moving an entity and cannot run " .. command .. ".\n")
          end
        else
          pPlayer:SayText("You do not have access to the command " .. command .. ".\n", HUD_PRINTCONSOLE)
        end
      else
        // command run at server console
        // kill each entity id specified on command line
        for k,v in pairs(arguments) do
          if k >= 1 then
            local ent = ents.GetByIndex(tonumber(v))
            ent:Kill()
          end
        end
      end
    end
    
    if(sourceop.FeatureStatus(FEAT_ENTCOMMANDS)) then
      concommand.Add("e_kill", ekill)
    end
    What can you do with this?
    Well, one example I thought of is if I or anybody else wanted, we could add another command (e.g e_kill_fromplayer) based off this code that allows an admin to force a player to run e_kill. This command could be run from the server console or executed by event scripts or something to force a player to e_kill. In fact, such a command would be simple:
    Code:
    function ekillfromplayer( playerid, command, arguments )
      local pPlayer = player.GetByID(arguments[1])
      if(pPlayer) then
        local ent, endpos = pPlayer:FindEntityForward(MASK_ALL)
        if(ent) then
          local classname = ent:GetClassname()
          if(validkillent(classname)) then
            drawkillbeam(pPlayer:GetAbsOrigin(), endpos)
            if(classname != "player") then
              // kill the entity with explosion sound
              ent:EmitSound("^weapons/explode" .. math.random(3,5) .. ".wav", 60, math.random(90,110))
              ent:Kill()
            else
              // slay the player
              local pDeadPlayer = player.GetByID(ent:EntIndex())
              pDeadPlayer:Kill()
            end
          end
        end
      end
    end
    
    concommand.Add("e_kill_fromplayer", ekillfromplayer)
    I haven't tested that e_kill_fromplayer code, but it should work. Almost all of this code was reused from e_kill.

    This is just a preview of things to come. Any comments?

  2. #2
    mIKe's Avatar



    Join Date
    Dec 02, 2007
    Last Online
    Dec 03, 2007
    Posts
    1
    Threads
    0




    This is perfect... very useful for us Eventscript coders. Thanks!

    Will these also work with your +commands, eventually?

  3. #3
    Drunken F00l's Avatar



    Join Date
    Dec 11, 2004
    Last Online
    Jun 11, 2019
    Posts
    5,874
    Threads
    182
    Reputation
    SourceOP Thread


        
    Steam: 76561197968459473 
    Steam join date: Aug 23, 2004
    Steam Level: 56
    Profile Status: Public



    In one way or another, yes. I'm not sure how I'm going to go about it. For example, do I make +grabent just do pPlayer:StartEntMoving() or do I write out all the code for that in Lua. Either way, you should be able to do what you want with those eventscripts but writing it all out in Lua will make it more robust.

    Also, since the forum doesn't do a very good job of making code look good, I posted a prettier version at http://www.sourceop.com/e_kill.lua.html .

  4. #4
    Killer-Instinct's Avatar



    Join Date
    Nov 23, 2007
    Last Online
    Dec 10, 2007
    Posts
    3
    Threads
    1




    When will this be in the download-able sourceop? Sounds like fun stuff.

  5. #5
    Drunken F00l's Avatar



    Join Date
    Dec 11, 2004
    Last Online
    Jun 11, 2019
    Posts
    5,874
    Threads
    182
    Reputation
    SourceOP Thread


        
    Steam: 76561197968459473 
    Steam join date: Aug 23, 2004
    Steam Level: 56
    Profile Status: Public



    Next release. Perhaps early January?

  6. #6
    Pwner_Express's Avatar



    Join Date
    Jan 03, 2005
    Last Online
    Mar 21, 2008
    Posts
    23
    Threads
    6




    so if i learn LUA scripting you are saying I can make my own commands for source op? is there compiling involved or would id just be in some sort of text file?

    edit
    ps dont know anything about LUA incase (this seems like a dumb Q).

  7. #7
    OmegaZero_Alpha's Avatar



    Join Date
    Jan 02, 2005
    Last Online
    Jul 16, 2019
    Posts
    4,880
    Threads
    253
    Reputation
    SourceOP Thread


        
    Steam: 76561197979925166 
    Steam join date: Dec 24, 2005
    Steam Level: 44
    Profile Status: Public



    it would be in an LUA file, and as long as you write it to be compatible and set SourceOP to look for it, it will work fine
    /sarcasm

  8. #8
    Drunken F00l's Avatar



    Join Date
    Dec 11, 2004
    Last Online
    Jun 11, 2019
    Posts
    5,874
    Threads
    182
    Reputation
    SourceOP Thread


        
    Steam: 76561197968459473 
    Steam join date: Aug 23, 2004
    Steam Level: 56
    Profile Status: Public



    The way it works is a script called lua/includes/init.lua is loaded at server start. After this, all scripts in the lua/autorun are loaded. There is no compiling process required.

    I'm naming the functions and libraries similar to how they are named in Garry's mod. I'm doing this so that it's familiar to people, and because it's overall a good naming convention.

    There will obviously be some differences. I can't put in every gmod function, but on the other hand there will be some functions that aren't in gmod.

    Also, Lua is written as "Lua". It is not an acronym.

  9. #9
    Pwner_Express's Avatar



    Join Date
    Jan 03, 2005
    Last Online
    Mar 21, 2008
    Posts
    23
    Threads
    6




    Thanks DF for clearing things up, you too omega. I am going to have to teach my self Lua, I see there are good tutorials here for anyone else who might be interested. http://www.garrysmod.com/wiki/?title=Lua

Tags for this Thread