Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function infectionBattleWithActiveKillCells(options) {
- options = options || {};
- var cursorX = cursor.x;
- var cursorY = cursor.y;
- // Each group has characters and a color
- var infectionGroups = options.infectionGroups || [
- { chars: ["░","▒","▓"], color: 12 }, // Group A (blue)
- { chars: ["🮑","🮒"], color: 9 } // Group B (red)
- ];
- var walls = options.walls || ["#", "█"];
- var killCells = options.killCells || ["+"]; // active kill cells
- var limit = options.limit || Infinity;
- var speed = options.speed || 1;
- var maxJump = options.maxJump || 1;
- var battleChance = options.battleChance || 0.4;
- var killRadius = options.killRadius || 1; // radius around kill cell to clear infection
- // Directions (8 neighbors)
- 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 filled = 0;
- function key(x, y) { return x + "," + y; }
- function getCellChar(x, y) {
- var info = getCharInfoXY(x, y);
- if (!info) return null;
- return (info.char || info.c || info.text || "").toString();
- }
- function clearInfectionAround(x, y) {
- for (var dx = -killRadius; dx <= killRadius; dx++) {
- for (var dy = -killRadius; dy <= killRadius; dy++) {
- var nx = x + dx, ny = y + dy;
- var ch = getCellChar(nx, ny);
- if (!ch) continue;
- // Clear if it's an infected character
- for (var g = 0; g < infectionGroups.length; g++) {
- if (infectionGroups[g].chars.includes(ch)) {
- writeCharAt(" ", 15, nx, ny);
- }
- }
- }
- }
- }
- function canInfect(x, y, group) {
- var ch = getCellChar(x, y);
- if (!ch) return false;
- // Wall check
- if (walls.includes(ch)) return false;
- // Active kill cell: cannot infect
- if (killCells.includes(ch)) {
- clearInfectionAround(x, y); // push back nearby infection
- return false;
- }
- if (ch === " ") return true;
- if (group.chars.includes(ch)) return false;
- // battle mechanics
- for (var g = 0; g < infectionGroups.length; g++) {
- if (infectionGroups[g].chars.includes(ch)) {
- return Math.random() < battleChance;
- }
- }
- return true;
- }
- function randChar(group) {
- return group.chars[Math.floor(Math.random() * group.chars.length)];
- }
- function getColor(info, group) {
- return group.color || info.fg || 15;
- }
- // Seed each group at cursor
- infectionGroups.forEach(group => {
- if (canInfect(cursorX, cursorY, group)) {
- var info = getCharInfoXY(cursorX, cursorY);
- writeCharAt(randChar(group), getColor(info, group), cursorX, cursorY);
- queue.push({ x: cursorX, y: cursorY, group });
- filled++;
- }
- });
- var head = 0;
- 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, y = t.y, group = t.group;
- var shuffledDirs = dirs.slice().sort(() => Math.random() - 0.5);
- for (var i = 0; i < shuffledDirs.length; i++) {
- var dx = shuffledDirs[i][0];
- var dy = shuffledDirs[i][1];
- var nx = x + dx, ny = y + dy;
- var ch = getCellChar(nx, ny);
- if (killCells.includes(ch)) {
- clearInfectionAround(nx, ny);
- continue;
- }
- if (!canInfect(nx, ny, group)) continue;
- var info = getCharInfoXY(nx, ny);
- if (!info) continue;
- writeCharAt(randChar(group), getColor(info, group), nx, ny);
- queue.push({ x: nx, y: ny, group });
- filled++;
- if (filled >= limit) break;
- }
- processed++;
- }
- if (head >= queue.length || filled >= limit) return;
- rafId = requestAnimationFrame(tick);
- }
- rafId = requestAnimationFrame(tick);
- return () => cancelAnimationFrame(rafId);
- }
- // Example usage
- infectionBattleWithActiveKillCells({
- infectionGroups: [
- { chars: ["░","▒","▓"], color: 12 }, // blue
- { chars: ["🮑","🮒"], color: 9 } // red
- ],
- walls: ["#", "█"],
- killCells: ["+"],
- limit: 400,
- speed: 0.1,
- maxJump: 1,
- battleChance: 0.4,
- killRadius: 1 // radius around kill cell that clears infection
- });
Advertisement
Add Comment
Please, Sign In to add comment