package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
// Where the output will be stored
const filename = "CharacterAppearanceData.txt"
// Color struct with R, G, B as int and A as float64
type C struct {
R, G, B int
A float64
}
// Generate random color
func rC(rMin, rMax, gMin, gMax, bMin, bMax int, aMin, aMax float64) C {
r := rand.Intn(rMax-rMin+1) + rMin
g := rand.Intn(gMax-gMin+1) + gMin
b := rand.Intn(bMax-bMin+1) + bMin
a := aMin + rand.Float64()(aMax-aMin)
return C{R: r, G: g, B: b, A: a}
}
// Define Morandi, Macaron and no color palettes
var (
moC = rC(0, 255, 0, 255, 0, 255, 0.25, 0.5)
maC = rC(0, 255, 0, 255, 0, 255, 1.0, 1.0)
nC = C{0, 0, 0, 0}
)
// Round a float64 to three decimal places
func r3dp(f float64) float64 {
return float64(int(f1000+0.5)) / 1000
}
// Generate random bloodline and random percentage
func rBarP() ([]int, []float64) {
const total = 100.0
rP := make([]float64, 100)
sum := 0.0
for i := range rP {
rP[i] = rand.Float64()
sum += rP[i]
}
for i := range rP {
rP[i] = r3dp((rP[i] / sum) * total)
}
rB := make([]int, 100)
for i := range rB {
rB[i] = i + 1
}
return rB, rP
}
// Find the bloodlineID with the highest bloodline percentage
func fhBID(rB []int, percentages []float64) int {
hBID := 0
for i := 1; i < len(percentages); i++ {
if percentages[i] > percentages[hBID] {
hBID = i
}
}
return rB[hBID]
}
// Generate random NPC
func gNPC() []interface{} {
// Get the next sequential NPC ID
playerID := getNextID()
// Firstsexgene1, firstsexgene2 (op 1-2); secondsexgene1, secondsexgene2 (op 1-3)
fsg1, fsg2 := rand.Intn(2)+1, rand.Intn(2)+1
ssg1, ssg2 := rand.Intn(3)+1, rand.Intn(3)+1
// Skin colors (predefined realistic RGBA range)
sC := []int{rand.Intn(56) + 200, rand.Intn(51) + 100, rand.Intn(51) + 80, 255}
// Determine color palettes for hairandbrow, eye, tail, wingsub, wingstripe and antenna color based on firstsexgenes combination
var habC, eC, tC, wsbC, wspC, aC
switch {
case fsg1 == 1 && fsg2 == 1:
habC, eC, tC = moC, moC, moC
wsbC, wspC, aC = nC, nC, nC
case fsg1 == 2 && fsg2 == 2:
habC, eC = moC, moC
tC, wsbC, wspC = nC, nC, nC
aC = maC
default:
habC, eC, wsbC, wspC, aC = maC, maC, maC, maC, maC
tC = nC
}
// Random shape attributes for face, brow, eye, ear, nose, mouth, fronthair, backhair, body, tail, tooth, wing and antenna shape (op 1-10)
rS := func() int { return rand.Intn(10) + 1 }
fS, bwS, eeS, erS, nS, mS, fhS, bhS := rS(), rS(), rS(), rS(), rS(), rS(), rS(), rS()
byS := dbyS(fsg1, fsg2)
// Random bloodlineIDs and bloodline percentages
BID, BP := rBarP()
maxID := fhBID(BID, BP)
tlS, thS, wS, aS := dtlSthSwSaS(fsg1, fsg2, maxID)
// Output
out := []interface{}{
playerID, fsg1, fsg2, ssg1, ssg2,
}
out = append(out, sCInts2Intf(sC)...)
out = append(out, habCeCtCwsbCwspCaCInts2Intf(habC)...)
out = append(out, habCeCtCwsbCwspCaCInts2Intf(eC)...)
out = append(out, habCeCtCwsbCwspCaCInts2Intf(tC)...)
out = append(out, habCeCtCwsbCwspCaCInts2Intf(wsbC)...)
out = append(out, habCeCtCwsbCwspCaCInts2Intf(wspC)...)
out = append(out, habCeCtCwsbCwspCaCInts2Intf(aC)...)
out = append(out, fS, bwS, eeS, erS, nS, mS, fhS, bhS, byS, tlS, thS, wS, aS)
for i := range BID {
out = append(out, BID[i], BP[i])
}
out = append(out, maxID)
return out
}
// Determine body shape based on firstsexgenes, ((1,1),(2,2),others) = (op 1-3,op 7-9,op 4-6)
func dbyS(fsg1, fsg2 int) int {
switch {
case fsg1 == 1 && fsg2 == 1:
return rand.Intn(3) + 1
case fsg1 == 2 && fsg2 == 2:
return rand.Intn(3) + 7
default:
return rand.Intn(3) + 4
}
}
// Determine tail, tooth, wing, antenna shape based on firstsexgenes (op 1-100, op 404 = no shape)
func dtlSthSwSaS(fsg1, fsg2, maxID int) (tlS, thS, wS, aS int) {
switch {
case fsg1 == 1 && fsg2 == 1:
return maxID, maxID, 404, 404
case fsg1 == 2 && fsg2 == 2:
return 404, 404, 404, maxID
default:
return 404, 404, maxID, maxID
}
}
// Convert skin color []int to []interface{}
func sCInts2Intf(slice []int) []interface{} {
out := make([]interface{}, len(slice))
for i, v := range slice {
out[i] = v
}
return out
}
// Convert Color struct to []interface{} {R, G, B, A255 rounded}
func habCeCtCwsbCwspCaCInts2Intf(c Color) []interface{} {
return []interface{}{
c.R, c.G, c.B,
int(c.A255 + 0.5),
}
}
// Function to read existing data from the file and find the next ID
func getNextID() int {
file, err := os.Open(filename)
if err != nil {
// No file means start at 1
return 1
}
defer file.Close()
maxID := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
parts := strings.Split(line, ",")
if len(parts) == 0 {
continue
}
idStr := strings.Trim(parts[0], "[ca/]")
id, err := strconv.Atoi(idStr)
if err != nil {
continue
}
if id > maxID {
maxID = id
}
}
return maxID + 1
}
// Function to format output as expected
func formatOutput(output []interface{}) string {
strs := make([]string, len(output))
for i, v := range output {
strs[i] = fmt.Sprint(v)
}
return "[ca/" + strings.Join(strs, ",") + "]"
}
// Function to append the generated NPC output to the text file
func appendToFile(data string) {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer f.Close()
if _, err := f.WriteString(data + "\n"); err != nil {
fmt.Println("Error writing to file:", err)
}
}
// Start NPC generation
func main() {
rand.Seed(time.Now().UnixNano())
reader := bufio.NewReader(os.Stdin)
fmt.Println("Press 'R' then Enter to generate a random NPC...")
for {
fmt.Print("> ")
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if strings.EqualFold(input, "r") {
npc := gNPC()
output := formatOutput(npc)
fmt.Println(output)
appendToFile(output)
} else {
fmt.Println("Please press 'R' then Enter to generate a random NPC.")
}
}
}
What's wrong?
.\CA.go:74:35: syntax error: unexpected newline, expected type