Jump to content

[RNG] In xml!


stasis78

Recommended Posts

Hey all,

 

rng functions are currently broken in the xml, randomint and randomfloat don't work and error out. Anyways I need a reliable rng for Farm Life and spent some time developing the necessary xml for a basic but functioning rng generator!

 

It was fun because there is also no modulus operation in the xml. There are also no methods for converting floats to ints or performing any rounding/flooring/etc. so I figured out how to floor floats! (Yes yes yes, its super hacky but it does work)

 

Comments and stuff in the xml here.

 

Enjoy ;)

 

Note: I did test this but only with a modulo of around 110. Not sure how it will perform with numbers larger than that, but in general this should be sufficient for most use cases I can think of off hand.

 

The epsilon check in the last part is not necessary but I left it in as an example.

Link to comment
Share on other sites

The guts of it are here if you are allergic to GitHub ->

 

 <buff name="example">
   <!-- This is a magic number for flooring -->
   <triggered_effect trigger="onSelfBuffStart" action="ModifyCVar" target="self" cvar="floor_c" operation="set" value="16777216"/>

   <!-- Init our seed and reset it if we get too big -->
   <triggered_effect trigger="onSelfBuffStart" action="ModifyCVar" target="self" cvar="rnd_seed" operation="set" value="@random_ticker"/>
   <triggered_effect trigger="onSelfBuffRemove" action="ModifyCVar" target="self" cvar="rnd_seed" operation="set" value="0"/>
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="rnd_seed" operation="set" value="@random_ticker">
       <requirement name="CVarCompare" cvar="rnd_seed" operation="LTE" value="1"/>
       <requirement name="CVarCompare" cvar="rnd_seed" operation="GTE" value="@floor_c"/>
   </triggered_effect>

   <!-- Setup the result, which will be 'result_cvar' - 'work_cvar' in the last step.  -->
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="result_cvar" operation="set" value="@rnd_seed"/>
   <!-- Copy the result into our working variable -->
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="work_cvar" operation="set" value="@result_cvar"/>

   <!-- This is the modulus operation. So basically its 'rnd_seed' % YOUR_NUMBER-1 -->
   <!--  YOUR_NUMBER is whatever you want the count to be, rng range 0 to YOUR_NUMBER-1 -->
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="work_cvar" operation="divide" value="YOUR_NUMBER"/>
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="before_floor" operation="set" value="@work_cvar"/>
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="work_cvar" operation="add" value="@floor_c"/>
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="work_cvar" operation="subtract" value="@floor_c"/>
   <!-- The rounding function is super hacky but we have a whole number atleast. Make sure we rounded correctly. -->
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="work_cvar" operation="subtract" value="1.0">
       <requirement name="CVarCompare" cvar="work_cvar" operation="GTE" value="@before_floor"/>
   </triggered_effect>
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="work_cvar" operation="multiply" value="YOUR_NUMBER"/>
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="result_cvar" operation="subtract" value="@work_cvar"/>
   <!-- This adds the results back into the seed for the next round -->
   <triggered_effect trigger="onSelfBuffUpdate" action="ModifyCVar" target="self" cvar="rnd_seed" operation="add" value="@result_cvar"/>

   <!-- Now you can use the result like this -->
   <triggered_effect name="onSelfBuffUpdate" action="ShowToolbeltMessage" message="I'm a random number 0">
       <requirement name="CVarCompare" cvar="result_cvar" operation="GTE" value="0.000"/>
       <requirement name="CVarCompare" cvar="result_cvar" operation="LTE" value="0.999"/>
   </triggered_effect>

 </buff>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...