Skip to content

Commit b79f70d

Browse files
committed
add GameStateEnum::Starting; separate events in c#
1 parent 34a7802 commit b79f70d

36 files changed

+366
-336
lines changed

c/uwapi/uwapi/modules/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern "C"
3434
typedef uint64_t uint64;
3535
typedef int64_t sint64;
3636

37-
static const uint32 UW_VERSION = 37;
37+
static const uint32 UW_VERSION = 38;
3838
static const uint32 UW_GameTicksPerSecond = 20;
3939

4040
typedef struct UwIds

c/uwapi/uwapi/modules/game.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ extern "C"
1919
UwGameStateEnum_None = 0,
2020
UwGameStateEnum_Session = 1,
2121
UwGameStateEnum_Preparation = 2,
22-
UwGameStateEnum_Game = 3,
23-
UwGameStateEnum_Finish = 4,
24-
UwGameStateEnum_Paused = 5,
25-
UwGameStateEnum_CutscenePaused = 6,
26-
UwGameStateEnum_CutsceneRunning = 7,
22+
UwGameStateEnum_Starting = 3,
23+
UwGameStateEnum_Game = 4,
24+
UwGameStateEnum_Finish = 5,
25+
UwGameStateEnum_Pause = 6,
26+
UwGameStateEnum_CutscenePaused = 7,
27+
UwGameStateEnum_CutsceneRunning = 8,
2728
} UwGameStateEnum;
2829
#ifdef UNNATURAL_BOTS
2930
typedef void (*UwGameStateCallbackType)(UwGameStateEnum state);
@@ -34,14 +35,16 @@ extern "C"
3435
#endif
3536
UNNATURAL_API UwGameStateEnum uwGameState(void);
3637

38+
UNNATURAL_API uint32 uwGameTick(void);
39+
3740
// update callback
3841

3942
#ifdef UNNATURAL_BOTS
40-
typedef void (*UwUpdateCallbackType)(uint32 tick, bool stepping);
43+
typedef void (*UwUpdateCallbackType)(bool stepping);
4144
UNNATURAL_API void uwSetUpdateCallback(UwUpdateCallbackType callback);
4245
#endif
4346
#ifdef UNNATURAL_SCRIPTS
44-
UNNATURAL_ENTRY void uwUpdateCallback(uint32 tick, bool stepping);
47+
UNNATURAL_ENTRY void uwUpdateCallback(bool stepping);
4548
#endif
4649

4750
// shooting callback

csharp/bot/main.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void AttackNearestEnemies()
2626
{
2727
if (Commands.Orders(own.Id).Length == 0)
2828
{
29-
Entity enemy = enemyUnits.OrderByDescending(x => Map.DistanceEstimate(own.Pos, x.Pos)).First();
29+
Entity enemy = enemyUnits.OrderBy(x => Map.DistanceEstimate(own.Pos, x.Pos)).First();
3030
Commands.Order(own.Id, Commands.FightToEntity(enemy.Id));
3131
}
3232
}
@@ -81,7 +81,7 @@ void Updating(object sender, bool stepping)
8181
}
8282
}
8383

84-
void Start()
84+
void Run()
8585
{
8686
Game.LogInfo("bot-cs start");
8787
if (!Game.TryReconnect())
@@ -95,7 +95,7 @@ void Start()
9595

9696
Bot()
9797
{
98-
Game.Updating += Updating;
98+
Events.Updating += Updating;
9999
}
100100

101101
static int Main(string[] args)
@@ -120,7 +120,7 @@ static int Main(string[] args)
120120
Directory.SetCurrentDirectory(root);
121121

122122
Bot bot = new Bot();
123-
bot.Start();
123+
bot.Run();
124124
return 0;
125125
}
126126
}

