-
Notifications
You must be signed in to change notification settings - Fork 6
/
roll_func.go
442 lines (374 loc) · 9.23 KB
/
roll_func.go
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package dicescript
import (
"errors"
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
"golang.org/x/exp/rand"
)
func getSource() *rand.PCGSource {
s := &rand.PCGSource{}
s.Seed(uint64(time.Now().UnixMilli()))
return s
}
var randSource = getSource()
func _roll32(src *rand.PCGSource, dicePoints int) int {
// 注: int的长度至少为32位,也可以高于此数,此处只是当作32位处理
if dicePoints > math.MaxInt32-1 {
return 0
}
v := uint32(src.Uint64() >> 32)
n := uint32(dicePoints)
// 下面这段取整代码来自 golang 的 exp/rand
if n&(n-1) == 0 { // n is power of two, can mask
return int(v&(n-1) + 1)
}
if v > math.MaxUint32-n { // Fast check.
ceiling := math.MaxUint32 - math.MaxUint32%n
for v >= ceiling {
v = uint32(src.Uint64() >> 32)
}
}
return int(v%n + 1)
}
func _roll64(src *rand.PCGSource, dicePoints int64, mod int) int64 {
if dicePoints > math.MaxInt64-1 {
return 0
}
v := src.Uint64()
n := uint64(dicePoints)
// 下面这段取整代码来自 golang 的 exp/rand
if n&(n-1) == 0 { // n is power of two, can mask
return int64(v&(n-1) + 1)
}
if v > math.MaxUint64-n { // Fast check.
ceiling := math.MaxUint64 - math.MaxUint64%n
for v >= ceiling {
v = src.Uint64()
}
}
return int64(v%n + 1)
}
func Roll(src *rand.PCGSource, dicePoints IntType, mod int) IntType {
if dicePoints == 0 {
return 0
}
if mod == -1 {
return 1
}
if mod == 1 {
return dicePoints
}
if src == nil {
src = randSource
}
// 大部分情况会走这个分支
if IntTypeSize == 8 {
return IntType(_roll64(src, int64(dicePoints), mod))
}
return IntType(_roll32(src, int(dicePoints)))
}
func wodCheck(e *Context, addLine IntType, pool IntType, points IntType, threshold IntType) bool {
// makeE6 := func() {
// e.Error = errors.New("E6: 类型错误")
// }
if pool < 1 || pool > 20000 {
e.Error = errors.New("E7: 非法数值, 骰池范围是1到20000")
return false
}
if addLine != 0 && addLine < 2 {
e.Error = errors.New("E7: 非法数值, 加骰线必须为0[不加骰],或≥2")
return false
}
if points < 1 {
e.Error = errors.New("E7: 非法数值, 面数至少为1")
return false
}
if threshold < 1 {
e.Error = errors.New("E7: 非法数值, 成功线至少为1")
return false
}
return true
}
// RollWoD 返回: 成功数,总骰数,轮数,细节
func RollWoD(src *rand.PCGSource, addLine IntType, pool IntType, points IntType, threshold IntType, isGE bool, mode int) (IntType, IntType, IntType, string) {
var details []string
addTimes := 1
isShowDetails := pool < 15
allRollCount := pool
successCount := IntType(0)
for times := 0; times < addTimes; times++ {
addCount := IntType(0)
var detailsOne []string
for i := IntType(0); i < pool; i++ {
var reachSuccess bool
var reachAddRound bool
one := Roll(src, points, mode)
if addLine != 0 {
reachAddRound = one >= addLine
}
if isGE {
reachSuccess = one >= threshold
} else {
reachSuccess = one <= threshold
}
if reachSuccess {
successCount += 1
}
if reachAddRound {
addCount += 1
}
if isShowDetails {
baseText := strconv.FormatInt(int64(one), 10)
if reachSuccess {
baseText += "*"
}
if reachAddRound {
baseText = "<" + baseText + ">"
}
detailsOne = append(detailsOne, baseText)
}
}
allRollCount += addCount
// 有加骰,再骰一次
if addCount > 0 {
addTimes += 1
pool = addCount
}
if allRollCount > 100 {
// 多于100,清空
isShowDetails = false
details = details[:0]
}
if isShowDetails {
details = append(details, "{"+strings.Join(detailsOne, ",")+"}")
}
}
// 生成detail文本
roundsText := ""
if addTimes > 1 {
roundsText = fmt.Sprintf(" 轮数:%d", addTimes)
}
detailText := ""
if len(details) > 0 {
detailText = " " + strings.Join(details, ",")
}
detailText = fmt.Sprintf("成功%d/%d%s%s", successCount, allRollCount, roundsText, detailText)
// 成功数,总骰数,轮数,细节
return successCount, allRollCount, IntType(addTimes), detailText
}
func doubleCrossCheck(ctx *Context, addLine, pool, points IntType) bool {
if pool < 1 || pool > 20000 {
ctx.Error = errors.New("E7: 非法数值, 骰池范围是1到20000")
return false
}
if addLine < 2 {
ctx.Error = errors.New("E7: 非法数值, 加骰线必须大于等于2")
return false
}
if points < 1 {
ctx.Error = errors.New("E7: 非法数值, 面数至少为1")
return false
}
return true
}
func RollDoubleCross(src *rand.PCGSource, addLine IntType, pool IntType, points IntType, mode int) (IntType, IntType, IntType, string) {
var details []string
addTimes := 1
isShowDetails := pool < 15
allRollCount := pool
resultDice := IntType(0)
for times := 0; times < addTimes; times++ {
addCount := IntType(0)
detailsOne := []string{}
maxDice := IntType(0)
for i := IntType(0); i < pool; i++ {
one := Roll(src, points, mode)
if one > maxDice {
maxDice = one
}
reachAddRound := one >= addLine
if reachAddRound {
addCount += 1
maxDice = 10
}
if isShowDetails {
baseText := strconv.FormatInt(int64(one), 10)
if reachAddRound {
baseText = "<" + baseText + ">"
}
detailsOne = append(detailsOne, baseText)
}
}
resultDice += maxDice
allRollCount += addCount
// 有加骰,再骰一次
if addCount > 0 {
addTimes += 1
pool = addCount
}
if allRollCount > 100 {
// 多于100,清空
isShowDetails = false
details = details[:0]
}
if isShowDetails {
details = append(details, "{"+strings.Join(detailsOne, ",")+"}")
}
}
// 详细信息
detailText := ""
if len(details) > 0 {
detailText = " " + strings.Join(details, ",")
}
roundsText := ""
if addTimes > 1 {
roundsText = fmt.Sprintf(" 轮数:%d", addTimes)
}
var lastDetail string
if resultDice == 1 {
lastDetail = fmt.Sprintf("大失败 出目%d/%d%s%s", resultDice, allRollCount, roundsText, detailText)
} else {
lastDetail = fmt.Sprintf("出目%d/%d%s%s", resultDice, allRollCount, roundsText, detailText)
}
// 成功数,总骰数,轮数,细节
return resultDice, allRollCount, IntType(addTimes), lastDetail
}
// RollCommon (times)d(dicePoints)kl(lowNum) 或 (times)d(dicePoints)kh(highNum)
func RollCommon(src *rand.PCGSource, times, dicePoints IntType, diceMin, diceMax *IntType, isKeepLH, lowNum, highNum IntType, mode int) (IntType, string) {
var nums []IntType
for i := IntType(0); i < times; i += 1 {
die := Roll(src, dicePoints, mode)
if diceMax != nil {
if die > *diceMax {
die = *diceMax
}
}
if diceMin != nil {
if die < *diceMin {
die = *diceMin
}
}
nums = append(nums, die)
}
// 默认pickNum为全部,稍后由kh或kl做削减
pickNum := times
if isKeepLH != 0 {
// 为1对应取低个数,为2对应取高个数,3为丢弃低个数,4为丢弃高个数
if isKeepLH == 1 || isKeepLH == 4 {
sort.Slice(nums, func(i, j int) bool { return nums[i] < nums[j] }) // 从小到大
} else {
sort.Slice(nums, func(i, j int) bool { return nums[i] > nums[j] }) // 从大到小
}
switch isKeepLH {
case 1, 3:
pickNum = lowNum
case 2, 4:
pickNum = highNum
}
if isKeepLH > 2 {
pickNum = times - pickNum
}
// clamp
if pickNum < 0 {
pickNum = 0
}
if pickNum > times {
pickNum = times
}
}
num := IntType(0)
for i := IntType(0); i < pickNum; i++ {
// 当取数大于上限 跳过
if i >= IntType(len(nums)) {
continue
}
num += nums[i]
}
// details
var text string
if pickNum == times {
text = ""
for i := 0; i < len(nums); i++ {
text += fmt.Sprintf("%d+", nums[i])
}
if len(nums) > 0 {
text = text[:len(text)-1]
}
} else {
text = "{"
for i := IntType(0); i < IntType(len(nums)); i++ {
if i == pickNum {
text += "| "
}
text += fmt.Sprintf("%d ", nums[i])
}
if len(nums) > 0 {
text = text[:len(text)-1]
}
text += "}"
}
return num, text
}
func RollCoC(src *rand.PCGSource, isBonus bool, diceNum IntType, mode int) (IntType, string) {
diceResult := Roll(src, 100, mode)
diceTens := diceResult / 10
diceUnits := diceResult % 10
var nums []string
diceMin := diceTens
diceMax := diceTens
num10Exists := false
for i := IntType(0); i < diceNum; i++ {
n := Roll(src, 10, mode)
if n == 10 {
num10Exists = true
nums = append(nums, "0")
continue
} else {
nums = append(nums, strconv.FormatInt(int64(n), 10))
}
if n < diceMin {
diceMin = n
}
if n > diceMax {
diceMax = n
}
}
if isBonus {
// 如果个位数不是0,那么允许十位为0
if diceUnits != 0 && num10Exists {
diceMin = 0
}
newVal := diceMin*10 + diceUnits
lastDetail := fmt.Sprintf("(D100=%d,奖励%s)", diceResult, strings.Join(nums, " "))
return newVal, lastDetail
} else {
// 如果个位数为0,那么允许十位为10
if diceUnits == 0 && num10Exists {
diceMax = 10
}
newVal := diceMax*10 + diceUnits
lastDetail := fmt.Sprintf("(D100=%d,惩罚%s)", diceResult, strings.Join(nums, " "))
return newVal, lastDetail
}
}
func RollFate(src *rand.PCGSource, mode int) (IntType, string) {
detail := ""
sum := IntType(0)
for i := 0; i < 4; i++ {
n := Roll(src, 3, mode) - 2
sum += n
switch n {
case -1:
detail += "-"
case 0:
detail += "0"
case +1:
detail += "+"
}
}
return sum, detail
}