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

function EnableCheats()

  local e2=Effect.GlobalEffect()
  e2:SetType(EFFECT_TYPE_CONTINUOUS+EFFECT_TYPE_FIELD)
  e2:SetCode(EVENT_PHASE+PHASE_DRAW)
  e2:SetCountLimit(1)
  e2:SetOperation(function(e,tp,eg,ep,ev,re,r,rp) 
    if Duel.GetTurnPlayer()==player_ai then 
	  Duel.Draw(player_ai,1,REASON_RULE) 
      Duel.Recover(player_ai,1000,REASON_RULE) 
	  	  if Duel.GetMatchingGroupCount(nil,player_ai,LOCATION_DECK,0,nil)<11 then
		local aig=Duel.GetMatchingGroup(nil,player_ai,LOCATION_GRAVE+LOCATION_REMOVED,0,nil)
		Duel.SendtoDeck(aig,nil,5,REASON_RULE)
		end
    end 
  end)
  Duel.RegisterEffect(e2,player_ai)

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+PHASE_STANDBY)
  e1:SetCountLimit(1)
  e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
	
         tc=Duel.CreateToken(tp,87593200)
		Duel.MoveToField(tc,tp,tp,LOCATION_SZONE,POS_FACEUP,true)
		Duel.RaiseEvent(tc,EVENT_CHAIN_SOLVED,e,0,tp,tp,Duel.GetCurrentChain())
 
  local e1=Effect.CreateEffect(tc)
  e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_F)
  e1:SetProperty(EFFECT_FLAG_NO_TURN_RESET+EFFECT_FLAG_DELAY+EFFECT_FLAG_CANNOT_DISABLE)
  e1:SetCode(EVENT_PHASE+PHASE_STANDBY)
  e1:SetCountLimit(1)
  e1:SetRange(LOCATION_FZONE)
  e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
        local  g=Group.CreateGroup()
	for code=87593001,87593018 do
        local t=Duel.CreateToken(tp,code)
			g:AddCard(t)
	end
		Duel.Remove(g,POS_FACEUP,REASON_RULE)
		
	for tp=0,1 do
		Duel.Hint(HINT_SELECTMSG,tp,HINTMSG_TODECK)
		local sg1=g:Select(tp,5,7,nil)
		local tc=sg1:GetFirst()
	while tc do
		local code=tc:GetCode()
		g1=Group.FromCards(
		Duel.CreateToken(tp,code),
		Duel.CreateToken(tp,code))
		Duel.SendtoDeck(g1,tp,2,REASON_RULE)
		tc=sg1:GetNext()
		end	
		Duel.ShuffleDeck(tp)
		end
    end)
	tc:RegisterEffect(e1)
	FIELD_RULE(tc)
	end)
    Duel.RegisterEffect(e1,0)
end

function FIELD_RULE(tc)
	
	local e3=Effect.CreateEffect(tc)
	e3:SetType(EFFECT_TYPE_SINGLE)
	e3:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e3:SetRange(LOCATION_FZONE)
	e3:SetCode(EFFECT_IMMUNE_EFFECT)
	e3:SetValue(function (e,te)
	return te:GetOwner()~=e:GetOwner() and not te:GetHandler():IsSetCard(0x874) end)
	tc:RegisterEffect(e3)
	--cannot be target
	local e4=e3:Clone()
	e4:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
	tc:RegisterEffect(e4)
	
	local e7=Effect.GlobalEffect()
	e7:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e7:SetCode(EVENT_PREDRAW)
	e7:SetCondition(function(e,tp,eg,ep,ev,re,r,rp)
       local tp=Duel.GetTurnPlayer()
       local g=Duel.GetFieldGroup(tp,LOCATION_HAND+LOCATION_ONFIELD,0)
       local fc=Duel.GetFieldCard(tp,LOCATION_SZONE,5)
	   if fc and fc:IsCode(87593200) then g:RemoveCard(fc) end
		return g:GetCount()==0
		and Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>1 and Duel.GetDrawCount(tp)>0
		end)
	e7:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
       local tp=Duel.GetTurnPlayer()
		Duel.Hint(HINT_SELECTMSG,tp,aux.Stringid(87593200,14))
		local g=Duel.SelectMatchingCard(tp,nil,tp,LOCATION_DECK,0,1,1,nil):GetFirst()
	if g then
		AI.Chat("最强决斗者的决斗一切都是必然，就连抽卡也由决斗者自己创造，闪光抽卡！")
		Duel.ShuffleDeck(tp)
		Duel.MoveSequence(g,0)
	end
end)
	Duel.RegisterEffect(e7,0)
	
	local e8=e7:Clone()
	e8:SetProperty(EFFECT_FLAG_NO_TURN_RESET)
	e8:SetCountLimit(1)
	e8:SetCondition(function(e,tp,eg,ep,ev,re,r,rp)
       local tp=Duel.GetTurnPlayer()
		return Duel.GetLP(tp)<=100 and Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)>1 and Duel.GetDrawCount(tp)>0
		end)
	Duel.RegisterEffect(e8,0)
	
	local e9=Effect.GlobalEffect()
	e9:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e9:SetCode(EVENT_DRAW)
	e9:SetProperty(EFFECT_FLAG_NO_TURN_RESET)
	e9:SetCountLimit(1)
	e9:SetCondition(function(e,tp,eg,ep,ev,re,r,rp)
       local tp=Duel.GetTurnPlayer()
		return r==REASON_RULE and Duel.GetFieldGroupCount(tp,LOCATION_DECK,0)==0
		end)
	e9:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
       local tp=Duel.GetTurnPlayer()
	Duel.Hint(HINT_SELECTMSG,tp,aux.Stringid(87593200,15))
	local ac=Duel.AnnounceCard(tp)
		if Duel.Remove(eg,POS_FACEUP,REASON_RULE)~=0 then
			AI.Chat("重合的炽热友情，会将世界重构筑成希望的未来，重构万物！")
		   local tc=Duel.CreateToken(tp,ac)
		   Duel.SendtoHand(tc,tp,REASON_RULE)
		   Duel.ConfirmCards(1-tp,tc)
		end
	end)
	Duel.RegisterEffect(e9,0)

end