-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
298 lines (247 loc) · 8.12 KB
/
index.js
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//const { editor } = require("monaco-editor")
let WIDTH = 16
let HEIGHT = 16
let DEFAULTCELLTYPE = 0
let WORLDWRAP = true
let selected = 0
let map = []
let oldmap = []
function generateEmptyMaps(){
for(let i = 0; i < HEIGHT; i++){
map.push([])
oldmap.push([])
for(let j = 0; j < WIDTH; j++){
map[i].push(DEFAULTCELLTYPE)
oldmap[i].push(DEFAULTCELLTYPE)
}
}
}
generateEmptyMaps()
function resizeMaps(){
while(map.length < HEIGHT){
map.push([])
oldmap.push([])
}
map.length = HEIGHT
oldmap.length = HEIGHT
for(let i = 0; i < map.length; i++){
while(map[i].length < WIDTH){
map[i].push(DEFAULTCELLTYPE)
oldmap[i].push(DEFAULTCELLTYPE)
}
map[i].length = WIDTH
oldmap[i].length = WIDTH
}
}
function getNeighborCells(x, y, rollaround = false){
let neighborsmap = []
for(let i = -1; i < 2; i++){
for(let j = -1; j < 2; j++){
if(i == 0 & j == 0) continue
if(rollaround == false && x+i < 0) {neighborsmap.push(DEFAULTCELLTYPE); continue}
if(rollaround == false && x+i >= WIDTH) {neighborsmap.push(DEFAULTCELLTYPE); continue}
if(rollaround == false && y+j < 0) {neighborsmap.push(DEFAULTCELLTYPE); continue}
if(rollaround == false && y+j >= HEIGHT) {neighborsmap.push(DEFAULTCELLTYPE); continue}
neighborsmap.push(oldmap[(x + i + HEIGHT)%HEIGHT][(y + j + WIDTH)%WIDTH])
}
}
return neighborsmap
}
class RuleSimple {
constructor(requestedType, amountMin, amountMax, output, neighborsMap = 0b11111111, isReversed = false){
this.type = requestedType
this.min = amountMin
this.max = amountMax
this.output = output
this.checkMap = neighborsMap
this.reverse = isReversed
}
check(neighborsMap){
let count = 0
for(let i = 0; i < 8; i++){
if(2**i & this.checkMap == 0) continue
if(neighborsMap[i] == this.type) count ++
}
let condition = count >= this.min & count <= this.max
if (this.reverse) condition = !condition
if(condition){
if(Array.isArray(this.output)){
for(let i of this.output){
let result = i.check(neighborsMap)
if (result != -1) return result
}
return -1
}else{return this.output}
}else{
return -1
}
}
}
class RuleAction{
constructor(output){
this.output = output
}
check(neighborsMap){
return this.output
}
}
class Element{
constructor(color, name, rules=[]){
this.color = color
this.name = name
this.rules = rules
}
check(neighborsMap){
for(let i of this.rules){
let result = i.check(neighborsMap)
if (result != -1) return result
}
return -1
}
}
/*
const LIVE = new Element("#AEAEBB", "LIVE", [new RuleSimple(1, 2, 3, 0, undefined , isReversed = true)])
const DEAD = new Element("#161616", "DEAD", [new RuleSimple(1, 3, 3, 1)])
const WIRE = new Element("#565656", "WIRE", [new RuleSimple(3, 1, 2, 3)])
const WIREACRIVE = new Element("#EA5656", "WIREACTIVE", [new RuleAction(4)])
const WIRETRACE = new Element("#5656AE", "WIRETRACE", [new RuleAction(2)])
*/
let TYPES = []
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
function prepare(){
for(let i = 0; i < HEIGHT; i++){
for(let j = 0; j < WIDTH; j++){
if(TYPES.length <= DEFAULTCELLTYPE) break
ctx.fillStyle = TYPES[DEFAULTCELLTYPE].color;
ctx.fillRect(i, j, 1, 1)
}
}
}
function STEP(){
for(let i = 0; i < HEIGHT; i++){
for(let j = 0; j < WIDTH; j++){
//let nMap = getNeighborCells(i, j, WORLDWRAP)
let nMap = []
for(let is = -1; is < 2; is++){
for(let js = -1; js < 2; js++){
if(is == 0 & js == 0) continue
if(WORLDWRAP == false && is+i < 0) {nMap.push(DEFAULTCELLTYPE); continue}
if(WORLDWRAP == false && is+i >= WIDTH) {nMap.push(DEFAULTCELLTYPE); continue}
if(WORLDWRAP == false && js+j < 0) {nMap.push(DEFAULTCELLTYPE); continue}
if(WORLDWRAP == false && js+j >= HEIGHT) {nMap.push(DEFAULTCELLTYPE); continue}
nMap.push(oldmap[(is + i + HEIGHT)%HEIGHT][(js + j + WIDTH)%WIDTH])
}
}
if(oldmap[i][j] >= TYPES.length) continue
let newtype = TYPES[oldmap[i][j]].check(nMap, [i, j])
if (newtype == -1) newtype = oldmap[i][j]
if(newtype != oldmap[i][j]){
ctx.fillStyle = TYPES[newtype].color
ctx.fillRect(j, i, 1, 1)
}
map[i][j] = newtype
}
}
[map, oldmap] = [oldmap, map]
}
document.getElementById('canvas').onclick = function(e) {
// e = Mouse click event.
var rect = e.target.getBoundingClientRect();
var x = Math.floor((e.clientX - rect.left) / (rect.width / WIDTH)); //x position within the element.
var y = Math.floor((e.clientY - rect.top) / (rect.height / HEIGHT)); //y position within the element.
oldmap[y][x] = selected
const canvas = e.target;
const ctx = canvas.getContext("2d");
ctx.fillStyle = TYPES[selected].color;
ctx.fillRect(x, y, 1, 1)
}
function update(){
applyParsing(editor.getValue());
let sel = document.getElementById('selection');
let seld = document.getElementById('selectdefault');
sel.innerHTML = ''
seld.innerHTML = ''
sel.onchange = (event) => {
selected = Number(event.target.value);
}
seld.onchange = (event) => {
DEFAULTCELLTYPE = Number(event.target.value);
}
for(let i = 0; i < TYPES.length; i++){
sel.innerHTML += `<option value=${i}>${TYPES[i].name}</option>`
seld.innerHTML += `<option value=${i}>${TYPES[i].name}</option>`
}
}
let selectedexample = 0
function setupExampleLoader(){
let esel = document.getElementById('examples')
esel.onchange = (event) => {
selectedexample = Number(event.target.value);
}
}
function loadExample(){
switch(Number(selectedexample)){
case 0:
editor.setValue(`element Live {
color #aeaebb;
rules {
ifnot [2..3] Live at DIR.ANY => Dead;
}
}
element Dead {
color #161616;
rules {
if 3 Live at DIR.ANY => Live;
}
}`)
update();
for(let i = 0; i < TYPES.length; i++){
if(TYPES[i].name == "Live") {
selected = i
}
if(TYPES[i].name == "Dead") {
DEFAULTCELLTYPE = i
}
}
break;
case 1:
editor.setValue(`element _Empty {
color #000000;
}
element Wire {
color #aeaeae;
rules {
if [1..2] Charge at DIR.ANY => Charge;
}
}
element Charge {
color #aeae56;
rules {
=> ChargeTrace;
}
}
element ChargeTrace {
color #ae56ae;
rules {
=> Wire;
}
}`)
update();
for(let i = 0; i < TYPES.length; i++){
if(TYPES[i].name == "Wire") selected = i
if(TYPES[i].name == "_Empty") DEFAULTCELLTYPE = i
}
break;
}
document.getElementById('selection').value = selected
document.getElementById('selectdefault').value = DEFAULTCELLTYPE
var rect = document.getElementById('canvas').getBoundingClientRect();
for(let i = 0; i < HEIGHT; i++){
for(let j = 0; j < WIDTH; j++){
oldmap[i][j] = DEFAULTCELLTYPE
ctx.fillStyle = TYPES[DEFAULTCELLTYPE].color;
ctx.fillRect(i, j, 1, 1)
}
}
}