# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "K-Shoot MANIA Application"
        subgraph "Entry Points"
            KSM["kshootmania.hsp<br/>(Game v1.75b)"]
            KSE["kshooteditor.hsp<br/>(Chart Editor v1.75b)"]
        end

        subgraph "Scene Layer"
            TITLE["title.hsp<br/>Title Screen"]
            SELECT["select.hsp<br/>Song Selection"]
            PLAY["play.hsp<br/>Play Screen"]
            RESULT["result.hsp<br/>Result Screen"]
            CRESULT["course_result.hsp<br/>Course Result"]
            OPTION["option.hsp<br/>Options"]
            GATE["gate.hsp<br/>INPUT GATE (disabled)"]
        end

        subgraph "Play Sub-modules"
            PINIT["play_init.hsp"]
            PUPDATE["play_update.hsp"]
            PKEY["play_key_input.hsp"]
            PBTN["play_button_note.hsp"]
            PLASER["play_laser_note.hsp"]
            PDRAW["play_draw_frame.hsp"]
            PCHART["play_chart_load.hsp"]
            PGAME["play_game_logic.hsp"]
            PGFX["play_graphics.hsp"]
            PAUDIO["play_audio.hsp"]
            PAFX["play_audio_effects.hsp"]
            PVIDEO["play_video.hsp"]
            PUTILS["play_utils.hsp"]
            PSPLASH["play_splash_screen.hsp"]
        end

        subgraph "Audio Layer"
            FXDEF["fxdef.hsp<br/>FX Definitions"]
            KEYSND["key_sound.as<br/>(#module)"]
            LSLAM["laser_slam_sound.as<br/>(#module)"]
        end

        subgraph "Library / Utility Layer"
            WINAPI["winapi.as<br/>Win32 API Bindings"]
            KSHLIB["kshlib.as<br/>kshlib.dll Bindings"]
            KSMCORE["ksmcore.as<br/>ksmcore.dll Bindings"]
            KSON["kson.as<br/>kson.dll Bindings"]
            BASS["bass.as<br/>BASS Bindings"]
            BASSFX["bass_fx.as<br/>BASS FX Bindings"]
            BASSVST["bass_vst.as<br/>BASS VST Bindings"]
            BASSCONV["bassconv.as<br/>BASS Conv Bindings"]
            FSUTILS["filesystem_utils.as<br/>File System Utils"]
        end

        subgraph "Network Layer"
            IR["ir.hsp<br/>Internet Ranking (disabled)"]
        end
    end

    subgraph "External DLLs"
        BASSDLL["bass.dll"]
        BASSFXDLL["bass_fx.dll"]
        BASSVSTDLL["bass_vst.dll"]
        BASSCONVDLL["bassconv.dll"]
        KSHLIBDLL["kshlib.dll (C++)"]
        KSMCOREDLL["ksmcore.dll (C++)"]
        KSONDLL["kson.dll (C++)"]
        VSTPLUGINS["VST Plugins<br/>(regawo/flan/pitc/bitc/phas/tstp/sich.dll)"]
    end

    subgraph "HSP Standard Extensions"
        HGIMG3["HGIMG3<br/>3D Graphics"]
        HSPINET["hspinet<br/>Network"]
        D3M["d3m.hsp<br/>Timer"]
    end

    subgraph "OS / Platform"
        WIN32["Windows API<br/>(user32/kernel32/gdi32)"]
        MCI["MCI<br/>Media Player"]
    end

    KSM --> TITLE
    KSM --> SELECT
    KSM --> PLAY
    KSM --> RESULT
    KSM --> CRESULT
    KSM --> OPTION
    KSM --> GATE

    PLAY --> PINIT
    PLAY --> PUPDATE
    PLAY --> PKEY
    PLAY --> PBTN
    PLAY --> PLASER
    PLAY --> PDRAW
    PLAY --> PCHART
    PLAY --> PGAME
    PLAY --> PGFX
    PLAY --> PAUDIO
    PLAY --> PAFX
    PLAY --> PVIDEO
    PLAY --> PUTILS
    PLAY --> PSPLASH

    KSM --> BASS
    KSM --> BASSFX
    KSM --> BASSVST
    KSM --> BASSCONV
    KSM --> WINAPI
    KSM --> KSHLIB
    KSM --> KSMCORE
    KSM --> KSON
    KSM --> FSUTILS
    KSM --> IR
    KSM --> KEYSND
    KSM --> LSLAM
    KSM --> FXDEF

    KSE --> BASS
    KSE --> BASSFX
    KSE --> BASSVST
    KSE --> BASSCONV
    KSE --> WINAPI
    KSE --> KSMCORE
    KSE --> KSON
    KSE --> FSUTILS
    KSE --> KEYSND
    KSE --> LSLAM
    KSE --> FXDEF

    BASS --> BASSDLL
    BASSFX --> BASSFXDLL
    BASSVST --> BASSVSTDLL
    BASSCONV --> BASSCONVDLL
    KSHLIB --> KSHLIBDLL
    KSMCORE --> KSMCOREDLL
    KSON --> KSONDLL
    BASSVST --> VSTPLUGINS

    KSM --> HGIMG3
    KSM --> HSPINET
    KSM --> D3M
    KSE --> D3M

    WINAPI --> WIN32
    PVIDEO --> MCI
    IR --> HSPINET
