-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
280 lines (236 loc) · 11.9 KB
/
MainWindow.xaml.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Path = System.IO.Path;
namespace Ha {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
#region Constants & Shit
public MainWindow() {
InitializeComponent();
}
//Parametry globalne kalkulacji
int rows, cols, step;
double offsetX, offsetY, panicParameter = 0, averageTime = 0, panicStep = 0.1;
double[,] data, doorData;
Cell[][] cells, theMostImportantCopyOfCells;
int numberOfEvacuations = 0, numberOfIterations = 0;
Popup pop = new Popup();
private BackgroundWorker evacuationWorker = null;
private BackgroundWorker calcWorker = null;
public static string path, path2;
protected override void OnClosed(EventArgs e) {
base.OnClosed(e);
pop.Close();
Application.Current.Shutdown();
Console.WriteLine("Application has been closed");
}
public static bool CheckConvertion(string name, bool type) {
double numberdbl;
int numberint;
bool success;
if (type) {
success = int.TryParse(name, out numberint);
} else {
success = Double.TryParse(name, out numberdbl);
}
if (success) {
return true;
} else {
Console.WriteLine("Attempted conversion of '{0}' failed.",
name ?? "<null>");
return false;
}
}
private void ParametersDialog(object sender, RoutedEventArgs e) {
var dialog = new ParameterWindow();
if (CheckConvertion(dialog.panicParameterTB.Text,false)) {
if (dialog.ShowDialog() == true) {
panicParameter = Double.Parse(dialog.panicParameterTB.Text);
}
} else {
MessageBox.Show("Don't.");
}
}
#endregion
#region Simulations & Heavy Lifting
private void GenerateFloorField(object sender, RoutedEventArgs e) {
if (CheckConvertion(rowTb.Text, true) && CheckConvertion(colTb.Text, true) == true) {
MessageBoxResult result = MessageBox.Show("Are you sure you want to generate the floor field?"
+ '\n' + "You will be not allowed to add more stuff to this miserable world you just created.", "Confirm", MessageBoxButton.YesNo);
switch (result) {
case MessageBoxResult.Yes:
if (Cell.FindHoomans(cells).Count != 0) {
Cell.GenerateField(cells);
obst.IsEnabled = false;
door.IsEnabled = false;
people.IsEnabled = false;
evacuateHoomansBtn.IsEnabled = true;
evacuateHoomansNTimesBtn.IsEnabled = true;
multiplePanicParametersButton.IsEnabled = true;
if (Cell.DoorsCount(cells) == 1)
widerDoorButton.IsEnabled = true;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
DrawLabel(i, j);
}
}
evacuateHoomansBtn.IsEnabled = true;
obst.IsChecked = false;
door.IsChecked = false;
people.IsChecked = false;
Console.WriteLine(people.IsChecked);
Console.WriteLine(obst.IsChecked);
Console.WriteLine(door.IsChecked);
} else {
MessageBox.Show("Who are we going to evacuate?");
}
break;
case MessageBoxResult.No:
break;
}
}
theMostImportantCopyOfCells = Cell.DeepCopy(cells);
}
private string SaveArrayToFile(string fileName, double[,] array, string text) {
System.Windows.Forms.FolderBrowserDialog openfiledalog = new System.Windows.Forms.FolderBrowserDialog();
if (openfiledalog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
var dest = Path.Combine(openfiledalog.SelectedPath, fileName);
using (StreamWriter file = new StreamWriter(dest, false)) {
for (int j = 0; j < array.GetLength(1); j++) {
for (int i = 0; i < array.GetLength(0); i++) {
file.Write(array[i, j].ToString() + '\t');
}
file.Write("\n");
}
MessageBox.Show("Data has been saved!", text);
}
return openfiledalog.SelectedPath;
} else {
return null;
}
}
int EvacuationCalc(bool doYouWantBackgroundWorker, Cell[][] fieldArray, double panicParameter) { //ewakuuje wszystkich i zwraca ilosc iteracji potrzebnych do ewakuacji
int counter = 0;
int numberOfIterations = 0;
List<Cell> listOfHoomans = Cell.FindHoomans(fieldArray);
while (listOfHoomans.Count != 0) {
counter++;
numberOfIterations++;
Cell evacuateTo;
foreach (Cell cell in listOfHoomans) {
cell.howManyHoomansWereThere += 1;
evacuateTo = Cell.FindNeighbour(cell, fieldArray, panicParameter); //znajdujemy pozycje gdzie ma sie ewakuowac
fieldArray[cell.i][cell.j].isAPerson = false; //likwidujemy ludzika z miejsca gdzie stal
if (!evacuateTo.isADoor) {
fieldArray[evacuateTo.i][evacuateTo.j].isAPerson = true; //i wstawiamy go tam gdzie ma sie ewakuowac
}
}
Random rng = new Random(); //szuffle
int n = listOfHoomans.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
Cell value = listOfHoomans[k];
listOfHoomans[k] = listOfHoomans[n];
listOfHoomans[n] = value;
}
if (doYouWantBackgroundWorker) {
evacuationWorker.ReportProgress(1);
Thread.Sleep(500);
}
listOfHoomans = Cell.FindHoomans(fieldArray);
}
return numberOfIterations;
}
private void EvacuateHoomans(object sender, RoutedEventArgs e) {
if (null == evacuationWorker) {
evacuationWorker = new BackgroundWorker();
evacuationWorker.DoWork += new DoWorkEventHandler(evacuationWorker_DoWork);
evacuationWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(evacuationWorker_RunWorkerCompleted);
evacuationWorker.ProgressChanged += new ProgressChangedEventHandler(evacuationWorker_ProgressChanged);
evacuationWorker.WorkerReportsProgress = true;
evacuationWorker.WorkerSupportsCancellation = true;
}
if (!evacuationWorker.IsBusy) {
evacuationWorker.RunWorkerAsync();
}
}
private void EvacuateHoomansNTimesBtn_Click(object sender, RoutedEventArgs e) {
var dialog = new MoreCalculationParameters();
if (dialog.ShowDialog() == true) {
if (CheckConvertion(dialog.numevacTb.Text, true) && CheckConvertion(dialog.panicParTb.Text, false)) {
numberOfEvacuations = Int32.Parse(dialog.numevacTb.Text);
panicParameter = Double.Parse(dialog.panicParTb.Text);
dialog.Close();
if (null == calcWorker) {
calcWorker = new BackgroundWorker();
calcWorker.DoWork += new DoWorkEventHandler(calcWorker_DoWork);
calcWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(calcWorker_RunWorkerCompleted);
calcWorker.ProgressChanged += new ProgressChangedEventHandler(calcWorker_ProgressChanged);
calcWorker.WorkerReportsProgress = true;
calcWorker.WorkerSupportsCancellation = true;
}
if (!calcWorker.IsBusy) {
calcWorker.RunWorkerAsync();
pop.Show();
}
} else {
MessageBox.Show("Don't");
}
}
}
private void multiplePanicParametersButton_Click(object sender, RoutedEventArgs e) {
panicParameter = 0;
var dialog = new GraphSettings();
if (dialog.ShowDialog() == true) {
if (CheckConvertion(dialog.TbNumberOfEvacuations.Text, true) && CheckConvertion(dialog.TbStep.Text, false)) {
numberOfEvacuations = Int32.Parse(dialog.TbNumberOfEvacuations.Text);
panicStep = Double.Parse(dialog.TbStep.Text);
dialog.Close();
calcWorker = new BackgroundWorker();
calcWorker.DoWork += new DoWorkEventHandler(panicEvacuationWorker_DoWork);
calcWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(panicEvacuationWorker_RunWorkerCompleted);
calcWorker.ProgressChanged += new ProgressChangedEventHandler(calcWorker_ProgressChanged);
calcWorker.WorkerReportsProgress = true;
calcWorker.WorkerSupportsCancellation = true;
if (!calcWorker.IsBusy) {
calcWorker.RunWorkerAsync();
pop.Show();
}
} else {
MessageBox.Show("Don't");
}
}
}
private void widerDoorButton_Click(object sender, RoutedEventArgs e) {
var dialog = new MoreCalculationParameters();
if (dialog.ShowDialog() == true) {
if (CheckConvertion(dialog.numevacTb.Text, true) && CheckConvertion(dialog.panicParTb.Text, false)) {
numberOfEvacuations = Int32.Parse(dialog.numevacTb.Text);
panicParameter = Double.Parse(dialog.panicParTb.Text);
dialog.Close();
calcWorker = new BackgroundWorker();
calcWorker.DoWork += new DoWorkEventHandler(widerDoor_DoWork);
calcWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(widerDoor_RunWorkerCompleted);
calcWorker.ProgressChanged += new ProgressChangedEventHandler(calcWorker_ProgressChanged);
calcWorker.WorkerReportsProgress = true;
calcWorker.WorkerSupportsCancellation = true;
if (!calcWorker.IsBusy) {
calcWorker.RunWorkerAsync();
pop.Show();
}
} else {
MessageBox.Show("Don't.");
}
}
}
#endregion
}
}