• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 18:22
CEST 00:22
KST 07:22
  • 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
BGE Stara Zagora 2025: Info & Preview27Code S RO12 Preview: GuMiho, Bunny, SHIN, ByuN3The Memories We Share - Facing the Final(?) GSL46Code S RO12 Preview: Cure, Zoun, Solar, Creator4[ASL19] Finals Preview: Daunting Task30
Community News
[BSL20] ProLeague: Bracket Stage & Dates9GSL Ro4 and Finals moved to Sunday June 15th12Weekly Cups (May 27-June 1): ByuN goes back-to-back0EWC 2025 Regional Qualifier Results26Code S RO12 Results + RO8 Groups (2025 Season 2)3
StarCraft 2
General
The SCII GOAT: A statistical Evaluation what is flash bitcoin BGE Stara Zagora 2025: Info & Preview Magnus Carlsen and Fabi review Clem's chess game. Jim claims he and Firefly were involved in match-fixing
Tourneys
Bellum Gens Elite: Stara Zagora 2025 Sparkling Tuna Cup - Weekly Open Tournament SOOPer7s Showmatches 2025 Master Swan Open (Global Bronze-Master 2) $5,100+ SEL Season 2 Championship (SC: Evo)
Strategy
[G] Darkgrid Layout Simple Questions Simple Answers [G] PvT Cheese: 13 Gate Proxy Robo
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 477 Slow and Steady Mutation # 476 Charnel House Mutation # 475 Hard Target Mutation # 474 Futile Resistance
Brood War
General
Mihu vs Korea Players Statistics BGH auto balance -> http://bghmmr.eu/ BW General Discussion [BSL20] ProLeague: Bracket Stage & Dates Will foreigners ever be able to challenge Koreans?
Tourneys
[ASL19] Grand Finals NA Team League 6/8/2025 [Megathread] Daily Proleagues [BSL20] ProLeague Bracket Stage - Day 2
Strategy
I am doing this better than progamers do. [G] How to get started on ladder as a new Z player
Other Games
General Games
Armies of Exigo - YesYes? Nintendo Switch Thread Stormgate/Frost Giant Megathread What do you want from future RTS games? Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
LiquidLegends to reintegrate into TL.net
Heroes of the Storm
Heroes of the Storm 2.0 Simple Questions, Simple Answers
Hearthstone
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Vape Nation Thread European Politico-economics QA Mega-thread
Fan Clubs
Maru Fan Club Serral Fan Club
Media & Entertainment
Korean Music Discussion [Manga] One Piece
Sports
2024 - 2025 Football Thread Formula 1 Discussion NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread Cleaning My Mechanical Keyboard
TL Community
The Automated Ban List
Blogs
Cognitive styles x game perf…
TrAiDoS
StarCraft improvement
iopq
Heero Yuy & the Tax…
KrillinFromwales
I was completely wrong ab…
jameswatts
Need Your Help/Advice
Glider
Trip to the Zoo
micronesia
Poker
Nebuchad
Customize Sidebar...

Website Feedback

Closed Threads



Active: 21972 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 States4332 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
BSL: ProLeague
18:00
Bracket Stage: Day 2
HBO vs Doodle
spx vs Tech
DragOn vs Hawk
Dewalt vs TerrOr
ZZZero.O328
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
CosmosSc2 125
EnDerr 46
StarCraft: Brood War
Calm 2771
Rain 1402
Horang2 376
ZZZero.O 328
Artosis 228
Dota 2
NeuroSwarm55
LuMiX1
League of Legends
tarik_tv13207
Dendi1634
Counter-Strike
Stewie2K502
Super Smash Bros
C9.Mang012282
Mew2King70
Heroes of the Storm
Grubby3541
Liquid`Hasu511
Khaldor165
Other Games
summit1g7391
FrodaN774
JimRising 532
Mlord482
ViBE129
elazer107
Maynarde62
KnowMe37
Organizations
Dota 2
PGL Dota 2 - Secondary Stream5668
Other Games
gamesdonequick1307
BasetradeTV135
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 22 non-featured ]
StarCraft 2
• Berry_CruncH237
• musti20045 63
• Hupsaiya 62
• StrangeGG 45
• RyuSc2 36
• Adnapsc2 30
• Migwel
• AfreecaTV YouTube
• sooper7s
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
StarCraft: Brood War
• 3DClanTV 39
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota22124
• Ler152
League of Legends
• Doublelift6504
• Shiphtur288
Other Games
• imaqtpie2043
Upcoming Events
Wardi Open
12h 38m
Replay Cast
1d 1h
Replay Cast
1d 11h
WardiTV Invitational
1d 12h
WardiTV Invitational
1d 12h
GSL Code S
2 days
Rogue vs GuMiho
Maru vs Solar
Online Event
3 days
Replay Cast
3 days
GSL Code S
3 days
herO vs Zoun
Classic vs Bunny
The PondCast
3 days
[ Show More ]
Replay Cast
4 days
WardiTV Invitational
4 days
Korean StarCraft League
5 days
CranKy Ducklings
5 days
WardiTV Invitational
5 days
Cheesadelphia
5 days
GSL Code S
6 days
Sparkling Tuna Cup
6 days
Liquipedia Results

Completed

Proleague 2025-06-05
BGE Stara Zagora 2025
Heroes 10 EU

Ongoing

JPL Season 2
BSL 2v2 Season 3
BSL Season 20
KCM Race Survival 2025 Season 2
NPSL S3
Rose Open S1
CSL Season 17: Qualifier 2
2025 GSL S2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
ECL Season 49: Europe
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025
YaLLa Compass Qatar 2025
PGL Bucharest 2025
BLAST Open Spring 2025

Upcoming

CSL 17: 2025 SUMMER
Copa Latinoamericana 4
CSLPRO Last Chance 2025
CSLPRO Chat StarLAN 3
K-Championship
SEL Season 2 Championship
Esports World Cup 2025
HSC XXVII
Championship of Russia 2025
Murky Cup #2
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
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.