# misc_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: misc_cmd.cpp 19665 2010-04-17 22:27:49Z rubidium $ */
    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 "functions.h"
    00021 #include "company_func.h"
    00022 #include "company_gui.h"
    00023 #include "company_base.h"
    00024
    00025 #include "table/strings.h"
    00026
    00037 CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00038 {
    00039   Company *c = Company::Get(_current_company);
    00040
    00041   if (c->current_loan >= _economy.max_loan) {
    00042     SetDParam(0, _economy.max_loan);
    00043     return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
    00044   }
    00045
    00046   Money loan;
    00047   switch (p2) {
    00048     default: return CMD_ERROR; // Invalid method
    00049     case 0: // Take some extra loan
    00050       loan = LOAN_INTERVAL;
    00051       break;
    00052     case 1: // Take a loan as big as possible
    00053       loan = _economy.max_loan - c->current_loan;
    00054       break;
    00055     case 2: // Take the given amount of loan
    00056       if ((((int32)p1 < LOAN_INTERVAL) || c->current_loan + (int32)p1 > _economy.max_loan || (p1 % LOAN_INTERVAL) != 0)) return CMD_ERROR;
    00057       loan = p1;
    00058       break;
    00059   }
    00060
    00061   /* Overflow protection */
    00062   if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
    00063
    00064   if (flags & DC_EXEC) {
    00065     c->money        += loan;
    00066     c->current_loan += loan;
    00067     InvalidateCompanyWindows(c);
    00068   }
    00069
    00070   return CommandCost(EXPENSES_OTHER);
    00071 }
    00072
    00083 CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00084 {
    00085   Company *c = Company::Get(_current_company);
    00086
    00087   if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
    00088
    00089   Money loan;
    00090   switch (p2) {
    00091     default: return CMD_ERROR; // Invalid method
    00092     case 0: // Pay back one step
    00093       loan = min(c->current_loan, (Money)LOAN_INTERVAL);
    00094       break;
    00095     case 1: // Pay back as much as possible
    00096       loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
    00097       loan -= loan % LOAN_INTERVAL;
    00098       break;
    00099     case 2: // Repay the given amount of loan
    00100       if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
    00101       loan = p1;
    00102       break;
    00103   }
    00104
    00105   if (c->money < loan) {
    00106     SetDParam(0, loan);
    00107     return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
    00108   }
    00109
    00110   if (flags & DC_EXEC) {
    00111     c->money        -= loan;
    00112     c->current_loan -= loan;
    00113     InvalidateCompanyWindows(c);
    00114   }
    00115   return CommandCost();
    00116 }
    00117
    00124 static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
    00125 {
    00126   DoCommandP(0, PM_PAUSED_ERROR, confirmed ? 0 : 1, CMD_PAUSE);
    00127 }
    00128
    00141 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00142 {
    00143   switch (p1) {
    00144     case PM_PAUSED_SAVELOAD:
    00145     case PM_PAUSED_ERROR:
    00146     case PM_PAUSED_NORMAL:
    00147       break;
    00148
    00149 #ifdef ENABLE_NETWORK
    00150     case PM_PAUSED_JOIN:
    00151     case PM_PAUSED_ACTIVE_CLIENTS:
    00152       if (!_networking) return CMD_ERROR;
    00153       break;
    00154 #endif /* ENABLE_NETWORK */
    00155
    00156     default: return CMD_ERROR;
    00157   }
    00158   if (flags & DC_EXEC) {
    00159     if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
    00160       ShowQuery(
    00161         STR_NEWGRF_UNPAUSE_WARNING_TITLE,
    00162         STR_NEWGRF_UNPAUSE_WARNING,
    00163         NULL,
    00164         AskUnsafeUnpauseCallback
    00165       );
    00166     } else {
    00167 #ifdef ENABLE_NETWORK
    00168       PauseMode prev_mode = _pause_mode;
    00169 #endif /* ENABLE_NETWORK */
    00170
    00171       if (p2 == 0) {
    00172         _pause_mode = _pause_mode & ~p1;
    00173       } else {
    00174         _pause_mode = _pause_mode | p1;
    00175       }
    00176
    00177 #ifdef ENABLE_NETWORK
    00178       NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
    00179 #endif /* ENABLE_NETWORK */
    00180     }
    00181
    00182     SetWindowDirty(WC_STATUS_BAR, 0);
    00183     SetWindowDirty(WC_MAIN_TOOLBAR, 0);
    00184   }
    00185   return CommandCost();
    00186 }
    00187
    00198 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00199 {
    00200 #ifndef _DEBUG
    00201   if (_networking) return CMD_ERROR;
    00202 #endif
    00203   return CommandCost(EXPENSES_OTHER, -(int32)p1);
    00204 }
    00205
    00217 CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00218 {
    00219   if (!_settings_game.economy.give_money) return CMD_ERROR;
    00220
    00221   const Company *c = Company::Get(_current_company);
    00222   CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
    00223
    00224   /* You can only transfer funds that is in excess of your loan */
    00225   if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() <= 0) return CMD_ERROR;
    00226   if (!_networking || !Company::IsValidID((CompanyID)p2)) return CMD_ERROR;
    00227
    00228   if (flags & DC_EXEC) {
    00229     /* Add money to company */
    00230     CompanyID old_company = _current_company;
    00231     _current_company = (CompanyID)p2;
    00232     SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
    00233     _current_company = old_company;
    00234   }
    00235
    00236   /* Subtract money from local-company */
    00237   return amount;
    00238 }