LUA script to check first time a tech is researched

Contribute, display and discuss rulesets and modpacks for use in Freeciv here.
bard
Veteran
Posts: 110
Joined: Fri Jun 14, 2013 2:00 pm

LUA script to check first time a tech is researched

Postby bard » Thu May 11, 2017 3:48 pm

I'm trying to write a script to place strategic resources in the map when certain tech is researched the first time.
Something like this:

Code: Select all

function place_extra_resources(tech, player, reason)
  local tech_name = tech:rule_name()
  -- and first time researched

  if tech_name == "Gunpowder" then
    for place in whole_map_iterate() do
      local terr = place.terrain
      local tname = terr:rule_name()

      if random(1, 100) <= 1 then
        if tname == "Desert" then
          place:create_extra("Niter")
        elseif tname == "Plains" then
          place:create_extra("Niter")
        end
      end
    end
end

signal.connect("tech_researched", "place_extra_resources")

Can someone help me to modify this script to verify if it is the first time that the tech is researched?

I have seen a new feature for v3.0 (appearance_reqs) that allows what I want without need of any script, but I'm trying to do the script for v2.6
Last edited by bard on Thu May 11, 2017 7:51 pm, edited 1 time in total.

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

Re: LUA script to check first time a tech is researched

Postby cazfi » Thu May 11, 2017 4:59 pm

Set a (boolean) variable when the script runs, and check against that variable to see if it runs first time or has it already run previously. The way it is now, more and more extras get placed when ever someone research the tech (new player, or someone who temporarily lost the tech to tech upkeep)

bard
Veteran
Posts: 110
Joined: Fri Jun 14, 2013 2:00 pm

Re: LUA script to check first time a tech is researched

Postby bard » Thu May 11, 2017 7:50 pm

cazfi wrote:Set a (boolean) variable when the script runs, and check against that variable to see if it runs first time or has it already run previously.

Thanks, I thought script variables would be reseted when you save and reload a game. If the state of script variables is saved, they are more powerful than I thought.

I have found another trouble, my script may place strategic resources on tiles where there is already other resource, and it seems the required condition is not available until v3.0: (Tile):has_extra(Resource).

Another question, I have noticed that huts are never placed on Glaciers (v2.6). If I remove the restrictions of hut extra, then it can appear in oceans, but never on glaciers. Is it some hardcoded limitation, or it is possible for a ruleset to workaround it?

User avatar
dunnoob
Elite
Posts: 326
Joined: Mon Dec 23, 2013 3:13 am
Location: Hamburg
Contact:

Re: LUA script to check first time a tech is researched

Postby dunnoob » Sat Oct 21, 2017 7:32 pm

bard wrote: I thought script variables would be reseted when you save and reload a game. If the state of script variables is saved, they are more powerful than I thought.
Caveat: Only simple strings, numbers and booleans are reset. Special Lua multi-line strings, arrays, tables, signals, etc. are not reset. The multiplayer ruleset has a tricky workaround encoding who researched what when first (or similar) as simple string.

I have noticed that huts are never placed on Glaciers (v2.6).
Wild guess (untested), that could be a side-effect of the d**ned NOCITIES flag. Nothing is wrong with the flag for 0-0-0 terrains like glaciers, the city centre bonus is supposed to upgrade at most one 0, never two. Arguably glaciers with resources are not 0-0-0 and should not have the flag. But I'm not sure where to draw the line, what about glaciers with good adjacent tiles, e.g., whales?

Updated later: Cf. Bug #660056, my wild guess was wrong.