function p.start_use(gun, primary)
    ga_set_i("var.inv_exec.cur_wep", gun)
    ga_set_b("var.inv_exec.primary", primary)
    ga_set_b("var.inv_exec.starting_shoot", true)
end

function p.end_use()
    ga_set_i("var.inv_exec.cur_wep", -1)
end

function p.update()
    if ga_get_b("var.inv_exec.starting_shoot") then
        p.try_shoot()
        ga_set_b("var.inv_exec.starting_shoot", false)
    end
end

function p.try_shoot()
    local cur_gun = ga_get_i("var.inv_exec.cur_wep")
    local primary = ga_get_b("var.inv_exec.primary")

    local viewer_chunk_id = ga_get_viewer_chunk_id()
    local viewer_level = ga_chunk_id_to_level(viewer_chunk_id)
    local viewer_pos = ga_get_viewer_offset()
    local look = ga_get_sys_v("game.player.camera.look")

    if ga_use_object_exists() then
        if( ga_win_mouse_down(false) ) then
            --The use object exists and the player is
            --holding down the right mouse.
            ga_command("use2")
            return
        else 
            ga_command("use")
            return
        end
    end

    if cur_gun == 1 then
        --Block gun.
        local ent_type = (primary and
            "ment_block_gun_add" or "ment_block_gun_remove")

        local extend_len = 0.1
        local pos = std.vec_add(viewer_pos, std.vec_scale(look, extend_len))

        ga_ment_start(viewer_level, pos, ent_type)

        local speed = 30.0
        local vel = std.vec_scale(look, speed)
        ga_ment_init_set_v("__vel", vel)

        ga_ment_end()
    end
    if cur_gun == 2 then
        --Ladder gun.
        local ent_type = "ment_ladder_gun_add" --Could add a secondary.

        local extend_len = 0.1
        local pos = std.vec_add(viewer_pos, std.vec_scale(look, extend_len))

        ga_ment_start(viewer_level, pos, ent_type)

        local speed = 10.0
        local vel = std.vec_scale(look, speed)
        ga_ment_init_set_v("__vel", vel)

        ga_ment_end()
    end
    if cur_gun == 3 then
        --Note gun.
        local ent_type = (primary and
            "ment_note_gun_add" or "ment_block_gun_remove")

        local extend_len = 0.1
        local pos = std.vec_add(viewer_pos, std.vec_scale(look, extend_len))

        ga_ment_start(viewer_level, pos, ent_type)

        local speed = 10.0
        local vel = std.vec_scale(look, speed)
        ga_ment_init_set_v("__vel", vel)

        ga_ment_end() 
    end
    if cur_gun == 4 then
        --Diamond gun.
        local ent_type = (primary and
            "ment_diamond_gun_add" or "ment_diamond_gun_remove")

        local extend_len = 0.1
        local pos = std.vec_add(viewer_pos, std.vec_scale(look, extend_len))

        ga_ment_start(viewer_level, pos, ent_type)

        local speed = 30.0
        local vel = std.vec_scale(look, speed)
        ga_ment_init_set_v("__vel", vel)

        ga_ment_end()
    end
    if cur_gun == 5 then
        if( primary ) then
            --Method #1:
            --Shrinking the player where they are now.
            --They will drop down over the next few seconds
            --and hit the ground.
            ga_shrink()
            --
            --Method #2:
            --We shrink the player now, then every cycle call
            --function to look for a place to place the player.
            -- ga_set_b("var.movement.shrinking", true)
            -- local eye_pos1 = ga_get_sys_v("game.player.camera.offset")
            -- local eye_pos2 = std.vec(eye_pos1.x, eye_pos1.y, eye_pos1.z - 1.0)
            -- ga_shrink2(eye_pos2)
        else
            local level_fine = ga_get_sys_i("game.player.level")
            local level_coarse = level_fine - 1
            local zero_vcp = std.bp(0,0,0)
            --The bp is in the corase level.
            local bp_bot = ga_vcp_to_bp(level_fine, zero_vcp)
            local bt_bot = ga_block_get(level_coarse, bp_bot)
            if( ga_bt_get_physically_solid(bt_bot) ) then return end
            --
            local bp_top = std.bp(bp_bot.x, bp_bot.y, bp_bot.z+1)
            local bt_top = ga_block_get(level_coarse, bp_top)
            if( ga_bt_get_physically_solid(bt_top) ) then return end
            --
            --Actually growing.
            ga_grow2( std.vec(7.5, 7.5, 21.5) )
        end
    end
end
