This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
crpl:crpltutorial:code_examples [2013/09/22 13:02] – Added Treasure Chest example Grauniad | crpl:crpltutorial:code_examples [2020/06/08 16:35] – html comment removed builder17 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~DISCUSSION~~ | + | |
- | < | + | |
< | < | ||
====== Code Examples ====== | ====== Code Examples ====== | ||
+ | |||
Code examples contributed by map makers and coders alike. Feel free to submit your own - it will be much appreciated. | Code examples contributed by map makers and coders alike. Feel free to submit your own - it will be much appreciated. | ||
- | ===== Manage Treasure | + | You can also view the scripts in any map: make a copy of the map you want to inspect, rename the copy " |
+ | |||
+ | <wrap hi> | ||
+ | ===== Manage Treasure | ||
< | < | ||
Line 443: | Line 446: | ||
</ | </ | ||
< | < | ||
+ | |||
+ | ===== Wrap Creeper around the edges of the map ===== | ||
+ | The following code will allow creeper to flow around the edges of the map. | ||
+ | That makes the map feel like a globe. Best use on terrain, that is seamless. | ||
+ | |||
+ | Add this code to a tower. As long as this tower exists, the creeper will leave the map at the left and enter at right, and vise versa. | ||
+ | |||
+ | The parameters can be used to activate or deactivate left-right or top-bottom wrap. | ||
+ | |||
+ | |||
+ | |||
+ | < | ||
+ | # Author: knattertc | ||
+ | # ------------------------------------------ | ||
+ | $wrapLeftRight: | ||
+ | $wrapTopBottom: | ||
+ | |||
+ | # Init Vars | ||
+ | MapWidth 1 sub ->right | ||
+ | 0 ->left | ||
+ | 0 ->top | ||
+ | MapHeight 1 sub ->bottom | ||
+ | |||
+ | # Wrap Left to Right | ||
+ | < | ||
+ | < | ||
+ | # Get Creeper on left and right side - add them up - devide by 2 | ||
+ | <-left I GetCreeper <-right I GetCreeper add 2 div -> | ||
+ | <-left I <-mid SetCreeper | ||
+ | < | ||
+ | loop | ||
+ | endif | ||
+ | |||
+ | # Wrap Top to Bottom | ||
+ | < | ||
+ | <-left <-right do | ||
+ | # Get Creeper on top and bottom - add them up - devide by 2 | ||
+ | I <-top GetCreeper I <-bottom GetCreeper add 2 div -> | ||
+ | I < | ||
+ | I <-top <-mid SetCreeper | ||
+ | loop | ||
+ | endif | ||
+ | |||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====Create an emitter that increases its output rate over time======= | ||
+ | This script is an example of how to create an emitter that increases its output rate from a minimum value (BaseRate) to a maximum value (MaxRate) over a specified time (RampTime, in seconds) with a specified precision (NumIncrements). All variables are changeable by the user without editing the script. | ||
+ | |||
+ | This script uses Timer0 to track the rate of increase, so the tower is capable of doing other things while this is running (like moving). | ||
+ | |||
+ | The default values create an emitter that deploys creeper every 0.5 seconds starting at a rate of 25 creeper per drop and increasing to a rate of 150 creeper per drop over a period of ten minutes using a precision of 100 increments. | ||
+ | |||
+ | < | ||
+ | # Author: albrittbrat | ||
+ | #Program to ramp creeper deployment rate over time | ||
+ | #Time is in seconds | ||
+ | # | ||
+ | $BaseRate: | ||
+ | $MaxRate: | ||
+ | $RampTime: | ||
+ | $NumIncrements: | ||
+ | $DeployRate: | ||
+ | once | ||
+ | < | ||
+ | 0 SetTimer0 | ||
+ | < | ||
+ | < | ||
+ | endonce | ||
+ | #If the queue is empty, queue next move | ||
+ | #Drop AC based on input parameters | ||
+ | GetTimer0 eq0 if | ||
+ | @DetermineCAmount | ||
+ | endif | ||
+ | CurrentCoords <-CRate SetCreeper | ||
+ | < | ||
+ | #Function to determine amount of creeper to drop | ||
+ | #Range goes from BaseRate to MaxRate | ||
+ | : | ||
+ | <-CRate < | ||
+ | <-CRate 1 add ->CRate | ||
+ | < | ||
+ | endif | ||
+ | #End DetermineCAmount Function | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====Emitter that spawns more emitters on death======= | ||
+ | This script makes a " | ||
+ | |||
+ | < | ||
+ | # A basic emitter at first. | ||
+ | # Creates an emitter which spawns new emitters in random locations within a defined range when it dies, which themselves also spawn new emitters. | ||
+ | # Note that the decay rates are multiplied, so in theory you could make them bigger than 1 and have the rate/ | ||
+ | # Note also, the spawn code checks to make sure it's not spawning on void. You can comment out that line if you want it to spawn in void, but be careful to not spawn outside of the map! | ||
+ | # ------------------------------------------ | ||
+ | $rate:15 | ||
+ | $amount:100 | ||
+ | $rateDecay: | ||
+ | $amountDecay: | ||
+ | $newHeads:3 | ||
+ | $headLossPerDeath: | ||
+ | $headRange: | ||
+ | $headRangeDecay: | ||
+ | |||
+ | CurrentCoords <-amount AddCreeper | ||
+ | <-rate Delay | ||
+ | |||
+ | :destroyed | ||
+ | 0 ->count | ||
+ | while <-count lt(< | ||
+ | CurrentCoords < | ||
+ | if(< | ||
+ | " | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | endif | ||
+ | endwhile</ | ||
+ | < | ||
+ | |||
+ | =====Make circular terrain======= | ||
+ | With this modified version of Mistet' | ||
+ | This script is applied to a CRPLcore. The CRPLcore generates a circle of terrain around itself of the chosen type and size. | ||
+ | The external radius is most important: this determines the size of the circle and is measured in cells on the map. Remember it is a radius, not a diameter. | ||
+ | The terrain variable is also important. This determines the height of the terrain that it will change to. | ||
+ | The internal radius variable allows you to create a smaller circle within the larger. This smaller circle will ignore terrain changes and is useful for creating rings. If this variable is larger than the external radius, the script will automatically swap the two so it works anyway. | ||
+ | |||
+ | < | ||
+ | # circular_terrain.crpl | ||
+ | # A modified version of MakeCircle.crpl from Mistet | ||
+ | # Made by VirgilW | ||
+ | # Modified by Clean0nion | ||
+ | # ------------------------------------------ | ||
+ | # HOW TO USE THIS SCRIPT | ||
+ | # 1. Create a square map with odd dimensions. | ||
+ | # e.g. 255 x 255 | ||
+ | # 51 x 51 | ||
+ | # | ||
+ | # 2. Add this script to the map. | ||
+ | # Go to Edit Map -> Units -> Scripts | ||
+ | # Type " | ||
+ | # Press Create. | ||
+ | # Find it in the list and click Edit. | ||
+ | # Paste in this code. | ||
+ | # | ||
+ | # 3. Place a CRPLcore anywhere in the map. | ||
+ | # Edit Map -> Units -> CRPLcore | ||
+ | # Then put your mouse anywhere on the map and press B. | ||
+ | # | ||
+ | # 4. Change the variables on the CRPLcore. | ||
+ | # Double click the CRPLcore. | ||
+ | # Open the " | ||
+ | # click " | ||
+ | # In the new list, double click " | ||
+ | # The variables will appear. | ||
+ | # | ||
+ | # " | ||
+ | # " | ||
+ | # " | ||
+ | # | ||
+ | # For example, " | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # 5. Once the variables have been set, save the map. | ||
+ | # | ||
+ | # 6. Finally, run the script. | ||
+ | # Click the red X to leave the level editor and return | ||
+ | # to the game. | ||
+ | # Unpause the game. | ||
+ | # Wait until the circle has finished generating then | ||
+ | # pause again. | ||
+ | # Your circle is done. | ||
+ | # | ||
+ | # 7. If you want to, run the script again with different | ||
+ | # | ||
+ | # | ||
+ | # !!! If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | # If " | ||
+ | |||
+ | |||
+ | |||
+ | $terrain: | ||
+ | $rad_external: | ||
+ | $rad_internal: | ||
+ | |||
+ | once | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | else | ||
+ | < | ||
+ | < | ||
+ | endif | ||
+ | endonce | ||
+ | |||
+ | |||
+ | if (<-row lt (MapHeight)) | ||
+ | | ||
+ | < | ||
+ | endif | ||
+ | |||
+ | |||
+ | :DoRow | ||
+ | | ||
+ | Distance(CurrentCoords I <-row) -> | ||
+ | if (< | ||
+ | if (< | ||
+ | 0.1 < | ||
+ | I <-row 2 SetDigitalisGrowth | ||
+ | else | ||
+ | 0.2 < | ||
+ | I <-row 0 SetDigitalisGrowth | ||
+ | else | ||
+ | 0.3 < | ||
+ | I <-row 1 SetWall | ||
+ | else | ||
+ | 0.4 < | ||
+ | I <-row 0 SetWall | ||
+ | else | ||
+ | 0.5 < | ||
+ | I <-row GetTerrain SetTerrainOverride | ||
+ | else | ||
+ | < | ||
+ | I <-row < | ||
+ | else | ||
+ | 2.1 < | ||
+ | I <-row GetTerrain 2 mul SetCreeper | ||
+ | else | ||
+ | I <-row < | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | endif | ||
+ | loop | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====Flip-Emitters===== | ||
+ | Use suffitient quantities of AC to turn an emitter into an AC emitter. | ||
+ | |||
+ | < | ||
+ | # Created on: 1/3/2014 1:53:35 PM | ||
+ | # ------------------------------------------ | ||
+ | $amtCreeper: | ||
+ | $amtAnti: | ||
+ | $interval: | ||
+ | $startState: | ||
+ | # | ||
+ | Once | ||
+ | < | ||
+ | " | ||
+ | " -> | ||
+ | endonce | ||
+ | #Checks if there is creeper, if there is, sets the converter to make creeper. | ||
+ | CurrentCoords GetCreeper 0 gt if | ||
+ | 1 ->state | ||
+ | SetPopUpText(" | ||
+ | SetUnitAttribute(Self CONST_COUNTSFORVICTORY TRUE) | ||
+ | endif | ||
+ | #Checks if there is anticreeper, | ||
+ | CurrentCoords GetCreeper 0 lt if | ||
+ | 0 ->state | ||
+ | #the linebreak is on purpose, and concat because the amt isn't always the same. | ||
+ | SetPopUpText(" | ||
+ | SetUnitAttribute(Self CONST_COUNTSFORVICTORY FALSE) | ||
+ | endif | ||
+ | #Checks the state, remember if there was nothing under it, it will use it's last state! | ||
+ | <-state if | ||
+ | CurrentCoords < | ||
+ | else | ||
+ | CurrentCoords < | ||
+ | endif | ||
+ | < | ||
+ | |||
+ | =====Frames to minutes===== | ||
+ | Converts an amount of frames to minutes in M:SS format. Frames stored in variable " | ||
+ | 1800 displays 1:00; | ||
+ | 60 displays 0:02; | ||
+ | 2400 displays 1:20. | ||
+ | |||
+ | < | ||
+ | dup 10 lt if | ||
+ | " | ||
+ | endif | ||
+ | StackSize 1 do | ||
+ | concat | ||
+ | loop | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====Survival Pod===== | ||
+ | Attach to a core to create a survival pod. Survival pods must be rescued. | ||
+ | |||
+ | < | ||
+ | if (not(< | ||
+ | FailMission | ||
+ | Destroy(Self 2) | ||
+ | endif | ||
+ | if ( not(< | ||
+ | AscendToOrbit | ||
+ | true -> | ||
+ | PlaySound(" | ||
+ | SetPopupText("" | ||
+ | ShowGameEventTag(" | ||
+ | endif | ||
+ | |||
+ | if (< | ||
+ | GetUnitTransformPosition ->z ->y ->x | ||
+ | # | ||
+ | CreateEffect(8 <-x <-y -1 2 2 0.04) | ||
+ | else | ||
+ | SetPopupText(GetUnitAttribute(Self CONST_HEALTH) "/" | ||
+ | SetPopupTextX(0) | ||
+ | endif | ||
+ | |||
+ | :Awake | ||
+ | if (GetUpdateCount eq (0)) | ||
+ | SetUnitAttribute(Self CONST_ISBUILDING TRUE) | ||
+ | SetUnitAttribute(Self CONST_BUILDCOST 30) | ||
+ | SetUnitAttribute(Self CONST_HEALTH 0) | ||
+ | SetPopupTextAlwaysVisible(true) | ||
+ | SetPopupTextY(-12) | ||
+ | endif | ||
+ | |||
+ | :Destroyed | ||
+ | CreateUnit(" | ||
+ | AddScriptToUnit(< | ||
+ | if (< | ||
+ | SetScriptVar(< | ||
+ | else | ||
+ | SetScriptVar(< | ||
+ | SetScriptVar(< | ||
+ | SetScriptVar(< | ||
+ | SetScriptVar(< | ||
+ | |||
+ | CreateUnit(" | ||
+ | AddScriptToUnit(< | ||
+ | |||
+ | ShowGameEventTag(" | ||
+ | |||
+ | endif | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====PodMessage===== | ||
+ | Shows a message on the map that goes away. Used by the SurvivalPod script above. | ||
+ | |||
+ | < | ||
+ | $solidTime: | ||
+ | $message:" | ||
+ | $red:255 | ||
+ | $green:255 | ||
+ | $blue:255 | ||
+ | |||
+ | once | ||
+ | < | ||
+ | SetText(< | ||
+ | SetTextColor(< | ||
+ | endonce | ||
+ | |||
+ | if (GetUpdateCount gte(< | ||
+ | Destroy(Self 0) | ||
+ | endif | ||
+ | |||
+ | if (GetUpdateCount gt(1)) | ||
+ | if (GetUpdateCount gte(< | ||
+ | < | ||
+ | else | ||
+ | 255 ->alpha | ||
+ | endif | ||
+ | |||
+ | SetTextColor(< | ||
+ | SetImageColor(Self " | ||
+ | endif | ||
+ | |||
+ | :Awake | ||
+ | SetUnitAttribute(Self CONST_CREATEPZ FALSE) | ||
+ | SetUnitAttribute(Self CONST_TAKEMAPSPACE FALSE) | ||
+ | SetUnitAttribute(Self CONST_SUPPORTSDIGITALIS FALSE) | ||
+ | SetUnitAttribute(Self CONST_NULLIFIERDAMAGES FALSE) | ||
+ | SetUnitAttribute(Self CONST_COUNTSFORVICTORY FALSE) | ||
+ | |||
+ | SetScreenMode(TRUE) | ||
+ | SetTextY(-5) | ||
+ | SetTextSize(2) | ||
+ | SetScreenPixelCoordX(ScreenWidth div(2)) | ||
+ | SetScreenPixelCoordY(135) | ||
+ | |||
+ | SetImage(Self " | ||
+ | SetImageScaleX(Self " | ||
+ | SetImageScaleY(Self " | ||
+ | SetImageColor(Self " | ||
+ | |||
+ | PlaySound(" | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | =====GetClosestUnitToCoords===== | ||
+ | A customizable method to grab a nearest unit of any kind. Specify what types of units you want to grab, at what position, and with what radius. Strings are used for clarity to grab units that you need, you can delete or comment out units you wont be using once you copy the code. The code contains 3 methods that are commented for use on the battle-field. Contains two additional convenience methods. | ||
+ | < | ||
+ | #Warning: use this convenience function sparingly, tens of CRPL Cores | ||
+ | #running this all at once every frame can slow the game down. | ||
+ | |||
+ | #Notation: x y f1 - i1 | ||
+ | #eg. CurrentCoords 10 @GetClosestUnitToCoords ->unit | ||
+ | #If all units are outside of the maxDistance or none found returns integer value ' | ||
+ | : | ||
+ | asfloat -> | ||
+ | asfloat ->gcu_y | ||
+ | asfloat ->gcu_x | ||
+ | StackSize -> | ||
+ | |||
+ | # Reference: https:// | ||
+ | # Place Unit Types that you /WANT/ to look for here. | ||
+ | # Simply comment '#' | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | #" | ||
+ | " | ||
+ | " | ||
+ | # ===== | ||
+ | |||
+ | #Finding the boundary stack size (how many elements were added to stack) | ||
+ | #and then forming a list from the units we want. | ||
+ | StackSize -> | ||
+ | CreateList -> | ||
+ | < | ||
+ | -> | ||
+ | < | ||
+ | loop | ||
+ | |||
+ | # | ||
+ | -1 -> | ||
+ | 999999 -> | ||
+ | |||
+ | #retrieve /all/ nearby units | ||
+ | |||
+ | # x y distance boolean(true-square, | ||
+ | # unit checks have been ordered in perceived processing complexity | ||
+ | <-gcu_x <-gcu_y < | ||
+ | < | ||
+ | < | ||
+ | -> | ||
+ | < | ||
+ | #get the distance | ||
+ | < | ||
+ | #is that distance closer? | ||
+ | < | ||
+ | #do we care about this unit type? | ||
+ | < | ||
+ | #set our new baseline distance and unit | ||
+ | < | ||
+ | < | ||
+ | endif | ||
+ | endif | ||
+ | loop | ||
+ | endif | ||
+ | |||
+ | < | ||
+ | |||
+ | #Function :UnitCoords | ||
+ | #Simple convenience method. Takes a unit and returns the coordinates. | ||
+ | #' | ||
+ | #Notation: n1 - x y | ||
+ | #eg. < | ||
+ | :UnitCoords | ||
+ | ->u | ||
+ | <-u CONST_COORDX GetUnitAttribute | ||
+ | <-u CONST_COORDY GetUnitAttribute | ||
+ | |||
+ | #Function : | ||
+ | #This function an object, then a list and returns a boolean value. | ||
+ | #Notation: L1 n1 - b1 | ||
+ | #eg. <-myList < | ||
+ | : | ||
+ | -> | ||
+ | -> | ||
+ | false -> | ||
+ | |||
+ | < | ||
+ | < | ||
+ | true -> | ||
+ | break | ||
+ | endif | ||
+ | loop | ||
+ | |||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | |||
+ | |||
+ | ===CRPL generating CRPL=== | ||
+ | This CRPL will generate CRPL with custom program (if it is in the map scripts) | ||
+ | < | ||
+ | CreateUnit(" | ||
+ | AddScriptToUnit(< | ||
+ | </ | ||
+ | <sub> Stolen from Teknotiss(who took it from virgil), where pyramids make golems. I simply changed golem to unit. Added by cpaca, known as Karma' |