# Python library for StarCraft II
# About this tutorial
- 0. This introduction
- 1. Gathering resources
- 2. Building units
async def buildWorkers(self):for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:await self.do(commandcenter.train(UnitTypeId.SCV))
import sc2from sc2 import run_game, maps, Race, Difficultyfrom sc2.player import Bot, Computerfrom sc2.ids.unit_typeid import *class AlanBot(sc2.BotAI):async def on_step(self, iteration):await self.buildWorkers()async def buildWorkers(self):for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:await self.do(commandcenter.train(UnitTypeId.SCV))run_game(maps.get("AbyssalReefLE"), [Bot(Race.Terran, AlanBot()),Computer(Race.Zerg, Difficulty.Hard)], realtime=True)
import sc2from sc2 import run_game, maps, Race, Difficultyfrom sc2.player import Bot, Computerfrom sc2.ids.unit_typeid import *class AlanBot(sc2.BotAI):async def on_step(self, iteration):await self.buildWorkers()await self.distribute_workers()async def buildWorkers(self):for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:await self.do(commandcenter.train(UnitTypeId.SCV))run_game(maps.get("AbyssalReefLE"), [Bot(Race.Terran, AlanBot()),Computer(Race.Zerg, Difficulty.Hard)], realtime=True)
async def buildRefineries(self):for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready:vespenes = self.state.vespene_geyser.closer_than(18.0, commandcenter)for vespene in vespenes:if not self.can_afford(UnitTypeId.REFINERY):breakworker = self.select_build_worker(vespene.position)if worker is None:breakif not self.units(UnitTypeId.REFINERY).closer_than(1.0, vespene).exists:await self.do(worker.build(UnitTypeId.REFINERY, vespene))
import sc2from sc2 import run_game, maps, Race, Difficultyfrom sc2.player import Bot, Computerfrom sc2.ids.unit_typeid import *class AlanBot(sc2.BotAI):async def on_step(self, iteration):await self.buildWorkers()await self.distribute_workers()await self.buildRefineries()async def buildWorkers(self):for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:await self.do(commandcenter.train(UnitTypeId.SCV))async def buildRefineries(self):for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready:vespenes = self.state.vespene_geyser.closer_than(18.0, commandcenter)for vespene in vespenes:if not self.can_afford(UnitTypeId.REFINERY):breakworker = self.select_build_worker(vespene.position)if worker is None:breakif not self.units(UnitTypeId.REFINERY).closer_than(1.0, vespene).exists:await self.do(worker.build(UnitTypeId.REFINERY, vespene))run_game(maps.get("AbyssalReefLE"), [Bot(Race.Terran, AlanBot()),Computer(Race.Zerg, Difficulty.Hard)], realtime=True)
You need to check different things. When you want to create one Barrack you need:
And when you want to create a Marine you need:
This is why we create two different functions: buildStructure() and buildOffensiveUnit().
commandCenter = self.units(UnitTypeId.COMMANDCENTER).ready.randomnearCC = await self.find_placement(UnitTypeId.SUPPLYDEPOT, commandcenter.position, placement_step=2)
async def buildStructure(self, structureName, amount):if self.units(UnitTypeId.COMMANDCENTER).ready.exists:commandCenter = self.units(UnitTypeId.COMMANDCENTER).ready.randomworkers = self.workers.gatheringnearCC = await self.find_placement(UnitTypeId.SUPPLYDEPOT, commandCenter.position, placement_step=2)if structureName == 'supplydepot':if self.supply_left < 6 and self.can_afford(UnitTypeId.SUPPLYDEPOT):if workers:w = workers.furthest_to(workers.center)if nearCC:await self.do(w.build(UnitTypeId.SUPPLYDEPOT, nearCC))if structureName == 'barracks':if self.units.of_type([UnitTypeId.SUPPLYDEPOT, UnitTypeId.SUPPLYDEPOTLOWERED, UnitTypeId.SUPPLYDEPOTDROP]).ready.existsand self.units(UnitTypeId.BARRACKS).amount + self.already_pending(UnitTypeId.BARRACKS) < amountand self.can_afford(UnitTypeId.BARRACKS):if workers:w = workers.furthest_to(workers.center)if nearCC:await self.do(w.build(UnitTypeId.BARRACKS, nearCC))
async def buildOffensiveUnit(self, unitName, structureName, maxAmount):structure = self.unitSelector(structureName)unit = self.unitSelector(unitName)if self.units(structure).ready.exists:for struct in self.units(structure).ready.noqueue:if self.can_afford(unit) and self.supply_left > 0 and self.units(unit).amount < maxAmount:await self.do(struct.train(unit))
async def on_step(self, iteration):await self.buildWorkers()await self.distribute_workers()await self.buildRefineries()await self.buildStructure('supplydepot', 2000)await self.buildStructure('barracks', 4)await self.buildOffensiveUnit('marine', 'barracks', 27)if self.units(UnitTypeId.MARINE).amount > 17:await self.move('marine', True)else:await self.move('marine', False)
Here you can find a start up code for running your python project.
Import the necessary code at the beginning and modify the options in the options dictionary.
import time# Import here the main part of your codefrom My_script import Scriptdef user_data():options = {0: "option 0",1: "option 1",2: "option 2"}# DISPLAY MENU WITH OPTIONSwhile 1:print("\nSelect and option")for option in options.items():print(option)selectedOption = input("Option to use: ")if selectedOption.isdigit():selectedOption = int(selectedOption)if selectedOption not in options:print("Error, not a valid option")else:breakelse:print("Error, choose the option digit")# READ A FILENAMEwhile 1:file_name = input("Write the file name")try:with open(file_name) as f:# File opened successfullyfile = fbreakexcept FileNotFoundError:print("Error, couldn't open the file")return file, options[selectedOption]# Beginning of the scriptprint("[ # PROGRAM_NAME # ]")print("Init program...")file, option = user_data()start = time.time()print("Starting the program...")Script.main(file, option)print("Total time:", round(time.time() - start, 3), "seconds.")
You need to pair to your smartphone and enable Developer Mode in the Oculus app for the smartphone. Once you have done this you can continue in your computer by installing Unity.
First install Unity Hub and download Unity (a LTS version if you want)
With the installation complete create a new Unity 3D project.
Now we have an empty 3D project:If you didn't install the needed External Tools as the JDK and SDK, you need to install them from the Unity Hub, just Add Modules to the Unity install, add the Android SDK & NDK Tools, and if you don't have it either, install the Open JDK.Quick sand material / shader to add to your project, with a NodeGroup to make it easy to change the parameters. Download at the bottom of the post.
Rails.application.routes.draw doroot 'home#index'end
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script><script><!-- three.js code goes here --></script>
<h1>Home#index</h1><p>Find me in app/views/home/index.html.erb</p><%= render partial: "3d_viewer" %>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script><script>var scene = new THREE.Scene();const WIDTH = 500;const HEIGHT = 500;var camera = new THREE.PerspectiveCamera( 75, WIDTH/HEIGHT, 0.1, 1000 );camera.position.z = 4;var renderer = new THREE.WebGLRenderer({antialias:true});renderer.setClearColor("#000000");renderer.setSize( 400, 400 );document.body.appendChild( renderer.domElement );var geometry = new THREE.BoxGeometry( 1, 1, 1 );var material = new THREE.MeshBasicMaterial( { color: "#433F81" } );var cube = new THREE.Mesh( geometry, material );scene.add( cube );var render = function () {requestAnimationFrame( render );cube.rotation.y += 0.01;renderer.render(scene, camera);};render();</script>
Find the explanation in https://xcran.blogspot.com/2022/03/0-starcraft-ii-python-bot-introduction.html
import sc2from sc2 import run_game, maps, Race, Difficultyfrom sc2.player import Bot, Computerfrom sc2.ids.unit_typeid import *
class AlanBot(sc2.BotAI): async def on_step(self, iteration): await self.buildWorkers() await self.distribute_workers() await self.buildRefineries() await self.buildStructure('supplydepot', 2000) await self.buildStructure('barracks', 4) await self.buildOffensiveUnit('marine', 'barracks', 27) if self.units(UnitTypeId.MARINE).amount > 17: await self.move('marine', True) else: await self.move('marine', False)
async def buildWorkers(self): for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue: if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4: await self.do(commandcenter.train(UnitTypeId.SCV))
async def buildRefineries(self): for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready: vespenes = self.state.vespene_geyser.closer_than( 18.0, commandcenter) for vespene in vespenes: if not self.can_afford(UnitTypeId.REFINERY): break worker = self.select_build_worker(vespene.position) if worker is None: break if not self.units(UnitTypeId.REFINERY).closer_than(1.0, vespene).exists: await self.do(worker.build(UnitTypeId.REFINERY, vespene))
async def buildStructure(self, structureName, amount): if self.units(UnitTypeId.COMMANDCENTER).ready.exists: commandCenter = self.units(UnitTypeId.COMMANDCENTER).ready.random workers = self.workers.gathering
nearCC = await self.find_placement(UnitTypeId.SUPPLYDEPOT, commandCenter.position, placement_step=2)
if structureName == 'supplydepot': if self.supply_left < 6 and self.can_afford(UnitTypeId.SUPPLYDEPOT): if workers: w = workers.furthest_to(workers.center) if nearCC: await self.do(w.build(UnitTypeId.SUPPLYDEPOT, nearCC))
if structureName == 'barracks': if self.units.of_type([UnitTypeId.SUPPLYDEPOT, UnitTypeId.SUPPLYDEPOTLOWERED, UnitTypeId.SUPPLYDEPOTDROP]).ready.exists \ and self.units(UnitTypeId.BARRACKS).amount + self.already_pending(UnitTypeId.BARRACKS) < amount \ and self.can_afford(UnitTypeId.BARRACKS): if workers: w = workers.furthest_to(workers.center) if nearCC: await self.do(w.build(UnitTypeId.BARRACKS, nearCC))
async def buildOffensiveUnit(self, unitName, structureName, maxAmount): structure = self.unitSelector(structureName) unit = self.unitSelector(unitName) if self.units(structure).ready.exists: for struct in self.units(structure).ready.noqueue: if self.can_afford(unit) and self.supply_left > 0 and self.units(unit).amount < maxAmount: await self.do(struct.train(unit))
run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Terran, AlanBot()), Computer(Race.Zerg, Difficulty.Medium)], realtime=True)
#include <stdio.h>
#define IN 1
#define OUT 0
int main(){
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while((c = getchar()) != EOF){
++nc; /*Count chars*/
if(c == '\n') ++nl; /*Count lines*/
if(c == ' ' || c == '\n' || c == '\t') state = OUT;
else if(state == OUT){
state = IN;
++nw; /*Count words*/
}
}
printf("[Lines: %d]\n[Words: %d]\n[Chars: %d]\n", nl, nw, nc);
return 0;
}
#include <stdio.h>
int multNumbers(int a, int b);
int main(){
int number1 = 3;
int number2 = 4;
int mult;
mult = multNumbers(number1, number2);
printf("mult = %d",mult);
return 0;
}
int multNumbers(int a,int b){
return result a * b;
}