Skip to content

Commit 3a67cdd

Browse files
committed
performance
1 parent a43f691 commit 3a67cdd

File tree

7 files changed

+109
-17
lines changed

7 files changed

+109
-17
lines changed

c/uwapi/uwapi/modules/botsConnection.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ extern "C"
6767
// my player
6868

6969
UNNATURAL_API void uwSetPlayerName(const char *name);
70-
UNNATURAL_API void uwSetPlayerColor(float r, float g, float b); // [0 .. 1]
71-
UNNATURAL_API void uwSetPlayerRace(uint32 raceProto);
70+
UNNATURAL_API void uwPlayerJoinForce(uint32 force);
71+
UNNATURAL_API void uwSetForceColor(float r, float g, float b); // [0 .. 1]
72+
UNNATURAL_API void uwSetForceRace(uint32 raceProto);
73+
UNNATURAL_API void uwForceJoinTeam(uint32 team);
7274

7375
typedef struct UwMyPlayer
7476
{
@@ -99,6 +101,10 @@ extern "C"
99101
} UwPerformanceStatistics;
100102
UNNATURAL_API void uwPerformanceStatistics(UwPerformanceStatistics *data);
101103

104+
UNNATURAL_API void uwPerformanceProfiling(bool enable);
105+
UNNATURAL_API uint64 uwProfilingEventBegin(void);
106+
UNNATURAL_API void uwProfilingEventEnd(const char *name, uint64 eventStartTime);
107+
102108
#endif
103109

104110
#ifdef __cplusplus

csharp/bot/main.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace Unnatural
99
internal class Bot
1010
{
1111
readonly Random random = new Random();
12-
uint step = 0; // save some cpu cycles by splitting work over multiple steps
12+
bool isConfigured = false;
13+
uint workStep = 0; // save some cpu cycles by splitting work over multiple steps
1314

1415
void AttackNearestEnemies()
1516
{
@@ -35,6 +36,8 @@ void AssignRandomRecipes()
3536
{
3637
foreach (Entity own in World.Entities().Values.Where(x => x.Own && x.Unit.HasValue))
3738
{
39+
if (own.Recipe.HasValue)
40+
continue;
3841
List<uint> recipes = own.ProtoUnit.recipes;
3942
if (recipes?.Count > 0)
4043
{
@@ -46,12 +49,14 @@ void AssignRandomRecipes()
4649

4750
void Configure()
4851
{
52+
if (isConfigured)
53+
return;
54+
isConfigured = true;
55+
4956
Game.SetPlayerName("bot-cs");
50-
Game.SetPlayerColor(1f, 0f, 0f);
51-
if (Game.MapState() == Interop.UwMapStateEnum.Loaded)
52-
{
53-
// todo choose race
54-
}
57+
Game.PlayerJoinForce(0); // create new force
58+
Game.SetForceColor(1f, 0f, 0f);
59+
// todo choose race
5560
}
5661

5762
void Updating(object sender, bool stepping)
@@ -61,9 +66,11 @@ void Updating(object sender, bool stepping)
6166
Configure();
6267
return;
6368
}
69+
6470
if (!stepping)
6571
return;
66-
switch (step++ % 10) // save some cpu cycles by splitting work over multiple steps
72+
73+
switch (workStep++ % 10) // save some cpu cycles by splitting work over multiple steps
6774
{
6875
case 1:
6976
AttackNearestEnemies();

csharp/uwapi/game.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@ public static void SetPlayerName(string name)
5050
Interop.uwSetPlayerName(name);
5151
}
5252

53-
public static void SetPlayerColor(float r, float g, float b) // [0 .. 1]
53+
public static void PlayerJoinForce(uint forceId)
5454
{
55-
Interop.uwSetPlayerColor(r, g, b);
55+
Interop.uwPlayerJoinForce(forceId);
5656
}
5757

58-
public static void SetPlayerRace(uint raceProto)
58+
public static void SetForceColor(float r, float g, float b) // [0 .. 1]
5959
{
60-
Interop.uwSetPlayerRace(raceProto);
60+
Interop.uwSetForceColor(r, g, b);
61+
}
62+
63+
public static void SetForceRace(uint raceProto)
64+
{
65+
Interop.uwSetForceRace(raceProto);
66+
}
67+
68+
public static void ForceJoinTeam(uint team)
69+
{
70+
Interop.uwForceJoinTeam(team);
6171
}
6272

6373
public static void SetConnectStartGui(bool startGui, string extraParams = "--observer 1")
@@ -129,6 +139,21 @@ public static Interop.UwPerformanceStatistics PerformanceStatistics()
129139
return data;
130140
}
131141

142+
public static void PerformanceProfiling(bool enable)
143+
{
144+
Interop.uwPerformanceProfiling(enable);
145+
}
146+
147+
public static ulong ProfilingEventBegin()
148+
{
149+
return Interop.uwProfilingEventBegin();
150+
}
151+
152+
public static void ProfilingEventEnd(string name, ulong eventStartTime)
153+
{
154+
Interop.uwProfilingEventEnd(name, eventStartTime);
155+
}
156+
132157
static readonly Interop.UwExceptionCallbackType ExceptionDelegate = new Interop.UwExceptionCallbackType(ExceptionCallback);
133158
static readonly Interop.UwConnectionStateCallbackType ConnectionStateDelegate = new Interop.UwConnectionStateCallbackType(ConnectionStateCallback);
134159
static readonly Interop.UwGameStateCallbackType GameStateDelegate = new Interop.UwGameStateCallbackType(GameStateCallback);

csharp/uwapi/interop.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ public static extern void uwConnectNewServer(uint visibility, [MarshalAs(Unmanag
170170
public static extern void uwSetPlayerName([MarshalAs(UnmanagedType.LPStr)] string name);
171171

172172
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
173-
public static extern void uwSetPlayerColor(float r, float g, float b);
173+
public static extern void uwPlayerJoinForce(uint force);
174174

175175
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
176-
public static extern void uwSetPlayerRace(uint raceProto);
176+
public static extern void uwSetForceColor(float r, float g, float b);
177+
178+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
179+
public static extern void uwSetForceRace(uint raceProto);
180+
181+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
182+
public static extern void uwForceJoinTeam(uint team);
177183

178184
[StructLayout(LayoutKind.Sequential)]
179185
public struct UwMyPlayer
@@ -217,6 +223,16 @@ public struct UwPerformanceStatistics
217223
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
218224
public static extern void uwPerformanceStatistics(ref UwPerformanceStatistics data);
219225

226+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
227+
public static extern void uwPerformanceProfiling([MarshalAs(UnmanagedType.I1)] bool enable);
228+
229+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
230+
public static extern ulong uwProfilingEventBegin();
231+
232+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
233+
public static extern void uwProfilingEventEnd([MarshalAs(UnmanagedType.LPStr)] string name,
234+
ulong eventStartTime);
235+
220236
public enum UwOrderTypeEnum
221237
{
222238
None = 0,

python/uwapi/uw/bots.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ bool uwTryReconnect(void);
138138
void uwDisconnect(void);
139139

140140
void uwSetPlayerName(const char *name);
141-
void uwSetPlayerColor(float r, float g, float b);
142-
void uwSetPlayerRace(uint32 raceProto);
141+
void uwPlayerJoinForce(uint32 force);
142+
void uwSetForceColor(float r, float g, float b);
143+
void uwSetForceRace(uint32 raceProto);
144+
void uwForceJoinTeam(uint32 team);
143145

144146
typedef struct UwMyPlayer
145147
{
@@ -167,6 +169,10 @@ typedef struct UwPerformanceStatistics
167169
uint32 networkDown;
168170
} UwPerformanceStatistics;
169171
void uwPerformanceStatistics(UwPerformanceStatistics *data);
172+
173+
void uwPerformanceProfiling(bool enable);
174+
uint64 uwProfilingEventBegin(void);
175+
void uwProfilingEventEnd(const char *name, uint64 eventStartTime);
170176
typedef enum UwOrderTypeEnum
171177
{
172178
UwOrderTypeEnum_None = 0,

sphinx/source/bots/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Specifically, bots cannot cheat - no more than a regular player could.
1010
setup
1111
programOverview
1212
programSafety
13+
performance
1314
troubleshooting

sphinx/source/bots/performance.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Performance
2+
===========
3+
Some hints on improving performance.
4+
5+
Distribute work over multiple ticks
6+
-----------------------------------
7+
Most events in the game take some time: shooting, moving, building, processing, etc.
8+
Therefore, it is generally unnecessary to keep updating everything all the time.
9+
10+
It is recommended to split work between multiple ticks to save some cpu time.
11+
Use switch on ``workStep++ mod 10``, and update different systems (eg. analyzing enemies, controlling armies, building, etc..).
12+
Note the use of ``workStep``, which is different variable than the tick provided by the game.
13+
This is to ensure continuous processing of all systems in case that some game ticks are skipped.
14+
15+
Be mindful of potential changes in the game state (eg. entities being destroyed) in between processing different systems.
16+
17+
Performance Statistics
18+
----------------------
19+
The game provides several statistics related to performance, which you can monitor and adapt your program.
20+
21+
- mainThreadUtilization - fraction (0..1) of the time the main thread does any work, vs time it sleeps.
22+
- ping - network delay measured in milliseconds.
23+
- networkUp - network bandwidth use measured in KB/s, from client to game server.
24+
- networkDown - same going from game server to client.
25+
26+
Profiling
27+
---------
28+
The game has built-in performance profiler.
29+
When enabled, it will open a browser with real-time flame-graphs of tasks running on all threads in the game client.
30+
31+
You may also inject your own profiling events.

0 commit comments

Comments
 (0)