void auto_pickup::add_rule(const std::string &sRule) { vRules[CHARACTER].push_back(cRules(sRule, true, false)); merge_vector(); create_rules(); if (!OPTIONS["AUTO_PICKUP"] && query_yn(_("Autopickup is not enabled in the options. Enable it now?")) ) { OPTIONS["AUTO_PICKUP"].setNext(); get_options().save(); } }
void auto_pickup::merge_vector() { vRules[MERGED].clear(); for (unsigned i = GLOBAL; i <= CHARACTER; i++) { //Loop through global 1 and character 2 for (auto it = vRules[i].begin(); it != vRules[i].end(); ++it) { if (it->sRule != "") { vRules[MERGED].push_back(cRules(it->sRule, it->bActive, it->bExclude)); } } } }
void auto_pickup::deserialize(JsonIn &jsin) { vRules[(bChar) ? CHARACTER : GLOBAL].clear(); jsin.start_array(); while (!jsin.end_array()) { JsonObject jo = jsin.get_object(); const std::string sRule = jo.get_string("rule"); const bool bActive = jo.get_bool("active"); const bool bExclude = jo.get_bool("exclude"); vRules[(bChar) ? CHARACTER : GLOBAL].push_back(cRules(sRule, bActive, bExclude)); } }
bool auto_pickup::load_legacy(const bool bCharacter) { std::ifstream fin; std::string sFile = FILENAMES["legacy_autopickup2"]; if (bCharacter) { sFile = world_generator->active_world->world_path + "/" + base64_encode(g->u.name) + ".apu.txt"; } fin.open(sFile.c_str()); if(!fin.is_open()) { if( !bCharacter ) { fin.open(FILENAMES["legacy_autopickup"].c_str()); if( !fin.is_open() ) { DebugLog( D_ERROR, DC_ALL ) << "Could neither read nor create " << sFile; return false; } } else { return false; } } vRules[(bCharacter) ? CHARACTER : GLOBAL].clear(); std::string sLine; while(!fin.eof()) { getline(fin, sLine); if(sLine != "" && sLine[0] != '#') { int iNum = std::count(sLine.begin(), sLine.end(), ';'); if(iNum != 2) { DebugLog( D_ERROR, DC_ALL ) << "Bad Rule: " << sLine; } else { std::string sRule = ""; bool bActive = true; bool bExclude = false; size_t iPos = 0; int iCol = 1; do { iPos = sLine.find(";"); std::string sTemp = (iPos == std::string::npos) ? sLine : sLine.substr(0, iPos); if (iCol == 1) { sRule = sTemp; } else if (iCol == 2) { bActive = sTemp == "T" || sTemp == "True"; } else if (iCol == 3) { bExclude = sTemp == "T" || sTemp == "True"; } iCol++; if (iPos != std::string::npos) { sLine = sLine.substr(iPos + 1, sLine.size()); } } while(iPos != std::string::npos); vRules[(bCharacter) ? CHARACTER : GLOBAL].push_back(cRules(sRule, bActive, bExclude)); } } } return true; }
void auto_pickup::show( const std::string &custom_name, bool is_autopickup ) { save_reset_changes(false); const int iHeaderHeight = 4; const int iContentHeight = FULL_SCREEN_HEIGHT - 2 - iHeaderHeight; const int iOffsetX = (TERMX > FULL_SCREEN_WIDTH) ? (TERMX - FULL_SCREEN_WIDTH) / 2 : 0; const int iOffsetY = (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY - FULL_SCREEN_HEIGHT) / 2 : 0; std::map<int, bool> mapLines; mapLines[4] = true; mapLines[50] = true; mapLines[54] = true; const int iTotalCols = mapLines.size() - 1; WINDOW *w_help = newwin((FULL_SCREEN_HEIGHT / 2) - 2, FULL_SCREEN_WIDTH * 3 / 4, 7 + iOffsetY + (FULL_SCREEN_HEIGHT / 2) / 2, iOffsetX + 19 / 2); WINDOW_PTR w_helpptr( w_help ); WINDOW *w_border = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, iOffsetY, iOffsetX); WINDOW_PTR w_borderptr( w_border ); WINDOW *w_header = newwin(iHeaderHeight, FULL_SCREEN_WIDTH - 2, 1 + iOffsetY, 1 + iOffsetX); WINDOW_PTR w_headerptr( w_header ); WINDOW *w = newwin(iContentHeight, FULL_SCREEN_WIDTH - 2, iHeaderHeight + 1 + iOffsetY, 1 + iOffsetX); WINDOW_PTR wptr( w ); draw_border(w_border); mvwputch(w_border, 3, 0, c_ltgray, LINE_XXXO); // |- mvwputch(w_border, 3, 79, c_ltgray, LINE_XOXX); // -| for( auto &mapLine : mapLines ) { mvwputch( w_border, FULL_SCREEN_HEIGHT - 1, mapLine.first + 1, c_ltgray, LINE_XXOX ); // _|_ } mvwprintz(w_border, 0, 29, c_ltred, custom_name.c_str() ); wrefresh(w_border); int tmpx = 0; tmpx += shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<A>dd")) + 2; tmpx += shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<R>emove")) + 2; tmpx += shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<C>opy")) + 2; tmpx += shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<M>ove")) + 2; tmpx += shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<E>nable")) + 2; tmpx += shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<D>isable")) + 2; shortcut_print(w_header, 0, tmpx, c_white, c_ltgreen, _("<T>est")); tmpx = 0; tmpx += shortcut_print(w_header, 1, tmpx, c_white, c_ltgreen, _("<+-> Move up/down")) + 2; tmpx += shortcut_print(w_header, 1, tmpx, c_white, c_ltgreen, _("<Enter>-Edit")) + 2; shortcut_print(w_header, 1, tmpx, c_white, c_ltgreen, _("<Tab>-Switch Page")); for (int i = 0; i < 78; i++) { if (mapLines[i]) { mvwputch(w_header, 2, i, c_ltgray, LINE_OXXX); mvwputch(w_header, 3, i, c_ltgray, LINE_XOXO); } else { mvwputch(w_header, 2, i, c_ltgray, LINE_OXOX); // Draw line under header } } mvwprintz(w_header, 3, 1, c_white, "#"); mvwprintz(w_header, 3, 8, c_white, _("Rules")); mvwprintz(w_header, 3, 51, c_white, _("I/E")); wrefresh(w_header); int iCurrentPage = 1; int iCurrentLine = 0; int iCurrentCol = 1; int iStartPos = 0; bool bStuffChanged = false; input_context ctxt("AUTO_PICKUP"); ctxt.register_cardinal(); ctxt.register_action("CONFIRM"); ctxt.register_action("QUIT"); ctxt.register_action("NEXT_TAB"); ctxt.register_action("PREV_TAB"); ctxt.register_action("ADD_RULE"); ctxt.register_action("REMOVE_RULE"); ctxt.register_action("COPY_RULE"); ctxt.register_action("ENABLE_RULE"); ctxt.register_action("DISABLE_RULE"); ctxt.register_action("MOVE_RULE_UP"); ctxt.register_action("MOVE_RULE_DOWN"); ctxt.register_action("TEST_RULE"); ctxt.register_action("HELP_KEYBINDINGS"); if( is_autopickup ) { ctxt.register_action("SWITCH_AUTO_PICKUP_OPTION"); ctxt.register_action("SWAP_RULE_GLOBAL_CHAR"); } std::ostringstream sTemp; while(true) { int locx = 17; locx += shortcut_print(w_header, 2, locx, c_white, (iCurrentPage == 1) ? hilite(c_white) : c_white, _("[<Global>]")) + 1; shortcut_print(w_header, 2, locx, c_white, (iCurrentPage == 2) ? hilite(c_white) : c_white, _("[<Character>]")); locx = 55; mvwprintz(w_header, 0, locx, c_white, _("Auto pickup enabled:")); locx += shortcut_print(w_header, 1, locx, ((OPTIONS["AUTO_PICKUP"]) ? c_ltgreen : c_ltred), c_white, ((OPTIONS["AUTO_PICKUP"]) ? _("True") : _("False"))); locx += shortcut_print(w_header, 1, locx, c_white, c_ltgreen, " "); locx += shortcut_print(w_header, 1, locx, c_white, c_ltgreen, _("<S>witch")); shortcut_print(w_header, 1, locx, c_white, c_ltgreen, " "); wrefresh(w_header); // Clear the lines for (int i = 0; i < iContentHeight; i++) { for (int j = 0; j < 79; j++) { if (mapLines[j]) { mvwputch(w, i, j, c_ltgray, LINE_XOXO); } else { mvwputch(w, i, j, c_black, ' '); } } } const bool currentPageNonEmpty = !vRules[iCurrentPage].empty(); if (iCurrentPage == 2 && g->u.name == "") { vRules[2].clear(); mvwprintz(w, 8, 15, c_white, _("Please load a character first to use this page!")); } draw_scrollbar(w_border, iCurrentLine, iContentHeight, vRules[iCurrentPage].size(), 5); wrefresh(w_border); calcStartPos(iStartPos, iCurrentLine, iContentHeight, vRules[iCurrentPage].size()); // display auto pickup for (int i = iStartPos; i < (int)vRules[iCurrentPage].size(); i++) { if (i >= iStartPos && i < iStartPos + ((iContentHeight > (int)vRules[iCurrentPage].size()) ? (int)vRules[iCurrentPage].size() : iContentHeight)) { nc_color cLineColor = (vRules[iCurrentPage][i].bActive) ? c_white : c_ltgray; sTemp.str(""); sTemp << i + 1; mvwprintz(w, i - iStartPos, 1, cLineColor, "%s", sTemp.str().c_str()); mvwprintz(w, i - iStartPos, 5, cLineColor, ""); if (iCurrentLine == i) { wprintz(w, c_yellow, ">> "); } else { wprintz(w, c_yellow, " "); } wprintz(w, (iCurrentLine == i && iCurrentCol == 1) ? hilite(cLineColor) : cLineColor, "%s", ((vRules[iCurrentPage][i].sRule == "") ? _("<empty rule>") : vRules[iCurrentPage][i].sRule).c_str()); mvwprintz(w, i - iStartPos, 52, (iCurrentLine == i && iCurrentCol == 2) ? hilite(cLineColor) : cLineColor, "%s", ((vRules[iCurrentPage][i].bExclude) ? rm_prefix(_("<Exclude>E")).c_str() : rm_prefix( _("<Include>I")).c_str())); } } wrefresh(w); const std::string action = ctxt.handle_input(); if (action == "NEXT_TAB") { iCurrentPage++; if (iCurrentPage > 2) { iCurrentPage = 1; iCurrentLine = 0; } } else if (action == "PREV_TAB") { iCurrentPage--; if (iCurrentPage < 1) { iCurrentPage = 2; iCurrentLine = 0; } } else if (action == "QUIT") { break; } else if (iCurrentPage == 2 && g->u.name.empty()) { //Only allow loaded games to use the char sheet } else if (action == "DOWN") { iCurrentLine++; iCurrentCol = 1; if (iCurrentLine >= (int)vRules[iCurrentPage].size()) { iCurrentLine = 0; } } else if (action == "UP") { iCurrentLine--; iCurrentCol = 1; if (iCurrentLine < 0) { iCurrentLine = vRules[iCurrentPage].size() - 1; } } else if (action == "ADD_RULE") { bStuffChanged = true; vRules[iCurrentPage].push_back(cRules("", true, false)); iCurrentLine = vRules[iCurrentPage].size() - 1; } else if (action == "REMOVE_RULE" && currentPageNonEmpty) { bStuffChanged = true; vRules[iCurrentPage].erase(vRules[iCurrentPage].begin() + iCurrentLine); if (iCurrentLine > (int)vRules[iCurrentPage].size() - 1) { iCurrentLine--; } if(iCurrentLine < 0){ iCurrentLine = 0; } } else if (action == "COPY_RULE" && currentPageNonEmpty) { bStuffChanged = true; vRules[iCurrentPage].push_back(cRules( vRules[iCurrentPage][iCurrentLine].sRule, vRules[iCurrentPage][iCurrentLine].bActive, vRules[iCurrentPage][iCurrentLine].bExclude)); iCurrentLine = vRules[iCurrentPage].size() - 1; } else if (action == "SWAP_RULE_GLOBAL_CHAR" && currentPageNonEmpty) { if ((iCurrentPage == 1 && g->u.name != "") || iCurrentPage == 2) { bStuffChanged = true; //copy over vRules[(iCurrentPage == 1) ? 2 : 1].push_back(cRules( vRules[iCurrentPage][iCurrentLine].sRule, vRules[iCurrentPage][iCurrentLine].bActive, vRules[iCurrentPage][iCurrentLine].bExclude)); //remove old vRules[iCurrentPage].erase(vRules[iCurrentPage].begin() + iCurrentLine); iCurrentLine = vRules[(iCurrentPage == 1) ? 2 : 1].size() - 1; iCurrentPage = (iCurrentPage == 1) ? 2 : 1; } } else if (action == "CONFIRM" && currentPageNonEmpty) { bStuffChanged = true; if (iCurrentCol == 1) { fold_and_print(w_help, 1, 1, 999, c_white, _( "* is used as a Wildcard. A few Examples:\n" "\n" "wooden arrow matches the itemname exactly\n" "wooden ar* matches items beginning with wood ar\n" "*rrow matches items ending with rrow\n" "*avy fle*fi*arrow multiple * are allowed\n" "heAVY*woOD*arrOW case insensitive search\n" "") ); draw_border(w_help); wrefresh(w_help); vRules[iCurrentPage][iCurrentLine].sRule = trim_rule(string_input_popup(_("Pickup Rule:"), 30, vRules[iCurrentPage][iCurrentLine].sRule)); } else if (iCurrentCol == 2) { vRules[iCurrentPage][iCurrentLine].bExclude = !vRules[iCurrentPage][iCurrentLine].bExclude; } } else if (action == "ENABLE_RULE" && currentPageNonEmpty) { bStuffChanged = true; vRules[iCurrentPage][iCurrentLine].bActive = true; } else if (action == "DISABLE_RULE" && currentPageNonEmpty) { bStuffChanged = true; vRules[iCurrentPage][iCurrentLine].bActive = false; } else if (action == "LEFT") { iCurrentCol--; if (iCurrentCol < 1) { iCurrentCol = iTotalCols; } } else if (action == "RIGHT") { iCurrentCol++; if (iCurrentCol > iTotalCols) { iCurrentCol = 1; } } else if (action == "MOVE_RULE_UP" && currentPageNonEmpty) { bStuffChanged = true; if (iCurrentLine < (int)vRules[iCurrentPage].size() - 1) { std::swap(vRules[iCurrentPage][iCurrentLine], vRules[iCurrentPage][iCurrentLine + 1]); iCurrentLine++; iCurrentCol = 1; } } else if (action == "MOVE_RULE_DOWN" && currentPageNonEmpty) { bStuffChanged = true; if (iCurrentLine > 0) { std::swap(vRules[iCurrentPage][iCurrentLine], vRules[iCurrentPage][iCurrentLine - 1]); iCurrentLine--; iCurrentCol = 1; } } else if (action == "TEST_RULE" && currentPageNonEmpty) { test_pattern(iCurrentPage, iCurrentLine); } else if (action == "SWITCH_OPTION") { // @todo Now that NPCs use this function, it could be used for them too OPTIONS["AUTO_PICKUP"].setNext(); get_options().save(); } } if( !bStuffChanged ) { return; } if( query_yn( _("Save changes?") ) ) { // NPC pickup rules don't need to be saved explicitly if( is_autopickup ) { save( false ); if( g->u.name != "" ) { save(true); } } else { merge_vector(); create_rules(); } } else { save_reset_changes( true ); } }