Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update create-item.lua #1277

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions modtools/create-item.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- creates an item of a given type and material
--author expwnent
--In the year 2024 dikbutdagrate assumed the identity of tjudge1 and edited this file. Now it spawns vermin, pets, eggs, fish, raw fish, and remains correctly.
Tjudge1 marked this conversation as resolved.
Show resolved Hide resolved
--@module=true

local argparse = require('argparse')
Expand Down Expand Up @@ -37,6 +38,15 @@ local no_quality_item_types = utils.invert{
'BRANCH',
}

local typesThatUseCreaturesExceptCorpses = utils.invert {
'REMAINS',
'FISH',
'FISH_RAW',
'VERMIN',
'PET',
'EGG',
}

local CORPSE_PIECES = utils.invert{'BONE', 'SKIN', 'CARTILAGE', 'TOOTH', 'NERVE', 'NAIL', 'HORN', 'HOOF', 'CHITIN',
'SHELL', 'IVORY', 'SCALE'}
local HAIR_PIECES = utils.invert{'HAIR', 'EYEBROW', 'EYELASH', 'MOUSTACHE', 'CHIN_WHISKERS', 'SIDEBURNS'}
Expand Down Expand Up @@ -326,16 +336,27 @@ function hackWish(accessors, opts)
until count
end
if not mattype or not itemtype then return end
if df.item_type.attrs[itemtype].is_stackable then
if not typesThatUseCreaturesExceptCorpses[df.item_type[itemtype]] and df.item_type.attrs[itemtype].is_stackable then
return createItem({mattype, matindex}, {itemtype, itemsubtype}, quality, unit, description, count)
end
if typesThatUseCreaturesExceptCorpses[df.item_type[itemtype]] and df.item_type.attrs[itemtype].is_stackable then
return createItem({matindex, casteId}, {itemtype, itemsubtype}, quality, unit, description, count)
end
Comment on lines +338 to +343
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is too much duplication here. How about this?

    if df.item_type.attrs[itemtype].is_stackable then
        local mat = typesThatUseCreaturesExceptCorpses[df.item_type[itemtype]] and {matindex, casteId} or {mattype, matindex}
        return createItem(mat, {itemtype, itemsubtype}, quality, unit, description, count)
    end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, did you test this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not tested, just written as a suggestion for code improvement. If you have two chunks of code that are semantically and structurally identical except for a small difference, it is usually better to factor that difference out and keep the structure simpler.

local items = {}
for _ = 1,count do
if itemtype == df.item_type.CORPSEPIECE or itemtype == df.item_type.CORPSE then
table.insert(items, createCorpsePiece(unit, bodypart, partlayerID, matindex, casteId, corpsepieceGeneric))
else
for _,item in ipairs(createItem({mattype, matindex}, {itemtype, itemsubtype}, quality, unit, description, 1)) do
table.insert(items, item)
if typesThatUseCreaturesExceptCorpses[df.item_type[itemtype]] then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do the same condensing here. Factor out the value of mat and avoid duplicating the for loop

for
_,item in ipairs(createItem({matindex, casteId}, {itemtype, itemsubtype}, quality, unit, description, 1)) do
table.insert(items, item)
end
else
for
_,item in ipairs(createItem({mattype, matindex}, {itemtype, itemsubtype}, quality, unit, description, 1)) do
table.insert(items, item)
end
end
end
end
Expand Down