Auto-translated
Here is my combat script. It consists of two parts (indicated). The first part is for when the hero is defending, and the second is for when the hero is attacking. I want to fix 3 errors in it:
1) How do I make the human player lose the battle by default? That is, at the beginning, losing/winning is disabled and enabled only when all additional units are summoned. How do I make the human player lose without taking into account the variables dwarf or atdwarf, if all of their units are killed?
2) In the attacking script, the hero first casts a General speed boost. The second spell is not cast on the next turn for some reason. There are no errors in the console. The defending script applies all spells as needed.
3) I noticed that the script gives an error if the hero cannot cast a spell (all creatures are immune to the spell). How can I fix this? That is, so that it does not cast the spell if there is no one to cast it on?
diff = GetDifficulty() + 1
dwarf = 0
dwarfspell = 0
atdwarf = 0
atdwarfspell = 0
SetUnitManaPoints(GetDefenderHero(), GetUnitMaxManaPoints(GetDefenderHero()))
SetUnitManaPoints(GetAttackerHero(), GetUnitMaxManaPoints(GetAttackerHero()))
EnableDynamicBattleMode(nil)
EnableAutoFinish(nil)
--start of the defending script
function DefenderHeroMove(hero)
if IsHuman(DEFENDER) == nil then
if dwarfspell == 0 then
dwarfspell = 1
commandDoSpell(GetDefenderHero(),235,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = 2
elseif dwarfspell == 2 then
dwarfspell = 3
commandDoSpell(GetDefenderHero(),43,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = 4
elseif dwarfspell == 4 then
dwarfspell = 5
commandDoSpell(GetDefenderHero(), 221,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = 6
elseif dwarfspell == 6 then
dwarfspell = 7
commandDoSpell(GetDefenderHero(),216,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = 8
elseif dwarfspell == 8 then
dwarfspell = 9
commandDoSpell(GetDefenderHero(),212,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = 10
elseif dwarfspell > 9 and dwarfspell < 16 then
dwarfspell = dwarfspell + 1
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = dwarfspell + 1
elseif dwarfspell == 16 then
dwarfspell = 17
commandDoSpell(GetDefenderHero(),210,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
dwarfspell = 0
end
end
end
sleep(4)
function DefenderCreatureDeath(hero)
if IsHuman(DEFENDER) == nil then
if dwarf == 0 then
dwarf = 1
playAnimation(GetDefenderHero(), "cast", ONESHOT)
combatSetPause(1)
sleep(60)
SummonCreature(DEFENDER, CREATURE_DEFENDER, 900 * diff, 15, -1)
sleep(70)
dwarf = 2
combatSetPause(nil)
elseif dwarf == 2 then
dwarf = 3
playAnimation(GetDefenderHero(), "cast", ONESHOT)
combatSetPause(1)
sleep(60)
SummonCreature(DEFENDER, CREATURE_WARLORD, 45 * diff, 15, -1)
sleep(70)
dwarf = 4
combatSetPause(nil)
elseif dwarf == 4 then
dwarf = 5
playAnimation(GetDefenderHero(), "cast", ONESHOT)
combatSetPause(1)
sleep(60)
SummonCreature(DEFENDER, CREATURE_MAGMA_DRAGON, 20 * diff, 15, -1)
sleep(70)
dwarf = 6
combatSetPause(nil)
end
end
end
function finish()
while 1 do
local att, def = GetAttackerCreatures(), GetDefenderCreatures()
if length(att) == 0 or length(def) == 0 then
if dwarf == 6 then
EnableAutoFinish(1)
break
end
end
sleep(20)
end
end
--start of the attacking script
function AttackerHeroMove(hero)
if IsHuman(ATTACKER) == nil then
if atdwarfspell == 0 then
atdwarfspell = 1
commandDoSpell(GetAttackerHero(),221,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = 2
elseif dwarfspell == 2 then
atdwarfspell = 3
commandDoSpell(GetAttackerHero(),216,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = 4
elseif dwarfspell == 4 then
atdwarfspell = 5
commandDoSpell(GetAttackerHero(),235,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = 6
elseif dwarfspell == 6 then
atdwarfspell = 7
commandDoSpell(GetAttackerHero(),212,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = 8
elseif dwarfspell == 8 then
atdwarfspell = 9
commandDoSpell(GetAttackerHero(),43,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = 10
elseif dwarfspell > 9 and dwarfspell < 16 then
atdwarfspell = dwarfspell + 1
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = dwarfspell + 1
elseif dwarfspell == 16 then
atdwarfspell = 17
commandDoSpell(GetAttackerHero(),210,-1)
SetUnitManaPoints(hero, GetUnitManaPoints(hero) + 4 * diff )
atdwarfspell = 0
end
end
end
sleep(4)
function AttackerCreatureDeath()
if IsHuman(ATTACKER) == nil then
if atdwarf == 0 then
atdwarf = 1
playAnimation(GetAttackerHero(), "cast", ONESHOT)
combatSetPause(1)
sleep(60)
SummonCreature(ATTACKER, CREATURE_FLAME_KEEPER, 65 * diff, 15, -1)
sleep(60)
SummonCreature(ATTACKER, CREATURE_STOUT_DEFENDER, 600 * diff, 15, -1)
sleep(70)
atdwarf = 2
combatSetPause(nil)
end
end
end
function atfinish()
while 1 do
local att, def = GetAttackerCreatures(), GetDefenderCreatures()
if length(att) == 0 or length(def) == 0 then
if atdwarf == 2 then
EnableAutoFinish(1)
atdwarf = 0
break
end
end
sleep(20)
end
end
startThread(finish)
startThread(atfinish)