name: Broken
card_text: Activate only when a Defense Position monster you control, whose DEF is higher than its ATK, is destroyed by battle. Special Summon up to 2 monsters with the same name as that monster from your Deck in face-up Defense Position.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_BATTLE_DESTROYED)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_BATTLED)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local t = Duel.GetAttackTarget()
  	if a and a:IsDefensePos() and a:GetDefense() > a:GetAttack() and a:IsStatus(STATUS_BATTLE_DESTROYED) then
  		a:RegisterFlagEffect(id, RESET_PHASE + PHASE_DAMAGE, 0, 1)
  	end
  	if t and t:IsDefensePos() and t:GetDefense() > t:GetAttack() and t:IsStatus(STATUS_BATTLE_DESTROYED) then
  		t:RegisterFlagEffect(id, RESET_PHASE + PHASE_DAMAGE, 0, 1)
  	end
  end
  function s.filter(c, e, tp)
  	if c:GetFlagEffect(id) ~= 0 and c:IsPreviousControler(tp) then
  		e:SetLabel(c:GetCode())
  		return true
  	else
  		return false
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.filter, 1, nil, e, tp)
  end
  function s.spfilter(c, e, tp, code)
  	return c:IsCode(code) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp, e:GetLabel())
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ft = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), 2)
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK, 0, 1, ft, nil, e, tp, e:GetLabel())
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  
  
source_text: Broken
  Activate only when a Defense Position monster you control, whose DEF is higher than its ATK, is destroyed by battle. Special Summon up to 2 monsters with the same name as that monster from your Deck in face-up Defense Position.
target_text: -- Broken
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_BATTLE_DESTROYED)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_BATTLED)
  		ge1:SetOperation(s.checkop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.checkop(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local t = Duel.GetAttackTarget()
  	if a and a:IsDefensePos() and a:GetDefense() > a:GetAttack() and a:IsStatus(STATUS_BATTLE_DESTROYED) then
  		a:RegisterFlagEffect(id, RESET_PHASE + PHASE_DAMAGE, 0, 1)
  	end
  	if t and t:IsDefensePos() and t:GetDefense() > t:GetAttack() and t:IsStatus(STATUS_BATTLE_DESTROYED) then
  		t:RegisterFlagEffect(id, RESET_PHASE + PHASE_DAMAGE, 0, 1)
  	end
  end
  function s.filter(c, e, tp)
  	if c:GetFlagEffect(id) ~= 0 and c:IsPreviousControler(tp) then
  		e:SetLabel(c:GetCode())
  		return true
  	else
  		return false
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.filter, 1, nil, e, tp)
  end
  function s.spfilter(c, e, tp, code)
  	return c:IsCode(code) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false, POS_FACEUP_DEFENSE)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.spfilter, tp, LOCATION_DECK, 0, 1, nil, e, tp, e:GetLabel())
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ft = math.min((Duel.GetLocationCount(tp, LOCATION_MZONE)), 2)
  	if ft <= 0 then
  		return
  	end
  	if Duel.IsPlayerAffectedByEffect(tp, CARD_ID_0) then
  		ft = 1
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, s.spfilter, tp, LOCATION_DECK, 0, 1, ft, nil, e, tp, e:GetLabel())
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, false, false, POS_FACEUP_DEFENSE)
  	end
  end
  
  