2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file misc_cmd.cpp Some misc functions that are better fitted in other files, but never got moved there... */
|
2007-03-03 04:04:22 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
|
|
|
#include "economy_func.h"
|
2007-12-19 20:45:46 +00:00
|
|
|
#include "window_func.h"
|
|
|
|
#include "textbuf_gui.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network.h"
|
2009-11-12 20:33:30 +00:00
|
|
|
#include "network/network_func.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_gui.h"
|
2010-01-05 17:11:56 +00:00
|
|
|
#include "company_base.h"
|
2010-05-31 20:22:57 +00:00
|
|
|
#include "core/backup_type.hpp"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Increase the loan of your company.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2009-01-12 17:11:45 +00:00
|
|
|
* @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
|
2007-06-14 15:24:34 +00:00
|
|
|
* @param p2 when 0: loans LOAN_INTERVAL
|
|
|
|
* when 1: loans the maximum loan permitting money (press CTRL),
|
2009-01-12 17:11:45 +00:00
|
|
|
* when 2: loans the amount specified in p1
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
Company *c = Company::Get(_current_company);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->current_loan >= _economy.max_loan) {
|
2007-06-21 17:25:17 +00:00
|
|
|
SetDParam(0, _economy.max_loan);
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
Money loan;
|
2007-06-14 15:24:34 +00:00
|
|
|
switch (p2) {
|
|
|
|
default: return CMD_ERROR; // Invalid method
|
|
|
|
case 0: // Take some extra loan
|
2009-01-12 17:11:45 +00:00
|
|
|
loan = LOAN_INTERVAL;
|
2007-06-14 15:24:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // Take a loan as big as possible
|
2008-09-30 20:39:50 +00:00
|
|
|
loan = _economy.max_loan - c->current_loan;
|
2007-06-14 15:24:34 +00:00
|
|
|
break;
|
2009-01-12 17:11:45 +00:00
|
|
|
case 2: // Take the given amount of loan
|
2010-03-20 15:30:57 +00:00
|
|
|
if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
|
2009-01-12 17:11:45 +00:00
|
|
|
loan = p1;
|
|
|
|
break;
|
2007-06-14 15:24:34 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-19 00:05:26 +00:00
|
|
|
/* Overflow protection */
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;
|
2007-06-19 00:05:26 +00:00
|
|
|
|
2007-06-14 15:24:34 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->money += loan;
|
|
|
|
c->current_loan += loan;
|
|
|
|
InvalidateCompanyWindows(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
return CommandCost(EXPENSES_OTHER);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Decrease the loan of your company.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2009-01-12 17:11:45 +00:00
|
|
|
* @param p1 amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
|
2007-06-14 15:24:34 +00:00
|
|
|
* @param p2 when 0: pays back LOAN_INTERVAL
|
|
|
|
* when 1: pays back the maximum loan permitting money (press CTRL),
|
2009-01-12 17:11:45 +00:00
|
|
|
* when 2: pays back the amount specified in p1
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
Company *c = Company::Get(_current_company);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2007-06-19 00:05:26 +00:00
|
|
|
Money loan;
|
2007-06-14 15:24:34 +00:00
|
|
|
switch (p2) {
|
|
|
|
default: return CMD_ERROR; // Invalid method
|
|
|
|
case 0: // Pay back one step
|
2009-01-12 17:11:45 +00:00
|
|
|
loan = min(c->current_loan, (Money)LOAN_INTERVAL);
|
2007-06-14 15:24:34 +00:00
|
|
|
break;
|
|
|
|
case 1: // Pay back as much as possible
|
2008-09-30 20:39:50 +00:00
|
|
|
loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
|
2007-06-14 15:24:34 +00:00
|
|
|
loan -= loan % LOAN_INTERVAL;
|
|
|
|
break;
|
2009-01-12 17:11:45 +00:00
|
|
|
case 2: // Repay the given amount of loan
|
2010-04-17 14:57:49 +00:00
|
|
|
if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
|
2009-01-12 17:11:45 +00:00
|
|
|
loan = p1;
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
if (c->money < loan) {
|
2007-06-21 17:25:17 +00:00
|
|
|
SetDParam(0, loan);
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
c->money -= loan;
|
|
|
|
c->current_loan -= loan;
|
|
|
|
InvalidateCompanyWindows(c);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:50:07 +00:00
|
|
|
/**
|
|
|
|
* In case of an unsafe unpause, we want the
|
|
|
|
* user to confirm that it might crash.
|
|
|
|
* @param w unused
|
|
|
|
* @param confirmed whether the user confirms his/her action
|
|
|
|
*/
|
|
|
|
static void AskUnsafeUnpauseCallback(Window *w, bool confirmed)
|
|
|
|
{
|
2009-05-06 15:06:57 +00:00
|
|
|
DoCommandP(0, PM_PAUSED_ERROR, confirmed ? 0 : 1, CMD_PAUSE);
|
2007-12-04 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2009-11-12 20:33:30 +00:00
|
|
|
/**
|
|
|
|
* Pause/Unpause the game (server-only).
|
|
|
|
* Set or unset a bit in the pause mode. If pause mode is zero the game is
|
|
|
|
* unpaused. A bitset is used instead of a boolean value/counter to have
|
|
|
|
* more control over the game when saving/loading, etc.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2009-05-06 15:06:57 +00:00
|
|
|
* @param p1 the pause mode to change
|
|
|
|
* @param p2 1 pauses, 0 unpauses this mode
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdPause(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-06 15:06:57 +00:00
|
|
|
switch (p1) {
|
|
|
|
case PM_PAUSED_SAVELOAD:
|
|
|
|
case PM_PAUSED_ERROR:
|
|
|
|
case PM_PAUSED_NORMAL:
|
|
|
|
break;
|
2007-12-04 22:50:07 +00:00
|
|
|
|
2009-11-12 17:44:49 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
case PM_PAUSED_JOIN:
|
2009-11-12 17:46:04 +00:00
|
|
|
case PM_PAUSED_ACTIVE_CLIENTS:
|
2009-11-12 17:44:49 +00:00
|
|
|
if (!_networking) return CMD_ERROR;
|
|
|
|
break;
|
2009-11-12 20:33:30 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2009-11-12 17:44:49 +00:00
|
|
|
|
2009-05-06 15:06:57 +00:00
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (p1 == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
|
|
|
|
ShowQuery(
|
|
|
|
STR_NEWGRF_UNPAUSE_WARNING_TITLE,
|
|
|
|
STR_NEWGRF_UNPAUSE_WARNING,
|
|
|
|
NULL,
|
|
|
|
AskUnsafeUnpauseCallback
|
|
|
|
);
|
|
|
|
} else {
|
2009-11-12 20:33:30 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
PauseMode prev_mode = _pause_mode;
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2009-05-06 15:06:57 +00:00
|
|
|
if (p2 == 0) {
|
|
|
|
_pause_mode = _pause_mode & ~p1;
|
|
|
|
} else {
|
|
|
|
_pause_mode = _pause_mode | p1;
|
|
|
|
}
|
2010-04-02 17:35:20 +00:00
|
|
|
InvalidateWindowClassesData(WC_AI_DEBUG, -2);
|
2009-11-12 20:33:30 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
NetworkHandlePauseChange(prev_mode, (PauseMode)p1);
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2007-12-04 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_STATUS_BAR, 0);
|
|
|
|
SetWindowDirty(WC_MAIN_TOOLBAR, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Change the financial flow of your company.
|
2005-05-12 23:46:01 +00:00
|
|
|
* This is normally only enabled in offline mode, but if there is a debug
|
|
|
|
* build, you can cheat (to test).
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-12 23:46:01 +00:00
|
|
|
* @param p1 the amount of money to receive (if negative), or spend (if positive)
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 23:46:01 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdMoneyCheat(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-12 23:46:01 +00:00
|
|
|
#ifndef _DEBUG
|
|
|
|
if (_networking) return CMD_ERROR;
|
|
|
|
#endif
|
2008-03-22 10:50:50 +00:00
|
|
|
return CommandCost(EXPENSES_OTHER, -(int32)p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Transfer funds (money) from one company to another.
|
2006-08-28 18:53:03 +00:00
|
|
|
* To prevent abuse in multiplayer games you can only send money to other
|
2008-09-30 20:39:50 +00:00
|
|
|
* companies if you have paid off your loan (either explicitely, or implicitely
|
2005-05-13 17:09:05 +00:00
|
|
|
* given the fact that you have more money than loan).
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-13 17:09:05 +00:00
|
|
|
* @param p1 the amount of money to transfer; max 20.000.000
|
2008-09-30 20:39:50 +00:00
|
|
|
* @param p2 the company to transfer the money to
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 23:46:01 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdGiveMoney(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-12-04 17:54:56 +00:00
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (!_settings_game.economy.give_money) return CMD_ERROR;
|
2007-09-30 17:38:42 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Company *c = Company::Get(_current_company);
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost amount(EXPENSES_OTHER, min((Money)p1, (Money)20000000LL));
|
2010-06-05 11:51:34 +00:00
|
|
|
CompanyID dest_company = (CompanyID)p2;
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-05-13 17:09:05 +00:00
|
|
|
/* You can only transfer funds that is in excess of your loan */
|
2010-04-20 18:02:08 +00:00
|
|
|
if (c->money - c->current_loan < amount.GetCost() || amount.GetCost() < 0) return CMD_ERROR;
|
2010-06-05 11:51:34 +00:00
|
|
|
if (!_networking || !Company::IsValidID(dest_company)) return CMD_ERROR;
|
2004-12-28 09:24:02 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Add money to company */
|
2010-06-05 12:16:12 +00:00
|
|
|
Backup<CompanyByte> cur_company(_current_company, dest_company, FILE_LINE);
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -amount.GetCost()));
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
/* Subtract money from local-company */
|
2005-05-13 17:09:05 +00:00
|
|
|
return amount;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|