diff --git a/Missions/Hoops/Example 2/mission.xml b/Missions/Hoops/Example 2/mission.xml index 89e8f9e..f91462e 100644 --- a/Missions/Hoops/Example 2/mission.xml +++ b/Missions/Hoops/Example 2/mission.xml @@ -1,27 +1,6 @@ - - Test mission - - - Hoops / gates / checkpoints - - - - - Missions/Hoops-mission - + -9999 -9999 diff --git a/Nasal/extensions.nas b/Nasal/extensions.nas index 3ac03d3..1818501 100644 --- a/Nasal/extensions.nas +++ b/Nasal/extensions.nas @@ -1,4 +1,6 @@ +var objects = []; +var handlers = []; var extensions_load = func { @@ -10,18 +12,28 @@ var extensions_load = func { #load mission extension foreach(var script; mission_node.getChildren("include")) { globals["__mission"][k = "__" ~ i] = {}; - load_nasal(mission_root ~ "/extensions/" ~ script.getValue(), k); + _load_nasal(mission_root ~ "/extensions/" ~ script.getValue(), k); i += 1; } #load core extensions foreach(var script; extension_list()) { globals["__mission"][k = "__" ~ i] = {}; - load_nasal(getprop("/sim/mission/root_path") ~ "/Nasal/extensions/" ~ script, k); + _load_nasal(getprop("/sim/mission/root_path") ~ "/Nasal/extensions/" ~ script, k); i += 1; } } +var extensions_clear = func { + setsize(objects, 0); + + foreach (var h; handlers) { + if (hasmember(h, "stop")) { + h.stop(); + } + } + setsize(handlers, 0); +} var extension_add = func (type, h) { if (type == "MissionObject") { @@ -49,8 +61,29 @@ var extension_list = func { return v; } +var extensions_models_init = func() { + foreach (var h; handlers) { + if( hasmember(h, "init") ) { + h.init(); + } + } -var load_nasal = func(file, module) { # (copy-paste from io.nas) + foreach(var c; mission_node.getChildren("object")) { + foreach(var obj; objects) { + if (c.getValue("type") == obj.type) { + append(mission_objects, obj.new(c)); + } + } + } + + foreach(var obj; mission_objects) { + if( hasmember(obj, "init") ) { + obj.init(); + } + } +} + +var _load_nasal = func(file, module) { # (copy-paste from io.nas) var code = call(func compile(io.readfile(file), file), nil, var err = []); if (size(err)) { if (substr(err[0], 0, 12) == "Parse error:") { # hack around Nasal feature diff --git a/Nasal/mission.nas b/Nasal/mission.nas index dce3804..031a366 100644 --- a/Nasal/mission.nas +++ b/Nasal/mission.nas @@ -3,13 +3,10 @@ var mission_objects = []; var mission_started = 0; var mission_node = props.getNode("/sim/mission/data", 1); var mission_root = ""; -var objects = []; -var handlers = []; - var hasmember = view.hasmember; -var load_preferences = func() { +var preferences_load = func() { foreach (var a; directory(mission_root)) { if (a == "preferences.xml") { io.read_properties(mission_root ~ "/preferences.xml", props.getNode("")); @@ -28,64 +25,49 @@ var start_mission = func(name) { if (mission_root == "") { return; } - - load_preferences(); + # save mission root for repositioning (will reset addon) + setprop("/sim/mission/current_mission/path", mission_root); mission_node.removeAllChildren(); io.read_properties(mission_root ~ "/mission.xml", mission_node); + # set flightgear main state (location, weather) var presets = mission_node.getChild("presets"); -# causes FG reload and reinit all data -# if (presets != nil) { -# props.copy(presets, props.getNode("/sim/presets")); -# fgcommand("reposition"); -# } + if (presets != nil) { + props.copy(presets, props.getNode("/sim/presets")); + fgcommand("reposition"); + } var timeofday = mission_node.getChild("timeofday"); if (timeofday != nil) { fgcommand("timeofday", props.Node.new({ "timeofday" : timeofday.getValue() })); } - extensions_load(); - + # wait for simulator restart & proceed settimer (func _start_mission(), 0); } var _start_mission = func { + # wait for splash screen closing if (splash_screen()) { settimer(func _start_mission(), 2); return; } - foreach (var h; handlers) { - if( hasmember(h, "init") ) { - h.init(); - } - } + # reload data + mission_root = getprop("/sim/mission/current_mission/path"); + print("mission root: " ~ mission_root); - foreach(var c; mission_node.getChildren("object")) { - foreach(var obj; objects) { - if (c.getValue("type") == obj.type) { - append(mission_objects, obj.new(c)); - } - } - } + mission_node.removeAllChildren(); + io.read_properties(mission_root ~ "/mission.xml", mission_node); - foreach(var obj; mission_objects) { - if( hasmember(obj, "init") ) { - obj.init(); - } - } + preferences_load(); + extensions_load(); + extensions_models_init(); mission_started = 1; } -var splash_screen = func { - var s = getprop("sim/startup/splash-alpha"); - if (s == nil) s = 1; - return s > 0 ? 1 : 0; -} - var stop_mission = func { if (!mission_started) { return; @@ -94,17 +76,9 @@ var stop_mission = func { foreach(var obj; mission_objects) { obj.del(); } - setsize(mission_objects, 0); - setsize(objects, 0); - - foreach (var h; handlers) { - if (hasmember(h, "stop")) { - h.stop(); - } - } - setsize(handlers, 0); + extensions_clear(); delete(globals, "__mission"); mission_started = 0; @@ -127,14 +101,12 @@ var activate_object = func(name, start = 1) { } } - var activate_object_group = func(group, start = 1) { foreach(var ref; group.getChildren("object-reference")) { activate_object(ref.getValue(), start); } } - var get_coord = func(n) { geo.Coord.new().set_latlon ( var lat = n.getValue("latitude-deg") or 0, diff --git a/Nasal/utils.nas b/Nasal/utils.nas new file mode 100644 index 0000000..908a942 --- /dev/null +++ b/Nasal/utils.nas @@ -0,0 +1,6 @@ + +var splash_screen = func { + var s = getprop("sim/startup/splash-alpha"); + if (s == nil) s = 1; + return s > 0 ? 1 : 0; +} diff --git a/TODO b/TODO index d9941fe..1d8b0fd 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,15 @@ +bugs: - bug - presets does not work - reloading FG causes whole addon reload and lost of mission state + +tasks: - actions documentation - fully working all example missions + +features: +- weather - set METAR weather in mission.xml +- canvas map - dynamically extend layers with mission target layer - hoops - dynamic mission generator - tours - dynamic mission generator - skills - dynamic mission generator +- mission browser - map for mission targets +- ufo extension/mission for placing objects for new missions diff --git a/main.nas b/main.nas index 12ad1d2..57d26c8 100644 --- a/main.nas +++ b/main.nas @@ -12,7 +12,7 @@ var main = func( root ) { setprop("/sim/mission/root_path", root); # load scripts - foreach(var f; ['mission.nas', 'extensions.nas', 'persistence.nas', 'gui.nas'] ) { + foreach(var f; ['mission.nas', 'extensions.nas', 'persistence.nas', 'gui.nas', 'utils.nas'] ) { io.load_nasal( root ~ "/Nasal/" ~ f, "mission" ); } } \ No newline at end of file