Added method to scan entire location for tiles

master
Mohammad Shoaib 2022-05-01 13:20:18 +05:30
parent 666dfd745b
commit c3c0169ac6
2 changed files with 103 additions and 50 deletions

View File

@ -133,6 +133,34 @@ namespace stardew_access.Features
return detectedTiles; return detectedTiles;
} }
/// <summary>
/// Search the entire location.
/// </summary>
/// <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()
{
Dictionary<Vector2, (string, string)> detectedTiles = new Dictionary<Vector2, (string, string)>();
Vector2 position = Vector2.Zero;
(bool, string? name, string category) tileInfo;
// FIXME try using BFS
for (int i = 0; i < Game1.currentLocation.Map.Layers[0].LayerSize.Width; i++)
{
for (int j = 0; j < Game1.currentLocation.Map.Layers[0].LayerSize.Height; j++)
{
position.X = i;
position.Y = j;
tileInfo = CheckTile(position, true);
if (tileInfo.Item1 && tileInfo.name != null)
{
MainClass.DebugLog($"\n{tileInfo.name}:\t{tileInfo.category}");
detectedTiles.Add(position, (tileInfo.name, tileInfo.category));
}
}
}
return detectedTiles;
}
/// <summary> /// <summary>
/// Checks if the provided tile position is within the range/radius and whether the tile has already been checked or not. /// Checks if the provided tile position is within the range/radius and whether the tile has already been checked or not.
/// </summary> /// </summary>
@ -154,9 +182,9 @@ namespace stardew_access.Features
return true; return true;
} }
public (bool, string?, string) CheckTile(Vector2 position) public (bool, string? name, string category) CheckTile(Vector2 position, bool lessInfo = false)
{ {
(string? name, CATEGORY? category) tileDetail = TileInfo.getNameWithCategoryAtTile(position); (string? name, CATEGORY? category) tileDetail = TileInfo.getNameWithCategoryAtTile(position, lessInfo);
if (tileDetail.name == null) if (tileDetail.name == null)
return (false, null, CATEGORY.Others.ToString()); return (false, null, CATEGORY.Others.ToString());

View File

@ -29,7 +29,7 @@ namespace stardew_access.Features
} }
///<summary>Returns the name of the object at tile alongwith it's category</summary> ///<summary>Returns the name of the object at tile alongwith it's category</summary>
public static (string? name, CATEGORY? category) getNameWithCategoryAtTile(Vector2 tile) public static (string? name, CATEGORY? category) getNameWithCategoryAtTile(Vector2 tile, bool lessInfo = false)
{ {
int x = (int)tile.X; int x = (int)tile.X;
int y = (int)tile.Y; int y = (int)tile.Y;
@ -37,14 +37,15 @@ namespace stardew_access.Features
CATEGORY? category = CATEGORY.Others; CATEGORY? category = CATEGORY.Others;
bool isColliding = isCollidingAtTile(x, y); bool isColliding = isCollidingAtTile(x, y);
Dictionary<Vector2, Netcode.NetRef<TerrainFeature>> terrainFeature = Game1.currentLocation.terrainFeatures.FieldDict; var terrainFeature = Game1.currentLocation.terrainFeatures.FieldDict;
string? door = getDoorAtTile(x, y); string? door = getDoorAtTile(x, y);
(CATEGORY? category, string? name) dynamicTile = getDynamicTilesInfo(x, y); (CATEGORY? category, string? name) dynamicTile = getDynamicTilesInfo(x, y, lessInfo);
string? junimoBundle = getJunimoBundleAt(x, y); string? junimoBundle = getJunimoBundleAt(x, y);
string? resourceClump = getResourceClumpAtTile(x, y); string? resourceClump = getResourceClumpAtTile(x, y, lessInfo);
string? farmAnimal = getFarmAnimalAt(Game1.currentLocation, x, y); string? farmAnimal = getFarmAnimalAt(Game1.currentLocation, x, y);
string? parrot = getParrotPerchAtTile(x, y); string? parrot = getParrotPerchAtTile(x, y);
(string? name, CATEGORY category) staticTile = MainClass.STiles.getStaticTileInfoAtWithCategory(x, y); (string? name, CATEGORY category) staticTile = MainClass.STiles.getStaticTileInfoAtWithCategory(x, y);
string? bush = getBushAtTile(x, y, lessInfo);
if (Game1.currentLocation.isCharacterAtTile(tile) != null) if (Game1.currentLocation.isCharacterAtTile(tile) != null)
{ {
@ -70,24 +71,24 @@ namespace stardew_access.Features
toReturn = dynamicTile.name; toReturn = dynamicTile.name;
category = dynamicTile.category; category = dynamicTile.category;
} }
else if (Game1.currentLocation is VolcanoDungeon && ((VolcanoDungeon)Game1.currentLocation).IsCooledLava(x, y)) else if (Game1.currentLocation is VolcanoDungeon && ((VolcanoDungeon)Game1.currentLocation).IsCooledLava(x, y) && !lessInfo)
{ {
toReturn = "Cooled lava"; toReturn = "Cooled lava";
category = CATEGORY.WaterTiles; category = CATEGORY.WaterTiles;
} }
else if (Game1.currentLocation is VolcanoDungeon && StardewValley.Monsters.LavaLurk.IsLavaTile((VolcanoDungeon)Game1.currentLocation, x, y)) else if (Game1.currentLocation is VolcanoDungeon && StardewValley.Monsters.LavaLurk.IsLavaTile((VolcanoDungeon)Game1.currentLocation, x, y) && !lessInfo)
{ {
toReturn = "Lava"; toReturn = "Lava";
category = CATEGORY.WaterTiles; category = CATEGORY.WaterTiles;
} }
else if (Game1.currentLocation.isWaterTile(x, y) && isColliding) else if (Game1.currentLocation.isWaterTile(x, y) && isColliding && !lessInfo)
{ {
toReturn = "Water"; toReturn = "Water";
category = CATEGORY.WaterTiles; category = CATEGORY.WaterTiles;
} }
else if (Game1.currentLocation.isObjectAtTile(x, y)) else if (Game1.currentLocation.isObjectAtTile(x, y))
{ {
(string? name, CATEGORY? category) obj = getObjectAtTile(x, y); (string? name, CATEGORY? category) obj = getObjectAtTile(x, y, lessInfo);
toReturn = obj.name; toReturn = obj.name;
category = obj.category; category = obj.category;
} }
@ -102,9 +103,9 @@ namespace stardew_access.Features
} }
} }
else if (Game1.currentLocation.getLargeTerrainFeatureAt(x, y) != null) else if (bush != null)
{ {
toReturn = getBushAtTile(x, y); toReturn = bush;
category = CATEGORY.Bush; category = CATEGORY.Bush;
} }
else if (resourceClump != null) else if (resourceClump != null)
@ -149,10 +150,15 @@ namespace stardew_access.Features
return (toReturn, category); return (toReturn, category);
} }
public static string? getBushAtTile(int x, int y) public static string? getBushAtTile(int x, int y, bool lessInfo = false)
{ {
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)
return null;
if (lessInfo && (bush.tilePosition.Value.X != x || bush.tilePosition.Value.Y != y))
return null;
int size = bush.size.Value; int size = bush.size.Value;
#region Check if bush is harvestable or not #region Check if bush is harvestable or not
@ -293,19 +299,23 @@ namespace stardew_access.Features
/// <param name="y"></param> /// <param name="y"></param>
/// <returns>category: This is the category of the tile. Default to Furnitures. /// <returns>category: This is the category of the tile. Default to Furnitures.
/// <br/>name: This is the name of the tile. Default to null if the tile tile has nothing on it.</returns> /// <br/>name: This is the name of the tile. Default to null if the tile tile has nothing on it.</returns>
public static (CATEGORY? category, string? name) getDynamicTilesInfo(int x, int y) public static (CATEGORY? category, string? name) getDynamicTilesInfo(int x, int y, bool lessInfo = false)
{ {
if (Game1.currentLocation is Farm farm) if (Game1.currentLocation is Farm farm)
{ {
Building building = farm.getBuildingAt(new Vector2(x, y)); Building building = farm.getBuildingAt(new Vector2(x, y));
if (building != null) if (building != null)
{ {
string name = building.buildingType.Value;
if ((building.humanDoor.Value.X + building.tileX.Value) == x && (building.humanDoor.Value.Y + building.tileY.Value) == y) if ((building.humanDoor.Value.X + building.tileX.Value) == x && (building.humanDoor.Value.Y + building.tileY.Value) == y)
return (CATEGORY.Doors, building.buildingType.Value + " Door"); return (CATEGORY.Doors, name + " Door");
else if ((building.animalDoor.Value.X + building.tileX.Value) == x && (building.animalDoor.Value.Y + building.tileY.Value) == y) else if ((building.animalDoor.Value.X + building.tileX.Value) == x && (building.animalDoor.Value.Y + building.tileY.Value) == y)
return (CATEGORY.Doors, building.buildingType.Value + " Animal Door " + ((building.animalDoorOpen.Value) ? "Opened" : "Closed")); return (CATEGORY.Doors, name + " Animal Door " + ((building.animalDoorOpen.Value) ? "Opened" : "Closed"));
else else if (building.tileX.Value == x && building.tileY.Value == y)
return (CATEGORY.Buildings, building.buildingType.Value); return (CATEGORY.Buildings, name);
else if (!lessInfo)
return (CATEGORY.Buildings, name);
} }
} }
else if (Game1.currentLocation is Town) else if (Game1.currentLocation is Town)
@ -582,7 +592,7 @@ namespace stardew_access.Features
} }
#region Objects #region Objects
public static (string? name, CATEGORY category) getObjectAtTile(int x, int y) public static (string? name, CATEGORY category) getObjectAtTile(int x, int y, bool lessInfo = false)
{ {
(string? name, CATEGORY category) toReturn = (null, CATEGORY.Others); (string? name, CATEGORY category) toReturn = (null, CATEGORY.Others);
@ -604,7 +614,16 @@ namespace stardew_access.Features
toReturn = (chest.DisplayName, CATEGORY.Chests); toReturn = (chest.DisplayName, CATEGORY.Chests);
} }
else if (obj is Furniture) else if (obj is Furniture)
toReturn.category = CATEGORY.Furnitures; {
if (lessInfo && (((Furniture)obj).TileLocation.X != x || ((Furniture)obj).TileLocation.Y != y))
{
toReturn.category = CATEGORY.Others;
toReturn.name = null;
}
else
toReturn.category = CATEGORY.Furnitures;
}
else if (obj.Type == "Crafting" && obj.bigCraftable.Value) else if (obj.Type == "Crafting" && obj.bigCraftable.Value)
{ {
@ -618,7 +637,7 @@ namespace stardew_access.Features
} }
} }
if (toReturn.category == CATEGORY.Others) // Fix for `Harvestable table` and `Busy nodes` if (toReturn.category == CATEGORY.Machines) // Fix for `Harvestable table` and `Busy nodes`
{ {
MachineState machineState = GetMachineState(obj); MachineState machineState = GetMachineState(obj);
if (machineState == MachineState.Ready) if (machineState == MachineState.Ready)
@ -854,42 +873,45 @@ namespace stardew_access.Features
return null; return null;
} }
public static string? getResourceClumpAtTile(int x, int y) public static string? getResourceClumpAtTile(int x, int y, bool lessInfo = false)
{ {
if (Game1.currentLocation is Woods) if (Game1.currentLocation is Woods)
return getStumpsInWoods(x, y); return getStumpsInWoods(x, y, true);
for (int i = 0; i < Game1.currentLocation.resourceClumps.Count; i++) for (int i = 0; i < Game1.currentLocation.resourceClumps.Count; i++)
{ {
if (Game1.currentLocation.resourceClumps[i].occupiesTile(x, y)) if (!Game1.currentLocation.resourceClumps[i].occupiesTile(x, y))
{ continue;
int index = Game1.currentLocation.resourceClumps[i].parentSheetIndex.Value;
switch (index) if (lessInfo && (Game1.currentLocation.resourceClumps[i].tile.X != x || Game1.currentLocation.resourceClumps[i].tile.Y != y))
{ continue;
case 600:
return "Large Stump"; int index = Game1.currentLocation.resourceClumps[i].parentSheetIndex.Value;
case 602:
return "Hollow Log"; switch (index)
case 622: {
return "Meteorite"; case 600:
case 752: return "Large Stump";
case 754: case 602:
case 756: return "Hollow Log";
case 758: case 622:
return "Mine Rock"; return "Meteorite";
case 672: case 752:
return "Boulder"; case 754:
default: case 756:
return "Unknown"; case 758:
} return "Mine Rock";
case 672:
return "Boulder";
default:
return "Unknown";
} }
} }
return null; return null;
} }
public static string? getStumpsInWoods(int x, int y) public static string? getStumpsInWoods(int x, int y, bool lessInfo = false)
{ {
if (Game1.currentLocation is not Woods) if (Game1.currentLocation is not Woods)
return null; return null;
@ -901,10 +923,13 @@ namespace stardew_access.Features
Netcode.NetObjectList<ResourceClump> stumps = ((Woods)Game1.currentLocation).stumps; Netcode.NetObjectList<ResourceClump> stumps = ((Woods)Game1.currentLocation).stumps;
for (int i = 0; i < stumps.Count; i++) for (int i = 0; i < stumps.Count; i++)
{ {
if (stumps[i].occupiesTile(x, y)) if (!stumps[i].occupiesTile(x, y))
{ continue;
return "Large Stump";
} if (lessInfo && (stumps[i].tile.X != x || stumps[i].tile.Y != y))
continue;
return "Large Stump";
} }
return null; return null;
} }