Beispiel #1
0
    /*
     *  Core agent update function.
     */
    void Update()
    {
        if (this.sensorModule == null || this.knowledgeBase == null)
        {
            return;
        }
        float distToGoal = -1;

        if (WorldStateManager.CONTROL)
        {
            // This is control logic for a simple test to compare algos against
            // here for now because logic was tightly coupled
            // TODO: extract this into separate agent
            this.sensorModule.Update();
            if (knowledgeBase.GetCurrentGoal() == null)
            {
                Tile t = WorldStateManager.GetControlTileForAgent(this.ID);
                if (t == null)
                {
                    // Debug.Log("Tile is null");
                    //No goal so just chill out
                    flightController.SetNextDestination(sensorModule.gps.position);
                    this.flightController.UpdateMotion(1f);
                    return;
                }
                knowledgeBase.SetGoalTile(new SearchTile(t.GetID(), t.GetCenter()));
            }
            this.flightController.UpdateMotion(distToGoal);
            flightController.SetNextDestination(knowledgeBase.GetCurrentGoal().center);
            Debug.DrawLine(this.sensorModule.gps.position, new Vector3(this.knowledgeBase.GetCurrentGoal().center.x, this.knowledgeBase.GetCurrentGoal().center.y - 40f, this.knowledgeBase.GetCurrentGoal().center.z), Color.black);

            distToGoal = this.GetDistanceToGoal(this.sensorModule.gps.position, knowledgeBase.GetCurrentGoal().center);
            if (distToGoal < 5.1f)
            {
                WorldStateManager.MarkTileAsSearched(knowledgeBase.GetCurrentGoal());
                knowledgeBase.SetCurrentGoalAsSearched();
            }
            return;
        }
        // Update the knowledge base first
        this.sensorModule.Update();
        this.knowledgeBase.UpdateKnowledgeBase();

        // Check if the agent is in range of the tile
        SearchTile goalTile = knowledgeBase.GetCurrentGoal();

        if (goalTile != null)
        {
            distToGoal = this.GetDistanceToGoal(this.sensorModule.gps.position, goalTile.center);
            if (distToGoal < 5.1f)
            {
                WorldStateManager.MarkTileAsSearched(goalTile);
                knowledgeBase.SetCurrentGoalAsSearched();
            }
        }

        //This should be used with horizons and stuff
        if (shouldUpdateHeading())
        {
            this.knowledgeBase.ComputeNextHorizon(HEADING_UPDATE_TIME);
            Vector3 nextPoint = decisionManager.getNextDesiredPoint();
            // Debug.DrawLine( new Vector3(nextPoint.x, nextPoint.y - 50, nextPoint.z), new Vector3(nextPoint.x, nextPoint.y + 20, nextPoint.z));
            flightController.SetNextDestination(nextPoint);
        }

        // Update position
        this.flightController.UpdateMotion(distToGoal);
        // if(this.knowledgeBase.GetCurrentGoal() != null)
        // Debug.DrawLine( this.sensorModule.gps.position, new Vector3(this.knowledgeBase.GetCurrentGoal().center.x, this.knowledgeBase.GetCurrentGoal().center.y-40f, this.knowledgeBase.GetCurrentGoal().center.z), Color.black);
        this.knowledgeBase.SendAgentUpdate();
        // if(this.ID == 0){
        foreach (AgentUpdate upd in this.knowledgeBase.updates)
        {
            if (Vector3.Distance(this.sensorModule.gps.position, upd.lastUpdate.position) < 200)
            {
                Debug.DrawLine(this.sensorModule.gps.position, upd.lastUpdate.position, Color.black);
            }
        }
        // }
    }