name: Soul
card_text: To activate this card you must control no monsters, and a Set Spell/Trap(s) you control must have been destroyed by card effect this turn. During the Main Phase, if exactly 1 monster with less ATK than your LP is Summoned and you have "Z-ONE" in your GY: Destroy that monster, and if you do, take 800 damage, then inflict 800 damage to your opponent.
external_ids: local CARD_ID_0 = 62499965 --Z-ONE
  
lua: local CARD_ID_0 = 62499965 --Z-ONE
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	e0:SetCondition(s.condition)
  	c:RegisterEffect(e0)
  	--destroy
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DESTROY + CATEGORY_DAMAGE)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetRange(LOCATION_FZONE)
  	e1:SetCondition(s.descon)
  	e1:SetTarget(s.destg)
  	e1:SetOperation(s.desop)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EVENT_FLIP_SUMMON_SUCCESS)
  	c:RegisterEffect(e2)
  	local e3 = e1:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	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_DESTROYED)
  		ge1:SetOperation(s.regop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.filter(c)
  	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousPosition(POS_FACEDOWN) and c:GetPreviousTypeOnField() & (TYPE_SPELL + TYPE_TRAP) ~= 0 and c:IsReason(REASON_EFFECT)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local g = eg:Filter(s.filter, nil)
  	local tc = g:GetFirst()
  	while tc do
  		if Duel.GetFlagEffect(tc:GetPreviousControler(), id) == 0 then
  			Duel.RegisterFlagEffect(tc:GetPreviousControler(), id, RESET_PHASE + PHASE_END, 0, 1)
  		end
  		if Duel.GetFlagEffect(0, id) > 0 and Duel.GetFlagEffect(1, id) > 0 then
  			break
  		end
  		tc = g:GetNext()
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) > 0 and Duel.GetFieldGroupCount(tp, LOCATION_MZONE, 0) == 0
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	if not (ph == PHASE_MAIN1 or ph == PHASE_MAIN2) then
  		return false
  	end
  	if eg:GetCount() > 1 then
  		return false
  	end
  	local tc = eg:GetFirst()
  	if not tc then
  		return false
  	end
  	e:SetLabelObject(tc)
  	return tc:IsLocation(LOCATION_MZONE) and tc:IsFaceup() and tc:GetAttack() < Duel.GetLP(tp) and Duel.IsExistingMatchingCard(Card.IsCode, tp, LOCATION_GRAVE, 0, 1, nil, CARD_ID_0)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tc = e:GetLabelObject()
  	if chk == 0 then
  		return tc
  	end
  	Duel.SetTargetCard(tc)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, tc, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, PLAYER_ALL, 800)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.Destroy(tc, REASON_EFFECT) ~= 0 and Duel.Damage(tp, 800, REASON_EFFECT) ~= 0 and Duel.GetLP(tp) > 0 then
  		Duel.BreakEffect()
  		Duel.Damage(1 - tp, 800, REASON_EFFECT)
  	end
  end
  
  
source_text: Soul
  To activate this card you must control no monsters, and a Set Spell/Trap(s) you control must have been destroyed by card effect this turn. During the Main Phase, if exactly 1 monster with less ATK than your LP is Summoned and you have "Z-ONE" in your GY: Destroy that monster, and if you do, take 800 damage, then inflict 800 damage to your opponent.
target_text: -- Soul
  local CARD_ID_0 = 62499965 --Z-ONE
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e0 = Effect.CreateEffect(c)
  	e0:SetType(EFFECT_TYPE_ACTIVATE)
  	e0:SetCode(EVENT_FREE_CHAIN)
  	e0:SetCondition(s.condition)
  	c:RegisterEffect(e0)
  	--destroy
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_DESTROY + CATEGORY_DAMAGE)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e1:SetCode(EVENT_SUMMON_SUCCESS)
  	e1:SetRange(LOCATION_FZONE)
  	e1:SetCondition(s.descon)
  	e1:SetTarget(s.destg)
  	e1:SetOperation(s.desop)
  	c:RegisterEffect(e1)
  	local e2 = e1:Clone()
  	e2:SetCode(EVENT_FLIP_SUMMON_SUCCESS)
  	c:RegisterEffect(e2)
  	local e3 = e1:Clone()
  	e3:SetCode(EVENT_SPSUMMON_SUCCESS)
  	c:RegisterEffect(e3)
  	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_DESTROYED)
  		ge1:SetOperation(s.regop)
  		Duel.RegisterEffect(ge1, 0)
  	end
  end
  function s.filter(c)
  	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousPosition(POS_FACEDOWN) and c:GetPreviousTypeOnField() & (TYPE_SPELL + TYPE_TRAP) ~= 0 and c:IsReason(REASON_EFFECT)
  end
  function s.regop(e, tp, eg, ep, ev, re, r, rp)
  	local g = eg:Filter(s.filter, nil)
  	local tc = g:GetFirst()
  	while tc do
  		if Duel.GetFlagEffect(tc:GetPreviousControler(), id) == 0 then
  			Duel.RegisterFlagEffect(tc:GetPreviousControler(), id, RESET_PHASE + PHASE_END, 0, 1)
  		end
  		if Duel.GetFlagEffect(0, id) > 0 and Duel.GetFlagEffect(1, id) > 0 then
  			break
  		end
  		tc = g:GetNext()
  	end
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetFlagEffect(tp, id) > 0 and Duel.GetFieldGroupCount(tp, LOCATION_MZONE, 0) == 0
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local ph = Duel.GetCurrentPhase()
  	if not (ph == PHASE_MAIN1 or ph == PHASE_MAIN2) then
  		return false
  	end
  	if eg:GetCount() > 1 then
  		return false
  	end
  	local tc = eg:GetFirst()
  	if not tc then
  		return false
  	end
  	e:SetLabelObject(tc)
  	return tc:IsLocation(LOCATION_MZONE) and tc:IsFaceup() and tc:GetAttack() < Duel.GetLP(tp) and Duel.IsExistingMatchingCard(Card.IsCode, tp, LOCATION_GRAVE, 0, 1, nil, CARD_ID_0)
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	local tc = e:GetLabelObject()
  	if chk == 0 then
  		return tc
  	end
  	Duel.SetTargetCard(tc)
  	Duel.SetOperationInfo(0, CATEGORY_DESTROY, tc, 1, 0, 0)
  	Duel.SetOperationInfo(0, CATEGORY_DAMAGE, nil, 0, PLAYER_ALL, 800)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) and Duel.Destroy(tc, REASON_EFFECT) ~= 0 and Duel.Damage(tp, 800, REASON_EFFECT) ~= 0 and Duel.GetLP(tp) > 0 then
  		Duel.BreakEffect()
  		Duel.Damage(1 - tp, 800, REASON_EFFECT)
  	end
  end
  
  