techage_modpack/signs_bot/random.lua

73 lines
1.3 KiB
Lua
Raw Permalink Normal View History

2020-05-31 23:31:18 +03:00
--[[
Signs Bot
=========
2021-05-14 19:50:16 +03:00
Copyright (C) 2019-2021 Joachim Stolberg
2020-05-31 23:31:18 +03:00
GPL v3
See LICENSE.txt for more information
2022-09-24 12:01:30 +03:00
Signs Bot: Random Series
2020-05-31 23:31:18 +03:00
]]--
-- generate a table of unique pseudo random numbers
local function gen_serie(inv_size)
local tbl = {}
local offs = inv_size/2
if offs % 2 == 1 then
offs = offs + 1
end
local index = 1
2022-09-24 12:01:30 +03:00
2020-05-31 23:31:18 +03:00
for n = 1, (inv_size*2) do
tbl[#tbl + 1] = index
index = ((index + offs) % inv_size) + 1
end
return tbl
end
-- Pseudo numbers series to iterate "randomly" through inventories
local InvRandomSeries = {}
local function get_serie(inv_size)
if not InvRandomSeries[inv_size] then
InvRandomSeries[inv_size] = gen_serie(inv_size)
end
return InvRandomSeries[inv_size]
end
local function random(inv_size)
local t = get_serie(inv_size)
local i = 0
local n = #t/2
local offs = math.random(n)
return function ()
i = i + 1
if i <= n then return t[i+offs] end
end
end
--
-- API
--
-- func signs_bot.random(inv_size)
-- random iterator providing 'inv_size' possible bot inventory stack numbers
-- inv_size must be even and between 4 and 64
signs_bot.random = random
--
-- Test
--
--for size = 4,64,2 do
-- local tbl = {}
-- for idx in random(size) do
-- tbl[#tbl + 1] = idx
-- end
-- print(table.concat(tbl, ", "))
2022-09-24 12:01:30 +03:00
--end