name: Dinomorphia
card_text: When your opponent activates a Spell/Trap Card, while you control a "Dinomorphia" monster: Pay half your LP; negate the activation, and if you do, destroy that card, then, destroy 1 "Dinomorphia" monster you control. During damage calculation, while your LP are 2000 or less, if you would take battle damage: You can banish this card from your GY; you take no battle damage from that battle. You can only activate 1 "Dinomorphia Sonic" per turn.
external_ids: local SETCARD_ID_0 = 0x173 --Dinomorphia
  
lua: local SETCARD_ID_0 = 0x173 --Dinomorphia
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--negate damage
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_PRE_DAMAGE_CALCULATE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCondition(s.damcon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetOperation(s.damop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsFaceup()
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil) and re:IsHasType(EFFECT_TYPE_ACTIVATE) and Duel.IsChainNegatable(ev) and rp == 1 - tp
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.PayLPCost(tp, math.floor(Duel.GetLP(tp) / 2))
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.Destroy(g, REASON_EFFECT)
  		end
  	end
  end
  function s.damcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetLP(tp) <= 2000 and Duel.GetBattleDamage(tp) > 0
  end
  function s.damop(e, tp, eg, ep, ev, re, r, rp)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_AVOID_BATTLE_DAMAGE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetTargetRange(1, 0)
  	e1:SetReset(RESET_PHASE + PHASE_DAMAGE)
  	Duel.RegisterEffect(e1, tp)
  end
  
  
source_text: Dinomorphia
  When your opponent activates a Spell/Trap Card, while you control a "Dinomorphia" monster: Pay half your LP; negate the activation, and if you do, destroy that card, then, destroy 1 "Dinomorphia" monster you control. During damage calculation, while your LP are 2000 or less, if you would take battle damage: You can banish this card from your GY; you take no battle damage from that battle. You can only activate 1 "Dinomorphia Sonic" per turn.
target_text: -- Dinomorphia
  local SETCARD_ID_0 = 0x173 --Dinomorphia
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_NEGATE + CATEGORY_DESTROY)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_CHAINING)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.operation)
  	c:RegisterEffect(e1)
  	--negate damage
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_PRE_DAMAGE_CALCULATE)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCondition(s.damcon)
  	e2:SetCost(aux.bfgcost)
  	e2:SetOperation(s.damop)
  	c:RegisterEffect(e2)
  end
  function s.cfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsFaceup()
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil) and re:IsHasType(EFFECT_TYPE_ACTIVATE) and Duel.IsChainNegatable(ev) and rp == 1 - tp
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.PayLPCost(tp, math.floor(Duel.GetLP(tp) / 2))
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_NEGATE, eg, 1, 0, 0)
  	if re:GetHandler():IsDestructable() and re:GetHandler():IsRelateToEffect(re) then
  		Duel.SetOperationInfo(0, CATEGORY_DESTROY, eg, 1, 0, 0)
  	end
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.NegateActivation(ev) and re:GetHandler():IsRelateToEffect(re) then
  		Duel.Destroy(eg, REASON_EFFECT)
  		Duel.BreakEffect()
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local g = Duel.SelectMatchingCard(tp, s.cfilter, tp, LOCATION_MZONE, 0, 1, 1, nil)
  		if g:GetCount() > 0 then
  			Duel.HintSelection(g)
  			Duel.Destroy(g, REASON_EFFECT)
  		end
  	end
  end
  function s.damcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetLP(tp) <= 2000 and Duel.GetBattleDamage(tp) > 0
  end
  function s.damop(e, tp, eg, ep, ev, re, r, rp)
  	local e1 = Effect.CreateEffect(e:GetHandler())
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_AVOID_BATTLE_DAMAGE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetTargetRange(1, 0)
  	e1:SetReset(RESET_PHASE + PHASE_DAMAGE)
  	Duel.RegisterEffect(e1, tp)
  end
  
  