How do you program AI? (And some more...)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

How do you program AI? (And some more...)

Post by redsled »

I know this is a very general question, so i'll try to make the scope smaller. If your coding AI, is it basically a set of if statements, or is something more complex and algorithmic along the lines of like path-finding? No idea where to start making fairly complex AI, such as a boss for example. I'm not saying I want to program a boss, but along the lines of lower level enemies, like the logic behind the curtain. Also, when dealing with multiple AI enemies, how does one go "organizing" all the weaknesses and strengths of the enemy in a file(ex: is weak-spot is the head)? Would multiple enemies be each seperately coded in different files, or would you create a base AI structure, then apply that to separate files (would there be any extra files at all?)?

Thanks a bunch,
-redsled
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How do you program AI? (And some more...)

Post by ivan »

redsled wrote:I know this is a very general question, so i'll try to make the scope smaller. If your coding AI, is it basically a set of if statements, or is something more complex and algorithmic along the lines of like path-finding?
There are many different approaches.
For action games, finite state machines are common and behavior trees are gradually becoming more popular.
For turn based games like chess, it's more about searching and evaluating positions.
redsled wrote:No idea where to start making fairly complex AI, such as a boss for example. I'm not saying I want to program a boss, but along the lines of lower level enemies, like the logic behind the curtain.
If you are making a simple platformer, a lot of AI could be scripted. In a sense that the boss may have 3-4 attacks and cycles through them in some way (choosing a certain attack at random or depending on some input condition).
redsled wrote:Also, when dealing with multiple AI enemies, how does one go "organizing" all the weaknesses and strengths of the enemy in a file(ex: is weak-spot is the head)?
Generally speaking, you want your AI to be 'generic'.
In a sense that some event happens in the collision/physics system and AI reacts accordingly.
Weakpoints could be implemented using 'triggers' and 'events' that are passed as messages to the AI.
redsled wrote:Would multiple enemies be each seperately coded in different files, or would you create a base AI structure, then apply that to separate files (would there be any extra files at all?)?
With behavior trees the code is divided into simple tasks and ordered in a tree-like structure.
It could be overwhelming if you're not experienced with programming.
Finite state machines are easier to understand.
With FSMs you could have an attack state that accepts certain parameters (like 'speed', 'weapon', 'accuracy') and tweak those to produce varying behaviors.
Inheritance could also be useful with finite state machines.
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

Re: How do you program AI? (And some more...)

Post by redsled »

Thanks! I've of seen a couple of terms like behavior trees around the web, but never really understood what they were used for. Also;
ivan wrote: With behavior trees the code is divided into simple tasks and ordered in a tree-like structure.
It could be overwhelming if you're not experienced with programming.
I'm an unexperienced coder, like one year of coding. And your point is very accurate, when getting a couple hundred lines of code in a single file, it's hard to not be overwhelmed with how much there is. What do other programmers, such as your self, not get overwhelmed? Do you efficiently comment everything or do you like structure everything out on a piece of paper?
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How do you program AI? (And some more...)

Post by ivan »

redsled wrote:What do other programmers, such as your self, not get overwhelmed? Do you efficiently comment everything or do you like structure everything out on a piece of paper?
Hey, this is a very common problem.
For starters, get used to to reading other people's code.
This will teach you what's good and bad because sooner or later you'll have to go back to your own code.
Looking back at my own code, it's hard not to cringe especially at some of the older stuff that I wrote.
I think it's hard to write clean code because Lua already provides so much that there are always simpler/more elegant solutions.
So ya, comments are good, clean and understandable code is even better. :)
And your point is very accurate, when getting a couple hundred lines of code in a single file, it's hard to not be overwhelmed with how much there is
Another thing you could try is to divide your code into domains as much as possible.
Graphics code goes in one file, input code goes in another file, game logic goes in a third file...
Whey you decouple your code as much as possible - the dependencies/weaknesses becomes apparent.

For example, you have some code that handles input from a keyboard:

Code: Select all

if love.keyboard.isDown('l') then
  player.x = player.x - player.vel*dt
end
Suppose that you want to add custom key mappings or support joystick input.
Ok, but I just want to change the input method, why do I have to bother with "player" too?
Hmm, does the input code really need to know anything about the global variable "player" at all...

These seem like trivial things, but when you divide your code into domains
it becomes more general and could be reused/expanded in the future.
Just some basic ideas, I'm sure others will have a lot more to add on the subject.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests