name: Marincess
card_text: During damage calculation, if a monster battles another monster (Quick Effect): You can send 1 other "Marincess" monster from your hand to the GY; Special Summon this card from your hand, and if you do, the damage you take from that battle is halved. At the start of the Damage Step, if an opponent's monster attacks: You can banish this card from your GY; this turn, you take no battle damage that is less than or equal to the total Link Rating of the "Marincess" Link Monsters in your GY x 1000. You can only use each effect of "Marincess Crown Tail" once per turn.
external_ids: local SETCARD_ID_0 = 0x12b --Marincess
  
lua: local SETCARD_ID_0 = 0x12b --Marincess
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_PRE_DAMAGE_CALCULATE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetCost(s.spcost)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--battle damage
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_START)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.damcon1)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.damtg1)
  	e2:SetOperation(s.damop1)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local d = Duel.GetAttackTarget()
  	return a and d and a:IsRelateToBattle() and d:IsRelateToBattle()
  end
  function s.costfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToGraveAsCost()
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_HAND, 0, 1, c)
  	end
  	Duel.DiscardHand(tp, s.costfilter, 1, 1, REASON_COST, c)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetCode(EFFECT_CHANGE_BATTLE_DAMAGE)
  		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e1:SetTargetRange(1, 0)
  		e1:SetValue(HALF_DAMAGE)
  		e1:SetReset(RESET_PHASE + PHASE_DAMAGE)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.damcon1(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	return a and a:IsControler(1 - tp)
  end
  function s.damfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_LINK)
  end
  function s.damtg1(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.damfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  end
  function s.damop1(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.damfilter, tp, LOCATION_GRAVE, 0, nil)
  	local ct = g:GetSum(Card.GetLink) * 1000
  	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:SetLabel(ct)
  	e1:SetCondition(s.valcon)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.valcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetBattleDamage(tp) <= e:GetLabel()
  end
  
  
source_text: Marincess
  During damage calculation, if a monster battles another monster (Quick Effect): You can send 1 other "Marincess" monster from your hand to the GY; Special Summon this card from your hand, and if you do, the damage you take from that battle is halved. At the start of the Damage Step, if an opponent's monster attacks: You can banish this card from your GY; this turn, you take no battle damage that is less than or equal to the total Link Rating of the "Marincess" Link Monsters in your GY x 1000. You can only use each effect of "Marincess Crown Tail" once per turn.
target_text: -- Marincess
  local SETCARD_ID_0 = 0x12b --Marincess
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_PRE_DAMAGE_CALCULATE)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCountLimit(1, id)
  	e1:SetCondition(s.spcon)
  	e1:SetCost(s.spcost)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	c:RegisterEffect(e1)
  	--battle damage
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_START)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCondition(s.damcon1)
  	e2:SetCost(aux.bfgcost)
  	e2:SetTarget(s.damtg1)
  	e2:SetOperation(s.damop1)
  	c:RegisterEffect(e2)
  end
  function s.spcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local d = Duel.GetAttackTarget()
  	return a and d and a:IsRelateToBattle() and d:IsRelateToBattle()
  end
  function s.costfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToGraveAsCost()
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	local c = e:GetHandler()
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.costfilter, tp, LOCATION_HAND, 0, 1, c)
  	end
  	Duel.DiscardHand(tp, s.costfilter, 1, 1, REASON_COST, c)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsCanBeSpecialSummoned(e, 0, tp, false, false) and Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, e:GetHandler(), 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) and Duel.SpecialSummon(c, 0, tp, tp, false, false, POS_FACEUP) > 0 then
  		local e1 = Effect.CreateEffect(c)
  		e1:SetType(EFFECT_TYPE_FIELD)
  		e1:SetCode(EFFECT_CHANGE_BATTLE_DAMAGE)
  		e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  		e1:SetTargetRange(1, 0)
  		e1:SetValue(HALF_DAMAGE)
  		e1:SetReset(RESET_PHASE + PHASE_DAMAGE)
  		Duel.RegisterEffect(e1, tp)
  	end
  end
  function s.damcon1(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	return a and a:IsControler(1 - tp)
  end
  function s.damfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_LINK)
  end
  function s.damtg1(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.damfilter, tp, LOCATION_GRAVE, 0, 1, nil)
  	end
  end
  function s.damop1(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.damfilter, tp, LOCATION_GRAVE, 0, nil)
  	local ct = g:GetSum(Card.GetLink) * 1000
  	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:SetLabel(ct)
  	e1:SetCondition(s.valcon)
  	e1:SetReset(RESET_PHASE + PHASE_END)
  	Duel.RegisterEffect(e1, tp)
  end
  function s.valcon(e)
  	local tp = e:GetHandlerPlayer()
  	return Duel.GetBattleDamage(tp) <= e:GetLabel()
  end
  
  