Where best to search for "HowTo" (for client Lua scripting)?

Various topics about the game, the website, or anything else Freeciv related that doesn't fit elsewhere.
ILoveLilyAllen
Posts: 29
Joined: Wed Jul 30, 2014 5:50 pm

Where best to search for "HowTo" (for client Lua scripting)?

Postby ILoveLilyAllen » Thu Jul 31, 2014 2:03 pm

Greetings All

For a "toe in the water" I have an idea for creating some lua scripts for example to tell all my engineers to build a railway if they are on a tile which only has a road.

Despite the valiant efforts and achievements of the wiki authors and knowledgeable forum contributors, being twice the age or more compared to the quick thinkers and fast learners I really need an idiot's or bluffer's or even slow-person's guide to where best to search for the relevant information on how to do this. I realise that thoroughly scouring this website and spending weeks reading up on lua would obviously be advantageous, but of course am looking for an easier way.

I have done a bit of lua programming in the past on other games so do not need a beginners tutorial. On the other hand I have read through a bit of the lua guide in the wiki and it seems to be a bit too advanced/obscure/non-specific for me. The example are of course good and instructive but don't come close to what I want to achieve.

Furthermore I have searched for instructions about how to launch a lua script and cannot find anything - it is almost certainly out there somewhere but maybe hidden? There is a "Client\Lua Script" menu option on the "Edit" menu which says "This is the Client Lua console" and has an option to load a script, but there is no "Help" there either!

Apologies for the newbie questions.

Zacheus
Posts: 4
Joined: Fri Aug 01, 2014 8:58 am

Re: Where best to search for "HowTo"?

Postby Zacheus » Fri Aug 01, 2014 10:00 am

Hi,

Looks like I am in a very similar situation(incl. your age). So far discovered how to go through units(Cities, etc) and pick up some info:
This code can be copied into the Client Lua console and it works(at least on my computer).

Code: Select all

a=find.player(0)
ir=a:units_iterate()
while true do
      local unit = ir() -- calls the iterator
      if unit == nil then break end
      log.normal(unit.utype:name_translation())
      log.normal(unit.id)
      tl= unit.tile
      log.normal(tl.id)   
      log.normal(tl.x)
      log.normal(tl.y)
      log.normal(tl.nat_x)
      log.normal(tl.nat_y)
      log.normal(tl:sq_distance(find.tile(1604)))
end

Zacheus
Posts: 4
Joined: Fri Aug 01, 2014 8:58 am

Re: Where best to search for "HowTo"?

Postby Zacheus » Fri Aug 01, 2014 10:13 am

Oops! Pleas remove the line:

log.normal(tl:sq_distance(find.tile(1604)))

The hardcoded number(1604) will not probably work generally.

ILoveLilyAllen
Posts: 29
Joined: Wed Jul 30, 2014 5:50 pm

Re: Where best to search for "HowTo"?

Postby ILoveLilyAllen » Fri Aug 01, 2014 10:28 am

Marvellous, thank you so much.

Well that is simple: just put the code into the Lua console - no wonder there's no "HowTo"!

Just a small supplementary question if I may: how do you actually run the code please?

Zacheus
Posts: 4
Joined: Fri Aug 01, 2014 8:58 am

Re: Where best to search for "HowTo"?

Postby Zacheus » Fri Aug 01, 2014 2:38 pm

Try restarting of the application and starting a new game.
Then copy this command:

Code: Select all

log.normal("Hello world")

into the command line of the console and presss Enter.
You must get the output "Hello world".

Then copy this sequence

Code: Select all

a=find.player(0)
ir=a:units_iterate()
while true do
      local unit = ir() -- calls the iterator
      if unit == nil then break end
      log.normal(unit.utype:name_translation())
      log.normal(unit.id)
end

and press Enter
You should get output like this:
Explorer
109
Workers
108
Workers
107
Settlers
106
Settlers
101

User avatar
JTN
Elite
Posts: 462
Joined: Wed Jan 30, 2013 12:15 am

Re: Where best to search for "HowTo"?

Postby JTN » Fri Aug 01, 2014 3:25 pm

ILoveLilyAllen wrote:I have an idea for creating some lua scripts for example to tell all my engineers to build a railway if they are on a tile which only has a road.

ILoveLilyAllen wrote:There is a "Client\Lua Script" menu option on the "Edit" menu which says "This is the Client Lua console" and has an option to load a script, but there is no "Help" there either!

One reason there isn't much help for the client Lua console is that it's currently quite limited; you won't be able to use it to tell your engineers what to do, I'm afraid.

