// Copyright (C) 1999-2000 Id Software, Inc.
//
/*
=======================================================================
 
USER INTERFACE MAIN
 
=======================================================================
*/

/*
The work contained within this file is software written by various
copyright holders.  The initial contributor, Id Software holds all
copyright over their software.  However, software used and written by
and for UI Enhanced has copyrights held by the initial author of the
software.
 
The changes written by and for UI Enhanced are contained alongside the
original work from Id Software for convenience and ease of interoperability.
 
For the code contained herein that was written by Id Software, see
the license agreement on their original archive for restrictions and
limitations.
 
The UI Enhanced copyright owner permit free reuse of his code contained
herein, as long as the following terms are met:
 
----------------------------------------------------------------------
1) Credit is given in a place where users of the mod may read
   it. (Title screen, credit screen or README will do).  The
   recommended format is: "First, Last, alias, email"
 
2) There are no attempts to misrepresent the public as to who made the
   alterations.  The UI Enhanced copyright owner does not give permission
   for others to release software under the UI Enhanced name.
----------------------------------------------------------------------
 
Ian Jefferies   - HypoThermia ( uie@planetquake.com )
http://www.planetquake.com/uie
*/


#include "../q3_ui/q3ui_local.h"
//#include "../cgame/cg_local.h"

/*
================
vmMain
 
This is the only way control passes into the module.
This must be the very first function compiled into the .qvm file
================
*/
int vmMain( int command, int arg0, int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7, int arg8, int arg9, int arg10, int arg11  ) {
    switch ( command ) {
    case UI_GETAPIVERSION:
        return UI_API_VERSION;

    case UI_INIT:
		//trap_Cmd_ExecuteText( EXEC_NOW, "pb_sv_disable\n");
 		trap_Cvar_Set("sv_punkbuster", "0");
		//trap_Cvar_Update(&sv_punkbuster);
		//trap_Cmd_ExecuteText( EXEC_NOW, "pb_cl_disable\n");
		//trap_Cvar_Set("cl_punkbuster", "0");
		//trap_Cvar_Update(&cl_punkbuster);
		//trap_Cvar_Update(&cl_punkbuster);
/*
#ifdef MISSIONPACK
		trap_Cvar_Set("fs_basegame","missionpack");
#endif
*/
		UI_Init();
        return 0;

    case UI_SHUTDOWN:
        UI_Shutdown();
        return 0;

    case UI_KEY_EVENT:
        UI_KeyEvent( arg0, arg1 );
        return 0;

    case UI_MOUSE_EVENT:
        UI_MouseEvent( arg0, arg1 );
        return 0;

    case UI_REFRESH:
        UI_Refresh( arg0 );
        return 0;

    case UI_IS_FULLSCREEN:
        return UI_IsFullscreen();

    case UI_SET_ACTIVE_MENU:
        UI_SetActiveMenu( arg0 );
        return 0;

    case UI_CONSOLE_COMMAND:
        return UI_ConsoleCommand(arg0);

    case UI_DRAW_CONNECT_SCREEN:
        UI_DrawConnectScreen( arg0 );
        return 0;
    case UI_HASUNIQUECDKEY:				// mod authors need to observe this
        return qfalse;  // bk010117 - change this to qfalse for mods!
    }

    return -1;
}

/*
================
cvars
================
*/

typedef struct {
    vmCvar_t	*vmCvar;
    char		*cvarName;
    char		*defaultString;
    int			cvarFlags;
} cvarTable_t;

/*vmCvar_t	ui_ffa_fraglimit;
vmCvar_t	ui_ffa_timelimit;
 
vmCvar_t	ui_tourney_fraglimit;
vmCvar_t	ui_tourney_timelimit;
 
vmCvar_t	ui_team_fraglimit;
vmCvar_t	ui_team_timelimit;
vmCvar_t	ui_team_friendly;
 
vmCvar_t	ui_ctf_capturelimit;
vmCvar_t	ui_ctf_timelimit;
vmCvar_t	ui_ctf_friendly;*/

vmCvar_t	ui_arenasFile;
vmCvar_t	ui_botsFile;
vmCvar_t	ui_spScores1;
vmCvar_t	ui_spScores2;
vmCvar_t	ui_spScores3;
vmCvar_t	ui_spScores4;
vmCvar_t	ui_spScores5;
vmCvar_t	ui_spAwards;
vmCvar_t	ui_spVideos;
vmCvar_t	ui_spSkill;

