]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_cmds.c
fixed up a number of nudging bugs in Collision_TraceLineTriangleFloat
[xonotic/darkplaces.git] / prvm_cmds.c
index 6b390e7b59198a58c56122f9401a27f731668004..cad16a48cf21ed978763549fd89f01ab8666760b 100644 (file)
@@ -96,6 +96,13 @@ float        search_getsize(float handle)
 string search_getfilename(float handle, float num)
 
 string chr(float ascii)
+
+float  itof(intt ent)
+intt   ftoi(float num)
+
+float  altstr_count(string)
+string altstr_prepare(string)
+string altstr_get(string,float)
                
 perhaps only : Menu : WriteMsg 
 ===============================
@@ -109,8 +116,8 @@ perhaps only : Menu : WriteMsg
                WriteString(string data, float dest, float desto)
                WriteEntity(entity data, float dest, float desto)
                
-Client & Menu : draw functions 
-===============================
+Client & Menu : draw functions & video functions
+===================================================
 
 float  iscachedpic(string pic)
 string precache_pic(string pic) 
@@ -122,7 +129,12 @@ float      drawfill(vector position, vector size, vector rgb, float alpha, float flag
                drawsetcliparea(float x, float y, float width, float height)
                drawresetcliparea()
 vector getimagesize(string pic)
-               
+
+float  cin_open(string file, string name)
+void   cin_close(string name)
+void   cin_setstate(string name, float type)
+float  cin_getstate(string name)               
+void   cin_restart(string name)
 
 ==============================================================================
 menu cmd list:
@@ -142,14 +154,17 @@ string    findkeysforcommand(string command)
 float  gethostcachevalue(float type)
 string gethostcachestring(float type, float hostnr)
 
-
+               parseentitydata(entity ent, string data)
 */
 
 #include "quakedef.h"
 #include "progdefs.h"
+#include "progsvm.h"
 #include "clprogdefs.h"
 #include "mprogdefs.h"
 
+#include "cl_video.h"
+
 //============================================================================
 // nice helper macros
 
@@ -388,8 +403,6 @@ void VM_sprint (void)
        }
        
        client = svs.clients + clientnum;
-       if (!client->netconnection)
-               return;
        VM_VarString(1, string, sizeof(string));
        MSG_WriteChar(&client->message,svc_print);
        MSG_WriteString(&client->message, string);
