forked from Garethp/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles_builder.js
129 lines (111 loc) · 3.54 KB
/
roles_builder.js
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
/**
* @TODO: Make it more carry heavy, make it have helpers
* @type {{parts: *[], getParts: getParts, action: action}}
*/
var builder = {
parts: [
[Game.WORK,Game.WORK,Game.CARRY,Game.CARRY,Game.MOVE],
// [Game.WORK,Game.WORK,Game.CARRY,Game.CARRY,Game.MOVE, Game.MOVE, Game.CARRY],
// [Game.WORK,Game.WORK,Game.CARRY,Game.CARRY,Game.MOVE, Game.MOVE, Game.CARRY, Game.MOVE],
// [Game.WORK,Game.WORK,Game.CARRY,Game.CARRY,Game.MOVE, Game.MOVE, Game.CARRY, Game.MOVE, Game.WORK],
// [Game.WORK,Game.WORK,Game.CARRY,Game.CARRY,Game.MOVE, Game.MOVE, Game.CARRY, Game.MOVE, Game.WORK, Game.MOVE],
// [Game.WORK,Game.WORK,Game.CARRY,Game.CARRY,Game.MOVE, Game.MOVE, Game.CARRY, Game.MOVE, Game.WORK, Game.MOVE, Game.CARRY]
],
// getParts: function()
// {
// var _= require('lodash');
//
// var partsAllowed = Game.getRoom('1-1').find(Game.MY_STRUCTURES, {
// filter: function(structure)
// {
// return (structure.structureType == Game.STRUCTURE_EXTENSION && structure.energy >= 200);
// }
// }).length;
//
// var parts = [ Game.WORK, Game.WORK, Game.WORK, Game.CARRY, Game.MOVE ];
// var modulo = partsAllowed % 2;
// partsAllowed -= modulo;
// partsAllowed /= 2;
//
// if(partsAllowed > 5)
// partsAllowed = 5;
//
// for(var i = 0; i < partsAllowed; i++)
// parts.push(Game.MOVE, Game.CARRY);
//
// return parts;
//
// return this.prototype.getParts.call(this);
// },
action: function()
{
var creep = this.creep;
//If out of energy, go to spawn and recharge
if(creep.energy == 0) {
var closestSpawn = creep.pos.findNearest(Game.MY_SPAWNS, {
filter: function(spawn)
{
return spawn.energy > 0 && creep.pos.inRangeTo(spawn, 3);
}
});
if(closestSpawn) {
creep.moveTo(closestSpawn);
closestSpawn.transferEnergy(creep);
}
}
else {
//First, we're going to check for damaged ramparts. We're using ramparts as the first line of defense
//and we want them nicely maintained. This is especially important when under attack. The builder will
//repair the most damaged ramparts first
var structures = creep.room.find(Game.STRUCTURES);
var damagedRamparts = [ ];
for(var index in structures)
{
var structure = structures[index];
if(structure.structureType == 'rampart' && structure.hits < (structure.hitsMax - 50))
damagedRamparts.push(structure);
}
damagedRamparts.sort(function(a, b)
{
return(a.hits - b.hits);
});
if(damagedRamparts.length)
{
creep.moveTo(damagedRamparts[0]);
creep.repair(damagedRamparts[0]);
return;
}
//Next we're going to look for general buildings that have less than 50% health, and we'll go to repair those.
//We set it at 50%, because we don't want builders abandoning their duty every time a road gets walked on
var halfBroken = creep.room.find(Game.STRUCTURES);
var toRepair = [ ];
for(var index in halfBroken)
if((halfBroken[index].hits / halfBroken[index].hitsMax) < 0.5)
toRepair.push(halfBroken[index]);
if(toRepair.length)
{
var structure = toRepair[0];
creep.moveTo(structure);
creep.repair(structure);
return;
}
//If no repairs are needed, we're just going to go find some structures to build
var targets = creep.pos.findNearest(Game.CONSTRUCTION_SITES);
if(targets) {
if(!creep.pos.isNearTo(targets))
creep.moveTo(targets);
if(creep.pos.inRangeTo(targets, 0))
creep.suicide();
creep.build(targets);
return;
}
var target = this.rangedAttack();
if(target)
{
this.kite(target);
}
this.rest(true);
}
}
}
module.exports = builder;