/
summon_creature.lua
46 lines (40 loc) · 1.42 KB
/
summon_creature.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function onCastSpell(creature, variant)
local monsterName = variant:getString()
local monsterType = MonsterType(monsterName)
if not monsterType then
creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
if not getPlayerFlagValue(creature, PlayerFlag_CanSummonAll) then
if not monsterType:isSummonable() then
creature:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
if #creature:getSummons() >= 2 then
creature:sendCancelMessage("You cannot summon more creatures.")
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
end
local manaCost = monsterType:getManaCost()
if creature:getMana() < manaCost and not getPlayerFlagValue(creature, PlayerFlag_HasInfiniteMana) then
creature:sendCancelMessage(RETURNVALUE_NOTENOUGHMANA)
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
end
local position = creature:getPosition()
local summon = Game.createMonster(monsterName, position, true)
if not summon then
creature:sendCancelMessage(RETURNVALUE_NOTENOUGHROOM)
position:sendMagicEffect(CONST_ME_POFF)
return false
end
creature:addMana(-manaCost)
creature:addManaSpent(manaCost)
creature:addSummon(summon)
position:sendMagicEffect(CONST_ME_MAGIC_BLUE)
summon:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
return true
end