PDA

View Full Version : XML math problem on custom charsheet...



Zakarius
July 19th, 2007, 04:13
In my campaign, half of your armor bonus (rounded up) goes to Armor Class and the other half goes to Damage Reduction. In the charsheet_combat.xml, I've modified the script as follows:


<script>
function onSourceUpdate()
setValue(10 + calculateSources());
end

function onSourceValue(source, sourcename)
if sourcename == "abilities.dexterity.bonus" then
local dex = source.getValue();
local max = sources["encumbrance.armormaxdexbonus"].getValue();
local maxactive = sources["encumbrance.armormaxdexbonusactive"].getValue();
if maxactive == 0 or max > dex then
return dex;
else
return max;
end
else
if sourcename == "ac.sources.armor" then
local armor = source.getValue();
return trunc((armor + 1)/2);
else
return super.onSourceValue(source, sourcename);
end
end
end
</script>


I'm not even sure if the 'trunc' is correct, but even removing it has strange results. The total AC values differ when starting FG up from scratch and after the armor bonus values are modified. If the <op>+</op> is removed from the armor bonus source, no changes are made to AC. If it's left there, it simply adds the value.

Any ideas where I'm going wrong?

Zakarius

joshuha
July 19th, 2007, 05:10
One problem and it does make for unpredictable results is you are using a > sign with inline scripting. This is a big no-no as its an XML tag and probably truncating your code in the internal parser. I bet if you either put it in a file or use the escape &GT; it will help.

As far as trunc are you trying to get just the integer part? If so math.floor() is the function you are looking for.

There's a note about this on https://fantasygrounds.com/modguide/scripting.xcp but it is not emphasised enough in my opinion.

Zakarius
July 19th, 2007, 06:12
One problem and it does make for unpredictable results is you are using a > sign with inline scripting. This is a big no-no as its an XML tag and probably truncating your code in the internal parser. I bet if you either put it in a file or use the escape &GT; it will help.


Actually, that code with the 'greater than' sign is packaged with the original FG II and seems to work fine. The code I added begins with:

if sourcename == "ac.sources.armor" then


As far as trunc are you trying to get just the integer part? If so math.floor() is the function you are looking for.

There's a note about this on https://fantasygrounds.com/modguide/scripting.xcp but it is not emphasised enough in my opinion.

Thanks a lot... it worked like a charm!

Zakarius

Foen
July 19th, 2007, 06:32
Instead of math.floor((armor + 1)/2) the formula math.ceil(armor/2) is slightly neater. Math.ceil rounds up.

Stuart
(Foen)

Dachannien
July 19th, 2007, 08:16
You can probably get away with > because it isn't ambiguous about whether it's part of a tag or not. You can't do it with <, and in fact, a < in an inline script doesn't occur anywhere in the default ruleset. It always occurs as &lt;.

Zakarius
July 19th, 2007, 15:23
Thanks to your feedback, I've got my code working... kinda. When I change the Armor Bonus to 4, it immediately adds FOUR to the total AC though it's only supposed to add TWO because of the ruleset I'm using. If I close the character sheet and open it again, voila--it has added the proper +2 into the total AC (if you're curious, the other 2 points go to DR).

I've tried this in both host and client mode... same result. I've posted the relevant code for the AC tally below if anyone cares to sift through it... (or have other people experienced similar trouble with the normal d20 ruleset):


<linkednumber name="ac" source="ac.totals.general">
<anchored>
<to>acframe</to>
<position>insidetopleft</position>
<offset>20,25</offset>
<size>
<width>61</width>
<height>32</height>
</size>
</anchored>
<frame>
<name>acicon</name>
<offset>4,16,0,24</offset>
</frame>
<font>sheetnumber</font>
<readonly />
<description>
<text>Armor Class</text>
</description>
<source>
<name>abilities.dexterity.bonus</name>
<op>+</op>
</source>
<source>
<name>ac.sources.armor</name>
<op>+</op>
</source>
<source>
<name>ac.sources.shield</name>
<op>+</op>
</source>
<source>
<name>attackbonus.grapple.size</name>
<op>+</op>
</source>
<source>
<name>attackbonus.base</name>
<op>+</op>
</source>
<source>
<name>ac.sources.naturalarmor</name>
<op>+</op>
</source>
<source>
<name>ac.sources.deflection</name>
<op>+</op>
</source>
<source>
<name>ac.sources.misc</name>
<op>+</op>
</source>
<source>
<name>ac.sources.temp.ac</name>
<op>+</op>
</source>
<source>
<name>encumbrance.armormaxdexbonus</name>
</source>
<source>
<name>encumbrance.armormaxdexbonusactive</name>
</source>
<script>
function onSourceUpdate()
setValue(10 + calculateSources());
end

function onSourceValue(source, sourcename)
if sourcename == "abilities.dexterity.bonus" then
local dex = source.getValue();
local max = sources["encumbrance.armormaxdexbonus"].getValue();
local maxactive = sources["encumbrance.armormaxdexbonusactive"].getValue();

if maxactive == 0 or max > dex then
return dex;
else
return max;
end
else
if sourcename == "ac.sources.armor" then
local armor = source.getValue();
return math.floor((armor + 1)/2);
else
if sourcename == "ac.sources.naturalarmor" then
local na = source.getValue();
return na-(math.floor(na/5));
else
if sourcename == "attackbonus.base" then
local bab = source.getValue();
return math.floor(bab/2);
else
return super.onSourceValue(source, sourcename);
end
end
end
end
end
</script>
</linkednumber>

Zakarius
July 19th, 2007, 15:49
By removing all of the else .. if nesting, the code now works. I incorrectly assumed that if statements needed to check one after another, but the eventual return super.onSourceValue(source, sourcename) needn't be connected to the conditionals which all have their own return clause.

Zakarius