Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "command_func.h"
00014 #include "economy_func.h"
00015 #include "window_func.h"
00016 #include "textbuf_gui.h"
00017 #include "network/network.h"
00018 #include "network/network_func.h"
00019 #include "strings_func.h"
00020 #include "company_func.h"
00021 #include "company_gui.h"
00022 #include "company_base.h"
00023 #include "core/backup_type.hpp"
00024
00025 #include "table/strings.h"
00026
00038 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00039 {
00040 Company *c = Company::Get(_current_company);
00041
00042 if (c->current_loan >= _economy.max_loan) {
00043 SetDParam(0, _economy.max_loan);
00044 return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
00045 }
00046
00047 Money loan;
00048 switch (p2) {
00049 default: return CMD_ERROR;
00050 case 0:
00051 loan = LOAN_INTERVAL;
00052 break;
00053 case 1:
00054 loan = _economy.max_loan - c->current_loan;
00055 break;
00056 case 2:
00057 if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
00058 loan = p1;
00059 break;
00060 }
00061
00062
00063 if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
00064
00065 if (flags & DC_EXEC) {
00066 c->money += loan;
00067 c->current_loan += loan;
00068 InvalidateCompanyWindows(c);
00069 }
00070
00071 return CommandCost(EXPENSES_OTHER);
00072 }
00073
00085 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00086 {
00087 Company *c = Company::Get(_current_company);
00088
00089 if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
00090
00091 Money loan;
00092 switch (p2) {
00093 default: return CMD_ERROR;
00094 case 0:
00095 loan = min(c->current_loan, (Money)LOAN_INTERVAL);
00096 break;
00097 case 1:
00098 loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
00099 loan -= loan % LOAN_INTERVAL;
00100 break;
00101 case 2:
00102 if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR;
00103 loan = p1;
00104 break;
00105 }
00106
00107 if (c->money < loan) {
00108 SetDParam(0, loan);
00109 return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
00110 }
00111
00112 if (flags & DC_EXEC) {
00113 c->money -= loan;
00114 c->current_loan -= loan;
00115 InvalidateCompanyWindows(c);
00116 }
00117 return CommandCost();
00118 }
00119
00126 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
00127 {
00128 if (confirmed) {
00129 DoCommandP(0, PM_PAUSED_ERROR, 0, CMD_PAUSE);
00130 }
00131 }
00132
00145 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00146 {
00147 switch (p1) {
00148 case PM_PAUSED_SAVELOAD:
00149 case PM_PAUSED_ERROR:
00150 case PM_PAUSED_NORMAL:
00151 break;
00152
00153 #ifdef ENABLE_NETWORK
00154 case PM_PAUSED_JOIN:
00155 case PM_PAUSED_ACTIVE_CLIENTS:
00156 if (!_networking) return CMD_ERROR;
00157 break;
00158 #endif
00159
00160 default: return CMD_ERROR;
00161 }
00162 if (flags & DC_EXEC) {
00163 if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
00164 ShowQuery(
00165 STR_NEWGRF_UNPAUSE_WARNING_TITLE,
00166 STR_NEWGRF_UNPAUSE_WARNING,
00167 NULL,
00168 AskUnsafeUnpauseCallback
00169 );
00170 } else {
00171 #ifdef ENABLE_NETWORK
00172 PauseMode prev_mode = _pause_mode;
00173 #endif
00174
00175 if (p2 == 0) {
00176 _pause_mode = _pause_mode & ~p1;
00177 } else {
00178 _pause_mode = _pause_mode | p1;
00179 }
00180 InvalidateWindowClassesData(WC_AI_DEBUG, -2);
00181
00182 #ifdef ENABLE_NETWORK
00183 NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
00184 #endif
00185 }
00186
00187 SetWindowDirty(WC_STATUS_BAR, 0);
00188 SetWindowDirty(WC_MAIN_TOOLBAR, 0);
00189 }
00190 return CommandCost();
00191 }
00192
00202 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00203 {
00204 return CommandCost(EXPENSES_OTHER, -(int32)p1);
00205 }
00206
00219 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00220 {
00221 if (!_settings_game.economy.give_money) return CMD_ERROR;
00222
00223 const Company *c = Company::Get(_current_company);
00224 CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
00225 CompanyID dest_company = (CompanyID)p2;
00226
00227
00228 if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
00229 if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
00230
00231 if (flags & DC_EXEC) {
00232
00233 Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
00234 SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
00235 cur_company.Restore();
00236 }
00237
00238
00239 return amount;
00240 }