Changeset View
Standalone View
binaries/data/mods/public/gui/session/unit_actions.js
/** | /** | ||||
* Specifies which template should indicate the target location of a player command, | * Specifies which template should indicate the target location of a player command, | ||||
* given a command type. | * given a command type. | ||||
*/ | */ | ||||
var g_TargetMarker = { | var g_TargetMarker = { | ||||
"move": "special/target_marker" | "move": "special/target_marker" | ||||
}; | }; | ||||
/** | /** | ||||
* Which enemy entity types will be attacked on sight when patroling. | * Which enemy entity types will be attacked on sight when patroling. | ||||
*/ | */ | ||||
var g_PatrolTargets = ["Unit"]; | var g_PatrolTargets = ["Unit"]; | ||||
var disabledColor = { "color": "255 140 0" }; | |||||
/** | /** | ||||
elexis: Color -> Tags as it may equally contain Font changes | |||||
* List of different actions units can execute, | * List of different actions units can execute, | ||||
* this is mostly used to determine which actions can be executed | * this is mostly used to determine which actions can be executed | ||||
* | * | ||||
* "execute" is meant to send the command to the engine | * "execute" is meant to send the command to the engine | ||||
* | * | ||||
* The next functions will always return false | * The next functions will always return false | ||||
* in case you have to continue to seek | * in case you have to continue to seek | ||||
* (i.e. look at the next entity for getActionInfo, the next | * (i.e. look at the next entity for getActionInfo, the next | ||||
▲ Show 20 Lines • Show All 535 Lines • ▼ Show 20 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; | ||||
let possible = true; | |||||
let disabled = false; | |||||
switch (tradingDetails.type) | switch (tradingDetails.type) | ||||
{ | { | ||||
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 | ||||
tooltip += translate("Right-click on another market to set it as a destination trade market."); | tooltip += translate("Right-click on another market to set it as a destination trade market."); | ||||
break; | break; | ||||
case "is second": | case "is second": | ||||
tooltip = translate("Destination trade market.") + "\n" + | tooltip = translate("Destination trade market.") + "\n" + | ||||
sprintf(translate("Gain: %(gain)s"), { | sprintf(translate("Gain: %(gain)s"), { | ||||
"gain": getTradingTooltip(tradingDetails.gain) | "gain": getTradingTooltip(tradingDetails.gain) | ||||
}); | }); | ||||
break; | break; | ||||
case "set first": | case "set first": | ||||
tooltip = translate("Right-click to set as origin trade market"); | tooltip = translate("Right-click to set as origin trade market"); | ||||
break; | break; | ||||
case "set second": | case "set second": | ||||
if (tradingDetails.gain.traderGain == 0) // markets too close | if (tradingDetails.gain.traderGain == 0) | ||||
Not Done Inline Actions(I've seen value == 0 being changed to !value in d2470, not that it matters) elexis: (I've seen value == 0 being changed to !value in d2470, not that it matters) | |||||
return false; | { | ||||
possible = false; | |||||
disabled = true; | |||||
tooltip = setStringTags(translate("This market is too close to the origin market."), disabledColor); | |||||
} | |||||
else | |||||
tooltip = translate("Right-click to set as destination trade market.") + "\n" + | tooltip = translate("Right-click to set as destination trade market.") + "\n" + | ||||
sprintf(translate("Gain: %(gain)s"), { | sprintf(translate("Gain: %(gain)s"), { | ||||
"gain": getTradingTooltip(tradingDetails.gain) | "gain": getTradingTooltip(tradingDetails.gain) | ||||
}); | }); | ||||
Not Done Inline ActionsI believe you can remove 'setting of' Stan: I believe you can remove 'setting of' | |||||
Not Done Inline Actions"Market is too close to the first one.\n Right-click to cancel." Stan: "Market is too close to the first one.\n Right-click to cancel." | |||||
break; | break; | ||||
} | } | ||||
return { | return { | ||||
"possible": true, | "possible": possible, | ||||
Not Done Inline ActionsBut it still returns true then? Freagarach: But it still returns `true` then? | |||||
Done Inline Actionsyes, esle it would not display. Silier: yes, esle it would not display.
its similar to trying set the same market twice, it will not… | |||||
Not Done Inline ActionsOkay, but what happens if you return "possible": false here? The icon/tooltip do not show up at all? Freagarach: Okay, but what happens if you return `"possible": false` here? The icon/tooltip do not show up… | |||||
"tooltip": tooltip | "tooltip": tooltip, | ||||
"disabled": disabled | |||||
}; | }; | ||||
}, | }, | ||||
"actionCheck": function(target, selection) | "actionCheck": function(target, selection) | ||||
{ | { | ||||
let actionInfo = getActionInfo("setup-trade-route", target, selection); | let actionInfo = getActionInfo("setup-trade-route", target, selection); | ||||
if (actionInfo.disabled) | |||||
return { | |||||
"type": "none", | |||||
"cursor": "action-setup-trade-route", | |||||
"target": null, | |||||
"tooltip": actionInfo.tooltip, | |||||
}; | |||||
Not Done Inline Actions(Not a fan of trailing commas, I've used them once and not seen any reward, but thats subjective) elexis: (Not a fan of trailing commas, I've used them once and not seen any reward, but thats… | |||||
Not Done Inline ActionsIt's in the cc now apparently https://trac.wildfiregames.com/wiki/Coding_Conventions?action=diff&version=46 Stan: It's in the cc now apparently https://trac.wildfiregames.com/wiki/Coding_Conventions? | |||||
Done Inline Actionsbecause one does not have to edit that last line if he wants to add new propperty after that Silier: because one does not have to edit that last line if he wants to add new propperty after that | |||||
Not Done Inline ActionsThe argument for not adding it is that a comma is a separator between two items, but the trailing comma does not separate two items, so it seems contradictory to that definition to me. (Its not the definition since the JS syntax allows it with an explicit exception.) I didn't know we can just edit the CC, I could have won many arguments based on what the CC says if Id just edit it beforehand :p I don't mind how you write your patches with this not so relevant syntax change, but if you add something to the CC you also impose how other people should write their patches, so I suppose it necessitates consensus, no? elexis: The argument for not adding it is that a comma is a separator between two items, but the… | |||||
Not Done Inline ActionsThen L596 also ;) Freagarach: Then L596 also ;) | |||||
if (!actionInfo.possible) | if (!actionInfo.possible) | ||||
return false; | return false; | ||||
return { | return { | ||||
"type": "setup-trade-route", | "type": "setup-trade-route", | ||||
"cursor": "action-setup-trade-route", | "cursor": "action-setup-trade-route", | ||||
"tooltip": actionInfo.tooltip, | "tooltip": actionInfo.tooltip, | ||||
"target": target | "target": target | ||||
Show All 30 Lines | "getActionInfo": function(entState, targetState) | ||||
"garrisoned": targetState.garrisonHolder.garrisonedEntitiesCount, | "garrisoned": targetState.garrisonHolder.garrisonedEntitiesCount, | ||||
"capacity": targetState.garrisonHolder.capacity | "capacity": targetState.garrisonHolder.capacity | ||||
}); | }); | ||||
let extraCount = 0; | let extraCount = 0; | ||||
if (entState.garrisonHolder) | if (entState.garrisonHolder) | ||||
extraCount += entState.garrisonHolder.garrisonedEntitiesCount; | extraCount += entState.garrisonHolder.garrisonedEntitiesCount; | ||||
let possible = true; | |||||
let disabled = false; | |||||
if (targetState.garrisonHolder.garrisonedEntitiesCount + extraCount >= targetState.garrisonHolder.capacity) | if (targetState.garrisonHolder.garrisonedEntitiesCount + extraCount >= targetState.garrisonHolder.capacity) | ||||
tooltip = coloredText(tooltip, "orange"); | { | ||||
tooltip = setStringTags(tooltip, disabledColor); | |||||
disabled = true; | |||||
possible = false; | |||||
} | |||||
if (!MatchesClassList(entState.identity.classes, targetState.garrisonHolder.allowedClasses)) | if (!MatchesClassList(entState.identity.classes, targetState.garrisonHolder.allowedClasses)) | ||||
return false; | return false; | ||||
return { | return { | ||||
"possible": true, | "possible": possible, | ||||
"tooltip": tooltip | "tooltip": tooltip, | ||||
"disabled": disabled | |||||
}; | }; | ||||
}, | }, | ||||
"preSelectedActionCheck": function(target, selection) | "preSelectedActionCheck": function(target, selection) | ||||
{ | { | ||||
if (preSelectedAction != ACTION_GARRISON) | if (preSelectedAction != ACTION_GARRISON) | ||||
return false; | return false; | ||||
let actionInfo = getActionInfo("garrison", target, selection); | let actionInfo = getActionInfo("garrison", target, selection); | ||||
if (actionInfo.disabled) | |||||
return { | |||||
"type": "none", | |||||
"cursor": "action-garrison-disabled", | |||||
"target": null, | |||||
"tooltip": actionInfo.tooltip, | |||||
}; | |||||
if (!actionInfo.possible) | if (!actionInfo.possible) | ||||
return { | return { | ||||
"type": "none", | "type": "none", | ||||
"cursor": "action-garrison-disabled", | "cursor": "action-garrison-disabled", | ||||
"target": null | "target": null | ||||
}; | }; | ||||
return { | return { | ||||
"type": "garrison", | "type": "garrison", | ||||
"cursor": "action-garrison", | "cursor": "action-garrison", | ||||
"tooltip": actionInfo.tooltip, | "tooltip": actionInfo.tooltip, | ||||
"target": target | "target": target | ||||
}; | }; | ||||
}, | }, | ||||
"hotkeyActionCheck": function(target, selection) | "hotkeyActionCheck": function(target, selection) | ||||
{ | { | ||||
let actionInfo = getActionInfo("garrison", target, selection); | let actionInfo = getActionInfo("garrison", target, selection); | ||||
if (!Engine.HotkeyIsPressed("session.garrison") || !actionInfo.possible) | if (!Engine.HotkeyIsPressed("session.garrison") || !actionInfo.possible && !actionInfo.disabled) | ||||
return false; | return false; | ||||
if (actionInfo.disabled) | |||||
return { | |||||
"type": "none", | |||||
"cursor": "action-garrison-disabled", | |||||
"target": null, | |||||
"tooltip": actionInfo.tooltip, | |||||
}; | |||||
return { | return { | ||||
"type": "garrison", | "type": "garrison", | ||||
"cursor": "action-garrison", | "cursor": "action-garrison", | ||||
"tooltip": actionInfo.tooltip, | "tooltip": actionInfo.tooltip, | ||||
"target": target | "target": target | ||||
}; | }; | ||||
}, | }, | ||||
"specificness": 20, | "specificness": 20, | ||||
▲ Show 20 Lines • Show All 120 Lines • ▼ Show 20 Lines | "execute": function(target, action, selection, queued) | ||||
"queued": queued | "queued": queued | ||||
}); | }); | ||||
return true; | return true; | ||||
}, | }, | ||||
"getActionInfo": function(entState, targetState) | "getActionInfo": function(entState, targetState) | ||||
{ | { | ||||
let tooltip; | let tooltip; | ||||
let possible = true; | |||||
let disabled = true; | |||||
// default to walking there (or attack-walking if hotkey pressed) | // default to walking there (or attack-walking if hotkey pressed) | ||||
let data = { "command": "walk" }; | let data = { "command": "walk" }; | ||||
let cursor = ""; | let cursor = ""; | ||||
if (Engine.HotkeyIsPressed("session.attackmove")) | if (Engine.HotkeyIsPressed("session.attackmove")) | ||||
{ | { | ||||
let targetClasses; | let targetClasses; | ||||
if (Engine.HotkeyIsPressed("session.attackmoveUnit")) | if (Engine.HotkeyIsPressed("session.attackmoveUnit")) | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | "getActionInfo": function(entState, targetState) | ||||
let traderData = { | let traderData = { | ||||
"firstMarket": entState.id, | "firstMarket": entState.id, | ||||
"secondMarket": targetState.id, | "secondMarket": targetState.id, | ||||
"template": trader | "template": trader | ||||
}; | }; | ||||
let gain = Engine.GuiInterfaceCall("GetTradingRouteGain", traderData); | let gain = Engine.GuiInterfaceCall("GetTradingRouteGain", traderData); | ||||
if (gain && gain.traderGain) | if (gain) | ||||
{ | { | ||||
data.command = "trade"; | data.command = "trade"; | ||||
data.target = traderData.secondMarket; | data.target = traderData.secondMarket; | ||||
data.source = traderData.firstMarket; | data.source = traderData.firstMarket; | ||||
cursor = "action-setup-trade-route"; | cursor = "action-setup-trade-route"; | ||||
if (gain.traderGain) | |||||
tooltip = translate("Right-click to establish a default route for new traders.") + "\n" + | tooltip = translate("Right-click to establish a default route for new traders.") + "\n" + | ||||
sprintf( | sprintf( | ||||
trader ? | trader ? | ||||
translate("Gain: %(gain)s") : | translate("Gain: %(gain)s") : | ||||
translate("Expected gain: %(gain)s"), | translate("Expected gain: %(gain)s"), | ||||
{ "gain": getTradingTooltip(gain) }); | { "gain": getTradingTooltip(gain) }); | ||||
else | |||||
{ | |||||
possible = false; | |||||
disabled = true; | |||||
tooltip = setStringTags(translate("This market is too close to the origin market."), disabledColor); | |||||
} | |||||
} | } | ||||
} | } | ||||
else if ((targetState.needsRepair || targetState.foundation) && playerCheck(entState, targetState, ["Ally"])) | else if ((targetState.needsRepair || targetState.foundation) && playerCheck(entState, targetState, ["Ally"])) | ||||
{ | { | ||||
data.command = "repair"; | data.command = "repair"; | ||||
data.target = targetState.id; | data.target = targetState.id; | ||||
cursor = "action-repair"; | cursor = "action-repair"; | ||||
} | } | ||||
Show All 9 Lines | "getActionInfo": function(entState, targetState) | ||||
if (!Engine.HotkeyIsPressed("session.autorallypoint") || | if (!Engine.HotkeyIsPressed("session.autorallypoint") || | ||||
!targetState.production || | !targetState.production || | ||||
!targetState.production.entities.length) | !targetState.production.entities.length) | ||||
for (let ent in g_Selection.selected) | for (let ent in g_Selection.selected) | ||||
if (targetState.id == +ent) | if (targetState.id == +ent) | ||||
return false; | return false; | ||||
return { | return { | ||||
"possible": true, | "possible": possible, | ||||
"data": data, | "data": data, | ||||
"position": targetState.position, | "position": targetState.position, | ||||
"cursor": cursor, | "cursor": cursor, | ||||
"disabled": disabled, | |||||
"tooltip": tooltip | "tooltip": tooltip | ||||
}; | }; | ||||
}, | }, | ||||
"actionCheck": function(target, selection) | "actionCheck": function(target, selection) | ||||
{ | { | ||||
if (someUnitAI(selection) || !someRallyPoints(selection)) | if (someUnitAI(selection) || !someRallyPoints(selection)) | ||||
return false; | return false; | ||||
let actionInfo = getActionInfo("set-rallypoint", target, selection); | let actionInfo = getActionInfo("set-rallypoint", target, selection); | ||||
if (actionInfo.disabled) | |||||
return { | |||||
"type": "none", | |||||
"cursor": actionInfo.cursor, | |||||
"target": null, | |||||
Not Done Inline Actions(Just wondering if there is a use case to distinguish undefined from null, I've never seen a use case where the distinction mattered, and not distinguishing falsy values allow for simpler falsy checks, or only distinguishing undefined from 0/emptystring, but doesn't matter) elexis: (Just wondering if there is a use case to distinguish undefined from null, I've never seen a… | |||||
Done Inline Actionsi used null because L391, havent look further Silier: i used null because L391, havent look further | |||||
"tooltip": actionInfo.tooltip, | |||||
}; | |||||
if (!actionInfo.possible) | if (!actionInfo.possible) | ||||
return false; | return false; | ||||
return { | return { | ||||
"type": "set-rallypoint", | "type": "set-rallypoint", | ||||
"cursor": actionInfo.cursor, | "cursor": actionInfo.cursor, | ||||
"data": actionInfo.data, | "data": actionInfo.data, | ||||
"tooltip": actionInfo.tooltip, | "tooltip": actionInfo.tooltip, | ||||
▲ Show 20 Lines • Show All 583 Lines • ▼ Show 20 Lines | function getActionInfo(action, target, selection) | ||||
{ | { | ||||
let entState = GetEntityState(entityID); | let entState = GetEntityState(entityID); | ||||
if (!entState) | if (!entState) | ||||
continue; | continue; | ||||
if (g_UnitActions[action] && g_UnitActions[action].getActionInfo) | if (g_UnitActions[action] && g_UnitActions[action].getActionInfo) | ||||
{ | { | ||||
let r = g_UnitActions[action].getActionInfo(entState, targetState, simState); | let r = g_UnitActions[action].getActionInfo(entState, targetState, simState); | ||||
if (r && r.possible) // return true if it's possible for one of the entities | if (r && r.possible || r.disabled) // return true if it's possible for one of the entities | ||||
return r; | return r; | ||||
} | } | ||||
} | } | ||||
return { "possible": false }; | return { "possible": false }; | ||||
} | } |
Color -> Tags as it may equally contain Font changes