name: Desrook
card_text: Once per turn, during your Standby Phase, you must pay 500 LP (this is not optional), or this card is destroyed. When resolving an opponent's card effect that targets this card, roll a six-sided die and negate that effect if you roll a 3, and if you do, destroy that card. When "Terrorking Archfiend" you control is destroyed and sent to the GY (even during the Damage Step) (Quick Effect): You can send this card from your hand to the GY, then target that "Terrorking Archfiend" in your GY; Special Summon it.
external_ids: local CARD_ID_0 = 35975813 --Terrorking Archfiend
  local CARD_ID_1 = 94585852 --Pandemonium
  
lua: local CARD_ID_0 = 35975813 --Terrorking Archfiend
  local CARD_ID_1 = 94585852 --Pandemonium
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--maintain
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EVENT_PHASE + PHASE_STANDBY)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.mtcon)
  	e1:SetOperation(s.mtop)
  	c:RegisterEffect(e1)
  	--disable and destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_DICE)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_SOLVING)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  	--special summon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetRange(LOCATION_HAND)
  	e3:SetCost(s.spcost)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.mtcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.mtop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.CheckLPCost(tp, 500) or Duel.IsPlayerAffectedByEffect(tp, CARD_ID_1) then
  		if not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_1) or not Duel.SelectEffectYesNo(tp, e:GetHandler(), aux.Stringid(94585852, 1)) then
  			Duel.PayLPCost(tp, 500)
  		end
  	else
  		Duel.Destroy(e:GetHandler(), REASON_COST)
  	end
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	if ep == tp then
  		return
  	end
  	if not re:IsHasProperty(EFFECT_FLAG_CARD_TARGET) then
  		return false
  	end
  	local tg = Duel.GetChainInfo(ev, CHAININFO_TARGET_CARDS)
  	if not tg or not tg:IsContains(e:GetHandler()) or not Duel.IsChainDisablable(ev) then
  		return false
  	end
  	local rc = re:GetHandler()
  	local dc = Duel.TossDice(tp, 1)
  	if dc ~= 3 then
  		return
  	end
  	if Duel.NegateEffect(ev, true) and rc:IsRelateToEffect(re) then
  		Duel.Destroy(rc, REASON_EFFECT)
  	end
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGraveAsCost()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  function s.filter(c, e, tp)
  	return c:IsCode(CARD_ID_0) and c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsCanBeEffectTarget(e) 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 eg:IsContains(chkc) and s.filter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and eg:IsExists(s.filter, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = eg:FilterSelect(tp, s.filter, 1, 1, nil, e, tp)
  	Duel.SetTargetCard(g)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  
source_text: Desrook
  Once per turn, during your Standby Phase, you must pay 500 LP (this is not optional), or this card is destroyed. When resolving an opponent's card effect that targets this card, roll a six-sided die and negate that effect if you roll a 3, and if you do, destroy that card. When "Terrorking Archfiend" you control is destroyed and sent to the GY (even during the Damage Step) (Quick Effect): You can send this card from your hand to the GY, then target that "Terrorking Archfiend" in your GY; Special Summon it.
target_text: -- Desrook
  local CARD_ID_0 = 35975813 --Terrorking Archfiend
  local CARD_ID_1 = 94585852 --Pandemonium
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--maintain
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE + EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EVENT_PHASE + PHASE_STANDBY)
  	e1:SetRange(LOCATION_MZONE)
  	e1:SetCountLimit(1)
  	e1:SetCondition(s.mtcon)
  	e1:SetOperation(s.mtop)
  	c:RegisterEffect(e1)
  	--disable and destroy
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_DICE)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e2:SetCode(EVENT_CHAIN_SOLVING)
  	e2:SetRange(LOCATION_MZONE)
  	e2:SetOperation(s.disop)
  	c:RegisterEffect(e2)
  	--special summon
  	local e3 = Effect.CreateEffect(c)
  	e3:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e3:SetDescription(aux.Stringid(id, 0))
  	e3:SetType(EFFECT_TYPE_QUICK_O)
  	e3:SetProperty(EFFECT_FLAG_CARD_TARGET + EFFECT_FLAG_DAMAGE_STEP)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetRange(LOCATION_HAND)
  	e3:SetCost(s.spcost)
  	e3:SetTarget(s.sptg)
  	e3:SetOperation(s.spop)
  	c:RegisterEffect(e3)
  end
  function s.mtcon(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetTurnPlayer() == tp
  end
  function s.mtop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.CheckLPCost(tp, 500) or Duel.IsPlayerAffectedByEffect(tp, CARD_ID_1) then
  		if not Duel.IsPlayerAffectedByEffect(tp, CARD_ID_1) or not Duel.SelectEffectYesNo(tp, e:GetHandler(), aux.Stringid(94585852, 1)) then
  			Duel.PayLPCost(tp, 500)
  		end
  	else
  		Duel.Destroy(e:GetHandler(), REASON_COST)
  	end
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	if ep == tp then
  		return
  	end
  	if not re:IsHasProperty(EFFECT_FLAG_CARD_TARGET) then
  		return false
  	end
  	local tg = Duel.GetChainInfo(ev, CHAININFO_TARGET_CARDS)
  	if not tg or not tg:IsContains(e:GetHandler()) or not Duel.IsChainDisablable(ev) then
  		return false
  	end
  	local rc = re:GetHandler()
  	local dc = Duel.TossDice(tp, 1)
  	if dc ~= 3 then
  		return
  	end
  	if Duel.NegateEffect(ev, true) and rc:IsRelateToEffect(re) then
  		Duel.Destroy(rc, REASON_EFFECT)
  	end
  end
  function s.spcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGraveAsCost()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  function s.filter(c, e, tp)
  	return c:IsCode(CARD_ID_0) and c:IsPreviousControler(tp) and c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsCanBeEffectTarget(e) 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 eg:IsContains(chkc) and s.filter(chkc, e, tp)
  	end
  	if chk == 0 then
  		return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and eg:IsExists(s.filter, 1, nil, e, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = eg:FilterSelect(tp, s.filter, 1, 1, nil, e, tp)
  	Duel.SetTargetCard(g)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, g, 1, 0, 0)
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  	end
  end
  
  