Browse Source

v.01

master
Gabriel Genois 9 months ago
parent
commit
de9d5f5597
  1. 30
      bootstrap.js
  2. 656
      building.js
  3. 150
      cUtility.js
  4. 126
      creepBasic.js
  5. 93
      creepBuilder.js
  6. 79
      main.js

30
bootstrap.js

@ -0,0 +1,30 @@
var bootstrap = {
name: 'bootstrap',
// Build the backbone memory
refreshMemory : function(Engine) {
console.log("Refreshing memory..");
Memory.engine = null;
Memory.engine = {
rooms: []
}
Memory.engine.rooms = [];
for(const i in Game.spawns) {
let spawn = Game.spawns[i];
let iRoomIndex = Engine.m['cUtility'].getRoomIndexFromName(spawn.room.name);
if (iRoomIndex < 0) {
Memory.engine.rooms.push({
name: spawn.room.name,
rSpawn: [],
rMap: []
});
iRoomIndex = Memory.engine.rooms.length - 1;
}
Memory.engine.rooms[iRoomIndex].rSpawn.push(spawn.name)
}
Engine.refresh = true;
}
}
module.exports = bootstrap;

656
building.js

@ -0,0 +1,656 @@
var building = {
name: 'building',
/*
Since I'm trying to get an "organic" buidling engine, each buidling is
referenced here.
It shoud be an array or array, by room.
When we want a new building, we add an
item into the room memory. Then a script looking at this
memory will put the contruction site if needed.
Then if a construction site is detected, a builder spawn.
*/
buildingLimits : [
null,
null,
[
{t: STRUCTURE_EXTENSION, n: 5}
],
[
{t: STRUCTURE_EXTENSION, n: 10},
{t: STRUCTURE_TOWER, n: 1}
],
[
{t: STRUCTURE_EXTENSION, n: 20},
{t: STRUCTURE_TOWER, n: 1},
{t: STRUCTURE_STORAGE, n: 1}
],
[
{t: STRUCTURE_EXTENSION, n: 30},
{t: STRUCTURE_TOWER, n: 2},
{t: STRUCTURE_STORAGE, n: 1}
],
[],
[],
[],
],
buildingTypes: [
STRUCTURE_EXTENSION,
STRUCTURE_TOWER,
STRUCTURE_STORAGE
],
refrechBuildingList: function (Engine, r) {
if (r.rBatiments == null || r.rBatiments == undefined){
r.rBatiments = [];
}
if (r.RoomLevel < Game.rooms[r.name].controller.level)
{
let rRef = [];
// Set a counter for each type of building
for (let i in building.buildingTypes) {
rRef[building.buildingTypes[i]] = 0;
}
// Set the right amount of suported buidling by room level'ish.
for (let l in building.buildingLimits[r.RoomLevel + 1]) {
rRef[building.buildingLimits[r.RoomLevel + 1][l].t] = building.buildingLimits[r.RoomLevel + 1][l].n;
}
// Remove already put buidling.
if (r.rBatiments.length > 0) {
for (let b in r.rBatiments) {
rRef[r.rBatiments[b].t]--;
}
}
// Populaire batiment memory with missing items.
for (let type in rRef) {
while (rRef[type] > 0) {
r.rBatiments.push({
t: type,
s: 0, // 0: Init. 1: Place found, 2: Contruction site, 3: Built.
x: 0,
y: 0
});
rRef[type]--;
}
}
}
},
isCrossingRoad: function (Engine, r, x, y) {
if (r.rRoadPath != undefined) {
for (let rO in r.rRoadPath) {
if (r.rRoadPath[rO].x == x && r.rRoadPath[rO].y == y) {
return true;
}
}
}
return false;
},
isAroundMiningSpot: function (Engine, r, x, y) {
for (iR in r.rSources) {
for (iS in r.rSources[iR].rSpot) {
let rMSpot = building.lookAround(
Engine,
r,
r.rSources[iR].rSpot[iS].x,
r.rSources[iR].rSpot[iS].y
);
for (let inner in rMSpot) {
if (rMSpot[inner].x == x && rMSpot[inner].y == y) {
return true;
}
}
}
}
return false;
},
isOnBuilding: function (Engine, r, x, y) {
for (let b in r.rBatiments) {
if (r.rBatiments[b].s > 0 && r.rBatiments[b].x == x && r.rBatiments[b].y == y) {
return true;
}
}
return false;
},
/*
1- Must be reacheable (Path finding)
2- Should be in groups
3- Should be close to the spawn and storage.
*/
findExtentionPlacement: function (Engine, r, bi) {
let iRange = 2;
let MaxRange = 6;
let oPos = null;
let iXMin;
let iXMax;
let iYMin;
let iYMax;
let iX;
let iY;
let iSX = Game.spawns[r.rSpawn[0]].pos.x;
let iSY = Game.spawns[r.rSpawn[0]].pos.y;
let iStep = 0;
let iInterval = 2;
while (iRange < MaxRange && oPos == null) {
iYMin = iSY - iRange;
iYMax = iSY + iRange;
iXMin = iSX - iRange;
iXMax = iSX + iRange;
iY = iYMin;
while (iY >= iYMin && iY <= iYMax && oPos == null) {
iX = iXMin;
while (iX >= iXMin && iX <= iXMax && oPos == null) {
if (iX >= iSX - (iRange - 1) && iX <= iSX + (iRange - 1) && iY >= iSY - (iRange - 1) && iY <= iSY + (iRange - 1)) {
// inner range
} else {
if (building.getAt(r.rMap, iX, iY) != 1) {
if (
!building.isCrossingRoad(Engine, r, iX ,iY) &&
!building.isAroundMiningSpot(Engine, r, iX ,iY) &&
!building.isOnBuilding(Engine, r, iX, iY)
) {
if (++iStep > iInterval) {
iStep = 0;
} else {
if (building.lookAround(Engine, r, iX, iY).length == 9) {
new RoomVisual(r.name).circle(
iX,
iY,
{
fill: 'transparent',
radius: 0.5,
fill: "blue"
}
);
oPos = {
x: iX,
y: iY
};
}
}
}
}
}
iX++;
}
iY++;
}
iRange++;
}
return oPos;
},
/*
0: Building needed
1: Location found
2: Construction site placed
3: Buidling built
*/
findBuildSpace: function(Engine, r) {
//r.rBatiments = [];
building.refrechBuildingList(Engine, r);
for (let b in r.rBatiments) {
// watcher
if (r.rBatiments[b].s > 0 && r.rBatiments[b].s < 3) {
/*switch (r.rBatiments[b].t) {
case STRUCTURE_EXTENSION:
building.findExtentionPlacement(Engine, r, b);
break;
}*/
let data = Game.spawns[r.rSpawn[0]].room.lookAt(
r.rBatiments[b].x,
r.rBatiments[b].y
);
for (let d in data) {
if (data[d].type == "constructionSite") {
// is still a construction site...
}
if (data[d].type == "structure") {
// is still a construction site...
r.rBatiments[b].s = 3;
}
}
break ;
}
}
for (let b in r.rBatiments) {
if (r.rBatiments[b].s == 0) {
let oPos = null;
switch (r.rBatiments[b].t) {
case STRUCTURE_EXTENSION:
oPos = building.findExtentionPlacement(Engine, r, b);
break;
}
if (oPos != null) {
r.rBatiments[b].x = oPos.x;
r.rBatiments[b].y = oPos.y;
r.rBatiments[b].s = 1;
}
break ;
}
}
let count = 0;
for (let b in r.rBatiments) {
if (r.rBatiments[b].s == 2) {
count++;
}
}
if (count == 0)
for (let b in r.rBatiments) {
if (r.rBatiments[b].s == 1) {
r.rBatiments[b].s = 2;
Game.rooms[r.name].createConstructionSite(
r.rBatiments[b].x,
r.rBatiments[b].y,
r.rBatiments[b].t
);
break ;
}
}
},
getRoomControlLevelByEnergy: function(Engine, r) {
let energy = Game.rooms[r.name].energyCapacityAvailable;
let lvl = Game.rooms[r.name].controller.level;
if (r.RoomLevel == undefined || r.RoomLevel == null) {
r.RoomLevel = 0;
}
if (energy >= 300 && r.RoomLevel == 0 && lvl >= 1) {
// Buidling lvl 1 items.
r.RoomLevel = 1;
} else if (energy >= 550 && r.RoomLevel == 1 && lvl >= 2) {
r.RoomLevel = 2;
} else if (energy >= 800 && r.RoomLevel == 2 && lvl >= 3) {
r.RoomLevel = 3;
} else if (energy >= 1300 && r.RoomLevel == 3 && lvl >= 4) {
r.RoomLevel = 4;
}
return r.RoomLevel;
},
// Return type of terrain from a buffer for x,y
getAt : function(buffer, x,y) {
let pos = (y * 50) + x;
return buffer[pos];
},
// Get rawbuffer data of asked room
mapRoom: function (Engine, r) {
const roomTerrain = Game.map.getRoomTerrain(r.name);
r.rMap = roomTerrain.getRawBuffer();
},
planExitRoads: function (Engine, r) {
if (r.rRoadPath == undefined) {
return ;
}
let exits = Game.map.describeExits(r.name);
for (let e in exits) {
const exitDir = Game.map.findExit(r.name, exits[e]);
const sExit = Game.spawns[r.rSpawn[0]].room.find(exitDir);
let path = null;
for (let rp in r.rRoadPath) {
let origin = new RoomPosition(
r.rRoadPath[rp].x,
r.rRoadPath[rp].y,
r.name
);
let oPath = PathFinder.search(origin, sExit);
if (path == null || oPath.path.length < path.length) {
path = oPath.path;
}
}
if (path != null) {
for (let p in path) {
new RoomVisual(r.name).circle(
path[p].x,
path[p].y,
{
fill: 'transparent',
radius: 0.5,
fill: "gray"
}
);
r.rRoadPath.push({
x: path[p].x,
y: path[p].y,
d: exits[e]
});
}
}
}
},
planControllerRoad: function(Engine, r) {
if (r.rRoadPath == undefined) {
return ;
}
let dest = new RoomPosition(
Game.rooms[r.name].controller.pos.x,
Game.rooms[r.name].controller.pos.y,
r.name
);
let path = null;
for (let rp in r.rRoadPath) {
let origin = new RoomPosition(
r.rRoadPath[rp].x,
r.rRoadPath[rp].y,
r.name
);
let oPath = PathFinder.search(origin, dest);
if (path == null || oPath.path.length < path.length) {
path = oPath.path;
}
}
if (path != null) {
path.pop();
path.pop();
for (let p in path) {
new RoomVisual(r.name).circle(
path[p].x,
path[p].y,
{
fill: 'transparent',
radius: 0.5,
fill: "gray"
}
);
r.rRoadPath.push({
x: path[p].x,
y: path[p].y,
d: 'c'
});
}
}
building.planExitRoads(Engine, r);
},
// Plan link betwen ressources of the room.
planRoadPath: function(Engine, r) {
if (r.rSources == undefined || (r.rRoadPath != undefined && r.rRoadPath != null)) {
return ;
}
let SpawnPos = Game.spawns[r.rSpawn[0]].pos;
r.rRoadPath = [];
for (let iS in r.rSources) {
let rPath = null;
for (let iSp in r.rSources[iS].rSpot) {
let DestPos = new RoomPosition(
r.rSources[iS].rSpot[iSp].x,
r.rSources[iS].rSpot[iSp].y,
r.name
);
let oPath = PathFinder.search(SpawnPos, DestPos);
if ((rPath == null || oPath.path.length < rPath.length) && !oPath.incomplete) {
rPath = oPath.path;
}
}
rPath.pop();
for (let p in rPath) {
new RoomVisual(r.name).circle(
rPath[p].x,
rPath[p].y,
{
fill: 'transparent',
radius: 0.5,
fill: "gray"
}
);
r.rRoadPath.push({
x: rPath[p].x,
y: rPath[p].y,
d: r.rSources[iS].id
});
}
}
building.planControllerRoad(Engine, r);
},
/*
. . . . . .
. x x * * .
. . x # * .
. . x x x .
. . . . . .
*/
lookAround : function (Engine, r, x, y) {
let iXMin = x - 1;
let iXMax = x + 1;
let iYMin = y - 1;
let iYMax = y + 1;
let rReturn = [];
let iY = iYMin;
while (iY >= iYMin && iY <= iYMax) {
let iX = iXMin;
while (iX >= iXMin && iX <= iXMax) {
if (r.rMap[(iY * 50) + iX] == 0 || r.rMap[(iY * 50) + iX] == 2) {
rReturn.push({
x: iX,
y: iY
});
}
iX++;
}
iY++;
}
return rReturn;
},
autoCleanSpot: function(Engine, r) {
let iMaxTime = Game.time - 100;
for (let iS in r.rSources) {
for (let iSp in r.rSources[iS].rSpot) {
if (r.rSources[iS].rSpot[iSp].timeSince < iMaxTime) {
r.rSources[iS].rSpot[iSp].timeSince = null;
r.rSources[iS].rSpot[iSp].takenBy = null;
}
}
}
},
// Get room sources
getRoomSources: function (Engine, r) {
r.rSources = [];
let rSource = Game.spawns[r.rSpawn[0]].room.find(FIND_SOURCES);
console.log(rSource);
for (let s in rSource) {
let oSource = Game.getObjectById(rSource[s].id);
let rAvailableSpot = building.lookAround(
Engine,
r,
oSource.pos.x,
oSource.pos.y
);
let rResSpot = [];
for (ras in rAvailableSpot) {
let oSpot = {
x: rAvailableSpot[ras].x,
y: rAvailableSpot[ras].y,
takenBy: null,
timeSince: null
};
rResSpot.push(oSpot);
}
let sourceModel = {
id: oSource.id,
pos: {
x: oSource.pos.x,
y: oSource.pos.y
},
rSpot: rResSpot
};
r.rSources.push(sourceModel);
}
},
displaySpots : function(Engine, r) {
if (r.rSources != undefined) {
for (let rs in r.rSources) {
for (let s in r.rSources[rs].rSpot) {
let color = "green";
if (r.rSources[rs].rSpot[s].takenBy != null) {
color = "blue";
}
new RoomVisual(r.name).circle(
r.rSources[rs].rSpot[s].x,
r.rSources[rs].rSpot[s].y,
{
fill: 'transparent',
radius: 0.5,
fill: color
}
);
}
}
}
},
displayRoads: function (Engine, r) {
if (r.rRoadPath != undefined) {
for (let rO in r.rRoadPath) {
new RoomVisual(r.name).circle(
r.rRoadPath[rO].x,
r.rRoadPath[rO].y,
{
fill: 'transparent',
radius: 0.5,
fill: "gray"
}
);
}
}
},
displayBluildings: function (Engine, r) {
if (r.rBatiments != undefined) {
for (let rO in r.rBatiments) {
if (r.rBatiments[rO].s > 0) {
new RoomVisual(r.name).circle(
r.rBatiments[rO].x,
r.rBatiments[rO].y,
{
fill: 'transparent',
radius: 0.5,
fill: "yellow"
}
);
}
}
}
},
// Entry Hook. Run for each owned room.
rHook : function (Engine, r) {
if (r.rMap.length == 0)
{
building.mapRoom(Engine, r);
return ;
}
if (r.rSources == undefined) {
building.getRoomSources(Engine, r);
}
//r.rRoadPath = null;
building.planRoadPath(Engine, r);
building.displaySpots(Engine, r);
building.displayRoads(Engine, r);
//if (Game.time % 10) {
building.autoCleanSpot(Engine, r);
//}
building.getRoomControlLevelByEnergy(Engine, r);
building.findBuildSpace(Engine, r);
//building.displayBluildings(Engine, r);
/*
1- Get the raw data to be able to analyze the map.
2- Evaluate the best path to access ressources and set road for them
3- Build around this road.
A creep must have an easy way to
Storage ==> capacitor
==> spawn
==> Labs
==> Towers
Mineral ==> Storage
Lab ==> Storage
Donc, un point de départ:
>>> Une room a nécessairement 2 sources énergitiques
Donc, après que le path engine ait fait des liens entre les éléments,
On cherche place le storage sur le chemin.
Cela nous donne le point central de la nouvelle installation
Puis ont cré le reste de la base de .
new RoomVisual(oBase.sName).circle(ifX,ifY, {fill: 'transparent', radius: 0.5, fill: '#5b9a50'});
*/
}
};
module.exports = building;

