User Tools

Site Tools



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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
crpl:crpltutorial:code_examples [2013/10/25 19:46]
ThirdParty [Code Examples] Added instructions for viewing built-in scripts.
crpl:crpltutorial:code_examples [2020/06/08 16:35] (current)
builder17 html comment removed
Line 1: Line 1:
-<​html><​!--Comments visible to editors only can be placed in HTML comments --></​html>​+
 <​-[[cw3:​creeper_world_3|CW3 Home]]<- [[crpl:​start|CRPL Home]] <​-[[cw3:​creeper_world_3|CW3 Home]]<- [[crpl:​start|CRPL Home]]
 ====== 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.
 You can also view the scripts in any map: make a copy of the map you want to inspect, rename the copy "​save.cw3",​ create a new project, and then replace the project'​s "​save.cw3"​ file. You can also view the scripts in any map: make a copy of the map you want to inspect, rename the copy "​save.cw3",​ create a new project, and then replace the project'​s "​save.cw3"​ file.
 +<wrap hi>​Telanir contributed an [[EasyCRPL|entire section on EasyCRPL]]. </​wrap>​
 ===== Manage Treasure Chests in Credits Mission ===== ===== Manage Treasure Chests in Credits Mission =====
Line 572: Line 574:
  endwhile</​code>​  endwhile</​code>​
 <​sub>​Contributed by Xindaris</​sub>​ <​sub>​Contributed by Xindaris</​sub>​
 +=====Make circular terrain=======
 +With this modified version of Mistet'​s script, you can create circular terrain. Choose the terrain level (or digitalis, or wall, or terrain override), choose the radius, and even choose an internal radius! ​
 +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
 +# ------------------------------------------
 +#   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 "​circular_terrain"​ into the bar.
 +#      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 "​Attached Scripts"​ bar, find this code, and
 +#        click "​Add"​. ​  
 +#      In the new list, double click "​circular_terrain.crpl"​
 +#      The variables will appear.
 +# "​terrain"​ is the height of the terrain you want to create.
 +# "​rad_external"​ is the external radius of the circle.
 +# "​rad_internal"​ is the internal radius of the circle.
 +# For example, "​terrain"​=5;​ "​rad_external"​=20;​ "​rad_internal"​=0 will create a circle of land that is 40 cells across, and is oh terrain level 5. There will be no gap in the middle of the circle.
 +#   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 ​
 +#     ​variables.
 +# !!! If "​terrain"​ is a value from 1 - 10, that level terrain will be placed.
 +# If "​terrain"​ is 0 or below, void will be placed.
 +# If "​terrain"​ is 11 or above, that level terrain override will be placed.
 +# If "​terrain"​ is "​0.1",​ digitalis will be placed.
 +# If "​terrain"​ is "​0.2",​ digitalis will be removed.
 +# If "​terrain"​ is "​0.3",​ wall will be placed.
 +# If "​terrain"​ is "​0.4",​ wall will be removed.
 +# If "​terrain"​ is "​0.5",​ terrain override will be removed.
 +# If "​terrain"​ is "​2.1",​ creeper will be added that is twice the terrain height.
 +# If "​terrain"​ is anything else, either nothing will happen, or something entirely unpredictable will happen.
 +   ​PlaySound("​Retro26"​)
 +   <​-rad_external <​-rad_internal lt if
 +      <​-rad_external ->​internal
 +      <​-rad_internal ->​external
 +   else
 +      <​-rad_external ->​external
 +      <​-rad_internal ->​internal
 +   endif
 +if (<-row lt (MapHeight))
 +   ​@DoRow
 +   <​-row add(1) ->row
 +   ​do(MapWidth 0)
 +      Distance(CurrentCoords I <-row) ->​distance
 +      if (<​-distance gte(<​-internal))
 +         if (<​-distance lte(<​-external))
 +            0.1 <​-terrain eq if
 +               I <-row 2 SetDigitalisGrowth
 +            else
 +               0.2 <​-terrain eq if
 +                  I <-row 0 SetDigitalisGrowth
 +               else
 +                  0.3 <​-terrain eq if
 +                     I <-row 1 SetWall
 +                  else
 +                     0.4 <​-terrain eq if
 +                        I <-row 0 SetWall
 +                     else
 +                        0.5 <​-terrain eq if
 +                           I <-row GetTerrain SetTerrainOverride
 +                        else
 +                           <​-terrain 11 gte if
 +                              I <-row <​-terrain SetTerrainOverride
 +                           else
 +                              2.1 <​-terrain eq if
 +                                 I <-row GetTerrain 2 mul SetCreeper
 +                              else
 +                                 I <-row <​-terrain SetTerrain
 +                              endif
 +                           endif
 +                        endif
 +                     endif
 +                  endif
 +               endif
 +            endif
 +         endif
 +      endif
 +   loop
 +<​sub>​Made and submitted by Clean0nion</​sub>​
 +Use suffitient quantities of AC to turn an emitter into an AC emitter. ​
 +<​code>#​ Converterv2.crpl
 +# Created on: 1/3/2014 1:53:35 PM
 +# ------------------------------------------
 +#​Initializes state, thanks to Grayzzur for the idea.
 +<​-startState ->state
 +" ->​lineBreak
 +#Checks if there is creeper, if there is, sets the converter to make creeper.
 +CurrentCoords GetCreeper 0 gt if
 +1 ->state
 +SetPopUpText("​amt:​ " <​-amtCreeper <​-lineBreak "​delay:​ " <​-interval 30 div concat concat concat concat)
 +#Checks if there is anticreeper,​ if there is, sets the converter to make AC
 +CurrentCoords GetCreeper 0 lt if
 +0 ->state
 +#the linebreak is on purpose, and concat because the amt isn't always the same.
 +SetPopUpText("​amt:​ " <​-amtAnti <​-lineBreak "​delay:​ " <​-interval 30 div concat concat concat concat)
 +#Checks the state, remember if there was nothing under it, it will use it's last state!
 +<-state if
 +CurrentCoords <​-amtCreeper SetCreeper
 +CurrentCoords <​-amtAnti SetCreeper
 +<​-interval Delay</​code><​sub>​Made and submitted by Flabort; named by VirgilW</​sub>​
 +=====Frames to minutes=====
 +Converts an amount of frames to minutes in M:SS format. Frames stored in variable "​frames";​ result placed at top of stack. This is best used with Timers as it doesn'​t work when "​frames"​ is 0. As in, it reaches 0:00 then just disappears.
 +1800 displays 1:00;
 +60 displays 0:02;
 +2400 displays 1:20.
 +<​code><​-frames 1800 div ceil dup <-frames 30 div ceil swap 60 mul sub 60 mod ":"​ swap
 +dup 10 lt if
 + "​0"​ swap
 +StackSize 1 do
 + concat
 +<​sub>​Written,​ coded, developed, procrastinated over, contributed,​ posted, wiki'​d,​ wordified, done, made, finished and submitted by Clean0nion</​sub>​
 +=====Survival Pod=====
 +Attach to a core to create a survival pod. Survival pods must be rescued. ​ If they are destroyed, the mission will fail.  Makes use of the Message script found in the next entry below.
 +<​code>​GetCreeper(CurrentCoords) ->​creeper
 +if (not(<​-ascending) and (<​-creeper gt (0)))
 + FailMission
 + Destroy(Self 2)
 +if ( not(<​-ascending or (GetUnitAttribute(Self CONST_ISBUILDING))) )
 + AscendToOrbit
 + true ->​ascending
 + PlaySound("​Misc17"​)
 + SetPopupText(""​)
 + ShowGameEventTag("​GREEN"​ "​INFO"​ "​Survival Pod" "​Rescued!"​ GetUnitAttribute(Self CONST_PIXELCOORDX) GetUnitAttribute(Self CONST_PIXELCOORDY))
 +if (<​-ascending)
 + GetUnitTransformPosition ->z ->y ->x
 + #​CreateEffect(10 <-x <-y -1 2 2 0.04)
 + CreateEffect(8 <-x <-y -1 2 2 0.04)
 + SetPopupText(GetUnitAttribute(Self CONST_HEALTH) "/"​ concat "​30"​ concat)
 + SetPopupTextX(0)
 +if (GetUpdateCount eq (0))
 + SetUnitAttribute(Self CONST_ISBUILDING TRUE)
 + SetUnitAttribute(Self CONST_BUILDCOST 30)
 + SetUnitAttribute(Self CONST_HEALTH 0)
 + SetPopupTextAlwaysVisible(true)
 + SetPopupTextY(-12)
 + CreateUnit("​CRPLCORE"​ 0 0) ->​message
 + AddScriptToUnit(<​-message "​PodMessage.crpl"​) ​
 + if (<​-ascending)
 + SetScriptVar(<​-message "​PodMessage.crpl"​ "​message"​ "Pod Rescued"​)
 + else
 + SetScriptVar(<​-message "​PodMessage.crpl"​ "​message"​ "Pod Destroyed!"​)
 + SetScriptVar(<​-message "​PodMessage.crpl"​ "​red"​ 255)
 + SetScriptVar(<​-message "​PodMessage.crpl"​ "​green"​ 0)
 + SetScriptVar(<​-message "​PodMessage.crpl"​ "​blue"​ 0)
 + CreateUnit("​CRPLCORE"​ CurrentCoords) ->​tombstone
 + AddScriptToUnit(<​-tombstone "​PodDestroyed.crpl"​) ​
 + ShowGameEventTag("​RED"​ "​INFO"​ "​Survival Pod" "​Destroyed"​ GetUnitAttribute(Self CONST_PIXELCOORDX) GetUnitAttribute(Self CONST_PIXELCOORDY))
 + endif
 +<​sub>​Contributed by virgilw</​sub>​
 +Shows a message on the map that goes away.  Used by the SurvivalPod script above.
 +$message:"​Pod Rescued"​
 + <​-timeToLive sub(<​-solidTime) ->​fadeTime
 + SetText(<​-message)
 + SetTextColor(<​-red <-green <-blue 0)
 +if (GetUpdateCount gte(<​-timeToLive))
 + Destroy(Self 0)
 +if (GetUpdateCount gt(1))
 + if (GetUpdateCount gte(<​-solidTime))
 + <​-timeToLive sub(GetUpdateCount) asfloat div(<​-fadeTime) mul(255) ->alpha
 + else
 + 255 ->alpha
 + endif
 + SetTextColor(<​-red <-green <-blue <-alpha)
 + SetImageColor(Self "​Main"​ 32 32 32 <-alpha sub(60))
 + SetUnitAttribute(Self CONST_CREATEPZ FALSE)
 + SetScreenMode(TRUE)
 + SetTextY(-5)
 + SetTextSize(2)
 + SetScreenPixelCoordX(ScreenWidth div(2))
 + SetScreenPixelCoordY(135)
 + SetImage(Self "​Main"​ "​Custom1"​)
 + SetImageScaleX(Self "​Main"​ 14)
 + SetImageScaleY(Self "​Main"​ 1.5)
 + SetImageColor(Self "​Main"​ 32 32 32 0)
 + PlaySound("​Retro9"​) ​
 +<​sub>​Contributed by virgilw</​sub>​
 +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.
 +<​code>#​Function :​GetClosestUnitToCoords
 +#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 '​-1'​.
 + asfloat ->​gcu_mdist
 + asfloat ->gcu_y
 + asfloat ->gcu_x
 + StackSize ->​gcu_szmin
 + # Reference: https://​​wiki/​doku.php?​id=crpl:​docs:​getunittype
 + # Place Unit Types that you /WANT/ to look for here.
 + # Simply comment '#'​ out or remove any that you don't want. All this area does is push strings to the stack.
 + "​FORGE"​ "​BERTHA"​ "​THOR"​
 + #"​CRPLCORE"​ #Not recommended as this will grab literally any CRPL Core.
 + "​RUNNER"​
 + # =====
 + #Finding the boundary stack size (how many elements were added to stack)
 + #and then forming a list from the units we want.
 + StackSize ->​gcu_szmax
 + CreateList ->​gcu_types
 + <​-gcu_szmax <​-gcu_szmin do
 + ->​gcu_t
 + <​-gcu_types <-gcu_t AppendToList
 + loop
 + #​initialize unit
 + -1 ->​gcu_unit
 + 999999 ->​gcu_unitd
 + #retrieve /all/ nearby units
 + # x y distance boolean(true-square,​ false-circle)
 + # unit checks have been ordered in perceived processing complexity
 + <-gcu_x <-gcu_y <​-gcu_mdist false GetAllUnitsInRange ->​gcu_count
 + <​-gcu_count neq0 if
 + <​-gcu_count 0 do
 + ->​gcu_tempunit
 + <​-gcu_tempunit GetUnitType ->gcu_t
 + #get the distance
 + <​-gcu_x <-gcu_y @UnitCoords(<​-gcu_tempunit) Distance ->gcu_d
 + #is that distance closer?
 + <​-gcu_d <​-gcu_unitd lt if
 + #do we care about this unit type?
 + <​-gcu_types <-gcu_t @DoesListContain if
 + #set our new baseline distance and unit
 + <​-gcu_d ->​gcu_unitd
 + <​-gcu_tempunit ->​gcu_unit
 + endif
 + endif
 + loop
 + endif
 + <​-gcu_unit
 +#Function :UnitCoords
 +#Simple convenience method. Takes a unit and returns the coordinates.
 +#'​CurrentCoords'​ and 'self @UnitCoords'​ are equivalent.
 +#Notation: n1 - x y
 +#eg. <​-myUnitID @UnitCoords ->unitX ->unitY
 + ->u
 + <-u CONST_COORDX GetUnitAttribute
 + <-u CONST_COORDY GetUnitAttribute
 +#Function :​DoesListContain
 +#This function an object, then a list and returns a boolean value.
 +#Notation: L1 n1 - b1
 +#eg. <-myList <​-myValue @DoesListContain ->​contains
 + ->​dl_value
 + ->​dl_list
 + false ->​dl_contained
 + <​-dl_list GetListCount 0 do
 + <​-dl_list I GetListElement eq(<​-dl_value) if
 + true ->​dl_contained
 + break
 + endif
 + loop
 + <​-dl_contained
 +<​sub>​Contributed by Telanir</​sub>​
 +===CRPL generating CRPL===
 +This CRPL will generate CRPL with custom program (if it is in the map scripts)
 +CreateUnit("​CRPLCORE"​ CurrentX CurrentY) ->​unit
 +AddScriptToUnit(<​-unit "​[Programname].crpl"​)
 +<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'​s coming on map search </​sub>​
crpl/crpltutorial/code_examples.1382744786.txt.gz · Last modified: 2014/10/01 15:02 (external edit)