Macros!

For topics that do not fit in another sub-forum.

Moderator: Oberlus

Message
Author
User avatar
qsswin
Pupating Mass
Posts: 93
Joined: Tue Oct 18, 2011 6:48 pm
Location: UTC-5

Macros!

#1 Post 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.

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Features for post v0.4

#2 Post 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).

User avatar
eleazar
Design & Graphics Lead Emeritus
Posts: 3858
Joined: Sat Sep 23, 2006 7:09 pm
Location: USA — midwest

Re: Features for post v0.4

#3 Post 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.

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Features for post v0.4

#4 Post 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...

User avatar
Adrian
Space Floater
Posts: 49
Joined: Thu Jan 26, 2012 8:40 pm
Location: Eridanus Supervoid

Re: Features for post v0.4

#5 Post by Adrian »

Would it be useful to maintain a tree graphic, showing the basic relationship of script files, to aid new volunteers?

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Features for post v0.4

#6 Post 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.

Dart00_Tech
Space Kraken
Posts: 143
Joined: Sat Jun 11, 2011 1:27 am
Location: Modesto, CA USA

Re: Features for post v0.4

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

User avatar
Adrian
Space Floater
Posts: 49
Joined: Thu Jan 26, 2012 8:40 pm
Location: Eridanus Supervoid

Re: Features for post v0.4

#8 Post 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:

User avatar
eleazar
Design & Graphics Lead Emeritus
Posts: 3858
Joined: Sat Sep 23, 2006 7:09 pm
Location: USA — midwest

Re: Features for post v0.4

#9 Post 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.

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Features for post v0.4

#10 Post 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.

User avatar
eleazar
Design & Graphics Lead Emeritus
Posts: 3858
Joined: Sat Sep 23, 2006 7:09 pm
Location: USA — midwest

Re: Features for post v0.4

#11 Post 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.

Dart00_Tech
Space Kraken
Posts: 143
Joined: Sat Jun 11, 2011 1:27 am
Location: Modesto, CA USA

Re: Features for post v0.4

#12 Post 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....

User avatar
Vezzra
Release Manager, Design
Posts: 6095
Joined: Wed Nov 16, 2011 12:56 pm
Location: Sol III

Re: Features for post v0.4

#13 Post 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

User avatar
eleazar
Design & Graphics Lead Emeritus
Posts: 3858
Joined: Sat Sep 23, 2006 7:09 pm
Location: USA — midwest

Re: Macros!

#14 Post 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.
Attachments
macro-problem.zip
(11.39 KiB) Downloaded 113 times

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 13587
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Macros!

#15 Post by Geoff the Medio »

You need to have a newline at the end of the file.

Post Reply