Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function infectionFill(options) {
- options = options || {};
- var cursorX = cursor.x;
- var cursorY = cursor.y;
- // infection characters (array)
- var infectionChars = options.infectionChars || ["░", "▒", "▓", "🮑", "🮒"];
- function randChar() {
- return infectionChars[Math.floor(Math.random() * infectionChars.length)];
- }
- // speed / limit
- var limit = (typeof options.limit !== "undefined") ? options.limit : Infinity;
- var speed = (typeof options.speed !== "undefined") ? options.speed : 1;
- // ---------------------------------------------
- // LONG-DISTANCE SPREAD
- // ----------------------------------------------
- var maxJump = 1
- var dirs = [];
- for (var d = 1; d <= maxJump; d++) {
- dirs.push(
- [d, 0], [-d, 0], [0, d], [0, -d],
- [d, d], [-d, d], [d, -d], [-d, -d]
- );
- }
- var queue = [];
- var visited = {};
- var head = 0;
- var filled = 0;
- function key(x, y) { return x + "," + y; }
- // only spread into NON-EMPTY, NON-INFECTED cells
- function canInfect(x, y) {
- var info = getCharInfoXY(x, y);
- if (!info) return false;
- // correctly extract char
- var ch = (info.char || info.c || info.text || "").toString();
- // skip empty
- if (ch === " ") return false;
- // skip already infected
- if (infectionChars.includes(ch)) return false;
- return true;
- }
- // safe color getter
- function getColor(info) {
- return info.fg
- || info.fgColor
- || info.color
- || info.col
- || info.bg
- || info.bgColor
- || 0; // safe bright fallback
- }
- // Initial spawn around the cursor
- for (var i = 0; i < dirs.length; i++) {
- var dx = dirs[i][0];
- var dy = dirs[i][1];
- var sx = cursorX + dx;
- var sy = cursorY + dy;
- if (!canInfect(sx, sy)) continue;
- var original = getCharInfoXY(sx, sy);
- if (!original) continue; // <---- FIXED: OOB protection
- var color = getColor(original);
- writeCharAt(randChar(), color, sx, sy);
- queue.push({ x: sx, y: sy });
- visited[key(sx, sy)] = true;
- filled++;
- }
- // If nothing was infected initially, stop
- if (queue.length === 0) return;
- var rafId = null;
- var speedAccumulator = 0;
- function tick() {
- speedAccumulator += speed;
- var maxToProcess = Math.floor(speedAccumulator);
- if (maxToProcess < 1) {
- rafId = requestAnimationFrame(tick);
- return;
- }
- speedAccumulator -= maxToProcess;
- var processed = 0;
- while (head < queue.length && processed < maxToProcess && filled < limit) {
- var t = queue[head++];
- var x = t.x;
- var y = t.y;
- for (var i = 0; i < dirs.length; i++) {
- var dx = dirs[i][0];
- var dy = dirs[i][1];
- var nx = x + dx;
- var ny = y + dy;
- var k = key(nx, ny);
- if (!visited[k] && canInfect(nx, ny)) {
- var original = getCharInfoXY(nx, ny);
- if (!original) continue; // <---- FIXED
- var color = getColor(original);
- writeCharAt(randChar(), color, nx, ny);
- visited[k] = true;
- queue.push({ x: nx, y: ny });
- filled++;
- }
- }
- processed++;
- }
- if (head >= queue.length || filled >= limit) return;
- // no need for delayFunc — jump directly
- rafId = requestAnimationFrame(tick);
- }
- rafId = requestAnimationFrame(tick);
- }
- infectionFill();
Advertisement
Add Comment
Please, Sign In to add comment