aboutsummaryrefslogtreecommitdiff
path: root/source/WindowsFormsApplication1/Gameplay/HackUI.cs
blob: 9767c7cb65a6685929608aec1ebdec07d285004c (plain) (blame)
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ShiftUI;

namespace ShiftOS
{
    public partial class HackUI : Form
    {
        public HackUI()
        {
            InitializeComponent();
            this.TopMost = false;
            ThisEnemyHacker = new EnemyHacker("Test Dummy", "A test hacker", "A test hacker", 1, 1, "easy");
        }

        public event EventHandler OnWin;

        private bool InOnlineBattle = false;
        private Online.Hacking.NetTransmitter transmitter = null;
        private Online.Hacking.NetTransmitter _playerTransmitter = null;
        private Online.Hacking.NetListener receiver = null;
        private Online.Hacking.NetListener player_listener = null;

        public HackUI(EnemyHacker enemy)
        {
            ThisEnemyHacker = enemy;
            InitializeComponent();
        }

        public HackUI(Online.Hacking.NetTransmitter t, Online.Hacking.NetListener l, Online.Hacking.NetListener playerListener, Online.Hacking.NetTransmitter playerTransmitter)
        {
            InOnlineBattle = true;
            transmitter = t;
            receiver = l;
            player_listener = playerListener;
            _playerTransmitter = playerTransmitter;
            InitializeComponent();
        }

        public Computer ThisPlayerPC = null;
        private decimal TotalPlayerHP = 0;
        private EnemyHacker EnemyNet = null;

        private List<Module> MyNetwork = new List<Module>();

        #region PLAYER

        private void LoadPlayerScreen()
        {
            AntiVirusBounds = new List<Rectangle>();
            var tc = new Module(SystemType.Core, 1, "localhost");
            tc.HP = 100;
            TutorialNetwork.Add(tc);
            foreach (var m in GetMyNet())
            {
                if (m.Type == SystemType.Core)
                {
                    MyNetwork.Add(m);
                }
            }
            VisualizePlayerNetwork();
            if (EnemyNet != null)
            {
                tmrplayerhealthdetect.Start();
            }
            else
            {
                tmrplayerhealthdetect.Start();
            }
            if (IsTutorial)
            {
                SetupTutorialUI(0);
            }
            if (InOnlineBattle)
                LoadOnlinePlayer();
        }

        private void VisualizePlayerNetwork()
        {
            AllPlayerComputers = new List<Computer>();
            foreach (Module m in MyNetwork)
            {
                if (AllPlayerComputers.Count <= 10)
                {
                    var c = m.Deploy();
                    if (c.Type == SystemType.Core)
                    {
                        ThisPlayerPC = c;
                    }
                    AddModule(c);
                }
            }
            if(ThisPlayerPC == null)
            {
                var m = new Module(SystemType.Core, 1, "localhost");
                GetMyNet().Add(m);
                MyNetwork.Add(m);
                var c = m.Deploy();
                ThisPlayerPC = c;
                AddModule(c);
            }
        }

        private void player_listener_ModuleRemoved(object sender, Online.Hacking.Events.ModuleRemoved e)
        {
            Computer c = null;
            foreach (var m in AllPlayerComputers)
            {
                if (m.Hostname == e.new_module)
                {
                    c = m;
                }
            }
            AllPlayerComputers.Remove(c);
            c.Dispose();
        }

        private void player_listener_ModuleHealthSet(object sender, Online.Hacking.Events.Health e)
        {
            var mod = new Computer();
            foreach (var m in AllPlayerComputers)
            {
                if (m.Hostname == e.host_name)
                    mod = m;
            }
            mod.HP = e.health;
            int old_hp = mod.HP;
            mod.HP = e.health;
            if (mod.HP > old_hp)
            {
                mod.throw_repaired();
            }
            else
            {
                mod.throw_damaged();
            }
        }


        public void LoadOnlinePlayer()
        {
            //register event handlers
            player_listener.ModuleHealthSet += player_listener_ModuleHealthSet;
            player_listener.ModuleRemoved += player_listener_ModuleRemoved;
            player_listener.ModuleDisabled += (o, e) =>
            {
                foreach (var c in AllPlayerComputers)
                {
                    if (c.Hostname == e.hostName)
                    {
                        c.Disable();
                    }
                }
            };
        }

        public List<Computer> AllPlayerComputers = null;

