-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.builderSpawn.js
More file actions
126 lines (125 loc) · 4.69 KB
/
Room.builderSpawn.js
File metadata and controls
126 lines (125 loc) · 4.69 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
var optimalBuilder = function(room) {
var carryBodyparts, workBodyparts, moveBodyparts, creepCost, bodyparts, progressPerTick;
carryBodyparts = 2;
//x = 1
workBodyparts = Math.floor((2 * room.energyAvailable - (2 * BODYPART_COST[CARRY] + BODYPART_COST[MOVE]) * carryBodyparts) / (BODYPART_COST[MOVE] + 2 * BODYPART_COST[WORK]));
//y = (2 * t - (2 * c + m) * x) / (m + 2 * w);
moveBodyparts = Math.ceil((carryBodyparts + workBodyparts) / 2);
//z = (x + y) / 2
//W = x + y
creepCost = carryBodyparts * BODYPART_COST[CARRY] + workBodyparts * BODYPART_COST[WORK] + moveBodyparts * BODYPART_COST[MOVE];
//t = x * c + y * w + z * m
bodyparts = [];
for(var i = 0; i < moveBodyparts; i++) {
bodyparts.push(MOVE);
}
for(i = 0; i < workBodyparts; i++) {
bodyparts.push(WORK);
}
for(i = 0; i < carryBodyparts; i++) {
bodyparts.push(CARRY);
}
if(workBodyparts > 0 && moveBodyparts > 0 && carryBodyparts > 0) {
return {
carryBodyparts: carryBodyparts,
workBodyparts: workBodyparts,
moveBodyparts: moveBodyparts,
creepCost: creepCost,
bodyparts: bodyparts,
progressPerTick: progressPerTick
};
}
};
var RoomBuilderSpawn = function() {
var multiplier = 1;
var constructionProjects = this.memory.found.myConstructionSites;
var progressTotal = 0;
var progress = 0;
constructionProjects.forEach(function(constructionProject) {
progressTotal = progressTotal + constructionProject.progressTotal;
progress = progress + constructionProject.progress;
});
var remainingProgress = progressTotal - progress;
var structures = this.memory.found.structures;
var hitsTotal = 0;
var hits = 0;
structures.forEach(function(structure) {
if(structure.hitsMax > 0 && structure.hits) {
hitsTotal = hitsTotal + structure.hitsMax;
hits = hits + structure.hits;
}
});
var repairNeeded = hitsTotal - hits;
//console.log("hitsTotal: "+hitsTotal);
//console.log("hits: "+hits);
//REPAIR_POWER
//console.log("remainingProgress: "+remainingProgress);
var creeps = this.memory.found.myCreeps;
var builders = _.filter(creeps, function(creep) {
if(creep.memory.recycle) {
return false;
} else if(creep.memory.role == 'builder') {
return true;
}
});
builders.sort(function(a, b) {
var a_work = a.getActiveBodyparts(WORK);
var b_work = b.getActiveBodyparts(WORK);
return a_work - b_work;
});
var builder = optimalBuilder(this);
var workBodyparts = 0;
builders.forEach(function(creep) {
workBodyparts = workBodyparts + creep.getActiveBodyparts(WORK);
});
var buildPerTick = workBodyparts * BUILD_POWER;
var repairPerTick = workBodyparts * REPAIR_POWER;
var timeToFinish;
var timeToBuild;
if(buildPerTick > 0) {
timeToBuild = remainingProgress / buildPerTick;
}
var timeToRepair;
if(repairPerTick > 0) {
timeToRepair = repairNeeded / repairPerTick;
}
if(buildPerTick > 0) {
if(repairPerTick > 0) {
timeToFinish = timeToBuild + timeToRepair;
} else {
timeToFinish = timeToBuild;
}
} else {
if(repairPerTick > 0) {
timeToFinish = timeToRepair;
}
}
if(builders.length > 0) {
if(this.memory.spawns.length > 0) {
if(builder) {
if(this.memory.energyPercent === 1) {
if(builder.workBodyparts > builders[0].getActiveBodyparts(WORK)) {
builders[0].memory.recycle = true;
this.memory.spawns[0].createCreep(builder.bodyparts, undefined, {role: 'builder'});
this.memory.spawns[0].memory.busy = Game.time;
} else if(timeToFinish > 720) {
if(buildPerTick + builder.workBodyparts * BUILD_POWER < this.memory.harvestPerTick * multiplier) {
this.memory.spawns[0].createCreep(builder.bodyparts, undefined, {role: 'builder'});
this.memory.spawns[0].memory.busy = Game.time;
}
}
}
}
}
} else {
if(this.memory.spawns.length > 0) {
if(builder) {
this.memory.spawns[0].createCreep(builder.bodyparts, undefined, {role: 'builder'});
this.memory.spawns[0].memory.busy = Game.time;
}
}
}
this.memory.buildPerTick = buildPerTick;
this.memory.remainingProgress = remainingProgress;
};
module.exports = RoomBuilderSpawn;