From 84f6ec47be56d397fe140b5707b76078917fe8fb Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 03:49:33 +0200 Subject: Random naut selection is back! --- not/Selector.lua | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'not') 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 -- cgit v1.1