// // coordinatorAC.js // // Created by Thijs Wenker on 4/14/17 // Copyright 2017 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // //debugger; var randFloat = function(low, high) { return low + Math.random() * (high - low); }; var VISUALIZATION = false; var COORDINATOR_CHANNEL = 'COORDINATOR-AC'; var ACTOR_CHANNEL_PREFIX = 'ACTOR-PLAYBACK-AC_'; var AVATARS_ARRAY = [ "http://cdn.highfidelity.com/chris/production/morph/1/1.fst", // "http://mpassets.highfidelity.com/bd80a6d7-7173-489e-87c6-f7ee56e65530-v1/M_RetFut.fst", // "http://mpassets.highfidelity.com/47c8d706-d486-4c2d-afcc-70d4e1e25117-v1/M_RetSpaSuit.fst", // "http://mpassets.highfidelity.com/548d0792-0bac-4933-bbfc-57d71912d77e-v1/M_OutMer.fst", // "http://mpassets.highfidelity.com/13277c09-892f-4a5e-b9a5-8994a37d68bf-v1/F_WasWar.fst", // "http://mpassets.highfidelity.com/2d384111-0f0e-42e2-b800-66bfcab4aefb-v1/F_VooQue.fst", // "http://mpassets.highfidelity.com/57e4d1cd-9f52-4c95-9051-326f9bb114ea-v1/F_SteAvi.fst", // "http://mpassets.highfidelity.com/da2ad4cd-47d4-41da-b764-41f39ff77e30-v1/F_JerGir.fst", // "http://mpassets.highfidelity.com/96c747ab-f71b-44ee-8eb9-d19fc9593dda-v1/F_CatBur.fst", // "http://mpassets.highfidelity.com/ede82c38-c66e-4f67-9e0b-0bb0782db18f-v1/M_WesOut.fst", // "http://mpassets.highfidelity.com/8872ae86-a763-4db3-8373-d27514c1481e-v1/M_VinAvi.fst", // "http://mpassets.highfidelity.com/faf505f1-4fd1-4ed2-8909-816af246c48f-v1/M_VicGen.fst", // "http://mpassets.highfidelity.com/d807a7d2-5122-4436-a6f9-3173c94d1c49-v1/M_SuaGen.fst", // "http://mpassets.highfidelity.com/1dd41735-06f4-45a3-9ec0-d05215ace77b-v1/M_MarSen.fst", // "http://mpassets.highfidelity.com/2cad3894-8ab3-4ba5-a723-0234f93fbd6a-v1/M_BowBea.fst", // "http://mpassets.highfidelity.com/cf0eb1be-9ec7-4756-8eaf-ac8f3ec09eba-v1/F_ClaDef.fst", // "http://mpassets.highfidelity.com/0cedeca3-c1a4-4be9-9fd5-dad716afcc7e-v1/F_Cyria.fst", // "http://mpassets.highfidelity.com/dc55803b-9215-47dd-9408-eb835dac4082-v1/F_ParGir.fst", // "http://mpassets.highfidelity.com/775a8fb3-cfe7-494d-b603-a0a2d6910e55-v1/F_VinCov.fst", // "http://mpassets.highfidelity.com/eba0d8f8-aa72-4a6b-ab64-4d3fd4695b20-v1/F_VogHei.fst", // "http://mpassets.highfidelity.com/4f400c78-38f9-42af-b03b-11b5451d41b9-v1/M_MidRog.fst", // "http://mpassets.highfidelity.com/ad774d79-13f1-46e2-87c9-de49a261b264-v1/F_GunSli.fst", // "http://mpassets.highfidelity.com/5acbaefa-5455-49a2-8d40-89d12aa393ca-v1/M_KniWol.fst", // "http://mpassets.highfidelity.com/aaa1b0a8-3e1b-492a-9aee-600e5dc907db-v1/F_RetSciSuit.fst", // "http://mpassets.highfidelity.com/d8da10b6-25c1-40e2-9a66-369316c722d7-v1/F_AniSuit.fst", // "http://mpassets.highfidelity.com/f3fbb9f4-e159-49ed-ac32-03af9056b17e-v1/matthew.fst", // "http://mpassets.highfidelity.com/0c954ba0-4d87-4353-b65e-c45509f85658-v1/priscilla.fst", // "http://mpassets.highfidelity.com/e76946cc-c272-4adf-9bb6-02cde0a4b57d-v1/9e8c5c42a0cbd436962d6bd36f032ab3.fst", // "http://mpassets.highfidelity.com/72e083ee-194d-4113-9c61-0591d8257493-v1/skeleton_Rigged.fst", // "http://mpassets.highfidelity.com/f14bf7c9-49a1-4249-988a-0a577ed78957-v1/beingOfLight.fst", // "http://mpassets.highfidelity.com/1b7e1e7c-6c0b-4f20-9cd0-1d5ccedae620-v1/bb64e937acf86447f6829767e958073c.fst", // "http://mpassets.highfidelity.com/67d7c7aa-c300-4d03-85f4-86480130eaa5-v1/F_StarCrew.fst", // "http://mpassets.highfidelity.com/d293ef06-c659-467a-9288-c3cbaff0372a-v1/arya_avatar.fst", // "http://mpassets.highfidelity.com/faf249d5-12a8-48e2-a08e-fb0c33087011-v1/F_Ranger.fst", // "http://mpassets.highfidelity.com/b4502145-15eb-4023-b7d6-a81c5cbf6abf-v1/F_FitTra.fst", // "http://mpassets.highfidelity.com/548d0792-0bac-4933-bbfc-57d71912d77e-v1/M_OutMer.fst", // "http://mpassets.highfidelity.com/caa61e5d-5629-4165-81d8-6a7eb55e942d-v1/F_DeaSur.fst", // "http://mpassets.highfidelity.com/2cad3894-8ab3-4ba5-a723-0234f93fbd6a-v1/M_BowBea.fst", // "http://mpassets.highfidelity.com/fd4fa45a-9d2a-463e-a484-f9d1b3bba724-v1/M_BeaWar.fst", // "http://mpassets.highfidelity.com/367a5b60-8a92-4d56-a152-a00f3086f02b-v1/M_Espio.fst", // "http://mpassets.highfidelity.com/ab466729-31da-4b4c-a33c-366f7c1d38e5-v1/M_MMAFig.fst", // "http://mpassets.highfidelity.com/b0795a0c-493d-4abd-b4cc-5f32e6d6df46-v1/M_SalMer.fst", // "http://mpassets.highfidelity.com/0a1d44bf-a988-4199-b29e-a532ab85a2e8-v1/M_StaShi.fst", // "http://mpassets.highfidelity.com/d807a7d2-5122-4436-a6f9-3173c94d1c49-v1/M_SuaGen.fst", // "http://mpassets.highfidelity.com/cb20212c-36f2-4d41-bdad-132361ca6ff4-v1/M_TreTee.fst", // "http://mpassets.highfidelity.com/830988dc-619a-4e88-96e1-a19fa0aaa30f-v1/M_UrbEnf.fst", // "http://mpassets.highfidelity.com/faf505f1-4fd1-4ed2-8909-816af246c48f-v1/M_VicGen.fst", // "http://mpassets.highfidelity.com/883ac86f-dd29-4676-8bda-7dd52fb6465f-v1/M_WasWan.fst", // "http://mpassets.highfidelity.com/ede82c38-c66e-4f67-9e0b-0bb0782db18f-v1/M_WesOut.fst", // "http://mpassets.highfidelity.com/04c9a1e9-0390-4a7f-b6c6-5f135c19e3fb-v1/F_ArmTro.fst", // "http://mpassets.highfidelity.com/e863348f-a777-4f36-86e6-af6e65ffa161-v1/F_BloSam.fst", // "http://mpassets.highfidelity.com/cf0eb1be-9ec7-4756-8eaf-ac8f3ec09eba-v1/F_ClaDef.fst", // "http://mpassets.highfidelity.com/0cedeca3-c1a4-4be9-9fd5-dad716afcc7e-v1/F_Cyria.fst", // "http://mpassets.highfidelity.com/da2ad4cd-47d4-41da-b764-41f39ff77e30-v1/F_JerGir.fst", // "http://mpassets.highfidelity.com/534d42f8-ec13-4145-929f-5c8facac2fb7-v1/F_LegFig.fst", // "http://mpassets.highfidelity.com/dc55803b-9215-47dd-9408-eb835dac4082-v1/F_ParGir.fst", // "http://mpassets.highfidelity.com/f823e831-d8c4-4191-a3bd-427e406e69f9-v1/F_Shinjuku.fst", // "http://mpassets.highfidelity.com/eba0d8f8-aa72-4a6b-ab64-4d3fd4695b20-v1/F_VogHei.fst", // "http://mpassets.highfidelity.com/13277c09-892f-4a5e-b9a5-8994a37d68bf-v1/F_WasWar.fst", // "http://mpassets.highfidelity.com/9b589fbb-59e4-47a9-8b3f-bf8d3a0bd1d8-v1/M_LawSur.fst", // "http://mpassets.highfidelity.com/4f400c78-38f9-42af-b03b-11b5451d41b9-v1/M_MidRog.fst", // "http://mpassets.highfidelity.com/c90d755d-0456-48fd-b98c-09c4d85cd481-v1/M_MouOff.fst", // "http://mpassets.highfidelity.com/c2ed3b9a-b3a9-4424-9fd2-8a798209f32b-v1/M_PerTra.fst", // "http://mpassets.highfidelity.com/c48928ac-7657-41f4-bbdc-9b47385736ab-v1/M_SpaMar.fst", // "http://mpassets.highfidelity.com/d029ae8d-2905-4eb7-ba46-4bd1b8cb9d73-v1/4618d52e711fbb34df442b414da767bb.fst", // "http://mpassets.highfidelity.com/c85c497d-c87b-42b1-9bbf-5405e05a0ad3-v1/M_ArmSol.fst", // "http://mpassets.highfidelity.com/1dd41735-06f4-45a3-9ec0-d05215ace77b-v1/M_MarSen.fst", // "http://mpassets.highfidelity.com/bd80a6d7-7173-489e-87c6-f7ee56e65530-v1/M_RetFut.fst", // "http://mpassets.highfidelity.com/8872ae86-a763-4db3-8373-d27514c1481e-v1/M_VinAvi.fst", // "http://mpassets.highfidelity.com/f798d926-9a9e-481a-b298-af0e45451252-v1/F_Assassin.fst", // "http://mpassets.highfidelity.com/ad774d79-13f1-46e2-87c9-de49a261b264-v1/F_GunSli.fst", // "http://mpassets.highfidelity.com/aaa1b0a8-3e1b-492a-9aee-600e5dc907db-v1/F_RetSciSuit.fst" ]; var RECORDINGS_ARRAY = [ "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/peanuts.hfr", // "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/jim.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/arya_heyChris.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/cal_alrightCool.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/carmen_excuseMe.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/fausto_someGoodSeats.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/halima_thatdBeFine.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_sargon_duckiesAndBunnies.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_bianca_smileInTheMorning.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_kobe_adversityHasStrengthenedMe.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_raffaello_tryTheCottonCandy.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_rasta_noLinesInNature.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_mcGee_strawberries.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_purple_manNeedsColorToLive.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_roia_differentOpinions.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_annie_newDayNewStrength.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_halima_aCalmMind.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_isobel_warIsPeace.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_carmen_humanRelationship.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_barbarian_grunting.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_arya_iAmALoveBoat.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_skeleton_buenoEstante.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_robot_scanningLifeForms.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_priscille_iCantDecide.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_beingOfLight_beingAbeing.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/artemis_wheresBill.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/chloe_imOkay.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/haj_overhere.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/halima_dietCoke.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/maery_hiChrisOverHere.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/matthew_cool.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/matthew_niceplace.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/mcgee_takePeanuts.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/pris_isntSoBad.hfr", "http://hifi-content.s3.amazonaws.com/caitlyn/dev/npc/npc_theaterTakes/raff_venue.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/usher3.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/usher2.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/usher1.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/claude.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting10.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting9.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting8.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting7.hfr", // "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting6.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting5.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting4.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting3.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting2.hfr", "http://hifi-content.s3.amazonaws.com/DomainContent/Event%20/NPC%27s/waiting1.hfr" ]; // http://stackoverflow.com/a/2450976 function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } Messages.messageReceived.connect(function (channel, message, senderID) { if (channel === COORDINATOR_CHANNEL) { if (message === 'request') { if (reservedActors.length === 0) { print('No actor spaces available.'); return; } var reservedActor = reservedActors.pop(); Messages.sendMessage(ACTOR_CHANNEL_PREFIX + senderID, JSON.stringify({ position: reservedActor.position, avatarURL: reservedActor.avatarURL, recordingURL: reservedActor.recordingURL, displayName: reservedActor.displayName })); } } }); Script.setTimeout(function() { Messages.subscribe(COORDINATOR_CHANNEL); Messages.sendMessage(COORDINATOR_CHANNEL, 'init'); }, 300); var COMFORTABLE_AVATAR_DISTANCE = 0.6; // location comments are given while looking from stage towards the audience var AREAS_ARRAY = [ // front level { minX: 97.0, maxX: 103.0, y: -0.6, minZ: 37, maxZ: 41 , rows: 4, fill: 0.6 }, // level 1 left { minX: 92.7, maxX: 98.9, y: -0.3, minZ: 43, maxZ: 43 }, // level 1 right { minX: 100.3, maxX: 106.6, y: -0.3, minZ: 43, maxZ: 43 }, // level 2 left { minX: 92.7, maxX: 98.9, y: 0.3, minZ: 45, maxZ: 45 }, // level 2 right { minX: 100.3, maxX: 106.6, y: 0.3, minZ: 45, maxZ: 45 }, // level 3 left { minX: 92.7, maxX: 98.9, y: 1 , minZ: 47, maxZ: 47 }, // level 3 right { minX: 100.3, maxX: 106.6, y: 1 , minZ: 47, maxZ: 47 }, // top level { minX: 92.7, maxX: 106.6, y: 1.7, minZ: 51.9, maxZ: 51.9} ]; var DUPLICATE_PREVENTION_MODES = { NONE: 0, RANDOM_LISTS: 1, PREVENT_SAME_NEIGHBOR: 2 }; var reservedActors = []; var totalSpaces = 0; var minAvatarFill = 0.8; // 80% var maxRand = 0; var createSpaceManager = function(areas, expectedAvatars, duplicatePreventionMode) { var _this, _areas, _areaDetails, _expectedAvatars, _duplicatePreventionMode; function SpaceManager(areas) { _this = this; _areas = areas; _expectedAvatars = expectedAvatars; _duplicatePreventionMode = duplicatePreventionMode; _areaDetails = []; _this.initAreaDetails(); _this.reserveActors(); } SpaceManager.prototype = { initAreaDetails: function() { _areaDetails = []; _areas.forEach(function(area) { var randomNum = Math.random(); maxRand += randomNum; var width = area.maxX - area.minX; var height = area.maxZ - area.minZ; var columns = (Math.floor(width / COMFORTABLE_AVATAR_DISTANCE) + 1); var rows = (Math.floor(height / COMFORTABLE_AVATAR_DISTANCE) + 1); var spaces = columns * rows; totalSpaces += spaces; _areaDetails.push({ area: area, spaces: spaces, reservedActorIndexes: [], spacesPreferred: -1, width: width, height: height, columns: columns, rows: rows, random: randomNum }); }); var averageFactor = _expectedAvatars / totalSpaces; print('avg fact' + averageFactor); var spacesOccupied = 0; _areaDetails.forEach(function(details) { var percentage = details.random / maxRand; var fillPercentage = details.area.fill !== undefined ? details.area.fill : minAvatarFill + (_areaDetails.length * percentage * ((1.0 - minAvatarFill) * 2.0)); print('fp: ' + percentage + ' ' + fillPercentage); details.spacesPreferred = Math.ceil(fillPercentage * details.spaces) * averageFactor; spacesOccupied += details.spacesPreferred; }); print("Spaces occupied " + spacesOccupied); if (totalSpaces < _expectedAvatars) { print('Please add some more area for your actors.'); } }, getAreas: function() { return _areaDetails; }, reserveActors: function() { reservedActors = []; var randomRecordingURLS = shuffle(RECORDINGS_ARRAY.slice()); var randomAvatarModelURLS = shuffle(AVATARS_ARRAY.slice()); _areaDetails.forEach(function(details) { var rows = details.area.rows !== undefined ? details.area.rows : details.rows; var columnStep = (details.width / details.spacesPreferred) * rows; var rowStep = Math.floor(details.height / rows); for (var i = 0; i < details.spacesPreferred; i++) { if (randomRecordingURLS.length === 0) { randomRecordingURLS = shuffle(RECORDINGS_ARRAY.slice()); } if (randomAvatarModelURLS.length === 0) { randomAvatarModelURLS = shuffle(AVATARS_ARRAY.slice()); } reservedActors.push({ position: { x: details.area.minX + ((columnStep * (i / rows))) + (columnStep * 0.5) + (Math.random() * columnStep * 0.5), y: details.area.y, z: details.area.minZ + ((rowStep * (i % rows))) + (rowStep * 0.5) + (Math.random() * rowStep * 0.5) //Math.random() * details.height }, displayName: 'Crowd NPC (spot ' + (reservedActors.length + 1) + ')', recordingURL: randomRecordingURLS.pop(), avatarURL: randomAvatarModelURLS.pop(), sessionUUID: null }); } }); reservedActors = shuffle(reservedActors); } }; return new SpaceManager(areas, expectedAvatars, duplicatePreventionMode); }; var EXPECTED_NUMBER_OF_AVATARS = 100; var spaceManager = createSpaceManager(AREAS_ARRAY, EXPECTED_NUMBER_OF_AVATARS, DUPLICATE_PREVENTION_MODES.RANDOM_LISTS); if (this.Agent !== undefined) { Agent.isAvatar = true; Avatar.position = {x: 0, y: 0, z: 0}; Avatar.orientation = Quat.fromPitchYawRollDegrees(0, 0, 0); Avatar.scale = 1.0; Avatar.skeletonModelURL = 'http://hifi-content.s3.amazonaws.com/ozan/dev/avatars/invisible_avatar/invisible_avatar.fst'; } if (VISUALIZATION) { // Visualization var FLOOR_OFFSET = {y: -0.6}; var VISUALIZATION_LIFETIME = 360; // seconds var floorEntities = []; var actorEntities = []; spaceManager.getAreas().forEach(function (details) { var dimensionX = details.width; var dimensionZ = details.height; floorEntities.push(Entities.addEntity({ type: 'Box', dimensions: { x: Math.max(dimensionX, COMFORTABLE_AVATAR_DISTANCE), y: 0.05, z: Math.max(dimensionZ, COMFORTABLE_AVATAR_DISTANCE) }, position: Vec3.sum({ x: details.area.minX + (dimensionX / 2), y: details.area.y, z: details.area.minZ + (dimensionZ / 2) }, FLOOR_OFFSET), lifetime: VISUALIZATION_LIFETIME })); }); reservedActors = shuffle(reservedActors); for (var i = 0; i < EXPECTED_NUMBER_OF_AVATARS && i < reservedActors.length; i++) { actorEntities.push(Entities.addEntity({ type: 'Model', position: reservedActors[i].position, modelURL: reservedActors[i].avatarURL, rotation: Quat.fromPitchYawRollDegrees(0.0, 180.0, 0.0), lifetime: VISUALIZATION_LIFETIME })); } Script.scriptEnding.connect(function () { floorEntities.forEach(function (floor) { Entities.deleteEntity(floor); }); actorEntities.forEach(function (actor) { Entities.deleteEntity(actor); }); }); }