Skip to content

Commit

Permalink
etc
Browse files Browse the repository at this point in the history
  • Loading branch information
aedenthorn committed Dec 4, 2022
1 parent 60e602f commit 3e3799a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 93 deletions.
63 changes: 20 additions & 43 deletions DynamicMapTiles/CodePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,7 @@ public static void Postfix(GameLocation __instance, float x, float y)
{
explodingFarmer.mailReceived.Add(mail);
}
List<string> actions = new List<string>();
foreach (var kvp in tile.Properties)
{
foreach (var str in actionKeys)
{
if (kvp.Key == str + "Explode")
{
actions.Add(str);
}
}
}
if (actions.Count > 0)
{
TriggerActions(actions, new List<Layer>() { layer }, explodingFarmer, new Point((int)x, (int)y), new List<string>() { "Explode" });
}
TriggerActions(new List<Layer>() { tile.Layer }, explodingFarmer, new Point((int)x, (int)y), new List<string>() { "Explode" });
layer.Tiles[(int)x, (int)y] = null;
}
}
Expand Down Expand Up @@ -107,7 +93,24 @@ public static void Postfix(GameLocation __instance, Tool t, int tileX, int tileY
{
if (!Config.ModEnabled || !__instance.isTileOnMap(new Vector2(tileX, tileY)))
return;
TriggerActions(actionKeys, __instance.Map.Layers.ToList(), t.getLastFarmerToUse(), new Point(tileX, tileY), new List<string>() { t.GetType().Name, t.Name });

TriggerActions(__instance.Map.Layers.ToList(), t.getLastFarmerToUse(), new Point(tileX, tileY), new List<string>() { t.GetType().Name, t.Name });
}
}
[HarmonyPatch(typeof(GameLocation), nameof(GameLocation.checkAction))]
public class GameLocation_checkAction_Patch
{
public static void Postfix(GameLocation __instance, Location tileLocation, xTile.Dimensions.Rectangle viewport, Farmer who, ref bool __result)
{
if (!Config.ModEnabled || __result || !__instance.isTileOnMap(new Vector2(tileLocation.X, tileLocation.Y)))
return;
if (who.ActiveObject is null || !TriggerActions(__instance.Map.Layers.ToList(), who, new Point(tileLocation.X, tileLocation.Y), new List<string>() { "Object"+ who.ActiveObject.Name, "Object" + who.ActiveObject.ParentSheetIndex }))
{
__result = TriggerActions(__instance.Map.Layers.ToList(), who, new Point(tileLocation.X, tileLocation.Y), new List<string>() { "Action" });
return;
}
__result = true;

}
}
[HarmonyPatch(typeof(Farmer), nameof(Farmer.getMovementSpeed))]
Expand Down Expand Up @@ -178,33 +181,7 @@ public static void Postfix(Farmer __instance, Vector2[] __state)
var split = item.Split(' ');
if(split.Length == 2 && int.TryParse(split[0], out int x) && int.TryParse(split[1], out int y) && destTile.X == x && destTile.Y == y)
{
List<(Point, Tile)> tileList = new() { (startTile, tile) };
if (tile.Properties.TryGetValue(pushOthersKey, out PropertyValue others))
{
split = others.ToString().Split(',');
foreach(var t in split)
{
try
{
var split2 = t.Split(' ');
if (split2.Length == 3 && int.TryParse(split2[1], out int x2) && int.TryParse(split2[2], out int y2))
{
x2 += startTile.X;
y2 += startTile.Y;
var layer = __instance.currentLocation.Map.GetLayer(split2[0]);
if (layer is not null && __instance.currentLocation.isTileOnMap(x2, y2) && layer.Tiles[x2, y2] is not null)
{
tileList.Add((new Point(x2, y2), layer.Tiles[x2, y2]));
}
}
}
catch(Exception ex)
{
SMonitor.Log(ex.ToString(), StardewModdingAPI.LogLevel.Error);
}
}
}
PushTiles(__instance.currentLocation, tileList, __instance.FacingDirection, __instance);
PushTileWithOthers(__instance, tile, startTile);
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions DynamicMapTiles/DynamicMapTilesApi.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using Microsoft.Xna.Framework;
using StardewValley;
using System.Collections.Generic;
using xTile.Tiles;
using xTile.Layers;

namespace DynamicMapTiles
{
public interface IDynamicMapTilesApi
{
public bool PushTiles(GameLocation location, List<(Point, Tile)> tiles, int dir, Farmer farmer);
public bool TriggerActions(List<Layer> layers, Farmer farmer, Point tilePos, List<string> suffixes);
}
public class DynamicMapTilesApi : IDynamicMapTilesApi
{
public bool PushTiles(GameLocation location, List<(Point, Tile)> tiles, int dir, Farmer farmer)
public bool TriggerActions(List<Layer> layers, Farmer farmer, Point tilePos, List<string> suffixes)
{
return ModEntry.PushTiles(location, tiles, dir, farmer);
return ModEntry.TriggerActions(layers, farmer, tilePos, suffixes);
}
}
}
115 changes: 84 additions & 31 deletions DynamicMapTiles/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public partial class ModEntry
{
private static void DoStepOnActions(Farmer farmer, Point tilePos)
{
TriggerActions(actionKeys, new List<Layer>() { farmer.currentLocation.Map.GetLayer("Back") }, farmer, tilePos, new List<string>() { "On" });
TriggerActions(new List<Layer>() { farmer.currentLocation.Map.GetLayer("Back") }, farmer, tilePos, new List<string>() { "On" });
}

private static void DoStepOffActions(Farmer farmer, Point tilePos)
{
TriggerActions(actionKeys, new List<Layer>() { farmer.currentLocation.Map.GetLayer("Back") }, farmer, tilePos, new List<string>() { "Off" });
TriggerActions(new List<Layer>() { farmer.currentLocation.Map.GetLayer("Back") }, farmer, tilePos, new List<string>() { "Off" });
}

public static void TriggerActions(List<string> actions, List<Layer> layers, Farmer farmer, Point tilePos, List<string> postfixes)
public static bool TriggerActions(List<Layer> layers, Farmer farmer, Point tilePos, List<string> postfixes)
{
if (!farmer.currentLocation.isTileOnMap(tilePos.ToVector2()))
return;

return false;
bool triggered = false;
foreach (var layer in layers)
{
var tile = layer.Tiles[tilePos.X, tilePos.Y];
Expand All @@ -45,23 +45,30 @@ public static void TriggerActions(List<string> actions, List<Layer> layers, Farm
foreach(var k in tile.Properties.Keys.ToArray())
{
string key = k;
if (postfixes is not null)
if (postfixes is not null && postfixes.Any())
{
bool found = false;
foreach(var postfix in postfixes)
{
if(key.EndsWith(postfix))
key = key.Substring(0, key.Length - postfix.Length);
break;
if (key.EndsWith(postfix))
{
key = key.Substring(0, key.Length - postfix.Length);
found = true;
break;
}
}
if (!found)
continue;
}
bool remove = false;
if (key.EndsWith("Once"))
{
remove = true;
key = key.Substring(0, key.Length - 4);
}
if (!actions.Contains(key) || !tile.Properties.TryGetValue(k, out PropertyValue v))
if (!actionKeys.Contains(key) || !tile.Properties.TryGetValue(k, out PropertyValue v))
continue;
triggered = true;
SMonitor.Log($"Triggering property {key}");
string value = v?.ToString();
if (remove)
Expand All @@ -72,7 +79,19 @@ public static void TriggerActions(List<string> actions, List<Layer> layers, Farm
try
{
int number;
if (key == changeIndexKey)
if (key == addLayerKey)
{
AddLayer(farmer.currentLocation.map, value);
}
else if (key == addTilesheetKey)
{
var split = value.Split(',');
if (split.Length == 2)
{
AddTilesheet(farmer.currentLocation.map, split[0], split[1]);
}
}
else if (key == changeIndexKey)
{
if (string.IsNullOrEmpty(value))
{
Expand Down Expand Up @@ -396,16 +415,30 @@ public static void TriggerActions(List<string> actions, List<Layer> layers, Farm
}
}
}
else if (key == addLayerKey)
else if (key == pushKey)
{
AddLayer(farmer.currentLocation.map, value);
PushTileWithOthers(farmer, tile, tilePos);
}
else if (key == addTilesheetKey)
else if (key == takeKey && farmer.ActiveObject is not null && (string.IsNullOrEmpty(value) || farmer.ActiveObject.ParentSheetIndex+"" == value || farmer.ActiveObject.Name == value))
{
farmer.reduceActiveItemByOne();
}
else if (key == pushOthersKey)
{
var split = value.Split(',');
if(split.Length == 2)
foreach(var str in split)
{
AddTilesheet(farmer.currentLocation.map, split[0], split[1]);
Layer l = tile.Layer;
var split2 = str.Split(' ');
if (split2.Length == 3)
{
l = farmer.currentLocation.Map.GetLayer(split2[0]);
PushTileWithOthers(farmer, l.Tiles[int.Parse(split2[1]), int.Parse(split2[2])], tilePos);
}
else
{
PushTileWithOthers(farmer, l.Tiles[int.Parse(split2[0]), int.Parse(split2[1])], tilePos);
}
}
}
}
Expand All @@ -416,6 +449,7 @@ public static void TriggerActions(List<string> actions, List<Layer> layers, Farm

}
}
return triggered;
}

private static Layer AddLayer(Map map, string id)
Expand Down Expand Up @@ -566,6 +600,38 @@ private static Item GetItemFromString(string value)
return item;
}


private static void PushTileWithOthers(Farmer farmer, Tile tile, Point startTile)
{
List<(Point, Tile)> tileList = new() { (startTile, tile) };
if (tile.Properties.TryGetValue(pushAlsoKey, out PropertyValue others))
{
var split = others.ToString().Split(',');
foreach (var t in split)
{
try
{
var split2 = t.Split(' ');
if (split2.Length == 3 && int.TryParse(split2[1], out int x2) && int.TryParse(split2[2], out int y2))
{
x2 += startTile.X;
y2 += startTile.Y;
var layer = farmer.currentLocation.Map.GetLayer(split2[0]);
if (layer is not null && farmer.currentLocation.isTileOnMap(x2, y2) && layer.Tiles[x2, y2] is not null)
{
tileList.Add((new Point(x2, y2), layer.Tiles[x2, y2]));
}
}
}
catch (Exception ex)
{
SMonitor.Log(ex.ToString(), StardewModdingAPI.LogLevel.Error);
}
}
}
PushTiles(farmer.currentLocation, tileList, farmer.FacingDirection, farmer);
}

public static bool PushTiles(GameLocation location, List<(Point, Tile)> tileList, int dir, Farmer farmer)
{
for(int i = 0; i < tileList.Count; i++)
Expand All @@ -588,27 +654,14 @@ public static bool PushTiles(GameLocation location, List<(Point, Tile)> tileList
{
if (tileList[i].Item2.Layer.Id == "Buildings")
{
List<string> actions = new List<string>();
foreach (var kvp in tileList[i].Item2.Properties)
{
foreach (var str in actionKeys)
{
if (kvp.Key == str + "Push")
{
actions.Add(kvp.Key);
}
}
}
if (actions.Count > 0)
{
TriggerActions(actions, new List<Layer>() { tileList[i].Item2.Layer }, farmer, tileList[i].Item1, new List<string>() { "Push" });
}
TriggerActions(new List<Layer>() { tileList[i].Item2.Layer }, farmer, tileList[i].Item1, new List<string>() { "Push" });
}
pushedList.Add(new PushedTile() { tile = tileList[i].Item2, position = new Point(tileList[i].Item1.X * 64, tileList[i].Item1.Y * 64), dir = dir, farmer = farmer });
tileList[i].Item2.Layer.Tiles[new Location(tileList[i].Item1.X, tileList[i].Item1.Y)] = null;
}
return true;
}

public static Point GetNextTile(int dir)
{
switch (dir)
Expand Down
17 changes: 3 additions & 14 deletions DynamicMapTiles/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ public partial class ModEntry : Mod
public static string explosionKey = "DMT/explosion";
public static string pushKey = "DMT/push";
public static string pushableKey = "DMT/pushable";
public static string pushAlsoKey = "DMT/pushAlso";
public static string pushOthersKey = "DMT/pushOthers";
public static string soundKey = "DMT/sound";
public static string teleportKey = "DMT/teleport";
public static string teleportTileKey = "DMT/teleportTile";
public static string giveKey = "DMT/give";
public static string takeKey = "DMT/take";
public static string chestKey = "DMT/chest";
public static string chestAdvancedKey = "DMT/chestAdvanced";
public static string messageKey = "DMT/message";
Expand Down Expand Up @@ -250,20 +252,7 @@ private void GameLoop_UpdateTicked(object sender, StardewModdingAPI.Events.Updat
var t = l.PickTile(new xTile.Dimensions.Location(tile.position.X, tile.position.Y), Game1.viewport.Size);
if (t is not null)
{
foreach (var kvp in t.Properties)
{
foreach (var str in actionKeys)
{
if (kvp.Key == str + "Pushed")
{
actions.Add(kvp.Key);
}
}
}
if (actions.Count > 0)
{
TriggerActions(actions, new List<Layer>() { t.Layer }, tile.farmer, new Point(tile.position.X / 64, tile.position.Y / 64), new List<string>() { "Pushed" });
}
TriggerActions(new List<Layer>() { t.Layer }, tile.farmer, new Point(tile.position.X / 64, tile.position.Y / 64), new List<string>() { "Pushed" });
}
}
tiles.RemoveAt(i);
Expand Down
2 changes: 1 addition & 1 deletion DynamicMapTiles/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Name": "DynamicMapTiles",
"Author": "aedenthorn",
"Version": "0.6.1",
"Version": "0.7.1",
"Description": "DynamicMapTiles.",
"UniqueID": "aedenthorn.DynamicMapTiles",
"EntryDll": "DynamicMapTiles.dll",
Expand Down

0 comments on commit 3e3799a

Please sign in to comment.