csharp/uwapi/events.cs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Unnatural
5+
{
6+
using ConnectionStateEnum = Interop.UwConnectionStateEnum;
7+
using GameStateEnum = Interop.UwGameStateEnum;
8+
using MapStateEnum = Interop.UwMapStateEnum;
9+
using ShootingData = Interop.UwShootingData;
10+
using ShootingArray = Interop.UwShootingArray;
11+
using ExplosionData = Interop.UwExplosionData;
12+
using ExplosionsArray = Interop.UwExplosionsArray;
13+
using ChatTargetFLags = Interop.UwChatTargetFlags;
14+
15+
public class ChatMessage
16+
{
17+
public string Message;
18+
public uint Sender;
19+
public ChatTargetFLags Flags;
20+
}
21+
22+
public static class Events
23+
{
24+
25+
public static event EventHandler<ConnectionStateEnum> ConnectionStateChanged;
26+
public static event EventHandler<GameStateEnum> GameStateChanged;
27+
public static event EventHandler<MapStateEnum> MapStateChanged;
28+
public static event EventHandler<bool> Updating;
29+
public static event EventHandler<ShootingData[]> Shooting;
30+
public static event EventHandler<ExplosionData[]> Explosions;
31+
public static event EventHandler<uint> ForceEliminated;
32+
public static event EventHandler<ChatMessage> ChatReceived;
33+
34+
static readonly Interop.UwExceptionCallbackType ExceptionDelegate = new Interop.UwExceptionCallbackType(ExceptionCallback);
35+
static readonly Interop.UwConnectionStateCallbackType ConnectionStateDelegate = new Interop.UwConnectionStateCallbackType(ConnectionStateCallback);
36+
static readonly Interop.UwGameStateCallbackType GameStateDelegate = new Interop.UwGameStateCallbackType(GameStateCallback);
37+
static readonly Interop.UwMapStateCallbackType MapStateDelegate = new Interop.UwMapStateCallbackType(MapStateCallback);
38+
static readonly Interop.UwUpdateCallbackType UpdateDelegate = new Interop.UwUpdateCallbackType(UpdateCallback);
39+
static readonly Interop.UwShootingCallbackType ShootingDelegate = new Interop.UwShootingCallbackType(ShootingCallback);
40+
static readonly Interop.UwExplosionsCallbackType ExplosionsDelegate = new Interop.UwExplosionsCallbackType(ExplosionsCallback);
41+
static readonly Interop.UwForceEliminatedCallbackType ForceEliminatedDelegate = new Interop.UwForceEliminatedCallbackType(ForceEliminatedCallback);
42+
static readonly Interop.UwChatCallbackType ChatDelegate = new Interop.UwChatCallbackType(ChatCallback);
43+
44+
static void ExceptionCallback([MarshalAs(UnmanagedType.LPStr)] string message)
45+
{
46+
Console.WriteLine("exception: " + message);
47+
if (System.Diagnostics.Debugger.IsAttached)
48+
System.Diagnostics.Debugger.Break();
49+
}
50+
51+
static void ConnectionStateCallback(ConnectionStateEnum state)
52+
{
53+
if (ConnectionStateChanged != null)
54+
ConnectionStateChanged(null, state);
55+
}
56+
57+
static void GameStateCallback(GameStateEnum state)
58+
{
59+
if (GameStateChanged != null)
60+
GameStateChanged(null, state);
61+
}
62+
63+
static void MapStateCallback(MapStateEnum state)
64+
{
65+
if (MapStateChanged != null)
66+
MapStateChanged(null, state);
67+
}
68+
69+
static void UpdateCallback(bool stepping)
70+
{
71+
if (Updating != null)
72+
Updating(null, stepping);
73+
}
74+
75+
static void ShootingCallback(ref ShootingArray data)
76+
{
77+
if (Shooting == null)
78+
return;
79+
ShootingData[] arr = new ShootingData[data.count];
80+
int size = Marshal.SizeOf(typeof(ShootingData));
81+
for (int i = 0; i < data.count; i++)
82+
{
83+
IntPtr currentPtr = IntPtr.Add(data.data, i * size);
84+
arr[i] = Marshal.PtrToStructure<ShootingData>(currentPtr);
85+
}
86+
Shooting(null, arr);
87+
}
88+
89+
static void ExplosionsCallback(ref ExplosionsArray data)
90+
{
91+
if (Explosions == null)
92+
return;
93+
ExplosionData[] arr = new ExplosionData[data.count];
94+
int size = Marshal.SizeOf(typeof(ExplosionData));
95+
for (int i = 0; i < data.count; i++)
96+
{
97+
IntPtr currentPtr = IntPtr.Add(data.data, i * size);
98+
arr[i] = Marshal.PtrToStructure<ExplosionData>(currentPtr);
99+
}
100+
Explosions(null, arr);
101+
}
102+
103+
static void ForceEliminatedCallback(uint force)
104+
{
105+
if (ForceEliminated == null)
106+
return;
107+
ForceEliminated(null, force);
108+
}
109+
110+
static void ChatCallback(string msg, uint sender, ChatTargetFLags flags)
111+
{
112+
if (ChatReceived == null)
113+
return;
114+
ChatMessage c = new ChatMessage();
115+
c.Message = msg;
116+
c.Sender = sender;
117+
c.Flags = flags;
118+
ChatReceived(null, c);
119+
}
120+
121+
122+
static Events()
123+
{
124+
AppDomain.CurrentDomain.ProcessExit += Destructor;
125+
Interop.uwInitialize(Interop.UW_VERSION);
126+
Interop.uwInitializeConsoleLogger();
127+
128+
Interop.uwSetExceptionCallback(ExceptionDelegate);
129+
Interop.uwSetConnectionStateCallback(ConnectionStateDelegate);
130+
Interop.uwSetGameStateCallback(GameStateDelegate);
131+
Interop.uwSetMapStateCallback(MapStateDelegate);
132+
Interop.uwSetUpdateCallback(UpdateDelegate);
133+
Interop.uwSetShootingCallback(ShootingDelegate);
134+
Interop.uwSetExplosionsCallback(ExplosionsDelegate);
135+
Interop.uwSetForceEliminatedCallback(ForceEliminatedDelegate);
136+
Interop.uwSetChatCallback(ChatDelegate);
137+
138+
// make sure that others register their callbacks too (call any method)
139+
Prototypes.All();
140+
Map.Positions();
141+
World.Entities();
142+
UwapiTasks.Init();
143+
}
144+
145+
static void Destructor(object sender, EventArgs e)
146+
{
147+
Interop.uwDeinitialize();
148+
}
149+
}
150+
151+
public static class InteropHelpers
152+
{
153+
public static uint[] Ids(Interop.UwIds ids)
154+
{
155+
uint[] tmp = new uint[ids.count];
156+
if (ids.count > 0)
157+
Marshal.Copy(ids.ids, (int[])(object)tmp, 0, (int)ids.count);
158+
return tmp;
159+
}
160+
}
161+
162+
public static class UwapiTasks
163+
{
164+
public static ulong InsertTask(Action a)
165+
{
166+
ulong i = index++;
167+
actions.Add(i, a);
168+
return i;
169+
}
170+
171+
static ulong index = 1;
172+
static System.Collections.Generic.Dictionary<ulong, Action> actions = new System.Collections.Generic.Dictionary<ulong, Action>();
173+
static readonly Interop.UwTaskCompletedCallbackType TaskCompletedDelegate = new Interop.UwTaskCompletedCallbackType(TaskCompleted);
174+
175+
static void TaskCompleted(ulong taskUserData, Interop.UwTaskTypeEnum type)
176+
{
177+
if (type != Interop.UwTaskTypeEnum.None)
178+
{
179+
Action a;
180+
if (actions.TryGetValue(taskUserData, out a))
181+
a();
182+
}
183+
actions.Remove(taskUserData);
184+
}
185+
186+
static UwapiTasks()
187+
{
188+
Interop.uwSetTaskCompletedCallback(TaskCompletedDelegate);
189+
}
190+
191+
public static void Init()
192+
{ }
193+
}
194+
}

0 commit comments

Comments
 (0)