Mittens 2018 will be in Boston this September. There are three spaces left, so check out the thread for details!

Author Topic: Coding Fight AI  (Read 352 times)

Coding Fight AI
« on: 30 Dec 2017, 02:19 »
Hi. I am creating a game in AGS and have recently finished creating the monster sprites in Aseprite. However, I am clueless as to how to code some sort of AI for a fight scene. I have a QFG kinda fight style in mind, how would I go about coding this? (I also skimmed the scripting tutorial, but still am confused as to how to code an AI).

Thank you.

PS In QFG the fight style angles are different from those of QFG4. One shows a front view of the villain whereas QFG4 shows a side scroll fight scene playing out. How would this be different in regard to code? (I have drawn all the villains from a side view in order to fulfill a QFG4 like fight scene).

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: Coding Fight AI
« Reply #1 on: 30 Dec 2017, 08:23 »
It can be done, but it is a complex and difficult task. Mage's Initiation (a QFG clone being made in AGS) spent well over a year just fixing bugs in their combat system. That's not counting the time it took to write it in the first place, mind you! And this is for an experienced AGS coder with several major games released. (Admittedly their system sounds like it's very complex, with a lot of different spells, weapons and enemies to deal with.)

So my advice for you is to not try it. Not for your first game, at least.

Turn-based fighting is much easier, and you can see examples of it in e.g. some of the Oceanspirit Dennis games.

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Coding Fight AI
« Reply #2 on: 30 Dec 2017, 19:06 »
Coding is not only about knowing the programming language, but first of all about writing algorithms and structuring your program.
Complex algorithms are usually broken into smaller parts, to make it easier to understand and code.

My biggest advice to anyone who begins to learn programming and wants to make something complex, like fighting in this topic:
First, write down what you'd like to have "on paper". At this stage there's no need to go into much details, just rough outline. What objects would you like to have on screen, how these objects act, how player is supposed to act.
Second, try to break the whole scene down to smaller tasks, and learn to make these one by one, separately. After you did, you will be able to combine complex scene out of those pieces.

For example, you want to have QFG-like combat. This involves a picture of a player that swings sword (or does other stuff) by player's command and picture of enemy that does actions by AI command. Essentially this means that there is some visual character that receives commands from a source. Source can be either player or AI script.

Try following: make a simplier scene (perhaps in a test game), where you have a character on screen, that runs animations when player presses a button. Learn how to test when action ends, so that player could not run new actions before that.

Then make another scene where same character is controlled by a script. Learn how to make it launch same actions randomly, by a timer.
It may be important to learn how to "detach" animation script from AI script, to make them independent of each other. This way you will be able to connect same AI script to multiple characters.

Next step: teach AI to react on opponent's actions. You do not have to have actual opponent for that. Implement "imaginary" opponent. For example, remember what opponent is doing (defending, attacking, etc) in a variable, put a text on screen telling that to make it easier for you, and let player change that imaginary action by key press. Make AI react to this variable: when you set it to "attack" - AI should defend, and vice versa (or other variants).

Fourth scene could be where two characters stand face to face. They may be even both controlled by AI. Make them fight with each other using techniques you learnt in previous three scenes.

After you succeed in this, move on to combat rules and GUI part, with health, winning and loosing conditions, and so forth.
« Last Edit: 30 Dec 2017, 19:08 by Crimson Wizard »

Coding a Fighting AI Algorithm Basics
« Reply #3 on: 20 Feb 2018, 05:53 »
So, even though this is my first time creating a game in AGS, I wish to code a fighting AI like that of the QFG series in my game (even though people have highly discouraged me from doing so).

In retrospect, there are so many variables to consider! First off, the player needs some sort of health point system, secondly, I need to ensure that the monsters know the player is the target variable. The latter is fairly intuitive, however I am somewhat clueless as to how to do a point system.

Furthermore, for my fighting AI ( I will not be utilizing any kind of magic, simply 5 basic commands). Advance, Retreat, Defend, Attack 1, and Attack 2. (Attack 1 and 2 are two different styles of attack)
The monsters will furthermore have to have similar commands, and randomly use them.

I have no idea where to even start going about this. Please help!
Thank you.

Khris

    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
Re: Coding a Fighting AI Algorithm Basics
« Reply #4 on: 20 Feb 2018, 09:51 »
I would like to highly discourage you from pursuing this, at least right now.

Creating something more complex like this works like creating any other complex thing: break it down into its parts, create those individually, then put them together.

The fact that you have no idea where to start means you simply don't have the required programming knowledge yet to pull this off. The only thing we can possibly do to help is to hold your hand through the entire development process, feeding you bits and pieces of code you'll need, explaining them at length.

This is useless.

Try to start simpler, much simpler. Place two characters on the screen. Show a GUI with a few buttons. Then have each button make the player character do something and have the enemy react.
Next, try to add health.

Slasher

  • slasher
    • I can help with AGS tutoring
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
    • Slasher worked on a game that was nominated for an AGS Award!
Re: Coding a Fighting AI Algorithm Basics
« Reply #5 on: 20 Feb 2018, 13:52 »
I have to agree with Khris....

You can still work on the game and add bits as you learn them..

The best way to learn things is to make other games that are far less complex and as your skills improve you can start making more advanced games. Eventually you will have learnt enough scripting skills to start building up your original idea.

It's like just passing your driving test and then going out and buying a super-charged Ferrari... Not the best of ideas..

Khris is a seasoned script'er and advice from him is gold... take note..

If you can't find what you want in the help file in AGS or here on the forums then ask a question here.

Good luck (nod)

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: Coding a Fighting AI Algorithm Basics
« Reply #6 on: 20 Feb 2018, 14:45 »
Vverano7, you have already asked this question on this very forum just a couple months ago.
http://www.adventuregamestudio.co.uk/forums/index.php?topic=55643.0

The answers will probably be the same this time.

I posted a big reply there, suggesting small steps to learn coding a combat system, so I will just citate myself.
(Although frankly I think the threads should be merged as duplicates)

EDIT: cut out citation because threads were merged by moderator.
« Last Edit: 20 Feb 2018, 17:33 by Crimson Wizard »

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: Coding Fight AI
« Reply #7 on: 20 Feb 2018, 17:22 »
(Although frankly I think the threads should be merged as duplicates):

Done.

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: Coding Fight AI
« Reply #8 on: 20 Feb 2018, 20:16 »
I agree completely with all the recommendations to break the problem down into small pieces and trying to get the basics to work first.

I will add one specific hint about AI, because I remember what a revelation it was when I learned about it: Finite State Machines.

This is a very simple but effective way to do enemy AI. Basically, you define a number of different states the enemy can be in – like your Advance, Retreat, Defend, Attack 1, and Attack 2 (though you may find that the logic requires some others) – and when the enemy should go from one state to another: this may be based on some event (e.g. player attacks), or it could be time-based. Often there is a random component. You can usually draw a little diagram showing the states and transitions. In each state, the enemy behavior is very simple: most of the "intelligence" comes from how it transitions between the states.

The classic example is a guard in a stealth/fps game, who might have these states and behaviors:

Patrol: Walk along a pre-set route
Chase: Follow the player and shoot at them
Seek: Walk around the nearby area, "looking for the player"
Dead: Be dead

With these transition rules:
Patrol: If killed, go to dead. If see player, go to Chase. If shot at, go to seek.
Chase: If killed, go to dead. If player dead, go to Patrol. If lost sight of player, go to Seek (with probability 1% per game loop; at 60 fps it will take about 1 second on average, with a random component)
Seek: If killed, go to dead. If see player, go to Chase. If shot at, reset timer. If not seen player for 20 seconds, go to Patrol
Dead: Don't go anywhere

A good way to code it in AGS is to make an enum for all the states, make a variable of that enum type to keep track of the current state, write a function with a switch/case statement that checks whether we should transfer to a different state, and call it every game loop from repeatedly_execute().