all repos — uStrat @ b2d31962065bd666ca09b2ecf3e810702787af00

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

debugging AI path generation and movement
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQJDBAABCAAtFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAl3z3jAPHG5pbGl4QG5p
bGZtLmNjAAoJEDt/vCIUTmOYIikP/2QjxBVYeGXpFL6MpF6R2bIeHrAkGgMvMb/X
JAOXfoH1R/VV3W1nKL49VSHpO2cDnO3FhrfKa9HrCORLDpjTHtPYi0jamE8vvxtS
kC+wLdPMqtMP3uENq1KEKDjCCcfvRuorp/QX4lXGLo1lr7lwaogNUPy8/3QwO4MA
bX8I4Ax9Pc/SZ+e2tInmcVemvBEcMrogtiye39bUO+qmG47UbNUcCAC1v4FOouSW
gToAJgskup6EUelf5d0QoSjNOldqcIiUyVbO4CV7jBQmOQ9pXqHhVBNhb9mEhq3r
XkjVTeneFf48+zXjXL0a8iRuPBw+nvBtHdwBB4JEggxwHuQgAFYZ+Tw6BZcgz7yA
tU+EVjH+NtDYacn0fgJfn0CDWmU4wameSzuwP0Z2hEdMk88/l2vglywdL+UOsZ/u
no4KOVPBbIpH1PTqvtrnMVwfWOynkhaAaM2DQF0zZmJgCPtx+Syj9s6fya77Lkre
NKkDxDu+cpKVu0PzuW/tQI2RpABrNPmZqSuLmYnSLZaOqRTQ4wR4/raRHvviB+Vz
kUTl9wybT+CQZebiqxsnrxX0lj5VhkK9QZWWNXo1Z1kprZc7r5QgULPlTUTuFfG2
Cstqk78CkhQLGEDXsMfVNcv7kxJ2OgfSa7tKsN85KkFICd6LH5oZc+bEttKJaN95
zMb5VpWM
=5V4p
-----END PGP SIGNATURE-----
commit

b2d31962065bd666ca09b2ecf3e810702787af00

parent

660e1c96e377402845d42abf38351f30a5e59bf0

4 files changed, 117 insertions(+), 17 deletions(-)

jump to
M AI.jsAI.js

@@ -104,7 +104,7 @@ }

} if (tX >= 0 && tY >= 0) return map.data[tX][tY]; - else return {}; + else return map.data[16][16]; } AI.selectEnemyUnit = function()

@@ -238,6 +238,80 @@ equipOrUpgradeHellBomb();

} break; } + if (!isEmptyObject(unit.equipment)) + { + unit.target = {}; + } +} + +AI.generateSimplePath = function(u) +{ + movement.init(u); + console.log("AI movement init"); + 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 (movement.unit.mode != "defend" && dist(movement.unit.target, movement.unit) <= 1) + { + return; + } + for (;;) + { + 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); + 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]; + break; + case 's': + w = map.data[x][y + 1]; + break; + case 'e': + w = map.data[x + 1][y]; + break; + case 'w': + w = map.data[x - 1][y]; + break; + } + last = k; + if (movement.unit.name == "cpu BattleAngel") + { + if (movement.pool >= 1) + { + movement.pool -= 1; + movement.chain.push(new Coords(w.x, w.y)); + console.log(movement.chain); + } + else return; + } + else + { + 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; + } + } } AI.generatePath = function(u)

@@ -299,6 +373,10 @@ {

console.log(" surrounded by high-cost cells, going straight ahead anyway"); break; } + k = w; + w = map.data[w + AI.rand(3) - 1][w + AI.rand(3) - 1]; + if (w.agiMod < k.agiMod) + break; } console.log(" going directly to target not ideal");

@@ -530,7 +608,7 @@ map.degradeCell(map.data[cell.x][cell.y]);

