Skip to content

Commit ce2f342

Browse files
committed
simplify overview (performance imrpovement); add async connect
1 parent abc99dd commit ce2f342

File tree

9 files changed

+38
-31
lines changed

9 files changed

+38
-31
lines changed

c/uwapi/uwapi/modules/botsConnection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ extern "C"
5656
// connect
5757

5858
UNNATURAL_API void uwSetConnectStartGui(bool enabled, const char *extraCmdParams);
59+
UNNATURAL_API void uwSetConnectAsync(bool async);
5960
UNNATURAL_API bool uwConnectFindLan(uint64 timeoutMicroseconds);
6061
UNNATURAL_API void uwConnectDirect(const char *address, uint16 port);
6162
UNNATURAL_API void uwConnectLobbyId(uint64 lobbyId);
6263
UNNATURAL_API bool uwConnectEnvironment();
6364
UNNATURAL_API void uwConnectNewServer(uint32 visibility, const char *name, const char *extraCmdParams);
6465
UNNATURAL_API bool uwTryReconnect(void);
6566
UNNATURAL_API void uwDisconnect(void);
67+
UNNATURAL_API bool uwAsyncUpdate(void);
6668

6769
// my player
6870

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 = 46;
37+
static const uint32 UW_VERSION = 47;
3838
static const uint32 UW_GameTicksPerSecond = 20;
3939

4040
typedef struct UwIds

csharp/uwapi/admin.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static void SetMapSelection(string path)
2525
Interop.uwAdminSetMapSelection(path);
2626
}
2727

