# SL(1)

> SL (Steam Locomotive) runs across your terminal when you type "sl" as you meant to type "ls". It's just a joke command, and not useful at all.

**Showcase URL:** https://shipdocs.sh/showcase/2ddacbda-f0bd-474b-b209-51f80a8208ec
**Repository:** https://github.com/mtoyoda/sl
**Docs count:** 2

## Overview

## Project Overview

This project is a terminal-based animation of a steam locomotive. It displays a moving train on the terminal, with options to alter its appearance and behavior.

## Tech Stack

*   **C**: The primary programming language.
*   **ncurses**: A library for creating text-based user interfaces in the terminal.

## High-Level Architecture

The core logic resides in `sl.c`.

*   **`main` function (`sl.c`)**: Parses command-line arguments to set options, initializes the ncurses environment, and enters a loop to render the train.
*   **`option` function (`sl.c`)**: Parses command-line flags to enable different features like accidents, flying, or specific locomotive models.
*   **`add_sl`, `add_D51`, `add_C51` functions (`sl.c`)**: These functions are responsible for drawing different representations of the locomotive and its associated elements (coal car, passenger car) at a given horizontal position (`x`). They use static arrays of strings defined in `sl.h` to represent the train's frames.
*   **`add_smoke` function (`sl.c`)**: Animates smoke particles emanating from the locomotive. It manages a static array of smoke structures to track their position, pattern, and type.
*   **`add_man` function (`sl.c`)**: Draws a small figure, presumably a person, in case of an "accident" option.
*   **`my_mvaddstr` function (`sl.c`)**: A helper function to print strings to the ncurses screen, handling potential out-of-bounds drawing.
*   **`sl.h`**: Contains definitions for the various character arrays that form the train's sprites, as well as constants for dimensions and patterns.

## Entry Points

1.  **`main` function in `sl.c`**: This is the primary entry point.
2.  **Execution Flow**:
    *   The `main` function is called with command-line arguments.
    *   It initializes the ncurses screen using `initscr()`.
    *   It enters a loop that iterates from `COLS - 1` down to a point where the train is off-screen.
    *   Inside the loop, based on the `LOGO`, `C51`, or default settings, one of `add_sl`, `add_C51`, or `add_D51` is called to draw the train at the current horizontal position `x`.
    *   `getch()` is called to wait for user input (though it's non-blocking due to `nodelay(stdscr, TRUE)`).
    *   `refresh()` updates the terminal display.
    *   `usleep(40000)` introduces a delay to control the animation speed.
    *   Once the loop breaks (train is off-screen), `endwin()` is called to restore the terminal to its normal state.

## Key Concepts

1.  **Ncurses Initialization and Control**: The `main` function sets up the ncurses environment using `initscr()`, `noecho()`, `curs_set(0)`, `nodelay(stdscr, TRUE)`, `leaveok(stdscr, TRUE)`, and `scrollok(stdscr, FALSE)`. Understanding these functions is crucial for how the terminal display is managed.
2.  **Sprite-Based Animation**: The train and its components are represented by static arrays of character strings (`sl`, `d51`, `c51`, `coal`, `car` in `sl.c`, and the `D51STR*`, `LOGO*`, etc. in `sl.h`). The animation is achieved by cycling through these arrays to display different frames.
3.  **Command-Line Options**: The `option` function and the loop in `main` that calls it demonstrate how command-line arguments (like `-a`, `-F`, `-l`, `-c`) modify the program's behavior (accidents, flying, different locomotive models).
4.  **Coordinate System and Drawing**: The `my_mvaddstr` function and its usage within the `add_*` functions show how characters are placed on the terminal screen using `(y, x)` coordinates. The animation loop manipulates the `x` coordinate to create the illusion of movement.
5.  **Smoke Animation**: The `add_smoke` function implements a particle system for smoke. It uses a static array `S` to store the state of each smoke particle (position, pattern, kind) and updates their positions and appearance in each animation frame.

## All docs

- **SL(1)** (overview)
- **Steam Locomotive Command** (module)

Browse the interactive showcase: https://shipdocs.sh/showcase/2ddacbda-f0bd-474b-b209-51f80a8208ec
