Created
October 28, 2015 00:14
-
-
Save Hernanduer/8c3e2aaca086fde3eb5d 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
deserialize: function(str) { | |
//var timer = Game.getUsedCpu(); | |
if (str == null) | |
str = "02161101101010100121222222222222222222222222222222222222222222"; | |
var path = []; | |
for (var i = 0; i < (str.length - 4) / 2; ++i) { | |
path.push({x: 0, y: 0, dx: 0, dy: 0, direction: 0}); | |
} | |
var pos = [0, 0]; | |
var loc = 0; | |
for (var i = 0; i < 100; ++i) { | |
if (loc == str.length) break; | |
var n = (loc - 4) / 2; | |
if (i == 0) { | |
n = 0; | |
path[n].x = parseInt(str.substring(0, 2)); | |
path[n].y = parseInt(str.substring(2, 4)); | |
loc = 4; | |
} else { | |
path[n].dx = parseInt(str.substring(loc++, loc)) - 1; | |
path[n].dy = parseInt(str.substring(loc++, loc)) - 1; | |
path[n].x += path[n].dx; | |
path[n].y += path[n].dy; | |
if (n > 0) { | |
path[n].x += path[n - 1].x; | |
path[n].y += path[n - 1].y; | |
} | |
switch (path[n].dx) { | |
case 1: | |
switch (path[n].dy) { | |
case -1: | |
path[n].direction = 2; | |
break; | |
case 0: | |
path[n].direction = 3; | |
break; | |
case 1: | |
path[n].direction = 4; | |
break; | |
} | |
break; | |
case 0: | |
switch (path[n].dy) { | |
case -1: | |
path[n].direction = 1; | |
break; | |
case 1: | |
path[n].direction = 5; | |
break; | |
} | |
break; | |
case -1: | |
switch (path[n].dy) { | |
case -1: | |
path[n].direction = 6; | |
break; | |
case 0: | |
path[n].direction = 7; | |
break; | |
case 1: | |
path[n].direction = 8; | |
break; | |
} | |
break; | |
} | |
} | |
} | |
//var t = Game.getUsedCpu() - timer; | |
//for (var p in path) { | |
// console.log(path[p].x + ";" + path[p].y); | |
//} | |
//console.log(t); | |
return path; | |
}, | |
serialize: function(path) { | |
var str = ""; | |
var x = (path[0].x + path[0].dx); | |
var y = (path[0].y + path[0].dy); | |
if (x < 10) str += ("0" + x); | |
else str += x; | |
if (y < 10) str += ("0" + y); | |
else str += y; | |
for (var p = 0; p < path.length; ++p) { | |
str += (path[p].dx + 1); | |
str += (path[p].dy + 1); | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment