@supalosa/chronodivide-bot 0.5.4 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.template +4 -4
- package/.github/workflows/npm-publish.yml +24 -0
- package/README.md +108 -103
- package/dist/bot/bot.js +105 -105
- package/dist/bot/logic/awareness.js +136 -136
- package/dist/bot/logic/building/antiAirStaticDefence.js +42 -42
- package/dist/bot/logic/building/antiGroundStaticDefence.js +34 -34
- package/dist/bot/logic/building/{ArtilleryUnit.js → artilleryUnit.js} +18 -18
- package/dist/bot/logic/building/basicAirUnit.js +19 -19
- package/dist/bot/logic/building/basicBuilding.js +26 -26
- package/dist/bot/logic/building/basicGroundUnit.js +19 -19
- package/dist/bot/logic/building/buildingRules.js +175 -175
- package/dist/bot/logic/building/common.js +19 -19
- package/dist/bot/logic/building/harvester.js +16 -16
- package/dist/bot/logic/building/powerPlant.js +20 -20
- package/dist/bot/logic/building/queueController.js +183 -183
- package/dist/bot/logic/building/resourceCollectionBuilding.js +36 -36
- package/dist/bot/logic/common/scout.js +126 -126
- package/dist/bot/logic/common/utils.js +95 -95
- package/dist/bot/logic/composition/alliedCompositions.js +12 -12
- package/dist/bot/logic/composition/common.js +1 -1
- package/dist/bot/logic/composition/sovietCompositions.js +12 -12
- package/dist/bot/logic/map/map.js +44 -44
- package/dist/bot/logic/map/sector.js +137 -137
- package/dist/bot/logic/mission/actionBatcher.js +91 -91
- package/dist/bot/logic/mission/mission.js +122 -122
- package/dist/bot/logic/mission/missionController.js +321 -321
- package/dist/bot/logic/mission/missionFactories.js +12 -12
- package/dist/bot/logic/mission/missions/attackMission.js +214 -214
- package/dist/bot/logic/mission/missions/defenceMission.js +82 -82
- package/dist/bot/logic/mission/missions/engineerMission.js +63 -63
- package/dist/bot/logic/mission/missions/expansionMission.js +60 -60
- package/dist/bot/logic/mission/missions/retreatMission.js +33 -33
- package/dist/bot/logic/mission/missions/scoutingMission.js +133 -133
- package/dist/bot/logic/mission/missions/squads/combatSquad.js +115 -115
- package/dist/bot/logic/mission/missions/squads/common.js +57 -57
- package/dist/bot/logic/mission/missions/squads/squad.js +1 -1
- package/dist/bot/logic/threat/threat.js +22 -22
- package/dist/bot/logic/threat/threatCalculator.js +73 -73
- package/dist/exampleBot.js +100 -100
- package/package.json +32 -29
- package/src/bot/bot.ts +161 -161
- package/src/bot/logic/awareness.ts +245 -245
- package/src/bot/logic/building/antiAirStaticDefence.ts +64 -64
- package/src/bot/logic/building/antiGroundStaticDefence.ts +55 -55
- package/src/bot/logic/building/artilleryUnit.ts +39 -39
- package/src/bot/logic/building/basicAirUnit.ts +39 -39
- package/src/bot/logic/building/basicBuilding.ts +49 -49
- package/src/bot/logic/building/basicGroundUnit.ts +39 -39
- package/src/bot/logic/building/buildingRules.ts +250 -250
- package/src/bot/logic/building/common.ts +21 -21
- package/src/bot/logic/building/harvester.ts +31 -31
- package/src/bot/logic/building/powerPlant.ts +32 -32
- package/src/bot/logic/building/queueController.ts +297 -297
- package/src/bot/logic/building/resourceCollectionBuilding.ts +52 -52
- package/src/bot/logic/common/scout.ts +183 -183
- package/src/bot/logic/common/utils.ts +120 -120
- package/src/bot/logic/composition/alliedCompositions.ts +22 -22
- package/src/bot/logic/composition/common.ts +3 -3
- package/src/bot/logic/composition/sovietCompositions.ts +21 -21
- package/src/bot/logic/map/map.ts +66 -66
- package/src/bot/logic/map/sector.ts +174 -174
- package/src/bot/logic/mission/actionBatcher.ts +124 -124
- package/src/bot/logic/mission/mission.ts +232 -232
- package/src/bot/logic/mission/missionController.ts +413 -413
- package/src/bot/logic/mission/missionFactories.ts +51 -51
- package/src/bot/logic/mission/missions/attackMission.ts +336 -336
- package/src/bot/logic/mission/missions/defenceMission.ts +151 -151
- package/src/bot/logic/mission/missions/engineerMission.ts +113 -113
- package/src/bot/logic/mission/missions/expansionMission.ts +104 -104
- package/src/bot/logic/mission/missions/retreatMission.ts +54 -54
- package/src/bot/logic/mission/missions/scoutingMission.ts +186 -186
- package/src/bot/logic/mission/missions/squads/combatSquad.ts +160 -160
- package/src/bot/logic/mission/missions/squads/common.ts +63 -63
- package/src/bot/logic/mission/missions/squads/squad.ts +19 -19
- package/src/bot/logic/threat/threat.ts +15 -15
- package/src/bot/logic/threat/threatCalculator.ts +100 -100
- package/src/exampleBot.ts +111 -111
- package/tsconfig.json +73 -73
- package/dist/bot/logic/awarenessImpl.js +0 -132
- package/dist/bot/logic/awarenessImpl.js.map +0 -1
- package/dist/bot/logic/building/building.js +0 -126
- package/dist/bot/logic/building/building.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/combatSquad.js +0 -124
- package/dist/bot/logic/mission/behaviours/combatSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/common.js +0 -58
- package/dist/bot/logic/mission/behaviours/common.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/engineerSquad.js +0 -39
- package/dist/bot/logic/mission/behaviours/engineerSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/expansionSquad.js +0 -46
- package/dist/bot/logic/mission/behaviours/expansionSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/retreatSquad.js +0 -31
- package/dist/bot/logic/mission/behaviours/retreatSquad.js.map +0 -1
- package/dist/bot/logic/mission/behaviours/scoutingSquad.js +0 -94
- package/dist/bot/logic/mission/behaviours/scoutingSquad.js.map +0 -1
- package/dist/bot/logic/mission/missions/basicMission.js +0 -13
- package/dist/bot/logic/mission/missions/basicMission.js.map +0 -1
- package/dist/bot/logic/mission/missions/missionBehaviour.js +0 -2
- package/dist/bot/logic/mission/missions/missionBehaviour.js.map +0 -1
- package/dist/bot/logic/mission/missions/oneTimeMission.js +0 -27
- package/dist/bot/logic/mission/missions/oneTimeMission.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/attackSquad.js +0 -89
- package/dist/bot/logic/squad/behaviours/combatSquad.js +0 -102
- package/dist/bot/logic/squad/behaviours/combatSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/common.js +0 -40
- package/dist/bot/logic/squad/behaviours/common.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/defenceSquad.js +0 -61
- package/dist/bot/logic/squad/behaviours/engineerSquad.js +0 -36
- package/dist/bot/logic/squad/behaviours/engineerSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/expansionSquad.js +0 -43
- package/dist/bot/logic/squad/behaviours/expansionSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/retreatSquad.js +0 -28
- package/dist/bot/logic/squad/behaviours/retreatSquad.js.map +0 -1
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js +0 -86
- package/dist/bot/logic/squad/behaviours/scoutingSquad.js.map +0 -1
- package/dist/bot/logic/squad/squad.js +0 -126
- package/dist/bot/logic/squad/squad.js.map +0 -1
- package/dist/bot/logic/squad/squadBehaviour.js +0 -6
- package/dist/bot/logic/squad/squadBehaviour.js.map +0 -1
- package/dist/bot/logic/squad/squadBehaviours.js +0 -7
- package/dist/bot/logic/squad/squadBehaviours.js.map +0 -1
- package/dist/bot/logic/squad/squadController.js +0 -199
- package/dist/bot/logic/squad/squadController.js.map +0 -1
- /package/dist/bot/logic/building/{ArtilleryUnit.js.map → artilleryUnit.js.map} +0 -0
|
@@ -1,184 +1,184 @@
|
|
|
1
|
-
import { QueueStatus, QueueType, } from "@chronodivide/game-api";
|
|
2
|
-
import { BUILDING_NAME_TO_RULES, DEFAULT_BUILDING_PRIORITY, getDefaultPlacementLocation, } from "./buildingRules.js";
|
|
3
|
-
export const QUEUES = [
|
|
4
|
-
QueueType.Structures,
|
|
5
|
-
QueueType.Armory,
|
|
6
|
-
QueueType.Infantry,
|
|
7
|
-
QueueType.Vehicles,
|
|
8
|
-
QueueType.Aircrafts,
|
|
9
|
-
QueueType.Ships,
|
|
10
|
-
];
|
|
11
|
-
export const queueTypeToName = (queue) => {
|
|
12
|
-
switch (queue) {
|
|
13
|
-
case QueueType.Structures:
|
|
14
|
-
return "Structures";
|
|
15
|
-
case QueueType.Armory:
|
|
16
|
-
return "Armory";
|
|
17
|
-
case QueueType.Infantry:
|
|
18
|
-
return "Infantry";
|
|
19
|
-
case QueueType.Vehicles:
|
|
20
|
-
return "Vehicles";
|
|
21
|
-
case QueueType.Aircrafts:
|
|
22
|
-
return "Aircrafts";
|
|
23
|
-
case QueueType.Ships:
|
|
24
|
-
return "Ships";
|
|
25
|
-
default:
|
|
26
|
-
return "Unknown";
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
export class QueueController {
|
|
30
|
-
constructor() {
|
|
31
|
-
this.queueStates = [];
|
|
32
|
-
}
|
|
33
|
-
onAiUpdate(game, productionApi, actionsApi, playerData, threatCache, unitTypeRequests, logger) {
|
|
34
|
-
this.queueStates = QUEUES.map((queueType) => {
|
|
35
|
-
const options = productionApi.getAvailableObjects(queueType);
|
|
36
|
-
const items = this.getPrioritiesForBuildingOptions(game, options, threatCache, playerData, unitTypeRequests, logger);
|
|
37
|
-
const topItem = items.length > 0 ? items[items.length - 1] : undefined;
|
|
38
|
-
return {
|
|
39
|
-
queue: queueType,
|
|
40
|
-
items,
|
|
41
|
-
// only if the top item has a priority above zero
|
|
42
|
-
topItem: topItem && topItem.priority > 0 ? topItem : undefined,
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
const totalWeightAcrossQueues = this.queueStates
|
|
46
|
-
.map((decision) => decision.topItem?.priority)
|
|
47
|
-
.reduce((pV, cV) => pV + cV, 0);
|
|
48
|
-
const totalCostAcrossQueues = this.queueStates
|
|
49
|
-
.map((decision) => decision.topItem?.unit.cost)
|
|
50
|
-
.reduce((pV, cV) => pV + cV, 0);
|
|
51
|
-
this.queueStates.forEach((decision) => {
|
|
52
|
-
this.updateBuildQueue(game, productionApi, actionsApi, playerData, threatCache, decision.queue, decision.topItem, totalWeightAcrossQueues, totalCostAcrossQueues, logger);
|
|
53
|
-
});
|
|
54
|
-
// Repair is simple - just repair everything that's damaged.
|
|
55
|
-
if (playerData.credits > 0) {
|
|
56
|
-
game.getVisibleUnits(playerData.name, "self", (r) => r.repairable).forEach((unitId) => {
|
|
57
|
-
const unit = game.getUnitData(unitId);
|
|
58
|
-
if (!unit || !unit.hitPoints || !unit.maxHitPoints || unit.hasWrenchRepair) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (unit.hitPoints < unit.maxHitPoints) {
|
|
62
|
-
actionsApi.toggleRepairWrench(unitId);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
updateBuildQueue(game, productionApi, actionsApi, playerData, threatCache, queueType, decision, totalWeightAcrossQueues, totalCostAcrossQueues, logger) {
|
|
68
|
-
const myCredits = playerData.credits;
|
|
69
|
-
const queueData = productionApi.getQueueData(queueType);
|
|
70
|
-
if (queueData.status == QueueStatus.Idle) {
|
|
71
|
-
// Start building the decided item.
|
|
72
|
-
if (decision !== undefined) {
|
|
73
|
-
logger(`Decision (${queueTypeToName(queueType)}): ${decision.unit.name}`);
|
|
74
|
-
actionsApi.queueForProduction(queueType, decision.unit.name, decision.unit.type, 1);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
else if (queueData.status == QueueStatus.Ready && queueData.items.length > 0) {
|
|
78
|
-
// Consider placing it.
|
|
79
|
-
const objectReady = queueData.items[0].rules;
|
|
80
|
-
if (queueType == QueueType.Structures || queueType == QueueType.Armory) {
|
|
81
|
-
let location = this.getBestLocationForStructure(game, playerData, objectReady);
|
|
82
|
-
if (location !== undefined) {
|
|
83
|
-
logger(`Completed: ${queueTypeToName(queueType)}: ${objectReady.name}, placing at ${location.rx},${location.ry}`);
|
|
84
|
-
actionsApi.placeBuilding(objectReady.name, location.rx, location.ry);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
logger(`Completed: ${queueTypeToName(queueType)}: ${objectReady.name} but nowhere to place it`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
else if (queueData.status == QueueStatus.Active && queueData.items.length > 0 && decision != null) {
|
|
92
|
-
// Consider cancelling if something else is significantly higher priority than what is currently being produced.
|
|
93
|
-
const currentProduction = queueData.items[0].rules;
|
|
94
|
-
if (decision.unit != currentProduction) {
|
|
95
|
-
// Changing our mind.
|
|
96
|
-
let currentItemPriority = this.getPriorityForBuildingOption(currentProduction, game, playerData, threatCache);
|
|
97
|
-
let newItemPriority = decision.priority;
|
|
98
|
-
if (newItemPriority > currentItemPriority * 2) {
|
|
99
|
-
logger(`Dequeueing queue ${queueTypeToName(queueData.type)} unit ${currentProduction.name} because ${decision.unit.name} has 2x higher priority.`);
|
|
100
|
-
actionsApi.unqueueFromProduction(queueData.type, currentProduction.name, currentProduction.type, 1);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
// Not changing our mind, but maybe other queues are more important for now.
|
|
105
|
-
if (totalCostAcrossQueues > myCredits && decision.priority < totalWeightAcrossQueues * 0.25) {
|
|
106
|
-
logger(`Pausing queue ${queueTypeToName(queueData.type)} because weight is low (${decision.priority}/${totalWeightAcrossQueues})`);
|
|
107
|
-
actionsApi.pauseProduction(queueData.type);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
else if (queueData.status == QueueStatus.OnHold) {
|
|
112
|
-
// Consider resuming queue if priority is high relative to other queues.
|
|
113
|
-
if (myCredits >= totalCostAcrossQueues) {
|
|
114
|
-
logger(`Resuming queue ${queueTypeToName(queueData.type)} because credits are high`);
|
|
115
|
-
actionsApi.resumeProduction(queueData.type);
|
|
116
|
-
}
|
|
117
|
-
else if (decision && decision.priority >= totalWeightAcrossQueues * 0.25) {
|
|
118
|
-
logger(`Resuming queue ${queueTypeToName(queueData.type)} because weight is high (${decision.priority}/${totalWeightAcrossQueues})`);
|
|
119
|
-
actionsApi.resumeProduction(queueData.type);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
getPrioritiesForBuildingOptions(game, options, threatCache, playerData, unitTypeRequests, logger) {
|
|
124
|
-
let priorityQueue = [];
|
|
125
|
-
options.forEach((option) => {
|
|
126
|
-
const calculatedPriority = this.getPriorityForBuildingOption(option, game, playerData, threatCache);
|
|
127
|
-
// Get the higher of the dynamic and the mission priority for the unit.
|
|
128
|
-
const actualPriority = Math.max(calculatedPriority, unitTypeRequests.get(option.name) ?? calculatedPriority);
|
|
129
|
-
if (actualPriority > 0) {
|
|
130
|
-
priorityQueue.push({ unit: option, priority: actualPriority });
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
priorityQueue = priorityQueue.sort((a, b) => a.priority - b.priority);
|
|
134
|
-
return priorityQueue;
|
|
135
|
-
}
|
|
136
|
-
getPriorityForBuildingOption(option, game, playerStatus, threatCache) {
|
|
137
|
-
if (BUILDING_NAME_TO_RULES.has(option.name)) {
|
|
138
|
-
let logic = BUILDING_NAME_TO_RULES.get(option.name);
|
|
139
|
-
return logic.getPriority(game, playerStatus, option, threatCache);
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
// Fallback priority when there are no rules.
|
|
143
|
-
return (DEFAULT_BUILDING_PRIORITY - game.getVisibleUnits(playerStatus.name, "self", (r) => r == option).length);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
getBestLocationForStructure(game, playerData, objectReady) {
|
|
147
|
-
if (BUILDING_NAME_TO_RULES.has(objectReady.name)) {
|
|
148
|
-
let logic = BUILDING_NAME_TO_RULES.get(objectReady.name);
|
|
149
|
-
return logic.getPlacementLocation(game, playerData, objectReady);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
// fallback placement logic
|
|
153
|
-
return getDefaultPlacementLocation(game, playerData, playerData.startLocation, objectReady);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
getGlobalDebugText(gameApi, productionApi) {
|
|
157
|
-
const productionState = QUEUES.reduce((prev, queueType) => {
|
|
158
|
-
if (productionApi.getQueueData(queueType).size === 0) {
|
|
159
|
-
return prev;
|
|
160
|
-
}
|
|
161
|
-
const paused = productionApi.getQueueData(queueType).status === QueueStatus.OnHold;
|
|
162
|
-
return (prev +
|
|
163
|
-
" [" +
|
|
164
|
-
queueTypeToName(queueType) +
|
|
165
|
-
(paused ? " PAUSED" : "") +
|
|
166
|
-
": " +
|
|
167
|
-
productionApi
|
|
168
|
-
.getQueueData(queueType)
|
|
169
|
-
.items.map((item) => item.rules.name + (item.quantity > 1 ? "x" + item.quantity : "")) +
|
|
170
|
-
"]");
|
|
171
|
-
}, "");
|
|
172
|
-
const queueStates = this.queueStates
|
|
173
|
-
.filter((queueState) => queueState.items.length > 0)
|
|
174
|
-
.map((queueState) => {
|
|
175
|
-
const queueString = queueState.items
|
|
176
|
-
.map((item) => item.unit.name + "(" + Math.round(item.priority * 10) / 10 + ")")
|
|
177
|
-
.join(", ");
|
|
178
|
-
return `${queueTypeToName(queueState.queue)} Prios: ${queueString}\n`;
|
|
179
|
-
})
|
|
180
|
-
.join("");
|
|
181
|
-
return `Production: ${productionState}\n${queueStates}`;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
1
|
+
import { QueueStatus, QueueType, } from "@chronodivide/game-api";
|
|
2
|
+
import { BUILDING_NAME_TO_RULES, DEFAULT_BUILDING_PRIORITY, getDefaultPlacementLocation, } from "./buildingRules.js";
|
|
3
|
+
export const QUEUES = [
|
|
4
|
+
QueueType.Structures,
|
|
5
|
+
QueueType.Armory,
|
|
6
|
+
QueueType.Infantry,
|
|
7
|
+
QueueType.Vehicles,
|
|
8
|
+
QueueType.Aircrafts,
|
|
9
|
+
QueueType.Ships,
|
|
10
|
+
];
|
|
11
|
+
export const queueTypeToName = (queue) => {
|
|
12
|
+
switch (queue) {
|
|
13
|
+
case QueueType.Structures:
|
|
14
|
+
return "Structures";
|
|
15
|
+
case QueueType.Armory:
|
|
16
|
+
return "Armory";
|
|
17
|
+
case QueueType.Infantry:
|
|
18
|
+
return "Infantry";
|
|
19
|
+
case QueueType.Vehicles:
|
|
20
|
+
return "Vehicles";
|
|
21
|
+
case QueueType.Aircrafts:
|
|
22
|
+
return "Aircrafts";
|
|
23
|
+
case QueueType.Ships:
|
|
24
|
+
return "Ships";
|
|
25
|
+
default:
|
|
26
|
+
return "Unknown";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
export class QueueController {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.queueStates = [];
|
|
32
|
+
}
|
|
33
|
+
onAiUpdate(game, productionApi, actionsApi, playerData, threatCache, unitTypeRequests, logger) {
|
|
34
|
+
this.queueStates = QUEUES.map((queueType) => {
|
|
35
|
+
const options = productionApi.getAvailableObjects(queueType);
|
|
36
|
+
const items = this.getPrioritiesForBuildingOptions(game, options, threatCache, playerData, unitTypeRequests, logger);
|
|
37
|
+
const topItem = items.length > 0 ? items[items.length - 1] : undefined;
|
|
38
|
+
return {
|
|
39
|
+
queue: queueType,
|
|
40
|
+
items,
|
|
41
|
+
// only if the top item has a priority above zero
|
|
42
|
+
topItem: topItem && topItem.priority > 0 ? topItem : undefined,
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
const totalWeightAcrossQueues = this.queueStates
|
|
46
|
+
.map((decision) => decision.topItem?.priority)
|
|
47
|
+
.reduce((pV, cV) => pV + cV, 0);
|
|
48
|
+
const totalCostAcrossQueues = this.queueStates
|
|
49
|
+
.map((decision) => decision.topItem?.unit.cost)
|
|
50
|
+
.reduce((pV, cV) => pV + cV, 0);
|
|
51
|
+
this.queueStates.forEach((decision) => {
|
|
52
|
+
this.updateBuildQueue(game, productionApi, actionsApi, playerData, threatCache, decision.queue, decision.topItem, totalWeightAcrossQueues, totalCostAcrossQueues, logger);
|
|
53
|
+
});
|
|
54
|
+
// Repair is simple - just repair everything that's damaged.
|
|
55
|
+
if (playerData.credits > 0) {
|
|
56
|
+
game.getVisibleUnits(playerData.name, "self", (r) => r.repairable).forEach((unitId) => {
|
|
57
|
+
const unit = game.getUnitData(unitId);
|
|
58
|
+
if (!unit || !unit.hitPoints || !unit.maxHitPoints || unit.hasWrenchRepair) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (unit.hitPoints < unit.maxHitPoints) {
|
|
62
|
+
actionsApi.toggleRepairWrench(unitId);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
updateBuildQueue(game, productionApi, actionsApi, playerData, threatCache, queueType, decision, totalWeightAcrossQueues, totalCostAcrossQueues, logger) {
|
|
68
|
+
const myCredits = playerData.credits;
|
|
69
|
+
const queueData = productionApi.getQueueData(queueType);
|
|
70
|
+
if (queueData.status == QueueStatus.Idle) {
|
|
71
|
+
// Start building the decided item.
|
|
72
|
+
if (decision !== undefined) {
|
|
73
|
+
logger(`Decision (${queueTypeToName(queueType)}): ${decision.unit.name}`);
|
|
74
|
+
actionsApi.queueForProduction(queueType, decision.unit.name, decision.unit.type, 1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (queueData.status == QueueStatus.Ready && queueData.items.length > 0) {
|
|
78
|
+
// Consider placing it.
|
|
79
|
+
const objectReady = queueData.items[0].rules;
|
|
80
|
+
if (queueType == QueueType.Structures || queueType == QueueType.Armory) {
|
|
81
|
+
let location = this.getBestLocationForStructure(game, playerData, objectReady);
|
|
82
|
+
if (location !== undefined) {
|
|
83
|
+
logger(`Completed: ${queueTypeToName(queueType)}: ${objectReady.name}, placing at ${location.rx},${location.ry}`);
|
|
84
|
+
actionsApi.placeBuilding(objectReady.name, location.rx, location.ry);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
logger(`Completed: ${queueTypeToName(queueType)}: ${objectReady.name} but nowhere to place it`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (queueData.status == QueueStatus.Active && queueData.items.length > 0 && decision != null) {
|
|
92
|
+
// Consider cancelling if something else is significantly higher priority than what is currently being produced.
|
|
93
|
+
const currentProduction = queueData.items[0].rules;
|
|
94
|
+
if (decision.unit != currentProduction) {
|
|
95
|
+
// Changing our mind.
|
|
96
|
+
let currentItemPriority = this.getPriorityForBuildingOption(currentProduction, game, playerData, threatCache);
|
|
97
|
+
let newItemPriority = decision.priority;
|
|
98
|
+
if (newItemPriority > currentItemPriority * 2) {
|
|
99
|
+
logger(`Dequeueing queue ${queueTypeToName(queueData.type)} unit ${currentProduction.name} because ${decision.unit.name} has 2x higher priority.`);
|
|
100
|
+
actionsApi.unqueueFromProduction(queueData.type, currentProduction.name, currentProduction.type, 1);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
// Not changing our mind, but maybe other queues are more important for now.
|
|
105
|
+
if (totalCostAcrossQueues > myCredits && decision.priority < totalWeightAcrossQueues * 0.25) {
|
|
106
|
+
logger(`Pausing queue ${queueTypeToName(queueData.type)} because weight is low (${decision.priority}/${totalWeightAcrossQueues})`);
|
|
107
|
+
actionsApi.pauseProduction(queueData.type);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (queueData.status == QueueStatus.OnHold) {
|
|
112
|
+
// Consider resuming queue if priority is high relative to other queues.
|
|
113
|
+
if (myCredits >= totalCostAcrossQueues) {
|
|
114
|
+
logger(`Resuming queue ${queueTypeToName(queueData.type)} because credits are high`);
|
|
115
|
+
actionsApi.resumeProduction(queueData.type);
|
|
116
|
+
}
|
|
117
|
+
else if (decision && decision.priority >= totalWeightAcrossQueues * 0.25) {
|
|
118
|
+
logger(`Resuming queue ${queueTypeToName(queueData.type)} because weight is high (${decision.priority}/${totalWeightAcrossQueues})`);
|
|
119
|
+
actionsApi.resumeProduction(queueData.type);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
getPrioritiesForBuildingOptions(game, options, threatCache, playerData, unitTypeRequests, logger) {
|
|
124
|
+
let priorityQueue = [];
|
|
125
|
+
options.forEach((option) => {
|
|
126
|
+
const calculatedPriority = this.getPriorityForBuildingOption(option, game, playerData, threatCache);
|
|
127
|
+
// Get the higher of the dynamic and the mission priority for the unit.
|
|
128
|
+
const actualPriority = Math.max(calculatedPriority, unitTypeRequests.get(option.name) ?? calculatedPriority);
|
|
129
|
+
if (actualPriority > 0) {
|
|
130
|
+
priorityQueue.push({ unit: option, priority: actualPriority });
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
priorityQueue = priorityQueue.sort((a, b) => a.priority - b.priority);
|
|
134
|
+
return priorityQueue;
|
|
135
|
+
}
|
|
136
|
+
getPriorityForBuildingOption(option, game, playerStatus, threatCache) {
|
|
137
|
+
if (BUILDING_NAME_TO_RULES.has(option.name)) {
|
|
138
|
+
let logic = BUILDING_NAME_TO_RULES.get(option.name);
|
|
139
|
+
return logic.getPriority(game, playerStatus, option, threatCache);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
// Fallback priority when there are no rules.
|
|
143
|
+
return (DEFAULT_BUILDING_PRIORITY - game.getVisibleUnits(playerStatus.name, "self", (r) => r == option).length);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
getBestLocationForStructure(game, playerData, objectReady) {
|
|
147
|
+
if (BUILDING_NAME_TO_RULES.has(objectReady.name)) {
|
|
148
|
+
let logic = BUILDING_NAME_TO_RULES.get(objectReady.name);
|
|
149
|
+
return logic.getPlacementLocation(game, playerData, objectReady);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
// fallback placement logic
|
|
153
|
+
return getDefaultPlacementLocation(game, playerData, playerData.startLocation, objectReady);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
getGlobalDebugText(gameApi, productionApi) {
|
|
157
|
+
const productionState = QUEUES.reduce((prev, queueType) => {
|
|
158
|
+
if (productionApi.getQueueData(queueType).size === 0) {
|
|
159
|
+
return prev;
|
|
160
|
+
}
|
|
161
|
+
const paused = productionApi.getQueueData(queueType).status === QueueStatus.OnHold;
|
|
162
|
+
return (prev +
|
|
163
|
+
" [" +
|
|
164
|
+
queueTypeToName(queueType) +
|
|
165
|
+
(paused ? " PAUSED" : "") +
|
|
166
|
+
": " +
|
|
167
|
+
productionApi
|
|
168
|
+
.getQueueData(queueType)
|
|
169
|
+
.items.map((item) => item.rules.name + (item.quantity > 1 ? "x" + item.quantity : "")) +
|
|
170
|
+
"]");
|
|
171
|
+
}, "");
|
|
172
|
+
const queueStates = this.queueStates
|
|
173
|
+
.filter((queueState) => queueState.items.length > 0)
|
|
174
|
+
.map((queueState) => {
|
|
175
|
+
const queueString = queueState.items
|
|
176
|
+
.map((item) => item.unit.name + "(" + Math.round(item.priority * 10) / 10 + ")")
|
|
177
|
+
.join(", ");
|
|
178
|
+
return `${queueTypeToName(queueState.queue)} Prios: ${queueString}\n`;
|
|
179
|
+
})
|
|
180
|
+
.join("");
|
|
181
|
+
return `Production: ${productionState}\n${queueStates}`;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
184
|
//# sourceMappingURL=queueController.js.map
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { GameMath } from "@chronodivide/game-api";
|
|
2
|
-
import { BasicBuilding } from "./basicBuilding.js";
|
|
3
|
-
import { getDefaultPlacementLocation } from "./buildingRules.js";
|
|
4
|
-
import { Vector2 } from "three";
|
|
5
|
-
export class ResourceCollectionBuilding extends BasicBuilding {
|
|
6
|
-
constructor(basePriority, maxNeeded, onlyBuildWhenFloatingCreditsAmount) {
|
|
7
|
-
super(basePriority, maxNeeded, onlyBuildWhenFloatingCreditsAmount);
|
|
8
|
-
}
|
|
9
|
-
getPlacementLocation(game, playerData, technoRules) {
|
|
10
|
-
// Prefer spawning close to ore.
|
|
11
|
-
let selectedLocation = playerData.startLocation;
|
|
12
|
-
var closeOre;
|
|
13
|
-
var closeOreDist;
|
|
14
|
-
let allTileResourceData = game.mapApi.getAllTilesResourceData();
|
|
15
|
-
for (let i = 0; i < allTileResourceData.length; ++i) {
|
|
16
|
-
let tileResourceData = allTileResourceData[i];
|
|
17
|
-
if (tileResourceData.spawnsOre) {
|
|
18
|
-
let dist = GameMath.sqrt((selectedLocation.x - tileResourceData.tile.rx) ** 2 +
|
|
19
|
-
(selectedLocation.y - tileResourceData.tile.ry) ** 2);
|
|
20
|
-
if (closeOreDist == undefined || dist < closeOreDist) {
|
|
21
|
-
closeOreDist = dist;
|
|
22
|
-
closeOre = tileResourceData.tile;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (closeOre) {
|
|
27
|
-
selectedLocation = new Vector2(closeOre.rx, closeOre.ry);
|
|
28
|
-
}
|
|
29
|
-
return getDefaultPlacementLocation(game, playerData, selectedLocation, technoRules);
|
|
30
|
-
}
|
|
31
|
-
// Don't build/start selling these if we don't have any harvesters
|
|
32
|
-
getMaxCount(game, playerData, technoRules, threatCache) {
|
|
33
|
-
const harvesters = game.getVisibleUnits(playerData.name, "self", (r) => r.harvester).length;
|
|
34
|
-
return Math.max(1, harvesters * 2);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
1
|
+
import { GameMath } from "@chronodivide/game-api";
|
|
2
|
+
import { BasicBuilding } from "./basicBuilding.js";
|
|
3
|
+
import { getDefaultPlacementLocation } from "./buildingRules.js";
|
|
4
|
+
import { Vector2 } from "three";
|
|
5
|
+
export class ResourceCollectionBuilding extends BasicBuilding {
|
|
6
|
+
constructor(basePriority, maxNeeded, onlyBuildWhenFloatingCreditsAmount) {
|
|
7
|
+
super(basePriority, maxNeeded, onlyBuildWhenFloatingCreditsAmount);
|
|
8
|
+
}
|
|
9
|
+
getPlacementLocation(game, playerData, technoRules) {
|
|
10
|
+
// Prefer spawning close to ore.
|
|
11
|
+
let selectedLocation = playerData.startLocation;
|
|
12
|
+
var closeOre;
|
|
13
|
+
var closeOreDist;
|
|
14
|
+
let allTileResourceData = game.mapApi.getAllTilesResourceData();
|
|
15
|
+
for (let i = 0; i < allTileResourceData.length; ++i) {
|
|
16
|
+
let tileResourceData = allTileResourceData[i];
|
|
17
|
+
if (tileResourceData.spawnsOre) {
|
|
18
|
+
let dist = GameMath.sqrt((selectedLocation.x - tileResourceData.tile.rx) ** 2 +
|
|
19
|
+
(selectedLocation.y - tileResourceData.tile.ry) ** 2);
|
|
20
|
+
if (closeOreDist == undefined || dist < closeOreDist) {
|
|
21
|
+
closeOreDist = dist;
|
|
22
|
+
closeOre = tileResourceData.tile;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (closeOre) {
|
|
27
|
+
selectedLocation = new Vector2(closeOre.rx, closeOre.ry);
|
|
28
|
+
}
|
|
29
|
+
return getDefaultPlacementLocation(game, playerData, selectedLocation, technoRules);
|
|
30
|
+
}
|
|
31
|
+
// Don't build/start selling these if we don't have any harvesters
|
|
32
|
+
getMaxCount(game, playerData, technoRules, threatCache) {
|
|
33
|
+
const harvesters = game.getVisibleUnits(playerData.name, "self", (r) => r.harvester).length;
|
|
34
|
+
return Math.max(1, harvesters * 2);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
37
|
//# sourceMappingURL=resourceCollectionBuilding.js.map
|