]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
sqrt and normalize builtins
authorWolfgang Bumiller <blub@speed.at>
Tue, 8 Jan 2013 18:21:24 +0000 (19:21 +0100)
committerWolfgang Bumiller <blub@speed.at>
Tue, 8 Jan 2013 19:46:30 +0000 (20:46 +0100)
doc/qcvm.1
exec.c

index 332cce969802eabeed281a9336d3202b4b9a8caf..de8633ffedff0e58c1088c1cc4a4c61f0a96cb8b 100644 (file)
@@ -123,6 +123,16 @@ Concatenate two strings, returning a tempstring.
 .in +8
 Compare two strings. Returns the same as the corresponding C functions.
 .in
+
+.RI "12) " vector " normalize (" vector ") = " "#12" ;
+.in +8
+Normalize a vector so its length is 1.
+.in
+
+.RI "13) " float " sqrt (" float ") = " "#13" ;
+.in +8
+Get a value's square root.
+.in
 .SH BUGS
 Please report bugs on <http://github.com/graphitemaster/gmqcc/issues>,
 or see <http://graphitemaster.github.com/gmqcc> on how to contact us.
diff --git a/exec.c b/exec.c
index 2383ad70e5bf23827dae59e657b7877252745d3e..5bff85e06a8605e692c58778820096b19cffb8ad 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -733,6 +733,16 @@ static int qc_kill(qc_program *prog)
     return 0;
 }
 
+static int qc_sqrt(qc_program *prog)
+{
+    qcany *num, out;
+    CheckArgs(1);
+    num = GetArg(0);
+    out._float = sqrt(num->_float);
+    Return(out);
+    return 0;
+}
+
 static int qc_vlen(qc_program *prog)
 {
     qcany *vec, len;
@@ -745,6 +755,27 @@ static int qc_vlen(qc_program *prog)
     return 0;
 }
 
+static int qc_normalize(qc_program *prog)
+{
+    double len;
+    qcany *vec;
+    qcany out;
+    CheckArgs(1);
+    vec = GetArg(0);
+    len = sqrt(vec->vector[0] * vec->vector[0] +
+               vec->vector[1] * vec->vector[1] +
+               vec->vector[2] * vec->vector[2]);
+    if (len)
+        len = 1.0 / len;
+    else
+        len = 0;
+    out.vector[0] = len * vec->vector[0];
+    out.vector[1] = len * vec->vector[1];
+    out.vector[2] = len * vec->vector[2];
+    Return(out);
+    return 0;
+}
+
 static int qc_strcat(qc_program *prog)
 {
     char  *buffer;
@@ -795,17 +826,19 @@ static int qc_strcmp(qc_program *prog)
 
 static prog_builtin qc_builtins[] = {
     NULL,
-    &qc_print,  /*   1   */
-    &qc_ftos,   /*   2   */
-    &qc_spawn,  /*   3   */
-    &qc_kill,   /*   4   */
-    &qc_vtos,   /*   5   */
-    &qc_error,  /*   6   */
-    &qc_vlen,   /*   7   */
-    &qc_etos,   /*   8   */
-    &qc_stof,   /*   9   */
-    &qc_strcat, /*   10  */
-    &qc_strcmp  /*   11  */
+    &qc_print,       /*   1   */
+    &qc_ftos,        /*   2   */
+    &qc_spawn,       /*   3   */
+    &qc_kill,        /*   4   */
+    &qc_vtos,        /*   5   */
+    &qc_error,       /*   6   */
+    &qc_vlen,        /*   7   */
+    &qc_etos,        /*   8   */
+    &qc_stof,        /*   9   */
+    &qc_strcat,      /*   10  */
+    &qc_strcmp,      /*   11  */
+    &qc_normalize,   /*   12  */
+    &qc_sqrt         /*   13  */
 };
 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);