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

if [ hole-detected ]
  OpenConnSlot(0)                  # Seed the SQUARE morphology (since Hole detected before connection slot seen)
  SendRuleID(1)
  OpenConnSlot(2)
  SendRuleID(2)
  OpenConnSlot(6)
  SendRuleID(2)
  OpenConnSlot(4)
  SendRuleID(1)
  WaitSignal('front-left')                # Wait for first branch to report back completion
  WaitSignal('back-right')                # Wait for second branch to report back completion
  SendSignal('front-left')
  SendSignal('back-right')
  SendSignal('back-left')
  SendSignal('front-right')
  RandomWalkTimeout( 30 )
  OpenConnSlot(1)                   # Become seed for new 3-bot arrow morphology
  SendRuleID(9)
  OpenConnSlot(3)
  SendRuleID(9)
  StopExecution()
endif

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 ]
    OpenConnSlot(1)                 # Continue square morphology
    SendRuleID(3)
    OpenConnSlot(5)                 # Continue square morphology
    SendRuleID(2)
    WaitTimeout(1)                     # Allow completed morphology to stay in place for fixed time period
    SendSignal('front')                # Tell seed that my branch is finished (two child already attached => finished)
    WaitSignal('front')              # Wait for seed to tell me that other branch is also finished
    SendSignal('right')                # Tell second child to start reconfiguration
    SendSignal('left')                # Tell first child to start reconfiguration (reconfiguration => coordinated motion with this child)
    Disconnect()
    MoveAwayTimeout(10)
    OpenConnSlot(3)                 # Continue 3-bot arrow morphology
    SendRuleID(9)
endif

if [ receivedruleid == 2 ]
    WaitSignal('front')              # Wait for parent to tell me it is ok to disconnect
    Disconnect()
    SearchForConnSlot()                     # Search for open slot to become part of 3-bot arrow morphology
    ReceiveRuleID()
    StopExecution()
endif

if [ receivedruleid == 3 ]
    WaitSignal('front')              # Wait for parent to tell me he is ready for coordinated motion
    MoveAwayTimeout(10)
    StopExecution()
endif

if [ receivedruleid == 9 ]
    StopExecution()
endif

StopExecution()


syntax highlighted by Code2HTML, v. 0.9.1