User Tools

Site Tools


crpl:crpltutorial

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
crpl:crpltutorial [2013/09/09 11:06] – Added link to CW3 home Grauniadcrpl:crpltutorial [2020/05/27 07:59] (current) – All referenced functions are now links to their respective wiki pages. Also added a paragraph about ELSE and changed up some code examples to be more consise. Sanian
Line 1: Line 1:
-~~DISCUSSION~~ 
-<html><!--Comments visible to editors only can be placed in HTML comments --></html> 
-<html><!-- Right now, this is a wall of text.  I'm working on getting it into wiki form, but it might take a bit of work :D --></html> 
 <-[[cw3:creeper_world_3|CW3 Home]]<- [[crpl:start|CRPL Home]] <-[[cw3:creeper_world_3|CW3 Home]]<- [[crpl:start|CRPL Home]]
 ====== CRPL Tutorial ====== ====== CRPL Tutorial ======
 **ATTENTION:  WORK IN PROGRESS** **ATTENTION:  WORK IN PROGRESS**
-<box 60% round green |**Acknowledgements**> + 
-//This tutorial is a contribution by members of the CW3 beta team. Their contributions are gratefully acknowledged//\\+**Acknowledgements** 
 +//This tutorial is a contribution by members of the CW3 beta team. Their contributions are gratefully acknowledged.//\\
 \\  \\ 
 Special thanks to: Special thanks to:
