Wired Expression Chip
From GMod Wiki
This page has been nominated for deletion. Deletion is due within approximately 30 days from nomination, unless the deletion has been disputed on the talk page. See more pages nominated for deletion here. Reason for deletion: Not what Garry wants the wiki to be used for Last Edit was made on 11/16/2011 |
Wiremod Tool | Back |
Function: | A programmable gate. |
Primary fire: | Spawns an Expression Gate. |
Secondary fire: | Loads the Expression code to the menu. |
Reload: | Resets all variables. |
Notes: | The Expression Gate was made by Syranide |
Brief
This gate no longer is available from the Wire tool menu, to access it: revert back to an older Wiremod revision, copy the folder containing Expression 1 data, and paste it into the newer version
The expression gate itself works like any other gate, except that you need to enter a valid program to be able to spawn it. In theory the Expression Gate can only do what already can be done with other gates, but in a much faster and easier way.
NOTICE: Expression 1 is deprecated and being replaced by Expression 2. No more development is being done to expression 1.
Instructions
Read the whole guide through several times. If you get confused the first time you might understand everything better the second.
First you need a program. There are four ways of getting a program.
- From the list of programs in the expression gate interface.
- Using the "New Expression..."-button in the interface which allows you to enter expressions.
- Fetch the program from a gate using the right mouse button, it will then show up in the expression gate interface.
- Write your own. Open up notepad and write your program, then save it to C:Program Files\Steam\SteamApps\<steamaccountname>\garrysmod\garrysmod\addons\
wire\data\ExpressionGate
Arithmetic Operators
Arithmetic operators are the heart and soul of each expression and should not be difficult to comprehend. They are binary operators, which mean that they take two values as input and output one. This simply means that you can add and multiply values.
Here is a list of the Arithmetic operators:
N + N : addition
N - N : subtraction
N * N : multiplication
N / N : division
N % N : modulo, also referred to as the remainder of division "4 / 5 = 2.5 => 0.5", note "-1 % 3 = 2"
N ^ N : exponentiation, "X to the power of 2" which is also referred to as X squared
Normal math rules apply here. "2/2+2" is the same as "(2/2)+2" but "2/(2+2)" is not.
Variables
A variable can store a value, just like the memory in a calculator. All variables start with an uppercase letter.
A = B
Out = In
Syranide = God
In the above examples we have just taken one variable and made another variable the same. Now let's set variables to values (numbers):
Life = 42
Now we are getting somewhere. In this example Life is set too 42 but you can also use math:
Year = 1000 * 2 + 7
Foo = Year + 1000 * Life
So in the two above examples: Year is first set to 2007. (1000 * 2 + 7 = 2007)
Next Foo is is set 44007. (2007 + 1000 * 42)
Now where you know how the variables work, lets go further.
A variable that changes it's value but is not an Input nor Output is Session Variable. These are kept between each execution meaning that if you assign "C" a value, next time the expression is executed "C" will still have that value. This allows you to do some even more complex things, but it is also dangerous, because it means that you have to reset all variables at the start of each execution! Here are two examples:
# BAD! Once Speed has reached 50 then Break will always be 1
Speed >= 50 -> Break = 1;
# GOOD! Break will revert to 0 once Speed is less than 50
Break = 0, Speed >= 50 -> Break = 1;
# BETTER! Same but without zeroing first
Break = (Speed >= 50 ? 1 : 0)
There are also constants, variables which never change their value (except by reprogramming the gate). Constants are used to get consistent results from equations, or to get rid of "magic numbers" in the code. Basically, constants represent numbers that are specific to your contraption, such as height or a value for tweaking. Therefore instead of writing the actual height of the contraption where needed, you assign "HEIGHT" the value "500" (all uppercase is just a recommendation) at the start of your expression, and then use "HEIGHT" instead in your expressions.
Assignment Operators
Assignment operators are used for assigning values to variables. In theory there exists only one such operator, namely the assignment operator "=".
However, assignment operations where you do not replace the current variable, but act upon its current value, are also available. These are referred to as syntactic sugar (because they are just shorthand for a longer expression). An example of one of these is an assignment operation that adds a value to the current value of the variable "+=".
Life += 2
This above example is equal to:
Life = Life + 2
Presuming our earlier value for Life (42), in both of the examples Life will be set to 44. (42 + 2)
Here is a list of the assignment operators:
V = N : assignment
V += N : assignment using addition
V -= N : assignment using subtraction
V *= N : assignment using multiplication
V /= N : assignment using division
V %= N : assignment using modulo
V ^= N : assignment using exponentiation
Now we are going to learn more about Inputs and Outputs which are also variables.
Inputs and Outputs
Inputs and outputs are variables just like any other, however, they are bound to the inputs and outputs of the expression gate.
This means that for inputs, whatever is connected (wired) to input "A" on the expression gate will trigger an execution of the expression every time it changes values. The value of input "A" is also assigned to variable "A" so that it can be used during that execution. Likewise for outputs, whatever is connected to output "B" of the expression gate receives the value "B" was assigned when the last execution ended.
Both inputs and outputs are specified as a sequence of variables delimited by a single space.
A B Google
Functions
Certain functions are provided to give you access to access to more complex equations. All functions should follow this formula:
- Name (first letter lowercase)
- Left parenthesis (a left bracket "(")
- A comma separated argument list.
- A final right parenthesis (a right bracket ")")
- Eg.: atan2(4, 5.2)
# Absolute value of -2, equals 2
abs(-2)
# Round the value 3.42 down, equals 3
floor(3.42)
# Convert 67 from degrees to radians, equals 1.169...
rad(67)
This is a short list of the available functions, all of them are listed at the bottom of the page. The Expression Gate has 57 functions available.
Comparison Operators
Comparison operators compare two values and then output either one (true) or zero (false).
Comparison Operators:
N == N : equal
N != N : not equal
N > N : greater than
N < N : less than
N >= N : greater or equal
N <= N : less or equal
Note: The comparison operators in the expression gate are different from those of the other gates, namely that zero is false (0) and non-zero is true (1), which is different from that used in gates where negative and zero is false (0) and positive is (1). The cause for this is choice for programming semantics, although this might change in the future. Of further notice is that this gate as all others determine equality using a delta of 0.001, namely that "0 is equal to 0.00099" and so on.
Logical Operators
Logical operators are of little use by themselves, but when used in conjunction with comparison operators they become a powerful tool for creating conditions. These are used for creating more complex setups such as checking whether a value is in a particular range. The same applies here as for the comparison operators, either they return true (1) or false (0), also they input false (0) or true (non-zero).
Logical Operators
B & B : and: if all are true then outputs true, otherwise outputs false
B
Useful hints
- If you update a program on an Expression Gate, wires connected to the outputs and inputs will be kept if the name doesn't change. If the name is changed, all wires connected to the expression gate will be removed.
Example
N/A
Internal Clock
The internal clock makes it possible for the expression chip to perform actions independent of any change in input. There are three functions which use the internal clock. Values given to internal clock functions are measured in milliseconds, and the lowest value is 20 milliseconds. Any value less than 20 milliseconds will be increased to 20, except for 0, which will cancel any scheduled executions. You can't schedule more than one execution at a time; the most recently scheduled execution overrides any others. schedule (x) will cause a single execution to occur, in x milliseconds. interval (x) will cause an execution to occur every x milliseconds. clk () returns 1 if the expression was executed by the internal clock, as opposed to changes in inputs.
<code> schedule (85) : #Executes the expression in 85 milliseconds interval (30) :# Executes the expression every 30 milliseconds clk() : #If it was executed by either of the above functions, returns 1.
- Else, it returns 0.
Complete Function Listing
(complete as of SVN Revision 298--interpreted from wire\lua\entities\gmod_wire_expression\init.lua)
Function | Arguments | Description |
---|---|---|
abs | n | Returns the absolute value of n |
ceil | n | Returns n, rounded up to the nearest whole number |
ceil | n, d | Returns n, rounded up d places. ceil(135.2,1) = 140 |
clamp | v, l, u | If v > l and v < u, v. If v < l, l. If v > u, u. clamp(4.3,-1,1) = 1 and clamp(.5,-1,1) = .5 |
exp | n | Returns e^n (e = 2.781828...) |
floor | n | Returns n, rounded down to the nearest whole number |
floor | n, d | Returns n, rounded down d places. floor(135.2,1) = 130 |
frac | n | Returns the fractional part of n. frac(35.211) = .211 |
int | n | Returns n as an integer. Identical to floor(n). int(45.92) = 45 |
ln | n | Returns the natural logarithm of n. (e^ln(n)=n) |
log | n, b | Returns the logarithm of n base b. (b^log(n,b)=n) |
log2 | n | Returns the logarithm of n base 2. Identical (?) to ln_1(n) |
log10 | n | Returns the logarithm of n base 10. (10^log10(n)=n) |
mod | x, y | Returns the remainder of x / y. Similar (Identical?) to x % y. |
sgn | n | If n is positive, 1. If n is negative, -1. If n is zero, 0. |
sqrt | n | Returns the square root of n |
cbrt | n | Returns the cube root of n |
root | n, k | Return the kth root of n |
round | n | Returns n rounded to the nearest whole number |
round | n, d | Returns n rounded to the nearest d places |
e | None | Euler's constant. 2.78182818... |
max | a, b, ... | Returns the largest value in the group. max(9,6) = 9, and max(1,7,3,14,2) = 14 |
min | a, b, ... | Returns the smallest value in the group. min(9,6) = 6, and min(1,7,3,14,2) = 1 |
avg | a, b, ... | Returns the average value of the group. avg(3,5) = 4, and avg(5,1,88,32) = 31.5 |
sel | i, a, b, ... | Returns the ith value in the group, counting from 0. sel(2,1,2,3,4,5) = 3 |
random | None | Returns a random value between 0 and 1. random() might give 0.143038310 |
random | l, u | Returns a random value between l and u. random(3,8) might give 5.16872 |
curtime | None | Returns the time, in seconds, since the server started |
deg | r | Converts r from radians to degrees |
rad | d | Converts d from degrees to radians |
pi | None | Returns the value of pi, 3.14159265358979... |
sin | d | Returns the sine of d degrees |
cos | d | Returns the cosine of d degrees |
tan | d | Returns the tangent of d degrees |
sinr | r | Returns the sine of r radians |
cosr | r | Returns the cosine of r radians |
tanr | r | Returns the tangent of r radians |
asin | n | Returns the inverse sine of n, in degrees |
acos | n | Returns the inverse cosine of n, in degrees |
atan | n | Returns the inverse tangent of n, in degrees |
atan | x, y | Returns the inverse tangent of x/y, in degrees. Gives correct quadrant, and properly handles the case of y=0 |
atan2 | x, y | Identical to atan(x,y) |
asinr | n | Returns the inverse sine of n, in radians |
acosr | n | Returns the inverse cosine of n, in radians |
atanr | n | Returns the inverse tangent of n, in radians |
atanr | x, y | Returns the inverse tangent of x/y, in radians. Gives correct quadrant, and properly handles the case of y=0 |
atan2r | x, y | Identical to atanr(x,y) |
sin | d | Returns the hyperbolic sine of d degrees |
cos | d | Returns the hyperbolic cosine of d degrees |
tan | d | Returns the hyperbolic tangent of d degrees |
sinr | r | Returns the hyperbolic sine of r radians |
cosr | r | Returns the hyperbolic cosine of r radians |
tanr | r | Returns the hyperbolic tangent of r radians |
angnorm | d | Returns d degrees such that -180 <= d <= 180 (angnorm(270) = -90) |
angnormr | r | Returns r radians such that -pi <= d <= pi (angnorm(3pi/2) = -1pi/2) |
send | a, b, ... | Muxes a group together for transmission |
recv | x, n | Demuxes x and returns the nth number |
extcolor | r, g, b, a | Sets the color, 0-255 |
extcolorr | none | Returns the color (red component) |
extcolorg | none | Returns the color (green component) |
extcolorb | none | Returns the color (blue component) |
extcolora | none | Returns the color (alpha component) |
extposx | none | Returns the position (x coordinate) |
extposy | none | Returns the position (y coordinate) |
extposz | none | Returns the position (z coordinate) |
extpos | none | Returns the position (vector) |
extvelx | none | Returns the velocity (x coordinate) |
extvely | none | Returns the velocity (y coordinate) |
extvelz | none | Returns the velocity (z coordinate) |
extvel | none | Returns the velocity (vector) |
extangr | none | Returns the angle (roll angle) |
extangy | none | Returns the angle (yaw angle) |
extangp | none | Returns the angle (pitch angle) |
extdirfwx | none | Returns the forward vector (x component) |
extdirfwy | none | Returns the forward vector (y component) |
extdirfwz | none | Returns the forward vector (z component) |
extdirfw | none | Returns the forward vector (vector) |
extdirrtx | none | Returns the right vector (x component) |
extdirrty | none | Returns the right vector (y component) |
extdirrtz | none | Returns the right vector (z component) |
extdirrt | none | Returns the right vector (vector) |
extdirupx | none | Returns the up vector (x component) |
extdirupy | none | Returns the up vector (y component) |
extdirupz | none | Returns the up vector (z component) |
extdirup | none | Returns the up vector (vector) |
Sample gates done in expressions
Add gate
N@AddGate
I@A B C D E F G H
O@Value
Value = A + B + C + D + E + F + G + H
Subtraction gate
N@SubtractionGate
I@A B
O@Value
Value = A - B
Multiplication gate
N@SMultiplicationGate
I@A B C D E F G H
O@Value
Value = A * B * C * D * E * F * G * H
Division gate
N@DivisionGate
I@A B
O@Value
Value = A / B
Greater than gate
N@GreaterThanGate
I@A B
O@Value
Value = (A > B)
Greater than or equal to gate
N@GreaterThanOrEqualGate
I@A B
O@Value
Value = (A >= B)
Less than gate
N@LessThanGate
I@A B
O@Value
Value = (B > A)
Less than or equal to gate
N@LessThanOrEqualGate
I@A B
O@Value
Value = (B >= A)
"And" gate
N@AndGate
I@A B C D E F G H
O@Value
Value = (A & B & C & D & E & F & G & H)
"Or" gate
N@OrGate
I@A B C D E F G H
O@Value
Value = (A | B | C | D | E | F | G | H)
If then else gate
N@IfThenElseGate
I@A B C
O@Value
Value = C, A == 1 -> Value = B;
Timer
N@Timer
I@Run Reset
O@Value
interval (20)
Run -> Value += 0.02;
Reset -> Value = 0;
Accumulator
N@Accumulator
I@A Hold Reset
O@Value
interval (20)
!Hold -> Value += 0.02 * A;
Reset -> Value = 0;
Smoother
N@Smoother
I@A Rate
O@Value
interval (20)
A > Value -> Value += 0.02 * Rate;
Value > A -> Value -= 0.02 * Rate;
Angular velocity
N@angle velocity
I@None
O@VR VY VP
Roll = extangr()
VR = $Roll
Yaw = extangy()
VY = $Yaw
Pitch = extangp()
VP = $Pitch
Toggle
N@Toggle
I@Clk
O@A
Clk&~Clk->A=!A
Best of
N@Best Of
I@Clk Data Reset Invert
O@Out
!Invert & Clk & Data > Out -> Out = Data;
!Invert & Reset -> Out = 0;
Invert & Clk & Data < Out -> Out = Data;
Invert & Reset -> Out = Data;
Rated Random Chip
N@Rated Random Chip
I@Run Min Max Rate Round
O@Out
interval (Rate * 1000)
Run -> Out = random(Min,Max);
Round -> Out = round(Out);
Bi-way Accumilator
N@Accumulator
I@Up Down Reset Amount
O@Out
interval(20)
Up -> Out += (Amount / 50);
Down -> Out -= (Amount / 50);
Reset -> Out = 0;
Looper
N@Looper
I@Clk Rate Amount Max Min Invert
O@Out
interval(Rate * 1000)
Clk & !Invert -> Out += Amount;
Clk & Invert -> Out -= Amount;
Out > Max -> Out = Min;
Out < Min -> Out = Max;
Strober
N@Strober
I@Clk Amount Min Max Rate
O@Out
interval(Rate * 1000)
Out >= Max -> Down = 1;
Out <= Min -> Up = 1;
Out > Max -> Up = 0;
Out < Min -> Down = 0;
!Clk -> Out = 0;
Up == 1 -> Out += Amount;
Down == 1 -> Out -= Amount;
Clock
N@Clocker
I@Clk Data
O@Out
!Clk -> Out = 0;
Clk -> Out = Data;