package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"time"
)
const filename = "CharacterAppearanceData.txt"
// Color struct with R, G, B as int and A as float64
type Color struct {
R, G, B int
A float64
}
// Generate a random Color within the specified channel & alpha ranges
func randomColor(rMin, rMax, gMin, gMax, bMin, bMax int, aMin, aMax float64) Color {
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 Color{R: r, G: g, B: b, A: a}
}
var (
morandiColor = randomColor(0, 255, 0, 255, 0, 255, 0.25, 0.5)
macaronColor = randomColor(0, 255, 0, 255, 0, 255, 1.0, 1.0)
noColor = Color{0, 0, 0, 0}
)
func GenerateRandomBloodline() ([]int, []float64) {
const total = 100.0
randVals := make([]float64, 100)
sum := 0.0
for i := range randVals {
randVals[i] = rand.Float64()
sum += randVals[i]
}
for i := range randVals {
randVals[i] = roundToThreeDecimal((randVals[i] / sum) * total)
}
ids := make([]int, 100)
for i := range ids {
ids[i] = i + 1
}
return ids, randVals
}
func roundToThreeDecimal(f float64) float64 {
return float64(int(f*1000+0.5)) / 1000
}
func FindMaxID(ids []int, percentages []float64) int {
maxIdx := 0
for i := 1; i < len(percentages); i++ {
if percentages[i] > percentages[maxIdx] {
maxIdx = i
}
}
return ids[maxIdx]
}
func generateNPC() []interface{} {
playerID := getNextID()
// Sex genes
fsg1, fsg2 := rand.Intn(2)+1, rand.Intn(2)+1
ssg1, ssg2 := rand.Intn(3)+1, rand.Intn(3)+1
// Skin colors (predefined realistic range)
skin := []int{rand.Intn(56) + 200, rand.Intn(51) + 100, rand.Intn(51) + 80, 255}
// Choose color palettes based on sex genes combination
var hairAndBrow, eye, tail, wingSub, wingStripe, antenna Color
switch {
case fsg1 == 1 && fsg2 == 1:
hairAndBrow, eye, tail = morandiColor, morandiColor, morandiColor
wingSub, wingStripe, antenna = noColor, noColor, noColor
case fsg1 == 2 && fsg2 == 2:
hairAndBrow, eye = morandiColor, morandiColor
tail, wingSub, wingStripe = noColor, noColor, noColor
antenna = macaronColor
default:
hairAndBrow, eye, wingSub, wingStripe, antenna = macaronColor, macaronColor, macaronColor, macaronColor, macaronColor
tail = noColor
}
// Shape attributes
randShape := func() int { return rand.Intn(10) + 1 }
faceshape, browshape := randShape(), randShape()
eyeshape, earshape := randShape(), randShape()
noseshape, mouthshape := randShape(), randShape()
fronthairshape, backhairshape := randShape(), randShape()
bodyshape := determineBodyShape(fsg1, fsg2)
bloodlineIDs, bloodlinePercentages := GenerateRandomBloodline()
maxID := FindMaxID(bloodlineIDs, bloodlinePercentages)
tailshape, toothshape, wingshape, antennashape := determineShapes(fsg1, fsg2, maxID)
out := []interface{}{
playerID, fsg1, fsg2, ssg1, ssg2,
}
out = append(out, skinIntsToInterfaces(skin)...)
out = append(out, colorToInts(hairAndBrow)...)
out = append(out, colorToInts(eye)...)
out = append(out, colorToInts(tail)...)
out = append(out, colorToInts(wingSub)...)
out = append(out, colorToInts(wingStripe)...)
out = append(out, colorToInts(antenna)...)
out = append(out,
faceshape, browshape, eyeshape, earshape,
noseshape, mouthshape, fronthairshape, backhairshape,
bodyshape, tailshape, toothshape, wingshape, antennashape,
)
for i := range bloodlineIDs {
out = append(out, bloodlineIDs[i], bloodlinePercentages[i])
}
out = append(out, maxID)
return out
}
func determineBodyShape(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
}
}
func determineShapes(fsg1, fsg2, maxID int) (tail, tooth, wing, antenna int) {
switch {
case fsg1 == 1 && fsg2 == 1:
return maxID, maxID, 101, 101
case fsg1 == 2 && fsg2 == 2:
return 101, 101, 101, maxID
default:
return 101, 101, maxID, maxID
}
}
func skinIntsToInterfaces(slice []int) []interface{} {
out := make([]interface{}, len(slice))
for i, v := range slice {
out[i] = v
}
return out
}
func colorToInts(c Color) []interface{} {
// Convert Color struct to []interface{} {R, G, B, A*255 rounded}
return []interface{}{
c.R, c.G, c.B,
int(c.A*255 + 0.5),
}
}
func getNextID() int {
file, err := os.Open(filename)
if err != nil {
return 1 // no file means start at 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
}
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, ",") + "]"
}
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)
}
}
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 := generateNPC()
output := formatOutput(npc)
fmt.Println(output)
appendToFile(output)
} else {
fmt.Println("Please press 'R' then Enter to generate a random NPC.")
}
}
}