-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreep_create.js
More file actions
142 lines (110 loc) · 3.37 KB
/
Copy pathcreep_create.js
File metadata and controls
142 lines (110 loc) · 3.37 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
let info_perf = require('info_perf');
let info_creep = require('info_creep');
let info_room = require('info_room');
let costBody = 0;
let body = [];
let spawn;
let energyAvailable
let scriptName = "creep_create";
let add_part = function(part) {
let cost_part = BODYPART_COST[part];
if (costBody + cost_part <= energyAvailable && body.length < 50) {
body.push(part);
costBody += cost_part;
return true;
}
return false;
}
let try_create_creep = function(config) {
info_perf.init(scriptName, false);
let homes = [];
let home_spawns = [];
info_perf.log(scriptName, "Init variables");
if (config.range === "local") {
let creeps = info_creep.get(config.role);
let creepsGroupByHome = _.groupBy(creeps, 'memory.home')
let myRoomKeys = info_room.get_my_room_keys();
for (room_key of myRoomKeys) {
let creepsRoom = creepsGroupByHome[room_key];
let nbCreepsRoom;
if (creepsRoom !== undefined) {
nbCreepsRoom = creepsRoom.length;
} else {
nbCreepsRoom = 0;
}
let nb_max_creep_by_room = config.max / info_room.get_nb_my_room();
if (nbCreepsRoom < nb_max_creep_by_room) {
homes.push(room_key);
}
}
info_perf.log(scriptName, "Init liste home");
config.home = homes[0]
info_perf.log(scriptName, "Init home in config");
home_spawns = Game.rooms[config.home].find(FIND_STRUCTURES, {
filter: (structure) => structure.structureType == STRUCTURE_SPAWN
});
info_perf.log(scriptName, "Get home spawn");
}
let others_spawns = _.difference(Object.values(Game.spawns), home_spawns)
let spawns = home_spawns.concat(others_spawns);
info_perf.log(scriptName, "Get list spawn");
for (let spawn of spawns) {
let ret = create_creep(config, spawn);
if (ret === OK) {
return true;
}
}
info_perf.finish(scriptName);
return false;
}
let create_creep = function(config, spawn) {
if (spawn == undefined) return;
if (config.strict == undefined) config.strict = false;
let ret = false;
energyAvailable = spawn.room.energyAvailable;
costBody = 0;
body = [];
let okLaunchSpawn = false;
let message = config.role + ", home:" + config.home + "(" + spawn.room.name + ")";
info_perf.log(scriptName, "Initialisation de variable");
if (config.strict) {
config.model.forEach((module) => okLaunchSpawn = add_part(module))
} else {
let indexSpe = 0;
while (add_part(config.model[indexSpe])) {
indexSpe = (indexSpe + 1) % config.model.length;
}
}
info_perf.log(scriptName, "construction du body");
message += " (" + body.length + " parts) (" + costBody + "/" + energyAvailable + ")";
if (!config.strict) {
okLaunchSpawn = body.length >= 3
info_perf.log(scriptName, "vérification de la taille mini du body");
}
//message += "config.strict " + config.strict + " okLaunchSpawn " + okLaunchSpawn;
if (okLaunchSpawn) {
ret = spawn.spawnCreep(body, config.role + Game.time, {
memory: {
role: config.role,
color: config.color,
range: config.range,
home: config.home
}
});
message += " spaw du creep (ret=" + ret + ")"
info_perf.log(scriptName, "spaw du creep");
} else {
if (config.strict) {
message += " config.model non respecté (move," + config.model + ")";
} else {
message += " ressource insuffisante";
}
info_perf.log(scriptName, "message erreur");
}
info_perf.simpleLog(scriptName, message);
info_perf.finish(scriptName);
return ret;
}
module.exports = {
try_create_creep: try_create_creep
};