name: Duamutef,
card_text: If you control "King's Sarcophagus", you can Special Summon this card (from your GY). You can only Special Summon "Duamutef, Blessing of Horus" once per turn this way. Gains 1200 ATK/DEF for each "Horus" monster you control. If another card(s) you control leaves the field by an opponent's card effect, while this card is in your Monster Zone (except during the Damage Step): You can draw cards equal to the number of monsters with different names in your Main Monster Zone. You can only use this effect of "Duamutef, Blessing of Horus" once per turn.
external_ids: local CARD_ID_1 = 16528181 --King's Sarcophagus
  local SETCARD_ID_0 = 0x19d --Horus
  
lua: local CARD_ID_1 = 16528181 --King's Sarcophagus
  local SETCARD_ID_0 = 0x19d --Horus
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetRange(LOCATION_GRAVE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.sprcon)
  	c:RegisterEffect(e1)
  	--buff
  	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)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_UPDATE_DEFENSE)
  	c:RegisterEffect(e3)
  	--Leave Field
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_DRAW)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCode(EVENT_LEAVE_FIELD)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetCondition(s.descon)
  	e4:SetTarget(s.destg)
  	e4:SetOperation(s.desop)
  	c:RegisterEffect(e4)
  end
  function s.sprfilter(c)
  	return c:IsFaceup() and c:IsCode(CARD_ID_1)
  end
  function s.sprcon(e, c)
  	if c == nil then
  		return true
  	end
  	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
  		return false
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.sprfilter, tp, LOCATION_ONFIELD, 0, 1, nil)
  end
  function s.bfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.atkval(e, c)
  	return Duel.GetMatchingGroupCount(s.bfilter, c:GetControler(), LOCATION_MZONE, 0, nil) * 1200
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:GetReasonPlayer() == 1 - tp and c:IsReason(REASON_EFFECT)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp) and not eg:IsContains(e:GetHandler())
  end
  function s.drfilter(c, tp)
  	return c:GetSequence() < 5 and c:IsFaceup()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local g = Duel.GetMatchingGroup(s.drfilter, tp, LOCATION_MZONE, 0, nil)
  		local ct = s.count_unique_code(g)
  		e:SetLabel(ct)
  		return ct > 0 and Duel.IsPlayerCanDraw(tp, ct)
  	end
  	Duel.SetTargetParam(e:GetLabel())
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, e:GetLabel())
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.drfilter, tp, LOCATION_MZONE, 0, nil)
  	local ct = s.count_unique_code(g)
  	Duel.Draw(tp, ct, REASON_EFFECT)
  end
  function s.count_unique_code(g)
  	local check = {}
  	local count = 0
  	local tc = g:GetFirst()
  	while tc do
  		for i, code in ipairs({ tc:GetCode() }) do
  			if not check[code] then
  				check[code] = true
  				count = count + 1
  			end
  		end
  		tc = g:GetNext()
  	end
  	return count
  end
  
  
source_text: Duamutef,
  If you control "King's Sarcophagus", you can Special Summon this card (from your GY). You can only Special Summon "Duamutef, Blessing of Horus" once per turn this way. Gains 1200 ATK/DEF for each "Horus" monster you control. If another card(s) you control leaves the field by an opponent's card effect, while this card is in your Monster Zone (except during the Damage Step): You can draw cards equal to the number of monsters with different names in your Main Monster Zone. You can only use this effect of "Duamutef, Blessing of Horus" once per turn.
target_text: -- Duamutef,
  local CARD_ID_1 = 16528181 --King's Sarcophagus
  local SETCARD_ID_0 = 0x19d --Horus
  local s, id, o = GetID()
  function s.initial_effect(c)
  	aux.AddCodeList(c, CARD_ID_1)
  	--special summon
  	local e1 = Effect.CreateEffect(c)
  	e1:SetType(EFFECT_TYPE_FIELD)
  	e1:SetProperty(EFFECT_FLAG_UNCOPYABLE)
  	e1:SetCode(EFFECT_SPSUMMON_PROC)
  	e1:SetRange(LOCATION_GRAVE)
  	e1:SetCountLimit(1, id + EFFECT_COUNT_CODE_OATH)
  	e1:SetCondition(s.sprcon)
  	c:RegisterEffect(e1)
  	--buff
  	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)
  	local e3 = e2:Clone()
  	e3:SetCode(EFFECT_UPDATE_DEFENSE)
  	c:RegisterEffect(e3)
  	--Leave Field
  	local e4 = Effect.CreateEffect(c)
  	e4:SetDescription(aux.Stringid(id, 1))
  	e4:SetCategory(CATEGORY_DRAW)
  	e4:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e4:SetProperty(EFFECT_FLAG_DELAY)
  	e4:SetCode(EVENT_LEAVE_FIELD)
  	e4:SetRange(LOCATION_MZONE)
  	e4:SetCountLimit(1, id + 1)
  	e4:SetCondition(s.descon)
  	e4:SetTarget(s.destg)
  	e4:SetOperation(s.desop)
  	c:RegisterEffect(e4)
  end
  function s.sprfilter(c)
  	return c:IsFaceup() and c:IsCode(CARD_ID_1)
  end
  function s.sprcon(e, c)
  	if c == nil then
  		return true
  	end
  	if c:IsHasEffect(EFFECT_NECRO_VALLEY) then
  		return false
  	end
  	local tp = c:GetControler()
  	return Duel.GetLocationCount(tp, LOCATION_MZONE) > 0 and Duel.IsExistingMatchingCard(s.sprfilter, tp, LOCATION_ONFIELD, 0, 1, nil)
  end
  function s.bfilter(c)
  	return c:IsFaceup() and c:IsSetCard(SETCARD_ID_0)
  end
  function s.atkval(e, c)
  	return Duel.GetMatchingGroupCount(s.bfilter, c:GetControler(), LOCATION_MZONE, 0, nil) * 1200
  end
  function s.cfilter(c, tp)
  	return c:IsPreviousControler(tp) and c:GetReasonPlayer() == 1 - tp and c:IsReason(REASON_EFFECT)
  end
  function s.descon(e, tp, eg, ep, ev, re, r, rp)
  	return eg:IsExists(s.cfilter, 1, nil, tp) and not eg:IsContains(e:GetHandler())
  end
  function s.drfilter(c, tp)
  	return c:GetSequence() < 5 and c:IsFaceup()
  end
  function s.destg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		local g = Duel.GetMatchingGroup(s.drfilter, tp, LOCATION_MZONE, 0, nil)
  		local ct = s.count_unique_code(g)
  		e:SetLabel(ct)
  		return ct > 0 and Duel.IsPlayerCanDraw(tp, ct)
  	end
  	Duel.SetTargetParam(e:GetLabel())
  	Duel.SetOperationInfo(0, CATEGORY_DRAW, nil, 0, tp, e:GetLabel())
  end
  function s.desop(e, tp, eg, ep, ev, re, r, rp)
  	local g = Duel.GetMatchingGroup(s.drfilter, tp, LOCATION_MZONE, 0, nil)
  	local ct = s.count_unique_code(g)
  	Duel.Draw(tp, ct, REASON_EFFECT)
  end
  function s.count_unique_code(g)
  	local check = {}
  	local count = 0
  	local tc = g:GetFirst()
  	while tc do
  		for i, code in ipairs({ tc:GetCode() }) do
  			if not check[code] then
  				check[code] = true
  				count = count + 1
  			end
  		end
  		tc = g:GetNext()
  	end
  	return count
  end
  
  