function onCastSpell(cid, var)
local maxsummons = xx -- change xx to the maximum number of summons a player may have at one time.
local newsummon = variantToString(var)
local summons, summonedcreaturenames = getCreatureSummons(cid), {}
local pos = getThingPos(cid)
local firstval = math.random(-2, 2)
if((table.maxn(summons) > 0) and (isString(newsummon))) then
for _, pid in ipairs(summons) do
local name = getCreatureName(pid)
table.insert(summonedcreaturenames, name)
end
end
if not table.find(summonedcreaturenames, newsummon) then
doConvinceCreature(cid, doCreateMonster(newsummon, getClosestFreeTile(cid, {x=pos.x+firstval,y=pos.y+firstval,z=pos.z}), false))
elseif table.find(summonedcreaturenames, newsummon) then
doSendMagicEffect(pos, CONST_ME_POFF)
doPlayerSendCancel(cid, "You may not summon two monsters of the same name.")
elseif table.maxn(summonedcreaturenames) >= maxsummons then
doSendMagicEffect(pos, CONST_ME_POFF)
doPlayerSendCancel(cid, "You may not summon more than ".. maxsummons .." summons at one time.")
end
return true
end