all repos — uStrat @ bd7f6f259bff3b8151fb9ac19eb6092cf3cdf35d

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

cpu units head for armory first thing to get equipped (equip/upgrade code forthcoming). if no cpu armory, they stay chillin
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQJDBAABCAAtFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAl1LuEAPHG5pbGl4QG5p
bGZtLmNjAAoJEDt/vCIUTmOYB1sP/jvEKlvK+lrplefdm1T/7xmYUA+6yP7fzGNx
85dezWzTM16BibYlhYCzaz7JOhPyLhMzz/XxI+05ciD/MOFlC/2BO2V6hoQQRhDc
iFw7eO+Z3IpEYtgyniDV2Vpqp+R51DvzTVp4q42+1GoalXeUugLxzKFEfjzD+Z7m
9N5glhG2vymIqfn8M3ZZcGdB6dDB7RnPKc/BlSnE/SA9/ii58wGu7ZoppO8QlB8Z
oFrA8oBUSsnjRdAPpJ1O5/FQk/FKkRlkn471V6L/u+MBe7TiHjPVrWc6a/HUc3Tm
QmrhJ9kDBJ3/LmacJJstqDz4F87qRDaAtaQVeiIWfKFcJ7FrymBXugZ1XAo6TlHX
Q5shjr/D1HB7brsISQbjMa3EZEu3IIl1K5vw+hK+oyR00R097yQ9rUbAfHvvDwSO
2Tj+Xq5XNb06Gyja/eydsNLd/9I0WRHu3DsbxYlBoFBComR5NXOgtE/w/Tt0jYMz
OocpV816qQUbZyvGjn14DV6uHnzyrsorCRGaKLn5a+MJbCehKD+ij6lWuRZ44Mhq
Ag89Ej67Nxo6Au9V1uc0eo/0boLePxJxG0c1AdEwnYNQKLKX5mXwvxiGjxH1ywPZ
oWdq/ECj6X22cioO1WTERpfRNhg7N2Df4Tyv9xs0isVdOSPTvvoIOVxzOlaGWdoo
3oP68JQ8
=kZd2
-----END PGP SIGNATURE-----
commit

bd7f6f259bff3b8151fb9ac19eb6092cf3cdf35d

parent

a630b7683012d6cb73f072f829f4171343c06e09

3 files changed, 56 insertions(+), 6 deletions(-)

jump to
M AI.jsAI.js

@@ -12,9 +12,10 @@ {

return Math.floor(Math.random()*i); } -AI.assignTarget = function(self, target) +AI.assignTarget = function(self, target, mode) { self.target = target; + self.mode = mode; } AI.getBearing = function(self, target)

@@ -65,6 +66,30 @@ }

} +AI.getClosestArmory = function(u) +{ + var i, j, k, A; + A = -1; + j = 99999; + for (i = 0; i < teams.cpu.structs.length; i++) + { + if (teams.cpu.structs[i].name == "cpu armory") + { + k = dist(teams.cpu.structs[i], u); + if (k < j) + { + j = k; + A = i; + } + } + } + if (A >= 0) + { + return teams.cpu.structs[A]; + } + else return u; +} + AI.generatePath = function(u) { movement.init(u);

@@ -73,6 +98,18 @@ var i, j, k, b, w, x, y, z, q, last;

z = "nsew"; last = ""; i = 0; + if (movement.unit.mode == "defend" && dist(movement.unit.target, movement.unit) == 0) + { + return; + } + else if (!isEmptyObject(movement.unit.equipment) && dist(movement.unit.target, movement.unit) <= movement.unit.equipment.range) + { + return; + } + else if (dist(movement.unit.target, movement.unit) <= 1) + { + return; + } for (;;) { b = AI.getBearing(movement.chain[movement.chain.length -1], movement.unit.target);

@@ -99,8 +136,9 @@ case 'w':

w = map.data[x - 1][y]; break; } - while (w.agiMod + 1 >= Math.sqrt(dist(movement.chain[movement.chain.length - 1], movement.unit.target))/2 || !isEmptyObject(w.unit)) + while ((w.agiMod + 1 >= Math.sqrt(dist(movement.chain[movement.chain.length - 1], movement.unit.target))/2 && dist(movement.chain[movement.chain.length - 1], movement.unit.target) >= 10) || !isEmptyObject(w.unit)) { + i++; if (isEmptyObject(w.unit)) { // if agiMod of all panels surrounding unit are equal, just keep original path

@@ -121,7 +159,6 @@ console.log(" trying to go another way");

q = z[AI.rand(4)]; i++; } while (q == k || q == AI.oppositeDir(last) || i <= 20); - i = 0; switch (q) { case 'n':

@@ -334,7 +371,7 @@ buildArmory();

} else if (AI.rand(4) < 2) { - if (AI.countStructs("cpu factory") <= AI.countStructs("cpu armory")) + if (AI.countStructs("cpu factory") < AI.countStructs("cpu armory")) { buildFactory(); }
M Engine.jsEngine.js

@@ -143,7 +143,7 @@ {

switch (gameState.phase) { case "cpu": - gameState.flow = "cpuManageUnits"; + gameState.flow = "cpuAttackReady" break; case "p1": gameState.flow = "freeLook";

@@ -181,7 +181,14 @@ {

var i; for (i = 0; i < teams.cpu.units.length; i++) { - AI.assignTarget(teams.cpu.units[i], map.data[16][16]); + if (isEmptyObject(teams.cpu.units[i].equipment)) + { + AI.assignTarget(teams.cpu.units[i], AI.getClosestArmory(teams.cpu.units[i]), "defend"); + } + else + { + AI.assignTarget(teams.cpu.units[i], map.data[16][16]); + } } gameState.flow = "cpuPathGen"; }
M Movement.jsMovement.js

@@ -148,6 +148,12 @@

function moveToTile(unit, tile) { var oX, oY; + + if (dist(unit, tile) < 1) + { + return; + } + oX = unit.x; oY = unit.y; map.data[tile.x][tile.y].unit = unit;