def determine_order(self):
        pos, com, success = self.perception
        pos.append(array([0,0]))

        pos = list(map(self.transform2, pos))

        leaveorder = list(reversed(buildorder(pos)))

        for i in range(0,len(leaveorder)):
            if array_equal(array([0,0]),leaveorder[i][1]):
                return i
            i += 1

        print("HOLYSHITWTFBBQ! (This should not happen)")
    def behave(self, perception):
        self.time += 1
        self.perception = perception
        pos, com, success = self.perception

        if self.selected:
            import code
            code.interact(local=locals())

        if self.cluster_radius is None:
            self.cluster_radius = calculate_cluster_radius(len(pos) + 1)

        if self.lastmove is not None and self.position is not None and success:
            if self.phase == 2:
                self.position += self.lastmove
            elif self.phase > 2:
                self.position += self.transform2(self.lastmove)
            self.lastmove = None
            
        if not self.awake:
            if self.sleepCounter <= 0:
                self.awake = True
                self.sleepCounter = 0
            else:
                self.sleepCounter -= 1
      
        if self.awake:
                    
            if self.phase == 1:
                """Move towards the center of mass"""



                if self.internal_flag:
                    self.update_flags()                

                if not self.internal_flag:
                    if self.arrived() and self.all_bees_in_cluster(self.destination) and self.position is None:
                        self.update_flags(self.destination)
                        self.position = array([0,0]) - self.destination
                    else:
                        self.set_destination(self.center_of_bees())
                else:
                    self.set_destination(None)
                
                if self.consensus_reached() and self.internal_flag and self.way_out():
                    self.phase = 2
                    self.set_destination(None)
                    self.internal_flag = False
                    self.lower_flags()

                                            
            elif self.phase == 2:
                """Move towards most popular [1,0]"""
                d = floor(self.cluster_radius*3) + 1
                direction = [array([0,d]), array([d,0]), array([0,-d]), array([-d,0])]
                
                if self.destination is None and not self.internal_flag:
                    self.set_destination(array([d,0]))
                    self.debugInformation = "Moving to 3*cluster_radius,0"
                if self.internal_flag:
                    self.update_flags()

                if self.arrived() and (self.count_cluster_size_at(array([0,0])) == 0 or self.internal_flag) and not (self.consensus_reached() and len(com) > 0):
                    self.debugInformation = "Arrived and 0,0 is empty"                    
                    most_popular = max(direction, key = self.count_cluster_size_at)

                    if not self.internal_flag:
                        self.set_destination(most_popular)
                    else:
                        self.set_destination(None)

                    if self.arrived() and self.all_bees_in_cluster(self.destination) and not self.internal_flag:
                        self.update_flags(self.destination)
                        self.debugInformation = "Arrived at most popular and everyone is here"

                if self.consensus_reached() and self.internal_flag and self.way_out():
                    self.phase = 3
                    self.set_destination(None)
                    self.debugInformation = "Everyone has raised flag"
                    self.internal_flag = False
                    self.lower_flags()

                    most_popular = min(direction, key=self.distance)
                    
                    if array_equal(most_popular, array([0,d])):
                        self.swapxy = True
                        self.position = array([self.position[1],self.position[0]])
                    elif array_equal(most_popular, array([0,-d])):
                        self.swapxy = True
                        self.flipx = True
                        self.position = array([-self.position[1],self.position[0]])
                    elif array_equal(most_popular, array([-d,0])):
                        self.flipx = True
                        self.position = array([-self.position[0],self.position[1]])


            elif self.phase == 3:
                """Move towards most popular [0,1]"""
                d = floor(self.cluster_radius*3) + 1
                direction = [array([0,d]), array([0,-d])]
                if self.destination is None and not self.internal_flag:
                    self.set_destination(array([0,d]))
                    self.debugInformation = "Moving to 0,1"

                if self.internal_flag:
                    self.update_flags()
                    
                if self.arrived() and (self.count_cluster_size_at(array([0,0])) == 0 or self.internal_flag) and not (self.consensus_reached() and len(com) > 0):
                    self.debugInformation = "Arrived and 0,0 is empty"
                    
                    most_popular = max(direction, key = self.count_cluster_size_at)

                    if not self.internal_flag:
                        self.set_destination(most_popular)
                    else:
                        self.set_destination(None)

                    if self.arrived() and self.all_bees_in_cluster(self.destination) and not self.internal_flag:
                        self.update_flags(self.destination)
                        self.debugInformation = "Arrived at most popular and everyone is here"
                        
                        if most_popular[1] < 0:
                            self.flipy = True
                            self.position = array([self.position[0],-self.position[1]])

                        self.order = self.determine_order()

                        if most_popular[1] < 0:
                            self.flipy = False
                            self.position = array([self.position[0],-self.position[1]])


                if self.consensus_reached() and self.internal_flag and self.way_out():
                    self.phase = 4
                    self.set_destination(None)
                    self.debugInformation = "Everyone has raised flag"
                    self.internal_flag = False
                    
                    most_popular = min(direction, key=self.distance)
                    if most_popular[1] < 0:
                        self.flipy = True
                        self.position = array([self.position[0],-self.position[1]])

            elif self.phase == 4:
                """ Move to formation """
                d = floor(self.cluster_radius*3) + 1

                self.debugInformation = "Phase 4"

                if self.proper_formation is None:
                    """ Setting up formation build order """
                    self.proper_formation = [ (x[0], x[1] + array([0,-d])) for x in normalize(buildorder(self.formation))]
                    

                if all(map(lambda x: self.bee_at(x[1]), self.proper_formation)):
                    self.phase = 5

                if self.destination is None and self.lower_orders_in_formation():
                    self.set_destination(self.proper_formation[self.order][1])
                    self.lastposition = self.position.copy()
                    self.stagnant = 0
                if self.destination is None and all(map(lambda x: self.bee_at(x[1]), self.proper_formation[0:self.order])):
                    self.set_destination(self.proper_formation[self.order][1])
                    self.lastposition = self.position.copy()
                    self.stagnant = 0
                else:
                    if array_equal(self.destination,self.position):
                        self.phase = 5

                if array_equal(self.lastposition,self.position) and self.destination is not None:
                    self.stagnant += 1
                    if self.stagnant > 5:
                        self.set_destination(array([0,0]))
                        self.makingway = True
                        self.stagnant = 0
                        self.moving = 0
                else:
                    self.stagnant = 0

                if self.stagnant > -1:
                    self.lastposition = self.position.copy()

                if self.makingway:
                    self.moving += 1
                    if self.moving > 3:
                        self.set_destination(self.proper_formation[self.order][1])
                        self.makingway = False

                if self.selected:
                    import code
                    code.interact(local=locals())
                    
                
        else:            
            self.lastmove = None
            if self.phase == 5:
                return (None, {"flag":self.flag,"consensus":self.consensus,"phase":self.phase,"order":self.order})
            else:
                return (array([0,0]), {"flag":self.flag,"consensus":self.consensus,"phase":self.phase,"order":self.order})

        if self.phase == 5:
            self.lastmove = None
            return (None, {"flag":self.flag,"consensus":self.consensus,"phase":self.phase,"order":self.order})

        m = self.move().copy()
        self.lastmove = m.copy()
        return (m, {"flag":self.flag,"consensus":self.consensus,"phase":self.phase,"order":self.order})