function CHOICE.KNAVE given STATION.ID
''
'' AUTHOR: Steve Bailey
'' CREATED: 7-25-90
'' DESCRIPTION:
''    This function returns with the least busy knave that is closest to
''    the current workstation
''

define STATION.ID as an integer variable
define MY.CHOICE as an integer variable

for each KNAVE, 
    with N.X.KNAVE(KNAVE) eq 0, find the first case

if found
    '' find the closest of the free knaves
    for each KNAVE with N.X.KNAVE(KNAVE) eq 0 do
        if KITCHEN.STATION(KNAVE) ne 0
            if SIDE.OF.BROILER(STATION.ID) ne 
              SIDE.OF.BROILER(KITCHEN.STATION(KNAVE))
                let DX = XLOC(KITCHEN.STATION(KNAVE)) - XLOC(.RBROILER)
                let DY = YLOC(KITCHEN.STATION(KNAVE)) - YLOC(.RBROILER)
                let DIST = SQRT.F(DX * DX + DY * DY)
                let DX = XLOC(STATION.ID) - XLOC(.RBROILER)
                let DY = YLOC(STATION.ID) - YLOC(.RBROILER)
                add SQRT.F(DX * DX + DY * DY) to DIST
            else
                let DX = XLOC(KITCHEN.STATION(KNAVE)) - XLOC(STATION.ID)
                let DY = YLOC(KITCHEN.STATION(KNAVE)) - YLOC(STATION.ID)
                let DIST = SQRT.F(DX * DX + DY * DY)
            always
        else
            let DIST = 0
        always
        compute MY.CHOICE as the minimum(KNAVE) of DIST
    loop
else
    for each KNAVE,
        compute MY.CHOICE as the minimum(KNAVE) of N.Q.KNAVE(KNAVE)
always

return with MY.CHOICE
end
