aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDevine Lu Linvega <[email protected]>2018-10-19 11:22:52 +1200
committerDevine Lu Linvega <[email protected]>2018-10-19 11:22:52 +1200
commit3aeb3cf9acd49d595f54eed6a42501150a7d4634 (patch)
tree3266c8418a709e5c74d02476748c1d2dd96886d1
parentadd55227a3bbd11d8850c05541cd04da5ab36641 (diff)
downloadOrca-3aeb3cf9acd49d595f54eed6a42501150a7d4634.tar.gz
Orca-3aeb3cf9acd49d595f54eed6a42501150a7d4634.zip
Optimized logic fns
-rw-r--r--desktop/core/lib/_base.js12
-rw-r--r--desktop/core/lib/a.js13
-rw-r--r--desktop/core/lib/e.js2
-rw-r--r--desktop/core/lib/f.js10
-rw-r--r--desktop/core/lib/m.js9
-rw-r--r--desktop/core/lib/n.js2
-rw-r--r--desktop/core/lib/s.js2
-rw-r--r--desktop/core/lib/w.js2
-rw-r--r--desktop/core/lib/y.js1
-rw-r--r--desktop/core/lib/z.js2
-rw-r--r--desktop/core/pico.js2
11 files changed, 30 insertions, 27 deletions
diff --git a/desktop/core/lib/_base.js b/desktop/core/lib/_base.js
index 338769a..a225002 100644
--- a/desktop/core/lib/_base.js
+++ b/desktop/core/lib/_base.js
@@ -49,7 +49,7 @@ function FnBase (pico, x, y, glyph = '.', passive = false) {
pico.add((this.x + x) % pico.w, (this.y + y) % pico.h, this.glyph)
}
- this.is_free = function (x, y) {
+ this.isFree = function (x, y) {
if (this.x + x >= pico.w) { return false }
if (this.x + x <= -1) { return false }
if (this.y + y >= pico.h) { return false }
@@ -59,6 +59,14 @@ function FnBase (pico, x, y, glyph = '.', passive = false) {
return target === '.' || target === '*' ? true : target
}
+ this.toValue = function (g = '0') {
+ return g ? clamp(pico.allowed.indexOf(g.toLowerCase()), 0, pico.allowed.length) : 0
+ }
+
+ this.toChar = function (index = 0) {
+ return index && pico.allowed[index] ? pico.allowed[index] : '0'
+ }
+
this.neighbor_by = function (x, y) {
return pico.glyphAt(this.x + x, this.y + y) !== '.' ? { x: this.x + x, y: this.y + y, glyph: pico.glyphAt(this.x + x, this.y + y) } : null
}
@@ -109,6 +117,8 @@ function FnBase (pico, x, y, glyph = '.', passive = false) {
this.docs = function () {
return `${this.name}: ${this.info}`
}
+
+ function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
}
module.exports = FnBase
diff --git a/desktop/core/lib/a.js b/desktop/core/lib/a.js
index c6b625b..cf49c8a 100644
--- a/desktop/core/lib/a.js
+++ b/desktop/core/lib/a.js
@@ -22,15 +22,12 @@ function FnA (pico, x, y, passive) {
this.operation = function () {
const w = this.west()
const e = this.east()
- const west = !w ? '0' : w.glyph
- const east = !e ? '0' : e.glyph
- const index = (this.convert(west) + this.convert(east)) % pico.allowed.length
- const output = pico.allowed[index]
- pico.add(this.x, this.y + 1, output)
- }
- this.convert = function (glyph) {
- return pico.allowed.indexOf(glyph)
+ const val1 = this.toValue(w ? w.glyph : null)
+ const val2 = this.toValue(e ? e.glyph : null)
+ const res = this.toChar(val1 + val2)
+
+ pico.add(this.x, this.y + 1, res)
}
}
diff --git a/desktop/core/lib/e.js b/desktop/core/lib/e.js
index 7d6475e..b356cc9 100644
--- a/desktop/core/lib/e.js
+++ b/desktop/core/lib/e.js
@@ -10,7 +10,7 @@ function FnE (pico, x, y, passive) {
this.info = 'Moves eastward, or bangs.'
this.haste = function () {
- if (this.is_free(1, 0) !== true) { this.replace('*'); this.lock(); return }
+ if (this.isFree(1, 0) !== true) { this.replace('*'); this.lock(); return }
this.move(1, 0)
}
}
diff --git a/desktop/core/lib/f.js b/desktop/core/lib/f.js
index 1095282..f436de3 100644
--- a/desktop/core/lib/f.js
+++ b/desktop/core/lib/f.js
@@ -21,14 +21,10 @@ function FnF (pico, x, y, passive) {
this.operation = function () {
const w = this.west()
const e = this.east()
- const west = !w ? '.' : w.glyph
- const east = !e ? '.' : e.glyph
+ const val1 = this.toValue(w ? w.glyph : null)
+ const val2 = this.toValue(e ? e.glyph : null)
- if (west === east) {
- pico.add(this.x, this.y + 1, '1')
- } else {
- pico.add(this.x, this.y + 1, '0')
- }
+ pico.add(this.x, this.y + 1, val1 === val2 ? '1' : '0')
}
}
diff --git a/desktop/core/lib/m.js b/desktop/core/lib/m.js
index d44023c..5248f5f 100644
--- a/desktop/core/lib/m.js
+++ b/desktop/core/lib/m.js
@@ -21,13 +21,12 @@ function FnM (pico, x, y, passive) {
this.operation = function () {
const w = this.west()
const e = this.east()
- const west = !w ? '0' : w.glyph
- const east = !e ? '0' : e.glyph
- const val = pico.allowed.indexOf(west)
- const mod = pico.allowed.indexOf(east) !== 0 ? pico.allowed.indexOf(east) : '1'
+ const val = this.toValue(w ? w.glyph : null)
+ const mod = this.toValue(e ? e.glyph : null)
+ const res = this.toChar(val % (mod !== 0 ? mod : 1))
- pico.add(this.x, this.y + 1, `${parseInt(val) % parseInt(mod)}`)
+ pico.add(this.x, this.y + 1, res)
}
}
diff --git a/desktop/core/lib/n.js b/desktop/core/lib/n.js
index 05fbe90..46b3974 100644
--- a/desktop/core/lib/n.js
+++ b/desktop/core/lib/n.js
@@ -10,7 +10,7 @@ function FnN (pico, x, y, passive) {
this.info = 'Moves Northward, or bangs.'
this.haste = function () {
- if (this.is_free(0, -1) !== true) { this.replace('*'); this.lock(); return }
+ if (this.isFree(0, -1) !== true) { this.replace('*'); this.lock(); return }
this.move(0, -1)
}
}
diff --git a/desktop/core/lib/s.js b/desktop/core/lib/s.js
index 6828dc3..d68f121 100644
--- a/desktop/core/lib/s.js
+++ b/desktop/core/lib/s.js
@@ -10,7 +10,7 @@ function FnS (pico, x, y, passive) {
this.info = 'Moves southward, or bangs.'
this.haste = function () {
- if (this.is_free(0, 1) !== true) { this.replace('*'); return }
+ if (this.isFree(0, 1) !== true) { this.replace('*'); return }
this.move(0, 1)
}
}
diff --git a/desktop/core/lib/w.js b/desktop/core/lib/w.js
index 0ee27a2..8526250 100644
--- a/desktop/core/lib/w.js
+++ b/desktop/core/lib/w.js
@@ -10,7 +10,7 @@ function FnW (pico, x, y, passive) {
this.info = 'Moves westward, or bangs.'
this.haste = function () {
- if (this.is_free(-1, 0) !== true) { this.replace('*'); this.lock(); return }
+ if (this.isFree(-1, 0) !== true) { this.replace('*'); this.lock(); return }
this.move(-1, 0)
}
}
diff --git a/desktop/core/lib/y.js b/desktop/core/lib/y.js
index fce8f3e..c311aee 100644
--- a/desktop/core/lib/y.js
+++ b/desktop/core/lib/y.js
@@ -21,6 +21,7 @@ function FnY (pico, x, y, passive) {
this.operation = function () {
const w = this.west()
const e = this.east()
+
if (!w && !this.east()) {
pico.add(this.x, this.y + 1, '1')
} else if ((w && !e) || (e && !w)) {
diff --git a/desktop/core/lib/z.js b/desktop/core/lib/z.js
index eff7c1c..aacbb25 100644
--- a/desktop/core/lib/z.js
+++ b/desktop/core/lib/z.js
@@ -12,7 +12,7 @@ function FnZ (pico, x, y, passive) {
const positions = [{ x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 0, y: -1 }]
const position = positions[pico.f % 4]
- if (this.is_free(position.x, position.y) === true) {
+ if (this.isFree(position.x, position.y) === true) {
this.move(position.x, position.y)
}
}
diff --git a/desktop/core/pico.js b/desktop/core/pico.js
index 918070d..cfbffdd 100644
--- a/desktop/core/pico.js
+++ b/desktop/core/pico.js
@@ -102,7 +102,7 @@ function Pico (w, h) {
this.add = function (x, y, ch) {
const glyph = ch.substr(0, 1)
- if (!this.isAllowed(glyph)) { this.terminal.log(`[${glyph}] is not allowed`); return }
+ if (!this.isAllowed(glyph)) { this.terminal.log(`[${glyph},${ch}] is not allowed`); return }
if (!this.inBounds(x, y)) { this.terminal.log(`[${glyph}] is out of range`); return }
const index = this.indexAt(x, y)
this.s = this.s.substr(0, index) + glyph + this.s.substr(index + glyph.length)