Skip to content

Commit 95b1097

Browse files
committed
documentation: callbacks, network
1 parent 98ce7da commit 95b1097

File tree

13 files changed

+158
-68
lines changed

13 files changed

+158
-68
lines changed

sphinx/source/bots/programOverview.rst

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ Program Overview
22
================
33
High-level look at basic bot program.
44

5-
Environment Variables
6-
---------------------
7-
Some variables that may alter the behavior of your bot program.
8-
9-
- ``UNNATURAL_ROOT`` - path to the ``bin`` folder that contains the shared library.
10-
- ``UNNATURAL_CONNECT_LOBBY`` - id of Steam lobby to connect to.
11-
- ``UNNATURAL_CONNECT_ADDR`` - IP address or domain name to connect to.
12-
- ``UNNATURAL_CONNECT_PORT`` - port of the game server to connect to.
13-
145
Program Lifecycle
156
-----------------
167
At start, you are in full control of the program.
@@ -24,7 +15,7 @@ Initialization
2415
.. warning::
2516
Do *not* change current working directory once configured.
2617

27-
- Next you load the shared library.
18+
- Next, you load the shared library.
2819

2920
.. tab-set::
3021
:sync-group: language
@@ -45,7 +36,7 @@ Initialization
4536

4637
Connect
4738
^^^^^^^
48-
There are multiple ways to connect to a server.
39+
There are multiple ways to connect to a game server.
4940
The usual approach is to first try reconnecting, in case the game has crashed previously.
5041
Second, you connect to an existing server using the parameters provided in the environment variables, if any.
5142
Third, you spin up your own server.
@@ -54,7 +45,7 @@ Alternatively, you may implement your own logic to determine how to connect to a
5445

5546
No matter the method, the game will now take control over the program.
5647
Your program is blocked until the network connection closes.
57-
All your further actions will happen inside callbacks only.
48+
*All your further actions will happen inside callbacks only.*
5849

5950
Game Loop
6051
^^^^^^^^^
@@ -63,7 +54,7 @@ Notably, the ``Update`` callback is periodically called, no matter the game stat
6354
This is where you keep track of the state of the game, and perform any of your actions.
6455

6556
.. warning::
66-
The ``Update`` callback, and some other, may be called before the game has actually started (eg. in ``Session``), or when the map is not yet ``Loaded``.
57+
The ``Update`` callback, and some others, may be called before the game has actually started (eg. in ``Session``), or when the map is not yet ``Loaded``.
6758
Be mindful of what operations are valid in these circumstances.
6859

6960
You may prematurely close the connection with the ``Disconnect`` function.
@@ -76,4 +67,36 @@ When the connection function returns, the network connection has already been cl
7667
You may do any cleanup operations, for example you may save some statistics from the game.
7768
Ultimately, you should unload the shared library, and exit the program.
7869

79-
It may be tempting to just loop and connect again to next server, however this is strongly discouraged.
70+
It may be tempting to just loop over and connect to next server, however this is strongly discouraged.
71+
72+
Network
73+
-------
74+
The game is designed for multiplayer, and always plays over network, even in single-player scenarios.
75+
76+
Any actions that you do in your program are first send to the game server, then processed, and then the results are send back to your client.
77+
78+
Example: you call a function to place a construction.
79+
After that you look through all the entities and the construction is not there, as expected.
80+
You will not know the id of the entity either.
81+
The construction will appear only after the game server has processed the request.
82+
It is recommended to wait several ticks between these kinds of actions, to avoid placing same construction multiple times in different places.
83+
84+
.. note::
85+
Test your program over a real network, not just localhost.
86+
87+
Client-only State
88+
^^^^^^^^^^^^^^^^^
89+
Some state is stored on client only, notably:
90+
91+
- Units orders
92+
- Pathfinding
93+
- Logistics planning
94+
95+
Environment Variables
96+
---------------------
97+
Some variables that may alter the behavior of your bot program.
98+
99+
- ``UNNATURAL_ROOT`` - path to the ``bin`` folder that contains the shared library.
100+
- ``UNNATURAL_CONNECT_LOBBY`` - id of Steam lobby to connect to.
101+
- ``UNNATURAL_CONNECT_ADDR`` - IP address or domain name to connect to.
102+
- ``UNNATURAL_CONNECT_PORT`` - port of the game server to connect to.

sphinx/source/bots/troubleshooting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Troubleshooting
22
===============
33

4-
.. warning::
4+
.. important::
55
Do *not* use Flatpak for Steam on Linux.
66