28+
public static void SetGameConfig(Interop.UwGameConfig cfg)
29+
{
30+
Interop.uwAdminSetGameConfig(ref cfg);
31+
}
32+
2833
public static void StartGame()
2934
{
3035
Interop.uwAdminStartGame();

csharp/uwapi/interop.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public enum UwConnectionStateEnum
153153
public static extern void uwSetConnectStartGui([MarshalAs(UnmanagedType.I1)] bool enabled,
154154
[MarshalAs(UnmanagedType.LPStr)] string extraCmdParams);
155155

156+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
157+
public static extern void uwSetConnectAsync([MarshalAs(UnmanagedType.I1)] bool async);
158+
156159
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
157160
[return:MarshalAs(UnmanagedType.I1)]
158161
public static extern bool uwConnectFindLan(ulong timeoutMicroseconds);
@@ -178,6 +181,10 @@ public static extern void uwConnectNewServer(uint visibility, [MarshalAs(Unmanag
178181
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
179182
public static extern void uwDisconnect();
180183

184+
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
185+
[return:MarshalAs(UnmanagedType.I1)]
186+
public static extern bool uwAsyncUpdate();
187+
181188
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
182189
public static extern void uwSetPlayerName([MarshalAs(UnmanagedType.LPStr)] string name);
183190

@@ -320,7 +327,7 @@ public static extern void uwCommandPlaceConstruction(uint constructionProto, uin
320327
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
321328
public static extern void uwCommandSelfDestruct(uint entityId);
322329

323-
public const uint UW_VERSION = 46;
330+
public const uint UW_VERSION = 47;
324331
public const uint UW_GameTicksPerSecond = 20;
325332
[StructLayout(LayoutKind.Sequential)]
326333
public struct UwIds

csharp/uwapi/world.cs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ public static uint FindConstructionPlacement(uint constructionProto, uint positi
6767

6868
public static IReadOnlyList<OverviewFlags> OverviewFlags()
6969
{
70+
Interop.UwOverviewExtract ex = new Interop.UwOverviewExtract();
71+
Interop.uwOverviewExtract(ref ex);
72+
OverviewFlags[] overview = new OverviewFlags[ex.count];
73+
if (ex.count > 0)
74+
Marshal.Copy(ex.flags, (int[])(object)overview, 0, (int)ex.count);
7075
return overview;
7176
}
7277

7378
public static OverviewFlags OverviewFlags(uint position)
7479
{
75-
return overview[(int)position];
80+
return Interop.uwOverviewFlags(position);
7681
}
7782

7883
public static uint[] OverviewEntities(uint position)
@@ -128,7 +133,6 @@ public static void OfferForeignPolicy(uint forceId, PolicyEnum policy)
128133
static MyForceStatistics myForceStatistics = new MyForceStatistics();
129134
static readonly Dictionary<uint, Entity> entities = new Dictionary<uint, Entity>();
130135
static readonly Dictionary<uint, PolicyEnum> policies = new Dictionary<uint, PolicyEnum>();
131-
static OverviewFlags[] overview = new OverviewFlags[0];
132136

133137
static uint[] AllIds()
134138
{
@@ -196,21 +200,6 @@ static void UpdatePolicies()
196200
}
197201
}
198202

199-
static void UpdateOverview(bool stepping)
200-
{
201-
if (stepping)
202-
{
203-
Interop.UwOverviewExtract ex = new Interop.UwOverviewExtract();
204-
Interop.uwOverviewExtract(ref ex);
205-
if (overview.Length != ex.count)
206-
overview = new OverviewFlags[ex.count];
207-
if (ex.count > 0)
208-
Marshal.Copy(ex.flags, (int[])(object)overview, 0, (int)ex.count);
209-
}
210-
else
211-
overview = new OverviewFlags[0];
212-
}
213-
214203
static void Updating(object sender, bool stepping)
215204
{
216205
Interop.uwMyPlayer(ref myPlayer);
@@ -219,7 +208,6 @@ static void Updating(object sender, bool stepping)
219208
UpdateFresh();
220209
UpdateModified();
221210
UpdatePolicies();
222-
UpdateOverview(stepping);
223211
}
224212

225213
static World()

python/uwapi/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def get_server_port(self) -> int:
2121
def set_map_selection(self, path: str) -> None:
2222
uw_interop.uwAdminSetMapSelection(path)
2323

24+
def set_game_config(self, cfg: UwGameConfig) -> None:
25+
uw_interop.uwAdminSetGameConfig(cfg)
26+
2427
def start_game(self) -> None:
2528
uw_interop.uwAdminStartGame()
2629

python/uwapi/bots.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef int32_t sint32;
99
typedef uint64_t uint64;
1010
typedef int64_t sint64;
1111

12-
static const uint32 UW_VERSION = 46;
12+
static const uint32 UW_VERSION = 47;
1313
static const uint32 UW_GameTicksPerSecond = 20;
1414

1515
typedef struct UwIds
@@ -131,13 +131,15 @@ void uwSetConnectionStateCallback(UwConnectionStateCallbackType callback);
131131
UwConnectionStateEnum uwConnectionState(void);
132132

133133
void uwSetConnectStartGui(bool enabled, const char *extraCmdParams);
134+
void uwSetConnectAsync(bool async);
134135
bool uwConnectFindLan(uint64 timeoutMicroseconds);
135136
void uwConnectDirect(const char *address, uint16 port);
136137
void uwConnectLobbyId(uint64 lobbyId);
137138
bool uwConnectEnvironment();
138139
void uwConnectNewServer(uint32 visibility, const char *name, const char *extraCmdParams);
139140
bool uwTryReconnect(void);
140141
void uwDisconnect(void);
142+
bool uwAsyncUpdate(void);
141143

142144
void uwSetPlayerName(const char *name);
143145
void uwPlayerJoinForce(uint32 force);

python/uwapi/interop.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ def uwSetConnectStartGui(self, enabled: bool, extraCmdParams: str) -> None:
642642
extraCmdParams_ = self._str_pytoc(extraCmdParams)
643643
self._api.uwSetConnectStartGui(enabled, extraCmdParams_)
644644

645+
def uwSetConnectAsync(self, async: bool) -> None:
646+
self._api.uwSetConnectAsync(async)
647+
645648
def uwConnectFindLan(self, timeoutMicroseconds: int) -> bool:
646649
ret = self._api.uwConnectFindLan(timeoutMicroseconds)
647650
ret = bool(ret)
@@ -672,6 +675,11 @@ def uwTryReconnect(self) -> bool:
672675
def uwDisconnect(self) -> None:
673676
self._api.uwDisconnect()
674677

678+
def uwAsyncUpdate(self) -> bool:
679+
ret = self._api.uwAsyncUpdate()
680+
ret = bool(ret)
681+
return ret
682+
675683
def uwSetPlayerName(self, name: str) -> None:
676684
name_ = self._str_pytoc(name)
677685
self._api.uwSetPlayerName(name_)

python/uwapi/world.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class World:
1818
_my_force_statistics = _make_empty_UwMyForceStatistics()
1919
_entities: dict[int, Entity] = {}
2020
_policies: dict[int, UwForeignPolicyEnum] = {}
21-
_overview: list[UwOverviewFlags] = []
2221

2322
def __new__(cls):
2423
if cls._instance is None:
@@ -65,10 +64,10 @@ def find_construction_placement(
6564
)
6665

6766
def overview_flags_all(self) -> list[UwOverviewFlags]:
68-
return self._overview
67+
return uw_interop.uwOverviewExtract().flags
6968

7069
def overview_flags(self, position: int) -> UwOverviewFlags:
71-
return self._overview[position]
70+
return uw_interop.uwOverviewFlags(position)
7271

7372
def overview_entities(self, position: int) -> list[int]:
7473
return uw_interop.uwOverviewIds(position).ids
@@ -144,12 +143,6 @@ def _update_policies(self) -> None:
144143
if fp.forces[1] == self._my_player.forceEntityId:
145144
self._policies[fp.forces[0]] = fp.policy
146145

147-
def _update_overview(self, stepping: bool) -> None:
148-
if stepping:
149-
self._overview = uw_interop.uwOverviewExtract().flags
150-
else:
151-
self._overview = []
152-
153146
def _update(self, stepping: bool) -> None:
154147
tmp = uw_interop.uwMyPlayer()
155148
self._my_player = tmp[1] if tmp[0] else _make_empty_UwMyPlayer()
@@ -158,7 +151,6 @@ def _update(self, stepping: bool) -> None:
158151
self._update_fresh()
159152
self._update_modified()
160153
self._update_policies()
161-
self._update_overview(stepping)
162154

163155

164156
uw_world = World()

0 commit comments

Comments
 (0)