--ATROMB

function a.atromb_select(worm)
 if worm:data().spread < 30 then
  worm:data().spread = 30
 end
 worm:data().lasersight1 = randomint(0, 360)
 worm:data().lasersight2 = nil
 worm:data().stability = 1
 worm:data().portability = 30
 worm:data().ammo = 1
end

function a.atromb_think(worm)
 local atromb = worm:data().atromb
 if atromb then
  worm:data().atromb = atromb - 1
  if atromb == 1 then
   worm:data().atromb = nil
   worm:shoot(atromb_case, 1, 0.4, 0.2, 0, 0, 50, 230, 2)
  end
 end
end

function a.atromb_shoot(worm)
 worm:shoot(atromb_bullet, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 worm:data().spread = worm:data().spread + 30
 worm:data().lasersight1 = 40
 worm:data().atromb = 40
end


--AZAZEL

function a.azazel_select(worm)
 if worm:data().spread < 20 then
  worm:data().spread = 20
 end
 worm:data().lasersight1 = randomint(0, 360)
 worm:data().lasersight2 = nil
 worm:data().stability = 0.99
 worm:data().portability = 0.3
 worm:data().ammo = 1
end

function a.azazel_shoot(worm)
 worm:data().lasersight1 =  worm:data().lasersight1 + randomint(0,10)
 worm:shoot(azazel_case, 1, 0.4, 0.2, 0, 0, 50, 230, 2)
 if worm:current_weapon():ammo() % 3 == 0  then
  worm:shoot(azazel_bullet_t, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 else
  worm:shoot(azazel_bullet, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 end
 worm:data().spread = worm:data().spread + 4
end


--BOOST/STIM (DRUGS)

function a.drugselect(worm)
 worm:data().spread = 0
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0
 worm:data().portability = 0
 worm:data().ammo = 1
end

function a.boost_shoot(worm)
 local boost = worm:data().boost
 if boost then
  boost = boost + 10
  if boost > 100 then
   boost = 100
  end
 else
  boost = 10
 end
 worm:data().boost = boost
end

function a.stim_shoot(worm)
 local stim = worm:data().stim
 if stim then
  stim = stim + 10
  if stim > 100 then
   stim = 100
  end
 else
  stim = 10
 end
 worm:data().stim = stim
end


--BUTTERFLY

function a.butterfly_select(worm)
 if worm:data().spread < 20 then
  worm:data().spread = 20
 end
 worm:data().lasersight1 = randomint(0, 360)
 worm:data().lasersight2 = nil
 worm:data().stability = 0.99
 worm:data().portability = 0.3
 worm:data().ammo = 1
end

function a.butterfly_think(worm)
 local player = worm:player()
 local mindistance = 999
 local x1, y1 = worm:pos()
 worm:data().butterfly_distance = 999
 worm:data().butterfly_target = nil
 for enemy_player in game_players() do
  if enemy_player ~= player then
   local enemy_worm = enemy_player:worm()
   local x2, y2 = enemy_worm:pos()
   local distance = vector_distance(x1, y1, x2, y2)
   if distance < mindistance then
    mindistance = distance
    worm:data().butterfly_target = enemy_worm
    worm:data().butterfly_distance = distance
   end
  end 
 end
end

function a.butterfly_shoot(worm)
 local bullet = worm:shoot(butterfly_bullet, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 bullet:data().ttl = worm:data().butterfly_distance
 worm:data().spread = worm:data().spread + 30
 worm:data().lasersight1 = 40
end

function a.butterfly_bullet(object)
 local ttl = object:data().ttl
 if ttl < 0 then
  object:shoot(butterfly_explosion, 1, 0, 0, 0, 0, 0, 0, 0)
  object:remove()
 else
  object:data().ttl = ttl - 1
 end
end

--CANDLELIGHTER

function a.candlelighter_select(worm)
 worm:data().spread = 0
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0
 worm:data().portability = 0
 worm:data().ammo = nil
end

--FLASHBANG

function a.flashbang_select(worm)
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0
 worm:data().portability = 0
 worm:data().ammo = nil
end

function a.flashbang(object, worm)
 objectx, objecty = object:pos()
 wormx, wormy = worm:pos()
 if not(map_is_blocked(wormx, wormy, objectx, objecty)) then
  worm:data().blinded = worm:data().blinded + 0.04*(150-vector_distance(wormx, wormy, objectx, objecty))
  worm:data().confused = worm:data().confused + worm:data().blinded/10
 end
 if worm:data().blinded > 10 then
  worm:data().blinded = 7
 end
end

function a.flashbang_init(object)
 object:data().time = 0
end

function a.flashbang_think(object)
 worm = object:player():worm()
 local x, y = object:pos()
 fx.lensflare(x, y, 255-object:data().time*5)
 object:data().time = object:data().time + 1
end

--KEVLAR/SCANNER

function a.kevlar(worm)
 worm:data().armour = 1
end

--KNIFE

function a.knife_select(worm)
 worm:data().spread = 0
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0
 worm:data().portability = 0
 worm:data().ammo = nil
 worm:data().knife = 0
end

function a.knife_think(worm)
 local dx, dy = worm:spd()
 speed = vector_distance(0, 0, dx, dy)
 if speed > 0.4 then
  speed = 0
 end
 local knife = worm:data().knife + speed * 0.3
 if knife > 6.28 then
  knife = 0
 end
 if speed < 0.2 then
  knife = knife * 0.95
 end
 worm:data().knife = knife
 if not(worm:current_weapon():is_reloading()) then
  worm:shoot(knife_hand, 1, 3, 0, 0, 0, 0, math.sin(knife)*10, 1 + math.cos(knife*2))
 end
end

function a.knife_wall(object)
 local dx, dy = object:spd()
 local move_angle = vector_direction(dx, dy)
 if move_angle < 0 then
  move_angle = move_angle + 360
 end
 dangle = math.abs(move_angle-object:angle())
 if dangle < 120 or dangle > 240 then
  local knife = object:shoot(knife_bounced, 1, 0, 0, 0, 0, 0, 0, 0)
  knife:set_spd(0.3*dx, 0.3*dy)
 else 
  object:shoot(knife_stuck, 1, 0, 0, 0, 0, 0, 0, 0)
 end
end

function a.knife_hit(object, worm)
 local knife = worm:shoot(knife_worm, 1, 0, 0, 0, 0, 0, 0, 0)
 knife:data().target = worm
 knife:data().start_angle = object:angle() - worm:angle()
end

function a.knife_worm(object)
 worm = object:data().target
 if worm:health() > 0 then
  object:set_pos(worm:pos())
  object:set_angle(object:data().start_angle + worm:angle())
 else
  object:remove()
 end
end

--MAMMOTH

function a.mammoth_select(worm)
 if worm:data().spread < 30 then
  worm:data().spread = 30
 end
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0.995
 worm:data().portability = 4
 worm:data().ammo = 1
end

function a.mammoth_shoot(worm)
 worm:shoot(mammoth_case, 1, 0.4, 0.2, 0, 0, 50, 230, 2)
 if worm:current_weapon():ammo() % 3 == 0 then
  worm:shoot(mammoth_bullet_t, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 else
  worm:shoot(mammoth_bullet, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 end
 worm:data().spread = worm:data().spread + 6
end

function a.mammoth_flare(object)
 local time = object:data().time
 if time then
  object:data().time = time + 0.5
 else
  object:data().time = 0.5
  time = 0
 end
 object:shoot(muzzle_flare, 4, 0.1, 0.1, 0, 0, 360, 0, 2*math.sin(time))
end

--MARSDEN GRENADE

function a.marsden_select(worm)
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0
 worm:data().portability = 0
 worm:data().ammo = nil
end

function a.marsden(object, worm)
 local objectx, objecty = object:pos()
 local wormx, wormy = worm:pos()
 local range = vector_distance(objectx, objecty, wormx, wormy)
 if not(map_is_blocked(wormx, wormy, objectx, objecty)) then
  worm:damage(120-1.2*range,object:player())
 end
end


function a.marsden_init(object)
 object:data().time = 0
end

function a.marsden_think(object)
 worm = object:player():worm()
 local x, y = object:pos()
 fx.lensflare(x, y, 100 - object:data().time * 2)
 object:data().time = object:data().time + 1
end

--NEEDLES

function a.needles(worm)
 local energy = worm:data().energy
 if energy > 10 then
  energy = energy - 10
  worm:shoot(needle_bullet, 1, 1, 0.2, 0, 0, 10, 0, 5)
  worm:data().energy = energy
 end
end

--WESLEY MK 2

function a.wesley2_select(worm)
 if worm:data().spread < 30 then
  worm:data().spread = 30
 end
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = nil
 worm:data().stability = 0.98
 worm:data().portability = 0.3
 worm:data().ammo = 1
end

function a.wesley2_shoot(worm)
 worm:shoot(wesley2_bullet, 1, 1, 0, 0, 0, worm:data().spread, 0, 5)
 worm:shoot(wesley2_case, 1, 0.4, 0.2, 0, 0, 50, 230, 2)
 worm:data().spread = worm:data().spread + 8
end

--WESLEY MK 12

function a.wesley12_select(worm)
 if worm:data().spread < 30 then
  worm:data().spread = 30
 end
 worm:data().lasersight1 = nil
 worm:data().lasersight2 = randomint(0, 360)
 worm:data().stability = 0.99
 worm:data().portability = 0.3
 worm:data().ammo = 1
end

function a.wesley12_think(worm)
 local wesley12 = worm:data().wesley12
 if wesley12 then
  worm:data().wesley12 = wesley12 - 1
  if wesley12 == 1 then
   worm:data().wesley12 = nil
   worm:shoot(wesley12_case, 1, 0.4, 0.2, 0, 0, 50, 230, 2)
  end
 end
end

function a.wesley12_shoot(worm)
 local direction = math.random(-worm:data().spread, worm:data().spread)
 worm:shoot(wesley12_bullet, 12, 1, 0, 0, 0, 12, direction, 5)
 worm:data().spread = worm:data().spread + 30
 worm:data().wesley12 = 40
end

function a.wesley12_flare(object)
 local time = object:data().time
 if time then
  object:data().time = time + 0.5
 else
  object:data().time = 0.5
  time = 0
 end
 object:shoot(muzzle_flare, 1, 0.1, 0.1, 0, 0, 360, 0, 2*math.sin(time*0.8))
end

--MAIN LOOP FOR BAD GUYS

function a.main_loop0(worm)

--set faction (lazyness)
 worm:data().faction = 0

--energy/healing
 local energy = worm:data().energy
 if energy then
  energy = energy + 0.1
  if energy > 200 then
   energy = 200
   worm:damage(-0.05)
   if worm:health() > 200 then
    worm:damage(0.05)
   end
  end
 else
  energy = 100
 end
 worm:data().energy = energy

--sensing

 local player = worm:player()
 local mindistance = 9999
 local x1, y1 = worm:pos()
 local angle = nil
 for enemy_player in game_players() do
  if enemy_player ~= player then
   local enemy_worm = enemy_player:worm()
   local x2, y2 = enemy_worm:pos()
   local distance = vector_distance(x1, y1, x2, y2)
   if distance < mindistance then
    mindistance = distance
    angle = vector_direction(x1, y1, x2, y2)
   end
  end
 end

 if angle then
  worm:data().sensing_angle = angle
 end

--bleeding
 if worm:health() < randomfloat(-1000,40) then
  worm:shoot(bloodgen0, 1, 0.3, 0.3, 0, 0, 360, 0, 0)
 end

--confused/blinded
 worm:data().confused = worm:data().confused * 0.999
 worm:data().blinded = worm:data().blinded - 0.01
 if worm:data().blinded < 0 then
  worm:data().blinded = 0
 end

end


--MAIN LOOP FOR GOOD GUYS

function a.main_loop1(worm)

--set faction (lazyness)
 worm:data().faction = 1

--bleeding
 if worm:health() < randomfloat(-1000,40) then
  worm:shoot(bloodgen1, 1, 0.3, 0.3, 0, 0, 360, 0, 0)
 end

--confused/blinded
 worm:data().confused = worm:data().confused * 0.999
 worm:data().blinded = worm:data().blinded - 0.01
 if worm:data().blinded < 0 then
  worm:data().blinded = 0
 end

--drugs
 local boost = worm:data().boost
 local stim = worm:data().stim

 if boost then
  boost = boost - 0.02
  if boost < 0 then
   boost = nil
  end
  worm:data().boost = boost
  if worm:health() < 100 then
   worm:damage(-0.0333)
  end
 end

 if stim then
  stim = stim - 0.02
  if stim < 0 then
   stim = nil
  end
  worm:data().stim = stim
 end

 if stim and boost then
  stim = stim - 0.02
  boost = boost - 0.02
  worm:damage(0.0666, worm:player())
 end

--aiming
 local wx, wy = worm:pos()
 local stability = worm:data().stability
 local portability = worm:data().portability
 local confused = worm:data().confused
 local spread = worm:data().spread
 local health = worm:health()

 if not (map_is_particle_pass(wx, wy + 10) and map_is_particle_pass(wx + 1, wy + 5) and map_is_particle_pass(wx - 1, wy + 5)) then

  local drugsfactor = 1
  if worm:data().stim then
   drugsfactor = 0.97
  end
  spread = (spread - 0.001 * health * (1 - confused) + 0.0005) * stability * drugsfactor
 end

 local wormspeed = vector_distance(0, 0, worm:spd())
 if wormspeed > 0.2 then
  spread = spread + wormspeed * portability
 end

 if spread < 0 then
  spread = 0
 end

 if spread > 36 then
  spread = 36
 end
 worm:data().spread = spread

--lasersights
 local lasersight1 = worm:data().lasersight1
 if lasersight1 then
  worm:data().lasersight1 = lasersight1 + randomfloat(0, 0.02)
  worm:shoot(lasersight1_projectile, 1, 1, 0, 0, 0, 0, math.cos(lasersight1*0.7)*math.sin(lasersight1)*worm:data().spread, 5)
 end

 local lasersight2 = worm:data().lasersight2
 if lasersight2 then
  worm:data().lasersight2 = lasersight2 + randomfloat(0, 0.02)
  worm:shoot(lasersight2_projectile, 1, 1, 0, 0, 0, 0, math.cos(lasersight2*0.7)*math.sin(lasersight2)*worm:data().spread, 5)
 end
end


--LASERSIGHT FUNCTIONS

function a.lasersight1_draw(object)
 if object:player():is_local() then
  if object:player():worm():data().spread < 2 then
   object:shoot(lasersight1_line_rr, 1, 1, 0, 0, 0, 0, 0, 0)
  else
   object:shoot(lasersight1_line_r, 1, 1, 0, 0, 0, 0, 0, 0)
  end
 end
end

function a.lasersight2_draw(object)
 if object:player():is_local() then
  if object:player():worm():data().spread < 2 then
   object:shoot(lasersight2_line_rr, 1, 1, 0, 0, 0, 0, 0, 0)
  else
   object:shoot(lasersight2_line_r, 1, 1, 0, 0, 0, 0, 0, 0)
  end
 end
end

--CONFUSE FUNCTION (0.1)

function a.confuse(object, worm2)
 worm2:data().confused = worm2:data().confused + 0.1
end

--HIT TYPES

function a.hit_clean(object, worm2)
 worm2:data().deathtype = "CLEAN"
end

function a.hit_gib(object, worm2)
 worm2:data().deathtype = "GIB"
end

function a.hit_burn(object, worm2)
 worm2:data().deathtype = "BURN"
end

--BLEED TYPES

function a.bleed_bullet(object, worm2)
 faction = worm2:data().faction
 if faction then
  if faction == 0 then
   object:shoot(bloodgen0, 7, 0.5, 0.5, 0, 0, 25, 180, 0)
  elseif faction == 1 then
   object:shoot(bloodgen1, 4, 0.4, 0.4, 0, 0, 20, 180, 0)
  end
 end
end

--BLOOD DAMAGE

function a.blood_damage(object, worm2)
 local faction = worm2:data().faction
 if faction then
  if faction == 1 then
   worm2:damage(1, object:player())
  end
 end
end

--CONFUSE FUNCTION (0.1)

function a.confuse(object, worm2)
 worm2:data().confused = worm2:data().confused + 0.1
end

--SET ANGLE

function a.set_angle(object)
 object:set_angle(vector_direction(0, 0, object:spd()))
end

--CASE BOUNCE FUNCTION

function a.case_onground(object)
 object:set_angle(90)
 local x, y = object:spd()
 x = x * 0.4
 y = y * 0.4
 if x < 0.01 and y < 0.01 then
  x = 0
  y = 0
 end
 object:set_spd(x, y)
end

function a.knife_onground(object)
 object:set_angle(90)
 local x, y = object:spd()
 x = x * 0.6
 y = y * 0.35
 if x < 0.01 and y < 0.01 then
  x = 0
  y = 0
 end
 object:set_spd(x, y)
end


--WEAPONSWAP FUNCTION

function a.weaponswap(worm)
 local player = worm:player()
 worm:data().butterfly_distance = nil
 worm:data().butterfly_target = nil
end

--INITS

function a.init()
 atromb_bullet = load_particle("atromb_bullet.obj")
 atromb_case = load_particle("case_atromb.obj")

 azazel_bullet = load_particle("azazel_bullet.obj")
 azazel_bullet_t = load_particle("azazel_bullet_t.obj")
 azazel_case = load_particle("case_small.obj")

 butterfly_bullet = load_particle("butterfly_bullet.obj")
 butterfly_explosion = load_particle("butterfly_explosion.obj")

 knife_hand = load_particle("knife_hand.obj")
 knife_stuck = load_particle("knife_stuck.obj")
 knife_bounced = load_particle("knife_bounced.obj")
 knife_worm = load_particle("knife_worm.obj")

 mammoth_bullet = load_particle("mammoth_bullet.obj")
 mammoth_bullet_t = load_particle("mammoth_bullet_t.obj")
 mammoth_case = load_particle("case_large.obj")

 needle_bullet = load_particle("needle.obj")

 wesley2_bullet = load_particle("wesley2_bullet.obj")
 wesley2_case = load_particle("case_small.obj")

 wesley12_bullet = load_particle("wesley12_bullet.obj")
 wesley12_case = load_particle("case_wesley12.obj")

 muzzle_flare = load_particle("muzzle_flare.obj")

 lasersight1_projectile = load_particle("lasersight1.obj")
 lasersight1_line_r = load_particle("lasersight1_line_r.obj")
 lasersight1_line_rr = load_particle("lasersight1_line_rr.obj")

 lasersight2_projectile = load_particle("lasersight2.obj")
 lasersight2_line_r = load_particle("lasersight2_line_r.obj")
 lasersight2_line_rr = load_particle("lasersight2_line_rr.obj")

 bloodgen0 = load_particle("bloodgen0.obj")
 bloodgen1 = load_particle("bloodgen1.obj")
 death0_burn = load_particle("death0_burn.obj")
 death1_burn = load_particle("death1_burn.obj")
 death0_clean = load_particle("death0_clean.obj")
 death1_clean = load_particle("death1_clean.obj")
 death0_gib = load_particle("death0_gib.obj")
 death1_gib = load_particle("death1_gib.obj")

--DEATH
 function bindings.wormDeath(worm)
  --RESET STUFF
  worm:data().spread = 0
  worm:data().stability = 0
  worm:data().portability = 0
  worm:data().confused = 0
  worm:data().blinded = 0
  worm:data().boost = nil
  worm:data().stim = nil
  worm:data().energy = 100
  --MAKE CORPSE/GIBS/ASHES/WOOTEVER
  local faction = worm:data().faction
  local type = worm:data().deathtype
  if faction and type then
   if faction == 0 then
    if type == "BURN" then
     worm:shoot(death0_burn, 1, 0, 0, 0, 0, 360, 0, 0)
    elseif type == "CLEAN" then
     worm:shoot(death0_clean, 1, 0, 0, 0, 0, 360, 0, 0)
    elseif type == "GIB" then
     worm:shoot(death0_gib, 1, 0, 0, 0, 0, 360, 0, 0)
    end
   elseif faction == 1 then
    if type == "BURN" then
     worm:shoot(death1_burn, 1, 0, 0, 0, 0, 360, 0, 0)
    elseif type == "CLEAN" then
     worm:shoot(death1_clean, 1, 0, 0, 0, 0, 360, 0, 0)
    elseif type == "GIB" then
     worm:shoot(death1_gib, 1, 0, 0, 0, 0, 360, 0, 0)
    end
   end
  end
 end


--PLAYERINIT
 function bindings.playerInit(player)
  worm = player:worm()
  worm:data().spread = 0
  worm:data().stability = 0
  worm:data().portability = 0
  worm:data().confused = 0
  worm:data().blinded = 0
 end

end