Lines of
script/utility.lua
from check-in ab919e5a98
that are changed by the sequence of edits moving toward
check-in e8dc830ca1:
1:
ab919e5a98 2023-07-15 2: Debug.Message(23)
3: Auxiliary={}
4: aux=Auxiliary
5: POS_FACEUP_DEFENCE=POS_FACEUP_DEFENSE
6: POS_FACEDOWN_DEFENCE=POS_FACEDOWN_DEFENSE
7: RACE_CYBERS=RACE_CYBERSE
8:
9: function GetID()
10: local offset=self_code<100000000 and 1 or 100
11: return self_table,self_code,offset
12: end
13:
14: --the lua version of the bit32 lib, which is deprecated in lua 5.3
15: bit={}
16: function bit.band(a,b)
17: return a&b
18: end
19: function bit.bor(a,b)
20: return a|b
21: end
22: function bit.bxor(a,b)
23: return a~b
24: end
25: function bit.lshift(a,b)
26: return a<<b
27: end
28: function bit.rshift(a,b)
29: return a>>b
30: end
31: function bit.bnot(a)
32: return ~a
33: end
34: local function fieldargs(f,width)
35: w=width or 1
36: assert(f>=0,"field cannot be negative")
37: assert(w>0,"width must be positive")
38: assert(f+w<=32,"trying to access non-existent bits")
39: return f,~(-1<<w)
40: end
41: function bit.extract(r,field,width)
42: local f,m=fieldargs(field,width)
43: return (r>>f)&m
44: end
45: function bit.replace(r,v,field,width)
46: local f,m=fieldargs(field,width)
47: return (r&~(m<<f))|((v&m)<< f)
48: end
49:
50: --the table of xyz number
51: Auxiliary.xyz_number={}
52: function Auxiliary.GetXyzNumber(v)
53: local id
54: if Auxiliary.GetValueType(v)=="Card" then id=v:GetCode() end
55: if Auxiliary.GetValueType(v)=="number" then id=v end
56: return Auxiliary.xyz_number[id]
57: end
58:
59: --the chain id of the results modified by EVENT_TOSS_DICE_NEGATE
60: Auxiliary.idx_table=table.pack(1,2,3,4,5,6,7,8)
61:
62: function Auxiliary.Stringid(code,id)
63: return code*16+id
64: end
65: function Auxiliary.Next(g)
66: local first=true
67: return function()
68: if first then first=false return g:GetFirst()
69: else return g:GetNext() end
70: end
71: end
72: function Auxiliary.NULL()
73: end
74: function Auxiliary.TRUE()
75: return true
76: end
77: function Auxiliary.FALSE()
78: return false
79: end
80: function Auxiliary.AND(...)
81: local function_list={...}
82: return function(...)
83: local res=false
84: for i,f in ipairs(function_list) do
85: res=f(...)
86: if not res then return res end
87: end
88: return res
89: end
90: end
91: function Auxiliary.OR(...)
92: local function_list={...}
93: return function(...)
94: local res=false
95: for i,f in ipairs(function_list) do
96: res=f(...)
97: if res then return res end
98: end
99: return res
100: end
101: end
102: function Auxiliary.NOT(f)
103: return function(...)
104: return not f(...)
105: end
106: end
107: function Auxiliary.BeginPuzzle(effect)
108: local e1=Effect.GlobalEffect()
109: e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
110: e1:SetCode(EVENT_TURN_END)
111: e1:SetCountLimit(1)
112: e1:SetOperation(Auxiliary.PuzzleOp)
113: Duel.RegisterEffect(e1,0)
114: local e2=Effect.GlobalEffect()
115: e2:SetType(EFFECT_TYPE_FIELD)
116: e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
117: e2:SetCode(EFFECT_SKIP_DP)
118: e2:SetTargetRange(1,0)
119: Duel.RegisterEffect(e2,0)
120: local e3=Effect.GlobalEffect()
121: e3:SetType(EFFECT_TYPE_FIELD)
122: e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
123: e3:SetCode(EFFECT_SKIP_SP)
124: e3:SetTargetRange(1,0)
125: Duel.RegisterEffect(e3,0)
126: end
127: function Auxiliary.PuzzleOp(e,tp)
128: Duel.SetLP(0,0)
129: end
130: --Duel.SelectOption with option condition
131: --Return value starts from 1, different from Duel.SelectOption
132: function Auxiliary.SelectFromOptions(tp,...)
133: local options={...}
134: local ops={}
135: local opvals={}
136: for i=1,#options do
137: if options[i][1] then
138: table.insert(ops,options[i][2])
139: table.insert(opvals,options[i][3] or i)
140: end
141: end
142: if #ops==0 then return nil end
143: local select=Duel.SelectOption(tp,table.unpack(ops))
144: return opvals[select+1]
145: end
146: function Auxiliary.IsDualState(effect)
147: local c=effect:GetHandler()
148: return not c:IsDisabled() and c:IsDualState()
149: end
150: function Auxiliary.IsNotDualState(effect)
151: local c=effect:GetHandler()
152: return c:IsDisabled() or not c:IsDualState()
153: end
154: function Auxiliary.DualNormalCondition(effect)
155: local c=effect:GetHandler()
156: return c:IsFaceup() and not c:IsDualState()
157: end
158: function Auxiliary.EnableDualAttribute(c)
159: local e1=Effect.CreateEffect(c)
160: e1:SetType(EFFECT_TYPE_SINGLE)
161: e1:SetCode(EFFECT_DUAL_SUMMONABLE)
162: c:RegisterEffect(e1)
163: local e2=Effect.CreateEffect(c)
164: e2:SetType(EFFECT_TYPE_SINGLE)
165: e2:SetCode(EFFECT_ADD_TYPE)
166: e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE+EFFECT_FLAG_IGNORE_IMMUNE)
167: e2:SetRange(LOCATION_MZONE+LOCATION_GRAVE)
168: e2:SetCondition(aux.DualNormalCondition)
169: e2:SetValue(TYPE_NORMAL)
170: c:RegisterEffect(e2)
171: local e3=e2:Clone()
172: e3:SetCode(EFFECT_REMOVE_TYPE)
173: e3:SetValue(TYPE_EFFECT)
174: c:RegisterEffect(e3)
175: end
176: --register effect of return to hand for Spirit monsters
177: function Auxiliary.EnableSpiritReturn(c,event1,...)
178: local e1=Effect.CreateEffect(c)
179: e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
180: e1:SetCode(event1)
181: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
182: e1:SetOperation(Auxiliary.SpiritReturnReg)
183: c:RegisterEffect(e1)
184: for i,event in ipairs{...} do
185: local e2=e1:Clone()
186: e2:SetCode(event)
187: c:RegisterEffect(e2)
188: end
189: end
190: function Auxiliary.SpiritReturnReg(e,tp,eg,ep,ev,re,r,rp)
191: local c=e:GetHandler()
192: local e1=Effect.CreateEffect(c)
193: e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
194: e1:SetDescription(1104)
195: e1:SetCategory(CATEGORY_TOHAND)
196: e1:SetCode(EVENT_PHASE+PHASE_END)
197: e1:SetRange(LOCATION_MZONE)
198: e1:SetCountLimit(1)
199: e1:SetReset(RESET_EVENT+0xd6e0000+RESET_PHASE+PHASE_END)
200: e1:SetCondition(Auxiliary.SpiritReturnConditionForced)
201: e1:SetTarget(Auxiliary.SpiritReturnTargetForced)
202: e1:SetOperation(Auxiliary.SpiritReturnOperation)
203: c:RegisterEffect(e1)
204: local e2=e1:Clone()
205: e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
206: e2:SetCondition(Auxiliary.SpiritReturnConditionOptional)
207: e2:SetTarget(Auxiliary.SpiritReturnTargetOptional)
208: c:RegisterEffect(e2)
209: end
210: function Auxiliary.SpiritReturnConditionForced(e,tp,eg,ep,ev,re,r,rp)
211: local c=e:GetHandler()
212: return not c:IsHasEffect(EFFECT_SPIRIT_DONOT_RETURN) and not c:IsHasEffect(EFFECT_SPIRIT_MAYNOT_RETURN)
213: end
214: function Auxiliary.SpiritReturnTargetForced(e,tp,eg,ep,ev,re,r,rp,chk)
215: if chk==0 then return true end
216: Duel.SetOperationInfo(0,CATEGORY_TOHAND,e:GetHandler(),1,0,0)
217: end
218: function Auxiliary.SpiritReturnConditionOptional(e,tp,eg,ep,ev,re,r,rp)
219: local c=e:GetHandler()
220: return not c:IsHasEffect(EFFECT_SPIRIT_DONOT_RETURN) and c:IsHasEffect(EFFECT_SPIRIT_MAYNOT_RETURN)
221: end
222: function Auxiliary.SpiritReturnTargetOptional(e,tp,eg,ep,ev,re,r,rp,chk)
223: if chk==0 then return e:GetHandler():IsAbleToHand() end
224: Duel.SetOperationInfo(0,CATEGORY_TOHAND,e:GetHandler(),1,0,0)
225: end
226: function Auxiliary.SpiritReturnOperation(e,tp,eg,ep,ev,re,r,rp)
227: local c=e:GetHandler()
228: if c:IsRelateToEffect(e) then
229: Duel.SendtoHand(c,nil,REASON_EFFECT)
230: end
231: end
232: function Auxiliary.EnableNeosReturn(c,operation,set_category)
233: --return
234: local e1=Effect.CreateEffect(c)
235: e1:SetDescription(1193)
236: e1:SetCategory(CATEGORY_TODECK)
237: e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
238: e1:SetCode(EVENT_PHASE+PHASE_END)
239: e1:SetRange(LOCATION_MZONE)
240: e1:SetCountLimit(1)
241: e1:SetCondition(Auxiliary.NeosReturnConditionForced)
242: e1:SetTarget(Auxiliary.NeosReturnTargetForced(set_category))
243: e1:SetOperation(operation)
244: c:RegisterEffect(e1)
245: local e2=e1:Clone()
246: e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
247: e2:SetCondition(Auxiliary.NeosReturnConditionOptional)
248: e2:SetTarget(Auxiliary.NeosReturnTargetOptional(set_category))
249: c:RegisterEffect(e2)
250: return e1,e2
251: end
252: function Auxiliary.NeosReturnConditionForced(e,tp,eg,ep,ev,re,r,rp)
253: return not e:GetHandler():IsHasEffect(42015635)
254: end
255: function Auxiliary.NeosReturnTargetForced(set_category)
256: return function(e,tp,eg,ep,ev,re,r,rp,chk)
257: if chk==0 then return true end
258: Duel.SetOperationInfo(0,CATEGORY_TODECK,e:GetHandler(),1,0,0)
259: if set_category then set_category(e,tp,eg,ep,ev,re,r,rp) end
260: end
261: end
262: function Auxiliary.NeosReturnConditionOptional(e,tp,eg,ep,ev,re,r,rp)
263: return e:GetHandler():IsHasEffect(42015635)
264: end
265: function Auxiliary.NeosReturnTargetOptional(set_category)
266: return function(e,tp,eg,ep,ev,re,r,rp,chk)
267: if chk==0 then return e:GetHandler():IsAbleToExtra() end
268: Duel.SetOperationInfo(0,CATEGORY_TODECK,e:GetHandler(),1,0,0)
269: if set_category then set_category(e,tp,eg,ep,ev,re,r,rp) end
270: end
271: end
272: function Auxiliary.IsUnionState(effect)
273: local c=effect:GetHandler()
274: return c:IsHasEffect(EFFECT_UNION_STATUS) and c:GetEquipTarget()
275: end
276: --set EFFECT_EQUIP_LIMIT after equipping
277: function Auxiliary.SetUnionState(c)
278: local eset={c:IsHasEffect(EFFECT_UNION_LIMIT)}
279: if #eset==0 then return end
280: local e0=Effect.CreateEffect(c)
281: e0:SetType(EFFECT_TYPE_SINGLE)
282: e0:SetCode(EFFECT_EQUIP_LIMIT)
283: e0:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
284: e0:SetValue(eset[1]:GetValue())
285: e0:SetReset(RESET_EVENT+RESETS_STANDARD)
286: c:RegisterEffect(e0)
287: local e1=Effect.CreateEffect(c)
288: e1:SetType(EFFECT_TYPE_SINGLE)
289: e1:SetCode(EFFECT_UNION_STATUS)
290: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
291: e1:SetReset(RESET_EVENT+RESETS_STANDARD)
292: c:RegisterEffect(e1)
293: if c.old_union then
294: local e2=e1:Clone()
295: e2:SetCode(EFFECT_OLDUNION_STATUS)
296: c:RegisterEffect(e2)
297: end
298: end
299: --uc: the union monster to be equipped, tc: the target monster
300: function Auxiliary.CheckUnionEquip(uc,tc,exclude_modern_count)
301: local modern_count,old_count=tc:GetUnionCount()
302: if exclude_modern_count then modern_count=modern_count-exclude_modern_count end
303: if uc.old_union then return modern_count==0
304: else return old_count==0 end
305: end
306: --EFFECT_DESTROY_SUBSTITUTE filter for modern union monsters
307: function Auxiliary.UnionReplaceFilter(e,re,r,rp)
308: return r&(REASON_BATTLE+REASON_EFFECT)~=0
309: end
310: --add effect to modern union monsters
311: function Auxiliary.EnableUnionAttribute(c,f)
312: --destroy sub
313: local e1=Effect.CreateEffect(c)
314: e1:SetType(EFFECT_TYPE_EQUIP)
315: e1:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE)
316: e1:SetCode(EFFECT_DESTROY_SUBSTITUTE)
317: e1:SetValue(aux.UnionReplaceFilter)
318: c:RegisterEffect(e1)
319: --limit
320: local e2=Effect.CreateEffect(c)
321: e2:SetType(EFFECT_TYPE_SINGLE)
322: e2:SetCode(EFFECT_UNION_LIMIT)
323: e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
324: e2:SetValue(f)
325: c:RegisterEffect(e2)
326: end
327: function Auxiliary.EnableChangeCode(c,code,location,condition)
328: Auxiliary.AddCodeList(c,code)
329: local loc=c:GetOriginalType()&TYPE_MONSTER~=0 and LOCATION_MZONE or LOCATION_SZONE
330: loc=location or loc
331: if condition==nil then condition=Auxiliary.TRUE end
332: local e1=Effect.CreateEffect(c)
333: e1:SetType(EFFECT_TYPE_SINGLE)
334: e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
335: e1:SetCode(EFFECT_CHANGE_CODE)
336: e1:SetRange(loc)
337: e1:SetCondition(condition)
338: e1:SetValue(code)
339: c:RegisterEffect(e1)
340: return e1
341: end
342: function Auxiliary.TargetEqualFunction(f,value,...)
343: local ext_params={...}
344: return function(effect,target)
345: return f(target,table.unpack(ext_params))==value
346: end
347: end
348: function Auxiliary.TargetBoolFunction(f,...)
349: local ext_params={...}
350: return function(effect,target)
351: return f(target,table.unpack(ext_params))
352: end
353: end
354: function Auxiliary.FilterEqualFunction(f,value,...)
355: local ext_params={...}
356: return function(target)
357: return f(target,table.unpack(ext_params))==value
358: end
359: end
360: function Auxiliary.FilterBoolFunction(f,...)
361: local ext_params={...}
362: return function(target)
363: return f(target,table.unpack(ext_params))
364: end
365: end
366: function Auxiliary.Tuner(f,...)
367: local ext_params={...}
368: return function(target,syncard)
369: return target:IsTuner(syncard) and (not f or f(target,table.unpack(ext_params)))
370: end
371: end
372: function Auxiliary.NonTuner(f,...)
373: local ext_params={...}
374: return function(target,syncard)
375: return target:IsNotTuner(syncard) and (not f or f(target,table.unpack(ext_params)))
376: end
377: end
378: function Auxiliary.GetValueType(v)
379: local t=type(v)
380: if t=="userdata" then
381: local mt=getmetatable(v)
382: if mt==Group then return "Group"
383: elseif mt==Effect then return "Effect"
384: else return "Card" end
385: else return t end
386: end
387: function Auxiliary.MustMaterialCheck(v,tp,code)
388: local g=Duel.GetMustMaterial(tp,code)
389: if not v then
390: if code==EFFECT_MUST_BE_XMATERIAL and Duel.IsPlayerAffectedByEffect(tp,67120578) then return false end
391: return #g==0
392: end
393: return Duel.CheckMustMaterial(tp,v,code)
394: end
395: function Auxiliary.MustMaterialCounterFilter(c,g)
396: return not g:IsContains(c)
397: end
398: --Synchro monster, 1 tuner + min to max monsters
399: function Auxiliary.AddSynchroProcedure(c,f1,f2,minc,maxc)
400: if maxc==nil then maxc=99 end
401: local e1=Effect.CreateEffect(c)
402: e1:SetDescription(1164)
403: e1:SetType(EFFECT_TYPE_FIELD)
404: e1:SetCode(EFFECT_SPSUMMON_PROC)
405: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
406: e1:SetRange(LOCATION_EXTRA)
407: e1:SetCondition(Auxiliary.SynCondition(f1,f2,minc,maxc))
408: e1:SetTarget(Auxiliary.SynTarget(f1,f2,minc,maxc))
409: e1:SetOperation(Auxiliary.SynOperation(f1,f2,minc,maxc))
410: e1:SetValue(SUMMON_TYPE_SYNCHRO)
411: c:RegisterEffect(e1)
412: end
413: function Auxiliary.SynCondition(f1,f2,minc,maxc)
414: return function(e,c,smat,mg,min,max)
415: if c==nil then return true end
416: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
417: local minc=minc
418: local maxc=maxc
419: if min then
420: if min>minc then minc=min end
421: if max<maxc then maxc=max end
422: if minc>maxc then return false end
423: end
424: if smat and smat:IsTuner(c) and (not f1 or f1(smat)) then
425: return Duel.CheckTunerMaterial(c,smat,f1,f2,minc,maxc,mg) end
426: return Duel.CheckSynchroMaterial(c,f1,f2,minc,maxc,smat,mg)
427: end
428: end
429: function Auxiliary.SynTarget(f1,f2,minc,maxc)
430: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,smat,mg,min,max)
431: local minc=minc
432: local maxc=maxc
433: if min then
434: if min>minc then minc=min end
435: if max<maxc then maxc=max end
436: if minc>maxc then return false end
437: end
438: local g=nil
439: if smat and smat:IsTuner(c) and (not f1 or f1(smat)) then
440: g=Duel.SelectTunerMaterial(c:GetControler(),c,smat,f1,f2,minc,maxc,mg)
441: else
442: g=Duel.SelectSynchroMaterial(c:GetControler(),c,f1,f2,minc,maxc,smat,mg)
443: end
444: if g then
445: g:KeepAlive()
446: e:SetLabelObject(g)
447: return true
448: else return false end
449: end
450: end
451: function Auxiliary.SynOperation(f1,f2,minct,maxc)
452: return function(e,tp,eg,ep,ev,re,r,rp,c,smat,mg,min,max)
453: local g=e:GetLabelObject()
454: c:SetMaterial(g)
455: Duel.SendtoGrave(g,REASON_MATERIAL+REASON_SYNCHRO)
456: g:DeleteGroup()
457: end
458: end
459: --Synchro monster, 1 tuner + 1 monster
460: --backward compatibility
461: function Auxiliary.AddSynchroProcedure2(c,f1,f2)
462: Auxiliary.AddSynchroProcedure(c,f1,f2,1,1)
463: end
464: --Synchro monster, f1~f3 each 1 MONSTER + f4 min to max monsters
465: function Auxiliary.AddSynchroMixProcedure(c,f1,f2,f3,f4,minc,maxc,gc)
466: local e1=Effect.CreateEffect(c)
467: e1:SetDescription(1164)
468: e1:SetType(EFFECT_TYPE_FIELD)
469: e1:SetCode(EFFECT_SPSUMMON_PROC)
470: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
471: e1:SetRange(LOCATION_EXTRA)
472: e1:SetCondition(Auxiliary.SynMixCondition(f1,f2,f3,f4,minc,maxc,gc))
473: e1:SetTarget(Auxiliary.SynMixTarget(f1,f2,f3,f4,minc,maxc,gc))
474: e1:SetOperation(Auxiliary.SynMixOperation(f1,f2,f3,f4,minc,maxc,gc))
475: e1:SetValue(SUMMON_TYPE_SYNCHRO)
476: c:RegisterEffect(e1)
477: end
478: function Auxiliary.SynMaterialFilter(c,syncard)
479: return c:IsFaceupEx() and c:IsCanBeSynchroMaterial(syncard)
480: end
481: function Auxiliary.SynLimitFilter(c,f,e,syncard)
482: return f and not f(e,c,syncard)
483: end
484: function Auxiliary.GetSynchroLevelFlowerCardian(c)
485: return 2
486: end
487: function Auxiliary.GetSynMaterials(tp,syncard)
488: local mg=Duel.GetSynchroMaterial(tp):Filter(Auxiliary.SynMaterialFilter,nil,syncard)
489: if mg:IsExists(Card.GetHandSynchro,1,nil) then
490: local mg2=Duel.GetMatchingGroup(Card.IsCanBeSynchroMaterial,tp,LOCATION_HAND,0,nil,syncard)
491: if mg2:GetCount()>0 then mg:Merge(mg2) end
492: end
493: return mg
494: end
495: function Auxiliary.SynMixCondition(f1,f2,f3,f4,minc,maxc,gc)
496: return function(e,c,smat,mg1,min,max)
497: if c==nil then return true end
498: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
499: local minc=minc
500: local maxc=maxc
501: if min then
502: if min>minc then minc=min end
503: if max<maxc then maxc=max end
504: if minc>maxc then return false end
505: end
506: if smat and not smat:IsCanBeSynchroMaterial(c) then return false end
507: local tp=c:GetControler()
508: local mg
509: local mgchk=false
510: if mg1 then
511: mg=mg1
512: mgchk=true
513: else
514: mg=Auxiliary.GetSynMaterials(tp,c)
515: end
516: if smat~=nil then mg:AddCard(smat) end
517: return mg:IsExists(Auxiliary.SynMixFilter1,1,nil,f1,f2,f3,f4,minc,maxc,c,mg,smat,gc,mgchk)
518: end
519: end
520: function Auxiliary.SynMixTarget(f1,f2,f3,f4,minc,maxc,gc)
521: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,smat,mg1,min,max)
522: local minc=minc
523: local maxc=maxc
524: if min then
525: if min>minc then minc=min end
526: if max<maxc then maxc=max end
527: if minc>maxc then return false end
528: end
529: ::SynMixTargetSelectStart::
530: local g=Group.CreateGroup()
531: local mg
532: local mgchk=false
533: if mg1 then
534: mg=mg1
535: mgchk=true
536: else
537: mg=Auxiliary.GetSynMaterials(tp,c)
538: end
539: if smat~=nil then mg:AddCard(smat) end
540: local c1
541: local c2
542: local c3
543: local cancel=Duel.IsSummonCancelable()
544: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SMATERIAL)
545: c1=mg:Filter(Auxiliary.SynMixFilter1,nil,f1,f2,f3,f4,minc,maxc,c,mg,smat,gc,mgchk):SelectUnselect(g,tp,false,cancel,1,1)
546: if not c1 then return false end
547: g:AddCard(c1)
548: if f2 then
549: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SMATERIAL)
550: c2=mg:Filter(Auxiliary.SynMixFilter2,g,f2,f3,f4,minc,maxc,c,mg,smat,c1,gc,mgchk):SelectUnselect(g,tp,false,cancel,1,1)
551: if not c2 then return false end
552: if g:IsContains(c2) then goto SynMixTargetSelectStart end
553: g:AddCard(c2)
554: if f3 then
555: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SMATERIAL)
556: c3=mg:Filter(Auxiliary.SynMixFilter3,g,f3,f4,minc,maxc,c,mg,smat,c1,c2,gc,mgchk):SelectUnselect(g,tp,false,cancel,1,1)
557: if not c3 then return false end
558: if g:IsContains(c3) then goto SynMixTargetSelectStart end
559: g:AddCard(c3)
560: end
561: end
562: local g4=Group.CreateGroup()
563: for i=0,maxc-1 do
564: local mg2=mg:Clone()
565: if f4 then
566: mg2=mg2:Filter(f4,g,c,c1,c2,c3)
567: else
568: mg2:Sub(g)
569: end
570: local cg=mg2:Filter(Auxiliary.SynMixCheckRecursive,g4,tp,g4,mg2,i,minc,maxc,c,g,smat,gc,mgchk)
571: if cg:GetCount()==0 then break end
572: local finish=Auxiliary.SynMixCheckGoal(tp,g4,minc,i,c,g,smat,gc,mgchk)
573: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SMATERIAL)
574: local c4=cg:SelectUnselect(g+g4,tp,finish,cancel,minc,maxc)
575: if not c4 then
576: if finish then break
577: else return false end
578: end
579: if g:IsContains(c4) or g4:IsContains(c4) then goto SynMixTargetSelectStart end
580: g4:AddCard(c4)
581: end
582: g:Merge(g4)
583: if g:GetCount()>0 then
584: g:KeepAlive()
585: e:SetLabelObject(g)
586: return true
587: else return false end
588: end
589: end
590: function Auxiliary.SynMixOperation(f1,f2,f3,f4,minct,maxc,gc)
591: return function(e,tp,eg,ep,ev,re,r,rp,c,smat,mg,min,max)
592: local g=e:GetLabelObject()
593: c:SetMaterial(g)
594: Duel.SendtoGrave(g,REASON_MATERIAL+REASON_SYNCHRO)
595: g:DeleteGroup()
596: end
597: end
598: function Auxiliary.SynMixFilter1(c,f1,f2,f3,f4,minc,maxc,syncard,mg,smat,gc,mgchk)
599: return (not f1 or f1(c,syncard)) and mg:IsExists(Auxiliary.SynMixFilter2,1,c,f2,f3,f4,minc,maxc,syncard,mg,smat,c,gc,mgchk)
600: end
601: function Auxiliary.SynMixFilter2(c,f2,f3,f4,minc,maxc,syncard,mg,smat,c1,gc,mgchk)
602: if f2 then
603: return f2(c,syncard,c1)
604: and (mg:IsExists(Auxiliary.SynMixFilter3,1,Group.FromCards(c1,c),f3,f4,minc,maxc,syncard,mg,smat,c1,c,gc,mgchk)
605: or minc==0 and Auxiliary.SynMixFilter4(c,nil,1,1,syncard,mg,smat,c1,nil,nil,gc,mgchk))
606: else
607: return mg:IsExists(Auxiliary.SynMixFilter4,1,c1,f4,minc,maxc,syncard,mg,smat,c1,nil,nil,gc,mgchk)
608: end
609: end
610: function Auxiliary.SynMixFilter3(c,f3,f4,minc,maxc,syncard,mg,smat,c1,c2,gc,mgchk)
611: if f3 then
612: return f3(c,syncard,c1,c2)
613: and (mg:IsExists(Auxiliary.SynMixFilter4,1,Group.FromCards(c1,c2,c),f4,minc,maxc,syncard,mg,smat,c1,c2,c,gc,mgchk)
614: or minc==0 and Auxiliary.SynMixFilter4(c,nil,1,1,syncard,mg,smat,c1,c2,nil,gc,mgchk))
615: else
616: return mg:IsExists(Auxiliary.SynMixFilter4,1,Group.FromCards(c1,c2),f4,minc,maxc,syncard,mg,smat,c1,c2,nil,gc,mgchk)
617: end
618: end
619: function Auxiliary.SynMixFilter4(c,f4,minc,maxc,syncard,mg1,smat,c1,c2,c3,gc,mgchk)
620: if f4 and not f4(c,syncard,c1,c2,c3) then return false end
621: local sg=Group.FromCards(c1,c)
622: sg:AddCard(c1)
623: if c2 then sg:AddCard(c2) end
624: if c3 then sg:AddCard(c3) end
625: local mg=mg1:Clone()
626: if f4 then
627: mg=mg:Filter(f4,sg,syncard,c1,c2,c3)
628: else
629: mg:Sub(sg)
630: end
631: return Auxiliary.SynMixCheck(mg,sg,minc-1,maxc-1,syncard,smat,gc,mgchk)
632: end
633: function Auxiliary.SynMixCheck(mg,sg1,minc,maxc,syncard,smat,gc,mgchk)
634: local tp=syncard:GetControler()
635: local sg=Group.CreateGroup()
636: if minc<=0 and Auxiliary.SynMixCheckGoal(tp,sg1,0,0,syncard,sg,smat,gc,mgchk) then return true end
637: if maxc==0 then return false end
638: return mg:IsExists(Auxiliary.SynMixCheckRecursive,1,nil,tp,sg,mg,0,minc,maxc,syncard,sg1,smat,gc,mgchk)
639: end
640: function Auxiliary.SynMixCheckRecursive(c,tp,sg,mg,ct,minc,maxc,syncard,sg1,smat,gc,mgchk)
641: sg:AddCard(c)
642: ct=ct+1
643: local res=Auxiliary.SynMixCheckGoal(tp,sg,minc,ct,syncard,sg1,smat,gc,mgchk)
644: or (ct<maxc and mg:IsExists(Auxiliary.SynMixCheckRecursive,1,sg,tp,sg,mg,ct,minc,maxc,syncard,sg1,smat,gc,mgchk))
645: sg:RemoveCard(c)
646: ct=ct-1
647: return res
648: end
649: -- the material is in hand and don't has extra synchro material effect itself
650: -- that mean some other tuner added it as material
651: function Auxiliary.SynMixHandFilter(c,tp,syncard)
652: if not c:IsLocation(LOCATION_HAND) then return false end
653: local le={c:IsHasEffect(EFFECT_EXTRA_SYNCHRO_MATERIAL,tp)}
654: for _,te in pairs(le) do
655: local tf=te:GetValue()
656: if Auxiliary.GetValueType(tf)=="function" then
657: if tf(te,syncard) then return false end
658: else
659: if tf~=0 then return false end
660: end
661: end
662: return true
663: end
664: function Auxiliary.SynMixCheckGoal(tp,sg,minc,ct,syncard,sg1,smat,gc,mgchk)
665: if ct<minc then return false end
666: local g=sg:Clone()
667: g:Merge(sg1)
668: if Duel.GetLocationCountFromEx(tp,tp,g,syncard)<=0 then return false end
669: if gc and not gc(g) then return false end
670: if smat and not g:IsContains(smat) then return false end
671: if not Auxiliary.MustMaterialCheck(g,tp,EFFECT_MUST_BE_SMATERIAL) then return false end
672: if not g:CheckWithSumEqual(Card.GetSynchroLevel,syncard:GetLevel(),g:GetCount(),g:GetCount(),syncard)
673: and (not g:IsExists(Card.IsHasEffect,1,nil,89818984)
674: or not g:CheckWithSumEqual(Auxiliary.GetSynchroLevelFlowerCardian,syncard:GetLevel(),g:GetCount(),g:GetCount(),syncard))
675: then return false end
676: local hg=g:Filter(Auxiliary.SynMixHandFilter,nil,tp,syncard)
677: local hct=hg:GetCount()
678: if hct>0 and not mgchk then
679: local found=false
680: for c in aux.Next(g) do
681: local he,hf,hmin,hmax=c:GetHandSynchro()
682: if he then
683: found=true
684: if hf and hg:IsExists(Auxiliary.SynLimitFilter,1,c,hf,he,syncard) then return false end
685: if (hmin and hct<hmin) or (hmax and hct>hmax) then return false end
686: end
687: end
688: if not found then return false end
689: end
690: for c in aux.Next(g) do
691: local le,lf,lloc,lmin,lmax=c:GetTunerLimit()
692: if le then
693: local lct=g:GetCount()-1
694: if lloc then
695: local llct=g:FilterCount(Card.IsLocation,c,lloc)
696: if llct~=lct then return false end
697: end
698: if lf and g:IsExists(Auxiliary.SynLimitFilter,1,c,lf,le,syncard) then return false end
699: if (lmin and lct<lmin) or (lmax and lct>lmax) then return false end
700: end
701: end
702: return true
703: end
704: --Checking Tune Magician
705: function Auxiliary.TuneMagicianFilter(c,e)
706: local f=e:GetValue()
707: return f(e,c)
708: end
709: function Auxiliary.TuneMagicianCheckX(c,sg,ecode)
710: local eset={c:IsHasEffect(ecode)}
711: for _,te in pairs(eset) do
712: if sg:IsExists(Auxiliary.TuneMagicianFilter,1,c,te) then return true end
713: end
714: return false
715: end
716: function Auxiliary.TuneMagicianCheckAdditionalX(ecode)
717: return function(g)
718: return not g:IsExists(Auxiliary.TuneMagicianCheckX,1,nil,g,ecode)
719: end
720: end
721: function Auxiliary.XyzAlterFilter(c,alterf,xyzc,e,tp,alterop)
722: return alterf(c) and c:IsCanBeXyzMaterial(xyzc) and Duel.GetLocationCountFromEx(tp,tp,c,xyzc)>0
723: and Auxiliary.MustMaterialCheck(c,tp,EFFECT_MUST_BE_XMATERIAL) and (not alterop or alterop(e,tp,0,c))
724: end
725: --Xyz monster, lv k*n
726: function Auxiliary.AddXyzProcedure(c,f,lv,ct,alterf,alterdesc,maxct,alterop)
727: local e1=Effect.CreateEffect(c)
728: e1:SetDescription(1165)
729: e1:SetType(EFFECT_TYPE_FIELD)
730: e1:SetCode(EFFECT_SPSUMMON_PROC)
731: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
732: e1:SetRange(LOCATION_EXTRA)
733: if not maxct then maxct=ct end
734: if alterf then
735: e1:SetCondition(Auxiliary.XyzConditionAlter(f,lv,ct,maxct,alterf,alterdesc,alterop))
736: e1:SetTarget(Auxiliary.XyzTargetAlter(f,lv,ct,maxct,alterf,alterdesc,alterop))
737: e1:SetOperation(Auxiliary.XyzOperationAlter(f,lv,ct,maxct,alterf,alterdesc,alterop))
738: else
739: e1:SetCondition(Auxiliary.XyzCondition(f,lv,ct,maxct))
740: e1:SetTarget(Auxiliary.XyzTarget(f,lv,ct,maxct))
741: e1:SetOperation(Auxiliary.XyzOperation(f,lv,ct,maxct))
742: end
743: e1:SetValue(SUMMON_TYPE_XYZ)
744: c:RegisterEffect(e1)
745: end
746: --Xyz Summon(normal)
747: function Auxiliary.XyzCondition(f,lv,minc,maxc)
748: --og: use special material
749: return function(e,c,og,min,max)
750: if c==nil then return true end
751: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
752: local tp=c:GetControler()
753: local minc=minc
754: local maxc=maxc
755: if min then
756: if min>minc then minc=min end
757: if max<maxc then maxc=max end
758: if minc>maxc then return false end
759: end
760: return Duel.CheckXyzMaterial(c,f,lv,minc,maxc,og)
761: end
762: end
763: function Auxiliary.XyzTarget(f,lv,minc,maxc)
764: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,og,min,max)
765: if og and not min then
766: return true
767: end
768: local minc=minc
769: local maxc=maxc
770: if min then
771: if min>minc then minc=min end
772: if max<maxc then maxc=max end
773: end
774: local g=Duel.SelectXyzMaterial(tp,c,f,lv,minc,maxc,og)
775: if g then
776: g:KeepAlive()
777: e:SetLabelObject(g)
778: return true
779: else return false end
780: end
781: end
782: function Auxiliary.XyzOperation(f,lv,minc,maxc)
783: return function(e,tp,eg,ep,ev,re,r,rp,c,og,min,max)
784: if og and not min then
785: local sg=Group.CreateGroup()
786: local tc=og:GetFirst()
787: while tc do
788: local sg1=tc:GetOverlayGroup()
789: sg:Merge(sg1)
790: tc=og:GetNext()
791: end
792: Duel.SendtoGrave(sg,REASON_RULE)
793: c:SetMaterial(og)
794: Duel.Overlay(c,og)
795: else
796: local mg=e:GetLabelObject()
797: local sg=Group.CreateGroup()
798: local tc=mg:GetFirst()
799: while tc do
800: local sg1=tc:GetOverlayGroup()
801: sg:Merge(sg1)
802: tc=mg:GetNext()
803: end
804: Duel.SendtoGrave(sg,REASON_RULE)
805: c:SetMaterial(mg)
806: Duel.Overlay(c,mg)
807: mg:DeleteGroup()
808: end
809: end
810: end
811: --Xyz summon(alterf)
812: function Auxiliary.XyzConditionAlter(f,lv,minc,maxc,alterf,alterdesc,alterop)
813: return function(e,c,og,min,max)
814: if c==nil then return true end
815: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
816: local tp=c:GetControler()
817: local mg=nil
818: if og then
819: mg=og
820: else
821: mg=Duel.GetFieldGroup(tp,LOCATION_MZONE,0)
822: end
823: if (not min or min<=1) and mg:IsExists(Auxiliary.XyzAlterFilter,1,nil,alterf,c,e,tp,alterop) then
824: return true
825: end
826: local minc=minc
827: local maxc=maxc
828: if min then
829: if min>minc then minc=min end
830: if max<maxc then maxc=max end
831: if minc>maxc then return false end
832: end
833: return Duel.CheckXyzMaterial(c,f,lv,minc,maxc,og)
834: end
835: end
836: function Auxiliary.XyzTargetAlter(f,lv,minc,maxc,alterf,alterdesc,alterop)
837: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,og,min,max)
838: if og and not min then
839: return true
840: end
841: local minc=minc
842: local maxc=maxc
843: if min then
844: if min>minc then minc=min end
845: if max<maxc then maxc=max end
846: end
847: local mg=nil
848: if og then
849: mg=og
850: else
851: mg=Duel.GetFieldGroup(tp,LOCATION_MZONE,0)
852: end
853: local altg=mg:Filter(Auxiliary.XyzAlterFilter,nil,alterf,c,e,tp,alterop)
854: local b1=Duel.CheckXyzMaterial(c,f,lv,minc,maxc,og)
855: local b2=(not min or min<=1) and #altg>0
856: local g=nil
857: local cancel=Duel.IsSummonCancelable()
858: if b2 and (not b1 or Duel.SelectYesNo(tp,alterdesc)) then
859: e:SetLabel(1)
860: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
861: local tc=altg:SelectUnselect(nil,tp,false,cancel,1,1)
862: if tc then
863: g=Group.FromCards(tc)
864: if alterop then alterop(e,tp,1,tc) end
865: end
866: else
867: e:SetLabel(0)
868: g=Duel.SelectXyzMaterial(tp,c,f,lv,minc,maxc,og)
869: end
870: if g then
871: g:KeepAlive()
872: e:SetLabelObject(g)
873: return true
874: else return false end
875: end
876: end
877: function Auxiliary.XyzOperationAlter(f,lv,minc,maxc,alterf,alterdesc,alterop)
878: return function(e,tp,eg,ep,ev,re,r,rp,c,og,min,max)
879: if og and not min then
880: local sg=Group.CreateGroup()
881: local tc=og:GetFirst()
882: while tc do
883: local sg1=tc:GetOverlayGroup()
884: sg:Merge(sg1)
885: tc=og:GetNext()
886: end
887: Duel.SendtoGrave(sg,REASON_RULE)
888: c:SetMaterial(og)
889: Duel.Overlay(c,og)
890: else
891: local mg=e:GetLabelObject()
892: if e:GetLabel()==1 then
893: local mg2=mg:GetFirst():GetOverlayGroup()
894: if mg2:GetCount()~=0 then
895: Duel.Overlay(c,mg2)
896: end
897: else
898: local sg=Group.CreateGroup()
899: local tc=mg:GetFirst()
900: while tc do
901: local sg1=tc:GetOverlayGroup()
902: sg:Merge(sg1)
903: tc=mg:GetNext()
904: end
905: Duel.SendtoGrave(sg,REASON_RULE)
906: end
907: c:SetMaterial(mg)
908: Duel.Overlay(c,mg)
909: mg:DeleteGroup()
910: end
911: end
912: end
913: function Auxiliary.AddXyzProcedureLevelFree(c,f,gf,minc,maxc,alterf,alterdesc,alterop)
914: local e1=Effect.CreateEffect(c)
915: e1:SetDescription(1165)
916: e1:SetType(EFFECT_TYPE_FIELD)
917: e1:SetCode(EFFECT_SPSUMMON_PROC)
918: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
919: e1:SetRange(LOCATION_EXTRA)
920: if alterf then
921: e1:SetCondition(Auxiliary.XyzLevelFreeConditionAlter(f,gf,minc,maxc,alterf,alterdesc,alterop))
922: e1:SetTarget(Auxiliary.XyzLevelFreeTargetAlter(f,gf,minc,maxc,alterf,alterdesc,alterop))
923: e1:SetOperation(Auxiliary.XyzLevelFreeOperationAlter(f,gf,minc,maxc,alterf,alterdesc,alterop))
924: else
925: e1:SetCondition(Auxiliary.XyzLevelFreeCondition(f,gf,minc,maxc))
926: e1:SetTarget(Auxiliary.XyzLevelFreeTarget(f,gf,minc,maxc))
927: e1:SetOperation(Auxiliary.XyzLevelFreeOperation(f,gf,minc,maxc))
928: end
929: e1:SetValue(SUMMON_TYPE_XYZ)
930: c:RegisterEffect(e1)
931: end
932: --Xyz Summon(level free)
933: function Auxiliary.XyzLevelFreeFilter(c,xyzc,f)
934: return (not c:IsOnField() or c:IsFaceup()) and c:IsCanBeXyzMaterial(xyzc) and (not f or f(c,xyzc))
935: end
936: function Auxiliary.XyzLevelFreeGoal(g,tp,xyzc,gf)
937: return (not gf or gf(g)) and Duel.GetLocationCountFromEx(tp,tp,g,xyzc)>0
938: end
939: function Auxiliary.XyzLevelFreeCondition(f,gf,minct,maxct)
940: return function(e,c,og,min,max)
941: if c==nil then return true end
942: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
943: local tp=c:GetControler()
944: local minc=minct
945: local maxc=maxct
946: if min then
947: minc=math.max(minc,min)
948: maxc=math.min(maxc,max)
949: end
950: if maxc<minc then return false end
951: local mg=nil
952: if og then
953: mg=og:Filter(Auxiliary.XyzLevelFreeFilter,nil,c,f)
954: else
955: mg=Duel.GetMatchingGroup(Auxiliary.XyzLevelFreeFilter,tp,LOCATION_MZONE,0,nil,c,f)
956: end
957: local sg=Duel.GetMustMaterial(tp,EFFECT_MUST_BE_XMATERIAL)
958: if sg:IsExists(Auxiliary.MustMaterialCounterFilter,1,nil,mg) then return false end
959: Duel.SetSelectedCard(sg)
960: Auxiliary.GCheckAdditional=Auxiliary.TuneMagicianCheckAdditionalX(EFFECT_TUNE_MAGICIAN_X)
961: local res=mg:CheckSubGroup(Auxiliary.XyzLevelFreeGoal,minc,maxc,tp,c,gf)
962: Auxiliary.GCheckAdditional=nil
963: return res
964: end
965: end
966: function Auxiliary.XyzLevelFreeTarget(f,gf,minct,maxct)
967: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,og,min,max)
968: if og and not min then
969: return true
970: end
971: local minc=minct
972: local maxc=maxct
973: if min then
974: if min>minc then minc=min end
975: if max<maxc then maxc=max end
976: end
977: local mg=nil
978: if og then
979: mg=og:Filter(Auxiliary.XyzLevelFreeFilter,nil,c,f)
980: else
981: mg=Duel.GetMatchingGroup(Auxiliary.XyzLevelFreeFilter,tp,LOCATION_MZONE,0,nil,c,f)
982: end
983: local sg=Duel.GetMustMaterial(tp,EFFECT_MUST_BE_XMATERIAL)
984: Duel.SetSelectedCard(sg)
985: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
986: local cancel=Duel.IsSummonCancelable()
987: Auxiliary.GCheckAdditional=Auxiliary.TuneMagicianCheckAdditionalX(EFFECT_TUNE_MAGICIAN_X)
988: local g=mg:SelectSubGroup(tp,Auxiliary.XyzLevelFreeGoal,cancel,minc,maxc,tp,c,gf)
989: Auxiliary.GCheckAdditional=nil
990: if g and g:GetCount()>0 then
991: g:KeepAlive()
992: e:SetLabelObject(g)
993: return true
994: else return false end
995: end
996: end
997: function Auxiliary.XyzLevelFreeOperation(f,gf,minct,maxct)
998: return function(e,tp,eg,ep,ev,re,r,rp,c,og,min,max)
999: if og and not min then
1000: local sg=Group.CreateGroup()
1001: local tc=og:GetFirst()
1002: while tc do
1003: local sg1=tc:GetOverlayGroup()
1004: sg:Merge(sg1)
1005: tc=og:GetNext()
1006: end
1007: Duel.SendtoGrave(sg,REASON_RULE)
1008: c:SetMaterial(og)
1009: Duel.Overlay(c,og)
1010: else
1011: local mg=e:GetLabelObject()
1012: if e:GetLabel()==1 then
1013: local mg2=mg:GetFirst():GetOverlayGroup()
1014: if mg2:GetCount()~=0 then
1015: Duel.Overlay(c,mg2)
1016: end
1017: else
1018: local sg=Group.CreateGroup()
1019: local tc=mg:GetFirst()
1020: while tc do
1021: local sg1=tc:GetOverlayGroup()
1022: sg:Merge(sg1)
1023: tc=mg:GetNext()
1024: end
1025: Duel.SendtoGrave(sg,REASON_RULE)
1026: end
1027: c:SetMaterial(mg)
1028: Duel.Overlay(c,mg)
1029: mg:DeleteGroup()
1030: end
1031: end
1032: end
1033: --Xyz summon(level free&alterf)
1034: function Auxiliary.XyzLevelFreeConditionAlter(f,gf,minct,maxct,alterf,alterdesc,alterop)
1035: return function(e,c,og,min,max)
1036: if c==nil then return true end
1037: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
1038: local tp=c:GetControler()
1039: local mg=nil
1040: if og then
1041: mg=og
1042: else
1043: mg=Duel.GetFieldGroup(tp,LOCATION_MZONE,0)
1044: end
1045: local altg=mg:Filter(Auxiliary.XyzAlterFilter,nil,alterf,c,e,tp,alterop)
1046: if (not min or min<=1) and altg:GetCount()>0 then
1047: return true
1048: end
1049: local minc=minct
1050: local maxc=maxct
1051: if min then
1052: if min>minc then minc=min end
1053: if max<maxc then maxc=max end
1054: if minc>maxc then return false end
1055: end
1056: mg=mg:Filter(Auxiliary.XyzLevelFreeFilter,nil,c,f)
1057: local sg=Duel.GetMustMaterial(tp,EFFECT_MUST_BE_XMATERIAL)
1058: if sg:IsExists(Auxiliary.MustMaterialCounterFilter,1,nil,mg) then return false end
1059: Duel.SetSelectedCard(sg)
1060: Auxiliary.GCheckAdditional=Auxiliary.TuneMagicianCheckAdditionalX(EFFECT_TUNE_MAGICIAN_X)
1061: local res=mg:CheckSubGroup(Auxiliary.XyzLevelFreeGoal,minc,maxc,tp,c,gf)
1062: Auxiliary.GCheckAdditional=nil
1063: return res
1064: end
1065: end
1066: function Auxiliary.XyzLevelFreeTargetAlter(f,gf,minct,maxct,alterf,alterdesc,alterop)
1067: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,og,min,max)
1068: if og and not min then
1069: return true
1070: end
1071: local minc=minct
1072: local maxc=maxct
1073: if min then
1074: if min>minc then minc=min end
1075: if max<maxc then maxc=max end
1076: end
1077: local mg=nil
1078: if og then
1079: mg=og
1080: else
1081: mg=Duel.GetFieldGroup(tp,LOCATION_MZONE,0)
1082: end
1083: local sg=Duel.GetMustMaterial(tp,EFFECT_MUST_BE_XMATERIAL)
1084: local altg=mg:Filter(Auxiliary.XyzAlterFilter,nil,alterf,c,e,tp,alterop)
1085: local mg2=mg:Filter(Auxiliary.XyzLevelFreeFilter,nil,c,f)
1086: Duel.SetSelectedCard(sg)
1087: local b1=mg2:CheckSubGroup(Auxiliary.XyzLevelFreeGoal,minc,maxc,tp,c,gf)
1088: local b2=(not min or min<=1) and #altg>0
1089: local g=nil
1090: local cancel=Duel.IsSummonCancelable()
1091: if b2 and (not b1 or Duel.SelectYesNo(tp,alterdesc)) then
1092: e:SetLabel(1)
1093: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
1094: local tc=altg:SelectUnselect(nil,tp,false,cancel,1,1)
1095: if tc then
1096: g=Group.FromCards(tc)
1097: if alterop then alterop(e,tp,1,tc) end
1098: end
1099: else
1100: e:SetLabel(0)
1101: Duel.SetSelectedCard(sg)
1102: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_XMATERIAL)
1103: Auxiliary.GCheckAdditional=Auxiliary.TuneMagicianCheckAdditionalX(EFFECT_TUNE_MAGICIAN_X)
1104: g=mg2:SelectSubGroup(tp,Auxiliary.XyzLevelFreeGoal,cancel,minc,maxc,tp,c,gf)
1105: Auxiliary.GCheckAdditional=nil
1106: end
1107: if g and g:GetCount()>0 then
1108: g:KeepAlive()
1109: e:SetLabelObject(g)
1110: return true
1111: else return false end
1112: end
1113: end
1114: function Auxiliary.XyzLevelFreeOperationAlter(f,gf,minct,maxct,alterf,alterdesc,alterop)
1115: return function(e,tp,eg,ep,ev,re,r,rp,c,og,min,max)
1116: if og and not min then
1117: local sg=Group.CreateGroup()
1118: local tc=og:GetFirst()
1119: while tc do
1120: local sg1=tc:GetOverlayGroup()
1121: sg:Merge(sg1)
1122: tc=og:GetNext()
1123: end
1124: Duel.SendtoGrave(sg,REASON_RULE)
1125: c:SetMaterial(og)
1126: Duel.Overlay(c,og)
1127: else
1128: local mg=e:GetLabelObject()
1129: if e:GetLabel()==1 then
1130: local mg2=mg:GetFirst():GetOverlayGroup()
1131: if mg2:GetCount()~=0 then
1132: Duel.Overlay(c,mg2)
1133: end
1134: else
1135: local sg=Group.CreateGroup()
1136: local tc=mg:GetFirst()
1137: while tc do
1138: local sg1=tc:GetOverlayGroup()
1139: sg:Merge(sg1)
1140: tc=mg:GetNext()
1141: end
1142: Duel.SendtoGrave(sg,REASON_RULE)
1143: end
1144: c:SetMaterial(mg)
1145: Duel.Overlay(c,mg)
1146: mg:DeleteGroup()
1147: end
1148: end
1149: end
1150: --material: names in material list
1151: --Fusion monster, mixed materials
1152: function Auxiliary.AddFusionProcMix(c,sub,insf,...)
1153: if c:IsStatus(STATUS_COPYING_EFFECT) then return end
1154: local val={...}
1155: local fun={}
1156: local mat={}
1157: for i=1,#val do
1158: if type(val[i])=='function' then
1159: fun[i]=function(c,fc,sub,mg,sg) return val[i](c,fc,sub,mg,sg) and not c:IsHasEffect(6205579) end
1160: elseif type(val[i])=='table' then
1161: fun[i]=function(c,fc,sub,mg,sg)
1162: for _,fcode in ipairs(val[i]) do
1163: if type(fcode)=='function' then
1164: if fcode(c,fc,sub,mg,sg) and not c:IsHasEffect(6205579) then return true end
1165: else
1166: if c:IsFusionCode(fcode) or (sub and c:CheckFusionSubstitute(fc)) then return true end
1167: end
1168: end
1169: return false
1170: end
1171: for _,fcode in ipairs(val[i]) do
1172: if type(fcode)~='function' then mat[fcode]=true end
1173: end
1174: else
1175: fun[i]=function(c,fc,sub) return c:IsFusionCode(val[i]) or (sub and c:CheckFusionSubstitute(fc)) end
1176: mat[val[i]]=true
1177: end
1178: end
1179: local mt=getmetatable(c)
1180: if mt.material==nil then
1181: mt.material=mat
1182: end
1183: if mt.material_count==nil then
1184: mt.material_count={#fun,#fun}
1185: end
1186: for index,_ in pairs(mat) do
1187: Auxiliary.AddCodeList(c,index)
1188: end
1189: local e1=Effect.CreateEffect(c)
1190: e1:SetType(EFFECT_TYPE_SINGLE)
1191: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
1192: e1:SetCode(EFFECT_FUSION_MATERIAL)
1193: e1:SetCondition(Auxiliary.FConditionMix(insf,sub,table.unpack(fun)))
1194: e1:SetOperation(Auxiliary.FOperationMix(insf,sub,table.unpack(fun)))
1195: c:RegisterEffect(e1)
1196: end
1197: function Auxiliary.FConditionMix(insf,sub,...)
1198: --g:Material group(nil for Instant Fusion)
1199: --gc:Material already used
1200: --chkf: check field, default:PLAYER_NONE
1201: --chkf&0x100: Not fusion summon
1202: --chkf&0x200: Concat fusion
1203: local funs={...}
1204: return function(e,g,gc,chkfnf)
1205: if g==nil then return insf and Auxiliary.MustMaterialCheck(nil,e:GetHandlerPlayer(),EFFECT_MUST_BE_FMATERIAL) end
1206: local c=e:GetHandler()
1207: local tp=c:GetControler()
1208: local notfusion=chkfnf&0x100>0
1209: local concat_fusion=chkfnf&0x200>0
1210: local sub=(sub or notfusion) and not concat_fusion
1211: local mg=g:Filter(Auxiliary.FConditionFilterMix,c,c,sub,concat_fusion,table.unpack(funs))
1212: if gc then
1213: if not mg:IsContains(gc) then return false end
1214: Duel.SetSelectedCard(Group.FromCards(gc))
1215: end
1216: return mg:CheckSubGroup(Auxiliary.FCheckMixGoal,#funs,#funs,tp,c,sub,chkfnf,table.unpack(funs))
1217: end
1218: end
1219: function Auxiliary.FOperationMix(insf,sub,...)
1220: local funs={...}
1221: return function(e,tp,eg,ep,ev,re,r,rp,gc,chkfnf)
1222: local c=e:GetHandler()
1223: local tp=c:GetControler()
1224: local notfusion=chkfnf&0x100>0
1225: local concat_fusion=chkfnf&0x200>0
1226: local sub=(sub or notfusion) and not concat_fusion
1227: local mg=eg:Filter(Auxiliary.FConditionFilterMix,c,c,sub,concat_fusion,table.unpack(funs))
1228: if gc then Duel.SetSelectedCard(Group.FromCards(gc)) end
1229: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
1230: local sg=mg:SelectSubGroup(tp,Auxiliary.FCheckMixGoal,false,#funs,#funs,tp,c,sub,chkfnf,table.unpack(funs))
1231: Duel.SetFusionMaterial(sg)
1232: end
1233: end
1234: function Auxiliary.FConditionFilterMix(c,fc,sub,concat_fusion,...)
1235: local fusion_type=concat_fusion and SUMMON_TYPE_SPECIAL or SUMMON_TYPE_FUSION
1236: if not c:IsCanBeFusionMaterial(fc,fusion_type) then return false end
1237: for i,f in ipairs({...}) do
1238: if f(c,fc,sub) then return true end
1239: end
1240: return false
1241: end
1242: function Auxiliary.FCheckMix(c,mg,sg,fc,sub,fun1,fun2,...)
1243: if fun2 then
1244: sg:AddCard(c)
1245: local res=false
1246: if fun1(c,fc,false,mg,sg) then
1247: res=mg:IsExists(Auxiliary.FCheckMix,1,sg,mg,sg,fc,sub,fun2,...)
1248: elseif sub and fun1(c,fc,true,mg,sg) then
1249: res=mg:IsExists(Auxiliary.FCheckMix,1,sg,mg,sg,fc,false,fun2,...)
1250: end
1251: sg:RemoveCard(c)
1252: return res
1253: else
1254: return fun1(c,fc,sub,mg,sg)
1255: end
1256: end
1257: --if sg1 is subset of sg2 then not Auxiliary.FCheckAdditional(tp,sg1,fc) -> not Auxiliary.FCheckAdditional(tp,sg2,fc)
1258: Auxiliary.FCheckAdditional=nil
1259: Auxiliary.FGoalCheckAdditional=nil
1260: function Auxiliary.FCheckMixGoal(sg,tp,fc,sub,chkfnf,...)
1261: local chkf=chkfnf&0xff
1262: local concat_fusion=chkfnf&0x200>0
1263: if not concat_fusion and sg:IsExists(Auxiliary.TuneMagicianCheckX,1,nil,sg,EFFECT_TUNE_MAGICIAN_F) then return false end
1264: if not Auxiliary.MustMaterialCheck(sg,tp,EFFECT_MUST_BE_FMATERIAL) then return false end
1265: local g=Group.CreateGroup()
1266: return sg:IsExists(Auxiliary.FCheckMix,1,nil,sg,g,fc,sub,...) and (chkf==PLAYER_NONE or Duel.GetLocationCountFromEx(tp,tp,sg,fc)>0)
1267: and (not Auxiliary.FCheckAdditional or Auxiliary.FCheckAdditional(tp,sg,fc))
1268: and (not Auxiliary.FGoalCheckAdditional or Auxiliary.FGoalCheckAdditional(tp,sg,fc))
1269: end
1270: --Fusion monster, mixed material * minc to maxc + material + ...
1271: function Auxiliary.AddFusionProcMixRep(c,sub,insf,fun1,minc,maxc,...)
1272: if c:IsStatus(STATUS_COPYING_EFFECT) then return end
1273: local val={fun1,...}
1274: local fun={}
1275: local mat={}
1276: for i=1,#val do
1277: if type(val[i])=='function' then
1278: fun[i]=function(c,fc,sub,mg,sg) return val[i](c,fc,sub,mg,sg) and not c:IsHasEffect(6205579) end
1279: elseif type(val[i])=='table' then
1280: fun[i]=function(c,fc,sub,mg,sg)
1281: for _,fcode in ipairs(val[i]) do
1282: if type(fcode)=='function' then
1283: if fcode(c,fc,sub,mg,sg) and not c:IsHasEffect(6205579) then return true end
1284: else
1285: if c:IsFusionCode(fcode) or (sub and c:CheckFusionSubstitute(fc)) then return true end
1286: end
1287: end
1288: return false
1289: end
1290: for _,fcode in ipairs(val[i]) do
1291: if type(fcode)~='function' then mat[fcode]=true end
1292: end
1293: else
1294: fun[i]=function(c,fc,sub) return c:IsFusionCode(val[i]) or (sub and c:CheckFusionSubstitute(fc)) end
1295: mat[val[i]]=true
1296: end
1297: end
1298: local mt=getmetatable(c)
1299: if mt.material==nil then
1300: mt.material=mat
1301: end
1302: if mt.material_count==nil then
1303: mt.material_count={#fun+minc-1,#fun+maxc-1}
1304: end
1305: for index,_ in pairs(mat) do
1306: Auxiliary.AddCodeList(c,index)
1307: end
1308: local e1=Effect.CreateEffect(c)
1309: e1:SetType(EFFECT_TYPE_SINGLE)
1310: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
1311: e1:SetCode(EFFECT_FUSION_MATERIAL)
1312: e1:SetCondition(Auxiliary.FConditionMixRep(insf,sub,fun[1],minc,maxc,table.unpack(fun,2)))
1313: e1:SetOperation(Auxiliary.FOperationMixRep(insf,sub,fun[1],minc,maxc,table.unpack(fun,2)))
1314: c:RegisterEffect(e1)
1315: end
1316: function Auxiliary.FConditionMixRep(insf,sub,fun1,minc,maxc,...)
1317: local funs={...}
1318: return function(e,g,gc,chkfnf)
1319: if g==nil then return insf and Auxiliary.MustMaterialCheck(nil,e:GetHandlerPlayer(),EFFECT_MUST_BE_FMATERIAL) end
1320: local c=e:GetHandler()
1321: local tp=c:GetControler()
1322: local notfusion=chkfnf&0x100>0
1323: local concat_fusion=chkfnf&0x200>0
1324: local sub=(sub or notfusion) and not concat_fusion
1325: local mg=g:Filter(Auxiliary.FConditionFilterMix,c,c,sub,concat_fusion,fun1,table.unpack(funs))
1326: if gc then
1327: if not mg:IsContains(gc) then return false end
1328: local sg=Group.CreateGroup()
1329: return Auxiliary.FSelectMixRep(gc,tp,mg,sg,c,sub,chkfnf,fun1,minc,maxc,table.unpack(funs))
1330: end
1331: local sg=Group.CreateGroup()
1332: return mg:IsExists(Auxiliary.FSelectMixRep,1,nil,tp,mg,sg,c,sub,chkfnf,fun1,minc,maxc,table.unpack(funs))
1333: end
1334: end
1335: function Auxiliary.FOperationMixRep(insf,sub,fun1,minc,maxc,...)
1336: local funs={...}
1337: return function(e,tp,eg,ep,ev,re,r,rp,gc,chkfnf)
1338: local c=e:GetHandler()
1339: local tp=c:GetControler()
1340: local notfusion=chkfnf&0x100>0
1341: local concat_fusion=chkfnf&0x200>0
1342: local sub=(sub or notfusion) and not concat_fusion
1343: local mg=eg:Filter(Auxiliary.FConditionFilterMix,c,c,sub,concat_fusion,fun1,table.unpack(funs))
1344: local sg=Group.CreateGroup()
1345: if gc then sg:AddCard(gc) end
1346: while sg:GetCount()<maxc+#funs do
1347: local cg=mg:Filter(Auxiliary.FSelectMixRep,sg,tp,mg,sg,c,sub,chkfnf,fun1,minc,maxc,table.unpack(funs))
1348: if cg:GetCount()==0 then break end
1349: local finish=Auxiliary.FCheckMixRepGoal(tp,sg,c,sub,chkfnf,fun1,minc,maxc,table.unpack(funs))
1350: local cancel_group=sg:Clone()
1351: if gc then cancel_group:RemoveCard(gc) end
1352: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
1353: local tc=cg:SelectUnselect(cancel_group,tp,finish,false,minc+#funs,maxc+#funs)
1354: if not tc then break end
1355: if sg:IsContains(tc) then
1356: sg:RemoveCard(tc)
1357: else
1358: sg:AddCard(tc)
1359: end
1360: end
1361: Duel.SetFusionMaterial(sg)
1362: end
1363: end
1364: function Auxiliary.FCheckMixRep(sg,g,fc,sub,chkf,fun1,minc,maxc,fun2,...)
1365: if fun2 then
1366: return sg:IsExists(Auxiliary.FCheckMixRepFilter,1,g,sg,g,fc,sub,chkf,fun1,minc,maxc,fun2,...)
1367: else
1368: local ct1=sg:FilterCount(fun1,g,fc,sub,mg,sg)
1369: local ct2=sg:FilterCount(fun1,g,fc,false,mg,sg)
1370: return ct1==sg:GetCount()-g:GetCount() and ct1-ct2<=1
1371: end
1372: end
1373: function Auxiliary.FCheckMixRepFilter(c,sg,g,fc,sub,chkf,fun1,minc,maxc,fun2,...)
1374: if fun2(c,fc,sub,mg,sg) then
1375: g:AddCard(c)
1376: local sub=sub and fun2(c,fc,false,mg,sg)
1377: local res=Auxiliary.FCheckMixRep(sg,g,fc,sub,chkf,fun1,minc,maxc,...)
1378: g:RemoveCard(c)
1379: return res
1380: end
1381: return false
1382: end
1383: function Auxiliary.FCheckMixRepGoalCheck(tp,sg,fc,chkfnf)
1384: local concat_fusion=chkfnf&0x200>0
1385: if not concat_fusion and sg:IsExists(Auxiliary.TuneMagicianCheckX,1,nil,sg,EFFECT_TUNE_MAGICIAN_F) then return false end
1386: if not Auxiliary.MustMaterialCheck(sg,tp,EFFECT_MUST_BE_FMATERIAL) then return false end
1387: if Auxiliary.FGoalCheckAdditional and not Auxiliary.FGoalCheckAdditional(tp,sg,fc) then return false end
1388: return true
1389: end
1390: function Auxiliary.FCheckMixRepGoal(tp,sg,fc,sub,chkfnf,fun1,minc,maxc,...)
1391: local chkf=chkfnf&0xff
1392: if sg:GetCount()<minc+#{...} or sg:GetCount()>maxc+#{...} then return false end
1393: if not (chkf==PLAYER_NONE or Duel.GetLocationCountFromEx(tp,tp,sg,fc)>0) then return false end
1394: if Auxiliary.FCheckAdditional and not Auxiliary.FCheckAdditional(tp,sg,fc) then return false end
1395: if not Auxiliary.FCheckMixRepGoalCheck(tp,sg,fc,chkfnf) then return false end
1396: local g=Group.CreateGroup()
1397: return Auxiliary.FCheckMixRep(sg,g,fc,sub,chkf,fun1,minc,maxc,...)
1398: end
1399: function Auxiliary.FCheckMixRepTemplate(c,cond,tp,mg,sg,g,fc,sub,chkfnf,fun1,minc,maxc,...)
1400: for i,f in ipairs({...}) do
1401: if f(c,fc,sub,mg,sg) then
1402: g:AddCard(c)
1403: local sub=sub and f(c,fc,false,mg,sg)
1404: local t={...}
1405: table.remove(t,i)
1406: local res=cond(tp,mg,sg,g,fc,sub,chkfnf,fun1,minc,maxc,table.unpack(t))
1407: g:RemoveCard(c)
1408: if res then return true end
1409: end
1410: end
1411: if maxc>0 then
1412: if fun1(c,fc,sub,mg,sg) then
1413: g:AddCard(c)
1414: local sub=sub and fun1(c,fc,false,mg,sg)
1415: local res=cond(tp,mg,sg,g,fc,sub,chkfnf,fun1,minc-1,maxc-1,...)
1416: g:RemoveCard(c)
1417: if res then return true end
1418: end
1419: end
1420: return false
1421: end
1422: function Auxiliary.FCheckMixRepSelectedCond(tp,mg,sg,g,...)
1423: if g:GetCount()<sg:GetCount() then
1424: return sg:IsExists(Auxiliary.FCheckMixRepSelected,1,g,tp,mg,sg,g,...)
1425: else
1426: return Auxiliary.FCheckSelectMixRep(tp,mg,sg,g,...)
1427: end
1428: end
1429: function Auxiliary.FCheckMixRepSelected(c,...)
1430: return Auxiliary.FCheckMixRepTemplate(c,Auxiliary.FCheckMixRepSelectedCond,...)
1431: end
1432: function Auxiliary.FCheckSelectMixRep(tp,mg,sg,g,fc,sub,chkfnf,fun1,minc,maxc,...)
1433: local chkf=chkfnf&0xff
1434: if Auxiliary.FCheckAdditional and not Auxiliary.FCheckAdditional(tp,g,fc) then return false end
1435: if chkf==PLAYER_NONE or Duel.GetLocationCountFromEx(tp,tp,g,fc)>0 then
1436: if minc<=0 and #{...}==0 and Auxiliary.FCheckMixRepGoalCheck(tp,g,fc,chkfnf) then return true end
1437: return mg:IsExists(Auxiliary.FCheckSelectMixRepAll,1,g,tp,mg,sg,g,fc,sub,chkfnf,fun1,minc,maxc,...)
1438: else
1439: return mg:IsExists(Auxiliary.FCheckSelectMixRepM,1,g,tp,mg,sg,g,fc,sub,chkfnf,fun1,minc,maxc,...)
1440: end
1441: end
1442: function Auxiliary.FCheckSelectMixRepAll(c,tp,mg,sg,g,fc,sub,chkf,fun1,minc,maxc,fun2,...)
1443: if fun2 then
1444: if fun2(c,fc,sub,mg,sg) then
1445: g:AddCard(c)
1446: local sub=sub and fun2(c,fc,false,mg,sg)
1447: local res=Auxiliary.FCheckSelectMixRep(tp,mg,sg,g,fc,sub,chkf,fun1,minc,maxc,...)
1448: g:RemoveCard(c)
1449: return res
1450: end
1451: elseif maxc>0 and fun1(c,fc,sub,mg,sg) then
1452: g:AddCard(c)
1453: local sub=sub and fun1(c,fc,false,mg,sg)
1454: local res=Auxiliary.FCheckSelectMixRep(tp,mg,sg,g,fc,sub,chkf,fun1,minc-1,maxc-1)
1455: g:RemoveCard(c)
1456: return res
1457: end
1458: return false
1459: end
1460: function Auxiliary.FCheckSelectMixRepM(c,tp,...)
1461: return c:IsControler(tp) and c:IsLocation(LOCATION_MZONE)
1462: and Auxiliary.FCheckMixRepTemplate(c,Auxiliary.FCheckSelectMixRep,tp,...)
1463: end
1464: function Auxiliary.FSelectMixRep(c,tp,mg,sg,fc,sub,chkfnf,...)
1465: sg:AddCard(c)
1466: local res=false
1467: if Auxiliary.FCheckAdditional and not Auxiliary.FCheckAdditional(tp,sg,fc) then
1468: res=false
1469: elseif Auxiliary.FCheckMixRepGoal(tp,sg,fc,sub,chkfnf,...) then
1470: res=true
1471: else
1472: local g=Group.CreateGroup()
1473: res=sg:IsExists(Auxiliary.FCheckMixRepSelected,1,nil,tp,mg,sg,g,fc,sub,chkfnf,...)
1474: end
1475: sg:RemoveCard(c)
1476: return res
1477: end
1478: --Fusion monster, name + name
1479: function Auxiliary.AddFusionProcCode2(c,code1,code2,sub,insf)
1480: Auxiliary.AddFusionProcMix(c,sub,insf,code1,code2)
1481: end
1482: --Fusion monster, name + name + name
1483: function Auxiliary.AddFusionProcCode3(c,code1,code2,code3,sub,insf)
1484: Auxiliary.AddFusionProcMix(c,sub,insf,code1,code2,code3)
1485: end
1486: --Fusion monster, name + name + name + name
1487: function Auxiliary.AddFusionProcCode4(c,code1,code2,code3,code4,sub,insf)
1488: Auxiliary.AddFusionProcMix(c,sub,insf,code1,code2,code3,code4)
1489: end
1490: --Fusion monster, name * n
1491: function Auxiliary.AddFusionProcCodeRep(c,code1,cc,sub,insf)
1492: local code={}
1493: for i=1,cc do
1494: code[i]=code1
1495: end
1496: Auxiliary.AddFusionProcMix(c,sub,insf,table.unpack(code))
1497: end
1498: --Fusion monster, name * minc to maxc
1499: function Auxiliary.AddFusionProcCodeRep2(c,code1,minc,maxc,sub,insf)
1500: Auxiliary.AddFusionProcMixRep(c,sub,insf,code1,minc,maxc)
1501: end
1502: --Fusion monster, name + condition * n
1503: function Auxiliary.AddFusionProcCodeFun(c,code1,f,cc,sub,insf)
1504: local fun={}
1505: for i=1,cc do
1506: fun[i]=f
1507: end
1508: Auxiliary.AddFusionProcMix(c,sub,insf,code1,table.unpack(fun))
1509: end
1510: --Fusion monster, condition + condition
1511: function Auxiliary.AddFusionProcFun2(c,f1,f2,insf)
1512: Auxiliary.AddFusionProcMix(c,false,insf,f1,f2)
1513: end
1514: --Fusion monster, condition * n
1515: function Auxiliary.AddFusionProcFunRep(c,f,cc,insf)
1516: local fun={}
1517: for i=1,cc do
1518: fun[i]=f
1519: end
1520: Auxiliary.AddFusionProcMix(c,false,insf,table.unpack(fun))
1521: end
1522: --Fusion monster, condition * minc to maxc
1523: function Auxiliary.AddFusionProcFunRep2(c,f,minc,maxc,insf)
1524: Auxiliary.AddFusionProcMixRep(c,false,insf,f,minc,maxc)
1525: end
1526: --Fusion monster, condition1 + condition2 * n
1527: function Auxiliary.AddFusionProcFunFun(c,f1,f2,cc,insf)
1528: local fun={}
1529: for i=1,cc do
1530: fun[i]=f2
1531: end
1532: Auxiliary.AddFusionProcMix(c,false,insf,f1,table.unpack(fun))
1533: end
1534: --Fusion monster, condition1 + condition2 * minc to maxc
1535: function Auxiliary.AddFusionProcFunFunRep(c,f1,f2,minc,maxc,insf)
1536: Auxiliary.AddFusionProcMixRep(c,false,insf,f2,minc,maxc,f1)
1537: end
1538: --Fusion monster, name + condition * minc to maxc
1539: function Auxiliary.AddFusionProcCodeFunRep(c,code1,f,minc,maxc,sub,insf)
1540: Auxiliary.AddFusionProcMixRep(c,sub,insf,f,minc,maxc,code1)
1541: end
1542: --Fusion monster, name + name + condition * minc to maxc
1543: function Auxiliary.AddFusionProcCode2FunRep(c,code1,code2,f,minc,maxc,sub,insf)
1544: Auxiliary.AddFusionProcMixRep(c,sub,insf,f,minc,maxc,code1,code2)
1545: end
1546: --Fusion monster, Shaddoll materials
1547: function Auxiliary.AddFusionProcShaddoll(c,attr)
1548: local e1=Effect.CreateEffect(c)
1549: e1:SetType(EFFECT_TYPE_SINGLE)
1550: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
1551: e1:SetCode(EFFECT_FUSION_MATERIAL)
1552: e1:SetCondition(Auxiliary.FShaddollCondition(attr))
1553: e1:SetOperation(Auxiliary.FShaddollOperation(attr))
1554: c:RegisterEffect(e1)
1555: end
1556: function Auxiliary.FShaddollFilter(c,fc,attr)
1557: return (Auxiliary.FShaddollFilter1(c) or Auxiliary.FShaddollFilter2(c,attr)) and c:IsCanBeFusionMaterial(fc) and not c:IsHasEffect(6205579)
1558: end
1559: function Auxiliary.FShaddollExFilter(c,fc,attr,fe)
1560: return c:IsFaceup() and not c:IsImmuneToEffect(fe) and Auxiliary.FShaddollFilter(c,fc,attr)
1561: end
1562: function Auxiliary.FShaddollFilter1(c)
1563: return c:IsFusionSetCard(0x9d)
1564: end
1565: function Auxiliary.FShaddollFilter2(c,attr)
1566: return c:IsFusionAttribute(attr) or c:IsHasEffect(4904633)
1567: end
1568: function Auxiliary.FShaddollSpFilter1(c,fc,tp,mg,exg,attr,chkf)
1569: return mg:IsExists(Auxiliary.FShaddollSpFilter2,1,c,fc,tp,c,attr,chkf)
1570: or (exg and exg:IsExists(Auxiliary.FShaddollSpFilter2,1,c,fc,tp,c,attr,chkf))
1571: end
1572: function Auxiliary.FShaddollSpFilter2(c,fc,tp,mc,attr,chkf)
1573: local sg=Group.FromCards(c,mc)
1574: if sg:IsExists(Auxiliary.TuneMagicianCheckX,1,nil,sg,EFFECT_TUNE_MAGICIAN_F) then return false end
1575: if not Auxiliary.MustMaterialCheck(sg,tp,EFFECT_MUST_BE_FMATERIAL) then return false end
1576: if Auxiliary.FCheckAdditional and not Auxiliary.FCheckAdditional(tp,sg,fc)
1577: or Auxiliary.FGoalCheckAdditional and not Auxiliary.FGoalCheckAdditional(tp,sg,fc) then return false end
1578: return ((Auxiliary.FShaddollFilter1(c) and Auxiliary.FShaddollFilter2(mc,attr))
1579: or (Auxiliary.FShaddollFilter2(c,attr) and Auxiliary.FShaddollFilter1(mc)))
1580: and (chkf==PLAYER_NONE or Duel.GetLocationCountFromEx(tp,tp,sg,fc)>0)
1581: end
1582: function Auxiliary.FShaddollCondition(attr)
1583: return function(e,g,gc,chkf)
1584: if g==nil then return Auxiliary.MustMaterialCheck(nil,e:GetHandlerPlayer(),EFFECT_MUST_BE_FMATERIAL) end
1585: local c=e:GetHandler()
1586: local mg=g:Filter(Auxiliary.FShaddollFilter,nil,c,attr)
1587: local tp=e:GetHandlerPlayer()
1588: local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
1589: local exg=nil
1590: if fc and fc:IsHasEffect(81788994) and fc:IsCanRemoveCounter(tp,0x16,3,REASON_EFFECT) then
1591: local fe=fc:IsHasEffect(81788994)
1592: exg=Duel.GetMatchingGroup(Auxiliary.FShaddollExFilter,tp,0,LOCATION_MZONE,mg,c,attr,fe)
1593: end
1594: if gc then
1595: if not mg:IsContains(gc) then return false end
1596: return Auxiliary.FShaddollSpFilter1(gc,c,tp,mg,exg,attr,chkf)
1597: end
1598: return mg:IsExists(Auxiliary.FShaddollSpFilter1,1,nil,c,tp,mg,exg,attr,chkf)
1599: end
1600: end
1601: function Auxiliary.FShaddollOperation(attr)
1602: return function(e,tp,eg,ep,ev,re,r,rp,gc,chkf)
1603: local c=e:GetHandler()
1604: local mg=eg:Filter(Auxiliary.FShaddollFilter,nil,c,attr)
1605: local fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
1606: local exg=nil
1607: if fc and fc:IsHasEffect(81788994) and fc:IsCanRemoveCounter(tp,0x16,3,REASON_EFFECT) then
1608: local fe=fc:IsHasEffect(81788994)
1609: exg=Duel.GetMatchingGroup(Auxiliary.FShaddollExFilter,tp,0,LOCATION_MZONE,mg,c,attr,fe)
1610: end
1611: local g=nil
1612: if gc then
1613: g=Group.FromCards(gc)
1614: mg:RemoveCard(gc)
1615: else
1616: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
1617: g=mg:FilterSelect(tp,Auxiliary.FShaddollSpFilter1,1,1,nil,c,tp,mg,exg,attr,chkf)
1618: mg:Sub(g)
1619: end
1620: if exg and exg:IsExists(Auxiliary.FShaddollSpFilter2,1,nil,c,tp,g:GetFirst(),attr,chkf)
1621: and (mg:GetCount()==0 or (exg:GetCount()>0 and Duel.SelectYesNo(tp,aux.Stringid(81788994,0)))) then
1622: fc:RemoveCounter(tp,0x16,3,REASON_EFFECT)
1623: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
1624: local sg=exg:FilterSelect(tp,Auxiliary.FShaddollSpFilter2,1,1,nil,c,tp,g:GetFirst(),attr,chkf)
1625: g:Merge(sg)
1626: else
1627: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_FMATERIAL)
1628: local sg=mg:FilterSelect(tp,Auxiliary.FShaddollSpFilter2,1,1,nil,c,tp,g:GetFirst(),attr,chkf)
1629: g:Merge(sg)
1630: end
1631: Duel.SetFusionMaterial(g)
1632: end
1633: end
1634: function Auxiliary.AddContactFusionProcedure(c,filter,self_location,opponent_location,mat_operation,...)
1635: local self_location=self_location or 0
1636: local opponent_location=opponent_location or 0
1637: local operation_params={...}
1638: local e2=Effect.CreateEffect(c)
1639: e2:SetType(EFFECT_TYPE_FIELD)
1640: e2:SetCode(EFFECT_SPSUMMON_PROC)
1641: e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
1642: e2:SetRange(LOCATION_EXTRA)
1643: e2:SetCondition(Auxiliary.ContactFusionCondition(filter,self_location,opponent_location))
1644: e2:SetOperation(Auxiliary.ContactFusionOperation(filter,self_location,opponent_location,mat_operation,operation_params))
1645: c:RegisterEffect(e2)
1646: return e2
1647: end
1648: function Auxiliary.ContactFusionMaterialFilter(c,fc,filter)
1649: return c:IsCanBeFusionMaterial(fc,SUMMON_TYPE_SPECIAL) and (not filter or filter(c,fc))
1650: end
1651: function Auxiliary.ContactFusionCondition(filter,self_location,opponent_location)
1652: return function(e,c)
1653: if c==nil then return true end
1654: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
1655: local tp=c:GetControler()
1656: local mg=Duel.GetMatchingGroup(Auxiliary.ContactFusionMaterialFilter,tp,self_location,opponent_location,c,c,filter)
1657: return c:CheckFusionMaterial(mg,nil,tp|0x200)
1658: end
1659: end
1660: function Auxiliary.ContactFusionOperation(filter,self_location,opponent_location,mat_operation,operation_params)
1661: return function(e,tp,eg,ep,ev,re,r,rp,c)
1662: local mg=Duel.GetMatchingGroup(Auxiliary.ContactFusionMaterialFilter,tp,self_location,opponent_location,c,c,filter)
1663: local g=Duel.SelectFusionMaterial(tp,c,mg,nil,tp|0x200)
1664: c:SetMaterial(g)
1665: mat_operation(g,table.unpack(operation_params))
1666: end
1667: end
1668: function Auxiliary.AddRitualProcUltimate(c,filter,level_function,greater_or_equal,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1669: summon_location=summon_location or LOCATION_HAND
1670: local e1=Effect.CreateEffect(c)
1671: e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
1672: e1:SetType(EFFECT_TYPE_ACTIVATE)
1673: e1:SetCode(EVENT_FREE_CHAIN)
1674: e1:SetTarget(Auxiliary.RitualUltimateTarget(filter,level_function,greater_or_equal,summon_location,grave_filter,mat_filter,extra_target))
1675: e1:SetOperation(Auxiliary.RitualUltimateOperation(filter,level_function,greater_or_equal,summon_location,grave_filter,mat_filter,extra_operation))
1676: if not pause then
1677: c:RegisterEffect(e1)
1678: end
1679: return e1
1680: end
1681: function Auxiliary.RitualCheckGreater(g,c,lv)
1682: Duel.SetSelectedCard(g)
1683: return g:CheckWithSumGreater(Card.GetRitualLevel,lv,c)
1684: end
1685: function Auxiliary.RitualCheckEqual(g,c,lv)
1686: return g:CheckWithSumEqual(Card.GetRitualLevel,lv,#g,#g,c)
1687: end
1688: Auxiliary.RCheckAdditional=nil
1689: function Auxiliary.RitualCheck(g,tp,c,lv,greater_or_equal)
1690: return Auxiliary["RitualCheck"..greater_or_equal](g,c,lv) and Duel.GetMZoneCount(tp,g,tp)>0 and (not c.mat_group_check or c.mat_group_check(g,tp))
1691: and (not Auxiliary.RCheckAdditional or Auxiliary.RCheckAdditional(tp,g,c))
1692: end
1693: function Auxiliary.RitualCheckAdditionalLevel(c,rc)
1694: local raw_level=c:GetRitualLevel(rc)
1695: local lv1=raw_level&0xffff
1696: local lv2=raw_level>>16
1697: if lv2>0 then
1698: return math.min(lv1,lv2)
1699: else
1700: return lv1
1701: end
1702: end
1703: Auxiliary.RGCheckAdditional=nil
1704: function Auxiliary.RitualCheckAdditional(c,lv,greater_or_equal)
1705: if greater_or_equal=="Equal" then
1706: return function(g)
1707: return (not Auxiliary.RGCheckAdditional or Auxiliary.RGCheckAdditional(g)) and g:GetSum(Auxiliary.RitualCheckAdditionalLevel,c)<=lv
1708: end
1709: else
1710: return function(g,ec)
1711: if ec then
1712: return (not Auxiliary.RGCheckAdditional or Auxiliary.RGCheckAdditional(g,ec)) and g:GetSum(Auxiliary.RitualCheckAdditionalLevel,c)-Auxiliary.RitualCheckAdditionalLevel(ec,c)<=lv
1713: else
1714: return not Auxiliary.RGCheckAdditional or Auxiliary.RGCheckAdditional(g)
1715: end
1716: end
1717: end
1718: end
1719: function Auxiliary.RitualUltimateFilter(c,filter,e,tp,m1,m2,level_function,greater_or_equal,chk)
1720: if bit.band(c:GetType(),0x81)~=0x81 or (filter and not filter(c,e,tp,chk)) or not c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_RITUAL,tp,false,true) then return false end
1721: local mg=m1:Filter(Card.IsCanBeRitualMaterial,c,c)
1722: if m2 then
1723: mg:Merge(m2)
1724: end
1725: if c.mat_filter then
1726: mg=mg:Filter(c.mat_filter,c,tp)
1727: else
1728: mg:RemoveCard(c)
1729: end
1730: local lv=level_function(c)
1731: Auxiliary.GCheckAdditional=Auxiliary.RitualCheckAdditional(c,lv,greater_or_equal)
1732: local res=mg:CheckSubGroup(Auxiliary.RitualCheck,1,lv,tp,c,lv,greater_or_equal)
1733: Auxiliary.GCheckAdditional=nil
1734: return res
1735: end
1736: function Auxiliary.RitualExtraFilter(c,f)
1737: return c:GetLevel()>0 and f(c) and c:IsType(TYPE_MONSTER) and c:IsAbleToRemove()
1738: end
1739: function Auxiliary.RitualUltimateTarget(filter,level_function,greater_or_equal,summon_location,grave_filter,mat_filter,extra_target)
1740: return function(e,tp,eg,ep,ev,re,r,rp,chk)
1741: if chk==0 then
1742: local mg=Duel.GetRitualMaterial(tp)
1743: if mat_filter then mg=mg:Filter(mat_filter,nil,e,tp,true) end
1744: local exg=nil
1745: if grave_filter then
1746: exg=Duel.GetMatchingGroup(Auxiliary.RitualExtraFilter,tp,LOCATION_GRAVE,0,nil,grave_filter)
1747: end
1748: return Duel.IsExistingMatchingCard(Auxiliary.RitualUltimateFilter,tp,summon_location,0,1,nil,filter,e,tp,mg,exg,level_function,greater_or_equal,true)
1749: end
1750: Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,summon_location)
1751: if grave_filter then
1752: Duel.SetOperationInfo(0,CATEGORY_REMOVE,nil,0,tp,LOCATION_GRAVE)
1753: end
1754: if extra_target then
1755: extra_target(e,tp,eg,ep,ev,re,r,rp)
1756: end
1757: end
1758: end
1759: function Auxiliary.RitualUltimateOperation(filter,level_function,greater_or_equal,summon_location,grave_filter,mat_filter,extra_operation)
1760: return function(e,tp,eg,ep,ev,re,r,rp)
1761: ::RitualUltimateSelectStart::
1762: local mg=Duel.GetRitualMaterial(tp)
1763: if mat_filter then mg=mg:Filter(mat_filter,nil,e,tp) end
1764: local exg=nil
1765: if grave_filter then
1766: exg=Duel.GetMatchingGroup(Auxiliary.RitualExtraFilter,tp,LOCATION_GRAVE,0,nil,grave_filter)
1767: end
1768: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
1769: local tg=Duel.SelectMatchingCard(tp,Auxiliary.NecroValleyFilter(Auxiliary.RitualUltimateFilter),tp,summon_location,0,1,1,nil,filter,e,tp,mg,exg,level_function,greater_or_equal)
1770: local tc=tg:GetFirst()
1771: local mat
1772: if tc then
1773: mg=mg:Filter(Card.IsCanBeRitualMaterial,tc,tc)
1774: if exg then
1775: mg:Merge(exg)
1776: end
1777: if tc.mat_filter then
1778: mg=mg:Filter(tc.mat_filter,tc,tp)
1779: else
1780: mg:RemoveCard(tc)
1781: end
1782: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
1783: local lv=level_function(tc)
1784: Auxiliary.GCheckAdditional=Auxiliary.RitualCheckAdditional(tc,lv,greater_or_equal)
1785: mat=mg:SelectSubGroup(tp,Auxiliary.RitualCheck,true,1,lv,tp,tc,lv,greater_or_equal)
1786: Auxiliary.GCheckAdditional=nil
1787: if not mat then goto RitualUltimateSelectStart end
1788: tc:SetMaterial(mat)
1789: Duel.ReleaseRitualMaterial(mat)
1790: Duel.BreakEffect()
1791: Duel.SpecialSummon(tc,SUMMON_TYPE_RITUAL,tp,tp,false,true,POS_FACEUP)
1792: tc:CompleteProcedure()
1793: end
1794: if extra_operation then
1795: extra_operation(e,tp,eg,ep,ev,re,r,rp,tc,mat)
1796: end
1797: end
1798: end
1799: --Ritual Summon, geq fixed lv
1800: function Auxiliary.AddRitualProcGreater(c,filter,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1801: return Auxiliary.AddRitualProcUltimate(c,filter,Card.GetOriginalLevel,"Greater",summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1802: end
1803: function Auxiliary.AddRitualProcGreaterCode(c,code1,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1804: Auxiliary.AddCodeList(c,code1)
1805: return Auxiliary.AddRitualProcGreater(c,Auxiliary.FilterBoolFunction(Card.IsCode,code1),summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1806: end
1807: --Ritual Summon, equal to fixed lv
1808: function Auxiliary.AddRitualProcEqual(c,filter,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1809: return Auxiliary.AddRitualProcUltimate(c,filter,Card.GetOriginalLevel,"Equal",summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1810: end
1811: function Auxiliary.AddRitualProcEqualCode(c,code1,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1812: Auxiliary.AddCodeList(c,code1)
1813: return Auxiliary.AddRitualProcEqual(c,Auxiliary.FilterBoolFunction(Card.IsCode,code1),summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1814: end
1815: --Ritual Summon, equal to monster lv
1816: function Auxiliary.AddRitualProcEqual2(c,filter,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1817: return Auxiliary.AddRitualProcUltimate(c,filter,Card.GetLevel,"Equal",summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1818: end
1819: function Auxiliary.AddRitualProcEqual2Code(c,code1,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1820: Auxiliary.AddCodeList(c,code1)
1821: return Auxiliary.AddRitualProcEqual2(c,Auxiliary.FilterBoolFunction(Card.IsCode,code1),summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1822: end
1823: function Auxiliary.AddRitualProcEqual2Code2(c,code1,code2,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1824: Auxiliary.AddCodeList(c,code1,code2)
1825: return Auxiliary.AddRitualProcEqual2(c,Auxiliary.FilterBoolFunction(Card.IsCode,code1,code2),summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1826: end
1827: --Ritual Summon, geq monster lv
1828: function Auxiliary.AddRitualProcGreater2(c,filter,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1829: return Auxiliary.AddRitualProcUltimate(c,filter,Card.GetLevel,"Greater",summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1830: end
1831: function Auxiliary.AddRitualProcGreater2Code(c,code1,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1832: Auxiliary.AddCodeList(c,code1)
1833: return Auxiliary.AddRitualProcGreater2(c,Auxiliary.FilterBoolFunction(Card.IsCode,code1),summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1834: end
1835: function Auxiliary.AddRitualProcGreater2Code2(c,code1,code2,summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1836: Auxiliary.AddCodeList(c,code1,code2)
1837: return Auxiliary.AddRitualProcGreater2(c,Auxiliary.FilterBoolFunction(Card.IsCode,code1,code2),summon_location,grave_filter,mat_filter,pause,extra_operation,extra_target)
1838: end
1839: --add procedure to Pendulum monster, also allows registeration of activation effect
1840: function Auxiliary.EnablePendulumAttribute(c,reg)
1841: if not Auxiliary.PendulumChecklist then
1842: Auxiliary.PendulumChecklist=0
1843: local ge1=Effect.GlobalEffect()
1844: ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
1845: ge1:SetCode(EVENT_PHASE_START+PHASE_DRAW)
1846: ge1:SetOperation(Auxiliary.PendulumReset)
1847: Duel.RegisterEffect(ge1,0)
1848: end
1849: local e1=Effect.CreateEffect(c)
1850: e1:SetDescription(1163)
1851: e1:SetType(EFFECT_TYPE_FIELD)
1852: e1:SetCode(EFFECT_SPSUMMON_PROC_G)
1853: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
1854: e1:SetRange(LOCATION_PZONE)
1855: e1:SetCondition(Auxiliary.PendCondition())
1856: e1:SetOperation(Auxiliary.PendOperation())
1857: e1:SetValue(SUMMON_TYPE_PENDULUM)
1858: c:RegisterEffect(e1)
1859: --register by default
1860: if reg==nil or reg then
1861: local e2=Effect.CreateEffect(c)
1862: e2:SetDescription(1160)
1863: e2:SetType(EFFECT_TYPE_ACTIVATE)
1864: e2:SetCode(EVENT_FREE_CHAIN)
1865: e2:SetRange(LOCATION_HAND)
1866: c:RegisterEffect(e2)
1867: end
1868: end
1869: function Auxiliary.PendulumReset(e,tp,eg,ep,ev,re,r,rp)
1870: Auxiliary.PendulumChecklist=0
1871: end
1872: function Auxiliary.PConditionExtraFilterSpecific(c,e,tp,lscale,rscale,te)
1873: if not te then return true end
1874: local f=te:GetValue()
1875: return not f or f(te,c,e,tp,lscale,rscale)
1876: end
1877: function Auxiliary.PConditionExtraFilter(c,e,tp,lscale,rscale,eset)
1878: for _,te in ipairs(eset) do
1879: if Auxiliary.PConditionExtraFilterSpecific(c,e,tp,lscale,rscale,te) then return true end
1880: end
1881: return false
1882: end
1883: function Auxiliary.PConditionFilter(c,e,tp,lscale,rscale,eset)
1884: local lv=0
1885: if c.pendulum_level then
1886: lv=c.pendulum_level
1887: else
1888: lv=c:GetLevel()
1889: end
1890: local bool=Auxiliary.PendulumSummonableBool(c)
1891: return (c:IsLocation(LOCATION_HAND) or (c:IsFaceup() and c:IsType(TYPE_PENDULUM)))
1892: and lv>lscale and lv<rscale and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_PENDULUM,tp,bool,bool)
1893: and not c:IsForbidden()
1894: and (Auxiliary.PendulumChecklist&(0x1<<tp)==0 or Auxiliary.PConditionExtraFilter(c,e,tp,lscale,rscale,eset))
1895: end
1896: function Auxiliary.PendCondition()
1897: return function(e,c,og)
1898: if c==nil then return true end
1899: local tp=c:GetControler()
1900: local eset={Duel.IsPlayerAffectedByEffect(tp,EFFECT_EXTRA_PENDULUM_SUMMON)}
1901: if Auxiliary.PendulumChecklist&(0x1<<tp)~=0 and #eset==0 then return false end
1902: local rpz=Duel.GetFieldCard(tp,LOCATION_PZONE,1)
1903: if rpz==nil or c==rpz then return false end
1904: local lscale=c:GetLeftScale()
1905: local rscale=rpz:GetRightScale()
1906: if lscale>rscale then lscale,rscale=rscale,lscale end
1907: local loc=0
1908: if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then loc=loc+LOCATION_HAND end
1909: if Duel.GetLocationCountFromEx(tp,tp,nil,TYPE_PENDULUM)>0 then loc=loc+LOCATION_EXTRA end
1910: if loc==0 then return false end
1911: local g=nil
1912: if og then
1913: g=og:Filter(Card.IsLocation,nil,loc)
1914: else
1915: g=Duel.GetFieldGroup(tp,loc,0)
1916: end
1917: return g:IsExists(Auxiliary.PConditionFilter,1,nil,e,tp,lscale,rscale,eset)
1918: end
1919: end
1920: function Auxiliary.PendOperationCheck(ft1,ft2,ft)
1921: return function(g)
1922: local exg=g:Filter(Card.IsLocation,nil,LOCATION_EXTRA)
1923: local mg=g-exg
1924: return #g<=ft and #exg<=ft2 and #mg<=ft1
1925: end
1926: end
1927: function Auxiliary.PendOperation()
1928: return function(e,tp,eg,ep,ev,re,r,rp,c,sg,og)
1929: local rpz=Duel.GetFieldCard(tp,LOCATION_PZONE,1)
1930: local lscale=c:GetLeftScale()
1931: local rscale=rpz:GetRightScale()
1932: if lscale>rscale then lscale,rscale=rscale,lscale end
1933: local eset={Duel.IsPlayerAffectedByEffect(tp,EFFECT_EXTRA_PENDULUM_SUMMON)}
1934: local tg=nil
1935: local loc=0
1936: local ft1=Duel.GetLocationCount(tp,LOCATION_MZONE)
1937: local ft2=Duel.GetLocationCountFromEx(tp,tp,nil,TYPE_PENDULUM)
1938: local ft=Duel.GetUsableMZoneCount(tp)
1939: local ect=c29724053 and Duel.IsPlayerAffectedByEffect(tp,29724053) and c29724053[tp]
1940: if ect and ect<ft2 then ft2=ect end
1941: if Duel.IsPlayerAffectedByEffect(tp,59822133) then
1942: if ft1>0 then ft1=1 end
1943: if ft2>0 then ft2=1 end
1944: ft=1
1945: end
1946: if ft1>0 then loc=loc|LOCATION_HAND end
1947: if ft2>0 then loc=loc|LOCATION_EXTRA end
1948: if og then
1949: tg=og:Filter(Card.IsLocation,nil,loc):Filter(Auxiliary.PConditionFilter,nil,e,tp,lscale,rscale,eset)
1950: else
1951: tg=Duel.GetMatchingGroup(Auxiliary.PConditionFilter,tp,loc,0,nil,e,tp,lscale,rscale,eset)
1952: end
1953: local ce=nil
1954: local b1=Auxiliary.PendulumChecklist&(0x1<<tp)==0
1955: local b2=#eset>0
1956: if b1 and b2 then
1957: local options={1163}
1958: for _,te in ipairs(eset) do
1959: table.insert(options,te:GetDescription())
1960: end
1961: local op=Duel.SelectOption(tp,table.unpack(options))
1962: if op>0 then
1963: ce=eset[op]
1964: end
1965: elseif b2 and not b1 then
1966: local options={}
1967: for _,te in ipairs(eset) do
1968: table.insert(options,te:GetDescription())
1969: end
1970: local op=Duel.SelectOption(tp,table.unpack(options))
1971: ce=eset[op+1]
1972: end
1973: if ce then
1974: tg=tg:Filter(Auxiliary.PConditionExtraFilterSpecific,nil,e,tp,lscale,rscale,ce)
1975: end
1976: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
1977: Auxiliary.GCheckAdditional=Auxiliary.PendOperationCheck(ft1,ft2,ft)
1978: local g=tg:SelectSubGroup(tp,aux.TRUE,true,1,math.min(#tg,ft))
1979: Auxiliary.GCheckAdditional=nil
1980: if not g then return end
1981: if ce then
1982: Duel.Hint(HINT_CARD,0,ce:GetOwner():GetOriginalCode())
1983: ce:UseCountLimit(tp)
1984: else
1985: Auxiliary.PendulumChecklist=Auxiliary.PendulumChecklist|(0x1<<tp)
1986: end
1987: sg:Merge(g)
1988: Duel.HintSelection(Group.FromCards(c))
1989: Duel.HintSelection(Group.FromCards(rpz))
1990: end
1991: end
1992: --enable revive limit for monsters that are also pendulum sumonable from certain locations (Odd-Eyes Revolution Dragon)
1993: function Auxiliary.EnableReviveLimitPendulumSummonable(c, loc)
1994: if c:IsStatus(STATUS_COPYING_EFFECT) then return end
1995: c:EnableReviveLimit()
1996: local mt=getmetatable(c)
1997: if loc==nil then loc=0xff end
1998: mt.psummonable_location=loc
1999: --complete procedure on pendulum summon success
2000: local e1=Effect.CreateEffect(c)
2001: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
2002: e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
2003: e1:SetCode(EVENT_SPSUMMON_SUCCESS)
2004: e1:SetOperation(Auxiliary.PSSCompleteProcedure)
2005: c:RegisterEffect(e1)
2006: end
2007: function Auxiliary.PendulumSummonableBool(c)
2008: return c.psummonable_location~=nil and c:GetLocation()&c.psummonable_location>0
2009: end
2010: function Auxiliary.PSSCompleteProcedure(e,tp,eg,ep,ev,re,r,rp)
2011: local c=e:GetHandler()
2012: if c:IsSummonType(SUMMON_TYPE_PENDULUM) then
2013: c:CompleteProcedure()
2014: end
2015: end
2016: --Link Summon
2017: function Auxiliary.AddLinkProcedure(c,f,min,max,gf)
2018: local e1=Effect.CreateEffect(c)
2019: e1:SetDescription(1166)
2020: e1:SetType(EFFECT_TYPE_FIELD)
2021: e1:SetCode(EFFECT_SPSUMMON_PROC)
2022: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)
2023: e1:SetRange(LOCATION_EXTRA)
2024: if max==nil then max=c:GetLink() end
2025: e1:SetCondition(Auxiliary.LinkCondition(f,min,max,gf))
2026: e1:SetTarget(Auxiliary.LinkTarget(f,min,max,gf))
2027: e1:SetOperation(Auxiliary.LinkOperation(f,min,max,gf))
2028: e1:SetValue(SUMMON_TYPE_LINK)
2029: c:RegisterEffect(e1)
2030: return e1
2031: end
2032: function Auxiliary.LConditionFilter(c,f,lc,e)
2033: return (c:IsFaceup() or not c:IsOnField() or e:IsHasProperty(EFFECT_FLAG_SET_AVAILABLE))
2034: and c:IsCanBeLinkMaterial(lc) and (not f or f(c))
2035: end
2036: function Auxiliary.LExtraFilter(c,f,lc,tp)
2037: if c:IsOnField() and c:IsFacedown() then return false end
2038: if not c:IsCanBeLinkMaterial(lc) or f and not f(c) then return false end
2039: local le={c:IsHasEffect(EFFECT_EXTRA_LINK_MATERIAL,tp)}
2040: for _,te in pairs(le) do
2041: local tf=te:GetValue()
2042: local related,valid=tf(te,lc,nil,c,tp)
2043: if related then return true end
2044: end
2045: return false
2046: end
2047: function Auxiliary.GetLinkCount(c)
2048: if c:IsType(TYPE_LINK) and c:GetLink()>1 then
2049: return 1+0x10000*c:GetLink()
2050: else return 1 end
2051: end
2052: function Auxiliary.GetLinkMaterials(tp,f,lc,e)
2053: local mg=Duel.GetMatchingGroup(Auxiliary.LConditionFilter,tp,LOCATION_MZONE,0,nil,f,lc,e)
2054: local mg2=Duel.GetMatchingGroup(Auxiliary.LExtraFilter,tp,LOCATION_HAND+LOCATION_SZONE,LOCATION_ONFIELD,nil,f,lc,tp)
2055: if mg2:GetCount()>0 then mg:Merge(mg2) end
2056: return mg
2057: end
2058: function Auxiliary.LCheckOtherMaterial(c,mg,lc,tp)
2059: local le={c:IsHasEffect(EFFECT_EXTRA_LINK_MATERIAL,tp)}
2060: local res1=false
2061: local res2=true
2062: for _,te in pairs(le) do
2063: local f=te:GetValue()
2064: local related,valid=f(te,lc,mg,c,tp)
2065: if related then res2=false end
2066: if related and valid then res1=true end
2067: end
2068: return res1 or res2
2069: end
2070: function Auxiliary.LUncompatibilityFilter(c,sg,lc,tp)
2071: local mg=sg:Filter(aux.TRUE,c)
2072: return not Auxiliary.LCheckOtherMaterial(c,mg,lc,tp)
2073: end
2074: function Auxiliary.LCheckGoal(sg,tp,lc,gf,lmat)
2075: return sg:CheckWithSumEqual(Auxiliary.GetLinkCount,lc:GetLink(),#sg,#sg)
2076: and Duel.GetLocationCountFromEx(tp,tp,sg,lc)>0 and (not gf or gf(sg,lc,tp))
2077: and not sg:IsExists(Auxiliary.LUncompatibilityFilter,1,nil,sg,lc,tp)
2078: and (not lmat or sg:IsContains(lmat))
2079: end
2080: function Auxiliary.LExtraMaterialCount(mg,lc,tp)
2081: for tc in aux.Next(mg) do
2082: local le={tc:IsHasEffect(EFFECT_EXTRA_LINK_MATERIAL,tp)}
2083: for _,te in pairs(le) do
2084: local sg=mg:Filter(aux.TRUE,tc)
2085: local f=te:GetValue()
2086: local related,valid=f(te,lc,sg,tc,tp)
2087: if related and valid then
2088: te:UseCountLimit(tp)
2089: end
2090: end
2091: end
2092: end
2093: function Auxiliary.LinkCondition(f,minc,maxc,gf)
2094: return function(e,c,og,lmat,min,max)
2095: if c==nil then return true end
2096: if c:IsType(TYPE_PENDULUM) and c:IsFaceup() then return false end
2097: local minc=minc
2098: local maxc=maxc
2099: if min then
2100: if min>minc then minc=min end
2101: if max<maxc then maxc=max end
2102: if minc>maxc then return false end
2103: end
2104: local tp=c:GetControler()
2105: local mg=nil
2106: if og then
2107: mg=og:Filter(Auxiliary.LConditionFilter,nil,f,c,e)
2108: else
2109: mg=Auxiliary.GetLinkMaterials(tp,f,c,e)
2110: end
2111: if lmat~=nil then
2112: if not Auxiliary.LConditionFilter(lmat,f,c,e) then return false end
2113: mg:AddCard(lmat)
2114: end
2115: local fg=Duel.GetMustMaterial(tp,EFFECT_MUST_BE_LMATERIAL)
2116: if fg:IsExists(Auxiliary.MustMaterialCounterFilter,1,nil,mg) then return false end
2117: Duel.SetSelectedCard(fg)
2118: return mg:CheckSubGroup(Auxiliary.LCheckGoal,minc,maxc,tp,c,gf,lmat)
2119: end
2120: end
2121: function Auxiliary.LinkTarget(f,minc,maxc,gf)
2122: return function(e,tp,eg,ep,ev,re,r,rp,chk,c,og,lmat,min,max)
2123: local minc=minc
2124: local maxc=maxc
2125: if min then
2126: if min>minc then minc=min end
2127: if max<maxc then maxc=max end
2128: if minc>maxc then return false end
2129: end
2130: local mg=nil
2131: if og then
2132: mg=og:Filter(Auxiliary.LConditionFilter,nil,f,c,e)
2133: else
2134: mg=Auxiliary.GetLinkMaterials(tp,f,c,e)
2135: end
2136: if lmat~=nil then
2137: if not Auxiliary.LConditionFilter(lmat,f,c,e) then return false end
2138: mg:AddCard(lmat)
2139: end
2140: local fg=Duel.GetMustMaterial(tp,EFFECT_MUST_BE_LMATERIAL)
2141: Duel.SetSelectedCard(fg)
2142: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_LMATERIAL)
2143: local cancel=Duel.IsSummonCancelable()
2144: local sg=mg:SelectSubGroup(tp,Auxiliary.LCheckGoal,cancel,minc,maxc,tp,c,gf,lmat)
2145: if sg then
2146: sg:KeepAlive()
2147: e:SetLabelObject(sg)
2148: return true
2149: else return false end
2150: end
2151: end
2152: function Auxiliary.LinkOperation(f,minc,maxc,gf)
2153: return function(e,tp,eg,ep,ev,re,r,rp,c,og,lmat,min,max)
2154: local g=e:GetLabelObject()
2155: c:SetMaterial(g)
2156: Auxiliary.LExtraMaterialCount(g,c,tp)
2157: Duel.SendtoGrave(g,REASON_MATERIAL+REASON_LINK)
2158: g:DeleteGroup()
2159: end
2160: end
2161: function Auxiliary.EnableExtraDeckSummonCountLimit()
2162: if Auxiliary.ExtraDeckSummonCountLimit~=nil then return end
2163: Auxiliary.ExtraDeckSummonCountLimit={}
2164: Auxiliary.ExtraDeckSummonCountLimit[0]=1
2165: Auxiliary.ExtraDeckSummonCountLimit[1]=1
2166: local ge1=Effect.GlobalEffect()
2167: ge1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
2168: ge1:SetCode(EVENT_PHASE_START+PHASE_DRAW)
2169: ge1:SetOperation(Auxiliary.ExtraDeckSummonCountLimitReset)
2170: Duel.RegisterEffect(ge1,0)
2171: end
2172: function Auxiliary.ExtraDeckSummonCountLimitReset()
2173: Auxiliary.ExtraDeckSummonCountLimit[0]=1
2174: Auxiliary.ExtraDeckSummonCountLimit[1]=1
2175: end
2176: --Fusion Monster is unnecessary to use this
2177: function Auxiliary.AddMaterialCodeList(c,...)
2178: if c:IsStatus(STATUS_COPYING_EFFECT) then return end
2179: local mat={}
2180: for _,code in ipairs{...} do
2181: mat[code]=true
2182: end
2183: if c.material==nil then
2184: local mt=getmetatable(c)
2185: mt.material=mat
2186: end
2187: for index,_ in pairs(mat) do
2188: Auxiliary.AddCodeList(c,index)
2189: end
2190: end
2191: function Auxiliary.IsMaterialListCode(c,code)
2192: return c.material and c.material[code]
2193: end
2194: function Auxiliary.IsMaterialListSetCard(c,setcode)
2195: if not c.material_setcode then return false end
2196: if type(c.material_setcode)=='table' then
2197: for i,scode in ipairs(c.material_setcode) do
2198: if setcode&0xfff==scode&0xfff and setcode&scode==setcode then return true end
2199: end
2200: else
2201: return setcode&0xfff==c.material_setcode&0xfff and setcode&c.material_setcode==setcode
2202: end
2203: return false
2204: end
2205: function Auxiliary.IsMaterialListType(c,type)
2206: return c.material_type and type&c.material_type==type
2207: end
2208: function Auxiliary.GetMaterialListCount(c)
2209: if not c.material_count then return 0,0 end
2210: return c.material_count[1],c.material_count[2]
2211: end
2212: function Auxiliary.AddCodeList(c,...)
2213: if c:IsStatus(STATUS_COPYING_EFFECT) then return end
2214: if c.card_code_list==nil then
2215: local mt=getmetatable(c)
2216: mt.card_code_list={}
2217: for _,code in ipairs{...} do
2218: mt.card_code_list[code]=true
2219: end
2220: else
2221: for _,code in ipairs{...} do
2222: c.card_code_list[code]=true
2223: end
2224: end
2225: end
2226: function Auxiliary.IsCodeListed(c,code)
2227: return c.card_code_list and c.card_code_list[code]
2228: end
2229: function Auxiliary.AddSetNameMonsterList(c,...)
2230: if c:IsStatus(STATUS_COPYING_EFFECT) then return end
2231: if c.setcode_monster_list==nil then
2232: local mt=getmetatable(c)
2233: mt.setcode_monster_list={}
2234: for i,scode in ipairs{...} do
2235: mt.setcode_monster_list[i]=scode
2236: end
2237: else
2238: for i,scode in ipairs{...} do
2239: c.setcode_monster_list[i]=scode
2240: end
2241: end
2242: end
2243: function Auxiliary.IsSetNameMonsterListed(c,setcode)
2244: if not c.setcode_monster_list then return false end
2245: for i,scode in ipairs(c.setcode_monster_list) do
2246: if setcode&0xfff==scode&0xfff and setcode&scode==setcode then return true end
2247: end
2248: return false
2249: end
2250: function Auxiliary.IsCounterAdded(c,counter)
2251: if not c.counter_add_list then return false end
2252: for i,ccounter in ipairs(c.counter_add_list) do
2253: if counter==ccounter then return true end
2254: end
2255: return false
2256: end
2257: function Auxiliary.IsTypeInText(c,type)
2258: return c.has_text_type and type&c.has_text_type==type
2259: end
2260: function Auxiliary.GetAttributeCount(g)
2261: if #g==0 then return 0 end
2262: local att=0
2263: for tc in Auxiliary.Next(g) do
2264: att=att|tc:GetAttribute()
2265: end
2266: local ct=0
2267: while att~=0 do
2268: if att&0x1~=0 then ct=ct+1 end
2269: att=att>>1
2270: end
2271: return ct
2272: end
2273: function Auxiliary.IsInGroup(c,g)
2274: return g:IsContains(c)
2275: end
2276: --return the column of card c (from the viewpoint of p)
2277: function Auxiliary.GetColumn(c,p)
2278: local seq=c:GetSequence()
2279: if c:IsLocation(LOCATION_MZONE) then
2280: if seq==5 then
2281: seq=1
2282: elseif seq==6 then
2283: seq=3
2284: end
2285: elseif c:IsLocation(LOCATION_SZONE) then
2286: if seq>4 then
2287: return nil
2288: end
2289: else
2290: return nil
2291: end
2292: if c:IsControler(p or 0) then
2293: return seq
2294: else
2295: return 4-seq
2296: end
2297: end
2298: --return the column of monster zone seq (from the viewpoint of controller)
2299: function Auxiliary.MZoneSequence(seq)
2300: if seq==5 then return 1 end
2301: if seq==6 then return 3 end
2302: return seq
2303: end
2304: --return the column of spell/trap zone seq (from the viewpoint of controller)
2305: function Auxiliary.SZoneSequence(seq)
2306: if seq>4 then return nil end
2307: return seq
2308: end
2309: --generate the value function of EFFECT_CHANGE_BATTLE_DAMAGE on monsters
2310: function Auxiliary.ChangeBattleDamage(player,value)
2311: return function(e,damp)
2312: if player==0 then
2313: if e:GetOwnerPlayer()==damp then
2314: return value
2315: else
2316: return -1
2317: end
2318: elseif player==1 then
2319: if e:GetOwnerPlayer()==1-damp then
2320: return value
2321: else
2322: return -1
2323: end
2324: end
2325: end
2326: end
2327: --filter for "negate the effects of a face-up monster" (無限泡影/Infinite Impermanence)
2328: function Auxiliary.NegateMonsterFilter(c)
2329: return c:IsFaceup() and not c:IsDisabled() and (c:IsType(TYPE_EFFECT) or c:GetOriginalType()&TYPE_EFFECT~=0)
2330: end
2331: --filter for "negate the effects of an Effect Monster" (エフェクト・ヴェーラー/Effect Veiler)
2332: function Auxiliary.NegateEffectMonsterFilter(c)
2333: return c:IsFaceup() and not c:IsDisabled() and c:IsType(TYPE_EFFECT)
2334: end
2335: --filter for "negate the effects of a face-up card"
2336: function Auxiliary.NegateAnyFilter(c)
2337: if c:IsType(TYPE_TRAPMONSTER) then
2338: return c:IsFaceup()
2339: elseif c:IsType(TYPE_SPELL+TYPE_TRAP) then
2340: return c:IsFaceup() and not c:IsDisabled()
2341: else
2342: return aux.NegateMonsterFilter(c)
2343: end
2344: end
2345: --alias for compatibility
2346: Auxiliary.disfilter1=Auxiliary.NegateAnyFilter
2347: --condition of EVENT_BATTLE_DESTROYING
2348: function Auxiliary.bdcon(e,tp,eg,ep,ev,re,r,rp)
2349: local c=e:GetHandler()
2350: return c:IsRelateToBattle()
2351: end
2352: --condition of EVENT_BATTLE_DESTROYING + opponent monster
2353: function Auxiliary.bdocon(e,tp,eg,ep,ev,re,r,rp)
2354: local c=e:GetHandler()
2355: return c:IsRelateToBattle() and c:IsStatus(STATUS_OPPO_BATTLE)
2356: end
2357: --condition of EVENT_BATTLE_DESTROYING + to_grave
2358: function Auxiliary.bdgcon(e,tp,eg,ep,ev,re,r,rp)
2359: local c=e:GetHandler()
2360: local bc=c:GetBattleTarget()
2361: return c:IsRelateToBattle() and bc:IsLocation(LOCATION_GRAVE) and bc:IsType(TYPE_MONSTER)
2362: end
2363: --condition of EVENT_BATTLE_DESTROYING + opponent monster + to_grave
2364: function Auxiliary.bdogcon(e,tp,eg,ep,ev,re,r,rp)
2365: local c=e:GetHandler()
2366: local bc=c:GetBattleTarget()
2367: return c:IsRelateToBattle() and c:IsStatus(STATUS_OPPO_BATTLE) and bc:IsLocation(LOCATION_GRAVE) and bc:IsType(TYPE_MONSTER)
2368: end
2369: --condition of EVENT_DAMAGE_STEP_END + this monster is releate to battle
2370: function Auxiliary.dsercon(e,tp,eg,ep,ev,re,r,rp)
2371: local c=e:GetHandler()
2372: return c:IsRelateToBattle() or c:IsStatus(STATUS_BATTLE_DESTROYED)
2373: end
2374: --condition of EVENT_TO_GRAVE + destroyed by opponent
2375: function Auxiliary.dogcon(e,tp,eg,ep,ev,re,r,rp)
2376: local c=e:GetHandler()
2377: return c:IsPreviousControler(tp) and c:IsReason(REASON_DESTROY) and rp==1-tp
2378: end
2379: --condition of EVENT_TO_GRAVE + destroyed by opponent + from field
2380: function Auxiliary.dogfcon(e,tp,eg,ep,ev,re,r,rp)
2381: local c=e:GetHandler()
2382: return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousControler(tp)
2383: and c:IsReason(REASON_DESTROY) and rp==1-tp
2384: end
2385: --condition of "except the turn this card was sent to the Graveyard"
2386: function Auxiliary.exccon(e)
2387: return Duel.GetTurnCount()~=e:GetHandler():GetTurnID() or e:GetHandler():IsReason(REASON_RETURN)
2388: end
2389: --condition of checking battle phase availability
2390: function Auxiliary.bpcon(e,tp,eg,ep,ev,re,r,rp)
2391: return Duel.IsAbleToEnterBP() or (Duel.GetCurrentPhase()>=PHASE_BATTLE_START and Duel.GetCurrentPhase()<=PHASE_BATTLE)
2392: end
2393: --condition of free chain effects changing ATK/DEF
2394: function Auxiliary.dscon(e,tp,eg,ep,ev,re,r,rp)
2395: return Duel.GetCurrentPhase()~=PHASE_DAMAGE or not Duel.IsDamageCalculated()
2396: end
2397: --flag effect for spell counter
2398: function Auxiliary.chainreg(e,tp,eg,ep,ev,re,r,rp)
2399: if e:GetHandler():GetFlagEffect(1)==0 then
2400: e:GetHandler():RegisterFlagEffect(1,RESET_EVENT+RESETS_STANDARD-RESET_TURN_SET+RESET_CHAIN,0,1)
2401: end
2402: end
2403: --default filter for EFFECT_CANNOT_BE_BATTLE_TARGET
2404: function Auxiliary.imval1(e,c)
2405: return not c:IsImmuneToEffect(e)
2406: end
2407: --filter for EFFECT_INDESTRUCTABLE_EFFECT + self
2408: function Auxiliary.indsval(e,re,rp)
2409: return rp==e:GetHandlerPlayer()
2410: end
2411: --filter for EFFECT_INDESTRUCTABLE_EFFECT + opponent
2412: function Auxiliary.indoval(e,re,rp)
2413: return rp==1-e:GetHandlerPlayer()
2414: end
2415: --filter for EFFECT_CANNOT_BE_EFFECT_TARGET + self
2416: function Auxiliary.tgsval(e,re,rp)
2417: return rp==e:GetHandlerPlayer()
2418: end
2419: --filter for EFFECT_CANNOT_BE_EFFECT_TARGET + opponent
2420: function Auxiliary.tgoval(e,re,rp)
2421: return rp==1-e:GetHandlerPlayer()
2422: end
2423: --filter for non-zero ATK
2424: function Auxiliary.nzatk(c)
2425: return c:IsFaceup() and c:GetAttack()>0
2426: end
2427: --filter for non-zero DEF
2428: function Auxiliary.nzdef(c)
2429: return c:IsFaceup() and c:GetDefense()>0
2430: end
2431: --flag effect for summon/sp_summon turn
2432: function Auxiliary.sumreg(e,tp,eg,ep,ev,re,r,rp)
2433: local tc=eg:GetFirst()
2434: local code=e:GetLabel()
2435: while tc do
2436: if tc:GetOriginalCode()==code then
2437: tc:RegisterFlagEffect(code,RESET_EVENT+0x1ec0000+RESET_PHASE+PHASE_END,0,1)
2438: end
2439: tc=eg:GetNext()
2440: end
2441: end
2442: --for EVENT_BE_MATERIAL effect releated to the summoned monster
2443: function Auxiliary.CreateMaterialReasonCardRelation(c,te)
2444: local e1=Effect.CreateEffect(c)
2445: e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
2446: e1:SetCode(EVENT_BE_MATERIAL)
2447: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
2448: e1:SetOperation(Auxiliary.MaterialReasonCardReg)
2449: e1:SetLabelObject(te)
2450: c:RegisterEffect(e1)
2451: end
2452: function Auxiliary.MaterialReasonCardReg(e,tp,eg,ep,ev,re,r,rp)
2453: local c=e:GetHandler()
2454: local te=e:GetLabelObject()
2455: c:GetReasonCard():CreateEffectRelation(te)
2456: end
2457: --sp_summon condition for fusion monster
2458: function Auxiliary.fuslimit(e,se,sp,st)
2459: return st&SUMMON_TYPE_FUSION==SUMMON_TYPE_FUSION
2460: end
2461: --sp_summon condition for ritual monster
2462: function Auxiliary.ritlimit(e,se,sp,st)
2463: return st&SUMMON_TYPE_RITUAL==SUMMON_TYPE_RITUAL
2464: end
2465: --sp_summon condition for synchro monster
2466: function Auxiliary.synlimit(e,se,sp,st)
2467: return st&SUMMON_TYPE_SYNCHRO==SUMMON_TYPE_SYNCHRO
2468: end
2469: --sp_summon condition for xyz monster
2470: function Auxiliary.xyzlimit(e,se,sp,st)
2471: return st&SUMMON_TYPE_XYZ==SUMMON_TYPE_XYZ
2472: end
2473: --sp_summon condition for pendulum monster
2474: function Auxiliary.penlimit(e,se,sp,st)
2475: return st&SUMMON_TYPE_PENDULUM==SUMMON_TYPE_PENDULUM
2476: end
2477: --sp_summon condition for link monster
2478: function Auxiliary.linklimit(e,se,sp,st)
2479: return st&SUMMON_TYPE_LINK==SUMMON_TYPE_LINK
2480: end
2481: --effects inflicting damage to tp
2482: function Auxiliary.damcon1(e,tp,eg,ep,ev,re,r,rp)
2483: local e1=Duel.IsPlayerAffectedByEffect(tp,EFFECT_REVERSE_DAMAGE)
2484: local e2=Duel.IsPlayerAffectedByEffect(tp,EFFECT_REVERSE_RECOVER)
2485: local rd=e1 and not e2
2486: local rr=not e1 and e2
2487: local ex,cg,ct,cp,cv=Duel.GetOperationInfo(ev,CATEGORY_DAMAGE)
2488: if ex and (cp==tp or cp==PLAYER_ALL) and not rd and not Duel.IsPlayerAffectedByEffect(tp,EFFECT_NO_EFFECT_DAMAGE) then
2489: return true
2490: end
2491: ex,cg,ct,cp,cv=Duel.GetOperationInfo(ev,CATEGORY_RECOVER)
2492: return ex and (cp==tp or cp==PLAYER_ALL) and rr and not Duel.IsPlayerAffectedByEffect(tp,EFFECT_NO_EFFECT_DAMAGE)
2493: end
2494: --filter for the immune effect of qli monsters
2495: function Auxiliary.qlifilter(e,te)
2496: if te:IsActiveType(TYPE_MONSTER) and te:IsActivated() then
2497: local lv=e:GetHandler():GetLevel()
2498: local ec=te:GetOwner()
2499: if ec:IsType(TYPE_LINK) then
2500: return false
2501: elseif ec:IsType(TYPE_XYZ) then
2502: return ec:GetOriginalRank()<lv
2503: else
2504: return ec:GetOriginalLevel()<lv
2505: end
2506: else
2507: return false
2508: end
2509: end
2510: --sp_summon condition for gladiator beast monsters
2511: function Auxiliary.gbspcon(e,tp,eg,ep,ev,re,r,rp)
2512: local st=e:GetHandler():GetSummonType()
2513: return st&SUMMON_VALUE_GLADIATOR>0
2514: end
2515: --sp_summon condition for evolsaur monsters
2516: function Auxiliary.evospcon(e,tp,eg,ep,ev,re,r,rp)
2517: local st=e:GetHandler():GetSummonType()
2518: return st&SUMMON_VALUE_EVOLTILE>0
2519: end
2520: --filter for necro_valley test
2521: function Auxiliary.NecroValleyFilter(f)
2522: return function(target,...)
2523: return (not f or f(target,...)) and not target:IsHasEffect(EFFECT_NECRO_VALLEY)
2524: end
2525: end
2526: --Necrovalley test for effect with not certain target or not certain action
2527: function Auxiliary.NecroValleyNegateCheck(v)
2528: if not Duel.IsChainDisablable(0) then return false end
2529: local g=Group.CreateGroup()
2530: if Auxiliary.GetValueType(v)=="Card" then g:AddCard(v) end
2531: if Auxiliary.GetValueType(v)=="Group" then g:Merge(v) end
2532: if g:IsExists(Card.IsHasEffect,1,nil,EFFECT_NECRO_VALLEY) then
2533: Duel.NegateEffect(0)
2534: return true
2535: end
2536: return false
2537: end
2538: --Ursarctic common summon from hand effect
2539: function Auxiliary.AddUrsarcticSpSummonEffect(c)
2540: local e1=Effect.CreateEffect(c)
2541: e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
2542: e1:SetType(EFFECT_TYPE_QUICK_O)
2543: e1:SetCode(EVENT_FREE_CHAIN)
2544: e1:SetRange(LOCATION_HAND)
2545: e1:SetHintTiming(0,TIMINGS_CHECK_MONSTER+TIMING_MAIN_END)
2546: e1:SetCondition(Auxiliary.UrsarcticSpSummonCondition)
2547: e1:SetCost(Auxiliary.UrsarcticSpSummonCost)
2548: e1:SetTarget(Auxiliary.UrsarcticSpSummonTarget)
2549: e1:SetOperation(Auxiliary.UrsarcticSpSummonOperation)
2550: c:RegisterEffect(e1)
2551: return e1
2552: end
2553: function Auxiliary.UrsarcticSpSummonCondition(e,tp,eg,ep,ev,re,r,rp)
2554: return Duel.GetCurrentPhase()==PHASE_MAIN1 or Duel.GetCurrentPhase()==PHASE_MAIN2
2555: end
2556: function Auxiliary.UrsarcticReleaseFilter(c)
2557: return c:IsLevelAbove(7) and c:IsLocation(LOCATION_HAND)
2558: end
2559: function Auxiliary.UrsarcticExCostFilter(c,tp)
2560: return c:IsAbleToRemoveAsCost() and (c:IsHasEffect(16471775,tp) or c:IsHasEffect(89264428,tp))
2561: end
2562: function Auxiliary.UrsarcticSpSummonCost(e,tp,eg,ep,ev,re,r,rp,chk)
2563: local g1=Duel.GetReleaseGroup(tp,true):Filter(Auxiliary.UrsarcticReleaseFilter,e:GetHandler())
2564: local g2=Duel.GetMatchingGroup(Auxiliary.UrsarcticExCostFilter,tp,LOCATION_GRAVE,0,nil,tp)
2565: g1:Merge(g2)
2566: if chk==0 then return g1:GetCount()>0 end
2567: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
2568: local tc=g1:Select(tp,1,1,nil):GetFirst()
2569: local te=tc:IsHasEffect(16471775,tp) or tc:IsHasEffect(89264428,tp)
2570: if te then
2571: te:UseCountLimit(tp)
2572: Duel.Remove(tc,POS_FACEUP,REASON_EFFECT+REASON_REPLACE)
2573: else
2574: Duel.Release(tc,REASON_COST)
2575: end
2576: end
2577: function Auxiliary.UrsarcticSpSummonTarget(e,tp,eg,ep,ev,re,r,rp,chk)
2578: if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)>0
2579: and e:GetHandler():IsCanBeSpecialSummoned(e,0,tp,false,false) end
2580: Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,e:GetHandler(),1,0,0)
2581: end
2582: function Auxiliary.UrsarcticSpSummonOperation(e,tp,eg,ep,ev,re,r,rp)
2583: local c=e:GetHandler()
2584: if c:IsRelateToEffect(e) then
2585: Duel.SpecialSummon(c,0,tp,tp,false,false,POS_FACEUP)
2586: end
2587: local e1=Effect.CreateEffect(c)
2588: e1:SetType(EFFECT_TYPE_FIELD)
2589: e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
2590: e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
2591: e1:SetTargetRange(1,0)
2592: e1:SetTarget(Auxiliary.UrsarcticSpSummonLimit)
2593: e1:SetReset(RESET_PHASE+PHASE_END)
2594: Duel.RegisterEffect(e1,tp)
2595: end
2596: function Auxiliary.UrsarcticSpSummonLimit(e,c)
2597: return c:IsLevel(0)
2598: end
2599: --Drytron common summon effect
2600: function Auxiliary.AddDrytronSpSummonEffect(c,func)
2601: local e1=Effect.CreateEffect(c)
2602: e1:SetType(EFFECT_TYPE_IGNITION)
2603: e1:SetRange(LOCATION_HAND+LOCATION_GRAVE)
2604: e1:SetCost(Auxiliary.DrytronSpSummonCost)
2605: e1:SetTarget(Auxiliary.DrytronSpSummonTarget)
2606: e1:SetOperation(Auxiliary.DrytronSpSummonOperation(func))
2607: c:RegisterEffect(e1)
2608: Duel.AddCustomActivityCounter(97148796,ACTIVITY_SPSUMMON,Auxiliary.DrytronCounterFilter)
2609: return e1
2610: end
2611: function Auxiliary.DrytronCounterFilter(c)
2612: return not c:IsSummonableCard()
2613: end
2614: function Auxiliary.DrytronCostFilter(c,tp)
2615: return (c:IsSetCard(0x154) or c:IsType(TYPE_RITUAL)) and c:IsType(TYPE_MONSTER) and Duel.GetMZoneCount(tp,c)>0
2616: and (c:IsControler(tp) or c:IsFaceup())
2617: end
2618: function Auxiliary.DrytronExtraCostFilter(c,tp)
2619: return c:IsAbleToRemove() and c:IsHasEffect(89771220,tp)
2620: end
2621: function Auxiliary.DrytronSpSummonCost(e,tp,eg,ep,ev,re,r,rp,chk)
2622: e:SetLabel(100)
2623: local g1=Duel.GetReleaseGroup(tp,true):Filter(Auxiliary.DrytronCostFilter,e:GetHandler(),tp)
2624: local g2=Duel.GetMatchingGroup(Auxiliary.DrytronExtraCostFilter,tp,LOCATION_GRAVE,0,nil,tp)
2625: g1:Merge(g2)
2626: if chk==0 then return #g1>0 and Duel.GetCustomActivityCount(97148796,tp,ACTIVITY_SPSUMMON)==0 end
2627: local e1=Effect.CreateEffect(e:GetHandler())
2628: e1:SetType(EFFECT_TYPE_FIELD)
2629: e1:SetCode(EFFECT_CANNOT_SPECIAL_SUMMON)
2630: e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
2631: e1:SetTargetRange(1,0)
2632: e1:SetTarget(Auxiliary.DrytronSpSummonLimit)
2633: e1:SetReset(RESET_PHASE+PHASE_END)
2634: Duel.RegisterEffect(e1,tp)
2635: --cant special summon summonable card check
2636: local e2=Effect.CreateEffect(e:GetHandler())
2637: e2:SetType(EFFECT_TYPE_FIELD)
2638: e2:SetCode(97148796)
2639: e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_OATH)
2640: e2:SetTargetRange(1,0)
2641: e2:SetReset(RESET_PHASE+PHASE_END)
2642: Duel.RegisterEffect(e2,tp)
2643: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RELEASE)
2644: local rg=g1:Select(tp,1,1,nil)
2645: local tc=rg:GetFirst()
2646: local te=tc:IsHasEffect(89771220,tp)
2647: if te then
2648: te:UseCountLimit(tp)
2649: Duel.Remove(tc,POS_FACEUP,REASON_EFFECT+REASON_REPLACE)
2650: else
2651: Auxiliary.UseExtraReleaseCount(rg,tp)
2652: Duel.Release(tc,REASON_COST)
2653: end
2654: end
2655: function Auxiliary.DrytronSpSummonLimit(e,c,sump,sumtype,sumpos,targetp,se)
2656: return c:IsSummonableCard()
2657: end
2658: function Auxiliary.DrytronSpSummonTarget(e,tp,eg,ep,ev,re,r,rp,chk)
2659: local res=e:GetLabel()==100 or Duel.GetLocationCount(tp,LOCATION_MZONE)>0
2660: if chk==0 then
2661: e:SetLabel(0)
2662: return res and e:GetHandler():IsCanBeSpecialSummoned(e,0,tp,false,false,POS_FACEUP_DEFENSE)
2663: end
2664: e:SetLabel(0)
2665: Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,e:GetHandler(),1,0,0)
2666: end
2667: function Auxiliary.DrytronSpSummonOperation(func)
2668: return function(e,tp,eg,ep,ev,re,r,rp)
2669: local c=e:GetHandler()
2670: if not c:IsRelateToEffect(e) then return end
2671: if Duel.SpecialSummon(c,0,tp,tp,false,false,POS_FACEUP_DEFENSE)~=0 then func(e,tp) end
2672: end
2673: end
2674: --additional destroy effect for the Labrynth field
2675: function Auxiliary.LabrynthDestroyOp(e,tp,res)
2676: local c=e:GetHandler()
2677: local chk=not c:IsStatus(STATUS_ACT_FROM_HAND) and c:IsSetCard(0x117e) and c:GetType()==TYPE_TRAP and e:IsHasType(EFFECT_TYPE_ACTIVATE)
2678: local exc=nil
2679: if c:IsStatus(STATUS_LEAVE_CONFIRMED) then exc=c end
2680: local te=Duel.IsPlayerAffectedByEffect(tp,33407125)
2681: if chk and te
2682: and Duel.IsExistingMatchingCard(nil,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,exc)
2683: and Duel.SelectYesNo(tp,aux.Stringid(33407125,0)) then
2684: if res>0 then Duel.BreakEffect() end
2685: Duel.Hint(HINT_CARD,0,33407125)
2686: Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_DESTROY)
2687: local dg=Duel.SelectMatchingCard(tp,nil,tp,LOCATION_ONFIELD,LOCATION_ONFIELD,1,1,exc)
2688: Duel.HintSelection(dg)
2689: Duel.Destroy(dg,REASON_EFFECT)
2690: te:UseCountLimit(tp)
2691: end
2692: end
2693: --shortcut for Gizmek cards
2694: function Auxiliary.AtkEqualsDef(c)
2695: if not c:IsType(TYPE_MONSTER) or c:IsType(TYPE_LINK) then return false end
2696: if c:GetAttack()~=c:GetDefense() then return false end
2697: return c:IsLocation(LOCATION_MZONE) or c:GetTextAttack()>=0 and c:GetTextDefense()>=0
2698: end
2699: --shortcut for self-banish costs
2700: function Auxiliary.bfgcost(e,tp,eg,ep,ev,re,r,rp,chk)
2701: if chk==0 then return e:GetHandler():IsAbleToRemoveAsCost() end
2702: Duel.Remove(e:GetHandler(),POS_FACEUP,REASON_COST)
2703: end
2704: --check for cards with different names
2705: function Auxiliary.dncheck(g)
2706: return g:GetClassCount(Card.GetCode)==#g
2707: end
2708: --check for cards with different levels
2709: function Auxiliary.dlvcheck(g)
2710: return g:GetClassCount(Card.GetLevel)==#g
2711: end
2712: --check for cards with different ranks
2713: function Auxiliary.drkcheck(g)
2714: return g:GetClassCount(Card.GetRank)==#g
2715: end
2716: --check for cards with different links
2717: function Auxiliary.dlkcheck(g)
2718: return g:GetClassCount(Card.GetLink)==#g
2719: end
2720: --check for cards with different attributes
2721: function Auxiliary.dabcheck(g)
2722: return g:GetClassCount(Card.GetAttribute)==#g
2723: end
2724: --check for cards with different races
2725: function Auxiliary.drccheck(g)
2726: return g:GetClassCount(Card.GetRace)==#g
2727: end
2728: --check for group with 2 cards, each card match f with a1/a2 as argument
2729: function Auxiliary.gfcheck(g,f,a1,a2)
2730: if #g~=2 then return false end
2731: local c1=g:GetFirst()
2732: local c2=g:GetNext()
2733: return f(c1,a1) and f(c2,a2) or f(c2,a1) and f(c1,a2)
2734: end
2735: --check for group with 2 cards, each card match f1 with a1, f2 with a2 as argument
2736: function Auxiliary.gffcheck(g,f1,a1,f2,a2)
2737: if #g~=2 then return false end
2738: local c1=g:GetFirst()
2739: local c2=g:GetNext()
2740: return f1(c1,a1) and f2(c2,a2) or f1(c2,a1) and f2(c1,a2)
2741: end
2742: function Auxiliary.mzctcheck(g,tp)
2743: return Duel.GetMZoneCount(tp,g)>0
2744: end
2745: function Auxiliary.mzctcheckrel(g,tp)
2746: return Duel.GetMZoneCount(tp,g)>0 and Duel.CheckReleaseGroup(tp,Auxiliary.IsInGroup,#g,nil,g)
2747: end
2748: --used for "except this card"
2749: function Auxiliary.ExceptThisCard(e)
2750: local c=e:GetHandler()
2751: if c:IsRelateToChain() then return c else return nil end
2752: end
2753: --used for multi-linked zone(zone linked by two or more link monsters)
2754: function Auxiliary.GetMultiLinkedZone(tp)
2755: local f=function(c)
2756: return c:IsFaceup() and c:IsType(TYPE_LINK)
2757: end
2758: local lg=Duel.GetMatchingGroup(f,tp,LOCATION_MZONE,LOCATION_MZONE,nil)
2759: local multi_linked_zone=0
2760: local single_linked_zone=0
2761: for tc in aux.Next(lg) do
2762: local zone=tc:GetLinkedZone(tp)&0x7f
2763: multi_linked_zone=single_linked_zone&zone|multi_linked_zone
2764: single_linked_zone=single_linked_zone~zone
2765: end
2766: return multi_linked_zone
2767: end
2768: Auxiliary.SubGroupCaptured=nil
2769: Auxiliary.GCheckAdditional=nil
2770: function Auxiliary.CheckGroupRecursive(c,sg,g,f,min,max,ext_params)
2771: sg:AddCard(c)
2772: if Auxiliary.GCheckAdditional and not Auxiliary.GCheckAdditional(sg,c,g,f,min,max,ext_params) then
2773: sg:RemoveCard(c)
2774: return false
2775: end
2776: local res=(#sg>=min and #sg<=max and f(sg,table.unpack(ext_params)))
2777: or (#sg<max and g:IsExists(Auxiliary.CheckGroupRecursive,1,sg,sg,g,f,min,max,ext_params))
2778: sg:RemoveCard(c)
2779: return res
2780: end
2781: function Auxiliary.CheckGroupRecursiveCapture(c,sg,g,f,min,max,ext_params)
2782: sg:AddCard(c)
2783: if Auxiliary.GCheckAdditional and not Auxiliary.GCheckAdditional(sg,c,g,f,min,max,ext_params) then
2784: sg:RemoveCard(c)
2785: return false
2786: end
2787: local res=#sg>=min and #sg<=max and f(sg,table.unpack(ext_params))
2788: if res then
2789: Auxiliary.SubGroupCaptured:Clear()
2790: Auxiliary.SubGroupCaptured:Merge(sg)
2791: else
2792: res=#sg<max and g:IsExists(Auxiliary.CheckGroupRecursiveCapture,1,sg,sg,g,f,min,max,ext_params)
2793: end
2794: sg:RemoveCard(c)
2795: return res
2796: end
2797: function Group.CheckSubGroup(g,f,min,max,...)
2798: local min=min or 1
2799: local max=max or #g
2800: if min>max then return false end
2801: local ext_params={...}
2802: local sg=Duel.GrabSelectedCard()
2803: if #sg>max or #(g+sg)<min then return false end
2804: if #sg==max and (not f(sg,...) or Auxiliary.GCheckAdditional and not Auxiliary.GCheckAdditional(sg,nil,g,f,min,max,ext_params)) then return false end
2805: if #sg>=min and #sg<=max and f(sg,...) and (not Auxiliary.GCheckAdditional or Auxiliary.GCheckAdditional(sg,nil,g,f,min,max,ext_params)) then return true end
2806: local eg=g:Clone()
2807: for c in aux.Next(g-sg) do
2808: if Auxiliary.CheckGroupRecursive(c,sg,eg,f,min,max,ext_params) then return true end
2809: eg:RemoveCard(c)
2810: end
2811: return false
2812: end
2813: function Group.SelectSubGroup(g,tp,f,cancelable,min,max,...)
2814: Auxiliary.SubGroupCaptured=Group.CreateGroup()
2815: local min=min or 1
2816: local max=max or #g
2817: local ext_params={...}
2818: local sg=Group.CreateGroup()
2819: local fg=Duel.GrabSelectedCard()
2820: if #fg>max or min>max or #(g+fg)<min then return nil end
2821: for tc in aux.Next(fg) do
2822: fg:SelectUnselect(sg,tp,false,false,min,max)
2823: end
2824: sg:Merge(fg)
2825: local finish=(#sg>=min and #sg<=max and f(sg,...))
2826: while #sg<max do
2827: local cg=Group.CreateGroup()
2828: local eg=g:Clone()
2829: for c in aux.Next(g-sg) do
2830: if not cg:IsContains(c) then
2831: if Auxiliary.CheckGroupRecursiveCapture(c,sg,eg,f,min,max,ext_params) then
2832: cg:Merge(Auxiliary.SubGroupCaptured)
2833: else
2834: eg:RemoveCard(c)
2835: end
2836: end
2837: end
2838: cg:Sub(sg)
2839: finish=(#sg>=min and #sg<=max and f(sg,...))
2840: if #cg==0 then break end
2841: local cancel=not finish and cancelable
2842: local tc=cg:SelectUnselect(sg,tp,finish,cancel,min,max)
2843: if not tc then break end
2844: if not fg:IsContains(tc) then
2845: if not sg:IsContains(tc) then
2846: sg:AddCard(tc)
2847: if #sg==max then finish=true end
2848: else
2849: sg:RemoveCard(tc)
2850: end
2851: elseif cancelable then
2852: return nil
2853: end
2854: end
2855: if finish then
2856: return sg
2857: else
2858: return nil
2859: end
2860: end
2861: function Auxiliary.CreateChecks(f,list)
2862: local checks={}
2863: for i=1,#list do
2864: checks[i]=function(c) return f(c,list[i]) end
2865: end
2866: return checks
2867: end
2868: function Auxiliary.CheckGroupRecursiveEach(c,sg,g,f,checks,ext_params)
2869: if not checks[1+#sg](c) then
2870: return false
2871: end
2872: sg:AddCard(c)
2873: if Auxiliary.GCheckAdditional and not Auxiliary.GCheckAdditional(sg,c,g,f,min,max,ext_params) then
2874: sg:RemoveCard(c)
2875: return false
2876: end
2877: local res
2878: if #sg==#checks then
2879: res=f(sg,table.unpack(ext_params))
2880: else
2881: res=g:IsExists(Auxiliary.CheckGroupRecursiveEach,1,sg,sg,g,f,checks,ext_params)
2882: end
2883: sg:RemoveCard(c)
2884: return res
2885: end
2886: function Group.CheckSubGroupEach(g,checks,f,...)
2887: if f==nil then f=Auxiliary.TRUE end
2888: if #g<#checks then return false end
2889: local ext_params={...}
2890: local sg=Group.CreateGroup()
2891: return g:IsExists(Auxiliary.CheckGroupRecursiveEach,1,sg,sg,g,f,checks,ext_params)
2892: end
2893: function Group.SelectSubGroupEach(g,tp,checks,cancelable,f,...)
2894: if cancelable==nil then cancelable=false end
2895: if f==nil then f=Auxiliary.TRUE end
2896: local ct=#checks
2897: local ext_params={...}
2898: local sg=Group.CreateGroup()
2899: local finish=false
2900: while #sg<ct do
2901: local cg=g:Filter(Auxiliary.CheckGroupRecursiveEach,sg,sg,g,f,checks,ext_params)
2902: if #cg==0 then break end
2903: local tc=cg:SelectUnselect(sg,tp,false,cancelable,ct,ct)
2904: if not tc then break end
2905: if not sg:IsContains(tc) then
2906: sg:AddCard(tc)
2907: if #sg==ct then finish=true end
2908: else
2909: sg:Clear()
2910: end
2911: end
2912: if finish then
2913: return sg
2914: else
2915: return nil
2916: end
2917: end
2918: --condition of "negate activation and banish"
2919: function Auxiliary.nbcon(tp,re)
2920: local rc=re:GetHandler()
2921: return Duel.IsPlayerCanRemove(tp)
2922: and (not rc:IsRelateToEffect(re) or rc:IsAbleToRemove())
2923: end
2924: function Auxiliary.nbtg(e,tp,eg,ep,ev,re,r,rp,chk)
2925: if chk==0 then return aux.nbcon(tp,re) end
2926: Duel.SetOperationInfo(0,CATEGORY_NEGATE,eg,1,0,0)
2927: if re:GetHandler():IsRelateToEffect(re) then
2928: Duel.SetOperationInfo(0,CATEGORY_REMOVE,eg,1,0,0)
2929: end
2930: if re:GetActivateLocation()==LOCATION_GRAVE then
2931: e:SetCategory(e:GetCategory()|CATEGORY_GRAVE_ACTION)
2932: else
2933: e:SetCategory(e:GetCategory()&~CATEGORY_GRAVE_ACTION)
2934: end
2935: end
2936: --condition of "negate activation and return to deck"
2937: function Auxiliary.ndcon(tp,re)
2938: local rc=re:GetHandler()
2939: return re:IsHasType(EFFECT_TYPE_ACTIVATE) or not rc:IsRelateToEffect(re) or rc:IsAbleToDeck()
2940: end
2941: --send to deck of contact fusion
2942: function Auxiliary.tdcfop(c)
2943: return function(g)
2944: local cg=g:Filter(Card.IsFacedown,nil)
2945: if cg:GetCount()>0 then
2946: Duel.ConfirmCards(1-c:GetControler(),cg)
2947: end
2948: Duel.SendtoDeck(g,nil,SEQ_DECKSHUFFLE,REASON_COST)
2949: end
2950: end
2951: --return the global index of the zone in (p,loc,seq)
2952: function Auxiliary.SequenceToGlobal(p,loc,seq)
2953: if p~=0 and p~=1 then
2954: return 0
2955: end
2956: if loc==LOCATION_MZONE then
2957: if seq<=6 then
2958: return 0x0001<<(16*p+seq)
2959: else
2960: return 0
2961: end
2962: elseif loc == LOCATION_SZONE then
2963: if seq<=4 then
2964: return 0x0100<<(16*p+seq)
2965: else
2966: return 0
2967: end
2968: else
2969: return 0
2970: end
2971: end
2972: --use the count limit of Lair of Darkness if the tributes are not selected by Duel.SelectReleaseGroup
2973: function Auxiliary.UseExtraReleaseCount(g,tp)
2974: local eg=g:Filter(Auxiliary.ExtraReleaseFilter,nil,tp)
2975: for ec in Auxiliary.Next(eg) do
2976: local te=ec:IsHasEffect(EFFECT_EXTRA_RELEASE_NONSUM,tp)
2977: if te then te:UseCountLimit(tp) end
2978: end
2979: end
2980: function Auxiliary.ExtraReleaseFilter(c,tp)
2981: return c:IsControler(1-tp) and c:IsHasEffect(EFFECT_EXTRA_RELEASE_NONSUM,tp)
2982: end
2983: function Auxiliary.IsSpecialSummonedByEffect(e)
2984: return not ((e:GetCode()==EFFECT_SPSUMMON_PROC or e:GetCode()==EFFECT_SPSUMMON_PROC_G) and e:GetProperty()&(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE)==(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE))
2985: end
2986: --
2987: function Auxiliary.GetCappedLevel(c)
2988: local lv=c:GetLevel()
2989: if lv>MAX_PARAMETER then
2990: return MAX_PARAMETER
2991: else
2992: return lv
2993: end
2994: end
2995: --
2996: function Auxiliary.GetCappedAttack(c)
2997: local x=c:GetAttack()
2998: if x>MAX_PARAMETER then
2999: return MAX_PARAMETER
3000: else
3001: return x
3002: end
3003: end
3004: --when this card is sent to grave, record the reason effect
3005: --to check whether the reason effect do something simultaneously
3006: --so the "while this card is in your GY" condition isn't met
3007: function Auxiliary.AddThisCardInGraveAlreadyCheck(c)
3008: local e1=Effect.CreateEffect(c)
3009: e1:SetType(EFFECT_TYPE_SINGLE+EFFECT_TYPE_CONTINUOUS)
3010: e1:SetCode(EVENT_TO_GRAVE)
3011: e1:SetCondition(Auxiliary.ThisCardInGraveAlreadyCheckReg)
3012: c:RegisterEffect(e1)
3013: return e1
3014: end
3015: function Auxiliary.ThisCardInGraveAlreadyCheckReg(e,tp,eg,ep,ev,re,r,rp)
3016: --condition of continous effect will be checked before other effects
3017: if re==nil then return false end
3018: if e:GetLabelObject()~=nil then return false end
3019: if (r&REASON_EFFECT)>0 then
3020: e:SetLabelObject(re)
3021: local e1=Effect.CreateEffect(e:GetHandler())
3022: e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
3023: e1:SetCode(EVENT_CHAIN_END)
3024: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
3025: e1:SetOperation(Auxiliary.ThisCardInGraveAlreadyReset1)
3026: e1:SetLabelObject(e)
3027: Duel.RegisterEffect(e1,tp)
3028: local e2=e1:Clone()
3029: e2:SetCode(EVENT_BREAK_EFFECT)
3030: e2:SetOperation(Auxiliary.ThisCardInGraveAlreadyReset2)
3031: e2:SetReset(RESET_CHAIN)
3032: e2:SetLabelObject(e1)
3033: Duel.RegisterEffect(e2,tp)
3034: elseif (r&REASON_MATERIAL)>0 or not re:IsActivated() and (r&REASON_COST)>0 then
3035: e:SetLabelObject(re)
3036: local reset_event=EVENT_SPSUMMON
3037: if re:GetCode()~=EFFECT_SPSUMMON_PROC then reset_event=EVENT_SUMMON end
3038: local e1=Effect.CreateEffect(e:GetHandler())
3039: e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
3040: e1:SetCode(reset_event)
3041: e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
3042: e1:SetOperation(Auxiliary.ThisCardInGraveAlreadyReset1)
3043: e1:SetLabelObject(e)
3044: Duel.RegisterEffect(e1,tp)
3045: end
3046: return false
3047: end
3048: function Auxiliary.ThisCardInGraveAlreadyReset1(e)
3049: --this will run after EVENT_SPSUMMON_SUCCESS
3050: e:GetLabelObject():SetLabelObject(nil)
3051: e:Reset()
3052: end
3053: function Auxiliary.ThisCardInGraveAlreadyReset2(e)
3054: local e1=e:GetLabelObject()
3055: e1:GetLabelObject():SetLabelObject(nil)
3056: e1:Reset()
3057: e:Reset()
3058: end
3059: --Player p place g on the top of Deck in any order
3060: function Auxiliary.PlaceCardsOnDeckTop(p,g,reason)
3061: if reason==nil then reason=REASON_EFFECT end
3062: Duel.SendtoDeck(g,nil,SEQ_DECKTOP,reason)
3063: local rg=Duel.GetOperatedGroup()
3064: local og=rg:Filter(Card.IsLocation,nil,LOCATION_DECK)
3065: local ct1=og:FilterCount(Card.IsControler,nil,p)
3066: local ct2=og:FilterCount(Card.IsControler,nil,1-p)
3067: if ct1>1 then
3068: Duel.SortDecktop(p,p,ct1)
3069: end
3070: if ct2>1 then
3071: Duel.SortDecktop(p,1-p,ct2)
3072: end
3073: return #rg
3074: end
3075: --Player p place g on the bottom of Deck in any order
3076: function Auxiliary.PlaceCardsOnDeckBottom(p,g,reason)
3077: if reason==nil then reason=REASON_EFFECT end
3078: Duel.SendtoDeck(g,nil,SEQ_DECKTOP,reason)
3079: local rg=Duel.GetOperatedGroup()
3080: local og=rg:Filter(Card.IsLocation,nil,LOCATION_DECK)
3081: local ct1=og:FilterCount(Card.IsControler,nil,p)
3082: local ct2=og:FilterCount(Card.IsControler,nil,1-p)
3083: if ct1>0 then
3084: if ct1>1 then
3085: Duel.SortDecktop(p,p,ct1)
3086: end
3087: for i=1,ct1 do
3088: local tc=Duel.GetDecktopGroup(p,1):GetFirst()
3089: Duel.MoveSequence(tc,SEQ_DECKBOTTOM)
3090: end
3091: end
3092: if ct2>0 then
3093: if ct2>1 then
3094: Duel.SortDecktop(p,1-p,ct2)
3095: end
3096: for i=1,ct2 do
3097: local tc=Duel.GetDecktopGroup(1-p,1):GetFirst()
3098: Duel.MoveSequence(tc,SEQ_DECKBOTTOM)
3099: end
3100: end
3101: return #rg
3102: end
3103: --The event is triggered multiple times in a chain
3104: --but only 1 event with EVENT_CUSTOM+code will be triggered at EVENT_CHAIN_END, or immediately if not in chain
3105: --NOTE: re,r,rp,ep,ev of that custom event ARE NOT releated to the real event that trigger this custom event
3106: function Auxiliary.RegisterMergedDelayedEvent(c,code,event,g)
3107: local mt=getmetatable(c)
3108: if mt[event]==true then return end
3109: mt[event]=true
3110: if not g then g=Group.CreateGroup() end
3111: g:KeepAlive()
3112: local ge1=Effect.CreateEffect(c)
3113: ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
3114: ge1:SetCode(event)
3115: ge1:SetLabel(code)
3116: ge1:SetLabelObject(g)
3117: ge1:SetOperation(Auxiliary.MergedDelayEventCheck1)
3118: Duel.RegisterEffect(ge1,0)
3119: local ge2=ge1:Clone()
3120: ge2:SetCode(EVENT_CHAIN_END)
3121: ge2:SetOperation(Auxiliary.MergedDelayEventCheck2)
3122: Duel.RegisterEffect(ge2,0)
3123: end
3124: function Auxiliary.MergedDelayEventCheck1(e,tp,eg,ep,ev,re,r,rp)
3125: local g=e:GetLabelObject()
3126: g:Merge(eg)
3127: if Duel.GetCurrentChain()==0 and not Duel.CheckEvent(EVENT_CHAIN_END) then
3128: local _eg=g:Clone()
3129: Duel.RaiseEvent(_eg,EVENT_CUSTOM+e:GetLabel(),re,r,rp,ep,ev)
3130: g:Clear()
3131: end
3132: end
3133: function Auxiliary.MergedDelayEventCheck2(e,tp,eg,ep,ev,re,r,rp)
3134: local g=e:GetLabelObject()
3135: if #g>0 then
3136: local _eg=g:Clone()
3137: Duel.RaiseEvent(_eg,EVENT_CUSTOM+e:GetLabel(),re,r,rp,ep,ev)
3138: g:Clear()
3139: end
3140: end