[TOTPP] Get Started With Lua Events

@tootall_2012 @Knighttime

Thanks for bringing this up. JPetroski explained that the limit was the reason he was using tables for his object naming, and a web search confirmed there was a limit, that the solution to exceeding this limit was to use the table method, and also that exceeding this limit probably meant that you should have multiple files anyway.

Based on some testing I did today (boy am I glad I learned how to use Vim (well, GVim) and I could put in the code with a macro), there is a 200 local variable limit per function. This applies to the over all "module," since it is also a function. If you declare a local function inside another function, that function gets its own 200 local variables, and doesn't cut in to the allotment of the "outer" function (except that there is one local variable used for the function itself). I'll correct the lesson at some point.

Disclaimer: much of what follows is an "educated" guess and could very well be wrong. But it is why I think the above statement is correct. This stuff isn't important to understand to use Lua.

This makes sense with a piece of information that I learned in a CS course I took years ago. Programs have two "kinds" of memory they can use, "static" and "dynamic". When you call a function, it needs some place in memory to store the variables it will use, so it gets some memory from the operating system and uses it while the function is active. When the instance of the function is complete, the memory is made available for other uses. This is the "static" memory. In Lua terms, our local variables are (as far as I understand) the static memory.

In a programming language like C, which is compiled, it is known how much static memory to allocate for each function, since the compiler can count the number of variable declarations during the compiling process. In Lua terms, if there are 7 uses of "local", then the function would ask for 7 "units" of static memory when the function is called. However, since Lua is not compiled (I think they do something kind of like compiling, but for our purposes I don't think it is compiling), but instead read line by line during execution, it doesn't know how much static memory to ask for. Therefore, I think the Lua creators simply said that 200 variables worth would be enough for most purposes.

"Dynamic" memory allocation doesn't require knowing how much memory you will need when you write the program, and isn't freed up once any given function is completed. Tables (and global variables, which are entries in a table) are Lua's form dynamic memory, and Lua does "garbage collecting" to check when this dynamic memory is no longer used. In a language like C, this kind of memory must manually be deallocated (and causes "memory leaks" if not done properly).
 
Lesson 6 added to the Scenario League Wiki http://sleague.civfanatics.com/inde..._Lesson_6:_Splitting_Code_into_Multiple_Files

Feedback, comments, and criticism are welcome. As part of the lesson, I link to a video where I create the munition library "live", and do much of the testing it needs. I'm not sure how valuable it is compared to the time it would take to watch, since I was familiar with what happened. After doing it, I figured that, given the small potential audience, watching and editing it would probably cost me more time than it would end up saving others. If you think there is little value in it, then I won't recommend it. On the other hand, if you think it is valuable (or would be valuable with relatively minor tweaks), and want more, say so as well. Producing an unedited (or minimally edited) video isn't a very large burden.
 
CHALLENGE: How to design a LUA event so that the Foreign Minister includes Military Might and Territorial Extent in the "power" calculation.

It's PERFECTLY fine if NO 1 can figure this 1 out. I just want to know if it's possible. Thanks in advance, though.
 
Lesson 7 added to the Scenario League Wiki http://sleague.civfanatics.com/inde...ents_Lesson_7:_Saving_Data_in_the_State_Table

Feedback, comments, and criticism are welcome. If a modification would make the lessons better, I'd really like to hear about it.

This is the last lesson I have planned, unless I get some feedback asking for more content. I probably promised to revisit something, and have since forgotten about it, so there might be that at some point when I go back through the lessons.
 
@Prof. Garfield Within the body of lesson 4, you included the following section on "Distance in Civilization II", which I'm inserting as an image to preserve the formulas:

Distance in Civilization II.png


All of this makes sense to me, and it's how I've always believed that Civ2 calculated distance. If you're talking about unit movement, it's certainly true that it reflects the number of moves it will take a unit to get from x1,y1 to x2,y2 (ignoring terrain movement cost, etc.).

But as part of working on a formula for corruption, I also had to incorporate the concept of distance (from a city to the capital of that tribe) and it turns out that at least in the corruption formula, this doesn't seem to be the calculation that is used. (I was definitely surprised! Actually I was pretty frustrated for awhile first, because my code was generating results that didn't match the game, until I finally tracked down the issue.)

In the corruption formula, the game treats "corner" moves -- that is, truly horizontal or vertical -- as counting 1.5 tiles instead of 1 tile. This is apparently an approximation of √2 ≈ 1.414. A distance formula that I believe works correctly in Lua, at least within the context of the corruption calculation, is:
Code:
xdiff = math.abs(tile1.x - tile2.x)
ydiff = math.abs(tile1.y - tile2.y)
distance = math.floor(math.min(xdiff, ydiff) + (math.abs(xdiff - ydiff) * 0.5 * 1.5))

It's possible that the game engine uses both of these distance formulas, in different situations. But I wanted to share what I've learned recently based on my experimentation.
 
Last edited:
Thanks @Knighttime . The distance formula you provide seems vaguely familiar; I'm pretty sure it is the same one that is used for calculating trade bonuses. I suspect that all distances in the game will use the same distance function. I should probably update that section in my lessons, and perhaps, remind that world shape must be taken into account as well.
 
I guess it would make sense to me if trade bonuses used the same formula. Do you think it would be a fair working assumption that anything to do with unit movement or range (e.g. paradrops) uses "taxicab" distance, but all other distance calculations use a form of the one I provided? I'm sure I don't know all of the instances where distance is relevant though.

It's true that I've been working on flat maps and haven't tried to account for ones that wrap around, so the formula I provided above isn't as flexible as it needs to be.
 
Top Bottom