Skip to content

Commit

Permalink
Add Partial Share (FAForever#4095)
Browse files Browse the repository at this point in the history
Adds a new share condition that transfers buildings and engineers like in full share, but kills other units like in share until death.
  • Loading branch information
Penguin5 authored Dec 5, 2022
1 parent eed79a1 commit 68c11b7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 2 additions & 0 deletions loc/US/strings_db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7735,6 +7735,8 @@ lobui_0792="Manual unit sharing are allowed"
lobui_0793="Yes except builders"
lobui_0794="No manual sharing of builder/factory"
lobui_0795="No manual sharing of units"
lobui_0796="Partial Share"
lobui_0797="Your buildings and engineers will be transferred to your highest rated ally when you die. Your other units will be destroyed when you die, except those captured by the enemy."

aisettings_0001="AIx Cheat Multiplier"
aisettings_0002="Set the cheat multiplier for the cheating AIs."
Expand Down
16 changes: 13 additions & 3 deletions lua/SimUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ local buildersCategory = categories.ALLUNITS - categories.CONSTRUCTION - categor
local sharedUnits = {}

---@param owner number
function KillSharedUnits(owner)
-- categoriesToKill is an optional input (it defaults to all categories)
function KillSharedUnits(owner, categoriesToKill)
local sharedUnitOwner = sharedUnits[owner]
if sharedUnitOwner and not table.empty(sharedUnitOwner) then
for _, unit in sharedUnitOwner do
if not unit.Dead and unit.oldowner == owner then
unit:Kill()
if categoriesToKill then
if ContainsCategory(unit, categoriesToKill) then
table.remove(sharedUnits[owner], unit)
unit:Kill()
end
else
unit:Kill()
end
end
end
sharedUnits[owner] = {}
if not categoriesToKill then
sharedUnits[owner] = {}
end
end
end

Expand Down
24 changes: 18 additions & 6 deletions lua/aibrain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -918,19 +918,25 @@ AIBrain = Class(moho.aibrain_methods) {
end

-- Transfer our units to other brains. Wait in between stops transfer of the same units to multiple armies.
local function TransferUnitsToBrain(brains)
-- Optional Categories input (defaults to all units except wall and command)
local function TransferUnitsToBrain(brains, categoriesToTransfer)
if not table.empty(brains) then
local units
if shareOption == 'FullShare' then
local indexes = {}
for _, brain in brains do
table.insert(indexes, brain.index)
end
local units = self:GetListOfUnits(categories.ALLUNITS - categories.WALL - categories.COMMAND, false)
units = self:GetListOfUnits(categories.ALLUNITS - categories.WALL - categories.COMMAND, false)
TransferUnfinishedUnitsAfterDeath(units, indexes)
end

for k, brain in brains do
local units = self:GetListOfUnits(categories.ALLUNITS - categories.WALL - categories.COMMAND, false)
if categoriesToTransfer then
units = self:GetListOfUnits(categoriesToTransfer, false)
else
units = self:GetListOfUnits(categories.ALLUNITS - categories.WALL - categories.COMMAND, false)
end
if units and not table.empty(units) then
local givenUnitCount = table.getn(TransferUnitsOwnership(units, brain.index))

Expand All @@ -946,7 +952,8 @@ AIBrain = Class(moho.aibrain_methods) {
end

-- Sort the destiniation brains (armies/players) by rating (and if rating does not exist (such as with regular AI's), by score, after players with positive rating)
local function TransferUnitsToHighestBrain(brains)
-- optional category input (default of everything but walls and command)
local function TransferUnitsToHighestBrain(brains, categoriesToTransfer)
if not table.empty(brains) then
local ratings = ScenarioInfo.Options.Ratings
for i, brain in brains do
Expand All @@ -959,7 +966,7 @@ AIBrain = Class(moho.aibrain_methods) {
end
-- sort brains by rating
table.sort(brains, function(a, b) return a.rating > b.rating end)
TransferUnitsToBrain(brains)
TransferUnitsToBrain(brains, categoriesToTransfer)
end
end

Expand Down Expand Up @@ -1041,7 +1048,12 @@ AIBrain = Class(moho.aibrain_methods) {
KillSharedUnits(self:GetArmyIndex()) -- Kill things I gave away
ReturnBorrowedUnits() -- Give back things I was given by others
elseif shareOption == 'FullShare' then
TransferUnitsToHighestBrain(BrainCategories.Allies) -- Transfer things to allies, highest score first
TransferUnitsToHighestBrain(BrainCategories.Allies) -- Transfer things to allies, highest rating first
TransferOwnershipOfBorrowedUnits(BrainCategories.Allies) -- Give stuff away permanently
elseif shareOption == 'PartialShare' then
KillSharedUnits(self:GetArmyIndex(), categories.ALLUNITS - categories.STRUCTURE - categories.ENGINEER) -- Kill some things I gave away
ReturnBorrowedUnits() -- Give back things I was given by others
TransferUnitsToHighestBrain(BrainCategories.Allies, categories.STRUCTURE + categories.ENGINEER) -- Transfer some things to allies, highest rating first
TransferOwnershipOfBorrowedUnits(BrainCategories.Allies) -- Give stuff away permanently
else
GetBackUnits(BrainCategories.Allies) -- Get back units I gave away
Expand Down
2 changes: 2 additions & 0 deletions lua/ui/game/score.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,15 @@ end
local ShareNameLookup = { }
ShareNameLookup["FullShare"] = "lobui_0742"
ShareNameLookup["ShareUntilDeath"] = "lobui_0744"
ShareNameLookup["PartialShare"] = "lobui_0796"
ShareNameLookup["TransferToKiller"] = "lobui_0762"
ShareNameLookup["Defectors"] = "lobui_0766"
ShareNameLookup["CivilianDeserter"] = "lobui_0764"

local ShareDescriptionLookup = { }
ShareDescriptionLookup["FullShare"] = "lobui_0743"
ShareDescriptionLookup["ShareUntilDeath"] = "lobui_0745"
ShareDescriptionLookup["PartialShare"] = "lobui_0797"
ShareDescriptionLookup["TransferToKiller"] = "lobui_0763"
ShareDescriptionLookup["Defectors"] = "lobui_0767"
ShareDescriptionLookup["CivilianDeserter"] = "lobui_0765"
Expand Down
7 changes: 6 additions & 1 deletion lua/ui/lobby/lobbyOptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
---@field RevealCivilians 'No' | 'Yes'
---@field RandomMap 'Off' | 'Official' | 'All'
---@field Score 'no' | 'yes'
---@field Share 'FullShare' | 'ShareUntilDeath' | 'TransferToKiller' | 'Defectors' | 'CivilianDeserter'
---@field Share 'FullShare' | 'ShareUntilDeath' | 'PartialShare' | 'TransferToKiller' | 'Defectors' | 'CivilianDeserter'
---@field ShareUnitCap 'none' | 'allies' | 'all'
---@field Timeouts '0' | '3'| '-1'
---@field UnitCap '125' | '250' | '375' | '500' | '625' | '750' | '875' | '1000' | '1250' | '1500'
Expand Down Expand Up @@ -205,6 +205,11 @@ globalOpts = {
help = "<LOC lobui_0745>All units you have built this game will be destroyed when you die, except those captured by the enemy.",
key = 'ShareUntilDeath',
},
{
text = "<LOC lobui_0796>Partial Share",
help = "<LOC lobui_0797>Your buildings and engineers will be transferred to your highest rated ally when you die. Your other units will be destroyed when you die, except those captured by the enemy.",
key = 'PartialShare',
},
{
text = "<LOC lobui_0762>Traitors",
help = "<LOC lobui_0763>Your units will be transferred to the control of your killer.",
Expand Down

0 comments on commit 68c11b7

Please sign in to comment.