150
cUtility.js

@ -0,0 +1,150 @@
var cUtility = {
name: 'cUtility',
getRoomIndexFromName : function(name) {
let i = -1;
while (++i < Memory.engine.rooms.length)
{
if (Memory.engine.rooms[i].name == name) {
return i;
}
}
return -1;
},
isAvailableSpot: function(Engine, c) {
let rIndex = cUtility.getRoomIndexFromName(c.room.name);
let r = Memory.engine.rooms[rIndex];
for (iR in r.rSources) {
for (iS in r.rSources[iR].rSpot) {
if (r.rSources[iR].rSpot[iS].takenBy == null) {
return true;
}
}
}
return false;
},
getMeASpot: function(Engine, c) {
let rIndex = cUtility.getRoomIndexFromName(c.room.name);
let r = Memory.engine.rooms[rIndex];
let path = null;
let iRs = null;
let iSs = null;
if (c.memory.savedSpot = undefined || c.memory.savedSpot == null) {
for (iR in r.rSources) {
let oSource = Game.getObjectById(r.rSources[iR].id);
let iAllReadyReserved = 0;
for (iS in r.rSources[iR].rSpot) {
if (r.rSources[iR].rSpot[iS].takenBy != null) {
let creep = Game.getObjectById(r.rSources[iR].rSpot[iS].takenBy);
iAllReadyReserved = iAllReadyReserved + creep.store.getFreeCapacity();
}
}
if ((oSource.energy - iAllReadyReserved) >= c.store.getFreeCapacity()) {
for (iS in r.rSources[iR].rSpot) {
if (r.rSources[iR].rSpot[iS].takenBy == null) {
let DestPos = new RoomPosition(
r.rSources[iR].rSpot[iS].x,
r.rSources[iR].rSpot[iS].y,
r.name
);
let oPath = PathFinder.search(c.pos, DestPos);
if (path == null || oPath.path.length < path.length) {
path = oPath.path;
iRs = iR;
iSs = iS;
}
}
}
}
}
}
if (path != null) {
c.memory.savedSpot = r.rSources[iRs].rSpot[iSs];
c.memory.sourceId = r.rSources[iRs].id;
r.rSources[iRs].rSpot[iSs].takenBy = c.id;
r.rSources[iRs].rSpot[iSs].timeSince = Game.time;
}
},
clearMySpot: function(Engine, c) {
let rIndex = cUtility.getRoomIndexFromName(c.room.name);
let r = Memory.engine.rooms[rIndex];
let bFound = false;
for (iR in r.rSources) {
for (iS in r.rSources[iR].rSpot) {
if (r.rSources[iR].rSpot[iS].takenBy == c.id) {
bFound = true;
r.rSources[iR].rSpot[iS].takenBy = null;
r.rSources[iR].rSpot[iS].timeSince = null;
}
}
}
if (!bFound) {
console.log("Warning; Seat not found. Maybe was deleted by the safety");
}
c.memory.savedSpot = null;
c.memory.sourceId = null;
},
findEnergy(Engine, c) {
if (cUtility.isAvailableSpot && (c.memory.savedSpot == null || c.memory.savedSpot == undefined)) {
cUtility.getMeASpot(Engine, c);
} else {
// No spot available or already
}
if (c.memory.savedSpot != null) {
//console.log("we are using a spot!");
let source = Game.getObjectById(c.memory.sourceId);
//console.log(source);
c.harvest(source);
c.moveTo(c.memory.savedSpot.x, c.memory.savedSpot.y);
} else {
var sources = c.room.find(FIND_SOURCES);
if(c.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
c.moveTo(sources[0]);
}
}
},
addCount : function (Engine, room, role) {
//console.log("+1 to " + room + "for unit " + role);
if (cUtility.rLimits[room] == undefined) {
cUtility.rLimits[room] = [];
}
cUtility.rLimits[room].push(role)
},
getCount: function (Engine, room, role) {
if (cUtility.rLimits[room] == undefined) {
console.log("no index for " + room);
return 0;
}
let iCount = 0;
let i = -1;
while (++i < cUtility.rLimits[room].length) {
if (cUtility.rLimits[room][i] == role) {
iCount++;
}
}
return iCount;
},
rLimits: []
};
module.exports = cUtility;

