-- Minimap generator by MikuAuahDark
local mcfg=loadfile("sys/lua/minimap.cfg")
if not mcfg then
	Password="password"
	MinimapPath="."
	FloorColor={50,50,50}
	WaterColor={0,100,150}
	WallColor={255,255,255}
	ObstacleColor={125,125,125}
	DeadlyColor={255,0,0}
else
	mcfg()
end

function Text_MiniMapGen(v,size,big)
	local hex=string.format("%0"..(size*2).."X",v)
	local r=""
	if big then
		for i=1,size*2,2 do
			r=r..string.char(tonumber(hex:sub(i,i+1),16))
		end
	else
		for i=size*2-1,1,-2 do
			r=r..string.char(tonumber(hex:sub(i,i+1),16))
		end
	end
	return r
end

addhook("parse","MinimapGenerator")
function MinimapGenerator(cmd)
	if cmd:sub(1,7)=="Minimap" and (tonumber(cmd:sub(9)) or cmd:sub(9))==Password then
		msg("\169000255000Generating minimap..")
		local temp=assert(io.open(MinimapPath.."/"..game("sv_map").."_minimap.bmp","wb"))
		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")
		for y=map("ysize"),0,-1 do
			for x=0,map("xsize") do
				local property=tile(x,y,"property")
				if property==0 or (property>4 and property<17 and property~=14) then
					temp:write(string.char(FloorColor[3])..string.char(FloorColor[2])..string.char(FloorColor[1]))
				elseif property==1 or property==3 then
					temp:write(string.char(WallColor[3])..string.char(WallColor[2])..string.char(WallColor[1]))
				elseif property==2 or property==4 then
					temp:write(string.char(ObstacleColor[3])..string.char(ObstacleColor[2])..string.char(ObstacleColor[1]))
				elseif property==14 then
					temp:write(string.char(WaterColor[3])..string.char(WaterColor[2])..string.char(WaterColor[1]))
				elseif property>=50 then
					temp:write(string.char(DeadlyColor[3])..string.char(DeadlyColor[2])..string.char(DeadlyColor[1]))
				end
			end
			temp:write(string.rep("\0",(map("xsize")+1)%4))
		end
		temp:close()
		msg("\169000255000Saved as "..game("sv_map").."_minimap.bmp")
		return 2
	end
end