name: Madolche
card_text: If this card in your possession is destroyed by an opponent's card and sent to your GY: Shuffle this card into the Deck. When this card is Special Summoned: You can add 1 "Madolche" Spell/Trap from your Deck to your hand. You must control a face-up "Madolche" Beast monster to activate and to resolve this effect.
external_ids: local SETCARD_ID_0 = 0x71 --Madolche
  
lua: local SETCARD_ID_0 = 0x71 --Madolche
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--to deck
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCode(EVENT_TO_GRAVE)
  	e1:SetCondition(s.retcon)
  	e1:SetTarget(s.rettg)
  	e1:SetOperation(s.retop)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_ACTIVATE_CONDITION)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetCondition(s.shcon)
  	e2:SetTarget(s.shtg)
  	e2:SetOperation(s.shop)
  	c:RegisterEffect(e2)
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_DESTROY) and e:GetHandler():GetReasonPlayer() == 1 - tp and e:GetHandler():IsPreviousControler(tp)
  end
  function s.rettg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, e:GetHandler(), 1, 0, 0)
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SendtoDeck(e:GetHandler(), nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  	end
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsRace(RACE_BEAST)
  end
  function s.shcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, e:GetHandler())
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.shtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.shop(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, e:GetHandler()) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Madolche
  If this card in your possession is destroyed by an opponent's card and sent to your GY: Shuffle this card into the Deck. When this card is Special Summoned: You can add 1 "Madolche" Spell/Trap from your Deck to your hand. You must control a face-up "Madolche" Beast monster to activate and to resolve this effect.
target_text: -- Madolche
  local SETCARD_ID_0 = 0x71 --Madolche
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--to deck
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_TODECK)
  	e1:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCode(EVENT_TO_GRAVE)
  	e1:SetCondition(s.retcon)
  	e1:SetTarget(s.rettg)
  	e1:SetOperation(s.retop)
  	c:RegisterEffect(e1)
  	--search
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetProperty(EFFECT_FLAG_ACTIVATE_CONDITION)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetCondition(s.shcon)
  	e2:SetTarget(s.shtg)
  	e2:SetOperation(s.shop)
  	c:RegisterEffect(e2)
  end
  function s.retcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsReason(REASON_DESTROY) and e:GetHandler():GetReasonPlayer() == 1 - tp and e:GetHandler():IsPreviousControler(tp)
  end
  function s.rettg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, e:GetHandler(), 1, 0, 0)
  end
  function s.retop(e, tp, eg, ep, ev, re, r, rp)
  	if e:GetHandler():IsRelateToEffect(e) then
  		Duel.SendtoDeck(e:GetHandler(), nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  	end
  end
  function s.cfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsRace(RACE_BEAST)
  end
  function s.shcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, e:GetHandler())
  end
  function s.filter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP) and c:IsAbleToHand()
  end
  function s.shtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.shop(e, tp, eg, ep, ev, re, r, rp)
  	if not Duel.IsExistingMatchingCard(s.cfilter, tp, LOCATION_MZONE, 0, 1, e:GetHandler()) then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  