name: Lucky
card_text: Once per turn, when an opponent's monster declares an attack: You can toss a coin 3 times. If the result is 3 heads, draw 3 cards, but if the result is 3 tails, destroy this card. If this face-up card on the field is destroyed: You lose 6000 Life Points.
lua: 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)
  	--coin
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_DRAW + CATEGORY_DESTROY + CATEGORY_COIN)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.atkcon)
  	e2:SetTarget(s.atktg)
  	e2:SetOperation(s.atkop)
  	c:RegisterEffect(e2)
  	--life lost
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCondition(s.descon)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	return tp ~= Duel.GetTurnPlayer()
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_COIN, nil, 0, tp, 3)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local r1, r2, r3 = Duel.TossCoin(tp, 3)
  	if r1 + r2 + r3 == 3 then
  		Duel.Draw(tp, 3, REASON_EFFECT)
  	elseif r1 + r2 + r3 == 0 then
  		Duel.Destroy(e:GetHandler(), REASON_EFFECT)
  	end
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousPosition(POS_FACEUP)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local lp = Duel.GetLP(tp)
  	Duel.SetLP(tp, lp - 6000)
  end
  
  
source_text: Lucky
  Once per turn, when an opponent's monster declares an attack: You can toss a coin 3 times. If the result is 3 heads, draw 3 cards, but if the result is 3 tails, destroy this card. If this face-up card on the field is destroyed: You lose 6000 Life Points.
target_text: -- Lucky
  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)
  	--coin
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 0))
  	e2:SetCategory(CATEGORY_DRAW + CATEGORY_DESTROY + CATEGORY_COIN)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e2:SetRange(LOCATION_SZONE)
  	e2:SetCountLimit(1)
  	e2:SetCondition(s.atkcon)
  	e2:SetTarget(s.atktg)
  	e2:SetOperation(s.atkop)
  	c:RegisterEffect(e2)
  	--life lost
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_SINGLE + EFFECT_TYPE_TRIGGER_F)
  	e3:SetCode(EVENT_DESTROYED)
  	e3:SetCondition(s.descon)
  	e3:SetOperation(s.desop)
  	c:RegisterEffect(e3)
  end
  function s.atkcon(e, tp, eg, ep, ev, re, r, rp)
  	return tp ~= Duel.GetTurnPlayer()
  end
  function s.atktg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return true
  	end
  	Duel.SetOperationInfo(0, CATEGORY_COIN, nil, 0, tp, 3)
  end
  function s.atkop(e, tp, eg, ep, ev, re, r, rp)
  	local r1, r2, r3 = Duel.TossCoin(tp, 3)
  	if r1 + r2 + r3 == 3 then
  		Duel.Draw(tp, 3, REASON_EFFECT)
  	elseif r1 + r2 + r3 == 0 then
  		Duel.Destroy(e:GetHandler(), REASON_EFFECT)
  	end
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	return c:IsPreviousLocation(LOCATION_ONFIELD) and c:IsPreviousPosition(POS_FACEUP)
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local lp = Duel.GetLP(tp)
  	Duel.SetLP(tp, lp - 6000)
  end
  
  