rpg-westreiche/westreiche_website/app/rules.php

568 lines
18 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/** Westreiche – Regelkonstanten (Hausregeln V1.1, OSE-alike). */
const WS_ATTRIBUTES = [
'attr_str' => ['STR', 'Stärke'],
'attr_dex' => ['GES', 'Geschicklichkeit'],
'attr_con' => ['KON', 'Konstitution'],
'attr_int' => ['INT', 'Intelligenz'],
'attr_wis' => ['WEI', 'Weisheit'],
'attr_cha' => ['CHA', 'Charisma'],
];
/**
* Attributkürzel → Spaltenname am Charakterbogen. Die Kürzel sind deutsch
* (GES, KON, WEI), die Spalten bleiben historisch attr_dex/attr_con/attr_wis.
*/
const WS_ATTRIBUTE_CODES = [
'STR' => 'attr_str',
'GES' => 'attr_dex',
'KON' => 'attr_con',
'INT' => 'attr_int',
'WEI' => 'attr_wis',
'CHA' => 'attr_cha',
];
const WS_CLASSES = [
'Kämpfer', 'Ritter', 'Paladin', 'Barbar', 'Waldläufer',
'Dieb', 'Assassine', 'Barde',
'Magier', 'Illusionist', 'Kleriker', 'Druide',
];
const WS_SAVES = [
'save_death' => 'Tod & Gift',
'save_wand' => 'Zauberstäbe',
'save_paralysis' => 'Lähmung',
'save_breath' => 'Atemwaffen',
'save_spell' => 'Zauber',
];
const WS_ALIGNMENTS = ['Rechtschaffen', 'Neutral', 'Chaotisch', 'Gut', 'Böse', '–'];
/**
* Geschlecht: bestimmt das Platzhalterporträt (…-weiblich.webp). Der erste
* Eintrag ist der Standardwert für neue und bestehende Charaktere.
*/
const WS_DEFAULT_GENDER = 'männlich';
const WS_GENDERS = ['männlich', 'weiblich'];
const WS_ITEM_CATEGORIES = ['Waffe', 'Rüstung', 'Schild', 'Ausrüstung', 'Verbrauch', 'Schatz', 'Sonstiges'];
/**
* Verfügbarkeit im Ausrüstungskatalog: Methaven (Dorf) und Garana (Kleinstadt)
* sind Stufen – was in Methaven zu haben ist, gibt es auch in Garana. Mächtige
* oder seltene Ware führt nur der Filter „Alle“.
*/
const WS_ITEM_AVAILABILITY = [
'methaven' => 'Methaven (Dorf)',
'garana' => 'Garana (Kleinstadt)',
'selten' => 'nur Alle',
];
/**
* Fertigkeitengruppen wie auf dem Westreiche-Charakterbogen
* (RPG/6-Dokumente/Charakterbogen.pdf): KÖRPER, WILDNIS, SOZIAL, WISSEN.
*/
const WS_SKILL_GROUPS = ['Körper', 'Wildnis', 'Sozial', 'Wissen'];
const WS_SKILL_CATEGORIES = WS_SKILL_GROUPS;
/**
* Offizieller Fertigkeitenkatalog des Charakterbogens:
* Name => [Attribut, Gruppe]. Startwert ist der Attributsbonus.
*/
const WS_SKILL_CATALOG = [
'Ausdauer' => ['KON', 'Körper'],
'Balancieren' => ['GES', 'Körper'],
'Fliegen' => ['GES', 'Körper'],
'Klettern' => ['STR', 'Körper'],
'Reiten & Wagenführung' => ['GES', 'Körper'],
'Schleichen' => ['GES', 'Körper'],
'Schwimmen' => ['KON', 'Körper'],
'Wahrnehmung' => ['WEI', 'Körper'],
'Wildniskunde' => ['WEI', 'Wildnis'],
'Nahrung finden' => ['WEI', 'Wildnis'],
'Navigation' => ['WEI', 'Wildnis'],
'Orientierung' => ['INT', 'Wildnis'],
'Pflanzenkunde' => ['INT', 'Wildnis'],
'Segeln + Schiffahrt' => ['INT', 'Wildnis'],
'Seilkunde' => ['GES', 'Wildnis'],
'Spurenlesen' => ['WEI', 'Wildnis'],
'Anführen' => ['CHA', 'Sozial'],
'Aufführung' => ['CHA', 'Sozial'],
'Einschüchtern' => ['STR', 'Sozial'],
'Etikette' => ['CHA', 'Sozial'],
'Handeln & Schätzen' => ['WEI', 'Sozial'],
'Überzeugen' => ['CHA', 'Sozial'],
'Verstellen' => ['INT', 'Sozial'],
'Alchimie' => ['INT', 'Wissen'],
'Geschichts + Sagenkunde' => ['INT', 'Wissen'],
'Handwerk' => ['GES', 'Wissen'],
'Heilkunde: Geist' => ['WEI', 'Wissen'],
'Heilkunde: Gift' => ['INT', 'Wissen'],
'Heilkunde: Krankheiten' => ['INT', 'Wissen'],
'Heilkunde: Wunden' => ['WEI', 'Wissen'],
'Religionskunde' => ['INT', 'Wissen'],
'Verwaltung' => ['INT', 'Wissen'],
'Zauberkunde' => ['INT', 'Wissen'],
];
/**
* Fertigkeitsstufen des Charakterbogens (RPG/6-Dokumente/Charakterbogen.pdf):
* Index = Stufe. Stufe 0 = ungelernt, ab Stufe 6 immer 3W6.
*/
const WS_SKILL_DICE = ['W4', 'W6', 'W8', 'W10', 'W12', '2W6', '3W6'];
const WS_SKILL_TIERS = ['ungelernt', 'Anfänger', 'Geselle', 'Könner', 'Profi', 'Meister', 'Großmeister'];
/** Ankreuzbare Praxispunkte je Fertigkeit (Bogen-Spalte „PP"). */
const WS_SKILL_PRACTICE_SLOTS = 10;
const WS_CHARACTER_STATUS = [
'active' => 'im Spiel',
'dead' => 'gefallen',
'retired' => 'im Ruhestand',
];
/**
* Attributsbonus nach Hausregel V1.1:
* 3 → −3, 4–5 → −2, 6–8 → −1, 9–12 → 0, 13–15 → +1, 16–17 → +2, 18 → +3.
* Werte über 18 werden wie 18 behandelt.
*/
function attribute_bonus(int $value): int
{
return match (true) {
$value <= 3 => -3,
$value <= 5 => -2,
$value <= 8 => -1,
$value <= 12 => 0,
$value <= 15 => 1,
$value <= 17 => 2,
default => 3,
};
}
function signed(int $value): string
{
return $value > 0 ? '+' . $value : (string) $value;
}
/**
* CSS-Klasse für einen Bonus: positive Werte grün, Null grau, negative rot.
*/
function bonus_class(int $value): string
{
return $value > 0 ? 'bonus-pos' : ($value < 0 ? 'bonus-neg' : 'bonus-zero');
}
/**
* Stufe einer Fertigkeit auf die Würfelleiter abbilden (0 … 6).
*/
function skill_tier_index(int $level): int
{
return max(0, min(count(WS_SKILL_DICE) - 1, $level));
}
/** Würfel der Fertigkeitsstufe, z. B. Stufe 3 → „W10". */
function skill_die(int $level): string
{
return WS_SKILL_DICE[skill_tier_index($level)];
}
/** Bezeichnung der Fertigkeitsstufe, z. B. Stufe 0 → „ungelernt". */
function skill_tier(int $level): string
{
return WS_SKILL_TIERS[skill_tier_index($level)];
}
/** Attributkürzel (STR, GES, …) prüfen; unbekannte Werte werden zu ''. */
function skill_attribute_code(string $value): string
{
$code = strtoupper(trim($value));
return isset(WS_ATTRIBUTE_CODES[$code]) ? $code : '';
}
/** Spaltenname (attr_dex …) zu einem Attributkürzel; unbekannt → ''. */
function attribute_field_for_code(string $code): string
{
return WS_ATTRIBUTE_CODES[skill_attribute_code($code)] ?? '';
}
/**
* Rettungswurf-Feld formatieren; leere Werte bleiben ein Gedankenstrich.
*/
function save_display(mixed $value): string
{
if ($value === null || $value === '') {
return '–';
}
return (string) (int) $value;
}
/* ------------------------------------------------------- Rettungswürfe */
/**
* Rettungswurf-Grundwerte je Klasse und Stufe (Würfelwurf ≥ Wert, OSE-SRD).
*
* Aufbau: Klassenname → Liste aus [bis Stufe, [Tod & Gift, Zauberstäbe,
* Lähmung, Atemwaffen, Zauber]]. Der erste Eintrag, dessen Stufengrenze
* erreicht ist, gilt. Die Westreiche-Klassen ohne eigene Tabelle laufen auf
* der passenden Grundklasse: Kämpfer, Ritter, Paladin, Barbar und Waldläufer
* auf der Kämpfer-Tabelle, Druide auf der Kleriker-, Assassine und Barde auf
* der Diebes-Tabelle.
*/
const WS_SAVE_TABLES = [
'Kämpfer' => [
[3, [12, 13, 14, 15, 16]],
[6, [10, 11, 12, 13, 14]],
[9, [8, 9, 10, 11, 12]],
[12, [6, 7, 8, 9, 10]],
[99, [4, 5, 6, 7, 8]],
],
'Kleriker' => [
[3, [11, 12, 14, 16, 15]],
[6, [9, 10, 12, 14, 13]],
[9, [6, 7, 9, 11, 10]],
[12, [3, 5, 7, 8, 8]],
[99, [2, 3, 5, 6, 6]],
],
'Magier' => [
[5, [13, 14, 13, 16, 15]],
[10, [11, 12, 11, 14, 12]],
[99, [8, 9, 8, 11, 8]],
],
'Dieb' => [
[4, [13, 14, 13, 16, 15]],
[8, [12, 13, 11, 14, 13]],
[12, [11, 10, 9, 12, 10]],
[99, [9, 8, 7, 10, 8]],
],
];
/** Klasse → Grundklasse für Rettungswürfe. */
const WS_SAVE_BASE_CLASS = [
'Ritter' => 'Kämpfer',
'Paladin' => 'Kämpfer',
'Barbar' => 'Kämpfer',
'Waldläufer' => 'Kämpfer',
'Druide' => 'Kleriker',
'Illusionist' => 'Magier',
'Assassine' => 'Dieb',
'Barde' => 'Dieb',
];
/* ----------------------------------------------- Stufen aus Erfahrung */
/** Höchste Stufe, die die App kennt (Formular, EP-Tabellen). */
const WS_MAX_LEVEL = 20;
/**
* Erfahrungspunkte, ab denen eine Stufe erreicht ist (OSE-SRD, Stufe 1–14).
* Index 0 = Stufe 1. Die Westreiche-Klassen laufen über
* WS_SAVE_BASE_CLASS auf der passenden Grundklasse.
*/
const WS_XP_TABLES = [
'Kämpfer' => [0, 2000, 4000, 8000, 16000, 32000, 64000, 120000, 240000, 360000, 480000, 600000, 720000, 840000],
'Kleriker' => [0, 1500, 3000, 6000, 12000, 25000, 50000, 100000, 200000, 300000, 400000, 500000, 600000, 700000],
'Magier' => [0, 2500, 5000, 10000, 20000, 40000, 80000, 150000, 300000, 450000, 600000, 750000, 900000, 1050000],
'Dieb' => [0, 1250, 2500, 5000, 10000, 20000, 40000, 80000, 160000, 280000, 400000, 520000, 640000, 760000],
];
/**
* EP-Zuwachs je weiterer Stufe oberhalb der OSE-Tabelle – damit die
* epische Leiter bis Stufe 20 weiterläuft (Namenstufe + fester Schritt).
*/
const WS_XP_STEP = [
'Kämpfer' => 120000,
'Kleriker' => 100000,
'Magier' => 150000,
'Dieb' => 120000,
];
/** EP-Schwellen je Stufe (Index 0 = Stufe 1) bis WS_MAX_LEVEL. */
function xp_thresholds_for(string $className): array
{
$base = class_base_class($className);
$thresholds = WS_XP_TABLES[$base] ?? WS_XP_TABLES['Kämpfer'];
$step = WS_XP_STEP[$base] ?? 120000;
while (count($thresholds) < WS_MAX_LEVEL) {
$thresholds[] = $thresholds[count($thresholds) - 1] + $step;
}
return array_slice($thresholds, 0, WS_MAX_LEVEL);
}
/** Stufe aus Erfahrungspunkten und Klasse ableiten (1 … WS_MAX_LEVEL). */
function level_for_xp(string $className, int $xp): int
{
$level = 1;
foreach (xp_thresholds_for($className) as $index => $threshold) {
if ($xp >= $threshold) {
$level = $index + 1;
}
}
return $level;
}
/**
* EP-Stand für die Anzeige: aktuelle Stufe, Schwelle der Stufe, Schwelle der
* nächsten Stufe, fehlende EP und Füllstand (0 … 1) für Balken.
*/
function xp_progress(string $className, int $xp): array
{
$xp = max(0, $xp);
$thresholds = xp_thresholds_for($className);
$level = level_for_xp($className, $xp);
$current = $thresholds[$level - 1];
$next = $thresholds[$level] ?? null;
$span = $next === null ? 0 : $next - $current;
return [
'level' => $level,
'current' => $current,
'next' => $next,
'missing' => $next === null ? 0 : max(0, $next - $xp),
'ratio' => $span > 0 ? min(1.0, max(0.0, ($xp - $current) / $span)) : 1.0,
];
}
/** Klasse → Grundklasse (für Rettungswürfe und EP-Tabellen). */
function class_base_class(string $className): string
{
return WS_SAVE_BASE_CLASS[$className] ?? $className;
}
/** Rettungswürfe für Klasse + Stufe als Feld → Wert. */
function saving_throws_for(string $className, int $level): array
{
$base = class_base_class($className);
$table = WS_SAVE_TABLES[$base] ?? WS_SAVE_TABLES['Kämpfer'];
$level = max(1, min(WS_MAX_LEVEL, $level));
$values = $table[count($table) - 1][1];
foreach ($table as [$maxLevel, $row]) {
if ($level <= $maxLevel) {
$values = $row;
break;
}
}
$fields = array_keys(WS_SAVES);
$result = [];
foreach ($fields as $index => $field) {
$result[$field] = $values[$index];
}
return $result;
}
/* ----------------------------------------- Waffen und Rüstungen (Ableitung) */
/** Trefferwurf mit einer Waffe: Grundangriff + Attributsbonus + Waffenbonus. */
function weapon_attack_total(array $character, array $weapon): int
{
$attribute = ($weapon['category'] ?? 'Nahkampf') === 'Fernkampf' ? 'attr_dex' : 'attr_str';
return (int) ($character['attack_bonus'] ?? 0)
+ attribute_bonus((int) ($character[$attribute] ?? 10))
+ (int) ($weapon['attack_bonus'] ?? 0);
}
/** Schaden mit Waffe: Würfelausdruck + Attributsbonus (bei Fernkampf Geschick). */
function weapon_damage_total(array $character, array $weapon): string
{
$damage = trim((string) ($weapon['damage'] ?? ''));
if ($damage === '') {
return '–';
}
$attribute = ($weapon['category'] ?? 'Nahkampf') === 'Fernkampf' ? 'attr_dex' : 'attr_str';
$bonus = attribute_bonus((int) ($character[$attribute] ?? 10));
return $bonus === 0 ? $damage : $damage . signed($bonus);
}
/**
* Rüstungsklasse: 10 + Geschicklichkeitsbonus + alle angelegten Rüstungsteile.
* Ein manueller Wert in characters.ac (nicht NULL) überschreibt das.
*/
function armor_class_total(array $character, array $armors): array
{
$dexterity = attribute_bonus((int) ($character['attr_dex'] ?? 10));
$armorBonus = 0;
foreach ($armors as $armor) {
if ((int) ($armor['equipped'] ?? 0) === 1) {
$armorBonus += (int) ($armor['ac_bonus'] ?? 0);
}
}
$derived = 10 + $dexterity + $armorBonus;
$manual = $character['ac'] ?? null;
$isManual = $manual !== null && $manual !== '' && (int) $manual > 0;
return [
'derived' => $derived,
'manual' => $isManual ? (int) $manual : null,
'total' => $isManual ? (int) $manual : $derived,
'dexterity' => $dexterity,
'armor' => $armorBonus,
'manual_active' => $isManual,
];
}
/* ------------------------------------------------------- Trefferpunkte */
/**
* Trefferpunkte: 1W4 für Stufe 0 + je Stufenlevel 1W<Klassenwürfel> +
* Konstitutionsbonus (Hausregel V1.1: KON zählt nur einmal, nicht je Stufe).
*
* Jeder einzelne Wurf kann später nachgewürfelt werden, deshalb werden die
* Ergebnisse einzeln gespeichert (`characters.hp_rolls`, kommasepariert:
* erst der Stufe-0-Wurf, dann Stufe 1, 2, …). `characters.hp_max` ist daraus
* berechnet; steht dort kein Wurf (Altdaten), gilt das Feld als manuell.
*/
const WS_HP_LEVEL0_DIE = 4;
/** Klassenwürfel für Trefferpunkte (OSE-alike). */
const WS_HIT_DICE = [
'Kämpfer' => 8, 'Ritter' => 8, 'Paladin' => 8, 'Barbar' => 8, 'Waldläufer' => 8,
'Kleriker' => 6, 'Druide' => 6,
'Dieb' => 4, 'Assassine' => 4, 'Barde' => 4,
'Magier' => 4, 'Illusionist' => 4,
];
/** Klassenwürfel (Seitenzahl) einer Klasse; unbekannt → W4. */
function hit_die_for(string $className): int
{
return WS_HIT_DICE[$className] ?? WS_HIT_DICE['Magier'];
}
/** Anzahl der Würfe, die ein Charakter der Stufe braucht (Stufe 0 + Stufen). */
function hp_roll_slots(int $level): int
{
return 1 + max(1, min(WS_MAX_LEVEL, $level));
}
/**
* Gespeicherte Würfe einlesen. Leere Stellen dürfen als 0 notiert werden,
* damit die Positionen (Stufe 0, 1, 2, …) erhalten bleiben.
*/
function hp_rolls_parse(mixed $stored): array
{
if (is_array($stored)) {
$tokens = $stored;
} else {
$text = trim((string) $stored);
if ($text === '') {
return [];
}
$tokens = preg_split('/[^0-9]+/', $text, -1, PREG_SPLIT_NO_EMPTY) ?: [];
}
$rolls = [];
foreach ($tokens as $token) {
$rolls[] = max(0, min(99, (int) $token));
if (count($rolls) >= hp_roll_slots(WS_MAX_LEVEL)) {
break;
}
}
while ($rolls !== [] && end($rolls) === 0) {
array_pop($rolls);
}
return $rolls;
}
/** Würfe kompakt als Text speichern („3 7 5 2“). */
function hp_rolls_format(array $rolls): string
{
return implode(' ', array_map('intval', $rolls));
}
/** Alle Würfe für Klasse + Stufe auswürfeln (Stufe 0 zuerst). */
function hp_rolls_roll(string $className, int $level): array
{
$die = hit_die_for($className);
$rolls = [random_int(1, WS_HP_LEVEL0_DIE)];
for ($i = 0; $i < max(1, min(WS_MAX_LEVEL, $level)); $i++) {
$rolls[] = random_int(1, $die);
}
return $rolls;
}
/**
* Aufschlüsselung der Trefferpunkte für Bogen, PDF und Formular:
* Stufe-0-Wurf, die Würfe je Stufe, Summe, Konstitutionsbonus und Maximum.
* `manual` = keine Würfe erfasst, dann gilt der gespeicherte Wert in hp_max.
*/
function hp_breakdown(array $character): array
{
$level = max(1, min(WS_MAX_LEVEL, (int) ($character['level'] ?? 1)));
$die = hit_die_for((string) ($character['class_name'] ?? 'Kämpfer'));
$raw = hp_rolls_parse($character['hp_rolls'] ?? '');
$manual = $raw === [];
$base = ($raw[0] ?? 0) > 0 ? (int) $raw[0] : null;
$sum = $base ?? 0;
$complete = $base !== null;
$rolls = [];
for ($slot = 1; $slot <= $level; $slot++) {
$value = ($raw[$slot] ?? 0) > 0 ? (int) $raw[$slot] : null;
$rolls[] = $value;
if ($value === null) {
$complete = false;
} else {
$sum += $value;
}
}
$con = attribute_bonus((int) ($character['attr_con'] ?? 10));
$stored = max(0, (int) ($character['hp_max'] ?? 0));
return [
'level' => $level,
'die' => $die,
'base' => $base,
'rolls' => $rolls,
'slots' => hp_roll_slots($level),
'sum' => $sum,
'con' => $con,
'manual' => $manual,
'complete' => $complete,
'total' => $manual ? max(1, $stored) : max(1, $sum + $con),
];
}
/** Berechnete maximale Trefferpunkte (bzw. gespeicherter Wert ohne Würfe). */
function hp_max_for(array $character): int
{
return hp_breakdown($character)['total'];
}
/**
* Kurze Rechenkette für das Formular: „3 + 4 + 7 + 8 = 25“.
* Fehlende Würfe erscheinen als „–“; der Konstitutionsbonus steckt im Ergebnis.
*/
function hp_rolls_sum(array $character): string
{
$info = hp_breakdown($character);
if ($info['manual']) {
return 'noch keine Würfe erfasst';
}
$parts = [$info['base'] === null ? '–' : (string) $info['base']];
foreach ($info['rolls'] as $value) {
$parts[] = $value === null ? '–' : (string) $value;
}
$chain = implode(' + ', $parts);
if ((int) $info['con'] !== 0) {
// Konstitutionsbonus als eigener Summand, damit die Kette aufgeht.
$chain .= ((int) $info['con'] > 0 ? ' + ' : ' − ') . abs((int) $info['con']) . ' KON';
}
return $chain . ' = ' . (int) $info['total'];
}
/**
* Kompakte Klartextkette der Würfe für Bogen und PDF:
* „1W4 3 · W8 7 5 2 · KON +1“ (fehlende Würfe erscheinen als „–“).
*/
function hp_rolls_label(array $character): string
{
$info = hp_breakdown($character);
if ($info['manual']) {
return 'keine Einzelwürfe erfasst';
}
$parts = ['1W' . WS_HP_LEVEL0_DIE . ' ' . ($info['base'] ?? '–')];
$levelDice = implode(' ', array_map(static fn (?int $v): string => $v === null ? '–' : (string) $v, $info['rolls']));
$parts[] = 'W' . $info['die'] . ' ' . ($levelDice === '' ? '–' : $levelDice);
$parts[] = 'KON ' . str_replace('-', '−', signed((int) $info['con']));
return implode(' · ', $parts);
}