name: Advanced
card_text: All "Crystal Beast" monsters on the field and in the GY become DARK. If an "Ultimate Crystal" monster attacks, negate the effects of the attack target during that Battle Phase. During damage calculation, if a "Crystal Beast" monster you control battles and you would take damage: You can send 1 "Crystal Beast" monster from your Deck to the GY; you take no battle damage from that battle.
external_ids: local SETCARD_ID_0 = 0x1034 --Crystal
  local SETCARD_ID_1 = 0x2034 --Ultimate
  
lua: local SETCARD_ID_0 = 0x1034 --Crystal
  local SETCARD_ID_1 = 0x2034 --Ultimate
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--attribute
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  	e2:SetCode(EFFECT_CHANGE_ATTRIBUTE)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_1))
  	e2:SetValue(ATTRIBUTE_DARK)
  	c:RegisterEffect(e2)
  	local e2g = e2:Clone()
  	e2g:SetTargetRange(LOCATION_GRAVE, LOCATION_GRAVE)
  	e2g:SetCondition(s.gravecon)
  	c:RegisterEffect(e2g)
  	--negate
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetCondition(s.discon)
  	e3:SetOperation(s.disop)
  	c:RegisterEffect(e3)
  	local e6 = Effect.CreateEffect(c)
  	e6:SetType(EFFECT_TYPE_FIELD)
  	e6:SetCode(EFFECT_DISABLE)
  	e6:SetRange(LOCATION_FZONE)
  	e6:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  	e6:SetTarget(s.distg)
  	c:RegisterEffect(e6)
  	local e7 = e6:Clone()
  	e7:SetCode(EFFECT_DISABLE_EFFECT)
  	c:RegisterEffect(e7)
  	--
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_PRE_DAMAGE_CALCULATE)
  	e4:SetRange(LOCATION_FZONE)
  	e4:SetCondition(s.damcon)
  	e4:SetCost(s.damcost)
  	e4:SetOperation(s.damop)
  	c:RegisterEffect(e4)
  	--
  	local e5 = Effect.CreateEffect(c)
  	e5:SetType(EFFECT_TYPE_FIELD)
  	e5:SetCode(EFFECT_CHANGE_GRAVE_ATTRIBUTE)
  	e5:SetRange(LOCATION_FZONE)
  	e5:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e5:SetTargetRange(1, 1)
  	e5:SetCondition(s.gravecon)
  	e5:SetValue(ATTRIBUTE_DARK)
  	e5:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_1))
  	c:RegisterEffect(e5)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local at = Duel.GetAttackTarget()
  	return at and a:IsSetCard(SETCARD_ID_1)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetAttackTarget()
  	tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_BATTLE, 0, 1)
  end
  function s.distg(e, c)
  	return c:GetFlagEffect(id) ~= 0
  end
  function s.gravecon(e)
  	local tp = e:GetHandlerPlayer()
  	return not Duel.IsPlayerAffectedByEffect(tp, EFFECT_NECRO_VALLEY) and not Duel.IsPlayerAffectedByEffect(1 - tp, EFFECT_NECRO_VALLEY)
  end
  function s.damcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local at = Duel.GetAttackTarget()
  	return Duel.GetBattleDamage(tp) > 0 and ((a:IsControler(tp) and a:IsSetCard(SETCARD_ID_1)) or (at and at:IsControler(tp) and at:IsSetCard(SETCARD_ID_1)))
  end
  function s.dfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsType(TYPE_MONSTER) and c:IsAbleToGraveAsCost()
  end
  function s.damcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.dfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectMatchingCard(tp, s.dfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	Duel.SendtoGrave(g, REASON_COST)
  end
  function s.damop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_AVOID_BATTLE_DAMAGE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetTargetRange(1, 0)
  	e1:SetReset(RESET_PHASE + PHASE_DAMAGE)
  	Duel.RegisterEffect(e1, tp)
  end
  
  
source_text: Advanced
  All "Crystal Beast" monsters on the field and in the GY become DARK. If an "Ultimate Crystal" monster attacks, negate the effects of the attack target during that Battle Phase. During damage calculation, if a "Crystal Beast" monster you control battles and you would take damage: You can send 1 "Crystal Beast" monster from your Deck to the GY; you take no battle damage from that battle.
target_text: -- Advanced
  local SETCARD_ID_0 = 0x1034 --Crystal
  local SETCARD_ID_1 = 0x2034 --Ultimate
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--Activate
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_ACTIVATE)
  	e1:SetCode(EVENT_FREE_CHAIN)
  	c:RegisterEffect(e1)
  	--attribute
  	local e2 = Effect.CreateEffect(c)
  	e2:SetType(EFFECT_TYPE_FIELD)
  	e2:SetRange(LOCATION_FZONE)
  	e2:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  	e2:SetCode(EFFECT_CHANGE_ATTRIBUTE)
  	e2:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_1))
  	e2:SetValue(ATTRIBUTE_DARK)
  	c:RegisterEffect(e2)
  	local e2g = e2:Clone()
  	e2g:SetTargetRange(LOCATION_GRAVE, LOCATION_GRAVE)
  	e2g:SetCondition(s.gravecon)
  	c:RegisterEffect(e2g)
  	--negate
  	local e3 = Effect.CreateEffect(c)
  	e3:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_CONTINUOUS)
  	e3:SetCode(EVENT_ATTACK_ANNOUNCE)
  	e3:SetRange(LOCATION_FZONE)
  	e3:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
  	e3:SetCondition(s.discon)
  	e3:SetOperation(s.disop)
  	c:RegisterEffect(e3)
  	local e6 = Effect.CreateEffect(c)
  	e6:SetType(EFFECT_TYPE_FIELD)
  	e6:SetCode(EFFECT_DISABLE)
  	e6:SetRange(LOCATION_FZONE)
  	e6:SetTargetRange(LOCATION_MZONE, LOCATION_MZONE)
  	e6:SetTarget(s.distg)
  	c:RegisterEffect(e6)
  	local e7 = e6:Clone()
  	e7:SetCode(EFFECT_DISABLE_EFFECT)
  	c:RegisterEffect(e7)
  	--
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 0))
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetCode(EVENT_PRE_DAMAGE_CALCULATE)
  	e4:SetRange(LOCATION_FZONE)
  	e4:SetCondition(s.damcon)
  	e4:SetCost(s.damcost)
  	e4:SetOperation(s.damop)
  	c:RegisterEffect(e4)
  	--
  	local e5 = Effect.CreateEffect(c)
  	e5:SetType(EFFECT_TYPE_FIELD)
  	e5:SetCode(EFFECT_CHANGE_GRAVE_ATTRIBUTE)
  	e5:SetRange(LOCATION_FZONE)
  	e5:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e5:SetTargetRange(1, 1)
  	e5:SetCondition(s.gravecon)
  	e5:SetValue(ATTRIBUTE_DARK)
  	e5:SetTarget(aux.TargetBoolFunction(Card.IsSetCard, SETCARD_ID_1))
  	c:RegisterEffect(e5)
  end
  function s.discon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local at = Duel.GetAttackTarget()
  	return at and a:IsSetCard(SETCARD_ID_1)
  end
  function s.disop(e, tp, eg, ep, ev, re, r, rp)
  	local tc = Duel.GetAttackTarget()
  	tc:RegisterFlagEffect(id, RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_BATTLE, 0, 1)
  end
  function s.distg(e, c)
  	return c:GetFlagEffect(id) ~= 0
  end
  function s.gravecon(e)
  	local tp = e:GetHandlerPlayer()
  	return not Duel.IsPlayerAffectedByEffect(tp, EFFECT_NECRO_VALLEY) and not Duel.IsPlayerAffectedByEffect(1 - tp, EFFECT_NECRO_VALLEY)
  end
  function s.damcon(e, tp, eg, ep, ev, re, r, rp)
  	local a = Duel.GetAttacker()
  	local at = Duel.GetAttackTarget()
  	return Duel.GetBattleDamage(tp) > 0 and ((a:IsControler(tp) and a:IsSetCard(SETCARD_ID_1)) or (at and at:IsControler(tp) and at:IsSetCard(SETCARD_ID_1)))
  end
  function s.dfilter(c)
  	return c:IsSetCard(SETCARD_ID_1) and c:IsType(TYPE_MONSTER) and c:IsAbleToGraveAsCost()
  end
  function s.damcost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return Duel.IsExistingMatchingCard(s.dfilter, tp, LOCATION_DECK, 0, 1, nil)
  	end
  	Duel.Hint(HINT_SELECTMSG, tp, HINTMSG_TOGRAVE)
  	local g = Duel.SelectMatchingCard(tp, s.dfilter, tp, LOCATION_DECK, 0, 1, 1, nil)
  	Duel.SendtoGrave(g, REASON_COST)
  end
  function s.damop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetCode(EFFECT_AVOID_BATTLE_DAMAGE)
  	e1:SetProperty(EFFECT_FLAG_PLAYER_TARGET)
  	e1:SetTargetRange(1, 0)
  	e1:SetReset(RESET_PHASE + PHASE_DAMAGE)
  	Duel.RegisterEffect(e1, tp)
  end
  
  