More flexibility for Requirement Vectors

What would you like to see in Freeciv? Do you have a good idea what should be improved or how?
Post Reply
User avatar
Alien Valkyrie
Elite
Posts: 513
Joined: Sun Feb 10, 2013 10:21 pm
Location: Stuttgart, Germany

More flexibility for Requirement Vectors

Post by Alien Valkyrie »

As it stands now, requirement vectors can only have the general form of (A & B & C & !D & !E), which, in my opinion, lacks a bit of flexibility.
In my special case, I'm having the problem that I want Nuclear Plants to get a bonus if you've discovered Fusion Power, so that players can choose between Solar Plants (zero pollution) or Nuclear Plants (higher production). In order to do this, I'd have to disable all Hoover Dam and Solar Plant effects if a city has a Nuclear plant and you have Fusion Power ─ right now, I can only check if you have one or the other.

Now, I have an idea how this could be done. Instead of putting up a list for reqs, ruleset authors could put in a logical expression in brackets. Single requirements could be put in a different kind of brackets (depending on how this is implemented, it might be possible and practical or not). Also, OutputType "requirements" could be integrated into the effect type instead of being part of the requirement vector.
Let's look at an example, with curly brackets for single requirements and line breaks for readability:

Code: Select all

[effect_nuclear_plant]
type    = "Output_Bonus_Shield"
value   = 25
reqs    = ( {"Building", "Factory", "City"}
          & {"Building", "Nuclear Plant", "City"}
          & !{"Building", "Hoover Dam", "Player"}
          & !{"Building", "Solar Plant", "City"} )
Now, this is just a transcription for something that's already in the game, but with this syntax, more things could be possible:

Code: Select all

[effect_solar_plant]
type    = "Output_Bonus_Shield"
value   = 25
reqs    = ( {"Building", "Factory", "City"} & {"Building", "Solar Plant", "City"}
          & !( {"Building", "Nuclear Plant", "City"} & {"Tech", "Fusion Power", "Player"} ) )
This code would deactivate the Solar Plant effect if you have a Nuclear Plant with Fusion Power.
In order to keep all of this cleanly, it should probably reject any requirement where ands and ors are used in the same brackets.

From a programming point of view, I think this could be implemented not too difficultly by replacing all the single requirements {"type", "name", "range"} with the respective value (true or false) and then running the whole thing through an evaluate function.

Now, for the "survive" part of wonder-based effects, this could be added as an optional fourth part of a single requirement, e.g. {"Building", "Manhattan Project", "World", "survives"} or {"Building", "Apollo Program", "World", TRUE}; this would of course require the parser to be able to accept a fourth argument, which, based on my programming experiences, shouldn't be too difficult.

All in all, this would of course require some work, but open up a whole lot of new possibilities for ruleset authors.
Please note that I see this as an optional alternative, adding this wouldn't force anyone to change their existing requirement vectors.
~ AVL
User avatar
GriffonSpade
Elite
Posts: 578
Joined: Mon Apr 29, 2013 4:41 pm

Re: More flexibility for Requirement Vectors

Post by GriffonSpade »

Wouldn't another entry that subtracts the solar plant/hoover dam's effect work?

Code: Select all

[effect_nuclear_plant_obsolar]
type    = "Output_Bonus_Shield"
value   = -25
reqs    = ( "Building", "Factory", "City"
          "Building", "Nuclear Plant", "City"
          "Tech", "Fusion Power", "Player"
          "Building", "Solar Plant", "City" )

[effect_nuclear_plant_obsdam]
type    = "Output_Bonus_Shield"
value   = -25
reqs    = ( "Building", "Factory", "City"
          "Building", "Nuclear Plant", "City"
          "Tech", "Fusion Power", "Player"
          "Building", "Hoover Dam", "Player" )
One may need to have an nreq of the other building: If hoover dam already nullifies Solar Plant, then hoover dam should be added as an NREQ to obsoleting the solar plant (It's already obsolete)
User avatar
Alien Valkyrie
Elite
Posts: 513
Joined: Sun Feb 10, 2013 10:21 pm
Location: Stuttgart, Germany

Re: More flexibility for Requirement Vectors

Post by Alien Valkyrie »

Sure, that would be a workaround, the same way that I can deactivate the default Nuclear Plant effect if the player has Fusion Power and make the Fusion Power-based effect give the full bonus (default Nuclear Plant bonus and Nuclear Plant + Fusion Power bonus), instead of having the default effect activate again once the player has Fusion Power. Workarounds are nice and all, and while they fix the problem, they don't fix fix the problem.
Also, this way, when clicking on the production output in the city dialog, it'd say both "Solar Plant + Factory + Shields (+25%)" and "Solar Plant + Factory + Nuclear Plant + Fusion Power + Shields (-25%)". Also, since the Solar Plant would still have effects (that cancel each other out, but are active nonetheless), it would appear normally in the Production window, whereas other buildings that no longer have an effect are struck-out.
I personally am of the opinion that not too many workarounds should be necessary. If a possibility can be added directly, it should be.
~ AVL
cazfi
Elite
Posts: 3093
Joined: Tue Jan 29, 2013 6:54 pm

Re: More flexibility for Requirement Vectors

Post by cazfi »

Your summary is well written, but I'm afraid this won't be helping much. There's nothing we don't know already, even if this particular forum had not this exact wishlist item already. Simply put: the ruleset author facing interface has been written down, almost exactly like you do, numerous times, including my own implementation in the late 90's, but to make everything to work in freeciv engine side (well, things other than AI would be relatively doable) is not so easy.
We're constantly working on effects/requirements stuff. While the changes in S2_6 are mostly "under the hood", inivisible to user, they put us to much better position to inch towards this goal. We've had some discussions about the possibilities to have it in 3.0, but I'm not ready to promise anything yet.
User avatar
JTN
Elite
Posts: 473
Joined: Wed Jan 30, 2013 12:15 am

Re: More flexibility for Requirement Vectors

Post by JTN »

I'd go further than cazfi and say that the current level of expressiveness in requirements/effects lists is about the maximum we can support, because of the AI issue.

While it would be relatively easy to extend the system to allow evaluation of arbitrarily complex boolean expressions (or, worse, imperative Lua script), and that wouldn't make it any harder to answer the obvious question "what is the total output bonus in this situation?", bits of Freeciv need to be able to navigate the rulesets to answer different questions: "what is this building good for?" or "what building should I build next?":
  • The AI
  • Autogenerated help text describing the benefits of buildings, units, governments etc
  • UI features such as marking buildings as redundant
While we could give up on the last two of these, the Freeciv AI's ability to adequately understand arbitrary rulesets without help is one of its strengths and I'd be loath to lose it.

It is possible, albeit painful, to build arbitrarily complex boolean expressions in the current effects system. It would be possible in principle for Freeciv to expand out a boolean expression at ruleset load time to a set of terms expressible in the current system. However, this would make it easy for a ruleset author to unwittingly express a rule that expanded to tens or hundreds of terms; having an input format with such a mismatch from how the game engine works seems like a bad idea.

Also, it's often necessary to choose carefully from the many ways of constructing such expressions in order to maximise the ability of the AI/help/etc to understand what's going on.

I think this is one of those cases where you pick the formalism with the minimum expressive power which can do what you need it to. I think the current effects system is at about the right point in this tradeoff.

All that said, maintaining even the standard rulesets' mutually exclusive relationships between various electricity plants and pollution buildings is very error-prone and it's taken us ages to get it right. I'm open to ideas about how to make it easier without changing the underlying computational model.

It would clearly be easy to invent an "effects compiler", but I'm not in favour of the "wizard" type approach where you express your intent once, "compile" it, and thereafter have to maintain the derived form by hand. I'd prefer something where the file you give to Freeciv is the preferred form for modification.

It feels like there might be some possibility with cross-references between effects clauses -- "copy [effect_power_plant] but remove the term about the Power Plant and add this one about the Hydro Plant instead". But I haven't thought about it very hard.

Another idea I had to make maintaining effects in rulesets easier was an effects browser -- see mockup PDF. This would work by making it easier to see related effects all at once, which is hard if you're just editing a text file.
cazfi
Elite
Posts: 3093
Joined: Tue Jan 29, 2013 6:54 pm

Re: More flexibility for Requirement Vectors

Post by cazfi »

JTN wrote:the current level of expressiveness in requirements/effects lists is about the maximum we can support, because of the AI issue.
One of the ideas I played with when implementing AI module interface (in freeciv-2.4 development cycle) was the ability to strictly separate strictly multiplayer and single-player modes of freeciv. "Strictly multiplayer" would be a mode where there is no AI support at all (all players are and must be humans). We could have more ruleset flexibility in "strictly multiplayer" mode than in "AI supported" mode.
User avatar
Alien Valkyrie
Elite
Posts: 513
Joined: Sun Feb 10, 2013 10:21 pm
Location: Stuttgart, Germany

Re: More flexibility for Requirement Vectors

Post by Alien Valkyrie »

Well... I guess. Though it'd be nice if there was some way to link multiple nreqs with AND.

I decided to transcribe some of the effects into a JSON form to interpret with a small Python script I wrote, and you're right ─ the autogenerated help text does get messy when allowing complicated stuff.

I just got a new idea: Looking back at an older idea about custom effect types, I realized that could be used for more complex effect constructions, while still being able to handle the autogenerated help text stuff.
In out current example, having a Nuclear Plant in the City and knowing Fusion Power would give the City the custom "Fusion_Plant" effect, which would then deactivate the Solar Plant and Hoover Dam effects. For autogenerated help, the Fusion Power technology could have "* Grants a city a Fusion Power effect (with Nuclear Plant)" or something similar.
Also, additional help pages could be added for custom effects. They'd show a list of effects that require the specifc custom effect ("* Increases a city's production output by 25% (with Factory)", "* Increases a city's production output by 25% (with Mfg. Plant)" etc.).

Also, is there actually any difference between putting something in the nreqs field and putting it into the reqs field with negated = TRUE?
~ AVL
cazfi
Elite
Posts: 3093
Joined: Tue Jan 29, 2013 6:54 pm

Re: More flexibility for Requirement Vectors

Post by cazfi »

Caedo wrote:Also, is there actually any difference between putting something in the nreqs field and putting it into the reqs field with negated = TRUE?
In 2.5 and earlier, "negated = TRUE" is not really supported for effects (there's a lot of more or less subtle bugs where the "negated" part would be essentially ignored)
In 2.6 it works, with logic turned around as "present = FALSE" (should be less hard to read than requirement with 'TRUE' in their definition actually disabling what ever one is checking), and in fact nreqs are deprecated in favor of that way to express things (and in 3.0, obsoleted).
morphles
Posts: 3
Joined: Sat Apr 11, 2015 7:50 pm

Re: More flexibility for Requirement Vectors

Post by morphles »

Discussion here is a bit saddening. Because we get what is in actuallyti very limited system due to mostly AI issues. Help can be edited properly by ruleset author, even though auto generated texts are nice, I do not see them as all that essential. In fact I would thing that having some tools to "analyze ruleset" and then allow author to write Deocumentation wouldn't be too bad. For example having tool to list all places where say building is mentioned (all effects, all other buildigns and units that need it), would allow author to quite easily write very nice description for that building.

Though all in all I would be most for supporting lua callbacks in effecst and requirements of everything (effects, buildings, units, techs even). Though this makes autogeneration of docs and even tool described previously likely totally impossible even in theory. But that would give basically full freedom to ruleset authors. Maybe with enough effort most parsing for currently existing things could be droped resulting in much simpler C code, while leaving most to high level and more flexible lua, which is also much easier and faster to test. Well this might be going a bit far :). From couple of important things with regards to this, one is AI. I'm not sure how AI works, but couldn't quite some stuff be exported to lua too? Suppose ruleset could define various "AI hinting function". Suppos AI chose mostly overal strategy, use eco growth now, use military pressure on those and those cities of enemy, these kinds of things. Then it could execute lua hints for every city and it hint might output say improvement/unit that city should produce (well it's more complicated, maybe city should change production and some other things, also with arbitraryish effects governors would likely become more complicated...). Not sure if such things are possible, but maybe it is worth thinking about it for a bit?

Lastly there is of course performance question and how much things would slow down with lua being called out all that much, and maybe "brutaforced governors" trying various combination of worker placement to see what matches best.

And I know that talking is way way easier than doing :). Still I thoguh maybe these ideas might be something that was not considered all that much.

Why one might want such flexibility. well some examples:
Unit that you can build if your nation has wonder OR building city has specific improvement, but maybe not if some tech was already researched OR if some other tech is missing.
Rulset without techs, where all advacement would happen through buildings and wonders. I really really want this one, cause in my opinion that would be way way supperior to tech based game, which currently allows all kinds of crazynes, like taking enemies throway vilage, with nothing in it, then dump enough gold (oh how I hate buying mechanic too :) and there is no way to adjust anything about it at all, apart from reworking shield requirements to be crazy high for everything, but then tile outputs can only display one digit... so tiles can not output large amounts of resources, workaround is free bonus tu city production...), and bam aircraft carrier (or battleship, or nuke) comes out of it in next turn! Thats just totally crazy. More over you can do this with aquired tech that you do not even have prerequisites! (Well 2.6 will have allow_holes disabled, and quite a big part of this will be solved, still).
User avatar
Alien Valkyrie
Elite
Posts: 513
Joined: Sun Feb 10, 2013 10:21 pm
Location: Stuttgart, Germany

