/** ** Check if the spell can be auto cast and cast it. ** ** @param caster Unit who can cast the spell. ** @param spell Spell-type pointer. ** ** @return 1 if spell is casted, 0 if not. */ int AutoCastSpell(CUnit &caster, const SpellType &spell) { // Check for mana and cooldown time, trivial optimization. if (!SpellIsAvailable(*caster.Player, spell.Slot) || caster.Variable[MANA_INDEX].Value < spell.ManaCost || caster.SpellCoolDownTimers[spell.Slot]) { return 0; } Target *target = SelectTargetUnitsOfAutoCast(caster, spell); if (target == NULL) { return 0; } else { // Save previous order COrder *savedOrder = NULL; if (caster.CurrentAction() != UnitActionStill && caster.CanStoreOrder(caster.CurrentOrder())) { savedOrder = caster.CurrentOrder()->Clone(); } // Must move before ? CommandSpellCast(caster, target->targetPos, target->Unit, spell, FlushCommands); delete target; if (savedOrder != NULL) { caster.SavedOrder = savedOrder; } } return 1; }
/** ** Check what computer units can do with magic. ** In fact, turn on autocast for AI. */ void AiCheckMagic() { CPlayer &player = *AiPlayer->Player; const int n = player.GetUnitCount(); for (int i = 0; i < n; ++i) { CUnit &unit = player.GetUnit(i); if (unit.Type->CanCastSpell) { // Check only idle magic units for (size_t i = 0; i != unit.Orders.size(); ++i) { if (unit.Orders[i]->Action == UnitActionSpellCast) { return; } } for (unsigned int j = 0; j < SpellTypeTable.size(); ++j) { // Check if we can cast this spell. SpellIsAvailable checks for upgrades. if (unit.Type->CanCastSpell[j] && SpellIsAvailable(player, j) && SpellTypeTable[j]->AICast) { if (AutoCastSpell(unit, *SpellTypeTable[j])) { break; } } } } } }
/** ** Check what computer units can do with magic. ** In fact, turn on autocast for AI. */ void AiCheckMagic(void) { const int n = AiPlayer->Player->TotalNumUnits; CUnit **units = AiPlayer->Player->Units; const CPlayer *player = AiPlayer->Player; /*units[0]->Player */ for (int i = 0; i < n; ++i) { CUnit &unit = *units[i]; // Check only magic units if (unit.Type->CanCastSpell) { for (unsigned int j = 0; j < SpellTypeTable.size(); ++j) { // Check if we can cast this spell. SpellIsAvailable checks for upgrades. if (unit.Type->CanCastSpell[j] && SpellIsAvailable(player, j) && (SpellTypeTable[j]->AutoCast || SpellTypeTable[j]->AICast)) { AutoCastSpell(unit, SpellTypeTable[j]); } } } } }