function b.drawHBar(bitmap, filled, x, y, width, height, ammo, ammoleft)
 if filled > 0 then
  if ammo then
   local counter = 0
   local divider = (width)/ammo
   while counter < ammoleft do
    bitmap:draw_box(x+1+counter*divider, y+1, x-1+counter*divider+divider, y+height-1, hudtools.greenRedInterpol(1-filled))
    counter = counter + 1
   end
  else
   bitmap:draw_box(x+1, y+1, x+1+(width-2)*filled, y+height-1, hudtools.greenRedInterpol(1-filled))
  end
 end
end


--HUD etc
function b.initHUD(options)

 if not DEDSERV then
  options = options or {} 

  local testCrosshair = sprites_load("crosshair")
  local killsMeter = sprites_load("kills")

  local narcon_e1 = sprites_load("narcon_energy1.png")
  local narcon_e2 = sprites_load("narcon_energy2.png")
  local narcon_e3 = sprites_load("narcon_energy3.png")

  local narcon_l1 = sprites_load("narcon_life1.png")
  local narcon_l2 = sprites_load("narcon_life2.png")
  local narcon_l3 = sprites_load("narcon_life3.png")

  local narcon_aim = sprites_load("narcon_aim.bmp")
  local narcon_sense = sprites_load("narcon_sense.bmp")

  local hudFont = fonts.liero


--WORMRENDER
  function bindings.wormRender(x, y, worm, viewport, ownerPlayer)
   local ownViewport = ( ownerPlayer == worm:player() )
   local bitmap = viewport:bitmap()

   if  ownViewport then

--weapon changing
    if worm:is_changing() then
     fonts.minifont:render( bitmap, string.sub(worm:current_weapon():type():name(), 2), x, y - 10, color(255, 255, 255), Font.Formatting + Font.CenterH + Font.CenterV )
    end

--start of faction-specifics
    local faction = worm:data().faction

    if faction then
     if faction == 0 then

--0 - aiming
      local dx, dy = angle_vector(worm:angle(), 2)
      for i = 0, 20 do
       gfx_set_add(i*6.4)
       local distance = i * 0.5 + 10
       narcon_aim:render(bitmap, 0, x + dx * distance, y + dy * distance)
      end

--0 - sensing
      local angle = worm:data().sensing_angle
      if angle then
       local dx, dy = angle_vector(worm:data().sensing_angle, 2)
       for i = 0, 20 do
        gfx_set_add(i*3.2)
        local distance = i * 0.5 + 10
        narcon_sense:render(bitmap, 0, x + dx * distance, y + dy * distance)
       end
      end

      gfx_reset_blending()
      
     
     elseif faction == 1 then

--1 - crosshair(with spread)
      if not (worm:data().lasersight1 or worm:data().lasersight2) then
  
       local dx, dy = angle_vector(worm:angle(), 50)
 
       if worm:data().spread then
        spread1 = worm:data().spread*0.3 + 2
        spread2 = worm:data().spread*0.15 + 1
       else
        spread1 = 2
        spread2 = 1
       end

       local xpos = math.floor(x+dx)
       local ypos = math.floor(y+dy)
 
       bitmap:line(xpos+spread1, ypos, xpos+spread2, ypos, color(0,255,0))
       bitmap:line(xpos-spread1, ypos, xpos-spread2, ypos, color(0,255,0))
       bitmap:line(xpos, ypos+spread1, xpos, ypos+spread2, color(0,255,0))
       bitmap:line(xpos, ypos-spread1, xpos, ypos-spread2, color(0,255,0))

      end
     end
--end of faction specifics
    end
   end
  end

--VIEWPORTRENDER	
  function bindings.viewportRender(viewport, worm)
   local spacer = 19
 
   local bitmap = viewport:bitmap()
   local faction = worm:data().faction

   local filled = (worm:health() / 100)
   local limit = 200-filled*2

--start of faction specifics
   if faction and worm:health() > 0 then
    if faction == 0 then

--HEALTH
     local health = worm:health()
     local h1 = 0
     local h2 = 0
     local h3 = 0
     if health > 100 then
      h3 = 2.55 * (health - 100)
     end
     if health > 50 then
      if health < 100 then
       h2 = (health - 50) * 2 * 2.55
      else
       h2 = 255
      end
     end
     if health < 50 then
      h1 = health * 2 * 2.55
     else
      h1 = 255
     end
     local x = bitmap:w() - 20
     gfx_set_add(h1)
     narcon_l1:render(bitmap, 0, x, 1)
     gfx_set_add(h2)
     narcon_l2:render(bitmap, 0, x, 1)
     gfx_set_add(h3)
     narcon_l3:render(bitmap, 0, x, 1)
     gfx_reset_blending()
      
