from __future__ import print_function
from __future__ import division
from builtins import range
from past.utils import old_div
try:
    from malmo import MalmoPython
except:
    import MalmoPython
import os
import sys
import time
import TextGen
import os
import sys
import time
import json
import random
from tqdm import tqdm
from collections import deque
import matplotlib.pyplot as plt 
import numpy as np
from numpy.random import randint
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F


# Hyperparameters
SIZE = 50
WEATHER="clear"
time_now=5000
OBS_SIZE = 10
REPLAY_BUFFER_SIZE = 10000
BATCH_SIZE = 128
GAMMA = .9
TARGET_UPDATE = 100
WORLD_PATH = os.path.join(os.getcwd(), 'new_world')
if sys.version_info[0] == 2:
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  # flush print output immediately
else:
    import functools
    print = functools.partial(print, flush=True)


def get_observation(world_state,weather,time_now):
    """
    Use the agent observation API to get a 2 x 5 x 5 grid around the agent. 
    The agent is in the center square facing up.

    Args
        world_state: <object> current agent world state

    Returns
        observation: <np.array>
    """
    obs = np.zeros((2, OBS_SIZE, OBS_SIZE))
    object_list=[]
    while world_state.is_mission_running:
        time.sleep(0.1)
        world_state = agent_host.getWorldState()
       
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')

        if world_state.number_of_observations_since_last_state > 0:
            # First we get the json from the observation API
            msg = world_state.observations[-1].text
            observations = json.loads(msg)
            # Get observation
            if weather != 'clear':
                object_list.append(weather)
            else:
                object_list.append('sunny')

            if time_now>13000:
                object_list.append("night")
            else:
                object_list.append("morning")
            grid = observations['floorAll']
            
            if grid.count('water')>3:
                object_list.append("river")
            if grid.count('leaves')>2:
                object_list.append("tree")
            if grid.count('lava')>1:
                object_list.append("lava")
                
            animal=observations['NearbyEntities']
            target_ani=['Sheep','Cow','Pig']
            for i in animal:
                if i['name'] not in object_list and i['name'] in target_ani:
                    object_list.append(i['name'])
            break
            
           
    return object_list

 
missionXML='''<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
            <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            
              <About>
                <Summary>Generate Text With Malmo</Summary>
              </About>
              
            <ServerSection>
              <ServerInitialConditions>
                <Time>
                    <StartTime>'''+str(time_now)+'''</StartTime>
                    <AllowPassageOfTime>false</AllowPassageOfTime>
                </Time>
                <Weather>'''+WEATHER+'''</Weather>
              </ServerInitialConditions>
              <ServerHandlers>
                  <FileWorldGenerator src="'''+ WORLD_PATH + '''"/>
                  <ServerQuitFromTimeUp timeLimitMs="3000000"/>
                  <ServerQuitWhenAnyAgentFinishes/>
                </ServerHandlers>
              </ServerSection>
              
              <AgentSection mode="Survival">
                <Name>MalmoTutorialBot</Name>
                <AgentStart>
                    <Inventory>
                        <InventoryItem slot="8" type="diamond_pickaxe"/>
                    </Inventory>
                </AgentStart>
                <AgentHandlers>
                <ChatCommands/>
                  <ObservationFromFullStats/>
                  <ObservationFromGrid>
                            <Grid name="floorAll">
                                <min x="-'''+str(int(OBS_SIZE/2))+'''" y="-2" z="-'''+str(int(OBS_SIZE/2))+'''"/>
                                <max x="'''+str(int(OBS_SIZE/2))+'''" y="3" z="'''+str(int(OBS_SIZE/2))+'''"/>
                            </Grid>
                  </ObservationFromGrid>
                  <ObservationFromNearbyEntities>
                        <Range name="NearbyEntities"
                        xrange="10"
                        yrange="4"
                        zrange="2"
                        update_frequency="1"/>
                  </ObservationFromNearbyEntities>
                  <ContinuousMovementCommands turnSpeedDegs="180"/>
                  <InventoryCommands/>
                  <AgentQuitFromReachingPosition>
                    <Marker x="-26.5" y="40.0" z="0.5" tolerance="0.5" description="Goal_found"/>
                  </AgentQuitFromReachingPosition>
                </AgentHandlers>
              </AgentSection>
            </Mission>'''