The 2.4 release notes sum it up:
(Gtk) The client now has a Lua scripting console. GNAPATCH#2515
  • This is currently something of a proof of concept rather than a finished feature, as a comprehensive API suitable for the client-side has not been provided. Notably, there are not yet any signals from which client-side scripts can be triggered.
  • However, client scripts do have access to the client's view of most of the same game data that server-side ruleset scripts do (map, units, cities etc); see the Lua reference manual. This should be sufficient for some useful work. For example, it should in principle be possible to implement an on-demand optimal trade route calculator with these facilities (although we haven't tried).

ILoveLilyAllen
Posts: 29
Joined: Wed Jul 30, 2014 5:50 pm

Re: Where best to search for "HowTo"?

Postby ILoveLilyAllen » Fri Aug 01, 2014 3:37 pm

That's wonderful advice again thank you :) That tells me what to do if I start a new game. I would like to run a new script on a game which has already been created. if I am half-way through a turn and want to run a script at that point how do I do it?

And JTN has been extremely helpful as well, thank you very much too :) But again there is missing information: For example, <quote>it should in principle be possible to implement an on-demand optimal trade route calculator with these facilities (although we haven't tried)</quote> - on-demand to me means pressing a button, but which button? I couldn't find it in http://www.freeciv.org/wiki/Lua_reference_manual

Once again thank you for your help though :)

Zacheus
Posts: 4
Joined: Fri Aug 01, 2014 8:58 am

Re: Where best to search for "HowTo"?

Postby Zacheus » Fri Aug 01, 2014 4:48 pm

ILoveLilyAllen, the scripts I sent you normally work on the console also in the middle of game. I just wanted to start from the most simple example.

I fully agree that it would be really helpful having possibility to manage e.g. engineers by Lua scripts. It is quite hard work to manage all the engineers manually and the function AutoSetler does not behave well sometimes. The same situation for caravans. It is exhausting, if you have to manually establish all traderoutes for a large civilisation. So I am looking forward that it will be possible sometime.

User avatar
JTN
Elite
Posts: 462
Joined: Wed Jan 30, 2013 12:15 am

Re: Where best to search for "HowTo"?

Postby JTN » Fri Aug 01, 2014 5:19 pm

ILoveLilyAllen wrote:But again there is missing information: For example, <quote>it should in principle be possible to implement an on-demand optimal trade route calculator with these facilities (although we haven't tried)</quote> - on-demand to me means pressing a button, but which button?

There's no way to wire it up to a GUI button -- what I had in mind by "on-demand" was that you'd go to the console and type 'find_next_trade_route()' or something. A bit primitive, I know.

Basically I think the client Lua console fell out of some other work 'for free', and no thought or effort has yet gone into a useful API for the client side. Lua scripting in Freeciv at the moment is really for server-side ruleset stuff (although there are still many holes in the API even for that purpose), not automation to serve players. The trade route calculator was an idea I had for a way in which the console might actually be useful once it has been implemented, rather than the original purpose that the Lua console was designed around.

ILoveLilyAllen
Posts: 29
Joined: Wed Jul 30, 2014 5:50 pm

Re: Where best to search for "HowTo"?

Postby ILoveLilyAllen » Fri Aug 01, 2014 5:43 pm

Zacheus wrote:ILoveLilyAllen, the scripts I sent you normally work on the console also in the middle of game. I just wanted to start from the most simple example.


JTN wrote:There's no way to wire it up to a GUI button -- what I had in mind by "on-demand" was that you'd go to the console and type 'find_next_trade_route()' or something. A bit primitive, I know.


Many, many thanks again. That's what I have tried - and that's why I keep asking!

I am currently running a game using "This is Freeciv version 2.5.0-beta1 (beta version), gui-gtk-2.0 client." I select the menu "Edit" and the item "Client Lua Script". This launches a new window which says "This is the Client Lua Console.". In that window I cannot type anything!

If I load an old game using the Freeciv-2.3.0-RC1-gtk2 client there is no Lua Script option - I have not downloaded any 2.4 versions and maybe that is what you helpful people have where the Lua scripts work.

Anyway whatever the result of this it is wonderful to have so much help from this community. Thank you so much :)

<update>
Some modicum of success: if I create a text file containing:

log.normal("Hello world")

then press the Button "Load Lua Script" and select that file, I get the following in the console:

(file)> C:\Program Files (x86)\Freeciv-2.5.0-beta1-gtk2\lua\HelloWorld.lua
Hello world

So thanks to you I hopefully have a way to do this *crosses fingers*