units = {}
items = {}
structs = {}
rooms = {}
foes = {}
map = {}
dirs = {{1,0},{0,-1},{-1,0},{0,1}}
names = {"Baba","Keke","Fofo","Jiji","Walter","Joanna","Elmo","Uhhh","Dunno","Name","Traitor"}
searchlimit = 3000

function init(dataid,targetid,mw,mh,ts,w,h)
	map.width = mw
	map.height = mh
	map.tilesize = ts
	
	map.astar = {}
	map.astar["marray"] = {}
	map.rooms = {}
	map.doors = {}
	
	screenw = w
	screenh = h
	
	createtasks()
	
	GDATA = dataid
	RTARGET = targetid
	
	menu = {"init"}
	openmenu("main")
end

function adddoor(doorid,x,y,dir)
	local drs = dirs[dir+1]
	local ox = drs[1]
	local oy = drs[2]
	
	local tileid1 = gettileid(x,y)
	local tileid2 = gettileid(x+ox,y+oy)
	
	if (map.doors[tileid1] == nil) then
		map.doors[tileid1] = {}
	end
	
	if (map.doors[tileid2] == nil) then
		map.doors[tileid2] = {}
	end
	
	map.doors[tileid1][dir] = doorid
	map.doors[tileid2][rot(dir)] = doorid
end

function setupmap()
	MF_setvalue(GDATA,MATERIAL,20)
	MF_setvalue(GDATA,FOOD,5)
	MF_setvalue(GDATA,ORE,3)
	MF_setvalue(GDATA,ENERGY,10000)
	
	setuproom("R_LARGE",30,28)
	setuproom("D_VER",30,27)
	setuproom("R_SMALL",30,27,"guard")
	setuproom("R_SMALL",31,27,"energy")
	
	setuproom("D_HOR",31,28)
	setuproom("R_HOR",32,28,"bed")
	
	setuproom("D_HOR",31,29)
	setuproom("R_HOR",32,29,"food")
	setuproom("D_VER",32,29)
	setuproom("R_SMALL",32,30,"disposal")
	
	setuproom("D_HOR",29,28)
	setuproom("R_HOR",28,28,"gear")
	setuproom("D_VER",28,28)
	setuproom("R_SMALL",28,29,"airlock")
	setuproom("D_VER",28,29)
	
	setuproom("D_HOR",29,29)
	setuproom("R_VER",29,29,"garden")
	
	setuproom("D_VER",30,29)
	setuproom("R_LARGE",30,30,"engine")
	setuproom("D_VER",31,31)
	setuproom("R_HOR",30,32,"generator")
	
	setuproom("R_LARGE",28,31,"rotor")
	setuproom("R_LARGE",32,31,"rotor")
	
	create_unit(30,28)
	create_unit(31,28)
	create_unit(30,29)
	create_unit(31,29)
end

function createtasks()
	local i_ = 0
	for i,v in pairs(tasklist) do
		if (v.unlisted == nil) or (v.unlisted == false) then
			local unitid = MF_create("hud_task")
			MF_setpos(unitid,8 + 36,8 + (i_) * 32 + 12)
			MF_setlayer(unitid,1)
			
			MF_setstring(unitid,TYPE,i)
			MF_setstring(unitid,NAME,v.name)
			
			v.unit = unitid
			i_ = i_ + 1
		end
	end
end

function create_unit(x,y)
	local unitid = MF_create("unit")
	MF_setvalue(unitid,ONLINE,1)
	MF_setvalue(unitid,XPOS,x * map.tilesize + map.tilesize * 0.5)
	MF_setvalue(unitid,YPOS,y * map.tilesize + map.tilesize * 0.5)
	addunit(unitid)
end

