# scfmod/fs25_terrafarm architecture

> Architecture diagram of the GitHub repository scfmod/fs25_terrafarm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/scfmod/fs25_terrafarm
- Repository: https://github.com/scfmod/fs25_terrafarm

## Overview

##### Project Type and Purpose
- TerraFarm is a game mod for Farming Simulator (FS25). It extends the base game by adding terraforming tools—landscaping, material management, and surveyor functionality—via Lua scripts, XML definitions, and in-game assets (models, textures).

##### Key Architectural Components
a. FS25 Game Engine (Host)
- Provides the mod loading mechanism, vehicle specialization hooks, GUI subsystem, event synchronization (multiplayer), terrain API, and asset pipeline.
b. Mod Descriptor & Configuration
- modDesc.xml / project.json: declare mod metadata, dependencies, assets, script entry points.
- XML folders: gui/dialogs, gui/frames, machines definitions under xml/, which FS engine reads at startup to inject UI and machine specs.
c. Core Script Modules (Lua)
- main.lua & ModController.lua: initialization, registration of event listeners, specializations, GUI screens, HUD.
- managers (MachineManager, ResourceManager): maintain global state, available machines/materials, map-resource areas.
- events: custom Lua events (SetEnabledEvent, SetMaterialsEvent, etc.) for decoupled communication and multiplayer sync.
- specializations: scripts that extend vehicle behavior (Machine.lua, Surveyor.lua) by hooking into FS vehicle lifecycle.
d. GUI & HUD Layer
- scripts/gui & xml/gui definitions: dialogs (SelectMachineDialog, GlobalSettingsDialog), frames, input controls—abstracted via GUI extensions.
- scripts/hud/elements & xml/gui/hud: overlay elements for in-game machine status, calibration display.
e. Landscaping & Visualization
- scripts/landscaping: core terraforming algorithms (Flatten, Raise, Slope, etc.) invoked by Machine specializations.
f. Assets
- l10n: translation XML for UI.

##### Interactions and Data Flow
1. FS Engine reads modDesc.xml → loads Lua scripts and XML UI/machine definitions.
2. ModController registers specializations with FS Vehicle System.
3. Player opens TerraFarm GUI → GUI modules dispatch events to managers.
4. Managers update state (selected machine, materials, surveyor calibration) and broadcast via custom events.
5. Machine specialization code listens to state changes → on work area activation, invokes landscaping functions.
6. Landscaping scripts call FS terrain API to deform terrain → results synced over multiplayer via FS event system.
8. XML-driven UI reflects state changes via data bound to Lua controllers.

