Jump to content

Recommended Posts

Hello, I am the author of mod, Happy Farm Continued. I've had some people say that the code to the crops never rot function isn't really working as intended. Here's the code:

    if GetModConfigData("no_rotten") == true then
        local old_StartGrowing = self.StartGrowing
        function self:StartGrowing(time)
            if self.stages and self.stages[self.stage] and self.stages[self.stage].name ~= nil and 
                self.stages[self.stage].name == "full" then
                self:StopGrowing()
                return
            end
            return old_StartGrowing(self,time)
        end
        self.inst:DoTaskInTime(0,function()
            if self.stages ~= nil and self.stages[self.stage] and self.stages[self.stage].name ~= nil and self.stages[self.stage].name == "full" then
                self:StopGrowing()
            end    
        end)
    end
end)

Do note that I'm not the one who wrote this code. This is the author of the original mod's code. If anyone could lend me code that functions better than this, it would be much appreciated.

The code snippet you provided is incomplete, making it a little hard to evaluate the issues. I guess you are trying to modify the growable component. As expected, the `self.inst:DoTaskInTime ...` and the subsequent code will not take effect but the previous seems to be working.

Additionally, this approach might impact other items that use the `growable` component.
I can suggest two ideas to re-implement this functionality:

1. Modify the duration of the "full" stage in the `inst.components.growable.stages` table of crops to a significantly large number.
   RI:

   {
       name = "full",
       time = GetSpoilTime, -- Modify this field
       pregrowfn = function(inst)
           MakeStressCheckpoint(inst, true)
           inst.is_oversized = inst.force_oversized or (not inst.no_oversized and inst.components.farmplantstress ~= nil and inst.components.farmplantstress:GetFinalStressState() == FARM_PLANT_STRESS.NONE)
       end,
       fn = function(inst, stage, stage_data)
           MakePlantedSeed(inst, false)
           MakeRotten(inst, false)
           MakePickable(inst, true)
           inst.components.farmplanttendable:SetTendable(stage_data.tendable)

           TheWorld:PushEvent("ms_fruitflyspawneractive", {plant = inst, check_others = true})

           inst:UpdateResearchStage(stage)
           PlayStageAnim(inst, inst.is_oversized and "oversized" or "full")
       end,
       dig_fx = "dirt_puff",
       inspect_str = "FULL",
       tendable = false,
       night_grow = true,
   },

2. Listen to the `ms_fruitflyspawneractive` event to stop the growth of a specific crop.
   RI: `TheWorld:PushEvent("ms_fruitflyspawneractive", {plant = inst, check_others = true})`

Oh sorry, I didn't quite know how to add code snippets. Here's every single detail of the code: 

  

  --No Rotten
 if GetModConfigData("no_rotten") == true then
        local old_StartGrowing = self.StartGrowing
        function self:StartGrowing(time)
            if self.stages and self.stages[self.stage] and self.stages[self.stage].name ~= nil and 
                self.stages[self.stage].name == "full" then
                self:StopGrowing()
                return
            end
            return old_StartGrowing(self,time)
        end
        self.inst:DoTaskInTime(0,function()
            if self.stages ~= nil and self.stages[self.stage] and self.stages[self.stage].name ~= nil and self.stages[self.stage].name == "full" then
                self:StopGrowing()
            end    
        end)
    end
end)

Can you also show me what the code should look like? I should also mention what people are saying is that under certain circumstances, the rot timer starts up again. I wasn't able to reproduce the issue but I figure I would rewrite the code anyway. 

Edited by evergreen893401
Addition

1. I would like you to provide a more complete code that extends forward until it corresponds to the last `end`.
2. In your original code, after calling `StopGrowing()`, it should not continue to grow, so the original code should work. Unless we figure out where the timer is being restarted, the second idea I mentioned above might also fail.

Here's the entire mod's code. Hope this works:

GLOBAL.setmetatable(env,{__index=function(t,k) return GLOBAL.rawget(GLOBAL,k) end})

--Auto Aligning
local yuyuyu = {[0] = 0.667,[1] = 0.667,[2] = 2,[3] = 3.333}

if TUNING.FARM_TILL_SPACING ~= nil then
    local auto_aligning = GetModConfigData("to_auto_aligning")
    if auto_aligning ~= false then
        AddComponentPostInit("farmtiller", function(self)
            local old_Till = self.Till
            function self:Till(pt, doer,...)
                if auto_aligning == "4*4" then
                    pt.x = math.floor(pt.x/4)*4 + (math.floor(pt.x)%4)*1.333
                    pt.z = math.floor(pt.z/4)*4 + (math.floor(pt.z)%4)*1.333
                else
                    --3*3
                    --pt.x = math.floor(pt.x/4)*4 + yuyuyu[(math.floor(pt.x+0.5)%4)]
                    --pt.z = math.floor(pt.z/4)*4 + yuyuyu[(math.floor(pt.z+0.5)%4)]
                    --3*3
                    local pt1 = Point( TheWorld.Map:GetTileCenterPoint(pt:Get()) )
                    local p2 = pt1 - pt
                    local p3 = Point( (p2.x + 0.5) - (p2.x + 0.5)%1.333, 0, (p2.z + 0.5) - (p2.z + 0.5)%1.333 )
                    pt = pt1 - p3    
                    --顶点3*3                
                    --local xx = pt.x%2
                    --local zz = pt.z%2
                    --local x = (xx == 1 and -0.95) or (xx < 1 and  0 ) or 0.95
                    --local z = (zz == 1 and -0.95) or (zz < 1 and  0 ) or 0.95
                    --pt.x =     math.floor(pt.x)+x pt.z = math.floor(pt.z)+z
                end
                --print(pt)
                return old_Till(self,pt, doer,...)
            end
        end)
    end
end

AddComponentPostInit("growable", function(self)
    --To Oversize
    if GetModConfigData("to_oversize") == true then
        self.inst:DoTaskInTime(0,function()
            if not(self.stages ~= nil and type(self.stages)  =="table") then return end
            for i, v in ipairs(self.stages) do
                if v and v.name ~= nil and v.name == "full" and
                    v.pregrowfn ~= nil then
                    local old = v.pregrowfn
                    v.pregrowfn = function(inst)
                        old(inst)
                        inst.is_oversized = true
                    end
                end
            end
        end)
    end

    --No Rotten
 if GetModConfigData("no_rotten") == true then
        local old_StartGrowing = self.StartGrowing
        function self:StartGrowing(time)
            if self.stages and self.stages[self.stage] and self.stages[self.stage].name ~= nil and 
                self.stages[self.stage].name == "full" then
                self:StopGrowing()
                return
            end
            return old_StartGrowing(self,time)
        end
        self.inst:DoTaskInTime(0,function()
            if self.stages ~= nil and self.stages[self.stage] and self.stages[self.stage].name ~= nil and self.stages[self.stage].name == "full" then
                self:StopGrowing()
            end    
        end)
    end
end)

--Quick plant
if GetModConfigData("quick_plant") == true then
    AddStategraphPostInit("wilson", function(sg)
        local plant = ACTIONS.PLANTSOIL ~= nil and sg.actionhandlers[ACTIONS.PLANTSOIL].deststate or nil
        if plant then
            sg.actionhandlers[ACTIONS.PLANTSOIL].deststate = function(inst, action)
                return "doshortaction"
            end
        end
    end)
end

-- All Crops like All Seasons
if GetModConfigData("allow_all_seasons") == true then
local function SetAllSeasonsForPlants()
    local PLANT_DEFS = require("prefabs/farm_plant_defs").PLANT_DEFS
    
    for _, data in pairs(PLANT_DEFS) do
        data.good_seasons = { autumn = true, winter = true, spring = true, summer = true }
    end

end

AddSimPostInit(SetAllSeasonsForPlants)

end

 

I found another `crop` component that might be affecting the growth timer together with the `growable` component. Before I have time to look into it further, I can provide a workaround solution:

local function SetAllSeasonsForPlants()
    local PLANT_DEFS = require("prefabs/farm_plant_defs").PLANT_DEFS

    for _, data in pairs(PLANT_DEFS) do
        data.good_seasons = { autumn = true, winter = true, spring = true, summer = true }
        
        -- No Rotten
        if GetModConfigData("no_rotten") == true then
            data.grow_time.full = 100000 * TUNING.TOTAL_DAY_TIME
        end
    end
end

 

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
×
  • Create New...