name: Plunder
card_text: You can declare 1 Attribute, then target 1 face-up monster your opponent controls; it becomes that Attribute until the end of this turn (even if this card leaves the field), then you can take 1 "Plunder Patroll" monster from your GY, and either shuffle it into the Deck or Special Summon it. You can only use this effect of "Plunder Patroll Booty" once per turn. Once per turn, during the End Phase, if you control no "Plunder Patroll" monsters: Send this card to the GY.
external_ids: local SETCARD_ID_0 = 0x13f --Plunder
  
lua: local SETCARD_ID_0 = 0x13f --Plunder
  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)
  	--attribute
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.atrtg)
  	e2:SetOperation(s.atrop)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOGRAVE)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.tgcon)
  	e3:SetTarget(s.tgtg)
  	e3:SetOperation(s.tgop)
  	c:RegisterEffect(e3)
  end
  function s.atrtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and chkc:IsFaceup()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATTRIBUTE)
  	local att = Duel.AnnounceAttribute(tp, 1, ATTRIBUTE_ALL & ~g:GetFirst():GetAttribute())
  	e:SetLabel(att)
  end
  function s.thfilter(c, e, tp, ft)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and (c:IsAbleToDeck() or (ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)))
  end
  function s.atrop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	local att = e:GetLabel()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and not tc:IsImmuneToEffect(e) and not tc:IsAttribute(att) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_CHANGE_ATTRIBUTE)
  		e1:SetValue(att)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  		if Duel.IsExistingMatchingCard(aux.NecroValleyFilter(s.thfilter), tp, LOCATION_GRAVE, 0, 1, nil, e, tp, ft) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SELECT)
  			local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.thfilter), tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp, ft)
  			local sc = g:GetFirst()
  			if ft > 0 and sc:IsCanBeSpecialSummoned(e, 0, tp, false, false) and (not sc:IsAbleToDeck() or Duel.SelectOption(tp, aux.Stringid(id, 3), 1152) == 1) then
  				Duel.SpecialSummon(sc, 0, tp, tp, false, false, POS_FACEUP)
  			else
  				Duel.HintSelection(g)
  				Duel.SendtoDeck(sc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  			end
  		end
  	end
  end
  function s.tgfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return not Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_MZONE, 0, 1, nil)
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, e:GetHandler(), 1, 0, 0)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoGrave(c, REASON_EFFECT)
  	end
  end
  
  
source_text: Plunder
  You can declare 1 Attribute, then target 1 face-up monster your opponent controls; it becomes that Attribute until the end of this turn (even if this card leaves the field), then you can take 1 "Plunder Patroll" monster from your GY, and either shuffle it into the Deck or Special Summon it. You can only use this effect of "Plunder Patroll Booty" once per turn. Once per turn, during the End Phase, if you control no "Plunder Patroll" monsters: Send this card to the GY.
target_text: -- Plunder
  local SETCARD_ID_0 = 0x13f --Plunder
  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)
  	--attribute
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TODECK + CATEGORY_SPECIAL_SUMMON + CATEGORY_GRAVE_SPSUMMON)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e2:SetCountLimit(1, id)
  	e2:SetTarget(s.atrtg)
  	e2:SetOperation(s.atrop)
  	c:RegisterEffect(e2)
  	--to grave
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOGRAVE)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_PHASE + PHASE_END)
  	e3:SetRange(LOCATION_SZONE)
  	e3:SetCountLimit(1)
  	e3:SetCondition(s.tgcon)
  	e3:SetTarget(s.tgtg)
  	e3:SetOperation(s.tgop)
  	c:RegisterEffect(e3)
  end
  function s.atrtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_MZONE) and chkc:IsControler(1 - tp) and chkc:IsFaceup()
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
  	local g = Duel.SelectTarget(tp, Card.IsFaceup, tp, 0, LOCATION_MZONE, 1, 1, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATTRIBUTE)
  	local att = Duel.AnnounceAttribute(tp, 1, ATTRIBUTE_ALL & ~g:GetFirst():GetAttribute())
  	e:SetLabel(att)
  end
  function s.thfilter(c, e, tp, ft)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and (c:IsAbleToDeck() or (ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)))
  end
  function s.atrop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	local att = e:GetLabel()
  	if tc:IsRelateToEffect(e) and tc:IsFaceup() and not tc:IsImmuneToEffect(e) and not tc:IsAttribute(att) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_CHANGE_ATTRIBUTE)
  		e1:SetValue(att)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  		local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  		if Duel.IsExistingMatchingCard(aux.NecroValleyFilter(s.thfilter), tp, LOCATION_GRAVE, 0, 1, nil, e, tp, ft) and Duel.SelectYesNo(tp, aux.Stringid(id, 2)) then
  			Duel.BreakEffect()
  			Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SELECT)
  			local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.thfilter), tp, LOCATION_GRAVE, 0, 1, 1, nil, e, tp, ft)
  			local sc = g:GetFirst()
  			if ft > 0 and sc:IsCanBeSpecialSummoned(e, 0, tp, false, false) and (not sc:IsAbleToDeck() or Duel.SelectOption(tp, aux.Stringid(id, 3), 1152) == 1) then
  				Duel.SpecialSummon(sc, 0, tp, tp, false, false, POS_FACEUP)
  			else
  				Duel.HintSelection(g)
  				Duel.SendtoDeck(sc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  			end
  		end
  	end
  end
  function s.tgfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.tgcon(e, tp, eg, ep, ev, re, r, rp)
  	return not Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_MZONE, 0, 1, nil)
  end
  function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, e:GetHandler(), 1, 0, 0)
  end
  function s.tgop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoGrave(c, REASON_EFFECT)
  	end
  end
  
  