        private void tmrplayerhealthdetect_Tick(object sender, EventArgs e)
        {
            if(ThisPlayerPC != null)
            {
                ThisPlayerPC.Left = (pnlyou.Width - ThisPlayerPC.Width) / 2;
                ThisPlayerPC.Top = (pnlyou.Height - ThisPlayerPC.Height) / 2;

            }
            var rnd = new Random();
            int chance = 0;
            foreach (var pc in AllPlayerComputers)
            {
                if (pc.Disabled == false)
                {
                    var elist = new List<Computer>();
                    if (pc.Enslaved)
                        elist = AllPlayerComputers;
                    else
                        elist = pc.Enemies;
                    if (elist != null)
                    {
                        foreach (var enemy in elist)
                        {
                            if (enemy != null && enemy.HP > 0)
                            {
                                chance = rnd.Next(1, 15);
                                if (chance == 7)
                                {
                                    if (IsTutorial)
                                    {
                                        if (TutorialProgress == 32 || TutorialProgress == 9)
                                        {
                                            enemy.LaunchAttack(pc.GetProperType(), pc.GetDamageRate());
                                        }
                                    }
                                    else
                                    {
                                        enemy.LaunchAttack(pc.GetProperType(), pc.GetDamageRate());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            decimal health = 0;
            foreach (var pc in AllPlayerComputers)
            {
                health += (decimal)pc.HP;
            }
            if (health > TotalPlayerHP)
            {
                TotalPlayerHP = health;
            }
            try
            {
                decimal percent = (health / TotalPlayerHP) * 100;
                lbstats.Text = $"System Health: {percent}%";
            }
            catch
            {

            }
                if (ThisPlayerPC.HP <= 0)
            {
                API.CreateInfoboxSession("System compromised.", "The enemy hacker has overthrown your defenses and compromised your system. You will need to wait until your core heals before beginning another battle.", infobox.InfoboxMode.Info);
                Hacking.Failure = true;
                Hacking.FailDate = DateTime.Now;
                UserRequestedClose = false;
                this.Close();
            }
        }

        public bool UserRequestedClose = true;

        private void this_Closing(object sender, FormClosingEventArgs e)
        {
            var t = new Thread(new ThreadStart(() =>
            {
                int prev_volume = Audio._wmp.settings.volume;
                while(Audio._wmp.settings.volume > 0)
                {
                    Thread.Sleep(100);
                    Audio._wmp.settings.volume -= 1;
                }
                Audio.ForceStop();
                Audio._wmp.settings.volume = prev_volume;
            }));
            t.Start();
            if (UserRequestedClose == false)
            {
                this.TopMost = false;
                Computer[] pcs = new Computer[AllPlayerComputers.Count];
                AllPlayerComputers.CopyTo(pcs);
                foreach (var pc in pcs)
                {
                    pc?.Dispose();
                }
                Computer[] epcs = new Computer[AllEnemyComputers.Count];
                AllEnemyComputers.CopyTo(epcs);
                foreach (var epc in epcs)
                {
                    epc?.Dispose();
                }
                tmrplayerhealthdetect.Stop();
                tmrenemyhealthdetect.Stop();
                Hacking.RepairTimer.Start(); //Now the player can repair.
            }
            else
            {
                e.Cancel = true;
            }
        }

        public Computer SelectedPlayerComputer = null;

        public Computer module_to_steal = null;

        public void AddModule(Computer newModule)
        {
            if (InOnlineBattle)
            {
                newModule.Transmitter = transmitter;
                transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.PlaceModule, new Online.Hacking.Module { Grade = newModule.Grade, Hostname = newModule.Hostname, HP = newModule.HP, Type = (int)newModule.Type, X = newModule.Left, Y = newModule.Top });
            }
            pnlyou.Widgets.Add(newModule);
            int hp = newModule.HP;
            WriteLine($"[Network] Welcome to the network, {newModule.Hostname}!");
            TotalPlayerHP += newModule.HP;
            AllPlayerComputers.Add(newModule);
            newModule.Show();
            if (!InOnlineBattle)
            {
                newModule.StolenModule += (o, a) =>
                {
                    var t = new Thread(new ThreadStart(() =>
                    {
                        var rnd = new Random();
                        var lst = new List<Computer>();
                        if (newModule.Enslaved)
                            lst = AllPlayerComputers;
                        else
                            lst = AllEnemyComputers;
                        WriteLine($"[{newModule.Hostname}] Starting network hack...");
                        Thread.Sleep(5000);

                        var pc = lst[rnd.Next(0, lst.Count)];
                        this.Invoke(new Action(() =>
                        {
                            if (pc.Type != SystemType.Core)
                            {
                                module_to_steal = pc;

                                pgpong.Left = (this.Width - pgpong.Width) / 2;
                                pgpong.Top = (this.Height - pgpong.Height) / 2;

                                pgpong.Show();
                                newgame();
                            }
                        }));
                    }));
                    t.Start();
                };
                newModule.EnslavedModule += (o, e) =>
                {
                    if (!newModule.Enslaved)
                    {
                        var pc = AllEnemyComputers[rand.Next(0, AllEnemyComputers.Count)];
                        if (!pc.Enslaved)
                        {
                            WriteLine($"[{newModule.Hostname}] Successfully enslaved {pc.Hostname}");
                            pc.Enslaved = true;
                        }

                    }
                };
            }
            newModule.OnDestruction += (object s, EventArgs a) =>
            {
                if (this.SelectedPlayerComputer == newModule)
                {
                    SelectedPlayerComputer = null;
                }
                if (newModule.Type == SystemType.Firewall)
                {
                    Player_Firewall_Destroy(newModule);
                }
                AllPlayerComputers.Remove(newModule);
                newModule.Dispose();
                WriteLine($"[Network] {newModule.Hostname} has gone OFFLINE.");
                transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.RemoveModule, newModule.Hostname);
            };
            newModule.Select += (object s, EventArgs e) =>
            {
                SelectedPlayerComputer = newModule;
                ShowPCInfo(newModule.Hostname);
                if (IsTutorial)
                {
                    if (TutorialProgress == 6)
                    {
                        if (newModule == ThisPlayerPC)
                        {
                            SetupTutorialUI(7);
                        }
                    }
                }
            };
            newModule.HP_Decreased += new EventHandler(Player_System_Damaged);
            newModule.OnRepair += new EventHandler(Player_System_Repaired);
            if (newModule.Type == SystemType.Antivirus || newModule.Type == SystemType.RepairModule)
            {
                var b = newModule.GetAreaOfEffect();
                AntiVirusBounds.Add(b);
                pnlyou.Refresh();
                newModule.AntivirusRepair += (object s, EventArgs a) =>
                {
                    foreach (Computer pc in AllPlayerComputers)
                    {
                        if (pc != newModule && pc.Bounds.IntersectsWith(b))
                        {
                            if (newModule.Type == SystemType.RepairModule)
                            {
                                if (pc.HP < newModule.HP)
                                {
                                    if (pc.HP < pc.GetTotal())
                                    {
                                        WriteLine($"[{newModule.Hostname}] Repairing neighbouring system \"{pc.Hostname}\"");
                                        pc.Repair(1);
                                    }
                                }

                            }
                            else
                            {
                                if (pc.HP < 10)
                                {
                                    WriteLine($"[{newModule.Hostname}] Repairing neighbouring system \"{pc.Hostname}\"");
                                    pc.Repair(1);
                                }
                            }
                        }
                    }
                };

            }
            if (newModule.Type == SystemType.Firewall)
            {
                var b = newModule.GetAreaOfEffect();
                AntiVirusBounds.Add(b);
                pnlenemy.Refresh();
                Player_Firewall_Deflect(newModule);
            }
            if (newModule.Type == SystemType.ServerStack)
            {
                newModule.MassDDoS += (object s, EventArgs a) =>
                {
                    if (newModule.Enslaved)
                        WormToPlayer();
                    else
                        WormToEnemy();
                };
            }
        }

        public void WormToEnemy()
        {
            var rnd = new Random();
            int r = rnd.Next(0, 10);
            WriteLine("[Network] Launching distributed denial-of-service attack on rival network.");
            foreach (Computer c in AllEnemyComputers)
            {
                if (r == 5)
                {
                    c.Disable();
                }
            }
        }

        public void ShowPCInfo(string hostname)
        {
            Computer c = null;
            foreach (var pc in AllPlayerComputers)
            {
                if (pc.Hostname == hostname)
                {
                    c = pc;
                }
            }

            Module mod = null;
            foreach (var m in GetMyNet())
            {
                if (m.Hostname == hostname)
                {
                    mod = m;
                }
            }
            if (mod != null)
            {
                pnlpcinfo.Left = 7;
                pnlpcinfo.Show();
                lbmoduletitle.Text = "Module Info - " + hostname;
                lbpcinfo.Text = $"Hostname: {hostname}{Environment.NewLine}Type: {mod.Type.ToString()}{Environment.NewLine}{Environment.NewLine}";
                if (mod.Type == SystemType.Core)
                {
                    lbpcinfo.Text += Environment.NewLine + "This represents your main system. If this module is destroyed, you will automatically lose the battle. Protect it at all costs.";
                    btnupgrade.Hide();
                    btnpoweroff.Hide();
                }
                else
                {
                    lbpcinfo.Text += $"{Environment.NewLine}Grade: {mod.Grade}";
                    if (mod.Grade < 4)
                    {
                        btnupgrade.Show();
                    }
                    else
                    {
                        btnupgrade.Hide();
                    }
                    btnpoweroff.Show();
                }
                
            }
            if (c != null)
            {
                if (c.Enslaved)
                {
                    lbtargets.Text = "*** WARNING ***: This module has been ENSLAVED! Consider a redeploy.";
                }
                else
                {
                    lbtargets.Text = "Targets: ";
                    if (c.Enemies != null)
                    {
                        if (c.Enemies.Count > 0)
                        {
                            foreach (var pc in c.Enemies)
                            {
                                lbtargets.Text += " " + pc.Hostname + ",";
                            }
                        }
                        else
                        {
                            lbtargets.Text += " - Click on an enemy module to target it.";
                        }
                    }
                    else
                    {
                        lbtargets.Text += " - Click on an enemy module to target it.";
                    }
                    lbtargets.Text += Environment.NewLine + "Some modules will not fire at their targets.";
                }
            }
        }

        public void Player_Firewall_Deflect(Computer fwall)
        {
            //Safegaurd...
            if (fwall.Type == SystemType.Firewall)
            {
                var r = fwall.GetAreaOfEffect();
                foreach (var pc in AllPlayerComputers)
                {
                    if (pc != fwall)
                    {
                        if (pc.Bounds.IntersectsWith(r))
                        {
                            pc.DamageDefector = fwall.Grade;
                        }
                    }
                }
            }
        }

        public void Player_Firewall_Destroy(Computer fwall)
        {
            //Safegaurd...
            if (fwall.Type == SystemType.Firewall)
            {
                var r = fwall.GetAreaOfEffect();
                foreach (var pc in AllPlayerComputers)
                {
                    if (pc.Bounds.IntersectsWith(r))
                    {
                        pc.DamageDefector = 1;
                        UpdatePlayerFirewalls();
                    }
                }
            }
        }

        public void UpdatePlayerFirewalls()
        {
            foreach (var pc in AllPlayerComputers)
            {
                if (pc.Type == SystemType.Firewall)
                {
                    Player_Firewall_Deflect(pc);
                }
            }
        }


        private void Player_System_Repaired(object s, EventArgs e)
        {
            var c = (Computer)s;
            WriteLine($"[{c.Hostname}] System repaired.");
            lbcompromised.Text = "System regenerating...";
            int location = c.Left - (lbcompromised.Width / 4);
            int y = c.Top - 25;
            lbcompromised.Location = new Point(location, y);
            lbcompromised.Show();
            c.Flash(lbcompromised);
            transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.SetHealth, $"{c.Hostname} {c.HP}");
        }


        private void Player_System_Damaged(object s, EventArgs e)
        {
            var c = (Computer)s;
            WriteLine($"[{c.Hostname}] System damaged. Total HP: {c.HP}");
            lbcompromised.Text = "System damaged!";
            int location = c.Left - (lbcompromised.Width / 4);
            int y = c.Top - 25;
            lbcompromised.Location = new Point(location, y);
            lbcompromised.Show();
            c.Flash(lbcompromised);
            transmitter?.send_message(Online.Hacking.NetTransmitter.Messages.SetHealth, $"{c.Hostname} {c.HP}");

        }

        private void btnaddmodule_Click(object sender, EventArgs e)
        {
            SetupModuleList();
            pnldefensemanager.Left = 7;
            pnldefensemanager.Visible = !pnldefensemanager.Visible;
            if (IsTutorial)
            {
                if (TutorialProgress == 12)
                {
                    SetupTutorialUI(13);
                }
            }
        }

        private Dictionary<string, SystemType> FutureModules = null;
        public List<Module> TutorialNetwork = new List<Module>();

        public void SetupModuleList()
        {
            FutureModules = new Dictionary<string, SystemType>();
            cmbmodules.Items.Clear();
            List<Module> net = null;
            if (IsTutorial)
            {
                net = TutorialNetwork;
            }
            else
            {
                net = Hacking.MyNetwork;
            }
            foreach (var item in net)
            {
                Computer m = null;
                foreach (var mod in AllPlayerComputers)
                {
                    if (mod.Hostname == item.Hostname)
                    {
                        m = mod;
                    }
                }
                if (m == null)
                {
                    if (item.HP > 0)
                    {
                        cmbmodules.Items.Add(item.Hostname);
                        FutureModules.Add(item.Hostname, item.Type);
                    }
                }
            }

        }

        bool PlacingNewModule = false;


        private void button1_Click_1(object sender, EventArgs e)
        {
            if (cmbmodules.Text != "")
            {
                PlacingNewModule = true;
                pnldefensemanager.Hide();
                if (IsTutorial)
                {
                    if (TutorialProgress == 20)
                    {
                        SetupTutorialUI(21);
                    }
                }
            }
        }

        private void playfield_MouseDown(object sender, MouseEventArgs e)
        {
            if (PlacingNewModule == true)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (AllPlayerComputers.Count < 10)
                    {
                        bool cont = true;
                        try
                        {
                            SystemType type = FutureModules[cmbmodules.Text];
                        }
                        catch 
                        {
                            cont = false;
                            API.CreateInfoboxSession("Error", "Please select a module type.", infobox.InfoboxMode.Info);
                        }
                        if (cont == true)
                        {
                            var coordinates = pnlyou.PointToClient(Cursor.Position);
                            int x = coordinates.X;
                            int y = coordinates.Y;

                            var m = new Module(FutureModules[cmbmodules.Text], 1, cmbmodules.Text);
                            foreach (var mod in GetMyNet())
                            {
                                if (mod.Hostname == cmbmodules.Text)
                                {
                                    m = mod;
                                }
                            }
                            m.X = x;
                            m.Y = y;
                            var c = m.Deploy();
                            AddModule(c);
                            API.RemoveCodepoints(10);
                            pnldefensemanager.Hide();
                            if (IsTutorial)
                            {
                                if (TutorialProgress == 21)
                                {
                                    SetupTutorialUI(22);
                                }
                                else if (TutorialProgress == 25)
                                {
                                    SetupTutorialUI(26);
                                }
                            }
                        }
                        PlacingNewModule = false;
                    }
                    else
                    {
                        API.CreateInfoboxSession("Too much deployed modules", "You can have a maximum of 10 modules deployed on your network, including your main system. You will have to wait for one to be destroyed.", infobox.InfoboxMode.Info);
                        PlacingNewModule = false;
                    }
                }
                else
                {
                    PlacingNewModule = false;
                }
            }
        }

        public List<FutureModule> BuyableModules = null;

        public void SetupBuyable()
        {
            if (!IsTutorial)
            {
                BuyableModules = Hacking.GetFutureModules();
            }
            cmbbuyable.Items.Clear();
            foreach (var m in BuyableModules)
            {
                cmbbuyable.Items.Add(m.Name);
            }
            lbmoduleinfo.Text = "";
            txtgrade.Text = "1";
        }

        private void btnbuy_Click(object sender, EventArgs e)
        {
            if (IsTutorial)
            {
                if (TutorialProgress == 14)
                {
                    SetupTutorialUI(15);
                }
            }
            SetupBuyable();
            pnlbuy.Show();
            pnldefensemanager.Hide();
        }

        public List<Rectangle> AntiVirusBounds = null;
        public List<Rectangle> IndicatorsToDestroy = new List<Rectangle>();
        private void boundpaint(object sender, PaintEventArgs e)
        {
            foreach (Rectangle r in IndicatorsToDestroy)
            {
                AntiVirusBounds.Remove(r);
                var sb = new SolidBrush(Color.Black);
                var p = new Pen(sb);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                p.Width = 2;
                e.Graphics.DrawRectangle(p, r);

            }
            IndicatorsToDestroy.Clear();
            foreach (Rectangle r in AntiVirusBounds)
            {
                IndicatorsToDestroy.Add(r);
                tmrredraw.Start();
                var sb = new SolidBrush(Color.White);
                var p = new Pen(sb);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                p.Width = 2;
                e.Graphics.DrawRectangle(p, r);
            }
        }

        private void tmrredraw_Tick(object sender, EventArgs e)
        {
            pnlyou.Refresh();
            tmrredraw.Stop();
        }

        private void SetupModuleInfo()
        {
            bool cont = false;
            FutureModule m = null;
            foreach (var mod in BuyableModules)
            {
                if (mod.Name == cmbbuyable.Text)
                {
                    m = mod;
                    cont = true;
                }
            }
            if (cont == true)
            {
                lbmoduleinfo.Text = m.Name;
                lbmoduleinfo.Text += Environment.NewLine + $"Cost: {m.Cost * Convert.ToInt32(txtgrade.Text)} CP";
                lbmoduleinfo.Text += Environment.NewLine + $"Description: {Environment.NewLine}{m.Description}";
            }
        }

        private void cmbbuyable_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetupModuleInfo();
            if (IsTutorial)
            {
                if (TutorialProgress == 17)
                {
                    if (cmbbuyable.Text == "Antivirus")
                    {
                        SetupTutorialUI(18);
                    }
                }
            }
        }

        private void txtgrade_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int g = Convert.ToInt32(txtgrade.Text);
                if (g < 1)
                {
                    txtgrade.Text = "1";
                }
                else if (g > 4)
                {
                    txtgrade.Text = "4";
                }
                SetupModuleInfo();
            }
            catch 
            {
                txtgrade.Text = "1";
                SetupModuleInfo();
            }
        }

        public List<Module> GetMyNet()
        {
            if (IsTutorial)
            {
                return TutorialNetwork;
            }
            else
            {
                return Hacking.MyNetwork;
            }
        }

        private void btndonebuying_Click(object sender, EventArgs e)
        {
            var mod = new FutureModule("", 0, "", SystemType.Core);
            bool cont = false;
            foreach (var m in BuyableModules)
            {
                if (m.Name == cmbbuyable.Text)
                {
                    mod = m;
                    cont = true;
                }
            }
            if (cont == true)
            {
                if (API.Codepoints >= mod.Cost)
                {
                    if (txthostname.Text != "")
                    {
                        bool cont2 = true;
                        string hname = txthostname.Text.Replace(" ", "_");
                        foreach (var pc in GetMyNet())
                        {
                            if (pc.Hostname == hname)
                            {
                                cont2 = false;
                            }
                        }
                        if (cont2 == true)
                        {
                            var newModule = new Module(mod.Type, Convert.ToInt32(txtgrade.Text), hname);
                            newModule.HP = newModule.GetTotalHP();
                            GetMyNet().Add(newModule);
                            API.RemoveCodepoints(mod.Cost);
                            API.CreateInfoboxSession("Module added.", "To deploy the module to the network, select 'Add Module' and choose the hostname from the menu.", infobox.InfoboxMode.Info);
                            pnlbuy.Hide();
                            if (IsTutorial)
                            {
                                if (TutorialProgress == 19)
                                {
                                    SetupTutorialUI(20);
                                }
                                else if (TutorialProgress == 24)
                                {
                                    SetupTutorialUI(25);
                                }
                            }
                        }
                        else
                        {
                            API.CreateInfoboxSession("Please enter a unique hostname.", "No two computers can share the same hostname. Please choose another.", infobox.InfoboxMode.Info);
                        }
                    }
                    else
                    {
                        API.CreateInfoboxSession("Please enter a hostname.", "It is best to enter a hostname for your new computer so you know which one it is.", infobox.InfoboxMode.Info);
                    }

                }
                else
                {
                    API.CreateInfoboxSession("Insufficient Codepoints", "You do not have enough Codepoints to buy this module.", infobox.InfoboxMode.Info);
                }
            }
        }

