Trefferpunkte aus Einzelwuerfen berechnen (1W4 + 1W je Stufe + KON)

This commit is contained in:
torsten 2026-11-22 19:00:31 +01:00
parent 216a62191e
commit 6a6ce7b197
11 changed files with 277 additions and 11 deletions

View file

@ -59,6 +59,13 @@ zauberturm.de: Nachtblau/Schwarz/Gold, Pergament, Animationen.
Grundklassen abgebildet) und werden bei Stufen-/Klassenwechsel neu gesetzt.
**Falls der SL eigene RW-Tabellen führt: dort in `app/rules.php` ändern**,
nicht in der DB.
- **Trefferpunkte:** berechnet aus Einzelwürfen – `1W4` für Stufe 0 + je
Stufenlevel `1W<Klassenwürfel>` (`WS_HIT_DICE`: Kämpferklassen W8, Kleriker
W6, Dieb/Magier W4) + Konstitutionsbonus (einmal, Hausregel V1.1). Die Würfe
liegen kompakt in `characters.hp_rolls` („3 7 5 2“), `hp_max` wird beim
Speichern daraus berechnet (leer = Altdaten, dann gilt `hp_max` manuell).
Formular: ein Textfeld + „Würfeln“-Knopf (füllt fehlende Würfe), Live-Summe;
Bogen/PDF zeigen die Kette „1W4 3 · W8 7 5 2 · KON +1“ (`hp_rolls_label()`).
- **Ableitungen im Bogen:** Trefferwurf = Grundangriff + Attributsbonus
(Nahkampf STR, Fernkampf DEX) + Waffenbonus; Schaden = Würfel + Bonus;
RK = 10 + DEX-Bonus + angelegte Rüstung (Feld `characters.ac` = manueller

View file

@ -30,6 +30,12 @@ Garamond) mit Animationen.
- **Rettungswürfe**: kommen aus der Klassentabelle (`WS_SAVE_TABLES` in
`app/rules.php`, OSE-SRD-Grundwerte) und werden bei Änderung von Klasse
oder Stufe automatisch nachgezogen; einzelne Werte bleiben überschreibbar.
- **Trefferpunkte** sind berechnet: `1W4` (Stufe 0) + je Stufenlevel
`1W<Klassenwürfel>` (`WS_HIT_DICE` in `app/rules.php`) + Konstitutionsbonus
(Hausregel V1.1: KON nur einmal). Die Einzelwürfe stehen kompakt in
`characters.hp_rolls` (`"3 7 5 2"` = Stufe 0, Stufe 1, 2, …), damit jeder
Wurf einzeln nachgewürfelt werden kann; `characters.hp_max` wird daraus
berechnet. Ohne erfasste Würfe (Altdaten) gilt `hp_max` weiter als manuell.
- **Bewaffnung & Rüstung**: Waffe oder Rüstungsteil zunächst aus dem Katalog
wählen (`weapons`/`armors`), danach ist jeder Eintrag frei änderbar
(Name, Schadenswürfel, Trefferbonus, RK-Bonus, Notiz, geführt/angelegt).

View file

