ULX

Author Topic: Lua Coder Needed for a Vote Build/Fight Environment Script  (Read 5474 times)

0 Members and 1 Guest are viewing this topic.

Offline atomicspark

  • Full Member
  • ***
  • Posts: 196
  • Karma: 12
Lua Coder Needed for a Vote Build/Fight Environment Script
« on: July 05, 2007, 03:19:20 AM »
I know you all are free and not busy with other projects :D, but if you could write me a quick script, that would be awesome. Basically I'm looking for a script that will change server variables. These variables would be sbox_noclip, sbox_playergod, and sbox_plpldamage (I think. It's all the ones in the !adminmenu except for enable NPCs).

Anyways let me know if you could help out. No pressure on time. I would just try to edit another script but I know how you guys can make code incredibly simple (ex. Megiddo's fly script). Let me know if "environment" isn't the best choice of word to use. I just want to enable some flexibility to my lil server. Thanks!
« Last Edit: July 09, 2007, 04:01:12 PM by atomicspark »

Offline atomicspark

  • Full Member
  • ***
  • Posts: 196
  • Karma: 12
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #1 on: July 09, 2007, 04:00:30 PM »
I have edited Luk0r's map reset vote to do what I wanted. Basically they just toggle sbox_playergod, sbox_noclip, and sbox_plpldamage. I wish I could condense this code into one file but my knowledge of Lua is very limited. If it was one file, only one vote request could run at a time. For instance, if it was currently a fighting environment (default) only the /votebuild command would work. I would also like it to advertise only the enabled vote command. This way it would be simple and conflicts would be kept to a minimum.

So if you guys can check it out and make suggestions, that would be great! Code below.

/lua/autorun/VoteBuild.lua
Code: [Select]
/*

Name: Vote Build
Version: 0.0
Author: [atomicspark] - www.atomicsparkonline.net
Original Code: Luk0r - luk0r@luk0r.net
For: Garry's Mod v10

*/

vb = {}; vb.Settings = {}; vb.VotedFor = {}

// Configure these variables to suit your needs, the defaults will probably be fine though

vb.Settings.advertise  =  false        // Advertise the script?
vb.Settings.advdelay   =  300          // Delay in seconds between advertising.
vb.Settings.trigger    =  "/votebuild" // The chat trigger to begin a vote.
vb.Settings.votetrig   =  "/vote"      // Trigger to vote for the map reset (used when a vote is taking place)
vb.Settings.votetimer  =  30           // How long players have to vote.
vb.Settings.majority   =  60           // The majority of votes required for a reset in percent.


// Main code block

if CLIENT then return nil end

function vb.sendNotify( ply, text, sound, notifytype )
ply:SendLua("GAMEMODE:AddNotify(\"" .. text .. "\", " .. notifytype .. ", 5); surface.PlaySound( \"" .. sound .. "\" )")
end

function vb.checkForVoteTrigger( ply, txt )
if vb.votetakingplace == 0 then
if string.find( txt, vb.Settings.trigger ) then
vb.votetakingplace = 1
vb.votesfor = 1
table.insert( vb.VotedFor, ply:SteamID() )
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, ply:Nick() .. " has requested a building environment!", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
plyr:PrintMessage( HUD_PRINTTALK, "Type " .. vb.Settings.votetrig .. " to vote for the map reset.\n" )
plyr:PrintMessage( HUD_PRINTTALK, "Type nothing if you disagree.\n" )
end
vb.votereminder = vb.Settings.votetimer
vb.votereminderchunk = math.Round( vb.votereminder / 3 )
timer.Create( "vb.RemindPlayersOfVote", vb.votereminderchunk, 3, vb.voteTimerCheck )
timer.Create( "vb.EndOfVote", vb.Settings.votetimer, 1, vb.voteEnded )
return ""
end
else
if string.find( txt, vb.Settings.trigger ) then
vb.sendNotify( ply, "A building environment vote is already running!", "buttons/button2.wav", "NOTIFY_ERROR" )
return ""
else
if string.find( txt, vb.Settings.votetrig ) then
for i, vfornick in ipairs( vb.VotedFor ) do
if vfornick == ply:SteamID() then
vb.sendNotify( ply, "You have already voted!", "buttons/button2.wav", "NOTIFY_ERROR" )
return ""
end
end
vb.votesfor = vb.votesfor + 1
vb.sendNotify( ply, "You have voted for a building environment.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
table.insert( vb.VotedFor, ply:SteamID() )
local players = player.GetAll()
for i, plyr in ipairs( players ) do
plyr:PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has voted for a building environment.\n" )
end
return ""
end
end
end
end

function vb.voteTimerCheck()
vb.votereminder = vb.votereminder - vb.votereminderchunk
if vb.votereminder > 0 then
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "Around " .. vb.votereminder .. " seconds remaining on the building environment vote!", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
end
end

function vb.voteEnded()
playersonserver = 0
local players = player.GetAll()
for i, v in ipairs( players ) do
playersonserver = playersonserver + 1
end
local majorityplayers = math.ceil( ( playersonserver * vb.Settings.majority ) / 100 )
if vb.votesfor >= majorityplayers then
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "The building environment vote has succeeded, the map will now reset.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
timer.Simple( 3, vb.enableBuild )
else
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "The building environment vote has failed.", "buttons/button2.wav", "NOTIFY_ERROR" )
end
vb.VotedFor = {}
vb.votetakingplace = 0
vb.votesfor = 0
end
end

function vb.enableBuild()
// game.ConsoleCommand( "changelevel " .. game.GetMap() .. "\n" )
game.ConsoleCommand( "sbox_playergod " .. "1" .. "\n" )
game.ConsoleCommand( "sbox_noclip " .. "1" .. "\n" )
game.ConsoleCommand( "sbox_plpldamage " .. "1" .. "\n" )
end

function vb.advertise()
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "To begin a building environment vote, type " .. vb.Settings.trigger .. " in chat.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
end

if vb.Settings.advertise == true then
timer.Create( "vb.Advertise", vb.Settings.advdelay, 0, vb.advertise )
end

vb.votetakingplace = 0  // Don't change these
vb.votesfor = 0

hook.Add( "PlayerSay", "vb.CheckForVoteTrigger", vb.checkForVoteTrigger )

/lua/autorun/VoteFight.lua
Code: [Select]
/*

Name: Vote Fight
Version: 0.0
Author: [atomicspark] - www.atomicsparkonline.net
Original Code: Luk0r - luk0r@luk0r.net
For: Garry's Mod v10

*/

vf = {}; vf.Settings = {}; vf.VotedFor = {}

// Configure these variables to suit your needs, the defaults will probably be fine though

vf.Settings.advertise  =  false        // Advertise the script?
vf.Settings.advdelay   =  300          // Delay in seconds between advertising.
vf.Settings.trigger    =  "/votefight" // The chat trigger to begin a vote.
vf.Settings.votetrig   =  "/vote"      // Trigger to vote for the map reset (used when a vote is taking place)
vf.Settings.votetimer  =  30           // How long players have to vote.
vf.Settings.majority   =  60           // The majority of votes required for a reset in percent.


// Main code block

if CLIENT then return nil end

function vf.sendNotify( ply, text, sound, notifytype )
ply:SendLua("GAMEMODE:AddNotify(\"" .. text .. "\", " .. notifytype .. ", 5); surface.PlaySound( \"" .. sound .. "\" )")
end

function vf.checkForVoteTrigger( ply, txt )
if vf.votetakingplace == 0 then
if string.find( txt, vf.Settings.trigger ) then
vf.votetakingplace = 1
vf.votesfor = 1
table.insert( vf.VotedFor, ply:SteamID() )
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vf.sendNotify( plyr, ply:Nick() .. " has requested a fighting environment!", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
plyr:PrintMessage( HUD_PRINTTALK, "Type " .. vf.Settings.votetrig .. " to vote for the map reset.\n" )
plyr:PrintMessage( HUD_PRINTTALK, "Type nothing if you disagree.\n" )
end
vf.votereminder = vf.Settings.votetimer
vf.votereminderchunk = math.Round( vf.votereminder / 3 )
timer.Create( "vf.RemindPlayersOfVote", vf.votereminderchunk, 3, vf.voteTimerCheck )
timer.Create( "vf.EndOfVote", vf.Settings.votetimer, 1, vf.voteEnded )
return ""
end
else
if string.find( txt, vf.Settings.trigger ) then
vf.sendNotify( ply, "A fighting environment vote is already running!", "buttons/button2.wav", "NOTIFY_ERROR" )
return ""
else
if string.find( txt, vf.Settings.votetrig ) then
for i, vfornick in ipairs( vf.VotedFor ) do
if vfornick == ply:SteamID() then
vf.sendNotify( ply, "You have already voted!", "buttons/button2.wav", "NOTIFY_ERROR" )
return ""
end
end
vf.votesfor = vf.votesfor + 1
vf.sendNotify( ply, "You have voted for a fighting environment.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
table.insert( vf.VotedFor, ply:SteamID() )
local players = player.GetAll()
for i, plyr in ipairs( players ) do
plyr:PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has voted for a fighting environment.\n" )
end
return ""
end
end
end
end

function vf.voteTimerCheck()
vf.votereminder = vf.votereminder - vf.votereminderchunk
if vf.votereminder > 0 then
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vf.sendNotify( plyr, "Around " .. vf.votereminder .. " seconds remaining on the fighting environment vote!", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
end
end

function vf.voteEnded()
playersonserver = 0
local players = player.GetAll()
for i, v in ipairs( players ) do
playersonserver = playersonserver + 1
end
local majorityplayers = math.ceil( ( playersonserver * vf.Settings.majority ) / 100 )
if vf.votesfor >= majorityplayers then
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vf.sendNotify( plyr, "The fighting environment vote has succeeded, PvP damage enabled and noclip disabled.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
timer.Simple( 3, vf.enableFight )
else
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vf.sendNotify( plyr, "The fighting environment vote has failed.", "buttons/button2.wav", "NOTIFY_ERROR" )
end
vf.VotedFor = {}
vf.votetakingplace = 0
vf.votesfor = 0
end
end

function vf.enableFight()
game.ConsoleCommand( "sbox_playergod " .. "0" .. "\n" )
game.ConsoleCommand( "sbox_noclip " .. "0" .. "\n" )
game.ConsoleCommand( "sbox_plpldamage " .. "0" .. "\n" )
end

function vf.advertise()
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vf.sendNotify( plyr, "To begin a fighting environment vote, type " .. vf.Settings.trigger .. " in chat.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
end

if vf.Settings.advertise == true then
timer.Create( "vf.Advertise", vf.Settings.advdelay, 0, vf.advertise )
end

vf.votetakingplace = 0  // Don't change these
vf.votesfor = 0

hook.Add( "PlayerSay", "vf.CheckForVoteTrigger", vf.checkForVoteTrigger )
« Last Edit: July 09, 2007, 04:07:18 PM by atomicspark »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #2 on: July 09, 2007, 05:25:47 PM »
AtomicSpark, do you really want a vote? Or is that just what you ended up using?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline atomicspark

  • Full Member
  • ***
  • Posts: 196
  • Karma: 12
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #3 on: July 09, 2007, 06:14:39 PM »
Voting is good so that users can vote on it? lol. I don't intend for this to be an admin only command. It's meant just for players.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #4 on: July 09, 2007, 07:23:02 PM »
I'm tinkering with combining the two. Gimme a day or two.
(I'm quickly running out of time today, sorry. Think I have most of it hacked.)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #5 on: July 09, 2007, 08:23:01 PM »
AtomicSpark.
 This is untested.
Tried to clean it up a bit. Renamed a command. Added a few variables.
Let me know what errors you get. I'll hopefully have more time tomorrow (Tuesday) evening to test/fix anything you find.
Code: [Select]
/*                                                                            /*

Name: Environment Vote (Fight/Build)
Version: 0.0
Author: [atomicspark] - www.atomicsparkonline.net
Code Note: Heavily modified by JamminR of Team Ulysses for [atomicspark]
Original Code: Luk0r - luk0r@luk0r.net
For: Garry's Mod v10

*/

vb = {}; vb.Settings = {}; vb.VotedFor = {}

// Configure these variables to suit your needs, the defaults will probably be fine though

vb.Settings.advertise  =  false        // Advertise the script?
vb.Settings.advdelay   =  300          // Delay in seconds between advertising.
vb.Settings.trigger.build    =  "/votebuild" // The chat trigger to begin a build vote.
vb.Settings.trigger.fight    =  "/votefight" // The chat trigger to begin a fight vote.
vb.Settings.votetrig   =  "/vote"      // Trigger to vote for the map reset (used when a vote is taking place)
vb.Settings.votetimer  =  30           // How long players have to vote.
vb.Settings.majority   =  60           // The majority of votes required for a reset in percent.
vb.Settings.currentmode = "Build"      // "Build" or "Fight" Mode that server runs at startup/default

// Main code block

if CLIENT then return nil end

function vb.sendNotify( ply, text, sound, notifytype )
ply:SendLua("GAMEMODE:AddNotify(\"" .. text .. "\", " .. notifytype .. ", 5); surface.PlaySound( \"" .. sound .. "\" )")
end

function vb.checkForVoteTrigger( ply, txt )
if vb.votetakingplace == 0 then
           if string.find( txt, vb.Settings.trigger.build ) or string.find( txt, vb.Settings.trigger.fight ) then -- Check if either vote was requested
if string.find( txt, vb.Settings.trigger.build ) then -- Check if a build vote was requested
   if vb.Settings.currentmode = "Build" then -- Check if we're already IN build mode.
                      vb.sendNotify( ply, "Build mode is already engaged!", "buttons/button2.wav", "NOTIFY_ERROR" )
                      return ""
   else
                       vb.voteinaction = "Build"
   end
                end
if string.find( txt, vb.Settings.trigger.fight ) then -- Check if a fight vote was requested
   if vb.Settings.currentmode = "Fight" then -- Check if we're already IN fight mode.
                      vb.sendNotify( ply, "Fight mode is already engaged!", "buttons/button2.wav", "NOTIFY_ERROR" )
                      return ""
   else
                       vb.voteinaction = "Fight"
   end
                end
                vb.votetakingplace = 1
                vb.votesfor = 1
                table.insert( vb.VotedFor, ply:SteamID() )
                local players = player.GetAll()
                for i, plyr in ipairs( players ) do
                    vb.sendNotify( plyr, ply:Nick() .. " has requested a "..vb.voteinaction.."ing environment!", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
    plyr:PrintMessage( HUD_PRINTTALK, "Type " .. vb.Settings.votetrig .. " to vote for the map reset.\n" )
    plyr:PrintMessage( HUD_PRINTTALK, "Type nothing if you disagree.\n" )
                end
                vb.votereminder = vb.Settings.votetimer
                vb.votereminderchunk = math.Round( vb.votereminder / 3 )
                timer.Create( "vb.RemindPlayersOfVote", vb.votereminderchunk, 3, vb.voteTimerCheck )
                timer.Create( "vb.EndOfVote", vb.Settings.votetimer, 1, vb.voteEnded )
                return ""
           end
else
if string.find( txt, vb.Settings.trigger.build ) or string.find( txt, vb.Settings.trigger.fight )then
vb.sendNotify( ply, "A "..vb.voteinaction.."ing environment vote is already running!", "buttons/button2.wav", "NOTIFY_ERROR" )
return ""
else
if string.find( txt, vb.Settings.votetrig ) then
for i, vfornick in ipairs( vb.VotedFor ) do
if vfornick == ply:SteamID() then
vb.sendNotify( ply, "You have already voted!", "buttons/button2.wav", "NOTIFY_ERROR" )
return ""
end
end
vb.votesfor = vb.votesfor + 1
vb.sendNotify( ply, "You have voted for a "..vb.voteinaction.."ing environment.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
table.insert( vb.VotedFor, ply:SteamID() )
local players = player.GetAll()
for i, plyr in ipairs( players ) do
plyr:PrintMessage( HUD_PRINTTALK, ply:Nick() .. " has voted for a "..vb.voteinaction.."ing environment.\n" )
end
return ""
end
end
end
end

function vb.voteTimerCheck()
vb.votereminder = vb.votereminder - vb.votereminderchunk
if vb.votereminder > 0 then
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "Around " .. vb.votereminder .. " seconds remaining on the "..vb.voteinaction.."ing environment vote!", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
end
end

function vb.voteEnded()
playersonserver = 0
local players = player.GetAll()
for i, v in ipairs( players ) do
playersonserver = playersonserver + 1
end
local majorityplayers = math.ceil( ( playersonserver * vb.Settings.majority ) / 100 )
if vb.votesfor >= majorityplayers then
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "The "..vb.voteinaction.."ing environment vote has succeeded, the map will now reset.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
timer.Simple( 3, vb.enableEnvChange )
else
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "The "..vb.voteinaction.."ing environment vote has failed.", "buttons/button2.wav", "NOTIFY_ERROR" )
end
vb.VotedFor = {}
vb.votetakingplace = 0
vb.votesfor = 0
vb.voteinaction = ""
end
end

function vb.enableEnvChange()
// game.ConsoleCommand( "changelevel " .. game.GetMap() .. "\n" )
        vb.Settings.currentmode = vb.voteinaction
        if vb.voteinaction = "Build" then
game.ConsoleCommand( "sbox_playergod " .. "1" .. "\n" )
game.ConsoleCommand( "sbox_noclip " .. "1" .. "\n" )
game.ConsoleCommand( "sbox_plpldamage " .. "1" .. "\n" )
else
game.ConsoleCommand( "sbox_playergod " .. "0" .. "\n" )
game.ConsoleCommand( "sbox_noclip " .. "0" .. "\n" )
game.ConsoleCommand( "sbox_plpldamage " .. "0" .. "\n" )
        end

end

function vb.advertise()
local players = player.GetAll()
for i, plyr in ipairs( players ) do
vb.sendNotify( plyr, "To begin an environment vote, type " .. vb.Settings.trigger.build .. " or " .. vb.Settings.trigger.fight .. " in chat.", "ambient/water/drip" .. math.random(1, 4) .. ".wav", "NOTIFY_GENERIC" )
end
end

if vb.Settings.advertise == true then
timer.Create( "vb.Advertise", vb.Settings.advdelay, 0, vb.advertise )
end

vb.votetakingplace = 0  // Don't change these
vb.votesfor = 0

hook.Add( "PlayerSay", "vb.CheckForVoteTrigger", vb.checkForVoteTrigger )

/votebuild will start a build mode vote if it's not already in build mode or in progress.
/votefight will start a fight mode vote if it's not already in fight mode or in progress.
/vote does same, votes for whatever mode is being voted on.

Don't uncomment the map change code. I'm pretty sure changelevel forgets all the variables set, and it be 'dumb' to what mode is actually running.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline atomicspark

  • Full Member
  • ***
  • Posts: 196
  • Karma: 12
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #6 on: July 09, 2007, 11:08:05 PM »
I saw that I forgot to change a line. The notification when you first start a vote still said something about a map reset. You seem to have caught that tho. I like your little code note in there. :) I haven't thought about releasing it on FP or anything, but credit is still good since it will be on here.

I'll check it out if I get around to it.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Lua Coder Needed for a Vote Build/Fight Environment Script
« Reply #7 on: July 10, 2007, 07:37:56 PM »
Any luck getting this running?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming