Scripting Questions In General

Creation, discussion, and balancing of game content such as techs, buildings, ship parts.

Moderators: Oberlus, Committer

Message
Author
xlightwavex
Space Kraken
Posts: 111
Joined: Mon Nov 16, 2015 5:57 am

Re: Scripting Questions In General

#46 Post by xlightwavex »

To summarize the if[] effect[] bracket behavior is inconsistent depending on the level of the nesting.
Good catch. Could you create and test a very simple example for a bug report?
I can do it later this week if you don't find the time.
not really sure how to make this simpler

Expected behavior:
1). mutual exclusion.
When a condition is met the other conditions within the scope of the else statements following or preceding each other are not executed such as, case c and case d in the same turn
2). non repeated execution
The same code will not execute twice in the same turn such as case D twice or case A D in the same turn (sometimes).

Code: Select all

Tech
    name = "DEF_PLANET_GUN"
    description = "DEF_PLANET_GUN_DESC"
    short_description = "DEFENSE_SHORT_DESC"
    category = "DEFENSE_CATEGORY"
    researchcost = 10
    researchturns = 1
    prerequisites = "DEF_ROOT_DEFENSE"
    unlock = Item type = Building name = "BLD_PLANETARY_GUN"
    graphic = "icons/tech/defense.png" 
   
#include "/scripting/common/priorities.macros"
#include "/scripting/common/base_prod.macros"

Code: Select all

BLD_PLANETARY_GUN
Planetary Gun Building

BLD_PLANETARY_GUN_DESC
'''planetary defense gun building description.'''

DEF_PLANET_GUN
Planetary Gun
 
