// Pure-Typst creature database importer. // // SQLite is read directly from the project through typsqlite. Every source row // remains available under `raw`; the renderer-facing fields are derived here so // database access, relational composition, and presentation stay independent. #import "@preview/typsqlite:0.1.0": sqlite #import "../core/creature-timing.typ": initiative #let db = sqlite(read("/data/src/db/monsters.db", encoding: none)) #let _records(sql) = { let result = (db.query)(sql) result.rows.map(row => { let record = (:) for (column, value) in result.columns.zip(row) { record.insert(column, value) } record }) } // Public raw tables. These intentionally use SELECT * so newly used columns do // not disappear during normalization and balancing targets remain accessible. #let creature-rows = _records("SELECT * FROM creature ORDER BY sort_id, id") #let reward-rows = _records("SELECT * FROM creature_rewards ORDER BY creature_id, reward_id") #let stats-rows = _records("SELECT * FROM creature_stats ORDER BY creature_id, stats_id") #let body-part-rows = _records("SELECT * FROM creature_body_parts ORDER BY creature_id, body_part_id") #let ability-rows = _records( "SELECT * FROM creature_abilities " + "ORDER BY creature_id, " + "CASE TRIM(COALESCE(phase, '')) " + "WHEN '' THEN 0 WHEN 'Always' THEN 1 WHEN 'Phase1' THEN 2 " + "WHEN 'Phase2' THEN 3 WHEN 'Phase3' THEN 4 ELSE 5 END, " + "phase, CASE WHEN attack_group IS NULL OR attack_group = '' THEN attack_id ELSE 0 END, " + "attack_group, COALESCE(ordering, 2147483647), attack_id", ) #let balancing-rows = _records("SELECT * FROM creature_balancing ORDER BY creature_id, balancing_id") #let player-balancing-rows = _records("SELECT * FROM player_balancing ORDER BY player_balancing_id") #let raw-tables = ( creature: creature-rows, rewards: reward-rows, stats: stats-rows, body-parts: body-part-rows, abilities: ability-rows, balancing: balancing-rows, player-balancing: player-balancing-rows, ) #let _present(value) = value != none and value != "" #let _text(value) = if value == none { "" } else { str(value) } #let _trim(value) = _text(value).trim() #let _for(rows, creature-id) = rows.filter(row => row.creature_id == creature-id) #let _first(rows, fallback: none) = if rows.len() == 0 { fallback } else { rows.first() } #let _first-present(values, fallback: "") = { let found = values.filter(value => _present(value) and _trim(value) != "") if found.len() == 0 { fallback } else { found.first() } } #let _image-paths = ( "1": "arangar.jpg", "2": "yajjpnir.jpg", "3": "mjonn_des_brumes.jpg", "4": "mjonn_des_brumes_alpha.jpg", "5": "baloth_joueur.jpg", "6": "oorgroonth_mou.jpg", "7": "ormarkiste_penible.jpg", "8": "Urgaei Givrecornes.jpg", "9": "abjernide_arroseur.jpg", "10": "atkeolajakis_noble.jpg", "11": "askoride_fangeux.jpg", "12": "arpaneris_commun.jpg", "13": "jetethl_ah_herault_des_temps_perdus.jpg", "14": "eleris_crepusculaire.jpg", "15": "gravan_crapaud.jpg", "16": "ekrasil_irefeu.jpg", "17": "kioenkkol.png", "18": "margrenth_condamne.jpg", "19": "yynngren_inseparables.jpg", "20": "nmatithiel_justicar_des_temps_perdus.jpg", "21": "tarrasque.png", ) #let _image(core) = { let filename = _image-paths.at(str(core.id), default: "nodata.png") "/images/bestiary/" + filename } #let _csv(value) = _text(value).split(",").map(it => it.trim()).filter(_present) #let _initiatives(stats) = { let p1-values = _csv(stats.p1_init) let p2-values = _csv(stats.p2_init) let p1-actions = _csv(stats.p1_actions) let p2-actions = _csv(stats.p2_actions) let count = calc.max(p1-values.len(), p2-values.len(), p1-actions.len(), p2-actions.len()) range(count).map(index => ( name: initiative(index + 1), p1-value: p1-values.at(index, default: ""), p1-actions: p1-actions.at(index, default: ""), p2-value: p2-values.at(index, default: ""), p2-actions: p2-actions.at(index, default: ""), )) } #let _part(row, stats) = ( name: row.body_part_name, p1: ( threshold: ( base: row.threshold_p1, modifier: [#(row.threshold_p1 - 8) + #stats.size_bonus_to_hit], ), protection: (base: stats.strength + row.protection_p1, armor: row.protection_p1), integrity: row.integrity_p1, life: row.life_points_p1, ), p2: ( threshold: ( base: row.threshold_p2, modifier: [#(row.threshold_p2 - 8) + #stats.size_bonus_to_hit], ), protection: (base: stats.strength + row.protection_p2, armor: row.protection_p2), integrity: row.integrity_p2, life: row.life_points_p2, ), resistances: ( _text(row.slash), _text(row.piercing), _text(row.blunt), _text(row.celestial), _text(row.fire), _text(row.earth), _text(row.wood), _text(row.magma), _text(row.abyssal), _text(row.water), _text(row.wind), _text(row.ice), _text(row.thunder), ), raw: row, ) #let _ability(row) = { let damage = (_text(row.damage_regular), _text(row.success)).filter(_present).join(" ") let effect-tags = ( if row.armor_break != none { ((key: "rl_arbreak", value: str(row.armor_break)),) } else { () }, if row.antimaterial != none { ((key: "rl_am", value: str(row.antimaterial)),) } else { () }, if row.armor_pierce != none { ((key: "rl_parmure", value: str(row.armor_pierce)),) } else { () }, ).flatten() ( name: row.name, short-name: row.short_name, type: row.attack_type, timing: _text(row.prerequisites), target: _text(row.targetting), fatigue-cost: _text(row.cost_fatigue), action-cost: _text(row.cost_action), element: _text(row.element), fatigue-damage: _text(row.damage_fatigue), damage: damage, effect-tags: effect-tags, effects: _text(row.effects), parry: _text(row.threshold_parry), dodge: _text(row.threshold_dodge), raw: row, ) } #let _phases(rows) = { let phase-names = () for row in rows { let phase = if _trim(row.phase) == "" { "Général" } else { _trim(row.phase) } if phase not in phase-names { phase-names.push(phase) } } phase-names.map(phase-name => { let phase-rows = rows.filter(row => { let candidate = if _trim(row.phase) == "" { "Général" } else { _trim(row.phase) } candidate == phase-name }) let group-keys = () for row in phase-rows { // Null groups are deliberately unique: only an explicit DB group joins // multiple sub-attacks. let key = if _trim(row.attack_group) == "" { "attack:" + str(row.attack_id) } else { "group:" + _trim(row.attack_group) } if key not in group-keys { group-keys.push(key) } } let groups = group-keys.map(key => { let grouped = phase-rows.filter(row => { let candidate = if _trim(row.attack_group) == "" { "attack:" + str(row.attack_id) } else { "group:" + _trim(row.attack_group) } candidate == key }) ( key: key, color: grouped.first().attack_type, description: _text(_first-present(grouped.map(row => row.description_all))), attacks: grouped.map(_ability), raw: grouped, ) }) (name: phase-name, groups: groups, raw: phase-rows) }) } #let build-creature(core) = { let stats-source = _first(_for(stats-rows, core.id)) assert(stats-source != none, message: "Missing creature_stats row for creature " + str(core.id)) let rewards-source = _for(reward-rows, core.id) let parts-source = _for(body-part-rows, core.id) let abilities-source = _for(ability-rows, core.id) let balancing-source = _for(balancing-rows, core.id) ( id: core.id, sort-id: core.sort_id, name: core.name, denomination: _text(core.denomination), name-translation: _text(core.name_translation), calamity: core.calamity, danger-level: _text(core.danger_level).replace(".", ","), image: _image(core), habitat: _text(core.habitat), region: _text(core.region), subregion: _text(core.subregion), locality: _text(core.locality), dysmorphism: _text(core.dysmorphism), colors: _text(core.colors), human-reaction: _text(core.default_human_reaction), variants: _text(core.variants), size: _text(core.size), traces: _text(core.traces), habits: _text(core.habits), description: _text(core.description), annotations: _text(core.annotations), master-notes: _text(core.annotations_master), battle-plan: _text(core.battle_plan), intent-graph: _text(core.intent_graph), show-to-players: core.show_to_players, show-to-master: core.show_to_master, blood: ( base: stats-source.blood_base, battle: stats-source.blood_battle, carve: stats-source.blood_carve, ), rewards: rewards-source.map(row => ( name: _text(row.name), category: _text(row.category), cost: _text(row.cost), effect: _text(row.effect), raw: row, )), body-parts: parts-source.map(row => _part(row, stats-source)), initiatives: _initiatives(stats-source), stats: ( strength: stats-source.strength, perception: stats-source.perception, willpower: stats-source.willpower, alteration-resistance: stats-source.alteration_resistance, resistances: _text(stats-source.resistances), torque: stats-source.torque, awareness: stats-source.awareness, fatigue: stats-source.fatigue, movement: ( walk: stats-source.mvt_walk, trot: stats-source.mvt_trot, run: stats-source.mvt_run, flight: stats-source.mvt_flight, ), transcendental-resistance: stats-source.resistance_transcendentale, physical-bonus: stats-source.physical_bonus_dmg, elemental-bonus: stats-source.elemental_bonus_dmg, size-bonus-to-hit: stats-source.size_bonus_to_hit, threshold-maluses: _text(stats-source.threshold_maluses), raw: stats-source, ), phases: _phases(abilities-source), raw: ( creature: core, rewards: rewards-source, stats: stats-source, body-parts: parts-source, abilities: abilities-source, balancing: balancing-source, ), ) } #let creatures = creature-rows.map(build-creature) #let master-creatures = creatures.filter(creature => creature.show-to-master == 1) #let player-creatures = creatures.filter(creature => creature.show-to-players == 1) #let creature-by-id(id) = _first(creatures.filter(creature => creature.id == id))