# Create default Malmo objects:
prvious_list=[]
agent_host = MalmoPython.AgentHost()
try:
    agent_host.parse( sys.argv )
except RuntimeError as e:
    print('ERROR:',e)
    print(agent_host.getUsage())
    exit(1)
if agent_host.receivedArgument("help"):
    print(agent_host.getUsage())
    exit(0)

my_mission = MalmoPython.MissionSpec(missionXML, True)
my_mission_record = MalmoPython.MissionRecordSpec()

# Attempt to start a mission:
max_retries = 3
for retry in range(max_retries):
    try:
        agent_host.startMission( my_mission, my_mission_record )
        break
    except RuntimeError as e:
        if retry == max_retries - 1:
            print("Error starting mission:",e)
            exit(1)
        else:
            time.sleep(2)

# Loop until mission starts:

world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
    print(".", end="")
    time.sleep(0.1)
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print("Error:",error.text)

print()
TOTAL_LOOP = 25
textGen = TextGen.TextGen()
genre_list = ['superhero', 'sci_fi', 'action', 'drama', 'horror', 'thriller']
mode = 'nucleus'
search_size = 10
# Loop until mission ends:

while world_state.is_mission_running:
    time.sleep(0.1)
    signal = input('Please enter any key to start detect')
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print("Error:",error.text)
    obs=get_observation(world_state,WEATHER,time_now)
    obs = [x.lower() for x in obs]
    
    #out put observation
    agent_host.sendCommand("chat " + 'observation: ' + str(obs))
    print()
    print('observation:', obs)
    
    prvious_list = list(obs)

    #out put genre
    if 'night' in obs:
        if random.random()<0.8:
            genre = genre_list[randint(4,6)]
        else:
            genre = genre_list[randint(0,4)]
    elif 'lava' in obs:
        if random.random()<0.8:
            genre = genre_list[randint(0,2)]
        else:
            genre = genre_list[randint(2,6)]
    else:
        if random.random()<0.8:
            genre = genre_list[randint(0,4)]
        else:
            genre = genre_list[randint(4,6)]
    
    print('genre:', genre)
    agent_host.sendCommand("chat " + 'genre: ' + genre)

    if genre == 'sci_fi' or genre =='superhero':
        input_word = ['In the future', 'In the year of 2077', 'Yesterday'][randint(0, 3)]
    elif genre == 'horror' or genre =='thriller':
        input_word = ['In a darks house', 'In the deep forest', 'Midnight'][randint(0, 3)]
    else:
        input_word = ['This summer', 'Long long ago', 'Today'][randint(0, 3)]
    
    target_list = obs
    sentence=textGen.generate_target_list(input_word, target_list, genre, search_size,TOTAL_LOOP, mode)
    print()
    print(sentence)
    print()
    agent_host.sendCommand("chat " + sentence)
        
   

print()
print("Mission ended")
# Mission has ended.

795798b62ea57940d041acb09b9b8b9b178e5b37
<mask token>


def get_observation(world_state, weather, time_now):
    """
    Use the agent observation API to get a 2 x 5 x 5 grid around the agent. 
    The agent is in the center square facing up.

    Args
        world_state: <object> current agent world state

    Returns
        observation: <np.array>
    """
    obs = np.zeros((2, OBS_SIZE, OBS_SIZE))
    object_list = []
    while world_state.is_mission_running:
        time.sleep(0.1)
        world_state = agent_host.getWorldState()
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')
        if world_state.number_of_observations_since_last_state > 0:
            msg = world_state.observations[-1].text
            observations = json.loads(msg)
            if weather != 'clear':
                object_list.append(weather)
            else:
                object_list.append('sunny')
            if time_now > 13000:
                object_list.append('night')
            else:
                object_list.append('morning')
            grid = observations['floorAll']
            if grid.count('water') > 3:
                object_list.append('river')
            if grid.count('leaves') > 2:
                object_list.append('tree')
            if grid.count('lava') > 1:
                object_list.append('lava')
            animal = observations['NearbyEntities']
            target_ani = ['Sheep', 'Cow', 'Pig']
            for i in animal:
                if i['name'] not in object_list and i['name'] in target_ani:
                    object_list.append(i['name'])
            break
    return object_list


<mask token>

<mask token>
try:
    from malmo import MalmoPython
except:
    import MalmoPython
<mask token>
if sys.version_info[0] == 2:
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
else:
    import functools
    print = functools.partial(print, flush=True)


def get_observation(world_state, weather, time_now):
    """
    Use the agent observation API to get a 2 x 5 x 5 grid around the agent. 
    The agent is in the center square facing up.

    Args
        world_state: <object> current agent world state

    Returns
        observation: <np.array>
    """
    obs = np.zeros((2, OBS_SIZE, OBS_SIZE))
    object_list = []
    while world_state.is_mission_running:
        time.sleep(0.1)
        world_state = agent_host.getWorldState()
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')
        if world_state.number_of_observations_since_last_state > 0:
            msg = world_state.observations[-1].text
            observations = json.loads(msg)
            if weather != 'clear':
                object_list.append(weather)
            else:
                object_list.append('sunny')
            if time_now > 13000:
                object_list.append('night')
            else:
                object_list.append('morning')
            grid = observations['floorAll']
            if grid.count('water') > 3:
                object_list.append('river')
            if grid.count('leaves') > 2:
                object_list.append('tree')
            if grid.count('lava') > 1:
                object_list.append('lava')
            animal = observations['NearbyEntities']
            target_ani = ['Sheep', 'Cow', 'Pig']
            for i in animal:
                if i['name'] not in object_list and i['name'] in target_ani:
                    object_list.append(i['name'])
            break
    return object_list


<mask token>
try:
    agent_host.parse(sys.argv)
except RuntimeError as e:
    print('ERROR:', e)
    print(agent_host.getUsage())
    exit(1)
if agent_host.receivedArgument('help'):
    print(agent_host.getUsage())
    exit(0)
<mask token>
for retry in range(max_retries):
    try:
        agent_host.startMission(my_mission, my_mission_record)
        break
    except RuntimeError as e:
        if retry == max_retries - 1:
            print('Error starting mission:', e)
            exit(1)
        else:
            time.sleep(2)
<mask token>
while not world_state.has_mission_begun:
    print('.', end='')
    time.sleep(0.1)
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print('Error:', error.text)
print()
<mask token>
while world_state.is_mission_running:
    time.sleep(0.1)
    signal = input('Please enter any key to start detect')
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print('Error:', error.text)
    obs = get_observation(world_state, WEATHER, time_now)
    obs = [x.lower() for x in obs]
    agent_host.sendCommand('chat ' + 'observation: ' + str(obs))
    print()
    print('observation:', obs)
    prvious_list = list(obs)
    if 'night' in obs:
        if random.random() < 0.8:
            genre = genre_list[randint(4, 6)]
        else:
            genre = genre_list[randint(0, 4)]
    elif 'lava' in obs:
        if random.random() < 0.8:
            genre = genre_list[randint(0, 2)]
        else:
            genre = genre_list[randint(2, 6)]
    elif random.random() < 0.8:
        genre = genre_list[randint(0, 4)]
    else:
        genre = genre_list[randint(4, 6)]
    print('genre:', genre)
    agent_host.sendCommand('chat ' + 'genre: ' + genre)
    if genre == 'sci_fi' or genre == 'superhero':
        input_word = ['In the future', 'In the year of 2077', 'Yesterday'][
            randint(0, 3)]
    elif genre == 'horror' or genre == 'thriller':
        input_word = ['In a darks house', 'In the deep forest', 'Midnight'][
            randint(0, 3)]
    else:
        input_word = ['This summer', 'Long long ago', 'Today'][randint(0, 3)]
    target_list = obs
    sentence = textGen.generate_target_list(input_word, target_list, genre,
        search_size, TOTAL_LOOP, mode)
    print()
    print(sentence)
    print()
    agent_host.sendCommand('chat ' + sentence)
print()
print('Mission ended')

<mask token>
try:
    from malmo import MalmoPython
except:
    import MalmoPython
<mask token>
SIZE = 50
WEATHER = 'clear'
time_now = 5000
OBS_SIZE = 10
REPLAY_BUFFER_SIZE = 10000
BATCH_SIZE = 128
GAMMA = 0.9
TARGET_UPDATE = 100
WORLD_PATH = os.path.join(os.getcwd(), 'new_world')
if sys.version_info[0] == 2:
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
else:
    import functools
    print = functools.partial(print, flush=True)


def get_observation(world_state, weather, time_now):
    """
    Use the agent observation API to get a 2 x 5 x 5 grid around the agent. 
    The agent is in the center square facing up.

    Args
        world_state: <object> current agent world state

    Returns
        observation: <np.array>
    """
    obs = np.zeros((2, OBS_SIZE, OBS_SIZE))
    object_list = []
    while world_state.is_mission_running:
        time.sleep(0.1)
        world_state = agent_host.getWorldState()
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')
        if world_state.number_of_observations_since_last_state > 0:
            msg = world_state.observations[-1].text
            observations = json.loads(msg)
            if weather != 'clear':
                object_list.append(weather)
            else:
                object_list.append('sunny')
            if time_now > 13000:
                object_list.append('night')
            else:
                object_list.append('morning')
            grid = observations['floorAll']
            if grid.count('water') > 3:
                object_list.append('river')
            if grid.count('leaves') > 2:
                object_list.append('tree')
            if grid.count('lava') > 1:
                object_list.append('lava')
            animal = observations['NearbyEntities']
            target_ani = ['Sheep', 'Cow', 'Pig']
            for i in animal:
                if i['name'] not in object_list and i['name'] in target_ani:
                    object_list.append(i['name'])
            break
    return object_list


missionXML = """<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
            <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            
              <About>
                <Summary>Generate Text With Malmo</Summary>
              </About>
              
            <ServerSection>
              <ServerInitialConditions>
                <Time>
                    <StartTime>""" + str(time_now) + """</StartTime>
                    <AllowPassageOfTime>false</AllowPassageOfTime>
                </Time>
                <Weather>""" + WEATHER + """</Weather>
              </ServerInitialConditions>
              <ServerHandlers>
                  <FileWorldGenerator src=\"""" + WORLD_PATH + """"/>
                  <ServerQuitFromTimeUp timeLimitMs="3000000"/>
                  <ServerQuitWhenAnyAgentFinishes/>
                </ServerHandlers>
              </ServerSection>
              
              <AgentSection mode="Survival">
                <Name>MalmoTutorialBot</Name>
                <AgentStart>
                    <Inventory>
                        <InventoryItem slot="8" type="diamond_pickaxe"/>
                    </Inventory>
                </AgentStart>
                <AgentHandlers>
                <ChatCommands/>
                  <ObservationFromFullStats/>
                  <ObservationFromGrid>
                            <Grid name="floorAll">
                                <min x="-""" + str(int(OBS_SIZE / 2)) + '" y="-2" z="-' + str(int(OBS_SIZE / 2)) + """"/>
                                <max x=\"""" + str(int(OBS_SIZE / 2)) + '" y="3" z="' + str(int(OBS_SIZE / 2)) + """"/>
                            </Grid>
                  </ObservationFromGrid>
                  <ObservationFromNearbyEntities>
                        <Range name="NearbyEntities"
                        xrange="10"
                        yrange="4"
                        zrange="2"
                        update_frequency="1"/>
                  </ObservationFromNearbyEntities>
                  <ContinuousMovementCommands turnSpeedDegs="180"/>
                  <InventoryCommands/>
                  <AgentQuitFromReachingPosition>
                    <Marker x="-26.5" y="40.0" z="0.5" tolerance="0.5" description="Goal_found"/>
                  </AgentQuitFromReachingPosition>
                </AgentHandlers>
              </AgentSection>
            </Mission>"""
prvious_list = []
agent_host = MalmoPython.AgentHost()
try:
    agent_host.parse(sys.argv)
except RuntimeError as e:
    print('ERROR:', e)
    print(agent_host.getUsage())
    exit(1)
if agent_host.receivedArgument('help'):
    print(agent_host.getUsage())
    exit(0)
