2020-05-18 22:21:11 +02:00
local math_random = math.random
local Public = { }
function Public . shuffle ( tbl )
2021-03-24 17:46:00 +02:00
local size = # tbl
for i = size , 1 , - 1 do
local rand = math_random ( size )
tbl [ i ] , tbl [ rand ] = tbl [ rand ] , tbl [ i ]
end
return tbl
2020-05-18 22:21:11 +02:00
end
2021-02-07 13:54:25 +02:00
local function is_closer ( pos1 , pos2 , pos )
2021-03-24 17:46:00 +02:00
return ( ( pos1.x - pos.x ) ^ 2 + ( pos1.y - pos.y ) ^ 2 ) < ( ( pos2.x - pos.x ) ^ 2 + ( pos2.y - pos.y ) ^ 2 )
2021-02-07 13:54:25 +02:00
end
function Public . shuffle_distance ( tbl , position )
2021-03-24 17:46:00 +02:00
local size = # tbl
for i = size , 1 , - 1 do
local rand = math_random ( size )
if is_closer ( tbl [ i ] . position , tbl [ rand ] . position , position ) and i > rand then
tbl [ i ] , tbl [ rand ] = tbl [ rand ] , tbl [ i ]
end
end
return tbl
2021-02-07 13:54:25 +02:00
end
2020-05-18 22:21:11 +02:00
2021-03-24 17:46:00 +02:00
function Public . raffle ( values , weights ) --arguments of the form {[a] = A, [b] = B, ...} and {[a] = a_weight, [b] = b_weight, ...} or just {a,b,c,...} and {1,2,3...}
local total_weight = 0
for k , w in pairs ( weights ) do
assert ( values [ k ] )
if w > 0 then
total_weight = total_weight + w
end
-- negative weights treated as zero
end
assert ( total_weight > 0 )
2020-05-18 22:21:11 +02:00
2021-03-24 17:46:00 +02:00
local cumulative_probability = 0
local rng = math_random ( )
for k , v in pairs ( values ) do
assert ( weights [ k ] )
cumulative_probability = cumulative_probability + ( weights [ k ] / total_weight )
if rng <= cumulative_probability then
return v
end
end
2020-05-18 22:21:11 +02:00
end
2021-12-16 20:52:34 +02:00
function Public . spairs ( t , order )
-- collect the keys
local keys = { }
for k in pairs ( t ) do keys [ # keys + 1 ] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort ( keys , function ( a , b ) return order ( t , a , b ) end )
else
table.sort ( keys )
end
-- return the iterator function
local i = 0
return function ( )
i = i + 1
if keys [ i ] then
return keys [ i ] , t [ keys [ i ] ]
end
end
end
2021-02-07 13:54:25 +02:00
return Public