Skip to content

Commit

Permalink
Merge pull request #4879 from quietust/plant_growth
Browse files Browse the repository at this point in the history
Items::createItem - auto-select growth prints
  • Loading branch information
myk002 authored Aug 28, 2024
2 parents 988c4d6 + 1a740f3 commit f11efe6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ Template for new versions:

## API
- ``Units``: new ``isWildlife`` and ``isAgitated`` property checks
- ``Items::createItem``: removed growth_print parameter, now determined automatically

## Lua
- ``dfhack.units``: ``isWildlife`` and ``isAgitated`` property checks
- ``dfhack.units.isDanger``: no longer returns true for intelligent undead
- Overlay widgets can now assume their ``active`` and ``visible`` functions will only execute in a context that matches their ``viewscreens`` associations
- ``gui.simulateInput``: do not generate spurious keycode from ``_STRING``
- ``dfhack.items.createItem``: removed growth_print parameter to match C++ API

## Removed
- ``quickfortress.csv``: remove old sample blueprints for "The Quick Fortress", which were unmaintained and non-functional in DF v50+. Online blueprints are available at https://docs.google.com/spreadsheets/d/1WuLYZBM6S2nt-XsPS30kpDnngpOQCuIdlw4zjrcITdY if anyone is interested in giving these blueprints some love
Expand Down
5 changes: 2 additions & 3 deletions library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2414,10 +2414,9 @@ static int items_createItem(lua_State *state)
auto item_subtype = lua_tointeger(state, 3);
auto mat_type = lua_tointeger(state, 4);
auto mat_index = lua_tointeger(state, 5);
int growth_print = luaL_optint(state, 6, -1);
bool no_floor = lua_toboolean(state, 7);
bool no_floor = lua_toboolean(state, 6);
vector<df::item *> out_items;
Items::createItem(out_items, unit, item_type, item_subtype, mat_type, mat_index, growth_print, no_floor);
Items::createItem(out_items, unit, item_type, item_subtype, mat_type, mat_index, no_floor);
Lua::PushVector(state, out_items);
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion library/include/modules/Items.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ DFHACK_EXPORT int getItemBaseValue(int16_t item_type, int16_t item_subtype, int1
DFHACK_EXPORT int getValue(df::item *item, df::caravan_state *caravan = NULL);

DFHACK_EXPORT bool createItem(std::vector<df::item *> &out_items, df::unit *creator, df::item_type type,
int16_t item_subtype, int16_t mat_type, int32_t mat_index, int32_t growth_print = -1, bool no_floor = false);
int16_t item_subtype, int16_t mat_type, int32_t mat_index, bool no_floor = false);

// Returns true if the item is free from mandates, or false if mandates prevent trading the item.
DFHACK_EXPORT bool checkMandates(df::item *item);
Expand Down
32 changes: 31 additions & 1 deletion library/modules/Items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ distribution.
#include "df/mandate.h"
#include "df/map_block.h"
#include "df/material.h"
#include "df/plant_raw.h"
#include "df/plant_growth.h"
#include "df/plant_growth_print.h"
#include "df/proj_itemst.h"
#include "df/proj_list_link.h"
#include "df/reaction_product_itemst.h"
Expand Down Expand Up @@ -1707,7 +1710,7 @@ int Items::getValue(df::item *item, df::caravan_state *caravan) {
}

