
otstd.doors = {}
otstd.doorlist = {
		[1209] = {newid=1211, horizontal=false, closed=true, level=false},
		[1210] = {newid=1211, horizontal=false, closed=true, level=false},
		[1211] = {newid=1210, horizontal=false, closed=false, level=false},
		[1212] = {newid=1214, horizontal=true, closed=true, level=false},
		[1213] = {newid=1214, horizontal=true, closed=true, level=false},
		[1214] = {newid=1213, horizontal=true, closed=false, level=false},
		[1219] = {newid=1220, horizontal=false, closed=true, level=false},
		[1220] = {newid=1219, horizontal=false, closed=false, level=false},
		[1221] = {newid=1222, horizontal=true, closed=true, level=false},
		[1222] = {newid=1221, horizontal=true, closed=false, level=false},
		[1223] = {newid=1224, horizontal=false, closed=true, level=false},
		[1224] = {newid=1223, horizontal=false, closed=false, level=false},
		[1225] = {newid=1226, horizontal=true, closed=true, level=false},
		[1226] = {newid=1225, horizontal=true, closed=false, level=false},
		[1227] = {newid=1228, horizontal=false, closed=true, level=true},
		[1228] = {newid=1227, horizontal=false, closed=false, level=true},
		[1229] = {newid=1230, horizontal=true, closed=true, level=true},
		[1230] = {newid=1229, horizontal=true, closed=false, level=true},
		[1231] = {newid=1233, horizontal=false, closed=true, level=false},
		[1232] = {newid=1233, horizontal=false, closed=true, level=false},
		[1233] = {newid=1232, horizontal=false, closed=false, level=false},
		[1234] = {newid=1236, horizontal=true, closed=true, level=false},
		[1235] = {newid=1236, horizontal=true, closed=true, level=false},
		[1236] = {newid=1235, horizontal=true, closed=false, level=false},
		[1237] = {newid=1238, horizontal=false, closed=true, level=false},
		[1238] = {newid=1237, horizontal=false, closed=false, level=false},
		[1239] = {newid=1240, horizontal=true, closed=true, level=false},
		[1240] = {newid=1239, horizontal=true, closed=false, level=false},
		[1241] = {newid=1242, horizontal=false, closed=true, level=false},
		[1242] = {newid=1241, horizontal=false, closed=false, level=false},
		[1243] = {newid=1244, horizontal=true, closed=true, level=false},
		[1244] = {newid=1243, horizontal=true, closed=false, level=false},
		[1245] = {newid=1246, horizontal=false, closed=true, level=true},
		[1246] = {newid=1245, horizontal=false, closed=false, level=true},
		[1247] = {newid=1248, horizontal=true, closed=true, level=true},
		[1248] = {newid=1248, horizontal=true, closed=false, level=true},
		
		[1249] = {newid=1251, horizontal=false, closed=true, level=false},
		[1250] = {newid=1251, horizontal=false, closed=true, level=false},
		[1251] = {newid=1250, horizontal=false, closed=false, level=false},
		[1252] = {newid=1254, horizontal=true, closed=true, level=false},
		[1253] = {newid=1254, horizontal=true, closed=true, level=false},
		[1254] = {newid=1253, horizontal=true, closed=false, level=false},
		[1255] = {newid=1256, horizontal=false, closed=true, level=false},
		[1256] = {newid=1255, horizontal=false, closed=false, level=false},
		[1257] = {newid=1258, horizontal=true, closed=true, level=false},
		[1258] = {newid=1257, horizontal=true, closed=false, level=false},
		[1259] = {newid=1260, horizontal=false, closed=true, level=false},
		[1260] = {newid=1259, horizontal=false, closed=false, level=false},
		[1261] = {newid=1262, horizontal=true, closed=true, level=false},
		[1262] = {newid=1261, horizontal=true, closed=false, level=false},
		
		[4913] = {newid=4915, horizontal=false, closed=true, level=false},
		[4914] = {newid=4915, horizontal=false, closed=true, level=false},
		[4915] = {newid=4914, horizontal=false, closed=false,level=false},
		[4916] = {newid=4918, horizontal=true, closed=true,  level=false},
		[4917] = {newid=4918, horizontal=true, closed=true,  level=false},
		[4918] = {newid=4917, horizontal=true, closed=false, level=false},
			
		[5082] = {newid=5083, horizontal=false, closed=true, level=false},
		[5083] = {newid=5082, horizontal=false, closed=false, level=false},
		[5084] = {newid=5085, horizontal=true, closed=true, level=false},
		[5085] = {newid=5084, horizontal=true, closed=false, level=false},
		
		[5098] = {newid=5100, horizontal=true, closed=true, level=false},
		[5099] = {newid=5100, horizontal=true, closed=true, level=false},
		[5100] = {newid=5099, horizontal=true, closed=false, level=false},
		[5101] = {newid=5102, horizontal=true, closed=true, level=false},
		[5102] = {newid=5101, horizontal=true, closed=false, level=false},
		[5103] = {newid=5104, horizontal=true, closed=true, level=true},
		[5104] = {newid=5103, horizontal=true, closed=false, level=true},
		[5105] = {newid=5106, horizontal=true, closed=true, level=false},
		[5106] = {newid=5105, horizontal=true, closed=false, level=false},
		[5107] = {newid=5109, horizontal=false, closed=true, level=false},
		[5108] = {newid=5109, horizontal=false, closed=true, level=false},
		[5109] = {newid=5108, horizontal=false, closed=false, level=false},
		[5110] = {newid=5111, horizontal=false, closed=true, level=false},
		[5111] = {newid=5110, horizontal=false, closed=false, level=false},
		[5112] = {newid=5113, horizontal=false, closed=true, level=true},
		[5113] = {newid=5112, horizontal=false, closed=false, level=true},
		[5114] = {newid=5115, horizontal=false, closed=true, level=false},
		[5115] = {newid=5114, horizontal=false, closed=false, level=false},
		
		[5116] = {newid=5118, horizontal=true, closed=true, level=false},
		[5117] = {newid=5118, horizontal=true, closed=true, level=false},
		[5118] = {newid=5117, horizontal=true, closed=false, level=false},
		[5119] = {newid=5120, horizontal=true, closed=true, level=false},
		[5120] = {newid=5119, horizontal=true, closed=false, level=false},
		[5121] = {newid=5122, horizontal=true, closed=true, level=true},
		[5122] = {newid=5121, horizontal=true, closed=false, level=true},
		[5123] = {newid=5124, horizontal=true, closed=true, level=false},
		[5124] = {newid=5123, horizontal=true, closed=false, level=false},
		[5125] = {newid=5127, horizontal=false, closed=true, level=false},
		[5126] = {newid=5127, horizontal=false, closed=true, level=false},
		[5127] = {newid=5126, horizontal=false, closed=false, level=false},
		[5128] = {newid=5129, horizontal=false, closed=true, level=false},
		[5129] = {newid=5128, horizontal=false, closed=false, level=false},
		[5130] = {newid=5131, horizontal=false, closed=true, level=true},
		[5131] = {newid=5130, horizontal=false, closed=false, level=true},
		[5132] = {newid=5133, horizontal=false, closed=true, level=false},
		[5133] = {newid=5132, horizontal=false, closed=false, level=false},
		[5134] = {newid=5136, horizontal=true, closed=true, level=false},
		[5135] = {newid=5136, horizontal=true, closed=true, level=false},
		[5136] = {newid=5135, horizontal=true, closed=false, level=false},
	
		[5137] = {newid=5139, horizontal=true, closed=true, level=false},
		[5138] = {newid=5139, horizontal=true, closed=true, level=false},
		[5139] = {newid=5138, horizontal=true, closed=false, level=false},
		[5140] = {newid=5142, horizontal=false, closed=true, level=false},
		[5141] = {newid=5142, horizontal=false, closed=true, level=false},
		[5142] = {newid=5141, horizontal=false, closed=false, level=false},
		[5143] = {newid=5145, horizontal=false, closed=true, level=false},
		[5144] = {newid=5145, horizontal=false, closed=true, level=false},
		[5145] = {newid=5144, horizontal=false, closed=false, level=false},
	
		[5278] = {newid=5280, horizontal=true, closed=true, level=false},
		[5279] = {newid=5280, horizontal=true, closed=true, level=false},
		[5280] = {newid=5279, horizontal=true, closed=false, level=false},
		[5281] = {newid=5283, horizontal=false, closed=true, level=false},
		[5282] = {newid=5283, horizontal=false, closed=true, level=false},
		[5283] = {newid=5282, horizontal=false, closed=false, level=false},
		[5284] = {newid=5285, horizontal=false, closed=true, level=false},
		[5285] = {newid=5284, horizontal=false, closed=false, level=false},
		[5286] = {newid=5287, horizontal=true, closed=true, level=false},
		[5287] = {newid=5286, horizontal=true, closed=false, level=false},
		[5288] = {newid=5289, horizontal=false, closed=true, level=false},
		[5289] = {newid=5288, horizontal=false, closed=false, level=false},
		[5290] = {newid=5291, horizontal=true, closed=true, level=false},
		[5291] = {newid=5290, horizontal=true, closed=false, level=false},
		[5292] = {newid=5293, horizontal=false, closed=true, level=true},
		[5293] = {newid=5292, horizontal=false, closed=false, level=true},
		[5294] = {newid=5295, horizontal=true, closed=true, level=true},
		[5295] = {newid=5294, horizontal=true, closed=false, level=true},
		
		[6249] = {newid=6251, horizontal=false, closed=true, level=false},
		[6250] = {newid=6251, horizontal=false, closed=true, level=false},
		[6251] = {newid=6250, horizontal=false, closed=false, level=false},
		[6252] = {newid=6254, horizontal=true, closed=true, level=false},
		[6253] = {newid=6254, horizontal=true, closed=true, level=false},
		[6254] = {newid=6253, horizontal=true, closed=false, level=false},
		[6255] = {newid=6256, horizontal=false, closed=true, level=false},
		[6256] = {newid=6255, horizontal=false, closed=false, level=false},
		[6257] = {newid=6258, horizontal=true, closed=true, level=false},
		[6258] = {newid=6257, horizontal=true, closed=false, level=false},
		[6259] = {newid=6260, horizontal=false, closed=true, level=false},
		[6260] = {newid=6259, horizontal=false, closed=false, level=false},
		[6261] = {newid=6262, horizontal=true, closed=true, level=false},
		[6262] = {newid=6261, horizontal=true, closed=false, level=false},
		[6263] = {newid=6264, horizontal=false, closed=true, level=true},
		[6264] = {newid=6263, horizontal=false, closed=false, level=true},
		[6265] = {newid=6266, horizontal=true, closed=true, level=true},
		[6266] = {newid=6265, horizontal=true, closed=false, level=true},
		
		[6462] = {newid=6463, horizontal=false, closed=false, level=false},
		[6463] = {newid=6462, horizontal=false, closed=false, level=false},
		[6464] = {newid=6465, horizontal=false, closed=true, level=false},
		[6465] = {newid=6464, horizontal=false, closed=true, level=false},
		[6466] = {newid=6467, horizontal=false, closed=false, level=false},
		[6467] = {newid=6466, horizontal=false, closed=false, level=false},
		[6468] = {newid=6469, horizontal=false, closed=true, level=false},
		[6469] = {newid=6468, horizontal=false, closed=true, level=false},
		[6470] = {newid=6472, horizontal=false, closed=false, level=false},
		[6471] = {newid=6473, horizontal=false, closed=false, level=false},
		[6472] = {newid=6470, horizontal=false, closed=true, level=false},
		[6473] = {newid=6471, horizontal=false, closed=true, level=false},
		
		[6891] = {newid=6893, horizontal=false, closed=true, level=false},
		[6892] = {newid=6893, horizontal=false, closed=true, level=false},
		[6893] = {newid=6892, horizontal=false, closed=false, level=false},
		[6894] = {newid=6895, horizontal=false, closed=true, level=false},
		[6895] = {newid=6894, horizontal=false, closed=false, level=false},
		[6896] = {newid=6896, horizontal=false, closed=true, level=true},
		[6897] = {newid=6897, horizontal=false, closed=false, level=true},
		[6898] = {newid=6899, horizontal=false, closed=true, level=false},
		[6899] = {newid=6898, horizontal=false, closed=false, level=false},
		[6900] = {newid=6902, horizontal=true, closed=true, level=false},
		[6901] = {newid=6902, horizontal=true, closed=true, level=false},
		[6902] = {newid=6901, horizontal=true, closed=false, level=false},
		[6903] = {newid=6904, horizontal=true, closed=true, level=false},
		[6904] = {newid=6903, horizontal=true, closed=false, level=false},
		[6905] = {newid=6906, horizontal=true, closed=true, level=true},
		[6906] = {newid=6905, horizontal=true, closed=false, level=true},
		[6907] = {newid=6908, horizontal=true, closed=true, level=false},
		[6908] = {newid=6907, horizontal=true, closed=false, level=false},
		
		[7025] = {newid=7026, horizontal=true, closed=true, level=false},
		[7026] = {newid=7025, horizontal=false, closed=true, level=false},
		[7027] = {newid=7028, horizontal=true, closed=false, level=false},
		[7028] = {newid=7027, horizontal=false, closed=false, level=false},
		[7029] = {newid=7030, horizontal=true, closed=true, level=false},
		[7030] = {newid=7029, horizontal=false, closed=true, level=false},
		[7031] = {newid=7032, horizontal=true, closed=false, level=false},
		[7032] = {newid=7031, horizontal=false, closed=false, level=false},
		
		[7033] = {newid=7035, horizontal=true, closed=true, level=false},
		[7034] = {newid=7035, horizontal=true, closed=true, level=false},
		[7035] = {newid=7034, horizontal=true, closed=false, level=false},
		[7036] = {newid=7037, horizontal=true, closed=true, level=false},
		[7037] = {newid=7036, horizontal=true, closed=false, level=false},
		[7038] = {newid=7039, horizontal=true, closed=true, level=true},
		[7039] = {newid=7038, horizontal=true, closed=false, level=true},
		[7040] = {newid=7041, horizontal=true, closed=true, level=false},
		[7041] = {newid=7040, horizontal=true, closed=false, level=false},
		[7042] = {newid=7044, horizontal=false, closed=true, level=false},
		[7043] = {newid=7044, horizontal=false, closed=true, level=false},
		[7044] = {newid=7043, horizontal=false, closed=false, level=false},
		[7045] = {newid=7046, horizontal=false, closed=true, level=false},
		[7046] = {newid=7045, horizontal=false, closed=false, level=false},
		[7047] = {newid=7048, horizontal=false, closed=true, level=true},
		[7048] = {newid=7047, horizontal=false, closed=false, level=true},
		[7049] = {newid=7050, horizontal=false, closed=true, level=false},
		[7050] = {newid=7049, horizontal=false, closed=false, level=false},
		
		[7054] = {newid=7055, horizontal=false, closed=true, level=false},
		[7055] = {newid=7054, horizontal=false, closed=false, level=false},
		[7056] = {newid=7057, horizontal=true, closed=true, level=false},
		[7057] = {newid=7056, horizontal=true, closed=false, level=false}

		--[[
		[8541" script="doors/door_locked.lua"/>
		[8542" script="increment.lua"/>
		[8543] = {newid= , horizontal=false, closed=false, level=false},
		[8544" script="doors/door_locked.lua"/>
		[8545" script="increment.lua"/>
		[8546] = {newid= , horizontal=true, closed=false, level=false},

		[8547" script="increment.lua"/>
		[8548] = {newid= , horizontal=false, closed=false, level=false},
		[8549" script="increment.lua"/>
		[8550] = {newid= , horizontal=true, closed=false, level=false},

		[8551" script="doors/questdoor_closed.lua"/>
		[8552" script="doors/door_open_vertical.lua"/>
		[8553" script="doors/questdoor_closed.lua"/>
		[8554] = {newid= , horizontal=true, closed=false, level=false},

		[8555" script="doors/gateofexp_closed.lua"/>
		[8556" script="doors/door_open_vertical.lua"/>
		[8557" script="doors/gateofexp_closed.lua"/>
		[8558] = {newid= , horizontal=true, closed=false, level=false},

		[8696" script="increment.lua"/>
		[8697] = {newid= , horizontal=false, closed=false, level=false},

		[9165" script="doors/door_locked.lua"/>
		[9166" script="increment.lua"/>
		[9167] = {newid= , horizontal=false, closed=false, level=false},
		[9168" script="doors/door_locked.lua"/>
		[9169" script="increment.lua"/>
		[9170] = {newid= , horizontal=true, closed=false, level=false},

		[9171" script="increment.lua"/>
		[9172" script="doors/door_open_vertical.lua"/>
		[9173" script="increment.lua"/>
		[9174] = {newid= , horizontal=true, closed=false, level=false},

		[9175" script="doors/questdoor_closed.lua"/>
		[9176" script="doors/door_open_vertical.lua"/>
		[9177" script="doors/questdoor_closed.lua"/>
		[9178] = {newid= , horizontal=true, closed=false, level=false},

		[9179" script="doors/gateofexp_closed.lua"/>
		[9180] = {newid= , horizontal=false, closed=false, level=false},
		[9181" script="doors/gateofexp_closed.lua"/>
		[9182] = {newid= , horizontal=true, closed=false, level=false},

		[9267" script="doors/door_locked.lua"/>
		[9268" script="increment.lua"/>
		[9269" script="doors/door_open_vertical.lua"/>
		[9270" script="doors/door_locked.lua"/>
		[9271" script="increment.lua"/>
		[9272] = {newid= , horizontal=true, closed=false, level=false},

		[9273" script="increment.lua"/>
		[9274" script="doors/door_open_vertical.lua"/>
		[9275" script="increment.lua"/>
		[9276] = {newid= , horizontal=true, closed=false, level=false},

		[9277" script="doors/questdoor_closed.lua"/>
		[9278" script="doors/door_open_vertical.lua"/>
		[9279" script="doors/questdoor_closed.lua"/>
		[9280] = {newid= , horizontal=true, closed=false, level=false},

		[9281" script="doors/gateofexp_closed.lua"/>
		[9282" script="doors/door_open_vertical.lua"/>
		[9283" script="doors/gateofexp_closed.lua"/>
		[9284] = {newid= , horizontal=true, closed=false, level=false},
			
		[10268" script="doors/door_locked.lua"/>
		[10269" script="increment.lua"/>
		[10270] = {newid= , horizontal=false, closed=false, level=false},
		[10271" script="doors/door_locked.lua"/>
		[10272" script="increment.lua"/>
		[10273] = {newid= , horizontal=true, closed=false, level=false},
		
		[10274" script="increment.lua"/>
		[10275" script="doors/door_open_vertical.lua"/>
		[10276" script="increment.lua"/>
		[10277] = {newid= , horizontal=true, closed=false, level=false},
		
		[10278" script="doors/questdoor_closed.lua"/>
		[10279" script="doors/door_open_vertical.lua"/>
		[10280" script="doors/gateofexp_closed.lua"/>
		[10281] = {newid= , horizontal=true, closed=false, level=false},
		
		[10282" script="doors/gateofexp_closed.lua"/>
		[10283" script="doors/door_open_vertical.lua"/>
		[10284" script="doors/gateofexp_closed.lua"/>
		[10285] = {newid= , horizontal=true, closed=false, level=false},
		
		[10468" script="doors/door_locked.lua"/>
		[10469" script="increment.lua"/>
		[10470" script="doors/door_open_vertical.lua"/>
		[10471" script="increment.lua"/>
		[10472" script="doors/door_open_vertical.lua"/>
		
		[10473" script="doors/gateofexp_closed.lua"/>
		[10474" script="doors/door_open_vertical.lua"/>
		[10475" script="doors/questdoor_closed.lua"/>
		[10476" script="doors/door_open_vertical.lua"/>
		
		[10477" script="doors/door_locked.lua"/>
		[10478" script="increment.lua"/>
		[10479] = {newid= , horizontal=true, closed=false, level=false},
		[10480" script="increment.lua"/>
		[10481] = {newid= , horizontal=true, closed=false, level=false},
		
		[10482" script="doors/gateofexp_closed.lua"/>
		[10483] = {newid= , horizontal=true, closed=false, level=false},
		[10484" script="doors/questdoor_closed.lua"/>
		[10485" script="doors/door_open_vertical.lua"/>
		]]--
	}
	
function Item:switchDoor(moveobjects)
	local doordata = otstd.doorlist[self:getItemID()] 
	
	if self:isDoor() then
		if moveobjects ~= nil and moveobjects then
			local tile = self:getParentTile()
			if doordata.closed == false then
				creatures = tile:getCreatures()
				for _, creature in pairs(creatures) do
					if(doordata.horizontal) then
						if not creature:walk(SOUTH) and not creature:walk(NORTH) then
							local npos = tile:getPosition()
							-- Couldn't walk there, try teleport south
							npos.y = npos.y + 1
							if creature:moveTo(tile:getPosition()) then
								-- Move succeded
							else
								-- Teleport north instead?
								npos.y = npos.y - 2
								if creature:moveTo(creature:getMasterPos()) then
									break
								end
							end
						end
					else
						-- vertical
						if not creature:walk(EAST) and not creature:walk(WEST) then
							local npos = tile:getPosition()
							-- Couldn't walk there, try teleport east
							npos.x = npos.x + 1
							if creature:moveTo(tile:getPosition()) then
								-- Move succeded
							else
								-- Teleport south instead?
								npos.x = npos.x - 2
								if creature:moveTo(creature:getMasterPos()) then
									break
								end
							end
						end
					end
				end
			end
		end
		self:setItemID(doordata.newid)
		return true
	end
	return false
end

function Item:isDoorClosed()
	local doordata = otstd.doorlist[self:getItemID()]
	if doordata ~= nil then
		return doordata.closed
	end
	return false
end

function Item:isDoorOpen()
	local doordata = otstd.doorlist[self:getItemID()]
	if doordata ~= nil then
		return not doordata.closed
	end
	return false
end

function Item:openDoor()
	if self:isDoorClosed() then
		return self:switchDoor()
	end
	return false
end

function Item:closeDoor(moveobjects)
	if self:isDoorOpen() then
		return self:switchDoor(moveobjects)
	end
	return false
end

function Item:isDoor()
	return otstd.doorlist[self:getItemID()] ~= nil
end

-- Generic door switch function
function otstd.doors.callback(event)
	local door = event.item
	if door:isDoor() then
		door:switchDoor(true)
		event:skip()
	end
end

-- Register listeners!
function otstd.doors.registerHandlers()
	for doorid, doortype in pairs(otstd.doorlist) do
		if doortype.listener ~= nil then
			stopListener(doortype.listener)
		end
		doortype.listener = 
			registerOnUseItem("itemid", doorid, otstd.doors.callback)
	end
end

otstd.doors.registerHandlers()