my_mission = MalmoPython.MissionSpec(missionXML, True)
my_mission_record = MalmoPython.MissionRecordSpec()
max_retries = 3
for retry in range(max_retries):
    try:
        agent_host.startMission(my_mission, my_mission_record)
        break
    except RuntimeError as e:
        if retry == max_retries - 1:
            print('Error starting mission:', e)
            exit(1)
        else:
            time.sleep(2)
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
    print('.', end='')
    time.sleep(0.1)
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print('Error:', error.text)
print()
TOTAL_LOOP = 25
textGen = TextGen.TextGen()
genre_list = ['superhero', 'sci_fi', 'action', 'drama', 'horror', 'thriller']
mode = 'nucleus'
search_size = 10
while world_state.is_mission_running:
    time.sleep(0.1)
    signal = input('Please enter any key to start detect')
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print('Error:', error.text)
    obs = get_observation(world_state, WEATHER, time_now)
    obs = [x.lower() for x in obs]
    agent_host.sendCommand('chat ' + 'observation: ' + str(obs))
    print()
    print('observation:', obs)
    prvious_list = list(obs)
    if 'night' in obs:
        if random.random() < 0.8:
            genre = genre_list[randint(4, 6)]
        else:
            genre = genre_list[randint(0, 4)]
    elif 'lava' in obs:
        if random.random() < 0.8:
            genre = genre_list[randint(0, 2)]
        else:
            genre = genre_list[randint(2, 6)]
    elif random.random() < 0.8:
        genre = genre_list[randint(0, 4)]
    else:
        genre = genre_list[randint(4, 6)]
    print('genre:', genre)
    agent_host.sendCommand('chat ' + 'genre: ' + genre)
    if genre == 'sci_fi' or genre == 'superhero':
        input_word = ['In the future', 'In the year of 2077', 'Yesterday'][
            randint(0, 3)]
    elif genre == 'horror' or genre == 'thriller':
        input_word = ['In a darks house', 'In the deep forest', 'Midnight'][
            randint(0, 3)]
    else:
        input_word = ['This summer', 'Long long ago', 'Today'][randint(0, 3)]
    target_list = obs
    sentence = textGen.generate_target_list(input_word, target_list, genre,
        search_size, TOTAL_LOOP, mode)
    print()
    print(sentence)
    print()
    agent_host.sendCommand('chat ' + sentence)
print()
print('Mission ended')

from __future__ import print_function
from __future__ import division
from builtins import range
from past.utils import old_div
try:
    from malmo import MalmoPython
except:
    import MalmoPython
import os
import sys
import time
import TextGen
import os
import sys
import time
import json
import random
from tqdm import tqdm
from collections import deque
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randint
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F
SIZE = 50
WEATHER = 'clear'
time_now = 5000
OBS_SIZE = 10
REPLAY_BUFFER_SIZE = 10000
BATCH_SIZE = 128
GAMMA = 0.9
TARGET_UPDATE = 100
WORLD_PATH = os.path.join(os.getcwd(), 'new_world')
if sys.version_info[0] == 2:
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
else:
    import functools
    print = functools.partial(print, flush=True)


def get_observation(world_state, weather, time_now):
    """
    Use the agent observation API to get a 2 x 5 x 5 grid around the agent. 
    The agent is in the center square facing up.

    Args
        world_state: <object> current agent world state

    Returns
        observation: <np.array>
    """
    obs = np.zeros((2, OBS_SIZE, OBS_SIZE))
    object_list = []
    while world_state.is_mission_running:
        time.sleep(0.1)
        world_state = agent_host.getWorldState()
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')
        if world_state.number_of_observations_since_last_state > 0:
            msg = world_state.observations[-1].text
            observations = json.loads(msg)
            if weather != 'clear':
                object_list.append(weather)
            else:
                object_list.append('sunny')
            if time_now > 13000:
                object_list.append('night')
            else:
                object_list.append('morning')
            grid = observations['floorAll']
            if grid.count('water') > 3:
                object_list.append('river')
            if grid.count('leaves') > 2:
                object_list.append('tree')
            if grid.count('lava') > 1:
                object_list.append('lava')
            animal = observations['NearbyEntities']
            target_ani = ['Sheep', 'Cow', 'Pig']
            for i in animal:
                if i['name'] not in object_list and i['name'] in target_ani:
                    object_list.append(i['name'])
            break
    return object_list


