Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/gui/session/unit_actions.js
Show First 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | "execute": function(target, action, selection, queued) | ||||
"name": "order_attack", | "name": "order_attack", | ||||
"entity": selection[0] | "entity": selection[0] | ||||
}); | }); | ||||
return true; | return true; | ||||
}, | }, | ||||
"getActionInfo": function(entState, targetState) | "getActionInfo": function(entState, targetState) | ||||
{ | { | ||||
if (!entState.attack || !targetState.hitpoints) | if (!targetState.hitpoints) | ||||
return false; | return false; | ||||
return { | return { | ||||
"possible": Engine.GuiInterfaceCall("CanAttack", { | "possible": Engine.GuiInterfaceCall("CanAttack", { | ||||
"entity": entState.id, | "entity": entState.id, | ||||
"target": targetState.id, | "target": targetState.id, | ||||
"types": ["Capture"] | "types": ["Capture"] | ||||
}) | }) | ||||
Show All 29 Lines | "execute": function(target, action, selection, queued) | ||||
"name": "order_attack", | "name": "order_attack", | ||||
"entity": selection[0] | "entity": selection[0] | ||||
}); | }); | ||||
return true; | return true; | ||||
}, | }, | ||||
"getActionInfo": function(entState, targetState) | "getActionInfo": function(entState, targetState) | ||||
{ | { | ||||
if (!entState.attack || !targetState.hitpoints) | if (!targetState.hitpoints) | ||||
return false; | return false; | ||||
return { | return { | ||||
"possible": Engine.GuiInterfaceCall("CanAttack", { | "possible": Engine.GuiInterfaceCall("CanAttack", { | ||||
"entity": entState.id, | "entity": entState.id, | ||||
"target": targetState.id, | "target": targetState.id, | ||||
"types": ["!Capture"] | "types": ["!Capture"] | ||||
}) | }) | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | "execute": function(target, action, selection, queued) | ||||
"name": "order_heal", | "name": "order_heal", | ||||
"entity": selection[0] | "entity": selection[0] | ||||
}); | }); | ||||
return true; | return true; | ||||
}, | }, | ||||
"getActionInfo": function(entState, targetState) | "getActionInfo": function(entState, targetState) | ||||
{ | { | ||||
if (!entState.heal || | if (!hasClass(targetState, "Unit") || !targetState.needsHeal || | ||||
!hasClass(targetState, "Unit") || !targetState.needsHeal || | |||||
!playerCheck(entState, targetState, ["Player", "Ally"]) || | !playerCheck(entState, targetState, ["Player", "Ally"]) || | ||||
entState.id == targetState.id) // Healers can't heal themselves. | entState.id == targetState.id) // Healers can't heal themselves. | ||||
return false; | return false; | ||||
let unhealableClasses = entState.heal.unhealableClasses; | return { | ||||
if (MatchesClassList(targetState.identity.classes, unhealableClasses)) | "possible": Engine.GuiInterfaceCall("CanHeal", { | ||||
return false; | "entity": entState.id, | ||||
"target": targetState.id | |||||
let healableClasses = entState.heal.healableClasses; | }) | ||||
if (!MatchesClassList(targetState.identity.classes, healableClasses)) | }; | ||||
return false; | |||||
return { "possible": true }; | |||||
}, | }, | ||||
"actionCheck": function(target, selection) | "actionCheck": function(target, selection) | ||||
{ | { | ||||
if (!getActionInfo("heal", target, selection).possible) | if (!getActionInfo("heal", target, selection).possible) | ||||
return false; | return false; | ||||
return { | return { | ||||
"type": "heal", | "type": "heal", | ||||
▲ Show 20 Lines • Show All 204 Lines • ▼ Show 20 Lines | "actionCheck": function(target, selection) | ||||
"target": target | "target": target | ||||
}; | }; | ||||
}, | }, | ||||
"specificness": 2, | "specificness": 2, | ||||
}, | }, | ||||
"cancel-setup-trade-route": | "cancel-setup-trade-route": | ||||
{ | { | ||||
"execute":function(target, action, selection, queued) | "execute":function(target, action, selection, queued) | ||||
Lint: ESLintBear (key-spacing): `Missing space before value for key 'execute'.` | |||||
{ | { | ||||
Engine.PostNetworkCommand({ | Engine.PostNetworkCommand({ | ||||
"type": "cancel-setup-trade-route", | "type": "cancel-setup-trade-route", | ||||
"entities": selection, | "entities": selection, | ||||
"target": action.target, | "target": action.target, | ||||
"queued": queued | "queued": queued | ||||
}); | }); | ||||
Show All 28 Lines | "actionCheck": function(target, selection) | ||||
if (!actionInfo.possible) | if (!actionInfo.possible) | ||||
return false; | return false; | ||||
return { | return { | ||||
"type": "cancel-setup-trade-route", | "type": "cancel-setup-trade-route", | ||||
"cursor": "action-cancel-setup-trade-route", | "cursor": "action-cancel-setup-trade-route", | ||||
"tooltip": actionInfo.tooltip, | "tooltip": actionInfo.tooltip, | ||||
"target": target | "target": target | ||||
} | } | ||||
Lint: ESLintBear (semi) Missing semicolon. Lint: ESLintBear (semi): `Missing semicolon.` | |||||
Lint: JSHintBear Missing semicolon. Lint: JSHintBear: `Missing semicolon.` | |||||
}, | }, | ||||
"specificness": 2, | "specificness": 2, | ||||
}, | }, | ||||
"setup-trade-route": | "setup-trade-route": | ||||
{ | { | ||||
"execute": function(target, action, selection, queued) | "execute": function(target, action, selection, queued) | ||||
{ | { | ||||
Show All 25 Lines | "getActionInfo": function(entState, targetState) | ||||
"trader": entState.id, | "trader": entState.id, | ||||
"target": targetState.id | "target": targetState.id | ||||
}); | }); | ||||
if (!tradingDetails) | if (!tradingDetails) | ||||
return false; | return false; | ||||
let tooltip; | let tooltip; | ||||
switch (tradingDetails.type) | switch (tradingDetails.type) | ||||
Lint: ESLintBear (default-case) Expected a default case. Lint: ESLintBear (default-case): `Expected a default case.` | |||||
{ | { | ||||
case "is first": | case "is first": | ||||
tooltip = translate("Origin trade market.") + "\n"; | tooltip = translate("Origin trade market.") + "\n"; | ||||
if (tradingDetails.hasBothMarkets) | if (tradingDetails.hasBothMarkets) | ||||
tooltip += sprintf(translate("Gain: %(gain)s"), { | tooltip += sprintf(translate("Gain: %(gain)s"), { | ||||
"gain": getTradingTooltip(tradingDetails.gain) | "gain": getTradingTooltip(tradingDetails.gain) | ||||
}); | }); | ||||
else | else | ||||
▲ Show 20 Lines • Show All 481 Lines • ▼ Show 20 Lines | |||||
var g_EntityCommands = | var g_EntityCommands = | ||||
{ | { | ||||
"unload-all": { | "unload-all": { | ||||
"getInfo": function(entStates) | "getInfo": function(entStates) | ||||
{ | { | ||||
let count = 0; | let count = 0; | ||||
for (let entState of entStates) | for (let entState of entStates) | ||||
if (entState.garrisonHolder) | if (entState.garrisonHolder) | ||||
count += entState.garrisonHolder.entities.length; | count += entState.garrisonHolder.entities.length; | ||||
Done Inline ActionsIn my tests this is faster. I wonder how much perf we can squeeze out of GetEntity state by using for loops. for (let entityIndex = 0; entityIndex < entState.garrisonHolder.entities.length; ++ entityIndex) { var entity = entState.garrisonHolder.entities[entityIndex]; let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } Test result Poor Man For 0.002200744999572635 ms. Poor man for filter 0.004132418988738209 ms. Reduce 0.006193987999577075 ms. Reduce Poor man for 0.005311861000955105 ms. Reduce Poor man for 0.0031248840107582507 ms. Poor man for Poor man for 0.001599128011148423 ms. Dubious code nodejs let GetEntityState = function(ent) { return { "canGarrison": { "unloadable": ent % 2 == 0 }, } }; const iterations = 100000; class EntityState { constructor() { this.garrisonHolder = undefined; } SetGarrisonHolder(obj) { this.garrisonHolder = obj; } } var entState1 = new EntityState(); let entities = [1, 2, 3, 4, 5, 6, 7, 8] entState1.SetGarrisonHolder({"entities": entities, "buffHeal": 2, "allowedClasses": "Infantry+Ranged", "capacity": entities.length + 1, "garrisonedEntitiesCount": entities.length }) var entStates = []; for (i = 0; i < 50; ++i) { if(i % 3 == 0) entStates.push(new EntityState()); entStates.push(entState1); } console.log("Poor Man For"); let start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) { let count = 0; for (let entState of entStates) if (entState.garrisonHolder) for (let entity of entState.garrisonHolder.entities) { let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } } let duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Poor man for filter"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) { let count = 0; for (let entState of entStates) if (entState.garrisonHolder) count += entState.garrisonHolder.entities.filter(ent =>{ let state = GetEntityState(ent) return state.canGarrison && state.canGarrison.unloadable }).length } duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Reduce"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) entStates.reduce((r, entState) => r + (entState.garrisonHolder ? (entState.garrisonHolder.entities.filter(ent =>{ let state = GetEntityState(ent) return state.canGarrison && state.canGarrison.unloadable }).length) : 0), 0) duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Reduce Poor man for"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) entStates.reduce((total, entState) => { let count = 0; if (!entState.garrisonHolder) return total + 0; for (let entity of entState.garrisonHolder.entities) { let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } return total + count; }, 0) duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Reduce Poor man for"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) entStates.reduce((total, entState) => { let count = 0; if (!entState.garrisonHolder) return total + 0; for (let entityIndex = 0; entityIndex < entState.garrisonHolder.entities.length; ++ entityIndex) { var entity = entState.garrisonHolder.entities[entityIndex]; let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } return total + count; }, 0) duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); console.log("Poor man for Poor man for"); start = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001); for(let a = 0; a < iterations; ++a) { let count = 0; for (let entState of entStates) if (entState.garrisonHolder) for (let entityIndex = 0; entityIndex < entState.garrisonHolder.entities.length; ++ entityIndex) { var entity = entState.garrisonHolder.entities[entityIndex]; let state = GetEntityState(entity); if (state.canGarrison && state.canGarrison.unloadable) ++count; } } duration = process.hrtime()[0] + (process.hrtime()[1] * 0.000000001) - start; console.log((duration / iterations * 1000) + " ms."); } Stan: In my tests this is faster. I wonder how much perf we can squeeze out of GetEntity state by… | |||||
if (!count) | if (!count) | ||||
return false; | return false; | ||||
Done Inline Actionscheck for state.canGarrison existance or if not needed do GetEntityState(entity).canGarrison.unloadable Silier: check for state.canGarrison existance or if not needed do GetEntityState(entity).canGarrison. | |||||
return { | return { | ||||
Done Inline ActionsThat and above also seems unrelated. wraitii: That and above also seems unrelated. | |||||
Done Inline ActionsThe above is to allowe healers as turrets. Freagarach: The above is to allowe healers as turrets.
This is so that one can't think that a turret can be… | |||||
Done Inline ActionsAh, yeah. The above sounds hacky then, this is fine. wraitii: Ah, yeah. The above sounds hacky then, this is fine. | |||||
"tooltip": colorizeHotkey("%(hotkey)s" + " ", "session.unload") + | "tooltip": colorizeHotkey("%(hotkey)s" + " ", "session.unload") + | ||||
translate("Unload All."), | translate("Unload All."), | ||||
"icon": "garrison-out.png", | "icon": "garrison-out.png", | ||||
"count": count, | "count": count, | ||||
}; | }; | ||||
}, | }, | ||||
"execute": function() | "execute": function() | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 517 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Missing space before value for key 'execute'.