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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import store from './js/ui_state/store/index.js';
import MorphologiesList from './js/ui_state/components/morphologies_list.js';
import AgentsList from './js/ui_state/components/agents_list.js';
import MainButtons from './js/ui_state/components/main_buttons.js';
import ParkourConfig from './js/ui_state/components/parkour_config.js';
import DrawingMode from "./js/ui_state/components/drawing_mode.js";
import AdvancedOptions from "./js/ui_state/components/advanced_options.js";
import EnvsSet from "./js/ui_state/components/envs_set.js";
import GlobalElements from "./js/ui_state/components/global_elements.js";
import AboutTab from "./js/ui_state/components/about_tab.js";

/**
 * Opens the given modal.
 * @param modal {HTMLDivElement}
 */
window.openModal = (modal) => {
    modal.style.backgroundColor = 'rgba(0,0,0,0.5)';
    modal.style.display = "block";
    modal.classList.add('show');
}

/**
 * Closes the given modal and clears the text fields.
 * @param modal {HTMLDivElement}
 */
window.closeModal = (modal) => {
    modal.classList.remove('show');
    modal.style.display = 'none';
    modal.querySelectorAll('.text-field').forEach((span, index) => {
       span.value = "";
    });
}

// Save env modal setup
const saveEnvModal = document.querySelector('#saveEnvModal');
saveEnvModal.querySelectorAll('.btn').forEach((span, index) => {
    span.addEventListener('click', () => {

        // If the confirm button is clicked
        if(span.getAttribute('id') == "save-confirm-btn"){

            // Gets the name and description values
            let name = saveEnvModal.querySelector('#env-name').value;
            if(name == ""){
                name = "Custom Environment " + store.state.envsSets.customEnvsSet.length;
            }
            let description = saveEnvModal.querySelector('#env-description').value;

            // Saves the current position of the agents
            for(let i = 0; i < store.state.agents.length; i++){
                store.dispatch('setAgentInitPos', {index: i, init_pos: window.game.env.agents[i].agent_body.reference_head_object.GetPosition().Clone()});
            }

            // Adjusts the zoom and scroll to capture the thumbnail
            let previous_zoom = window.game.env.zoom;
            let previous_scroll = [...window.game.env.scroll];
            window.game.env.set_zoom(THUMBNAIL_ZOOM);
            window.game.env.set_scroll(null, THUMBNAIL_SCROLL_X, 0);
            window.game.env.render();

            // Creates the state of the current env
            let env = {
                terrain: {
                    ground: [...window.ground],
                    ceiling: [...window.ceiling],
                    parkourConfig: Object.assign({}, store.state.parkourConfig.terrain),
                    creepersConfig: Object.assign({}, store.state.parkourConfig.creepers)
                },
                agents: [...store.state.agents],
                description: {
                    name: name,
                    text: description
                },
                // Captures the canvas to create the thumbnail of the env
                image: window.canvas.canvas.toDataURL("image/png").replace("image/png", "image/octet-stream")
            };

            // Adds the env to the custom set
            store.dispatch('addEnv',{set: 'custom', env: env});

            // Sets back the zoom and scroll to the previous values
            window.game.env.set_zoom(previous_zoom);
            window.game.env.set_scroll(null, previous_scroll[0], previous_scroll[1]);
            window.game.env.render();
        }

        closeModal(saveEnvModal);
    });
});

// Main buttons setup
const runButton = document.querySelector("#runButton");
runButton.addEventListener('click', () => {
    store.dispatch('toggleRun', {});
});
const resetButton = document.querySelector("#resetButton");
resetButton.addEventListener('click', () => {
    store.dispatch('resetSimulation', {});
});
const saveEnvButton = document.querySelector('#saveEnvButton');
saveEnvButton.addEventListener('click', () => {
    openModal(saveEnvModal);
});
const mainButtonsInstance = new MainButtons();
mainButtonsInstance.render();

// Morphologies list setup
const morphologiesListInstance = new MorphologiesList();
morphologiesListInstance.render();

// Agents list setup
const agentListInstance = new AgentsList();
agentListInstance.render();

// Environments set setup
const envsSetInstance = new EnvsSet();
envsSetInstance.render();

