forked from ProjectIgnis/CardScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.lua
2208 lines (2085 loc) · 67.2 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
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
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
--Multi purpose token
if not c946 then
c946 = {}
setmetatable(c946, Card)
c946.__tostring=Debug.CardToStringWrapper
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 cost_chk=base(e,tp,eg,ep,ev,re,r,rp,0)
if chk==0 then
if cost_chk 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
local effs=cost_replace_getvalideffs(replacecode,extracon,e,tp,eg,ep,ev,re,r,rp,chk)
if alwaysexecute then alwaysexecute(e,tp,eg,ep,ev,re,r,rp,1) end
if not cost_chk or #effs>0 then
local eff=effs[1]
if #effs>1 then
local effsPerCard={}
local effsHandlersGroup=Group.CreateGroup()
for _,_eff in ipairs(effs) do
local _effCard=_eff:GetHandler()
effsHandlersGroup:AddCard(_effCard)
if not effsPerCard[_effCard] then effsPerCard[_effCard]={} end
table.insert(effsPerCard[_effCard],_eff)
end
local effCard=nil
if #effsHandlersGroup==1 and (not cost_chk or Duel.SelectEffectYesNo(tp,effCard)) then
effCard=effsHandlersGroup:GetFirst()
elseif #effsHandlersGroup>1 then
while effCard==nil and (not cost_chk or Duel.SelectYesNo(tp,98)) do
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RESOLVEEFFECT)
effCard=effsHandlersGroup:Select(tp,1,1,cost_chk,nil)
end
if effCard then effCard=effCard:GetFirst() end
end
if not effCard then return base(e,tp,eg,ep,ev,re,r,rp,1) end
local effsOfThatCard=effsPerCard[effCard]
if #effsOfThatCard==1 then
eff=effsOfThatCard[1]
else
local desctable={}
for _,_eff in ipairs(effsOfThatCard) do
table.insert(desctable,_eff:GetDescription())
end
eff=effsOfThatCard[Duel.SelectOption(tp,false,table.unpack(desctable)) + 1]
end
elseif cost_chk and not Duel.SelectEffectYesNo(tp,eff:GetHandler()) then
return base(e,tp,eg,ep,ev,re,r,rp,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
Card.IsMonster=aux.FilterBoolFunction(Card.IsType,TYPE_MONSTER)
Card.IsSpell=aux.FilterBoolFunction(Card.IsType,TYPE_SPELL)
Card.IsTrap=aux.FilterBoolFunction(Card.IsType,TYPE_TRAP)
Card.IsSpellTrap=aux.FilterBoolFunction(Card.IsType,TYPE_SPELL|TYPE_TRAP)
local function make_exact_type_check(type)
return aux.FilterBoolFunction(Card.IsExactType,type)
end
Card.IsQuickPlaySpell=make_exact_type_check(TYPE_SPELL|TYPE_QUICKPLAY)
Card.IsContinuousSpell=make_exact_type_check(TYPE_SPELL|TYPE_CONTINUOUS)
Card.IsEquipSpell=make_exact_type_check(TYPE_SPELL|TYPE_EQUIP)
Card.IsFieldSpell=make_exact_type_check(TYPE_SPELL|TYPE_FIELD)
Card.IsRitualSpell=make_exact_type_check(TYPE_SPELL|TYPE_RITUAL)
Card.IsLinkSpell=make_exact_type_check(TYPE_SPELL|TYPE_LINK)
Card.IsContinuousTrap=make_exact_type_check(TYPE_TRAP|TYPE_CONTINUOUS)
Card.IsCounterTrap=make_exact_type_check(TYPE_TRAP|TYPE_COUNTER)
Card.IsRitualMonster=make_exact_type_check(TYPE_MONSTER|TYPE_RITUAL)
Card.IsLinkMonster=make_exact_type_check(TYPE_MONSTER|TYPE_LINK)
function Card.IsNormalSpell(c)
return c:GetType()==TYPE_SPELL
end
function Card.IsNormalTrap(c)
return c:GetType()==TYPE_TRAP
end
function Card.IsNonEffectMonster(c)
return c:IsMonster() and not c:IsType(TYPE_EFFECT)
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 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.SelectAdjacent(c,sel_player)
local seq=c:GetSequence()
if seq>4 then return end
local flag=0
local targ_player=c:GetControler()
if seq>0 and Duel.CheckLocation(targ_player,LOCATION_MZONE,seq-1) then flag=flag|(0x1<<seq-1) end
if seq<4 and Duel.CheckLocation(targ_player,LOCATION_MZONE,seq+1) then flag=flag|(0x1<<seq+1) end
if flag==0 then return end
local sel_player=sel_player==nil and targ_player or sel_player
local loc1=targ_player==sel_player and LOCATION_MZONE or 0
local loc2=loc1==0 and LOCATION_MZONE or 0
local shift=loc2*4
Duel.Hint(HINT_SELECTMSG,sel_player,HINTMSG_TOZONE)
local zone=(Duel.SelectDisableField(sel_player,1,loc1,loc2,~(flag<<shift)))>>shift
Duel.Hint(HINT_ZONE,targ_player,zone)
return math.log(zone,2)
end
function Card.MoveAdjacent(c,sel_player)
local seq=c:GetSequence()
if seq>4 then return end
local flag=0
local targ_player=c:GetControler()
if seq>0 and Duel.CheckLocation(targ_player,LOCATION_MZONE,seq-1) then flag=flag|(0x1<<seq-1) end
if seq<4 and Duel.CheckLocation(targ_player,LOCATION_MZONE,seq+1) then flag=flag|(0x1<<seq+1) end
if flag==0 then return end
local sel_player=sel_player==nil and targ_player or sel_player
local loc1=targ_player==sel_player and LOCATION_MZONE or 0
local loc2=loc1==0 and LOCATION_MZONE or 0
local shift=loc2*4
Duel.Hint(HINT_SELECTMSG,sel_player,HINTMSG_TOZONE)
local zone=Duel.SelectDisableField(sel_player,1,loc1,loc2,~(flag<<shift))
Duel.MoveSequence(c,math.log(zone>>shift,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 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 Card.GetScale(c)
if not c:IsType(TYPE_PENDULUM) then return 0 end
local sc=0
if c:IsLocation(LOCATION_PZONE) then
local seq=c:GetSequence()
if seq==0 --mr4+ pzones
or seq==1 --three columns field pzones
or seq==6 --mr3 pzones
then sc=c:GetLeftScale() else sc=c:GetRightScale() end
else
sc=c:GetLeftScale()
end
return sc
end
function Card.IsOddScale(c)
if not c:IsType(TYPE_PENDULUM) then return false end
return c:GetScale() % 2 ~= 0
end
function Card.IsEvenScale(c)
if not c:IsType(TYPE_PENDULUM) then return false end
return c:GetScale() % 2 == 0
end
function Card.CanSummonOrSet(...)
return Card.IsSummonable(...) or Card.IsMSetable(...)
end
function Card.IsBattleDestroyed(c)
return c:IsStatus(STATUS_BATTLE_DESTROYED) and c:IsReason(REASON_BATTLE)
end
function Card.IsInMainMZone(c,tp)
return c:IsLocation(LOCATION_MMZONE) and (not tp or c:IsControler(tp))
end
function Card.IsInExtraMZone(c,tp)
return c:IsLocation(LOCATION_EMZONE) and (not tp or c:IsControler(tp))
end
function Card.AnnounceAnotherAttribute(c,tp)
local att=c:GetAttribute()
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_ATTRIBUTE)
return Duel.AnnounceAttribute(tp,1,att&(att-1)==0 and ~att or ATTRIBUTE_ALL)
end
function Card.IsDifferentAttribute(c,att)
local _att=c:GetAttribute()
return (_att&att)~=_att
end
function Card.AnnounceAnotherRace(c,tp)
local race=c:GetRace()
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_RACE)
return Duel.AnnounceRace(tp,1,race&(race-1)==0 and ~race or RACE_ALL)
end
function Card.IsDifferentRace(c,race)
local _race=c:GetRace()
return (_race&race)~=_race
end
--Checks whether the card is located at any of the sequences passed as arguments.
function Card.IsSequence(c,...)
local arg={...}
local seq=c:GetSequence()
for _,v in ipairs(arg) do
if seq==v then return true end
end
return false
end
--Checks wheter a card has a level or not
--For Links: false. For Xyzs: false, except if affected by "EFFECT_RANK_LEVEL..." effects
--For Dark Synchros: true, because they have a negative level. For level 0: true, because 0 is a value
function Card.HasLevel(c)
if c:IsMonster() then
return c:GetType()&TYPE_LINK~=TYPE_LINK
and (c:GetType()&TYPE_XYZ~=TYPE_XYZ and not (c:IsHasEffect(EFFECT_RANK_LEVEL) or c:IsHasEffect(EFFECT_RANK_LEVEL_S)))
and not c:IsStatus(STATUS_NO_LEVEL)
elseif c:IsOriginalType(TYPE_MONSTER) then
return not (c:IsOriginalType(TYPE_XYZ+TYPE_LINK) or c:IsStatus(STATUS_NO_LEVEL))
end
return false
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
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_table.__tostring=Debug.CardToStringWrapper
self_code=tonumber(string.sub(card,2))
Duel.LoadScript(code)
self_table=oldtable
self_code=oldcode
end
end
function Effect.IsMonsterEffect(e)
return e:IsActiveType(TYPE_MONSTER)
end
function Effect.IsSpellEffect(e)
return e:IsActiveType(TYPE_SPELL)
end
function Effect.IsTrapEffect(e)
return e:IsActiveType(TYPE_TRAP)
end
function Effect.IsSpellTrapEffect(e)
return e:IsActiveType(TYPE_SPELL|TYPE_TRAP)
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
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
--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
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
if key == "vaargs" and type(val)=="table" then
return table.unpack(val)
end
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
Card.RegisterEffect=(function()
local oldf=Card.RegisterEffect
local function map_to_effect_code(val)
if val==1 then return 511002571 end -- access to effects that activate that detach an Xyz Material as cost
if val==2 then return 511001692 end -- access to Cardian Summoning conditions/effects
if val==4 then return 12081875 end -- access to Thunder Dragon effects that activate by discarding
if val==8 then return 511310036 end -- access to Allure Queen effects that activate by sending themselves to GY
if val==16 then return 58858807 end -- access to tellarknights/constellar effects that activate when Normal Summoned
return nil
end
return function(c,e,forced,...)
local reg_e=oldf(c,e,forced)
if not reg_e or reg_e<=0 then return reg_e end
local resetflag,resetcount=e:GetReset()
for _,val in ipairs{...} do
local code=map_to_effect_code(val)
if code then
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)
e2:SetCode(code)
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
end
return reg_e
end
end)()
function Card.ListsCodeAsMaterial(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
local function MatchSetcode(set_code,to_match)
return (set_code&0xfff)==(to_match&0xfff) and (set_code&to_match)==set_code;
end
function Card.ListsArchetypeAsMaterial(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 MatchSetcode(setcode,v) then return true end
end
else
if MatchSetcode(setcode,c.material_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 Card.ListsCode(c,...)
if c.listed_names then
local codes={...}
for _,wanted in ipairs(codes) do
for _,cardcode in ipairs(c.listed_names) do
if wanted==cardcode then return true end
end
end
elseif c.fit_monster then
local codes={...}
for _,wanted in ipairs(codes) do
for _,listed in ipairs(c.fit_monster) do
if wanted==listed then return true end
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 "..."
function Card.ListsCodeWithArchetype(c,...)
if not c.listed_names then return false end
local setcodes={...}
for _,cardcode in ipairs(c.listed_names) do
local match_setcodes={Duel.GetCardSetcodeFromCode(cardcode)}
if #match_setcodes>0 then
for _,setcode in ipairs(setcodes) do
for _,to_match in ipairs(match_setcodes) do
if MatchSetcode(setcode,to_match) then return true end
end
end
end
end
return false
end
--Returns true if the Card "c" specifically lists any of the card types in "..."
function Card.ListsCardType(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
--Returns true if the Card "c" lists any of the setcodes passed in "..."
function Card.ListsArchetype(c,...)
if not c.listed_series then return false end
local listed_archetypes={...}
for _,wanted in ipairs(listed_archetypes) do
for _,listed in ipairs(c.listed_series) do
if wanted==listed then return true end
end
end
return false
end
--"Can be negated" check for monsters
function Card.IsNegatableMonster(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 Card.IsNegatableSpellTrap(c)
return c:IsFaceup() and not c:IsDisabled() and c:IsSpellTrap()
end
--"Can be negated" check for cards
function Card.IsNegatable(c)
return c:IsNegatableMonster() or c:IsNegatableSpellTrap()
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:IsMonster()
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:IsMonster()
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 Card.HasNonZeroAttack(c)
return c:IsFaceup() and c:GetAttack()>0
end
--filter for non-zero DEF monsters
function Card.HasNonZeroDefense(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