bool Items::createItem(vector<df::item *> &out_items, df::unit *unit, df::item_type item_type,
int16_t item_subtype, int16_t mat_type, int32_t mat_index, int32_t growth_print, bool no_floor)
int16_t item_subtype, int16_t mat_type, int32_t mat_index, bool no_floor)
{ // Based on Quietust's plugins/createitem.cpp
CHECK_NULL_POINTER(unit);
auto pos = Units::getPosition(unit);
Expand Down Expand Up @@ -1756,7 +1759,34 @@ bool Items::createItem(vector<df::item *> &out_items, df::unit *unit, df::item_t
for (auto out_item : out_items)
{ // Plant growths need a valid "growth print", otherwise they behave oddly
if (auto growth = virtual_cast<df::item_plant_growthst>(out_item))
{
int growth_print = -1;
// Make sure it's made of a valid plant material, then grab its definition
if (growth->mat_type >= 419 && growth->mat_type <= 618 && growth->mat_index >= 0 && (unsigned)growth->mat_index < world->raws.plants.all.size())
{
auto plant_def = world->raws.plants.all[growth->mat_index];
// Make sure it subtype is also valid
if (growth->subtype >= 0 && (unsigned)growth->subtype < plant_def->growths.size())
{
auto growth_def = plant_def->growths[growth->subtype];
// Try and find a growth print matching the current time
// (in practice, only tree leaves use this for autumn color changes)
for (size_t i = 0; i < growth_def->prints.size(); i++)
{
auto print_def = growth_def->prints[i];
if (print_def->timing_start <= *df::global::cur_year_tick && *df::global::cur_year_tick <= print_def->timing_end)
{
growth_print = i;
break;
}
}
// If we didn't find one, then pick the first one (if it exists)
if (growth_print == -1 && !growth_def->prints.empty())
growth_print = 0;
}
}
growth->growth_print = growth_print;
}
if (!no_floor)
out_item->moveToGround(pos.x, pos.y, pos.z);
}
Expand Down
29 changes: 6 additions & 23 deletions plugins/createitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "df/game_type.h"
#include "df/item.h"
#include "df/plant_growth.h"
#include "df/plant_growth_print.h"
#include "df/plant_raw.h"
#include "df/tool_uses.h"
#include "df/unit.h"
Expand Down Expand Up @@ -54,7 +53,7 @@ DFhackCExport command_result plugin_shutdown(color_ostream &out) {
}

bool makeItem(df::unit *unit, df::item_type type, int16_t subtype, int16_t mat_type, int32_t mat_index,
int32_t growth_print = -1, bool move_to_cursor = false, bool second_item = false)
bool move_to_cursor = false, bool second_item = false)
{ // Special logic for making Gloves and Shoes in pairs
bool is_gloves = (type == item_type::GLOVES);
bool is_shoes = (type == item_type::SHOES);
Expand All @@ -69,7 +68,7 @@ bool makeItem(df::unit *unit, df::item_type type, int16_t subtype, int16_t mat_t
bool on_floor = (container == NULL) && (building == NULL) && !move_to_cursor;

vector<df::item *> out_items;
if (!Items::createItem(out_items, unit, type, subtype, mat_type, mat_index, growth_print, !on_floor))
if (!Items::createItem(out_items, unit, type, subtype, mat_type, mat_index, !on_floor))
return false;

for (size_t i = 0; i < out_items.size(); i++) {
Expand Down Expand Up @@ -105,7 +104,7 @@ bool makeItem(df::unit *unit, df::item_type type, int16_t subtype, int16_t mat_t
is_shoes = false;
// If we asked for gloves/shoes and only got one (and we're making the first one), make another
if ((is_gloves || is_shoes) && !second_item)
return makeItem(unit, type, subtype, mat_type, mat_index, growth_print, move_to_cursor, true);
return makeItem(unit, type, subtype, mat_type, mat_index, move_to_cursor, true);
return true;
}

Expand Down Expand Up @@ -157,7 +156,7 @@ static inline bool select_caste_mat(color_ostream &out, vector<string> &tokens,
}

static inline bool select_plant_growth(color_ostream &out, vector<string> &tokens, df::item_type &item_type,
int16_t &item_subtype, int16_t &mat_type, int32_t &mat_index, int32_t &growth_print, const string &material_str)
int16_t &item_subtype, int16_t &mat_type, int32_t &mat_index, const string &material_str)
{
split_string(&tokens, material_str, ":");
if (tokens.size() == 1)
Expand Down Expand Up @@ -191,21 +190,6 @@ static inline bool select_plant_growth(color_ostream &out, vector<string> &token
item_subtype = growth->item_subtype;
mat_type = growth->mat_type;
mat_index = growth->mat_index;

// Try and find a growth print matching the current time
// (in practice, only tree leaves use this for autumn color changes)
for (size_t k = 0; k < growth->prints.size(); k++)
{
auto print = growth->prints[k];
if (print->timing_start <= *cur_year_tick && *cur_year_tick <= print->timing_end)
{
growth_print = k;
break;
}
}
// If we didn't find one, then pick the first one (if it exists)
if (growth_print == -1 && !growth->prints.empty())
growth_print = 0;
break;
}
if (mat_type == -1) {
Expand All @@ -230,7 +214,6 @@ command_result df_createitem (color_ostream &out, vector<string> &parameters) {
int16_t item_subtype = -1;
int16_t mat_type = -1;
int32_t mat_index = -1;
int32_t growth_print = -1;
int count = 1;
bool move_to_cursor = false;

Expand Down Expand Up @@ -372,7 +355,7 @@ command_result df_createitem (color_ostream &out, vector<string> &parameters) {
break;
case PLANT_GROWTH:
if (!select_plant_growth(out, tokens, item_type, item_subtype,
mat_type, mat_index, growth_print, material_str)
mat_type, mat_index, material_str)
)
return CR_FAILURE;
break;
Expand Down Expand Up @@ -452,7 +435,7 @@ command_result df_createitem (color_ostream &out, vector<string> &parameters) {
}

for (int i = 0; i < count; i++) {
if (!makeItem(unit, item_type, item_subtype, mat_type, mat_index, growth_print, move_to_cursor, false))
if (!makeItem(unit, item_type, item_subtype, mat_type, mat_index, move_to_cursor, false))
{
out.printerr("Failed to create item!\n");
return CR_FAILURE;
Expand Down

0 comments on commit f11efe6

Please sign in to comment.