Unity3d, Agents and Tanchiki

    image

    Good day to all!

    Even at the university in high school, he began to get involved in intellectual agents. Even the topic for the diploma was originally associated with the creation of agents involved in hostilities in an environment that mimics the battlefield. But because of the work, I had to change the subject.

    I have long wanted to do this, but all the time was gone. Now they finally got around to making an environment for agents, although not on such a scale. So if anyone is interested, join in! The repository is open, link below.

    Intelligent Agent.

    In simple words, an agent is an entity placed in the environment, able to perceive the environment using sensors and act on it using actuators.

    The simplest agent scheme, as Wikipedia says, looks like this:

    image

    Such an agent has no reason, it acts exclusively according to the list of simple if-then rules. There are several types of much more complex agents that can analyze their actions and learn. But I’m still far from them.

    Unity3d

    I created a project in Unity3d that has a battlefield (square) and several tanks. I took the textures of the tanks from the good old Battle City game. All that every tank can do is drive across the field and shoot.
    This is how the whole project looks:

    image

    2 control scripts are attached to each tank:
    - TankBehavior
    - BasicTankControls (or its subclass)

    TankBehavior is the main script that fully describes one tank. This script implements the methods of moving, shooting, and all the rest. This script also contains a link to BasicTankControls, which implements tank control. The basic script looks like this:

    public class BasicTankControls : MonoBehaviour
    {
         public void Init(TankBehavior tank)
         {
              this.tank = tank;
         }
         public virtual void Act(List tanksData)
         {
              // no operation
         }
         protected TankBehavior tank;
    }
    


    This initializes the control script in the TankBehavior script:

    void Start ()
    {
         ...
         controls = GetComponent();
         if(controls != null)
         {
              controls.Init(this);
         }
         ...
    }
    


    This method is called every physical tick:

    void FixedUpdate()
    {
         if(controls != null)
         {
              GameObject[] tankObjects = GameObject.FindGameObjectsWithTag("Tank");
              List tanksData = new List();
              foreach (var item in tankObjects)
              {
                   if (item.GetInstanceID() != this.GetInstanceID())
                   {
                        tanksData.Add(item.GetComponent().GetData());
                   }
              }
              controls.Act(tanksData);
         }
    }
    


    First, data is collected about all the tanks on the field. In this way, the collection of environmental information by agent sensors is simulated. Then this information is transmitted to the control script, which, based on the received data, will have an impact on the environment.
    Data on one tank looks like this:

    public class TankData
    {
         public TankData(int instanceId, Vector2 position, float health, Direction direction)
         {
              this.InstanceID = instanceId;
              this.Position = position;
              this.Health = health;
              this.Direction = direction;
         }
         public readonly int InstanceID;
         public readonly Vector2 Position;
         public readonly float Health;
         public readonly Direction Direction;
    }
    


    That's basically it. Now the task is only to write a control script for the tanks. I wrote one script for managing a person if someone suddenly wants to intervene in the course of events:

    public class HumanControls : BasicTankControls
    {
         public override void Act(List tanksData)
         {
              if(Input.GetButton("Up"))
              {
                   tank.MoveForward();
              }
              else if (Input.GetButton("Down"))
              {
                   tank.MoveBackward();
              }
              if (Input.GetButtonDown("Left"))
              {
                   tank.RotateLeft();
              }
              else if (Input.GetButtonDown("Right"))
              {
                   tank.RotateRight();
              }
              if (Input.GetButton("Fire"))
              {
                   tank.Fire();
              }
         }
    }
    

    Tank behavior

    To begin with, I plan to make simple autonomous behaviors, such as driving and shooting in random directions. Then there is the idea of ​​adding walls (maybe demolished) and the very headquarters, which some need to protect, and others to destroy. Maybe there will even be joint action planning.
    But these are very optimistic plans. I myself have never written this before, so I’ll have to study it. Any help and advice is appreciated! :)

    Link to the repository.

    Also popular now: