Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search tiles by flag #84

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions source/find_item_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ FindItemDialog::FindItemDialog(wxWindow* parent, const wxString &title, bool onl
"Find by Client ID",
"Find by Name",
"Find by Types",
"Find by Tile Types",
"Find by Properties" };

int radio_boxNChoices = sizeof(radio_boxChoices) / sizeof(wxString);
Expand Down Expand Up @@ -104,6 +105,19 @@ FindItemDialog::FindItemDialog(wxWindow* parent, const wxString &title, bool onl

box_sizer->Add(type_box_sizer, 1, wxALL | wxEXPAND, 5);

// --------------- Tile Types ---------------

wxString tileTypesChoices[] = { "PZ",
"PVP",
"No PVP",
"No Logout" };

int tileTypesChoicesCount = sizeof(tileTypesChoices) / sizeof(wxString);
tileTypesRadioBox = newd wxRadioBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, tileTypesChoicesCount, tileTypesChoices, 1, wxRA_SPECIFY_COLS);
tileTypesRadioBox->SetSelection(0);
tileTypesRadioBox->Enable(false);
type_box_sizer->Add(tileTypesRadioBox, 0, wxALL | wxEXPAND, 5);

// --------------- Properties ---------------

wxStaticBoxSizer* properties_box_sizer = newd wxStaticBoxSizer(newd wxStaticBox(this, wxID_ANY, "Properties"), wxVERTICAL);
Expand Down Expand Up @@ -180,6 +194,7 @@ FindItemDialog::FindItemDialog(wxWindow* parent, const wxString &title, bool onl
name_text_input->Connect(wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(FindItemDialog::OnText), nullptr, this);

types_radio_box->Connect(wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEventHandler(FindItemDialog::OnTypeChange), nullptr, this);
tileTypesRadioBox->Connect(wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEventHandler(FindItemDialog::OnTypeChange), nullptr, this);

unpassable->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindItemDialog::OnPropertyChange), nullptr, this);
unmovable->Connect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindItemDialog::OnPropertyChange), nullptr, this);
Expand Down Expand Up @@ -208,6 +223,7 @@ FindItemDialog::~FindItemDialog() {
name_text_input->Disconnect(wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(FindItemDialog::OnText), nullptr, this);

types_radio_box->Disconnect(wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEventHandler(FindItemDialog::OnTypeChange), nullptr, this);
tileTypesRadioBox->Disconnect(wxEVT_COMMAND_RADIOBOX_SELECTED, wxCommandEventHandler(FindItemDialog::OnTypeChange), nullptr, this);

unpassable->Disconnect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindItemDialog::OnPropertyChange), nullptr, this);
unmovable->Disconnect(wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler(FindItemDialog::OnPropertyChange), nullptr, this);
Expand All @@ -227,18 +243,23 @@ FindItemDialog::~FindItemDialog() {
}

FindItemDialog::SearchMode FindItemDialog::getSearchMode() const {
return (SearchMode)options_radio_box->GetSelection();
return static_cast<SearchMode>(options_radio_box->GetSelection());
}

FindItemDialog::SearchTileType FindItemDialog::getSearchTileType() const {
return static_cast<SearchTileType>(tileTypesRadioBox->GetSelection());
}

