General > Developers Corner

Lua Coder Needed for a Vote Build/Fight Environment Script

<< < (2/2)

JamminR:
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: ---/*                                                                            /*

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 )

--- End code ---

/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.

atomicspark:
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.

JamminR:
Any luck getting this running?

Navigation

[0] Message Index

[*] Previous page

Go to full version