Skip to content

Commit 83480d7

Browse files
committed
pathfinding in c#
1 parent a2f5bf3 commit 83480d7

File tree

11 files changed

+225
-96
lines changed

11 files changed

+225
-96
lines changed

c/uwapi/uwapi/modules/game.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ extern "C"
116116
{
117117
UwTaskTypeEnum_None = 0,
118118
UwTaskTypeEnum_UnitPathfinding = 1,
119-
UwTaskTypeEnum_TilesPathfinding = 2,
120-
UwTaskTypeEnum_ClustersPathfinding = 3,
119+
UwTaskTypeEnum_ClustersDistances = 2,
121120
} UwTaskTypeEnum;
122121

123122
#ifdef UNNATURAL_BOTS

c/uwapi/uwapi/modules/map.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,24 @@ extern "C"
8585
UNNATURAL_API bool uwTestVisible(float x1, float y1, float z1, float x2, float y2, float z2);
8686
UNNATURAL_API bool uwTestShooting(uint32 shooterPosition, uint32 shooterProto, float shootingRangeUpgrade, uint32 targetPosition, uint32 targetProto);
8787
UNNATURAL_API float uwDistanceLine(float x1, float y1, float z1, float x2, float y2, float z2);
88-
UNNATURAL_API float uwDistanceEstimate(uint32 a, uint32 b);
89-
UNNATURAL_API float uwYaw(uint32 position, uint32 towards);
88+
UNNATURAL_API float uwDistanceEstimate(uint32 positionA, uint32 positionB);
89+
UNNATURAL_API float uwYaw(uint32 startPosition, uint32 goalPosition);
90+
91+
// clusters distances
92+
93+
typedef struct UwClustersDistancesQuery
94+
{
95+
uint64 taskUserData;
96+
uint32 startingCluster;
97+
uint32 unitPrototype;
98+
bool allowImpassableTerrain;
99+
} UwClustersDistancesQuery;
100+
typedef struct UwClustersDistancesResult
101+
{
102+
UwIds distances;
103+
} UwClustersDistancesResult;
104+
UNNATURAL_API void uwStartClustersDistances(const UwClustersDistancesQuery *query);
105+
UNNATURAL_API void uwRetrieveClustersDistances(UwClustersDistancesResult *data);
90106

91107
#ifdef __cplusplus
92108
} // extern C

c/uwapi/uwapi/modules/world.h

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,24 @@ extern "C"
6767
} UwOverviewExtract;
6868
UNNATURAL_API void uwOverviewExtract(UwOverviewExtract *data);
6969

70-
// pathfinding
70+
// unit pathfinding
7171

7272
typedef struct UwUnitPathfindingQuery
7373
{
7474
uint64 taskUserData;
7575
uint32 startingPosition;
7676
uint32 goalPosition;
7777
uint32 unitPrototype;
78+
uint32 maxIterations;
79+
bool allowNearbyPosition;
7880
} UwUnitPathfindingQuery;
79-
8081
typedef struct UwUnitPathfindingResult
8182
{
82-
UNNATURAL_POINTER(const uint32 *) tilesData;
83-
uint32 tilesCount;
83+
UwIds path;
8484
UwPathStateEnum state;
8585
} UwUnitPathfindingResult;
86-
8786
UNNATURAL_API void uwStartUnitPathfinding(const UwUnitPathfindingQuery *query);
88-
UNNATURAL_API void uwRetrieveUnitPathfinding(UwUnitPathfindingResult *query);
89-
90-
typedef struct UwMapPathfindingQuery
91-
{
92-
uint64 taskUserData;
93-
uint32 start; // tile or cluster
94-
uint32 goal; // tile or cluster
95-
} UwMapPathfindingQuery;
96-
97-
typedef struct UwMapPathfindingResult
98-
{
99-
UNNATURAL_POINTER(const uint32 *) data; // tiles or clusters
100-
uint32 count;
101-
} UwMapPathfindingResult;
102-
103-
UNNATURAL_API void uwStartTilesPathfinding(const UwMapPathfindingQuery *query);
104-
UNNATURAL_API void uwStartClustersPathfinding(const UwMapPathfindingQuery *query);
105-
UNNATURAL_API void uwRetrieveTilesPathfinding(UwMapPathfindingResult *query);
106-
UNNATURAL_API void uwRetrieveClustersPathfinding(UwMapPathfindingResult *query);
87+
UNNATURAL_API void uwRetrieveUnitPathfinding(UwUnitPathfindingResult *data);
10788

10889
#ifdef __cplusplus
10990
} // extern C

csharp/uwapi/game.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ static void ChatCallback(string msg, uint sender, ChatTargetFLags flags)
223223
ChatReceived(null, c);
224224
}
225225

226-
// todo task completed callback
227-
228226
static Game()
229227
{
230228
AppDomain.CurrentDomain.ProcessExit += Destructor;
@@ -245,6 +243,7 @@ static Game()
245243
Prototypes.All();
246244
Map.Positions();
247245
World.Entities();
246+
UwapiTasks.Init();
248247
}
249248

250249
static void Destructor(object sender, EventArgs e)

csharp/uwapi/interop.cs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,7 @@ public enum UwTaskTypeEnum
708708
{
709709
None = 0,
710710
UnitPathfinding = 1,
711-
TilesPathfinding = 2,
712-
ClustersPathfinding = 3,
711+
ClustersDistances = 2,
713712
}
714713

715714
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -828,10 +827,32 @@ public static extern bool uwTestShooting(uint shooterPosition, uint shooterProto
828827
public static extern float uwDistanceLine(float x1, float y1, float z1, float x2, float y2, float z2);
829828

830829
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
831-
public static extern float uwDistanceEstimate(uint a, uint b);
830+
public static extern float uwDistanceEstimate(uint positionA, uint positionB);
832831

833832
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
834-
public static extern float uwYaw(uint position, uint towards);
833+
public static extern float uwYaw(uint startPosition, uint goalPosition);
834+
835+
[StructLayout(LayoutKind.Sequential)]
836+
public struct UwClustersDistancesQuery
837+
{
838+
public ulong taskUserData;
839+
public uint startingCluster;
840+
public uint unitPrototype;
841+
[MarshalAs(UnmanagedType.I1)]
842+
public bool allowImpassableTerrain;
843+
}
844+
845+
[StructLayout(LayoutKind.Sequential)]
846+
public struct UwClustersDistancesResult
847+
{
848+
public UwIds distances;
849+
}
850+
851+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
852+
public static extern void uwStartClustersDistances(ref UwClustersDistancesQuery query);
853+
854+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
855+
public static extern void uwRetrieveClustersDistances(ref UwClustersDistancesResult data);
835856

836857
public enum UwPrototypeTypeEnum
837858
{
@@ -934,48 +955,23 @@ public struct UwUnitPathfindingQuery
934955
public uint startingPosition;
935956
public uint goalPosition;
936957
public uint unitPrototype;
958+
public uint maxIterations;
959+
[MarshalAs(UnmanagedType.I1)]
960+
public bool allowNearbyPosition;
937961
}
938962

939963
[StructLayout(LayoutKind.Sequential)]
940964
public struct UwUnitPathfindingResult
941965
{
942-
public IntPtr tilesData;
943-
public uint tilesCount;
966+
public UwIds path;
944967
public UwPathStateEnum state;
945968
}
946969

947970
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
948971
public static extern void uwStartUnitPathfinding(ref UwUnitPathfindingQuery query);
949972

950973
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
951-
public static extern void uwRetrieveUnitPathfinding(ref UwUnitPathfindingResult query);
952-
953-
[StructLayout(LayoutKind.Sequential)]
954-
public struct UwMapPathfindingQuery
955-
{
956-
public ulong taskUserData;
957-
public uint start;
958-
public uint goal;
959-
}
960-
961-
[StructLayout(LayoutKind.Sequential)]
962-
public struct UwMapPathfindingResult
963-
{
964-
public IntPtr data;
965-
public uint count;
966-
}
967-
968-
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
969-
public static extern void uwStartTilesPathfinding(ref UwMapPathfindingQuery query);
970-
971-
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
972-
public static extern void uwStartClustersPathfinding(ref UwMapPathfindingQuery query);
973-
974-
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
975-
public static extern void uwRetrieveTilesPathfinding(ref UwMapPathfindingResult query);
976-
977-
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
978-
public static extern void uwRetrieveClustersPathfinding(ref UwMapPathfindingResult query);
974+
public static extern void uwRetrieveUnitPathfinding(ref UwUnitPathfindingResult data);
979975
}
980976

981977
public partial class Entity

csharp/uwapi/map.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ public static float DistanceLine(uint a, uint b)
130130
return (float)Math.Sqrt(x + y + z);
131131
}
132132

133-
public static float DistanceEstimate(uint a, uint b)
133+
public static float DistanceEstimate(uint positionA, uint positionB)
134134
{
135-
return Interop.uwDistanceEstimate(a, b);
135+
return Interop.uwDistanceEstimate(positionA, positionB);
136136
}
137137

138-
public static float Yaw(uint a, uint b)
138+
public static float Yaw(uint startPosition, uint goalPosition)
139139
{
140-
return Interop.uwYaw(a, b);
140+
return Interop.uwYaw(startPosition, goalPosition);
141141
}
142142

