Replaced some foreach loops with for loops and nullchecks to use `is` instead of `==`.

master
Katie Durden 2023-02-26 05:12:05 -08:00
parent e9ba7c96b9
commit a88172c0cf
2 changed files with 53 additions and 24 deletions

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using StardewValley; using StardewValley;
using StardewValley.Objects; using StardewValley.Objects;
@ -129,6 +130,7 @@ namespace stardew_access.Features
} }
} }
searched.Clear();
return detectedTiles; return detectedTiles;
} }
@ -138,6 +140,7 @@ namespace stardew_access.Features
/// <returns>A dictionary with all the detected tiles along with the name of the object on it and it's category.</returns> /// <returns>A dictionary with all the detected tiles along with the name of the object on it and it's category.</returns>
public Dictionary<Vector2, (string, string)> SearchLocation() public Dictionary<Vector2, (string, string)> SearchLocation()
{ {
var watch = System.Diagnostics.Stopwatch.StartNew();
Dictionary<Vector2, (string, string)> detectedTiles = new Dictionary<Vector2, (string, string)>(); Dictionary<Vector2, (string, string)> detectedTiles = new Dictionary<Vector2, (string, string)>();
Vector2 position = Vector2.Zero; Vector2 position = Vector2.Zero;
(bool, string? name, string category) tileInfo; (bool, string? name, string category) tileInfo;
@ -151,6 +154,11 @@ namespace stardew_access.Features
toSearch.Enqueue(Game1.player.getTileLocation()); toSearch.Enqueue(Game1.player.getTileLocation());
searched.Add(Game1.player.getTileLocation()); searched.Add(Game1.player.getTileLocation());
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
MainClass.DebugLog($"Search init duration: {elapsedMs}");
elapsedMs = 0;
watch = System.Diagnostics.Stopwatch.StartNew();
while (toSearch.Count > 0) while (toSearch.Count > 0)
{ {
Vector2 item = toSearch.Dequeue(); Vector2 item = toSearch.Dequeue();
@ -174,7 +182,10 @@ namespace stardew_access.Features
} }
} }
} }
watch.Stop();
elapsedMs = watch.ElapsedMilliseconds;
MainClass.DebugLog($"Search loop duration: {elapsedMs}; {count} iterations.");
searched.Clear();
return detectedTiles; return detectedTiles;
} }

View File

