diff options
author | Devine Lu Linvega <[email protected]> | 2018-10-14 09:15:41 +1200 |
---|---|---|
committer | Devine Lu Linvega <[email protected]> | 2018-10-14 09:15:41 +1200 |
commit | b31bdab4f00eb424bf6c160feb58199b56dcf0f9 (patch) | |
tree | 85eadc795e010c37c02eb5fe53063610485ab6ea | |
parent | 1064cd2ef4eb006bb3479851a35964935eeacd92 (diff) | |
download | Orca-b31bdab4f00eb424bf6c160feb58199b56dcf0f9.tar.gz Orca-b31bdab4f00eb424bf6c160feb58199b56dcf0f9.zip |
Added file load
-rw-r--r-- | cli/terminal.js | 33 | ||||
-rw-r--r-- | core/lib.js | 1 | ||||
-rw-r--r-- | core/lib/_base.js | 10 | ||||
-rw-r--r-- | core/lib/_move.js | 9 | ||||
-rw-r--r-- | core/lib/_wirev.js | 12 | ||||
-rw-r--r-- | core/lib/a.js | 6 | ||||
-rw-r--r-- | core/lib/c.js | 4 | ||||
-rw-r--r-- | core/lib/d.js | 8 | ||||
-rw-r--r-- | core/lib/e.js | 31 | ||||
-rw-r--r-- | core/lib/f.js | 4 | ||||
-rw-r--r-- | core/lib/i.js | 4 | ||||
-rw-r--r-- | core/lib/j.js | 8 | ||||
-rw-r--r-- | core/lib/m.js | 6 | ||||
-rw-r--r-- | core/lib/n.js | 25 | ||||
-rw-r--r-- | core/lib/s.js | 4 | ||||
-rw-r--r-- | core/lib/t.js | 2 | ||||
-rw-r--r-- | core/lib/w.js | 4 | ||||
-rw-r--r-- | core/lib/x.js | 4 | ||||
-rw-r--r-- | core/lib/y.js | 8 | ||||
-rw-r--r-- | core/pico.js | 11 | ||||
-rw-r--r-- | examples/blank.pico | 20 | ||||
-rw-r--r-- | examples/wires.pico | 20 | ||||
-rw-r--r-- | index.js | 4 |
23 files changed, 191 insertions, 47 deletions
diff --git a/cli/terminal.js b/cli/terminal.js index 70c903f..0ba6df9 100644 --- a/cli/terminal.js +++ b/cli/terminal.js @@ -8,8 +8,9 @@ function Terminal(pico) this._screen = blessed.screen(); this._grid = blessed.box({ top: 1, left: 2, height: '100%-3', width: pico.w, keys: true, mouse: true, style: { fg: '#efefef' } }); - this._output = blessed.box({ bottom: 2, left: 2, height: 1, width: '100%-2', style: { fg: '#fff' } }); - this._inspector = blessed.box({ bottom: 1, left: 2, height: 1, width: '100%-4', style: { fg: '#efefef' } }); + this._output = blessed.box({ bottom: 4, left: 2, height: 1, width: '100%-2', style: { fg: '#fff' } }); + this._inspector = blessed.box({ bottom: 2, left: 2, height: 1, width: '100%-4', style: { fg: '#efefef' } }); + this._log = blessed.box({ bottom: 3, left: 2, height: 1, width: '100%-4', style: { fg: '#efefef' } }); this.is_paused = false @@ -34,9 +35,10 @@ function Terminal(pico) this._screen.append(this._grid); this._screen.append(this._output); this._screen.append(this._inspector); + this._screen.append(this._log); } - this.start = function() + this.start = function(path) { this.pico.start() this._screen.key(['escape', 'q', 'C-c'], (ch, key) => (process.exit(0))); @@ -52,8 +54,12 @@ function Terminal(pico) this.cursor.insert(g) }); + if(path){ + this.load(path) + } + this.update() - setInterval(() => { this.run() }, 200) + setInterval(() => { this.run() }, 1200) } this.run = function() @@ -69,6 +75,25 @@ function Terminal(pico) this.is_paused = !this.is_paused } + this.load = function(path) + { + const terminal = this + var fs = require('fs') + fs.readFile(path, 'utf8', function(err, data) { + if (err) throw err; + const w = data.split("\n")[0].length + const h = data.split("\n").length + terminal.log(`Loaded: ${path} ${w}x${h}`) + pico.load(w,h,data) + }); + } + + this.log = function(msg) + { + this._log.setContent(msg) + this.update() + } + this.add_cursor = function(s) { const index = this.pico.index_at(this.cursor.x, this.cursor.y) diff --git a/core/lib.js b/core/lib.js index 682ffe1..6b40963 100644 --- a/core/lib.js +++ b/core/lib.js @@ -45,5 +45,6 @@ module.exports = { '.': require('./lib/_null'), ':': require('./lib/_port'), '-': require('./lib/_wireh'), + '|': require('./lib/_wirev'), } } diff --git a/core/lib/_base.js b/core/lib/_base.js index 916cd98..aefbc65 100644 --- a/core/lib/_base.js +++ b/core/lib/_base.js @@ -55,7 +55,7 @@ function FnBase (pico, x, y) { } this.neighbors = function (g) { - return [this.up(g), this.right(g), this.down(g), this.left(g)].filter(function (e) { return e }) + return [this.north(g), this.east(g), this.south(g), this.west(g)].filter(function (e) { return e }) } this.free_neighbors = function () { @@ -78,23 +78,23 @@ function FnBase (pico, x, y) { return false } - this.left = function (target = null) { + this.west = function (target = null) { const g = pico.glyph_at(this.x - 1, this.y) return g != '.' && (g == target || !target) ? { x: this.x - 1, y: this.y, glyph: g } : null } - this.right = function (target) { + this.east = function (target) { const g = pico.glyph_at(this.x + 1, this.y) return g != '.' && (g == target || !target) ? { x: this.x + 1, y: this.y, glyph: g } : null } - this.up = function (target) { + this.north = function (target) { const g = pico.glyph_at(this.x, this.y - 1) return g != '.' && (g == target || !target) ? { x: this.x, y: this.y - 1, glyph: g } : null } - this.down = function (target) { + this.south = function (target) { const g = pico.glyph_at(this.x, this.y + 1) return g != '.' && (g == target || !target) ? { x: this.x, y: this.y + 1, glyph: g } : null } diff --git a/core/lib/_move.js b/core/lib/_move.js new file mode 100644 index 0000000..fb74aef --- /dev/null +++ b/core/lib/_move.js @@ -0,0 +1,9 @@ +'use strict' + +const FnBase = require('./_base') + +function FnMove (pico, x, y) { + FnBase.call(this, pico, x, y) +} + +module.exports = FnMove diff --git a/core/lib/_wirev.js b/core/lib/_wirev.js new file mode 100644 index 0000000..2e2f089 --- /dev/null +++ b/core/lib/_wirev.js @@ -0,0 +1,12 @@ +'use strict' + +const FnBase = require('./_base') + +function FnWireH (pico, x, y) { + FnBase.call(this, pico, x, y) + + this.name = 'wire-v' + this.glyph = '|' +} + +module.exports = FnWireH diff --git a/core/lib/a.js b/core/lib/a.js index 4bef241..a1a7d95 100644 --- a/core/lib/a.js +++ b/core/lib/a.js @@ -11,13 +11,13 @@ function FnA (pico, x, y) { this.ports = [{ x: -1, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1, output: true }] this.operation = function () { - if (!this.left() || !this.right()) { + if (!this.west() || !this.east()) { pico.remove(this.x, this.y + 1) return } - const left = !this.left() ? '0' : this.left().glyph - const right = !this.right() ? '0' : this.right().glyph + const left = !this.west() ? '0' : this.west().glyph + const right = !this.east() ? '0' : this.east().glyph const index = (this.convert(left) + this.convert(right)) % pico.allowed.length const output = pico.allowed[index] diff --git a/core/lib/c.js b/core/lib/c.js index d8ca5a5..548b73e 100644 --- a/core/lib/c.js +++ b/core/lib/c.js @@ -10,8 +10,8 @@ function FnC (pico, x, y) { this.ports = [{ x: 0, y: 0, bang: true }, { x: 1, y: 0, output: true }, { x: -1, y: 0 }] this.operation = function () { - if (this.bang() && this.left()) { - pico.add(this.x + 1, this.y, this.left().glyph) + if (this.bang() && this.west()) { + pico.add(this.x + 1, this.y, this.west().glyph) } } } diff --git a/core/lib/d.js b/core/lib/d.js index 03da5be..676b96a 100644 --- a/core/lib/d.js +++ b/core/lib/d.js @@ -11,19 +11,19 @@ function FnD (pico, x, y) { this.ports = [{ x: 0, y: 1 }, { x: 0, y: -1 }, { x: 1, y: 0 }, { x: -1, y: 0 }] this.operation = function () { - if (this.up() && this.up().glyph != 'n') { + if (this.north() && this.north().glyph != 'n') { pico.add(this.x, this.y - 1, 'n') pico.lock(this.x, this.y - 1) } - if (this.down() && this.down().glyph != 'd') { + if (this.south() && this.south().glyph != 'd') { pico.add(this.x, this.y + 1, 'd') pico.lock(this.x, this.y + 1) } - if (this.left() && this.left().glyph != 'w') { + if (this.west() && this.west().glyph != 'w') { pico.add(this.x - 1, this.y, 'w') pico.lock(this.x - 1, this.y) } - if (this.right() && this.right().glyph != 'e') { + if (this.east() && this.east().glyph != 'e') { pico.add(this.x + 1, this.y, 'e') pico.lock(this.x + 1, this.y) } diff --git a/core/lib/e.js b/core/lib/e.js index 8901d48..8c457ae 100644 --- a/core/lib/e.js +++ b/core/lib/e.js @@ -1,18 +1,43 @@ 'use strict' -const FnBase = require('./_base') +const FnMove = require('./_move') function FnE (pico, x, y) { - FnBase.call(this, pico, x, y) + FnMove.call(this, pico, x, y) this.name = 'east' - this.glyph = 'r' + this.glyph = 'e' this.info = 'Moves eastward, or bangs.' this.operation = function () { + const wire = this.signal() + if(wire){ return; } + if (this.is_free(1, 0) != true) { this.replace('b'); this.lock(); return } this.move(1, 0) } + + this.signal = function() + { + const e = this.east() + const w = this.west() + const n = this.north() + const s = this.south() + + // Along the wire + if(e && e.glyph == "-" && w && w.glyph == "-"){ + pico.add(this.x,this.y,'-') + pico.add(this.x+1,this.y,'e') + pico.lock(this.x+1,this.y) + return true + } + if(n && n.glyph == "|" && s && s.glyph == "|"){ + pico.add(this.x,this.y,'|') + pico.add(this.x,this.y-1,'n') + return true + } + return false + } } module.exports = FnE diff --git a/core/lib/f.js b/core/lib/f.js index 664ac37..d3b1ce8 100644 --- a/core/lib/f.js +++ b/core/lib/f.js @@ -11,9 +11,9 @@ function FnF (pico, x, y) { this.ports = [{ x: -1, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1, output: true }] this.operation = function () { - if (!this.left() || !this.right()) { return } + if (!this.west() || !this.east()) { return } - if (this.left(this.right().glyph)) { + if (this.west(this.east().glyph)) { pico.add(this.x, this.y + 1, '1') } else { pico.add(this.x, this.y + 1, '0') diff --git a/core/lib/i.js b/core/lib/i.js index d5b0bd0..f7dd444 100644 --- a/core/lib/i.js +++ b/core/lib/i.js @@ -12,9 +12,9 @@ function FnI (pico, x, y) { this.operation = function () { if (!this.bang()) { return } - if (!this.down()) { return } + if (!this.south()) { return } - const n = this.down() + const n = this.south() pico.add(this.x, this.y + 1, this.inc(n.glyph)) } diff --git a/core/lib/j.js b/core/lib/j.js index cb997b8..75a63b2 100644 --- a/core/lib/j.js +++ b/core/lib/j.js @@ -14,13 +14,13 @@ function FnJ (pico, x, y) { this.operation = function () { if (!this.bang()) { return } - if (this.left()) { - pico.add(this.x + 1, this.y, this.left().glyph) + if (this.west()) { + pico.add(this.x + 1, this.y, this.west().glyph) pico.remove(this.x - 1, this.y) pico.lock(this.x - 1, this.y) pico.lock(this.x + 1, this.y) - } else if (this.right()) { - pico.add(this.x - 1, this.y, this.right().glyph) + } else if (this.east()) { + pico.add(this.x - 1, this.y, this.east().glyph) pico.remove(this.x + 1, this.y) pico.lock(this.x - 1, this.y) pico.lock(this.x + 1, this.y) diff --git a/core/lib/m.js b/core/lib/m.js index b769562..91a7417 100644 --- a/core/lib/m.js +++ b/core/lib/m.js @@ -11,10 +11,10 @@ function FnM (pico, x, y) { this.ports = [{ x: -1, y: 0 }, { x: 1, y: 0 }, { x: 0, y: 1, output: true }] this.operation = function () { - if (!this.left() || !this.right()) { return } + if (!this.west() || !this.east()) { return } - const val = pico.glyphs.indexOf(this.left().glyph) - const mod = pico.glyphs.indexOf(this.right().glyph) + const val = pico.glyphs.indexOf(this.west().glyph) + const mod = pico.glyphs.indexOf(this.east().glyph) if (mod == 0) { return } diff --git a/core/lib/n.js b/core/lib/n.js index 517e98c..c9185e3 100644 --- a/core/lib/n.js +++ b/core/lib/n.js @@ -1,18 +1,39 @@ 'use strict' -const FnBase = require('./_base') +const FnMove = require('./_move') function FnN (pico, x, y) { - FnBase.call(this, pico, x, y) + FnMove.call(this, pico, x, y) this.name = 'north' this.glyph = 'n' this.info = 'Moves Northward, or bangs.' this.operation = function () { + const wire = this.signal() + if(wire){ return; } + if (this.is_free(0, -1) != true) { this.replace('b'); this.lock(); return } this.move(0, -1) } + + this.signal = function() + { + const e = this.east() + const w = this.west() + const n = this.north() + const s = this.south() + + // Along the wire + if(n && n.glyph == "|" && s && s.glyph == "|"){ + pico.add(this.x,this.y,'|') + pico.add(this.x,this.y-1,'n') + pico.lock(this.x,this.y-1) + return true + } + + return false + } } module.exports = FnN diff --git a/core/lib/s.js b/core/lib/s.js index 684a1d5..cfe3000 100644 --- a/core/lib/s.js +++ b/core/lib/s.js @@ -1,9 +1,9 @@ 'use strict' -const FnBase = require('./_base') +const FnMove = require('./_move') function FnS (pico, x, y) { - FnBase.call(this, pico, x, y) + FnMove.call(this, pico, x, y) this.name = 'south' this.glyph = 's' diff --git a/core/lib/t.js b/core/lib/t.js index fdac934..f0ba99f 100644 --- a/core/lib/t.js +++ b/core/lib/t.js @@ -11,7 +11,7 @@ function FnT (pico, x, y) { this.ports = [{ x: -1, y: 0 }, { x: 0, y: 1, output: true }] this.operation = function () { - if (this.left('1') || this.left('r') || this.left('l') || this.left('u') || this.left('d') || this.left('b') || this.left('z')) { + if (this.west('1') || this.west('r') || this.west('l') || this.west('u') || this.west('d') || this.west('b') || this.west('z')) { this.fire() } } diff --git a/core/lib/w.js b/core/lib/w.js index 75febea..c0d8a93 100644 --- a/core/lib/w.js +++ b/core/lib/w.js @@ -1,9 +1,9 @@ 'use strict' -const FnBase = require('./_base') +const FnMove = require('./_move') function FnW (pico, x, y) { - FnBase.call(this, pico, x, y) + FnMove.call(this, pico, x, y) this.name = 'west' this.glyph = 'w' diff --git a/core/lib/x.js b/core/lib/x.js index e9b1878..e3adc41 100644 --- a/core/lib/x.js +++ b/core/lib/x.js @@ -11,10 +11,10 @@ function FnX (pico, x, y) { this.ports = [{ x: -1, y: 0 }, { x: 0, y: 1, output: true }, { x: 1, y: 0, output: true }] this.operation = function () { - if (this.left('0')) { + if (this.west('0')) { this.fire(1, 0) } - if (this.left('1')) { + if (this.west('1')) { this.fire(0, 1) } } diff --git a/core/lib/y.js b/core/lib/y.js index 21d4315..1009911 100644 --- a/core/lib/y.js +++ b/core/lib/y.js @@ -11,13 +11,13 @@ function FnY (pico, x, y) { this.ports = [{ x: -1, y: 0, input: true }, { x: 1, y: 0, input: true }, { x: 0, y: 1, output: true }] this.operation = function () { - if (!this.left() && !this.right()) { + if (!this.west() && !this.east()) { pico.add(this.x, this.y + 1, '1') - } else if ((this.left() && !this.right()) || (this.right() && !this.left())) { + } else if ((this.west() && !this.east()) || (this.east() && !this.west())) { pico.add(this.x, this.y + 1, '0') - } else if ((this.left() && !this.right()) || (this.right() && !this.left())) { + } else if ((this.west() && !this.east()) || (this.east() && !this.west())) { pico.add(this.x, this.y + 1, '0') - } else if (is_num(this.left().glyph) == is_num(this.right().glyph)) { + } else if (is_num(this.west().glyph) == is_num(this.east().glyph)) { pico.add(this.x, this.y + 1, '1') } else { pico.add(this.x, this.y + 1, '0') diff --git a/core/pico.js b/core/pico.js index 478587a..2109f33 100644 --- a/core/pico.js +++ b/core/pico.js @@ -1,6 +1,7 @@ 'use strict' function Pico (w, h) { + this.f = 0 // Frame this.w = w // Width this.h = h // Height @@ -22,7 +23,7 @@ function Pico (w, h) { return h } - this.start = function () { + this.start = function (path) { this.lib = require('./lib') this.allowed = [].concat(Object.keys(this.lib.num)).concat(Object.keys(this.lib.alpha)).concat(Object.keys(this.lib.special)) this.docs = make_docs(Object.values(this.lib.alpha)) @@ -89,6 +90,14 @@ function Pico (w, h) { this.r = '' } + this.load = function(w,h,s) + { + this.w = w // Width + this.h = h // Height + this.reset() + this.s = s.replace(/\n/g,'').trim() // String + } + this.is_prog = function (g) { return this.lib.alpha[g] } diff --git a/examples/blank.pico b/examples/blank.pico new file mode 100644 index 0000000..afa2dba --- /dev/null +++ b/examples/blank.pico @@ -0,0 +1,20 @@ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................
\ No newline at end of file diff --git a/examples/wires.pico b/examples/wires.pico new file mode 100644 index 0000000..aead57a --- /dev/null +++ b/examples/wires.pico @@ -0,0 +1,20 @@ +........................................ +........................................ +........................................ +........................................ +........................................ +........................................ +..................---w---............... +........................................ +..................---e---............... +........................................ +........................................ +.............|..|....................... +.............|..|....................... +.............n..s....................... +.............|..|....................... +.............|..|....................... +........................................ +........................................ +........................................ +........................................
\ No newline at end of file @@ -4,5 +4,7 @@ const Terminal = require('./cli/terminal') const pico = new Pico(40, 20) const terminal = new Terminal(pico) +const file = process.argv[2] + terminal.install() -terminal.start()
\ No newline at end of file +terminal.start(file)
\ No newline at end of file |