void FindItemDialog::setSearchMode(FindItemDialog::SearchMode mode) {
if ((SearchMode)options_radio_box->GetSelection() != mode) {
void FindItemDialog::setSearchMode(SearchMode mode) {
if (static_cast<SearchMode>(options_radio_box->GetSelection()) != mode) {
options_radio_box->SetSelection(mode);
}

server_id_spin->Enable(mode == SearchMode::ServerIDs);
client_id_spin->Enable(mode == SearchMode::ClientIDs);
name_text_input->Enable(mode == SearchMode::Names);
types_radio_box->Enable(mode == SearchMode::Types);
tileTypesRadioBox->Enable(mode == SearchMode::TileTypes);
EnableProperties(mode == SearchMode::Properties);
RefreshContentsInternal();

Expand Down Expand Up @@ -393,9 +414,9 @@ void FindItemDialog::RefreshContentsInternal() {
}
}

ok_button->Enable(found_search_results || selection == SearchMode::TileTypes);
if (found_search_results) {
items_list->SetSelection(0);
ok_button->Enable(true);
} else {
items_list->SetNoMatches();
}
Expand All @@ -404,7 +425,7 @@ void FindItemDialog::RefreshContentsInternal() {
}

void FindItemDialog::OnOptionChange(wxCommandEvent &WXUNUSED(event)) {
setSearchMode((SearchMode)options_radio_box->GetSelection());
setSearchMode(static_cast<SearchMode>(options_radio_box->GetSelection()));
}

void FindItemDialog::OnServerIdChange(wxCommandEvent &WXUNUSED(event)) {
Expand Down Expand Up @@ -432,14 +453,18 @@ void FindItemDialog::OnInputTimer(wxTimerEvent &WXUNUSED(event)) {
}

void FindItemDialog::OnClickOK(wxCommandEvent &WXUNUSED(event)) {
if (items_list->GetItemCount() != 0) {
if (items_list->GetItemCount() != 0 && !tileTypesRadioBox->IsEnabled()) {
Brush* brush = items_list->GetSelectedBrush();
if (brush) {
result_brush = brush;
result_id = brush->asRaw()->getItemID();
EndModal(wxID_OK);
g_gui.SelectBrush(brush->asRaw(), TILESET_RAW);
}
} else if (tileTypesRadioBox->IsEnabled()) {
result_brush = nullptr;
result_id = 0;
EndModal(wxID_OK);
}
}

Expand Down
12 changes: 11 additions & 1 deletion source/find_item_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ class FindItemDialog : public wxDialog {
ClientIDs,
Names,
Types,
TileTypes,
Properties,
};

enum SearchTileType {
ProtectionZone,
PlayerVsPlayer,
NoPlayerVsPlayer,
NoLogout
};

enum SearchItemType {
Depot,
Mailbox,
Expand All @@ -62,6 +70,7 @@ class FindItemDialog : public wxDialog {
}

SearchMode getSearchMode() const;
SearchTileType getSearchTileType() const;
void setSearchMode(SearchMode mode);

private:
Expand All @@ -81,6 +90,7 @@ class FindItemDialog : public wxDialog {
wxRadioBox* options_radio_box;

wxRadioBox* types_radio_box;
wxRadioBox* tileTypesRadioBox;

wxSpinCtrl* server_id_spin;
wxSpinCtrl* client_id_spin;
Expand All @@ -107,7 +117,7 @@ class FindItemDialog : public wxDialog {
wxButton* ok_button;
wxButton* cancel_button;
Brush* result_brush;
uint16_t result_id;
uint16_t result_id = 0;
bool only_pickupables;

DECLARE_EVENT_TABLE()
Expand Down
79 changes: 69 additions & 10 deletions source/main_menubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,10 @@ void MainMenuBar::OnRedo(wxCommandEvent &WXUNUSED(event)) {

namespace OnSearchForItem {
struct Finder {
Finder(uint16_t itemId, uint32_t maxCount) :
itemId(itemId), maxCount(maxCount) { }
Finder(uint16_t itemId, uint32_t maxCount, bool findTile = false) :
itemId(itemId), maxCount(maxCount), findTile(findTile) { }

bool findTile = false;
uint16_t itemId;
uint32_t maxCount;
std::vector<std::pair<Tile*, Item*>> result;
Expand All @@ -925,6 +926,41 @@ namespace OnSearchForItem {
if (item->getID() == itemId) {
result.push_back(std::make_pair(tile, item));
}

if (!findTile) {
return;
}

if (tile->isHouseTile()) {
return;
}

const auto &tileSearchType = static_cast<FindItemDialog::SearchTileType>(g_settings.getInteger(Config::FIND_TILE_TYPE));
if (tileSearchType == FindItemDialog::SearchTileType::NoLogout && !tile->isNoLogout()) {
return;
}

if (tileSearchType == FindItemDialog::SearchTileType::PlayerVsPlayer && !tile->isPVP()) {
return;
}

if (tileSearchType == FindItemDialog::SearchTileType::NoPlayerVsPlayer && !tile->isNoPVP()) {
return;
}

if (tileSearchType == FindItemDialog::SearchTileType::ProtectionZone && !tile->isPZ()) {
return;
}

const auto it = std::ranges::find_if(result, [&tile](const auto &pair) {
return pair.first == tile;
});

if (it != result.end()) {
return;
}

result.push_back(std::make_pair(tile, nullptr));
}
};
}
Expand All @@ -934,10 +970,16 @@ void MainMenuBar::OnSearchForItem(wxCommandEvent &WXUNUSED(event)) {
return;
}

const auto &searchMode = static_cast<FindItemDialog::SearchMode>(g_settings.getInteger(Config::FIND_ITEM_MODE));

FindItemDialog dialog(frame, "Search for Item");
dialog.setSearchMode((FindItemDialog::SearchMode)g_settings.getInteger(Config::FIND_ITEM_MODE));
dialog.setSearchMode(searchMode);
if (dialog.ShowModal() == wxID_OK) {
OnSearchForItem::Finder finder(dialog.getResultID(), (uint32_t)g_settings.getInteger(Config::REPLACE_SIZE));
g_settings.setInteger(Config::FIND_ITEM_MODE, static_cast<int>(dialog.getSearchMode()));
g_settings.setInteger(Config::FIND_TILE_TYPE, static_cast<int>(dialog.getSearchTileType()));

OnSearchForItem::Finder finder(dialog.getResultID(), (uint32_t)g_settings.getInteger(Config::REPLACE_SIZE), dialog.getSearchMode() == FindItemDialog::SearchMode::TileTypes);

g_gui.CreateLoadBar("Searching map...");

foreach_ItemOnMap(g_gui.GetCurrentMap(), finder, false);
Expand All @@ -953,13 +995,30 @@ void MainMenuBar::OnSearchForItem(wxCommandEvent &WXUNUSED(event)) {

SearchResultWindow* window = g_gui.ShowSearchWindow();
window->Clear();
for (std::vector<std::pair<Tile*, Item*>>::const_iterator iter = result.begin(); iter != result.end(); ++iter) {
Tile* tile = iter->first;
Item* item = iter->second;
window->AddPosition(wxstr(item->getName()), tile->getPosition());
}

g_settings.setInteger(Config::FIND_ITEM_MODE, (int)dialog.getSearchMode());
const auto &searchTileType = dialog.getSearchTileType();

for (auto it = result.begin(); it != result.end(); ++it) {
const auto &tile = it->first;

if (dialog.getSearchMode() == FindItemDialog::SearchMode::TileTypes) {
wxString tileType;

if (tile->isNoLogout() && searchTileType == FindItemDialog::SearchTileType::NoLogout) {
tileType = "No Logout";
} else if (tile->isPVP() && searchTileType == FindItemDialog::SearchTileType::PlayerVsPlayer) {
tileType = "PVP";
} else if (tile->isNoPVP() && searchTileType == FindItemDialog::SearchTileType::NoPlayerVsPlayer) {
tileType = "No PVP";
} else if (tile->isPZ() && searchTileType == FindItemDialog::SearchTileType::ProtectionZone) {
tileType = "PZ";
}

window->AddPosition(tileType, tile->getPosition());
} else {
window->AddPosition(wxstr(it->second->getName()), tile->getPosition());
}
}
}
dialog.Destroy();
}
Expand Down
1 change: 1 addition & 0 deletions source/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ void Settings::IO(IOMode mode) {
String(RECENT_EDITED_MAP_POSITION, "");

Int(FIND_ITEM_MODE, 0);
Int(FIND_TILE_TYPE, 0);
Int(JUMP_TO_ITEM_MODE, 0);

#undef section
Expand Down
1 change: 1 addition & 0 deletions source/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ namespace Config {
RECENT_EDITED_MAP_PATH,
RECENT_EDITED_MAP_POSITION,

FIND_TILE_TYPE,
FIND_ITEM_MODE,
JUMP_TO_ITEM_MODE,

Expand Down
14 changes: 13 additions & 1 deletion source/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,20 @@ class Tile {
return testFlags(statflags, TILESTATE_BLOCKING);
}

// PVP
bool isPVP() const noexcept {
return mapflags & TILESTATE_PVPZONE;
}
// NO PVP
bool isNoPVP() const noexcept {
return mapflags & TILESTATE_NOPVP;
}
// PZ
bool isNoLogout() const noexcept {
return mapflags & TILESTATE_NOLOGOUT;
}
// PZ
bool isPZ() const {
bool isPZ() const noexcept {
return testFlags(mapflags, TILESTATE_PROTECTIONZONE);
}
void setPZ(bool pz) {
Expand Down
Loading