126
creepBasic.js

@ -0,0 +1,126 @@
var creepBasic = {
name: 'creepBasic',
role: 'basic',
body: [
[],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY]
],
limits: [3,3,3,2,1,1,1,1],
// inner function for the creep
fnc: function (Engine, c) {
Engine.m['cUtility'].addCount(
Engine,
c.memory.home,
c.memory.role
);
if (c.memory.mode == null || c.memory.mode == undefined) {
c.memory.mode = 0;
}
if(c.store[RESOURCE_ENERGY] == 0) {
c.memory.mode = 0;
}
if(c.store.getFreeCapacity(RESOURCE_ENERGY) == 0 && c.memory.mode == 0) {
Engine.m['cUtility'].clearMySpot(Engine, c);
c.memory.mode = 1;
}
let oTarget = null;
switch (c.memory.mode) {
case 0:
Engine.m['cUtility'].findEnergy(Engine, c);
break ;
case 1:
oTarget = c.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (
structure.structureType == STRUCTURE_TOWER &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > c.store.getCapacity()
);
}
});
if (oTarget) {
oAction = c.transfer(oTarget, RESOURCE_ENERGY);
if (oAction == ERR_NOT_IN_RANGE) {
c.moveTo(oTarget);
}
} else {
c.memory.mode = 2;
}
break ;
case 2:
oTarget = c.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (
(
structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION
) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
);
}
});
if (oTarget) {
oAction = c.transfer(oTarget, RESOURCE_ENERGY);
if (oAction == ERR_NOT_IN_RANGE) {
c.moveTo(oTarget);
}
} else {
c.memory.mode = 3;
}
break ;
case 3:
if(c.upgradeController(c.room.controller) == ERR_NOT_IN_RANGE) {
c.moveTo(c.room.controller);
}
break ;
}
},
// creepHook for module
cHook : function (Engine, c) {
if (c.memory.role == creepBasic.role) {
creepBasic.fnc(Engine, c);
}
},
// RoomHook for module.
rHook : function (Engine, r) {
let iCount = Engine.m['cUtility'].getCount(
Engine,
r.name,
creepBasic.role
);
let iMax = creepBasic.limits[r.RoomLevel];
if (iCount < iMax) {
// should build
Game.spawns[r.rSpawn[0]].spawnCreep(
creepBasic.body[r.RoomLevel],
'Worker_' + iCount + '--' + Math.random().toString(36).substr(2, 9),
{
memory : {
role: creepBasic.role,
home: r.name,
module: creepBasic.name
}
}
);
}
}
};
module.exports = creepBasic;

