Last active
August 29, 2015 14:01
-
-
Save samlam/2dd181132561eab679ec to your computer and use it in GitHub Desktop.
lwg test
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
if (!window.aiScope){ | |
window.aiScope = scope; | |
window.aiContainer = this; | |
} | |
var ai = this.ai; | |
///setup and cached the func | |
if (!ai){ | |
ai = this.ai = {}; | |
ai.playerNumber = scope.getMyPlayerNumber(); | |
ai.mines = scope.getBuildings({type: "Goldmine"}); | |
ai.getSupply = function (){ | |
return game.players[ai.playerNumber].supply; | |
} | |
ai.findNearestMine = function(fromX, fromY){ | |
var nearestMine = null; | |
var neasrestDist = 99999; | |
ai.mines.forEach(function(mine){ | |
var dist = Math.pow(mine.getX() - fromX, 2) + Math.pow(mine.getY() - fromY, 2); | |
if(dist < neasrestDist) | |
{ | |
nearestMine = mine; | |
neasrestDist = dist; | |
} | |
}); | |
return nearestMine; | |
} | |
ai.findNextBuilder = function (idleWorkers){ | |
var visibleWorkers = scope.getUnits({type:"Worker", player:ai.playerNumber}); | |
if (idleWorkers.length > 0) return idleWorkers[0]; | |
if (visibleWorkers.length > 0) return visibleWorkers[0]; | |
} | |
ai.getMaxSupply = function (){ | |
return scope.player.maxSupply; | |
} | |
ai.cycle = 0; | |
console.log('ai is initialized'); | |
} | |
ai.cycle ++; | |
var castles = scope.getBuildings({type: "Castle", player: ai.playerNumber }); | |
var houses = scope.getBuildings({type: "House", player: ai.playerNumber}); | |
var barracks = scope.getBuildings({type: "Barracks", player: ai.playerNumber }); | |
var idleWorkers = scope.getUnits({type: "Worker", player: ai.playerNumber, order: "Stop"}); | |
var allVisibleUnits = scope.getUnits({player: ai.playerNumber}); | |
var availableSupply = ai.getMaxSupply() - ai.getSupply(); | |
var gold = scope.getGold(); | |
if (window.DEBUG) debugger; | |
if (!ai.builder || !ai.builder.unit.isAlive){ | |
ai.builder = ai.findNextBuilder(idleWorkers); | |
if (ai.builder){ | |
var homeCastle = castles[0]; | |
if (!homeCastle) return; //temp. avoid exception | |
ai.builder.nxBuildSite = {x:castles[0].getX() + 4, y:castles[0].getY()}; | |
ai.builder.findNextBuildSite = function (currentPos){ | |
//if game.fieldIsBlocked(x,y) | |
} | |
ai.builder.isOnBuildSite = function (){ | |
return (this.unit.pos.y == this.nxBuildSite.y && this.unit.pos.x == this.nxBuildSite.x)? true: false; | |
} | |
ai.builder.clearLastOrder = function(){ | |
this.LastOrder = this.LastOrderFromPos = this.LastOrderTargetPos = null; | |
this.LastOrderCycle = 0; | |
console.log('cancel order/ resetting last order'); | |
} | |
ai.builder.buildNewHouse = function(availGold){ | |
if (availGold < 100) { | |
//cancel order | |
this.clearLastOrder(); | |
return; | |
} | |
scope.order("Build House", [this], {x: this.nxBuildSite.x, y: this.nxBuildSite.y}); | |
this.LastOrder = "Build House"; | |
this.LastOrderCycle = ai.cycle; | |
console.log("build new house"); | |
return true; | |
} | |
ai.builder.buildBarracks = function(availGold){ | |
if(availGold < 200) { | |
//cancel order | |
this.clearLastOrder(); | |
return; | |
} | |
scope.order("Build Barracks", [this],this.nxBuildSite); | |
this.LastOrder = "Build Barracks"; | |
this.LastOrderCycle = ai.cycle; | |
console.log("build barrack"); | |
} | |
ai.builder.move = function(pos){ | |
scope.order("Move",[builder], pos); | |
this.LastOrder = "Move"; | |
this.LastOrderCycle = ai.cycle; | |
this.LastOrderTargetPos = pos; | |
this.LastOrderFromPos = this.unit.pos; | |
console.log ('builder is moving from (' + this.unit.pos.x.toString() + ',' + this.unit.pos.y.toString() + ') to target', pos); | |
} | |
ai.builder.isStuck = function(){ | |
if (ai.cycle > this.LastOrderCycle && this.LastOrderFromPos == this.unit.pos) | |
return true; | |
} | |
} | |
} | |
var builder = ai.builder; | |
console.log('cycle/gold/availableSupply', ai.cycle, scope.getGold(), availableSupply); | |
if (builder){ | |
if ( (builder.unit.order.name == "Stop") || builder.unit.order.name == "Mine"){ | |
//build house | |
if ((houses.length == 0 || availableSupply < 6 ) && gold >= 100){ | |
if (builder.isOnBuildSite()){ | |
builder.buildNewHouse(gold); | |
}else if (!builder.isOnBuildSite() && !builder.isStuck()) { | |
builder.move(builder.nxBuildSite); | |
}else if (builder.isStuck()){ | |
console.log('stuck on building houses'); | |
builder.nxBuildSite.x += -4; | |
builder.nxBuildSite.y += -4; | |
builder.buildNewHouse(gold); | |
} | |
} | |
//build barracks | |
if (houses.length > 0 && barracks.length < 2 && gold >= 200){ | |
console.log('should build barracks'); | |
if (builder.isOnBuildSite()){ | |
console.log('inposition, let\'s build barracks'); | |
builder.buildBarracks(gold); | |
}else if (!builder.isOnBuildSite() && !builder.isStuck()){ | |
console.log('not in position yet, let\'s move to buildsite', builder.nxBuildSite); | |
builder.move(builder.nxBuildSite); | |
}else if (builder.isStuck()){ | |
console.log('stuck on building barracks'); | |
builder.nxBuildSite.x += -4; | |
builder.nxBuildSite.y += -4; | |
builder.buildBarracks(gold); | |
} | |
} | |
} | |
//no worker should be idle | |
idleWorkers.forEach(function(worker){ | |
if (worker.unit.id == builder.unit.id){ | |
//if builder is idle and its last order = the current cycle, which means builder is yet to execute the last order | |
if (builder.LastOrder && builder.LastOrderCycle >= ai.cycle) { | |
return; | |
} | |
else{ | |
console.log('idle builder, let\'s head to mine'); | |
} | |
} | |
var m = ai.findNearestMine(worker.getX(), worker.getY()); | |
scope.order("Mine", [worker], {unit:m}); | |
}); | |
} | |
//no building should be idle | |
castles.forEach(function(castle){ | |
var q = castle.getUnitTypeNameInProductionQueAt(0); | |
if (!castle.getUnitTypeNameInProductionQueAt(1) && gold > 60){ | |
scope.order("Train Worker", [castle]); | |
console.log("train new worker"); | |
} | |
}); | |
//no barracks should be idle | |
barracks.forEach(function(barrack){ | |
if (!barrack.getUnitTypeNameInProductionQueAt(1) && gold > 80 && !barrack.unit.isUnderConstruction){ | |
scope.order("Train Rifleman", [barrack]); | |
console.log("train new Rifleman"); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment