Need help with a somewhat complex math equation in Java/Acroscript

Madmaxneo

Explorer
This is for Adobe Acrobat which uses an enhanced form of Javascript, but I believe Javascript wil work so if you have a Javascript solution please post it.
I am developing an auto-calculating character sheet in Acrobat Pro XI, so far it is coming along great. Except for one (currently) minor roadblock. In this game it is possible to raise your stats every level through the use of this table:
Stat rangeCost per point
1-901
91-952
96-1003
101-10510
<tbody> </tbody>
Basically if you have a stat of 75 and want to raise it 5 points then it will cost you 5 DP's (development points). If you have a stat of 89 and want to raise it 2 points it will cost you 3 points, 1 to raise it to a 90 then 2 to raise it to a 91, etc. I have a simple script that works as long as you don't raise it more than through one set at a time, i.e. if you have a 94 and try to raise it to a 96 it will not calculate the cost correctly. Here is my script for those that can understand it:

Code:
var stat = this.getField("St.0").valuevar value = 0


if (stat > 100) {
  value = (((this.getField("NewStat1").value) - stat) *10) 
}
else if (stat > 95) {
   value = (((this.getField("NewStat1").value) - stat) *3)
} else if (stat > 90) {
  value = (((this.getField("NewStat1").value) - stat) *2)  
}  else {
  value = ((this.getField("NewStat1").value) - stat)
}


event.value = value
Where St.0 is one of the 8 stats, in this case it is Strength (in the script above it becomes "stat" from the "var stat" part of the script) and NewStat1 is the what the new stat will be once the points are added.
The problem is, as I described above, if you have a 94 and try to raise it to a 96 it will not calculate the cost correctly. As a 94 to a 95 costs 2 points but from a 95 to a 96 costs 3 points the total should be a 5 but in the script above it comes out to a 6. Can anyone please help me figure this out?

Buce
 

log in or register to remove this ad

delericho

Legend
I would suggest that the simplest way is simply to calculate the total value of both the old and the new stats, and then simply subtract the old from the new:

Code:
var oldStat = this.getField("St.0").value
var newStat = this.getField("NewStat1").value
var oldVal = 0
var newVal = 0

// Calculate the value of the old stat
// Stats up to 90 cost 1 point each
oldVal = oldStat

if(oldStat > 90)
{
  /* Stats from 91 - 95 cost two each - but note that we've
  ** already added one above!
  */
  oldVal = oldVal + (oldStat - 90)
}
if(oldStat > 95)
{
  /* Stats from 96 - 100 cost three each - but note that we've
  ** already added two above!
  */
  oldVal = oldVal + (oldStat - 95)
}
if(oldStat > 100)
{
  /* Stats above 100 cost ten each - but note that we've
  ** already added three above!
  */
  oldVal = oldVal + ((oldStat - 100) * 7)
}

// Repeat for the new stat
// Stats up to 90 cost 1 point each
newVal = newStat

if(newStat > 90)
{
  /* Stats from 91 - 95 cost two each - but note that we've
  ** already added one above!
  */
  newVal = newVal + (newStat - 90)
}
if(newStat > 95)
{
  /* Stats from 96 - 100 cost three each - but note that we've
  ** already added two above!
  */
  newVal = newVal + (newStat - 95)
}
if(newStat > 100)
{
  /* Stats above 100 cost ten each - but note that we've
  ** already added three above!
  */
  newVal = newVal + ((newStat - 100) * 7)
}

event.value = (newVal - oldVal)

There are almost certainly cleverer ways to do this, but this should do the job and is hopefully easy enough to understand.
 

Madmaxneo

Explorer
Looks good though I'm a little confused about the multiplying (*) by 7 you have in there. Edit: Ok, I see it now..... Hmmm......
I'm also having trouble getting this to work. Is this in Javascript or Acroscript?
If I run it as is I get a missing syntax error (right after the "var newVal = 0"). Which consequently is the same error I was getting when I tried having two "var" statements at the beginning like you have. Nothing I tried would correct that error so I switched it to the "getfield" script above. I don't think the "getfield" script would work correctly in yours. Any ideas?

Bruce
 
Last edited:

delericho

Legend
I'm also having trouble getting this to work. Is this in Javascript or Acroscript?

I just copied your formatting, so it may not be 100% correct in either. :) Sorry.

If I run it as is I get a missing syntax error (right after the "var newVal = 0"). Which consequently is the same error I was getting when I tried having two "var" statements at the beginning like you have.

Any chance you could give the full text of the error message? That might give a clue as to just what's wrong. Failing that, I don't have anything immediate to suggest. When I get time, I'll try it out on a form at home to see if I can find the problem, but it may be a few days before I get a chance.
 


delericho

Legend
Ah. Try sticking a ; at the end of each statement. So

var oldStat = this.getField("St.0").value

becomes

var oldStat = this.getField("St.0").value;

and so on.

I'm not entirely clear on the rules for exactly when acroscript does and does not require that semi-colon - they don't seem to be quite as consistent as I'm used to. But that appears to be the problem.
 

Madmaxneo

Explorer
Ok, It took me some time but I corrected the script above to work, here is the Acroscript version...:
Code:
var oldStat = this.getField("St.0").value 
var newStat = this.getField("NewStat1").value 
var oldVal = 0 
var newVal = 0 
oldVal = oldStat 
if(oldStat > 90) {oldVal = oldVal + (oldStat - 90) } 
else if(oldStat > 95) { oldVal = oldVal + (oldStat - 95) } 
else if(oldStat > 100) {oldVal = oldVal + ((oldStat - 100) * 7) }  

newVal = newStat 
if(newStat > 90) {newVal = newVal + (newStat - 90) } 
else if(newStat > 95) {newVal = newVal + (newStat - 95) } 
else if(newStat > 100) {newVal = newVal + ((newStat - 100) * 7) } 
event.value = (newVal - oldVal)
/

In Acrobat you have to use "else if" statements when using more than 1 "if". Regardless it still does not work. I set a stat at 94 and then put the points into it to raise it two points to a 96 which should come to a total cost of a 5 (2 for raising it to a 95 then 3 to raise it again to a 96). But it calculated the cost as a 4. Consequently that is the same problem I had in my script above.
Upon further investigation, I changed the stat to a 95 then increased it by 1 (the 2 and finally 5) and it still calculates the cost by doubling the number. Which is incorrect, it should be tripling the number. I will mess with it some more and see if I can find anything else.

Bruce
 

Madmaxneo

Explorer
If there is no way to get this to work in a math equation then I could always try it in an array, though I am not so sure I could figure out how to get it to pull the cost for each point given......

Another thought is to do a counter where each click and it automatically adds one to the new stat and recalculates the cost....hmmm I will have to see if I can do that in adobe. I do know a way I can do this.....hmmmm
 

delericho

Legend
In Acrobat you have to use "else if" statements when using more than 1 "if".

Using "else if" changes the logic quite significantly, which is why you're getting the wrong results. Try the following:

Code:
var oldStat = this.getField("St.0").value
var oldVal = 0

if(oldStat <= 90)
{
  oldVal = oldStat
}
else if(oldStat > 90)
{
  oldVal = 90 + ((oldStat - 90) * 2)
}
else if(oldStat > 95)
{
  oldVal = 100 + ((oldStat - 95) * 3)
} 
else if(oldStat > 100)
{
  oldVal = 115 + ((oldStat - 100) * 10)
}

Note that I've only done the oldStat/oldVal pair here; you'll need to duplicate this for newStat/newVal.
 

Madmaxneo

Explorer
Awesome!
It's "almost" working. Right now it will calculate the numbers correctly up to a 95 for the NewStat1, but it stays at a cost of 2 when I go above a 95 (it still goes up for each point I add but it only multiplies by 2). It even does that if I start at a 95 or even a 100 in the St.0 (oldStat) field, it never goes to a cost of "3" or a "10". I can't figure out why, the script all makes sense and should add up correctly. Here is the final script:

Code:
var oldStat = this.getField("St.0").value var newStat = this.getField("NewStat1").value 
var oldVal = 0 
var newVal = 0 
if(oldStat <= 90) { oldVal = oldStat } 
else if(oldStat > 90) { oldVal = 90 + ((oldStat - 90) * 2) } 
else if(oldStat > 95) { oldVal = 100 + ((oldStat - 95) * 3) } 
else if(oldStat > 100) { oldVal = 115 + ((oldStat - 100) * 10) }


if(newStat <= 90) { newVal = newStat } 
else if(newStat > 90) { newVal = 90 + ((newStat - 90) * 2) } 
else if(newStat > 95) { newVal = 100 + ((newStat - 95) * 3) } 
else if(newStat > 100) { newVal = 115 + ((newStat - 100) * 10) }
event.value = (newVal - oldVal)
 

Remove ads

Top