93
creepBuilder.js

@ -0,0 +1,93 @@
var creepBuilder = {
name: 'creepBuilder',
role: 'builder',
body: [
[],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY]
],
limits: [3,3,2,3,1,1,1,1],
// inner function for the creep
fnc: function (Engine, c) {
Engine.m['cUtility'].addCount(
Engine,
c.memory.home,
c.memory.role
);
if (c.memory.mode == null || c.memory.mode == undefined) {
c.memory.mode = 0;
}
if(c.store[RESOURCE_ENERGY] == 0) {
c.memory.mode = 0;
}
if(c.store.getFreeCapacity(RESOURCE_ENERGY) == 0 && c.memory.mode == 0) {
Engine.m['cUtility'].clearMySpot(Engine, c);
c.memory.mode = 1;
}
switch (c.memory.mode) {
case 0:
Engine.m['cUtility'].findEnergy(Engine, c);
break ;
case 1:
// builder
const target = c.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if(target) {
if(c.build(target) == ERR_NOT_IN_RANGE) {
c.moveTo(target);
}
} else {
if(c.upgradeController(c.room.controller) == ERR_NOT_IN_RANGE) {
c.moveTo(c.room.controller);
}
}
break ;
}
},
// creepHook for module
cHook : function (Engine, c) {
if (c.memory.role == creepBuilder.role) {
creepBuilder.fnc(Engine, c);
}
},
// RoomHook for module.
rHook : function (Engine, r) {
let iCount = Engine.m['cUtility'].getCount(
Engine,
r.name,
creepBuilder.role
);
let iMax = creepBuilder.limits[r.RoomLevel];
if (iCount < iMax) {
// should build
Game.spawns[r.rSpawn[0]].spawnCreep(
creepBuilder.body[r.RoomLevel],
'Builder' + iCount + '--' + Math.random().toString(36).substr(2, 9),
{
memory : {
role: creepBuilder.role,
home: r.name,
module: creepBuilder.name
}
}
);
}
}
};
module.exports = creepBuilder;

