Commit 154883c0 authored by nanahira's avatar nanahira Committed by GitHub

use cache in subgroup check (#1129)

* use cache in subgroup check

* rename

* use bool

* a little improve

* use key to store caches

* fix

* fix

* revert

* Revert "revert"

This reverts commit f30279eb6961c009fcc60b22dcce604a5b6066e1.

* recheck selected card in cache

* caching a single selected group is meaningless

* revert
parent 251954e9
...@@ -2004,10 +2004,32 @@ function Auxiliary.GetMultiLinkedZone(tp) ...@@ -2004,10 +2004,32 @@ function Auxiliary.GetMultiLinkedZone(tp)
end end
return multi_linked_zone return multi_linked_zone
end end
function Auxiliary.CheckGroupRecursive(c,sg,g,f,min,max,ext_params) function Auxiliary.GetGroupKey(g)
local v=0
for c in Auxiliary.Next(g) do
math.randomseed(c:GetFieldID())
v=v+math.random()
end
return v
end
function Auxiliary.LookupSubGroupCache(cache,sg)
local res=cache[Auxiliary.GetGroupKey(sg)]
return res,(res==1)
end
function Auxiliary.StoreSubGroupCache(cache,sg,res)
cache[Auxiliary.GetGroupKey(sg)]=(res and 1 or 0)
end
function Auxiliary.CheckGroupRecursive(c,sg,g,cache,f,min,max,ext_params)
sg:AddCard(c) sg:AddCard(c)
local res=(#sg>=min and #sg<=max and f(sg,table.unpack(ext_params))) local res=false
or (#sg<max and g:IsExists(Auxiliary.CheckGroupRecursive,1,sg,sg,g,f,min,max,ext_params)) local found,data=Auxiliary.LookupSubGroupCache(cache,sg)
if found then
res=data
else
res=(#sg>=min and #sg<=max and f(sg,table.unpack(ext_params)))
or (#sg<max and g:IsExists(Auxiliary.CheckGroupRecursive,1,sg,sg,g,cache,f,min,max,ext_params))
Auxiliary.StoreSubGroupCache(cache,sg,res)
end
sg:RemoveCard(c) sg:RemoveCard(c)
return res return res
end end
...@@ -2017,9 +2039,10 @@ function Group.CheckSubGroup(g,f,min,max,...) ...@@ -2017,9 +2039,10 @@ function Group.CheckSubGroup(g,f,min,max,...)
if min>max then return false end if min>max then return false end
local ext_params={...} local ext_params={...}
local sg=Duel.GrabSelectedCard() local sg=Duel.GrabSelectedCard()
if #sg>max or #sg==max and not f(sg,...) then return false end if #sg>max or #(g+sg)<min or #sg==max and not f(sg,...) then return false end
if #sg>=min and #sg<=max and f(sg,...) then return true end if #sg>=min and #sg<=max and f(sg,...) then return true end
return g:IsExists(Auxiliary.CheckGroupRecursive,1,sg,sg,g,f,min,max,ext_params) local cache={}
return g:IsExists(Auxiliary.CheckGroupRecursive,1,sg,sg,g,cache,f,min,max,ext_params)
end end
function Group.SelectSubGroup(g,tp,f,cancelable,min,max,...) function Group.SelectSubGroup(g,tp,f,cancelable,min,max,...)
local min=min or 1 local min=min or 1
...@@ -2027,13 +2050,15 @@ function Group.SelectSubGroup(g,tp,f,cancelable,min,max,...) ...@@ -2027,13 +2050,15 @@ function Group.SelectSubGroup(g,tp,f,cancelable,min,max,...)
local ext_params={...} local ext_params={...}
local sg=Group.CreateGroup() local sg=Group.CreateGroup()
local fg=Duel.GrabSelectedCard() local fg=Duel.GrabSelectedCard()
if #fg>max or min>max or #(g+fg)<min then return nil end
for tc in aux.Next(fg) do for tc in aux.Next(fg) do
fg:SelectUnselect(sg,tp,false,false,min,max) fg:SelectUnselect(sg,tp,false,false,min,max)
end end
sg:Merge(fg) sg:Merge(fg)
local finish=(#sg>=min and #sg<=max and f(sg,...)) local finish=(#sg>=min and #sg<=max and f(sg,...))
local cache={}
while #sg<max do while #sg<max do
local cg=g:Filter(Auxiliary.CheckGroupRecursive,sg,sg,g,f,min,max,ext_params) local cg=g:Filter(Auxiliary.CheckGroupRecursive,sg,sg,g,cache,f,min,max,ext_params)
finish=(#sg>=min and #sg<=max and f(sg,...)) finish=(#sg>=min and #sg<=max and f(sg,...))
if #cg==0 then break end if #cg==0 then break end
local cancel=not finish and cancelable local cancel=not finish and cancelable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment