Resource Yields - need help

hdd

Chieftain
Joined
Oct 2, 2005
Messages
82
Location
Bucharest
Hello There,

I'm trying to build a mod where an improvement doubles the resource Yields, in certain conditions.
E.g., if you build a farm on a resource-free tile, you get the vanilla bonuses.
However, if you build a farm on a Wheat tile, then the food bonuses are doubled.

Mines double production and gold, fishing boats food, camp food and gold, pastures food and production, plantations food and gold, quarries gold and culture etc.

I explored the approaches to achieve this objective. Let's take the Wheat example.
The game only has a single IMPROVEMENT_FARM. In order to apply mod logic using vanilla tools, I'd need 4x farms: Wheat Farm, Maize Farm, Rice Farm, Simple Farm.
Applied to the 53 resources I have in my mod list, this would result in a large number of improvements, making UI clunky, if not impossible.
A single Builder can't build them all. Adding distinct builders for Farm, Mine, Plantations etc. is a solution, but then I need to modify the A.I. I'm in for a simple resource change mod, not a 1y of work overhaul project.

As I can't see an SQL solution, I was thinking applying a LUA script.
On building a Farm, script should intercept the event and add the YIELDS.

Attached is my LUA code. It doesn't run. I can see no prints in the logs.
The modinfo is correct, and there are no errors in loading the file.
Verified it several times.

One more info: I'm testing on an existing save. Should I start a new game? does it matter? AFAIK, it shouldn't.

I would appreciate any help.

Thanks




Code:
-- Define the list of resources for farms that should have doubled Food
local farmResources = {
    "RESOURCE_MAIZE", "RESOURCE_RICE", "RESOURCE_WHEAT"
}

-- Function to handle when an improvement is added to the map
function OnImprovementAddedToMap(iImprovementType, iX, iY, iOwner)
    print("Improvement added to map.") -- Debugging

    -- Get the plot where the improvement was added
    local pPlot = Map.GetPlot(iX, iY)
    print("Coordinates: ", iX, iY) -- Debugging coordinates

    -- Get the improvement type
    local improvementType = GameInfo.Improvements[iImprovementType].ImprovementType
    print("Improvement Type:", improvementType) -- Debugging

    -- Process the farm improvement
    if improvementType == "IMPROVEMENT_FARM" then
        print("Processing farm improvement.") -- Debugging

        -- Get the resource on the plot
        local resourceType = pPlot:GetResourceType(iOwner)
        print("Resource Type on the plot:", resourceType) -- Debugging

        -- Check if the resource is one of the specified farm resources
        if resourceType ~= -1 then
            local resourceInfo = GameInfo.Resources[resourceType]
            print("Resource Info:", resourceInfo.ResourceType) -- Debugging
            if IsResourceInList(resourceInfo.ResourceType, farmResources) then
                print("Valid farm resource detected:", resourceInfo.ResourceType) -- Debugging
                -- Get yield changes for the resource from the Resource_YieldChanges table
                local resourceYieldChanges = {}
                for row in GameInfo.Resource_YieldChanges() do
                    if row.ResourceType == resourceInfo.ResourceType then
                        table.insert(resourceYieldChanges, row)
                    end
                end
                print("Yield changes:", resourceYieldChanges) -- Debugging

                -- Double the yields for the resource
                for _, yieldChange in ipairs(resourceYieldChanges) do
                    if yieldChange.YieldType == "YIELD_FOOD" then
                        pPlot:ChangeYield(yieldChange.YieldType, yieldChange.YieldAmount * 2)
                        print("Yield changed for:", yieldChange.YieldType, "Amount:", yieldChange.YieldAmount * 2) -- Debugging
                    end
                end
            else
                print("Resource not applicable for doubling yields.") -- Debugging
            end
        else
            print("No resource on the plot.") -- Debugging
        end
    end
end

-- Helper function to check if a resource is in the list
function IsResourceInList(resource, resourceList)
    print("Checking resource:", resource) -- Debugging
    for _, r in ipairs(resourceList) do
        if r == resource then
            return true
        end
    end
    return false
end

-- Register the function to the event when an improvement is added
Events.ImprovementAddedToMap.Add(OnImprovementAddedToMap)
print("Lua script loaded successfully.") -- Debugging

-- Additional event to confirm the script is loaded
Events.LoadScreenClose.Add(function()
    print("HDD Mod: Load Screen Closed, Game Ready")
end)
 
So you arent even getting the print statement saying the Lua has loaded: 'Lua script loaded successfully.'? if you arent getting that, the Lua hasn't been loaded for sure. I know you said your modinfo was working but that would be my intuition, so might be worth posting the .modinfo to be sure. Your code otherwise looks good, or at least no immediate syntax errors.

Also, have you enabled Tuner in the game settings? I think the Lua.log and Firetuner access is very limited if you havent.
 
Top Bottom