Skip to content

Commit

Permalink
feat: search option for items under walls/windows/doors (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luan Luciano authored Jun 3, 2024
1 parent cb5c73e commit 6c09718
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
2 changes: 2 additions & 0 deletions data/menubar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<item name="Find $Container" action="SEARCH_ON_MAP_CONTAINER" help="Find all containers on map."/>
<item name="Find $Writeable" action="SEARCH_ON_MAP_WRITEABLE" help="Find all writeable items on map."/>
<item name="Find $Duplicate Items" action="SEARCH_ON_MAP_DUPLICATE" help="Find all positions where there are duplicate items on the map."/>
<item name="Find Walls $Upon Walls" action="SEARCH_ON_MAP_WALLS_UPON_WALLS" help="Find all positions where there are walls/windows/doors on walls/windows/doors on the map."/>
</menu>
<separator/>
<menu name="$Border Options">
Expand Down Expand Up @@ -93,6 +94,7 @@
<item name="Find $Container" action="SEARCH_ON_SELECTION_CONTAINER" help="Find all containers on selected area."/>
<item name="Find $Writeable" action="SEARCH_ON_SELECTION_WRITEABLE" help="Find all writeable items on selected area."/>
<item name="Find $Duplicate Items" action="SEARCH_ON_SELECTION_DUPLICATE" help="Find all positions where there are duplicate items on selected area."/>
<item name="Find Walls $Upon Walls" action="SEARCH_ON_SELECTION_WALLS_UPON_WALLS" help="Find all positions where there are walls/windows/doors on walls/windows/doors on selected area."/>
</menu>
<separator/>
<menu name="$Selection Mode">
Expand Down
3 changes: 3 additions & 0 deletions source/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ class Item : public ItemAttributes {
bool hasElevation() const {
return getItemType().hasElevation;
}
bool isBlockMissiles() const {
return getItemType().blockMissiles;
}

// Wall alignment (vertical, horizontal, pole, corner)
BorderType getWallAlignment() const;
Expand Down
95 changes: 94 additions & 1 deletion source/main_menubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ MainMenuBar::MainMenuBar(MainFrame* frame) :
MAKE_ACTION(REMOVE_ON_MAP_DUPLICATE_ITEMS, wxITEM_NORMAL, OnRemoveForDuplicateItemsOnMap);
MAKE_ACTION(REMOVE_ON_SELECTION_DUPLICATE_ITEMS, wxITEM_NORMAL, OnRemoveForDuplicateItemsOnSelection);

MAKE_ACTION(SEARCH_ON_MAP_WALLS_UPON_WALLS, wxITEM_NORMAL, OnSearchForWallsUponWallsOnMap);
MAKE_ACTION(SEARCH_ON_SELECTION_WALLS_UPON_WALLS, wxITEM_NORMAL, OnSearchForWallsUponWallsOnSelection);

// A deleter, this way the frame does not need
// to bother deleting us.
class CustomMenuBar : public wxMenuBar {
Expand Down Expand Up @@ -419,6 +422,9 @@ void MainMenuBar::Update() {
EnableItem(REMOVE_ON_MAP_DUPLICATE_ITEMS, is_local);
EnableItem(REMOVE_ON_SELECTION_DUPLICATE_ITEMS, is_local && has_selection);

EnableItem(SEARCH_ON_MAP_WALLS_UPON_WALLS, is_host);
EnableItem(SEARCH_ON_SELECTION_WALLS_UPON_WALLS, is_host && has_selection);

UpdateFloorMenu();
UpdateIndicatorsMenu();
}
Expand Down Expand Up @@ -1322,7 +1328,7 @@ namespace OnMapRemoveCorpses {
g_gui.SetLoadDone((unsigned int)(100 * done / map.getTileCount()));
}

return g_materials.isInTileset(item, "Corpses") & !item->isComplex();
return g_materials.isInTileset(item, "Corpses") && !item->isComplex();
}
};
}
Expand Down Expand Up @@ -2273,6 +2279,7 @@ void MainMenuBar::SearchItems(bool unique, bool action, bool container, bool wri

SearchResultWindow* result = g_gui.ShowSearchWindow();
result->Clear();

for (std::vector<std::pair<Tile*, Item*>>::iterator iter = found.begin(); iter != found.end(); ++iter) {
result->AddPosition(searcher.desc(iter->second), iter->first->getPosition());
}
Expand All @@ -2294,6 +2301,14 @@ void MainMenuBar::OnRemoveForDuplicateItemsOnSelection(wxCommandEvent &WXUNUSED(
RemoveDuplicatesItems(true);
}

void MainMenuBar::OnSearchForWallsUponWallsOnMap(wxCommandEvent &WXUNUSED(event)) {
SearchWallsUponWalls(false);
}

void MainMenuBar::OnSearchForWallsUponWallsOnSelection(wxCommandEvent &WXUNUSED(event)) {
SearchWallsUponWalls(true);
}

namespace SearchDuplicatedItems {
struct condition {
std::unordered_set<Tile*> foundTiles;
Expand Down Expand Up @@ -2432,3 +2447,81 @@ void MainMenuBar::RemoveDuplicatesItems(bool onSelection /* = false*/) {
g_gui.GetCurrentMap().doChange();
}
}

namespace SearchWallsUponWalls {
struct condition {
std::unordered_set<Tile*> foundTiles;

void operator()(const Map &map, Tile* tile, const Item* item, long long done) {
if (done % 0x8000 == 0) {
g_gui.SetLoadDone(static_cast<unsigned int>(100 * done / map.getTileCount()));
}

if (!tile) {
return;
}

if (!item) {
return;
}

if (!item->isBlockMissiles()) {
return;
}

if (!item->isWall() && !item->isDoor()) {
return;
}

std::unordered_set<int> itemIDs;
for (const Item* itemInTile : tile->items) {
if (!itemInTile || (!itemInTile->isWall() && !itemInTile->isDoor())) {
continue;
}

if (item->getID() != itemInTile->getID()) {
itemIDs.insert(itemInTile->getID());
}
}

if (!itemIDs.empty()) {
foundTiles.insert(tile);
}

itemIDs.clear();
}
};
}

void MainMenuBar::SearchWallsUponWalls(bool onSelection /* = false*/) {
if (!g_gui.IsEditorOpen()) {
return;
}

if (onSelection) {
g_gui.CreateLoadBar("Searching on selected area...");
} else {
g_gui.CreateLoadBar("Searching on map...");
}

SearchWallsUponWalls::condition finder;

foreach_ItemOnMap(g_gui.GetCurrentMap(), finder, onSelection);

const std::unordered_set<Tile*> &foundTiles = finder.foundTiles;

g_gui.DestroyLoadBar();

size_t setSize = foundTiles.size();

wxString msg;
msg << setSize << " items under walls and doors founded.";

g_gui.PopupDialog("Search completed", msg, wxOK);

SearchResultWindow* result = g_gui.ShowSearchWindow();
result->Clear();
for (const Tile* tile : foundTiles) {
result->AddPosition("Item Under", tile->getPosition());
}
}
5 changes: 5 additions & 0 deletions source/main_menubar.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ namespace MenuBar {
SEARCH_ON_SELECTION_DUPLICATE,
REMOVE_ON_MAP_DUPLICATE_ITEMS,
REMOVE_ON_SELECTION_DUPLICATE_ITEMS,
SEARCH_ON_MAP_WALLS_UPON_WALLS,
SEARCH_ON_SELECTION_WALLS_UPON_WALLS,
};
}

Expand Down Expand Up @@ -302,6 +304,8 @@ class MainMenuBar : public wxEvtHandler {
void OnSearchForDuplicateItemsOnSelection(wxCommandEvent &event);
void OnRemoveForDuplicateItemsOnMap(wxCommandEvent &event);
void OnRemoveForDuplicateItemsOnSelection(wxCommandEvent &event);
void OnSearchForWallsUponWallsOnMap(wxCommandEvent &event);
void OnSearchForWallsUponWallsOnSelection(wxCommandEvent &event);

protected:
// Load and returns a menu item, also sets accelerator
Expand All @@ -311,6 +315,7 @@ class MainMenuBar : public wxEvtHandler {
void SearchItems(bool unique, bool action, bool container, bool writable, bool onSelection = false);
void SearchDuplicatedItems(bool onSelection = false);
void RemoveDuplicatesItems(bool onSelection = false);
void SearchWallsUponWalls(bool onSelection = false);

protected:
MainFrame* frame;
Expand Down

0 comments on commit 6c09718

Please sign in to comment.