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 / 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 / log.txt
Created April 26, 2016 00:00
node or v8 bug?
marcel@marcel-2 ~ $ node -v
v5.10.1
marcel@marcel-2 ~ $ cat a.js
"use strict";
switch (1) {
case 2:
let foo;
}
foo.bar;
marcel@marcel-2 ~ $ node a.js
@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) {
[...]
// Reverse of parseRoomName
renderRoomName(room) {
if (room[0] === 255) {
return 'sim';
}
return (
(room[0] < 0 ? 'W'+ (-room[0] - 1) : 'E'+ room[0])+
(room[1] < 0 ? 'N'+ (-room[1] - 1) : 'S'+ room[1])
);