added DP_ASINACOSATANATAN2TAN extension which adds common trig functions missing...
[xonotic/darkplaces.git] / mvm_cmds.c
index 5636ad0..4391965 100644 (file)
@@ -4,7 +4,59 @@
 // Menu
 
 char *vm_m_extensions =
-"DP_CINEMATIC_DPV";
+"DP_CINEMATIC_DPV "
+"DP_QC_ASINACOSATANATAN2TAN";
+
+/*
+=========
+VM_M_precache_file
+
+string precache_file(string)
+=========
+*/
+void VM_M_precache_file (void)
+{      // precache_file is only used to copy files with qcc, it does nothing
+       VM_SAFEPARMCOUNT(1,VM_precache_file);
+
+       PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
+}
+
+/*
+=========
+VM_M_preache_error
+
+used instead of the other VM_precache_* functions in the builtin list
+=========
+*/
+
+void VM_M_precache_error (void)
+{
+       PRVM_ERROR ("PF_Precache_*: Precache can only be done in spawn functions");
+}
+
+/*
+=========
+VM_M_precache_sound
+
+string precache_sound (string sample)
+=========
+*/
+void VM_M_precache_sound (void)
+{
+       const char      *s;
+
+       VM_SAFEPARMCOUNT(1, VM_precache_sound);
+
+       s = PRVM_G_STRING(OFS_PARM0);
+       PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
+       VM_CheckEmptyString (s);
+
+       if(snd_initialized.integer && !S_PrecacheSound (s,true, true))
+       {
+               VM_Warning("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
+               return;
+       }
+}
 
 /*
 =========
@@ -26,7 +78,7 @@ void VM_M_setmousetarget(void)
                in_client_mouse = true;
                break;
        default:
-               PRVM_ERROR("VM_M_setmousetarget: wrong destination %i !\n",PRVM_G_FLOAT(OFS_PARM0));
+               PRVM_ERROR("VM_M_setmousetarget: wrong destination %f !",PRVM_G_FLOAT(OFS_PARM0));
        }
 }
 
@@ -75,7 +127,7 @@ void VM_M_setkeydest(void)
                // key_dest = key_message
                // break;
        default:
-               PRVM_ERROR("VM_M_setkeydest: wrong destination %i !\n",prog->globals[OFS_PARM0]);
+               PRVM_ERROR("VM_M_setkeydest: wrong destination %f !", PRVM_G_FLOAT(OFS_PARM0));
        }
 }
 
@@ -123,19 +175,19 @@ void VM_M_callfunction(void)
        const char *s;
 
        if(prog->argc == 0)
-               PRVM_ERROR("VM_M_callfunction: 1 parameter is required !\n");
+               PRVM_ERROR("VM_M_callfunction: 1 parameter is required !");
 
        s = PRVM_G_STRING(OFS_PARM0 + (prog->argc - 1));
 
        if(!s)
-               PRVM_ERROR("VM_M_callfunction: null string !\n");
+               PRVM_ERROR("VM_M_callfunction: null string !");
 
        VM_CheckEmptyString(s);
 
        func = PRVM_ED_FindFunction(s);
 
        if(!func)
-               PRVM_ERROR("VM_M_callfunciton: function %s not found !\n", s);
+               PRVM_ERROR("VM_M_callfunciton: function %s not found !", s);
        else if (func->first_statement < 0)
        {
                // negative statements are built in functions
@@ -172,7 +224,7 @@ void VM_M_isfunction(void)
        s = PRVM_G_STRING(OFS_PARM0);
 
        if(!s)
-               PRVM_ERROR("VM_M_isfunction: null string !\n");
+               PRVM_ERROR("VM_M_isfunction: null string !");
 
        VM_CheckEmptyString(s);
 
@@ -198,15 +250,17 @@ void VM_M_writetofile(void)
 
        VM_SAFEPARMCOUNT(2, VM_M_writetofile);
 
-       file = VM_GetFileHandle( PRVM_G_FLOAT(OFS_PARM0) );
-       if( !file ) {
+       file = VM_GetFileHandle( (int)PRVM_G_FLOAT(OFS_PARM0) );
+       if( !file )
+       {
+               VM_Warning("VM_M_writetofile: invalid or closed file handle\n");
                return;
        }
-       
+
        ent = PRVM_G_EDICT(OFS_PARM1);
        if(ent->priv.required->free)
        {
-               Con_Printf("VM_M_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_EDICT_NUM(OFS_PARM1));
+               VM_Warning("VM_M_writetofile: %s: entity %i is free !\n", PRVM_NAME, PRVM_EDICT_NUM(OFS_PARM1));
                return;
        }
 
@@ -226,7 +280,7 @@ void VM_M_getresolution(void)
        int nr;
        VM_SAFEPARMCOUNT(1, VM_getresolution);
 
-       nr = PRVM_G_FLOAT(OFS_PARM0);
+       nr = (int)PRVM_G_FLOAT(OFS_PARM0);
 
 
        PRVM_G_VECTOR(OFS_RETURN)[0] = video_resolutions[nr][0];
@@ -236,45 +290,6 @@ void VM_M_getresolution(void)
 
 /*
 =========
-VM_M_keynumtostring
-
-string keynumtostring(float keynum)
-=========
-*/
-void VM_M_keynumtostring(void)
-{
-       int keynum;
-       char *tmp;
-       VM_SAFEPARMCOUNT(1, VM_M_keynumtostring);
-
-       keynum = PRVM_G_FLOAT(OFS_PARM0);
-
-       tmp = VM_GetTempString();
-
-       strcpy(tmp, Key_KeynumToString(keynum));
-
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(tmp);
-}
-
-/*
-=========
-VM_M_stringtokeynum
-
-float stringtokeynum(string key)
-=========
-*/
-void VM_M_stringtokeynum( void )
-{
-       const char *str;
-       VM_SAFEPARMCOUNT( 1, VM_M_keynumtostring );
-
-       str = PRVM_G_STRING( OFS_PARM0 );
-
-       PRVM_G_INT(OFS_RETURN) = Key_StringToKeynum( str );
-}
-
-/*
-=========
 VM_M_findkeysforcommand
 
 string findkeysforcommand(string command)
@@ -303,7 +318,7 @@ void VM_M_findkeysforcommand(void)
        M_FindKeysForCommand(cmd, keys);
 
        for(i = 0; i < NUMKEYS; i++)
-               ret = strcat(ret, va(" \'%i\'", keys[i]));
+               strlcat(ret, va(" \'%i\'", keys[i]), VM_STRINGTEMP_LENGTH);
 
        PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(ret);
 }
@@ -333,7 +348,7 @@ void VM_M_getserverliststat( void )
 
        PRVM_G_FLOAT( OFS_RETURN ) = 0;
 
-       type = PRVM_G_FLOAT( OFS_PARM0 );
+       type = (int)PRVM_G_FLOAT( OFS_PARM0 );
        switch(type)
        {
        case 0:
@@ -360,7 +375,7 @@ void VM_M_getserverliststat( void )
                PRVM_G_FLOAT ( OFS_RETURN ) = serverlist_sortdescending;
                return;
        default:
-               Con_Printf( "VM_M_getserverliststat: bad type %i!\n", type );
+               VM_Warning( "VM_M_getserverliststat: bad type %i!\n", type );
        }
 }
 
@@ -394,17 +409,18 @@ void VM_M_setserverlistmaskstring( void )
        int field;
 
        VM_SAFEPARMCOUNT( 4, VM_M_setserverlistmaskstring );
-       str = PRVM_G_STRING( OFS_PARM1 );
+       str = PRVM_G_STRING( OFS_PARM2 );
        if( !str )
                PRVM_ERROR( "VM_M_setserverlistmaskstring: null string passed!" );
 
-       masknr = PRVM_G_FLOAT( OFS_PARM0 );
+       masknr = (int)PRVM_G_FLOAT( OFS_PARM0 );
        if( masknr >= 0 && masknr <= SERVERLIST_ANDMASKCOUNT )
                mask = &serverlist_andmasks[masknr];
        else if( masknr >= 512 && masknr - 512 <= SERVERLIST_ORMASKCOUNT )
                mask = &serverlist_ormasks[masknr - 512 ];
-       else {
-               Con_Printf( "VM_M_setserverlistmaskstring: invalid mask number %i\n", masknr );
+       else
+       {
+               VM_Warning( "VM_M_setserverlistmaskstring: invalid mask number %i\n", masknr );
                return;
        }
 
@@ -412,27 +428,27 @@ void VM_M_setserverlistmaskstring( void )
 
        switch( field ) {
                case SLIF_CNAME:
-                       strncpy( mask->info.cname, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.cname) );
+                       strlcpy( mask->info.cname, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.cname) );
                        break;
                case SLIF_NAME:
-                       strncpy( mask->info.name, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.name)  );
+                       strlcpy( mask->info.name, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.name)  );
                        break;
                case SLIF_MAP:
-                       strncpy( mask->info.map, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.map)  );
+                       strlcpy( mask->info.map, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.map)  );
                        break;
                case SLIF_MOD:
-                       strncpy( mask->info.mod, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.mod)  );
+                       strlcpy( mask->info.mod, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.mod)  );
                        break;
                case SLIF_GAME:
-                       strncpy( mask->info.game, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.game)  );
+                       strlcpy( mask->info.game, PRVM_G_STRING( OFS_PARM2 ), sizeof(mask->info.game)  );
                        break;
                default:
-                       Con_Printf( "VM_M_setserverlistmaskstring: Bad field number %i passed!\n", field );
+                       VM_Warning( "VM_M_setserverlistmaskstring: Bad field number %i passed!\n", field );
                        return;
        }
 
        mask->active = true;
-       mask->tests[field] = (int) PRVM_G_FLOAT( OFS_PARM3 );
+       mask->tests[field] = (serverlist_maskop_t)((int)PRVM_G_FLOAT( OFS_PARM3 ));
 }
 
 /*
@@ -453,17 +469,18 @@ void VM_M_setserverlistmasknumber( void )
        int field;
        VM_SAFEPARMCOUNT( 4, VM_M_setserverlistmasknumber );
 
-       masknr = PRVM_G_FLOAT( OFS_PARM0 );
+       masknr = (int)PRVM_G_FLOAT( OFS_PARM0 );
        if( masknr >= 0 && masknr <= SERVERLIST_ANDMASKCOUNT )
                mask = &serverlist_andmasks[masknr];
        else if( masknr >= 512 && masknr - 512 <= SERVERLIST_ORMASKCOUNT )
                mask = &serverlist_ormasks[masknr - 512 ];
-       else {
-               Con_Printf( "VM_M_setserverlistmasknumber: invalid mask number %i\n", masknr );
+       else
+       {
+               VM_Warning( "VM_M_setserverlistmasknumber: invalid mask number %i\n", masknr );
                return;
        }
 
-       number = PRVM_G_FLOAT( OFS_PARM2 );
+       number = (int)PRVM_G_FLOAT( OFS_PARM2 );
        field = (int) PRVM_G_FLOAT( OFS_PARM1 );
 
        switch( field ) {
@@ -480,12 +497,12 @@ void VM_M_setserverlistmasknumber( void )
                        mask->info.protocol = number;
                        break;
                default:
-                       Con_Printf( "VM_M_setserverlistmasknumber: Bad field number %i passed!\n", field );
+                       VM_Warning( "VM_M_setserverlistmasknumber: Bad field number %i passed!\n", field );
                        return;
        }
 
        mask->active = true;
-       mask->tests[field] = (int) PRVM_G_FLOAT( OFS_PARM3 );
+       mask->tests[field] = (serverlist_maskop_t)((int)PRVM_G_FLOAT( OFS_PARM3 ));
 }
 
 
@@ -517,7 +534,7 @@ void VM_M_getserverliststring(void)
 
        PRVM_G_INT(OFS_RETURN) = 0;
 
-       hostnr = PRVM_G_FLOAT(OFS_PARM1);
+       hostnr = (int)PRVM_G_FLOAT(OFS_PARM1);
 
        if(hostnr < 0 || hostnr >= serverlist_viewcount)
        {
@@ -569,7 +586,7 @@ void VM_M_getserverlistnumber(void)
 
        PRVM_G_INT(OFS_RETURN) = 0;
 
-       hostnr = PRVM_G_FLOAT(OFS_PARM1);
+       hostnr = (int)PRVM_G_FLOAT(OFS_PARM1);
 
        if(hostnr < 0 || hostnr >= serverlist_viewcount)
        {
@@ -606,7 +623,7 @@ void VM_M_setserverlistsort( void )
 {
        VM_SAFEPARMCOUNT( 2, VM_M_setserverlistsort );
 
-       serverlist_sortbyfield = (int) PRVM_G_FLOAT( OFS_PARM0 );
+       serverlist_sortbyfield = (serverlist_infofield_t)((int)PRVM_G_FLOAT( OFS_PARM0 ));
        serverlist_sortdescending = (qboolean) PRVM_G_FLOAT( OFS_PARM1 );
 }
 
@@ -620,7 +637,7 @@ refreshserverlist()
 void VM_M_refreshserverlist( void )
 {
        VM_SAFEPARMCOUNT( 0, VM_M_refreshserverlist );
-       ServerList_QueryList();
+       ServerList_QueryList(true, false);
 }
 
 /*
@@ -672,6 +689,98 @@ void VM_M_addwantedserverlistkey( void )
        VM_SAFEPARMCOUNT( 1, VM_M_addwantedserverlistkey );
 }
 
+/*
+===============================================================================
+MESSAGE WRITING
+
+used only for client and menu
+severs uses VM_SV_...
+
+Write*(* data, float type, float to)
+
+===============================================================================
+*/
+
+#define        MSG_BROADCAST   0               // unreliable to all
+#define        MSG_ONE                 1               // reliable to one (msg_entity)
+#define        MSG_ALL                 2               // reliable to all
+#define        MSG_INIT                3               // write to the init string
+
+sizebuf_t *VM_WriteDest (void)
+{
+       int             dest;
+       int             destclient;
+
+       if(!sv.active)
+               PRVM_ERROR("VM_WriteDest: game is not server (%s)", PRVM_NAME);
+
+       dest = (int)PRVM_G_FLOAT(OFS_PARM1);
+       switch (dest)
+       {
+       case MSG_BROADCAST:
+               return &sv.datagram;
+
+       case MSG_ONE:
+               destclient = (int) PRVM_G_FLOAT(OFS_PARM2);
+               if (destclient < 0 || destclient >= svs.maxclients || !svs.clients[destclient].active || !svs.clients[destclient].netconnection)
+                       PRVM_ERROR("VM_clientcommand: %s: invalid client !", PRVM_NAME);
+
+               return &svs.clients[destclient].netconnection->message;
+
+       case MSG_ALL:
+               return &sv.reliable_datagram;
+
+       case MSG_INIT:
+               return &sv.signon;
+
+       default:
+               PRVM_ERROR ("WriteDest: bad destination");
+               break;
+       }
+
+       return NULL;
+}
+
+void VM_M_WriteByte (void)
+{
+       MSG_WriteByte (VM_WriteDest(), (int)PRVM_G_FLOAT(OFS_PARM0));
+}
+
+void VM_M_WriteChar (void)
+{
+       MSG_WriteChar (VM_WriteDest(), (int)PRVM_G_FLOAT(OFS_PARM0));
+}
+
+void VM_M_WriteShort (void)
+{
+       MSG_WriteShort (VM_WriteDest(), (int)PRVM_G_FLOAT(OFS_PARM0));
+}
+
+void VM_M_WriteLong (void)
+{
+       MSG_WriteLong (VM_WriteDest(), (int)PRVM_G_FLOAT(OFS_PARM0));
+}
+
+void VM_M_WriteAngle (void)
+{
+       MSG_WriteAngle (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0), sv.protocol);
+}
+
+void VM_M_WriteCoord (void)
+{
+       MSG_WriteCoord (VM_WriteDest(), PRVM_G_FLOAT(OFS_PARM0), sv.protocol);
+}
+
+void VM_M_WriteString (void)
+{
+       MSG_WriteString (VM_WriteDest(), PRVM_G_STRING(OFS_PARM0));
+}
+
+void VM_M_WriteEntity (void)
+{
+       MSG_WriteShort (VM_WriteDest(), PRVM_G_EDICTNUM(OFS_PARM0));
+}
+
 prvm_builtin_t vm_m_builtins[] = {
        0, // to be consistent with the old vm
        // common builtings (mostly)
@@ -702,8 +811,8 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_findfloat,
        VM_findchain,
        VM_findchainfloat,
-       VM_precache_file,
-       VM_precache_sound,
+       VM_M_precache_file,
+       VM_M_precache_sound,
        VM_coredump,    // 30
        VM_traceon,
        VM_traceoff,
@@ -754,27 +863,30 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_search_getfilename, // 77
        VM_chr,
        VM_itof,
-       VM_ftoi,                // 80
+       VM_ftoe,                // 80
        VM_itof,                // isString
        VM_altstr_count,
        VM_altstr_prepare,
        VM_altstr_get,
        VM_altstr_set,
-       VM_altstr_ins,  // 86
-       0,0,0,0,        // 90
+       VM_altstr_ins,
+       VM_findflags,
+       VM_findchainflags,
+       VM_cvar_defstring, // 89
+       0, // 90
        e10,                    // 100
        e100,                   // 200
        e100,                   // 300
        e100,                   // 400
        // msg functions
-       VM_WriteByte,
-       VM_WriteChar,
-       VM_WriteShort,
-       VM_WriteLong,
-       VM_WriteAngle,
-       VM_WriteCoord,
-       VM_WriteString,
-       VM_WriteEntity, // 408
+       VM_M_WriteByte,
+       VM_M_WriteChar,
+       VM_M_WriteShort,
+       VM_M_WriteLong,
+       VM_M_WriteAngle,
+       VM_M_WriteCoord,
+       VM_M_WriteString,
+       VM_M_WriteEntity,       // 408
        0,
        0,                              // 410
        e10,                    // 420
@@ -797,8 +909,14 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_cin_setstate,
        VM_cin_getstate,
        VM_cin_restart, // 465
-       0,0,0,0,0,      // 470
-       e10,                    // 480
+       VM_drawline,    // 466
+       0,0,0,0,        // 470
+       VM_asin,                                        // #471 float(float s) VM_asin (DP_QC_ASINACOSATANATAN2TAN)
+       VM_acos,                                        // #472 float(float c) VM_acos (DP_QC_ASINACOSATANATAN2TAN)
+       VM_atan,                                        // #473 float(float t) VM_atan (DP_QC_ASINACOSATANATAN2TAN)
+       VM_atan2,                                       // #474 float(float c, float s) VM_atan2 (DP_QC_ASINACOSATANATAN2TAN)
+       VM_tan,                                         // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN)
+       0,0,0,0,0,                      // 480
        e10,                    // 490
        e10,                    // 500
        e100,                   // 600
@@ -811,12 +929,12 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_M_writetofile,
        VM_M_isfunction,
        VM_M_getresolution,
-       VM_M_keynumtostring,
+       VM_keynumtostring,
        VM_M_findkeysforcommand,// 610
        VM_M_getserverliststat,
        VM_M_getserverliststring,
        VM_parseentitydata,
-       VM_M_stringtokeynum,
+       VM_stringtokeynum,
        VM_M_resetserverlistmasks,
        VM_M_setserverlistmaskstring,
        VM_M_setserverlistmasknumber,