-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
158 lines (132 loc) · 6.37 KB
/
system.js
File metadata and controls
158 lines (132 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
const debugSilly = require('debug')('sc2:silly:supplySystem');
const { createSystem } = require('@node-sc2/core');
const { Alliance } = require('@node-sc2/core/constants/enums');
const { frontOfGrid } = require('@node-sc2/core/utils/map/region');
const { SupplyUnitRace } = require('@node-sc2/core/constants/race-map');
const { distance, areEqual, avgPoints, nClosestPoint } = require('@node-sc2/core/utils/geometry/point');
function calculateSupplyGap(gameLoop, supply, bases) {
// increase supply gap as supply gets higher
const supplyMultiplier = supply / 40 < 1 ? 1 : supply / 40;
// increase supply gap as we expand
const baseMultiplier = bases.length >= 4 ? 4 : bases.length;
const gap = 4 * baseMultiplier * supplyMultiplier;
debugSilly('Calculated supply gap desired: ', Math.floor(gap));
return Math.floor(gap);
}
module.exports = createSystem({
name: 'SupplySystem',
type: 'agent',
/** @param {World} world */
findSupplyPositions({ agent, resources }) {
const { units, map, debug } = resources.get();
const [main, natural] = map.getExpansions();
const myExpansions = map.getOccupiedExpansions(Alliance.SELF);
const myPylons = units.getById(SupplyUnitRace[agent.race]);
// first pylon being placed
if (myPylons.length === 0) {
const mainMineralLine = main.areas.mineralLine;
const geysers = main.cluster.vespeneGeysers;
const mainBase = main.getBase();
const locations = main.areas.areaFill.filter((point) => {
return (
// with-in super pylon distance from main nexus
(distance(point, mainBase.pos) <= 6.5) &&
// far enough away to stay outta the mineral line
(mainMineralLine.every(mlp => distance(mlp, point) > 2)) &&
// far enough away from gas line
(geysers.every(gp => distance(gp.pos, point) > 3))
);
});
return locations;
}
if (myPylons.length === 1) {
// front of natural pylon for great justice
const naturalWall = map.getNatural().getWall();
let possiblePlacements = frontOfGrid({ resources }, map.getNatural().areas.areaFill)
.filter(point => naturalWall.every(wallCell => (
(distance(wallCell, point) <= 6.5) &&
(distance(wallCell, point) >= 3)
)));
if (possiblePlacements.length <= 0) {
possiblePlacements = frontOfGrid({ resources }, map.getNatural().areas.areaFill)
.map(point => {
point.coverage = naturalWall.filter(wallCell => (
(distance(wallCell, point) <= 6.5) &&
(distance(wallCell, point) >= 1)
)).length;
return point;
})
.sort((a, b) => b.coverage - a.coverage)
.filter((cell, i, arr) => cell.coverage === arr[0].coverage);
}
return possiblePlacements;
}
const needsSuperPylon = myExpansions.find((expansion) => {
const expansionBase = expansion.getBase();
return !myPylons.some(pylon => distance(expansionBase.pos, pylon.pos) < 6.5);
});
if (needsSuperPylon) {
const baseWhichNeedsSuperPylon = needsSuperPylon.getBase();
return needsSuperPylon.areas.placementGrid.filter((point) => {
return (
distance(point, baseWhichNeedsSuperPylon.pos) < 6.5 &&
distance(point, baseWhichNeedsSuperPylon.pos) > 3.5
);
})
}
// behind mineral line pylons, for canons and things, every base should have one if it can fit
const needsBmlPylon = myExpansions.find((expansion) => {
return (
// no existing bml pylon
!myPylons.some((pylon) => {
return expansion.areas.behindMineralLine.some(point => areEqual(pylon.pos, point));
}) &&
// label prevents getting stuck retrying where it doesn't fit
!expansion.labels.has('attemptedBML')
);
});
if (needsBmlPylon) {
needsBmlPylon.labels.set('attemptedBML', true);
const bml = needsBmlPylon.areas.behindMineralLine;
const bmlCentroid = avgPoints(bml);
return bml.filter(point => distance(point, bmlCentroid) < 5);
} else {
// otherwise just return all points in main and nat
return [ ...main.areas.placementGrid, ...natural.areas.placementGrid];
}
},
async onStep({ agent, data, resources }, gameLoop) {
const { units, actions, map, debug } = resources.get();
const supplyUnitId = SupplyUnitRace[agent.race];
const bases = units.getBases(Alliance.SELF);
const buildAbilityId = data.getUnitTypeData(supplyUnitId).abilityId;
const { foodUsed: supply, foodCap } = agent;
// current supplyCap includes pylons currently building and existing orders given to build them
const supplyCap = (
foodCap +
(units.inProgress(supplyUnitId).length * 8) +
(units.withCurrentOrders(buildAbilityId).length * 8)
);
if (supplyCap >= 200) return;
const conditions = [
supplyCap - supply < calculateSupplyGap(gameLoop, supply, bases), // need more supply gap
agent.canAfford(supplyUnitId), // can afford to build a pylon
units.withCurrentOrders(buildAbilityId).length <= 0
];
if (conditions.every(c => c)) {
const positions = this.findSupplyPositions({ agent, data, resources });
// pick 10 random positions from the list
const randomPositions = positions
.map(pos => ({ pos, rand: Math.random() }))
.sort((a, b) => a.rand - b.rand)
.map(a => a.pos)
.slice(0, 20);
// see if any of them are good
const foundPosition = await actions.canPlace(supplyUnitId, randomPositions);
if (foundPosition) {
const res = await actions.build(supplyUnitId, foundPosition);
}
}
}
});