General > Developers Corner

Custom Zombie Spawner Locations. ( Allot of zombie RP servers will like this. )

(1/2) > >>

LuaTenshi:
Here is my code...


--- Code: ----- Zombie Spawning --

function ZombieSpawnsToFile( ply, hfZombieSpawnLocations )
    local g = glon.encode(hfZombieSpawnLocations)
    file.Write( "zombiepos.txt", g )
end

function hfZombieSpawnersHelp(ply)
    ply:ChatPrint("*Zombie Spawner Help*")
    ply:ChatPrint("hf_zm_spawn r | Will remove all current spawn points and make a spawn point where you are standing.")
    ply:ChatPrint("hf_zm_spawn a | Adds a spawn point.")
    ply:ChatPrint("hf_zm_spawn p | Lists all current spawn points.")
    ply:ChatPrint("hf_zm 1 <time in secounds> | Spawns zombies, and controls how fast they spawn.")
    ply:ChatPrint("hf_zm 0 | Stops all zombies from spawning.")
    ply:ChatPrint("*Zombie Spawner Help End*")
end

function hfCZombieSpawns( ply, command, args )
    if( ply:IsAdmin() or ply:IsSuperAdmin() ) then
        if( ply and ply:IsValid() and file.Exists("zombiepos.txt") ) then
        local File = file.Read("zombiepos.txt")
        local g = glon.decode(File)
        local hfZombieSpawnLocations = {}
            if( args[1] == "r" ) then
                table.Empty( hfZombieSpawnLocations )
                table.insert( hfZombieSpawnLocations, ply:GetPos() )
                ZombieSpawnsToFile( ply, hfZombieSpawnLocations )
                ply:ChatPrint("Zombie spawn position set at " .. tostring(ply:GetPos()) .. ". Other spawn points have been removed.")
            elseif ( args[1] == "a" ) then
                for i,item in pairs(g) do
                    table.insert( hfZombieSpawnLocations, g[i] )
                end
                table.insert( hfZombieSpawnLocations, ply:GetPos() )
                ZombieSpawnsToFile( ply, hfZombieSpawnLocations )
                ply:ChatPrint("Zombie spawn position set at " .. tostring(ply:GetPos()))
            elseif( args[1] == "p" ) then
                for i,p in pairs(g) do
                    ply:ChatPrint("Zombie_Spawn_" .. tostring(i) .. " located at " .. tostring(p))
                end
            else
                hfZombieSpawnersHelp(ply)
            end
        elseif( ply and ply:IsValid() ) then
            ply:ChatPrint("Error File Not Found.")
        else
            print("Player Invalid. ( If you are in the console join the server. )")
        end
    else
        ply:ChatPrint("You are not allowed to do that " .. ply:Name())
    end
end

function hfZombieSpawn( ply, command, args )
    if( ply:IsAdmin() or ply:IsSuperAdmin() ) then
        if( file.Exists("zombiepos.txt") ) then
            local run = 1
            timer.Destroy( "ZombieSpawnTimer" )
            if( args[1] and args[1] != "0" ) then
                run = 0               
                for _,plys in pairs(player.GetAll()) do
                    plys:ChatPrint( ply:Name() .. " Enabled Zombie Spawning." )
                end   
            else
                for _,plys in pairs(player.GetAll()) do
                    plys:ChatPrint( ply:Name() .. " Disabled Zombie Spawning." )
                end
            end
           
            local time = tonumber(args[2])
            if( time < 5 ) then
                time = 5
            end
           
            if( run and run != 1 and time and time >= 5 ) then
                ply:ChatPrint("Zombies will spawn every " .. tostring(time) .. " secounds.")
                timer.Create( "ZombieSpawnTimer", time, 0, function( ply, args )
                    if( file.Exists("zombiepos.txt") and args[1] and args[1] != 0 ) then
                        local zombieTypes = { "npc_fastzombie", "npc_zombie", "npc_poisonzombie" }
                        local Read = file.Read("zombiepos.txt")
                        local positions = glon.decode(Read)
                        for _, pos in pairs(positions) do
                            local ent = ents.Create(zombieTypes[math.random(1,table.getn(zombieTypes))])
                            if( ent and ent:IsValid() ) then
                                ent:SetPos(pos)
                                ent:Spawn()
                                ent:Activate()
                            end
                        end
                    else
                        timer.Destroy( "ZombieSpawnTimer" )
                        if not ( file.Exists("zombiepos.txt") ) then
                            ply:ChatPrint("Error File Not Found.")
                        end
                    end
                end, ply, args)
            end
        else
            ply:ChatPrint("Error File Not Found.")
        end
    else
        ply:ChatPrint("You are not allowed to do that " .. ply:Name())
    end
end

concommand.Add("hf_zm_help",hfZombieSpawnersHelp)
concommand.Add("hf_zm_spawn",hfCZombieSpawns)
concommand.Add("hf_zm",hfZombieSpawn)

--- End code ---

It works, but it needs allot of cleaning up. Please help. The main problem is that it lags very, badly.

Assault_Trooper:
Propably lags because you're decoding and encoding all the time. Just decode it once to a variable when it's loaded, and use the variable instead.

LuaTenshi:

--- Quote from: Assault_Trooper on July 14, 2012, 04:00:40 AM ---Propably lags because you're decoding and encoding all the time. Just decode it once to a variable when it's loaded, and use the variable instead.

--- End quote ---

Where exactly am I decoding and encoding all the time? I cant seem to see it.

And it actually does not lag that much only a short spike now and then.

Before I thought it was lagging badly because some one was DDoSing my server.

Assault_Trooper:

--- Quote from: HeLLFox_15 on July 14, 2012, 12:38:33 PM ---Where exactly am I decoding and encoding all the time? I cant seem to see it.

And it actually does not lag that much only a short spike now and then.

Before I thought it was lagging badly because some one was DDoSing my server.

--- End quote ---


--- Code: ---function ZombieSpawnsToFile( ply, hfZombieSpawnLocations )
    local g = glon.encode(hfZombieSpawnLocations)
    file.Write( "zombiepos.txt", g )

function hfCZombieSpawns( ply, command, args )
        local File = file.Read("zombiepos.txt")
        local g = glon.decode(File)

timer.Create( "ZombieSpawnTimer", time, 0, function( ply, args )
         local Read = file.Read("zombiepos.txt")
         local positions = glon.decode(Read)

--- End code ---

Not sure why'd you say the problem is lag if you're not even sure it's the code.

Schiaffino:
Suggestion:

I Have My Zombie RP Server Working

40 Slots Availables , Just Using 20 To Avoid Lag.

When I Spawn +30 or +20 Zombies ( Extra Zombies ) ( Not The Zombie Maps )
Server Start Lagging ( For Some People )
Try To Make This Code More ... what is the word ... ( LIGHT )

* Sorry for My Bad Spelling ! =)

Navigation

[0] Message Index

[#] Next page

Go to full version