Page 1 of 2

Macros!

Posted: Fri Feb 17, 2012 12:43 am
by qsswin
Will macros be able take "paramteters," like C function-like macros? This would probably be a useful feature for macros.
Also, just how far away from an interactive space combat system are we? From looking around at old dev logs, it seems we already have a basic engine down.

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 5:30 am
by Geoff the Medio
eleazar wrote:2) Macros
I've committed a basic but functional macro, or text substitution, mechanism for content script files. It works much like the equivalent substitution mechanism for stringtables. For example, I can add to space_monster_spawn_fleets.txt a macro definition:

Code: Select all

LOCATION_NO_MONSTER_AND_NOT_WITHIN_2
'''location = And [
        Not Contains Monster
        Not WithinStarlaneJumps 2 Contains And [
            Planet
            OwnedBy AnyEmpire
        ]
    ]'''
The necessary format is a macro label (broken by whitespace), followed by ''', followed by the macro text, followed by another '''.

I can insert the macro text by referencing the label:

Code: Select all

MonsterFleet
    name = "SM_FLOATER"
    ships = [
        "SM_FLOATER"
    ]
    spawnrate = 0.7
    spawnlimit = 50
    [[LOCATION_NO_MONSTER_AND_NOT_WITHIN_2]]
Macros may be used anywhere in and only in the same file as they are defined.

This was the best syntax for defining and inserting macros that I could think of, and was chosen because ''' should not be otherwise needed in a script file, and this format and delimiter is already used for multi-line stringtable entries. The ''' surrounding the macro text are needed, even for single-line macros, to differentiate macro definitions from normal script file contents. If another suggested format is better, things can be changed.

Presently macro text cannot contain macro insertions, though I plan to add this capability (again, much like stringtable nested references).

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 2:10 pm
by eleazar
Sweet.

EDIT:
"Macros may be used anywhere in and only in the same file as they are defined."

Is that a "just the way i first implemented it" thing, or part of the big plan?

