Adding a Feature

S3rgeus

Emperor
Joined
Apr 10, 2011
Messages
1,270
Location
London, United Kingdom
So I've added a new feature in XML and have gotten started on implementing the lua to distribute it across the map. I've been working on a simple InGameUIAddin approach to placing it on the map, rather than integrating it full-whack into AssignStartingPlots.lua. (Consequently, is this approach doomed to failure? I'm sure there are others who know more about feature distribution than me.) I don't need the new feature to be distributed in any pattern or frequency across the map, I just need large concentrations of it at the top and bottom. (Assuming the map Wraps in the x-direction.) So I've been taking cues from how the ice feature is placed in assignstartingplots.lua, and that's been helpful, but I've run into a few problems:

My file activates and places the feature at the end of the player's first turn (turns out the GameEvent PreGameStart also doesn't fire. If anyone knows of an event I can hang my function onto that fires before the game starts, that would be great.) and the feature doesn't show up graphically on the map. (I've used the marsh art define tag just so I have something to see.) I'm thinking this is because the map is already 'drawn', so there aren't any aesthetic changes after the game starts. Is there some kind of 'refresh' function that tells the game to reload the textures on the hexes?

My other problem is, obviously, not all map types have geography that is in any way similar, so a single distribution setting doesn't work too great. But, using the ice feature as a template again, I only see ice placed by a single algorithm in the assignstartingplots.lua, not in each map type's individual lua file, yet ice seems quite different on the different map types. Are there adjustments made for map type or world size that I'm missing?

As an aside, is there an easier way to iterate over the map than looping through all of the hexes starting at 0,0 all the way to mapwidth,mapheight?

There were other things I wanted to ask, but I can't remember everything that went wrong all at once. :lol:

EDIT: I want to be able to keep track of how many hexes of the new feature have been put down on the map, but over the course of the entire game rather than just while it's distributing it. Is it possible to declare a global variable that will be accessible at any time? (later turns) Or, more simply, what would be the best way to preserve a counting integer over the course of the game?
 
I'm thinking this is because the map is already 'drawn', so there aren't any aesthetic changes after the game starts. Is there some kind of 'refresh' function that tells the game to reload the textures on the hexes?

No there is no refresh function, and yes, your guess is correct as to why a lot of things won't appear; most of the graphical decisions are made during initialization. If you do something like change the terrain type of a hex, it'll only draw it correctly the next time you reload the game (since that'd re-generate the graphics).

Now, improvements, certain features, and resources ARE designed to be added to a map after the fact. Forests and Jungles can be added mid-game and will display correctly. So it's not that it's inherent to Features in general, but the specifics of the art define in question. (Ice, for instance, can't be added the same way, despite being a feature.)

As an aside, is there an easier way to iterate over the map than looping through all of the hexes starting at 0,0 all the way to mapwidth,mapheight?

Depends what you're trying to do. There are a few tricks you can do if, for instance, you're trying to modify an improvement that's been placed on the map within controlled territory.
If you're trying to make a feature that can only be placed in polar regions, then you'd still go 0-width on the X direction, but for the Y you'd only need to go something like 0-5 and (height-5)-height. No need to iterate over the middle areas if they'll never get that feature.

Or, more simply, what would be the best way to preserve a counting integer over the course of the game?

There are a few different ways to create pseudo-counters. For instance, you can create a Project, set its cost to -1 so no one can build it, and increment the number of times it's been completed through Lua. But that doesn't really work for the type of thing you're describing.
The question is why you even NEED a counter. If you only want to place a specific number of this feature, then the easier way to do it is to loop 1 to N, where N is the number you want to place, and then pick a random hex within whatever criteria you want to place that feature on.
 
No there is no refresh function, and yes, your guess is correct as to why a lot of things won't appear; most of the graphical decisions are made during initialization. If you do something like change the terrain type of a hex, it'll only draw it correctly the next time you reload the game (since that'd re-generate the graphics).

Now, improvements, certain features, and resources ARE designed to be added to a map after the fact. Forests and Jungles can be added mid-game and will display correctly. So it's not that it's inherent to Features in general, but the specifics of the art define in question. (Ice, for instance, can't be added the same way, despite being a feature.)

I was hoping that forests and jungles growing would be helpful for this. Is it possible to declare a new art define that shares some property with forests and jungles that would allow it to be redrawn mid-game? I did notice that in the plot object, there's a member function SetPlotType() which takes in a type value and then two booleans, the second of which indicates whether or not to 'rebuildgraphics', unfortunately it didn't seem to change anything.

I know that art defines aren't modular like most of the rest of XML, so I'll need to replace the existing art define file. (I was planning on doing that eventually anyway.)



Spatzimaus said:
Depends what you're trying to do. There are a few tricks you can do if, for instance, you're trying to modify an improvement that's been placed on the map within controlled territory.
If you're trying to make a feature that can only be placed in polar regions, then you'd still go 0-width on the X direction, but for the Y you'd only need to go something like 0-5 and (height-5)-height. No need to iterate over the middle areas if they'll never get that feature.

Very good point, that drastically decreases the number of plots it needs to deal with, I'll just scan through the polar regions.



Spatzimaus said:
There are a few different ways to create pseudo-counters. For instance, you can create a Project, set its cost to -1 so no one can build it, and increment the number of times it's been completed through Lua. But that doesn't really work for the type of thing you're describing.
The question is why you even NEED a counter. If you only want to place a specific number of this feature, then the easier way to do it is to loop 1 to N, where N is the number you want to place, and then pick a random hex within whatever criteria you want to place that feature on.

Ideally I'd like the feature to 'spread' throughout the course of the game. I was thinking to make it more balanced across different map types it would spread less if there was already a lot of it on the map to avoid consuming entire regions. (It's got some negative yields, so it would be quite disadvantageous for a civilization to get surrounded by it.)
 
Is it possible to declare a new art define that shares some property with forests and jungles that would allow it to be redrawn mid-game?

Not that I can tell. The reason forests and jungles work for this is that they're designed from the start to be added onto an existing terrain, instead of replacing it graphically. Hills, on the other hand, replace the existing terrain's graphics with their own, so adding a hill to a plot won't have a graphical effect until you reload the game. Ice seems to replace the tiles' graphics, so it'd suffer the same sort of problem.

There's also a graphical issue with removing an improvement, feature, or resource; doing this can cause crashes, and even when it works correctly it'll sometimes leave the previous graphic behind (most commonly with resources, since they're not designed to be removed in the course of a game).

I know that art defines aren't modular like most of the rest of XML, so I'll need to replace the existing art define file. (I was planning on doing that eventually anyway.)

Depends what you're doing. If you're trying to use something that's identical to an existing graphical option, then you don't need to modify that file. But yes, any change beyond that would require replacing the files in question in their entireties. For many of these files, that means extracting them from the packages they're hidden inside.
 
If you want a graphical representation that doesn't confuse the out of you would fallout work better. It should work in a similar fashion to forrest and jungle in terms of adding it during game.
 
Spatzimaus said:
Not that I can tell. The reason forests and jungles work for this is that they're designed from the start to be added onto an existing terrain, instead of replacing it graphically. Hills, on the other hand, replace the existing terrain's graphics with their own, so adding a hill to a plot won't have a graphical effect until you reload the game. Ice seems to replace the tiles' graphics, so it'd suffer the same sort of problem.

There's also a graphical issue with removing an improvement, feature, or resource; doing this can cause crashes, and even when it works correctly it'll sometimes leave the previous graphic behind (most commonly with resources, since they're not designed to be removed in the course of a game).

I've gotten around that graphical issue by adding my function into AssignStartingPlots.lua. (Just an include and a call.) I hadn't heard about those crashes, so I'll avoid removing resources from the map, is it possible to change the tech that reveals them mid-game to hide them that way?

Spatzimaus said:
Depends what you're doing. If you're trying to use something that's identical to an existing graphical option, then you don't need to modify that file. But yes, any change beyond that would require replacing the files in question in their entireties. For many of these files, that means extracting them from the packages they're hidden inside.

Yeah, I've extracted them now, but it took me forever to figure out that where that option was in Nexus lol.

MdREV said:
If you want a graphical representation that doesn't confuse the out of you would fallout work better. It should work in a similar fashion to forrest and jungle in terms of adding it during game.

Very good idea; I've changed it to use the fallout art define. It still didn't show up when I added it on the first turn, but I've got it working through assignstartingplot now. I'll look into the way nukes work to see if I can get the growing thing working soon.
 
I hadn't heard about those crashes, so I'll avoid removing resources from the map, is it possible to change the tech that reveals them mid-game to hide them that way?

Well, even assuming you COULD do that (and I'm not sure you can), that'd hide all of them at once, and would make that resource unavailable for use.

What I'm talking about is this: let's say that I want to create an "Alchemy" improvement that, when placed on certain resource types, converts them into OTHER resources. So Alchemy, placed on an Iron deposit, changes it into a Gold deposit and then harvests it for you. Iron needs to still be on the map, I just want something that can use up the old Iron deposits once they stop being useful.

Ideally, then, I'd want to have this improvement trigger a Lua function that would remove the Iron resource and place a Gold resource on the same hex. The problem is that removing the Iron resource sometimes causes a crash, and even when it doesn't, it wouldn't always cause the Iron resource graphic to disappear, because the game isn't designed to have resources disappear once they're on the map. The same goes for many other things.

You can get this crash a few other ways, as well. For instance, if you place a Trading Post on a hex and then it later turns out to have a Uranium, then placing a Mine on the Trading Post can cause a crash. (Not consistently, but it happens.) The key seems to be the removal of the previous Improvement and/or any features (like the forest or jungle that trading post might be in). I found this out when testing my terraforming logic, which involves removing a temporary Improvement and then placing a forest, jungle, etc.; the problem seemed to happen when removing the old Improvement, not the addition of the new features, and it depended on how much else was going on at the same time (implying that it's something graphics-related, where it just can't handle the extra load or something).
 
Back
Top Bottom