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()

  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,0)

end
require("ai.JINB.JINB")
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)
         local tc=Duel.CreateToken(0,afc1[math.random(#afc1)])
        Duel.MoveToField(tc,0,0,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)
		 FIELD_RULE(tc)
      end)
    tc:RegisterEffect(e1)
    end)
    Duel.RegisterEffect(e1,0)
end
function FIELD_RULE(tc)

  local e1=Effect.CreateEffect(tc)
	e1:SetProperty(EFFECT_FLAG_BOTH_SIDE)
	e1:SetType(EFFECT_TYPE_IGNITION)
	e1:SetRange(LOCATION_FZONE)
	e1:SetCondition(function(e,tp,eg,ep,ev,re,r,rp)
	return not Duel.IsExistingMatchingCard(Card.IsSetCard,tp,LOCATION_HAND,0,1,nil,0x871)
	and Duel.GetFlagEffect(tp,87)==0
	end)
	e1:SetOperation(function(e,tp,eg,ep,ev,re,r,rp)
	local c1,c2=Duel.TossCoin(tp,2)
	if c1+c2==1 then
	Duel.RegisterFlagEffect(tp,87,RESET_PHASE+PHASE_END,0,1)
	else
	if c1+c2==0 then tp=1-tp end
	if c1+c2==0 and Duel.IsExistingMatchingCard(Card.IsSetCard,tp,LOCATION_HAND,0,1,nil,0x871)
	then return end
	local c=Duel.CreateToken(tp,ac1[math.random(#ac1)])
	Duel.SendtoHand(c,tp,REASON_RULE)
	--action
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_CANNOT_SSET)
	c:RegisterEffect(e1)
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE)
	e2:SetCode(EFFECT_QP_ACT_IN_NTPHAND)
	c:RegisterEffect(e2)
	end
	end)
	tc:RegisterEffect(e1)
	local e2=e1:Clone()
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_QUICK_O)
	e2:SetCode(EVENT_ATTACK_ANNOUNCE)
	e2:SetTarget(function(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return Duel.GetTurnPlayer()~=tp and not e:GetHandler():IsStatus(STATUS_CHAINING) end
	end)
	tc:RegisterEffect(e2)

	
	local e8=Effect.CreateEffect(tc)
	e8:SetType(EFFECT_TYPE_SINGLE)
	e8:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e8:SetRange(LOCATION_FZONE)
	e8:SetCode(EFFECT_IMMUNE_EFFECT)
	e8:SetValue(1)
	tc:RegisterEffect(e8)
	--cannot activate
	local e9=Effect.CreateEffect(tc)
	e9:SetType(EFFECT_TYPE_FIELD)
	e9:SetCode(EFFECT_CANNOT_ACTIVATE)
	e9:SetRange(LOCATION_FZONE)
	e9:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e9:SetTargetRange(1,0)
	e9:SetValue(function(e,re,tp)
	return re:GetHandler():IsType(TYPE_FIELD) and re:IsHasType(EFFECT_TYPE_ACTIVATE) end)
	tc:RegisterEffect(e9)
	--cannot set
	local e10=Effect.CreateEffect(tc)
	e10:SetType(EFFECT_TYPE_FIELD)
	e10:SetCode(EFFECT_CANNOT_SSET)
	e10:SetRange(LOCATION_FZONE)
	e10:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
	e10:SetTargetRange(1,0)
	e10:SetTarget(function(e,c,tp)
	return c:IsType(TYPE_FIELD) end)
	tc:RegisterEffect(e10)
	--cannot be target
	local e11=Effect.CreateEffect(tc)
	e11:SetType(EFFECT_TYPE_SINGLE)
	e11:SetCode(EFFECT_CANNOT_BE_EFFECT_TARGET)
	e11:SetRange(LOCATION_FZONE)
	e11:SetValue(1)
	tc:RegisterEffect(e11)
	
	local e12=Effect.CreateEffect(tc)
	e12:SetType(EFFECT_TYPE_SINGLE)
	e12:SetCode(EFFECT_CANNOT_USE_AS_COST)
	e12:SetRange(LOCATION_FZONE)
	e12:SetValue(1)
	tc:RegisterEffect(e12)

end
