News:

Simutrans Sites
Know our official sites. Find tools and resources for Simutrans.

[Script api] Increase and decrease the bank

Started by Yona-TYT, December 12, 2024, 12:57:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Yona-TYT

The scenario I'm working on will have a special fee and commission system for players, so I need a simple way to increase/decrease the bank (money).

I made this little experiment api_player.cc:

call_tool_init player_book_account(player_t *player, sint32 delta)
{
    // build param string (see tool_change_player_t)
    cbuffer_t buf;
    buf.printf( "$,%i,%i", player->get_player_nr(), delta);
    return call_tool_init(TOOL_CHANGE_PLAYER | SIMPLE_TOOL, buf, 0, welt->get_public_player());
}

call_tool_init player_plus_cash(player_t *player, sint32 delta)
{
    return player_book_account(player, player->get_account_balance_as_double() + delta);
}

call_tool_init player_minus_cash(player_t *player, sint32 delta)
{
    return player_book_account(player, player->get_account_balance_as_double() - delta);
}
    if (scenario) {
        /**
         * Change bank account of player by given amount @p delta.
         * @param delta
         * @ingroup scen_only
         */
        register_method(vm, player_book_account, "book_cash", true);

        /**
         * Increase bank account of player by given amount @p delta.
         * @param delta
         * @ingroup scen_only
         */
        register_method(vm, player_plus_cash, "plus_cash", true);

        /**
         * Decrease bank account of player by given amount @p delta.
         * @param delta
         * @ingroup scen_only
         */
        register_method(vm, player_minus_cash, "minus_cash", true);
    }

prissi

I think the scenario test has such a function.
pl.book_cash(200000 * 100 + 1)It can handle positive and negative amounts already. So why a new function?

Yona-TYT

Quote from: prissi on December 12, 2024, 05:15:58 AMI think the scenario test has such a function.
pl.book_cash(200000 * 100 + 1)It can handle positive and negative amounts already. So why a new function?
That replaces the total amount in the bank, so I would have to get the player's current money, do the addition or subtraction operation, and then send the results to "book_cash" to be replaced.

So I thought it would be a good idea to create specific functions for increment and decrement operations, I was inspired by a Java class that uses plus and minus to do something similar but with dates.

prissi

No, the book function is additive.

And even then, there is get_player_stat() and get_cash() where zou could querz the player money. The AI could hardly work without query the player's own money.