File size: 27,080 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
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/* GLOBAL VARIABLES */

window.erasing_radius = 15;
window.asset_size = 8;

// Lists of points {x, y} composing the terrain shapes
window.ground = [];
window.ceiling = [];

// Lists of raw points {x, y} drawn by the user for the terrain shapes
window.terrain = {
    ground: [],
    ceiling: []
};

// Parameters to handle the alignment of the terrain to the startpad according to the situation
window.align_terrain = {
    align: true,
    ceiling_offset: null,
    ground_offset: null,
    smoothing: null
};

/* INIT FUNCTIONS */

/**
 * Initializes the game.
 * @param cppn_input_vector {Array} - 3-dimensional array that encodes the CPPN
 * @param water_level {number}
 * @param creepers_width {number}
 * @param creepers_height {number}
 * @param creepers_spacing {number}
 * @param smoothing {number}
 * @param creepers_type {boolean}
 * @param ground {Array} - List of points {x, y} composing the ground
 * @param ceiling {Array} - List of points {x, y} composing the ceiling
 * @param align {Object}
 * @param zoom {number} - Zoom to apply to the environment
 * @param scroll {{x: number, y:number}} - Scroll to apply to the environment
 */
function init_game(cppn_input_vector, water_level, creepers_width, creepers_height, creepers_spacing,
              smoothing, creepers_type, ground, ceiling, align, zoom=null, scroll=null) {

    let agents = {
        morphologies: [],
        policies: [],
        positions: []
    }

    // Pauses the game if it already exists and gets the information about the running agents
    if(window.game != null){
        window.game.pause();
        agents.morphologies = [...window.game.env.agents.map(a => a.morphology)];
        agents.policies = [...window.game.env.agents.map(a => a.policy)];
        agents.positions = [...window.game.env.agents.map(agent => agent.agent_body.reference_head_object.GetPosition())];
    }
    window.game = new Game(agents, cppn_input_vector, water_level, creepers_width, creepers_height,
                            creepers_spacing, smoothing, creepers_type, ground, ceiling, align);
    window.set_agent_selected(-1);
    window.asset_selected = null;

    if(zoom == null){
        window.game.env.set_zoom(INIT_ZOOM);
    }
    else {
        window.game.env.set_zoom(zoom);
    }

    if(scroll == null){
        window.game.env.set_scroll(window.agent_selected, INIT_SCROLL_X, 0);
    }
    else{
        window.game.env.set_scroll(window.agent_selected, scroll[0], scroll[1]);
    }
    window.game.env.render();
}

/**
 * Indicates if the creepers type is 'Swingable' or not.
 * @returns {boolean}
 */
function getCreepersType() {
    return document.getElementById("creepersType").value == 'Swingable';
}

/**
 * First function called after the code is entirely loaded.
 * Loads the model of the CPPN, initializes the game by default, loads the default environmnent and starts the language selection.
 * @returns {Promise<void>}
 */
async function onLoadInit() {
    window.cppn_model = await tf.loadGraphModel('./js/CPPN/weights/same_ground_ceiling_cppn/tfjs_model/model.json');
    window.init_default();
    window.loadDefaultEnv();
    window.langIntroSetUp();
}

// Calls onLoadInit() when all the files are loaded
window.addEventListener("load", onLoadInit, false);

/* IN-CANVAS MOUSE INTERACTIONS */

/**
 * Converts the given position relative to the canvas to the environment scale.
 * @param x_pos {number} - X-coordinate inside the canvas.
 * @param y_pos {number} - Y-coordinate inside the canvas.
 * @returns {{x: number, y: number}} - Position inside the environment.
 */
function convertPosCanvasToEnv(x_pos, y_pos){
    let x = Math.max(-window.canvas.width * 0.01, Math.min(x_pos, window.canvas.width * 1.01));
    let y = Math.max(0, Math.min(y_pos, window.canvas.height));

    x +=  window.game.env.scroll[0];
    y = -(y - window.game.env.scroll[1]);

    x = x / (window.game.env.scale * window.game.env.zoom);
    y = y / (window.game.env.scale * window.game.env.zoom);

    y += (1 - window.game.env.scale * window.game.env.zoom) * RENDERING_VIEWER_H/(window.game.env.scale * window.game.env.zoom)
        + (window.game.env.zoom - 1) * (window.game.env.ceiling_offset)/window.game.env.zoom * 1/3 + RENDERING_VIEWER_H;

    return {x: x, y: y};
}

/**
 * Converts the given position relative to the environment to the canvas scale.
 * @param x_pos {number} - X-coordinate inside the environment.
 * @param y_pos {number} - Y-coordinate inside the environment.
 * @returns {{x: number, y: number}} - Position inside the canvas.
 */
function convertPosEnvToCanvas(x_pos, y_pos){
    let x = x_pos * window.game.env.scale * window.game.env.zoom - window.game.env.scroll[0];
    let y = window.game.env.scroll[1] - (y_pos - RENDERING_VIEWER_H) * window.game.env.scale * window.game.env.zoom
        + (1 - window.game.env.scale * window.game.env.zoom) * RENDERING_VIEWER_H
        + (window.game.env.zoom - 1) * window.game.env.ceiling_offset * window.game.env.scale * 1/3;

    return {x: x, y: y};
}

/**
 * Checks if the given position is inside the given body.
 * Used for clicking on assets.
 * @param pos {{x: number, y: number}}
 * @param body {b2Body} - A Box2D body
 * @returns {boolean}
 */
function isPosInsideBody(pos, body){
    let shape = body.GetFixtureList().GetShape();

    if(shape.m_type == b2.Shape.e_circle){
        let center = body.GetWorldCenter();
        return Math.pow(center.x - pos.x, 2) + Math.pow(center.y - pos.y, 2) <= Math.pow(shape.m_radius, 2);
    }
}

/**
 * Handles actions when mouse is pressed.
 */
