Mission to Destroy a Target

A common mission is to send the player out to destroy a target. Here is a basic example:

<MissionType unid="..."
      ...
      >
   <Properties>
      <Constant id="targetCriteria">
         "TAE +populated; -questTarget; -uncharted;"
      </Constant>

      <Data     id="targetID"></Data>
   </Properties>

   <Events>
      <OnCreate>
         (block (targetObj)

            ; Pick a random target

            (setq targetObj 
               (random
                  (sysFindObject 
                     aOwnerObj 
                     (msnGetProperty gSource 'targetCriteria)
                     )
                  )
               )

            ; If we have a target, remember it

            (if targetObj
               (block ()
                  (msnRegisterForEvents gSource targetObj)
                  (msnSetProperty gSource 'targetID 
                     (objGetID targetObj)
                     )
                  )

               ; Otherwise, no mission
               (msnDestroy gSource)
               )
            ) 
      </OnCreate>

      <OnObjDestroyed>
         (switch

            ; If our target was destroyed, then mission success

            (= (objGetID aObjDestroyed) 
                  (msnGetProperty gSource 'targetID)
                  )
               (switch
                  aDestroy
                     (msnSuccess gSource)

                  ; Otherwise, the target left the system or something.
                  ; Check aDestroyReason to figure out what happened 
                  ; and deal with it accordingly.

                  ...
                  )
            )
      </OnObjDestroyed>
   </Events>
</MissionType>

A few notes on this code:

  • We recommend using properties here instead of the old system of storing in mission data. Properties are self-documenting and easier to introspect.
  • We placed the target criteria as a property so that it can be changed easily. Note that the code uses the random function to pick a random target from the criteria. Adjust this if necessary (e.g., if the target criteria returns a single object).
  • The call to msnRegisterForEvents is required so that the mission object gets the <OnObjDestroyed> event.
  • If the player declines the mission and the target is later destroyed, the mission will still get called at <OnObjDestroyed> and the mission will succeed, but the player will not be credited (because they did not accept).
Sign In
Username:
Password:
Register a new account
Forgot my password
Sign In
Cancel
Register
Username:
Password:
Confirm:
Email:
I already have an account
Terms of Service
Sign In
Cancel
Are You Sure?
Are you sure?
OK
Cancel
Error.
OK