--Carpathian Castle
function c99909002.initial_effect(c)
--Activate
local e1=Effect.CreateEffect(c)
e1:SetType(EFFECT_TYPE_ACTIVATE)
e1:SetCode(EVENT_FREE_CHAIN)
c:RegisterEffect(e1)
--add counter
local e2=Effect.CreateEffect(c)
e2:SetDescription(aux.Stringid(99909002,0))
e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F+EFFECT_TYPE_CONTINUOUS)
e2:SetCategory(CATEGORY_COUNTER)
e2:SetProperty(EFFECT_FLAG_DELAY)
e2:SetRange(LOCATION_SZONE)
e2:SetCode(EVENT_BATTLE_DESTROYING)
e2:SetTarget(c99909002.tar)
e2:SetCondition(c99909002.con)
e2:SetOperation(c99909002.op)
c:RegisterEffect(e2)
--Remove Counter and Summon
local e3=Effect.CreateEffect(c)
e3:SetDescription(aux.Stringid(99909002,1))
e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
e3:SetRange(LOCATION_SZONE)
e3:SetType(EFFECT_TYPE_ACTIVATION)
e3:SetCode(EVENT_REMOVE_COUNTER)
e3:SetCost(c99909002.scost)
e3:SetOperation(c99909002.sop)
e3:SetTarget(c99909002.star)
c:RegisterEffect(e3)
end
function c99909002.tar(e,tp,eg,ep,ev,re,r,rp,chk)
if chk==0 then return true end
local ec=eg:GetFirst()
local bc=ec:GetBattleTarget()
return e:GetHandler()==eg:GetFirst() and ec:IsControler(tp)
and bc:IsLocation(LOCATION_GRAVE) and bc:IsReason(REASON_BATTLE)
end
function c99909002.con(e,tp,eg,ep,ev,re,r,rp)
local c=e:GetHandler()
local rc=eg:GetFirst()
return rc:IsRelateToBattle() and rc:IsSetCard(0x29A) and rc:IsFaceup() and rc:IsControler(tp)
end
function c99909002.op(e,tp,eg,ep,ev,re,r,rp)
e:GetHandler():AddCounter(0x666,1)
end
function c99909002.filter(c,e,tp)
return c:IsCode(99909001) and c:IsCanBeSpecialSummoned(e,0,tp,true,false)
end
function c99909002.scost(e,tp,eg,ep,ev,re,r,rp,chk)
if chk==0 then return e:GetHandler():IsCanRemoveCounter(tp,0x666,3,REASON_COST) end
e:GetHandler():RemoveCounter(tp,0x666,3,REASON_COST)
end
function c99909002.star(e,tp,eg,ep,ev,re,r,rp,chk)
if chk==0 then return Duel.GetLocationCount(tp,LOCATION_MZONE)<=0
and Duel.IsExistingMatchingCard(c99909002.filter,tp,LOCATION_EXTRA,0,1,nil,e,tp) end
Duel.SetOperationInfo(0,CATEGORY_SPECIAL_SUMMON,nil,1,tp,LOCATION_EXTRA)
end
function c99909002.sop(e,tp,eg,ep,ev,re,r,rp)
if Duel.GetLocationCount(tp,LOCATION_MZONE)>0 then return end
Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_SPSUMMON)
local g=Duel.SelectMatchingCard(tp,c99909002.filter,tp,LOCATION_EXTRA,0,1,1,nil,e,tp)
if g:GetCount()>0 then
Duel.SpecialSummon(g,0,tp,tp,true,false,POS_FACEUP_ATTACK)
g:GetFirst():CompleteProcedure()
end
end