How can I modify workplace min value to 0?
#1
I am building a update option which can let production building with no workers, like real "automation".

Then I find the the definition of property "MaxWorkers" being in "\Lua\Lua\Game\BuildingComponents\Workplace.lua"

The section is the following:

modified_properties = {
category = "Workplace",
{
id = "MaxWorkers",
min = 1,
notify = true
},
......
},

I just set the "min" value to 0, and put the .lua file in "\Lua\Lua\Game\BuildingComponents\".

When I restarted the game, it crashed with black screen(only the music exists).

I tried to delete the "\Lua" folder and restated.

It was back to normal.

So, it looks like that I can not just modify the .lua file from the "\Lua" folder and put it to its original file path.

Is there any way to achieve my purpose?

Thank you.

Sad
Reply
#2
Code:
OnMsg.ClassesPreprocess = function()
    for i = 1, #Workplace.modified_properties do
        local mp = Workplace.modified_properties[i]
        if mp.id == "MaxWorkers" then
            mp.min = 0
        end
    end
end

It's not quite as simple as changing the modified property minimum to 0.   A building that has the workplace component and has zero workers will have a fully negative effectiveness rating.  It will also always have that annoying No Workers sign above the building.  To fix this you have to modify Workplace:GetWorkersEffectivenessModification(), Building:GetEffectiveness(), and Workplace:HasWorkers()

Code:
OnMsg.ClassesPreprocess = function()
    local wp = Workplace.modified_properties
    for i = 1, #Workplace.modified_properties do
        local mp = Workplace.modified_properties[i]
        if mp.id == "MaxWorkers" then
            mp.min = 0
        end
    end
    function Workplace:GetWorkersEffectivenessModification()
        local effectiveness = self.parent:GetInherentEffectiveness()
        local workers = self:GetWorkersCount()
        if self:GetMaxWorkers() == 0 then
            return 0
        end
        if workers == 0 then
            return -effectiveness
        end
        local workerseffectiveness = MulDivRound(effectiveness, workers, self:GetMaxWorkers())
        return workerseffectiveness - effectiveness
    end
    function Building:GetEffectiveness(ignore_power)
        if not self:CanFunction(ignore_power) then
            return 0
        end
        if self:IsStriking() then
            return 0
        end
        if self:IsProhibited() then
            return 0
        end
        local effectiveness = self:GetInherentEffectiveness()
        local workplace = self.workplace
        if workplace then
            local workers = workplace.workers
            if workplace:GetMaxWorkers() == 0 then
                return effectiveness
            end
            workers = workers and #workers or 0
            if workers == 0 then
                return 0
            end
            effectiveness = MulDivRound(effectiveness, workers, workplace:GetMaxWorkers())
        end
        if self.farming and self:GetFirstPOI("WaterShortagePOI") then
            effectiveness = 50 * effectiveness / 100
        end
        if self.BudgetLevel == const.MaxBudgetLevel then
            effectiveness = effectiveness + self.city:GetSkillBonus("Tycoon")
        end
        if self.production then
            effectiveness = effectiveness + self.city:GetSkillBonus("Foreman")
        end
        return effectiveness
    end
    function Workplace:HasWorkers()
        if self:GetMaxWorkers() == 0 then
            return true
        end
        return self.workers and #self.workers > 0
    end
end
Reply
#3
(13-04-2017, 07:30 AM)DarthPresidente Wrote:
Code:
OnMsg.ClassesPreprocess = function()
    for i = 1, #Workplace.modified_properties do
        local mp = Workplace.modified_properties[i]
        if mp.id == "MaxWorkers" then
            mp.min = 0
        end
    end
end

It's not quite as simple as changing the modified property minimum to 0.   A building that has the workplace component and has zero workers will have a fully negative effectiveness rating.  It will also always have that annoying No Workers sign above the building.  To fix this you have to modify Workplace:GetWorkersEffectivenessModification(), Building:GetEffectiveness(), and Workplace:HasWorkers()

Code:
OnMsg.ClassesPreprocess = function()
    local wp = Workplace.modified_properties
    for i = 1, #Workplace.modified_properties do
        local mp = Workplace.modified_properties[i]
        if mp.id == "MaxWorkers" then
            mp.min = 0
        end
    end
    function Workplace:GetWorkersEffectivenessModification()
        local effectiveness = self.parent:GetInherentEffectiveness()
        local workers = self:GetWorkersCount()
        if self:GetMaxWorkers() == 0 then
            return 0
        end
        if workers == 0 then
            return -effectiveness
        end
        local workerseffectiveness = MulDivRound(effectiveness, workers, self:GetMaxWorkers())
        return workerseffectiveness - effectiveness
    end
    function Building:GetEffectiveness(ignore_power)
        if not self:CanFunction(ignore_power) then
            return 0
        end
        if self:IsStriking() then
            return 0
        end
        if self:IsProhibited() then
            return 0
        end
        local effectiveness = self:GetInherentEffectiveness()
        local workplace = self.workplace
        if workplace then
            local workers = workplace.workers
            if workplace:GetMaxWorkers() == 0 then
                return effectiveness
            end
            workers = workers and #workers or 0
            if workers == 0 then
                return 0
            end
            effectiveness = MulDivRound(effectiveness, workers, workplace:GetMaxWorkers())
        end
        if self.farming and self:GetFirstPOI("WaterShortagePOI") then
            effectiveness = 50 * effectiveness / 100
        end
        if self.BudgetLevel == const.MaxBudgetLevel then
            effectiveness = effectiveness + self.city:GetSkillBonus("Tycoon")
        end
        if self.production then
            effectiveness = effectiveness + self.city:GetSkillBonus("Foreman")
        end
        return effectiveness
    end
    function Workplace:HasWorkers()
        if self:GetMaxWorkers() == 0 then
            return true
        end
        return self.workers and #self.workers > 0
    end
end

Thank you! Big Grin
It looks complicated to modify a lua file.
Not only modify the value, but also check the function which use this value.
Does all the lua file in tropico 5 HPK file follow the LUA language grammar rule, or contain some custom grammar rule? Shy
Reply




Users browsing this thread: 1 Guest(s)