Re: More flexibility for Requirement Vectors

Post by Alien Valkyrie »

morphles wrote:Why one might want such flexibility. well some examples:
Unit that you can build if your nation has wonder OR building city has specific improvement, but maybe not if some tech was already researched OR if some other tech is missing.
This is something I thought about a bit, and perhaps a solution for it could be that firstly, there are requirement vectors for units instead of the current system, and secondly, that it's possible to define multiple different requirement vectors for units and buildings, one of which must evaluate to true to be able to build the unit/building.
morphles wrote:Rulset without techs, where all advacement would happen through buildings and wonders. I really really want this one, cause in my opinion that would be way way supperior to tech based game, which currently allows all kinds of crazynes, like taking enemies throway vilage, with nothing in it, then dump enough gold (oh how I hate buying mechanic too :) and there is no way to adjust anything about it at all, apart from reworking shield requirements to be crazy high for everything, but then tile outputs can only display one digit... so tiles can not output large amounts of resources, workaround is free bonus tu city production...), and bam aircraft carrier (or battleship, or nuke) comes out of it in next turn! Thats just totally crazy. More over you can do this with aquired tech that you do not even have prerequisites! (Well 2.6 will have allow_holes disabled, and quite a big part of this will be solved, still).
Most of this is already possible, since buildings have requirement vectors - just like the Energy plants and Mfg. Plant need a factory in the city, you can have buildings require specific other buildings. And since units can already have one building requirement, you can just use that.
Still, though, the buying mechanic should be ruleset-configurable.
Also, about the thing with the techs: you should try the experimental ruleset, where every technology (except for the initial ones) has a root_req, meaning you need a specific other tech to be able to have that tech (e.g. you can't know Writing if you don't know Alphabet).
~ AVL
Post Reply