-- Compiled by Scene2Res, a MTA:SA GTA III engine map importer
local textureCache = {};
local streamedObjects = {};
local pModels = {};

setCloudsEnabled(false);
debug.sethook(nil);

local function getResourceSize(path)
	if not (fileExists(path)) then return 0; end;

	local file = fileOpen(path);
	local size = fileGetSize(file)

	fileClose(file);
	return size;
end

local function requestTexture(model, path)
	local txd = textureCache[path];

	if not (txd) then
		local size = getResourceSize(path);
		if (size == 0) then return false; end

		txd = engineLoadTXD(path, false);

		if not (txd) then
			return false;
		end

		textureCache[path] = txd;
	end
	return txd;
end

local function loadModel(model)
	if (model.loaded) then return true; end

	if (model.super) then
       local superModel = pModels[model.super];
       if (superModel) then
		    if not (loadModel(superModel)) then
			    return false;
		    end
       end
	end

	engineReplaceModel(model.model, model.id);
	engineReplaceCOL(model.col, model.id);

	model.loaded = true;
	return true;
end

local function freeModel(model)
	if not (model.loaded) then return true; end

	engineRestoreModel(model.id);
	engineRestoreCOL(model.id);

	model.loaded = false;
	if (model.super) then
       local superModel = pModels[model.super];
       if (superModel) then
		    freeModel(superModel);
       end
	end
end

local function modelStreamOut ()
	local pModel = pModels[getElementModel(source)];

	if not (pModel) then return end;

	if (pModel.lodID) then return true; end

	if (pModel.isRequesting == 2) then
		pModel.isRequesting = 1;
		return true;
	elseif (pModel.isRequesting == 3) then
		pModel.isRequesting = 4;
		return true;
	elseif (pModel.isRequesting) then return true; end

	pModel.numStream = pModel.numStream - 1;

	if (pModel.numStream == 0) then
		pModel.isRequesting = 3;

		freeModel(pModel);

		pModel.isRequesting = false;
	end
end

local function modelStreamIn ()
	local pModel = pModels[getElementModel(source)];

	if not (pModel) then return end;

	if (pModel.lodID) then return true; end

	if (pModel.isRequesting == 1) then
		pModel.isRequesting = false;
		return true;
	elseif (pModel.isRequesting) then return true; end

	pModel.numStream = pModel.numStream + 1;

	if not (pModel.numStream == 1) then return true; end

	pModel.isRequesting = 2;

	if not (loadModel(pModel)) then
		setElementInterior(source, 123);
		setElementCollisionsEnabled(source, false);
	end

	if (pModel.isRequesting == 2) then
		outputDebugString("invalid model request '"..pModel.name.."'");
		freeModel(pModel);
		pModel.isRequesting = false;

		pModel.numStream = 0;
	end
end

