Generate a YGOPro Lua script for the following Yu-Gi-Oh card effect:
Unaffected by your opponent's Spell effects. Gains 500 ATK for each monster your opponent controls. When an opponent's monster declares an attack: You can have this card lose exactly 1000 DEF, and if you do, your "Valkyrie" monsters cannot be destroyed by battle this turn.
--ワルキューレ・ヴリュンヒルデ
--Valkyrie Brunhilde
--Scripted by Naim and AlphaKretin
local s,id=GetID()
function s.initial_effect(c)
	--Unaffected by opponent's spells
	local e1=Effect.CreateEffect(c)
	e1:SetType(EFFECT_TYPE_SINGLE)
	e1:SetCode(EFFECT_IMMUNE_EFFECT)
	e1:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e1:SetRange(LOCATION_MZONE)
	e1:SetValue(s.efilter)
	c:RegisterEffect(e1)
	--Gains 500 ATK per opponent's monsters
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_SINGLE)
	e2:SetProperty(EFFECT_FLAG_SINGLE_RANGE)
	e2:SetRange(LOCATION_MZONE)
	e2:SetCode(EFFECT_UPDATE_ATTACK)
	e2:SetValue(s.atkval)
	c:RegisterEffect(e2)
	--Lose 1000 DEF, and if you do, your "Valkyrie" monsters cannot be destroyed by battle
	local e3=Effect.CreateEffect(c)
	e3:SetDescription(aux.Stringid(id,0))
	e3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_TRIGGER_O)
	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
	e3:SetRange(LOCATION_MZONE)
	e3:SetCondition(s.ptcon)
	e3:SetTarget(s.pttg)
	e3:SetOperation(s.ptop)
	c:RegisterEffect(e3)
end
s.listed_series={SET_VALKYRIE}
function s.efilter(e,te)
	return te:IsSpellEffect() and te:GetOwnerPlayer()~=e:GetHandlerPlayer()
end
function s.atkval(e,c)
	return Duel.GetFieldGroupCount(c:GetControler(),0,LOCATION_MZONE)*500
end
function s.ptcon(e,tp,eg,ep,ev,re,r,rp)
	local at=Duel.GetAttacker()
	return at and at:IsControler(1-tp) and at:IsRelateToBattle()
end
function s.pttg(e,tp,eg,ep,ev,re,r,rp,chk,chkc)
	local c=e:GetHandler()
	if chk==0 then return c:GetDefense()>=1000 end
end
function s.ptop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:IsRelateToEffect(e) and c:IsFaceup() and c:UpdateDefense(-1000)==-1000 then
		--Your "Valkyrie" monsters cannot be destroyed by battle
		local e1=Effect.CreateEffect(c)
		e1:SetDescription(3000)
		e1:SetProperty(EFFECT_FLAG_CLIENT_HINT)
		e1:SetType(EFFECT_TYPE_FIELD)
		e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
		e1:SetTargetRange(LOCATION_MZONE,0)
		e1:SetReset(RESET_PHASE|PHASE_END)
		e1:SetTarget(function(e,cc) return cc:IsSetCard(SET_VALKYRIE) end)
		e1:SetValue(1)
		Duel.RegisterEffect(e1,tp)
	end
end