Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)

This commit is contained in:
2026-05-14 11:17:02 +02:00
parent bd6cdeb97b
commit 300c8f5a42
54 changed files with 2030 additions and 1745 deletions
+108 -103
View File
@@ -1,131 +1,136 @@
using System.Collections.Generic;
using Godot;
public class Pathfinding
public static class Pathfinding
{
private static AStar3D aStar = new AStar3D();
private static Dictionary<Vector3I, long> coordToId = new Dictionary<Vector3I, long>();
private static Dictionary<long, Vector3I> idToCoord = new Dictionary<long, Vector3I>();
private static long nextId = 1;
private static Dictionary<int, (long fromId, long toId)> verticalConnections = new Dictionary<int, (long fromId, long toId)>();
private static AStar3D aStar = new AStar3D();
private static Dictionary<Vector3I, long> coordToId = new Dictionary<Vector3I, long>();
private static Dictionary<long, Vector3I> idToCoord = new Dictionary<long, Vector3I>();
private static long nextId = 1;
private static Dictionary<int, (long fromId, long toId)> verticalConnections = new Dictionary<int, (long fromId, long toId)>();
private static long GetOrCreateId(Vector3I coord)
{
if (coordToId.TryGetValue(coord, out long id))
return id;
private static long GetOrCreateId(Vector3I coord)
{
if (coordToId.TryGetValue(coord, out long id)) return id;
id = nextId++;
coordToId[coord] = id;
idToCoord[id] = coord;
id = nextId++;
coordToId[coord] = id;
idToCoord[id] = coord;
return id;
}
return id;
}
public static void BuildAStarGraph()
{
aStar.Clear();
coordToId.Clear();
idToCoord.Clear();
verticalConnections.Clear();
nextId = 1;
public static void BuildAStarGraph()
{
aStar.Clear();
coordToId.Clear();
idToCoord.Clear();
verticalConnections.Clear();
nextId = 1;
for (int y = 0; y < GameData.ruinSize; y++)
{
for (int x = 0; x < GameData.layerSize; x++)
{
for (int z = 0; z < GameData.layerSize; z++)
{
Vector3I coord = new Vector3I(x, y, z);
Tile tile = GameData.map[y].tiles[x, z];
for (int y = 0; y < GameData.ruinSize; y++)
{
for (int x = 0; x < GameData.layerSize; x++)
{
for (int z = 0; z < GameData.layerSize; z++)
{
AddPointIfValid(x, y, z);
}
}
}
if (tile == null || tile.collapsedMesh == null) continue;
foreach (KeyValuePair<Vector3I, long> kvp in coordToId)
{
ConnectPoint(kvp.Key, kvp.Value);
}
long id = GetOrCreateId(coord);
for (int y = 0; y < GameData.ruinSize; y++)
{
UpdateGatePoint(y, false);
}
}
aStar.AddPoint(id, tile.Position);
}
}
}
private static void AddPointIfValid(int x, int y, int z)
{
Vector3I coord = new Vector3I(x, y, z);
Tile tile = GameData.map[y].tiles[x, z];
if (tile == null || tile.collapsedMesh == null) return;
foreach (KeyValuePair<Vector3I, long> kvp in coordToId)
{
Vector3I from = kvp.Key;
long fromId = kvp.Value;
long id = GetOrCreateId(coord);
aStar.AddPoint(id, tile.Position);
}
foreach (Vector3I offset in WFC.offsets3D)
{
Vector3I to = new Vector3I(
from.X + offset.X,
from.Y + offset.Y,
from.Z + offset.Z
);
private static void ConnectPoint(Vector3I from, long fromId)
{
foreach (Vector3I offset in WFC.offsets3D)
{
Vector3I to = new Vector3I(
from.X + offset.X,
from.Y + offset.Y,
from.Z + offset.Z
);
if (!coordToId.ContainsKey(to)) continue;
if (!coordToId.ContainsKey(to)) continue;
if (!WFC.CanWalk3D(from, to)) continue;
if (!WFC.CanWalk3D(from, to)) continue;
long toId = coordToId[to];
if (TryRegisterGateConnection(from, to, fromId, toId)) continue;
long toId = coordToId[to];
ConnectPointsIfNeeded(fromId, toId);
}
}
if (from.Y != to.Y && GameData.map[from.Y].tiles[from.X, from.Z].collapsedMesh == "gate")
{
verticalConnections[from.Y] = (fromId, toId);
if (GameData.map[from.Y].isGateOpen)
{
if (!aStar.ArePointsConnected(fromId, toId))
{
aStar.ConnectPoints(fromId, toId, true);
}
}
continue;
}
private static bool TryRegisterGateConnection(Vector3I from, Vector3I to, long fromId, long toId)
{
if (from.Y == to.Y) return false;
if (GameData.map[from.Y].tiles[from.X, from.Z].collapsedMesh != "gate") return false;
if (!aStar.ArePointsConnected(fromId, toId))
{
aStar.ConnectPoints(fromId, toId, true);
}
}
}
verticalConnections[from.Y] = (fromId, toId);
if (GameData.map[from.Y].isGateOpen)
{
ConnectPointsIfNeeded(fromId, toId);
}
for (int y = 0; y < GameData.ruinSize; y++)
{
UpdateGatePoint(y, false);
}
}
return true;
}
public static void UpdateGatePoint(int layer, bool isOpen)
{
if (!verticalConnections.ContainsKey(layer)) return;
private static void ConnectPointsIfNeeded(long fromId, long toId)
{
if (aStar.ArePointsConnected(fromId, toId)) return;
(long fromId, long toId) = verticalConnections[layer];
aStar.ConnectPoints(fromId, toId, true);
}
if (isOpen)
{
if (!aStar.ArePointsConnected(fromId, toId))
{
aStar.ConnectPoints(fromId, toId, true);
}
}
else
{
if (aStar.ArePointsConnected(fromId, toId))
{
aStar.DisconnectPoints(fromId, toId);
}
}
}
public static void UpdateGatePoint(int layer, bool isOpen)
{
if (!verticalConnections.ContainsKey(layer)) return;
public static List<Vector3> GetPath(Vector3I start, Vector3I end)
{
if (!coordToId.ContainsKey(start) || !coordToId.ContainsKey(end)) return new List<Vector3>();
(long fromId, long toId) = verticalConnections[layer];
long startId = coordToId[start];
long endId = coordToId[end];
if (isOpen)
{
ConnectPointsIfNeeded(fromId, toId);
return;
}
return new List<Vector3>(aStar.GetPointPath(startId, endId));
}
if (aStar.ArePointsConnected(fromId, toId))
{
aStar.DisconnectPoints(fromId, toId);
}
}
public static Vector3I GetClosestStartPoint(Vector3 robotPosition)
{
return idToCoord[aStar.GetClosestPoint(robotPosition)];
}
public static List<Vector3> GetPath(Vector3I start, Vector3I end)
{
if (!coordToId.ContainsKey(start) || !coordToId.ContainsKey(end)) return new List<Vector3>();
long startId = coordToId[start];
long endId = coordToId[end];
return new List<Vector3>(aStar.GetPointPath(startId, endId));
}
public static Vector3I GetClosestStartPoint(Vector3 robotPosition)
{
return idToCoord[aStar.GetClosestPoint(robotPosition)];
}
}