/*
 * This file contains all the update functions.
 */
#include <sys/types.h>
#include <stdio.h>

/* include main header file */
#include "mud.h"

TIME_INFO_DATA time_data;

/*
 * Update_handler()
 *
 * This is the toplevel update function.
 */
void update_world();

void update_handler()
{
    static int pulse_world;
    static int pulse_doors;
    
    if (--pulse_world <= 0)
    {
        pulse_world = PULSE_WORLD;
        update_world();
    }
    
    if (--pulse_doors <= 0)
    {
        pulse_doors = PULSE_DOORS;
        close_doors();
    }
} 


void close_doors()
{
    int x;
    int y;
    
    for (y = 0; y < MAX_Y; y++)
    {
        for (x = 0; x < MAX_X; x++)
        {
            if (map[x][y] == 19)
                map[x][y] = 14;
        }
    }
}

void update_world()
{
    D_MOBILE *xMob;
    D_SOCKET *dsock;
    static char buf[MAX_BUFFER];
    bool send_the_thing = FALSE;
    
    switch(++time_data.hour)
    {
        case 5:
            time_data.sunlight  = SUN_RISE;
            sprintf (buf, "The day has begun.\n\r");
            send_the_thing  = TRUE;
            break;
        case 6:
            time_data.sunlight  = SUN_LIGHT;
            sprintf (buf, "The sun rises in the east.\n\r");
            send_the_thing  = TRUE;
            break;
        case 12:
            time_data.sunlight  = SUN_HIGH;
            sprintf (buf, "The sun is high in the sky.\n\r");
            send_the_thing  = TRUE;
            break;
        case 19:
            time_data.sunlight  = SUN_SETTING;
            sprintf (buf, "The orange sun begins to decend into the underworld.\n\r");
            send_the_thing  = TRUE;
            break;
        case 20:
            time_data.sunlight  = SUN_DARK;
            sprintf (buf, "The night decends upon the world.\n\r");
            send_the_thing  = TRUE;
            break;
        case 24:
            time_data.hour = 0;
            time_data.day++;
            sprintf (buf, "The clock strikes Midnight.\n\r");
            send_the_thing  = TRUE;
            break;
    }

    /* Increment Day */
    if (time_data.day >= 7)
    {
        time_data.day = 0;
        time_data.week++;
    }
    /* Increment Week */
    if (time_data.week >= 4)
    {
        time_data.week = 0;
        time_data.month++;
    }
    /* Increment Month */
    if (time_data.month >= 12)
    {
        time_data.month = 0;
        time_data.year++;
    }
    
    for (dsock = dsock_list; dsock; dsock = dsock->next)
    {
        if (dsock->state != STATE_PLAYING)
            continue;
        if ((xMob = dsock->player) == NULL)
            continue;
        /*  Disabled due to Spamming me when im idle, asshole */
        /*
        if (send_the_thing  == TRUE)
            stc(buf, xMob);
        */
    }
}


/*
 * Days of the week
 */
char *days[7] =
{
    "Morndas",
    "Tirdas",
    "Middas",
    "Turdas",
    "Fredas",
    "Loredas",
    "Sundas"
};

/*
 * Week names
 */
char *week[4] =
{
    "Fiswok",
    "Siwok",
    "Tiewok",
    "Quashwok",
};

char *month[12] =
{
    /* Winter */
    "Sun's Dusk",
    "Evening Star",
    "Morning Star",
    /* Spring */
    "Sun's Dawn",
    "First Seed",
    "Rain's Hand",
    /* Summer */
    "Second Seed",
    "Mid Year",
    "Sun's Height",
    /* Fall */
    "Last Seed",
    "Hearthfire",
    "Frostfall",
};

void cmd_time (D_M * ch, char *arg)
{
    BUFFER *buf = buffer_new(MAX_BUFFER);
    
    bprintf(buf, "System time is %s", ctime (&current_time));
    bprintf(buf, "The day is the %s %s, of %s in the year %d.\n\r",
        week[time_data.week],
        days[time_data.day],
        month[time_data.month],
        time_data.year);
    bprintf(buf, "It is %d%s o'clock.\n\r",
        (time_data.hour % 12 == 0) ? 12 : time_data.hour % 12,
            time_data.hour >= 12 ? "pm" : "am");
    
    text_to_mobile(ch, buf->data);
    buffer_free(buf);
}

/*
 * save_time()
 *
 * Saves time!
 */
void save_time ()
{
    FILE * fp;
    
    if (!(fp = fopen (TIME_FILE,"w+")))
    {
        log_string ("*** TIME  *** creation of new time_file failed");
    }
    else
    {
        int total;
        total = fprintf (fp, "%d %d %d %d %d",
            time_data.hour,
            time_data.day,
            time_data.week,
            time_data.month,
            time_data.year);
        if (total<4)
            log_string("*** TIME  *** failed fprintf to time_file");
        fclose(fp);
    }
}
/*
 * load_time()
 *
 * oads up the time stuff
 */
void load_time()
{
    int total;
    bool fail=FALSE;
    /* Try and load time from file first */
    FILE *fp;
    if ((fp = fopen (TIME_FILE, "r")))
    {
        log_string ("*** TIME  *** opening time_file");
        total=fscanf(fp, "%d %d %d %d %d",
            &(time_data.hour),
            &(time_data.day),
            &(time_data.week),
            &(time_data.month),
            &(time_data.year));
        if (total < 4)
        {
            log_string ("*** TIME  *** loading stored time failed, using default");
            fail=TRUE;
        }
        fclose (fp);
    }
    else
    {
        log_string ("*** TIME  *** failed open of time_file, loading default time.");
        fail=TRUE;
    }

    if (fail)
    {
        time_data.hour  = 12;
        time_data.day   = 1;
        time_data.week  = 3;
        time_data.month = 7;
        time_data.year  = 1004;
        log_string ("*** TIME  *** : Loading Default game time.");
    }
}