Difference between revisions of "Sensor degradation with range or facing"

From FreeSpace Wiki
Jump to: navigation, search
(Added an if-then-else version of the example event)
(Improved comments)
Line 219: Line 219:
 
In the revised event above, [[if-then-else]] is used to check the three distance brackets in sequence. If a ship is shown to place within a bracket, it acts on that and breaks out, whereas if a ship is not within the bracket being checked, it moves on to the next bracket and checks that instead. This reduces the amount of checks that happen compared to the first version of the event, and as a result will place a slightly lower demand on system resources. And while this is generally not a noticable effect, every CPU cycle does count.
 
In the revised event above, [[if-then-else]] is used to check the three distance brackets in sequence. If a ship is shown to place within a bracket, it acts on that and breaks out, whereas if a ship is not within the bracket being checked, it moves on to the next bracket and checks that instead. This reduces the amount of checks that happen compared to the first version of the event, and as a result will place a slightly lower demand on system resources. And while this is generally not a noticable effect, every CPU cycle does count.
  
Note the use of an additional nested 'when' in the second if-then-else. This is necessary as there is more than one action to be taken, and only the second argument of the if-then-else operator is the action to be taken if it is true - Third and beyond is for when it is false. Hence, the 'when' is used to group two actions into one argument.
+
Note the use of an additional nested 'when' in the second 'if-then-else'. This is necessary as there is more than one action to be taken, and only the second argument of the 'if-then-else' operator is the action to be taken if it is true - Third and beyond is for when it is false. Hence, the 'when' is used to group two actions into one argument.
 +
 
 +
Also note that the last case is not an 'if-then-else', but a 'when', just as in the first version of the event. This is because it is the last case, and as such there is no 'else' if this fails as well. However, it is NOT a default case which will always be carried out should the other two fail. Were that the case, the actions in it could simply be tacked onto the third-and-beyond argument of the second 'if-then-else', with no need for the 'when'.
  
 
[[Category:FRED Examples]]
 
[[Category:FRED Examples]]

Revision as of 17:23, 16 February 2011

The following event is based on an idea by SypheDMar, as proposed in #freespace. It will, in theory, make a set of fighters either be invisible, show up as a fuzzy radar blip, or be fully targetable, all depending on their range to the player as well as whether the player is facing them (or more specifically if he has them within a 60-degree forward cone). Detailed explanation below, but without further ado, the event itself:

- op when-argument
   - op any-of
      # <list of ships for which to apply range/facing-based targetability>
   - op true
   - op when
      - op and
         - op not
            - op is-ship-stealthy
               # <argument>
         - op or
            - op and
               - op >
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 3000
               - op facing
                  # <argument>
                  # 60
            - op and
               - op >
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 1000
               - op not
                  - op facing
                     # <argument>
                     # 60
      - op ship-stealthy
         # <argument>
   - op when
      - op and
         - op or
            - op is-ship-visible
               # <argument>
            - op is-ship-stealthy
               # <argument>
         - op or
            - op and
               - op <=
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 3000
               - op >
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 1000
               - op facing
                  # <argument>
                  # 60
            - op and
               - op <=
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 1000
               - op >
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 300
               - op not
                  - op facing
                     # <argument>
                     # 60
      - op ship-invisible
         # <argument>
      - op ship-unstealthy
         # <argument>
   - op when
      - op and
         - op or
            - op not
               - op is-ship-visible
                  # <argument>
            - op is-ship-stealthy
               # <argument>
         - op or
            - op and
               - op <=
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 1000
               - op facing
                  # <argument>
                  # 60
            - op and
               - op <=
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 300
               - op not
                  - op facing
                     # <argument>
                     # 60
      - op ship-visible
         # <argument>
      - op ship-unstealthy
         # <argument>

Note the use of nested 'when' operators to cover three distinct sets of possibilities within the same event, which allows them to draw on the same argument list. This is not terribly important in this particular case, as no arguments are invalidated or revalidated, but it is kept as one event for demonstration purposes. If one prefers, the event could easily be split in three without any change in functionality (one for each distance bracket).

Now, using the above event (or three seperate ones), every distance bracket will get checked every frame against the argument list. Which, on the surface of it, is exactly what you want. But what if you could make it so the distance brackets are checked in turn, the next one in line only checked if the first one fails? That would surely save some CPU cycles. The thing is, you can. FreeSpace_Open now includes an if-then-else operator, which will let us do precisely that:

- op when-argument
   - op any-of
      # <list of ships for which to apply range/facing-based targetability>
   - op true
   - op if-then-else
      - op and
         - op not
            - op is-ship-stealthy
               # <argument>
         - op or
            - op and
               - op >
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 3000
               - op facing
                  # <argument>
                  # 60
            - op and
               - op >
                  - op distance
                     # <argument>
                     # Alpha 1
                  # 1000
               - op not
                  - op facing
                     # <argument>
                     # 60
      - op ship-stealthy
         # <argument>
      - op if-then-else
         - op and
            - op or
               - op is-ship-visible
                  # <argument>
               - op is-ship-stealthy
                  # <argument>
            - op or
               - op and
                  - op <=
                     - op distance
                        # <argument>
                        # Alpha 1
                     # 3000
                  - op >
                     - op distance
                        # <argument>
                        # Alpha 1
                     # 1000
                  - op facing
                     # <argument>
                     # 60
               - op and
                  - op <=
                     - op distance
                        # <argument>
                        # Alpha 1
                     # 1000
                  - op >
                     - op distance
                        # <argument>
                        # Alpha 1
                     # 300
                  - op not
                     - op facing
                        # <argument>
                        # 60
         - op when
            - op true
            - op ship-invisible
               # <argument>
            - op ship-unstealthy
               # <argument>
         - op when
            - op and
               - op or
                  - op not
                     - op is-ship-visible
                        # <argument>
                  - op is-ship-stealthy
                     # <argument>
               - op or
                  - op and
                     - op <=
                        - op distance
                           # <argument>
                           # Alpha 1
                        # 1000
                     - op facing
                        # <argument>
                        # 60
                  - op and
                     - op <=
                        - op distance
                           # <argument>
                           # Alpha 1
                        # 300
                     - op not
                        - op facing
                           # <argument>
                           # 60
            - op ship-visible
               # <argument>
            - op ship-unstealthy
               # <argument>

In the revised event above, if-then-else is used to check the three distance brackets in sequence. If a ship is shown to place within a bracket, it acts on that and breaks out, whereas if a ship is not within the bracket being checked, it moves on to the next bracket and checks that instead. This reduces the amount of checks that happen compared to the first version of the event, and as a result will place a slightly lower demand on system resources. And while this is generally not a noticable effect, every CPU cycle does count.

Note the use of an additional nested 'when' in the second 'if-then-else'. This is necessary as there is more than one action to be taken, and only the second argument of the 'if-then-else' operator is the action to be taken if it is true - Third and beyond is for when it is false. Hence, the 'when' is used to group two actions into one argument.

Also note that the last case is not an 'if-then-else', but a 'when', just as in the first version of the event. This is because it is the last case, and as such there is no 'else' if this fails as well. However, it is NOT a default case which will always be carried out should the other two fail. Were that the case, the actions in it could simply be tacked onto the third-and-beyond argument of the second 'if-then-else', with no need for the 'when'.