r/howdidtheycodeit • • 1d ago

what programming patterns are used in fighting games?

It seems like fighting games are pretty complex to program. I mean having an input that determines a move is the basis, but there seems like a lot of complication in terms of how to detect who hit who at what time and how one move is interrupted by another. Anyone have any experience with the topic, and can tell me about what strategies fighting game devs use to do this?

26 Upvotes

15 comments sorted by

28

u/HyperCutIn 1d ago

Dig into how Mugen characters are coded in the engine. It's not gonna be the exact same with other fighting games, but basically each character has a gigantic state machine that transitions characters into different states depending on different conditions. Input processing is handled in what's effectively one of Mugen's three "universal states" that are running at all times, as opposed to regular states that have limited transitions to other states. Of course, since certain moves have restrictions on when you can use them, they're limited by conditionals, drawn from Mugen's "trigger" code. Stuff like hit detection and gethit states are all under the hood for the most part, but I'd imagine they're handled similarly such that the engine can transition a character from almost any of their states (whether it be them idling, moving, attacking, etc.) into the appropriate gethit state.

2

u/hotchocolateisascam 1d ago

do you have any favorite resources to look into ? for characters/scripting in Mugen ?

6

u/HyperCutIn 1d ago

Best place is probably Mugen's official documentation... which I just found out the official link to it is dead. Unfortunate, but you can still access it when you download a base copy of the latest version of Mugen, as the documentation comes as part of the download. It also includes their sample character Kung Fu Man, where his code is also well documented and designed to teach you how to code in Mugen, and the structure of his code.

Another good place is MFG's Mugen Class resource: https://mugenguild.com/forum/mugen-class.415

Unfortunately, I believe that forum is no longer browsable without making an account, due to many web attacks over the years. But it basically goes over various parts of the documentation, but in more detail. Along with some tips and techniques for implementing certain features in characters and stages.

But beyond those, your other best bet is to download a character and look into their code to see how their author implemented them. As well as asking around in one of the Mugen discords focused on development. Though I imagine most developers have graduated onto working with Ikemen instead, which is an open source Mugen clone that the community has used to expand beyond Mugen's features.

3

u/WorkingMansGarbage 1d ago

Wouldn't the source code of the FOSS successor to MUGEN, Ikemen GO, be a good place to look, too?

11

u/Ashamed-Subject-8573 1d ago

Generally speaking, each animation has frames to it. And the frames have not just character data, but also "hurt boxes." Those are tested to see if they intersect the other character's "hit box," to determine a hit.

Honestly the more interesting thing to me is programming enemy AI. You can make it hard by cheating, but making actually fun-to-duel AI seems to be pretty hard.

1

u/hotchocolateisascam 1d ago

have you found any interesting sources on the subject of writing enemy AI?

4

u/wahnsinnwanscene 1d ago

One way I'd do it is to attach different behaviours to the opponent. It'll observe the environment. Once some condition is met it'll swap out the current behaviour with a another one. You could put a probability distribution to a set of behaviours to choose. That way you can scale the difficulty across levels. A hard mode means it'll choose the next attack immediately instead of backing off.

3

u/andeee23 1d ago

yep, another way to prevent "cheating" is to give the AI human-like delays in behavior and responses, so you can also scale difficulty by randomizing the scale of how quickly or slowly it'll respond. so sometimes it'll do the right thing like block, but do it slightly too late

3

u/WorkingMansGarbage 1d ago

I don't have them, but I recall reading a bit about Smash AI for instance and it's a lot simpler than it looks : it has a simple behaviour tree driven by the game state, saying things like "if in range, try short hop fair" and "if you get hit next frame, shield", and it has a random chance of actually performing that action based on its difficulty level. That's somehow enough for singleplayer purposes. It doesn't need to be complex.

1

u/wahnsinnwanscene 1d ago

Yes 2nd or 3rd order markov chains

3

u/WorkingMansGarbage 1d ago

Something that might help without needing to know technical details is learning more about the gameplay systems' more obscure aspects.

Like, for instance ; unlike what I thought, frame advantage is usually not determined by literally setting the number of hit/blockstun frames for a hitbox, at least not in Guilty Gear. Instead they give moves one of five Attack Levels that have their own values; the variance in frame advantage then comes from animation length. It's less control, but a lot less work setting individual variables as I would have done. 

Understanding a game system in and out as a player helps understand it on a technical level.

1

u/hotchocolateisascam 1h ago

totally, I agree with this sentiment/idea and that's prompted me to look into frame advantage in guilty gear.

13

u/jax024 1d ago

They’re essentially state machines that run on a tick by frame.

4

u/Ronin-s_Spirit 1d ago edited 1d ago

I think it's about coded mechanics and not programming patterns. One mechanic virtually any game uses is "state machines", which is a graph of what you are doing right now and what you can do next considering the state you're in.

And of course there will be a rendering loop and an event loop, the rendering has to run at a fixed framerate (or try to anyway) and the event loop can run as fast as it wants to because it doesn't actually show anything it just changes program state. Older games mushed together render and simulation (logic) so you get stuff like faster input processing in Warframe at higher fps, or more enemy spawns.

1

u/detroitmatt 1d ago

you start with a state machine for each character. the state machine's inputs include that player's controller inputs, whether they got hit this frame, and maybe a few other variables depending on the game. with each state you associate hitbox+hurtbox data. most interactions can be handled directly by the state machine. for some particularly unique movies, you need to bypass the state machine at the engine level.