------------------------------------------------------------------------------
--  Game class
------------------------------------------------------------------------------
-- Contains everything needed to run the game, including:
--    -Player and weapons definitions
--    -Enemies definitions
--    -Waves of enemies
--    -Engine
-- The engine is located in the update() functions of the subclasses
-- All the drawing is done in the draw() functions of the subclasses,
-- or in the ui class.
--
-- Events management is done at the end of the script
------------------------------------------------------------------------------

game = class()

-- states
game.playing = 0
game.paused = 1
game.menu = 2
game.scores = 3
game.emergency = 4  --blank screen
game.shop_state = 5
game.help = 6

game.state = game.menu
game.formerstate = game.menu    --used with emergency

------------------------------------------------------------------------------
-- pseudo-asynchronous events management
------------------------------------------------------------------------------
game.events = {}
game.events.up = false
game.events.down = false
game.events.left = false
game.events.right = false
game.events.pause = false
game.events.emergency = false
game.events.enter = false
game.events.del = false
game.events.char = nil

------------------------------------------------------------------------------
-- Player's ship
------------------------------------------------------------------------------
game.player = class()
game.player.image = image.new("\024\000\000\000\016\000\000\000\000\000\000\000\048\000\000\000\016\000\001\000wwwwww\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128wwwwwwwwwwww\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128wwwwwwwwww\000\128ww\000\128wwwwwwwwwwww\000\128\000\128wwwwwwwwwwww\000\128ww\000\128wwwwwwww\000\128wwww\000\128wwwwwwww\000\128u\129u\129\000\128wwwwwwww\000\128wwww\000\128wwwwwwww\000\128wwww\000\128wwwwwwww\000\128u\129u\129\000\128wwwwwwww\000\128wwww\000\128wwwwwwww\000\128wwwwww\000\128wwwwww\000\128u\129u\129\000\128wwwwww\000\128wwwwww\000\128wwwwwwww\000\128wwww\000\128ww\000\128\000\128\000\128\000\128u\129u\129\000\128\000\128\000\128\000\128ww\000\128wwww\000\128wwwwwwww\000\128wwww\000\128ww\000\128wwww\000\128u\129u\129\000\128wwww\000\128ww\000\128wwww\000\128wwwwwwww\000\128wwww\000\128wwww\000\128wwww\000\128\000\128wwww\000\128wwww\000\128wwww\000\128wwwwwwww\000\128wwww\000\128wwww\000\128wwwwwwwwwwww\000\128wwww\000\128wwww\000\128wwwwwwww\000\128wwww\000\128wwww\000\128wwwwwwwwwwww\000\128wwww\000\128wwww\000\128wwwwwwww\000\128\000\128\000\128\000\128wwww\000\128wwwwwwwwwwww\000\128wwww\000\128\000\128\000\128\000\128wwwwwwww\000\128wwwwww\000\128ww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128ww\000\128wwwwww\000\128wwwwwwww\000\128wwwwwwww\000\128wwww\000\252\000\128\000\128\000\252wwww\000\128wwwwwwww\000\128wwwwwwwwww\000\128wwwwww\000\128wwww\192\252\000\252\000\252\192\252wwww\000\128wwwwww\000\128wwwwwwwwwwwwww\000\128\000\128\000\128wwwwwwww\192\252\1\252wwwwwwww\000\128\000\128\000\128wwwwwwww")
game.player.image_width = 24
game.player.image_height = 16
game.player.x = 147
game.player.y = 190
game.player.move_speed = 6
game.player.max_x = 294
game.player.max_y = 196
game.player.min_x = -2
game.player.min_y = 150
game.player.money = 200
game.player.score = 0
game.player.autoshoot = 0
game.player.autoshoot_period = 10
game.player.min_shoot_period = 5
game.player.bought_weapons = {}
game.player.bought_weapons["type1"] = true
game.player.life = 30
game.player.max_life = 50
game.player.collision_rect = {{2, 2}, {20, 14}}
game.player.current_weapon = "type1"

function game.player:update()
    if game.events.right then
        if self.x + self.move_speed >= self.max_x then
            self.x = self.max_x
        else
            self.x = self.x + self.move_speed
        end
        game.events.right = false

    end
    if game.events.left then
        if self.x - self.move_speed <= self.min_x then
            self.x = self.min_x
        else
            self.x = self.x - self.move_speed
        end
        game.events.left = false
    end
    if game.events.up then
        if self.y - self.move_speed <= self.min_y then
            self.y = self.min_y
        else
            self.y = self.y - self.move_speed
        end
        game.events.up = false
    end
    if game.events.down then
        if self.y + self.move_speed >= self.max_y then
            self.y = self.max_y
        else
            self.y = self.y + self.move_speed
        end
        game.events.down = false
    end
    self.autoshoot = self.autoshoot - 1
    if self.autoshoot <= 0 then
        self.weapons[self.current_weapon]:shoot()
        self.autoshoot = self.autoshoot_period
    end
    --check if he's dead
    if self.life < 0 then
        game.state = game.scores
        game.highscores.insert(self.score + math.floor(0.5 * self.money))
    end
end

function game.player:draw(gc)
    gc:drawImage(self.image, self.x, self.y)
end

------------------------------------------------------------------------------
-- Weapons
------------------------------------------------------------------------------
-- Weapon 1
game.player.weapons = {}
game.player.weapons.type1 = class()
game.player.weapons.type1.image = image.new("\004\000\000\000\007\000\000\000\000\000\000\000\008\000\000\000\016\000\001\000ww\128\248\128\248ww\128\248\000\250\000\250\128\248\128\248 \249 \249\128\248z\255\128\248\128\248z\255\222\255\147\254\147\254\222\255wwz\255z\255wwwwwwwwww")
game.player.weapons.type1.damage = 1
game.player.weapons.type1.missile_size = {4, 7}

function game.player.weapons.type1:shoot()
    game.player.missiles.number = game.player.missiles.number + 1
    game.player.missiles.list[game.player.missiles.number] = {"type1", game.player.x + 10, game.player.y, 0, -5}
end

-- Weapon 2: Dual Cannon
game.player.weapons.type2 = class()
game.player.weapons.type2.image = game.player.weapons.type1.image
game.player.weapons.type2.damage = 1
game.player.weapons.type2.missile_size = {4, 7}

function game.player.weapons.type2:shoot()
    game.player.missiles.number = game.player.missiles.number + 2
    game.player.missiles.list[game.player.missiles.number-1] = {"type2", game.player.x, game.player.y, 0, -5}
    game.player.missiles.list[game.player.missiles.number  ] = {"type2", game.player.x + 20, game.player.y, 0, -5}
end

-- Weapon 3: Triple cannon
game.player.weapons.type3 = class()
game.player.weapons.type3.image = game.player.weapons.type1.image
game.player.weapons.type3.damage = 1
game.player.weapons.type3.missile_size = {4, 7}

function game.player.weapons.type3:shoot()
    game.player.missiles.number = game.player.missiles.number + 3
    game.player.missiles.list[game.player.missiles.number-2] = {"type3", game.player.x, game.player.y, -2, -5}
    game.player.missiles.list[game.player.missiles.number-1] = {"type3", game.player.x + 20, game.player.y, 2, -5}
    game.player.missiles.list[game.player.missiles.number  ] = {"type3", game.player.x + 10, game.player.y, 0, -5}
end

-- Weapon 4: Quad cannon
game.player.weapons.type4 = class()
game.player.weapons.type4.image = game.player.weapons.type1.image
game.player.weapons.type4.damage = 1
game.player.weapons.type4.missile_size = {4, 7}

function game.player.weapons.type4:shoot()
    game.player.missiles.number = game.player.missiles.number + 4
    game.player.missiles.list[game.player.missiles.number-3] = {"type4", game.player.x, game.player.y, -2, -6}
    game.player.missiles.list[game.player.missiles.number-2] = {"type4", game.player.x + 7, game.player.y, -1, -6}
    game.player.missiles.list[game.player.missiles.number-1] = {"type4", game.player.x + 13, game.player.y, 1, -6}
    game.player.missiles.list[game.player.missiles.number  ] = {"type4", game.player.x + 20, game.player.y, 2, -6}
end

-- Weapon 5 : Plasma cannon
game.player.weapons.type5 = class()
game.player.weapons.type5.image = image.new("\006\000\000\000\010\000\000\000\000\000\000\000\012\000\000\000\016\000\001\000\148\210\132\144B\136B\136\132\144\148\210\132\144\000\128\000\128\000\128\000\128\132\144\000\128\000\128\000\128\000\128\000\128\000\128\173\181!\132\000\128\000\128!\132\173\181\185\133\244\165\000\128\000\128\244\165\185\133\185\133\185\133B\136B\136\185\133wwww\185\133c\140c\140\185\133wwww\185\133\185\133\185\133\185\133wwwwww\185\133\185\133wwwwww\223\243\185\133\185\133wwww")
game.player.weapons.type5.damage = 2
game.player.weapons.type5.missile_size = {6, 10}

function game.player.weapons.type5:shoot()
    game.player.missiles.number = game.player.missiles.number + 2
    game.player.missiles.list[game.player.missiles.number-1] = {"type5", game.player.x - 1, game.player.y, 0, -7}
    game.player.missiles.list[game.player.missiles.number  ] = {"type5", game.player.x + 19, game.player.y, 0, -7}
end

-- Weapon 6 : Triple plasma cannon
game.player.weapons.type6 = class()
game.player.weapons.type6.image = game.player.weapons.type5.image
game.player.weapons.type6.damage = 2
game.player.weapons.type6.missile_size = {6, 10}

function game.player.weapons.type6:shoot()
    game.player.missiles.number = game.player.missiles.number + 3
    game.player.missiles.list[game.player.missiles.number-2] = {"type6", game.player.x - 1, game.player.y, -4, -6}
    game.player.missiles.list[game.player.missiles.number-1] = {"type6", game.player.x + 19, game.player.y, 4, -6}
    game.player.missiles.list[game.player.missiles.number  ] = {"type6", game.player.x + 9, game.player.y, 0, -7}
end

-- Weapon 7 : Quad plasma cannon
game.player.weapons.type7 = class()
game.player.weapons.type7.image = game.player.weapons.type5.image
game.player.weapons.type7.damage = 2
game.player.weapons.type7.missile_size = {6, 10}

function game.player.weapons.type7:shoot()
    game.player.missiles.number = game.player.missiles.number + 4
    game.player.missiles.list[game.player.missiles.number-3] = {"type7", game.player.x - 1, game.player.y, -2, -8}
    game.player.missiles.list[game.player.missiles.number-2] = {"type7", game.player.x + 6, game.player.y, -1, -8}
    game.player.missiles.list[game.player.missiles.number-1] = {"type7", game.player.x + 12, game.player.y, 1, -8}
    game.player.missiles.list[game.player.missiles.number  ] = {"type7", game.player.x + 19, game.player.y, 2, -8}
end

-- Weapon 8: Ultimate plasma cannon
game.player.weapons.type8 = class()
game.player.weapons.type8.image = game.player.weapons.type5.image
game.player.weapons.type8.damage = 3
game.player.weapons.type8.missile_size = {6, 10}

function game.player.weapons.type8:shoot()
    game.player.missiles.number = game.player.missiles.number + 5
    game.player.missiles.list[game.player.missiles.number-4] = {"type8", game.player.x - 1, game.player.y, -3, -7}
    game.player.missiles.list[game.player.missiles.number-3] = {"type8", game.player.x + 4, game.player.y, -1, -8}
    game.player.missiles.list[game.player.missiles.number-2] = {"type8", game.player.x + 9, game.player.y, 0, -9}
    game.player.missiles.list[game.player.missiles.number-1] = {"type8", game.player.x + 14, game.player.y, 1, -8}
    game.player.missiles.list[game.player.missiles.number  ] = {"type8", game.player.x + 19, game.player.y, 3, -7}
end


