Skip to content

Commit

Permalink
etc
Browse files Browse the repository at this point in the history
  • Loading branch information
aedenthorn committed Nov 25, 2021
1 parent 9d964d4 commit 5dae30b
Show file tree
Hide file tree
Showing 21 changed files with 754 additions and 208 deletions.
8 changes: 8 additions & 0 deletions UtilityGrid/GridPipe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UtilityGrid
{
public class GridPipe
{
public int index;
public int rotation;
}
}
245 changes: 245 additions & 0 deletions UtilityGrid/HelperEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewValley;
using System;
using System.Collections.Generic;

namespace UtilityGrid
{
/// <summary>The mod entry point.</summary>
public partial class ModEntry
{


public void Input_ButtonPressed(object sender, StardewModdingAPI.Events.ButtonPressedEventArgs e)
{
if (!Config.EnableMod || !(Game1.currentLocation is Farm) || !Game1.currentLocation.IsOutdoors || Game1.activeClickableMenu != null)
return;

if (e.Button == Config.ToggleGrid)
{
Helper.Input.Suppress(e.Button);
ShowingGrid = !ShowingGrid;
Monitor.Log($"Showing grid: {ShowingGrid}");
}
if (!ShowingGrid)
return;
if (e.Button == Config.SwitchGrid)
{
Helper.Input.Suppress(e.Button);
CurrentGrid = CurrentGrid == GridType.water ? GridType.electric : GridType.water;
Monitor.Log($"Showing grid: {CurrentGrid}");
}
else if (e.Button == Config.SwitchTile)
{
Helper.Input.Suppress(e.Button);
CurrentTile++;
CurrentTile %= 6;
CurrentRotation = 0;
//Monitor.Log($"Showing tile: {CurrentTile},{CurrentRotation}");
}
else if (e.Button == Config.RotateTile)
{
Helper.Input.Suppress(e.Button);
CurrentRotation++;
if (CurrentTile == 1)
CurrentRotation %= 2;
else if (CurrentTile == 4)
CurrentRotation = 0;
else
CurrentRotation %= 4;
//Monitor.Log($"Showing tile: {CurrentTile},{CurrentRotation}");
}
else if (e.Button == Config.PlaceTile)
{
Helper.Input.Suppress(e.Button);
Dictionary<Vector2, GridPipe> pipeDict;
if (CurrentGrid == GridType.electric)
{
pipeDict = electricPipes;
}
else
{
pipeDict = waterPipes;
}
if(CurrentTile == 5)
{
Monitor.Log($"Removing tile at {Game1.currentCursorTile}");
pipeDict.Remove(Game1.lastCursorTile);
}
else
{
Monitor.Log($"Placing tile {CurrentTile},{CurrentRotation} at {Game1.currentCursorTile}");
pipeDict[Game1.lastCursorTile] = new GridPipe() { index = CurrentTile, rotation = CurrentRotation };
}
RemakeGroups(CurrentGrid);
}
}
public void Display_RenderedWorld(object sender, StardewModdingAPI.Events.RenderedWorldEventArgs e)
{
if (!Config.EnableMod || !ShowingGrid)
return;

if(!(Game1.currentLocation is Farm) || !Game1.currentLocation.IsOutdoors || Game1.activeClickableMenu != null)
{
ShowingGrid = false;
return;
}

List<PipeGroup> groupList;
Color color;
Dictionary<Vector2, GridPipe> pipeDict;
if (CurrentGrid == GridType.electric)
{
groupList = electricGroups;
pipeDict = electricPipes;
color = Config.ElectricityColor;
}
else
{
groupList = waterGroups;
pipeDict = waterPipes;
color = Config.WaterColor;
}
foreach (var group in groupList)
{
Vector2 power = GetGroupPower(group, CurrentGrid);
float netPower = power.X + power.Y;
bool powered = power.X > 0 && netPower >= 0;
foreach (var pipe in group.pipes)
{
if (Utility.isOnScreen(new Vector2(pipe.X * 64 + 32, pipe.Y * 64 + 32), 32))
{
if (pipe != Game1.currentCursorTile)
{
if (pipeDict[pipe].index == 4)
DrawTile(e.SpriteBatch, pipe, new GridPipe() { index = 1, rotation = 2 }, powered ? color : Color.White);
else
DrawTile(e.SpriteBatch, pipe, pipeDict[pipe], powered ? color : Color.White);
}
}
}
foreach (var kvp in group.objects)
{
float objPower;
bool enough = netPower >= 0;
if (CurrentGrid == GridType.electric)
{
objPower = kvp.Value.electric;
}
else
{
objPower = kvp.Value.water;
if (kvp.Value.electric < 0 && GetTileNetElectricPower(kvp.Key) < 0)
enough = false;
}

if (objPower == 0)
continue;


string str = "" + Math.Round(objPower);

e.SpriteBatch.DrawString(Game1.dialogueFont, str, Game1.GlobalToLocal(Game1.viewport, kvp.Key * 64) - new Vector2(16, 16) + new Vector2(-2, 2), Color.Black, 0, Vector2.Zero, 1f, SpriteEffects.None, 0.999999f);
e.SpriteBatch.DrawString(Game1.dialogueFont, str, Game1.GlobalToLocal(Game1.viewport, kvp.Key * 64) - new Vector2(16, 16), enough ? color : Config.InsufficientColor, 0, Vector2.Zero, 1f, SpriteEffects.None, 0.9999999f);
}

}
if (CurrentTile == 4)
DrawTile(e.SpriteBatch, Game1.currentCursorTile, new GridPipe() { index = 1, rotation = 2 }, color);
else if(CurrentTile != 5)
DrawTile(e.SpriteBatch, Game1.currentCursorTile, new GridPipe() { index = CurrentTile, rotation = CurrentRotation }, color);
}

private void GameLoop_OneSecondUpdateTicked(object sender, StardewModdingAPI.Events.OneSecondUpdateTickedEventArgs e)
{
if (!Config.EnableMod || !(Game1.currentLocation is Farm))
return;

RemakeAllGroups();

}

private void GameLoop_SaveLoaded(object sender, StardewModdingAPI.Events.SaveLoadedEventArgs e)
{
waterPipes = new Dictionary<Vector2, GridPipe>();
electricPipes = new Dictionary<Vector2, GridPipe>();
ShowingGrid = false;
CurrentRotation = 0;
CurrentTile = 0;

objectDict = SHelper.Content.Load<Dictionary<string, UtilityObject>>(dictPath, ContentSource.GameContent);
UtilityGridData gridData = Helper.Data.ReadSaveData<UtilityGridData>(saveKey) ?? new UtilityGridData();
Monitor.Log($"reading {gridData.electricData.Count} ep and {gridData.waterData.Count} wp from save");
foreach (var arr in gridData.waterData)
{
waterPipes[new Vector2(arr[0], arr[1])] = new GridPipe() { index = arr[2], rotation = arr[3] };
}
foreach(var arr in gridData.electricData)
{
electricPipes[new Vector2(arr[0], arr[1])] = new GridPipe() { index = arr[2], rotation = arr[3] };
}
RemakeAllGroups();
}
private void GameLoop_Saving(object sender, StardewModdingAPI.Events.SavingEventArgs e)
{

UtilityGridData gridData = new UtilityGridData(waterPipes, electricPipes);
Monitor.Log($"writing {gridData.electricData.Count} ep and {gridData.waterData.Count} wp to save");
Helper.Data.WriteSaveData(saveKey, gridData);
}
public void GameLoop_GameLaunched(object sender, StardewModdingAPI.Events.GameLaunchedEventArgs e)
{
// get Generic Mod Config Menu's API (if it's installed)
var configMenu = Helper.ModRegistry.GetApi<IGenericModConfigMenuApi>("spacechase0.GenericModConfigMenu");
if (configMenu is null)
return;

// register mod
configMenu.Register(
mod: ModManifest,
reset: () => Config = new ModConfig(),
save: () => Helper.WriteConfig(Config)
);

configMenu.AddBoolOption(
mod: ModManifest,
name: () => "Mod Enabled?",
getValue: () => Config.EnableMod,
setValue: value => Config.EnableMod = value
);
configMenu.AddKeybind(
mod: ModManifest,
name: () => "Toggle Grid Key",
getValue: () => Config.ToggleGrid,
setValue: value => Config.ToggleGrid = value
);
configMenu.AddKeybind(
mod: ModManifest,
name: () => "Switch Grid Key",
getValue: () => Config.ToggleGrid,
setValue: value => Config.ToggleGrid = value
);
configMenu.AddKeybind(
mod: ModManifest,
name: () => "Change Tile Key",
getValue: () => Config.SwitchTile,
setValue: value => Config.SwitchTile = value
);
configMenu.AddKeybind(
mod: ModManifest,
name: () => "Rotate Tile Key",
getValue: () => Config.RotateTile,
setValue: value => Config.RotateTile = value
);
configMenu.AddKeybind(
mod: ModManifest,
name: () => "Place Tile Key",
getValue: () => Config.PlaceTile,
setValue: value => Config.PlaceTile = value
);
}
}
}
58 changes: 58 additions & 0 deletions UtilityGrid/MethodPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.Xna.Framework;
using StardewValley;

