smoothretro1982

Infection Battle 0.4 (2 colors fighting one another)

Feb 28th, 2026 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. function infectionBattleWithActiveKillCells(options) {
  2. options = options || {};
  3.  
  4. var cursorX = cursor.x;
  5. var cursorY = cursor.y;
  6.  
  7. // Each group has characters and a color
  8. var infectionGroups = options.infectionGroups || [
  9. { chars: ["░","▒","▓"], color: 12 }, // Group A (blue)
  10. { chars: ["🮑","🮒"], color: 9 } // Group B (red)
  11. ];
  12.  
  13. var walls = options.walls || ["#", "█"];
  14. var killCells = options.killCells || ["+"]; // active kill cells
  15.  
  16. var limit = options.limit || Infinity;
  17. var speed = options.speed || 1;
  18. var maxJump = options.maxJump || 1;
  19. var battleChance = options.battleChance || 0.4;
  20. var killRadius = options.killRadius || 1; // radius around kill cell to clear infection
  21.  
  22. // Directions (8 neighbors)
  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 filled = 0;
  33.  
  34. function key(x, y) { return x + "," + y; }
  35.  
  36. function getCellChar(x, y) {
  37. var info = getCharInfoXY(x, y);
  38. if (!info) return null;
  39. return (info.char || info.c || info.text || "").toString();
  40. }
  41.  
  42. function clearInfectionAround(x, y) {
  43. for (var dx = -killRadius; dx <= killRadius; dx++) {
  44. for (var dy = -killRadius; dy <= killRadius; dy++) {
  45. var nx = x + dx, ny = y + dy;
  46. var ch = getCellChar(nx, ny);
  47. if (!ch) continue;
  48.  
  49. // Clear if it's an infected character
  50. for (var g = 0; g < infectionGroups.length; g++) {
  51. if (infectionGroups[g].chars.includes(ch)) {
  52. writeCharAt(" ", 15, nx, ny);
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59. function canInfect(x, y, group) {
  60. var ch = getCellChar(x, y);
  61. if (!ch) return false;
  62.  
  63. // Wall check
  64. if (walls.includes(ch)) return false;
  65.  
  66. // Active kill cell: cannot infect
  67. if (killCells.includes(ch)) {
  68. clearInfectionAround(x, y); // push back nearby infection
  69. return false;
  70. }
  71.  
  72. if (ch === " ") return true;
  73. if (group.chars.includes(ch)) return false;
  74.  
  75. // battle mechanics
  76. for (var g = 0; g < infectionGroups.length; g++) {
  77. if (infectionGroups[g].chars.includes(ch)) {
  78. return Math.random() < battleChance;
  79. }
  80. }
  81. return true;
  82. }
  83.  
  84. function randChar(group) {
  85. return group.chars[Math.floor(Math.random() * group.chars.length)];
  86. }
  87.  
  88. function getColor(info, group) {
  89. return group.color || info.fg || 15;
  90. }
  91.  
  92. // Seed each group at cursor
  93. infectionGroups.forEach(group => {
  94. if (canInfect(cursorX, cursorY, group)) {
  95. var info = getCharInfoXY(cursorX, cursorY);
  96. writeCharAt(randChar(group), getColor(info, group), cursorX, cursorY);
  97. queue.push({ x: cursorX, y: cursorY, group });
  98. filled++;
  99. }
  100. });
  101.  
  102. var head = 0;
  103. var rafId = null;
  104. var speedAccumulator = 0;
  105.  
  106. function tick() {
  107. speedAccumulator += speed;
  108. var maxToProcess = Math.floor(speedAccumulator);
  109. if (maxToProcess < 1) {
  110. rafId = requestAnimationFrame(tick);
  111. return;
  112. }
  113. speedAccumulator -= maxToProcess;
  114.  
  115. var processed = 0;
  116. while (head < queue.length && processed < maxToProcess && filled < limit) {
  117. var t = queue[head++];
  118. var x = t.x, y = t.y, group = t.group;
  119.  
  120. var shuffledDirs = dirs.slice().sort(() => Math.random() - 0.5);
  121.  
  122. for (var i = 0; i < shuffledDirs.length; i++) {
  123. var dx = shuffledDirs[i][0];
  124. var dy = shuffledDirs[i][1];
  125. var nx = x + dx, ny = y + dy;
  126. var ch = getCellChar(nx, ny);
  127.  
  128. if (killCells.includes(ch)) {
  129. clearInfectionAround(nx, ny);
  130. continue;
  131. }
  132.  
  133. if (!canInfect(nx, ny, group)) continue;
  134.  
  135. var info = getCharInfoXY(nx, ny);
  136. if (!info) continue;
  137.  
  138. writeCharAt(randChar(group), getColor(info, group), nx, ny);
  139. queue.push({ x: nx, y: ny, group });
  140. filled++;
  141. if (filled >= limit) break;
  142. }
  143.  
  144. processed++;
  145. }
  146.  
  147. if (head >= queue.length || filled >= limit) return;
  148. rafId = requestAnimationFrame(tick);
  149. }
  150.  
  151. rafId = requestAnimationFrame(tick);
  152.  
  153. return () => cancelAnimationFrame(rafId);
  154. }
  155.  
  156. // Example usage
  157. infectionBattleWithActiveKillCells({
  158. infectionGroups: [
  159. { chars: ["░","▒","▓"], color: 12 }, // blue
  160. { chars: ["🮑","🮒"], color: 9 } // red
  161. ],
  162. walls: ["#", "█"],
  163. killCells: ["+"],
  164. limit: 400,
  165. speed: 0.1,
  166. maxJump: 1,
  167. battleChance: 0.4,
  168. killRadius: 1 // radius around kill cell that clears infection
  169. });
Advertisement
Add Comment
Please, Sign In to add comment