I can foresee for example a player's personal species being created or saved in a separate text file. For simplicity, and to survive updates, i'd think we'd want a single set of standard macros to be used for both.

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 6:23 pm
by Geoff the Medio
eleazar wrote:Is that a "just the way i first implemented it" thing, or part of the big plan?
There could be one or more general-use macro definition files, but I think a more powerful additoin would be a mechanism to insert the whole text of one file into another (much like the C++ #include preprocessor command) file before processing it. The inserted text would be added before macro processing, so could include macro definitions.

However, I'm a bit concerned that adding too many mechanisms of this sort starts to make things harder to read and follow (through several files of macro references) and (in the case of parameters into macros) makes things more complicated to write and understand without being a programmer...

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 8:27 pm
by Adrian
Would it be useful to maintain a tree graphic, showing the basic relationship of script files, to aid new volunteers?

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 8:29 pm
by Geoff the Medio
Adrian wrote:Would it be useful to maintain a tree graphic, showing the basic relationship of script files, to aid new volunteers?
Perhaps, but that sort of documentation tends to not actually be maintained very well, even if initially set up with good intentions.

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 9:08 pm
by Dart00_Tech
Geoff the Medio wrote:
Adrian wrote:Would it be useful to maintain a tree graphic, showing the basic relationship of script files, to aid new volunteers?
Perhaps, but that sort of documentation tends to not actually be maintained very well, even if initially set up with good intentions.
Maybe some comments at the top of the script files saying which files they are involved in....

Re: Features for post v0.4

Posted: Fri Feb 17, 2012 11:42 pm
by Adrian
Geoff the Medio wrote:Perhaps, but that sort of documentation tends to not actually be maintained very well, even if initially set up with good intentions.
Yes, good point; i trust your long term experience on this.

I'll see if i can tackle one of the minor tasks/improvements to get a feel for python and the txt files. Just need to make sure Dart00_Tech isn't working on that particular part too :wink:

Re: Features for post v0.4

Posted: Sat Feb 18, 2012 12:41 am
by eleazar
Geoff the Medio wrote:
eleazar wrote:Is that a "just the way i first implemented it" thing, or part of the big plan?
There could be one or more general-use macro definition files, but I think a more powerful additoin would be a mechanism to insert the whole text of one file into another (much like the C++ #include preprocessor command) file before processing it. The inserted text would be added before macro processing, so could include macro definitions.
Either way addresses my concerns.

Geoff the Medio wrote:However, I'm a bit concerned that adding too many mechanisms of this sort starts to make things harder to read and follow (through several files of macro references) and (in the case of parameters into macros) makes things more complicated to write and understand without being a programmer...
Well, based on my only partly applicable experience with Wesnoth macros, it could be confusing when something got messed up, and a macro was called before the text that defined it was read, or something like that.

But generally speaking, it made things more approachable, and readable. If a macro is clearly labeled, it is easier for somebody to read, understand and use: "[[LOCATION_NO_MONSTER_AND_NOT_WITHIN_2]]" than the actual scripting that makes it up. Quite a lot of Wesnoth's important scripting is handled by macros, and some of the macros (for terrain for instance) are so complicated that only a handful really understand what's going on. In other words, a lot of people just use macros for many things blissfully unaware of the underlying scripting, and often unable to read it if they tried. I don't think that's a bad thing.

Wesnoth allowed parameters in there macros, but i have no opinion if they are needed for FO.

Re: Features for post v0.4

Posted: Sat Feb 18, 2012 1:00 am
by Geoff the Medio
eleazar wrote:...it could be confusing when something got messed up, and a macro was called before the text that defined it was read...
As implemented now, FO macros do not depend on where they are defined, as long as it's in the same file. They can all be at the end, before they are referenced, or mixed through the text of the file; it shouldn't matter. The macros are all read and stored, and their definitions replaced by whitespace, which subsequent actual parsing ignores.
...some [Wesnoth's] macros (for terrain for instance) are so complicated that only a handful really understand what's going on.[...]
Wesnoth allowed parameters in there macros, but i have no opinion if they are needed for FO.
Having parameters would make macros a lot more powerful, but would also be much more likely to result in very complicated macros that are hard to understand.

Once you get a build of FO that allows recursive macros and start using them, see if you find yourself wanting to use parameters. I suspect they'll be felt necessary quite quickly...

For example, [[LOCATION_NO_MONSTER_AND_NOT_WITHIN_2]] is good, but you'd presently need to define a whole separate macro for [[LOCATION_NO_MONSTER_AND_NOT_WITHIN_3]] whereas a parameter would be obviously useful to specify the range.

There's a potential workaround without parameters, which would be to have two macros, one of which has all the text up to the place where the 2 appears, and another that has all the text after that, and then you'd use [[MACRO_BEFORE_NUMBER]]2[[MACRO_AFTER_NUMBER]] to effectively create your own parametrized macros without built-in support for it, but that would be awkward in anything but simple cases.

Re: Features for post v0.4

Posted: Sat Feb 18, 2012 1:36 am
by eleazar
Geoff the Medio wrote:As implemented now, FO macros do not depend on where they are defined, as long as it's in the same file. They can all be at the end, before they are referenced, or mixed through the text of the file; it shouldn't matter.
excellent.
Geoff the Medio wrote:Once you get a build of FO that allows recursive macros and start using them, see if you find yourself wanting to use parameters. I suspect they'll be felt necessary quite quickly...
Could be. I'm very eager to macro-ify and diversify the species, it will be my top FO priority. But for the next week, i'll be on a family vacation, so i may disappear from FO for much/all of that time.

I like to think that there will be a strong correspondence between the individual macros used for species, and the species picks we eventually will have. But i haven't closely looked at much of the scripting in species.txt. The way stuff interrelates may make that much more difficult than i wish. But the hope is to allow players to mostly create a custom species via standardized macros, without needing to understand effects.

Re: Features for post v0.4

Posted: Sat Feb 18, 2012 5:51 am
by Dart00_Tech
Adrian wrote:Just need to make sure Dart00_Tech isn't working on that particular part too :wink:
Haha, Nah, Im just working on attempting to refine the English string-table. Im currently waiting on a deciion if my last edit will be committed or not...in the mean time im working on some more icons....I was thinking about playing with the AI a bit maybe next...Also the v.4 Quick Start guide is in need of updates from v.3....

Re: Features for post v0.4

Posted: Tue Feb 21, 2012 9:43 am
by Vezzra
eleazar wrote:...
Geoff the Medio wrote:Once you get a build of FO that allows recursive macros and start using them, see if you find yourself wanting to use parameters. I suspect they'll be felt necessary quite quickly...
Could be. I'm very eager to macro-ify and diversify the species, it will be my top FO priority. But for the next week, i'll be on a family vacation, so i may disappear from FO for much/all of that time...
Have fun and get some rest :D When you're back, well rested and re-energized, a new FO build (with macros) is waiting for you! :D

Re: Macros!

Posted: Tue Feb 21, 2012 5:45 pm
by eleazar
Yah! I stole a little time to try this out...

revision 4667
""First sucessful application of macros. Species EP scripting now macro-ified."

Focus and other sorts of scripting seem to work fine.
But I'm having trouble getting it to work within effects groups. It looks like there might be an bug.

Code: Select all

2012-02-21 11:25:39,998 ERROR Client : Unresolved macro reference: STANDARD_HEALTH_AND_POPULATION
2012-02-21 11:25:40,006 ERROR Client : /Users/jbjerk/Desktop/FreeOrion.app/Contents/Resources/default/species.txt:157:8: Parse error.  Expected EffectsGroup here:
          [[STANDARD_HEALTH_AND_POPULATION]]
          ^
logs and species.txt attached.

Re: Macros!

Posted: Tue Feb 21, 2012 8:09 pm
by Geoff the Medio
You need to have a newline at the end of the file.