name: Awakening
card_text: You can Special Summon this card (from your hand or Deck) by sending 1 face-up Spellcaster monster and 1 face-up Level 4 or lower WATER monster you control to the GY. You can only use each of the following effects of "Awakening of the Possessed - Gagigobyte" once per turn.
  ● When this card is Special Summoned by its effect: You can send 1 random card from your opponent's hand to the GY, then each player draws 1 card.
  ● If this card is sent from the field to the GY: You can add 1 "Spiritual Water Art" card or 1 "Possessed" Spell/Trap from your Deck to your hand.
external_ids: local SETCARD_ID_0 = 0xc0 --Possessed
  
lua: local SETCARD_ID_0 = 0xc0 --Possessed
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon proc
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND + LOCATION_DECK)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	e1:SetValue(SUMMON_VALUE_SELF)
  	c:RegisterEffect(e1)
  	--discard & draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_HANDES + CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.hdtg)
  	e2:SetOperation(s.hdop)
  	c:RegisterEffect(e2)
  	--search
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.thcon)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.spfilter(c)
  	return c:IsFaceup() and c:IsAbleToGraveAsCost()
  end
  function s.spfilter2(c)
  	return c:IsAttribute(ATTRIBUTE_WATER) and c:IsLevelBelow(4)
  end
  function s.fselect(g, tp)
  	return aux.mzctcheck(g, tp) and aux.gffcheck(g, Card.IsRace, RACE_SPELLCASTER, s.spfilter2, nil)
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_MZONE, 0, nil)
  	return g:CheckSubGroup(s.fselect, 2, 2, tp)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_MZONE, 0, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local sg = g:SelectSubGroup(tp, s.fselect, true, 2, 2, tp)
  	if sg then
  		sg:KeepAlive()
  		e:SetLabelObject(sg)
  		return true
  	else
  		return false
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = e:GetLabelObject()
  	Duel.SendtoGrave(g, REASON_SPSUMMON)
  	g:DeleteGroup()
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetSummonType() == SUMMON_TYPE_SPECIAL + SUMMON_VALUE_SELF
  end
  function s.hdtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, 0, LOCATION_HAND) > 0 and Duel.IsPlayerCanDraw(tp, 1) and Duel.IsPlayerCanDraw(1 - tp, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, 1 - tp, 1)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, PLAYER_ALL, 1)
  end
  function s.hdop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetFieldGroup(tp, 0, LOCATION_HAND)
  	if g:GetCount() == 0 then
  		return
  	end
  	local sg = g:RandomSelect(tp, 1)
  	if Duel.SendtoGrave(sg, REASON_EFFECT) ~= 0 then
  		Duel.BreakEffect()
  		Duel.Draw(tp, 1, REASON_EFFECT)
  		Duel.Draw(1 - tp, 1, REASON_EFFECT)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.thfilter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP)) or c:IsSetCard(0x514c)) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  
source_text: Awakening
  You can Special Summon this card (from your hand or Deck) by sending 1 face-up Spellcaster monster and 1 face-up Level 4 or lower WATER monster you control to the GY. You can only use each of the following effects of "Awakening of the Possessed - Gagigobyte" once per turn.
  ● When this card is Special Summoned by its effect: You can send 1 random card from your opponent's hand to the GY, then each player draws 1 card.
  ● If this card is sent from the field to the GY: You can add 1 "Spiritual Water Art" card or 1 "Possessed" Spell/Trap from your Deck to your hand.
target_text: -- Awakening
  local SETCARD_ID_0 = 0xc0 --Possessed
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--spsummon proc
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetRange(LOCATION_HAND + LOCATION_DECK)
  	e1:SetCondition(s.spcon)
  	e1:SetTarget(s.sptg)
  	e1:SetOperation(s.spop)
  	e1:SetValue(SUMMON_VALUE_SELF)
  	c:RegisterEffect(e1)
  	--discard & draw
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_HANDES + CATEGORY_DRAW)
  	e2:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_SPSUMMON_SUCCESS)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.condition)
  	e2:SetTarget(s.hdtg)
  	e2:SetOperation(s.hdop)
  	c:RegisterEffect(e2)
  	--search
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e3:SetProperty(EFFECT_FLAG_DELAY)
  	e3:SetCode(EVENT_TO_GRAVE)
  	e3:SetCountLimit(1, id + 1)
  	e3:SetCondition(s.thcon)
  	e3:SetTarget(s.thtg)
  	e3:SetOperation(s.thop)
  	c:RegisterEffect(e3)
  end
  function s.spfilter(c)
  	return c:IsFaceup() and c:IsAbleToGraveAsCost()
  end
  function s.spfilter2(c)
  	return c:IsAttribute(ATTRIBUTE_WATER) and c:IsLevelBelow(4)
  end
  function s.fselect(g, tp)
  	return aux.mzctcheck(g, tp) and aux.gffcheck(g, Card.IsRace, RACE_SPELLCASTER, s.spfilter2, nil)
  end
  function s.spcon(e, c)
  	if c == nil then
  		return true
  	end
  	local tp = c:GetControler()
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_MZONE, 0, nil)
  	return g:CheckSubGroup(s.fselect, 2, 2, tp)
  end
  function s.sptg(e, tp, eg, ep, ev, re, r, rp, chk, c)
  	local g = Duel.GetMatchingGroup(s.spfilter, tp, LOCATION_MZONE, 0, nil)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local sg = g:SelectSubGroup(tp, s.fselect, true, 2, 2, tp)
  	if sg then
  		sg:KeepAlive()
  		e:SetLabelObject(sg)
  		return true
  	else
  		return false
  	end
  end
  function s.spop(e, tp, eg, ep, ev, re, r, rp, c)
  	local g = e:GetLabelObject()
  	Duel.SendtoGrave(g, REASON_SPSUMMON)
  	g:DeleteGroup()
  end
  function s.condition(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():GetSummonType() == SUMMON_TYPE_SPECIAL + SUMMON_VALUE_SELF
  end
  function s.hdtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.GetFieldGroupCount(tp, 0, LOCATION_HAND) > 0 and Duel.IsPlayerCanDraw(tp, 1) and Duel.IsPlayerCanDraw(1 - tp, 1)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_HANDES, nil, 0, 1 - tp, 1)
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, PLAYER_ALL, 1)
  end
  function s.hdop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetFieldGroup(tp, 0, LOCATION_HAND)
  	if g:GetCount() == 0 then
  		return
  	end
  	local sg = g:RandomSelect(tp, 1)
  	if Duel.SendtoGrave(sg, REASON_EFFECT) ~= 0 then
  		Duel.BreakEffect()
  		Duel.Draw(tp, 1, REASON_EFFECT)
  		Duel.Draw(1 - tp, 1, REASON_EFFECT)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	return e:GetHandler():IsPreviousLocation(LOCATION_ONFIELD)
  end
  function s.thfilter(c)
  	return ((c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_SPELL + TYPE_TRAP)) or c:IsSetCard(0x514c)) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	if g:GetCount() > 0 then
  		Duel.SendtoHand(g, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, g)
  	end
  end
  
  