-- Sky switcher, version 1.1
-- idea: Ethan2
-- Modded: Ethan2
-- module written by: juce

local inifile = ".\\modules\\ms_settings.ini"
local skyroot = ".\\Data\\MjTs-140914\\ms_sky"
local sky = 0
local sky_map = {
[1] = "st099",
[2] = "st100",
[3] = "st101",
[4] = "st102",
[5] = "st103",
[6] = "st104",
[7] = "st105",
[8] = "st106",
[9] = "st107",
[10] = "st108",
[11] = "st109",
[12] = "st110",
[13] = "st111",
[14] = "st112",
[15] = "st113",
[16] = "st114",
[17] = "st115",
[18] = "st116",
[19] = "st117",
[20] = "st118",
[21] = "st119",
[22] = "st120",
[23] = "st121",
[24] = "st122",
[25] = "st123",
[26] = "st124",
[27] = "st125",
[28] = "st126",
[29] = "st127",
[30] = "st128",
[31] = "st129",
[32] = "st130",
[33] = "st131",
[34] = "st132",
[35] = "st133",
[36] = "st134",
[37] = "st135",
[38] = "st136",
[39] = "st137",
[40] = "st138",
[41] = "st139",
[42] = "st140",
[43] = "st141",
[44] = "st142",
[45] = "st143",
[46] = "st144",

}

function read_ini(ctx)
    if inifile:sub(1,1) == "." then
        inifile = ctx.sider_dir .. inifile
    end
    local t = {}
    for line in io.lines(inifile) do
        local name, value = string.match(line, "^(dynamic.sky)%s*=%s*([-%d.]+)")
        if name and value then
            t[name] = tonumber(value) or value
			dynamic_sky = t[name]
        end
    end
    return t
end	

function make_key(ctx, filename)
    ini = read_ini(ctx)
    if sky > 0 and dynamic_sky == 1 then
	   return string.gsub(filename, "st%d%d%d", sky_map[sky])
    end
end

function get_filepath(ctx, filename, key)
    if key then
       return skyroot .. key
	end
end

function set_conditions(ctx, options)
    sky = math.random(1,46)
	log("dynamic.sky.enable: " .. dynamic_sky)
	if dynamic_sky == 1 then
       log("sky selected: " .. sky)
	end
end


function init(ctx)
    if skyroot:sub(1,1) == "." then
       skyroot = ctx.sider_dir .. skyroot
    end
    math.randomseed(os.time()) -- seed random generator
    ctx.register("set_conditions", set_conditions)
    ctx.register("set_conditions_for_replay", set_conditions)
    ctx.register("livecpk_make_key", make_key)
    ctx.register("livecpk_get_filepath", get_filepath)
end

return { init = init }