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! --- config/menus/select.lua | 2 +- not/Selector.lua | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/config/menus/select.lua b/config/menus/select.lua index 0540bd5..d350c32 100644 --- a/config/menus/select.lua +++ b/config/menus/select.lua @@ -34,7 +34,7 @@ do return end if self.index == 2 then - return -- roll random, soon. + self.index = self:rollRandom({1, 2}) end if self:isUnique() then self.lock = true 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