name: Battlefield
card_text: During your Main Phase 2, if no monsters battled each other this turn: You can discard 1 card; Set 1 "Battlefield Tragedy" directly from your Deck to your Spell & Trap Zone. You can only use this effect of "Battlefield Tragedy" once per turn. Once per turn, during the End Phase, if any monsters battled each other this turn: The turn player sends 5 cards from the top of their Deck to the GY.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--sset
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.sscon)
  	e2:SetCost(s.sscost)
  	e2:SetTarget(s.sstg)
  	e2:SetOperation(s.ssop)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_DECKDES)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.tgcon)
  	e3:SetTarget(s.tgtg)
  	e3:SetOperation(s.tgop)
  	c:RegisterEffect(e3)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.GlobalEffect()
  		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)
  	if Duel.GetAttackTarget() then
  		Duel.RegisterFlagEffect(0, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  end
  function s.sscon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(0, id) == 0 and Duel.GetCurrentPhase() == PHASE_MAIN2
  end
  function s.sscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.ssfilter(c)
  	return c:IsCode(id) and c:IsSSetable()
  end
  function s.sstg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.ssfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  end
  function s.ssop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  	local g = Duel.SelectMatchingCard(tp, s.ssfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SSet(tp, g)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(0, id) > 0
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, Duel.GetTurnPlayer(), 5)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.DiscardDeck(Duel.GetTurnPlayer(), 5, REASON_EFFECT)
  end
  
  
source_text: Battlefield
  During your Main Phase 2, if no monsters battled each other this turn: You can discard 1 card; Set 1 "Battlefield Tragedy" directly from your Deck to your Spell & Trap Zone. You can only use this effect of "Battlefield Tragedy" once per turn. Once per turn, during the End Phase, if any monsters battled each other this turn: The turn player sends 5 cards from the top of their Deck to the GY.
target_text: -- Battlefield
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--sset
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.sscon)
  	e2:SetCost(s.sscost)
  	e2:SetTarget(s.sstg)
  	e2:SetOperation(s.ssop)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_DECKDES)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCondition(s.tgcon)
  	e3:SetTarget(s.tgtg)
  	e3:SetOperation(s.tgop)
  	c:RegisterEffect(e3)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.GlobalEffect()
  		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)
  	if Duel.GetAttackTarget() then
  		Duel.RegisterFlagEffect(0, id, RESET_PHASE + PHASE_END, 0, 1)
  	end
  end
  function s.sscon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(0, id) == 0 and Duel.GetCurrentPhase() == PHASE_MAIN2
  end
  function s.sscost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(Card.IsDiscardable, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.DiscardHand(tp, Card.IsDiscardable, 1, 1, REASON_COST + REASON_DISCARD)
  end
  function s.ssfilter(c)
  	return c:IsCode(id) and c:IsSSetable()
  end
  function s.sstg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.ssfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  end
  function s.ssop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  	local g = Duel.SelectMatchingCard(tp, s.ssfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SSet(tp, g)
  	end
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(0, id) > 0
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_DECKDES, nil, 0, Duel.GetTurnPlayer(), 5)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.DiscardDeck(Duel.GetTurnPlayer(), 5, REASON_EFFECT)
  end
  
  