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/14 13:15] virgilw4rpl: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 ======
Line 6: Line 8:
 Scripting in CW4 uses a custom language, **4RPL**. It is stack-based, similar to an HP calculator or the Forth programming language.  Scripting in CW4 uses a custom language, **4RPL**. It is stack-based, similar to an HP calculator or the Forth programming language. 
  
-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 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  
 + 
 +<code 4rpl> 
 +2 4 
 +</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.  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. 
  
-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 'addcommand. +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. 
  
 <code 4rpl> <code 4rpl>
-   2 4 + +2 4 + 
-   TraceAllSP+TraceAllSP
 </code> </code>
  
-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 stack, puts a space in between them and prints them to the Console. A '6is now printed on the console. +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 stack, puts a space in between them and prints them to the Console. A "6is now printed on the console.  
 + 
 +:!: 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.
  
 Part of 4RPL is the commands that manipulate map objects, such as creeper, terrain and units. Here is a simple script to put some creeper on a specific cell on a map.  Part of 4RPL is the commands that manipulate map objects, such as creeper, terrain and units. Here is a simple script to put some creeper on a specific cell on a map. 
  
 <code 4rpl>  <code 4rpl> 
-  12 23 100 AddCreeper  +12 23 100 AddCreeper   
 +</code> 
 + 
 +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 
 +# Below is code         # Below is a comment 
 +2 4 5  add              # adds 4 and 5 to get 9
 </code> </code>
  
-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 [[map coordinate system]])). 
  
 ===== Warp Notation ===== ===== 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 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
  
-==== Example ==== 
 <code 4rpl> <code 4rpl>
-   12 23 100 AddCreeper +12 23 100 AddCreeper 
-   AddCreeper(12 23 100) #Does the same thing as the line above.+AddCreeper(12 23 100) # Does the same thing as the line above. 
 + 
 +1 (2 3 +) 4  # Demonstrate wrapping affects all stack locations. 
 +2 3 + 1 4    # This line is the same as the one above.
 </code> </code>
  
  
 ===== Conditionals ===== ===== Conditionals =====
-'Ifstatements in 4rpl consist of an 'if' an option 'elseand a closing 'endif' command. The 'if' command 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 not true, then execution jumps to either the 'elseor the 'endifinstruction in the script.+**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.
  
 <code 4rpl> <code 4rpl>
Line 49: Line 74:
 endif endif
 </code> </code>
 +
 +
 +
 +Note: See [[4rpl:Data Types]] for comparison between dissimilar types and type conversion. 
 +
 +
 +
 +===== 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 a 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 4rpl>
 +3 4 + ->result
 +"3 plus 4 is" <-result TraceAllSP
 +</code>
 +
 +
 +
 +Note: See [[4rpl:Data Types]] for comparison between dissimilar types and type conversion. 
 +
 +
 +
 +===== Functions =====
 +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 a function is formatted as **@FunctionName**. Function names are //case-insensitive//.
 +
 +<code 4rpl>
 +4 2 @Function1 Trace  # prints "11"
 +@Function2            # prints "function two"
 +
 +:function1
 +    add    # adds together 2 items on the stack
 +    5 add  # adds an extra 5 to that
 +
 +:function2
 +    "function two" Trace
 +</code>
 +
 +Read more about functions on the pages for [[4rpl:commands:call|Function Calls]] and [[4rpl:commands:func|Function Declarations]]
 +
  
 ===== Loops ===== ===== Loops =====
-4rpl support both "do/loop" and "while/repeat" looping constructs. \\ +4RPL supports both "do/loop" and "while/repeat" looping constructs. \\ 
-For an example of a 'doloop see: [[4rpl:commands:do]] \\ +For an example of a **do** loop see: [[4rpl:commands:do]] \\ 
-For an example of a 'whileloop see: [ [4rpl:commands:while]] \\+For an example of a **while** loop see: [[4rpl:commands:while]] \\
  
-==== Comments ==== +\\ 
-Comments in 4RPL can be either whole line or partial lineThe comment terminates when line ends+ 
-Comments are indicated by the "hashcharacter (**#**),+---- 
 +===== Working with CPACKs ===== 
 +When a CPACK is imported, it 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 width center twothirds> 
 +<code> 
 +%HOMEPATH%\Documents\My Games\creeperworld4\creeperworld4\editor\map2\cpacks\[CPACKNAME] 92415695-49d6-4e97-852b-64493e76233b\scripts\ 
 +</code>  
 +</WRAP> 
 + 
 +If you load map as a regular player and then enter edit mode, the game will stick the scripts into the _UNKNOWN project directory. If there are already matching scripts present at that location, they will not be overwritten (importing CPACK is the only exception to this).  If you export, it will take the scripts on disk.  Basically, in the normal case there is project directory on diskWhen you open 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  
 + 
 +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 "_UNKNOWNas the directory.  Everything else is the same after that. 
 + 
 +If you want to grab the scripts from a map (like an FPS mapand you want to make sure you have the latest scriptsyou should either create a 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. 
 + 
 +Basically, just deleting the _UNKNOWN directory is the easiest way to clean it
  
-<code 4rpl># This is how we add two of the three numbers 
-# that are on the stack 
-# Below is code         #  Below is a comment 
-2 4 5  add              # adds 4 and 5 to get 9</code> 
  
-==== Symbol Aliasing ==== +----
-Many arithmetic operators  can be substituted with shorthand symbols. +
  
-^ Operator ^ Symbol ^ +===== Code translator =====
-| ADD | + |  +
-| SUB | - |  +
-| MUL | * |  +
-| DIV | / |  +
-| MOD | % |  +
-| AND | && |  +
-| OR  | %%||%% |  +
-| NOT | ! |  +
-| POW | %%^%% |  +
-| GT  | > |  +
-| GTE | >=|  +
-| LT  | < |  +
-| LTE | %%<=%% |   +
-| EQ  | == |  +
-| NEQ | != |  +
-  +
-==== 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 statements. It 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.  [[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 statements. It 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. 
  
 For those struggling to master 4RPL'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.  For those struggling to master 4RPL'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. 
  
4rpl/overview.1610648156.txt.gz · Last modified: 2021/01/14 13:15 by virgilw