{"id":20025,"date":"2013-06-13T04:18:08","date_gmt":"2013-06-13T09:18:08","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20025"},"modified":"2013-06-13T06:34:49","modified_gmt":"2013-06-13T11:34:49","slug":"starcraft-2-rush-analysis","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=20025","title":{"rendered":"Starcraft 2: Rush Analysis"},"content":{"rendered":"<p>Hang on a second&#8230; <em>A Starcraft post filed under &#8220;programming&#8221;? What sort of organizational shenanigans are going on here, Shamus? Is this some kind of trick?<\/em> I&#8217;ll tell you: Last week I was pondering the mechanics of a Starcraft II rush. I wound up making the hilarious mistake of assuming it would be an easy thing to analyze with a bit of code.<\/p>\n<p>A &#8220;rush&#8221; in Starcraft is where you forego building workers, constructing defenses, or expanding. Instead you begin the game by building as many of the most basic army unit as you can in the hopes of crushing your opponent before they have anything on the field. A rush is usually considered an &#8220;all in&#8221;, because if your initial attack fails then you&#8217;ll be at a massive disadvantage. You don&#8217;t have many workers, or defenses, or expansions. All you&#8217;ve got is this small handful of units. Your opponent will be far more wealthy than you, and will be able to out-spend you over the next five minutes or so.<\/p>\n<p>It&#8217;s basically like <a href=\"http:\/\/en.wikipedia.org\/wiki\/Trick_play\">a trick play in NFL<\/a>. They&#8217;re rare because they&#8217;re risky, and the risk intensifies the more often you attempt them. It&#8217;s considered a lame, cheesy trick only used by the unskilled, the desperate, or (occasionally) the troll. There&#8217;s even a <a href=\"http:\/\/www.youtube.com\/watch?v=wyx6JDQCslE\">Sexy and I Know It<\/a> parody <a href=\"http:\/\/www.youtube.com\/watch?v=lskNTCzdLNE\">song about it<\/a> that makes fun of the notorious and meme-spawning <a href=\"http:\/\/knowyourmeme.com\/memes\/zerg-rush\">Zerg rush<\/a>:<\/p>\n<p><table class='nomargin' cellspacing='0' width='100%' cellpadding='0' align='center' border='0'><tr><td><iframe loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/www.youtube.com\/embed\/lskNTCzdLNE\" frameborder=\"0\" allowfullscreen class=\"embed\"><\/iframe><br\/><small><a href='http:\/\/www.youtube.com\/watch?v=lskNTCzdLNE'>Link (YouTube)<\/a><\/small><\/td><\/tr><\/table><\/p>\n<p>Trivia: The rapper (?) in that song is <a href=\"http:\/\/www.youtube.com\/user\/HuskyStarcraft\">Husky<\/a>, of <a href=\"http:\/\/www.youtube.com\/playlist?list=PLooJOgo-8bToEW-PRePfc0q2P8RqbTpev\">BRONZE LEAGUE HEROES<\/a> fame. <\/p>\n<p>Setting aside the jokes, the memes, the flame wars over game balance, and arguments over race: Just how much is the advantage of rushing, and how far behind will you be if you fail? What&#8217;s the damage, in a total numerical sense? How many units will you be ahead, and assuming there&#8217;s no game-ending engagement how long will it take for your opponent to pull ahead of you? I&#8217;m sure a pro player could intuit the answer just from the sheer volume of games they&#8217;ve played, but I wanted to see the breakdown on a chart where the rest of us mortals can visualize it. I so decided to write a little program to figure it out.  <\/p>\n<p><!--more-->The cost of everything is right there in the game: The cost of the buildings, the time to construct them, the cost of the units, etc. It&#8217;s just like calculating compound interest, right? Just plug the numbers in and see what it gives you.<\/p>\n<p>Well, no. <\/p>\n<p>About twenty minutes into the project I realized I&#8217;d greatly oversimplified the complexity of the problem. About two hours later I realized that even my twenty-minute re-assessment had been overlooking important details. What was supposed to be a quick, slapdash problem wound up taking the better part of a day and at the end I have a very strange bit of code and a graph that may or may not be useful.<\/p>\n<p>But before we look at the data, let&#8217;s look at how we get the data.<\/p>\n<p>For the purposes of our test, we&#8217;re going to be looking at a series of hypothetical Terran players. Zerg have to worry about larvae spawn rates and Protoss have more expensive units. In the former I&#8217;d have an extra layer of complexity to worry about that might obscure the thing I was trying to observe, and in the latter it would give our chart less granularity. So what we&#8217;re doing is making a program to simulate a Terran player that will create some number of workers, construct appropriate supply depots, and then build one or more barracks structures that will begin pumping out marines as fast as possible. Also note that in the parlance of the game a barracks is often called a &#8216;racks. Because two syllable words are for people with too much free time.<\/p>\n<p>Players identify early building strategies by how many workers you have when you begin building your production structures. So an 8-racks means you begin building your first racks once you have 8 workers on the field. (You actually need a supply depot first. I won&#8217;t get into it here, but for people familiar with the game I just want to note that I did take that into account.) A 10-racks means you build the barracks once you have 10 workers. You begin the game with six workers, so a 6-racks is the lowest you can go.  <\/p>\n<p>We keep the whole thing focused on simple barracks units. We ignore gas and we ignore expanding. This is complicated enough as it is. <\/p>\n<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/starcraft2_newbie_workers.jpg' class='insetimage'   alt='starcraft2_newbie_workers.jpg' title='starcraft2_newbie_workers.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>The first thing I needed was some basic numbers on how fast you can acquire minerals with a worker.  Like everything else in this project, this was less obvious than it seems. In the game, workers walk from the base to the minerals, spend a few seconds chipping away at them, and then walk back to base to deposit them in the &#8220;bank&#8221; where they can be spent. Repeat. This means that harvesting from nearby mineral patches is very slightly faster than harvesting from distant ones. If another worker is already harvesting from the desired mineral cluster, the worker will move to a free one. If there aren&#8217;t any free clusters available, it stands in line and waits its turn.  This means that there&#8217;s some diminishing returns when you add more workers, and at some point more workers will do nothing to improve the yield. <\/p>\n<p>This is really messy and involves way more complexity than I want in my program. I&#8217;m not interested in re-creating the precise movement mechanics of the game itself, and attempting to do so would run the risk of <a href=\"?p=1200\">oversimulation<\/a>. I just want a timer that counts seconds and minerals. I don&#8217;t want to have to simulate walking workers and worry about base layout and AI pathing. <\/p>\n<p>Thankfully, someone else did the research for me.  <a href=\"http:\/\/www.teamliquid.net\/forum\/viewmessage.php?topic_id=140055\">This post on Team Liquid<\/a> gives us the breakdown on how fast the minerals come in. The yield between near and far minerals is narrow enough that we can just use the average. Without getting into the details (read the post if you want the full breakdown) the first 16 workers will each harvest 42 minerals a minute. The next 8 will only yield an additional 18 per minute each. Further workers do nothing but stand in line. So workers 6-16 make 42 <acronym title=\"Mineral Per Minute\">MPM<\/acronym>. Workers 17-24 make 18 MPM. Workers 25 and above will not speed up yield. (Although, they might still be useful. Workers are needed to make supply depots and barracks, and worker #25 could do that building so the other 24 can keep on mining. This would be a very small gain and would a take a while to pay for itself.)<\/p>\n<p>The first question that seems obvious (but isn&#8217;t!) is: <em>How many barracks do I build?<\/em>  If I&#8217;m doing a 9-racks, do I need to build one bunker? Or two? Three? If I build too few, then money will pile up and I won&#8217;t be as strong as possible. If I build too many, then they&#8217;ll sit idle most of the time and the resources spent on the barracks could have just been spent making more marines. <\/p>\n<p>A barracks makes a marine every 25 seconds. Which means a single barracks produces 2.4 marines a minute. A marine costs 50 minerals. So it costs 120 minerals a minute to keep a barracks working at capacity. If I&#8217;m doing a 10-racks then I&#8217;ll have 10 workers who will bring in 420 minerals per minute.  That&#8217;s enough to keep 3.5 barracks going. Hm. So what the AI (it does seem less like I&#8217;m writing numerical analysis and more like I&#8217;m writing AI at this point) will end up with is 4 total barracks. The first three will work continuously and the fourth one will be idle about half the time. <\/p>\n<p>But wait! WHEN do we build these barracks? Let&#8217;s say I&#8217;ve got 3 racks pumping out marines. My bank has grown up to 150 minerals, which is enough for another racks. Do I build another racks right now? The other three racks are busy now, but what if &#8211; a single game tick from now &#8211; all three racks finish their current production and their marines all pop out? If I just spend my money on a racks, then marine production will stall for some period of time. Is it better to lose a little output now so I can have more capacity later, or should I focus on making marines and ONLY make additional barracks when I have enough cash that building the barracks won&#8217;t interfere with marines? Keep in mind that doing the later is actually really complicated. A barracks takes ages to make. I&#8217;d need to look at income, look at how soon the current barracks will complete. I&#8217;d need to take into account if this is the final barracks, because it might be worth losing production to get a barracks up UNLESS it&#8217;s that last odd barracks that&#8217;s going to be idle half the time anyway. And what about&#8230; <\/p>\n<p>Wow. <em>This question is more complicated than the original question I was trying to analyze.<\/em> To keep this problem from getting away from me, I decide to simplify. I have it favor building barracks sooner at the risk of forgoing marine production in the short term.  I don&#8217;t know if this is optimal, but finding the <em>optimal<\/em> strategy could take bloody ages and I don&#8217;t think I need to.  As long as all of my tests use the same strategy we should have a pretty good apples-to-apples comparison. <\/p>\n<p>This is a lot of code. I write a system to run a theoretical base, tracking income, counting off units, and building structures. Then I run into the next problem.<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/starcraft2_scv1.jpg' class='insetimage' width='600' alt='This is what a 6-racks looks like. We&#8217;re two and a half minutes into the game and we&#8217;ve got our first marine on the field.' title='This is what a 6-racks looks like. We&#8217;re two and a half minutes into the game and we&#8217;ve got our first marine on the field.'\/><\/td><\/tr><tr><td class='insetcaption'>This is what a 6-racks looks like. We&#8217;re two and a half minutes into the game and we&#8217;ve got our first marine on the field.<\/td><\/tr><\/table><\/p>\n<p>As <a href=\"?p=19923\" title=\"A Newbie&#39;s Guide to Watching Starcraft \">I mentioned before<\/a>, supply is an important concept in the game. You need to build a supply depot for every 8 small units (workers or marines) on the field. A newbie player will forget to build depots until they run into the limit, at which point all production must halt until they can add a depot. It wouldn&#8217;t make for a good test if my program made this same mistake. So, when should you build a supply depot? Build it too soon, and you&#8217;ll tie up resources that should be spent on pumping out marines. Wait too long, and you&#8217;ll get supply blocked. Either way you can choke off production. <\/p>\n<p>Players usually eyeball it at first, and as they master the game the supply depots become a natural part of the rhythm of their build order. But since we need to maximize production to make this test work, we need to build our depots at exactly the right time. It takes 30 seconds to build a depot, so we need to begin building one 30 seconds <em>before<\/em> we need it.<\/p>\n<p>I don&#8217;t want to get too fancy with this, and this can get annoyingly cumbersome if I wanted to be precise. (If I have to start considering partially-built barracks and measuring the time left on current marines I could end up working on this for the next couple of days. It&#8217;s not hard code to write, but it&#8217;s murderously slow and fiddly to test and once again would complicate something I&#8217;m trying to keep as simple as possible.) Thankfully, the time to produce a marine (25) and the time to build a depot (30) are very close. I should be able to look at the number of barracks I have and throw down a supply depot when my supply is that many short of the limit. (Plus one, to give me a slight buffer. Building too soon is better than too late.) If the current supply limit is 19 and I have two racks going, then I should build the depot when  (marines + workers + 1) >= 17. <\/p>\n<p>This doesn&#8217;t actually work very well. In some cases, the 12-racks might out-perform (say) a 13-racks, both short and long term. That doesn&#8217;t make sense.  <\/p>\n<p>Eventually I realize that my program is <strong>still<\/strong> oversimplifying things. I see that in certain builds, the racks are spending a lot of time idle because the AI is broke. It&#8217;s built too many barracks and crippled its production. <\/p>\n<p>It turns out that marines do not cost 50 minerals. Oh, that&#8217;s what they cost you up-front at the barracks, but in the long run you also need to account for the depots you&#8217;ll be building.  A depot supports 8 marines, so each marine is 50 minerals <em>plus<\/em> 1\/8 of a supply depot.<\/p>\n<p>So how much does a depot cost? Well, up-front they cost 100 minerals to build, but remember that a worker must stop harvesting for 30 seconds to build it. (For the sake of my sanity, we&#8217;re going to ignore the time it takes the worker to walk over to the building site and walk back when it&#8217;s done.) So a depot costs 100 minerals plus it causes you to miss out on 24 units of mineral gathering. The total cost of a depot is 124, divided by 8 marines equals (rounding up) 16 minerals. So the cost of a marine is actually the base 50 plus this extra 16 of sunk costs for a total of 66.  <\/p>\n<p>Using this new value, I basically un-kink my production pipelines and all of the builds operate at near maximum capacity. I play a quick game of actual Starcraft 2, and the points of my 6-racks seem to roughly line up with the numbers I&#8217;m getting out of my program. As far as mineral values and marine counts, it&#8217;s all acceptably within bounds. I don&#8217;t guarantee my program is correct, but I&#8217;m reasonably sure the values make sense and the rules are being properly enforced. (It&#8217;s not spending itself into negative money or building units despite a supply block.) If there are flaws, they&#8217;re most likely inefficiencies in my AI and not in the simulation. The values we&#8217;re getting should be good enough for the purposes of our analysis.<\/p>\n<p>I have the simulator run various builds and save the unit counts to a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\">CSV file<\/a> that I can load into Excel. Here is the build for a 6-racks compared to a player that aims for a more reasonable team of 16 workers. <\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><a href='images\/chart_sc1.jpg'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/chart_sc1.jpg' class='insetimage' width='600' alt='chart_sc1.jpg' title='chart_sc1.jpg'\/><\/a><\/td><\/tr><\/table><\/p>\n<p>This is the first ten minutes of the game. Our green player was aiming for 16 workers and red was going for a cheesy 6-racks. We see that red gets their first marine at around the 2:20 mark. Green doesn&#8217;t get their first marine until a full minute later. Note that it would be foolish for red to charge green&#8217;s base right when marine #1 pops out. Green has 13 workers at that point. Workers are terrible in combat, but 13 of them should be able to swarm and kill a single marine with minor losses. <\/p>\n<p>But what if red waits another minute? By 3:20 red has four marines and green has zero. Right at this point in the game, green has finished one barracks and has a single marine in development. At the same time, green is building two more racks, one of which is nearly done and the other is only just started. Still, the rushing player has 4 marines, which should be able to mow down all the green workers. That&#8217;s a guaranteed win, right? <\/p>\n<p>It would be, except for this: <\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/chart_sc2.jpg' class='insetimage' width='600' alt='chart_sc2.jpg' title='chart_sc2.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>It takes anywhere from 30 seconds to a minute to cross the average two-player map. The rushed army of four sets out from their base at 3:20. But by the time they arrive it&#8217;s 3:50 and green has 3 units on the field. 4 vs. 3 is still a win for the rushing player in an open fight, but red is likely fighting through a choke point and up a ramp to get into the base. When the fight is over red will have one or two injured marines, and the 16 workers should be able to mop them up. Red&#8217;s reinforcements are streaming in, but the rushing player doesn&#8217;t have the production capacity to sustain it. Red has two barracks and green has three, with number four on the way. Green has more income and more barracks. There will be minor skirmishes for the next minute or so, at which point the defender will begin to pull ahead and the rusher will never catch up. <\/p>\n<p>You can think of production as a slider between aggression and greed. The sooner you attack, the more aggressive. The later you go, the more greedy you are. Greed will win over aggression in a long game. The trick is that you have to survive long enough for the greed to pay off.<\/p>\n<p>Here is a chart showing a bunch of different builds:<\/p>\n<p><table width='600'  cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><a href='images\/chart_sc3.jpg'><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/chart_sc3.jpg' class='insetimage' width='600' alt='Click to expand to some kinda readable size.' title='Click to expand to some kinda readable size.'\/><\/a><\/td><\/tr><tr><td class='insetcaption'>Click to expand to some kinda readable size.<\/td><\/tr><\/table><\/p>\n<p>One final little bit of data is the chart below, which I generated for debugging purposes but might be kind of interesting to the reader. It shows the efficiency of the simulated builds. <\/p>\n<table border=\"1\">\n<tr>\n<th>Build<\/th>\n<th>Marine Count<\/th>\n<th>Average Minerals<\/th>\n<th>Supply Blocked<\/th>\n<th>Production Wasted<\/th>\n<th>Barracks Built<\/th>\n<\/tr>\n<tr>\n<td>6&nbsp;Workers<\/td>\n<td>31<\/td>\n<td>51<\/td>\n<td>0:00<\/td>\n<td>1:57<\/td>\n<td>2 (2)<\/td>\n<\/tr>\n<tr>\n<td>8&nbsp;Workers<\/td>\n<td>38<\/td>\n<td>46<\/td>\n<td>0:00<\/td>\n<td>5:52<\/td>\n<td>3 (3)<\/td>\n<\/tr>\n<tr>\n<td>10&nbsp;Workers<\/td>\n<td>46<\/td>\n<td>61<\/td>\n<td>0:00<\/td>\n<td>1:48<\/td>\n<td>3 (3)<\/td>\n<\/tr>\n<tr>\n<td>12&nbsp;Workers<\/td>\n<td>51<\/td>\n<td>50<\/td>\n<td>0:00<\/td>\n<td>5:15<\/td>\n<td>4 (4)<\/td>\n<\/tr>\n<tr>\n<td>16&nbsp;Workers<\/td>\n<td>60<\/td>\n<td>60<\/td>\n<td>0:00<\/td>\n<td>4:14<\/td>\n<td>5 (5)<\/td>\n<\/tr>\n<tr>\n<td>24&nbsp;Workers<\/td>\n<td>63<\/td>\n<td>67<\/td>\n<td>0:00<\/td>\n<td>2:33<\/td>\n<td>6 (6)<\/td>\n<\/tr>\n<\/table>\n<ul>\n<li>Marine count is how many marines the player had on the field after ten minutes.\n<li>Average minerals is how much it had <em>in the bank<\/em>, on average.\n<li>Time supply blocked. Not a problem now, but during testing I had a lot of trial-and-error figuring out how to avoid this.\n<li>Production wasted is how many seconds the barracks were idle. If there are three barracks and they all sit idle for a single tick, that counts as three seconds of waste. This mostly happens in situations where the player has enough income to support 4.25 barracks and so barracks #5 spends 75% of its time idle.\n<li>How many barracks it built. (How many barracks it planned to build.)  These numbers really only apply in shorter tests.\n<\/ul>\n<p>Well, it was a lot of work and in the end we just confirmed what we already knew: Rushing only works because it&#8217;s used rarely.<\/p>\n<p>In a real game, people that fall to rushes are usually people who got a little too greedy.  Instead of getting out those first couple of marines (or other starter unit) they built upgrade facilities, expanded, and invested too much in technology and too little in defense. <\/p>\n<p>Given that this project was not properly thought out ahead of time, the source for this is pretty rough. Still, <a href=\"http:\/\/www.shamusyoung.com\/files\/SCV_Ready.zip\">the source code<\/a> is available if you want to mess around with it. It&#8217;s a handful of class definitions all crammed into a single .cpp file, because I find it obnoxious when people hand out tiny and self-contained 500 line programs spread over three modules and header files. However, if this project was going to grow beyond this point the next order of business would be some kind of multi-file organization. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hang on a second&#8230; A Starcraft post filed under &#8220;programming&#8221;? What sort of organizational shenanigans are going on here, Shamus? Is this some kind of trick? I&#8217;ll tell you: Last week I was pondering the mechanics of a Starcraft II rush. I wound up making the hilarious mistake of assuming it would be an easy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[75],"class_list":["post-20025","post","type-post","status-publish","format-standard","hentry","category-programming","tag-starcraft"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20025"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/20025\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}