2008-06-11 18:23:03 +00:00
|
|
|
# Multiplayer
|
|
|
|
# ===========
|
2006-10-10 05:23:38 +00:00
|
|
|
#
|
|
|
|
# 1) Display chat messages from other aircraft to
|
|
|
|
# the screen using screen.nas
|
|
|
|
#
|
|
|
|
# 2) Display a complete history of chat via dialog.
|
|
|
|
#
|
|
|
|
# 3) Allow chat messages to be written by the user.
|
2010-06-27 22:23:56 +00:00
|
|
|
|
|
|
|
var lastmsg = {};
|
|
|
|
var ignore = {};
|
2011-04-02 09:41:32 +00:00
|
|
|
var msg_loop_id = 0;
|
|
|
|
var msg_timeout = 0;
|
2012-01-04 22:54:21 +00:00
|
|
|
var log_file = nil;
|
2012-01-22 11:42:57 +00:00
|
|
|
var log_listeners = [];
|
2009-02-15 17:50:13 +00:00
|
|
|
|
2011-04-02 09:41:32 +00:00
|
|
|
var check_messages = func(loop_id) {
|
|
|
|
if (loop_id != msg_loop_id) return;
|
2009-02-15 15:48:38 +00:00
|
|
|
foreach (var mp; values(model.callsign)) {
|
2009-02-15 17:50:13 +00:00
|
|
|
var msg = mp.node.getNode("sim/multiplay/chat", 1).getValue();
|
2009-02-16 15:38:47 +00:00
|
|
|
if (msg and msg != lastmsg[mp.callsign]) {
|
2010-06-27 22:23:56 +00:00
|
|
|
if (!contains(ignore, mp.callsign))
|
2009-10-11 18:40:55 +00:00
|
|
|
echo_message(mp.callsign, msg);
|
2009-02-16 15:38:47 +00:00
|
|
|
lastmsg[mp.callsign] = msg;
|
|
|
|
}
|
2009-02-15 15:48:38 +00:00
|
|
|
}
|
2011-04-02 09:41:32 +00:00
|
|
|
settimer(func check_messages(loop_id), 1);
|
2009-02-15 15:48:38 +00:00
|
|
|
}
|
2008-05-03 19:45:22 +00:00
|
|
|
|
2009-02-15 15:48:38 +00:00
|
|
|
var echo_message = func(callsign, msg) {
|
2009-02-16 15:38:47 +00:00
|
|
|
msg = string.trim(string.replace(msg, "\n", " "));
|
|
|
|
|
2009-02-15 15:48:38 +00:00
|
|
|
# Only prefix with the callsign if the message doesn't already include it.
|
|
|
|
if (find(callsign, msg) < 0)
|
|
|
|
msg = callsign ~ ": " ~ msg;
|
2009-01-27 16:55:46 +00:00
|
|
|
|
2009-02-15 15:48:38 +00:00
|
|
|
setprop("/sim/messages/mp-plane", msg);
|
2009-01-27 16:55:46 +00:00
|
|
|
|
2009-02-15 15:48:38 +00:00
|
|
|
# Add the chat to the chat history.
|
2009-02-16 15:38:47 +00:00
|
|
|
if (var history = getprop("/sim/multiplay/chat-history"))
|
2009-02-15 15:48:38 +00:00
|
|
|
msg = history ~ "\n" ~ msg;
|
2007-12-08 19:01:03 +00:00
|
|
|
|
2009-02-15 15:48:38 +00:00
|
|
|
setprop("/sim/multiplay/chat-history", msg);
|
2006-10-10 05:23:38 +00:00
|
|
|
}
|
|
|
|
|
2011-04-02 09:41:32 +00:00
|
|
|
var timeout_handler = func()
|
|
|
|
{
|
|
|
|
var t = props.globals.getNode("/sim/time/elapsed-sec").getValue();
|
|
|
|
if (t >= msg_timeout)
|
|
|
|
{
|
|
|
|
msg_timeout = 0;
|
|
|
|
setprop("/sim/multiplay/chat", "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
settimer(timeout_handler, msg_timeout - t);
|
|
|
|
}
|
|
|
|
|
|
|
|
var chat_listener = func(n)
|
|
|
|
{
|
|
|
|
var msg = n.getValue();
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
# ensure we see our own messages.
|
|
|
|
echo_message(getprop("/sim/multiplay/callsign"), msg);
|
2008-05-03 19:45:22 +00:00
|
|
|
|
2011-04-02 09:41:32 +00:00
|
|
|
# set expiry time
|
|
|
|
if (msg_timeout == 0)
|
|
|
|
settimer(timeout_handler, 10); # need new timer
|
|
|
|
msg_timeout = 10 + props.globals.getNode("/sim/time/elapsed-sec").getValue();
|
|
|
|
}
|
|
|
|
}
|
2006-10-10 05:23:38 +00:00
|
|
|
|
|
|
|
|
2009-02-15 15:48:38 +00:00
|
|
|
|
2007-12-08 19:01:03 +00:00
|
|
|
# Message composition function, activated using the - key.
|
|
|
|
var prefix = "Chat Message:";
|
|
|
|
var input = "";
|
|
|
|
var kbdlistener = nil;
|
|
|
|
|
2008-05-03 19:45:22 +00:00
|
|
|
var compose_message = func(msg = "")
|
2007-12-08 19:01:03 +00:00
|
|
|
{
|
2008-05-03 19:45:22 +00:00
|
|
|
input = prefix ~ msg;
|
|
|
|
gui.popupTip(input, 1000000);
|
2007-12-08 19:01:03 +00:00
|
|
|
|
2008-05-03 19:45:22 +00:00
|
|
|
kbdlistener = setlistener("/devices/status/keyboard/event", func (event) {
|
|
|
|
var key = event.getNode("key");
|
2007-12-08 19:01:03 +00:00
|
|
|
|
2008-05-03 19:45:22 +00:00
|
|
|
# Only check the key when pressed.
|
|
|
|
if (!event.getNode("pressed").getValue())
|
|
|
|
return;
|
2006-10-10 05:23:38 +00:00
|
|
|
|
2008-05-03 19:45:22 +00:00
|
|
|
if (handle_key(key.getValue()))
|
2008-05-03 21:29:38 +00:00
|
|
|
key.setValue(-1); # drop key event
|
2008-05-03 19:45:22 +00:00
|
|
|
});
|
2007-12-08 19:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var handle_key = func(key)
|
|
|
|
{
|
2008-05-03 19:45:22 +00:00
|
|
|
if (key == `\n` or key == `\r`)
|
|
|
|
{
|
|
|
|
# CR/LF -> send the message
|
|
|
|
|
|
|
|
# Trim off the prefix
|
|
|
|
input = substr(input, size(prefix));
|
|
|
|
# Send the message and switch off the listener.
|
|
|
|
setprop("/sim/multiplay/chat", input);
|
|
|
|
removelistener(kbdlistener);
|
|
|
|
gui.popdown();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif (key == 8)
|
|
|
|
{
|
|
|
|
# backspace -> remove a character
|
|
|
|
|
|
|
|
if (size(input) > size(prefix))
|
|
|
|
{
|
|
|
|
input = substr(input, 0, size(input) - 1);
|
|
|
|
gui.popupTip(input, 1000000);
|
|
|
|
}
|
2010-09-27 20:13:30 +00:00
|
|
|
|
|
|
|
# Always handle key so excessive backspacing doesn't toggle the heading autopilot
|
|
|
|
return 1;
|
2008-05-03 19:45:22 +00:00
|
|
|
}
|
|
|
|
elsif (key == 27)
|
|
|
|
{
|
|
|
|
# escape -> cancel
|
|
|
|
removelistener(kbdlistener);
|
|
|
|
gui.popdown();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
elsif ((key > 31) and (key < 128))
|
|
|
|
{
|
|
|
|
# Normal character - add it to the input
|
|
|
|
input ~= chr(key);
|
|
|
|
gui.popupTip(input, 1000000);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Unknown character - pass through
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-08 19:01:03 +00:00
|
|
|
}
|
2008-06-11 18:23:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-06 12:03:31 +00:00
|
|
|
# multiplayer.dialog.show() -- displays pilot list dialog
|
2008-06-11 18:23:03 +00:00
|
|
|
#
|
2009-01-31 17:15:48 +00:00
|
|
|
var PILOTSDLG_RUNNING = 0;
|
2008-06-11 18:23:03 +00:00
|
|
|
|
|
|
|
var dialog = {
|
2009-01-31 17:15:48 +00:00
|
|
|
init: func(x = nil, y = nil) {
|
2008-06-11 18:23:03 +00:00
|
|
|
me.x = x;
|
|
|
|
me.y = y;
|
|
|
|
me.bg = [0, 0, 0, 0.3]; # background color
|
2009-02-06 12:03:31 +00:00
|
|
|
me.fg = [[0.9, 0.9, 0.2, 1], [1, 1, 1, 1], [1, 0.5, 0, 1]]; # alternative active & disabled color
|
2009-01-31 17:15:48 +00:00
|
|
|
me.unit = 1;
|
2008-06-11 18:23:03 +00:00
|
|
|
me.toggle_unit(); # set to imperial
|
|
|
|
#
|
|
|
|
# "private"
|
2009-01-31 17:15:48 +00:00
|
|
|
var font = { name: "FIXED_8x13" };
|
2013-05-14 16:13:11 +00:00
|
|
|
me.header = ["chat", " callsign"," code"," model", " brg", func dialog.dist_hdr, func dialog.alt_hdr ~ " ", "ignore" ~ " "];
|
2008-06-11 18:23:03 +00:00
|
|
|
me.columns = [
|
2012-06-16 09:36:22 +00:00
|
|
|
{ type: "button", legend: "", halign: "right", callback: "multiplayer.compose_message", "pref-height": 14, "pref-width": 14},
|
2009-10-11 18:40:55 +00:00
|
|
|
{ type: "text", property: "callsign", format: " %s", label: "-----------", halign: "fill" },
|
2013-05-14 16:13:11 +00:00
|
|
|
{ type: "text", property: "id-code", format: " %s", label: "-----", halign: "fill" },
|
2009-10-11 18:40:55 +00:00
|
|
|
{ type: "text", property: "model-short", format: "%s", label: "--------------", halign: "fill" },
|
|
|
|
{ type: "text", property: "bearing-to", format: " %3.0f", label: "----", halign: "right", font: font },
|
|
|
|
{ type: "text", property: func dialog.dist_node, format:" %8.2f", label: "---------", halign: "right", font: font },
|
|
|
|
{ type: "text", property: func dialog.alt_node, format:" %7.0f", label: "---------", halign: "right", font: font },
|
2010-05-21 21:09:38 +00:00
|
|
|
{ type: "checkbox", property: "controls/invisible", callback: "multiplayer.dialog.toggle_ignore",
|
2009-10-11 18:40:55 +00:00
|
|
|
argprop: "callsign", label: "---------", halign: "right", font: font },
|
2008-06-11 18:23:03 +00:00
|
|
|
];
|
2010-10-16 16:31:19 +00:00
|
|
|
me.cs_warnings = {};
|
2008-06-11 18:23:03 +00:00
|
|
|
me.name = "who-is-online";
|
|
|
|
me.dialog = nil;
|
2009-01-31 21:53:54 +00:00
|
|
|
me.loopid = 0;
|
2008-06-11 18:23:03 +00:00
|
|
|
|
|
|
|
me.listeners=[];
|
2009-01-31 17:15:48 +00:00
|
|
|
append(me.listeners, setlistener("/sim/startup/xsize", func me._redraw_()));
|
|
|
|
append(me.listeners, setlistener("/sim/startup/ysize", func me._redraw_()));
|
2009-01-31 21:22:18 +00:00
|
|
|
append(me.listeners, setlistener("/sim/signals/reinit-gui", func me._redraw_()));
|
2009-01-31 21:53:54 +00:00
|
|
|
append(me.listeners, setlistener("/sim/signals/multiplayer-updated", func me._redraw_()));
|
2008-06-11 18:23:03 +00:00
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
create: func {
|
2008-06-11 18:23:03 +00:00
|
|
|
if (me.dialog != nil)
|
|
|
|
me.close();
|
|
|
|
|
2009-02-06 12:03:31 +00:00
|
|
|
me.dialog = gui.dialog[me.name] = gui.Widget.new();
|
2008-06-11 18:23:03 +00:00
|
|
|
me.dialog.set("name", me.name);
|
2009-01-31 17:15:48 +00:00
|
|
|
me.dialog.set("dialog-name", me.name);
|
2008-06-11 18:23:03 +00:00
|
|
|
if (me.x != nil)
|
|
|
|
me.dialog.set("x", me.x);
|
|
|
|
if (me.y != nil)
|
|
|
|
me.dialog.set("y", me.y);
|
|
|
|
|
|
|
|
me.dialog.set("layout", "vbox");
|
|
|
|
me.dialog.set("default-padding", 0);
|
|
|
|
|
|
|
|
me.dialog.setColor(me.bg[0], me.bg[1], me.bg[2], me.bg[3]);
|
|
|
|
|
2009-01-31 17:15:48 +00:00
|
|
|
var titlebar = me.dialog.addChild("group");
|
2008-06-11 18:23:03 +00:00
|
|
|
titlebar.set("layout", "hbox");
|
|
|
|
|
|
|
|
var w = titlebar.addChild("button");
|
2009-02-06 12:03:31 +00:00
|
|
|
w.node.setValues({ "pref-width": 16, "pref-height": 16, legend: me.unit_button, default: 0 });
|
2008-06-11 18:23:03 +00:00
|
|
|
w.setBinding("nasal", "multiplayer.dialog.toggle_unit(); multiplayer.dialog._redraw_()");
|
|
|
|
|
|
|
|
titlebar.addChild("empty").set("stretch", 1);
|
|
|
|
titlebar.addChild("text").set("label", "Pilots: ");
|
|
|
|
|
2009-01-31 17:15:48 +00:00
|
|
|
var p = titlebar.addChild("text");
|
2009-02-06 12:03:31 +00:00
|
|
|
p.node.setValues({ label: "---", live: 1, format: "%d", property: "ai/models/num-players" });
|
2008-06-11 18:23:03 +00:00
|
|
|
titlebar.addChild("empty").set("stretch", 1);
|
|
|
|
|
|
|
|
var w = titlebar.addChild("button");
|
2009-02-06 12:03:31 +00:00
|
|
|
w.node.setValues({ "pref-width": 16, "pref-height": 16, legend: "", default: 0 });
|
2013-12-01 03:18:31 +00:00
|
|
|
# "Esc" causes dialog-close
|
|
|
|
w.set("key", "Esc");
|
2008-06-11 18:23:03 +00:00
|
|
|
w.setBinding("nasal", "multiplayer.dialog.del()");
|
|
|
|
|
|
|
|
me.dialog.addChild("hrule");
|
|
|
|
|
2009-01-31 17:15:48 +00:00
|
|
|
var content = me.dialog.addChild("group");
|
2008-06-11 18:23:03 +00:00
|
|
|
content.set("layout", "table");
|
|
|
|
content.set("default-padding", 0);
|
|
|
|
|
2009-01-31 17:15:48 +00:00
|
|
|
var row = 0;
|
|
|
|
var col = 0;
|
2008-06-11 18:23:03 +00:00
|
|
|
foreach (var h; me.header) {
|
|
|
|
var w = content.addChild("text");
|
|
|
|
var l = typeof(h) == "func" ? h() : h;
|
2009-01-31 17:15:48 +00:00
|
|
|
w.node.setValues({ "label": l, "row": row, "col": col, halign: me.columns[col].halign });
|
|
|
|
w = content.addChild("hrule");
|
2009-02-06 12:03:31 +00:00
|
|
|
w.node.setValues({ "row": row + 1, "col": col });
|
2009-01-31 17:15:48 +00:00
|
|
|
col += 1;
|
2008-06-11 18:23:03 +00:00
|
|
|
}
|
2009-01-31 17:15:48 +00:00
|
|
|
row += 2;
|
2009-02-06 12:03:31 +00:00
|
|
|
var odd = 1;
|
2009-01-31 17:15:48 +00:00
|
|
|
foreach (var mp; model.list) {
|
|
|
|
var col = 0;
|
2010-11-13 16:38:37 +00:00
|
|
|
var color = mp.node.getNode("model-installed").getValue() ? me.fg[odd = !odd] : me.fg[2];
|
2008-06-11 18:23:03 +00:00
|
|
|
foreach (var column; me.columns) {
|
2009-10-11 18:40:55 +00:00
|
|
|
var w = nil;
|
2013-05-14 16:13:11 +00:00
|
|
|
if (column.type == "button") {
|
|
|
|
w = content.addChild("button");
|
|
|
|
w.node.setValues(column);
|
|
|
|
w.setBinding("nasal", column.callback ~ "(\"" ~ mp.callsign ~ ", \");");
|
2012-06-16 09:36:22 +00:00
|
|
|
w.node.setValues({ row: row, col: col});
|
2013-05-14 16:13:11 +00:00
|
|
|
} else {
|
|
|
|
var p = typeof(column.property) == "func" ? column.property() : column.property;
|
|
|
|
if (column.type == "text") {
|
|
|
|
w = content.addChild("text");
|
|
|
|
w.node.setValues(column);
|
|
|
|
} elsif (column.type == "checkbox") {
|
|
|
|
w = content.addChild("checkbox");
|
|
|
|
w.setBinding("nasal", column.callback ~ "(getprop(\"" ~ mp.root ~ "/" ~ column.argprop ~ "\"))");
|
|
|
|
}
|
2012-06-16 09:36:22 +00:00
|
|
|
w.node.setValues({ row: row, col: col, live: 1, property: mp.root ~ "/" ~ p });
|
2013-05-14 16:13:11 +00:00
|
|
|
}
|
2009-02-06 12:03:31 +00:00
|
|
|
w.setColor(color[0], color[1], color[2], color[3]);
|
2009-01-31 17:15:48 +00:00
|
|
|
col += 1;
|
2008-06-11 18:23:03 +00:00
|
|
|
}
|
2009-02-06 12:03:31 +00:00
|
|
|
row += 1;
|
2008-06-11 18:23:03 +00:00
|
|
|
}
|
2010-11-13 16:38:37 +00:00
|
|
|
if (me.x != nil)
|
|
|
|
me.dialog.set("x", me.x);
|
|
|
|
if (me.y != nil)
|
|
|
|
me.dialog.set("y", me.y);
|
2009-01-31 21:53:54 +00:00
|
|
|
me.update(me.loopid += 1);
|
2008-06-11 18:23:03 +00:00
|
|
|
fgcommand("dialog-new", me.dialog.prop());
|
2009-01-31 17:15:48 +00:00
|
|
|
fgcommand("dialog-show", me.dialog.prop());
|
2008-06-11 18:23:03 +00:00
|
|
|
},
|
2009-01-31 21:53:54 +00:00
|
|
|
update: func(id) {
|
|
|
|
id == me.loopid or return;
|
2009-01-31 17:15:48 +00:00
|
|
|
var self = geo.aircraft_position();
|
2009-01-31 21:22:18 +00:00
|
|
|
foreach (var mp; model.list) {
|
2009-01-31 17:15:48 +00:00
|
|
|
var n = mp.node;
|
2009-02-15 15:48:38 +00:00
|
|
|
var x = n.getNode("position/global-x").getValue();
|
|
|
|
var y = n.getNode("position/global-y").getValue();
|
|
|
|
var z = n.getNode("position/global-z").getValue();
|
|
|
|
var ac = geo.Coord.new().set_xyz(x, y, z);
|
2009-02-04 21:47:19 +00:00
|
|
|
var distance = nil;
|
2013-05-14 16:13:11 +00:00
|
|
|
var idcode = "----";
|
|
|
|
idcode = me.IDCode(n.getNode("instrumentation/transponder/transmitted-id").getValue());
|
|
|
|
|
2009-02-04 21:47:19 +00:00
|
|
|
call(func distance = self.distance_to(ac), nil, var err = []);
|
2013-05-14 16:13:11 +00:00
|
|
|
|
2010-10-16 16:31:19 +00:00
|
|
|
if ((size(err))or(distance==nil)) {
|
|
|
|
# Oops, have errors. Bogus position data (and distance==nil).
|
|
|
|
if (me.cs_warnings[mp.callsign]!=1) {
|
|
|
|
# report each callsign once only (avoid cluttering)
|
|
|
|
me.cs_warnings[mp.callsign] = 1;
|
|
|
|
print("Received invalid position data: " ~ debug._error(mp.callsign));
|
|
|
|
}
|
|
|
|
# debug.printerror(err);
|
|
|
|
# debug.dump(self, ac, mp);
|
|
|
|
# debug.tree(mp.node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# Node with valid position data (and "distance!=nil").
|
|
|
|
n.setValues({
|
2010-11-13 16:38:37 +00:00
|
|
|
"model-short": n.getNode("model-installed").getValue() ? mp.model : "[" ~ mp.model ~ "]",
|
2010-10-16 16:31:19 +00:00
|
|
|
"bearing-to": self.course_to(ac),
|
|
|
|
"distance-to-km": distance / 1000.0,
|
|
|
|
"distance-to-nm": distance * M2NM,
|
|
|
|
"position/altitude-m": n.getNode("position/altitude-ft").getValue() * FT2M,
|
|
|
|
"controls/invisible": contains(ignore, mp.callsign),
|
2013-05-14 16:13:11 +00:00
|
|
|
"id-code": idcode
|
2010-10-16 16:31:19 +00:00
|
|
|
});
|
2009-02-06 12:03:31 +00:00
|
|
|
}
|
2008-06-11 18:23:03 +00:00
|
|
|
}
|
2009-01-31 17:15:48 +00:00
|
|
|
if (PILOTSDLG_RUNNING)
|
2009-01-31 21:53:54 +00:00
|
|
|
settimer(func me.update(id), 1, 1);
|
2008-06-11 18:23:03 +00:00
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
_redraw_: func {
|
2008-06-11 18:23:03 +00:00
|
|
|
if (me.dialog != nil) {
|
|
|
|
me.close();
|
|
|
|
me.create();
|
|
|
|
}
|
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
toggle_unit: func {
|
2008-06-11 18:23:03 +00:00
|
|
|
me.unit = !me.unit;
|
2009-01-31 21:22:18 +00:00
|
|
|
if (me.unit) {
|
2008-06-11 18:23:03 +00:00
|
|
|
me.alt_node = "position/altitude-m";
|
|
|
|
me.alt_hdr = "alt-m";
|
|
|
|
me.dist_hdr = "dist-km";
|
|
|
|
me.dist_node = "distance-to-km";
|
|
|
|
me.unit_button = "IM";
|
2009-01-31 21:22:18 +00:00
|
|
|
} else {
|
2008-06-11 18:23:03 +00:00
|
|
|
me.alt_node = "position/altitude-ft";
|
|
|
|
me.dist_node = "distance-to-nm";
|
|
|
|
me.alt_hdr = "alt-ft";
|
|
|
|
me.dist_hdr = "dist-nm";
|
|
|
|
me.unit_button = "SI";
|
|
|
|
}
|
|
|
|
},
|
2009-10-11 18:40:55 +00:00
|
|
|
toggle_ignore: func (callsign) {
|
2010-06-27 22:23:56 +00:00
|
|
|
if (contains(ignore, callsign)) {
|
|
|
|
delete(ignore, callsign);
|
2009-10-11 18:40:55 +00:00
|
|
|
} else {
|
2010-06-27 22:23:56 +00:00
|
|
|
ignore[callsign] = 1;
|
2009-10-11 18:40:55 +00:00
|
|
|
}
|
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
close: func {
|
2010-11-13 16:38:37 +00:00
|
|
|
if (me.dialog != nil) {
|
|
|
|
me.x = me.dialog.prop().getNode("x").getValue();
|
|
|
|
me.y = me.dialog.prop().getNode("y").getValue();
|
|
|
|
}
|
2009-01-31 17:15:48 +00:00
|
|
|
fgcommand("dialog-close", me.dialog.prop());
|
2008-06-11 18:23:03 +00:00
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
del: func {
|
2009-02-06 12:03:31 +00:00
|
|
|
PILOTSDLG_RUNNING = 0;
|
2008-06-11 18:23:03 +00:00
|
|
|
me.close();
|
2009-01-31 17:15:48 +00:00
|
|
|
foreach (var l; me.listeners)
|
2008-06-11 18:23:03 +00:00
|
|
|
removelistener(l);
|
2009-02-06 12:03:31 +00:00
|
|
|
delete(gui.dialog, me.name);
|
2008-06-11 18:23:03 +00:00
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
show: func {
|
|
|
|
if (!PILOTSDLG_RUNNING) {
|
2009-02-06 12:03:31 +00:00
|
|
|
PILOTSDLG_RUNNING = 1;
|
2008-06-11 18:23:03 +00:00
|
|
|
me.init(-2, -2);
|
|
|
|
me.create();
|
2009-02-01 01:58:15 +00:00
|
|
|
me.update(me.loopid += 1);
|
2008-06-11 18:23:03 +00:00
|
|
|
}
|
|
|
|
},
|
2009-01-31 17:15:48 +00:00
|
|
|
toggle: func {
|
|
|
|
if (!PILOTSDLG_RUNNING)
|
2008-06-11 18:23:03 +00:00
|
|
|
me.show();
|
|
|
|
else
|
|
|
|
me.del();
|
2009-01-31 17:15:48 +00:00
|
|
|
},
|
2013-05-14 16:13:11 +00:00
|
|
|
IDCode: func(code){
|
|
|
|
|
|
|
|
var idcode= "----";
|
|
|
|
|
|
|
|
if (code != nil )
|
|
|
|
{
|
|
|
|
if (code < 0)
|
|
|
|
{
|
|
|
|
idcode = "----";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
idcode = sprintf("%04d", code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idcode;
|
|
|
|
},
|
2008-06-11 18:23:03 +00:00
|
|
|
};
|
|
|
|
|
2009-01-31 17:15:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Autonomous singleton class that monitors multiplayer aircraft,
|
2009-02-04 21:47:19 +00:00
|
|
|
# maintains data in various structures, and raises signal
|
|
|
|
# "/sim/signals/multiplayer-updated" whenever an aircraft
|
|
|
|
# joined or left. Available data containers are:
|
|
|
|
#
|
2013-05-14 16:13:11 +00:00
|
|
|
# multiplayer.model.data: hash, key := /ai/models/~ path
|
2009-02-04 21:47:19 +00:00
|
|
|
# multiplayer.model.callsign hash, key := callsign
|
|
|
|
# multiplayer.model.list vector, sorted alphabetically (ASCII, case insensitive)
|
|
|
|
#
|
|
|
|
# All of them contain hash entries of this form:
|
2009-01-31 17:15:48 +00:00
|
|
|
#
|
|
|
|
# {
|
2009-02-04 21:47:19 +00:00
|
|
|
# callsign: "BiMaus",
|
|
|
|
# path: "Aircraft/bo105/Models/bo105.xml", # relative file path
|
|
|
|
# root: "/ai/models/multiplayer[4]", # root property
|
|
|
|
# node: {...}, # root property as props.Node hash
|
|
|
|
# model: "bo105", # model name (extracted from path)
|
|
|
|
# sort: "bimaus", # callsign in lower case (for sorting)
|
2009-01-31 17:15:48 +00:00
|
|
|
# }
|
|
|
|
#
|
|
|
|
var model = {
|
|
|
|
init: func {
|
|
|
|
me.L = [];
|
2009-10-11 11:57:58 +00:00
|
|
|
append(me.L, setlistener("ai/models/model-added", func(n) {
|
|
|
|
# Defer update() to the next convenient time to allow the
|
|
|
|
# new MP entry to become fully initialized.
|
|
|
|
settimer(func me.update(n.getValue()), 0);
|
|
|
|
}));
|
|
|
|
append(me.L, setlistener("ai/models/model-removed", func(n) {
|
|
|
|
# Defer update() to the next convenient time to allow the
|
|
|
|
# old MP entry to become fully deactivated.
|
|
|
|
settimer(func me.update(n.getValue()), 0);
|
|
|
|
}));
|
2009-01-31 17:15:48 +00:00
|
|
|
me.update();
|
|
|
|
},
|
|
|
|
update: func(n = nil) {
|
2009-09-05 20:22:14 +00:00
|
|
|
var changedNode = props.globals.getNode( n, 1 );
|
|
|
|
if (n != nil and changedNode.getName() != "multiplayer")
|
2009-01-31 17:15:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
me.data = {};
|
2009-02-03 16:15:37 +00:00
|
|
|
me.callsign = {};
|
2009-02-04 21:47:19 +00:00
|
|
|
|
2009-01-31 17:15:48 +00:00
|
|
|
foreach (var n; props.globals.getNode("ai/models", 1).getChildren("multiplayer")) {
|
2012-09-16 12:00:40 +00:00
|
|
|
if ((var valid = n.getNode("valid")) == nil or (!valid.getValue()))
|
2009-01-31 17:15:48 +00:00
|
|
|
continue;
|
|
|
|
if ((var callsign = n.getNode("callsign")) == nil or !(callsign = callsign.getValue()))
|
|
|
|
continue;
|
2009-02-16 15:38:47 +00:00
|
|
|
if (!(callsign = string.trim(callsign)))
|
|
|
|
continue;
|
2009-01-31 17:15:48 +00:00
|
|
|
|
2009-02-04 21:47:19 +00:00
|
|
|
var path = n.getNode("sim/model/path").getValue();
|
|
|
|
var model = split(".", split("/", path)[-1])[0];
|
2009-01-31 17:15:48 +00:00
|
|
|
model = me.remove_suffix(model, "-model");
|
|
|
|
model = me.remove_suffix(model, "-anim");
|
|
|
|
|
2009-02-04 21:47:19 +00:00
|
|
|
var root = n.getPath();
|
|
|
|
var data = { node: n, callsign: callsign, model: model, root: root,
|
2010-11-13 16:38:37 +00:00
|
|
|
sort: string.lc(callsign) };
|
2009-02-03 16:15:37 +00:00
|
|
|
|
2009-02-04 21:47:19 +00:00
|
|
|
me.data[root] = data;
|
2009-02-03 16:15:37 +00:00
|
|
|
me.callsign[callsign] = data;
|
2009-01-31 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
2009-02-16 23:35:54 +00:00
|
|
|
me.list = sort(values(me.data), func(a, b) cmp(a.sort, b.sort));
|
2009-01-31 17:15:48 +00:00
|
|
|
|
|
|
|
setprop("ai/models/num-players", size(me.list));
|
|
|
|
setprop("sim/signals/multiplayer-updated", 1);
|
|
|
|
},
|
|
|
|
remove_suffix: func(s, x) {
|
|
|
|
var len = size(x);
|
|
|
|
if (substr(s, -len) == x)
|
|
|
|
return substr(s, 0, size(s) - len);
|
|
|
|
return s;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-01-07 15:17:02 +00:00
|
|
|
var mp_mode_changed = func(n) {
|
|
|
|
var is_online = n.getBoolValue();
|
|
|
|
foreach (var menuitem;["mp-chat","mp-chat-menu","mp-list","mp-carrier"])
|
|
|
|
{
|
|
|
|
gui.menuEnable(menuitem, is_online);
|
|
|
|
}
|
2012-01-04 22:54:21 +00:00
|
|
|
|
2012-01-07 15:17:02 +00:00
|
|
|
if (is_online) {
|
2012-01-22 11:42:57 +00:00
|
|
|
if (getprop("/sim/multiplay/write-message-log") and (log_file == nil)) {
|
|
|
|
var t = props.globals.getNode("/sim/time/real");
|
|
|
|
if (t == nil)
|
|
|
|
{
|
|
|
|
# not ready yet, delay...
|
|
|
|
settimer(func mp_mode_changed(n), 0.1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t = t.getValues();
|
|
|
|
var ac = getprop("/sim/aircraft");
|
|
|
|
var cs = getprop("/sim/multiplay/callsign");
|
|
|
|
var apt = airportinfo().id;
|
2012-01-04 22:54:21 +00:00
|
|
|
var file = string.normpath(getprop("/sim/fg-home") ~ "/mp-message.log");
|
|
|
|
|
|
|
|
log_file = io.open(file, "a");
|
|
|
|
io.write(log_file, sprintf("\n===== %s %04d/%02d/%02d\t%s\t%s\t%s\n",
|
|
|
|
["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"][t.weekday],
|
|
|
|
t.year, t.month, t.day, apt, ac, cs));
|
2012-01-22 11:42:57 +00:00
|
|
|
io.flush(log_file);
|
2012-01-04 22:54:21 +00:00
|
|
|
|
2012-01-22 11:42:57 +00:00
|
|
|
append(log_listeners, setlistener("/sim/signals/exit", func io.write(log_file, "===== EXIT\n") and io.close(log_file)));
|
|
|
|
append(log_listeners, setlistener("/sim/messages/mp-plane", func(n) {
|
2012-01-04 22:54:21 +00:00
|
|
|
io.write(log_file, sprintf("%02d:%02d %s\n",
|
|
|
|
getprop("/sim/time/real/hour"),
|
|
|
|
getprop("/sim/time/real/minute"),
|
|
|
|
n.getValue()));
|
|
|
|
io.flush(log_file);
|
2012-01-22 11:42:57 +00:00
|
|
|
}));
|
2012-01-04 22:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
check_messages(msg_loop_id += 1);
|
|
|
|
}
|
2012-01-07 15:17:02 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
# stop message loop
|
|
|
|
msg_loop_id += 1;
|
2012-01-22 11:42:57 +00:00
|
|
|
if (log_file != nil)
|
|
|
|
{
|
|
|
|
io.write(log_file, "===== DISCONNECT\n");
|
|
|
|
io.flush(log_file);
|
|
|
|
io.close(log_file);
|
|
|
|
foreach (var l; log_listeners)
|
|
|
|
removelistener(l);
|
|
|
|
log_listeners = [];
|
|
|
|
log_file = nil;
|
|
|
|
}
|
2012-01-07 15:17:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setlistener("/sim/signals/nasal-dir-initialized", func {
|
|
|
|
|
|
|
|
model.init();
|
|
|
|
|
2012-01-22 11:42:57 +00:00
|
|
|
setlistener("/sim/multiplay/online", mp_mode_changed, 1, 1);
|
2009-01-31 17:15:48 +00:00
|
|
|
|
2012-01-04 22:54:21 +00:00
|
|
|
# Call-back to ensure we see our own messages.
|
|
|
|
setlistener("/sim/multiplay/chat", chat_listener);
|
|
|
|
});
|
2009-01-31 17:15:48 +00:00
|
|
|
|