@ -60,12 +60,20 @@ function character_create(int $userId, array $data): int
$data['level'] = level_for_xp((string) ($data['class_name'] ?? 'Kämpfer'), (int) ($data['xp'] ?? 0));
$data = array_merge($data, saving_throws_for((string) ($data['class_name'] ?? 'Kämpfer'), (int) $data['level']));
// Trefferpunkte: neue Bögen starten mit ausgewürfelten Einzelwürfen,
// hp_max ergibt sich daraus (Stufe 0: 1W4, je Stufe 1W<Klassenwürfel>).
$data['hp_rolls'] = hp_rolls_format(hp_rolls_parse($data['hp_rolls'] ?? ''));
if ($data['hp_rolls'] === '') {
$data['hp_rolls'] = hp_rolls_format(hp_rolls_roll((string) ($data['class_name'] ?? 'Kämpfer'), (int) $data['level']));
}
$data['hp_max'] = hp_max_for($data);
$stmt = db()->prepare(
'INSERT INTO characters (user_id, name, title, gender, class_name, level, xp, hp_max, hp_current, ac, attack_bonus,
'INSERT INTO characters (user_id, name, title, gender, class_name, level, xp, hp_max, hp_rolls, hp_current, ac, attack_bonus,
attr_str, attr_dex, attr_con, attr_int, attr_wis, attr_cha,
save_death, save_wand, save_paralysis, save_breath, save_spell,
alignment, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
);
$stmt->execute([
$userId,
@ -76,6 +84,7 @@ function character_create(int $userId, array $data): int
$data['level'] ?? 1,
$data['xp'] ?? 0,
$data['hp_max'] ?? 1,
$data['hp_rolls'] ?? '',
$data['hp_current'] ?? $data['hp_max'] ?? 1,
character_ac_value($data['ac'] ?? null),
$data['attack_bonus'] ?? 0,
@ -207,8 +216,13 @@ function character_update(int $id, array $d): void
}
}
// Trefferpunkte sind berechnet: einzelne Würfe speichern, hp_max daraus
// ableiten (ohne erfasste Würfe bleibt hp_max der manuelle Wert).
$d['hp_rolls'] = hp_rolls_format(hp_rolls_parse($d['hp_rolls'] ?? ($before['hp_rolls'] ?? '')));
$d['hp_max'] = hp_max_for($d);
$stmt = db()->prepare(
'UPDATE characters SET name = ?, title = ?, gender = ?, class_name = ?, level = ?, xp = ?, hp_max = ?, hp_current = ?,
'UPDATE characters SET name = ?, title = ?, gender = ?, class_name = ?, level = ?, xp = ?, hp_max = ?, hp_rolls = ?, hp_current = ?,
ac = ?, attack_bonus = ?, attr_str = ?, attr_dex = ?, attr_con = ?, attr_int = ?, attr_wis = ?, attr_cha = ?,
save_death = ?, save_wand = ?, save_paralysis = ?, save_breath = ?, save_spell = ?,
alignment = ?, deity = ?, age = ?, height = ?, weight = ?, gold_coins = ?,
@ -223,6 +237,7 @@ function character_update(int $id, array $d): void
$d['level'] ?? 1,
$d['xp'] ?? 0,
$d['hp_max'] ?? 1,
$d['hp_rolls'] ?? '',
$d['hp_current'] ?? 1,
character_ac_value($d['ac'] ?? null),
$d['attack_bonus'] ?? 0,
@ -298,7 +313,7 @@ function character_derived(array $c): array
$bonuses[$field] = attribute_bonus((int) ($c[$field] ?? 10));
}
$hp = max(0, (int) $c['hp_current']);
$hpMax = max(1, (int) $c['hp_max']);
$hpMax = max(1, hp_max_for($c));
return [
'bonuses' => $bonuses,
'hp_ratio' => min(1.0, $hp / $hpMax),

View file

@ -59,6 +59,12 @@ const WS_MIGRATIONS = [
"UPDATE character_skills SET attribute = 'WEI' WHERE attribute = 'WIS'",
"ALTER TABLE skills MODIFY attribute ENUM('STR', 'GES', 'KON', 'INT', 'WEI', 'CHA', '') NOT NULL DEFAULT ''",
],
// 6: Einzelne Trefferpunkt-Würfe (Stufe 0: 1W4, dann je Stufe 1W<Klassen-
// würfel>). hp_max wird daraus berechnet; leer = Altdaten, hp_max gilt
// weiter als manueller Wert.
6 => [
"ALTER TABLE characters ADD COLUMN IF NOT EXISTS hp_rolls VARCHAR(160) NOT NULL DEFAULT '' AFTER hp_max",
],
];
function ws_migrations_apply(PDO $pdo): void

View file

