function weaponselect.initWeaponSelection()


 if not DEDSERV then
  local maxSelectableWeapons = 5
  local selectWeaponStart = maxSelectableWeapons+1
  local selectWeaponCount = selectWeaponStart+1

  function bindings.viewportRender(viewport, worm)
   local bitmap = viewport:bitmap()
   local player = worm:player()
   if player and player:data().weaponSelection then
    local o = player:data().weaponSelection
    local y = (bitmap:h() - selectWeaponCount * 10 - 15) / 2
    local x = bitmap:w() / 2
    local i = -1
    local function drawRow(name)
    if i == o.cur then
     fonts.liero:render(bitmap,	name, x, y, color(50, 190, 50), Font.Shadow + Font.CenterH)
    else
     fonts.liero:render(bitmap,	name, x, y, color(255, 255, 255), Font.Shadow + Font.CenterH)
    end
    y = y + 10
    i = i + 1
    end
				
    drawRow("RANDOMIZE")

    y = y + 5			

    local faction = o.list[maxSelectableWeapons + 1]
    if faction == "0" then
     drawRow("NARCON")
    elseif faction == "1" then
     drawRow("ALPHA GHOST")
    end

    y = y + 5
				
    while i <= maxSelectableWeapons do
     drawRow(string.sub(o.list[i]:name(), 2))
    end

    y = y + 5

    drawRow("ENTER COMBAT")
   end
  end

  --RANDOMWEAPONS
  function randomweapons(inlist)
   local list = inlist

   --DO THIS FOR ALL WEAPON SLOTS
   for i = 1, maxSelectableWeapons do
    local ok
    
    --DO THIS WHILE NOT OK
    while not(ok) do   

     --SELECT A WEAPON
     list[i] = weapon_random()
      
     ok = true  

     --IF WEAPON IS WRONG FACTION
     if string.sub(list[i]:name(), 1, 1) ~= list[maxSelectableWeapons + 1] then
      ok = false
     end

    --CONTINUE IF OK?
    end
 
   end
  return(list)
  end

  --RANDOMIZE
  local function randomize()
   local list = {}

   --SELECT FACTION
   local faction = randomint(0,1)
   if faction == 0 then
    list[maxSelectableWeapons + 1] = "0"
   elseif faction == 1 then
    list[maxSelectableWeapons + 1] = "1"
   end
   
   list = randomweapons(list)

   --RETURN THE LIST OF WEAPONS,FACTION
   return list
  end
	
  function bindings.localplayerEvent(player, event, state)
   local o = player:data().weaponSelection
   if o then

    local faction = o.list[maxSelectableWeapons + 1] 
    if state then
     if event == Player.Down then
      o.cur = o.cur + 1
      if o.cur >= selectWeaponCount then
       o.cur = -1
      end
     elseif event == Player.Up then
      o.cur = o.cur - 1
      if o.cur < -1 then
       o.cur = selectWeaponCount - 1
      end						
     elseif event == Player.Fire then
      o.pressed = true
     elseif o.list[o.cur] then
      if event == Player.Left then
       o.list[o.cur] = o.list[o.cur]:prev()
       while string.sub(o.list[o.cur]:name(), 1, 1) ~= faction do
        o.list[o.cur] = o.list[o.cur]:prev()
       end
      elseif event == Player.Right then
       o.list[o.cur] = o.list[o.cur]:next()
       while string.sub(o.list[o.cur]:name(), 1, 1) ~= faction do
        o.list[o.cur] = o.list[o.cur]:next()
       end
      end
     elseif o.cur == 0 then
      if event == Player.Left then
       if faction == "0" then
        o.list[maxSelectableWeapons + 1] = "1"
       elseif faction == "1" then
        o.list[maxSelectableWeapons + 1] = "0"
       end
       randomweapons(o.list)
      elseif event == Player.Right then
       if faction == "0" then
        o.list[maxSelectableWeapons + 1]  = "1"
       elseif faction == "1" then
        o.list[maxSelectableWeapons + 1]  = "0"
       end
       randomweapons(o.list)
      end
     end
    else
     if event == Player.Fire then
      o.pressed = false
     end
    end
    return true -- Swallow all events if we're in weapon selection
   end
  end


  function bindings.playerUpdate(player)
   local o = player:data().weaponSelection
   if o and o.pressed then
    if o.cur == -1 then
     o.list = randomize()
    elseif o.cur == 0  then
    elseif o.cur == selectWeaponStart then
     player:select_weapons({o.list[1],o.list[2],o.list[3],o.list[4],o.list[5]})
     player:data().weaponSelection = nil
    else
     local ok
     while not(ok) do
      o.list[o.cur] = weapon_random()
      ok = true
      if string.sub(o.list[o.cur]:name(), 1, 1) ~= o.list[maxSelectableWeapons + 1] then
       ok = false
      end
     end
    end
   end
  end


  function bindings.localplayerInit(player)
   player:data().weaponSelection =
   {list = randomize(),	cur = 0}
  end
 end
end