DEF_PLANET_GUN_DESC
'''planetary gun description''

Code: Select all

EFFECT_TESTCASE_LABEL
effect_if_else testcase label

EFFECT_TESTCASE_A
case A

EFFECT_TESTCASE_B
case B

EFFECT_TESTCASE_C
case C

EFFECT_TESTCASE_D
case D
Two tests but neither are simple visibility doesn't matter as the second one errors without it at all just the same.

Code: Select all

BuildingType
name = "BLD_PLANETARY_GUN"
description = "BLD_PLANETARY_GUN_DESC"
buildcost = 10
buildtime = 1
location = And [
    Planet
    Not Contains Building name = "BLD_PLANETARY_GUN"
    Not Planet type = [GasGiant]
    OwnedBy empire = Source.Owner
]
EnqueueLocation = And [
    Not Contains Building name = "BLD_PLANETARY_GUN"
    Not Enqueued type = Building name = "BLD_PLANETARY_GUN"
    Not Planet type = [GasGiant]
    OwnedBy empire = Source.Owner
]
effectsgroups = [
    EffectsGroup
    scope = NumberOf number = 1 condition = And [
        Ship
        InSystem id = Source.SystemID
        Or [
            OwnedBy affiliation = EnemyOf empire = Source.Owner
            Unowned
        ]
        //VisibleToEmpire empire = Source.Owner
    ]
    activation = Source
    effects = 
    [     
        If condition = and [
                Not VisibleToEmpire empire = Source.Owner
                Structure high = 15
        ]
            effects = 
            [
                GenerateSitrepMessage
                    message = "EFFECT_TESTCASE_A"
                    label = "EFFECT_TESTCASE_LABEL"
                    icon = "icons/sitrep/combat_destroyed.png"
            ]
        else = 
            If condition = and [
                    Not VisibleToEmpire empire = Source.Owner
                    Structure low = 15
            ]
            effects = 
            [
                GenerateSitrepMessage
                    message = "EFFECT_TESTCASE_B"
                    label = "EFFECT_TESTCASE_LABEL"
                    icon = "icons/sitrep/combat_destroyed.png"
            ]
        else = 
            If condition = and [
                    VisibleToEmpire empire = Source.Owner
                    Structure high = 15
            ]
            effects = 
            [
                GenerateSitrepMessage
                    message = "EFFECT_TESTCASE_C"
                    label = "EFFECT_TESTCASE_LABEL"
                    icon = "icons/sitrep/combat_destroyed.png"
            ]
        else = 
            If condition = and [
                    VisibleToEmpire empire = Source.Owner
                    Structure low = 15
            ]
            effects = 
            [
                GenerateSitrepMessage
                    message = "EFFECT_TESTCASE_D"
                    label = "EFFECT_TESTCASE_LABEL"
                    icon = "icons/sitrep/combat_destroyed.png"
            ]
    ]
]
icon = "icons/meter/damage.png"
#include "/scripting/common/priorities.macros"
#include "/scripting/common/base_prod.macros"
In the above case D should not execute twice per turn from a single planetary gun building.
Nor any duel combination just one of them per turn.

Or if you like simply replace that above with the below.
It still messes up chained else if's statements don't work right either.

Visibility makes no difference as shown below and above same messed up results.

Code: Select all

BuildingType
name = "BLD_PLANETARY_GUN"
description = "BLD_PLANETARY_GUN_DESC"
buildcost = 10
buildtime = 1
location = And [
    Planet
    Not Contains Building name = "BLD_PLANETARY_GUN"
    Not Planet type = [GasGiant]
    OwnedBy empire = Source.Owner
]
EnqueueLocation = And [
    Not Contains Building name = "BLD_PLANETARY_GUN"
    Not Enqueued type = Building name = "BLD_PLANETARY_GUN"
    Not Planet type = [GasGiant]
    OwnedBy empire = Source.Owner
]
effectsgroups = [
    EffectsGroup
    scope = NumberOf number = 1 condition = And [
        Ship
        InSystem id = Source.SystemID
        Or [
            OwnedBy affiliation = EnemyOf empire = Source.Owner
            Unowned
        ]
        VisibleToEmpire empire = Source.Owner
    ]
    activation = Source
    effects = 
    [     
            //#0
            If condition = and [
                Structure high = 8
            ]
            effects = 
            [
                GenerateSitrepMessage
                    message = "EFFECT_TESTCASE_A"
                    label = "EFFECT_TESTCASE_LABEL"
                    icon = "icons/sitrep/combat_destroyed.png"
            ]
            //#1
            else = 
                If condition = and [
                        Structure high = 15
                ]
                effects = 
                [
                    GenerateSitrepMessage
                        message = "EFFECT_TESTCASE_B"
                        label = "EFFECT_TESTCASE_LABEL"
                        icon = "icons/sitrep/combat_destroyed.png"
                ]
                //#2
                else = 
                    If condition = and [
                            Structure high = 30
                    ]
                    effects = 
                    [
                        GenerateSitrepMessage
                            message = "EFFECT_TESTCASE_C"
                            label = "EFFECT_TESTCASE_LABEL"
                            icon = "icons/sitrep/combat_destroyed.png"
                    ]
                    //#3
                    else = 
                        If condition = and [
                                Structure high = 100
                        ]
                        effects = 
                        [
                            GenerateSitrepMessage
                                message = "EFFECT_TESTCASE_D"
                                label = "EFFECT_TESTCASE_LABEL"
                                icon = "icons/sitrep/combat_destroyed.png"
                        ]
           // end
    ]
]
icon = "icons/meter/damage.png"
#include "/scripting/common/priorities.macros"
#include "/scripting/common/base_prod.macros"

If else statements are mutually exclusive on the 1 or 0 return value of the if statement.
Honestly id be happy with just like 2 or 3 levels of ifs even hard coded if it worked right, i don't really need it to be true recursive nesting.
Else if and nested if's complicates it a bit due to parsing and rules i suppose that's gotta be tough to parse.

Some kind of switch case might have been easier were i added matching conditions to a int flag to do a switch case on. Something like the below binary selection.

flag a Value = value + if[ VisibleToEmpire empire = Source.Owner ] * 1
flag a Value = value + if[ Structure low = 15 ] * 2
flag a Value = value + if[ Fuel low = 1 ] * 4

switch[ flag a ]
[
effect[...] // 0
effect[...] // 1
effect[...] // 2
effect[...] // 3
effect[...] // 4
effect[...] // 5
effect[...] // 6
effect[...] // 7
]

Were switch just holds a list/array of effects and the flag has one number to pick one that can execute that's basically the 'same' as a chain of, if else if or even groups of nested chains of them. Id take that alternatively :)

dbenage-cx
Programmer
Posts: 389
Joined: Sun Feb 14, 2016 12:08 am

Re: Scripting Questions In General

#47 Post by dbenage-cx »

Trying to generate duplicate SitReps or effects, the following was added to Cultural Archives entry:

Code: Select all

        EffectsGroup
            scope = And [
                Ship
                InSystem
                OwnedBy empire = Source.Owner
            ]
            effects = [
                If condition = Fuel high = 2  // Adding And[] to both If conditions has no impact
                    effects = SetFuel value = Value + 1
                Else = If condition = Detection high = 1
                        effects = SetDetection value = Value + 10
                    Else = SetMaxFuel value = Value + 1
                        GenerateSitRepMessage  // Position of this SitRep has no impact
                            message = "EFFECT_DRYDOCK_SHIP_REPAIR_COMPLETE"
                            label = "SITREP_SHIP_REPAIR_DOCK_COMPLETE"
                            icon = "icons/sitrep/ship-repair-complete.png"
                            parameters = [
                                tag = "ship" data = Target.ID
                                tag = "building" data = Source.ID
                                tag = "planet" data = Source.PlanetID
                            ]
            ]
Despite moving the SitRep around and deepening the if/else structure, I am not able to produce any duplicated effects.

I will try your samples later on to check further, I assume there were no duplicates with the last Planetary Gun I posted.

xlightwavex
Space Kraken
Posts: 111
Joined: Mon Nov 16, 2015 5:57 am

Re: Scripting Questions In General

#48 Post by xlightwavex »

Nop your's worked before when its only nested to two level but that's the same as my temporary fix i posted before. The behavior isn't consistent either im seeing, sometimes it will appear to work or the messages will be right and damage will be wrong. Regardless a fix that doesn't address the actual problem really doesn't fix the problem.

I compiled the october 1st build from scratch for that test. Now that i think about it, i guess it's possible i screwed something up trying to get the ship names to show. Humm ill try recompiling it and check again, as well as starting a new test over from the beginning. Rebuilt it same behavior.

RE:
Recompiled out the change i made and altered the test slightly still failing, pic at the bottom.

Code: Select all

EFFECT_TESTCASE_LABEL
effect_if_else testcase label

EFFECT_TESTCASE_A
from %planet% case A

EFFECT_TESTCASE_B
from %planet% case B

EFFECT_TESTCASE_C
from %planet% case C

EFFECT_TESTCASE_D
from %planet% case D

Code: Select all

BuildingType
name = "BLD_PLANETARY_GUN"
description = "BLD_PLANETARY_GUN_DESC"
buildcost = 10
buildtime = 1
location = And [
    Planet
    Not Contains Building name = "BLD_PLANETARY_GUN"
    Not Planet type = [GasGiant]
    OwnedBy empire = Source.Owner
]
EnqueueLocation = And [
    Not Contains Building name = "BLD_PLANETARY_GUN"
    Not Enqueued type = Building name = "BLD_PLANETARY_GUN"
    Not Planet type = [GasGiant]
    OwnedBy empire = Source.Owner
]
effectsgroups = [
    EffectsGroup
    scope = NumberOf number = 1 condition = And [
        Ship
        InSystem id = Source.SystemID
        Or [
            OwnedBy affiliation = EnemyOf empire = Source.Owner
            Unowned
        ]
        VisibleToEmpire empire = Source.Owner
    ]
    activation = Source
    effects = 
    [     
        If condition = and [
            Structure high = 9.9999
            //VisibleToEmpire empire = Source.Owner
        ]
            effects = 
            [
                Destroy
                GenerateSitrepMessage
                    message = "EFFECT_TESTCASE_A"
                    label = "EFFECT_TESTCASE_LABEL"
                    icon = "icons/sitrep/combat_destroyed.png"
                    parameters = [tag = "planet" data = Source.PlanetID]
            ]
            else = 
                If condition = and [
                    Structure low = 10
                    //VisibleToEmpire empire = Source.Owner
                ]
                effects = 
                [
                    SetStructure value = Value - 10
                    GenerateSitrepMessage
                        message = "EFFECT_TESTCASE_B"
                        label = "EFFECT_TESTCASE_LABEL"
                        icon = "icons/sitrep/combat_destroyed.png"
                        parameters = [tag = "planet" data = Source.PlanetID]
                ]
                else = 
                    If condition = and [
                        Structure high = 9.9999
                        //Not VisibleToEmpire empire = Source.Owner
                    ]
                    effects = 
                    [
                        Destroy
                        GenerateSitrepMessage
                            message = "EFFECT_TESTCASE_C"
                            label = "EFFECT_TESTCASE_LABEL"
                            icon = "icons/sitrep/combat_destroyed.png"
                            parameters = [tag = "planet" data = Source.PlanetID]
                    ]
                    else = 
                        If condition = and [
                            Structure low = 10
                            //Not VisibleToEmpire empire = Source.Owner
                        ]
                        effects = 
                        [
                            SetStructure value = Value - 10
                            GenerateSitrepMessage
                                message = "EFFECT_TESTCASE_D"
                                label = "EFFECT_TESTCASE_LABEL"
                                icon = "icons/sitrep/combat_destroyed.png"
                                parameters = [tag = "planet" data = Source.PlanetID]
                        ]
    ]
]
icon = "icons/meter/damage.png"
#include "/scripting/common/priorities.macros"
#include "/scripting/common/base_prod.macros"

Image

Ya something is seriously wrong there ^^^.

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

Re: Scripting Questions In General

#49 Post by Geoff the Medio »

Edit: I think this test was done before I pushed some changes I made when I didn't have internet access for a few days last weekend. Try with the latest master? /Edit

I'm not sure what your script is doing, and it looks overly complicated for that. So perhaps try adding this to the effectsgroups for the cultural archives, and starting a game with no enemy empires. Run a few turns and observe the 3 different sitreps generated, then scrap your orbital drydock and observe your starting fleet losing structure over time and then being destroyed, and the sitreps changing accordingly. Seems to work fine to me...

Code: Select all

        EffectsGroup
            scope = And [
                Ship
                InSystem id = Source.SystemID
                VisibleToEmpire empire = Source.Owner
            ]
            activation = Source
            effects = [
                If condition = Structure low = 12
                    effects = [
                        GenerateSitrepMessage
                            message = "EFFECT_TESTCASE_A"
                            label = "EFFECT_TESTCASE_LABEL"
                            icon = "icons/sitrep/combat_destroyed.png"
                            parameters = [tag = "planet" data = Source.PlanetID]
                        SetStructure value = Value - 1
                    ]
                    else = If condition = Structure low = 7
                        effects = [
                            GenerateSitrepMessage
                                message = "EFFECT_TESTCASE_B"
                                label = "EFFECT_TESTCASE_LABEL"
                                icon = "icons/sitrep/combat_destroyed.png"
                                parameters = [tag = "planet" data = Source.PlanetID]
                            SetStructure value = Value - 1
                        ]
                        else = If condition = Structure low = 4
                            effects = [
                                GenerateSitrepMessage
                                    message = "EFFECT_TESTCASE_C"
                                    label = "EFFECT_TESTCASE_LABEL"
                                    icon = "icons/sitrep/combat_destroyed.png"
                                    parameters = [tag = "planet" data = Source.PlanetID]
                                SetStructure value = Value - 1
                            ]
                            else = [
                                GenerateSitrepMessage
                                    message = "EFFECT_TESTCASE_D"
                                    label = "EFFECT_TESTCASE_LABEL"
                                    icon = "icons/sitrep/combat_destroyed.png"
                                    parameters = [tag = "planet" data = Source.PlanetID]
                                Destroy
                            ]
            ]

xlightwavex
Space Kraken
Posts: 111
Joined: Mon Nov 16, 2015 5:57 am

Re: Scripting Questions In General

#50 Post by xlightwavex »

It's really not complicated at all, im trying to basically get one enemy ship in the same system, i don't care if its visible or not when it is the scope block.

Before applying just 1 of 4 effects to it, im jtrying to branch depending on if the target is visible to the gun and if it has a specific amount of hp higher or lower then the guns damage. In the case its not visible the if condition should actually roll a dice to execute which is actually a third branch that essential breaks out of the if else chain completely because that is essentially a miss.

if it was in programing code something like the below but just using if else instead.

Code: Select all

foreach(gun in buildings)
{
  var target = GetOneEnemyShipInTheSameSystemAsBuilding(gun);
  int bitflags = 0;
  if(target.Structure > 10){ flag += 1;}
  if(target.IsVisibleTo(gun)){ flag += 2;}
  switch(bitflags)
  {
    case 0: /* effect 0 do something */ break;
    case 1: /* effect 1 do something */ break;
    case 2: /* effect 2 do something */ break;
    case 3: /* effect 3 do something */ break;
  }
}
or basically as the first example on page 1 of this topic shows in script.


Ill try to re download and recompile the entire thing again then retest it if you guys are not getting repeats it seems likely that i somehow have the older one. Ill let you know what happens in a bit.

re:
looks like its working now at least vs monsters

Post Reply