Ruleset Scripting

Contribute, display and discuss rulesets and modpacks for use in Freeciv here.
drakeraider
Posts: 16
Joined: Sat Feb 03, 2018 11:49 pm

Ruleset Scripting

Postby drakeraider » Mon Nov 12, 2018 7:41 pm

Three questions about the Lua script, as I'm having trouble with the primer.
One:
Would it be possible to generate huts or Barbarian units dynamically during the game?
- Context: I want areas outside borders that players can't see periodically spawn individual units or add Hut extras. I can find the functions for this but I'm not sure the situations where scripts are run within freeciv or how I would use this. An additional effect of this if possible is I would like certain government types to have either the hut or barbarian effect to happen more often.

Two:
Could I add resources to the map throughout the game at certain landmarks?
- Context: I found out through testing that the Visibility_Req property can be read by the engine but has no effect in 2.6. Understandable. But given the indefinite length of time before 3.0, and the above functions accessible by the Lua, could I replace this by having resources be added to tiles when a player discovers a certain technology? It's not a perfect solution, but the map looks super cluttered in my ruleset with Uranium and Rubber appearing in the early game.

Three:
Least important, but how do I add more random events to the game?
- Context: I played a ruleset that did that similarly to Civ4's behaviour, and loved those features. I'd like to learn how to create my own random events and define the conditions to have them occur to a player when the turn starts. I think it was Corbeu's but I'm not sure.

User avatar
Corbeau
Elite
Posts: 840
Joined: Mon Jan 13, 2014 11:13 pm

Re: Ruleset Scripting

Postby Corbeau » Mon Nov 12, 2018 8:46 pm

May I add a question? I have no clue whatsoever how lua works, but what I like about it is that it's ruleset-specific and can be changed.

So, the question: is it possible to edit it in such a way to permit dismantling a (small) wonder, either selling it or simply removing it, and making it possible to build it again later?

Ignatus
Veteran
Posts: 62
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Re: Ruleset Scripting

Postby Ignatus » Wed Nov 14, 2018 8:40 pm

The main way to communicate with server-side Lua (the client-side one can do few more than you can do manually) is the callbacks, which are global Lua functions called by the binary code when it processes some specific events; they are declared by putting a

Code: Select all

signal.connect(signal_name, callback_name);
line into your script.lua, possible events are listed on Wikia.
drakeraider wrote: Would it be possible to generate huts or Barbarian units dynamically during the game?

Use turn_started callback and edit.create_unit (likely, you should check if the barbarian player already exist) and
edit.create_extra functions; just I don't know how to check tile visibility another way than checking vision-related effects for everything.
Could I add resources to the map throughout the game at certain landmarks?

Your case is helped by the tech_researched callback; consult the label placer in civ2civ3 and resource placer in C map generator to see what to do. Well, players that are living in isolated undevelopedness will suddenly see Uranium in their lands, but that is not really a big problem.

Least important, but how do I add more random events to the game?

See the callbacks your version has, and put there some functions with random inside (hut_enter in default.lua is an example). There is currently no way to inform AIs about these events, but without them (or giving them a handicap) you can play.

Corbeau wrote:is it possible to edit it in such a way to permit dismantling a (small) wonder, either selling it or simply removing it, and making it possible to build it again later?

I can't find any functionality related to city improvements edit. Probably it should be requested on HRM.

drakeraider
Posts: 16
Joined: Sat Feb 03, 2018 11:49 pm

Re: Ruleset Scripting

Postby drakeraider » Fri Nov 16, 2018 10:19 pm

Okay. Thanks for the guidance, Ignatius.
Some other questions:

Could I have Cities scripted to rebel at high anger? Like the Civil War mechanic but without requiring the loss of a palace. I found the create.player and some other necessary functions, but how would I have the game detect and execute the trigger conditions?

And finally:
Can I use Lua variables in FreeCiv? I have a couple ruleset-specific counters to track and while I could figure out the code on that one, I'm curious if its possible.

