-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from dysonreturns/docs-tut8
docs: adding tutorials 7 and 8 along with some refactors on existing …
- Loading branch information
Showing
17 changed files
with
1,002 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# TUTORIAL 7 | ||
|
||
# Terran | ||
|
||
<div class="docstring"> | ||
<div class="note"> | ||
<strong>Glossary</strong><br/> | ||
<br/> | ||
<strong>Expo</strong>: Short for "expansion". A location where you would- or already have established a base.<br /><br/> | ||
<strong>Natural Expansion</strong>: The most likely first expansion you would take. Typically it's nearest to the main base, while also separating your main and your enemy.<br /> | ||
<br/> | ||
</div> | ||
</div> | ||
|
||
## Rallying | ||
|
||
All of our new army units are piling up around the Barracks. | ||
We can set rally points for structures to make emerging units go a target position. | ||
Let's rally all the Barracks to the entrance of our base. | ||
|
||
Once we have a few units in hand and we'll send them across the map. | ||
|
||
### Where to go | ||
|
||
For a rally point, let's use our natural expansion location and then take a few steps forward in the direction of our enemy's base. | ||
This way, the army isn't blocking ourselves when we decide to expand and defends our entrance. | ||
|
||
#### Expansion points | ||
The first step is finding our natural expansion. | ||
`geo.expansion_points` holds a list of all expansion points (where you can build a base structure). | ||
We'll learn more about `geo.expansions`, `geo.expansion_points` and `geo.expansions_unoccupied` later. | ||
For now, here is some code you can run to visualize these points in-game: | ||
|
||
```ruby | ||
def on_step | ||
#... | ||
|
||
# Debug: Draw spheres and labels for each expansion point | ||
geo.expansion_points.each do |point| | ||
point = Api::Point[point.x, point.y, geo.terrain_height_for_pos(point)] | ||
debug_text_world("x: #{point.x}, y: #{point.y}", point: point) | ||
debug_draw_sphere(point: point) | ||
end | ||
end | ||
``` | ||
|
||
#### Natural position | ||
|
||
From these points, we can detect which one is at our natural using a simple distance check: | ||
|
||
```ruby | ||
geo.expansion_points.min_by(2) { |p| p.distance_to(geo.start_position) }.last | ||
``` | ||
|
||
Each expansion point is compared our start position (`geo.start_position`) and the nearest 2 are picked using a `Array#min_by`. | ||
Of these two points, the first will be our main base's position and the second nearest is the natural base. So, we pick the latter with `.last`. | ||
|
||
You can get the distance between any two things using `distance_to`. This is true for both Units and Positions. | ||
|
||
#### A step in the right direction | ||
|
||
From the natural we wish to move a few squares, say `8`, in the general direction of our enemy. | ||
|
||
Both `Point` (3D) and `Point2D` share a module `Sc2::Position`, which provide geometry helpers such as `towards` (and `distance_to` we used above). | ||
`towards` takes the current position and moves it by a `distance` in the direction of an`other` position. | ||
|
||
```ruby | ||
new_position = one_position.towards(another_position, distance) | ||
``` | ||
|
||
By using a relative method like `towards`, this equation works regardless of which side of the map you get spawned. | ||
|
||
⌖ Explore {Sc2::Position}'s methods first when dealing with such matters as positioning. Auto-complete will guide you too. | ||
|
||
Let's see everything in action: | ||
```ruby | ||
def on_step | ||
# ... | ||
|
||
# Get a rally point and save it, since it's expensive. | ||
if @army_rally_point.nil? | ||
# Calculate our natural's position | ||
natural_expansion_pos = geo.expansion_points | ||
.min_by(2) { |point| point.distance_to(geo.start_position) } | ||
.last | ||
|
||
# 8 squares forward from the natural, in the direction of our enemy | ||
@army_rally_point = natural_expansion_pos.towards(geo.enemy_start_position, 8) | ||
end | ||
end | ||
``` | ||
|
||
#### Set the rally | ||
|
||
```ruby | ||
def on_step | ||
# @army_rally_point = nat... | ||
|
||
barracks.each do | barrack| | ||
barrack.smart(target: @army_rally_point) unless barrack.rally_targets.size > 0 | ||
end | ||
end | ||
``` | ||
|
||
We loop over the barracks and if they don't have a `rally_targets` set, we set one. | ||
|
||
If you look at full exercise, there is debug code which prints spheres at all the location points with their coordinates. | ||
It also marks out our rally point and it's coordinates. | ||
[07_terran_attack.rb](https://github.com/dysonreturns/sc2ai/blob/main/docs/examples/07_terran_attack.rb) | ||
|
||
|
||
![rallly_point](images/07_rally_point.jpg) | ||
### Attack | ||
|
||
Add the code below if you've been following along or check out and run [07_terran_attack.rb](https://github.com/dysonreturns/sc2ai/blob/main/docs/examples/06_terran_army.rb). | ||
|
||
After assembling a small army of, say, a dozen units, send everyone to attack to enemy's start position: | ||
|
||
```ruby | ||
def on_step | ||
#... | ||
# barracks.each do rally... | ||
|
||
# As soon as we have a few units, attack that enemy's starting position | ||
if units.army.size >= 12 | ||
units.army.attack(target: geo.enemy_start_position) | ||
end | ||
|
||
end | ||
``` | ||
|
||
Look at those little guys go! | ||
Sometimes the enemy dares to send a few units across to our base, but we are read and waiting at the entrance. | ||
|
||
Run the simulation a few times to see the varied outcomes. | ||
There are some obvious things we can fix to improve our chances. | ||
|
||
--- | ||
|
||
The Barracks enabled a tech upgrade to our Command Centre and we haven't used the Tech Lab for upgrades yet. | ||
Let's get the upgrades and see our new tools in action. | ||
|
||
--- | ||
|
||
{file:docs/TUTORIAL_07.md Next ➡️} | ||
|
Oops, something went wrong.