User Tools

Site Tools


prpl:prplsnippets

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
prpl:prplsnippets [2017/12/16 16:00] – added buildable and chargable unit example kajacxprpl:prplsnippets [2021/04/05 14:40] (current) – Typo fixes Karsten75
Line 7: Line 7:
 If you are looking for what is possible in PRPL or some inspiration for your own units, this is a good place to start, provided you know [[prpl:prpltutorial|the fundamentals]]. Each segemnt will consist of the code doing the work, the explaination of the code and a little homework for you to modify it in some way. If you are looking for what is possible in PRPL or some inspiration for your own units, this is a good place to start, provided you know [[prpl:prpltutorial|the fundamentals]]. Each segemnt will consist of the code doing the work, the explaination of the code and a little homework for you to modify it in some way.
  
-(TODO: buildable/charable unit, creating a unit, work with images, work with text, communication betweeen scripts, work with ship inventory - this list is mostly for myself so I don't forget what I wanted to talk about) +(TODO: buildable/charable unit, creating a unit, work with images, work with text, communication betwee n scripts, work with ship inventory - this list is mostly for myself so I don't forget what I wanted to talk about) 
  
 ===== Discovering units/ships/particles in a range ===== ===== Discovering units/ships/particles in a range =====
Line 261: Line 261:
  
 Aslo, when checking if the ehalth is low enought, `Self GetUnitHealth 10 lt if # health is low enought to flip` it is wise to choose a slightly larger number, like `10`, so that even when multiple lathes are targeting the unit, they will not destroy it before it can flip and fully heal itself. Aslo, when checking if the ehalth is low enought, `Self GetUnitHealth 10 lt if # health is low enought to flip` it is wise to choose a slightly larger number, like `10`, so that even when multiple lathes are targeting the unit, they will not destroy it before it can flip and fully heal itself.
 +</hidden>
 +
 +===== Working with images =====
 +
 +Let's try a new format - we will start with a simple script of showing a star image, and then we will add new features gradually. So, here is the basics of adding an image: First, create the image you want. It must have exactly one if the following sizes: 64x64 pixels, 128x128 pixels, or 256x256 pixels. You download the 2 images used in this tutorial right here:
 +
 +<html><div style="background-color: lightgrey"></html>
 +{{:prpl:full256.png|}} {{:prpl:border256.png|}}
 +<html></div></html>
 +
 +(It's a white image on a transparent background, so some images viewers might display it as a white square, but don't worry, it will work fine).
 +
 +Next, you need to register the images into the game. In the PF map editor, go to Editor -> Map -> Custom Images -> 256x256 tab -> Select on the first slot, and then open the downlaoded file in the file chooser.
 +
 +{{:prpl:custom_images.png?600|}}
 +
 +Finally, once the images are loaded into the game, you can write a PRPL script that will use the images:
 +
 +<code prpl>
 +$StarFill:"Custom0_256"
 +$StarBorder:"Custom1_256"
 +
 +once
 +    Self "fill" <-StarFill SetImage
 +    Self "fill" 255 255 0 255 SetImageColor # R=255 G=255 B=0 A=255 - yellow
 +    Self "fill" 2 2 SetImageScale
 +    
 +    Self "border" <-StarBorder SetImage
 +    Self "border" 255 128 0 255 SetImageColor # R=255 G=128 B=0 A=255 - orange
 +    Self "border" 2 2 SetImageScale
 +endonce
 +</code>
 +
 +Again, search for "image" on the [[prpl:prplreference|PRPL Reference]] for more detail, or go to the [[https://knucklecracker.com/wiki/doku.php?id=crpl:crplreference#image_commands|CRPL Reference]] if the page isn't filled on the PRPL wiki.
 +
 +How it looks:
 +{{:prpl:first_star.png|}}
 +
 +The interesting part here is that the image itself is whire, but the color is set in PRPL. The advantage over having the image itself colored is that we can now compute and change the color with PRPL to our liking, which we will do momentary.
 +
 +But first, a few notes:  ''%%Self "fill" <-StarFill SetImage%%'' The first agument, Self, is the ID of the core on which we want to set the image, so you can manipulate images on different cores, but that is rarely used. The second argument, ''%%"fill"%%'', is the internal name of the image. Since a PRPL core can have multiple images attached to them and you need to differentiate between them, this is the way to it. Finally, ''%%<-StarFill%%'' is the name of the image in the game's image repository (if you can call that). This is the slot name into which you loaded the image earlier. It's a good practice to always set this as Script Argument, since you could easily end up with 2 scripts that want to use the same slot for different images, so you can easily change the image slot without even editing the script's code.
 +
 +Next, let's take a look at ''%%Self "fill" 255 255 0 255 SetImageColor%%'' The 4 numbers specify the color as a RGBA color - that means the first number is for Red, second for Green, third for Blue and fourth for Alpha (visibility). Each pixel's values in the original image is "multiplied" by this value (or rather, by it's relative value). For example, if the original pixel in the image had color (255,128,0,128) (half-transparent orange), and the color set by PRPL was (32,128,255,64), then the resulting color would be (64,128,0,32), since (32 = 255 * (32/256), 64 = 128 * (128/256), 0 = 0 * (255/256), 32 = 128 / (64/256)). Note that this can only decrese the color value, so while you can turn white into any other color, black will always remain black.
 +
 +Finally, ''%%Self "fill" 2 2 SetImageScale%%'' is relatively simple, for image scale just remember that ''1 scale = 3 cells'', probably since 3x3 modules are a common image, so they would have scale 1.
 +
 +==== Making the star change color ====
 +
 +Now we are getting into some fun. Let's start by chaning the color from red to yellow by keeping the red color channel at 255 and changing the green color channel:
 +
 +<code prpl>
 +$StarFill:"Custom0_256"
 +$StarBorder:"Custom1_256"
 +$Step:2 #the speed of color change
 +
 +once
 +    Self "fill" <-StarFill SetImage
 +    #Self "fill" 255 255 0 255 SetImageColor # R=255 G=255 B=0 A=255 - yellow
 +    Self "fill" 2 2 SetImageScale
 +    
 +    Self "border" <-StarBorder SetImage
 +    Self "border" 255 128 32 255 SetImageColor
 +    Self "border" 2 2 SetImageScale
 +    
 +    0 ->green
 +    1 ->growing #the green color is growing
 +endonce
 +
 +<-growing if
 +    <-green <-Step add ->green
 +    <-green 255 gt if
 +        # the green color is more than the maximum value of 255
 +        255 ->green #set it back to maximum
 +        0 ->growing #make it so its growing smaller isntead
 +    endif
 +else
 +    <-green <-Step sub ->green
 +    <-green 0 lt if
 +        # the green color is less than the minimum value of 0
 +        0 ->green #set it back to minimum
 +        1 ->growing #make it so its growing larger again
 +    endif
 +endif
 +
 +Self "fill" 255 <-green 0 255 SetImageColor
 +</code>
 +
 +How it looks:
 +{{:prpl:second_star.gif|}}
 +
 +The code should be pretty self-explanatory. If you have never worked with RGb values before and ar ewondering where the 255 number comes from, it's from the fact taht computers store color as 3 8-bit numbers, one for Red, one for Green and one for Blue (RGB). And since 8 bits can hold 256 diffent values, the color values are from the range 0-255. Finally, if you want to see some examples of colors and their RGB values, jsut any [[http://lmgtfy.com/?q=color+picker+online|online color picker]].
 +
 +==== Setting position and rotation ====
 +
 +Next we are going to make the start rotate around the core by chaning it's position, as well as rotate as an image to make it spin. Code:
 +
 +<code prpl>
 +$StarFill:"Custom0_256"
 +$StarBorder:"Custom1_256"
 +$Step:2 #the speed of color change
 +$Distance:40 #the radius of the star's orbit around the core, in pixels
 +$OrbitingSpeed:0.01 #the speed at which the star orbits around the code, in radians/frame
 +$SpinningSpeed:-0.03 #the speed at which the star spins around it's own center, in radians/frame
 +
 +once
 +    Self "fill" <-StarFill SetImage
 +    #Self "fill" 255 255 0 255 SetImageColor # R=255 G=255 B=0 A=255 - yellow
 +    Self "fill" 2 2 SetImageScale
 +    
 +    Self "border" <-StarBorder SetImage
 +    Self "border" 255 128 32 255 SetImageColor
 +    Self "border" 2 2 SetImageScale
 +    
 +    0 ->green
 +    1 ->growing #the green color is growing
 +    
 +    0 ->orbitAngle
 +    0 ->spinAngle
 +endonce
 +
 +# set the color
 +<-growing if
 +    <-green <-Step add ->green
 +    <-green 255 gt if
 +        # the green color is more than the maximum value of 255
 +        255 ->green #set it back to maximum
 +        0 ->growing #make it so its growing smaller isntead
 +    endif
 +else
 +    <-green <-Step sub ->green
 +    <-green 0 lt if
 +        # the green color is less than the minimum value of 0
 +        0 ->green #set it back to minimum
 +        1 ->growing #make it so its growing larger again
 +    endif
 +endif
 +
 +Self "fill" 255 <-green 0 255 SetImageColor
 +
 +#set the position
 +<-orbitAngle <-OrbitingSpeed add ->orbitAngle
 +Self "fill" <-orbitAngle cos <-Distance mul SetImagePositionX
 +Self "fill" <-orbitAngle sin <-Distance mul SetImagePositionY
 +Self "border" <-orbitAngle cos <-Distance mul SetImagePositionX
 +Self "border" <-orbitAngle sin <-Distance mul SetImagePositionY
 +
 +#set the rotation
 +<-spinAngle <-SpinningSpeed add ->spinAngle
 +Self "fill" <-spinAngle SetImageRotation
 +Self "border" <-spinAngle SetImageRotation
 +</code>
 +
 +Result:
 +{{:prpl:third_star.gif|}}
 +
 +The key to making it look good is balancing the rotations speeds properly. I definetely like it more when the start is spinning in the oposite derection than the orbit (positiv/negative rotation speed), but you can change it however you like.
 +
 +==== The homework ====
 +
 +We could go on and on, making the star bigger/smaller, or moving it closer/further away, or even adding more interesting color changes, etc. But for the homework, make it so that 6 stars orbit the core, evenly spread out. You will need 12 image slots in total (6 for the "fills", and 6 for the "borders") and I recommend you name them "fill0", "fill1" etc so you can easily manipulate them by using ''%%"fill" I concat%%'' inside a do-loop. Use ''TWOPI 6 div'' to get the radian equivalent of 60 degrees.
 +
 +Preview:
 +
 +{{:prpl:fourth_star.gif|}}
 +
 +Solution:
 +<hidden>
 +<code prpl>
 +$StarFill:"Custom0_256"
 +$StarBorder:"Custom1_256"
 +$Step:2 #the speed of color change
 +$Distance:40 #the radius of the star's orbit around the core, in pixels
 +$OrbitingSpeed:0.01 #the speed at which the star orbits around the code, in radians/frame
 +$SpinningSpeed:-0.03 #the speed at which the star spins around it's own center, in radians/frame
 +$Stars:6 #the amount of stars
 +
 +once
 +    Self RemoveImages
 +
 +    <-Stars 0 do
 +        Self "fill" I concat <-StarFill SetImage
 +        Self "fill" I concat 2 2 SetImageScale
 +        
 +        Self "border" I concat <-StarBorder SetImage
 +        Self "border" I concat 255 128 32 255 SetImageColor
 +        Self "border" I concat 2 2 SetImageScale
 +    loop
 +    
 +    0 ->green
 +    1 ->growing #the green color is growing
 +    
 +    0 ->orbitAngle
 +    0 ->spinAngle
 +endonce
 +
 +# set the color
 +<-growing if
 +    <-green <-Step add ->green
 +    <-green 255 gt if
 +        # the green color is more than the maximum value of 255
 +        255 ->green #set it back to maximum
 +        0 ->growing #make it so its growing smaller isntead
 +    endif
 +else
 +    <-green <-Step sub ->green
 +    <-green 0 lt if
 +        # the green color is less than the minimum value of 0
 +        0 ->green #set it back to minimum
 +        1 ->growing #make it so its growing larger again
 +    endif
 +endif
 +
 +
 +<-orbitAngle <-OrbitingSpeed add ->orbitAngle
 +<-spinAngle <-SpinningSpeed add ->spinAngle
 +
 +<-Stars 0 do
 +    Self "fill" I concat 255 <-green 0 255 SetImageColor
 +
 +    #set the position
 +    TWOPI <-Stars div I mul ->offset
 +    Self "fill" I concat <-orbitAngle <-offset add cos <-Distance mul SetImagePositionX
 +    Self "fill" I concat <-orbitAngle <-offset add sin <-Distance mul SetImagePositionY
 +    Self "border" I concat <-orbitAngle <-offset add cos <-Distance mul SetImagePositionX
 +    Self "border" I concat <-orbitAngle <-offset add sin <-Distance mul SetImagePositionY
 +
 +    #set the rotation
 +    Self "fill" I concat <-spinAngle SetImageRotation
 +    Self "border" I concat <-spinAngle SetImageRotation
 +loop
 +</code>
 </hidden> </hidden>
prpl/prplsnippets.1513458026.txt.gz · Last modified: 2017/12/16 16:00 by kajacx