User Tools

Site Tools


crpl:docs:pathfindterrain

<- CRPL reference <- Terrain and Wall Commands

PathFindTerrain

Arguments Result Notation
X1, Y1, X2, Y2, min_height, max_height, avoid_occupied, full_cells_only a list. X1 Y1 X2 Y2 i1 i2 b1 b2 – l1

Description

Returns a list of packed coordinates in the y*MapWidth+x format that satisfy the following conditions:

  • The set is a path. Meaning each in order is a neighbour.
  • The set is empty or contains (X1,Y1)
  • The set is empty or contains (X2,Y2)
  • The set is filled only with squares at height between min_height and max_height inclusive.
  • If avoid_occupied is set, no square is under an object, unless that square is (X1,Y1).
  • If full_cells_only is set, no square is one of those triangle areas at the border of two height regions.
  • No square is one of those triangle areas if the path is on the low is on the low side unless that square is (X1,Y).

Examples

Enjoy this script that draws a path between the mouse and the nearest anticreeper, avoiding terrain higher or lower than the source or destination, as well as buildings. This script requires a vertically aligned line like image in image slot Custom0.

  #Clean up from last frame
  Self RemoveImages
  #move the core to the mouse.
  GetMouseCell SetCurrentCoords
  #create target coords
  @testCoords ->y ->x

  #create the path (from me to (x,y))
  CurrentCoords <-x <-y
      CurrentCoords GetTerrain <-x <-y GetTerrain @sort2
      TRUE FALSE PathFindTerrain ->list

  @drawTheLightning

:awake
  TRUE OperateWhilePaused

#Replace this with whatever you want to path to.
:testCoords
  CurrentCoords 999 FALSE FALSE FALSE TRUE GetNearestGroundEnemy

#This function sorts the two items on the stack from lowest to highest
:sort2
 ->b ->a
 <-a <-b lt if <-a <-b else <-b <-a endif

:drawTheLightning
  #initialize the old coordinates
  CurrentCoords CellToPixel ->oldY ->oldX
  #Memorize the core position, as images draw relative to the core.
  CurrentCoords CellToPixel ->y0 ->x0
  
  #for each square in the path
  <-list GetListCount 0 do
    #load the line graphic and paint it red
    Self I "Custom0" SetImage
    Self I 255 0 0 255 SetImageColor
    
    #unpack the coordinates (a=y*w+x)
    <-list I GetListElement dup ->a
    <-a MapWidth mod <-a MapWidth div CellToPixel ->y ->x
    
    #move the line between the last and current coordinates
    Self I 
        <-x <-oldX add 2 div <-x0 sub
        <-y <-oldY add 2 div <-y0 sub
        0 SetImagePosition
    #use arctan to get the slope of the line
    Self I 
        <-x <-oldX sub
        <-oldY <-y sub atan2
        SetImageRotation
    #stretch the line to the appropriate length
    Self I .25
        <-x <-oldX sub dup mul
        <-y <-oldY sub dup mul add sqrt 22 div
        SetImageScale

    #store the old coordinates
    <-x ->oldX    
    <-y ->oldY    
  loop
  
crpl/docs/pathfindterrain.txt · Last modified: 2021/02/05 11:45 by Karsten75