name: Lair
card_text: All face-up monsters on the field become DARK. Once per turn, if you would Tribute a monster you control to activate a card effect, you can Tribute 1 DARK monster your opponent controls, even though you do not control it. Once per turn, during the End Phase: Special Summon as many "Torment Tokens" (Fiend/DARK/Level 3/ATK 1000/DEF 1000) as possible to the turn player's field in Defense Position, up to the number of monsters Tributed this turn while this card was face-up.
external_ids: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 59160189 --Torment Token
  
lua: local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 59160189 --Torment Token
  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)
  	--attribute
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  	e2:SetCode(EFFECT_CHANGE_ATTRIBUTE)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsFaceup))
  	e2:SetValue(ATTRIBUTE_DARK)
  	c:RegisterEffect(e2)
  	--release replace
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_EXTRA_RELEASE_NONSUM)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetTargetRange(0, LOCATION_MZONE)
  	e3:SetTarget(aux.TargetBoolFunction(Card.IsAttribute, ATTRIBUTE_DARK))
  	e3:SetCountLimit(1)
  	e3:SetValue(s.relval)
  	c:RegisterEffect(e3)
  	--token
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e4:SetCode(EVENT_RELEASE)
  	e4:SetRange(LOCATION_FZONE)
  	e4:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e4:SetOperation(s.regop)
  	c:RegisterEffect(e4)
  	local e5 = Effect.CreateEffect(c)
  	e5:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e5:SetCode(EVENT_PHASE + PHASE_END)
  	e5:SetRange(LOCATION_FZONE)
  	e5:SetCountLimit(1)
  	e5:SetTarget(s.sptg)
  	e5:SetOperation(s.spop)
  	c:RegisterEffect(e5)
  end
  function s.relval(e, re, r, rp)
  	return (s.re_activated or re:IsActivated()) and bit.band(r, REASON_COST) ~= 0
  end
  function s.regfilter(c)
  	local typ = c:IsPreviousLocation(LOCATION_ONFIELD) and c:GetPreviousTypeOnField() or c:GetType()
  	return typ & TYPE_MONSTER > 0
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local mct = eg:FilterCount(s.regfilter, nil)
  	if mct == 0 then
  		return
  	end
  	local c = e:GetHandler()
  	local ct = c:GetFlagEffectLabel(id)
  	if ct then
  		c:SetFlagEffectLabel(id, ct + mct)
  	else
  		c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1, mct)
  	end
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local ct = e:GetHandler():GetFlagEffectLabel(id)
  	if ct then
  		Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, ct, 0, 0)
  		Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, ct, 0, 0)
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetHandler():GetFlagEffectLabel(id)
  	if not ct then
  		return
  	end
  	local p = Duel.GetTurnPlayer()
  	if not Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, 0, TYPES_TOKEN_MONSTER, 1000, 1000, 3, RACE_FIEND, ATTRIBUTE_DARK, POS_FACEUP_DEFENSE, p) then
  		return
  	end
  	local ft = Duel.GetLocationCount(p, LOCATION_MZONE)
  	ct = math.min(ct, ft)
  	if ct > 1 and Duel.IsPlayerAffectedByEffect(p, CARD_ID_0) then
  		ct = 1
  	end
  	for i = 1, ct do
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummonStep(token, 0, tp, p, false, false, POS_FACEUP_DEFENSE)
  	end
  	Duel.SpecialSummonComplete()
  end
  
  
source_text: Lair
  All face-up monsters on the field become DARK. Once per turn, if you would Tribute a monster you control to activate a card effect, you can Tribute 1 DARK monster your opponent controls, even though you do not control it. Once per turn, during the End Phase: Special Summon as many "Torment Tokens" (Fiend/DARK/Level 3/ATK 1000/DEF 1000) as possible to the turn player's field in Defense Position, up to the number of monsters Tributed this turn while this card was face-up.
target_text: -- Lair
  local CARD_ID_0 = 59822133 --Blue-Eyes Spirit Dragon
  local CARD_ID_1 = 59160189 --Torment Token
  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)
  	--attribute
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  	e2:SetCode(EFFECT_CHANGE_ATTRIBUTE)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsFaceup))
  	e2:SetValue(ATTRIBUTE_DARK)
  	c:RegisterEffect(e2)
  	--release replace
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD)
  	e3:SetCode(EFFECT_EXTRA_RELEASE_NONSUM)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetTargetRange(0, LOCATION_MZONE)
  	e3:SetTarget(aux.TargetBoolFunction(Card.IsAttribute, ATTRIBUTE_DARK))
  	e3:SetCountLimit(1)
  	e3:SetValue(s.relval)
  	c:RegisterEffect(e3)
  	--token
  	local e4 = Effect.CreateEffect(c)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e4:SetCode(EVENT_RELEASE)
  	e4:SetRange(LOCATION_FZONE)
  	e4:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e4:SetOperation(s.regop)
  	c:RegisterEffect(e4)
  	local e5 = Effect.CreateEffect(c)
  	e5:SetCategory(CATEGORY_SPECIAL_SUMMON + CATEGORY_TOKEN)
  	e5:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e5:SetCode(EVENT_PHASE + PHASE_END)
  	e5:SetRange(LOCATION_FZONE)
  	e5:SetCountLimit(1)
  	e5:SetTarget(s.sptg)
  	e5:SetOperation(s.spop)
  	c:RegisterEffect(e5)
  end
  function s.relval(e, re, r, rp)
  	return (s.re_activated or re:IsActivated()) and bit.band(r, REASON_COST) ~= 0
  end
  function s.regfilter(c)
  	local typ = c:IsPreviousLocation(LOCATION_ONFIELD) and c:GetPreviousTypeOnField() or c:GetType()
  	return typ & TYPE_MONSTER > 0
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local mct = eg:FilterCount(s.regfilter, nil)
  	if mct == 0 then
  		return
  	end
  	local c = e:GetHandler()
  	local ct = c:GetFlagEffectLabel(id)
  	if ct then
  		c:SetFlagEffectLabel(id, ct + mct)
  	else
  		c:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END, 0, 1, mct)
  	end
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	local ct = e:GetHandler():GetFlagEffectLabel(id)
  	if ct then
  		Duel.SetOperationInfo(0, CATEGORY_TOKEN, nil, ct, 0, 0)
  		Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, ct, 0, 0)
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp)
  	local ct = e:GetHandler():GetFlagEffectLabel(id)
  	if not ct then
  		return
  	end
  	local p = Duel.GetTurnPlayer()
  	if not Duel.IsPlayerCanSpecialSummonMonster(tp, id + 1, 0, TYPES_TOKEN_MONSTER, 1000, 1000, 3, RACE_FIEND, ATTRIBUTE_DARK, POS_FACEUP_DEFENSE, p) then
  		return
  	end
  	local ft = Duel.GetLocationCount(p, LOCATION_MZONE)
  	ct = math.min(ct, ft)
  	if ct > 1 and Duel.IsPlayerAffectedByEffect(p, CARD_ID_0) then
  		ct = 1
  	end
  	for i = 1, ct do
  		local token = Duel.CreateToken(tp, id + 1)
  		Duel.SpecialSummonStep(token, 0, tp, p, false, false, POS_FACEUP_DEFENSE)
  	end
  	Duel.SpecialSummonComplete()
  end
  
  