Skip to content

Commit

Permalink
feature: broadly, allows sc2ai new and sc2ai ladderzip
Browse files Browse the repository at this point in the history
A lot of refactoring and additions needed to allow lader play, including a templated start directory.

feature: Adding templates for sc2ai new and sc2ai ladderzip
feature: Paths#template_root for template sources
feature: during init callback to Bot#configure to define additional attributes
feature: enable_feature_layer/interface_options removed from join_game and added as attributes
feature: removed enable_feature_layer from Sc2 config, and forcing it via Bot#configure
feature: Sc2#is_live? reads bin/ladder env "AIARENA" to toggle live or not
feature: moved /data/replays to ./replays/
  • Loading branch information
dysonreturns committed Feb 5, 2024
1 parent 5f055c7 commit 4ca8c2b
Show file tree
Hide file tree
Showing 34 changed files with 2,510 additions and 117 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -63,8 +63,7 @@ Gemfile.lock


# Library specific excludes
/data/replays/autosave*.SC2Replay
/replays/*.SC2Replay
/data/debug_observation.json
/*.gem
/test/
/sc2ai.yml
1 change: 1 addition & 0 deletions .standard.yml
Expand Up @@ -2,6 +2,7 @@ ruby_version: 3.3

ignore: # default: []
- 'data/**/*'
- 'lib/docker_build/**/*'
# Testing by hand
- 'outside_*.rb'
- 'test/**/*'
Expand Down
20 changes: 20 additions & 0 deletions bin/docker_compose_ladder
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby

# Builds image used in ladderzip

puts "This command builds the base image with semi-portable ruby for the server."
require "pathname"

compose_file = Pathname(__FILE__).parent.parent
.join("lib", "docker_build", "docker-compose-base-image.yml")
.expand_path
cmd = "docker compose --progress=plain -f #{compose_file.to_s} build"
puts cmd
Kernel.exec(cmd)

puts "Ruby can be found in the image at: /root/ruby-builder/.ruby"

# If you need to use the ruby on the container,
# export PATH=/root/ruby-builder/.ruby/bin:$PATH
# To allow access to ruby/bunder/etc.
# Or simply execute a ruby command directly: /root/ruby-builder/.ruby/bin/ruby -v
4 changes: 2 additions & 2 deletions docs/README.md
Expand Up @@ -46,7 +46,7 @@ The gem, however, needs some linear algebra for heavy lifting in the form of Ope
`brew install openblas`

**Debian/Ubuntu and WSL2**
`apt install libblas3 liblapacke`
`apt install libopenblas0`

**Windows**
From Command Prompt:
Expand Down Expand Up @@ -196,7 +196,7 @@ Sc2::Match.new(
```

Congrats, you're botting!
The replay is auto-saved as `data/replays/autosave-#{botname}.SC2Replay` for casual review.
The replay is auto-saved as `replays/autosave-#{botname}.SC2Replay` for casual review.

If the code scares you, especially that `game_info.start_raw.start_locations.first` part, fear not.
The syntax is generally quite friendly while also forcibly teaching you the API.
Expand Down
71 changes: 28 additions & 43 deletions exe/sc2ai
@@ -1,51 +1,36 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path("lib")) # TODO: REMOVE
# require "pathname"
# $LOAD_PATH.unshift(Pathname(File.dirname(__FILE__)).parent.join("lib").expand_path.to_s)

require "sc2ai"
require "sc2ai/cli/cli"