77
.. dropdown:: Unable to load DLL 'unnatural-uwapi-hard'
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Callbacks
2+
=========
3+
4+
Game/Map/Connection State Callback
5+
-------------------
6+
Viz :ref:`concepts/programState:Game State`, :ref:`concepts/programState:Map State`, and :ref:`concepts/programState:Connection State`.
7+
8+
Update Callback
9+
---------------
10+
This callback is called every time the simulation time progresses (:ref:`tick <concepts/programState:Ticks>` changes).
11+
It is also called when game time is not progressing (eg. when paused, or in session), but real time elapses.
12+
The difference is indicated with the ``stepping`` parameter.
13+
14+
This callback is the primary driving point for your program.
15+
16+
.. note::
17+
Some ticks may be skipped, eg. when the game is lagging.
18+
Use ``>=`` when waiting for number of ticks to pass.
19+
20+
Shooting Callback
21+
-----------------
22+
Shooting events are synchronized *asynchronously and unreliably*.
23+
This event is useful for measuring threat levels.
24+
25+
.. warning::
26+
The entity id may be expired.
27+
28+
Explosions Callback
29+
-------------------
30+
Explosions events are synchronized *asynchronously and unreliably* - some events may be lost, or delivered much later.
31+
Do *not* depend on explosions events for important decisions.
32+
It is useful for measuring threat levels.
33+
34+
.. warning::
35+
The entity id may be expired.
36+
37+
Task Completed Callback
38+
-----------------------
39+
Some functions, such as pathfinding and clusters distances, need more time to compute the results.
40+
The calculations happen on separate threads internally in the uwapi library.
41+
Finally, this callback is called, passing in the results, when it finishes.
42+
43+
This callback may be called at any time, relative to other callbacks - there are no guarantees.
44+
45+
.. warning::
46+
Do *not* try to wait (blocking) for the tasks.
47+
It will block the whole program indefinitely.
48+
49+
.. warning::
50+
Any entity id may have expired by the time the task completed.
51+
52+
Force Eliminated Callback
53+
-------------------------
54+
This informs you that a player lost.
55+
56+
Chat Callback
57+
-------------
58+
You have received a message.

sphinx/source/concepts/entities.rst

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,107 +25,107 @@ These ids are assigned by the server, and client cannot predict the values.
2525
.. note::
2626
Some entities and/or components are synchronized only with the owner of the entity.
2727

28-
Proto
29-
-----
28+
Proto Component
29+
---------------
3030
Defines the prototype of the entity.
3131

3232
It is allowed to change from construction to unit, or from unit to another unit.
3333
It is forbidden to change eg. from unit to resource.
3434

35-
Owner
36-
-----
35+
Owner Component
36+
---------------
3737
Defines which force owns this entity.
3838

3939
Immutable.
4040

41-
Controller
42-
----------
41+
Controller Component
42+
--------------------
4343
In case that multiple players belong to the same force, the last player to give any orders to this entity will become the controller of the entity.
4444

45-
.. warning::
45+
.. note::
4646
Controllers are not yet implemented.
4747

48-
Position
49-
--------
48+
Position Component
49+
------------------
5050
Index of the tile this entity is placed on.
5151
In case the entity has large radius, this component defines the center tile.
5252

5353
The yaw defines the orientation (a rotation along the local vertical axis) of this entity on the tile.
54-
The actual direction of 0 degrees yaw is different for each tile.
54+
The actual facing of 0 degrees yaw is different for each tile.
5555

56-
Unit
57-
----
56+
Unit Component
57+
--------------
5858
Contains additional state for a unit (or building).
5959

6060
- ``Shooting`` - waiting for the cannon to cool down.
6161
- ``Processing`` - the unit has processed a recipe and is waiting for it to complete.
6262
- ``Rebuilding`` - recipe for the unit has changed, and the unit is waiting for the changes to complete.
6363
- ``Stalling`` - the unit's recipe cannot be executed, usually because a limit for the outputs has been reached.
6464

65-
Life
66-
----
65+
Life Component
66+
--------------
6767
Amount of life of the unit (or building).
6868

69-
Move
70-
----
69+
Move Component
70+
--------------
7171
Contains information about current movement of the unit.
7272
Information is available for the next neighboring tile only.
7373

74-
Aim
75-
---
74+
Aim Component
75+
-------------
7676
Id of a target unit that this unit will automatically shoot at.
7777

78-
Recipe
79-
------
78+
Recipe Component
79+
----------------
8080
Id of the prototype of the recipe that this unit will automatically process.
8181

82-
UpdateTimestamp
83-
---------------
82+
Update Timestamp Component
83+
--------------------------
8484
Contains a timestamp (tick) of when this construction started, or when this unit's recipe was last processed.
8585
It is used for planning logistics deliveries.
8686

