Source

Lost Sky Project (Public) / Story of a Lost Sky / lostsky / worldmap / worldmap.py

Full commit
   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
# World Map Class
# Map engine: "Gensokyo Navi"
import pygame
import os
from pygame.locals import *
from lostsky.core.linalg import Vector2
from lostsky.core.utils import draw_aligned_text, split_lines, padlib_rounded_rect, get_ui_panel
from lostsky.core.colors import border_color, panel_color, selected_color
from sys import exit

class Worldmap(object):

    def __init__(self, engine, region_list, region_path_list):
        """
        # Function Name: __init__
        # Purpose: Intializes world map
        # Inputs: Engine - Game system engine
        #         region_list - a list of regions in the world map
        """


        self.engine = engine
        engine.worldmap = self
        self.all_regions = {}
        self.all_regions_by_name = {}
        self.all_locations_by_name = {}
        self.conv_images = {}
        self.region_path_list = region_path_list
        self.background_surface = pygame.surface.Surface((840, 630))

        self.dot_positions = set()
        for region in region_list:
            self.all_regions[tuple(region.wm_coords)] = region
            self.all_regions_by_name[region.name] = region
            region.wm_parent = self
            for location in region.all_locations.values():
                self.all_locations_by_name[location.name] = location

        self.default_starting_pos = tuple(self.all_regions_by_name['Netherworld'].wm_coords)
        self.player_pos = Vector2(self.default_starting_pos)
        self.player_sprite_group = pygame.sprite.RenderUpdates()
        self.player_sprite = WorldmapSprite( 35*self.player_pos, self.engine.player_img)
        self.player_sprite_group.add(self.player_sprite)

        # Sets up connecting paths

        # List of all dots to not include a range around the region
        remove_pts = []
        for region in region_list:
            remove_pts.extend((tuple(region.wm_coords), tuple((region.wm_coords+Vector2(-1, 1))), tuple((region.wm_coords+Vector2(0, 1))), tuple((region.wm_coords+Vector2(0, 1)))))

        for path in region_path_list:

            # Each path has two connecting points along with the direction key to press to get to each location
            direction_to_1 = path.locations.keys()[0]
            region_1 = path.locations[direction_to_1]
            direction_to_2 = path.locations.keys()[1]
            region_2 = path.locations[direction_to_2]

            # We assign the starting point's path directions to this path so it can be called when the character needs to move
            self.all_regions_by_name[region_2].paths[direction_to_1] = path
            self.all_regions_by_name[region_1].paths[direction_to_2] = path

            # Constructs destination vectors
            delta_pos = self.all_regions_by_name[region_1].wm_coords - self.all_regions_by_name[region_2].wm_coords
            path.vectors[direction_to_1] = delta_pos
            path.vectors[direction_to_2] = -1*(delta_pos.copy())

            # # Decomposes delta vector into path dots for the map
            for i in xrange(min(1, int(delta_pos.y)), max(int(delta_pos.y), 0)):
                dot_vector = self.all_regions_by_name[region_2].wm_coords + Vector2(0, i)
                if tuple(dot_vector) not in remove_pts:
                    path.dot_positions.append(tuple(35*dot_vector))

            for j in xrange(min(1, int(delta_pos.x)), max(int(delta_pos.x), 0)):
                dot_vector = self.all_regions_by_name[region_2].wm_coords + Vector2(j, delta_pos.y)
                if tuple(dot_vector) not in remove_pts:
                    path.dot_positions.append(tuple(35*dot_vector))




        # Region connection paths
        self.unit_associate()

    ############################################
    # Computational Methods
    ############################################

    def unit_associate(self):

        """
        # Function Name: unit_associate
        # Purpose: Associates the unit's map attribute with the world map
        """

        for unit in self.engine.player_units:
            unit.map = self
            unit.in_battle = False

    def update_all_events(self):
        """
        # Function Name: unit_associate
        # Purpose: Associates the unit's map attribute with the world map
        # Outputs: new_mission = True/False if a new mission is available
        """

        # Clears out all location event data
        for location in self.all_locations_by_name.values():
            location.all_events = []

        # Clears out the available list
        self.engine.all_events_available = []
        self.engine.all_events_completed = []
        self.engine.all_events_sign_up = []

        # Updates all events
        for event in self.engine.all_events_master.values():

            # If event is done, add it to the list of completed events
            if event.done:
                event.available = False
                event.sign_up = False
                self.engine.all_events_completed.append(event)
                event.location = self.all_locations_by_name[event.location_name]
            else:

                # Updates availability of all events

                # Mission is added if no prerequisites are present or
                # all prerequisites are met
                if ((not event.prereq or self.engine.check_event_completion(event.prereq))
                    and not event.sign_up):

                    event.sign_up = True

                # if the event is signed up for add it to the world map
                if event.sign_up:
                    self.engine.all_events_sign_up.append(event)
                    self.all_locations_by_name[event.location_name].add_event(event)

            # Updates player data
            self.engine.player.all_event_data[event.event_id].update_from_event(event)
            self.update_all_paths()


    def update_all_paths(self):

        """
        # Function Name: update all paths
        # Purpose: Updates the unlocking of all the world map paths
        """

        for path in self.region_path_list:
            # A path is unlocked if there are no prerequisite missions or if all of these missions has been completed
            # The Engine's unlock_wm debug flag unlocks the whole map
            if self.engine.unlock_wm or not path.prereq or self.engine.check_event_completion(path.prereq):
                path.unlock = True
            else:
                path.unlock = False

        for region in self.all_regions.values():
            if self.engine.unlock_wm or not region.prereq or self.engine.check_event_completion(region.prereq):
                region.unlock = True
            else:
                region.unlock = False

            for path in region.location_path_list:
                # A path is unlocked if there are no prerequisite missions or if all of these missions has been completed
                # The Engine's unlock_wm debug flag unlocks the whole map
                if self.engine.unlock_wm or not path.prereq or self.engine.check_event_completion(path.prereq):
                    path.unlock = True
                else:
                    path.unlock = False
            for location in region.all_locations_by_name.values():
                if self.engine.unlock_wm or not location.prereq or self.engine.check_event_completion(location.prereq):
                    location.unlock = True
                else:
                    location.unlock = False



    def update_wm_player_data(self):

        """
        # Function Name: update wm player data
        # Purpose: Updates the location of the player
        """
        self.engine.player.wm_data['wm_coords'] = tuple(self.player_pos)
        self.engine.player.wm_data['in_region'] = False

    ############################################
    # Graphical Methods
    ############################################

    def render_background(self):

        """
        # Function Name: Render map to background surface
        # Purpose: Draws the background map and the locations
        """

        self.background_surface.blit(self.engine.wm_bg, (0, 0))
        self.background_surface.blit(self.engine.menu_board, (0, 490))

        for region in self.all_regions.values():
            draw_bubble = False
            # Checks if there is an event in the region
            for location in region.all_locations.values():
                if location.has_event():
                    draw_bubble = True
                    break

            if not region.unlock:
                self.background_surface.blit(self.engine.hidden_location_images[region.name], region.image_pos)

            elif region == self.all_regions[tuple(self.player_pos)]:
                self.background_surface.blit(self.engine.active_location_images[region.name], region.image_pos)
            else:
                self.background_surface.blit(self.engine.inactive_location_images[region.name], region.image_pos)

            if draw_bubble:
                self.background_surface.blit(self.engine.event_icon, (region.pix_coords+Vector2(25, -35)))


        name_panel = get_ui_panel((280, 45), border_color, panel_color)
        desc_panel = get_ui_panel((320, 60), border_color, panel_color)

        text_location_name = self.all_regions[tuple(self.player_pos)].name_big

        # Draws the description and name of the current region
        self.background_surface.blit(name_panel, (40, 505))
        self.background_surface.blit(text_location_name, (40 + name_panel.get_width()/2 - text_location_name.get_width()/2,
                                                          507 + name_panel.get_height()/2 - text_location_name.get_height()/2))

        self.background_surface.blit(desc_panel, (20, 555))

        self.draw_directions()

        draw_aligned_text(self.background_surface, self.all_regions[tuple(self.player_pos)].desc, self.engine.sfont,
                          (0, 0, 0), (30, 565), desc_panel.get_width()-20)


    def draw_directions(self):
        """
        function name: draw_directions

        Desc: Draw basic interaction directions on the world map
        """

        text_enter = self.engine.message_font.render("Z - Enter Region", True, (0, 0, 0))
        text_menu = self.engine.message_font.render("A - Main Menu", True, (0, 0, 0))

        panel = get_ui_panel((200, 35), border_color, panel_color)

        self.background_surface.blit(panel, (630 - panel.get_width()/2, 520))
        self.background_surface.blit(text_enter, (630 - text_enter.get_width()/2,
                                              520 + panel.get_height()/2 - text_enter.get_height()/2))

        self.background_surface.blit(panel, (630 - panel.get_width()/2, 565))
        self.background_surface.blit(text_menu, (630 - text_menu.get_width()/2 ,
                                              565 + panel.get_height()/2 - text_menu.get_height()/2))




    def render_arrows(self):

        """
        draw_arrows

        Draws the direction arrows for the unit on the main map screen.

        """

        # Draws arrows indicating where to move
        arrow_offset = {'up':Vector2(0, -35),
                     'down':Vector2(0, 35),
                     'left':Vector2(-35, 0),
                     'right':Vector2(35, 0)
                     }

        for direction in self.all_regions[tuple(self.player_pos)].paths.keys():
            if self.all_regions[tuple(self.player_pos)].paths[direction].unlock:

                arrow_coord = 35*self.player_pos + arrow_offset[direction]

                self.background_surface.blit(self.engine.direction_arrows[direction], arrow_coord)

    def render_updated_map(self, draw_arrows = True, fade_from = False):
        """
        Purpose: Updates the background surface and draws everything
        """
        self.render_background()
        if draw_arrows:
            self.render_arrows()

        self.engine.surface.blit(self.background_surface, (0, 0))
        self.player_sprite_group.update(self.player_pos*35)
        self.player_sprite_group.clear( self.engine.surface, self.background_surface)
        self.player_sprite_group.draw(self.engine.surface)

        if fade_from:

            self.engine.fade_from('black', 0.5)

        pygame.display.flip()
        self.engine.clock.tick(60)


    def move_to(self, direction_vector):
        """
        # Function Name: move to
        # Purpose: moves the player from point A to point B
        # Inputs: Destination - Where the player is going
        """

        # Smoothstep function
        # See: http://sol.gfxile.net/interpolation/
        smoothstep = lambda v: (v*v*(3-2*v))

        start_pos = 35*self.player_pos

        self.render_updated_map(draw_arrows = False)

        # Sets frame count proportional to the square root of the magnitude of the step vector
        frame_count = int(10*direction_vector.get_magnitude()**(1.0/2))

        for t in xrange(0, int(frame_count)+1):
            v = float(t)/float(frame_count)
            v = smoothstep(v)
            scale_term = int(35*v)
            intermediate_step = scale_term*direction_vector

            current_pos = start_pos + intermediate_step

            self.player_sprite_group.update(current_pos)
            self.player_sprite_group.clear( self.engine.surface, self.background_surface)
            rects = self.player_sprite_group.draw( self.engine.surface)

            pygame.display.update(rects)
            self.engine.clock.tick(60)

        self.player_pos += direction_vector

    ############################################
    # Interactive methods
    ############################################
    def say(self, line, speaker=""):

        """
        # Function name: say
        # Purpose: Displays a line of text and awaits for the player to press Z to continue
        # Inputs: string = The string of text to be displayed
        #         speaker = The name of the person saying it
        """

        text_speaker = self.engine.speaker_font.render(speaker, True, (0, 0, 0))

        menu_flag = True

        self.render_background()

        self.engine.surface.blit(self.background_surface, (0, 0))
        self.player_sprite_group.update(self.player_pos*35)
        self.player_sprite_group.clear( self.engine.surface, self.background_surface)
        self.player_sprite_group.draw(self.engine.surface)

        for conversation_image in self.conv_images.values():
            self.engine.surface.blit(conversation_image[0], conversation_image[1])

        self.engine.draw_conversation_message(line, speaker, None)

        pygame.display.flip()

        while menu_flag:

            # looks for event type data to select interaction
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN and (event.key == K_z or event.key == K_RETURN):
                    menu_flag = False

            # Hold down C to skip through the dialog.
            keys = pygame.key.get_pressed()
            if keys[K_c]:
                menu_flag = False

            self.engine.clock.tick(60)

    def choice(self, query, responses):
        """
        # Function name: Choice
        # Purpose: Displays a question for the player to answer
        # Inputs: string = The string of text to be displayed (e.g. do you like pie?)
        #         responses = A list of (preferably less than 3) options given to the player
        # Output: the response selected from the given choices
        """

        menu_flag = True
        menu_pos = 0
        update = True


        while menu_flag:

            # looks for event type data to select interaction
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN:
                    if event.key == K_UP or event.key == K_LEFT:
                        if menu_pos > 0:
                            menu_pos -= 1
                        elif menu_pos == 0:
                            menu_pos = len(responses)-1
                    if event.key == K_DOWN or event.key == K_RIGHT:
                        if menu_pos < len(responses)-1:
                            menu_pos += 1
                        elif menu_pos == len(responses)-1:
                            menu_pos = 0

                    if (event.key == K_z or event.key == K_RETURN):
                        menu_flag = False
                        return responses[menu_pos]

            if menu_flag:

                if update:
                    self.render_background()

                    self.engine.surface.blit(self.background_surface, (0, 0))
                    self.player_sprite_group.update(self.player_pos*35)
                    self.player_sprite_group.clear( self.engine.surface, self.background_surface)
                    self.player_sprite_group.draw(self.engine.surface)

                    self.engine.draw_choice_prompt(query, responses)
                    padlib_rounded_rect(self.engine.surface, selected_color, (138, 533 + menu_pos*45, 564, 39), 6, 5)

                    pygame.display.flip()

                self.engine.clock.tick(60)

    def user_input(self):
        """
        # Function Name: user input
        # Purpose: top level interaction. Checks if there is any input by the user
        #   and allows the user to move around the cursor with the arrow keys and select a unit
        #   if there is one under the mouse
        """

        arrowkeys = False
        directions = {K_LEFT:'left', K_RIGHT:'right', K_UP:'up', K_DOWN:'down'}
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                exit()
            if event.type == KEYDOWN:
                if event.key == K_LEFT or event.key == K_RIGHT or event.key == K_UP or event.key == K_DOWN:
                    # Checks if the player is allowed to move:
                    #   a. A path exists
                    #   b. The path is unlocked
                    if directions[event.key] in self.all_regions[tuple(self.player_pos)].paths.keys() and self.all_regions[tuple(self.player_pos)].paths[directions[event.key]].unlock:
                        self.move_to(self.all_regions[tuple(self.player_pos)].paths[directions[event.key]].vectors[directions[event.key]])
                        self.update_wm_player_data()
                        self.last_move = directions[event.key]

                        # Refreshes world map display

                        self.render_updated_map()

                        return
                if event.key == K_z or event.key == K_RETURN:

                    self.engine.fade_to('black', 0.5)
                    confirm_load = self.all_regions[tuple(self.player_pos)].navigate_loop(incoming_direction=self.last_move)
                    if not confirm_load:
                        self.render_updated_map(fade_from=True)

                    return confirm_load

                # World Map Menu
                if event.key == K_a:
                    self.engine.fade_to('black')
                    confirm_load = self.wm_menu()
                    self.engine.fade_to('black')
                    if not confirm_load:
                        self.render_updated_map(fade_from=True)
                    return confirm_load

    def navigate_loop(self, load_player=False):

        """
        # Function Name: Navigate Loop
        # Purpose: Player Interaction with the world map
        # Inputs: Load Player = True if player location data is to be loaded,
        #                       False if the map starts fresh
        """
        menu_flag = True


        self.engine.play_music('overworld')
        self.framenum = 0

        # Tracking for player's movement on the map
        self.last_move = None

        # Associates all player units with the world map
        self.unit_associate()

        if load_player:
            if self.engine.player.wm_data['wm_coords'] in self.all_regions.keys():
                self.player_pos = Vector2(self.engine.player.wm_data['wm_coords'])
                reset = False
            else:
                self.player_pos = Vector2(self.default_starting_pos)
                reset = True



            if self.engine.player.wm_data['in_region'] and not reset:


                if self.engine.player.wm_data['region_coords'] in self.all_regions[tuple(self.player_pos)].all_locations.keys():
                    confirm_load = self.all_regions[tuple(self.player_pos)].navigate_loop(self.engine.player.wm_data['region_coords'])
                else:
                    confirm_load = self.all_regions[tuple(self.player_pos)].navigate_loop()

                if confirm_load:
                    return

        self.update_wm_player_data()
        self.render_updated_map(fade_from=True)

        while menu_flag:

            if self.framenum == 9:
                self.framenum = 0
            self.framenum += 1

            confirm_load = self.user_input()
            if confirm_load:
                return


            self.player_sprite_group.clear( self.engine.surface, self.background_surface)
            self.player_sprite_group.update(self.player_pos*35)
            self.player_sprite_group.clear( self.engine.surface, self.background_surface)
            rects = self.player_sprite_group.draw( self.engine.surface)

            pygame.display.update(rects)
            self.engine.clock.tick(60)

    def wm_menu(self):

        """
        # Function Name: World Map Menu
        # Purpose: Manages access to team management, save / load, options
        """

        menu_flag = True
        menu_pos = 0

        fade_flag = False

        options_panel = get_ui_panel((280, 60), border_color, panel_color)
        desc_panel = get_ui_panel((280, 140), border_color, panel_color)

        text_options = []
        icon_names = ['Party', 'Treasures']
        text_options.append(self.engine.section_font.render("Party Stats", True, (0, 0, 0)))
        text_options.append(self.engine.section_font.render("Treasures", True, (0, 0, 0)))

        description_list = [ "Manage the members of the party.",
                             "Displays the treasures that have been collected.",
                             "Create spell actions from elemental items.",
                             "Trade artifacts found for items to Kourindou.",
                             "Return to the world map.",
                             "Check the latest mission news in the Bunbunmaru Newspaper.",
                             "Save or load game data. Remember to save often!",
                             "Change game settings.",
                             "Ends the game.",
                                ]


        if self.engine.unlock_shops or self.engine.check_event_completion(['CH1ST4']):
            text_options.append(self.engine.section_font.render("Synthesis", True, (0, 0, 0)))
            text_options.append(self.engine.section_font.render("Trading", True, (0, 0, 0)))
            icon_names.append('Synthesis')
            icon_names.append('Trading')
        else:
            text_options.append(self.engine.section_font.render("Synthesis", True, (100, 100, 100)))
            text_options.append(self.engine.section_font.render("Trading", True, (100,100, 100)))
            icon_names.append('Disabled Synthesis')
            icon_names.append('Disabled Trading')

        icon_names.append('Cancel')
        icon_names.append('Missions')
        icon_names.append('Data')
        icon_names.append('Options')
        icon_names.append('Exit')

        text_options.append(self.engine.section_font.render("Cancel", True, (0, 0, 0)))

        text_options.append(self.engine.section_font.render("Newspaper", True, (0, 0, 0)))
        text_options.append(self.engine.section_font.render("Save/Load", True, (0, 0, 0)))
        text_options.append(self.engine.section_font.render("Options", True, (0, 0, 0)))
        text_options.append(self.engine.section_font.render("Exit Game", True, (0, 0, 0)))

        text_desc = self.engine.speaker_font.render('Description', True, (0, 0, 0))

        self.engine.play_music('shop_theme')

        update = True

        while menu_flag:

             # looks for event type data to select interaction
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN:
                    update = True
                    if (event.key == K_z or event.key == K_RETURN):

                        # COLUMN 1

                        # Selects Party Management Menu
                        if menu_pos == 0:
                            self.party_menu()

                        # Treasure Item menu
                        elif menu_pos == 1:
                            self.engine.treasure_menu()

                        # Spell Synthesis menu
                        elif menu_pos == 2 and (self.engine.unlock_shops or self.engine.check_event_completion(['CH1ST4'])):
                            self.engine.spell_synthesis_system.spell_synthesis_menu()

                        # Item Trading Menu
                        elif menu_pos == 3 and (self.engine.unlock_shops or self.engine.check_event_completion(['CH1ST4'])):
                            self.engine.trading_system.trading_menu()

                        # COLUMN 3

                        # Mission Management Menu
                        elif menu_pos == 5:

                            self.engine.mission_manager.index_menu()

                        # Selects Save / Load Menu
                        elif menu_pos == 6:
                            confirm_load = self.engine.save_menu()
                            if confirm_load == True:
                                return True

                        # Selects Options Menu
                        elif menu_pos == 7:
                            self.engine.options_menu()

                        # Exits program
                        elif menu_pos == 8:
                            exit()

                    if event.key == K_UP:

                        # Top of first column, go to bottom of first column
                        if menu_pos == 0:
                            menu_pos = 4
                        # Top of second column, go to bottom of second column
                        elif menu_pos == 5:
                            menu_pos = 8
                        else:
                            menu_pos -= 1
                    if event.key == K_DOWN:
                        # Bottom of first column, go to top of first  column
                        if menu_pos == 4:
                            menu_pos = 0

                        # Bottom of second column, go to top of second column
                        elif menu_pos == 8:
                            menu_pos = 5
                        else:
                            menu_pos += 1
                    if event.key == K_LEFT or event.key == K_RIGHT:
                        if menu_pos < 5:
                            menu_pos += 5
                        else:
                            menu_pos -= 5

                    if menu_pos > 8:
                        menu_pos = 8


                    if ((event.key == K_z or event.key == K_RETURN) and menu_pos == 4) or event.key == K_x:
                        menu_flag = False
                        self.engine.play_music('overworld')


            if menu_flag:
                if update:
                    update = False
                    # Background image
                    self.engine.surface.blit(self.engine.stats_bg, (0, 0))
                    #self.engine.surface.blit(self.engine.menu_board, (0, 490))
                    # Header
                    # Renders the options
                    for index, option in enumerate(text_options):
                        if index < 5:
                            self.engine.surface.blit(options_panel, (70, 70 + 80*index))
                            self.engine.surface.blit(self.engine.wm_icons[icon_names[index]],
                                                     (90, 70 + options_panel.get_height()/2 - self.engine.wm_icons[icon_names[index]].get_height()/2 + 80*index))
                            self.engine.surface.blit(text_options[index], (90 + options_panel.get_width()/2 - text_options[index].get_width()/2,
                                                                           72 + options_panel.get_height()/2 - text_options[index].get_height()/2 + 80*index))

                        else:
                            self.engine.surface.blit(options_panel, (490, 70 + 80*(index-5)))
                            self.engine.surface.blit(self.engine.wm_icons[icon_names[index]],
                                                     (510, 70 + options_panel.get_height()/2 - self.engine.wm_icons[icon_names[index]].get_height()/2 + 80*(index-5)))
                            self.engine.surface.blit(text_options[index], (510 + options_panel.get_width()/2 - text_options[index].get_width()/2,
                                                                           72 + options_panel.get_height()/2 - text_options[index].get_height()/2 + 80*(index-5)))

                    if menu_pos < 5:
                        padlib_rounded_rect(self.engine.surface, selected_color,
                                            (68, 68 + 80*menu_pos, options_panel.get_width() + 4, options_panel.get_height()+4), 6, 5)
                    else:
                        padlib_rounded_rect(self.engine.surface, selected_color,
                                    (488, 68 + 80*(menu_pos-5), options_panel.get_width() + 4, options_panel.get_height()+4), 6, 5)

                    self.engine.surface.blit(desc_panel, (490,  390))
                    self.engine.surface.blit(text_desc, (490 + desc_panel.get_width()/2 - text_desc.get_width()/2,  400))
                    draw_aligned_text(self.engine.surface, description_list[menu_pos], self.engine.message_font, (0, 0, 0), (500, 430), 260)



                # Fade in once
                if fade_flag == False:
                    self.engine.fade_from('black')
                    fade_flag = True

                pygame.display.flip()
                self.engine.clock.tick(60)

    def party_menu(self):

        """
        # Party Management Menu
        # Purpose: Manages units in the party allowing management of
        #       traits and spells, etc
        """

        units = self.engine.player_units
        menu_flag = True
        menu_pos = [0, 0]
        unit_index = 0

        update = True

        while menu_flag:

             # looks for event type data to select interaction
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN:
                    if event.key == K_LEFT:
                        update = True
                        if menu_pos[0] > 0:
                            menu_pos[0] -= 1
                        elif menu_pos[0] == 0:
                            menu_pos[0] = 3

                    if event.key == K_RIGHT:
                        update = True
                        if menu_pos[0] < 3:
                            menu_pos[0] += 1
                        elif menu_pos[0] == 3:
                            menu_pos[0] = 0

                    if event.key == K_UP:
                        update = True
                        if menu_pos[1] > 0:
                            menu_pos[1] -= 1
                        elif menu_pos[1] == 0:
                            menu_pos[1] = 3
                    if event.key == K_DOWN:
                        update = True
                        if menu_pos[1] < 3:
                            menu_pos[1] += 1
                        elif menu_pos[1] == 3:
                            menu_pos[1] = 0

                    # Computes the selected unit
                    unit_index = menu_pos[0] + 4*menu_pos[1]

                    # Calls up the options screen
                    if event.key == K_z or event.key == K_RETURN:
                        if unit_index < len(units):
                            self.unit_options(unit_index)
                        update = True

                    # Jumps straight to stats screen
                    if event.key == K_c:
                        if unit_index < len(units):
                            self.stats_interface(units[unit_index])
                        update = True

                    # Sort Units
                    if event.key == K_a:
                        update = True
                        self.engine.player_units = self.sort_units(units)
                        units = self.engine.player_units

                    if event.key == K_x:
                        return

            if update:
                update = False
                self.draw_units_screen(units)
                self.draw_party_directions()

                # Draw the cursor
                x_spacing = 150
                y_spacing = 100
                padlib_rounded_rect(self.engine.surface, selected_color,
                                    (178 - 35 + menu_pos[0]*x_spacing, 103 + menu_pos[1]*y_spacing, 109, 39), 6, 5)

                # If the selected position is in the range of the length of the units, draw the
                # stats of the unit
                if unit_index < len(units):
                    units[unit_index].plot_stats()

            pygame.display.flip()
            self.engine.clock.tick(60)




    def draw_party_directions(self):
        """
        function name: draw_party_directions

        Desc: Draw basic interaction directions on the world map
        """

        text_enter = self.engine.message_font.render("Z - Select Unit", True, (0, 0, 0))
        text_sort = self.engine.message_font.render("A - Sort Units", True, (0, 0, 0))
        text_stats = self.engine.message_font.render("C - Show Stats", True, (0, 0, 0))

        panel = get_ui_panel((180, 35), border_color, panel_color)
        big_panel = get_ui_panel((280, 35), border_color, panel_color)

        self.engine.surface.blit(panel, (630 - panel.get_width() - 5, 520))
        self.engine.surface.blit(text_enter, (630 - panel.get_width()/2 - text_enter.get_width()/2 - 5 ,
                                              520 + panel.get_height()/2 - text_enter.get_height()/2))

        self.engine.surface.blit(panel, (635, 520))
        self.engine.surface.blit(text_sort, (635 + panel.get_width()/2 - text_sort.get_width()/2 ,
                                              520 + panel.get_height()/2 - text_sort.get_height()/2))

        self.engine.surface.blit(big_panel, (630 - big_panel.get_width()/2, 565))
        self.engine.surface.blit(text_stats, (630 - text_stats.get_width()/2 ,
                                              565 + panel.get_height()/2 - text_stats.get_height()/2))


    def draw_units_screen(self, units):

        """
        Draw units screen

        Draws the interface background for the units screen

        Units - A list consisting of player character units

        """

        # Compiles a list of names
        name_panel = get_ui_panel((105, 35), border_color, panel_color)

        names = []
        for unit in units:
            text_name = self.engine.message_font.render(unit.name, True, (0, 0, 0))
            names.append(text_name)

        self.engine.surface.blit(self.engine.party_bg, (0, 0))
        self.engine.surface.blit(self.engine.menu_board, (0, 490))


        x_spacing = 150
        y_spacing = 100

        # Draw the panels
        for row in xrange(0, 4):
            for column in xrange(0, 4):
                self.engine.surface.blit(self.engine.unit_tile, (180+x_spacing*column, 62+y_spacing*row))
                self.engine.surface.blit(name_panel, (180-35+x_spacing*column, 105+y_spacing*row))

        # Draw the units
        for index, unit in enumerate(units):

            y_position = index/4
            x_position = index%4

            self.engine.surface.blit(unit.image, (180+x_spacing*x_position, 58+y_spacing*y_position), (0, 0, 35, 35))
            name = names[x_position+4*y_position]
            self.engine.surface.blit(name, (180 - 35 + name_panel.get_width()/2 + x_spacing*x_position - name.get_width()/2,
                                            105 + name_panel.get_height()/2 - name.get_height()/2 + y_spacing*y_position))


    def stats_interface(self, unit):


        # Records the unit's position in the team list
        index = self.engine.player_units.index(unit)

        menu_flag = True
        # page 0 (traits), page 1 (stats, default), page 2 (spells)
        page = 1
        while menu_flag:

            # Calls a unit's stats data loop in the requested page
            if page == 0:
                event = unit.traits_stats_loop()
            elif page == 1:
                event = unit.stats_loop()
            else:
                event = unit.spell_stats_loop()

            if event.key == K_x:
                menu_flag = False

            # Up and down keys traverse the list of units in a team
            elif event.key == K_DOWN:
                index += 1
                if index > len(self.engine.player_units)-1:
                    index = 0
                self.engine.fade_to('black', 0.25)
                unit = self.engine.player_units[index]
            elif event.key == K_UP:
                index -= 1
                if index < 0:
                    index = len(self.engine.player_units)- 1
                unit = self.engine.player_units[index]
                self.engine.fade_to('black', 0.25)
            elif event.key == K_LEFT:
                page -= 1
                if page < 0:
                    page = 2
                self.engine.fade_to('black', 0.25)
            elif event.key == K_RIGHT:
                page += 1
                if page > 2:
                    page = 0
                self.engine.fade_to('black', 0.25)

    def sort_units(self, units):
        """"
        # function name: sort_units
        # purpose: Allows player to sort units.
        """

        # Sorts units by a certain critieria
        def sort_units_by(criteria):
            sort_list = []
            # Compiles the criteria to be sorted for each unit (Name will be tiebreaker)
            for unit in self.engine.player_units:
                sort_criteria = {'Name':(unit.name, unit.name, unit),
                                 'Level':(unit.level, unit.name, unit),
                                 'HP':(unit.HP, unit.name, unit),
                                 'STR':(unit.STR, unit.name, unit),
                                 'MAG':(unit.MAG, unit.name, unit),
                                 'DEF':(unit.DEF, unit.name, unit),
                                 'MDEF':(unit.MDEF, unit.name, unit),
                                 'ACC':(unit.ACC, unit.name, unit),
                                 'AGL':(unit.AGL, unit.name, unit)}

                sort_list.append(sort_criteria[criteria])

            # Sorts units
            sort_list.sort()
            # Sorts Highest to Lowest for numerical criteria
            if criteria in ('Level', 'HP', 'STR', 'MAG', 'DEF', 'MDEF', 'ACC', 'AGL'):
                sort_list.reverse()

            units_list = [unit[2] for unit in sort_list]
            return units_list


        menu_flag = True
        menu_pos = [0, 0]
        criteria_list = ['Name', 'Level', 'HP', 'STR', 'DEF', 'MDEF', 'ACC', 'AGL']
        text_criteria_list = [self.engine.section_font.render(criteria, True, (0, 0, 0)) for criteria in criteria_list]
        update = True

        options_panel = get_ui_panel((80, 40), border_color, panel_color)

        while menu_flag:
             # looks for event type data to select interaction
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN:
                    # Handles left/right
                    if event.key == K_UP or event.key == K_DOWN:
                        if menu_pos[1] == 1:
                            menu_pos[1] = 0
                        else:
                            menu_pos[1] = 1

                        update = True

                    if event.key == K_LEFT:
                        if menu_pos[0] > 0:
                            menu_pos[0] -= 1
                        elif menu_pos[0] == 0:
                            menu_pos[0] = 3
                        update = True

                    if event.key == K_RIGHT:
                        if menu_pos[0] < 3:
                            menu_pos[0] += 1
                        elif menu_pos[0] == 3:
                            menu_pos[0] = 0
                        update = True

                    # Calls up the options screen
                    if event.key == K_z or event.key == K_RETURN:
                        units = sort_units_by(criteria_list[menu_pos[0]+4*menu_pos[1]])
                        update = True
                    if event.key == K_x:
                        return units

            if update:
                update = False
                self.draw_units_screen(units)

                # Draw the OPTIONS

                for index, text_criteria in enumerate(text_criteria_list):
                    if index < 4:
                        x_position = 455 + index*(options_panel.get_width()+10)
                        y_position = 510
                    else:
                        x_position = 455 + (index-4)*(options_panel.get_width()+10)
                        y_position = 570

                    self.engine.surface.blit(options_panel, (x_position, y_position))
                    self.engine.surface.blit(text_criteria, (x_position+options_panel.get_width()/2 - text_criteria.get_width()/2,
                                                             y_position+2+options_panel.get_height()/2 - text_criteria.get_height()/2))


                # Draw the cursor
                cursor_x = 453 + (options_panel.get_width()+10)*menu_pos[0]
                cursor_y = 508 + 60*menu_pos[1]
                padlib_rounded_rect(self.engine.surface, selected_color, (cursor_x, cursor_y, options_panel.get_width()+4, options_panel.get_height()+4), 6, 5)

                pygame.display.flip()
            self.engine.clock.tick(60)


    def unit_options(self, selected):
        """
        # Name: Unit_options
        # Purpose: Allows selection of various managment options for an individual unit
        # Inputs: None
        """

        menu_pos = 0
        menu_flag = True

        text_spells = self.engine.section_font.render("Spell Actions", True, (0, 0, 0))
        text_traits = self.engine.section_font.render("Set Traits", True, (0, 0, 0))
        text_stats = self.engine.section_font.render("Stats", True, (0, 0, 0))
        text_cancel = self.engine.section_font.render("Cancel", True, (0, 0, 0))

        options_panel = get_ui_panel((180, 40), border_color, panel_color)

        units = self.engine.player_units
        selected_unit = self.engine.player_units[selected]

        update = True

        while menu_flag:

             # looks for event type data to select interaction
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN:
                    if event.key == K_UP or event.key == K_DOWN:
                        # Sets the new position (up and down swap the current row)
                        menu_pos = [2, 3, 0, 1][menu_pos]
                        update = True
                    if event.key == K_LEFT or event.key == K_RIGHT:
                        # sets the new position (left and right swap the current column)
                        menu_pos = [1, 0, 3, 2][menu_pos]
                        update = True

                    if event.key == K_z or event.key == K_RETURN:
                        update = True
                        # Spells
                        if menu_pos == 0:
                            selected_unit.spell_swap_menu()

                        # Swap Traits
                        elif menu_pos == 1:
                            selected_unit.trait_swap_menu()

                        # Stats
                        elif menu_pos == 2:
                            self.stats_interface(selected_unit)

                        # Cancel
                        elif menu_pos == 3:
                            return

                    if event.key == K_x:
                        return

            if menu_flag:

                if update:
                    update = False
                    self.draw_units_screen(units)

                    # Draw the cursor
                    x_spacing = 150
                    y_spacing = 100
                    unit_cursor_x = selected % 4
                    unit_cursor_y = selected / 4
                    padlib_rounded_rect(self.engine.surface, selected_color,
                                        (178 - 35 + unit_cursor_x*x_spacing, 103 + unit_cursor_y*y_spacing, 109, 39), 6, 5)

                    # draws the available options
                    option_positions = ((440, 510), (640, 510), (440, 570), (640, 570))
                    for position in option_positions:
                        self.engine.surface.blit(options_panel, position)

                    self.engine.surface.blit(text_spells, (440 + options_panel.get_width()/2 - text_spells.get_width()/2,
                                                           512 + options_panel.get_height()/2 - text_spells.get_height()/2))
                    self.engine.surface.blit(text_traits, (640 + options_panel.get_width()/2 - text_traits.get_width()/2,
                                                           512 + options_panel.get_height()/2 - text_traits.get_height()/2))
                    self.engine.surface.blit(text_stats, (440 + options_panel.get_width()/2 - text_stats.get_width()/2,
                                                           572 + options_panel.get_height()/2 - text_stats.get_height()/2))
                    self.engine.surface.blit(text_cancel, (640 + options_panel.get_width()/2 - text_cancel.get_width()/2,
                                                           572 + options_panel.get_height()/2 - text_cancel.get_height()/2))

                    # draws a box around current selection
                    menu_cursor_x, menu_cursor_y = option_positions[menu_pos]
                    padlib_rounded_rect(self.engine.surface, selected_color,
                                        (menu_cursor_x-2, menu_cursor_y-2, options_panel.get_width()+4, options_panel.get_height()+4), 6, 5)

                    # Draw the cursor
                    # If the selected position is in the range of the length of the units, draw the
                    # stats of the unit
                    selected_unit.plot_stats()




                    pygame.display.flip()
                self.engine.clock.tick(60)


