about summary refs log tree commit diff stats
path: root/src/com/fourisland/frigidearth/MapViewGameState.java
blob: 5a13e7ef33f56d7b527f67c6a2d6cbeae9ba127c (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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fourisland.frigidearth;

import com.fourisland.frigidearth.mobs.Mouse;
import com.fourisland.frigidearth.mobs.Rat;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author hatkirby
 */
public class MapViewGameState implements GameState
{
    private final int TILE_WIDTH = 12;
    private final int TILE_HEIGHT = 12;
    private final int MAP_WIDTH = 100;
    private final int MAP_HEIGHT = 100;
    private final int MESSAGE_HEIGHT = 6;
    private final int VIEWPORT_WIDTH = Main.CANVAS_WIDTH / TILE_WIDTH;
    private final int VIEWPORT_HEIGHT = Main.CANVAS_HEIGHT / TILE_HEIGHT - MESSAGE_HEIGHT;
    private final int MAX_ROOM_WIDTH = 13;
    private final int MIN_ROOM_WIDTH = 7;
    private final int MAX_ROOM_HEIGHT = 13;
    private final int MIN_ROOM_HEIGHT = 7;
    private final int MAX_CORRIDOR_LENGTH = 6;
    private final int MIN_CORRIDOR_LENGTH = 2;
    private final int[][] OCTET_MULTIPLIERS = new int[][] {new int[] {1,0,0,-1,-1,0,0,1}, new int[] {0,1,-1,0,0,-1,1,0}, new int[] {0,1,1,0,0,-1,-1,0}, new int[] {1,0,0,1,-1,0,0,-1}};
    private Tile[][] grid;
    private boolean[][] gridLighting;
    private String[] messages = new String[MESSAGE_HEIGHT];
    private List<Room> rooms = new ArrayList<Room>();
    private List<Mob> mobs = new ArrayList<Mob>();
    private int playerx = 4;
    private int playery = 4;
    private int viewportx = 0;
    private int viewporty = 0;
    private int health = 15;
    private int maxHealth = 15;
    private int defense = 0;
    private int keyx;
    private int keyy;
    private boolean haveKey = false;
    private boolean snowGrow = false;
    private int heartbeat = 0;
    
    public MapViewGameState()
    {
        grid = new Tile[MAP_WIDTH][MAP_HEIGHT];
        gridLighting = new boolean[MAP_WIDTH][MAP_HEIGHT];
        
        for (int x=0; x<MAP_WIDTH; x++)
        {
            for (int y=0; y<MAP_HEIGHT; y++)
            {
                if ((x == 0) || (x == MAP_WIDTH-1) || (y == 0) || (y == MAP_HEIGHT-1))
                {
                    grid[x][y] = Tile.StoneWall;
                } else {
                    grid[x][y] = Tile.Unused;
                }
            }
        }
        
        Direction keyRoomDirection = Direction.getRandomDirection();
        Rectangle legalBounds = null;
        switch (keyRoomDirection)
        {
            case North:
                legalBounds = new Rectangle(0, 14, MAP_WIDTH, MAP_HEIGHT-14);
                break;
                
            case East:
                legalBounds = new Rectangle(0, 0, MAP_WIDTH-14, MAP_HEIGHT);
                break;
                
            case South:
                legalBounds = new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT-14);
                break;
                
            case West:
                legalBounds = new Rectangle(14, 0, MAP_WIDTH-14, MAP_HEIGHT);
                break;
        }
        
        makeRoom(MAP_WIDTH/2, MAP_HEIGHT/2, Direction.getRandomDirection(), legalBounds);
        
        int currentFeatures = 1;
        int objects = 300;
        
        for (int countingTries = 0; countingTries < 1000; countingTries++)
        {
            if (currentFeatures == objects)
            {
                break;
            }
            
            int newx = 0;
            int xmod = 0;
            int newy = 0;
            int ymod = 0;
            Direction validTile = null;
            for (int testing = 0; testing < 1000; testing++)
            {
                newx = Functions.random(1, MAP_WIDTH-1);
                newy = Functions.random(1, MAP_HEIGHT-1);
                validTile = null;
                
                if ((grid[newx][newy] == Tile.DirtWall) || (grid[newx][newy] == Tile.Corridor))
                {
                    if ((grid[newx][newy+1] == Tile.DirtFloor) || (grid[newx][newy+1] == Tile.Corridor))
                    {
                        validTile = Direction.North;
                        xmod = 0;
                        ymod = -1;
                    } else if ((grid[newx-1][newy] == Tile.DirtFloor) || (grid[newx-1][newy] == Tile.Corridor))
                    {
                        validTile = Direction.East;
                        xmod = 1;
                        ymod = 0;
                    } else if ((grid[newx][newy-1] == Tile.DirtFloor) || (grid[newx][newy-1] == Tile.Corridor))
                    {
                        validTile = Direction.South;
                        xmod = 0;
                        ymod = 1;
                    } else if ((grid[newx+1][newy] == Tile.DirtFloor) || (grid[newx+1][newy] == Tile.Corridor))
                    {
                        validTile = Direction.West;
                        xmod = -1;
                        ymod = 0;
                    }
                    
                    if (validTile != null)
                    {
                        if (grid[newx][newy+1] == Tile.Door)
                        {
                            validTile = null;
                        } else if (grid[newx-1][newy] == Tile.Door)
                        {
                            validTile = null;
                        } else if (grid[newx][newy-1] == Tile.Door)
                        {
                            validTile = null;
                        } else if (grid[newx+1][newy] == Tile.Door)
                        {
                            validTile = null;
                        }
                    }
                    
                    if (validTile != null)
                    {
                        break;
                    }
                }
            }
            
            if (validTile != null)
            {
                if (Functions.random(0, 100) <= 75)
                {
                    if (makeRoom(newx+xmod, newy+ymod, validTile, legalBounds))
                    {
                        currentFeatures++;
                        grid[newx][newy] = Tile.Door;
                        grid[newx+xmod][newy+ymod] = Tile.DirtFloor;
                    }
                } else {
                    if (makeCorridor(newx+xmod, newy+ymod, validTile))
                    {
                        currentFeatures++;
                        grid[newx][newy] = Tile.Door;
                    }
                }
            }
        }
        
        int newx = 0;
        int newy = 0;
        int ways = 0;
        int state = 0;
        while (state != 10)
        {
            for (int testing = 0; testing < 1000; testing++)
            {
                newx = Functions.random(1, MAP_WIDTH-1);
                newy = Functions.random(1, MAP_HEIGHT-2);
                ways = 4;
                
                for (Direction dir : Direction.values())
                {
                    Point to = dir.to(new Point(newx, newy));
                    
                    if ((isValidPosition(to.x, to.y)) && (grid[to.x][to.y] == Tile.DirtFloor) || (grid[to.x][to.y] == Tile.Corridor))
                    {
                        ways--;
                    }
                }
                
                if (state == 0)
                {
                    if (ways == 0)
                    {
                        grid[newx][newy] = Tile.UpStairs;
                        state = 1;
                        break;
                    }
                } else if (state == 1)
                {
                    if (ways == 0)
                    {
                        grid[newx][newy] = Tile.DownStairs;
                        playerx=newx;
                        playery=newy;
                        state = 10;
                        break;
                    }
                }
            }
        }
        
        // Create key room
        Room oRoom = null;
        for (Room r : rooms)
        {
            if (oRoom == null)
            {
                oRoom = r;
            } else if ((keyRoomDirection == Direction.North) && (r.getY() < oRoom.getY()))
            {
                oRoom = r;
            } else if ((keyRoomDirection == Direction.East) && (r.getX() > oRoom.getX()))
            {
                oRoom = r;
            } else if ((keyRoomDirection == Direction.South) && (r.getY() > oRoom.getY()))
            {
                oRoom = r;
            } else if ((keyRoomDirection == Direction.West) && (r.getX() < oRoom.getX()))
            {
                oRoom = r;
            }
        }
        
        Room room = null;
        switch (keyRoomDirection)
        {
            case North:
                room = new Room(oRoom.getX()+oRoom.getWidth()/2-3, oRoom.getY()-13, 7, 13, false);
                break;
                
            case East:
                room = new Room(oRoom.getX()+oRoom.getWidth(), oRoom.getY()+oRoom.getHeight()/2-3, 13, 7, false);
                break;
                
            case South:
                room = new Room(oRoom.getX()+oRoom.getWidth()/2-3, oRoom.getY()+oRoom.getHeight(), 7, 13, false);
                break;
                
            case West:
                room = new Room(oRoom.getX()-13, oRoom.getY()+oRoom.getHeight()/2-3, 13, 7, false);
                break;
        }
        
        for (int ytemp=room.getY(); ytemp < room.getY()+room.getHeight(); ytemp++)
        {
            for (int xtemp=room.getX(); xtemp < room.getX()+room.getWidth(); xtemp++)
            {
                if (xtemp == room.getX())
                {
                    if (((keyRoomDirection == Direction.North) || (keyRoomDirection == Direction.South)) && (ytemp % 2 == 0) && (ytemp != room.getY()))
                    {
                        grid[xtemp][ytemp] = Tile.Window;
                    } else {
                        grid[xtemp][ytemp] = Tile.DirtWall;
                    }
                } else if (xtemp == room.getX()+room.getWidth()-1)
                {
                    if (((keyRoomDirection == Direction.North) || (keyRoomDirection == Direction.South)) && (ytemp % 2 == 0) && (ytemp != (room.getY()+room.getHeight())))
                    {
                        grid[xtemp][ytemp] = Tile.Window;
                    } else {
                        grid[xtemp][ytemp] = Tile.DirtWall;
                    }
                } else if (ytemp == room.getY())
                {
                    if (((keyRoomDirection == Direction.West) || (keyRoomDirection == Direction.East)) && (xtemp % 2 == 0) && (xtemp != room.getX()))
                    {
                        grid[xtemp][ytemp] = Tile.Window;
                    } else {
                        grid[xtemp][ytemp] = Tile.DirtWall;
                    }
                } else if (ytemp == room.getY()+room.getHeight()-1)
                {
                    if (((keyRoomDirection == Direction.West) || (keyRoomDirection == Direction.East)) && (xtemp % 2 == 0) && (xtemp != (room.getX()+room.getWidth())))
                    {
                        grid[xtemp][ytemp] = Tile.Window;
                    } else {
                        grid[xtemp][ytemp] = Tile.DirtWall;
                    }
                } else {
                    grid[xtemp][ytemp] = Tile.DirtFloor;
                }
            }
        }
        
        switch (keyRoomDirection)
        {
            case North:
                grid[room.getX()+room.getWidth()/2][room.getY()+room.getHeight()] = Tile.Door;
                grid[room.getX()+room.getWidth()/2][room.getY()+room.getHeight()-1] = Tile.DirtFloor;
                keyx = room.getX()+3;
                keyy = room.getY()+3;
                break;
                
            case East:
                grid[room.getX()-1][room.getY()+room.getHeight()/2] = Tile.Door;
                grid[room.getX()][room.getY()+room.getHeight()/2] = Tile.DirtFloor;
                keyx = room.getX()+10;
                keyy = room.getY()+3;
                break;
                
            case South:
                grid[room.getX()+room.getWidth()/2][room.getY()-1] = Tile.Door;
                grid[room.getX()+room.getWidth()/2][room.getY()] = Tile.DirtFloor;
                keyx = room.getX()+3;
                keyy = room.getY()+10;
                break;
                
            case West:
                grid[room.getX()+room.getWidth()][room.getY()+room.getHeight()/2] = Tile.Door;
                grid[room.getX()+room.getWidth()-1][room.getY()+room.getHeight()/2] = Tile.DirtFloor;
                keyx = room.getX()+3;
                keyy = room.getY()+3;
                break;
        }
        
        rooms.add(room);
        
        adjustViewport();
        calculateFieldOfView();
    }
    
    private boolean makeRoom(int x, int y, Direction direction, Rectangle legalBounds)
    {
        int width = Functions.random(MIN_ROOM_WIDTH, MAX_ROOM_WIDTH);
        int height = Functions.random(MIN_ROOM_HEIGHT, MAX_ROOM_HEIGHT);
        Tile floor = Tile.DirtFloor;
        Tile wall = Tile.DirtWall;
        Room room = null;
        Rectangle bounds = null;
        
        if (legalBounds == null)
        {
            bounds = new Rectangle(0, 0, MAP_WIDTH, MAP_HEIGHT);
        } else {
            bounds = legalBounds;
        }
        
        switch (direction)
        {
            case North:
                room = new Room(x-width/2, y-height, width+1, height+1, true);
                
                break;
                
            case East:
                room = new Room(x, y-height/2, width+1, height+1, true);
                
                break;
                
            case South:
                room = new Room(x-width/2, y, width+1, height+1, true);
                
                break;
                
            case West:
                room = new Room(x-width, y-height/2, width+1, height+1, true);
                
                break;
        }
        
        for (int ytemp=room.getY(); ytemp < room.getY()+room.getHeight(); ytemp++)
        {
            if ((ytemp < bounds.y) || (ytemp > bounds.y+bounds.height ))
            {
                return false;
            }

            for (int xtemp=room.getX(); xtemp < room.getX()+room.getWidth(); xtemp++)
            {
                if ((xtemp < bounds.x) || (xtemp > bounds.x+bounds.width))
                {
                    return false;
                }

                if (grid[xtemp][ytemp] != Tile.Unused)
                {
                    return false;
                }
            }
        }

        for (int ytemp=room.getY(); ytemp < room.getY()+room.getHeight(); ytemp++)
        {
            for (int xtemp=room.getX(); xtemp < room.getX()+room.getWidth(); xtemp++)
            {
                if (xtemp == room.getX())
                {
                    grid[xtemp][ytemp] = wall;
                } else if (xtemp == room.getX()+room.getWidth()-1)
                {
                    grid[xtemp][ytemp] = wall;
                } else if (ytemp == room.getY())
                {
                    grid[xtemp][ytemp] = wall;
                } else if (ytemp == room.getY()+room.getHeight()-1)
                {
                    grid[xtemp][ytemp] = wall;
                } else {
                    grid[xtemp][ytemp] = floor;
                }
            }
        }
        
        rooms.add(room);
        
        // Place mice in random rooms because yolo
        int random = Functions.random(0,100);
        if (random < 25)
        {
            Mob mob = new Mouse(Functions.random(room.getX()+1, room.getX()+room.getWidth()-2), Functions.random(room.getY()+1, room.getY()+room.getHeight()-2));
            mobs.add(mob);
        } else if (random < 50)
        {
            Mob mob = new Rat(Functions.random(room.getX()+1, room.getX()+room.getWidth()-2), Functions.random(room.getY()+1, room.getY()+room.getHeight()-2));
            mobs.add(mob);
        }
        
        return true;
    }
    
    private boolean makeCorridor(int x, int y, Direction direction)
    {
        int length = Functions.random(MIN_CORRIDOR_LENGTH, MAX_CORRIDOR_LENGTH);
        Tile floor = Tile.Corridor;
        
        int xtemp = 0;
        int ytemp = 0;
        
        switch (direction)
        {
            case North:
                if ((x < 0) || (x > MAP_WIDTH))
                {
                    return false;
                } else {
                    xtemp = x;
                }
                
                for (ytemp = y; ytemp > (y-length); ytemp--)
                {
                    if ((ytemp < 0) || (ytemp > MAP_HEIGHT))
                    {
                        return false;
                    }
                    
                    if (grid[xtemp][ytemp] != Tile.Unused)
                    {
                        return false;
                    }
                }
                
                for (ytemp = y; ytemp > (y-length); ytemp--)
                {
                    grid[xtemp][ytemp] = floor;
                }
                
                break;
                
            case East:
                if ((y < 0) || (y > MAP_HEIGHT))
                {
                    return false;
                } else {
                    ytemp = y;
                }
                
                for (xtemp = x; xtemp < (x+length); xtemp++)
                {
                    if ((xtemp < 0) || (xtemp > MAP_WIDTH))
                    {
                        return false;
                    }
                    
                    if (grid[xtemp][ytemp] != Tile.Unused)
                    {
                        return false;
                    }
                }
                
                for (xtemp = x; xtemp < (x+length); xtemp++)
                {
                    grid[xtemp][ytemp] = floor;
                }
                
                break;
                
            case South:
                if ((x < 0) || (x > MAP_WIDTH))
                {
                    return false;
                } else {
                    xtemp = x;
                }
                
                for (ytemp = y; ytemp < (y+length); ytemp++)
                {
                    if ((ytemp < 0) || (ytemp > MAP_HEIGHT))
                    {
                        return false;
                    }
                    
                    if (grid[xtemp][ytemp] != Tile.Unused)
                    {
                        return false;
                    }
                }
                
                for (ytemp = y; ytemp < (y+length); ytemp++)
                {
                    grid[xtemp][ytemp] = floor;
                }
                
                break;
                
            case West:
                if ((y < 0) || (y > MAP_HEIGHT))
                {
                    return false;
                } else {
                    ytemp = y;
                }
                
                for (xtemp = x; xtemp > (x-length); xtemp--)
                {
                    if ((xtemp < 0) || (xtemp > MAP_WIDTH))
                    {
                        return false;
                    }
                    
                    if (grid[xtemp][ytemp] != Tile.Unused)
                    {
                        return false;
                    }
                }
                
                for (xtemp = x; xtemp > (x-length); xtemp--)
                {
                    grid[xtemp][ytemp] = floor;
                }
                
                break;
        }
        
        return true;
    }
    
    private void calculateFieldOfView()
    {
        for (int x=0; x<MAP_WIDTH; x++)
        {
            for (int y=0; y<MAP_HEIGHT; y++)
            {
                gridLighting[x][y] = false;
            }
        }
        
        for (int i=0; i<8; i++)
        {
            castLight(playerx, playery, 1, 1.0, 0.0, Math.max(VIEWPORT_WIDTH/2, VIEWPORT_HEIGHT/2), OCTET_MULTIPLIERS[0][i], OCTET_MULTIPLIERS[1][i], OCTET_MULTIPLIERS[2][i], OCTET_MULTIPLIERS[3][i], 0);
        }
    }
    
    private void castLight(int cx, int cy, int row, double start, double end, int radius, int xx, int xy, int yx, int yy, int id)
    {
        if (start < end)
        {
            return;
        }
        
        int r2 = radius * radius;
        for (int j=row; j<radius+1; j++)
        {
            int dx = -j-1;
            int dy = -j;
            boolean blocked = false;
            double newStart = 0.0;
            
            while (dx <= 0)
            {
                dx++;
                
                int x = cx + dx*xx + dy*xy;
                int y = cy + dx*yx + dy*yy;
                double l_slope = ((double)dx-0.5)/((double)dy+0.5);
                double r_slope = ((double)dx+0.5)/((double)dy-0.5);
                
                if (start < r_slope)
                {
                    continue;
                } else if (end > l_slope)
                {
                    break;
                } else {
                    if ((dx*dx + dy*dy) < r2)
                    {
                        gridLighting[x][y] = true;
                    }
                    
                    if (blocked)
                    {
                        if (grid[x][y].isBlocked())
                        {
                            newStart = r_slope;
                            continue;
                        } else {
                            blocked = false;
                            start = newStart;
                        }
                    } else {
                        if ((grid[x][y].isBlocked()) && (j < radius))
                        {
                            blocked = true;
                            castLight(cx, cy, j+1, start, l_slope, radius, xx, xy, yx, yy, id+1);
                            newStart = r_slope;
                        }
                    }
                }
            }
            
            if (blocked)
            {
                break;
            }
        }
    }

    public void render(Graphics2D g)
    {
        // Render tiles
        for (int x=viewportx; x<viewportx+VIEWPORT_WIDTH; x++)
        {
            for (int y=viewporty; y<viewporty+VIEWPORT_HEIGHT; y++)
            {
                if (gridLighting[x][y])
                {
                    char displayChar = grid[x][y].getDisplayCharacter();
                    Color displayColor = grid[x][y].getBackgroundColor();

                    if (!displayColor.equals(Color.BLACK))
                    {
                        g.setColor(displayColor);
                        g.fillRect((x-viewportx)*TILE_WIDTH, (y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
                    }

                    if (displayChar != ' ')
                    {
                        g.drawImage(SystemFont.getCharacter(grid[x][y].getDisplayCharacter(), Color.WHITE), (x-viewportx)*TILE_WIDTH, (y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
                    }
                }
            }
        }
        
        // Render mobs
        for (Mob mob : mobs)
        {
            if ((gridLighting[mob.x][mob.y]) && (isInViewport(mob.x, mob.y)))
            {
                g.drawImage(SystemFont.getCharacter(mob.getDisplayCharacter(), mob.getDisplayColor()), (mob.x-viewportx)*TILE_WIDTH, (mob.y-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
            }
        }
        
        // Render key
        if ((!haveKey) && (gridLighting[keyx][keyy]))
        {
            g.drawImage(SystemFont.getCharacter('k', Color.YELLOW), (keyx-viewportx)*TILE_WIDTH, (keyy-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
        }
        
        // Render player
        g.drawImage(SystemFont.getCharacter('@', Color.WHITE), (playerx-viewportx)*TILE_WIDTH, (playery-viewporty)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
        
        // Render messages
        g.setColor(new Color(53, 63, 62));
        g.fillRect(0, VIEWPORT_HEIGHT*TILE_HEIGHT, Main.CANVAS_WIDTH, TILE_HEIGHT*MESSAGE_HEIGHT);
        
        for (int i=0; i<MESSAGE_HEIGHT; i++)
        {
            if (messages[i] != null)
            {
                for (int j=0; j<messages[i].length(); j++)
                {
                    g.drawImage(SystemFont.getCharacter(messages[i].charAt(j), Color.WHITE), j*TILE_WIDTH, (VIEWPORT_HEIGHT+i)*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, null);
                }
            }
        }
        
        // Render status bar
        g.drawImage(SystemFont.getCharacter((char) 3, Color.RED), TILE_WIDTH, 0, TILE_WIDTH, TILE_HEIGHT, null);
        String healthText = Integer.toString(health);
        double healthPercentage = ((double) health) / ((double) maxHealth);
        Color healthColor = Color.WHITE;
        if (healthPercentage < 0.2)
        {
            healthColor = Color.RED;
        } else if (healthPercentage < 0.55)
        {
            healthColor = Color.YELLOW;
        } else if (healthPercentage < 1)
        {
            healthColor = Color.GREEN;
        }
        
        for (int i=0; i<healthText.length(); i++)
        {
            g.drawImage(SystemFont.getCharacter(healthText.charAt(i), healthColor), (i+2)*TILE_WIDTH, 0, TILE_WIDTH, TILE_HEIGHT, null);
        }
        
        g.drawImage(SystemFont.getCharacter((char) 5, Color.GRAY), (healthText.length()+3)*TILE_WIDTH, 0, TILE_WIDTH, TILE_HEIGHT, null);
        int b = healthText.length()+4;
        String defenseText = Integer.toBinaryString(defense);
        for (int i=0; i<defenseText.length(); i++)
        {
            g.drawImage(SystemFont.getCharacter(defenseText.charAt(i), Color.WHITE), (i+b)*TILE_WIDTH, 0, TILE_WIDTH, TILE_HEIGHT, null);
        }
    }
    
    public void processInput(KeyEvent e)
    {
        switch (e.getKeyCode())
        {
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_RIGHT:
            case KeyEvent.VK_UP:
            case KeyEvent.VK_DOWN:
                Direction dir = Direction.fromKeyEvent(e);
                Point to = dir.to(new Point(playerx, playery));
                
                if ((isValidPosition(to.x,to.y)) && (!grid[to.x][to.y].isBlocked()))
                {
                    // Check for mobs
                    boolean foundMob = false;
                    for (Mob mob : mobs)
                    {
                        if (mob.getPosition().equals(to))
                        {
                            printMessage("You hit the " + mob.getName().toLowerCase());
                            mob.health--;
                            
                            if (mob.health <= 0)
                            {
                                printMessage("You killed the " + mob.getName().toLowerCase() + "!");
                                mobs.remove(mob);
                            }
                            
                            foundMob = true;
                            break;
                        }
                    }
                    
                    if (!foundMob)
                    {
                        playerx = to.x;
                        playery = to.y;
                    }
                } else {
                    printMessage("Blocked: " + dir.name());
                    
                    return;
                }
                
                break;
                
            case KeyEvent.VK_G:
                if ((playerx == keyx) && (playery == keyy) && (!haveKey))
                {
                    haveKey = true;
                    printMessage("You get the key");
                    printMessage("All the windows in the room shatter!");
                    
                    for (int x=0; x<MAP_WIDTH; x++)
                    {
                        for (int y=0; y<MAP_HEIGHT; y++)
                        {
                            if (grid[x][y] == Tile.Window)
                            {
                                grid[x][y] = Tile.ShatteredWindow;
                            }
                        }
                    }
                }
                
                break;
                
            default:
                return;
        }
        
        // Move mobs
        for (Mob mob : mobs)
        {
            // If the mob is hostile, it should move toward the player IF IT CAN SEE the player
            // Also, if it is adjacent to the player, it should attack
            if ((mob.hostile) && (canSeePlayer(mob.x, mob.y)))
            {
                if (arePointsAdjacent(playerx, playery, mob.x, mob.y))
                {
                    // Attack!
                    health -= (mob.power - defense);
                    printMessage(mob.getBattleMessage());
                } else {
                    List<Direction> path = findPath(mob.getPosition(), new Point(playerx, playery));
                    
                    if (path != null)
                    {
                        mob.moveInDirection(path.get(0));
                    }
                }
            } else {
                // If the mob isn't hostile, it should just move around randomly
                Direction toDir = null;

                for (int i=0; i<10; i++)
                {
                    toDir = Direction.getRandomDirection();
                    Point to = toDir.to(mob.getPosition());
                    if ((isValidPosition(to.x,to.y)) && (!grid[to.x][to.y].isBlocked()) && (!to.equals(new Point(playerx, playery))))
                    {
                        mob.moveInDirection(toDir);
                        break;
                    }
                }
            }
        }
        
        // Move snow
        if (snowGrow)
        {
            for (int x=0; x<MAP_WIDTH; x++)
            {
                for (int y=0; y<MAP_HEIGHT; y++)
                {
                    if (grid[x][y] == Tile.Snow)
                    {
                        for (Direction d : Direction.values())
                        {
                            Point to = d.to(new Point(x, y));
                            if ((!grid[to.x][to.y].isBlocked()) && (grid[to.x][to.y] != Tile.Snow) && (grid[to.x][to.y] != Tile.UpStairs))
                            {
                                grid[to.x][to.y] = Tile.SnowTemp;
                            }
                        }
                    }
                }
            }

            for (int x=0; x<MAP_WIDTH; x++)
            {
                for (int y=0; y<MAP_HEIGHT; y++)
                {
                    if (grid[x][y] == Tile.ShatteredWindow)
                    {
                        for (Direction d : Direction.values())
                        {
                            Point to = d.to(new Point(x, y));
                            if ((!grid[to.x][to.y].isBlocked()) && (grid[to.x][to.y] != Tile.Snow))
                            {
                                grid[to.x][to.y] = Tile.Snow;
                            }
                        }
                    } else if (grid[x][y] == Tile.SnowTemp)
                    {
                        grid[x][y] = Tile.Snow;
                    }
                }
            }
        }
        
        snowGrow = !snowGrow;
        
        // Heartbeat
        if (heartbeat % 2 == 0)
        {
            if (grid[playerx][playery] == Tile.Snow)
            {
                health--;
            } else if (heartbeat == Functions.random(0, 3)) {
                if (health < maxHealth)
                {
                    health++;
                }
            }
        }
        
        heartbeat++;
        if (heartbeat == 4) heartbeat = 0;
        
        adjustViewport();
        calculateFieldOfView();
        
        if (health <= 0)
        {
            printMessage("You have died! Press [ENTER] to quit.");
            
            Main.addInputable(new Inputable() {
                @Override
                public void processInput(KeyEvent e)
                {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER)
                    {
                        System.exit(0);
                    }
                }
            });
        }
    }
    
    private void adjustViewport()
    {
        if (playerx > (VIEWPORT_WIDTH/2))
        {
            if (playerx < (MAP_WIDTH - (VIEWPORT_WIDTH/2-1)))
            {
                viewportx = playerx - (VIEWPORT_WIDTH/2);
            } else {
                viewportx = MAP_WIDTH - VIEWPORT_WIDTH;
            }
        } else {
            viewportx = 0;
        }
        
        if (playery > (VIEWPORT_HEIGHT/2))
        {
            if (playery < (MAP_HEIGHT - (VIEWPORT_HEIGHT/2-1)))
            {
                viewporty = playery - (VIEWPORT_HEIGHT/2);
            } else {
                viewporty = MAP_HEIGHT - VIEWPORT_HEIGHT;
            }
        } else {
            viewporty = 0;
        }
    }
    
    private boolean isValidPosition(int x, int y)
    {
        if (x < 0) return false;
        if (x > MAP_WIDTH) return false;
        if (y < 0) return false;
        if (y > MAP_HEIGHT) return false;
        
        return true;
    }
    
    private boolean isInViewport(int x, int y)
    {
        if (x < viewportx) return false;
        if (x > viewportx+VIEWPORT_WIDTH) return false;
        if (y < viewporty) return false;
        if (y > viewporty+VIEWPORT_HEIGHT) return false;
        
        return true;
    }
    
    private void printMessage(String message)
    {
        String temp = message;
        
        while (temp.length() > VIEWPORT_WIDTH)
        {
            String shortm = temp.substring(0, VIEWPORT_WIDTH);
            
            if ((temp.charAt(VIEWPORT_WIDTH) == ' ') || (shortm.endsWith(" ")))
            {
                pushUpMessages(shortm);
                temp = temp.substring(VIEWPORT_WIDTH);
            } else {
                int lastSpace = shortm.lastIndexOf(" ");
                pushUpMessages(shortm.substring(0, lastSpace));
                temp = temp.substring(lastSpace);
            }
        }
        
        pushUpMessages(temp);
    }
    
    private void pushUpMessages(String message)
    {
        for (int i=1; i<MESSAGE_HEIGHT; i++)
        {
            messages[i-1] = messages[i];
        }
        
        messages[MESSAGE_HEIGHT-1] = message;
    }
    
    private boolean canSeePlayer(int mx, int my)
    {
        int dx = playerx - mx;
        int dy = playery - my;
        int ax = Math.abs(dx) << 1;
        int ay = Math.abs(dy) << 1;
        int sx = (int) Math.signum(dx);
        int sy = (int) Math.signum(dy);
        int x = mx;
        int y = my;
        
        if (ax > ay)
        {
            int t = ay - (ax >> 1);
            
            do
            {
                if (t >= 0)
                {
                    y += sy;
                    t -= ax;
                }
                
                x += sx;
                t += ay;
                
                if ((x == playerx) && (y == playery))
                {
                    return true;
                }
            } while (!grid[x][y].isBlocked());
            
            return false;
        } else {
            int t = ax - (ay >> 1);
            
            do
            {
                if (t >= 0)
                {
                    x += sx;
                    t -= ay;
                }
                
                y += sy;
                t += ax;
                
                if ((x == playerx) && (y == playery))
                {
                    return true;
                }
            } while (!grid[x][y].isBlocked());
            
            return false;
        }
    }
    
    private boolean arePointsAdjacent(int px, int py, int mx, int my)
    {
        if (mx == (px-1))
        {
            if (my == (py-1)) return true;
            if (my == py) return true;
            if (my == (py+1)) return true;
        } else if (mx == px)
        {
            if (my == (py-1)) return true;
            if (my == (py+1)) return true;
        } else if (mx == (px+1))
        {
            if (my == (py-1)) return true;
            if (my == py) return true;
            if (my == (py+1)) return true;
        }
        
        return false;
    }
    
    private List<Direction> findPath(Point from, Point to)
    {
        return findPath(from, to, new ArrayList<Point>());
    }
    
    private List<Direction> findPath(Point from, Point to, List<Point> attempts)
    {
        /* Iterate over all of the directions and check if moving in that
         * direction would result in the destination position. If so, the
         * correct path has been acquired and thus we can return. */
        for (Direction d : Direction.values())
        {
            Point loc = d.to(from);
            if (to.equals(loc))
            {
                List<Direction> moves = new ArrayList<Direction>();
                moves.add(d);
                
                return moves;
            }
        }
        
        /* Calculate the directions to attempt and the order in which to do so
         * based on proximity to the destination */
        List<Direction> ds = new ArrayList<Direction>();
        for (Direction d : Direction.values())
        {
            Point loc = d.to(from);
            if ((isValidPosition(loc.x, loc.y)) && (!grid[loc.x][loc.y].isBlocked()))
            {
                ds.add(d);
            }
        }
        
        List<Direction> tempd = new ArrayList<Direction>();
        
        if (to.x < from.x)
        {
            tempd.add(Direction.West);
        } else if (to.x > from.x)
        {
            tempd.add(Direction.East);
        } else {
            if (!ds.contains(Direction.North) || !ds.contains(Direction.South))
            {
                tempd.add(Direction.West);
                tempd.add(Direction.East);
            }
        }
        
        if (to.y < from.y)
        {
            tempd.add(Direction.North);
        } else if (to.y > from.y)
        {
            tempd.add(Direction.South);
        } else {
            if (!ds.contains(Direction.West) || !ds.contains(Direction.East))
            {
                tempd.add(Direction.North);
                tempd.add(Direction.South);
            }
        }
        
        // Remove calculated directions that aren't legal movements
        tempd.retainAll(ds);
        
        // Randomize directions so movement is more fluid
        Collections.shuffle(tempd);
        
        // Iterate over the suggested directions
        for (Direction d : tempd)
        {
            /* If the position in the suggested direction has not already been
             * covered, recursively search from the new position */
            Point loc = d.to(from);
            if (!attempts.contains(loc))
            {
                attempts.add(loc);
                
                List<Direction> moves = findPath(loc, to, attempts);
                if (moves != null)
                {
                    moves.add(0, d);
                    return moves;
                }
            }
        }
        
        return null;
    }
}