[GS] (UI) Buying/Selling StrategicResources

Zegangani

King
Joined
Oct 9, 2020
Messages
893
I've edited Windfly's Mod (UI) Strategy Resources to Production so you can buy resourses with Gold instead of selling resources for production. I got it to work, problem is that i've made it so that you can't buy resources untill you have a certain amount of money in the treasury, so you really can't buy 'em intill you have that amount of money,( but when you got that money amount you can buy , say resource 1, and also after the gold in the treasury got below the required amount, just a few times , but you can also buy at the same time some other resources for some few times and the Treasury is keeping going into negative amounts (just in the same turn, if you click next turn and treasury is below the amount needed you can't buy resources))==> This Bug occured because the m_nNewGoldAdd numbers were negative, I fixe that by chaniging them to postive numbers, and by adding the Minus sign to 'm_nNewGoldAdd' in 'OnSellResources' function. I've also made a boostedThisTurn so you can only buy one time in a turn (if that's the real purpuse of it), but it doesn't have any result.

here is the complete code:
CityPanel:
Code:
GameEvents = ExposedMembers.GameEvents;

local m_iResources = {
    GameInfo.Resources["RESOURCE_HORSES"].Index,
    GameInfo.Resources["RESOURCE_IRON"].Index,
    GameInfo.Resources["RESOURCE_NITER"].Index,
    GameInfo.Resources["RESOURCE_ALUMINUM"].Index,
    GameInfo.Resources["RESOURCE_COAL"].Index,
    GameInfo.Resources["RESOURCE_OIL"].Index,
    GameInfo.Resources["RESOURCE_URANIUM"].Index
    };
local m_iTechs = {
    GameInfo.Technologies["TECH_ANIMAL_HUSBANDRY"].Index,
    GameInfo.Technologies["TECH_BRONZE_WORKING"].Index,
    GameInfo.Technologies["TECH_MILITARY_ENGINEERING"].Index,
    GameInfo.Technologies["TECH_RADIO"].Index,
    GameInfo.Technologies["TECH_INDUSTRIALIZATION"].Index,
    GameInfo.Technologies["TECH_REFINING"].Index,
    GameInfo.Technologies["TECH_COMBINED_ARMS"].Index
    };
local m_nNewGoldAdd = {
    20,
    40,
    60,
    80,
    100,
    150,
    200
    };

function CanSellResource(PlayerID, mTreasury, nNewGoldAdd, bDecrease)
    local pPlayer = Players[PlayerID];
    local pTreasury:number = pPlayer:GetTreasury():GetGoldBalance(mTreasury);
    if bDecrease then
        pTreasury = pTreasury - nNewGoldAdd ;
    end
    if (pTreasury >= 100 ) then
        return false ;
    else
        return true ;
    end
end

function HasResourceTech(PlayerID, mResource )
    local pPlayer = Players[PlayerID];
    local bTech:boolean = not pPlayer:GetTechs():HasTech(m_iTechs[mResource]);
    return bTech;
end

function OnEnterCityPanel( ownerPlayerID:number, cityID:number, i:number, j:number, k:number, isSelected:boolean, isEditable:boolean)
    print("City Selected.");
    Controls.SRP_SellHorseCheck:SetHide(HasResourceTech(ownerPlayerID,1));
    Controls.SRP_SellHorseCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,20, false));
    Controls.SRP_SellHorseCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,1); end );

    Controls.SRP_SellIronCheck:SetHide(HasResourceTech(ownerPlayerID,2));
    Controls.SRP_SellIronCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,40, false));
    Controls.SRP_SellIronCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,2); end );

    Controls.SRP_SellNiterCheck:SetHide(HasResourceTech(ownerPlayerID,3));
    Controls.SRP_SellNiterCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,60, false));
    Controls.SRP_SellNiterCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,3); end );

    Controls.SRP_SellAluminumCheck:SetHide(HasResourceTech(ownerPlayerID,4));
    Controls.SRP_SellAluminumCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,80, false));
    Controls.SRP_SellAluminumCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,4); end );

    Controls.SRP_SellCoalCheck:SetHide(HasResourceTech(ownerPlayerID,5));
    Controls.SRP_SellCoalCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,100, false));
    Controls.SRP_SellCoalCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,5); end );

    Controls.SRP_SellOilCheck:SetHide(HasResourceTech(ownerPlayerID,6));
    Controls.SRP_SellOilCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,150, false));
    Controls.SRP_SellOilCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,6); end );

    Controls.SRP_SellUraniumCheck:SetHide(HasResourceTech(ownerPlayerID,7));
    Controls.SRP_SellUraniumCheck:SetDisabled(CanSellResource(ownerPlayerID,mTreasury,200, false));
    Controls.SRP_SellUraniumCheck:RegisterCallback( Mouse.eLClick, function() OnSellResources(ownerPlayerID,cityID,7); end );
