G_lander

CCRandomizer

Apr 1st, 2020
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.     let shuffle = function(array, power) {
  3.         let currentIndex = array.length - 1,
  4.             temporaryValue,
  5.             randomIndex,
  6.             distance
  7.         while (0 !== currentIndex) {
  8.             // Get new index
  9.             randomIndex = Math.floor(Math.random() * currentIndex)
  10.             //Find distance
  11.             distance = randomIndex - currentIndex
  12.             //Nerf distance
  13.             distance *= power
  14.             //Reapply it
  15.             randomIndex = Math.round(currentIndex + distance)
  16.             temporaryValue = array[currentIndex]
  17.             array[currentIndex] = array[randomIndex]
  18.             array[randomIndex] = temporaryValue
  19.             currentIndex -= 1
  20.         }
  21.         return array
  22.     }
  23.     let clone = function(obj) {
  24.         let copy = new obj.constructor()
  25.         for (const attr in obj) {
  26.             if (obj.hasOwnProperty(attr)) {
  27.                 if (obj[attr] instanceof Object) {
  28.                     copy[attr] = clone(obj[attr])
  29.                 } else {
  30.                     copy[attr] = obj[attr]
  31.                 }
  32.             }
  33.         }
  34.         return copy
  35.     }
  36.  
  37.     window.randomizeArray = function(input, params) {
  38.         params.enforce = params.enforce || {}
  39.         let values = {}
  40.         let empty = Symbol("empty")
  41.         //Assume all objects have the same prototype
  42.         let prototype = input[0].__proto__
  43.         input.forEach(element => {
  44.             //Scan for all properties
  45.             Object.keys(element).forEach(element => {
  46.                 values[element] = []
  47.             })
  48.         })
  49.         input.forEach(element => {
  50.             //Actually add values to the array
  51.             Object.keys(values).forEach(prop => {
  52.                 values[prop].push(element[prop] !== undefined ? element[prop] : empty)
  53.             })
  54.         })
  55.         Object.keys(values).forEach(prop => {
  56.             values[prop] = shuffle(values[prop], params.power)
  57.         })
  58.         let output = []
  59.         input.forEach((_value, i) => {
  60.             output[i] = {}
  61.             output[i].__proto__ = prototype
  62.             Object.keys(values).forEach(prop => {
  63.                 let add = values[prop].shift()
  64.                 if (add !== empty) output[i][prop] = add
  65.             })
  66.             Object.keys(params.enforce).forEach(key => {
  67.                 output[i][key] =
  68.                     params.enforce[key] instanceof Function
  69.                         ? params.enforce[key](output[i])
  70.                         : params.enforce[key]
  71.             })
  72.         })
  73.         return output
  74.     }
  75. }
  76. Game.Tiers[0] = {}
  77. window.CCR = {
  78.     upgrades: function(power) {
  79.         Game.UpgradesById = randomizeArray(Game.UpgradesById, {
  80.             enforce: {
  81.                 buildingTie1: {
  82.                     basePrice:
  83.                         Game.ObjectsById[Game.ObjectsById.length - 1].price * Math.random()
  84.                 },
  85.                 buildingTie2: {
  86.                     basePrice:
  87.                         Game.ObjectsById[Game.ObjectsById.length - 1].price * Math.random()
  88.                 }
  89.             },
  90.             power: power / 255
  91.         })
  92.         ;[...Game.UpgradesById].forEach(val => {
  93.             Game.UpgradesById[val.id] = val
  94.             Game.Upgrades[val.name] = val
  95.         })
  96.     },
  97.     achievements: function(power) {
  98.         Game.AchievementsById = randomizeArray(Game.AchievementsById, {
  99.             power: power / 255
  100.         })
  101.         ;[...Game.AchievementsById].forEach(val => {
  102.             Game.AchievementsById[val.id] = val
  103.             Game.Achievements[val.name] = val
  104.         })
  105.     },
  106.     milk: function(power) {
  107.         Game.Milks = randomizeArray(Game.Milks, { power: power / 255 })
  108.     }
  109. }
  110. function randomizePrompt() {
  111.     Game.timedout = true
  112.     window.CCR.randomize = () => {
  113.         const seed = document.getElementById("seed").value
  114.         Math.seedrandom(seed)
  115.         const power = document.getElementById("power").value
  116.         const checkboxes = ["Upgrades", "Achievements", "Milk"]
  117.         checkboxes.forEach(val => {
  118.             const randomize = document.getElementById(`randomize${val}`).checked
  119.             if (randomize) CCR[val.toLowerCase()](power)
  120.         })
  121.         Game.timedout = false
  122.         Game.Loop()
  123.     }
  124.     Game.Prompt(
  125.         `Seed:
  126. <input placeholder="Enter seed here" id="seed"><br>
  127. Power(0-255):
  128. <input type="number" min="0" max="255" id="power" placeholder="Enter power here"><br>
  129. Randomize:<br><br>
  130. Upgrades<input type="checkbox" id="randomizeUpgrades">
  131. Achievements<input type="checkbox" id="randomizeAchievements">
  132. Buildings<input type="checkbox" id ="randomizeBuildings">
  133. Milk<input type="checkbox" id="randomizeMilk">
  134. `,
  135.         [["Let's go", "CCR.randomize();Game.ClosePrompt()"], "Nevermind"]
  136.     )
  137. }
  138. Game.UpdateMenu = new Proxy(Game.UpdateMenu, {
  139.     apply: (target, thisArg, argumentsList) => {
  140.         target.apply(thisArg, argumentsList)
  141.         if (Game.onMenu === "prefs") {
  142.             //Create master div
  143.             const div = document.createElement("div")
  144.             div.classList.add("listing")
  145.             //Create title
  146.             const title = document.createElement("div")
  147.             title.innerText = "Randomizer"
  148.             title.classList.add("title")
  149.             div.appendChild(title)
  150.             //Create our button
  151.             const button = document.createElement("a")
  152.             button.innerText = "Randomize!"
  153.             button.classList.add("option")
  154.             button.classList.add("neato")
  155.             button.addEventListener("click", randomizePrompt)
  156.             div.appendChild(button)
  157.             // Create the label
  158.             const label = document.createElement("label")
  159.             label.innerText = "Randomizes everything!"
  160.             div.appendChild(label)
  161.             const options = document
  162.                 .getElementById("menu")
  163.                 .getElementsByClassName("subsection")[0]
  164.             options.insertBefore(div, options.lastChild)
  165.         }
  166.     }
  167. })
  168. Game.Notify("CCRandomizer loaded!", "Go to the bottom of settings to actually randomize things!", [-1,0])
  169. Game.UpdateMenu()
Advertisement
Add Comment
Please, Sign In to add comment