        private void btncloseinfo_Click(object sender, EventArgs e)
        {
            SelectedPlayerComputer = null;
            pnlpcinfo.Hide();
            Hacking.SaveCharacters();
        }

        private void btnpoweroff_Click(object sender, EventArgs e)
        {
            //Remove the computer from the game.
            pnlyou.Widgets.Remove(SelectedPlayerComputer);
            AllPlayerComputers.Remove(SelectedPlayerComputer);
            btnpoweroff.Hide();
        }

        private void btnupgrade_Click(object sender, EventArgs e)
        {
            int price = 20 * SelectedPlayerComputer.Grade;
            if (API.Codepoints >= price)
            {
                foreach (var m in GetMyNet())
                {
                    if (m.Hostname == SelectedPlayerComputer.Hostname)
                    {
                        SelectedPlayerComputer.Grade += 1;
                        m.Grade += 1;
                        Hacking.SaveCharacters();
                        API.CreateInfoboxSession("Upgrade successful.", "Your module has been upgraded.", infobox.InfoboxMode.Info);
                    }
                }
            }
            else
            {
                API.CreateInfoboxSession("Insufficient funds", $"You need at least {price} CP to upgrade this module.", infobox.InfoboxMode.Info);
            }
            ShowPCInfo(SelectedPlayerComputer.Hostname);
        }

        public int TutorialProgress = 0;
        public bool IsTutorial = false;

        public void SetupTutorialUI(int p)
        {
            TutorialProgress = p;
            lbtutorial.Show();
            pnltutorial.Show();
            pnltutorial.Left = (this.Width - pnltutorial.Width) / 2;
            pnltutorial.Top = (this.Height - pnltutorial.Height) / 2;
            switch (p)
            {
                case 0:
                    lbtutorial.Text = "Welcome to the Hacker Battle tutorial. This guide will teach you the fundamentals and basics of taking part in a Hacker Battle. When you're done here, you'll be able to start up a network and start dominating others' networks.";
                    btnaddmodule.Hide();
                    btnnext.Show();
                    break;
                case 1:
                    lbtutorial.Text = "Let's go over the user interface. It's quite simple, actually. There are 4 different displays on your screen. One for your network, one for your console, and same for the enemy.";
                    break;
                case 2:
                    lbtutorial.Text = "On the left side is your console and playfield. Your console will log all the actions that happen on your network. Your Playfield is a visual representation of your network. Each square represents a different module. Most of your actions will take place in the Playfield.";
                    break;
                case 3:
                    lbtutorial.Text = "On the right is the enemy's console and playfield. Both playfields will show the HP (health) of each module, and the total network HP.";
                    break;
                case 4:
                    lbtutorial.Text = "If the enemy's total network health hits 0%, or his core's strength hits 0%, you win.";
                    break;
                case 5:
                    lbtutorial.Text = "However, if the same happens to you, you will lose the battle, and won't be able to fight back until your Core heals.";
                    break;
                case 6:
                    lbtutorial.Text = "Each network has one core. It is represented by the square in the centre of the playfield. Click on your Core to view information about it.";
                    btnnext.Hide();
                    break;
                case 7:
                    lbtutorial.Text = "When you click on a module, you can see information about it, such as it's grade level, HP, and type. This module is a Core, so it has no grade level. It currently has 100 HP.";
                    btnnext.Show();
                    break;
                case 8:
                    lbtutorial.Text = "When a module is selected you can left-click an enemy module to target it. This will make the selected module attempt to fire at the target. Cores are very weak, but are capable of bringing a target down by 1 HP.";
                    break;
                case 9:
                    lbtutorial.Text = "The enemy Core is attacking your Core now. Looks like it's time to fight back. Select your core, and target the enemy's Core to fight back.";
                    btnnext.Hide();
                    break;
                case 10:
                    lbtutorial.Text = "Phew! He couldn't do much damage before we fought back. In a real battle, modules won't just stop fighting randomly, but for the purpose of this tutorial, your Core will stop attacking the enemy Core.";
                    btnnext.Show();
                    ThisPlayerPC.Enemies.Clear();
                    break;
                case 11:
                    lbtutorial.Text = "Should your Core fall to a fatal state in the future, you can deploy some defenses to slow the enemy down. Any hacker knows it's best to disable defenses before attacking the main target.";
                    ThisPlayerPC.HP = 5;
                    break;
                case 12:
                    lbtutorial.Text = "We will look at ways of healing damaged modules now. You can add new modules to the network by clicking the [Add Module] button in the lower-left corner.";
                    btnaddmodule.Show();
                    btnnext.Hide();
                    break;
                case 13:
                    btnbuy.Hide();
                    lbtutorial.Text = "You can select a module from the list of hostnames. Only modules that are not powered on will display in the menu.";
                    btnnext.Show();
                    BuyableModules = new List<FutureModule>();
                    BuyableModules.Add(new FutureModule("Antivirus", 0, "This module will heal any other module within it's area of effect to 10 HP. Higher grades can improve it's area of effect.", SystemType.Antivirus));
                    break;
                case 14:
                    btnbuy.Show();
                    lbtutorial.Text = "In this demonstration, you have no other modules to deploy. You will need to buy some modules to get started. Click [Buy New Module] to continue.";
                    btnnext.Hide();
                    break;
                case 15:
                    cmbbuyable.Enabled = false;
                    lbtutorial.Text = "Let's go over the user interface, shall we? At the top is a list of all possible module types.";
                    btnnext.Show();
                    btndonebuying.Hide();
                    lbgrade.Hide();
                    txtgrade.Hide();
                    txthostname.Hide();
                    lbhostname.Hide();
                    lbmoduleinfo.Hide();
                    break;
                case 16:
                    lbtutorial.Text = "Below that, is the cost and description of the selected module type. It's invisible right now, but when you select a new module, it will populate.";
                    lbmoduleinfo.Show();
                    break;
                case 17:
                    cmbbuyable.Enabled = true;
                    lbtutorial.Text = "We need an Antivirus module, so go ahead and select it from the menu.";
                    btnnext.Hide();
                    break;
                case 18:
                    lbtutorial.Text = "Normally, an Antivirus would cost us 15 Codepoints, however since this is a tutorial, it is free.";
                    btnnext.Show();
                    break;
                case 19:
                    lbtutorial.Text = "Below the description is the Hostname box. A Hostname is used to identify the new module. Pick something you'll remember as this new Antivirus, then click [Buy] to purchase it.";
                    btnnext.Hide();
                    lbhostname.Show();
                    txthostname.Show();
                    btndonebuying.Show();
                    break;
                case 20:
                    btnnext.Hide();
                    lbtutorial.Text = "Now that we have an antivirus module, go ahead and deploy it by selecting [Add Module], choosing the hostname you entered, and clicking [Done].";
                    break;
                case 21:
                    btnnext.Hide();
                    lbtutorial.Text = "Alrighty. Now you get to choose where you place your module. Simply left-click in a blank area where you'd like to place the module. Right-click to cancel. Oh, be sure to place close to our Core!";
                    break;
                case 22:
                    lbtutorial.Text = "The Antivirus has been placed. You may've noticed that white dotted box around it when you placed it. If the Core is even slightly within the box, the Antivirus will heal it back to 10 HP.";
                    btnnext.Show();
                    break;
                case 23:
                    lbtutorial.Text = "The higher the Antivirus' grade, the bigger this area of effect becomes. However, it will always only be able to heal modules to 10 HP.";
                    break;
                case 24:
                    lbtutorial.Text = "A Turret has been added to the list of buyable modules. Go pick one up!";
                    btnnext.Hide();
                    BuyableModules.Clear();
                    BuyableModules.Add(new FutureModule("Turret", 0, "It's not super-effective, but the Turret will blast through any Grade 1 defenses pretty quickly. The higher the grade, the higher the strength.", SystemType.Turret));
                    break;
                case 25:
                    lbtutorial.Text = "Turrets can fire damaging viruses at their targets. Go ahead and place your Turret somewhere within the Antivirus's Area of Effect.";
                    break;
                case 26:
                    lbtutorial.Text = "Notice how the Turret only has 10 HP, just like the Antivirus?";
                    btnnext.Show();
                    break;
                case 27:
                    lbtutorial.Text = "This is because both the Antivirus and Turret are at Grade 1. There are four grades of modules you can get, and you can easily upgrade by selecting a module and choosing [Upgrade This Module].";
                    break;
                case 28:
                    lbtutorial.Text = "Upgrading a module will increase it's max HP. Grade 1 is 10 HP, Grade 2 is 20 HP, Grade 3 is 40 HP, and Grade 4 is 80 HP.";
                    break;
                case 29:
                    lbtutorial.Text = "Some modules will have a higher attack rate based on their grade. Others may throw more damaging attacks, and any module's Area of Effect (if it has one) will grow.";
                    break;
                case 30:
                    lbtutorial.Text = "Another tip: Multiple modules can target the same module, and a module can have multiple targets. This means that you can have your Core and your Turret both attack the enemy Core.";
                    break;
                case 31:
                    lbtutorial.Text = "Some modules do not work on a target-based system. Some may work using an area-of-effect system (like an Antivirus), and some may target the entire enemy network.";
                    break;
                case 32:
                    pnltutorial.Left = this.Width - pnltutorial.Width;
                    pnltutorial.Top = this.Height - flcontrols.Height - pnltutorial.Height;
                    lbtutorial.Text = "We have reset both your Cores' health. Go ahead and finish off the enemy Core using your newfound skills.";
                    ThisPlayerPC.HP = 100;
                    ThisEnemyPC.HP = 100;
                    btnnext.Hide();
                    BuyableModules = new List<FutureModule>();
                    BuyableModules.Add(new FutureModule("Antivirus", 0, "This module will heal any other module within it's area of effect to 10 HP. Higher grades can improve it's area of effect.", SystemType.Antivirus));
                    BuyableModules.Add(new FutureModule("Turret", 0, "It's not super-effective, but the Turret will blast through any Grade 1 defenses pretty quickly. The higher the grade, the higher the strength.", SystemType.Turret));
                    break;
                default:
                    btnnext.Show();
                    lbtutorial.Text = "This concludes the Hacker Battle tutorial. Happy hunting, soldier. Just kidding. Stay safe.";
                    btnnext.Text = "Close";
                    break;
            }
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            if (btnnext.Text == "Close")
            {
                UserRequestedClose = false;
                this.Close();
            }
            SetupTutorialUI(TutorialProgress + 1);
        }

        #endregion

        private void HackUI_Load(object sender, EventArgs e)
        {
            this.TopMost = false;
            Audio.Stopped += (o, a) =>
            {
                if (this != null)
                {
                    Audio.Play("hackerbattle_ambient");
                }
            };
            Audio.Play("hackerbattle_ambient");

            Hacking.RepairTimer.Stop(); //Don't want the player to be able to repair dead modules during a battle!
            this.WindowState = FormWindowState.Maximized;
            LoadPlayerScreen();
            if (InOnlineBattle)
                LoadOnlineEnemy();
            else
                LoadEnemyScreen();
            tmrvisualizer.Interval = 10;
            tmrvisualizer.Start();
        }

            #region ENEMY
        private EnemyHacker ThisEnemyHacker { get; set; }
        public List<Computer> AllEnemyComputers = null;
        public Computer ThisEnemyPC { get; set; }
        private void LoadEnemyScreen()
        {
            AllEnemyComputers = new List<Computer>();
            VisualizeEnemyNetwork();
            tmrenemyhealthdetect.Start();
            ThisEnemyPC.Enemy = !InOnlineBattle;
        }

        private void VisualizeEnemyNetwork()
        {
            var rnd = new Random();
            foreach (Module m in ThisEnemyHacker.Network)
            {
                m.HP = m.GetTotalHP();
                var c = m.Deploy();
                if (c.Type == SystemType.Core)
                {
                    ThisEnemyPC = c;
                    ThisEnemyPC.EnemyComputer = ThisPlayerPC;
                    ThisPlayerPC.EnemyComputer = ThisEnemyPC;
                }
                AddEnemyModule(c);
                c.Location = new Point(m.X, m.Y);
            }
        }

        public void Enemy_System_Attacking(object s, EventArgs a)
        {
            if (!InOnlineBattle)
            {
                int i = new Random().Next(AllPlayerComputers.Count);
                var pc = AllPlayerComputers[i];
                var se = (Computer)s;
                pc.LaunchAttack(se.GetProperType());
            }
        }

        public Computer SelectedEnemyComputer = null;

