Gabriel Genois 9 months ago
parent
commit
e6d96de7f9
  1. 10
      bootstrap.js
  2. 173
      building.js
  3. 57
      cUtility.js
  4. 2
      creepBasic.js
  5. 83
      creepBuilder.js
  6. 131
      creepMiner.js
  7. 3
      creepMover.js
  8. 42
      defence.js
  9. 4
      main.js

10
bootstrap.js

@ -8,6 +8,16 @@ var bootstrap = {
rooms: []
}
Memory.engine.rooms = [];
if (Game.time % 20) {
for(var i in Memory.creeps) {
if(Game.creeps[i]) {
Game.creeps[i].memory.savedSpot = null;
Game.creeps[i].memory.sourceId = null;
}
}
}
for(const i in Game.spawns) {
let spawn = Game.spawns[i];
let iRoomIndex = Engine.m['cUtility'].getRoomIndexFromName(spawn.room.name);

173
building.js

@ -18,26 +18,38 @@ var building = {
buildingLimits : [
null,
null,
[
[ // lvl 2
{t: STRUCTURE_EXTENSION, n: 5}
],
[
[ // lvl 3
{t: STRUCTURE_EXTENSION, n: 10},
{t: STRUCTURE_TOWER, n: 1}
],
[
[ // lvl 4
{t: STRUCTURE_EXTENSION, n: 20},
{t: STRUCTURE_TOWER, n: 1},
{t: STRUCTURE_STORAGE, n: 1}
],
[
[ // lvl 5
{t: STRUCTURE_EXTENSION, n: 30},
{t: STRUCTURE_TOWER, n: 2},
{t: STRUCTURE_STORAGE, n: 1}
],
[],
[],
[],
[ // lvl 6
{t: STRUCTURE_EXTENSION, n: 40},
{t: STRUCTURE_TOWER, n: 2},
{t: STRUCTURE_STORAGE, n: 1}
],
[ // lvl 7
{t: STRUCTURE_EXTENSION, n: 50},
{t: STRUCTURE_TOWER, n: 3},
{t: STRUCTURE_STORAGE, n: 1}
],
[ // lvl8
{t: STRUCTURE_EXTENSION, n: 60},
{t: STRUCTURE_TOWER, n: 6},
{t: STRUCTURE_STORAGE, n: 1}
]
],
buildingTypes: [
@ -131,9 +143,11 @@ var building = {
*/
findExtentionPlacement: function (Engine, r, bi) {
findExtentionPlacement: function (Engine, r) {
let iRange = 2;
let MaxRange = 6;
let MaxRange = 13;
let oPos = null;
let iXMin;
@ -163,13 +177,17 @@ var building = {
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)
!building.isAroundMiningSpot(Engine, r, iX ,iY)
) {
if (++iStep > iInterval) {
iStep = 0;
} else {
if (building.lookAround(Engine, r, iX, iY).length == 9) {
if (
building.lookAround(Engine, r, iX, iY).length == 9 &&
!building.isOnBuilding(Engine, r, iX, iY)
) {
new RoomVisual(r.name).circle(
iX,
iY,
@ -179,6 +197,7 @@ var building = {
fill: "blue"
}
);
oPos = {
x: iX,
y: iY
@ -199,6 +218,69 @@ var building = {
},
findExtentionPlacementView: function (Engine, r, bi) {
let iRange = 2;
let MaxRange = 13;
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"
}
);
}
}
}
}
}
iX++;
}
iY++;
}
iRange++;
}
},
/*
0: Building needed
1: Location found
@ -213,7 +295,7 @@ var building = {
for (let b in r.rBatiments) {
// watcher
if (r.rBatiments[b].s > 0 && r.rBatiments[b].s < 3) {
if (r.rBatiments[b].s > 0) {
/*switch (r.rBatiments[b].t) {
case STRUCTURE_EXTENSION:
building.findExtentionPlacement(Engine, r, b);
@ -223,17 +305,21 @@ var building = {
r.rBatiments[b].x,
r.rBatiments[b].y
);
let bF = false;
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...
if (r.rBatiments[b].s == 2 && data[d].type == "structure") {
r.rBatiments[b].s = 3;
bF = true;
} else if (r.rBatiments[b].s == 2 && data[d].type == "constructionSite") {
// keep going...
bF = true;
}
if (!bF && r.rBatiments[b].s == 2) {
// something happen. rebuld.
r.rBatiments[b].s = 0;
}
}
break ;
}
}
@ -245,6 +331,8 @@ var building = {
let oPos = null;
switch (r.rBatiments[b].t) {
case STRUCTURE_EXTENSION:
case STRUCTURE_TOWER:
case STRUCTURE_STORAGE:
oPos = building.findExtentionPlacement(Engine, r, b);
break;
}
@ -287,16 +375,18 @@ var building = {
if (r.RoomLevel == undefined || r.RoomLevel == null) {
r.RoomLevel = 0;
}
if (energy >= 300 && r.RoomLevel == 0 && lvl >= 1) {
if (energy >= 1300 && lvl >= 4) {
r.RoomLevel = 4;
} else if (energy >= 800 && lvl >= 3) {
r.RoomLevel = 3;
} else if (energy >= 550 && lvl >= 2) {
r.RoomLevel = 2;
} else if (energy >= 300 && 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;
},
@ -348,11 +438,14 @@ var building = {
fill: "gray"
}
);
r.rRoadPath.push({
x: path[p].x,
y: path[p].y,
d: exits[e]
});
console.log(path[p].x + ":" + path[p].y + "|" + exits[e]);
if (path[p].x > 0 && path[p].x < 49 && path[p].y > 0 && path[p].y < 49) {
r.rRoadPath.push({
x: path[p].x,
y: path[p].y,
d: exits[e]
});
}
}
}
}
@ -438,11 +531,14 @@ var building = {
fill: "gray"
}
);
r.rRoadPath.push({
x: rPath[p].x,
y: rPath[p].y,
d: r.rSources[iS].id
});
}
}
building.planControllerRoad(Engine, r);
@ -498,7 +594,6 @@ var building = {
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(
@ -605,25 +700,23 @@ var building = {
building.planRoadPath(Engine, r);
building.displaySpots(Engine, r);
building.displayRoads(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.findExtentionPlacementView(Engine, r);
//building.displayBluildings(Engine, r);
//building.removeRoadExtrimities(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

57
cUtility.js

@ -91,7 +91,7 @@ var cUtility = {
}
}
if (!bFound) {
console.log("Warning; Seat not found. Maybe was deleted by the safety");
//console.log("Warning; Seat not found. Maybe was deleted by the safety");
}
c.memory.savedSpot = null;
c.memory.sourceId = null;
@ -99,29 +99,50 @@ var cUtility = {
findEnergy(Engine, c) {
if (cUtility.isAvailableSpot(Engine, c) && (c.memory.savedSpot == null || c.memory.savedSpot == undefined)) {
cUtility.getMeASpot(Engine, c);
} else {
// No spot available or already
}
let bDropt = false;
if (c.memory.savedSpot == null) {
if (c.memory.savedSpot != null) {
const targets = c.room.find(FIND_DROPPED_RESOURCES);
if(targets.length) {
for (let t in targets) {
if (targets[t].amount >= c.store.getFreeCapacity()){
bDropt = true;
if(c.pickup(targets[t]) == ERR_NOT_IN_RANGE) {
c.moveTo(targets[t]);
}
}
}
}}
if (!bDropt) {
if (cUtility.isAvailableSpot(Engine, c) && (c.memory.savedSpot == null || c.memory.savedSpot == undefined)) {
cUtility.getMeASpot(Engine, c);
} else {
// No spot available or already
}
}
//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]);
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 if(!bDropt) {
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) {

2
creepBasic.js

@ -12,7 +12,7 @@ var creepBasic = {
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY]
],
limits: [3,3,3,2,1,1,1,1],
limits: [3,3,3,3,2,2,1,1],
// inner function for the creep
fnc: function (Engine, c) {

83
creepBuilder.js

@ -3,16 +3,16 @@ var creepBuilder = {
role: 'builder',
body: [
[],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY], // 200
[WORK, WORK, WORK, MOVE, MOVE, MOVE, CARRY, CARRY], // 550
[WORK, WORK, WORK, MOVE, MOVE, MOVE, CARRY, CARRY],
[WORK, WORK, WORK, MOVE, MOVE, MOVE, CARRY, CARRY],
[WORK, WORK, WORK, MOVE, MOVE, MOVE, CARRY, CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY]
],
limits: [3,3,2,3,1,1,1,1],
limits: [0,3,3,3,4,1,1,1],
// inner function for the creep
fnc: function (Engine, c) {
@ -41,23 +41,66 @@ var creepBuilder = {
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);
// builder
const target = c.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if(target) {
if(c.build(target) == ERR_NOT_IN_RANGE) {
c.moveTo(target);
}
} else {
c.memory.mode = 2;
}
break ;
case 2:
// Build roads
oRoad = null;
let rIndex = Engine.m['cUtility'].getRoomIndexFromName(c.room.name);
let r = Memory.engine.rooms[rIndex];
let sbfound;
for (let ro in r.rRoadPath) {
let data = Game.spawns[r.rSpawn[0]].room.lookAt(
r.rRoadPath[ro].x,
r.rRoadPath[ro].y
);
sbfound = false;
if (data.length > 0) {
for (let d in data) {
if (data[d].type == 'structure') {
// There is allready a road here.
sbfound = true;
}
} else {
if(c.upgradeController(c.room.controller) == ERR_NOT_IN_RANGE) {
c.moveTo(c.room.controller);
}
}
break ;
}
if (!sbfound) {
Game.rooms[c.room.name].createConstructionSite(
r.rRoadPath[ro].x,
r.rRoadPath[ro].y,
STRUCTURE_ROAD
);
c.memory.mode = 1;
break ;
}
}
c.memory.mode = 3;
break ;
case 3:
// Build Walls
c.memory.mode = 4;
break ;
case 4:
// Build Ramparts
c.memory.mode = 5;
break ;
case 5:
if(c.upgradeController(c.room.controller) == ERR_NOT_IN_RANGE) {
c.moveTo(c.room.controller);
}
break ;
}
},
// creepHook for module

131
creepMiner.js

@ -0,0 +1,131 @@
var creepMiner = {
name: 'creepMiner',
role: 'miner',
body: [
[],
[], // 200
[], // 550
[WORK, WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE],
[WORK, WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE],
[WORK, WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE],
[WORK, WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE],
[WORK, WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE],
[WORK, WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE]
],
limits: [0,0,0,2,2,2,2,2],
workedSource: [],
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;
}
var rIndex = Engine.m['cUtility'].getRoomIndexFromName(c.room.name);
var r = Memory.engine.rooms[rIndex];
if (c.memory.sourceId != null && c.memory.sourceId != undefined) {
creepMiner.workedSource.push(c.memory.sourceId);
var oSource = Game.getObjectById(c.memory.sourceId);
}
switch (c.memory.mode) {
case 0: // Clean up. If I am here, a creep died.
c.memory.sourceId = null;
for (let iS in r.rSources) {
if (creepMiner.workedSource.indexOf(r.rSources[iS].id) < 0) {
c.memory.sourceId = r.rSources[iS].id;
}
}
if (c.memory.sourceId != null) {
c.memory.mode = 1;
} else {
// Waiting for an empty place.
}
break ;
case 1: // move to it.
const path = c.pos.findPathTo(oSource, {ignoreCreeps: true});
if(path.length > 1) {
c.move(path[0].direction);
} else {
c.memory.mode = 2;
}
break;
case 2: // arived.
for (let iS in r.rSources) {
if (r.rSources[iS].id == c.memory.sourceId) {
for (let iSpot in r.rSources[iS].rSpot) {
if (c.pos.x == r.rSources[iS].rSpot[iSpot].x && c.pos.y == r.rSources[iS].rSpot[iSpot].y) {
r.rSources[iS].rSpot[iSpot].takenBy = c.id;
r.rSources[iS].rSpot[iSpot].timeSince = c.ticksToLive + Game.time;
c.memory.mode = 3;
}
}
}
}
break;
case 3: // put a storage ?
let data = Game.rooms[c.room.name].lookAt(
c.pos.x,
c.pos.y
);
let bShouldBuild = false;
for (let iB in data) {
if (!(data[iB].type == "structure" || data[iB].type == "constructionSite")) {
bShouldBuild = true;
}
}
if (bShouldBuild) {
Game.rooms[c.room.name].createConstructionSite(
c.pos.x,
c.pos.y,
STRUCTURE_CONTAINER
);
}
c.memory.mode = 4;
break;
case 4: // Working
c.harvest(oSource);
break;
}
},
// creepHook for module
cHook : function (Engine, c) {
if (c.memory.role == creepMiner.role) {
creepMiner.fnc(Engine, c);
}
},
// RoomHook for module.
rHook : function (Engine, r) {
let iCount = Engine.m['cUtility'].getCount(
Engine,
r.name,
creepMiner.role
);
let iMax = creepMiner.limits[r.RoomLevel];
if (iCount < iMax) {
Game.spawns[r.rSpawn[0]].spawnCreep(
creepMiner.body[r.RoomLevel],
'miner' + iCount + '--' + Math.random().toString(36).substr(2, 9),
{
memory : {
role: creepMiner.role,
home: r.name,
module: creepMiner.name
}
}
);
}
}
};
module.exports = creepMiner;

3
creepMover.js

@ -0,0 +1,3 @@
var creepMover = {};
module.exports = creepMover;

42
defence.js

@ -0,0 +1,42 @@
var defence = {
maintenance (Engine, r) {
const targets = Game.rooms[r.name].find(FIND_STRUCTURES, {
filter: object => object.hits < object.hitsMax
});
targets.sort((a,b) => a.hits - b.hits);
var towers = Game.rooms[r.name].find(
FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER}});
if(targets.length > 0) {
towers.forEach(tower => tower.repair(targets[0]));
}
},
rHook : function (Engine, r) {
var hostiles = Game.rooms[r.name].find(FIND_HOSTILE_CREEPS);
if(hostiles.length > 0) {
var username = hostiles[0].owner.username;
//Game.notify(`User ${username} spotted in room ${roomName}`);
var towers = Game.rooms[r.name].find(
FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER}});
towers.forEach(tower => tower.attack(hostiles[0]));
} else {
defence.maintenance(Engine, r);
}
}
};
module.exports = defence;

4
main.js

@ -45,7 +45,9 @@ var Engine = {
'bootstrap', // Init tools
'creepBasic', // Creep > Basic
'building', // Buidling planner
'creepBuilder'
'creepBuilder', // Creep > Builder
'defence',
'creepMiner'
];
// Load each module

Loading…
Cancel
Save