--ENERGY
     local energy = worm:data().energy
     local e1 = 0
     local e2 = 0
     local e3 = 0
     if energy > 100 then
      e3 = 2.55 * (energy - 100)
     end
     if energy > 50 then
      if energy < 100 then
       e2 = (energy - 50) * 2 * 2.55
      else
       e2 = 255
      end
     end
     if energy < 50 then
      e1 = energy * 2 * 2.55
     else
      e1 = 255
     end
     gfx_set_add(e1)
     narcon_e1:render(bitmap, 0, 1, 1)
     gfx_set_add(e2)
     narcon_e2:render(bitmap, 0, 1, 1)
     gfx_set_add(e3)
     narcon_e3:render(bitmap, 0, 1, 1)
     gfx_reset_blending()

    elseif faction == 1 then

--BACKGROUND

     gfx_set_alpha(100)
     bitmap:draw_box(bitmap:w()-129, 3,  bitmap:w()-4, 16, 0)
     gfx_reset_blending()

--KILLS/DEATHS
     killsMeter:render(bitmap, 0, bitmap:w()-128, 4)
     fonts.minifont:render(bitmap, worm:player():kills(), bitmap:w()-117, 3, color(255, 255, 255), Font.CenterH)
     fonts.minifont:render(bitmap, worm:player():deaths(), bitmap:w()-117, 11, color(255, 255, 255), Font.CenterH)

--HEALTH/ARMOUR
     if worm:data().armour then
      bitmap:draw_box(bitmap:w()-108, 11, bitmap:w()-4, 16, color(64, 64, 192))
     end
     b.drawHBar( bitmap, filled, bitmap:w()-108, 11, 104, 5 )

--AMMOBAR
     local w = worm:current_weapon()
     if w:is_reloading() then
      filled = 1 - w:reload_time() / w:type():reload_time()
     else
      filled = w:ammo() / w:type():ammo()
     end
      if worm:data().ammo and not(w:is_reloading()) then
       ammo = w:type():ammo()
       ammoleft = w:ammo()
      else
       ammo = nil
       ammoleft = nil
      end
     b.drawHBar( bitmap, filled, bitmap:w() -108, 3, 104, 5, ammo, ammoleft )

--DRUGS
     local boost = worm:data().boost
     local stim = worm:data().stim
  
     if boost or stim then
      gfx_set_alpha(160)
      bitmap:draw_box(bitmap:w()-108, spacer, bitmap:w()-4, spacer + 5, 0)
      gfx_reset_blending()
      if boost and stim then
       bitmap:draw_box(bitmap:w()-107, spacer + 1, bitmap:w()-5, spacer + 4, color(255, 0, 0))
      end
      if boost then
       bitmap:draw_box(bitmap:w()-107, spacer + 1, bitmap:w()-107+1.02*boost, spacer + 2, color(100, 255, 100))
      end
      if stim then 
       bitmap:draw_box(bitmap:w()-107, spacer + 3, bitmap:w()-107+1.02*stim, spacer + 4, color(100, 100, 255))
      end
      spacer = spacer + 9
     end
  
--BUTTERFLY LAUNCHER
     local distance = worm:data().butterfly_distance
     if distance then
      local filled = 1
      if distance < 120 then
       filled = distance / 120
      end
      hudFont:render( bitmap, floor(distance), bitmap:w() - 18, spacer, hudtools.greenRedInterpol(1-filled), Font.Shadow )
      spacer = spacer + 9
     end

    end
   end
--END OF FACTION-SPECIFICS


--DEAD?
   if filled <= 0 then
    hudFont:render( bitmap, "You are dead... Press [JUMP] to respawn!", bitmap:w()/2, 18, color(255, 255, 255), Font.Shadow + Font.CenterH )
   end


--BLINDED? and CONFUSED? DEADISH?
   if worm:health() > 0 and worm:data().confused then
    gfx_set_add(255)
    local blinded = worm:data().blinded * 100
    if blinded > 255 then
     blinded = 255
    end
    bitmap:draw_box(0, 0, bitmap:w(), bitmap:h(), color( blinded, blinded, blinded)) 
    gfx_set_add(255)
    local health = worm:health()
    local confused = worm:data().confused
    if confused > 1 then
     confused = 1
    end
    if health < 16 then
     wounded = 128-health*8
    else
     wounded = 0
    end
    bitmap:draw_box(0, 0, bitmap:w(), bitmap:h(), color(wounded, 0, confused*255))
    gfx_reset_blending()
   end

  end

 end
end


function b.init(options)
	b.initHUD(options)
	common.initChat(options)
	common.initScoreboard(options)
	weaponselect.initWeaponSelection(options)
	
	console_register_command("SHOWMENU", function()
		mainm.show()
	end)

	if not DEDSERV then
		mainm.init()
	end

end