_result = getText (configFile >> "CfgPatches" >> "ArmA_RTE_Editor_4") == "";
if !(_result) then {
	_result = getNumber (configFile >> "CfgPatches" >> "ArmA_RTE_Editor_4" >> "version") == 3;
};
if (_result) then {
	if (isNil "ION_Broadcast_Logic") then {
		ION_Broadcast_Logic = "Logic" createVehicle [random 100, random 100];
	};

	if (isNil "ION_fBroadcast") then {
		ION_fBroadcast = compile "
			ION_Broadcast_Logic setVehicleInit format ['[] spawn {%1}', _this]; 
			processInitCommands;
		";
	};
	
	if (isNil "KRON_Replace") then {
		_script = [] execVM "KRON_Strings.sqf";
		waitUntil {scriptDone _script};
	};

	ION_RTE_fRestore = {
		if (count _this == 10) then {
			private ["_Created", "_CreatedGroups", "_Marker", "_Trigger", "_Weapons", "_Objects", "_Groups", "_Waypoints", "_OldPos", "_arr", "_result", "_i", "_groupdata", "_group", "_data", "_model", "_pos", "_dir", "_special", "_skill", "_rank", "_health", "_init", "_name", "_lock", "_fuel", "_ammo", "_previous", "_type", "_description", "_mode", "_formation", "_speed", "_behaviour", "_radius", "_timeoutmin", "_timeoutmax", "_timeoutmid", "_condition", "_activation", "_script", "_vehicleid"];
			_Created = [];
			_CreatedGroups = [];
			_Marker = _this select 0;
			_Trigger = _this select 1;
			_Weapons = _this select 2;
			_Objects = _this select 3;
			_Groups  = _this select 4;
			//Marker erstellen
			{
				_name = _x select 0;
				if (markerColor _name != "") then {
					private ["_i"];
					_i = 1;
					while {markerColor (format ['%1_%2', _name, _i]) != ""} do {
						_i = _i + 1;
					};
					_name = format ["%1_%2", _name, _i];
				};
				_pos = _x select 1;
				_axisA = _x select 2;
				_axisB = _x select 3;
				_angle = _x select 4;
				_color = _x select 5;
				_shape = _x select 6;
				_type = _x select 7;
				_text = _x select 8;

				_result = createMarker [_name, _pos];
				_result setMarkerColor _color;
				switch (_shape) do {
					case "RECTANGLE": {
						_result setMarkerShape "RECTANGLE";
						_result setMarkerBrush _type;
					};
					case "ELLIPSE": {
						_result setMarkerShape "ELLIPSE";
						_result setMarkerBrush _type;
					};
					default {
						_result setMarkerShape "ICON";
						_result setMarkerType _type;
					};
				};
				_result setMarkerSize [_axisA, _axisB];
				_result setMarkerDir _angle;
				_result setMarkerText _text;
			} forEach _Marker;
			//Trigger erstellen
			{
				_pos = _x select 0;
				_axisA = _x select 1;
				_axisB = _x select 2;
				_angle = _x select 3;
				_rectangle = _x select 4;
				_activation = _x select 5;
				_type = _x select 6;
				_repeat = _x select 7;
				_min = _x select 8;
				_max = _x select 9;
				_mid = _x select 10;
				_interruptable = _x select 11;
				_endtype = _x select 12;
				_name = _x select 13;
				if (_name != "") then {
					if !(isNil _name) then {
						private ["_i"];
						_i = 1;
						while {!isNil format ["%1_%2", _name, _i]} do {
							_i = _i + 1;
						};
						_name = format ["%1_%2", _name, _i];
					};
				};
				_text = _x select 14;
				_text2 = [_text, "'", "''"] call KRON_Replace;
				_condition = _x select 15;
				_condition2 = [_condition, "'", "''"] call KRON_Replace;
				_onactivation = _x select 16;
				_onact2 = [_onactivation, "'", "''"] call KRON_Replace;
				_ctrl = _display displayCtrl ION_CREATETRIGGER_ONDEA;
				_ondeactivation = _x select 17;
				_ondea2 = [_ondeactivation, "'", "''"] call KRON_Replace;

				_code = format ["_trigger = createTrigger ['EmptyDetector', %1];
								_trigger setTriggerActivation ['%2', '%3', %4];
								_trigger setTriggerArea [%5, %6, %7, %8];
								_trigger setTriggerStatements ['%9', '%10', '%11'];
								_trigger setTriggerText '%12';
								_trigger setTriggerTimeout [%13, %14, %15, %16];
								_trigger setTriggerType '%17';",
								_pos,
								_activation, _type, _repeat, 
								_axisA, _axisB, _angle, _rectangle,
								_condition2, _onact2, _ondea2,
								_text2,
								_min, _mid, _max, _interruptable,
								_endtype];
								
				_code call ION_fBroadcast;
			} forEach _Trigger;
			//Waffen erstellen
			{
				_arr = (_x select 1);
				_pos = _arr select 0;
				_dir = _arr select 1;
				_vectorUp = _arr select 2;
				_unit = "WeaponHolder" createVehicle _pos;
				_unit setPos _pos;
				_unit setDir _dir;
				_unit setVectorUp _vectorUp;
				_init = format ["{this addWeaponCargo _x} forEach %1; {this addMagazineCargo _x} forEach %2", _arr select 3, _arr select 4];
				_unit setVehicleInit _init;
				processInitCommands;
				
				_Created set [_x select 0, _unit];
			} forEach _Weapons;
			//Objekte erstellen
			{
				_arr = (_x select 1);
				_model = _arr select 0;
				_pos = _arr select 1;
				if (count _arr > 2) then {
					_dir = _arr select 2;
				} else {
					_dir = 0;
				};
				if (count _arr > 3) then {
					_special = _arr select 3;
				} else {
					_special = "NONE";
				};
				if (count _arr > 4) then {
					_lock = _arr select 4;
				} else {
					_lock = "DEFAULT";
				};
				if (count _arr > 5) then {
					_health = _arr select 5;
				} else {
					_health = 1;
				};
				if (count _arr > 6) then {
					_fuel = _arr select 6;
				} else {
					_fuel = 1;
				};
				if (count _arr > 7) then {
					_ammo = _arr select 7;
				} else {
					_ammo = 1;
				};
				if (count _arr > 8) then {
					_init = _arr select 8;
				} else {
					_init = "";
				};
				if (count _arr > 9) then {
					_name = _arr select 9;
					if (_name != "") then {
						if !(isNil _name) then {
							private ["_i"];
							_i = 1;
							while {!isNil format ["%1_%2", _name, _i]} do {
								_i = _i + 1;
							};
							_name = format ["%1_%2", _name, _i];
						};
					};
				} else {
					_name = "";
				};
				_unit = createVehicle [_model, _pos, [], 0, _special];
				_unit setPos _pos;
				_unit setDir _dir;
				_unit setVehicleLock _lock;
				_unit setDamage (1 - _health);
				_unit setFuel _fuel;
				_unit setVehicleAmmo _ammo;
				if (_name != "") then {
					call compile format ["%1 = _unit", _name];
					publicVariable _name;
				};
				_unit setVehicleInit _init;
				processInitCommands;
				_Created set [_x select 0, _unit];
			} forEach _Objects;
			//Gruppen erstellen
			for "_i" from 0 to count _Groups - 1 do {
				_groupdata = _Groups select _i;
				_group = createGroup (_groupdata select 0);
				_CreatedGroups set [_i, _group];
				{
					//Einheiten erstellen
					_data = _x select 1;
					_model = _data select 0;
					_pos = _data select 1;
					_dir = _data select 2;
					_special = _data select 3;
					_skill = _data select 4;
					_rank = _data select 5;
					_health = _data select 6;
					_init = _data select 7;
					_name = _data select 8;
					if (_name != "") then {
						if !(isNil _name) then {
							private ["_i"];
							_i = 1;
							while {!isNil format ["%1_%2", _name, _i]} do {
								_i = _i + 1;
							};
							_name = format ["%1_%2", _name, _i];
						};
					};
					_side = _groupdata select 0;
					
					_unit = _group createUnit [_model, _pos, [], 0, _special];
					switch (_special) do {
						case "NONE": {
							[_unit, _pos] spawn ION_RTE_fSetPos;
						};
					};
					[_unit, _dir] call ION_RTE_fSetDir;
					[_unit] join _group;
					_unit setSkill _skill;
					_unit setRank _rank;
					_unit setDamage (1 - _health);
					if (_name != "") then {
						call compile format ["%1 = _unit", _name];
						publicVariable _name;
					};
					_unit setVehicleInit _init;
					processInitCommands;
					
					_Created set [_x select 0, _unit];
				} forEach (_groupdata select 1);
				{
					//Fahrzeug erstellen
					_data = _x select 1;
					_model = _data select 0; 
					_pos = _data select 1;
					_dir = _data select 2;
					_special = _data select 3;
					_skill = _data select 4;
					_rank = _data select 5;
					_lock = _data select 6;
					_health = _data select 7;
					_fuel = _data select 8;
					_ammo = _data select 9;
					_init = _data select 10;
					_name = _data select 11;
					if (_name != "") then {
						if !(isNil _name) then {
							private ["_i"];
							_i = 1;
							while {!isNil format ["%1_%2", _name, _i]} do {
								_i = _i + 1;
							};
							_name = format ["%1_%2", _name, _i];
						};
					};
					_side = _groupdata select 0;
					
					_vehicle = createVehicle [_model, _pos, [], 0, _special];
					_vehicle setPos _pos;
					_vehicle setDir _dir;
					_vehicle setVehicleLock _lock;
					_vehicle setDamage (1 - _health);
					_vehicle setFuel _fuel;
					_vehicle setVehicleAmmo _ammo;
					if (_name != "") then {
						call compile format ["%1 = _vehicle", _name];
						publicVariable _name;
					};
					_vehicle setVehicleInit _init;
					processInitCommands;
					
					_crewModel = getText(configFile >> "CfgVehicles" >> _model >> "crew");

					if (_vehicle emptyPositions "Commander" == 1) then {
						_commander = _group createUnit [_crewModel, _pos, [], 0, "NONE"];
						_commander moveInCommander _vehicle;
						_commander setSkill _skill;
						_commander setRank _rank;
					};

					for "_i" from 0 to ((_vehicle emptyPositions "Gunner") - 1) do {
						_gunner = _group createUnit [_crewModel, _pos, [], 0, "NONE"];
						_gunner moveInTurret [_vehicle, [_i]];
						_gunner setSkill _skill;
						_gunner setRank _rank;
					};

					if (_vehicle emptyPositions "Driver" == 1) then {
						_driver = _group createUnit [_crewModel, _pos, [], 0, "NONE"];
						_driver moveInDriver _vehicle;
						_driver setSkill _skill;
						_driver setRank _rank;
					};
					_vehicle setDir _dir;
					[_vehicle] join _group;
					
					_Created set [_x select 0, _vehicle];
				} forEach (_groupdata select 2);
			};
			for "_i" from 0 to count _Groups - 1 do {
				_groupdata = _Groups select _i;
				_group = _CreatedGroups select _i;
				{
					//Wegpunkt erstellen
					_pos = _x select 0;
					_type = _x select 1;
					_description = _x select 2;
					_mode = _x select 3;
					_formation = _x select 4;
					_speed = _x select 5;
					_behaviour = _x select 6;
					_radius = _x select 7;
					_timeout =  [_x select 8, _x select 9, _x select 10];
					_condition = _x select 11;
					_activation = _x select 12;
					_script = _x select 13;
					_vehicleid = _x select 14;
					if (_vehicleid == -1) then {
						_vehicleid = objNull;
					} else {
						_vehicleid = _Created select _vehicleid;
					};
					_wp = _group addWaypoint [_pos, _radius];
					_wp setWaypointStatements [_condition, _activation];
					_wp setWaypointType _type;
					_wp setWaypointDescription _description;
					_wp setWaypointCombatMode _mode;
					_wp setWaypointFormation _formation;
					_wp setWaypointSpeed _speed;	
					_wp setWaypointBehaviour _behaviour;
					_wp setWaypointTimeout _timeout;
					_waypoint setWaypointScript _script;
					if !(isNull _vehicleid) then {
						_wp waypointAttachVehicle _vehicleid;
					};
				} forEach (_groupdata select 3);
			};
		};
	};
};