name: Kozmotown
card_text: Once per turn: You can target 1 of your banished "Kozmo" monsters; return it to the hand, and if you do, lose LP equal to its original Level x 100. Once per turn: You can reveal any number of "Kozmo" monsters in your hand and shuffle them into the Deck, then draw cards equal to the number of cards you shuffled into the Deck. If this card in the Field Zone is destroyed by a card effect: You can add 1 "Kozmo" card from your Deck to your hand.
external_ids: local SETCARD_ID_0 = 0xd2 --Kozmo
  
lua: local SETCARD_ID_0 = 0xd2 --Kozmo
  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)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCountLimit(1)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--draw
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TODECK + CATEGORY_DRAW)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.drtg)
  	e3:SetOperation(s.drop)
  	c:RegisterEffect(e3)
  	--to hand
  	local e4 = Effect.CreateEffect(c)
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_DESTROYED)
  	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e4:SetCondition(s.thcon2)
  	e4:SetTarget(s.thtg2)
  	e4:SetOperation(s.thop2)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_REMOVED) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_REMOVED, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_REMOVED, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, tc)
  		local lv = tc:GetOriginalLevel()
  		local lp = Duel.GetLP(tp)
  		Duel.SetLP(tp, lp - lv * 100)
  	end
  end
  function s.drfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck()
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp) and Duel.IsExistingMatchingCard(s.drfilter, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_HAND)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	Duel.Hint(HINT_SELECTMSG, p, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(p, s.drfilter, p, LOCATION_HAND, 0, 1, 63, nil)
  	if g:GetCount() > 0 then
  		Duel.ConfirmCards(1 - p, g)
  		local ct = Duel.SendtoDeck(g, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  		Duel.ShuffleDeck(p)
  		Duel.BreakEffect()
  		Duel.Draw(p, ct, REASON_EFFECT)
  	end
  end
  function s.thcon2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_FZONE)
  end
  function s.thfilter2(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.thtg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter2, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop2(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter2, 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: Kozmotown
  Once per turn: You can target 1 of your banished "Kozmo" monsters; return it to the hand, and if you do, lose LP equal to its original Level x 100. Once per turn: You can reveal any number of "Kozmo" monsters in your hand and shuffle them into the Deck, then draw cards equal to the number of cards you shuffled into the Deck. If this card in the Field Zone is destroyed by a card effect: You can add 1 "Kozmo" card from your Deck to your hand.
target_text: -- Kozmotown
  local SETCARD_ID_0 = 0xd2 --Kozmo
  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)
  	--to hand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_IGNITION)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e2:SetCountLimit(1)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  	--draw
  	local e3 = Effect.CreateEffect(c)
  	e3:SetDescription(aux.Stringid(id, 1))
  	e3:SetCategory(CATEGORY_TODECK + CATEGORY_DRAW)
  	e3:SetType(EFFECT_TYPE_IGNITION)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e3:SetCountLimit(1)
  	e3:SetTarget(s.drtg)
  	e3:SetOperation(s.drop)
  	c:RegisterEffect(e3)
  	--to hand
  	local e4 = Effect.CreateEffect(c)
  	e4:SetCategory(CATEGORY_TOHAND + CATEGORY_SEARCH)
  	e4:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_DESTROYED)
  	e4:SetProperty(EFFECT_FLAG_DAMAGE_STEP + EFFECT_FLAG_DELAY)
  	e4:SetCondition(s.thcon2)
  	e4:SetTarget(s.thtg2)
  	e4:SetOperation(s.thop2)
  	c:RegisterEffect(e4)
  end
  function s.thfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToHand()
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return chkc:IsControler(tp) and chkc:IsLocation(LOCATION_REMOVED) and s.thfilter(chkc)
  	end
  	if chk == 0 then
  		return Duel.IsExistingTarget(s.thfilter, tp, LOCATION_REMOVED, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectTarget(tp, s.thfilter, tp, LOCATION_REMOVED, 0, 1, 1, nil)
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, g, 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetFirstTarget()
  	if tc:IsRelateToEffect(e) then
  		Duel.SendtoHand(tc, nil, REASON_EFFECT)
  		Duel.ConfirmCards(1 - tp, tc)
  		local lv = tc:GetOriginalLevel()
  		local lp = Duel.GetLP(tp)
  		Duel.SetLP(tp, lp - lv * 100)
  	end
  end
  function s.drfilter(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsType(TYPE_MONSTER) and c:IsAbleToDeck()
  end
  function s.drtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsPlayerCanDraw(tp) and Duel.IsExistingMatchingCard(s.drfilter, tp, LOCATION_HAND, 0, 1, nil)
  	end
  	Duel.SetTargetPlayer(tp)
  	Duel.SetOperationInfo(0, CATEGORY_TODECK, nil, 1, tp, LOCATION_HAND)
  end
  function s.drop(e, tp, eg, ep, ev, re, r, rp)
  	local p = Duel.GetChainInfo(0, CHAININFO_TARGET_PLAYER)
  	Duel.Hint(HINT_SELECTMSG, p, HINTMSG_TODECK)
  	local g = Duel.SelectMatchingCard(p, s.drfilter, p, LOCATION_HAND, 0, 1, 63, nil)
  	if g:GetCount() > 0 then
  		Duel.ConfirmCards(1 - p, g)
  		local ct = Duel.SendtoDeck(g, nil, SEQ_DECKSHUFFLE, REASON_EFFECT)
  		Duel.ShuffleDeck(p)
  		Duel.BreakEffect()
  		Duel.Draw(p, ct, REASON_EFFECT)
  	end
  end
  function s.thcon2(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsReason(REASON_EFFECT) and c:IsPreviousLocation(LOCATION_FZONE)
  end
  function s.thfilter2(c)
  	return c:IsSetCard(SETCARD_ID_0) and c:IsAbleToHand()
  end
  function s.thtg2(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.thfilter2, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, nil, 1, tp, LOCATION_DECK)
  end
  function s.thop2(e, tp, eg, ep, ev, re, r, rp)
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_ATOHAND)
  	local g = Duel.SelectMatchingCard(tp, s.thfilter2, 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
  
  