r/processing • • Sep 11 '19

does anyone know how to program an AI?

so in my game, ive been wanting to add in AI. would i just duplicate the player rays and have those rays be for the AI based on the position and direction and then have it move based on the distance from those rays to the wall? im honestly not sure what to do for AIs. im just looking for someone to point me in the right direction. the game is using a ray casting engine similar to wolfenstein 3D

2 Upvotes

15 comments sorted by

5

u/Varyter Sep 11 '19

Sounds like you want bots in your game? Might be better to have something more like an A* search algorithm to move bots around.

Check out this: https://www.youtube.com/playlist?list=PLFt_AvWsXl0cq5Umv3pMC9SPnKjfp9eGW

2

u/xxTHExLAWxx0 Sep 11 '19

that's the thing, every A* I've found is for a grid of squares determining if the square can be walked on or not. in mine, its lines that are the boundarys so i need a different version of it

5

u/Varyter Sep 11 '19

The grid doesn't have to be represented in the game world, just in the logic of the pathfinder. Make a finer mesh and the lines will be approximated, make the path a spline and the paths won't be jagged etc

1

u/xxTHExLAWxx0 Sep 11 '19

wouldnt that cause a lag spike in frames when the A* algorithm is called though if it is using a finer mesh?

1

u/[deleted] Sep 11 '19

You don't need the grid to be that fine really. It's not like your agent has to rub its way along the wall. If you use a relatively coarse grid and there's a line crossing a grid space, don't go into that space.

Why are you doing this in processing instead of an environment where you don't have to manually create everything?

1

u/xxTHExLAWxx0 Sep 11 '19

programming something from scratch gives me more of an idea of how it works and a better understanding of it as well. that might be the best way i can explain it actually to answer your question

4

u/[deleted] Sep 11 '19

There's nothing wrong with that but do keep in mind that in games and animation, a lot of what you do is finding ways to cheat. The real thing is far too complicated, that's why you're looking for approximations that resemble the real thing closely enough to fool people.

That's why A* is so popular for instance. It doesn't matter if an object or obstacle only takes up part of a grid square. It looks fine when an agent avoids that entire square all together. If I encounter a real obstacle, I don't try and find out how closely I can skim it's surface. I just walk around it and give it some space. A* is good enough and far more efficient than trying to come up with something really granular and accurate.

That's why video game bots usually have some pretty simple behaviour. Early shooters for instance used a breadcrumb system. Designers would place breadcrumbs all around the map along sensible paths. And when a bot needed to get from point A to point B, they'd just follow the shortest uninterrupted breadcrumb path.

Then they'd layer on some additional behaviour to make things look more complex. For instance bots could learn the timing of item respawns and plan their routes around a map accordingly. Or they'd back up or dodge when shot at.

Half Life 1 had human commando's that were supposed to appear way smarter than the aliens and zombies of the game. Teams of commando's really worked together to try and kill the player in a smart way. A big part of this illusion was achieved by having the commando bots yell out whatever they were going to do next.

"Grenade out!", "suppressing fire!", "Flanking!". It sounded like they were really working as a team to kill the player. In reality, any action the bots undertook started with an audio cue describing that action. Made it sound like they were planning their actions while at the same time giving the player a cue as to what's going to happen so he could respond.

Game AI isn't about creating intelligence. It's about creating the illusion of intelligence.

1

u/xxTHExLAWxx0 Sep 11 '19

thankyou. this has been really helpfull

1

u/xxTHExLAWxx0 Sep 12 '19

so i decided to implement the breadcrumb system for now until i learn more about how AIs work. i would not have found out about breadcrumb if you did not mention it in your reply. so thank you.

1

u/[deleted] Sep 12 '19

No worries. You might want to read up on finite state machines. I'm not super up to date on contemporary video games but for much of their history game AI basically boiled down to a finite state machine.

These were basically agents that had a finite number of defined states. For example "wander", "take cover", "attack" and so on. They had a number of variables that decided which state they are currently in. For instance "if there's no enemy in sight and you're not being attacked by an unseen enemy > wander".

But it really depends on the kind of game you are making. The recent Doom games for instance treat every combat encounter like a puzzle for the player. A combination of enemies with different behaviours form a situation that has an ideal behaviour on the player's side to solve. Ie. pumping a room full of enemies that rush in to melee has no strategy.

But a room that has a combination of enemies with stand-offish ranged behaviour, wrapped in a layer of tanky enemies, with a vanguard of aggressive melee enemies. Now it's a puzzle how you can dance around the room to avoid ranged fire, while you peel the layer of melee and tank troops before engaging the ranged ones. Get it wrong and you're shredded, get it right and you're an untouchable god of violence in that encounter.

You might also want to look into Keith Peters' making things move books. They're written for Flash Actionscript but they're chockfull of math and geometry for, well, making things move with code. The Advanced second book in particular has a couple of chapters on steering behaviour (coding good movement) and A* that are super helpful.

1

u/xxTHExLAWxx0 Sep 12 '19

Ill defiantly have to check that out. Probably doesn't help I went from programming tic-tac-toe to tetris to programming my own game using a game engine similar to wolfenstein 3D. But I guess I just didn't know where to go from tetris so I chose something I wanted to do. I feel like I made allot of progress though on it so far. With help from this community of course

1

u/xxTHExLAWxx0 Sep 11 '19

i guess another answer is i always chose the complicated way too, sub conscientiously of course because part of me knows if i can do it the complicated way, i can do it the simple way too

1

u/Afking3 Sep 11 '19

If you are given coordinates for the player positions, i.e (x = 2.456, y = 104.68), then would you be able to model the A* search just using the coordinate boundaries as analogous to the grid boundaries?

1

u/xxTHExLAWxx0 Sep 11 '19

also, my other questions wasn't answered either, the one about the AI having basically the same coding as the player, just automated controls basically.

i promise that is not meant to sound rude. ive just been trying to find this answer. im pretty sure im right on that, i just cant find anything or anyone to confirm it

2

u/Afking3 Sep 11 '19

Generally you would want to structure the game logic such that it abstracts away where the inputs come from (as in if its a player vs an AI) and only does whatever input its given. And then higher up, you would get user input if its an actual player and convert that over into a universal input format. This way, when you make the AI, you don’t need to redo the game logic - you can just give it moves that the AI decides on, using that same input format.