vmCvar_t	ui_spSelection;

vmCvar_t	ui_browserMaster;
vmCvar_t	ui_browserGameType;
vmCvar_t	ui_browserSortKey;
vmCvar_t	ui_browserShowFull;
vmCvar_t	ui_browserShowEmpty;

vmCvar_t	ui_brassTime;
vmCvar_t	ui_drawCrosshair;
vmCvar_t	ui_drawCrosshairNames;
vmCvar_t	ui_marks;

vmCvar_t	ui_server1;
vmCvar_t	ui_server2;
vmCvar_t	ui_server3;
vmCvar_t	ui_server4;
vmCvar_t	ui_server5;
vmCvar_t	ui_server6;
vmCvar_t	ui_server7;
vmCvar_t	ui_server8;
vmCvar_t	ui_server9;
vmCvar_t	ui_server10;
vmCvar_t	ui_server11;
vmCvar_t	ui_server12;
vmCvar_t	ui_server13;
vmCvar_t	ui_server14;
vmCvar_t	ui_server15;
vmCvar_t	ui_server16;

vmCvar_t	ui_cdkeychecked;

// UIE conventional cvars
vmCvar_t	uie_animsfx;
vmCvar_t	uie_mapicons;
vmCvar_t	uie_autoclosebotmenu;
vmCvar_t	uie_ingame_dynamicmenu;
vmCvar_t	uie_map_multisel;
vmCvar_t	uie_map_list;
vmCvar_t	uie_bot_multisel;
vmCvar_t	uie_bot_list;
vmCvar_t	uie_olditemmenu;
//vmCvar_t	cl_guid;
//vmCvar_t	cg_version;	

//vmCvar_t	cg_enemyModel;
//vmCvar_t	cg_enemyHeadModel;