        public void AddEnemyModule(Computer newModule)
        {
            if(newModule.Type == SystemType.Core)
            {
                newModule.Left = (pnlenemy.Width - newModule.Width) / 2;
                newModule.Top = (pnlenemy.Height - newModule.Height) / 2;
            }
            var rnd = new Random();
            pnlenemy.Widgets.Add(newModule);
            foreach (var pc in AllEnemyComputers)
            {
                while (newModule.Bounds.IntersectsWith(pc.Bounds))
                {
                    newModule.Location = new Point(rnd.Next(100, 350), rnd.Next(100, 350));
                }
            }
            AllEnemyComputers.Add(newModule);
            int hp = newModule.HP;
            TotalEnemyHP += (decimal)newModule.HP;
            newModule.Show();

            newModule.StolenModule += (o, a) =>
            {
                var t = new Thread(new ThreadStart(() =>
                {
                    var lst = new List<Computer>();
                    if (!newModule.Enslaved)
                        lst = AllPlayerComputers;
                    else
                        lst = AllEnemyComputers;
                    WriteLine_Enemy($"[{newModule.Hostname}] Starting network hack...");
                    Thread.Sleep(5000);

                    var pc = lst[rnd.Next(0, lst.Count)];
                    this.Invoke(new Action(() =>
                    {
                        if (pc.Type != SystemType.Core)
                        {
                            module_to_steal = pc;

                            pgpong.Left = (this.Width - pgpong.Width) / 2;
                            pgpong.Top = (this.Height - pgpong.Height) / 2;

                            pgpong.Show();
                            newgame();
                        }
                    }));
                }));
                t.Start();
            };
            newModule.EnslavedModule += (o, e) =>
            {
                if (!newModule.Enslaved)
                {
                    var pc = AllPlayerComputers[rand.Next(0, AllPlayerComputers.Count)];
                    if (!pc.Enslaved)
                    {
                        WriteLine_Enemy($"[{newModule.Hostname}] Successfully enslaved {pc.Hostname}");
                        pc.Enslaved = true;
                    }

                }
            };
            newModule.OnDestruction += (object s, EventArgs a) =>
            {
                if (this.SelectedEnemyComputer == newModule)
                {
                    SelectedEnemyComputer = null;
                }
                WriteLine_Enemy($"[Network] {newModule.Hostname} has gone OFFLINE.");
                AllEnemyComputers.Remove(newModule);
                newModule.Dispose();
            };
            newModule.Select += (object s, EventArgs a) =>
            {
                bool c = true;
                foreach (var pc in SelectedPlayerComputer.Enemies)
                {
                    if (pc.Hostname == newModule.Hostname)
                    {
                        c = false;
                    }
                }
                if (c == true)
                {
                    SelectedPlayerComputer.Enemies.Add(newModule);
                    ShowPCInfo(SelectedPlayerComputer.Hostname);
                    if (IsTutorial)
                    {
                        if (TutorialProgress == 9)
                        {
                            SetupTutorialUI(10);
                        }
                    }
                    WriteLine_Enemy("[Network] WARNING! Rival system has targeted a system on this network!");
                    WriteLine($"[Network] System \"{SelectedPlayerComputer.Hostname}\" is now targeting rival system \"{newModule.Hostname}\".");
                }
            };
            newModule.HP_Decreased += new EventHandler(Enemy_System_Damaged);
            newModule.OnRepair += new EventHandler(Enemy_System_Repaired);
            if (newModule.Type == SystemType.Antivirus)
            {
                var b = newModule.GetAreaOfEffect();
                newModule.AntivirusRepair += (object s, EventArgs a) =>
                {
                    foreach (Computer pc in AllEnemyComputers)
                    {
                        if (pc != newModule && pc.Bounds.IntersectsWith(b))
                        {
                            if (newModule.Type == SystemType.RepairModule)
                            {
                                if (pc.HP < newModule.HP)
                                {
                                    if (pc.HP < pc.GetTotal())
                                    {
                                        WriteLine_Enemy($"[{newModule.Hostname}] Repairing neighbouring system \"{pc.Hostname}\"...");
                                        pc.Repair(1);
                                    }
                                }

                            }
                            else
                            {
                                if (pc.HP < 10)
                                {
                                    pc.Repair(1);
                                }
                            }
                        }
                    }
                };
            }
            if (newModule.Type == SystemType.Firewall)
            {
                pnlenemy.Refresh();
                Enemy_Firewall_Deflect(newModule);
            }
            if (newModule.Type == SystemType.ServerStack)
            {
                newModule.MassDDoS += (object s, EventArgs a) =>
                {
                    if (newModule.Enslaved)
                        WormToEnemy();
                    else
                        WormToPlayer();
                };
            }
            newModule.Enemy = !InOnlineBattle;
        }

        public void Enemy_Firewall_Deflect(Computer fwall)
        {
            //Safeguard... also apparently I can't spell... because this used to be 'Safegaurd'...
            if (fwall.Enslaved == false && fwall.Type == SystemType.Firewall)
            {
                var r = fwall.GetAreaOfEffect();
                foreach (var pc in AllEnemyComputers)
                {
                    if (pc != fwall)
                    {
                        if (pc.Bounds.IntersectsWith(r))
                        {
                            pc.DamageDefector = fwall.Grade;
                        }
                    }
                }
            }
        }

        public void Enemy_Firewall_Destroy(Computer fwall)
        {
            //Safegaurd...
            if (fwall.Type == SystemType.Firewall)
            {
                var r = fwall.GetAreaOfEffect();
                foreach (var pc in AllEnemyComputers)
                {
                    if (pc.Bounds.IntersectsWith(r))
                    {
                        pc.DamageDefector = 1;
                        UpdateEnemyFirewalls();
                    }
                }
            }
        }

        public void UpdateEnemyFirewalls()
        {
            foreach (var pc in AllEnemyComputers)
            {
                if (pc.Type == SystemType.Firewall)
                {
                    Enemy_Firewall_Deflect(pc);
                }
            }
        }


        private void Enemy_System_Repaired(object s, EventArgs e)
        {
            var c = (Computer)s;
            WriteLine_Enemy($"[{c.Hostname}] System repaired.");
            lbenemycompromised.Text = "System regenerating...";
            int location = c.Left - (lbenemycompromised.Width / 4);
            int y = c.Top - 25;
            lbenemycompromised.Location = new Point(location, y);
            lbenemycompromised.Show();
            c.Flash(lbenemycompromised);
            _playerTransmitter?.send_message(Online.Hacking.NetTransmitter.Messages.SetHealth, $"{c.Hostname} {c.HP}");
        }


        private void Enemy_System_Damaged(object s, EventArgs e)
        {
            var c = (Computer)s;
            WriteLine_Enemy($"[{c.Hostname}] System damaged. Current HP: {c.HP}");
            WriteLine($"[Network] Damaged system on rival network with hostname \"{c.Hostname}\"");
            lbenemycompromised.Text = "System damaged!";
            int location = c.Left - (lbenemycompromised.Width / 4);
            int y = c.Top - 25;
            lbenemycompromised.Location = new Point(location, y);
            lbenemycompromised.Show();
            c.Flash(lbenemycompromised);
            _playerTransmitter?.send_message(Online.Hacking.NetTransmitter.Messages.SetHealth, $"{c.Hostname} {c.HP}");
        }

        private decimal TotalEnemyHP = 0;

        private void tmrenemyhealthdetect_Tick(object sender, EventArgs e)
        {
            lbsong.Visible = true;
            btntogglemusic.Visible = true;

            lbsong.Text = Audio.Name + " @ " + Audio.CurrentPosition;

            decimal health = 0;
            lbcodepoints.Text = $"Codepoints: {API.Codepoints}";
            var rnd = new Random();
            int chance = 0;
            if (!InOnlineBattle)
            {
                foreach (var pc in AllEnemyComputers)
                {
                    if (pc.Disabled == false)
                    {
                        var elist = new List<Computer>();
                        if (pc.Enslaved)
                            elist = AllEnemyComputers;
                        else
                            elist = AllPlayerComputers;
                        foreach (var enemy in elist)
                        {
                            chance = rnd.Next(1, 20);
                            if (chance == 10)
                            {
                                if (IsTutorial)
                                {
                                    if (TutorialProgress == 9)
                                    {
                                        ThisPlayerPC.LaunchAttack(pc.GetProperType(), pc.GetDamageRate());
                                    }
                                    else if (TutorialProgress == 32)
                                    {
                                        enemy.LaunchAttack(pc.GetProperType(), pc.GetDamageRate());
                                    }
                                    else
                                    {
                                        enemy.Enemies.Clear();
                                    }
                                }
                                else
                                {
                                    enemy.LaunchAttack(pc.GetProperType(), pc.GetDamageRate());
                                }
                            }
                        }
                    }
                }
            }
            foreach (var pc in AllEnemyComputers)
            {
                health += (decimal)pc.HP;
            }
            try
            {
                decimal percent = (health / TotalEnemyHP) * 100;
                lbenemystats.Text = $"Enemy Health: {percent}%";
                if (ThisEnemyPC.HP <= 0)
                {
                    if (IsTutorial)
                    {
                        SetupTutorialUI(33);
                    }
                    else
                    {
                        string message = "You have successfully beaten the enemy hacker.";
                        if (ThisEnemyHacker.IsLeader == false)
                        {
                            switch(rnd.Next(0, 6))
                            {
                                case 1:
                                    API.AddCodepoints(1000);
                                    message = "You have beaten the enemy. You have earned some precious Codepoints for your effort!";
                                    break;
                                case 2:
                                    message = "You have beaten the enemy. As a reward, all Shiftorium Upgrades cost half-price.";
                                    Hacking.GiveHack(Hack.PriceDrop);
                                    break;
                                case 3:
                                    message = "You have beaten the enemy. As a reward, applications will now pay out more Codepoints than usual.";
                                    Hacking.GiveHack(Hack.PayoutIncrease);
                                    break;
                                case 4:
                                    message = "The enemy has recognized your skill and has decided to become a friend. You can now hire them for free as a partner during a system hack.";
                                    //befriend the enemy.
                                    var skill = ThisEnemyHacker.FriendSkill;
                                    var speed = ThisEnemyHacker.FriendSpeed;
                                    var desc = ThisEnemyHacker.FriendDesc;
                                    var name = ThisEnemyHacker.Name;
                                    Hacking.AddCharacter(new Character(name, desc, skill, speed, 0));
                                    break;
                                case 5:
                                    var cats = SaveSystem.ShiftoriumRegistry.GetCategories(false);
                                    string cat = cats[rnd.Next(0, cats.Count - 1)];
                                    if (API.Upgrades[cat] == false)
                                    {
                                        API.Upgrades[cat] = true;
                                        message = $"You have beaten the enemy, and as a result, the {cat.ToUpper()} Shiftorium category has been unlocked.";
                                    }
                                    break;
                                default:
                                    message = "You have successfully beaten the enemy hacker.";
                                    break;
                            }
                        }
                        API.CreateInfoboxSession("You won.", message, infobox.InfoboxMode.Info);

                        UserRequestedClose = false;
                        var h = OnWin;
                        if(h != null)
                        {
                            h(this, new EventArgs());
                        }
                        this.Close();
                    }
                }
            }
            catch
            {

            }
        }

