游戏王残局简化版

Annotation For script/utility.lua
Login

Annotation For script/utility.lua

Lines of script/utility.lua from check-in c83543efed that are changed by the sequence of edits moving toward check-in 9789dcf4b5:

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