Screeps: World
JOHNNY GUEGGU Nov 30, 2016 @ 12:46pm
If you struggle with the Tower - look here! :)
Hi Guys

I had a fair amount of trouble getting the tower to work. I never programmed Javascript (or any other Object-Oriented language) before. Anyhow, I start getting the hang of this. :D

So for the attacking Part of the tower, you can just copy someone else's snippet. They are everywhere. I've gone with this one: http://support.screeps.com/hc/en-us/articles/203339002-Defending-your-room

The Building-Repairing is a bit more complicated, and code is vry hard to find. I had some inspiration here:
https://m.reddit.com/r/screeps/comments/4s6jrc/help_for_a_code_its_for_the_tower/?utm_source=mweb_redirect&compact=true

So I merged those two routines and learned enough in the process to write the third myself, the creep-healing part.

The code below does [In that order]:

- Defend your room and kill ANY hostiles that enter it, until the tower is EMPTY

- Heal any damaged creeps, until the tower is EMPTY

- If there are NO hostile creeps in the room, the tower repairs your buildings (But NOT the Walls or Ramparts - you can google the reasons) but only until the tower's energy is HALF gone. (You can tweak the value of course. ;) Because if you use up all your energy repairing stuff, and someone shows up at your door, you' have a problem. ;)

The advantages of the tower repairing / healing things are obvious:

-You need less repairer/healer creeps
- The tower repairs/heals more efficient than creeps - he doesn't have to travel! :)

This being said, I am still in a Novice Zone. As soon as the gloves are off, I'll deactivate that repairer-function I guess, just to be ready.

And I'll probarbly add a cooldown value after the tower attacked a hostile, so that in case the attack pasuses for a moment, the tower doesn't get drained healing/repairing stuff.

EDIT: The previous code had a bug in it when you had more than 1 tower.
I updated the code below. Now, you cna use several towers and everything works.

module.exports = { //TOWER CODE defendMyRoom: function(myRoomName) { /* ORIGINAL TOWER CODE //TOWER CODE var towers = Game.rooms.W61S27.find(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_TOWER}); for (let tower of towers) { var target = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS); if (target != undefined) { tower.attack(target); } } */ var hostiles = Game.rooms[myRoomName].find(FIND_HOSTILE_CREEPS); var towers = Game.rooms[myRoomName].find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER}}); //if there are hostiles - attakc them if(hostiles.length > 0) { var username = hostiles[0].owner.username; Game.notify(`User ${username} spotted in room ${myRoomName}`); towers.forEach(tower => tower.attack(hostiles[0])); console.log("ALERT!!!! WE ARE UNDER ATTACK!!!!! ALERT!!!! WE ARE UNDER ATTACK!!!!! ALERT!!!! WE ARE UNDER ATTACK!!!!! ALERT!!!! WE ARE UNDER ATTACK!!!!! "); } //if there are no hostiles.... if(hostiles.length === 0) { //....first heal any damaged creeps for (let name in Game.creeps) { // get the creep object var creep = Game.creeps[name]; if (creep.hits < creep.hitsMax) { towers.forEach(tower => tower.heal(creep)); console.log("Tower is healing Creeps."); } } for(var i in towers){ //...repair Buildings! :) But ONLY until HALF the energy of the tower is gone. //Because we don't want to be exposed if something shows up at our door :) if(towers.energy > ((towers.energyCapacity / 10)* 9)){

//Find the closest damaged Structure
var closestDamagedStructure = towers.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL && s.structureType != STRUCTURE_RAMPART});
if(closestDamagedStructure) {
towers.repair(closestDamagedStructure);
console.log("The tower is repairing buildings.");
}
}
}

}
}
};
Last edited by JOHNNY GUEGGU; Dec 4, 2016 @ 7:33am
< >
Showing 1-5 of 5 comments
Mangochicken Sep 30, 2021 @ 9:12pm 
this may be a 4 year old thread but how do you run this in the main thread
Kuma Sama Jun 30, 2022 @ 9:55pm 
this may be a 5 year old thread but how do you run this in the main thread
Joe Musashi Jun 6, 2023 @ 1:00pm 
this may be a 6 year old thread but how do you run this in the main thread
MileZero313 Jul 9, 2023 @ 6:26pm 
Originally posted by Joe Musashi:
this may be a 6 year old thread but how do you run this in the main thread
I came here trying to figure this out.. I am thinking using the function created above similarly to how the tutorial does run(creep)
Shylo132 Apr 7, 2024 @ 5:49pm 
this may be a 7 year old thread but how do you run this in the main thread
Last edited by Shylo132; Apr 7, 2024 @ 5:49pm
< >
Showing 1-5 of 5 comments
Per page: 1530 50