File size: 1,965 Bytes
2952f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Godot;
using Godot.Collections;
using System;
using Array = Godot.Collections.Array;
using Dictionary = Godot.Collections.Dictionary;

public partial class PlayerAIController : AIControllerSharp3D
{
    [Export]
    public Player Player;
    
    [Export]
    public Array<Node> RayCastSensors;

    public override void _PhysicsProcess(double delta) {
        // Intentionally not calling base._PhysicsProcess(delta)
        n_steps += 1;
        
        if(n_steps > reset_after) {
            if (DebugOn) GD.Print("Timeout reached. Setting 'needs_reset' to true.");
            Player.GameSceneManager.Reset();
        }
    }

    public void EndEpisode() {
        done = true;
        reset();
    }

    public override Dictionary get_obs() {
        var obs = new Array<float>();
        foreach (var sensor in RayCastSensors) {
            var sensorObs = (Array<float>)sensor.Call("get_observation");
            obs += sensorObs;
        }

        float levelSize = 10;
        Vector3 relativeGoalPosition = Player.ToLocal(Player.Goal.GlobalPosition);
        Vector3 relativeObstaclePosition = Player.ToLocal(Player.Obstacle.GlobalPosition);

        obs += new Array<float> {
            relativeGoalPosition.X / levelSize,
            relativeGoalPosition.Z / levelSize,
            relativeObstaclePosition.X / levelSize,
            relativeObstaclePosition.Z / levelSize
        };
        
        return new Dictionary {
            { "obs", obs }
        };
    }
    
    public override Dictionary get_action_space() {
        return new Dictionary {
            {"move_action", new Dictionary {
                {"size", 2}, {"action_type", "continuous"}
            }}
        };
    }
    
    public override void set_action(Dictionary action) {
        float x = (float)((Array) action["move_action"])[0];
        float y = (float)((Array) action["move_action"])[1];
        Player.RequestedMovement = new Vector2(x, y);
    }
}