--次元领域决斗
--Dimension Duel
--Scripted by Larry126
--note: Please contact with me if wanting to edit this script
local s,id=GetID()
function s.initial_effect(c)
	aux.EnableExtraRules(c,s,s.op)
end
function s.op(c)
	--limit summon
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e1:SetCode(EVENT_ADJUST)
	e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_UNCOPYABLE)
	e1:SetCondition(s.limitcon)
	e1:SetOperation(s.limitop)
	Duel.RegisterEffect(e1,0)
	--Damage
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e2:SetCode(EVENT_DESTROYED)
	e2:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_DAMAGE_STEP+EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_IGNORE_IMMUNE)
	e2:SetOperation(s.damop)
	Duel.RegisterEffect(e2,0)
	--no battle damage
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetCode(EFFECT_AVOID_BATTLE_DAMAGE)
	e3:SetProperty(EFFECT_FLAG_PLAYER_TARGET+EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_IGNORE_IMMUNE)
	e3:SetTargetRange(1,1)
	e3:SetCondition(s.bcon)
	Duel.RegisterEffect(e3,0)
	--Dimension Summon
	local e4=Effect.CreateEffect(c)
	e4:SetDescription(aux.Stringid(id,0))
	e4:SetType(EFFECT_TYPE_FIELD)
	e4:SetCode(EFFECT_SUMMON_PROC)
	e4:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_IGNORE_IMMUNE)
	e4:SetTargetRange(LOCATION_HAND,LOCATION_HAND)
	e4:SetTarget(aux.FieldSummonProcTg(s.nttg))
	e4:SetCondition(s.ntcon)
	e4:SetValue(6)
	Duel.RegisterEffect(e4,0)
	local e5=e4:Clone()
	e5:SetCode(EFFECT_LIMIT_SUMMON_PROC)
	e5:SetTarget(aux.FieldSummonProcTg(s.nttg2))
	e5:SetValue(6)
	Duel.RegisterEffect(e5,0)
	--spirit
	local e6=Effect.CreateEffect(c)
	e6:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e6:SetCode(EVENT_SUMMON_SUCCESS)
	e6:SetTarget(s.spttg)
	e6:SetOperation(s.sptop)
	e6:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_IGNORE_IMMUNE)
	Duel.RegisterEffect(e6,0)
	local e7=e6:Clone()
	e7:SetCode(EVENT_FLIP_SUMMON_SUCCESS)
	Duel.RegisterEffect(e7,0)
	local e8=e6:Clone()
	e8:SetCode(EVENT_SPSUMMON_SUCCESS)
	Duel.RegisterEffect(e8,0)
end
function s.limitfilter(c)
	return c:IsHasEffect(EFFECT_LIMIT_SUMMON_PROC) and c:GetFlagEffect(51160002)<=0
end
function s.limitcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.IsExistingMatchingCard(s.limitfilter,tp,0xff,0xff,1,nil)
end
function s.limitop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local g=Duel.GetMatchingGroup(s.limitfilter,tp,0xff,0xff,nil)
	if #g>0 then
		for tc in aux.Next(g) do
			tc:RegisterFlagEffect(51160002,0,0,0)
		end
	end
end
------------------------------------------------------------------------
--tribute
function s.nttg(e,c)
	return c==0 or c==1 or c:GetFlagEffect(51160002)==0
end
function s.nttg2(e,c)
	return c==0 or c==1 or c:GetFlagEffect(51160002)>0
end
function s.ntcon(e,c,minc)
	if c==nil then return true end
	local _,max=c:GetTributeRequirement()
	return max>0 and Duel.GetLocationCount(c:GetControler(),LOCATION_MZONE)>0
end
------------------------------------------------------------------------
--spirit charge
function s.sptfilter(c)
	return c:IsType(TYPE_MONSTER+TYPE_TRAPMONSTER+TYPE_TOKEN) and c:IsOnField()
end
function s.spttg(e,tp,eg,ep,ev,re,r,rp,chk)
	if chk==0 then return eg:IsExists(s.sptfilter,1,nil) end
end
function s.sptop(e,tp,eg,ep,ev,re,r,rp)
	local g=eg:Filter(s.sptfilter,nil)
	for tc in aux.Next(g) do
		local baseAtk=0
		local baseDef=0
		local textAtk=tc:GetTextAttack()
		local textDef=tc:GetTextDefense()
		local ctl=tc:GetControler()
		if textAtk~=-2 and textAtk~=0 then
			Duel.Hint(HINT_SELECTMSG,ctl,aux.Stringid(id,4))
			local atkop=Duel.SelectOption(ctl,aux.Stringid(id,1),aux.Stringid(id,2),aux.Stringid(id,3))
			if atkop==0 then
				baseAtk=textAtk
			elseif atkop==1 then
				baseAtk=0
			elseif atkop==2 then
				baseAtk=aux.ComposeNumberDigitByDigit(ctl,0,textAtk)
			end
			local e1=Effect.CreateEffect(tc)
			e1:SetType(EFFECT_TYPE_SINGLE)
			e1:SetCode(EFFECT_SET_BASE_ATTACK)
			e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_UNCOPYABLE)
			e1:SetRange(LOCATION_MZONE)
			e1:SetValue(baseAtk)
			e1:SetReset(RESET_EVENT|RESETS_STANDARD&~(RESET_TOFIELD|RESET_TEMP_REMOVE|RESET_TURN_SET))
			tc:RegisterEffect(e1)
		end
		if textDef~=-2 and textDef~=0 then
			Duel.Hint(HINT_SELECTMSG,ctl,aux.Stringid(id,5))
			local defop=Duel.SelectOption(ctl,aux.Stringid(id,1),aux.Stringid(id,2),aux.Stringid(id,3))
			if defop==0 then
				baseDef=textDef
			elseif defop==1 then
				baseDef=0
			elseif defop==2 then
				baseDef=aux.ComposeNumberDigitByDigit(ctl,0,textDef)
			end
			local e2=Effect.CreateEffect(tc)
			e2:SetType(EFFECT_TYPE_SINGLE)
			e2:SetCode(EFFECT_SET_BASE_DEFENSE)
			e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_IGNORE_IMMUNE+EFFECT_FLAG_UNCOPYABLE)
			e2:SetRange(LOCATION_MZONE)
			e2:SetValue(baseDef)
			e2:SetReset(RESET_EVENT|RESETS_STANDARD&~(RESET_TOFIELD|RESET_TEMP_REMOVE|RESET_TURN_SET))
			tc:RegisterEffect(e2)
		end
	end
end
------------------------------------------------------------------------
--inflict battle damage
function s.damfilter(c)
	return c:IsPreviousLocation(LOCATION_MZONE) and c:IsPreviousPosition(POS_FACEUP)
end
function s.damop(e,tp,eg,ep,ev,re,r,rp)
	local g=eg:Filter(s.damfilter,nil)
	for tc in aux.Next(g) do
		local ctl=tc:GetControler()
		if tc:IsPreviousPosition(POS_ATTACK) then
			Duel.Damage(ctl,tc:GetPreviousAttackOnField(),REASON_RULE,true)
		elseif tc:IsPreviousPosition(POS_DEFENSE) then
			Duel.Damage(ctl,tc:GetPreviousDefenseOnField(),REASON_RULE,true)
		end
	end
	Duel.RDComplete()
end
------------------------------------------------------------------------
--no battle damaged
function s.bcon(e,tp,eg,ep,ev,re,r,rp)
	return Duel.GetAttackTarget()~=nil
end