missionXML = """<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
            <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            
              <About>
                <Summary>Generate Text With Malmo</Summary>
              </About>
              
            <ServerSection>
              <ServerInitialConditions>
                <Time>
                    <StartTime>""" + str(time_now) + """</StartTime>
                    <AllowPassageOfTime>false</AllowPassageOfTime>
                </Time>
                <Weather>""" + WEATHER + """</Weather>
              </ServerInitialConditions>
              <ServerHandlers>
                  <FileWorldGenerator src=\"""" + WORLD_PATH + """"/>
                  <ServerQuitFromTimeUp timeLimitMs="3000000"/>
                  <ServerQuitWhenAnyAgentFinishes/>
                </ServerHandlers>
              </ServerSection>
              
              <AgentSection mode="Survival">
                <Name>MalmoTutorialBot</Name>
                <AgentStart>
                    <Inventory>
                        <InventoryItem slot="8" type="diamond_pickaxe"/>
                    </Inventory>
                </AgentStart>
                <AgentHandlers>
                <ChatCommands/>
                  <ObservationFromFullStats/>
                  <ObservationFromGrid>
                            <Grid name="floorAll">
                                <min x="-""" + str(int(OBS_SIZE / 2)) + '" y="-2" z="-' + str(int(OBS_SIZE / 2)) + """"/>
                                <max x=\"""" + str(int(OBS_SIZE / 2)) + '" y="3" z="' + str(int(OBS_SIZE / 2)) + """"/>
                            </Grid>
                  </ObservationFromGrid>
                  <ObservationFromNearbyEntities>
                        <Range name="NearbyEntities"
                        xrange="10"
                        yrange="4"
                        zrange="2"
                        update_frequency="1"/>
                  </ObservationFromNearbyEntities>
                  <ContinuousMovementCommands turnSpeedDegs="180"/>
                  <InventoryCommands/>
                  <AgentQuitFromReachingPosition>
                    <Marker x="-26.5" y="40.0" z="0.5" tolerance="0.5" description="Goal_found"/>
                  </AgentQuitFromReachingPosition>
                </AgentHandlers>
              </AgentSection>
            </Mission>"""
prvious_list = []
agent_host = MalmoPython.AgentHost()
try:
    agent_host.parse(sys.argv)
except RuntimeError as e:
    print('ERROR:', e)
    print(agent_host.getUsage())
    exit(1)
if agent_host.receivedArgument('help'):
    print(agent_host.getUsage())
    exit(0)
my_mission = MalmoPython.MissionSpec(missionXML, True)
my_mission_record = MalmoPython.MissionRecordSpec()
max_retries = 3
for retry in range(max_retries):
    try:
        agent_host.startMission(my_mission, my_mission_record)
        break
    except RuntimeError as e:
        if retry == max_retries - 1:
            print('Error starting mission:', e)
            exit(1)
        else:
            time.sleep(2)
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
    print('.', end='')
    time.sleep(0.1)
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print('Error:', error.text)
print()
TOTAL_LOOP = 25
textGen = TextGen.TextGen()
genre_list = ['superhero', 'sci_fi', 'action', 'drama', 'horror', 'thriller']
mode = 'nucleus'
search_size = 10
while world_state.is_mission_running:
    time.sleep(0.1)
    signal = input('Please enter any key to start detect')
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print('Error:', error.text)
    obs = get_observation(world_state, WEATHER, time_now)
    obs = [x.lower() for x in obs]
    agent_host.sendCommand('chat ' + 'observation: ' + str(obs))
    print()
    print('observation:', obs)
    prvious_list = list(obs)
    if 'night' in obs:
        if random.random() < 0.8:
            genre = genre_list[randint(4, 6)]
        else:
            genre = genre_list[randint(0, 4)]
    elif 'lava' in obs:
        if random.random() < 0.8:
            genre = genre_list[randint(0, 2)]
        else:
            genre = genre_list[randint(2, 6)]
    elif random.random() < 0.8:
        genre = genre_list[randint(0, 4)]
    else:
        genre = genre_list[randint(4, 6)]
    print('genre:', genre)
    agent_host.sendCommand('chat ' + 'genre: ' + genre)
    if genre == 'sci_fi' or genre == 'superhero':
        input_word = ['In the future', 'In the year of 2077', 'Yesterday'][
            randint(0, 3)]
    elif genre == 'horror' or genre == 'thriller':
        input_word = ['In a darks house', 'In the deep forest', 'Midnight'][
            randint(0, 3)]
    else:
        input_word = ['This summer', 'Long long ago', 'Today'][randint(0, 3)]
    target_list = obs
    sentence = textGen.generate_target_list(input_word, target_list, genre,
        search_size, TOTAL_LOOP, mode)
    print()
    print(sentence)
    print()
    agent_host.sendCommand('chat ' + sentence)