-- Weapon 9: Ultimate weapon
game.player.weapons.type9 = class()
game.player.weapons.type9.image = image.new("\008\000\000\000\008\000\000\000\000\000\000\000\016\000\000\000\016\000\001\000wwww\000\128\000\128\000\128\000\128wwwwww\000\128\128\248\128\248\128\248\128\248\000\128ww\000\128\128\248\128\248\195\255\195\255\128\248\128\248\000\128\000\128\128\248\195\255wwww\195\255\128\248\000\128\000\128\128\248\195\255wwww\195\255\128\248\000\128\000\128\128\248\128\248\195\255\195\255\128\248\128\248\000\128ww\000\128\128\248\128\248\128\248\128\248\000\128wwwwww\000\128\000\128\000\128\000\128wwww")
game.player.weapons.type9.damage = 5
game.player.weapons.type9.missile_size = {8, 8}

function game.player.weapons.type9:shoot()
    game.player.missiles.number = game.player.missiles.number + 5
    game.player.missiles.list[game.player.missiles.number-4] = {"type9", game.player.x - 2, game.player.y, -4, -9}
    game.player.missiles.list[game.player.missiles.number-3] = {"type9", game.player.x + 3, game.player.y, -2, -10}
    game.player.missiles.list[game.player.missiles.number-2] = {"type9", game.player.x + 8, game.player.y, 0, -10}
    game.player.missiles.list[game.player.missiles.number-1] = {"type9", game.player.x + 13, game.player.y, 2, -10}
    game.player.missiles.list[game.player.missiles.number  ] = {"type9", game.player.x + 18, game.player.y, 4, -9} 
end
    

------------------------------------------------------------------------------
-- Player's missiles management
------------------------------------------------------------------------------
game.player.missiles = class()
game.player.missiles.list = {}
game.player.missiles.number = 0
function game.player.missiles:update()
    local i = 1
    while i <= self.number do
        if self.list[i][3] < -10 then       --Out of screen
            self.list[i] = self.list[self.number]
            self.list[self.number] = nil
            self.number = self.number - 1
            i = i - 1
        else
            self.list[i][2] = self.list[i][2] + self.list[i][4]
            self.list[i][3] = self.list[i][3] + self.list[i][5]
        end
        i = i + 1
    end
end

function game.player.missiles:draw(gc)
    for i = 1, self.number do
        gc:drawImage(game.player.weapons[self.list[i][1]].image, self.list[i][2], self.list[i][3])
    end
end

game.enemies = class()
------------------------------------------------------------------------------
-- Enemies types
------------------------------------------------------------------------------
game.enemies.type1 = {}
game.enemies.type1.image = image.new("\014\000\000\000\008\000\000\000\000\000\000\000\028\000\000\000\016\000\001\000wwwwww\000\128ww7\255\145\254\145\2547\255ww\000\128wwwwwwwwww\000\128\000\128\000\1287\255\012\254\012\2547\255\000\128\000\128\000\128wwwwww\000\128\000\128\162\155\000\128\000\128\135\253\135\253\000\128\000\128\162\155\000\128\000\128ww\000\128\000\128\162\155\162\155\162\155\000\128\000\128\000\128\000\128\162\155\162\155\162\155\000\128\000\128ww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwww\000\128\000\128\000\128u\129u\129\000\128\000\128\000\128wwwwwwwwwwwwww\000\128\000\128u\129u\129\000\128\000\128wwwwwwwwwwwwwwwwwwww\000\128\000\128wwwwwwwwwwww")
if platform.isColorDisplay() then
    game.enemies.type1.missile = image.new("\004\000\000\000\007\000\000\000\000\000\000\000\008\000\000\000\016\000\001\000ww{\255{\255ww{\255J\253J\253{\255\214\254d\253d\253\214\254\173\253\194\254\194\254\173\253\008\253\163\255\163\255\008\253\231\252\163\255\163\255\231\252s\254\132\252\132\252s\254")
else        --Using dark images on old handhelds really improves the visibility of enemy missiles
    game.enemies.type1.missile = image.new("\004\000\000\000\007\000\000\000\000\000\000\000\008\000\000\000\016\000\001\000)\165\008\161\008\161)\165\008\161\132\144\132\144\008\161\231\156B\136B\136\231\156B\136\000\128\000\128B\136\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\198\152\000\128\000\128\198\152")
end
game.enemies.type1.missile_size = {4, 7}
game.enemies.type1.shoot_offset = {{5, 5}}
game.enemies.type1.missile_speed = {{0, 3}}
game.enemies.type1.damage = 1
game.enemies.type1.life = 1
game.enemies.type1.score = 50
game.enemies.type1.aimed_shoot = false
game.enemies.type1.bounce_on_borders = false
game.enemies.type1.size = {14, 8}
game.enemies.type1.shooting_period = 100
game.enemies.type1.drop_probability = 0.15    --drop value is contained between 0.5 and 2 * score

game.enemies.type2 = {}
game.enemies.type2.image = image.new("\020\000\000\000\012\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128wwwwwwwwwwwwww\000\128\000\128wwwwwwwwwwwwww\000\128\000\128\000\128\000\128\000\128ww\188\255y\255\188\255ww\000\128\000\128\000\128\000\128ww\188\255y\255\188\255ww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\179\254\169\253\179\254\000\128\000\128\162\155\162\155\000\128\000\128\179\254\169\253\179\254\000\128\000\128\000\128\000\128ww\000\128\000\128\000\128\000\128f\253\000\128\000\128\162\155\162\155\162\155\162\155\000\128\000\128f\253\000\128\000\128\000\128\000\128wwwwww\000\128\000\128\000\128\000\128\000\128\162\155\162\155\000\128\000\128\162\155\162\155\000\128\000\128\000\128\000\128\000\128wwwwwwww\000\128u\129\000\128\000\128\000\128\162\155\162\155\000\128\000\128\162\155\162\155\000\128\000\128\000\128u\129\000\128wwwwwwww\000\128u\129u\129\000\128\000\128\000\128\162\155\162\155\162\155\162\155\000\128\000\128\000\128u\129u\129\000\128wwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\162\155\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128wwww\000\128wwwwwwwwwwwwwwww")
if platform.isColorDisplay() then
    game.enemies.type2.missile = image.new("\004\000\000\000\004\000\000\000\000\000\000\000\008\000\000\000\016\000\001\000\148\254)\253)\253\214\254)\253\227\254\227\254)\253)\253\227\254\227\254)\253\214\254)\253)\253\214\254")
else
    game.enemies.type2.missile = image.new("\004\000\000\000\004\000\000\000\000\000\000\000\008\000\000\000\016\000\001\000\231\156\132\144\132\144\231\156\132\144B\136B\136\132\144\132\144B\136B\136\132\144\231\156\132\144\132\144\231\156")    
end
game.enemies.type2.missile_size = {4, 4}
game.enemies.type2.shoot_offset = {{8, 10}}
game.enemies.type2.missile_speed = 2
game.enemies.type2.damage = 1
game.enemies.type2.life = 2
game.enemies.type2.score = 75
game.enemies.type2.aimed_shoot = true
game.enemies.type2.bounce_on_borders = true
game.enemies.type2.size = {20, 12}
game.enemies.type2.shooting_period = 60
game.enemies.type2.drop_probability = 0.2

game.enemies.type3 = {}
game.enemies.type3.image = image.new("\020\000\000\000\012\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000wwwwwwwwwwww\189\2479\231s\206\016\194\016\194s\2069\231\189\247wwwwwwwwwwww{\239s\206s\206{\239ww\189\247s\206J\169\000\128\000\128\000\128\000\128J\169s\206\189\247ww{\239s\206s\206{\239s\206\000\128\000\128s\206y\255s\206\198\152\000\128 \137\161\137\161\137 \137\000\128\198\152s\206y\255s\206\000\128\000\128s\206s\206\000\128\000\128\179\254\169\253\179\254\000\128\193\141\001\151b\155b\155\001\151\193\141\000\128\179\254\169\253\179\254\000\128\000\128s\206{\239\000\128\000\128c\140f\253\000\128 \137\001\151e\151\235\146\235\146e\151\001\151 \137\000\128f\253c\140\000\128\000\128{\239wws\206\000\128\000\128\000\128\000\128\161\137b\155\235\146\246\137\246\137\235\146b\155\161\137\000\128\000\128\000\128\000\128s\206wwww{\239s\206\000\128\000\128\000\128\161\137b\155\235\146\246\137\246\137\235\146b\155\161\137\000\128\000\128\000\128s\206{\239wwwwwwwwwws\206\000\128 \137\001\151e\151\235\146\235\146e\151\001\151 \137\000\128s\206wwwwwwwwwwwwwwww9\231J\169\000\128\193\141\001\151b\155b\155\001\151\193\141\000\128J\1699\231wwwwwwwwwwwwwwww\189\247s\206\198\152\000\128 \137\161\137\161\137 \137\000\128\198\152s\206\189\247wwwwwwwwwwwwwwwwww\189\247s\206J\169\000\128\000\128\000\128\000\128J\169s\206\189\247wwwwwwwwwwwwwwwwwwwwww\189\2479\231s\206\016\194\016\194s\2069\231\189\247wwwwwwwwwwww")
game.enemies.type3.missile = game.enemies.type1.missile
game.enemies.type3.missile_size = {4, 7}
game.enemies.type3.shoot_offset = {{8, 11}}
game.enemies.type3.missile_speed = {{0, 3}}
game.enemies.type3.damage = 1
game.enemies.type3.life = 2
game.enemies.type3.score = 100
game.enemies.type3.aimed_shoot = false
game.enemies.type3.bounce_on_borders = false
game.enemies.type3.size = {20, 12}
game.enemies.type3.shooting_period = 90
game.enemies.type3.drop_probability = 0.17

game.enemies.type4 = {}
game.enemies.type4.image = image.new("\020\000\000\000\012\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000\000\128\181\214Z\235\222\251wwwwwwwwwwwwwwwwwwwwwwww\222\251Z\235\181\214\000\128!\132\000\128\000\128\214\218{\239\222\251wwwwwwwwwwwwwwww\222\251{\239\214\218\000\128\000\128!\132\000\128\000\128!\128\000\128\000\128\214\218{\239\222\251\179\254\179\254\179\254\179\254\222\251{\239\214\218\000\128\000\128!\128\000\128\000\128\000\128\000\128\128\128\128\128!\132\000\128\000\128\169\253\169\253\169\253\169\253\169\253\169\253\000\128\000\128!\132\128\128\128\128\000\128\000\128\000\128\128\128\162\155\162\155\128\128\000\128\001\132\000\128f\253f\253f\253f\253\000\128\001\132\000\128\128\128\162\155\162\155\128\128\000\128\000\128\160\132\162\155\162\155\128\132\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\128\132\162\155\162\155\160\132\000\128\000\128\160\132\162\155\162\155\128\132\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\128\132\162\155\162\155\160\132\000\128\000\128\128\128\162\155\162\155\128\128\000\128\000\128\000\128u\129u\129u\129u\129\000\128\000\128\000\128\128\128\162\155\162\155\128\128\000\128\000\128\000\128\128\128\128\128!\132\000\128\000\128u\129u\129u\129u\129u\129u\129\000\128\000\128!\132\128\128\128\128\000\128\000\128\000\128\000\128!\128\000\128\000\128s\206\000\128u\129u\129u\129u\129u\129u\129\000\128s\206\000\128\000\128!\128\000\128\000\128!\132\000\128\000\128\214\218{\239\189\247\024\223\000\128\000\128\000\128\000\128\000\128\000\128\024\223\189\247{\239\214\218\000\128\000\128!\132\000\128\181\214Z\235\222\251wwww\222\251Z\2359\2319\2319\2319\231Z\235\222\251wwww\222\251Z\235\181\214\000\128")
game.enemies.type4.missile = game.enemies.type1.missile
game.enemies.type4.missile_size = {4, 7}
game.enemies.type4.shoot_offset = {{8, 10}}
game.enemies.type4.missile_speed = {{0, 5}}
game.enemies.type4.damage = 1
game.enemies.type4.life = 3
game.enemies.type4.score = 125
game.enemies.type4.aimed_shoot = false
game.enemies.type4.bounce_on_borders = false
game.enemies.type4.size = {20, 12}
game.enemies.type4.shooting_period = 80
game.enemies.type4.drop_probability = 0.15

game.enemies.type5 = {}
game.enemies.type5.image = image.new("\020\000\000\000\012\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000wwwwwwwwwwww1\198\000\128\000\128\000\128\000\128\000\128\000\1281\198wwwwwwwwwwwwwwwwwwwwwwwwwwJ\169\000\128\000\128\000\128\000\128J\169wwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128u\129u\129\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128c\140u\129u\129u\129u\129c\140\000\128\000\128\000\128\000\128\000\128\000\128wwww\000\128\162\155\162\155\000\128\000\128c\140\000\128u\129u\129u\129u\129\000\128c\140\000\128\000\128\162\155\162\155\000\128wwww\000\128\162\155\000\128\000\128c\140R\202ww\000\128u\129u\129\000\128wwR\202c\140\000\128\000\128\162\155\000\128wwww\000\128\000\128\000\128\132\144\148\210wwwwww\000\128\000\128wwwwwwR\202B\136\000\128\000\128\000\128ww\000\128\000\128\000\128\231\156\247\222wwwwwwwwwwwwwwwwwwww\214\218\231\156\000\128\000\128\000\128\000\128\000\128\198\1529\231wwwwwwwwwwwwwwwwwwwwwwww9\231\198\152\000\128\000\128\000\128\231\156\156\243wwwwwwwwwwwwwwwwwwwwwwwwwwww\156\243\231\156\000\128")
game.enemies.type5.missile = game.enemies.type2.missile
game.enemies.type5.missile_size = {4, 4}
game.enemies.type5.shoot_offset = {{8, 7}}
game.enemies.type5.missile_speed = 3
game.enemies.type5.damage = 2
game.enemies.type5.life = 4
game.enemies.type5.score = 150
game.enemies.type5.aimed_shoot = true
game.enemies.type5.bounce_on_borders = true
game.enemies.type5.size = {20, 12}
game.enemies.type5.shooting_period = 50
game.enemies.type5.drop_probability = 0.23

game.enemies.type6 = {}
game.enemies.type6.image = image.new("\020\000\000\000\012\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000wwwwwwwwww\222\251\214\218\132\144\000\128\179\254\179\254\000\128\132\144\214\218\222\251wwwwwwwwwwwwwwwwww\222\251\247\222\231\156\000\128\000\128f\253f\253\000\128\000\128\231\156\247\222\222\251wwwwwwwwwwwwww\222\251\247\222\008\161!\132\000\128\000\128\000\128\000\128\000\128\000\128!\132\008\161\247\222\222\251wwwwwwwwww\222\251\247\222\008\161!\132\000\128\000\128\162\155\162\155\162\155\162\155\000\128\000\128!\132\008\161\247\222\222\251wwwwww\222\251\247\222\008\161!\132\000\128\000\128\162\155\162\155\162\155\162\155\162\155\162\155\000\128\000\128!\132\008\161\247\222\222\251ww\222\251\247\222\008\161!\132\000\128\000\128\000\128\162\155\000\128\000\128\000\128\000\128\162\155\000\128\000\128\000\128!\132\008\161\247\222\222\251\214\218\008\161!\132\000\128u\129\000\128\000\128\000\128\000\128\132\144\132\144\000\128\000\128\000\128\000\128u\129\000\128!\132\008\161\214\218\008\161!\132\000\128u\129u\129u\129\000\128!\132\008\161\214\218\214\218\008\161!\132\000\128u\129u\129u\129\000\128!\132\008\161\214\218\008\161!\132\000\128u\129\000\128!\132\008\161\247\222\222\251\222\251\247\222\008\161!\132\000\128u\129\000\128!\132\008\161\214\218\222\251\247\222\008\161!\132\000\128!\132\008\161\247\222\222\251wwww\222\251\247\222\008\161!\132\000\128!\132\008\161\247\222\222\251ww\222\251\247\222\008\161!\132\008\161\247\222\222\251wwwwwwww\222\251\247\222\008\161!\132\008\161\247\222\222\251wwwwww\222\251\214\218\008\161\214\218\222\251wwwwwwwwwwww\222\251\214\218\008\161\214\218\222\251wwww")
game.enemies.type6.missile = game.enemies.type2.missile
game.enemies.type6.missile_size = {4, 4}
game.enemies.type6.shoot_offset = {{8, 7}}
game.enemies.type6.missile_speed = 4
game.enemies.type6.damage = 1
game.enemies.type6.life = 5
game.enemies.type6.score = 175
game.enemies.type6.aimed_shoot = true
game.enemies.type6.bounce_on_borders = false
game.enemies.type6.size = {20, 12}
game.enemies.type6.shooting_period = 100
game.enemies.type6.drop_probability = 0.17

game.enemies.type7 = {}
game.enemies.type7.image = image.new("\020\000\000\000\012\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000wwwwwwwwy\255y\255\222\251Z\2359\2319\2319\2319\231Z\235\222\251y\255y\255wwwwwwwwww\222\251\000\128\179\254\169\253\169\253\179\254\000\128\165\148\165\148\165\148\165\148\000\128\179\254\169\253\169\253\179\254\000\128\222\251wwZ\235\000\128\000\128\000\128f\253f\253\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128f\253f\253\000\128\000\128\000\128Z\235\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128u\129u\129\000\128\000\128\000\128\000\128\000\128\000\128\000\128\162\155\000\128\000\128\162\155\162\155\000\128\000\128\000\128\000\128\000\128\000\128u\129u\129\000\128\000\128\000\128\000\128\000\128\000\128\162\155\162\155\000\128\000\128\162\155\162\155\162\155\162\155\000\128\000\128\000\128\000\128u\129u\129\000\128\000\128\000\128\000\128\162\155\162\155\162\155\162\155\000\128\000\128\162\155\162\155\162\155\162\155\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\162\155\162\155\162\155\162\155\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128u\129u\129u\129u\129\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128Z\235Z\235Z\235Z\235Z\235\214\218\008\161\000\128\000\128u\129u\129\000\128\000\128\008\161\214\218Z\235Z\235Z\235Z\235Z\235wwwwwwwwww\222\251\247\222\231\156\000\128\000\128\000\128\000\128\231\156\247\222\222\251wwwwwwwwwwwwwwwwwwwwww\222\251\214\218\132\144\000\128\000\128\132\144\214\218\222\251wwwwwwwwwwww")
game.enemies.type7.missile = game.enemies.type2.missile
game.enemies.type7.missile_size = {4, 4}
game.enemies.type7.shoot_offset = {{8, 11}}
game.enemies.type7.missile_speed = 4
game.enemies.type7.damage = 2
game.enemies.type7.life = 5
game.enemies.type7.score = 200
game.enemies.type7.aimed_shoot = true
game.enemies.type7.bounce_on_borders = false
game.enemies.type7.size = {20, 12}
game.enemies.type7.shooting_period = 90
game.enemies.type7.drop_probability = 0.15

game.enemies.type8 = {}
game.enemies.type8.image = image.new("\020\000\000\000\020\000\000\000\000\000\000\000\040\000\000\000\016\000\001\000wwwwww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwww\000\128\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\162\155\000\128wwwwww\000\128\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\162\155\000\128ww\000\128\162\155\000\128\000\128\000\128\000\128!\252!\252!\252!\252!\252!\252!\252!\252\000\128\000\128\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128!\252!\252!\252a\252\129\252\129\252\129\252\129\252a\252!\252!\252!\252\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128!\252A\252\161\252\129\253\193\253\193\253\193\253\193\253\129\253\161\252A\252!\252\000\128\000\128\162\155\000\128\000\128\162\155\000\128!\252A\252\161\252\161\253\001\254!\254a\254a\254!\254\001\254\161\253\161\252A\252!\252\000\128\162\155\000\128\000\128\162\155\000\128!\252a\252\129\253!\254a\254\130\254B\255B\255\130\254a\254!\254\129\253a\252!\252\000\128\162\155\000\128\000\128\162\155\000\128!\252\129\252\193\253a\254B\255\131\255\163\255\163\255\131\255B\255a\254\193\253\129\252!\252\000\128\162\155\000\128\000\128\162\155\000\128!\252\129\252\193\253a\254B\255\131\255\163\255\163\255\131\255B\255a\254\193\253\129\252!\252\000\128\162\155\000\128\000\128\162\155\000\128!\252a\252\129\253!\254a\254\130\254B\255B\255\130\254a\254!\254\129\253a\252!\252\000\128\162\155\000\128\000\128\162\155\000\128!\252A\252\161\252\161\253\001\254!\254a\254a\254!\254\001\254\161\253\161\252A\252!\252\000\128\162\155\000\128\000\128\162\155\000\128\000\128!\252A\252\161\252\129\253\193\253\193\253\193\253\193\253\129\253\161\252A\252!\252\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128!\252!\252A\252a\252\129\252\129\252\129\252\129\252a\252A\252!\252!\252\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128\000\128\000\128!\252!\252!\252!\252!\252!\252!\252!\252\000\128\000\128\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128\000\128\000\128\000\128u\129u\129\000\128\000\128u\129u\129\000\128\000\128\000\128\000\128\000\128\162\155\000\128ww\000\128\000\128\000\128\000\128u\129u\129u\129u\129\000\128\000\128u\129u\129u\129u\129\000\128\000\128\000\128\000\128wwwwww\000\128\000\128\000\128u\129u\129u\129u\129\000\128\000\128u\129u\129u\129u\129\000\128\000\128\000\128wwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128wwwwww")
game.enemies.type8.missile = game.enemies.type2.missile
game.enemies.type8.missile_size = {4, 4}
game.enemies.type8.shoot_offset = {{8, 20}}
game.enemies.type8.missile_speed = 5
game.enemies.type8.damage = 2
game.enemies.type8.life = 8
game.enemies.type8.score = 250
game.enemies.type8.aimed_shoot = true
game.enemies.type8.bounce_on_borders = false
game.enemies.type8.size = {20, 12}
game.enemies.type8.shooting_period = 80
game.enemies.type8.drop_probability = 0.15

game.enemies.boss1 = {}
game.enemies.boss1.image = image.new("\030\000\000\000\018\000\000\000\000\000\000\000\060\000\000\000\016\000\001\000ww\188\255y\255y\255y\255\188\255wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\188\255y\255y\255y\255\188\255wwww\179\254\169\253\169\253\169\253\179\254wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\179\254\169\253\169\253\169\253\179\254ww\000\128\000\128f\253f\253f\253\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128f\253f\253f\253\000\128\000\128\000\128\173\181\000\128\000\128\000\128\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\000\128\000\128\000\128\173\181\000\128\000\128\173\181\173\181\173\181\173\181\173\181\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\173\181\173\181\173\181\173\181\173\181\000\128\000\128\173\181\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128\173\181\000\128\000\128\173\181\000\128\162\155\000\128\173\181\173\181\173\181\173\181\000\128\000\128\000\128\162\155\162\155\000\128\000\128\162\155\162\155\000\128\000\128\000\128\173\181\173\181\173\181\173\181\000\128\162\155\000\128\173\181\000\128\000\128\173\181\000\128\162\155\000\128\173\181\000\128\173\181\000\128\162\155\162\155\162\155\162\155\162\155\000\128\000\128\162\155\162\155\162\155\162\155\162\155\000\128\173\181\000\128\173\181\000\128\162\155\000\128\173\181\000\128\000\128\173\181\000\128\162\155\000\128\173\181\000\128\173\181\173\181\000\128\000\128\162\155\162\155\162\155\000\128\000\128\162\155\162\155\162\155\000\128\000\128\173\181\173\181\000\128\173\181\000\128\162\155\000\128\173\181\000\128\000\128\173\181\000\128\162\155\000\128\173\181\000\128\173\181\173\181\173\181\173\181\000\128\000\128\162\155\000\128\000\128\162\155\000\128\000\128\173\181\173\181\173\181\173\181\000\128\173\181\000\128\162\155\000\128\173\181\000\128\000\128\173\181\000\128\000\128\000\128\173\181\173\181\000\128\000\128\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\000\128\000\128\173\181\173\181\000\128\000\128\000\128\173\181\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\173\181\000\128\000\128\000\128\173\181\000\128\000\128ww\000\128u\129u\129u\129\000\128wwww\000\128u\129u\129u\129\000\128ww\000\128\000\128\173\181\000\128\000\128\000\128\173\181\000\128\000\128\000\128\173\181\173\181\173\181\000\128\000\128wwwwww\000\128u\129\000\128wwwwwwww\000\128u\129\000\128wwwwww\000\128\000\128\173\181\173\181\173\181\000\128\000\128ww\000\128\000\128\173\181\000\128\000\128wwwwwwwwww\000\128wwwwwwwwwwww\000\128wwwwwwwwww\000\128\000\128\173\181\000\128\000\128wwwwww\000\128\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128wwwwwwwwww\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128wwwwww")
if platform.isColorDisplay() then
    game.enemies.boss1.missile = image.new("\006\000\000\000\006\000\000\000\000\000\000\000\012\000\000\000\016\000\001\000\180\254\005\253\161\252\161\252\005\253\180\254\005\253\162\253\034\254\034\254\162\253\005\253\161\252\034\254B\255B\255\034\254\161\252\161\252\034\254B\255B\255\034\254\161\252\005\253\162\253\034\254\034\254\162\253\005\253\180\254\005\253\161\252\161\252\005\253\180\254")
