Home /Modules/gameplay/DiceRoll.sleela

DiceRoll.sleela

lib/gameplay/DiceRoll.sleela · 123 line(s) · sleela

<< lib/gameplay/DiceRoll.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines DiceRoll, the deterministic percent-roller at the heart of
     the gameplay package. Every "80% then 20%" and "50/50" in a turn is a
     DiceRoll. It is a seeded linear-congruential generator, so a given seed
     always replays the same game — the archive must repeat, even the luck.
   
   DiceRoll is a NATURAL / LOADABLE MODULE sharing the common module contract
   (load / loaded / moduleName / describe). It holds a single 32-bit-ish state
   word and advances it on every roll. No native randomness is used; "live play"
   is modeled by advancing the same deterministic stream.
   
   Family: gameplay. >>
#sleela 2.5
material;
< Unified SLeeLa SDK front-end object. >
class DiceRoll {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10310;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-07";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  static final String MODULE = "DiceRoll";
  < 2^31 - 1, a classic LCG modulus >
  static final int MODULUS = 2147483647;
  < MINSTD multiplier >
  static final int MULT     = 48271;

  int loadedFlag;
  < current generator state, 1..MODULUS-1 >
  int state;
  < how many rolls this stream has produced >
  int rollCount;

  < --- loadable-module contract ------------------------------------------ >
  void load() { loadedFlag = 1; state = 1; rollCount = 0; }
  boolean loaded() { return loadedFlag == 1; }
  String moduleName() { return MODULE; }

  < Seed the stream. The same seed replays the same sequence, every time. >
  void seed(int s) {
    int v = s;
    if (v < 0) { v = 0 - v; }
    v = v % MODULUS;
    < 0 is a fixed point; nudge it off >
    if (v == 0) { v = 1; }
    state = v;
    rollCount = 0;
  }

  < Advance and return the next raw value in 1..MODULUS-1. >
  int next() {
    state = (MULT * state) % MODULUS;
    if (state <= 0) { state = state + MODULUS; }
    rollCount = rollCount + 1;
    return state;
  }

  < A percentage result in 0..99 for the next step of play. >
  int percent() { return next() % 100; }

  < Roll against a target: returns true when the roll "succeeds" at `chance` >
  < percent. chance<=0 never succeeds; chance>=100 always does. >
  boolean rollUnder(int chance) {
    if (chance <= 0) { return false; }
    if (chance >= 100) { return true; }
    return percent() < chance;
  }

  < The 50/50 your "ask for help" move leans on. >
  boolean coinFlip() { return rollUnder(50); }

  < A value in [lo, hi] inclusive, for picking amounts and indices. >
  int between(int lo, int hi) {
    if (hi <= lo) { return lo; }
    int span = hi - lo + 1;
    return lo + (next() % span);
  }

  int rolls() { return rollCount; }

  String describe() {
    return MODULE + " state=" + state + " rolls=" + rollCount;
  }
}