what is going on everybody and welcome
to part 6 of the Starcraft 2 AI Series
in Python what we've been doing up to
this point is defeating the easy and the
medium AI which both were easy and
medium I would say is not medium we
could not defeat the hard AI but given
how simply we defeated the easy and
medium AI just by simply creating some
fighting units
I think defeating the heart AI is
probably not going to be too difficult
let's get into it but before we do quick
shout out to my most recent sponsors
googlebot deepak samir florian jo Wow
Tyler Durden Faust and Nick bender thank
you guys very much for your support if
you didn't know you can support the
channel by clicking that beautiful blue
button lookit look at it
you just gotta click that button thanks
again y'all for your support and let's
get into it so at least looking at this
there's a few major things that we that
we have going wrong so first of all this
bot has no notion of time like how much
time has elapsed in the game so for
example when the game immediately starts
our bot has a goal of creating 3 nexuses
or command centers whatever you want to
call them you know that that might be a
worthy objective to have just
immediately but do you really want to
build as that are those 3 Nexus is gonna
be your primary objective right are you
gonna build 3 nexuses before you build a
single military unit maybe but unless
that's your goal you need to focus on
that also the same thing here or do we
want to build three gateways right away
or do we just want one gateway and then
take those resources because the
gateways are our version of offense or
defense for our base as opposed to say
resource collection do we want 3
immediately or should we take some of
those resources and start building an
army also 3 is pretty good for the first
few minutes but what if the match for
whatever reason gets us to say 11 12 13
14 15 minutes three gateways exactly
to be enough probably not our enemies
probably got many more
you know unit producing buildings and
eventually it's just they're just gonna
suffocate us so a fixed number doesn't
make any sense this needs to be a
variable and probably dependent on how
much time has elapsed so that's another
thing or you could make it a variable
based on how many resources you have but
this fluctuates so much you could maybe
do like a resource rate or a variable
based on how many workers and game or
how many workers and nexuses you have or
something I'm gonna do time next same
thing here how many units is it gonna
take for us to attack initially 15 units
is a lot to send to an enemy base or if
an enemy was to send 15 units to you but
later on in the game especially 15 of
these little based stalkers no that's
not enough so so we need to figure that
one out and then finally the other major
issue is that as we produce new units
they kind of run towards the enemy and
at least in this case with even three
you you have a scenario where ones may
be dying ones on its way
ones also on its way and we just
continually just send new units up one
by one though they're not clustered they
need to come together before they go
that's another issue not really gonna
solve that issue here but just know that
is an issue so what we're gonna do first
is give this bot a notion of time so
coming up here basically you see on this
on step there's an iteration we can use
that iteration to our advantage now we
could just arbitrarily say if the
iteration is greater than X or whatever
then that would give us some sort of way
to make rules around this but I think at
least for me I want to think in terms of
minutes so at least for what I checked
in iteration there's about 165
iterations per minute
I have again no idea what an iteration
is if that's like a game tick or if that
is just how quick your processors are I
really don't know so you'll want to
check that
for yourself but that's what I found I
found it to be 165 or very close to 165
I didn't there's no empirical testing
that went on there so what we're gonna
do is we're gonna define an
initialization method here and basically
yourself all we want to do is define a
constant here four iterations per minute
just so basically you want your bot to
always be important
okay so and I'll show you why hopefully
at the end here if this video doesn't
make it to like 6,000 hours long we'll
probably end up copypasta inning a
little bit off of the text base tutorial
just so I can get through everything
because never mind anyways let's go so
what we need to do is self dot
iterations per minute it's 165 now we
can reference self dot iterations per
minute everywhere everywhere in our
script so now what we're going to do is
on step we also want to track where's
the current where are we currently so
self dot iteration iteration equals
iteration just so we can access this
everywhere in all of these methods and
then we can start building things with
that in mind so the first thing that I
want to do is like building assimilators
like pylons and assimilators we just if
we don't have the supply builds and
pylons right pretty simple stuff and
actually our pylons that was another
area of major issue our pylons should be
much more intelligently built anyway so
really and expand I'm gonna leave that
the way it is I'll hopefully I'll
explain later
to a little more or at least show you
guys but at the end of the day this
logic despite not being like fully
logical before we can afford a nexus we
can afford other things so it's only at
the point where we have full queues
basically or something's already being
built that we expand onto a noon like to
a new Nexus so it just kind of works out
you'd probably want to put some logic in
there but
it just doesn't matter because of the
cost of things but anyway so the first
thing we want to focus on is offensive
force buildings because while we only
need one theoretically probably good
good idea to have like two cybernetics
cores maybe because then if you just
have one and someone destroys it you're
in trouble
but anyways most people want to focus on
the Gateway so if the Len of gateways is
less than three I proposed we'd like to
have one gateway per minute so at the 9
minute mark
it'd be nice to have nine gateway he's
right and continuing on so about once a
minute I'd like to have a new gateway so
rather than being less than three let's
just say I want to know if is that less
than self dot iteration the iteration
divided by self dot iterations per
minute so if and this is just this is
super approximate okay it's not going to
be perfect but from all the games that
I've watched it seems to be about 165
seems to it does what I hope it to do
basically so so that's the first thing
that we're gonna we're gonna add there
the next issue that we have or if games
go long enough is build workers so in
this case as long as the we have a nexus
that has no queue we're building workers
and initially that's no big deal but
eventually that's kind of a big deal
because we're going to like generally
it's our military units that get
destroyed first our military units are
more expensive and so eventually we end
up having too many workers and they kind
of suffocate out the ability for us to
have military units so what we need to
do is have some limitation on this so I
proposed just we have like a max workers
so self dot whoops self dot max
underscore workers equals and for now
I'm gonna say fifty I saw a comment
someone said like usually if games last
long enough it's more like eighty we
could go somewhere in between I have
tested mostly fifty but we could go
seventy chances are most of the time the
game
at least versus these a eyes will end
long before you get to those numbers but
we can do some let's do let's do 70 I'll
go I'll go 74 now so now or if we really
want it to be right in between 65 okay I
know everybody was really worried about
that so now what we're gonna do for
build workers is rather than if we have
so so not only do we want to have a max
workers but we also kind of want to have
like a maximum mental workers per Nexus
so what I'm gonna do is I'm gonna say
this will be a long one so for Nexus in
units really we're just gonna add a big
line on top of that so we're just going
to say if the Len of self dot units if
lens self dot units Nexus Nexus x we're
gonna say 16 is if that number is
greater than Len whoops the Len of self
dot units if that's greater than our
probes right then we're fairly content
and I'm trying to decide if I really
want and really I'd like to put this all
on one line but I know sublime linor
it's gonna be like ah it's too long of a
line man so we'll go down here in the
and you can either throw in the and/or
satisfy Pepe whatever you want to do if
if Len so really now we just want to ask
is that less than self dot max workers
if both of those are the case now we're
gonna build the workers otherwise let's
not do that we don't want to run out of
workers next I think for expansions we
we could modify this again it shouldn't
be an issue but in the later game you
may want to make that expansion
and the way that we could do that I'm
this one I am going to copy/paste
because this one isn't as essential but
again it's very similar to what we've
seen before so if it's less than how
many minutes so one per minute if we can
afford one let's go ahead and make that
expansion eventually it's kind of silly
to have like nine expansions really you
probably don't want to do any more than
three I think that might be a mistake
then the next thing is offensive force
buildings so like I was saying before
you you want to have more than what we
have but the other thing too is if the
game goes long enough and really
offensive force but buildings has to be
referenced first if the game goes a
little long enough we don't want to
continue using our stalkers we'd like a
better unit now you can upgrade your
stalker with a thing called blink and
that makes the stalkers much much better
I have no idea how to control that blink
capability with piety - I haven't tried
to figure it out I just knew that I
don't know how to do that so I'm sure
there's a way to do it I just don't know
how but but don't take that as you can't
do it if you want to add that capability
to the stalker and see how you do with
that upgrade go for it otherwise I'm
going to add in the void ray as one of
our other units so we've got stalker and
to build the void ray we're going to
need a Stargate and then you can build a
void right so the void Ray's like this
it's a flying ship and it shoots out
this like beam and it does just tons of
damage now according to the
documentation for the game the void ray
is a weak unit I haven't seen that to be
the case and I think it's just because
the void ray does so much damage so
quickly that it kind of like balances
out pretty well so the void ray is a
pretty good pretty good unit in my
opinion of playing Starcraft for about a
week my expert opinion so I think that's
a pretty good
pretty good unit to have also it flies
so it doesn't like get stuck on rocks
like a lot of times right before
someone's base there's like a clump of
rocks that again I'm sure there's a way
in the in the API to find those things
and destroy them so you can move more
units in quickly to an enemy base I just
didn't bother so so with void ray you
just fly over that stuff right and it
makes it a good unit because that unit
can also like a lot of times you're
fighting AI and like let's say the AI
has flying and ground units but you're
losing the battle well you could retreat
and now all you have to fight is the
flying units you don't even have to
worry about the ground units because you
could just retreat over a Ridge or
whatever and they can't get to you so
you can split up enemy armies really
well especially if you if you're all
flying units okay so I'm trying to side
if I really want I think I'm just gonna
put copy and paste this one in just just
because I don't think like there's
really nothing new here so let's change
let's see so let me take the offensive
force method copy yeah this is our
buildings I'm just gonna paste this one
in so here I'm just this was just for
debugging purposes I just wanted to see
that it was generally right because
there is a little game clock so I wanted
to see if that was close right so if a
gateway already exists and we don't have
a cybernetics core if we can afford one
and we're not already building one we
build a cybernetics core same as what
we've seen before and all this requires
a pylon to exist then so if this
statement is true then we're gonna build
the cybernetics core if not we're going
to build the Gateway so this would not
be true if a gateway didn't exist so
Elif build the Gateway so long as that's
less than half of the minutes so so in
this case so just a moment ago what we
had was we wanted to have as many
gateways as we had minutes into the game
now we want to do half of that because
we're gonna be building both a gateway
and a Stargate and eventually that's
just way too many buildings so so we're
gonna go with this number instead
so half of that so at night or si8
minutes we should have four gateways and
four stargates okay so this is just for
the gateway stuff right then coming down
here and sort of I mean because you do
need the cybernetics core and all that
but anyways coming down here if we have
the cybernetics core already and we
don't have enough stargates if we can
afford a Stargate and one's not already
pending we want to build a Stargate okay
so once we have the Stargate we actually
have to we need to add that into our
offensive force so again I'm just gonna
copy and paste this one in looks like
I'll have to fix it in the text-based
tutorial it's a the tabbing isn't quite
right but there we go so now for gateway
in there we're gonna build stalkers as
many as we can our goal here is just to
make because the stalker is gonna be
quicker and cheaper and everything to
build then avoid ray we want to keep the
the numbers at least for now we just
wanna have some sort of control
otherwise the stalker is just always
gonna suffocate out the void ray because
it's cheaper to build and quicker so
we're just gonna wind up only building
stalkers so that's why I wanted to say
hey as long as the stalker amount isn't
greater than void ray otherwise we're
gonna come down here and then we're just
gonna build void Ray's would just give
the void ray the priority anyways the
way things are gonna shake out is it'll
be about even so once we've done that
the only thing that we really need to
modify is the attack method because
right now it just does stalker I want to
say there's like four I think you can do
like some sort of work or unit in like
that you could do like self dot workers
or something on in fact I think it's
even up here so you could either
reference your probes directly or you
can reference workers let's see if we
have one though build workers worker I'm
not seeing it here okay yeah so so so
self dot do we're on this is selecting a
build worker
there is a way to select like just a
worker in general so you don't have to
know the unique name and I'm guessing
you could do like some sort of fighting
unit general selection as well again I'm
sure it exists I just didn't know it so
I didn't use it so now I'm gonna copy
this other method in for the attack and
now let me know down below if you guys
so like what I've been trying to do
lately in tutorials is for like new
concepts or something we I always try to
still write that out by hand but for
things that we've already covered I
don't really see a value in it so I've
been doing this where like I'll just
copy and paste in just kind of explain
it let me know what you think of that
would you rather me still continue to
write it all out by hand or we're not so
we can cover so much more like this this
tutorial would have taken probably three
or four videos whereas I'm gonna be able
to knock this out one video anyway
carrying on so what I did here is I
created a you know just like a little
dictionary and then in here we have the
objects and then two numbers in the list
here I just to denote taking here this
is the number to fight so like the
number to like go and look for a fight
and then the number required to defend a
fight and both of these numbers are just
we should really tweak them quite a bit
like five and three is probably not
really that great probably should do
like three or something like that and
then have some sort of clumping like it
needs to be three in a closed group
because it's too often it because in
this case what I've found it's it's like
three anywhere on the map so then they
go and attack but they attack one by one
and they just go to slaughter basically
it's so that would that's something
you'd want to fix anyway all we're doing
is we just iterate through the
aggressive units if they're not idle
ball the law they find the target it's
pretty simple so like we're just
iterating through here and then
performing the same logic that we had
before if we have enough units to attack
just like find a target well then we're
gonna look for a target otherwise if we
just have enough units to defend well
then we're going to
and that should be enough so let's go
ahead and run that probably hit some
sort of error so we'll wait for the
lovely lovely error that I know I'm
gonna hit and we're gonna fight
Taron some people got a little irritated
at my pronunciation of Tehran so now I'm
gonna have to just kind of like it's
kind of like epochs and epochs make this
a little bigger can I give me this there
it is
full screen oh shoot I don't know if we
have a real time I guess we're gonna
find out really quick this game is
loading slow as heck come on Mike it
looks like yeah okay we're going pretty
quick no airs yet this is incredible
we're building things it's the it's the
glory of copypasta really it should we
shouldn't have any airs
okay so Alissa I think this is our start
no that's our cybernetics core see if
we're building there come here comes our
Stargate also I am aware of the cannon
rush strategy everybody thank you I'm
aware it exists and we'll talk about
that in a minute
also I found out so this is your little
workers you could nor it like if you're
a plane you can press f1 or f2 to select
all of your military units oh my gosh
we've been rushed Oh No we're probably I
guess we might not win this one but
we'll see oh we destroy I love these
void rays that are just they're just so
insane anyway so you can see what you
have in your entire military just by
pressing f2 or you could just look here
and see there's ten of them but you can
see like okay this is a relatively
balanced I'm assuming we're building
some void rays right now
was this
oh man they came in with a big army
uh-oh Oh bad Oh No ha ha oh we might
actually lose for the record this script
is fully capable of winning it's not
going well though looks like we're about
to if we lose that Nexus let me restart
it I guess oh no she just edited out of
the video an act like that never
happened so we are playing difficulty
hard by the way that was just
automatically changed uh embarrassing
trying to think if there was anything I
didn't change Oh anyway we're back
playing hopefully they won't I think our
issue that time was they rushed us and
we just were not prepared for that
moving forward there's a few things I
want to show you guys first is first of
all at least in my testing I'm able to
defeat the heart AI here at least eight
out of ten times like most of the time
I'm able to defeat them and then I was
able to defeat both Zerg and also
fighting Protoss as well okay and still
like eight out of ten
Zerg was probably our worst maybe more
like six seven out of ten times and then
once you get to that point the next
thing that we could do looks like we got
three Nexus is we already got five
military units hopefully if they don't
rush this time like by the time we start
getting up the right amount like once we
have enough resources to keep building
these buildings I don't think they'll
beat us but if we get rushed we we tend
to lose looks like we're doing pretty
good though oh no we got rushed again
yeah this guy well you're not gonna get
very far because these void rays are
just nuts anyway coming back over here
to the to the actual package that we're
using if you check out the examples
directory they've got like Protoss
Tehran and Zerg all in here and each of
these has examples of common strategies
so the cannon rush just three bays void
ray just dominance I think of some of
the other ones these ones I don't
actually know too much about I've only
just started looking at Oh looks like
we're about to see yeah yeah we got well
in the 20s and we're producing more than
we're losing at least at the moment okay
as game over see ya bye
aren't these void the void arrays just
look absolutely just it looks so much
better than these stalkers I just love
the little blue yeah bye-bye
no I don't really feel like accepting
here
I want to destroy you completely for
last match anyway okay so we can defeat
Tehran so the next thing is to check out
these and you can import these and also
you know people were asking like can you
do bot versus bot and all that kind of
stuff and like they were curious about
you know could we have like players
compete against each other and all that
and if you guys want to create BOTS and
and all that I'd be happy to showcase
your BOTS and go through them we were
we're hoping to do some sort of like
tournament type system but we'll see
there's a lot of hardships involved in
that one
there's also sc2 AI dot net I'm kind of
the same story I've been telling this
whole time I there is a way to submit
things into here I just don't know it I
haven't spent enough time looking into
it it also looks like maybe they're
mostly C++ BOTS I'm not really sure
anyway it exists now let's see oh the
other thing I was gonna show you guys
those like I was saying you can import
other BOTS so what I've done is since I
cloned that directory so I close the you
know the package that we're using and
then here are here are those BOTS so for
example we could bring in Protoss and
let me just show you can and rush
because we're we're gonna get just
dominated well we're not gonna get
dominated but we're not gonna handle
cannon rush very well so what we can do
is we can come up here and we can so so
this tutorial actually shoot I'm not
sure if this tutorial is in the right
directory for me to do this see where
are we so maybe I let me just copy and
paste it into tutorials then okay so my
script right now like we're working
right here and here's the examples
directory so so what we can do to to
compete against either these example
BOTS or bought other bots that we make
or we want to compete like two BOTS
versus each other you can do something
like from
examples dot Protoss dot and let's do so
examples Protoss and then let's do
cannon rush so I'm just going to copy
this dot cannon rush we want to import
the main class here cannon rush bot so
we're gonna do this import cannon rush
bot then what we do is come down to the
very bottom and rather than doing a
computer we're gonna do a bot and in
this case cannon Rush is a Protoss race
so we need to make sure we allow them to
be Protoss that would be rude of us to
not do that and we say ok this time we
want to do the cannon rush bot so now
let's run that up hitting err just
something up here are you gonna tell me
it doesn't it no examples dot pro 3 s's
it also is at lower cased it is lower
case but not 3 s's just two S's try
again also when you run to bots a couple
things let's see if it does it to me it
looks like it is so you run to bots 2
windows will pop up this time it
actually worked
sometimes it hangs on a black screen
it's super frustrating and if that
happens to you go to like documents
starcraft to open up that stock like my
you know go to your user My Documents
Starcraft 2 and then there's like a
variables text go to variables text and
I'm guessing this is yes so so they run
in here they build these pylons and then
they start building the cannons so like
in your actual base and because at this
stage my I don't have any fighters we're
not really now a couple fighters came in
but it's not enough it's too late so
let's go back into here I'm waiting for
one ok so here's one of their cannons so
then they just start putting these up
but we just don't have the firepower to
defeat any of these cans like our work
robots aren't going to be able to do it
now we have already expanded over here
so it doesn't totally
but the the problem at least with our
logic at the moment is we're gonna
continue coming over here because these
are known units and with this many
little canons we're never gonna defeat
that many canons so over time there's
just more and more and more of these
that are just gonna keep popping up now
both of these a ice should be greatly
improved like they know where we are
they know we're over here so this cannon
rush should now expand to over here
probably but they're not doing that also
for us as someone comes in like that you
should you should stop you should divert
all it like there's no good reason one
of the enemy workers would be at your
location so you want to you want to
divert all of your resources to going
after them pretty quickly
now other than that once they get them
placed the other thing you can do is you
can just pick up your your little base
and fly away
so that's doable as well but this cannon
rush bot isn't exactly the best the best
one I've ever seen they could do better
but basically the entire match is gonna
go this way now one thing they also keep
in mind is this they're all they're
doing is they just have little workers
right they're completely defenseless all
we need to do to really win this battle
would be first to come up here and just
destroy this nexus destroy this entire
area make a few more enemy like fighting
units and then just slowly defeat the
rest of this like it wouldn't be the end
of the world to defeat this it's just
you have to handle for it okay so I'm
gonna close out of these
and now so at at this point you know
what you're gonna do is you're gonna
start going through all these examples
and you're gonna start building cases
and handling for all these conditions
and eventually it just becomes kind of a
pissing contest over who can write the
most if statements to handle for the
most conditions and it starts to get
tedious at that point in my opinion by
all means have at it it's just I think
that makes for a fairly boring tutorial
so now we're gonna get more into doing
deep learning on this stuff now I just
want to stress there know deep learning
Bhatt is going to to outperform at
playing the game a rule-based bottom
probably not even a human player
especially if we fairly limit actions
per minute if we don't limit actions per
minute a fairly rudimentary bot could
probably beat a human player but but
even with deep learning it's pretty
difficult because there's just so many
variables here to consider but that's
what we're gonna start playing around
with is deep learning inside of
Starcraft 2 and probably like there's
just so many things that we could
optimize like there's just so many
hard-coded values that we've put in here
like but even just things like should we
expand should we not should we attack
should we not what kind of unit should
we build that kind of stuff is useful
and then also just yeah like at what
point should we expect
how many units should be mining at any
given time and like there's just so many
variables here that we can use deep
learning to to best at least pick those
values and then maybe you could put it
all together like a bunch of classifiers
coming together and regressors coming
together could could be pretty good but
anyways that's what we're gonna be
poking around with I personally I'd like
to build something that's like full AI
like all decisions are AI based I think
that would be cool but like I said that
doesn't currently exist there's nothing
that really can do that to any good
successful degree so don't expect that
we're going to come up with some sort of
you know
massive thing but it will be fun I think
so maybe anyway that's all for now
thanks again to my recent sponsors whoa
look at that button dang that looks
attractive you click it anyways I will
see you guys next time