//////////////////////////////////////////////////// /////////////// The Chaos Collection /////////////// //////////////////////////////////////////////////// // Note: If a function depends on another function in the library, it will be noted. // Note: Some of these functions are included in the Sauerbraten SVN. //////////////////////////////////////////////////// // Basic operators and compound assignments, analogous to those of Java/C++: ++ = [ $arg1 = (+ (getalias $arg1) 1) ] -- = [ $arg1 = (- (getalias $arg1) 1) ] += = [ $arg1 = (+ (getalias $arg1) $arg2) ] -= = [ $arg1 = (- (getalias $arg1) $arg2) ] *= = [ $arg1 = (* (getalias $arg1) $arg2) ] /= = [ $arg1 = (div (getalias $arg1) $arg2) ] +=f = [ $arg1 = (+f (getalias $arg1) $arg2) ] -=f = [ $arg1 = (-f (getalias $arg1) $arg2) ] *=f = [ $arg1 = (*f (getalias $arg1) $arg2) ] /=f = [ $arg1 = (divf (getalias $arg1) $arg2) ] &= = [ $arg1 = (& (getalias $arg1) $arg2) ] |= = [ $arg1 = (| (getalias $arg1) $arg2) ] >>= = [ $arg1 = (>> (getalias $arg1) $arg2) ] <<= = [ $arg1 = (<< (getalias $arg1) $arg2) ] ///////////////////////////////////////////////////// //////////////////////////////////////////////////// // Summation/Multiplication functions that work with an arbitrary number of arguments: sum = [ _lib_res = 0 loop i $numargs [ _lib_res = (+ $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result $_lib_res ] sumf = [ _lib_res = 0 loop i $numargs [ _lib_res = (+f $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result $_lib_res ] prod = [ _lib_res = 1 loop i $numargs [ _lib_res = (* $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result $_lib_res ] prodf = [ _lib_res = 1 loop i $numargs [ _lib_res = (*f $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result $_lib_res ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // Statistical Functions: avg = [ _lib_res = 0 loop i $numargs [ _lib_res = (+ $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result (div $_lib_res $numargs) ] avgf = [ _lib_res = 0 loop i $numargs [ _lib_res = (+f $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result (divf $_lib_res $numargs) ] range = [ _lib_res = "" loop i $numargs [ _lib_res = (concat $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result (- (do (concat "max" $_lib_res)) (do (concat "min" $_lib_res))) ] rangef = [ _lib_res = "" loop i $numargs [ _lib_res = (concat $_lib_res (getalias (concatword "arg" (+ $i 1)))) ] result (-f (do (concat "maxf" $_lib_res)) (do (concat "minf" $_lib_res))) ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // Larger library of trigonometric functions: cot = [ divf 1 (tan $arg1) ] sec = [ divf 1 (cos $arg1) ] csc = [ divf 1 (sin $arg1) ] acot = [ atan (divf 1 $arg1) ] asec = [ acos (divf 1 $arg1) ] acsc = [ asin (divf 1 $arg1) ] // Hyperbolic functions: sinh = [ divf (-f (exp $arg1) (exp (*f $arg1 -1))) 2 ] cosh = [ divf (+f (exp $arg1) (exp (*f $arg1 -1))) 2 ] tanh = [ divf (-f (exp $arg1) (exp (*f $arg1 -1))) (+f (exp $arg1) (exp (*f $arg1 -1))) ] coth = [ divf (+f (exp $arg1) (exp (*f $arg1 -1))) (-f (exp $arg1) (exp (*f $arg1 -1))) ] sech = [ divf 2 (+f (exp $arg1) (exp (*f $arg1 -1))) ] csch = [ divf 2 (-f (exp $arg1) (exp (*f $arg1 -1))) ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // Absolute value functions: abs = [ * (? (< $arg1) -1 1) $arg1 ] absf = [ *f (? (< $arg1) -1 1) $arg1 ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // Other math functions: // factorial // finds the factorial of its argument. factorial = [ _lib_res = 1 loop i $arg1 [ _lib_res = (* $_lib_res (+ $i 1)) ] result $_lib_res ] // lcm // finds the least common multiple of its two arguments lcm = [ _lib_res = 0 loopwhile i (+ (* $arg1 $arg2) 1) [ (! $_lib_res) ] [ if (= (+ (mod (+ $i 1) $arg1) (mod (+ $i 1) $arg2))) [ _lib_res = (+ $i 1) ] ] result $_lib_res ] // gcf // finds the greatest common factor of its two arguments. gcf = [ _lib_res = 1 loop i $arg1 [ if (= (+ (mod $arg1 (+ $i 1)) (mod $arg2 (+ $i 1)))) [ _lib_res = (+ $i 1) ] ] result $_lib_res ] // listfactors // lists all the factors of its argument. listfactors = [ _lib_res = "" loop i $arg1 [ if (! (mod $arg1 (+ $i 1))) [ _lib_res = (concat $_lib_res (+ $i 1)) ] ] result (substr $_lib_res 1) ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // Commands concerning lists (and, therefore, also Strings): // transpose // applies the command given as the second argument to all elements of the given list and returns the result. transpose = [ _lib_res = "" looplist i $arg1 [ _lib_res = (concat $_lib_res ($arg2 $i)) ] result (substr $_lib_res 1) ] // sublist // returns a sublist of a given list starting at the given index of given length, similar to 'substr' but works on lists. sublist = [ _lib_res = "" loop i $arg3 [ _lib_res = (concat $_lib_res (at $arg1 (+ $arg2 $i))) ] result (substr $_lib_res 1) ] // instances // counts how many times an element given as the second argument is repeated in a given list. instances = [ _lib_res = 0 looplist i $arg1 [ _lib_res = (+ $_lib_res (=s $i $arg2)) ] result $_lib_res ] // listtoargs // executes the given command using the elements of the given list as arguments. listtoargs = [ do (concat $arg1 $arg2) ] // looplistwhile I [] [] // analogous to 'loopwhile' but works on lists. looplistwhile = [ _lib_res = 1 looplist $arg1 $arg2 [ if (! (arg3)) [ _lib_res = 0 ]; if $_lib_res [ arg4 ] ] ] // listreplace // swaps out the element at a given index of the given list for a new element. listreplace = [ _lib_res = "" looplist i $arg1 [ _lib_res = (concat $_lib_res (? (= (indexof $arg1 $i) $arg2) $arg3 $i)) ] result (substr $_lib_res 1) ] // partition // partitions the given list into the given number of fragments. ** Note: This function depends on 'sublist'. partition = [ _lib_res = "" loop i (+ (div (listlen $arg1) $arg2) (> (mod (listlen $arg1) $arg2) 0)) [ _lib_res = (concat $_lib_res (format ["%1"] (sublist $arg1 (* $i $arg2) $arg2))) ] result (substr $_lib_res 1) ] // reverse // reverses the order of the elements in the given list. reverse = [ _lib_res = "" loop i (listlen $arg1) [ _lib_res = (concat $_lib_res (at $arg1 (- (listlen $arg1) (+ $i 1)))) ] result (substr $_lib_res 1) ] // listdelall etc... // deletes all the given items from the given list and returns the result. listdelall = [ _lib_res = $arg1 loop i (- $numargs 1) [ _lib_res = (listdel $_lib_res (getalias (concatword "arg" (+ $i 2)))) ] result $_lib_res ] // orderlist // arranges the given list based on the ASCII order, lowest items first. ** Note: depends on 'mins'. orderlist = [ _lib_res = "" loop j (listlen $arg1) [ _lib_res = (concat $_lib_res (do (concat mins (do (concat listdel (format ["%1"] $arg1) (format ["%1"] $_lib_res)))))) ] result $_lib_res ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // GUI-related: // guiclock // displays a clock of the form "hh:mm:ss" representing total play time as GUI text with the given icon. guiclock = [ guitext ( concatword "^f" $arg1 ( ? (< (div (getmillis) 360000) 10) 0 ) ( div (getmillis) 360000 ) : ( ? (< (mod (div (getmillis) 60000) 60) 10) 0 ) ( mod (div (getmillis) 60000) 60 ) : ( ? (< (mod (div (getmillis) 1000) 60) 10) 0 ) ( mod (div (getmillis) 1000) 60 ) ) $arg2 ] // guicrazytext // displays the given text with the given icon in a crazy fashion guicrazytext = [ guitext (concatword "^f" (rnd 8) $arg1) $arg2 ] // guifuntext // displays the given text with the given icon, text will cycle through colors guifuntext = [ guitext (concatword "^f" (div (+ (mod (getmillis) 7000) 1000) 1000) $arg1) $arg2 ] //////////////////////////////////////////////////// //////////////////////////////////////////////////// // Other: // loopif (cond1) [body1] (cond2) [body2] etc... // if (cond-n) is true, executes [body-n], does not stop when it reaches a true (cond). loopif = [ loop i (div $numargs 2) [ if ((concatword "arg" (+ (* $i 2) 1))) [ ((concatword "arg" (+ (* $i 2) 2))) ] ] ] // getdef // echoes the definition of the command that is given as its argument. getdef = [ if $numargs [ echo (format "%1 = [%2]" $arg1 $@arg1) ] [ echo (format "getdef = [%1]" $getdef) ] ] // swapvalues // swaps the values of the two variables that are given as arguments. swapvalues = [ if (> $numargs 1) [ [@@arg1] = [@@(getalias $arg2)] [@@arg2] = [@@(getalias $arg1)] ] ] // execdir // executes all the files at the given location. execdir = [ loopfiles f $arg1 cfg [ exec (format "%1/%2.cfg" $arg1 $f) ] ] // mins etc... // returns the string that is lowest in the ASCII order. mins = [ _lib_res = $arg1 loop i $numargs [ if ( etc... // returns the string that is highest in the ASCII order. maxs = [ _lib_res = $arg1 loop i $numargs [ if (>s (getalias (concatword "arg" (+ $i 1))) $_lib_res) [ _lib_res = (getalias (concatword "arg" (+ $i 1))) ] ] result $_lib_res ] // rnds // returns a random string of a species length composed of random characters. rnds = [ _lib_res = "" loop i $arg1 [ _lib_res = (concatword $_lib_res (substr "abcdefghijklmnopqrstuvwxyz" (rnd 26) 1)) ] result $_lib_res ] // for [initialization] [cond] [incrementation] [body] // standard 'for' function. for = [ arg1 ; if (arg2) [ arg4 ; arg3 ; for "" [ @@arg2 ] [ @@arg3 ] [ @@arg4 ] ] ] ////////////////////////////////////////////////////