User Tools

Site Tools


crpl:crpltutorial:code_examples

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
Next revisionBoth sides next revision
crpl:crpltutorial:code_examples [2014/10/01 15:02] – external edit 127.0.0.1crpl:crpltutorial:code_examples [2020/06/08 16:35] – html comment removed builder17
Line 1: Line 1:
-~~DISCUSSION~~ +
-<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 864: Line 866:
 </code> </code>
 <sub>Contributed by virgilw</sub> <sub>Contributed by virgilw</sub>
 +
 +=====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.
 +<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'.
 +:GetClosestUnitToCoords
 + asfloat ->gcu_mdist
 + asfloat ->gcu_y
 + asfloat ->gcu_x
 + StackSize ->gcu_szmin
 +
 + # Reference: https://knucklecracker.com/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.
 + "COMMANDNODE"
 + "COLLECTOR" "RELAY" "REACTOR" "OREMINE" "SIPHON" "TERP" "GUPPY"
 + "PULSECANNON" "MORTAR" "STRAFER" "BOMBER" "SPRAYER" "NULLIFIER" "BEAM" "SNIPER"
 + "FORGE" "BERTHA" "THOR"
 + "GUPPYAIR" "STRAFERAIR" "BOMBERAIR"
 + "POWERZONE" "OREDEPOSIT" "RESOURCEPACK" "TOTEM"
 + "SHIELDKEY" "TECHARTIFACT" "MESSAGEARTIFACT"
 + #"CRPLCORE" #Not recommended as this will grab literally any CRPL Core.
 + "EMITTER" "SPORETOWER" "RUNNERNEST" "AETOWER" "INHIBITOR"
 + "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
 +:UnitCoords
 + ->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
 +:DoesListContain
 + ->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
 +</code>
 +<sub>Contributed by Telanir</sub>
 +
 +
 +===CRPL generating CRPL===
 +This CRPL will generate CRPL with custom program (if it is in the map scripts)
 +<code>
 +CreateUnit("CRPLCORE" CurrentX CurrentY) ->unit
 +AddScriptToUnit(<-unit "[Programname].crpl")
 +</code>
 +<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.txt · Last modified: 2022/01/11 03:00 by Karsten75