游戏王残局简化版

Annotation For script/utility.lua
Login

Annotation For script/utility.lua

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

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