@@ -557,13 +570,9 @@ float random()
 */
 void VM_random (void)
 {
-       float           num;
-
        VM_SAFEPARMCOUNT(0,VM_random);
 
-       num = (rand ()&0x7fff) / ((float)0x7fff);
-
-       PRVM_G_FLOAT(OFS_RETURN) = num;
+       PRVM_G_FLOAT(OFS_RETURN) = lhrandom(0, 1);
 }
 
 /*
@@ -624,7 +633,7 @@ void VM_localsound(void)
 
        s = PRVM_G_STRING(OFS_PARM0);
 
-       if(!S_LocalSound(s, true))
+       if(!S_LocalSound (s))
        {
                Con_Printf("VM_localsound: Failed to play %s for %s !\n", s, PRVM_NAME);
                PRVM_G_FLOAT(OFS_RETURN) = -4;
@@ -834,6 +843,38 @@ void VM_stof(void)
        PRVM_G_FLOAT(OFS_RETURN) = atof(string);
 }
 
+/*
+========================
+VM_itof
+
+float itof(intt ent)
+========================
+*/
+void VM_itof(void)
+{
+       VM_SAFEPARMCOUNT(1, VM_itof);
+       PRVM_G_FLOAT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
+}
+
+/*
+========================
+VM_itoe
+
+intt ftoi(float num)
+========================
+*/
+void VM_ftoi(void)
+{
+       int ent;
+       VM_SAFEPARMCOUNT(1, VM_ftoi);
+
+       ent = PRVM_G_FLOAT(OFS_PARM0);
+       if(PRVM_PROG_TO_EDICT(ent)->p.e->free)
+               PRVM_ERROR ("VM_ftoe: %s tried to access a freed entity (entity %i)!\n", PRVM_NAME, ent);
+    
+       PRVM_G_INT(OFS_RETURN) = ent;
+}
+
 /*
 =========
 VM_spawn
@@ -1006,7 +1047,7 @@ void VM_findchain (void)
                if (strcmp(t,s))
                        continue;
 
-               PRVM_E_FLOAT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
+               PRVM_E_INT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
                chain = ent;
        }
 
@@ -1049,10 +1090,10 @@ void VM_findchainfloat (void)
                prog->xfunction->builtinsprofile++;
                if (ent->p.e->free)
                        continue;
-               if (E_FLOAT(ent,f) != s)
+               if (PRVM_E_FLOAT(ent,f) != s)
                        continue;
 
-               PRVM_E_FLOAT(ent,chain_of) = PRVM_NUM_FOR_EDICT(chain);
+               PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
                chain = ent;
        }
 
@@ -1102,8 +1143,8 @@ void VM_precache_sound (void)
        s = PRVM_G_STRING(OFS_PARM0);
        PRVM_G_INT(OFS_RETURN) = PRVM_G_INT(OFS_PARM0);
        VM_CheckEmptyString (s);
-       
-       if(!S_PrecacheSound(s,true, true, true))
+
+       if(!S_PrecacheSound (s,true, true))
                Con_Printf("VM_precache_sound: Failed to load %s for %s\n", s, PRVM_NAME);
 }
 
@@ -1507,7 +1548,7 @@ void VM_registercvar (void)
                return;
        }
 
-       Cvar_Get(name, value, 0);
+       Cvar_Get(name, value, flags);
 
        PRVM_G_FLOAT(OFS_RETURN) = 1; // success
 }
@@ -1560,7 +1601,7 @@ void VM_max (void)
                for (i = 1;i < prog->argc;i++)
                        if (PRVM_G_FLOAT((OFS_PARM0+i*3)) > f)
                                f = PRVM_G_FLOAT((OFS_PARM0+i*3));
-               G_FLOAT(OFS_RETURN) = f;
+               PRVM_G_FLOAT(OFS_RETURN) = f;
        }
        else
                PRVM_ERROR("VM_max: %s must supply at least 2 floats\n", PRVM_NAME);
@@ -1877,8 +1918,8 @@ void VM_strcat(void)
 {
        char *s;
 
-       if(prog->argc < 2
-               PRVM_ERROR("VM_strcat wrong parameter count (min. 2 expected ) !\n");
+       if(prog->argc < 1
+               PRVM_ERROR("VM_strcat wrong parameter count (min. 1 expected ) !\n");
        
        s = VM_GetTempString();
        VM_VarString(0, s, VM_STRINGTEMP_LENGTH);
@@ -1962,9 +2003,14 @@ strunzone(string s)
 //void(string s) strunzone = #119; // removes a copy of a string from the string zone (you can not use that string again or it may crash!!!)
 void VM_strunzone(void)
 {
+       char *str;
        VM_SAFEPARMCOUNT(1,VM_strunzone);
 
-       Mem_Free(PRVM_G_STRING(OFS_PARM0));
+       str = PRVM_G_STRING(OFS_PARM0);
+       if( developer.integer && !Mem_IsAllocated( VM_STRINGS_MEMPOOL, str ) )
+               PRVM_ERROR( "VM_strunzone: Zone string already freed in %s!", PRVM_NAME );
+       else
+               Mem_Free( str );
 }
 
 /*
@@ -2219,6 +2265,34 @@ void VM_loadfromdata(void)
        PRVM_ED_LoadFromFile(PRVM_G_STRING(OFS_PARM0));
 }
 
+/*
+========================
+VM_M_parseentitydata
+
+parseentitydata(entity ent, string data)
+========================
+*/
+void VM_M_parseentitydata(void)
+{
+       prvm_edict_t *ent;
+       const char *data;
+
+       VM_SAFEPARMCOUNT(2, VM_parseentitydata);
+    
+    // get edict and test it
+       ent = PRVM_G_EDICT(OFS_PARM0);
+       if (ent->p.e->free)
+               PRVM_ERROR ("VM_parseentitydata: %s: Can only set already spawned entities (entity %i is free)!\n", PRVM_NAME, PRVM_NUM_FOR_EDICT(ent));
+
+       data = PRVM_G_STRING(OFS_PARM1);
+
+    // parse the opening brace
+       if (!COM_ParseToken(&data, false) || com_token[0] != '{' )
+               PRVM_ERROR ("VM_parseentitydata: %s: Couldn't parse entity data:\n%s\n", PRVM_NAME, data );
+
+       PRVM_ED_ParseEdict (data, ent);
+}
+
 /*
 =========
 VM_loadfromfile
@@ -2458,7 +2532,7 @@ void VM_iscachedpic(void)
        VM_SAFEPARMCOUNT(1,VM_iscachedpic);
 
        // drawq hasnt such a function, thus always return true 
-       PRVM_G_FLOAT(OFS_RETURN) = TRUE;
+       PRVM_G_FLOAT(OFS_RETURN) = false;
 }
 
 /*
@@ -2579,7 +2653,7 @@ void VM_drawstring(void)
                return;
        }
        
-       VM_CheckEmptyString(string);
+       //VM_CheckEmptyString(string); Why should it be checked - perhaps the menu wants to the precolored letters, too?
        
        pos = PRVM_G_VECTOR(OFS_PARM0);
        scale = PRVM_G_VECTOR(OFS_PARM2);
@@ -2705,12 +2779,12 @@ void VM_drawsetcliparea(void)
        float x,y,w,h;
        VM_SAFEPARMCOUNT(4,VM_drawsetcliparea);
 
-       x = bound(0,PRVM_G_FLOAT(OFS_PARM0),vid.conwidth);
-       y = bound(0,PRVM_G_FLOAT(OFS_PARM1),vid.conheight);
-       w = bound(0,PRVM_G_FLOAT(OFS_PARM2),(vid.conwidth  - x));
-       h = bound(0,PRVM_G_FLOAT(OFS_PARM3),(vid.conheight - y)); 
+       x = bound(0, PRVM_G_FLOAT(OFS_PARM0), vid.conwidth);
+       y = bound(0, PRVM_G_FLOAT(OFS_PARM1), vid.conheight);
+       w = bound(0, PRVM_G_FLOAT(OFS_PARM2) + PRVM_G_FLOAT(OFS_PARM0) - x, (vid.conwidth  - x));
+       h = bound(0, PRVM_G_FLOAT(OFS_PARM3) + PRVM_G_FLOAT(OFS_PARM1) - y, (vid.conheight - y)); 
 
-       DrawQ_SetClipArea(x,y,w,h);
+       DrawQ_SetClipArea(x, y, w, h);
 }
 
 /*
@@ -2755,6 +2829,231 @@ void VM_getimagesize(void)
        PRVM_G_VECTOR(OFS_RETURN)[2] = 0;
 }
 
+// CL_Video interface functions
+
+/*
+========================
+VM_cin_open
+
+float cin_open(string file, string name)
+========================
+*/
+void VM_cin_open( void )
+{
+       char *file;
+       char *name;
+       
+       VM_SAFEPARMCOUNT( 2, VM_cin_open );
+       
+       file = PRVM_G_STRING( OFS_PARM0 );
+       name = PRVM_G_STRING( OFS_PARM1 );
+
+       VM_CheckEmptyString( file );
+    VM_CheckEmptyString( name );
+
+       if( CL_OpenVideo( file, name, MENUOWNER ) )
+               PRVM_G_FLOAT( OFS_RETURN ) = 1;
+       else
+               PRVM_G_FLOAT( OFS_RETURN ) = 0;
+}
+
+/*
+========================
+VM_cin_close
+
+void cin_close(string name)
+========================
+*/
+void VM_cin_close( void )
+{
+       char *name;
+
+       VM_SAFEPARMCOUNT( 1, VM_cin_close );
+
+       name = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( name );
+
+       CL_CloseVideo( CL_GetVideo( name ) ); 
+}
+
+/*
+========================
+VM_cin_setstate
+void cin_setstate(string name, float type)
+========================
+*/
+void VM_cin_setstate( void )
+{
+       char *name;
+       clvideostate_t  state;
+       clvideo_t               *video;
+
+       VM_SAFEPARMCOUNT( 2, VM_cin_netstate );
+
+       name = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( name );
+
+       state = PRVM_G_FLOAT( OFS_PARM1 );
+
+       video = CL_GetVideo( name );
+       if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
+               CL_SetVideoState( video, state );               
+}
+
+/*
+========================
+VM_cin_getstate
+
+float cin_getstate(string name)
+========================
+*/
+void VM_cin_getstate( void )
+{
+       char *name;
+       clvideo_t               *video;
+
+       VM_SAFEPARMCOUNT( 1, VM_cin_getstate );
+
+       name = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( name );
+
+       video = CL_GetVideo( name );
+       if( video )
+               PRVM_G_FLOAT( OFS_RETURN ) = (int)video->state;
+       else
+               PRVM_G_FLOAT( OFS_RETURN ) = 0;
+}
+
+/*
+========================
+VM_cin_restart
+
+void cin_restart(string name)
+========================
+*/
+void VM_cin_restart( void )
+{
+       char *name;
+       clvideo_t               *video;
+
+       VM_SAFEPARMCOUNT( 1, VM_cin_restart );
+
+       name = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( name );
+
+       video = CL_GetVideo( name );
+       if( video )
+               CL_RestartVideo( video );
+}
+
+////////////////////////////////////////
+// AltString functions
+////////////////////////////////////////
+
+/*
+========================
+VM_altstr_count
+
+float altstr_count(string)
+========================
+*/
+void VM_altstr_count( void )
+{
+       char *altstr, *pos;
+       int     count;
+
+       VM_SAFEPARMCOUNT( 1, VM_altstr_count );
+
+       altstr = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( altstr );
+
+       for( count = 0, pos = altstr ; *pos ; pos++ )
+               if( *pos == '\\' && !*++pos )
+                               break;
+               else if( *pos == '\'' )
+                       count++;
+
+       PRVM_G_FLOAT( OFS_RETURN ) = (float) (count / 2);
+}
+
+/*
+========================
+VM_altstr_prepare
+
+string altstr_prepare(string)
+========================
+*/
+void VM_altstr_prepare( void )
+{
+       char *outstr, *out;
+       char *instr, *in;
+       int size;
+
+       VM_SAFEPARMCOUNT( 1, VM_altstr_prepare );
+
+       instr = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( instr );
+       outstr = VM_GetTempString();
+
+       for( out = outstr, in = instr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *in ; size--, in++, out++ )
+               if( *in == '\'' ) {
+                       *out++ = '\\';
+                       *out = '\'';
+                       size--;
+               } else
+                       *out = *in;
+       *out = 0;
+
+       PRVM_G_INT( OFS_RETURN ) = PRVM_SetString( outstr );
+}
+
+/*
+========================
+VM_altstr_get
+
+string altstr_get(string, float)
+========================
+*/
+void VM_altstr_get( void )
+{
+       char *altstr, *pos, *outstr, *out;
+       int count, size;
+
+       VM_SAFEPARMCOUNT( 2, VM_altstr_get );
+
+       altstr = PRVM_G_STRING( OFS_PARM0 );
+       VM_CheckEmptyString( altstr );
+
+       count = PRVM_G_FLOAT( OFS_PARM1 );
+       count = count * 2 + 1;
+
+       for( pos = altstr ; *pos && count ; pos++ )
+               if( *pos == '\\' && !*++pos )
+                       break;                          
+               else if( *pos == '\'' )
+                       count--;
+
+       if( !*pos ) {
+               PRVM_G_INT( OFS_RETURN ) = PRVM_SetString( "" );
+               return;
+       }
+
+    outstr = VM_GetTempString();
+       for( out = outstr, size = VM_STRINGTEMP_LENGTH - 1 ; size && *pos ; size--, pos++, out++ )
+               if( *pos == '\\' ) {
+                       if( !*++pos )
+                               break;
+                       *out = *pos;
+                       size--;
+               } else if( *pos == '\'' )
+                       break;
+               else
+                       *out = *pos;
+
+       *out = 0;
+       PRVM_G_INT( OFS_RETURN ) = PRVM_SetString( outstr );
+}
+
 void VM_Cmd_Init(void)
 {
        // only init the stuff for the current prog
@@ -2766,7 +3065,16 @@ void VM_Cmd_Init(void)
 void VM_Cmd_Reset(void)
 {
        //Mem_EmptyPool(VM_STRINGS_MEMPOOL);
+       if( developer.integer >= 2 && VM_STRINGS_MEMPOOL ) {
+               memheader_t *header;
+               int     i;
+
+               for( i = 0, header = VM_STRINGS_MEMPOOL->chain ; header ; header = header->next, i++ )
+                       Con_DPrintf( "Leaked string %i (size: %i): %.*s\n", i, header->size, header->size, ((char*)header) + sizeof( memheader_t ) );
+       }
+
        Mem_FreePool(&VM_STRINGS_MEMPOOL);
+       CL_PurgeOwner( MENUOWNER );
        VM_Search_Reset();
        VM_Files_CloseAll();
 }
@@ -2815,7 +3123,7 @@ void VM_CL_Cmd_Reset(void)
 // Menu
 
 char *vm_m_extensions =
-"";
+"DP_CINEMATIC_DPV";
 
 /*
 =========
@@ -2924,6 +3232,7 @@ void VM_M_getkeydest(void)
 VM_M_callfunction
 
        callfunction(...,string function_name)
+Extension: pass 
 =========
 */
 mfunction_t *PRVM_ED_FindFunction (const char *name);
@@ -3277,9 +3586,14 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_search_end,
        VM_search_getsize,
        VM_search_getfilename, // 77
-       VM_chr, //78
-       0,0,// 80
-       e10,                    // 90
+       VM_chr, 
+       VM_itof,
+       VM_ftoi,                // 80
+       VM_itof,                // isString
+       VM_altstr_count,
+       VM_altstr_prepare,
+       VM_altstr_get,  // 84
+       0,0,0,0,0,0,    // 90
        e10,                    // 100
        e100,                   // 200
        e100,                   // 300
@@ -3310,7 +3624,12 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_drawsetcliparea,
        VM_drawresetcliparea,
        VM_getimagesize,// 460
-       e10,                    // 470
+       VM_cin_open,
+       VM_cin_close,
+       VM_cin_setstate,        
+       VM_cin_getstate,
+       VM_cin_restart, // 465
+       0,0,0,0,0,      // 470
        e10,                    // 480
        e10,                    // 490
        e10,                    // 500
@@ -3327,7 +3646,8 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_M_keynumtostring,
        VM_M_findkeysforcommand,// 610
        VM_M_gethostcachevalue,
-       VM_M_gethostcachestring // 612 
+       VM_M_gethostcachestring,
+       VM_M_parseentitydata    // 613
 };
 
 const int vm_m_numbuiltins = sizeof(vm_m_builtins) / sizeof(prvm_builtin_t);