```

## シーン遷移図

```mermaid
stateDiagram-v2
    [*] --> TITLE : Application Start

    TITLE --> IR_SIGN_IN : IR Login Selected
    TITLE --> SELECT : Start Game
    TITLE --> OPTION : Open Options
    TITLE --> GATE : Open INPUT GATE

    IR_SIGN_IN --> SELECT : Login Complete / Skip
    IR_SIGN_IN --> TITLE : Back (ESC)

    SELECT --> PLAY : Song Selected (Single)
    SELECT --> BEFORE_PLAY : Song Selected (Course)
    SELECT --> OPTION : Open Options
    SELECT --> TITLE : Back (ESC)

    BEFORE_PLAY --> PLAY : Course Prepared
    BEFORE_PLAY --> SELECT : Error / Cancel

    PLAY --> RESULT : Play Completed
    PLAY --> SELECT : Quit (ESC)

    RESULT --> SELECT : Continue (Single)
    RESULT --> PLAY : Next Song (Course)
    RESULT --> COURSE_RESULT : Course Complete

    COURSE_RESULT --> SELECT : Continue

    OPTION --> TITLE : Back

    GATE --> TITLE : Back (disabled)
```

## 音声システム構成図

```mermaid
graph LR
    subgraph "Audio Input"
        SONGFILE["Song File<br/>(.ogg/.mp3)"]
        KEYSNDFILE["Key Sound Files<br/>(.wav)"]
        SLAMSNDFILE["Slam Sound Files<br/>(.wav)"]
        SEFILES["SE Files<br/>(.wav/.ogg)"]
    end

    subgraph "BASS Engine"
        BASSINIT["BASS_Init<br/>(44100Hz)"]
        STREAM["BASS_StreamCreateFile<br/>Song Playback"]
        SAMPLE["BASS_SampleLoad<br/>SE Playback"]
    end

    subgraph "DSP / Effects Chain"
        BASSFXE["BASS FX<br/>Tempo Control"]
        VSTHOST["BASS VST Host"]
        subgraph "VST Plugins"
            REGAWO["regawo.dll<br/>Retrigger/Gate/Wobble/Echo"]
            FLAN["flan.dll<br/>Flanger"]
            PITC["pitc.dll<br/>PitchShift"]
            BITC["bitc.dll<br/>BitCrusher"]
            PHAS["phas.dll<br/>Phaser"]
            TSTP["tstp.dll<br/>TapeStop"]
            SICH["sich.dll<br/>SideChain"]
        end
    end

    subgraph "Key Sound System (ksmcore.dll)"
        KSKEYSND["CreateKeySound"]
        KSPLAY["PlayKeySound"]
        KSDESTROY["DestroyKeySound"]
    end

    subgraph "Output"
        SPEAKER["Audio Output"]
    end

    SONGFILE --> STREAM
    SEFILES --> SAMPLE
    KEYSNDFILE --> KSKEYSND
    SLAMSNDFILE --> KSKEYSND

    STREAM --> BASSFXE
    BASSFXE --> VSTHOST
    VSTHOST --> REGAWO
    VSTHOST --> FLAN
    VSTHOST --> PITC
    VSTHOST --> BITC
    VSTHOST --> PHAS
    VSTHOST --> TSTP
    VSTHOST --> SICH

    REGAWO --> SPEAKER
    FLAN --> SPEAKER
    PITC --> SPEAKER
    BITC --> SPEAKER
    PHAS --> SPEAKER
    TSTP --> SPEAKER
    SICH --> SPEAKER
    SAMPLE --> SPEAKER
    KSPLAY --> SPEAKER
