You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
833 B
37 lines
833 B
package helpers
|
|
|
|
import (
|
|
"mazeratsgen/internal/dice"
|
|
"path"
|
|
"runtime"
|
|
)
|
|
|
|
func LocalFile(filepath string) string {
|
|
_, thisFile, _, _ := runtime.Caller(1)
|
|
return path.Join(path.Dir(thisFile), filepath)
|
|
}
|
|
|
|
type genFunc func([2]int) string
|
|
|
|
func GenUniqueItems(total int, fn genFunc, seed int64) []string {
|
|
roller := dice.NewRoller(seed)
|
|
items := []string{}
|
|
|
|
// Generate initial items
|
|
for i := 0; i <= total; i++ {
|
|
items = append(items, fn(roller.TableRoll()))
|
|
}
|
|
|
|
// Walk items and check if they match any other item. If they do then replace
|
|
// it with a fresh item. This doesn't guarantee unique items, but prevents
|
|
// Denial of Service attacks (a bit)
|
|
for i, item := range items {
|
|
for x := 0; x < len(items); x++ {
|
|
if items[x] == item {
|
|
items[i] = fn(roller.TableRoll())
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return items
|
|
}
|
|
|