summaryrefslogtreecommitdiffhomepage
path: root/not/Selector.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-09-05 03:49:33 +0200
committerAki <nthirtyone@gmail.com>2017-09-05 03:49:33 +0200
commit84f6ec47be56d397fe140b5707b76078917fe8fb (patch)
treebcacf782f0c9bc7fd8919cbe43f38cfde1b37506 /not/Selector.lua
parent9fb64e49be5d991a0bf4bc1f6f162f40276ba2f5 (diff)
downloadroflnauts-84f6ec47be56d397fe140b5707b76078917fe8fb.zip
roflnauts-84f6ec47be56d397fe140b5707b76078917fe8fb.tar.gz
roflnauts-84f6ec47be56d397fe140b5707b76078917fe8fb.tar.bz2
Random naut selection is back!
Diffstat (limited to 'not/Selector.lua')
-rw-r--r--not/Selector.lua23
1 files changed, 21 insertions, 2 deletions
diff --git a/not/Selector.lua b/not/Selector.lua
index bd6224b..ee6f0e3 100644
--- a/not/Selector.lua
+++ b/not/Selector.lua
@@ -49,6 +49,23 @@ function Selector:setIndex (index)
return old
end
+function Selector:rollRandom (exclude)
+ local exclude = exclude or {}
+ local index = love.math.random(1, #self.list)
+ local elgible = true
+ for _,i in ipairs(exclude) do
+ if index == i then
+ elgible = false
+ break
+ end
+ end
+ if not elgible or not self:isUnique(self.list[index]) then
+ table.insert(exclude, index)
+ return self:rollRandom(exclude)
+ end
+ return index
+end
+
--- Returns selected item's value.
-- @return item selected from the list
function Selector:getSelected ()
@@ -64,12 +81,14 @@ function Selector:getLocked ()
end
--- Checks if Selected value is unique in group's scope.
+-- @param index optional parameter to fill in place of currently selected item
-- @return boolean answering question
-function Selector:isUnique ()
+function Selector:isUnique (item)
+ local item = item or self:getSelected()
if self.group then
local locked = self.group:callEachBut(self, "getLocked")
for _,value in pairs(locked) do
- if value == self:getSelected() then
+ if value == item then
return false
end
end