-  * J +  * J (for writing the guide) 
-  * Michionlion +  * Michionlion (for porting the biggest part to the wiki)
-</box> +
- +
-<HTML><!-- I don't believe we need an additional "contents" section of contents are auto-generated - see the contents section at the top-right of the page.  +
-==== Contents==== +
-[[crpltutorial#Starting with CRPL|Starting with CRPL]]\\ +
-[[crpltutorial#Using the tracelog and further introduction of a stack language|Using the tracelog and further introduction of a stack language]]\\ +
-[[crpltutorial#Comparing numbers and conditions|Comparing numbers and conditions]]\\ +
-[[crpltutorial#More functions|More functions]]\\ +
-[[crpltutorial#Variables and Loops|Variables and Loops]]\\ +
-[[crpltutorial#Defining your own functions|Defining your own functions]]\\ +
-[[crpltutorial#Making you script more useful|Making you script more useful]]\\ +
-[[crpltutorial#Useful links|Useful links]]\\ +
---></HTML>+
  
 === Before you start === === Before you start ===
Line 29: Line 14:
 The most important thing that you will need to know is the basics of CW3 gameplay and CW3 editor. It is a good thing if you have programmed before but if that wasn't a stack language it sometimes is a disadvantage. You don't have to have any programming skills to start with [[crpl:overview|CRPL]]. The most important thing that you will need to know is the basics of CW3 gameplay and CW3 editor. It is a good thing if you have programmed before but if that wasn't a stack language it sometimes is a disadvantage. You don't have to have any programming skills to start with [[crpl:overview|CRPL]].
  
 +In order to proceed, you will need to know how to get a script to run, including attaching it to a CRPL Core. The collapsed section below contains a button-by-button guide.
 +
 +<hidden>
 +To do this, you will need a script, such as the following (feel free to copy/paste this):
 +
 +<code>
 +once
 +    ShowTraceLog
 +    "Hello World!" Trace
 +endonce
 +</code>
 +
 +To run it:
 +  * Open the editor.
 +    * Press "Edit Map"
 +    * Then "Units"
 +    * And finally "Scripts"
 +  * Enter a name, and press "Create".
 +  * Press "Edit". The game will minimise, and a text editor will open.
 +  * Paste the above script into the text file and save it.
 +  * Return to the editor, and press "compile all" at the bottom of the "scripts" panel.
 +    * Note that some text appears. It should say "1 Script successfully compiled into 5 opcodes".
 +  * Create (build) a CRPLCORE (that unfamiliar unit in the lower panel) and select it.
 +  * On the right, there is a drop-down, from which you can select the script you just made.
 +    * Select it and press "Add".
 +  * Unpause the game, and watch the result. You now know how to run scripts.
 +</hidden>
 +\\
  
 ==== Starting with CRPL ==== ==== Starting with CRPL ====
  
 +<note>
 +If at anytime you need more practice than given here, go to [[crpl:crpltutorial:interactive|the interactive tutorials]]
 +</note>
 +
 +First of all, if you have programmed before, try to forget that while reading. CRPL is (unlike many others) a stack based language, that means you put (push) all numbers on a big stack of numbers and functions (like //[[crpl:docs:setcreeper|SetCreeper]]// or //[[crpl:docs:queuemove|QueueMove]]//) get (pop) the last number(s) from the stack and do something with it (create a spore, emit creeper). Think of a stack of plates in the cafeteria, you can only take the top plate. Then someone brings more plates, those go on top and people start taking from the top again. The whole script will be executed once per frame. In scripts, you can use '#' to skip the rest of the line. Think of it as a comment in your script.
  
-First of all, if you have programmed before, try to forget that while reading. CRPL is (unlike many others) a stack based language, that means you put (push) all numers on a big stack of numbers and functions (like SetCreeper or QueueMove) get (pop) the last number(s) from the stack and do something with it (create a spore, emit creeper). Think of a stack of plates in the cafeteria. People take the top plate. Then someone brings more plates and they go on top and people start taking from the top again. The whole script will be executed once per frame. In scripts, you can use '#' skip the rest of the line. Think of it as a comment in you script. 
 <code> <code>
 #this is a comment and will not be executed</code> #this is a comment and will not be executed</code>
-Time to create your first CRPL script! Try to use the functions GetCoords and SetCreeper. CurrentCoords puts two numbers on the stack (the X and Y coordinates of the CRPL tower) and SetCreeper pops 3 numbers from the stack (X and Y coords the creeper will be set on and the amount of creeper). The following code sets the creeper height to 5 every frame (there are 30 frames in an in-game second):+Time to create your first CRPL script! Try to use the functions //[[crpl:docs:currentcoords|CurrentCoords]]// and //[[crpl:docs:setcreeper|SetCreeper]]////CurrentCoords// puts two numbers on the stack (the X and Y coordinates of the CRPL tower) and //SetCreeper// pops 3 numbers from the stack (X and Y coords the creeper will be set on and the amount of creeper). The following code sets the creeper height to 5 every frame (there are 30 frames in an in-game second):
 <code> <code>
 #Set the creeper to height 5 on the current position #Set the creeper to height 5 on the current position
-CurrentCoords 5 SetCreeper</code> +CurrentCoords 5 SetCreeper 
-Instead of '5' you could pick another number or a variable (explained later) and instead of CurrentCoords you could pick two number to indicate the cell where the creeper has to be set. +</code> 
 + 
 +Instead of '5' you could pick another number or a variable (explained later) and instead of //CurrentCoords// you could pick two number to indicate the cell where the creeper has to be set. 
  
 Here are 2 more examples of what you could write: Here are 2 more examples of what you could write:
  
-  * AddCreeper: instead of setting the creeper to a specific height, this adds creeper.<code>+  * //[[crpl:docs:addcreeper|AddCreeper]]//: instead of setting the creeper to a specific height, this adds creeper.<code>
 0 0 1 AddCreeper</code> 0 0 1 AddCreeper</code>
-  * RandCoords: This picks a random X and Y coordinate somewhere on the map. <code>#Set the creeper to 1 on a random position+  * //[[crpl:docs:randcoords|RandCoords]]//: This picks a random X and Y coordinate somewhere on the map. <code>#Set the creeper to 1 on a random position
 RandCoords 1 SetCreeper</code> RandCoords 1 SetCreeper</code>
  
-One more useful functions before I move on to the next section, you can use TIME Delay to stop the execution of the script for the set amount of time. So if you want to add 20 creeper every 3 seconds, you could write:+One more useful functions before I move on to the next section, you can use TIME //[[crpl:docs:delay|Delay]]// to stop the execution of the script for the set amount of time. So if you want to add 20 creeper every 3 seconds, you could write:
  
-FIXME  
 <note warning> <note warning>
-Time intervals are not seconds, but frames. each second has 30 frames in it.+Time intervals are not seconds, but frames. Each second has 30 frames in it.
 </note> </note>
 +
 <code> <code>
 #first we add the creeper #first we add the creeper
Line 61: Line 80:
 90 Delay 90 Delay
 </code> </code>
-This of course gives much more possibilities check out this one:+ 
 +This of course gives many more possibilitiescheck out this one: 
 <code> <code>
 CurrentCoords -10 AddCreeper 30 Delay CurrentCoords -10 AddCreeper 30 Delay
Line 71: Line 92:
 ==== Using the tracelog and further introduction of a stack language ==== ==== Using the tracelog and further introduction of a stack language ====
  
 +There's a built-in function to show numbers that are currently on the stack. To enable the trace log, you must call the function //[[crpl:docs:showtracelog|ShowTraceLog]]//. After that you can use the function //[[crpl:docs:trace|Trace]]// to pop an item from the stack and show it on the trace log (removes the item from the stack!). Use //[[crpl:docs:trace2|Trace2]]//, //[[crpl:docs:trace3|Trace3]]//, //[[crpl:docs:trace4|Trace4]]//, //[[crpl:docs:trace5|Trace5]]// and //[[crpl:docs:tracestack|TraceStack]]// to pop and show 2, 3, 4, 5 or the whole stack on the trace log (//TraceStack// doesn't pop anything from the list).
 +In the examples below we use the following notation to show how an operation affects the stack: OPERATION (BEFORE -- AFTER). 
  
- (click to show/hide) +<hidden Show/Hide Examples> 
-There's a built-in function to show numbers that are currently on the stack. To enable the trace log, you must call the function ShowTraceLog. After that you can use the function Trace to pop an item from the stack and show it on the trace log (removes the item from the stack!). Use Trace2, Trace3, Trace4, Trace5 and TraceStack to pop and show 2, 3, 4, 5 or the whole stack on the trace log (TraceStack doesn't pop anything from the list). +  //[[crpl:docs:add|Add]]// (99 33 -- 132)<code>
-The notation of the stackWe use OPERATION (BEFORE -- AFTER), try to use this if possible. So here are a few examples: +
-<box>%%Add (99 33 -- 132)%%</box> +
-<code>+
 once  once 
-ShowTraceLog  +    ShowTraceLog  
-99 33 Add Trace +    99 33 Add Trace 
 endonce endonce
 </code> </code>
  
-<box> +  * //[[crpl:docs:sub|Sub]]// (12 7 -- 5)<code> 
-%% +once  
-Sub (12 7 -- 5) +    ShowTraceLog  
-Mod (15 6 -- 3) +    12 7 Sub Trace  
-%% +endonce 
-</box>+</code> 
 + 
 +  * //[[crpl:docs:mod|Mod]]// (15 6 -- 3)<code> 
 +once  
 +    ShowTraceLog  
 +    15 6 ModTrace  
 +endonce 
 +</code> 
 +</hidden> 
 + 
 +\\
 You can use multiple operators, but remember that they only pop the last added items from the stack. So where we normally would write (1+2)*(3+4), you now have to write You can use multiple operators, but remember that they only pop the last added items from the stack. So where we normally would write (1+2)*(3+4), you now have to write
 +
 +<code>1 2 add 3 4 add mul</code>
 +
 +The most important thing here is that you keep in mind that what you've put on the stack last, will be the first you take off (Last In First Out). So:
 +
 <code> <code>
-1 2 add 3 4 add mul</code> +8 5 4 add  
-The most important thing here is that you keep in mind that what you've put on the stack last, will be the first you take off (Last In First Out). So <code>8 5 4 add</code> will result in <code>8 9</code> because 5+4=9. If you add another 'add' the sum of 9 and 8 will be calculated since that are the last two items on the stack.+will result in 8 9 
 +because 5+4=9. 
 +</code> 
 + 
 +If you add another 'add' the sum of 9 and 8 will be calculated since that are the last two items on the stack. 
 If you want a challenge, read the following piece of code:  If you want a challenge, read the following piece of code: 
-<code>once   + 
-ShowTraceLog +<code> 
-8 9 5 sub 3 8 mul add 5 7 8 add  +once   
-add mod div +    ShowTraceLog 
-Trace +    8 9 5 sub 3 8 mul add 5 7 8 add  
 +    add mod div 
 +    Trace 
 endonce endonce
 </code> </code>
-Try to guess what's in the trace log+ 
 +Try to guess what's in the trace log
 <hidden Click here to see the answer> <hidden Click here to see the answer>
-<box left> +^ Operation ^ Stack before ^ Stack after ^ 
-( -- 8)\\ +|                      |           | 
-(-- 8 9)\\ +|         |            |8 9          | 
-(8 9 -- 8 9 5)\\ +|         |8 9           |8 9 5        | 
-sub (8 9 5 -- 8 4)\\ +|sub        |8 9 5         |8 4          | 
-(8 4 -- 8 4 3)\\ +|         |8 4           |8 4 3        | 
-(8 4 3 -- 8 4 3 8)\\ +|         |8 4 3         |8 4 3 8      | 
-mul (8 4 3 8 -- 8 4 24)\\ +|mul        |8 4 3 8       |8 4 24       | 
-add (8 4 24 -- 8 28)\\ +|add        |8 4 24        |8 28         | 
-(8 28 -- 8 28 5)\\ +|         |8 28          |8 28 5       | 
-(8 28 5 -- 8 28 5 7)\\ +|         |8 28 5        |8 28 5 7     | 
-(8 28 5 7 -- 8 28 5 7 8)\\ +|         |8 28 5 7      |8 28 5 7 8   | 
-add (8 28 5 7 8 -- 8 28 5 15)\\ +|add        |8 28 5 7 8    |8 28 5 15    | 
-add (8 28 5 15 -- 8 28 20)\\ +|add        |8 28 5 15     |8 28 20      | 
-mod (8 28 20 -- 8 8)\\ +|mod        |8 28 20       |8 8          | 
-div (8 8 -- 1)\\ +|div        |8 8           |           | 
-Trace (-- )\\ +|Trace      |            |             |
-</box>+
 It will show you the last item on the stack and that is 1. It will show you the last item on the stack and that is 1.
 </hidden> </hidden>
Line 153: Line 196:
 ==== Comparing numbers and conditions ==== ==== Comparing numbers and conditions ====
  
-Sometimes you want a condition to check if code must be executed or not. Like in many other languages, this is possible with the 'iffunction. Since we can't use brackets to show what piece of code must be skipped if the condition is not true, we must use 'endif'. 'true' is indicated by the number 1, and 'false' is indicated by the number 0. The 'iffunction pops one number from the stack and if it is not equal to 0, the code will be executed. If that number is 0, the program skips the code until the next endif. The words 'true' and 'falsesimply push a 1 (true) or (false) on the stack. There are a lot of functions to compare numbers. Most these functions pop two items from the stack and push 0 (false) or 1 (true) back on the stack. You can find the full list in other wiki pages. I'll highlight some:+Sometimes you want a condition to check if code should be executed or not. Like in many other languages, this is possible with the **//[[crpl:docs:if|if]]//** function. Since we can't use brackets to show what piece of code must be skipped if the condition is not true, we must use **//[[crpl:docs:endif|endif]]//**. The **//if//** function pops one number from the stack and if it is not equal to 0, the code will be executed. If that number //is// 0, the program skips the code until the next **//endif//** 
 + 
 +**//if//** may also be used in combination with **//[[crpl:docs:else|else]]//**. This function always has to be in between the **//if//** and **//endif//** functions (''if ... else ... endif''), and works as you'd expect from the name. When an **//if//** function evaluates to //true// the code in between **//else//** and **//endif//** will be skipped and the code will continue from **//endif//**. When an **//if//** function evaluates to //false// the code in between **//if//** and **//else//** will be skipped and the code will continue from **//else//**.  
 + 
 +Typing the words **//[[crpl:docs:true|true]]//** and **//[[crpl:docs:false|false]]//** simply push a 1 and 0 on the stack respectively. There are a lot of functions to compare numbers, most of these functions pop two items from the stack and push 0 (false) or 1 (true) back on the stack. You can find the full list in other wiki pages. I'll highlight some:
  
 ^ Function ^ Description ^ ^ Function ^ Description ^
-|and|true if last 2 items are true| +|[[crpl:docs:and|and]]|true if last 2 items are true| 
-|or|true if one of the last 2 items are true| +|[[crpl:docs:or|or]]|true if at least one of the last 2 items are true| 
-|xor|true if last 2 items are false+|[[crpl:docs:xor|xor]]|true if exactly one of the last 2 items are true
-|not|true if the last item is false| +|[[crpl:docs:not|not]]|true if the last item is false| 
-|gt|'greater than', pops 2 items from the stack, if the first is greater than the second, it results in true| +|[[crpl:docs:gt|gt]]|'greater than', pops 2 items from the stack, if the first is greater than the second, it results in true| 
-|gte|'greater than or equal'+|[[crpl:docs:gte|gte]]|'greater than or equal'
-|lt|'lower than'+|[[crpl:docs:lt|lt]]|'lower than'
-|lte|'lower than or equal'+|[[crpl:docs:lte|lte]]|'lower than or equal'
-|eq|'equal' true if the last 2 item have the same value| +|[[crpl:docs:eq|eq]]|'equal' true if the last 2 item have the same value| 
-|neq|'not equal', true if the last 2 items are not the same| +|[[crpl:docs:neq|neq]]|'not equal', true if the last 2 items are not the same| 
-|eq0|true if the last item on the stack is equal to 0| +|[[crpl:docs:eq0|eq0]]|true if the last item on the stack is equal to 0| 
-|neq0|true if the last item is not equal to 0|+|[[crpl:docs:neq0|neq0]]|true if the last item is not equal to 0|
  
  
Line 173: Line 220:
  
 ^ Arg1 ^ Arg2 ^ Operation ^ Result ^ ^ Arg1 ^ Arg2 ^ Operation ^ Result ^
- true   false  | or| True +false | false | or | False 
- true   true  and| True | +| true | false or | True | 
- true   true  | or| True | +| true | true | or | True | 
- false   false  xor| True | +true true and | True | 
- false   |not  True +| false | true and False 
- 5   5  eqTrue +false false and False 
- 4   7  neqTrue +false false xor false 
- 9   neq0True +false true xor true 
- 9   2  gtTrue +true true xor false 
- 6   7  lt| True +false | |not | True |
-|  2  |  2  | gte| True | +
-| false | false | and| False | +
-| true | false | xor| False |+
 | true | | not | False | | true | | not | False |
-false false or| False | +| | eq0 | False | 
-false true and| False | +| | neq0 | True | 
-| 9 | 9 | gt| False | +| 5 | 5 | eq | True | 
-| 6 | lteFalse +| 4 | 5 |eq | False 
-| 5 | 5 | lt| False | +| 4 | 7 | neq | True | 
-eqFalse +| 9 | 2 | gt | True 
-| | eq0| False | +| 9 | 9 | gt | False | 
-| 1 | 1 | sub neq0| False |+| 6 | lt True 
 +| 5 | 5 | lt | False | 
 +gte True 
 +lte | False | 
 +| 1 | 1 | sub neq0 | False | 
 Written as: Written as:
 <code>Arg1 Arg2 Operation</code> <code>Arg1 Arg2 Operation</code>
Line 201: Line 250:
 ==== More functions ==== ==== More functions ====
  
-Time to learn some more useful commands, I'll take the pre-made towers as example. A function is shown by the function name followed by bracket with the arguments in the order they must be put on the stack, seperated by a ','. Empty brackets mean that you don't need to give any arguments.+Time to learn some more useful commands, I'll take the pre-made towers as example. A function is shown by the function name followed by brackets with the arguments in the order in which they must be put on the stack, seperated by a ','. Empty brackets mean that you don't need to give any arguments.
  
- <hidden Emitters> +<hidden Emitters> 
-AddCreeper (x, y, amount)adds [amount] creeper to the cell on [x, y]. +^ Command (args) ^ Description ^ 
-SetCreeper (x, y, amount)sets creeper to [amount] height on cell [x, y]. +|[[crpl:docs:addcreeper|AddCreeper]] (x, y, amount)adds [amount] creeper to the cell on [x, y]. | 
-SetCreeperNoLower (x, y, amount)sets creeper to [amount] height on cell [x, y], but it won't remove creeper if there is more creeper than [amount]. +|[[crpl:docs:setcreeper|SetCreeper]] (x, y, amount)sets creeper to [amount] height on cell [x, y].| 
-GetCreeper (x, y)gets the creeper of cell [x, y] and puts it on the stack. +|[[crpl:docs:setcreepernolower|SetCreeperNoLower]] (x, y, amount)sets creeper to [amount] height on cell [x, y], but it won't remove creeper if there is more creeper than [amount].| 
-Code:+|[[crpl:docs:getcreeper|GetCreeper]] (x, y)gets the creeper of cell [x, y] and puts it on the stack.| 
 + 
 +<code>
 # this: # this:
 CurrentCoords 5 SetCreeperNoLower CurrentCoords 5 SetCreeperNoLower
 # does exactly the same as this: # does exactly the same as this:
-CurrentCoords GetCreeper 5 lt if +CurrentCoords GetCreeper  
-CurrentCoords 5 SetCreeper+5 lt if 
 +    CurrentCoords 5 SetCreeper
 endif endif
-#lt means lower than and if decides if the code until endif must be executed or not+# lt means lower than and if decides if the code until endif must be executed or not 
 +</code>
 </hidden> </hidden>
 +
 <hidden  Spore Towers> <hidden  Spore Towers>
-CreateSpore (x start, y start, x destination, y destination, health, payload)Creates a spore at cell [x start, y start] that moves towards cell [x destination, y destination]. The spore has a health of [health] ('normal' spores have 1) and if it lands it will drop [payload] creeper.+^ Command (args) ^ Description ^ 
 +|[[crpl:docs:createspore|CreateSpore]] (x start, y start, x destination, y destination, health, payload) |Creates a spore at cell [x start, y start] that moves towards cell [x destination, y destination]. The spore has a health of [health] ('normal' spores have 1) and if it lands it will drop [payload] creeper.
 <code> <code>
 # Send a spore to a random unit # Send a spore to a random unit
Line 225: Line 281:
 </code> </code>
 </hidden> </hidden>
 +
 <hidden  Timing> <hidden  Timing>
-SetTimer0 (time)set the timer of timer 0 to [time], the timers decrease with 1 each game frame. SetTimer1, SetTimer2 and SetTimer3 work the same. +^ Command (args) ^ Description ^ 
-GetTimer0 ()pushes the current value of timer 0 on the stack, same thing for GetTimer1, GetTimer2 and GetTimer3+|[[crpl:docs:settimer0|SetTimer0]] (time)set the timer of timer 0 to [time], the timers decrease with 1 each game frame. //SetTimer1////SetTimer2// and //SetTimer3// work the same.| 
 +|[[crpl:docs:gettimer0|GetTimer0]] ()pushes the current value of timer 0 on the stack, same thing for //GetTimer1////GetTimer2// and //GetTimer3//.| 
 <code> <code>
 # Send a spore to a random unit every second # Send a spore to a random unit every second
Line 239: Line 298:
 </code> </code>
 </hidden> </hidden>
-<hidden  Runner Nest> + 
-GetDigitalis (x, y)checks for Digitalis on cell [x, y] and pushes the health of the Digitalis back on the stack (0 - no Digitalis, 1 - full health). +<hidden Runner Nest> 
-CreateRunner (x, y, move, health, payload)creates a runner on position [x, y], the runner has [health] health and moves +^ Command (args) ^ Description ^ 
-pixels per frame. Once the runner is killed [payload] creeper is deposited. +|[[crpl:docs:getdigitalis|GetDigitalis]] (x, y)checks for Digitalis on cell [x, y] and pushes the health of the Digitalis back on the stack (0 - no Digitalis, 1 - full health).| 
-GetRunnerCount ()pushes the amount of runner currently on the map created by this CRPL-Core on the stack (exactly the same as GetGlobalRunnerCount, except this is only for runners created by this tower). +|[[crpl:docs:createrunner|CreateRunner]] (x, y, move, health, payload)creates a runner on position [x, y], the runner has [health] health and moves [move] pixels per frame. Once the runner is killed [payload] creeper is deposited.| 
-GetGlobalRunnerCount ()pushes the total amount of runners currently on the map on the stack +|[[crpl:docs:getrunnercount|GetRunnerCount]] ()pushes the amount of runner currently on the map created by this CRPL-Core on the stack (exactly the same as GetGlobalRunnerCount, except this is only for runners created by this tower).| 
-Code:+|[[crpl:docs:getglobalrunnercount|GetGlobalRunnerCount]] ()pushes the total amount of runners currently on the map on the stack.| 
 + 
 +<code>
 # Create a weak runner every frame if Digitalis is present and less than 20 runners are on the map # Create a weak runner every frame if Digitalis is present and less than 20 runners are on the map
 CurrentCoords GetDigitalis neq0 GetGlobalRunnerCount 20 lt and if CurrentCoords GetDigitalis neq0 GetGlobalRunnerCount 20 lt and if
-CurrentCoords 2 5 5 CreateRunner endif+    CurrentCoords 2 5 5 CreateRunner  
 +endif 
 +</code>
 </hidden> </hidden>
 +
 <hidden  More Digitalis stuff> <hidden  More Digitalis stuff>
-A CRPL-Core can 'activate' Digitalis, that means once the Digitalis is connected to a CRPLT, the will grow. You can change this in the settings for each core. +A CRPL-Core can 'activate' Digitalis, that means once the Digitalis is connected to a CRPLT, the Digitalis will grow. You can change this in the settings for each core. 
-GetDigitalisGrowth (x, y)checks for digitalis growth area at cell [x, y], if Digitalis can grow there, 1 is pushed back on the stack, if not, 0 is pushed back on the stack. +^ Command (args) ^ Description ^ 
-SetDigitalis (x, y, health)sets the health of the digitalis on cell [x, y] to [health], 0 removes the Digitalis while 1 sets the D to full health. +|[[crpl:docs:getdigitalisgrowth|GetDigitalisGrowth]] (x, y)checks for digitalis growth area at cell [x, y], if Digitalis can grow there, 1 is pushed back on the stack, if not, 0 is pushed back on the stack.| 
-SetDigitalisGrowth (x, y, present)sets if Digitalis can grow on cell [x, y]. Present must be 0 (no Digitalis growth area) or 1 (creates growth area).+|[[crpl:docs:setdigitalis|SetDigitalis]] (x, y, health)sets the health of the digitalis on cell [x, y] to [health], 0 removes the Digitalis while 1 sets the D to full health.| 
 +|[[crpl:docs:setdigitalisgrowth|SetDigitalisGrowth]] (x, y, present)sets if Digitalis can grow on cell [x, y]. Present must be 0 (no Digitalis growth area) or 1 (creates growth area).
 <code> <code>
-#this code is mostly used in moving towers +# this code is mostly used in moving towers 
-#leave a trail of Digitalis growth area and set the digitalis to full health+# leave a trail of Digitalis growth area and set the digitalis to full health
 CurrentCoords true SetDigitalisGrowth CurrentCoords true SetDigitalisGrowth
 CurrentCoords 1 SetDigitalis CurrentCoords 1 SetDigitalis
 </code> </code>
 </hidden> </hidden>
 +
 \\ \\
 ==== Variables and Loops ==== ==== Variables and Loops ====
- 
  
 A variable: a way to store numbers without using the stack once stored. A variable: a way to store numbers without using the stack once stored.
-You can use ->VARNAME to pop the last item from the stack and store it as variable and you can use <-VARNAME to push the value of the variable on the stack (doesn't remove the variable). Please not that instead of VARNAME you can use any word. Examples: +You can use **->VARNAME** to pop the last item from the stack and store it as variable and you can use **<-VARNAME** to push the value of the variable on the stack (doesn't remove the variable). Please note that instead of **VARNAME** you can use any word. Examples: 
-Code:<code>+ 
 +<code>
 16 ->mynumb 16 ->mynumb
 2 ->n2 2 ->n2
 4 ->endnumb 4 ->endnumb
 while <-mynumb <-endnumb neq repeat while <-mynumb <-endnumb neq repeat
-<-mynumb <-n2 div endwhile</code>+<-mynumb <-n2 div endwhile 
 +</code>
  
-As you may have noticed, I used 'while''repeatand 'endwhile'. These functions form a 'loop'. There are different loops you can create in CRPL. Lets start with a 'do' loop: +As you may have noticed, I used **//[[crpl:docs:while|while]]//****//[[crpl:docs:repeat|repeat]]//** and **//[[crpl:docs:endwhile|endwhile]]//**. These functions form a loop. There are different loops you can create in CRPL. 
-A do loop has 2 functions and has the following form: +
-do (limit, index) loop +
-'do' pops 2 items from the stack, if the index is bigger or equal to the limit, the execution skips to 'loop', else the loop runs with 'index', when 'loop' is read, the execution returns to 'do', the index is raised with 1 and everything starts again. +
-Code:<code> +
-#Add 5 creeper to 5 random locations every 5 seconds +
-5 ->times +
-5 ->creeper +
-150 ->wait +
-<-times 0 do +
-  RandCoords <-creeper AddCreeper +
-loop +
-<-wait Delay</code>+
  
-There'also a while loop. A while loop has 3 functions and has the following form: +Let'start with **//while//** loop, like in the example. A while loop has 3 functions and has the following form: 
-while repeat (execute) endwhile + 
-When while is read, the code between while and repeat is executed (and should push true or false on the stack). If true is read, the code will be executed until endwhile and execution return to while. If false is read, the code between repeat and endwhile is skipped and the execution continues at endwhile. +''while repeat (condition... endwhile'' 
-Code:<code> + 
-#Add 5 creeper to 5 random locations every 5 seconds+When **//[[crpl:docs:while|while]]//** is read, the code between **//while//** and **//repeat//** is executed (and should push //true// or //false// on the stack). If //true// is read, the code will be executed until **//endwhile//** and execution return to **//while//**. If //false// is read, the code between repeat and **//endwhile//** is skipped and the execution continues at **//endwhile//**
 + 
 +<code> 
 +# Add 5 creeper to 5 random locations every 5 seconds
 5 ->times 5 ->times
 5 ->creeper 5 ->creeper
Line 302: Line 361:
 endwhile endwhile
 <-wait Delay <-wait Delay
-#lte means lower than or equal</code>+# lte means lower than or equal 
 +</code>
  
-In a do or while loop, you can use 'breakto stop the loop immediatly and continue at loop or endwhile +There's also a 'do' loop. A do loop has 2 functions and has the following form: 
-Code:<code> + 
-#Add 5 creeper to 5 random locations every 5 seconds +''do (limit, index) ... loop'' 
-5 ->times + 
-5 ->creeper +**//[[crpl:docs:do|do]]//** pops 2 items from the stack, if the //index// is bigger or equal to the //limit//, the execution skips to **//loop//**, else the loop will run. When **//loop//** is read, the execution returns to **//do//**, the index is raised with 1 and everything starts again. Simply put: the code between **//do//** and **//loop//** will repeat as many times as (''limit-index''), it is skipped if that value is 0 or lower. 
-150 ->wait + 
-0 ->numb +Within a **//do//** loop the value of //index// can be accessed by typing **//[[crpl:docs:i|I]]//**. This will push the value of //index// onto the stack.  
-while true repeat + 
-  RandCoords <-creeper AddCreeper <-numb add <-numb 4 gt if break endif ->numb+<code> 
 +# Show the trace log, clear it, then show the numbers 0 through 4 in the log  
 +ShowTraceLog  
 +ClearTraceLog  
 +5 0 do  
 +   I Trace  
 +loop 
 +</code> 
 + 
 +In a **//do//** or **//while//** loop, you can use **//[[crpl:docs:break|break]]//** to stop the loop immediatly and continue at loop or endwhile 
 + 
 +<code> 
 +deposit creeper in random spots in a range of 20 round the Core 
 +# 'while true' = repeat endlessly 
 +while true repeat  
 +    CurrentCoords 20 RandCoordsInRange 60 AddCreeper  
 +    30 Delay 
 +    CurrentCoords GetCreeper 1 gt if break endif  
 +    # the only way to break out of the while-loop 
 +    # is if the core has creeper under it that is 1 high or higher (1 gt = greater than 1)
 endwhile endwhile
-<-wait Delay +</code>
-#'true' pushes 1 (true) on the stack, this means repeat allways reads true +
-#and the loop will keep going until break is read +
-#this can easily make your game crash if you don't add the break command! +
-#gt means greater than</code>+
  
  
 ====Defining your own functions==== ====Defining your own functions====
-If you want to use the same piece of code multiple times or want a better overview, you can use functions. In the main code, use @FUNCTION to call the function. At the end of the code, use :FUNCTION to define the function. The function is the piece of code between :FUNCTION and the end of the script or another function. If you want to give arguments or return a value, use the stack. An example to help you:+If you want to use the same piece of code multiple times or want a better overview, you can use functions. In the main code, use **[[crpl:docs:call|@FUNCTION]]** to call the function. At the end of the code, use **[[crpl:docs:func|:FUNCTION]]** to define the function. The function is the piece of code between **:FUNCTION** and the end of the script or another function. If you want to give arguments or return a value, use the stack. An example to help you:
 <code> <code>
-@getnumb #pass the execution to :getnumb +@getnumb         # pass the execution to :getnumb 
-@emit #pass the execution to :emit +@emit            # pass the execution to :emit 
-#------------------ (end of main bodyusing a line helps visualizing it) + 
-:emit #once @emit is read, execution continues here +# -------------- end of main body (using a line helps to visualize it) 
-->numb #pop value from the stack and store it+ 
 +:emit            # once @emit is read, execution continues here 
 +->numb           store variable and do something with it
 CurrentCoords <-numb AddCreeper CurrentCoords <-numb AddCreeper
-:getnumb #start a new function here, the :emit function stops here + 
-5 #push 5 on the stack +:getnumb         define a new function, the :emit function stops here and execution returns to the main body 
-#end of the code, the :getnumb function stops here+               # push 5 on the stack 
 +                 # end of the code, the :getnumb function stops here and execution returns to the main body
 </code> </code>
 +
 And how it looks without comments: And how it looks without comments:
 +
 <code> <code>
 @getnumb @emit @getnumb @emit
Line 342: Line 422:
 :getnumb 5 :getnumb 5
 </code> </code>
 +
 +
 +====Attributes====
 +
 +CRPL allows you to modify the attributes of units, which is arguably the most powerful feature of CRPL. Unit attributes may look very complicated but they're actually pretty simple: You give the unit UID (unique identifier), the attribute you want to get/set and then call the GetUnitAttribute or SetUnitAttribute function.
 +^ Function ^ Description ^ 
 +|[[crpl:docs:self|Self]] () |Pushes the UID of the current unit on the stack|
 +|[[crpl:docs:getunitattribute|GetUnitAttribute]] (unit UID, attribute) |Finds the unit with the given UID, gets the given attribute and pushes  it onto the stack|
 +|[[crpl:docs:setunitattribute|SetUnitAttribute]] (unit UID, attribute, value) |Finds the unit with the given UID and sets the given attribute to the given value|
 +
 +Each attribute has a number. The 'attribute' part you pass to the above functions has to contain the number of the desired attribute. Though you //could// type 17 in order to select a units Ammo-attribute, all attribute values are stored as constant variables that you can access them at any point in your code. This means you don't have to learn which attribute has has which value, you can simply type //CONST_AMMO// and that will add 17 (the attribute number of Ammo) to the stack.
 +
 +Here's a short list with //some// of the attributes you can use (The full list can be found on the [[crpl:docs:getunitattribute#unit_constants|GetUnitAttribute]] page):
 +
 +^ Attribute ^ Description ^ Value ^
 +| CONST_COORDX | The x coordinate of the unit. |0|
 +| CONST_COORDY | The y coordinate of the unit. |1|
 +| CONST_AMMO | The unit's ammo. Floating point value. |17|
 +| CONST_COUNTSFORVICTORY | Whether the CrplTower must be destroyed before map victory on annihilation game modes. Only works for CRPLTowers. |27|
 +| CONST_CREATEPZ | Whether the CrplTower creates a power zone when destroyed. Only works for CRPLTowers. |21|
 +| CONST_HEALTH | The unit's health. Floating point value. |15|
 +| CONST_MAXAMMO | The unit's max ammo. Floating point value. |18|
 +| CONST_MAXAMMOAC |The unit's max AntiCreeper ammo. Floating point value. |10|
 +| CONST_MAXHEALTH | The unit's max health. Floating point value. |16|
 +| CONST_NULLIFIERDAMAGES | Whether the CrplTower can be targeted and damaged by Nullifiers. Only works for CRPLTowers. |18|
 +
 +Remember that simply typing an attributes name doesn't return a unit's value of that attribute. In order to get or set the the attributes of a unit you have to use a their UID and the //GetUnitAttribute// and //SetUnitAttribute// functions.
 +
 +If this is still a bit hard to follow, here's an example:
 +
 +<code>
 +:randUnitBuildMode          # This function puts a random unit back into build mode
 +# GetUnitsInRange first pushes the UIDs of all units in range, then the amount of units
 +# If there are no units in range it pushes 0 on the stack
 +CurrentCoords 10000 GetUnitsInRange ->unitCount
 +
 +0 <-unitCount RandInt ->unitNr        # create a nr where 0 <= unitNr < unitCount
 +
 +<-unitCount 0 do            # for each unit
 +    I <-unitNr eq if        # if unit is the chosen one
 +        CONST_ISBUILDING true SetUnitAttribute  
 +    else pop endif
 +loop
 +</code>
 +
  
 ====Making your script more useful==== ====Making your script more useful====
  
-If you use $VARNAME:DEFAULT at the start of your code you can define the variable when adding scripts to units in-game. In other words, you can set these variables in-game and that can be different for other cores.+If you use **[[crpl:docs:define|$VARNAME:DEFAULT]]** at the start of your code you can define the variable when adding scripts to units in-game. In other words, you can set these variables in-game so they are different for other cores running the same script. 
 <code> <code>
 $amtToEmit:10 $amtToEmit:10
Line 352: Line 478:
 <-interval Delay <-interval Delay
 </code> </code>
-Take a look at '$interval:15'. When you attach the script to a core in-game you can choose a value for the variable 'interval'. If you don't input a number in-game, 15 is used. + 
-This is a very powerful mechanism to use the same script over different cores or if you want to give the script to other map makers.+Take a look at ''$amtToEmit:10''. When you attach the script to a core in-game you can choose a value for the variable //amtToEmit//. If you don't input a number in-game, 10 is used. This is a very powerful mechanism to use the same script over different cores or if you want to give the script to other map makers
 +You should use it instead of 
 +''10 ->amtToEmit'' 
 +where possible. Also try to use this instead of fixed values (''10 AddCreeper''), which should help you alot if you want to make small balance changes later. Ofcourse a good coder can change values directly from within the script, but that'll get harder as your scripts grow larger.
  
  
 ==== Useful links==== ==== Useful links====
  
-A page filled with functions and examples (old)+CRPL reference with all available functions: 
-http://knucklecracker.com/creeperworld3/CRPL/docs/crpldocs.html+[[:crpl:CrplReference|CRPL Reference]] 
 + 
 +Guide to [[examine map resources]] to extract scripts and custom images from another map.
  
-The KC wiki with CRPL overview and reference: +If you need more practice or help to common problem, you might be able to find it at the [[:crpl:CrplTutorial:Interactive|CRPL Interactive Tutorials]]
-http://knucklecracker.com/wiki/doku.php?id=cw3:creeper_world_3+
  
-The same more frequently updated guide on the KC forums:+This wiki page is largely based off of a guide on the KC forums, some changes have been made however. The original guide on the KC forums:
 http://knucklecracker.com/forums/index.php?topic=12253.0 http://knucklecracker.com/forums/index.php?topic=12253.0
crpl/crpltutorial.1378739219.txt.gz · Last modified: 2014/10/01 15:02 (external edit)