function weapons_menu.init()
 if not DEDSERV then

  local scaling = 70
  local perspective = 3

  
  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*25) *0.5
   local x = bitmap:w() / 2
   local i = 0

   local angle = worm:data().menu_angle
   if not angle then
    angle = 0
   end

   if angle > 6.28 then
    angle = 0
   end
   angle = angle + 0.02

   worm:data().menu_angle = angle

    local function drawRow(name)
     y = y + 7
     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 + 7
     i = i + 1
    end

    local function renderweapon(name)
     local weaponmodel = weapon_models[name]
     if not weaponmodel then
      weaponmodel = weapon_models["CUBE"]
     end
     local size = weaponmodel[1]
     local sin = math.sin(angle) * size
     local cos = math.cos(angle) * size
     local r = 2
     y = y + 15
     if i == o.cur then

      while weaponmodel[r] do
       local x1 = weaponmodel[r    ] * cos - weaponmodel[r + 2] * sin
       local y1 = weaponmodel[r + 1] * size
       local z1 = weaponmodel[r    ] * sin + weaponmodel[r + 2] * cos + perspective
       local x2 = weaponmodel[r + 3] * cos - weaponmodel[r + 5] * sin
       local y2 = weaponmodel[r + 4] * size
       local z2 = weaponmodel[r + 3] * sin + weaponmodel[r + 5] * cos + perspective
       local colour = weaponmodel[r + 6]
       r = r + 7
       local xb1 = x1/z1 * scaling + x
       local yb1 = y1/z1 * scaling + y
       local xb2 = x2/z2 * scaling + x
       local yb2 = y2/z2 * scaling+ y
       bitmap:linewu(xb1, yb1, xb2, yb2, colour)
      end
     else
      while weaponmodel[r] do
       local x1 = weaponmodel[r    ] * size
       local y1 = weaponmodel[r + 1] * size
       local z1 = weaponmodel[r + 2] * size + perspective
       local x2 = weaponmodel[r + 3] * size
       local y2 = weaponmodel[r + 4] * size
       local z2 = weaponmodel[r + 5] * size + perspective
       local colour = weaponmodel[r + 6]
       r = r + 7
       local xb1 = x1/z1 * scaling + x
       local yb1 = y1/z1 * scaling + y
       local xb2 = x2/z2 * scaling + x
       local yb2 = y2/z2 * scaling+ y
       bitmap:linewu(xb1, yb1, xb2, yb2, colour)
      end
     end

     y = y + 15
     i = i + 1
    end
				
    renderweapon("RANDOM")

    while i <= maxSelectableWeapons do
     renderweapon(o.list[i]:name())
    end

    renderweapon("LAUNCH")
   end
  end
		
		local function randomWeapons()
			local list = {}
			
			for i = 1, maxSelectableWeapons do
				local ok
				
				repeat
					list[i] = weapon_random()
					
					if weapon_count() < maxSelectableWeapons then
						break
					end
					
					ok = true
					for j = 1, i-1 do
						if list[j] == list[i] then
							ok = false
						end
					end
				until ok
			end
			
			return list
		end
		
		function bindings.localplayerEvent(player, event, state)
			local o = player:data().weaponSelection
			if o then
				if state then
					if event == Player.Down then
						o.cur = o.cur + 1
						player:worm():data().menu_angle = 0
						if o.cur >= selectWeaponCount then
							o.cur = 0
						end
					elseif event == Player.Up then
						o.cur = o.cur - 1
						player:worm():data().menu_angle = 0
						if o.cur < 0 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()
						elseif event == Player.Right then
							o.list[o.cur] = o.list[o.cur]:next()
						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 == 0 then
					o.list = randomWeapons()
				elseif o.cur == selectWeaponStart then
					player:select_weapons(o.list)

   					player:data().weapon_list = o.list
					player:data().weapon_pos = 1
					player:data().weaponSelection = nil

				else
					local ok
					repeat
						o.list[o.cur] = weapon_random()
						
						if weapon_count() < maxSelectableWeapons then
							break
						end
						
						ok = true
						for j = 1, maxSelectableWeapons do
							if j ~= o.cur and o.list[j] == o.list[o.cur] then
								ok = false
							end
						end
					until ok
				end
			end
		end
		
		function bindings.localplayerInit(player)
			player:data().weaponSelection =
			{
				list = randomWeapons(),
				cur = 1
			}
		end
	end
end