The workaround code is rather simple, but I'm pointing it out because it's clearly not how it's supposed to work. For one thing, it's contrary to the function description given in the lua reference
(although that description does contain a subtle error discussed below). You're not supposed to have asteroids with trees belonging to two (or more) different factions, and when it happens it can really mess things up.
Given a = asteroid in question; f = faction whose tree you want to plant:
if a:AvailableStructureSpace() and (a.IsBarren() or a.owner == f) then
In looking into this bug I did gain an appreciation of the subtleties involved in planting a tree so it's not so straightforward as a simple owner check.
Well, why bother so much of this? And this actually allows more stuff to be done, to prevent it; just check if the owner is themself else that there is noone that have been planting one it...
That alone won't work, two different empires can plant on an asteroid at the same time. The asteroid doesn't actually change owners until the roots reach the center.
Exactly. You can have legitimate situations where two different factions can legally plant a tree at roughly the same time. If you have a virgin asteroid with zero existing trees (and room for trees of course), the new owner will be whoever gets their roots to touch the center first, in which case all the trees will become theirs. Once there is one root system that touches the center, the rules are different.
Here's the subtle omission in the function description I mentioned above:
Attempt to plant a Dyson tree using the seedlings present at the asteroid; if nothing can be done (all tree slots are taken, or there are not enough seedlings or the asteroi [sic] is owned by some other faction than team 0 or the given faction) the function does nothing and returns nil. Otherwise the function returns a Structure, which is the base type of the trees in Dyson.
Any faction can plant on any asteroid with zero existing tree roots so long as there are tree slots, no matter which faction is designated as the owner.