Dilnu

CF rough chance calculations

Nov 18th, 2020 (edited)
640
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let chanceToRoll = [.0716,  .0911,  .1529,  .2731,  .0286,  .3521, 0,   .0306];
  2.  
  3. let nothingObtained = [0, 0, 0, 0, 0, 0, 0, 0];
  4.  
  5. function equals (a, b) {
  6.     for (let i = 0; i < 8; ++i)
  7.     {
  8.         if (a[i] != b[i]) return false;
  9.     }
  10.     return true;
  11. }
  12.  
  13. function available(state, total) {
  14.     let available = total;
  15.     for (let i = 0; i < 8; ++i)
  16.     {
  17.         available -= (i + 1) * state[i];
  18.     }
  19.     return available;
  20. }
  21.  
  22. function addToData(currentData, pair) {
  23.     for (let index = 0; index < currentData.length; ++index) {
  24.         if (equals(currentData[index][0], pair[0])) {
  25.             currentData[index][1] += pair[1];
  26.             return;
  27.         }
  28.     }
  29.     currentData.push(pair);
  30. }
  31.  
  32. function calculate(startingPoints, runUntil, rank) {
  33.     let data = [[nothingObtained, 1]];
  34.     for (let total = startingPoints + 1; total <= runUntil; total++) {
  35.         let newData = [];
  36.         for(let pair of data) {
  37.             let state = pair[0];
  38.             let baseChance = pair[1];
  39.             let toSpend = available(pair[0], total);
  40.             for (let index = 0; index < toSpend; index++) {
  41.                 let newState = pair[0].slice(0, 8);
  42.                 newState[index]++;
  43.                 addToData(newData, [newState, baseChance * chanceToRoll[index]]);
  44.             }
  45.             let chanceToMiss = baseChance * chanceToRoll.slice(toSpend, 8).reduce((a, b) => a+b, 0);
  46.             if (chanceToMiss > 0) {
  47.                 addToData(newData, [state, chanceToMiss]);
  48.             }
  49.         }
  50.         data = newData;
  51.     }
  52.     let chanceToObtain = 0;
  53.     for(let pair of data) {
  54.         let state = pair[0];
  55.         let chance = pair[1];
  56.         if (state[rank - 1] > 0) {
  57.             chanceToObtain += chance;
  58.         }
  59.     }
  60.     return chanceToObtain;
  61. }
  62.  
  63. function makeColumn(startingPoints, max, rank) {
  64.     let output = [];
  65.     for(let i = startingPoints+1; i <=max; ++i) {
  66.         output.push(calculate(startingPoints, i, rank));
  67.     }
  68.     return output.join('\n');
  69. }
RAW Paste Data