r/howdidtheycodeit • u/fr032 • Jan 13 '26
OGame-like combat system
For those unaware OGame is a space simulation-strategy kind of game, all of the gameplay happens without any kind of visuals, is just mostly numbers.
The combat system is fairly straightforward.
Basically, fleets and defenses have: Weapon Power (aka Weaponry), Shield Power (aka Shielding), and Hull plating, and Rapid Fire.
In each round, all participating units(defenses+ships) randomly choose a target enemy unit.
For each shooting unit:
-If the Weaponry of the shooting unit is less than 1% of the Shielding of the target unit, the shot is bounced, and the target unit does not lose anything (i.e. shot is wasted).
-Else, if the weaponry is lower than the Shielding, then the shield absorbs the shot, and the unit does not lose Hull Plating: S = S - W.
-Else, the weaponry is sufficiently strong, i.e. W > S. Then the shield only absorbs part of the shoot and the rest is dealt to the hull: H = H - (W - S) and S = 0.
-If the Hull of the target ship is less than 70% of the initial Hull (H_i) of the ship (initial of the combat), then the ship has a probability of 1 - H/H_i of exploding. If it explodes, the hull is set to zero: H = 0. (but it can still be shot by the other units on this round, because they already target it.)
-Finally, if the shooting unit has rapid fire (with value r) against the target unit, it has a chance of (r-1)/r of choosing another target at random, and repeating the above steps for that new target.Initially, I thought simulation was done on a per-unit basis as this is what this wiki seems to indicate.
However, when trying to code something like this you realize the amount of operations become quite large, it is not uncommon to find players with a few million ships fighting another player with a few million defenses + ships.
I've even found an attack that totalled 500M units total.
Not only keeping 500M units in memory would be nuts (even if it's a simple struct of, say, 10bytes each), but also doing a foreach for 500M units seems insane.
So I'm guessing (and chatgpt seems to indicate the same) that all the stats are somewhat aggregated and this is simulated on a large scale.
Say, if you have an attacking fleet (AF) of 500 fleets of type A against a defense of 500 type A (D1), 500 type B (D2) and 500 type C (D3) you would calculate the chance of AF hitting D1, D2 or D3, then simulate the rapid fire, run the weaponry/hull calculation, delete the appropiate amounts of ships based on a global hull value, etc.
Does this seem reasonable? I find it might be too complicated to write all of this as a series of math calcs while also maintaining the randomness of the combat system (sometimes you might win a battle that was, say, 30% w, 40% d, 30% l).
I guess this is kind of a trivial problem in some area of math and I'm just not aware of it, got any recommendations and resources to read/learn about this? ty!
4
u/EagleNait Jan 13 '26
There's many open source recreations of ogame. But yes the solution is batching, predictable math and other fancy techniques
4
u/KittensInc Jan 13 '26
I'm not quite convinced it isn't done the "dumb" way.
Normally this sort of thing could indeed be done in smarter ways. If you want to know the sum of 10.000 dice rolls you don't have to actually do 100 dice rolls: probability theory tells us that the expected value follows a normal distribution, so we could just sample the formula for that instead.
The problem is that the math here is extremely complex. Rapid fire allows for essentially infinite recursion, so there's a small-but-nonzero possibility that a single ship will kill thousands of rocket launchers in one round. Properly modeling the probability of all the possible outcomes sounds virtually impossible to me.
If you're worried about memory and not as much about compute, you could normally also keep track of the count at each unique value. If there are only 10 health values possible, storing a unit count for each of those values is a lot more efficient.
The problem is that the number of health values is also extremely large. For example, a Death Star has 900.000 Hull Plating. There's a small but nonzero probability of 1 DS ending the round with 1 health, 1 with 2 health, ..., 1 with 899.999 health, 1 with 900.000 health. So you still have to keep track of nearly a million unique values.
On the other hand, bruteforcing it is borderline doable. Sure, the simulation would require a few gigabytes of RAM, but it isn't very complicated: input and output are tiny, and the simulations can probably be calculated in advance. You could just have a single beefy server work through a queue of pending battles. If you can get the per-unit calculation cost down to a handful of cycles, then looping over them all isn't a huge problem with CPUs running at several GHz.
1
u/leorid9 Jan 13 '26
For the rapid fire, you don't calculate it for one unit and apply it to all other units, you calculate it once for all units as a percentage - within a percentage range (e.g. "bonus damage of 40-60%", and not "0-4 extra targets hit per attack x 5 Million")