ULX

Author Topic: Custom Zombie Spawner Locations. ( Allot of zombie RP servers will like this. )  (Read 4490 times)

0 Members and 2 Guests are viewing this topic.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Here is my code...

Code: [Select]
-- 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)

It works, but it needs allot of cleaning up. Please help. The main problem is that it lags very, badly.
« Last Edit: July 14, 2012, 02:22:38 AM by HeLLFox_15 »
I cry every time I see that I am not a respected member of this community.

Offline Assault_Trooper

  • Newbie
  • *
  • Posts: 20
  • Karma: 1
    • Trooper's Gaming Servers
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.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
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.

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.
« Last Edit: July 14, 2012, 12:42:04 PM by HeLLFox_15 »
I cry every time I see that I am not a respected member of this community.

Offline Assault_Trooper

  • Newbie
  • *
  • Posts: 20
  • Karma: 1
    • Trooper's Gaming Servers
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.

Code: [Select]
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)

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

Offline Schiaffino

  • Jr. Member
  • **
  • Posts: 88
  • Karma: 1
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 ! =)

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Not so sure it would be the code that would cause that much lag.
Remove the code, restart server, have ~20 join, then spawn ~20 zombies using console or admin control panel.
I'd bet it will still lag.
Server has to monitor/maintain/send traffic about each zombie to 20 different players.
That alone slows down the server.
Add some clients PCs not being all that great, and they too might slow down even in single player with 20 zombies.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
I did add a limit recently, the maximum amount of zombies that can be created is...

The # of players * 2

And if there are more then 25 players the maximum amount of zombies is 50.

Also if a zombie remains idle for to long as in if it has no players to attack for a long time, it will get removed.

( I think that's a good way to limit the zombies. )
I cry every time I see that I am not a respected member of this community.

Offline Schiaffino

  • Jr. Member
  • **
  • Posts: 88
  • Karma: 1
This ( lua code )

Create zombies around the server map no ?

maybe this code works with

RP_eVOCITY ?

because i want to change my zombie roleplay server , to this server map.
( why ? )

because all the zombie servers use the same maps by Pretberk.

I Want to make the difference...
( change the map to this , and also increase my server capacity )

I Want to get in contact with you ! add me to 

STEAM: STEAM_0:0:20370318
SKYPE: l2schiaffino or l2coventry , i do not remember xD :)

Bye =D