name: Pandemonium
card_text: Neither player has to pay LP during the Standby Phase for "Archfiend" monsters. When an "Archfiend" monster is destroyed and sent to the GY, except by battle: Its owner can add 1 "Archfiend" monster from their Deck to their hand with a lower Level than the destroyed card.
external_ids: local SETCARD_ID_0 = 0x45 --Archfiend
  
lua: local SETCARD_ID_0 = 0x45 --Archfiend
  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)
  	--cost change
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(id)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(1, 1)
  	e2:SetCondition(s.costcon)
  	c:RegisterEffect(e2)
  	--search
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetRange(LOCATION_FZONE)
  	e4:SetProperty(EFFECT_FLAG_EVENT_PLAYER + EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetCode(EVENT_CUSTOM + id)
  	e4:SetTarget(s.target)
  	e4:SetOperation(s.operation)
  	c:RegisterEffect(e4)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_TO_GRAVE)
  		ge1:SetOperation(s.regop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.costcon(e)
  	return Duel.GetCurrentPhase() == PHASE_STANDBY
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local lv1 = 0
  	local lv2 = 0
  	local g1 = Group.CreateGroup()
  	local g2 = Group.CreateGroup()
  	local tc = eg:GetFirst()
  	while tc do
  		if tc:IsReason(REASON_DESTROY) and not tc:IsReason(REASON_BATTLE) and tc:GetPreviousTypeOnField() & TYPE_MONSTER ~= 0 and tc:IsSetCard(SETCARD_ID_0) and tc:GetLevel() > 0 then
  			local tlv = tc:GetLevel()
  			if tc:IsControler(0) then
  				if tlv > lv1 then
  					lv1 = tlv
  				end
  				g1:AddCard(tc)
  			else
  				if tlv > lv2 then
  					lv2 = tlv
  				end
  				g2:AddCard(tc)
  			end
  		end
  		tc = eg:GetNext()
  	end
  	if g1:GetCount() > 0 then
  		Duel.RaiseEvent(g1, EVENT_CUSTOM + id, re, r, rp, 0, lv1)
  	end
  	if g2:GetCount() > 0 then
  		Duel.RaiseEvent(g2, EVENT_CUSTOM + id, re, r, rp, 1, lv2)
  	end
  end
  function s.filter(c, lv)
  	return c:GetLevel() < lv and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil, ev)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, ev)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Pandemonium
  Neither player has to pay LP during the Standby Phase for "Archfiend" monsters. When an "Archfiend" monster is destroyed and sent to the GY, except by battle: Its owner can add 1 "Archfiend" monster from their Deck to their hand with a lower Level than the destroyed card.
target_text: -- Pandemonium
  local SETCARD_ID_0 = 0x45 --Archfiend
  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)
  	--cost change
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetCode(id)
  	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(1, 1)
  	e2:SetCondition(s.costcon)
  	c:RegisterEffect(e2)
  	--search
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetRange(LOCATION_FZONE)
  	e4:SetProperty(EFFECT_FLAG_EVENT_PLAYER + EFFECT_FLAG_DAMAGE_STEP)
  	e4:SetCode(EVENT_CUSTOM + id)
  	e4:SetTarget(s.target)
  	e4:SetOperation(s.operation)
  	c:RegisterEffect(e4)
  	if not s.global_check then
  		s.global_check = true
  		local ge1 = Effect.CreateEffect(c)
  		ge1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  		ge1:SetCode(EVENT_TO_GRAVE)
  		ge1:SetOperation(s.regop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.costcon(e)
  	return Duel.GetCurrentPhase() == PHASE_STANDBY
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local lv1 = 0
  	local lv2 = 0
  	local g1 = Group.CreateGroup()
  	local g2 = Group.CreateGroup()
  	local tc = eg:GetFirst()
  	while tc do
  		if tc:IsReason(REASON_DESTROY) and not tc:IsReason(REASON_BATTLE) and tc:GetPreviousTypeOnField() & TYPE_MONSTER ~= 0 and tc:IsSetCard(SETCARD_ID_0) and tc:GetLevel() > 0 then
  			local tlv = tc:GetLevel()
  			if tc:IsControler(0) then
  				if tlv > lv1 then
  					lv1 = tlv
  				end
  				g1:AddCard(tc)
  			else
  				if tlv > lv2 then
  					lv2 = tlv
  				end
  				g2:AddCard(tc)
  			end
  		end
  		tc = eg:GetNext()
  	end
  	if g1:GetCount() > 0 then
  		Duel.RaiseEvent(g1, EVENT_CUSTOM + id, re, r, rp, 0, lv1)
  	end
  	if g2:GetCount() > 0 then
  		Duel.RaiseEvent(g2, EVENT_CUSTOM + id, re, r, rp, 1, lv2)
  	end
  end
  function s.filter(c, lv)
  	return c:GetLevel() < lv and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_DECK, 0, 1, nil, ev)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.operation(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.filter, tp, LOCATION_DECK, 0, 1, 1, nil, ev)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  