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   DoCommandP(0, PM_PAUSED_ERROR, confirmed ? 0 : 1, CMD_PAUSE);
00129 }
00130 
00143 CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00144 {
00145   switch (p1) {
00146     case PM_PAUSED_SAVELOAD:
00147     case PM_PAUSED_ERROR:
00148     case PM_PAUSED_NORMAL:
00149       break;
00150 
00151 #ifdef ENABLE_NETWORK
00152     case PM_PAUSED_JOIN:
00153     case PM_PAUSED_ACTIVE_CLIENTS:
00154       if (!_networking) return CMD_ERROR;
00155       break;
00156 #endif /* ENABLE_NETWORK */
00157 
00158     default: return CMD_ERROR;
00159   }
00160   if (flags & DC_EXEC) {
00161     if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
00162       ShowQuery(
00163         STR_NEWGRF_UNPAUSE_WARNING_TITLE,
00164         STR_NEWGRF_UNPAUSE_WARNING,
00165         NULL,
00166         AskUnsafeUnpauseCallback
00167       );
00168     } else {
00169 #ifdef ENABLE_NETWORK
00170       PauseMode prev_mode = _pause_mode;
00171 #endif /* ENABLE_NETWORK */
00172 
00173       if (p2 == 0) {
00174         _pause_mode = _pause_mode & ~p1;
00175       } else {
00176         _pause_mode = _pause_mode | p1;
00177       }
00178       InvalidateWindowClassesData(WC_AI_DEBUG, -2);
00179 
00180 #ifdef ENABLE_NETWORK
00181       NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
00182 #endif /* ENABLE_NETWORK */
00183     }
00184 
00185     SetWindowDirty(WC_STATUS_BAR, 0);
00186     SetWindowDirty(WC_MAIN_TOOLBAR, 0);
00187   }
00188   return CommandCost();
00189 }
00190 
00200 CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00201 {
00202   return CommandCost(EXPENSES_OTHER, -(int32)p1);
00203 }
00204 
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   CompanyID dest_company = (CompanyID)p2;
00224 
00225   /* You can only transfer funds that is in excess of your loan */
00226   if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
00227   if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
00228 
00229   if (flags & DC_EXEC) {
00230     /* Add money to company */
00231     Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
00232     SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
00233     cur_company.Restore();
00234   }
00235 
00236   /* Subtract money from local-company */
00237   return amount;
00238 }

Generated on Sun Jun 5 04:19:57 2011 for OpenTTD by  doxygen 1.6.1