Merge remote-tracking branch 'PatrikLundell/stocks' into develop

Conflicts:
	docs/changelog.txt
develop
lethosor 2020-04-25 21:12:27 -04:00
commit 7c1d1c43e5
6 changed files with 114 additions and 3 deletions

@ -1292,6 +1292,12 @@ Items module
Returns true *x,y,z* of the item, or *nil* if invalid; may be not equal to item.pos if in inventory.
* ``dfhack.items.getBookTitle(item)``
Returns the title of the "book" item, or an empty string if the item isn't a "book" or it doesn't
have a title. A "book" is a codex or a tool item that has page or writings improvements, such as
scrolls and quires.
* ``dfhack.items.getDescription(item, type[, decorate])``
Returns the string description of the item, as produced by the ``getItemDescription``

@ -43,6 +43,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
- Fixed translation of certain types of in-game names
- Fixed a crash in ``find()`` for some types when no world is loaded
- `autogems`: fixed an issue with binned gems being ignored in linked stockpiles
- `stocks`: fixed display of book titles
- `tweak` embark-profile-name: fixed handling of the native shift+space key
## Misc Improvements
@ -51,6 +52,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
- `manipulator`: added intrigue to displayed skills
- `search`: added support for the fortress mode justice screen
## API
- Added ``Items::getBookTitle`` to get titles of books. Catches titles buried in improvements, unlike getDescription.
## Lua
- ``pairs()`` now returns available class methods for DF types

@ -1766,6 +1766,7 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = {
WRAPM(Items, getContainer),
WRAPM(Items, getHolderBuilding),
WRAPM(Items, getHolderUnit),
WRAPM(Items, getBookTitle),
WRAPM(Items, getDescription),
WRAPM(Items, isCasteMaterial),
WRAPM(Items, getSubtypeCount),

@ -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 case for everything that isn't a "book").
DFHACK_EXPORT std::string getBookTitle(df::item *item);
/// Returns the description string of the item.
DFHACK_EXPORT std::string getDescription(df::item *item, int type = 0, bool decorate = false);

@ -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"
@ -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"
@ -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;
@ -681,6 +687,87 @@ 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::getBookTitle(df::item *item)
{
CHECK_NULL_POINTER(item);
std::string tmp;
if (item->getType() == df::item_type::BOOK)
{
auto book = virtual_cast<df::item_bookst>(item);
if (book->title != "")
{
return book->title;
}
else
{
for (size_t i = 0; i < book->improvements.size(); i++)
{
if (auto page = virtual_cast<df::itemimprovement_pagesst>(book->improvements[i]))
{
for (size_t k = 0; k < page->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[page->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
else if (auto writing = virtual_cast<df::itemimprovement_writingst>(book->improvements[i]))
{
for (size_t k = 0; k < writing->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[writing->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
}
}
}
else if (item->getType() == df::item_type::TOOL)
{
auto book = virtual_cast<df::item_toolst>(item);
if (book->hasToolUse(df::tool_uses::CONTAIN_WRITING))
{
for (size_t i = 0; i < book->improvements.size(); i++)
{
if (auto page = virtual_cast<df::itemimprovement_pagesst>(book->improvements[i]))
{
for (size_t k = 0; k < page->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[page->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
else if (auto writing = virtual_cast<df::itemimprovement_writingst>(book->improvements[i]))
{
for (size_t k = 0; k < writing->contents.size(); k++)
{
df::written_content *contents = world->written_contents.all[writing->contents[k]];
if (contents->title != "")
{
return contents->title;
}
}
}
}
}
}
return "";
}
std::string Items::getDescription(df::item *item, int type, bool decorate)
{
CHECK_NULL_POINTER(item);

@ -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);
@ -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::getBookTitle(item);
if (label == "")
{
label = Items::getDescription(item, 0, false);
}
if (trim && item->getType() == item_type::BIN)
{
auto pos = label.find("<#");
@ -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::getBookTitle(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;