143143
public static IReadOnlyList<uint> TileToCluster()
@@ -170,6 +170,24 @@ public static IReadOnlyList<uint> ClustersNeighbors(uint cluster)
170170
return clustersNeighbors[(int)cluster];
171171
}
172172

173+
public static void ClustersDistances(Action<ClustersDistancesResult> callback, uint startingCluster, uint unitPrototype, bool allowImpassableTerrain = false)
174+
{
175+
Action fin = () =>
176+
{
177+
Interop.UwClustersDistancesResult tmp = new Interop.UwClustersDistancesResult();
178+
Interop.uwRetrieveClustersDistances(ref tmp);
179+
ClustersDistancesResult res = new ClustersDistancesResult();
180+
res.distances = InteropHelpers.Ids(tmp.distances);
181+
callback(res);
182+
};
183+
Interop.UwClustersDistancesQuery q = new Interop.UwClustersDistancesQuery();
184+
q.startingCluster = startingCluster;
185+
q.unitPrototype = unitPrototype;
186+
q.allowImpassableTerrain = allowImpassableTerrain;
187+
q.taskUserData = UwapiTasks.InsertTask(fin);
188+
Interop.uwStartClustersDistances(ref q);
189+
}
190+
173191
static string name;
174192
static string guid;
175193
static string path;
@@ -278,4 +296,9 @@ static Map()
278296
Game.MapStateChanged += MapStateChanged;
279297
}
280298
}
299+
300+
public struct ClustersDistancesResult
301+
{
302+
public uint[] distances;
303+
}
281304
}

csharp/uwapi/utils.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Runtime.InteropServices;
23

34
namespace Unnatural
@@ -12,4 +13,37 @@ public static uint[] Ids(Interop.UwIds ids)
1213
return tmp;
1314
}
1415
}
16+
17+
public static class UwapiTasks
18+
{
19+
public static ulong InsertTask(Action a)
20+
{
21+
ulong i = index++;
22+
actions.Add(i, a);
23+
return i;
24+
}
25+
26+
static ulong index = 1;
27+
static System.Collections.Generic.Dictionary<ulong, Action> actions = new System.Collections.Generic.Dictionary<ulong, Action>();
28+
static readonly Interop.UwTaskCompletedCallbackType TaskCompletedDelegate = new Interop.UwTaskCompletedCallbackType(TaskCompleted);
29+
30+
static void TaskCompleted(ulong taskUserData, Interop.UwTaskTypeEnum type)
31+
{
32+
if (type != Interop.UwTaskTypeEnum.None)
33+
{
34+
Action a;
35+
if (actions.TryGetValue(taskUserData, out a))
36+
a();
37+
}
38+
actions.Remove(taskUserData);
39+
}
40+
41+
static UwapiTasks()
42+
{
43+
Interop.uwSetTaskCompletedCallback(TaskCompletedDelegate);
44+
}
45+
46+
public static void Init()
47+
{ }
48+
}
1549
}

csharp/uwapi/world.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Runtime.InteropServices;
34

@@ -66,7 +67,26 @@ public static uint[] OverviewEntities(uint position)
6667
return InteropHelpers.Ids(ns);
6768
}
6869

69-
// todo pathfinding
70+
public static void UnitPathfinding(Action<UnitPathfindingResult> callback, uint startingPosition, uint goalPosition, uint unitPrototype, bool allowNearbyPosition = false, uint maxIterations = 0)
71+
{
72+
Action fin = () =>
73+
{
74+
Interop.UwUnitPathfindingResult tmp = new Interop.UwUnitPathfindingResult();
75+
Interop.uwRetrieveUnitPathfinding(ref tmp);
76+
UnitPathfindingResult res = new UnitPathfindingResult();
77+
res.path = InteropHelpers.Ids(tmp.path);
78+
res.state = tmp.state;
79+
callback(res);
80+
};
81+
Interop.UwUnitPathfindingQuery q = new Interop.UwUnitPathfindingQuery();
82+
q.startingPosition = startingPosition;
83+
q.goalPosition = goalPosition;
84+
q.unitPrototype = unitPrototype;
85+
q.allowNearbyPosition = allowNearbyPosition;
86+
q.maxIterations = maxIterations;
87+
q.taskUserData = UwapiTasks.InsertTask(fin);
88+
Interop.uwStartUnitPathfinding(ref q);
89+
}
7090

7191
public static IReadOnlyDictionary<uint, Entity> Entities()
7292
{
@@ -191,4 +211,10 @@ static World()
191211
Game.Updating += Updating;
192212
}
193213
}
214+
215+
public struct UnitPathfindingResult
216+
{
217+
public uint[] path;
218+
public PathStateEnum state;
219+
}
194220
}

0 commit comments

Comments
 (0)