From f8dc5cd7c72a50f34929b94cbf95b6a6723890ef Mon Sep 17 00:00:00 2001
From: Thorsten Renk <thorsten@science-and-fiction.org>
Date: Tue, 5 Sep 2017 10:18:55 +0300
Subject: [PATCH] Set up dialog to list nearby AI-objects which can be
 controlled, interface Truman with dialog

---
 Models/Geometry/Nimitz/truman.xml |   5 +-
 gui/dialogs/ai-objects.xml        | 107 ++++++++++++++++++++++++++++++
 gui/dialogs/truman.xml            |   2 +-
 gui/menubar.xml                   |  45 +++++++++++++
 4 files changed, 157 insertions(+), 2 deletions(-)
 create mode 100644 gui/dialogs/ai-objects.xml

diff --git a/Models/Geometry/Nimitz/truman.xml b/Models/Geometry/Nimitz/truman.xml
index 72db829d5..d4d2ebd08 100644
--- a/Models/Geometry/Nimitz/truman.xml
+++ b/Models/Geometry/Nimitz/truman.xml
@@ -23,6 +23,9 @@ by Thorsten Renk 2017
       var pathc = cmdarg().getPath();
       var self = cmdarg();
 
+      # register the dialog
+
+      setprop(pathc~"/dlg-name", "Truman");
 
       # times for components to move to their position
 
@@ -3899,7 +3902,7 @@ by Thorsten Renk 2017
       <repeatable>false</repeatable>
       <binding>
 	<command>dialog-show</command>
-	<dialog-name>TrumanControls</dialog-name>
+	<dialog-name>Truman</dialog-name>
 	<property></property>
       </binding>
     </action>
diff --git a/gui/dialogs/ai-objects.xml b/gui/dialogs/ai-objects.xml
new file mode 100644
index 000000000..2d7a4f022
--- /dev/null
+++ b/gui/dialogs/ai-objects.xml
@@ -0,0 +1,107 @@
+<?xml version="1.0"?>
+
+<PropertyList>
+	<name>ai-objects</name>
+	<layout>vbox</layout>
+	<default-padding>1</default-padding>
+	<modal>false</modal>
+	<width>400</width>
+
+	<empty>
+		<pref-height>6</pref-height>
+	</empty>
+
+	<text>
+		<label>Control nearby AI objects</label>
+	</text>
+
+	<empty>
+		<pref-height>4</pref-height>
+	</empty>
+
+	<hrule/>
+
+ <group>
+    <layout>table</layout>
+  
+    <text>
+      <label>Select an AI object to bring up its own control dialog</label>
+      <halign>left</halign>
+      <row>0</row>
+      <col>0</col>
+    </text>
+
+  	<combo>
+       		 <row>1</row>
+        	 <col>0</col>
+        	 <name>object-selection</name>
+        	 <halign>left</halign>
+        	 <stretch>true</stretch>
+        	 <pref-width>300</pref-width>
+        	 <property>/ai/control/object-selected</property>
+        	 <properties>/ai/control/objects-nearby/</properties>
+		 <value>Truman</value>
+        	 <binding>
+             	 	<command>dialog-apply</command>
+          	 	<object-name>object-selection</object-name>
+        	 </binding>
+      	</combo>
+
+
+
+</group>
+
+
+        <empty>
+		<pref-height>12</pref-height>
+	</empty>
+
+	<hrule/>
+
+	<empty>
+		<pref-height>20</pref-height>
+	</empty>
+
+ <group>
+    <layout>table</layout>
+
+	<button>
+       		<row>0</row>
+        	<col>0</col>
+		<legend>OK</legend>
+		<default>true</default>
+		<equal>true</equal>
+		<binding>
+			<command>dialog-apply</command>
+		</binding>
+
+		 <binding>
+			<command>nasal</command>
+			<script>
+			var obj_dlg_id = getprop("/ai/control/object-selected");
+
+			var dlg = gui.Dialog.new(obj_dlg_id);
+			dlg.open();
+					
+			</script>
+		</binding>
+
+		<binding>
+			<command>dialog-close</command>
+		</binding>
+	</button>
+
+	<button>
+       		<row>0</row>
+        	<col>1</col>
+		<legend>Close</legend>
+		<default>true</default>
+		<key>Esc</key>
+		<binding>
+			<command>dialog-close</command>
+		</binding>
+	</button>
+
+</group>
+
+</PropertyList>
diff --git a/gui/dialogs/truman.xml b/gui/dialogs/truman.xml
index 107b47c16..205064ba1 100644
--- a/gui/dialogs/truman.xml
+++ b/gui/dialogs/truman.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 
 <PropertyList>
-	<name>TrumanControls</name>
+	<name>Truman</name>
 	<modal>false</modal>
 	<layout>vbox</layout>
 
diff --git a/gui/menubar.xml b/gui/menubar.xml
index 9fc5c81d6..5c2baff09 100644
--- a/gui/menubar.xml
+++ b/gui/menubar.xml
@@ -492,6 +492,51 @@
 			</binding>
 		</item>
 
+		<item>
+			<label>AI Objects</label>
+			<name>ai-objects</name>
+			<binding>
+
+			<command>nasal</command>
+			<script>
+				<![CDATA[
+				var carriers = props.globals.getNode("/ai/models").getChildren("carrier");
+				var i = 0;
+				foreach(c; carriers)
+					{
+					var name = c.getNode("dlg-name",1).getValue();
+					if (name != nil) # check whether a dialog is defined at all
+						{
+						var aircraft_pos = geo.aircraft_position();
+					
+						var carrier_lat = c.getNode("position").getNode("latitude-deg").getValue();
+						var carrier_lon = c.getNode("position").getNode("longitude-deg").getValue();
+						var carrier_pos = geo.Coord.new();
+						carrier_pos.set_latlon(carrier_lat, carrier_lon);
+
+						var distance = aircraft_pos.distance_to(carrier_pos);
+	
+						if (distance < 50000.0) # only add nearby objects
+							{
+							setprop("/ai/control/objects-nearby/value["~i~"]", name);			
+							if (i==0)
+								{setprop("/ai/control/object-selected", name);}
+
+							i=i+1;
+							}
+						}
+
+					}
+
+			    	]]>
+			</script>
+			</binding>
+			<binding>
+				<command>dialog-show</command>
+				<dialog-name>ai-objects</dialog-name>
+			</binding>
+		</item>
+
 	</menu>
 
 	<menu>