puts <<-LOGO
...',;:clooc;'. .':loocc:;,'..
.,lxkkOOO0OOOOOOOkoc;'. .':ldkOOOOOOOOOOOkkdc,
.,:okOOOOOOOOOOOOOOOkdl, ,ldkOOOOOOOOOOOOOOOko:'.
.;lkOOOOOOOOOOOOOO0o. .d0OOOOOOOOOOOOOOxl,.
.;dOOOOOOOOOOOO0o. .d0OOOOOOOOOOOOo;.
.cOOOOOOOOOOO0o. .d0OOOOOOOOOOk:
.x0OOOOOOOOO0o. .d0OOOOOOOOO0d.
.............:OKKKKKKKKKKKx;. .d0OOO0000000d' ..................
.dKXNXXXXXXXXXXNWMWWMWMMMMWMNk; .d00XNWWWWWWWNK00000000000000000Oo'
.xMMMMMMMMMMMMMMMMMMMMMMMMMMWO' .xXNMMMMMMMMMMMMMMMMMMMMMMMMMWKd,.
.o0O000000000000KNNNNWWMMMWNKo. ,KWNWNNWWWNWWN00000000OKNMWXx;.
,x0O00KNWNKOOo. .x0OOO00000O0d. .:d:.
.:ccllllllcl;;x0OOOO000OO0o. .,:xKNXXXXXNX0O0d.
:OWMMMMMMMMWK00OOOOOOOOO0o. cKNMM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&@&J ^G#&&&&&@@@@@@@@@@&G!.
# ^77!!!!!!!PGGB&&BY. !BGGGGGGP77777?BG7.
# ~??777?~7JJJYYJJ: !PBBBBBYJ~
# J&@@@@@&PJJJJJJ..&@&&&&&YJ~ ~PGJ JG?
# .Y&@&&&&#YJJJJ.!@&&&&&&JJ~ .#P @@! &@#
# .P&&&&&&GJJJ.^@&&&&&&YJ~ @#G77@@. #@#
# ^B&&&&&&PJ. B@&&&&&BJ~ 7@J.: :@B #@5
# ~: Y&&&&&&#^ .B@&&&&&#! . :J
# 7#@&BPPPPPB&&&&&&&&! ^B&&&&&&&5~.
# .J&@&&&&&&&&&&&&&&&&&&! :JYP#&&&&@@&&B5^
# .:...........?5555555^ ^JJJJY5PJ^^:.
# .JJJJJJJ: ^JJJJJJJ.
# .!JJJJJ: ^JJJJJ!
# .!JJY: ^YJJ~.
# :7. :7:
#
# LOGO

Sc2::Cli.start
61 changes: 61 additions & 0 deletions lib/docker_build/Dockerfile.ruby
@@ -0,0 +1,61 @@
FROM aiarena/arenaclient-bot:latest
LABEL service="bot-ruby-local"

USER root
WORKDIR /root/ruby-builder

ARG RUBY_VERSION=3.3.0
ARG DEBIAN_DISABLE_RUBYGEMS_INTEGRATION=true

# Deps - Ruby build
RUN apt-get update
RUN apt install --assume-yes rustc curl build-essential libssl-dev zlib1g-dev libgmp-dev uuid-dev

# Deps - libyaml from source
RUN curl https://pyyaml.org/download/libyaml/yaml-0.2.5.tar.gz -o yaml-0.2.5.tar.gz
RUN tar -zxf yaml-0.2.5.tar.gz

# Ruby
RUN mkdir -p /root/ruby-builder/.ruby
RUN curl https://cache.ruby-lang.org/pub/ruby/3.3/ruby-$RUBY_VERSION.tar.gz -o ruby-$RUBY_VERSION.tar.gz
RUN tar -zxf ruby-$RUBY_VERSION.tar.gz
RUN mkdir ruby-$RUBY_VERSION/build
WORKDIR /root/ruby-builder/ruby-$RUBY_VERSION/build
RUN ../configure --prefix="/root/ruby-builder/.ruby" --disable-install-doc --enable-shared --enable-load-relative --with-libyaml-source-dir=/root/ruby-builder/yaml-0.2.5
RUN make install -j8

# Clean
WORKDIR /root/ruby-builder
RUN rm ruby-$RUBY_VERSION.tar.gz
RUN rm -rf ./ruby-$RUBY_VERSION
RUN rm yaml-0.2.5.tar.gz
RUN rm -rf ./yaml-0.2.5

