File size: 12,532 Bytes
09a6f7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// All available actions
export default {

    /**
     * Adds the given environment to the specified set.
     * @param context {Store}
     * @param payload {{set: string, env: Object}}
     */
    addEnv(context, payload){
        context.commit('addEnv', payload);
    },

    /**
     * Removes the environment of the given index from the custom set.
     * @param context {Store}
     * @param payload {number} - Index of the environment to remove
     */
    deleteEnv(context, payload){
        context.commit('deleteEnv', payload);
    },

    /**
     * Loads the given environment into the simulation.
     * @param context {Store}
     * @param payload {Object} - Environment to load
     */
    loadEnv(context, payload){

        // Resets drawing mode
        drawing_canvas.clear();
        window.terrain = {
            ground: [],
            ceiling: []
        };

        window.ground = [...payload.terrain.ground];
        window.ceiling = [...payload.terrain.ceiling];

        // Updates the values of the terrain sliders
        for(let param in payload.terrain.parkourConfig){
            context.commit('updateParkourConfig', {name: param, value: payload.terrain.parkourConfig[param]});
        }
        for(let param in payload.terrain.creepersConfig){
            context.commit('updateParkourConfig', {name: param, value: payload.terrain.creepersConfig[param]});
        }

        // Replaces previous agents by the ones of the env
        while (context.state.agents.length > 0){
            context.commit('deleteAgent', {index: 0});
        }
        for(let agent of payload.agents){
            context.commit('addAgent', {
                morphology: agent.morphology,
                name: agent.name,
                path: agent.path,
                init_pos: agent.init_pos
            });
        }

        // Initializes the new environment
        context.commit('init_default', {});
    },

    /**
     * Initializes the game with default parameters.
     * @param context {Store}
     * @param payload
     */
    init_default(context, payload) {
        context.commit('init_default', {});
    },

    /**
     * Changes the value of the given terrain parameter.
     * @param context {Store}
     * @param payload {{name: string, value: number}} - Name and value of the terrain parameter to change
     */
    changeParkourConfig(context, payload){

        // Case one of the cppn dims is changed : aligns the terrain with the startpad
        if(['dim1', 'dim2', 'dim3'].indexOf(payload.name) != -1){

            window.ground = [];
            window.ceiling = [];
            window.align_terrain = {
                align: true,
                ceiling_offset: null, // aligns the ceiling with the startpad
                ground_offset: null, // aligns the ground with the startpad
                smoothing: window.game.env.TERRAIN_CPPN_SCALE // previous smoothing
            };
        }
        // Case smoothing, water_level or creepers is changed
        else{
            window.align_terrain = {
                align: true,
                ceiling_offset: window.align_terrain.ceiling_offset, // keeps the same
                ground_offset: window.align_terrain.ground_offset, // keeps the same
                smoothing: window.game.env.TERRAIN_CPPN_SCALE // previous smoothing
            };
        }

        // Updates the parameter
        context.commit('updateParkourConfig', payload);

        // Generates the terrain if drawing mode is active
        if(context.state.drawingModeState.drawing){
            context.commit('generateTerrain', true);
        }

        // Resets drawing mode
        drawing_canvas.clear();
        window.terrain = {
            ground: [],
            ceiling: []
        };

        // Resets the simulation and deselects agent
        context.commit('resetSimulation', {keepPositions: true});
        context.commit('selectAgent', -1);
    },

    /**
     * Changes the value of the given switch parameter.
     * @param context {Store}
     * @param payload {{name: string, value: number}} - Name and value of the switch to toggle
     */
    toggleSwitch(context, payload) {
        switch (payload.name) {
            case 'drawJoints':
                context.commit('drawJoints', payload.value);
                break;
            case 'drawLidars':
                context.commit('drawLidars', payload.value);
                break;
            case 'drawNames':
                context.commit('drawNames', payload.value);
                break;
            case 'drawObservation':
                context.commit('drawObservation', payload.value);
                break;
            case 'drawReward':
                context.commit('drawReward', payload.value);
                break;
        }
    },

    /**
     * Changes the status of the simulation.
     * @param context {Store}
     * @param payload {}
     */
    toggleRun(context, payload) {
        const status = context.state.simulationState.status;
        switch (status) {
            case 'init':
                context.commit('startSimulation', {});
                break;
            case 'running':
                context.commit('pauseSimulation', {});
                break;
            case 'paused':
                context.commit('startSimulation', {});
                break;
        }
    },

    /**
     * Resets the simulation.
     * @param context {Store}
     * @param payload {}
     */
    resetSimulation(context, payload) {
        // Updates the terrain alignment
        window.align_terrain = {
            align: true, // aligns the terrain with the startpad
            ceiling_offset: window.ceiling.length > 0 ? window.game.env.ceiling_offset - window.ceiling[0].y : null,
            ground_offset: window.ground.length > 0 ? window.ground[0].y : null, // first ground y value
            smoothing: window.game.env.TERRAIN_CPPN_SCALE // smoothing of the current terrain
        };

        context.commit('selectAgent', {index: -1});
        context.commit('resetSimulation', {keepPositions: false});
    },

    /**
     * Adds the given agent to the simulation.
     * @param context {Store}
     * @param payload {{morphology: string, name: string, path: string, init_pos: {x: number, y: number}}}
     */
    addAgent(context, payload) {
        // Pauses the simulation if it is running
        if (context.state.simulationState.status == 'running') {
            context.commit('pauseSimulation', {});
        }
        context.commit('addAgent', payload);
    },

    /**
     * Deletes the agent of the given index from the simulation.
     * @param context {Store}
     * @param payload {{index: number}}
     */
    deleteAgent(context, payload) {
        context.commit('deleteAgent', payload);
    },

    /**
     * Sets the initial position of the agent of the given index.
     * @param context {Store}
     * @param payload {{index: number, init_pos: {x: number, y: number}}}
     */
    setAgentInitPos(context, payload){
        context.commit('setAgentInitPos', payload);
    },

    /**
     * Selects the agent of the given index.
     * @param context {Store}
     * @param payload {{index: number}}
     */
    selectAgent(context, payload){
        context.commit('selectAgent', payload);
    },

    /**
     * Follows or not the agent of the given index according to the given boolean.
     * @param context {Store}
     * @param payload {{index: number, value: boolean}}
     */
    followAgent(context, payload) {
        context.commit('followAgent', payload);
    },

    /**
     * Renames the agent of the given index with the given string value.
     * @param context {Store}
     * @param payload {{index: number, value: string}}
     */
    renameAgent(context, payload){
        context.commit('renameAgent', payload);
    },

    /**
     * Selects the seed option of the given index for the given morphology.
     * @param context {Store}
     * @param payload {{morphology: string, index: number}}
     */
    selectSeedIdx(context, payload) {
        context.commit('selectSeedIdx', payload);
    },

    /**
     * Adds the given morphology with the given policy seeds to the list of morphologies.
     * @param context {Store}
     * @param payload {{morphology: string, seeds: []}}
     */
    addMorphology(context, payload) {
        context.commit('addMorphology', payload);
        // Selects the first seed option
        context.commit('selectSeedIdx', {morphology: payload.morphology, index: 0});
    },

    /**
     * Changes the active tab.
     * @param context {Store}
     * @param payload {string} - Name of the tab to activate
     */
    switchTab(context, payload) {

        // Switch from 'Draw Yourself!' to another tab
        if(context.state.activeTab == 'draw_yourself'){
            if(payload != 'draw_yourself' && context.state.drawingModeState.drawing) {
                // Generates the terrain from the drawing
                context.commit('generateTerrain', true);
            }
        }
        // Switch to 'Draw Yourself!' from another tab
        else if(payload == 'draw_yourself'){
            // Generates the drawing from the terrain
            context.commit('generateTerrain', false);
        }
        context.commit('switchTab', payload);
    },

    /**
     * Activates or deactivates the ground drawing mode.
     * @param context {Store}
     * @param payload {boolean}
     */
    drawGround(context, payload) {
        context.commit('drawGround', payload);
    },

    /**
     * Activates or deactivates the ceiling drawing mode.
     * @param context {Store}
     * @param payload {boolean}
     */
    drawCeiling(context, payload) {
        context.commit('drawCeiling', payload);
    },

    /**
     * Activates or deactivates the erasing mode.
     * @param context {Store}
     * @param payload {boolean}
     */
    erase(context, payload) {
        context.commit('erase', payload);
    },

    /**
     * Activates or deactivates the drawing mode of the given asset.
     * @param context {Store}
     * @param payload {{name: string, value: boolean}}
     */
    drawAsset(context, payload){
        // Only supports circle asset for now
        switch (payload.name){
            case 'circle':
                context.commit('drawCircle', payload.value);
                break;
        }
    },

    /**
     * Handles clicks outside the canvas when drawing.
     * @param context {Store}
     * @param payload {}
     */
    deselectDrawingButtons(context, payload){
        context.commit('deselectDrawingButtons', payload);
    },

    /**
     * Resets drawing mode.
     * @param context {Store}
     * @param payload {}
     */
    clear(context, payload) {
        context.commit('clear', payload);
        context.commit('generateTerrain', false);
    },

    /**
     * Generates the terrain from the drawing (true) or vice-versa (false).
     * @param context {Store}
     * @param payload {boolean}
     */
    generateTerrain(context, payload){
        context.commit('generateTerrain', payload);
    },

    refreshDrawing(context, payload){
        context.commit('refreshDrawing', payload);
    },

    /**
     * Starts (true) or exits (false) intro tour.
     * @param context {Store}
     * @param payload {boolean}
     */
    setIntroTour(context, payload){
        context.commit('setIntroTour', payload);
    },

    /**
     * Sets the language.
     * @param context {Store}
     * @param payload {string}
     */
    setLanguage(context, payload){
        context.commit('setLanguage', payload);

        // Resets the hints options of the intro
        if(window.introTour != null){
            window.introTour.setOptions({
                hintButtonLabel: window.lang_dict[context.state.language]['introHints']['buttonLabel'],
                hints: [
                    {
                        hint: window.lang_dict[context.state.language]['introHints']['tips'],
                        element: document.querySelector('#canvas_container'),
                        hintPosition: 'top-right',
                    }
                ]
            });

            // Removes the div element of the hints
            let introDiv = document.getElementsByClassName("introjs-hints")[0];
            introDiv.parentNode.removeChild(introDiv);

            // Recreates the hints
            window.introTour.addHints();
        }

        window.game.env.render();
    }
};