Re: New features via Lua script
Posted: Sat Oct 28, 2023 8:45 pm
PersistentMapLabels Re-applies persistent map labels at start of each turn with some considerations. Incidentally, in the labels use \n for newline, \t for tab, max width per line appears to be about 25 characters, multiple lines work (example "\t1\n2\n\t3\n4\n\t\5" and so on.)
^ Before unhomed unit moves onto labelled tile, then on the labelled tile.
^ Then next turn, off the labelled tile again and label has been re-applied.
The routine is included in the "FairTest" scenario file attached to my latest post in this topic.
Attached file: "RandoMap" with self-contained PersistentMapLabels routine only (no other Lua scripts) for testing purposes.
^ To test, start the scenario game, go to the editor and change any existing tile label, then "turn done" and the persistent tile label will be re-applied.
Code: Select all
--##############################################################################
function Lua_PersistentMapLabels(turn, year)
-- If needed global vars not set, set them
if ( PersistentMapLabels == nil ) then PersistentMapLabels=true end
if ( debugLuaScripts == nil ) then debugLuaScripts=false end
-- If this routine is disabled, just return without doing anything else
if not ( PersistentMapLabels ) then return end
if ( debugLuaScripts ) then log.error("%s","PersistentMapLabels BEGIN") end
-- Map labels -MUST- be placed here for availability via Lua script in Fc 2.6.4
-- They may also be placed in the [map] section of the Scenario file, but it is not required.
-- \t for tab and \n for newline works in the label text.
labels={"\tHard\n Rock\n \tCandy\n Mountain", "Stinkin' SSpot\n\tP.U.!", " Do NOT \nGo There!"}
labelsX={20,30,43}
labelsY={10,15,29}
-- ToDo: Optional terrain type dependency per label (so that "Highest Peak" stops being applied if the Mountains tile is transformed to Hills.)
for i=1,#labels,1 do
tile=find.tile(labelsX[i],labelsY[i])
-- Check for unhomed units on tile
if ( (tile):num_units() > 0 ) then
for unit in ( (tile):units_iterate() ) do
-- If there is still any unhomed unit on the tile, then skip this tile
if ( (unit).homecity == 0 ) then goto continue end
end
end
-- If we are here, then there are no unhomed units on tile
-- Reapply the persistent map label
-- Skip re-applying the label if tile is a city
if not ( tile:city() ) then
edit.tile_set_label(tile,labels[i])
end
end
::continue::
if ( debugLuaScripts ) then log.error("%s","PersistentMapLabels END") end
end
signal.connect("turn_started", "Lua_PersistentMapLabels")
^ Before unhomed unit moves onto labelled tile, then on the labelled tile.
^ Then next turn, off the labelled tile again and label has been re-applied.
The routine is included in the "FairTest" scenario file attached to my latest post in this topic.
Attached file: "RandoMap" with self-contained PersistentMapLabels routine only (no other Lua scripts) for testing purposes.
^ To test, start the scenario game, go to the editor and change any existing tile label, then "turn done" and the persistent tile label will be re-applied.