m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/dice.js
blob: 5a484b0aada3230bdc22da34a04eb87eec2120a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const greaterThanOrEqual = (a, b) => a >= b
const lessThanOrEqual = (a, b) => a <= b

const constant = n => () => [() => n]

const pool = (die) => {
  return die().map((d) => d())
}

const roll = (die) => {
  return pool(die).reduce((a, b) => (a + b), 0)
}

const d = (number, sides) => {
  return () => {
    let pool = []

    const currentNumber = roll(number)
    const currentSides = roll(sides)

    for (let i = 0; i < currentNumber; i++) {
      pool.push(() => (1 + Math.floor(Math.random() * currentSides)))
    }

    return pool
  }
}

const add = (die1, die2) => {
  return () => {
    return die1().concat(die2())
  }
}

const negative = (die) => {
  return () => {
    return die().map(die => () => (-die()))
  }
}

const subtract = (die1, die2) => {
  return () => {
    return die1().concat(negative(die2)())
  }
}

const multiply = (die1, die2) => {
  return () => {
    return [() => roll(die1) * roll(die2)]
  }
}

const divide = (die1, die2) => {
  return () => {
    return [() => Math.floor(roll(die1) / roll(die2))]
  }
}

const bonusAdd = (die1, die2) => {
  return () => {
    return die1().map(die => {
      return () => die() + roll(die2)
    })
  }
}

const bonusSubtract = (die1, die2) => {
  const negative2 = negative(die2)
  return () => {
    return die1().map(die => {
      return () => die() + roll(negative2)
    })
  }
}

const explode = (comparison, die1, die2) => {
  return () => {
    const explodeOn = roll(die1)
    return die2().map(die => () => {
      let lastRoll = die()
      let total = lastRoll
      while (comparison(lastRoll, explodeOn)) {
        lastRoll = die()
        total += lastRoll
      }
      return total
    })
  }
}

const explodeAbove = (die1, die2) => {
  return explode(greaterThanOrEqual, die1, die2)
}

const explodeUnder = (die1, die2) => {
  return explode(lessThanOrEqual, die1, die2)
}



const keep = (order, die1, die2) => {
  return () => {
    const numberToKeep = roll(die1)
    let rolls = die2().map(die => [die, die()])
    rolls.sort(order)
    return rolls.slice(0, numberToKeep).map(pair => {
      let returnedOriginal = false
      return () => {
        if (!returnedOriginal) {
          returnedOriginal = true
          return pair[1]
        } else {
          return pair[0]()
        }
      }
    })
  }
}

const keepHigh = (die1, die2) => {
  return keep((a, b) => (b[1] - a[1]), die1, die2)
}

const keepLow = (die1, die2) => {
  return keep((a, b) => (a[1] - b[1]), die1, die2)
}

const again = (comparison, die1, die2) => {
  return () => {
    const againOn = roll(die1)
    let rolls = []
    let rollAgain = []

    const rollDie = (die) => {
      const roll = die()

      if (comparison(roll, againOn)) {
        rollAgain.push(die)
      }

      let returnedOriginal = false
      rolls.push(() => {
        if (!returnedOriginal) {
          returnedOriginal = true
          return roll
        } else {
          return die()
        }
      })
    }

    die2().forEach(rollDie)

    while (rollAgain.length > 0) {
      const oldRollAgain = rollAgain
      rollAgain = []
      oldRollAgain.forEach(rollDie)
    }

    return rolls
  }
}

const againAbove = (die1, die2) => {
  return again(greaterThanOrEqual, die1, die2)
}

const againUnder = (die1, die2) => {
  return again(lessThanOrEqual, die1, die2)
}

const threshold = (comparison, die1, die2) => {
  return () => {
    const cutoff = roll(die1)

    return die2().map(die => {
      return () => {
        if (comparison(die(), cutoff)) {
          return 1
        } else {
          return 0
        }
      }
    })
  }
}

const thresholdHigh = (die1, die2) => {
  return threshold(greaterThanOrEqual, die1, die2)
}

const thresholdLow = (die1, die2) => {
  return threshold(lessThanOrEqual, die1, die2)
}

const repeat = (die1, die2) => {
  return () => {
    const times = roll(die2)

    let results = []
    for (let i = 0; i < times; i++) {
      results = results.concat(die1())
    }

    return results
  }
}

exports.pool = pool
exports.roll = roll
exports.constant = constant
exports.d = d
exports.add = add
exports.subtract = subtract
exports.multiply = multiply
exports.divide = divide
exports.bonusAdd = bonusAdd
exports.bonusSubtract = bonusSubtract
exports.negative = negative
exports.explodeAbove = explodeAbove
exports.explodeUnder = explodeUnder
exports.keepHigh = keepHigh
exports.keepLow = keepLow
exports.againAbove = againAbove
exports.againUnder = againUnder
exports.thresholdHigh = thresholdHigh
exports.thresholdLow = thresholdLow
exports.repeat = repeat