aboutsummaryrefslogtreecommitdiff
path: root/space.js
diff options
context:
space:
mode:
Diffstat (limited to 'space.js')
-rw-r--r--space.js93
1 files changed, 55 insertions, 38 deletions
diff --git a/space.js b/space.js
index 58023a9..619b47b 100644
--- a/space.js
+++ b/space.js
@@ -5,10 +5,11 @@
const fs = require('fs')
function chop(string, n){ // chops a string into n-sized chunks. Assumed to be perfect multiple
- arr = []
- while (string.length > 0){
- arr += string.slice(0,16);
+ let arr = [];
+ for (let sec = 0; sec < string.length; sec++){
+ arr.push(string.slice(sec*n,(sec+1)*n));
}
+ return arr;
}
function replace(text, old, repl){ //replaces, in an array `text`, the instances of `old` with `repl`
for (let i=0; i<text.length; i++){
@@ -16,74 +17,86 @@ function replace(text, old, repl){ //replaces, in an array `text`, the instances
}
return text;
}
+function zeroifnull(obj, prop, alt=0){ //If obj is null, return 0; else obj.prop
+ if (typeof(obj) == 'undefined') return alt;
+ else return obj[prop];
+}
function Space(){ // CLASS
this.data = []; //
self = this;
- this.fromfetch = function(tiles, dimension){ //tiles is straight from fetch function, dimension is a quadruplet
+ this.fromfetch = function(tiles, dimension, conform=true){ //tiles is straight from fetch/tileUpdate function, dimension is a quadruplet; conform is false for tileUpdate because the cell_props don't actually mean anything; all data is still included
for (let y=dimension[0]; y<=dimension[2]; y++){
for (let line=0; line<8; line++)
this.data.push([]); // Adds lines
for (let x=dimension[1]; x<=dimension[3]; x++){
- tilein(tiles[[y,x]],y-dimension[0]);
+ if (! tiles[[y,x]]) tilein({"content":' '.repeat(16*8),"properties":{"cell_props":{}}}); // Insert a null tile
+ else tilein(tiles[[y,x]],y-dimension[0]);
}
}
function tilein(tile, tilerow){ //tile is one of the tiles from `tiles`, and tilerow is y-dimension[0]; helper function
- let incl = Object.keys(tile.properties.cell_props).map(linenum => linenum.parseInt()); // list of included lines in the content
+ let incl = Object.keys(tile.properties.cell_props).map(linenum => parseInt(linenum)) // list of included lines in the content
let cont = chop(tile.content,16);
let read = 0; //line of cont to read
for (let line=0; line<8; line++){
curline = line+8*tilerow;
- if (incl.includes(line)){
- this.data[curline] += cont[read].split('');
- read++;
+ if (conform && incl.includes(line)){
+ for (let i=0; i<16; i++) self.data[curline].push(' ');
} else {
- for (let i=0; i<16; i++) this.data[curline].push(' ');
+ Array.prototype.push.apply(self.data[curline],cont[read].split(''));
+ read++;
}
}
}
+ //console.log(this.data);
}
this.towrite = function(charoffset){ // Does no splitting or anything like that. Just returns a list of triplets for the write function
let writes = [];
for (let line = 0; line < this.data.length; line++) for (let chr = 0; chr< this.data[line].length; chr++){
if (this.data[line][chr] == '') continue;
- writes.push([[Math.floor( (charoffset[0]+line)/8 ),Math.floor( (charoffset[1]+chr)/16 )],[ (charoffset[0]+line) % 8, (charoffset[1]+chr) % 16 ],this.data[line][chr]]);
+ writes.push([[charoffset[0]+line,charoffset[1]+chr],this.data[line][chr]]);
}
return writes;
}
this.tofile = function(filename){
- fs.writeFile(filename,this.data.map(row => replace(row,'','&').join('').replace(/\\|&/g, '\\$&')).join('\n')+'\n',(err)=>{console.log(err);});
+ fs.writeFileSync(filename, this.print());
};
this.fromfile = function(filename){ //Reads an external file into internal data
- fs.readFile(filename, (err, data) => {
- if (err) throw err;
- self.adhoc(data);
- });
+ this.adhoc(fs.readFileSync(filename,'utf8'));
}
this.adhoc = function(text){
- this.data = text.split('\n').map(row => replace( row.replace(/\\(.)/g,'$1').split('') ,'&','' ));
+ text = text.split('\n')
+ text = text.map(row => row.split(''));
+ this.data = text.map(row => {
+ for (let i = 0; i<row.length; i++){
+ if (row[i] == '\\') row.splice(i,1);
+ else if (row[i] == '&') row[i] = '';
+ }
+ return row;
+ });
+ }
+ this.print = function(){
+ return this.data.map(row => replace(replace(replace(row,'&','\\&'),'\\','\\\\'),'','&').join('')).join('\n');
}
this.comb = function(other, func, offset){
- offsety = offset[0];
- offsetx = offset[1];
- for (let row = offset[0]; row<Math.max(other.data.length+offset[0],this.data.length); row++) for (let chr = offset[1]; chr<Math.max(otherlens.data[row].length+offset[1],this.data[row].length); chr++){
- if (row < 0)
- this.data.unshift([]);
+ for (let row = offset[0]; row<Math.max(other.data.length+offset[0],this.data.length); row++){
+ if (row < 0) this.data.unshift([]);
+ if (row >= this.data.length) this.data.push([]);
+ for (let chr = offset[1]; chr<Math.max(zeroifnull(other.data[row],'length')+offset[1], zeroifnull(this.data[row],'length')); chr++){
if (chr < 0)
this.data[row].unshift('');
- if (row > this.data.length)
- this.data.push([]);
- if (chr > this.data[row].length)
+ if (chr >= this.data[row].length)
this.data[row].push('');
- this.data[row][chr] = func(this.data[row][chr], other.data[row][chr]);
- }
+ this.data[row][chr] = func(this.data[row][chr], other.data[row][chr] || '');
+ console.log(row,chr,this.data[row][chr])
+ }}
}
- this.search = function(other){
- loc = [];
- for (let line=0; line<=this.data.length-other.data.length; line++) for (let chr=0; chr<=this.data[y].length-other.data[0].length; chr++){
- match = true;
- for (var y=0; y<other.data.length; y++) for (var x=0; x<other[y].data.length; x++){
+ this.search = function(other){ //Returns first instance of a subspace (prioritized vertically then horizontally)
+ let loc = [];
+ for (let line=0; line<=this.data.length-other.data.length; line++){ for (let chr=0; chr<=this.data[line].length-other.data[0].length; chr++){
+ var match = true;
+ for (let y=0; y<other.data.length; y++) for (let x=0; x<other.data[y].length; x++){
if (this.data[line+y][chr+x] != other.data[y][x]){
match = false;
break;
@@ -93,17 +106,21 @@ function Space(){ // CLASS
loc = [line,chr];
break;
}
- }
+ } if (match) break;}
return loc;
}
this.subsection = function(range){ // range is a standard quadruplet
newspace = new Space();
- for (let line=0; line<range[2]-range[0]; line++) for (let chr=0; chr<range[3]-range[1]; chr++){
- newspace.data[line][chr] = this.data[line+range[0]][line+range[1]];
+ for (let line=0; line<=range[2]-range[0]; line++){
+ newspace.data.push([]);
+ for (let chr=0; chr<=range[3]-range[1]; chr++){
+ newspace.data[line].push(
+ this.data[line+range[0]][chr+range[1]] || ''
+ );
+ }
}
+ return newspace;
}
}
-const comb = require('comb'); // A small set of utilities for Space.comb
-exports.Space = Space;
-exports.comb = comb;
+module.exports = Space