2011-09-30 22:29:23 +00:00
|
|
|
###############################################################################
|
|
|
|
##
|
|
|
|
## Support tools for multiplayer enabled scenery objects.
|
|
|
|
##
|
2013-02-03 18:23:53 +00:00
|
|
|
## Copyright (C) 2011 - 2013 Anders Gidenstam (anders(at)gidenstam.org)
|
2011-09-30 22:29:23 +00:00
|
|
|
## This file is licensed under the GPL license version 2 or later.
|
|
|
|
##
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
# The event channel for scenery objects.
|
|
|
|
# See mp_broadcast.EventChannel for documentation.
|
|
|
|
var events = nil;
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# An extended aircraft.door that transmits the door events over MP using the
|
|
|
|
# scenery.events channel.
|
|
|
|
# Use only for single instance objects (e.g. static scenery objects).
|
|
|
|
#
|
|
|
|
# Note: Currently toggle() is the only shared event.
|
|
|
|
var sharedDoor = {
|
|
|
|
new: func(node, swingtime, pos = 0) {
|
|
|
|
var obj = aircraft.door.new(node, swingtime, pos);
|
|
|
|
obj.parents = [sharedDoor] ~ obj.parents;
|
|
|
|
obj.event_hash = mp_broadcast.Binary.stringHash
|
|
|
|
(isa(node, props.Node) ? node.getPath() : node);
|
2011-10-07 21:36:21 +00:00
|
|
|
obj.clock = mp_broadcast.LamportClock.new();
|
|
|
|
obj.loopid = 0;
|
|
|
|
events.register(obj.event_hash,
|
|
|
|
func (sender, msg) { obj._process(sender, msg) });
|
2011-09-30 22:29:23 +00:00
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
toggle: func {
|
2011-10-07 21:36:21 +00:00
|
|
|
# Send current time, current position and target position.
|
|
|
|
me.clock.advance();
|
2011-09-30 22:29:23 +00:00
|
|
|
me.move(me.target);
|
2011-10-07 21:36:21 +00:00
|
|
|
me._loop(me.loopid += 1);
|
2011-09-30 22:29:23 +00:00
|
|
|
},
|
|
|
|
destroy : func {
|
2011-10-07 21:36:21 +00:00
|
|
|
me.loopid += 1;
|
2011-09-30 22:29:23 +00:00
|
|
|
events.deregister(me.event_hash);
|
|
|
|
},
|
2011-10-07 21:36:21 +00:00
|
|
|
_process : func (sender, msg) {
|
|
|
|
if (me.clock.merge(sender, msg)) {
|
|
|
|
me.setpos(mp_broadcast.Binary.decodeDouble
|
|
|
|
(substr(msg, mp_broadcast.Binary.sizeOf["LamportTS"])));
|
|
|
|
me.target = mp_broadcast.Binary.decodeByte
|
|
|
|
(substr(msg,
|
|
|
|
mp_broadcast.Binary.sizeOf["LamportTS"] +
|
|
|
|
mp_broadcast.Binary.sizeOf["double"]));
|
|
|
|
me.move(me.target);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_loop : func (id) {
|
|
|
|
id == me.loopid or return;
|
|
|
|
# Send current time, current position and target position.
|
|
|
|
events.send(me.event_hash,
|
|
|
|
me.clock.timestamp() ~
|
|
|
|
mp_broadcast.Binary.encodeDouble(me.positionN.getValue()) ~
|
|
|
|
mp_broadcast.Binary.encodeByte(!me.target));
|
|
|
|
settimer(func { me._loop(id); }, 17, 1);
|
2011-09-30 22:29:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Internals
|
|
|
|
var shared_pp = "scenery/share-events";
|
|
|
|
|
2013-02-03 18:23:53 +00:00
|
|
|
var _set_state = func {
|
|
|
|
if (getprop("/sim/signals/reinit")) return; # Ignore resets.
|
|
|
|
if (getprop(shared_pp)) {
|
|
|
|
#print("scenery.nas: starting event sharing.");
|
|
|
|
events.start();
|
|
|
|
} else {
|
|
|
|
#print("scenery.nas: stopping event sharing.");
|
|
|
|
events.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-30 22:29:23 +00:00
|
|
|
_setlistener("sim/signals/nasal-dir-initialized", func {
|
|
|
|
events = mp_broadcast.EventChannel.new("scenery/events");
|
2013-02-03 18:23:53 +00:00
|
|
|
if (getprop(shared_pp)) {
|
|
|
|
#print("scenery.nas: starting event sharing.");
|
|
|
|
events.start();
|
|
|
|
} else {
|
2011-09-30 22:29:23 +00:00
|
|
|
#print("scenery.nas: stopping event sharing.");
|
|
|
|
events.stop();
|
|
|
|
}
|
2013-02-03 18:23:53 +00:00
|
|
|
setlistener(shared_pp, _set_state);
|
2011-09-30 22:29:23 +00:00
|
|
|
});
|