Skip to content

Instantly share code, notes, and snippets.

@laverdet
laverdet / Cargo.toml
Created June 28, 2021 18:47
Cargo.toml
[package]
name = "loop"
version = "0.0.1"
edition = "2018"
[lib]
crate-type = [ "cdylib" ]
[dependencies]
chrono = "0.4"
@laverdet
laverdet / binlog-tailer.js
Created May 6, 2011 07:56
MySQL binlog tailer
"use strict";
this.MysqlBinlogTailer = MysqlBinlogTailer;
var EventEmitter = require('events').EventEmitter;
var fs = require('fs');
var path = require('path');
/**
* Tails a Mysql binlog and emits an event for every query executed.
*/
@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 / Makefile
Created June 28, 2021 04:36
Screeps WebAssembly module demo
# emcc: `-fwasm-exceptions` and node: `--experimental-wasm-eh` are available but not super well
# supported right now, so C++ exceptions will exit the wasm runtime via a JS exception.
CXX = em++
LD = emcc
CXXFLAGS = -O3 -std=c++20
EMFLAGS = -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ALLOW_MEMORY_GROWTH=1 --no-entry
loop.wasm: loop.o
@laverdet
laverdet / leading-spread-failure.cc
Created May 28, 2019 21:57
Leading spread failure
#include <stdio.h>
#include "v8.h"
#include "libplatform/libplatform.h"
using namespace v8;
void GCEpilogueCallback(Isolate* isolate, GCType type, GCCallbackFlags flags) {
printf("gc epilogue\n");
isolate->TerminateExecution();
}
@laverdet
laverdet / local_matrix.h
Created November 29, 2018 01:37
screeps local matrix
// Matrix of any type which holds a value for each position in a room
template <typename Type, typename Store, size_t Pack, bool Packed = sizeof(Store) << 3 != Pack>
class local_matrix_store_t {};
template <typename Type, typename Store, size_t Pack>
class local_matrix_store_t<Type, Store, Pack, false> {
protected:
std::array<Store, 2500> costs;
using reference = Type&;
using const_reference = typename std::conditional<std::is_trivial<Type>::value && sizeof(Type) <= sizeof(size_t), Type, const Type&>::type;
marcel@localhost ~/isolated-vm $ g++ thing.cc -std=c++11
thing.cc: In instantiation of 'void MakeThing(Args&& ...) [with Args = {}]':
thing.cc:16:12: required from here
thing.cc:12:44: error: use of deleted function 'Thing::Thing(const Thing&)'
Thing instance(std::forward<Args>(args)...);
^
thing.cc:5:2: note: declared here
Thing(const Thing&) = delete;
^
@laverdet
laverdet / niahash.c
Last active November 12, 2016 08:46
niahash.c w/ __uint128_t removed
#include <stdint.h>
#include <string.h>
#define HI(n) ((uint64_t)(n)>>32)
#define LO(n) ((uint64_t)(n)&0xffffffff)
#define U128(hi,lo) ((my_uint128_t){ .high = hi, .low = lo})
typedef struct {
uint64_t high;
uint64_t low;
let Long = require('long');
class Long128 {
constructor(lo, hi) {
this.lo = lo;
this.hi = hi;
}
static mul64(left, right) {
let u1 = new Long(left.low, 0, true);
@laverdet
laverdet / terrain.js
Created April 6, 2016 00:35
parse terrain from screeps png
getPx('https://s3.amazonaws.com/static.screeps.com/map2/'+ roomName+ '.png', function(err, pixels) {
if (err) {
throw err;
}
let kyskip = 50 * 3 * 3 * 4;
let kxskip = 3 * 4;
for (let yy = 0; yy < 50; ++yy) {
for (let xx = 0; xx < 50; ++xx) {
let px = pixels.data[xx * kxskip + yy * kyskip] * 0x10000 + pixels.data[xx * kxskip + yy * kyskip + 1] * 0x100 + pixels.data[xx * kxskip + yy * kyskip + 2];
switch (px) {