Author Topic: Example Event: Setting cargo at random  (Read 1379 times)

0 Members and 1 Guest are viewing this topic.

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Example Event: Setting cargo at random
After helping somebody with this idea, I thought I'd paste this little snippet here in case anybody else could use it.

Code: [Select]
$Formula: ( when-argument
   ( any-of "TC 1" "TC 2" "TC 3" "TC 4" )
   ( true )
   ( when
      ( true )
      ( modify-variable
         @tempRandomVar[0]
         ( rand-multiple 0 5 )
      )
      ( when
         ( = @tempRandomVar[0] 0 )
         ( set-cargo "Nothing" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 1 )
         ( set-cargo "Calipers" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 2 )
         ( set-cargo "Monkeys" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 3 )
         ( set-cargo "Weapons" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 4 )
         ( set-cargo "Spice" "<argument>" )
      )
      ( when
         ( = @tempRandomVar[0] 5 )
         ( set-cargo
            "Medical Supplies"
            "<argument>"
         )
      )
   )
)
+Name: Set random cargo
+Repeat Count: 1
+Interval: 1

"Why are all the actions under a sub-when that just has a condition of ( true )?" you might ask. "Don't we have a '$Loop SEXPs Then Arguments:' mod table setting for that?" Well, funny story: if you try doing it without the sub-when, it doesn't work even with Loop SEXPs Then Arguments set. The reason is that the modify-variable will only get evaluated once, because it doesn't have "<argument>" in it; with the sub-when, the whole when (including modify-variable) gets evaluated once per argument.

Worth noting that the 0-"Nothing" branch is usually going to be redundant (default cargo is "Nothing"), but included for the sake of completeness.

For fun, you can give the random number a wider range of values and then change the conditionals to check to see if the variable is between two values, so that some cargoes are more likely than others.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.

 

Offline niffiwan

  • 211
  • Eluder Class
Re: Example Event: Setting cargo at random
Hey cool, I think I have a future use for this :)

* niffiwan bookmarks post
Creating a fs2_open.log | Red Alert Bug = Hex Edit | MediaVPs 2014: Bigger HUD gauges | 32bit libs for 64bit Ubuntu
----
Debian Packages (testing/unstable): Freespace2 | wxLauncher
----
m|m: I think I'm suffering from Stockholm syndrome. Bmpman is starting to make sense and it's actually written reasonably well...

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Example Event: Setting cargo at random
This is the sort of thing the new container code excels at. But this is a nice solution until that code is in trunk.

Quote
"Why are all the actions under a sub-when that just has a condition of ( true )?" you might ask. "Don't we have a '$Loop SEXPs Then Arguments:' mod table setting for that?" Well, funny story: if you try doing it without the sub-when, it doesn't work even with Loop SEXPs Then Arguments set. The reason is that the modify-variable will only get evaluated once, because it doesn't have "<argument>" in it; with the sub-when, the whole when (including modify-variable) gets evaluated once per argument.

Actually that's what the do-for-valid-arguments SEXP is for. It basically makes any SEXP in a branch below it trigger once for every argument that is valid. Which means that as long as you have already used '$Loop SEXPs Then Arguments in mod.tbl you no longer need the inner when and can simplify the code to this



when-argument
- any-of
-- TC 1
-- etc
- Do-for-valid-arguments
-- modify-variable
etc.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

  

Offline AdmiralRalwood

  • 211
  • The Cthulhu programmer himself!
    • Skype
    • Steam
    • Twitter
Re: Example Event: Setting cargo at random
Actually that's what the do-for-valid-arguments SEXP is for. It basically makes any SEXP in a branch below it trigger once for every argument that is valid. Which means that as long as you have already used '$Loop SEXPs Then Arguments in mod.tbl you no longer need the inner when and can simplify the code to this
Yes, that's another option. However, doing it this way makes it easier to replace the when's "true" condition with something else (campaign-persistent variable from a previous mission, for example). A bit of future-proofing, if you will (side effect that it also works properly with Loop SEXPs Then Arguments not set/set to false is just a bonus).

Yes, you can just change the condition of the when-argument, or wrap the whole thing in another when. It's purely a personal preference thing.
Ph'nglui mglw'nafh Codethulhu GitHub wgah'nagl fhtagn.

schrödinbug (noun) - a bug that manifests itself in running software after a programmer notices that the code should never have worked in the first place.

When you gaze long into BMPMAN, BMPMAN also gazes into you.

"I am one of the best FREDders on Earth" -General Battuta

<Aesaar> literary criticism is vladimir putin

<MageKing17> "There's probably a reason the code is the way it is" is a very dangerous line of thought. :P
<MageKing17> Because the "reason" often turns out to be "nobody noticed it was wrong".
(the very next day)
<MageKing17> this ****ing code did it to me again
<MageKing17> "That doesn't really make sense to me, but I'll assume it was being done for a reason."
<MageKing17> **** ME
<MageKing17> THE REASON IS PEOPLE ARE STUPID
<MageKing17> ESPECIALLY ME

<MageKing17> God damn, I do not understand how this is breaking.
<MageKing17> Everything points to "this should work fine", and yet it's clearly not working.
<MjnMixael> 2 hours later... "God damn, how did this ever work at all?!"
(...)
<MageKing17> so
<MageKing17> more than two hours
<MageKing17> but once again we have reached the inevitable conclusion
<MageKing17> How did this code ever work in the first place!?

<@The_E> Welcome to OpenGL, where standards compliance is optional, and error reporting inconsistent

<MageKing17> It was all working perfectly until I actually tried it on an actual mission.

<IronWorks> I am useful for FSO stuff again. This is a red-letter day!
* z64555 erases "Thursday" and rewrites it in red ink

<MageKing17> TIL the entire homing code is held up by shoestrings and duct tape, basically.