FieldType
    name = "FLD_ION_STORM"
    description = "FLD_ION_STORM_DESC"
    stealth = 0.01
    tags = [ "EXOTIC" ]
    effectsgroups = [
        EffectsGroup    // move around
            scope = Source
            effects = [
                SetSpeed value = 10
                MoveTowards speed = Source.Speed/1.4 x = UniverseCentreX y = UniverseCentreY
                MoveInOrbit speed = Source.Speed/1.4 x = UniverseCentreX y = UniverseCentreY
            ]

        EffectsGroup    // grow size when young
            scope = Source
            activation = And [
                ValueTest high = max((UniverseWidth ^ 1.1) / 50, 20) TestValue = Source.Age
                Size high = 50
               ]
            effects = SetSize value = Value + min(max(Value * RandomNumber(0.05, 0.1), 1.0), 3.0)

        EffectsGroup    // shrink size when old
            scope = Source
            activation = ValueTest low = max((UniverseWidth ^ 1.1) / 50, 20) TestValue = Source.Age
            effects = SetSize value = Value - min(max(Value * RandomNumber(0.05, 0.1), 1.0), 3.0)
/*
        EffectsGroup    // after reaching a certain age, shrink size a bit each turn when near systems
            scope = Source
            activation = ValueTest low = 10 TestValue = Source.Age
            effects = SetSize value = Value - 0.3 * (Count condition = And [
                System
                WithinDistance distance = 80 condition = Source
            ])
*/
        EffectsGroup    // affect stealth / detection of objects in storm
            scope = And [
            	Not Field
            	WithinDistance distance = Source.Size condition = Source
            ]
            stackinggroup = "ION_STORM_STEALTH_DETECTION_REDUCTION"
            effects = [
                SetStealth value = Value + 40
                SetDetection value = Value - 40
            ]

        EffectsGroup    // after reaching a certain age, dissipate when small
            scope = Source
            activation = And [
                ValueTest low = 10 TestValue = Source.Age
                Size high = 10
            ]
            effects = Destroy

    ]
    graphic = "fields/rainbow_storm.png"

FieldType
    name = "FLD_MOLECULAR_CLOUD"
    description = "FLD_MOLECULAR_CLOUD_DESC"
    stealth = 0.01
    tags = [ "EXOTIC" ]
    effectsgroups = [
        EffectsGroup    // move around
            scope = Source
            effects = [
                SetSpeed value = 5
                MoveTowards speed = Source.Speed/1.4 x = UniverseCentreX y = UniverseCentreY
                MoveInOrbit speed = Source.Speed/1.4 x = UniverseCentreX y = UniverseCentreY
            ]

        EffectsGroup    // grow size when young
            scope = Source
            activation = And [
                ValueTest high = max((UniverseWidth ^ 1.1) / 50, 30) TestValue = Source.Age
                Size high = 120
               ]
            effects = SetSize value = Value + min(max(Value * RandomNumber(0.05, 0.1), 1.0), 5.0)

        EffectsGroup    // shrink size when old
            scope = Source
            activation = ValueTest low = max((UniverseWidth ^ 1.1) / 50, 30) TestValue = Source.Age
            effects = SetSize value = Value - min(max(Value * RandomNumber(0.05, 0.1), 1.0), 5.0)
/*
        EffectsGroup    // after reaching a certain age, shrink size a bit each turn when near systems
            scope = Source
            activation = ValueTest low = 10 TestValue = Source.Age
            effects = SetSize value = Value - 0.1 * (Count condition = And [
                System
                WithinDistance distance = 80 condition = Source
            ])
*/
        EffectsGroup    // reduce shields
            scope = And [
                Ship
                WithinDistance distance = Source.Size condition = Source
            ]
            stackinggroup = "MOLECULAR_CLOUD_SHIELD_REDUCTION"
            effects = SetMaxShield value = Value - 15

        EffectsGroup    // after reaching a certain age, dissipate when small
            scope = Source
            activation = And [
                ValueTest low = 10 TestValue = Source.Age
                Size high = 10
            ]
            effects = Destroy
    ]
    graphic = "nebulae/nebula8.png"

