Friday, April 9, 2021

Project Breach #5 - "Pawn AI, Gadgets and Performance"

 Project Breach #5
"Pawn AI, Gadgets and Performance"


Pawn AI
The new wandering system was improved in the exact way that I talked about in the last update, and it worked perfectly. Pawns now convincingly wander around, making sure not to drift too far from their starting place.




But pawns taking a casual stroll while bullets flied past? Probably not the best idea. So I implemented a series of different behaviours pawns will engage in depending on the situation.

Pawns can now seek & take cover. This may happen because they've been shot, or because they think that they might be. In this example you can see the pawn at the bottom left seeks cover after they've killed the threat, just in case someone else is around.




Pawns can also hear gunshots. This one was one of the more important changes as before, if a pawn didn't have line of sight over an enemy, it would never know they were there, even if all his allies were battling it out in the next room over.

Pawns can also decide to forget cover altogether and charge the enemy. This is used as more of a reinforcement tactic, that while your allies are pinned down, you can afford to run up and go for quick clean shots on whoever is attacking them.




And finally, pawns can investigate. If they've had line of sight or been shot they know exactly where their attacker is. But if all they heard was a gunshot, they'll only know the general area of where the attacker might've been but not exactly. If they show up and find no one there, they'll look around for awhile before eventually returning to normal.




Comparing this new system with what we had before and you truly get to see how much it changes the gameplay. Only 2 weeks ago, a pawn would just stand there, practically waiting to be shot. Now they move about, try to better defend themselves with cover, assist their allies, and work together to hunt down the one attacking them.

At some point I'd like to probably redo the pawn AI, I've learned so much since first starting on it and I think it's a little messy but for the demo I'm going to leave it how it is.

Gadgets
The gadgets system was something I just didn't want to do. And in hindsight, I really don't know why. There was just some mental block preventing me from wanting to even start on it. But with the game getting so close to demo, I finally pulled the trigger to do it. And with most things that you put off, it was much easier to do then how you thought it'd be inside your head.

I knew in-advance what gadgets I'd be making with this system so I was able to quickly identify the 2 types. Placed and thrown. After that I began adding 2 types of functionality: Triggers and Trigger Effects.

Triggers were what made the gadget go off. Right now that just means a timer, but in the future could mean things like impacting with a wall or being shot. Trigger effects were what happened when the gadget was triggered. I added 3 "Damage pawns in a radius", "Damage mod objects in a radius" and "Stun pawns in a radius". And with these 3 effects I was able to make the 3 gadgets pawns will be using:
1. Breach Charge (Damage Pawns & Damage Mod Objects)
2. HE Grenade (Damage Pawns)
3. Flashbang (Stun Pawns)

You can't yet order pawns to use these gadgets (although I'm working on that already), but here's me testing them by spawning them in the editor:






Performance:
Massive improvements have been made to how well pawns perform. The main performance draw was when pawns were within range of each other, they had to perform a line of sight check. This check can be expensive when so many pawns are doing at once, the game (on my PC) would drop below 144fps at ~80 pawns.

This annoyed me because I really want the game to be able to support a lot more. I used Unity's built-in deep profiler and discovered that, I was right, 90% of the pawns performance drop was because of that line of sight check. So I spent time doing dedicated tests to improve one part of it at a time & measure the change, the result was that after only a few hours of testing I had managed to massively reduce the performance use of the line of sight check. How much is massive? 47% less milliseconds to calculate. That's pretty massive, and I've been considering other changes that could help improve it further.

While debugging I also discovered that the pawns movement script was using up more resources than it should be. It wasn't using up much (16 pawns took 0.1ms) but I knew that number could still be improved. It turned out it was a similar performance problem that was plaguing the line of sight check, only this time it was caused by the system that made so pawns could auto-open doors. After implementing my solution 16 pawns took 0.03ms to do this check. That's a 97% improvement!

I know it's not terribly exciting to talk about performance improvements, but it's really important to me that this game run as well as possible so that I can continue to build on top of what I've already written without having to worry that it won't be able to handle it. And so as many people are able to play the game of course.

With these two changes, the game now wouldn't drop below 144fps on my PC until over 170 pawns. That's double the amount of pawns the engine could now support! Even after that improvement, the line of sight checks is still about 80% of a pawns performance usage so improving it further will be massively beneficial.

