Thalassicus / cat

(Deprecated: now in "cep-bnw" project)
0 stars 4 forks source link

'Pairs' runtime error (+Plot_GetPlotsInCircle) #4

Closed stackpoint closed 10 years ago

stackpoint commented 10 years ago

Got the following error:

Runtime Error: \Communitas AI and Tools (v 3)\Tools/Cat_Events.lua:353: bad argument #1 to 'pairs' (table expected, got function)

Below is the corresponding code:

    query = string.format("BuildingType = '%s'", buildingInfo.Type)
    for info in GameInfo.Building_PlotYieldChanges(query) do
        local nearbyPlots = Plot_GetPlotsInCircle(plot, 0, 3)
        for _,adjPlot in pairs(nearbyPlots) do
            local adjPlotID = Plot_GetID(adjPlot)
            if adjPlot:GetPlotType() == PlotTypes.PLOT_HILLS and (adjPlot:GetOwner() == playerID or adjPlot:GetOwner() == -1) then
                if adjPlot:GetOwner() == -1 then
                    adjPlot:SetOwner(playerID, city:GetID())
                end
                Plot_ChangeYield(adjPlot, GameInfo.Yields[info.YieldType].ID, info.Yield)
            end
        end
    end

I'm not sure what the intent of the code is but the rest of the function follows the format below:

        for adjPlot in Plot_GetPlotsInCircle(plot, 0, 3) do

Not sure if the same should be done with this but the following would suggest so: https://github.com/Thalassicus/cat/commit/0957d70e081999b010c43fb3d5b8b04ad0c6c5cd#diff-775b10cce940685df61cbde72bfb570a

As an aside, while looking for a solution to this problem, I looked into Plot_GetPlotsInCircle function and found it defined in four different lua files. However, the function in NaturalWondersCustomMethods.lua/Communitas.lua, is different from the one in FeatureGenerator.lua/MT_Plot.lua. It does not use the "Game" prefix for many of it's calls:

    leftX   = isWrapX and ((centerX-maxR) % mapW) or Game.Constrain(0, centerX-maxR, mapW-1)
    rightX  = isWrapX and ((centerX+maxR) % mapW) or Game.Constrain(0, centerX+maxR, mapW-1)
    bottomY = isWrapY and ((centerY-maxR) % mapH) or Game.Constrain(0, centerY-maxR, mapH-1)
    topY    = isWrapY and ((centerY+maxR) % mapH) or Game.Constrain(0, centerY+maxR, mapH-1)

vs

    leftX   = isWrapX and ((centerX-maxR) % mapW) or Constrain(0, centerX-maxR, mapW-1)
    rightX  = isWrapX and ((centerX+maxR) % mapW) or Constrain(0, centerX+maxR, mapW-1)
    bottomY = isWrapY and ((centerY-maxR) % mapH) or Constrain(0, centerY-maxR, mapH-1)
    topY    = isWrapY and ((centerY+maxR) % mapH) or Constrain(0, centerY+maxR, mapH-1)

and

                if Game.IsBetween(minR, distance, maxR) then
                    return plot, distance
                end

vs

                if IsBetween(minR, distance, maxR) then
                    return plot, distance
                end

I don't know if this is significant or not but it seems like a matter of consistency.

GrantSP commented 10 years ago

I too have started getting this error. Never seen it before.

On 9 November 2013 06:07, stackpointer notifications@github.com wrote:

Got the following error:

Runtime Error: \Communitas AI and Tools (v 3)\Tools/Cat_Events.lua:353: bad argument #1 to 'pairs' (table expected, got function)

Below is the corresponding code:

query = string.format("BuildingType = '%s'", buildingInfo.Type)
for info in GameInfo.Building_PlotYieldChanges(query) do
    local nearbyPlots = Plot_GetPlotsInCircle(plot, 0, 3)
    for _,adjPlot in pairs(nearbyPlots) do
        local adjPlotID = Plot_GetID(adjPlot)
        if adjPlot:GetPlotType() == PlotTypes.PLOT_HILLS and (adjPlot:GetOwner() == playerID or adjPlot:GetOwner() == -1) then
            if adjPlot:GetOwner() == -1 then
                adjPlot:SetOwner(playerID, city:GetID())
            end
            Plot_ChangeYield(adjPlot, GameInfo.Yields[info.YieldType].ID, info.Yield)
        end
    end
end

I'm not sure what the intent of the code is but the rest of the function follows the format below:

    for adjPlot in Plot_GetPlotsInCircle(plot, 0, 3) do

Not sure if the same should be done with this but the following would suggest so: 0957d70#diff-775b10cce940685df61cbde72bfb570ahttps://github.com/Thalassicus/cat/commit/0957d70e081999b010c43fb3d5b8b04ad0c6c5cd#diff-775b10cce940685df61cbde72bfb570a

As an aside, while looking for a solution to this problem, I looked into Plot_GetPlotsInCircle function and found it defined in four different lua files. However, the function in NaturalWondersCustomMethods.lua/Communitas.lua, is different from the one in FeatureGenerator.lua/MT_Plot.lua. It does not use the "Game" prefix for many of it's calls:

leftX   = isWrapX and ((centerX-maxR) % mapW) or Game.Constrain(0, centerX-maxR, mapW-1)
rightX  = isWrapX and ((centerX+maxR) % mapW) or Game.Constrain(0, centerX+maxR, mapW-1)
bottomY = isWrapY and ((centerY-maxR) % mapH) or Game.Constrain(0, centerY-maxR, mapH-1)
topY    = isWrapY and ((centerY+maxR) % mapH) or Game.Constrain(0, centerY+maxR, mapH-1)

vs

leftX   = isWrapX and ((centerX-maxR) % mapW) or Constrain(0, centerX-maxR, mapW-1)
rightX  = isWrapX and ((centerX+maxR) % mapW) or Constrain(0, centerX+maxR, mapW-1)
bottomY = isWrapY and ((centerY-maxR) % mapH) or Constrain(0, centerY-maxR, mapH-1)
topY    = isWrapY and ((centerY+maxR) % mapH) or Constrain(0, centerY+maxR, mapH-1)

and

            if Game.IsBetween(minR, distance, maxR) then
                return plot, distance
            end

vs

            if IsBetween(minR, distance, maxR) then
                return plot, distance
            end

I don't know if this is significant or not but it seems like a matter of consistency.

— Reply to this email directly or view it on GitHubhttps://github.com/Thalassicus/cat/issues/4 .

"There are 10 types of people in the world: those who understand binary, and those who don't."

stackpoint commented 10 years ago

It looks like this was code reintroduced for the Churches of Lalibela and Banaue Rice Terraces wonders. I adjusted the code so that'll it'll work as intended and have tested and confirmed the fix.