Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI for massfabs #3883

Merged
merged 15 commits into from May 26, 2022
11 changes: 11 additions & 0 deletions loc/RU/strings_db.lua
Expand Up @@ -8670,6 +8670,17 @@ tooltipui0704="Выберите название для новой конфиг
tooltipui0705="Настройки"
tooltipui0706="Это окно позволяет Вам сохранить текущую конфигурацию игры и загрузить ее позже.\n\nЧтобы сохранить конфигурацию нажмите на кнопку \"Создать\" в этом окне. Вы можете загрузить сохранённую конфигурацию путём её выбора и нажимания \"Загрузить\".\n\nКнопка \"Сохранить\" перезапишет выбранную существующую конфигурацию на текущую."

tooltipui1000="Приход материи"
tooltipui1001="Масса произведенная фабрикаторами в секунду"
tooltipui1002="Расход энергии"
tooltipui1003="Энергия потребляемая фабрикаторами в секунду"
tooltipui1004="Активные фабрикаторы"
tooltipui1005="Количество активных фабрикаторов"
tooltipui1006="Неактивные фабрикаторы"
tooltipui1007="Количество неактивных фабрикаторов"
tooltipui1008="Необходимая энергия"
tooltipui1009="Энергия необходимая для работы всех фабрикатров"

LOADING = "Загрузка..."
sorian_0001="ИИ: Сориан"
sorian_0002="Сбалансированный ИИ. Хорош для 20x20+ карт."
Expand Down
12 changes: 12 additions & 0 deletions loc/US/strings_db.lua
Expand Up @@ -7559,6 +7559,18 @@ tooltipui0640 = "Removes units which hover"
tooltipui0696 = "Rehost"
tooltipui0800 = "Pin Minimap Window"
tooltipui0801 = "Locks the position and layout of the minimap window"

tooltipui1000="Mass Income"
tooltipui1001="Mass being generated with mass fabricators per second"
tooltipui1002="Energy Expense"
tooltipui1003="Energy being spent with mass fabricators per second"
tooltipui1004="Active mass fabricators"
tooltipui1005="Amount of active mass fabricators"
tooltipui1006="Inactive mass fabricators"
tooltipui1007="Amount of inactive mass fabricators"
tooltipui1008="Energy Required"
tooltipui1009="Amount of required energy for mass fabricators to work"

LOADING = "Loading..."
lobui_0427 = "FAF Game Lobby"
lobui_0428 = "Kick Player"
Expand Down
4 changes: 4 additions & 0 deletions lua/UserSync.lua
Expand Up @@ -142,4 +142,8 @@ function OnSync()
if Sync.LobbyOptions then
import('/lua/ui/game/gamemain.lua').LobbyOptions = table.deepcopy(Sync.LobbyOptions)
end

