summaryrefslogtreecommitdiff
path: root/src/company_cmd.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-09-01 12:57:04 +0000
committerrubidium <rubidium@openttd.org>2009-09-01 12:57:04 +0000
commit4c84db16361c17e6c3def857b400b9a974f90d44 (patch)
tree57d6185c490b3a04178e78f6bbcca714ac1b3824 /src/company_cmd.cpp
parent4abf4602e7a0b624de9836b5a58d3d606612c2fa (diff)
downloadopenttd-4c84db16361c17e6c3def857b400b9a974f90d44.tar.xz
(svn r17345) -Fix [FS#2769]: one wasn't offered to take over bankrupt companies anymore; caused by the introduction NoAI, although NewAI had the same problem too.
Diffstat (limited to 'src/company_cmd.cpp')
-rw-r--r--src/company_cmd.cpp69
1 files changed, 67 insertions, 2 deletions
diff --git a/src/company_cmd.cpp b/src/company_cmd.cpp
index f1ae42c70..f47072562 100644
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -492,13 +492,78 @@ void InitializeCompanies()
_cur_company_tick_index = 0;
}
+/**
+ * Handle the bankruptcy take over of a company.
+ * Companies going bankrupt will ask the other companies in order of their
+ * performance rating, so better performing companies get the 'do you want to
+ * merge with Y' question earlier. The question will then stay till either the
+ * company has gone bankrupt or got merged with a company.
+ *
+ * @param c the company that is going bankrupt.
+ */
+static void HandleBankruptcyTakeover(Company *c)
+{
+ /* Amount of time out for each company to take over a company;
+ * Timeout is a quarter (3 months of 30 days) divided over the
+ * number of companies. The minimum number of days in a quarter
+ * is 90: 31 in January, 28 in February and 31 in March.
+ * Note that the company going bankrupt can't buy itself. */
+ static const int TAKE_OVER_TIMEOUT = 3 * 30 * DAY_TICKS / (MAX_COMPANIES - 1);
+
+ assert(c->bankrupt_asked != 0);
+
+ /* We're currently asking some company to buy 'us' */
+ if (c->bankrupt_timeout != 0) {
+ c->bankrupt_timeout -= MAX_COMPANIES;
+ if (c->bankrupt_timeout > 0) return;
+ c->bankrupt_timeout = 0;
+
+ return;
+ }
+
+ /* Did we ask everyone for bankruptcy? If so, bail out. */
+ if (c->bankrupt_asked == MAX_UVALUE(CompanyMask)) return;
+
+ Company *c2, *best = NULL;
+ int32 best_performance = -1;
+
+ /* Ask the company with the highest performance history first */
+ FOR_ALL_COMPANIES(c2) {
+ if (c2->bankrupt_asked == 0 && // Don't ask companies going bankrupt themselves
+ !HasBit(c->bankrupt_asked, c2->index) &&
+ best_performance < c2->old_economy[1].performance_history) {
+ best_performance = c2->old_economy[1].performance_history;
+ best = c2;
+ }
+ }
+
+ /* Asked all companies? */
+ if (best_performance == -1) {
+ c->bankrupt_asked = MAX_UVALUE(CompanyMask);
+ return;
+ }
+
+ SetBit(c->bankrupt_asked, best->index);
+
+ if (IsInteractiveCompany(best->index)) {
+ c->bankrupt_timeout = TAKE_OVER_TIMEOUT;
+ ShowBuyCompanyDialog(c->index);
+ return;
+ }
+
+ if (best->is_ai) {
+ AI::NewEvent(best->index, new AIEventCompanyAskMerger(c->index, ClampToI32(c->bankrupt_value)));
+ }
+}
+
void OnTick_Companies()
{
if (_game_mode == GM_EDITOR) return;
Company *c = Company::GetIfValid(_cur_company_tick_index);
- if (c != NULL && c->name_1 != 0) {
- GenerateCompanyName(c);
+ if (c != NULL) {
+ if (c->name_1 != 0) GenerateCompanyName(c);
+ if (c->bankrupt_asked != 0) HandleBankruptcyTakeover(c);
}
if (_next_competitor_start == 0) {