// Parkour config parameters setup
const dim1SliderElement = document.querySelector("#dim1Slider")
dim1SliderElement.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "dim1",
        value: parseFloat(dim1SliderElement.value)
    });
});
const dim2SliderElement = document.querySelector("#dim2Slider")
dim2SliderElement.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "dim2",
        value: parseFloat(dim2SliderElement.value)
    });
});
const dim3SliderElement = document.querySelector("#dim3Slider")
dim3SliderElement.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "dim3",
        value: parseFloat(dim3SliderElement.value)
    });
});
const smoothingSliderElement = document.querySelector("#smoothingSlider")
smoothingSliderElement.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "smoothing",
        value: parseFloat(smoothingSliderElement.value)
    });
});
const waterSliderElement = document.querySelector("#waterSlider")
waterSliderElement.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "waterLevel",
        value: parseFloat(waterSliderElement.value)
    });
});
const creepersWidthSlider = document.querySelector("#creepersWidthSlider");
creepersWidthSlider.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "width",
        value: parseFloat(creepersWidthSlider.value)
    });
});
const creepersHeightSlider = document.querySelector("#creepersHeightSlider");
creepersHeightSlider.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "height",
        value: parseFloat(creepersHeightSlider.value)
    });
});
const creepersSpacingSlider = document.querySelector("#creepersSpacingSlider");
creepersSpacingSlider.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "spacing",
        value: parseFloat(creepersSpacingSlider.value)
    });
});
const creepersTypeSelect = document.querySelector("#creepersType");
creepersTypeSelect.addEventListener('input', () => {
    store.dispatch('changeParkourConfig', {
        name: "type",
        value: creepersTypeSelect.value
    });
});
const parkourConfigInstance = new ParkourConfig();
parkourConfigInstance.render();

// Tabs buttons setup
const gettingStartedBtn = document.querySelector('#getting-started-btn');
gettingStartedBtn.addEventListener('click', () => {
    store.dispatch('switchTab', 'getting_started');
})
const parkourCustomTab = document.querySelector('#parkour-custom-btn');
parkourCustomTab.addEventListener('click', () => {
    // Shows the "Draw Yourself!" subtab when opening the "Parkour Customization" tab
    if(store.state.activeTab != 'draw_yourself' && store.state.activeTab != 'proc_gen'){
        let drawTabBtn = document.querySelector('#draw-tab-btn');
        let drawYourselfTab = new bootstrap.Tab(drawTabBtn);
        drawYourselfTab.show();
        store.dispatch('switchTab', 'draw_yourself');
    }
});
const drawYourselfBtn = document.querySelector('#draw-tab-btn');
drawYourselfBtn.addEventListener('click', () => {
    store.dispatch('switchTab', 'draw_yourself');
});
const procGenBtn = document.querySelector('#proc-gen-tab-btn');
procGenBtn.addEventListener('click', () => {
    store.dispatch('switchTab', 'proc_gen');
});
const advancedOptionsBtn = document.querySelector('#advanced-options-btn');
advancedOptionsBtn.addEventListener('click', () => {
    store.dispatch('switchTab', 'advanced_options');
});
const aboutBtn = document.querySelector('#about-btn');
aboutBtn.addEventListener('click', () => {
    store.dispatch('switchTab', 'about');
});

// Drawing Mode buttons setup
const drawGroundButton = document.querySelector('#drawGroundButton');
drawGroundButton.addEventListener('click', () => {
    store.dispatch('drawGround', !store.state.drawingModeState.drawing_ground);
});
const drawCeilingButton = document.querySelector('#drawCeilingButton');
drawCeilingButton.addEventListener('click', () => {
    store.dispatch('drawCeiling', !store.state.drawingModeState.drawing_ceiling);
});
const eraseButton = document.querySelector('#eraseButton');
eraseButton.addEventListener('click', () => {
    store.dispatch('erase', !store.state.drawingModeState.erasing);
});
const clearButton = document.querySelector('#clearButton');
clearButton.addEventListener('click', () => {
    store.dispatch('clear', {});
});
const generateTerrainButton = document.querySelector('#generateTerrainButton');
generateTerrainButton.addEventListener('click', () => {
    store.dispatch('generateTerrain', store.state.drawingModeState.drawing);
});
const drawingModeInstance = new DrawingMode();
drawingModeInstance.render();

// Advanced Options setup
const drawJointsSwitch = document.querySelector("#drawJointsSwitch");
drawJointsSwitch.addEventListener('input', () => {
    store.dispatch('toggleSwitch', {name: 'drawJoints', value: drawJointsSwitch.checked} );
});
const drawLidarsSwitch = document.querySelector("#drawLidarsSwitch");
window.draw_lidars = true;
drawLidarsSwitch.addEventListener('input', () => {
    store.dispatch('toggleSwitch', {name: 'drawLidars', value: drawLidarsSwitch.checked});
});
const drawNamesSwitch = document.querySelector("#drawNamesSwitch");
window.draw_names = true;
drawNamesSwitch.addEventListener('input', () => {
    store.dispatch('toggleSwitch', {name: 'drawNames', value: drawNamesSwitch.checked});
});
const drawObservationSwitch = document.querySelector("#drawObservationSwitch");
drawObservationSwitch.addEventListener('input', () => {
    store.dispatch('toggleSwitch', {name: 'drawObservation', value: drawObservationSwitch.checked});
});
const drawRewardSwitch = document.querySelector("#drawRewardSwitch");
drawRewardSwitch.addEventListener('input', () => {
    store.dispatch('toggleSwitch', {name: 'drawReward', value: drawRewardSwitch.checked});
});
const circleAssetButton = document.querySelector('#circleAssetButton');
circleAssetButton.addEventListener('click', () => {
    store.dispatch('drawAsset', {name: 'circle', value: !store.state.advancedOptionsState.assets.circle});
});
const advancedOptionsInstance = new AdvancedOptions();
advancedOptionsInstance.render();

// Language set up
const langSelect = document.querySelector("#langSelect");
langSelect.addEventListener('input', () => {
    store.dispatch('setLanguage', langSelect.value);
});

// About tab set up
const aboutTabInstance = new AboutTab();
aboutTabInstance.render();

// Global elements set up
const globalElementsInstance = new GlobalElements();
globalElementsInstance.render();

/*
 * Fetches the available morphologies and policies from the JSON file
 */
fetch('https://huggingface.co/api/models?filter=teach-my-agent-parkour')
    .then(resp => resp.text().then(body => {
        return JSON.parse(body);
    }))
    .then(models => {
        let morphologies = {}
        let promises = []
        models.forEach(model => {
            promises.push(
                fetch('https://huggingface.co/'+model.id+'/raw/main/ta-config.json')
                .then(result => result.text().then(body => {
                    let policy = JSON.parse(body);
                    if(!(policy["morphology"] in morphologies)){
                        morphologies[policy["morphology"]] = {
                            "morphology": policy["morphology"],
                            "seeds": []
                        }
                    }

                    morphologies[policy["morphology"]]["seeds"].push({
                        "seed": policy["seed"],
                        "name": policy["name"],
                        "path": 'https://huggingface.co/'+model.id+'/resolve/main'
                    })
                }))
            ); 
        });

        Promise.all(promises).then(() => {
            for (const [key, value] of Object.entries(morphologies)) {
                store.dispatch('addMorphology', {
                    morphology: value["morphology"],
                    seeds: value["seeds"].map((seed, index) => {
                        seed["idx"] = index;
                        return seed;
                    })
                });
            }
        });
    });

/*
 * Fetches the base environments from the JSON file.
 * The JSON file must contain a list of the names of all the files contained in the 'base_envs_set' folder.
 */
fetch('./base_envs_set.json')
    .then(resp => resp.text().then(body => {
        return JSON.parse(body);
    }))
    .then(data => data['filenames'].forEach(filename => {

        // Fetches each JSON env file and parses them to get the corresponding environment
        fetch('./base_envs_set/' + filename)
            .then(resp => resp.text().then(body => {
                let env = JSON.parse(body);

                // Adds the environment contained in the JSON file to the base set
                store.dispatch('addEnv',{set: 'base', env: env});
            }))
    }));

/* Utility functions that require access to 'store' */

/**
 * Wrapper for init_game() with default parameters.
 */
window.init_default = () => {
    store.dispatch('init_default', {});
}

/**
 * Indicates whether the drawing is active.
 * @returns {boolean}
 */
window.is_drawing = () => {
    return store.state.drawingModeState.drawing;
}

/**
 * Indicates whether the drawing_ground is active.
 * @returns {boolean}
 */
window.is_drawing_ground = () => {
    return store.state.drawingModeState.drawing_ground;
}

/**
 * Indicates whether the drawing_ceiling is active.
 * @returns {boolean}
 */
window.is_drawing_ceiling = () => {
    return store.state.drawingModeState.drawing_ceiling;
}

/**
 * Indicates whether the erasing is active.
 * @returns {boolean}
 */
window.is_erasing = () => {
    return store.state.drawingModeState.erasing;
}

/**
 * Indicates whether the drawing_circle is active.
 * @returns {boolean}
 */
window.is_drawing_circle = () => {
    return store.state.advancedOptionsState.assets.circle;
}

/**
 * Generates the terrain from the drawing (true) or vice-versa (false).
 * @param payload {boolean}
 */
window.generateTerrain = (payload) => {
    store.dispatch('generateTerrain', payload);
}

/**
 * Re-draws the drawing canvas.
 */
window.refresh_drawing = () => {
    store.dispatch('refreshDrawing', {});
}

/**
 * Loads the default environment (Flat Parkour).
 */
window.loadDefaultEnv = () => {
    let defaultEnv = store.state.envsSets.baseEnvsSet.find(env => env.description["EN"].name.split(" ")[0] == "Flat");
    store.dispatch('loadEnv', defaultEnv != null ? defaultEnv : store.state.envsSets.baseEnvsSet[0]);
}

/**
 * Handles mouse clicks outside the canvas.
 */
window.deselectDrawingButtons = () => {
    store.dispatch('deselectDrawingButtons', {});
}

/**
 * Handles agent selection according to the given index.
 * @param index {number} - Index of the agent to select in the list of agents
 */
window.set_agent_selected = (index) => {
    store.dispatch('selectAgent', {index: index});
}

/**
 * Handles agent following according to the given index.
 * @param index {number} - Index of the agent to select in the list of agents
 */
window.set_agent_followed = (index) => {
    store.dispatch('followAgent', {index: index});
}

/**
 * Handles agent deletion.
 * @param agent {Object} - Agent to delete
 */
window.delete_agent = (agent) => {
    store.dispatch('deleteAgent', {index: window.game.env.agents.indexOf(agent)});
}

/**
 * Returns the current language.
 * @return {string}
 */
window.get_language = () => {
    return store.state.language;
}

/**
 * Sets up and starts language selection with IntroJs.
 */
window.langIntroSetUp = () => {
    window.langIntro = introJs();

    // Called when langIntro is exited: sets up and starts the guide tour
    window.langIntro.onexit(function (){
        window.introTourSetUp();
    });

    window.langIntro.setOptions({
        exitOnOverlayClick: false,
        showBullets: false,
        doneLabel: "Ok",
        steps: [
            {
                title: "Choose a language:",
                intro: `<select id="langSelectIntro" class="form-select w-auto">
                        <option value="EN">🇬🇧 English</option>
                        <option value="FR">🇫🇷 Français</option>
                      </select>`
            },
        ]
    });

    // Starts the language selection intro
    window.langIntro.start();

    // Sets the language on select input
    const langSelectIntro = document.querySelector('#langSelectIntro');
    langSelectIntro.addEventListener('input', () => {
        store.dispatch('setLanguage', langSelectIntro.value);
    });
}

/**
 * Sets up guide tour with IntroJs.
 */
window.introTourSetUp = () => {

    store.dispatch('setIntroTour', true);

    // Guide tour and hints manager
    window.introTour = introJs();

    // Called when the tour is exited: enables save env button and show the hints
    window.introTour.onexit(function (){
        store.dispatch('setIntroTour', false);
    });

    // Shows hints when a hint is closed so that they are always available
    window.introTour.onhintclose(function (){
        window.introTour.showHints();
    });

    let tour_dict = window.lang_dict[store.state.language]['introTour'];

    // Sets up the intro options
    window.introTour.setOptions({

        hidePrev: true,
        exitOnOverlayClick: false,
        nextLabel: tour_dict['nextLabel'],
        prevLabel: tour_dict['prevLabel'],
        doneLabel: tour_dict['doneLabel'],

        // Steps of the guide tour
        steps: [
            {
                title: tour_dict['welcomeTitle'],
                intro: tour_dict['welcomeText'],
                disableInteraction: true,
            },
            {
                title: tour_dict['viewportTitle'],
                intro: tour_dict['viewportText'],
                element: document.querySelector('#canvas_container'),
                disableInteraction: false,
                position: "bottom"
            },
            {
                title: tour_dict['runTitle'],
                intro: tour_dict['runText'],
                element: document.querySelector('#canvas-and-main-buttons'),
                disableInteraction: false
            },
            {
                title: tour_dict['baseEnvsTitle'],
                intro: tour_dict['baseEnvsText'],
                element: document.querySelector('#baseEnvsSet'),
                disableInteraction: true,
            },
            {
                title: tour_dict['morphologiesTitle'],
                intro: tour_dict['morphologiesText'],
                element: document.querySelector('#agents-selection'),
                disableInteraction: true,
            },
            {
                title: tour_dict['agentsListTitle'],
                intro: tour_dict['agentsListText'],
                element: document.querySelector('#agents_list_container'),
                disableInteraction: true,
            },
            {
                title: tour_dict['customEnvsTitle'],
                intro: tour_dict['customEnvsText'],
                element: document.querySelector('#customSetSection'),
                disableInteraction: true,
            },
            {
                title: tour_dict['furtherTitle'],
                intro: tour_dict['furtherText'],
                element: document.querySelector('#tabs-buttons'),
                disableInteraction: true,
            }
        ],

        // Hints buttons
        hintButtonLabel: window.lang_dict[store.state.language]['introHints']['buttonLabel'],
        hints: [
            {
                hint: window.lang_dict[store.state.language]['introHints']['tips'],
                element: document.querySelector('#canvas_container'),
                hintPosition: 'top-right',
            }
        ]
    });

    // Starts the guide tour
    window.introTour.start();
}