]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_cmds.c
Added isfunction and changed callfunction so it works
[xonotic/darkplaces.git] / prvm_cmds.c
index cc3e46bd882a991e58a0bd10cd26f2af6c894aa7..6adb8be7239b5ad5672624a3bbbd801ba17574b1 100644 (file)
@@ -2732,25 +2732,73 @@ VM_M_callfunction
 =========
 */
 mfunction_t *PRVM_ED_FindFunction (const char *name);
-int PRVM_EnterFunction (mfunction_t *f);
 void VM_M_callfunction(void)
 {
        mfunction_t *func;
        char *s;
 
+       if(prog->argc == 0)
+               PRVM_ERROR("VM_M_callfunction: 1 parameter is required !\n");
+
        s = PRVM_G_STRING(OFS_PARM0 + (prog->argc - 1));
 
        if(!s)
-               PRVM_ERROR("VM_M_getfunction: null string !\n");
+               PRVM_ERROR("VM_M_callfunction: null string !\n");
 
        VM_CheckEmptyString(s); 
 
        func = PRVM_ED_FindFunction(s);
 
-       if(func)
-               PRVM_EnterFunction(func);
+       if(!func)
+               PRVM_ERROR("VM_M_callfunciton: function %s not found !\n", s);
+       else if (func->first_statement < 0)
+       {
+               // negative statements are built in functions
+               int builtinnumber = -func->first_statement;
+               prog->xfunction->builtinsprofile++;
+               if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
+                       prog->builtins[builtinnumber]();
+               else
+                       PRVM_ERROR("No such builtin #%i in %s", builtinnumber, PRVM_NAME);
+       }
+       else if(func > 0)
+       {
+               prog->argc--;
+               PRVM_ExecuteProgram(func - prog->functions,"");
+               prog->argc++;
+       }
 }      
 
+/*
+=========
+VM_M_isfunction
+
+float  isfunction(string function_name)
+=========
+*/
+mfunction_t *PRVM_ED_FindFunction (const char *name);
+void VM_M_isfunction(void)
+{
+       mfunction_t *func;
+       char *s;
+       
+       VM_SAFEPARMCOUNT(1, VM_M_isfunction);
+       
+       s = PRVM_G_STRING(OFS_PARM0);
+       
+       if(!s)
+               PRVM_ERROR("VM_M_isfunction: null string !\n");
+       
+       VM_CheckEmptyString(s); 
+       
+       func = PRVM_ED_FindFunction(s);
+
+       if(!func)
+               PRVM_G_FLOAT(OFS_RETURN) = false;
+       else
+               PRVM_G_FLOAT(OFS_RETURN) = true;
+}
+
 /*
 =========
 VM_M_writetofile
@@ -2905,7 +2953,8 @@ prvm_builtin_t vm_m_builtins[] = {
        VM_M_setmousetarget,
        VM_M_getmousetarget,
        VM_M_callfunction,
-       VM_M_writetofile        // 606
+       VM_M_writetofile,
+       VM_M_isfunction // 607
 };
 
 const int vm_m_numbuiltins = sizeof(vm_m_builtins) / sizeof(prvm_builtin_t);