Processes.md - Mirroar/hivemind GitHub Wiki
Processes
Processes are the backbone of Hivemind's multitasking and scheduling system. They allow the bot to manage many tasks in parallel, such as creep management, scouting, expansion, and more. This page explains how processes are called, how to write your own, and how process intervals interact with CPU management.
How Processes Are Called
- Most core processes are started in
src/main.ts
usinghivemind.runProcess
. Each process is given a unique name, a class, and optional parameters (such as interval and priority). - You can also start your own processes from the
onTick
callback in your settings, or from within other processes. See Using Callbacks for more details ononTick
. - Example from
main.ts
:hivemind.runProcess('creeps', CreepsProcess, { priority: PROCESS_PRIORITY_ALWAYS, }); hivemind.runProcess('strategy.scout', ScoutProcess, { interval: hivemind.settings.get('scoutProcessInterval'), priority: PROCESS_PRIORITY_LOW, requireSegments: true, });
Writing Your Own Process
To create a custom process, extend the base Process
class (see src/process/process.ts
).
import Process from 'process/process';
export default class MyCustomProcess extends Process {
run() {
// Your logic here
console.log(`My custom process ran on tick ${Game.time}!`);
}
}
You can then start your process in onTick
or another process:
import hivemind from 'hivemind';
import MyCustomProcess from './my-custom-process';
export function myOnTick() {
hivemind.runProcess('my.custom', MyCustomProcess, {interval: 50});
}
Process Intervals and CPU Management
- Each process can specify an
interval
(in ticks) for how often it should run. Lower intervals mean more frequent execution. - If the bot is under CPU pressure, intervals may be automatically increased to prioritize critical tasks and avoid exceeding CPU limits.
- This means your process might run less often than requested if the system is busy.
Process Priorities
Hivemind uses process priorities to determine which processes are throttled or stopped first when CPU resources are low. The available priority levels are:
PROCESS_PRIORITY_ALWAYS
: Critical processes that should always run, even under extreme CPU pressure.PROCESS_PRIORITY_HIGH
: Important processes that are throttled only when CPU is very low.PROCESS_PRIORITY_DEFAULT
: Standard processes that may be throttled under moderate CPU pressure.PROCESS_PRIORITY_LOW
: Non-essential processes that are throttled or stopped first when CPU is limited.
You can set the priority when starting a process:
hivemind.runProcess('my.important', MyImportantProcess, {
interval: 10,
priority: PROCESS_PRIORITY_HIGH,
});
How priorities affect throttling:
- When CPU usage or bucket is low, processes with lower priorities (
LOW
,DEFAULT
) will be delayed or skipped before higher-priority ones. ALWAYS
priority processes are only skipped in the most extreme cases (e.g., emergency brake when the tick limit is nearly reached).- Use priorities to ensure your most important logic always runs, while less critical tasks can be deferred.
Best Practices
- Use processes for recurring or complex logic that benefits from built-in scheduling and throttling.
- Use the
priority
parameter to ensure important processes run even under CPU pressure. - Avoid heavy logic in
onTick
; prefer processes for anything that isn't trivial. - Use long intervals combined with caching for CPU-intensive tasks to avoid throttling issues.