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

@ -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;