EDIT:
Oh, hey! Before I forget, does the name detection function apply to player name or leader name? I have much ideas for AIs if I can detect leader.

Ignatus
Veteran
Posts: 62
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Re: Ruleset Scripting

Postby Ignatus » Mon Nov 19, 2018 6:38 pm

drakeraider wrote:Could I have Cities scripted to rebel at high anger? Like the Civil War mechanic but without requiring the loss of a palace. I found the create.player and some other necessary functions, but how would I have the game detect and execute the trigger conditions?

Currently, you have only is_happy() etc. boolean methods, no data of how deeply unhappy it is.

Can I use Lua variables in FreeCiv? I have a couple ruleset-specific counters to track and while I could figure out the code on that one, I'm curious if its possible.

Yes, you can use any variables, but note that if you consider restarting from a savegame (and you should) only those global variables will be saved that are handled by the function _freeciv_state_dump() from common/scriptcore/tolua_common_a.pkg. Currently, they are boolean, numeric (note some locale-related warning) and string scalars, and also the game-related userdata objects that have .id property, but not tables or functions. Any other Lua items should be coded by name in script.lua or parsed by parser.lua from luadata.txt (uses luadata.get_str("section.param") (3.1+); you can't write by Lua to any files).

does the name detection function apply to player name or leader name?

You mean (Player).name method? It's likely the player's connected name, the rulers' data are pitifully missing now.

drakeraider
Posts: 16
Joined: Sat Feb 03, 2018 11:49 pm

Re: Ruleset Scripting

Postby drakeraider » Sun Dec 02, 2018 4:28 pm

Alright. Last two questions for now. And one of these might be needing a workaround.
First, I need to have the game ask you dialogue questions at certain junctures. Like during startup or at certain events or actions. Like Civ4's Events? I was wondering if I can do that with Lua. Second, can scripts be used to toggle effects? I'm having trouble making a temporary player-wide effect and I'm trying to create a method, since buildings can only have a Player effect in Small Wonder genus.

Ignatus
Veteran
Posts: 62
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Re: Ruleset Scripting

Postby Ignatus » Mon Dec 03, 2018 7:47 pm

[quote=Ignatus]You mean (Player).name method? It's likely the player's connected name, the rulers' data are pitifully missing now.[/quote]
Sorry, I've sorted out better, the property is leader name but without title or gender.
drakeraider wrote:First, I need to have the game ask you dialogue questions at certain junctures. Like during startup or at certain events or actions. Like Civ4's Events? I was wondering if I can do that with Lua.

Nope. There should be some callback mechanism involved that is missing.
Second, can scripts be used to toggle effects? I'm having trouble making a temporary player-wide effect and I'm trying to create a method, since buildings can only have a Player effect in Small Wonder genus.

Also no. If your ruleset ignores culture, you can toggle things by changing culture levels with (Player):add_history(n) and bind effects to MinCulture intervals, but this is a kludge. For things like Civ3 "Golden Age", we should be able to e.g. give achievments, but we can't. This is also for AI better understanding the game (but I doubt if it helps).

drakeraider
Posts: 16
Joined: Sat Feb 03, 2018 11:49 pm

Re: Ruleset Scripting

Postby drakeraider » Fri Dec 07, 2018 5:07 am

Could I do it with the Civil War value instead of Culture? I'm hoping to replace the mechanic entirely in script anyway, so I may not need the variable. Would that be tracked across saves? Is it possible to keep the variable and still disable the effects of the default civil war mechanic?

Ignatus
Veteran
Posts: 62
Joined: Mon Nov 06, 2017 12:05 pm
Location: St.Petersburg, Russia
Contact:

Re: Ruleset Scripting

Postby Ignatus » Fri Dec 07, 2018 2:29 pm

There is no CivilWar variable, its probability is either the effect checked once a capital is conquered or the explicit parameter in the Lua method call.