static cvarTable_t		cvarTable[] = {
                                      /*	{ &ui_ffa_fraglimit, "ui_ffa_fraglimit", "20", CVAR_ARCHIVE },
                                      	{ &ui_ffa_timelimit, "ui_ffa_timelimit", "0", CVAR_ARCHIVE },
                                       
                                      	{ &ui_tourney_fraglimit, "ui_tourney_fraglimit", "0", CVAR_ARCHIVE },
                                      	{ &ui_tourney_timelimit, "ui_tourney_timelimit", "15", CVAR_ARCHIVE },
                                       
                                      	{ &ui_team_fraglimit, "ui_team_fraglimit", "0", CVAR_ARCHIVE },
                                      	{ &ui_team_timelimit, "ui_team_timelimit", "20", CVAR_ARCHIVE },
                                      	{ &ui_team_friendly, "ui_team_friendly",  "1", CVAR_ARCHIVE },
                                       
                                      	{ &ui_ctf_capturelimit, "ui_ctf_capturelimit", "8", CVAR_ARCHIVE },
                                      	{ &ui_ctf_timelimit, "ui_ctf_timelimit", "30", CVAR_ARCHIVE },
                                      	{ &ui_ctf_friendly, "ui_ctf_friendly",  "0", CVAR_ARCHIVE },*/
//									{ &cl_guid, "cl_guid", (char *)&cl_guid, CVAR_INIT | CVAR_ARCHIVE | CVAR_NORESTART | CVAR_ROM},
									//{ &cl_guid, "cl_guid", (char *)&cl_guid, CVAR_ROM },
//									{ &cg_version, "cl_version", UIE_VERSION , CVAR_USERINFO | CVAR_ARCHIVE | CVAR_NORESTART | CVAR_ROM  },
                                    { &ui_arenasFile, "g_arenasFile", "", CVAR_INIT|CVAR_ROM },
                                    { &ui_botsFile, "g_botsFile", "", CVAR_INIT|CVAR_ROM },
                                    { &ui_spScores1, "g_spScores1", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spScores2, "g_spScores2", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spScores3, "g_spScores3", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spScores4, "g_spScores4", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spScores5, "g_spScores5", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spAwards, "g_spAwards", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spVideos, "g_spVideos", "", CVAR_ARCHIVE | CVAR_ROM },
                                    { &ui_spSkill, "g_spSkill", "2", CVAR_ARCHIVE | CVAR_LATCH },
                                    { &ui_spSelection, "ui_spSelection", "", CVAR_ROM },
                                    { &ui_browserMaster, "ui_browserMaster", "0", CVAR_ARCHIVE },
                                    { &ui_browserGameType, "ui_browserGameType", "0", CVAR_ARCHIVE },
                                    { &ui_browserSortKey, "ui_browserSortKey", "4", CVAR_ARCHIVE },
                                    { &ui_browserShowFull, "ui_browserShowFull", "1", CVAR_ARCHIVE },
                                    { &ui_browserShowEmpty, "ui_browserShowEmpty", "1", CVAR_ARCHIVE },
                                    { &ui_brassTime, "cg_brassTime", "2500", CVAR_ARCHIVE },
                                    { &ui_drawCrosshair, "cg_drawCrosshair", "4", CVAR_ARCHIVE },
                                    { &ui_drawCrosshairNames, "cg_drawCrosshairNames", "1", CVAR_ARCHIVE },
                                    { &ui_marks, "cg_marks", "1", CVAR_ARCHIVE },
                                    { &ui_server1, "server1", "", CVAR_ARCHIVE },
                                    { &ui_server2, "server2", "", CVAR_ARCHIVE },
                                    { &ui_server3, "server3", "", CVAR_ARCHIVE },
                                    { &ui_server4, "server4", "", CVAR_ARCHIVE },
                                    { &ui_server5, "server5", "", CVAR_ARCHIVE },
                                    { &ui_server6, "server6", "", CVAR_ARCHIVE },
                                    { &ui_server7, "server7", "", CVAR_ARCHIVE },
                                    { &ui_server8, "server8", "", CVAR_ARCHIVE },
                                    { &ui_server9, "server9", "", CVAR_ARCHIVE },
                                    { &ui_server10, "server10", "", CVAR_ARCHIVE },
                                    { &ui_server11, "server11", "", CVAR_ARCHIVE },
                                    { &ui_server12, "server12", "", CVAR_ARCHIVE },
                                    { &ui_server13, "server13", "", CVAR_ARCHIVE },
                                    { &ui_server14, "server14", "", CVAR_ARCHIVE },
                                    { &ui_server15, "server15", "", CVAR_ARCHIVE },
                                    { &ui_server16, "server16", "", CVAR_ARCHIVE },
                                    { &ui_cdkeychecked, "ui_cdkeychecked", "0", CVAR_ROM },
                                    { &uie_map_multisel, "uie_map_multisel", "0", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_map_list, "uie_map_list", "0", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_bot_multisel, "uie_bot_multisel", "0", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_bot_list, "uie_bot_list", "0", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_ingame_dynamicmenu, "uie_ingame_dynamicmenu", "1", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_animsfx, "uie_s_animsfx", "1", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_mapicons, "uie_mapicons", "0", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_autoclosebotmenu, "uie_autoclosebotmenu", "0", CVAR_ROM|CVAR_ARCHIVE },
                                    { &uie_olditemmenu, "uie_olditemmenu", "0", CVAR_ARCHIVE },
//									{ &cg_enemyModel, "cg_enemyModel", "visor", CVAR_USERINFO | CVAR_ARCHIVE },
//									{ &cg_enemyHeadModel, "cg_enemyHeadmodel", "visor", CVAR_USERINFO | CVAR_ARCHIVE }
                                  };

static int		cvarTableSize = sizeof(cvarTable) / sizeof(cvarTable[0]);


/*
=================
UI_RegisterCvars
=================
*/
void UI_RegisterCvars( void ) {
    int			i;
    cvarTable_t	*cv;

    for ( i = 0, cv = cvarTable ; i < cvarTableSize ; i++, cv++ ) {
        trap_Cvar_Register( cv->vmCvar, cv->cvarName, cv->defaultString, cv->cvarFlags );
    }
	
//	trap_Cvar_Update(&cl_guid);

    // we also set default values for the disable_* Cvars here
    // since first usage defines their default value
    UIE_StartServer_RegisterDisableCvars(qtrue);
}

/*
=================
UI_UpdateCvars
=================
*/
void UI_UpdateCvars( void ) {
    int			i;
    cvarTable_t	*cv;

    for ( i = 0, cv = cvarTable ; i < cvarTableSize ; i++, cv++ ) {
        trap_Cvar_Update( cv->vmCvar );
    }
}



/*
=================
UI_IsValidCvar
=================
*/
qboolean UI_IsValidCvar(const char* cvar) {
    int i;

    for (i = 0; i < cvarTableSize; i++) {
        if (!Q_stricmp(cvar, cvarTable[i].cvarName))
            return qtrue;
    }
    return qfalse;
}



