diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index fcd7353ea..aebe8e5c1 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -1465,6 +1465,20 @@ static df::proj_itemst *items_makeProjectile(df::item *item) return Items::makeProjectile(mc, item); } +static int16_t items_findType(std::string token) +{ + DFHack::ItemTypeInfo result; + result.find(token); + return result.type; +} + +static int32_t items_findSubtype(std::string token) +{ + DFHack::ItemTypeInfo result; + result.find(token); + return result.subtype; +} + static const LuaWrapper::FunctionReg dfhack_items_module[] = { WRAPM(Items, getGeneralRef), WRAPM(Items, getSpecificRef), @@ -1486,6 +1500,8 @@ static const LuaWrapper::FunctionReg dfhack_items_module[] = { WRAPN(moveToInventory, items_moveToInventory), WRAPN(makeProjectile, items_makeProjectile), WRAPN(remove, items_remove), + WRAPN(findType, items_findType), + WRAPN(findSubtype, items_findSubtype), { NULL, NULL } }; diff --git a/scripts/modtools/create-item.lua b/scripts/modtools/create-item.lua new file mode 100644 index 000000000..f0a9c8cc5 --- /dev/null +++ b/scripts/modtools/create-item.lua @@ -0,0 +1,86 @@ +--create-item.lua +--author expwnent +--creates an item of a given type and material + +local utils = require 'utils' + +validArgs = --[[validArgs or]] utils.invert({ + 'help', + 'creator', + 'material', + 'item', + 'creature', + 'caste', + 'matchingGloves', + 'matchingShoes' +}) + +local args = utils.processArgs({...}, validArgs) + +if args.help then + --TODO: help string + return +end + +if not args.creator or not tonumber(args.creator) or not df.unit.find(tonumber(args.creator)) then + error 'Invalid creator.' +end +args.creator = df.unit.find(tonumber(args.creator)) +if not args.creator then + error 'Invalid creator.' +end + +if not args.item then + error 'Invalid item.' +end +local itemType = dfhack.items.findType(args.item) +local itemSubtype = dfhack.items.findSubtype(args.item) +if itemType == -1 then + error 'Invalid item.' +end + +organicTypes = organicTypes or utils.invert({ + df.item_type.REMAINS, + df.item_type.FISH, + df.item_type.FISH_RAW, + df.item_type.VERMIN, + df.item_type.PET, + df.item_type.EGG, +}) + +if organicTypes[itemType] then + --TODO: look up creature and caste + error 'Not yet supported.' +end + +badTypes = badTypes or utils.invert({ + df.item_type.CORPSE, + df.item_type.CORPSEPIECE, + df.item_type.FOOD, +}) + +if badTypes[itemType] then + error 'Not supported.' +end + +if not args.material then + error 'Invalid material.' +end +args.material = dfhack.matinfo.find(args.material) +if not args.material then + error 'Invalid material.' +end + +local item1 = dfhack.items.createItem(itemType, itemSubtype, args.material['type'], args.material.index, args.creator) +if args.matchingGloves or args.matchingShoes then + if args.matchingGloves then + item1 = df.item.find(item1) + item1:setGloveHandedness(1); + end + local item2 = dfhack.items.createItem(itemType, itemSubtype, args.material['type'], args.material.index, args.creator) + if args.matchingGloves then + item2 = df.item.find(item2) + item2:setGloveHandedness(2); + end +end +