Skip to content

Instantly share code, notes, and snippets.

@Dykam
Forked from anonymous/Ballista shorten
Created September 13, 2010 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dykam/577184 to your computer and use it in GitHub Desktop.
Save Dykam/577184 to your computer and use it in GitHub Desktop.
typeToName = ["gb", "rb", "bb", "yb", "sgb", "srb", "sbb", "syb", "sb", "sub", "td", "tdm", "db", "1eab", "2eab", "sbv", "sbh", "sbc", "pgb", "prb", "pbb", "pyb"];
var bitsPerChar = 14; // 14 bits fit in the CJK character range, 0x4E00 -> 0x9FCF
var arrowBits = Math.ceil(Math.log( 20 )/Math.log(2));
var amountBits = Math.ceil(Math.log( 256 )/Math.log(2));
var gameWidth = 11;
var gameHeight = 14;
var types = typeToName.length;
nameToType = [];
var i = types;
while(i-- > 0) {
nameToType[typeToName[i]] = i + 1;
typeToName[i + 1] = typeToName[i];
}
typeToName[0] = undefined;
var typeBits = Math.ceil(Math.log(types + 1)/Math.log(2));
var positionBits = Math.ceil(Math.log(gameWidth * gameHeight)/Math.log(2));
var endMarker = 0;
getShortCode = function(objects, arrows, req_green, req_red, req_blue, req_yellow) {
var pack = function(bitArray) {
var result = [];
var i = 0;
do {
var num = 0;
var shift = 0;
while(true) {
num |= (bitArray[i++] || 0) & 1;
if(++shift == bitsPerChar)
break;
num <<= 1;
};
result.push(num + 0x4E00);
} while(i < bitArray.length);
return String.fromCharCode.apply(null, result);
}
function group(objects) {
var getPos = function(object) {
return gameWidth * object.tileY + object.tileX;
}
var groupedObjects = [];
for(var i in objects) {
var type = nameToType[objects[i].type];
var groupedObject = groupedObjects[type] || (groupedObjects[type] = []);
groupedObject.push(getPos(objects[i]));
}
return groupedObjects;
}
var process = function(groupedObjects) {
var bitArray = [];
var write = function(number, bits) {
while(bits-- > 0)
bitArray.push((number >> bits) & 1);
}
write(arrows, arrowBits);
write(req_green, amountBits);
write(req_red, amountBits);
write(req_blue, amountBits);
write(req_yellow, amountBits);
for(i in groupedObjects) {
var groupedObject = groupedObjects[i];
if(groupedObject) {
write(i, typeBits);
write(groupedObject.length, amountBits);
for(var j in groupedObject)
write(groupedObject[j], positionBits);
}
}
write(0, amountBits);
}
return pack(process(group(objects)));
}
getObjects = function(shortCode) {
var unpack = function(shortCode) {
var bitArray = []
var i = 0;
var mask = 1 << bitsPerChar - 1;
do {
var num = shortCode.charCodeAt(i);
if(num < 0x4E00 || num > 0x9FFF)
return false;
num -= 0x4E00;
var shift = 0;
do {
bitArray.push((num & mask) ? 1 : 0);
num <<= 1;
} while(++shift < bitsPerChar)
} while(++i < shortCode.length);
return bitArray;
}
var process = function(bitArray) {
var read = function(bits) {
var num = 0;
while(bits-- > 0)
num |= (bitArray.shift() & 1) << bits;
return num;
}
var result = Object();
result.arrows = read(arrowBits);
result.req_green = read(amountBits);
result.req_red = read(amountBits);
result.req_blue = read(amountBits);
result.req_yellow = read(amountBits);
result.objects = [];
var type;
while((type = read(typeBits)) != 0) {
var amount = read(amountBits);
while(amount-- > 0) {
result.objects.push([typeToName[type], pos % gameWidth, Math.floor(pos / gameWidth)]);
}
}
return result;
}
var unpacked = unpack(shortCode);
if(unpacked)
return process(unpacked);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment