|
| 1 | +# Sequences |
| 2 | + |
| 3 | +A __Sequence__ ticks all it's children, from left to right, as long as |
| 4 | +they return SUCCESS. If any child returns FAILURE, the sequence is suspended. |
| 5 | + |
| 6 | +Here we introduce two kinds of TreeNodes: |
| 7 | + |
| 8 | +- SequenceNode |
| 9 | +- SequenceStarNode |
| 10 | +- SequenceAllNode |
| 11 | + |
| 12 | +The best way to determine which one should be used is to ask yourself: |
| 13 | + |
| 14 | + Q: "What should I do if one of the childs returns FAILURE?" |
| 15 | + |
| 16 | +Use __SequenceNode__ if you answer is: |
| 17 | + |
| 18 | + A: "Restart the entire sequence" |
| 19 | + |
| 20 | +Use __SequenceStarNode__ if, instead, the answer is: |
| 21 | + |
| 22 | + |
| 23 | + A: "Try again to execute the failed child. |
| 24 | + Do not re-tick children which succeeded already." |
| 25 | + |
| 26 | +__SequenceAllNode__ is used when you want all the children to be ticked at least |
| 27 | +once. If any of them failed, the SequenceAllNode returns FAILURE. |
| 28 | + |
| 29 | +The shared logic is: |
| 30 | + |
| 31 | +- Before ticking the first child, SequenceNode becomes __RUNNING__. |
| 32 | +- If a child returns __SUCCESS__, it ticks the next child. |
| 33 | +- If the __last__ child returns __SUCCESS__ too, all the children are halted and |
| 34 | + the SequenceNode returns __SUCCESS__. |
| 35 | +- If a child returns __RUNNING__, Sequence suspends and returns __RUNNING__. |
| 36 | +The next time it is ticked, it will use the same index. |
| 37 | + |
| 38 | +The three Sequences differ in what they do if a child returns FAILURE. |
| 39 | + |
| 40 | +## SequenceNode |
| 41 | +If a child returns FAILURE, the sequence returns FAILURE. |
| 42 | +Reset the index and halt all the children. |
| 43 | +The entire sequence will be executed again at the next tick. |
| 44 | + |
| 45 | +??? example "See the pseudocode" |
| 46 | + ``` c++ |
| 47 | + // At the beginning, start from first child |
| 48 | + if( state != RUNNING) { |
| 49 | + index = 0; |
| 50 | + } |
| 51 | + state = RUNNING; |
| 52 | + |
| 53 | + while( index < number_of_children ) |
| 54 | + { |
| 55 | + child_state = child[index]->tick(); |
| 56 | + |
| 57 | + if( child_state == RUNNING ) { |
| 58 | + // Suspend execution and return RUNNING. |
| 59 | + // At the next tick, index will be the same. |
| 60 | + state = RUNNING; |
| 61 | + return state; |
| 62 | + } |
| 63 | + else if( child_state == SUCCESS ) { |
| 64 | + // continue the while loop |
| 65 | + index++; |
| 66 | + } |
| 67 | + else if( child_state == FAILURE ) { |
| 68 | + // Suspend execution and return FAILURE. |
| 69 | + // index is reset and children are halted. |
| 70 | + state = FAILURE; |
| 71 | + index = 0; |
| 72 | + HaltAllChildren(); |
| 73 | + return state; |
| 74 | + } |
| 75 | + } |
| 76 | + // all the children returned success. Return SUCCESS too. |
| 77 | + state = SUCCESS; |
| 78 | + HaltAllChildren(); |
| 79 | + return state; |
| 80 | + ``` |
| 81 | + |
| 82 | + |
| 83 | +## SequenceStarNode |
| 84 | + |
| 85 | +If a child returns FAILURE, the sequence returns FAILURE. At the next tick, |
| 86 | +the failed child is executed again. |
| 87 | + |
| 88 | +??? example "See the pseudocode" |
| 89 | + ``` c++ |
| 90 | + |
| 91 | + // index is initialized to 0 in the constructor |
| 92 | + state = RUNNING; |
| 93 | + |
| 94 | + while( index < number_of_children ) |
| 95 | + { |
| 96 | + child_state = child[index]->tick(); |
| 97 | + |
| 98 | + if( child_state == RUNNING ) { |
| 99 | + // Suspend execution and return RUNNING. |
| 100 | + // At the next tick, index will be the same. |
| 101 | + state = RUNNING; |
| 102 | + return state; |
| 103 | + } |
| 104 | + else if( child_state == SUCCESS ) { |
| 105 | + // continue the while loop |
| 106 | + index++; |
| 107 | + } |
| 108 | + else if( child_state == FAILURE ) { |
| 109 | + // Suspend execution and return FAILURE. |
| 110 | + // At the next tick, index will be the same. |
| 111 | + state = FAILURE; |
| 112 | + return state; |
| 113 | + } |
| 114 | + } |
| 115 | + // all the children returned success. Return SUCCESS too. |
| 116 | + state = SUCCESS; |
| 117 | + HaltAllChildren(); |
| 118 | + return state; |
| 119 | + ``` |
| 120 | + |
| 121 | + |
| 122 | +## SequenceAllNode |
| 123 | + |
| 124 | +All the children are executed at least once. |
| 125 | +If __any__ child returned FAILURE, |
| 126 | +the sequence is __not__ interrupted but the sequence itself will return FAILURE. |
| 127 | + |
| 128 | +??? example "See the pseudocode" |
| 129 | + ``` c++ |
| 130 | + if( state != RUNNING) { |
| 131 | + index = 0; |
| 132 | + at_least_one_failure = false; |
| 133 | + } |
| 134 | + state = RUNNING; |
| 135 | + |
| 136 | + while( index < number_of_children ) |
| 137 | + { |
| 138 | + child_state = child[index]->tick(); |
| 139 | + |
| 140 | + if( child_state == RUNNING ) { |
| 141 | + // Suspend execution and return RUNNING. |
| 142 | + // At the next tick, index will be the same. |
| 143 | + state = RUNNING; |
| 144 | + return state; |
| 145 | + } |
| 146 | + else if( child_state == SUCCESS ) { |
| 147 | + index++; |
| 148 | + } |
| 149 | + else if( child_state == FAILURE ) { |
| 150 | + index++; |
| 151 | + at_least_one_failure = true; |
| 152 | + } |
| 153 | + } |
| 154 | + // If any child failed, the entire sequence fails. |
| 155 | + state = at_least_one_failure ? FAILURE : SUCCESS; |
| 156 | + HaltAllChildren(); |
| 157 | + return state; |
| 158 | + ``` |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
0 commit comments