Official CRPL examples

Started by Grauniad, December 20, 2012, 12:31:44 PM

Previous topic - Next topic

eduran

#15

# BorderSpores.crpl
# Created on: 11/2/2013 6:48:09 PM
# ------------------------------------------
# This script tries to emulate CW1 spores. Spores spawn in random cells along the edge of the map and target random
# player units. The script also includes a warning indicator similar to the one spore towers create.


# map borders that are allowed to spawn spores are set below
# 0 means no spawn from that direction; has to be either 0 or 1
# at least one side has to be set to 1
$left:1
$right:1
$top:1
$bottom:1

# spore settings
$initialDelay:9000 # time before the first wave spawns in frames (1800 frames = 1 minute)
$nOfSpores:5 # number of spores spawned per interval
$interval:1800 # time between wave spawns in frames
$delay:30 # time between individual spores within a wave
$payload:10 # amount of creeper each spore carries
$health:1 # health of the spores; default is 1

# warning settings
$backgroundImg:"Custom0_256" # image slot for the red background of the warning message
$displayWarningTime:1800 # from this time point of the countdown onward the warning message is displayed (-1 to deactivate)
$playSoundTime:1800 # at this point of the countdown the warning sound is played (-1 to deactivate)
$alternativeMode:0 # set to 1 if you want to use this script and spore towers at the same time to avoid
# overlap of warning messages
$sound:"Misc19" # warning sound
$backgroundScaleX:29.15 # parameters to tweak the background image
$backgroundScaleY:8
$backgroundPosX:61.5

$name:"BorderSporeSpawner" # allows other CRPL cores to find the spawner by using GetCoresWithVar

once
@Setup
SetTimer0(<-initialDelay)
0 ->counter
0 ->soundPlayed
endonce

# check if spores need to be spawned
if(GetTimer0 eq0 and(GetTimer1 eq0))
if(<-counter lt(<-nOfSpores))
if (<-delay gt(0))
# create one spore and wait for the delay
@PickOrigin
@PickTarget
CreateSpore(<-originX <-originY <-targetX <-targetY <-health <-payload)
<-counter add(1) ->counter
SetTimer1(<-delay)
else
# create all spores at once
do(<-nOfSpores 0)
@PickOrigin
@PickTarget
CreateSpore(<-originX <-originY <-targetX <-targetY <-health <-payload)
loop
<-nOfSpores ->counter
endif
PlaySound("SporeFire")
else
# all spores spawned, reset timer and warning
SetTimer0(<-interval)
0 ->counter
0 ->soundPlayed
@DeactivateWarning
endif
endif

# display warning message
if(<-warningActive)
if(GetTimer0 mod(30) eq0)
@SetWarningText
endif
else
if(GetTimer0 lt(<-displayWarningTime))
@ActivateWarning
endif
endif

# play warning sound
if(not(<-soundPlayed) and(GetTimer0 lt(<-playSoundTime)))
1 ->soundPlayed
PlaySound(<-sound)
endif

:PickOrigin
# picks a random cell at the edge of the map
GetListElement(<-Sides RandInt(0 GetListCount(<-Sides))) ->side
if(<-side eq("left") or(<-side eq("right")))
RandInt(0 MapHeight) ->originY
if(<-side eq("left"))
0 ->originX
else
MapWidth ->originX
endif
else
RandInt(0 MapHeight) ->originX
if(<-side eq("top"))
0 ->originY
else
MapHeight ->originY
endif
endif

:PickTarget
# picks a random player unit as target, or a random cell if no unit is present
CreateList ->Targets
GetUnitsInRange(0 0 9999) ->nOfTargets
AppendStackToList(<-Targets)
GetListElement(<-Targets RandInt(0 <-nOfTargets)) ->target
if(<-target neq0)
GetUnitAttribute(<-target CONST_COORDY) ->targetY
GetUnitAttribute(<-target CONST_COORDX) ->targetX
else
RandCoords ->targetY ->targetX
endif

:ActivateWarning
# enables the red warning strip right above the build menu
if(not(<-warningActive))
SetImageColor(Self "background" 255 0 0 255)
@SetWarningText
1 ->warningActive
endif

:DeactivateWarning
# disables the red warning strip right above the build menu
if(<-warningActive )
SetImageColor(Self "background" 255 0 0 0)
SetText("")
0 ->warningActive
endif

:SetWarningText
# converts timer into text
GetTimer0 div(1800) ->minutes
GetTimer0 mod(1800) div(30) ->seconds
if(<-seconds lt(10))
"0" concat(<-seconds) ->seconds
endif
SetText(<-nOfSpores concat(" spore(s) in ") concat(<-minutes) concat(":") concat(<-seconds))

:Setup
# one-time setup for everything
SetUnitAttribute(Self CONST_COUNTSFORVICTORY 0)
SetUnitAttribute(Self CONST_TAKEMAPSPACE 0)
SetUnitAttribute(Self CONST_NULLIFIERDAMAGES 0)
# the following lines anchor the CRPL core to the top of the build menu
SetScreenMode(true)
SetScreenPixelCoordX(ScreenWidth div(2) sub(62))
if(<-alternativeMode)
SetScreenPixelCoordY(130)
else
SetScreenPixelCoordY(110)
endif
# configures text size and position, but does not display anything yet
SetText("")
SetTextSize(0.6)
SetTextColor(255 255 255 255)
SetTextAnchor("MiddleLeft")
# same for the text background
SetImage(Self "main" "NONE")
SetImage(Self "background" <-backgroundImg)
SetImageScale(Self "background" <-backgroundScaleX <-backgroundScaleY)
SetImagePositionX(Self "background" <-backgroundPosX)
SetImageColor(Self "background" 255 0 0 0)
# create a list of valid spawn locations, needed for @PickOrigin
<-left add(<-right) add(<-top) add(<-bottom) ->sideCount
CreateList ->Sides
if(<-left eq(1))
AppendToList(<-Sides "left")
endif
if(<-right eq(1))
AppendToList(<-Sides "right")
endif
if(<-top eq(1))
AppendToList(<-Sides "top")
endif
if(<-bottom eq(1))
AppendToList(<-Sides "bottom")
endif


