"""
thepolm3
how high can you go? designed to reduce the need for boundaries
"""

LIMIT=2 #highest players can go

def apply_script(protocol, connection, config):
    
    class LimitConnection(connection):
        last_safe_pos=(0,0,0)

        def on_spawn_location(self,pos):
            if pos<=LIMIT:
                for z in range(LIMIT,62):
                    if is_location_free(pos[0],pos[1],z):
                        return (pos[0],pos[1],z)
            return connection.on_spawn_location(self,pos)
                
        def on_position_update(self):
            pos=self.get_location()
            if pos[2]<=LIMIT:
                self.set_location(self.last_safe_pos)
            else:
                self.last_safe_pos=int(pos[0]),int(pos[1])+0.5,pos[2]-0.5
            return connection.on_position_update(self)

        def on_orientation_update(self,x,y,z):
            if self.get_location()[2]<=LIMIT: self.set_location(self.last_safe_pos)
            return connection.on_orientation_update(self,x,y,z)
    return protocol, LimitConnection