```

## プレイ画面フレーム処理フロー

```mermaid
graph TB
    START["on_update_play"]

    TIMING["processPlayTiming<br/>Timing Sync with BASS"]
    CONTROL["processPlaybackControlInput<br/>Pause/Resume/Seek"]
    TICK["calculateCurrentTick<br/>Current Measure Position"]
    GAMESYS["UpdateGameSystem<br/>(ksmcore.dll)"]

    BTNSPEC["specifyJudgmentTargetOfButtonNotes<br/>BT/FX Target Determination"]
    LSRSPEC["specifyJudgmentTargetOfLaserNotes<br/>Laser Target Determination"]

    ESCINPUT["processEscKeyInput<br/>ESC Key Handling"]

    BTNJUDGE["processButtonNoteJudgment<br/>BT/FX Hit Detection"]
    LSRJUDGE["prepareLaserRelatedValues<br/>Laser Position Calculation"]

    SCORING["Score/Chain/Gauge Update"]

    FXPROC["Audio Effects Processing<br/>(VST Parameter Update)"]

    DRAW["drawFrame<br/>Lane/Notes/Laser Drawing"]
    UIDRAW["UI Drawing<br/>Score/Chain/Gauge/Info"]

    REDRAW["redraw 1<br/>Frame Output"]

    START --> TIMING
    TIMING --> CONTROL
    CONTROL --> TICK
    TICK --> GAMESYS
    GAMESYS --> BTNSPEC
    BTNSPEC --> LSRSPEC
    LSRSPEC --> ESCINPUT
    ESCINPUT --> BTNJUDGE
    BTNJUDGE --> LSRJUDGE
    LSRJUDGE --> SCORING
    SCORING --> FXPROC
    FXPROC --> DRAW
    DRAW --> UIDRAW
    UIDRAW --> REDRAW
```

## データフロー図（譜面読込〜プレイ）

```mermaid
graph LR
    subgraph "File System"
        KSH[".ksh File<br/>Chart Data"]
        SONGDIR["songs/ Directory"]
        SCOREDIR["score/ Directory"]
    end

    subgraph "Chart Loading"
        LOAD["loadKSHChartFile<br/>Read Text File"]
        PARSE["parseKSHChartParamLines<br/>Parse Metadata"]
        META["Metadata<br/>(title/artist/BPM/etc)"]
        NOTES["Note Data<br/>(BT/FX/Laser Arrays)"]
        FXDEFS["FX Definitions<br/>(User Effects)"]
    end

    subgraph "Game Play"
        JUDGE["Judgment System<br/>(Critical/Near/Error)"]
        SCORE["Score Calculation"]
        GAUGE["Gauge Management"]
        CHAIN["Chain Counter"]
    end

    subgraph "Result"
        DISPLAY["Result Display"]
        SAVE["Score Save"]
    end

    KSH --> LOAD
    LOAD --> PARSE
    PARSE --> META
    PARSE --> NOTES
    PARSE --> FXDEFS

    NOTES --> JUDGE
    JUDGE --> SCORE
    JUDGE --> GAUGE
    JUDGE --> CHAIN

    SCORE --> DISPLAY
    GAUGE --> DISPLAY
    CHAIN --> DISPLAY
    DISPLAY --> SAVE
    SAVE --> SCOREDIR
```

## 設定管理フロー図

```mermaid
graph TB
    subgraph "Configuration"
        CONFIGFILE["config.ini"]
        LANGFILE["lang/*.txt<br/>Language Files"]
    end

    subgraph "Load (Startup)"
        READCONF["noteload config.ini"]
        PARSECONF["Parse key=value lines<br/>(StartsWith matching)"]
        GLOBALVARS["Global Variables<br/>(key/joy/fullscr/hispeed/...)"]
    end

    subgraph "Save (On Change)"
        WRITECONF["*saveconfig Routine"]
        BUILDCONF["Build config string<br/>(key=value format)"]
        SAVECONF["notesave config.ini"]
    end

    subgraph "Settings Categories"
        KEYS["Key Bindings<br/>(key/key2/joy/joy2)"]
        DISPLAY["Display<br/>(fullscr/windowsize/lowquality)"]
        AUDIO["Audio<br/>(mastervol/bufferlength)"]
        GAMEPLAY["Gameplay<br/>(hispeed/bt_mode/fx_mode)"]
        TIMING["Timing<br/>(globaloffset/inputdelay)"]
        MISC["Misc<br/>(currentlang/currentdir)"]
    end

    CONFIGFILE --> READCONF
    READCONF --> PARSECONF
    PARSECONF --> GLOBALVARS
    GLOBALVARS --> KEYS
    GLOBALVARS --> DISPLAY
    GLOBALVARS --> AUDIO
    GLOBALVARS --> GAMEPLAY
    GLOBALVARS --> TIMING
    GLOBALVARS --> MISC

    KEYS --> BUILDCONF
    DISPLAY --> BUILDCONF
    AUDIO --> BUILDCONF
    GAMEPLAY --> BUILDCONF
    TIMING --> BUILDCONF
    MISC --> BUILDCONF
    BUILDCONF --> SAVECONF
    SAVECONF --> CONFIGFILE

    LANGFILE --> GLOBALVARS
```
