1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Improvement grand resources

Discussion in 'Civ5 - SDK / LUA' started by Gilgamesch, Sep 3, 2013.

  1. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    Hey all, like the title say.
    Iam searching for a way to give improvement x the ability to provide resource y.
    Also with the lostof the resource when the improvement was pillaged.
    Iam new to lua, and still learning.
    So if someone of you nice guys could tell me where to start, or any other help would be very welcome.:D
     
  2. LastSword

    LastSword Prince

    Joined:
    Feb 24, 2013
    Messages:
    1,010
    1. Check every tile on start. Store tiles with your improvement.
    2. Check every tile during game if new improvement comes up.* Store tiles with improvement.
    3. During Barbarian PlayerDoTurn go through all stored tiles, checking if pillaged and for owner. You should easily count how many certain Player has those improvements (you can even check if tile is worked). You have to save/store data from previous turn. So if player had 6 improvements and now it has only 5, you reduce your resource by 1 and then store 5 for next turn.
    4. Avoid typos in title causing misinformation and chaos.


    *depends on your improvement, if it can be build only by certain civilization, in borders, on land etc. In worst scenario you can always go through all tiles on map. Though even if you don't want to detect on culture/owner change you should make base selection on start/load.
     
  3. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    Great thanks, iam not planning to loop to all tiles, this would slow down the game massiv i guest.
    Only a few different requirements for the improvement.
    Iam going to try it when i got time....
     
  4. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    BUMB


    I know do it for your self, but if anybody has done a code that would fit with what i need, i would be very thankfull for help.

    In fact i only need a loop trough all coast, see and flat terrain.
    If the improvement exist ok, if not remove the resource.
    Well maybe a stupid question, but hey help me to learn and i will not nerve you in the future with stupid questions.
    Also thousand of peoples would play with your code in the future, anyway please help me....
     
  5. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    Ok redox has created a lua code, that exacty do what iam looking for.
    So altered it and changed it. So its working but i got the message in the database that thecache will not shared. This means when you pillage the related improvement you will get+1 resource instead of -1

    This is the code.

    Spoiler :
    print("---ImprovementsGrandsResources---");
    include( "SaveUtils" ); MY_MOD_NAME = "ImprovementsGrandsResources";

    --remote installation registry. Every time after modifying, make sure to call updateSaveData()
    local aManufactory = {};

    local mapMaxX, mapMaxY = Map.GetGridSize();



    function onImprovementCreated(iHexX, iHexY)
    local pPlot = Map.GetPlot(ToGridFromHex(iHexX, iHexY));
    if (pPlot~= nil) then
    local improvementType = pPlot:GetImprovementType();
    if(improvementType == GameInfoTypes.IMPROVEMENT_MANUFACTORY) then

    local plotID = pPlot:GetX() + pPlot:GetY() * mapMaxX;

    --local playerTeamID = Game.GetActiveTeam();
    local playerID = pPlot:GetOwner();

    local isPillaged = pPlot:IsImprovementPillaged();

    if(isPillaged) then
    if( aManufactory[plotID]~=nil and aManufactory[plotID].PlayerID ) then
    --need to take back Manpower resource
    DisconnectManpower( plotID, playerID );
    end
    print("Manufactory Pillaged at plot ID:", plotID);
    else
    --we don't want to overwrite data if registered mine already exists on site
    if(aManufactory[plotID]==nil) then
    print("Manufactory Built or Repaired", plotID);
    ConnectManpower( plotID, playerID );
    end
    end

    end
    end
    end
    Events.SerialEventImprovementCreated.Add(onImprovementCreated)


    function onImprovementDestroyed(iHexX, iHexY)
    local plotX, plotY = ToGridFromHex(iHexX, iHexY);
    local plotID = plotX + plotY * mapMaxX;

    if(aManufactory[plotID]) then
    --local improvementType = pPlot:GetImprovementType();
    --if(improvementType == GameInfoTypes.IMPROVEMENT_MANUFACTORY) then

    -- there used to be Manufactory here - remove it
    print("onImprovementDestroyed: ManufactoryDestroyed");
    DisconnectManpower( plotID, aManufactory[plotID].PlayerID );
    end

    end
    Events.SerialEventImprovementDestroyed.Add(onImprovementDestroyed)


    function onPlotOwnershipChange(iHexX, iHexY)
    local plotX, plotY = ToGridFromHex(iHexX, iHexY);
    local plotID = plotX + plotY * mapMaxX;

    if(aManufactory[plotID]) then
    print("onPlotOwnershipChange: Manufactory Ownership hac changed");
    DisconnectManpower( plotID, aManufactory[plotID].PlayerID );
    local pPlot = Map.GetPlot(ToGridFromHex(iHexX, iHexY));
    if(pPlot) then
    local newOwner = pPlot:GetOwner();
    ConnectManpower( plotID, newOwner );
    else
    print("ERROR in ManufactoryControl: Map.GetPlot returned nil");
    end
    end

    end
    Events.SerialEventHexCultureChanged.Add(onPlotOwnershipChange)

    --Events.SerialEventCityCaptured.Add(CityCultureOnCapture)


    function ConnectManpower( plotID, playerID )

    local pPlayer = Players[playerID];
    local iResourceID = GameInfoTypes.RESOURCE_MANPOWER;
    local iResourceNum = 2;

    if(pPlayer) then
    pPlayer:ChangeNumResourceTotal(iResourceID, iResourceNum);
    end

    --register/update mine status
    aManufactory[plotID] = {};
    aManufactory[plotID].PlayerID = playerID;
    updateSaveData(plotID);

    print("Connected resource ", iResourceID, " for player ", playerID, "amount:", iResourceNum);
    end


    function DisconnectManpower( plotID, playerID )

    local pPlayer = Players[playerID];
    local iResourceID = GameInfoTypes.RESOURCE_MANPOWER;
    local iResourceNum = 1;

    if(pPlayer) then
    pPlayer:ChangeNumResourceTotal(iResourceID, -iResourceNum);
    end

    --update Manufactory status
    --aManufactory[plotID].PlayerID = false;
    aManufactory[plotID] = nil;
    updateSaveData(plotID);

    print("Disconnected resource ", iResourceID, " for player ", playerID, "amount:", iResourceNum);
    end


    function updateSaveData(plotID)
    local pPlayer = Players[Game.GetActivePlayer()];
    save( pPlayer, plotID, aManufactory[plotID] );
    end


    function loadSaveData()
    local pPlayer = Players[Game.GetActivePlayer()];
    local wholeTable = load( pPlayer );
    for plotID, v in pairs(wholeTable) do
    print("plotID:",plotID);
    for ii,kk in pairs(v) do
    print(ii,kk);
    end

    aManufactory[plotID] = v;

    end
    end
    loadSaveData(); --call on startup


    Well maybe the SaveUtils is not compatible with CCTP or iam using a older version.

    What we need is a tipp how to get the cache shared be the database thanks guys for the help.
     
  6. LastSword

    LastSword Prince

    Joined:
    Feb 24, 2013
    Messages:
    1,010
    Events.SerialEventImprovementDestroyed / Created seems to work only if Human Player has vision on the plot. At least before BNW I had issues with it.

    Now, your code seems to add +2 resource, but removes only 1.
    Code:
    local iResourceNum = [COLOR="Red"]2[/COLOR];
    
    if(pPlayer) then
    pPlayer:ChangeNumResourceTotal(iResourceID, iResourceNum);
    Code:
    local iResourceNum = [COLOR="Red"]1[/COLOR];
    
    if(pPlayer) then
    pPlayer:ChangeNumResourceTotal(iResourceID, -iResourceNum);
     
  7. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    Yip thats intended, also its working now correct but still got the message in the database.
    Thanks for the reply.
     
  8. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    Quck question to the lua shamanes outthere.
    What new lines are needed to improve the code above, to only work when at least one era is triggered by the player.
    I know, i read the api functions, and also dons great lua tutorial for all the functions.
    But if someone could give me come code or a tipp, this would be very welcome, and will get endless thanks.
     
  9. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    :bump:
    Can someone please explaine me how i can adjust the code above to be only working when a related technology is discovered or even era is reached.
    Thanks for your time)
     
  10. Tomatekh

    Tomatekh Sahib-i-Qirani

    Joined:
    Aug 6, 2012
    Messages:
    1,386
    Get the team of pPlayer

    local pTeam = pPlayer:GetTeam();

    Then check the era with

    if (Teams[pTeam]:GetCurrentEra() >= [here you can use the era number, Ancient era is 0, etc., or define a specific era. i.e. GameInfoTypes.ERA_INDUSTRIAL] then

    or check a specific tech with

    if (Teams[pTeam]:IsHasTech( your defined tech )) then
     
  11. Gilgamesch

    Gilgamesch Ancient Alien

    Joined:
    Dec 15, 2010
    Messages:
    2,228
    Location:
    good old germany
    Big thanks, will try to create the code when i get some free time:)
     
  12. Ambug666

    Ambug666 Chieftain

    Joined:
    Jun 23, 2009
    Messages:
    92
    So I was able to get the code to work. I was even able to modify it so that farms produce horses. Then I created my own resource and got it so that farms produced that new resource.

    Then I created my own improvement and tried to modify it further, but Events.SerialEventImprovementCreated isn't firing for that new improvement. Not at all. Any thoughts? My workers are definitely creating the new improvement, but my trace messages aren't showing up when it is created. They still show up when other improvements are created.

    Any ideas? Thanks!
     
  13. Pazyryk

    Pazyryk Deity

    Joined:
    Jun 13, 2008
    Messages:
    3,584
    Events are used by game engine or graphics engine (stuff we don't have access to) to control graphics or other UI stuff. When you see Events.SerialEventImprovementCreated, you should be thinking "creation of the improvement graphics object". These have nothing to do with the objects that you think of for game rules. It just so happens that some Events "work" in the sense that the graphics object is created at roughly the same time as the game rule object. But usually that is not the case. And it's a risky thing to rely on even when it is true. In this case, the human player has to see the improvement before the graphic is created.

    Some modders keep bashing on Firaxis for this. Or for patching the graphics system (perhaps they want it to work like it did in September 2010?). Perhaps Firaxis shouldn't have exposed these to modders at all. It would have been nice if they cleared up this misunderstanding very early. It would have been nice if there were more GameEvents.
     
  14. Ambug666

    Ambug666 Chieftain

    Joined:
    Jun 23, 2009
    Messages:
    92
    Aha. And since my improvement is creating a game object but not a graphic object (debugging needed, but I can handle that), the event isn't being called. Once I get my improvement to create the graphic object, life will -- hopefully-- be good.

    Thanks!
     
  15. Pazyryk

    Pazyryk Deity

    Joined:
    Jun 13, 2008
    Messages:
    3,584
    Better not to hook it to graphics at all...

    I just remembered that there is a new GameEvents (BNW only):

    GameEvents.BuildFinished( playerID, x, y, improvementID )

    Use that instead. There is a list of all of these here (bookmark it).
     
  16. Ambug666

    Ambug666 Chieftain

    Joined:
    Jun 23, 2009
    Messages:
    92
    Great! That totally fixed it. Is there also one for when an improvement is destroyed? I didn't see one in the list on that page.
     

Share This Page