Changeset View
Standalone View
binaries/data/mods/public/simulation/components/UnitAI.js
function UnitAI() {} | function UnitAI() {} | ||||
Itms: This is not used | |||||
Done Inline Actionsright, forgotten placeholder. wraitii: right, forgotten placeholder. | |||||
UnitAI.prototype.Schema = | UnitAI.prototype.Schema = | ||||
"<a:help>Controls the unit's movement, attacks, etc, in response to commands from the player.</a:help>" + | "<a:help>Controls the unit's movement, attacks, etc, in response to commands from the player.</a:help>" + | ||||
"<a:example/>" + | "<a:example/>" + | ||||
"<element name='DefaultStance'>" + | "<element name='DefaultStance'>" + | ||||
"<choice>" + | "<choice>" + | ||||
"<value>violent</value>" + | "<value>violent</value>" + | ||||
▲ Show 20 Lines • Show All 1,646 Lines • ▼ Show 20 Lines | "GUARD": { | ||||
this.StopMoving(); | this.StopMoving(); | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
return; | return; | ||||
} | } | ||||
this.SetHeldPositionOnEntity(this.isGuardOf); | this.SetHeldPositionOnEntity(this.isGuardOf); | ||||
}, | }, | ||||
"leave": function(msg) { | "leave": function(msg) { | ||||
this.SetMoveSpeed(this.GetWalkSpeed()); | this.ResetMoveSpeed(); | ||||
this.StopTimer(); | this.StopTimer(); | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
}, | }, | ||||
"MoveStarted": function(msg) { | "MoveStarted": function(msg) { | ||||
// Adapt the speed to the one of the target if needed | // Adapt the speed to the one of the target if needed | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
if (cmpUnitMotion.IsInTargetRange(this.isGuardOf, 0, 3*this.guardRange)) | if (cmpUnitMotion.IsInTargetRange(this.isGuardOf, 0, 3*this.guardRange)) | ||||
{ | { | ||||
var cmpUnitAI = Engine.QueryInterface(this.isGuardOf, IID_UnitAI); | var cmpUnitAI = Engine.QueryInterface(this.isGuardOf, IID_UnitAI); | ||||
if (cmpUnitAI) | if (cmpUnitAI) | ||||
{ | { | ||||
var speed = cmpUnitAI.GetWalkSpeed(); | var speed = cmpUnitAI.GetWalkSpeed(); | ||||
if (speed < this.GetWalkSpeed()) | if (speed < this.GetWalkSpeed()) | ||||
this.SetMoveSpeed(speed); | this.SetMoveSpeedRatio(speed / this.GetWalkSpeed()); | ||||
Not Done Inline Actionsgood its re-added, but it should calculate the ratio I guess and use existing functions (I guess after the rewrite there could perhaps come a unitMotion.follow function or so, that treats this case, but for now just keep the current state) bb: good its re-added, but it should calculate the ratio I guess and use existing functions
(I… | |||||
} | } | ||||
} | } | ||||
}, | }, | ||||
Not Done Inline Actionswhat is (if there is) the reasoning in removing this hunk? The escorting behaviour of a fast unit escorting a slow one is definitely looking more ugly now. bb: what is (if there is) the reasoning in removing this hunk? The escorting behaviour of a fast… | |||||
Not Done Inline ActionsIf there was a reason, I forgot it. I'll add it back I guess. wraitii: If there was a reason, I forgot it. I'll add it back I guess. | |||||
Done Inline ActionsSounds good to nuke that :) fatherbushido: Sounds good to nuke that :) | |||||
"MoveCompleted": function() { | "MoveCompleted": function() { | ||||
this.SetMoveSpeed(this.GetWalkSpeed()); | this.ResetMoveSpeed(); | ||||
if (!this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | if (!this.MoveToTargetRangeExplicit(this.isGuardOf, 0, this.guardRange)) | ||||
this.SetNextState("GUARDING"); | this.SetNextState("GUARDING"); | ||||
}, | }, | ||||
}, | }, | ||||
"GUARDING": { | "GUARDING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
Show All 39 Lines | "GUARD": { | ||||
}, | }, | ||||
}, | }, | ||||
}, | }, | ||||
"FLEEING": { | "FLEEING": { | ||||
"enter": function() { | "enter": function() { | ||||
this.PlaySound("panic"); | this.PlaySound("panic"); | ||||
// Run quickly | // Run quickly | ||||
var speed = this.GetRunSpeed(); | |||||
this.SelectAnimation("move"); | this.SelectAnimation("move"); | ||||
Not Done Inline Actionsthese stuff could be inlined ofc bb: these stuff could be inlined ofc | |||||
this.SetMoveSpeed(speed); | this.SetMoveSpeedRatio(this.GetRunMultiplier()); | ||||
}, | }, | ||||
"HealthChanged": function() { | "HealthChanged": function() { | ||||
var speed = this.GetRunSpeed(); | this.SetMoveSpeedRatio(this.GetRunMultiplier()); | ||||
this.SetMoveSpeed(speed); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
// Reset normal speed | this.ResetMoveSpeed(); | ||||
this.SetMoveSpeed(this.GetWalkSpeed()); | |||||
}, | }, | ||||
"MoveCompleted": function() { | "MoveCompleted": function() { | ||||
// When we've run far enough, stop fleeing | // When we've run far enough, stop fleeing | ||||
this.FinishOrder(); | this.FinishOrder(); | ||||
}, | }, | ||||
// TODO: what if we run into more enemies while fleeing? | // TODO: what if we run into more enemies while fleeing? | ||||
▲ Show 20 Lines • Show All 267 Lines • ▼ Show 20 Lines | "COMBAT": { | ||||
// Show weapons rather than carried resources. | // Show weapons rather than carried resources. | ||||
this.SetAnimationVariant("combat"); | this.SetAnimationVariant("combat"); | ||||
this.SelectAnimation("move"); | this.SelectAnimation("move"); | ||||
var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); | var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); | ||||
if (cmpUnitAI && cmpUnitAI.IsFleeing()) | if (cmpUnitAI && cmpUnitAI.IsFleeing()) | ||||
{ | { | ||||
// Run after a fleeing target | // Run after a fleeing target | ||||
var speed = this.GetRunSpeed(); | this.SetMoveSpeedRatio(this.GetRunMultiplier()); | ||||
Not Done Inline Actionsmore inline bb: more inline | |||||
this.SetMoveSpeed(speed); | |||||
} | } | ||||
this.StartTimer(1000, 1000); | this.StartTimer(1000, 1000); | ||||
}, | }, | ||||
"HealthChanged": function() { | "HealthChanged": function() { | ||||
var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); | var cmpUnitAI = Engine.QueryInterface(this.order.data.target, IID_UnitAI); | ||||
if (!cmpUnitAI || !cmpUnitAI.IsFleeing()) | if (!cmpUnitAI || !cmpUnitAI.IsFleeing()) | ||||
return; | return; | ||||
var speed = this.GetRunSpeed(); | this.SetMoveSpeedRatio(this.GetRunMultiplier()); | ||||
Not Done Inline Actionsand here bb: and here | |||||
bbUnsubmitted Done Inline ActionsThe logic in the getRunspeed got removed so this code has become death I suppose (same for the other changed healthchanged stuff) bb: The logic in the getRunspeed got removed so this code has become death I suppose (same for the… | |||||
this.SetMoveSpeed(speed); | |||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
// Reset normal speed in case it was changed | // Reset normal speed in case it was changed | ||||
this.SetMoveSpeed(this.GetWalkSpeed()); | this.ResetMoveSpeed(); | ||||
// Show carried resources when walking. | // Show carried resources when walking. | ||||
this.SetDefaultAnimationVariant(); | this.SetDefaultAnimationVariant(); | ||||
this.StopTimer(); | this.StopTimer(); | ||||
}, | }, | ||||
"Timer": function(msg) { | "Timer": function(msg) { | ||||
if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType)) | if (this.ShouldAbandonChase(this.order.data.target, this.order.data.force, IID_Attack, this.order.data.attackType)) | ||||
▲ Show 20 Lines • Show All 1,065 Lines • ▼ Show 20 Lines | "IDLE": { | ||||
this.SetNextState("FEEDING"); | this.SetNextState("FEEDING"); | ||||
return true; | return true; | ||||
}, | }, | ||||
}, | }, | ||||
"ROAMING": { | "ROAMING": { | ||||
"enter": function() { | "enter": function() { | ||||
// Walk in a random direction | // Walk in a random direction | ||||
this.SelectAnimation("walk", false, this.GetWalkSpeed()); | this.SelectAnimation("walk", false, 1); | ||||
Done Inline Actionsno need for the .0 I guess bb: no need for the `.0` I guess | |||||
this.SetFacePointAfterMove(false); | this.SetFacePointAfterMove(false); | ||||
this.MoveRandomly(+this.template.RoamDistance); | this.MoveRandomly(+this.template.RoamDistance); | ||||
// Set a random timer to switch to feeding state | // Set a random timer to switch to feeding state | ||||
this.StartTimer(randIntInclusive(+this.template.RoamTimeMin, +this.template.RoamTimeMax)); | this.StartTimer(randIntInclusive(+this.template.RoamTimeMin, +this.template.RoamTimeMax)); | ||||
}, | }, | ||||
"leave": function() { | "leave": function() { | ||||
this.StopTimer(); | this.StopTimer(); | ||||
▲ Show 20 Lines • Show All 827 Lines • ▼ Show 20 Lines | |||||
UnitAI.prototype.OnPackFinished = function(msg) | UnitAI.prototype.OnPackFinished = function(msg) | ||||
{ | { | ||||
this.UnitFsm.ProcessMessage(this, {"type": "PackFinished", "packed": msg.packed}); | this.UnitFsm.ProcessMessage(this, {"type": "PackFinished", "packed": msg.packed}); | ||||
}; | }; | ||||
//// Helper functions to be called by the FSM //// | //// Helper functions to be called by the FSM //// | ||||
UnitAI.prototype.GetWalkSpeed = function() | UnitAI.prototype.GetWalkSpeed = function() | ||||
Not Done Inline Actionswe still need that function... bb: we still need that function... | |||||
{ | { | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
if (!cmpUnitMotion) | |||||
return 0; | |||||
return cmpUnitMotion.GetWalkSpeed(); | return cmpUnitMotion.GetWalkSpeed(); | ||||
Done Inline ActionsLooks like a forgotten placeholder. Itms: Looks like a forgotten placeholder. | |||||
Done Inline ActionsSetSpeed expects a ratio, so the walk ratio is "1.0", I should just change the name of the function. wraitii: SetSpeed expects a ratio, so the walk ratio is "1.0", I should just change the name of the… | |||||
Done Inline ActionsImo we could just nuke the GetWalkSpeed function and its 8 occurence in UnitAI and the two ones in the test ? fatherbushido: Imo we could just nuke the GetWalkSpeed function and its 8 occurence in UnitAI and the two ones… | |||||
Done Inline ActionsFair enough, done. wraitii: Fair enough, done. | |||||
}; | }; | ||||
UnitAI.prototype.GetRunSpeed = function() | UnitAI.prototype.GetRunMultiplier = function() | ||||
{ | { | ||||
var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | var cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
var runSpeed = cmpUnitMotion.GetRunSpeed(); | if (!cmpUnitMotion) | ||||
Done Inline ActionscmpUnitMotion is not a component every ent has, also not essentially required for having a unitAI, thus check for it bb: cmpUnitMotion is not a component every ent has, also not essentially required for having a… | |||||
Not Done Inline Actionsimo this should go in a separate diff, unitAI is entirely dependent on unitMotion as it stands now. There's far more cases than just this one. wraitii: imo this should go in a separate diff, unitAI is entirely dependent on unitMotion as it stands… | |||||
var walkSpeed = cmpUnitMotion.GetWalkSpeed(); | return 0; | ||||
if (runSpeed <= walkSpeed) | return cmpUnitMotion.GetRunSpeedMultiplier(); | ||||
Done Inline Actionstimes base? See also my comment to the source file header, which would prevent such oversights. Itms: times base? See also my comment to the source file header, which would prevent such oversights. | |||||
Done Inline ActionsSame issue, setSpeed expects a ratio so this returns the correct thing. I should probably just change the name. wraitii: Same issue, setSpeed expects a ratio so this returns the correct thing. I should probably just… | |||||
Not Done Inline ActionsWhat if no unitMotion? imo unitAI shouldn't be dependent on unitMotion, better just return 0 when no unitMotion (notice that is correct since those units can't move) count for more calls below bb: What if no unitMotion? imo unitAI shouldn't be dependent on unitMotion, better just return 0… | |||||
Not Done Inline ActionsThis is a whooooole other level of refactoring imo. wraitii: This is a whooooole other level of refactoring imo. | |||||
Done Inline Actions(have yelled earlier about those things iirc, but theoretically should need a cmpUnitMotion check, not adding missing ones in unrelated code if fine with me, but when we already change the code, it seems clean/easy enough to just add them) bb: (have yelled earlier about those things iirc, but theoretically should need a cmpUnitMotion… | |||||
return runSpeed; | |||||
var cmpHealth = Engine.QueryInterface(this.entity, IID_Health); | |||||
var health = cmpHealth.GetHitpoints()/cmpHealth.GetMaxHitpoints(); | |||||
Not Done Inline ActionsThat code is certainly misplaced, probably even outdated, so fine with removing it bb: That code is certainly misplaced, probably even outdated, so fine with removing it | |||||
return (health*runSpeed + (1-health)*walkSpeed); | |||||
Done Inline Actionsok to nuke that fatherbushido: ok to nuke that | |||||
Not Done Inline Actionsnot required to remove this code in theory, no strong opinion bb: not required to remove this code in theory, no strong opinion | |||||
}; | }; | ||||
/** | /** | ||||
* Returns true if the target exists and has non-zero hitpoints. | * Returns true if the target exists and has non-zero hitpoints. | ||||
*/ | */ | ||||
UnitAI.prototype.TargetIsAlive = function(ent) | UnitAI.prototype.TargetIsAlive = function(ent) | ||||
{ | { | ||||
var cmpFormation = Engine.QueryInterface(ent, IID_Formation); | var cmpFormation = Engine.QueryInterface(ent, IID_Formation); | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | UnitAI.prototype.SelectAnimation = function(name, once = false, speed = 1.0) | ||||
let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | let cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | ||||
if (!cmpVisual) | if (!cmpVisual) | ||||
return; | return; | ||||
// Special case: the "move" animation gets turned into a special | // Special case: the "move" animation gets turned into a special | ||||
// movement mode that deals with speeds and walk/run automatically | // movement mode that deals with speeds and walk/run automatically | ||||
if (name == "move") | if (name == "move") | ||||
{ | { | ||||
// Speed to switch from walking to running animations | // Speed to switch from walking to running animations | ||||
cmpVisual.SelectMovementAnimation((this.GetWalkSpeed() + this.GetRunSpeed()) / 2); | cmpVisual.SelectMovementAnimation(this.GetWalkSpeed()); | ||||
Done Inline ActionsImo something like cmpUnitMotion.GetBaseSpeed() * (1 - x + cmpUnitMotion.GetTopSpeedRatio() * x) fatherbushido: Imo something like cmpUnitMotion.GetBaseSpeed() * (1 - x + cmpUnitMotion.GetTopSpeedRatio() *… | |||||
Done Inline ActionsMade irrelevant by later patches iirc. wraitii: Made irrelevant by later patches iirc. | |||||
Done Inline Actionsok fatherbushido: ok
(but in case top speed multiplier < 1.2, you will never reach the threshold)
| |||||
Not Done Inline ActionsRight, my comment was more that I'm moving this to visual actor (iirc) later. Don't think it matters much anyhow. wraitii: Right, my comment was more that I'm moving this to visual actor (iirc) later. Don't think it… | |||||
Not Done Inline Actions(and check here too, and more below) bb: (and check here too, and more below) | |||||
Not Done Inline Actionsinline (1.2 is a magical number, but as everything >1 or so would work, meh) bb: inline
(1.2 is a magical number, but as everything >1 or so would work, meh) | |||||
return; | return; | ||||
Done Inline Actionswhy multiply with 1.2?if that looks better, it is an animation problem, not unitai bb: why multiply with 1.2?if that looks better, it is an animation problem, not unitai | |||||
} | } | ||||
cmpVisual.SelectAnimation(name, once, speed); | cmpVisual.SelectAnimation(name, once, speed); | ||||
}; | }; | ||||
UnitAI.prototype.SetAnimationSync = function(actiontime, repeattime) | UnitAI.prototype.SetAnimationSync = function(actiontime, repeattime) | ||||
{ | { | ||||
var cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | var cmpVisual = Engine.QueryInterface(this.entity, IID_Visual); | ||||
▲ Show 20 Lines • Show All 1,350 Lines • ▼ Show 20 Lines | UnitAI.prototype.GetSelectableStances = function() | ||||
return Object.keys(g_Stances).filter(key => g_Stances[key].selectable); | return Object.keys(g_Stances).filter(key => g_Stances[key].selectable); | ||||
}; | }; | ||||
UnitAI.prototype.GetStanceName = function() | UnitAI.prototype.GetStanceName = function() | ||||
{ | { | ||||
return this.stance; | return this.stance; | ||||
}; | }; | ||||
/* | |||||
Done Inline ActionsjsDoc, cap and makes => make bb: jsDoc, cap and `makes` => `make` | |||||
* Make the unit walk at its normal pace. | |||||
*/ | |||||
UnitAI.prototype.ResetMoveSpeed = function() | |||||
{ | |||||
Not Done Inline Actions1.0 => 1, IMO make clear we talk about a ratio here setting that in the function name SetSpeedRatio(foo) bb: 1.0 => 1, IMO make clear we talk about a ratio here setting that in the function name… | |||||
Not Done Inline ActionsRedundant in the C++ code as the parameter is named such, however the JS code could use it. wraitii: Redundant in the C++ code as the parameter is named such, however the JS code could use it. | |||||
Done Inline Actions-.0 bb: -`.0` | |||||
let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | |||||
cmpUnitMotion.SetSpeedRatio(1); | |||||
Not Done Inline Actionscheck for it bb: check for it | |||||
bbUnsubmitted Done Inline Actionscheck him bb: check him | |||||
}; | |||||
Done Inline Actionsif (cmpFoo) cmpFoo.doFoo(); bb: ```
if (cmpFoo)
cmpFoo.doFoo();
``` | |||||
UnitAI.prototype.SetMoveSpeed = function(speed) | UnitAI.prototype.SetMoveSpeedRatio = function(speed) | ||||
Not Done Inline ActionsSame bb: Same | |||||
{ | { | ||||
var cmpMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | let cmpUnitMotion = Engine.QueryInterface(this.entity, IID_UnitMotion); | ||||
cmpMotion.SetSpeed(speed); | cmpUnitMotion.SetSpeedRatio(speed); | ||||
Done Inline Actions? (you can use let while you're at it) Itms: ? (you can use `let` while you're at it) | |||||
Not Done Inline Actionssame bb: same | |||||
bbUnsubmitted Done Inline Actionssame bb: same | |||||
}; | }; | ||||
Done Inline Actionssame bb: same | |||||
UnitAI.prototype.SetHeldPosition = function(x, z) | UnitAI.prototype.SetHeldPosition = function(x, z) | ||||
{ | { | ||||
this.heldPosition = {"x": x, "z": z}; | this.heldPosition = {"x": x, "z": z}; | ||||
}; | }; | ||||
UnitAI.prototype.SetHeldPositionOnEntity = function(entity) | UnitAI.prototype.SetHeldPositionOnEntity = function(entity) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 363 Lines • Show Last 20 Lines |
This is not used