• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 15:07
CET 21:07
KST 05:07
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
RSL Season 3: RO16 results & RO8 bracket13Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge2[TLMC] Fall/Winter 2025 Ladder Map Rotation14Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA17
StarCraft 2
General
SC: Evo Complete - Ranked Ladder OPEN ALPHA Weekly Cups (Nov 10-16): Reynor, Solar lead Zerg surge RSL Season 3: RO16 results & RO8 bracket RSL Season 3 - Playoffs Preview Mech is the composition that needs teleportation t
Tourneys
RSL Revival: Season 3 $5,000+ WardiTV 2025 Championship StarCraft Evolution League (SC Evo Biweekly) Constellation Cup - Main Event - Stellar Fest 2025 RSL Offline Finals Dates + Ticket Sales!
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death
Brood War
General
Data analysis on 70 million replays soO on: FanTaSy's Potential Return to StarCraft 2v2 maps which are SC2 style with teams together? What happened to TvZ on Retro? BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [BSL21] RO16 Tie Breaker - Group B - Sun 21:00 CET [BSL21] RO16 Tie Breaker - Group A - Sat 21:00 CET Small VOD Thread 2.0
Strategy
Current Meta Game Theory for Starcraft How to stay on top of macro? PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread Should offensive tower rushing be viable in RTS games? Path of Exile Nintendo Switch Thread Clair Obscur - Expedition 33
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread The Games Industry And ATVI Things Aren’t Peaceful in Palestine About SC2SEA.COM
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
The Health Impact of Joining…
TrAiDoS
Dyadica Evangelium — Chapt…
Hildegard
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1951 users

Towards a good SC bot - P56 - Latency

Blogs > imp42
Post a Reply
imp42
Profile Blog Joined November 2010
398 Posts
March 07 2017 17:32 GMT
#1
Towards a good StarCraft bot - Part 6 - "Latency in Brood War"

Summary:
+ Show Spoiler +
Introduction into latency considerations and one example how to apply it in a micro situation.


When programming a bot sooner or later you will deal with latency. And more likely than not you will experience a couple of surprises the first time you play against other bots over a network.
This posts intends to save you some frustration and shed some light on the internal workings of Brood War.

Let’s start with some definitions to create a common vocabulary for discussion.

Latency Frames (constant throughout the course of a game)

Latency in Brood War is the maximum delay between the frame a command gets issued and the frame that command gets executed. Latency Frames define the maximum delay in terms of number of frames.

Turn (constant throughout the course of a game)

A turn is a set of sequential frames, which are bundled together such that commands issued in any of those frames are executed together. If a turn consists of 3 frames, then any commands issued in frame 1 and 2 will be executed together with commands issued in frame 3.

Turn Size

The number of frames per turn

BWAPI Remaining Latency Frames (variable per frame)

Remaining latency frames indicate the actual delay until an issued command is executed (as opposed to the maximum delay). Since commands from the same turn are executed together, this delay varies depending on when during the turn the command was issued. If a command is issued during the first frame of a turn, then Remaining Latency Frames will be equal to Latency Frames. That is, the actual delay will be equal to the maximum delay.

BWAPI latency compensation (default: enabled)

BWAPI has a functionality to simulate status changes as soon as a command is issued rather than when it is executed. This includes order, target position and a bunch of stats like isIdle, etc. The drawback being the information is not actually correct. So there might be just as many cases where latency compensation backfires and does hurt more than it helps. Imagine an SCV constructing a building and you tell it to stop. With latency compensation enabled it will immediately show as isIdle. But relying on the unit to be idle may result in an error since actually the unit is still constructing. The general recommendation is therefore to disable latency compensation.

Example: latency frames = 6, turn size = 2

Frame 0: issue unit1.attack(). Remaining latency frames: 6
Frame 1: issue unit2.attack(). Remaining latency frames: 5
Frame 2: do nothing. Remaining latency frames: 6
Frame 3: do nothing. Remaining latency frames: 5
Frame 4: do nothing. Remaining latency frames: 6
Frame 5: do nothing. Remaining latency frames: 5
Frame 6: execute unit1.attack(), execute unit2.attack()

How do you detect the latency settings?

Latency Frames and Remaining Latency Frames can be queried via BWAPI. To detect the turn size you need to calculate how many frames it takes until Remaining Latency Frames equals Latency Frames.

Application

Consider the scenario where a marine runs into a group of hostile units. The image below shows the exact moment a teal marine detected it is outnumbered and turned around to run away. The circles show the attack range (weapon max range) of the respective units.
As you can see, the teal marine is well within the attack range of the closer red marine and will therefore take hits.
[image loading]

Two factors are responsible:

1. If the teal marine issues a move command to run away the exact moment it spots enemy units within their weapon range the command will only be executed <remaining latency frames> later.
2. Furthermore, the teal marine will require a couple of frames to turn around (between 4 and 5 in this case)

So how do we make sure we run away in time?

We could of course just run away as soon as enemies are in sight range. But this will result in a very conservative behavior with many missed opportunities to get your own shots off.
Instead, we account for latency and frames required to turn as follows:

If (distance between enemy position and my position <= enemy weapon range) then run away

Becomes

If (distance between enemy position in <remaining latency frames + turn frames> frames into the future and my position <remaining latency frames> frames into the future <= enemy weapon range) then run away.

Ok, but where will each unit be x frames into the future?

For your own units you can just extrapolate along the current path.
For enemy units, there are two main approaches:
- Either assume they are walking directly towards you (conservative case)
- Or assume they continue their current direction at their current speed

The image below shows a scenario where the green marine accounts for 6 latency frames and 4 frames to turn. It therefore calculates the distance between where the teal marines will be 10 frames into the future and itself 6 frames into the future (since it turns on the spot).
[image loading]

The result is a very precise micro, where the green marine will remain just outside of enemy weapon range.

Disclaimer:
A word of warning: escaping is considerably harder than attacking. You need to avoid getting cornered and may have to take unexpected turns, allowing the enemy to catch up. It might be a good idea to introduce some safety margin to account for this.

Credits:
Thanks tscmoo for proof-reading
RadicalZephyr explained turns and latency well at https://github.com/RadicalZephyr/bwapi/wiki/Latency



*****
50 pts Copper League
Psyonic_Reaver
Profile Blog Joined June 2007
United States4337 Posts
March 11 2017 09:24 GMT
#2
Pretty soon we dont even need to play BW. It will play itself and there will finally be peace on earth.
So wait? I'm bad? =(
imp42
Profile Blog Joined November 2010
398 Posts
March 12 2017 02:53 GMT
#3
On March 11 2017 18:24 Psyonic_Reaver wrote:
Pretty soon we dont even need to play BW. It will play itself and there will finally be peace on earth.

it already plays itself 24/7 at https://www.twitch.tv/sscait
50 pts Copper League
Please log in or register to reply.
Live Events Refresh
Monday Night Weeklies
17:00
#31
RotterdaM1499
SteadfastSC318
IndyStarCraft 304
kabyraGe 166
BRAT_OK 134
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 1499
SteadfastSC 318
IndyStarCraft 304
BRAT_OK 134
Livibee 113
JuggernautJason76
UpATreeSC 57
StarCraft: Brood War
Britney 20727
Calm 2737
Dewaltoss 123
firebathero 123
Backho 56
NaDa 12
Dota 2
qojqva3358
Counter-Strike
fl0m5572
pashabiceps841
zeus726
allub185
Heroes of the Storm
Liquid`Hasu323
Other Games
Grubby4092
FrodaN2755
Beastyqt1010
ArmadaUGS127
C9.Mang0124
Sick97
Mew2King79
QueenE76
KnowMe60
Trikslyr52
Organizations
Dota 2
PGL Dota 2 - Main Stream280
Other Games
Algost 1
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• IndyKCrew
• sooper7s
• AfreecaTV YouTube
• Migwel
• LaughNgamezSOOP
• intothetv
• Kozan
StarCraft: Brood War
• FirePhoenix14
• Eskiya23 11
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 4167
League of Legends
• TFBlade1303
Other Games
• imaqtpie986
• WagamamaTV529
• Shiphtur274
Upcoming Events
OSC
2h 54m
Wardi Open
15h 54m
PiGosaur Cup
1d 4h
Replay Cast
1d 12h
Wardi Open
1d 15h
OSC
1d 16h
Tenacious Turtle Tussle
2 days
The PondCast
2 days
Replay Cast
3 days
OSC
3 days
[ Show More ]
LAN Event
3 days
Replay Cast
4 days
Replay Cast
4 days
Sparkling Tuna Cup
5 days
Replay Cast
6 days
Wardi Open
6 days
Liquipedia Results

Completed

SOOP Univ League 2025
RSL Revival: Season 3
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
SLON Tour Season 2
META Madness #9
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.