forked from ProjectIgnis/CardScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_pendulum.lua
154 lines (154 loc) · 5.18 KB
/
proc_pendulum.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
if not aux.PendulumProcedure then
aux.PendulumProcedure = {}
Pendulum = aux.PendulumProcedure
end
if not Pendulum then
Pendulum = aux.PendulumProcedure
end
--add procedure to Pendulum monster, also allows registeration of activation effect
Pendulum.AddProcedure = aux.FunctionWithNamedArgs(
function(c,reg,desc)
local e1=Effect.CreateEffect(c)
e1:SetType(EFFECT_TYPE_FIELD)
if desc then
e1:SetDescription(desc)
else
e1:SetDescription(1074)
end
e1:SetCode(EFFECT_SPSUMMON_PROC_G)
e1:SetProperty(EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_CANNOT_DISABLE)
e1:SetRange(LOCATION_PZONE)
e1:SetCondition(Pendulum.Condition())
e1:SetOperation(Pendulum.Operation())
e1:SetValue(SUMMON_TYPE_PENDULUM)
c:RegisterEffect(e1)
--register by default
if reg==nil or reg then
local e2=Effect.CreateEffect(c)
e2:SetDescription(1160)
e2:SetType(EFFECT_TYPE_ACTIVATE)
e2:SetCode(EVENT_FREE_CHAIN)
e2:SetRange(LOCATION_HAND)
c:RegisterEffect(e2)
end
end,"handler","register","desc")
function Pendulum.Filter(c,e,tp,lscale,rscale,lvchk)
if lscale>rscale then lscale,rscale=rscale,lscale end
local lv=0
if c.pendulum_level then
lv=c.pendulum_level
else
lv=c:GetLevel()
end
return (c:IsLocation(LOCATION_HAND) or (c:IsFaceup() and c:IsType(TYPE_PENDULUM)))
and (lvchk or (lv>lscale and lv<rscale) or c:IsHasEffect(511004423)) and c:IsCanBeSpecialSummoned(e,SUMMON_TYPE_PENDULUM,tp,false,false)
and not c:IsForbidden()
end
function Pendulum.Condition()
return function(e,c,inchain,re,rp)
if c==nil then return true end
local tp=c:GetControler()
local rpz=Duel.GetFieldCard(tp,LOCATION_PZONE,1)
if rpz==nil or c==rpz or (not inchain and Duel.GetFlagEffect(tp,10000000)>0) then return false end
local lscale=c:GetLeftScale()
local rscale=rpz:GetRightScale()
local loc=0
if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then loc=loc+LOCATION_HAND end
if Duel.GetLocationCountFromEx(tp,tp,nil,TYPE_PENDULUM)>0 then loc=loc+LOCATION_EXTRA end
if loc==0 then return false end
local g=nil
if og then
g=og:Filter(Card.IsLocation,nil,loc)
else
g=Duel.GetFieldGroup(tp,loc,0)
end
return g:IsExists(Pendulum.Filter,1,nil,e,tp,lscale,rscale,c:IsHasEffect(511007000) and rpz:IsHasEffect(511007000))
end
end
function Pendulum.Operation()
return function(e,tp,eg,ep,ev,re,r,rp,c,sg,inchain)
local rpz=Duel.GetFieldCard(tp,LOCATION_PZONE,1)
local lscale=c:GetLeftScale()
local rscale=rpz:GetRightScale()
local ft1=Duel.GetLocationCount(tp,LOCATION_MZONE)
local ft2=Duel.GetLocationCountFromEx(tp,tp,nil,TYPE_PENDULUM)
local ft=Duel.GetUsableMZoneCount(tp)
if Duel.IsPlayerAffectedByEffect(tp,CARD_BLUEEYES_SPIRIT) then
if ft1>0 then ft1=1 end
if ft2>0 then ft2=1 end
ft=1
end
local loc=0
if ft1>0 then loc=loc+LOCATION_HAND end
if ft2>0 then loc=loc+LOCATION_EXTRA end
local tg=nil
if og then
tg=og:Filter(Card.IsLocation,nil,loc):Match(Pendulum.Filter,nil,e,tp,lscale,rscale,c:IsHasEffect(511007000) and rpz:IsHasEffect(511007000))
else
tg=Duel.GetMatchingGroup(Pendulum.Filter,tp,loc,0,nil,e,tp,lscale,rscale,c:IsHasEffect(511007000) and rpz:IsHasEffect(511007000))
end
ft1=math.min(ft1,tg:FilterCount(Card.IsLocation,nil,LOCATION_HAND))
ft2=math.min(ft2,tg:FilterCount(Card.IsLocation,nil,LOCATION_EXTRA))
ft2=math.min(ft2,aux.CheckSummonGate(tp) or ft2)
while true do
local ct1=tg:FilterCount(Card.IsLocation,nil,LOCATION_HAND)
local ct2=tg:FilterCount(Card.IsLocation,nil,LOCATION_EXTRA)
local ct=ft
if ct1>ft1 then ct=math.min(ct,ft1) end
if ct2>ft2 then ct=math.min(ct,ft2) end
local loc=0
if ft1>0 then loc=loc+LOCATION_HAND end
if ft2>0 then loc=loc+LOCATION_EXTRA end
local g=tg:Filter(Card.IsLocation,sg,loc)
if #g==0 or ft==0 then break end
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
local tc=Group.SelectUnselect(g,sg,tp,#sg>0,Duel.IsSummonCancelable())
if not tc then break end
if sg:IsContains(tc) then
sg:RemoveCard(tc)
if tc:IsLocation(LOCATION_HAND) then
ft1=ft1+1
else
ft2=ft2+1
end
ft=ft+1
else
sg:AddCard(tc)
if c:IsHasEffect(511007000)~=nil or rpz:IsHasEffect(511007000)~=nil then
if not Pendulum.Filter(tc,e,tp,lscale,rscale) then
local pg=sg:Filter(aux.TRUE,tc)
local ct0,ct3,ct4=#pg,pg:FilterCount(Card.IsLocation,nil,LOCATION_HAND),pg:FilterCount(Card.IsLocation,nil,LOCATION_EXTRA)
sg:Sub(pg)
ft1=ft1+ct3
ft2=ft2+ct4
ft=ft+ct0
else
local pg=sg:Filter(aux.NOT(Pendulum.Filter),nil,e,tp,lscale,rscale)
sg:Sub(pg)
if #pg>0 then
if pg:GetFirst():IsLocation(LOCATION_HAND) then
ft1=ft1+1
else
ft2=ft2+1
end
ft=ft+1
end
end
end
if tc:IsLocation(LOCATION_HAND) then
ft1=ft1-1
else
ft2=ft2-1
end
ft=ft-1
end
end
if #sg>0 then
if not inchain then
Duel.RegisterFlagEffect(tp,10000000,RESET_PHASE+PHASE_END+RESET_SELF_TURN,0,1)
end
Duel.HintSelection(Group.FromCards(c),true)
Duel.HintSelection(Group.FromCards(rpz),true)
end
end
end