Ejemplo n.º 1
0
    def run(self, agent_host):
        """ Run the Agent on the world """
        agent_host.sendCommand("move 0.25")
        world_state = agent_host.getWorldState()
        while world_state.is_mission_running:
            #sys.stdout.write("*")
            time.sleep(0.1)
            world_state = agent_host.getWorldState()
            if world_state.number_of_observations_since_last_state > 0:
                msg = world_state.observations[-1].text
                ob = json.loads(msg)
                '''Obs has the following keys:
                ['PlayersKilled', 'TotalTime', 'Life', 'ZPos', 'IsAlive',
                'Name', 'entities', 'DamageTaken', 'Food', 'Yaw', 'TimeAlive',
                'XPos', 'WorldTime', 'Air', 'DistanceTravelled', 'Score', 'YPos',
                'Pitch', 'MobsKilled', 'XP']
                '''
                print ob.keys()

                xPos = ob['XPos']
                yPos = ob['YPos']
                zPos = ob['ZPos']
                yaw = ob['Yaw']
                pitch = ob['Pitch']
                target = self.getNextTarget(ob['entities'])
                if target == None or target[
                        'name'] not in Arena.HEIGHT_CHART.keys(
                        ):  # No enemies nearby
                    if target != None:
                        sys.stdout.write("Not found: " + target['name'] + "\n")
                    agent_host.sendCommand("move 0")  # stop moving
                    agent_host.sendCommand("attack 0")  # stop attacking
                    agent_host.sendCommand("turn 0")  # stop turning
                    agent_host.sendCommand("pitch 0")  # stop looking up/down
                else:  # enemy nearby, kill kill kill
                    deltaYaw, deltaPitch = Arena.calcYawPitch(
                        target['name'], target['x'], target['y'], target['z'],
                        yaw, pitch, xPos, yPos, zPos)
                    # And turn:
                    agent_host.sendCommand("turn " + str(deltaYaw))
                    agent_host.sendCommand("pitch " + str(deltaPitch))
                    agent_host.sendCommand("attack 1")

            for error in world_state.errors:
                print "Error:", error.text