AI can move units toward their targets; trying to figure out how to animate it
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE----- iQJDBAABCAAtFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAl1HzjUPHG5pbGl4QG5p bGZtLmNjAAoJEDt/vCIUTmOY9msP/2Nvq+qPqX7+np5KTwlYAZkr3YE8/Oimk7ta daQYUGeViPil9WoxaLYrhOw99MvI4Ek44DcaYbl6oGf/3676oZYd6ht3CTncdt+H Ij+eFrV8zBZ3e+0zSYkLcLNGshWRO4moG6ePyJ9PC9PSXOSTfwr4BoVgcFOc4eIg hPZGcY8FUsJXZyWQm22lc4fiYB+OTDA010fpRggGLNFrxeKMRmf54yNVpHuYb8n2 tpGXjyyc0oDkbbhelTr0a36Wwf4wpc6MYy0EtjZfxxaTRxErIVCkCP+dgTwQJvca 1A65NlBi/G/fg8+ZY1T8uqY3a94wMhohYnbhz4kNmkoL0IJmE3AoYp45SOHcFMfF 9XnjCY++/HPlX15HQ2WbEe7uMOFXcmkH6+X3A5JAtbkJxyquF1xDi2YO9d6fzVwK A8pAtps7KSTaKhIoMMKe0EeMOlbN9RHqD6CTdIG7gCD+S8IQ4Z+pPgHOtQREtmN3 jXiQIKOIkwApEOOw+2SWwUuvizbCPa4vey6CpzXgyyhg/K5bSIh+OnnWFOYIU/wQ 1zJsXJYW/MtK/0Ri9zJ9u3OmbVBKJGxtqa258VpHWKlHl+ElAUJJfc++6mSEYSur EK/aWrgA6O0h7yBsN35Mc2vRPQvYJ7WCO0Vro8/duAVjXYUnEPlO2BeYrxz2kmMI b+ZXa2IG =VP6J -----END PGP SIGNATURE-----
3 files changed,
91 insertions(+),
33 deletions(-)
M
AI.js
→
AI.js
@@ -20,16 +20,16 @@ AI.getBearing = function(self, target)
{ var w, z, d; var bearing = ""; - w = self.x - target.x; - z = self.y - target.y; + w = target.x - self.x; + z = target.y - self.y; d = dist(self, target); if (z/d > 0) { - bearing += "n"; + bearing += "s"; } else if (z/d < 0) { - bearing += "s"; + bearing += "n"; } if (w/d > 0) {@@ -67,24 +67,24 @@ console.log("AI movement init");
var i, j, k, b, w, x, y, z, q, last; z = "nsew"; last = ""; - console.log(" trial run " + i); for (;;) { - b = getBearing(movement.chain[movement.chain.length -1], movement.unit.target); + b = AI.getBearing(movement.chain[movement.chain.length -1], movement.unit.target); do { console.log(" trying to go directly to target"); - j = AI.rand(b.length()); + j = AI.rand(b.length); k = b[j]; x = movement.chain[movement.chain.length - 1].x; y = movement.chain[movement.chain.length - 1].y; } while (k == AI.oppositeDir(last)); + console.log(" trying " + k); switch (k) { case 'n': - w = map.data[x][y + 1]; + w = map.data[x][y - 1]; break; case 's': - w = map.data[x][y - 1]; + w = map.data[x][y + 1]; break; case 'e': w = map.data[x + 1][y];@@ -93,21 +93,21 @@ case 'w':
w = map.data[x - 1][y]; break; } - while (w.agiMod + 1 >= 2*Math.sqrt(dist(movement.chain[movement.chain.length - 1], movement.unit.target || !isEmptyObject(w.unit)) + while (w.agiMod + 1 >= Math.sqrt(dist(movement.chain[movement.chain.length - 1], movement.unit.target))/2 || !isEmptyObject(w.unit)) { console.log(" going directly to target not ideal"); do { console.log(" trying to go another way"); - q = z[rand(4)]; + q = z[AI.rand(4)]; } while (q == k || q == AI.oppositeDir(last)); switch (q) { case 'n': - w = map.data[x][y + 1]; + w = map.data[x][y - 1]; break; case 's': - w = map.data[x][y - 1]; + w = map.data[x][y + 1]; break; case 'e': w = map.data[x + 1][y];@@ -128,6 +128,7 @@ if (movement.pool >= 1)
{ movement.pool -= 1; movement.chain.push(new Coords(w.x, w.y)); + console.log(movement.chain); } else return; }@@ -137,6 +138,7 @@ if (movement.pool >= w.agiMod + 1)
{ movement.pool -= w.agiMod + 1; movement.chain.push(new Coords(w.x, w.y)); + console.log(movement.chain); } else return; }@@ -301,7 +303,11 @@ map.degradeCell(map.data[cell.x][cell.y]);
case "plain": if (teams.cpu.energy >= 40) { - if (AI.rand(4) < 2) + if (teams.cpu.energy/teams.p1.energy > 2 || (teams.cpu.units.length/teams.p1.units.length <= 1.5 && AI.countStructs("cpu factory") == 0) || teams.cpu.structs.length/teams.p1.structs.length >= 1.5) + { + buildFactory() + } + else if (AI.rand(4) < 2) { if (AI.countStructs("cpu factory") <= AI.countStructs("cpu armory")) {@@ -350,10 +356,9 @@ }
} } } - AI.buildUnits = function() { - if (AI.rand(5) < 3) + if (AI.rand(5) < 1) { return; }@@ -363,7 +368,7 @@ if (AI.rand(4) < 1)
{ buildBlitzWalker(); } - else if (AI.rand(4) < 1) + else if (AI.rand(4) < 3) { buildBattleAngel(); }@@ -375,7 +380,7 @@ if (AI.rand(4) < 1)
{ buildBattleAngel(); } - else if (AI.rand(4) < 1) + else if (AI.rand(4) < 3) { buildAce(); }@@ -387,7 +392,7 @@ if (AI.rand(4) < 1)
{ buildAce(); } - else if (AI.rand(4) < 1) + else if (AI.rand(4) < 3) { buildKnight(); }@@ -399,7 +404,7 @@ if (AI.rand(4) < 1)
{ buildKnight(); } - else if (AI.rand(4) < 1) + else if (AI.rand(4) < 3) { buildRanger(); }@@ -407,7 +412,7 @@ else return;
} if (teams.cpu.energy >= 30) { - if (AI.rand(4) < 2) + if (AI.rand(4) < 3) { buildRanger(); }@@ -415,7 +420,31 @@ }
} -AI.actions = [ AI.cycleThruFactories, AI.buildStruct, AI.cycleThruFactories ]; +AI.testPath = function() +{ + var i, j, k; + for (i = 0; i < teams.cpu.units.length ; i++) + { + AI.assignTarget(teams.cpu.units[i], map.data[16][16]); + AI.generatePath(teams.cpu.units[i]); + while (gameState.flow != "moveFinished") + { + k = new Date(); + if (k - gameState.frame >= 33.34) + { + gameState.frame = k; + movement.go(); + camera.centerOnCursor(); + camera.manage(); + map.draw(); + mapCursor.draw(); + } + } + gameState.flow = "cpuPhase"; + } +} + +AI.actions = [ AI.buildStruct, AI.cycleThruFactories, AI.testPath ]; AI.animLoop = function() {
A
LICENSE
@@ -0,0 +1,25 @@
+Copyright (c) 2019, Derek Stevens +nilix@nilfm.cc +"PC Senior" font (c) codeman38 (zone38.net) + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must include source code and reproduce the + above copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +
M
Movement.js
→
Movement.js
@@ -117,24 +117,28 @@ }
movement.go = function() { - this.animCounter++; - if (this.animCounter == 10) + movement.animCounter++; + if (movement.animCounter == 10) { - this.animCounter = 0; - if (this.chain.length > 1) + movement.animCounter = 0; + if (movement.chain.length > 1) { - this.chain.shift(); + movement.chain.shift(); } else { - this.unit.hasMoved = true; - this.unit = {}; - this.chain = new Array(); - this.pool = 0; - gameState.flow = "freeLook"; + movement.unit.hasMoved = true; + movement.unit = {}; + movement.chain = new Array(); + movement.pool = 0; + if (gameState.phase == "p1") + { + gameState.flow = "freeLook"; + } + else { gameState.flow = "moveFinished"; } return; } - moveToTile(this.unit, this.chain[0]); + moveToTile(movement.unit, movement.chain[0]); } }