Advertisement
DevByPowerPoint

SURVIVAL game engine

Oct 9th, 2024
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 7.58 KB | Source Code | 0 0
  1. function runSurvivalSimulation() {
  2.     // Initial values
  3.     var food = 100;
  4.     var shelter = 100;
  5.     var security = 100;
  6.     var comms = 100;
  7.     var daysLeft = 90;
  8.     var daysIn = 0;
  9.     var survive = true;
  10.     var daysWithoutFood = 0;
  11.     var maxDaysWithoutFood = 7;
  12.     var failureReason = "";
  13.     var initialFailureDay = 0;
  14.     var initialFailureCause = "";
  15.  
  16.     // Reset variables at the start of each run
  17.     player.SetVar("Survive", true);
  18.     player.SetVar("DaysWithoutFood", 0);
  19.     player.SetVar("DaysIn", 0);
  20.     player.SetVar("FailureReason", "");
  21.  
  22.     // Team composition
  23.     var teamA = player.GetVar("Person_A");
  24.     var teamB = player.GetVar("Person_B");
  25.     var teamC = player.GetVar("Person_C");
  26.  
  27.     // Calculate daily resource changes
  28.     var foodChange = -1.3;
  29.     var shelterChange = -0.2;
  30.     var securityChange = -0.5;
  31.     var commsChange = -0.3;
  32.  
  33.     if (teamA) {
  34.         foodChange += 1.2;
  35.         shelterChange -= 0.05;
  36.         securityChange -= 0.3;
  37.         commsChange -= 0.1;
  38.     }
  39.     if (teamB) {
  40.         foodChange -= 0.2;
  41.         shelterChange -= 0.05;
  42.         securityChange -= 0.3;
  43.         commsChange += 0.3;
  44.     }
  45.     if (teamC) {
  46.         foodChange -= 0.2;
  47.         shelterChange += 0.05;
  48.         securityChange += 0.5;
  49.         commsChange += 0.3;
  50.     }
  51.  
  52.     // Additional security penalty for A+B combination
  53.     if (teamA && teamB && !teamC) {
  54.         securityChange -= 0.2; // Increase security depletion rate for A+B
  55.     }
  56.  
  57.     function getRandomPenalty(min, max) {
  58.         return Math.floor(Math.random() * (max - min + 1)) + min;
  59.     }
  60.  
  61.     function updateResources() {
  62.         // Update resources
  63.         food = Math.max(0, Math.min(100, food + foodChange));
  64.         shelter = Math.max(0, Math.min(100, shelter + shelterChange));
  65.         security = Math.max(0, Math.min(100, security + securityChange));
  66.         comms = Math.max(0, Math.min(100, comms + commsChange));
  67.  
  68.         daysLeft--;
  69.         daysIn++;
  70.  
  71.         console.log(`Day ${daysIn} (${daysLeft} left): Food=${food.toFixed(2)}, Shelter=${shelter.toFixed(2)}, Security=${security.toFixed(2)}, Comms=${comms.toFixed(2)}, DaysWithoutFood=${daysWithoutFood}`);
  72.  
  73.         // Apply cascading effects
  74.         if (security < 40) {
  75.             var severeFoodPenalty = getRandomPenalty(10, 20);
  76.             var severeShelterPenalty = getRandomPenalty(5, 15);
  77.             var severeCommsPenalty = getRandomPenalty(15, 25);
  78.            
  79.             food -= severeFoodPenalty;
  80.             shelter -= severeShelterPenalty;
  81.             comms -= severeCommsPenalty;
  82.  
  83.             console.log(`Day ${daysIn}: Security breach! Lost ${severeFoodPenalty} Food and Water, ${severeShelterPenalty} Shelter, and ${severeCommsPenalty} Comms.`);
  84.            
  85.             if (initialFailureCause === "") {
  86.                 initialFailureDay = daysIn;
  87.                 initialFailureCause = "Security";
  88.             }
  89.         }
  90.  
  91.         if (shelter < 50) {
  92.             var criticalFoodPenalty = getRandomPenalty(5, 10);
  93.             food -= criticalFoodPenalty;
  94.             console.log(`Day ${daysIn}: Critical shelter condition! Lost ${criticalFoodPenalty} Food and Water due to exposure.`);
  95.         }
  96.  
  97.         // Ensure resources don't go below 0
  98.         food = Math.max(0, food);
  99.         shelter = Math.max(0, shelter);
  100.         security = Math.max(0, security);
  101.         comms = Math.max(0, Math.min(100, comms));
  102.  
  103.         // Check for food depletion
  104.         if (food <= 0) {
  105.             daysWithoutFood++;
  106.             player.SetVar("DaysWithoutFood", daysWithoutFood);
  107.             console.log(`Day ${daysIn}: No food and water! Days without sustenance: ${daysWithoutFood}`);
  108.  
  109.             if (initialFailureCause === "") {
  110.                 initialFailureDay = daysIn;
  111.                 initialFailureCause = "Food";
  112.             }
  113.         } else {
  114.             daysWithoutFood = 0;
  115.             player.SetVar("DaysWithoutFood", 0);
  116.         }
  117.  
  118.         // Update Storyline variables
  119.         player.SetVar("Food", Math.round(food));
  120.         player.SetVar("Shelter", Math.round(shelter));
  121.         player.SetVar("Security", Math.round(security));
  122.         player.SetVar("Comms", Math.round(comms));
  123.         player.SetVar("DaysLeft", daysLeft);
  124.         player.SetVar("DaysIn", daysIn);
  125.  
  126.         // Continue simulation if there are still days left and haven't exceeded max days without food
  127.         if (daysLeft > 0 && daysWithoutFood < maxDaysWithoutFood) {
  128.             setTimeout(updateResources, 400); // Run every 0.4 seconds
  129.         } else {
  130.             endSimulation();
  131.         }
  132.     }
  133.  
  134.     function endSimulation() {
  135.         // Determine survival and failure reason
  136.         if (daysWithoutFood >= maxDaysWithoutFood) {
  137.             survive = false;
  138.             if (initialFailureCause === "Security") {
  139.                 failureReason = `On day ${initialFailureDay}, the security shields dropped below 40%, allowing hostile local wildlife to destroy the homing beacon, damage your shelter, and consume your food and water stores. This led to complete depletion of sustenance on day ${daysIn - daysWithoutFood} and severe damage to your communications system. You lasted ${daysWithoutFood} more days without food and water before perishing from starvation, dehydration, and exposure to the elements.`;
  140.             } else {
  141.                 if (comms >= 90) {
  142.                     failureReason = `On day ${initialFailureDay}, you ran out of food and drinking water. You lasted ${daysWithoutFood} more days without sustenance before perishing from starvation, dehydration, and exposure to the elements. Ironically, your communication system continued to broadcast a strong signal, though the camp fell eerily silent. The rescue team will find a fully functioning beacon, but no survivors.`;
  143.                 } else {
  144.                     failureReason = `On day ${initialFailureDay}, you ran out of food and drinking water. You lasted ${daysWithoutFood} more days without sustenance before perishing from starvation, dehydration, and exposure to the elements.`;
  145.                 }
  146.             }
  147.         } else if (daysLeft === 0) {
  148.             if (comms < 90 || comms > 100) {
  149.                 survive = false;
  150.                 failureReason = `On the final day, your homing beacon failed to maintain the required signal strength (${comms.toFixed(2)}%), leaving you stranded on the planet despite surviving for 90 days.`;
  151.             } else {
  152.                 survive = true;
  153.                 failureReason = "Congratulations! Your crew successfully survived the full 90 days until rescue arrived. Through careful resource management and strategic decision-making, you maintained your food and water supplies, kept your shelter intact, defended against wildlife, and maintained a strong communication signal.";
  154.             }
  155.         }
  156.  
  157.         // Round down all values at the end of the simulation
  158.         food = Math.floor(food);
  159.         shelter = Math.floor(shelter);
  160.         security = Math.floor(security);
  161.         comms = Math.floor(comms);
  162.        
  163.         // Update Storyline variables with final values
  164.         player.SetVar("Food", food);
  165.         player.SetVar("Shelter", shelter);
  166.         player.SetVar("Security", security);
  167.         player.SetVar("Comms", comms);
  168.         player.SetVar("DaysLeft", daysLeft);
  169.         player.SetVar("DaysIn", daysIn);
  170.         player.SetVar("Survive", survive);
  171.         player.SetVar("FailureReason", failureReason);
  172.  
  173.         console.log(`Simulation ended. ${failureReason}`);
  174.     }
  175.  
  176.     // Start the simulation
  177.     updateResources();
  178. }
  179.  
  180. // Run the simulation
  181. runSurvivalSimulation();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement