From cd6eb8a5728eee506c9677980236fefbf78d609e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Houbart?= <cedric@scil.coop>
Date: Fri, 5 Jun 2015 11:09:54 +0200
Subject: [PATCH] GetBestAttack replacement (stand ground response)
---
.../mods/public/simulation/components/Attack.js | 35 +++++++++++++++++-----
.../mods/public/simulation/components/UnitAI.js | 12 ++------
.../simulation/components/tests/test_UnitAI.js | 2 --
3 files changed, 30 insertions(+), 19 deletions(-)
diff --git a/binaries/data/mods/public/simulation/components/Attack.js b/binaries/data/mods/public/simulation/components/Attack.js
index 1d78bfa..4c512a4 100644
a
|
b
|
Attack.prototype.GetPreference = function(target)
|
309 | 309 | }; |
310 | 310 | |
311 | 311 | /** |
312 | | * Return the type of the best attack. |
313 | | * TODO: this should probably depend on range, target, etc, |
314 | | * so we can automatically switch between ranged and melee |
| 312 | * Get the full range of attack regarding available attack types. |
315 | 313 | */ |
316 | | Attack.prototype.GetBestAttack = function() |
| 314 | Attack.prototype.GetFullAttackRange = function() |
317 | 315 | { |
318 | | return this.GetAttackTypes().pop(); |
319 | | }; |
| 316 | let ret = {"min": Infinity, "max": 0}; |
| 317 | let types = this.GetAttackTypes(); |
| 318 | for (let key in types) |
| 319 | { |
| 320 | let type = types[key]; |
| 321 | if (type != "Slaughter") |
| 322 | { |
| 323 | // Ignore the special attack "slaughter" dedicated to domestic animals |
| 324 | let range = this.GetRange(type); |
| 325 | if (range.min < ret.min) |
| 326 | ret.min = range.min; |
| 327 | if (range.max > ret.max) |
| 328 | ret.max = range.max; |
| 329 | } |
| 330 | } |
| 331 | return ret; |
| 332 | } |
320 | 333 | |
321 | 334 | Attack.prototype.GetBestAttackAgainst = function(target, allowCapture) |
322 | 335 | { |
323 | 336 | var cmpFormation = Engine.QueryInterface(target, IID_Formation); |
324 | 337 | if (cmpFormation) |
325 | | return this.GetBestAttack(); |
| 338 | { |
| 339 | // TODO: Formation against formation needs review |
| 340 | let best = ["Ranged", "Melee", "Capture"]; |
| 341 | let types = this.GetAttackTypes(); |
| 342 | for (let key in best) |
| 343 | if (types.indexOf(best[key]) != -1) |
| 344 | return best[key]; |
| 345 | return undefined; |
| 346 | } |
326 | 347 | |
327 | 348 | var cmpIdentity = Engine.QueryInterface(target, IID_Identity); |
328 | 349 | if (!cmpIdentity) |
diff --git a/binaries/data/mods/public/simulation/components/UnitAI.js b/binaries/data/mods/public/simulation/components/UnitAI.js
index 5fc38b6..7ebdad8 100644
a
|
b
|
UnitAI.prototype.CheckTargetIsInVisionRange = function(target)
|
4466 | 4466 | return distance < range; |
4467 | 4467 | }; |
4468 | 4468 | |
4469 | | UnitAI.prototype.GetBestAttack = function() |
4470 | | { |
4471 | | var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); |
4472 | | if (!cmpAttack) |
4473 | | return undefined; |
4474 | | return cmpAttack.GetBestAttack(); |
4475 | | }; |
4476 | | |
4477 | 4469 | UnitAI.prototype.GetBestAttackAgainst = function(target, allowCapture) |
4478 | 4470 | { |
4479 | 4471 | var cmpAttack = Engine.QueryInterface(this.entity, IID_Attack); |
… |
… |
UnitAI.prototype.GetQueryRange = function(iid)
|
5466 | 5458 | var cmpRanged = Engine.QueryInterface(this.entity, iid); |
5467 | 5459 | if (!cmpRanged) |
5468 | 5460 | return ret; |
5469 | | var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetRange(cmpRanged.GetBestAttack()); |
| 5461 | var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange(); |
5470 | 5462 | ret.min = range.min; |
5471 | 5463 | ret.max = range.max; |
5472 | 5464 | } |
… |
… |
UnitAI.prototype.GetQueryRange = function(iid)
|
5483 | 5475 | var cmpRanged = Engine.QueryInterface(this.entity, iid); |
5484 | 5476 | if (!cmpRanged) |
5485 | 5477 | return ret; |
5486 | | var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetRange(cmpRanged.GetBestAttack()); |
| 5478 | var range = iid !== IID_Attack ? cmpRanged.GetRange() : cmpRanged.GetFullAttackRange(); |
5487 | 5479 | var cmpVision = Engine.QueryInterface(this.entity, IID_Vision); |
5488 | 5480 | if (!cmpVision) |
5489 | 5481 | return ret; |
diff --git a/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js b/binaries/data/mods/public/simulation/components/tests/test_UnitAI.js
index 7e7df8d..90923e8 100644
a
|
b
|
function TestFormationExiting(mode)
|
97 | 97 | |
98 | 98 | AddMock(unit, IID_Attack, { |
99 | 99 | GetRange: function() { return { "max": 10, "min": 0}; }, |
100 | | GetBestAttack: function() { return "melee"; }, |
101 | 100 | GetBestAttackAgainst: function(t) { return "melee"; }, |
102 | 101 | GetTimers: function() { return { "prepare": 500, "repeat": 1000 }; }, |
103 | 102 | CanAttack: function(v) { return true; }, |
… |
… |
function TestMoveIntoFormationWhileAttacking()
|
248 | 247 | |
249 | 248 | AddMock(unit + i, IID_Attack, { |
250 | 249 | GetRange: function() { return {"max":10, "min": 0}; }, |
251 | | GetBestAttack: function() { return "melee"; }, |
252 | 250 | GetBestAttackAgainst: function(t) { return "melee"; }, |
253 | 251 | GetTimers: function() { return { "prepare": 500, "repeat": 1000 }; }, |
254 | 252 | CanAttack: function(v) { return true; }, |