Give each group the waypoints you want, including the retreat. Use switch triggers to control which waypoint the group is on. There is a switch trigger tute if you need it. The triggers can be fired by any condition you want, casualties, enemy present in a certain area, combination of both, whatever. But don't use == for number left in the group, use <= in case an explosion or something takes the number from 5 to 3 in one go. Almost never use == in OFP.