Wired Expression Chip

From GMod Wiki

Revision as of 11:19, 16 November 2011 by [RV] TJjokerR (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Deletion.pngThis 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
Wire Icon.png Wiremod ToolYarin Kaul Icon ArrowSquare32 left.png Back
Information 16x16.png Function:A programmable gate.
link=User:{{{author}}} Primary fire:Spawns an Expression Gate.
Wrench orange.png Secondary fire:Loads the Expression code to the menu.
Link break.png Reload:Resets all variables.
Page white text.png 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.

Expression Gate Documentation

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.

  1. From the list of programs in the expression gate interface.
  2. Using the "New Expression..."-button in the interface which allows you to enter expressions.
  3. Fetch the program from a gate using the right mouse button, it will then show up in the expression gate interface.
  4. 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:

  1. Name (first letter lowercase)
  2. Left parenthesis (a left bracket "(")
  3. A comma separated argument list.
  4. 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.

  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
absnReturns the absolute value of n
ceilnReturns n, rounded up to the nearest whole number
ceiln, dReturns n, rounded up d places. ceil(135.2,1) = 140
clampv, l, uIf 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
expnReturns e^n (e = 2.781828...)
floornReturns n, rounded down to the nearest whole number
floorn, dReturns n, rounded down d places. floor(135.2,1) = 130
fracnReturns the fractional part of n. frac(35.211) = .211
intnReturns n as an integer. Identical to floor(n). int(45.92) = 45
lnnReturns the natural logarithm of n. (e^ln(n)=n)
logn, bReturns the logarithm of n base b. (b^log(n,b)=n)
log2nReturns the logarithm of n base 2. Identical (?) to ln_1(n)
log10nReturns the logarithm of n base 10. (10^log10(n)=n)
modx, yReturns the remainder of x / y. Similar (Identical?) to x % y.
sgnnIf n is positive, 1. If n is negative, -1. If n is zero, 0.
sqrtnReturns the square root of n
cbrtnReturns the cube root of n
rootn, kReturn the kth root of n
roundnReturns n rounded to the nearest whole number
roundn, dReturns n rounded to the nearest d places
eNoneEuler's constant. 2.78182818...
maxa, b, ...Returns the largest value in the group. max(9,6) = 9, and max(1,7,3,14,2) = 14
mina, b, ...Returns the smallest value in the group. min(9,6) = 6, and min(1,7,3,14,2) = 1
avga, b, ...Returns the average value of the group. avg(3,5) = 4, and avg(5,1,88,32) = 31.5
seli, a, b, ...Returns the ith value in the group, counting from 0. sel(2,1,2,3,4,5) = 3
randomNoneReturns a random value between 0 and 1. random() might give 0.143038310
randoml, u Returns a random value between l and u. random(3,8) might give 5.16872
curtimeNoneReturns the time, in seconds, since the server started
degrConverts r from radians to degrees
raddConverts d from degrees to radians
piNoneReturns the value of pi, 3.14159265358979...
sindReturns the sine of d degrees
cosdReturns the cosine of d degrees
tandReturns the tangent of d degrees
sinrrReturns the sine of r radians
cosrrReturns the cosine of r radians
tanrrReturns the tangent of r radians
asinnReturns the inverse sine of n, in degrees
acosnReturns the inverse cosine of n, in degrees
atannReturns the inverse tangent of n, in degrees
atanx, yReturns the inverse tangent of x/y, in degrees. Gives correct quadrant, and properly handles the case of y=0
atan2x, yIdentical to atan(x,y)
asinrnReturns the inverse sine of n, in radians
acosrnReturns the inverse cosine of n, in radians
atanrnReturns the inverse tangent of n, in radians
atanrx, yReturns the inverse tangent of x/y, in radians. Gives correct quadrant, and properly handles the case of y=0
atan2rx, yIdentical to atanr(x,y)
sindReturns the hyperbolic sine of d degrees
cosdReturns the hyperbolic cosine of d degrees
tandReturns the hyperbolic tangent of d degrees
sinrrReturns the hyperbolic sine of r radians
cosrrReturns the hyperbolic cosine of r radians
tanrrReturns the hyperbolic tangent of r radians
angnormdReturns d degrees such that -180 <= d <= 180 (angnorm(270) = -90)
angnormrrReturns r radians such that -pi <= d <= pi (angnorm(3pi/2) = -1pi/2)
senda, b, ...Muxes a group together for transmission
recvx, nDemuxes x and returns the nth number
extcolorr, g, b, aSets the color, 0-255
extcolorrnoneReturns the color (red component)
extcolorgnoneReturns the color (green component)
extcolorbnoneReturns the color (blue component)
extcoloranoneReturns the color (alpha component)
extposxnoneReturns the position (x coordinate)
extposynoneReturns the position (y coordinate)
extposznoneReturns the position (z coordinate)
extposnoneReturns the position (vector)
extvelxnoneReturns the velocity (x coordinate)
extvelynoneReturns the velocity (y coordinate)
extvelznoneReturns the velocity (z coordinate)
extvelnoneReturns the velocity (vector)
extangrnoneReturns the angle (roll angle)
extangynoneReturns the angle (yaw angle)
extangpnoneReturns the angle (pitch angle)
extdirfwxnoneReturns the forward vector (x component)
extdirfwynoneReturns the forward vector (y component)
extdirfwznoneReturns the forward vector (z component)
extdirfwnoneReturns the forward vector (vector)
extdirrtxnoneReturns the right vector (x component)
extdirrtynoneReturns the right vector (y component)
extdirrtznoneReturns the right vector (z component)
extdirrtnoneReturns the right vector (vector)
extdirupxnoneReturns the up vector (x component)
extdirupynoneReturns the up vector (y component)
extdirupznoneReturns the up vector (z component)
extdirup noneReturns 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;

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox