public void DurabilityService_SetDurability_ShouldSetToSpecifiedValue()
        {
            INWScript          script  = Substitute.For <INWScript>();
            IColorTokenService color   = Substitute.For <IColorTokenService>();
            DurabilityService  service = new DurabilityService(script, color);
            float  value = 0.0f;
            NWItem item  = Substitute.For <NWItem>(script, service);

            item.BaseItemType.Returns(x => BASE_ITEM_LONGSWORD);
            item.When(x => x.SetLocalFloat("DURABILITY_CURRENT", Arg.Any <float>()))
            .Do(x => value = x.ArgAt <float>(1));

            service.SetDurability(item, 12.52f);
            Assert.AreEqual(12.52f, value);
        }
        public void DurabilityService_SetDurability_ShouldSetToDefaultValue()
        {
            INWScript          script  = Substitute.For <INWScript>();
            IColorTokenService color   = Substitute.For <IColorTokenService>();
            DurabilityService  service = new DurabilityService(script, color);
            float  value = 0.0f;
            NWItem item  = Substitute.For <NWItem>(script, service);

            item.BaseItemType.Returns(x => BASE_ITEM_LONGSWORD);
            item.When(x => x.SetLocalFloat(Arg.Any <string>(), Arg.Any <float>()))
            .Do(x => value = x.ArgAt <float>(1));

            service.SetDurability(item, -50.0f);
            Assert.AreEqual(0.0f, value);
        }
        public void DurabilityService_SetDurability_InvalidType_ShouldNotRunOnce()
        {
            INWScript          script  = Substitute.For <INWScript>();
            IColorTokenService color   = Substitute.For <IColorTokenService>();
            DurabilityService  service = new DurabilityService(script, color);
            bool   ranOnce             = false;
            NWItem item = Substitute.For <NWItem>(script, service);

            item.BaseItemType.Returns(x => BASE_ITEM_BLANK_SCROLL);
            item.When(x => x.SetLocalFloat(Arg.Any <string>(), Arg.Any <float>()))
            .Do(x => ranOnce = true);

            service.SetDurability(item, 999.0f);
            Assert.AreEqual(false, ranOnce);
        }
Exemple #4
0
        public void Apply(NWPlayer player, NWItem target, params string[] args)
        {
            var maxDurability = DurabilityService.GetMaxDurability(target);
            var curDurability = DurabilityService.GetDurability(target);

            int   value    = Convert.ToInt32(args[0]);
            float newValue = maxDurability + value;

            if (newValue > 100)
            {
                newValue = 100;
            }
            maxDurability  = newValue;
            curDurability += value;

            DurabilityService.SetMaxDurability(target, maxDurability);
            DurabilityService.SetDurability(target, curDurability);
        }