end

function OnEnterGame()

    Controls.SRP_SellHorseCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));
    Controls.SRP_SellIronCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));
    Controls.SRP_SellNiterCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));
    Controls.SRP_SellAluminumCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));
    Controls.SRP_SellCoalCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));
    Controls.SRP_SellOilCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));
    Controls.SRP_SellUraniumCheck:ChangeParent(ContextPtr:LookUpControl("/InGame/CityPanel/MainPanel"));

end

-- ================================卖资源===========================================

function OnSellResources(playerID,cityID,mResource)
    print("Click Sell Resources");
    if playerID == Game.GetLocalPlayer() then
        local pPlayer = Players[playerID];
        local iResource = m_iResources[mResource];
        ExposedMembers.SRP.SellResources(playerID,cityID,iResource,10, - m_nNewGoldAdd[mResource]);
        RecheckResouces(playerID,mResource);
        UI.PlaySound("Click_Confirm");
    end
end
 
function RecheckResouces(playerID,iResource)
    if (iResource == 1) then
        Controls.SRP_SellHorseCheck:SetDisabled(CanSellResource(playerID,mTreasury,20,true));
    elseif (iResource == 2) then
        Controls.SRP_SellIronCheck:SetDisabled(CanSellResource(playerID,mTreasury,40,true));
    elseif (iResource == 3) then
        Controls.SRP_SellNiterCheck:SetDisabled(CanSellResource(playerID,mTreasury,60,true));
    elseif (iResource == 4) then
        Controls.SRP_SellAluminumCheck:SetDisabled(CanSellResource(playerID,mTreasury,80,true));
    elseif (iResource == 5) then
        Controls.SRP_SellCoalCheck:SetDisabled(CanSellResource(playerID,mTreasury,100,true));
    elseif (iResource == 6) then
        Controls.SRP_SellOilCheck:SetDisabled(CanSellResource(playerID,mTreasury,150,true));
    elseif (iResource == 7) then
        Controls.SRP_SellUraniumCheck:SetDisabled(CanSellResource(playerID,mTreasury,200,true));
    end
end

-- ===========================================================================

Events.LocalPlayerTurnBegin.Add(OnEnterGame);
Events.TreasuryChanged.Add(                OnEnterCityPanel );

and in ScriptSetUp:
Code:
ExposedMembers.SRP = {};

function SellResouces(playerID, iPlayer, iResource, nResource, pNewGoldAdd)
    local pPlayer = Players[playerID];
    local pTreasury = pPlayer:GetTreasury();
    pTreasury:ChangeGoldBalance(pNewGoldAdd);
    pPlayer:GetResources():ChangeResourceAmount(iResource, nResource);
end

ExposedMembers.SRP.SellResources = SellResouces;

Can anyone help me with this? please :)!!
 
Last edited:
I don't have time to analyse code (and I'm bad at it), so is there any error in the lua. log?

when I debug my code I use a lot of "print" to display in the log the values of my variables before conditionnal blocks.
 
No errors at all in the lua.log
 
Top Bottom