Gabriel Genois 9 months ago
parent
commit
2ac0f644d5
  1. 478
      building.js
  2. 11
      cUtility.js
  3. 2
      creepBasic.js
  4. 56
      creepBuilder.js
  5. 203
      creepMover.js
  6. 4
      defence.js
  7. 3
      main.js

478
building.js

@ -38,24 +38,28 @@ var building = {
[ // lvl 6
{t: STRUCTURE_EXTENSION, n: 40},
{t: STRUCTURE_TOWER, n: 2},
{t: STRUCTURE_STORAGE, n: 1}
{t: STRUCTURE_STORAGE, n: 1},
{t: STRUCTURE_TERMINAL, n: 1}
],
[ // lvl 7
{t: STRUCTURE_EXTENSION, n: 50},
{t: STRUCTURE_TOWER, n: 3},
{t: STRUCTURE_STORAGE, n: 1}
{t: STRUCTURE_STORAGE, n: 1},
{t: STRUCTURE_TERMINAL, n: 1}
],
[ // lvl8
{t: STRUCTURE_EXTENSION, n: 60},
{t: STRUCTURE_TOWER, n: 6},
{t: STRUCTURE_STORAGE, n: 1}
{t: STRUCTURE_STORAGE, n: 1},
{t: STRUCTURE_TERMINAL, n: 1}
]
],
buildingTypes: [
STRUCTURE_EXTENSION,
STRUCTURE_TOWER,
STRUCTURE_STORAGE
STRUCTURE_STORAGE,
STRUCTURE_TERMINAL
],
refrechBuildingList: function (Engine, r) {
@ -99,7 +103,9 @@ var building = {
if (r.rRoadPath != undefined) {
for (let rO in r.rRoadPath) {
if (r.rRoadPath[rO].x == x && r.rRoadPath[rO].y == y) {
return true;
if (r.rRoadPath[rO].d != "storage") {
return true;
}
}
}
}
@ -333,6 +339,7 @@ var building = {
case STRUCTURE_EXTENSION:
case STRUCTURE_TOWER:
case STRUCTURE_STORAGE:
case STRUCTURE_TERMINAL:
oPos = building.findExtentionPlacement(Engine, r, b);
break;
}
@ -375,8 +382,12 @@ var building = {
if (r.RoomLevel == undefined || r.RoomLevel == null) {
r.RoomLevel = 0;
}
if (energy >= 1300 && lvl >= 4) {
if (energy >= 2300 && lvl >= 6) {
r.RoomLevel = 6;
} else if (energy >= 1800 && lvl >= 5) {
r.RoomLevel = 5;
} else if (energy >= 1300 && lvl >= 4) {
r.RoomLevel = 4;
} else if (energy >= 800 && lvl >= 3) {
r.RoomLevel = 3;
@ -402,7 +413,67 @@ var building = {
const roomTerrain = Game.map.getRoomTerrain(r.name);
r.rMap = roomTerrain.getRawBuffer();
},
planRoadForStorage: function (Engine, r) {
if (r.rRoadPath == undefined) {
return ;
}
if (
Game.rooms[r.name].storage == null ||
Game.rooms[r.name].storage == undefined
) {
return ;
}
let isAlreadySet = false;
for (let rp in r.rRoadPath) {
if (r.rRoadPath[rp].d == "storage") {
isAlreadySet = true;
}
}
if (isAlreadySet) {
return ;
}
let dest = new RoomPosition(
Game.rooms[r.name].storage.pos.x,
Game.rooms[r.name].storage.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();
for (let p in path) {
new RoomVisual(r.name).circle(
path[p].x,
path[p].y,
{
fill: 'transparent',
radius: 0.5,
fill: "gray"
}
);
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: "storage"
});
}
}
}
},
planExitRoads: function (Engine, r) {
if (r.rRoadPath == undefined) {
@ -682,7 +753,389 @@ var building = {
}
}
},
planWalls: function (Engine, r) {
// Only after path and storage
if (
r.rRoadPath == null ||
r.rRoadPath == undefined ||
Game.rooms[r.name].storage == null ||
Game.rooms[r.name].storage == undefined
) {
return ;
}
if (!(r.rWalls == null || r.rWalls == undefined)) {
// already done.
return ;
}
let rWalls = [];
// top
let x;
let y;
// isCrossingRoad
//building.getAt
//r.rMap
x = -1;
y = 0;
let rCorners = [];
while (++x < 50) {
let t = building.getAt(r.rMap, x, y);
if (t == 0) {
// detect corner
if (building.getAt(r.rMap, x - 1, y) == 1) {
rCorners = [];
rCorners.push({
x: x - 2,
y: y + 1
});
rCorners.push({
x: x - 2,
y: y + 2
});
rCorners.push({
x: x - 1,
y: y + 2
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (
ter != 1 &&
!building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)
) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
if (building.getAt(r.rMap, x + 1, y) == 1) {
rCorners = [];
rCorners.push({
x: x + 2,
y: y + 1
});
rCorners.push({
x: x + 2,
y: y + 2
});
rCorners.push({
x: x + 1,
y: y + 2
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (ter != 1 && !building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
let d = building.getAt(r.rMap, x, y + 2);
if (
d != 1 &&
!building.isCrossingRoad(
Engine,
r,
x,
y + 2
)
) {
rWalls.push({x: x, y: y + 2});
}
}
}
x = 0;
y = 49;
while (++x < 50) {
let t = building.getAt(r.rMap, x, y);
if (t == 0) {
// detect corner
if (building.getAt(r.rMap, x - 1, y) == 1) {
rCorners = [];
rCorners.push({
x: x - 2,
y: y - 1
});
rCorners.push({
x: x - 2,
y: y - 2
});
rCorners.push({
x: x - 1,
y: y - 2
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (
ter != 1 &&
!building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)
) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
if (building.getAt(r.rMap, x + 1, y) == 1) {
rCorners = [];
rCorners.push({
x: x + 2,
y: y - 1
});
rCorners.push({
x: x + 2,
y: y - 2
});
rCorners.push({
x: x + 1,
y: y - 2
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (ter != 1 && !building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
let d = building.getAt(r.rMap, x, y - 2);
if (
d != 1 &&
!building.isCrossingRoad(
Engine,
r,
x,
y - 2
)
) {
rWalls.push({x: x, y: y - 2});
}
}
}
x = 0;
y = 0;
while (++y < 50) {
let t = building.getAt(r.rMap, x, y);
if (t == 0) {
// detect corner
if (building.getAt(r.rMap, x, y - 1) == 1) {
rCorners = [];
rCorners.push({
x: x + 1,
y: y - 2
});
rCorners.push({
x: x + 2,
y: y - 2
});
rCorners.push({
x: x + 2,
y: y - 1
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (
ter != 1 &&
!building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)
) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
if (building.getAt(r.rMap, x, y + 1) == 1) {
rCorners = [];
rCorners.push({
x: x + 1,
y: y + 2
});
rCorners.push({
x: x + 2,
y: y + 2
});
rCorners.push({
x: x + 2,
y: y + 1
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (ter != 1 && !building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
let d = building.getAt(r.rMap, x + 2, y);
if (
d != 1 &&
!building.isCrossingRoad(
Engine,
r,
x + 2,
y
)
) {
rWalls.push({x: x + 2, y: y});
}
}
}
x = 49;
y = 0;
while (++y < 50) {
let t = building.getAt(r.rMap, x, y);
if (t == 0) {
// detect corner
if (building.getAt(r.rMap, x, y - 1) == 1) {
rCorners = [];
rCorners.push({
x: x - 1,
y: y - 2
});
rCorners.push({
x: x - 2,
y: y - 2
});
rCorners.push({
x: x - 2,
y: y - 1
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (
ter != 1 &&
!building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)
) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
if (building.getAt(r.rMap, x, y + 1) == 1) {
rCorners = [];
rCorners.push({
x: x - 1,
y: y + 2
});
rCorners.push({
x: x - 2,
y: y + 2
});
rCorners.push({
x: x - 2,
y: y + 1
});
for (let co in rCorners) {
let ter = building.getAt(
r.rMap,
rCorners[co].x,
rCorners[co].y
);
if (ter != 1 && !building.isCrossingRoad(
Engine,
r,
rCorners[co].x,
rCorners[co].y
)) {
rWalls.push({x: rCorners[co].x, y: rCorners[co].y});
}
}
}
let d = building.getAt(r.rMap, x - 2, y);
if (
d != 1 &&
!building.isCrossingRoad(
Engine,
r,
x - 2,
y
)
) {
rWalls.push({x: x - 2, y: y});
}
}
}
for (let w in rWalls) {
new RoomVisual(r.name).circle(
rWalls[w].x,
rWalls[w].y,
{
fill: 'transparent',
radius: 0.5,
fill: "yellow"
}
);
}
r.rWalls = rWalls;
},
// Entry Hook. Run for each owned room.
rHook : function (Engine, r) {
if (r.rMap.length == 0)
@ -710,12 +1163,13 @@ var building = {
building.getRoomControlLevelByEnergy(Engine, r);
building.findBuildSpace(Engine, r);
//building.findExtentionPlacementView(Engine, r);
building.planRoadForStorage(Engine, r);
// building.findExtentionPlacementView(Engine, r);
//building.displayBluildings(Engine, r);
building.planWalls(Engine, r);
//building.removeRoadExtrimities(Engine, r);
/*
1- Get the raw data to be able to analyze the map.

11
cUtility.js

@ -99,6 +99,15 @@ var cUtility = {
findEnergy(Engine, c) {
if (c.room.storage.store[RESOURCE_ENERGY] > 2000) {
if (c.withdraw(c.room.storage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
c.moveTo(c.room.storage);
}
} else {
let bDropt = false;
if (c.memory.savedSpot == null) {
@ -142,7 +151,7 @@ var cUtility = {
}
}
}
},
addCount : function (Engine, room, role) {

2
creepBasic.js

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

56
creepBuilder.js

@ -8,11 +8,19 @@ var creepBuilder = {
[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, WORK, WORK, WORK, WORK, // 1000
WORK, WORK, WORK, WORK, WORK, //
MOVE, MOVE, MOVE, MOVE, MOVE, // 250
CARRY, CARRY, CARRY, CARRY, CARRY,// 1000
CARRY, CARRY, CARRY, CARRY, CARRY,//
MOVE, MOVE, MOVE, MOVE, MOVE, //
MOVE, MOVE, MOVE, MOVE, MOVE //
], // 2300
[WORK,MOVE,CARRY],
[WORK,MOVE,CARRY]
],
limits: [0,3,3,3,4,1,1,1],
limits: [0,3,3,3,4,2,2,1],
// inner function for the creep
fnc: function (Engine, c) {
@ -33,7 +41,11 @@ var creepBuilder = {
c.memory.mode = 1;
}
let rIndex = Engine.m['cUtility'].getRoomIndexFromName(c.room.name);
let r = Memory.engine.rooms[rIndex];
let sbfound;
switch (c.memory.mode) {
case 0:
@ -55,10 +67,6 @@ var creepBuilder = {
// 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,
@ -84,12 +92,40 @@ var creepBuilder = {
break ;
}
}
c.memory.mode = 3;
if (c.memory.mode == 2) {
c.memory.mode = 3;
}
break ;
case 3:
// Build Walls
c.memory.mode = 4;
oRoad = null;
for (let ro in r.rWalls) {
let data = Game.spawns[r.rSpawn[0]].room.lookAt(
r.rWalls[ro].x,
r.rWalls[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;
}
}
}
if (!sbfound) {
Game.rooms[c.room.name].createConstructionSite(
r.rWalls[ro].x,
r.rWalls[ro].y,
STRUCTURE_WALL
);
c.memory.mode = 1;
break ;
}
}
if (c.memory.mode == 3) {
c.memory.mode = 4;
}
break ;
case 4:
// Build Ramparts

203
creepMover.js

@ -1,3 +1,204 @@
var creepMover = {};
var creepMover = {
name: 'creepMover',
role: 'mover',
body: [
[],
[],
[],
[],
[ // lvl4
CARRY, CARRY, CARRY, CARRY,
MOVE, MOVE
],
[ // lvl 5
CARRY, CARRY, CARRY, CARRY,
MOVE, MOVE
],
[ // lvl6 2300
CARRY, CARRY, CARRY, CARRY, CARRY,
CARRY, CARRY, CARRY, CARRY, CARRY, // 500
MOVE, MOVE, MOVE, MOVE, MOVE,
CARRY, CARRY, CARRY, CARRY, CARRY, // 500
CARRY, CARRY, CARRY, CARRY, CARRY,
MOVE, MOVE, MOVE, MOVE, MOVE, // 500
CARRY, CARRY, CARRY, CARRY, CARRY,
CARRY, CARRY, CARRY, CARRY, CARRY, // 500
MOVE, MOVE, MOVE, MOVE, MOVE // 250
],
[ // lvl7
CARRY, CARRY, CARRY, CARRY,
MOVE, MOVE
],
[ // lvl8
CARRY, CARRY, CARRY, CARRY,
MOVE, MOVE
]
],
limits: [0,0,0,0,1,1,1,1,1],
fnc: function (Engine, c) {
Engine.m['cUtility'].addCount(
Engine,
c.memory.home,
c.memory.role
);
if (c.memory.mode == 0) {
switch (c.memory.seek) {
case 0:
let nfound = false;
// Is there a capacitor needed energy ?
if (c.room.storage.store[RESOURCE_ENERGY] > 1000) {
let oTarget = c.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (
(
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_SPAWN
) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
);
}
});
if (oTarget) {
c.memory.sOrigin = c.room.storage.id;
c.memory.sDestination = oTarget.id;
if (c.store.getUsedCapacity() > 0) {
c.memory.mode = 2;
} else {
c.memory.mode = 1;
}
} else {
nfound = true;
}
} else {
nfound = true;
}
if (nfound == true) {
c.memory.seek++;
}
break;
case 1:
// Look if a container need empying
//
if (
c.room.storage == null ||
c.room.storage == undefined
) {
// No storage
c.memory.seek++;
} else {
const containersWithEnergy = c.room.find(
FIND_STRUCTURES,
{
filter: (i) =>
i.structureType == STRUCTURE_CONTAINER &&
i.store[
RESOURCE_ENERGY
] >= c.store.getFreeCapacity(
RESOURCE_ENERGY
)
}
);
//console.log(containersWithEnergy);
if (containersWithEnergy.length > 0) {
c.memory.sOrigin = containersWithEnergy[0].id;
c.memory.sDestination = c.room.storage.id;
if (c.store.getUsedCapacity() > 0) {
c.memory.mode = 2;
} else {
c.memory.mode = 1;
}
} else {
c.memory.seek++;
}
}
break;
default:
c.memory.seek = 0;
break;
}
} else {
c.memory.seek = 0;
}
let oAction = 0;
switch (c.memory.mode) {
case 1:
// take it from container
oDest = Game.getObjectById(c.memory.sOrigin);
oAction = c.withdraw(oDest, RESOURCE_ENERGY);
switch(oAction) {
case ERR_NOT_IN_RANGE:
c.moveTo(oDest);
break;
case OK:
case ERR_FULL:
c.memory.mode = 2;
break;
default:
c.memory.mode = 0;
break;
}
break;
case 2:
// put it in container
oDest = Game.getObjectById(c.memory.sDestination);
oAction = c.transfer(oDest, RESOURCE_ENERGY);
switch (oAction) {
case ERR_NOT_IN_RANGE:
c.moveTo(oDest);
break;
case OK:
case ERR_FULL:
c.memory.mode = 0;
c.memory.seek = 0;
break;
default:
c.memory.mode = 0;
break;
}
break ;
default:
c.memory.mode = 0;
break;
}
},
cHook: function (Engine, c) {
if (c.memory.role == creepMover.role) {
creepMover.fnc(Engine, c);
}
},
rHook: function (Engine, r) {
if (
Game.rooms[r.name].storage != null &&
Game.rooms[r.name].storage != null
) {
// Only is there is a storage active.
let iCount = Engine.m['cUtility'].getCount(
Engine,
r.name,
creepMover.role
);
let iMax = creepMover.limits[r.RoomLevel];
if (iCount < iMax) {
Game.spawns[r.rSpawn[0]].spawnCreep(
creepMover.body[r.RoomLevel],
'mover' + iCount + '--' + Math.random().toString(36).substr(2, 9),
{
memory : {
role: creepMover.role,
home: r.name,
module: creepMover.name
}
}
);
}
}
}
};
module.exports = creepMover;

4
defence.js

@ -3,10 +3,10 @@ var defence = {
maintenance (Engine, r) {
const maxHits = 20000;
const targets = Game.rooms[r.name].find(FIND_STRUCTURES, {
filter: object => object.hits < object.hitsMax
filter: object => object.hits < (object.hitsMax > maxHits ? maxHits : object.hitsMax)
});
targets.sort((a,b) => a.hits - b.hits);

3
main.js

@ -47,7 +47,8 @@ var Engine = {
'building', // Buidling planner
'creepBuilder', // Creep > Builder
'defence',
'creepMiner'
'creepMiner',
'creepMover'
];
// Load each module

Loading…
Cancel
Save