User Tools

Site Tools


4rpl:overview

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
4rpl:overview [2021/01/09 13:26] Karsten754rpl:overview [2022/02/14 06:12] (current) – Attempt to make wrapping notation slightly easier to grok. serdalis
Line 1: Line 1:
  
 <=[[4rpl:start| 4RPL Scripting Language]] <=[[4rpl:start| 4RPL Scripting Language]]
 +
 +:!: To edit files you will need an editor. Read how to get started with [[creating and editing 4rpl scripts]].
  
 ====== Scripting ====== ====== Scripting ======
  
-4RPL programming is stack-based language, similar to programming a HP calculator or Forth language programming. If this is not something you have done, read on for a brief introduction to 4RPL and stack-based programming+Scripting in CW4 uses a custom language, **4RPL**. It is stack-based, similar to an HP calculator or the Forth programming language. 
  
-An 4RPL instruction either use arguments (data) that is on a "stack", or place arguments on the "stack"Arguments on the stack are in LIFO (**L**ast **I**n, **F**irst **O**ut) order. That means the argument that was placed on the stack last will be the first to be retrieved. Imagine this as a stack of coins. If you put a coin on the top, then the first coin to be taken off will also be the one that was last placed on the stack+4RPL commands either read data from the //stack// or place data on the //stack// Data (arguments) on the stack are in LIFO (**L**ast **I**n, **F**irst **O**ut) order. That means the argument that was placed on the stack last will be the first to be retrieved. Imagine this as a stack of coins. If you put a coin on the top, then the first coin to be taken off will also be the one that was last placed on top
  
-You can place arguments on the stack by typing them, or executing an instruction that will push arguments on to the stack. For instance, you can type <code 4rpl>2 4 </code> and then these two numbers will be pushed onto the stack when your script executes.+You can place arguments on the stack by typing them, or executing an instruction that will push arguments on to the stack. For instance, you can type 
  
-As an illustration, imagine you want to add two of the numbers you entered above. The instruction to perform addition is **[[4rpl:commands:add|add]]**. Either of these mini scripts will now perform the addition:+<code 4rpl
 +2 4 
 +</code> 
  
-<code 4rpl>2 4 5 add</code>+and then these two numbers will be pushed onto the stack when your script executes.
  
-   +An easy way to familiarize yourself with 4RPL is to use the [[cw4:4rpl_console|Console]] in the [[cw4:editor_mods|Mission Editor]]Let's use this to illustrate the next few examples
-Either of the scripts above will read the two most recent arguments on the stack (4 and 5), add them and push the sum on to the stack. After the instruction has completed, there will be two numbers on the stackThe "2" from your original entry, and "9"the sum of the two arguments added by the "add" instruction The stack would now look like this: +
-<code 4rpl>2 9</code>+
  
-<wrap info round > +As an illustration, imagine you want to add two of the numbers you entered above. The instruction to perform addition is **[[4rpl:commands:add|add]]**. You can also simply use a "+" rather than the **add** command. 
-You can find more commands and detailed explanations of them in the [[crpl:crplreference|CRPL Reference]]+
-</wrap>+
  
-==== Comments ====+<code 4rpl> 
 +2 4 + 
 +TraceAllSP 
 +</code>
  
-Adding comments makes code easier to understandand sometimes helps the programmer or other readers to grasp complex pieces of logicAlso, after some time intervalit refreshes one's memory about exactly what certain piece of code was intended to do.+The code above puts 2 numbers on the stack, the ''%%add%%'' (or ''%%+%%'') command took the 4 and the 2 (last-in, first out) added them and pushed the result (6) back on the stack[[4rpl:commands:traceAllSp|TraceAllSp]] pops (takes/removes) all elements on the stackputs space in between them and prints them to the Console. A "6" is now printed on the console
  
-Comments in xRPL can be either a whole line or a partial line. The comment terminates when a line ends.+:!: Note that 4RPL is a //case-insensitive// language, meaning **__any and all capitalization is ignored__**. **TraceAllSP** may as well be written as **TRACEALLSP** or **traceallsp**, there is no difference.
  
-Comments are indicated by the "hash" character (**#**),+Part of 4RPL is the commands that manipulate map objectssuch as creeper, terrain and units. Here is a simple script to put some creeper on a specific cell on a map. 
  
 +<code 4rpl> 
 +12 23 100 AddCreeper  
 +</code>
  
-<code 4rpl># This is how we add two of the three numbers+Run this **once** in the console and then unpause the game and let the game simulation run a few frames. You will see a stack of 100 deep creeper at map coordinate x=12 and z=23 ((For more information on the game coordinate system and why we use **x** and **z** coordinates, see [[cw4:custom_maps#Understanding the 3D Coordinate System]])). 
 + 
 + 
 +===== Comments ===== 
 +Comments in 4RPL can be either a whole line or a partial line. The comment terminates when a line ends. 
 +Comments are indicated by the "hash" character **#**, 
 + 
 +<code 4rpl> 
 +# This is how we add two of the three numbers
 # that are on the stack # that are on the stack
-# Below is code         #  Below is a comment +# Below is code         # Below is a comment 
-2 4 5  add              # adds 4 and 5 to get 9</code>+2 4 5  add              # adds 4 and 5 to get 9 
 +</code>
  
-<wrap important>Note:</wrap> Unless explicitly noted otherwise, all instructions are destructive stack operations in that they will remove as many arguments as is required for their execution from the stack and replace those with the output from their execution. In the example above, the original two items on the stack have been replaced by their sum. 
  
-Likewise, note that the most recent item pushed on to the stack will also be the first item to be removed. This is referred to as LIFO (**L**ast **I**n, **F**irst **O**ut) processing.+===== Warp Notation ===== 
 +Warp notation is an optional notation that allows the use of parentheses to change the order that things are written. The use of parentheses is called warp notation since it warps, or moves, things around. Parentheses can be added anywhere in 4RPL and will move the command that proceeded the opening parenthesis to the location of the closing parenthesis. This can be useful for making some expressions look more like functions in some other languages
  
-==== Warp Notation ====+<code 4rpl> 
 +12 23 100 AddCreeper 
 +AddCreeper(12 23 100) # Does the same thing as the line above.
  
-An extra and optional <wrap hi>operator</wrap> ((In mathematics and sometimes in computer programming, an operator is a character that represents an action, as for example x is an arithmetic operator that represents multiplicationIn computer programs, one of the most familiar sets of operators, the Boolean operators, is used to work with true/false values.)) change how you can choose to write the syntax.  It doesn't change anything about the stack, or how xRPL works.  It only gives you a syntax alternative that can make things easier to read in some cases.+(2 3 +) 4  # Demonstrate wrapping affects all stack locations. 
 +2 3 + 1 4    # This line is the same as the one above. 
 +</code>
  
-This operator is called the  **warp**  operator since it warps things around. In more technical terms,  it allows for prefix notation.   Take the following example: 
  
-<code prpl>3 4 add </code>+===== Conditionals ===== 
 +**If** statements in 4RPL consist of an **if**, optional **else** and a closing **endif** instruction. The **if** instruction pops the item from the top of the stack and evaluates it as a boolean (true or false, not 0 or 0). If the item is false, then execution jumps to the **else** or **endif** instruction in the script.
  
-This means to push 3 to the stack, push 4 to the stack, then to call <wrap round box>add</wrap> <wrap round box>Add</wrappops two items from the stack, adds them together, then pushes the result back.  +<code 4rpl> 
 +if ( GetGameUpdateCount 900 >= ) 
 +   TraceAllSp("More than 30 seconds of game time have passed"
 +else 
 +   TraceAllSp("Less than 30 seconds of game time have passed"
 +endif 
 +</code>
  
-This means two items on the stack before the operation, and one item on the stack afterwards. 
-This is all xRPL (or RPL, or Forth...) standard stuff and the primary principle of the language.  It remains unchanged and untouched. 
  
-Introducing the Warp operator.   
  
-<code prpl>3 4 add</code>  +Note: See [[4rpl:Data Types]] for comparison between dissimilar types and type conversion. 
-can become +
-<code prpl>add (3 4)</code>+
  
-The open parenthesis <wrap round box>(</wrap> means to warp the all arguments between it and the closing parenthesis <wrap round box>)</wrap> to **before** the command during compilation.  This is merely a syntax trick to improve readability of some code and has no run-time penalty. It is resolved during compilation, when the  compiler literally //warps// the 3 and 4 from inside the parenthesis to the front of the <wrap round box>add</wrap> operation when the code is compiled.  
  
-Here the warp operator is used to make the code slightly more readable. 
-<code prpl>3 add(4)</code> 
-Here, the <wrap round box>4</wrap>to the front of the <wrap round box>add</wrap>, resulting in <wrap round box>3 4 add</wrap> which is the exact same thing as the first example. 
  
-Take second example:+===== Variables ===== 
 +The stack is not the only place to store information in 4RPL, it is possible to take items from the stack and store them in //variables//. To store variable //from// the stack, write **->varname**. To write a variable back //to// the stack, write **<-varname**. Variables remember their value and can be written to the stack multiple times. Variable names are //case-insensitive//.
  
-<code prpl+<code 4rpl
-# CurrentCoords obtains the (X, y) location and places it on the stack +3 4 + ->result 
-# GetCreeper uses the (X,Y) coordinates to  look at a cell on a map +"3 plus 4 is<-result TraceAllSP
-#   and returns the amount of creeper on that cell +
-# The "IFstatement then evaluates for a value greater than one (In  +
-#  other words, creeper exists in a significant amount) and if true +
-#  displays a message in the trace output. +
- +
-CurrentCoords GetCreeper 1 gt if +
-   "Creeper exists here" Trace +
-endif+
 </code> </code>
  
-The comments in the sample above explains what we're trying to achieve with this code snippet.  
  
-It uses the current coordinates, to get the creeper at that coordinate location, then checks if it is greater than 1.  If so, it writes the string //"Creeper exists here"// to the trace log.  
  
-Because of the way the stack works, you have to push two coordinates to the stack first (<wrap round box>[[crpl:docs:currentcoords|CurrentCoords]]</wrap> does that), then call <wrap round box>[[crpl:docs:GetCreeper|GetCreeper]]</wrap> That takes two items from the stack, uses them as coordinates, then pushes the value of the creeper at that map location back to the stack. The number <wrap round box>1</wrap> is then pushed to the stack, and the <wrap round box>[[crpl:docs:gt|gt]]</wrap> operator  takes two items from the stack and does a //"greater than"// comparison between them.  The result is either <wrap round box>0</wrap> (//<wrap round box>[[crpl:docs:false|false]]</wrap>//) or <wrap round box>1</wrap> (//<wrap round box>[[crpl:docs:true|true]]</wrap>//and that is pushed back to the stack.  Finally, the <wrap round box>[[crpl:docs:if|if]]</wrap> statement pops the result of the <wrap round box>[[crpl:docs:gt|gt]]</wrap> call from the stack and then either allows execution to pass to the <wrap round box>[[crpl:docs:trace|Trace]]</wrap> statement, or jumps to the <wrap round box>[[crpl:docs:endif|endif]]</wrap>.+NoteSee [[4rpl:Data Types]] for comparison between dissimilar types and type conversion
  
-That's xRPL in a nutshell.  
  
-Using **warp notation**, we can make this slightly more readable.  
  
-<code prpl> +===== Functions ===== 
-# CurrentCoords obtains the (X, y) location and places it on the stack +4RPL supports user-defined functions that can be called from anywhere in the script. Functions are declared at the end of a script and are formatted as **:FunctionName**. A call to function is formatted as **@FunctionName**. Function names are //case-insensitive//.
-# GetCreeper uses the (X,Y) coordinates to  look at a cell on a map +
-#   and returns the amount of creeper on that cell +
-# The "IF" statement then evaluates for value greater than one (In  +
-#  other words, creeper exists in a significant amount) and if true +
-#  displays message in the trace output.+
  
-if ( GetCreeper(CurrentCoords)  gt (1) ) +<code 4rpl> 
-   Trace ("Creeper exists here") +4 2 @Function1 Trace  # prints "11
-endif +@Function2            # prints "function two"
-</code> +
-Notice that spaces before or after a warp operator <wrap round box>(</wrap> and <wrap round box>)</wrap> don't matter.  You can put spaces, or you can bump the warp operator up right next to something else.+
  
-Note also that this syntax is totally optional and can be intermixed with standard RPL notation as seems appropriate.  For instance, assignments still look better without warping. +:function1 
-<code prpl> +    add    # adds together 2 items on the stack 
-7 ->x +    5 add  # adds an extra 5 to that 
-->x(7)+ 
 +:function2 
 +    "function two" Trace
 </code> </code>
-Both the above statements assign 7 to the variable "x". Choose which format you prefer and you think makes your code most readable - then be consistent. :) 
  
-==== Symbol Aliasing ====+Read more about functions on the pages for [[4rpl:commands:call|Function Calls]] and [[4rpl:commands:func|Function Declarations]]
  
-Many arithmetic operators  can be substituted with shorthand symbols **(4RPL only)**.  
  
-^ Operator ^ Symbol ^ +===== Loops ===== 
-| ADD | + |  +4RPL supports both "do/loop" and "while/repeat" looping constructs. \\ 
-| SUB | - |  +For an example of a **do** loop see: [[4rpl:commands:do]] \\ 
-| MUL | * |  +For an example of **while** loop see: [[4rpl:commands:while]] \\
-| DIV | / |  +
-| MOD | % |  +
-| AND | && |  +
-| OR  | %%||%% |  +
-| NOT | ! |  +
-| POW | %%^%% |  +
-| GT  | > |  +
-| GTE | >=|  +
-| LT  | < |  +
-| LTE | <|   +
-| EQ  | == |  +
-| NEQ | !|  +
-  +
-==== Code translator ==== +
-[[https://github.com/Arin112}Arin112]] wrote code translator that can translate in-fix ((Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.)) code to the xRPL post-fix notation. +
  
-For those struggling to master xRPL's post-fix format, this may be a useful tool. It can be obtained from [[https://github.com/Arin112/mplLang|github]] and is accompanied by many samples. Of course, it should be noted that the code and translation will only be up-to-date as long as the repository is maintained. +\\
  
-Here are some sample translationstaken directly from the GitHub repository: +---- 
- +===== Working with CPACKs ===== 
-<WRAP group> +When a CPACK is importedit does not overwrite any scripts that might be on disk.   So if there are scripts on disk those will get jammed back into the CPACK whenever a compile is done (and a compile is done during finalization).  You can look on disk at a location like this:  
-<WRAP half column> +<WRAP width center twothirds
-**mplLang code** +<code> 
-<code c+%HOMEPATH%\Documents\My Games\creeperworld4\creeperworld4\editor\map2\cpacks\[CPACKNAME] 92415695-49d6-4e97-852b-64493e76233b\scripts\ 
-x = 2 + 2 * 2; +</code> 
-</code> +
-</WRAP> +
-<WRAP half column> +
-**xRPL translated code** +
-<code 4rpl> +
-2 2 2 mul add ->x +
-</code+
-</WRAP>+
 </WRAP> </WRAP>
  
-<WRAP group> +If you load map as a regular player and then enter edit modethe game will stick the scripts into the _UNKNOWN project directory. If there are already matching scripts present at that locationthey will not be overwritten (importing a CPACK is the only exception to this).  If you export, it will take the scripts on disk.  Basically, in the normal case there is a project directory on disk. When you open a project, that directory is where scripts are location on disk.  When you compile, those are the ones compiled. They get placed into the exported or finalized CPACK.  
-<WRAP half column> +
-<code c> +
-z = f(xy); +
-</code> +
-</WRAP> +
-<WRAP half column> +
-<code 4rpl> +
-z = f(xy)+
-</code> +
-</WRAP> +
-</WRAP>+
  
-<WRAP group> +The case of on-the-fly opening the editor in a map you are playing is no different -with one exception: The game doesn't know what the project directory is, so it uses "_UNKNOWN" as the directory.  Everything else is the same after that.
-<WRAP half column> +
-<code c> +
-[x, y] = CurrentCoords(); +
-</code> +
-</WRAP> +
-<WRAP half column> +
-<code 4rpl> +
-CurrentCoords ->y ->x +
-</code> +
-</WRAP> +
-</WRAP>+
  
-<WRAP group> +If you want to grab the scripts from map (like an FPS mapand you want to make sure you have the latest scriptsyou should either create project for the map and open it from the project page, or you should clean your _UNKNOWN directory before you open the map on the fly.
-<WRAP half column> +
-<code c> +
-if(a<b && (c+1 == -c)) [ab, c] = 1, 2.0, 3.14; +
-</code> +
-</WRAP> +
-<WRAP half column> +
-<code 4rpl> +
-<-<-b lt <-c 1 add <-c neg eq and if +
-1 2 3.140000 ->c ->b ->a +
-endif +
-</code> +
-</WRAP> +
-</WRAP>+
  
-<WRAP group> +Basically, just deleting the _UNKNOWN directory is the easiest way to clean it 
-<WRAP half column> + 
-<code c> + 
-do (1 .. 42){ +---- 
-refRead("num"); + 
-refWrite(7*(3.14+i), "num")+===== Code translator ===== 
-+[[https://github.com/Arin112}Arin112]] wrote a code translator that can translate in-fix ((Infix notation is the notation commonly used in arithmetical and logical formulae and statementsIt is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.)) code to the 4RPL post-fix notation.  
-</code> + 
-</WRAP> +For those struggling to master 4RPL's post-fix format, this may be useful toolIt can be obtained from [[https://github.com/Arin112/mplLang|github]] and is accompanied by many samples. Of course, it should be noted that the code and translation will only be up-to-date as long as the repository is maintained. 
-<WRAP half column> +
-<code 4rpl> +
-42 1 do +
-"num" <-! ->a +
-7 3.140000 i add mul "num" ->! +
-loop +
-</code> +
-</WRAP> +
-</WRAP>+
  
-===== Tutorials and how-to ===== 
-link to Cornucanis' examine map resources 
-link to rotation tutorial 
4rpl/overview.1610216790.txt.gz · Last modified: 2021/01/09 13:26 by Karsten75