Exemple #1
0
        public static void Entity(IAttackable target, int totalDamage, Types damageType, int attackerId = 0, double shieldPenetration = 1, int totalAbsDamage = 0, bool direct = false)
        {
            if (target == null)
            {
                return;
            }
            Character attacker = null;

            if (attackerId != 0 && target.Spacemap.Entities.ContainsKey(attackerId))
            {
                attacker = target.Spacemap.Entities[attackerId];
            }

            #region DamageCalculations

            if (target.CurrentShield > 0)
            {
                //if (totalAbsDamage > 0)
                //    totalDamage += totalAbsDamage;
                //For example => Target has 80% abs but you' have moth (+20% penetration) :> damage * 0.6

                var totalAbs = Math.Abs(shieldPenetration - target.ShieldAbsorption);

                if (totalAbs > 0)
                {
                    var _absDmg = (int)(totalAbs * totalAbsDamage);
                    target.CurrentShield -= _absDmg;
                    if (attacker != null)
                    {
                        attacker.CurrentShield += _absDmg;
                    }
                }

                var shieldDamage = totalDamage * totalAbs;
                if (target is Player targetedPlayer && targetedPlayer.Storage.SentinelFortressActive)
                {
                    shieldDamage *= 0.7;
                }

                var healthDamage = 0;
                if (target.CurrentShield <= totalDamage)
                {
                    healthDamage          = Math.Abs(target.CurrentShield - totalDamage);
                    target.CurrentShield  = 0;
                    target.CurrentHealth -= healthDamage;
                }
                else
                {
                    target.CurrentShield -= (int)shieldDamage; //Correspondent shield damage
                    healthDamage          = (int)(totalDamage * (1 - totalAbs));
                }

                if (target.CurrentNanoHull > 0)
                {
                    //If the player can receive some damage on the nanohull
                    if (target.CurrentNanoHull - healthDamage < 0)
                    {
                        var nanoDamage = healthDamage - target.CurrentNanoHull;
                        target.CurrentNanoHull = 0;
                        target.CurrentHealth  -= nanoDamage;
                    }
                    else
                    {
                        target.CurrentNanoHull -= healthDamage;
                    }
                }
                else
                {
                    target.CurrentHealth -= healthDamage; //80% shield abs => 20% life
                }
            }
            else //NO SHIELD
            {
                totalAbsDamage = 0;

                if (target.CurrentNanoHull > 0)
                {
                    //If the player can receive some damage on the nanohull
                    if (target.CurrentNanoHull - totalDamage < 0)
                    {
                        var nanoDamage = totalDamage - target.CurrentNanoHull;
                        target.CurrentNanoHull = 0;
                        target.CurrentHealth  -= nanoDamage;
                    }
                    else
                    {
                        target.CurrentNanoHull -= totalDamage; //Full dmg to nanohull
                    }
                }
                else
                {
                    target.CurrentHealth -= totalDamage; //Full dmg to health
                }
            }

            #endregion

            foreach (var session in AssembleSelectedSessions(target))
            {
                //Todo: fix
                //Packet.Builder.AttackHitCommand(session, attackerId, target, totalDamage + totalAbsDamage, (short)damageType);
            }

            if (target is AttackableAssetCore)
            {
                foreach (var session in target.Spacemap.Entities.Values.Where(x => x is Player))
                {
                    var player      = session as Player;
                    var gameSession = player?.GetGameSession();
                    if (gameSession != null)
                    {
                        //todo: fix
                        //Packet.Builder.AttackHitAssetCommand(gameSession, target.Id, target.CurrentHealth,
                        //    target.MaxHealth);
                    }
                }
            }

            if (target.CurrentHealth <= 0 && target.EntityState == EntityStates.ALIVE)
            {
                target.Destroy(attacker);
            }
            (target as Character)?.Updaters.Update();
        }