Created
September 13, 2010 11:53
-
-
Save anonymous/577182 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = 10; // the higher, the more bits are tried to put into a single character. Max seems 20. | |
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 bitArray = []; | |
var write = function(number, bits) { | |
while(bits-- > 0) | |
bitArray.push((number >> bits) & 1); | |
} | |
var readyNumber = function(num) { // make sure the num is a valid unicode character. E.g. not a space or a backspace character, nor noncharacters | |
num += 33; // Skip weirdnesses. | |
if(num < 127) return num; | |
num++; | |
if(num < 255) return num; | |
num++; | |
// based of the unicode part 16.7 Noncharacters and http://en.wikipedia.org/wiki/Specials_(Unicode_block) | |
if(num < 0xFDD0) return num; | |
num += 0x20; | |
if(num < 0xFFF0) return num; | |
num += 0x10; | |
for(i = 0; i <= 0x10; i++) { | |
if(num < (0xFFFE & (i << 16))) return num; | |
num += 2; | |
} | |
return num; | |
} | |
var pack = function() { | |
var result = []; | |
var i = 0; | |
do { | |
var num = 0; | |
var shift = 0; | |
do { | |
num |= bitArray[i++] & 1; | |
num <<= 1; | |
} while(++shift < bitsPerChar); | |
result.push(String.fromCharCode(readyNumber(num))); | |
} while(i < bitArray.length); | |
return result.join(""); | |
} | |
var getPos = function(object) { | |
return gameWidth * object._y + object._x; | |
} | |
var groupedObjects = [] | |
for(i in objects) { | |
var groupedObject = groupedObjects[i] || []; | |
groupedObject.push(getPos(objects[i])); | |
groupedObjects[nameToType[groupedObject.type]] = groupedObject; | |
} | |
trace("groupedObjects = " + groupedObjects); | |
trace("Objects = " + objects); | |
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(); | |
} | |
getObjects = function(shortCode) { | |
var result = Object(); | |
var readyNumber = function(num) { // make sure the num is a valid unicode character. E.g. not a space or a backspace character, nor noncharacters | |
for(i = 0x10; i >= 1; i--) | |
if(num > (0xFFFF & (i << 16))) | |
num -= 2; | |
if(num > xFFFF) | |
num -= 0x10; | |
if(num > 0xFDEF) | |
num -= 0x20; | |
if(num > 255) | |
num--; | |
if(num > 127) | |
num--; | |
num -= 33; | |
return num; | |
} | |
var unpack = function(nums) { | |
var i = 0; | |
var mask = 1 << bitsPerChar - 1; | |
do { | |
var num = bytes[i]; | |
var shift = 0; | |
do { | |
bitArray.push((num & mask) ? 1 : 0); | |
num <<= 1; | |
} while(shift < bitsPerChar) | |
} while(i < nums.length); | |
} | |
var i = shortCode.length; | |
var nums = [] | |
while(i-- > 0) { | |
nums[i] = shortCode.charCodeAt(i); | |
} | |
var bitArray = unpack(nums); | |
var read = function(bits) { | |
var num = 0; | |
while(bits > 0) | |
num |= (bitArray.shift() & 1) << bits; | |
} | |
var setPos = function(pos, object) { | |
object._x = pos % gameWidth; | |
object._y = Math.floor(pos / gameWidth); | |
} | |
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) { | |
var object = new Object(); | |
object.type = type; | |
setPos(read(positionBits), object); | |
result.objects.push(object); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment