Example #1
0
        public static async Task Act(Unit unit, Battlefield battlefield, List <Unit> enemies1, List <Unit> enemies2)
        {
            _logger.LogDebug(unit.ID + " act (" + unit.Healthbar + " " + unit.Speed + ") " + unit.Name);

            if (unit.Healthbar > 0)
            {
                List <Unit> enemies = new List <Unit>();
                List <Unit> allies  = new List <Unit>();
                if (unit.Owner <= 3)
                {
                    enemies = enemies1;
                    allies  = enemies2;
                }
                else
                {
                    enemies = enemies2;
                    allies  = enemies1;
                }

                await AbilityService.UseAbilities(unit, battlefield, enemies, allies);

                if (unit.Target != null && unit.Target.Healthbar > 0)
                {
                    await AbilityService.TriggerAbilities(unit, unit.Target, UnitAbilityTrigger.EnemyInVision, battlefield, enemies);

                    await FightService.Fight(unit, unit.Target, battlefield, enemies);
                }
                else
                {
                    KeyValuePair <Unit, Unit> myenemy = EnemyinRange(unit, enemies);

                    if (myenemy.Key.Name != null)
                    {
                        await AbilityService.TriggerAbilities(unit, myenemy.Key, UnitAbilityTrigger.EnemyInVision, battlefield, enemies);

                        unit.Target = myenemy.Key;
                        await FightService.Fight(unit, myenemy.Key, battlefield, enemies);
                    }
                    else
                    {
                        if (myenemy.Value.Name != null)
                        {
                            await AbilityService.TriggerAbilities(unit, myenemy.Value, UnitAbilityTrigger.EnemyInVision, battlefield, enemies);
                        }
                        await MoveService.Move(unit, myenemy.Value, battlefield);
                    }
                }
            }
            unit.Path.Add(new KeyValuePair <float, float>(unit.RelPos.Key, unit.RelPos.Value));
            Interlocked.Increment(ref battlefield.Done);
            _logger.LogDebug(unit.ID + " act done (" + unit.Healthbar + " " + unit.Speed + ") " + unit.Name);
        }
Example #2
0
        public static async Task Fight(Unit unit, Unit vs, Battlefield battlefield, List <Unit> enemies, float damage = 0, float dur = 0.25f)
        {
            if (vs.Healthbar > 0)
            {
                _logger.LogDebug(unit.ID + " fighting " + vs.ID);

                float attacdamage = unit.Attacdamage;
                if (unit.Bonusdamage != null && vs.Attributes.Contains(unit.Bonusdamage.Attribute))
                {
                    attacdamage += unit.Bonusdamage.Damage;
                }

                if (damage != 0)
                {
                    attacdamage /= damage;
                }
                else
                {
                    await AbilityService.TriggerAbilities(unit, vs, UnitAbilityTrigger.FightStart, battlefield, enemies);

                    attacdamage = unit.Attacdamage;
                    if (unit.Bonusdamage != null && vs.Attributes.Contains(unit.Bonusdamage.Attribute))
                    {
                        attacdamage += unit.Bonusdamage.Damage;
                    }
                    if (unit.Areadamage != null)
                    {
                        await FightArea(unit, vs, battlefield, enemies);
                    }
                    await AbilityService.TriggerAbilities(unit, vs, UnitAbilityTrigger.FightEnd, battlefield, enemies);
                }
                if (unit.Healthbar == 0)
                {
                    return;
                }

                float attacspeed       = unit.Attacspeed;
                float enemyarmor       = vs.Armor;
                float enemyshieldarmor = vs.ShieldArmor;

                float attacs = 1;
                if (attacspeed > 0)
                {
                    attacs = dur / attacspeed;
                }
                float restdps = 0;

                for (int i = 0; i < unit.Attacs; i++)
                {
                    if (vs.Healthbar == 0)
                    {
                        break;
                    }
                    bool hasShield = false;
                    if (vs.Shieldbar > 0)
                    {
                        hasShield = true;
                        float smitigation = attacs * vs.ShieldArmor;
                        float sdps        = attacs * attacdamage - smitigation;
                        if (sdps <= 0)
                        {
                            sdps = 0;
                        }
                        float snewhp = vs.Shieldbar - sdps;
                        if (snewhp < 0)
                        {
                            restdps   = snewhp * -1;
                            hasShield = false;
                            snewhp    = 0;
                        }
                        vs.Shieldbar = snewhp;

                        // TODO: Move to Fightstart Abilities
                        UnitAbility shield = vs.Abilities.SingleOrDefault(x => x.Ability == UnitAbilities.ShieldRegeneration);
                        if (shield != null)
                        {
                            shield.Cooldown = AbilityPool.Abilities.SingleOrDefault(x => x.Ability == UnitAbilities.ShieldRegeneration).Cooldown;
                        }

                        unit.DamageDone += sdps;
                    }

                    if (hasShield == false)
                    {
                        float mydps = attacdamage;
                        if (restdps > 0)
                        {
                            mydps = restdps;
                        }

                        float mitigation = attacs * enemyarmor;
                        float dps        = attacs * mydps - mitigation;
                        if (dps <= 0)
                        {
                            dps = 0;
                        }
                        float newhp = vs.Healthbar - dps;

                        if (newhp <= 0)
                        {
                            newhp       = 0;
                            unit.Target = null;

                            lock (battlefield.KilledUnits)
                            {
                                if (!battlefield.KilledUnits.Contains(vs))
                                {
                                    battlefield.KilledUnits.Add(vs);
                                    unit.Kills++;
                                    unit.MineralValueKilledRound += vs.Cost;
                                    AbilityService.TriggerAbilities(vs, unit, UnitAbilityTrigger.OnDeath, battlefield, enemies);
                                }
                            }
                            battlefield.UnitPostions.TryRemove(vs.Pos, out _);
                            _logger.LogDebug(vs.ID + " died (killer: " + unit.ID + ")");
                        }
                        vs.Healthbar = newhp;

                        unit.DamageDoneRound += dps;
                    }
                }
            }
        }