Lastly
I didn't quite manage to take my advice last update and take much of a break. But I did manage to take most of Saturday & Sunday off, especially with Sunday being Easter I was able to enjoy talking with family which I don't do as much as I should. I've also really gotten into playing State of Decay 2 so that's helped me to not work all day. Being so close to the demo though, I can see the finish line and that's giving me massive motivation to keep working and get things done. Whereas before I was just doing things every day because I felt I should, now I'm excited to open up the project and take another step closer to that demo!

As always, thanks for reading, if you're curious about the progress of the demo I recommend reading further, and I'll see you in the next one!

-Blake Gillman


Extras

>Lines of code written:
22,079 lines / 245 scripts.
(+1,410 lines / +9 scripts since last update)

>"Demo Essentials" checklist:
(Done) 3~4 cover objects
(Done) Breach charge that goes through wood
Pawns can use gadgets
(Done) Pawns
(Done) Biome Editor
Lobby UI
Squad Customization
(Done) Interactables System
(Done) Health System
(Done) Map Editor
(Done) Ranged Weapons (LMG, SMG, Shotgun, Semi Rifle, Auto Rifle, Pistol)
Melee Weapons (Knife, Baton)
Map Entry Vehicle / Spawn
(Done) Loading Screen
Fog of War System
(Done) Main Menu
1 decent map
Player UI
Pawn UI
(Done) Most things have some sort of SFX
Friendly pawn start spawns
Servers can be created on request (Likely, from Amazon)

For this checklist, I really decided to think harder about what the game really needs for an accurate demo. A lot of what I had listed on there such as Amazon servers, Lobby UI and others were for multiplayer which isn't the focus of the game, so I decided to remove them from the list for the demo. This means the demo is now planned to be a singleplayer experience, since that's the core of the game. I also removed "Player UI'" from the list since that's not really necessary either and instead replaced it with "Pawn UI" since that'll be necessary for giving some of the orders like "use gadget" and "attack position". I also removed "Melee Weapons" from the list. Although I still think the final game will have them, I don't think they're necessary for a demo. I also added "Pawns can use gadgets" to the list since gadgets do play an important enough of a role in the game to be needed for the demo. I added "Friendly pawn start spawns" since at the moment, for testing I just press space to spawn in a friendly pawn. So for the demo I'll need to make so friendly pawns can start already on the map. And finally I removed "Squad customization" from the demo requirements. This was the hardest one for me to debate if it was necessary or not, and although it like many other things is important, I don't think having different loadouts is important for testing if the game is fun to play or not, which is what the demo is about.

So with these new demo requirements in mind, I'm optimistic and think the game's demo should be ready to release sometime around update #7 or #8, although I'm fine with it being sooner or longer than that. I should have the game website up by next update so you'll be able to sign up for the demo & invite others to sign up as well.

Notes & Technical Extras

>Pawn brains (AI system) is readily expandable
I wrote the pawn brain system to support different brains. This means that how I program AI to react to things & do things in the future I could write completely differently. Right now the current system is called "Normal", but in the future I could write more aggressive brains that rush the enemy when they're shot at, or more cowardly brains that run away in fear (Which would be good for civilians for example). I've also been considering making another system called "Brain Data" which will let you modify the data, so even the same brain could be customized to do things differently. That might be taking it a little far though but having the option to do these different brains in the future I think was a good call. This is all ready for mod support too, pawns templates have a variable called "Brain Type" so if I add more in the future, all people would need to do is change the number to whichever brain they want it to use.

Blog Summary

>Notes used to create this post
Pawn AI:
-Implemented new wandering solution
-Pawns take cover when shot at
-Pawns can hear gunshots
-Pawns run toward enemies 
-Pawns investigate where they think an enemy used to be

Gadgets:
-Created gadget framework
-Created gadget types (Placed, Thrown)
-Created new gadgets (Breach Charge, HE Grenade, Flashbang)
-Added gadget sounds (On Trigger, Loop, On Create)
-Made so pawns can start with gadgets
-Made "Use Gadget" pawn order

Performance:
-Improved PawnCombat.cs performance
-Improved PawnMovement.cs performance
-Improved OwnableObject.cs performance

No comments:

Post a Comment