def OnRealmMOD():
    global REALM
    global PARTY
    global REALMCHARACTERS

    if RPG_BUILD_DEMO:
        TGEObject("DEMOINFO_BITMAP").setBitmap("~/data/ui/demo/MoD")
        TGEObject("DEMOINFOWIND_LATERBUTTON"
                  ).command = "canvas.popDialog(DemoInfoWnd);"
        TGEEval("canvas.pushDialog(DemoInfoWnd);")

    if REALM == RPG_REALM_DARKNESS:
        return

    PARTY = []

    REALM = RPG_REALM_DARKNESS
    REALMCHARACTERS = DARKCHARACTERS
    FillCharacters()

    from playerSettings import PLAYERSETTINGS
    getRealm, lastParty = PLAYERSETTINGS.loadLastParty(RPG_REALM_DARKNESS)
    for lp in lastParty:
        for ci in REALMCHARACTERS:
            if ci.name == lp:
                gotone = True
                PARTY.append(ci)

    UpdatePartyList()
def OnRealmMOD():
    global REALM
    global PARTY
    global REALMCHARACTERS
    
    if RPG_BUILD_DEMO:
        TGEObject("DEMOINFO_BITMAP").setBitmap("~/data/ui/demo/MoD")
        TGEObject("DEMOINFOWIND_LATERBUTTON").command = "canvas.popDialog(DemoInfoWnd);"
        TGEEval("canvas.pushDialog(DemoInfoWnd);")
    
    if REALM == RPG_REALM_DARKNESS:
        return
    
    PARTY = []
    
    REALM = RPG_REALM_DARKNESS
    REALMCHARACTERS = DARKCHARACTERS
    FillCharacters()
    
    from playerSettings import PLAYERSETTINGS
    getRealm,lastParty = PLAYERSETTINGS.loadLastParty(RPG_REALM_DARKNESS)
    for lp in lastParty:
        for ci in REALMCHARACTERS:
            if ci.name == lp:
                gotone = True
                PARTY.append(ci)
    
    UpdatePartyList()
def OnRealmFOL():
    global REALM
    global PARTY
    global REALMCHARACTERS

    if REALM == RPG_REALM_LIGHT:
        return

    PARTY = []
    REALM = RPG_REALM_LIGHT
    REALMCHARACTERS = LIGHTCHARACTERS
    FillCharacters()

    from playerSettings import PLAYERSETTINGS
    getRealm, lastParty = PLAYERSETTINGS.loadLastParty(RPG_REALM_LIGHT)
    for lp in lastParty:
        for ci in REALMCHARACTERS:
            if ci.name == lp:
                gotone = True
                PARTY.append(ci)

    UpdatePartyList()
def OnRealmFOL():
    global REALM
    global PARTY
    global REALMCHARACTERS
    
    if REALM == RPG_REALM_LIGHT:
        return
    
    PARTY = []
    REALM = RPG_REALM_LIGHT
    REALMCHARACTERS = LIGHTCHARACTERS
    FillCharacters()
    
    from playerSettings import PLAYERSETTINGS
    getRealm,lastParty = PLAYERSETTINGS.loadLastParty(RPG_REALM_LIGHT)
    for lp in lastParty:
        for ci in REALMCHARACTERS:
            if ci.name == lp:
                gotone = True
                PARTY.append(ci)
    
    UpdatePartyList()
def QueryCharactersResults(results):
    if len(results) == 2:
        cinfos, mspawns = results
        maxparty = 6
    else:
        cinfos, mspawns, maxparty = results

    TGECall("CloseMessagePopup")
    global CHARACTERS
    global LIGHTCHARACTERS
    global DARKCHARACTERS
    global MONSTERCHARACTERS
    global REALMCHARACTERS
    global MONSTERCHOICES
    global DOLASTPARTY
    global MAXPARTY
    global CHARINFOS
    global REALM
    MAXPARTY = maxparty
    CHARACTERS = cinfos

    DARKCHARACTERS = []
    LIGHTCHARACTERS = []
    MONSTERCHARACTERS = []

    MONSTERCHOICES = mspawns

    CHARINFOS = {}
    for ci in cinfos:
        CHARINFOS[ci.name] = ci

        if ci.realm == RPG_REALM_DARKNESS:
            DARKCHARACTERS.append(ci)
        elif ci.realm == RPG_REALM_MONSTER:
            MONSTERCHARACTERS.append(ci)
        else:
            LIGHTCHARACTERS.append(ci)

    gotone = False
    if DOLASTPARTY:
        DOLASTPARTY = False
        from playerSettings import PLAYERSETTINGS
        REALM, lastParty = PLAYERSETTINGS.loadLastParty()

        if REALM == RPG_REALM_DARKNESS:
            TGEObject("REALM_MOD_BUTTON").performClick()
            REALMCHARACTERS = DARKCHARACTERS
        elif REALM == RPG_REALM_MONSTER:
            TGEObject("REALM_MONSTER_BUTTON").performClick()
            REALMCHARACTERS = MONSTERCHARACTERS
        else:
            TGEObject("REALM_FOL_BUTTON").performClick()
            REALMCHARACTERS = LIGHTCHARACTERS

        for lp in lastParty:
            for ci in REALMCHARACTERS:
                if ci.name == lp:
                    gotone = True
                    PARTY.append(ci)
    else:
        if REALM == RPG_REALM_DARKNESS:
            REALMCHARACTERS = DARKCHARACTERS
        elif REALM == RPG_REALM_MONSTER:
            REALMCHARACTERS = MONSTERCHARACTERS
        else:
            REALMCHARACTERS = LIGHTCHARACTERS

    FillCharacters()

    UpdatePartyList()

    #fill monster selection

    tc = TGEObject("MOM_MONSTER_LIST")
    tc.setVisible(False)
    tc.clear()

    for i, name in enumerate(mspawns):
        TGEEval(r'MOM_MONSTER_LIST.addRow(%i,"%s");' % (i, name))

    tc.sort(0)  # this sorts alphabetically
    tc.setSelectedRow(0)
    tc.scrollVisible(0)
    tc.setActive(True)  #this should be based on any worlds found
    tc.setVisible(True)
def QueryCharactersResults(results):
    if len(results) == 2:
        cinfos,mspawns = results
        maxparty = 6
    else:
        cinfos,mspawns,maxparty = results
    
    TGECall("CloseMessagePopup")
    global CHARACTERS
    global LIGHTCHARACTERS
    global DARKCHARACTERS
    global MONSTERCHARACTERS
    global REALMCHARACTERS
    global MONSTERCHOICES
    global DOLASTPARTY
    global MAXPARTY
    global CHARINFOS
    global REALM
    MAXPARTY = maxparty
    CHARACTERS = cinfos
    
    DARKCHARACTERS = []
    LIGHTCHARACTERS = []
    MONSTERCHARACTERS = []
    
    MONSTERCHOICES = mspawns
    
    CHARINFOS = {}
    for ci in cinfos:
        CHARINFOS[ci.name] = ci
        
        if ci.realm == RPG_REALM_DARKNESS:
            DARKCHARACTERS.append(ci)
        elif ci.realm == RPG_REALM_MONSTER:
            MONSTERCHARACTERS.append(ci)
        else:
            LIGHTCHARACTERS.append(ci)
    
    gotone = False
    if DOLASTPARTY:
        DOLASTPARTY = False
        from playerSettings import PLAYERSETTINGS
        REALM,lastParty = PLAYERSETTINGS.loadLastParty()
        
        if REALM == RPG_REALM_DARKNESS:
            TGEObject("REALM_MOD_BUTTON").performClick()
            REALMCHARACTERS = DARKCHARACTERS
        elif REALM == RPG_REALM_MONSTER:
            TGEObject("REALM_MONSTER_BUTTON").performClick()
            REALMCHARACTERS = MONSTERCHARACTERS
        else:
            TGEObject("REALM_FOL_BUTTON").performClick()
            REALMCHARACTERS = LIGHTCHARACTERS
        
        for lp in lastParty:
            for ci in REALMCHARACTERS:
                if ci.name == lp:
                    gotone = True
                    PARTY.append(ci)
    else:
        if REALM == RPG_REALM_DARKNESS:
            REALMCHARACTERS = DARKCHARACTERS
        elif REALM == RPG_REALM_MONSTER:
            REALMCHARACTERS = MONSTERCHARACTERS
        else:
            REALMCHARACTERS = LIGHTCHARACTERS
    
    FillCharacters()
    
    UpdatePartyList()
    
    #fill monster selection
    
    tc = TGEObject("MOM_MONSTER_LIST")
    tc.setVisible(False)
    tc.clear()
    
    for i,name in enumerate(mspawns):
        TGEEval(r'MOM_MONSTER_LIST.addRow(%i,"%s");'%(i,name))
    
    tc.sort(0) # this sorts alphabetically
    tc.setSelectedRow(0)
    tc.scrollVisible(0)
    tc.setActive(True)#this should be based on any worlds found
    tc.setVisible(True)