forked from ProjectIgnis/CardScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_workaround.lua
259 lines (249 loc) · 10.5 KB
/
proc_workaround.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
--Utilities to be added to the core
--[[
A monster that is temporarily banished shouldn't be treated as a monster that was Normal, Flip, or Special Summoned that turn, or as a monster that has already changed its battle position, after it returns to the field.
Manually sets the relevant statuses to "false" before returning the monster to the field.
--]]
Duel.ReturnToField=(function()
local oldfunc=Duel.ReturnToField
return function(card,pos,zone,...)
if not card:IsReason(REASON_TEMPORARY) then return false end
card:SetStatus(STATUS_FORM_CHANGED,false)
card:SetStatus(STATUS_SUMMON_TURN,false)
card:SetStatus(STATUS_FLIP_SUMMON_TURN,false)
card:SetStatus(STATUS_SPSUMMON_TURN,false)
pos=pos or card:GetPreviousPosition()
zone=zone or 0xff
return oldfunc(card,pos,zone,...)
end
end)()
--[[
Places a hint that says "Added to the hand by a currently resolving effect" (string 225) on any cards added to the hand for the duration of that Chain's/effect's resolution.
Used to differentiate which card(s) were just added to the hand and which ones were already there for cases of multiple copies of the same card being present.
The card ID used for the flag effect (30336082) belongs to "Brimming Sangen Manor", the card that initially needed this workaround.
--]]
Duel.SendtoHand=(function()
local oldfunc=Duel.SendtoHand
return function(card_or_group,dest_player,reason,reason_player,...)
local res=oldfunc(card_or_group,dest_player,reason,reason_player,...)
if res==0 then return res end
if type(card_or_group)=="Group" then
local hand_group=card_or_group:Filter(Card.IsLocation,nil,LOCATION_HAND)
for tc in hand_group:Iter() do
tc:RegisterFlagEffect(30336082,RESET_EVENT|RESETS_STANDARD|RESET_CHAIN,EFFECT_FLAG_CLIENT_HINT,1,0,225)
end
elseif type(card_or_group)=="Card" and card_or_group:IsLocation(LOCATION_HAND) then
card_or_group:RegisterFlagEffect(30336082,RESET_EVENT|RESETS_STANDARD|RESET_CHAIN,EFFECT_FLAG_CLIENT_HINT,1,0,225)
end
return res
end
end)()
--Use the "selected" string by default. Pass "false" as the boolean to use the "targeted" string instead.
Duel.HintSelection=(function()
local oldfunc=Duel.HintSelection
return function(card_or_group,log_as_selection,...)
if log_as_selection==nil then log_as_selection=true end
return oldfunc(card_or_group,log_as_selection,...)
end
end)()
--[[
If called while an effect isn't resolving (e.g. a regular Xyz Summon or through an effect like "Wonder Xyz") then proceed as usual with the attaching.
If called while an effect is resolving treat it as attaching by card effect and handle the relevant rulings.
Attaching by card effect is ruled to affect both the Xyz Monster and the cards that are to be attached.
Return early if the Xyz Monster is unaffected by the currently resolving effect.
Remove any cards that are unaffected by the currently resolving effect from the group of cards to be attached.
If all the cards to be attached are unaffected by the currently resolving effect then return early.
Proceed as usual with the attaching otherwise.
Duel.Overlay=(function()
local oldfunc=Duel.Overlay
return function(xyz_monster,xyz_mats,send_to_grave)
if not Duel.IsChainSolving() then return oldfunc(xyz_monster,xyz_mats,send_to_grave) end
local trig_eff=Duel.GetChainInfo(0,CHAININFO_TRIGGERING_EFFECT)
if xyz_monster:IsImmuneToEffect(trig_eff) then return end
if type(xyz_mats)=="Group" then
xyz_mats:Match(aux.NOT(Card.IsImmuneToEffect),nil,trig_eff)
if #xyz_mats==0 then return end
elseif type(xyz_mats)=="Card" and xyz_mats:IsImmuneToEffect(trig_eff) then
return
end
return oldfunc(xyz_monster,xyz_mats,send_to_grave)
end
end)()
--]]
--Remove counter from only 1 card if it is the only card with counter
local p_rem=Duel.RemoveCounter
function Duel.RemoveCounter(tp,s,o,counter,...)
local ex_params={...}
local s,o=s>0 and LOCATION_ONFIELD or 0,o>0 and LOCATION_ONFIELD or 0
local cg=Duel.GetFieldGroup(tp,s,o):Match(function(c) return c:GetCounter(counter)>0 end,nil)
if #cg==1 then
return cg:GetFirst():RemoveCounter(tp,counter,table.unpack(ex_params))
end
return p_rem(tp,s,o,counter,table.unpack(ex_params))
end
function Auxiliary.ReleaseNonSumCheck(c,tp,e)
if c:IsControler(tp) then return false end
local chk=false
for _,eff in ipairs({c:GetCardEffect(EFFECT_EXTRA_RELEASE_NONSUM)}) do
local val=eff:GetValue()
if type(val)=="number" then chk=val~=0
else chk=val(eff,e,REASON_COST,tp) end
if chk then return true end
end
return chk
end
function Auxiliary.ZoneCheckFunc(c,tp,zone)
if c:IsLocation(LOCATION_EXTRA) then
return function(sg) return Duel.GetLocationCountFromEx(tp,tp,sg,c) end
end
return function(sg) return Duel.GetMZoneCount(tp,sg,zone) end
end
function Auxiliary.CheckZonesReleaseSummonCheck(must,oneof,checkfunc)
return function(sg,e,tp,mg)
local count=#(oneof&sg)
return checkfunc(sg+must)>0 and count<2,count>=2
end
end
function Duel.CheckReleaseGroupSummon(c,tp,e,fil,minc,maxc,last,...)
local zone=0xff
local params={...}
local ex=last
if type(last)=="number" then
zone=last
ex=params[1]
table.remove(params,1)
end
local checkfunc=aux.ZoneCheckFunc(c,tp,zone)
local rg,rgex=Duel.GetReleaseGroup(tp,false):Match(aux.TRUE,ex):Split(fil,nil,table.unpack(params))
if #rg<minc or rgex:IsExists(Card.IsHasEffect,nil,EFFECT_EXTRA_RELEASE) then return false end
local ft=Duel.GetLocationCount(tp,LOCATION_MZONE)
local must,nonmust=rg:Split(Card.IsHasEffect,nil,EFFECT_EXTRA_RELEASE)
local mustc=#must
if mustc>maxc then return false end
if (mustc-minc>=0) and checkfunc(must)>0 then return true end
local extraoneof,nonmust=nonmust:Split(aux.ReleaseNonSumCheck,nil,tp,e)
local count=mustc+#nonmust+(#extraoneof>0 and 1 or 0)
return count>=minc and aux.SelectUnselectGroup(nonmust,e,tp,minc-mustc,maxc-mustc,aux.CheckZonesReleaseSummonCheck(must,extraoneof,checkfunc),0)
end
function Auxiliary.CheckZonesReleaseSummonCheckSelection(must,oneof,checkfunc)
return function(sg,e,tp,mg)
local count=#(oneof&sg)
return sg:Includes(must) and checkfunc(sg)>0 and count<2,count>=2
end
end
function Duel.SelectReleaseGroupSummon(c,tp,e,fil,minc,maxc,last,...)
local cancelable=false
local zone=0xff
local ex=last
local params={...}
if type(last)=="boolean" then
cancelable=last
zone=params[1]
table.remove(params,1)
ex=params[1]
table.remove(params,1)
end
local checkfunc=aux.ZoneCheckFunc(c,tp,zone)
local rg,rgex=Duel.GetReleaseGroup(tp,false):Match(aux.TRUE,ex):Split(fil,nil,...)
if #rg<minc or rgex:IsExists(Card.IsHasEffect,nil,EFFECT_EXTRA_RELEASE) then return nil end
local ft=Duel.GetLocationCount(tp,LOCATION_MZONE)
local must,nonmust=rg:Split(Card.IsHasEffect,nil,EFFECT_EXTRA_RELEASE)
local mustc=#must
if mustc>maxc then return nil end
if (mustc-maxc>=0) and checkfunc(must)>0 then return must:Select(tp,mustc,mustc,true) end
local extraoneof,nonmust=nonmust:Split(aux.ReleaseNonSumCheck,nil,tp,e)
local count=mustc+#nonmust+(#extraoneof>0 and 1 or 0)
local res=count>=minc and aux.SelectUnselectGroup(rg,e,tp,minc,maxc,aux.CheckZonesReleaseSummonCheckSelection(must,extraoneof,checkfunc),1,tp,500,function(sg,e,tp,g) return sg:Includes(must) and Duel.GetMZoneCount(tp,sg,zone)>0 end,nil,cancelable)
return #res>0 and res or nil
end
--Lair of Darkness
function Auxiliary.ReleaseCheckSingleUse(sg,tp,exg)
local ct=#sg-#(sg-exg)
return ct<=1,ct>1
end
function Auxiliary.ReleaseCheckMMZ(sg,tp)
return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
or sg:IsExists(aux.FilterBoolFunction(Card.IsInMainMZone,tp),1,nil)
end
function Auxiliary.ReleaseCheckTarget(sg,tp,exg,dg)
return dg:IsExists(aux.TRUE,1,sg)
end
function Auxiliary.RelCheckRecursive(c,tp,sg,mg,exg,mustg,ct,minc,maxc,specialchk)
sg:AddCard(c)
ct=ct+1
local res,stop=Auxiliary.RelCheckGoal(tp,sg,exg,mustg,ct,minc,maxc,specialchk)
if not res and not stop then
res=(ct<maxc and mg:IsExists(Auxiliary.RelCheckRecursive,1,sg,tp,sg,mg,exg,mustg,ct,minc,maxc,specialchk))
end
sg:RemoveCard(c)
return res
end
function Auxiliary.RelCheckGoal(tp,sg,exg,mustg,ct,minc,maxc,specialchk)
if ct<minc or ct>maxc then return false,ct>maxc end
local res,stop=specialchk(sg)
return (res and sg:Includes(mustg)),stop
end
function Auxiliary.ReleaseCostFilter(c,tp)
local eff=c:IsHasEffect(EFFECT_EXTRA_RELEASE_NONSUM)
return not (c:IsControler(1-tp) and eff and eff:CheckCountLimit(tp)) and not c:IsHasEffect(EFFECT_EXTRA_RELEASE)
end
function Auxiliary.MakeSpecialCheck(check,tp,exg,...)
local params={...}
if not check then return
function(sg)
return Auxiliary.ReleaseCheckSingleUse(sg,tp,exg)
end
end
return function(sg)
local res,stop=check(sg,tp,exg,table.unpack(params))
local res2,stop2=Auxiliary.ReleaseCheckSingleUse(sg,tp,exg)
return res and res2,stop or stop2
end
end
function Duel.CheckReleaseGroupCost(tp,f,minc,maxc,use_hand,check,ex,...)
local params={...}
if type(maxc)~="number" then
table.insert(params,1,ex)
maxc,use_hand,check,ex=minc,maxc,use_hand,check
end
if not ex then ex=Group.CreateGroup() end
local mg=Duel.GetReleaseGroup(tp,use_hand):Match(f or aux.TRUE,ex,table.unpack(params))
local g,exg=mg:Split(Auxiliary.ReleaseCostFilter,nil,tp)
local specialchk=Auxiliary.MakeSpecialCheck(check,tp,exg,table.unpack(params))
local mustg=g:Match(function(c,tp)return c:IsHasEffect(EFFECT_EXTRA_RELEASE) and c:IsControler(1-tp)end,nil,tp)
local sg=Group.CreateGroup()
return mg:Includes(mustg) and mg:IsExists(Auxiliary.RelCheckRecursive,1,nil,tp,sg,mg,exg,mustg,0,minc,maxc,specialchk)
end
function Duel.SelectReleaseGroupCost(tp,f,minc,maxc,use_hand,check,ex,...)
if not ex then ex=Group.CreateGroup() end
local mg=Duel.GetReleaseGroup(tp,use_hand):Match(f or aux.TRUE,ex,...)
local g,exg=mg:Split(Auxiliary.ReleaseCostFilter,nil,tp)
local specialchk=Auxiliary.MakeSpecialCheck(check,tp,exg,...)
local mustg=g:Match(function(c,tp)return c:IsHasEffect(EFFECT_EXTRA_RELEASE) and c:IsControler(1-tp)end,nil,tp)
local sg=Group.CreateGroup()
local cancel=false
sg:Merge(mustg)
while #sg<maxc do
local cg=mg:Filter(Auxiliary.RelCheckRecursive,sg,tp,sg,mg,exg,mustg,#sg,minc,maxc,specialchk)
if #cg==0 then break end
cancel=Auxiliary.RelCheckGoal(tp,sg,exg,mustg,#sg,minc,maxc,specialchk)
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
local tc=Group.SelectUnselect(cg,sg,tp,cancel,cancel,1,1)
if not tc then break end
if #mustg==0 or not mustg:IsContains(tc) then
if not sg:IsContains(tc) then
sg=sg+tc
else
sg=sg-tc
end
end
end
if #sg==0 then return sg end
if #(sg&exg)>0 then
local eff=(sg&exg):GetFirst():IsHasEffect(EFFECT_EXTRA_RELEASE_NONSUM)
if eff then
eff:UseCountLimit(tp,1)
Duel.Hint(HINT_CARD,0,eff:GetHandler():GetCode())
end
end
return sg
end