roomlist =
{
	{
		name = "small",
		cost = 10,
		id = "R_SMALL",
		size = {1,1},
		dir = 0,
		code = "0000",
		work_needed = 200,
		nearby = 1,
		upgrades =
			{
				airlock =
				{
					name = "Airlock",
					subtitle = "Unfinished",
				},
				energy =
				{
					name = "Power source",
					subtitle = "Small source of energy (20 material, 1 ore, 200 energy)",
					cost = {20,0,1,200},
					setup =
						function(roomid,data)
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							local structid = create_struct("ENERGY",x,y,roomid)
							local eid = MF_create("electricity_small")
							
							MF_setvalue(eid,OWNER,structid)
						end,
				},
				--[[
				light =
				{
					name = "Light source",
				},
				]]--
				guard =
				{
					name = "Guarding spot",
					subtitle = "Used for defending against foes (50 material, 200 energy)",
					cost = {50,0,0,200},
					setup =
						function(roomid,data)
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							local structid = create_struct("GUARD",x,y,roomid)
						end,
				},
				disposal =
				{
					name = "Disposal",
					subtitle = "Disposing of waste creates materials (20 material, 5 food, 50 energy)",
					cost = {20,5,0,50},
					setup =
						function(roomid,data)
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							create_struct("DISPOSAL",x,y,roomid)
						end,
				},
			},
	},
	{
		name = "horizontal",
		id = "R_HOR",
		cost = 20,
		size = {2,1},
		dir = 1,
		code = "1000 0010",
		work_needed = 350,
		nearby = 1,
		upgrades =
			{
				garden =
				{
					name = "Hydroponics",
					subtitle = "Creates food to harvest (20 material, 10 food)",
					cost = {20,10,0,0},
					setup =
						function(roomid,data)
							local tiles = data.size[1] * data.size[2]
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							for i=1,tiles do
								local ox = (i-1) % data.size[1]
								local oy = math.floor((i-1) / data.size[1])
								
								create_struct("GARDEN",x + ox,y + oy,roomid)
							end
						end,
				},
				gear =
				{
					name = "Diving suits",
					subtitle = "These are needed to go outside (20 material, 1 ore)",
					cost = {30,0,1,0},
					setup =
						function(roomid,data)
							local tiles = data.size[1] * data.size[2]
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							for i=1,tiles do
								local ox = (i-1) % data.size[1]
								local oy = math.floor((i-1) / data.size[1])
								
								create_item("GEAR",x + ox,y + oy)
								create_struct("STAND",x + ox,y + oy,roomid)
							end
						end,
				},
				bed =
				{
					name = "Resting area",
					subtitle = "For sleeping (20 material, 50 energy)",
					cost = {20,0,0,50},
					setup =
						function(roomid,data)
							local tiles = data.size[1] * data.size[2]
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							for i=1,tiles do
								local ox = (i-1) % data.size[1]
								local oy = math.floor((i-1) / data.size[1])
								
								create_struct("BED",x + ox,y + oy,roomid)
							end
						end,
				},
				food =
				{
					name = "Kitchen",
					subtitle = "For eating & food preparation (20 material, 5 food, 50 energy)",
					cost = {20,5,0,50},
					setup =
						function(roomid,data)
							local tiles = data.size[1] * data.size[2]
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							for i=1,tiles do
								local ox = (i-1) % data.size[1]
								local oy = math.floor((i-1) / data.size[1])
								
								create_struct("TABLE",x + ox,y + oy,roomid)
							end
						end,
				},
				generator =
				{
					name = "Generator",
					subtitle = "For harvesting energy (50 material, 1 ore, 300 energy)",
					cost = {50,0,1,300},
					setup =
						function(roomid,data)
							local tiles = data.size[1] * data.size[2]
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							for i=1,tiles do
								local ox = (i-1) % data.size[1]
								local oy = math.floor((i-1) / data.size[1])
								
								create_struct("GENERATOR",x + ox,y + oy,roomid)
							end
						end,
				},
			},
	},
	{
		name = "vertical",
		id = "R_VER",
		cost = 20,
		size = {1,2},
		dir = 2,
		code = "0001 0100",
		work_needed = 400,
		nearby = 1,
		upgrades =
			{
				garden =
				{
					name = "Hydroponics",
					subtitle = "Creates food to harvest (20 material, 10 food)",
					cost = {20,10,0,0},
					setup =
						function(roomid,data)
							local rdata = roomlist[2]["upgrades"]["garden"]
							rdata.setup(roomid,data)
						end,
				},
				gear =
				{
					name = "Diving suits",
					subtitle = "These are needed to go outside (20 material, 1 ore)",
					cost = {30,0,1,0},
					setup =
						function(roomid,data)
							local rdata = roomlist[2]["upgrades"]["gear"]
							rdata.setup(roomid,data)
						end,
				},
				bed =
				{
					name = "Resting area",
					subtitle = "For sleeping (20 material, 50 energy)",
					cost = {20,0,0,50},
					setup =
						function(roomid,data)
							local rdata = roomlist[2]["upgrades"]["bed"]
							rdata.setup(roomid,data)
						end,
				},
				food =
				{
					name = "Kitchen",
					subtitle = "For eating & food preparation (20 material, 5 food, 50 energy)",
					cost = {20,5,0,50},
					setup =
						function(roomid,data)
							local rdata = roomlist[2]["upgrades"]["food"]
							rdata.setup(roomid,data)
						end,
				},
				generator =
				{
					name = "Generator",
					subtitle = "For harvesting energy (50 material, 1 ore, 300 energy)",
					cost = {50,0,1,300},
					setup =
						function(roomid,data)
							local rdata = roomlist[2]["upgrades"]["generator"]
							rdata.setup(roomid,data)
						end,
				},
			},
	},
	{
		name = "large",
		id = "R_LARGE",
		cost = 30,
		size = {2,2},
		dir = 3,
		code = "1001 0011 1100 0110",
		work_needed = 500,
		nearby = 1,
		upgrades =
			{
				engine =
				{
					name = "Engine",
					subtitle = "Bring harvested energy here for use (100 material, 2 ore, 1000 energy)",
					cost = {100,0,2,1000},
					setup =
						function(roomid,data)
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							local structid = create_struct("ENGINE",x,y,roomid)
							local eid = MF_create("electricity")
							
							MF_setvalue(eid,OWNER,structid)
						end,
				},
				rotor =
				{
					name = "Rotor",
					subtitle = "These move the vessel deeper (60 material, 1 ore, 200 energy)",
					cost = {60,0,1,200},
					setup =
						function(roomid,data)
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							local structid = create_struct("ROTOR",x,y,roomid)
						end,
				},
				large_garden =
				{
					name = "Large hydroponics",
					subtitle = "Large garden (50 material, 15 food, 1 ore, 800 energy)",
					cost = {50,15,1,800},
					setup =
						function(roomid,data)
							local tiles = data.size[1] * data.size[2]
							local x,y = MF_getvalue(roomid,XTILE),MF_getvalue(roomid,YTILE)
							
							for i=1,tiles do
								local ox = (i-1) % data.size[1]
								local oy = math.floor((i-1) / data.size[1])
								
								create_struct("GARDEN",x + ox,y + oy,roomid)
							end
						end,
				},
			},
	},
	{
		name = "door_hor",
		id = "D_HOR",
		size = {1,1},
		cost = 5,
		dir = 4,
		code = "",
		work_needed = 120,
		nearby = 2,
		special = true,
		object = "door_hor",
	},
	{
		name = "door_ver",
		id = "D_VER",
		size = {1,1},
		cost = 5,
		dir = 5,
		code = "",
		work_needed = 120,
		nearby = 2,
		special = true,
		object = "door_ver",
	},
	{
		name = "scaffold",
		id = "R_SCAFFOLD",
		cost = 10,
		size = {1,1},
		dir = 6,
		code = "1111",
		work_needed = 50,
		nearby = 1,
		special = true,
	},
}

function designateroom(unitid,style,x,y)
	local data = roomlist[style]
	
	MF_setvalue(unitid,XPOS,x * map.tilesize)
	MF_setvalue(unitid,YPOS,y * map.tilesize)
	MF_setvalue(unitid,ONLINE,1)
	
	MF_setvalue(unitid,ROOMW,data.size[1])
	MF_setvalue(unitid,ROOMH,data.size[2])
	MF_setvalue(unitid,D_STYLE,style - 1)
	MF_setvalue(unitid,NEARBY,data.nearby)
	
	addstruct(unitid)
end

function buildroom(structid)
	local style = MF_getvalue(structid,D_STYLE) + 1
	local data = roomlist[style]
	local obj = data.object or "room"
	local s = data.special or false
	
	local roomid = MF_create(obj)
	local x,y = MF_getvalue(structid,XTILE),MF_getvalue(structid,YTILE)
	local w = data.size[1]
	local h = data.size[2]
	local str = data.code
	
	MF_setvalue(roomid,ONLINE,1)
	MF_setvalue(roomid,XPOS,x * map.tilesize)
	MF_setvalue(roomid,YPOS,y * map.tilesize)
	MF_setstring(roomid,TYPE,data.id)
	MF_setstring(roomid,NAME,data.name)
	
	if (s == false) then
		MF_setdir(roomid,data.dir)
		MF_setvalue(roomid,ROOMW,w)
		MF_setvalue(roomid,ROOMH,h)
		MF_setvalue(roomid,STYLE,style - 1)
		
		addroom(roomid,x,y,w,h,str)
	else
		if (data.name == "door_hor") then
			addwall(x,y,0,2)
			adddoor(roomid,x,y,0)
		elseif (data.name == "door_ver") then
			addwall(x,y,3,2)
			adddoor(roomid,x,y,3)
		elseif (data.name == "scaffold") then
			MF_setdir(roomid,data.dir)
		end
	end
	
	delete(structid)