79
main.js

@ -0,0 +1,79 @@
var Engine = {
// module Array
m : Array,
// Run cHook for each module
cHookRuntime : function (Engine) {
for (const i in Game.creeps) {
let creep = Game.creeps[i];
for (const m in Engine.m) {
if (Engine.m[m].cHook != undefined) {
let fnc = Engine.m[m].cHook;
try {
fnc(Engine, creep);
} catch (e) {
console.log("chook| " + m + " | " + e);
}
}
}
}
},
// Run rHook for each module
rHoohRunetime : function (Engine) {
for (const r in Memory.engine.rooms) {
for (const m in Engine.m) {
if (Engine.m[m].rHook != undefined) {
try {
let fnc = Engine.m[m].rHook;
fnc(Engine, Memory.engine.rooms[r]);
} catch (e) {
console.log("rHook| " + m + " | " + e);
}
}
}
}
},
// Init - Entry point
init : function() {
// Module array
const rM = [
'cUtility', // Generic Creep Tools
'bootstrap', // Init tools
'creepBasic', // Creep > Basic
'building', // Buidling planner
'creepBuilder'
];
// Load each module
for (i in rM) {
Engine.m[rM[i]] = require(rM[i]);
}
// Init memory base if needed.
if (Memory.engine == null) {
Engine.m['bootstrap'].refreshMemory(Engine);
return ;
}
// Run cHook
Engine.cHookRuntime(Engine);
// Run rHook
Engine.rHoohRunetime(Engine);
// Remove dead creep memory
if (Game.time % 20) {
for(var i in Memory.creeps) {
if(!Game.creeps[i]) {
delete Memory.creeps[i];
}
}
}
}
};
Engine.init();
Loading…
Cancel
Save