name: Altergeist
card_text: When this card is Normal Summoned: You can Set 1 "Altergeist" Trap directly from your Deck. You can target 1 "Altergeist" card you control and 1 "Altergeist" monster in your GY; send that card on the field to the GY, and if you do, Special Summon that other monster from your GY. You can only use this effect of "Altergeist Marionetter" once per turn.
external_ids: local SETCARD_ID_0 = 0x103 --Altergeist
  
lua: local SETCARD_ID_0 = 0x103 --Altergeist
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--set
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetTarget(s.settg)
  	e1:SetOperation(s.setop)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOGRAVE + CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.setfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_TRAP) 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 g = Duel.SelectMatchingCard(tp, s.setfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SSet(tp, g:GetFirst())
  	end
  end
  function s.thfilter1(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGrave()
  end
  function s.thfilter2(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGrave() and c:GetSequence() < 5
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if chk == 0 then
  		local b = false
  		if ft > 0 then
  			b = Duel.IsExistingTarget(s.thfilter1, tp, LOCATION_ONFIELD, 0, 1, nil)
  		else
  			b = Duel.IsExistingTarget(s.thfilter2, tp, LOCATION_MZONE, 0, 1, nil)
  		end
  		return b and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	local g1 = nil
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	if ft > 0 then
  		g1 = Duel.SelectTarget(tp, s.thfilter1, tp, LOCATION_ONFIELD, 0, 1, 1, nil)
  	else
  		g1 = Duel.SelectTarget(tp, s.thfilter2, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g2 = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, g1, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g2, 1, 0, 0)
  	e:SetLabelObject(g1:GetFirst())
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc1, tc2 = Duel.GetFirstTarget()
  	if tc1 ~= e:GetLabelObject() then
  		tc1, tc2 = tc2, tc1
  	end
  	if tc1:IsRelateToEffect(e) and Duel.SendtoGrave(tc1, REASON_EFFECT) > 0 and tc1:IsLocation(LOCATION_GRAVE) and tc2:IsRelateToEffect(e) and aux.NecroValleyFilter()(tc2) then
  		Duel.SpecialSummon(tc2, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Altergeist
  When this card is Normal Summoned: You can Set 1 "Altergeist" Trap directly from your Deck. You can target 1 "Altergeist" card you control and 1 "Altergeist" monster in your GY; send that card on the field to the GY, and if you do, Special Summon that other monster from your GY. You can only use this effect of "Altergeist Marionetter" once per turn.
target_text: -- Altergeist
  local SETCARD_ID_0 = 0x103 --Altergeist
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--set
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetTarget(s.settg)
  	e1:SetOperation(s.setop)
  	c:RegisterEffect(e1)
  	--spsummon
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOGRAVE + CATEGORY_SPECIAL_SUMMON)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetTarget(s.sptg)
  	e2:SetOperation(s.spop)
  	c:RegisterEffect(e2)
  end
  function s.setfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_TRAP) 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 g = Duel.SelectMatchingCard(tp, s.setfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SSet(tp, g:GetFirst())
  	end
  end
  function s.thfilter1(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGrave()
  end
  function s.thfilter2(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsAbleToGrave() and c:GetSequence() < 5
  end
  function s.spfilter(c, e, tp)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	if chk == 0 then
  		local b = false
  		if ft > 0 then
  			b = Duel.IsExistingTarget(s.thfilter1, tp, LOCATION_ONFIELD, 0, 1, nil)
  		else
  			b = Duel.IsExistingTarget(s.thfilter2, tp, LOCATION_MZONE, 0, 1, nil)
  		end
  		return b and Duel.IsExistingTarget(s.spfilter, tp, LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	local g1 = nil
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	if ft > 0 then
  		g1 = Duel.SelectTarget(tp, s.thfilter1, tp, LOCATION_ONFIELD, 0, 1, 1, nil)
  	else
  		g1 = Duel.SelectTarget(tp, s.thfilter2, tp, LOCATION_MZONE, 0, 1, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g2 = Duel.SelectTarget(tp, s.spfilter, tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, g1, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g2, 1, 0, 0)
  	e:SetLabelObject(g1:GetFirst())
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local tc1, tc2 = Duel.GetFirstTarget()
  	if tc1 ~= e:GetLabelObject() then
  		tc1, tc2 = tc2, tc1
  	end
  	if tc1:IsRelateToEffect(e) and Duel.SendtoGrave(tc1, REASON_EFFECT) > 0 and tc1:IsLocation(LOCATION_GRAVE) and tc2:IsRelateToEffect(e) and aux.NecroValleyFilter()(tc2) then
  		Duel.SpecialSummon(tc2, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  