# Package config
# numo-linalg needs openblas, copy to ruby-prefix/lib/ dir.
RUN apt download libopenblas0-serial
RUN mkdir openblas
RUN dpkg-deb -x ./libopenblas*.deb openblas
RUN cp -d openblas/usr/lib/x86_64-linux-gnu/openblas-serial/* /root/ruby-builder/.ruby/lib/
RUN rm -rf ./openblas
RUN rm ./libopenblas0-serial*.deb

RUN apt download libgfortran5
RUN mkdir libgfortran5
RUN dpkg-deb -x ./libgfortran*.deb libgfortran5
RUN find libgfortran5
RUN cp libgfortran5/usr/lib/x86_64-linux-gnu/libgfortran.so.5 /root/ruby-builder/.ruby/lib/
RUN rm -rf ./libgfortran5
RUN rm ./libgfortran5*.deb

RUN /root/ruby-builder/.ruby/bin/ruby --yjit -v
RUN apt remove --assume-yes rustc curl build-essential libssl-dev zlib1g-dev libgmp-dev uuid-dev

# Ladder zip
RUN apt install --assume-yes zip

ENV PATH "/root/ruby-builder/.ruby/bin:$PATH"

WORKDIR /root/

ENTRYPOINT ["/bin/bash"]
10 changes: 10 additions & 0 deletions lib/docker_build/docker-compose-base-image.yml
@@ -0,0 +1,10 @@
version: "3.9"
services:
bot:
image: dysonreturns/aiarena-ruby-builder:latest
environment:
- CPPFLAGS=-DPNG_ARM_NEON_OPT=0
platform: "linux/amd64"
build:
dockerfile: ./Dockerfile.ruby

9 changes: 9 additions & 0 deletions lib/docker_build/docker-compose-ladderzip.yml
@@ -0,0 +1,9 @@
version: "3.9"
services:
bot:
image: dysonreturns/aiarena-ruby-builder:latest
environment:
- CPPFLAGS=-DPNG_ARM_NEON_OPT=0
platform: "linux/amd64"
tty: true

47 changes: 23 additions & 24 deletions lib/sc2ai.rb
@@ -1,45 +1,38 @@
# frozen_string_literal: true

# TODO: MOVE RUBY_GC_* to load into shell ENV, because they apparently can't be set at runtime.
# Arbitrary very large numbers which should not be reached
# RUBY_GC_MALLOC_LIMIT=128000000000;
# RUBY_GC_OLDMALLOC_LIMIT=128000000000;
# RUBY_GC_OLDMALLOC_LIMIT_MAX=128000000000;
# RUBY_GC_HEAP_INIT_SLOTS=329375

# GC tuning.
GC.disable
# TODO: MOVE RUBY_GC_* to load into shell ENV, because they apparently can't be set at runtime.
# Arbitrary very large numbers which should not be reached
# ENV['RUBY_GC_OLDMALLOC_LIMIT'] = "128000000000"
# ENV['RUBY_GC_OLDMALLOC_LIMIT_MAX'] = "128000000000"

# Preallocate slightly over observed GC stats
# Ruby 3.3
# TODO: Check if these vars can be set at runtime when moving to 3.3 or move to a runner.
# TODO: Benchmark extremely long filled map and re-adjust. These are medium sized game and can be 2-10x'd.
ENV["RUBY_GC_HEAP_INIT_SIZE_40_SLOTS"] = "170000"
ENV["RUBY_GC_HEAP_INIT_SIZE_80_SLOTS"] = "25000"
ENV["RUBY_GC_HEAP_INIT_SIZE_160_SLOTS"] = "9000"
ENV["RUBY_GC_HEAP_INIT_SIZE_320_SLOTS"] = "1000"
ENV["RUBY_GC_HEAP_INIT_SIZE_640_SLOTS"] = "500"

GC.enable
GC.start
# In the event major runs, let it compact
GC.auto_compact = true

# For Numo linear algebra, fix paths for specific macOS's
if ENV["LD_LIBRARY_PATH"].nil?
ENV["LD_LIBRARY_PATH"] = "/opt/homebrew/opt/lapack/lib:/opt/homebrew/opt/openblas/lib"
# For Numo linear algebra, fix paths for specific macOS's and on ladder
library_paths = if ENV["LD_LIBRARY_PATH"].nil?
[]
else
ENV["LD_LIBRARY_PATH"] += ":/opt/homebrew/opt/lapack/lib:/opt/homebrew/opt/openblas/lib"
ENV["LD_LIBRARY_PATH"].split(":")
end
library_paths << [
# MacOS
"/opt/homebrew/opt/lapack/lib",
"/opt/homebrew/opt/lapack/lib",
"/opt/homebrew/opt/openblas/lib",
# Ladder deploy
RbConfig::CONFIG["libdir"]
]
ENV["LD_LIBRARY_PATH"] = library_paths.compact.join(":")

begin
require "numo/linalg/autoloader"
rescue => e
puts "Error from numo-linalg: #{e}"
puts "Lets get some linear algebra on your system..."
puts 'Mac: "brew install openblas"'
puts 'Debian/Ubuntu and WSL2: "apt install libblas3 liblapacke"'
puts 'Debian/Ubuntu and WSL2: "apt install libopenblas0"'
if Gem.win_platform?
puts 'Windows: from CMD "ridk enable & pacman -S mingw-w64-ucrt-x86_64-openblas --noconfirm".'
end
Expand Down Expand Up @@ -97,5 +90,11 @@ def logger
@logger.level = :debug
@logger
end

# Returns whether we are on the ladder or not
# @return [Boolean]
def ladder?
@is_live ||= ENV.has_key?("AIARENA")
end
end
end

0 comments on commit 4ca8c2b

Please sign in to comment.