Skip to content

Instantly share code, notes, and snippets.

function addBlockedRoomsToMatrix(theRoom)
{
console.log(theRoom)
var theMatrix = new PathFinder.CostMatrix
{
var exits = Game.map.describeExits(theRoom);
for(var ext in exits)
{
var room = exits[ext];
@laverdet
laverdet / index.js
Created March 10, 2016 22:39
Find a very long path in Screeps
function longPath(from, to) {
let rooms = {};
Game.map.findRoute(from.roomName, to.roomName).forEach(function(step, ii, route) {
function generateCostMatrix(dir1, dir2) {
let cm = new PathFinder.CostMatrix;
let dir = 1 << dir1 | 1 << dir2;
for (let ii = 0; ii < 49; ++ii) {
if (!(dir & 1 << TOP)) {
cm.set(ii, 0, 0xff);
}
@laverdet
laverdet / README
Created October 9, 2015 20:38
screeps cpu migration
Put this at the top of your main.js file. Log `extraCPU` at the end to see how much extra CPU will be spent.
If you're using the "loop" architecture you have to move some stuff around. Just reset `extraCPU` and `called` every tick.
@laverdet
laverdet / sorted.js
Last active August 29, 2015 14:25
How to merge sorted arrays
function mergeSorted(a, b) {
var array = new Array(a.length + b.length);
var ii = a.length + b.length - 1;
var jj = a.length - 1, kk = b.length - 1;
while (jj >= 0 || kk >= 0) {
array[ii--] = (jj < 0 || a[jj] < b[kk]) ? b[kk--] : a[jj--];
}
return array;
}
@laverdet
laverdet / body.js
Created July 20, 2015 05:42
Screeps Body class
// Descriptor of a creep body
function Body(body) {
this.body = body;
this._isCreep = typeof body[0] === 'object';
this._cost = undefined;
this._weight = undefined;
}
_.assign(Body.prototype, {
@laverdet
laverdet / README.md
Last active September 7, 2021 05:02
Screeps git sync script

To use first you need to create two branches, master and sim using the in-game editor. Then just run node sync.js from your local machine. When you're on your local master branch it will push to master in game, any other branch (including detached states) will push to sim. Note that it will push unstaged changes, so be careful when switching to branches with modified code.

There is support included for compressing your JS files on the server side via UglifyJS. Some users reported some speedups by compressing their code, however I did not notice any significant gains. Furthermore, UglifyJS does not yet support ES6 features, so it's disabled by default.

@laverdet
laverdet / repl
Created April 30, 2015 06:18
fibers repl
#!/usr/bin/env node
"use strict";
var fs = require('fs');
var Future = require('fibers/future');
// Start the repl
var vm = require('vm');
var domain = require('domain');
var repl = require('repl').start('node> ', null, fiberEval, true, true);
function fiberEval(code, context, file, cb) {
@laverdet
laverdet / patch.diff
Created December 12, 2014 03:27
node-fibers issue #158
diff --git a/src/coroutine.cc b/src/coroutine.cc
index f2a0d3d..64bf201 100644
--- a/src/coroutine.cc
+++ b/src/coroutine.cc
@@ -20,9 +20,10 @@
using namespace std;
const size_t v8_tls_keys = 3;
-static pthread_key_t floor_thread_key = 0;
-static pthread_key_t ceil_thread_key = 0;
diff --git a/src/coroutine.cc b/src/coroutine.cc
index f2a0d3d..3c9a8ad 100644
--- a/src/coroutine.cc
+++ b/src/coroutine.cc
@@ -20,8 +20,6 @@
using namespace std;
const size_t v8_tls_keys = 3;
-static pthread_key_t floor_thread_key = 0;
-static pthread_key_t ceil_thread_key = 0;
#include <iostream>
#include <v8.h>
using namespace std;
using namespace v8;
// simple print utility; mostly lifted from shell.cc
Handle<Value> Print(const Arguments& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {