#lang racket/base

; Pictogrammic adventurer generator
; Generates a character sheet with all indications replaced by pictograms
(require
  racket/string
  racket/list
  json
  "../base-generation/random.rkt"
  "../base-generation/category.rkt")

(provide
  ; Model builder
  make-pictogrammic-adventurer-generator
  ; generative function
  pictogrammic-adventurer-generate
  ; accessors to adventurer structure
  adventurer-name adventurer-gender adventurer-species adventurer-class
  adventurer-health adventurer-mana adventurer-spells adventurer-status
  adventurer-equipment adventurer-inventory adventurer-party adventurer-battle-cry
  adventurer-astrological-sign adventurer-living-place adventurer-cards
  )

; Adventurer structure
(struct adventurer
  (name
   gender
   species
   class
   health
   mana
   spells
   status
   equipment
   inventory
   party
   battle-cry
   astrological-sign
   living-place
   cards
  ))

; Model builder
(define (make-pictogrammic-adventurer-generator path)
  (jsexpr->category-generator (read-json (open-input-file path))))

; Generate a name
; Choose random characters from the name category, and a few from other categories
(define (name-generate gen)
  (string-join
    (shuffle
      (append
        (category-generate-list gen 'name (+ 3 (random 5)))
        (map
          (lambda (x)
            (category-generate gen x))
          (random:sublist
            '(gender class health mana spells status inventory party astrological-sign cards)
            (+ 1 (random 5))))))
    ""))

; Generate a battle cry
; Choose random characters from the battle-cry category, and a few from other categories
; Generated like a sentence with the pictograms making words
(define (battle-cry-generate gen)
  (string-join
    (build-list
      (+ 2 (random 6))
      (lambda (_)
        (string-join
          (shuffle
            (append
              (category-generate-list gen 'battle-cry (+ 1 (random 5)))
              (map
                (lambda (x)
                  (category-generate gen x))
                (random:sublist
                  '(name gender class health mana spells status inventory party astrological-sign cards)
                  (random 3)))))
          "")))
    "\u2003")) ; use em space between words as symbola's space is too small

; Generator
(define (pictogrammic-adventurer-generate gen)
  (adventurer
    (name-generate gen)
    (category-generate gen 'gender)
    (category-generate gen 'species)
    (category-generate gen 'class)
    (string-join (make-list (+ 1 (random 9)) (category-generate gen 'health)) "")
    (string-join (make-list (+ 1 (random 9)) (category-generate gen 'mana)) "")
    (string-join (category-generate-list gen 'mana (+ 2 (random 8))) "")
    (string-join (category-generate-list gen 'status (+ 1 (random 4))) "")
    (string-join (category-generate-list gen 'equipment (+ 3 (random 3))) "")
    (string-join (category-generate-list gen 'inventory (+ 3 (random 12))) "")
    (string-join (category-generate-list gen 'party (+ 1 (random 4))) "")
    (battle-cry-generate gen)
    (category-generate gen 'astrological-sign)
    (category-generate gen 'living-place)
    (string-join (category-generate-list gen 'cards 5) "")
    ))
