player_ai = nil
GlobalTargetID = nil
GlobalCheating = false
-- Sets up some variables for using card script functions
function set_player_turn(init)
if init and not playersetupcomplete
or player_ai == nil 
then
player_ai = Duel.GetTurnPlayer()
playersetupcomplete = true
Startup()
GlobalPreviousLP=AI.GetPlayerLP(1)
if GlobalCheating then
EnableCheats()
end
end
end
-- sets up cheats for the cheating AI

function EnableCheats()

end
function EnableCheats2()
  local e1=Effect.GlobalEffect()
  e1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
  e1:SetProperty(EFFECT_FLAG_NO_TURN_RESET)
  e1:SetCode(EVENT_PHASE_START+PHASE_DRAW)
  e1:SetCountLimit(1)
  e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
	
local seq=-4
while seq~=28 do
seq=seq+4
local e1=Effect.GlobalEffect()
e1:SetType(EFFECT_TYPE_FIELD)
e1:SetCode(EFFECT_DISABLE_FIELD)
e1:SetLabel(seq)
e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
return bit.lshift(0x1,e:GetLabel())
end
)
e1:SetReset(0)
Duel.RegisterEffect(e1,tp)
end

	for tp=0,1 do 
	local g=Duel.GetMatchingGroup(nil,tp,LOCATION_HAND,0,nil)
	if g:GetCount()>4 then
		local sg=g:RandomSelect(tp,g:GetCount()-4)
		Duel.SendtoDeck(sg,nil,2,REASON_RULE)
		end
	end
	
	--Effect Draw
	local e2=Effect.GlobalEffect()
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_HAND_LIMIT)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e2:SetTargetRange(1,1)
	e2:SetValue(5)
	Duel.RegisterEffect(e2,0)
	
	--skip
	local e3=Effect.GlobalEffect()
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e3:SetTargetRange(1,1)
	e3:SetCode(EFFECT_SKIP_M2)
	Duel.RegisterEffect(e3,0)
	
	for tp=0,1 do 
	skill(tp)
	end
	
    end)
    Duel.RegisterEffect(e1,tp)
end
function skill(tp)
	local sel=Duel.SelectOption(tp,aux.Stringid(87591001,1),aux.Stringid(87591001,2)
	,aux.Stringid(87591001,3))
	if sel==0 then
	--draw
	local e1=Effect.GlobalEffect()
	e1:SetCategory(CATEGORY_DRAW+CATEGORY_DAMAGE)
	e1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
	e1:SetCode(EVENT_PHASE+PHASE_BATTLE)
	e1:SetCountLimit(1)
	e1:SetCondition(function (e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetTurnPlayer()~=tp and Duel.GetFlagEffect(tp,6239)==0
	and Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)<3
	end)
	e1:SetOperation(function (e,tp,eg,ep,ev,re,r,rp)
	if Duel.IsExistingMatchingCard(Card.IsDiscardable,tp,LOCATION_HAND,0,1,nil)
	and Duel.SelectYesNo(tp,aux.Stringid(87591001,1)) then
	Duel.Hint(HINT_MESSAGE,1-tp,aux.Stringid(87591001,10))
	AI.Chat("技能发动！淘气仙星·欺诈：对方战斗阶段结束时丢弃1张手卡使用")
	AI.Chat("对方抽卡到3张为止，对方受到抽卡数量×800的伤害。")
	local ht=Duel.GetFieldGroupCount(tp,0,LOCATION_HAND)
	Duel.DiscardHand(tp,Card.IsDiscardable,1,1,REASON_COST+REASON_DISCARD)
		local dc=Duel.Draw(1-tp,3-ht,REASON_RULE)
		Duel.Damage(1-tp,dc*800,REASON_RULE)
		Duel.RegisterFlagEffect(tp,6239,nil,0,1)
	 end
	end)
	Duel.RegisterEffect(e1,tp)
elseif sel==1 then
		local e1=Effect.GlobalEffect()
	e1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
	e1:SetCode(EVENT_PHASE_START+PHASE_DRAW)
	e1:SetCountLimit(1)
	e1:SetCondition(function (e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetTurnPlayer()==tp and Duel.GetFlagEffect(tp,6239)==0
	end)
	e1:SetOperation(function (e,tp,eg,ep,ev,re,r,rp)
	if Duel.SelectYesNo(tp,aux.Stringid(87591001,2)) then
	Duel.Hint(HINT_MESSAGE,1-tp,aux.Stringid(87591001,10))
	AI.Chat("技能发动！双重抽卡：抽卡阶段开始时使用；那次通常抽卡变成2张。")
	local e2=Effect.GlobalEffect()
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetCode(EFFECT_DRAW_COUNT)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e2:SetTargetRange(1,0)
	e2:SetReset(RESET_PHASE+PHASE_DRAW)
	e2:SetValue(2)
	Duel.RegisterEffect(e2,tp)
	Duel.RegisterFlagEffect(tp,6239,nil,0,1)
	end
	end)
	Duel.RegisterEffect(e1,tp)
elseif sel==2 then
	local e1=Effect.GlobalEffect()
	e1:SetCategory(CATEGORY_SPECIAL_SUMMON)
	e1:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
	e1:SetCode(EVENT_PHASE+PHASE_BATTLE)
	e1:SetCountLimit(1)
	e1:SetCondition(function (e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetFlagEffect(tp,6239)==0
	end)
	e1:SetOperation(function (e,tp,eg,ep,ev,re,r,rp)
	local g=Duel.GetMatchingGroup(mon,tp,LOCATION_GRAVE,0,nil,e,tp)
	if g:GetCount()>0 and g:GetCount()<=Duel.GetLocationCount(tp,LOCATION_MZONE)
	and Duel.SelectYesNo(tp,aux.Stringid(87591001,3)) then
	Duel.Hint(HINT_MESSAGE,1-tp,aux.Stringid(87591001,10))
	AI.Chat("技能发动！斗魂：这个回合被战斗破坏的怪兽全部从墓地特殊召唤。")
	Duel.SpecialSummon(g,0,tp,tp,false,false,POS_FACEUP)
	end
	end)
	Duel.RegisterEffect(e1,tp)
	end
end
function mon(c,e,tp)
	return c:IsType(TYPE_MONSTER) and c:GetTurnID()==Duel.GetTurnCount()
	and c:IsReason(REASON_BATTLE) and c:IsCanBeSpecialSummoned(e,0,tp,false,false)
end