Thursday, October 7, 2021

My development workflow

Today, I'm going to demonstrate my developer workflow and architecture for my game, with Unity3D, GitHub, and Visual Studio. I've created a workflow that allows me to verify any change with a rich set of automated tests, and automatically load those changes into my Unity project, reducing the time to resolve bugs, and giving me confidence that my code is robust.

Architecture

At the top level, I have my Unity project (in green), a private project stored in GitHub. This has an attached .NET project, (named the default "Assembly-CSharp"), that references a .NET standard dependency "Battle.Logic", a public project in GitHub. Attached to the Battle.Logic project, is a unit test project that currently has ~97% code coverage - and leverages over 200 tests that run in just a few seconds - giving me almost instant feedback that the project is in good shape.


The bug

Let's look at the bug. I've been iterating on AI for a few weeks, but when this code runs in Unity, it fails with the error below. You'll note the error is in the Battle.Logic. Uh oh. How can I replicate this? Luckily we can do it in just a few minutes. You'll notice in the top right of the UI, I have a "Save JSON" button. What does this do? Let's press it and see:



The button serializes our game into a JSON file. In the console below we can see where it was saved on our laptop, and copying the file into our Battle.Logic test project, I can create an automated test to load the file, replicate the game state, and test it.



The code to do this is quite simple, I open the JSON file, add the correct line to replicate the issue we saw in our Unity file (run the AI). Look at the error in the bottom left of the tests- we get exactly the same error we saw in Unity. Hurray! Now we can troubleshoot and resolve the issue. In this situation, an object, (character "CoverState"), wasn't being initialized correctly. I resolve the issue, my test passes, and I can continue building my game.



Let's look at how we get this fix back into Unity. In GitHub, we open a new pull request with the fix. We have a couple other checks in here to confirm quality - with code coverage (Coverall) and code analysis (SonarCloud). Everything looks good, so we merge the Pull Request.


This triggers a new build on the main branch in GitHub, which when successful, publishes a new dll to a "release", with a unique version - in this case 0.9.2. 



To get this into my Unity project, I wrote a simple script to download the files from the latest release, unblock them (thank you Windows, but I trust my files), and copy them to the right place in my Unity project



Opening up my Unity project, I can see in the files in the "Git Changes" box.



Running the same situation as before, I can see the issue is resolved - although I did find another bug - my character AI moved it to an unexpected position. Time to do this again, and write a more comprehensive test for this scenario!



Wrap up

Just a simple example, but I value my time, and developer productivity is everything. In just a few minutes, with some key enabling automation, I can troubleshoot and resolve an issue, create a test to prevent it from happening again, and get the change back into my Unity project in just a few minutes.

Sunday, August 1, 2021

Reorganizing, again.

Every couple of years I end up in the same spot, lost, overwhelmed, unmotivated. 6-12 months later I repeat the cycle, rebooting and starting again. I've done this for roughly ten years... 

This round is different. This one is powered by DevOps and GitHub.

Why is this one so different? 

First, I've separated the logic into a separate project, and it's open source and on GitHub for anyone to use. Using .NET Standard 2.0, and C#, I'm able to write unit tests that validate that this project is working as expected. As it's all just C#, no graphics are being rendered, the tests run extremely fast, nearly 200 tests in less than half a second. I calculate paths, chance to hit, field of view, etc, without a graphics engine. These automated tests are a crutch when I walk away for a day, week, or several months. I know that when I return, I have a series of tests to tell me when I break something else. 




This also allows me to measure code coverage - the percent of code that has a test. My code coverage is very high, 99.61%, when 70-80% is typical.


Additionally, I can visually see my maps and situations in 2D using ASCII. I can render these data structures in 3D with Unity3D relatively easily, but seeing it in 2D gives me an easy debug mechanism. 




I also have a detailed log to help me with debugging. Human readable, important as I am a human. 


More is coming, I've just started to integrate this in Unity3D, and I know there will be more breaks in development, but now I know I'm chipping away at a system I can always pick back up again. I start a turn by calling my battle logic, all of the movement calls return a list of moves and if their are any interrupts - and in general, it's making the 3D part of this, much, much easier to manage




Tuesday, September 26, 2017

Update 51: Emerging AI

Next on the list was AI. I had a plan for this. Since most interactions are over in 2-3 turns, I don't think I have to worry about a higher AI (where the AI is trying to think long term). I think, (at least initially), I just want to encourage the AI to move towards the player and be fairly aggressive, staying in cover and shooting if the chance to hit is high. These ten rules are definitely just a starting point.

  1. Find all possible tiles the character can move to, initializing them with a score of 0.
  2. Assign points to any tiles that are in cover (+10). If the character is currently not in cover, give all of those positions an extra boost to encourage the character to move into cover. (+10)
  3. Add points for tiles that are in range for the character to shoot at the opponent. (+10)
  4. Add points for tiles that close the gap between the AI and opponent. (+5)
  5. Add points for positions in cover that flank the opponent (+20)
  6. Add points if the opponent is within range and has more than a 50% chance to hit. (+10)
  7. Add even more points if the chance to hit is 80% or more. (+20)
  8. Add even more points if the chance to hit is 95% or higher. (+30)
  9. Order the list by total score, descending order (highest first)
  10. Pick the top score from the list. 

When I add difficulty levels later, I will randomize the top 3-5 scores and pick one. This will help to give the AI some unpredictability and ensure that it's not always brutally taking the best position. Everyone makes mistakes, and it's important the AI does too (except on maybe the highest difficulty level). I may do this sooner than later to keep things interesting.

This AI is currently working, but I have some timing issues, where the AI moves into cover, but fires his weapon for his second action before he has completed his first action. 

Here is a series of screenshots showing the AI's at the beginning of a turn (standing out of cover), and then moving to cover and shooting at the (flanked) player. 

This one is showing the timing issues I have, where the AI fired (action 2) before completing his walk to the new cover (action 1), in the process destroying the cover.




Next I'm going to tweak the timings so the actions are one at a time and add side steps to the characters so that when behind cover they don't shoot out their own cover, but step to the side and shoot around the cover.

Thursday, September 14, 2017

Update 50: Combat and 50!!

5 years and 50 posts later, here we are, still working through the combat mechanics. Amazingly, I'm still making regular progress. I think the keyword here is regular, there have definitely been some large gaps in progress.

However, the combat is proceeding along nicely. I've added projectiles and the destructive terrain back. I've added back the shoulder cam when you are aiming and choosing your target. The enemy and player are now dealing (and receiving) damage. I almost have a quasi game here. 

Here you can see the setup of the level, with the second (shoulder) camera ready



The view from the shoulder cam. The models really look low res here!

Boom!


 Before the shot...

"Missed", that is the end of that wall!


Here you can see the ray-cast passing through the enemy, it was a hit!


Next on the list is basic AI, it's time for the enemy to respond to your actions and try to defeat you. 

Thursday, August 31, 2017

Update 49: Added weapons to the characters

I have now added weapons to the characters. The Simple Military pack has a variety of weapons, but at this stage, I've assigned a 'modern assault rifle' for the 'good guys', and a AK47 for the 'bad guys'.




I also added a muzzle flash when the gun shoots (it shows for just a fraction of a second).




Next I will add projectiles and add back in the destructive environment that I started with, all those years ago.

Sunday, August 27, 2017

Update 48: Implementing the turn state diagrams

Next I've been working on implementing more of the game, which is a big departure of most of my posts, where I've been working on game mechanics, usually in isolation. I'm calling this the "player state diagram". You can see this represented in a state diagram below. Also encased in the diagram is a road map of the features I've implemented and what I plan to implement next.




When a player has finished all of their moves, it switches to the enemy turn, where the (overly simplified AI) decides to just pass ("Patrol"), and switch back to the player. Since the player still can't attack, I think this is fair. 

Next we need to add AI to the player to move into cover and shooting between the two teams...

Tuesday, August 22, 2017

Update 47: Cover and Flanking

In my last update I showed a screenshot with a soldier in cover. In this latest update, I've added an enemy and added the code that marks the players as in cover or being flanked/out of cover.

Here my player is in cover (the enemy has a lower chance of hitting the player and dealing damage):




Here my player is flanked and not in cover (the enemy has a much higher chance of hitting the player and dealing critical damage):


There was actually quite a lot of rework involved in the code and corresponding systems to get the enemy soldier to (re-)appear and have everything work. The next update will be even more involved as I refactor the code into more systems and add more players and enemies - adding turns that switch back and forth after movement.