Skip to content

Instantly share code, notes, and snippets.

View LiamKarlMitchell's full-sized avatar

Liam Mitchell LiamKarlMitchell

View GitHub Profile
@LiamKarlMitchell
LiamKarlMitchell / Clone keys of an object
Created December 11, 2014 22:13
Copies the keys from passed in object and returns a new object that references all the same kerys.
function Clone(o) {
var n = {};
for (var a in o) {
if (o.hasOwnProperty(a)) {
n[a] = o[a];
}
}
return n;
}
@LiamKarlMitchell
LiamKarlMitchell / Quad-Tree-Implementation---Including-AI.markdown
Created February 27, 2015 23:05
Quad Tree Implementation - Including AI

Quad Tree Implementation - Including AI

We needed a Quad Tree to handle spartial positioning in our MMORPG server over at github.com/LiamKarlMitchell/InfiniteSky

So this was born. I have been wanting to implement it for a while anyway.

Using Pixi.js for rendering it so I can see what is going on.

Add a few moving objects by using the button or click in the quad area to add static ones.

// Dump a bunch of things into it, it will queue them up and either send them when the queue is full or add more.
// Using some to call the callback function for the queued things.
// If you want you could view the array, process just the first or last thing and return true to break
// then no further execution would be done.
// Callback should expect value, index, array as arguments
function GroupOverTimeThenCall(callback, options) {
if (!(callback instanceof Function)) {
throw new Error('GroupOverTimeThenCall expects callback to be a function.');
}
@LiamKarlMitchell
LiamKarlMitchell / ToggleRows.js
Created August 22, 2015 23:31
Google sheets toggle, hide, show rows.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name: "Toggle Rows",
functionName: "toggleRows"
},{
name: "Hide Rows",
functionName: "hideRows"
},{
name: "Show Rows",
// Sometimes when using Send and Recv API it will not send or recv all the data. You will notice your socket might have an error and gets disconnected.
// Such as the other end wont be able to recv the entire packet correctly or something. The bytes that were not sent are never sent.
// This is horrible of course, I did not read the fine print and just expected it to work -_- (okay I guess an error could happen half way through sending or receiving some data and thats why it works this way.
// Email: liamkarlmitchell@gmail.com
// License: MIT - do whatever you want with this code.
// Borrowed from UNIX Network Programming: Sockets Introduction
// By Andrew M. Rudoff, Bill Fenner, W. Richard Stevens Feb 27, 2004
// http://www.informit.com/articles/article.aspx?p=169505&seqNum=9
// I license this as MIT do whatever you want with it basically.
// I can't be assed including the header you know what it is.
// If you don't want Mutex.h comment it out, check out my other gists for it if you need it.
#ifndef __CLOG_H
#define __CLOG_H
#include "Mutex.h"
#include <fstream>
// Another thing I made ages ago feel free to use :)
// licensed under MIT / Public / do whatever.
#ifndef __MUTEX_H
#define __MUTEX_H
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
// A way to compare two floating point numbers for "equality".
const EPSILON = 0.00000001;
function fEqual(a, b) {
return ((a - b) < EPSILON && (b - a) < EPSILON);
}
// An infamous mistake by rookies is to notice the following.
// 0.1 + 0.2 == 0.3
// And think it is a mistake when the language says false. Us humans would just say 0.3.
// But because of how float points work in computer you end up with something like 0.30000000000000004.
@LiamKarlMitchell
LiamKarlMitchell / unicode_card.js
Last active February 18, 2016 03:14
A way to output unicode information for a card number.
// https://en.wikipedia.org/wiki/Standard_52-card_deck
function unicodeCard(cardNumber) {
// As of Unicode 7.0 playing cards are now represented. Note that the following chart ("Playing Cards", Range: 1F0A0–1F0FF) includes cards from the Tarot Nouveau deck as well as the standard 52-card deck.
if (cardNumber < 0 || cardNumber > 56) { throw 'Invalid cardNumber '+cardNumber }
cardNumber += 2*Math.max(Math.ceil(cardNumber/14),1);
return String.fromCodePoint(127136-2+cardNumber);
}
function unicodeCardHTMLEncoded(cardNumber) {
// As of Unicode 7.0 playing cards are now represented. Note that the following chart ("Playing Cards", Range: 1F0A0–1F0FF) includes cards from the Tarot Nouveau deck as well as the standard 52-card deck.
@LiamKarlMitchell
LiamKarlMitchell / downloadFile.js
Last active September 3, 2019 10:34
node.js download file outputs progress and does not re-download already downloaded file.
var fs = require('fs');
var http = require('http');
var forceredownload = false;
function downloadFile(url, file, callback, redirect_count, known_size) {
console.log('Attempting to download ' + url + ' to ' + file);
if (redirect_count) {
if (redirect_count > 5) {
callback('Max redirects reached', url);
return;