(not) building roads



  • Hi,

    I just started with the game, running simulations to test what I wrote so that I do have at least something going on when I release my creeps into the world.

    Now I wrote some code that doesn't do what I expect and I can't figure out why:

    var sources = Game.spawns[spawn].room.find(FIND_SOURCES);
    var newLength = sources.unshift(Game.spawns[spawn]);
    var newLength = sources.push(Game.rooms[name].controller);
    for(var i in towers) {
    	var newLength = sources.push(towers[i]);
    }
    for(var i in extenders) {
    	var newLength = sources.push(extenders[i]);
    }
    
    for(var poi in sources) {
    	for(var roadx = 0; roadx < 3; roadx++) {
    		for(var roady = 0; roady < 3; roady++) {
    			if(roadx == 1 && roady == 1) {
    				roady++;
    			} else {
    				var buildx = (sources[poi].x - 1 + roadx).valueOf();
    				var buildy = (sources[poi].y - 1 + roady).valueOf();
    				Game.spawns[spawn].room.createConstructionSite(buildx, buildy, STRUCTURE_ROAD);
    				Game.spawns[spawn].memory.lastbuild = Game.time + 100;
    				console.log('building Road at '+buildx+', '+buildy);
    			}
    		}
    	}
    }
    

    The expected behavior is to build roads around every source, the spawn, the controller, extenders and towers. The actual behavior is spamming "building Road at NaN, NaN" 8 times per sources.length (you know what I mean).

    What do I miss?



  • sources[poi].x is likely the issue. Every game object has a roomPosition, which is normally stored in its .pos property.

    https://docs.screeps.com/api/#Source.pos

    So, sources[poi].pos.x & sources[poi].pos.y will be your X and Y values, while sources[poi].x is no a defined property of sources, valueOf is probably making it NaN, instead of throwing an error (I presume)

    Also, would recommend checking out the screep's slack! There's a #help section that is normally pretty lively and faster to get response, bit more dynamic as well.



  • just changed the lines to the structure of

    var buildx = sources[poi].pos.x - 1 + roadx;
    

    Well, now it's trying to build the roads on 7 of the 8 spots so my logic must hav a mistake. A way more interesting problem is the game putting construction sites on the spawned barriers: 0_1602245653598_Screeps Roads.PNG What's going on there? 😕



  • Fixed it by using Game.map.getRoomTerrain and if(terrain.get(buildx, buildy) == 0 || terrain.get(buildx, buildy) == 2)