Lua Script

Contribute, display and discuss rulesets and modpacks for use in Freeciv here.
gm1530
Posts: 27
Joined: Fri Mar 29, 2013 11:48 am

Lua Script

Postby gm1530 » Thu May 09, 2013 11:31 am

Hi,

The image is a piece of my scenario (attached)

Image

I would like to create two scripts: one for the red line and another for black line. The idea behind black line is a sort of mission. I've tried to create a script, but it doesn't run. The summary: an UNIT (i.e. Phalanx) move from POINT A to POINT B, when UNIT arrive to POINT B, the PLAYER or NATION gets 2000 gold and a line in chat with ''Mission Complete''. This script is ''high-potential'', especially for tutorial.sav.

Code: Select all

function has_unit_type_name(unit, utype_name)
  return (unit.utype.id == find.unit_type(utype_name).id)
end

function unit_moved_callback(unit, src_tile, dst_tile)
     local beta = find.tile(178,192)
     local owner = find.player(0)
     local type = find.unit_type('Phalanx')
    if unit.owner:is_human() then
         if unit_type == type then
           if dst_tile.tile == beta then
          change_gold(owner, 2000)
             notify.player(unit.owner, [[Mission complete!]])
           end
         end
     end
end

signal.connect('unit_moved', 'unit_moved_callback')


The second script, red line... eh, I haven't any idea about the structure. The idea is: fixate an easy target (enemy) like conquer a city. In example: I play with French, I would like to fixate as target (for English) to conquer my city (Wimereux) or move their troops close a specific coordinate (on the other side of English Channel).

Details greatbritainv2.5.sav.gz:
  • 6 nations (English, Scottish, Welsh, Irish, French and Manx)
  • map size 350x350 (122'500 tiles)
  • 58 cities (English)
  • 20 cities (Scottish)
  • 15 cities (Manx)
  • 13 cities (Irish)
  • 11 cities (French)
  • 07 cities (Welsh)
  • 124 cities total
Last edited by gm1530 on Thu May 09, 2013 7:27 pm, edited 1 time in total.

Unknown
Posts: 20
Joined: Sat Feb 23, 2013 5:18 pm

Re: Lua Script

Postby Unknown » Thu May 09, 2013 3:40 pm

if unit.owner:is_human() then
if unit_type == type then
if dst_tile.tile == beta then


Does this work?

if unit.owner:is_human() then
if has_unit_type_name(unit,'Phalanx') then
if dst_tile.id == beta.id then

gm1530
Posts: 27
Joined: Fri Mar 29, 2013 11:48 am

Re: Lua Script

Postby gm1530 » Thu May 09, 2013 4:10 pm

Does this work?
if unit.owner:is_human() then
if has_unit_type_name(unit,'Phalanx') then
if dst_tile.id == beta.id then

Yes! Thank you very much :)

gm1530
Posts: 27
Joined: Fri Mar 29, 2013 11:48 am

Re: Lua Script

Postby gm1530 » Wed Jun 19, 2013 7:55 am

I would like to reward only the units from a specific city. Now the script rewards every unit. I have to insert ''src_tile'' parameter in the script to define the city, but I don't know how to do.

Code: Select all

function unit_moved_callback(unit, src_tile, dst_tile)
     local origin = find.tile(321,336) --Wissant (France) ORIGIN CITY
     local beta = find.tile(326,333) --Calais (France) DESTINATION CITY
     local owner = find.player(0)
   if unit.owner:is_human() then
      if has_unit_type_name(unit,'Warriors') then
         if src_tile.id == origin.id and dst_tile.id == beta.id then --INSERT SCR_TILE PARAMETER
            change_gold(owner, 500)
            notify.player(unit.owner, [[Mission complete!]])
         end
      end
   end
end

It doesn't run.

Unknown
Posts: 20
Joined: Sat Feb 23, 2013 5:18 pm

Re: Lua Script

Postby Unknown » Thu Jun 20, 2013 4:20 am

Did you try unit:get_homecity() ?

gm1530
Posts: 27
Joined: Fri Mar 29, 2013 11:48 am

Re: Lua Script

Postby gm1530 » Thu Jul 18, 2013 9:33 pm

Unknown wrote:Did you try unit:get_homecity() ?

Yes, it works!

There is a lua function: edit.create_road(tile, name). Now, I would like to create a road from POINT A to POINT B. My attempt:

Code: Select all

function distance_between(x1, y1, x2, y2)
    local hdist = math.abs(x1 - x2)
    local vdist = math.abs(y1 - y2)
    return math.max(hdist, vdist + math.floor(hdist / 2))
end

local ATTEMPT = print(distance_between(10, 20, 10, 70):sq_distance
edit.create_road(ATTEMPT, 'Road')


Under ''Type Tile'' I've found: (Tile):sq_distance, but I don't know how to do. Maybe it's the correct parameter.

gm1530
Posts: 27
Joined: Fri Mar 29, 2013 11:48 am

Re: Lua Script

Postby gm1530 » Fri Jul 19, 2013 9:52 pm

Is there any possibility to load an image with lua script in notify module? I've searched on the code about ''message'' parameter in notify and I've found: const char *message. This constant is connected (I think) to send_chat. Now.. Is there any possibility to add at ''send_chat'' a markup language for bold, italic, underline, image, ecc.? I've read http://gna.org/patch/?3361, but I don't know if helptexts is connected to ''message'' in notify.

cazfi
Elite
Posts: 1782
Joined: Tue Jan 29, 2013 6:54 pm

Re: Lua Script

Postby cazfi » Sun Jul 21, 2013 8:14 pm

I'm not entirely sure if "featured text" is passed that way, but have you tested that text with markup doesn't work there already? There's no support for image but only text modifications such as bold, italic, underline, color, and making links to units, cities, or tiles - see "Chat tools" in gtk-clients (opened by clicking button next to chat-line)

gm1530
Posts: 27
Joined: Fri Mar 29, 2013 11:48 am

Re: Lua Script

Postby gm1530 » Sun Jul 21, 2013 9:57 pm

cazfi wrote:There's no support for image but only text modifications such as bold, italic, underline, color, and making links to units, cities, or tiles - see "Chat tools" in gtk-clients (opened by clicking button next to chat-line)

Yes, there are text modifications in Chat tools, but these (tags ie. b, u, etc.) don't work in Lua script (notify module).
Script:

Code: Select all

function turn_callback(turn, year)
  if turn == 0 then
    notify.event(nil, nil, E.SCRIPT,
_("[b]Welcome to Freeciv[/b].  You lead a civilization.  Your\n\
task is [i]to conquer the world[/i]!  You should start by\n\
exploring the [u]land around you with your explorer[/u],\n\
and using your settlers to find a good place to build\n\
a city. Use the number pad to move units around."))
  end
end
signal.connect('turn_started', 'turn_callback')

Output:
notify.png

cazfi
Elite
Posts: 1782
Joined: Tue Jan 29, 2013 6:54 pm

Re: Lua Script

Postby cazfi » Mon Jul 22, 2013 5:53 am

gm1530 wrote:
cazfi wrote:Yes, there are text modifications in Chat tools, but these (tags ie. b, u, etc.) don't work in Lua script (notify module).

Actually, it's the popup where they don't work. They're parsed, as you can see from the fact that tags are not displayed literally, but only the chat window has ability to show them.