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

How to check in LUA in what direction a river flows?

Discussion in 'Civ5 - SDK / LUA' started by LDeska, Oct 4, 2010.

  1. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    I created a map script that reflects half of map. It works fine - terrain, featuers, natural wonders, starting positions, rivers - everything is mirrored but I can't get rid of very annoying thing - the river flows in incorrect directions. The reason is that nowhere in LUA files I haven't spotted a function that reads river direction. There is a check if a plot is next to a river:
    plot:IsWOfRiver()
    plot:IsNWOfRiver()
    plot:IsNEOfRiver()
    and there are functions to set it but it requires second parameter - directions which I'm unable to read from the original plot:
    mirror_plot:SetWOfRiver(true,FlowDirectionTypes.NO_FLOWDIRECTION)
    mirror_plot:SetNWOfRiver(true,FlowDirectionTypes.NO_FLOWDIRECTION)
    mirror_plot:SetNEOfRiver(true,FlowDirectionTypes.NO_FLOWDIRECTION)

    So, do I have to wait till Firaxis will finally release such info or I can somehow get this info from Civ5 files?
     
  2. Nercury

    Nercury Chieftain

    Joined:
    Aug 5, 2006
    Messages:
    126
    Location:
    Lithuania
    Can you somehow rebuild those rivers, based on where they end? I know, that may be too much work, and I haven't looked into it yet.
     
  3. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    I'm not convinced that it will be easy to determine where river ends. For now I think that easiest workaround is to remove all the rivers, copy the part of code that creates the rivers (they use recurrency for this :-O ) and modify it to put rivers on both parts of map - "source" and mirrored half... But it's ridiculous :)
     
  4. Nercury

    Nercury Chieftain

    Joined:
    Aug 5, 2006
    Messages:
    126
    Location:
    Lithuania
    Oh well, I guess it is still better than rivers uphill ;)
     
  5. EmperorFool

    EmperorFool Chieftain

    Joined:
    Mar 2, 2007
    Messages:
    9,633
    Location:
    Mountain View, California
    GetRiverDirection() probably won't fly because a plot can be multiple rivers. What about GetWRiverDirection() or GetRiverDirectionW()? Is there no way to inspect the objects in Lua as there is in Python? I haven't had a chance to dive in yet, but I believe you can output all of the valid indexes (methods) in a table (object) in Lua.

    Here's a stab at it. Try this once you have code that access a Plot object.

    Code:
    plot = ...
    for i, v in ipairs(plot) do
        print(i)
    end
    
    Edit: Bah, ipairs() strictly works with integer indexes. There must be a function that will return all of the named indexes.
     
  6. Nercury

    Nercury Chieftain

    Joined:
    Aug 5, 2006
    Messages:
    126
    Location:
    Lithuania
    pairs probably
     
  7. EmperorFool

    EmperorFool Chieftain

    Joined:
    Mar 2, 2007
    Messages:
    9,633
    Location:
    Mountain View, California
    :lol: That's the trouble with not playing with the language yet. :)
     
  8. LDiCesare

    LDiCesare Chieftain

    Joined:
    Dec 22, 2005
    Messages:
    2,612
    Location:
    France
    Yes, pairs returns all the indexes, ints and others.
    ipairs returns the first consecutive integer indices, starting at one.
    so for a table with elements at 0,1,2,3,5 as keys, ipairs will return the values for 1,2,3.
     
  9. EmperorFool

    EmperorFool Chieftain

    Joined:
    Mar 2, 2007
    Messages:
    9,633
    Location:
    Mountain View, California
    Ah, so ipairs() always starts at 1? I understand that tables normally start with 1 instead of 0 if you allow the indexes to be assigned automatically. And I did read that it stops with the highest consecutive index. Good to know it skips any indexes below 1, too.
     
  10. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    Eh.. I played yesterday all evening so I haven't checked it and here at work I use Linux so I'm not able to test if I can print out method names. But from your posts I understand that this pairs/ipairs will not print out method names of Plot object?
     
  11. LDiCesare

    LDiCesare Chieftain

    Joined:
    Dec 22, 2005
    Messages:
    2,612
    Location:
    France
    pairs can print method names.
    For instance
    table t =
    {
    method1 = function( self ) rreturn 42 end,
    aValue = 44
    }
    for i,j in pairs(t) do print i end
    Will print
    method1 and aValue.
    One safety trick if you want to print stuff: Concatenation si done with .. operator but it doesn't like to concatenate non string objects so make sure you use ..tostring( stuff ) when you concatenate strings in case stuff is an integer for instance.
     
  12. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    Hmm interesting, I have to test it when I'll get back home, do you think that it will work?

    Code:
    plot = ...;
    for i,j in pairs(plot) do
    print i;
    print j;
    end;
    
    How to check of what type is variable "j" ? It would be ideally if I could check somehow what are the parameters that I need to pass to this method. Maybe LUA supports reflection like in Java ;) ?

    But honestly, Firaxis haven't done good job - they released Civ5 way too early. They should release SDK-LUA interface together with game!
     
  13. Nercury

    Nercury Chieftain

    Joined:
    Aug 5, 2006
    Messages:
    126
    Location:
    Lithuania
  14. EmperorFool

    EmperorFool Chieftain

    Joined:
    Mar 2, 2007
    Messages:
    9,633
    Location:
    Mountain View, California
    There's no need to print the second value from pairs(). You just need the names (indexes) in the table. For the most part we should be able to tell what the thing is by its name, but further I expect that the objects hold only methods as they did in the Python SDK. To get the name field of a CyCity you would call CyCity.getName() which went into C++ and returned the value in the field.

    BTW, this is reflection as far as Lua is concerned. Lua has only tables and primitive values. There is no such thing as classes and objects in Lua--they are all tables. Object methods are merely a convention of storing functions in metatables.
     
  15. Nercury

    Nercury Chieftain

    Joined:
    Aug 5, 2006
    Messages:
    126
    Location:
    Lithuania
    I am just using it like javascipt, my class looks like this:

    Code:
    ClassName = function() -- << this serves as constructor too
    
        local b = function() -- << private function
            return 0
        end
    
        local self = {
            a = function() -- << public function
                return b()
            end, 
        }
    
        return self;
    end
    
    local instance = ClassName()
    
    
    This works quite well. Is it ok to do this in lua? Never found any example like this, so I am a bit wary.

    EDIT: sorry for derailing thread completely, I have just realized that...
     
  16. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    I have error when trying to run loop like

    for i,j in pairs(plot) do
    ...
    end

    I have syntax error saying:

    [1454.615] Syntax Error: [string "Assets\Maps\LD_Mirror_Pangaea.lua"]:508: '=' expected near 'i'

    it looks like it's not possible to put two variables after "for", it expects equal sign after first variable. I tried to put it inside parenthesis like for (i,j) in .. but it also didn't worked. It failed on first parenthesis.
     
  17. Nercury

    Nercury Chieftain

    Joined:
    Aug 5, 2006
    Messages:
    126
    Location:
    Lithuania
    Line

    Code:
    for offset, _ in pairs(tilesOffset) do
    
    Works fine for me. Maybe you forgot an "end" or "then" and compiler is just confused? Start by commenting-out portions of your code ;)
     
  18. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    I made it work by using this underscore but it haven't helped me at all....

    Code:
    	local plot1 = Map.GetPlot(1, 1)
    	print "LD start"
    	--print plot1
    	local offset;
    	for offset, _ in pairs(plot1) do
    		print(offset)
    	end
    	print "LD stop"
    
    produced:

    Code:
    [8520.369] Map Script: Map Generation - Normalize City State Locations
    [8520.369] Map Script: LD start
    [8520.369] Map Script: __instance
    [8520.369] Map Script: LD stop
    [8520.369] Map Script: -------------------------------
    [8520.369] Map Script: Map Generation - Adding Goodies
    
    so in fact it iterated only once, the key is equal "__instance" :(
     
  19. LDiCesare

    LDiCesare Chieftain

    Joined:
    Dec 22, 2005
    Messages:
    2,612
    Location:
    France
    Why should the plot have data in the first place? It's probably a pointer to a C++ object.
    What are you trying to retrieve? The rivers adjacent to a plot? It's probably doable only through function calls, not iterations.
     
  20. LDeska

    LDeska LDeska

    Joined:
    Jan 22, 2003
    Messages:
    411
    Gender:
    Male
    Location:
    Poland
    I want to read direction of river - I know it's doable via function calls but I don't know how those functions are named. EmperorFool proposed to read function names using such loop - but it doesn't work.
    I'm really angry over Firaxis for not releasing that SDK-LUA interface... I'll keep trying and will notify you with my results
     

Share This Page