Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/ai/common-api/entity.js
Show First 20 Lines • Show All 374 Lines • ▼ Show 20 Lines | m.Template = m.Class({ | |||||||||||
}, | }, | |||||||||||
"resourceSupplyType": function() { | "resourceSupplyType": function() { | |||||||||||
if (!this.get("ResourceSupply")) | if (!this.get("ResourceSupply")) | |||||||||||
return undefined; | return undefined; | |||||||||||
let [type, subtype] = this.get("ResourceSupply/Type").split('.'); | let [type, subtype] = this.get("ResourceSupply/Type").split('.'); | |||||||||||
return { "generic": type, "specific": subtype }; | return { "generic": type, "specific": subtype }; | |||||||||||
}, | }, | |||||||||||
// will return either "food", "wood", "stone", "metal" and not treasure. | ||||||||||||
"getResourceType": function() { | "getResourceType": function() { | |||||||||||
if (!this.get("ResourceSupply")) | if (!this.get("ResourceSupply")) | |||||||||||
return undefined; | return undefined; | |||||||||||
let [type, subtype] = this.get("ResourceSupply/Type").split('.'); | return this.get("ResourceSupply/Type").split('.')[0]; | |||||||||||
if (type == "treasure") | ||||||||||||
return subtype; | ||||||||||||
return type; | ||||||||||||
}, | }, | |||||||||||
"getDiminishingReturns": function() { return +(this.get("ResourceSupply/DiminishingReturns") || 1); }, | "getDiminishingReturns": function() { return +(this.get("ResourceSupply/DiminishingReturns") || 1); }, | |||||||||||
"resourceSupplyMax": function() { return +this.get("ResourceSupply/Amount"); }, | "resourceSupplyMax": function() { return +this.get("ResourceSupply/Amount"); }, | |||||||||||
"maxGatherers": function() { return +(this.get("ResourceSupply/MaxGatherers") || 0); }, | "maxGatherers": function() { return +(this.get("ResourceSupply/MaxGatherers") || 0); }, | |||||||||||
Show All 10 Lines | m.Template = m.Class({ | |||||||||||
"resourceDropsiteTypes": function() { | "resourceDropsiteTypes": function() { | |||||||||||
if (!this.get("ResourceDropsite")) | if (!this.get("ResourceDropsite")) | |||||||||||
return undefined; | return undefined; | |||||||||||
let types = this.get("ResourceDropsite/Types"); | let types = this.get("ResourceDropsite/Types"); | |||||||||||
return types ? types.split(/\s+/) : []; | return types ? types.split(/\s+/) : []; | |||||||||||
}, | }, | |||||||||||
"isTreasure": function() { return this.get("Treasure") !== undefined; }, | ||||||||||||
Stan: can it be null/Falsy? | ||||||||||||
Done Inline Actions
No, but other function explicitly check for undefined. Freagarach: > can it be null/Falsy?
No, but other function explicitly check for undefined. | ||||||||||||
"treasureResources": function() { | ||||||||||||
if (!this.get("Treasure")) | ||||||||||||
return undefined; | ||||||||||||
let ret = {}; | ||||||||||||
for (let r in this.get("Treasure/Resources")) | ||||||||||||
ret[r] = +this.get("Treasure/Resources/" + r); | ||||||||||||
Done Inline ActionsMight be able to use.map() not sure. Stan: Might be able to use.map() not sure. | ||||||||||||
Done Inline ActionsBut I don't want an array, right? Freagarach: But I don't want an array, right? | ||||||||||||
Done Inline ActionsIndeed. Stan: Indeed. | ||||||||||||
return ret; | ||||||||||||
}, | ||||||||||||
"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"); }, | |||||||||||
"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"); }, | |||||||||||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | "canCapture": function(target) | |||||||||||
return !restrictedClasses || !MatchesClassList(target.classes(), restrictedClasses); | return !restrictedClasses || !MatchesClassList(target.classes(), restrictedClasses); | |||||||||||
}, | }, | |||||||||||
"isCapturable": function() { return this.get("Capturable") !== undefined; }, | "isCapturable": function() { return this.get("Capturable") !== undefined; }, | |||||||||||
"canGuard": function() { return this.get("UnitAI/CanGuard") === "true"; }, | "canGuard": function() { return this.get("UnitAI/CanGuard") === "true"; }, | |||||||||||
"canGarrison": function() { return "Garrisonable" in this._template; }, | "canGarrison": function() { return "Garrisonable" in this._template; }, | |||||||||||
"isTreasureCollecter": function() { return this.get("TreasureCollecter") !== undefined; }, | ||||||||||||
}); | }); | |||||||||||
// defines an entity, with a super Template. | // defines an entity, with a super Template. | |||||||||||
// also redefines several of the template functions where the only change is applying aura and tech modifications. | // also redefines several of the template functions where the only change is applying aura and tech modifications. | |||||||||||
m.Entity = m.Class({ | m.Entity = m.Class({ | |||||||||||
"_super": m.Template, | "_super": m.Template, | |||||||||||
▲ Show 20 Lines • Show All 150 Lines • ▼ Show 20 Lines | if (this.unitAIOrderData().length && | |||||||||||
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") | ||||||||||||
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; | |||||||||||
Show All 39 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; | |||||||||||
}; | } | |||||||||||
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 All 40 Lines | "garrison": function(target, queued = false) { | |||||||||||
return this; | return this; | |||||||||||
}, | }, | |||||||||||
"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; | |||||||||||
}, | }, | |||||||||||
"collectTreasure": function(target, queued = false) { | ||||||||||||
Engine.PostCommand(PlayerID, { | ||||||||||||
Done Inline ActionsMultiline? Stan: Multiline? | ||||||||||||
"type": "collect-treasure", | ||||||||||||
"entities": [this.id()], | ||||||||||||
"target": target.id(), | ||||||||||||
"queued": queued | ||||||||||||
}); | ||||||||||||
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.` |
Wildfire Games · Phabricator
can it be null/Falsy?