else
    game.enemies.boss1.missile = image.new("\006\000\000\000\006\000\000\000\000\000\000\000\012\000\000\000\016\000\001\000\198\152c\140B\136B\136c\140\198\152c\140B\136B\136B\136B\136c\140B\136B\136B\136B\136B\136B\136B\136B\136B\136B\136B\136B\136c\140B\136B\136B\136B\136c\140\198\152c\140B\136B\136c\140\198\152")
end
game.enemies.boss1.missile_size = {6, 6}
game.enemies.boss1.shoot_offset = {{7, 14}, {19, 14}}
game.enemies.boss1.missile_speed = 4
game.enemies.boss1.damage = 2
game.enemies.boss1.life = 15
game.enemies.boss1.score = 400
game.enemies.boss1.aimed_shoot = true
game.enemies.boss1.bounce_on_borders = true
game.enemies.boss1.size = {30, 18}
game.enemies.boss1.shooting_period = 40
game.enemies.boss1.drop_probability = 0.25

game.enemies.boss2 = {}
game.enemies.boss2.image = image.new("\030\000\000\000\018\000\000\000\000\000\000\000\060\000\000\000\016\000\001\000wwwwwwwwwwwwwwwwwwww\181\214\140\177\198\152B\136\000\128\000\128B\136\198\152\140\177\181\214wwwwwwwwwwwwwwwwwwwwwwww\188\255y\255y\255\188\255ww\156\243\206\185!\132\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128!\132\206\185\156\243ww\188\255y\255y\255\188\255wwwwwwww\179\254\169\253\169\253\179\2541\198\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\1281\198\179\254\169\253\169\253\179\254wwwwwwww\000\128f\253f\253\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128f\253f\253\000\128wwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128\173\181\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\173\181\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwww)\165\000\128\000\128\000\128\000\128\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\173\181\000\128\000\128\000\128\000\128)\165wwwwwwwwww\239\189\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\239\189wwwwwwZ\235\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128u\129\000\128\000\128\000\128\000\128u\129\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128Z\235wwwwk\173\000\128\162\155\162\155\000\128\000\128B\136\156\243ww\000\128u\129u\129u\129\000\128\000\128u\129u\129u\129\000\128ww\156\243B\136\000\128\000\128\162\155\162\155\000\128k\173ww\156\243\000\128\000\128\162\155\162\155\000\128!\132{\239wwww\000\128u\129u\129u\129\000\128\000\128u\129u\129u\129\000\128wwww{\239!\132\000\128\162\155\162\155\000\128\000\128\156\243R\202\000\128\162\155\162\155\162\155\000\128B\136\222\251wwwwww\000\128u\129u\129\000\128\000\128u\129u\129\000\128wwwwww\222\251B\136\000\128\162\155\162\155\162\155\000\128R\202J\169\000\128\162\155\162\155\000\128 \128\156\243wwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwww\156\243 \128\000\128\162\155\162\155\000\128J\169\132\144\000\128\162\155\162\155\000\128!\132\189\247wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\189\247!\132\000\128\162\155\162\155\000\128\132\144!\132\000\128\162\155\162\155\000\128!\132\189\247wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\189\247!\132\000\128\162\155\162\155\000\128!\132\000\128\000\128\162\155\162\155\000\128!\132{\239wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\156\243!\132\000\128\162\155\162\155\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128c\140wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwB\136\000\128\000\128\000\128\000\128\000\128\000\128J\169\000\128\000\128\000\128\000\128\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128\000\128\000\128\000\128\000\128J\169R\202\000\128wwwwww\000\128\000\128wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\000\128\000\128wwwwww\000\128R\202")
game.enemies.boss2.missile = game.enemies.boss1.missile
game.enemies.boss2.missile_size = {6, 6}
game.enemies.boss2.shoot_offset = {{2, 17}, {24, 17}}
game.enemies.boss2.missile_speed = 5
game.enemies.boss2.damage = 3
game.enemies.boss2.life = 30
game.enemies.boss2.score = 500
game.enemies.boss2.aimed_shoot = true
game.enemies.boss2.bounce_on_borders = true
game.enemies.boss2.size = {30, 18}
game.enemies.boss2.shooting_period = 25
game.enemies.boss2.drop_probability = 0.29

