Skip to content

Commit

Permalink
Respect export mandates
Browse files Browse the repository at this point in the history
  • Loading branch information
Falconne committed Apr 1, 2013
1 parent 2027416 commit c020c02
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion plugins/autotrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "modules/Job.h"
#include "df/ui.h"
#include "df/caravan_state.h"
#include "df/mandate.h"
#include "modules/Maps.h"
#include "modules/World.h"

Expand Down Expand Up @@ -251,6 +252,31 @@ static TradeDepotInfo depot_info;
* Item Manipulation
*/

static bool check_mandates(df::item *item)
{
for (auto it = world->mandates.begin(); it != world->mandates.end(); it++)
{
auto mandate = *it;

if (mandate->mode != 0)
continue;

if (item->getType() != mandate->item_type ||
(mandate->item_subtype != -1 && item->getSubtype() != mandate->item_subtype))
continue;

if (mandate->mat_type != -1 && item->getMaterial() != mandate->mat_type)
continue;

if (mandate->mat_index != -1 && item->getMaterialIndex() != mandate->mat_index)
continue;

return false;
}

return true;
}

static bool is_valid_item(df::item *item)
{
for (size_t i = 0; i < item->general_refs.size(); i++)
Expand Down Expand Up @@ -284,6 +310,8 @@ static bool is_valid_item(df::item *item)
}
}

if (!check_mandates(item))
return false;

return true;
}
Expand All @@ -300,7 +328,6 @@ static void mark_all_in_stockpiles(vector<StockpileInfo> &stockpiles, bool annou

std::vector<df::item*> &items = world->items.other[items_other_id::IN_PLAY];

//FIXME filter out mandates

// Precompute a bitmask with the bad flags
df::item_flags bad_flags;
Expand Down Expand Up @@ -329,6 +356,22 @@ static void mark_all_in_stockpiles(vector<StockpileInfo> &stockpiles, bool annou
if (!it->inStockpile(item))
continue;

// In case of container, check contained items for mandates
bool mandates_ok = true;
vector<df::item*> contained_items;
Items::getContainedItems(item, &contained_items);
for (auto cit = contained_items.begin(); cit != contained_items.end(); cit++)
{
if (!check_mandates(*cit))
{
mandates_ok = false;
break;
}
}

if (!mandates_ok)
continue;

if (depot_info.assignItem(item))
{
++marked_count;
Expand Down

0 comments on commit c020c02

Please sign in to comment.