Example #1
0
/** Decrease the loan of your company.
 * @param tile unused
 * @param flags operation to perform
 * @param p1 amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
 * @param p2 when 0: pays back LOAN_INTERVAL
 *           when 1: pays back the maximum loan permitting money (press CTRL),
 *           when 2: pays back the amount specified in p1
 * @param text unused
 * @return the cost of this operation or an error
 */
CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
	Company *c = Company::Get(_current_company);

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

	Money loan;
	switch (p2) {
		default: return CMD_ERROR; // Invalid method
		case 0: // Pay back one step
			loan = min(c->current_loan, (Money)LOAN_INTERVAL);
			break;
		case 1: // Pay back as much as possible
			loan = max(min(c->current_loan, c->money), (Money)LOAN_INTERVAL);
			loan -= loan % LOAN_INTERVAL;
			break;
		case 2: // Repay the given amount of loan
			if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
			loan = p1;
			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();
}
Example #2
0
/** Increase the loan of your company.
 * @param tile unused
 * @param flags operation to perform
 * @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
 * @param p2 when 0: loans LOAN_INTERVAL
 *           when 1: loans the maximum loan permitting money (press CTRL),
 *           when 2: loans the amount specified in p1
 * @param text unused
 * @return the cost of this operation or an error
 */
CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
	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 (p2) {
		default: return CMD_ERROR; // Invalid method
		case 0: // Take some extra loan
			loan = LOAN_INTERVAL;
			break;
		case 1: // Take a loan as big as possible
			loan = _economy.max_loan - c->current_loan;
			break;
		case 2: // Take the given amount of loan
			if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
			loan = p1;
			break;
	}

	/* Overflow protection */
	if (c->money + c->current_loan + loan < c->money) return CMD_ERROR;

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

	return CommandCost(EXPENSES_OTHER);
}
/**
 * Deduct costs of a command from the money of a company.
 * @param c Company to pay the bill.
 * @param cost Money to pay.
 */
static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
{
	if (cost.GetCost() == 0) return;
	assert(cost.GetExpensesType() != INVALID_EXPENSES);

	c->money -= cost.GetCost();
	c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();

	if (HasBit(1 << EXPENSES_TRAIN_INC    |
	           1 << EXPENSES_ROADVEH_INC  |
	           1 << EXPENSES_AIRCRAFT_INC |
	           1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
		c->cur_economy.income -= cost.GetCost();
	} else if (HasBit(1 << EXPENSES_TRAIN_RUN    |
	                  1 << EXPENSES_ROADVEH_RUN  |
	                  1 << EXPENSES_AIRCRAFT_RUN |
	                  1 << EXPENSES_SHIP_RUN     |
	                  1 << EXPENSES_PROPERTY     |
	                  1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
		c->cur_economy.expenses -= cost.GetCost();
	}

	InvalidateCompanyWindows(c);
}
Example #4
0
/**
 * Deduct costs of a command from the money of a company.
 * @param c Company to pay the bill.
 * @param cost Money to pay.
 */
static void SubtractMoneyFromAnyCompany(Company *c, CommandCost cost)
{
	if (cost.GetCost() == 0) return;
	assert(cost.GetExpensesType() != INVALID_EXPENSES);

	if (HasBit(1 << EXPENSES_TRAIN_INC    |
	           1 << EXPENSES_ROADVEH_INC  |
	           1 << EXPENSES_AIRCRAFT_INC |
	           1 << EXPENSES_SHIP_INC     |
			   1 << EXPENSES_SHARING_INC, cost.GetExpensesType())) {
		c->cur_economy.income -= cost.GetCost();
	} else if (HasBit(1 << EXPENSES_TRAIN_RUN    |
	                  1 << EXPENSES_ROADVEH_RUN  |
	                  1 << EXPENSES_AIRCRAFT_RUN |
//	                  1 << EXPENSES_SHIP_RUN     |
//	                  1 << EXPENSES_LOST_RUN     |
//	                  1 << EXPENSES_PROPERTY     |
//	                  1 << EXPENSES_LOAN_INT     |
                          1 << EXPENSES_SHIP_RUN , cost.GetExpensesType())) {
               if ((_settings_game.economy.day_length_balance_type == DBT_ALL_COSTS ||
                         _settings_game.economy.day_length_balance_type == DBT_RUN_COST)) {
                       cost.MultiplyCost(_settings_game.economy.day_length_balance_factor);
               }
                c->cur_economy.expenses -= cost.GetCost();
       } else if (HasBit(1 << EXPENSES_LOST_RUN, cost.GetExpensesType())) {
               if (_settings_game.economy.day_length_balance_type == DBT_ALL_COSTS) {
                       cost.AffectCost(_settings_game.economy.day_length_balance_factor);
               }
                c->cur_economy.expenses -= cost.GetCost();
       } else if (HasBit(1 << EXPENSES_SHARING_COST, cost.GetExpensesType())) {
               if (_settings_game.economy.day_length_balance_type == DBT_ALL_COSTS) {
                       cost.AffectCost(_settings_game.economy.day_length_balance_factor);
               }
                c->cur_economy.expenses -= cost.GetCost();
       } else if (HasBit(1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
               if (_settings_game.economy.day_length_balance_type == DBT_ALL_COSTS ||
                  (_settings_game.economy.day_length_balance_type == DBT_RUN_COST &&
                       _settings_game.economy.include_loan_int_to_run)) {
                       cost.AffectCost(_settings_game.economy.day_length_balance_factor);
               }
                c->cur_economy.expenses -= cost.GetCost();
       } else if (HasBit(1 << EXPENSES_PROPERTY, cost.GetExpensesType())) {
               if (_settings_game.economy.day_length_balance_type == DBT_ALL_COSTS ||
                  (_settings_game.economy.day_length_balance_type == DBT_RUN_COST &&
                       _settings_game.economy.include_prop_main_to_run)) {
                       cost.AffectCost(_settings_game.economy.day_length_balance_factor);
               }
               c->cur_economy.expenses -= cost.GetCost();
       } else if (HasBit(1 << EXPENSES_CONSTRUCTION, cost.GetExpensesType())) {
               /* Multiply construction costs according to day length balance type. */
               if (_settings_game.economy.day_length_balance_type == DBT_ALL_COSTS) {
                       cost.AffectCost(_settings_game.economy.day_length_balance_factor);
               }
        }

       /* Subtract money. */
       c->money -= cost.GetCost();
       c->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();

	InvalidateCompanyWindows(c);
}