Advertisement
Guest User

Eco Fire Barrel Mod Source Code

a guest
Apr 1st, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.94 KB | None | 0 0
  1. namespace Eco.Mods.TechTree {
  2.     using System;
  3.     using System.Collections.Generic;
  4.     using Eco.Gameplay.Components;
  5.     using Eco.Gameplay.Interactions;
  6.     using Eco.Gameplay.Items;
  7.     using Eco.Gameplay.Objects;
  8.     using Eco.Gameplay.Pipes.Gases;
  9.     using Eco.Gameplay.Pipes.LiquidComponents;
  10.     using Eco.Gameplay.Skills;
  11.     using Eco.Gameplay.Systems.TextLinks;
  12.     using Eco.Gameplay.Systems.Tooltip;
  13.     using Eco.Shared.Math;
  14.     using Eco.Shared.Serialization;
  15.     using Eco.Shared.Utils;
  16.  
  17.     [Serialized]
  18.     [RequireComponent(typeof(AttachmentComponent))]
  19.     [RequireComponent(typeof(MinimapComponent))]
  20.     [RequireComponent(typeof(PipeComponent))]
  21.     [RequireComponent(typeof(FuelSupplyComponent))]
  22.     [RequireComponent(typeof(FuelConsumptionComponent))]
  23.     [RequireComponent(typeof(AirPollutionComponent))]
  24.  
  25.     public partial class FireBarrelObject : WorldObject {
  26.         public override string FriendlyName { get { return "Fire Barrel"; } }
  27.  
  28.         static FireBarrelObject() {
  29.             AddOccupancyList(typeof(FireBarrelObject), new BlockOccupancy(new Vector3i(0, 0, 0), typeof(WorldObjectBlock), new Quaternion(0f, 0f, 0f, 1f)));
  30.         }
  31.  
  32.         private static Type[] fuelTypeList = new Type[]
  33.         {
  34.             typeof(ArrowItem),
  35.             typeof(BoardItem),
  36.             typeof(CharcoalItem),
  37.             typeof(CoalItem),
  38.             typeof(LogItem),
  39.             typeof(LumberItem),
  40.         };
  41.  
  42.         [Serialized] private bool fireEnabled = false;
  43.         [Serialized] private float garbageWeightFloat = 0f;
  44.  
  45.         protected override void Initialize() {
  46.             this.GetComponent<MinimapComponent>().Initialize("Fire Barrel");
  47.             this.GetComponent<FuelSupplyComponent>().Initialize(4, fuelTypeList);
  48.             this.GetComponent<FuelConsumptionComponent>().Initialize(20f);
  49.             this.GetComponent<AirPollutionComponent>().Initialize(0.1f);
  50.  
  51.             BlockOccupancy thisBlock = this.Occupancy.Find(x => x.Name == "ChimneyOut");
  52.             thisBlock.Offset = new Vector3i(0, 2, 0); //offset of chimney output
  53.             thisBlock.BlockType = typeof(WorldObjectBlock);
  54.  
  55.             List<LiquidTank> tankList = new List<LiquidTank> {
  56.                 new LiquidProducer("Chimney", typeof(SmogItem), 100, null, thisBlock, (float)(0.25f * SmogItem.SmogItemsPerCO2PPM) / TimeUtil.SecondsPerHour)
  57.             };
  58.  
  59.             this.GetComponent<PipeComponent>().Initialize(tankList);
  60.         }
  61.        
  62.         public override void Destroy() {
  63.             base.Destroy();
  64.         }
  65.  
  66.         public override void Tick() {
  67.             base.Tick();
  68.  
  69.             if (CheckFireEnabled()) { //check if there's fuel and garbage in the barrel, set animation state, set operating state
  70.                 garbageWeightFloat -= 2.0f;
  71.             }
  72.         }
  73.  
  74.         public override InteractResult OnActRight(InteractionContext context) {
  75.             if (context.Parameters != null && context.Parameters.ContainsKey("TakeButton")) {
  76.                 TakeBarrel(context); //add fuel inventory to player, give them the barrel
  77.                 return InteractResult.Success;
  78.             } else {
  79.                 ShowInfo(context); //show garbage weight
  80.                 return InteractResult.Success;
  81.             }
  82.         }
  83.  
  84.         public override InteractResult OnActLeft(InteractionContext context) {
  85.             if (context.SelectedItem != null) {
  86.                 string currentItemName = context.SelectedItem.FriendlyName;
  87.  
  88.                 bool stackMode = CheckStackMode(context.Player.User.Inventory.Toolbar.SelectedStack.Weight); //if garbage barrel + selected stack weight is less than barrel max weight
  89.  
  90.                 if ((garbageWeightFloat / 1000f) < 9.9) //this check is run here instead of AddTrash so the weight can go over the max weight by 1 item
  91.                     AddTrash(context, currentItemName, stackMode); //add to garbageWeight, notify player they threw x item(s) in trash, remove item(s)
  92.                 else
  93.                     context.Player.SendTemporaryErrorLoc("The barrel is full.");
  94.  
  95.                 return InteractResult.Success;
  96.             }
  97.  
  98.             return InteractResult.NoOp;
  99.         }
  100.  
  101.         private void AddTrash(InteractionContext context, string currentItemName, bool stackMode) {
  102.             if (stackMode) {
  103.                 garbageWeightFloat += context.Player.User.Inventory.Toolbar.SelectedStack.Weight;
  104.  
  105.                 context.Player.SendTemporaryMessageLoc("You threw " + context.Player.User.Inventory.Toolbar.SelectedStack.Quantity.ToString() + " " + currentItemName + " into the fire barrel.");
  106.                 context.Player.User.Inventory.TryRemoveItems(context.Player.User.Inventory.Toolbar.SelectedStack);
  107.             } else {
  108.                 garbageWeightFloat += context.SelectedItem.Weight;
  109.  
  110.                 context.Player.SendTemporaryMessageLoc("You threw " + currentItemName + " into the fire barrel.");
  111.                 context.Player.User.Inventory.TryRemoveItem(context.SelectedItem.Type);
  112.             }
  113.         }
  114.  
  115.         private bool CheckStackMode(float selectedStackWeight) {
  116.             bool stackMode = false;
  117.  
  118.             if (garbageWeightFloat / 1000f + selectedStackWeight / 1000f < 9.9)
  119.                 stackMode = true;
  120.             else
  121.                 stackMode = false;
  122.  
  123.             return stackMode;
  124.         }
  125.  
  126.         private void TakeBarrel(InteractionContext context) {
  127.             if (garbageWeightFloat < 0.01f)
  128.                 AddItemsFromBarrel(context); //Make sure the items can be added
  129.             else
  130.                 context.Player.SendTemporaryErrorLoc("You can not pick up the barrel with trash already in it.");
  131.         }
  132.  
  133.         private void AddItemsFromBarrel(InteractionContext context) {
  134.             FuelSupplyComponent fuelSupply = this.GetComponent<FuelSupplyComponent>();
  135.  
  136.             bool addLog = false;
  137.             bool addLumber = false;
  138.             bool addArrow = false;
  139.             bool addBoard = false;
  140.             bool addCharcoal = false;
  141.             bool addCoal = false;
  142.  
  143.             if (context.Player.User.Inventory.TryAddItems(typeof(LogItem), fuelSupply.Inventory.TotalNumberOfItems<LogItem>())) {
  144.                 fuelSupply.Inventory.RemoveItems<LogItem>(fuelSupply.Inventory.TotalNumberOfItems<LogItem>());
  145.                 addLog = true;
  146.             } else {
  147.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the logs.");
  148.             }
  149.  
  150.             if (context.Player.User.Inventory.TryAddItems(typeof(LumberItem), fuelSupply.Inventory.TotalNumberOfItems<LumberItem>())) {
  151.                 fuelSupply.Inventory.RemoveItems<LumberItem>(fuelSupply.Inventory.TotalNumberOfItems<LumberItem>());
  152.                 addLumber = true;
  153.             } else {
  154.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the lumber.");
  155.             }
  156.  
  157.             if (context.Player.User.Inventory.TryAddItems(typeof(ArrowItem), fuelSupply.Inventory.TotalNumberOfItems<ArrowItem>())) {
  158.                 fuelSupply.Inventory.RemoveItems<ArrowItem>(fuelSupply.Inventory.TotalNumberOfItems<ArrowItem>());
  159.                 addArrow = true;
  160.             } else {
  161.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the arrows.");
  162.             }
  163.  
  164.             if (context.Player.User.Inventory.TryAddItems(typeof(BoardItem), fuelSupply.Inventory.TotalNumberOfItems<BoardItem>())) {
  165.                 fuelSupply.Inventory.RemoveItems<BoardItem>(fuelSupply.Inventory.TotalNumberOfItems<BoardItem>());
  166.                 addBoard = true;
  167.             } else {
  168.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the boards.");
  169.             }
  170.  
  171.             if (context.Player.User.Inventory.TryAddItems(typeof(CharcoalItem), fuelSupply.Inventory.TotalNumberOfItems<CharcoalItem>())) {
  172.                 fuelSupply.Inventory.RemoveItems<CharcoalItem>(fuelSupply.Inventory.TotalNumberOfItems<CharcoalItem>());
  173.                 addCharcoal = true;
  174.             } else {
  175.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the charcoal.");
  176.             }
  177.  
  178.             if (context.Player.User.Inventory.TryAddItems(typeof(CoalItem), fuelSupply.Inventory.TotalNumberOfItems<CoalItem>())) {
  179.                 fuelSupply.Inventory.RemoveItems<CoalItem>(fuelSupply.Inventory.TotalNumberOfItems<CoalItem>());
  180.                 addCoal = true;
  181.             } else {
  182.                 context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the coal.");
  183.             }
  184.  
  185.             if (addLog && addLumber && addArrow && addBoard && addCharcoal && addCoal) {
  186.                 if (context.Player.User.Inventory.TryAddItem(typeof(FireBarrelItem))) {
  187.                     this.Destroy();
  188.                 } else {
  189.                     context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up the fire barrel.");
  190.                 }
  191.             }
  192.         }
  193.        
  194.         private void ShowInfo(InteractionContext context) {
  195.             float garbagePercent = ((garbageWeightFloat / 1000f) / 10.0f) * 100;
  196.             garbagePercent = (float) Math.Round(garbagePercent, 2);
  197.  
  198.             context.Player.OpenInfoPanel("Fire Barrel Info", (GetColor(garbagePercent) + "<size=50>" + garbagePercent.ToString() + "% full</size>"));
  199.         }
  200.  
  201.         private bool CheckFireEnabled() {
  202.             FuelSupplyComponent fuelSupplyComponent = this.GetComponent<FuelSupplyComponent>();
  203.             float fuelSupply = fuelSupplyComponent.Energy;
  204.  
  205.             if (fuelSupply > 0 && (garbageWeightFloat) > 0) {
  206.                 fireEnabled = true;
  207.             } else if (garbageWeightFloat < 0) {
  208.                 garbageWeightFloat = 0f; //lazy fix for negative values
  209.                 fireEnabled = false;
  210.             } else
  211.                 fireEnabled = false;
  212.  
  213.             SetAnimatedState("FireBarrel", fireEnabled);
  214.             this.operating = fireEnabled;
  215.             return fireEnabled;
  216.         }
  217.  
  218.         private string GetColor(float garbagePercent) {
  219.             string colorString = "";
  220.  
  221.             if (garbagePercent < 25)
  222.                 colorString = "<color=#ffffff>";
  223.             else if (garbagePercent >= 25 && garbagePercent < 50)
  224.                 colorString = "<color=#33cc33>";
  225.             else if (garbagePercent >= 50 && garbagePercent < 75)
  226.                 colorString = "<color=#ffff00>";
  227.             else if (garbagePercent >= 75 && garbagePercent < 100)
  228.                 colorString = "<color=#ff6600>";
  229.             else if (garbagePercent >= 100)
  230.                 colorString = "<color=#ff0000>";
  231.  
  232.             return colorString;
  233.         }
  234.     }
  235.  
  236.     [Serialized]
  237.     public partial class FireBarrelItem : WorldObjectItem<FireBarrelObject> {
  238.         public override string FriendlyName { get { return "Fire Barrel"; } }
  239.         public override string Description { get { return "A barrel you can burn trash items in."; } }
  240.  
  241.         static FireBarrelItem() {
  242.  
  243.         }
  244.  
  245.     }
  246.  
  247.     [RequiresSkill(typeof(MetalworkingSkill), 1)]
  248.     public partial class FireBarrelRecipe : Recipe {
  249.         public FireBarrelRecipe() {
  250.             this.Products = new CraftingElement[]
  251.             {
  252.                 new CraftingElement<FireBarrelItem>(),
  253.             };
  254.  
  255.             this.Ingredients = new CraftingElement[]
  256.             {
  257.                 new CraftingElement<IronIngotItem>(typeof(MetalworkingEfficiencySkill), 25, MetalworkingEfficiencySkill.MultiplicativeStrategy),
  258.             };
  259.  
  260.             this.CraftMinutes = CreateCraftTimeValue(typeof(FireBarrelRecipe), Item.Get<FireBarrelItem>().UILink(), 5.0f, typeof(MetalworkingSpeedSkill));
  261.             this.Initialize("Fire Barrel", typeof(FireBarrelRecipe));
  262.             CraftingComponent.AddRecipe(typeof(AnvilObject), this);
  263.         }
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement