migrate markForTrade logic from logistics to core

develop
Myk Taylor 2023-06-29 19:36:05 -07:00
parent acd03486a9
commit d39440d33b
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
5 changed files with 47 additions and 25 deletions

@ -1775,6 +1775,10 @@ Items module
Checks whether the item and all items it contains, if any, can be traded.
* ``dfhack.items.markForTrade(item, depot)``
Marks the given item for trade at the given depot.
* ``dfhack.items.isRouteVehicle(item)``
Checks whether the item is an assigned hauling vehicle.

@ -2014,6 +2014,7 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = {
WRAPM(Items, checkMandates),
WRAPM(Items, canTrade),
WRAPM(Items, canTradeWithContents),
WRAPM(Items, markForTrade),
WRAPM(Items, isRouteVehicle),
WRAPM(Items, isSquadEquipment),
WRAPN(moveToGround, items_moveToGround),

@ -33,6 +33,7 @@ distribution.
#include "MemAccess.h"
#include "DataDefs.h"
#include "df/building_tradedepotst.h"
#include "df/item.h"
#include "df/item_type.h"
#include "df/general_ref.h"
@ -199,6 +200,8 @@ DFHACK_EXPORT bool checkMandates(df::item *item);
DFHACK_EXPORT bool canTrade(df::item *item);
/// Checks whether the item and all items it contains, if any, can be traded
DFHACK_EXPORT bool canTradeWithContents(df::item *item);
/// marks the given item for trade at the given depot
DFHACK_EXPORT bool markForTrade(df::item *item, df::building_tradedepotst *depot);
/// Checks whether the item is an assigned hauling vehicle
DFHACK_EXPORT bool isRouteVehicle(df::item *item);

@ -39,6 +39,7 @@ distribution.
using namespace std;
#include "ModuleFactory.h"
#include "modules/Job.h"
#include "modules/MapCache.h"
#include "modules/Materials.h"
#include "modules/Items.h"
@ -48,6 +49,7 @@ using namespace std;
#include "df/body_part_template_flags.h"
#include "df/building.h"
#include "df/building_actual.h"
#include "df/building_tradedepotst.h"
#include "df/caste_raw.h"
#include "df/creature_raw.h"
#include "df/general_ref.h"
@ -1637,6 +1639,42 @@ bool Items::canTradeWithContents(df::item *item)
return true;
}
bool Items::markForTrade(df::item *item, df::building_tradedepotst *depot) {
CHECK_NULL_POINTER(item);
CHECK_NULL_POINTER(depot);
// validate that the depot is in a good state
if (depot->getBuildStage() < depot->getMaxBuildStage())
return false;
if (depot->jobs.size() && depot->jobs[0]->job_type == df::job_type::DestroyBuilding)
return false;
auto href = df::allocate<df::general_ref_building_holderst>();
if (!href)
return false;
auto job = new df::job();
job->job_type = df::job_type::BringItemToDepot;
job->pos = df::coord(depot->centerx, depot->centery, depot->z);
// job <-> item link
if (!Job::attachJobItem(job, item, df::job_item_ref::Hauled)) {
delete job;
delete href;
return false;
}
// job <-> building link
href->building_id = depot->id;
depot->jobs.push_back(job);
job->general_refs.push_back(href);
// add to job list
Job::linkIntoWorld(job);
return true;
}
bool Items::isRouteVehicle(df::item *item)
{
CHECK_NULL_POINTER(item);

@ -323,31 +323,7 @@ public:
bool designate(color_ostream& out, df::item* item) override {
if (!depot)
return false;
auto href = df::allocate<df::general_ref_building_holderst>();
if (!href)
return false;
auto job = new df::job();
job->job_type = df::job_type::BringItemToDepot;
job->pos = df::coord(depot->centerx, depot->centery, depot->z);
// job <-> item link
if (!Job::attachJobItem(job, item, df::job_item_ref::Hauled)) {
delete job;
delete href;
return false;
}
// job <-> building link
href->building_id = depot->id;
depot->jobs.push_back(job);
job->general_refs.push_back(href);
// add to job list
Job::linkIntoWorld(job);
return true;
return Items::markForTrade(item, depot);
}
private: