clua_version = 2.042
OUTPUT_VERBOSE = true
require "halo_classes"
require "cmt"
RUNS_ON = {
	["rend"]=true,
	["range"]=true,
	["spv3a05"]=true,
	["spv3a10"]=true,
	["spv3a30"]=true,
	["spv3a50"]=true,
	["spv3b30"]=true,
	["spv3b30_evolved"]=true,
	["spv3b40"]=true,
	["spv3c10"]=true,
	["spv3c20"]=true,
	["spv3c40"]=true,
	["spv3d20"]=true,
	["spv3d25"]=true,
	["spv3d30"]=true,
	["spv3d30_evolved"]=true,
	["spv3d40"]=true,
	["spv3_lumoria_a"]=true,
	["spv3_lumoria_b"]=true,
	["spv3_lumoria_c"]=true,
}
set_callback("map load", "OnMapLoad")
set_callback("command", "OnConsoleCommand")
local previous_ticks = 0
--local pp_processed = false
local cmt_rocket = nil
local PlayerLocal = Player(nil,0)
local hud = nil
local fplegs = nil
function OnMapLoad()
  User = os.getenv("USERPROFILE")
  --Halo:Print(User)
  file = io.open(User .. "\\Documents\\My Games\\Halo CE\\test_file.txt", "a+")
  file:write(os.date() .. " | " .. map .. "\n")
  file:flush()
  file:close()
  Halo:Print("Loaded Map: " .. map)
  
  pp_processed = false
  previous_ticks = 0
  if RUNS_ON[map] then
    cmt_rocket = Rocket(PlayerLocal)
    cmt_rocket:Enabled(true)
    --hud = HUD("cmt\\ui\\hudv5\\characters\\hudv5_spv3", "cmt\\ui\\hudv5\\characters\\spartan_hud", "cmt\\ui\\hudv5\\characters\\slim_hud")
    hud = HUD("cmt\\ui\\hudv5\\base_hud_globals_spv3", "cmt\\ui\\hudv5\\characters\\spartan_hud", "cmt\\ui\\hudv5\\characters\\slim_hud")
    set_callback("tick", "OnTick")
    set_callback("pretick", "PreTick")
	set_callback("preframe", "PreFrame")
  else
    Halo:Print("NULLING")
	hud = nil
	set_callback("tick", "nulled")
	set_callback("pretick", "nulled")
	set_callback("preframe", "nulled")
  end
end
function nulled()
end

function PreTick()
  Halo:pretick_update()
end
function OnTick()
  --JAZZ : LOOK HERE
  if (not pp_processed) then 
    --kirby_pp_decode_a() 
	pp_processed = true 
	--load_pp_config()
	-- PostProcessing:load_bin()
	--Halo:Print("Loaded PP")
	fplegs = FP_Legs(0, "weap")
  end
  
  --Halo:Print("Test")
  --Halo:Print(previous_ticks)
  Halo:ontick_update()
  fplegs:tick()
  cmt_rocket:update()
end
function PreFrame()
  if fplegs ~= nil then fplegs:frame() end
end
OnMapLoad()
function OnConsoleCommand(command)
	args = {}
	for word in string.gmatch(string.lower(command), "%S+") do
	  table.insert(args, word)
	end
	if (args[1] == "set_radar" and args[2] ~= nil and tonumber(args[2])) then
		hud:set_motion_tracker_distance(tonumber(args[2]))
		return false
	end
	if (args[1] == "set_radar_25") then
		hud:set_motion_tracker_25m()
		return false
	end
	if (args[1] == "set_radar_50") then
		hud:set_motion_tracker_50m()
		return false
	end
	if (args[1] == "activate_skull" and args[2] ~= nil and tonumber(args[2])) then
		if (not skull_list[tonumber(args[2])].is_on) then
		skull_list[tonumber(args[2])]:activate()
		Halo:Print("Activating skull " .. skull_list[tonumber(args[2])].name)
		else
		Halo:Print("Skull already active!")
		end
		Halo:Print("???")
		return false
	end
end

	-- local unit_hud = read_u32(get_tag("hudg","ui\\hud\\default")+0x14)
	-- Halo:Print("Radar Range: " .. read_f32(unit_hud + 0x2D0))
	-- write_f32(unit_hud + 0x2D0, 40)