game.enemies.final_boss11r = {}
game.enemies.final_boss11r.image = image.new("\008\000\000\000\008\000\000\000\000\000\000\000\016\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
game.enemies.final_boss11r.missile = game.enemies.type2.missile
game.enemies.final_boss11r.missile_size = {4, 4}
game.enemies.final_boss11r.shoot_offset = {{2, 2}}
game.enemies.final_boss11r.missile_speed = {{1, 6}}
game.enemies.final_boss11r.damage = 1
game.enemies.final_boss11r.life = 15
game.enemies.final_boss11r.score = 150
game.enemies.final_boss11r.aimed_shoot = false
game.enemies.final_boss11r.bounce_on_borders = false
game.enemies.final_boss11r.size = {20, 12}
game.enemies.final_boss11r.shooting_period = 80
game.enemies.final_boss11r.drop_probability = 0.07

game.enemies.final_boss11l = {}
game.enemies.final_boss11l.image = game.enemies.final_boss11r.image
game.enemies.final_boss11l.missile = game.enemies.type2.missile
game.enemies.final_boss11l.missile_size = {4, 4}
game.enemies.final_boss11l.shoot_offset = {{2, 2}}
game.enemies.final_boss11l.missile_speed = {{-1, 6}}
game.enemies.final_boss11l.damage = 1
game.enemies.final_boss11l.life = 15
game.enemies.final_boss11l.score = 150
game.enemies.final_boss11l.aimed_shoot = false
game.enemies.final_boss11l.bounce_on_borders = false
game.enemies.final_boss11l.size = {20, 12}
game.enemies.final_boss11l.shooting_period = 80
game.enemies.final_boss11l.drop_probability = 0.07


game.enemies.final_boss21 = {}
game.enemies.final_boss21.image = image.new("\016\000\000\000\008\000\000\000\000\000\000\000\032\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
game.enemies.final_boss21.missile = game.enemies.type2.missile
game.enemies.final_boss21.missile_size = {4, 4}
game.enemies.final_boss21.shoot_offset = {{6, 2}}
game.enemies.final_boss21.missile_speed = 4
game.enemies.final_boss21.damage = 1
game.enemies.final_boss21.life = 30
game.enemies.final_boss21.score = 200
game.enemies.final_boss21.aimed_shoot = true
game.enemies.final_boss21.bounce_on_borders = false
game.enemies.final_boss21.size = {20, 12}
game.enemies.final_boss21.shooting_period = 100
game.enemies.final_boss21.drop_probability = 0.07

game.enemies.final_boss12 = {}
game.enemies.final_boss12.image = image.new("\008\000\000\000\016\000\000\000\000\000\000\000\016\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
game.enemies.final_boss12.missile = game.enemies.type2.missile
game.enemies.final_boss12.missile_size = {4, 4}
game.enemies.final_boss12.shoot_offset = {{2, 6}}
game.enemies.final_boss12.missile_speed = 4
game.enemies.final_boss12.damage = 1
game.enemies.final_boss12.life = 30
game.enemies.final_boss12.score = 200
game.enemies.final_boss12.aimed_shoot = true
game.enemies.final_boss12.bounce_on_borders = false
game.enemies.final_boss12.size = {20, 12}
game.enemies.final_boss12.shooting_period = 100
game.enemies.final_boss12.drop_probability = 0.07

game.enemies.final_boss22 = {}
game.enemies.final_boss22.image = image.new("\016\000\000\000\016\000\000\000\000\000\000\000\032\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
game.enemies.final_boss22.missile = game.enemies.boss1.missile
game.enemies.final_boss22.missile_size = {6, 6}
game.enemies.final_boss22.shoot_offset = {{5, 5}}
game.enemies.final_boss22.missile_speed = 4
game.enemies.final_boss22.damage = 3
game.enemies.final_boss22.life = 45
game.enemies.final_boss22.score = 400
game.enemies.final_boss22.aimed_shoot = true
game.enemies.final_boss22.bounce_on_borders = false
game.enemies.final_boss22.size = {20, 12}
game.enemies.final_boss22.shooting_period = 100
game.enemies.final_boss22.drop_probability = 0.07

game.enemies.final_boss32 = {}
game.enemies.final_boss32.image = image.new("\024\000\000\000\016\000\000\000\000\000\000\000\048\000\000\000\016\000\001\000\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128\000\128")
game.enemies.final_boss32.missile = game.enemies.boss1.missile
game.enemies.final_boss32.missile_size = {6, 6}
game.enemies.final_boss32.shoot_offset = {{4, 5}, {14, 5}}
game.enemies.final_boss32.missile_speed = 6
game.enemies.final_boss32.damage = 3
game.enemies.final_boss32.life = 45
game.enemies.final_boss32.score = 500
game.enemies.final_boss32.aimed_shoot = true
game.enemies.final_boss32.bounce_on_borders = false
game.enemies.final_boss32.size = {20, 12}
game.enemies.final_boss32.shooting_period = 100
game.enemies.final_boss32.drop_probability = 0.07


------------------------------------------------------------------------------
-- Enemies waves
------------------------------------------------------------------------------
game.enemies.waves = {
    {   --Standard wave 1 with enemy type type1
        {
            "type1",        --type
            --initial positions
            {{59, -12}, {59, -34}, {59, -56}, {59, -78}, {95, -12}, {95, -34}, {95, -56}, {95, -78}, {131, -12}, {131, -34}, {131, -56}, {131, -78}, {167, -12}, {167, -34}, {167, -56}, {167, -78}, {203, -12}, {203, -34}, {203, -56}, {203, -78}, {239, -12}, {239, -34}, {239, -56}, {239, -78}, },
            --speeds and times they are to be used + 0 used in update func
            {{2, 0, 46, 0}, {-2, 0, 46, 0}},
            --landing speeds, same format (used only once)
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
    },
    {   --Standard wave 2 with enemy type type1
        {
            "type1",
            {{-20, 12}, {-20, 56}, {-56, 12}, {-56, 56}, {-92, 12}, {-92, 56}, {-128, 12}, {-128, 56}, {-164, 12}, {-164, 56}, {-200, 12}, {-200, 56}, },
            {{2, 0, 38, 0}, {-2, 0, 38, 0}},
            {{4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
        {
            "type1",
            {{318, 34}, {318, 78}, {354, 34}, {354, 78}, {390, 34}, {390, 78}, {426, 34}, {426, 78}, {462, 34}, {462, 78}, {498, 34}, {498, 78}, },
            {{2, 0, 38, 0}, {-2, 0, 38, 0}},
            {{-4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
    },
    {   --Standard boss wave with enemy type type1 and boss boss1
        {
            "type1",
            {{-20, 80}, {-20, 102}, {-56, 80}, {-56, 102}, {-92, 80}, {-92, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type1",
            {{328, 80}, {328, 102}, {364, 80}, {364, 102}, {400, 80}, {400, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "boss1",            --type
            {{11, -18}},        --position(s)
            {{1, 0.5}},         --initial speed only because bounce_on_borders is true for this enemy
            {10, 308, 12, 55},  --rect in which it bounces
            {{0, 2, 16}}        --landing speeds and time they are used
        },
    },
    {   --Transitional wave 1 with enemy type type2
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 1}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 9}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 17}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 25}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 33}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 41}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 49}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 57}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 65}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 73}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 81}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 89}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type2",
            {{10 - 288 / 2, 66 - 108 / 2}},
            {{288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 97}, {288 / 50, 108 / 50, 40}}
        },
    },
    {   --Standard wave 1 with enemy type type3
        {
            "type3",
            {{59, -12}, {59, -34}, {59, -56}, {59, -78}, {95, -12}, {95, -34}, {95, -56}, {95, -78}, {131, -12}, {131, -34}, {131, -56}, {131, -78}, {167, -12}, {167, -34}, {167, -56}, {167, -78}, {203, -12}, {203, -34}, {203, -56}, {203, -78}, {239, -12}, {239, -34}, {239, -56}, {239, -78}, },
            {{2, 0, 46, 0}, {0, 2, 15, 0}, {-2, 0, 46, 0}, {0, -2, 15, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
    },
    {   --Standard wave 2 with enemy type type3
        {
            "type3",
            {{-20, 12}, {-20, 56}, {-56, 12}, {-56, 56}, {-92, 12}, {-92, 56}, {-128, 12}, {-128, 56}, {-164, 12}, {-164, 56}, {-200, 12}, {-200, 56}, },
            {{2, 0, 28, 0}, {1, 1, 20, 0}, {-2, 0, 28, 0}, {-1, -1, 20 ,0}},
            {{4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
        {
            "type3",
            {{318, 34}, {318, 78}, {354, 34}, {354, 78}, {390, 34}, {390, 78}, {426, 34}, {426, 78}, {462, 34}, {462, 78}, {498, 34}, {498, 78}, },
            {{2, 0, 28, 0}, {1, 1, 20, 0}, {-2, 0, 28, 0}, {-1, -1, 20 ,0}},
            {{-4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
    },
    {   --Standard boss wave with enemy type type3 and boss boss1
        {
            "type3",
            {{-20, 80}, {-20, 102}, {-56, 80}, {-56, 102}, {-92, 80}, {-92, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type3",
            {{328, 80}, {328, 102}, {364, 80}, {364, 102}, {400, 80}, {400, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "boss1",
            {{11, -18}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 2, 16}}
        },
        {
            "boss1",
            {{11, -118}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 40}, {0, 2, 66}}
        },
    },
    {   --Custom wave 2 with enemy type type2
        {
            "type2",
            {{-125, 10}, {-125, 130}, {-90, 30}, {-90, 110}, {-55, 50}, {-55, 90}, {-20, 70}},
            {{4, 0}, {4, 0}, {4, 0}, {4, 0}, {4, 0}, {4, 0}, {4, 0}, },
            {2, 316, 0, 104},
            {{4, 0, 50}},
        },
        {
            "type2",
            {{423, 10}, {423, 130}, {388, 30}, {388, 110}, {353, 50}, {353, 90}, {318, 70}},
            {{-4, 0}, {-4, 0}, {-4, 0}, {-4, 0}, {-4, 0}, {-4, 0}, {-4, 0}, },
            {2, 316, 0, 104},
            {{-4, 0, 50}},
        },
    },
    {   --Standard wave 1 with enemy type type4
        {
            "type4",
            {{59, -12}, {59, -34}, {59, -56}, {59, -78}, {95, -12}, {95, -34}, {95, -56}, {95, -78}, {131, -12}, {131, -34}, {131, -56}, {131, -78}, {167, -12}, {167, -34}, {167, -56}, {167, -78}, {203, -12}, {203, -34}, {203, -56}, {203, -78}, {239, -12}, {239, -34}, {239, -56}, {239, -78}, },
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
    },
    {   --Standard wave 2 with enemy type type4
        {
            "type4",
            {{-20, 12}, {-20, 56}, {-56, 12}, {-56, 56}, {-92, 12}, {-92, 56}, {-128, 12}, {-128, 56}, {-164, 12}, {-164, 56}, {-200, 12}, {-200, 56}, },
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type4",
            {{318, 34}, {318, 78}, {354, 34}, {354, 78}, {390, 34}, {390, 78}, {426, 34}, {426, 78}, {462, 34}, {462, 78}, {498, 34}, {498, 78}, },
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{-4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
    },
    {   --Standard boss wave with enemy type type4 and boss boss1 & boss2
        {
            "type4",
            {{-20, 80}, {-20, 102}, {-56, 80}, {-56, 102}, {-92, 80}, {-92, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type4",
            {{328, 80}, {328, 102}, {364, 80}, {364, 102}, {400, 80}, {400, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "boss1",
            {{11, -18}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 2, 16}}
        },
        {
            "boss2",
            {{11, -118}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 40}, {0, 2, 66}}
        },
        {
            "boss1",
            {{11, -218}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 80}, {0, 2, 116}}
        },
    },
    {
        {
            "type2", --Custom wave 3 with enemy type type2
            {{149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}},
            {{4, 0}, {3.69551813005, 1.53073372946}, {2.82842712475, 2.82842712475}, {1.53073372946, 3.69551813005}, {0, 4}, {-1.53073372946, 3.69551813005}, {-2.82842712475, 2.82842712475}, {-3.69551813005, 1.53073372946}, {-4, 0}, {-3.69551813005, -1.53073372946}, {-2.82842712475, -2.82842712475}, {-1.53073372946, -3.69551813005}, {0, -4}, {1.53073372946, -3.69551813005}, {2.82842712475, -2.82842712475}, {3.69551813005, -1.53073372946}},
            {10, 308, 12, 124},
            {{0, 4, 19}}
        },
    },
    {   --Standard wave 1 with enemy type type6 & type1
        {
            "type1",
            {{63, -12}, {63, -34}, {99, -12}, {99, -34}, {135, -12}, {135, -34}, {171, -12}, {171, -34}, {207, -12}, {207, -34}, {243, -12}, {243, -34}},
            {{2, 0, 46, 0}, {0, 2, 15, 0}, {-2, 0, 46, 0}, {0, -2, 15, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type6",
            {{59, -56}, {59, -78}, {95, -56}, {95, -78}, {131, -56}, {131, -78}, {167, -56}, {167, -78}, {203, -56}, {203, -78}, {239, -56}, {239, -78}},
            {{2, 0, 46, 0}, {0, 2, 15, 0}, {-2, 0, 46, 0}, {0, -2, 15, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
    },
    {   --Standard wave 2 with enemy type type6 & type3
        {
            "type6",
            {{-20, 12}, {-56, 12}, {-92, 12}, {-128, 12}, {-164, 12}, {-200, 12}},
            {{2, 0, 28, 0}, {1, 1, 20, 0}, {0, 2, 8, 0}, {-2, 0, 28, 0}, {-1, -1, 20 ,0},  {0, -2, 8, 0},},
            {{4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
        {
            "type3",
            {{-20, 56}, {-56, 56}, {-92, 56}, {-128, 56}, {-164, 56}, {-200, 56}},
            {{2, 0, 28, 0}, {1, 1, 20, 0}, {0, 2, 8, 0}, {-2, 0, 28, 0}, {-1, -1, 20 ,0},  {0, -2, 8, 0},},
            {{4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
        {
            "type6",
            {{318, 34}, {354, 34}, {390, 34}, {426, 34}, {462, 34}, {498, 34}},
            {{2, 0, 28, 0}, {1, 1, 20, 0}, {0, 2, 8, 0}, {-2, 0, 28, 0}, {-1, -1, 20 ,0},  {0, -2, 8, 0},},
            {{-4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
        {
            "type3",
            {{318, 78}, {354, 78}, {390, 78}, {426, 78}, {462, 78}, {498, 78},},
            {{2, 0, 28, 0}, {1, 1, 20, 0}, {0, 2, 8, 0}, {-2, 0, 28, 0}, {-1, -1, 20 ,0},  {0, -2, 8, 0},},
            {{-4, 0, 67, 0}, {-2, 0, 19, 0}}
        },
    },
    {   --Standard boss wave with enemy type type6 and boss boss1
        {
            "type6",
            {{-20, 80}, {-20, 102}, {-56, 80}, {-56, 102}, {-92, 80}, {-92, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type6",
            {{328, 80}, {328, 102}, {364, 80}, {364, 102}, {400, 80}, {400, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "boss1",
            {{11, -18}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 2, 16}}
        },
        {
            "boss1",
            {{11, -118}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 40}, {0, 2, 66}}
        },
        {
            "boss1",
            {{11, -218}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 80}, {0, 2, 116}}
        },
        {
            "boss1",
            {{11, -318}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 120}, {0, 2, 166}}
        },
    },
    {
        {
            "type5",     --Custom wave 4 with enemy type type5
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 1}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 11}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 21}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 31}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 41}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 51}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 61}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 71}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 81}, {288 / 50, 108 / 50, 40}}
        },
        {
            "type5",
            {{10 - 288 / 2, 66 - 108 / 2 - 10}, {10 - 288 / 2, 66 - 108 / 2 + 10}},
            {{288 / 50, 108 / 50}, {288 / 50, 108 / 50}},
            {10, 308, 12, 132},
            {{0, 0, 91}, {288 / 50, 108 / 50, 40}}
        },
    },
    {   --Standard wave 1 with enemy type type7
        {
            "type7",
            {{59, -56}, {59, -78}, {95, -56}, {95, -78}, {131, -56}, {131, -78}, {167, -56}, {167, -78}, {203, -56}, {203, -78}, {239, -56}, {239, -78}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type3",
            {{59, -34}, {95, -34}, {131, -34}, {167, -34}, {203, -34}, {239, -34}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type1",
            {{59, -12}, {95, -12}, {131, -12}, {167, -12}, {203, -12}, {239, -12}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
    },
    {   --Standard wave 2 with enemy type type7
        {
            "type7",
            {{-20, 12}, {-56, 12}, {-92, 12}, {-128, 12}, {-164, 12}, {-200, 12}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type6",
            {{-20, 56}, {-56, 56}, {-92, 56}, {-128, 56}, {-164, 56}, {-200, 56}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type7",
            {{318, 34}, {354, 34}, {390, 34}, {426, 34}, {462, 34}, {498, 34}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{-4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type3",
            {{318, 78}, {354, 78}, {390, 78}, {426, 78}, {462, 78}, {498, 78}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{-4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
    },
    {   --Standard boss wave with enemy type type7 and boss boss1 & boss2
        {
            "type7",
            {{-20, 80}, {-20, 102}, {-56, 80}, {-56, 102}, {-92, 80}, {-92, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type7",
            {{328, 80}, {328, 102}, {364, 80}, {364, 102}, {400, 80}, {400, 102}, },
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "boss1",
            {{11, -18}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 2, 16}}
        },
        {
            "boss2",
            {{11, -118}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 40}, {0, 2, 66}}
        },
        {
            "boss1",
            {{11, -218}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 80}, {0, 2, 116}}
        },
        {
            "boss2",
            {{11, -318}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 120}, {0, 2, 166}}
        },
    },
    {
        {
            "type5", --Custom wave 5 with enemy type type5
            {{149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}},
            {{4, 0}, {3.69551813005, 1.53073372946}, {2.82842712475, 2.82842712475}, {1.53073372946, 3.69551813005}, {0, 4}, {-1.53073372946, 3.69551813005}, {-2.82842712475, 2.82842712475}, {-3.69551813005, 1.53073372946}, {-4, 0}, {-3.69551813005, -1.53073372946}, {-2.82842712475, -2.82842712475}, {-1.53073372946, -3.69551813005}, {0, -4}, {1.53073372946, -3.69551813005}, {2.82842712475, -2.82842712475}, {3.69551813005, -1.53073372946}},
            {10, 308, 12, 124},
            {{0, 4, 19}}
        },
        {
            "type5",
            {{149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}, {149, -14}},
            {{4.0, 0.0}, {2.49395920743, 3.12732592987}, {-0.890083735825, 3.89971164873}, {-3.60387547161, 1.73553495647}, {-3.60387547161, -1.73553495647}, {-0.890083735825, -3.89971164873}, {2.49395920743, -3.12732592987}},
            {10, 308, 12, 124},
            {{0, 4, 19}, {0, 0, 8}}
        },
    },
    {   --Standard wave 1 with enemy type type8
        {
            "type8",
            {{59, -78}, {95, -78}, {131, -78}, {167, -78}, {203, -78}, {239, -78}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type6",
            {{59, -56}, {95, -56}, {131, -56}, {167, -56}, {203, -56}, {239, -56}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type3",
            {{59, -34}, {95, -34}, {131, -34}, {167, -34}, {203, -34}, {239, -34}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type1",
            {{59, -12}, {95, -12}, {131, -12}, {167, -12}, {203, -12}, {239, -12}},
            {{2, 0, 23, 0}, {0, 2, 12, 0}, {2, 0, 23, 0}, {0, -2, 12, 0}, {-2, 0, 23, 0}, {0, 2, 12, 0}, {-2, 0, 23, 0}, {0, -2, 12, 0}},
            {{0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
    },
    {   --Standard wave 2 with enemy type type8
        {
            "type8",
            {{-20, 12}, {-56, 12}, {-92, 12}, {-128, 12}, {-164, 12}, {-200, 12}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type7",
            {{-20, 56}, {-56, 56}, {-92, 56}, {-128, 56}, {-164, 56}, {-200, 56}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type8",
            {{318, 34}, {354, 34}, {390, 34}, {426, 34}, {462, 34}, {498, 34}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{-4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
        {
            "type4",
            {{318, 78}, {354, 78}, {390, 78}, {426, 78}, {462, 78}, {498, 78}},
            {{-1, 1, 16, 0}, {1, 1, 16, 0}, {2, 0, 22, 0}, {1, -1, 16, 0}, {-1, -1, 16, 0}, {-2, 0, 22, 0}},
            {{-4, 0, 67, 0}, {-2, 0, 11, 0}}
        },
    },
    {   --Standard boss wave with enemy type type8 and boss boss1 & boss2
        {
            "type8",
            {{-20, 80}, {-56, 80}, {-92, 80}},
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type4",
            {{-20, 102}, {-56, 102}, {-92, 102}},
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type8",
            {{328, 80}, {364, 80}, {400, 80}},
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type4",
            {{328, 102}, {364, 102}, {400, 102}},
            {{2, 0, 47, 0}, {-2, 0, 47, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "boss2",
            {{11, -18}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 2, 16}}
        },
        {
            "boss2",
            {{11, -118}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 40}, {0, 2, 66}}
        },
        {
            "boss2",
            {{11, -218}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 80}, {0, 2, 116}}
        },
        {
            "boss2",
            {{11, -318}},
            {{1, 0.5}},
            {10, 308, 12, 55},
            {{0, 0, 120}, {0, 2, 166}}
        },
    },
    {       --Custom wave 6
        {
            "type2",
            {{-20, 12}, {-30, 26}},
            {{2, 1}, {2, 1}},
            {10, 60, 30, 60},
            {{0, 0, 23}, {2, 1, 20}},
        },
        {
            "type2",
            {{318, 12}, {328, 26}},
            {{-2, 1}, {-2, 1}},
            {258, 308, 30, 60},
            {{0, 0, 23}, {-2, 1, 20}},
        },
        {
            "type5",
            {{149, -20}, {149, -20}, {149, -20}},
            {{-1, 2}, {0, 3}, {1, 2}},
            {159 - 50, 159 + 50, 15, 55},
            {{0, 0, 23}, {0, 3, 16}},
        },
        {
            "type8",
            {{59, -34}, {95, -34}, {131, -34}, {167, -34}, {203, -34}, {239, -34}},
            {{2, 0, 46, 0}, {-2, 0, 46, 0}},
            {{0, 0, 14, 0}, {0, 4, 25, 0}, {-2, 0, 23, 0}}
        },
        {
            "type4",
            {{323, 102}, {359, 102}, {395, 102}},
            {{2, 0, 46, 0}, {-2, 0, 46, 0}},
            {{-4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        {
            "type4",
            {{-25, 102}, {-61, 102}, {-97, 102}},
            {{2, 0, 46, 0}, {-2, 0, 46, 0}},
            {{4, 0, 39, 0}, {-2, 0, 23, 0}}
        },
        
    },
    {
        {       --Final boss, constituted of multiple enemies
            "final_boss11r",
            {{16 + 115, 0 - 64}, {0 + 115, 48 - 64}, {56 + 115, 8 - 64}},
            {{-2, 0, 80, 0}, {2, 0, 80, 0}},
            {{0, 2, 42, 0}, {2, 0, 40, 0}},
        },
        { 
            "final_boss21",
            {{16 + 115, 16 - 64}, {24 + 115, 56 - 64}, {48 + 115, 56 - 64}, {56 + 115, 16 - 64}},
            {{-2, 0, 80, 0}, {2, 0, 80, 0}},
            {{0, 2, 42, 0}, {2, 0, 40, 0}},
        },
        { 
            "final_boss11l",
            {{24 + 115, 8 - 64}, {80 + 115, 48 - 64}, {64 + 115, 0 - 64}},
            {{-2, 0, 80, 0}, {2, 0, 80, 0}},
            {{0, 2, 42, 0}, {2, 0, 40, 0}},
        },
        { 
            "final_boss12",
            {{0 + 115, 32 - 64}, {16 + 115, 40 - 64}, {24 + 115, 32 - 64}, {56 + 115, 32 - 64}, {64 + 115, 40 - 64}, {80 + 115, 32 - 64}},
            {{-2, 0, 80, 0}, {2, 0, 80, 0}},
            {{0, 2, 42, 0}, {2, 0, 40, 0}},
        },
        { 
            "final_boss22",
            {{8 + 115, 24 - 64}, {64 + 115, 24 - 64}},
            {{-2, 0, 80, 0}, {2, 0, 80, 0}},
            {{0, 2, 42, 0}, {2, 0, 40, 0}},
        },
        { 
            "final_boss32",
            {{32 + 115, 16 - 64}, {32 + 115, 32 - 64}},
            {{-2, 0, 80, 0}, {2, 0, 80, 0}},
            {{0, 2, 42, 0}, {2, 0, 40, 0}},
        },
    },
}

------------------------------------------------------------------------------
-- Enemies management
------------------------------------------------------------------------------
game.enemies.enemy_list = {}
game.enemies.missiles_list = {}
game.enemies.current_wave = 0
game.enemies.wave_cycle_count = 1
function game.enemies:update()
    local i, j, save
    if #self.enemy_list == 0 then   --new wave
        document.markChanged()  --kind of "checkpoint", to ensure the player won't lose too much progress
        math.randomseed(timer.getMilliSecCounter())
        self.current_wave = self.current_wave + 1
        if self.current_wave > #self.waves then
            self.current_wave = 1
            self.wave_cycle_count = self.wave_cycle_count * 2
        end
        for i = 1, #self.waves[self.current_wave] do
            for j = 1, #self.waves[self.current_wave][i][2] do
                if self[self.waves[self.current_wave][i][1]].bounce_on_borders then
                    self.enemy_list[#self.enemy_list + 1] = {self.waves[self.current_wave][i][1],
                                                             table_copy_1(self.waves[self.current_wave][i][2][j]),
                                                             table_copy_1(self.waves[self.current_wave][i][3][j]),
                                                             math.random(self[self.waves[self.current_wave][i][1]].shooting_period),
                                                             self[self.waves[self.current_wave][i][1]].life * self.wave_cycle_count,
                                                             table_copy_1(self.waves[self.current_wave][i][4]),
                                                             table_copy_2(self.waves[self.current_wave][i][5]),
                                                             false}
                else
                    self.enemy_list[#self.enemy_list + 1] = {self.waves[self.current_wave][i][1],
                                                             table_copy_1(self.waves[self.current_wave][i][2][j]),
                                                             table_copy_2(self.waves[self.current_wave][i][3]),
                                                             math.random(self[self.waves[self.current_wave][i][1]].shooting_period),
                                                             self[self.waves[self.current_wave][i][1]].life * self.wave_cycle_count,
                                                             table_copy_2(self.waves[self.current_wave][i][4]), 
                                                             false}
                end
            end
        end
        return
    end
    for i = 1, #self.enemy_list do  
        -- movement
        if self[self.enemy_list[i][1]].bounce_on_borders then
            if self.enemy_list[i][8] then       --has landed
                self.enemy_list[i][2][1] = self.enemy_list[i][2][1] + self.enemy_list[i][3][1]
                self.enemy_list[i][2][2] = self.enemy_list[i][2][2] + self.enemy_list[i][3][2]
                if self.enemy_list[i][2][1] <= self.enemy_list[i][6][1] or self.enemy_list[i][2][1] + self[self.enemy_list[i][1]].size[1] >= self.enemy_list[i][6][2] then
                    self.enemy_list[i][3][1] = - self.enemy_list[i][3][1]
                end
                if self.enemy_list[i][2][2] <= self.enemy_list[i][6][3] or self.enemy_list[i][2][2] + self[self.enemy_list[i][1]].size[2] >= self.enemy_list[i][6][4] then
                    self.enemy_list[i][3][2] = - self.enemy_list[i][3][2]
                end
            else
                self.enemy_list[i][2][1] = self.enemy_list[i][2][1] + self.enemy_list[i][7][1][1]
                self.enemy_list[i][2][2] = self.enemy_list[i][2][2] + self.enemy_list[i][7][1][2]
                self.enemy_list[i][7][1][3] = self.enemy_list[i][7][1][3] - 1
                if self.enemy_list[i][7][1][3] <= 0 then
                    for j = 1, #self.enemy_list[i][7] - 1 do
                        self.enemy_list[i][7][j] = self.enemy_list[i][7][j + 1]
                    end
                    self.enemy_list[i][7][#self.enemy_list[i][7]] = nil
                    if #self.enemy_list[i][7] == 0 then
                        self.enemy_list[i][8] = true
                    end
                end
            end
        else
            if self.enemy_list[i][7] then -- has landed
                self.enemy_list[i][2][1] = self.enemy_list[i][2][1] + self.enemy_list[i][3][1][1]
                self.enemy_list[i][2][2] = self.enemy_list[i][2][2] + self.enemy_list[i][3][1][2]
                self.enemy_list[i][3][1][4] = self.enemy_list[i][3][1][4] + 1
                if self.enemy_list[i][3][1][4] >= self.enemy_list[i][3][1][3] then
                    self.enemy_list[i][3][1][4] = 0
                    save = self.enemy_list[i][3][1]
                    for j = 1, #self.enemy_list[i][3] - 1 do
                        self.enemy_list[i][3][j] = self.enemy_list[i][3][j + 1]
                    end
                    self.enemy_list[i][3][#self.enemy_list[i][3]] = save
                end
            else
                self.enemy_list[i][2][1] = self.enemy_list[i][2][1] + self.enemy_list[i][6][1][1]
                self.enemy_list[i][2][2] = self.enemy_list[i][2][2] + self.enemy_list[i][6][1][2]
                self.enemy_list[i][6][1][4] = self.enemy_list[i][6][1][4] + 1
                if self.enemy_list[i][6][1][4] >= self.enemy_list[i][6][1][3] then
                    for j = 1, #self.enemy_list[i][6] - 1 do
                        self.enemy_list[i][6][j] = self.enemy_list[i][6][j + 1]
                    end
                    self.enemy_list[i][6][#self.enemy_list[i][6]] = nil
                    if #self.enemy_list[i][6] == 0 then
                        self.enemy_list[i][7] = true
                    end
                end
            end
        end
        -- collisions
        j = 1
        while j <= game.player.missiles.number do
            if rect_intersect(self.enemy_list[i][2], self[self.enemy_list[i][1]].size,
                             {game.player.missiles.list[j][2], game.player.missiles.list[j][3]},
                              game.player.weapons[game.player.missiles.list[j][1]].missile_size) then
                self.enemy_list[i][5] = self.enemy_list[i][5] - game.player.weapons[game.player.missiles.list[j][1]].damage
                game.player.missiles.list[j] = game.player.missiles.list[game.player.missiles.number]
                game.player.missiles.list[game.player.missiles.number] = nil
                game.player.missiles.number = game.player.missiles.number - 1
                j = j - 1
            end
            j = j + 1
        end
        -- shoot
        self.enemy_list[i][4] = self.enemy_list[i][4] - 1
        if self.enemy_list[i][4] == 0 then
            self.enemy_list[i][4] = self[self.enemy_list[i][1]].shooting_period
            for j = 1, #self[self.enemy_list[i][1]].shoot_offset do
                local position = add(self.enemy_list[i][2], self[self.enemy_list[i][1]].shoot_offset[j])
                local speed
                if self[self.enemy_list[i][1]].aimed_shoot then
                    local x, y
                    x = game.player.x + game.player.image_width / 2 - position[1]
                    y = game.player.y + game.player.image_height / 2 - position[2]
                    local div = math.sqrt(x * x + y * y) / self[self.enemy_list[i][1]].missile_speed
                    speed = {x / div, y / div}
                else
                    speed = self[self.enemy_list[i][1]].missile_speed[j]
                end
                self.missiles_list[#self.missiles_list + 1] = {self.enemy_list[i][1], position, speed}
            end
        end
    end
    -- delete dead enemies
    i = 1
    while i <= #self.enemy_list do
        if self.enemy_list[i][5] <= 0 then
            game.player.score = game.player.score + self[self.enemy_list[i][1]].score * self.wave_cycle_count
            -- random drops
            if math.random() <= self[self.enemy_list[i][1]].drop_probability then
                game.bonuses:add(math.random(0.5 * self[self.enemy_list[i][1]].score, 2 * self[self.enemy_list[i][1]].score), self.enemy_list[i][2])
            end
            self.enemy_list[i] = self.enemy_list[#self.enemy_list]
            self.enemy_list[#self.enemy_list] = nil
            i = i - 1
        end
        i = i + 1
    end
    i = 1
    while i <= #self.missiles_list do
        --movement
        self.missiles_list[i][2] = add(self.missiles_list[i][2], self.missiles_list[i][3])
        --collision
        if rect_intersect(self.missiles_list[i][2], self[self.missiles_list[i][1]].missile_size,
                            add({game.player.x, game.player.y}, game.player.collision_rect[1]),
                            game.player.collision_rect[2]) then
            game.player.life = game.player.life - self[self.missiles_list[i][1]].damage * self.wave_cycle_count
            self.missiles_list[i] = self.missiles_list[#self.missiles_list]
            self.missiles_list[#self.missiles_list] = nil
            i = i - 1
        end
        --remove missiles that are out of screen
        if i > 0 and self.missiles_list[i][2][2] > 220 then
            self.missiles_list[i] = self.missiles_list[#self.missiles_list]
            self.missiles_list[#self.missiles_list] = nil
            i = i - 1
        end
        i = i + 1
    end
end

function game.enemies:draw(gc)
    local i
    --missiles
    for i = 1, #self.missiles_list do
        gc:drawImage(self[self.missiles_list[i][1]].missile, self.missiles_list[i][2][1], self.missiles_list[i][2][2])
    end
    --enemies
    for i = 1, #self.enemy_list do
        gc:drawImage(self[self.enemy_list[i][1]].image, self.enemy_list[i][2][1], self.enemy_list[i][2][2])
    end
end

------------------------------------------------------------------------------
-- Bonuses (money)
------------------------------------------------------------------------------

game.bonuses = class()
game.bonuses.list = {}
game.bonuses.speed = 2 --same speed for all drops

function game.bonuses:add(value, position)
    local g = platform.gc()
    g:setFont("sansserif", "r", 8)
    local width = g:getStringWidth(value)
    local height = g:getStringHeight(value)
    self.list[#self.list + 1] = {value, table_copy_1(position), {width, height}}
end

function game.bonuses:update()
    local i = 1
    while i <= #self.list do
        self.list[i][2][2] = self.list[i][2][2] + self.speed
        if self.list[i][2][2] > 230 then   --out of screen
            self.list[i] = self.list[#self.list]
            self.list[#self.list] = nil
            i = i - 1
        end
        i = i + 1
    end
    i = 1
    while i <= #self.list do --collision with player
        if rect_intersect(self.list[i][2], self.list[i][3], 
                            add({game.player.x, game.player.y}, game.player.collision_rect[1]),
                            game.player.collision_rect[2]) then
            game.player.money = game.player.money + self.list[i][1]
            self.list[i] = self.list[#self.list]
            self.list[#self.list] = nil
            i = i - 1
        end
        i = i + 1
    end
end

function game.bonuses:draw(gc)
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "r", 8)
    for i = 1, #self.list do
        gc:drawString(self.list[i][1], self.list[i][2][1], self.list[i][2][2], "top")
    end
end

------------------------------------------------------------------------------
-- Shop
------------------------------------------------------------------------------

game.shop = {}
game.shop.nb_items = 10
game.shop.selected_item = 1
game.shop.buyable_items = {"Life", "Faster fire", "Dual cannon", "Triple cannon", 
                            "Quad cannon", "Plasma cannon", "Triple plasma cannon",
                             "Quad plasma cannon", "Ultimate plasma cannon", "Ultimate weapon"}
game.shop.prices = {60, 2000, 400, 700, 1000, 1500, 2000, 3000, 4500, 7000}

function game.shop.can_buy(n)
    if n == 1 then
        return game.player.life < game.player.max_life and game.player.money >= game.shop.prices[n]
    elseif n == 2 then
        return game.player.autoshoot_period > game.player.min_shoot_period and game.player.money >= game.shop.prices[n]
    elseif 3 <= n and n <= 10 then
        return not game.player.bought_weapons["type"..(n-1)] and game.player.money >= game.shop.prices[n]
    end
    return false
end

function game.shop.next_buyable()
    local i = game.shop.selected_item + 1
    while i <= game.shop.nb_items and not game.shop.can_buy(i) do
        i = i + 1
    end
    if i > game.shop.nb_items then
        return game.shop.selected_item
    else
        return i
    end
end

function game.shop.previous_buyable()
    local i = game.shop.selected_item - 1
    while i >= 1 and not game.shop.can_buy(i) do
        i = i - 1
    end
    if i < 1 then
        return game.shop.selected_item
    else
        return i
    end
end

function game.shop.buy(n)
    if game.shop.can_buy(n) then
        game.player.money = game.player.money - game.shop.prices[n]
        if n == 1 then
            game.player.life = game.player.life + 1
        elseif n == 2 then
            if game.player.autoshoot_period == 10 then
                game.player.autoshoot_period = 8
            else
                game.player.autoshoot_period = game.player.autoshoot_period - 1
            end
        elseif 3 <= n and n <= 10 then
            game.player.bought_weapons["type"..(n-1)] = true
            game.player.current_weapon = "type"..(n-1)
        end
        document.markChanged()
    end
end
    

------------------------------------------------------------------------------
-- Highscores
------------------------------------------------------------------------------

game.highscores = {}
game.highscores.maxnumber = 9
game.highscores.list = {}
game.highscores.names = {}
game.highscores.new = nil
function game.highscores.insert(score)
    local i = #game.highscores.list
    while i > 0 and score >= game.highscores.list[i] do
        game.highscores.list[i+1] = game.highscores.list[i]
        game.highscores.names[i+1] = game.highscores.names[i]
        i = i - 1
    end
    if i >= game.highscores.maxnumber then
        game.highscores.new = nil    --no new highscore
    else
        game.highscores.list[i+1] = score
        game.highscores.names[i+1] = ""
        game.highscores.list[game.highscores.maxnumber + 1] = nil
        game.highscores.names[game.highscores.maxnumber + 1] = nil
        game.highscores.new = i + 1
        document.markChanged()
    end
    game.events.enter = false
    game.events.del = false
end

------------------------------------------------------------------------------
-- UI
------------------------------------------------------------------------------

game.ui = {}
function game.ui.show(gc)
    gc:setFont("serif", "r", 8)
    gc:setColorRGB(0, 0, 0)
    gc:drawString("Life: "..game.player.life, 2, -2, "top")
    gc:drawString("Score: "..game.player.score, 316 - gc:getStringWidth("Score: "..game.player.score), -2, "top")
    gc:drawString("Money: "..game.player.money, 159 - gc:getStringWidth("Money: "..game.player.money) / 2, -2, "top")
end

function game.ui.pausemenu(gc)
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "b", 14)
    local s1
    if locale.name():sub(1,3) == "fr" then
        s1 = "Appuyez sur Esc. pour continuer"
    else
        s1 = "Press Esc. to resume"
    end
    gc:drawString("-- Game Paused --", 159 - gc:getStringWidth("-- Game Paused --") / 2, 90)
    gc:setFont("sansserif", "r", 12)
    gc:drawString(s1, 159 - gc:getStringWidth(s1) / 2, 140)
end

function game.ui.mainmenu(gc)
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "b", 14)
    gc:drawString("nSpired Phoenix", 159 - gc:getStringWidth("nSpired Phoenix") / 2, 50)
    gc:setFont("sansserif", "r", 12)
    local s1, s2, s3, s4
    if locale.name():sub(1,3) == "fr" then
        s1 = "1. Nouvelle partie"
        s2 = "2. Highscores"
        s3 = "3. Aide"
        s4 = "Appuyez sur un chiffre pour faire le choix correspondant"
    else
        s1 = "1. New game"
        s2 = "2. Highscores"
        s3 = "3. Help"
        s4 = "Press a number to make the corresponding choice"
    end
    gc:drawString(s1, 100, 90)
    gc:drawString(s2, 100, 110)
    gc:drawString(s3, 100, 130)
    gc:setFont("sansserif", "r", 8)
    gc:drawString(s4, 159 - gc:getStringWidth(s4) / 2, 210)
end

function game.ui.highscores(gc)
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "b", 14)
    gc:drawString("Highscores", 159 - gc:getStringWidth("Highscores") / 2, 38)
    gc:setFont("sansserif", "r", 12)
    local i
    for i = 1, #game.highscores.list do
        gc:drawString(i..". "..game.highscores.list[i], 30, 45 + 16 * i)
        gc:drawString(game.highscores.names[i], 120, 45 + 16 * i)
    end
    if game.highscores.new then
        gc:setFont("sansserif", "b", 12)
        gc:drawString(">>", 7, 45 + 16 * game.highscores.new)
        gc:setFont("sansserif", "r", 8)
        local s1
        if locale.name():sub(1,3) == "fr" then
            s1 = "Nouveau highscore! Entrez votre nom et appuyez sur Enter."
        else
            s1 = "New highscore! Please enter your name and press enter."
        end
        gc:drawString(s1, 159 - gc:getStringWidth(s1) / 2, 210)
    end
end

function game.ui.shop(gc)
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "b", 12)
    gc:drawString("Welcome to the shop!", 159 - gc:getStringWidth("Welcome to the shop!") / 2, 34)
    gc:setFont("sansserif", "r", 8)
    local s1
    if locale.name():sub(1,3) == "fr" then
        s1 = "Utilisez les fl"..string.uchar(232).."ches ou les chiffres pour choisir un "..string.uchar(233).."lement et appuyez sur Enter pour acheter"
    else
        s1 = "Use arrows or numbers to select an item, and enter to buy it."
    end
    gc:drawString(s1, 159 - gc:getStringWidth(s1) / 2, 210)
    gc:setFont("sansserif", "r", 9)
    local i
    for i = 1, game.shop.nb_items do
        if i == game.shop.selected_item then
            if game.shop.can_buy(i) then
                gc:setColorRGB(0, 0, 0)
                gc:fillRect(78, 25 + 15 * i, gc:getStringWidth((i-1)..". "..game.shop.buyable_items[i].." ("..game.shop.prices[i].."$)") + 4, 15)
                gc:setColorRGB(255, 255, 255)
                gc:drawString((i-1)..". "..game.shop.buyable_items[i].." ("..game.shop.prices[i].."$)", 80, 22 + 15 * i, "top")
            else
                gc:setColorRGB(100, 100, 100)
                gc:fillRect(78, 25 + 15 * i, gc:getStringWidth((i-1)..". "..game.shop.buyable_items[i].." ("..game.shop.prices[i].."$)") + 4, 15)
                gc:setColorRGB(255, 255, 255)
                gc:drawString((i-1)..". "..game.shop.buyable_items[i].." ("..game.shop.prices[i].."$)", 80, 22 + 15 * i, "top")
            end
        else
            if game.shop.can_buy(i) then
                gc:setColorRGB(0, 0, 0)
                gc:drawString((i-1)..". "..game.shop.buyable_items[i].." ("..game.shop.prices[i].."$)", 80, 22 + 15 * i, "top")
            else
                gc:setColorRGB(85, 85, 85)
                gc:drawString((i-1)..". "..game.shop.buyable_items[i].." ("..game.shop.prices[i].."$)", 80, 22 + 15 * i, "top")
            end
        end 
    end
end

game.ui.helpstrings = {
    fr = {"Bienvenue dans nSpired Phoenix",
          "",
          "Vous contr"..string.uchar(244).."lez le vaisseau situ"..string.uchar(233).." au bas de l'"..string.uchar(233).."cran avec",
          "les touches fl"..string.uchar(232).."ch"..string.uchar(233).."es. Le but est de tuer les ennemis situ"..string.uchar(233).."s",
          "en haut de l'écran, en "..string.uchar(233).."vitant leurs missiles!",
          "Collectez les bonus largu"..string.uchar(233).." par les ennemis que vous tuer pour am"..string.uchar(233).."liorer",
          "votre vaisseau dans le magasin, accessible avec la touche s.",
          "A tout moment vous pouvez mettre le jeu en pause en appuyant sur Esc,",
          " ou m"..string.uchar(234).."me "..string.uchar(233).."teindre l'"..string.uchar(233).."cran en appuyant sur tab.",
          "R"..string.uchar(233).."appuyez alors sur tab pour montrer "..string.uchar(224).." nouveau le jeu",
          "",
          "N'oubliez pas de sauvegarder r"..string.uchar(233).."guli"..string.uchar(232).."rement le classeur pour ne pas",
          " perdre votre progression ou vos highscores!",
          "",
          "Appuyez sur Esc pour revenir au menu."},
    fr_title = "Aide",
    en = {"Welcome to nSpired Phoenix",
          "",
          "You control the ship situated at the bottom of the screen",
          "with the arrow keys. Your mission is to kill the ennemies",
          "at the top of the screen, while avoiding their missiles!",
          "Collect the bonuses dropped by dead ennemies to improve your ship.",
          "You can buy upgrades in the shop, which you can reach with the 's' key.",
          "You can pause the game at any time with the Esc key, or even",
          "completely clear the screen with Tab.",
          "If you do so, press Tab again to show the game.",
          "",
          "Don't forget to save the document regularly not to lose your",
          "progress or your highscores!",
          "",
          "Press Esc to come back the main menu."},
    en_title = "Help"
    }

function game.ui.help(gc)
    local i
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "b", 14)
    if locale.name():sub(1, 3) == "fr" then
        gc:setFont("sansserif", "b", 12)
        gc:drawString(game.ui.helpstrings.fr_title, 159 - gc:getStringWidth(game.ui.helpstrings.fr_title) / 2, 30)
        gc:setFont("sansserif", "r", 8)
        for i = 1, #game.ui.helpstrings.fr do
            gc:drawString(game.ui.helpstrings.fr[i], 159 - gc:getStringWidth(game.ui.helpstrings.fr[i]) / 2, 40 + 11 * i)
        end
    else
        gc:setFont("sansserif", "b", 12)
        gc:drawString(game.ui.helpstrings.en_title, 159 - gc:getStringWidth(game.ui.helpstrings.en_title) / 2, 30)
        gc:setFont("sansserif", "r", 8)
        for i = 1, #game.ui.helpstrings.en do
            gc:drawString(game.ui.helpstrings.en[i], 159 - gc:getStringWidth(game.ui.helpstrings.en[i]) / 2, 40 + 11 * i)
        end
    end
end

function game.ui.memorycheck(gc)
    gc:setColorRGB(0, 0, 0)
    gc:setFont("sansserif", "r", 8)
    gc:drawString(#game.player.missiles.list, 5, 50)
    gc:drawString(#game.enemies.enemy_list, 5, 60)
    gc:drawString(#game.enemies.missiles_list, 5, 70)
    gc:drawString(#game.bonuses.list, 5, 80)
end


------------------------------------------------------------------------------
-- Game management
------------------------------------------------------------------------------

function game.update()
    if game.events.emergency then
        if game.state == game.emergency then
            game.state = game.formerstate
        else
            if game.state == game.playing then
                game.formerstate = game.paused
            else
                game.formerstate = game.state
            end
            game.state = game.emergency
        end
    end
    if game.state == game.playing then
        if game.events.char then
            if 49 <= game.events.char:byte() and game.events.char:byte() <= 57 then
                if game.player.bought_weapons["type"..game.events.char] then
                    game.player.current_weapon = "type"..game.events.char
                end
            elseif game.events.char == "s" or game.events.char == "S" then
                game.state = game.shop_state
                game.shop.selected_item = 1
            end
        end
        if game.events.pause then
            game.state = game.paused
        else
            game.player:update()
            game.player.missiles:update()
            game.enemies:update()
            game.bonuses:update()
        end
    elseif game.state == game.paused then
        if game.events.pause then
            game.events.pause = false
            game.state = game.playing
        end
    elseif game.state == game.emergency then
        -- do nothing
    elseif game.state == game.menu then
        if game.events.char == "1" then
            game.reset()
            document.markChanged()
            game.state = game.playing
        elseif game.events.char == "2" then
            game.state = game.scores
        elseif game.events.char == "3" then
            game.state = game.help
        end
    elseif game.state == game.scores then
        if game.highscores.new then
            if game.events.char then
                if string.len(game.highscores.names[game.highscores.new]) < 16 then
                    game.highscores.names[game.highscores.new] = game.highscores.names[game.highscores.new]..game.events.char
                end
            end
            if game.events.del and string.len(game.highscores.names[game.highscores.new]) > 0 then
                game.highscores.names[game.highscores.new] = string.sub(game.highscores.names[game.highscores.new], 1, -2)
            end
            if game.events.enter and game.highscores.names[game.highscores.new] ~= "" then
                game.highscores.new = nil
            end
        elseif game.events.enter or game.events.pause then
            game.state = game.menu
        end 
    elseif game.state == game.shop_state then
        if game.events.pause then
            game.state = game.playing   -- esc = return to game
        elseif game.events.char then
            if 48 <= game.events.char:byte() and game.events.char:byte() <= 57 then
                game.shop.selected_item = game.events.char:byte() - 47
            end
        elseif game.events.down then
            game.shop.selected_item = game.shop.next_buyable()
        elseif game.events.up then
            game.shop.selected_item = game.shop.previous_buyable()
        elseif game.events.enter then
            game.shop.buy(game.shop.selected_item)
        end
    elseif game.state == game.help then
        if game.events.pause or game.events.enter then
            game.state = game.menu
        end
    end
    game.reset_events()
    platform.window:invalidate()
end

function game.reset_events()
    game.events.up = false
    game.events.down = false
    game.events.left = false
    game.events.right = false
    game.events.pause = false
    game.events.emergency = false
    game.events.enter = false
    game.events.del = false
    game.events.char = nil
end

function game.draw(gc)
    if game.state == game.playing then
        game.ui.show(gc)
        game.player:draw(gc)
        game.enemies:draw(gc)
        game.bonuses:draw(gc)
        game.player.missiles:draw(gc)
        --game.ui.memorycheck(gc)
    elseif game.state == game.paused then
        game.ui.pausemenu(gc)
        game.ui.show(gc) --also show current score, money & life
    elseif game.state == game.emergency then
        --do nothing
    elseif game.state == game.menu then
        game.ui.mainmenu(gc)
    elseif game.state == game.scores then
        game.ui.highscores(gc)
    elseif game.state == game.shop_state then
        game.ui.show(gc)
        game.ui.shop(gc)
    elseif game.state == game.help then
        game.ui.help(gc)
    end
end

function game.reset()
    game.player.x = 147
    game.player.y = 190
    game.player.money = 200
    game.player.score = 0
    game.player.autoshoot = 0
    game.player.autoshoot_period = 10
    game.player.bought_weapons = {}
    game.player.bought_weapons["type1"] = true
    game.player.life = 30
    game.player.current_weapon = "type1"
    game.player.missiles.list = {}
    game.player.missiles.number = 0
    game.enemies.enemy_list = {}
    game.enemies.missiles_list = {}
    game.enemies.current_wave = 0
    game.bonuses.list = {}
    game.enemies.wave_cycle_count = 1
end

------------------------------------------------------------------------------
-- Events management
------------------------------------------------------------------------------

function on.paint(gc)
    game.draw(gc)
end

function on.timer()
    game.update()
end

function on.arrowRight()
    game.events.right = true
end

function on.escapeKey()
    game.events.pause = true
end

function on.enterKey()
    game.events.enter = true
end

function on.backspaceKey()
    game.events.del = true
end

function on.tabKey()
    game.events.emergency = true
end

function on.arrowLeft()
    game.events.left = true
end

function on.arrowUp()
    game.events.up = true
end

function on.arrowDown()
    game.events.down = true
end

function on.charIn(ch)
    game.events.char = ch
end

function on.deactivate()
    timer.stop()
    game.events.pause = true
    game.update()
end

function on.activate() 
    timer.start(0.05)
end

------------------------------------------------------------------------------
-- Save & restore
------------------------------------------------------------------------------
function on.save()
    local r = {
        game.player.x,
        game.player.y,
        game.player.money,
        game.player.score,
        game.player.autoshoot,
        game.player.autoshoot_period,
        game.player.bought_weapons,
        game.player.life,
        game.player.current_weapon,
        game.player.missiles.list,
        game.player.missiles.number,
        game.enemies.enemy_list,
        game.enemies.missiles_list,
        game.enemies.current_wave,
        game.bonuses.list,
        game.state,
        game.highscores.list,
        game.highscores.names,
        game.highscores.new,
        game.enemies.wave_cycle_count
    }
    return r
end

function on.restore(data)
    game.player.x = data[1]
    game.player.y = data[2]
    game.player.money = data[3]
    game.player.score = data[4]
    game.player.autoshoot = data[5]
    game.player.autoshoot_period = data[6]
    game.player.bought_weapons = data[7]
    game.player.life = data[8]
    game.player.current_weapon = data[9]
    game.player.missiles.list = data[10]
    game.player.missiles.number = data[11]
    game.enemies.enemy_list = data[12]
    game.enemies.missiles_list = data[13]
    game.enemies.current_wave = data[14]
    game.bonuses.list = data[15]
    if data[16] == game.playing then
        game.state = game.paused
    else
        game.state = data[16]
    end
    game.highscores.list = data[17]
    game.highscores.names = data[18]
    game.highscores.new = data[19]
    game.enemies.wave_cycle_count = data[20]
end

------------------------------------------------------------------------------
-- Utilities
------------------------------------------------------------------------------

function table_copy_1(t)
    local i, v
    local r = {}
    for i, v in ipairs(t) do
        r[i] = v
    end
    return r
end

function table_copy_2(t)
    local i, j
    local r = {}
    for i, _ in ipairs(t) do
        r[i] = {}
        for j, v in ipairs(t[i]) do
            r[i][j] = v
        end
    end
    return r
end

function add(v1, v2)
    return {v1[1] + v2[1], v1[2] + v2[2]}
end

function rect_intersect(p1, s1, p2, s2)
    if p1[2] + s1[2] < p2[2] then
        return false
    end
    if p1[1] + s1[1] < p2[1] then
        return false
    end
    if p1[1] > p2[1] + s2[1] then
        return false
    end
    if p1[2] > p2[2] + s2[2] then
        return false
    end
    return true
end