• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Spawn monsters in a certain area

chiitus

Member
Joined
Jun 1, 2009
Messages
206
Reaction score
12
Hello guys,
I'm trying to make a script that add creatures in a certain area if the numbers of monsters of area is lower than X, using this function:

Code:
getMonstersfromArea(frompos, topos)

But i want something more, when the script will gonna add the X creature, i want that before add the script see the best place to do this.
example: if on the 7 sqm arround of a existent creature don't have another creature the script will add the new monster exactly in that place..

I'm using a dungeon system that player have a time and more exp to hunt, i need this script to make the hunt better
:]

Thanks adv
 
i don't really know what you said but i understood few you can try using this function
Code:
local function getMonstersInRange(fromPosition, toPosition)
    local creatures = getCreaturesInRange(fromPosition, toPosition)
    local monsters = {}
    for _, creature in ipairs(creatures) do
        if(isMonster(creature)) then
            table.insert(monsters, creature)
        end
    end
    return monsters
end
put it in the lib/functions
so you just
Code:
if getMonstersInRange(from,to) > 5 then
doCreateMonster("monstername", math.random(from,to))
return true
end

This is just a helping not a full code :)
 
Sorry, my english so bad, but...
Yes, just like this.
Besides, i want before add the new creature the script check the monsters around this new creature position, and if around this new creature pos already have a creature so the script will not add.

Example:
In this topic, the script check if have 2 monsters close: http://otland.net/threads/check-in-area-if-two-monsters-are-closer-then-one-is-removed.151162/
But i want instead remove the creature, check if not have another monster close, then add the new creature, basically using this function:

n_pos1 = {x = pos_2.x - 2, y = pos_2.y - 2, z = pos_2.z}
n_pos2 = {x = pos_2.x + 2, y = pos_2.y + 2, z = pos_2.z}
for areax_2 = n_pos1.x, n_pos2.x do
for areay_2 = n_pos1.y, n_pos2.y do
pos_2 = {x=areax_2, y=areay_2, z=pos_2.z, stackpos=255}
creature_2 = getThingfromPos(pos_2)
if (isMonster(creature_2.uid) and creature_2.uid ~= creature.uid) then
doRemoveCreature(creature_2.uid)

Just chaning: "pos_2x -5", "pos_2.x + 5"......

You understand now??

It's quite hard :p
 
I Make this script now and tested on TFS 1.0.
More Don't get pos - 5 and pos_2.x + 5.
But it is not recommended to use this type of script to many monsters. Because it can return the server lag. Use it sparingly.

IMPORTANT
Add valid positions without walls and spaces where monsters can not be born. But try to understand the script, to give it an upgrade. The script is very simple and quite easy to understand.


AddMontersOnArea(fromx, tox, fromy, toy, z, NameCreature, TotalFixeMonsters)

Get 2 or more on TotalFixeMonsters

Code:
function onThink(interval, lastExecution)
    local TempSpawn = {
                        _ValidPositions = {},
                      }
                   
                   
    function AddMontersOnArea(fromx, tox, fromy, toy, z, NameCreature, TotalFixeMonsters)
            local FixeMonsters = tonumber(TotalFixeMonsters) - 1
            local NumberMonters = 0
         
            for _x = fromx, tox do
                for _y = fromy, toy do
                    local parcial_pos = {x = _x, y = _y, z = z}
                    local complete_pos = getThingfromPos({x = _x, y = _y, z = z, stackpos = 253}).uid
                    if isCreature(complete_pos) and isMonster(complete_pos) then
                        local GetName = getCreatureName(complete_pos)
                        if GetName:lower() == NameCreature:lower() then
                            NumberMonters = NumberMonters + 1
                        end
                    else
                        table.insert(TempSpawn._ValidPositions, parcial_pos)
                    end                     
                end
            end
     
 
            if (NumberMonters < FixeMonsters) and (table.maxn(TempSpawn._ValidPositions) > FixeMonsters) then
                for temp = NumberMonters, FixeMonsters do                 
                    local i =  math.random(1, table.maxn(TempSpawn._ValidPositions))
                    doSummonCreature(NameCreature, TempSpawn._ValidPositions[i])
                    table.remove(TempSpawn._ValidPositions, i)
                end
            else
                return false
            end 
    end     


        AddMontersOnArea(32333, 32338, 32214, 32219, 7, "Rat", 4)
        for i,v in ipairs(TempSpawn._ValidPositions) do table.remove(TempSpawn._ValidPositions, i) end         
        AddMontersOnArea(32367, 32370, 32214, 32217, 7, "Demon", 2)
        for i,v in ipairs(TempSpawn._ValidPositions) do table.remove(TempSpawn._ValidPositions, i) end         


    return true
end
 
Last edited:
Back
Top