]> git.xonotic.org Git - xonotic/xonotic.wiki.git/commitdiff
Added bound function and Ternary operator
authorLegendaryGuard <rootuser999@gmail.com>
Mon, 29 Mar 2021 22:37:07 +0000 (22:37 +0000)
committerLegendaryGuard <rootuser999@gmail.com>
Mon, 29 Mar 2021 22:37:07 +0000 (22:37 +0000)
Introduction-to-QuakeC.md

index 27cc21bfc1a166d3a342883d34c98cc078769a00..414679b8da9c335580082ccd01346684510f285d 100644 (file)
@@ -671,6 +671,45 @@ vectoangles does not match makevectors
 The pitch angle is inverted between these two functions. You have to negate the pitch (i.e. the *x* component of the vector representing the euler angles) to make it fit the other function.
 As a rule of thumb, *vectoangles* returns angles as stored in the *angles* field (used to rotate entities for display), while *makevectors* expects angles as stored in the *v\_angle* field (used to transmit the direction the player is aiming). There is about just as much good reason in this as there is for 1:1 patch cables. Just deal with it.
 
+bound
+-----
+
+A bound is a variable that was previously free, but has been bound to a specific value or set of values. If x > upperlimit the upperlimit is returned, if x < lowerlimit then lowerlimit is returned, if lowerlimit < x < upperlimit then x is returned. That function returns an x value calling this way: 
+```c
+bound(lower_limit, x, upper_limit)
+```
+
+Ternary operator
+----------------
+
+QuakeC allows ternary operators like in C:
+```c
+int a = 2;
+int b = 3;
+int c = 6;
+int d = 8;
+int max = (a > b) ? c : d;
+```
+More [**info**](https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c-c/).
+
+There is a complex example using `bound` function with this operator:
+```c
+bound(1, ((hunter_count >= 1) ? hunter_count : floor(total * hunter_count)), total - 1);
+bool wholenumber = (hunter_count >= 1) //is hunter count a specified whole number or percentage
+
+if (!wholenumber) //if hunters are defined with percentage count
+{
+ int z = total * hunter_count //wanted percentage amount from total is z
+ int y = floor(z) //round z downwards to nearest whole number
+}
+
+int x = (wholenumber ? hunter_count : y) //if whole number was given use it, 
+//if not use y which is calculated above
+bound(1, x, total - 1) //use the value x if it's above 1 but below (total - 1)
+//Otherwise use the bounding value of that direction to quarantee that 
+//there is always at least 1 hunter and always at least 1 survivor
+```
+
 Entry points
 ============
 
@@ -755,4 +794,4 @@ The server-side code calls the following entry points of the QuakeC code:
 <br />
 
 -   **void SV\_ParseClientCommand(string command)**: handles commands sent by the client to the server using “cmd ...”. Unhandled commands can be passed to the built-in function *clientcommand* to execute the normal engine behaviour.
-<br />
+<br />
\ No newline at end of file