name: Generation
card_text: If your LP are lower than your opponent's: Add to your hand, or Special Summon, 1 "Elemental HERO" monster, "Kuriboh" monster, or "Neo-Spacian" monster from your Deck or GY, with ATK less than or equal to the difference in LPs, also for the rest of this turn, you cannot activate cards, or the effects of cards, with the same name as that card. You can only activate 1 "Generation Next" per turn.
external_ids: local SETCARD_ID_0 = 0x3008 --Elemental
  
lua: local SETCARD_ID_0 = 0x3008 --Elemental
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetLP(tp) < Duel.GetLP(1 - tp)
  end
  function s.thfilter(c, e, tp, ft, atk)
  	return c:IsSetCard(SETCARD_ID_0, 0xa4, 0x1f) and c:IsAttackBelow(atk) and (c:IsAbleToHand() or (ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)))
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local atk = math.abs(Duel.GetLP(0) - Duel.GetLP(1))
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil, e, tp, ft, atk)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local atk = math.abs(Duel.GetLP(0) - Duel.GetLP(1))
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.thfilter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil, e, tp, ft, atk)
  	local tc = g:GetFirst()
  	if tc then
  		local res = nil
  		if ft > 0 and tc:IsCanBeSpecialSummoned(e, 0, tp, false, false) and (not tc:IsAbleToHand() or Duel.SelectOption(tp, 1190, 1152) == 1) then
  			res = Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  		else
  			res = Duel.SendtoHand(tc, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tc)
  		end
  		if res ~= 0 then
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetCode(EFFECT_CANNOT_ACTIVATE)
  			e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  			e1:SetTargetRange(1, 0)
  			e1:SetValue(s.aclimit)
  			e1:SetLabel(tc:GetCode())
  			e1:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e1, tp)
  		end
  	end
  end
  function s.aclimit(e, re, tp)
  	return re:GetHandler():IsCode(e:GetLabel())
  end
  
  
source_text: Generation
  If your LP are lower than your opponent's: Add to your hand, or Special Summon, 1 "Elemental HERO" monster, "Kuriboh" monster, or "Neo-Spacian" monster from your Deck or GY, with ATK less than or equal to the difference in LPs, also for the rest of this turn, you cannot activate cards, or the effects of cards, with the same name as that card. You can only activate 1 "Generation Next" per turn.
target_text: -- Generation
  local SETCARD_ID_0 = 0x3008 --Elemental
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SEARCH + CATEGORY_TOHAND + CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetHintTiming(0, TIMINGS_CHECK_MONSTER + TIMING_END_PHASE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.condition)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return Duel.GetLP(tp) < Duel.GetLP(1 - tp)
  end
  function s.thfilter(c, e, tp, ft, atk)
  	return c:IsSetCard(SETCARD_ID_0, 0xa4, 0x1f) and c:IsAttackBelow(atk) and (c:IsAbleToHand() or (ft > 0 and c:IsCanBeSpecialSummoned(e, 0, tp, false, false)))
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local atk = math.abs(Duel.GetLP(0) - Duel.GetLP(1))
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil, e, tp, ft, atk)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	local ft = Duel.GetLocationCount(tp, LOCATION_MZONE)
  	local atk = math.abs(Duel.GetLP(0) - Duel.GetLP(1))
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_OPERATECARD)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.thfilter), tp, LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil, e, tp, ft, atk)
  	local tc = g:GetFirst()
  	if tc then
  		local res = nil
  		if ft > 0 and tc:IsCanBeSpecialSummoned(e, 0, tp, false, false) and (not tc:IsAbleToHand() or Duel.SelectOption(tp, 1190, 1152) == 1) then
  			res = Duel.SpecialSummon(tc, 0, tp, tp, false, false, POS_FACEUP)
  		else
  			res = Duel.SendtoHand(tc, nil, REASON_EFFECT)
  			Duel.ConfirmCards(1 - tp, tc)
  		end
  		if res ~= 0 then
  			local e1 = Effect.CreateEffect(e:GetHandler())
  			e1:SetType(EFFECT_TYPE_FIELD)
  			e1:SetCode(EFFECT_CANNOT_ACTIVATE)
  			e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  			e1:SetTargetRange(1, 0)
  			e1:SetValue(s.aclimit)
  			e1:SetLabel(tc:GetCode())
  			e1:SetReset(RESET_PHASE + PHASE_END)
  			Duel.RegisterEffect(e1, tp)
  		end
  	end
  end
  function s.aclimit(e, re, tp)
  	return re:GetHandler():IsCode(e:GetLabel())
  end
  
  