@ -539,6 +539,13 @@ function character_form_values(array $source): array
$values['gender'] = character_gender(post_string('gender', (string) ($source['gender'] ?? WS_DEFAULT_GENDER)));
$values['class_name'] = post_string('class_name', (string) ($source['class_name'] ?? 'Kämpfer'));
$values['xp'] = max(0, post_int('xp', (int) ($source['xp'] ?? 0)));
// Trefferpunkte: Einzelwürfe sind die Quelle, hp_max wird daraus berechnet
// (nur ohne erfasste Würfe gilt ein manuell gesetzter Wert).
$values['hp_rolls'] = hp_rolls_format(hp_rolls_parse(
array_key_exists('hp_rolls', $_POST) && is_string($_POST['hp_rolls'])
? (string) $_POST['hp_rolls']
: ($source['hp_rolls'] ?? '')
));
$values['hp_max'] = max(1, post_int('hp_max', (int) ($source['hp_max'] ?? 1)));
$values['hp_current'] = post_int('hp_current', (int) ($source['hp_current'] ?? $values['hp_max']));
$acRaw = post_string('ac', $source['ac'] === null ? '' : (string) $source['ac']);
@ -563,6 +570,7 @@ function character_form_values(array $source): array
// Stufe wird aus Erfahrungspunkten und Klasse abgeleitet (WS_XP_TABLES) –
// sie ist kein eigenes Eingabefeld mehr.
$values['level'] = level_for_xp((string) $values['class_name'], (int) $values['xp']);
$values['hp_max'] = hp_max_for($values);
foreach (WS_SAVES as $field => $label) {
// Auf GET (Formular anzeigen) gilt der gespeicherte Wert; nur ein
// tatsächlich abgeschicktes Feld überschreibt ihn.
@ -581,6 +589,11 @@ function page_character_new(string $method, array $user): void
{
$error = '';
$values = character_form_values(['attr_str' => 14, 'attr_dex' => 12, 'attr_con' => 11, 'attr_int' => 10, 'attr_wis' => 9, 'attr_cha' => 7]);
// Neue Bögen starten mit ausgewürfelten Trefferpunkten (nachwürfelbar).
if ($values['hp_rolls'] === '') {
$values['hp_rolls'] = hp_rolls_format(hp_rolls_roll((string) $values['class_name'], (int) $values['level']));
$values['hp_max'] = hp_max_for($values);
}
if ($method === 'POST') {
if (!csrf_valid()) {
$error = 'Die Sitzung ist abgelaufen. Bitte erneut versuchen.';

View file

@ -372,3 +372,144 @@ function armor_class_total(array $character, array $armors): array
'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'];
}
/**
* 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 ' . signed((int) $info['con']);
return implode(' · ', $parts);
}

View file

@ -146,7 +146,7 @@ function character_pdf(array $c, array $extra): string
/* ---------------------------------------------------------- Attribute */
$sectionTitle('Attribute');
$boxW = 60.0;
$boxH = 54.0;
$boxH = 64.0;
$gap = 6.0;
$x = $M;
foreach (WS_ATTRIBUTES as $field => [$short, $label]) {
@ -162,17 +162,20 @@ function character_pdf(array $c, array $extra): string
$combatX = $x + 4;
$combatW = $W - $M - $combatX;
$pdf->rect($combatX, $y, $combatW, $boxH, null, $gold, 0.6);
// Trefferpunkte: Maximum ist aus den Einzelwürfen berechnet, die Würfe
// stehen als eigene Zeile daneben (Nachwürfeln = Zeile korrigieren).
$combatRows = [
['Trefferpunkte', (int) $c['hp_current'] . ' / ' . (int) $c['hp_max']],
['Trefferpunkte', (int) $c['hp_current'] . ' / ' . hp_max_for($c)],
['Trefferwürfe', WsPdf::fit(hp_rolls_label($c), 8, 'F2', $combatW - 42)],
['Rüstungsklasse', (string) (int) ($extra['ac']['total'] ?? 10)],
['Angriff', signed((int) $c['attack_bonus'])],
['Gold', (int) $c['gold_coins'] . ' GM'],
];
$ry = $y + 12;
$rowStep = 12.0;
$rowStep = 11.0;
foreach ($combatRows as $i => [$label, $value]) {
$pdf->text($combatX + 7, $ry, 7.0, 'F1', $soft, $label);
$pdf->textRight($combatX + $combatW - 7, $ry, 9, 'F2', $ink, $value);
$pdf->textRight($combatX + $combatW - 7, $ry, $i === 1 ? 8 : 9, 'F2', $i === 1 ? $soft : $ink, $value);
$ry += $rowStep;
}
$y += $boxH + 6;

View file

@ -61,6 +61,63 @@
update();
});
// Trefferpunkte: Einzelwürfe live zu TP max zusammenrechnen und auswürfeln
const hpInput = document.querySelector('[data-hp-rolls]');
if (hpInput) {
const totalField = document.querySelector('[data-hp-total]');
const label = document.querySelector('[data-hp-label]');
const die = parseInt(hpInput.getAttribute('data-hp-die'), 10) || 4;
const baseDie = parseInt(hpInput.getAttribute('data-hp-base'), 10) || 4;
const slots = parseInt(hpInput.getAttribute('data-hp-slots'), 10) || 1;
const con = parseInt(hpInput.getAttribute('data-hp-con'), 10) || 0;
const signedText = function (value) {
return value > 0 ? '+' + value : String(value);
};
const parseRolls = function () {
return (hpInput.value.match(/[0-9]+/g) || []).map(function (token) {
return parseInt(token, 10) || 0;
}).slice(0, slots);
};
const update = function () {
const rolls = parseRolls();
let sum = 0;
const levelDice = [];
for (let slot = 0; slot < slots; slot++) {
const value = rolls[slot] || 0;
if (value > 0) {
sum += value;
}
if (slot > 0) {
levelDice.push(value > 0 ? String(value) : '–');
}
}
const total = Math.max(1, sum + con);
if (totalField) {
totalField.value = String(total);
}
if (label) {
const base = rolls[0] > 0 ? String(rolls[0]) : '–';
label.textContent = '1W' + baseDie + ' ' + base + ' · W' + die + ' ' + (levelDice.join(' ') || '–')
+ ' · KON ' + signedText(con) + ' = ' + total;
}
};
hpInput.addEventListener('input', update);
const rollButton = document.querySelector('[data-hp-roll]');
if (rollButton) {
rollButton.addEventListener('click', function () {
const rolls = parseRolls();
const next = [];
for (let slot = 0; slot < slots; slot++) {
const value = rolls[slot] || 0;
next.push(value > 0 ? value : 1 + Math.floor(Math.random() * (slot === 0 ? baseDie : die)));
}
hpInput.value = next.join(' ');
update();
});
}
update();
}
// Zauberdetails im Bogen: Klick auf den Namen füllt den Detail-Schirm
const spellDetail = document.querySelector('#spell-detail');
if (spellDetail) {

View file

@ -90,6 +90,7 @@ CREATE TABLE IF NOT EXISTS characters (
level TINYINT UNSIGNED NOT NULL DEFAULT 1,
xp INT UNSIGNED NOT NULL DEFAULT 0,
hp_max SMALLINT NOT NULL DEFAULT 1,
hp_rolls VARCHAR(160) NOT NULL DEFAULT '',
hp_current SMALLINT NOT NULL DEFAULT 1,
ac SMALLINT NULL DEFAULT NULL,
attack_bonus SMALLINT NOT NULL DEFAULT 0,

View file

@ -55,14 +55,30 @@
</div>
<div class="grid-4">
<?php $hp = hp_breakdown($values); ?>
<label class="field">
<span>TP aktuell</span>
<input type="number" name="hp_current" value="<?= (int) $values['hp_current'] ?>">
</label>
<label class="field">
<span>TP maximal</span>
<input type="number" name="hp_max" min="1" value="<?= (int) $values['hp_max'] ?>">
<span>TP maximal <small class="muted">(berechnet)</small></span>
<input type="text" data-hp-total value="<?= (int) $hp['total'] ?>" readonly
title="1W<?= (int) WS_HP_LEVEL0_DIE ?> (Stufe 0) + 1W<?= (int) $hp['die'] ?> je Stufe + Konstitutionsbonus">
<small class="muted">
1W<?= (int) WS_HP_LEVEL0_DIE ?> + <?= (int) $hp['level'] ?>× 1W<?= (int) $hp['die'] ?> <?= e(signed((int) $hp['con'])) ?> KON
</small>
</label>
<div class="field" style="grid-column: span 2;">
<span>Trefferwürfe <small class="muted">Stufe 0 (1W<?= (int) WS_HP_LEVEL0_DIE ?>), dann Stufe 1 … <?= (int) $hp['level'] ?></small></span>
<div style="display:flex; gap:.4rem; align-items:center;">
<input type="text" name="hp_rolls" data-hp-rolls value="<?= e(hp_rolls_format(hp_rolls_parse($values['hp_rolls']))) ?>"
inputmode="numeric" placeholder="z. B. 3 7 5 2" style="flex:1 1 auto; min-width:0;"
data-hp-die="<?= (int) $hp['die'] ?>" data-hp-base="<?= (int) WS_HP_LEVEL0_DIE ?>"
data-hp-slots="<?= (int) $hp['slots'] ?>" data-hp-con="<?= (int) $hp['con'] ?>">
<button class="btn" type="button" data-hp-roll title="Fehlende Würfe auswürfeln">Würfeln</button>
</div>
<small class="muted" data-hp-label><?= e(hp_rolls_label($values)) ?> = <?= (int) $hp['total'] ?></small>
</div>
<label class="field">
<span>Rüstungsklasse (leer = automatisch)</span>
<input type="number" name="ac" value="<?= $values['ac'] === null ? '' : (int) $values['ac'] ?>"

View file

@ -62,7 +62,8 @@ $equipped = array_values(array_filter($items, static fn ($item) => (int) $item['
<div class="stat-row">
<div class="stat">
<span class="k">Trefferpunkte</span>
<span class="v"><?= (int) $character['hp_current'] ?> / <?= (int) $character['hp_max'] ?></span>
<span class="v"><?= (int) $character['hp_current'] ?> / <?= hp_max_for($character) ?></span>
<span class="b" style="color:var(--parchment-dim)"><?= e(hp_rolls_label($character)) ?></span>
</div>
<div class="stat">
<span class="k">Rüstungsklasse</span>