Masterking
2 Fiend monsters
Once per turn, during your Standby Phase, pay 500 LP or destroy this card. Once per Chain, if you pay LP (except during the Damage Step): You can send 1 Fiend monster from your Deck to the GY with ATK or DEF equal to the amount paid. Once per Chain, if a Fiend monster(s) is sent to your GY (except during the Damage Step): You can roll a six-sided die, then apply this effect on 1 of those monsters based on the result.
● 1: Add it to your hand.
● 2, 3, 4, 5: Shuffle into the Deck.
● 6: Special Summon it.
local CARD_ID_1 = 94585852 --Pandemonium

local CARD_ID_1 = 94585852 --Pandemonium
local s, id, o = GetID()
function s.initial_effect(c)
	--link summon
	c:EnableReviveLimit()
	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_FIEND), 2, 2)
	--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)
	--tograve
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetCategory(CATEGORY_TOGRAVE)
	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_PAY_LPCOST)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCondition(s.tgcon)
	e2:SetCost(s.tgcost)
	e2:SetTarget(s.tgtg)
	e2:SetOperation(s.tgop)
	c:RegisterEffect(e2)
	--dice
	local e3 = Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id, 1))
	e3:SetCategory(CATEGORY_DICE + CATEGORY_TOHAND + CATEGORY_TODECK + CATEGORY_SPECIAL_SUMMON)
	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e3:SetProperty(EFFECT_FLAG_DELAY)
	e3:SetCode(EVENT_TO_GRAVE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCost(s.dccost)
	e3:SetTarget(s.dctg)
	e3:SetOperation(s.dcop)
	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)) and Duel.SelectEffectYesNo(tp, e:GetHandler(), aux.Stringid(id, 2)) 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.tgcon(e, tp, eg, ep, ev, re, r, rp)
	return ep == tp
end
function s.tgcost(e, tp, eg, ep, ev, re, r, rp, chk)
	local c = e:GetHandler()
	if chk == 0 then
		return c:GetFlagEffect(id) == 0
	end
	c:RegisterFlagEffect(id, RESET_CHAIN, 0, 1)
end
function s.tgfilter(c, val)
	return c:IsRace(RACE_FIEND) and c:IsType(TYPE_MONSTER) and (c:IsAttack(val) or c:IsDefense(val)) and c:IsAbleToGrave()
end
function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_DECK, 0, 1, nil, ev)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_DECK)
end
function s.tgop(e, tp, eg, ep, ev, re, r, rp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
	local g = Duel.SelectMatchingCard(tp, s.tgfilter, tp, LOCATION_DECK, 0, 1, 1, nil, ev)
	if g:GetCount() > 0 then
		Duel.SendtoGrave(g, REASON_EFFECT)
	end
end
function s.dccost(e, tp, eg, ep, ev, re, r, rp, chk)
	local c = e:GetHandler()
	if chk == 0 then
		return c:GetFlagEffect(35606859) == 0
	end
	c:RegisterFlagEffect(id + 1, RESET_CHAIN, 0, 1)
end
function s.cfilter(c, e, tp)
	return c:IsRace(RACE_FIEND)
		and c:IsType(TYPE_MONSTER)
		and c:IsLocation(LOCATION_GRAVE)
		and c:IsControler(tp)
		and (c:IsAbleToHand() or c:IsAbleToDeck() or Duel.GetLocationCount(tp, LOCATION_MZONE) > 0)
end
function s.dctg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsPlayerCanSpecialSummon(tp) and eg:IsExists(s.cfilter, 1, nil, e, tp)
	end
	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
end
function s.dcop(e, tp, eg, ep, ev, re, r, rp)
	local d = Duel.TossDice(tp, 1)
	local g = eg:Filter(aux.NecroValleyFilter(s.cfilter), nil, e, tp)
	if g:GetCount() == 0 then
		return
	end
	local tc = nil
	if g:GetCount() > 1 then
		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
		tc = g:Select(tp, 1, 1, nil):GetFirst()
	else
		tc = g:GetFirst()
	end
	if d == 1 then
		Duel.SendtoHand(tc, nil, REASON_EFFECT)
	elseif d == 6 then
		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
	elseif d >= 2 and d <= 5 then
		Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
	end
end


Masterking
2 Fiend monsters
Once per turn, during your Standby Phase, pay 500 LP or destroy this card. Once per Chain, if you pay LP (except during the Damage Step): You can send 1 Fiend monster from your Deck to the GY with ATK or DEF equal to the amount paid. Once per Chain, if a Fiend monster(s) is sent to your GY (except during the Damage Step): You can roll a six-sided die, then apply this effect on 1 of those monsters based on the result.
● 1: Add it to your hand.
● 2, 3, 4, 5: Shuffle into the Deck.
● 6: Special Summon it.
-- Masterking
local CARD_ID_1 = 94585852 --Pandemonium
local s, id, o = GetID()
function s.initial_effect(c)
	--link summon
	c:EnableReviveLimit()
	aux.AddLinkProcedure(c, aux.FilterBoolFunction(Card.IsLinkRace, RACE_FIEND), 2, 2)
	--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)
	--tograve
	local e2 = Effect.CreateEffect(c)
	e2:SetDescription(aux.Stringid(id, 0))
	e2:SetCategory(CATEGORY_TOGRAVE)
	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e2:SetProperty(EFFECT_FLAG_DELAY)
	e2:SetCode(EVENT_PAY_LPCOST)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCondition(s.tgcon)
	e2:SetCost(s.tgcost)
	e2:SetTarget(s.tgtg)
	e2:SetOperation(s.tgop)
	c:RegisterEffect(e2)
	--dice
	local e3 = Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id, 1))
	e3:SetCategory(CATEGORY_DICE + CATEGORY_TOHAND + CATEGORY_TODECK + CATEGORY_SPECIAL_SUMMON)
	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
	e3:SetProperty(EFFECT_FLAG_DELAY)
	e3:SetCode(EVENT_TO_GRAVE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCost(s.dccost)
	e3:SetTarget(s.dctg)
	e3:SetOperation(s.dcop)
	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)) and Duel.SelectEffectYesNo(tp, e:GetHandler(), aux.Stringid(id, 2)) 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.tgcon(e, tp, eg, ep, ev, re, r, rp)
	return ep == tp
