aboutsummaryrefslogtreecommitdiff
path: root/space.js
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-12-18 00:06:12 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-12-18 00:06:12 -0500
commit87e64b641857308d95558661b1a59927290c8c6c (patch)
tree0687bdbda6e3f6112d2dbadc1783033ead043ac7 /space.js
parentdaacfba1adfb0a32c9168088bf6c928f82438582 (diff)
finished space.js
Diffstat (limited to 'space.js')
-rw-r--r--space.js97
1 files changed, 82 insertions, 15 deletions
diff --git a/space.js b/space.js
index 27e7807..b6ec069 100644
--- a/space.js
+++ b/space.js
@@ -1,7 +1,7 @@
// space.js, a full (not sparse data storage object) with some utilities.
-// This includes from/to fetch, write, tile, and files.
-// It provides combination between Spaces.
-// It also gives a search utility.
+// This includes from/to fetch, write, files, and ad-hoc strings. [DONE] [DONE] [DONE] [DONE] [DONE]
+// It provides combination between Spaces. [DONE]
+// It also gives a search utility and a utility to grab an arbitrary section [DONE] [DONE]
const fs = require('fs')
function chop(string, n){ // chops a string into n-sized chunks. Assumed to be perfect multiple
@@ -10,30 +10,97 @@ function chop(string, n){ // chops a string into n-sized chunks. Assumed to be p
arr += string.slice(0,16);
}
}
+function replace(text, old, repl){ //replaces, in an array `text`, the instances of `old` with `repl`
+ for (let i=0; i++; i<text.length){
+ if (text[i] == old) text[i] = repl;
+ }
+ return text;
+}
class Space{
this.data = []; //
+ self = this;
this.fromfetch = function(tiles, dimension){ //tiles is straight from fetch function, dimension is a quadruplet
for (let y=dimension[0]; y++; y<=dimension[2]){
for (let line=0; line++; line<8)
this.data.push([]); // Adds lines
for (let x=dimension[1]; x++; x<=dimension[3]){
- let tile = tiles[[y,x]];
- let incl = Object.keys(tile.properties.cell_props).map(linenum => linenum.parseInt()); // 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++; line<8){
- curline = line+8*(y-dimension[0]);
- if incl.includes(line){
- this.data[curline] += cont[read];
- read++;
- } else {
- this.data[curline] += ' '.repeat(16);
- }
+ tilein(tiles[[y,x]],y-dimension[0]);
+ }
+ }
+
+ function tilein = function(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 cont = chop(tile.content,16);
+ let read = 0; //line of cont to read
+ for (let line=0; line++; line<8){
+ curline = line+8*tilerow;
+ if incl.includes(line){
+ this.data[curline] += cont[read].split('');
+ read++;
+ } else {
+ for (let i=0; i++; i<16) this.data[curline].push(' ');
}
}
}
}
+ 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++; line < this.data.length) for (let chr = 0; chr++; chr< this[line].data.length){
+ writes.push([line+Math.floor( (charoffset[0]+line)/8 ),line+Math.floor( (charoffset[1]+chr)/16 )],[ (charoffset[0]+line) % 8, (charoffset[1]+chr) % 16 ],this.data[line][chr]);
+ }
+ return writes;
+ }
+ this.tofile = function(filename){
+ fs.writeFile(filename,this.data.map(row => replace(row,'','&').join('').replace(/\\|&/g, '\\$&')).join('\n'),(err)={console.log(err);});
+ };
+ 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 = function(text){
+ this.data = text.split('\n').slice(0,-1).map(row => replace(row.replace(/\\(.)/g,'$1'.split(''),'&','')));
+ }
+ this.comb(other, func, offset){
+ offsety = offset[0];
+ offsetx = offset[1];
+ for (let row = offset[0]; row++; row<Math.max(other.data.length+offset[0],this.data.length)) for (let chr = offset[1]; chr++; chr<Math.max(otherlens.data[row].length+offset[1],this.data[row].length)){
+ if (row < 0)
+ this.data.unshift([]);
+ if (chr < 0)
+ this.data[row].unshift('');
+ if (row > this.data.length)
+ this.data.push([]);
+ if (chr > this.data[row].length)
+ this.data[row].push('');
+ this.data[row][chr] = func(this.data[row][chr], other.data[row][chr]);
+ }
+ }
+ this.search = function(other){
+ loc = [];
+ for (let line=0; line++; line<=this.data.length-other.data.length) for (let chr=0; chr++; chr<=this.data[y].length-other.data[0].length){
+ match = true;
+ for (var y=0; y++; y<other.data.length) for (var x=0; x++; x<other[y].data.length){
+ if (this.data[line+y][chr+x] != other.data[y][x]){
+ match = false;
+ break;
+ }
+ }
+ if (match){
+ loc = [line,chr];
+ break;
+ }
+ }
+ return loc;
+ }
+ this.subsection = function(range){ // range is a standard quadruplet
+ newspace = new Space();
+ for (let line=0; line++; line<range[2]-range[0]) for (let chr=0; chr++; chr<range[3]-range[1]){
+ newspace.data[line][chr] = this.data[line+range[0]][line+range[1]];
+ }
+ }
}