• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 12:07
CEST 18:07
KST 01: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
[ASL19] Finals Recap: Standing Tall9HomeStory Cup 27 - Info & Preview18Classic wins Code S Season 2 (2025)16Code S RO4 & Finals Preview: herO, Rogue, Classic, GuMiho0TL Team Map Contest #5: Presented by Monster Energy6
Community News
Flash Announces Hiatus From ASL55Weekly Cups (June 23-29): Reynor in world title form?13FEL Cracov 2025 (July 27) - $8000 live event18Esports World Cup 2025 - Final Player Roster16Weekly Cups (June 16-22): Clem strikes back1
StarCraft 2
General
TRUSTED USDT RECOVERY TECHY FORCE CYBER RETRIEVAL Statistics for vetoed/disliked maps The SCII GOAT: A statistical Evaluation Weekly Cups (June 23-29): Reynor in world title form? PiG Sty Festival #5: Playoffs Preview + Groups Recap
Tourneys
FEL Cracov 2025 (July 27) - $8000 live event RSL: Revival, a new crowdfunded tournament series Korean Starcraft League Week 77 Master Swan Open (Global Bronze-Master 2) [GSL 2025] Code S: Season 2 - Semi Finals & Finals
Strategy
How did i lose this ZvP, whats the proper response Simple Questions Simple Answers
Custom Maps
[UMS] Zillion Zerglings
External Content
Mutation # 480 Moths to the Flame Mutation # 479 Worn Out Welcome Mutation # 478 Instant Karma Mutation # 477 Slow and Steady
Brood War
General
Player “Jedi” cheat on CSL Replays question BW General Discussion Flash Announces Hiatus From ASL BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [BSL20] Grand Finals - Sunday 20:00 CET Small VOD Thread 2.0 [BSL20] GosuLeague RO16 - Tue & Wed 20:00+CET
Strategy
Simple Questions, Simple Answers I am doing this better than progamers do.
Other Games
General Games
Stormgate/Frost Giant Megathread Nintendo Switch Thread Path of Exile What do you want from future RTS games? Beyond All Reason
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
US Politics Mega-thread Trading/Investing Thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The Games Industry And ATVI
Fan Clubs
SKT1 Classic Fan Club! Maru Fan Club
Media & Entertainment
Anime Discussion Thread [Manga] One Piece [\m/] Heavy Metal Thread
Sports
Formula 1 Discussion 2024 - 2025 Football Thread NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023 NHL Playoffs 2024
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Blogs
Culture Clash in Video Games…
TrAiDoS
from making sc maps to makin…
Husyelt
Blog #2
tankgirl
StarCraft improvement
iopq
Trip to the Zoo
micronesia
Customize Sidebar...

Website Feedback

Closed Threads



Active: 692 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 States4336 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
FEL
16:00
Cracov 2025: Qualifier #1
CranKy Ducklings34
IndyStarCraft 31
Liquipedia
PSISTORM Gaming Misc
15:55
FSL team league: ASP vs PTB
Freeedom4
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
trigger 372
Hui .280
SC2Nice 15
StarCraft: Brood War
Calm 9558
Horang2 2731
Bisu 1917
Jaedong 1733
Flash 1466
Mini 975
Larva 967
firebathero 715
BeSt 582
actioN 383
[ Show more ]
hero 340
Mind 167
Hyun 139
sSak 55
GoRush 31
Mong 27
Rock 16
zelot 15
Dota 2
LuMiX2
Counter-Strike
fl0m89
Heroes of the Storm
Khaldor534
Other Games
Gorgc3903
singsing3045
B2W.Neo1311
FrodaN1205
Mlord348
Lowko325
Fuzer 293
TKL 164
KnowMe136
Trikslyr59
Organizations
Other Games
EGCTV1290
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• HeavenSC 31
• Kozan
• LaughNgamezSOOP
• AfreecaTV YouTube
• sooper7s
• Migwel
• intothetv
• IndyKCrew
StarCraft: Brood War
• Michael_bg 5
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2876
• WagamamaTV624
• Ler61
League of Legends
• Nemesis8360
Other Games
• Shiphtur52
Upcoming Events
RSL Revival
17h 53m
Clem vs Classic
SHIN vs Cure
FEL
19h 53m
WardiTV European League
19h 53m
BSL: ProLeague
1d 1h
Dewalt vs Bonyth
Replay Cast
2 days
Sparkling Tuna Cup
2 days
WardiTV European League
2 days
The PondCast
3 days
Replay Cast
4 days
RSL Revival
4 days
[ Show More ]
Replay Cast
5 days
RSL Revival
5 days
FEL
5 days
RSL Revival
6 days
FEL
6 days
FEL
6 days
Liquipedia Results

Completed

BSL 2v2 Season 3
HSC XXVII
Heroes 10 EU

Ongoing

JPL Season 2
BSL Season 20
Acropolis #3
KCM Race Survival 2025 Season 2
CSL 17: 2025 SUMMER
Copa Latinoamericana 4
Championship of Russia 2025
RSL Revival: Season 1
Murky Cup #2
BLAST.tv Austin Major 2025
ESL Impact League Season 7
IEM Dallas 2025
PGL Astana 2025
Asian Champions League '25
BLAST Rivals Spring 2025
MESA Nomadic Masters
CCT Season 2 Global Finals
IEM Melbourne 2025

Upcoming

2025 ACS Season 2: Qualifier
CSLPRO Last Chance 2025
2025 ACS Season 2
CSLPRO Chat StarLAN 3
K-Championship
uThermal 2v2 Main Event
SEL Season 2 Championship
FEL Cracov 2025
Esports World Cup 2025
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
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.