2021-08-23 18:53:20 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2021-09-16 13:52:38 +02:00
|
|
|
"os"
|
2021-08-23 18:53:20 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-09-16 13:52:38 +02:00
|
|
|
var (
|
|
|
|
surfaces, modifier, diceThrows, attacks int
|
|
|
|
advantage, disadvantage bool
|
|
|
|
)
|
|
|
|
|
2022-07-29 12:26:50 +02:00
|
|
|
type Color string
|
|
|
|
|
|
|
|
const (
|
2022-07-29 15:04:24 +02:00
|
|
|
ColorBlack Color = "\u001b[30m"
|
|
|
|
ColorRed = "\u001b[31m"
|
|
|
|
ColorGreen = "\u001b[32m"
|
|
|
|
ColorYellow = "\u001b[33m"
|
|
|
|
ColorBlue = "\u001b[34m"
|
|
|
|
ColorMagenta = "\u001b[35m"
|
|
|
|
ColorReset = "\u001b[0m"
|
2022-07-29 12:26:50 +02:00
|
|
|
)
|
|
|
|
|
2021-08-23 18:53:20 +02:00
|
|
|
func main() {
|
2021-09-16 11:59:45 +02:00
|
|
|
|
2021-08-23 18:53:20 +02:00
|
|
|
rand.Seed(time.Now().Unix())
|
2021-09-16 13:52:38 +02:00
|
|
|
ParseFlags()
|
2021-09-16 11:59:45 +02:00
|
|
|
|
2021-09-16 13:52:38 +02:00
|
|
|
switch {
|
|
|
|
case advantage:
|
2022-07-29 15:04:24 +02:00
|
|
|
if modifier != 0 {
|
|
|
|
fmt.Printf("%sRolling 1d20 + %d with advantage...%s\n", Color(ColorGreen), modifier, Color(ColorReset))
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%sRolling 1d20 with advantage...%s\n", Color(ColorGreen), Color(ColorReset))
|
|
|
|
}
|
|
|
|
fmt.Printf("\t%s%d%s\n", Color(ColorGreen), Advantage(SimpleCast(), SimpleCast()), Color(ColorReset))
|
|
|
|
|
2021-09-16 13:52:38 +02:00
|
|
|
case disadvantage:
|
2022-07-29 15:04:24 +02:00
|
|
|
if modifier != 0 {
|
|
|
|
fmt.Printf("%sRolling 1d20 + %d with disadvantage...%s\n", Color(ColorRed), modifier, Color(ColorReset))
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%sRolling 1d20 with disadvantage...%s\n", Color(ColorRed), Color(ColorReset))
|
|
|
|
}
|
|
|
|
fmt.Printf("\t%s%d%s\n", Color(ColorRed), Disadvantage(SimpleCast(), SimpleCast()), Color(ColorReset))
|
|
|
|
|
2021-09-16 13:52:38 +02:00
|
|
|
case attacks < 1:
|
2022-07-29 15:04:24 +02:00
|
|
|
fmt.Println("Attack amount cannot be below 1")
|
2021-09-16 13:52:38 +02:00
|
|
|
os.Exit(1)
|
2022-07-29 15:04:24 +02:00
|
|
|
|
2021-09-16 13:52:38 +02:00
|
|
|
case attacks > 1:
|
|
|
|
for i := 0; i < attacks; i++ {
|
2022-07-29 15:04:24 +02:00
|
|
|
fmt.Printf("%sAttack %d:%s\n", Color(ColorBlue), i+1, Color(ColorReset))
|
2022-07-29 11:24:46 +02:00
|
|
|
Cast(surfaces, diceThrows)
|
2021-09-16 11:59:45 +02:00
|
|
|
}
|
2021-09-16 13:52:38 +02:00
|
|
|
default:
|
2022-07-29 11:24:46 +02:00
|
|
|
Cast(surfaces, diceThrows)
|
2021-08-28 12:36:16 +02:00
|
|
|
}
|
2021-08-23 18:53:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-16 13:52:38 +02:00
|
|
|
func ParseFlags() {
|
2022-07-29 12:26:50 +02:00
|
|
|
flag.IntVar(&surfaces, "surfaces", 20, "Use to specify die surfaces, does not apply to advantage and disadvantage")
|
|
|
|
flag.IntVar(&surfaces, "s", 20, "Use to specify die surfaces, defaults to 20")
|
|
|
|
|
|
|
|
flag.IntVar(&diceThrows, "throws", 1, "Specify amount of dice to cast")
|
|
|
|
flag.IntVar(&diceThrows, "c", 1, "Specify amount of dice to cast")
|
|
|
|
|
|
|
|
flag.IntVar(&modifier, "modifier", 0, "Add modifier to result of rolls")
|
|
|
|
flag.IntVar(&modifier, "m", 0, "Add modifier to result of rolls")
|
|
|
|
|
|
|
|
flag.IntVar(&attacks, "attacks", 1, "Roll a set rules multiple times, does not apply to advantage and disadvantage")
|
|
|
|
flag.IntVar(&attacks, "a", 1, "Roll a set rules multiple times, does not apply to advantage and disadvantage")
|
2021-09-16 13:52:38 +02:00
|
|
|
|
2022-07-29 12:26:50 +02:00
|
|
|
flag.BoolVar(&advantage, "advantage", false, "Roll with advantage")
|
|
|
|
flag.BoolVar(&disadvantage, "disadvantage", false, "Roll with disadvantage")
|
2021-09-16 13:52:38 +02:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2022-07-29 11:24:46 +02:00
|
|
|
func Cast(dieSurfaces, castAmount int) {
|
2021-08-23 18:53:20 +02:00
|
|
|
var (
|
|
|
|
casts []int
|
|
|
|
cast, total int
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < castAmount; i++ {
|
|
|
|
cast = rand.Intn(dieSurfaces) + 1
|
|
|
|
casts = append(casts, cast)
|
|
|
|
total += cast
|
|
|
|
}
|
2021-08-23 23:19:01 +02:00
|
|
|
|
2022-07-29 13:30:48 +02:00
|
|
|
if modifier != 0 {
|
|
|
|
fmt.Printf("%sRolling %dd%d + %d...\n%s", ColorYellow, diceThrows, surfaces, modifier, ColorReset)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%sRolling %dd%d...\n%s", ColorYellow, diceThrows, surfaces, ColorReset)
|
|
|
|
}
|
|
|
|
|
2022-07-29 15:04:24 +02:00
|
|
|
if castAmount > 1 {
|
|
|
|
fmt.Println(Color(ColorMagenta), "\tIndividual rolls:", casts, Color(ColorReset))
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:59:45 +02:00
|
|
|
if modifier != 0 {
|
2022-07-29 12:26:50 +02:00
|
|
|
fmt.Println("\tWithout modifier:", total)
|
2022-07-29 15:04:24 +02:00
|
|
|
fmt.Println(Color(ColorGreen), "\tWith modifier:", total+modifier, Color(ColorReset))
|
2021-09-16 11:59:45 +02:00
|
|
|
} else {
|
2022-07-30 17:27:26 +02:00
|
|
|
fmt.Printf("%s\t%d%s\n", Color(ColorGreen), total, Color(ColorReset))
|
2021-09-16 11:59:45 +02:00
|
|
|
}
|
2021-08-23 18:53:20 +02:00
|
|
|
}
|
2021-09-16 13:52:38 +02:00
|
|
|
|
|
|
|
func SimpleCast() int {
|
2022-07-29 11:24:46 +02:00
|
|
|
|
|
|
|
var cast = rand.Intn(20) + 1
|
|
|
|
|
2022-07-29 15:04:24 +02:00
|
|
|
if modifier != 0 {
|
|
|
|
fmt.Println("Without modifier:", cast)
|
|
|
|
}
|
2022-07-29 11:24:46 +02:00
|
|
|
return cast + modifier
|
2021-09-16 13:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Advantage(x, y int) int {
|
2022-07-29 15:04:24 +02:00
|
|
|
fmt.Println("\tx:", x)
|
|
|
|
fmt.Println("\tY:", y)
|
2021-09-16 13:52:38 +02:00
|
|
|
if x > y {
|
|
|
|
return x
|
|
|
|
} else {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Disadvantage(x, y int) int {
|
2022-07-29 15:04:24 +02:00
|
|
|
fmt.Println("\tx:", x)
|
|
|
|
fmt.Println("\tY:", y)
|
2021-09-16 13:52:38 +02:00
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
} else {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
}
|