CW1 spores, spawning at the edge of the map instead of at a tower. Includes a warning message similar to the build-in one.

jakeflee

radar makes a radar circle using creeper or anticreeper i would use a small amount of creeper
$xcoord:128
$ycoord:128
$amount:-.3
$d:5
$radmax:100

once
pi 180 div ->radcon
endonce

0 ->ang

while <-ang 360 lte repeat

<-ang <-radcon mul ->rads
<-ang 1 add ->ang
1 ->radius

while <-radius <-radmax lte repeat
<-rads sin ->xucirc
<-rads cos ->yucirc
<-xucirc <-radius mul ->xrad
<-yucirc <-radius mul ->yrad
<-xrad <-xcoord add ->xccoord
<-yrad <-ycoord add ->yccoord

<-radius 1 add ->radius
<-xccoord <-yccoord <-amount addcreeper
endwhile
<-d delay
endwhile


i like to break up my equations like this so don't pester me about that

Flabort

After it's been used in a few maps, and Tek accidentally grabbed a version with a shield-key atached, I figured I should move this here:
The Flip-Emitter, V2.2
# Converterv2.crpl
# Created on: 1/3/2014 1:53:35 PM
# ------------------------------------------
$amtCreeper:50
$amtAnti:-25
$interval:60
$startState:0
#Initializes state, thanks to Grayzzur for the idea.
Once
<-startState ->state
"
" ->lineBreak
endonce
#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)
SetUnitAttribute(Self CONST_COUNTSFORVICTORY TRUE)
endif
#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)
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 <-amtCreeper SetCreeper
else
CurrentCoords <-amtAnti SetCreeper
endif
<-interval Delay
My maps: Top scores: Sugarplum, Cryz Dal, Cryz Torri, Cryz Bohz (Click fetch scores, page courtesy of kwinse)

Grauniad

Thank you for your contribution., It is indeed excellent.

The preferred location for code examples is the wiki.
A goodnight to all and to all a good night - Goodnight Moon

First

#19
Purpose: This tower attracts spores from the ether that detonate on the tower - a kind of emitter that can be defended against using beam weapons.
Illustrates timers, CreateSpore and RandCoordsInRange

$Range:50.0
$Payload:1000
$IntervalSeconds:2

once
#random start time between 1 and 2 intervalseconds
<-IntervalSeconds 30 mul <-IntervalSeconds 30 mul 2 mul RandInt SetTimer0
endonce

GetTimer0 eq0 if

CurrentCoords <-Range RandCoordsInRange CurrentCoords 1 <-Payload CreateSpore
<-IntervalSeconds 30 mul SetTimer0

endif
#delay might be better in this example

First

Purpose: Emulates an erupting volcano. Builds a cone by editing the local terrain. Emits spores land and detonate near the crater.
Illustrates timers, some coordinates API, Get/SetTerrain and basic flow control :)

# volcano.crpl
# Created on: 2/9/2014 2:13:15 PM
# Created by: First (jeffhfcotter@gmail.com)
# Defaults produce a fairly mild volcano that grows a cone and emits spore bombs
# ConeGrowthIntervalFrames is in game-frames
# SporeIntervalSeconds is in game-seconds
# ConeSize is a radius
# WTB: a better, more realistic looking cone growth algorithm
# WTB: <-xCurrent <-yCurrent <-Height GetNearestTerrain ->xNearest ->yNearest
#    Gets the location of nearest terrain that has height of Height
# WTB: cool looking crater image to replace the tower image
# ------------------------------------------
$ConeGrowthIntervalFrames:10
$ConeSize:10
$SporeIntervalSeconds:20
$SporeRange:20
$SporePayload:100

once
<-ConeGrowthIntervalFrames <-ConeGrowthIntervalFrames 2 mul RandInt SetTimer0
<-SporeIntervalSeconds 30 mul <-SporeIntervalSeconds 30 mul 2 mul RandInt SetTimer1
endonce

GetTimer0 eq0 if
0 ->CheckRange
while <-CheckRange <-ConeSize lt
repeat
CurrentCoords <-CheckRange RandCoordsInRange ->yTar ->xTar
CurrentCoords <-xTar <-yTar Distance ->Range
<-xTar <-yTar GetTerrain ->Height
10.0 <-Range 10.0 <-ConeSize div mul sub ->TargetHeight
<-TargetHeight <-Height gt if
<-xTar <-yTar <-Height 1 add SetTerrain
<-ConeSize ->CheckRange
else
<-CheckRange 1 add ->CheckRange
endif
endwhile
<-ConeGrowthIntervalFrames SetTimer0
endif

GetTimer1 eq0 if
CurrentCoords <-SporeRange RandCoordsInRange ->yTar ->xTar
CurrentCoords <-xTar <-yTar 1.0 <-SporePayload CreateSpore
<-SporeIntervalSeconds 30 mul SetTimer1
endif

Grauniad

I am locking this topic. As with all multi-purpose threads, it  becomes increasingly more difficult to find specific contributions.  Please submit all future code examples to the wiki so that each contribution can have a header and a description and can be found without having to page through multiple pages of this thread.

Thank you.
A goodnight to all and to all a good night - Goodnight Moon