Alien Invasion Simulator

5
5

Simulation Not Yet Run

Set parameters in the Setup tab and click "Initiate Simulation" to see the outcome.

Alien Invasion Simulation Report

Simulation Parameters

    Simulation Log

    Final Outcome

    Calculating invasion outcome...

    '; logMessages = []; // Clear message store // Switch to log tab document.querySelector('.sim-active-tab').classList.remove('sim-active-tab'); document.querySelector('.sim-active-panel').classList.remove('sim-active-panel'); document.querySelector('button[data-tab="log-panel"]').classList.add('sim-active-tab'); logPanel.classList.add('sim-active-panel'); // --- Get Parameters --- const settings = { earthDefense: parseInt(earthDefenseSlider.value, 10), alienTech: parseInt(alienTechSlider.value, 10), fleetSizeValue: parseFloat(fleetSizeSelect.value), fleetSizeName: fleetSizeSelect.options[fleetSizeSelect.selectedIndex].text }; logEvent("--- Simulation Initiated ---", "highlight"); await delay(500); logEvent(`Parameters: Earth Defense=${settings.earthDefense}, Alien Tech=${settings.alienTech}, Fleet Size=${settings.fleetSizeName}`); await delay(1000); // --- Simulation Steps --- let earthPower = settings.earthDefense * 10 + Math.random() * 20; // Base power + some randomness let alienPower = settings.alienTech * 10 * settings.fleetSizeValue + Math.random() * 20 * settings.fleetSizeValue; logEvent("Detecting incoming fleet...", "warning"); await delay(1500); if (settings.fleetSizeValue >= 2.0) { logEvent(`Massive energy signatures detected! Armada confirmed. Location: High Orbit over ${getRandomLocation()}`, "error"); } else if (settings.fleetSizeValue >= 1.0) { logEvent(`Multiple hostile contacts identified. Fleet approaching ${getRandomLocation()}.`, "warning"); } else { logEvent(`Anomalous probe signature detected near ${getRandomLocation()}. Stealth approach?`, "info"); } await delay(1000); logEvent("Global Defense Network Status: ACTIVATING...", "highlight"); await delay(1500); let earthVictories = 0; let alienVictories = 0; const battleRounds = 8; // Number of simulated combat rounds for (let i = 1; i <= battleRounds; i++) { logEvent(`--- Battle Round ${i} / ${battleRounds} ---`, "highlight"); await delay(800); let earthRoll = earthPower * (0.8 + Math.random() * 0.4); // Add variability let alienRoll = alienPower * (0.8 + Math.random() * 0.4); let earthWinChance = earthRoll / (earthRoll + alienRoll); logEvent(`Analyzing combat probabilities... Earth Advantage: ${Math.round(earthWinChance * 100)}%`); await delay(1200); if (Math.random() < earthWinChance) { earthVictories++; logEvent(`SUCCESS! Alien cruiser destroyed over ${getRandomLocation()}. Earth forces holding.`, "info"); alienPower *= 0.95; // Degrade alien power slightly on loss } else { alienVictories++; logEvent(`FAILURE! Earth defense platform offline near ${getRandomLocation()}. Heavy losses reported.`, "error"); earthPower *= 0.95; // Degrade earth power slightly on loss } await delay(1500); } logEvent("--- Combat Phase Concluded ---", "highlight"); await delay(1000); logEvent("Assessing planetary situation... Calculating final outcome."); await delay(2000); // --- Determine Outcome --- let finalOutcome = ""; let outcomeSummaryText = ""; const victoryRatio = earthVictories / battleRounds; if (victoryRatio > 0.7) { finalOutcome = "VICTORY: Invasion Repelled!"; outcomeSummaryText = `Against the odds, Earth's defense forces (${settings.earthDefense}) managed to push back the technologically superior (${settings.alienTech}) ${settings.fleetSizeName}. Key strategic victories (${earthVictories}/${battleRounds}) turned the tide. Earth remains defiant!`; resultsHeadline.style.color = 'var(--sim-accent-color)'; // Greenish/Cyan for victory } else if (victoryRatio < 0.3) { finalOutcome = "DEFEAT: Earth Overrun!"; outcomeSummaryText = `The alien onslaught proved too much. Despite valiant efforts by Earth's defenses (${settings.earthDefense}), the advanced alien technology (${settings.alienTech}) and sheer numbers of the ${settings.fleetSizeName} overwhelmed the planet (${alienVictories}/${battleRounds} rounds lost). A dark future awaits...`; resultsHeadline.style.color = '#ffaaaa'; // Red for defeat } else { finalOutcome = "STALEMATE: Brutal Conflict Continues!"; outcomeSummaryText = `Neither side could gain a decisive advantage. Earth's forces (${settings.earthDefense}) fought fiercely against the ${settings.fleetSizeName} with its level ${settings.alienTech} technology. The war grinds on (${earthVictories} Earth wins vs ${alienVictories} Alien wins), bleeding both sides dry. The future is uncertain.`; resultsHeadline.style.color = '#ffffaa'; // Yellow for stalemate } logEvent(`--- FINAL OUTCOME: ${finalOutcome} ---`, "highlight"); // --- Update Results & PDF --- resultsHeadline.textContent = finalOutcome; resultsSummary.innerHTML = `

    ${outcomeSummaryText}

    `; // Populate PDF Content pdfParamsList.innerHTML = `
  • Earth Defense Readiness: ${settings.earthDefense}/10
  • Alien Technology Level: ${settings.alienTech}/10
  • Invasion Fleet Size: ${settings.fleetSizeName} (Modifier: ${settings.fleetSizeValue})
  • `; pdfLogContainer.innerHTML = '

    Simulation Log

    '; // Reset PDF log area logMessages.forEach(msg => { const entry = document.createElement('div'); entry.className = 'pdf-log-entry'; entry.textContent = msg; pdfLogContainer.appendChild(entry); }); pdfOutcomePara.textContent = `${finalOutcome} - ${outcomeSummaryText}`; // Switch to results tab document.querySelector('.sim-active-tab').classList.remove('sim-active-tab'); document.querySelector('.sim-active-panel').classList.remove('sim-active-panel'); document.querySelector('button[data-tab="results-panel"]').classList.add('sim-active-tab'); document.getElementById('results-panel').classList.add('sim-active-panel'); // --- Finalize --- downloadButton.disabled = false; startButton.disabled = false; simulationRunning = false; } // --- Event Listeners --- startButton.addEventListener('click', runSimulation); downloadButton.addEventListener('click', () => { if (simulationRunning || logMessages.length === 0) return; const element = document.getElementById('sim-pdf-content'); const opt = { margin: [15, 10, 15, 10], // mm T, L, B, R filename: 'Alien_Invasion_Simulation_Report.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2, useCORS: true, logging: false }, jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' } }; // Make element temporarily visible for rendering element.style.visibility = 'visible'; element.style.position = 'fixed'; element.style.height = 'auto'; element.style.left = '0'; element.style.top = '0'; element.style.zIndex = '9999'; element.style.opacity = '1'; console.log("Generating PDF Report..."); html2pdf().set(opt).from(element).save().then(() => { console.log("PDF Report Generated."); // Hide element again element.style.visibility = 'hidden'; element.style.position = 'fixed'; element.style.height = '0'; element.style.left = '-9999px'; element.style.zIndex = '-1'; element.style.opacity = '0'; }).catch(err => { console.error("PDF generation failed:", err); // Ensure element is hidden on error element.style.visibility = 'hidden'; element.style.position = 'fixed'; element.style.height = '0'; element.style.left = '-9999px'; element.style.zIndex = '-1'; element.style.opacity = '0'; }); }); } // End of initializeSim })(); // IIFE
    Scroll to Top