(From another thread, but I think it suits this one better:)
ORIGINAL: Shannon V. OKeets
This monitor stuff is already annoying me in other places - and it is quite necessary in those other places. For example, changing who controls each hex whenever a player moves a land unit, and redetermining supply in the immediate vicinity whenever a unit moves. I am still puzzling over how best to do the latter. I would like the displayed supply status for each unit to be continuously correct, yet I do not want to have to redetermine supply for every unit on the map every time a unit moves. For instance, theoretically, moving a unit on one continent could put units on another continent out of supply.
I am not a programmer and don't have much experience with handling (potentially) large amounts of data in a fast way, but a few thoughts on this anyway:
Supply could be divided into basic supply lines (BSL) (most units) and railway supply lines (RSL)(secondary supply sources - SSS).
In general - when a hex (or sea area) changes control:
for the side which loses control of the hex: no supply needs to be checked for units already out-of-supply.
for the side taking control of the hex: no supply needs to be checked for units already in supply
Moving units:
Whenever a unit is moved from an enemy ZoC, supply needs to be checked, as enemy ZoC would then prevent supply to be traced through that hex. And vice-versa when moving into a previously empty hex within enemy ZoC
Whenever a SSS is moved, supply needs to be checked, as the SSS move may put some units into supply and some out of supply.
Combats:
Units been killed: if a SSS, supply needs to be checked.
hexes changing control and units moved - as above.
Some different approaches to the problem:
1.
Whenever a hex changes control, supply for every SSS which could have traced a RSL through the hex is rechecked. Depending on how fast the supply-path-check routine works and how many SSSs in play (late war maybe 50 or more (?) ) this may be somewhat timeconsuming.
Then check BSL for all units of that side within 4 hexes of the hex (or fewer, depending on wheather) to adjust for potentially new BSPs.
These checks could be made for one side first, and then for the other side, if preferred.
Advantage of this approach is that no data about supply paths need to be stored. Disadvantage: it may (quite likely) turn out to be to time-consuming.
2.
Store data about current supply path. Some ideas:
When 'main' check on supply is done (at least start of turn, but maybe at start of each phase), data about each units supply path is stored.
For non-SSS: to each unit, attach a list of up to 4 numbers, indicating the hexes of its BSL (maybe this is not necessary, since the BSL is only 1-4 hexes long, depending on wheather, and could probably be quickly determined directly without having to rely on stored data).
For each SSS: this is more difficult, since each RSL can potntially be very long, and cover a lot of hexes.
One way (a): to each SSS is assigned a list of hexes, like the one for non-SSS.
Another way (b): to each hex is assigned a list with the SSSs tracing a RSL through that hex (or for most hexes, no list is assigned, since no RSLs are traced through)
Whenever control of a hex is changed:
rechecking RSLs:
if (a): go through all SSSs lists and see if the hex is included - if it is, the RSL for that SSS needs to be recalculated.
(b): go through the list assigned to the hex, and recalculate for those SSSs.
In both cases: if a SSS traces to another SSS, which has been cut off, supply for the first SSS needs to be recalculated. If a SSS has been cut oos, recheck BSL for all friendly units within (up to) 4 hexes from that SSS.
After supply of SSS has been determined, BSL for each unit (not checked for already) within (up to) 4 hexes of the hex which changed control i checked.
If a list is attached to each unit, only check if the hex is on that list.
Moving units:
Since moving a SSS can lead to significant changes in supply, a list could be assigned to each SSS, indicating which units (including other SSS) have traced a BSL to it. When the SSS have been moved, BSL then has to be rechecked for those units (to see if they are still in supply), as well as for all currently oos units within (up to) 4 hexes from the new location of the SSS (to see if they get into supply).
With the lists mentioned above, maybe some kind of cross-reference are required, so both are available: a list for each SSS of which units it supplies, and a variable for each unit indicating from which SSS it gets its supply. That allows for quick changing of 'focus' from one to the other, eg when determinig a new supply path.
In general: when the 'main' check on supply is done (going through all units and determining each ones supply path), only one supply path is stored in the lists/data, even if there are many possible ones. The main thing here is to decide whether a unit is oos or not. Afterwards, as long as that supply path is not affected by movement, hexes changing control, combat losses etc. then that unit is still in supply, so no need to waste time on rechecking for that unit, every time.
Depending on how time consuming it is:
since 'main' check is not done during movement etc. maybe it would be worth it to find a 'good' supply path for a unit to be stored in the data set - ie. not just the first one which allows for supply, but maybe (if available) one that is less likely to be cut by enemy movement, combat etc. That will reduce the chance that this 'safer' supply path will be cut off during the impulse.
This is quite a complex problem - I probably haven't covered all possibilities in the above - and again, this is just some random thoughts - don't know how well they will work if implemented.