-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAltInteractions.cs
147 lines (131 loc) · 6.09 KB
/
AltInteractions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Linq;
using System.Numerics;
using AltV.Net.Elements.Entities;
using System.Runtime.InteropServices;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace AltV.Net.Interactions
{
[Obsolete("Got replaced with InteractionsService", false)]
public static class AltInteractions
{
private const string DllName = "csharp-module";
private const CallingConvention NativeCallingConvention = CallingConvention.Cdecl;
[DllImport(DllName, CallingConvention = NativeCallingConvention)]
private static extern unsafe void WorldObject_GetPositionCoords(void* player, float* positionX, float* positionY,
float* positionZ, int* dimension);
private static Grid _spatialPartition;
private readonly struct InteractionEvent
{
public readonly byte EventType;
public readonly object Data;
public InteractionEvent(byte eventType, object data)
{
this.EventType = eventType;
this.Data = data;
}
}
private static readonly Channel<InteractionEvent> InteractionChannel =
Channel.CreateUnbounded<InteractionEvent>(new UnboundedChannelOptions {SingleReader = true});
public static void Init(int maxX = 50_000, int maxY = 50_000, int areaSize = 100, int xOffset = 10_000, int yOffset = 10_000)
{
Init(new Grid(maxX, maxY, areaSize, xOffset, yOffset));
}
public static void Init(Grid spatialPartition)
{
_spatialPartition = spatialPartition;
Task.Run(async () =>
{
while (await InteractionChannel.Reader.WaitToReadAsync())
{
while (InteractionChannel.Reader.TryRead(out var interactionEvent))
{
switch (interactionEvent.EventType)
{
case 0:
_spatialPartition.Add((IInteraction) interactionEvent.Data);
break;
case 1:
_spatialPartition.Remove((IInteraction) interactionEvent.Data);
break;
case 2:
{
var (player, argument) = ((IPlayer, object)) interactionEvent.Data;
float x;
float y;
float z;
int currDimension;
lock (player)
{
if (player.Exists)
{
unsafe
{
WorldObject_GetPositionCoords(player.WorldObjectNativePointer.ToPointer(), &x, &y, &z,
&currDimension);
}
}
else
{
break;
}
}
var interactionPosition = new Vector3(x, y, z);
var foundInteractions = _spatialPartition.Find(interactionPosition, currDimension);
if (foundInteractions != null)
{
foreach (var interaction in foundInteractions)
{
try
{
if (interaction.OnInteraction(player, interactionPosition, currDimension, argument))
{
break;
}
}
catch (Exception exception)
{
Console.WriteLine("interaction " + interaction + " threw a exception.");
Console.WriteLine(exception);
}
}
}
break;
}
case 3:
{
var (interactionPosition, currDimension, callback) =
((Vector3, int, TaskCompletionSource<IInteraction[]>)) interactionEvent.Data;
callback.SetResult(_spatialPartition.Find(interactionPosition, currDimension).ToArray());
break;
}
}
}
}
});
}
public static void AddInteraction(IInteraction interaction)
{
InteractionChannel.Writer.TryWrite(new InteractionEvent(0, interaction));
}
public static void RemoveInteraction(IInteraction interaction)
{
InteractionChannel.Writer.TryWrite(new InteractionEvent(1, interaction));
}
public static void TriggerInteractions(IPlayer player)
{
InteractionChannel.Writer.TryWrite(new InteractionEvent(2, player));
}
public static Task<IInteraction[]> FindInteractions(Vector3 position, int dimension)
{
var callback = new TaskCompletionSource<IInteraction[]>(TaskCreationOptions.RunContinuationsAsynchronously);
InteractionChannel.Writer.TryWrite(new InteractionEvent(3, (position, dimension, callback)));
return callback.Task;
}
public static void Dispose()
{
InteractionChannel.Writer.Complete();
}
}
}