name: Necrovalley
card_text: While a "Gravekeeper's" monster(s) and "Necrovalley" are both on the field, monsters your opponent controls lose 500 ATK/DEF. Once per turn, during the Main Phase, if you control no card in your Field Zone: You can activate 1 "Necrovalley" directly from your hand or GY. If this card in your possession is destroyed by an opponent's effect and sent to your GY: You can Set 1 "Necrovalley" Spell/Trap directly from your Deck, except "Necrovalley Temple".
external_ids: local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
  local CARD_ID_1 = 47355498 --Necrovalley
  local SETCARD_ID_0 = 0x91 --Necrovalley
  local SETCARD_ID_1 = 0x2e --Gravekeeper's
  
lua: local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
  local CARD_ID_1 = 47355498 --Necrovalley
  local SETCARD_ID_0 = 0x91 --Necrovalley
  local SETCARD_ID_1 = 0x2e --Gravekeeper's
  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)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetHintTiming(TIMING_DAMAGE_STEP)
  	e1:SetCondition(aux.dscon)
  	c:RegisterEffect(e1)
  	--atk down
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_UPDATE_ATTACK)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetTargetRange(0, LOCATION_MZONE)
  	e2:SetCondition(s.atkcon)
  	e2:SetValue(-500)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_UPDATE_DEFENSE)
  	c:RegisterEffect(e3)
  	--activate Necrovalley
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetType(EFFECT_TYPE_QUICK_O)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCode(EVENT_FREE_CHAIN)
  	e4:SetCountLimit(1)
  	e4:SetCondition(s.accon)
  	e4:SetTarget(s.actg)
  	e4:SetOperation(s.acop)
  	c:RegisterEffect(e4)
  	--set
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 1))
  	e5:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e5:SetCode(EVENT_TO_GRAVE)
  	e5:SetProperty(EFFECT_FLAG_DELAY)
  	e5:SetCondition(s.setcon)
  	e5:SetTarget(s.settg)
  	e5:SetOperation(s.setop)
  	c:RegisterEffect(e5)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_1)
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil) and Duel.IsEnvironment(CARD_ID_1)
  end
  function s.accon(e, tp, eg, ep, ev, re, r, rp)
  	return (Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2) and not Duel.IsExistingMatchingCard(aux.TRUE, tp, LOCATION_FZONE, 0, 1, nil)
  end
  function s.filter(c, tp)
  	return c:IsCode(CARD_ID_1) and c:GetActivateEffect() and c:GetActivateEffect():IsActivatable(tp, true, true)
  end
  function s.actg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, tp)
  	end
  end
  function s.acop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOFIELD)
  	local tc = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, tp):GetFirst()
  	if tc then
  		local fc = Duel.GetFieldCard(tp, LOCATION_FZONE, 0)
  		if fc then
  			Duel.SendtoGrave(fc, REASON_RULE)
  			Duel.BreakEffect()
  		end
  		Duel.MoveToField(tc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
  		local te = tc:GetActivateEffect()
  		te:UseCountLimit(tp, 1, true)
  		local tep = tc:GetControler()
  		local cost = te:GetCost()
  		if cost then
  			cost(te, tep, eg, ep, ev, re, r, rp, 1)
  		end
  		Duel.RaiseEvent(tc, CARD_ID_0, te, 0, tp, tp, Duel.GetCurrentChain())
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return rp == 1 - tp and c:IsReason(REASON_DESTROY) and c:IsReason(REASON_EFFECT) and c:IsPreviousControler(tp)
  end
  function s.setfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and not c:IsCode(id) and c:IsSSetable()
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.setfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  	local tc = Duel.SelectMatchingCard(tp, s.setfilter, tp, LOCATION_DECK, 0, 1, 1, nil):GetFirst()
  	if tc then
  		Duel.SSet(tp, tc)
  	end
  end
  
  
source_text: Necrovalley
  While a "Gravekeeper's" monster(s) and "Necrovalley" are both on the field, monsters your opponent controls lose 500 ATK/DEF. Once per turn, during the Main Phase, if you control no card in your Field Zone: You can activate 1 "Necrovalley" directly from your hand or GY. If this card in your possession is destroyed by an opponent's effect and sent to your GY: You can Set 1 "Necrovalley" Spell/Trap directly from your Deck, except "Necrovalley Temple".
target_text: -- Necrovalley
  local CARD_ID_0 = 4179255 --Ancient Pixie Dragon
  local CARD_ID_1 = 47355498 --Necrovalley
  local SETCARD_ID_0 = 0x91 --Necrovalley
  local SETCARD_ID_1 = 0x2e --Gravekeeper's
  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)
  	e1:SetProperty(EFFECT_FLAG_DAMAGE_STEP)
  	e1:SetHintTiming(TIMING_DAMAGE_STEP)
  	e1:SetCondition(aux.dscon)
  	c:RegisterEffect(e1)
  	--atk down
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(EFFECT_UPDATE_ATTACK)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetTargetRange(0, LOCATION_MZONE)
  	e2:SetCondition(s.atkcon)
  	e2:SetValue(-500)
  	c:RegisterEffect(e2)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_UPDATE_DEFENSE)
  	c:RegisterEffect(e3)
  	--activate Necrovalley
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetType(EFFECT_TYPE_QUICK_O)
  	e4:SetRange(LOCATION_SZONE)
  	e4:SetCode(EVENT_FREE_CHAIN)
  	e4:SetCountLimit(1)
  	e4:SetCondition(s.accon)
  	e4:SetTarget(s.actg)
  	e4:SetOperation(s.acop)
  	c:RegisterEffect(e4)
  	--set
  	local e5 = Effect.CreateEffect(c)
  	e5:SetDescription(aux.Stringid(id, 1))
  	e5:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e5:SetCode(EVENT_TO_GRAVE)
  	e5:SetProperty(EFFECT_FLAG_DELAY)
  	e5:SetCondition(s.setcon)
  	e5:SetTarget(s.settg)
  	e5:SetOperation(s.setop)
  	c:RegisterEffect(e5)
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_1)
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, LOCATION_MZONE, 1, nil) and Duel.IsEnvironment(CARD_ID_1)
  end
  function s.accon(e, tp, eg, ep, ev, re, r, rp)
  	return (Duel.GetCurrentPhase() == PHASE_MAIN1 or Duel.GetCurrentPhase() == PHASE_MAIN2) and not Duel.IsExistingMatchingCard(aux.TRUE, tp, LOCATION_FZONE, 0, 1, nil)
  end
  function s.filter(c, tp)
  	return c:IsCode(CARD_ID_1) and c:GetActivateEffect() and c:GetActivateEffect():IsActivatable(tp, true, true)
  end
  function s.actg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, nil, tp)
  	end
  end
  function s.acop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOFIELD)
  	local tc = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_HAND + LOCATION_GRAVE, 0, 1, 1, nil, tp):GetFirst()
  	if tc then
  		local fc = Duel.GetFieldCard(tp, LOCATION_FZONE, 0)
  		if fc then
  			Duel.SendtoGrave(fc, REASON_RULE)
  			Duel.BreakEffect()
  		end
  		Duel.MoveToField(tc, tp, tp, LOCATION_FZONE, POS_FACEUP, true)
  		local te = tc:GetActivateEffect()
  		te:UseCountLimit(tp, 1, true)
  		local tep = tc:GetControler()
  		local cost = te:GetCost()
  		if cost then
  			cost(te, tep, eg, ep, ev, re, r, rp, 1)
  		end
  		Duel.RaiseEvent(tc, CARD_ID_0, te, 0, tp, tp, Duel.GetCurrentChain())
  	end
  end
  function s.setcon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return rp == 1 - tp and c:IsReason(REASON_DESTROY) and c:IsReason(REASON_EFFECT) and c:IsPreviousControler(tp)
  end
  function s.setfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and not c:IsCode(id) and c:IsSSetable()
  end
  function s.settg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.setfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  end
  function s.setop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SET)
  	local tc = Duel.SelectMatchingCard(tp, s.setfilter, tp, LOCATION_DECK, 0, 1, 1, nil):GetFirst()
  	if tc then
  		Duel.SSet(tp, tc)
  	end
  end
  
  