-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
179 lines (151 loc) · 4.19 KB
/
main.js
File metadata and controls
179 lines (151 loc) · 4.19 KB
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
// TEST YOUR (TTY) MIGHT: DOOM FIRE!
// (c) 2022 const void*
import { stdout } from 'process';
//
// copy+paste code as it helps.
//
// monolothic file by design - no dependencies.
//
// For zig version, see:
// https://github.com/const-void/DOOM-fire-zig/
// TRM //////////////////////////
// Terminal size
let sz=stdout.getWindowSize();
let term_col=sz[0];
let term_row=sz[1];
// TTY ///////////////////////////
//escape sequence
const esc='\u001b';
//xterm-256 color cache: foreground (fg) / background (bg)
const MAX_COLOR=255;
let fg=new Array(MAX_COLOR);
let bg=new Array(MAX_COLOR);
for (let c=0; c<=MAX_COLOR; c++) {
fg[c]=`${esc}[38;5;${c}m`;
bg[c]=`${esc}[48;5;${c}m`;
}
//cursor codes
let cursor_show=`${esc}[?25h`; //h=high
let cursor_hide=`${esc}[?25l`; //l=low
let cursor_home=`${esc}[1;1H`; //1,1
let screen_buf_on=`${esc}[1049h`; //h=high
let screen_buf_off=`${esc}[1049l`; //l=low
// FIRE ////////////////////////
const FIRE_H=term_row*2;
const FIRE_W=term_col;
const FIRE_SZ=FIRE_H*FIRE_W;
const FIRE_LAST_ROW=(FIRE_H-1)*FIRE_W;
let screen_buf=[FIRE_SZ-1];
let fire_pallette=[0,233,234,52,53,88,89,94,95,96,130,131,132,133,172,214,215,220,220,221,3,226,227,230,231,7];
let fire_black=0;
let fire_white=fire_pallette.length-1;
// start w/black screen
for (let i=0; i < FIRE_SZ; i++) {
screen_buf[i]=fire_black;
}
// last row is "white" - fire feeder
for (let i=0; i<FIRE_W; i++) {
screen_buf[FIRE_LAST_ROW+i]=fire_white;
}
let spread_px=0;
let spread_rnd_idx=0;
let spread_dst=0;
function spreadFire(px_idx) {
spread_px = screen_buf[px_idx];
if (spread_px==0) {
screen_buf[px_idx-FIRE_W]=0;
}
else {
spread_rnd_idx=Math.round(Math.random() * 3.0);// & 3;
spread_dst=px_idx-spread_rnd_idx+1;
screen_buf[spread_dst-FIRE_W]=spread_px - (spread_rnd_idx & 1);
}
}
//scope cache (thread unsafe)
let doFire_x=0;
let doFire_y=0;
function doFire() {
for (doFire_x=0; doFire_x<FIRE_W; doFire_x++) {
for (doFire_y=0; doFire_y<FIRE_H; doFire_y++) {
spreadFire(doFire_y*FIRE_W+doFire_x);
}
}
}
// c/o https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
function fmtKB(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
//scope cache (thread unsafe)
//let last_sz=0;
let init_s=cursor_home+bg[0]+fg[0];
let frame_x=0;
let frame_y=0;
let px_hi=0;
let px_lo=0;
let px_prev_hi=0;
let px_prev_lo=0;
let s=init_s;
let s_len=s.length;
let s_sz_min=0;
let s_sz_max=0;
let s_sz_avg=0;
let s_frame_tic=0;
let time_start=Date.now();
let time_now=Date.now();
let time_pass=time_now-time_start;
function drawFrame() {
doFire();
//scope cache reset
px_hi=0;
px_lo=0;
px_prev_hi=0;
px_prev_lo=0;
s=init_s;
for (frame_y=0; frame_y<FIRE_H; frame_y=frame_y+2) {
for (frame_x=0; frame_x<FIRE_W; frame_x++) {
px_hi=screen_buf[frame_y*FIRE_W+frame_x];
px_lo=screen_buf[(frame_y+1)*FIRE_W+frame_x];
if (px_lo!=px_prev_lo) {
s+=bg[fire_pallette[px_lo]];
}
if (px_hi!=px_prev_hi) {
s+=fg[fire_pallette[px_hi]];
}
s+='▀';
px_prev_hi=px_hi;
px_prev_lo=px_lo;
}
}
stdout.write(s);
time_now=Date.now();
s_len=s.length;
s_frame_tic++;
if (s_sz_min==0) {
s_sz_min=s_len;
s_sz_max=s_len;
s_sz_avg=s_len;
}
else {
if (s_len < s_sz_min) { s_sz_min=s_len; }
if (s_len > s_sz_max) { s_sz_max=s_len; }
s_sz_avg=s_sz_avg*(s_frame_tic-1)/s_frame_tic+s_len/s_frame_tic;
}
time_pass=(time_now-time_start)/1000;
//some stats
stdout.write(`${fg[0]} mem: ${fmtKB(s_sz_min)} min / ${fmtKB(s_sz_avg)} avg / ${fmtKB(s_sz_max)} max [ ${(s_frame_tic/time_pass).toFixed(2)} fps ] ${s_frame_tic}`)
}
const sleepNow = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
// MAIN ///////////////////////
// term prep
stdout.write(screen_buf_on);
stdout.write(cursor_hide);
let ok=true;
time_start=Date.now();
while(ok) {
drawFrame();
}