RandomWalkUntil( hole-detected || connection-slot-seen )

if [ hole-detected ]# INITIAL STAR MORPHOLOGY - Seed (Hole detected before any connection slots seen)
  OpenConnSlot(0)                 # Attract four child nodes in turn to create star.
  SendRuleID(1)
  OpenConnSlot(2)
  SendRuleID(1)
  OpenConnSlot(6)
  SendRuleID(1)
  OpenConnSlot(4)
  SendRuleID(1)
  SetLeds( 'red' )
  WaitTimeout(2)
  SetLeds( 'off' )
  SendSignal('front-left')                # Signal to four child nodes that we are ready for next morphology
  SendSignal('back-left')
  SendSignal('back-right')
  SendSignal('front-right')
  OpenConnSlot(3)                # Attract child node to start line morphology
  SendRuleID(2)
  WaitTimeout(9)                    # Wait until no connection slots are seen, then light up in red.
  SetLeds( 'red' )
  WaitSignal('back')             # Wait for signal that rest of swarm is ready to reconfigure
  SetLeds( 'off' )
  OpenConnSlot(1)                # Attract child node for square
  SendRuleID(6)
  WaitTimeout(9)                    # Wait till no more connection slot seen
  WaitSignal('back')             # Wait for signal that rest of square is ready to reconfigure
  SetLeds( 'red' )                    # Light up in red
  WaitTimeout(2)                    # Pause for visual effect
  SetLeds( 'off' )
  SendSignal('left')               # Signal both child nodes to start reconfiguration sequence for star morphology
  SendSignal('back')
  OpenConnSlot(0)                # Recreate the star
  SendRuleID(7)
  OpenConnSlot(2)
  SendRuleID(7)
  OpenConnSlot(6)
  SendRuleID(7)
  OpenConnSlot(4)
  SendRuleID(7)
  SetLeds( 'red' )
  StopExecution()
fi

SearchForConnSlot()                       # Connection slot seen before hole detected, so search for connection slot...
ReceiveRuleID()                       #      to become part of existing star, square or line morphologies.

if [ receivedruleid == 1 ]                  # INITIAL STAR MORPHOLOGY - Leaf Node
  WaitTimeout(9)                     # Wait until no connection slots seen, then light up in red for visual effect
  SetLeds( 'red' )                       
  WaitSignal('front')              # Wait for signal from parent to start reconfiguration into line
  SetLeds( 'off' )
  CallFunction("disconnect_reconnect")  # Disconnect, then start looking for new connection slots
  CallFunction("line_node")    # Execute code for line morphology child nodes
  CallFunction("star_or_square_node")  # Execute code for square morphology child nodes
endif

CallFunction("line_node")
CallFunction("star_or_square_node")

StopExecution()


FUNCTION: line_node

if [ receivedruleid == 2 ]                  # LINE MORPHOLOGY - Second robot in line
  OpenConnSlot(3)          # Attract child node to continue line morphology 
  SendRuleID(3)
  WaitTimeout(9)              # Wait till no more connection slots seen, then light up red for visual effect
  SetLeds( 'red' )
  WaitSignal('back')       # Wait for signal propagating back up line to commence reconfiguration
  SetLeds( 'off' )
  SendSignal('front')         # Propagate reconfiguration readiness signal on up line to parent node
  OpenConnSlot(1)          # Open a slot to start forming square morphology
  SendRuleID(6)
  SendSignal('front')         # Send signal to parent node that this part of the square morphology is complete
  SetLeds( 'red' )              # Visual effect
  WaitSignal('front')       # Wait for instruction from parent to reconfigure into star morphology
  SetLeds( 'off' )
  SendSignal('left')         # Propogate reconfiguration instruction down tree to child.
  CallFunction("disconnect_reconnect") # Disconnect, then looking for new connection slots to become part of star morphology
  CallFunction("star_or_square_node")  # Execute
  StopExecution()
endif

if [ receivedruleid == 3 ]                  # LINE MORPHOLOGY - Third robot in line
  OpenConnSlot(3)                # Attract child node to continue line morphology
  SendRuleID(4)
  SetLeds( 'red' )                    # Visual effect
  WaitSignal('back')             # Wait for signal from child to commence reconfiguration
  SetLeds( 'off' )
  SendSignal('front')               # Propagate reconfiguration readiness signal on up line to parent node
  CallFunction("disconnect_reconnect") # Disconnect, then start looking for new connection slots
  CallFunction("star_or_square_node")
endif

if [ receivedruleid == 4 ]                  # LINE MORPHOLOGY - Fourth robot in line
  SetLeds( 'red' )                    # Visual effect to show pattern completion
  WaitTimeout(2)                    # Leave morphology in place for given time period
  SetLeds( 'off' )
  SendSignal('front')               # Send signal to parent that morphology is ready to reconfigure
  CallFunction("disconnect_reconnect") # Disconnect, then look for new connection slots (to become part of star morphology)
  CallFunction("star_or_square_node")
endif

StopExecution()

END_FUNCTION


FUNCTION: star_or_square_node

if [ receivedruleid == 6 ]                  # SQUARE MORPHOLOGY - Leaf Node (one of two attaching robots that were not already connected from line morphology)
  WaitTimeout(9)                    # Wait till no connection slots, then light up in red for visual effect to show morphology completion
  SetLeds( 'red' )
  WaitSignal('front')             # Wait for instruction to reconfigure from parent
  SetLeds( 'off' )
  CallFunction("disconnect_reconnect")  # Disconnect, then look for new connection slots (to become part of star morphology)
  SetLeds( 'red' )                    # Visual effect to show morphology completion
  StopExecution()
endif

if [ receivedruleid == 7 ]                  # FINAL STAR MORPHOLOGY - Leaf node
  WaitTimeout(9)                    # Wait till no more connection slots seen, then light up red for visual effect
  SetLeds( 'red' )
  StopExecution()
endif

StopExecution()

END_FUNCTION


FUNCTION: disconnect_reconnect

  Disconnect()
  MoveAwayTimeout(10)
  SearchForConnSlot()
  ReceiveRuleID()

END_FUNCTION


syntax highlighted by Code2HTML, v. 0.9.1