Welcome to the Builder Academy

Question Write the exchange code?

More
02 Sep 2020 23:08 #9413 by cry1004
In the game I'm making, if you kill a monster, you don't pay money right away and drop bones.
The goal is to randomly change the currency exchange price at a specific time.

The following code calculates the sum of bones and coins and exchanges them.

When exchanging the maximum number of bones and the maximum number of coins, I want to make sure that the number does not overflow, but it seems to be complicated to make, so now I compare the number by subtracting one type from the total.

However, there may be a problem when the rate to be exchanged later changes over time.

For example, when you exchange bones for coins at the current time, if you exchange them for 120%, multiply the number of bones you have by 120%, and the calculated value should not exceed the number of coins you can hold.

If the exchange coin exceeds the maximum number of bones, I would like to inform you that only a few bones can be exchanged.


Code:
SPECIAL(exchange) { int curr_bone, rest_bone; long long int curr_coin; curr_coin = (long long int)GET_BONE(ch)+(long long int)GET_COIN(ch); if (CMD_IS("환전")) { if (GET_COIN(ch) >= MAX_COIN) { send_to_char(ch, "You can't have any more coins.\r\n"); return (TRUE); } if (GET_BONE(ch) <= 0) { send_to_char(ch, "You don't have bones to exchange.\r\n"); return (TRUE); } if (curr_coin == MAX_COIN) { curr_bone = GET_BONE(ch); GET_COIN(ch) = MAX_COIN; GET_BONE(ch) = 0; send_to_char(ch, "%d bones were exchanged for coins.\r\n", curr_bone); return (TRUE); } if (curr_coin > MAX_COIN) { rest_bone = curr_coin - MAX_COIN; curr_bone = GET_BONE(ch) - rest_bone; GET_COIN(ch) = MAX_COIN; GET_BONE(ch) = rest_bone; send_to_char(ch, "%d bones were exchanged for coins.\r\n", curr_bone); return (TRUE); } if (curr_coin < MAX_COIN) { curr_bone = GET_BONE(ch); GET_COIN(ch) = GET_COIN(ch) + GET_BONE(ch); GET_BONE(ch) = 0; send_to_char(ch, "%d bones were exchanged for coins.\r\n", curr_bone); return (TRUE); } } return (FALSE); }

Please Log in or Create an account to join the conversation.

More
03 Sep 2020 20:36 #9423 by thomas
Replied by thomas on topic Write the exchange code?
I think you'll need to do some calculations (this is mailer code, does probably not work, but should get you started):
Code:
// assume global exchange rate: float exchange_rate_bones_to_gold = 1.2f; SPECIAL(exchange) { int curr_bone, rest_bone; long long int curr_coin; long long int max_bones_to_exchange = 0; long long int actual_bones_to_exchange = 0; curr_coin = (long long int)GET_BONE(ch)+(long long int)GET_COIN(ch); if (CMD_IS("환전")) { if (GET_COIN(ch) >= MAX_COIN) { send_to_char(ch, "You can't have any more coins.\r\n"); return (TRUE); } if (GET_BONE(ch) <= 0) { send_to_char(ch, "You don't have bones to exchange.\r\n"); return (TRUE); } max_bones_to_exhange = (long long int)(MAX_COIN - GET_COIN(ch)) / exchange_rate_bones_to_gold); // [1] actual_bones_to_exchange = MAX(MIN(max_bones_to_exchange, GET_BONE(ch)), 0); // [2] if (actual_bones_to_exchange == 0) { send_to_char(ch, "You can't carry that many coins.\r\n"); return (TRUE); } GET_COIN(ch) += (long long int)(exchange_rate_bones_to_gold * actual_bones_to_exchange); // [3] GET_BONE(ch) -= actual_bones_to_exchange; send_to_char(ch, "%d bones were exchanged for coins.\r\n", actual_bones_to_exchange); return (TRUE); } } return (FALSE); }
The interesting lines here:
  1. Here, we figure out how many bones we can exchange in the abosolute max. It is dependant on hw much gold the char has and the exchange rate. This number will almost always be a lot higher than the actual amount of bones you carry, but will work as an upper limit in the next step.
  2. Here, we limit the actual amount we want to exchange to be between 0 and the number of bones currently carried by the char. If it is higher than the maximum allowed size, we limit by that as well.
  3. Here, we use the exchange rate to give the correct amount of coins. Note that we don't need to check if we overflow any values here. It can simply not be too high. Also, we cast to long long int to round off any decimals.
One thing that strikes me is that you're going to have an absolutely insane amount of bones and coin in your game. An ordinary long int has this domain: -9223372036854775808 to 9223372036854775807. That is almost 10e+18. You can have multiple-Quintillionaires just with that. For long long int all bets are off - the compiler just guarantees that it is at least 64 bits wide (same as long).
The following user(s) said Thank You: cry1004

Please Log in or Create an account to join the conversation.

Time to create page: 0.490 seconds