Talk:Siege

From Imperator Wiki
Jump to navigation Jump to search

Here is the source code for simulation which was used to get average capture times. Wethospu (talk) 14:55, 20 October 2019 (UTC)

   import { Random, MersenneTwister19937, createEntropy } from 'random-js'
   const percentile = require('percentile')
   const min = -5
   const max = 10
   const iterations = 1000000
   const seed = createEntropy()[0]
   const engine = MersenneTwister19937.seed(seed)
   const rng = new Random(engine)
   for (var delta = min; delta <= max; delta++) {
     let garrison_low = 0
     let diseases = 0
     let assaults = 0
     const results = []
     let total = 0
     let intervals_saved = 0
     for (var iteration = 0; iteration < iterations; iteration++) {
       let accumulation = 0
       let roll = 1
       let breach = false
       let garrison = 1.0
       let assaulted = false
       for (; roll < 10000; roll++) {
         const roll = rng.integer(1, 14)
         const modded = roll + accumulation + delta
         if (garrison < 0.1) {
           garrison_low++
           break
         }
         if (modded > 19) // Surrender
           break
         else if (roll === 14) { // Breach
           accumulation += 2
           garrison *= 0.95
         }
         else if (modded > 15) { // Desert
           accumulation += 2
           garrison *= 0.9
         }
         else if (modded > 13) { // Water
           accumulation += 3
           garrison *= 0.95
         }
         else if (modded > 11) { // Food
           accumulation += 2
           garrison *= 0.97
         }
         else if (modded > 4) { // Supplies
           accumulation += 1
           garrison *= 0.99
         }
         else if (modded == 1) // Disease
           diseases += 1
         // Status Quo
         accumulation = Math.min(accumulation, 11)
         if (!assaulted && garrison <= 0.50) {
           assaulted = true
           assaults++
         }
         if (!assaulted && breach && garrison <= 0.75) {
           assaulted = true
           assaults++
         }
         if (assaulted)
           intervals_saved++
       }
       results.push(roll)
       total += roll
     }
     console.log('| ' + delta + ' || ' + total / iterations + ' || ' + percentile(5, results) + ' || ' + percentile(25, results) + ' || ' + percentile(50, results) + ' || ' + percentile(75, results) + ' || ' + percentile(95, results) + ' || ' + diseases / iterations + ' || ' + assaults / iterations + ' || ' + intervals_saved / assaults +  ' || ' + intervals_saved / iterations + ' || ' + garrison_low / iterations)
   }