smoothretro82

Infection script (infect nearby text) (update 3)

Nov 25th, 2025 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. function infectionFill(options) {
  2. options = options || {};
  3.  
  4. var cursorX = cursor.x;
  5. var cursorY = cursor.y;
  6.  
  7. // infection characters (array)
  8. var infectionChars = options.infectionChars || ["░", "▒", "▓", "🮑", "🮒"];
  9.  
  10. function randChar() {
  11. return infectionChars[Math.floor(Math.random() * infectionChars.length)];
  12. }
  13.  
  14. // speed / limit
  15. var limit = (typeof options.limit !== "undefined") ? options.limit : Infinity;
  16. var speed = (typeof options.speed !== "undefined") ? options.speed : 1;
  17.  
  18. // ---------------------------------------------
  19. // LONG-DISTANCE SPREAD
  20. // ----------------------------------------------
  21. var maxJump = 1
  22.  
  23. var dirs = [];
  24. for (var d = 1; d <= maxJump; d++) {
  25. dirs.push(
  26. [d, 0], [-d, 0], [0, d], [0, -d],
  27. [d, d], [-d, d], [d, -d], [-d, -d]
  28. );
  29. }
  30.  
  31. var queue = [];
  32. var visited = {};
  33. var head = 0;
  34. var filled = 0;
  35.  
  36. function key(x, y) { return x + "," + y; }
  37.  
  38. // only spread into NON-EMPTY, NON-INFECTED cells
  39. function canInfect(x, y) {
  40. var info = getCharInfoXY(x, y);
  41. if (!info) return false;
  42.  
  43. // correctly extract char
  44. var ch = (info.char || info.c || info.text || "").toString();
  45.  
  46. // skip empty
  47. if (ch === " ") return false;
  48.  
  49. // skip already infected
  50. if (infectionChars.includes(ch)) return false;
  51.  
  52. return true;
  53. }
  54.  
  55. // safe color getter
  56. function getColor(info) {
  57. return info.fg
  58. || info.fgColor
  59. || info.color
  60. || info.col
  61. || info.bg
  62. || info.bgColor
  63. || 0; // safe bright fallback
  64. }
  65.  
  66. // Initial spawn around the cursor
  67. for (var i = 0; i < dirs.length; i++) {
  68. var dx = dirs[i][0];
  69. var dy = dirs[i][1];
  70. var sx = cursorX + dx;
  71. var sy = cursorY + dy;
  72.  
  73. if (!canInfect(sx, sy)) continue;
  74.  
  75. var original = getCharInfoXY(sx, sy);
  76. if (!original) continue; // <---- FIXED: OOB protection
  77.  
  78. var color = getColor(original);
  79.  
  80. writeCharAt(randChar(), color, sx, sy);
  81.  
  82. queue.push({ x: sx, y: sy });
  83. visited[key(sx, sy)] = true;
  84. filled++;
  85. }
  86.  
  87. // If nothing was infected initially, stop
  88. if (queue.length === 0) return;
  89.  
  90. var rafId = null;
  91. var speedAccumulator = 0;
  92.  
  93. function tick() {
  94. speedAccumulator += speed;
  95.  
  96. var maxToProcess = Math.floor(speedAccumulator);
  97. if (maxToProcess < 1) {
  98. rafId = requestAnimationFrame(tick);
  99. return;
  100. }
  101.  
  102. speedAccumulator -= maxToProcess;
  103. var processed = 0;
  104.  
  105. while (head < queue.length && processed < maxToProcess && filled < limit) {
  106. var t = queue[head++];
  107. var x = t.x;
  108. var y = t.y;
  109.  
  110. for (var i = 0; i < dirs.length; i++) {
  111. var dx = dirs[i][0];
  112. var dy = dirs[i][1];
  113. var nx = x + dx;
  114. var ny = y + dy;
  115.  
  116. var k = key(nx, ny);
  117.  
  118. if (!visited[k] && canInfect(nx, ny)) {
  119.  
  120. var original = getCharInfoXY(nx, ny);
  121. if (!original) continue; // <---- FIXED
  122.  
  123. var color = getColor(original);
  124.  
  125. writeCharAt(randChar(), color, nx, ny);
  126.  
  127. visited[k] = true;
  128. queue.push({ x: nx, y: ny });
  129. filled++;
  130. }
  131. }
  132.  
  133. processed++;
  134. }
  135.  
  136. if (head >= queue.length || filled >= limit) return;
  137.  
  138. // no need for delayFunc — jump directly
  139. rafId = requestAnimationFrame(tick);
  140. }
  141.  
  142. rafId = requestAnimationFrame(tick);
  143. }
  144.  
  145. infectionFill();
  146.  
Advertisement
Add Comment
Please, Sign In to add comment