smoothretro82

Infection script (infect nearby text) (update 6) revert added

Nov 14th, 2025 (edited)
207
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. function infectionFill(options) {
  2. options = options || {};
  3.  
  4. var cursorX = cursor.x;
  5. var cursorY = cursor.y;
  6.  
  7. // ---------------------------------------------
  8. // Infection groups (multiple selections)
  9. // ---------------------------------------------
  10. var infectionGroups = options.infectionGroups || [
  11. { chars: ["o"], color: "#000000" },
  12.  
  13. ];
  14.  
  15. var group = infectionGroups[Math.floor(Math.random() * infectionGroups.length)];
  16. var infectionChars = group.chars;
  17. var infectionColor = group.color || "#ffffff";
  18.  
  19. function randChar() {
  20. return infectionChars[Math.floor(Math.random() * infectionChars.length)];
  21. }
  22.  
  23. var limit = (typeof options.limit !== "undefined") ? options.limit : Infinity;
  24. var speed = (typeof options.speed !== "undefined") ? options.speed : 1;
  25.  
  26. // 🕒 Revert delays
  27. var revertDelay = options.revertDelay || 4000; // char revert
  28. var revertDelay2 = options.revertDelay2 || 8000; // final color restore
  29.  
  30. var maxJump = 2;
  31.  
  32. var dirs = [];
  33. for (var d = 1; d <= maxJump; d++) {
  34. dirs.push(
  35. [d, 0], [-d, 0], [0, d], [0, -d],
  36. [d, d], [-d, d], [d, -d], [-d, -d]
  37. );
  38. }
  39.  
  40. var queue = [];
  41. var visited = {};
  42. var revertedOnce = {}; // prevents re-infection & revert loops
  43.  
  44. var head = 0;
  45. var filled = 0;
  46.  
  47. function key(x, y) { return x + "," + y; }
  48.  
  49. function canInfect(x, y) {
  50. var info = getCharInfoXY(x, y);
  51. if (!info) return false;
  52.  
  53. var ch = (info.char || info.c || info.text || "").toString();
  54.  
  55. if (ch === " ") return false;
  56. if (infectionChars.includes(ch)) return false;
  57. if (revertedOnce[key(x, y)]) return false;
  58.  
  59. return true;
  60. }
  61.  
  62. function getColor(info) {
  63. return info.fg
  64. || info.fgColor
  65. || info.color
  66. || info.col
  67. || info.bg
  68. || info.bgColor
  69. || 15;
  70. }
  71.  
  72. // ---------------------------------------------
  73. // TWO-STAGE REVERT
  74. // ---------------------------------------------
  75. function scheduleRevert(x, y, originalChar, originalColor) {
  76.  
  77. // ➤ Stage 1: revert character only
  78. setTimeout(function() {
  79. var info = getCharInfoXY(x, y);
  80. if (!info) return;
  81.  
  82. var currentChar = (info.char || info.c || info.text || "").toString();
  83.  
  84. if (infectionChars.includes(currentChar)) {
  85. // revert char, KEEP infection color
  86. writeCharAt(originalChar, infectionColor, x, y);
  87. }
  88.  
  89. // mark immune regardless
  90. revertedOnce[key(x, y)] = true;
  91.  
  92. }, revertDelay);
  93.  
  94.  
  95. // ➤ Stage 2: restore original color
  96. setTimeout(function() {
  97. var info = getCharInfoXY(x, y);
  98. if (!info) return;
  99.  
  100. var currentChar = (info.char || info.c || info.text || "").toString();
  101.  
  102. // only restore color if the char already reverted
  103. if (currentChar === originalChar) {
  104. writeCharAt(originalChar, originalColor, x, y);
  105. }
  106.  
  107. }, revertDelay2);
  108. }
  109.  
  110. // ---------------------------------------------
  111. // INITIAL SPAWN
  112. // ---------------------------------------------
  113. for (var i = 0; i < dirs.length; i++) {
  114. var dx = dirs[i][0];
  115. var dy = dirs[i][1];
  116. var sx = cursorX + dx;
  117. var sy = cursorY + dy;
  118.  
  119. if (!canInfect(sx, sy)) continue;
  120.  
  121. var original = getCharInfoXY(sx, sy);
  122. if (!original) continue;
  123.  
  124. var originalChar = (original.char || original.c || original.text || "").toString();
  125. var originalColor = getColor(original);
  126.  
  127. writeCharAt(randChar(), infectionColor, sx, sy);
  128.  
  129. scheduleRevert(sx, sy, originalChar, originalColor);
  130.  
  131. queue.push({ x: sx, y: sy });
  132. visited[key(sx, sy)] = true;
  133. filled++;
  134. }
  135.  
  136. if (queue.length === 0) return;
  137.  
  138. var rafId = null;
  139. var speedAccumulator = 0;
  140.  
  141. function tick() {
  142. speedAccumulator += speed;
  143.  
  144. var maxToProcess = Math.floor(speedAccumulator);
  145. if (maxToProcess < 1) {
  146. rafId = requestAnimationFrame(tick);
  147. return;
  148. }
  149.  
  150. speedAccumulator -= maxToProcess;
  151. var processed = 0;
  152.  
  153. while (head < queue.length && processed < maxToProcess && filled < limit) {
  154. var t = queue[head++];
  155. var x = t.x;
  156. var y = t.y;
  157.  
  158. for (var i = 0; i < dirs.length; i++) {
  159. var dx = dirs[i][0];
  160. var dy = dirs[i][1];
  161. var nx = x + dx;
  162. var ny = y + dy;
  163.  
  164. var k = key(nx, ny);
  165.  
  166. if (!visited[k] && canInfect(nx, ny)) {
  167.  
  168. var original = getCharInfoXY(nx, ny);
  169. if (!original) continue;
  170.  
  171. var originalChar = (original.char || original.c || original.text || "").toString();
  172. var originalColor = getColor(original);
  173.  
  174. writeCharAt(randChar(), infectionColor, nx, ny);
  175.  
  176. scheduleRevert(nx, ny, originalChar, originalColor);
  177.  
  178. visited[k] = true;
  179. queue.push({ x: nx, y: ny });
  180. filled++;
  181. }
  182. }
  183.  
  184. processed++;
  185. }
  186.  
  187. if (head >= queue.length || filled >= limit) return;
  188.  
  189. rafId = requestAnimationFrame(tick);
  190. }
  191.  
  192. rafId = requestAnimationFrame(tick);
  193. }
  194.  
  195. infectionFill();
  196.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment