name: Bonding
card_text: Tribute 2 "Duoterion" and 1 "Oxygeddon" in your hand and/or field; Special Summon 1 "Water Dragon" or "Water Dragon Cluster" from your hand, Deck, or GY. (This is treated as a Special Summon with the effect of "Bonding - H2O".) If "Water Dragon" or "Water Dragon Cluster" is sent from the field to your GY while this card is in your GY: Add this card to your hand. You can only use this effect of "Bonding - D2O" once per turn.
external_ids: local CARD_ID_0 = 85066822 --Water Dragon
  local CARD_ID_1 = 43017476 --Duoterion
  local CARD_ID_2 = 58071123 --Oxygeddon
  local CARD_ID_3 = 6022371 --Water Dragon Cluster
  
lua: local CARD_ID_0 = 85066822 --Water Dragon
  local CARD_ID_1 = 43017476 --Duoterion
  local CARD_ID_2 = 58071123 --Oxygeddon
  local CARD_ID_3 = 6022371 --Water Dragon Cluster
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1, CARD_ID_2, CARD_ID_3, CARD_ID_0)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  s.spchecks = aux.CreateChecks(Card.IsCode, { CARD_ID_1, CARD_ID_1, CARD_ID_2 })
  function s.costfilter(c, tp)
  	return c:IsCode(CARD_ID_1, CARD_ID_2) and (c:IsControler(tp) or c:IsFaceup())
  end
  function s.fgoal(g, tp)
  	return Duel.GetMZoneCount(tp, g) > 0 and Duel.CheckReleaseGroupEx(tp, aux.IsInGroup, #g, REASON_COST, true, nil, g)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(1)
  	local g = Duel.GetReleaseGroup(tp, true):Filter(s.costfilter, nil, tp)
  	if chk == 0 then
  		return g:CheckSubGroupEach(s.spchecks, s.fgoal, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RELEASE)
  	local rg = g:SelectSubGroupEach(tp, s.spchecks, false, s.fgoal, tp)
  	aux.UseExtraReleaseCount(rg, tp)
  	Duel.Release(rg, REASON_COST)
  end
  function s.filter(c, e, tp)
  	return c:IsCode(CARD_ID_0, CARD_ID_3) and c:IsCanBeSpecialSummoned(e, 0, tp, true, true)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local res = e:GetLabel() == 1 or Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  	if chk == 0 then
  		e:SetLabel(0)
  		return res and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_HAND + LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, true, true, POS_FACEUP)
  		g:GetFirst():CompleteProcedure()
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.thfilter, 1, nil) and not eg:IsContains(e:GetHandler())
  end
  function s.thfilter(c)
  	return (c:IsCode(CARD_ID_0) or c:IsCode(CARD_ID_3)) and c:IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  end
  
  
source_text: Bonding
  Tribute 2 "Duoterion" and 1 "Oxygeddon" in your hand and/or field; Special Summon 1 "Water Dragon" or "Water Dragon Cluster" from your hand, Deck, or GY. (This is treated as a Special Summon with the effect of "Bonding - H2O".) If "Water Dragon" or "Water Dragon Cluster" is sent from the field to your GY while this card is in your GY: Add this card to your hand. You can only use this effect of "Bonding - D2O" once per turn.
target_text: -- Bonding
  local CARD_ID_0 = 85066822 --Water Dragon
  local CARD_ID_1 = 43017476 --Duoterion
  local CARD_ID_2 = 58071123 --Oxygeddon
  local CARD_ID_3 = 6022371 --Water Dragon Cluster
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1, CARD_ID_2, CARD_ID_3, CARD_ID_0)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	e1:SetCost(s.cost)
  	e1:SetTarget(s.target)
  	e1:SetOperation(s.activate)
  	c:RegisterEffect(e1)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_F)
  	e2:SetCode(EVENT_TO_GRAVE)
  	e2:SetProperty(EFFECT_FLAG_DELAY)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  s.spchecks = aux.CreateChecks(Card.IsCode, { CARD_ID_1, CARD_ID_1, CARD_ID_2 })
  function s.costfilter(c, tp)
  	return c:IsCode(CARD_ID_1, CARD_ID_2) and (c:IsControler(tp) or c:IsFaceup())
  end
  function s.fgoal(g, tp)
  	return Duel.GetMZoneCount(tp, g) > 0 and Duel.CheckReleaseGroupEx(tp, aux.IsInGroup, #g, REASON_COST, true, nil, g)
  end
  function s.cost(e, tp, eg, ep, ev, re, r, rp, chk)
  	e:SetLabel(1)
  	local g = Duel.GetReleaseGroup(tp, true):Filter(s.costfilter, nil, tp)
  	if chk == 0 then
  		return g:CheckSubGroupEach(s.spchecks, s.fgoal, tp)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_RELEASE)
  	local rg = g:SelectSubGroupEach(tp, s.spchecks, false, s.fgoal, tp)
  	aux.UseExtraReleaseCount(rg, tp)
  	Duel.Release(rg, REASON_COST)
  end
  function s.filter(c, e, tp)
  	return c:IsCode(CARD_ID_0, CARD_ID_3) and c:IsCanBeSpecialSummoned(e, 0, tp, true, true)
  end
  function s.target(e, tp, eg, ep, ev, re, r, rp, chk)
  	local res = e:GetLabel() == 1 or Duel.GetLocationCount(tp, LOCATION_MZONE) > 0
  	if chk == 0 then
  		e:SetLabel(0)
  		return res and Duel.IsExistingMatchingCard(s.filter, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_GRAVE, 0, 1, nil, e, tp)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_SPECIAL_SUMMON, nil, 1, tp, LOCATION_HAND + LOCATION_DECK + LOCATION_GRAVE)
  end
  function s.activate(e, tp, eg, ep, ev, re, r, rp)
  	if Duel.GetLocationCount(tp, LOCATION_MZONE) <= 0 then
  		return
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_SPSUMMON)
  	local g = Duel.SelectMatchingCard(tp, aux.NecroValleyFilter(s.filter), tp, LOCATION_HAND + LOCATION_DECK + LOCATION_GRAVE, 0, 1, 1, nil, e, tp)
  	if g:GetCount() > 0 then
  		Duel.SpecialSummon(g, 0, tp, tp, true, true, POS_FACEUP)
  		g:GetFirst():CompleteProcedure()
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.thfilter, 1, nil) and not eg:IsContains(e:GetHandler())
  end
  function s.thfilter(c)
  	return (c:IsCode(CARD_ID_0) or c:IsCode(CARD_ID_3)) and c:IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  end
  
  