Commit f71be13f authored by Vury Leo's avatar Vury Leo

refactor

parent c5f177cc
...@@ -3480,10 +3480,10 @@ end ...@@ -3480,10 +3480,10 @@ end
--- @return boolean -- True if a valid selection exists, false otherwise. --- @return boolean -- True if a valid selection exists, false otherwise.
function Synchro.CanCompleteSelection(candidates,target_level,tuner_filter,non_tuner_filter,tc, function Synchro.CanCompleteSelection(candidates,target_level,tuner_filter,non_tuner_filter,tc,
tuner_min,tuner_max,non_tuner_min,non_tuner_max,base_mapper,selected,index,state,prune_indexes) tuner_min,tuner_max,non_tuner_min,non_tuner_max,base_mapper,selected,index,state,prune_indexes)
local mapper = state.level_mapper
if state.tuner_count>tuner_max then return false end if state.tuner_count>tuner_max then return false end
if state.non_tuner_count>non_tuner_max then return false end if state.non_tuner_count>non_tuner_max then return false end
if (index>prune_indexes.hand_prune_index and state.hand_count>state.hand_count_limit) then return false end
if index>#candidates then if index>#candidates then
-- check if target_level achievable exactly -- check if target_level achievable exactly
...@@ -3502,114 +3502,24 @@ function Synchro.CanCompleteSelection(candidates,target_level,tuner_filter,non_t ...@@ -3502,114 +3502,24 @@ function Synchro.CanCompleteSelection(candidates,target_level,tuner_filter,non_t
end end
local mc=candidates[index] local mc=candidates[index]
local tuner_inc=tuner_filter(mc,tc) and 1 or 0 for _,diff in ipairs(Synchro.GenerateVariantForCard(
local non_tuner_inc=non_tuner_filter(mc,tc) and 1 or 0 state,mc,#selected+1,tc,tuner_filter,non_tuner_filter,prune_level
local hand_inc=mc:IsLocation(LOCATION_HAND) and 1 or 0 )) do
-- apply diff
-- include branch local branch_state=Synchro.ApplyVariantState(
local variants={} state,mc,diff,tuner_filter,non_tuner_filter,tc,prune_level
)
-- build inclusion variants if prune not blocking table.insert(selected,mc)
-- before/at hand_prune_index: allow any hand inclusion (no limit) if Synchro.CanCompleteSelection(
-- after hand_prune_index: enforce state.hand_count + hand_inc <= hand_max candidates,target_level,
local can_include=(index<=prune_indexes.hand_prune_index) or (state.hand_count+hand_inc<=state.hand_count_limit) tuner_filter,non_tuner_filter,tc,
tuner_min,tuner_max,non_tuner_min,non_tuner_max,
if can_include then base_mapper,selected,index+1,branch_state,prune_indexes
-- 1) MonoSynchron branch (priority<2 → first Mono; priority==2 → full Mono) ) then
if Synchro.IsMono(mc) then
--- Mono Synchron (priority=2) > Flower Cardian (1) > normal (0)
if state.level_mapper_priority<2 then
-- first Mono: that one keeps its real level, others=1
local firstMono=mc
local mono_mapper=function(c,tc_arg)
if c==firstMono then
return {c:GetLevel()}
else
return {1}
end
end
-- reset your sums_so_far into a single bucket of (#selected)*1+self
local sums1={[#selected+mc:GetLevel()]=true}
if next(sums1)~=nil then
table.insert(variants,{
possible_sums=sums1,
level_mapper=mono_mapper,
level_mapper_priority=2,
})
end
elseif state.level_mapper_priority==2 then
-- second (or later) Mono: full override—all cards=Level 1
local full_mapper=function() return {1} end
-- reset your sums_so_far into a single bucket of (#selected+1)*1
local cnt=#selected+1
local sums2={[cnt]=true}
table.insert(variants,{
possible_sums=sums2,
level_mapper=full_mapper,
level_mapper_priority=2, -- stays in Mono mode
})
end
end
--- mono is mandatory so it can not be include Normal
--- Tatsunoko never harm to enable
if not (Synchro.IsMono(mc) or Synchro.IsTatsunoko(mc)) then
-- 1) Normal include branch
local lvls=mapper(mc,tc)
local sums_norm=Synchro.UpdatepossibleSums(state.possible_sums,lvls,prune_level)
if next(sums_norm) then
table.insert(variants,{possible_sums=sums_norm})
end
end
-- override if Flower Cardian
if Synchro.IsFlower(mc) then
if state.level_mapper_priority<1 then
local cnt=#selected+1
local sums_ovr={[2*cnt]=true}
table.insert(variants,{
possible_sums=sums_ovr,
level_mapper=function() return{2} end,
level_mapper_priority=1
})
end
end
-- Tatsunoko effect: include and grant hand material slot
if Synchro.IsTatsunoko(mc) then
-- include Tatsunoko's own level contributions
local lvls=mapper(mc,tc)
local sums_tats=Synchro.UpdatepossibleSums(state.possible_sums,lvls,prune_level)
table.insert(variants,{
possible_sums=sums_tats,
hand_count_limit=1,
})
end
for _,var in ipairs(variants) do
local new_state={
possible_sums=var.possible_sums,
tuner_count=state.tuner_count+tuner_inc,
non_tuner_count=state.non_tuner_count+non_tuner_inc,
level_mapper=var.level_mapper or state.level_mapper,
level_mapper_priority=var.level_mapper_priority or state.level_mapper_priority,
hand_count_limit=var.hand_count_limit or state.hand_count_limit,
hand_count=state.hand_count+hand_inc,
}
table.insert(selected,mc)
if Synchro.CanCompleteSelection(
candidates,target_level,
tuner_filter,non_tuner_filter,tc,
tuner_min,tuner_max,non_tuner_min,non_tuner_max,
base_mapper,selected,index+1,new_state,prune_indexes
)
then
table.remove(selected)
return true
end
table.remove(selected) table.remove(selected)
return true
end end
table.remove(selected)
end end
-- exclude branch -- exclude branch
...@@ -3718,6 +3628,7 @@ function Synchro.SynTarget(tuner_filter,non_tuner_filter,tuner_min,tuner_max,non ...@@ -3718,6 +3628,7 @@ function Synchro.SynTarget(tuner_filter,non_tuner_filter,tuner_min,tuner_max,non
local finishable=Synchro.IsSelectionValid(sg_arr,target_level,tuner_filter,non_tuner_filter,tc,tuner_min,tuner_max,non_tuner_min,non_tuner_max) local finishable=Synchro.IsSelectionValid(sg_arr,target_level,tuner_filter,non_tuner_filter,tc,tuner_min,tuner_max,non_tuner_min,non_tuner_max)
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SMATERIAL)
local picked=Group.SelectUnselect(addable,sg,tp,finishable,true,selected_level_min,target_level) local picked=Group.SelectUnselect(addable,sg,tp,finishable,true,selected_level_min,target_level)
if not picked then if not picked then
...@@ -3840,100 +3751,17 @@ function Synchro.BuildStatesFromSelection(selection,tuner_filter,non_tuner_filte ...@@ -3840,100 +3751,17 @@ function Synchro.BuildStatesFromSelection(selection,tuner_filter,non_tuner_filte
for idx,card in ipairs(selection)do for idx,card in ipairs(selection)do
local next_states={} local next_states={}
for _,st in ipairs(states) do for _,st in ipairs(states) do
local tuner_inc=tuner_filter(card,tc) and 1 or 0 for _,diff in ipairs(Synchro.GenerateVariantForCard(
local non_tuner_inc=non_tuner_filter(card,tc) and 1 or 0 st,card,idx,tc,tuner_filter,non_tuner_filter,prune_level
local hand_inc=card:IsLocation(LOCATION_HAND) and 1 or 0 )) do
local mapper=st.level_mapper -- merge diff into full state
local new_state = Synchro.ApplyVariantState(
-- 1) MonoSynchron branch (priority<2 → first Mono; priority==2 → full Mono) st,card,diff,tuner_filter,non_tuner_filter,tc,prune_level
if Synchro.IsMono(card) then )
if st.level_mapper_priority<2 then table.insert(next_states,new_state)
-- first Mono: that one keeps its real level, others=1 end
local firstMono=card end
local mono_mapper=function(c,tc_arg)
if c==firstMono then
return {c:GetLevel()}
else
return {1}
end
end
local sums1={[idx-1+card:GetLevel()]=true}
table.insert(next_states,{
possible_sums=sums1,
tuner_count=st.tuner_count+tuner_inc,
non_tuner_count=st.non_tuner_count+non_tuner_inc,
level_mapper=mono_mapper,
level_mapper_priority=2,
hand_count_limit=st.hand_count_limit,
hand_count=st.hand_count+hand_inc,
})
elseif st.level_mapper_priority==2 then
-- second (or later) Mono: full override—all cards=Level 1
local full_mapper=function() return {1} end
-- reset your sums_so_far into a single bucket of idx*1
local sums2={[idx]=true}
table.insert(next_states,{
possible_sums=sums2,
tuner_count=st.tuner_count+tuner_inc,
non_tuner_count=st.non_tuner_count+non_tuner_inc,
level_mapper=full_mapper,
level_mapper_priority=2, -- stays in Mono mode
hand_count_limit=st.hand_count_limit,
hand_count=st.hand_count+hand_inc,
})
end
end
if not (Synchro.IsMono(card) or Synchro.IsTatsunoko(card)) then
-- normal branch
local lvls=mapper(card,tc)
local sums_norm=Synchro.UpdatepossibleSums(st.possible_sums,lvls,prune_level)
if next(sums_norm)~=nil then
table.insert(next_states,{
possible_sums=sums_norm,
tuner_count=st.tuner_count+tuner_inc,
non_tuner_count=st.non_tuner_count+non_tuner_inc,
level_mapper=mapper,
level_mapper_priority=st.level_mapper_priority,
hand_count_limit=st.hand_count_limit,
hand_count=st.hand_count+hand_inc,
})
end
end
-- override level mapper branch if Flower Cardian
if Synchro.IsFlower(card) then
if st.level_mapper_priority<1 then
local sums_ovr={[2*idx]=true}
table.insert(next_states,{
possible_sums=sums_ovr,
tuner_count=st.tuner_count+tuner_inc,
non_tuner_count=st.non_tuner_count+non_tuner_inc,
level_mapper=function() return {2} end,
level_mapper_priority=1,
hand_count_limit=st.hand_count_limit,
hand_count=st.hand_count+hand_inc,
})
end
end
-- increase hand count limit if tatsunoko
if Synchro.IsTatsunoko(card) then
-- include Tatsunoko's own level contributions
local lvls=mapper(card,tc)
local sums_tats=Synchro.UpdatepossibleSums(st.possible_sums,lvls,prune_level)
table.insert(next_states,{
possible_sums=sums_tats,
tuner_count=st.tuner_count+tuner_inc,
non_tuner_count=st.non_tuner_count+non_tuner_inc,
level_mapper=mapper,
level_mapper_priority=st.level_mapper_priority,
hand_count_limit=1,
hand_count=st.hand_count+hand_inc,
})
end
end
states=next_states states=next_states
...@@ -3983,3 +3811,72 @@ end ...@@ -3983,3 +3811,72 @@ end
function Synchro.IsHandAlter(c) function Synchro.IsHandAlter(c)
return Synchro.IsTatsunoko(c) return Synchro.IsTatsunoko(c)
end end
-- Helper to generate only the variant-specific diffs for a card
function Synchro.GenerateVariantForCard(st,card,idx,tc,tuner_filter,non_tuner_filter,prune_level)
local variants = {}
-- MonoSynchron first/second
if Synchro.IsMono(card) then
if st.level_mapper_priority<2 then
local firstMono = card
table.insert(variants,{
possible_sums={[(idx-1)+card:GetLevel()]=true},
level_mapper=function(c)
return (c==firstMono) and {c:GetLevel()} or {1}
end,
level_mapper_priority=2,
})
elseif st.level_mapper_priority==2 then
table.insert(variants,{
possible_sums={[idx]=true},
level_mapper=function() return {1} end,
})
end
end
-- Normal branch
if not (Synchro.IsMono(card) or Synchro.IsTatsunoko(card)) then
table.insert(variants,{})
end
-- Flower Cardian override
if Synchro.IsFlower(card) and st.level_mapper_priority < 1 then
table.insert(variants,{
possible_sums={[2*idx]=true},
level_mapper=function() return {2} end,
level_mapper_priority=1,
})
end
-- Tatsunoko effect
if Synchro.IsTatsunoko(card) then
table.insert(variants,{
hand_count_limit=1,
})
end
return variants
end
-- Helper to apply a diff to a base state
function Synchro.ApplyVariantState(st,card,diff,tuner_filter,non_tuner_filter,tc,prune_level)
-- compute increments using provided filters
local tuner_inc=tuner_filter(card,tc) and 1 or 0
local non_tuner_inc=non_tuner_filter(card,tc) and 1 or 0
local hand_inc=card:IsLocation(LOCATION_HAND) and 1 or 0
local possible_sums=Synchro.UpdatepossibleSums(
st.possible_sums,
st.level_mapper(card, tc),
prune_level
)
return {
possible_sums=diff.possible_sums or possible_sums,
tuner_count=st.tuner_count+tuner_inc,
non_tuner_count=st.non_tuner_count+non_tuner_inc,
level_mapper=diff.level_mapper or st.level_mapper,
level_mapper_priority=diff.level_mapper_priority or st.level_mapper_priority,
hand_count_limit=diff.hand_count_limit or st.hand_count_limit,
hand_count=st.hand_count+hand_inc,
}
end
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