Skip to content

Commit

Permalink
make runnable, works but no gateways are build.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperGeurtz committed Jan 20, 2019
1 parent 4391a82 commit 22c2333
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/main/scala/Debugging/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Logger {
}

def onException(exception: Exception) {
throw exception
errorOcurred = true
log("An exception was thrown on frame " + With.frame)
logMessages.append(formatException(exception))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ object ShowBattleSummary extends View {
}

private def drawEstimationReport(estimation: Prediction) {
With.game.setTextSize(bwapi.Text.Size.Enum.Large)
With.game.setTextSize(bwapi.TextSize.Large)
With.game.drawTextScreen(tableHeader0.bwapi, With.self.name)
With.game.drawTextScreen(tableHeader1.bwapi, With.enemy.name)
With.game.drawTextScreen(tableStart0.bwapi, "+" + estimation.costToEnemy.toInt)
With.game.drawTextScreen(tableStart1.bwapi, "-" + estimation.costToUs.toInt)
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import ProxyBwapi.Players.PlayerInfo
object ShowPlayerNames extends {

def renderScreen(visionLabel: String) {
With.game.setTextSize(bwapi.Text.Size.Enum.Large)
With.game.setTextSize(bwapi.TextSize.Large)
With.game.drawBoxScreen (5, 5, 5 + 20, 5 + 20, With.self.colorMedium, true)
With.game.drawBoxScreen (5, 5 + 25, 5 + 20, 5 + 20 + 25, With.enemy.colorMedium, true)
With.game.drawTextScreen (5 + 25, 5, With.self.name + renderRace(With.self) + " vs. ")
With.game.drawTextScreen (5 + 25, 5 + 25, With.enemy.name + renderRace(With.enemy))
With.game.setTextSize(bwapi.Text.Size.Enum.Default)
With.game.setTextSize(bwapi.TextSize.Default)
With.game.drawTextScreen (5, 5 + 50, With.self.name + " is displaying this game in " + visionLabel + "!")
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)
}

private def renderRace(player: PlayerInfo): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object ShowArchitectureHeuristics extends View {
}

private def renderPlacementHeuristicsScreen(blueprint: Blueprint, placement: Placement) = {
With.game.setTextSize(bwapi.Text.Size.Enum.Default)
With.game.setTextSize(bwapi.TextSize.Default)
DrawScreen.column(
5,
5 * With.visualization.lineHeightSmall,
Expand All @@ -50,7 +50,7 @@ object ShowArchitectureHeuristics extends View {
.zipWithIndex
.map(pair => "#" + pair._2 + " " + pair._1._1 + " (" + (-pair._1._2) + ")"))
.flatten)
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)
}

private def renderPlacementHeuristicsMap(blueprint: Blueprint, placement: Placement) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ object ShowZones extends View {

override def renderMap() {

With.geography.zones.foreach(zone => {
DrawMap.polygonPixels(
zone.points,
zone.owner.colorDark)
})
// With.geography.zones.foreach(zone => {
// DrawMap.polygonPixels(
// zone.points,
// zone.owner.colorDark)
// })

With.geography.zones.foreach(zone => {
zone.edges.foreach(edge => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ object ShowCarriers extends View {
if (carrier.complete && carrier.is(Protoss.Carrier)) {
carrier.orderTarget.foreach(target => DrawMap.line(carrier.pixelCenter, target.pixelCenter, Colors.MediumRed))
DrawMap.box(carrier.pixelCenter.subtract(32, 16), carrier.pixelCenter.add(32, 16), Colors.DarkViolet, true)
With.game.setTextSize(bwapi.Text.Size.Enum.Large)
With.game.setTextSize(bwapi.TextSize.Large)
carrier.agent.lastAction.foreach(action => DrawMap.label(action.name, carrier.pixelCenter.subtract(16, 16)))
DrawMap.label(
+ carrier.interceptors.count(_.order == Orders.InterceptorAttack)
+ "/"
+ carrier.interceptors.count(_.complete),
carrier.pixelCenter.subtract(16, 0))
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)
if (carrier.interceptors.exists(_.complete)) {
DrawMap.circle(carrier.pixelCenter, 32*8, Colors.BrightGray)
DrawMap.circle(carrier.pixelCenter, 32*10, Colors.DarkGray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ object ShowUnitsAll extends View {
val ratio255 = Math.max(0, (255 * (1.0 - ratio)).toInt)
val radius = Math.max(10.0, unit.unitClass.dimensionMin / 2.0)
DrawMap.circle(unit.pixelCenter, radius.toInt, Colors.hsv(Hues.Red, 255, ratio255), solid = true)
With.game.setTextSize(bwapi.Text.Size.Enum.Large)
With.game.setTextSize(bwapi.TextSize.Large)
DrawMap.text(unit.pixelCenter.subtract(4, 10), ttlCurrent.toInt.toString)
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)
}
}
if (showSafety) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object ShowUnitsFriendly extends View {
}
if (showCommand) {
DrawMap.label(
unit.command.map(_.getUnitCommandType.toString).getOrElse(""),
unit.command.map(_.getType.toString).getOrElse(""),
unit.pixelCenter.add(0, labelY),
drawBackground = false)
labelY += 7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ object ShowPerformanceSummary extends View {
With.game.drawTextScreen(80, 2 * With.visualization.lineHeightSmall, "+1000ms: " + With.performance.framesOver1000 + "/10")
With.game.drawTextScreen(155, 2 * With.visualization.lineHeightSmall, "+10000ms: " + With.performance.framesOver10000 + "/1")
if (With.performance.disqualified) {
With.game.setTextSize(bwapi.Text.Size.Enum.Large)
With.game.setTextSize(bwapi.TextSize.Large)
DrawScreen.header(295, ":/")
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Visualization {
requireInitialization()
if ( ! enabled) return
if (With.tasks.tasks.exists(_.totalRuns == 0)) return
With.game.setTextSize(bwapi.Text.Size.Enum.Small)
With.game.setTextSize(bwapi.TextSize.Small)

if (happy) {
ShowHappyVision.render()
Expand Down
54 changes: 18 additions & 36 deletions src/main/scala/Information/Geography/Calculations/ZoneBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Lifecycle.With
import Mathematics.Points.{Pixel, Tile, TileRectangle}
import Mathematics.Shapes.Spiral
import Utilities.ByOption
import bwta.{BWTA, Polygon, Region}
import bwta.{BWTA, Region}

import scala.collection.JavaConverters._
import scala.collection.mutable
Expand Down Expand Up @@ -66,46 +66,28 @@ object ZoneBuilder {
}

def buildZone(thisRegion: Region, name: String): Zone = {

// The goal: We want to check if two regions are the same.
// The problem: BWMirror gives its proxy objects no unique identifiers, hashcodes, or equality comparisons for objects.
// The workaround: Use properties of the polygon to construct a hopefully-unique identifier

val thisIdentifier = new RegionIdentifier(thisRegion)

val tiles = With.geography.allTiles.filter(tile => BWTA.getRegion(tile.bwapi) == thisRegion)
val x = tiles.map(_.x)
val y = tiles.map(_.y)

val boundingBox = TileRectangle(
Pixel(
thisIdentifier.polygon.getPoints.asScala.map(_.getX).min,
thisIdentifier.polygon.getPoints.asScala.map(_.getY).min)
.tileIncluding,
Pixel(
32 + thisIdentifier.polygon.getPoints.asScala.map(_.getX).max,
32 + thisIdentifier.polygon.getPoints.asScala.map(_.getY).max)
.tileIncluding)
val tiles = new mutable.HashSet[Tile]
tiles ++= boundingBox.tiles
.filter(tile => {
val thatRegion = BWTA.getRegion(tile.bwapi)
if (thatRegion == null) {
false
}
else {
val thatIdentifier = new RegionIdentifier(thatRegion)
thatIdentifier.same(thisIdentifier)
}
})
.toSet

new Zone(name, thisRegion, boundingBox, tiles)
Tile(
ByOption.min(x).getOrElse(0),
ByOption.min(y).getOrElse(0)),
Tile(
ByOption.max(x).getOrElse(0),
ByOption.max(y).getOrElse(0)))

val tileSet = new mutable.HashSet[Tile]
tileSet ++= tiles
new Zone(name, thisRegion, boundingBox, tileSet )
}

private class RegionIdentifier(region: Region) {
val polygon : Polygon = region.getPolygon
val area : Double = polygon.getArea
val perimeter : Double = polygon.getPerimeter
private class RegionIdentifier(val region: Region) {

def same(other: RegionIdentifier): Boolean = {
area == other.area &&
perimeter == other.perimeter
region.equals(other.region)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/Information/Geography/Types/Edge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Edge(choke: Chokepoint) {
lazy val pixelCenter : Pixel = new Pixel(choke.getCenter)
lazy val radiusPixels : Double = choke.getWidth / 2
lazy val tiles : Vector[Tile] = PixelRay(sidePixels.head, sidePixels.last).tilesIntersected.toVector
lazy val sidePixels : Vector[Pixel] = Vector(new Pixel(choke.getSides.first), new Pixel(choke.getSides.second))
lazy val sidePixels : Vector[Pixel] = Vector(new Pixel(choke.getSides.getFirst), new Pixel(choke.getSides.getSecond))
lazy val endPixels: Vector[Pixel] = Vector(-1, 1)
.map(m => pixelCenter
.radiateRadians(
Expand All @@ -28,8 +28,8 @@ class Edge(choke: Chokepoint) {
32))
lazy val zones: Vector[Zone] =
Vector(
choke.getRegions.first,
choke.getRegions.second)
choke.getRegions.getFirst,
choke.getRegions.getSecond)
.map(region => With.geography.zones.minBy(
_.centroid.pixelCenter.pixelDistanceSquared(
new Pixel(region.getCenter))))
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/Information/Geography/Types/Zone.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Zone(
lazy val border : Set[Tile] = tiles.filter(_.adjacent8.exists( ! tiles.contains(_))).toSet
lazy val perimeter : Set[Tile] = tiles.filter(tile => tile.tileDistanceFromEdge <= 1 || ! tile.adjacent8.forall(With.grids.walkableTerrain.get)).toSet
lazy val centroid : Tile = if (tiles.isEmpty) new Pixel(bwtaRegion.getCenter).tileIncluding else tiles.minBy(_.tileDistanceSquared(new Pixel(bwtaRegion.getCenter).tileIncluding))
lazy val area : Double = bwtaRegion.getPolygon.getArea
lazy val points : Iterable[Pixel] = bwtaRegion.getPolygon.getPoints.asScala.map(new Pixel(_)).toVector
lazy val area : Double = tiles.size
lazy val island : Boolean = With.geography.startBases.count(st => With.paths.groundPathExists(st.heart, centroid)) < 2
lazy val tilesBuildable : Array[Tile] = { With.grids.buildableTerrain.initialize(); tiles.filter(With.grids.buildableTerrain.get).toArray }
lazy val maxMobility : Int = ByOption.max(tiles.map(With.grids.mobilityGround.get)).getOrElse(0)
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/Lifecycle/JBWAPIClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Lifecycle

import bwapi.BWClient

object JBWAPIClient extends BWClient(Listener)
20 changes: 2 additions & 18 deletions src/main/scala/Lifecycle/Listener.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,21 @@ package Lifecycle
import bwapi.{BWEventListener, Player}

object Listener extends BWEventListener{
val mirror:bwapi.Mirror = new bwapi.Mirror()
var bot:Option[Bot] = None
var bot: Option[Bot] = None

var lastException: Option[Exception] = None
var lastStackTrace: Option[String] = None
var doesThisCodeEvenGetExecuted: Int = 0

def initialize(): Unit = {
try {
mirror.getModule.setEventListener(this)
mirror.startGame()
}
catch { case exception: Exception =>
lastException = Some(exception)
lastStackTrace = Some(exception.getStackTrace.toString)
doesThisCodeEvenGetExecuted = 123
val setABreakpointHere = 12345
}
}

override def onStart(): Unit = {
try {
With.game = mirror.getGame
With.game = JBWAPIClient.getGame
bot = Some(new Bot())
bot.get.onStart()
}
catch { case exception: Exception =>
lastException = Some(exception)
lastStackTrace = Some(exception.getStackTrace.toString)
doesThisCodeEvenGetExecuted = 456
val setABreakpointHere = 12345
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Lifecycle/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package Lifecycle

object Main {
def main(args: Array[String]) {
Listener.initialize
JBWAPIClient.startGame()
}
}
8 changes: 2 additions & 6 deletions src/main/scala/Lifecycle/With.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ object With {

def onStart() {
game.setLatCom(false)
game.enableFlag(1) //Enable unit control
game.enableFlag(bwapi.Flag.UserInput) //Enable unit control
game.setLocalSpeed(0)

proxy = new ProxyBWMirror
Expand Down Expand Up @@ -146,15 +146,11 @@ object With {

def onEnd() {
With.logger.flush()
BWTA.cleanMemory()
}

private def initializeBWTA() {
With.logger.debug("Loading BWTA for " + With.game.mapName + " at " + With.game.mapFileName())
BWTA.readMap()
BWTA.readMap(game)
BWTA.analyze()
//These may not be necessary or helpful since BWTA2 doesn't seem to work in BWMirror
BWTA.computeDistanceTransform()
BWTA.buildChokeNodes()
}
}
2 changes: 1 addition & 1 deletion src/main/scala/Micro/Agency/Commander.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Commander {
if (unready(unit)) return

val alreadyAttackMovingThere = unit.command.exists(c =>
c.getUnitCommandType.toString == UnitCommandType.Attack_Move.toString &&
c.getType.toString == UnitCommandType.Attack_Move.toString &&
new Pixel(c.getTargetPosition).pixelDistance(destination) < 128)

if ( ! alreadyAttackMovingThere || unit.seeminglyStuck) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/ProxyBwapi/Players/PlayerProxy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract class PlayerProxy(base:Player) {
def supplyUsed : Int = supplyUsedCache()
def supplyTotal : Int = supplyTotalCache()

def rawUnits: mutable.Buffer[Unit] = unitsCache()
def rawUnits: Vector[Unit] = unitsCache()

private lazy val maxUpgradeLevels = new mutable.HashMap[Upgrade, Int] ++ Upgrades.all.map(upgrade => (upgrade, 0))
def getUpgradeLevel(upgrade: Upgrade): Int = {
Expand Down Expand Up @@ -67,7 +67,7 @@ abstract class PlayerProxy(base:Player) {
private val gatheredMineralsCache = new Cache(() => base.gatheredMinerals)
private val supplyUsedCache = new Cache(() => if (!isUs) 0 else With.units.ours.toSeq.filter(u => u.unitClass.race == raceInitial).map(_.unitClass.supplyRequired).sum)
private val supplyTotalCache = new Cache(() => if (!isUs) 0 else Math.min(400, With.units.ours.toSeq.filter(u => u.complete && u.unitClass.race == raceInitial).map(_.unitClass.supplyProvided).sum))
private val unitsCache = new Cache(() => base.getUnits.asScala)
private val unitsCache = new Cache(() => base.getUnits.asScala.toVector)
private val upgradeLevelCaches = new mutable.HashMap[Upgrade, Cache[Int]]
private val techsResearchedCaches = new mutable.HashMap[Tech, Cache[Boolean]]
}
2 changes: 1 addition & 1 deletion src/main/scala/ProxyBwapi/UnitClasses/UnitClassProxy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class UnitClassProxy(val baseType: UnitType) {
lazy val turnRadius = baseType.turnRadius
lazy val upgrades = baseType.upgrades.asScala.map(Upgrades.get)
lazy val upgradesWhat = baseType.upgradesWhat.asScala.map(Upgrades.get)
lazy val whatBuilds = new Pair(UnitClasses.get(baseType.whatBuilds.first), baseType.whatBuilds.second)
lazy val whatBuilds = new Pair(UnitClasses.get(baseType.whatBuilds.getFirst), baseType.whatBuilds.getSecond)
lazy val width = baseType.width
lazy val race = baseType.getRace
lazy val sizeRaw = size
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ProxyBwapi/UnitInfo/FriendlyUnitProxy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ abstract class FriendlyUnitProxy(base: bwapi.Unit, id: Int) extends UnitInfo(bas
private val isGatheringMineralsCache = new Cache(() => base.isGatheringMinerals)
private val isGatheringGasCache = new Cache(() => base.isGatheringGas)

private val badPositions = Vector(Position.Invalid, Position.None, Position.Unknown)
private val badPositions = Vector(null, Position.Invalid, Position.None, Position.Unknown)

def target : Option[UnitInfo] = targetCache()
def targetPixel : Option[Pixel] = targetPixelCache()
Expand Down

0 comments on commit 22c2333

Please sign in to comment.