Jump to content

How to mod specific crops and their harvest numbers?


Recommended Posts

Hi everybody, I am new to lua modding and right of the bat I don´t understand much of it, yet.

 

I am very fond of using the mod Gorge Crops!, because I think this is how farming should have been from the start. For myself, I managed to modify things like the recipes, the values of food and stuff. But I do not know how I can achieve a different number of harvests for different crops, or simply speaking that some crops give more per harvest than others.

 

For example: wheat shows 4 ears, so I would like to set it to 4 wheat per harvest. A Tomato shows 2, so I would like to have set it to 2. And so on with every crop.

 

There already is a configuration option of how many crops one gets with each harvest, however, that counts for all of them. So if I want the harvest numbers be specific to each crop, how can I do this? Help is very appreciated, because atm I am simply stuck.

 

Thank you very much!

Edited by Gobbel
typos
Link to comment
Share on other sites

I have managed to find code on how to control if there are random (chance can be adjusted in the menu) extra drops when harvesting crops.

 

But what has to be changed, so that it is not all crops, but only one type? In this case a new type added by the mod I am working on.

 

---------------------------------------------------------------------------------------------------------------------------------------------------------

local function CropPostInit(self)
    local _Harvest = self.Harvest
    self.Harvest = function(self, harvester)
        local pos = self.inst:GetPosition()
        local ret, product = _Harvest(self, harvester)
        if ret and product and harvester and pos then
            local rand = GLOBAL.GetRandomMinMax(0,1) -- between 0 and 1
            local count = 0
            if rand >= (1-GetModConfigData("one_harvest")) then -- chance for getting one extra
                count = 1
            end
            if rand >= (1-GetModConfigData("two_harvest")) then -- chance for getting two extra
                count = 2
            end
            print("Harvest Mod, give "..count.." extra harvest")
            if count>0 then
                local _prefab = product.prefab
                local _wetness = GLOBAL.TheWorld.state.wetness
                local _iswet = GLOBAL.TheWorld.state.iswet
                -- Giving function to schedule

                local _givefn = function()
                    local extra = GLOBAL.SpawnPrefab(_prefab)
                    if extra.components.inventoryitem then
                        extra.components.inventoryitem:InheritMoisture(_wetness, _iswet)
                        harvester.components.inventory:GiveItem(extra, nil, pos)
                    else
                        extra.Transform:SetPosition(pos.x, pos.y, pos.z)
                    end
                end
                -- For some reason the game can display incorrect stack amounts when giving many items at once
                -- It is better to time them separately
                if count>=1 then
                    harvester:DoTaskInTime(0.23, _givefn)
                end
                if count==2 then
                    harvester:DoTaskInTime(0.46, _givefn)
                end
            end
        end
        return ret, product
    end
end
AddComponentPostInit("crop", CropPostInit)

 

---------------------------------------------------------------------------------------------------------------------------------------------------------

Link to comment
Share on other sites

Is there noone who could help? So many views and no comments. Is it because what I would like to do is not possible?

 

I tried several combinations of changing the code in the original mod:

if harvester = SpawnPrefab("wheat") then
                product.components.stackable:SetStackSize(4)
                harvester.components.inventory:GiveItem(product)

            elseif harvester = SpawnPrefab("tomato") then
                product.components.stackable:SetStackSize(2)
                harvester.components.inventory:GiveItem(product)

            elseif harvester = SpawnPrefab("potato") then
                product.components.stackable:SetStackSize(2)
                harvester.components.inventory:GiveItem(product)

            elseif harvester = SpawnPrefab("onion") then
                product.components.stackable:SetStackSize(1)
                harvester.components.inventory:GiveItem(product)

            elseif harvester = SpawnPrefab("garlic") then
                product.components.stackable:SetStackSize(1)
                harvester.components.inventory:GiveItem(product)

            elseif harvester = SpawnPrefab("turnip") then
                product.components.stackable:SetStackSize(1)
                harvester.components.inventory:GiveItem(product)

 

as well as: if harvester.components.inventoryitem = SpawnPrefab("wheat") then

and: if product.components.inventoryitem = SpawnPrefab("wheat") then

 

All didn`t work.

Edited by Gobbel
Link to comment
Share on other sites

My current attempt at fixing this:

 

function MoreCrop:Harvest(harvester)
    if self.matured or self.inst:HasTag("withered") then
        if self.onharvestfn ~= nil then
            self.onharvestfn(self.inst, self.grower)
        end
    
        local product = nil
        
        if self.grower ~= nil and
            (self.grower.components.burnable ~= nil and self.grower.components.burnable:IsBurning()) or
            (self.inst.components.burnable ~= nil and self.inst.components.burnable:IsBurning()) then
            local temp = SpawnPrefab(self.product_prefab)
            
            product = SpawnPrefab(temp.components.cookable ~= nil and temp.components.cookable.product or "seeds_cooked")
            temp:Remove()
        else
            if not self.inst:HasTag("withered") then
                product = SpawnPrefab(self.product_prefab)
            else
                product = SpawnPrefab("spoiled_food")
            end
        end

        if product ~= nil then
            if product.components.inventoryitem ~= nil then
                product.components.inventoryitem:InheritMoisture(TheWorld.state.wetness, TheWorld.state.iswet)
            end

            if harvester ~= nil then
            if not product.inst:HasPrefab("wheat", "tomato", "potato") then 
                product.components.stackable:SetStackSize(1)
                harvester.components.inventory:GiveItem(product)

            else
                product.components.stackable:SetStackSize(4)
                harvester.components.inventory:GiveItem(product)

            end
            else
                product.Transform:SetPosition(self.inst.Transform:GetWorldPosition())
            end
        end

        self.matured = false
        self.growthpercent = 0
        self.product_prefab = nil
        self.grower = nil

        return true, product
    end
end

 

 

Didn`t work by the way. It crashed when I tried to harvest a wheat plant. Well, at least compared to before it is better. Befor it crashed when planting any new seed.

Edited by Gobbel
Link to comment
Share on other sites

1 hour ago, Gobbel said:

product.components.stackable:SetStackSize(4)

 

doing this only changes how many items of this kind can be in one inventory slot, it does not affect the spawning count. 

id try doing this

for k = 1, # do 
        local product= SpawnPrefab("prefab")
end

or this

product = SpawnPrefab("prefab",#)

# stands for number of items per harvest

Edited by Parusoid
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...