@ -34,7 +34,7 @@ namespace stardew_access.Features
{ {
int x = (int)tile.X; int x = (int)tile.X;
int y = (int)tile.Y; int y = (int)tile.Y;
string? toReturn = ""; string? toReturn = null;
CATEGORY? category = CATEGORY.Others; CATEGORY? category = CATEGORY.Others;
bool isColliding = isCollidingAtTile(x, y); bool isColliding = isCollidingAtTile(x, y);
@ -57,7 +57,7 @@ namespace stardew_access.Features
else else
category = CATEGORY.NPCs; category = CATEGORY.NPCs;
} }
else if (farmAnimal != null) else if (farmAnimal is not null)
{ {
toReturn = farmAnimal; toReturn = farmAnimal;
category = CATEGORY.FarmAnimals; category = CATEGORY.FarmAnimals;
@ -161,10 +161,12 @@ namespace stardew_access.Features
try try
{ {
NetCollection<Debris> droppedItems = Game1.currentLocation.debris; NetCollection<Debris> droppedItems = Game1.currentLocation.debris;
if (droppedItems.Count() > 0) int droppedItemsCount = droppedItems.Count();
if (droppedItemsCount > 0)
{ {
foreach (var item in droppedItems) for (int i = 0; i < droppedItemsCount; i++)
{ {
var item = droppedItems[i];
int xPos = ((int)item.Chunks[0].position.Value.X / Game1.tileSize) + 1; int xPos = ((int)item.Chunks[0].position.Value.X / Game1.tileSize) + 1;
int yPos = ((int)item.Chunks[0].position.Value.Y / Game1.tileSize) + 1; int yPos = ((int)item.Chunks[0].position.Value.Y / Game1.tileSize) + 1;
if (xPos != x || yPos != y) continue; if (xPos != x || yPos != y) continue;
@ -174,10 +176,11 @@ namespace stardew_access.Features
string name = item.item.DisplayName; string name = item.item.DisplayName;
int count = item.item.Stack; int count = item.item.Stack;
if (toReturn == "") if (toReturn is null)
return ($"Dropped Item: {count} {name}", CATEGORY.DroppedItems); return ($"Dropped Item: {count} {name}", CATEGORY.DroppedItems);
else else
toReturn = $"{toReturn}, Dropped Item: {count} {name}"; toReturn = $"{toReturn}, Dropped Item: {count} {name}";
item = null;
} }
} }
} }
@ -188,9 +191,6 @@ namespace stardew_access.Features
} }
#endregion #endregion
if (toReturn == "")
return (null, category);
return (toReturn, category); return (toReturn, category);
} }
@ -198,7 +198,7 @@ namespace stardew_access.Features
{ {
string? toReturn = null; string? toReturn = null;
Bush? bush = (Bush)Game1.currentLocation.getLargeTerrainFeatureAt(x, y); Bush? bush = (Bush)Game1.currentLocation.getLargeTerrainFeatureAt(x, y);
if (bush == null) if (bush is null)
return null; return null;
if (lessInfo && (bush.tilePosition.Value.X != x || bush.tilePosition.Value.Y != y)) if (lessInfo && (bush.tilePosition.Value.X != x || bush.tilePosition.Value.Y != y))
return null; return null;
@ -264,7 +264,7 @@ namespace stardew_access.Features
(46, 12) => "Bulletin Board", (46, 12) => "Bulletin Board",
_ => null, _ => null,
}; };
if (name != null && communityCenter.shouldNoteAppearInArea(CommunityCenter.getAreaNumberFromName(name))) if (name is not null && communityCenter.shouldNoteAppearInArea(CommunityCenter.getAreaNumberFromName(name)))
return $"{name} bundle"; return $"{name} bundle";
} }
else if (Game1.currentLocation is AbandonedJojaMart) else if (Game1.currentLocation is AbandonedJojaMart)
@ -275,7 +275,7 @@ namespace stardew_access.Features
_ => null, _ => null,
}; };
if (name != null) if (name is not null)
return $"{name} bundle"; return $"{name} bundle";
} }
@ -286,6 +286,7 @@ namespace stardew_access.Features
{ {
Rectangle rect = new Rectangle(x * 64 + 1, y * 64 + 1, 62, 62); Rectangle rect = new Rectangle(x * 64 + 1, y * 64 + 1, 62, 62);
/* Reference
// Check whether the position is a warp point, if so then return false, sometimes warp points are 1 tile off the map for example in coops and barns // Check whether the position is a warp point, if so then return false, sometimes warp points are 1 tile off the map for example in coops and barns
if (isWarpPointAtTile(x, y)) return false; if (isWarpPointAtTile(x, y)) return false;
@ -294,19 +295,32 @@ namespace stardew_access.Features
return true; return true;
} }
if (Game1.currentLocation is Woods && getStumpsInWoods(x, y) != null) if (Game1.currentLocation is Woods && getStumpsInWoods(x, y) is not null)
return true; return true;
return false; return false;
*/
// Optimized
// Sometimes warp points are 1 tile off the map for example in coops and barns; check that this is not a warp point
if (!isWarpPointAtTile(x, y))
{
// not a warp point
//directly return the value of the logical comparison rather than wasting time in conditional
return ((Game1.currentLocation.isCollidingPosition(rect, Game1.viewport, true, 0, glider: false, Game1.player, pathfinding: true)) || (Game1.currentLocation is Woods && getStumpsInWoods(x, y) is not null));
}
// was a warp point; return false
return false;
} }
public static Boolean isWarpPointAtTile(int x, int y) public static Boolean isWarpPointAtTile(int x, int y)
{ {
if (Game1.currentLocation == null) return false; if (Game1.currentLocation is null) return false;
foreach (Warp warpPoint in Game1.currentLocation.warps) int warpsCount = Game1.currentLocation.warps.Count();
for (int i = 0; i < warpsCount; i++)
{ {
if (warpPoint.X == x && warpPoint.Y == y) return true; if (Game1.currentLocation.warps[i].X == x && Game1.currentLocation.warps[i].Y == y) return true;
} }
return false; return false;
@ -314,11 +328,11 @@ namespace stardew_access.Features
public static string? getFarmAnimalAt(GameLocation? location, int x, int y) public static string? getFarmAnimalAt(GameLocation? location, int x, int y)
{ {
if (location == null) if (location is null || (location is not Farm && location is not AnimalHouse))
return null; return null;
if (location is not Farm && location is not AnimalHouse) //if (location is not Farm && location is not AnimalHouse)
return null; //return null;
List<FarmAnimal>? farmAnimals = null; List<FarmAnimal>? farmAnimals = null;
@ -368,7 +382,7 @@ namespace stardew_access.Features
else else
{ {
Building building = farm.getBuildingAt(new Vector2(x, y)); Building building = farm.getBuildingAt(new Vector2(x, y));
if (building != null) if (building is not null)
{ {
string name = building.buildingType.Value; string name = building.buildingType.Value;
@ -397,7 +411,7 @@ namespace stardew_access.Features
} }
} }
} }
else if (Game1.currentLocation.currentEvent != null) else if (Game1.currentLocation.currentEvent is not null)
{ {
string event_name = Game1.currentLocation.currentEvent.FestivalName; string event_name = Game1.currentLocation.currentEvent.FestivalName;
if (event_name == "Egg Festival" && x == 21 && y == 55) if (event_name == "Egg Festival" && x == 21 && y == 55)
@ -1198,8 +1212,10 @@ namespace stardew_access.Features
{ {
if (Game1.currentLocation == null) return null; if (Game1.currentLocation == null) return null;
foreach (Warp warpPoint in Game1.currentLocation.warps) int warpCount = Game1.currentLocation.warps.Count();
for (int i = 0; i < warpCount; i++)
{ {
Warp warpPoint = Game1.currentLocation.warps[i];
if (warpPoint.X != x || warpPoint.Y != y) continue; if (warpPoint.X != x || warpPoint.Y != y) continue;
return $"{warpPoint.TargetName} Entrance"; return $"{warpPoint.TargetName} Entrance";
@ -1303,8 +1319,10 @@ namespace stardew_access.Features
if (Game1.currentLocation is not IslandLocation islandLocation) if (Game1.currentLocation is not IslandLocation islandLocation)
return null; return null;
foreach (var perch in islandLocation.parrotUpgradePerches) int perchCount = islandLocation.parrotUpgradePerches.Count();
for (int i = 0; i < perchCount; i++)
{ {
var perch = islandLocation.parrotUpgradePerches[i];
if (!perch.tilePosition.Value.Equals(new Point(x, y))) if (!perch.tilePosition.Value.Equals(new Point(x, y)))
continue; continue;