• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 21:01
CET 02:01
KST 10:01
  • 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
[ASL20] Finals Preview: Arrival10TL.net Map Contest #21: Voting10[ASL20] Ro4 Preview: Descent11Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9
Community News
Merivale 8 Open - LAN - Stellar Fest3Chinese SC2 server to reopen; live all-star event in Hangzhou22Weekly Cups (Oct 13-19): Clem Goes for Four3BSL Team A vs Koreans - Sat-Sun 16:00 CET10Weekly Cups (Oct 6-12): Four star herO8
StarCraft 2
General
Could we add "Avoid Matchup" Feature for rankgame RotterdaM "Serral is the GOAT, and it's not close" Chinese SC2 server to reopen; live all-star event in Hangzhou The New Patch Killed Mech! Weekly Cups (Oct 13-19): Clem Goes for Four
Tourneys
Crank Gathers Season 2: SC II Pro Teams Merivale 8 Open - LAN - Stellar Fest $5,000+ WardiTV 2025 Championship $3,500 WardiTV Korean Royale S4 Tenacious Turtle Tussle
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 497 Battle Haredened Mutation # 496 Endless Infection Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment
Brood War
General
ASL Runner-Up Race Stats ASL20 Pre-season Tier List ranking! [ASL20] Finals Preview: Arrival Is there anyway to get a private coach? BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[ASL20] Grand Finals ASL final tickets help [ASL20] Semifinal A Small VOD Thread 2.0
Strategy
Soma's 9 hatch build from ASL Game 2 Simple Questions, Simple Answers Roaring Currents ASL final Relatively freeroll strategies
Other Games
General Games
Stormgate/Frost Giant Megathread Path of Exile General RTS Discussion Thread Nintendo Switch Thread Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
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
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
Things Aren’t Peaceful in Palestine US Politics Mega-thread Russo-Ukrainian War Thread YouTube Thread The Chess Thread
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Korean Music Discussion Series you have seen recently... Movie Discussion!
Sports
Formula 1 Discussion 2024 - 2026 Football Thread MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023 NBA General Discussion
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
The Benefits Of Limited Comm…
TrAiDoS
Sabrina was soooo lame on S…
Peanutsc
Our Last Hope in th…
KrillinFromwales
Certified Crazy
Hildegard
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1310 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
Next event in 10h 59m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
PiGStarcraft495
Livibee 74
Nina 9
StarCraft: Brood War
Artosis 853
NaDa 23
Dota 2
monkeys_forever478
XaKoH 348
NeuroSwarm47
Counter-Strike
Stewie2K306
Super Smash Bros
AZ_Axe274
Mew2King52
Heroes of the Storm
Khaldor183
Other Games
summit1g8265
FrodaN4517
Grubby2560
JimRising 299
Skadoodle215
Maynarde153
ViBE127
KnowMe125
Organizations
Other Games
gamesdonequick1394
BasetradeTV30
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• Berry_CruncH84
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota21151
League of Legends
• Doublelift4622
Upcoming Events
Wardi Open
10h 59m
CrankTV Team League
11h 59m
Streamerzone vs Shopify Rebellion
TBD vs Team Vitality
Monday Night Weeklies
15h 59m
Replay Cast
1d 8h
WardiTV Invitational
1d 10h
CrankTV Team League
1d 11h
BASILISK vs TBD
Team Liquid vs Team Falcon
Replay Cast
2 days
CrankTV Team League
2 days
Replay Cast
2 days
The PondCast
3 days
[ Show More ]
CrankTV Team League
3 days
Replay Cast
4 days
WardiTV Invitational
4 days
CrankTV Team League
4 days
Replay Cast
5 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

ASL Season 20
WardiTV TLMC #15
HCC Europe

Ongoing

BSL 21 Points
CSL 2025 AUTUMN (S18)
BSL 21 Team A
C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
CranK Gathers Season 2: SC II Pro Teams
Eternal Conflict S1
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025

Upcoming

SC4ALL: Brood War
YSL S2
BSL Season 21
SLON Tour Season 2
BSL 21 Non-Korean Championship
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
META Madness #9
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
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.