namespace UtilityGrid
{
public partial class ModEntry
{
// dga_add "aedenthorn.WaterPumpDGA/Bronze Water Pump" 1
public static bool Utility_playerCanPlaceItemHere_Prefix(GameLocation location, Item item, int x, int y, ref bool __result)
{
//SMonitor.Log($"placing {item.Name}, {objectDict.ContainsKey(item.Name)}");
if (!Config.EnableMod || !objectDict.ContainsKey(item.Name) || !(location is Farm) || !objectDict[item.Name].onlyInWater || location.Objects.ContainsKey(new Vector2(x, y)))
return true;
__result = location.isWaterTile(x / 64, y / 64);
return false;
}
public static void Object_IsSprinkler_Postfix(Object __instance, ref bool __result)
{
//SMonitor.Log($"placing {item.Name}, {objectDict.ContainsKey(item.Name)}");
if (!Config.EnableMod || !__result || !objectDict.ContainsKey(__instance.Name))
return;
__result = IsObjectPowered(__instance.TileLocation, objectDict[__instance.Name]);

}
public static void Object_updateWhenCurrentLocation_Prefix(Object __instance)
{
//SMonitor.Log($"placing {item.Name}, {objectDict.ContainsKey(item.Name)}");
if (!Config.EnableMod || !objectDict.ContainsKey(__instance.Name))
return;
__instance.IsOn = IsObjectPowered(__instance.TileLocation, objectDict[__instance.Name]);

}
public static Object preItem = null;
public static int minutesUntilReady = 0;
public static void Object_DayUpdate_Prefix(Object __instance, ref bool __state)
{
//SMonitor.Log($"placing {item.Name}, {objectDict.ContainsKey(item.Name)}");
if (!Config.EnableMod || !objectDict.ContainsKey(__instance.Name))
return;
if(!IsObjectPowered(__instance.TileLocation, objectDict[__instance.Name]))
{
preItem = __instance.heldObject.Value;
minutesUntilReady = __instance.MinutesUntilReady;
__instance.heldObject.Value = null;
__state = true;
}

}
public static void Object_DayUpdate_Postfix(Object __instance, bool __state)
{
//SMonitor.Log($"placing {item.Name}, {objectDict.ContainsKey(item.Name)}");
if (!__state)
return;
__instance.MinutesUntilReady = minutesUntilReady;
__instance.heldObject.Value = preItem;
}
}
}
1 change: 1 addition & 0 deletions UtilityGrid/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class ModConfig
public SButton PlaceTile { get; set; } = SButton.MouseLeft;
public Color WaterColor { get; set; } = Color.Aqua;
public Color ElectricityColor { get; set; } = Color.Yellow;
public Color InsufficientColor { get; set; } = Color.Red;
}
}
Loading

0 comments on commit 5dae30b

Please sign in to comment.