-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car.cs
642 lines (612 loc) · 21.9 KB
/
Car.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
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
using System;
using System.Threading;
using Terminal.Gui;
using crossing1;
namespace crossing1
{
enum CarPos
{
TOP,
RIGHT,
BOTTOM,
LEFT
}
enum CarDirection
{
FORWARD,
TURN_RIGHT,
TURN_LEFT
}
enum CarRotation
{
UP,
RIGHT,
DOWN,
LEFT
}
class Car
{
Thread threadAction;
private bool carExists = true;
private Mutex carExistsMutex = new Mutex();
private int pos_X = 0;
private int pos_Y = 0;
private int posOrigin_X = 0;
private int posOrigin_Y = 0;
private Mutex positionMutex;
private int speed = 500; //?wspieranie predkosci?
private static Random randSpeed = new Random();
private String graphic = "A";
private Terminal.Gui.Attribute carColor;//= new Terminal.Gui.Attribute(Color.BrightGreen, Color.Black);
private CarRotation carRot; // :D
private CarDirection carDir;
private CarPos carDestination;
//pozycja którą chce następnie zająć auto
private int next_pos_X = 0;
private int next_pos_Y = 0;
private int old_pos_X = 0;
private int old_pos_Y = 0;
private bool shouldFree = false;
crossing1.Program program;
crossing1.Road road;
SemaphoreSlim roadGod;
private int rightPosX = 999;
private int rightPosY = 999;
private int crossingPos = 999;
//i dont wanna to use road in retriving constants
public const int crossOuter1X = 12;//A
public const int crossOuter1Y = 11;
public const int crossOuter2X = 14;//B
public const int crossOuter2Y = 12;
public const int crossOuter3X = 11;//C
public const int crossOuter3Y = 13;
public const int crossOuter4X = 13;//D
public const int crossOuter4Y = 14;
private const bool hierarchy = true;
private bool roadWithoutPrio()
{
if (hierarchy)
{
//A has prio top
if (pos_X == crossOuter1X && pos_Y == crossOuter1Y)
{
return false;
}
//B right
if (pos_X == crossOuter2X && pos_Y == crossOuter2Y)
{
return true;
}
//C left
if (pos_X == crossOuter3X && pos_Y == crossOuter3Y)
{
return true;
}
//D bottom
if (pos_X == crossOuter4X && pos_Y == crossOuter4Y)
{
return true;
}
}
return true;
}
private bool getCarDecision()
{
if (randSpeed.Next(100) < randSpeed.Next(10, 70))
{
return true;
}
return false;
}
public Terminal.Gui.Attribute getCarColor()
{
return carColor;
}
public void setThread(Thread passed)
{
threadAction = passed;
}
public Thread getThread()
{
return threadAction;
}
public String getGraphic()
{
return graphic.ToString();
}
//use only with lock&unlock functionS!
public int getPosX()
{
return pos_X;
}
public int getPosY()
{
return pos_Y;
}
public void lockPosition()
{
positionMutex.WaitOne();
}
public void unlockPosition()
{
positionMutex.ReleaseMutex();
}
public Car(CarPos carPos, CarDirection carDirection, String carGraphic, Program program1, Road carRoad, SemaphoreSlim godOfRoad)
{
program = program1;
road = carRoad;
roadGod = godOfRoad;
speed = randSpeed.Next(100, 500);
carDir = carDirection;
switch (carDirection)
{
case CarDirection.FORWARD:
switch (carPos)
{
case CarPos.TOP:
carDestination = CarPos.BOTTOM;
break;
case CarPos.RIGHT:
carDestination = CarPos.LEFT;
break;
case CarPos.BOTTOM:
carDestination = CarPos.TOP;
break;
case CarPos.LEFT:
carDestination = CarPos.RIGHT;
break;
}
break;
case CarDirection.TURN_RIGHT:
switch (carPos)
{
case CarPos.TOP:
carDestination = CarPos.LEFT;
break;
case CarPos.RIGHT:
carDestination = CarPos.TOP;
break;
case CarPos.BOTTOM:
carDestination = CarPos.RIGHT;
break;
case CarPos.LEFT:
carDestination = CarPos.BOTTOM;
break;
}
break;
case CarDirection.TURN_LEFT:
switch (carPos)
{
case CarPos.TOP:
carDestination = CarPos.RIGHT;
break;
case CarPos.RIGHT:
carDestination = CarPos.BOTTOM;
break;
case CarPos.BOTTOM:
carDestination = CarPos.LEFT;
break;
case CarPos.LEFT:
carDestination = CarPos.TOP;
break;
}
break;
}
switch (carDestination)
{
case CarPos.TOP:
carColor = new Terminal.Gui.Attribute(Color.BrightGreen, Color.Black);
break;
case CarPos.RIGHT:
carColor = new Terminal.Gui.Attribute(Color.BrightYellow, Color.Black);
break;
case CarPos.BOTTOM:
carColor = new Terminal.Gui.Attribute(Color.BrightRed, Color.Black);
break;
case CarPos.LEFT:
carColor = new Terminal.Gui.Attribute(Color.BrightBlue, Color.Black);
break;
}
//pozniej zalezne od kierunku
graphic = carGraphic;
positionMutex = new Mutex();
lockPosition();
pos_X = road.getStartPointX(carPos);
pos_Y = road.getStartPointY(carPos);
posOrigin_X = pos_X;
posOrigin_Y = pos_Y;
unlockPosition();
switch (carPos)
{
case CarPos.TOP:
carRot = CarRotation.DOWN;
break;
case CarPos.RIGHT:
carRot = CarRotation.LEFT;
break;
case CarPos.BOTTOM:
carRot = CarRotation.UP;
break;
case CarPos.LEFT:
carRot = CarRotation.RIGHT;
break;
}
}
public bool checkIfCarExists()
{
bool safeCarExists = true;
carExistsMutex.WaitOne();
safeCarExists = carExists;
carExistsMutex.ReleaseMutex();
return safeCarExists;
}
public void ThreadProc()
{
while (program.getPrun())
{
Thread.Sleep(speed);
lockPosition();
bool temp = tryToMove();
unlockPosition();
if (!temp)
{
carExistsMutex.WaitOne();
carExists = false;
carExistsMutex.ReleaseMutex();
break;
}
}
}
//jak zawracaja: w oparciu o miejsce gdzie są i w oparciu o swoje destination
//licz nową pozycja w zależnośći od tego gdzie auto jedzie
//po dotarciu do skrzyrzowania na kazdym polu sprawdz kierunek
//po wykonaniu ruchu update pozycji next_pos
void step()
{
switch (carRot)
{
case CarRotation.UP:
next_pos_X = pos_X;
next_pos_Y = pos_Y - 1;//pos_Y--;
break;
case CarRotation.RIGHT:
next_pos_X = pos_X + 1;//pos_X++;
next_pos_Y = pos_Y;
break;
case CarRotation.DOWN:
next_pos_X = pos_X;
next_pos_Y = pos_Y + 1;//pos_Y++;
break;
case CarRotation.LEFT:
next_pos_X = pos_X - 1;//pos_X--;
next_pos_Y = pos_Y;
break;
}
}
void unlockYourLane(Road road)
{
if (posOrigin_X == 12 && posOrigin_Y == 1)
{//top
road.getOnlyOneMutex(0).WaitOne();
road.setOnlyOne(0, true);
road.getOnlyOneMutex(0).ReleaseMutex();
}
if (posOrigin_X == 23 && posOrigin_Y == 12)
{//right
road.getOnlyOneMutex(1).WaitOne();
road.setOnlyOne(1, true);
road.getOnlyOneMutex(1).ReleaseMutex();
}
if (posOrigin_X == 13 && posOrigin_Y == 23)
{//bottom
road.getOnlyOneMutex(3).WaitOne();
road.setOnlyOne(3, true);
road.getOnlyOneMutex(3).ReleaseMutex();
}
if (posOrigin_X == 1 && posOrigin_Y == 13)
{//left
road.getOnlyOneMutex(2).WaitOne();
road.setOnlyOne(2, true);
road.getOnlyOneMutex(2).ReleaseMutex();
}
}
bool singleMove()
{
step();
road.GetRoadMutex(next_pos_X, next_pos_Y).WaitOne();
if (road.checkSpace(next_pos_X, next_pos_Y))
{
//you can go into next_pos
//zajmij miejsce ,zmien miejsce, zwolnij stare miejsce
road.setSpaceOccupied(next_pos_X, next_pos_Y);
old_pos_X = pos_X;
old_pos_Y = pos_Y;
pos_X = next_pos_X;
pos_Y = next_pos_Y;
shouldFree = true;
}
else
{
shouldFree = false;
}
road.GetRoadMutex(next_pos_X, next_pos_Y).ReleaseMutex();
//avoiding nested mutex lock!!!
if (shouldFree)
{//
road.GetRoadMutex(old_pos_X, old_pos_Y).WaitOne();
road.setSpaceFree(old_pos_X, old_pos_Y);
road.GetRoadMutex(old_pos_X, old_pos_Y).ReleaseMutex();
if (pos_X == 13 && pos_Y == 11)
{
unlockYourLane(road);
}
if (pos_X == 14 && pos_Y == 13)
{
unlockYourLane(road);
}
if (pos_X == 11 && pos_Y == 12)
{
unlockYourLane(road);
}
if (pos_X == 12 && pos_Y == 14)
{
unlockYourLane(road);
}
return true;
}
return false;
}
void correctRotation(int pos_X, int pos_Y, CarPos carDestination)
{
switch (carDestination)
{
case CarPos.TOP:
if (pos_X == 12 && pos_Y == 12)
{
//niemozliwe
}
if (pos_X == 13 && pos_Y == 12)
{
carRot = CarRotation.UP;
}
if (pos_X == 12 && pos_Y == 13)
{
carRot = CarRotation.RIGHT;
}
if (pos_X == 13 && pos_Y == 13)
{
carRot = CarRotation.UP;
}
break;
case CarPos.RIGHT:
if (pos_X == 12 && pos_Y == 12)
{
carRot = CarRotation.DOWN;
}
if (pos_X == 13 && pos_Y == 12)
{
//niemozliwe
}
if (pos_X == 12 && pos_Y == 13)
{
carRot = CarRotation.RIGHT;
}
if (pos_X == 13 && pos_Y == 13)
{
carRot = CarRotation.RIGHT;
}
break;
case CarPos.LEFT:
if (pos_X == 12 && pos_Y == 12)
{
carRot = CarRotation.LEFT;
}
if (pos_X == 13 && pos_Y == 12)
{
carRot = CarRotation.LEFT;
}
if (pos_X == 12 && pos_Y == 13)
{
//niemozliwe
}
if (pos_X == 13 && pos_Y == 13)
{
carRot = CarRotation.UP;
}
break;
case CarPos.BOTTOM:
if (pos_X == 12 && pos_Y == 12)
{
carRot = CarRotation.DOWN;
}
if (pos_X == 13 && pos_Y == 12)
{
carRot = CarRotation.LEFT;
}
if (pos_X == 12 && pos_Y == 13)
{
carRot = CarRotation.DOWN;
}
if (pos_X == 13 && pos_Y == 13)
{
//niemożliwe
}
break;
}
}
bool endOfRoad(int pos_X, int pos_Y, Road road)
{
if (pos_X >= 24 || pos_Y >= 24 || pos_X <= 0 || pos_Y <= 0)
{
road.GetRoadMutex(pos_X, pos_Y).WaitOne();
road.setSpaceFree(pos_X, pos_Y);
road.GetRoadMutex(pos_X, pos_Y).ReleaseMutex();
return true;
}
return false;
}
bool tryToMove()
{
if (endOfRoad(pos_X, pos_Y, road))
{
return false;
}
//wykonaj ruch jeżeli możesz
if (road.straightRoad(pos_X, pos_Y))
{
singleMove();
}
else
{ //crossing rules
roadGod.Wait();
getRightCarPositions(pos_X, pos_Y);
bool lanePassPermission = false;
road.getOnlyOneMutex(crossingPos).WaitOne();
lanePassPermission = road.getOnlyOne(crossingPos);
road.getOnlyOneMutex(crossingPos).ReleaseMutex();
if (lanePassPermission)
{
if (carDir == CarDirection.TURN_RIGHT)
{
//singleMove();
if (singleMove())
{
road.getOnlyOneMutex(crossingPos).WaitOne();
lanePassPermission = road.getOnlyOne(crossingPos);
road.setOnlyOne(crossingPos, false);
road.getOnlyOneMutex(crossingPos).ReleaseMutex();
}
//..^-..ZABLOKUJ PAS
}
else
{
//otworz mutex sasiada i zamknij go dopiero po rozpatrzeniu calej skewencji ruchu
// road.GetRoadMutex(rightPosX, rightPosY).WaitOne();
//
road.GetRoadMutex(12, 12).WaitOne();
road.GetRoadMutex(13, 12).WaitOne();
road.GetRoadMutex(12, 13).WaitOne();
road.GetRoadMutex(13, 13).WaitOne();
if ((!road.checkSpace(rightPosX, rightPosY)) && roadWithoutPrio())
{
//warunek- jeśli po twojej prawej jest false, to znaczy ze ktos tam stoi
//hierarchia zasobów- zignoruj to że ktoś stoi bo pas na którym jesteś ma pierwszeństwo
//wait for your turn
}
else
{
//youCanTryDrive();
int ccNumber = 0;
if (!road.checkSpace(12, 12))
{
ccNumber++;
}
if (!road.checkSpace(13, 12))
{
ccNumber++;
}
if (!road.checkSpace(12, 13))
{
ccNumber++;
}
if (!road.checkSpace(13, 13))
{
ccNumber++;
}
//!!!!!!!!!...w tym miejscu trzeba trzymac zalokowany mutex drogi na którą się wybiera nasz auto
//puszczamy dopiero po skonczeniu ruchu auta
if (ccNumber >= 3)
{
//u cant drive
}
else
{
//kolizje hipotetyczne
if (!roadWithoutPrio())
{
//przy wykrywaniu kolizji powinien byc tryb z tylko jedna droga pierwszenstwa
//dwie dorgi z pierwszeństwem poskutkowałyby podwójnym losowaniem czy zaszła kolizja
int ccNumber2 = 0;
if (!road.checkSpace(12, 12) || getCarDecision())
{
ccNumber2++;
}
if (!road.checkSpace(13, 12) || getCarDecision())
{
ccNumber2++;
}
if (!road.checkSpace(12, 13) || getCarDecision())
{
ccNumber2++;
}
if (!road.checkSpace(13, 13) || getCarDecision())
{
ccNumber2++;
}
if (ccNumber2 == 4)
{
road.getCrossingCarsNumberMutex().WaitOne();//bad name but already existed
road.incCrossingCarsNumber();
road.getCrossingCarsNumberMutex().ReleaseMutex();
}
//co z przypadkiem keidy choc jedno auto skreca w prawo?
//komu to sprawdzac- wszelkim autom na skrzyżowaniu?
}
//u can drive
singleMove();
}
}
road.GetRoadMutex(13, 13).ReleaseMutex();
road.GetRoadMutex(12, 13).ReleaseMutex();
road.GetRoadMutex(13, 12).ReleaseMutex();
road.GetRoadMutex(12, 12).ReleaseMutex();
//roadGod.Release();
}
}
else
{
//wait for your turn
}
roadGod.Release();
}
//car after trying to move
correctRotation(pos_X, pos_Y, carDestination);
return true;
}
void getRightCarPositions(int pos_X, int pos_Y)
{
// A
// X X B
//C X X
// D
//A
if (pos_X == crossOuter1X && pos_Y == crossOuter1Y)
{
rightPosX = crossOuter3X;//C
rightPosY = crossOuter3Y;
crossingPos = 0;
}
//B
if (pos_X == crossOuter2X && pos_Y == crossOuter2Y)
{
rightPosX = crossOuter1X;//A
rightPosY = crossOuter1Y;
crossingPos = 1;
}
//C
if (pos_X == crossOuter3X && pos_Y == crossOuter3Y)
{
rightPosX = crossOuter4X;
rightPosY = crossOuter4Y;
crossingPos = 2;
}
//D
if (pos_X == crossOuter4X && pos_Y == crossOuter4Y)
{
rightPosX = crossOuter2X;
rightPosY = crossOuter2Y;
crossingPos = 3;
}
}
}
}