PDA

View Full Version : Question about mathematical averaging



In-Betweener
June 28th, 2011, 04:32
I'm working on modifying a Shadowrun 4.0 ruleset back to 3.0 and need to average two stat numbers to get the Reaction number, but I can't get the div operator to work properly. It'll still only take the sum of the two stats and ignore the division operation. I'm relatively new to modifying these rulesets and was wondering if anyone had a pointer on how to perform that operation.

Moon Wizard
June 28th, 2011, 07:26
How are you performing the averaging? Perhaps some code snippets of the function performing the div would help.

Regards,
JPG

In-Betweener
June 28th, 2011, 11:08
OK, i'll put up the block as it normally appears. I've tried the div operator, i've tried mod, i've even tried *0.5 and i'm guessing I just don't have the syntax right or I'm completely misunderstanding something vital.

<speccheck name="memorycheck" source="stat.memory.check">
<anchored>
<to>composurecheck</to>
</anchored>
<readonly />
<target>memorycheck</target>
<description>
<text>Memory (LOG+INT)</text>
</description>
<source>
<name>stat.logic.score</name>
<op>+</op>
</source>
<source>
<name>stat.intuition.score</name>
<op>+</op>
</source>
</speccheck>

Moon Wizard
June 28th, 2011, 17:25
I am not the developer on that ruleset, so I'm not positive without seeing the templates used. However, it looks like one of the base templates for that field is "linkednumber" (or something similar). You can look up "speccheck" to find that template and see what it is doing.

The "linkednumber" template has been around for a while and will allow you to perform mathematical operations using existing fields. However, you can not use constants in these expressions, it has to be a database field.

The recommended way to do what you are asking is to override the final calculation. The linkednumber template allows you to override the value for any given source, or for the overall total. You can look in the linkednumber template lua file to see more.

Add this tag plus code to the fields you want to perform averaging:


<script>
function onSourceUpdate()
setValue(calculateSources() / 2);
end
</script>


Cheers,
JPG

In-Betweener
June 28th, 2011, 20:56
so would i add that to the section of code on the charsheet_main.xml or to the lua file? I've tried adding it to the end of the section i want to average and it still sums without averaging. I figure i'm just missing something very simple and i apologize for my inexperience.

Moon Wizard
June 28th, 2011, 21:29
No worries. We've all been at the beginning at some point.

It would go between </source> and </speccheck>, in the example you provided.

If that doesn't work, we may need to look at the speccheck template to see what it is doing.

Cheers,
JPG

In-Betweener
June 28th, 2011, 22:18
The worked perfectly. thank you so much for your help. I could use that script at the end of the entry to average without having to go into the lua scripts. thanks again for your help!

In-Betweener
June 29th, 2011, 03:05
is there any way to add the round command to the output result?

joshuha
June 29th, 2011, 03:40
In lua it should be math.floor(setValue(calculateSources() / 2)); to round down or math.ceiling to round up.

In-Betweener
June 29th, 2011, 03:51
I'm using the script from moon_wizard in the charsheet_main.xml to get the average. if i use the math.floor command it returns an error saying 'expected number' so, the entire script so far looks like this:


<draincheck name="astralpool" source="stat.astralp.score">
<anchored>
<to>spellpool</to>
</anchored>
<readonly />
<noreset />
<source>
<name>intelligencecheck</name>
<op>+</op>
</source>
<source>
<name>willpowercheck</name>
<op>+</op>
</source>
<source>
<name>charismacheck</name>
<op>+</op>
</source>
<script>
function onSourceUpdate()
setValue(calculateSources() / 2);
end
</script>

<target>astralpool</target>
<description>
<text>Astral Pool</text>
</description>
</draincheck>

joshuha
June 29th, 2011, 03:54
In lua it should be math.floor(setValue(calculateSources() / 2)); to round down or math.ceiling to round up.

Yeah had that wrong.
setValue(math.floor(calculateSources() / 2))

Moon Wizard
June 29th, 2011, 06:05
Slight reorganization of Joshuha's example.

setValue(math.floor(calculateSources() / 2));

EDIT: J beat me to it.

JPG

In-Betweener
June 29th, 2011, 21:36
thank you guys again so much for the help! you guys are lifesavers