##### Architectural Patterns & Principles
- Event-Driven: decoupling via Set*Event classes for state change and MP synchronization.
- Modular Specializations: inject functionality into FS vehicles without monoliths.
- MVC-like GUI: XML defines view, Lua scripts act as controller, managers hold model state.
- Separation of Concerns:
- Core logic (landscaping) vs. UI vs. visualization vs. data/config.
- Asset files separate from code.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% FS25 Engine
    subgraph "FS25 Engine" 
        direction TB
        Engine_FS["Game Engine"]:::engine
        VehicleSys["Vehicle System"]:::engine
        GUISys["GUI System"]:::engine
        TerrainAPI["Terrain API"]:::engine
        EventSync["Event Sync"]:::engine
    end

    %% TerraFarm Mod
    subgraph "TerraFarm Mod" 
        direction TB

        %% Initialization Layer
        subgraph "Initialization Layer" 
            direction TB
            Main["main.lua"]:::core
            ModCtrl["ModController.lua"]:::core
            ModSettings["ModSettings.lua"]:::core
            ModGui["ModGui.lua"]:::core
            ModHud["ModHud.lua"]:::core
            ModI3D["ModI3DManager.lua"]:::core
        end

        %% Specializations
        subgraph "Specializations" 
            direction TB
            SpecMachine["Machine.lua"]:::core
            SpecSurveyor["Surveyor.lua"]:::core
        end

        %% Managers
        subgraph "Managers" 
            direction TB
            MachineMgr["MachineManager.lua"]:::core
            ResourceMgr["ResourceManager.lua"]:::core
        end

        %% Events
        subgraph "Events" 
            direction TB
            E1["SetDefaultEnabledEvent.lua"]:::core
            E2["SetEnabledEvent.lua"]:::core
            E3["SetMaterialsEvent.lua"]:::core
            E4["SetResourcesEvent.lua"]:::core
        end

        %% Landscaping Algorithms
        subgraph "Landscaping Algorithms" 
            direction TB
            L1["LandscapingFlatten.lua"]:::core
            L2["LandscapingRaise.lua"]:::core
            L3["LandscapingSlope.lua"]:::core
            L4["LandscapingSmooth.lua"]:::core
            L5["LandscapingPaint.lua"]:::core
        end

        %% Visualization
        subgraph "Visualization" 
            direction TB
            Vis1["Shape.lua"]:::core
            Vis2["ShapeLine.lua"]:::core
            Vis3["CalibrationDisplay.lua"]:::core
        end

        %% GUI Layer
        subgraph "GUI Layer" 
            direction TB
            GUI_XML["xml/gui/"]:::assets
            GUI_Scripts["scripts/gui"]:::core
        end

        %% Assets
        subgraph "Assets" 
            direction TB
            ObjShapes["objects/shapes"]:::assets
            ObjPSV2["objects/surveyorPSV2"]:::assets
            Textures["textures"]:::assets
            L10n["l10n"]:::assets
        end

        %% Extensions
        subgraph "Extensions" 
            direction TB
            Ext1["GuiOverlayExtension.lua"]:::core
            Ext2["InteractiveControlExtension.lua"]:::core
            Ext3["SavegameControllerExtension.lua"]:::core
            Ext4["ShopControllerExtension.lua"]:::core
            Ext5["VehicleExtension.lua"]:::core
        end
    end

    %% Connections
    Engine_FS -->|loads| Main
    Main -->|registers| SpecMachine
    Main -->|registers| SpecSurveyor
    Main -->|registers| GUI_Scripts
    GUI_Scripts -->|dispatches events| E2
    E2 -->|updates| MachineMgr
    MachineMgr -->|publishes events| E3
    SpecMachine -->|invokes| L1
    SpecMachine -->|invokes| L2
    SpecMachine -->|invokes| L3
    L1 -->|calls| TerrainAPI
    L2 -->|calls| TerrainAPI
    L3 -->|calls| TerrainAPI
    L1 -->|reads| ObjShapes
    L2 -->|reads| ObjShapes
    Vis1 -->|renders| Engine_FS
    E2 <-->|sync| EventSync

    %% Click Events for component mappings
    click Main "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/main.lua"
    click ModCtrl "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/ModController.lua"
    click ModSettings "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/ModSettings.lua"
    click ModGui "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/ModGui.lua"
    click ModHud "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/ModHud.lua"
    click ModI3D "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/ModI3DManager.lua"
    click MachineMgr "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/managers/MachineManager.lua"
    click ResourceMgr "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/managers/ResourceManager.lua"
    click E1 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/events/SetDefaultEnabledEvent.lua"
    click E2 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/events/SetEnabledEvent.lua"
    click E3 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/events/SetMaterialsEvent.lua"
    click E4 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/events/SetResourcesEvent.lua"
    click SpecMachine "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/specializations/Machine.lua"
    click SpecSurveyor "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/specializations/Surveyor.lua"
    click L1 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/landscaping/LandscapingFlatten.lua"
    click L2 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/landscaping/LandscapingRaise.lua"
    click L3 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/landscaping/LandscapingSlope.lua"
    click L4 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/landscaping/LandscapingSmooth.lua"
    click L5 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/landscaping/LandscapingPaint.lua"
    click Vis1 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/visualization/Shape.lua"
    click Vis2 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/visualization/ShapeLine.lua"
    click Vis3 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/visualization/CalibrationDisplay.lua"
    click GUI_XML "https://github.com/scfmod/fs25_terrafarm/tree/main/xml/gui/"
    click GUI_Scripts "https://github.com/scfmod/fs25_terrafarm/tree/main/scripts/gui"
    click ObjShapes "https://github.com/scfmod/fs25_terrafarm/tree/main/objects/shapes/"
    click ObjPSV2 "https://github.com/scfmod/fs25_terrafarm/blob/main/objects/surveyorPSV2/psv2.i3d"
    click Textures "https://github.com/scfmod/fs25_terrafarm/blob/main/textures/ui_elements.dds"
    click L10n "https://github.com/scfmod/fs25_terrafarm/tree/main/l10n/"
    click Ext1 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/extensions/GuiOverlayExtension.lua"
    click Ext2 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/extensions/InteractiveControlExtension.lua"
    click Ext3 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/extensions/SavegameControllerExtension.lua"
    click Ext4 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/extensions/ShopControllerExtension.lua"
    click Ext5 "https://github.com/scfmod/fs25_terrafarm/blob/main/scripts/extensions/VehicleExtension.lua"

    %% Styles
    classDef engine fill:#D0E6FF,stroke:#009,stroke-width:1px;
    classDef core fill:#DFFFD0,stroke:#090,stroke-width:1px;
    classDef assets fill:#FFE8A0,stroke:#E67E00,stroke-width:1px;
```
