diff options
author | Patric Stout <truebrain@openttd.org> | 2021-01-05 18:06:48 +0100 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-01-05 21:56:24 +0100 |
commit | 62cdadb58207b28dfdf1ab4ceb46278c2561f079 (patch) | |
tree | d6e8308cf106daf372ad673d87169f075d3c3a86 /src/company_cmd.cpp | |
parent | d6e15d4943936a89d275354ee59d0e3b7c6a2018 (diff) | |
download | openttd-62cdadb58207b28dfdf1ab4ceb46278c2561f079.tar.xz |
Change: move "give money" from client-list to company window
This is a much better location for this button, as you send
money from one company to another company, not from player
to player.
This is based on work done by JGRPP in:
https://github.com/JGRennison/OpenTTD-patches/commit/f82054339124cc6b89c5f4f9dac2d9da62f0108b
and surrounding commits, which took the work from estys:
https://www.tt-forums.net/viewtopic.php?p=1183311#p1183311
We did modify it to fix several bugs and clean up the code while
here anyway.
The callback was removed, as it meant a modified client could
prevent anyone from seeing money was transfered. The message
is now generated in the command itself, making that impossible.
Diffstat (limited to 'src/company_cmd.cpp')
-rw-r--r-- | src/company_cmd.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp index c871b64c9..1ecdaf89a 100644 --- a/src/company_cmd.cpp +++ b/src/company_cmd.cpp @@ -11,6 +11,7 @@ #include "company_base.h" #include "company_func.h" #include "company_gui.h" +#include "core/backup_type.hpp" #include "town.h" #include "news_func.h" #include "cmd_helper.h" @@ -1179,3 +1180,50 @@ uint32 CompanyInfrastructure::GetTramTotal() const } return total; } + +/** + * Transfer funds (money) from one company to another. + * To prevent abuse in multiplayer games you can only send money to other + * companies if you have paid off your loan (either explicitly, or implicitly + * given the fact that you have more money than loan). + * @param tile unused + * @param flags operation to perform + * @param p1 the amount of money to transfer; max 20.000.000 + * @param p2 the company to transfer the money to + * @param text unused + * @return the cost of this operation or an error + */ +CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) +{ + if (!_settings_game.economy.give_money) return CMD_ERROR; + + const Company *c = Company::Get(_current_company); + CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL)); + CompanyID dest_company = (CompanyID)p2; + + /* You can only transfer funds that is in excess of your loan */ + if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return_cmd_error(STR_ERROR_INSUFFICIENT_FUNDS); + if (!Company::IsValidID(dest_company)) return CMD_ERROR; + + if (flags & DC_EXEC) { + /* Add money to company */ + Backup<CompanyID> cur_company(_current_company, dest_company, FILE_LINE); + SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost())); + cur_company.Restore(); + + if (_networking) { + char dest_company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH]; + SetDParam(0, dest_company); + GetString(dest_company_name, STR_COMPANY_NAME, lastof(dest_company_name)); + + char from_company_name[MAX_LENGTH_COMPANY_NAME_CHARS * MAX_CHAR_LENGTH]; + SetDParam(0, _current_company); + GetString(from_company_name, STR_COMPANY_NAME, lastof(from_company_name)); + + NetworkTextMessage(NETWORK_ACTION_GIVE_MONEY, GetDrawStringCompanyColour(_current_company), false, from_company_name, dest_company_name, amount.GetCost()); + } + } + + /* Subtract money from local-company */ + return amount; +} |