Skip to content

Commit d4c4cf1

Browse files
committed
add python prototypes extractor; fix some docs
1 parent 7648de1 commit d4c4cf1

File tree

9 files changed

+91
-40
lines changed

9 files changed

+91
-40
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 = 42;
37+
static const uint32 UW_VERSION = 43;
3838
static const uint32 UW_GameTicksPerSecond = 20;
3939

4040
typedef struct UwIds

csharp/uwapi/interop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public static extern void uwCommandPlaceConstruction(uint constructionProto, uin
317317
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
318318
public static extern void uwCommandSelfDestruct(uint entityId);
319319

320-
public const uint UW_VERSION = 42;
320+
public const uint UW_VERSION = 43;
321321
public const uint UW_GameTicksPerSecond = 20;
322322
[StructLayout(LayoutKind.Sequential)]
323323
public struct UwIds

python/prototypes.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from uwapi import *
2+
3+
class Extractor:
4+
def __init__(self):
5+
uw_events.on_map_state(self.map_state)
6+
7+
def map_state(self, state: MapState):
8+
if state == MapState.Loaded:
9+
self.extract()
10+
uw_game.disconnect()
11+
12+
def extract(self):
13+
uw_game.log_info("extracting")
14+
15+
types = {}
16+
for proto_id, prototype in uw_prototypes._all.items():
17+
if prototype.type not in types:
18+
types[prototype.type] = {}
19+
types[prototype.type][proto_id] = prototype
20+
21+
output = ""
22+
for type, mapping in types.items():
23+
output += f"## {type.name}\n"
24+
for id, prototype in sorted(mapping.items(), key=lambda x: x[1].name):
25+
output += f"{prototype.name}: {id}\n"
26+
output += f"\n\n"
27+
28+
with open("prototypes.md", "w") as f:
29+
f.write(output)
30+
31+
uw_game.log_info("extraction done")
32+
33+
def run(self):
34+
uw_game.set_connect_start_gui(True)
35+
uw_game.connect_new_server()
36+
37+
if __name__ == "__main__":
38+
with UwapiLibrary():
39+
Extractor().run()

python/uwapi/bots.h

Lines changed: 1 addition & 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 = 42;
12+
static const uint32 UW_VERSION = 43;
1313
static const uint32 UW_GameTicksPerSecond = 20;
1414

1515
typedef struct UwIds

sphinx/source/concepts/callbacks.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Make sure to carefully decode them, as shown in the example below.
4040
index = 0
4141
while index < len(data):
4242
control = uw_events.shooting_control_data(data[index])
43-
if control.type == UwShootingEventEnum.Shooting:
43+
if control.type == ShootingEvent.Shooting:
4444
shooter_id = data[index + 1]
4545
for i in range(1, control.count):
4646
target_id = data[index + i + 1]
@@ -80,22 +80,22 @@ Make sure to carefully decode them, as shown in the example below.
8080
extern "C"
8181
void uwShootingsCallback(const UwShootingsArray *data)
8282
{
83-
const auto shooting = uw::makeVector(data);
84-
uint32 index = 0;
85-
while (index < shooting.size())
86-
{
87-
const auto control = uw::shootingControlData(shooting[index]);
88-
if (control.type == UwShootingEventEnum_Shooting)
89-
{
90-
const uint32 shooterId = shooting[index + 1];
91-
for (uint32 i = 1; i < control.count; i++)
92-
{
93-
const uint32 targetId = shooting[index + i + 1];
94-
// handle shooting event
95-
}
96-
}
97-
index += control.count + 1;
98-
}
83+
const auto shooting = uw::makeVector(data);
84+
uint32 index = 0;
85+
while (index < shooting.size())
86+
{
87+
const auto control = uw::shootingControlData(shooting[index]);
88+
if (control.type == UwShootingEventEnum_Shooting)
89+
{
90+
const uint32 shooterId = shooting[index + 1];
91+
for (uint32 i = 1; i < control.count; i++)
92+
{
93+
const uint32 targetId = shooting[index + i + 1];
94+
// handle shooting event
95+
}
96+
}
97+
index += control.count + 1;
98+
}
9999
}
100100
101101
Task Completed Callback

sphinx/source/concepts/economy.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ Producing units or researching upgrades works the same way as recipes.
1515
# find closest viable position for miner:
1616
p = uw_world.find_construction_placement(DRILL_CONSTRUCTION_ID, home_position, METAL_RECIPE_ID) # recipe id is optional
1717
if p == INVALID:
18-
return
18+
return
1919
2020
# place construction:
21-
uw_commands.place_construction(DRILL_CONSTRUCTION_ID, p, 0, METAL_RECIPE_ID, UwPriorityEnum.Normal) # yaw, recipe, and priority are optional
21+
uw_commands.place_construction(DRILL_CONSTRUCTION_ID, p, 0, METAL_RECIPE_ID, Priority.High) # yaw, recipe, and priority are optional
2222
2323
# recipe and priority can be changed later:
2424
uw_commands.set_recipe(own_id, ANOTHER_RECIPE_ID)
25-
uw_commands.set_priority(own_id, UwPriorityEnum.High)
25+
uw_commands.set_priority(own_id, Priority.Normal)
2626
2727
.. tab-item:: C#
2828
:sync: csharp
@@ -32,14 +32,14 @@ Producing units or researching upgrades works the same way as recipes.
3232
// find closest viable position for miner:
3333
uint p = World.FindConstructionPlacement(DRILL_CONSTRUCTION_ID, homePosition, METAL_RECIPE_ID); // recipe id is optional
3434
if (p == Entity.Invalid):
35-
return;
35+
return;
3636
3737
// place construction:
38-
Commands.PlaceConstruction(DRILL_CONSTRUCTION_ID, p, 0, METAL_RECIPE_ID, Interop.UwPriorityEnum.Normal); // yaw, recipe, and priority are optional
38+
Commands.PlaceConstruction(DRILL_CONSTRUCTION_ID, p, 0, METAL_RECIPE_ID, UwPriorityEnum.High); // yaw, recipe, and priority are optional
3939
4040
// recipe and priority can be changed later:
4141
Commands.SetRecipe(own_id, ANOTHER_RECIPE_ID)
42-
Commands.SetPriority(own_id, Interop.UwPriorityEnum.High)
42+
Commands.SetPriority(own_id, UwPriorityEnum.Normal)
4343
4444
.. tab-item:: C++
4545
:sync: cpp

sphinx/source/concepts/entities.rst

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,17 @@ It is forbidden to change eg. from unit to resource.
7474
7575
x = uw_world.entity(id)
7676
77+
# check if entity has Proto component:
78+
x.Proto is not None
79+
7780
# find type (unit, resource, construction, ...) of the entity:
7881
x.type()
7982
80-
# check if entity has Proto component:
81-
x.Proto is not None
83+
# check if entity is a unit:
84+
x.type() == PrototypeType.Unit
85+
86+
# alternative check if entity is a unit (only units have the Unit component):
87+
x.Unit is not None
8288
8389
# find id of the prototype of an entity:
8490
x.Proto.proto # assumes that the entity actually has Proto component
@@ -93,12 +99,18 @@ It is forbidden to change eg. from unit to resource.
9399
94100
Entity x = World.Entity(id);
95101
96-
// find type (unit, resource, construction, ...) of the entity:
97-
x.type
98-
99102
// check if entity has Proto component:
100103
x.Proto.HasValue
101104
105+
// find type (unit, resource, construction, ...) of the entity:
106+
x.Type
107+
108+
// check if entity is a unit:
109+
x.Type == UwPrototypeTypeEnum.Unit
110+
111+
// alternative check if entity is a unit (only units have the Unit component):
112+
x.Unit.HasValue
113+
102114
// find id of the prototype of an entity:
103115
x.Proto.Value.proto // assumes that the entity actually has Proto component
104116
@@ -142,8 +154,8 @@ Immutable.
142154
x.Owner.Value.force
143155
144156
// check if entity is own or enemy:
145-
x.Own()
146-
x.Enemy()
157+
x.Own
158+
x.Enemy
147159
148160
.. tab-item:: C++
149161
:sync: cpp
@@ -212,7 +224,7 @@ Contains additional state for a unit (or building).
212224
.. code-block:: python
213225
214226
# check if unit is Processing:
215-
(x.Unit.state & UwUnitStateFlags.Processing) != 0
227+
(x.Unit.state & UnitState.Processing) != 0
216228
217229
.. tab-item:: C#
218230
:sync: csharp

sphinx/source/concepts/map.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ There are multiple functions for finding tiles within some distance.
8888

8989
.. code-block:: python
9090
91-
# find all connected tiles around home position:
92-
uw_map.area_connected(home_position, 300)
91+
# find all connected tiles around a position, up to 300 meters:
92+
uw_map.area_connected(tile_index, 300)
9393
9494
.. tab-item:: C#
9595
:sync: csharp
9696

9797
.. code-block:: csharp
9898
99-
// find all connected tiles around home position:
100-
Map.AreaConnected(homePosition, 300)
99+
// find all connected tiles around a position, up to 300 meters:
100+
Map.AreaConnected(tileIndex, 300)
101101
102102
.. tab-item:: C++
103103
:sync: cpp

sphinx/source/concepts/military.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ You can have multiple orders queued for each unit.
2525
2626
# alternatively, enqueue attack order:
2727
o = uw_commands.fight_to_entity(enemy_id)
28-
o.priority = o.priority | UwOrderPriorityFlags.Enqueue
28+
o.priority = o.priority | OrderPriority.Enqueue
2929
uw_commands.order(own_id, o)
3030
3131
.. tab-item:: C#
@@ -41,7 +41,7 @@ You can have multiple orders queued for each unit.
4141
4242
// alternatively, enqueue attack order:
4343
Order o = Commands.FightToEntity(enemy_id);
44-
o.priority |= Interop.UwOrderPriorityFlags.Enqueue;
44+
o.priority |= UwOrderPriorityFlags.Enqueue;
4545
Commands.Order(own_id, o);
4646
4747
.. tab-item:: C++

0 commit comments

Comments
 (0)