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

Add an initial setup for the AI base chunk manager #6677

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
88 changes: 88 additions & 0 deletions lua/aibrains/components/AIBaseChunkManagerBrainComponent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--******************************************************************************************************
--** Copyright (c) 2025 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 TableInsert = table.insert
local VerifyBaseChunk = import("/lua/shared/AIBrain/AIBaseChunkSharedUtils.lua").VerifyBaseChunk

--- Responsible for managing the available chunks of a brain.
---@class AIChunkBrainComponent
---@field IsChunkModuleLoaded table<string, boolean> # Quick lookup whether the given chunk is already loaded
---@field ChunksBySize table<number, AIChunkTemplate[]>
AIChunkTemplateBrainComponent = ClassSimple {

--- Populates the necessary state
---@param self AIChunkBrainComponent | moho.aibrain_methods
OnCreateAI = function(self)
self.IsChunkModuleLoaded = {}
self.Chunks = {}
end,

--- Loads in a chunk. Function is idempotent - loading in the same chunk twice won't do anything.
---@param self AIChunkBrainComponent | moho.aibrain_methods
---@return 'AlreadyLoaded' | 'NoChunkInModule' | 'ChunkIsInvalid'?
LoadChunk = function(self, modulePath)
if self.IsChunkModuleLoaded[module] then
return 'AlreadyLoaded'
end

-- we intentionally use `import` here and not `doscript`. Modules that are
-- loaded via `import` are cached. That means even if multiple AIBrains load
-- the same chunk, they'll all have the same reference to that chunk. This
-- is fine since chunks are read-only.

local module = import(modulePath)
local chunk = module.Chunk
if not chunk then
return 'NoChunkInModule'
end

-- validity check
if not VerifyBaseChunk(chunk) then
WARN(string.format("Invalid chunk: %s", modulePath))
return 'ChunkIsInvalid'
end

-- keep track that this chunk is loaded
self.IsChunkModuleLoaded[module] = true
TableInsert(self.ChunksBySize[chunk.Size], chunk)
end,

--- Retrieves a random base chunk of a given chunk size.
---@param self AIChunkBrainComponent | moho.aibrain_methods
---@param chunkSize number # a number between 1 and 16
---@return AIBaseChunk?
---@return 'NoChunksOfSize' | 'InvalidInput' | nil
GetChunkBySize = function(self, chunkSize)
-- todo
return nil
end,

--- Retrieves a base chunk of a given chunk size that has at least one building site that has a type preference that matches the given unitId.
---@param self AIChunkBrainComponent | moho.aibrain_methods
---@param chunkSize number
---@param unitId UnitId
---@return AIBaseChunk?
GetChunkByUnitId = function(self, chunkSize, unitId, force)
-- todo
return nil
end,
}
10 changes: 5 additions & 5 deletions lua/keymap/debugKeyActions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -290,23 +290,23 @@ local keyActionsDebugAI = {
category = 'ai'
},
['create_build_template_02'] = {
action = 'UI_Lua import("/lua/ui/game/aichunktemplates.lua").AddUnitSelectionToEmptyChunkTemplate(2)',
action = 'UI_Lua import("/lua/ui/game/aibrains/AIBaseChunkUIUtils.lua").CreateBaseChunkFromUnitSelection(2)',
category = 'ai'
},
['create_build_template_04'] = {
action = 'UI_Lua import("/lua/ui/game/aichunktemplates.lua").AddUnitSelectionToEmptyChunkTemplate(4)',
action = 'UI_Lua import("/lua/ui/game/aibrains/AIBaseChunkUIUtils.lua").CreateBaseChunkFromUnitSelection(4)',
category = 'ai'
},
['create_build_template_08'] = {
action = 'UI_Lua import("/lua/ui/game/aichunktemplates.lua").AddUnitSelectionToEmptyChunkTemplate(8)',
action = 'UI_Lua import("/lua/ui/game/aibrains/AIBaseChunkUIUtils.lua").CreateBaseChunkFromUnitSelection(8)',
category = 'ai'
},
['create_build_template_16'] = {
action = 'UI_Lua import("/lua/ui/game/aichunktemplates.lua").AddUnitSelectionToEmptyChunkTemplate(16)',
action = 'UI_Lua import("/lua/ui/game/aibrains/AIBaseChunkUIUtils.lua").CreateBaseChunkFromUnitSelection(16)',
category = 'ai'
},
['create_build_template_32'] = {
action = 'UI_Lua import("/lua/ui/game/aichunktemplates.lua").AddUnitSelectionToEmptyChunkTemplate(32)',
action = 'UI_Lua import("/lua/ui/game/aibrains/AIBaseChunkUIUtils.lua").CreateBaseChunkFromUnitSelection(32)',
category = 'ai'
},
}
Expand Down
Loading