print()
print('Mission ended')

from __future__ import print_function
from __future__ import division
from builtins import range
from past.utils import old_div
try:
    from malmo import MalmoPython
except:
    import MalmoPython
import os
import sys
import time
import TextGen
import os
import sys
import time
import json
import random
from tqdm import tqdm
from collections import deque
import matplotlib.pyplot as plt 
import numpy as np
from numpy.random import randint
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F


# Hyperparameters
SIZE = 50
WEATHER="clear"
time_now=5000
OBS_SIZE = 10
REPLAY_BUFFER_SIZE = 10000
BATCH_SIZE = 128
GAMMA = .9
TARGET_UPDATE = 100
WORLD_PATH = os.path.join(os.getcwd(), 'new_world')
if sys.version_info[0] == 2:
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)  # flush print output immediately
else:
    import functools
    print = functools.partial(print, flush=True)


def get_observation(world_state,weather,time_now):
    """
    Use the agent observation API to get a 2 x 5 x 5 grid around the agent. 
    The agent is in the center square facing up.

    Args
        world_state: <object> current agent world state

    Returns
        observation: <np.array>
    """
    obs = np.zeros((2, OBS_SIZE, OBS_SIZE))
    object_list=[]
    while world_state.is_mission_running:
        time.sleep(0.1)
        world_state = agent_host.getWorldState()
       
        if len(world_state.errors) > 0:
            raise AssertionError('Could not load grid.')

        if world_state.number_of_observations_since_last_state > 0:
            # First we get the json from the observation API
            msg = world_state.observations[-1].text
            observations = json.loads(msg)
            # Get observation
            if weather != 'clear':
                object_list.append(weather)
            else:
                object_list.append('sunny')

            if time_now>13000:
                object_list.append("night")
            else:
                object_list.append("morning")
            grid = observations['floorAll']
            
            if grid.count('water')>3:
                object_list.append("river")
            if grid.count('leaves')>2:
                object_list.append("tree")
            if grid.count('lava')>1:
                object_list.append("lava")
                
            animal=observations['NearbyEntities']
            target_ani=['Sheep','Cow','Pig']
            for i in animal:
                if i['name'] not in object_list and i['name'] in target_ani:
                    object_list.append(i['name'])
            break
            
           
    return object_list

 
