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

Lua Scripting Possibilities

Discussion in 'Civ2 - General Discussions' started by JPetroski, Jan 21, 2018.

  1. Prof. Garfield

    Prof. Garfield Chieftain

    Joined:
    Mar 6, 2004
    Messages:
    1,858
    Location:
    Ontario
    Counting units is how I would implement that functionality.

    I counted settlers and engineers for SorFox earlier

    <a href="https://forums.civfanatics.com/threads/lua-scripting-possibilities.627220/page-6#post-15188320">Lua Scripting Possibilities</a>

    Try writing the code from scratch. Look at the code to understand how it works (I offer fairly detailed explanation on a slightly incorrect version of the code a few posts up), but don't just try to replace the "key bits." Practice thinking about the instructions you must give the computer to get it to do what you want. In fact, start first by writing a function that counts the number of 7s in a table.
     
  2. JPetroski

    JPetroski Chieftain

    Joined:
    Jan 24, 2011
    Messages:
    908
    First off, I do want to thank both of you again for all of your help here. I know I am being very needy. I do offer what I feel is a reasonable excuse: I'm trying like mad to finalize this scenario and get it to a point to alpha test asap because my wife and I are expecting our second child any day now (she has a doctor's appointment today and it might be the day). I suspect I will occasionally have time to alpha test and do things that I can do quickly but this stuff is very challenging for me and I have a hard time doing it with distractions, hence why I'm asking rapid fire questions like a machine gun! Also, if I drop off the face of the planet suddenly for a few days, you know what happened :)

    With that said, I do think your approach to this will help me learn, Prof. Garfield (I might have to forgo the 7's experiment for now though for the sake of expediency). I'll try this a little bit later but looking at the code and trying to understand how it works, I am skeptical it is going to work for my needs because it seems you're basing it off of civ.getActiveUnit() and civ.getCurrentTile(), neither of which I suspect will work for a situation with units that have 0 movement points (they'll never be "active"). I do think I might be able to modify it by using for unit in civ.iterateUnits() do because my understanding is that this assigns an integer to every single unit in the game at the moment, which would work well here. For now though, here's my interpretation of your code:

    function doThisOnKeyPress(keyCode)
    if keyCode == 214 then -- Condition is that BackSpace is pressed here you are saying that this is going to fire if and only if backspace is pressed
    local activeTile = nil I think you're starting the count at nil so that the first assumption is that there is nothing and it gives the computer a way to recognize that "1" is "1"?
    if civ.getActiveUnit() then --Check that there is an active unit you want to make sure that the key press will only do something if there is an actual unit that is active on the tile (so you can't press it over empty land and have something happen)
    activeTile = civ.getActiveUnit().location -- Gets the acive unit and selects the tile
    else --Code for case where there is no active unit
    activeTile = civ.getCurrentTile()
    end
    local settlerType = civ.getUnitType(0) on this and the next line you define which unit is what so that you can use the phrase "settlerType" or "engineerType" rather than an integer (the unit)
    local engineerType = civ.getUnitType(1)
    local numberOfSettlers = 0 I'm pretty sure here you are doing this so that the count starts at 0?
    local numberOfEngineers = 0 same?
    local firstSettler = nil
    for currentUnit in activeTile.units do this and the next few lines is you telling the computer if the current active unit is in the current active tile, you want it to first see if it is a Settler and if so you want number of settlers count to increase by 1
    if currentUnit.type == settlerType then
    numberOfSettlers = numberOfSettlers+1 this is how you are achieving the count sequence
    if numberOfSettlers == 1 then
    firstSettler = currentUnit
    end
    end
    if currentUnit.type == engineerType then --I think I cna I don't think I need this stuff
    numberOfEngineers = numberOfEngineers+1
    end
    end


    I think instead, I could probably use something like this:

    if numberOfSettlers >=1 and numberOfEngineers>=1 then
    civ.ui.text("Allies get $100")
    tribe = civ.getTribe(1)
    tribe.money = tribe.money + 100

    I am wondering, however, if I have to keep doing this 29 more times:

    if numberOfSettlers >=2 then
    civ.ui.text("Allies get $200")
    tribe = civ.getTribe(1)
    tribe.money = tribe.money + 200

    if numberOfSettlers >=3 then
    civ.ui.text("Allies get $300")
    tribe = civ.getTribe(1)
    tribe.money = tribe.money + 300

    or if there is simply a way to say:

    if numberOfSettlers >=0 then
    Multiply that number by 100 This is the line I have NO idea how to achieve
    local=theResultOfThatLineAbove --even though I might need to put this outside of this "if"
    tribe = civ.getTribe(1)
    tribe.money = tribe.money + theResultOfThatLineAbove


    I'll look at this all in more detail later assuming I still only have one kid!
     
  3. Prof. Garfield

    Prof. Garfield Chieftain

    Joined:
    Mar 6, 2004
    Messages:
    1,858
    Location:
    Ontario
    If your goal is getting this done before your next child arrives (a very understandable goal), upload your scenario and I'll write the events. That will probably be easier for both of us.
     
  4. JPetroski

    JPetroski Chieftain

    Joined:
    Jan 24, 2011
    Messages:
    908
    I'd be interested in your feedback regarding the scenario as well as you're an active player in multiplayer so I will send you a PM with the files when I get home from work.

    This is really the only event I'm still needing as I was able to piece a few together, and you and Knighttime wrote many, so with the time crunch I'm under I will cheerfully accept your help so I can spend this last weekend getting the rest of the design files in order. Thanks!
     
  5. Prof. Garfield

    Prof. Garfield Chieftain

    Joined:
    Mar 6, 2004
    Messages:
    1,858
    Location:
    Ontario
    Using Maps For Seasons (instead of batch conversion)

    At the moment, we're still lacking the ToTPP functionality to change some parts of the game rules, particularly the terrain characteristics. @tootall_2012 has mentioned, for example, that his Napoleonic Wars scenario will still use a batch file to change seasons, at least in part for this reason.

    For scenarios that need only one map, there may be a way to avoid the use of batch files, even without the ability to change terrain characteristics. The rules.txt allows for the terrain characteristics of each map to be defined separately, so we can use up to four maps as four different seasons. (or if the game needs 2 maps, 2 seasons per map) This could allow much more frequent season changes, since it wouldn't break gameplay.

    The procedure for changing seasons wouldn't actually be all that difficult. Have a loop go over all the tiles on the current season. For each tile, copy any terrain improvements on the tile to the same tile in the new season. If the tile has a city, use the relocate city functionality to move it to the new tile. Then, cycle through all the units on the tile, and teleport them to the new 'season.' If the unit being teleported has a goto command, change the destination to match the new season.

    There might be something I've forgotten to move or property I've forgotten to update for the new season, but this looks doable.

    The 'hard' part would seem to be keeping track of where events should happen, since the tile would depend on the current season. It would probably be necessary to have some sort of getSeason() function which would return the current season (i.e. map). I'm not sure how much work it would be to convert an in progress scenario events to this format, but it might be worth considering for any future scenario
     
  6. Civinator

    Civinator Blue Lion Supporter

    Joined:
    May 5, 2005
    Messages:
    5,412
    The Caesar scenario has changing summer/winter terrain with lua functions, starting with:

    ----------------------------------------------------------------------------------------------------------------------------------------

    --g Helper functions


    --g set the terrain graphics - is called each turn and onLoad - if state.winterSeason==1 then the winter terrain is loaded, otherwise the summerTerrain

    --g also: modify the movement points of all land units to half the summer value - hard coded, has to be updated if rules are changed!

    local function setTerrainGraphicsAndRules(winterSeason)


    if winterSeason==1 then

    --g it is so cold!!!

    civ.ui.loadTerrain(0, 'wTerrain1.bmp', 'wTerrain2.bmp')


    if totpp.movementMultipliers then

    totpp.movementMultipliers.road=2

    totpp.movementMultipliers.railroad=4

    else

    civ.cosmic.roadMultiplier=2

    end

    local multiplier = civ.cosmic.roadMultiplier


    A lot more is following in the programming, but with these texts the code can be found.
     
  7. Prof. Garfield

    Prof. Garfield Chieftain

    Joined:
    Mar 6, 2004
    Messages:
    1,858
    Location:
    Ontario
    Yes, I knew Caesar's Gallic Wars has a winter/summer system. You can do a lot with the TOTPP lua functionality that currently exists. However, changing terrain move cost (to have individual terrain type variation), production and defensive bonuses is not possible with current functionality. I offered this as a way to achieve that kind of change without resorting to a batch file. This sort of map swap could also be used to represent good or bad harvests by choosing a different one each turn, and probably many more things I haven't thought of yet.
     
  8. PlutonianEmpire

    PlutonianEmpire Socially Awkward Goofball

    Joined:
    Mar 11, 2004
    Messages:
    4,772
    Gender:
    Male
    Location:
    MinneSNOWta
    So, if I wanted to replace a map with a different map from a .mp map file in the console, how would I do that?
     
  9. Prof. Garfield

    Prof. Garfield Chieftain

    Joined:
    Mar 6, 2004
    Messages:
    1,858
    Location:
    Ontario
    As far as I know, there is no easy way of doing that at the moment. And any 'hard' way would not be able to copy rivers, because the current TOTPP lua functions don't allow placing rivers on terrain (and eliminate rivers when changing terrain). What you'd want to do is start a game with the new map (which should have the same dimensions of the existing map), run a script that puts the terrain type of each square in a table, and prints that table to a file. Then, you run a script in the game where you want to replace the map that goes over each square and references the table you created to change that square to what it 'should' be.

    If you have a specific use case, I might be able to provide more details and some code.
     
  10. PlutonianEmpire

    PlutonianEmpire Socially Awkward Goofball

    Joined:
    Mar 11, 2004
    Messages:
    4,772
    Gender:
    Male
    Location:
    MinneSNOWta
    Considering my maps in mind have no rivers to begin with, that may be helpful :)

    Edit: Also, how do I fix the fertility on a map if something like this happens again?
     
    Last edited: Nov 6, 2018

Share This Page