LUA script to check first time a tech is researched

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

LUA script to check first time a tech is researched

Post by bard »

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: 3077
Joined: Tue Jan 29, 2013 6:54 pm

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

Post by cazfi »

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: 121
Joined: Fri Jun 14, 2013 2:00 pm

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

Post by bard »

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: 327
Joined: Mon Dec 23, 2013 3:13 am
Location: Hamburg
Contact:

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

Post by dunnoob »

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.
Post Reply