Skip to content

Commit

Permalink
Issue DFHack#1262. Added Items::getTitle and used in stocks
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikLundell committed Jan 25, 2020
1 parent 82f082d commit c6bbf39
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
======== IMPORTANT: rename this, and add a new "future" section, BEFORE ========
======== making a new DFHack release! ========
================================================================================
# Future

## Fixes
- `stocks`: fixed display of titles by using the new ``Items::`getTitle`` operation before the original one, issue 1262

## API
- Added ``Items::`getTitle`` to get titles of "books". Catches titles buried in improvements, unlike getDescription.

# 0.44.12-r3

## New Plugins
Expand Down
5 changes: 5 additions & 0 deletions library/include/modules/Items.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ DFHACK_EXPORT df::unit *getHolderUnit(df::item *item);
/// Returns the true position of the item.
DFHACK_EXPORT df::coord getPosition(df::item *item);

/// Returns the title of a codex or "tool", either as the codex title or as the title of the
/// first page or writing it has that has a non blank title. An empty string is returned if
/// no title is found (which is the casee for everything that isn't a "book").
DFHACK_EXPORT std::string getTitle(df::item *item);

/// Returns the description string of the item.
DFHACK_EXPORT std::string getDescription(df::item *item, int type = 0, bool decorate = false);

Expand Down
81 changes: 81 additions & 0 deletions library/modules/Items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ using namespace std;
#include "df/general_ref_unit_holderst.h"
#include "df/historical_entity.h"
#include "df/item.h"
#include "df/item_bookst.h"
#include "df/item_toolst.h"
#include "df/item_type.h"
#include "df/itemdef_ammost.h"
#include "df/itemdef_armorst.h"
Expand All @@ -74,6 +76,9 @@ using namespace std;
#include "df/itemdef_toyst.h"
#include "df/itemdef_trapcompst.h"
#include "df/itemdef_weaponst.h"
#include "df/itemimprovement.h"
#include "df/itemimprovement_pagesst.h"
#include "df/itemimprovement_writingst.h"
#include "df/job_item.h"
#include "df/mandate.h"
#include "df/map_block.h"
Expand All @@ -90,6 +95,7 @@ using namespace std;
#include "df/viewscreen_itemst.h"
#include "df/world.h"
#include "df/world_site.h"
#include "df/written_content.h"

using namespace DFHack;
using namespace df::enums;
Expand Down Expand Up @@ -681,6 +687,81 @@ static void addQuality(std::string &tmp, int quality)
}
}

// It's not impossible the functionality of this operation is provided by one of the unmapped item functions.
std::string Items::getTitle(df::item *item)
{
CHECK_NULL_POINTER(item);

std::string tmp;

if (item->getType() == df::item_type::BOOK)
{
if (virtual_cast<df::item_bookst>(item)->title != "")
{
return virtual_cast<df::item_bookst>(item)->title;
}
else
{
for (size_t i = 0; i < virtual_cast<df::item_bookst>(item)->improvements.size(); i++)
{
if (virtual_cast<df::item_bookst>(item)->improvements[i]->getType() == df::improvement_type::PAGES)
{
for (size_t k = 0; k < virtual_cast<df::itemimprovement_pagesst>(virtual_cast<df::item_bookst>(item)->improvements[i])->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[virtual_cast<df::itemimprovement_pagesst>(virtual_cast<df::item_bookst>(item)->improvements[i])->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
else if (virtual_cast<df::item_bookst>(item)->improvements[i]->getType() == df::improvement_type::WRITING)
{
for (size_t k = 0; k < virtual_cast<df::itemimprovement_writingst>(virtual_cast<df::item_bookst>(item)->improvements[i])->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[virtual_cast<df::itemimprovement_writingst>(virtual_cast<df::item_bookst>(item)->improvements[i])->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
}
}
}
else if (item->getType() == df::item_type::TOOL &&
virtual_cast<df::item_toolst>(item)->hasToolUse(df::tool_uses::CONTAIN_WRITING))
{
for (size_t i = 0; i < virtual_cast<df::item_toolst>(item)->improvements.size(); i++)
{
if (virtual_cast<df::item_toolst>(item)->improvements[i]->getType() == df::improvement_type::PAGES)
{
for (size_t k = 0; k < virtual_cast<df::itemimprovement_pagesst>(virtual_cast<df::item_toolst>(item)->improvements[i])->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[virtual_cast<df::itemimprovement_pagesst>(virtual_cast<df::item_toolst>(item)->improvements[i])->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
else if (virtual_cast<df::item_toolst>(item)->improvements[i]->getType() == df::improvement_type::WRITING)
{
for (size_t k = 0; k < virtual_cast<df::itemimprovement_writingst>(virtual_cast<df::item_toolst>(item)->improvements[i])->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[virtual_cast<df::itemimprovement_writingst>(virtual_cast<df::item_toolst>(item)->improvements[i])->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
}
}

return "";
}

std::string Items::getDescription(df::item *item, int type, bool decorate)
{
CHECK_NULL_POINTER(item);
Expand Down
14 changes: 11 additions & 3 deletions plugins/stocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "df/ui_advmode.h"

DFHACK_PLUGIN("stocks");
#define PLUGIN_VERSION 0.12
#define PLUGIN_VERSION 0.13

REQUIRE_GLOBAL(world);

Expand Down Expand Up @@ -248,7 +248,11 @@ static string get_keywords(df::item *item)

static string get_item_label(df::item *item, bool trim = false)
{
auto label = Items::getDescription(item, 0, false);
auto label = Items::getTitle(item);
if (label == "")
{
label = Items::getDescription(item, 0, false);
}
if (trim && item->getType() == item_type::BIN)
{
auto pos = label.find("<#");
Expand Down Expand Up @@ -562,7 +566,11 @@ class StockListColumn : public ListColumn<T>
if (!ListColumn<T>::showEntry(entry, search_tokens))
return false;

string item_name = toLower(Items::getDescription(entry->elem->entries[0], 0, false));
string item_name = toLower(Items::getTitle(entry->elem->entries[0]));
if (item_name == "")
{
item_name = toLower(Items::getDescription(entry->elem->entries[0], 0, false));
}

if ((match_start || match_end) && raw_search.size() > item_name.size())
return false;
Expand Down

0 comments on commit c6bbf39

Please sign in to comment.