Changeset View
Standalone View
binaries/data/mods/public/simulation/ai/common-api/entity.js
Show First 20 Lines • Show All 413 Lines • ▼ Show 20 Lines | "resourceDropsiteTypes": function() { | ||||
return types ? types.split(/\s+/) : []; | return types ? types.split(/\s+/) : []; | ||||
}, | }, | ||||
"garrisonableClasses": function() { return this.get("GarrisonHolder/List/_string"); }, | "garrisonableClasses": function() { return this.get("GarrisonHolder/List/_string"); }, | ||||
"garrisonMax": function() { return this.get("GarrisonHolder/Max"); }, | "garrisonMax": function() { return this.get("GarrisonHolder/Max"); }, | ||||
"garrisonSize": function() { return this.get("Garrisonable/Size"); }, | |||||
"garrisonEjectHealth": function() { return +this.get("GarrisonHolder/EjectHealth"); }, | "garrisonEjectHealth": function() { return +this.get("GarrisonHolder/EjectHealth"); }, | ||||
"getDefaultArrow": function() { return +this.get("BuildingAI/DefaultArrowCount"); }, | "getDefaultArrow": function() { return +this.get("BuildingAI/DefaultArrowCount"); }, | ||||
"getArrowMultiplier": function() { return +this.get("BuildingAI/GarrisonArrowMultiplier"); }, | "getArrowMultiplier": function() { return +this.get("BuildingAI/GarrisonArrowMultiplier"); }, | ||||
"getGarrisonArrowClasses": function() { | "getGarrisonArrowClasses": function() { | ||||
if (!this.get("BuildingAI")) | if (!this.get("BuildingAI")) | ||||
▲ Show 20 Lines • Show All 275 Lines • ▼ Show 20 Lines | "currentGatherRate": function() { | ||||
// returns the gather rate for the current target if applicable. | // returns the gather rate for the current target if applicable. | ||||
if (!this.get("ResourceGatherer")) | if (!this.get("ResourceGatherer")) | ||||
return undefined; | return undefined; | ||||
if (this.unitAIOrderData().length && | if (this.unitAIOrderData().length && | ||||
(this.unitAIState().split(".")[1] == "GATHER" || this.unitAIState().split(".")[1] == "RETURNRESOURCE")) | (this.unitAIState().split(".")[1] == "GATHER" || this.unitAIState().split(".")[1] == "RETURNRESOURCE")) | ||||
{ | { | ||||
let res; | let res; | ||||
// this is an abuse of "_ai" but it works. | // this is an abuse of "_ai" but it works. | ||||
Freagarach: Got that (below) from here. | |||||
if (this.unitAIState().split(".")[1] == "GATHER" && this.unitAIOrderData()[0].target !== undefined) | if (this.unitAIState().split(".")[1] == "GATHER" && this.unitAIOrderData()[0].target !== undefined) | ||||
res = this._ai._entities.get(this.unitAIOrderData()[0].target); | res = this._ai._entities.get(this.unitAIOrderData()[0].target); | ||||
else if (this.unitAIOrderData()[1] !== undefined && this.unitAIOrderData()[1].target !== undefined) | else if (this.unitAIOrderData()[1] !== undefined && this.unitAIOrderData()[1].target !== undefined) | ||||
res = this._ai._entities.get(this.unitAIOrderData()[1].target); | res = this._ai._entities.get(this.unitAIOrderData()[1].target); | ||||
if (!res) | if (!res) | ||||
return 0; | return 0; | ||||
let type = res.resourceSupplyType(); | let type = res.resourceSupplyType(); | ||||
if (!type) | if (!type) | ||||
return 0; | return 0; | ||||
if (type.generic == "treasure") | if (type.generic == "treasure") | ||||
return 1000; | return 1000; | ||||
let tstring = type.generic + "." + type.specific; | let tstring = type.generic + "." + type.specific; | ||||
let rate = +this.get("ResourceGatherer/BaseSpeed"); | let rate = +this.get("ResourceGatherer/BaseSpeed"); | ||||
rate *= +this.get("ResourceGatherer/Rates/" +tstring); | rate *= +this.get("ResourceGatherer/Rates/" +tstring); | ||||
if (rate) | if (rate) | ||||
return rate; | return rate; | ||||
return 0; | return 0; | ||||
} | } | ||||
return undefined; | return undefined; | ||||
}, | }, | ||||
"garrisoned": function() { return this._entity.garrisoned; }, | "garrisoned": function() { return this._entity.garrisoned; }, | ||||
"canGarrisonInside": function() { return this._entity.garrisoned.length < this.garrisonMax(); }, | |||||
"garrisonedSlots": function() { | |||||
let count = 0; | |||||
if (this._entity.garrisoned) | |||||
for (let ent of this._entity.garrisoned) | |||||
count += +this._ai._entities.get(ent).garrisonSize(); | |||||
return count; | |||||
Done Inline Actions@Angen, please help yet again. I can't get this to work. ent.garrisonSlots() does not exist, but I created it above,,, Freagarach: @Angen, please help yet again. I can't get this to work. `ent.garrisonSlots()` does not exist… | |||||
Done Inline ActionsgarrisonSlots is template function, not entity function Silier: garrisonSlots is template function, not entity function | |||||
Done Inline ActionsBut even if I make it an entity function it whines about it being not a function :( Freagarach: But even if I make it an entity function it whines about it being not a function :(
I tried… | |||||
Done Inline ActionsI think .garrisoned returns only ids of entities not entities themselves Silier: I think .garrisoned returns only ids of entities not entities themselves | |||||
Done Inline ActionsAh, so from the IDs it cannot 'calculate' the data? I thought on the basis of an ID it could get the information needed! Freagarach: Ah, so from the IDs it cannot 'calculate' the data? I thought on the basis of an ID it could… | |||||
Done Inline ActionsI got it working, thanks again! Your help is truly appreciated. Freagarach: I got it working, thanks again! Your help is truly appreciated.
Not sure whether the way I do… | |||||
}, | |||||
"canGarrisonInside": function() | |||||
Done Inline ActionsNewlines ? Stan: Newlines ? | |||||
{ | |||||
return this.garrisonedSlots() < this.garrisonMax(); | |||||
}, | |||||
/** | /** | ||||
* returns true if the entity can attack (including capture) the given class. | * returns true if the entity can attack (including capture) the given class. | ||||
*/ | */ | ||||
"canAttackClass": function(aClass) | "canAttackClass": function(aClass) | ||||
{ | { | ||||
if (!this.get("Attack")) | if (!this.get("Attack")) | ||||
return false; | return false; | ||||
Show All 27 Lines | "canAttackTarget": function(target, allowCapture) | ||||
for (let type in attackTypes) | for (let type in attackTypes) | ||||
{ | { | ||||
if (type == "Capture" ? !canCapture : target.isInvulnerable()) | if (type == "Capture" ? !canCapture : target.isInvulnerable()) | ||||
continue; | continue; | ||||
let restrictedClasses = this.get("Attack/" + type + "/RestrictedClasses/_string"); | let restrictedClasses = this.get("Attack/" + type + "/RestrictedClasses/_string"); | ||||
if (!restrictedClasses || !MatchesClassList(target.classes(), restrictedClasses)) | if (!restrictedClasses || !MatchesClassList(target.classes(), restrictedClasses)) | ||||
return true; | return true; | ||||
}; | }; | ||||
Lint: ESLintBear (no-extra-semi) Unnecessary semicolon. Lint: ESLintBear (no-extra-semi): `Unnecessary semicolon.` | |||||
Lint: JSHintBear Unnecessary semicolon. Lint: JSHintBear: `Unnecessary semicolon.` | |||||
return false; | return false; | ||||
}, | }, | ||||
"move": function(x, z, queued = false) { | "move": function(x, z, queued = false) { | ||||
Engine.PostCommand(PlayerID, { "type": "walk", "entities": [this.id()], "x": x, "z": z, "queued": queued }); | Engine.PostCommand(PlayerID, { "type": "walk", "entities": [this.id()], "x": x, "z": z, "queued": queued }); | ||||
return this; | return this; | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | m.Entity = m.Class({ | ||||
"attack": function(unitId, allowCapture = true, queued = false) { | "attack": function(unitId, allowCapture = true, queued = false) { | ||||
Engine.PostCommand(PlayerID, { "type": "attack", "entities": [this.id()], "target": unitId, "allowCapture": allowCapture, "queued": queued }); | Engine.PostCommand(PlayerID, { "type": "attack", "entities": [this.id()], "target": unitId, "allowCapture": allowCapture, "queued": queued }); | ||||
return this; | return this; | ||||
}, | }, | ||||
// moveApart from a point in the opposite direction with a distance dist | // moveApart from a point in the opposite direction with a distance dist | ||||
"moveApart": function(point, dist) { | "moveApart": function(point, dist) { | ||||
if (this.position() !== undefined) { | if (this.position() !== undefined) { | ||||
Lint: ESLintBear (brace-rules/brace-on-same-line) Opening curly brace appears on the same line as controlling statement. Lint: ESLintBear (brace-rules/brace-on-same-line): `Opening curly brace appears on the same line as controlling statement.` | |||||
let direction = [this.position()[0] - point[0], this.position()[1] - point[1]]; | let direction = [this.position()[0] - point[0], this.position()[1] - point[1]]; | ||||
let norm = m.VectorDistance(point, this.position()); | let norm = m.VectorDistance(point, this.position()); | ||||
if (norm === 0) | if (norm === 0) | ||||
direction = [1, 0]; | direction = [1, 0]; | ||||
else | else | ||||
{ | { | ||||
direction[0] /= norm; | direction[0] /= norm; | ||||
direction[1] /= norm; | direction[1] /= norm; | ||||
} | } | ||||
Engine.PostCommand(PlayerID, { "type": "walk", "entities": [this.id()], "x": this.position()[0] + direction[0]*dist, "z": this.position()[1] + direction[1]*dist, "queued": false }); | Engine.PostCommand(PlayerID, { "type": "walk", "entities": [this.id()], "x": this.position()[0] + direction[0]*dist, "z": this.position()[1] + direction[1]*dist, "queued": false }); | ||||
} | } | ||||
return this; | return this; | ||||
}, | }, | ||||
// Flees from a unit in the opposite direction. | // Flees from a unit in the opposite direction. | ||||
"flee": function(unitToFleeFrom) { | "flee": function(unitToFleeFrom) { | ||||
if (this.position() !== undefined && unitToFleeFrom.position() !== undefined) { | if (this.position() !== undefined && unitToFleeFrom.position() !== undefined) { | ||||
Lint: ESLintBear (brace-rules/brace-on-same-line) Opening curly brace appears on the same line as controlling statement. Lint: ESLintBear (brace-rules/brace-on-same-line): `Opening curly brace appears on the same line as controlling statement.` | |||||
let FleeDirection = [this.position()[0] - unitToFleeFrom.position()[0], | let FleeDirection = [this.position()[0] - unitToFleeFrom.position()[0], | ||||
this.position()[1] - unitToFleeFrom.position()[1]]; | this.position()[1] - unitToFleeFrom.position()[1]]; | ||||
let dist = m.VectorDistance(unitToFleeFrom.position(), this.position()); | let dist = m.VectorDistance(unitToFleeFrom.position(), this.position()); | ||||
FleeDirection[0] = 40 * FleeDirection[0]/dist; | FleeDirection[0] = 40 * FleeDirection[0]/dist; | ||||
FleeDirection[1] = 40 * FleeDirection[1]/dist; | FleeDirection[1] = 40 * FleeDirection[1]/dist; | ||||
Engine.PostCommand(PlayerID, { "type": "walk", "entities": [this.id()], "x": this.position()[0] + FleeDirection[0], "z": this.position()[1] + FleeDirection[1], "queued": false }); | Engine.PostCommand(PlayerID, { "type": "walk", "entities": [this.id()], "x": this.position()[0] + FleeDirection[0], "z": this.position()[1] + FleeDirection[1], "queued": false }); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | m.Entity = m.Class({ | ||||
"removeGuard": function() { | "removeGuard": function() { | ||||
Engine.PostCommand(PlayerID, { "type": "remove-guard", "entities": [this.id()] }); | Engine.PostCommand(PlayerID, { "type": "remove-guard", "entities": [this.id()] }); | ||||
return this; | return this; | ||||
} | } | ||||
}); | }); | ||||
return m; | return m; | ||||
}(API3); | }(API3); | ||||
Lint: ESLintBear (no-use-before-define) 'API3' was used before it was defined. Lint: ESLintBear (no-use-before-define): `'API3' was used before it was defined.` |
Got that (below) from here.