function mousePressed(){

    // Hides all the tooltips when mouse pressed
    document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el, index) => {
        let tooltip = bootstrap.Tooltip.getInstance(el);
        tooltip.hide();
    });

    // Case mouse is pressed inside the canvas
    if(mouseX >= 0 && mouseX <= window.canvas.width
        && mouseY >= 0 && mouseY <= window.canvas.height){

        // Stores the current position of the mouse, used when dragging
        window.prevMouseX = mouseX;
        window.prevMouseY = mouseY;

        // Creates a circle asset at the mouse position and render the environment
        if(window.is_drawing_circle()){
            let mousePos = convertPosCanvasToEnv(mouseX, mouseY);
            window.game.env.create_circle_asset(mousePos, window.asset_size * 2 / window.game.env.scale);

            if(window.agent_selected != null){
                window.agent_selected.is_selected = false;
                window.set_agent_selected(-1);
            }
            window.game.env.render();
        }

        // Handles agents and assets selection
        else if(!window.is_drawing()){
            let mousePos = convertPosCanvasToEnv(mouseX, mouseY);

            // Selects an agent in the canvas if the mouse is clicked over its body
            let one_agent_touched = false;
            for(let i = 0; i < window.game.env.agents.length; i++){
                let agent = window.game.env.agents[i];

                // Checks if the agent is touched by the mouse
                let is_agent_touched = agent.agent_body.isPosInside(mousePos);

                // If the agent is touched and not selected yet, it is now selected and all other agents are deselected
                if(is_agent_touched){
                    one_agent_touched = true;

                    if(!agent.is_selected) {
                        agent.is_selected = true;
                        window.set_agent_selected(i);
                        for (let other_agent of window.game.env.agents) {
                            if (other_agent != agent) {
                                other_agent.is_selected = false;
                            }
                        }
                    }
                    break;
                }
                // If the agent is not touched it is deselected
                else {
                    agent.is_selected = false;
                }
            }

            // If no agent is touched, the selected agent is set to null
            if(!one_agent_touched && window.agent_selected != null){
                window.set_agent_selected(-1);
            }

            // Selects an asset in the canvas if the mouse is clicked over its body and no agent has been touched
            if(!one_agent_touched){
                let one_asset_touched = false;
                for(let asset of window.game.env.assets_bodies){

                    // Checks if the asset is touched by the mouse
                    let is_asset_touched = isPosInsideBody(mousePos, asset.body);

                    // If the asset is touched and not selected yet, it is now selected and all other assets are deselected
                    if(is_asset_touched){
                        one_asset_touched = true;

                        if(!asset.is_selected){
                            asset.is_selected = true;
                            window.asset_selected = asset;
                            for(let other_asset of window.game.env.assets_bodies){
                                if(other_asset != asset){
                                    other_asset.is_selected = false;
                                }
                            }
                            break;
                        }
                    }
                    // If the asset is not touched it is deselected
                    else if(!is_asset_touched){
                        asset.is_selected = false;
                    }
                }

                // If no asset is touched, the selected asset is set to null
                if(!one_asset_touched && window.asset_selected != null){
                    window.asset_selected = null;
                }
            }

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

// Handles clicks outside canvas when drawing (deselect drawing buttons)
document.addEventListener('mousedown', (event) => {
    if(window.is_drawing() || window.is_drawing_circle()){
        let canvas_id = "#" + window.canvas.canvas.id;

        // Elements that can be clicked without deselecting drawing buttons: canvas + ground, ceiling, erase buttons
        let authorized_elements = [
            document.querySelector(canvas_id),
            document.querySelector('#drawGroundButton'),
            document.querySelector('#drawCeilingButton'),
            document.querySelector('#eraseButton')
        ];

        // If
        if(authorized_elements.indexOf(event.target) == -1) {
            window.deselectDrawingButtons();
        }
    }
});

/**
 * Handles actions when mouse is dragged.
 * @returns {boolean}
 */
function mouseDragged(){

    // Case mouse is dragged inside the canvas
    if(mouseX >= 0 && mouseX <= window.canvas.width
        && mouseY >= 0 && mouseY <= window.canvas.height) {

        // DRAWING
        if(window.is_drawing()) {

            // Gets the position of the mouse in the environment scale
            let mousePos = convertPosCanvasToEnv(mouseX, mouseY);

            // Vertical offset to shift the drawing, trace and forbidden canvas in order to align them to the environment
            let y_offset = SCROLL_Y_MAX - window.game.env.scroll[1];

            // Drawing ground to the right of the terrain startpad
            if(window.is_drawing_ground() && mousePos.x > (INITIAL_TERRAIN_STARTPAD - 1) * TERRAIN_STEP){
                drawing_canvas.push();
                drawing_canvas.stroke("#66994D");
                drawing_canvas.strokeWeight(4);
                // Draws a ground line between the current and previous positions of the mouse
                drawing_canvas.line(mouseX, mouseY + y_offset, window.prevMouseX, window.prevMouseY + y_offset);
                drawing_canvas.pop();
                window.terrain.ground.push(mousePos);
            }

            // Drawing ceiling to the right of the terrain startpad
            else if(window.is_drawing_ceiling() && mousePos.x > (INITIAL_TERRAIN_STARTPAD - 1) * TERRAIN_STEP){
                drawing_canvas.push();
                drawing_canvas.stroke("#808080");
                drawing_canvas.strokeWeight(4);
                // Draws a ceiling line between the current and previous positions of the mouse
                drawing_canvas.line(mouseX, mouseY + y_offset, window.prevMouseX, window.prevMouseY + y_offset);
                drawing_canvas.pop();
                window.terrain.ceiling.push(mousePos);
            }

            // Erasing to the right of the terrain startpad
            else if(window.is_erasing() && mousePos.x > INITIAL_TERRAIN_STARTPAD * TERRAIN_STEP){

                // Draws a circle trace at the mouse position to show the erasing radius
                trace_canvas.clear();
                trace_canvas.noStroke();
                trace_canvas.fill(255);
                trace_canvas.circle(mouseX, mouseY + y_offset, window.erasing_radius * 2);

                // Removes the points that are within the circle's radius from the ground and ceiling lists
                window.terrain.ground = window.terrain.ground.filter(function(point, index, array){
                    return Math.pow(point.x - mousePos.x, 2) + Math.pow(point.y - mousePos.y, 2) > Math.pow(window.erasing_radius / (window.game.env.scale * window.game.env.zoom), 2);
                });
                window.terrain.ceiling = window.terrain.ceiling.filter(function(point, index, array){
                    return Math.pow(point.x - mousePos.x, 2) + Math.pow(point.y - mousePos.y, 2) > Math.pow(window.erasing_radius / (window.game.env.scale * window.game.env.zoom), 2);
                });

                // Erases the drawing canvas inside the circle's radius
                drawing_canvas.erase();
                drawing_canvas.circle(mouseX, mouseY + y_offset, window.erasing_radius * 2);
                drawing_canvas.noErase();
            }

            // Dragging to scroll
            else{
                cursor(MOVE);
                window.game.env.set_scroll(null, window.game.env.scroll[0] + window.prevMouseX - mouseX, window.game.env.scroll[1] + mouseY - prevMouseY);

                // Re-draws the terrain shapes according to the new scroll
                window.refresh_drawing();
                y_offset = SCROLL_Y_MAX - window.game.env.scroll[1];
            }

            // Renders the environment and displays the off-screen canvas on top of it
            window.game.env.render();
            image(drawing_canvas, 0, -y_offset);
            image(trace_canvas, 0, -y_offset);
            image(forbidden_canvas, 0, -y_offset);
        }

        // DRAGGING
        else{
            cursor(MOVE);

            // Dragging an agent
            for (let agent of window.game.env.agents) {

                // Drags the selected agent
                if (agent.is_selected) {

                    // Computes the terrain's length according to the agent's morphology
                    let terrain_length;
                    if (agent.agent_body.body_type == BodyTypesEnum.CLIMBER) {
                        terrain_length = window.game.env.terrain_ceiling[window.game.env.terrain_ceiling.length - 1].x;
                    }
                    else if (agent.agent_body.body_type == BodyTypesEnum.WALKER) {
                        terrain_length = window.game.env.terrain_ground[window.game.env.terrain_ground.length - 1].x;
                    }
                    else if(agent.agent_body.body_type == BodyTypesEnum.SWIMMER){
                        terrain_length = Math.max(window.game.env.terrain_ground[window.game.env.terrain_ground.length - 1].x,
                                                    window.game.env.terrain_ceiling[window.game.env.terrain_ceiling.length - 1].x);
                    }

                    // Gets the mouse position inside the environment and clamps it horizontally to the edges of the terrain
                    let mousePos = convertPosCanvasToEnv(mouseX, mouseY);
                    let x = Math.max(0.02, Math.min(0.98, mousePos.x / terrain_length)) * terrain_length;

                    // Sets the position of the agent to the mouse position
                    window.game.env.set_agent_position(agent, x, mousePos.y);
                    window.game.env.render();
                    window.is_dragging_agent = true;
                    break;
                }
            }

            // Dragging an asset
            for(let asset of window.game.env.assets_bodies){

                // Drags the selected asset
                if (asset.is_selected && !window.is_dragging_agent) {
                    let terrain_length = Math.max(window.game.env.terrain_ground[window.game.env.terrain_ground.length - 1].x,
                                                    window.game.env.terrain_ceiling[window.game.env.terrain_ceiling.length - 1].x);

                    // Gets the mouse position inside the environment and clamps it horizontally to the edges of the terrain
                    let mousePos = convertPosCanvasToEnv(mouseX, mouseY);
                    mousePos.x = Math.max(0.02, Math.min(0.98, mousePos.x / terrain_length)) * terrain_length;

                    // Sets the position of the asset to the mouse position
                    window.game.env.set_asset_position(asset, mousePos);
                    window.game.env.render();
                    window.is_dragging_asset = true;
                }
            }

            // Dragging to scroll
            if(!window.is_dragging_agent && !window.is_dragging_asset){

                // Scrolling manually cancels agent following
                if(window.agent_followed != null){
                    window.set_agent_followed(-1);
                }
                window.game.env.set_scroll(null, window.game.env.scroll[0] + window.prevMouseX - mouseX, window.game.env.scroll[1] + mouseY - prevMouseY);
                window.game.env.render();
            }
        }
    }

    // Dragging an agent horizontally out of canvas
    else if(window.is_dragging_agent
        && mouseY >= 0 && mouseY < window.canvas.height){

        if(mouseX < 0){
            window.dragging_side = "left";
        }
        else if(mouseX > window.canvas.width){
            window.dragging_side = "right";
        }

        cursor(MOVE);

        // Dragging an agent
        for (let agent of window.game.env.agents) {

            // Drags the selected agent
            if (agent.is_selected) {

                // Scrolls horizontally according to the dragging side to follow the agent
                window.game.env.set_scroll(null);

                // Computes the terrain's length according to the agent's morphology
                let terrain_length;
                if (agent.agent_body.body_type == BodyTypesEnum.CLIMBER) {
                    terrain_length = window.game.env.terrain_ceiling[window.game.env.terrain_ceiling.length - 1].x;
                }
                else if (agent.agent_body.body_type == BodyTypesEnum.WALKER) {
                    terrain_length = window.game.env.terrain_ground[window.game.env.terrain_ground.length - 1].x;
                }
                else if(agent.agent_body.body_type == BodyTypesEnum.SWIMMER){
                    terrain_length = Math.max(window.game.env.terrain_ground[window.game.env.terrain_ground.length - 1].x,
                        window.game.env.terrain_ceiling[window.game.env.terrain_ceiling.length - 1].x);
                }

                // Gets the mouse position inside the environment and clamps it horizontally to the edges of the terrain
                let mousePos = convertPosCanvasToEnv(mouseX, mouseY);
                let x = Math.max(0.02, Math.min(0.98, mousePos.x / terrain_length)) * terrain_length;

                // Sets the position of the agent to the mouse position
                window.game.env.set_agent_position(agent, x, mousePos.y);
                window.game.env.render();
                break;
            }
        }

        // Prevents default behaviour when dragging the mouse
        return false;
    }

    window.prevMouseX = mouseX;
    window.prevMouseY = mouseY;
}

/**
 * Handles actions when mouse is released.
 */
function mouseReleased(){
    cursor();
    window.is_dragging_agent = false;
    window.is_dragging_asset = false;
    window.dragging_side = null;
}

/**
 * Handles actions when mouse is moved.
 */
function mouseMoved(){

    // Draws the trace of the circle asset at the mouse position
    if(window.is_drawing_circle()){
        trace_canvas.clear();
        if(mouseX >= 0 && mouseX <= window.canvas.width
            && mouseY >= 0 && mouseY <= window.canvas.height) {
            trace_canvas.noStroke();
            trace_canvas.fill(136, 92, 0, 180);
            trace_canvas.circle(mouseX, mouseY + SCROLL_Y_MAX - window.game.env.scroll[1], window.asset_size * 4 * window.game.env.zoom);
        }
        window.game.env.render();
        image(trace_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
    }

    // Draws the trace of the eraser at the mouse position
    else if (window.is_erasing()) {
        trace_canvas.clear();
        if (mouseX >= 0 && mouseX <= window.canvas.width
            && mouseY >= 0 && mouseY <= window.canvas.height) {
            trace_canvas.noStroke();
            trace_canvas.fill(255, 180);
            trace_canvas.circle(mouseX, mouseY + SCROLL_Y_MAX - window.game.env.scroll[1], window.erasing_radius * 2);
        }
        window.game.env.render();
        image(drawing_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
        image(trace_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
        image(forbidden_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
    }
}

/**
 * Handles actions when a mouse wheel event is detected (actual mouse wheel or touchpad).
 * @param event {WheelEvent}
 * @returns {boolean}
 */
function mouseWheel(event){
    if(mouseX >= 0 && mouseX <= window.canvas.width
        && mouseY >= 0 && mouseY <= window.canvas.height) {

        trace_canvas.clear();

        // Resizes circle asset radius
        if(window.is_drawing_circle()){
            window.asset_size = Math.max(3, Math.min(window.asset_size - event.delta / 100, 30));
            trace_canvas.noStroke();
            trace_canvas.fill(136, 92, 0, 180);
            trace_canvas.circle(mouseX, mouseY + SCROLL_Y_MAX - window.game.env.scroll[1], window.asset_size * 4 * window.game.env.zoom);
            window.game.env.render();
            image(trace_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
        }

        // Resizes erasing radius
        else if(window.is_erasing()){
            window.erasing_radius = Math.max(5, Math.min(window.erasing_radius - event.delta / 100, 30));
            trace_canvas.noStroke();
            trace_canvas.fill(255, 180);
            trace_canvas.circle(mouseX, mouseY + SCROLL_Y_MAX - window.game.env.scroll[1], window.erasing_radius * 2);
            window.game.env.render();
            image(drawing_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
            image(trace_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
            image(forbidden_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
        }

        // Zooms in or out
        else {
            window.game.env.set_zoom(window.game.env.zoom - event.delta / 2000);
            // TODO: scroll on the mouse position
            window.game.env.set_scroll(null, window.game.env.scroll[0], window.game.env.scroll[1]);

            // If drawing mode, re-draws the terrain shapes according to the new zoom
            if(window.is_drawing()){
                window.refresh_drawing();
                window.game.env.render();
                image(drawing_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
                image(forbidden_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
            }
            else{
                window.game.env.render();
            }

        }

        // Prevents default behaviour for mouse wheel events inside the canvas
        return false;
    }
}

/**
 * Handles actions when a key is pressed.
 * @returns {boolean}
 */
function keyPressed(){
    // Deletes the agent or asset selected when pressing the delete key
    if(keyCode == DELETE){
        if(window.agent_selected != null){
            window.delete_agent(agent_selected);
            window.agent_selected(null);
            return false;
        }
        else if(window.asset_selected != null){
            window.game.env.delete_asset(window.asset_selected);
            window.asset_selected = null;
            window.game.env.render();
            return false;
        }
    }
}

/**
 * Handles actions when the window is resized.
 */
function windowResized(){

    let canvas_container = document.querySelector('#canvas_container');

    // Recomputes RENDERING_VIEWER_W, INIT_ZOOM and THUMBNAIL_ZOOM
    RENDERING_VIEWER_W = canvas_container.offsetWidth;
    INIT_ZOOM = RENDERING_VIEWER_W / ((TERRAIN_LENGTH + INITIAL_TERRAIN_STARTPAD) * 1.05 * TERRAIN_STEP * SCALE);
    THUMBNAIL_ZOOM = RENDERING_VIEWER_W / ((TERRAIN_LENGTH + INITIAL_TERRAIN_STARTPAD) * 0.99 * TERRAIN_STEP * SCALE);

    // Resizes the main canvas
    resizeCanvas(RENDERING_VIEWER_W, RENDERING_VIEWER_H);
    drawing_canvas.resizeCanvas(RENDERING_VIEWER_W + SCROLL_X_MAX, RENDERING_VIEWER_H + 2 * SCROLL_Y_MAX);
    trace_canvas.resizeCanvas(RENDERING_VIEWER_W + SCROLL_X_MAX, RENDERING_VIEWER_H + 2 * SCROLL_Y_MAX);
    forbidden_canvas.resizeCanvas(RENDERING_VIEWER_W + SCROLL_X_MAX, RENDERING_VIEWER_H + 2 * SCROLL_Y_MAX);

    // Generates the terrain from the drawing
    if(is_drawing()){
        window.refresh_drawing();
        window.game.env.render();
        image(drawing_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
        image(forbidden_canvas, 0, -SCROLL_Y_MAX + window.game.env.scroll[1]);
    }
    // Re-initializes the environment
    else{
        window.init_default();
    }
}

window.downloadObjectAsJson = (exportObj, exportName) => {
    let dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
    let downloadAnchorNode = document.createElement('a');
    downloadAnchorNode.setAttribute("href",     dataStr);
    downloadAnchorNode.setAttribute("download", exportName + ".json");
    document.body.appendChild(downloadAnchorNode); // required for firefox
    downloadAnchorNode.click();
    downloadAnchorNode.remove();
}

window.strUcFirst = (a) => {
    return (a+'').charAt(0).toUpperCase()+a.substr(1);
}

window.draw_forbidden_area = () => {
    forbidden_canvas.clear();
    forbidden_canvas.stroke("#FF0000");
    forbidden_canvas.strokeWeight(3);
    forbidden_canvas.fill(255, 50, 0, 75);
    let w = convertPosEnvToCanvas((INITIAL_TERRAIN_STARTPAD - 1) * TERRAIN_STEP, 0).x;
    forbidden_canvas.rect(0, 0, w, RENDERING_VIEWER_H + 2 * SCROLL_Y_MAX);
}