Skip to content

Commit 851f959

Browse files
authored
"Take One Chance" fixes
- Fixed some script errors that occurred with certain cards. - Added handling for HOPT restrictions. - Added handling for cards that can only be activated in certain zones. - Properly check for available zones both for the above case and for when this card is being activated from the hand. - Also raise the relevant events for activating an effect, not just for resolving one (e.g. now if a Spell is selected Spell Counters will be placed on cards looking for Spell Card activations). - This card shouldn't keep the randomly selected card's property after resolution. - Highlight the randomly selected card. - Make it unable to select a copy of itself to prevent infinite loops. - Should be negated by "Necrovalley".
1 parent dcf0e27 commit 851f959

File tree

1 file changed

+114
-131
lines changed

1 file changed

+114
-131
lines changed

unofficial/c511001499.lua

Lines changed: 114 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -4,154 +4,137 @@ local s,id=GetID()
44
function s.initial_effect(c)
55
--Activate
66
local e1=Effect.CreateEffect(c)
7+
e1:SetDescription(aux.Stringid(id,0))
78
e1:SetType(EFFECT_TYPE_ACTIVATE)
89
e1:SetCode(EVENT_FREE_CHAIN)
10+
e1:SetHintTiming(TIMING_CHAIN_END,TIMING_CHAIN_END|TIMING_STANDBY_PHASE|TIMING_MAIN_END|TIMINGS_CHECK_MONSTER_E)
911
e1:SetTarget(s.target)
1012
e1:SetOperation(s.activate)
1113
c:RegisterEffect(e1)
12-
local e2=Effect.CreateEffect(c)
13-
e2:SetType(EFFECT_TYPE_SINGLE)
14-
e2:SetProperty(EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_SET_AVAILABLE+EFFECT_FLAG_CANNOT_DISABLE)
15-
e2:SetCode(511001283)
16-
c:RegisterEffect(e2)
1714
end
18-
function s.cfilter(c,tp,eg,ep,ev,re,r,rp,chain)
19-
return not c:IsHasEffect(511001283) and s.filter(c,tp,eg,ep,ev,re,r,rp,chain)
20-
end
21-
function s.filter(c,tp,eg,ep,ev,re,r,rp,chain)
22-
if not c:IsType(TYPE_FIELD) and Duel.GetLocationCount(tp,LOCATION_SZONE)<=0 then return false end
15+
function s.tgfilter(c,tp,szone_adjustment)
16+
if not c:IsSpellTrap() or c:IsCode(id) then return false end
2317
local te=c:GetActivateEffect()
24-
if not te then return false end
18+
--Check for HOPT
19+
if not (te and te:CheckCountLimit(tp)) then return false end
20+
--Check for "cannot activate" restrictions
2521
if c:IsHasEffect(EFFECT_CANNOT_TRIGGER) then return false end
26-
local pre={Duel.GetPlayerEffect(tp,EFFECT_CANNOT_ACTIVATE)}
27-
if pre[1] then
28-
for i,eff in ipairs(pre) do
29-
local prev=eff:GetValue()
30-
if type(prev)~='function' or prev(eff,te,tp) then return false end
22+
local cannot_act_restrictions={Duel.GetPlayerEffect(tp,EFFECT_CANNOT_ACTIVATE)}
23+
if cannot_act_restrictions[1] then
24+
for _,eff in ipairs(cannot_act_restrictions) do
25+
local eff_value=eff:GetValue()
26+
if type(eff_value)~='function' or eff_value(eff,te,tp) then return false end
3127
end
3228
end
33-
local condition=te:GetCondition()
34-
local cost=te:GetCost()
35-
local target=te:GetTarget()
36-
if te:GetCode()==EVENT_CHAINING then
37-
if chain<=0 then return false end
38-
local te2=Duel.GetChainInfo(chain,CHAININFO_TRIGGERING_EFFECT)
39-
local tc=te2:GetHandler()
40-
local g=Group.FromCards(tc)
41-
local p=tc:GetControler()
42-
return (not condition or condition(e,tp,g,p,chain,te2,REASON_EFFECT,p)) and (not cost or cost(e,tp,g,p,chain,te2,REASON_EFFECT,p,0))
43-
and (not target or target(e,tp,g,p,chain,te2,REASON_EFFECT,p,0))
44-
elseif te:GetCode()==EVENT_FREE_CHAIN then
45-
return (not condition or condition(e,tp,eg,ep,ev,re,r,rp)) and (not cost or cost(e,tp,eg,ep,ev,re,r,rp,0))
46-
and (not target or target(e,tp,eg,ep,ev,re,r,rp,0))
47-
else
48-
local res,teg,tep,tev,tre,tr,trp=Duel.CheckEvent(te:GetCode(),true)
49-
return res and (not condition or condition(e,tp,teg,tep,tev,tre,tr,trp)) and (not cost or cost(e,tp,teg,tep,tev,tre,tr,trp,0))
50-
and (not target or target(e,tp,teg,tep,tev,tre,tr,trp,0))
29+
--Check using the returned values of "Duel.CheckEvent" only for the activation check
30+
if not Duel.IsChainSolving() then
31+
local te_effect_code=te:GetCode()
32+
local res,ceg,cep,cev,cre,cr,crp=Duel.CheckEvent(te_effect_code,true)
33+
--If it's "EVENT_FREE_CHAIN" then "res" will always be false
34+
if not res and te_effect_code~=EVENT_FREE_CHAIN then return false end
35+
local condition=te:GetCondition()
36+
local cost=te:GetCost()
37+
local target=te:GetTarget()
38+
--Check if the card can be activated
39+
if condition and not condition(te,tp,ceg,cep,cev,cre,cr,crp) then return false end
40+
if cost and not cost(te,tp,ceg,cep,cev,cre,cr,crp,0) then return false end
41+
if target and not target(te,tp,ceg,cep,cev,cre,cr,crp,0) then return false end
42+
end
43+
--Don't need to check for zones for a Field Card
44+
if c:IsType(TYPE_FIELD) then return true end
45+
--Check for cards that can only be activated in certain zones
46+
local zones=0xff
47+
if te:IsHasProperty(EFFECT_FLAG_LIMIT_ZONE) then
48+
zones=te:GetValue()(te,tp,eg,ep,ev,re,r,rp)
5149
end
50+
return Duel.GetLocationCount(tp,LOCATION_SZONE,tp,LOCATION_REASON_TOFIELD,zones)-szone_adjustment>0
5251
end
5352
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
54-
local chain=Duel.GetCurrentChain()
55-
if chk==0 then return Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_GRAVE,0,1,e:GetHandler(),tp,eg,ep,ev,re,r,rp,chain)
56-
and Duel.GetLocationCount(tp,LOCATION_SZONE)>0 end
53+
if chk==0 then
54+
local c=e:GetHandler()
55+
local szone_adjustment=c:IsLocation(LOCATION_SZONE) and 0 or 1
56+
return Duel.IsExistingMatchingCard(s.tgfilter,tp,LOCATION_GRAVE,0,1,c,tp,szone_adjustment)
57+
end
58+
Duel.SetOperationInfo(0,CATEGORY_LEAVE_GRAVE,nil,1,tp,0)
5759
end
5860
function s.activate(e,tp,eg,ep,ev,re,r,rp)
59-
local chain=Duel.GetCurrentChain()-1
60-
if Duel.GetLocationCount(tp,LOCATION_SZONE)<=0 then return end
61-
local sg=Duel.GetMatchingGroup(nil,tp,LOCATION_GRAVE,0,nil)
62-
local g=sg:RandomSelect(tp,1)
63-
if #g>0 then
64-
local tc=g:GetFirst()
65-
local tpe=tc:GetType()
66-
local te=tc:GetActivateEffect()
67-
if not te then return end
68-
if not Duel.IsExistingMatchingCard(s.cfilter,tp,LOCATION_GRAVE,0,1,e:GetHandler(),tp,eg,ep,ev,re,r,rp,chain) then return end
69-
local con=te:GetCondition()
70-
local co=te:GetCost()
71-
local tg=te:GetTarget()
72-
local op=te:GetOperation()
73-
if s.filter(tc,tp,eg,ep,ev,re,r,rp,chain) then
74-
Duel.ClearTargetCard()
75-
e:SetCategory(te:GetCategory())
76-
e:SetProperty(te:GetProperty())
77-
local loc=LOCATION_SZONE
78-
if (tpe&TYPE_FIELD)~=0 then
79-
loc=LOCATION_FZONE
80-
local fc=Duel.GetFieldCard(1-tp,LOCATION_FZONE,0)
81-
if Duel.IsDuelType(DUEL_1_FIELD) then
82-
if fc then Duel.Destroy(fc,REASON_RULE) end
83-
fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
84-
if fc and Duel.Destroy(fc,REASON_RULE)==0 then Duel.SendtoGrave(tc,REASON_RULE) end
85-
else
86-
fc=Duel.GetFieldCard(tp,LOCATION_FZONE,0)
87-
if fc and Duel.SendtoGrave(fc,REASON_RULE)==0 then Duel.SendtoGrave(tc,REASON_RULE) end
88-
end
89-
end
90-
Duel.MoveToField(tc,tp,tp,loc,POS_FACEUP,true)
91-
if (tpe&TYPE_TRAP+TYPE_FIELD)==TYPE_TRAP+TYPE_FIELD then
92-
Duel.MoveSequence(tc,5)
93-
end
94-
Duel.Hint(HINT_CARD,0,tc:GetCode())
95-
tc:CreateEffectRelation(te)
96-
if (tpe&TYPE_EQUIP+TYPE_CONTINUOUS+TYPE_FIELD)==0 and not tc:IsHasEffect(EFFECT_REMAIN_FIELD) then
97-
tc:CancelToGrave(false)
98-
end
99-
if te:GetCode()==EVENT_CHAINING then
100-
local te2=Duel.GetChainInfo(chain,CHAININFO_TRIGGERING_EFFECT)
101-
local tc=te2:GetHandler()
102-
local g=Group.FromCards(tc)
103-
local p=tc:GetControler()
104-
if co then co(e,tp,g,p,chain,te2,REASON_EFFECT,p,1) end
105-
if tg then tg(e,tp,g,p,chain,te2,REASON_EFFECT,p,1) end
106-
elseif te:GetCode()==EVENT_FREE_CHAIN then
107-
if co then co(e,tp,eg,ep,ev,re,r,rp,1) end
108-
if tg then tg(e,tp,eg,ep,ev,re,r,rp,1) end
109-
else
110-
local res,teg,tep,tev,tre,tr,trp=Duel.CheckEvent(te:GetCode(),true)
111-
if co then co(e,tp,teg,tep,tev,tre,tr,trp,1) end
112-
if tg then tg(e,tp,teg,tep,tev,tre,tr,trp,1) end
113-
end
114-
local g=Duel.GetChainInfo(0,CHAININFO_TARGET_CARDS)
115-
if g then
116-
local etc=g:GetFirst()
117-
while etc do
118-
etc:CreateEffectRelation(te)
119-
etc=g:GetNext()
120-
end
121-
end
122-
Duel.BreakEffect()
123-
tc:SetStatus(STATUS_ACTIVATED,true)
124-
if not tc:IsDisabled() then
125-
if te:GetCode()==EVENT_CHAINING then
126-
local te2=Duel.GetChainInfo(chain,CHAININFO_TRIGGERING_EFFECT)
127-
local tc=te2:GetHandler()
128-
local g=Group.FromCards(tc)
129-
local p=tc:GetControler()
130-
if op then op(e,tp,g,p,chain,te2,REASON_EFFECT,p) end
131-
elseif te:GetCode()==EVENT_FREE_CHAIN then
132-
if op then op(e,tp,eg,ep,ev,re,r,rp) end
133-
else
134-
local res,teg,tep,tev,tre,tr,trp=Duel.CheckEvent(te:GetCode(),true)
135-
if op then op(e,tp,teg,tep,tev,tre,tr,trp) end
136-
end
137-
else
138-
--insert negated animation here
139-
end
140-
Duel.RaiseEvent(Group.CreateGroup(tc),EVENT_CHAIN_SOLVED,te,0,tp,tp,Duel.GetCurrentChain())
141-
if g and tc:IsType(TYPE_EQUIP) and not tc:GetEquipTarget() then
142-
Duel.Equip(tp,tc,g:GetFirst())
61+
local g=Duel.GetMatchingGroup(nil,tp,LOCATION_GRAVE,0,nil)
62+
if #g==0 then return end
63+
local sc=g:RandomSelect(tp,1):GetFirst()
64+
if not sc then return end
65+
Duel.HintSelection(sc)
66+
if not s.tgfilter(sc,tp,0) then return end
67+
local te=sc:GetActivateEffect()
68+
if not te then return end
69+
local location=sc:IsType(TYPE_FIELD) and LOCATION_FZONE or LOCATION_SZONE
70+
--Adjust "zones" for cards that can only be activated in certain zones
71+
local zones=0xff
72+
if te:IsHasProperty(EFFECT_FLAG_LIMIT_ZONE) then
73+
zones=te:GetValue()(te,tp,eg,ep,ev,re,r,rp)
74+
end
75+
if not Duel.MoveToField(sc,tp,tp,location,POS_FACEUP,true,zones) then return end
76+
--Set activated status, show the card, create relation to itself, and use the effect's count limit (if any)
77+
sc:SetStatus(STATUS_ACTIVATED,true)
78+
Duel.Hint(HINT_CARD,0,sc:GetCode())
79+
sc:CreateEffectRelation(te)
80+
te:UseCountLimit(tp)
81+
--Mark the card as "to be sent to the GY" if it's not a card that remains on the field after activation
82+
if not (sc:IsType(TYPE_FIELD) or sc:IsEquipSpell() or sc:IsContinuousSpellTrap() or sc:IsLinkSpell()
83+
or sc:IsHasEffect(EFFECT_REMAIN_FIELD) or te:GetCost()==aux.RemainFieldCost) then
84+
sc:CancelToGrave(false)
85+
end
86+
local te_effect_code=te:GetCode()
87+
local ceg,cep,cev,cre,cr,crp
88+
if te_effect_code==EVENT_CHAINING then
89+
--If it's "EVENT_CHAINING" then manually grab the previous Chain Link's relevant values cuz "Duel.CheckEvent" won't return the proper values anymore
90+
local trig_chain_link=Duel.GetCurrentChain()-1
91+
local trig_eff,trig_player=Duel.GetChainInfo(trig_chain_link,CHAININFO_TRIGGERING_EFFECT,CHAININFO_TRIGGERING_PLAYER)
92+
local trig_eg=Group.FromCards(trig_eff:GetHandler())
93+
ceg,cep,cev,cre,cr,crp=trig_eg,trig_player,trig_chain_link,trig_eff,0,trig_player
94+
else
95+
--If it's not "EVENT_CHAINING" then use the values returned by "Duel.CheckEvent"
96+
_,ceg,cep,cev,cre,cr,crp=Duel.CheckEvent(te_effect_code,true)
97+
end
98+
--Execute the cost, target, and operation with the designated parameters if the card's effects after it's placed on the field aren't negated
99+
if not sc:IsDisabled() then
100+
local cost=te:GetCost()
101+
local target=te:GetTarget()
102+
local operation=te:GetOperation()
103+
--Set the effect's property so that targeted cards are highlighted normally
104+
e:SetProperty(te:GetProperty())
105+
--Activation procedure
106+
if cost then cost(te,tp,ceg,cep,cev,cre,cr,crp,1) end
107+
if target then target(te,tp,ceg,cep,cev,cre,cr,crp,1) end
108+
--Manually create the effect relation for any targeted cards
109+
local tg=Duel.GetChainInfo(0,CHAININFO_TARGET_CARDS)
110+
if tg then
111+
for tc in tg:Iter() do
112+
tc:CreateEffectRelation(te)
143113
end
114+
end
115+
e:SetProperty(0)
116+
local field_event_eg=Group.FromCards(sc)
117+
local current_chain_link=Duel.GetCurrentChain()
118+
--Manually raise the events for activating it
119+
Duel.RaiseSingleEvent(sc,EVENT_CHAINING,te,0,tp,tp,current_chain_link)
120+
Duel.RaiseEvent(field_event_eg,EVENT_CHAINING,te,0,tp,tp,current_chain_link)
121+
--Break to separate the activation and the resolution
122+
Duel.BreakEffect()
123+
--Manually raise the events for the start of resolution
124+
Duel.RaiseSingleEvent(sc,EVENT_CHAIN_SOLVING,te,0,tp,tp,current_chain_link)
125+
Duel.RaiseEvent(field_event_eg,EVENT_CHAIN_SOLVING,te,0,tp,tp,current_chain_link)
126+
--Resolution of the effect
127+
if operation then operation(te,tp,ceg,cep,cev,cre,cr,crp) end
128+
--Manually raise the events for the end of resolution
129+
Duel.RaiseSingleEvent(sc,EVENT_CHAIN_SOLVED,te,0,tp,tp,current_chain_link)
130+
Duel.RaiseEvent(field_event_eg,EVENT_CHAIN_SOLVED,te,0,tp,tp,current_chain_link)
131+
end
132+
--Manually release the effect relation for this card and any targeted cards
133+
sc:ReleaseEffectRelation(te)
134+
local tg=Duel.GetChainInfo(0,CHAININFO_TARGET_CARDS)
135+
if tg then
136+
for tc in tg:Iter() do
144137
tc:ReleaseEffectRelation(te)
145-
if etc then
146-
etc=g:GetFirst()
147-
while etc do
148-
etc:ReleaseEffectRelation(te)
149-
etc=g:GetNext()
150-
end
151-
end
152-
else
153-
Duel.ConfirmCards(1-tp,g)
154-
Duel.ConfirmCards(tp,g)
155138
end
156139
end
157140
end

0 commit comments

Comments
 (0)