func Calculate(addr string, button config.ButtonStruct) int { const COST_MOVE_ONE_FLOOR = 1 const COST_DOOR_OPEN = 1 const COST_STOP = 2 elev := config.Active_elevs[addr] cost := int(math.Abs(float64(elev.Last_floor-button.Floor))) * COST_MOVE_ONE_FLOOR //Moving towards destination floor if (elev.Direction == config.DIR_UP && button.Floor > elev.Last_floor) || (elev.Direction == config.DIR_DOWN && button.Floor < elev.Last_floor) { for f := elev.Last_floor; f != button.Floor; f += int(elev.Direction) { for b := config.BUTTON_CALL_UP; b < config.BUTTON_COMMAND; b++ { if GetOrder(config.ButtonStruct{config.ButtonType(b), f}).Addr == addr { cost += COST_STOP break } } } //Should pass the floor before serving it if (elev.Direction == config.DIR_UP && button.Button_type == config.BUTTON_CALL_DOWN) || (elev.Direction == config.DIR_DOWN && button.Button_type == config.BUTTON_CALL_UP) { furthest_floor := button.Floor for f := button.Floor + int(elev.Direction); f >= 0 && f < config.NUM_FLOORS; f += int(elev.Direction) { for b := config.BUTTON_CALL_UP; b < config.BUTTON_COMMAND; b++ { if GetOrder(config.ButtonStruct{config.ButtonType(b), f}).Addr == addr { furthest_floor = f cost += COST_STOP break } } } cost += int(math.Abs(float64(furthest_floor-button.Floor))) * COST_MOVE_ONE_FLOOR * 2 } //Is moving away from destination floor } else if !elev.Is_idle { furthest_floor := -1 for f := elev.Last_floor; f >= 0 && f < config.NUM_FLOORS; f += int(elev.Direction) { for b := config.BUTTON_CALL_UP; b < config.BUTTON_COMMAND; b++ { if GetOrder(config.ButtonStruct{config.ButtonType(b), f}).Addr == addr { furthest_floor = f cost += COST_STOP break } } } cost += int(math.Abs(float64(furthest_floor-elev.Last_floor))) * COST_MOVE_ONE_FLOOR * 2 for f := elev.Last_floor; f != button.Floor; f -= int(elev.Direction) { for b := config.BUTTON_CALL_UP; b < config.BUTTON_COMMAND; b++ { if GetOrder(config.ButtonStruct{config.ButtonType(b), f}).Addr == addr { cost += COST_STOP break } } } } return cost }
func SetLights() { for { time.Sleep(100 * time.Millisecond) for floor := 0; floor < len(queue.Queue); floor++ { for button := 0; button < len(queue.Queue[floor]); button++ { if queue.Queue[floor][button].Active { SetButtonLamp(config.ButtonType(button), floor, 1) } else { SetButtonLamp(config.ButtonType(button), floor, 0) } } } } }
func ReassignOrders(addr string, ch_new_order chan<- config.ButtonStruct) { for floor := 0; floor < config.NUM_FLOORS; floor++ { for button := 0; button < config.NUM_BUTTONS; button++ { if Queue[floor][button].Addr == addr { ch_new_order <- config.ButtonStruct{config.ButtonType(button), floor} } } } }