1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
'use strict'
import library from './library.js'
export default function Orca (terminal) {
this.w = 1 // Default Width
this.h = 1 // Default Height
this.f = 0 // Frame
this.s = '' // String
this.terminal = terminal
this.keys = Object.keys(library).slice(0, 36)
this.locks = []
this.runtime = []
this.variables = {}
this.run = function () {
this.runtime = this.parse()
this.operate(this.runtime)
this.f += 1
}
this.reset = function (w = this.w, h = this.h) {
this.f = 0
this.w = w
this.h = h
this.replace(new Array((this.h * this.w) + 1).join('.'))
}
this.load = function (w, h, s, f = 0) {
this.w = w
this.h = h
this.f = f
this.replace(this.clean(s))
return this
}
this.write = function (x, y, g) {
if (!g) { return false }
if (g.length !== 1) { return false }
if (!this.inBounds(x, y)) { return false }
if (this.glyphAt(x, y) === g) { return false }
const index = this.indexAt(x, y)
const glyph = !this.isAllowed(g) ? '.' : g
const string = this.s.substr(0, index) + glyph + this.s.substr(index + 1)
this.replace(string)
return true
}
this.clean = function (str) {
return `${str}`.replace(/\n/g, '').trim().substr(0, this.w * this.h)
}
this.replace = function (s) {
this.s = s
}
// Operators
this.parse = function () {
const a = []
for (let y = 0; y < this.h; y++) {
for (let x = 0; x < this.w; x++) {
const g = this.glyphAt(x, y)
const operator = this.cast(g, x, y)
if (operator) {
a.push(operator)
}
}
}
return a
}
this.cast = function (g, x, y) {
if (g === '.') { return }
if (!library[g.toLowerCase()]) { return }
const passive = g === g.toUpperCase()
return new library[g.toLowerCase()](this, x, y, passive)
}
this.operate = function (operators) {
this.release()
for (const id in operators) {
const operator = operators[id]
if (this.lockAt(operator.x, operator.y)) { continue }
if (operator.passive || operator.hasNeighbor('*')) {
operator.run()
}
}
}
this.bounds = function () {
let w = 0
let h = 0
for (let y = 0; y < this.h; y++) {
for (let x = 0; x < this.w; x++) {
const g = this.glyphAt(x, y)
if (g !== '.') {
if (x > w) { w = x }
if (y > h) { h = y }
}
}
}
return { w: w, h: h }
}
// Locks
this.release = function () {
this.locks = new Array(this.w * this.h)
this.variables = {}
}
this.unlock = function (x, y) {
this.locks[this.indexAt(x, y)] = null
}
this.lock = function (x, y) {
if (this.lockAt(x, y)) { return }
this.locks[this.indexAt(x, y)] = true
}
// Helpers
this.inBounds = function (x, y) {
return Number.isInteger(x) && Number.isInteger(y) && x >= 0 && x < this.w && y >= 0 && y < this.h
}
this.isAllowed = function (g) {
return g === '.' || !!library[`${g}`.toLowerCase()]
}
this.keyOf = function (val, uc = false) {
return uc === true ? this.keys[val % 36].toUpperCase() : this.keys[val % 36]
}
this.valueOf = function (g) {
return g === '.' ? 0 : this.keys.indexOf(`${g}`.toLowerCase())
}
this.indexAt = function (x, y) {
return this.inBounds(x, y) === true ? x + (this.w * y) : -1
}
this.operatorAt = function (x, y) {
return this.runtime.filter((item) => { return item.x === x && item.y === y })[0]
}
this.posAt = function (index) {
return { x: index % this.w, y: parseInt(index / this.w) }
}
this.glyphAt = function (x, y) {
return this.s.charAt(this.indexAt(x, y))
}
this.valueAt = function (x, y) {
return this.valueOf(this.glyphAt(x, y))
}
this.lockAt = function (x, y) {
return this.locks[this.indexAt(x, y)] === true
}
this.valueIn = function (key) {
return this.variables[key]
}
// Tools
this.format = function () {
const a = []
for (let y = 0; y < this.h; y++) {
a.push(this.s.substr(y * this.w, this.w))
}
return a.reduce((acc, val) => {
return `${acc}${val}\n`
}, '')
}
this.length = function () {
return this.strip().length
}
this.strip = function () {
return this.s.replace(/[^a-zA-Z0-9+]+/gi, '').trim()
}
this.toString = function () {
return this.format().trim()
}
this.reset()
}
|