*** SERVER ***

64K RAM SYSTEM 38911 BASIC BYTES FREE
READY.
LOAD "PIXEL_SERVER",8,1
SEARCHING FOR PIXEL_SERVER
LOADING... READY.
RUN

[ACTIVE SYSTEM] Displaying source code in the background:

SOURCE_C // C64_CORE_LOGIC

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h> /* POSIX standard library for threads */
#include "city_world_map_1_1.h"

/* 1. PROGRAM STATE */
typedef struct {
    bool running;             /* Flag to check if the infinite loop is active */
    pthread_t thread_id;      /* The identifier for the ThePickaxe_Blessed thread */
    int contatore_cicli;      /* An example of shared data */
} AppState;

typedef void (*CommandFunc)(AppState *state, const char *input_line);

typedef struct {
    const char *name;
    CommandFunc function;
    const char *help;
} Command;

/* 2. THE INFINITE LOOP OF PIXEL_SERVER */
/* Executed in the Thread. The thread function must ALWAYS have this signature: void* name(void*) */
void* loop_infinito_PIXEL_SERVER(void *arg) {
    AppState *state = (AppState *)arg;

    printf("\nTHREAD: Pixel_server logic started in background\n");

    /* This is your server/video game style loop */
    while (state->running) {
         sic_mundus_creatus_est(world, game_world, 60606060, 17171717);

        /* INSERT YOUR LOGIC HERE */
        state->contatore_cicli++; /* Example: simulates a calculation cycle */

        /* Prevents the CPU from hitting 100% usage unnecessarily on Slackware / FreeBSD (e.g., 20Hz or once per second) */
        sleep(1);
    }

    printf("\nTHREAD: PIXEL_SERVER logic stopped successfully\n");
    return NULL;
}

/* 3. INTERNAL CLI COMMANDS */
void cmd_avvia(AppState *state, const char *input) {
    if (state->running) {
        printf("The PIXEL_SERVER server is already running\n");
        return;
    }

    state->running = true;

    /* Create the separate thread that runs loop_infinito_PIXEL_SERVER in the background.
       We pass &state so the thread can read and write the same variables. */
    if (pthread_create(&state->thread_id, NULL, loop_infinito_PIXEL_SERVER, state) != 0) {
        printf("Critical error: unable to create the thread\n");
        state->running = false;
    }
}

void cmd_ferma(AppState *state, const char *input) {
    if (!state->running) {
        printf("The PIXEL_SERVER server is already stopped\n");
        return;
    }

    printf("Stopping PIXEL_SERVER requested\n");
    state->running = false; /* The infinite loop in the thread will see this false and shut down */

    /* Wait for the thread to finish its last cycle and close completely */
    pthread_join(state->thread_id, NULL);
    printf("Server stopped\n");
}

void cmd_status(AppState *state, const char *input) {
    printf("PIXEL_SERVER STATUS\n");
    printf("Running: %s\n", state->running ? "YES" : "NO");
    printf("Cycles executed by the server: %d\n", state->contatore_cicli);
}

void cmd_exit(AppState *state, const char *input) {
    printf("Shutting down...\n");

    if (state->running) {
        state->running = false;
        pthread_join(state->thread_id, NULL); /* Close the thread before exiting the app */
    }

    printf("Goodbye\n");
    exit(0);
}

void cmd_help(AppState *state, const char *input);

/* 4. UPDATED COMMAND TABLE */
static const Command command_table[] = {
    {"help", cmd_help, "Shows this list of commands"},
    {"start", cmd_avvia, "Starts the PIXEL_SERVER server in the background"},
    {"stop", cmd_ferma, "Stops the PIXEL_SERVER server temporarily"},
    {"status", cmd_status, "Shows the live data of the background server"},
    {"exit", cmd_exit, "Shuts everything down and exits the program"}
};

static const int num_commands = sizeof(command_table) / sizeof(Command);

void cmd_help(AppState *state, const char *input) {
    for (int i = 0; i < num_commands; i++) {
        printf("%-10s %s\n", command_table[i].name, command_table[i].help);
    }
}

/* 5. MAIN CODE */
int main(void) {
    /* Initialize the initial state */
    AppState state = { .running = false, .contatore_cicli = 0 };
    char input_buffer[100];
    char comando_estratto[50];

    printf("Server Controller PIXEL_SERVER\n");

    while (true) {
        printf("\nPIXEL_SERVER prompt> ");
        fflush(stdout);

        if (fgets(input_buffer, sizeof(input_buffer), stdin) == NULL) {
            break;
        }

        input_buffer[strcspn(input_buffer, "\n")] = 0;

        if (strlen(input_buffer) == 0) {
            continue;
        }

        if (sscanf(input_buffer, "%49s", comando_estratto) <= 0) {
            continue;
        }

        bool comando_trovato = false;
        for (int i = 0; i < num_commands; i++) {
            if (strcmp(comando_estratto, command_table[i].name) == 0) {
                command_table[i].function(&state, input_buffer);
                comando_trovato = true;
                break;
            }
        }

        if (!comando_trovato) {
            printf("Unknown command\n");
        }
    }

    return 0;
}



}