As I wrote my first AI

Hi, Habr. My story begins in January 2019.

My Tima game developers and I decided to take up the largest project in our history, a 2D platformer. No, we didn’t do any FlappyBirds or snakes before, but the amount of work in this project just blew our brains. To begin with, we abandoned the usual, vertical stairs, and took the step stairs. We wrote the logic for the doors, which could be locked “with a key” and screwed the destructible blocks. There came a time when it was necessary to write Artificial Intelligence. As the most experienced scripter from our inexperienced team, the honor of writing AI fell to me. I cried into the pillow, not understanding what to do, I was very proud that I would write one of the most difficult mechanics in our project.

Stage 1: finding the path and moving along the path


Step 1.1: Finding the Way

Since the main locations will not be in the open air, but in the buildings, it was necessary to search for the route among dozens of doors, stairs and rooms. After thinking, the team leader decided that it was worth doing a certain parody of the A * algorithm, where we would have nodes between which the bot would run. made a test scene, put the nodes, for clarity, hung SprateRenderers on them. What to do next?

With this question in mind I walked for 3 days. So far, one of my comrade has not proposed an interesting algorithm when the nodes will be excited, like brain neurons.

So. There is a node A, near which there is an AI and a node B, to which the AI ​​should come. gave all Nodes their ID and marked the connected nodes to which they will send a signal. Each node had its own boolean variable "isChecked" and the variable "triggeredBy", which contained the ID of the node that "excited" it. So, when node B is affected, it will go through the chain to node A, recognizing all the IDs of the nodes that the signal passed through. So I got the path from the IDs of the nodes that the bot should go through. If you suddenly do not understand how this works, then I will tell you a story.
Once, Ivan had nothing to do, and so he decided to make up his family tree. Unfortunately, he did not have enough information to translate this idea into reality. Ivan was so keen on this idea that he decided that when he reached the main progenitor he would be able to discover his unexpected relatives. Ivan knew where he could meet with his father to talk and went there. Father told him that Ivan’s grandfather’s name was Ivan and told where he could be found. Ivan found Ivan, and he said that Ivan's great-grandfather could know this ancestor personally, but he had long since died. Ivan devoted half his life to the study of dark arts, but in the end he was able to resurrect his great-grandfather. Great-grandfather said that his ancestor is a werewolf and that his body is buried at the edge of the three pines. Ivan went there and found a man digging a grave. It turned out that this man is the twelve-cousin Ivan. Ivan was very surprised that they came to one place, but his brother turned out to be a programmer and explained everything.

- The principle of navigation from my favorite game * name * works here!

- And what is it?

- From each heir, you can come to the common parent of the system, if in this system each heir personally knows his parent.
Here is the result:



This is an array of int-variables that mean the IDs of the nodes that the unit must pass.

Step 1.2.1: movement along the path

I have a list of node IDs, I have a bot. What's next? And then what you need to move the bot along this path.

Well, I figured this option: the bot reached the node, checked, looked what was next, went to the next node. Done. Earned. I was glad ... But ...

Stage 1.2.2: ladders and their interaction with AI

As one black-and-white hero used to say: “Stairs ... my main enemy ..”

It was necessary to determine whether the next node was above the AI, under or on the level. Depending on this information, he will walk past the stairs (ignore the collision), or climb onto it (interact with the collision). Oh, and a lot of nerve cells died in this battle with the engine ... I read on the forums that you can arrange everything in layers and in the Edit-> Physics2D tab you can configure ignoring collisions of one layer and another. It all worked!
It remains only to teach him to open the door. There were no problems.

Total:





Stage 2: Emotions and Cues


Stage 2.1: Emotions

Yes, we decided to attach emotions ... And cues.

Emotions will be highlighted with facial expressions and action animations.

Replicas will be displayed with text overhead.

I screwed emotions in one go ... For this, I have already made the variable "emotionID", which stored the ID of the emotion. And here are the replicas ...

Step 2.2: Replicas

For beauty made a separate class Phrases

[System.Serializable] //сериализуемый для красивого отображения в инспекторе
class Phrases
{
    public string Name;     //название эмоции, которой соответствуют реплики
    public int byEmotionID; //для определения, с какой эмоцией это соединять
    public string[] Phrases;//массив самих реплик
}

Made an array of this class. Further, just depending on emotionID, I put any phrase from the list. Updated once every N seconds.

But I decided to go further! For each character I made a file with the .phrs extension, encoded this by adding X bytes to the byte number of each character in the file. The result was unreadable, unchangeable text. did something like its markup, made an algorithm that takes and translates everything into this array into the Phrases class by this markup.
Excellent! Everything works!

I wanted to write a program on a clean sharpe to fill out such a file, but here we go to the end of the story.

The end..?


We were quickly tired of the big, unpaid work ... Attaching a new encoder did not help ... The team fell apart ... The code still lies on the Unity cloud.

Of course, not so long ago the idea to continue the project began to emerge, but with further monetization ... If something works out, then I will probably write the whole history of the development. But this is where my story about the novice scripter and AI ends.

Also popular now: