From be3906af4da0bb92f834258ee857b3d4cc8cb073 Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Mon, 27 Sep 2021 11:22:46 +0200 Subject: [PATCH 1/3] Test Created some folders. --- Assets/Scenes/NPCTest.unity | 331 ++++++++++++++++++ Assets/Scenes/NPCTest.unity.meta | 7 + Assets/Scripts/NPC.meta | 8 + Assets/Scripts/{ => NPC}/PathGenerator.cs | 0 .../Scripts/{ => NPC}/PathGenerator.cs.meta | 0 Assets/Scripts/{ => NPC}/PathMap.cs | 0 Assets/Scripts/{ => NPC}/PathMap.cs.meta | 0 UserSettings/EditorUserSettings.asset | 3 + 8 files changed, 349 insertions(+) create mode 100644 Assets/Scenes/NPCTest.unity create mode 100644 Assets/Scenes/NPCTest.unity.meta create mode 100644 Assets/Scripts/NPC.meta rename Assets/Scripts/{ => NPC}/PathGenerator.cs (100%) rename Assets/Scripts/{ => NPC}/PathGenerator.cs.meta (100%) rename Assets/Scripts/{ => NPC}/PathMap.cs (100%) rename Assets/Scripts/{ => NPC}/PathMap.cs.meta (100%) diff --git a/Assets/Scenes/NPCTest.unity b/Assets/Scenes/NPCTest.unity new file mode 100644 index 0000000..52c86de --- /dev/null +++ b/Assets/Scenes/NPCTest.unity @@ -0,0 +1,331 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_IndirectSpecularColor: {r: 0.44657826, g: 0.49641263, b: 0.57481676, a: 1} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 2 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + accuratePlacement: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!1 &329646042 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 329646044} + - component: {fileID: 329646043} + m_Layer: 0 + m_Name: Directional Light + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!108 &329646043 +Light: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 329646042} + m_Enabled: 1 + serializedVersion: 10 + m_Type: 1 + m_Shape: 0 + m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} + m_Intensity: 1 + m_Range: 10 + m_SpotAngle: 30 + m_InnerSpotAngle: 21.80208 + m_CookieSize: 10 + m_Shadows: + m_Type: 2 + m_Resolution: -1 + m_CustomResolution: -1 + m_Strength: 1 + m_Bias: 0.05 + m_NormalBias: 0.4 + m_NearPlane: 0.2 + m_CullingMatrixOverride: + e00: 1 + e01: 0 + e02: 0 + e03: 0 + e10: 0 + e11: 1 + e12: 0 + e13: 0 + e20: 0 + e21: 0 + e22: 1 + e23: 0 + e30: 0 + e31: 0 + e32: 0 + e33: 1 + m_UseCullingMatrixOverride: 0 + m_Cookie: {fileID: 0} + m_DrawHalo: 0 + m_Flare: {fileID: 0} + m_RenderMode: 0 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingLayerMask: 1 + m_Lightmapping: 4 + m_LightShadowCasterMode: 0 + m_AreaSize: {x: 1, y: 1} + m_BounceIntensity: 1 + m_ColorTemperature: 6570 + m_UseColorTemperature: 0 + m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} + m_UseBoundingSphereOverride: 0 + m_UseViewFrustumForShadowCasterCull: 1 + m_ShadowRadius: 0 + m_ShadowAngle: 0 +--- !u!4 &329646044 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 329646042} + m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} + m_LocalPosition: {x: 0, y: 3, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!1 &1141765714 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1141765717} + - component: {fileID: 1141765716} + - component: {fileID: 1141765715} + m_Layer: 0 + m_Name: Main Camera + m_TagString: MainCamera + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!81 &1141765715 +AudioListener: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1141765714} + m_Enabled: 1 +--- !u!20 &1141765716 +Camera: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1141765714} + m_Enabled: 1 + serializedVersion: 2 + m_ClearFlags: 1 + m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} + m_projectionMatrixMode: 1 + m_GateFitMode: 2 + m_FOVAxisMode: 0 + m_SensorSize: {x: 36, y: 24} + m_LensShift: {x: 0, y: 0} + m_FocalLength: 50 + m_NormalizedViewPortRect: + serializedVersion: 2 + x: 0 + y: 0 + width: 1 + height: 1 + near clip plane: 0.3 + far clip plane: 1000 + field of view: 60 + orthographic: 0 + orthographic size: 5 + m_Depth: -1 + m_CullingMask: + serializedVersion: 2 + m_Bits: 4294967295 + m_RenderingPath: -1 + m_TargetTexture: {fileID: 0} + m_TargetDisplay: 0 + m_TargetEye: 3 + m_HDR: 1 + m_AllowMSAA: 1 + m_AllowDynamicResolution: 0 + m_ForceIntoRT: 0 + m_OcclusionCulling: 1 + m_StereoConvergence: 10 + m_StereoSeparation: 0.022 +--- !u!4 &1141765717 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1141765714} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1438121554 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1438121555} + m_Layer: 0 + m_Name: Map + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1438121555 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1438121554} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 32.52278, y: 6.42986, z: 12.960086} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 2 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Scenes/NPCTest.unity.meta b/Assets/Scenes/NPCTest.unity.meta new file mode 100644 index 0000000..d159462 --- /dev/null +++ b/Assets/Scenes/NPCTest.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3fa94fc28affbde4c8273b7f6e39d2d6 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC.meta b/Assets/Scripts/NPC.meta new file mode 100644 index 0000000..c76bd87 --- /dev/null +++ b/Assets/Scripts/NPC.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: ef793abf4edd2e04b8782a5259797638 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PathGenerator.cs b/Assets/Scripts/NPC/PathGenerator.cs similarity index 100% rename from Assets/Scripts/PathGenerator.cs rename to Assets/Scripts/NPC/PathGenerator.cs diff --git a/Assets/Scripts/PathGenerator.cs.meta b/Assets/Scripts/NPC/PathGenerator.cs.meta similarity index 100% rename from Assets/Scripts/PathGenerator.cs.meta rename to Assets/Scripts/NPC/PathGenerator.cs.meta diff --git a/Assets/Scripts/PathMap.cs b/Assets/Scripts/NPC/PathMap.cs similarity index 100% rename from Assets/Scripts/PathMap.cs rename to Assets/Scripts/NPC/PathMap.cs diff --git a/Assets/Scripts/PathMap.cs.meta b/Assets/Scripts/NPC/PathMap.cs.meta similarity index 100% rename from Assets/Scripts/PathMap.cs.meta rename to Assets/Scripts/NPC/PathMap.cs.meta diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index 78a8391..b8ddf60 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -11,6 +11,9 @@ EditorUserSettings: RecentlyUsedScenePath-1: value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d flags: 0 + RecentlyUsedScenePath-2: + value: 22424703114646680e0b0227036c712035231d393866333e243d04 + flags: 0 vcSharedLogLevel: value: 0d5e400f0650 flags: 0 From c2f412b62e2cc553981064dd8ad3442bec782282 Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Tue, 28 Sep 2021 11:22:17 +0200 Subject: [PATCH 2/3] PathMap Class +Made the PathMap into a standalone class --- Assets/Scenes/NPCTest.unity | 13 ++ Assets/Scripts/NPC/NPCController.cs | 45 ++++ Assets/Scripts/NPC/NPCController.cs.meta | 11 + Assets/Scripts/NPC/PathGenerator.cs | 30 ++- Assets/Scripts/NPC/PathMap.cs | 260 ++++++++++++++++------- 5 files changed, 280 insertions(+), 79 deletions(-) create mode 100644 Assets/Scripts/NPC/NPCController.cs create mode 100644 Assets/Scripts/NPC/NPCController.cs.meta diff --git a/Assets/Scenes/NPCTest.unity b/Assets/Scenes/NPCTest.unity index 52c86de..209bdca 100644 --- a/Assets/Scenes/NPCTest.unity +++ b/Assets/Scenes/NPCTest.unity @@ -308,6 +308,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1438121555} + - component: {fileID: 1438121556} m_Layer: 0 m_Name: Map m_TagString: Untagged @@ -329,3 +330,15 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1438121556 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1438121554} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ef9b1c03bb478e84b931f1cbb3bbab8c, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Scripts/NPC/NPCController.cs b/Assets/Scripts/NPC/NPCController.cs new file mode 100644 index 0000000..a343cdb --- /dev/null +++ b/Assets/Scripts/NPC/NPCController.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class NPCController : MonoBehaviour +{ + + PathMap Map; + private GameObject[,] ball; + public List path; + + // Start is called before the first frame update + void Start() + { + ball = new GameObject[30, 30]; + Map = new PathMap(new Vector3(0, 0, 0), 30, 30, 30); + for (int r = 0; r < 30; r++) + { + for (int c = 0; c < 30; c++) + { + GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Cube); + sphere.transform.position = Map.map[r, c].Position; + ball[r, c] = sphere; + + + } + } + path = Map.QueryNodes(new Vector3(0,0, 0), new Vector3(100, 0,100)); + //Debug.Log("yeet"); + Debug.Log(path.Count); + Debug.Log((int)path[0].index.x + " "+ (int)path[0].index.y); + for (int i = 0; i < path.Count - 1; i++) { + int x = path[i].index.x; + int y = path[i].index.y; + Destroy(ball[x, y]); + } + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Scripts/NPC/NPCController.cs.meta b/Assets/Scripts/NPC/NPCController.cs.meta new file mode 100644 index 0000000..098ddac --- /dev/null +++ b/Assets/Scripts/NPC/NPCController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef9b1c03bb478e84b931f1cbb3bbab8c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/NPC/PathGenerator.cs b/Assets/Scripts/NPC/PathGenerator.cs index 4d5b404..10a97a8 100644 --- a/Assets/Scripts/NPC/PathGenerator.cs +++ b/Assets/Scripts/NPC/PathGenerator.cs @@ -1,3 +1,5 @@ + + using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -6,12 +8,32 @@ public class PathNode { private Vector3 position; - public Vector2 index; - private float score; - public PathNode(Vector3 Pos, float Score) { + public Vector2Int index; + private float scoreF; + private float scoreG; + private float scoreH; + public List neigbors; + private PathMap lowerLevel; + private PathNode previous; + + public PathNode(Vector3 Pos) + { + neigbors = new List(); position = Pos; - score = Score; + scoreG = Mathf.Infinity; + scoreF = Mathf.Infinity; + scoreH = Mathf.Infinity; } + + public void activateNextLevel() + { + //lowerLevel = new PathMap(30, 30, float width, float height); + } + public Vector3 Position { get => position; set => position = value; } + public float Hscore { get => scoreH; set => scoreH = value; } + public float Gscore { get => scoreG; set => scoreG = value; } + public float Fscore { get => scoreF; set => scoreF = value; } + public PathNode Previous { get => previous; set => previous = value; } } diff --git a/Assets/Scripts/NPC/PathMap.cs b/Assets/Scripts/NPC/PathMap.cs index 49d1c86..4d71fb8 100644 --- a/Assets/Scripts/NPC/PathMap.cs +++ b/Assets/Scripts/NPC/PathMap.cs @@ -2,118 +2,228 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class PathMap : MonoBehaviour +public class PathMap { + + private Vector3 position; - private PathNode[,] map; - private GameObject[,] ball; - private int rows = 40; - private int cols = 40; + public PathNode[,] map; + + private int rows = 30; + private int cols = 30; private float spacing = 1f; private float height = 0; + public Terrain t; + private float w, h; private PathNode[] uncheckedNodes; private List openList; private List closedList; private List nextList; - void Start() + private List path; + + + public PathMap(Vector3 Position, int Rows, int Cols, float Width) { - map = new PathNode[40, 40]; - ball = new GameObject[rows, cols]; + position = Position; + rows = Rows; + cols = Cols; + w = Width; + + //Array of all pathnodes in this chunk. + map = new PathNode[rows, cols]; + + + //only for debugging + + + openList = new List(); closedList = new List(); - nextList = new List(); + //Path that will be returned at the end. + path = new List(); + + //Add all nodes into the map. for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { - PathNode node = new PathNode(new Vector3(r * spacing, height, c * spacing), 1f); - node.index = new Vector2(r,c); + PathNode node = new PathNode(new Vector3(position.x + r * spacing, position.y + height, position.z + c * spacing)); + node.index = new Vector2Int(r, c); map[r, c] = node; - } - } - - - for (int r = 0; r < rows; r++) - { - for (int c = 0; c < cols; c++) - { - GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); - sphere.transform.position = map[r,c].Position; - ball[r,c] = sphere; - } } - FindClosestNode(new Vector3(0.7f,2,0.7f)); + + //Add the references to the neighbors of all nodes + AddAllNeighbors(); } - private void AddAllNeigbors(Vector2 index) { - if ((int)index.x - 1 >= 0 && (int) index.y - 1 >= 0 && !openList.Contains(map[(int)index.x - 1, (int)index.y - 1])) { - openList.Add(map[(int)index.x - 1, (int)index.y - 1]); - } - if ((int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x, (int)index.y - 1])) - { - openList.Add(map[(int)index.x, (int)index.y - 1]); - } - if ((int)index.x + 1 <= rows && (int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x + 1, (int)index.y - 1])) - { - openList.Add(map[(int)index.x + 1, (int)index.y - 1]); - } - if ((int)index.x - 1 >= 0 && !openList.Contains(map[(int)index.x - 1, (int)index.y])) - { - openList.Add(map[(int)index.x - 1, (int)index.y]); - } - if ((int)index.x + 1 <= rows && (int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x + 1, (int)index.y])) - { - openList.Add(map[(int)index.x + 1, (int)index.y]); - } - - - - if ((int)index.x - 1 >= 0 && (int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x - 1, (int)index.y + 1])) - { - openList.Add(map[(int)index.x - 1, (int)index.y + 1]); - } - if ((int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x, (int)index.y + 1])) - { - openList.Add(map[(int)index.x, (int)index.y + 1]); - } - if ((int)index.x + 1 <= rows && (int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x + 1, (int)index.y + 1])) - { - openList.Add(map[(int)index.x + 1, (int)index.y + 1]); - } - - } - private PathNode FindClosestNode(Vector3 pos) { + private PathNode FindClosestNode(Vector3 pos) + { if (pos.x > 0 && pos.x < rows * spacing && pos.z > 0 && pos.z < cols * spacing) { - Destroy(ball[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]); - return map[Mathf.RoundToInt(pos.x / 2), Mathf.RoundToInt(pos.z / 2)]; + + return map[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]; } - else { - return null; - } - } - public void QueryNodes() { - PathNode currentNode; + Vector2Int best = new Vector2Int(0,0); + for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { - currentNode = map[r,c]; + if (Vector3.Distance(map[best.x, best.y].Position, pos) > Vector3.Distance(map[r, c].Position, pos)) { + best.Set(r,c); + } } } + return map[best.x,best.y]; + } + public List QueryNodes(Vector3 Vstart, Vector3 Vend) + { + bool finished = false; + + PathNode start = FindClosestNode(Vstart); + start.Gscore = 0; + PathNode end = FindClosestNode(Vend); + + Debug.Log("Searching a path from " + start.index + " to " + end.index); + openList.Add(start); + + PathNode current; + int d = 0; + while (!finished) + { + d++; + if (d > 1000) + { + Debug.Log("Mist! Has not found a path"); + return null; + } + int winner = 0; + for (int i = 0; i < openList.Count; i++) + { + + if (openList[i].Fscore < openList[winner].Fscore) winner = i; + } + current = openList[winner]; + openList.RemoveAt(winner); + closedList.Add(current); + + + + if (current != end) + { + foreach (PathNode p in current.neigbors) + { + if (!closedList.Contains(p)) + { + float tempG = current.Gscore + heuristic(p.Position, current.Position); + bool newPath = false; + if (openList.Contains(p)) + { + if (tempG < p.Gscore) + { + p.Gscore = tempG; + newPath = true; + } + } + else + { + p.Gscore = tempG; + newPath = true; + openList.Add(p); + } + if (newPath) + { + p.Hscore = heuristic(p.Position, end.Position); + p.Fscore = p.Gscore + p.Hscore; + p.Previous = current; + } + + } + + } + } + else + { + Debug.Log("Path Has Been Found"); + PathNode temp = end; + path.Add(temp); + while (temp.Previous != null) + { + path.Add(temp.Previous); + temp = temp.Previous; + } + path.Add(start); + finished = true; + return path; + } + } + return null; + } + + + private float heuristic(Vector3 pos1, Vector3 pos2) + { + //Calculates the HScore for a node. + return Vector3.Distance(pos1, pos2); + + } + private void AddAllNeighbors() + { + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; c++) + { + AddNeighbors(new Vector2(r, c)); + } + } } - - // Update is called once per frame - void Update() + private void AddNeighbors(Vector2 index) { + //Adds references to all neigbors of a node. + if ((int)index.x - 1 >= 0 && (int)index.y - 1 >= 0) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y - 1]); + } + if ((int)index.y - 1 >= 0) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x, (int)index.y - 1]); + } + if ((int)index.x + 1 < rows && (int)index.y - 1 >= 0) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y - 1]); + } + + if ((int)index.x - 1 >= 0) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y]); + } + if ((int)index.x + 1 < rows && (int)index.y >= 0) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y]); + } + + + + if ((int)index.x - 1 >= 0 && (int)index.y + 1 < cols) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y + 1]); + } + if ((int)index.y + 1 < cols) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x, (int)index.y + 1]); + } + if ((int)index.x + 1 < rows && (int)index.y + 1 < cols) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y + 1]); + } } -} +} \ No newline at end of file From 6deadb9c227637fecd3f08254f75773e225beaa8 Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Thu, 30 Sep 2021 17:38:44 +0200 Subject: [PATCH 3/3] Pathfinding rework Pulled the whole thing apart and glued it back together --- Assets/Scenes/NPCTest.unity | 2 + Assets/Scripts/NPC/NPCController.cs | 37 +- Assets/Scripts/NPC/PathMap.cs | 443 +++++++++++++++--- .../NPC/{PathGenerator.cs => PathNode.cs} | 19 +- ...PathGenerator.cs.meta => PathNode.cs.meta} | 2 +- 5 files changed, 400 insertions(+), 103 deletions(-) rename Assets/Scripts/NPC/{PathGenerator.cs => PathNode.cs} (64%) rename Assets/Scripts/NPC/{PathGenerator.cs.meta => PathNode.cs.meta} (83%) diff --git a/Assets/Scenes/NPCTest.unity b/Assets/Scenes/NPCTest.unity index 209bdca..41cafcd 100644 --- a/Assets/Scenes/NPCTest.unity +++ b/Assets/Scenes/NPCTest.unity @@ -342,3 +342,5 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: ef9b1c03bb478e84b931f1cbb3bbab8c, type: 3} m_Name: m_EditorClassIdentifier: + startPoint: {x: 0, y: 0, z: 0} + endPoint: {x: 200, y: 0, z: 200} diff --git a/Assets/Scripts/NPC/NPCController.cs b/Assets/Scripts/NPC/NPCController.cs index a343cdb..0d38af2 100644 --- a/Assets/Scripts/NPC/NPCController.cs +++ b/Assets/Scripts/NPC/NPCController.cs @@ -8,32 +8,31 @@ public class NPCController : MonoBehaviour PathMap Map; private GameObject[,] ball; public List path; + private int rows; + [SerializeField] Vector3 startPoint; + [SerializeField] Vector3 endPoint; // Start is called before the first frame update void Start() { - ball = new GameObject[30, 30]; - Map = new PathMap(new Vector3(0, 0, 0), 30, 30, 30); - for (int r = 0; r < 30; r++) - { - for (int c = 0; c < 30; c++) - { + startPoint = new Vector3(0,0,0); + endPoint = new Vector3(100,0,100); + rows = 45; + Map = new PathMap(new Vector3(0, 0, 0), rows, rows, 100); + Debug.Log("yeet"); + Map.setupMapWithNextLayer(); + //Looking through the low res search + //path = Map.QueryNodes(startPoint,endPoint); + if (path != null) { + for (int i = 0; i < path.Count - 1; i++) { + int x = path[i].index.x; + int y = path[i].index.y; + Debug.Log(path[i].index); + Debug.Log(path[i].Position); GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Cube); - sphere.transform.position = Map.map[r, c].Position; - ball[r, c] = sphere; - - + sphere.transform.position = Map.map[x, y].Position; } } - path = Map.QueryNodes(new Vector3(0,0, 0), new Vector3(100, 0,100)); - //Debug.Log("yeet"); - Debug.Log(path.Count); - Debug.Log((int)path[0].index.x + " "+ (int)path[0].index.y); - for (int i = 0; i < path.Count - 1; i++) { - int x = path[i].index.x; - int y = path[i].index.y; - Destroy(ball[x, y]); - } } diff --git a/Assets/Scripts/NPC/PathMap.cs b/Assets/Scripts/NPC/PathMap.cs index 4d71fb8..c5b1980 100644 --- a/Assets/Scripts/NPC/PathMap.cs +++ b/Assets/Scripts/NPC/PathMap.cs @@ -8,35 +8,56 @@ public class PathMap private Vector3 position; public PathNode[,] map; + public PathMap parent; + public Vector2Int parentIndex; - private int rows = 30; - private int cols = 30; + private int rows = 100; + private int cols = 100; private float spacing = 1f; private float height = 0; public Terrain t; private float w, h; + private bool divided = false; - private PathNode[] uncheckedNodes; private List openList; private List closedList; - private List nextList; private List path; + + + + + public PathMap(Vector3 Position, int Rows, int Cols, float Width) + { + setup(Position, Rows, Cols, Width); + + + } + public void setupMap() + { + Generate(); + //Add the references to the neighbors of all nodes + AddAllNeighbors(); + + + } + private void setup(Vector3 Position, int Rows, int Cols, float Width) { position = Position; rows = Rows; cols = Cols; w = Width; + spacing = w / rows; //Array of all pathnodes in this chunk. map = new PathNode[rows, cols]; //only for debugging - + openList = new List(); @@ -45,43 +66,36 @@ public class PathMap //Path that will be returned at the end. path = new List(); + } + public void setupMapWithNextLayer() + { + Generate(); + AddAllNeighbors(); + //addNextLayer(); + + //Add the references to the neighbors of all nodes + + + } + public void Generate() + { //Add all nodes into the map. for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { - PathNode node = new PathNode(new Vector3(position.x + r * spacing, position.y + height, position.z + c * spacing)); + PathNode node = new PathNode(); + node.Position = new Vector3(this.position.x + r * spacing, this.position.y + height, this.position.z + c * spacing); node.index = new Vector2Int(r, c); + node.ConditionWeight = 1 + (float)Random.Range(1, 1000) / 1000; map[r, c] = node; } } - //Add the references to the neighbors of all nodes - AddAllNeighbors(); + } - private PathNode FindClosestNode(Vector3 pos) - { - if (pos.x > 0 && pos.x < rows * spacing && pos.z > 0 && pos.z < cols * spacing) - { - - return map[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]; - } - Vector2Int best = new Vector2Int(0,0); - - for (int r = 0; r < rows; r++) - { - for (int c = 0; c < cols; c++) - { - if (Vector3.Distance(map[best.x, best.y].Position, pos) > Vector3.Distance(map[r, c].Position, pos)) { - best.Set(r,c); - } - - } - } - return map[best.x,best.y]; - } public List QueryNodes(Vector3 Vstart, Vector3 Vend) { bool finished = false; @@ -89,8 +103,8 @@ public class PathMap PathNode start = FindClosestNode(Vstart); start.Gscore = 0; PathNode end = FindClosestNode(Vend); - - Debug.Log("Searching a path from " + start.index + " to " + end.index); + Debug.Log(start); + Debug.Log("Searching a path from " + start.Position + " to " + end.Position); openList.Add(start); PathNode current; @@ -100,7 +114,7 @@ public class PathMap d++; if (d > 1000) { - Debug.Log("Mist! Has not found a path"); + Debug.Log("Too many trys"); return null; } int winner = 0; @@ -119,9 +133,9 @@ public class PathMap { foreach (PathNode p in current.neigbors) { - if (!closedList.Contains(p)) + if (!closedList.Contains(p) && !p.Blocked) { - float tempG = current.Gscore + heuristic(p.Position, current.Position); + float tempG = current.Gscore + heuristic(p, current); bool newPath = false; if (openList.Contains(p)) { @@ -139,7 +153,7 @@ public class PathMap } if (newPath) { - p.Hscore = heuristic(p.Position, end.Position); + p.Hscore = heuristic(p, end); p.Fscore = p.Gscore + p.Hscore; p.Previous = current; } @@ -166,12 +180,292 @@ public class PathMap return null; } + public List LowerQueryNodes(Vector3 Vstart, Vector3 Vend, PathNode[] parentPath) + { + bool finished = false; - private float heuristic(Vector3 pos1, Vector3 pos2) + PathNode start = FindClosestNode(Vstart); + start.Gscore = 0; + PathNode end = FindClosestNode(Vend); + + Debug.Log("Searching a path from " + start.Position + " to " + end.Position + " but through the lower level."); + openList.Add(start); + + PathNode current; + int d = 0; + while (!finished) + { + d++; + if (d > 1000) + { + Debug.Log("Too many trys"); + return null; + } + int winner = 0; + for (int i = 0; i < openList.Count; i++) + { + + if (openList[i].Fscore < openList[winner].Fscore) winner = i; + } + current = openList[winner]; + openList.RemoveAt(winner); + closedList.Add(current); + + if (current != end) + { + foreach (PathNode p in current.neigbors) + { + if (!closedList.Contains(p) && !p.Blocked) + { + float tempG = current.Gscore + heuristic(p, current); + bool newPath = false; + if (openList.Contains(p)) + { + if (tempG < p.Gscore) + { + p.Gscore = tempG; + newPath = true; + } + } + else + { + p.Gscore = tempG; + newPath = true; + openList.Add(p); + } + if (newPath) + { + p.Hscore = heuristic(p, end); + p.Fscore = p.Gscore + p.Hscore; + p.Previous = current; + } + + } + + } + } + else + { + Debug.Log("Path Has Been Found"); + PathNode temp = end; + path.Add(temp); + while (temp.Previous != null) + { + path.Add(temp.Previous); + temp = temp.Previous; + } + path.Add(start); + finished = true; + return path; + } + } + return null; + } + + /* public void setupNeighborsLower() { + //schleife für alle parent Nodes + for (int i = 0; i < rows;i++) { + for (int j = 0; j < rows;j++) + { + for (int x = 0; x < rows; x++) + { + for (int y = 0; y < cols; y++) + { + //looking left and right and top and bottom + for (int x = -1; x <= 1; x++) + { + for (int y = -1; y <= 1; y++) + { + if (x != 0 && y != 0) + { + if (!indexOutOfBounds(index.x + x, index.y + y, rows, cols)) + { + map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + x, (int)index.y + y]); + } + else + { + //Rechts + if (x == 1 && y == 0) + { + + + } + //Links + if (x == -1 && y == 0) + { + + + } + //Oben + if (x == 0 && y == -1) + { + + + } + //Unten + if (x == 0 && y == 1) + { + + + } + + //Diagonal + + //Oben-Rechts + if (x == 1 && y == -1) + { + + + } + //Unten-Rechts + if (x == 1 && y == 1) + { + + + } + //Unten-Links + if (x == -1 && y == 1) + { + + + } + //Oben-Links + if (x == -1 && y == -1) + { + + + } + + + } + } + } + + } + + } + + } + } + + + + } + + + + } + + + }*/ + + + public void addNextLayer() + { + + + PathNode[,] full = new PathNode[rows * rows, cols * cols]; + for (int i = 0; i < rows * rows; i++) + { + for (int j = 0; j < cols * cols; j++) + { + + PathNode node = new PathNode(); + node.Position = new Vector3(this.position.x + i * spacing/rows,this.position.y,this.position.z + j * spacing/cols); + full[i, j] = node; + + } + + } + for (int i = 0; i < rows * rows; i++) + { + for (int j = 0; j < cols * cols; j++) + { + for (int x = -1; x <= 1; x++) + { + for (int y = -1; y <= 1; y++) + { + if (!(x == 0 && y == 0)) + { + if (!indexOutOfBounds(i + x, j + y, rows * rows, cols * cols)) + { + //try + //{ + full[i, j].neigbors.Add(full[i + x, j + y]); + //} + //catch + //{ + // Debug.Log("poggers"); + //} + } + } + } + } + + } + + } + + + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; c++) + { + PathNode node = map[r, c]; + Vector3 pos = node.Position; + node.lowerLevel = new PathMap(pos, rows, cols, spacing); + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + PathNode n = full[r*rows+i,c*cols + j]; + Debug.Log(n); + node.lowerLevel.map[i,j].index = new Vector2Int(i, j); + node.ConditionWeight = 1 + (float)Random.Range(1, 1000) / 1000; + node.lowerLevel.map[i, j] = node; + + } + } + node.lowerLevel.parent = this; + + node.HasLowerLevel = true; + + } + } + + divided = true; + } + private float heuristic(PathNode p1, PathNode p2) { //Calculates the HScore for a node. - return Vector3.Distance(pos1, pos2); + float dist = Vector3.Distance(p1.Position, p2.Position) / 2; + float dist1 = dist * p1.ConditionWeight; + float dist2 = dist * p2.ConditionWeight; + return dist1 + dist2; + + } +private PathNode FindClosestNode(Vector3 pos) + { + if (pos.x > 0 && pos.x < rows * spacing && pos.z > 0 && pos.z < cols * spacing && false) + { + + return map[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]; + } + Vector2Int best = new Vector2Int(0, 0); + + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; c++) + { + if (Vector3.Distance(map[best.x, best.y].Position, pos) > Vector3.Distance(map[r, c].Position, pos)) + { + best.Set(r, c); + } + + } + } + return map[best.x, best.y]; } private void AddAllNeighbors() { @@ -179,51 +473,48 @@ public class PathMap { for (int c = 0; c < cols; c++) { - AddNeighbors(new Vector2(r, c)); + AddNeighbors(new Vector2Int(r, c)); } } } - private void AddNeighbors(Vector2 index) + + private void lookThroughLowerLevel() { - //Adds references to all neigbors of a node. - if ((int)index.x - 1 >= 0 && (int)index.y - 1 >= 0) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y - 1]); - } - if ((int)index.y - 1 >= 0) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x, (int)index.y - 1]); - } - if ((int)index.x + 1 < rows && (int)index.y - 1 >= 0) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y - 1]); - } - - if ((int)index.x - 1 >= 0) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y]); - } - if ((int)index.x + 1 < rows && (int)index.y >= 0) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y]); - } - - - - if ((int)index.x - 1 >= 0 && (int)index.y + 1 < cols) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y + 1]); - } - if ((int)index.y + 1 < cols) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x, (int)index.y + 1]); - } - if ((int)index.x + 1 < rows && (int)index.y + 1 < cols) - { - map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y + 1]); - } } + private bool indexOutOfBounds(int x, int y, int rows, int cols) + { + if (x < 0 || x >= rows || y < 0 || y >= cols) + { + return true; + } + return false; + } + private void AddNeighbors(Vector2Int index) + { + for (int x = -1; x <= 1; x++) + { + for (int y = -1; y <= 1; y++) + { + if (!(x == 0 && y == 0)) + { + if (x == 0 || y == 0) + { + if (!indexOutOfBounds(index.x + x, index.y + y, rows, cols)) + { + map[index.x, index.y].neigbors.Add(map[index.x + x, index.y + y]); + + } + } + } + } + } + + + + + } + } \ No newline at end of file diff --git a/Assets/Scripts/NPC/PathGenerator.cs b/Assets/Scripts/NPC/PathNode.cs similarity index 64% rename from Assets/Scripts/NPC/PathGenerator.cs rename to Assets/Scripts/NPC/PathNode.cs index 10a97a8..c326a21 100644 --- a/Assets/Scripts/NPC/PathGenerator.cs +++ b/Assets/Scripts/NPC/PathNode.cs @@ -1,5 +1,3 @@ - - using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -9,18 +7,22 @@ public class PathNode private Vector3 position; public Vector2Int index; + public Vector2Int parentIndex; private float scoreF; private float scoreG; private float scoreH; public List neigbors; - private PathMap lowerLevel; - + public PathMap lowerLevel; + private bool hasLowerLevel = false; + private float conditionWeight = 1f; private PathNode previous; + private bool blocked; - public PathNode(Vector3 Pos) + + public PathNode() { neigbors = new List(); - position = Pos; + position = Vector3.zero; scoreG = Mathf.Infinity; scoreF = Mathf.Infinity; scoreH = Mathf.Infinity; @@ -28,7 +30,7 @@ public class PathNode public void activateNextLevel() { - //lowerLevel = new PathMap(30, 30, float width, float height); + } public Vector3 Position { get => position; set => position = value; } @@ -36,4 +38,7 @@ public class PathNode public float Gscore { get => scoreG; set => scoreG = value; } public float Fscore { get => scoreF; set => scoreF = value; } public PathNode Previous { get => previous; set => previous = value; } + public float ConditionWeight { get => conditionWeight; set => conditionWeight = value; } + public bool Blocked { get => blocked; set => blocked = value; } + public bool HasLowerLevel { get => hasLowerLevel; set => hasLowerLevel = value; } } diff --git a/Assets/Scripts/NPC/PathGenerator.cs.meta b/Assets/Scripts/NPC/PathNode.cs.meta similarity index 83% rename from Assets/Scripts/NPC/PathGenerator.cs.meta rename to Assets/Scripts/NPC/PathNode.cs.meta index 1d6a35e..76b0272 100644 --- a/Assets/Scripts/NPC/PathGenerator.cs.meta +++ b/Assets/Scripts/NPC/PathNode.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 0f50f0a081672ea4a9bc9f13de003f0a +guid: 8c0191b07f1e4654d9dd893623534cb6 MonoImporter: externalObjects: {} serializedVersion: 2