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 groups (multiple selections)
- // ---------------------------------------------
- var infectionGroups = options.infectionGroups || [
- { chars: ["o"], color: "#000000" },
- ];
- var group = infectionGroups[Math.floor(Math.random() * infectionGroups.length)];
- var infectionChars = group.chars;
- var infectionColor = group.color || "#ffffff";
- function randChar() {
- return infectionChars[Math.floor(Math.random() * infectionChars.length)];
- }
- var limit = (typeof options.limit !== "undefined") ? options.limit : Infinity;
- var speed = (typeof options.speed !== "undefined") ? options.speed : 1;
- // 🕒 Revert delays
- var revertDelay = options.revertDelay || 4000; // char revert
- var revertDelay2 = options.revertDelay2 || 8000; // final color restore
- var maxJump = 2;
- 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 revertedOnce = {}; // prevents re-infection & revert loops
- var head = 0;
- var filled = 0;
- function key(x, y) { return x + "," + y; }
- function canInfect(x, y) {
- var info = getCharInfoXY(x, y);
- if (!info) return false;
- var ch = (info.char || info.c || info.text || "").toString();
- if (ch === " ") return false;
- if (infectionChars.includes(ch)) return false;
- if (revertedOnce[key(x, y)]) return false;
- return true;
- }
- function getColor(info) {
- return info.fg
- || info.fgColor
- || info.color
- || info.col
- || info.bg
- || info.bgColor
- || 15;
- }
- // ---------------------------------------------
- // TWO-STAGE REVERT
- // ---------------------------------------------
- function scheduleRevert(x, y, originalChar, originalColor) {
- // ➤ Stage 1: revert character only
- setTimeout(function() {
- var info = getCharInfoXY(x, y);
- if (!info) return;
- var currentChar = (info.char || info.c || info.text || "").toString();
- if (infectionChars.includes(currentChar)) {
- // revert char, KEEP infection color
- writeCharAt(originalChar, infectionColor, x, y);
- }
- // mark immune regardless
- revertedOnce[key(x, y)] = true;
- }, revertDelay);
- // ➤ Stage 2: restore original color
- setTimeout(function() {
- var info = getCharInfoXY(x, y);
- if (!info) return;
- var currentChar = (info.char || info.c || info.text || "").toString();
- // only restore color if the char already reverted
- if (currentChar === originalChar) {
- writeCharAt(originalChar, originalColor, x, y);
- }
- }, revertDelay2);
- }
- // ---------------------------------------------
- // INITIAL SPAWN
- // ---------------------------------------------
- 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;
- var originalChar = (original.char || original.c || original.text || "").toString();
- var originalColor = getColor(original);
- writeCharAt(randChar(), infectionColor, sx, sy);
- scheduleRevert(sx, sy, originalChar, originalColor);
- queue.push({ x: sx, y: sy });
- visited[key(sx, sy)] = true;
- filled++;
- }
- 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;
- var originalChar = (original.char || original.c || original.text || "").toString();
- var originalColor = getColor(original);
- writeCharAt(randChar(), infectionColor, nx, ny);
- scheduleRevert(nx, ny, originalChar, originalColor);
- visited[k] = true;
- queue.push({ x: nx, y: ny });
- filled++;
- }
- }
- processed++;
- }
- if (head >= queue.length || filled >= limit) return;
- rafId = requestAnimationFrame(tick);
- }
- rafId = requestAnimationFrame(tick);
- }
- infectionFill();
Advertisement
Comments
-
- Note: If your running this in a browser conole, remove the infectionFill() at the end of the script, but keep it there if your going to run it in Eruda.
-
Comment was deleted
-
- Update: Infection now changes it's color to that of the character it infects.
-
- update 1: LONG-DISTANCE SPREAD ADDED
-
- Update #3: I've updated the infection script to choose a random set of characters to use as the infection characters, you can add more to the group if you want.
-
- Previous version: https://pastebin.com/ktMhmpyV
-
- Added undo funtion
Add Comment
Please, Sign In to add comment