r/IndieDev • u/Kalabasa • Jul 01 '23
Article I made a potential-field based algorithm for implementing dynamic patrol behavior in stealth games
889
Upvotes
r/IndieDev • u/Kalabasa • Jul 01 '23
92
u/Kalabasa Jul 01 '23
Hey everyone! The algorithm assigns probabilities to each tile based on the player's location. It incorporates inference and Markov chains to update these probabilities as the player moves and AI guards make observations.
The cool part is that the algorithm creates organic and responsive AI behavior without predefined patrol routes. I guess it would be great for proc-gen levels e.g. in a stealth-based roguelike. Guards follow the highest probability nodes and exhibit chasing, investigating, and patrol behaviour.
I wrote a blog post about it which includes a demo where you can play around with the algorithm in action.
18
u/CarpenterMelodic4247 Jul 01 '23
I read your blog and this is great work. I understand what you're doing but now I'm trying to decipher how to code it myself in Godot as a learning exercise. How did you come up with this?
5
u/Delicious-Branch-66 Jul 01 '23
Looks amazing. Please share more details about it's implementation.
2
u/Wavertron Jul 01 '23
I just played the demo on your blog post, ran down to the bottom room and "hid" behind the horizontal 3 square wall. The guard entered the room, then left to check the far left corridor, then came back in, searched the room above, before finally looking behind the 3 square wall where I was hiding. Very neat.
However I noticed a bug. If I hide in the top 6 square room, the guard will eventually find me and stand just outside the doorway. If I then ran down the bottom of the maze, passing the guard who can see the direction I went, sometimes those blue probabilities squares "leak" past the guard behind him, in the opposite direction I went (top left corner). This being a deadend where I couldn't possibly be since the guard saw which direction I went. It seems to be related to the white scanning squares of the guard that clear the blue squares, when there's a gap it allows the blue prob calcs to leak past.
7
u/SamuraiSnoots Jul 01 '23
that's so cool!
8
u/supremedalek925 Jul 01 '23
Very cool. At least from this short clip, it looks like the agent is performing a very convincing searching behavior.
1
u/bomber8013 Jul 01 '23
Was he sniffing out the hero?
1
u/spectraldecomp Jul 02 '23
Very interesting! Can you elaborate more into how the probabilities are redistributed when the human is not found?
3
u/thebaconator136 Jul 01 '23
So it seems like the winning play is to use loops in the map to stay behind the guards. Cool way of making a dynamic system!
2
u/Due_Raccoon3158 Jul 01 '23
Very awesome! Great job!
2
u/MarkWantsToQuit Jul 01 '23
I'm the middle of implementing this for a hex map - never thought of rooms "cooling down" for, whatever reason, when last checked by the NPC. (Assuming that's what the blue getting deep means)
Looks good!!
1
u/swolehammer Jul 01 '23
Pretty neat man!
1
u/CarpenterMelodic4247 Jul 01 '23
Saved!
1
u/pLeThOrAx Jul 01 '23
Did you ever play that old pc game, I forget what it's called. Basically there are balls and you use right click and left click to construct walls. The goal is to trap all the balls while having each ball occupy the smallest area possible.
Basically I was thinking (if this was a game) you could have perhaps for every 3rd move the opportunity to construct a block/barrier. You could maybe do it turn for turn
1
u/Soumya_ketan Jul 01 '23
pp h,
1
u/Healter-Skelter Jul 02 '23
Is that Han Tyumi the confused Cyborg?
1
u/Coder_Arg Jul 02 '23
I think you made the AI "too smart". AI's in video games are supposed to be dumb, it's something for the player to feel awesome when outplaying. In this case, you "outplayed" the robot, but he was too smart and found you anyway.
https://www.youtube.com/watch?v=1FBGR6vmNeU