How to get and set hex co-ordinates of units using Lua?

MoeTheCamel

Chieftain
Joined
Sep 28, 2017
Messages
11
I've gotten into Lua modding recently and so far I've gotten to grips with getting playerIDs and unitIDs and converting them into variables that I can use with the inbuilt functions. I can add dummy buildings and policies pretty easily now but I'm unable to figure out how to check which plot a unit is on, or how to convert a plotID (something like 5E7D7290) into an x,y coordinate which I can use with Unit.SetXY().

I'm hoping if I can figure out those then I can make a ship which will essentially pull a unit towards it after doing a ranged attack, kind of the opposite of what the Winged Hussar does. My plan is to get the plot coordinates of the two units involved in the combat and then use some calculation to find the plot in between them, check whether it's water then if it is move the attacked unit into it (the way the hex grid works means that sometimes there will be two possible plots which are between the units so i'd just pick one at random).

I'd also like to have two different units which give combat bonuses to any adjacent units of the other type, basically meaning that you would need an alternating wall of these units to get the most out of them. Again, I feel like knowing the plot coordinate of one of the units and checking the surrounding plots is a decent way to approach this but if there is an easier way to check adjacent tiles then I'd love to know.
 
Haven't tested it, but the wiki suggests that Map.GetPlotByIndex() is what you're looking for.

Followed by plot:GetX() / plot:GetY() for the coordinates on the grid.
 
Haven't tested it, but the wiki suggests that Map.GetPlotByIndex() is what you're looking for.
I can confirm that this works (as I've used it a ton myself as well)

[..]get the plot coordinates of the two units involved in the combat[..]
Detecting combat in civ5 is, well, limited as there's no (reliable) hook for it. You can abuse the PostCombatRandomPromotions, which grants a unit a (marker) promotion after entering combat. The problem with this however it that you won't know if the unit attacked, defended, and with which other unit it fought. Neither will you know something about the result of the battle. If you do decide to go this route, you could just 'pull in' a random enemy rather than a specific enemy because of the limitations.
Modded DLLs (VMC/the CP) do add a combat lua hook, though the downside of using these DLLs is that only one can be active at a single time, meaning that your mod would become incompatible with any mod that uses a different modded DLL.

EDIT: For an example on the PostCombatRandomPromotions, you can check out the code for the Whomp UU in my Whomp's Fortress mod. The unit receives extra combat strength for its first attack in a turn, but suffers a penalty after doing so. This bonus is refreshed the next turn.


I'd also like to have two different units which give combat bonuses to any adjacent units of the other type, basically meaning that you would need an alternating wall of these units to get the most out of them. Again, I feel like knowing the plot coordinate of one of the units and checking the surrounding plots is a decent way to approach this but if there is an easier way to check adjacent tiles then I'd love to know.
GameEvents.UnitSetXY(...) is the perfect Lua hook for this. Just give (or remove) a promotion that grants combat strength depending on where a unit moved to!
 
Last edited:
Thanks for the quick replies! I've been testing out Map.GetPlotByIndex() and plot:GetX/Y() and it's giving me the coordinates just like I wanted, making my life so much easier. I've also been using them with the Events:RunCombatSim() hook which so far has been working for me allowing me to get unitIDs of both the attacking and defending units, so I think I'll be able to pull off what I'm trying to do. I like the idea for giving temporary combat promotions based on when the units are moved next to each other so I'll definitely try out GameEvents.UnitSetXY(); sounds like a good plan to me!
 
Ok thanks for letting me know. I think at this point I'm fine with just letting it only work with humans; I doubt the AI will have much of an understanding of how to use the pull well anyway and I'd rather keep the ability to easily get the plots of the attacking and defending unit, although I'll definitely bear the limitation in mind if I ever want to dabble in combat again. Another thing is I've also noticed RunCombatSim doesn't seem to do anything when quick combat is on either which is kind of annoying but not too big of a problem.
 
[..]Another thing is I've also noticed RunCombatSim doesn't seem to do anything when quick combat is on either which is kind of annoying but not too big of a problem.
That's true, and neither will it fire if you're playing in Strategic View
 
Top Bottom