all repos — uStrat @ 36d62670c1002815e0f2e5e25db4d71769b02e43

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

update/fix targeting algorithms
Iris Lightshard nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQJDBAABCAAtFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAl5DHXAPHG5pbGl4QG5p
bGZtLmNjAAoJEDt/vCIUTmOYaagP/2DiMSCFRdPDntfCp85eVgNWlud/Q7D38sD/
IoPNsJE2uPb5lUzgSdqAJEraHwxLX9x+umL/PZqOE5RIp9vZfC+53KK+mz8qvrTF
/RwfChJPvkUL2cGT+SF3xW7Sgh8P3r0y0lfvUkFWBTf3ht6Vu6xSVSTLiouz5qyF
83ZIgIgBoJxx22w1S3Qy/MijQMZGz4oNPwE4AKFwduZqXzp05RpszqLmcMPVHMqe
adtmnXm936DM06XlYLJcBuHDHIkYlPmexMAiHdQjcDrJqN82HEXy+ywqJ7DA7LKH
xEDnFIXEGWPQzj5ouOmdwnxWQvDNqvXSA0gMrE3cERNPUH1a5V9qoNepwAKHTwl5
0FQM8XUHlElXzMNk1MRb0oBWElCbq0eYr+ggZRalSFwT+ZfXgdb7j7+w45lesfN2
v8vV8C1IDp8XbgNGLRHZZ/T4+ZKzEcTWlmuBqLQlsfahf4v+3PrfJu95dycp6rXf
DbELM+1WMktOnZZ06FfghdQq3Q2CVk35nfU7Lnem41TIYZXcpC3IOHlSAyoWXhSt
vkOVOrFkf+J7YOn52NUQ3q0QVoBqYQajbQng8QdEtjB9UK+s0hsupu/MdtGe/skt
Tnx+EFE75uhlZRQ0OTdFyXTiFjyKthZAVRxIsBwHUDOnk+GPzd7HX5CNsBuY6uwD
MFR6Z67/
=AYY4
-----END PGP SIGNATURE-----
commit

36d62670c1002815e0f2e5e25db4d71769b02e43

parent

abd33b2eed75765634891dd5157d81acd19fb300

2 files changed, 63 insertions(+), 10 deletions(-)

jump to
M AI.jsAI.js

@@ -31,16 +31,13 @@ AI.setTargetType = function()

{ var x = teams.cpu.units[AI.focus]; var w = AI.rand(2); - var z = AI.rand(5); + var z = AI.rand(3); switch (z) { case 0: - case 1: x.targetType = "city"; break; - case 2: - case 3: - case 4: + case 1: if (teams.p1.units.length > 0) { switch (w)

@@ -55,6 +52,8 @@ }

} else teams.cpu.units[AI.focus].targetType = "struct"; break; + case 2: + x.targetType= "defend" } }

@@ -66,13 +65,19 @@ switch (x.targetType)

{ case "city": x.target = AI.findSafeCity(); + x.mode = "attack"; break; case "unit": x.target = AI.selectEnemyUnit(); + x.mode = "attack"; break; case "struct": x.target = AI.selectEnemyStruct(); + x.mode = "attack"; break; + case "defend": + x.target = AI.selectOwnStruct(); + x.mode = "defend'; } }

@@ -115,9 +120,7 @@ u = -1

j = 0; for (i = 0; i < teams.cpu.units.length; i++) { - if (isEmptyObject(teams.cpu.units[i].equipment)) - continue; - k = teams.cpu.units[i].hp*(teams.cpu.units[i].pow + teams.cpu.units.equipment.grade)/teams.cpu.units[i].def; + k = teams.p1.units[i].hp*(teams.p1.units[i].pow + teams.p1.units.equipment.grade)/teams.p1.units[i].def; if (k > j) { k = j;

@@ -125,7 +128,7 @@ u = i;

} } if (u >= 0) - return teams.cpu.units[u]; + return teams.p1.units[u]; else return map.data[16][16]; }

@@ -137,11 +140,54 @@ var i, j, k, s;

j = 0; s = -1; + for (i = 0; i < teams.p1.structs.length; i++) + { + k = teams.p1.structs[i].hp/teams.p1.structs[i].def; + if (k > j) + { + j = k; + s = i; + } + } + if (s > 0) + return teams.p1.structs[u]; + else + return teams.p1.structs[0]; + +} + +AI.selectOwnStruct = function() +{ + // rank structs by HP/DEF + var i, j, k, s, x; + j = 0; + s = -1; + var skip = false; + for (i = 0; i < teams.cpu.structs.length; i++) { + skip = false; + + // if struct is already defense target of another AI unit, skip it! + + for (x = 0; x < teams.cpu.units.length; x++) + { + if ( !isEmptyObject(teams.cpu.units[x].target && + teams.cpu.units[x].target.x = teams.cpu.structs[i].x && + teams.cpu.units[y].target.y = teams.cpu.structs[i].y) + { + skip = true; + break; + } + } + if (skip) + continue; + k = teams.cpu.structs[i].hp/teams.cpu.structs[i].def; if (k > j) { + // if struct is already defense target of another AI unit, skip it! + // {code} j = k; s = i; }
M Engine.jsEngine.js

@@ -229,9 +229,13 @@ if (AI.focus >= teams.cpu.units.length)

{ AI.focus = 0; AI.manageEquipment(); - endPhase(); + gameState.flow = "cpuAttack"; } else gameState.flow = "cpuGenTargets"; +} + +function cpuAttack() +{ } function loseScreen()

@@ -314,6 +318,9 @@ break;

case "cpuMoveDone": cpuMoveDone(); break; + case "cpuAttack": + cpuAttack(); + break; case "youWin": winScreen(); break;