function loadModels ()
	local pModel, pTXD, pColl, pTable;

	pTable={

	};

	for m,n in ipairs(pTable) do
		pModels[n[1]] = {
			id = n[1],
			name = n[2],
			txd = requestTexture(false, "textures/"..n[3]..".txd"),
			numStream = 0,
			lod = n[4] / 5,
			super = n[5]
		};
		pModelEntry = pModels[n[1]];
		engineImportTXD(pModelEntry.txd, n[1]);
		pModelEntry.model = engineLoadDFF("models/"..n[2]..".dff", n[1]);
		engineReplaceModel(pModelEntry.model, n[1]);
		if ( #n == 6 ) then
			pModelEntry.col = engineLoadCOL("coll/"..n[6]..".col");
			engineReplaceCOL(pModelEntry.col, n[1]);
		end
		engineSetModelLODDistance(n[1], n[4] / 5);
	end

	pTable={
		{ model=11595, model_file="road_37", txd_file="1", coll_file="road_37", lod=500 },
		{ model=11596, model_file="back_60", txd_file="1", coll_file="back_60", lod=500 },
		{ model=11597, model_file="stvo_01", txd_file="1", coll_file="stvo_01", lod=500 },
		{ model=11598, model_file="othe_19", txd_file="1", coll_file="othe_19", lod=500 },
		{ model=11599, model_file="tree_162", txd_file="1", coll_file="tree_162", lod=500 },
		{ model=11600, model_file="tree_161", txd_file="1", coll_file="tree_161", lod=500 },
		{ model=11601, model_file="tree_160", txd_file="1", coll_file="tree_160", lod=500 },
		{ model=11602, model_file="tree_159", txd_file="1", coll_file="tree_159", lod=500 },
		{ model=11603, model_file="tree_158", txd_file="1", coll_file="tree_158", lod=500 },
		{ model=11604, model_file="tree_157", txd_file="1", coll_file="tree_157", lod=500 },
		{ model=11605, model_file="tree_156", txd_file="1", coll_file="tree_156", lod=500 },
		{ model=11606, model_file="tree_155", txd_file="1", coll_file="tree_155", lod=500 },
		{ model=11607, model_file="tree_154", txd_file="1", coll_file="tree_154", lod=500 },
		{ model=11611, model_file="tree_153", txd_file="1", coll_file="tree_153", lod=500 },
		{ model=11612, model_file="tree_152", txd_file="1", coll_file="tree_152", lod=500 },
		{ model=11613, model_file="tree_151", txd_file="1", coll_file="tree_151", lod=500 },
		{ model=11614, model_file="tree_150", txd_file="1", coll_file="tree_150", lod=500 },
		{ model=11615, model_file="tree_149", txd_file="1", coll_file="tree_149", lod=500 },
		{ model=11616, model_file="tree_148", txd_file="1", coll_file="tree_148", lod=500 },
		{ model=11617, model_file="tree_147", txd_file="1", coll_file="tree_147", lod=500 },
		{ model=11618, model_file="tree_146", txd_file="1", coll_file="tree_146", lod=500 },
		{ model=11619, model_file="tree_145", txd_file="1", coll_file="tree_145", lod=500 },
		{ model=11620, model_file="tree_144", txd_file="1", coll_file="tree_144", lod=500 },
		{ model=11621, model_file="tree_143", txd_file="1", coll_file="tree_143", lod=500 },
		{ model=11622, model_file="tree_142", txd_file="1", coll_file="tree_142", lod=500 },
		{ model=11623, model_file="tree_141", txd_file="1", coll_file="tree_141", lod=500 },
		{ model=11624, model_file="tree_140", txd_file="1", coll_file="tree_140", lod=500 },
		{ model=11628, model_file="tree_139", txd_file="1", coll_file="tree_139", lod=500 },
		{ model=11629, model_file="tree_138", txd_file="1", coll_file="tree_138", lod=500 },
		{ model=11630, model_file="tree_137", txd_file="1", coll_file="tree_137", lod=500 },
		{ model=11632, model_file="tree_136", txd_file="1", coll_file="tree_136", lod=500 },
		{ model=11633, model_file="tree_135", txd_file="1", coll_file="tree_135", lod=500 },
		{ model=11634, model_file="tree_134", txd_file="1", coll_file="tree_134", lod=500 },
		{ model=11635, model_file="tree_133", txd_file="1", coll_file="tree_133", lod=500 },
		{ model=11636, model_file="tree_132", txd_file="1", coll_file="tree_132", lod=500 },
		{ model=11637, model_file="tree_131", txd_file="1", coll_file="tree_131", lod=500 },
		{ model=11638, model_file="tree_130", txd_file="1", coll_file="tree_130", lod=500 },
		{ model=11639, model_file="tree_129", txd_file="1", coll_file="tree_129", lod=500 },
		{ model=11640, model_file="tree_128", txd_file="1", coll_file="tree_128", lod=500 },
		{ model=11641, model_file="tree_127", txd_file="1", coll_file="tree_127", lod=500 },
		{ model=11642, model_file="tree_126", txd_file="1", coll_file="tree_126", lod=500 },
		{ model=11643, model_file="tree_125", txd_file="1", coll_file="tree_125", lod=500 },
		{ model=11644, model_file="tree_124", txd_file="1", coll_file="tree_124", lod=500 },
		{ model=11645, model_file="tree_123", txd_file="1", coll_file="tree_123", lod=500 },
		{ model=11646, model_file="tree_122", txd_file="1", coll_file="tree_122", lod=500 },
		{ model=11647, model_file="tree_121", txd_file="1", coll_file="tree_121", lod=500 },
		{ model=11648, model_file="tree_120", txd_file="1", coll_file="tree_120", lod=500 },
		{ model=11649, model_file="tree_119", txd_file="1", coll_file="tree_119", lod=500 },
		{ model=11650, model_file="tree_118", txd_file="1", coll_file="tree_118", lod=500 },
		{ model=11651, model_file="tree_117", txd_file="1", coll_file="tree_117", lod=500 },
		{ model=11652, model_file="tree_116", txd_file="1", coll_file="tree_116", lod=500 },
		{ model=11653, model_file="tree_115", txd_file="1", coll_file="tree_115", lod=500 },
		{ model=11654, model_file="tree_114", txd_file="1", coll_file="tree_114", lod=500 },
		{ model=11655, model_file="tree_113", txd_file="1", coll_file="tree_113", lod=500 },
		{ model=11656, model_file="tree_112", txd_file="1", coll_file="tree_112", lod=500 },
		{ model=11657, model_file="tree_111", txd_file="1", coll_file="tree_111", lod=500 },
		{ model=11658, model_file="tree_110", txd_file="1", coll_file="tree_110", lod=500 },
		{ model=11659, model_file="tree_109", txd_file="1", coll_file="tree_109", lod=500 },
		{ model=11660, model_file="tree_108", txd_file="1", coll_file="tree_108", lod=500 },
		{ model=11661, model_file="tree_107", txd_file="1", coll_file="tree_107", lod=500 },
		{ model=11662, model_file="tree_106", txd_file="1", coll_file="tree_106", lod=500 },
		{ model=11664, model_file="tree_105", txd_file="1", coll_file="tree_105", lod=500 },
		{ model=11665, model_file="tree_104", txd_file="1", coll_file="tree_104", lod=500 },
		{ model=11666, model_file="tree_103", txd_file="1", coll_file="tree_103", lod=500 },
		{ model=11667, model_file="tree_102", txd_file="1", coll_file="tree_102", lod=500 },
		{ model=11668, model_file="tree_101", txd_file="1", coll_file="tree_101", lod=500 },
		{ model=11669, model_file="tree_100", txd_file="1", coll_file="tree_100", lod=500 },
		{ model=11670, model_file="tree_99", txd_file="1", coll_file="tree_99", lod=500 },
		{ model=11671, model_file="tree_98", txd_file="1", coll_file="tree_98", lod=500 },
		{ model=11672, model_file="tree_97", txd_file="1", coll_file="tree_97", lod=500 },
		{ model=11673, model_file="tree_96", txd_file="1", coll_file="tree_96", lod=500 },
		{ model=11674, model_file="tree_95", txd_file="1", coll_file="tree_95", lod=500 },
		{ model=11675, model_file="tree_94", txd_file="1", coll_file="tree_94", lod=500 },
		{ model=11676, model_file="tree_93", txd_file="1", coll_file="tree_93", lod=500 },
		{ model=17000, model_file="tree_92", txd_file="1", coll_file="tree_92", lod=500 },
		{ model=17001, model_file="tree_91", txd_file="1", coll_file="tree_91", lod=500 },
		{ model=17002, model_file="tree_90", txd_file="1", coll_file="tree_90", lod=500 },
		{ model=17003, model_file="tree_89", txd_file="1", coll_file="tree_89", lod=500 },
		{ model=17004, model_file="tree_88", txd_file="1", coll_file="tree_88", lod=500 },
		{ model=17005, model_file="tree_87", txd_file="1", coll_file="tree_87", lod=500 },
		{ model=17006, model_file="tree_86", txd_file="1", coll_file="tree_86", lod=500 },
		{ model=17007, model_file="tree_85", txd_file="1", coll_file="tree_85", lod=500 },
		{ model=17008, model_file="tree_84", txd_file="1", coll_file="tree_84", lod=500 },
		{ model=17009, model_file="tree_83", txd_file="1", coll_file="tree_83", lod=500 },
		{ model=17010, model_file="tree_82", txd_file="1", coll_file="tree_82", lod=500 },
		{ model=17011, model_file="tree_81", txd_file="1", coll_file="tree_81", lod=500 },
		{ model=17012, model_file="tree_80", txd_file="1", coll_file="tree_80", lod=500 },
		{ model=17013, model_file="tree_79", txd_file="1", coll_file="tree_79", lod=500 },
		{ model=17014, model_file="tree_78", txd_file="1", coll_file="tree_78", lod=500 },
		{ model=17015, model_file="tree_77", txd_file="1", coll_file="tree_77", lod=500 },
		{ model=17016, model_file="tree_76", txd_file="1", coll_file="tree_76", lod=500 },
		{ model=17017, model_file="tree_75", txd_file="1", coll_file="tree_75", lod=500 },
		{ model=17019, model_file="tree_74", txd_file="1", coll_file="tree_74", lod=500 },
		{ model=17020, model_file="tree_73", txd_file="1", coll_file="tree_73", lod=500 },
		{ model=17021, model_file="tree_72", txd_file="1", coll_file="tree_72", lod=500 },
		{ model=17022, model_file="tree_71", txd_file="1", coll_file="tree_71", lod=500 },
		{ model=17023, model_file="tree_70", txd_file="1", coll_file="tree_70", lod=500 },
		{ model=17024, model_file="tree_69", txd_file="1", coll_file="tree_69", lod=500 },
		{ model=17025, model_file="tree_68", txd_file="1", coll_file="tree_68", lod=500 },
		{ model=17026, model_file="tree_67", txd_file="1", coll_file="tree_67", lod=500 },
		{ model=17027, model_file="tree_66", txd_file="1", coll_file="tree_66", lod=500 },
		{ model=17028, model_file="tree_65", txd_file="1", coll_file="tree_65", lod=500 },
		{ model=17029, model_file="tree_64", txd_file="1", coll_file="tree_64", lod=500 },
		{ model=17030, model_file="tree_63", txd_file="1", coll_file="tree_63", lod=500 },
		{ model=17031, model_file="tree_62", txd_file="1", coll_file="tree_62", lod=500 },
		{ model=17032, model_file="tree_61", txd_file="1", coll_file="tree_61", lod=500 },
		{ model=17033, model_file="tree_60", txd_file="1", coll_file="tree_60", lod=500 },
		{ model=17034, model_file="tree_59", txd_file="1", coll_file="tree_59", lod=500 },
		{ model=17035, model_file="tree_58", txd_file="1", coll_file="tree_58", lod=500 },
		{ model=17036, model_file="tree_57", txd_file="1", coll_file="tree_57", lod=500 },
		{ model=17037, model_file="tree_56", txd_file="1", coll_file="tree_56", lod=500 },
		{ model=17038, model_file="tree_55", txd_file="1", coll_file="tree_55", lod=500 },
		{ model=17039, model_file="tree_54", txd_file="1", coll_file="tree_54", lod=500 },
		{ model=17041, model_file="tree_53", txd_file="1", coll_file="tree_53", lod=500 },
		{ model=17042, model_file="tree_52", txd_file="1", coll_file="tree_52", lod=500 },
		{ model=17043, model_file="tree_51", txd_file="1", coll_file="tree_51", lod=500 },
		{ model=17044, model_file="tree_50", txd_file="1", coll_file="tree_50", lod=500 },
		{ model=17045, model_file="tree_49", txd_file="1", coll_file="tree_49", lod=500 },
		{ model=17049, model_file="tree_48", txd_file="1", coll_file="tree_48", lod=500 },
		{ model=17050, model_file="tree_47", txd_file="1", coll_file="tree_47", lod=500 },
		{ model=17051, model_file="tree_46", txd_file="1", coll_file="tree_46", lod=500 },
		{ model=17052, model_file="tree_45", txd_file="1", coll_file="tree_45", lod=500 },
		{ model=17053, model_file="tree_44", txd_file="1", coll_file="tree_44", lod=500 },
		{ model=17054, model_file="tree_43", txd_file="1", coll_file="tree_43", lod=500 },
		{ model=17055, model_file="tree_42", txd_file="1", coll_file="tree_42", lod=500 },
		{ model=17056, model_file="tree_41", txd_file="1", coll_file="tree_41", lod=500 },
		{ model=17057, model_file="tree_40", txd_file="1", coll_file="tree_40", lod=500 },
		{ model=17058, model_file="tree_39", txd_file="1", coll_file="tree_39", lod=500 },
		{ model=17059, model_file="tree_38", txd_file="1", coll_file="tree_38", lod=500 },
		{ model=17060, model_file="tree_37", txd_file="1", coll_file="tree_37", lod=500 },
		{ model=17061, model_file="tree_36", txd_file="1", coll_file="tree_36", lod=500 },
		{ model=17062, model_file="tree_35", txd_file="1", coll_file="tree_35", lod=500 },
		{ model=17063, model_file="tree_34", txd_file="1", coll_file="tree_34", lod=500 },
		{ model=17064, model_file="tree_33", txd_file="1", coll_file="tree_33", lod=500 },
		{ model=17065, model_file="tree_32", txd_file="1", coll_file="tree_32", lod=500 },
		{ model=17066, model_file="tree_31", txd_file="1", coll_file="tree_31", lod=500 },
		{ model=17067, model_file="tree_30", txd_file="1", coll_file="tree_30", lod=500 },
		{ model=17068, model_file="tree_29", txd_file="1", coll_file="tree_29", lod=500 },
		{ model=17069, model_file="tree_28", txd_file="1", coll_file="tree_28", lod=500 },
		{ model=17071, model_file="tree_27", txd_file="1", coll_file="tree_27", lod=500 },
		{ model=17072, model_file="tree_26", txd_file="1", coll_file="tree_26", lod=500 },
		{ model=17073, model_file="tree_25", txd_file="1", coll_file="tree_25", lod=500 },
		{ model=17074, model_file="tree_24", txd_file="1", coll_file="tree_24", lod=500 },
		{ model=17075, model_file="tree_23", txd_file="1", coll_file="tree_23", lod=500 },
		{ model=17076, model_file="tree_22", txd_file="1", coll_file="tree_22", lod=500 },
		{ model=17077, model_file="tree_21", txd_file="1", coll_file="tree_21", lod=500 },
		{ model=17078, model_file="tree_20", txd_file="1", coll_file="tree_20", lod=500 },
		{ model=17079, model_file="tree_19", txd_file="1", coll_file="tree_19", lod=500 },
		{ model=17080, model_file="tree_18", txd_file="1", coll_file="tree_18", lod=500 },
		{ model=17081, model_file="tree_17", txd_file="1", coll_file="tree_17", lod=500 },
		{ model=17082, model_file="tree_16", txd_file="1", coll_file="tree_16", lod=500 },
		{ model=17083, model_file="tree_15", txd_file="1", coll_file="tree_15", lod=500 },
		{ model=17084, model_file="tree_14", txd_file="1", coll_file="tree_14", lod=500 },
		{ model=17085, model_file="tree_13", txd_file="1", coll_file="tree_13", lod=500 },
		{ model=17086, model_file="tree_12", txd_file="1", coll_file="tree_12", lod=500 },
		{ model=17087, model_file="tree_11", txd_file="1", coll_file="tree_11", lod=500 },
		{ model=17088, model_file="tree_10", txd_file="1", coll_file="tree_10", lod=500 },
		{ model=17089, model_file="tree_09", txd_file="1", coll_file="tree_09", lod=500 },
		{ model=17090, model_file="tree_08", txd_file="1", coll_file="tree_08", lod=500 },
		{ model=17091, model_file="tree_07", txd_file="1", coll_file="tree_07", lod=500 },
		{ model=17092, model_file="tree_06", txd_file="1", coll_file="tree_06", lod=500 },
		{ model=17093, model_file="tree_05", txd_file="1", coll_file="tree_05", lod=500 },
		{ model=17094, model_file="tree_04", txd_file="1", coll_file="tree_04", lod=500 },
		{ model=17095, model_file="tree_03", txd_file="1", coll_file="tree_03", lod=500 },
		{ model=17096, model_file="tree_02", txd_file="1", coll_file="tree_02", lod=500 },
		{ model=17097, model_file="tree_01", txd_file="1", coll_file="tree_01", lod=500 },
		{ model=17098, model_file="tree_00", txd_file="1", coll_file="tree_00", lod=500 },
		{ model=17099, model_file="othe_01", txd_file="1", coll_file="othe_01", lod=500 },
		{ model=17100, model_file="othe_02", txd_file="1", coll_file="othe_02", lod=500 },
		{ model=17101, model_file="othe_03", txd_file="1", coll_file="othe_03", lod=500 },
		{ model=17102, model_file="othe_04", txd_file="1", coll_file="othe_04", lod=500 },
		{ model=17103, model_file="othe_05", txd_file="1", coll_file="othe_05", lod=500 },
		{ model=17104, model_file="othe_06", txd_file="1", coll_file="othe_06", lod=500 },
		{ model=17105, model_file="othe_07", txd_file="1", coll_file="othe_07", lod=500 },
		{ model=17106, model_file="othe_08", txd_file="1", coll_file="othe_08", lod=500 },
		{ model=17107, model_file="othe_09", txd_file="1", coll_file="othe_09", lod=500 },
		{ model=17108, model_file="othe_10", txd_file="1", coll_file="othe_10", lod=500 },
		{ model=17109, model_file="othe_11", txd_file="1", coll_file="othe_11", lod=500 },
		{ model=17110, model_file="othe_12", txd_file="1", coll_file="othe_12", lod=500 },
		{ model=17111, model_file="othe_13", txd_file="1", coll_file="othe_13", lod=500 },
		{ model=17112, model_file="othe_14", txd_file="1", coll_file="othe_14", lod=500 },
		{ model=17113, model_file="othe_15", txd_file="1", coll_file="othe_15", lod=500 },
		{ model=17114, model_file="othe_16", txd_file="1", coll_file="othe_16", lod=500 },
		{ model=17115, model_file="othe_17", txd_file="1", coll_file="othe_17", lod=500 },
		{ model=17116, model_file="othe_18", txd_file="1", coll_file="othe_18", lod=500 },
		{ model=17117, model_file="road_01", txd_file="1", coll_file="road_01", lod=500 },
		{ model=17118, model_file="road_02", txd_file="1", coll_file="road_02", lod=500 },
		{ model=17119, model_file="road_03", txd_file="1", coll_file="road_03", lod=500 },
		{ model=17120, model_file="road_04", txd_file="1", coll_file="road_04", lod=500 },
		{ model=17121, model_file="road_05", txd_file="1", coll_file="road_05", lod=500 },
		{ model=17122, model_file="road_06", txd_file="1", coll_file="road_06", lod=500 },
		{ model=17123, model_file="road_07", txd_file="1", coll_file="road_07", lod=500 },
		{ model=17124, model_file="road_08", txd_file="1", coll_file="road_08", lod=500 },
		{ model=17125, model_file="road_09", txd_file="1", coll_file="road_09", lod=500 },
		{ model=17126, model_file="road_10", txd_file="1", coll_file="road_10", lod=500 },
		{ model=17127, model_file="road_11", txd_file="1", coll_file="road_11", lod=500 },
		{ model=17128, model_file="road_12", txd_file="1", coll_file="road_12", lod=500 },
		{ model=17129, model_file="road_13", txd_file="1", coll_file="road_13", lod=500 },
		{ model=17130, model_file="road_14", txd_file="1", coll_file="road_14", lod=500 },
		{ model=17131, model_file="road_15", txd_file="1", coll_file="road_15", lod=500 },
		{ model=17132, model_file="road_16", txd_file="1", coll_file="road_16", lod=500 },
		{ model=17133, model_file="road_17", txd_file="1", coll_file="road_17", lod=500 },
		{ model=17134, model_file="road_18", txd_file="1", coll_file="road_18", lod=500 },
		{ model=17135, model_file="road_19", txd_file="1", coll_file="road_19", lod=500 },
		{ model=17136, model_file="road_20", txd_file="1", coll_file="road_20", lod=500 },
		{ model=17137, model_file="road_21", txd_file="1", coll_file="road_21", lod=500 },
		{ model=17138, model_file="road_22", txd_file="1", coll_file="road_22", lod=500 },
		{ model=17139, model_file="road_23", txd_file="1", coll_file="road_23", lod=500 },
		{ model=17140, model_file="road_24", txd_file="1", coll_file="road_24", lod=500 },
		{ model=17141, model_file="road_25", txd_file="1", coll_file="road_25", lod=500 },
		{ model=17142, model_file="road_26", txd_file="1", coll_file="road_26", lod=500 },
		{ model=17143, model_file="road_27", txd_file="1", coll_file="road_27", lod=500 },
		{ model=17144, model_file="road_28", txd_file="1", coll_file="road_28", lod=500 },
		{ model=17145, model_file="road_29", txd_file="1", coll_file="road_29", lod=500 },
		{ model=17146, model_file="road_30", txd_file="1", coll_file="road_30", lod=500 },
		{ model=17147, model_file="road_31", txd_file="1", coll_file="road_31", lod=500 },
		{ model=17148, model_file="road_32", txd_file="1", coll_file="road_32", lod=500 },
		{ model=17149, model_file="road_33", txd_file="1", coll_file="road_33", lod=500 },
		{ model=17150, model_file="road_34", txd_file="1", coll_file="road_34", lod=500 },
		{ model=17151, model_file="road_35", txd_file="1", coll_file="road_35", lod=500 },
		{ model=17152, model_file="road_36", txd_file="1", coll_file="road_36", lod=500 },
		{ model=17153, model_file="back_59", txd_file="1", coll_file="back_59", lod=500 },
		{ model=17154, model_file="back_58", txd_file="1", coll_file="back_58", lod=500 },
		{ model=17155, model_file="back_57", txd_file="1", coll_file="back_57", lod=500 },
		{ model=17156, model_file="back_56", txd_file="1", coll_file="back_56", lod=500 },
		{ model=17157, model_file="back_55", txd_file="1", coll_file="back_55", lod=500 },
		{ model=17158, model_file="back_54", txd_file="1", coll_file="back_54", lod=500 },
		{ model=17159, model_file="back_53", txd_file="1", coll_file="back_53", lod=500 },
		{ model=17160, model_file="back_52", txd_file="1", coll_file="back_52", lod=500 },
		{ model=17161, model_file="back_51", txd_file="1", coll_file="back_51", lod=500 },
		{ model=17162, model_file="back_50", txd_file="1", coll_file="back_50", lod=500 },
		{ model=17163, model_file="back_49", txd_file="1", coll_file="back_49", lod=500 },
		{ model=17164, model_file="back_48", txd_file="1", coll_file="back_48", lod=500 },
		{ model=17165, model_file="back_47", txd_file="1", coll_file="back_47", lod=500 },
		{ model=17166, model_file="back_46", txd_file="1", coll_file="back_46", lod=500 },
		{ model=17167, model_file="back_45", txd_file="1", coll_file="back_45", lod=500 },
		{ model=17168, model_file="back_44", txd_file="1", coll_file="back_44", lod=500 },
		{ model=17169, model_file="back_43", txd_file="1", coll_file="back_43", lod=500 },
		{ model=17170, model_file="back_42", txd_file="1", coll_file="back_42", lod=500 },
		{ model=17171, model_file="back_41", txd_file="1", coll_file="back_41", lod=500 },
		{ model=17172, model_file="back_40", txd_file="1", coll_file="back_40", lod=500 },
		{ model=17173, model_file="back_39", txd_file="1", coll_file="back_39", lod=500 },
		{ model=17174, model_file="back_38", txd_file="1", coll_file="back_38", lod=500 },
		{ model=17175, model_file="back_37", txd_file="1", coll_file="back_37", lod=500 },
		{ model=17176, model_file="back_36", txd_file="1", coll_file="back_36", lod=500 },
		{ model=17177, model_file="back_35", txd_file="1", coll_file="back_35", lod=500 },
		{ model=17178, model_file="back_34", txd_file="1", coll_file="back_34", lod=500 },
		{ model=17179, model_file="back_33", txd_file="1", coll_file="back_33", lod=500 },
		{ model=17180, model_file="back_32", txd_file="1", coll_file="back_32", lod=500 },
		{ model=17181, model_file="back_31", txd_file="1", coll_file="back_31", lod=500 },
		{ model=17182, model_file="back_30", txd_file="1", coll_file="back_30", lod=500 },
		{ model=17183, model_file="back_29", txd_file="1", coll_file="back_29", lod=500 },
		{ model=17184, model_file="back_28", txd_file="1", coll_file="back_28", lod=500 },
		{ model=17185, model_file="back_27", txd_file="1", coll_file="back_27", lod=500 },
		{ model=17186, model_file="back_26", txd_file="1", coll_file="back_26", lod=500 },
		{ model=17187, model_file="back_25", txd_file="1", coll_file="back_25", lod=500 },
		{ model=17188, model_file="back_24", txd_file="1", coll_file="back_24", lod=500 },
		{ model=17189, model_file="back_23", txd_file="1", coll_file="back_23", lod=500 },
		{ model=17190, model_file="back_22", txd_file="1", coll_file="back_22", lod=500 },
		{ model=17191, model_file="back_21", txd_file="1", coll_file="back_21", lod=500 },
		{ model=17192, model_file="back_20", txd_file="1", coll_file="back_20", lod=500 },
		{ model=17193, model_file="back_19", txd_file="1", coll_file="back_19", lod=500 },
		{ model=17194, model_file="back_18", txd_file="1", coll_file="back_18", lod=500 },
		{ model=17195, model_file="back_17", txd_file="1", coll_file="back_17", lod=500 },
		{ model=17196, model_file="back_16", txd_file="1", coll_file="back_16", lod=500 },
		{ model=17197, model_file="back_15", txd_file="1", coll_file="back_15", lod=500 },
		{ model=17198, model_file="back_14", txd_file="1", coll_file="back_14", lod=500 },
		{ model=17199, model_file="back_13", txd_file="1", coll_file="back_13", lod=500 },
		{ model=17200, model_file="back_12", txd_file="1", coll_file="back_12", lod=500 },
		{ model=17201, model_file="back_11", txd_file="1", coll_file="back_11", lod=500 },
		{ model=17202, model_file="back_10", txd_file="1", coll_file="back_10", lod=500 },
		{ model=17203, model_file="back_09", txd_file="1", coll_file="back_09", lod=500 },
		{ model=17204, model_file="back_08", txd_file="1", coll_file="back_08", lod=500 },
		{ model=17205, model_file="back_07", txd_file="1", coll_file="back_07", lod=500 },
		{ model=17206, model_file="back_06", txd_file="1", coll_file="back_06", lod=500 },
		{ model=17207, model_file="back_05", txd_file="1", coll_file="back_05", lod=500 },
		{ model=17208, model_file="back_04", txd_file="1", coll_file="back_04", lod=500 },
		{ model=17209, model_file="back_03", txd_file="1", coll_file="back_03", lod=500 },
		{ model=17210, model_file="back_02", txd_file="1", coll_file="back_02", lod=500 },
		{ model=17211, model_file="back_01", txd_file="1", coll_file="back_01", lod=500 },
		{ model=17212, model_file="zabo_07", txd_file="1", coll_file="zabo_07", lod=500 },
		{ model=17213, model_file="zabo_06", txd_file="1", coll_file="zabo_06", lod=500 },
		{ model=17214, model_file="zabo_05", txd_file="1", coll_file="zabo_05", lod=500 },
		{ model=17215, model_file="zabo_04", txd_file="1", coll_file="zabo_04", lod=500 },
		{ model=17216, model_file="zabo_03", txd_file="1", coll_file="zabo_03", lod=500 },
		{ model=17217, model_file="zabo_02", txd_file="1", coll_file="zabo_02", lod=500 },
		{ model=17218, model_file="zabo_01", txd_file="1", coll_file="zabo_01", lod=500 },
		{ model=17219, model_file="stvo_02", txd_file="1", coll_file="stvo_02", lod=500 },
		{ model=17220, model_file="stvo_03", txd_file="1", coll_file="stvo_03", lod=500 },
		{ model=17221, model_file="stvo_04", txd_file="1", coll_file="stvo_04", lod=500 },
		{ model=17222, model_file="stvo_05", txd_file="1", coll_file="stvo_05", lod=500 }
	};

	local n,m;

	for n,m in ipairs(pTable) do
		pModels[m.model] = {
			id = m.model,
			name = m.model_file,
			txd = requestTexture(false, "textures/"..m.txd_file..".txd"),
			numStream = 0,
			lod = m.lod,
			lodID = m.lodID
		};
		pModelEntry = pModels[m.model];
       if (pModelEntry.txd) then
		    engineImportTXD(pModelEntry.txd, m.model);
       end
		pModelEntry.model=engineLoadDFF("models/"..m.model_file..".dff", m.model);
		pModelEntry.col=engineLoadCOL("coll/"..m.coll_file..".col");
		engineSetModelLODDistance(m.model, m.lod);

		if (m.lodID) then
			for j,k in ipairs(getElementsByType("object", resourceRoot)) do
				if (getElementModel(k) == m.model) then
					local x, y, z = getElementPosition(k);
					local rx, ry, rz = getElementRotation(k);
					setLowLODElement(k, createObject(m.lodID, x, y, z, rx, ry, rz, true));
				end
			end
		end
	end

	for m,n in pairs(pModels) do
		if (n.super) and not (n.col) then
           local superModel = pModels[n.super];
           if (superModel) then
			    n.col = superModel.col;
           end
		end
	end
end
loadModels();

for m,n in ipairs(getElementsByType("object", resourceRoot)) do
	if (isElementStreamedIn(n)) then
		source = n;
		modelStreamIn();
	end
end

addEventHandler("onClientElementStreamIn", resourceRoot, function()
		modelStreamIn();
	end
);
addEventHandler("onClientElementStreamOut", resourceRoot, function()
		modelStreamOut();
	end
);

addEventHandler("onClientResourceStop", resourceRoot, function()
		for m,n in pairs(pModels) do
			if (isElement(n.col)) then destroyElement(n.col); end
			if (isElement(n.model)) then destroyElement(n.model); end
			engineRestoreCOL(m);
			engineRestoreModel(m);
		end
       for m,n in pairs(textureCache) do
           destroyElement(n);
       end
	end
);

collectgarbage("collect");

local objects = getElementsByType ( "object", resourceRoot )
for theKey,object in ipairs(objects) do
local id = getElementModel(object)
local x,y,z = getElementPosition(object)
local rx,ry,rz = getElementRotation(object)
local scale = getObjectScale(object)
objLowLOD = createObject ( id, x,y,z,rx,ry,rz,true )
setObjectScale(objLowLOD, scale)
setLowLODElement ( object, objLowLOD )
engineSetModelLODDistance ( id, 3000 )
setElementStreamable ( object , false)
end