made darkplaces compile successfully with g++ to test for errors C doesn't care about...
[xonotic/darkplaces.git] / prvm_cmds.c
index 7d8354a..e3f6392 100644 (file)
@@ -76,7 +76,7 @@ static qboolean checkextension(const char *name)
 {
        int len;
        char *e, *start;
-       len = strlen(name);
+       len = (int)strlen(name);
 
        for (e = prog->extensionstring;*e;e++)
        {
@@ -87,11 +87,8 @@ static qboolean checkextension(const char *name)
                start = e;
                while (*e && *e != ' ')
                        e++;
-               if (e - start == len)
-                       if (!strncasecmp(start, name, len))
-                       {
-                               return true;
-                       }
+               if ((e - start) == len && !strncasecmp(start, name, len))
+                       return true;
        }
        return false;
 }
@@ -255,24 +252,20 @@ void VM_normalize (void)
 {
        float   *value1;
        vec3_t  newvalue;
-       float   new;
+       double  f;
 
        VM_SAFEPARMCOUNT(1,VM_normalize);
 
        value1 = PRVM_G_VECTOR(OFS_PARM0);
 
-       new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
-       new = sqrt(new);
-
-       if (new == 0)
-               newvalue[0] = newvalue[1] = newvalue[2] = 0;
-       else
+       f = VectorLength2(value1);
+       if (f)
        {
-               new = 1/new;
-               newvalue[0] = value1[0] * new;
-               newvalue[1] = value1[1] * new;
-               newvalue[2] = value1[2] * new;
+               f = 1.0 / sqrt(f);
+               VectorScale(value1, f, newvalue);
        }
+       else
+               VectorClear(newvalue);
 
        VectorCopy (newvalue, PRVM_G_VECTOR(OFS_RETURN));
 }
@@ -286,17 +279,8 @@ scalar vlen(vector)
 */
 void VM_vlen (void)
 {
-       float   *value1;
-       float   new;
-
        VM_SAFEPARMCOUNT(1,VM_vlen);
-
-       value1 = PRVM_G_VECTOR(OFS_PARM0);
-
-       new = value1[0] * value1[0] + value1[1] * value1[1] + value1[2]*value1[2];
-       new = sqrt(new);
-
-       PRVM_G_FLOAT(OFS_RETURN) = new;
+       PRVM_G_FLOAT(OFS_RETURN) = VectorLength(PRVM_G_VECTOR(OFS_PARM0));
 }
 
 /*
@@ -524,7 +508,7 @@ void VM_cvar_string(void)
        name = PRVM_G_STRING(OFS_PARM0);
 
        if(!name)
-               PRVM_ERROR("VM_str_cvar: %s: null string\n", PRVM_NAME);
+               PRVM_ERROR("VM_cvar_string: %s: null string\n", PRVM_NAME);
 
        VM_CheckEmptyString(name);
 
@@ -537,6 +521,36 @@ void VM_cvar_string(void)
        PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
 }
 
+
+/*
+========================
+VM_cvar_defstring
+
+const string   VM_cvar_defstring (string)
+========================
+*/
+void VM_cvar_defstring (void)
+{
+       char *out;
+       const char *name;
+       const char *cvar_string;
+       VM_SAFEPARMCOUNT(1,VM_cvar_string);
+
+       name = PRVM_G_STRING(OFS_PARM0);
+
+       if(!name)
+               PRVM_ERROR("VM_cvar_defstring: %s: null string\n", PRVM_NAME);
+
+       VM_CheckEmptyString(name);
+
+       out = VM_GetTempString();
+
+       cvar_string = Cvar_VariableDefString(name);
+
+       strcpy(out, cvar_string);
+
+       PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(out);
+}
 /*
 =================
 VM_cvar_set
@@ -564,7 +578,11 @@ void VM_dprint (void)
        if (developer.integer)
        {
                VM_VarString(0, string, sizeof(string));
+#if 1
+               Con_Printf("%s", string);
+#else
                Con_Printf("%s: %s", PRVM_NAME, string);
+#endif
        }
 }
 
@@ -832,7 +850,6 @@ VM_findchain
 entity findchain(.string field, string match)
 =========
 */