function addunit(unitid)
	table.insert(units, {id = unitid})
	local unit = units[#units]
	local name = names[math.random(1,#names)]
	unit.wait = 0
	unit.work = 0
	unit.nexttask = ""
	unit.actualtask = ""
	unit.limit = 100
	unit.sleep = math.random(0,500)
	unit.food = math.random(0,500)
	unit.idle = true
	
	local hudid = MF_create("hud_unit")
	MF_setvalue(hudid,OWNER,unitid)
	MF_setlayer(hudid,1)
	
	MF_setstring(unitid,NAME,name)
	MF_setstring(unitid,TASK,"IDLE")
	
	MF_setstring(hudid,NAME,name)
	MF_setstring(hudid,TASK,"IDLE")
	MF_setvalue(hudid,ID,#units-1)
	
	MF_givecolour(unitid)
end

function additem(unitid)
	table.insert(items, {id = unitid})
	local item = items[#items]
end

function addstruct(unitid)
	table.insert(structs, {id = unitid})
	local struct = structs[#structs]
	struct.status = 0
end

function addfoe(unitid)
	table.insert(foes, {id = unitid})
	local foe = foes[#foes]
end

function addtopology(x,y,str)
	local data = map.astar["marray"]
	local tileid = gettileid(x,y)
	
	if (data[tileid] == nil) then
		data[tileid] = {}
		data[tileid][1] = 0
		data[tileid][2] = "1111"
	end
	
	for j=1,#str do
		local l = string.sub(str, j, j)
		
		if (string.sub(data[tileid][2], j, j) ~= "2") then
			addwall(x,y,j-1,tonumber(l))
		end
	end
end

function addroom(unitid,x_,y_,w,h,str)
	local i = 0
	table.insert(rooms, {id = unitid})
	local room = rooms[#rooms]
	room.w = w
	room.h = h
	room.x = x_
	room.y = y_
	local mdata = map.astar["marray"]
	
	for data in string.gmatch(str, "%S+") do
		local x = x_ + i % w
		local y = y_ + math.floor(i / w)
		local tileid = gettileid(x,y)
		local existing = mdata[tileid] or {}
		existing = existing[2] or "1111"
		
		for j=1,#data do
			local l = string.sub(data, j, j)
			
			if (string.sub(existing, j, j) ~= "2") then
				addwall(x,y,j-1,tonumber(l))
			end
		end
		
		map.rooms[tileid] = unitid
		
		i = i + 1
	end
end

function addwall(x,y,dir,value)
	local drs = dirs[dir+1]
	local ox = drs[1]
	local oy = drs[2]
	
	local tileid1 = gettileid(x,y)
	local tileid2 = gettileid(x+ox,y+oy)
	
	local data = map.astar["marray"]
	
	if (data[tileid1] == nil) then
		data[tileid1] = {}
		data[tileid1][1] = 0
		data[tileid1][2] = "1111"
	end
	
	local tile = data[tileid1][2] or "1111"
	if (dir == 0) then
		tile = tostring(value) .. string.sub(tile, 2)
	elseif (dir == 3) then
		tile = string.sub(tile, 1, 3) .. tostring(value)
	else
		tile = string.sub(tile, 1, dir) .. tostring(value) .. string.sub(tile, dir + 2)
	end
	
	data[tileid1][2] = tile
	
	if (data[tileid2] == nil) then
		data[tileid2] = {}
		data[tileid2][1] = 0
		data[tileid2][2] = "1111"
	end
	
	local rot = {2,3,0,1}
	dir = rot[dir+1]
	
	tile = data[tileid2][2] or "1111"
	if (dir == 0) then
		tile = tostring(value) .. string.sub(tile, 2)
	elseif (dir == 3) then
		tile = string.sub(tile, 1, 3) .. tostring(value)
	else
		tile = string.sub(tile, 1, dir) .. tostring(value) .. string.sub(tile, dir + 2)
	end
	
	data[tileid2][2] = tile
end

function debug_gettile(x,y)
	local result = "nil"
	
	if inbounds(x,y) then
		local data = map.astar["marray"]
		local tileid = gettileid(x,y)
		
		if (data[tileid] ~= nil) then
			result = data[tileid][2] or "nil"
		end
	end
	
	return result
end