#include "dk_gce_main.h"
#include "dk_shared.h"
#include "qcommon.h"
#include "client.h"
#include "winquake.h"

cvar_t *gce_save_position;
cvar_t *gce_set_position;

void GCE_Init() {
    gce_save_position = Cvar_Get("gce_save_position", "0", 0);
    gce_set_position = Cvar_Get("gce_set_position", "0", 0);
}

//saved camera positions.
#define MAX_SAVE_POSITIONS 256
static vec3_t saved_positions[MAX_SAVE_POSITIONS];
static int num_saved_positions = 0;

//what the fuck ever, test setting the camera position.
bool animate_camera = false;

void GCE_ClientFrame() {
    //check if we are saving the current camera position.
    if (gce_save_position->value != 0.0f) {
        //turn it off.
        Cvar_SetValue("gce_save_position", 0.0f);

        if (num_saved_positions < MAX_SAVE_POSITIONS) {
            //save the position.
            VectorCopy(cl.refdef.vieworg, saved_positions[num_saved_positions]);
            num_saved_positions++;
        }
    }

    if (gce_set_position->value != 0.0f) {
        //turn it off
        Cvar_SetValue("gce_set_position", 0.0f);

        if (num_saved_positions > 0) {
            animate_camera = !animate_camera;
        }
    }
}

void GCE_SetCamera() {
    if (animate_camera == false) return;

    int use_pos = (GetTickCount() % (1000 * num_saved_positions)) / 1000;

    VectorCopy(saved_positions[use_pos], cl.refdef.vieworg);
}