-int PRVM_ED_FindFieldOffset(const char *field);
 // chained search for strings in entity fields
 // entity(.string field, string match) findchain = #402;
 void VM_findchain (void)
@@ -849,7 +866,7 @@ void VM_findchain (void)
        if(!prog->flag & PRVM_FE_CHAIN)
                PRVM_ERROR("VM_findchain: %s doesnt have a chain field !\n", PRVM_NAME);
 
-       chain_of = PRVM_ED_FindFieldOffset ("chain");
+       chain_of = PRVM_ED_FindField("chain")->ofs;
 
        chain = prog->edicts;
 
@@ -903,7 +920,7 @@ void VM_findchainfloat (void)
        if(!prog->flag & PRVM_FE_CHAIN)
                PRVM_ERROR("VM_findchainfloat: %s doesnt have a chain field !\n", PRVM_NAME);
 
-       chain_of = PRVM_ED_FindFieldOffset ("chain");
+       chain_of = PRVM_ED_FindField("chain")->ofs;
 
        chain = (prvm_edict_t *)prog->edicts;
 
@@ -927,6 +944,88 @@ void VM_findchainfloat (void)
 }
 
 /*
+========================
+VM_findflags
+
+entity findflags(entity start, .float field, float match)
+========================
+*/
+// LordHavoc: search for flags in float fields
+void VM_findflags (void)
+{
+       int             e;
+       int             f;
+       int             s;
+       prvm_edict_t    *ed;
+
+       VM_SAFEPARMCOUNT(3, VM_findflags);
+
+
+       e = PRVM_G_EDICTNUM(OFS_PARM0);
+       f = PRVM_G_INT(OFS_PARM1);
+       s = (int)PRVM_G_FLOAT(OFS_PARM2);
+
+       for (e++ ; e < prog->num_edicts ; e++)
+       {
+               prog->xfunction->builtinsprofile++;
+               ed = PRVM_EDICT_NUM(e);
+               if (ed->priv.required->free)
+                       continue;
+               if ((int)PRVM_E_FLOAT(ed,f) & s)
+               {
+                       VM_RETURN_EDICT(ed);
+                       return;
+               }
+       }
+
+       VM_RETURN_EDICT(prog->edicts);
+}
+
+/*
+========================
+VM_findchainflags
+
+entity findchainflags(.float field, float match)
+========================
+*/
+// LordHavoc: chained search for flags in float fields
+void VM_findchainflags (void)
+{
+       int             i;
+       int             f;
+       int             s;
+       int             chain_of;
+       prvm_edict_t    *ent, *chain;
+
+       VM_SAFEPARMCOUNT(2, VM_findchainflags);
+
+       if(!prog->flag & PRVM_FE_CHAIN)
+               PRVM_ERROR("VM_findchainflags: %s doesnt have a chain field !\n", PRVM_NAME);
+
+       chain_of = PRVM_ED_FindField("chain")->ofs;
+
+       chain = (prvm_edict_t *)prog->edicts;
+
+       f = PRVM_G_INT(OFS_PARM0);
+       s = (int)PRVM_G_FLOAT(OFS_PARM1);
+
+       ent = PRVM_NEXT_EDICT(prog->edicts);
+       for (i = 1;i < prog->num_edicts;i++, ent = PRVM_NEXT_EDICT(ent))
+       {
+               prog->xfunction->builtinsprofile++;
+               if (ent->priv.required->free)
+                       continue;
+               if (!((int)PRVM_E_FLOAT(ent,f) & s))
+                       continue;
+
+               PRVM_E_INT(ent,chain_of) = PRVM_EDICT_TO_PROG(chain);
+               chain = ent;
+       }
+
+       VM_RETURN_EDICT(chain);
+}
+
+/*
 =========
 VM_coredump
 
@@ -1448,7 +1547,7 @@ void VM_fopen(void)
                modestring = "wb";
                break;
        default:
-               Con_Printf("VM_fopen: %s no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
+               Con_Printf("VM_fopen: %s: no such mode %i (valid: 0 = read, 1 = append, 2 = write)\n", PRVM_NAME, mode);
                PRVM_G_FLOAT(OFS_RETURN) = -3;
                return;
        }
@@ -1459,9 +1558,17 @@ void VM_fopen(void)
                VM_FILES[filenum] = FS_Open(va("%s", filename), modestring, false, false);
 
        if (VM_FILES[filenum] == NULL)
+       {
+               if (developer.integer)
+                       Con_Printf("VM_fopen: %s: %s mode %s failed\n", PRVM_NAME, filename, modestring);
                PRVM_G_FLOAT(OFS_RETURN) = -1;
+       }
        else
+       {
+               if (developer.integer)
+                       Con_Printf("VM_fopen: %s: %s mode %s opened as #%i\n", PRVM_NAME, filename, modestring, filenum);
                PRVM_G_FLOAT(OFS_RETURN) = filenum;
+       }
 }
 
 /*
@@ -1489,6 +1596,8 @@ void VM_fclose(void)
                Con_Printf("VM_fclose: no such file handle %i (or file has been closed) in %s\n", filenum, PRVM_NAME);
                return;
        }
+       if (developer.integer)
+               Con_Printf("VM_fclose: %s: #%i closed\n", PRVM_NAME, filenum);
        FS_Close(VM_FILES[filenum]);
        VM_FILES[filenum] = NULL;
 }
@@ -1573,7 +1682,7 @@ void VM_fputs(void)
                return;
        }
        VM_VarString(1, string, sizeof(string));
-       if ((stringlength = strlen(string)))
+       if ((stringlength = (int)strlen(string)))
                FS_Write(VM_FILES[filenum], string, stringlength);
        if (developer.integer)
                Con_Printf("fputs: %s: %s\n", PRVM_NAME, string);
@@ -1686,9 +1795,8 @@ void VM_strzone(void)
        VM_SAFEPARMCOUNT(1,VM_strzone);
 
        in = PRVM_G_STRING(OFS_PARM0);
-       out = PRVM_AllocString(strlen(in) + 1);
+       PRVM_G_INT(OFS_RETURN) = PRVM_AllocString(strlen(in) + 1, &out);
        strcpy(out, in);
-       PRVM_G_INT(OFS_RETURN) = PRVM_SetQCString(out);
 }
 
 /*
@@ -1702,7 +1810,7 @@ strunzone(string s)
 void VM_strunzone(void)
 {
        VM_SAFEPARMCOUNT(1,VM_strunzone);
-       PRVM_FreeString((char *)PRVM_G_STRING(OFS_PARM0));
+       PRVM_FreeString(PRVM_G_INT(OFS_PARM0));
 }
 
 /*
@@ -1749,7 +1857,7 @@ int num_tokens = 0;
 char *tokens[256], tokenbuf[4096];
 void VM_tokenize (void)
 {
-       int pos;
+       size_t pos;
        const char *p;
 
        VM_SAFEPARMCOUNT(1,VM_tokenize);
@@ -1983,7 +2091,7 @@ loadfromfile(string file)
 void VM_loadfromfile(void)
 {
        const char *filename;
-       qbyte *data;
+       char *data;
 
        VM_SAFEPARMCOUNT(1,VM_loadfromfile);
 
@@ -2000,7 +2108,7 @@ void VM_loadfromfile(void)
        }
 
        // not conform with VM_fopen
-       data = FS_LoadFile(filename, tempmempool, false);
+       data = (char *)FS_LoadFile(filename, tempmempool, false);
        if (data == NULL)
                PRVM_G_FLOAT(OFS_RETURN) = -1;
 
@@ -2574,7 +2682,7 @@ void VM_cin_setstate( void )
        name = PRVM_G_STRING( OFS_PARM0 );
        VM_CheckEmptyString( name );
 
-       state = PRVM_G_FLOAT( OFS_PARM1 );
+       state = (clvideostate_t)PRVM_G_FLOAT( OFS_PARM1 );
 
        video = CL_GetVideo( name );
        if( video && state > CLVIDEO_UNUSED && state < CLVIDEO_STATECOUNT )
@@ -2651,7 +2759,7 @@ void VM_altstr_count( void )
        for( count = 0, pos = altstr ; *pos ; pos++ ) {
                if( *pos == '\\' ) {
                        if( !*++pos ) {
-                               break; 
+                               break;
                        }
                } else if( *pos == '\'' ) {
                        count++;