missionXML='''<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
            <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            
              <About>
                <Summary>Generate Text With Malmo</Summary>
              </About>
              
            <ServerSection>
              <ServerInitialConditions>
                <Time>
                    <StartTime>'''+str(time_now)+'''</StartTime>
                    <AllowPassageOfTime>false</AllowPassageOfTime>
                </Time>
                <Weather>'''+WEATHER+'''</Weather>
              </ServerInitialConditions>
              <ServerHandlers>
                  <FileWorldGenerator src="'''+ WORLD_PATH + '''"/>
                  <ServerQuitFromTimeUp timeLimitMs="3000000"/>
                  <ServerQuitWhenAnyAgentFinishes/>
                </ServerHandlers>
              </ServerSection>
              
              <AgentSection mode="Survival">
                <Name>MalmoTutorialBot</Name>
                <AgentStart>
                    <Inventory>
                        <InventoryItem slot="8" type="diamond_pickaxe"/>
                    </Inventory>
                </AgentStart>
                <AgentHandlers>
                <ChatCommands/>
                  <ObservationFromFullStats/>
                  <ObservationFromGrid>
                            <Grid name="floorAll">
                                <min x="-'''+str(int(OBS_SIZE/2))+'''" y="-2" z="-'''+str(int(OBS_SIZE/2))+'''"/>
                                <max x="'''+str(int(OBS_SIZE/2))+'''" y="3" z="'''+str(int(OBS_SIZE/2))+'''"/>
                            </Grid>
                  </ObservationFromGrid>
                  <ObservationFromNearbyEntities>
                        <Range name="NearbyEntities"
                        xrange="10"
                        yrange="4"
                        zrange="2"
                        update_frequency="1"/>
                  </ObservationFromNearbyEntities>
                  <ContinuousMovementCommands turnSpeedDegs="180"/>
                  <InventoryCommands/>
                  <AgentQuitFromReachingPosition>
                    <Marker x="-26.5" y="40.0" z="0.5" tolerance="0.5" description="Goal_found"/>
                  </AgentQuitFromReachingPosition>
                </AgentHandlers>
              </AgentSection>
            </Mission>'''

# Create default Malmo objects:
prvious_list=[]
agent_host = MalmoPython.AgentHost()
try:
    agent_host.parse( sys.argv )
except RuntimeError as e:
    print('ERROR:',e)
    print(agent_host.getUsage())
    exit(1)
if agent_host.receivedArgument("help"):
    print(agent_host.getUsage())
    exit(0)

my_mission = MalmoPython.MissionSpec(missionXML, True)
my_mission_record = MalmoPython.MissionRecordSpec()

# Attempt to start a mission:
max_retries = 3
for retry in range(max_retries):
    try:
        agent_host.startMission( my_mission, my_mission_record )
        break
    except RuntimeError as e:
        if retry == max_retries - 1:
            print("Error starting mission:",e)
            exit(1)
        else:
            time.sleep(2)

# Loop until mission starts:

world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
    print(".", end="")
    time.sleep(0.1)
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print("Error:",error.text)

print()
TOTAL_LOOP = 25
textGen = TextGen.TextGen()
genre_list = ['superhero', 'sci_fi', 'action', 'drama', 'horror', 'thriller']
mode = 'nucleus'
search_size = 10
# Loop until mission ends:

while world_state.is_mission_running:
    time.sleep(0.1)
    signal = input('Please enter any key to start detect')
    world_state = agent_host.getWorldState()
    for error in world_state.errors:
        print("Error:",error.text)
    obs=get_observation(world_state,WEATHER,time_now)
    obs = [x.lower() for x in obs]
    
    #out put observation
    agent_host.sendCommand("chat " + 'observation: ' + str(obs))
    print()
    print('observation:', obs)
    
    prvious_list = list(obs)

    #out put genre
    if 'night' in obs:
        if random.random()<0.8:
            genre = genre_list[randint(4,6)]
        else:
            genre = genre_list[randint(0,4)]
    elif 'lava' in obs:
        if random.random()<0.8:
            genre = genre_list[randint(0,2)]
        else:
            genre = genre_list[randint(2,6)]
    else:
        if random.random()<0.8:
            genre = genre_list[randint(0,4)]
        else:
            genre = genre_list[randint(4,6)]
    
    print('genre:', genre)
    agent_host.sendCommand("chat " + 'genre: ' + genre)

    if genre == 'sci_fi' or genre =='superhero':
        input_word = ['In the future', 'In the year of 2077', 'Yesterday'][randint(0, 3)]
    elif genre == 'horror' or genre =='thriller':
        input_word = ['In a darks house', 'In the deep forest', 'Midnight'][randint(0, 3)]
    else:
        input_word = ['This summer', 'Long long ago', 'Today'][randint(0, 3)]
    
    target_list = obs
    sentence=textGen.generate_target_list(input_word, target_list, genre, search_size,TOTAL_LOOP, mode)
    print()
    print(sentence)
    print()
    agent_host.sendCommand("chat " + sentence)
        
   

print()
print("Mission ended")
# Mission has ended.
