src/economy.cpp
branchnoai
changeset 10645 8cbdb511a674
parent 10513 33cb70ff2f5d
child 10715 6bdf79ffb022
equal deleted inserted replaced
10644:6c4314786d68 10645:8cbdb511a674
    27 #include "newgrf_industries.h"
    27 #include "newgrf_industries.h"
    28 #include "newgrf_industrytiles.h"
    28 #include "newgrf_industrytiles.h"
    29 #include "newgrf_station.h"
    29 #include "newgrf_station.h"
    30 #include "unmovable.h"
    30 #include "unmovable.h"
    31 #include "cargotype.h"
    31 #include "cargotype.h"
    32 #include "player_face.h"
       
    33 #include "group.h"
    32 #include "group.h"
    34 #include "strings_func.h"
    33 #include "strings_func.h"
    35 #include "tile_cmd.h"
    34 #include "tile_cmd.h"
    36 #include "functions.h"
    35 #include "functions.h"
    37 #include "window_func.h"
    36 #include "window_func.h"
   526 
   525 
   527 	owner = p->index;
   526 	owner = p->index;
   528 
   527 
   529 	switch (p->quarters_of_bankrupcy) {
   528 	switch (p->quarters_of_bankrupcy) {
   530 		case 2:
   529 		case 2:
   531 			AddNewsItem((StringID)(owner | NB_BTROUBLE),
   530 			SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
   532 				NM_CALLBACK, NF_NONE, NT_COMPANY_INFO, DNC_BANKRUPCY, 0, 0);
   531 			SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
       
   532 			SetDParam(2, owner);
       
   533 			AddNewsItem(STR_02B6, NS_COMPANY_TROUBLE, 0, owner);
   533 			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
   534 			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
   534 				AI_Event(i, new AIEventCompanyInTrouble(owner));
   535 				AI_Event(i, new AIEventCompanyInTrouble(owner));
   535 			}
   536 			}
   536 			break;
   537 			break;
   537 		case 3: {
   538 		case 3: {
   538 			/* XXX - In multiplayer, should we ask other players if it wants to take
   539 			/* XXX - In multiplayer, should we ask other players if it wants to take
   539 		          over when it is a human company? -- TrueLight */
   540 		          over when it is a human company? -- TrueLight */
   540 			if (IsHumanPlayer(owner)) {
   541 			if (IsHumanPlayer(owner)) {
   541 				AddNewsItem((StringID)(owner | NB_BTROUBLE),
   542 				SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
   542 					NM_CALLBACK, NF_NONE, NT_COMPANY_INFO, DNC_BANKRUPCY, 0, 0);
   543 				SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
       
   544 				SetDParam(2, owner);
       
   545 				AddNewsItem(STR_02B6, NS_COMPANY_TROUBLE, 0, owner);
   543 				break;
   546 				break;
   544 			}
   547 			}
   545 
   548 
   546 			/* Check if the company has any value.. if not, declare it bankrupt
   549 			/* Check if the company has any value.. if not, declare it bankrupt
   547 			 *  right now */
   550 			 *  right now */
   557 		case 4: {
   560 		case 4: {
   558 			/* Close everything the owner has open */
   561 			/* Close everything the owner has open */
   559 			DeletePlayerWindows(owner);
   562 			DeletePlayerWindows(owner);
   560 
   563 
   561 			/* Show bankrupt news */
   564 			/* Show bankrupt news */
   562 			SetDParam(0, p->index);
   565 			SetDParam(0, STR_705C_BANKRUPT);
   563 			AddNewsItem((StringID)(owner | NB_BBANKRUPT), NM_CALLBACK, NF_NONE, NT_COMPANY_INFO, DNC_BANKRUPCY, 0, 0);
   566 			SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
       
   567 			SetDParam(2, p->index);
       
   568 			AddNewsItem(STR_02B6, NS_COMPANY_BANKRUPT, 0, owner);
   564 
   569 
   565 			if (IsHumanPlayer(owner)) {
   570 			if (IsHumanPlayer(owner)) {
   566 				/* XXX - If we are in offline mode, leave the player playing. Eg. there
   571 				/* XXX - If we are in offline mode, leave the player playing. Eg. there
   567 				 * is no THE-END, otherwise mark the player as spectator to make sure
   572 				 * is no THE-END, otherwise mark the player as spectator to make sure
   568 				 * he/she is no long in control of this company */
   573 				 * he/she is no long in control of this company */
   586 			 * it might get confused. */
   591 			 * it might get confused. */
   587 			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
   592 			for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
   588 				AI_Event(i, new AIEventCompanyBankrupt(owner));
   593 				AI_Event(i, new AIEventCompanyBankrupt(owner));
   589 			}
   594 			}
   590 		}
   595 		}
   591 	}
       
   592 }
       
   593 
       
   594 void DrawNewsBankrupcy(Window *w, const NewsItem *ni)
       
   595 {
       
   596 	DrawNewsBorder(w);
       
   597 
       
   598 	Player *p = GetPlayer((PlayerID)GB(ni->string_id, 0, 4));
       
   599 	DrawPlayerFace(p->face, p->player_color, 2, 23);
       
   600 	GfxFillRect(3, 23, 3 + 91, 23 + 118, PALETTE_TO_STRUCT_GREY | (1 << USE_COLORTABLE));
       
   601 
       
   602 	SetDParam(0, p->index);
       
   603 
       
   604 	DrawStringMultiCenter(49, 148, STR_7058_PRESIDENT, 94);
       
   605 
       
   606 	switch (ni->string_id & 0xF0) {
       
   607 	case NB_BTROUBLE:
       
   608 		DrawStringCentered(w->width >> 1, 1, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE, TC_FROMSTRING);
       
   609 
       
   610 		SetDParam(0, p->index);
       
   611 
       
   612 		DrawStringMultiCenter(
       
   613 			((w->width - 101) >> 1) + 98,
       
   614 			90,
       
   615 			STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED,
       
   616 			w->width - 101);
       
   617 		break;
       
   618 
       
   619 	case NB_BMERGER:
       
   620 		DrawStringCentered(w->width >> 1, 1, STR_7059_TRANSPORT_COMPANY_MERGER, TC_FROMSTRING);
       
   621 		SetDParam(0, ni->params[0]);
       
   622 		SetDParam(1, p->index);
       
   623 		SetDParam(2, ni->params[1]);
       
   624 		DrawStringMultiCenter(
       
   625 			((w->width - 101) >> 1) + 98,
       
   626 			90,
       
   627 			ni->params[1] == 0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR,
       
   628 			w->width - 101);
       
   629 		break;
       
   630 
       
   631 	case NB_BBANKRUPT:
       
   632 		DrawStringCentered(w->width >> 1, 1, STR_705C_BANKRUPT, TC_FROMSTRING);
       
   633 		SetDParam(0, ni->params[0]);
       
   634 		DrawStringMultiCenter(
       
   635 			((w->width - 101) >> 1) + 98,
       
   636 			90,
       
   637 			STR_705D_HAS_BEEN_CLOSED_DOWN_BY,
       
   638 			w->width - 101);
       
   639 		break;
       
   640 
       
   641 	case NB_BNEWCOMPANY:
       
   642 		DrawStringCentered(w->width >> 1, 1, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED, TC_FROMSTRING);
       
   643 		SetDParam(0, p->index);
       
   644 		SetDParam(1, ni->params[0]);
       
   645 		DrawStringMultiCenter(
       
   646 			((w->width - 101) >> 1) + 98,
       
   647 			90,
       
   648 			STR_705F_STARTS_CONSTRUCTION_NEAR,
       
   649 			w->width - 101);
       
   650 		break;
       
   651 
       
   652 	default:
       
   653 		NOT_REACHED();
       
   654 	}
       
   655 }
       
   656 
       
   657 StringID GetNewsStringBankrupcy(const NewsItem *ni)
       
   658 {
       
   659 	const Player *p = GetPlayer((PlayerID)GB(ni->string_id, 0, 4));
       
   660 
       
   661 	switch (ni->string_id & 0xF0) {
       
   662 	case NB_BTROUBLE:
       
   663 		SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
       
   664 		SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
       
   665 		SetDParam(2, p->index);
       
   666 		return STR_02B6;
       
   667 	case NB_BMERGER:
       
   668 		SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
       
   669 		SetDParam(1, ni->params[1] == 0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR);
       
   670 		SetDParam(2, ni->params[0]);
       
   671 		SetDParam(3, p->index);
       
   672 		SetDParam(4, ni->params[1]);
       
   673 		return STR_02B6;
       
   674 	case NB_BBANKRUPT:
       
   675 		SetDParam(0, STR_705C_BANKRUPT);
       
   676 		SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
       
   677 		SetDParam(2, ni->params[0]);
       
   678 		return STR_02B6;
       
   679 	case NB_BNEWCOMPANY:
       
   680 		SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
       
   681 		SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
       
   682 		SetDParam(2, p->index);
       
   683 		SetDParam(3, ni->params[0]);
       
   684 		return STR_02B6;
       
   685 	default:
       
   686 		NOT_REACHED();
       
   687 	}
   596 	}
   688 }
   597 }
   689 
   598 
   690 static void PlayersGenStatistics()
   599 static void PlayersGenStatistics()
   691 {
   600 {
   806 {
   715 {
   807 	if (_opt.diff.economy == 0) return;
   716 	if (_opt.diff.economy == 0) return;
   808 
   717 
   809 	if (--_economy.fluct == 0) {
   718 	if (--_economy.fluct == 0) {
   810 		_economy.fluct = -(int)GB(Random(), 0, 2);
   719 		_economy.fluct = -(int)GB(Random(), 0, 2);
   811 		AddNewsItem(STR_7073_WORLD_RECESSION_FINANCIAL, NM_NORMAL, NF_NONE, NT_ECONOMY, DNC_NONE, 0, 0);
   720 		AddNewsItem(STR_7073_WORLD_RECESSION_FINANCIAL, NS_ECONOMY, 0, 0);
   812 	} else if (_economy.fluct == -12) {
   721 	} else if (_economy.fluct == -12) {
   813 		_economy.fluct = GB(Random(), 0, 8) + 312;
   722 		_economy.fluct = GB(Random(), 0, 8) + 312;
   814 		AddNewsItem(STR_7074_RECESSION_OVER_UPTURN_IN, NM_NORMAL, NF_NONE, NT_ECONOMY, DNC_NONE, 0, 0);
   723 		AddNewsItem(STR_7074_RECESSION_OVER_UPTURN_IN, NS_ECONOMY, 0, 0);
   815 	}
   724 	}
   816 }
   725 }
   817 
   726 
   818 static byte _price_category[NUM_PRICES] = {
   727 static byte _price_category[NUM_PRICES] = {
   819 	0, 2, 2, 2, 2, 2, 2, 2,
   728 	0, 2, 2, 2, 2, 2, 2, 2,
  1149 	for (s = _subsidies; s != endof(_subsidies); s++) {
  1058 	for (s = _subsidies; s != endof(_subsidies); s++) {
  1150 		if (s->cargo_type == CT_INVALID) continue;
  1059 		if (s->cargo_type == CT_INVALID) continue;
  1151 
  1060 
  1152 		if (s->age == 12-1) {
  1061 		if (s->age == 12-1) {
  1153 			pair = SetupSubsidyDecodeParam(s, 1);
  1062 			pair = SetupSubsidyDecodeParam(s, 1);
  1154 			AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NM_NORMAL, NF_TILE | NF_TILE2, NT_SUBSIDIES, DNC_NONE, pair.a, pair.b);
  1063 			AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, pair.a, pair.b);
  1155 			s->cargo_type = CT_INVALID;
  1064 			s->cargo_type = CT_INVALID;
  1156 			modified = true;
  1065 			modified = true;
  1157 			for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1066 			for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1158 				AI_Event(i, new AIEventSubsidyOfferExpired(s - _subsidies));
  1067 				AI_Event(i, new AIEventSubsidyOfferExpired(s - _subsidies));
  1159 			}
  1068 			}
  1160 		} else if (s->age == 2*12-1) {
  1069 		} else if (s->age == 2*12-1) {
  1161 			st = GetStation(s->to);
  1070 			st = GetStation(s->to);
  1162 			if (st->owner == _local_player) {
  1071 			if (st->owner == _local_player) {
  1163 				pair = SetupSubsidyDecodeParam(s, 1);
  1072 				pair = SetupSubsidyDecodeParam(s, 1);
  1164 				AddNewsItem(STR_202F_SUBSIDY_WITHDRAWN_SERVICE, NM_NORMAL, NF_TILE | NF_TILE2, NT_SUBSIDIES, DNC_NONE, pair.a, pair.b);
  1073 				AddNewsItem(STR_202F_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, pair.a, pair.b);
  1165 			}
  1074 			}
  1166 			s->cargo_type = CT_INVALID;
  1075 			s->cargo_type = CT_INVALID;
  1167 			modified = true;
  1076 			modified = true;
  1168 			for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1077 			for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1169 				AI_Event(i, new AIEventSubsidyExpired(s - _subsidies));
  1078 				AI_Event(i, new AIEventSubsidyExpired(s - _subsidies));
  1201 				}
  1110 				}
  1202 	add_subsidy:
  1111 	add_subsidy:
  1203 				if (!CheckSubsidyDuplicate(s)) {
  1112 				if (!CheckSubsidyDuplicate(s)) {
  1204 					s->age = 0;
  1113 					s->age = 0;
  1205 					pair = SetupSubsidyDecodeParam(s, 0);
  1114 					pair = SetupSubsidyDecodeParam(s, 0);
  1206 					AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NM_NORMAL, NF_TILE | NF_TILE2, NT_SUBSIDIES, DNC_NONE, pair.a, pair.b);
  1115 					AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NS_SUBSIDIES, pair.a, pair.b);
  1207 					for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1116 					for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1208 						AI_Event(i, new AIEventSubsidyOffer(s - _subsidies));
  1117 						AI_Event(i, new AIEventSubsidyOffer(s - _subsidies));
  1209 					}
  1118 					}
  1210 					modified = true;
  1119 					modified = true;
  1211 					break;
  1120 					break;
  1421 			InjectDParam(1);
  1330 			InjectDParam(1);
  1422 
  1331 
  1423 			SetDParam(0, _current_player);
  1332 			SetDParam(0, _current_player);
  1424 			AddNewsItem(
  1333 			AddNewsItem(
  1425 				STR_2031_SERVICE_SUBSIDY_AWARDED + _opt.diff.subsidy_multiplier,
  1334 				STR_2031_SERVICE_SUBSIDY_AWARDED + _opt.diff.subsidy_multiplier,
  1426 				NM_NORMAL, NF_TILE | NF_TILE2, NT_SUBSIDIES, DNC_NONE,
  1335 				NS_SUBSIDIES,
  1427 				pair.a, pair.b
  1336 				pair.a, pair.b
  1428 			);
  1337 			);
  1429 			for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1338 			for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
  1430 				AI_Event(i, new AIEventSubsidyAwarded(s - _subsidies));
  1339 				AI_Event(i, new AIEventSubsidyAwarded(s - _subsidies));
  1431 			}
  1340 			}
  1862 	Player *owner;
  1771 	Player *owner;
  1863 	int i;
  1772 	int i;
  1864 	Money value;
  1773 	Money value;
  1865 	PlayerID pi = p->index;
  1774 	PlayerID pi = p->index;
  1866 
  1775 
  1867 	SetDParam(0, p->index);
  1776 	SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
  1868 	SetDParam(1, p->bankrupt_value);
  1777 	SetDParam(1, p->bankrupt_value == 0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR);
  1869 	AddNewsItem((StringID)(_current_player | NB_BMERGER), NM_CALLBACK, NF_NONE, NT_COMPANY_INFO, DNC_BANKRUPCY, 0, 0);
  1778 	SetDParam(2, p->index);
       
  1779 	SetDParam(3, _current_player);
       
  1780 	SetDParam(4, p->bankrupt_value);
       
  1781 	AddNewsItem(STR_02B6, NS_COMPANY_MERGER, 0, _current_player);
  1870 	for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
  1782 	for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
  1871 		AI_Event(i, new AIEventCompanyMerger(pi, _current_player));
  1783 		AI_Event(i, new AIEventCompanyMerger(pi, _current_player));
  1872 	}
  1784 	}
  1873 
  1785 
  1874 	/* original code does this a little bit differently */
  1786 	/* original code does this a little bit differently */
  1892 
  1804 
  1893 	p->is_active = false;
  1805 	p->is_active = false;
  1894 	if (!IsHumanPlayer(p->index)) AI_PlayerDied(p->index);
  1806 	if (!IsHumanPlayer(p->index)) AI_PlayerDied(p->index);
  1895 
  1807 
  1896 	DeletePlayerWindows(pi);
  1808 	DeletePlayerWindows(pi);
  1897 	RebuildVehicleLists(); //Updates the open windows to add the newly acquired vehicles to the lists
  1809 	InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
       
  1810 	InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
       
  1811 	InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
       
  1812 	InvalidateWindowClassesData(WC_AIRCRAFT_LIST, 0);
  1898 }
  1813 }
  1899 
  1814 
  1900 extern int GetAmountOwnedBy(const Player *p, PlayerID owner);
  1815 extern int GetAmountOwnedBy(const Player *p, PlayerID owner);
  1901 
  1816 
  1902 /** Acquire shares in an opposing company.
  1817 /** Acquire shares in an opposing company.