Skip to content

Commit 3b46efd

Browse files
committed
improve python prototypes
1 parent eaa68a7 commit 3b46efd

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

python/bot/bot.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import random
22
from uwapi import *
33

4+
45
class Bot:
56
is_configured: bool = False
6-
work_step: int = 0 # save some cpu cycles by splitting work over multiple steps
7+
work_step: int = 0 # save some cpu cycles by splitting work over multiple steps
78

89
def __init__(self):
910
uw_events.on_update(self.on_update)
1011

1112
def attack_nearest_enemies(self):
12-
own_units = [x for x in uw_world.entities().values() if x.own() and x.Unit is not None and x.proto().data.get("dps", 0) > 0]
13+
own_units = [
14+
x
15+
for x in uw_world.entities().values()
16+
if x.own() and x.Unit is not None and x.proto().data.get("dps", 0) > 0
17+
]
1318
if not own_units:
1419
return
15-
enemy_units = [x for x in uw_world.entities().values() if x.enemy() and x.Unit is not None]
20+
enemy_units = [
21+
x for x in uw_world.entities().values() if x.enemy() and x.Unit is not None
22+
]
1623
if not enemy_units:
1724
return
1825
for own in own_units:
1926
if len(uw_commands.orders(own.id)) == 0:
20-
enemy = min(enemy_units, key=lambda x: uw_map.distance_estimate(own.pos(), x.pos()))
27+
enemy = min(
28+
enemy_units,
29+
key=lambda x: uw_map.distance_estimate(own.pos(), x.pos()),
30+
)
2131
uw_commands.order(own.id, uw_commands.fight_to_entity(enemy.id))
2232

2333
def assign_random_recipes(self):
@@ -35,7 +45,7 @@ def configure(self):
3545
self.is_configured = True
3646

3747
uw_game.set_player_name("bot-py")
38-
uw_game.player_join_force(0) # create new force
48+
uw_game.player_join_force(0) # create new force
3949
uw_game.set_force_color(1, 0, 0)
4050
# todo choose race
4151

@@ -48,7 +58,7 @@ def on_update(self, stepping: bool):
4858
return
4959

5060
self.work_step += 1
51-
match self.work_step % 10: # save some cpu cycles by splitting work over multiple steps
61+
match self.work_step % 10: # save some cpu cycles by splitting work over multiple steps
5262
case 1:
5363
self.attack_nearest_enemies()
5464
case 5:

python/uwapi/entity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,6 @@ def unit_upgrades(self) -> UwUnitUpgrades:
8181
if self.type() == UwPrototypeTypeEnum.Unit
8282
else _make_empty_UwUnitUpgrades()
8383
)
84+
85+
def tagged(self, tag: int) -> bool:
86+
return self.proto().tagged(tag)

python/uwapi/prototypes.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ class Prototype:
1111
type: UwPrototypeTypeEnum = UwPrototypeTypeEnum.Nothing
1212
name: str = ""
1313
data: Dict[str, Any] = field(default_factory=dict)
14+
tags: List[int] = field(default_factory=list)
15+
tagsNames: List[str] = field(default_factory=list)
16+
json: str = ""
17+
18+
def tagged(self, tag: int) -> bool:
19+
return tag in self.tags
20+
21+
def _load(self) -> None:
22+
self.type = uw_interop.uwPrototypeType(self.id)
23+
self.json = uw_interop.uwPrototypeJson(self.id)
24+
self.data = json.loads(self.json)
25+
self.name = self.data.get("name", "")
26+
self.tags = self.data.get("tags", [])
27+
self.tagsNames = self.data.get("tagsNames", [])
1428

1529

1630
class Prototypes:
@@ -33,15 +47,33 @@ def type(self, id: int) -> UwPrototypeTypeEnum:
3347
return p.type
3448
return UwPrototypeTypeEnum.Nothing
3549

50+
def name(self, id: int) -> str:
51+
p = self._all.get(id, None)
52+
return p.name if p is not None else ""
53+
54+
def json(self, id: int) -> str:
55+
p = self._all.get(id, None)
56+
return p.json if p is not None else ""
57+
58+
def definitions(self) -> Dict[str, Any]:
59+
return self._definitions
60+
61+
def hashString(self, name: str) -> int:
62+
return uw_interop.uwHashString(name)
63+
64+
def tagId(self, name: str) -> int:
65+
try:
66+
return self._definitions["tagsNames"].index(name)
67+
except ValueError:
68+
raise KeyError(f"tag name '{name}' not found")
69+
3670
def _load(self) -> None:
3771
uw_interop.uwLog(UwSeverityEnum.Info, "loading prototypes")
38-
self._all: Dict[int, Prototype] = {}
39-
self._definitions: Dict[str, Any] = {}
72+
self._all.clear()
73+
self._definitions.clear()
4074
for id in uw_interop.uwAllPrototypes().ids:
4175
p = Prototype(id)
42-
p.type = uw_interop.uwPrototypeType(id)
43-
p.data = json.loads(uw_interop.uwPrototypeJson(id))
44-
p.name = p.data["name"]
76+
p._load()
4577
self._all[id] = p
4678
self._definitions = json.loads(uw_interop.uwDefinitionsJson())
4779
uw_interop.uwLog(UwSeverityEnum.Info, "prototypes loaded")

python/uwapi/world.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ def _update_policies(self) -> None:
114114
if not e.ForeignPolicy:
115115
continue
116116
fp = e.ForeignPolicy
117-
if fp.forces[0] == self._my_force_id:
117+
if fp.forces[0] == self._my_player.forceEntityId:
118118
self._policies[fp.forces[1]] = fp.policy
119-
if fp.forces[1] == self._my_force_id:
119+
if fp.forces[1] == self._my_player.forceEntityId:
120120
self._policies[fp.forces[0]] = fp.policy
121121

122122
def _update_overview(self, stepping: bool) -> None:
@@ -127,7 +127,7 @@ def _update_overview(self, stepping: bool) -> None:
127127

128128
def _update(self, stepping: bool) -> None:
129129
tmp = uw_interop.uwMyPlayer()
130-
self._my_force_id = tmp[1].forceEntityId if tmp[0] else 0
130+
self._my_player = tmp[1] if tmp[0] else _make_empty_UwMyPlayer()
131131
self._my_force_statistics = uw_interop.uwMyForceStatistics()
132132
self._update_removed()
133133
self._update_fresh()

0 commit comments

Comments
 (0)