forked from CivLeague/BBG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitPanelBBG.lua
More file actions
395 lines (344 loc) · 18.8 KB
/
UnitPanelBBG.lua
File metadata and controls
395 lines (344 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
include("UnitPanel_Expansion2");
local modified_Commands = {};
modified_Commands.CONDEMN_MOD = {};
modified_Commands.CONDEMN_MOD.Properties = {};
modified_Commands.CONDEMN_MOD.EventName = "ModifiedCommand_Condemn";
modified_Commands.CONDEMN_MOD.CategoryInUI = "SPECIFIC";
modified_Commands.CONDEMN_MOD.Icon = "ICON_UNITCOMMAND_CONDEMN_HERETIC";
modified_Commands.CONDEMN_MOD.ToolTipString = "Condemn Heretic [NEWLINE][NEWLINE]Reduces health by 50";
modified_Commands.CONDEMN_MOD.DisabledToolTipString = "";
modified_Commands.CONDEMN_MOD.VisibleInUI = true;
-- ===========================================================================
-- Refresh unit actions
-- Returns a table of unit actions.
-- ===========================================================================
function GetUnitActionsTable( pUnit )
-- Build action table; holds sub-tables of commands & operations based on UI categories set in DB.
-- Also defines order actions show in panel.
local actionsTable = {
ATTACK = {},
BUILD = {},
GAMEMODIFY = {},
MOVE = {},
OFFENSIVESPY = {},
INPLACE = {},
SECONDARY = {},
SPECIFIC = {},
displayOrder = {
primaryArea = {"ATTACK","OFFENSIVESPY","SPECIFIC","MOVE","INPLACE","GAMEMODIFY"}, -- How they appear in the UI
secondaryArea = {"SECONDARY"}
}
};
local bestValidImprovement:number = -1; -- To recommend to player.
if pUnit == nil then
UI.DataError("NIL unit when attempting to get action table.");
return;
end
local unitType :string = GameInfo.Units[pUnit:GetUnitType()].UnitType;
for commandRow in GameInfo.UnitCommands() do
if ( commandRow.VisibleInUI ) then
local actionHash :number = commandRow.Hash;
local isDisabled :boolean = IsDisabledByTutorial(unitType, actionHash );
if (actionHash == UnitCommandTypes.MOVE_JUMP) then
local foo = 0;
end
if (actionHash == UnitCommandTypes.ENTER_FORMATION) then
--Check if there are any units in the same tile that this unit can create a formation with
--Call CanStartCommand asking for results
local bCanStart, tResults = UnitManager.CanStartCommand( pUnit, actionHash, nil, true);
if (bCanStart and tResults) then
if (tResults[UnitCommandResults.UNITS] ~= nil and #tResults[UnitCommandResults.UNITS] ~= 0) then
local tUnits = tResults[UnitCommandResults.UNITS];
for i, unit in ipairs(tUnits) do
local pUnitInstance = Players[unit.player]:GetUnits():FindID(unit.id);
if (pUnitInstance ~= nil) then
local toolTipString :string = Locale.Lookup(commandRow.Description, GameInfo.Units[pUnitInstance:GetUnitType()].Name);
local callback :ifunction = function() OnUnitActionClicked_EnterFormation(pUnitInstance) end
AddActionToTable( actionsTable, commandRow, isDisabled, toolTipString, actionHash, callback );
end
end
end
end
elseif (actionHash == UnitCommandTypes.PROMOTE) then
--Call CanStartCommand asking for a list of possible promotions for that unit
local bCanStart, tResults = UnitManager.CanStartCommand( pUnit, actionHash, true, true);
if (bCanStart and tResults) then
if (tResults[UnitCommandResults.PROMOTIONS] ~= nil and #tResults[UnitCommandResults.PROMOTIONS] ~= 0) then
local tPromotions = tResults[UnitCommandResults.PROMOTIONS];
local toolTipString = Locale.Lookup(commandRow.Description);
local callback = function() ShowPromotionsList(tPromotions); end
AddActionToTable( actionsTable, commandRow, isDisabled, toolTipString, actionHash, callback );
end
end
elseif (actionHash == UnitCommandTypes.NAME_UNIT) then
local bCanStart = UnitManager.CanStartCommand( pUnit, UnitCommandTypes.NAME_UNIT, true) and GameCapabilities.HasCapability("CAPABILITY_RENAME");
if (bCanStart) then
local toolTipString = Locale.Lookup(commandRow.Description);
AddActionToTable( actionsTable, commandRow, isDisabled, toolTipString, actionHash, OnNameUnit );
end
elseif (actionHash == UnitCommandTypes.DELETE) then
local bCanStart = UnitManager.CanStartCommand( pUnit, UnitCommandTypes.DELETE, true);
if (bCanStart) then
local toolTipString = Locale.Lookup(commandRow.Description);
AddActionToTable( actionsTable, commandRow, isDisabled, toolTipString, actionHash, OnPromptToDeleteUnit );
end
elseif (actionHash == UnitCommandTypes.CANCEL and GameInfo.Units[unitType].Spy) then
-- Route the cancel action for spies to the espionage popup for cancelling a mission
local bCanStart = UnitManager.CanStartCommand( pUnit, actionHash, true);
if (bCanStart) then
local bCanStartNow, tResults = UnitManager.CanStartCommand( pUnit, actionHash, false, true);
AddActionToTable( actionsTable, commandRow, isDisabled, Locale.Lookup("LOC_UNITPANEL_ESPIONAGE_CANCEL_MISSION"), actionHash, OnUnitActionClicked_CancelSpyMission, UnitCommandTypes.TYPE, actionHash );
end
--sss->
elseif (actionHash == UnitCommandTypes.CONDEMN_HERETIC) then
local bCanStart = UnitManager.CanStartCommand( pUnit, actionHash, true);
if (bCanStart) then
local bCanStartNow, tResults = UnitManager.CanStartCommand( pUnit, actionHash, false, true);
local bDisabled = not bCanStartNow;
local pCommandTable : table = modified_Commands.CONDEMN_MOD;
local toolTipString : string = pCommandTable.ToolTipString or "Undefined Unit Command";
local pCallback : ifunction =
function()
local pSelectedUnit = UI.GetHeadSelectedUnit();
if (pSelectedUnit == nil) then
return;
end
local tParameters = {};
tParameters[UnitCommandTypes.PARAM_NAME] = pCommandTable.EventName or "";
UnitManager.RequestCommand(pSelectedUnit, UnitCommandTypes.EXECUTE_SCRIPT, tParameters);
end
if ( bDisabled ) then
if (tResults[UnitOperationResults.FAILURE_REASONS] ~= nil) then
for i,v in ipairs(tResults[UnitOperationResults.FAILURE_REASONS]) do
toolTipString = toolTipString .. "[NEWLINE]" .. "[COLOR:Red]" .. Locale.Lookup(v) .. "[ENDCOLOR]";
end
end
end
AddActionToTable(actionsTable, pCommandTable, bDisabled, toolTipString, UnitCommandTypes.EXECUTE_SCRIPT, pCallback);
end
--<-sss
else
-- The UI check of an operation is a loose check where it only fails if the unit could never do the command.
local bCanStart = UnitManager.CanStartCommand( pUnit, actionHash, true);
if (bCanStart) then
-- Check again if the operation can occur, this time for real.
local bCanStartNow, tResults = UnitManager.CanStartCommand( pUnit, actionHash, false, true);
local bDisabled = not bCanStartNow;
local toolTipString:string;
if (actionHash == UnitCommandTypes.UPGRADE) then
-- if it's a unit upgrade action, add the unit it will upgrade to in the tooltip as well as the upgrade cost
if (tResults ~= nil) then
if (tResults[UnitCommandResults.UNIT_TYPE] ~= nil) then
local upgradeUnitName = GameInfo.Units[tResults[UnitCommandResults.UNIT_TYPE]].Name;
toolTipString = Locale.Lookup(upgradeUnitName);
local upgradeCost = pUnit:GetUpgradeCost();
if (upgradeCost ~= nil) then
toolTipString = Locale.Lookup("LOC_UNITOPERATION_UPGRADE_INFO", upgradeUnitName, upgradeCost);
end
toolTipString = toolTipString .. AddUpgradeResourceCost(pUnit);
end
end
elseif (actionHash == UnitCommandTypes.FORM_CORPS) then
if (GameInfo.Units[unitType].Domain == "DOMAIN_SEA") then
toolTipString = Locale.Lookup("LOC_UNITCOMMAND_FORM_FLEET_DESCRIPTION");
else
toolTipString = Locale.Lookup(commandRow.Description);
end
elseif (actionHash == UnitCommandTypes.FORM_ARMY) then
if (GameInfo.Units[unitType].Domain == "DOMAIN_SEA") then
toolTipString = Locale.Lookup("LOC_UNITCOMMAND_FORM_ARMADA_DESCRIPTION");
else
toolTipString = Locale.Lookup(commandRow.Description);
end
else
toolTipString = Locale.Lookup(commandRow.Description);
end
if (tResults ~= nil) then
if (tResults[UnitOperationResults.ACTION_NAME] ~= nil and tResults[UnitOperationResults.ACTION_NAME] ~= "") then
toolTipString = Locale.Lookup(tResults[UnitOperationResults.ACTION_NAME]);
end
if (tResults[UnitOperationResults.ADDITIONAL_DESCRIPTION] ~= nil) then
toolTipString = toolTipString .. "[NEWLINE]"; -- Line break before add'l desc block
for i,v in ipairs(tResults[UnitOperationResults.ADDITIONAL_DESCRIPTION]) do
toolTipString = toolTipString .. "[NEWLINE]" .. Locale.Lookup(v);
end
if (tResults[UnitOperationResults.FAILURE_REASONS] ~= nil) then
if (table.count(tResults[UnitOperationResults.FAILURE_REASONS]) > 0) then
toolTipString = toolTipString .. "[NEWLINE]"; -- Line break after add'l desc block if there are failure reasons to report
end
end
end
-- Are there any failure reasons?
if ( bDisabled ) then
if (tResults[UnitOperationResults.FAILURE_REASONS] ~= nil) then
-- Add the reason(s) to the tool tip
for i,v in ipairs(tResults[UnitOperationResults.FAILURE_REASONS]) do
toolTipString = toolTipString .. "[NEWLINE]" .. "[COLOR:Red]" .. Locale.Lookup(v) .. "[ENDCOLOR]";
end
end
end
end
isDisabled = bDisabled or isDisabled; -- Mix in tutorial disabledness
local overrideIcon:string = nil;
isDisabled, tooltipString, overrideIcon = LateCheckActionBeforeAdd( kActionsTable, actionHash, isDisabled, tooltipString, overrideIcon );
AddActionToTable( actionsTable, commandRow, isDisabled, toolTipString, actionHash, OnUnitActionClicked, UnitCommandTypes.TYPE, actionHash, overrideIcon );
end
end
end
end
-- Loop over the UnitOperations (like commands but may take 1 to N turns to complete)
-- Only show the operations if the unit has moves left.
local isHasMovesLeft = pUnit:GetMovesRemaining() > 0;
if isHasMovesLeft then
for operationRow in GameInfo.UnitOperations() do
local actionHash :number = operationRow.Hash;
local isDisabled :boolean= IsDisabledByTutorial( unitType, actionHash );
-- if unit can build an improvement, show all the buildable improvements for that tile
if IsBuildingImprovement(actionHash) then
local tParameters = GetBuildImprovementParameters(actionHash, pUnit);
--Call CanStartOperation asking for results
local bCanStart, tResults = UnitManager.CanStartOperation( pUnit, actionHash, nil, tParameters, true);
if (bCanStart and tResults ~= nil) then
if (tResults[UnitOperationResults.IMPROVEMENTS] ~= nil and #tResults[UnitOperationResults.IMPROVEMENTS] ~= 0) then
bestValidImprovement = tResults[UnitOperationResults.BEST_IMPROVEMENT];
local tImprovements = tResults[UnitOperationResults.IMPROVEMENTS];
for i, eImprovement in ipairs(tImprovements) do
tParameters[UnitOperationTypes.PARAM_IMPROVEMENT_TYPE] = eImprovement;
local improvement = GameInfo.Improvements[eImprovement];
bCanStart, tResults = UnitManager.CanStartOperation(pUnit, actionHash, nil, tParameters, true);
local isDisabled = not bCanStart;
local toolTipString = Locale.Lookup(operationRow.Description) .. ": " .. Locale.Lookup(improvement.Name);
if tResults ~= nil then
if (tResults[UnitOperationResults.ADDITIONAL_DESCRIPTION] ~= nil) then
for i,v in ipairs(tResults[UnitOperationResults.ADDITIONAL_DESCRIPTION]) do
toolTipString = toolTipString .. "[NEWLINE]" .. Locale.Lookup(v);
end
end
-- Are there any failure reasons?
if isDisabled then
if (tResults[UnitOperationResults.FAILURE_REASONS] ~= nil) then
-- Add the reason(s) to the tool tip
for i,v in ipairs(tResults[UnitOperationResults.FAILURE_REASONS]) do
toolTipString = toolTipString .. "[NEWLINE]" .. "[COLOR:Red]" .. Locale.Lookup(v) .. "[ENDCOLOR]";
end
end
end
end
-- If this improvement is the same enum as what the game marked as "the best" for this plot, set this flag for the UI to use.
if ( bestValidImprovement ~= nil and bestValidImprovement ~= -1 and bestValidImprovement == eImprovement ) then
improvement["IsBestImprovement"] = true;
else
improvement["IsBestImprovement"] = false;
end
improvement["CategoryInUI"] = "BUILD"; -- TODO: Force improvement to be a type of "BUILD", this can be removed if CategoryInUI is added to "Improvements" in the database schema. ??TRON
local callbackFn, isDisabled = GetBuildImprovementCallback( actionHash, isDisabled );
AddActionToTable( actionsTable, improvement, isDisabled, toolTipString, actionHash, callbackFn, improvement.Hash );
end
end
end
elseif (actionHash == UnitOperationTypes.MOVE_TO) then
local bCanStart :boolean= UnitManager.CanStartOperation( pUnit, UnitOperationTypes.MOVE_TO, nil, false, false); -- No exclusion test, no results
if (bCanStart) then
local toolTipString :string = Locale.Lookup(operationRow.Description);
AddActionToTable( actionsTable, operationRow, isDisabled, toolTipString, actionHash, OnUnitActionClicked_MoveTo );
end
elseif (operationRow.CategoryInUI == "OFFENSIVESPY") then
local bCanStart :boolean= UnitManager.CanStartOperation( pUnit, actionHash, nil, false, false); -- No exclusion test, no result
if (bCanStart) then
---- We only want a single offensive spy action which opens the EspionageChooser side panel
if actionsTable[operationRow.CategoryInUI] ~= nil and table.count(actionsTable[operationRow.CategoryInUI]) == 0 then
local toolTipString :string = Locale.Lookup("LOC_UNITPANEL_ESPIONAGE_CHOOSE_MISSION");
AddActionToTable( actionsTable, operationRow, isDisabled, toolTipString, actionHash, OnUnitActionClicked, UnitOperationTypes.TYPE, actionHash, "ICON_UNITOPERATION_SPY_MISSIONCHOOSER");
end
end
elseif (actionHash == UnitOperationTypes.SPY_COUNTERSPY) then
local bCanStart, tResults = UnitManager.CanStartOperation( pUnit, actionHash, nil, true );
if (bCanStart) then
local toolTipString = Locale.Lookup(operationRow.Description);
AddActionToTable( actionsTable, operationRow, isDisabled, toolTipString, actionHash, OnUnitActionClicked, UnitOperationTypes.TYPE, actionHash, "ICON_UNITOPERATION_SPY_COUNTERSPY_ACTION");
end
elseif (actionHash == UnitOperationTypes.FOUND_CITY) then
local bCanStart, tResults = UnitManager.CanStartOperation( pUnit, UnitOperationTypes.FOUND_CITY, nil, false, OperationResultsTypes.ALL); -- No exclusion test
if (bCanStart) then
local toolTipString :string = Locale.Lookup(operationRow.Description);
AddActionToTable( actionsTable, operationRow, isDisabled, toolTipString, actionHash, function() OnUnitActionClicked_FoundCity(tResults); end);
end
elseif (actionHash == UnitOperationTypes.WMD_STRIKE) then
-- if unit can deploy a WMD, create a unit action for each type
-- first check if the unit is capable of deploying a WMD
local bCanStart = UnitManager.CanStartOperation( pUnit, UnitOperationTypes.WMD_STRIKE, nil, true);
if (bCanStart) then
for entry in GameInfo.WMDs() do
local tParameters = {};
tParameters[UnitOperationTypes.PARAM_WMD_TYPE] = entry.Index;
bCanStart, tResults = UnitManager.CanStartOperation(pUnit, actionHash, nil, tParameters, true);
local isWMDTypeDisabled:boolean = (not bCanStart) or isDisabled;
local toolTipString :string = Locale.Lookup(operationRow.Description);
local wmd = entry.Index;
toolTipString = toolTipString .. "[NEWLINE]" .. Locale.Lookup(entry.Name);
local callBack =
function(void1,void2,mode)
OnUnitActionClicked_WMDStrike(wmd,mode);
end
-- Are there any failure reasons?
if ( not bCanStart ) then
if tResults ~= nil and (tResults[UnitOperationResults.FAILURE_REASONS] ~= nil) then
-- Add the reason(s) to the tool tip
for i,v in ipairs(tResults[UnitOperationResults.FAILURE_REASONS]) do
toolTipString = toolTipString .. "[NEWLINE]" .. "[COLOR:Red]" .. Locale.Lookup(v) .. "[ENDCOLOR]";
end
end
if(not IsActionLimited(entry.WeaponType))then
AddActionToTable( actionsTable, operationRow, isWMDTypeDisabled, toolTipString, actionHash, callBack );
end
else
AddActionToTable( actionsTable, operationRow, isWMDTypeDisabled, toolTipString, actionHash, callBack );
end
end
end
else
-- Is this operation visible in the UI?
-- The UI check of an operation is a loose check where it only fails if the unit could never do the operation.
if ( operationRow.VisibleInUI ) then
local bCanStart:boolean, tResults:table = UnitManager.CanStartOperation( pUnit, actionHash, nil, true );
if (bCanStart) then
-- Check again if the operation can occur, this time for real.
bCanStart, tResults = UnitManager.CanStartOperation(pUnit, actionHash, nil, false, OperationResultsTypes.NO_TARGETS); -- Hint that we don't require possibly expensive target results.
local bDisabled:boolean = not bCanStart;
local toolTipString:string = GetUnitOperationTooltip(operationRow);
if (tResults ~= nil) then
if (tResults[UnitOperationResults.ACTION_NAME] ~= nil and tResults[UnitOperationResults.ACTION_NAME] ~= "") then
toolTipString = Locale.Lookup(tResults[UnitOperationResults.ACTION_NAME]);
end
if (tResults[UnitOperationResults.FEATURE_TYPE] ~= nil) then
local featureName = GameInfo.Features[tResults[UnitOperationResults.FEATURE_TYPE]].Name;
toolTipString = toolTipString .. ": " .. Locale.Lookup(featureName);
end
if (tResults[UnitOperationResults.ADDITIONAL_DESCRIPTION] ~= nil) then
for i,v in ipairs(tResults[UnitOperationResults.ADDITIONAL_DESCRIPTION]) do
toolTipString = toolTipString .. "[NEWLINE]" .. Locale.Lookup(v);
end
end
-- Are there any failure reasons?
if ( bDisabled ) then
if (tResults[UnitOperationResults.FAILURE_REASONS] ~= nil) then
-- Add the reason(s) to the tool tip
for i,v in ipairs(tResults[UnitOperationResults.FAILURE_REASONS]) do
toolTipString = toolTipString .. "[NEWLINE]" .. "[COLOR:Red]" .. Locale.Lookup(v) .. "[ENDCOLOR]";
end
end
end
end
isDisabled = bDisabled or isDisabled;
if(not IsActionLimited(operationRow.PrimaryKey, pUnit))then
local overrideIcon:string = nil;
isDisabled, toolTipString, overrideIcon = LateCheckOperationBeforeAdd( tResults, actionsTable, actionHash, isDisabled, toolTipString, overrideIcon );
AddActionToTable( actionsTable, operationRow, isDisabled, toolTipString, actionHash, OnUnitActionClicked, UnitOperationTypes.TYPE, actionHash, overrideIcon );
end
end
end
end
end
end
return actionsTable;
end