Math/logic, transfer vars

Posted by Zeno on Sat 28 Jul 2007 06:41 PM — 6 posts, 21,517 views.

USA #0
This is somewhat complicated, so I'll try my best without having to explain everything.

A player has two variables, var1 and var2. There is a command that transfers from var1 to var2. The player picks the amount to transfer, and it is done over time. If they transfer 100% of var, it takes 10 rounds. 50% is 5 rounds, etc.

Since it takes time, obviously we need to store some temp variables. I'm looking to do this by using the least amount of variables.

Currently, I'm using two temp variables (var3 which stores the amount left to transfer, and var4 which stores how long it'll take. But these don't work correctly. The transfer will sometimes end but it hasn't transferred the correct amount (it cut short).

Would there be a better var to store instead of var3 and var4 so that this problem would be fixed?
USA #1
ok, so Mr(s). Player has X of something and chooses to transfer some or all of that to Y which will take T rounds based on how large a percentage of X (s)he wants to transfer to Y but during those ensuing T rounds (s)he may gain additional X so your storing the quantity of X to be transferred in Z and each round transferring some of 10% of X to Y by subtracting it from the player's X and Z, adding it to their Y and subtracting one from T, but it's not always transferring all of Z to Y before T runs out?

Would it be better to store their full X as Z when the transfer begins and then each round reduce T by one, reduce X by Z*0.1 and increase Y by Z*0.1 so that when T=0 the transfer is complete regardless and then you can just reinitalize Z?

Or have I just blown this all out of algebraic proportions on you? ;)
Amended on Sat 28 Jul 2007 07:03 PM by Conner
USA #2
Well, yes a player will transfer an amount (they do not choose a percent though) from X to Y which takes T rounds. I'm not sure what Z is. var3 and var4 are temp variables used solely to accomplish the transfer.

I don't think we could reduce X by Z*0.01 either, because what if they were only transferring 1? (Not the percent)
Australia Forum Administrator #3
I think I would store the amount left to transfer, and the amount to transfer each time. I assume you are doing it at a fixed interval. Based on the player input you would set how much to transfer per tick (or whatever the interval is). The total amount starts "full" initially.

Then each tick, you transfer more of the amount left over, until it runs out, with a possible half transfer at the end.

Something like this:


amount_to_transfer = amount_per_tick;  // tick amount
if (amount_to_transfer > amount_left)  // don't do more than max
  amount_to_transfer = amount_left;
amount_left -= amount_to_transfer;  // less next time
var1 -= amount_to_transfer;   // move from var1 to var2
var2 += amount_to_transfer;
if (amount_left <= 0)
  {
  // done
  }
Amended on Sat 28 Jul 2007 09:26 PM by Nick Gammon
USA #4
Sorry Zeno, my Z variable was the other temp variable besides number of rounds, but I was operating under the impression they got to choose only the percentage of X to be transferred in incriments of 10%.
USA #5
Thanks Nick, that worked out fine.