440 lines
15 KiB
GDScript
440 lines
15 KiB
GDScript
extends Node
|
|
## Game (Autoload) — der Spielzustand: Party, Ort, Zeit, Flaggen.
|
|
##
|
|
## Gespeichert werden **Eingaben**, keine abgeleiteten Werte (docs/engine.md §7):
|
|
## TP ja, RK nein. Wird eine Klasse nachbalanciert, korrigiert der nächste
|
|
## Ladevorgang die Ableitung automatisch.
|
|
|
|
const START = "garana"
|
|
const PARTY_MAX := 6
|
|
const START_GOLD := 20
|
|
|
|
var party: Array = [] ## Array von Dictionaries (ids + Werte)
|
|
var gold := START_GOLD ## Parteigold; Ausgaben kommen später dazu
|
|
var boden: Array = [] ## liegengelassene Gegenstände: {id,count,map,x,y}
|
|
var place: Dictionary = {"id": START, "x": 10, "y": 8}
|
|
var time: Dictionary = {"day": 1, "slot": 0}
|
|
var flags: Dictionary = {}
|
|
var log_lines: Array[String] = []
|
|
|
|
func _ready() -> void:
|
|
# Die Party entsteht in der Erstellung (docs/charaktererstellung.md).
|
|
# `party_schnellstart()` füllt sie für den Direktstart ohne Erstellung.
|
|
pass
|
|
|
|
## Vier Figuren, wie in docs/systeme.md §11 durchgerechnet.
|
|
func _neue_party() -> void:
|
|
var vorgabe := [
|
|
{"name": "Aldric", "race": "human", "class": "fighter",
|
|
"attributes": {"str": 15, "dex": 12, "con": 13, "int": 10, "wis": 9, "cha": 11},
|
|
"choice": ["str", "con"]},
|
|
{"name": "Bruna", "race": "dwarf", "class": "cleric",
|
|
"attributes": {"str": 12, "dex": 9, "con": 15, "int": 10, "wis": 16, "cha": 11}},
|
|
{"name": "Cael", "race": "elf", "class": "magier",
|
|
"attributes": {"str": 8, "dex": 15, "con": 11, "int": 17, "wis": 12, "cha": 10}},
|
|
{"name": "Dorn", "race": "halfling", "class": "thief",
|
|
"attributes": {"str": 11, "dex": 17, "con": 12, "int": 12, "wis": 10, "cha": 13}},
|
|
]
|
|
party = []
|
|
gold = START_GOLD
|
|
boden = []
|
|
flags = {}
|
|
# Nur die Karte; das Startfeld kommt aus `spawn` in den Kartendaten.
|
|
place = {"id": START}
|
|
for i in vorgabe.size():
|
|
party.append(neue_figur("pc_%d" % (i + 1), vorgabe[i]))
|
|
|
|
# ---------------------------------------------------------------- Roster ----
|
|
# Die Erstellung arbeitet auf demselben `party`-Array, das auch gespeichert
|
|
# wird. Ein Roster ist damit kein zweiter Zustand, der auseinanderlaufen kann.
|
|
|
|
func party_leeren() -> void:
|
|
party = []
|
|
gold = START_GOLD
|
|
boden = []
|
|
flags = {}
|
|
place = {"id": START}
|
|
|
|
## Nimmt eine Figur auf. Bei vollem Roster (6) kommt `{}` zurück — der Aufrufer
|
|
## fragt vorher `party_bereit()` oder prüft das Ergebnis.
|
|
func party_aufnehmen(vorgabe: Dictionary) -> Dictionary:
|
|
if party.size() >= PARTY_MAX:
|
|
return {}
|
|
var figur := neue_figur(_naechste_pc_id(), vorgabe)
|
|
party.append(figur)
|
|
return figur
|
|
|
|
func party_entfernen(id: String) -> bool:
|
|
for i in party.size():
|
|
if str(party[i].get("id", "")) == id:
|
|
party.remove_at(i)
|
|
return true
|
|
return false
|
|
|
|
func party_bereit() -> bool:
|
|
return party.size() >= 1 and party.size() <= PARTY_MAX
|
|
|
|
|
|
## Die vier Papierprobe-Figuren als Schnellstart (Titelschirm/Erstellung).
|
|
func party_schnellstart() -> void:
|
|
_neue_party()
|
|
|
|
func _naechste_pc_id() -> String:
|
|
var belegt := {}
|
|
for figur in party:
|
|
belegt[str(figur.get("id", ""))] = true
|
|
var nummer := 1
|
|
while belegt.has("pc_%d" % nummer):
|
|
nummer += 1
|
|
return "pc_%d" % nummer
|
|
|
|
func neue_figur(id: String, vorgabe: Dictionary) -> Dictionary:
|
|
var klasse: Dictionary = Content.class_def(str(vorgabe["class"]))
|
|
var volk: Dictionary = Content.entry("races", str(vorgabe["race"]))
|
|
var attrs: Dictionary = vorgabe["attributes"].duplicate()
|
|
for attribut in volk.get("attribute_mods", {}):
|
|
attrs[attribut] = int(attrs.get(attribut, 10)) + int(volk["attribute_mods"][attribut])
|
|
# Mensch: +1 auf zwei verschiedene Attribute, gewählt in der Erstellung.
|
|
var wahl: Array = vorgabe.get("choice", [])
|
|
var wahl_bonus: Dictionary = volk.get("choice_bonus", {})
|
|
if not wahl.is_empty():
|
|
for attribut in wahl:
|
|
attrs[attribut] = int(attrs.get(attribut, 10)) + int(wahl_bonus.get("amount", 0))
|
|
var hp_max := _hp_stufe1(klasse, attrs)
|
|
var start: Dictionary = klasse.get("starting_equipment", {})
|
|
var equipment := {}
|
|
for feld in ["armor", "weapon"]:
|
|
if start.has(feld):
|
|
equipment[feld] = str(start[feld])
|
|
if bool(start.get("shield", false)):
|
|
equipment["shield"] = true
|
|
var inventar := []
|
|
for item_id in start.get("items", []):
|
|
inventar.append({"id": str(item_id), "count": 1})
|
|
return {
|
|
"id": id, "name": vorgabe["name"],
|
|
"race": vorgabe["race"], "class": vorgabe["class"],
|
|
"portrait": str(vorgabe.get("portrait", "")),
|
|
"level": 1, "xp": 0,
|
|
"attributes": attrs,
|
|
"hp": {"current": hp_max, "max": hp_max},
|
|
"skills": _startfertigkeiten(klasse),
|
|
"spells": {"known": [], "prepared": [], "slots": {}},
|
|
"inventory": inventar, "equipment": equipment,
|
|
"conditions": [],
|
|
}
|
|
|
|
func _hp_stufe1(klasse: Dictionary, attrs: Dictionary) -> int:
|
|
var kon := Dice.modifier(int(attrs.get("con", 10)))
|
|
return maxi(1, int(klasse.get("hit_die", 6)) + kon)
|
|
|
|
func _startfertigkeiten(klasse: Dictionary) -> Dictionary:
|
|
var out := {}
|
|
for id in klasse.get("trained_skills", []):
|
|
var fertigkeit: Dictionary = Content.skill(str(id))
|
|
out[str(id)] = int(fertigkeit.get("trained_bonus", 3))
|
|
return out
|
|
|
|
## Abgeleitete Werte werden gerechnet, nie gespeichert.
|
|
func rk(figur: Dictionary) -> int:
|
|
var ac := 10 + Dice.modifier(int(figur["attributes"].get("dex", 10)))
|
|
var ruestung: Dictionary = Content.entry("items", str(figur.get("equipment", {}).get("armor", "")))
|
|
ac += int(ruestung.get("ac_bonus", 0))
|
|
if figur.get("equipment", {}).get("shield", false):
|
|
var schild: Dictionary = Content.entry("items", "schild")
|
|
ac += int(schild.get("ac_bonus", 1))
|
|
return ac
|
|
|
|
func angriffsbonus(figur: Dictionary) -> int:
|
|
var klasse: Dictionary = Content.class_def(str(figur["class"]))
|
|
var stufe := int(figur.get("level", 1))
|
|
var leiter: Array = klasse.get("attack_per_level", [])
|
|
if stufe - 1 < leiter.size():
|
|
return int(leiter[stufe - 1])
|
|
return int(leiter[leiter.size() - 1]) if not leiter.is_empty() else 0
|
|
|
|
# ------------------------------------------------------------- Inventar ----
|
|
# Ein Gegenstand ist `{"id": ..., "count": n}`; Ausrüstung referenziert ids.
|
|
# Ausrüsten nimmt ein Stück aus dem Rucksack, Ablegen legt es zurück.
|
|
|
|
func inventar_hat(figur: Dictionary, id: String) -> int:
|
|
for eintrag in figur.get("inventory", []):
|
|
if typeof(eintrag) == TYPE_DICTIONARY:
|
|
if str(eintrag.get("id", "")) == id:
|
|
return int(eintrag.get("count", 0))
|
|
elif str(eintrag) == id:
|
|
return 1
|
|
return 0
|
|
|
|
|
|
func inventar_hinzufuegen(figur: Dictionary, id: String, anzahl: int = 1) -> void:
|
|
if id.is_empty() or anzahl <= 0:
|
|
return
|
|
var inventar: Array = figur.get("inventory", [])
|
|
for eintrag in inventar:
|
|
if typeof(eintrag) == TYPE_DICTIONARY and str(eintrag.get("id", "")) == id:
|
|
eintrag["count"] = int(eintrag.get("count", 0)) + anzahl
|
|
return
|
|
inventar.append({"id": id, "count": anzahl})
|
|
figur["inventory"] = inventar
|
|
|
|
|
|
func inventar_entfernen(figur: Dictionary, id: String, anzahl: int = 1) -> bool:
|
|
var inventar: Array = figur.get("inventory", [])
|
|
for i in inventar.size():
|
|
var eintrag = inventar[i]
|
|
var treffer := false
|
|
var zahl := 1
|
|
if typeof(eintrag) == TYPE_DICTIONARY:
|
|
treffer = str(eintrag.get("id", "")) == id
|
|
zahl = int(eintrag.get("count", 1))
|
|
else:
|
|
treffer = str(eintrag) == id
|
|
if not treffer:
|
|
continue
|
|
if zahl <= anzahl:
|
|
inventar.remove_at(i)
|
|
else:
|
|
eintrag["count"] = zahl - anzahl
|
|
return true
|
|
return false
|
|
|
|
|
|
## Klassenbeschränkung aus `classes.json` (`weapons`, `armor`).
|
|
func ausruesten_erlaubt(figur: Dictionary, id: String) -> bool:
|
|
var item: Dictionary = Content.entry("items", id)
|
|
var typ := str(item.get("type", ""))
|
|
var klasse: Dictionary = Content.class_def(str(figur.get("class", "")))
|
|
match typ:
|
|
"weapon":
|
|
return klasse.get("weapons", []).has(id)
|
|
"armor":
|
|
return klasse.get("armor", []).has(id)
|
|
"shield":
|
|
return klasse.get("armor", []).has("schild")
|
|
return false
|
|
|
|
|
|
func ausruesten(figur: Dictionary, id: String) -> bool:
|
|
var item: Dictionary = Content.entry("items", id)
|
|
if item.is_empty():
|
|
return false
|
|
var typ := str(item.get("type", ""))
|
|
if typ not in ["weapon", "armor", "shield"]:
|
|
return false
|
|
if not ausruesten_erlaubt(figur, id):
|
|
return false
|
|
if inventar_hat(figur, id) <= 0:
|
|
return false
|
|
var equipment: Dictionary = figur.get("equipment", {})
|
|
if typ == "shield":
|
|
if bool(equipment.get("shield", false)):
|
|
inventar_hinzufuegen(figur, "schild")
|
|
equipment["shield"] = true
|
|
else:
|
|
var alt := str(equipment.get(typ, ""))
|
|
if not alt.is_empty():
|
|
inventar_hinzufuegen(figur, alt)
|
|
equipment[typ] = id
|
|
figur["equipment"] = equipment
|
|
inventar_entfernen(figur, id)
|
|
return true
|
|
|
|
|
|
## Gesamtgewicht inklusive ausgerüsteter Stücke.
|
|
func gewicht(figur: Dictionary) -> int:
|
|
var summe := 0
|
|
for eintrag in figur.get("inventory", []):
|
|
var id := ""
|
|
var anzahl := 1
|
|
if typeof(eintrag) == TYPE_DICTIONARY:
|
|
id = str(eintrag.get("id", ""))
|
|
anzahl = int(eintrag.get("count", 1))
|
|
else:
|
|
id = str(eintrag)
|
|
summe += int(Content.entry("items", id).get("weight", 1)) * anzahl
|
|
var equipment: Dictionary = figur.get("equipment", {})
|
|
for feld in ["weapon", "armor"]:
|
|
var id := str(equipment.get(feld, ""))
|
|
if not id.is_empty():
|
|
summe += int(Content.entry("items", id).get("weight", 1))
|
|
if bool(equipment.get("shield", false)):
|
|
summe += int(Content.entry("items", "schild").get("weight", 1))
|
|
return summe
|
|
|
|
|
|
## Traglast: 10 + STÄ. Kein Feld in den Daten, weil sie aus den Attributen folgt.
|
|
func kapazitaet(figur: Dictionary) -> int:
|
|
return 10 + int(figur.get("attributes", {}).get("str", 10))
|
|
|
|
|
|
func ueberladen(figur: Dictionary) -> bool:
|
|
return gewicht(figur) > kapazitaet(figur)
|
|
|
|
|
|
## 1 = langsamer Schritt in der Stadt; der Kampf nutzt später denselben Wert.
|
|
func bewegung_malus(figur: Dictionary) -> int:
|
|
return 1 if ueberladen(figur) else 0
|
|
|
|
|
|
## Ein Gegenstand wandert zwischen zwei Figuren; die Traglast des Ziels zählt.
|
|
func geben(von: Dictionary, an: Dictionary, id: String, anzahl: int = 1) -> String:
|
|
if str(von.get("id", "")) == str(an.get("id", "")):
|
|
return "Das ist dieselbe Figur."
|
|
if inventar_hat(von, id) < anzahl:
|
|
return "Nicht genug im Rucksack."
|
|
var einzel := int(Content.entry("items", id).get("weight", 1))
|
|
var last := gewicht(an) + einzel * anzahl
|
|
if last > kapazitaet(an):
|
|
return "%s ist überladen (%d/%d)." % [
|
|
str(an.get("name", "?")), last, kapazitaet(an)]
|
|
inventar_entfernen(von, id, anzahl)
|
|
inventar_hinzufuegen(an, id, anzahl)
|
|
return "%s → %s." % [_item_name(id), str(an.get("name", "?"))]
|
|
|
|
|
|
func _item_name(id: String) -> String:
|
|
return str(Content.entry("items", id).get("name", id))
|
|
|
|
|
|
## Verbrauchsgüter. Gibt eine Meldung für die UI zurück.
|
|
func benutzen(figur: Dictionary, id: String) -> String:
|
|
if inventar_hat(figur, id) <= 0:
|
|
return "Nicht im Rucksack."
|
|
match id:
|
|
"vorrat":
|
|
var tp: Dictionary = figur.get("hp", {})
|
|
if int(tp.get("current", 0)) >= int(tp.get("max", 0)):
|
|
return "TP sind voll."
|
|
var heilung: int = Dice.roll("1d6")
|
|
tp["current"] = mini(int(tp.get("max", 0)),
|
|
int(tp.get("current", 0)) + heilung)
|
|
figur["hp"] = tp
|
|
inventar_entfernen(figur, id)
|
|
return "Vorrat: +%d TP." % heilung
|
|
"laterne":
|
|
flags["licht"] = not bool(flags.get("licht", false))
|
|
return "Laterne %s." % ("an" if flags["licht"] else "aus")
|
|
"trank_heilung":
|
|
return _heilen(figur, id, "2d6+2", "Heiltrank")
|
|
"trank_heilung_klein":
|
|
return _heilen(figur, id, "1d6+1", "Kleiner Heiltrank")
|
|
"trank_heilung_gross":
|
|
return _heilen(figur, id, "4d6+4", "Großer Heiltrank")
|
|
"seil":
|
|
return "Seil: noch keine Anwendung."
|
|
return "Nicht benutzbar."
|
|
|
|
|
|
## Auf den Boden legen; die Stadt zeichnet und sammelt die Einträge wieder auf.
|
|
func fallen_lassen(figur: Dictionary, id: String) -> String:
|
|
if inventar_hat(figur, id) <= 0:
|
|
return "Nicht im Rucksack."
|
|
if not inventar_entfernen(figur, id):
|
|
return "Nicht im Rucksack."
|
|
boden.append({
|
|
"id": id, "count": 1,
|
|
"map": str(place.get("id", START)),
|
|
"x": int(place.get("x", 0)), "y": int(place.get("y", 0)),
|
|
})
|
|
return "%s liegt am Boden." % _item_name(id)
|
|
|
|
|
|
## Nimmt alles auf, was auf dem Feld liegt und noch in die Traglast passt.
|
|
func boden_aufnehmen(figur: Dictionary, map_id: String, x: int, y: int) -> Array[String]:
|
|
var namen: Array[String] = []
|
|
for i in range(boden.size() - 1, -1, -1):
|
|
var eintrag: Dictionary = boden[i]
|
|
if str(eintrag.get("map", "")) != map_id \
|
|
or int(eintrag.get("x", -1)) != x or int(eintrag.get("y", -1)) != y:
|
|
continue
|
|
var id := str(eintrag.get("id", ""))
|
|
var anzahl := int(eintrag.get("count", 1))
|
|
var last := int(Content.entry("items", id).get("weight", 1)) * anzahl
|
|
if gewicht(figur) + last > kapazitaet(figur):
|
|
continue
|
|
inventar_hinzufuegen(figur, id, anzahl)
|
|
boden.remove_at(i)
|
|
namen.append(_item_name(id))
|
|
return namen
|
|
|
|
|
|
## Truhe: einmalige Ausbeute, Gold zählt nicht zur Traglast.
|
|
func truhe_nehmen(container: Dictionary) -> String:
|
|
var id := str(container.get("id", ""))
|
|
if bool(flags.get(id, false)):
|
|
return "Die Truhe ist leer."
|
|
if party.is_empty():
|
|
return "Keine Partei."
|
|
var figur: Dictionary = party[0]
|
|
var last := 0
|
|
for eintrag in container.get("loot", []):
|
|
last += int(Content.entry("items", str(eintrag.get("id", "")))
|
|
.get("weight", 1)) * int(eintrag.get("count", 1))
|
|
if gewicht(figur) + last > kapazitaet(figur):
|
|
return "Überladen: erst Platz schaffen."
|
|
gold += int(container.get("gold", 0))
|
|
for eintrag in container.get("loot", []):
|
|
inventar_hinzufuegen(figur, str(eintrag.get("id", "")),
|
|
int(eintrag.get("count", 1)))
|
|
flags[id] = true
|
|
return "Truhe geleert."
|
|
|
|
|
|
func _heilen(figur: Dictionary, id: String, wurfel: String, name: String) -> String:
|
|
var tp: Dictionary = figur.get("hp", {})
|
|
if int(tp.get("current", 0)) >= int(tp.get("max", 0)):
|
|
return "TP sind voll."
|
|
var heilung: int = Dice.roll(wurfel)
|
|
tp["current"] = mini(int(tp.get("max", 0)),
|
|
int(tp.get("current", 0)) + heilung)
|
|
figur["hp"] = tp
|
|
inventar_entfernen(figur, id)
|
|
return "%s: +%d TP." % [name, heilung]
|
|
|
|
|
|
func ablegen(figur: Dictionary, slot: String) -> bool:
|
|
var equipment: Dictionary = figur.get("equipment", {})
|
|
if slot == "shield":
|
|
if not bool(equipment.get("shield", false)):
|
|
return false
|
|
equipment["shield"] = false
|
|
inventar_hinzufuegen(figur, "schild")
|
|
else:
|
|
var id := str(equipment.get(slot, ""))
|
|
if id.is_empty():
|
|
return false
|
|
equipment.erase(slot)
|
|
inventar_hinzufuegen(figur, id)
|
|
figur["equipment"] = equipment
|
|
return true
|
|
|
|
func notiz(text: String) -> void:
|
|
log_lines.append(text)
|
|
if log_lines.size() > 200:
|
|
log_lines.pop_front()
|
|
|
|
# ------------------------------------------------------------- speichern ----
|
|
|
|
func to_dict() -> Dictionary:
|
|
return {
|
|
"save_version": Saves.VERSION,
|
|
"place": place,
|
|
"time": time,
|
|
"gold": gold,
|
|
"boden": boden,
|
|
"party": party,
|
|
"flags": flags,
|
|
"log": log_lines,
|
|
}
|
|
|
|
func from_dict(d: Dictionary) -> void:
|
|
place = d.get("place", place)
|
|
time = d.get("time", time)
|
|
gold = int(d.get("gold", gold))
|
|
boden = d.get("boden", boden)
|
|
party = d.get("party", party)
|
|
flags = d.get("flags", {})
|
|
log_lines = []
|
|
for zeile in d.get("log", []):
|
|
log_lines.append(str(zeile))
|