class Region(object):

    def __init__(self, engine, name, wm_coords, prereq, image_pos, advanced_background):
        """
        # Function Name: __init__
        # Purpose: Intializes a region
        # Inputs:
        #         engine - main system engine
        #         name - Region Name
        #         wm_coords - World map coordinates
        #         prereq - when to unlock this location
        #         image_pos - location to draw this region on the worldmap
        #         advanced_background - flag to enable drawing of new map image system
                                        to be removed when all regions have new map system implemented.
        """

        self.engine = engine
        self.name = name
        self.wm_coords = wm_coords
        self.pix_coords = 35*wm_coords
        self.prereq = prereq
        self.image_pos = image_pos

        # World Map Display Name
        self.name_small = self.engine.sfont.render(self.name, True, (0, 0, 0))
        self.name_big = self.engine.section_font.render(self.name, True, (0, 0, 0))
        # Gets the size of the name to center it
        name_small_halfwidth = self.name_small.get_width()/2
        self.name_coords = 35*(wm_coords+Vector2(0, 1))+Vector2(17-name_small_halfwidth, 0)

        # Subregions and locations
        self.subregions = []
        self.all_locations = {}
        self.all_locations_by_name = {}

        # Connection dot points
        self.dot_positions = set()
        self.paths = {}
        # self.connection_paths = paths
        # self.vector_paths = {'up':Vector2(0, 0), 'down':Vector2(0, 0), 'left':Vector2(0, 0), 'right':Vector2(0, 0)}

        self.unlock = False

        default_position = Vector2(0, 0)
        self.player_pos = default_position
        self.player_sprite_group = pygame.sprite.RenderUpdates()
        self.player_sprite = WorldmapSprite( 35*self.player_pos, self.engine.player_img)
        self.player_sprite_group.add(self.player_sprite)
        self.background_surface = pygame.surface.Surface((840, 630))
        self.advanced_background = advanced_background

    def add_locations(self, locations):
        """
        # Function Name: add_locations
        # Purpose: Adds a set of locations
        # Inputs: locations - a list of location objects to be added to the
        """

        for location in locations:
            # Adds the location to the dictionary
            self.all_locations[tuple(location.wm_coords)] = location
            self.all_locations_by_name[location.name] = location

            # Associates a location with the region
            location.region = self
            location.get_graphical()

            # if the location is flagged as an entrance location, set it to the
            # # default entry way to the world map
            # if location.name == self.entrance:
                # location.entrance = True
                # self.entrance_coords = location.wm_coords
            if location.name in self.entrances.values():
                location.exit = True


    def initialize_paths(self, location_path_list):

        """
        # Function Name: initialize_paths
        # Purpose: Initializes the paths of each location
        # Input: Location path list - a list of the paths to be initialized
        """

        self.location_path_list = location_path_list

        # List of all dots to not include a range around the location
        remove_pts = []
        for location in self.all_locations.values():
            remove_pts.extend((tuple(location.wm_coords), tuple(location.wm_coords+Vector2(-1, 1))))

        for path in location_path_list:

            # Each path has two connecting points along with the direction key to press to get to each location
            direction_to_1 = path.locations.keys()[0]
            region_1 = path.locations[direction_to_1]
            direction_to_2 = path.locations.keys()[1]
            region_2 = path.locations[direction_to_2]

            # We assign the starting point's path directions to this path so it can be called when the character needs to move
            self.all_locations_by_name[region_2].paths[direction_to_1] = path
            self.all_locations_by_name[region_1].paths[direction_to_2] = path

            # Constructs destination vectors
            delta_pos = self.all_locations_by_name[region_1].wm_coords - self.all_locations_by_name[region_2].wm_coords
            path.vectors[direction_to_1] = delta_pos
            path.vectors[direction_to_2] = -1*(delta_pos.copy())

    def update_region_player_data(self):
        """
        # Function Name: update wm player data
        # Purpose: Updates the location of the player
        """

        self.engine.player.wm_data['in_region'] = True
        self.engine.player.wm_data['region_coords'] = tuple(self.player_pos)

    ############################################
    # Graphical Methods
    ############################################

    def render_background(self, draw_directions = True):

        """
        # Function Name: Render background to the background surface
        # Purpose: Draws the background map and the locations
        """

        if self.advanced_background:
            self.background_surface.blit(self.engine.region_backgrounds[self.name], (0, 0))
        else:
            self.background_surface.blit(self.engine.region_bg, (0, 0))

        self.background_surface.blit(self.engine.menu_board, (0, 490))

        # [self.background_surface.blit(self.engine.path_dot, dot_pos)  for path in self.location_path_list if path.unlock for dot_pos in path.dot_positions]

        # Location dots and path points
        for location in self.all_locations.values():
            if self.advanced_background:

                if not location.unlock:
                    self.background_surface.blit(self.engine.hidden_location_images[location.name], location.image_pos)

                elif location == self.all_locations[tuple(self.player_pos)]:
                    self.background_surface.blit(self.engine.active_location_images[location.name], location.image_pos)
                else:
                    self.background_surface.blit(self.engine.inactive_location_images[location.name], location.image_pos)

            else:
                if location.unlock:
                    self.background_surface.blit(location.name_small, location.name_coords)

            if location.unlock:
                self.background_surface.blit(self.engine.location_circle, location.pix_coords, (location.type*35, 0, 35, 35))


            if location.has_event() and location.unlock:
                if self.player_pos == location.wm_coords:
                    self.background_surface.blit(self.engine.event_icon, (location.pix_coords+Vector2(30, -25)))
                else:
                    self.background_surface.blit(self.engine.event_icon, (location.pix_coords+Vector2(10, -15)))

        # Location Description

        name_panel = get_ui_panel((280, 45), border_color, panel_color)
        desc_panel = get_ui_panel((320, 60), border_color, panel_color)

        text_location_name = self.all_locations[tuple(self.player_pos)].name_big

        self.background_surface.blit(name_panel, (40, 505))
        self.background_surface.blit(text_location_name, (40 + name_panel.get_width()/2 - text_location_name.get_width()/2,
                                                          507 + name_panel.get_height()/2 - text_location_name.get_height()/2))

        self.background_surface.blit(desc_panel, (20, 555))
        draw_aligned_text(self.background_surface, self.all_locations[tuple(self.player_pos)].desc, self.engine.sfont,
                          (0, 0, 0), (30, 565), desc_panel.get_width()-20)

        if draw_directions:
            self.draw_directions()


    def render_arrows(self):

        """
        draw_arrows

        Draws the direction arrows for the unit on the main map screen.

        """

        # Draws arrows indicating where to move
        arrow_offset = {'up':Vector2(0, -40),
                     'down':Vector2(0, 40),
                     'left':Vector2(-35, 15),
                     'right':Vector2(35, 15)
                     }

        for direction in self.all_locations[tuple(self.player_pos)].paths.keys():
            if self.all_locations[tuple(self.player_pos)].paths[direction].unlock:

                arrow_coord = 35*self.player_pos + arrow_offset[direction]

                self.background_surface.blit(self.engine.direction_arrows[direction], arrow_coord)

    def render_updated_map(self, draw_arrows = True, fade_from = False):

        """
        Purpose: Updates the background surface and draws everything

        input - draw_arrows - enable drawing of direction arrows
        """
        self.render_background()
        if draw_arrows:
            self.render_arrows()
        self.engine.surface.blit(self.background_surface, (0, 0))
        self.player_sprite_group.update(self.player_pos*35)
        self.player_sprite_group.clear( self.engine.surface, self.background_surface)
        self.player_sprite_group.draw(self.engine.surface)

        if fade_from:
            self.engine.fade_from('black', 0.5)

        pygame.display.flip()
        self.engine.clock.tick(60)



    def draw_directions(self):
        """
        function name: draw_directions

        Desc: Draw basic interaction directions on the world map
        """

        text_enter = self.engine.message_font.render("Z - Select Event", True, (0, 0, 0))
        text_menu = self.engine.message_font.render("A - Main Menu", True, (0, 0, 0))
        text_return = self.engine.message_font.render("X - Leave Region", True, (0, 0, 0))

        panel = get_ui_panel((180, 35), border_color, panel_color)
        big_panel = get_ui_panel((280, 35), border_color, panel_color)

        self.background_surface.blit(panel, (630 - panel.get_width() - 5, 520))
        self.background_surface.blit(text_enter, (630 - panel.get_width()/2 - text_enter.get_width()/2 - 5 ,
                                              520 + panel.get_height()/2 - text_enter.get_height()/2))

        self.background_surface.blit(panel, (635, 520))
        self.background_surface.blit(text_menu, (635 + panel.get_width()/2 - text_menu.get_width()/2 ,
                                              520 + panel.get_height()/2 - text_menu.get_height()/2))

        self.background_surface.blit(big_panel, (630 - big_panel.get_width()/2, 565))
        self.background_surface.blit(text_return, (630 - text_return.get_width()/2 ,
                                              565 + panel.get_height()/2 - text_return.get_height()/2))


    def move_to(self, direction_vector):
        """
        # Function Name: move_to
        # Purpose: Moves the player to a new direction
        # DIrection vector - Relative direction to move from curren tposition
        """

        # Smoothstep function
        # See: http://sol.gfxile.net/interpolation/
        smoothstep = lambda v: (v*v*(3-2*v))


        start_pos = 35*self.player_pos
        current_pos = 35*self.player_pos

        # Sets frame count proportional to the square root of the magnitude of the step vector
        frame_count = int(10*direction_vector.get_magnitude()**(1.0/2))


        self.render_updated_map(draw_arrows = False)

        for t in xrange(0, int(frame_count)+1):
            v = float(t)/float(frame_count)
            v = smoothstep(v)
            scale_term = int(35*v)
            intermediate_step = scale_term*direction_vector

            current_pos = start_pos + intermediate_step

            self.player_sprite_group.update(current_pos)
            self.player_sprite_group.clear( self.engine.surface, self.background_surface)
            rects = self.player_sprite_group.draw( self.engine.surface)

            pygame.display.update(rects)
            self.engine.clock.tick(60)

        start_pos = current_pos.copy()

        self.player_pos += direction_vector
        self.render_updated_map()


    ############################################
    # Interactive methods
    ############################################

    def user_input(self):
        """
        # Function Name: user input
        # Purpose: top level interaction. Checks if there is any input by the user
        #   and allows the user to move around the cursor with the arrow keys and select a unit
        #   if there is one under the mouse
        """

        directions = {K_LEFT:'left', K_RIGHT:'right', K_UP:'up', K_DOWN:'down'}

        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                exit()
            if event.type == KEYDOWN:
                if event.key == K_LEFT or event.key == K_RIGHT or event.key == K_UP or event.key == K_DOWN:
                    # Checks if the player is allowed to move:
                    #   a. A path exists
                    #   b. The path is unlocked
                    if directions[event.key] in self.all_locations[tuple(self.player_pos)].paths.keys() and self.all_locations[tuple(self.player_pos)].paths[directions[event.key]].unlock:
                        self.move_to(self.all_locations[tuple(self.player_pos)].paths[directions[event.key]].vectors[directions[event.key]])
                        self.update_region_player_data()
                        return

                # Location menu only accessible if events are present
                if (event.key == K_z or event.key == K_RETURN) and self.all_locations[tuple(self.player_pos)].all_events:
                    exit_flag = self.all_locations[tuple(self.player_pos)].menu_loop()
                    return exit_flag

               # World Map Menu
                if event.key == K_a:
                    self.engine.fade_to('black')
                    confirm_load = self.wm_parent.wm_menu()
                    if confirm_load:
                        return 'load_player'
                    else:
                        self.engine.fade_to('black')
                        self.render_updated_map(fade_from=True)

                if event.key == K_x:
                    return 'jump2wm'

        self.render_updated_map()
        return False

    def navigate_loop(self, start_coords=None, incoming_direction=None):

        """
        # Function Name: Navigate Loop
        # Purpose: Player Interaction with the world map
        # Inputs: start_coords - Player starting coordinates
        #         incoming direction - Player's incoming direction if a location has two+ gates
        """

        menu_flag = True

        if start_coords:
            self.player_pos = Vector2(start_coords)
        else:
            # If a location only has one entrance or player is not coming from another location since they just started the game, jump them directly to that default/only entrance
            if len(self.entrances.values()) == 1 or not incoming_direction:
                self.player_pos = Vector2(tuple(self.all_locations_by_name[self.entrances['default']].wm_coords))
            else:
                self.player_pos = Vector2(tuple(self.all_locations_by_name[self.entrances[incoming_direction]].wm_coords))

        self.update_region_player_data()
        self.render_updated_map(fade_from=True)
        while menu_flag:

            input_flag = self.user_input()


            # If the location flag received, return to the world map
            if input_flag == 'jump2wm':
                menu_flag = False
                self.wm_parent.player_pos = Vector2(tuple(self.wm_coords))
                self.wm_parent.update_wm_player_data()
            # If load player flag received, load new map
            elif input_flag == 'load_player':
                return True

            self.player_sprite_group.update(self.player_pos*35)
            self.player_sprite_group.clear( self.engine.surface, self.background_surface)
            self.player_sprite_group.draw(self.engine.surface)


            pygame.display.flip()

            self.engine.clock.tick(60)


# Connecting path object
class Path(object):

    def __init__(self, locations, prereq):

        """
        # Function Name; __init__
        # Purpose: Initializes a path object
        # Inputs:
        #           locations - where does this path connect to
        #           prereqs - events that must be completed for this path to open
        """

        self.locations = locations
        if prereq:
            self.prereq = prereq
            self.unlocked = False
        else:
            self.prereq = None
            self.unlocked = True
        self.vectors = {}
        self.dot_positions = []


    def __str__(self):
        """
        # function Name: __str__
        # Purpose: String representation of a path
        # Output: returns the two connections of the path, the prerequisites, and whether it is locked or not
        """

        return str([self.locations, self.prereqs, self.unlocked])


class Location(object):

    def __init__(self, name, desc, wm_coords, type, portrait, image_position, prereq):
        """
        # Function Name: __init__
        # Purpose: Intializes a location
        # Inputs:
        #         Region - Parent region
        #         Name - Location's name
        #         Wm_coords - World map coordinates
        #         type - An integer that corresponds to the following types of location
        #                   0 - Neutral
        #                   1 - Danger Zone (Likely location for monster attacks and missions)
        #                   2 - Safe Zone (Towns and villages)
        #                   3 - Region exit / entrance point
        #         portrait - portrait image ID
        """

        self.name = name
        self.desc = desc
        self.wm_coords = wm_coords
        self.pix_coords = 35*wm_coords + Vector2(0, 5)

        self.image_pos = image_position
        self.type = type
        self.portrait = portrait
        self.all_events = []
        self.entrance = False
        self.exit = False
        self.paths = {}
        self.prereq = prereq
        self.unlock = False
        self.region = None

    def add_event(self, event):
        """
        # Function Name: add_event
        # Purpose: Adds an event to the region's list
        """
        self.all_events.append(event)
        event.location = self

    def has_event(self):
        """
        # Function Name: has_event
        # Purpose: Checks if this location has an any events
        # Output: Return True if location has events
        """
        for event in self.all_events:
            if event:
                return True
        # no event, return False
        return False

    def get_graphical(self):

        """
        # Function Name: get_graphical
        # Purpose: Generates the graphical objects for a location (note: requires association with a region)
        """

        # World Map Display objects

        self.name_small = self.region.engine.sfont.render(self.name, True, (0, 0, 0))
        self.name_big = self.region.engine.section_font.render(self.name, True, (0, 0, 0))

        # Gets the size of the name to center it
        name_small_halfwidth = self.name_small.get_width()/2
        self.name_coords = 35*(self.wm_coords+Vector2(0, 1))+Vector2(17-name_small_halfwidth, 5)

    def get_text_all_events(self):
        """
        # Function Name: get_text_all_events
        # Purpose: Generates the text objects from a location's events
        """

        text_event = []
        for event in self.all_events:
            text_event.append(self.region.engine.section_font.render(event.name, True, (0, 0, 0)))
        return text_event

    def render_menu_options(self):
        """
        Purpose: Draws the locations event options to background surface
        """

        options_panel = get_ui_panel((360, 40), border_color, panel_color)

        # Generate text objects
        text_cancel = self.region.engine.section_font.render("Cancel", True, (0, 0, 0))

        text_event = self.get_text_all_events()
        menu_y = 515
        for index, event in enumerate(self.all_events):
            self.region.background_surface.blit(options_panel, (450, menu_y + 50*index))
            self.region.background_surface.blit(self.region.engine.wm_icons[event.type], (460, menu_y + 2 + 50*index))
            self.region.background_surface.blit(text_event[index], (450 + options_panel.get_width()/2 - text_event[index].get_width()/2,
                                                                    menu_y + 2 + options_panel.get_height()/2 - text_event[index].get_height()/2  - +50*index))

        self.region.background_surface.blit(options_panel, (450, menu_y + 50*(index+1)))
        self.region.background_surface.blit(text_cancel, (450 + options_panel.get_width()/2 - text_cancel.get_width()/2,
                                                          menu_y + 2 + options_panel.get_height()/2 - text_cancel.get_height()/2 + 50*(index+1)))


    def render_updated_map(self):
        """
        Renders a full frame of the map and options menu
        """

        self.region.render_background(draw_directions = False)
        self.render_menu_options()
        self.region.engine.surface.blit(self.region.background_surface, (0, 0))
        self.region.player_sprite_group.draw(self.region.engine.surface)

    def menu_loop(self):
        """
        # Function Name: menu_loop
        # Purpose: Location level menu loop that allows for the selection of an event in a location.
        # Inputs: None
        # Outputs: exit_flag
        """



        menu_flag = True
        menu_pos = 0

        menu_max = len(self.all_events)
        update = True
        while menu_flag:

            # User Input
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_F4 and bool(event.mod & KMOD_ALT)):
                    exit()
                if event.type == KEYDOWN:
                    if event.key == K_LEFT or event.key == K_UP:
                        if menu_pos > 0:
                            menu_pos -= 1
                        else:
                            menu_pos = menu_max
                        update = True
                    elif event.key == K_RIGHT or event.key == K_DOWN:
                        if menu_pos < menu_max:
                            menu_pos += 1
                        else:
                            menu_pos = 0
                        update = True
                    if event.key == K_z or event.key == K_RETURN:

                        # Selects event if it is available
                        if menu_pos < menu_max:

                            # Auto-save after mission if enabled
                            if self.region.engine.options.auto_save:
                                self.region.engine.player.save('autosave')

                            self.all_events[menu_pos].execute()

                            # Auto-save after mission if enabled
                            if self.region.engine.options.auto_save:
                                self.region.engine.player.save('autosave')

                            return
                        # Cancel
                        elif menu_pos == menu_max:
                            return

                    if event.key == K_x:
                        return

            if menu_flag:
                if update:
                    update = False
                    # Location menu

                    self.render_updated_map()
                    padlib_rounded_rect(self.region.engine.surface, selected_color,
                                        (448, 513 + 50*menu_pos, 364, 44), 6, 5)
                    pygame.display.flip()

                self.region.engine.clock.tick(60)


class WorldmapSprite(pygame.sprite.Sprite):

    def __init__(self, start_pos, image):
        """
        # Function: __init__
        # Purpose: Creates a sprite for the worldmap
        # Inputs:   map - worldmap object to associate with
        #           image - image to use in the worldmap sprite
        #
        """
        pygame.sprite.Sprite.__init__(self)
        self.image = image.subsurface((0, 0, 35, 35))
        self.rect = self.image.get_rect()
        self.rect.topleft = tuple(start_pos)

    def update(self, new_position):
        """
        # Function: update
        # Purpose: Updates sprite to current location of the unit (pixel)
        """
        self.rect.topleft = tuple(new_position)