forked from allenai/ai2thor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
416 lines (366 loc) · 14.8 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
$(
() => {
let gameInstance = null;
let getParams = parseGet();
window.game_build = 'build' in getParams ? getParams['build'] : window.game_build;
window.game_url = 'build' in getParams ? `${window.game_build}/Build/thor-local-WebGL.json` : window.game_url;
// let scene = getParams['scene'];
console.log("GAME URL: ", window.game_url);
let hider = getParams['role'] !== 'seeker';
let gameInitialized = false;
let objectId = '';
let spawnRandomSeed = 'spawnSeed' in getParams ? parseInt(getParams['spawnSeed']) : 0;
let objectsRandomSeed = 'objectsSeed' in getParams ? parseInt(getParams['objectsSeed']) : 0;
let outputData = {
object_type: getParams['object'],
object_variation: getParams['variation'],
open_objects: []
};
let gameConfig = null;
let lastMetadadta = null;
let reachablePositions = null;
let isTurkSanbox = 'sandbox' in getParams && getParams['sandbox'].toLowerCase() === 'true';
const turkSandboxUrl = 'https://workersandbox.mturk.com/mturk/externalSubmit';
const turkUrl = 'https://www.mturk.com/mturk/externalSubmit';
let pickupFailTimeout = false;
let hasObject = false;
// Utils
function paramStrToAssocArray(prmstr) {
let params = {};
let prmarr = prmstr.split('&');
for (let i = 0; i < prmarr.length; i++) {
let tmparr = prmarr[i].split('=');
params[tmparr[0]] = tmparr[1];
}
return params;
}
function parseGet() {
let paramStr = window.location.search.substr(1);
return paramStr !== null && paramStr !== ''
? paramStrToAssocArray(paramStr)
: {};
}
/////////////////////
///// Unity callbacks
window.onGameLoaded = function() {
if (!gameInitialized) {
if ('scene' in getParams && getParams['scene']) {
gameInstance.SendMessage('PhysicsSceneManager', 'SwitchScene', getParams['scene']);
}
gameInitialized = true;
}
};
window.onUnityMetadata = function(metadata) {
let jsonMeta = JSON.parse(metadata);
// FIRST init event
handleEvent(jsonMeta);
lastMetadadta = jsonMeta;
};
// Aggregate data
function gatherFinalState(metadata) {
let agentMetadata = metadata.agents[0];
let filtered = agentMetadata.objects.filter((obj) => obj.objectId === objectId);
if (filtered.length === 1) {
let object = filtered[0];
outputData['object_position'] = object.position;
outputData['object_rotation'] = object.rotation;
outputData['open_objects'] = agentMetadata.objects.filter((obj) => obj.isOpen).map(obj => obj.objectId);
outputData['object_locations_and_rotations'] = agentMetadata.objects.reduce((acc, obj, {}) => {
return {
...acc,
[obj.objectId]:{
position: obj.position,
rotation: obj.rotation
}
}
});
}
else {
throw `Invalid id ${objectId} in metadata.objects: ${agentMetadata.objects}`;
}
return outputData;
}
// Submit to turk
function submitHit(metadata) {
let data = gatherFinalState(metadata);
document.forms['mturk_form'].assignmentId.value = getParams['assignmentId'];
console.log('Turk submit!!', data);
document.forms['mturk_form'].data.value = JSON.stringify(data);
// document.forms['mturk_form'].submit();
window.parent.postMessage(JSON.stringify(data), '*');
}
///////////////////////
///// Hider's Handlers
function Move(metadata) {
outputData.trayectory.push(metadata.agents[0].agent.position);
}
function CreateObject(metadata) {
let agentMetadata = metadata.agents[0];
let actionSuccess = agentMetadata.lastActionSuccess;
if (!actionSuccess) {
throw `Action '${agentMetadata.lastAction}' failed with error: "${agentMetadata.errorMessage}"' `
}
objectId = agentMetadata.actionReturn;
hasObject = true;
outputData['target_id'] = objectId;
gameInstance.SendMessage('FPSController', 'SpawnAgent', spawnRandomSeed);
gameInstance.SendMessage('FPSController', 'DisableObjectCollisionWithAgent', objectId);
$("#finish-hit").click((e) => {
// The callback for ExhaustiveSearchForItem action will call submitHit
// with visibility information of the hiding spot
gameInstance.SendMessage ('FPSController', 'Step', JSON.stringify({
"action": "ExhaustiveSearchForItem",
"objectId": objectId,
"positions": reachablePositions
}));
});
$("#debug-json").click((e) => {
debugPrintJson(lastMetadadta)
});
$("#reset-hit").click((e) => {
$("#reset-hit").blur();
gameInstance.SendMessage ('PhysicsSceneManager', 'SwitchScene', outputData.scene);
}).attr("disabled", false);
$("#move").click((e) => {
gameInstance.SendMessage ('FPSController', 'Step', JSON.stringify({
action: "MoveAhead",
moveMagnitude: 0.25
}));
})
}
function OpenObject(metadata) {
// let agentMetadata = metadata.agents[0];
}
function RegisterAgentPosition(metadata) {
let agentMetadata = metadata.agents[0];
let agent = agentMetadata.agent;
outputData['agent_start_location'] =
{
"x": agent.position.x,
"y": agent.position.y,
"z": agent.position.z,
"rotation": agent.rotation.y,
"horizon": agent.cameraHorizon,
"standing": agentMetadata.isStanding
};
}
function ExhaustiveSearchForItem(metadata) {
let agentMetadata = metadata.agents[0];
outputData['visibility'] = {
objectSeen: agentMetadata.actionReturn['objectSeen'],
positionsTried: agentMetadata.actionReturn['positionsTried']
};
submitHit(metadata);
}
function PickupObject(metadata) {
$("#finish-hit").attr("disabled", true);
let agentMetadata = metadata.agents[0];
hasObject = agentMetadata.lastActionSuccess;
if (pickupFailTimeout) {
clearTimeout(pickupFailTimeout);
}
}
function DropObject(metadata) {
let agentMetadata = metadata.agents[0];
hasObject = !agentMetadata.lastActionSuccess;
if (pickupFailTimeout) {
clearTimeout(pickupFailTimeout);
}
$("#finish-hit").attr("disabled", false);
}
///////////////////////
///// Seeker's Handlers
function CreateObjectAtLocation(metadata) {
let agentMetadata = metadata.agents[0];
gameInstance.SendMessage('FPSController', 'SetOnlyObjectIdSeeker', agentMetadata.actionReturn);
gameInstance.SendMessage('FPSController', 'DisableObjectCollisionWithAgent', agentMetadata.actionReturn);
objectId = gameConfig.target_id;
}
function FoundObject(metadata) {
let agentMetadata = metadata.agents[0];
let pickedObjectId = agentMetadata.actionReturn;
if (pickedObjectId === objectId) {
console.log("Success!!");
outputData['success'] = true;
$("#message-text").html("<strong class='green-text'>You Found the object!</strong>").show();
// Auto submit
setTimeout(() => {
submitHit(lastMetadadta)
}, 1000);
}
}
function InitScene(metadata) {
console.log("--- ", metadata.agents[0].sceneName);
outputData['scene'] = metadata.agents[0].sceneName;
outputData['trayectory'] = [];
outputData['actions'] = [];
gameInstance.SendMessage ('FPSController', 'Step', JSON.stringify({
action: "RandomizeHideSeekObjects",
randomSeed: objectsRandomSeed,
removeProb: 0.0,
}));
["Cup", "Mug", "Bread", "Tomato", "Plunger", "Knife"]
.forEach(
k => gameInstance.SendMessage(
'FPSController',
'Step',
JSON.stringify({action: "DisableAllObjectsOfType", objectId: k})
)
);
if (hider) {
let objectName = getParams['object'];
let objectVariation = parseInt(getParams['variation']);
gameInstance.SendMessage('FPSController', 'SpawnObjectToHide', JSON.stringify(
{
objectType: objectName,
objectVariation: objectVariation,
}
));
}
else {
gameInstance.SendMessage('FPSController', 'Step', JSON.stringify({
action: "TeleportFull",
x: gameConfig.agent_start_location.x,
y: gameConfig.agent_start_location.y,
z: gameConfig.agent_start_location.z,
horizon: gameConfig.agent_start_location.horizon,
rotation: {x: 0.0, y: gameConfig.agent_start_location.rotation, z: 0.0},
standing: gameConfig.agent_start_location.standing
}));
gameInstance.SendMessage('FPSController', 'Step', JSON.stringify({
action: "CreateObjectAtLocation",
position: gameConfig.object_position,
rotation: gameConfig.object_rotation,
forceAction: true,
objectType: gameConfig.object_type,
objectVariation: gameConfig.object_variation,
randomizeObjectAppearance: false
}));
}
}
let eventHandlers = {
hider: {
[null]: InitScene,
MoveAhead: Move,
MoveBack: Move,
MoveLeft: Move,
MoveRight: Move,
DropHandObject: DropObject,
CreateObject: CreateObject,
OpenObject: OpenObject,
RandomlyMoveAgent: RegisterAgentPosition,
ExhaustiveSearchForItem: ExhaustiveSearchForItem,
PickupObject: PickupObject
},
seeker: {
[null]: InitScene,
MoveAhead: Move,
MoveBack: Move,
MoveLeft: Move,
MoveRight: Move,
OpenObject: OpenObject,
PickupObject: FoundObject,
CreateObjectAtLocation: CreateObjectAtLocation
}
};
function handleEvent(metadata) {
let action = metadata.agents[0].lastAction;
let role = hider ? 'hider' : 'seeker';
let handler = eventHandlers[role][action];
if (handler !== undefined) {
handler(metadata);
}
let agentMetadata = metadata.agents[0];
let agent = agentMetadata.agent;
outputData.actions.push({
lastAction: agentMetadata.lastAction,
lastActionSuccess: agentMetadata.lastActionSuccess,
agent: {
x: agent.position.x,
y: agent.position.y,
z: agent.position.z,
rotation: agent.rotation.y,
horizon: agent.cameraHorizon,
standing: agentMetadata.isStanding
}
});
}
function initGame(url) {
const t0 = performance.now();
gameInstance = UnityLoader.instantiate("gameContainer", url, {
onProgress: UnityProgress, Module: {
onRuntimeInitialized: function () {
// At this point unity loaded successfully
UnityProgress(gameInstance, "complete");
const t1 = performance.now();
console.log(`Load finished. Took: ${(t1 - t0) / 1000}s`);
},
}
});
}
// Unity Loader Script
$.getScript(`${window.game_build}/Build/UnityLoader.js` )
.done(function( script, textStatus ) {
console.log("Status: ", textStatus);
$("#mturk_form").attr("action", isTurkSanbox ? turkSandboxUrl : turkUrl);
// Instruction Rendering
if (hider) {
let objectHTML = $('<strong class="important-text"></strong>');
objectHTML.text(getParams['object']);
$("#instruction-text").append("You have to find a ");
$("#instruction-text").append(objectHTML);
$("#instruction-2").html(`Move around in the room, open drawers and cabinets to look for a good hiding spot.`);
$("#instruction-3").html('When you are ready, move the object (see Shift controls) to place it more precisely, click on it to drop it.');
$("#instruction-4").html(`If you're happy with your hiding spot click the <strong class="green-text">Finish</strong> button above. Or <strong class="red-text">Reset</strong> to start over.`);
$("#instruction-5").html("If a door/drawer opens and closes, it means you're in the way! Move back and try again.");
$("#instruction-6").html("If you're clicking on the object and it's not being picked up, try moving closer to the object! (within 1.5 meters)");
$("#instructions-hider").show();
$(document).keypress(function(event) {
if (event.keyCode === 32) {
if(event.shiftKey && !hasObject){
pickupFailTimeout = setTimeout(() => {
$("#last-action-text").html(`Action Failed: <strong class="red-text">Pick Up Failed</strong>`).show();
}, 800)
} else {
}
}
});
initGame(window.game_url);
}
else {
// Uses a hider game data to create a game
$.getJSON(
`https://thor-turk.s3-us-west-2.amazonaws.com/hide-n-seek/data/hider/${getParams['config']}`,
function(config) {
console.log("------ Config", config);
gameConfig = config;
$("#instructions-seeker").show();
$("#message-text").show();
// gameConfig['agentPosition']
let objectHtml = `<strong class="important-text">${gameConfig.object_type}</strong>`;
$("#instruction-text").html(`You have to find a ${objectHtml}`);
$("#instruction-2").html(`Move around in the room, open drawers and cabinets to look for a ${objectHtml}.`);
$("#instruction-3").html('Click on it once you have found it.');
$("#instruction-4").html(`If you cannot find the ${objectHtml} after some time, you can click the <strong class="red-text">Give Up</strong> button above.`);
getParams['object'] = gameConfig['object_type'];
getParams['variation'] = gameConfig['object_variation'];
outputData.object_type = gameConfig['object_type'];
outputData.object_variation = gameConfig['object_variation'];
$("#finish-hit").click((e) => {
outputData['success'] = false;
submitHit(lastMetadadta);
}).text("Give Up").toggleClass("giveup-btn");
$("#reset-hit").hide();
setTimeout(() => {
$("#finish-hit").attr("disabled", false);
}, 'giveUpEnableSeconds' in getParams ? parseInt(getParams['giveUpEnableSeconds']) * 1000 : 30000);
// Starting point for unity
initGame(window.game_url);
});
}
})
.fail(function( jqxhr, settings, exception ) {
console.error( "Triggered ajaxError handler.", exception);
});
}
);