Files @ r28520:f9aebe299cae
Branch filter:

Location: cpp/openttd-patchpack/source/src/misc_cmd.cpp - annotation

Patric Stout
Codechange: MacOS already has MIN/MAX macros defined

This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6201:2e76eb9a1d7a
r5584:545d748cc681
r8116:df67d3c5e4fd
r8116:df67d3c5e4fd
r8107:82461791b7a2
r8107:82461791b7a2
r5584:545d748cc681
r13521:8a785d107381
r8114:866ed489ed98
r10208:ef8fcc3dc4ca
r10208:ef8fcc3dc4ca
r14174:6999d5f69ce1
r24687:4a2fa1e7f559
r24687:4a2fa1e7f559
r15269:8b901e9e0694
r26094:7572e88decb3
r5584:545d748cc681
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r15610:623a23fb6560
r15610:623a23fb6560
r26095:00c14d52e378
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r13057:58af81fcdcf8
r5584:545d748cc681
r26120:15c912177c88
r5584:545d748cc681
r11917:612c11f7ab47
r5584:545d748cc681
r10207:a1fc2f2a33db
r7002:201ff6832d3a
r11725:57bc99fdc1bc
r5584:545d748cc681
r5584:545d748cc681
r6953:acd98f6a2bb8
r26120:15c912177c88
r6909:8484192a9653
r26120:15c912177c88
r10696:8dfe83e30d01
r6909:8484192a9653
r26120:15c912177c88
r10207:a1fc2f2a33db
r6909:8484192a9653
r26120:15c912177c88
r26120:15c912177c88
r24536:c6eb2a05f37b
r10696:8dfe83e30d01
r6909:8484192a9653
r6909:8484192a9653
r26770:bdeec98568f0
r26770:bdeec98568f0
r26770:bdeec98568f0
r26770:bdeec98568f0
r6957:88dc5fc7fccd
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r8230:33d57fce0ec2
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r15610:623a23fb6560
r26095:00c14d52e378
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r13057:58af81fcdcf8
r5584:545d748cc681
r26120:15c912177c88
r5584:545d748cc681
r11917:612c11f7ab47
r5584:545d748cc681
r11725:57bc99fdc1bc
r5584:545d748cc681
r6957:88dc5fc7fccd
r26120:15c912177c88
r6909:8484192a9653
r26120:15c912177c88
r24597:afde5721a3b6
r6909:8484192a9653
r26120:15c912177c88
r24597:afde5721a3b6
r6909:8484192a9653
r6909:8484192a9653
r26120:15c912177c88
r26120:15c912177c88
r24536:c6eb2a05f37b
r10696:8dfe83e30d01
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r7002:201ff6832d3a
r11725:57bc99fdc1bc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r10207:a1fc2f2a33db
r5584:545d748cc681
r6950:1a54b1afb12a
r5584:545d748cc681
r5584:545d748cc681
r8013:0e71243088a7
r8013:0e71243088a7
r8013:0e71243088a7
r25484:c5af4712cb7f
r8013:0e71243088a7
r27942:f7389062d120
r8013:0e71243088a7
r17820:911c0dc851f5
r26120:15c912177c88
r17820:911c0dc851f5
r8013:0e71243088a7
r8013:0e71243088a7
r13521:8a785d107381
r13521:8a785d107381
r13521:8a785d107381
r13521:8a785d107381
r13521:8a785d107381
r26095:00c14d52e378
r26120:15c912177c88
r26120:15c912177c88
r13057:58af81fcdcf8
r5584:545d748cc681
r26120:15c912177c88
r5584:545d748cc681
r26120:15c912177c88
r11847:235c011992a8
r11847:235c011992a8
r11847:235c011992a8
r18760:babf79514cd5
r24483:c8962d3f47a6
r11847:235c011992a8
r8013:0e71243088a7
r13519:c2a8bbec9a15
r13520:b28b2da3bc7b
r13519:c2a8bbec9a15
r13519:c2a8bbec9a15
r13519:c2a8bbec9a15
r11847:235c011992a8
r11847:235c011992a8
r11847:235c011992a8
r26120:15c912177c88
r11847:235c011992a8
r11847:235c011992a8
r11847:235c011992a8
r23607:36c15679007d
r11847:235c011992a8
r11847:235c011992a8
r11847:235c011992a8
r13521:8a785d107381
r13521:8a785d107381
r26120:15c912177c88
r26120:15c912177c88
r11847:235c011992a8
r26120:15c912177c88
r27177:0168210eb854
r27177:0168210eb854
r27177:0168210eb854
r27177:0168210eb854
r27177:0168210eb854
r11847:235c011992a8
r13521:8a785d107381
r26120:15c912177c88
r8013:0e71243088a7
r8013:0e71243088a7
r13024:48c81d0b078a
r13024:48c81d0b078a
r5584:545d748cc681
r6950:1a54b1afb12a
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r15610:623a23fb6560
r26120:15c912177c88
r13057:58af81fcdcf8
r5584:545d748cc681
r27942:f7389062d120
r5584:545d748cc681
r26120:15c912177c88
r5584:545d748cc681
r5584:545d748cc681
r15610:623a23fb6560
r20745:b1ebcc10c74a
r26095:00c14d52e378
r24687:4a2fa1e7f559
r26120:15c912177c88
r26120:15c912177c88
r26120:15c912177c88
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r26120:15c912177c88
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r23674:a5bafa1a02c5
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r24687:4a2fa1e7f559
r24687:4a2fa1e7f559
r24687:4a2fa1e7f559
r24687:4a2fa1e7f559
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
r23838:bfeaabaa7b1d
r26948:8bceb871d3e5
r20745:b1ebcc10c74a
r20745:b1ebcc10c74a
/*
 * 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/>.
 */