case "plain": if (teams.cpu.energy >= 40) { - if ((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) + if ((teams.cpu.units.length/teams.p1.units.length <= 1.5 && AI.countStructs("cpu factory") == 0)) { buildFactory() }
M Engine.jsEngine.js

@@ -180,24 +180,27 @@ }

function cpuGenTargets() { + AI.incrementAttention(); + if (teams.cpu.units[AI.focus].attentionSpan == 0) + { + teams.cpu.units[AI.focus].target = {}; + } if (isEmptyObject(teams.cpu.units[AI.focus].equipment)) { AI.assignTarget(teams.cpu.units[AI.focus], AI.getClosestArmory(teams.cpu.units[AI.focus]), "defend"); } else if (isEmptyObject(teams.cpu.units[AI.focus].target)) { - AI.getPriorityTarget(teams.cpu.units[AI.focus]) + AI.setTargetType(); + AI.findTarget(); } - else - { - AI.incrementAttention(); - } + console.log("unit: " + AI.focus + "/" + teams.cpu.units[AI.focus].name + " - " + teams.cpu.units[AI.focus].mode + " " + teams.cpu.units[AI.focus].target.x + "," + teams.cpu.units[AI.focus].target.y); gameState.flow = "cpuPathGen"; } function cpuManagePath() { - AI.generatePath(teams.cpu.units[AI.focus]); + AI.generateSimplePath(teams.cpu.units[AI.focus]); gameState.flow = "cpuMove"; }
M Equipment.jsEquipment.js

@@ -53,7 +53,7 @@ function equipOrUpgradeBeamSabreMKI()

{ var unit = map.data[mapCursor.x][mapCursor.y].unit; var m = false; - if (unit.equipment.name != "Beam Sabre mkI") + if (!isEmptyObject(unit.equipment) || unit.equipment.name != "Beam Sabre mkI") { unit.equipment = beamSabreMKI(); }

@@ -100,7 +100,7 @@ function equipOrUpgradeRifleMKI()

{ var unit = map.data[mapCursor.x][mapCursor.y].unit; var m = false; - if (unit.equipment.name != "Rifle mkI") + if (!isEmptyObject(unit.equipment) || unit.equipment.name != "Rifle mkI") { unit.equipment = rifleMKI(); }

@@ -145,7 +145,7 @@ function equipOrUpgradeBeamCannon()

{ var unit = map.data[mapCursor.x][mapCursor.y].unit; var m = false; - if (unit.equipment.name != "Beam Cannon") + if (!isEmptyObject(unit.equipment) || unit.equipment.name != "Beam Cannon") { unit.equipment = beamCannon(); }

@@ -190,7 +190,7 @@ function equipOrUpgradeHellBomb()

{ var unit = map.data[mapCursor.x][mapCursor.y].unit; var m = false; - if (unit.equipment.name != "Hell Bomb") + if (!isEmptyObject(unit.equipment) || unit.equipment.name != "Hell Bomb") { unit.equipment = hellBomb(); }
M Movement.jsMovement.js

@@ -121,8 +121,7 @@ movement.animCounter++;

if (movement.animCounter == 10) { movement.animCounter = 0; - if ((!isEmptyObject(movement.unit.equipment) && dist(movement.unit.target, movement.unit) > movement.unit.equipment.range) || movement.chain.length > 1 ) - + if (movement.chain.length > 1) { movement.chain.shift(); }

@@ -141,14 +140,30 @@ AI.focus++;

gameState.flow = "cpuMoveDone"; } return; } - moveToTile(movement.unit, movement.chain[0]); + if (movement.chain.length > 0 && movement.unit.target != undefined) + moveToTile(movement.unit, movement.chain[0]); + else{ + movement.unit.hasMoved = true; + movement.unit = {}; + movement.chain = new Array(); + movement.pool = 0; + if (gameState.phase == "p1") + { + gameState.flow = "freeLook"; + } + else { + AI.focus++; + gameState.flow = "cpuMoveDone"; } + return; + } } } function moveToTile(unit, tile) { - var oX, oY; + var oX, oY, tmp; + tmp = {}; if (dist(unit, tile) < 1) { return;

@@ -156,8 +171,12 @@ }

oX = unit.x; oY = unit.y; + if (!isEmptyObject(map.data[tile.x][tile.y].unit)) + { + tmp = map.data[tile.x][tile.y].unit; + } map.data[tile.x][tile.y].unit = unit; unit.x = tile.x; unit.y = tile.y; - map.data[oX][oY].unit = {} + map.data[oX][oY].unit = tmp; }