name: Aegaion
card_text: 2 Level 8 monsters
  During either player's turn: You can banish 1 random face-down monster from your opponent's Extra Deck, face-up, and if you do, this card's ATK becomes equal to the banished monster's. During either player's turn: You can detach 1 Xyz Material from this card, then target 1 of your opponent's banished Fusion, Synchro, or Xyz Monsters; return it to the Extra Deck, and if you do, destroy 1 monster your opponent controls with the same monster card type (Fusion, Synchro, or Xyz). You can only use each effect of "Aegaion the Sea Castrum" once per turn.
lua: local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 8, 2)
  	c:EnableReviveLimit()
  	--remove
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.rmtg)
  	e1:SetOperation(s.rmop)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOEXTRA + CATEGORY_DESTROY)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.rmfilter(c)
  	return c:IsFacedown() and c:IsAbleToRemove()
  end
  function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.rmfilter, tp, 0, LOCATION_EXTRA, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, 1 - tp, LOCATION_EXTRA)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.rmfilter, tp, 0, LOCATION_EXTRA, nil)
  	if g:GetCount() == 0 then
  		return
  	end
  	Duel.ShuffleExtra(1 - tp)
  	local tc = g:RandomSelect(tp, 1):GetFirst()
  	Duel.Remove(tc, POS_FACEUP, REASON_EFFECT)
  	local atk = tc:GetAttack()
  	if atk < 0 then
  		atk = 0
  	end
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or c:IsFacedown() then
  		return
  	end
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  	e1:SetValue(atk)
  	e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  	c:RegisterEffect(e1)
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.filter(c, tp)
  	local ctype = bit.band(c:GetType(), TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  	return c:IsFaceup() and ctype ~= 0 and c:IsAbleToExtra() and Duel.IsExistingMatchingCard(s.filter2, tp, 0, LOCATION_MZONE, 1, nil, ctype)
  end
  function s.filter2(c, ctype)
  	return c:IsFaceup() and c:IsType(ctype)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_REMOVED) and chkc:IsControler(1 - tp) and s.filter(chkc, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, 0, LOCATION_REMOVED, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.filter, tp, 0, LOCATION_REMOVED, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOEXTRA, g, 1, 0, 0)
  	local ctype = bit.band(g:GetFirst():GetType(), TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  	local dg = Duel.GetMatchingGroup(s.filter2, tp, 0, LOCATION_MZONE, nil, ctype)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, dg, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.SendtoDeck(tc, nil, SEQ_DECKTOP, REASON_EFFECT) ~= 0 then
  		local ctype = bit.band(tc:GetType(), TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local g = Duel.SelectMatchingCard(tp, s.filter2, tp, 0, LOCATION_MZONE, 1, 1, nil, ctype)
  		Duel.Destroy(g, REASON_EFFECT)
  	end
  end
  
  
source_text: Aegaion
  2 Level 8 monsters
  During either player's turn: You can banish 1 random face-down monster from your opponent's Extra Deck, face-up, and if you do, this card's ATK becomes equal to the banished monster's. During either player's turn: You can detach 1 Xyz Material from this card, then target 1 of your opponent's banished Fusion, Synchro, or Xyz Monsters; return it to the Extra Deck, and if you do, destroy 1 monster your opponent controls with the same monster card type (Fusion, Synchro, or Xyz). You can only use each effect of "Aegaion the Sea Castrum" once per turn.
target_text: -- Aegaion
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--xyz summon
  	aux.AddXyzProcedure(c, nil, 8, 2)
  	c:EnableReviveLimit()
  	--remove
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_REMOVE)
  	e1:SetType(EFFECT_TYPE_QUICK_O)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1, id)
  	e1:SetTarget(s.rmtg)
  	e1:SetOperation(s.rmop)
  	c:RegisterEffect(e1)
  	--destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOEXTRA + CATEGORY_DESTROY)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetType(EFFECT_TYPE_QUICK_O)
  	e2:SetCode(EVENT_FREE_CHAIN)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetCountLimit(1, id + 1)
  	e2:SetCost(s.descost)
  	e2:SetTarget(s.destg)
  	e2:SetOperation(s.desop)
  	c:RegisterEffect(e2)
  end
  function s.rmfilter(c)
  	return c:IsFacedown() and c:IsAbleToRemove()
  end
  function s.rmtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.rmfilter, tp, 0, LOCATION_EXTRA, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_REMOVE, nil, 1, 1 - tp, LOCATION_EXTRA)
  end
  function s.rmop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.rmfilter, tp, 0, LOCATION_EXTRA, nil)
  	if g:GetCount() == 0 then
  		return
  	end
  	Duel.ShuffleExtra(1 - tp)
  	local tc = g:RandomSelect(tp, 1):GetFirst()
  	Duel.Remove(tc, POS_FACEUP, REASON_EFFECT)
  	local atk = tc:GetAttack()
  	if atk < 0 then
  		atk = 0
  	end
  	local c = e:GetHandler()
  	if not c:IsRelateToEffect(e) or c:IsFacedown() then
  		return
  	end
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_SINGLE)
  	e1:SetCode(EFFECT_SET_ATTACK_FINAL)
  	e1:SetValue(atk)
  	e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_DISABLE)
  	c:RegisterEffect(e1)
  end
  function s.descost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():CheckRemoveOverlayCard(tp, 1, REASON_COST)
  	end
  	e:GetHandler():RemoveOverlayCard(tp, 1, 1, REASON_COST)
  end
  function s.filter(c, tp)
  	local ctype = bit.band(c:GetType(), TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  	return c:IsFaceup() and ctype ~= 0 and c:IsAbleToExtra() and Duel.IsExistingMatchingCard(s.filter2, tp, 0, LOCATION_MZONE, 1, nil, ctype)
  end
  function s.filter2(c, ctype)
  	return c:IsFaceup() and c:IsType(ctype)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsLocation(LOCATION_REMOVED) and chkc:IsControler(1 - tp) and s.filter(chkc, tp)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.filter, tp, 0, LOCATION_REMOVED, 1, nil, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TODECK)
  	local g = Duel.SelectTarget(tp, s.filter, tp, 0, LOCATION_REMOVED, 1, 1, nil, tp)
  	Duel.SetOperationInfo(0, CATEGORY_TOEXTRA, g, 1, 0, 0)
  	local ctype = bit.band(g:GetFirst():GetType(), TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  	local dg = Duel.GetMatchingGroup(s.filter2, tp, 0, LOCATION_MZONE, nil, ctype)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, dg, 1, 0, 0)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.SendtoDeck(tc, nil, SEQ_DECKTOP, REASON_EFFECT) ~= 0 then
  		local ctype = bit.band(tc:GetType(), TYPE_FUSION + TYPE_SYNCHRO + TYPE_XYZ)
  		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_DESTROY)
  		local g = Duel.SelectMatchingCard(tp, s.filter2, tp, 0, LOCATION_MZONE, 1, 1, nil, ctype)
  		Duel.Destroy(g, REASON_EFFECT)
  	end
  end
  
  