end
function s.tgcost(e, tp, eg, ep, ev, re, r, rp, chk)
	local c = e:GetHandler()
	if chk == 0 then
		return c:GetFlagEffect(id) == 0
	end
	c:RegisterFlagEffect(id, RESET_CHAIN, 0, 1)
end
function s.tgfilter(c, val)
	return c:IsRace(RACE_FIEND) and c:IsType(TYPE_MONSTER) and (c:IsAttack(val) or c:IsDefense(val)) and c:IsAbleToGrave()
end
function s.tgtg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsExistingMatchingCard(s.tgfilter, tp, LOCATION_DECK, 0, 1, nil, ev)
	end
	Duel.SetOperationInfo(0, CATEGORY_TOGRAVE, nil, 1, tp, LOCATION_DECK)
end
function s.tgop(e, tp, eg, ep, ev, re, r, rp)
	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
	local g = Duel.SelectMatchingCard(tp, s.tgfilter, tp, LOCATION_DECK, 0, 1, 1, nil, ev)
	if g:GetCount() > 0 then
		Duel.SendtoGrave(g, REASON_EFFECT)
	end
end
function s.dccost(e, tp, eg, ep, ev, re, r, rp, chk)
	local c = e:GetHandler()
	if chk == 0 then
		return c:GetFlagEffect(35606859) == 0
	end
	c:RegisterFlagEffect(id + 1, RESET_CHAIN, 0, 1)
end
function s.cfilter(c, e, tp)
	return c:IsRace(RACE_FIEND)
		and c:IsType(TYPE_MONSTER)
		and c:IsLocation(LOCATION_GRAVE)
		and c:IsControler(tp)
		and (c:IsAbleToHand() or c:IsAbleToDeck() or Duel.GetLocationCount(tp, LOCATION_MZONE) > 0)
end
function s.dctg(e, tp, eg, ep, ev, re, r, rp, chk)
	if chk == 0 then
		return Duel.IsPlayerCanSpecialSummon(tp) and eg:IsExists(s.cfilter, 1, nil, e, tp)
	end
	Duel.SetOperationInfo(0, CATEGORY_DICE, nil, 0, tp, 1)
end
function s.dcop(e, tp, eg, ep, ev, re, r, rp)
	local d = Duel.TossDice(tp, 1)
	local g = eg:Filter(aux.NecroValleyFilter(s.cfilter), nil, e, tp)
	if g:GetCount() == 0 then
		return
	end
	local tc = nil
	if g:GetCount() > 1 then
		Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TARGET)
		tc = g:Select(tp, 1, 1, nil):GetFirst()
	else
		tc = g:GetFirst()
	end
	if d == 1 then
		Duel.SendtoHand(tc, nil, REASON_EFFECT)
	elseif d == 6 then
		Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
	elseif d >= 2 and d <= 5 then
		Duel.SendtoDeck(tc, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
	end
end

