aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/core/operator.js
blob: dc8e79df0ce46ac614554ce8b14211a884702d42 (plain)
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
'use strict'

function Operator (orca, x, y, glyph = '.', passive = false) {
  this.name = 'unknown'
  this.x = x
  this.y = y
  this.passive = passive
  this.draw = true
  this.glyph = passive ? glyph.toUpperCase() : glyph
  this.info = '--'
  this.ports = { input: {}, haste: {}, bang: !passive }

  // Actions

  this.listen = function (port, toValue = false, min = 0, max = 36) {
    if (!port) { return toValue ? 0 : '.' }
    const g = orca.glyphAt(this.x + port.x, this.y + port.y)
    return toValue ? clamp(orca.valueOf(g), min, max) : g
  }

  this.output = function (g, lock = false, casesensitive = false) {
    if (!this.ports.output || !g) { return }
    const uppercase = casesensitive === true ? this.getCase() : false
    const glyph = uppercase === true ? `${g}`.toUpperCase() : g
    orca.write(this.x + this.ports.output.x, this.y + this.ports.output.y, glyph)
    if (lock) {
      orca.lock(this.x + this.ports.output.x, this.y + this.ports.output.y)
    }
  }

  // Phases

  this.permissions = function () {
    for (const id in this.ports.haste) {
      const port = this.ports.haste[id]
      if (!port.unlock) {
        orca.lock(this.x + port.x, this.y + port.y)
      }
    }
    for (const id in this.ports.input) {
      const port = this.ports.input[id]
      if (!port.unlock) {
        orca.lock(this.x + port.x, this.y + port.y)
      }
    }
    if (this.ports.output && !this.ports.output.unlock) {
      orca.lock(this.x + this.ports.output.x, this.y + this.ports.output.y)
    }
  }

  this.haste = function () {
  }

  this.run = function () {
  }

  // Helpers

  this.lock = function () {
    orca.lock(this.x, this.y)
  }

  this.replace = function (g) {
    orca.write(this.x, this.y, g)
  }

  this.erase = function () {
    this.replace('.')
  }

  this.explode = function () {
    this.replace('*')
    this.lock()
  }

  this.move = function (x, y) {
    const offset = { x: this.x + x, y: this.y + y }
    if (!orca.inBounds(offset.x, offset.y)) { this.explode(); return }
    const collider = orca.glyphAt(offset.x, offset.y)
    if (collider === this.glyph) { return }
    if (collider !== '*' && collider !== '.' && collider !== this.glyph) { this.explode(); return }
    this.erase()
    this.x += x
    this.y += y
    this.replace(this.glyph)
    this.lock()
  }

  this.bang = function () {
    if (orca.glyphAt(this.x + 1, this.y) === '*') { return { x: 1, y: 0 } }
    if (orca.glyphAt(this.x - 1, this.y) === '*') { return { x: -1, y: 0 } }
    if (orca.glyphAt(this.x, this.y + 1) === '*') { return { x: 0, y: 1 } }
    if (orca.glyphAt(this.x, this.y - 1) === '*') { return { x: 0, y: -1 } }
    return false
  }

  // Docs

  this.getPorts = function () {
    const a = []
    const TYPE = { operator: 0, haste: 1, input: 2, output: 3 }
    a.push([this.x, this.y, this.passive === true && this.draw === true ? TYPE.operator : 5, `${this.name.charAt(0).toUpperCase() + this.name.substring(1).toLowerCase()}`])
    for (const id in this.ports.haste) {
      const port = this.ports.haste[id]
      a.push([this.x + port.x, this.y + port.y, TYPE.haste, `${this.glyph}-${id}`])
    }
    for (const id in this.ports.input) {
      const port = this.ports.input[id]
      a.push([this.x + port.x, this.y + port.y, TYPE.input, `${this.glyph}-${id}`])
    }

    if (this.ports.output) {
      const port = this.ports.output
      a.push([this.x + port.x, this.y + port.y, TYPE.output, `${this.glyph}-output`])
    }
    return a
  }

  this.getCase = function (ports = this.ports.input) {
    for (const id in ports) {
      const value = this.listen(ports[id])
      if (isUpperCase(value) === false) {
        return false
      }
    }
    return true
  }

  this.docs = function () {
    return `\`${this.glyph.toUpperCase()}\` **${this.name}**: ${this.info}`
  }

  function isUpperCase (a) { return `${a}`.toUpperCase() === `${a}` }
  function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
}

module.exports = Operator