Lanchesters' Battle Simulator
Ever since I learned about Lanchesters' laws from this great (!) Youtube video I have always been very eager to run my own simulations in code to see if the law holds up. In short, the law states that the rate of which your army looses units depends on the number of units in the opposing army.
This is a very simple model that doesn't take into account many factors but you can imagine a fun exercise in Starcraft or Age of Empires with this in the back of your mind. With the advent of LLMs that might help one do some recreative programming ... I was wondering if I needed a big game engine at all for this sort of thing or if maybe I could just simulate this directly in the browser.
Thats how I started toying with the idea, and here's where I ended up:
Notice how a small difference between the armies can totally snowball into a victory for one side. This is the essence of Lanchesters' law.
The change for army \(A\) depends on the size of opponent army \(B\). The formulas have a lot of good reasoning behind it, but at the same time it doesn't really capture what I am seeing in this simulation. As you simulate you will notice moments where the battle just completely flips due to the location of the units. If there is ever a snowball of units on one side, it can really eat up the other side if the units are spread out. Divide and conquer does happen.
The simulation has it's flaws, mainly because units can just walk through each other and there is no attack range whatsoever. So I even made a version with proper collision detection to prevent units from walking through each other and the same effect holds there as well. Lanchesters' law is still just a formula, and simple simulations can already show that it is an approximation at best.
These kinds of projects
This was a fun brainfart and I am happy that I was able to get it to run so quickly. The whole exercise, including this blog, took me about two hours. Granted, I know my fair share of vanilla JS and webstuff, without that knowledge I wouldn't have been able to steer it when I needed it. That said, if I would have done this from scratch it may have taken me a full day. Not every project should be built with an LLM but it does feel like this kind of simulation stuff kind of hits the sweet spot.
ps. If you felt that this was interesting, you may enjoy this article about Lancesters' law in the original Starcraft game.