/** @file misc_cmd.cpp Some misc functions that are better fitted in other files, but never got moved there... */

#include "stdafx.h"
#include "command_func.h"
#include "economy_func.h"
#include "window_func.h"
#include "textbuf_gui.h"
#include "network/network.h"
#include "network/network_func.h"
#include "strings_func.h"
#include "company_func.h"
#include "company_gui.h"
#include "company_base.h"
#include "tile_map.h"
#include "texteff.hpp"
#include "core/backup_type.hpp"
#include "misc_cmd.h"

#include "table/strings.h"

#include "safeguards.h"

/**
 * Increase the loan of your company.
 * @param flags operation to perform
 * @param cmd when LoanCommand::Interval: loans LOAN_INTERVAL,
 *            when LoanCommand::Max: loans the maximum loan permitting money (press CTRL),
 *            when LoanCommand::Amount: loans the amount specified in \c amount
 * @param amount amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when cmd == LoanCommand::Amount.
 * @return the cost of this operation or an error
 */
CommandCost CmdIncreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
{
	Company *c = Company::Get(_current_company);

	if (c->current_loan >= _economy.max_loan) {
		SetDParam(0, _economy.max_loan);
		return_cmd_error(STR_ERROR_MAXIMUM_PERMITTED_LOAN);
	}

	Money loan;
	switch (cmd) {
		default: return CMD_ERROR; // Invalid method
		case LoanCommand::Interval: // Take some extra loan
			loan = LOAN_INTERVAL;
			break;
		case LoanCommand::Max: // Take a loan as big as possible
			loan = _economy.max_loan - c->current_loan;
			break;
		case LoanCommand::Amount: // Take the given amount of loan
			loan = amount;
			if (loan < LOAN_INTERVAL || c->current_loan + loan > _economy.max_loan || loan % LOAN_INTERVAL != 0) return CMD_ERROR;
			break;
	}

	/* In case adding the loan triggers the overflow protection of Money,
	 * we would essentially be losing money as taking and repaying the loan
	 * immediately would not get us back to the same bank balance anymore. */
	if (c->money > Money::max() - loan) return CMD_ERROR;

	if (flags & DC_EXEC) {
		c->money        += loan;
		c->current_loan += loan;
		InvalidateCompanyWindows(c);
	}

	return CommandCost(EXPENSES_OTHER);
}

/**
 * Decrease the loan of your company.
 * @param flags operation to perform
 * @param cmd when LoanCommand::Interval: pays back LOAN_INTERVAL,
 *            when LoanCommand::Max: pays back the maximum loan permitting money (press CTRL),
 *            when LoanCommand::Amount: pays back the amount specified in \c amount
 * @param amount amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when cmd == LoanCommand::Amount.
 * @return the cost of this operation or an error
 */
