Lucibear

Untitled

Aug 23rd, 2020
2,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function autoJobs() {
  2.         state.jobManager.calculateCraftingMaxs();
  3.         let jobList = state.jobManager.managedPriorityList();
  4.  
  5.         // No jobs unlocked yet
  6.         if (jobList.length === 0) {
  7.             return;
  8.         }
  9.  
  10.         let quarryWorkerIndex = jobList.indexOf(state.jobs.QuarryWorker);
  11.         let lumberjackIndex = -1;
  12.         let scavengerIndex = jobList.indexOf(state.jobs.Scavenger);
  13.         let farmerIndex = jobList.indexOf(state.jobs.Farmer);
  14.  
  15.         if (isEvilRace() && !isEvilUniverse()) {
  16.             lumberjackIndex = jobList.indexOf(state.jobs.Farmer);
  17.         } else {
  18.             lumberjackIndex = jobList.indexOf(state.jobs.Lumberjack);
  19.         }
  20.  
  21.         let breakpoint0Max = 0;
  22.         let breakpoint1Max = 0;
  23.  
  24.         // Cath / Balorg / Imp race doesn't have farmers, unemployed are their farmers
  25.         if (isHunterRace()) {
  26.             for (let i = 0; i < jobList.length; i++) {
  27.                 const job = jobList[i];
  28.                 breakpoint0Max += job.breakpointEmployees(0, false);
  29.                 breakpoint1Max += job.breakpointEmployees(1, false);
  30.             }
  31.  
  32.             log("autoJobs", "Max breakpoint 0: " + breakpoint0Max)
  33.             log("autoJobs", "Max breakpoint 1: " + breakpoint1Max)
  34.         }
  35.  
  36.         let availableEmployees = state.jobManager.totalEmployees;
  37.         let requiredJobs = [];
  38.         let jobAdjustments = [];
  39.  
  40.         log("autoJobs", "Total employees: " + availableEmployees);
  41.  
  42.         // First figure out how many farmers are required
  43.         if (state.jobs.Farmer.isManaged()) {
  44.             if (!state.jobs.Lumberjack.isUnlocked() && !state.jobs.QuarryWorker.isUnlocked() && game.global.race.species !== "entish") {
  45.                 // No other jobs are unlocked - everyone on farming!
  46.                 requiredJobs.push(availableEmployees);
  47.                 log("autoJobs", "Pushing all farmers")
  48.             } else if (resources.Population.currentQuantity > state.lastPopulationCount) {
  49.                 let populationChange = resources.Population.currentQuantity - state.lastPopulationCount;
  50.                 let farmerChange = state.jobs.Farmer.count - state.lastFarmerCount;
  51.  
  52.                 if (populationChange === farmerChange && resources.Food.rateOfChange > 0) {
  53.                     requiredJobs.push(Math.max(state.jobs.Farmer.count - populationChange, 0));
  54.                     log("autoJobs", "Removing a farmer due to population growth")
  55.                 } else {
  56.                     requiredJobs.push(state.jobs.Farmer.count);
  57.                 }
  58.             } else if (resources.Food.storageRatio < 0.2 && resources.Food.rateOfChange < 0) {
  59.                 // We want food to fluctuate between 0.2 and 0.8 only. We only want to add one per loop until positive
  60.                 requiredJobs.push(Math.min(state.jobs.Farmer.count + 1, availableEmployees));
  61.                 log("autoJobs", "Adding one farmer")
  62.             } else if (resources.Food.storageRatio > 0.8 && resources.Food.rateOfChange > 0) {
  63.                 // We want food to fluctuate between 0.2 and 0.8 only. We only want to remove one per loop until negative
  64.                 requiredJobs.push(Math.max(state.jobs.Farmer.count - 1, 0));
  65.                 log("autoJobs", "Removing one farmer")
  66.             } else if (isHunterRace() && resources.Food.storageRatio > 0.3 && resources.Food.rateOfChange > resources.Population.currentQuantity / 10) {
  67.                 // Carnivore race. Put We've got some food so put them to work!
  68.                 requiredJobs.push(Math.max(state.jobs.Farmer.count - 1, 0));
  69.                 log("autoJobs", "Removing one farmer - Carnivore")
  70.             } else {
  71.                 // We're good; leave farmers as they are
  72.                 requiredJobs.push(state.jobs.Farmer.count);
  73.                 log("autoJobs", "Leaving current farmers")
  74.             }
  75.  
  76.             log("autoJobs", "currentQuantity " + resources.Population.currentQuantity + " breakpoint1Max " + breakpoint1Max + " requiredJobs[0] " + requiredJobs[0] + " breakpointEmployees(1) " + state.jobs.Lumberjack.breakpointEmployees(1, false) +  " breakpointEmployees(0) " + state.jobs.Lumberjack.breakpointEmployees(0, false))
  77.             if (isEvilRace() && !isEvilUniverse()) {
  78.                 if (resources.Population.currentQuantity > breakpoint0Max && requiredJobs[0] < state.jobs.Lumberjack.breakpointEmployees(1, false)) {
  79.                     log("autoJobs", "Setting required hunters to breakpoint 1")
  80.                     requiredJobs[0] = state.jobs.Lumberjack.breakpointEmployees(1, false);
  81.                 } else if (requiredJobs[0] < state.jobs.Lumberjack.breakpointEmployees(0, false)) {
  82.                     log("autoJobs", "Setting required hunters to breakpoint 0")
  83.                     requiredJobs[0] = state.jobs.Lumberjack.breakpointEmployees(0, false);
  84.                 }
  85.             }
  86.  
  87.             if (requiredJobs[0] < 0) { requiredJobs[0] = 0; }
  88.  
  89.             jobAdjustments.push(requiredJobs[0] - state.jobs.Farmer.count);
  90.             availableEmployees -= requiredJobs[0];
  91.         }
  92.  
  93.         let availableCraftsmen = state.jobManager.maxCraftsmen;
  94.  
  95.         for (let i = 0; i < state.jobManager.maxJobBreakpoints; i++) {
  96.             for (let j = 0; j < jobList.length; j++) {
  97.                 const job = jobList[j];
  98.  
  99.                 // We've already done the farmer above
  100.                 if (job === state.jobs.Farmer) {
  101.                     continue;
  102.                 }
  103.  
  104.                 if (i !== 0) {
  105.                     // If we're going up to the next breakpoint then add back the workers from this job from the last one
  106.                     // so that we don't double-take them
  107.                     availableEmployees += requiredJobs[j];
  108.  
  109.                     // We have to keep track of craftsmen separately as they have a special max number of total craftsmen
  110.                     if (job.isCraftsman()) {
  111.                         availableCraftsmen += requiredJobs[j];
  112.                     }
  113.                 }
  114.  
  115.                 log("autoJobs", "job " + job._originalId + " job.breakpointEmployees(i) " + job.breakpointEmployees(i, false) + " availableEmployees " + availableEmployees);
  116.                 let jobsToAssign = 0;
  117.                 if (!job.isCraftsman()) {
  118.                     jobsToAssign = Math.min(availableEmployees, job.breakpointEmployees(i, false));
  119.                 } else {
  120.                     // We have to keep track of craftsmen separately as they have a special max number of total craftsmen
  121.                     jobsToAssign = Math.min(availableEmployees, availableCraftsmen, job.breakpointEmployees(i, false));
  122.                 }
  123.  
  124.                 // Don't assign bankers if our money is maxed and bankers aren't contributing to our money storage cap
  125.                 if (job === state.jobs.Banker && !isResearchUnlocked("swiss_banking") && resources.Money.storageRatio > 0.98) {
  126.                     jobsToAssign = 0;
  127.                 }
  128.  
  129.                 // Races with the Intelligent trait get bonus production based on the number of professors and scientists
  130.                 // Only unassign them when knowledge is max if the race is not intelligent
  131.                 // Once we've research shotgun sequencing we get boost and soon autoassemble genes so stop unassigning
  132.                 if (!isIntelligentRace() && !isResearchUnlocked("shotgun_sequencing")) {
  133.                     // Don't assign professors if our knowledge is maxed and professors aren't contributing to our temple bonus
  134.                     if (job === state.jobs.Professor && !isResearchUnlocked("indoctrination") && resources.Knowledge.storageRatio > 0.98) {
  135.                         jobsToAssign = 0;
  136.                     }
  137.  
  138.                     // Don't assign scientists if our knowledge is maxed and scientists aren't contributing to our knowledge cap
  139.                     if (job === state.jobs.Scientist && !isResearchUnlocked("scientific_journal") && resources.Knowledge.storageRatio > 0.98) {
  140.                         jobsToAssign = 0;
  141.                     }
  142.                 }
  143.  
  144.                 if (job === state.jobs.CementWorker) {
  145.                     let currentCementWorkers = job.count;
  146.                     log("autoJobs", "jobsToAssign: " + jobsToAssign + ", currentCementWorkers" + currentCementWorkers + ", resources.stone.rateOfChange " + resources.Stone.rateOfChange);
  147.  
  148.                     if (jobsToAssign < currentCementWorkers) {
  149.                         // great, remove workers as we want less than we have
  150.                     } else if (jobsToAssign >= currentCementWorkers && resources.Stone.rateOfChange < 5) {
  151.                         // If we're making less than 5 stone then lets remove a cement worker even if we want more
  152.                         jobsToAssign = job.count - 1;
  153.                     } else if (jobsToAssign > job.count && resources.Stone.rateOfChange > 8) {
  154.                         // If we want more cement workers and we're making more than 8 stone then add a cement worker
  155.                         jobsToAssign = job.count + 1;
  156.                     } else {
  157.                         // We're not making enough stone to add a new cement worker so leave it
  158.                         jobsToAssign = job.count;
  159.                     }
  160.                 }
  161.  
  162.                 if (i === 0) {
  163.                     requiredJobs.push(jobsToAssign);
  164.                     jobAdjustments.push(jobsToAssign - job.count);
  165.                 } else {
  166.                     requiredJobs[j] = jobsToAssign;
  167.                     jobAdjustments[j] = jobsToAssign - job.count;
  168.                 }
  169.  
  170.                 availableEmployees -= jobsToAssign;
  171.  
  172.                 // We have to keep track of craftsmen separately as they have a special max number of total craftsmen
  173.                 if (job.isCraftsman()) {
  174.                     availableCraftsmen -= jobsToAssign;
  175.                 }
  176.  
  177.                 log("autoJobs", "job " + job._originalId +  " has jobsToAssign: " + jobsToAssign + ", availableEmployees: " + availableEmployees + ", availableCraftsmen: " + availableCraftsmen);
  178.             }
  179.  
  180.             // No more workers available
  181.             if (availableEmployees <= 0) {
  182.                 break;
  183.             }
  184.         }
  185.  
  186.         let splitJobs = [];
  187.         if (lumberjackIndex !== -1) splitJobs.push( { jobIndex: lumberjackIndex, job: state.jobs.Lumberjack, weighting: settings.jobLumberWeighting, completed: false } );
  188.         if (quarryWorkerIndex !== -1) splitJobs.push( { jobIndex: quarryWorkerIndex, job: state.jobs.QuarryWorker, weighting: settings.jobQuarryWeighting, completed: false });
  189.         if (scavengerIndex !== -1) splitJobs.push( { jobIndex: scavengerIndex, job: state.jobs.Scavenger, weighting: settings.jobScavengerWeighting, completed: false });
  190.         if (farmerIndex !== -1 && game.global.race.species === "entish") splitJobs.push( { jobIndex: farmerIndex, job: state.jobs.Farmer, weighting: 1, completed: false });
  191.  
  192.         // Balance lumberjacks, quarry workers and scavengers if they are unlocked
  193.         if (splitJobs.length > 0) {
  194.             let minLumberjacks = 0;
  195.             let totalWeighting = 0;
  196.  
  197.             if (isEvilRace() && !isEvilUniverse() && lumberjackIndex !== -1) {
  198.                 // Evil races are a little bit different. Their "umemployed" workers act as both farmers and lumberjacks
  199.                 // We need to keep a minimum number on farming.
  200.                 minLumberjacks = requiredJobs[lumberjackIndex];
  201.             }
  202.  
  203.             // Reduce jobs required down to 0 and add them to the available employee pool so that we can split them according to weightings
  204.             splitJobs.forEach(jobDetails => {
  205.                 availableEmployees += requiredJobs[jobDetails.jobIndex];
  206.                 requiredJobs[jobDetails.jobIndex] = 0;
  207.                 jobAdjustments[jobDetails.jobIndex] = 0 - jobDetails.job.count;
  208.                 totalWeighting += jobDetails.weighting;
  209.             });
  210.  
  211.             // Bring them all up to breakpoint 0 one each at a time
  212.             while (availableEmployees >= 1 && findArrayIndex(splitJobs, "completed", false) != -1) {
  213.                 splitJobs.forEach(jobDetails => {
  214.                     if (availableEmployees <= 0 || requiredJobs[jobDetails.jobIndex] >= jobDetails.job.breakpointEmployees(0, true)) {
  215.                         jobDetails.completed = true;
  216.                         return;
  217.                     }
  218.  
  219.                     requiredJobs[jobDetails.jobIndex]++;
  220.                     jobAdjustments[jobDetails.jobIndex]++;
  221.                     availableEmployees--;
  222.                 });
  223.             }
  224.  
  225.             // Reset completed for next breakpoint
  226.             splitJobs.forEach(jobDetails => { jobDetails.completed = false; });
  227.  
  228.             // Bring them all up to breakpoint 1 one each at a time
  229.             while (availableEmployees >= 1 && findArrayIndex(splitJobs, "completed", false) != -1) {
  230.                 splitJobs.forEach(jobDetails => {
  231.                     if (availableEmployees <= 0 || requiredJobs[jobDetails.jobIndex] >= jobDetails.job.breakpointEmployees(1, true)) {
  232.                         jobDetails.completed = true;
  233.                         return;
  234.                     }
  235.  
  236.                     requiredJobs[jobDetails.jobIndex]++;
  237.                     jobAdjustments[jobDetails.jobIndex]++;
  238.                     availableEmployees--;
  239.                 });
  240.             }
  241.  
  242.             // splitJobs.forEach(jobDetails => {
  243.             //     console.log("3 " + jobDetails.job.name + " required " + requiredJobs[jobDetails.jobIndex] + ", adjustment " + jobAdjustments[jobDetails.jobIndex])
  244.             // });
  245.             //console.log(availableEmployees)
  246.  
  247.             if (availableEmployees > 0) {
  248.                 // Split the remainder in accordance to the given weightings
  249.                 if (isEvilRace() && !isEvilUniverse() && lumberjackIndex !== -1) {
  250.                     // Lumberjacks are special! for evil races they are also farmers so we need to keep a minimum even if the split doens't have that many
  251.                     let lumberjacks = Math.ceil(availableEmployees * settings.jobLumberWeighting / totalWeighting);
  252.                     lumberjacks = Math.max(minLumberjacks - requiredJobs[lumberjackIndex], lumberjacks);
  253.                     requiredJobs[lumberjackIndex] += lumberjacks;
  254.                     jobAdjustments[lumberjackIndex] += lumberjacks;
  255.                     availableEmployees -= lumberjacks;
  256.                 }
  257.  
  258.                 // Perform weighting - need the current available employees to multiply by the weighting
  259.                 let startingAvailableEmployees = availableEmployees;
  260.  
  261.                 splitJobs.forEach(jobDetails => {
  262.                     if (availableEmployees <= 0 || (isEvilRace() && !isEvilUniverse() && jobDetails.job === state.jobs.Lumberjack)) {
  263.                         // We've already dealt with evil lumberjacks above. Those dastardly lumberjacks!
  264.                         return;
  265.                     }
  266.  
  267.                     let workers = Math.ceil(startingAvailableEmployees * jobDetails.weighting / totalWeighting);
  268.                     workers = Math.min(availableEmployees, workers);
  269.                     requiredJobs[jobDetails.jobIndex] += workers;
  270.                     jobAdjustments[jobDetails.jobIndex] += workers;
  271.                     availableEmployees -= workers;
  272.                 });
  273.  
  274.                 // Any leftovers assign to the last job
  275.                 let jobIndex = splitJobs[splitJobs.length - 1].jobIndex;
  276.                 requiredJobs[jobIndex] += availableEmployees;
  277.                 jobAdjustments[jobIndex] += availableEmployees;
  278.                 availableEmployees -= availableEmployees;
  279.             }
  280.         }
  281.  
  282.         if (settings.autoCraftsmen && state.jobs.SheetMetal.isManaged() && settings['craft' + state.jobs.SheetMetal.resource.id]) {
  283.             if (state.cityBuildings.Wardenclyffe.count < 18) {
  284.                 let sheetMetalIndex = jobList.indexOf(state.jobs.SheetMetal);
  285.  
  286.                 if (sheetMetalIndex != -1 && state.cityBuildings.Cottage.count > 10 && state.cityBuildings.Library.count > 15 && state.cityBuildings.CoalMine.count > 8) {
  287.                     let plywoodIndex = jobList.indexOf(state.jobs.Plywood);
  288.                     let brickIndex = jobList.indexOf(state.jobs.Brick);
  289.                     let wroughtIronIndex = jobList.indexOf(state.jobs.WroughtIron);
  290.                     let additionalSheetMetalJobs = 0;
  291.  
  292.                     if (plywoodIndex !== -1 && state.jobs.Plywood.isManaged()) {
  293.                         // add plywood jobs above 1 to sheet metal
  294.                         let plywoodJobs = requiredJobs[plywoodIndex];
  295.  
  296.                         if (plywoodJobs > 1) {
  297.                             requiredJobs[plywoodIndex] = 1;
  298.                             jobAdjustments[plywoodIndex] -= (plywoodJobs - 1);
  299.                             additionalSheetMetalJobs += (plywoodJobs - 1);
  300.                         }
  301.                     }
  302.  
  303.                     if (brickIndex !== -1 && state.jobs.Brick.isManaged()) {
  304.                         // add brick jobs above 1 to sheet metal
  305.                         let brickJobs = requiredJobs[brickIndex];
  306.  
  307.                         if (brickJobs > 1) {
  308.                             requiredJobs[brickIndex] = 1;
  309.                             jobAdjustments[brickIndex] -= (brickJobs - 1);
  310.                             additionalSheetMetalJobs += (brickJobs - 1);
  311.                         }
  312.                     }
  313.  
  314.                     if (wroughtIronIndex !== -1 && state.jobs.WroughtIron.isManaged()) {
  315.                         // add wroughtIron jobs above 1 to sheet metal
  316.                         let wroughtIronJobs = requiredJobs[wroughtIronIndex];
  317.  
  318.                         if (wroughtIronJobs > 1) {
  319.                             requiredJobs[wroughtIronIndex] = 1;
  320.                             jobAdjustments[wroughtIronIndex] -= (wroughtIronJobs - 1);
  321.                             additionalSheetMetalJobs += (wroughtIronJobs - 1);
  322.                         }
  323.                     }
  324.  
  325.                     requiredJobs[sheetMetalIndex] += additionalSheetMetalJobs;
  326.                     jobAdjustments[sheetMetalIndex] += additionalSheetMetalJobs;
  327.                 }
  328.             }
  329.         }
  330.  
  331.         for (let i = 0; i < jobAdjustments.length; i++) {
  332.             let adjustment = jobAdjustments[i];
  333.             if (adjustment < 0) {
  334.                 // I have no clue how this is undefined... but it can be when the script first starts and playing a carnivore / evil race
  335.                 // May have fixed it by moving the evil race / hunter race checks to update state in the automate function
  336.                 if (jobList[i] !== undefined) {
  337.                     jobList[i].removeWorkers(-1 * adjustment);
  338.                     log("autoJobs", "Adjusting job " + jobList[i]._originalId + " down by " + adjustment);
  339.                 }
  340.             }
  341.         }
  342.  
  343.         for (let i = 0; i < jobAdjustments.length; i++) {
  344.             let adjustment = jobAdjustments[i];
  345.             if (adjustment > 0) {
  346.                 if (jobList[i] !== undefined) {
  347.                     jobList[i].addWorkers(adjustment);
  348.                     log("autoJobs", "Adjusting job " + jobList[i]._originalId + " up by " + adjustment);
  349.                 }
  350.             }
  351.         }
  352.  
  353.         state.lastPopulationCount = resources.Population.currentQuantity;
  354.         state.lastFarmerCount = state.jobs.Farmer.count;
  355.     }
Advertisement
Add Comment
Please, Sign In to add comment