forked from ProjectIgnis/CardScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.lua
1698 lines (1640 loc) · 49.9 KB
/
utility.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Auxiliary={}
aux=Auxiliary
function GetID()
return self_table,self_code
end
--Multi purpose token
if not c946 then
c946 = {}
setmetatable(c946, Card)
rawset(c946,"__index",c946)
c946.initial_effect=function()end
end
local function cost_replace_getvalideffs(replacecode,extracon,e,tp,eg,ep,ev,re,r,rp,chk)
local t={}
for _,eff in ipairs({Duel.GetPlayerEffect(tp,replacecode)}) do
if eff:CheckCountLimit(tp) then
local val=eff:GetValue()
if type(val)=="number" then
if val==1 then
table.insert(t,eff)
end
elseif type(val)=="function" then
if val(eff,e,tp,eg,ep,ev,re,r,rp,chk,extracon) then
table.insert(t,eff)
end
end
end
end
return t
end
function Auxiliary.CostWithReplace(base,replacecode,extracon,alwaysexecute)
return function(e,tp,eg,ep,ev,re,r,rp,chk)
if alwaysexecute and not alwaysexecute(e,tp,eg,ep,ev,re,r,rp,0) then return false end
local cond=base(e,tp,eg,ep,ev,re,r,rp,0)
if chk==0 then
if cond then return true end
for _,eff in ipairs({Duel.GetPlayerEffect(tp,replacecode)}) do
if eff:CheckCountLimit(tp) then
local val=eff:GetValue()
if type(val)=="number" and val==1 then return true end
if type(val)=="function" and val(eff,e,tp,eg,ep,ev,re,r,rp,chk,extracon) then return true end
end
end
return false
end
if alwaysexecute then alwaysexecute(e,tp,eg,ep,ev,re,r,rp,1) end
local effs=cost_replace_getvalideffs(replacecode,extracon,e,tp,eg,ep,ev,re,r,rp,chk)
if not cond or (cond and #effs>0 and Duel.SelectYesNo(tp,98)) then
local eff=effs[1]
if #effs>1 then
local desctable={}
for _,_eff in ipairs(effs) do
table.insert(desctable,_eff:GetDescription())
end
eff=effs[Duel.SelectOption(tp,false,table.unpack(desctable)) + 1]
end
local res={eff:GetOperation()(eff,e,tp,eg,ep,ev,re,r,rp,chk)}
eff:UseCountLimit(tp)
return table.unpack(res)
end
return base(e,tp,eg,ep,ev,re,r,rp,1)
end
end
local function setcodecondition(e)
local c=e:GetHandler()
local label=e:GetLabel()
if label>0 and c:GetOriginalCodeRule()==label then
return c:IsCode(c:GetOriginalCodeRule())
else
return true
end
end
function Card.AddSetcodesRule(c,code,copyable,...)
local prop=0
if not copyable then prop=EFFECT_FLAG_UNCOPYABLE end
local t={}
for _,setcode in pairs({...}) do
local e=Effect.CreateEffect(c)
e:SetType(EFFECT_TYPE_SINGLE)
e:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+prop)
e:SetCode(EFFECT_ADD_SETCODE)
e:SetValue(setcode)
e:SetLabel(code)
e:SetCondition(setcodecondition)
c:RegisterEffect(e)
table.insert(t,e)
end
return t
end
function Duel.LoadCardScript(code)
if type(code)=="number" then
code="c"..code..".lua"
elseif type(code)~="string" then
error("Parameter 1 should be \"number\" or \"string\"",2)
end
local card=string.sub(code,0,string.len(code)-4)
if not _G[card] then
local oldtable,oldcode=GetID()
_G[card] = {}
self_table=_G[card]
setmetatable(self_table, Card)
rawset(self_table,"__index",self_table)
self_code=tonumber(string.sub(card,2))
Duel.LoadScript(code)
self_table=oldtable
self_code=oldcode
end
end
function Card.GetMetatable(c,currentCode)
if currentCode then return _G["c" .. c:GetCode()] end
return c.__index
end
function Duel.GetMetatable(code)
return _G["c" .. code]
end
bit={}
function bit.band(a,b)
return a&b
end
function bit.bor(a,b)
return a|b
end
function bit.bxor(a,b)
return a~b
end
function bit.lshift(a,b)
return a<<b
end
function bit.rshift(a,b)
return a>>b
end
function bit.bnot(a)
return ~a
end
local function fieldargs(f,width)
w=width or 1
assert(f>=0,"field cannot be negative")
assert(w>0,"width must be positive")
assert(f+w<=64,"trying to access non-existent bits")
return f,~(-1<<w)
end
function bit.extract(r,field,width)
local f,m=fieldargs(field,width)
return (r>>f)&m
end
function bit.replace(r,v,field,width)
local f,m=fieldargs(field,width)
return (r&~(m<<f))|((v&m)<< f)
end
local _type=type
function type(o)
local tp=_type(o)
if tp~="userdata" then return tp
elseif o.GetOriginalCode then return "Card"
elseif o.KeepAlive then return "Group"
elseif o.SetLabelObject then return "Effect"
else return "userdata"
end
end
function Auxiliary.Stringid(code,id)
return (id&0xfffff)|code<<20
end
function Group.ForEach(g,f,...)
for tc in aux.Next(g) do
f(tc,...)
end
end
function Auxiliary.Next(g)
local first=true
return function()
if first then first=false return g:GetFirst()
else return g:GetNext() end
end
end
Group.Iter=Auxiliary.Next
function Auxiliary.NULL()
end
function Auxiliary.TRUE()
return true
end
function Auxiliary.FALSE()
return false
end
function Auxiliary.AND(...)
local funs={...}
return function(...)
for _,f in ipairs(funs) do
if not f(...) then return false end
end
return true
end
end
function Auxiliary.OR(...)
local funs={...}
return function(...)
for _,f in ipairs(funs) do
if f(...) then return true end
end
return false
end
end
function Auxiliary.tableAND(...)
local funs={...}
return function(...)
local ret={}
for _,f in ipairs(funs) do
local res={f(...)}
for _,val in pairs(res) do
ret[_]=val and (ret[_]==nil or ret[_])
end
end
return ret
end
end
function Auxiliary.tableOR(...)
local funs={...}
return function(...)
local ret={}
for _,f in ipairs(funs) do
local res={f(...)}
for _,val in pairs(res) do
ret[_]=val or not (ret[_]==nil or not ret[_])
end
end
return ret
end
end
function Auxiliary.NOT(f)
return function(...)
return not f(...)
end
end
function Auxiliary.TargetEqualFunction(f,value,...)
local params={...}
return function(effect,target)
return f(target,table.unpack(params))==value
end
end
function Auxiliary.TargetBoolFunction(f,...)
local params={...}
return function(effect,target)
return f(target,table.unpack(params))
end
end
function Auxiliary.FilterEqualFunction(f,value,...)
local params={...}
return function(target)
return f(target,table.unpack(params))==value
end
end
--used for Material Types Filter Bool (works for IsRace, IsAttribute, IsType)
function Auxiliary.FilterSummonCode(...)
local params={...}
return function(c,scard,sumtype,tp)
return c:IsSummonCode(scard,sumtype,tp,table.unpack(params))
end
end
function Auxiliary.FilterBoolFunctionEx(f,value)
return function(target,scard,sumtype,tp)
return f(target,value,scard,sumtype,tp)
end
end
function Auxiliary.FilterBoolFunctionEx2(f,...)
local params={...}
return function(target,scard,sumtype,tp)
return f(target,scard,sumtype,tp,table.unpack(params))
end
end
function Auxiliary.FilterBoolFunction(f,...)
local params={...}
return function(target)
return f(target,table.unpack(params))
end
end
local function GetMulti(tab,key,...)
if not key then return nil end
return (tab[key]~=nil and tab[key]) or GetMulti(tab,...)
end
function Auxiliary.ParamsFromTable(tab,key,...)
if key then
local val
if type(key)=="table" then
val=GetMulti(tab,table.unpack(key))
else
val=tab[key]
end
if ... then
return val,Auxiliary.ParamsFromTable(tab,...)
else
return val
end
end
end
function aux.FunctionWithNamedArgs(f,...)
local args={...}
return function(tab,...)
if type(tab)=="table" then
return f(Auxiliary.ParamsFromTable(tab,table.unpack(args)))
else
return f(tab,...)
end
end
end
function Auxiliary.GetExtraMaterials(tp,mustg,sc,summon_type)
local tg=Group.CreateGroup()
mustg = mustg or Group.CreateGroup()
local eff={Duel.GetPlayerEffect(tp,EFFECT_EXTRA_MATERIAL)}
local t={}
for _,te in ipairs(eff) do
if te:CheckCountLimit(tp) then
local eg=te:GetValue()(0,summon_type,te,tp,sc)-mustg
eg:KeepAlive()
tg=tg+eg
local efun=te:GetOperation() and te:GetOperation() or aux.TRUE
table.insert(t,{eg,efun,te})
end
end
return t,tg
end
function Auxiliary.CheckValidExtra(c,tp,sg,mg,lc,emt,filt)
local res=false
filt=filt or {}
for _,ex in ipairs(emt) do
if ex[1]:IsContains(c) and ex[2](c,ex[3],tp,sg,mg,lc,ex[1],0) then
res=true
table.insert(filt,ex)
end
end
return res
end
function Auxiliary.DeleteExtraMaterialGroups(emt)
for _,ex in ipairs(emt) do
if ex[3]:GetValue() then
ex[3]:GetValue()(2,nil,ex[3],ex[1])
end
ex[1]:DeleteGroup()
end
end
function Auxiliary.GetMustBeMaterialGroup(tp,eg,sump,sc,g,r)
--- eg all default materials, g - valid materials
local eff={Duel.GetPlayerEffect(tp,EFFECT_MUST_BE_MATERIAL)}
local sg=Group.CreateGroup()
for _,te in ipairs(eff) do
local val=type(te:GetValue())=='function' and te:GetValue()(te,eg,sump,sc,g) or te:GetValue()
if val&r>0 then
sg:AddCard(te:GetHandler())
end
end
return sg
end
--for additional registers
local regeff=Card.RegisterEffect
function Card.RegisterEffect(c,e,forced,...)
if c:IsStatus(STATUS_INITIALIZING) and not e then
error("Parameter 2 expected to be Effect, got nil instead.",2)
end
--1 == 511002571 - access to effects that activate that detach an Xyz Material as cost
--2 == 511001692 - access to Cardian Summoning conditions/effects
--4 == 12081875 - access to Thunder Dragon effects that activate by discarding
--8 == 511310036 - access to Allure Queen effects that activate by sending themselves to GY
local reg_e = regeff(c,e,forced)
if not reg_e then
return nil
end
local reg={...}
local resetflag,resetcount=e:GetReset()
for _,val in ipairs(reg) do
local prop=EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_SET_AVAILABLE
if e:IsHasProperty(EFFECT_FLAG_UNCOPYABLE) then prop=prop|EFFECT_FLAG_UNCOPYABLE end
local e2=Effect.CreateEffect(c)
e2:SetType(EFFECT_TYPE_SINGLE)
e2:SetProperty(prop,EFFECT_FLAG2_MAJESTIC_MUST_COPY)
if val==1 then
e2:SetCode(511002571)
elseif val==2 then
e2:SetCode(511001692)
elseif val==4 then
e2:SetCode(12081875)
elseif val==8 then
e2:SetCode(511310036)
end
e2:SetLabelObject(e)
e2:SetLabel(c:GetOriginalCode())
if resetflag and resetcount then
e2:SetReset(resetflag,resetcount)
elseif resetflag then
e2:SetReset(resetflag)
end
c:RegisterEffect(e2)
end
return reg_e
end
function Auxiliary.IsMaterialListCode(c,...)
if not c.material then return false end
local codes={...}
for _,code in ipairs(codes) do
for _,mcode in ipairs(c.material) do
if code==mcode then return true end
end
end
return false
end
function Auxiliary.IsMaterialListSetCard(c,...)
if not c.material_setcode then return false end
local setcodes={...}
for _,setcode in ipairs(setcodes) do
if type(c.material_setcode)=='table' then
for _,v in ipairs(c.material_setcode) do
if v==setcode then return true end
end
else
if c.material_setcode==setcode then return true end
end
end
return false
end
--Returns true if the Card "c" specifically lists any of the card IDs in "..."
function Auxiliary.IsCodeListed(c,...)
if not c.listed_names then return false end
local codes={...}
for _,code in ipairs(codes) do
for _,ccode in ipairs(c.listed_names) do
if code==ccode then return true end
end
end
return false
end
--Returns true if the Card "c" specifically lists the name of a card that is part of an archetype in "..."
Auxiliary.IsArchetypeCodeListed=(function()
local sc=Debug.AddCard(946,0,0,0,0,0)
Debug.ReloadFieldBegin=(function()
local old=Debug.ReloadFieldBegin
return function(...)
old(...)
sc=Debug.AddCard(946,0,0,0,0,0)
end
end
)()
return function(c,...)
if not c.listed_names then return false end
local setcodes={...}
for _,ccode in ipairs(c.listed_names) do
sc:Recreate(ccode)
for _,setcode in ipairs(setcodes) do
if sc:IsSetCard(setcode) then return true end
end
end
return false
end
end)()
--Returns true if the Card "c" specifically lists any of the card types in "..."
function Auxiliary.IsCardTypeListed(c,...)
if not c.listed_card_types then return false end
local card_types={...}
for _,typ in ipairs(card_types) do
for _,typp in ipairs(c.listed_card_types) do
if (typ&typp)~=0 then return true end
end
end
return false
end
--"Can be negated" check for monsters
function Auxiliary.disfilter1(c)
return c:IsFaceup() and not c:IsDisabled() and (not c:IsNonEffectMonster() or c:GetOriginalType()&TYPE_EFFECT~=0)
end
--"Can be negated" check for Spells/Traps
function Auxiliary.disfilter2(c)
return c:IsFaceup() and not c:IsDisabled() and c:IsType(TYPE_SPELL+TYPE_TRAP)
end
--"Can be negated" check for cards
function Auxiliary.disfilter3(c)
return aux.disfilter1(c) or aux.disfilter2(c)
end
--condition of EVENT_BATTLE_DESTROYING
function Auxiliary.bdcon(e,tp,eg,ep,ev,re,r,rp)
local c=e:GetHandler()
return c:IsRelateToBattle()
end
--condition of EVENT_BATTLE_DESTROYING + opponent monster
function Auxiliary.bdocon(e,tp,eg,ep,ev,re,r,rp)
local c=e:GetHandler()
return c:IsRelateToBattle() and c:IsStatus(STATUS_OPPO_BATTLE)
end
--condition of EVENT_BATTLE_DESTROYING + to_grave
function Auxiliary.bdgcon(e,tp,eg,ep,ev,re,r,rp)
local c=e:GetHandler()
local bc=c:GetBattleTarget()
return c:IsRelateToBattle() and bc:IsLocation(LOCATION_GRAVE) and bc:IsType(TYPE_MONSTER)
end
--condition of EVENT_BATTLE_DESTROYING + opponent monster + to_grave
function Auxiliary.bdogcon(e,tp,eg,ep,ev,re,r,rp)
local c=e:GetHandler()
local bc=c:GetBattleTarget()
return c:IsRelateToBattle() and c:IsStatus(STATUS_OPPO_BATTLE) and bc:IsLocation(LOCATION_GRAVE) and bc:IsType(TYPE_MONSTER)
end
--condition of EVENT_TO_GRAVE + destroyed_by_opponent_from_field
function Auxiliary.dogcon(e,tp,eg,ep,ev,re,r,rp)
local c=e:GetHandler()
return c:IsPreviousControler(tp) and c:IsReason(REASON_DESTROY) and rp==1-tp
end
--condition of "except the turn this card was sent to the Graveyard"
function Auxiliary.exccon(e)
return Duel.GetTurnCount()~=e:GetHandler():GetTurnID() or e:GetHandler():IsReason(REASON_RETURN)
end
--flag effect for spell counter
function Auxiliary.chainreg(e,tp,eg,ep,ev,re,r,rp)
if e:GetHandler():GetFlagEffect(1)==0 then
e:GetHandler():RegisterFlagEffect(1,RESET_EVENT+RESETS_STANDARD-RESET_TURN_SET+RESET_CHAIN,0,1)
end
end
--default filter for EFFECT_CANNOT_BE_BATTLE_TARGET
function Auxiliary.imval1(e,c)
return not c:IsImmuneToEffect(e)
end
--default filter for EFFECT_CANNOT_BE_BATTLE_TARGET + opponent
function Auxiliary.imval2(e,c)
return Auxiliary.imval1(e,c) and c:GetControler()~=e:GetHandlerPlayer()
end
--filter for EFFECT_CANNOT_BE_EFFECT_TARGET + opponent
function Auxiliary.tgoval(e,re,rp)
return rp~=e:GetHandlerPlayer()
end
--filter for EFFECT_INDESTRUCTABLE_EFFECT + self
function Auxiliary.indsval(e,re,rp)
return rp==e:GetHandlerPlayer()
end
--filter for EFFECT_INDESTRUCTABLE_EFFECT + opponent
function Auxiliary.indoval(e,re,rp)
return rp==1-e:GetHandlerPlayer()
end
--filter for non-zero ATK monsters
function Auxiliary.nzatk(c)
return c:IsFaceup() and c:GetAttack()>0
end
--filter for non-zero DEF monsters
function Auxiliary.nzdef(c)
return c:IsFaceup() and c:GetDefense()>0
end
--flag effect for summon/sp_summon turn
function Auxiliary.sumreg(e,tp,eg,ep,ev,re,r,rp)
local code=e:GetLabel()
for tc in aux.Next(eg) do
if tc:GetOriginalCode()==code then
tc:RegisterFlagEffect(code,RESET_EVENT|RESETS_STANDARD&~(RESET_TEMP_REMOVE|RESET_TURN_SET)|RESET_PHASE|PHASE_END,0,1)
end
end
end
function Auxiliary.sumlimit(sumtype)
return function(e,se,sp,st)
return (st&sumtype)==sumtype
end
end
--sp_summon condition for fusion monster
function Auxiliary.fuslimit(e,se,sp,st)
return aux.sumlimit(SUMMON_TYPE_FUSION)(e,se,sp,st)
end
--sp_summon condition for ritual monster
function Auxiliary.ritlimit(e,se,sp,st)
return aux.sumlimit(SUMMON_TYPE_RITUAL)(e,se,sp,st)
end
--sp_summon condition for synchro monster
function Auxiliary.synlimit(e,se,sp,st)
return aux.sumlimit(SUMMON_TYPE_SYNCHRO)(e,se,sp,st)
end
--sp_summon condition for xyz monster
function Auxiliary.xyzlimit(e,se,sp,st)
return aux.sumlimit(SUMMON_TYPE_XYZ)(e,se,sp,st)
end
--sp_summon condition for pendulum monster
function Auxiliary.penlimit(e,se,sp,st)
return aux.sumlimit(SUMMON_TYPE_PENDULUM)(e,se,sp,st)
end
--sp_summon condition for link monster
function Auxiliary.lnklimit(e,se,sp,st)
return aux.sumlimit(SUMMON_TYPE_LINK)(e,se,sp,st)
end
--value for EFFECT_CANNOT_BE_MATERIAL
function Auxiliary.cannotmatfilter(val1,...)
local allowed=val1
if type(val1)~="table" then allowed={val1,...} end
local tot=0
for _,val in pairs(allowed) do
tot = tot|val
end
return function(e,c,sumtype,tp)
local sum=tot&sumtype
for _,val in pairs(allowed) do
if sum==val then return 1 end
end
return 0
end
end
--effects inflicting damage to tp
function Auxiliary.damcon1(e,tp,eg,ep,ev,re,r,rp)
local e1=Duel.IsPlayerAffectedByEffect(tp,EFFECT_REVERSE_DAMAGE)
local e2=Duel.IsPlayerAffectedByEffect(tp,EFFECT_REVERSE_RECOVER)
local rd=e1 and not e2
local rr=not e1 and e2
local ex,cg,ct,cp,cv=Duel.GetOperationInfo(ev,CATEGORY_DAMAGE)
if ex and (cp==tp or cp==PLAYER_ALL) and not rd and not Duel.IsPlayerAffectedByEffect(tp,EFFECT_NO_EFFECT_DAMAGE) then
return true
end
ex,cg,ct,cp,cv=Duel.GetOperationInfo(ev,CATEGORY_RECOVER)
return ex and (cp==tp or cp==PLAYER_ALL) and rr and not Duel.IsPlayerAffectedByEffect(tp,EFFECT_NO_EFFECT_DAMAGE)
end
function Card.CheckAdjacent(c)
local p=c:GetControler()
local seq=c:GetSequence()
if seq>4 then return false end
return (seq>0 and Duel.CheckLocation(p,LOCATION_MZONE,seq-1))
or (seq<4 and Duel.CheckLocation(p,LOCATION_MZONE,seq+1))
end
function Card.MoveAdjacent(c)
local tp=c:GetControler()
local seq=c:GetSequence()
if seq>4 then return end
local flag=0
if seq>0 and Duel.CheckLocation(tp,LOCATION_MZONE,seq-1) then flag=flag|(0x1<<seq-1) end
if seq<4 and Duel.CheckLocation(tp,LOCATION_MZONE,seq+1) then flag=flag|(0x1<<seq+1) end
if flag==0 then return end
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TOZONE)
Duel.MoveSequence(c,math.log(Duel.SelectDisableField(tp,1,LOCATION_MZONE,0,~flag),2))
end
function Card.IsColumn(c,seq,tp,loc)
if not c:IsOnField() then return false end
local cseq=c:GetSequence()
local seq=seq
local loc=loc and loc or c:GetLocation()
local tp=tp and tp or c:GetControler()
if c:IsLocation(LOCATION_MZONE) then
if cseq==5 then cseq=1 end
if cseq==6 then cseq=3 end
else
if cseq==6 then cseq=5 end
end
if loc==LOCATION_MZONE then
if seq==5 then seq=1 end
if seq==6 then seq=3 end
else
if seq==6 then seq=5 end
end
if c:IsControler(tp) then
return cseq==seq
else
return cseq==4-seq
end
end
function Card.UpdateAttack(c,amt,reset,rc)
rc=rc and rc or c
local r=(c==rc) and RESETS_STANDARD_DISABLE or RESETS_STANDARD
reset=reset and reset or RESET_EVENT+r
local atk=c:GetAttack()
if atk>=-amt then --If amt is positive, it would become negative and always be lower than or equal to atk, if amt is negative, it would become postive and if it is too much it would be higher than atk
local e1=Effect.CreateEffect(rc)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetCode(EFFECT_UPDATE_ATTACK)
if c==rc then
e1:SetProperty(EFFECT_FLAG_COPY_INHERIT)
end
e1:SetValue(amt)
e1:SetReset(reset)
c:RegisterEffect(e1)
return c:GetAttack()-atk
end
return 0
end
function Card.UpdateDefense(c,amt,reset,rc)
rc=rc and rc or c
local r=(c==rc) and RESETS_STANDARD_DISABLE or RESETS_STANDARD
reset=reset and reset or RESET_EVENT+r
local def=c:GetDefense()
if def and def>=-amt then --See Card.UpdateAttack
local e1=Effect.CreateEffect(rc)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetCode(EFFECT_UPDATE_DEFENSE)
if c==rc then
e1:SetProperty(EFFECT_FLAG_COPY_INHERIT)
end
e1:SetValue(amt)
e1:SetReset(reset)
c:RegisterEffect(e1)
return c:GetDefense()-def
end
return 0
end
function Card.UpdateLevel(c,amt,reset,rc)
rc=rc and rc or c
local r=(c==rc) and RESETS_STANDARD_DISABLE or RESETS_STANDARD
reset=reset and reset or RESET_EVENT+r
local lv=c:GetLevel()
if c:IsLevelBelow(2147483647) then
if lv+amt<=0 then amt=-(lv-1) end --Unlike ATK, if amt is too much should reduce as much as possible
local e1=Effect.CreateEffect(rc)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetCode(EFFECT_UPDATE_LEVEL)
e1:SetValue(amt)
e1:SetReset(reset)
c:RegisterEffect(e1)
return c:GetLevel()-lv
end
return 0
end
function Card.UpdateRank(c,amt,reset,rc)
rc=rc and rc or c
local r=(c==rc) and RESETS_STANDARD_DISABLE or RESETS_STANDARD
reset=reset and reset or RESET_EVENT+r
local rk=c:GetRank()
if c:IsRankBelow(2147483647) then
if rk+amt<=0 then amt=-(rk-1) end --See Card.UpdateLevel
local e1=Effect.CreateEffect(rc)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetCode(EFFECT_UPDATE_RANK)
e1:SetValue(amt)
e1:SetReset(reset)
c:RegisterEffect(e1)
return c:GetRank()-rk
end
return 0
end
function Card.UpdateLink(c,amt,reset,rc)
rc=rc and rc or c
local r=(c==rc) and RESETS_STANDARD_DISABLE or RESETS_STANDARD
reset=reset and reset or RESET_EVENT+r
local lk=c:GetLink()
if c:IsLinkBelow(2147483647) then
if lk+amt<=0 then amt=-(lk-1) end --See Card.UpdateLevel
local e1=Effect.CreateEffect(rc)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetCode(EFFECT_UPDATE_LINK)
e1:SetValue(amt)
e1:SetReset(reset)
c:RegisterEffect(e1)
return c:GetLink()-lk
end
return 0
end
function Card.UpdateScale(c,amt,reset,rc)
rc=rc and rc or c
local r=(c==rc) and RESETS_STANDARD_DISABLE or RESETS_STANDARD
reset=reset and reset or RESET_EVENT+r
local scl=c:GetLeftScale()
if scl then
if scl+amt<=0 then amt = -(scl-1) end --See Card.UpdateLevel
local e1=Effect.CreateEffect(rc)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetCode(EFFECT_UPDATE_LSCALE)
e1:SetValue(amt)
e1:SetReset(reset)
c:RegisterEffect(e1)
local e2=e1:Clone()
e2:SetCode(EFFECT_UPDATE_RSCALE)
c:RegisterEffect(e2)
return c:GetLeftScale()-scl
end
return 0
end
function Auxiliary.BeginPuzzle()
local e1=Effect.GlobalEffect()
e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
e1:SetCode(EVENT_TURN_END)
e1:SetCountLimit(1)
e1:SetOperation(Auxiliary.PuzzleOp)
Duel.RegisterEffect(e1,0)
local e2=Effect.GlobalEffect()
e2:SetType(EFFECT_TYPE_FIELD)
e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
e2:SetCode(EFFECT_SKIP_DP)
e2:SetTargetRange(1,0)
Duel.RegisterEffect(e2,0)
local e3=Effect.GlobalEffect()
e3:SetType(EFFECT_TYPE_FIELD)
e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
e3:SetCode(EFFECT_SKIP_SP)
e3:SetTargetRange(1,0)
Duel.RegisterEffect(e3,0)
end
function Auxiliary.PuzzleOp(e,tp)
Duel.SetLP(0,0)
end
--Cost for effect "You can banish this card from your Graveyard"
function Auxiliary.bfgcost(e,tp,eg,ep,ev,re,r,rp,chk)
local c=e:GetHandler()
if chk==0 then return (not Duel.IsPlayerAffectedByEffect(e:GetHandlerPlayer(),69832741) or not c:IsType(TYPE_MONSTER)
or not c:IsLocation(LOCATION_GRAVE)) and c:IsAbleToRemoveAsCost() end
Duel.Remove(c,POS_FACEUP,REASON_COST)
end
-- "Detach Xyz Material Cost Generator"
-- Generates a function to be used by Effect.SetCost in order to detach
-- a number of Xyz Materials from the Effect's handler.
-- `min` minimum number of materials to check for detachment.
-- `max` maximum number of materials to detach or a function that gets called
-- as if by doing max(e,tp) in order to get the value of max detachments.
-- `op` optional function that gets called by passing the effect and the operated
-- group of just detached materials in order to do some additional handling with
-- them.
function Auxiliary.dxmcostgen(min,max,op)
do --Perform some sanity checks, simplifies debugging
local max_type=type(max)
local op_type=type(op)
if type(min)~="number" then
error("Parameter 1 should be an Integer",2)
end
if max_type~="number" and max_type~="function" then
error("Parameter 2 should be Integer|function",2)
end
if op_type~="nil" and op_type~="function" then
error("Parameter 2 should be nil|function",2)
end
end
return function(e,tp,eg,ep,ev,re,r,rp,chk)
local c=e:GetHandler()
local nn=c:IsSetCard(0x14b) and c:IsType(TYPE_XYZ) and Duel.IsPlayerAffectedByEffect(tp,CARD_NUMERON_NETWORK)
local crm=c:CheckRemoveOverlayCard(tp,min,REASON_COST)
if chk==0 then return (nn and c:IsLocation(LOCATION_MZONE)) or crm end
if nn and (not crm or Duel.SelectYesNo(tp,aux.Stringid(CARD_NUMERON_NETWORK,1))) then
Duel.Hint(HINT_CARD,tp,CARD_NUMERON_NETWORK)
return true --NOTE: Does not execute `op`
end
local m=type(max)=="number" and max or max(e,tp)
if c:RemoveOverlayCard(tp,min,m,REASON_COST) and op then
op(e,Duel.GetOperatedGroup())
end
return true --NOTE: to use with aux.AND
end
end
function Auxiliary.EquipByEffectLimit(e,c)
if e:GetOwner()~=c then return false end
local eff={c:GetCardEffect(89785779+EFFECT_EQUIP_LIMIT)}
for _,te in ipairs(eff) do
if te==e:GetLabelObject() then return true end
end
return false
end
--register for "Equip to this card by its effect"
function Auxiliary.EquipByEffectAndLimitRegister(c,e,tp,tc,code,mustbefaceup)
local up=false or mustbefaceup
if not Duel.Equip(tp,tc,c,up) then return false end
--Add Equip limit
if code then
tc:RegisterFlagEffect(code,RESET_EVENT+RESETS_STANDARD,0,0)
end
local te=e:GetLabelObject()
local e1=Effect.CreateEffect(c)
e1:SetType(EFFECT_TYPE_SINGLE)
e1:SetProperty(EFFECT_FLAG_OWNER_RELATE)
e1:SetCode(EFFECT_EQUIP_LIMIT)
e1:SetReset(RESET_EVENT+RESETS_STANDARD)
e1:SetValue(Auxiliary.EquipByEffectLimit)
e1:SetLabelObject(te)
tc:RegisterEffect(e1)
return true
end
--add a anounce digit by digit
function Auxiliary.ComposeNumberDigitByDigit(tp,min,max)
if min>max then min,max=max,min end
local mindc=#tostring(min)
local maxdc=#tostring(max)
local dbdmin={}
local dbdmax={}
local mi=maxdc-1
local aux=min
for i=1,maxdc do
dbdmin[i]=aux//(10^mi)
aux=aux%(10^mi)
mi=mi-1
end
aux=max
mi=maxdc-1
for i=1,maxdc do
dbdmax[i]=aux//(10^mi)
aux=aux%(10^mi)
mi=mi-1
end
local chku=true
local chkl=true
local dbd={}
mi=maxdc-1
for i=1,maxdc do
local maxval=9
local minval=0
if chku and i>1 and dbd[i-1]<dbdmax[i-1] then
chku=false
end
if chkl and i>1 and dbd[i-1]>dbdmin[i-1] then
chkl=false
end
if chku then
maxval=dbdmax[i]
end
if chkl then
minval=dbdmin[i]
end
local r={}
local j=1
for k=minval,maxval do
r[j]=k
j=j+1
end
dbd[i]=Duel.AnnounceNumber(tp,table.unpack(r))
mi=mi-1
end
local number=0
mi=maxdc-1
for i=1,maxdc do
number=number+dbd[i]*10^mi
mi=mi-1
end
return number
end
function Card.GetToBeLinkedZone(tc,c,tp,clink,emz)
local zone=0
local seq=tc:GetSequence()
if tc:IsLocation(LOCATION_MZONE) and tc:IsControler(tp) then
if c:IsLinkMarker(LINK_MARKER_LEFT) and seq < 4 and (not clink or tc:IsLinkMarker(LINK_MARKER_RIGHT)) then zone=zone|(1<<seq+1) end
if c:IsLinkMarker(LINK_MARKER_RIGHT) and seq > 0 and seq <= 4 and (not clink or tc:IsLinkMarker(LINK_MARKER_LEFT)) then zone=zone|(1<<seq-1) end
if c:IsLinkMarker(LINK_MARKER_TOP_RIGHT) and (seq == 5 or seq == 6) and (not clink or tc:IsLinkMarker(LINK_MARKER_BOTTOM_LEFT)) then zone=zone|(1<<2*(seq-5)) end
if c:IsLinkMarker(LINK_MARKER_TOP) and (seq == 5 or seq == 6) and (not clink or tc:IsLinkMarker(LINK_MARKER_BOTTOM)) then zone=zone|(1<<2*(seq-5)+1) end
if c:IsLinkMarker(LINK_MARKER_TOP_LEFT) and (seq == 5 or seq == 6) and (not clink or tc:IsLinkMarker(LINK_MARKER_BOTTOM_RIGHT)) then zone=zone|(1<<2*(seq-5)+2) end
if emz and c:IsLinkMarker(LINK_MARKER_BOTTOM_LEFT) and (seq == 0 or seq == 2) and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP_RIGHT)) then zone=zone|(1<<5+seq/2) end
if emz and c:IsLinkMarker(LINK_MARKER_BOTTOM) and (seq == 1 or seq == 3) and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP)) then zone=zone|(1<<5+(seq-1)/2) end
if emz and c:IsLinkMarker(LINK_MARKER_BOTTOM_RIGHT) and (seq == 2 or seq == 4) and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP_LEFT)) then zone=zone|(1<<5+(seq-2)/2) end
elseif tc:IsLocation(LOCATION_MZONE) then
if c:IsLinkMarker(LINK_MARKER_TOP_RIGHT) and (seq == 5 or seq == 6 or (emz and (seq == 0 or seq == 2))) and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP_RIGHT)) then
if seq == 5 or seq == 6 then
zone=zone|(1<<-2*(seq-5)+2)
else
zone=zone|(1<<-seq/2+6)
end
end
if c:IsLinkMarker(LINK_MARKER_TOP) and (seq == 5 or seq == 6 or (emz and (seq == 1 or seq == 3))) and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP)) then
if seq == 5 or seq == 6 then
zone=zone|(1<<-2*(seq-5)+3)
else
zone=zone|(1<<-(seq-1)/2+6)
end
end
if c:IsLinkMarker(LINK_MARKER_TOP_LEFT) and (seq == 2 or seq == 4 or (emz and (seq == 2 or seq == 4))) and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP_LEFT)) then
if seq == 5 or seq == 6 then
zone=zone|(1<<-2*(seq-5)+4)
else
zone=zone|(1<<-(seq-2)/2+6)
end
end
elseif tc:IsLocation(LOCATION_SZONE) and tc:IsControler(tp) then
if c:IsLinkMarker(LINK_MARKER_BOTTOM_LEFT) and seq < 4 and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP_RIGHT)) then zone=zone|(1<<(seq+1)) end
if c:IsLinkMarker(LINK_MARKER_BOTTOM) and seq <= 4 and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP)) then zone=zone|(1<<seq) end
if c:IsLinkMarker(LINK_MARKER_BOTTOM_RIGHT) and seq > 0 and seq <= 4 and (not clink or tc:IsLinkMarker(LINK_MARKER_TOP_LEFT)) then zone=zone(1<<(seq-1)) end
end
return zone
end
function Group.GetToBeLinkedZone(g,c,tp,clink,emz)