Advertisement
AviEzerzer

Revised code of OnlyJoe by FullCommit #4298

Nov 7th, 2020
3,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 KB | None | 0 0
  1. // Copyright (c) Strange Loop Games. All rights reserved.
  2. // See LICENSE file in the project root for full license information.
  3.  
  4. namespace Eco.Mods.TechTree
  5. {
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.ComponentModel;
  9.     using System.Linq;
  10.     using Eco.Core.Utils;
  11.     using Eco.Gameplay.DynamicValues;
  12.     using Eco.Gameplay.GameActions;
  13.     using Eco.Gameplay.Interactions;
  14.     using Eco.Gameplay.Items;
  15.     using Eco.Gameplay.Players;
  16.     using Eco.Gameplay.Systems.TextLinks;
  17.     using Eco.Gameplay.Systems.Tooltip;
  18.     using Eco.Gameplay.Objects;
  19.     using Eco.Gameplay.Skills;
  20.     using Eco.Shared.Items;
  21.     using Eco.Shared.Localization;
  22.     using Eco.Shared.Utils;
  23.     using Eco.World;
  24.     using Eco.World.Blocks;
  25.     using Eco.Gameplay.Objects;
  26.     using Eco.Core.Items;
  27.     using Eco.Shared.Math;
  28.  
  29.     [Category("Hidden"), Tag("Excavation")]
  30.     public partial class PickaxeItem : ToolItem
  31.     {
  32.         private static SkillModifiedValue caloriesBurn = CreateCalorieValue(20, typeof(MiningSkill), typeof(PickaxeItem), new PickaxeItem().UILink());
  33.         static PickaxeItem() { }
  34.    
  35.     public override IDynamicValue CaloriesBurn                  => caloriesBurn;
  36.  
  37.         public override ClientPredictedBlockAction LeftAction       => ClientPredictedBlockAction.Mine;
  38.         public override LocString LeftActionDescription             => Localizer.DoStr("Mine");
  39.  
  40.         public override Item RepairItem                             => Item.Get<StoneItem>();
  41.         public virtual float Damage                                 => 1.0f;
  42.         public override int FullRepairAmount                        => 1;
  43.  
  44.         [Tooltip(200)]
  45.         public TooltipSection MinablesTooltip(TooltipContext context)
  46.         {
  47.             var myHardness = this.Tier.GetCurrentValue(context.Player.User);
  48.             var minableBlockTypes = Block.BlockTypesWithAttribute(typeof(Minable)).Select(x => new KeyValuePair<Type, float>(x, Block.Get<Minable>(x).Hardness)).ToList();
  49.  
  50.             if (!minableBlockTypes.Any()) return null;
  51.  
  52.             var allBlocks = AllItems.OfType<BlockItem>();
  53.  
  54.             var resList = new List<LocString>();
  55.             minableBlockTypes.OrderBy(item => item.Value).ForEach(x =>
  56.             {
  57.                 var targetItem = allBlocks.FirstOrDefault(item => item.OriginType == x.Key);
  58.                 var hitCount = (int)Math.Ceiling(x.Value / myHardness);
  59.                 if (targetItem != null) resList.Add(new LocString(string.Format("{0}: {1} {2}", targetItem.UILink(), hitCount, "hit".Pluralize(hitCount))));
  60.             });
  61.  
  62.             return new TooltipSection(Localizer.DoStr("Can mine"), resList.FoldoutListLoc("item", context.Origin));
  63.         }
  64.  
  65.         public override InteractResult OnActLeft(InteractionContext context)
  66.         {
  67.             if (context.HasBlock && context.Block.Is<Minable>())
  68.             {
  69.                 var user = context.Player.User;
  70.                 var item = context.Block is IRepresentsItem ? Item.Get((IRepresentsItem)context.Block) : null;
  71.  
  72.                 var totalDamageToTarget = user.BlockHitCache.MemorizeHit(context.Block.GetType(), context.BlockPosition.Value, this.Tier.GetCurrentValue(context.Player.User) * this.Damage);
  73.                 if (context.Block.Get<Minable>().Hardness <= totalDamageToTarget)
  74.                 {
  75.                     var result = AtomicActions.DeleteBlockNow(this.CreateMultiblockContext(context), spawnRubble: false);
  76.  
  77.                     //Spawn the rubble if needed
  78.                     if (result.Success)
  79.                     {  
  80.                         Skill miningSkill = context.Player.User.Skillset.GetSkill(typeof(MiningSkill));
  81.                         bool didPickup = false;
  82.            
  83.                         //test for skill and auto pickup rubble
  84.                         if (miningSkill != null && miningSkill.Level >= 7)
  85.                         {
  86.                             BlockItem blockItem = BlockItem.GetBlockItem(context.Block.GetType());
  87.                             Type itemType = blockItem.GetType();
  88.                             var pickupResult = context.Player.User.Inventory.TryAddItems(itemType, 4);
  89.                             didPickup = pickupResult.Success;
  90.                         }
  91.  
  92.                         if (!didPickup) {
  93.                             //var forced = context.Player.User.Talentset.HasTalent(typeof(MiningLuckyBreakTalent)) ? 4 : -1;
  94.                             var forced = (miningSkill != null && miningSkill.Level >= 6) ? 4 : -1;
  95.  
  96.                             if (RubbleObject.TrySpawnFromBlock(context.Player, context.Block.GetType(), context.BlockPosition.Value, forced))
  97.                             {
  98.                                 var addition = item != null ? " " + item.UILink() : string.Empty;
  99.                                 this.AddExperience(user, 1f, new LocString(Localizer.Format("mining") + addition));
  100.                                 user.UserUI.OnCreateRubble.Invoke(item.DisplayName.NotTranslated);
  101.                                 user.BlockHitCache.ForgetHit(context.BlockPosition.Value);
  102.                             }
  103.                         } else {
  104.                             user.BlockHitCache.ForgetHit(context.BlockPosition.Value);
  105.                         }
  106.                     }
  107.  
  108.                     return (InteractResult)result;
  109.                 }else{
  110.                     return (InteractResult) AtomicActions.UseToolNow(this.CreateMultiblockContext(context));
  111.                 }
  112.             }else if (context.Target is RubbleObject)
  113.             {
  114.                 var rubble = (RubbleObject)context.Target;
  115.                 if (rubble.IsBreakable)
  116.                 {
  117.                     using var pack = new GameActionPack();
  118.                     pack.UseTool(this.CreateMultiblockContext(context));
  119.                     pack.AddPostEffect(() => rubble.Breakup(context.Player));
  120.                     return (InteractResult)pack.TryPerform(false);
  121.                 }
  122.                
  123.                 return InteractResult.NoOp;
  124.             }
  125.  
  126.             if (context.Target is WorldObject) return this.BasicToolOnWorldObjectCheck(context);
  127.             return base.OnActLeft(context);
  128.         }
  129.  
  130.         public override bool ShouldHighlight(Type block)
  131.         {
  132.             return Block.Is<Minable>(block);
  133.         }
  134.  
  135.         public override bool CanPickUpItemStack(ItemStack stack)
  136.         {
  137.             var blockitem = stack.Item as BlockItem;
  138.             return stack.Item.IsCarried && blockitem != null && Block.Get<Minable>(blockitem.OriginType) != null;
  139.         }
  140.     }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement