- Joined
- Jan 24, 2011
- Messages
- 4,898
Here's a link to a video showing off one of the cooler things done with lua yet: Strategic bombing!
Over the Reich - Creation Thread
Over the Reich - Creation Thread
1. What is the integer for special resource terrain? For example, terrain has an integer of 0 - 15 and terrain with rivers has an integer ranging from -128 to -114. What is the integer for these types of terrain that have a special resource on them? I ask because currently we are tying the call up of ammunition to the terrain type, yet no units can fire on terrain with a special resource.
and he responded:I've encountered a few issues with Lua returning negative values that seem unexpected. Specifically, in at least a couple places, some things return -128 instead of +128, which seems like a situation involving signed/unsigned bytes. One example is tile.terrainType: this normally returns 0 through 15, unless the tile has a river. The river seems to subtract 128 from the normal value -- so a Grassland tile is "2" but a Grassland with a river is "-126". Another example is tile.improvements, where I think pollution (0x80 bitmask value) returns "-128". Is this working as intended? Is trial and error the best approach to see what is returned in similar cases, or is there a more general/consistent rule?
It shouldn't really matter as it is just representation, except in the case of terrainType, since there is no way to add or remove rivers now (I restrict the values to 0-15 on assignment, I'll have to change this). Just make sure you use bitwise operators in tests and assignments (e.g. to test for the presence of a river, use tile.terrainType & 0x80, to add pollution to a tile, use tile.improvements = tile.improvements | 0x80).
You would need to account for ocean tiles on which a special resource is being suppressed, but I'm not sure what issue you might be having on other special resource tiles. Can you have Lua print the value of tile.terrainType to the console on every attempt to fire? But it seems to me that if you find the base terrain type using the bitwise "&" operator, you shouldn't have to worry about specials or rivers.we are tying the call up of ammunition to the terrain type, yet no units can fire on terrain with a special resource
1000 = This means the tile has a river. Unfortunately (in my opinion, since it adds unnecessary confusion), TOTPP evaluates this as negative 128 instead of positive 128, when adding it to the value of the base terrain type.
0001 = I haven't seen this, and I don't know what it means, but this would result in the value of tile.terrainType being increased by 16
0010 = I haven't seen this, and I don't know what it means, but this would result in the value of tile.terrainType being increased by 32
I would hazard to guess these are the special indicators. It should be fairly easy to check.
That seems reasonable, and you won't find me complaining about something that works. I've been trying to use bitwise operators in my own code, though, based on TNO's recommendation.instead of checking tile.terrainType == desiredTerrain, I introduced tile.terrainType % 16 == desiredTerrain, and it seems to have worked
That's a good, detailed explanation. The essence of my question to TNO, I think, was that it seems to me that Lua isn't consistent about when it does this. I mentioned tile.terrainType and tile.improvements as two cases where Lua can return negative numbers, apparently using twos complement as you said. But I'm pretty sure I've encountered some other similar fields where an integer value in Lua was always positive, implying that the left-most bit was not being treated as a negative indicator (i.e., twos complement was not being used). Unfortunately the original post was awhile ago and I don't have an example handy at the moment. At any rate, that's why I said "unnecessary confusion", since there isn't an inherent need to be able to represent a negative number; and if there's inconsistency in how bytes are translated to integers, that's likely to increase the struggles of designers (especially those without a programming background) who would like to try their hand at Lua events.This video explains twos complement, which is probably how lua is interpreting that byte.
I think I've figured it out. If the tile special is being animated, it changes from the basic terrain type to something else.
EDIT: P.S. I should have read Knighttime's post more closely, or just realized that he is clever enough to have checked special squares first anyway.
Too bad almost no scenario designers use animated specials! I always have them disabled. Thanks for this information though, and congratulations for figuring this out. How does this work -- do animated specials use the 0001 value in the second byte? or 0010?
But it does really seem odd to me -- and rather frustrating -- if non-animated specials (which are the norm in most scenarios) are undetectable by Lua!
If that is desired functionality, we'll just have to reverse engineer the pattern of special resource distribution. The formula probably isn't that complicated.