Skip to content

Commit

Permalink
Merge branch 'main' into tilesets-manager
Browse files Browse the repository at this point in the history
  • Loading branch information
phacUFPE committed Jun 4, 2024
2 parents a072703 + 0a0acba commit 76e5202
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 8 deletions.
2 changes: 2 additions & 0 deletions data/menubar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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 @@ -94,6 +95,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
12 changes: 7 additions & 5 deletions source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ void Editor::saveMap(FileName filename, bool showdialog) {

// Move to permanent backup
if (!save_as && g_settings.getInteger(Config::ALWAYS_MAKE_BACKUP)) {
std::string backup_path = map_path + "backups/";
ensureBackupDirectoryExists(backup_path);
// Move temporary backups to their proper files
time_t t = time(nullptr);
tm* current_time = localtime(&t);
Expand All @@ -437,31 +439,31 @@ void Editor::saveMap(FileName filename, bool showdialog) {

if (!backup_otbm.empty()) {
converter.SetFullName(wxstr(savefile));
std::string otbm_filename = map_path + nstr(converter.GetName());
std::string otbm_filename = backup_path + nstr(converter.GetName());
std::rename(backup_otbm.c_str(), std::string(otbm_filename + "." + date.str() + (save_otgz ? ".otgz" : ".otbm")).c_str());
}

if (!backup_house.empty()) {
converter.SetFullName(wxstr(map.housefile));
std::string house_filename = map_path + nstr(converter.GetName());
std::string house_filename = backup_path + nstr(converter.GetName());
std::rename(backup_house.c_str(), std::string(house_filename + "." + date.str() + ".xml").c_str());
}

if (!backup_spawn.empty()) {
converter.SetFullName(wxstr(map.spawnmonsterfile));
std::string spawn_filename = map_path + nstr(converter.GetName());
std::string spawn_filename = backup_path + nstr(converter.GetName());
std::rename(backup_spawn.c_str(), std::string(spawn_filename + "." + date.str() + ".xml").c_str());
}

if (!backup_spawn_npc.empty()) {
converter.SetFullName(wxstr(map.spawnnpcfile));
std::string spawnnpc_filename = map_path + nstr(converter.GetName());
std::string spawnnpc_filename = backup_path + nstr(converter.GetName());
std::rename(backup_spawn_npc.c_str(), std::string(spawnnpc_filename + "." + date.str() + ".xml").c_str());
}

if (!backup_zones.empty()) {
converter.SetFullName(wxstr(map.zonefile));
std::string zones_filename = map_path + nstr(converter.GetName());
std::string zones_filename = backup_path + nstr(converter.GetName());
std::rename(backup_zones.c_str(), std::string(zones_filename + "." + date.str() + ".xml").c_str());
}
} else {
Expand Down
8 changes: 8 additions & 0 deletions source/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Editor {
void undraw(const PositionVector &posvec, bool alt);
void undraw(const PositionVector &todraw, PositionVector &toborder, bool alt);

void ensureBackupDirectoryExists(const std::string &backup_path);

protected:
void drawInternal(const Position offset, bool alt, bool dodraw);
void drawInternal(const PositionVector &posvec, bool alt, bool dodraw);
Expand Down Expand Up @@ -183,4 +185,10 @@ inline void Editor::undraw(const PositionVector &todraw, PositionVector &toborde
drawInternal(todraw, toborder, alt, false);
}

inline void Editor::ensureBackupDirectoryExists(const std::string &backup_path) {
if (!std::filesystem::exists(backup_path)) {
std::filesystem::create_directory(backup_path);
}
}

#endif
1 change: 1 addition & 0 deletions source/find_item_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ void FindItemDialog::OnClickOK(wxCommandEvent &WXUNUSED(event)) {
result_brush = brush;
result_id = brush->asRaw()->getItemID();
EndModal(wxID_OK);
g_gui.SelectBrush(brush->asRaw(), TILESET_RAW);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/iominimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ bool IOMinimap::exportMinimap(const std::string &directory) {
wxBitmapType type = m_format == MinimapExportFormat::Png ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_BMP;
wxString extension_wx = wxString::FromAscii(extension.mb_str());
wxFileName file = wxString::Format("%s-%s-%s.%s", std::to_string(h), std::to_string(w), std::to_string(z), extension_wx);
file.Normalize(wxPATH_NORM_ALL, directory);
file.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_CASE, directory);
image->SaveFile(file.GetFullPath(), type);
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ bool IOMinimap::exportSelection(const std::string &directory, const std::string
wxString extension = m_format == MinimapExportFormat::Png ? "png" : "bmp";
wxBitmapType type = m_format == MinimapExportFormat::Png ? wxBITMAP_TYPE_PNG : wxBITMAP_TYPE_BMP;
wxFileName file = wxString::Format("%s-%d.%s", name, z, extension);
file.Normalize(wxPATH_NORM_ALL, directory);
file.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_CASE, directory);
image->SaveFile(file.GetFullPath(), type);
}
}
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 @@ -207,6 +207,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 @@ -421,6 +424,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 @@ -1332,7 +1338,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 @@ -2283,6 +2289,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 @@ -2304,6 +2311,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 @@ -2442,3 +2457,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 @@ -162,6 +162,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 @@ -304,6 +306,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 @@ -313,6 +317,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 76e5202

Please sign in to comment.