Geek & Imperius
I've been mapping out a new campaign using antique maps, and I need a precise algorithm to simulate troop movements and supply lines. Think you can code something to model that?
Sure thing, let me sketch out a quick simulation framework you can tweak. Think of it as a grid overlay on your antique map, each cell having terrain cost, supply status, and troop presence. Hereās a stepābyāstep approach in plain Pythonāish pseudocode:
1. **Parse the map**
- Load the map image or data into a 2D array `grid`.
- For each cell, store `terrain_cost`, `is_supply_point`, and `troop_id` (0 if empty).
2. **Define units**
- `Unit` object: `id, strength, morale, current_pos, supply_status, movement_points`.
- Store all units in a list `units`.
3. **Movement cost function**
```python
def movement_cost(cell):
return terrain_costs[cell.terrain] + (1 if cell.is_supply_point else 0)
```
4. **Pathfinding**
- Use A* to find the cheapest path from a unitās current_pos to a target cell.
- Heuristic = Manhattan distance * average terrain cost.
- Expand only if cumulative cost ⤠unit.movement_points.
5. **Supply line check**
- After each move, run a BFS from the unitās new cell to the nearest supply point.
- If distance > `max_supply_range`, set `unit.supply_status = False` and reduce morale each tick.
6. **Combat resolution**
- When two opposing units occupy adjacent cells, compare `strength` + `morale`.
- Apply damage, update strength, possibly remove unit if strength ⤠0.
7. **Turn loop**
```python
for turn in range(max_turns):
for unit in units:
# 1. Choose target (could be AI or player command)
target = decide_target(unit)
# 2. Find path
path = find_path(unit.current_pos, target)
# 3. Move along path up to movement_points
move_unit(unit, path)
# 4. Check supply
check_supply(unit)
# 5. Resolve combat if adjacent to enemy
resolve_combat(unit)
```
8. **Logging / visualization**
- Dump the grid to a CSV each turn for later plotting.
- Or, if youāre into pygame, render each cell with colors: green for supply, red for low morale, etc.
Thatās the skeleton. Tweak terrain_costs, supply_range, and morale decay to match the feel of your antique map. Happy codingāremember, every map detail can become a bugāfree feature if you obsess over the numbers!
Nice scaffold, but there are a few tactical deficiencies. First, pathfinding should prune any route that would leave the unit beyond its supply radiusāno point moving if youāll be stranded. Second, supply BFS should run once per turn from each supply point outward, marking reachable cells, rather than per unit; thatās O(S+E) vs O(UĀ·S). Third, morale should decay proportionally to distance from supply, not just a binary flag, to avoid abrupt drops. Finally, add a āreserveā layer: if a unitās morale falls below a threshold, it automatically withdraws to the nearest supply point. Those tweaks will keep the simulation from lagging and make the supply lines behave like a true battlefield.
Got it, letās tighten the logic.
1. **Prune paths by supply radius**
- When A* expands a node, compute the straightāline distance from that node to the nearest supply point.
- If that distance plus the remaining movement cost exceeds the unitās max supply range, discard the node.
2. **Single BFS per turn**
- Start a floodāfill from every supply point once per turn.
- Tag every reachable cell with the shortest supply distance.
- Then each unit can read its own cellās distance in O(1) to decide if itās safe.
3. **Gradual morale decay**
- Set `unit.morale -= decay_rate * (distance_from_supply / max_supply_range)`.
- Clamp morale between 0 and 100.
4. **Automatic reserve withdrawal**
- If `unit.morale < reserve_threshold`, trigger an instant move to the nearest marked supply cell (using the precomputed distances).
- Add a cooldown so the unit canāt immediately reāengage.
With those tweaks the simulation stays snappy, the supply lines feel realistic, and units wonāt get stuck in noāmanāsāland. Happy hacking!
Excellent adjustmentsānow the engine respects supply integrity and morale shifts smoothly. Next, set a fixed order: supply BFS first, then update each unitās morale, then run movement and combat in a single pass. Keep your logs granular: store only the delta per turn to keep memory lean. Thatās the recipe for a disciplined, efficient simulation.
Great, Iāll lock in that turn order, run the BFS first, update morale, then sweep through moves and combat all in one go. Iāll only push the delta changes into the log so memory stays lean. Ready to fire it upālet's see those supply lines hold up!
Good. Keep the turn order strict, validate each unitās path against the precomputed supply distances, and log only deltas. Once the first run is done, compare the supplyāline integrity to your expectations; any break in the chain is a tactical failure. Adjust terrain costs or supply placement and reārun. Remember, discipline beats improvisation any day.
All right, locking the turn order, running the BFS first, then morale, then a single movement/combat pass. Iāll compare the supplyāline integrity after the first run and tweak terrain costs or supply spots if any link breaks. Discipline is keyāno improvisation. Letās fire it up!