end

function setuproom(roomtype,x,y,upgrade_)
	local upgrade = upgrade_ or ""
	
	local data = {}
	local style = 0
	
	for i,v in ipairs(roomlist) do
		if (v.id == roomtype) then
			data = v
			style = i
			break
		end
	end
	
	local obj = data.object or "room"
	local s = data.special or false
	
	local roomid = MF_create(obj)
	local w = data.size[1]
	local h = data.size[2]
	local str = data.code
	
	MF_setvalue(roomid,ONLINE,1)
	MF_setvalue(roomid,XPOS,x * map.tilesize)
	MF_setvalue(roomid,YPOS,y * map.tilesize)
	MF_setvalue(roomid,XTILE,x)
	MF_setvalue(roomid,YTILE,y)
	MF_setstring(roomid,TYPE,data.id)
	MF_setstring(roomid,NAME,data.name)
	
	if (s == false) then
		MF_setdir(roomid,data.dir)
		MF_setvalue(roomid,ROOMW,w)
		MF_setvalue(roomid,ROOMH,h)
		MF_setvalue(roomid,STYLE,style - 1)
		MF_setstring(roomid,TYPE,data.code or data.id)
		
		addroom(roomid,x,y,w,h,str)
		
		if (string.len(upgrade) > 0) then
			local udata = data.upgrades[upgrade]
			
			if (udata.setup ~= nil) then
				udata.setup(roomid,data)
			end
			
			MF_setvalue(roomid,UPGRADED,1)
		end
	else
		if (data.name == "door_hor") then
			addwall(x,y,0,2)
			adddoor(roomid,x,y,0)
		elseif (data.name == "door_ver") then
			addwall(x,y,3,2)
			adddoor(roomid,x,y,3)
		elseif (data.name == "scaffold") then
			MF_setdir(roomid,data.dir)
		end
	end
end

function deleteroom(roomid)
	local mdata = map.astar["marray"]
	
	local delthis = 0
	
	for i,room in ipairs(rooms) do
		if (room.id == roomid) then
			local x,y = room.x,room.y
			local w = room.w
			local h = room.h
			local tiles = w * h
			
			for a=0,tiles-1 do
				local x_ = x + a % w
				local y_ = y + math.floor(a / w)
				
				local tileid = gettileid(x_,y_)
				local existing = mdata[tileid] or {}
				existing = existing[2] or "1111"
				local data = "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] = nil
				--mdata[tileid] = nil
			end
			
			delthis = i
			break
		end
	end
	
	if (delthis > 0) then
		table.remove(rooms, delthis)
	end
	
	local delthese = {}
	
	for i,struct in ipairs(structs) do
		local owner = MF_getvalue(struct.id,S_OWNER)
		
		if (owner == roomid) then
			table.insert(delthese, struct.id)
		end
	end
	
	for i,v in ipairs(delthese) do
		delete(v)
	end
	
	MF_delete(roomid)
end

function roomprice(roomtype)
	local rdata = roomlist[roomtype]
	local cm = MF_getvalue(GDATA,MATERIAL)
	
	if (cm >= rdata.cost) then
		MF_setvalue(GDATA,MATERIAL,cm - rdata.cost)
		
		return 1
	end
	
	return 0
end

function refund(roomtype)
	local rdata = roomlist[roomtype]
	local cm = MF_getvalue(GDATA,MATERIAL)
	MF_setvalue(GDATA,MATERIAL,cm + rdata.cost)
end

function checkprice(roomtype,upgradetype)
	local rdata = roomlist[roomtype]
	local udata = rdata.upgrades
	local udata2 = udata[upgradetype]
	local cost = udata2.cost or {0,0,0,0}
	
	local cm = MF_getvalue(GDATA,MATERIAL)
	local cf = MF_getvalue(GDATA,FOOD)
	local co = MF_getvalue(GDATA,ORE)
	local ce = MF_getvalue(GDATA,ENERGY)
	
	if (cm >= cost[1]) and (cf >= cost[2]) and (co >= cost[3]) and (ce >= cost[4]) then
		MF_setvalue(GDATA,MATERIAL,cm - cost[1])
		MF_setvalue(GDATA,FOOD,cf - cost[2])
		MF_setvalue(GDATA,ORE,co - cost[3])
		MF_setvalue(GDATA,ENERGY,ce - cost[4])
		
		return 1
	end
	
	return 0
end