Problem 1 is actually 2 different problems.
- Detecting convoy death.
- Determining convoy route.
Detecting death is pretty simple either count number of convoy units still alive using on death event to decrement the counter or every 5-10 seconds test the number of living units in a convoy unit group. If dead you select a suitable town to spawn at and then create the required number of units.
Determining convoy route is less simple. Each town needs a list of valid destination towns from which the convoy could visit. When at a town and the convoy is about to depart, a town from the list of destination towns for that town is chosen at random and this becomes the convoys destination. To prevent it cycling between 2 towns due to RNG an extra requirement could be made that it cannot depart for the town it immediately came from, which would be implemented by filtering the destination town list into a temporary list to select from.
Once a destination is set the convoy is ordered to move to a point in the destination town. Every 15 seconds an AI style think trigger is run for the convoy. This tests if its guard units stray too far from the caravan and if so orders the caravan to stop and the guards to move to the caravan. It also tests if it has arrived at its destination (near the selected town point) and if so then goes into its waiting state.
Problem 2 is the waiting state. After the AI think routine determines the caravan is in a town it goes into a waiting state. This could be implemented by turning off the AI think routine and then using a 30 second timer with timer expiry turning back on AI think and selecting a destination town as described above. Another way to implement it would be a state machine inside the AI think routine itself and have a variable to track state (traveling, at town) and then a counter which is incremented every time the think routine is run until it reaches a value representing 30 seconds in which case it selects destination and begins.
Problem 3 is part of problem 2. Once it detects arrival at a town it then determines what units it has left. These are compared with the desired unit numbers and any difference is created somewhere in the town and added to the convoy group. Remember to periodically flush dead units from unit groups to keep them performing well.
Problem 4 would be a separate periodic trigger which changes the desired guard stock used above. It could be implemented as a simple increase guard count counters by 1 every few minutes, however this approach could potentially end up spawning so many guard that it causes performance problems. A better approach would be a lookup table of sorts that at certain time milestones more guards or new types of guards are added. If old types of guard are removed for new types of guard one could either remove them at the next arrival, or simply ignore them and hope they eventually die.
Multiple caravans would be implemented by instancing. Each caravan is a separate instance (index) with members stored in a set of arrays. AI think and such would loop through all caravan instances. Possible staggering could be employed if there are performance concerns for the AI think so each convoy is updated at a separate time.