Exemple #5
0
        public bool Run(params object[] args)
        {
            NWCreature      attacker    = (_.GetLastDamager(Object.OBJECT_SELF));
            NWPlaceable     tower       = (Object.OBJECT_SELF);
            NWItem          weapon      = (_.GetLastWeaponUsed(attacker.Object));
            int             damage      = _.GetTotalDamageDealt();
            var             structureID = tower.GetLocalString("PC_BASE_STRUCTURE_ID");
            PCBaseStructure structure   = DataService.Single <PCBaseStructure>(x => x.ID == new Guid(structureID));
            int             maxShieldHP = BaseService.CalculateMaxShieldHP(structure);
            PCBase          pcBase      = DataService.Get <PCBase>(structure.PCBaseID);
            var             playerIDs   = DataService.Where <PCBasePermission>(x => x.PCBaseID == structure.PCBaseID &&
                                                                               !x.IsPublicPermission)
                                          .Select(s => s.PlayerID);
            var      toNotify = NWModule.Get().Players.Where(x => playerIDs.Contains(x.GlobalID));
            DateTime timer    = DateTime.UtcNow.AddSeconds(30);
            string   clock    = timer.ToString(CultureInfo.InvariantCulture);
            string   sector   = BaseService.GetSectorOfLocation(attacker.Location);

            if (DateTime.UtcNow <= DateTime.Parse(clock))
            {
                foreach (NWPlayer player in toNotify)
                {
                    player.SendMessage("Your base in " + attacker.Area.Name + " " + sector + " is under attack!");
                }
            }

            // Apply damage to the shields. Never fall below 0.
            pcBase.ShieldHP -= damage;
            if (pcBase.ShieldHP <= 0)
            {
                pcBase.ShieldHP = 0;
            }

            // Calculate the amount of shield percentage remaining. If the tower is in reinforced mode, HP
            // will always be set back to 25% of shields.
            float hpPercentage = (float)pcBase.ShieldHP / (float)maxShieldHP * 100.0f;

            if (hpPercentage <= 25.0f && pcBase.ReinforcedFuel > 0)
            {
                pcBase.IsInReinforcedMode = true;
                pcBase.ShieldHP           = (int)(maxShieldHP * 0.25f);
                hpPercentage = (float)pcBase.ShieldHP / (float)maxShieldHP * 100.0f;
            }

            // Notify the attacker.
            attacker.SendMessage("Tower Shields: " + hpPercentage.ToString("0.00") + "%");
            if (pcBase.IsInReinforcedMode)
            {
                attacker.SendMessage("Control tower is in reinforced mode and cannot be damaged. Reinforced mode will be disabled when the tower runs out of fuel.");
            }

            // HP is tracked in the database. Heal the placeable so it doesn't get destroyed.
            _.ApplyEffectToObject(DURATION_TYPE_INSTANT, _.EffectHeal(9999), tower.Object);

            var durability = DurabilityService.GetDurability(weapon) - RandomService.RandomFloat(0.01f, 0.03f);

            DurabilityService.SetDurability(weapon, durability);

            // If the shields have fallen to zero, the tower will begin to take structure damage.
            if (pcBase.ShieldHP <= 0)
            {
                pcBase.ShieldHP = 0;

                structure.Durability -= RandomService.RandomFloat(0.5f, 2.0f);
                if (structure.Durability < 0.0f)
                {
                    structure.Durability = 0.0f;
                }
                attacker.SendMessage("Structure Durability: " + structure.Durability.ToString("0.00"));

                // If the structure durability drops to zero, destroy everything in the base.
                if (structure.Durability <= 0.0f)
                {
                    structure.Durability = 0.0f;
                    BaseService.ClearPCBaseByID(pcBase.ID, true, false);
                    return(true);
                }
            }

            DataService.SubmitDataChange(pcBase, DatabaseActionType.Update);
            DataService.SubmitDataChange(structure, DatabaseActionType.Update);
            return(true);
        }
        private void RunCreateItem(NWPlayer player)
        {
            foreach (var effect in player.Effects)
            {
                if (_.GetEffectTag(effect) == "CRAFTING_IMMOBILIZATION")
                {
                    _.RemoveEffect(player, effect);
                }
            }

            var model = CraftService.GetPlayerCraftingData(player);

            CraftBlueprint blueprint     = DataService.CraftBlueprint.GetByID(model.BlueprintID);
            BaseStructure  baseStructure = blueprint.BaseStructureID == null ? null : DataService.BaseStructure.GetByID(Convert.ToInt32(blueprint.BaseStructureID));
            PCSkill        pcSkill       = SkillService.GetPCSkill(player, blueprint.SkillID);

            int   pcEffectiveLevel = CraftService.CalculatePCEffectiveLevel(player, pcSkill.Rank, (SkillType)blueprint.SkillID);
            int   itemLevel        = model.AdjustedLevel;
            int   atmosphereBonus  = CraftService.CalculateAreaAtmosphereBonus(player.Area);
            float chance           = CalculateBaseChanceToAddProperty(pcEffectiveLevel, itemLevel, atmosphereBonus);
            float equipmentBonus   = CalculateEquipmentBonus(player, (SkillType)blueprint.SkillID);

            if (chance <= 1.0f)
            {
                player.FloatingText(ColorTokenService.Red("Critical failure! You don't have enough skill to create that item. All components were lost."));
                CraftService.ClearPlayerCraftingData(player, true);
                return;
            }

            int    luckyBonus   = PerkService.GetCreaturePerkLevel(player, PerkType.Lucky);
            var    craftedItems = new List <NWItem>();
            NWItem craftedItem  = (_.CreateItemOnObject(blueprint.ItemResref, player.Object, blueprint.Quantity));

            craftedItem.IsIdentified = true;
            craftedItems.Add(craftedItem);

            // If item isn't stackable, loop through and create as many as necessary.
            if (craftedItem.StackSize < blueprint.Quantity)
            {
                for (int x = 2; x <= blueprint.Quantity; x++)
                {
                    craftedItem = (_.CreateItemOnObject(blueprint.ItemResref, player.Object));
                    craftedItem.IsIdentified = true;
                    craftedItems.Add(craftedItem);
                }
            }

            // Recommended level gets set regardless if all item properties make it on the final product.
            // Also mark who crafted the item. This is later used for display on the item's examination event.
            foreach (var item in craftedItems)
            {
                item.RecommendedLevel = itemLevel < 0 ? 0 : itemLevel;
                item.SetLocalString("CRAFTER_PLAYER_ID", player.GlobalID.ToString());

                BaseService.ApplyCraftedItemLocalVariables(item, baseStructure);
            }

            if (RandomService.Random(1, 100) <= luckyBonus)
            {
                chance += RandomService.Random(1, luckyBonus);
            }

            int successAmount = 0;

            foreach (var component in model.MainComponents)
            {
                var result = RunComponentBonusAttempt(player, component, equipmentBonus, chance, craftedItems);
                successAmount += result.Item1;
                chance         = result.Item2;
            }
            foreach (var component in model.SecondaryComponents)
            {
                var result = RunComponentBonusAttempt(player, component, equipmentBonus, chance, craftedItems);
                successAmount += result.Item1;
                chance         = result.Item2;
            }
            foreach (var component in model.TertiaryComponents)
            {
                var result = RunComponentBonusAttempt(player, component, equipmentBonus, chance, craftedItems);
                successAmount += result.Item1;
                chance         = result.Item2;
            }
            foreach (var component in model.EnhancementComponents)
            {
                var result = RunComponentBonusAttempt(player, component, equipmentBonus, chance, craftedItems);
                successAmount += result.Item1;
                chance         = result.Item2;
            }

            // Structures gain increased durability based on the blueprint
            if (baseStructure != null)
            {
                foreach (var item in craftedItems)
                {
                    var maxDur = DurabilityService.GetMaxDurability(item);
                    maxDur += (float)baseStructure.Durability;
                    DurabilityService.SetMaxDurability(item, maxDur);
                    DurabilityService.SetDurability(item, maxDur);
                }
            }

            player.SendMessage("You created " + blueprint.Quantity + "x " + blueprint.ItemName + "!");
            int   baseXP = 750 + successAmount * RandomService.Random(1, 50);
            float xp     = SkillService.CalculateRegisteredSkillLevelAdjustedXP(baseXP, model.AdjustedLevel, pcSkill.Rank);

            bool exists = DataService.PCCraftedBlueprint.ExistsByPlayerIDAndCraftedBlueprintID(player.GlobalID, blueprint.ID);

            if (!exists)
            {
                xp = xp * 1.50f;
                player.SendMessage("You receive an XP bonus for crafting this item for the first time.");

                var pcCraftedBlueprint = new PCCraftedBlueprint
                {
                    CraftBlueprintID = blueprint.ID,
                    DateFirstCrafted = DateTime.UtcNow,
                    PlayerID         = player.GlobalID
                };

                DataService.SubmitDataChange(pcCraftedBlueprint, DatabaseActionType.Insert);
            }

            SkillService.GiveSkillXP(player, blueprint.SkillID, (int)xp);
            CraftService.ClearPlayerCraftingData(player, true);
            player.SetLocalInt("LAST_CRAFTED_BLUEPRINT_ID_" + blueprint.CraftDeviceID, blueprint.ID);
        }