From 32e1c9d3ac058f7673bc0d241561da5ca6e672ad Mon Sep 17 00:00:00 2001 From: JoshuaNavarro Date: Wed, 25 Sep 2019 15:02:58 -0700 Subject: [PATCH] Added in pipes and added some optimization checks to searching out energy and fluid networks. --- .../Objects/Machines/Pipes/IronPipe.png | Bin 0 -> 231 bytes .../Machines/EnergyGeneration/SteamBoiler.cs | 2 +- .../Framework/Objects/Machines/Pipe.cs | 160 ++++++++++++++++ .../Objects/Machines/PipeMultiTiledObject.cs | 181 ++++++++++++++++++ .../Framework/Objects/Machines/Wire.cs | 4 +- .../Framework/Objects/MultiTiledComponent.cs | 5 +- .../Framework/Objects/ObjectManager.cs | 9 +- GeneralMods/Revitalize/ModCore.cs | 3 +- GeneralMods/Revitalize/Revitalize.csproj | 5 + 9 files changed, 362 insertions(+), 7 deletions(-) create mode 100644 GeneralMods/Revitalize/Content/Graphics/Objects/Machines/Pipes/IronPipe.png create mode 100644 GeneralMods/Revitalize/Framework/Objects/Machines/Pipe.cs create mode 100644 GeneralMods/Revitalize/Framework/Objects/Machines/PipeMultiTiledObject.cs diff --git a/GeneralMods/Revitalize/Content/Graphics/Objects/Machines/Pipes/IronPipe.png b/GeneralMods/Revitalize/Content/Graphics/Objects/Machines/Pipes/IronPipe.png new file mode 100644 index 0000000000000000000000000000000000000000..44d9bfdf3202462b17bdb6fc288572ab2de6b776 GIT binary patch literal 231 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|7J9lkhFJ6_ zryO9caryec+r!(a!~2uLo|=EH56@nH&J!IMpW+gJyyY}Qih|zB=HR{sj6p? + /// Creates the necessary components to display the machine menu properly. + /// + + public override Item getOne() + { + Pipe component = new Pipe(this.data, this.info.Copy(), this.TileLocation, this.offsetKey, this.containerObject); + return component; + } + + public override ICustomObject recreate(Dictionary additionalSaveData, object replacement) + { + //instead of using this.offsetkey.x use get additional save data function and store offset key there + + Vector2 offsetKey = new Vector2(Convert.ToInt32(additionalSaveData["offsetKeyX"]), Convert.ToInt32(additionalSaveData["offsetKeyY"])); + Pipe self = Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["GUID"]); + if (self == null) + { + return null; + } + + if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"])) + { + //Get new container + PipeMultiTiledObject obj = (PipeMultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["ParentGUID"]); + self.containerObject = obj; + obj.addComponent(offsetKey, self); + //Revitalize.ModCore.log("ADD IN AN OBJECT!!!!"); + Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["ParentGUID"], (MultiTiledObject)obj); + } + else + { + self.containerObject = Revitalize.ModCore.ObjectGroups[additionalSaveData["ParentGUID"]]; + Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]].addComponent(offsetKey, self); + //Revitalize.ModCore.log("READD AN OBJECT!!!!"); + } + + return (ICustomObject)self; + } + + public override void rebuild(Dictionary additionalSaveData, object replacement) + { + base.rebuild(additionalSaveData, replacement); + } + + public override Dictionary getAdditionalSaveData() + { + Dictionary saveData = base.getAdditionalSaveData(); + Revitalize.ModCore.Serializer.SerializeGUID(this.containerObject.childrenGuids[this.offsetKey].ToString(), this); + this.containerObject.getAdditionalSaveData(); + return saveData; + + } + + public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1f) + { + this.updateInfo(); + + if (this.info == null) + { + Revitalize.ModCore.log("info is null"); + if (this.syncObject == null) Revitalize.ModCore.log("DEAD SYNC"); + } + if (this.animationManager == null) Revitalize.ModCore.log("Animation Manager Null"); + if (this.displayTexture == null) Revitalize.ModCore.log("Display texture is null"); + + //The actual planter box being drawn. + if (this.animationManager == null) + { + if (this.animationManager.getExtendedTexture() == null) + ModCore.ModMonitor.Log("Tex Extended is null???"); + + spriteBatch.Draw(this.displayTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2((float)(x * Game1.tileSize), y * Game1.tileSize)), new Rectangle?(this.animationManager.currentAnimation.sourceRectangle), this.info.DrawColor * alpha, 0f, Vector2.Zero, (float)Game1.pixelZoom, this.flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, Math.Max(0f, (float)(y * Game1.tileSize) / 10000f)); + // Log.AsyncG("ANIMATION IS NULL?!?!?!?!"); + } + + else + { + //Log.AsyncC("Animation Manager is working!"); + float addedDepth = 0; + + + if (Revitalize.ModCore.playerInfo.sittingInfo.SittingObject == this.containerObject && this.info.facingDirection == Enums.Direction.Up) + { + addedDepth += (this.containerObject.Height - 1) - ((int)(this.offsetKey.Y)); + if (this.info.ignoreBoundingBox) addedDepth += 1.5f; + } + //this.determineWireOrientation(); + this.animationManager.draw(spriteBatch, this.displayTexture, Game1.GlobalToLocal(Game1.viewport, new Vector2((float)(x * Game1.tileSize), y * Game1.tileSize)), new Rectangle?(this.animationManager.currentAnimation.sourceRectangle), this.info.DrawColor * alpha, 0f, Vector2.Zero, (float)Game1.pixelZoom, this.flipped ? SpriteEffects.FlipHorizontally : SpriteEffects.None, Math.Max(0f, (float)((y + addedDepth) * Game1.tileSize) / 10000f) + .00001f); + try + { + this.animationManager.tickAnimation(); + // Log.AsyncC("Tick animation"); + } + catch (Exception err) + { + ModCore.ModMonitor.Log(err.ToString()); + } + } + + // spriteBatch.Draw(Game1.mouseCursors, Game1.GlobalToLocal(Game1.viewport, new Vector2((float)((double)tileLocation.X * (double)Game1.tileSize + (((double)tileLocation.X * 11.0 + (double)tileLocation.Y * 7.0) % 10.0 - 5.0)) + (float)(Game1.tileSize / 2), (float)((double)tileLocation.Y * (double)Game1.tileSize + (((double)tileLocation.Y * 11.0 + (double)tileLocation.X * 7.0) % 10.0 - 5.0)) + (float)(Game1.tileSize / 2))), new Rectangle?(new Rectangle((int)((double)tileLocation.X * 51.0 + (double)tileLocation.Y * 77.0) % 3 * 16, 128 + this.whichForageCrop * 16, 16, 16)), Color.White, 0.0f, new Vector2(8f, 8f), (float)Game1.pixelZoom, SpriteEffects.None, (float)(((double)tileLocation.Y * (double)Game1.tileSize + (double)(Game1.tileSize / 2) + (((double)tileLocation.Y * 11.0 + (double)tileLocation.X * 7.0) % 10.0 - 5.0)) / 10000.0)); + + } + + public override bool canBePlacedInWater() + { + return true; + } + + private void determinePipeOrientation() + { + //TODO: Make this so that the correct wire orientation is used if I want to get fancy with pipes and their graphics. + } + } +} diff --git a/GeneralMods/Revitalize/Framework/Objects/Machines/PipeMultiTiledObject.cs b/GeneralMods/Revitalize/Framework/Objects/Machines/PipeMultiTiledObject.cs new file mode 100644 index 00000000..f71bde6c --- /dev/null +++ b/GeneralMods/Revitalize/Framework/Objects/Machines/PipeMultiTiledObject.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using PyTK.CustomElementHandler; +using StardewValley; + +namespace Revitalize.Framework.Objects.Machines +{ + public class PipeMultiTiledObject:MultiTiledObject + { + public PipeMultiTiledObject() : base() + { + + } + + public PipeMultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info) + : base(PyTKData, info) + { + + } + + public PipeMultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info, Vector2 TileLocation) + : base(PyTKData, info, TileLocation) + { + + } + + public PipeMultiTiledObject(CustomObjectData PyTKData, BasicItemInformation info, Vector2 TileLocation, Dictionary ObjectsList) + : base(PyTKData, info, TileLocation, ObjectsList) + { + + + } + + public override Item getOne() + { + Dictionary objs = new Dictionary(); + foreach (var pair in this.objects) + { + objs.Add(pair.Key, (MultiTiledComponent)pair.Value.getOne()); + } + return new PipeMultiTiledObject(this.data, this.info.Copy(), this.TileLocation, objs); + } + + public override ICustomObject recreate(Dictionary additionalSaveData, object replacement) + { + PipeMultiTiledObject obj = (PipeMultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["GUID"]); + if (obj == null) + { + return null; + } + + Dictionary guids = new Dictionary(); + + foreach (KeyValuePair pair in obj.childrenGuids) + { + guids.Add(pair.Key, pair.Value); + } + + foreach (KeyValuePair pair in guids) + { + obj.childrenGuids.Remove(pair.Key); + MultiTiledComponent component = Revitalize.ModCore.Serializer.DeserializeGUID(pair.Value.ToString()); + component.InitNetFields(); + obj.removeComponent(pair.Key); + obj.addComponent(pair.Key, component); + + + } + obj.InitNetFields(); + + if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["GUID"])) + { + Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["GUID"], obj); + return obj; + } + else + { + return Revitalize.ModCore.ObjectGroups[additionalSaveData["GUID"]]; + } + + + } + + public override Dictionary getAdditionalSaveData() + { + Dictionary saveData = base.getAdditionalSaveData(); + //saveData.Add("GUID", this.guid.ToString()); + //Revitalize.ModCore.Serializer.SerializeGUID(this.guid.ToString(), this); + return saveData; + } + + public override void rebuild(Dictionary additionalSaveData, object replacement) + { + base.rebuild(additionalSaveData, replacement); + } + + public override void draw(SpriteBatch spriteBatch, int x, int y, float alpha = 1) + { + this.updateInfo(); + foreach (KeyValuePair pair in this.objects) + { + (pair.Value as MultiTiledComponent).draw(spriteBatch, x + ((int)pair.Key.X), y + ((int)pair.Key.Y), alpha); + } + } + + public override void draw(SpriteBatch spriteBatch, int xNonTile, int yNonTile, float layerDepth, float alpha = 1) + { + this.updateInfo(); + foreach (KeyValuePair pair in this.objects) + { + pair.Value.draw(spriteBatch, xNonTile + (int)pair.Key.X * Game1.tileSize, yNonTile + (int)pair.Key.Y * Game1.tileSize, layerDepth, alpha); + } + + //base.draw(spriteBatch, xNonTile, yNonTile, layerDepth, alpha); + } + + public override void drawInMenu(SpriteBatch spriteBatch, Vector2 location, float scaleSize, float transparency, float layerDepth, bool drawStackNumber, Color c, bool drawShadow) + { + this.updateInfo(); + foreach (KeyValuePair pair in this.objects) + { + //ModCore.log(location + (pair.Key * 16) + new Vector2(32, 32)); + pair.Value.drawInMenu(spriteBatch, location + (pair.Key * 16) + new Vector2(32, 32), 1.0f, transparency, layerDepth, drawStackNumber, c, drawShadow); + } + if (drawStackNumber && this.maximumStackSize() > 1 && ((double)scaleSize > 0.3 && this.Stack != int.MaxValue) && this.Stack > 1) + Utility.drawTinyDigits(this.Stack, spriteBatch, location + new Vector2((float)(Game1.tileSize - Utility.getWidthOfTinyDigitString(this.Stack, 3f * scaleSize)) + 3f * scaleSize, (float)((double)Game1.tileSize - 18.0 * (double)scaleSize + 2.0)), 3f * scaleSize, 1f, Color.White); + //base.drawInMenu(spriteBatch, location, scaleSize, transparency, layerDepth, drawStackNumber, c, drawShadow); + } + + public override void drawWhenHeld(SpriteBatch spriteBatch, Vector2 objectPosition, Farmer f) + { + this.updateInfo(); + foreach (KeyValuePair pair in this.objects) + pair.Value.drawWhenHeld(spriteBatch, objectPosition + (pair.Key * Game1.tileSize), f); + //base.drawWhenHeld(spriteBatch, objectPosition, f); + } + + public override bool canStackWith(Item other) + { + if (other is PipeMultiTiledObject) + { + return (other as PipeMultiTiledObject).info.id == this.info.id && (other as PipeMultiTiledObject).info.DyedColor == this.info.DyedColor; + } + else return false; + } + + public override int maximumStackSize() + { + return 999; + } + + public override bool placementAction(GameLocation location, int x, int y, Farmer who = null) + { + this.updateInfo(); + PipeMultiTiledObject m = (PipeMultiTiledObject)this.getOne(); + + foreach (KeyValuePair pair in m.objects) + { + /* + if ((pair.Value as CustomObject).info.ignoreBoundingBox) + { + pair.Value.placementAction(location, -1 * (x + (int)pair.Key.X * Game1.tileSize), -1 * (y + (int)pair.Key.Y * Game1.tileSize), who); + } + else + { + pair.Value.placementAction(location, x + (int)pair.Key.X * Game1.tileSize, y + (int)pair.Key.Y * Game1.tileSize, who); + }*/ + (pair.Value as MultiTiledComponent).placementAction(location, x + (int)pair.Key.X * Game1.tileSize, y + (int)pair.Key.Y * Game1.tileSize, who); + //ModCore.log(pair.Value.TileLocation); + } + m.location = location; + return true; + //return base.placementAction(location, x, y, who); + } + } +} diff --git a/GeneralMods/Revitalize/Framework/Objects/Machines/Wire.cs b/GeneralMods/Revitalize/Framework/Objects/Machines/Wire.cs index 3cc3ed59..217337c5 100644 --- a/GeneralMods/Revitalize/Framework/Objects/Machines/Wire.cs +++ b/GeneralMods/Revitalize/Framework/Objects/Machines/Wire.cs @@ -71,11 +71,11 @@ namespace Revitalize.Framework.Objects.Machines if (!Revitalize.ModCore.ObjectGroups.ContainsKey(additionalSaveData["ParentGUID"])) { //Get new container - MultiTiledObject obj = (MultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["ParentGUID"]); + WireMultiTiledObject obj = (WireMultiTiledObject)Revitalize.ModCore.Serializer.DeserializeGUID(additionalSaveData["ParentGUID"]); self.containerObject = obj; obj.addComponent(offsetKey, self); //Revitalize.ModCore.log("ADD IN AN OBJECT!!!!"); - Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["ParentGUID"], (MultiTiledObject)obj); + Revitalize.ModCore.ObjectGroups.Add(additionalSaveData["ParentGUID"], (WireMultiTiledObject)obj); } else { diff --git a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs index bba52826..095c0b76 100644 --- a/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs +++ b/GeneralMods/Revitalize/Framework/Objects/MultiTiledComponent.cs @@ -433,6 +433,7 @@ namespace Revitalize.Framework.Objects { if ((obj as MultiTiledComponent).GetEnergyManager().energyInteractionType == Enums.EnergyInteractionType.Produces || (obj as MultiTiledComponent).GetEnergyManager().energyInteractionType == Enums.EnergyInteractionType.Transfers || (obj as MultiTiledComponent).GetEnergyManager().energyInteractionType == Enums.EnergyInteractionType.Storage) { + if ((obj as MultiTiledComponent).containerObject == this.containerObject) continue; customObjects.Add((MultiTiledComponent)obj); } } @@ -471,6 +472,7 @@ namespace Revitalize.Framework.Objects { if ((obj as MultiTiledComponent).GetEnergyManager().energyInteractionType == Enums.EnergyInteractionType.Consumes || (obj as MultiTiledComponent).GetEnergyManager().energyInteractionType == Enums.EnergyInteractionType.Transfers || (obj as MultiTiledComponent).GetEnergyManager().energyInteractionType == Enums.EnergyInteractionType.Storage) { + if ((obj as MultiTiledComponent).containerObject == this.containerObject) continue; customObjects.Add((MultiTiledComponent)obj); } } @@ -714,12 +716,13 @@ namespace Revitalize.Framework.Objects if ((obj as MultiTiledComponent).GetFluidManager().InteractsWithFluids) { + if ((obj as MultiTiledComponent).containerObject == this.containerObject) continue; customObjects.Add((MultiTiledComponent)obj); //ModCore.log("Found a neighboring fluid manager"); } else { - ModCore.log("Found a neighboring object but it isn't a valid fluid manager."); + //ModCore.log("Found a neighboring object but it isn't a valid fluid manager."); } } else continue; diff --git a/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs b/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs index 38d4d164..1370a412 100644 --- a/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs +++ b/GeneralMods/Revitalize/Framework/Objects/ObjectManager.cs @@ -168,7 +168,7 @@ namespace Revitalize.Framework.Objects private void loadInMachines() { - this.loadInWires(); + this.loadInConnectionComponents(); @@ -416,12 +416,17 @@ namespace Revitalize.Framework.Objects this.AddItem("SteamBoilerV1", steamBoilerV1); } - private void loadInWires() + private void loadInConnectionComponents() { WireMultiTiledObject copperWire = new WireMultiTiledObject(PyTKHelper.CreateOBJData("Omegasis.Revitalize.Objects.Machines.Wires.CopperWire", TextureManager.GetTexture(ModCore.Manifest, "Machines", "CopperWire"), typeof(Wire), Color.White, true), new BasicItemInformation("Copper Wire", "Omegasis.Revitalize.Objects.Machines.Wire.CopperWire", "Wire made from copper bars. Transfers energy between sources.", "Machine", Color.SteelBlue, -300, 0, false, 15, true, true, TextureManager.GetTexture(ModCore.Manifest, "Machines", "CopperWire"), new AnimationManager(TextureManager.GetExtendedTexture(ModCore.Manifest, "Machines", "CopperWire"), new Animation(0, 0, 16, 16)), Color.White, true, null, null, new Energy.EnergyManager(100, Enums.EnergyInteractionType.Transfers), false)); Wire copperWire_0_0 = new Wire(PyTKHelper.CreateOBJData("Omegasis.Revitalize.Objects.Machines.Wires.CopperWire", TextureManager.GetTexture(ModCore.Manifest, "Machines", "CopperWire"), typeof(Wire), Color.White, true), new BasicItemInformation("Copper Wire", "Omegasis.Revitalize.Objects.Machines.Wire.CopperWire", "Wire made from copper bars. Transfers energy between sources.", "Machine", Color.SteelBlue, -300, 0, false, 15, true, true, TextureManager.GetTexture(ModCore.Manifest, "Machines", "CopperWire"), new AnimationManager(TextureManager.GetExtendedTexture(ModCore.Manifest, "Machines", "CopperWire"), new Animation(0, 0, 16, 16)), Color.White, true, null, null, new Energy.EnergyManager(100, Enums.EnergyInteractionType.Transfers),false)); copperWire.addComponent(new Vector2(0, 0), copperWire_0_0); this.AddItem("CopperWire", copperWire); + + PipeMultiTiledObject ironPipe = new PipeMultiTiledObject(PyTKHelper.CreateOBJData("Omegasis.Revitalize.Objects.Machines.Wires.IronPipe", TextureManager.GetTexture(ModCore.Manifest, "Machines", "IronPipe"), typeof(Pipe), Color.White, true), new BasicItemInformation("Iron Pipe", "Omegasis.Revitalize.Objects.Machines.Wire.Pipe", "Pipes made from iron. Transfers fluids between machines.", "Machine", Color.SteelBlue, -300, 0, false, 25, true, true, TextureManager.GetTexture(ModCore.Manifest, "Machines", "IronPipe"), new AnimationManager(TextureManager.GetExtendedTexture(ModCore.Manifest, "Machines", "IronPipe"), new Animation(0, 0, 16, 16)), Color.White, true, null, null, null, false,null,null,new Managers.FluidManagerV2(0,false, Enums.FluidInteractionType.Transfers,false))); + Pipe ironPipe_0_0 = new Pipe(PyTKHelper.CreateOBJData("Omegasis.Revitalize.Objects.Machines.Wires.IronPipe", TextureManager.GetTexture(ModCore.Manifest, "Machines", "IronPipe"), typeof(Pipe), Color.White, true), new BasicItemInformation("Iron Pipe", "Omegasis.Revitalize.Objects.Machines.Wire.Pipe", "Pipes made from iron. Transfers fluids between machines.", "Machine", Color.SteelBlue, -300, 0, false, 25, true, true, TextureManager.GetTexture(ModCore.Manifest, "Machines", "IronPipe"), new AnimationManager(TextureManager.GetExtendedTexture(ModCore.Manifest, "Machines", "IronPipe"), new Animation(0, 0, 16, 16)), Color.White, true, null, null, null, false, null, null, new Managers.FluidManagerV2(0, false, Enums.FluidInteractionType.Transfers, false))); + ironPipe.addComponent(new Vector2(0, 0), ironPipe_0_0); + this.AddItem("IronPipe", ironPipe); } private void loadInTools() diff --git a/GeneralMods/Revitalize/ModCore.cs b/GeneralMods/Revitalize/ModCore.cs index 1ce659d4..6b0a98aa 100644 --- a/GeneralMods/Revitalize/ModCore.cs +++ b/GeneralMods/Revitalize/ModCore.cs @@ -601,7 +601,8 @@ namespace Revitalize ModCore.ObjectManager.GetItem("AlloyFurnace"), new StardewValley.Object((int)Enums.SDVObject.IronBar,100), ModCore.ObjectManager.GetItem("WaterPumpV1"), - ModCore.ObjectManager.GetItem("SteamBoilerV1") + ModCore.ObjectManager.GetItem("SteamBoilerV1"), + ModCore.ObjectManager.GetItem("IronPipe",10) }); } diff --git a/GeneralMods/Revitalize/Revitalize.csproj b/GeneralMods/Revitalize/Revitalize.csproj index d2846b29..9f73a218 100644 --- a/GeneralMods/Revitalize/Revitalize.csproj +++ b/GeneralMods/Revitalize/Revitalize.csproj @@ -169,6 +169,8 @@ + + @@ -501,6 +503,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest