summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/msc/msc.sty
blob: 889bb9a3a051f37a53e9b7bf40a437023c9b738a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
%% MSC Macro Package
%% msc.sty
%%
%% Copyright 2022 V. Bos, T. van Deursen, P. Kordy,  and S. Mauw
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is P. Kordy
%
% This program consists of the files
%   msc.sty
%   manual.tex
%   manual_macros.tex
%   biblio.bib
%   README
%   COPYRIGHT

% Contact address:
% Reynaldo Gil Pons
% Université du Luxembourg 
% Maison du Nombre
% 6 Av. de la Fonte
% L-4364 Esch-sur-Alzette
% Email: reynaldo.gilpons@uni.lu
% Website: http://satoss.uni.lu/mscpackage/

\def\mscdate{2022/05/13}% update this whenever this file changes
\def\mscversion{2.00}%   update this whenever a new version is ready
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{msc}[\mscdate, v\mscversion]
\typeout{msc version \mscversion. (\mscdate)}
\RequirePackage{tikz,xstring,calc}
\usetikzlibrary{
  positioning,
  fit,
  calc,
  arrows,
  decorations.markings,
  shapes.misc,
  shapes.geometric,
  shapes.symbols
}
\pgfdeclarelayer{background} %background for drawing grid
\pgfsetlayers{background,main}


% {string}{target1}{target2}{code for match}{code for no match}
\newcommand*{\msc@StrEqEither}[5]{
    \IfEqCase{#1}{{#2}{#4}{#3}{#4}}[#5]
}
\newcommand*{\msc@caseFiveEither}[8]{
    \IfEqCase{#1}{{#2}{#7}{#3}{#7}{#4}{#7}{#5}{#7}{#6}{#7}}[#8]
}
%\newlength{\msc@textheight}
%\newlength{\msc@textdepth}
%\newlength{\msc@textwidth}
\newsavebox{\msc@box}
\newcommand{\msc@savebox}[1]{
  \savebox{\msc@box}{\pgfinterruptpicture#1\endpgfinterruptpicture}
}
%
%% \tracingmacros=2 \tracingcommands=2 %used for debugging
% "msc" is a family
\pgfkeys{/msc/.is family}
% set up a search path:
\pgfkeys{/msc/.search also={/tikz}}
% \pgfkeys{/msc/message/.search also={/msc}}
\newif\ifmsc@isstar
\newif\ifmsc@ismessreplay
\def\mscset{\pgfqkeys{/msc}}
\def\mscget#1{\pgfkeysvalueof{/msc/#1}}
%  * Arrow *
\pgfarrowsdeclare{mscarrow}{mscarrow}
{
  \pgfmathparse{\pgfgetarrowoptions{mscarrow}}%
  \ifpgfmathunitsdeclared%
    \pgfmathparse{\pgfmathresult pt}%
  \else%
    \pgfmathparse{\pgfmathresult*\pgflinewidth}%
  \fi%
  \let\thickness=\pgfmathresult
%   \edef\msc@thickness=\pgfmathresult
  \pgfutil@tempdima=0.28pt%
  \pgfutil@tempdimb=\pgfmathresult pt
  \ifdim\pgfinnerlinewidth>0pt%
    \pgfmathsetlength\pgfutil@tempdimb{.6\pgfutil@tempdimb-.4*\pgfinnerlinewidth}%
  \fi%
  \advance\pgfutil@tempdima by.3\pgfutil@tempdimb%
  \pgfarrowsleftextend{-3\pgfutil@tempdima}
  \pgfarrowsrightextend{+5\pgfutil@tempdima}
}
{
  \pgfmathparse{\pgfgetarrowoptions{mscarrow}}%
  \ifpgfmathunitsdeclared%
    \pgfmathparse{\pgfmathresult pt}%
  \else%
    \pgfmathparse{\pgfmathresult*\pgflinewidth}%
  \fi%
  \let\thickness=\pgfmathresult
  \pgfsetlinewidth{\thickness pt}
  \pgfutil@tempdima=0.28pt%
  \pgfutil@tempdimb=\pgflinewidth%
  \ifdim\pgfinnerlinewidth>0pt%
    \pgfmathsetlength\pgfutil@tempdimb{.6\pgflinewidth-.4*\pgfinnerlinewidth}%
  \fi%
  \advance\pgfutil@tempdima by.3\pgfutil@tempdimb%
  \pgfpathmoveto{\pgfqpoint{5\pgfutil@tempdima}{0pt}}
  \pgfpathlineto{\pgfqpoint{-3\pgfutil@tempdima}{4\pgfutil@tempdima}}
  \pgfpathlineto{\pgfpointorigin}
  \pgfpathlineto{\pgfqpoint{-3\pgfutil@tempdima}{-4\pgfutil@tempdima}}
  \pgfusepathqfill
}
\newlength{\actionheight}               % height of action symbols
\newlength{\actionwidth}                % width of action symbol
\newlength{\bottomfootdist}             % distance between bottom of foot symbol and frame
\newlength{\msccommentdist}             % distance of comment to its instance
\newlength{\conditionheight}            % height of condition symbols
\newlength{\conditionoverlap}           % overlap of condition symbol
\newlength{\envinstdist}                % distance between environments and nearest instance line
\newlength{\firstlevelheight}           % height of level just below head symbols
\newlength{\gatesymbolradius}           % radius of the gate symbol
\newlength{\hmscconditionheight}        % height of hmsc condition symbol
\newlength{\hmscconditionwidth}         % width of hmsc condition symbol
\newlength{\hmscconnectionradius}       % radius of hmsc connection symbols
\newlength{\hmscreferenceheight}        % % height of hmsc and mscdoc reference symbol
\newlength{\hmscreferencewidth}         % width of hmsc and mscdoc reference symbol
\newlength{\hmscstartsymbolwidth}       % width of hmsc start/end symbol
\newlength{\inlineoverlap}              % overlap of inline symbol
\newlength{\instbarwidth}               % default width of vertical instance bars
\newlength{\instdist}                   % distance between (vertical) instance lines
\newlength{\instfootheight}             % height of foot symbols
\newlength{\instheadheight}             % height of head symbols
\newlength{\instwidth}                  % width of header and foot symbols
\newlength{\labeldist}                  % distance between labels and message lines or head symbol boxes
\newlength{\lastlevelheight}            % height of level just above foot symbols
\newlength{\leftnamedist}               % distance between left of frame and (top of) msc title
\newlength{\levelheight}                % height of a level
\newlength{\lostsymbolradius}           % radius of the lost and found symbols
\newlength{\markdist}                   % distance between mark and its instance
\newlength{\measuredist}                % user definable length for horizontal measure distance
\newlength{\measuresymbolwidth}         % width of measure symbols
\newlength{\mscdocreferenceheight}      % minimal height of mscdoc reference
\newlength{\mscdocreferencewidth}       % minimal width of mscdoc reference
\newlength{\referenceoverlap}           % overlap of reference symbol
\newlength{\regionbarwidth}             % the width of the coregion start and end symbol
\newlength{\selfmesswidth}              % length of horizontal arms of self messages
\newlength{\stopwidth}                  % width of the stop symbol
\newlength{\timerwidth}                 % width of the timer symbols
\newlength{\topheaddist}                % distance between top of head symbols and frame
\newlength{\topnamedist}                % distance between top of frame and (top of) msc title
\mscset{%
  %mscdoc keys
  /msc/mscdoc margin/.code={
    \mscset{
      north mscdoc margin/.initial=#1,
      south mscdoc margin/.initial=#1,
      east mscdoc margin/.initial=#1,
      west mscdoc margin/.initial=#1
     }
  },
  top mscdoc margin/.style={north mscdoc margin=#1},
  bottom mscdoc margin/.style={south mscdoc margin=#1},
  left mscdoc margin/.style={west mscdoc margin=#1},
  right mscdoc margin/.style={east mscdoc margin=#1},
  /msc/mscdoc margin=0.5cm,
  /msc/mscdoc keyword/.initial=mscdoc,
  %hmsc keys
  /msc/hmsc keyword/.initial=hmsc,
  /msc/hmsc margin/.code={
    \mscset{
      north hmsc margin/.initial=#1,
      south hmsc margin/.initial=#1,
      east hmsc margin/.initial=#1,
      west hmsc margin/.initial=#1
     }
  },
  top hmsc margin/.style={north hmsc margin=#1},
  bottom hmsc margin/.style={south hmsc margin=#1},
  left hmsc margin/.style={west hmsc margin=#1},
  right hmsc margin/.style={east hmsc margin=#1},
  /msc/hmsc margin=0.5cm,
  %msc keys
  /msc/msc keyword/.initial=msc,
  /msc/inline overlap/.code={
    \mscset{
      right inline overlap/.initial=#1,
      left inline overlap/.initial=#1
     }
  },
  environment distance/.code={
    \mscset{
      right environment distance/.initial=#1,
      left environment distance/.initial=#1
     }
  },                                                 % distance between environments and the % first level
  /msc/reference overlap/.code={
    \mscset{
      right reference overlap/.initial=#1,
      left reference overlap/.initial=#1
     }
  },
  /msc/message options/.is family,     %options for message
  /msc/action options/.is family,      %options for action and naction
  /msc/msccomment options/.is family,  %options for message comments
% Default value keys
  /msc/line width/.initial=0.7pt,
  side/.initial=left,                  %side on which to draw self message
  position/.initial=above,             %how to draw mscmark - possible %values:above mid below 
  /msc/side/.belongs to family=/msc/message options,
  /msc/side/.belongs to family=/msc/msccomment options,
  /msc/msccomment distance/.belongs to family=/msc/msccomment options,
  /msc/offset/.belongs to family=/msc/msccomment options,
  /msc/offset/.belongs to family=/msc/message options,
  /msc/timer width/.belongs to family=/msc/msccomment options,
  /msc/self message width/.belongs to family=/msc/msccomment options,
  /msc/label distance/.belongs to family=/msc/msccomment options,
  /msc/label position/.initial=above, %position of label in self message
  /msc/label position/.belongs to family=/msc/message options,
  /msc/level shift/.initial=0,        %level shift in message
  /msc/offset/.initial=2,             %level shift in timers/self message etc
  /msc/dummy text/.initial={ },       %text used for determining the size of dummy node
  /msc/level shift/.belongs to family=/msc/message options,
  /msc/pos/.initial=0.5,              %placement of label in message
  /msc/pos/.belongs to family=/msc/message options,
  level offset/.initial=1,            %level shift in nextlevel
  level offset/.default=1,            
  /tikz/font=\small,
  /msc/draw frame/.initial=,       %color of the frame - if "none" we do not draw - "empty" uses last color usually black
  /msc/draw frame/.default=,       
  /msc/draw head/.initial=,       %color of the instance head
  /msc/draw head/.default=,       
  /msc/draw foot/.initial=,       %color of the instance foot
  /msc/draw foot/.default=,       
  /msc/draw grid/.initial=none,       %style to draw help lines - if empty we do
                                %not draw, other possible values are "none", "grid" or "color grid"
  /msc/draw grid/.default=grid,       
  /msc/draw color grid/.style={draw grid=color grid},       
% the lengths
  fat/.initial=normal,
  fat/.default=fat,
  %sizes
  /tikz/font=\normalsize,                            % use normal size of font
  action height/.initial=\the\actionheight,          % height of action symbols
  action height/.code={\setlength{\actionheight}{#1}},
  action width/.initial=\the\actionwidth,            % width of action symbol
  action width/.code={\setlength{\actionwidth}{#1}},
  foot distance/.initial=\the\bottomfootdist,        % distance between bottom of foot symbol and frame
  foot distance/.code={\setlength{\bottomfootdist}{#1}},
  msccomment distance/.initial=\the\msccommentdist,  % distance of comment to its instance
  msccomment distance/.code={\setlength{\msccommentdist}{#1}},     % distance of comment to its instance
  condition height/.initial=\the\conditionheight,    % height of condition symbols
  condition height/.code={\setlength{\conditionheight}{#1}},        % height of condition symbols
  condition overlap/.initial=\the\conditionoverlap,  % overlap of condition symbol
  condition overlap/.code={\setlength{\conditionoverlap}{#1}},       % overlap of condition symbol
  first level height/.initial=\the\firstlevelheight, % height of level just below head symbols
  first level height/.code={\setlength{\firstlevelheight}{#1}},
  gate symbol radius/.initial=\the\gatesymbolradius, % radius of the gate symbol
  gate symbol radius/.code={\setlength{\gatesymbolradius}{#1}},
  hmsc condition height/.initial=\the\hmscconditionheight,   % height of hmsc condition symbol
  hmsc condition height/.code={\setlength{\hmscconditionheight}{#1}},
  hmsc condition width/.initial=\the\hmscconditionwidth,     % width of hmsc condition symbol
  hmsc condition width/.code={\setlength{\hmscconditionwidth}{#1}},
  hmsc connection radius/.initial=\the\hmscconnectionradius, % radius of hmsc connection symbols
  hmsc connection radius/.code={\setlength{\hmscconnectionradius}{#1}},
  reference height/.initial=\the\hmscreferenceheight,        % height of hmsc and mscdoc reference symbol
  reference height/.code={\setlength{\hmscreferenceheight}{#1}},
  reference width/.initial=\the\hmscreferencewidth,          % width of hmsc and mscdoc reference symbol
  reference width/.code={\setlength{\hmscreferencewidth}{#1}},        % width of hmsc and mscdoc reference symbol
  hmsc symbol width/.initial=\the\hmscstartsymbolwidth,     % width of hmsc start/end symbol
  hmsc symbol width/.code={\setlength{\hmscstartsymbolwidth}{#1}},  % width of hmsc start/end symbol
  instance distance/.initial=\the\instdist,                 % distance between (vertical) instance lines
  instance distance/.code={\setlength{\instdist}{#1}},
  foot height/.initial=\the\instfootheight,                 % height of foot symbols
  foot height/.code={\setlength{\instfootheight}{#1}},
  head height/.initial=\the\instheadheight,                 % height of head symbols
  head height/.code={\setlength{\instheadheight}{#1}},
  instance width/.initial=\the\instwidth,                   % width of header and foot symbols
  instance width/.code={\setlength{\instwidth}{#1}},
  label distance/.initial=\the\labeldist,                   % distance between labels and message lines or head symbol boxes
  label distance/.code={\setlength{\labeldist}{#1}},
  last level height/.initial=\the\lastlevelheight,          % height of level just above foot symbols
  last level height/.code={\setlength{\lastlevelheight}{#1}},
  title distance/.initial=\the\leftnamedist,                % distance between left of frame and (top of) msc title
  title distance/.code={\setlength{\leftnamedist}{#1}},
  level height/.initial=\the\levelheight,                   % height of a level
  level height/.code={\setlength{\levelheight}{#1}},
  lost symbol radius/.initial=\the\lostsymbolradius,        % radius of the lost and found symbols
  lost symbol radius/.code={\setlength{\lostsymbolradius}{#1}},
  mark distance/.initial=\the\markdist,                     % distance between mark and its instance
  mark distance/.code={\setlength{\markdist}{#1}},
  measure distance/.initial=\the\measuredist,               % user definable length for horizontal measure distance
  measure distance/.code={\setlength{\measuredist}{#1}},
  measure symbol width/.initial=\the\measuresymbolwidth,    % width of measure symbols
  measure symbol width/.code={\setlength{\measuresymbolwidth}{#1}},
%   reference overlap/.initial=\the\referenceoverlap,         % overlap of reference symbol
%   reference overlap/.code={\setlength{\referenceoverlap}{#1}},
  region width/.initial=\the\regionbarwidth,           % the width of the coregion start and end symbol
  region width/.code={\setlength{\regionbarwidth}{#1}},
%   region width/.initial=0.4cm,                             % the width of the activation, suspension and coregion rectangle
  self message width/.initial=\the\selfmesswidth,          % length of horizontal arms of self messages
  self message width/.code={\setlength{\selfmesswidth}{#1}},
  stop width/.initial=\the\stopwidth,                      % width of the stop symbol
  stop width/.code={\setlength{\stopwidth}{#1}},
  timer width/.initial=\the\timerwidth,                    % width of the timer symbols
  timer width/.code={\setlength{\timerwidth}{#1}},
  head top distance/.initial=\the\topheaddist,             % distance between top of head symbols and frame
  head top distance/.code={\setlength{\topheaddist}{#1}},
  title top distance/.initial=\the\topnamedist,            % distance between top of frame and (top of) msc title
  title top distance/.code={\setlength{\topnamedist}{#1}},
  instance end/.initial=stop,                              % type of ending for the instance foot (foot or stop) - changed to follow ITU-T Z.120
  title position/.initial=left,                            % justification of the title: left, right or center
  arrow scale/.initial=1.5,                                % scale for the
                                % arrows
  msc scale/.initial=1,                                    % sets the scale factor
  msc scale/.code={                                        % that scales everynthing including text but one must be
    \pgfutil@ifundefined{msc@tikzstarted}{\relax}{         % careful with rotation transformations
      \mscset{
      /tikz/scale=#1,
      /tikz/every node/.style={scale=#1}}
    }
   \mscset{msc scale/.initial=#1}
  },
%Styles
  every instance head/.style={},             % style applied to all instance heads
  every instance foot/.style={},             % style applied to all instance feet
  every instance line/.style={},             % style applied to all instance lines
  every message/.style={},                   % style applied to all message arrows
  every msccomment/.style={},                % style applied to all msccomments
  every mscmark/.style={},                   % style applied to all mscmarks
  every action/.style={},                    % style applied to all actions and nactions
  every action node/.style={},               % style applied to all actions and nactions nodes
  every region/.style={},                    % style applied to all regions
  every coregion/.style={},                  % style applied to all coregions
  every suspension/.style={},                % style applied to all suspension regions
  every activation/.style={},                % style applied to all activation regions
  every timer/.style={},                     % style applied to simple timers
  every measure/.style={},                   % style applied to all measures
  every lostfound/.style={},                 % style applied to all lost and found messages
  every condition/.style={},                 % style applied to all conditions
  every order/.style={},                     % style applied to all orders
  every reference/.style={},                 % style applied to all references
  every inline/.style={},                    % style applied to all inline expressions
  large values/.style={%
    /tikz/font=\normalsize,
    /tikz/line width=0.8pt,
    /msc/line width=0.8pt,
    action height=0.75cm,
    /msc/action height/.belongs to family=/msc/action options,
    action width=1.25cm,
    /msc/action width/.belongs to family=/msc/action options,
    foot distance=1cm,
    msccomment distance=1.5cm,
    condition height=0.75cm,
    condition overlap=0.6cm,
    environment distance=2.5cm,
    first level height=0.75cm,
    gate symbol radius=0.5mm,
    hmsc condition height=0.8cm,
    hmsc condition width=1.6cm,
    hmsc connection radius=0.06cm,
    reference height=0.8cm,
    reference width=1.6cm,
    hmsc symbol width=0.85cm,
    inline overlap=1.5cm,
    instance distance=1.25cm,
    foot height=0.25cm,
    head height=0.6cm,
    instance width=1.75cm,
    label distance=1ex,
    last level height=0.5cm,
    title distance=0.3cm,
    level height=0.75cm,
    lost symbol radius=0.15cm,
    mark distance=1cm,
    measure distance=0.75cm,
    measure symbol width=0.25cm,
    reference overlap=1.5cm,
    region width=0.5cm,
    self message width=0.75cm,
    stop width=0.6cm,
    timer width=0.4cm,
    head top distance=1.5cm,
    title top distance=0.3cm,
    arrow scale=2
   },
  normal values/.style={%
%     /tikz/font=\small,%
    /tikz/line width=0.7pt,
    /msc/line width=0.7pt,
    action height=0.6cm,
    action width=1.25cm,
    foot distance=0.7cm,
    msccomment distance=1.1cm,
    condition height=0.6cm,
    condition overlap=0.5cm,
    environment distance=2cm,
    first level height=0.6cm,
    gate symbol radius=0.5mm,
    hmsc condition height=0.7cm,
    hmsc condition width=1.4cm,
    hmsc connection radius=0.05cm,
    reference height=0.7cm,
    reference width=1.4cm,
    hmsc symbol width=0.7cm,
    inline overlap=1cm,
    instance distance=0.6cm,
    foot height=0.2cm,
    head height=0.55cm,
    instance width=1.6cm,
    label distance=1ex,
    last level height=0.4cm,
    title distance=0.2cm,
    level height=0.5cm,
    lost symbol radius=0.12cm,
    mark distance=1cm,
    measure distance=0.6cm,
    measure symbol width=0.2cm,
    reference overlap=1cm,
    region width=0.4cm,
    self message width=0.6cm,
    stop width=0.5cm,
    timer width=0.3cm,
    head top distance=1.3cm,
    title top distance=0.2cm,
    arrow scale=1.5
  },
  small values/.style={%
    /tikz/font=\small,%
    /tikz/line width=0.6pt,
    /msc/line width=0.6pt,
    action height=0.5cm,
    action width=1.2cm,
    foot distance=0.5cm,
    msccomment distance=0.75cm,
    condition height=0.5cm,
    condition overlap=0.4cm,
    environment distance=1.2cm,
    first level height=0.4cm,
    gate symbol radius=0.5mm,
    hmsc condition height=0.6cm,
    hmsc condition width=1.2cm,
    hmsc connection radius=0.04cm,
    reference height=0.6cm,
    reference width=1.2cm,
    hmsc symbol width=0.4cm,
    inline overlap=0.75cm,
    instance distance=0.3cm,
    foot height=0.15cm,
    head height=0.5cm,
    instance width=1.2cm,
    label distance=0.8ex,
    last level height=0.3cm,
    title distance=0.1cm,
    level height=0.4cm,
    lost symbol radius=0.08cm,
    mark distance=0.4cm,
    measure distance=0.4cm,
    measure symbol width=0.15cm,
    reference overlap=0.75cm,
    region width=0.2cm,
    self message width=0.4cm,
    stop width=0.3cm,
    timer width=0.2cm,
    head top distance=1.2cm,
    title top distance=0.1cm,
    arrow scale=1.2
  },
  /msc/start symbol/.style={draw,/tikz/line width=\mscget{line width},
    inner sep=0,
    isosceles triangle,isosceles triangle apex angle=60,
    shape border rotate=-90,minimum width=\mscget{hmsc symbol width}},
    /msc/end symbol/.style={start symbol,shape border rotate=90},
%style to %draw hmsc condition
  /msc/condition/.style={draw,/tikz/line width=\mscget{line width},
    minimum width=\mscget{hmsc condition width},
    minimum height=\mscget{hmsc condition height},
    inner sep=0pt,
    signal pointer angle=120,
    signal to=east and west,
    signal},
  /msc/connection/.style={draw,/tikz/line width=\mscget{line width},
    minimum width=2*(\mscget{hmsc connection radius}) + \mscget{line width},
    circle
  },
  /msc/reference/.style={draw,/tikz/line width=\mscget{line width},
    minimum width=\mscget{reference width},
    minimum height=\mscget{reference height},
    inner sep=2pt,
    rounded corners=0.125cm,
    rectangle,
  },
  mscdash/.style={%
    dash pattern=on 4pt off 4pt
  },
  suspension dash/.style={%
    dash pattern=on 7pt off 7pt,
    dash phase=3.5pt
  },
  settimer style/.style={%
    /tikz/line width=\mscget{line width}, draw,
    to path={-- ++(60:#1) -- ++(-#1,0) -- ++(-60:2*#1) -- ++(-#1,0) -- ++(60:#1)  -- (\tikztotarget)}
  },
  timeout style/.style={%
    settimer style={#1}, arrow style
  },
  stoptimer style/.style={%
    /tikz/line width=\mscget{line width}, draw,
    to path={ ++(0.5*#1,0.5*#1) -- ++(-#1,-#1) ++(0,#1) -- ++(#1,-#1) ++(-0.5*#1,0.5*#1) -- (\tikztotarget)}
  },
  set mscarrow thickness/.code={
     \pgfsetarrowoptions{mscarrow}{#1}},
  arrow style/.default=\mscget{arrow scale}*\mscget{line width},
  arrow style/.style={set mscarrow thickness=#1,
    shorten >=\mscget{line width}/2,
    -mscarrow
  },
  instance head/.style={%
    rectangle,
    text height=1.5ex,%
    inner sep=2pt,
%     text width=\mscget{instance width}, text centered,%uncomment to get old behaviour
%     transform shape,
    /tikz/line width=\mscget{line width},
    minimum height=\mscget{head height},%
    minimum width=\mscget{instance width},%
    label={[text height=1.5ex,text depth=0ex,
      inner sep=0pt, /tikz/label distance=\mscget{label
        distance}-\mscget{line width}-0.1ex]above:#1}%
  },
  replay/.style={%
    /tikz/line width=\mscget{line width},
    line cap=butt,
    mscdash
  },
  msccomment/.style={%
    /tikz/line width=\mscget{line width},
    line cap=round,
  },
  msccomment line/.style={%
    mscdash
  },
  message/.style={%
    /tikz/line width=\mscget{line width},
    arrow style,
    line cap=round,
  },
  %action/.style={%
    %/tikz/line width=\mscget{line width},
    %line cap=round,
  %},
  instance line/.style={%
    /tikz/line width=\mscget{line width},
    draw,%orange
%     line cap=round
  },
  message loop/.default=\mscget{self message width},
  message loop/.style={%
    to path={-- ++(#1,0) coordinate (msc@somepoint) -- (msc@somepoint |-
      \tikztotarget) \tikztonodes -- (\tikztotarget)}
  },
  order loop/.default=\mscget{self message width},
  order loop/.style={%
    set mscarrow thickness=\mscget{arrow scale}*\mscget{line width},
    decoration={markings, mark=at position \mscget{pos} with {\arrow{mscarrow}}},
    /tikz/line width=\mscget{line width},postaction={decorate},
    line cap=round,
    mscdash,
    to path={-- ++(#1,0) coordinate (msc@somepoint) -- (msc@somepoint |-
      \tikztotarget) \tikztonodes -- (\tikztotarget)}
  },
  order/.style={%
    set mscarrow thickness=\mscget{arrow scale}*\mscget{line width},
    decoration={markings, mark=at position \mscget{pos} with {\arrow{mscarrow}}},
    /tikz/line width=\mscget{line width},postaction={decorate},
    line cap=round,
    mscdash,
  },
  settimeout style/.default={\mscget{self message width}}{\mscget{timer width}},
  settimeout style/.style 2 args={/msc,
    /tikz/line width=\mscget{line width}, draw, arrow style,
    to path={-- ++(#1,0) coordinate (msc@somepoint) -- ++(60:#2) --
      ++(-#2,0)--++(-60:2*#2) coordinate (msc@somepoin) -- ++(-#2,0) --(msc@somepoint)
      ($ (msc@somepoint)!0.866*#2!(\tikztotarget-|msc@somepoint) $) --
      (\tikztotarget-|msc@somepoint) -> (\tikztotarget)
    }
  },
  setstoptimer style/.default={\mscget{self message width}}{\mscget{timer width}},
  setstoptimer style/.style 2 args={/msc,
    /tikz/line width=\mscget{line width}, draw,
    to path={-- ++(#1,0) coordinate (msc@somepoint) -- ++(60:#2) --
      ++(-#2,0)--++(-60:2*#2) coordinate (msc@somepoin) -- ++(-#2,0) --(msc@somepoint)
      ($ (msc@somepoint)!0.866*#2!(\tikztotarget-|msc@somepoint) $) --
      (\tikztotarget-|msc@somepoint) ++(0.5*#2,0.5*#2) -- ++(-#2,-#2)
      ++(0,#2) -- ++(#2,-#2) ++(-0.5*#2,0.5*#2) --
      (\tikztotarget)
  }},
  measure style/.default={\mscget{measure distance}}{\mscget{measure symbol width}},
  measure style/.style 2 args={%
    /tikz/line width=\mscget{line width}, mscdash,
    to path={-- ++(#1,0) coordinate (a)
      ($ (a)!0.866*#2!(\tikztotarget-|a)$) -- ($(\tikztotarget-|a) !0.866*#2!(a) $)
      (a|-\tikztotarget) \tikztonodes coordinate (b) -- (\tikztotarget)
      \pgfextra{ \path[draw,/msc/mscdash,/tikz/line width=\mscget{line width}];}
      (a)--($ (a)!#2!30:(\tikztotarget-|a)$) coordinate (a1)--($ (a1)!#2!60:(a)$) --cycle(a)
      (b)--($ (b)!#2!30:(a)$) coordinate (b1)--($ (b1)!#2!60:(b)$) --cycle(b)
      \pgfextra{ \path[draw,solid,/tikz/line width=\mscget{line width}];}
      (\tikztotarget)
  }},
  measure* style/.default={\mscget{measure distance}}{\mscget{measure symbol width}},
  measure*  style/.style 2 args={%
    /tikz/line width=\mscget{line width}, mscdash,
    to path={
      -- ++(#1,0) coordinate (a)-- (\tikztotarget-|a) coordinate (b)\tikztonodes
      -- (\tikztotarget)
      \pgfextra{ \path[draw,/msc/mscdash,/tikz/line width=\mscget{line width}];}
      (a)--($ (a)!#2!210:(b)$) coordinate (a1)--($ (a1)!#2!60:(a)$) --cycle(a)
      (b)--($ (b)!#2!210:(a)$) coordinate (b1)--($ (b1)!#2!60:(b)$) --cycle(b)
      \pgfextra{ \path[draw,solid,/tikz/line width=\mscget{line width}];}
      (\tikztotarget)
  }},
  measurestart style/.default={\mscget{measure distance}}{\mscget{measure symbol width}},
  measurestart style/.style 2 args={%
    /tikz/line width=\mscget{line width}, mscdash,
    to path={
      -- ++(#1,0) coordinate (a)
      ($ (a)!0.866*#2!(\tikztotarget-|a)$)-- ($(\tikztotarget-|a)! 0.5*#2!(a) $) \tikztonodes
      (\tikztotarget -| a) coordinate (b)
      \pgfextra{ \path[draw,/msc/mscdash,/tikz/line width=\mscget{line width}];}
      (a)--($ (a)!#2!30:(\tikztotarget-|a)$) coordinate (a1)--($ (a1)!#2!60:(a)$) --cycle(a)
      (b) circle (0.5*#2)
      \pgfextra{ \path[draw,solid,/tikz/line width=\mscget{line width}];}
      (\tikztotarget)
  }},
  measurestart* style/.default={\mscget{measure distance}}{\mscget{measure symbol width}},
  measurestart* style/.style 2 args={%
    /tikz/line width=\mscget{line width}, mscdash,
    to path={
      -- ++(#1,0) coordinate (a) -- ($(\tikztotarget-|a)! 0.5*#2!(a) $) \tikztonodes
      (\tikztotarget -| a) coordinate (b)
      \pgfextra{ \path[draw,/msc/mscdash,/tikz/line width=\mscget{line width}];}
      (a)--($ (a)!#2!210:(b)$) coordinate (a1)--($ (a1)!#2!60:(a)$) --cycle(a)
      (b) circle (0.5*#2)
      \pgfextra{ \path[draw,solid,/tikz/line width=\mscget{line width}];}
      (\tikztotarget)
  }},
  found style/.default={2*\mscget{lost symbol radius}},
  found style/.style={%
    /msc, /tikz/line width=\mscget{line width},arrow style,
    to path={
      (\tikztotarget) circle (0.5*#1)
      \pgfextra{ \path[draw,solid,/tikz/line width=\mscget{line width}];}
      ($ (\tikztotarget)! 0.5*#1 !(\tikztostart) $)  -> (\tikztostart)
      \tikztonodes
  }},
  lost style/.default={2*\mscget{lost symbol radius}},
  lost style/.style={%
    /msc, /tikz/line width=\mscget{line width},arrow style,
    to path={
      (\tikztotarget) circle (0.5*#1)
      \pgfextra{ \path[draw,solid,fill,/tikz/line width=\mscget{line width}];}
      (\tikztostart) -> ($ (\tikztotarget)! 0.50*#1 !(\tikztostart) $)
      \tikztonodes
   }},
}
\mscset{normal values}
%special treatments of keys that can be specified as right or left but could not
%be in the old verison of msc
\setlength{\envinstdist}{\mscget{left environment distance}}
\setlength{\inlineoverlap}{\mscget{left inline overlap}}
\setlength{\referenceoverlap}{\mscget{left reference overlap}}
\mscset{
  left environment distance/.initial=\the\envinstdist,      % distance between environments and the % first level
  right environment distance/.initial=\the\envinstdist,      % distance between
  left inline overlap/.initial=\the\inlineoverlap,
  right inline overlap/.initial=\the\inlineoverlap,
  left reference overlap/.initial=\the\referenceoverlap,
  right reference overlap/.initial=\the\referenceoverlap
}
\def\msc@save@target#1{%
  \def\msc@target{#1}}
\def\msc@save@start#1{%
  \def\msc@start{#1}}
%defining styles to draw grid with help lines - usage e.g. \draw (0,-10) to[/msc/color grid] (10,0);
\mscset{
  /msc/grid/.style={
    to path={%
      \pgfextra{%
        \edef\msc@@target{(\tikztotarget)}%
        \tikz@scan@one@point\msc@save@target\msc@@target\relax
        \edef\msc@@start{(\tikztostart)}%
        \tikz@scan@one@point\msc@save@start\msc@@start\relax
        \draw[/msc/grid/minor lines] (\tikztostart) grid (\tikztotarget);
        \draw[/msc/grid/major lines,line cap=rect] (\tikztostart) grid (\tikztotarget);
        \msc@start
        \pgfmathsetmacro{\msc@xa}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\msc@ya}{\the\pgf@y/1cm}
        \msc@target
        \pgfmathsetmacro{\msc@xb}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\msc@yb}{\the\pgf@y/1cm}
        \pgfmathsetmacro{\msc@xc}{\msc@xa + \mscget{grid/major step}}
        \pgfmathsetmacro{\msc@yc}{\msc@ya + \mscget{grid/major step}}
        \foreach \x in {\msc@xa,\msc@xc,...,\msc@xb}
        \node[/msc/grid/x labelstyle] at (\x,\msc@ya) {\pgfmathprintnumber{\x}};
        \foreach \y in {\msc@ya,\msc@yc,...,\msc@yb}
        \node[/msc/grid/y labelstyle] at (\msc@xa,\y) {\pgfmathprintnumber{\y}};
      }
    }
  },
  /msc/color grid/.style={
    /msc/grid/labelstyle/.style={font=\tiny,text=blue},
    /msc/grid/minor line width=0.2pt,
    /msc/grid/major line width=0.4pt,
    /msc/grid/minor lines/.style={
      draw=green!10,line cap=rect,
      /tikz/line width=\mscget{grid/minor line width},
      step=\mscget{grid/minor step}
    },
    /msc/grid/major lines/.style={
      draw=red!10,line cap=rect,
      /tikz/line width=\mscget{grid/major line width},
      step=\mscget{grid/major step}
    },
    /msc/grid
  },
  /msc/grid/.cd,
  minor lines/.style={
    help lines,line cap=rect,dotted,
    step=\mscget{grid/minor step}
  },
  major lines/.style={
    help lines,line cap=rect,
    /tikz/line width=\mscget{grid/major line width},
    step=\mscget{grid/major step}
  },
  labelstyle/.style={font=\scriptsize,text=black},
  x labelstyle/.style={/msc/grid/labelstyle,below},
  y labelstyle/.style={/msc/grid/labelstyle,left},
  minor step/.initial=.2,
  major step/.initial=1,
  minor line width/.initial=0.3pt,
  major line width/.initial=0.5pt,
}
% And some internal counters
\newcount\c@mscinstcnt  %number of intstances
\newcount\c@condition  %number of conditions
\newcount\c@msclevelcnt %current level
% \newcount\c@maxlevelcnt %maximum level seen
%\advance\c@mscinstcnt by1
\global\let\msc@tempa\relax
\global\let\msc@tempb\relax
%\newdimen\msc@x
%\newdimen\msc@y

\newcommand{\mscgetx}[2]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
}

\newcommand{\mscgety}[2]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@y}%
}

% store the shifted level in #2 and level at which to draw message in #3
% also update \c@maxlevelcnt
% #1 - level difference
\def\msc@getlevel shift#1#2#3{
    \pgfkeys{/pgf/number format/precision=0}
    \pgfmathparse{\the\c@msclevelcnt + #1}
    \pgfmathparse{max(\pgfmathresult,0)}
    \pgfmathroundto{\pgfmathresult}
    \edef#2{\pgfmathresult} %calculate destination level
    \pgfmathparse{max((\the\c@msclevelcnt),#2)}
    \pgfmathroundto{\pgfmathresult}
    \xdef#3{\pgfmathresult} %calculate at which level to draw message
    \pgfmathparse{max(#3,\c@maxlevelcnt)}
    \pgfmathroundto{\pgfmathresult}
    \xdef\c@maxlevelcnt{\pgfmathresult} %update global max level counter
}

%
% * Shapes *
%
%
%% A rectangle with right side open
%
\pgfdeclareshape{right open rectangle}
{%
  \nodeparts{text}
  %%
  %% Anchors
  %%
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{
   % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathclose
   }
}
%
%% A rectangle with left side open
%
\pgfdeclareshape{left open rectangle}
{%
  \nodeparts{text}
  %%
  %% Anchors
  %%
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{
   % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathclose
   }
}
%
%% A rectangle with sloped sides
%
\pgfdeclareshape{condition shape}
{%
  \nodeparts{text}
  %%
  %% Anchors
  %%
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{
   % store lower right in xa/ya and upper right in xb/yb
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfmathparse{\pgf@xa-(\pgf@yb-\pgf@ya)*0.5}
    \pgf@xc=\pgfmathresult pt\relax
    \pgfmathparse{\pgf@yb*0.5+\pgf@ya*0.5}
    \pgf@yc=\pgfmathresult pt\relax
    \pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@yc}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfmathparse{\pgf@xb+(\pgf@yb-\pgf@ya)*0.5}
    \pgf@xc=\pgfmathresult pt\relax
    \pgfmathparse{\pgf@yb*0.5+\pgf@ya*0.5}
    \pgf@yc=\pgfmathresult pt\relax
    \pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@yc}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathclose
   }
}
%% A rectangle with sloped sides and strike out
%
\pgfdeclareshape{ncondition shape}
{%
  \nodeparts{text}
  %%
  %% Anchors
  %%
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \backgroundpath{
   % store lower right in xa/ya and upper right in xb/yb
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
    \pgfmathparse{\pgf@xa-(\pgf@yb-\pgf@ya)*0.5}
    \pgf@xc=\pgfmathresult pt\relax
    \pgfmathparse{\pgf@yb*0.5+\pgf@ya*0.5}
    \pgf@yc=\pgfmathresult pt\relax
    \pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@yc}}
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
    \pgfmathparse{\pgf@xb+(\pgf@yb-\pgf@ya)*0.5}
    \pgf@xc=\pgfmathresult pt\relax
    \pgfmathparse{\pgf@yb*0.5+\pgf@ya*0.5}
    \pgf@yc=\pgfmathresult pt\relax
    \pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@yc}}
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
    \pgfpathclose
   }
}


% \declinst[*] declares a new msc-instance and draws its head symbol
% the starred versions makes a fat instance.
% #1: msc/tikz options
% #2: nickname that can be used in \mess
% #3: name of the instance (above instance head symbol)
% #4: name of the instance (inside instance head symbol)
% Keys used:
% /msc/instance/nick - contains number for instance with nick
% /msc/instanceno/n - contains nick for instance with n, where n is a number
%\declinst(*){nickname}{instancenameabove}{instancenamewithin}
\def\declinst{\pgfutil@ifnextchar*{\msc@isstartrue\msc@declinst@nostar}{\msc@isstarfalse\msc@declinst@nostar*}}
\def\msc@declinst@nostar*{\pgfutil@ifnextchar[\msc@declinst@noopt{\msc@declinst@noopt[]}}
\def\msc@declinst@noopt[#1]#2#3#4{%
  \msc@nameinstance{#2}{#1}
  \msc@previousinstance{#2}
  \begin{pgfscope}
  \mscset{#1}
    \node[/msc,draw=\mscget{draw head},
          instance head={#3}, name={msc@node#2},
          right=\mscget{instance distance} of msc@node\msc@inst@name.south east,
%           inner sep=2pt,
          anchor=south west, every instance head,#1]{#4};
  \end{pgfscope}
  \advance\c@msclevelcnt by -1\relax
  \msc@createlevelnodes{#2}{0}
  \coordinate (msc@lastlevel#2) at (msc@node#2.south);
  \advance\c@msclevelcnt by 1\relax
  \msc@drawinstanceline{#2}{1}
}
% \dummyinst(*)[options]{createdinst}
\def\dummyinst{\pgfutil@ifnextchar*{\msc@isstartrue\msc@dummyinst@nostar}{\msc@isstarfalse\msc@dummyinst@nostar*}}
\def\msc@dummyinst@nostar*{\pgfutil@ifnextchar[\msc@dummyinst@noopt{\msc@dummyinst@noopt[]}}
\def\msc@dummyinst@noopt[#1]#2{%
  \msc@nameinstance{#2}{#1}
  \mscset{
    instance/#2/status/.initial=off
   }
  \begin{pgfscope}
    \mscset{#1}
    \msc@previousinstance{#2}
    \pgfmathparse{max(\mscget{instance width}, width(\mscget{dummy text})
                  +\pgfkeysvalueof{/pgf/inner xsep}*2+\mscget{line width}/2)}
    \edef\msc@x{\pgfmathresult pt}
    \node[/msc, name={msc@node#2},
          inner sep=2pt,
          right=\mscget{instance distance} of msc@node\msc@inst@name.south east,
          minimum width=\msc@x,
          minimum height=\mscget{head height},
          anchor=south west, #1]{};
  \end{pgfscope}
  \advance\c@msclevelcnt by -1\relax
  \msc@createlevelnodes{#2}{0}
  \coordinate (msc@lastlevel#2) at (msc@node#2.south);
  \advance\c@msclevelcnt by 1\relax
}

\def\inststart{\startinst}%old name of the command
%dd siple command \startinst[options]{instname}{text above}{text inside}
\def\startinst{\pgfutil@ifnextchar[\msc@startinst{\msc@startinst[]}}
\def\msc@startinst[#1]{
  \begin{pgfscope}
  \edef\msc@option{#1}\edef\msc@name{}\edef\msc@creator{}
  \pgfkeysactivatefamily{/msc/message options}
  \pgfkeysfiltered{/msc,#1}
  \msc@create
}
% \create[options]{name}[label position]{creator}[pos]{createdinst}{instancenameabove}{instancenamewithin}
\def\create{
  \begin{pgfscope}
  \def\msc@option{}
  \pgfutil@ifnextchar[\msc@create@opt{\msc@create@opt[]}
}
\def\msc@create@opt[#1]#2{
    \pgfkeysactivatefamily{/msc/message options}
    \pgfkeysfiltered{/msc,#1}
    \edef\msc@option{#1}
    \def\msc@name{#2}
    \pgfutil@ifnextchar[\msc@create@label{\msc@create@label[]}
}
\def\msc@create@label[#1]#2{
    \ifx#1t \mscset{label position=above}
    \else \ifx#1b \mscset{label position=below}
    \fi\fi
    \edef\msc@creator{#2}
    \pgfutil@ifnextchar[\msc@create{\msc@create@pos[\mscget{pos}]}
}
\def\msc@create@pos[#1]{
    \mscset{pos/.expanded=#1}
    \msc@create
}
%\create{insance name}{label above}{label inside}
\def\msc@create#1#2#3{
%     \mscset{pos/.expanded=#1}
    \pgfkeysifdefined{/msc/instance/#1/no}{
      \IfStrEq{off}{\mscget{instance/#1/status}}{
        \IfStrEq{fat}{\mscget{instance/#1/type}}{
          \edef\msc@tempa{(msc@node#1.east)++(-\mscget{line width}/2,0) coordinate (msc@node#1@r0)
               coordinate (msc@node#1@r\the\c@msclevelcnt)
              (msc@node#1.west)++(\mscget{line width}/2,0) coordinate (msc@node#1@l0)
               coordinate (msc@node#1@l\the\c@msclevelcnt)}
        }{%
          \edef\msc@tempa{(msc@node#1.east)++(-\mscget{line width}/2,0)
               coordinate (msc@node#1@r\the\c@msclevelcnt)
              (msc@node#1.west)++(\mscget{line width}/2,0)
               coordinate (msc@node#1@l\the\c@msclevelcnt)}
        }
        \edef\msc@tempb{
          [/msc, draw=\mscget{draw head}, instance head={#2}, name={msc@node#1}, every
          instance head,
          append after command={(msc@node#1.south) coordinate (msc@lastlevel#1)
              \msc@tempa},
          \msc@option] at (msc@node#1|-msc@level\the\c@msclevelcnt){\unexpanded\expandafter{#3}};
        }
        \adddraw{#1}{\the\c@msclevelcnt}{\expandafter\node\msc@tempb}
        \pgfkeysifdefined{/msc/instance/\msc@creator/no}{
          \edef\msc@tempb{[\msc@option]{\unexpanded\expandafter{\msc@name}}{\msc@creator}{#1}}
          \expandafter\mess\msc@tempb
        }{}
        \end{pgfscope}
        \mscset{instance/#1/status=on}
      }{
        \msc@instancestarted{#1}\end{pgfscope}
      }
%      \mscset{instance/#1/type=fat}
    }{\msc@instundefinederr{#1}\end{pgfscope}}
}

% creates nodes to which we draw a line when advancing a level
% #1 instance name
%associates name with number #1 in corresponding pgfkeys. It updates
%the right evironment
  % #1 name of instance
  % #2 options
\def\msc@nameinstance#1#2{
 \pgfkeysifdefined{/msc/instance/#1/no}{
   \msc@nicknamedefinederr{#1}
  }{
    \advance\c@mscinstcnt by2\relax
    \mscset{%update envright keys
      instance/envright/no/.initial/.expanded=\the\c@mscinstcnt,
      instanceno/\the\c@mscinstcnt/name/.initial/.expanded=envright
    }
    \advance\c@mscinstcnt by-1\relax%
    \mscset{
       instance/#1/no/.initial/.expanded=\the\c@mscinstcnt,
       instanceno/\the\c@mscinstcnt/name/.initial/.expanded=#1,%
       instance/#1/status/.initial=on,      %possible values: on, off (no draw),
       instance/#1/cover/.initial=0,        %how many times instance is covered
       instance/#1/region/.initial=normal,  %possible %values:normal,coregion,suspension,activation,seminormal
       instance/#1/region level/.initial=0,
       instance/#1/region width/.initial=0pt
    }
  }
  \begin{pgfscope}%parse options in scope not to propagate them
    \mscset{#2}%
    \ifnum\c@mscinstcnt<2\relax  %the first instance
      \coordinate[below=\mscget{first level height} of msc@level0] (msc@level1);
    \fi
    \ifmsc@isstar
      \xdef\msc@tempa{fat}
    \else
      \xdef\msc@tempa{\mscget{fat}}
    \fi
  \end{pgfscope}
  \mscset{/msc/instance/#1/type/.initial/.expand once=\msc@tempa}
  \iftikz@fullytransformed  %when fully transformed lines width is wrong - submitted bug nr 3597513.
    \pgfmathparse{\msc@tempa + (0.5-0.5/\pgf@pt@bb)*\mscget{line width}}
    \xdef\msc@tempa{\pgfmathresult}
  \fi
}
%assigns the name of the previous instance to the \msc@inst@name
  % #1 name or number of the instance
\def\msc@previousinstance#1{%
   \pgfkeysifdefined{/msc/instanceno/#1/name}%
     { \pgfmathsetcount{\c@pgf@counta}{#1} }
     { \pgfkeysifdefined{/msc/instance/#1/no}
       {\pgfmathsetcount{\c@pgf@counta}{\mscget{instance/#1/no}}}%
       {\msc@instundefinederr{#1}}
     }
  \advance\c@pgf@counta by -1\relax%
  \edef\msc@inst@name{\mscget{instanceno/\the\c@pgf@counta/name}}
}

\def\msc@numsplit#1.#2{\def\msc@ta{#1}\def\msc@tb{#2}}
% \nextlevel increases \msc@currentheight by #1 * \level height
% (optional) #1: a nonnegative integer number (defaults to 1)
% or tkiz options
%
\def\nextlevel{
  \pgfutil@ifnextchar[\msc@nextlevelopt{\msc@nextlevelopt[1]}
}
\def\msc@nextlevelopt[#1]{%
  \let\msc@tempheight\relax
  \if!\ifnum9<1#1!\else_\fi
    \mscset{/msc/level offset=#1}
  \else
    \msc@numsplit#1\relax
    \if!\ifnum9<1\msc@ta!\else_\fi
      \mscset{/msc/level offset=\msc@ta}%we get only 
      \if!\ifnum9<1\msc@tb!\else_\fi
       \pgfmathsetmacro{\msc@tempheight}{0.\msc@tb*\mscget{level height}}
       \msc@notpositiveintegerwarn{\msc@ta}{\msc@tb}
      \fi
    \fi
  \fi
  \mscgety{(msc@level\the\c@msclevelcnt)}{\msc@y}
  \foreach \msc@tempa [evaluate=\msc@tempc using \msc@y-(\msc@tempa*\mscget{level height})] in {1 ,...,\mscget{level offset}} {%
    \advance\c@msclevelcnt by 1\relax
    \coordinate (msc@level\the\c@msclevelcnt) at (0,\msc@tempc pt);
    \advance\c@msclevelcnt by -1\relax
    \foreach \msc@tempb in {1,...,\c@mscinstcnt} {
      \msc@createlevelnodes{\mscget{instanceno/\msc@tempb/name}}{\the\c@msclevelcnt}
    }
%        \message{-----Showing code for level=\the\c@msclevelcnt------------------- }
%          \pgfkeys{/msc/level\the\c@msclevelcnt/draw/.show code,}
    \pgfkeysifdefined{/msc/level\the\c@msclevelcnt/havedraw}{
      \begin{pgfscope}
         \mscset{/msc/level\the\c@msclevelcnt/draw}
         \msc@global@set{/msc/level\the\c@msclevelcnt/draw/.code={}}
      \end{pgfscope}
    }{
    }
    \global\advance\c@msclevelcnt by 1\relax
  }
  \foreach \msc@tempb in {1,...,\c@mscinstcnt} {
    \msc@drawinstanceline{\mscget{instanceno/\msc@tempb/name}}{\the\c@msclevelcnt}
  }
 \ifx\msc@tempheight\relax\else
   \edef\msc@ta{\mscget{level height}}
   \mscset{level height/.expanded=\msc@tempheight pt}
   \nextlevel[1]
   \mscset{level height/.expanded=\msc@ta}
 \fi
 \mscset{level offset=1}
}

% \coordinate (msc@level\the\c@msclevelcnt) at (0,#2);
% draw a line for a given instance
%  #1 name of the instance
%  #2 level number
\def\msc@drawinstanceline#1#2{
  \IfStrEq{\mscget{instance/#1/status}}{on}{
%     ifnum0<\mscget{instance/#1/cover} \else
      \mscgety{(msc@level#2)}{\msc@tempa}
      \mscgety{(msc@lastlevel#1)}{\msc@dil}
      \pgfmathparse{max(\msc@tempa-\msc@dil,\mscget{instance/#1/cover})}
      \ifnum0<\pgfmathresult\relax %draw only if last level is above current level
        \mscgety{(msc@level#2)}{\msc@io}\msc@setlastlevel{#1}{\msc@io}
      \else
        \msc@StrEqEither{\mscget{instance/#1/region}}{normal}{seminormal}{
            \draw[/msc,instance line,every instance line]
              (msc@lastlevel#1-|msc@node#1@l0)--(msc@level#2-|msc@node#1@l0)
              (msc@lastlevel#1-|msc@node#1@r0)--(msc@level#2-|msc@node#1@r0);
            \mscgety{(msc@level#2)}{\msc@io}\msc@setlastlevel{#1}{\msc@io}
         }{
%            \msc@regbody{#1}{#2}
         }
      \fi
  }{}
}

% create nodes at a current level for a given instance
%  #1 name of the instance
%  #2 level number
\def\msc@createlevelnodes#1#2{
%assume node msc@level#2 exists
    \IfEqCase{\mscget{instance/#1/type}}{
      {fat} {
       \coordinate[xshift=\mscget{line width} /2](msc@node#1@l#2) at (msc@node#1.west|-msc@level#2);
       \coordinate[xshift=-\mscget{line width} /2] (msc@node#1@r#2) at (msc@node#1.east|-msc@level#2);
       \IfStrEq{\mscget{instance/#1/region}}{seminormal}{
          \msc@global@set{instance/#1/region=normal}
        }{}
      }
      {normal} {%
        \msc@StrEqEither{\mscget{instance/#1/region}}{normal}{coregion}{%
            \coordinate (msc@node#1@r#2) at (msc@node#1 |-msc@level#2);
            \coordinate (msc@node#1@l#2) at (msc@node#1 |-msc@level#2);
        }
        {%
          \msc@StrEqEither{\mscget{instance/#1/region}}{activation}{suspension} {
            \path let \p1=(msc@node#1), \p2=(msc@level#2) in
              coordinate(msc@node#1@l#2) at (\x1-\mscget{instance/#1/region width}/2,\y2)
              coordinate(msc@node#1@r#2) at (\x1+\mscget{instance/#1/region width}/2,\y2);
          }
          {
            \IfStrEq{\mscget{instance/#1/region}}{seminormal}{
              \path let \p1=(msc@node#1), \p2=(msc@level#2) in
              coordinate(msc@node#1@l#2) at (\x1-\mscget{instance/#1/region width}/2,\y2)
              coordinate(msc@node#1@r#2) at (\x1+\mscget{instance/#1/region width}/2,\y2);
              \msc@global@set{instance/#1/region=normal}
            }{%debug message
              \message{[msc] Unknown region type: \mscget{instance/#1/region} for the nick: #1}
            }
          }
        }
      }
      [\message{[msc] Unknown instance type: \mscget{instance/#1/type} for the nick: #1}]
    }
%   }
%   { }
}
% \mess(*)[side]{name}[label position]{sender}[pos]{receiver}[level shift]
\def\mess{
  \begin{pgfscope}
    \def\msc@options{}
    \pgfutil@ifnextchar*{\msc@isstartrue\msc@mess@nostar}{\msc@isstarfalse\msc@mess@nostar*}
}
\def\msc@mess@nostar*{\pgfutil@ifnextchar[\msc@messopt{\msc@messopt[]}}
\def\msc@messopt[#1]{%parsing of [side] and [options]
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
  \else \ifx#1b \else \ifx#1t %ignoring t and b for backward compatibility
    \else
      \pgfkeysactivatefamily{/msc/message options}
      \pgfkeysfiltered{/msc,#1}
      \def\msc@options{#1}
%       \pgfutil@ifundefined{msc@mess@side}{\def\msc@mess@side{\mscget{side}}}{}
     \fi \fi \fi
  \fi
  \pgfutil@ifnextchar[{\msc@messopt}{\msc@mess@threeopt}
}
\def\msc@mess@threeopt#1{%parsing of {name}
  \def\msc@mess@name{#1}
  \pgfutil@ifnextchar[\msc@mess@twoopt{\msc@mess@twoopt[\mscget{label position}]}
}
\def\msc@mess@twoopt[#1]#2{%parsing of [label position] and {sender}
  \ifx#1l \mscset{label position=left}
  \else \ifx#1r \mscset{label position=right}
  \else \ifx#1t \mscset{label position=above}
  \else \ifx#1b \mscset{label position=below}
  \fi \fi \fi \fi
  \xdef\msc@sender{#2}
  \pgfutil@ifnextchar[\msc@mess@oneopt{\msc@mess@oneopt[\mscget{pos}]}
}
\def\msc@mess@oneopt[#1]#2{%parsing of [placement] and {receiver}
  \mscset{pos/.expanded=#1}
  \xdef\msc@receiver{#2}
  \IfStrEq{\msc@receiver}{\msc@sender}{
    \edef\msc@tempb{\mscget{offset}}
  }{
    \edef\msc@tempb{\mscget{level shift}}
  }
  \pgfutil@ifnextchar[{\msc@mess@noopt}{\msc@mess@noopt[\msc@tempb]}
}

\def\msc@mess@noopt[#1]{%parsing of [level shift]
  \def\msc@undefined{false}
  \msc@getlevel shift{#1}{\msc@level shift}{\msc@tempa}
  \ifx\msc@sender\msc@receiver  %self message
    \IfStrEq{\mscget{label position}}{above}{
      \mscset{label position=\mscget{side}}
    }{}
    \pgfkeysifdefined{/msc/instance/\msc@receiver/no}{%instance name
      \IfStrEq{\mscget{side}}{left}{
        \msc@selfmess{msc@node\msc@receiver @l\the\c@msclevelcnt}{msc@node\msc@receiver @l\msc@level shift}{-\mscget{self message width}}
      }{
        \msc@selfmess{msc@node\msc@receiver @r\the\c@msclevelcnt}{msc@node\msc@receiver @r\msc@level shift}{\mscget{self message width}}
      }
    }{
      \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@receiver}{% neither instance name nor reference node
        \def\msc@undefined{true}
        \msc@instundefinederr{\msc@receiver}
      }{%  reference node
        \IfStrEq{\mscget{side}}{left}{
          \msc@selfmess{msc@node\msc@receiver|-msc@level\the\c@msclevelcnt}%
          {msc@node\msc@receiver|-msc@level\msc@level shift}{-\mscget{self message width}}
        }{
          \msc@selfmess{msc@node\msc@receiver|-msc@level\the\c@msclevelcnt}%
          {msc@node\msc@receiver|-msc@level\msc@level shift} {\mscget{self message width}}
        }
      }
    }
  \else %not self message
    \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@receiver} {
      \def\msc@undefined{true}
      \msc@instundefinederr{\msc@receiver}
    }{
      \mscgetx{(msc@node\msc@receiver)}{\msc@minx}
    }
    \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@sender} {
      \def\msc@undefined{true}
      \msc@instundefinederr{\msc@sender}
    }{
      \mscgetx{(msc@node\msc@sender)}{\msc@maxx}
    }
    \IfStrEq{\msc@undefined}{false}{
      \pgfkeysifdefined{/msc/instance/\msc@sender/no}{%instance name
%        \pgfmathparse{\msc@maxx-\msc@minx}
        \pgfmathparse{ifthenelse((\msc@maxx-\msc@minx)<0,"r","l")}
        \edef\msc@left{msc@node\msc@sender @\pgfmathresult\the\c@msclevelcnt}
      }{
        \def\msc@left{msc@node\msc@sender|-msc@level\the\c@msclevelcnt}
      }
      \pgfkeysifdefined{/msc/instance/\msc@receiver/no}{%instance name
        \pgfmathparse{ifthenelse((\msc@maxx-\msc@minx)<0,"l","r")}
        \edef\msc@right{msc@node\msc@receiver @\pgfmathresult\msc@level shift}
      }{
       \def\msc@right{msc@node\msc@receiver|-msc@level\msc@level shift}
      }
      \msc@mess{\msc@left}{\msc@right}%stores result in \msc@tempb
    }{}
  \fi
  \IfStrEq{\msc@undefined}{false}{
    \end{pgfscope}
    \addDdraw{\msc@sender}{\msc@receiver}{\msc@tempa}{\expandafter\draw\msc@tempb}
  }{
    \end{pgfscope}
  }
}
% #1 - sender node
% #2 - receiver node
% #3 - message dist
% result defined in \msc@tempb
\def\msc@selfmess#1#2#3{
  \IfStrEq{\mscget{label position}}{left}{\xdef\msc@tempc{-1}}{\xdef\msc@tempc{1}}
  \xdef\msc@tempb{[\mscget{label position},/msc,message,message loop={#3},
            \ifmsc@isstar replay,\fi, inner sep=0pt, 
           /tikz/pos=\mscget{pos}, every message, \msc@options]
           (#1)
              to node[xshift=\msc@tempc*\mscget{label distance}](msc@lastnode){\expandafter\noexpand\msc@mess@name}
           (#2);
          }%use macro to expand parameters
}
% #1 - sender node
% #2 - receiver node
% result defined in \msc@tempb
\def\msc@mess#1#2{
  \IfSubStr{\mscget{label position}}{above}{\xdef\msc@tempc{1}}{\xdef\msc@tempc{-1}}
  \xdef\msc@tempb{[/msc,\mscget{label position},message,
          \ifmsc@isstar replay,\fi, inner sep=0pt, %
         /tikz/pos=\mscget{pos}, every message, \msc@options]
         (#1) ->
         node[yshift=\msc@tempc*\mscget{label distance}](msc@lastnode){\unexpanded\expandafter{\msc@mess@name}}(#2);%cheating %and using eTeX expansion
         % to use pure TeX see http://tex.stackexchange.com/questions/40674/replacement-for-unexpanded-without-etex-extension
  }
}
%add code to be executed at given level
% #1 - name of the instance
% #2 - number of level at which to add the code
% #3 - code to be added (expanded once - so prefix with \expandafter)
\def\adddraw#1#2#3{
  \msc@StrEqEither{#1}{envright}{envleft} {
    \msc@global@set{/msc/levelenv/draw/.append code/.expand once={#3},
           /msc/levelenv/havedraw/.initial=true}
  }{
    \msc@global@set{/msc/level#2/draw/.append code/.expand once={#3},
          /msc/level#2/havedraw/.initial=true}
  }
}
%add code to be executed at given level
% #1 - name of the instance
% #2 - name of the second instance
% #3 - number of level at which to add the code
% #4 - code to be added (expanded once - so prefix with \expandafter)
\def\addDdraw#1#2#3#4{
  \msc@StrEqEither{#1}{envright}{envleft} {
    \msc@global@set{/msc/levelenv/draw/.append code/.expand once={#4},
            /msc/levelenv/havedraw/.initial=true}
  }{
    \msc@StrEqEither{#2}{envright}{envleft} {
      \msc@global@set{/msc/levelenv/draw/.append code/.expand once={#4},
            /msc/levelenv/havedraw/.initial=true}
    }{
      \msc@global@set{/msc/level#3/draw/.append code/.expand once={#4},
          /msc/level#3/havedraw/.initial=true}
    }
  }
}
% \msccomment[position]{text}{instname}
\def\msccomment{
 \begin{pgfscope}
  \def\msc@options{}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@msccomment@nostar}{\msc@isstarfalse\msc@msccomment@nostar*}
}
\def\msc@msccomment@nostar*{\pgfutil@ifnextchar[\msc@msccomment@opt{\msc@msccomment@opt[]}}
\def\msc@msccomment@opt[#1]{%parsing of [side] and [options]
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \pgfkeysactivatefamily{/msc/msccomment options}
      \pgfkeysfiltered{/msc,#1}
      \def\msc@options{#1}
%       \pgfutil@ifundefined{msc@mess@side}{\def\msc@mess@side{\mscget{side}}}{}
     \fi
  \fi
  \pgfutil@ifnextchar[{\msc@msccomment@opt}{\msc@msccomment@noopt}
}

\def\msc@msccomment@noopt#1#2{%parsing of {text}
  \edef\msc@undefined{false}
  \edef\msc@instname{#2}
  \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@node{msc@node\msc@instname @l\the\c@msclevelcnt}
     } {
      \edef\msc@node{msc@node\msc@instname @r\the\c@msclevelcnt}
    }
  }{
    \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@instname}{% neither instance name nor reference node
      \edef\msc@undefined{true}
      \msc@instundefinederr{\msc@instname}
    }
    {
      \edef\msc@node{msc@node\msc@instname|-msc@level\the\c@msclevelcnt}
    }
  }
  \IfStrEq{\msc@undefined}{false}{
    \IfStrEq{\mscget{side}}{left}{
      \def\msc@anchor{east}
      \edef\msc@dist{-\mscget{msccomment distance}}
     } {
      \def\msc@anchor{west}
      \edef\msc@dist{\mscget{msccomment distance}}
    }
    \xdef\msc@tempb{[/msc,msccomment,msccomment line,
            every msccomment, \msc@options]
            let \noexpand\p1 = (\msc@node)
            in (\noexpand\x1+\msc@dist,\noexpand\y1)
            node [/msc,draw,solid,\mscget{side} open rectangle,anchor=\msc@anchor,
            append after command={(\msc@node)
              -- (\noexpand\tikzlastnode)},
            every msccomment, \msc@options] {#1};
          }%use macro to expand parameters
    \end{pgfscope}
    \adddraw{#2}{\the\c@msclevelcnt}{\expandafter\draw\msc@tempb}
  }{
    \msc@instundefinederr{\msc@instname}\end{pgfscope}
  }
}
% \action(*){name}{instance}
% \naction(*){name}{instance}
\def\naction{
 \begin{pgfscope}%,append after command={node[draw,cross out]{}}
  \def\msc@crossout{,append after command={
      [shorten >=1.2*\noexpand\mscget{line width},
       shorten <=1.2*\noexpand\mscget{line width}]
      (\noexpand\tikzlastnode.north west) edge (\noexpand\tikzlastnode.south east)
      (\noexpand\tikzlastnode.north east) edge (\noexpand\tikzlastnode.south west)}}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@action@nostar}{\msc@isstarfalse\msc@action@nostar*}
}
\def\action{
 \begin{pgfscope}
  \def\msc@crossout{}
  \def\msc@options{}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@action@nostar}{\msc@isstarfalse\msc@action@nostar*}
}
% #1 options
% #2 name
% #3 insstance
\def\msc@action@nostar*{\pgfutil@ifnextchar[\msc@action@opt{\msc@action@opt[]}}
\def\msc@action@opt[#1]#2#3{%parsing of [side] and [options]
  \pgfkeysactivatefamily{/msc/action options}
  \pgfkeysfiltered{/msc,#1}
  \def\msc@options{#1}
  \xdef\msc@instname{#3}
  \ifmsc@isstar
    \def\msc@tempa{}
  \else
    %\msc@savebox{#2}
    \edef\msc@tempa{,
       %text height={min({\the\ht\msc@box},\mscget{action height})},
       %text depth={\the\dp\msc@box},
                 %inner sep=0,
                 align=center,
                 text width={max( \mscget{action width},\noexpand\noexpand\noexpand\x2 -
                 \noexpand\noexpand\noexpand\x1)-2*\mscget{label distance}}
                 }
  \fi
  \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \xdef\msc@tempb{[, ]
            let \noexpand\p1 = (msc@node\msc@instname @l\the\c@msclevelcnt),
            \noexpand\p2 = (msc@node\msc@instname @r\the\c@msclevelcnt) in
            ( {\noexpand\x1+(\noexpand\x2-\noexpand\x1)/2},\noexpand\y1)
             node[/msc, line cap=round,
               yshift=\mscget{line width}/2, anchor=north,
               minimum height=\mscget{action height},
               minimum width= {max( \mscget{action width},\noexpand\x2 -
               \noexpand\x1)} \msc@tempa, draw \msc@crossout,
               inner sep=\mscget{label distance},
               every action, /tikz/line width=\mscget{line width},\msc@options 
            ](msc@tempnode){\unexpanded\expandafter{#2}};
            \noexpand\msc@StrEqEither{\msc@instname}{envright}{envleft}{}{
              \noexpand\mscgety{(msc@tempnode.south)}{\noexpand\msc@y}
              \noexpand\msc@setlastlevel{\msc@instname}{\noexpand\msc@y}
            }
          }
    \end{pgfscope}
    \msc@StrEqEither{\mscget{instance/#3/region}}{normal}{seminormal}{}{
      \msc@regbody{#3}{\the\c@msclevelcnt}
      \msc@regbar{#3}{\mscget{instance/#3/region level}}
    }
    \adddraw{\msc@instname}{\the\c@msclevelcnt}{\expandafter\draw\msc@tempb}
  }{
    \msc@instundefinederr{\msc@instname}\end{pgfscope}
  }
}
% \settimer[placement]{name}{instance}
\def\settimer{
 \begin{pgfscope}
  \def\msc@options{} \def\msc@timertype{settimer style}
  \pgfutil@ifnextchar[\msc@timer@opt{\msc@timer@opt[]}
}
% \timeout[placement]{name}{instance}
\def\timeout{
 \begin{pgfscope}
  \def\msc@options{} \def\msc@timertype{timeout style}
  \pgfutil@ifnextchar[\msc@timer@opt{\msc@timer@opt[]}
}
% \stoptimer[placement]{name}{instance}
\def\stoptimer{
 \begin{pgfscope}
  \def\msc@options{} \def\msc@timertype{stoptimer style}
  \pgfutil@ifnextchar[\msc@timer@opt{\msc@timer@opt[]}
}
% #1 - options or "l" or "r"
\def\msc@timer@opt[#1]{%parsing of [side] and [options]
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \pgfkeysactivatefamily{/msc/msccomment options}
      \pgfkeysfiltered{/msc,#1}
      \def\msc@options{#1}
     \fi
  \fi
  \pgfutil@ifnextchar[{\msc@timer@opt}{\msc@timer}
}
% #1 - label
% #2 - instance name
\def\msc@timer#1#2{
  \edef\msc@undefined{false}
  \edef\msc@instname{#2}
  \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@node{msc@node\msc@instname @l\the\c@msclevelcnt}
     } {
      \edef\msc@node{msc@node\msc@instname @r\the\c@msclevelcnt}
    }
  }{
    \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@instname}{% neither instance name nor reference node
      \edef\msc@undefined{true}
      \msc@instundefinederr{\msc@instname}
    }
    {
      \edef\msc@node{msc@node\msc@instname|-msc@level\the\c@msclevelcnt}
    }
  }
  \IfStrEq{\msc@undefined}{false}{
    \IfStrEq{\mscget{side}}{left}{
      \def\msc@anchor{east}
      \edef\msc@timer@dist{-\mscget{self message width}}
      \edef\msc@label@dist{-\mscget{label distance}}
     } {
      \edef\msc@timer@dist{\mscget{self message width}}
      \def\msc@anchor{west}
      \edef\msc@label@dist{\mscget{label distance}}
    }
    \xdef\msc@tempb{[/msc,\msc@timertype={\mscget{timer width}},
              every timer, \msc@options]
              let \noexpand\p1 = (\msc@node)
              in (\noexpand\x1+\msc@timer@dist,\noexpand\y1)
              node [xshift=\msc@label@dist,/msc,anchor=\msc@anchor]{#1} to (\noexpand\x1,\noexpand\y1);
          }%use macro to expand parameters
    \end{pgfscope}
    \adddraw{#2}{\the\c@msclevelcnt}{\expandafter\draw\msc@tempb}
  }{
    \msc@instundefinederr{#2}\end{pgfscope}
  }
}
%\setstoptimer[placement]{name}{instance}[offset]
\def\setstoptimer{
 \begin{pgfscope}
  \def\msc@options{} \def\msc@timertype{setstoptimer style}
  \pgfutil@ifnextchar[\msc@fulltimer@opt{\msc@fulltimer@opt[]}
}
%\settimeout[placement]{name}{instance}[offset]
\def\settimeout{
 \begin{pgfscope}
  \def\msc@options{} \def\msc@timertype{settimeout style}
  \pgfutil@ifnextchar[\msc@fulltimer@opt{\msc@fulltimer@opt[]}
}

% #1 - options or "l" or "r"
\def\msc@fulltimer@opt[#1]{%parsing of [side] and [options]
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \pgfkeysactivatefamily{/msc/msccomment options}
      \pgfkeysfiltered{/msc,#1}
      \def\msc@options{#1}
     \fi
  \fi
  \pgfutil@ifnextchar[{\msc@fulltimer@opt}{\msc@fulltimer}
}
\def\msc@fulltimer#1#2{
  \xdef\msc@instname{#2}
  \def\msc@name{#1}
  \pgfutil@ifnextchar[{\msc@fulltimer@noopt}{\msc@fulltimer@noopt[\mscget{offset}]}
}
\def\msc@fulltimer@noopt[#1]{
  \edef\msc@undefined{false}
  \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@node{msc@node\msc@instname @l}
     } {
      \edef\msc@node{msc@node\msc@instname @r}
    }
  }{
    \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@instname}{% neither instance name nor reference node
      \edef\msc@undefined{true}
      \msc@instundefinederr{\msc@instname}
    }
    {
      \edef\msc@node{msc@node\msc@instname|-msc@level}
    }
  }
  \IfStrEq{\msc@undefined}{false}{
%   \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \msc@getlevel shift{#1}{\msc@level shift}{\msc@drawlevel}
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@mess@dist{-\mscget{self message width}}
      \edef\msc@label@dist{-\mscget{label distance}}
      \def\msc@anchor{east}
    }{
      \edef\msc@mess@dist{\mscget{self message width}}
      \edef\msc@label@dist{\mscget{label distance}}
      \def\msc@anchor{west}
    }
    \xdef\msc@tempb{[/msc,\msc@timertype={\msc@mess@dist}{\mscget{timer width}},
                    every timer,\msc@options]
         (\msc@node\the\c@msclevelcnt)
      node [xshift=\msc@label@dist+\msc@mess@dist,/msc,anchor=\msc@anchor]{\msc@name}
      to (\msc@node\msc@level shift);
    }
   \end{pgfscope}
   \adddraw{\expandafter\noexpand\msc@instname}{\expandafter\noexpand\msc@drawlevel}{\expandafter\draw\msc@tempb}
%    \edef\msc@drawlevel{0}
  }{
   \end{pgfscope}\msc@instundefinederr{\msc@instname}
  }
}
% \mscmark[position]{name}{instname}
\def\mscmark{
 \begin{pgfscope}
  \def\msc@options{}
  \pgfutil@ifnextchar[\msc@mark@opt{\msc@mark@opt[]}
}
\def\msc@mark@opt[#1]{%parsing of [side] and [options]
   \IfEqCase{#1}{%
     {l}{\mscset{side=left}}%
     {r}{\mscset{side=right}}%
     {t}{\mscset{position=above}}%
     {b}{\mscset{position=below}}%
     {tl}{\mscset{side=left,position=above}}%
     {tr}{\mscset{side=right,position=above}}%
     {bl}{\mscset{side=left,position=below}}%
     {br}{\mscset{side=right,position=below}}%
   }[%
    \mscset{#1}
    \def\msc@options{#1}
  ]
  \pgfutil@ifnextchar[{\msc@mark@opt}{\msc@mark}
}

\def\msc@mark#1#2{
  \def\msc@undefined{false}
  \pgfkeysifdefined{/msc/instance/#2/no}{
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@node{msc@node#2@l\the\c@msclevelcnt}
     } {
      \edef\msc@node{msc@node#2@r\the\c@msclevelcnt}
    }
  }{
    \pgfutil@ifundefined{pgf@sh@ns@msc@node#2}{% neither instance name nor reference node
      \edef\msc@undefined{true}
      \msc@instundefinederr{#2}
    }
    {
      \edef\msc@node{msc@node#2|-msc@level\the\c@msclevelcnt}
    }
  }
  \IfStrEq{\msc@undefined}{false}{
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@anchor@l{south east}
      \edef\msc@anchor@r{south west}
      \edef\msc@dist{-\mscget{mark distance}}
     } {
      \edef\msc@dist{\mscget{mark distance}}
      \edef\msc@anchor@l{south west}
      \edef\msc@anchor@r{south east}
    }
    \IfStrEq{\mscget{position}}{above}{
     \edef\msc@mark@ydist{0.5*\mscget{mark distance}}
    }{
     \IfStrEq{\mscget{position}}{below}{
       \edef\msc@mark@ydist{-0.5*\mscget{mark distance}}
     }
     {
       \IfStrEq{\mscget{position}}{mid}{
         \edef\msc@mark@ydist{0pt}
       }{%unknown
       }
     }
    }
    \xdef\msc@tempb{[/msc,mscdash,/tikz/line width=\mscget{line width}, every mscmark, \msc@options]
            (\msc@node)
            ++(\msc@dist,\msc@mark@ydist)
            node[anchor=\msc@anchor@l,inner sep=\mscget{label distance}, append after command={
              (\msc@node)--
              (\noexpand\tikzlastnode .\msc@anchor@l)--(\noexpand\tikzlastnode .\msc@anchor@r)
            }] {#1};
          }
    \end{pgfscope}
    \adddraw{#2}{\the\c@msclevelcnt}{\expandafter\draw\msc@tempb}
  }{}
}
% \measure(*)[placement]{name}{instance1}{instance2}[offset]
\def\measure{
 \begin{pgfscope}
  \def\msc@options{}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@measure@nostar}{\msc@isstarfalse\msc@measure@nostar*}
}
\def\msc@measure@nostar*{\pgfutil@ifnextchar[\msc@measure@opt{\msc@measure@opt[]}}
\def\msc@measure@opt[#1]{%parsing of [side] and [options]
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \mscset{#1}
      \def\msc@options{#1}
     \fi
  \fi
  \pgfutil@ifnextchar[{\msc@measure@opt}{\msc@measure@noopt}
}

\def\msc@measure@noopt#1#2#3{
  \def\msc@measure@name{#1}
  \xdef\msc@instnameI{#2}
  \xdef\msc@instnameII{#3}
   \pgfutil@ifnextchar[{\msc@measure@final}{\msc@measure@final[\mscget{offset}]}
}

\def\msc@measure@final[#1]{
  \pgfkeysifdefined{/msc/instance/\msc@instnameI/no}{
    \pgfkeysifdefined{/msc/instance/\msc@instnameII/no}{
      \msc@getlevel shift{#1}{\msc@level shift}{\msc@drawlevel}
      \ifmsc@isstar\def\msc@style{measure* style}\else\def\msc@style{measure style}\fi
      \IfStrEq{\mscget{side}}{left}{
        \edef\msc@nodeI{msc@node\msc@instnameI @l\the\c@msclevelcnt}
        \edef\msc@nodeII{msc@node\msc@instnameII @l\msc@level shift}
        \edef\msc@dist{-\mscget{measure distance}-\noexpand\noexpand\noexpand\msc@x+
          min(\noexpand\noexpand\noexpand\msc@x,\noexpand\noexpand\noexpand\msc@y)}
        \edef\msc@anchor{east}
       } {
        \edef\msc@nodeI{msc@node\msc@instnameI @r\the\c@msclevelcnt}
        \edef\msc@nodeII{msc@node\msc@instnameII @r\msc@level shift}
        \edef\msc@dist{\mscget{measure distance}-\noexpand\noexpand\noexpand\msc@x
          +max(\noexpand\noexpand\noexpand\msc@x,\noexpand\noexpand\noexpand\msc@y)}
        \edef\msc@anchor{west}
      }
      \xdef\msc@tempa{
        \noexpand\mscgetx{(\msc@nodeI)}{\noexpand\msc@x}
        \noexpand\mscgetx{(\msc@nodeII)}{\noexpand\msc@y}
        \noexpand\pgfmathparse{\msc@dist}
        \noexpand\edef\noexpand\msc@dist{\noexpand\pgfmathresult}
      }
      \xdef\msc@tempb{[/msc,draw, mscdash,
        \msc@style={\noexpand\msc@dist pt}{\mscget{measure symbol width}}, every measure,
        \msc@options] (\msc@nodeI) to
          node[/msc,anchor=\msc@anchor, inner sep=\mscget{label distance}, every measure, \msc@options]{\unexpanded\expandafter{\msc@measure@name}}
          (\msc@nodeII);
%         \noexpand\path ($(a) !\mscget{pos}! (b)$)
%            node[/msc,anchor=\msc@anchor, inner sep=\mscget{label distance}, every measure, \msc@options]{text};
            }
      \end{pgfscope}
      \addDdraw{\msc@instnameI}{\msc@instnameII}{\expandafter\noexpand\msc@drawlevel}{\expandafter\noexpand\msc@tempa}
      \addDdraw{\msc@instnameI}{\msc@instnameII}{\expandafter\noexpand\msc@drawlevel}{\expandafter\draw\msc@tempb}
    }{
     \msc@instundefinederr{\msc@instnameII}\end{pgfscope}
   }
  }{
    \msc@instundefinederr{\msc@instnameI}\end{pgfscope}
  }
}
%\measurestart(*)[placement]{name}{instance}{gate}
\def\measurestart{
 \begin{pgfscope}
  \def\msc@options{}
  \def\msc@sign{}
  \pgfutil@ifnextchar*{\edef\msc@style{measurestart* style}\msc@measuregap@nostar}{\edef\msc@style{measurestart style}\msc@measuregap@nostar*}
}
% \measureend(*)[placement]{name}{instance}{gate}
\def\measureend{
 \begin{pgfscope}
  \def\msc@options{}
  \def\msc@sign{-1*}
  \pgfutil@ifnextchar*{\edef\msc@style{measurestart* style}\msc@measuregap@nostar}{\edef\msc@style{measurestart style}\msc@measuregap@nostar*}
}
\def\msc@measuregap@nostar*{\pgfutil@ifnextchar[\msc@measuregap@opt{\msc@measuregap@opt[]}}
\def\msc@measuregap@opt[#1]{%parsing of [side] and [options]
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \mscset{#1}
      \def\msc@options{#1}
     \fi
  \fi
  \pgfutil@ifnextchar[{\msc@measuregap@opt}{\msc@measuregap@noopt}
}

\def\msc@measuregap@noopt#1#2#3{
  \edef\msc@text{#1}
  \gdef\msc@instname{#2}
  \edef\msc@gate{#3}
   \pgfutil@ifnextchar[{\msc@measuregap@final}{\msc@measuregap@final[\mscget{offset}]}
}

\def\msc@measuregap@final[#1]{
  \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \msc@getlevel shift{\msc@sign#1}{\msc@level shift}{\msc@drawlevel}
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@nodeI{msc@node\msc@instname @l\the\c@msclevelcnt}
      \edef\msc@nodeII{msc@node\msc@instname @l\msc@level shift}
      \edef\msc@dist{-\mscget{measure distance}}
      \edef\msc@anchor{east}
     } {
      \edef\msc@nodeI{msc@node\msc@instname @r\the\c@msclevelcnt}
      \edef\msc@nodeII{msc@node\msc@instname @r\msc@level shift}
      \edef\msc@dist{\mscget{measure distance}}
      \edef\msc@anchor{west}
    }
    \xdef\msc@tempb{[/msc,mscdash,draw,
      \msc@style={\msc@dist}{\mscget{measure symbol width}}, every measure,
      \msc@options] (\msc@nodeI) to node[/msc,anchor=\msc@anchor, inner
      sep=\mscget{label distance}, every measure, \msc@options]{\msc@text} (\msc@nodeII);
      \noexpand\path (b) node[/msc,anchor=\msc@anchor, inner sep=\mscget{label distance}, every measure, \msc@options]{\msc@gate};
          }
    \end{pgfscope}
    \adddraw{\msc@instname}{\expandafter\noexpand\msc@drawlevel}{\expandafter\draw\msc@tempb}
  }{
  \msc@instundefinederr{\msc@instname}\end{pgfscope}
 }
}

% \lost[side]{name}[label position]{gate}{instance}[pos]
\def\lost{
 \begin{pgfscope}
  \def\msc@options{}
  \def\msc@style{lost style}
  \pgfutil@ifnextchar[{\msc@lost@opt}{\msc@lost@opt[]}
}
% \found[pos]{name}[label position]{gate}{instance}[placement]
\def\found{
 \begin{pgfscope}
  \def\msc@options{}
  \def\msc@style{found style}
  \pgfutil@ifnextchar[{\msc@lost@opt}{\msc@lost@opt[]}
}
\def\msc@lost@opt[#1]#2{
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \mscset{#1}
      \def\msc@options{#1}
     \fi
  \fi
  \def\msc@text{#2}
  \pgfutil@ifnextchar[{\msc@lost@noopt}{\msc@lost@noopt[]}
}
\def\msc@lost@noopt[#1]#2#3{
  \mscset{label position=above}
  \expandafter\ifx#1b \mscset{label position=below} \fi
  \def\msc@gate{#2}
  \xdef\msc@instname{#3}
  \pgfutil@ifnextchar[{\msc@lost@final}{\msc@lost@final[\mscget{pos}]}
}
\def\msc@lost@final[#1]{
  \def\msc@undefined{false}
  \pgfkeysifdefined{/msc/instance/\msc@instname/no}{
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@node{msc@node\msc@instname @l\the\c@msclevelcnt}
     } {
      \edef\msc@node{msc@node\msc@instname @r\the\c@msclevelcnt}
    }
  }{
    \pgfutil@ifundefined{pgf@sh@ns@msc@node\msc@instname}{% neither instance name nor reference node
      \edef\msc@undefined{true}
      \msc@instundefinederr{\msc@instname}
    }
    {
      \edef\msc@node{msc@node\msc@instname|-msc@level\the\c@msclevelcnt}
    }
  }
  \IfStrEq{\msc@undefined}{false}{
    \mscset{pos=#1}
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@dist{-\mscget{self message width}}
      \edef\msc@anchor{east}
     } {
      \edef\msc@dist{\mscget{self message width}}
      \edef\msc@anchor{west}
    }
    \xdef\msc@tempb{[/msc,draw,
      \msc@style={2*\mscget{lost symbol radius}}, every lostfound,
      \msc@options] (\msc@node) to node[/msc,\mscget{label position}, inner
      sep=\mscget{label distance}, every lostfound, \msc@options]{\unexpanded\expandafter{\msc@text}}
      ++(\msc@dist,0);
      \noexpand\path(\msc@node) ++(\msc@dist,0)
      node[/msc,above,anchor=\msc@anchor,%
      inner sep=\mscget{label distance}, every lostfound, \msc@options]{\msc@gate};
      }
    \end{pgfscope}
    \adddraw{\msc@instname}{\the\c@msclevelcnt}{\expandafter\draw\msc@tempb}
  }{}
}

% \condition puts a condition symbol over the given instances. The
% starred version adjusts the width and the height of the condition symbol.
% #1: name to be put inside the condition symbol
% #2: comma-separated list of instance nicknames, such that:
%     - The first instance nickname is supposed to be the leftmost
%       instance of the condition
%     - The last instance nickname is supposed to be the rightmost
%       instance of the condition
% \condition(*)[options]{text}{instancelist}
\def\condition{
  \advance\c@condition by1\relax
 \begin{pgfscope}
   \edef\msc@shape{condition shape}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@cond@star}{\msc@isstarfalse\msc@cond@star*}
}
\def\ncondition{
 \begin{pgfscope}
   \edef\msc@shape{ncondition shape}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@cond@star}{\msc@isstarfalse\msc@cond@star*}
}
% \ncondition(*)[options]{text}{instancelist}
\def\msc@cond@star*{
  \pgfutil@ifnextchar[{\msc@condition}{\msc@condition[]}
}
\newif\ifmsc@isfirst
\def\msc@condition[#1]#2#3{
 \mscset{inner sep=\mscget{label distance},#1}
  \msc@isfirsttrue
  \@for\msc@arg:=#3\do{%
    \pgfkeysifdefined{/msc/instance/\msc@arg/no}{
      \ifmsc@isfirst%first instance in the list
        \mscgetx{(msc@node\msc@arg @l0)}{\msc@minx}
        \mscgetx{(msc@node\msc@arg @r0)}{\msc@maxx}
        \mscgety{(msc@level\the\c@msclevelcnt)}{\msc@y}
      \else
        \mscgetx{(msc@node\msc@arg @r0)}{\msc@x}
        \pgfmathparse{max(\msc@x,\msc@maxx)}
        \edef\msc@maxx{\pgfmathresult pt}
        \mscgetx{(msc@node\msc@arg @l0)}{\msc@x}
        \pgfmathparse{min(\msc@x,\msc@minx)}
        \edef\msc@minx{\pgfmathresult pt}
      \fi
      \msc@isfirstfalse
      \xdef\msc@instname{\msc@arg}
    }{
      \msc@instundefinederr{\msc@arg}
    }
  }
 \ifmsc@isfirst \else%there are proper instances
   \pgfmathparse{2*\mscget{condition overlap}+\msc@maxx-\msc@minx}
   \edef\msc@width{\pgfmathresult pt}
   \ifmsc@isstar
     \def\msc@tempr{}
   \else
     %\msc@savebox{\parbox{\msc@width}{#2}}
     \edef\msc@tempr{%text depth={\the\dp\msc@box},
                    % text height={{\the\ht\msc@box+\the\dp\msc@box}},
                    align=center, text width=\msc@width-2*\mscget{label distance},}
   \fi
   \def\aa{aeee}
   \xdef\msc@tempb{[/msc, draw, anchor=north,shape=\msc@shape,
     \msc@tempr
     inner sep=\mscget{label distance},
     minimum height=\mscget{condition height},
     minimum width=\msc@width,
     every condition,/tikz/line width=\mscget{line width}, #1](msc@condition@\the\c@condition)
     at ({(\msc@maxx+\msc@minx)/2},\msc@y) {\unexpanded\expandafter{#2}};
   }
 \fi
 \end{pgfscope}
 \adddraw{\msc@instname}{\the\c@msclevelcnt}{\expandafter\node\msc@tempb }
  \@for\msc@args:=#3\do{%
    \edef\msc@tempa{\noexpand\mscgety{(msc@condition@\the\c@condition.south)}{\noexpand\msc@y}
      \noexpand\msc@setlastlevel{\msc@args}{\noexpand\msc@y}}
%     \show\msc@tempa
    \adddraw{\msc@args}{\the\c@msclevelcnt}{\msc@tempa}
  }
}
% \order[side]{sender}{receiver}[level shift]
\def\order{
  \begin{pgfscope}
  \def\msc@options{}
  \pgfutil@ifnextchar[{\msc@order@opt}{\msc@order@opt[]}
}
\def\msc@order@opt[#1]{
  \ifx#1l \mscset{side=left}
  \else \ifx#1r \mscset{side=right}
    \else
      \mscset{#1}
      \def\msc@options{#1}
     \fi
  \fi
  \pgfutil@ifnextchar[{\msc@order@opt}{\msc@order@noopt}
}
\def\msc@order@noopt#1#2{
  \xdef\msc@sender{#1}
  \xdef\msc@receiver{#2}
  \pgfutil@ifnextchar[{\msc@order}{\msc@order[\mscget{level shift}]}
}
\def\msc@order[#1]{
  \pgfkeysifdefined{/msc/instance/\msc@receiver/no}{
    \pgfkeysifdefined{/msc/instance/\msc@sender/no}{
      \msc@getlevel shift{#1}{\msc@level shift}{\msc@tempa}
      \ifx\msc@sender\msc@receiver  %self message
        \IfStrEq{\mscget{side}}{left}{
          \def\msc@dir{l}
          \edef\msc@dist{-\mscget{self message width}}
        }{
          \def\msc@dir{r}
          \edef\msc@dist{\mscget{self message width}}
        }
        \xdef\msc@tempb{[/msc, order loop={\msc@dist},
                 every order, \msc@options]
                 (msc@node\msc@sender @\msc@dir\the\c@msclevelcnt) to
                 (msc@node\msc@receiver @\msc@dir\msc@level shift);
        }
      \else
        \ifnum\mscget{instance/\msc@sender/no}\expandafter<\mscget{instance/\msc@receiver/no}\relax
              \def\msc@ldir{r}
              \def\msc@rdir{l}
        \else
              \def\msc@ldir{l}
              \def\msc@rdir{r}
        \fi
        \xdef\msc@tempb{[/msc,order,every order,\msc@options]
                 (msc@node\msc@sender @\msc@ldir\the\c@msclevelcnt) to
                 (msc@node\msc@receiver @\msc@rdir\msc@level shift);
        }
      \fi
      \end{pgfscope}
      \addDdraw{\msc@sender}{\msc@receiver}{\msc@tempa}{\expandafter\draw\msc@tempb}
    }{\msc@instundefinederr{\msc@sender}\end{pgfscope}}
  }{\msc@instundefinederr{\msc@receiver}\end{pgfscope}}
}

% update the lastlevel for a given instance
% #1 name of the instance
% #2 level number
\def\msc@updatelastlevel#1#2{
  \msc@StrEqEither{#1}{envright}{envleft} {}{
    \path let \p1 = (msc@lastlevel#1), \p2 = (msc@level#2)
      in coordinate (msc@lastlevel#1) at (\x1,{min(\y1,\y2)});
  }
}
% set the lastlevel for a given instance
% #1 name of the instance
% #2 new height
\def\msc@setlastlevel#1#2{
  \msc@StrEqEither{#1}{envright}{envleft} {}{
    \draw let \p1 = (msc@lastlevel#1) in coordinate (msc@lastlevel#1)
    at (\x1,{min(\y1,#2)});
  }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  CO-REGIONS
%
% \coregionstart starts a coregion on instance #1 (nickname) in the current level.
\def\coregionstart{\pgfutil@ifnextchar[{\msc@coreg}{\msc@coreg[]}}
\def\msc@coreg[#1]#2{
  \regionstart[#1]{coregion}{#2}}
% \coregionend ends a coregion on instance #1 (nickname) in the current level.
\def\coregionend#1{\regionend{#1}}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% REGIONS (normal, coregion, suspension, and activation)
%
%

% define the begining of a region on the instance line
%\regionstart[options]{type}{instname}
% instance/#2/region/.initial=line, %possible % values:normal,coregion,suspension,activation
\def\regionstart{\pgfutil@ifnextchar[\msc@regionstart{\msc@regionstart[]}}
\def\msc@regionstart[#1]#2#3{
  \begin{pgfscope}
    \mscset{#1}
    \xdef\msc@tempa{\mscget{region width}}
  \end{pgfscope}
  \pgfkeysifdefined{/msc/instance/#3/no}{
    \msc@StrEqEither{\mscget{instance/#3/region}}{normal}{seminormal}{
      \msc@drawinstanceline{#2}{\the\c@msclevelcnt}
      \IfEqCase{#2} {{coregion}{} {activation}{} {suspension}{}}
         [\msc@unknownregionstyleerr{#2}]
      \mscset{instance/#3/region={#2},
              instance/#3/region level/.expand once={\the\c@msclevelcnt},
              instance/#3/region width/.expand once=\msc@tempa}%
       \msc@regbar{#3}{\the\c@msclevelcnt}
    }
    {msc@regionstartederr{#3}{\mscget{instance/#3/region}}}
  }
  {\msc@instundefinederr{#3}}
}
%helper function to draw region horizontal bar(s)
% #1 instname
% #2 level
\def\msc@regbar#1#2{
  \xdef\msc@temp@body{}
  \edef\msc@region{\mscget{instance/#1/region}}
  \pgfpointanchor{msc@level#2}{center}
  \edef\msc@regbar@tempb{\the\pgf@y}
  \pgfpointanchor{msc@lastlevel#1}{center}
  \pgfmathparse{\msc@regbar@tempb-\the\pgf@y}
  \ifnum0<\pgfmathresult\relax
  \else
    \IfStrEq{\mscget{instance/#1/type}}{fat}{
      \IfStrEq{\msc@region}{coregion}{
        \xdef\msc@temp@body{
            [/msc,/tikz/line width=\mscget{line width}, draw, every region,every
            coregion] let \noexpand\p1=(msc@node#1@l#2) in
          (\noexpand\x1-\mscget{instance/#1/region width}/2-\mscget{line width}/2,\noexpand\y1) --
          (\noexpand\x1+\mscget{instance/#1/region width}/2+\mscget{line
            width}/2,\noexpand\y1);
          \noexpand\draw [/msc,/tikz/line width=\mscget{line width}, every region,every
            coregion] let \noexpand\p1=(msc@node#1@r#2) in
          (\noexpand\x1-\mscget{instance/#1/region width}/2-\mscget{line width}/2,\noexpand\y1) --
          (\noexpand\x1+\mscget{instance/#1/region width}/2+\mscget{line width}/2,\noexpand\y1);
        }
      }{
        \xdef\msc@temp@body{
          [/msc,/tikz/line width=\mscget{line width}, draw, every region,every \msc@region] let
            \noexpand\p1=(msc@node#1@l#2), \noexpand\p2=(msc@node#1@r#2) in
           (\noexpand\x1-\mscget{line width}/2,\noexpand\y1)--(\noexpand\x2+\mscget{line width}/2,\noexpand\y2);
        }
      }
    }{
      \IfStrEq{\msc@region}{coregion}{
        \xdef\msc@temp@body{
           [/msc,/tikz/line width=\mscget{line width}, draw, every region,every coregion] let
            \noexpand\p1=(msc@node#1@l#2) in
          (\noexpand\x1-\mscget{instance/#1/region width}/2-\mscget{line width}/2,\noexpand\y1) --
          (\noexpand\x1+\mscget{instance/#1/region width}/2+\mscget{line width}/2,\noexpand\y1);
        }
      }{
        \xdef\msc@temp@body{
              [/msc,/tikz/line width=\mscget{line width}, draw, every region, every \msc@region]
              let
              \noexpand\p1=(msc@node#1@l#2), \noexpand\p2=(msc@node#1@r#2) in
             (\noexpand\x1-\mscget{line width}/2,\noexpand\y1)--(\noexpand\x2+\mscget{line width}/2,\noexpand\y2);
        }
      }
    }
%     \show\msc@temp@body
    \adddraw{#1}{\the\c@msclevelcnt}{\expandafter\path\msc@temp@body}
    \adddraw{#1}{\the\c@msclevelcnt}{\mscgety{(msc@lastlevel#1)}{\msc@yo}\msc@setlastlevel{#1}{\msc@yo-\mscget{line width}/2}}
  \fi
}
% draw the instance line with the region
% #1 instname
% #2 level
\def\msc@regbody#1#2{
  \xdef\msc@temp@body{;}
  \pgfpointanchor{msc@level#2}{center}
  \edef\msc@regbody@tempb{\the\pgf@y}
  \pgfpointanchor{msc@lastlevel#1}{center}
  \pgfmathparse{\msc@regbody@tempb-\the\pgf@y}
  \ifnum0<\pgfmathresult\relax
  \else
    \IfEqCase{\mscget{instance/#1/region}}{
      {coregion}{
        \IfStrEq{\mscget{instance/#1/type}}{fat}{
          \xdef\msc@temp@body{
             [/msc,/tikz/line width=\mscget{line width}, draw, mscdash, every region, every coregion]
                let \noexpand\noexpand\noexpand\p1=(msc@node#1@r#2), \noexpand\noexpand\noexpand\p2=(msc@lastlevel#1) in
                (\noexpand\noexpand\noexpand\x1,\noexpand\noexpand\noexpand\y2)--
                (\noexpand\noexpand\noexpand\x1,{min(\noexpand\noexpand\noexpand\y2,\noexpand\noexpand\noexpand\y1)});
          }
        }{\xdef\msc@temp@body{;}}
        \xdef\msc@temp@body{
          [/msc,/tikz/line width=\mscget{line width}, draw, mscdash, every region, every coregion]
             let \noexpand\p1=(msc@node#1@l#2), \noexpand\p2=(msc@lastlevel#1) in
               (\noexpand\x1,\noexpand\y2)--(\noexpand\x1,{min(\noexpand\y2,\noexpand\y1)});\noexpand\path\msc@temp@body
       }
      }%
      {activation}{
        \xdef\msc@temp@body{
          [/msc,fill=lightgray,/tikz/line width=\mscget{line width},every region, every activation]
          (msc@node#1@l#2|-msc@lastlevel#1) rectangle(msc@node#1@r#2);
          \noexpand\draw[/msc,/tikz/line width=\mscget{line width},every region, every activation]
          (msc@node#1@l#2) -- (msc@node#1@l#2|-msc@lastlevel#1)
          (msc@node#1@r#2) -- (msc@node#1@r#2|-msc@lastlevel#1);
        }
      }%
      {suspension}{
        \xdef\msc@temp@body{
          [/msc,draw,suspension dash,
          /tikz/line width=\mscget{line width},every region, every activation]
          (msc@node#1@r#2)--(msc@node#1@r#2|-msc@lastlevel#1)
          (msc@node#1@l#2)--(msc@node#1@l#2|-msc@lastlevel#1);
        }
      }%
    }[]
    \adddraw{#1}{\the\c@msclevelcnt}{\expandafter\path\msc@temp@body}
  \fi
}
% define the end of region
%\regionend{instname}
\def\regionend#1{
  \pgfkeysifdefined{/msc/instance/#1/no}{
    \edef\msc@region{\mscget{instance/#1/region}}
    \IfStrEq{\msc@region}{normal}{
      \msc@regionnotstartederr{#1}
    }
    \msc@regbody{#1}{\the\c@msclevelcnt}
    \msc@regbar{#1}{\mscget{instance/#1/region level}}
    \msc@regbar{#1}{\the\c@msclevelcnt}
    \adddraw{#1}{\the\c@msclevelcnt}{\msc@updatelastlevel{#1}{\the\c@msclevelcnt}}
    \msc@StrEqEither{\msc@region}{suspension}{activation}{
      \mscset{instance/#1/region=seminormal}
    }{\mscset{instance/#1/region=normal}}
  }
  {
    \msc@instundefinederr{#1}
  }
}
%macro stores first character in \msc@fst and last character in \msc@lst
\def\msc@fl#1{\msc@flx#1\empty\empty\empty}
\def\msc@flx#1#2#3\empty{%
\edef\msc@fst{#1}%
 \edef\msc@cdar{#2}%
 \edef\msc@cddr{#3}%
  \ifx\msc@cddr\empty
    \let\msc@lst\msc@cdar
  \else
     \expandafter\msc@flxx
   \fi
  #3}
\def\msc@flxx#1#2\empty{%
 \edef\msc@car{#1}%
  \ifx\msc@car\empty
  \else
     \let\msc@lst\msc@car
     \expandafter\msc@flxx
   \fi
  #2\empty}
%\referencestart[options][lo][ro]{nickname}{text}{leftinstance}{rightinstance}
\def\referencestart{
  \begin{pgfscope}\gdef\msc@options{}
  \pgfutil@ifnextchar[\msc@refstartlo{\msc@refstartloro}}
\def\msc@refstartloro{
  \edef\msc@lo{\mscget{left reference overlap}}
  \edef\msc@ro{\mscget{right reference overlap}}
  \msc@refstart
}
\def\msc@refstartlo[#1]{
  \expandafter\expandafter\expandafter\msc@fl\expandafter{#1}
  \msc@caseFiveEither{\msc@fst}{1}{2}{3}{4}{5}{
    \edef\msc@lo{#1} \pgfutil@ifnextchar[\msc@refstartro{\msc@refstartro[\mscget{right reference overlap}]}
  }{
    \msc@caseFiveEither{\msc@fst}{6}{7}{8}{9}{0}{
      \edef\msc@lo{#1} \pgfutil@ifnextchar[\msc@refstartro{\msc@refstartro[\mscget{right reference overlap}]}
    }{
      \msc@StrEqEither{\msc@fst}{-}{.}{
        \edef\msc@lo{#1} \pgfutil@ifnextchar[\msc@refstartro{\msc@refstartro[\mscget{right reference overlap}]}
      }{
        \mscset{#1}
        \xdef\msc@options{#1}
        \pgfutil@ifnextchar[\msc@refstartlo{\msc@refstartloro}
      }
    }
  }
}
\def\msc@refstartro[#1]{
  \edef\msc@ro{#1}\msc@refstart
}
\def\msc@refstart#1#2#3#4{
  \pgfkeysifdefined{/msc/instance/#3/no}{
    \pgfkeysifdefined{/msc/instance/#4/no}{
        \path[draw] (msc@level\the\c@msclevelcnt-|msc@node#3@l0) ++(-\msc@lo,0) coordinate
       (msc@node#1left)
       (msc@level\the\c@msclevelcnt-|msc@node#4@r0) ++(\msc@ro,0) coordinate
       (msc@node#1right);
      \end{pgfscope}
      \msc@cover{#3}{#4}{+}
      \mscset{
        reference/#1/options/.style/.expanded=\msc@options,
        reference/#1/left/.initial=#3,
        reference/#1/text/.initial={#2},
        reference/#1/right/.initial=#4
      }
    }{
      \end{pgfscope}\msc@instundefinederr{#4}
    }
  }{
    \end{pgfscope}\msc@instundefinederr{#3}
  }
}
\def\msc@cover#1#2#3{
  \pgfmathparse{\mscget{instance/#2/cover} #3 1}
  \mscset{instance/#2/cover/.expand once=\pgfmathresult}
  \IfStrEq{#1}{#2}{}{
    \ifnum0<\mscget{instance/\msc@inst@name/no}
      \msc@previousinstance{#2}
      \msc@cover{#1}{\msc@inst@name}{#3}
      \msc@regbody{\msc@inst@name}{\the\c@msclevelcnt}
    \else
    \fi
  }
}
\def\referenceend#1{
  \pgfkeysifdefined{/msc/reference/#1/left}{%calculating framearch
    \mscgetx{(msc@node#1left)}{\msc@xa}
    \mscgetx{(msc@node#1right)}{\msc@xb}
    \mscgety{(msc@node#1left)}{\msc@yb}
    \mscgety{(msc@level\the\c@msclevelcnt)}{\msc@ya}
    \pgfmathparse{0.25/2 *min(\msc@xb-\msc@xa,\msc@yb-\msc@ya)}
    \edef\msc@tempa{\pgfmathresult}
    \msc@cover{\mscget{reference/#1/left}}{\mscget{reference/#1/right}}{-}
    \node[/msc,fit={(msc@node#1left) (msc@node#1right)
      (msc@node#1right|-msc@level\the\c@msclevelcnt)},
      draw,rectangle, rounded corners=\msc@tempa pt, inner sep=0,/tikz/line
      width=\mscget{line width},every reference,
      /msc/reference/#1/options
      ]{\mscget{reference/#1/text}};
   }{
    \msc@refundefinederr{#1}
   }
}
%\inlinestart[options][lo][ro]{nickname}{text}{leftinstance}{rightinstance}
\def\inlinestart{
  \begin{pgfscope}
  \gdef\msc@options{}
  \pgfutil@ifnextchar[\msc@inlstartlo{\msc@inlstartloro}
}
\def\msc@inlstartloro{
  \edef\msc@lo{\mscget{left inline overlap}}
  \edef\msc@ro{\mscget{right inline overlap}}
  \msc@inlstart
}

\def\msc@inlstartlo[#1]{
  \expandafter\expandafter\expandafter\msc@fl\expandafter{#1}
  \msc@caseFiveEither{\msc@fst}{1}{2}{3}{4}{5}{
    \edef\msc@lo{#1}
    \pgfutil@ifnextchar[\msc@inlstartro{\msc@inlstartro[\mscget{right inline overlap}]}
  }{
    \msc@caseFiveEither{\msc@fst}{6}{7}{8}{9}{0}{
      \edef\msc@lo{#1} \pgfutil@ifnextchar[\msc@inlstartro{\msc@inlstartro[\mscget{right inline overlap}]}
    }{
      \msc@StrEqEither{\msc@fst}{-}{.}{
        \edef\msc@lo{#1}
        \pgfutil@ifnextchar[\msc@inlstartro{\msc@inlstartro[\mscget{right inline overlap}]}
      }{
        \mscset{#1}
        \xdef\msc@options{#1}
        \pgfutil@ifnextchar[\msc@inlstartlo{\msc@inlstartloro}
      }
    }
  }
}
\def\msc@inlstartro[#1]{
  \edef\msc@ro{#1}\msc@inlstart
}
%msc@inlstart{nickname}{text}{leftinstance}{rightinstance}
\def\msc@inlstart#1#2#3#4{
  \pgfkeysifdefined{/msc/instance/#3/no}{
    \pgfkeysifdefined{/msc/instance/#4/no}{
      \path[draw] (msc@level\the\c@msclevelcnt-|msc@node#3@l0) ++(-\msc@lo,0) coordinate
        (msc@node#1left)
       (msc@level\the\c@msclevelcnt-|msc@node#4@r0) ++(\msc@ro,0) coordinate
       (msc@node#1right);
      \end{pgfscope}
      \mscset{
        inline/#1/options/.style/.expanded=\msc@options,
        inline/#1/left/.initial={#3},
        inline/#1/text/.initial={#2}
      }
    }{
      \end{pgfscope}\msc@instundefinederr{#4}
    }
  }{
    \end{pgfscope}\msc@instundefinederr{#3}
  }
}
%\inlineseparator[options]{nickname}
\def\inlineseparator{
  \begin{pgfscope}
  \pgfutil@ifnextchar[\msc@inlseparator{\msc@inlseparator[]}
}
\def\msc@inlseparator[#1]#2{
  \pgfkeysifdefined{/msc/inline/#2/left}{
    \mscset{#1}
    \end{pgfscope}
    \path[/msc,draw, mscdash,
    /tikz/line width=\mscget{line width},
    dash phase=2pt,
    every inline,inline/#2/options,#1]
    (msc@node#2left|-msc@level\the\c@msclevelcnt)--(msc@node#2right|-msc@level\the\c@msclevelcnt);
   }{
    \msc@inlundefinederr{#2}
    \end{pgfscope}
   }
}
%\inlineend{nickname}
\def\inlineend{
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@inlineend}{\msc@isstarfalse\msc@inlineend*}
}
\def\msc@inlineend*#1{
  \pgfkeysifdefined{/msc/inline/#1/text}{
    \path[/msc,draw,
    /tikz/line width=\mscget{line width},
    every inline,inline/#1/options]
    (msc@node#1left) node[/msc,draw,chamfered rectangle,
    chamfered rectangle corners=south east,
    chamfered rectangle xsep=\mscget{label distance}/2,
    chamfered rectangle ysep=\mscget{label distance}/2,
    inner sep=\mscget{label distance}/4,
    xshift=-\mscget{line width}/2, yshift=\mscget{line width}/2,
    anchor=north west,inline/#1/options](a)
    {\mscget{inline/#1/text}}
    (msc@node#1left|-msc@level\the\c@msclevelcnt)++(0,-\mscget{line width}/2)
    --(msc@node#1left)--(msc@node#1right)--
    (msc@node#1right|-msc@level\the\c@msclevelcnt)--++(0,-\mscget{line width}/2);
    \ifmsc@isstar
      \inlineseparator{#1}
    \else
      \inlineseparator[solid]{#1}
    \fi
   }{
    \msc@refundefinederr{#1}
   }
}
%\gate(*)[options][hpos][vpos]{gatename}{instname}
\def\gate{
  \begin{pgfscope}
  \def\msc@options{}
  \pgfutil@ifnextchar*{\msc@isstartrue\msc@gate@star}{\msc@isstarfalse\msc@gate@star*}
}
\def\msc@gate@star*{
  \pgfutil@ifnextchar[{\msc@gate@opt}{\msc@gate@opt[]}
}
\def\msc@gate@opt[#1]{
  \IfStrEq{#1}{l}{\mscset{side=left} \pgfutil@ifnextchar[\msc@gate{\msc@gate[]}
  }{
    \IfStrEq{#1}{r}{\mscset{side=right}\pgfutil@ifnextchar[\msc@gate{\msc@gate[]}
    }{
      \mscset{#1}
      \def\msc@options{#1}
      \pgfutil@ifnextchar[\msc@gate@opt{\msc@gate[]}
    }
  }
}
\def\msc@gate[#1]#2#3{
  \def\msc@undefined{false}
  \ifx#1c \mscset{position=mid} \else
  \ifx#1b \mscset{position=below} \else
  \ifx#1t \mscset{position=above} \fi \fi \fi
  \pgfkeysifdefined{/msc/instance/#3/no}{%instance name
    \IfStrEq{\mscget{side}}{left}{
      \edef\msc@node{msc@node#3@l\the\c@msclevelcnt}
    }{
      \edef\msc@node{msc@node#3@r\the\c@msclevelcnt}
    }
  }{
    \pgfutil@ifundefined{pgf@sh@ns@msc@node#3}{% neither instance name nor reference node
      \def\msc@undefined{true}
      \msc@instundefinederr{#3}
    }
    {
      \edef\msc@node{msc@node#3|-msc@level\the\c@msclevelcnt}
    }
  }
  \IfStrEq{\msc@undefined}{false}{
    \ifmsc@isstar
      \xdef\msc@tempb{[fill,\msc@options] (\msc@node)
        node[\mscget{position}
        \mscget{side}]{\unexpanded\expandafter{#2}} circle (\mscget{gate symbol radius});}
      \edef\msc@tempa{\noexpand\noexpand\noexpand\path[fill,line width=0pt](\msc@node) circle (\mscget{gate symbol radius});}
    \else
      \xdef\msc@tempb{[\msc@options] (\msc@node) node[\mscget{position} \mscget{side}]{\unexpanded\expandafter{#2}};}
    \fi
  }{}
  \end{pgfscope}
  \adddraw{#3}{\the\c@msclevelcnt}{\expandafter\path\msc@tempb}
}

\def\inststop{\stop*}%old name of command
%\stop(*)[options]{instancename}
\def\stop{\pgfutil@ifnextchar*{\msc@isstartrue\msc@stop@nostar}{\msc@isstarfalse\msc@stop@nostar*}}
\def\msc@stop@nostar*{\pgfutil@ifnextchar[\msc@stop{\msc@stop[]}}
\def\msc@stop[#1]#2{
  \pgfkeysifdefined{/msc/instance/#2/no}{
    \IfStrEq{on}{\mscget{instance/#2/status}}{
      \msc@global@set{instance/#2/status=off}
      \begin{pgfscope}
      \mscset{#1}
      \ifmsc@isstar \mscset{instance end=foot} \fi
      \IfStrEq{\mscget{instance end}}{foot}{
        \xdef\msc@tempb{[/msc,fill=\mscget{draw foot},/tikz/line width=\mscget{line width}, every instance foot,#1]
          (msc@lastlevel#2-|msc@node#2.east)
          ++(0,-\mscget{foot height}) rectangle
          (msc@lastlevel#2-|msc@node#2.west)
          (msc@lastlevel#2-|msc@node#2.south)
          ++(0,-\mscget{foot height})
          coordinate (msc@foot#2);
        }
      }{
        \xdef\msc@tempb{[/msc,draw=\mscget{draw foot}, /tikz/line width=\mscget{line width},
          every instance foot,#1]
          (msc@node#2@l\the\c@msclevelcnt) ++(-\mscget{line width}/2,0)--
          ($(msc@node#2@l\the\c@msclevelcnt)!0.5!(msc@node#2@r\the\c@msclevelcnt)$)
          coordinate (msc@foot#2)
          ++(\mscget{stop width}/2,\mscget{stop width}/2)
          -- ++(-\mscget{stop width},-\mscget{stop width})
          ++(0,\mscget{stop width})
          -- ++(\mscget{stop width},-\mscget{stop width})
          ++(-\mscget{stop width}/2,\mscget{stop width}/2)
          -- (msc@node#2@r\the\c@msclevelcnt)
          --++(\mscget{line width}/2,0);
        }
      }
      \end{pgfscope}
      \adddraw{#2}{\the\c@msclevelcnt}{\expandafter\path\msc@tempb}
    }{}
  }{\msc@instundefinederr{#2}}
}

\def\mscunit{cm}%
\def\msckeywordstyle#1{\textbf{#1}}
\newcommand{\setmsckeyword}[1]{\mscset{/msc/msc keyword={#1}}}
\newcommand{\setmsckeywordstyle}[1]{\def\msckeywordstyle{#1}}%
\newcommand{\drawframe}[1]{\IfStrEq{#1}{yes}{\mscset{draw frame=}}{\IfStrEq{#1}{no}{\mscset{draw frame=none}}{}}}%
\newcommand{\drawinsthead}[1]{\IfStrEq{#1}{yes}{\mscset{draw head=}}{\IfStrEq{#1}{no}{\mscset{draw head=none}}{}}}
\newcommand{\drawinstfoot}[1]{\IfStrEq{#1}{yes}{\mscset{draw foot=}}{\IfStrEq{#1}{no}{\mscset{draw foot=none}}{}}}
\newcommand{\showgrid}{\mscset{draw grid}}
\newcommand{\nogrid}{\mscset{draw grid={none}}}
\newcommand{\setmscscale}[1]{\mscset{msc scale=#1}}%
\newcommand{\messarrowscale}[1]{\mscset{arrow scale=#1}}%

%\mscset but globally
\def\msc@global@set#1{\globaldefs=1\relax\mscset{#1}\globaldefs=0\relax}%

% the msc-environment
% \begin{msc}[options][headerpos]{mscname}
% ...definition of the MSC...
% \end{msc}
%\regionend{instname}
\def\msc{%
  \pgfutil@ifnextchar[\msc@mscopt{\msc@mscopt[l]}
}
\def\msc@mscopt[#1]{%
  % \msc@titlejustification says how the title should be justified
  % Justification is relative to the total width of the msc
  % l: left (default)
  % c: centered
  % r: right
  \ifx#1l
    \mscset{title position=left}
  \else \ifx#1c
      \mscset{title position=center}
    \else \ifx#1r
        \mscset{title position=right}
      \else
        \pgfutil@ifundefined{msc@tikzstarted}
        {
          \tikzpicture[/msc,#1]%
          \def\msc@tikzstarted{}}%
        {
          \relax}
      \fi
    \fi
  \fi
  \pgfutil@ifnextchar[{\msc@mscopt}{\msc@mscnoopt}
}
% core of msc with options parsed -
\def\msc@mscnoopt#1{% parse name
  % #1: mscname
  \pgfutil@ifundefined{msc@tikzstarted}
    {
      \tikzpicture\def\msc@tikzstarted{}%
    }%
    {\relax}
  % Options handling ends here
  \c@mscinstcnt 0\relax%  initialize number of instances to 0
  \c@condition 0\relax%  initialize number of condition to 0
  \c@msclevelcnt 1\relax% initialize level to 1
  \gdef\c@maxlevelcnt{0}% initialize maximum encountered level to 0
%   \msc@global@set{
%     }
  \mscset{
    /tikz/scale/.expanded=\mscget{msc scale},                                              
    /tikz/every node/.style/.expanded={scale/.expanded=\mscget{msc scale}},
    instance/envleft/no/.initial=0,
    instance/envright/no/.initial=1,
    instanceno/0/name/.initial=envleft,%
    instanceno/1/name/.initial=envright,
    instance/envleft/type/.initial=environment,
    instance/envright/type/.initial=environment,
    instance/envleft/region/.initial=normal,
    instance/envright/region/.initial=normal,
    instance/envleft/status/.initial=on,
    instance/envright/status/.initial=on,
    /msc/msc/name/.initial={#1}
  }
  \pgfmathparse{0-\mscget{head top distance} - \mscget{head height}}
  \xdef\msc@tempa{\pgfmathresult}
  \coordinate (msc@level0) at (0,\msc@tempa pt);
  \coordinate (msc@nodeenvright) at (0,\msc@tempa pt);
  \coordinate (msc@nodeenvleft) at  (0,\msc@tempa pt);
  \coordinate (msc@footenvleft) at (msc@nodeenvleft);
  \coordinate[below=\mscget{first level height} of msc@level0] (msc@level1);
}
\def\msc@drawgrid{
  \begin{pgfonlayer}{background}
    \pgfpointanchor{current bounding box}{south west}
    \pgfmathparse{ceil(\pgf@x/\pgf@xx)}%pgf@xx stores 1 of current unit (e.g. 1cm)
    \edef\msg@xa{\pgfmathresult}%
    \pgfmathparse{ceil(\pgf@y/\pgf@yy)}%
    \edef\msg@ya{\pgfmathresult}%
    \pgfpointanchor{current bounding box}{north east}
    \pgfmathparse{floor(\pgf@x/\pgf@xx)}%
    \edef\msg@xb{\pgfmathresult}%
    \pgfmathparse{floor(\pgf@y/\pgf@yy)}%
    \edef\msg@yb{\pgfmathresult}%
    \path (\msg@xa,\msg@ya) to[/msc/\mscget{draw grid}](\msg@xb,\msg@yb);
  \end{pgfonlayer}
}
%\def\endmsc[options]
\def\endmsc{%
%   \let\setlength\msc@OrigSL
  \pgfutil@ifnextchar[\msc@endmsc{\msc@endmsc[]}
  \endtikzpicture
  \global\let\msc@tikzstarted\relax
}
\def\msc@endmsc[#1]{
  \begin{pgfscope}
    \mscset{#1}
    \ifnum0<\c@mscinstcnt\relax
      \ifnum\c@msclevelcnt<\c@maxlevelcnt\relax
        \pgfmathparse{\c@maxlevelcnt-\the\c@msclevelcnt}
        \pgfkeys{/pgf/number format/precision=0}
        \pgfmathroundto{\pgfmathresult}
        \nextlevel[\pgfmathresult]%add missing levels
      \fi
      \mscset{level height=\mscget{last level height}}
      \nextlevel %add last but one level
      \foreach \msc@tempa in {1,...,\c@mscinstcnt} {%
        \stop[instance end=foot,#1]{\mscget{instanceno/\msc@tempa/name}}
      }
      \nextlevel %add last level just to draw outstanding foots
    \else
      \coordinate (msc@footenvleft) at (msc@nodeenvright);
      \coordinate[below=\mscget{first level height} of msc@level0] (msc@level1);
    \fi
    \msc@drawenvironment
    \IfStrEq{none}{\mscget{draw grid}}{}{%we draw help grid
      \msc@drawgrid
    }
  \end{pgfscope}
}
\def\msc@drawenvironment{
  \path[line width=\mscget{line width}]
     let \p1=(msc@node\mscget{instanceno/1/name}.north),
         \p2=(msc@foot\mscget{instanceno/\the\c@mscinstcnt/name}.south),
         \p3=(current bounding box.south east),
         \p4=(current bounding box.north west)  in
          ({\x2+max(\mscget{right environment distance},\x3-\x2)},
          {\y2+min(-\mscget{foot distance},\y3-\y2)}) coordinate(msc@env@se) [draw=\mscget{draw frame}] rectangle
          ({\x1+min(-\mscget{left environment distance},\x4-\x1)},
           {\y1+max(\mscget{head top distance},\y4-\y1)}) coordinate(msc@env@nw);
     \path let \p1=(msc@env@nw),
                \p2=(msc@env@se) in
           (\x1,\y1) +(\mscget{title distance},-\mscget{title top distance})
          node[rectangle, anchor=north west, inner sep=0, align=\mscget{title
          position}, text
          width=(\x2-\x1)-2*\mscget{title distance}]
          {\msckeywordstyle{\mscget{msc keyword}} \mscget{msc/name}};
    \foreach \msc@tempa in {0,...,\c@msclevelcnt} {%create nodes for environment
     \path let \p1=(msc@env@se),
               \p2=(msc@env@nw),
               \p3=(msc@level\msc@tempa) in
         (\x2,\y3) coordinate[name=msc@nodeenvleft@r\msc@tempa,
                              alias=msc@nodeenvleft@l\msc@tempa] --
         (\x1,\y3) coordinate[name=msc@nodeenvright@l\msc@tempa,
                             alias=msc@nodeenvright@r\msc@tempa];
    }
   \pgfkeysifdefined{/msc/levelenv/havedraw}{
%     \tikzset{/msc/levelenv/draw/.show code}
      \mscset{/msc/levelenv/draw}
      \msc@global@set{/msc/levelenv/draw/.code={}}
    }{}
}
% \setmscvalues assigns compatible values to all msc-parameters
% Currently, three sets of values are supported: large, normal
% and small.
\def\setmscvalues#1{%
  \IfEqCase{#1}{%
    {large}{\mscset{#1 values}}%
    {normal}{\mscset{#1 values}}%
    {small}{\mscset{#1 values}}%
  }[\msc@unknownmscvalueserr{#1}]
}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                           HMSC
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\msckeywordstyle#1{\textbf{#1}}
\newcommand{\sethmsckeyword}[1]{\mscset{/msc/hmsc keyword={#1}}}%
\newcommand{\sethmsckeywordstyle}[1]{\def\hmsckeywordstyle{#1}}%

% \begin{hmsc}[headerpos]{hmscname}(llx,lly)(urx,ury)
\def\hmsc{\def\msc@key{hmsc} \pgfutil@ifnextchar[\msc@hmscopt{\msc@hmscopt[]}}
\def\msc@hmscopt[#1]{%
  \msc@global@set{/msc/draw code/.code={}}
  \ifx#1l
    \mscset{title position=left}
  \else \ifx#1c
      \mscset{title position=center}
    \else \ifx#1r
        \mscset{title position=right}
      \else
        \pgfutil@ifundefined{msc@tikzstarted}{
          \tikzpicture[#1]%
          \def\msc@tikzstarted{}
        }{\relax}
      \fi
    \fi
  \fi
  \pgfutil@ifnextchar[{\msc@hmscopt}{\msc@hmscnoopt}
}

\def\msc@hmscnoopt#1{%
  % #1: mscname
  \pgfutil@ifundefined{msc@tikzstarted}{
    \tikzpicture\def\msc@tikzstarted{}%
  }{\relax}
  \mscset{
    /tikz/scale/.expanded=\mscget{msc scale},                                              
    /tikz/every node/.style/.expanded={scale/.expanded=\mscget{msc scale}},
    /msc/\msc@key/name/.initial={#1}}
  \pgfutil@ifnextchar({\msc@hmsc}{}
}

\def\msc@hmsc(#1,#2)(#3,#4){
 \path (#1,#4) coordinate(msc@\msc@key @nw)
       (#3,#2) coordinate(msc@\msc@key @se);
}
%\end{hmsc}[options]
\def\endhmsc{\def\msc@key{hmsc}\pgfutil@ifnextchar[\msc@endhmsc{\msc@endhmsc[]}
  \endtikzpicture
  \global\let\msc@tikzstarted\relax
}
\def\msc@endhmsc[#1]{
  \begin{pgfscope}
    \mscset{#1}
    \pgfutil@ifundefined{pgf@sh@ns@msc@\msc@key @nw}{%use bounding box
      \path (current bounding box.north west)
      ++(-\mscget{west \msc@key\space margin},
      \mscget{north \msc@key\space margin}+\mscget{title top distance}+\heightof{\mscget{msc@key/name}}+\depthof{\mscget{\msc@key/name}})
      coordinate(msc@\msc@key @nw) (current bounding box.south east)
      ++(\mscget{east \msc@key\space margin},-\mscget{south \msc@key\space margin})
      coordinate(msc@\msc@key @se);
    }{}
    \IfStrEq{\mscget{title position}}{left}{%
      \path (msc@\msc@key @nw) +(\mscget{title distance},-\mscget{title top distance})
           node[anchor=north west,inner sep=0]
           {\msckeywordstyle{\mscget{\msc@key\space keyword}} \mscget{\msc@key/name}};
    }{
      \IfStrEq{\mscget{title position}}{center}{%
        \path let \p1=(msc@\msc@key @nw),
                  \p2=(msc@\msc@key @se) in
            (\x1/2+\x2/2,\y1) +(0,-\mscget{title top distance})
            node[anchor=north,inner sep=0]
            {\msckeywordstyle{\mscget{\msc@key\space keyword}} \mscget{\msc@key/name}};
      }{
        \IfStrEq{\mscget{title position}}{right}{%
        \path let \p1=(msc@\msc@key @nw),
                  \p2=(msc@\msc@key @se) in
            (\x2,\y1) +(-\mscget{title distance},-\mscget{title top distance})
            node[anchor=north east,inner sep=0]
            {\msckeywordstyle{\mscget{\msc@key\space keyword}} \mscget{\msc@key/name}};
        }{}
      }
    }
    \path[draw=\mscget{draw frame},line width=\mscget{line width}] (msc@\msc@key @nw)|-(msc@\msc@key @se)|-(msc@\msc@key @nw);
    \IfStrEq{none}{\mscget{draw grid}}{}{%we draw help grid
      \msc@drawgrid
    }
  \end{pgfscope}
  \mscset{/msc/draw code}
  \msc@global@set{/msc/draw code/.code={}}
}
%\hmscstartsymbol{nickname}(x,y)
\def\hmscstartsymbol{
  \edef\msc@nodetype{start symbol}
  \pgfutil@ifnextchar[\msc@nodenotext{\msc@nodenotext[]}
}
\def\msc@nodenotext[#1]#2{
  \edef\msc@options{#1} \edef\msc@name{#2}\def\msc@text{}
  \pgfutil@ifnextchar({\msc@nodeAt}{
    \edef\msc@tempb{[/msc,\msc@nodetype,#1](\msc@name){};}
    \expandafter\node\msc@tempb
  }
}
\def\msc@nodetext[#1]#2#3{
  \edef\msc@options{#1} \edef\msc@name{#2}\def\msc@text{#3}
  \pgfutil@ifnextchar({\msc@nodeAt}{
    \def\msc@tempb{[/msc,\msc@nodetype,#1](#2){#3};}
    \expandafter\node\msc@tempb
  }
}

\def\msc@nodeAt(#1,#2){
  \edef\msc@tempb{[/msc,\msc@nodetype,\msc@options](\msc@name) at (#1,#2) {\unexpanded\expandafter{\msc@text}};}
   \expandafter\node\msc@tempb
}
%\hmscendsymbol{nickname}(x,y)
\def\hmscendsymbol{
  \edef\msc@nodetype{end symbol}
  \pgfutil@ifnextchar[\msc@nodenotext{\msc@nodenotext[]}
}
%\hmscreference{nickname}{text}(x,y)
\def\hmscreference{
  \edef\msc@nodetype{reference}
  \pgfutil@ifnextchar[\msc@nodetext{\msc@nodetext[]}
}
%\hmsc condition{nickname}{text}(x,y)
\def\hmsccondition{
  \edef\msc@nodetype{condition}
  \pgfutil@ifnextchar[\msc@nodetext{\msc@nodetext[]}
}
%\hmscconnection{nickname}(x,y)
\def\hmscconnection{
  \edef\msc@nodetype{connection}
  \pgfutil@ifnextchar[\msc@nodenotext{\msc@nodenotext[]}
}
%\arrow{from-nickname}[coord-list]{to-n
\def\arrow{\pgfutil@ifnextchar[\msc@arrowopt{\msc@arrowopt[]}}
\def\msc@arrowopt[#1]#2{
  \def\msc@options{#1}
  \edef\msc@node{#2}
  \edef\msc@tempa{}
  \pgfutil@ifnextchar[\msc@arrowgetpath{\msc@arrow}
}
\def\msc@arrowgetpath[#1]{
  \StrSubstitute{#1}{(}{--(}[\msc@tempa]
  \msc@arrow
}
\def\msc@arrow#1{
  \edef\msc@tempb{[/msc,/tikz/line width=\mscget{line width},arrow style,\msc@options] (\msc@node)\msc@tempa--(#1);}
  \expandafter\draw\msc@tempb
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%                           MSC documents
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \mscdockeyword is the keyword representation of msc
\def\mscdockeywordstyle#1{\textbf{#1}}
\newcommand{\setmscdockeyword}[1]{\mscset{/msc/mscdoc keyword/.initial=mscdoc}}
\newcommand{\setmscdockeywordstyle}[1]{\def\mscdockeywordstyle{#1}}%

%\begin{mscdoc}[headerpos]{mscdocname}(llx,lly)(urx,ury)
\def\mscdoc{\edef\msc@key{mscdoc} \pgfutil@ifnextchar[\msc@hmscopt{\msc@hmscopt[]}}
%\end{mscdoc}[options]
\def\endmscdoc{\edef\msc@key{mscdoc}
  \pgfutil@ifnextchar[\msc@endhmsc{\msc@endhmsc[]}
  \endtikzpicture
  \global\let\msc@tikzstarted\relax
}
%\hmscreference{nickname}{text}(x,y)
\def\reference{\pgfutil@ifnextchar[\msc@nodenoname{\msc@nodenoname[]}}
\def\msc@nodenoname[#1]#2{
  \edef\msc@options{#1} \def\msc@text{#2}
  \pgfutil@ifnextchar({\msc@nodenonameAt}{
    \edef\msc@tempb{[/msc,reference,#1](\msc@name){\unexpanded\expandafter{\msc@text}};}
    \expandafter\node\msc@tempb
  }
}
\def\msc@nodenonameAt(#1,#2){
  \edef\msc@tempb{[/msc,reference,every reference,\msc@options] at (#1,#2) {\unexpanded\expandafter{\msc@text}};}
   \expandafter\node\msc@tempb
}
\def\separator{\pgfutil@ifnextchar[\msc@separator{\msc@separator[]}}
\def\msc@separator[#1]#2{
  \begin{pgfscope}
  \mscset{#1}
  \xdef\msc@tempb{[/msc,/tikz/line width=\mscget{line width},mscdash]
    let \noexpand\p1=(msc@mscdoc@nw), \noexpand\p2=(msc@mscdoc@se) in
    (\noexpand\x1,#2)--(\noexpand\x2,#2);}
  \end{pgfscope}
  \mscset{/msc/draw code/.append code/.expand once={\expandafter\draw\msc@tempb}}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Error and help messages
% We use the standard LaTeX2e facilities to generate error and help
% messages (for more info, see file lterror.dtx of LaTeX2e distribution).
%
%
% nickname already defined
\gdef\msc@nicknamedefinederr#1{%
  \PackageError{msc}{% error message
    nickname #1 already defined}{% help text
    You tried to use the nickname '#1' for a new\MessageBreak
    msc object (instance, reference, inline expression, etc.),\MessageBreak
    but the nickname is already assigned to another msc object.\MessageBreak
    press <enter> to continue (the new msc object will be ignored).}%
}
%instance already started
\gdef\msc@instancestarted#1{%
  \PackageError{msc}{% error message
    instance #1 is already active}{% help text
    You tried to create instance '#1' but this instance is \MessageBreak
    already active. Maybe try to stop it using "stop" command .\MessageBreak
    press <enter> to continue.}%
}
% no such msc reference error
\gdef\msc@refundefinederr#1{%
  \PackageError{msc}{% error message
    undefined reference with nickname: #1}{% help text
    You used '#1' as an reference nickname, but\MessageBreak
    there is no reference with that nickname.}%
}
% no such msc reference error
\gdef\msc@inlundefinederr#1{%
  \PackageError{msc}{% error message
    undefined inline with nickname: #1}{% help text
    You used '#1' as an inline nickname, but\MessageBreak
    there is no inline with that nickname.}%
}
% no such msc instance error
\gdef\msc@instundefinederr#1{%
  \PackageError{msc}{% error message
    undefined msc instance: #1}{% help text
    You used '#1' as an msc instance nickname, but\MessageBreak
    there is no msc instance with that nickname.}%
}
% level height is not a positive integer
\gdef\msc@notpositiveintegerwarn#1#2{%
  \PackageWarning{msc}{% error message
    parameter #1.#2 is a float when integer was expected -
    recovering by advancing by #1+1 levels where last level is scaled by 0.#2}
}
\gdef\msc@notpositiveintegererr#1{%
  \PackageError{msc}{% error message
    Could not parse as number: #1}{% help text
    The nextlevel should be a positive integer but instead #1 was given.}%
}

% unknown region style error
\gdef\msc@unknownregionstyleerr#1{%
  \PackageError{msc}{% error message
    unknown region style: #1}{% help text
    Known msc region styles are "coregion", "suspension", and "activation".\MessageBreak
    You used '#1'.}%
}
% region not started error
\gdef\msc@regionnotstartederr#1{%
  \PackageError{msc}{% error message
    no region is started for the instance "#1"}{% help text
    You tried to end a region for the instance "#1", \MessageBreak
    but no region has been started.}%
}
% region already started error
\gdef\msc@regionstartederr#1#2{%
  \PackageError{msc}{% error message
    region #2 is already started}{% help text
    You tried to start new region for the instance "#1", \MessageBreak
    while the previous region is still active.}%
}
% unknown self message label position error
\gdef\msc@unknownmscvalueserr#1{%
  \PackageError{msc}{% error message
    unknown set of msc-values: #1}{% help text
    Known sets of msc-values are "normal" and "small".\MessageBreak
    You used '#1'.}%
}
%################################################################################
%
\gdef\msc@illegaltitleplacement#1{%
  \PackageError{msc}{%
    illegal title placement specifier: #1}{% help text
    Valid title placement specifiers are:\MessageBreak
      l (left)\MessageBreak
      c (center)\MessageBreak
      r (right)}
}
%reading /usr/share/texlive/texmf-dist/tex/latex/base/latex.ltx