CommandCost CmdDecreaseLoan(DoCommandFlag flags, LoanCommand cmd, Money amount)
{
	Company *c = Company::Get(_current_company);

	if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);

	Money loan;
	switch (cmd) {
		default: return CMD_ERROR; // Invalid method
		case LoanCommand::Interval: // Pay back one step
			loan = std::min(c->current_loan, (Money)LOAN_INTERVAL);
			break;
		case LoanCommand::Max: // Pay back as much as possible
			loan = std::max(std::min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
			loan -= loan % LOAN_INTERVAL;
			break;
		case LoanCommand::Amount: // Repay the given amount of loan
			loan = amount;
			if (loan % LOAN_INTERVAL != 0 || loan < LOAN_INTERVAL || loan > c->current_loan) return CMD_ERROR; // Invalid amount to loan
			break;
	}

	if (c->money < loan) {
		SetDParam(0, loan);
		return_cmd_error(STR_ERROR_CURRENCY_REQUIRED);
	}

	if (flags & DC_EXEC) {
		c->money        -= loan;
		c->current_loan -= loan;
		InvalidateCompanyWindows(c);
	}
	return CommandCost();
}

/**
 * In case of an unsafe unpause, we want the
 * user to confirm that it might crash.
 * @param confirmed whether the user confirmed their action
 */
static void AskUnsafeUnpauseCallback(Window *, bool confirmed)
{
	if (confirmed) {
		Command<CMD_PAUSE>::Post(PM_PAUSED_ERROR, false);
	}
}

/**
 * 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.
 * @param flags operation to perform
 * @param mode the pause mode to change
 * @param pause true pauses, false unpauses this mode
 * @return the cost of this operation or an error
 */
CommandCost CmdPause(DoCommandFlag flags, PauseMode mode, bool pause)
{
	switch (mode) {
		case PM_PAUSED_SAVELOAD:
		case PM_PAUSED_ERROR:
		case PM_PAUSED_NORMAL:
		case PM_PAUSED_GAME_SCRIPT:
		case PM_PAUSED_LINK_GRAPH:
			break;

		case PM_PAUSED_JOIN:
		case PM_PAUSED_ACTIVE_CLIENTS:
			if (!_networking) return CMD_ERROR;
			break;

		default: return CMD_ERROR;
	}
	if (flags & DC_EXEC) {
		if (mode == PM_PAUSED_NORMAL && _pause_mode & PM_PAUSED_ERROR) {
			ShowQuery(
				STR_NEWGRF_UNPAUSE_WARNING_TITLE,
				STR_NEWGRF_UNPAUSE_WARNING,
				nullptr,
				AskUnsafeUnpauseCallback
			);
		} else {
			PauseMode prev_mode = _pause_mode;

			if (pause) {
				_pause_mode |= mode;
			} else {
				_pause_mode &= ~mode;

				/* If the only remaining reason to be paused is that we saw a command during pause, unpause. */
				if (_pause_mode == PM_COMMAND_DURING_PAUSE) {
					_pause_mode = PM_UNPAUSED;
				}
			}

			NetworkHandlePauseChange(prev_mode, mode);
		}

		SetWindowDirty(WC_STATUS_BAR, 0);
		SetWindowDirty(WC_MAIN_TOOLBAR, 0);
	}
	return CommandCost();
}

/**
 * Change the financial flow of your company.
 * @param amount the amount of money to receive (if positive), or spend (if negative)
 * @return the cost of this operation or an error
 */
CommandCost CmdMoneyCheat(DoCommandFlag, Money amount)
{
	return CommandCost(EXPENSES_OTHER, -amount);
}

/**
 * Change the bank bank balance of a company by inserting or removing money without affecting the loan.
 * @param flags operation to perform
 * @param tile tile to show text effect on (if not 0)
 * @param delta the amount of money to receive (if positive), or spend (if negative)
 * @param company the company ID.
 * @param expenses_type the expenses type which should register the cost/income @see ExpensesType.
 * @return zero cost or an error
 */
CommandCost CmdChangeBankBalance(DoCommandFlag flags, TileIndex tile, Money delta, CompanyID company, ExpensesType expenses_type)
{
	if (!Company::IsValidID(company)) return CMD_ERROR;
	if (expenses_type >= EXPENSES_END) return CMD_ERROR;
	if (_current_company != OWNER_DEITY) return CMD_ERROR;

	if (flags & DC_EXEC) {
		/* Change company bank balance of company. */
		Backup<CompanyID> cur_company(_current_company, company, FILE_LINE);
		SubtractMoneyFromCompany(CommandCost(expenses_type, -delta));
		cur_company.Restore();

		if (tile != 0) {
			ShowCostOrIncomeAnimation(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTilePixelZ(tile), -delta);
		}
	}

	/* This command doesn't cost anything for deity. */
	CommandCost zero_cost(expenses_type, (Money)0);
	return zero_cost;
}