• 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!

Random Chests in the map

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,484
Solutions
9
Reaction score
217
Hello folks :oops:
I'm here to ask to you, if I create a script to create 2000 chests in random positions in the map, my map is about 140 mb, how it will be to my machine, it can give some lags/freeze ? then, because in the script (just an idea) will have something like this:
Code:
for x = 30000, 35000 do
    for y = 30000, 35000 do
        for z = 1, 15 do
            -- some code, check the place, chance to create the chest, the count (when the count is in the end break), some script to the gamemaster, create a table with the coordinates of the chests...
        end
    end
end
it will be heavy?
this idea is based on PXG, they make it for the profession adventurer, is very good :eek::oops::oops:
 
Depends on your ram.. Try it out and print the time interval before and after the loop... print(os.clock()) before and after to see the amount of time it took to complete the action. Good technique for testing all your scripts for efficiency.
 
I really doubt they check the whole world map before place the chest. It could then easily be created in the city or some sort of places where you got no access.

I'm pretty sure, they store chunks of areas in a table and then loop through them until the chest get placed. Also placing 2000 chests seems bit overkill if you ask me. What is the point of having "hidden" chests if your going to place 2000 of them.

Here is something fast i coded and came up with, should help you to the right track:

Code:
local config = {
    itemid = CHEST_ID,
    storage = {key = 100, value = 10}, -- Value = how many we want to create
    position = {
        {fromPosition = Position(1000, 1000, 7), toPosition = Position(1100, 1100, 7)},
        {fromPosition = Position(1300, 1300, 7), toPosition = Position(1400, 1400, 7)}
    }
}

function onStartup()
    -- Always have these math functions outside the loop, to avoid generate garbage
    local random = math.random

    while (Game.getStorageValue(config.storage.key) <= config.storage.value) do
        for i = 1, #config.position do
            local chest = Game.createRandomChest(config.itemid, Position(random(config.fromPosition.x, config.toPosition.x), random(config.fromPosition.y, config.toPosition.y), random(config.fromPosition.z, config.toPosition.z))))
            if chest then
                chest:addItem(2160, 1)
                Game.setStorageValue(config.storage.key, (Game.getStorageValue(config.storage.key) or 0) + 1)
                break
            end
        end
    end

    return true
end

function Game.createRandomChest(itemid, position)
    local tile = Tile(position)
    if not tile then
        return false
    end

    local ground = tile:getGround()
    if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
        return false
    end

    return Game.createItem(itemid, 1, position)
end

WARNING! THIS HAS NOT BEEN TESTED, SO MAKE SURE TO TRY IT BEFORE ADDING IT TO YOUR LIVE SERVER.
 
Last edited:
Depends on your ram.. Try it out and print the time interval before and after the loop... print(os.clock()) before and after to see the amount of time it took to complete the action. Good technique for testing all your scripts for efficiency.
thank you, I never saw this function before, with it I can test the script
:):oops:
 
I really doubt they check the whole world map before place the chest. It could then easily be created in the city or some sort of places where you got no access.

I'm pretty sure, they store chunks of areas in a table and then loop through them until the chest get placed. Also placing 2000 chests seems bit overkill if you ask me. What is the point of having "hidden" chests if your going to place 2000 of them.

Here is something fast i coded and came up with, should help you to the right track:

Code:
local config = {
    itemid = CHEST_ID,
    storage = {key = 100, value = 10},
    position = {
        {fromPosition = Position(1000, 1000, 7), toPosition = Position(1100, 1100, 7)},
        {fromPosition = Position(1300, 1300, 7), toPosition = Position(1400, 1400, 7)}
    }
}

function onStartup()
    -- Always have these math functions outside the loop, to avoid generate garbage
    local random = math.random

    while (Game.getStorageValue(config.storage.key) <= config.storage.value) do
        for i = 1, config.position do
            local chest = Game.createRandomChest(config.itemid, 1, Position(random(config.fromPosition.x, config.toPosition.x), random(config.fromPosition.y, config.toPosition.y), random(config.fromPosition.z, config.toPosition.z))))
            if chest then
                chest:addItem(2160, 1)
                Game.setStorageValue(config.storage.key, (Game.getStorageValue(config.storage.key) or 0) + 1)
                break
            end
        end
    end

    return true
end

function Game.createRandomChest(itemid, position)
    local tile = Tile(position)
    if not tile then
        return false
    end

    local ground = tile:getGround()
    if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
        return false
    end

    Game.createItem(itemid, 1, position)
    return true
end

WARNING! THIS HAS NOT BEEN TESTED, SO MAKE SURE TO TRY IT BEFORE ADDING IT TO YOUR LIVE SERVER.
thank you @Printer I'll test before use, your code seems my idea haha
Thank you again, I'll update the tittle as closed.
 
@Printer I test it, get some errors
when load these lines:
Code:
for i = 1, config.position do
          local chest = Game.createRandomChest(config.itemid, Position(random(config.fromPosition.x, config.toPosition.x), random(config.fromPosition.y, config.toPosition.y), random(config.fromPosition.z, config.toPosition.z))))
          if chest then
the first line get error say about the limit of the for, I test it with for i = 1, 2 do and it work
the second line is about the fromPosition, you forgot to set where get it
Code:
config.fromPosition.x, config.toPosition.x
I just change to
Code:
config.position.fromPosition.x, config.position.toPosition.x
third line, if chest then, well, the error say about boolean, I don't understand about it, then I do something different,
Code:
local pos = Position(random(config.position.fromPosition.x, config.position.toPosition.x), random(config.position.fromPosition.y, config.position.toPosition.y), random(config.position.fromPosition.z, config.position.toPosition.z)) -- define a new style to declare it
           if Game.createRandomChest(config.itemid, pos) then -- use the function because the function check all things to create or not a chest
           --chest:addItem(~~) removed, I addItems in the chest inside of the function Game.createRandomChest
ahh, another thing, if I use the chest 12664 to be created in the function, if I do one more time it can create chests on the top of already created chests, about it I just added some code in the function Game.createRandomChest in:
Code:
if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
just it:
Code:
if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) or tile:getItemCount() ~= 0 then
then now don't create another chest on the top
about all that I said, have possibility to do the same that I did but more efficient and effective?
hugs.
 
I forgot to declare the length of the position table, thats why it threw a error.

I've updated the code with the fix or do it yourself:
Code:
for i = 1, config.position do

to:
Code:
for i = 1, #config.position do
 
I forgot to declare the length of the position table, thats why it threw a error.

I've updated the code with the fix or do it yourself:
Code:
for i = 1, config.position do

to:
Code:
for i = 1, #config.position do
yes sir, and what about the local chest = ... getiting a boolean?
 
Back
Top