# misc_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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; // Invalid method
    00050     case 0: // Take some extra loan
    00051       loan = LOAN_INTERVAL;
    00052       break;
    00053     case 1: // Take a loan as big as possible
    00054       loan = _economy.max_loan - c->current_loan;
    00055       break;
    00056     case 2: // Take the given amount of loan
    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   /* Overflow protection */
    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; // Invalid method
    00094     case 0: // Pay back one step
    00095       loan = min(c->current_loan, (Money)LOAN_INTERVAL);
    00096       break;
    00097     case 1: // Pay back as much as possible
    00098       loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
    00099       loan -= loan % LOAN_INTERVAL;
    00100       break;
    00101     case 2: // Repay the given amount of loan
    00102       if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
    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     case PM_PAUSED_GAME_SCRIPT:
    00152       break;
    00153
    00154 #ifdef ENABLE_NETWORK
    00155     case PM_PAUSED_JOIN:
    00156     case PM_PAUSED_ACTIVE_CLIENTS:
    00157       if (!_networking) return CMD_ERROR;
    00158       break;
    00159 #endif /* ENABLE_NETWORK */
    00160
    00161     default: return CMD_ERROR;
    00162   }
    00163   if (flags & DC_EXEC) {
    00164     if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
    00165       ShowQuery(
    00166         STR_NEWGRF_UNPAUSE_WARNING_TITLE,
    00167         STR_NEWGRF_UNPAUSE_WARNING,
    00168         NULL,
    00169         AskUnsafeUnpauseCallback
    00170       );
    00171     } else {
    00172 #ifdef ENABLE_NETWORK
    00173       PauseMode prev_mode = _pause_mode;
    00174 #endif /* ENABLE_NETWORK */
    00175
    00176       if (p2 == 0) {
    00177         _pause_mode = _pause_mode & ~p1;
    00178       } else {
    00179         _pause_mode = _pause_mode | p1;
    00180       }
    00181       InvalidateWindowClassesData(WC_AI_DEBUG, -2);
    00182
    00183 #ifdef ENABLE_NETWORK
    00184       NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
    00185 #endif /* ENABLE_NETWORK */
    00186     }
    00187
    00188     SetWindowDirty(WC_STATUS_BAR, 0);
    00189     SetWindowDirty(WC_MAIN_TOOLBAR, 0);
    00190   }
    00191   return CommandCost();
    00192 }
    00193
    00203 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00204 {
    00205   return CommandCost(EXPENSES_OTHER, -(int32)p1);
    00206 }
    00207
    00220 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00221 {
    00222   if (!_settings_game.economy.give_money) return CMD_ERROR;
    00223
    00224   const Company *c = Company::Get(_current_company);
    00225   CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
    00226   CompanyID dest_company = (CompanyID)p2;
    00227
    00228   /* You can only transfer funds that is in excess of your loan */
    00229   if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
    00230   if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
    00231
    00232   if (flags & DC_EXEC) {
    00233     /* Add money to company */
    00234     Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
    00235     SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
    00236     cur_company.Restore();
    00237   }
    00238
    00239   /* Subtract money from local-company */
    00240   return amount;
    00241 }