Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lua/SimCallbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ Callbacks.SetResourceSharing = SimUtils.SetResourceSharing

Callbacks.RequestAlliedVictory = SimUtils.RequestAlliedVictory

Callbacks.SetOfferDraw = SimUtils.SetOfferDraw

Callbacks.SetRecallVote = import("/lua/sim/recall.lua").SetRecallVote

Callbacks.SpawnPing = SimPing.SpawnPing
Expand Down Expand Up @@ -303,6 +301,27 @@ Callbacks.FlagShield = function(data, units)
end
end

--#region Draw functionality

--- Initiates or cancels a draw offer.
---@param data { Value : boolean }
Callbacks.SetOfferDraw = function(data)
local commandSource = GetCurrentCommandSource()
local brain = GetArmyBrain(commandSource) --[[@as AIBrain]]
if not brain then
WARN("Invalid draw offer. No pun intended, but the source has no brain.")
return
end

if (data.Value) then
brain:OfferDraw()
else
brain:WithdrawDrawOffer()
end
end

--#endregion

-------------------------------------------------------------------------------
--#region General orders

Expand Down
10 changes: 0 additions & 10 deletions lua/SimUtils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1262,16 +1262,6 @@ function RequestAlliedVictory(data)
brain.RequestingAlliedVictory = data.Value
end

---@param data {Army: Army, Value: boolean}
function SetOfferDraw(data)
local army = data.Army
if not OkayToMessWithArmy(army) then
return
end
local brain = GetArmyBrain(army)
brain.OfferingDraw = data.Value
end

---@param data {Sender: integer, Msg: string}
function SendChatToReplay(data)
if data.Sender and data.Msg then
Expand Down
10 changes: 3 additions & 7 deletions lua/aibrain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local FactoryManagerBrainComponent = import("/lua/aibrains/components/factoryman
local JammerManagerBrainComponent = import("/lua/aibrains/components/jammermanagerbraincomponent.lua").JammerManagerBrainComponent
local StatManagerBrainComponent = import("/lua/aibrains/components/statmanagerbraincomponent.lua").StatManagerBrainComponent
local EnergyManagerBrainComponent = import("/lua/aibrains/components/energymanagerbraincomponent.lua").EnergyManagerBrainComponent
local DrawBrainComponent = import("/lua/aibrains/components/drawbraincomponent.lua").DrawBrainComponent

local CommanderSafeTime = import("/lua/simutils.lua").CommanderSafeTime

Expand Down Expand Up @@ -54,7 +55,7 @@ local BrainGetUnitsAroundPoint = moho.aibrain_methods.GetUnitsAroundPoint
local BrainGetListOfUnits = moho.aibrain_methods.GetListOfUnits
local CategoriesDummyUnit = categories.DUMMYUNIT

---@class AIBrain: FactoryManagerBrainComponent, StatManagerBrainComponent, JammerManagerBrainComponent, EnergyManagerBrainComponent, StorageManagerBrainComponent, moho.aibrain_methods
---@class AIBrain: FactoryManagerBrainComponent, StatManagerBrainComponent, JammerManagerBrainComponent, EnergyManagerBrainComponent, StorageManagerBrainComponent, DrawBrainComponent, moho.aibrain_methods
---@field AI boolean
---@field Name string # Army name
---@field Nickname string # Player / AI / character name
Expand All @@ -71,7 +72,7 @@ local CategoriesDummyUnit = categories.DUMMYUNIT
---@field LastUnitKilledBy Army # Which army last killed one of our units. Used for transfering to killer in other victory conditions.
---@field Army Army # Cached `GetArmyIndex` engine call
AIBrain = Class(FactoryManagerBrainComponent, StatManagerBrainComponent, JammerManagerBrainComponent,
EnergyManagerBrainComponent, StorageManagerBrainComponent, moho.aibrain_methods) {
EnergyManagerBrainComponent, StorageManagerBrainComponent, DrawBrainComponent, moho.aibrain_methods) {

Status = 'InProgress',

Expand Down Expand Up @@ -435,11 +436,6 @@ AIBrain = Class(FactoryManagerBrainComponent, StatManagerBrainComponent, JammerM
self:PlayVOSound('OnTransportFull', Sound { Bank = 'XGG', Cue = cue })
end,

---@param self AIBrain
OnDraw = function(self)
self.Status = 'Draw'
end,

---@param self AIBrain
OnVictory = function(self)
self.Status = 'Victory'
Expand Down
70 changes: 70 additions & 0 deletions lua/aibrains/components/DrawBrainComponent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
--******************************************************************************************************
--** Copyright (c) 2024 Willem 'Jip' Wijnia
--**
--** 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.
--******************************************************************************************************

local DrawParams = import("/lua/shared/DrawParams.lua")

---@class DrawOffer
---@field InitiatedAt number

---@class DrawBrainComponent
---@field DrawOffer? DrawOffer
DrawBrainComponent = ClassSimple {

--- Initiates a draw offer.
---@param self DrawBrainComponent | AIBrain
OfferDraw = function(self)
self.DrawOffer = {
InitiatedAt = GetGameTick(),
}

print("Draw offered by " .. self.Nickname)
end,

--- Withdraws a draw offer.
---@param self DrawBrainComponent | AIBrain
WithdrawDrawOffer = function(self)
self.DrawOffer = nil

print("Draw withdrawn by " .. self.Nickname)
end,

--- Returns whether a draw offer is currently active.
---@param self DrawBrainComponent | AIBrain
---@return boolean
WantsToDraw = function(self)
local drawOffer = self.DrawOffer
if not drawOffer then
return false
end

if (drawOffer.InitiatedAt) + DrawParams.VoteTime < GetGameTick() then
return false
end

return true
end,

---@param self AIBrain
OnDraw = function(self)
self.Status = 'Draw'
end,
}
6 changes: 6 additions & 0 deletions lua/shared/DrawParams.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--**************************************************************************************************
--** Shared under the MIT license
--**************************************************************************************************

--- The duration it takes for a draw vote to expire, in ticks (30 seconds).
VoteTime = 30 * 10
2 changes: 1 addition & 1 deletion lua/sim/VictoryCondition/AbstractVictoryCondition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ AbstractVictoryCondition = Class(DebugComponent) {

for k = 1, TableGetn(aiBrains) do
local brain = aiBrains[k]
if not brain.OfferingDraw then
if not brain:WantsToDraw() then
return false
end
end
Expand Down
1 change: 0 additions & 1 deletion lua/ui/game/diplomacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ function BuildPlayerLines()
SimCallback({
Func = "SetOfferDraw",
Args = {
Army = GetFocusArmy(),
Value = checked,
},
})
Expand Down
2 changes: 1 addition & 1 deletion scripts/LaunchFAInstances.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (Test-Path $debuggerExecutable) {
}

# Command-line arguments common for all instances
$baseArguments = '/init "init_dev.lua" /EnableDiskWatch /nomovie /RunWithTheWind /gameoptions CheatsEnabled:true GameSpeed:adjustable'
$baseArguments = '/init "init_local_development.lua" /EnableDiskWatch /nomovie /RunWithTheWind /gameoptions CheatsEnabled:true GameSpeed:adjustable'

# Game-specific settings
$hostProtocol = "udp"
Expand Down