import outsmart as osmt
import numpy as np

MAP_NAME = "maps/tutorial.map"

osmt.STATE = osmt.load_state(MAP_NAME)

def step_6():
    """Resetting the robot"""
    osmt.STATE.active_ui["Reset"] = True
    osmt.script(s_text="""Well this is underwhelming, but the lab setting was not
very interesting to begin with.
Let's reset Bob.""")


def step_5():
    """Trying the policy out"""
    osmt.script(s_text="""Good Job !
Let's see what Bob has learnt by stepping through its new-found "policy".""",
                o_text="""Repeatedly press [s] on your keyboard to step through Bob's actions
until Bob collects the rocks.
If it doesnt work, you may have to train it again and then press [s]""",
                objective_function=lambda s: len(np.argwhere(np.abs(s.lab) == 4)) == 0,
                next_step=step_6)


def step_4():
    """Sucessful training"""
    osmt.script(s_text="""Good Job !
Now, training Bob will succeed.
Bob is wired to collect rocks and crystals. You can't change that.
You can, however, change the environment and use rocks and
crystals as a motivator to make Bob do what you want.
When dealing with animals food is used as a reward.
This is the same here, only with robots.""",
                o_text="Try again to train Bob by clicking on the"
                """"Train" button.""",
                objective_function=lambda s: s.log_text[0][1] == "Training"
                " successful !",
                next_step=step_5)


def step_3():
    """Rocks spawning"""
    def success(state):
        bob_on_patch = state.lab[6, 8] == -2
        rock_nearby = state.lab[7, 8] == 4
        rock_nearby = rock_nearby or state.lab[5, 8] == 4
        rock_nearby = rock_nearby or state.lab[6, 9] == 4
        rock_nearby = rock_nearby or state.lab[6, 7] == 4
        return bob_on_patch and rock_nearby
    osmt.script(s_text="""Uh-oh, something went wrong :
In this lab setting, there is no reward
that could let Bob know how it's doing.
Let's change that.""",
                o_text="""Spawn some rocks somewhere right next to Bob by right-clicking
multiple times on the appropriate tile.""",
                objective_function=success,
                next_step=step_4)


def step_2():
    """Frist training"""
    osmt.STATE.active_ui["Train"] = True
    osmt.script(s_text="""Good Job !
You can train Bob with a state-of-the-art
Reinforcement Learning algorithm.
This will allow you to train him like you would a dog or a rat.""",
                o_text="Train bob by clicking on the train button.",
                objective_function=lambda s: s.log_text[0][1] == 'Error !',
                next_step=step_3)


osmt.STATE.active_ui = {k:False for k in osmt.STATE.active_ui}
osmt.script(s_text="""This is your lab.
The blue robot is a test robot.
His name is Bob.
You can control its position.""",
            o_text="Click on the green patch to move the robot there.",
            objective_function=lambda s: s.lab[6, 8] == -2,
            next_step=step_2)

osmt.pyglet.app.run()

@@ -69,16 +69,18 @@
 l.map%22%0A%0A
+# 
 osmt.STA
@@ -111,16 +111,506 @@
 _NAME)%0A%0A
+osmt.STATE.lab%5B6,8%5D = 2%0A%0Adef step_8():%0A    %22%22%22The wild is harsh%22%22%22%0A    osmt.script(s_text=%22%22%22Well, this one is not Bob...%22%22%22)%0A%0A%0Adef step_7():%0A    %22%22%22Into the wild%22%22%22%0A    osmt.STATE.active_ui%5B%22Wild%22%5D = True%0A    osmt.script(s_text=%22%22%22Let's see what kind of problems we will have to solve%0Ain the wild.%22%22%22,%0A                o_text=%22%22%22Go into the wild by clicking on the %22Wild%22 button.%22%22%22,%0A                objective_function=lambda s: s.terrain == s.get_wild,%0A                next_step=step_8)%0A%0A%0A
 def step
@@ -815,24 +815,206 @@
 eset Bob.%22%22%22
+,%0A                o_text=%22%22%22Reset Bob by clicking on the %22Reset%22 button.%22%22%22,%0A                objective_function=lambda s: np.linalg.norm(s.omega)==0,%0A                next_step=step_7
 )%0A%0A%0Adef step
@@ -1129,17 +1129,17 @@
 stepping
- 
+%0A
 through 
@@ -1237,17 +1237,17 @@
  to step
- 
+%0A
 through 
@@ -1259,17 +1259,17 @@
  actions
-%0A
+ 
 until Bo
@@ -1331,19 +1331,20 @@
 o train 
-it 
+Bob%0A
 again an
@@ -2693,16 +2693,22 @@
 wn some 
+white 
 rocks so
@@ -2728,24 +2728,40 @@
  next to Bob
+%0A               
  by right-cl
@@ -2766,17 +2766,17 @@
 clicking
-%0A
+ 
 multiple

Implement wild/lab dichotomy