all repos — uStrat @ 9c9f67ae9fbec0cf1faf3b364eb028507642612f

simple turn-based strategy game inspired by uCity, Super Robot Wars, C&C, Fire Emblem

starting the AI. rudimentary structure building algorithm is in place
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQJDBAABCAAtFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAl0K0wsPHG5pbGl4QG5p
bGZtLmNjAAoJEDt/vCIUTmOYjToP/A3Pm7ru6kmqom/uqtvsXVSQudk8C8EIb9Xy
ESYU2NI9ghsNmFil50vEr5GTWHZUt4jk2mgIZpyC5fGhhE2EG/Tay00uZMR/wrHm
+Ea/6XkwLnMEWsujso7PSHkvACoxPoZiVSW8Mg7NJ1tf3b//4UrO29RDqsbOdZYp
vg492ASb/UUaUpTVoKVa5D/npCOjEk2tp3Qgnn+4VkDdC9ZH8Ux+umDHOVHo9vWq
039wEamvdRb+xPtU9bpaJLjIRFT30YPoi3/3aXPnSjcpipzVVUWHvtQyyy3H7laF
9L5QG4cioVKMh0i1FshZZ55NsDynbkz0FFnvM7N/d2/elmNuIg3ucdFYETEfY8lI
+ML/QiT7bJ/NlHPa94L6OuNLVHLHF4RkBem64bpq5ZmlkDZNAUlChiDqVgKrfq9o
2LBaAfv1rwpMwazUMoExxdJ6021ehBUuT+JQ/wV5wB4O44TteJx2LFY23Db11yH7
wpaxo873TTnl3lOc3kKy2XC1qrEuJVCT2Hh/e3g3D2bZrpbPC6bYrAUX7is4TRQP
ZhoiL+TJK9D+qaawb1wPX8MK0bj/xju2aWv9NJMkKS9CzTiYGbD0puHBA0lqR1Sq
MFhlP5B3dV3wJD3Tne0pSNhex+Nz9TCYNg59AeAzeP4P9mF1cgDK9rH/hdA6PGrE
QqQkGVeG
=WIkC
-----END PGP SIGNATURE-----
commit

9c9f67ae9fbec0cf1faf3b364eb028507642612f

parent

53886275d1056bfdd74a0ed2c83a4c23cd2446c9

4 files changed, 163 insertions(+), 5 deletions(-)

jump to
M AI.jsAI.js

@@ -1,5 +1,159 @@

AI = {}; +AI.threats = {}; +AI.morale = {}; +AI.losses = 0; +AI.mode = "frugal"; +AI.modes = [ "catchup", "frugal", "aggressive", "exploratory", "endgame" ]; + +AI.rand = function(i) +{ + return Math.floor(Math.random()*i); +} + +AI.getMorale = function() +{ + var x, y, z; + x = teams.cpu.energy - teams.p1.energy; + y = teams.cpu.units.length - teams.p1.units.length; + z = teams.cpu.structs.length - teams.p1.structs.length; + w = new Object(); + w.energy = x; + w.units = y; + w.structs = z; + AI.morale = w; +} + +AI.deliberatePriority = function() +{ + if (AI.morale.energy < teams.p1.energy/4) + { + AI.mode = "catchup"; + } + else if (AI.morale.structs < 8) + { + AI.mode = "frugal"; + } + if (AI.mode != "catchup") + { + if (AI.morale.units >= 0 ) + { + AI.mode = "exploratory"; + } + else { AI.mode = "aggressive" }; + if (AI.morale.units > 20 && AI.morale.structs > 20) + { + AI.mode = "endgame"; + } + } +} + +AI.findNewStruct = function() +{ + var origin = {}; + origin.x = teams.cpu.structs[0].x; + origin.y = teams.cpu.structs[0].y; + var i, j, w; + w = AI.rand(4); + switch(w) + { + case 0: + for (i = 0; i < 64; i++) + { + for (j = 0; j < 64; j++) + { + if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure)) + { + console.log(i + ", " + j); return map.data[i][j]; + } + } + } + break; + case 1: + for (i = 63; i >= 0; i--) + { + for (j = 63; j >= 0; j--) + { + if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure)) + { + console.log(i + ", " + j); return map.data[i][j]; + } + } + } + break; + case 2: + for (i = 63; i >= 0; i--) + { + for (j = 0; j < 64; j++) + { + if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure)) + { + console.log(i + ", " + j); return map.data[i][j]; + } + } + } + break; + case 3: + for (i = 0; i < 64; i++) + { + for (j = 63; j >= 0; j--) + { + if (dist(origin, map.data[i][j]) < teams.cpu.energy/15 && isEmptyObject(map.data[i][j].structure)) + { + console.log(i + ", " + j); return map.data[i][j]; + } + } + } + break; + } +} + +AI.buildStruct = function() +{ + var cell = this.findNewStruct(); + mapCursor.x = cell.x; + mapCursor.y = cell.y; + switch (cell.type) + { + case "forest": + case "city": + map.degradeCell(map.data[cell.x][cell.y]); + case "plain": + if (teams.cpu.energy >= 40) + { + if (AI.rand(4) < 2) + { + if (AI.rand(4) < 2) + { + buildFactory(); + } + else buildArmory(); + } + else buildSolar(); + } + else if (teams.cpu.energy >= 20) { buildSolar(); } + break; + case "water": + if (teams.cpu.energy >= 30) + { + buildTidal(); + } + break; + case "mountain": + if (teams.cpu.energy >= 30) + { + buildMine(); + } + else if (teams.cpu.energy >= 20) + { + buildSolar(); + } + break; + } +} + AI.main = function() { +// this.deliberatePriority(); + this.buildStruct(); endPhase(); }
M Engine.jsEngine.js

@@ -59,7 +59,7 @@ {

case "p1": tm = teams.p1; gameState.phase = "cpu"; - resetFlow(); + gameState.flow = "cpuPhase"; break; case "cpu": tm = teams.cpu;

@@ -186,6 +186,9 @@ attackAnim();

break; case "attackEnd": attackEnd(); + break; + case "cpuPhase": + AI.main(); break; }
M Map.jsMap.js

@@ -214,10 +214,10 @@ tiles.mountain.src = 'assets/mapbg/mountain.png';

} -map.cell = function() +map.cell = function(x, y) { - this.x = 0; - this.y = 0; + this.x = x; + this.y = y; this.sprite = {}; this.agiMod = 0; this.defMod = 0;

@@ -323,7 +323,7 @@ for (i = 0; i < map.sz; i++)

{ for (j = 0; j < map.sz; j++) { - map.data[i][j] = new map.cell(); + map.data[i][j] = new map.cell(i, j); map.changeCell(map.data[i][j], "plain"); } }
M index.htmlindex.html

@@ -181,6 +181,7 @@ <script type="text/javascript" src="Menu.js"></script>

<script type="text/javascript" src="Movement.js"></script> <script type="text/javascript" src="Attack.js"></script> <script type="text/javascript" src="MiniMap.js"></script> + <script type="text/javascript" src="AI.js"></script> <script type="text/javascript"> function main() {