-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
1958 lines (1695 loc) · 81.2 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
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Classes de bâtiments
class City {
constructor(nb_hab = 100000, conso = 54, density = 5000, category = 2, green_cover = 25) {
this.buildingtype = "city";
this.nb_hab = nb_hab; //nombre d'habitants
this.conso_hab = conso; //consommation par habitant (m³)
this.density = density; //densité d'habitants dans la ville (hab/km²)
this.category = category; //catégorie de ville : 1=petite, 2=moyenne, 3=grande
this.green_cover = green_cover; //couverture végétale de la ville (%)
}
export() { //fonction d'export du batiment
return {
buildingtype: this.buildingtype,
nb_hab: this.nb_hab,
conso_hab: this.conso_hab,
density: this.density,
category: this.category,
green_cover: this.green_cover
}
}
}
class Farm {
constructor(plante = "Blé", cover = 1000) {
this.buildingtype = "farm";
this.plante = plante; //plante cultivée
this.cover = cover; //couverture des plantes (ha)
}
export() { //fonction d'export du batiment
return {
buildingtype: this.buildingtype,
plante: this.plante,
cover: this.cover
}
}
}
class Forest {
constructor(tree_type = "Chêne", density = 50) {
this.buildingtype = "forest";
this.tree_type = tree_type; //type d'arbre (chêne, épicea, hêtre)
this.density = density; //densité de la foret (%)
}
export() { //fonction d'export du batiment
return {
buildingtype: this.buildingtype,
tree_type: this.tree_type,
density: this.density
}
}
}
class Industrial_area {
constructor(conso = 1500,size = 9, nb_industries = 1){
this.buildingtype = "industrial_area"
this.conso = conso; //consommation par industie (m³)
this.size = size; //taille de chaque industrie (ha)
this.nb_industries = nb_industries; //nombre d'industries
}
export() { //fonction d'export du batiment
return {
buildingtype: this.buildingtype,
conso: this.conso,
size: this.size,
nb_industries: this.nb_industries
}
}
}
class Animals {
constructor(animal_type = "Vache", nb_animals = 1000){
this.buildingtype = "animals";
this.animal_type = animal_type; //type d'animal (vache, cochon, poule, cheval, brebis)
this.nb_animals = nb_animals; //nombre d'animaux
}
export() { //fonction d'export du batiment
return {
buildingtype: this.buildingtype,
animal_type: this.animal_type,
nb_animals: this.nb_animals
}
}
}
var hidden_menu = true;
function shrink_menu() {
let menu_width = (side_menu.clientWidth / main_window.clientWidth)*100;
let game_width = 100 - menu_width;
if (hidden_menu) {
side_menu.style.left = "0";
shrink_button.style.transform = "rotateY(0deg)"
game_window.style.width = game_width + "%";
game_window.style.marginLeft = menu_width + "%";
} else {
side_menu.style.left = "-" + menu_width + "%";
game_window.style.width = "100%";
game_window.style.marginLeft = "0";
shrink_button.style.transform = "rotateY(180deg)"
}
hidden_menu = !hidden_menu;
}
window.onload = (event) => {
shrink_menu();
};
window.addEventListener('resize', () => {
hidden_menu = !hidden_menu;
shrink_menu();
}
);
var temp_data, rain_data;
var actual_scenario = "245"; //scenario du giec (126, 245 ou 370)
async function change_climatic_scenario(x){
actual_scenario = x;
rain_data = await read_climate_csv(`precipitations/precip_combine_${x}.csv`); //importation depuis le depot github
temp_data = await read_climate_csv(`temperatures/temp_combine_${x}.csv`); //importation depuis le depot github
}
async function read_climate_csv(name){
let response = await fetch(`data_meteo/${name}`);
let csv = await response.text();
let climate_data = Papa.parse(csv, {
header: true,
skipEmptyLines: true,
transformHeader: header => header.trim()
}).data.reduce((acc, row) => {
acc[row["date"]] = {"WCE" : row["WCE"], "World" : row["world"]};
return acc;
}, {});
return climate_data;
}
change_climatic_scenario(actual_scenario);
var square_size = 10000 //m de côté
var old_value = 0.65; //% remplissage de la nappe
var annee_debut = 2023; //année de début de la simulation (1850 à 2099)
var mois_debut = 01; //mois de début de la simulation (1 à 12)
var annee_actuelle = annee_debut; //année actuelle
var mois_actuel = mois_debut; //mois actuel
var new_data_label = mois_actuel.toLocaleString(undefined, { minimumIntegerDigits: 2 }) + "/" + annee_actuelle; //format mm/aaaa
var time_labels = [new_data_label]; //tableau des mois (abscisse graphique)
var water_data_per = [0.65]; //remplissage en %
var water_data_rain = [0]; //précipitations en mm
var water_data_temperature = [0]; //températures en °C
// Données calculées pendant la simulation
var record_data_per = true; //calcul des données de remplissage = oui
var record_data_ngf = true; //calcul des données de remplissage = oui
var record_data_temperature = false; //calcul des données de température = non (optimisation)
var record_data_rain = false; //calcul des données de précipitations = non (optimisation)
function create_chart(canvas_used, title_bool) { //création graphique
return new Chart(canvas_used, {
type: 'line', //graphique en ligne
data: {
labels: time_labels, //abscisse = mm/aaaa
datasets: [{
data: water_data_per, //ordonnées = remplissage nappe (%)
borderColor: 'rgb(104, 176, 235)',
tension: 0.25
}]
},
options: {
maintainAspectRatio: false,
scales: {
x: {
// display: false,
ticks: {
callback: function (val, index) {
return index % 2 === 0 ? this.getLabelForValue(val) : '';
},
},
},
y: {
min: 0, //ordonnée min
max: 1, //ordonnée max
ticks: {
display: true,
},
}
},
plugins: {
legend: {
display: false,
},
title: {
text: "Pourcentage de remplissage de la nappe phréatique au cours du temps",
display: title_bool,
font: {
size: 18,
weight: false,
}
},
subtitle: {
display: false,
}
}
}
});
}
var water_chart = create_chart(water_timeline, false); //timeline
var water_chart_stats = create_chart(water_timeline_stats, true); //graphique de la fenetre des stats
var water_consumption = [0, 0, 0, 0, 0, 0,0]; //consommations totales depuis début [villes, irrigation, forets, etp, ecoul. lateral, industries, elevages]
var water_consumption_current = [0, 0, 0, 0, 0, 0,0]; //consommations du mois actuel
var pie_chart_conso = new Chart(pie_chart_conso_canvas, {
type: 'pie', //graphique en camembert
data: {
labels: [
'Villes',
'Fermes',
'Forêts',
'Évapotranspiration',
'Écoulement latéral souterrain',
'Industries',
'Elevages'
],
datasets: [{
data: water_consumption,
}]
},
options: {
animation: {
animateRotate: false
}
}
});
var gridsize = 8; //nombre de cases par colonnes (nb total de cases = gridsize²)
var selected_case = null; //case sélectionnée
var capacity = 15 * 10 ** 9; //capacité max de la nappe (m³)
var phi = 48; //latitude (deg)
var z = 30; //altitude (m ngf)
var depth = 10; //prodondeur minimale de la nappe (m)
var permeability = 10**-5; //coefficient de permeabilite du sol (m/s)
var inclinaison = 5*Math.PI/180; //inclinaison de la nappe (deg)
var rain_intensity = 100; //intensité de la pluie (%)
var wind_intensity = 100; //intensité du vent (%)
var solar_intensity = 100; //intensité de l'irradiance (%)
var temperature_intensity = 100; //intensité des températures (%)
var etp_limit = Infinity; //limite d'etp (% des précipitations, Infinity=pas de limite)
var water_data_ngf = [z - depth - capacity*(1-old_value) / ((square_size ** 2)*(gridsize ** 2))]; //niveau de la nappe en m ngf
var displayed_data = "per"; //donnée affichée sur la timeline (per, ngf, rain ou temperature)
var displayed_stats_conso = "total"; //donnée affichée sur le camembert des consos (total ou month)
function change_chart_var(value){ //change la donnée affichée sur la timeline et le graphique des stats
switch(value){
case "percentage" :
water_chart.options.scales.y.min = 0; //ordonnée min (timeline)
water_chart.options.scales.y.max = 1; //ordonnée max (timeline)
water_chart_stats.options.scales.y.min = 0; //ordonnée min (stats)
water_chart_stats.options.scales.y.max = 1; //ordonnée max (stats)
displayed_data = "per";
water_chart_stats.config.options.plugins.title.text = "Remplissage de la nappe au cours du temps (%)";
update_all_charts();
break;
case "niv_ngf" :
water_chart.options.scales.y.min = z - depth - capacity / ((square_size ** 2)*(gridsize ** 2)); //ordonnée min (timeline)
water_chart.options.scales.y.max = z - depth; //ordonnée max (timeline)
water_chart_stats.options.scales.y.min = z - depth - capacity / ((square_size ** 2)*(gridsize ** 2)); //ordonnée min (stats)
water_chart_stats.options.scales.y.max = z - depth; //ordonnée max (stats)
displayed_data = "ngf";
water_chart_stats.config.options.plugins.title.text = "Niveau ngf de la nappe au cours du temps (m)";
update_all_charts();
break;
case "rain" :
water_chart.options.scales.y.min = 0; //ordonnée min (timeline)
water_chart.options.scales.y.max = 130; //ordonnée max (timeline)
water_chart_stats.options.scales.y.min = 0; //ordonnée min (stats)
water_chart_stats.options.scales.y.max = 130; //ordonnée max (stats)
displayed_data = "rain";
water_chart_stats.config.options.plugins.title.text = "Niveau mensuel des précipitations (mm)";
update_all_charts();
break;
case "temperature" :
water_chart.options.scales.y.min = -10; //ordonnée min (timeline)
water_chart.options.scales.y.max = 50; //ordonnée max (timeline)
water_chart_stats.options.scales.y.min = -10; //ordonnée min (stats)
water_chart_stats.options.scales.y.max = 50; //ordonnée max (stats)
displayed_data = "temperature";
water_chart_stats.config.options.plugins.title.text = "Température moyenne mensuelle (°C)";
update_all_charts();
break;
default:
water_chart.options.scales.y.min = 0; //ordonnée min (timeline)
water_chart.options.scales.y.max = 1; //ordonnée max (timeline)
water_chart_stats.options.scales.y.min = 0; //ordonnée min (stats)
water_chart_stats.options.scales.y.max = 1; //ordonnée max (stats)
displayed_data = "per";
water_chart_stats.config.options.plugins.title.text = "Remplissage de la nappe au cours du temps (%)";
update_all_charts();
break;
}
}
function change_stats_conso(value){ //change les valeurs du camembert des stats (consos totales ou mois actuel)
switch(value){
case "month":
displayed_stats_conso = "month"; //valeurs du mois actuel
update_all_charts();
break;
case "total":
displayed_stats_conso = "total"; //valeurs cummulées depuis le début
update_all_charts();
break;
default:
displayed_stats_conso = "total"; //valeurs cummulées depuis le début
update_all_charts();
break;
}
}
function change_record_parameters(value){ //change les données calculés
//Selection de la donnée affichée, on désactive les choix des données non-calculées
per_radio.disabled = !record_per_checkbox.checked;
ngf_radio.disabled = !record_ngf_checkbox.checked;
rain_radio.disabled = !record_rain_checkbox.checked;
temperature_radio.disabled = !record_temperature_checkbox.checked;
//Export de données, on désactive les choix des données non-calculées
export_per_button.disabled = !record_per_checkbox.checked;
export_ngf_button.disabled = !record_ngf_checkbox.checked;
export_rain_button.disabled = !record_rain_checkbox.checked;
export_temperature_button.disabled = !record_temperature_checkbox.checked;
//Mise a jour des booléens
record_data_per = record_per_checkbox.checked;
record_data_ngf = record_ngf_checkbox.checked;
record_data_rain = record_rain_checkbox.checked;
record_data_temperature = record_temperature_checkbox.checked;
}
function change_animations_status(value){ //active ou désactive les animations des graphiques
water_chart.options.animation = value;
water_chart_stats.options.animation = value;
}
function redraw_grid(size) { //recrée une grille de la taille size²
gridsize = size;
selected_case = null;
buildings = [];
grid_3d_div.textContent = '';
draw_grid(size);
}
function draw_grid(size) { //crée une grille de la taille size²
grid_3d_div.style.gridTemplateColumns = `repeat(${size}, minmax(0, 1fr))`;
grid_3d_div.style.gridTemplateRows = `repeat(${size}, minmax(0, 1fr))`;
for (let i = 1; i <= size; i++) {
for (let j = 1; j <= size; j++) { //création des cases
let div = document.createElement('div');
div.className = 'case_grid';
div.id = `case${i}_${j}`; //appelation : case_ligne_colonne
div.style.zIndex = i - j + parseInt(size) + 1; //ordre d'affichage
div.onclick = function () { div_selected(this) }; //case cliquée → on appelle div_selected()
grid_3d_div.appendChild(div); //élément html qui stocke toutes les cases
}
}
}
draw_grid(gridsize); //on crée initialement une grille de taille gridsize²
function setting_changed(setting, value) { //mise à jour de paramètre (depuis sliders ou fenetre parametres avancés)
switch (setting) {
case "size": //nb de cases par colonne
gridsize = parseInt(value);
map_size_label.textContent = "Taille de la carte (" + gridsize + ")";
map_size_input.value = gridsize;
redraw_grid(gridsize);
break;
case "square_size": //longueur d'une case (m)
//advanced only
square_size = parseInt(value);
break;
case "start_date": //date de début (mois+année)
mois_debut = parseInt(start_month_select.value);
annee_debut = parseInt(start_year_box.value);
annee_actuelle = annee_debut;
mois_actuel = mois_debut;
new_data_label = mois_actuel.toLocaleString(undefined, { minimumIntegerDigits: 2 }) + "/" + annee_actuelle;
time_labels = [new_data_label];
update_all_charts();
break;
case "altitude": //altitude du site (m)
altitude_label.textContent = "Altitude du site (" + value + " m)";
altitude_input.value = value;
altitude_box.value = value;
z = parseFloat(value);
water_data_ngf = [z - depth - capacity*(1-old_value) / ((square_size ** 2)*(gridsize ** 2))];
update_all_charts();
break;
case "latitude": //latitude du site (deg)
//advanced only
latitude_box.value = parseFloat(value);
phi = parseFloat(value);
break;
case "depth": //profondeur minimale de la nappe (m)
depth_label.textContent = "Profondeur de la nappe (" + value + " m)";
depth_input.value = value;
depth_box.value = value;
depth = parseInt(value);
water_data_ngf = [z - depth - capacity*(1-old_value) / ((square_size ** 2)*(gridsize ** 2))];
update_all_charts();
break;
case "permeability_coeff": //coeff de permeabilité (m/s)
permeability_coeff_label.textContent = "Coefficient de perméabilité (1e" + value + " m/s)";
permeability_coeff_input.value = value;
permeability = parseFloat(10 ** value);
permeability_coeff_box.value = "1e"+value.toString();
break;
case "permeability_coeff_advanced": //coeff de permeabilité format XXeX (parametres avancés) (m/s)
if(value.split("e").length == 2){
permeability_coeff_label.textContent = "Coefficient de perméabilité (" + value + " m/s)";
permeability_coeff_input.value = parseInt(value.split("e")[1]);
permeability = parseFloat(value.split("e")[0]*10**value.split("e")[1]);
} else{
permeability_coeff_advanced_label.textContent = "Coefficient de perméabilité (m/s) (ex: 9e-6) [FORMAT INVALIDE]";
}
break;
case "capacity": //capacité de la nappe (10^9 m³)
capacity_label.textContent = "Capacité de la nappe (" + value + " e9 m³)";
capacity_input.value = value;
capacity = parseFloat(value * 10 ** 9);
capacity_box.value = value.toString() + "e9";
break;
case "capacity_advanced": //capacité de la nappe format XXeX (parametres avancés) (10^9 m³)
if(value.split("e").length == 2){
capacity_label.textContent = "Capacité de la nappe (" + value + " m³)";
capacity_input.value = parseInt(value.split("e")[0]);
capacity = parseInt(value.split("e")[0]) * 10 ** parseInt(value.split("e")[1]);
capacity_box.value = value;
} else{
capacity_advanced_label.textContent = "Capacité de la nappe (m³) (ex: 10e9) [FORMAT INVALIDE]";
}
break;
case "inclinaison": //inclinaison de la nappe (deg)
inclinaison_label.textContent = "Inclinaison (" + value + "°)";
inclinaison_input.value = value;
inclinaison_box.value = value;
inclinaison = parseFloat(value)*Math.PI/180; //conversion en rad
break;
case "startfilling": //remplissage initial (%)
//advanced only
startfilling_box.value = value;
old_value = parseInt(value)/100;
water_data_per = [old_value];
water_data_ngf = [z - depth - capacity*(1-old_value) / ((square_size ** 2)*(gridsize ** 2))];
water_data_rain = [0];
water_data_temperature = [0];
water_bar_colored.style.height = old_value * 100 + "%";
water_bar_quantity.innerHTML = (Math.round(old_value * 100)).toString() + "%";
update_all_charts();
break;
case "rain_intensity": //intensité de la pluie (%)
rain_intensity_label.textContent = "Intensité des précipitations (" + value + "%)";
rain_intensity_input.value = value;
rain_intensity_box.value = value;
rain_intensity = parseFloat(value);
break;
case "wind_intensity": //intensité du vent (%)
//advanced only
wind_intensity_box.value = value;
wind_intensity = parseFloat(value);
break;
case "solar_intensity": //intensité du rayonnement solaire (%)
//advanced only
solar_intensity_box.value = value;
solar_intensity = parseFloat(value);
break;
case "temperature_intensity": //intensité des températures (%)
temperature_intensity_label.textContent = "Intensité des températures (" + value + "%)";
temperature_intensity_input.value = value;
temperature_intensity_box.value = value;
temperature_intensity = parseFloat(value);
break;
case "etp_limit": //limite d'etp (% de la pluie du mois)
//advanced only
if(parseFloat(value) == 0){
etp_limit = Infinity;
} else{
etp_limit = parseFloat(value);
}
break;
case "tabHR": //tableau mensuel des humidités relatives (%)
//advanced only
mytmp = value.toString();
mytmp = mytmp.replace("[","");
mytmp = mytmp.replace("]","");
mytmp = mytmp.split(",").map(Number);
if(mytmp.length == 12){
tabHR = mytmp;
tabHR_label.textContent = "Humidité relative mensuelle moyenne (%)";
} else{
tabHR_label.textContent = "Humidité relative mensuelle moyenne (%) [FORMAT INVALIDE]";
}
tabHR_box.value = value;
break;
case "tabRg": //tableau mensuel des valeurs moyennes quotidiennes des rayonnements solaires (kWh/m²/jour)
//advanced only
mytmp = value.toString();
mytmp = mytmp.replace("[","");
mytmp = mytmp.replace("]","");
mytmp = mytmp.split(",").map(Number);
if(mytmp.length == 12){
tabRg = mytmp;
tabRg_label.textContent = "Irradiance quotidienne moyenne (kWh/m²/jour)";
} else{
tabRg_label.textContent = "Irradiance quotidienne moyenne (kWh/m²/jour) [FORMAT INVALIDE]";
}
tabRg_box.value = value;
break;
case "tabv10": //tableau mensuel de la force du vent à 10m (m/s)
//advanced only
mytmp = value.toString();
mytmp = mytmp.replace("[","");
mytmp = mytmp.replace("]","");
mytmp = mytmp.split(",").map(Number);
if(mytmp.length == 12){
tabv10 = mytmp;
tabv10_label.textContent = "Vitesse mensuelle moyenne du vent à 10m (m/s)";
} else{
tabv10_label.textContent = "Vitesse mensuelle moyenne du vent à 10m (m/s) [FORMAT INVALIDE]"
}
tabv10_box.value = value;
break;
case "city_size": //[ville] taille de la ville (1=petite, 2=moyenne, 3=grande)
buildings[selected_case].category = parseInt(value);
if(value == "1"){ //[ville] petite ville
nb_hab_input.min = 1000;
nb_hab_input.max = 100000;
nb_hab_input.step = 1000;
setting_changed("city_nb_hab", value = 20000);
nb_hab_input.value = 20000;
city_green_cover_input.min = 10;
city_green_cover_input.max = 90;
setting_changed("city_green_cover", 25);
city_green_cover_input.value = 25;
document.getElementById(selected_case).firstChild.src = sprites["small city"];
}
if(value == "2"){ //[ville] moyenne ville
nb_hab_input.min = 100000;
nb_hab_input.max = 500000;
nb_hab_input.step = 10000;
setting_changed("city_nb_hab", value = 250000);
nb_hab_input.value = 250000;
city_green_cover_input.min = 10;
city_green_cover_input.max = 50;
setting_changed("city_green_cover", 25);
city_green_cover_input.value = 25;
document.getElementById(selected_case).firstChild.src = sprites["medium city"];
}
if(value == "3"){ //[ville] grande ville
nb_hab_input.min = 500000;
nb_hab_input.max = 2000000;
nb_hab_input.step = 100000;
setting_changed("city_nb_hab", value = 1000000);
nb_hab_input.value = 1000000;
city_green_cover_input.min = 5;
city_green_cover_input.max = 30;
setting_changed("city_green_cover", 25);
city_green_cover_input.value = 25;
document.getElementById(selected_case).firstChild.src = sprites["city"];
}
case "city_nb_hab": //[ville] nombre d'habitants
nb_hab_label.textContent = "Nombre d'habitants (" + value + ")";
buildings[selected_case].nb_hab = value;
city_density_input.min = Math.min(value/100,city_density_input.max);
if (city_density_input.value <= city_density_input.min){
setting_changed("city_density",value/100);
}
break;
case "city_conso_hab": //[ville] conso annuelle par habitant (m³/an)
conso_hab_label.textContent = "Consommation par habitant (" + value + " m³/an)";
buildings[selected_case].conso_hab = value;
break;
case "city_density": //[ville] densité de la population
city_density_label.textContent = "Densité de la population (" + value + " hb/km²)";
buildings[selected_case].density = value;
break;
case "city_green_cover": //[ville] couverture végétale
city_green_cover_label.textContent = "Couverture végétale (" + value + " %)";
buildings[selected_case].green_cover = value;
break;
case "farm_plante": //[ferme] type de culture (blé, maïs, pommes de terre, soja, tournesol)
buildings[selected_case].plante = value;
break;
case "farm_cover": //[ferme] couverture de la ferme (ha)
farm_cover_label.textContent = "Couverture des plantations (" + value + " ha)";
buildings[selected_case].cover = value;
break;
case "forest_density": //[fôret] densité de la foret (%)
forest_density_label.textContent = "Densité de la forêt (" + value + " %)";
buildings[selected_case].density = value;
//mise a jour de l'image de la foret selon sa densité
if(value==0){
document.getElementById(selected_case).firstChild.src = sprites["dead forest"];
} else if(value<20){
document.getElementById(selected_case).firstChild.src = sprites["small forest"];
} else if(value<30){
document.getElementById(selected_case).firstChild.src = sprites["medium forest"];
} else{
document.getElementById(selected_case).firstChild.src = sprites["forest"];
};
break;
case "forest_tree_type": //[fôret] type d'arbre (chêne, épicéa, hêtre)
buildings[selected_case].tree_type = value;
break;
case "industry_conso": //[zone industrielle] consommation annuelle par industrie (m³/an)
industrie_conso_label.textContent = "Consommation par industrie (" + value + " m³)";
buildings[selected_case].conso = parseInt(value);
break;
case "industry_size": //[zone industrielle] surface de chaque industrie (ha)
industrie_size_label.textContent = "Taille de chaque industrie (" + value + " ha)";
buildings[selected_case].size = parseInt(value);
nb_industries_input.max = 10000 / parseInt(value);
if (buildings[selected_case].nb_industries > parseInt(nb_industries_input.max)){
setting_changed("industry_number", value = parseInt(nb_industries_input.max));
}
break;
case "industry_number": //[zone industrielle] nombre d'industries
nb_industries_label.textContent = "Nombre d'industries (" + value + ")";
buildings[selected_case].nb_industries = parseInt(value);
break;
case "animal_type": //[élevage] type d'animal (vache, cochon, cheval, brebis, poules)
buildings[selected_case].animal_type = value;
switch (value){
case "Vache":
nb_animals_input.min = 100;
nb_animals_input.max = 10000;
if (nb_animals_input.value < 100){setting_changed("nb_animals",100)}
if (nb_animals_input.value > 10000){setting_changed("nb_animals",10000)}
break;
case "Cochon":
nb_animals_input.min = 2000;
nb_animals_input.max = 2000000;
if (nb_animals_input.value < 2000){setting_changed("nb_animals",2000)}
if (nb_animals_input.value > 2000000){setting_changed("nb_animals",2000000)}
break;
case "Cheval":
nb_animals_input.min = 100;
nb_animals_input.max = 10000;
if (nb_animals_input.value < 100){setting_changed("nb_animals",100)}
if (nb_animals_input.value > 10000){setting_changed("nb_animals",10000)}
break;
case "Brebis":
nb_animals_input.min = 1000;
nb_animals_input.max = 100000;
if (nb_animals_input.value < 1000){setting_changed("nb_animals",1000)}
if (nb_animals_input.value > 100000){setting_changed("nb_animals",100000)}
break;
case "Poules":
nb_animals_input.min = 100;
nb_animals_input.max = 10000;
if (nb_animals_input.value < 100){setting_changed("nb_animals",100)}
if (nb_animals_input.value > 10000){setting_changed("nb_animals",10000)}
break;
}
break;
case "nb_animals": //[élevage] nombre d'animaux
buildings[selected_case].nb_animals = value;
nb_animals_label.textContent = "Nombre d'animaux (" + value + ")"
nb_animals_input.value = value;
break;
}
}
function place_cube_side_faces() { //crée les faces inférieures du terrain (sol)
grid_3d_div.style.transform = `translateZ(${pave_3d.offsetWidth / 8}px)`;
cube_face_left.style.transform = `rotateY(-90deg) translateZ(${pave_3d.offsetWidth / 2}px)`;
cube_face_right.style.transform = `rotateX(-90deg) translateZ(${pave_3d.offsetHeight / 2}px)`;
}
place_cube_side_faces();
const resizeObserver_cube = new ResizeObserver(entries => {
for (let entry of entries) {
place_cube_side_faces();
}
});
resizeObserver_cube.observe(pave_3d);
var occupied_list = []; //liste des cases occupées
var buildings = {}; //liste des bâtiments et de leurs propriétés
function div_selected(item) { //case sélectionnée (click)
if (selected_case != null) { //si une autre case était sélectionné, on la déselectionne
document.getElementById(selected_case).style.backgroundColor = '';
document.getElementById(selected_case).classList.remove("grid_case_selected");
}
selected_case = item.id; //format casei_j
item.style.backgroundColor = 'red';
item.classList.add("grid_case_selected");
//on cache de la barre latérale les menus de toutes les classes de batiment
Selected_building_parameters_div.style.display = "none";
City_menu_div.style.display = "none";
Farm_menu_div.style.display = "none";
Forest_menu_div.style.display = "none";
Industrial_area_menu_div.style.display = "none";
Animals_menu_div.style.display = "none";
if (selected_case in buildings) {
switch (buildings[selected_case].buildingtype) {
case "city":
Selected_building_parameters_div.style.display = "block"; //affichage du menu de la classe sélectionnée
//affichage paramètres spécifiques ville
if (buildings[selected_case].category == 1){
nb_hab_input.min = 1000;
nb_hab_input.max = 100000;
nb_hab_input.step = 1000;
city_green_cover_input.min = 10;
city_green_cover_input.max = 90;
document.getElementById(selected_case).firstChild.src = sprites["small city"];
} else if(buildings[selected_case].category == 2){
nb_hab_input.min = 100000;
nb_hab_input.max = 500000;
nb_hab_input.step = 10000;
city_green_cover_input.min = 10;
city_green_cover_input.max = 50;
document.getElementById(selected_case).firstChild.src = sprites["medium city"];
} else if(buildings[selected_case].category == 3){
nb_hab_input.min = 500000;
nb_hab_input.max = 2000000;
nb_hab_input.step = 100000;
city_green_cover_input.min = 5;
city_green_cover_input.max = 30;
document.getElementById(selected_case).firstChild.src = sprites["city"];
}
nb_hab_input.value = buildings[selected_case].nb_hab;
setting_changed("city_nb_hab", buildings[selected_case].nb_hab);
conso_hab_input.value = buildings[selected_case].conso_hab;
setting_changed("city_conso_hab", buildings[selected_case].conso_hab);
city_density_input.value = buildings[selected_case].density;
setting_changed("city_density", buildings[selected_case].density);
city_green_cover_input.value = buildings[selected_case].green_cover;
setting_changed("city_green_cover", buildings[selected_case].green_cover);
City_menu_div.style.display = "block";
document.getElementsByClassName("city_size_button")[buildings[selected_case].category-1].checked = true;
break;
case "farm":
//affichage paramètres spécifiques ferme
Selected_building_parameters_div.style.display = "block";
plantation_type_select.value = buildings[selected_case].plante;
farm_cover_input.value = buildings[selected_case].cover;
setting_changed("farm_cover", buildings[selected_case].cover);
Farm_menu_div.style.display = "block";
break;
case "forest":
//affichage paramètres spécifiques fôret
Selected_building_parameters_div.style.display = "block";
tree_type_select.value = buildings[selected_case].tree_type;
forest_density_input.value = buildings[selected_case].density;
setting_changed("forest_density", buildings[selected_case].density);
Forest_menu_div.style.display = "block";
break;
case "industrial_area":
//affichage paramètres spécifiques zone industrielle
Selected_building_parameters_div.style.display = "block";
industrie_conso_input.value = buildings[selected_case].conso;
setting_changed("industry_conso", buildings[selected_case].conso);
industrie_size_input.value = buildings[selected_case].size;
setting_changed("industry_size", buildings[selected_case].size);
nb_industries_input.value = buildings[selected_case].nb_industries;
setting_changed("industry_number", buildings[selected_case].nb_industries);
Industrial_area_menu_div.style.display = "block";
break;
case "animals":
//affichage paramètres spécifiques élevage
Selected_building_parameters_div.style.display = "block";
setting_changed("nb_animals", buildings[selected_case].nb_animals);
Animals_menu_div.style.display = "block";
default:
break;
}
}
}
//liens internes vers les visuels du site
const sprites = {
"city": "Images/Ville.png",
"medium city" : "Images/Ville Moyenne.png",
"small city" : "Images/Ville Petite.png",
"farm": "Images/Ferme 2.png",
"forest": "Images/Forêt Epicea.png",
"medium forest": "Images/Forêt medium.png",
"small forest": "Images/Forêt mal.png",
"dead forest" : "Images/Forêt morte.png",
"industrial_area": "Images/Industrie.png",
"animals": "Images/Elevage.png",
}
var copied_building = false; //par défaut : aucun bâtiment sélectionné pour la copie
function place_building(type, template = false) { //place un bâtiment sur la grille
if (selected_case != null && !occupied_list.includes(selected_case)) { //case sélectionnée vide
occupied_list.push(selected_case);
div = document.getElementById(selected_case);
div.style.backgroundColor = 'yellow';
let maison_image = document.createElement('img');
maison_image.src = sprites[type];
maison_image.classList.add("sprite_cool");
div.appendChild(maison_image);
var build; //objet de la classe "type" de batiment
if (!template) { //pas de template (cas basique:ajout nouveau batiment par défaut)
switch (type) {
case "city":
build = new City();
nb_hab_input.min = 100000;
nb_hab_input.max = 500000;
nb_hab_input.step = 10000;
city_green_cover_input.min = 10;
city_green_cover_input.max = 50;
document.getElementById(selected_case).firstChild.src = sprites["medium city"];
break;
case "farm":
build = new Farm();
break;
case "forest":
build = new Forest();
break;
case "industrial_area":
build = new Industrial_area();
break;
case "animals":
build = new Animals();
break;
default:
break;
}
} else { //si template : on veut coller ou importer les batiments d'une config
if(copied_building){ //collage du batiment copié sur la grille
build = Object.assign(Object.create(Object.getPrototypeOf(copied_building)), copied_building)
} else{ //placement des batiments de l'import
build = template;
}
}
buildings[selected_case] = build; //case sélectionnée = case placée
}
div_selected(div);
}
function remove_building() { //supprime le batiment sélectionné
div = document.getElementById(selected_case); //case à supprimer
if (selected_case != null && occupied_list.includes(selected_case)) { //case occupée
occupied_list.splice(occupied_list.indexOf(selected_case), 1);
div.removeChild(div.childNodes[0]);
delete buildings[selected_case];
}
div_selected(div);
}
function copy_building() { //copie le batiment sélectionné
if (selected_case != null && occupied_list.includes(selected_case)) { //case occupée
copied_building = buildings[selected_case];
}
div_selected(div);
}
function paste_building() { //colle un batiment avec les memes paramètres que le batiment copié
if (copied_building) {
place_building(copied_building.buildingtype, template = true)
}
}
function calc_delay(date) { //calcul le délai d'infiltration de l'eau
Q = (permeability) * (parseFloat(rain_data[date]['WCE'])*0.001+depth)/(depth);
return Math.floor((depth/Q)/2629800); // conversion secondes -> mois
}
function evapotranspiration(date){ //EN ATTENTE DE SUPPRESSION
Ta = parseFloat(temp_data[date]['WCE']);
delta = (2504*Math.exp(17.27*(Ta-273.15)/(Ta-35.85)))/((Ta-35.85)**2);
console.log(delta)
deltae = 0.6108*Math.exp(17.27*(Ta-273.15)/(Ta-35.85));
e = soleil[mois_actuel];
Rg = 295*e+197.5*(1-e);
Ra = 492;
alpha = 0.23;
epsilon = 1;
sigma = 5.67*10**8;
Rn = (1-alpha)*Rg + epsilon*Ra - epsilon*sigma*(Ta**4)
console.log(Rn)
zom = 0.015;
zm = 50;
zh = 50;
zoh = 0.0015;
d = 0.08;
k = 0.41;
v = 6.9;
ra = (Math.log((zm-d)/zom)*Math.log((zh-d)/zoh))/((k**2)*v);
rho = 1.2;
Cp = 1013;
lambda = 2.45106;
gamma = 0.000665;
Rs = 70;
G = 0.3;
ETP = (delta*(Rn-G)+rho*Cp*deltae/ra)/(lambda*(delta+gamma*(1+Rs/ra)))
return ETP
}
function evapotranspiration2(date, cover){ //EN ATTENTE DE SUPPRESSION
T = 283.15;
delta = 0.145;
//delta = delta*10**-3 + 273.15;
Rn = 7.6;
//Rn = (Rn*10**-6);
//G = 0.1*Rn;
G = 0.3; //valeur officielle
v = 5.4;
deltae = 0.91;
gamma = 0.067;
//gamma = gamma/(10**3) + 273.15;
ETP = (0.408*delta*(Rn-G)+(900/T)*gamma*v*deltae)/(delta+gamma*(1+0.34*v));
return ETP*30*cover
}
function evapotranspiration3(date, cover) { //EN ATTENTE DE SUPPRESSION
p = rain_data[date]["WCE"];
t = temp_data[date]["WCE"];
l = 300 + 25*t + 0.05*t**3;
ETR = (p/((0.9+p**2/l**2))**1/2);
return ETR*30*cover
}
function fes(T){ //appelée depuis etp()
return 0.6108*Math.exp((17.27*T)/(T+237.3))
}
var tabHR = [85, 81, 77, 73, 74, 69, 68, 70, 70, 80, 85, 85]; //humidité relative mensuelle moyenne (%)
var tabRg = [1.96, 3.3, 4.6, 5.59, 5.65, 5.93, 5.99, 5.79, 5.43, 3.46, 2.46, 2.01]; //irradiance quotidienne moyenne par mois (kWh/m²/jour)
var tabv10 = [5.22, 5.06, 4.89, 4.69, 4.36, 4.22, 4.06, 3.92, 4.19, 4.64, 4.89, 5.14]; //vitesse du vent mensuelle moyenne (m/s)
var grasskc = 0.8; //EN ATTENTE DE SUPPRESSION
function etp(date,cover){ //calcul l'evapotranspiration pour une date donnée et sur une surface donnée