Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Eco.Mods.TechTree {
- using System;
- using System.Collections.Generic;
- using Eco.Core.Plugins.Interfaces;
- using Eco.Gameplay.Components;
- using Eco.Gameplay.Components.Auth;
- using Eco.Gameplay.Interactions;
- using Eco.Gameplay.Items;
- using Eco.Gameplay.Objects;
- using Eco.Gameplay.Pipes.Gases;
- using Eco.Gameplay.Pipes.LiquidComponents;
- using Eco.Gameplay.Skills;
- using Eco.Gameplay.Systems.TextLinks;
- using Eco.Gameplay.Systems.Tooltip;
- using Eco.Shared.Math;
- using Eco.Shared.Serialization;
- using Eco.Shared.Utils;
- [Serialized]
- [RequireComponent(typeof(AttachmentComponent))]
- [RequireComponent(typeof(MinimapComponent))]
- [RequireComponent(typeof(PipeComponent))]
- [RequireComponent(typeof(FuelSupplyComponent))]
- [RequireComponent(typeof(FuelConsumptionComponent))]
- [RequireComponent(typeof(AirPollutionComponent))]
- [RequireComponent(typeof(PropertyAuthComponent))]
- public partial class FireBarrelObject : WorldObject, IModKitPlugin {
- public override string FriendlyName { get { return "Fire Barrel"; } }
- public string GetStatus() {
- return String.Empty;
- }
- static FireBarrelObject() {
- AddOccupancyList(typeof(FireBarrelObject), new BlockOccupancy(new Vector3i(0, 0, 0), typeof(WorldObjectBlock), new Quaternion(0f, 0f, 0f, 1f)));
- }
- private static Type[] fuelTypeList = new Type[]
- {
- typeof(ArrowItem),
- typeof(BoardItem),
- typeof(CharcoalItem),
- typeof(CoalItem),
- typeof(LogItem),
- typeof(LumberItem),
- };
- [Serialized] private bool fireEnabled = false;
- [Serialized] private float garbageWeightFloat = 0f;
- protected override void Initialize() {
- this.GetComponent<MinimapComponent>().Initialize("Fire Barrel");
- this.GetComponent<FuelSupplyComponent>().Initialize(4, fuelTypeList);
- this.GetComponent<FuelConsumptionComponent>().Initialize(20f);
- this.GetComponent<AirPollutionComponent>().Initialize(0.1f);
- this.GetComponent<PropertyAuthComponent>().Initialize(AuthModeType.Inherited);
- BlockOccupancy thisBlock = this.Occupancy.Find(x => x.Name == "ChimneyOut");
- thisBlock.Offset = new Vector3i(0, 2, 0); //offset of chimney output
- thisBlock.BlockType = typeof(WorldObjectBlock);
- List<LiquidTank> tankList = new List<LiquidTank> {
- new LiquidProducer("Chimney", typeof(SmogItem), 100, null, thisBlock, (float)(0.25f * SmogItem.SmogItemsPerCO2PPM) / TimeUtil.SecondsPerHour)
- };
- this.GetComponent<PipeComponent>().Initialize(tankList);
- }
- public override void Destroy() {
- base.Destroy();
- }
- public override void Tick() {
- base.Tick();
- if (CheckFireEnabled()) { //check if there's fuel and garbage in the barrel, set animation state, set operating state
- garbageWeightFloat -= 2.0f;
- }
- }
- public override InteractResult OnActRight(InteractionContext context) {
- if (context.Parameters != null && context.Parameters.ContainsKey("TakeButton")) {
- TakeBarrel(context); //add fuel inventory to player, give them the barrel
- return InteractResult.Success;
- } else {
- ShowInfo(context); //show garbage weight
- return InteractResult.Success;
- }
- }
- public override InteractResult OnActLeft(InteractionContext context) {
- if (context.SelectedItem != null) {
- string currentItemName = context.SelectedItem.FriendlyName;
- bool stackMode = CheckStackMode(context.Player.User.Inventory.Toolbar.SelectedStack.Weight); //if garbage barrel + selected stack weight is less than barrel max weight
- 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
- AddTrash(context, currentItemName, stackMode); //add to garbageWeight, notify player they threw x item(s) in trash, remove item(s)
- else
- context.Player.SendTemporaryErrorLoc("The barrel is full.");
- return InteractResult.Success;
- }
- return InteractResult.NoOp;
- }
- private void AddTrash(InteractionContext context, string currentItemName, bool stackMode) {
- if (stackMode) {
- garbageWeightFloat += CheckGarbageWeight(context, true);
- context.Player.SendTemporaryMessageLoc("You threw " + context.Player.User.Inventory.Toolbar.SelectedStack.Quantity.ToString() + " " + currentItemName + " into the fire barrel.");
- context.Player.User.Inventory.TryRemoveItems(context.Player.User.Inventory.Toolbar.SelectedStack);
- } else {
- garbageWeightFloat += CheckGarbageWeight(context, false);
- context.Player.SendTemporaryMessageLoc("You threw " + currentItemName + " into the fire barrel.");
- context.Player.User.Inventory.TryRemoveItem(context.SelectedItem.Type);
- }
- }
- private float CheckGarbageWeight(InteractionContext context, bool stack) {
- float addGarbageWeight = 0.0f;
- if (stack)
- addGarbageWeight = context.Player.User.Inventory.Toolbar.SelectedStack.Weight;
- else
- addGarbageWeight = context.SelectedItem.Weight;
- if (addGarbageWeight == 0.0f)
- addGarbageWeight = 1000f;
- return addGarbageWeight;
- }
- private bool CheckStackMode(float selectedStackWeight) {
- bool stackMode = false;
- if (garbageWeightFloat / 1000f + selectedStackWeight / 1000f < 9.9)
- stackMode = true;
- else
- stackMode = false;
- return stackMode;
- }
- private void TakeBarrel(InteractionContext context) {
- if (garbageWeightFloat < 0.01f)
- AddItemsFromBarrel(context); //Make sure the items can be added
- else
- context.Player.SendTemporaryErrorLoc("You can not pick up the barrel with trash already in it.");
- }
- private void AddItemsFromBarrel(InteractionContext context) {
- FuelSupplyComponent fuelSupply = this.GetComponent<FuelSupplyComponent>();
- bool addLog = false;
- bool addLumber = false;
- bool addArrow = false;
- bool addBoard = false;
- bool addCharcoal = false;
- bool addCoal = false;
- if (context.Player.User.Inventory.TryAddItems(typeof(LogItem), fuelSupply.Inventory.TotalNumberOfItems<LogItem>())) {
- fuelSupply.Inventory.RemoveItems<LogItem>(fuelSupply.Inventory.TotalNumberOfItems<LogItem>());
- addLog = true;
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the logs.");
- }
- if (context.Player.User.Inventory.TryAddItems(typeof(LumberItem), fuelSupply.Inventory.TotalNumberOfItems<LumberItem>())) {
- fuelSupply.Inventory.RemoveItems<LumberItem>(fuelSupply.Inventory.TotalNumberOfItems<LumberItem>());
- addLumber = true;
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the lumber.");
- }
- if (context.Player.User.Inventory.TryAddItems(typeof(ArrowItem), fuelSupply.Inventory.TotalNumberOfItems<ArrowItem>())) {
- fuelSupply.Inventory.RemoveItems<ArrowItem>(fuelSupply.Inventory.TotalNumberOfItems<ArrowItem>());
- addArrow = true;
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the arrows.");
- }
- if (context.Player.User.Inventory.TryAddItems(typeof(BoardItem), fuelSupply.Inventory.TotalNumberOfItems<BoardItem>())) {
- fuelSupply.Inventory.RemoveItems<BoardItem>(fuelSupply.Inventory.TotalNumberOfItems<BoardItem>());
- addBoard = true;
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the boards.");
- }
- if (context.Player.User.Inventory.TryAddItems(typeof(CharcoalItem), fuelSupply.Inventory.TotalNumberOfItems<CharcoalItem>())) {
- fuelSupply.Inventory.RemoveItems<CharcoalItem>(fuelSupply.Inventory.TotalNumberOfItems<CharcoalItem>());
- addCharcoal = true;
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the charcoal.");
- }
- if (context.Player.User.Inventory.TryAddItems(typeof(CoalItem), fuelSupply.Inventory.TotalNumberOfItems<CoalItem>())) {
- fuelSupply.Inventory.RemoveItems<CoalItem>(fuelSupply.Inventory.TotalNumberOfItems<CoalItem>());
- addCoal = true;
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up all of the coal.");
- }
- if (addLog && addLumber && addArrow && addBoard && addCharcoal && addCoal) {
- if (context.Player.User.Inventory.TryAddItem(typeof(FireBarrelItem))) {
- this.Destroy();
- } else {
- context.Player.SendTemporaryErrorLoc("You do not have enough inventory space to pick up the fire barrel.");
- }
- }
- }
- private void ShowInfo(InteractionContext context) {
- float garbagePercent = ((garbageWeightFloat / 1000f) / 10.0f) * 100;
- garbagePercent = (float) Math.Round(garbagePercent, 2);
- context.Player.OpenInfoPanel("Fire Barrel Info", (GetColor(garbagePercent) + "<size=50>" + garbagePercent.ToString() + "% full</size>"));
- }
- private bool CheckFireEnabled() {
- FuelSupplyComponent fuelSupplyComponent = this.GetComponent<FuelSupplyComponent>();
- float fuelSupply = fuelSupplyComponent.Energy;
- if (fuelSupply > 0 && (garbageWeightFloat) > 0) {
- fireEnabled = true;
- } else if (garbageWeightFloat < 0) {
- garbageWeightFloat = 0f; //lazy fix for negative values
- fireEnabled = false;
- } else
- fireEnabled = false;
- SetAnimatedState("FireBarrel", fireEnabled);
- this.operating = fireEnabled;
- return fireEnabled;
- }
- private string GetColor(float garbagePercent) {
- string colorString = "";
- if (garbagePercent < 25)
- colorString = "<color=#ffffff>";
- else if (garbagePercent >= 25 && garbagePercent < 50)
- colorString = "<color=#33cc33>";
- else if (garbagePercent >= 50 && garbagePercent < 75)
- colorString = "<color=#ffff00>";
- else if (garbagePercent >= 75 && garbagePercent < 100)
- colorString = "<color=#ff6600>";
- else if (garbagePercent >= 100)
- colorString = "<color=#ff0000>";
- return colorString;
- }
- }
- [Serialized]
- public partial class FireBarrelItem : WorldObjectItem<FireBarrelObject> {
- public override string FriendlyName { get { return "Fire Barrel"; } }
- public override string Description { get { return "A barrel you can burn trash items in."; } }
- static FireBarrelItem() {
- }
- }
- [RequiresSkill(typeof(MetalworkingSkill), 1)]
- public partial class FireBarrelRecipe : Recipe {
- public FireBarrelRecipe() {
- this.Products = new CraftingElement[]
- {
- new CraftingElement<FireBarrelItem>(),
- };
- this.Ingredients = new CraftingElement[]
- {
- new CraftingElement<IronIngotItem>(typeof(MetalworkingEfficiencySkill), 25, MetalworkingEfficiencySkill.MultiplicativeStrategy),
- };
- this.CraftMinutes = CreateCraftTimeValue(typeof(FireBarrelRecipe), Item.Get<FireBarrelItem>().UILink(), 5.0f, typeof(MetalworkingSpeedSkill));
- this.Initialize("Fire Barrel", typeof(FireBarrelRecipe));
- CraftingComponent.AddRecipe(typeof(AnvilObject), this);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement