Skip to content

Commit

Permalink
Refactor using shared capture cost function
Browse files Browse the repository at this point in the history
  • Loading branch information
lL1l1 committed Mar 3, 2024
1 parent 4943eb4 commit d84cf3b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
42 changes: 42 additions & 0 deletions lua/shared/captureCost.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--******************************************************************************************************
--** Copyright (c) 2024 IL1I1
--**
--** Permission is hereby granted, free of charge, to any person obtaining a copy
--** of this software and associated documentation files (the "Software"), to deal
--** in the Software without restriction, including without limitation the rights
--** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--** copies of the Software, and to permit persons to whom the Software is
--** furnished to do so, subject to the following conditions:
--**
--** The above copyright notice and this permission notice shall be included in all
--** copies or substantial portions of the Software.
--**
--** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
--** SOFTWARE.
--******************************************************************************************************

-- All functions in this file (inside /lua/shared) should be:
-- - pure: they should only use the arguments provided, do not touch any global state.
-- - sim / ui proof: they should work for both sim code and ui code.

--- Formula to compute the energy and time cost of capturing.
---@param blueprint UnitBlueprint
---@param number buildRate
---@return number time
---@return number energy
GetBlueprintCaptureCost = function(blueprint, buildRate)
local blueprintEconomy = blueprint.Economy

local time = ((blueprintEconomy.BuildTime or 10) / buildRate) / 2
local energy = blueprintEconomy.BuildCostEnergy or 100
if time < 0 then
time = 0.1
end

return time, energy
end
6 changes: 4 additions & 2 deletions lua/sim/Unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ local Weapon = import("/lua/sim/weapon.lua").Weapon
local IntelComponent = import('/lua/defaultcomponents.lua').IntelComponent
local VeterancyComponent = import('/lua/defaultcomponents.lua').VeterancyComponent

local GetBlueprintCaptureCost = import('/lua/shared/captureCost.lua').GetBlueprintCaptureCost

local TrashBag = TrashBag
local TrashAdd = TrashBag.Add
local TrashDestroy = TrashBag.Destroy
Expand Down Expand Up @@ -3976,8 +3978,8 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent) {

-- compute capture costs
local targetBlueprintEconomy = target.Blueprint.Economy
local time = ((targetBlueprintEconomy.BuildTime or 10) / self:GetBuildRate()) / 2
local energy = targetBlueprintEconomy.BuildCostEnergy or 100
local time, energy = GetBlueprintCaptureCost(target.Blueprint, self:GetBuildRate())

time = time * (self.CaptureTimeMultiplier or 1)
if time < 0 then
time = 1
Expand Down
21 changes: 7 additions & 14 deletions lua/ui/controls/reticles/capture.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ local UIUtil = import("/lua/ui/uiutil.lua")
local LayoutHelpers = import("/lua/maui/layouthelpers.lua")
local Reticle = import('/lua/ui/controls/reticle.lua').Reticle

local GetBlueprintCaptureCost = import('/lua/shared/captureCost.lua').GetBlueprintCaptureCost

-- Local upvalues for performance
local GetRolloverInfo = GetRolloverInfo
local GetSelectedUnits = GetSelectedUnits
Expand All @@ -49,7 +51,7 @@ CaptureReticle = ClassUI(Reticle) {
totalBuildRate = totalBuildRate + unit:GetBuildRate()
end
self.selectionBuildRate = totalBuildRate
self.focusArmy = GetArmiesTable().focusArmy
self.focusArmy = GetFocusArmy()

self.BuildTimeIcon = Bitmap(self)
self.BuildTimeIcon:SetTexture(UIUtil.UIFile('/game/unit_view_icons/time.dds'))
Expand All @@ -72,11 +74,10 @@ CaptureReticle = ClassUI(Reticle) {
---@param self CaptureReticle
UpdateDisplay = function(self)
local rolloverInfo = GetRolloverInfo()
local isEnemy, targetBp
local isNotAlly, targetBp
local isCapturable = true
if rolloverInfo then
-- armyIndex is 0-indexed, but IsAlly requires 1-indexed armies.
isEnemy = not IsAlly(self.focusArmy, rolloverInfo.armyIndex + 1)
isNotAlly = not IsAlly(self.focusArmy, rolloverInfo.armyIndex + 1)
targetBp = __blueprints[rolloverInfo.blueprintId]
-- `Unit:IsCapturable()` is sim-side so we will use what we have in the bp.
-- May not work with units from mods or changed by script.
Expand All @@ -89,20 +90,12 @@ CaptureReticle = ClassUI(Reticle) {
end
end
end
if isEnemy and isCapturable then
if isNotAlly and isCapturable then
if self:IsHidden() then
self:SetHidden(false)
end

local targetBpEconomy = targetBp.Economy
-- Mimic Unit.lua GetCaptureCosts calculations
local time = ((targetBpEconomy.BuildTime or 10) / self.selectionBuildRate) / 2
local energy = targetBpEconomy.BuildCostEnergy or 100
-- This multiplier is sim-side. It would be nice to have, but it is rarely used.
-- time = time * (self.CaptureTimeMultiplier or 1)
if time < 0 then
time = 1
end
local time, energy = GetBlueprintCaptureCost(targetBp, self.selectionBuildRate)

self.eText:SetText(string.format('%.0f (-%.0f)', energy, energy/time))
local minutes = MathFloor(time/60)
Expand Down

0 comments on commit d84cf3b

Please sign in to comment.