        public void WormToPlayer()
        {
            var rnd = new Random();
            int r = rnd.Next(0, 10);
            WriteLine_Enemy("[NETWORK] Launching distributed denial-of-service attack on rival network...");
            foreach (Computer c in AllPlayerComputers)
            {
                if (r == 5)
                {
                    c.Disable();
                }
            }
        }
        
        public void WriteLine_Enemy(string text)
        {
            try
            {
                if (txtenemyconsole.Text.Length == 0)
                {
                    txtenemyconsole.Text = text + Environment.NewLine;
                }
                else
                {
                    txtenemyconsole.Text += text + Environment.NewLine;
                }
                txtenemyconsole.Select(txtenemyconsole.TextLength, 0);
                txtenemyconsole.ScrollToCaret();
            }
            catch
            {
                this.Invoke(new Action(() => { WriteLine_Enemy(text); }));
            }
        }

        public void WriteLine(string text)
        {
            try
            {
                if (txtyourconsole.Text.Length == 0)
                {
                    txtyourconsole.Text = text + Environment.NewLine;
                }
                else
                {
                    txtyourconsole.Text += text + Environment.NewLine;
                }
                txtyourconsole.Select(txtyourconsole.TextLength, 0);
                txtyourconsole.ScrollToCaret();
            }
            catch
            {
                this.Invoke(new Action(() => { WriteLine(text); }));
            }
        }

        #endregion

        #region ONLINE ENEMY

        public void LoadOnlineEnemy()
        {
            AllEnemyComputers = new List<Computer>();
            tmrenemyhealthdetect.Start();
            //register event handlers
            receiver.ModuleHealthSet += Receiver_ModuleHealthSet;
            receiver.ModulePlaced += Receiver_ModulePlaced;
            receiver.ModuleUpgraded += Receiver_ModuleUpgraded;
            receiver.ModuleRemoved += Receiver_ModuleRemoved;
            receiver.ModuleDisabled += (o, e) =>
            {
                foreach(var c in AllEnemyComputers)
                {
                    if(c.Hostname == e.hostName)
                    {
                        c.Disable();
                    }
                }
            };
            receiver.Won += (o, e) =>
            {
                //the enemy won!
                tmrplayerhealthdetect.Stop();
                tmrenemyhealthdetect.Stop();
                //dispose all the modules.
                while(AllPlayerComputers.Count > 0)
                {
                    AllPlayerComputers[0].Dispose();
                    AllPlayerComputers.RemoveAt(0);
                }
                while (AllEnemyComputers.Count > 0)
                {
                    AllEnemyComputers[0].Dispose();
                    AllEnemyComputers.RemoveAt(0);
                }
                //Destroy server connection.
                Online.Hacking.Matchmaker.DestroySession();
                //Display win message.
                API.CreateInfoboxSession($"{e.Winner.Name} won.", $"{e.Winner.Name} has overthrown your defenses and compromised your system.", infobox.InfoboxMode.Info);
                //Kill the hacker UI.
                UserRequestedClose = false;
                this.Close();
            };
        }

        private void Receiver_ModuleRemoved(object sender, Online.Hacking.Events.ModuleRemoved e)
        {
            Computer c = null;
            foreach(var m in AllEnemyComputers)
            {
                if(m.Hostname == e.new_module)
                {
                    c = m;
                }
            }
            AllEnemyComputers.Remove(c);
            c.Dispose();
        }

        private void Receiver_ModuleUpgraded(object sender, Online.Hacking.Events.ModuleUpgraded e)
        {
            foreach(var m in AllEnemyComputers)
            {
                if (m.Hostname == e.hostname)
                    m.Grade = e.grade;
            }
        }

        private void Receiver_ModulePlaced(object sender, Online.Hacking.Events.ModulePlaced e)
        {
            var newModule = new Module((SystemType)e.new_module.Type, e.new_module.Grade, e.new_module.Hostname);
            newModule.HP = e.new_module.HP;
            newModule.X = e.new_module.X;
            newModule.Y = e.new_module.Y;
            AddEnemyModule(newModule.Deploy());
        }

        private void Receiver_ModuleHealthSet(object sender, Online.Hacking.Events.Health e)
        {
            var mod = new Computer();
            foreach(var m in AllEnemyComputers)
            {
                if (m.Hostname == e.host_name)
                    mod = m;
            }
            int old_hp = mod.HP;
            mod.HP = e.health;
            if(mod.HP > old_hp)
            {
                mod.throw_repaired();
            }
            else
            {
                mod.throw_damaged();
            }
        }

        #endregion

        bool playing = true;

        private void button2_Click(object sender, EventArgs e)
        {
            if (playing == true)
            {
                Audio.ForceStop();
            }
            else
            {
                Audio.Play("hackerbattle_ambient");
            }
            playing = !playing;
        }

        Panel winningPlayfield = null;

        int bgcol = 0;
        int pulse = 0;
        Panel losingPlayfield = null;
        private void tmrvisualizer_Tick(object sender, EventArgs e)
        {
            int enemy_hp = 0;
            int player_hp = 0;
            foreach(var p in AllPlayerComputers)
            {
                player_hp += p.HP;
            }
            foreach (var p in AllEnemyComputers)
            {
                enemy_hp += p.HP;
            }

            if (player_hp >= enemy_hp)
            {
                winningPlayfield = pnlyou;
                losingPlayfield = pnlenemy;
            }
            else
            {
                winningPlayfield = pnlenemy;
                losingPlayfield = pnlyou;
            }

            losingPlayfield.BackColor = Color.Black;


            try
            {
                var visualizer = Audio.GetVisualizer();
                switch(visualizer.type)
                {
                    case VisualizerType.Pulse:
                        tmrvisualizer.Interval = 1;
                        if(pulse == 0)
                        {
                            if(bgcol < 255)
                            {
                                bgcol += 10;
                            }
                            else
                            {
                                pulse = 1;
                            }
                        }
                        else
                        {
                            if(bgcol > 0)
                            {
                                bgcol -= 10;
                            }
                            else
                            {
                                pulse = 0;
                            }
                        }
                        break;
                    case VisualizerType.CalmDown:
                        bgcol = 255 - MathEx.LinearInterpolate(visualizer.startTime * 100, visualizer.endTime * 100, Audio.CurrentPositionMS, 0, 255);
                        break;
                    case VisualizerType.BuildUp:
                        bgcol = MathEx.LinearInterpolate(visualizer.startTime, visualizer.endTime, Audio.CurrentPosition, 0, 255);
                        break;
                }
                Color c = new Color();
                int r = 0;
                int g = 0;
                int b = 0;
                if (visualizer.R)
                    r = bgcol;
                if (visualizer.G)
                    g = bgcol;
                if (visualizer.B)
                    b = bgcol;
                c = Color.FromArgb(r, g, b);
                if(playing)
                    winningPlayfield.BackColor = c;
            }
            catch
            {

            }
        }

        private void pongMain_MouseMove(object sender, ShiftUI.MouseEventArgs e)
        {
            paddleHuman.Location = new Point(paddleHuman.Location.X, (MousePosition.Y - pgpong.Location.Y) - (paddleHuman.Height / 2));
        }


