Trying to get the Agent to Mater more reliable

This commit is contained in:
samcake 2015-11-24 11:28:00 -08:00
parent 762709748d
commit bba5bf7a21
2 changed files with 132 additions and 36 deletions

View file

@ -11,7 +11,6 @@
// Set the following variables to the values needed
var commandChannel = "com.highfidelity.PlaybackChannel1";
var clip_url = null;
var playFromCurrentLocation = true;
var useDisplayName = true;
var useAttachments = true;
@ -22,6 +21,12 @@ var announceIDChannel = "com.highfidelity.playbackAgent.announceID";
var UNKNOWN_AGENT_ID = -2;
var id = UNKNOWN_AGENT_ID; // unknown until aknowledged
// The time between alive messages on the command channel
var timeSinceLastAlive = 0;
var ALIVE_PERIOD = 5;
var NUM_CYCLES_BEFORE_RESET = 5;
var notifyAlive = false;
// Set position/orientation/scale here if playFromCurrentLocation is true
Avatar.position = { x:0, y: 0, z: 0 };
Avatar.orientation = Quat.fromPitchYawRollDegrees(0, 0, 0);
@ -32,6 +37,7 @@ var subscribed = false;
var WAIT_FOR_AUDIO_MIXER = 1;
// Script. DO NOT MODIFY BEYOND THIS LINE.
var ALIVE = -1;
var DO_NOTHING = 0;
var PLAY = 1;
var PLAY_LOOP = 2;
@ -48,17 +54,14 @@ Recording.setPlayerUseSkeletonModel(useAvatarModel);
function getAction(channel, message, senderID) {
if(subscribed) {
var command = JSON.parse(message);
print("I'm the agent " + id + " and I received this: ID: " + command.id_key + " Action: " + command.action_key + " URL: " + command.clip_url_key);
if (command.id_key == id || command.id_key == -1) {
if (command.action_key === 6) {
clip_url = command.clip_url_key;
}
action = command.action_key;
print("That command was for me!");
print("My clip is: " + clip_url);
print("That command was for me! Agent with id: " + id);
} else {
action = DO_NOTHING;
}
@ -104,11 +107,21 @@ function getAction(channel, message, senderID) {
Agent.isAvatar = false;
break;
case LOAD:
print("Load");
if(clip_url !== null) {
Recording.loadRecording(clip_url);
print("Load");
if (!Agent.isAvatar) {
Agent.isAvatar = true;
}
if(command.clip_url_key !== null) {
print("Agent #" + id + " loading clip URL: " + command.clip_url_key);
Recording.loadRecording(command.clip_url_key);
} else {
print("Agent #" + id + " loading clip URL is NULL, nothing happened");
}
break;
case ALIVE:
print("Alive");
notifyAlive = true;
break;
case DO_NOTHING:
break;
default:
@ -136,11 +149,26 @@ function update(deltaTime) {
print("I'm the agent and I am ready to receive!");
}
if (subscribed && id == UNKNOWN_AGENT_ID) {
print("sending ready, id:" + id);
Messages.sendMessage(announceIDChannel, "ready");
}
}
if (subscribed && id != UNKNOWN_AGENT_ID) {
timeSinceLastAlive += deltaTime;
if (notifyAlive) {
timeSinceLastAlive = 0;
notifyAlive = false;
print("Master Alive");
} else if (timeSinceLastAlive > NUM_CYCLES_BEFORE_RESET * ALIVE_PERIOD) {
print("Master Lost, reseting Agent");
if (Recording.isPlaying()) {
Recording.stopPlaying();
}
Agent.isAvatar = false;
id = UNKNOWN_AGENT_ID;
}
}
}
Messages.messageReceived.connect(function (channel, message, senderID) {

View file

@ -14,9 +14,8 @@ HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/";
var ac_number = 1; // This is the default number of ACs. Their ID need to be unique and between 0 (included) and ac_number (excluded)
var names = new Array(); // It is possible to specify the name of the ACs in this array. ACs names ordered by IDs (Default name is "ACx", x = ID + 1))
var channel = "com.highfidelity.PlaybackChannel1";
var commandChannel = "com.highfidelity.PlaybackChannel1";
var subscribed = false;
var clip_url = null;
var input_text = null;
var knownAgents = new Array; // We will add our known agents here when we discover them
@ -24,9 +23,15 @@ var knownAgents = new Array; // We will add our known agents here when we discov
// available playbackAgents will announce their sessionID here.
var announceIDChannel = "com.highfidelity.playbackAgent.announceID";
// Script. DO NOT MODIFY BEYOND THIS LINE.
Script.include("../libraries/toolBars.js");
// The time between alive messages on the command channel
var timeSinceLastAlive = 0;
var ALIVE_PERIOD = 5;
// Script. DO NOT MODIFY BEYOND THIS LINE.
//Script.include("../libraries/toolBars.js");
Script.include(HIFI_PUBLIC_BUCKET + "scripts/libraries/toolBars.js");
var ALIVE = -1;
var DO_NOTHING = 0;
var PLAY = 1;
var PLAY_LOOP = 2;
@ -52,6 +57,9 @@ var playLoopIcon = new Array();
var stopIcon = new Array();
var loadIcon = new Array();
var performanceJSON = null;
var performanceLoadedNeedUpdate = false;
setupPlayback();
function setupPlayback() {
@ -59,7 +67,7 @@ function setupPlayback() {
if (ac_number === "" || ac_number === null) {
ac_number = 1;
}
Messages.subscribe(channel);
Messages.subscribe(commandChannel);
subscribed = true;
setupToolBars();
}
@ -139,7 +147,27 @@ function setupToolBars() {
}
}
function sendCommand(id, action) {
function loadAvatarClipsFromPerformanceJSON(performanceJSON) {
if (performanceJSON.avatarClips) {
var numClips = performanceJSON.avatarClips.length;
print("Performance file contains:" + JSON.stringify(performanceJSON));
print("Number of clips in the performance file is: " + numClips + " Number of agents is: " + knownAgents.length);
if (numClips > knownAgents.length) {
numClips = knownAgents.length;
}
for (i = 0; i < numClips; i++) {
var clipURL = performanceJSON.avatarClips[i];
print("Loading clip " + clipURL + " to Agent #" + i);
sendCommand(i, LOAD, clipURL);
}
} else {
print("Performance file cannot be interpreted:" + JSON.stringify(performanceJSON));
}
}
function sendCommand(id, action, argument) {
if (action === SHOW) {
toolBars[id].selectTool(onOffIcon[id], false);
toolBars[id].setAlpha(ALPHA_ON, playIcon[id]);
@ -152,24 +180,48 @@ function sendCommand(id, action) {
toolBars[id].setAlpha(ALPHA_OFF, playLoopIcon[id]);
toolBars[id].setAlpha(ALPHA_OFF, stopIcon[id]);
toolBars[id].setAlpha(ALPHA_OFF, loadIcon[id]);
} else if (action == ALIVE) {
} else if (toolBars[id].toolSelected(onOffIcon[id])) {
return;
}
if (id == (toolBars.length - 1)) {
id = -1; // Master command becomes broadcast.
}
var message = {
id_key: id,
action_key: action,
clip_url_key: clip_url
};
if(subscribed){
Messages.sendMessage(channel, JSON.stringify(message));
print("Message sent!");
clip_url = null;
if (action == LOAD) {
if (performanceLoadedNeedUpdate == false) {
Assets.downloadData(argument, function (data) {
performanceJSON = JSON.parse(data);
performanceLoadedNeedUpdate = true;
// print("Performance file contains:" + JSON.stringify(performanceJSON));
});
return;
}
} else {
id = -1;
var message = {
id_key: id,
action_key: action,
clip_url_key: argument
};
if(subscribed){
Messages.sendMessage(commandChannel, JSON.stringify(message));
print("Message sent!");
}
}
} else {
var message = {
id_key: id,
action_key: action,
clip_url_key: argument
};
if(subscribed){
Messages.sendMessage(commandChannel, JSON.stringify(message));
print("Message sent!");
}
}
}
@ -193,8 +245,7 @@ function mousePressEvent(event) {
} else if (loadIcon[i] === toolBars[i].clicked(clickedOverlay, false)) {
input_text = Window.prompt("Insert the url of the clip: ","");
if (!(input_text === "" || input_text === null)) {
clip_url = input_text;
sendCommand(i, LOAD);
sendCommand(i, LOAD, input_text);
}
} else {
// Check individual controls
@ -214,8 +265,7 @@ function mousePressEvent(event) {
} else if (loadIcon[i] === toolBars[i].clicked(clickedOverlay, false)) {
input_text = Window.prompt("Insert the url of the clip: ","");
if (!(input_text === "" || input_text === null)) {
clip_url = input_text;
sendCommand(i, LOAD);
sendCommand(i, LOAD, input_text);
}
} else {
@ -240,13 +290,25 @@ function moveUI() {
}
}
function update() {
function update(deltaTime) {
var newDimensions = Controller.getViewportDimensions();
if (windowDimensions.x != newDimensions.x ||
windowDimensions.y != newDimensions.y) {
windowDimensions = newDimensions;
moveUI();
}
if (performanceLoadedNeedUpdate) {
loadAvatarClipsFromPerformanceJSON(performanceJSON);
performanceLoadedNeedUpdate = false;
}
timeSinceLastAlive += deltaTime;
if (timeSinceLastAlive > ALIVE_PERIOD) {
timeSinceLastAlive = 0;
print("ping alive");
sendCommand((toolBars.length - 1), ALIVE);
}
}
function scriptEnding() {
@ -256,7 +318,7 @@ function scriptEnding() {
}
if (subscribed) {
Messages.unsubscribe(channel);
Messages.unsubscribe(commandChannel);
}
Messages.unsubscribe(announceIDChannel);
}
@ -272,13 +334,19 @@ Messages.messageReceived.connect(function (channel, message, senderID) {
if (channel == announceIDChannel && message == "ready") {
// check to see if we know about this agent
if (knownAgents.indexOf(senderID) < 0) {
print("New agent to be hired " + senderID);
var indexOfNewAgent = knownAgents.length;
knownAgents[indexOfNewAgent] = senderID;
var acknowledgeMessage = senderID + "." + indexOfNewAgent;
Overlays.editOverlay(nameOverlays[indexOfNewAgent], { backgroundColor: { red: 0, green: 255, blue: 0 }, text: "Agent-Hired" } );
print("Hired new Agent " + senderID + " #" + indexOfNewAgent);
Messages.sendMessage(announceIDChannel, acknowledgeMessage);
} else {
print("New agent still sending ready ? " + senderID);
}
}
});
moveUI();
moveUI();