I'd also restructure it, it is too WET right now, you want your code DRY
One might argue that Game.spawns.Spawn1.createCreep is only one line, but this is a function call, that you WILL change as your screeps empire grows, you'll heave more than one spawn, createCreep may not succeed and you'd want to fall back to another one. So I'd split this loop to two logical sections
1)Pick creep
2)Create creep
Code becomes:
```javascript
//Select what to build
var creepBody = null;
var creepMemory = null;
if (guards < 4){
creepBody = guard;
creepMemory = {role:"guard"}
}else if (medics < 3){
creepBody = medic;
creepMemory = {role:"medic"}
}
...
//Build
Game.spawns.Spawn1.createCreep(creepBody, null, creepMemory); //easy to replace the createCreep call, fall back when it fails, etc.
//Now, let's get more javascriptey:)
var creepSetupsByRole = {
"guard" : {
"min": 4,
"body": [ATTACK, ATTACK, MOVE],
"max": 10,
"curr": 0,
"roleName" : 'guard',
"essential" : true //Do not try to build others unless have enough of this role
},
"medic" : {
"min": 4,
"body": [HEAL, HEAL, MOVE],
"max": 10,
"curr": 0,
"roleName" : 'medic'
};
//Count the current number of creeps with given role here.
//Possibly sort roles by priorities
for (var roleName in creepSetupsByRole){
var currRoleSetup = creepSetupsByRole[roleName];
if (currRoleSetup.curr < currRoleSetup.min){
var buildResult = tryBuildCreep(currRoleSetup);
if (!(buildResult < 0) && !currRoleSetup.essential){ //
break;
}
}
}
function tryBuildCreep(creepSetup){
return Game.spawns.Spawn1.createCreep(creepSetup.body, null, {role: creepSetup.roleName});
}