        private void pongGameTimer_Tick(object sender, EventArgs e)
        {
            //Set the computer player to move according to the ball's position.
            if (ball.Location.X > 500 - xVel * 10 && xVel > 0)
            {
                if (ball.Location.Y > paddleComputer.Location.Y + 50)
                {
                    paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y + computerspeed * 2 + ((int)yveldec / 2));
                }
                if (ball.Location.Y < paddleComputer.Location.Y + 50)
                {
                    paddleComputer.Location = new Point(paddleComputer.Location.X, paddleComputer.Location.Y - computerspeed * 2 + ((int)yveldec / 2));
                }
                casualposition = rand.Next(-150, 201);
            }

            //Set Xvel and Yvel speeds from decimal
            if (xVel > 0)
                xVel = (int)Math.Round(xveldec);
            if (xVel < 0)
                xVel = (int)-Math.Round(xveldec);
            if (yVel > 0)
                yVel = (int)Math.Round(yveldec);
            if (yVel < 0)
                yVel = (int)-Math.Round(yveldec);

            // Move the game ball.
            ball.Location = new Point(ball.Location.X + xVel, ball.Location.Y + yVel);

            // Check for top wall.
            if (ball.Location.Y < 0)
            {
                ball.Location = new Point(ball.Location.X, 0);
                yVel = -yVel;
            }

            // Check for bottom wall.
            if (ball.Location.Y > pgpong.Height - ball.Height)
            {
                ball.Location = new Point(ball.Location.X, pgpong.Height - ball.Size.Height);
                yVel = -yVel;
            }

            // Check for player paddle.
            if (ball.Bounds.IntersectsWith(paddleHuman.Bounds))
            {
                ball.Location = new Point(paddleHuman.Location.X + ball.Size.Width, ball.Location.Y);
                //randomly increase x or y speed of ball
                switch (rand.Next(1, 3))
                {
                    case 1:
                        xveldec = xveldec + incrementx;
                        break;
                    case 2:
                        if (yveldec > 0)
                            yveldec = yveldec + incrementy;
                        if (yveldec < 0)
                            yveldec = yveldec - incrementy;
                        break;
                }
                xVel = -xVel;
            }

            // Check for computer paddle.
            if (ball.Bounds.IntersectsWith(paddleComputer.Bounds))
            {
                ball.Location = new Point(paddleComputer.Location.X - paddleComputer.Size.Width + 1, ball.Location.Y);
                xveldec = xveldec + incrementx;
                xVel = -xVel;
            }

            // Check for left wall.
            if (ball.Location.X < -100)
            {
                var m = module_to_steal;
                if (m.Enemy)
                    AllEnemyComputers.Remove(m);

                var lst = new List<Module>();
                if (!m.Enemy)
                    lst = MyNetwork;
                else
                    lst = ThisEnemyHacker.Network;

                string hn = m.Hostname;

                Module mod = null;
                foreach(var pc in lst)
                {
                    if(pc.Hostname == hn)
                    {
                        mod = pc;
                    }
                }

                if (!AllPlayerComputers.Contains(m))
                    AddModule(mod.Deploy());

                if(!Hacking.MyNetwork.Contains(mod))
                {
                    Hacking.MyNetwork.Add(mod);
                }

                if(module_to_steal.Parent != pnlyou)
                {
                    module_to_steal.Parent.Widgets.Remove(module_to_steal);
                    module_to_steal.Dispose();
                }
                tmrcountdown.Stop();
                pongGameTimer.Stop();
                counter.Stop();
                pgpong.Hide();
            }

            // Check for right wall.
            if (ball.Location.X > pgpong.Width - ball.Size.Width - paddleComputer.Width + 100)
            {
                var m = module_to_steal;
                if (!m.Enemy)
                    AllPlayerComputers.Remove(m);

                var lst = new List<Module>();
                if (!m.Enemy)
                    lst = MyNetwork;
                else
                    lst = ThisEnemyHacker.Network;

                string hn = m.Hostname;

                Module mod = null;
                foreach (var pc in lst)
                {
                    if (pc.Hostname == hn)
                    {
                        mod = pc;
                    }
                }

                if (!AllEnemyComputers.Contains(m))
                    AddEnemyModule(mod.Deploy());

                if (!ThisEnemyHacker.Network.Contains(mod))
                {
                    ThisEnemyHacker.Network.Add(mod);
                }

                if (module_to_steal.Parent != pnlenemy)
                {
                    module_to_steal.Parent.Widgets.Remove(module_to_steal);
                    module_to_steal.Dispose();
                }
                tmrcountdown.Stop();
                pongGameTimer.Stop();
                counter.Stop();
                pgpong.Hide();
            }

            //lblstats.Text = "Xspeed: " & Math.Abs(xVel) & " Yspeed: " & Math.Abs(yVel) & " Human Location: " & paddleHuman.Location.ToString & " Computer Location: " & paddleComputer.Location.ToString & Environment.NewLine & " Ball Location: " & ball.Location.ToString & " Xdec: " & xveldec & " Ydec: " & yveldec & " Xinc: " & incrementx & " Yinc: " & incrementy
            lblstatsX.Text = "Xspeed: " + xveldec;
            lblstatsY.Text = "Yspeed: " + yveldec;

            lbllevelandtime.Text = "Level: " + level;

            if (xVel > 20 || xVel < -20)
            {
                paddleHuman.Width = Math.Abs(xVel);
                paddleComputer.Width = Math.Abs(xVel);
            }
            else
            {
                paddleHuman.Width = 20;
                paddleComputer.Width = 20;
            }

            computerspeed = Math.Abs(yVel);

            //  pgcontents.Refresh()
            // pgcontents.CreateGraphics.FillRectangle(Brushes.Black, ball.Location.X, ball.Location.Y, ball.Width, ball.Height)

        }

        #region pong visualizer variables

        Random rand = new Random();
        int xVel = 7;
        int yVel = 8;
        int computerspeed = 8;
        int level = 1;
        int secondsleft = 60;
        int casualposition;
        double xveldec = 3.0;
        double yveldec = 3.0;
        double incrementx = 0.4;
        double incrementy = 0.2;
        int levelxspeed = 3;
        int levelyspeed = 3;
        int[] levelrewards = new int[50];
        int countdown = 3;

        #endregion

        private void counter_Tick(object sender, EventArgs e)
        {
            secondsleft = secondsleft - 1;
            if (secondsleft == -1)
            {
                secondsleft = 60;
                level = level + 1;
            }
        }

        private void tmrcountdown_Tick(object sender, EventArgs e)
        {
            switch (countdown)
            {
                case 0:
                    countdown = 3;
                    lblcountdown.Hide();
                    pongGameTimer.Start();
                    counter.Start();
                    tmrcountdown.Stop();
                    break;
                case 1:
                    lblcountdown.Text = "1";
                    countdown = countdown - 1;
                    break;
                case 2:
                    lblcountdown.Text = "2";
                    countdown = countdown - 1;
                    break;
                case 3:
                    lblcountdown.Text = "3";
                    countdown = countdown - 1;
                    lblcountdown.Show();
                    break;
            }

        }

        private void newgame()
        {
            level = 1;
            secondsleft = 60;
            //reset stats text
            lblstatsX.Text = "Xspeed: ";
            lblstatsY.Text = "Yspeed: ";

            levelxspeed = 3;
            levelyspeed = 3;

            incrementx = 0.4;
            incrementy = 0.2;

            xveldec = levelxspeed;
            yveldec = levelyspeed;

            tmrcountdown.Start();
            if (xVel < 0)
                xVel = Math.Abs(xVel);
            lbllevelandtime.Text = "Level: " + level + " - " + secondsleft + " Seconds Left";
        }
    }
}

namespace System
{
    public class MathEx
    {
        public static int LinearInterpolate(int input_start, int input_end, int input, int output_start, int output_end)
        {
            int input_range = input_end - input_start;
            int output_range = output_end - output_start;

            return (input - input_start) * output_range / input_range + output_start;
        }
    }
}