This routine demonstrates a variety of Lua script commands and functions.
Each entry shows the command, the live result, and the data type where applicable. The "userdata" output type = tolua.
Limited to Freeciv 2.6.4 to 2.6.11 compatible stuff, because I am using Freeciv 2.6.4 and 2.6.11.
The top part of the script is the "echo" output routine, the rest is the list of commands and their output.
Code: Select all
function echo( return_type, command, output, note )
if command == nil and output == nil then notify.event( nil, nil, E.CHAT_MSG, "" ) return end
if output == nil then output = "" end
if command == nil then command = "" end
-- This output is to the Chat pane only
notify.event( nil, nil, E.CHAT_MSG, return_type .. "\t" .. command .. " = " .. tostring( output ) )
if note ~= "" then
notify.event( nil, nil, E.CHAT_MSG, "\t\t" .. note )
end
-- A blank line after the output
notify.event( nil, nil, E.CHAT_MSG, "")
end
function s_command_examples( turn, year )
if game.turn() > 1 then return end
echo()
local output, note
local return_type = ""
local command = ""
local Player = find.player( 0 )
local Nation_type = find.nation_type( Player.nation.id )
local Government = find.government( 0 )
local Tech_type = find.tech_type( "Bridge Building" )
local Terrain = find.terrain( "Hills" )
local Unit_type = find.unit_type( "Phalanx" )
local Building_type = find.building_type( "City Walls" )
local Unit
for unit in Player:units_iterate() do
Unit = unit
break
end
note = "[b]Examples of Freeciv 2.6.4 Lua commands and functions[/b]"
notify.event( nil, nil, E.CHAT_MSG, "\t\t" .. note )
notify.event( nil, nil, E.CHAT_MSG, "" )
note = "Info source: https://freeciv.fandom.com/wiki/Lua_reference_manual#Tech_Type"
notify.event( nil, nil, E.CHAT_MSG, "\t\t" .. note )
notify.event( nil, nil, E.CHAT_MSG, "" )
note = "Variable names are in [ c fg \"#000099\"]blue[/c]"
notify.event( nil, nil, E.CHAT_MSG, "\t\t" .. note )
notify.event( nil, nil, E.CHAT_MSG, "" )
notify.event( nil, nil, E.CHAT_MSG, "" )
note = "[b]Output Command Result[/b]"
notify.event( nil, nil, E.CHAT_MSG, note )
notify.event( nil, nil, E.CHAT_MSG, "" )
output = fc_version() command = "fc_version()" return_type = type( output )
note = ""
echo( return_type, command, output, note )
output = _VERSION command = '[ c fg \"#000099\"]_VERSION[/c] is a variable which holds the Lua version' return_type = type( output )
note = ""
echo( return_type, command, output, note )
output = turn command = "[ c fg \"#000099\"]turn[/c]" return_type = type( output )
note = ""
echo( return_type, command, output, note )
output = year command = "[ c fg \"#000099\"]year[/c]" return_type = type( output )
note = ""
echo( return_type, command, output, note )
output = game.turn() command = "game.turn()" return_type = type( output )
note = "Deprecated as of Freeciv 3.0. Use game.current_turn() instead."
echo( return_type, command, output, note )
output = math.floor( game.turn() ) command = "math.floor( game.turn() )" return_type = type( output )
note = ""
echo( return_type, command, output, note )
output = find.player( 0 ) command = "find.player( 0 )" return_type = type( output )
note = "Like \"player #0 AI*1\" before gamestart, after it is like \"Player #0 Klef\""
echo( return_type, command, output, note )
output = Player.id command = "[ c fg \"#000099\"](Player)[/c].id" return_type = type( output )
note = ""
echo( return_type, command, output, note )
output = Player.name command = "[ c fg \"#000099\"](Player)[/c].name" return_type = type ( output )
note = "Like \"AI*1\" before gamestart, after it is like \"Klef\""
echo( return_type, command, output, note )
output = Player.ai_controlled command = "[ c fg \"#000099\"](Player)[/c].ai_controlled" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player.is_alive command = "[ c fg \"#000099\"](Player)[/c].is_alive" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:is_human() command = "[ c fg \"#000099\"](Player)[/c]:is_human()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:num_cities() command = "[ c fg \"#000099\"](Player)[/c]:num_cities()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:num_units() command = "[ c fg \"#000099\"](Player)[/c]:num_units()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:gold() command = "[ c fg \"#000099\"](Player)[/c]:gold()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:exists() command = "[ c fg \"#000099\"](Player)[/c]:exists()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.tech_type( "Bridge Building" ) command = "find.tech_type( \"Bridge Building\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.tech_type( 8 ) command = "find.tech_type( 7 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Tech_type.id command = "[ c fg \"#000099\"](Tech_type)[/c].id" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Tech_type:rule_name() command = "[ c fg \"#000099\"](Tech_type)[/c]:rule_name()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Tech_type:name_translation() command = "[ c fg \"#000099\"](Tech_type)[/c]:name_translation()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:knows_tech( Tech_type ) command = "[ c fg \"#000099\"](Player)[/c]:knows_tech( [ c fg \"#000099\"](Tech_type)[/c] )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:units_iterate() command = "[ c fg \"#000099\"](Player)[/c]:units_iterate()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:cities_iterate() command = "[ c fg \"#000099\"](Player)[/c]:cities_iterate()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player:shares_research( (Player) ) command = "[ c fg \"#000099\"](Player)[/c]:shares_research( [ c fg \"#000099\"](Player)[/c] )" return_type = type ( output )
note = "I like that I share with myself. It gives me a warm feeling."
echo( return_type, command, output, note )
output = Player:culture() command = "[ c fg \"#000099\"](Player)[/c]:culture()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player.nation command = "[ c fg \"#000099\"](Player)[/c].nation" return_type = type ( output )
note = "nil before gamestart, after like \"<Nation_Type #269 Langobardic>\""
echo( return_type, command, output, note )
output = Player.nation.id command = "[ c fg \"#000099\"](Player)[/c].nation.id" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.nation_type( Player.nation.id ) command = "find.nation_type( [ c fg \"#000099\"](Player)[/c].nation.id )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.nation_type( 555 ) command = "find.nation_type( 555 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.nation_type( "Barbarian" ) command = "find.nation_type( \"Barbarian\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.nation_type( "Pirate" ) command = "find.nation_type( \"Pirate\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player.nation:rule_name() command = "[ c fg \"#000099\"](Player)[/c].nation:rule_name()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player.nation:name_translation() command = "[ c fg \"#000099\"](Player)[/c].nation:name_translation()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Player.nation:plural_translation() command = "[ c fg \"#000099\"](Player)[/c].nation:plural_translation()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_min( "expansionist" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_min( \"expansionist\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_max( "expansionist" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_max( \"expansionist\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_default( "expansionist" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_default( \"expansionist\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_min( "trader" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_min( \"trader\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_max( "trader" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_max( \"trader\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_default( "trader" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_default( \"trader\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_min( "aggressive" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_min( \"aggressive\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_max( "aggressive" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_max( \"aggressive\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Nation_type:trait_default( "aggressive" ) command = "[ c fg \"#000099\"](Player)[/c].nation:trait_default( \"aggressive\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.government( "Monarchy" ) command = "find.government( \"Monarchy\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.government( 3 ) command = "find.government( 3 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Government:rule_name() command = "[ c fg \"#000099\"](Government)[/c]:rule_name()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Government:name_translation() command = "[ c fg \"#000099\"](Government)[/c]:name_translation()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = server.started() command = "server.started()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = server.civilization_score( Player ) command = "server.civilization_score( [ c fg \"#000099\"](Player)[/c] )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = server.setting.get( "startunits" ) command = "server.setting.get( \"startunits\" )" return_type = type ( output )
note = "Works for settings listed in a scenario .sav file, and any omitted there and thus from current settings on server."
echo( return_type, command, output, note )
Direction = direction.str2dir( "west" )
output = direction.str2dir( "north" ) command = "direction.str2dir( \"north\" )" return_type = type ( output )
note = "Malfunctional in Freeciv 2.6.4 -- returns a string rather than a tolua object. Direction stuff won't work at all, including 'Unit:facing()'."
echo( return_type, command, output, note )
output = direction.next_ccw( Direction ) command = "direction.next_ccw( [ c fg \"#000099\"](Direction)[/c] )" return_type = type ( output )
note = "Works in 2.6.11"
echo( return_type, command, output, note )
output = direction.next_cw( Direction ) command = "direction.next_cw( [ c fg \"#000099\"](Direction)[/c] )" return_type = type ( output )
note = "Works in 2.6.11"
echo( return_type, command, output, note )
output = direction.opposite( Direction ) command = "direction.opposite( [ c fg \"#000099\"](Direction)[/c] )" return_type = type ( output )
note = "Works in 2.6.11"
echo( return_type, command, output, note )
output = Unit:facing() command = "[ c fg \"#000099\"](Unit)[/c]:facing()" return_type = type ( output )
note = "Works in 2.6.11"
echo( return_type, command, output, note )
-- testing Directions
for unit in Player:units_iterate() do
Unit = unit
Direction = direction.str2dir( "east" )
output = edit.unit_turn( Unit, Direction ) command = "[ c fg \"#000099\"](Unit)[/c], [ c fg \"#000099\"](Direction)[/c] )" return_type = type ( output )
note = "Works in 2.6.11?"
echo( return_type, command, output, note )
end
output = find.building_type( 7 ) command = "find.building_type( 7 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.building_type( "City Walls" ) command = "find.building_type( \"City Walls\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type.build_cost command = "[ c fg \"#000099\"](Building_type)[/c].build_cost" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:build_shield_cost() command = "[ c fg \"#000099\"](Building_type)[/c]:build_shield_cost()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type.id command = "[ c fg \"#000099\"](Building_type)[/c].id" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:is_wonder() command = "[ c fg \"#000099\"](Building_type)[/c]:is_wonder()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:is_great_wonder() command = "[ c fg \"#000099\"](Building_type)[/c]:is_great_wonder()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:is_small_wonder() command = "[ c fg \"#000099\"](Building_type)[/c]:is_small_wonder()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:is_improvement() command = "[ c fg \"#000099\"](Building_type)[/c]:is_improvement()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:rule_name() command = "[ c fg \"#000099\"](Building_type)[/c]:rule_name()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Building_type:name_translation() command = "[ c fg \"#000099\"](Building_type)[/c]:name_translation()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.unit_type( 4 ) command = "find.unit_type( 4 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.unit_type( "Phalanx" ) command = "find.unit_type( \"Phalanx\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Unit_type.build_cost command = "[ c fg \"#000099\"](Unit_type)[/c].build_cost" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Unit_type:build_shield_cost() command = "[ c fg \"#000099\"](Unit_type)[/c]:build_shield_cost()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Unit_type:has_flag( "Paratroopers" ) command = "[ c fg \"#000099\"](Unit_type)[/c]:has_flag( \"Paratroopers\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Unit_type:has_role( "AttackFastStartUnit" ) command = "[ c fg \"#000099\"](Unit_type)[/c].has_role( \"AttackFastStartUnit\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.role_unit_type( "DefendGood", nil ) command = "find.role_unit_type( \"DefendGood\", nil )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.role_unit_type( "DefendOk", Player ) command = "find.role_unit_type( \"DefendOk\", [ c fg \"#000099\"](Player)[/c] )" return_type = type ( output )
note = "If nil is replaced with a player, then response unit type is limited to those which the player can build."
echo( return_type, command, output, note )
output = Unit_type:can_exist_at_tile( find.tile( 0, 1 ) ) command = "[ c fg \"#000099\"](Unit_type)[/c]:can_exist_at_tile( find.tile( 0, 1 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.tile( 10, 15 ) command = "find.tile( 10, 15 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.tile( 1810 ) command = "find.tile( 1810 )" return_type = type ( output )
note = "Tile ID is ( x = 10 ) + ( ( y = 15 ) * server.setting.get( \"xsize\" ) )"
echo( return_type, command, output, note )
Tech_type = find.tech_type( "Bronze Working" )
output = edit.give_tech( Player, Tech_type, 0, nil, "hut" ) command = "edit.give_tech( [ c fg \"#000099\"](Player)[/c], [ c fg \"#000099\"](Tech_type)[/c], 0, nil, \"hut\" )" return_type = type ( output )
note = "Fields are: player, tech_type or nil for random tech, cost, notify, reason"
echo( return_type, command, output, note )
output = find.terrain( 1 ) command = "find.terrain( 1 )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = find.terrain( "Grassland" ) command = "find.terrain( \"Grassland\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Terrain:rule_name() command = "[ c fg \"#000099\"](Terrain)[/c]:rule_name()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Terrain:name_translation() command = "[ c fg \"#000099\"](Terrain)[/c]:name_translation()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = Terrain:class_name() command = "[ c fg \"#000099\"](Terrain)[/c]:class_name()" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
output = effects.world_bonus( "Enable_Space" ) command = "effects.world_bonus( \"Enable_Space\" )" return_type = type ( output )
note = "Effects names are listed in effects.ruleset"
echo( return_type, command, output, note )
output = effects.player_bonus( Player, "Specialist_Output" ) command = "effects.player_bonus( Player, \"Specialist_Output\" )" return_type = type ( output )
note = ""
echo( return_type, command, output, note )
for index = 0, 100, 1 do
Signal = find.signal( index )
if Signal == nil then break end
output = find.signal( index ) command = "find.signal( " .. index .. " )" return_type = type ( output )
echo( return_type, command, output, note )
output = find.signal_callback( Signal, index ) command = "find.signal_callback( Signal, index )" return_type = type ( output )
if output ~= nil then echo( return_type, command, output, note ) end
end
notify.event( nil, nil, E.CHAT_MSG, "notify.event E.CHAT_MSG goes to the chat pane only, no pop-ups, no Messages pane, works on turn 0 too")
notify.all( "notify.all: ALWAYS pop-up message even on turn 0, does not show in Messages or Chat panes on turn 0, does show in Messages and Chat panes after turn 0" )
notify.event( nil, nil, E.SCRIPT, "notify.event E.SCRIPT: ALWAYS pop-up message, even on turn 0, but no message in Messages pane on turn 0, does show after turn 0" )
notify.event( nil, nil, E.REPORT, "notify.event E.REPORT: goes to chat on turn 0, to Messages thereafter, never a pop-up" )
log.error( "%s", "log.error: Appears on turn 0 and thereafter in Chat pane if msg is different each time, otherwise msg becomes only 'Last message repeated x times'', bright red text, also included in error log files." )
notify.event( nil, nil, E.CHAT_MSG, "" )
option_names={ "aifill", "airliftingstyle", "allowtake", "alltemperate", "animals", "aqueductloss", "autoattack", "autosaves", "autotoggle", "barbarians", "borders", "citymindist", "citynames", "civilwarsize", "compress", "compresstype", "conquercost", "contactturns", "demography", "diplbulbcost", "diplchance", "diplgoldcost", "diplomacy", "disasters", "dispersion", "ec_chat", "ec_info", "ec_max_size", "ec_turns", "endspaceship", "endturn", "first_timeout", "fixedlength", "flatpoles", "foggedborders", "fogofwar", "foodbox", "freecost", "fulltradesize", "gameseed", "generator", "globalwarming", "gold", "happyborders", "homecaughtunits", "huts", "kicktime", "killcitizen", "killstack", "killunhomed", "landmass", "mapseed", "mapsize", "maxconnectionsperhost", "maxplayers", "mgr_distance", "mgr_foodneeded", "mgr_nationchance", "mgr_turninterval", "mgr_worldchance", "migration", "minplayers", "nationset", "naturalcitynames", "nettimeout", "netwait", "notradesize", "nuclearwinter", "occupychance", "onsetbarbs", "persistentready", "phasemode", "pingtime", "pingtimeout", "plrcolormode", "rapturedelay", "razechance", "restrictinfra", "revealmap", "revolen", "revolentype", "savefrequency", "savename", "savepalace", "saveturns", "sciencebox", "scorefile", "scorelog", "scoreloglevel", "separatepoles", "shieldbox", "singlepole", "size", "specials", "startcity", "startpos", "startunits", "steepness", "team_pooled_research", "teamplacement", "techlevel", "techlossforgiveness", "techlossrestore", "techlost_donor", "techlost_recv", "techpenalty", "temperature", "tilesperplayer", "timeaddenemymove", "timeout", "tinyisles", "topology", "trademindist", "trading_city", "trading_gold", "trading_tech", "traitdistribution", "turnblock", "unitwaittime", "unreachableprotects", "victories", "wetness", "xsize", "ysize"}
log.error( "%s", "\nNumber of known option names = " .. #option_names .. "\n" )
for i = 1, #option_names, 1 do
-- if the server.setting.get output is a number then no quotes, else ""
local option_value = server.setting.get( option_names[ i ] )
if type( tonumber( option_value ) ) ~= "number" then option_value="\"" .. option_value .. "\"" end
log.error("%s","\"" .. option_names[ i ] .. "\"" .. "," .. option_value )
end
notify.event( nil, nil, E.CHAT_MSG, "" )
notify.event( nil, nil, E.CHAT_MSG, "Dump global vars and functions list" )
-- globals.lua
-- show all global variables
local seen={}
function dump( t, i )
seen[ t ] = true
local s={}
local n = 0
for k in pairs( t ) do
n = n + 1
s[ n ] = k
end
table.sort(s)
for k, v in ipairs( s ) do
log.error( "%s", tostring(i) .. tostring(v) )
v = t[ v ]
if type( v ) == "table" and not seen[ v ] then
dump( v, i .. "\t" )
end
end
end
dump( _G, " " )
notify.event( nil, nil, E.CHAT_MSG, "" )
end
signal.connect( "turn_started", "s_command_examples" )
EDIT: Updated to include routine showing all known server.setting.get() values (such as the [settings] section of a scenario .sav file.) Updated version also includes at the end a "dump globals" feature to list all Lua global vars and functions.
EDIT: Updated the routine to list known options and values.
This is for Lua script development and testing rather than a game feature.