87-
RecipeStatistics
88-
----------------
87+
Recipe Statistics Component
88+
---------------------------
8989
Used for calculating this unit's processing efficiency.
9090

9191
It is reset when the recipe changes.
9292

93-
Priority
94-
--------
93+
Priority Component
94+
------------------
9595
Contains the priority assigned by the player.
9696
The priority applies to both constructions and recipe processing.
9797
It is used by the logistics planning.
9898

99-
Amount
100-
------
99+
Amount Component
100+
----------------
101101
Contains the count of the resource in this entity.
102102

103-
Attachment
104-
----------
103+
Attachment Component
104+
--------------------
105105
Defines that this entity is attached to another entity.
106106
This entity is automatically moved to the position (and orientation) of the target.
107107
This is commonly used by a resource carried by a truck.
108108

109-
Player
110-
------
109+
Player Component
110+
----------------
111111
This entity represents a client - a player or an observer.
112112

113-
Force
114-
-----
113+
Force Component
114+
---------------
115115
This entity represents a force.
116116
The component contains public information about the force.
117117

118-
ForceDetails
119-
------------
118+
Force Details Component
119+
-----------------------
120120
This component contains private information about the force.
121121

122-
ForeignPolicy
123-
-------------
122+
Foreign Policy Component
123+
------------------------
124124
This entity declares the policy between two forces.
125125

126-
DiplomacyProposal
127-
-----------------
126+
Diplomacy Proposal Component
127+
----------------------------
128128
This entity contains a proposal of a policy to another force.
129129

130-
.. warning::
130+
.. note::
131131
Diplomacy is not yet implemented.

sphinx/source/concepts/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Glossary and explanation of concepts used throughout this documentation and in t
1010
resources
1111
combat
1212
programState
13+
callbacks

sphinx/source/concepts/map.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,28 @@ There are multiple functions for finding tiles within some distance.
5454
:img-bottom: /_static/area-query-range.svg
5555
:text-align: center
5656

57-
**AreaRange**
57+
**Area Range**
5858

5959
.. grid-item::
6060

6161
.. card::
6262
:img-bottom: /_static/area-query-connected.svg
6363
:text-align: center
6464

65-
**AreaConnected**
65+
**Area Connected**
6666

6767
.. grid-item::
6868

6969
.. card::
7070
:img-bottom: /_static/area-query-neighborhood.svg
7171
:text-align: center
7272

73-
**AreaNeighborhood**
73+
**Area Neighborhood**
7474

7575
.. grid-item::
7676

7777
.. card::
7878
:img-bottom: /_static/area-query-extended.svg
7979
:text-align: center
8080

81-
**AreaExtended**
81+
**Area Extended**

sphinx/source/concepts/players.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ In that case, these players share ownership of all the units associated with the
2727
Each unit remembers the last player that gave it any order, this player is called the controller.
2828
Any automated system in the game (eg. logistics controls for trucks) will not give any orders to units with different controller.
2929

30-
.. warning::
30+
.. note::
3131
Controllers are only partially implemented in the game for now.
3232

3333
Neutral
@@ -47,7 +47,7 @@ The game has additional enumeration values for some edge cases.
4747
Diplomacy
4848
^^^^^^^^^
4949

50-
.. warning::
50+
.. note::
5151
Diplomacy is not yet implemented.
5252

5353
Teams

sphinx/source/concepts/programState.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,12 @@ Connection State
3434
- ``Connected`` - connection has been fully established. We are receiving data, and can send commands.
3535
- ``Error`` - connection has been closed or broken. Do *not* send any commands.
3636

37+
Ticks
38+
-----
39+
Ticks are the measurement of time in the game simulation.
40+
41+
When ticks are ticking, units are moving.
42+
When tick is the same, no units are moving, no one is shooting, etc.
43+
44+
There is 20 ticks per second, by default.
45+
This is provided as a constant in the api.

sphinx/source/concepts/prototypes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Unit Prototype
2828
Both buildings and units are treated as units in the game.
2929

3030
.. note::
31-
Some neutral objects, such as trees, rocks, ore deposits, etc. are all units too.
31+
Some neutral objects, such as trees, rocks, ore deposits, etc. are units too.
3232

3333
The prototype contains combat related properties, movement speeds or building radius, a list of available recipes, and a list of applicable upgrades.
3434
Additionally, there are several boolean properties, which modify behavior or requirements of the unit/building.

sphinx/source/conf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
templates_path = ['_templates']
2626
exclude_patterns = []
27-
28-
27+
autosectionlabel_prefix_document = True
2928

3029
# -- Options for HTML output -------------------------------------------------
3130
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

0 commit comments

Comments
 (0)