-- Minimap generator by MikuAuahDark
-- Try to load configuration file
local mcfg=loadfile("sys/lua/minimap.cfg")
-- If it cannot load configuration file
if not mcfg then
	-- Set all default configuration
	Password="password"
	MinimapPath="."
	FloorColor={50,50,50}
	WaterColor={0,100,150}
	WallColor={255,255,255}
	ObstacleColor={125,125,125}
	DeadlyColor={255,0,0}
-- The configuration file is loaded
else
	-- Parse configuration
	mcfg()
end

-- Declare a function that allow integers converted to strings
function Text_MiniMapGen(v,size,big)
	-- Create hexadecimal code
	local hex=string.format("%0"..(size*2).."X",v)
	-- Define returned value
	local r=""
	-- Check if it's a big-endian
	if big then
		-- Do loop
		for i=1,size*2,2 do
			-- Add string
			r=r..string.char(tonumber(hex:sub(i,i+1),16))
		end
	-- It's little endian
	else
		-- Do loop
		for i=size*2-1,1,-2 do
			-- Add string
			r=r..string.char(tonumber(hex:sub(i,i+1),16))
		end
	end
	-- Return value
	return r
end

-- Create hook
addhook("parse","MinimapGenerator")
-- Declare function
function MinimapGenerator(cmd)
	-- Check if user is entering a "Minimap" command and correct password
	if cmd:sub(1,7)=="Minimap" and (tonumber(cmd:sub(9)) or cmd:sub(9))==Password then
		-- Show message
		msg("\169000255000Generating minimap..")
		-- Open generated filename. Break when error occurs
		local temp=assert(io.open(MinimapPath.."/"..game("sv_map").."_minimap.bmp","wb"))
		-- Write bitmap header with little endian mode
		temp:write("BM\0\0\0\0\0\0\0\0\54\0\0\0\40\0\0\0"..Text_MiniMapGen(map("xsize")+1,4,false)..Text_MiniMapGen(map("ysize")+1,4,false).."\1\0\24\0\0\0\0\0\0\0\0\0\196\14\0\0\196\14\0\0\0\0\0\0\0\0\0\0")
		-- Do nested loop. Y First
		for y=map("ysize"),0,-1 do
			-- Then loop X
			for x=0,map("xsize") do
				-- Get tile property
				local property=tile(x,y,"property")
				-- Check if it's floor
				if property==0 or (property>4 and property<17 and property~=14) then
					-- Write color
					temp:write(string.char(FloorColor[3])..string.char(FloorColor[2])..string.char(FloorColor[1]))
				-- Check if it's wall
				elseif property==1 or property==3 then
					-- Write color
					temp:write(string.char(WallColor[3])..string.char(WallColor[2])..string.char(WallColor[1]))
				-- Check if it's obstacle
				elseif property==2 or property==4 then
					-- Write color
					temp:write(string.char(ObstacleColor[3])..string.char(ObstacleColor[2])..string.char(ObstacleColor[1]))
				-- Check if it's floor(water)
				elseif property==14 then
					-- Write color
					temp:write(string.char(WaterColor[3])..string.char(WaterColor[2])..string.char(WaterColor[1]))
				-- Check if it's deadly tile
				elseif property>=50 then
					-- Write color
					temp:write(string.char(DeadlyColor[3])..string.char(DeadlyColor[2])..string.char(DeadlyColor[1]))
				end
			end
			-- Add padding
			temp:write(string.rep("\0",(map("xsize")+1)%4))
		end
		-- Close file
		temp:close()
		-- Show success message
		msg("\169000255000Saved as "..game("sv_map").."_minimap.bmp")
		-- Skip CS2D parsing.
		return 2
	end
end