FieldType
    name = "FLD_NEBULA_1"
    description = "FLD_NEBULA_1_DESC"
    stealth = 0.01
    effectsgroups = [
        EffectsGroup    // shrink slowly when in no-star system
            scope = Source
            activation = And [
                Size low = 2 high = 180
                Star type = NoStar
            ]            
            effects =
                SetSize value = Value + RandomNumber(-0.8, 0.3)

        EffectsGroup    // shrink slowly when in no-star system
            scope = Source
            activation = And [
                Size low = 180
                Star type = NoStar
            ]            
            effects =
                SetSize value = Value + RandomNumber(-0.8, 0)
        
        EffectsGroup    // spawn new star when small enough
            scope = And [ 
                    System
                    Object id = Source.SystemID
                ]
            activation = And [
                Size high = 5
                Star type = NoStar
            ]
            effects = [
                SetStarType type = Blue
                GenerateSitRepMessage
                    message = "EFFECT_NEBULA"
                    icon = "stars/blue01.png"
                    parameters = [
                        tag = "system" data = Source.SystemID
                        ]
                    affiliation = CanSee condition = Source
                ]

        [[CREATE_PLANETS]]

        EffectsGroup    // dissipate when small
            scope = Source
            activation = Size high = 5
            effects = Destroy
    ]
    graphic = "nebulae/nebula9.png"
    
FieldType
    name = "FLD_NEBULA_2"
    description = "FLD_NEBULA_2_DESC"
    stealth = 0.01
    effectsgroups = [
        EffectsGroup    // shrink slowly when in no-star system
            scope = Source
            activation = And [
                Size low = 2 high = 180
                Star type = NoStar
            ]
            effects =
                SetSize value = Value + RandomNumber(-0.5, 0.3)

        EffectsGroup    // shrink slowly when in no-star system
            scope = Source
            activation = And [
                Size low = 180
                Star type = NoStar
            ]
            effects =
                SetSize value = Value + RandomNumber(-0.5, 0)

        EffectsGroup    // spawn new star when small enough
            scope = And [
                System
                Object id = Source.SystemID
            ]
            activation = And [
                Size high = 5
                Star type = NoStar
            ]
            effects = [
                SetStarType type = White
                GenerateSitRepMessage
                    message = "EFFECT_NEBULA"
                    icon = "stars/white01.png"
                    parameters = [
                        tag = "system" data = Source.SystemID
                        ]
                    affiliation = CanSee condition = Source
                ]

        [[CREATE_PLANETS]]

        EffectsGroup    // dissipate when small
            scope = Source
            activation = Size high = 5
            effects = Destroy
    ]
    graphic = "nebulae/nebula15.png"

CREATE_PLANETS
'''      EffectsGroup
            scope = And [
                System
                Object id = Source.SystemID
            ]
            activation = Size high = 5
            effects = [
                If condition = Random probability = 0.5
                    effects = CreatePlanet type = asteroids planetsize = asteroids name = UserString("NEW_ASTEROIDS_NAME") % Target.Name % Source.Name
                If condition = Random probability = 0.6
                    effects = CreatePlanet type = OneOf(Barren, Desert, Inferno, Ocean, Radiated, Swamp, Terran, Toxic, Tundra) planetsize = OneOf(tiny, small, medium, large) name = UserString("NEW_PLANET_NAME") % Target.Name
                else = If condition = Random probability = 0.4
                    effects = CreatePlanet type = GasGiant planetsize = GasGiant name = UserString("NEW_PLANET_NAME") % Target.Name
                If condition = Random probability = 0.3
                    effects = CreatePlanet type = asteroids planetsize = asteroids name = UserString("NEW_ASTEROIDS_NAME") % Target.Name % Source.Name
            ]
'''