if Sync.MassFabs then
import('/lua/ui/game/massfabs.lua').Update(table.deepcopy(Sync.MassFabs))
end
end
72 changes: 60 additions & 12 deletions lua/aibrain.lua
Expand Up @@ -54,6 +54,11 @@ AIBrain = Class(moho.aibrain_methods) {
self:CreateBrainShared(planName)
self.BrainType = 'Human'

-- for mass fabs tracking
self.TotalEnergyConsumed = 0
self.TotalEnergyRequired = 0
self.TotalMassProduced = 0

-- human-only behavior
self.EnergyExcessThread = ForkThread(self.ToggleEnergyExcessUnitsThread, self)
end,
Expand Down Expand Up @@ -255,6 +260,7 @@ AIBrain = Class(moho.aibrain_methods) {

-- end of engi mod

self.Army = self:GetArmyIndex()
self.Result = nil -- No-op, just to be explicit it starts as nil
self.StatsSent = false
self.UnitStats = {}
Expand Down Expand Up @@ -333,23 +339,35 @@ AIBrain = Class(moho.aibrain_methods) {
-- @param unit The unit to keep track of
AddEnabledEnergyExcessUnit = function (self, unit)
self.EnergyExcessUnitsEnabled[unit.EntityId] = unit
self.EnergyExcessUnitsDisabled[unit.EntityId] = nil
self.EnergyExcessUnitsDisabled[unit.EntityId] = nil

local ecobp = unit.Blueprint.Economy
self.TotalEnergyConsumed = self.TotalEnergyConsumed + ecobp.MaintenanceConsumptionPerSecondEnergy
self.TotalMassProduced = self.TotalMassProduced + ecobp.ProductionPerSecondMass
end,

--- Adds a unit that is enabled / disabled depending on how much energy storage we have. The unit starts disabled
-- @param self The brain itself
-- @param unit The unit to keep track of
AddDisabledEnergyExcessUnit = function (self, unit)
self.EnergyExcessUnitsEnabled[unit.EntityId] = nil
self.EnergyExcessUnitsDisabled[unit.EntityId] = unit
self.EnergyExcessUnitsDisabled[unit.EntityId] = unit
self.TotalEnergyRequired = self.TotalEnergyRequired + unit.Blueprint.Economy.MaintenanceConsumptionPerSecondEnergy
end,

--- Removes a unit that is enabled / disabled depending on how much energy storage we have
-- @param self The brain itself
-- @param unit The unit to forget about
RemoveEnergyExcessUnit = function (self, unit)
self.EnergyExcessUnitsEnabled[unit.EntityId] = nil
self.EnergyExcessUnitsDisabled[unit.EntityId] = nil
local ecobp = unit.Blueprint.Economy
if self.EnergyExcessUnitsEnabled[unit.EntityId] then
self.TotalEnergyConsumed = self.TotalEnergyConsumed - ecobp.MaintenanceConsumptionPerSecondEnergy
self.TotalMassProduced = self.TotalMassProduced - ecobp.ProductionPerSecondMass
self.EnergyExcessUnitsEnabled[unit.EntityId] = nil
elseif self.EnergyExcessUnitsDisabled[unit.EntityId] then
self.TotalEnergyRequired = self.TotalEnergyRequired - ecobp.MaintenanceConsumptionPerSecondEnergy
self.EnergyExcessUnitsDisabled[unit.EntityId] = nil
end
end,

--- A continious thread that across the life span of the brain. Is the heart and sole of the enabling and disabling of units that are designed to eliminate excess energy.
Expand All @@ -367,10 +385,21 @@ AIBrain = Class(moho.aibrain_methods) {
end

-- localize scope for better performance

local pcall = pcall
local ok, msg
local TableSize = table.getsize
local CoroutineYield = CoroutineYield

local ok, msg

local energyRequired
local syncTable = {
on = 0,
off = 0,
totalEnergyConsumed = 0,
totalEnergyRequired = 0,
totalMassProduced = 0
}

local EnergyExcessUnitsDisabled = self.EnergyExcessUnitsDisabled
local EnergyExcessUnitsEnabled = self.EnergyExcessUnitsEnabled

Expand All @@ -386,10 +415,15 @@ AIBrain = Class(moho.aibrain_methods) {
for id, unit in EnergyExcessUnitsEnabled do
if not unit:BeenDestroyed() then

-- update internal state
EnergyExcessUnitsDisabled[unit.EntityId] = unit
EnergyExcessUnitsEnabled[unit.EntityId] = nil
local ecobp = unit.Blueprint.Economy
self.TotalEnergyConsumed = self.TotalEnergyConsumed - ecobp.MaintenanceConsumptionPerSecondEnergy
self.TotalEnergyRequired = self.TotalEnergyRequired + ecobp.MaintenanceConsumptionPerSecondEnergy
self.TotalMassProduced = self.TotalMassProduced - ecobp.ProductionPerSecondMass

-- update internal state
EnergyExcessUnitsDisabled[id] = unit
EnergyExcessUnitsEnabled[id] = nil

-- try to disable unit
ok, msg = pcall(ProtectedOnNoExcessEnergy, unit)

Expand All @@ -408,11 +442,16 @@ AIBrain = Class(moho.aibrain_methods) {
-- while we have units to retrieve
for id, unit in EnergyExcessUnitsDisabled do
if not unit:BeenDestroyed() then
if unit.Blueprint.Economy.MaintenanceConsumptionPerSecondEnergy < energyTrend then
if energyTrend > 100 then

local ecobp = unit.Blueprint.Economy
self.TotalEnergyConsumed = self.TotalEnergyConsumed + ecobp.MaintenanceConsumptionPerSecondEnergy
self.TotalEnergyRequired = self.TotalEnergyRequired - ecobp.MaintenanceConsumptionPerSecondEnergy
self.TotalMassProduced = self.TotalMassProduced + ecobp.ProductionPerSecondMass

-- update internal state
EnergyExcessUnitsDisabled[unit.EntityId] = nil
EnergyExcessUnitsEnabled[unit.EntityId] = unit
EnergyExcessUnitsDisabled[id] = nil
EnergyExcessUnitsEnabled[id] = unit

-- try to enable unit
ok, msg = pcall(ProtectedOnExcessEnergy, unit)
Expand All @@ -428,6 +467,15 @@ AIBrain = Class(moho.aibrain_methods) {
end
end

if self.Army == GetFocusArmy() then
syncTable.on = TableSize(EnergyExcessUnitsEnabled)
syncTable.off = TableSize(EnergyExcessUnitsDisabled)
syncTable.totalEnergyConsumed = self.TotalEnergyConsumed
syncTable.totalEnergyRequired = self.TotalEnergyRequired
syncTable.totalMassProduced = self.TotalMassProduced

Sync.MassFabs = syncTable
end
CoroutineYield(1)
end
end,
Expand Down
2 changes: 2 additions & 0 deletions lua/ui/game/gamemain.lua
Expand Up @@ -232,6 +232,8 @@ function CreateUI(isReplay)
mfdControl = import('/lua/ui/game/multifunction.lua').Create(controlClusterGroup)
controls.mfd = mfdControl

controls.mfp = import('/lua/ui/game/massfabs.lua').Create(statusClusterGroup)

if not isReplay then
ordersControl = import('/lua/ui/game/orders.lua').SetupOrdersControl(controlClusterGroup, mfdControl)
controls.ordersControl = ordersControl
Expand Down