tron@2186: /* $Id$ */ tron@2186: belugas@6451: /** @file economy.cpp */ belugas@6451: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2291: #include "currency.h" tron@2163: #include "functions.h" maedhros@6949: #include "landscape.h" tron@1309: #include "strings.h" // XXX InjectDParam() tron@507: #include "table/strings.h" celestar@2218: #include "table/sprites.h" tron@679: #include "map.h" truelight@0: #include "news.h" truelight@0: #include "player.h" truelight@0: #include "station.h" truelight@0: #include "vehicle.h" truelight@0: #include "window.h" truelight@0: #include "gfx.h" truelight@0: #include "command.h" truelight@0: #include "saveload.h" truelight@0: #include "economy.h" truelight@0: #include "industry.h" truelight@0: #include "town.h" rubidium@5720: #include "network/network.h" tron@337: #include "sound.h" tron@445: #include "engine.h" rubidium@5720: #include "network/network_data.h" tron@2159: #include "variables.h" tron@2159: #include "vehicle_gui.h" truelight@2395: #include "ai/ai.h" bjarni@2676: #include "train.h" Darkvater@6105: #include "aircraft.h" peter1138@2962: #include "newgrf_engine.h" peter1138@4701: #include "newgrf_sound.h" peter1138@5211: #include "newgrf_callbacks.h" celestar@3386: #include "unmovable.h" rubidium@4261: #include "date.h" peter1138@6417: #include "cargotype.h" rubidium@6516: #include "player_face.h" truelight@0: belugas@6451: /* Score info */ ludde@2261: const ScoreInfo _score_info[] = { tron@4077: { SCORE_VEHICLES, 120, 100 }, tron@4077: { SCORE_STATIONS, 80, 100 }, tron@4077: { SCORE_MIN_PROFIT, 10000, 100 }, tron@4077: { SCORE_MIN_INCOME, 50000, 50 }, tron@4077: { SCORE_MAX_INCOME, 100000, 100 }, tron@4077: { SCORE_DELIVERED, 40000, 400 }, tron@4077: { SCORE_CARGO, 8, 50 }, tron@4077: { SCORE_MONEY, 10000000, 50 }, tron@4077: { SCORE_LOAN, 250000, 50 }, tron@4077: { SCORE_TOTAL, 0, 0 } ludde@2261: }; ludde@2261: rubidium@5838: int _score_part[MAX_PLAYERS][SCORE_END]; ludde@2261: tron@2639: int64 CalculateCompanyValue(const Player* p) tron@2475: { tron@2475: PlayerID owner = p->index; truelight@200: int64 value; truelight@193: truelight@0: { truelight@0: Station *st; truelight@0: uint num = 0; truelight@193: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: if (st->owner == owner) { truelight@0: uint facil = st->facilities; truelight@0: do num += (facil&1); while (facil >>= 1); truelight@0: } truelight@0: } truelight@0: truelight@0: value = num * _price.station_value * 25; truelight@0: } truelight@0: truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@4346: if (v->owner != owner) continue; truelight@4346: rubidium@6585: if (v->type == VEH_TRAIN || rubidium@6585: v->type == VEH_ROAD || rubidium@6585: (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) || rubidium@6585: v->type == VEH_SHIP) { truelight@0: value += v->value * 3 >> 1; truelight@0: } truelight@0: } truelight@0: } truelight@0: celestar@997: value += p->money64 - p->current_loan; // add real money value tron@1019: celestar@5852: return max(value, 1LL); truelight@0: } truelight@0: belugas@6451: /** if update is set to true, the economy is updated with this score belugas@6451: * (also the house is updated, should only be true in the on-tick event) belugas@6451: * @param update the economy with calculated score belugas@6451: * @param p player been evaluated belugas@6451: * @return actual score of this player belugas@6451: * */ darkvater@147: int UpdateCompanyRatingAndValue(Player *p, bool update) truelight@0: { truelight@0: byte owner = p->index; dominik@116: int score = 0; dominik@116: dominik@116: memset(_score_part[owner], 0, sizeof(_score_part[owner])); truelight@0: truelight@0: /* Count vehicles */ truelight@0: { truelight@0: Vehicle *v; truelight@2829: int32 min_profit = 0; truelight@2829: bool min_profit_first = true; truelight@0: uint num = 0; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { tron@4077: if (v->owner != owner) continue; rubidium@6585: if ((v->type == VEH_TRAIN && IsFrontEngine(v)) || rubidium@6585: v->type == VEH_ROAD || rubidium@6585: (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) || rubidium@6585: v->type == VEH_SHIP) { truelight@0: num++; truelight@0: if (v->age > 730) { truelight@2829: /* Find the vehicle with the lowest amount of profit */ truelight@2829: if (min_profit_first == true) { truelight@2829: min_profit = v->profit_last_year; truelight@2829: min_profit_first = false; tron@4077: } else if (min_profit > v->profit_last_year) { truelight@0: min_profit = v->profit_last_year; tron@4077: } truelight@0: } truelight@0: } truelight@0: } truelight@0: dominik@116: _score_part[owner][SCORE_VEHICLES] = num; truelight@2829: /* Don't allow negative min_profit to show */ tron@1407: if (min_profit > 0) tron@1407: _score_part[owner][SCORE_MIN_PROFIT] = min_profit; truelight@0: } truelight@0: truelight@0: /* Count stations */ truelight@0: { truelight@0: uint num = 0; tron@3033: const Station* st; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: if (st->owner == owner) { truelight@0: int facil = st->facilities; truelight@0: do num += facil&1; while (facil>>=1); truelight@0: } truelight@0: } dominik@116: _score_part[owner][SCORE_STATIONS] = num; truelight@0: } truelight@0: truelight@0: /* Generate statistics depending on recent income statistics */ truelight@0: { tron@3033: const PlayerEconomyEntry* pee; truelight@0: int numec; truelight@0: int32 min_income; truelight@543: int32 max_income; truelight@0: truelight@0: numec = min(p->num_valid_stat_ent, 12); truelight@0: if (numec != 0) { truelight@0: min_income = 0x7FFFFFFF; truelight@0: max_income = 0; truelight@0: pee = p->old_economy; truelight@0: do { truelight@0: min_income = min(min_income, pee->income + pee->expenses); truelight@0: max_income = max(max_income, pee->income + pee->expenses); truelight@0: } while (++pee,--numec); truelight@0: truelight@0: if (min_income > 0) dominik@116: _score_part[owner][SCORE_MIN_INCOME] = min_income; truelight@0: dominik@116: _score_part[owner][SCORE_MAX_INCOME] = max_income; truelight@0: } truelight@0: } truelight@0: truelight@0: /* Generate score depending on amount of transported cargo */ truelight@0: { tron@3033: const PlayerEconomyEntry* pee; truelight@0: int numec; truelight@0: uint32 total_delivered; truelight@0: truelight@0: numec = min(p->num_valid_stat_ent, 4); truelight@0: if (numec != 0) { truelight@0: pee = p->old_economy; truelight@0: total_delivered = 0; truelight@0: do { truelight@0: total_delivered += pee->delivered_cargo; truelight@0: } while (++pee,--numec); truelight@0: dominik@116: _score_part[owner][SCORE_DELIVERED] = total_delivered; truelight@0: } truelight@0: } dominik@116: truelight@0: /* Generate score for variety of cargo */ truelight@0: { truelight@0: uint cargo = p->cargo_types; truelight@0: uint num = 0; truelight@0: do num += cargo&1; while (cargo>>=1); dominik@116: _score_part[owner][SCORE_CARGO] = num; tron@3033: if (update) p->cargo_types = 0; truelight@0: } truelight@0: truelight@0: /* Generate score for player money */ truelight@0: { truelight@0: int32 money = p->player_money; truelight@0: if (money > 0) { dominik@116: _score_part[owner][SCORE_MONEY] = money; truelight@0: } truelight@0: } truelight@0: truelight@0: /* Generate score for loan */ truelight@0: { ludde@2261: _score_part[owner][SCORE_LOAN] = _score_info[SCORE_LOAN].needed - p->current_loan; dominik@116: } truelight@193: belugas@6451: /* Now we calculate the score for each item.. */ dominik@116: { dominik@116: int total_score = 0; dominik@116: int s; dominik@116: score = 0; rubidium@5838: for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) { belugas@6451: /* Skip the total */ dominik@116: if (i == SCORE_TOTAL) continue; belugas@6451: /* Check the score */ ludde@2261: s = (_score_part[owner][i] >= _score_info[i].needed) ? ludde@2261: _score_info[i].score : tron@3033: _score_part[owner][i] * _score_info[i].score / _score_info[i].needed; dominik@116: if (s < 0) s = 0; dominik@116: score += s; ludde@2261: total_score += _score_info[i].score; dominik@116: } truelight@193: dominik@116: _score_part[owner][SCORE_TOTAL] = score; truelight@193: belugas@6451: /* We always want the score scaled to SCORE_MAX (1000) */ tron@3033: if (total_score != SCORE_MAX) score = score * SCORE_MAX / total_score; truelight@0: } truelight@0: dominik@116: if (update) { tron@4077: p->old_economy[0].performance_history = score; tron@4077: UpdateCompanyHQ(p, score); tron@4077: p->old_economy[0].company_value = CalculateCompanyValue(p); tron@4077: } truelight@193: dominik@116: InvalidateWindow(WC_PERFORMANCE_DETAIL, 0); darkvater@147: return score; truelight@0: } truelight@0: belugas@6451: /* use PLAYER_SPECTATOR as new_player to delete the player. */ Darkvater@1797: void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player) truelight@0: { peter1138@5284: Town *t; Darkvater@1797: PlayerID old = _current_player; truelight@6899: celestar@6901: assert(old_player != new_player); celestar@6901: truelight@6899: { truelight@6899: Player *p; truelight@6899: uint i; truelight@6899: truelight@6899: /* See if the old_player had shares in other companies */ truelight@6899: _current_player = old_player; truelight@6899: FOR_ALL_PLAYERS(p) { truelight@6899: for (i = 0; i < 4; i++) { truelight@6899: if (p->share_owners[i] == old_player) { truelight@6899: /* Sell his shares */ truelight@6899: int32 res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY); truelight@6899: /* Because we are in a DoCommand, we can't just execute an other one and truelight@6899: * expect the money to be removed. We need to do it ourself! */ truelight@6899: SubtractMoneyFromPlayer(res); truelight@6899: } truelight@6899: } truelight@6899: } truelight@6899: truelight@6899: /* Sell all the shares that people have on this company */ truelight@6899: p = GetPlayer(old_player); truelight@6899: for (i = 0; i < 4; i++) { truelight@6899: _current_player = p->share_owners[i]; truelight@6899: if (_current_player != PLAYER_SPECTATOR) { truelight@6899: /* Sell the shares */ truelight@6899: int32 res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY); truelight@6899: /* Because we are in a DoCommand, we can't just execute an other one and truelight@6899: * expect the money to be removed. We need to do it ourself! */ truelight@6899: SubtractMoneyFromPlayer(res); truelight@6899: } truelight@6899: } truelight@6899: } truelight@6899: truelight@0: _current_player = old_player; truelight@0: Darkvater@4260: /* Temporarily increase the player's money, to be sure that rubidium@4549: * removing his/her property doesn't fail because of lack of money. rubidium@4549: * Not too drastically though, because it could overflow */ Darkvater@4848: if (new_player == PLAYER_SPECTATOR) { Darkvater@4540: GetPlayer(old_player)->money64 = MAX_UVALUE(uint64) >>2; // jackpot ;p truelight@4423: UpdatePlayerMoney32(GetPlayer(old_player)); Darkvater@4260: } Darkvater@4260: Darkvater@4848: if (new_player == PLAYER_SPECTATOR) { truelight@0: Subsidy *s; truelight@193: Darkvater@1797: for (s = _subsidies; s != endof(_subsidies); s++) { tron@2469: if (s->cargo_type != CT_INVALID && s->age >= 12) { tron@4077: if (GetStation(s->to)->owner == old_player) s->cargo_type = CT_INVALID; truelight@0: } truelight@0: } truelight@0: } truelight@0: Darkvater@1797: /* Take care of rating in towns */ peter1138@5284: FOR_ALL_TOWNS(t) { peter1138@5284: /* If a player takes over, give the ratings to that player. */ peter1138@5284: if (new_player != PLAYER_SPECTATOR) { truelight@4346: if (HASBIT(t->have_ratings, old_player)) { Darkvater@4260: if (HASBIT(t->have_ratings, new_player)) { Darkvater@4260: // use max of the two ratings. Darkvater@4260: t->ratings[new_player] = max(t->ratings[new_player], t->ratings[old_player]); Darkvater@4260: } else { Darkvater@4260: SETBIT(t->have_ratings, new_player); Darkvater@4260: t->ratings[new_player] = t->ratings[old_player]; Darkvater@1797: } Darkvater@4260: } peter1138@5284: } truelight@193: peter1138@5284: /* Reset the ratings for the old player */ peter1138@5284: t->ratings[old_player] = 500; peter1138@5284: CLRBIT(t->have_ratings, old_player); truelight@0: } truelight@193: truelight@0: { truelight@0: int num_train = 0; truelight@0: int num_road = 0; truelight@0: int num_ship = 0; truelight@0: int num_aircraft = 0; truelight@0: Vehicle *v; truelight@0: belugas@6451: /* Determine Ids for the new vehicles */ truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->owner == new_player) { Darkvater@1797: switch (v->type) { rubidium@6585: case VEH_TRAIN: if (IsFrontEngine(v)) num_train++; break; rubidium@6585: case VEH_ROAD: num_road++; break; rubidium@6585: case VEH_SHIP: num_ship++; break; rubidium@6585: case VEH_AIRCRAFT: if (IsNormalAircraft(v)) num_aircraft++; break; Darkvater@1797: default: break; Darkvater@1797: } truelight@0: } truelight@0: } truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { rubidium@6585: if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_TRAIN, VEH_AIRCRAFT + 1)) { Darkvater@4848: if (new_player == PLAYER_SPECTATOR) { truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); truelight@0: DeleteWindowById(WC_VEHICLE_DETAILS, v->index); truelight@0: DeleteWindowById(WC_VEHICLE_ORDERS, v->index); truelight@0: DeleteVehicle(v); truelight@0: } else { truelight@0: v->owner = new_player; bjarni@4621: if (IsEngineCountable(v)) GetPlayer(new_player)->num_engines[v->engine_type]++; tron@2989: switch (v->type) { rubidium@6585: case VEH_TRAIN: if (IsFrontEngine(v)) v->unitnumber = ++num_train; break; rubidium@6585: case VEH_ROAD: v->unitnumber = ++num_road; break; rubidium@6585: case VEH_SHIP: v->unitnumber = ++num_ship; break; rubidium@6585: case VEH_AIRCRAFT: if (IsNormalAircraft(v)) v->unitnumber = ++num_aircraft; break; tron@2989: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@6451: /* Change ownership of tiles */ truelight@0: { Darkvater@1797: TileIndex tile = 0; truelight@0: do { truelight@0: ChangeTileOwner(tile, old_player, new_player); tron@863: } while (++tile != MapSize()); truelight@0: } truelight@0: bjarni@5077: /* Change color of existing windows */ bjarni@5077: if (new_player != PLAYER_SPECTATOR) ChangeWindowOwner(old_player, new_player); truelight@0: truelight@0: _current_player = old; truelight@0: truelight@0: MarkWholeScreenDirty(); truelight@0: } truelight@0: truelight@6588: static void ChangeNetworkOwner(PlayerID current_player, PlayerID new_player) truelight@6588: { truelight@6588: #ifdef ENABLE_NETWORK truelight@6588: if (!_networking) return; truelight@6588: truelight@6588: if (current_player == _local_player) { truelight@6588: _network_playas = new_player; truelight@6588: SetLocalPlayer(new_player); truelight@6588: } truelight@6588: truelight@6588: if (!_network_server) return; truelight@6588: truelight@6588: /* The server has to handle all administrative issues, for example truelight@6588: * updating and notifying all clients of what has happened */ truelight@6588: NetworkTCPSocketHandler *cs; truelight@6588: NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); truelight@6588: truelight@6588: /* The server has just changed from player */ truelight@6588: if (current_player == ci->client_playas) { truelight@6588: ci->client_playas = new_player; truelight@6588: NetworkUpdateClientInfo(NETWORK_SERVER_INDEX); truelight@6588: } truelight@6588: truelight@6588: /* Find all clients that were in control of this company, and mark them as new_player */ truelight@6588: FOR_ALL_CLIENTS(cs) { truelight@6588: ci = DEREF_CLIENT_INFO(cs); truelight@6588: if (current_player == ci->client_playas) { truelight@6588: ci->client_playas = new_player; truelight@6588: NetworkUpdateClientInfo(ci->client_index); truelight@6588: } truelight@6588: } truelight@6588: #endif /* ENABLE_NETWORK */ truelight@6588: } truelight@6588: truelight@0: static void PlayersCheckBankrupt(Player *p) truelight@0: { tron@2475: PlayerID owner; truelight@200: int64 val; truelight@0: belugas@6451: /* If the player has money again, it does not go bankrupt */ truelight@0: if (p->player_money >= 0) { truelight@0: p->quarters_of_bankrupcy = 0; truelight@0: return; truelight@0: } truelight@0: truelight@0: p->quarters_of_bankrupcy++; truelight@0: truelight@0: owner = p->index; truelight@0: truelight@543: switch (p->quarters_of_bankrupcy) { truelight@543: case 2: Darkvater@4873: AddNewsItem( (StringID)(owner | NB_BTROUBLE), truelight@543: NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0); truelight@543: break; truelight@543: case 3: { truelight@543: /* XXX - In multiplayer, should we ask other players if it wants to take truelight@543: over when it is a human company? -- TrueLight */ Darkvater@4845: if (IsHumanPlayer(owner)) { Darkvater@4873: AddNewsItem( (StringID)(owner | NB_BTROUBLE), truelight@543: NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0); truelight@543: break; truelight@543: } truelight@0: belugas@6451: /* Check if the company has any value.. if not, declare it bankrupt belugas@6451: * right now */ truelight@543: val = CalculateCompanyValue(p); truelight@543: if (val > 0) { truelight@543: p->bankrupt_value = val; truelight@543: p->bankrupt_asked = 1 << owner; // Don't ask the owner truelight@543: p->bankrupt_timeout = 0; truelight@543: break; truelight@543: } belugas@6451: /* Else, falltrue to case 4... */ truelight@0: } truelight@543: case 4: { belugas@6451: /* Close everything the owner has open */ truelight@543: DeletePlayerWindows(owner); truelight@0: belugas@6451: /* Show bankrupt news */ truelight@543: SetDParam(0, p->name_1); truelight@543: SetDParam(1, p->name_2); Darkvater@4873: AddNewsItem( (StringID)(owner | NB_BBANKRUPT), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0); truelight@543: Darkvater@5067: if (IsHumanPlayer(owner)) { Darkvater@5067: /* XXX - If we are in offline mode, leave the player playing. Eg. there Darkvater@5067: * is no THE-END, otherwise mark the player as spectator to make sure Darkvater@5067: * he/she is no long in control of this company */ Darkvater@5067: if (!_networking) { Darkvater@5067: p->bankrupt_asked = 0xFF; Darkvater@5067: p->bankrupt_timeout = 0x456; Darkvater@5067: break; Darkvater@5067: } Darkvater@5067: truelight@6588: ChangeNetworkOwner(owner, PLAYER_SPECTATOR); Darkvater@5067: } truelight@687: Darkvater@5067: /* Remove the player */ Darkvater@5067: ChangeOwnershipOfPlayerItems(owner, PLAYER_SPECTATOR); Darkvater@5067: /* Register the player as not-active */ Darkvater@5067: p->is_active = false; truelight@2395: Darkvater@5067: if (!IsHumanPlayer(owner) && (!_networking || _network_server) && _ai.enabled) Darkvater@5067: AI_PlayerDied(owner); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: void DrawNewsBankrupcy(Window *w) truelight@0: { truelight@0: Player *p; truelight@0: truelight@193: DrawNewsBorder(w); truelight@0: rubidium@5838: p = GetPlayer((PlayerID)GB(WP(w,news_d).ni->string_id, 0, 4)); truelight@0: DrawPlayerFace(p->face, p->player_color, 2, 23); peter1138@5919: GfxFillRect(3, 23, 3 + 91, 23 + 118, PALETTE_TO_STRUCT_GREY | (1 << USE_COLORTABLE)); truelight@0: tron@534: SetDParam(0, p->president_name_1); tron@534: SetDParam(1, p->president_name_2); truelight@0: truelight@0: DrawStringMultiCenter(49, 148, STR_7058_PRESIDENT, 94); truelight@0: Darkvater@4873: switch (WP(w,news_d).ni->string_id & 0xF0) { Darkvater@4873: case NB_BTROUBLE: truelight@0: DrawStringCentered(w->width>>1, 1, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE, 0); truelight@0: tron@534: SetDParam(0, p->name_1); tron@534: SetDParam(1, p->name_2); truelight@0: truelight@0: DrawStringMultiCenter( truelight@0: ((w->width - 101) >> 1) + 98, truelight@0: 90, truelight@0: STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED, truelight@0: w->width - 101); truelight@0: break; truelight@0: Darkvater@4873: case NB_BMERGER: { truelight@0: int32 price; truelight@0: truelight@0: DrawStringCentered(w->width>>1, 1, STR_7059_TRANSPORT_COMPANY_MERGER, 0); truelight@0: COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2); tron@534: SetDParam(2, p->name_1); tron@534: SetDParam(3, p->name_2); truelight@0: price = WP(w,news_d).ni->params[2]; tron@534: SetDParam(4, price); truelight@0: DrawStringMultiCenter( truelight@0: ((w->width - 101) >> 1) + 98, truelight@0: 90, truelight@0: price==0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR, truelight@0: w->width - 101); truelight@0: break; truelight@0: } truelight@0: Darkvater@4873: case NB_BBANKRUPT: truelight@0: DrawStringCentered(w->width>>1, 1, STR_705C_BANKRUPT, 0); truelight@0: COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2); truelight@0: DrawStringMultiCenter( truelight@0: ((w->width - 101) >> 1) + 98, truelight@0: 90, truelight@0: STR_705D_HAS_BEEN_CLOSED_DOWN_BY, truelight@0: w->width - 101); truelight@193: break; truelight@0: Darkvater@4873: case NB_BNEWCOMPANY: truelight@0: DrawStringCentered(w->width>>1, 1, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED, 0); tron@534: SetDParam(0, p->name_1); tron@534: SetDParam(1, p->name_2); truelight@0: COPY_IN_DPARAM(2,WP(w,news_d).ni->params, 2); truelight@0: DrawStringMultiCenter( truelight@0: ((w->width - 101) >> 1) + 98, truelight@0: 90, truelight@0: STR_705F_STARTS_CONSTRUCTION_NEAR, truelight@0: w->width - 101); truelight@0: break; truelight@0: truelight@0: default: truelight@0: NOT_REACHED(); truelight@0: } truelight@0: } truelight@0: Darkvater@2436: StringID GetNewsStringBankrupcy(const NewsItem *ni) truelight@0: { rubidium@5838: const Player *p = GetPlayer((PlayerID)GB(ni->string_id, 0, 4)); truelight@0: Darkvater@4873: switch (ni->string_id & 0xF0) { Darkvater@4873: case NB_BTROUBLE: tron@534: SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE); tron@534: SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED); tron@534: SetDParam(2, p->name_1); tron@534: SetDParam(3, p->name_2); truelight@0: return STR_02B6; Darkvater@4873: case NB_BMERGER: tron@534: SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER); tron@534: SetDParam(1, STR_705A_HAS_BEEN_SOLD_TO_FOR); truelight@0: COPY_IN_DPARAM(2,ni->params, 2); tron@534: SetDParam(4, p->name_1); tron@534: SetDParam(5, p->name_2); truelight@0: COPY_IN_DPARAM(6,ni->params + 2, 1); truelight@0: return STR_02B6; Darkvater@4873: case NB_BBANKRUPT: tron@534: SetDParam(0, STR_705C_BANKRUPT); tron@534: SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY); truelight@0: COPY_IN_DPARAM(2,ni->params, 2); truelight@0: return STR_02B6; Darkvater@4873: case NB_BNEWCOMPANY: tron@534: SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED); tron@534: SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR); tron@534: SetDParam(2, p->name_1); tron@534: SetDParam(3, p->name_2); truelight@0: COPY_IN_DPARAM(4,ni->params, 2); truelight@0: return STR_02B6; truelight@0: default: truelight@0: NOT_REACHED(); truelight@0: } truelight@193: truelight@0: /* useless, but avoids compiler warning this way */ truelight@0: return 0; truelight@0: } truelight@0: rubidium@6573: static void PlayersGenStatistics() truelight@0: { truelight@0: Station *st; truelight@0: Player *p; truelight@0: truelight@0: FOR_ALL_STATIONS(st) { truelight@4346: _current_player = st->owner; truelight@4346: SET_EXPENSES_TYPE(EXPENSES_PROPERTY); truelight@4346: SubtractMoneyFromPlayer(_price.station_value >> 1); truelight@0: } truelight@0: truelight@0: if (!HASBIT(1<<0|1<<3|1<<6|1<<9, _cur_month)) truelight@0: return; truelight@0: truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (p->is_active) { Darkvater@5677: memmove(&p->old_economy[1], &p->old_economy[0], sizeof(p->old_economy) - sizeof(p->old_economy[0])); Darkvater@5677: p->old_economy[0] = p->cur_economy; truelight@0: memset(&p->cur_economy, 0, sizeof(p->cur_economy)); truelight@0: Darkvater@5677: if (p->num_valid_stat_ent != 24) p->num_valid_stat_ent++; truelight@0: dominik@116: UpdateCompanyRatingAndValue(p, true); truelight@0: PlayersCheckBankrupt(p); truelight@0: Darkvater@5677: if (p->block_preview != 0) p->block_preview--; truelight@0: } truelight@0: } truelight@0: truelight@0: InvalidateWindow(WC_INCOME_GRAPH, 0); truelight@0: InvalidateWindow(WC_OPERATING_PROFIT, 0); truelight@0: InvalidateWindow(WC_DELIVERED_CARGO, 0); truelight@0: InvalidateWindow(WC_PERFORMANCE_HISTORY, 0); truelight@0: InvalidateWindow(WC_COMPANY_VALUE, 0); truelight@0: InvalidateWindow(WC_COMPANY_LEAGUE, 0); truelight@0: } truelight@0: truelight@0: static void AddSingleInflation(int32 *value, uint16 *frac, int32 amt) truelight@0: { tron@6445: int64 tmp = (int64)*value * amt + *frac; tron@6445: *frac = GB(tmp, 0, 16); tron@6445: *value += tmp >> 16; truelight@0: } truelight@0: rubidium@6573: static void AddInflation() truelight@0: { tron@6445: /* Approximation for (100 + infl_amount)% ** (1 / 12) - 100% tron@6445: * scaled by 65536 tron@6445: * 12 -> months per year tron@6445: * This is only a good approxiamtion for small values tron@6445: */ truelight@0: int32 inf = _economy.infl_amount * 54; truelight@0: tron@6445: for (uint i = 0; i != NUM_PRICES; i++) { tron@2639: AddSingleInflation((int32*)&_price + i, _price_frac + i, inf); truelight@0: } truelight@0: truelight@0: _economy.max_loan_unround += BIGMULUS(_economy.max_loan_unround, inf, 16); truelight@193: truelight@0: if (_economy.max_loan + 50000 <= _economy.max_loan_unround) truelight@0: _economy.max_loan += 50000; truelight@0: truelight@0: inf = _economy.infl_amount_pr * 54; peter1138@6676: for (CargoID i = 0; i < NUM_CARGO; i++) { truelight@193: AddSingleInflation( hackykid@1884: (int32*)_cargo_payment_rates + i, truelight@0: _cargo_payment_rates_frac + i, truelight@0: inf truelight@0: ); truelight@0: } truelight@0: truelight@0: InvalidateWindowClasses(WC_BUILD_VEHICLE); bjarni@1098: InvalidateWindowClasses(WC_REPLACE_VEHICLE); truelight@0: InvalidateWindowClasses(WC_VEHICLE_DETAILS); truelight@0: InvalidateWindow(WC_PAYMENT_RATES, 0); truelight@0: } truelight@0: rubidium@6573: static void PlayersPayInterest() truelight@0: { tron@2548: const Player* p; truelight@0: int interest = _economy.interest_rate * 54; truelight@0: truelight@0: FOR_ALL_PLAYERS(p) { tron@2639: if (!p->is_active) continue; truelight@0: truelight@0: _current_player = p->index; truelight@0: SET_EXPENSES_TYPE(EXPENSES_LOAN_INT); truelight@193: truelight@0: SubtractMoneyFromPlayer(BIGMULUS(p->current_loan, interest, 16)); truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); truelight@0: SubtractMoneyFromPlayer(_price.station_value >> 2); truelight@0: } truelight@0: } truelight@0: rubidium@6573: static void HandleEconomyFluctuations() truelight@0: { tron@2639: if (_opt.diff.economy == 0) return; truelight@0: truelight@0: if (--_economy.fluct == 0) { tron@2642: _economy.fluct = -(int)GB(Random(), 0, 2); truelight@0: AddNewsItem(STR_7073_WORLD_RECESSION_FINANCIAL, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0); truelight@0: } else if (_economy.fluct == -12) { tron@2642: _economy.fluct = GB(Random(), 0, 8) + 312; truelight@0: AddNewsItem(STR_7074_RECESSION_OVER_UPTURN_IN, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0); truelight@0: } truelight@0: } truelight@0: truelight@0: static byte _price_category[NUM_PRICES] = { truelight@0: 0, 2, 2, 2, 2, 2, 2, 2, truelight@0: 2, 2, 2, 2, 2, 2, 2, 2, truelight@0: 2, 2, 2, 2, 2, 2, 2, 2, truelight@0: 2, 2, 2, 2, 2, 2, 2, 2, truelight@0: 2, 2, 2, 2, 2, 2, 2, 2, truelight@0: 2, 2, 1, 1, 1, 1, 1, 1, truelight@0: 2, truelight@0: }; truelight@0: truelight@0: static const int32 _price_base[NUM_PRICES] = { belugas@6451: 100, ///< station_value belugas@6451: 100, ///< build_rail belugas@6451: 95, ///< build_road belugas@6451: 65, ///< build_signals belugas@6451: 275, ///< build_bridge belugas@6451: 600, ///< build_train_depot belugas@6451: 500, ///< build_road_depot belugas@6451: 700, ///< build_ship_depot belugas@6451: 450, ///< build_tunnel belugas@6451: 200, ///< train_station_track belugas@6451: 180, ///< train_station_length belugas@6451: 600, ///< build_airport belugas@6451: 200, ///< build_bus_station belugas@6451: 200, ///< build_truck_station belugas@6451: 350, ///< build_dock belugas@6451: 400000, ///< build_railvehicle belugas@6451: 2000, ///< build_railwagon belugas@6451: 700000, ///< aircraft_base belugas@6451: 14000, ///< roadveh_base belugas@6451: 65000, ///< ship_base belugas@6451: 20, ///< build_trees belugas@6451: 250, ///< terraform belugas@6451: 20, ///< clear_1 belugas@6451: 40, ///< purchase_land belugas@6451: 200, ///< clear_2 belugas@6451: 500, ///< clear_3 belugas@6451: 20, ///< remove_trees belugas@6451: -70, ///< remove_rail belugas@6451: 10, ///< remove_signals belugas@6451: 50, ///< clear_bridge belugas@6451: 80, ///< remove_train_depot belugas@6451: 80, ///< remove_road_depot belugas@6451: 90, ///< remove_ship_depot belugas@6451: 30, ///< clear_tunnel belugas@6451: 10000, ///< clear_water belugas@6451: 50, ///< remove_rail_station belugas@6451: 30, ///< remove_airport belugas@6451: 50, ///< remove_bus_station belugas@6451: 50, ///< remove_truck_station belugas@6451: 55, ///< remove_dock belugas@6451: 1600, ///< remove_house belugas@6451: 40, ///< remove_road belugas@6451: 5600, ///< running_rail[0] railroad belugas@6451: 5200, ///< running_rail[1] monorail belugas@6451: 4800, ///< running_rail[2] maglev belugas@6451: 9600, ///< aircraft_running belugas@6451: 1600, ///< roadveh_running belugas@6451: 5600, ///< ship_running belugas@6451: 1000000, ///< build_industry truelight@0: }; truelight@0: peter1138@2506: static byte price_base_multiplier[NUM_PRICES]; peter1138@2506: peter1138@2506: /** peter1138@2506: * Reset changes to the price base multipliers. peter1138@2506: */ rubidium@6573: void ResetPriceBaseMultipliers() peter1138@2506: { peter1138@2508: uint i; peter1138@2506: belugas@6451: /* 8 means no multiplier. */ peter1138@2506: for (i = 0; i < NUM_PRICES; i++) peter1138@2506: price_base_multiplier[i] = 8; peter1138@2506: } peter1138@2506: peter1138@2506: /** peter1138@2506: * Change a price base by the given factor. peter1138@2506: * The price base is altered by factors of two, with an offset of 8. peter1138@2506: * NewBaseCost = OldBaseCost * 2^(n-8) peter1138@2506: * @param price Index of price base to change. peter1138@2506: * @param factor Amount to change by. peter1138@2506: */ peter1138@2508: void SetPriceBaseMultiplier(uint price, byte factor) peter1138@2506: { peter1138@2508: assert(price < NUM_PRICES); peter1138@2508: price_base_multiplier[price] = factor; peter1138@2506: } peter1138@2506: rubidium@6573: void StartupEconomy() truelight@0: { truelight@0: int i; truelight@0: truelight@0: assert(sizeof(_price) == NUM_PRICES * sizeof(int32)); truelight@0: tron@2952: for (i = 0; i != NUM_PRICES; i++) { truelight@0: int32 price = _price_base[i]; truelight@0: if (_price_category[i] != 0) { truelight@0: uint mod = _price_category[i] == 1 ? _opt.diff.vehicle_costs : _opt.diff.construction_cost; truelight@0: if (mod < 1) { truelight@0: price = price * 3 >> 2; truelight@0: } else if (mod > 1) { truelight@0: price = price * 9 >> 3; truelight@0: } truelight@0: } peter1138@2506: if (price_base_multiplier[i] > 8) { peter1138@2506: price <<= price_base_multiplier[i] - 8; peter1138@2506: } else { peter1138@2506: price >>= 8 - price_base_multiplier[i]; peter1138@2506: } truelight@0: ((int32*)&_price)[i] = price; truelight@0: _price_frac[i] = 0; truelight@0: } truelight@0: truelight@0: _economy.interest_rate = _opt.diff.initial_interest; truelight@0: _economy.infl_amount = _opt.diff.initial_interest; truelight@0: _economy.infl_amount_pr = max(0, _opt.diff.initial_interest - 1); truelight@0: _economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000; tron@2150: _economy.fluct = GB(Random(), 0, 8) + 168; truelight@0: } truelight@0: tron@2630: Pair SetupSubsidyDecodeParam(const Subsidy* s, bool mode) truelight@0: { tron@1977: TileIndex tile; tron@1977: TileIndex tile2; truelight@0: Pair tp; truelight@0: tron@2272: /* if mode is false, use the singular form */ peter1138@6417: const CargoSpec *cs = GetCargo(s->cargo_type); peter1138@6417: SetDParam(0, mode ? cs->name_plural : cs->name); truelight@0: truelight@0: if (s->age < 12) { peter1138@6641: if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) { ludde@2070: SetDParam(1, STR_INDUSTRY); ludde@2070: SetDParam(2, s->from); ludde@2070: tile = GetIndustry(s->from)->xy; truelight@0: peter1138@6641: if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) { ludde@2070: SetDParam(4, STR_INDUSTRY); ludde@2070: SetDParam(5, s->to); ludde@2070: tile2 = GetIndustry(s->to)->xy; truelight@0: } else { ludde@2070: SetDParam(4, STR_TOWN); ludde@2070: SetDParam(5, s->to); ludde@2070: tile2 = GetTown(s->to)->xy; truelight@0: } truelight@0: } else { ludde@2070: SetDParam(1, STR_TOWN); ludde@2070: SetDParam(2, s->from); ludde@2070: tile = GetTown(s->from)->xy; truelight@0: ludde@2070: SetDParam(4, STR_TOWN); ludde@2070: SetDParam(5, s->to); ludde@2070: tile2 = GetTown(s->to)->xy; truelight@0: } truelight@0: } else { ludde@2070: SetDParam(1, s->from); ludde@2070: tile = GetStation(s->from)->xy; truelight@0: ludde@2070: SetDParam(2, s->to); ludde@2070: tile2 = GetStation(s->to)->xy; truelight@0: } truelight@0: truelight@0: tp.a = tile; truelight@0: tp.b = tile2; truelight@0: truelight@0: return tp; truelight@0: } truelight@0: rubidium@5566: void DeleteSubsidyWithTown(TownID index) rubidium@5566: { rubidium@5566: Subsidy *s; rubidium@5566: rubidium@5566: for (s = _subsidies; s != endof(_subsidies); s++) { peter1138@6641: if (s->cargo_type != CT_INVALID && s->age < 12) { peter1138@6641: const CargoSpec *cs = GetCargo(s->cargo_type); peter1138@6641: if (((cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) && (index == s->from || index == s->to)) || peter1138@6641: ((cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) && index == s->to)) { peter1138@6641: s->cargo_type = CT_INVALID; peter1138@6641: } rubidium@5566: } rubidium@5566: } rubidium@5566: } rubidium@5566: rubidium@4330: void DeleteSubsidyWithIndustry(IndustryID index) truelight@0: { truelight@0: Subsidy *s; truelight@193: tron@2952: for (s = _subsidies; s != endof(_subsidies); s++) { peter1138@6641: if (s->cargo_type != CT_INVALID && s->age < 12) { peter1138@6641: const CargoSpec *cs = GetCargo(s->cargo_type); peter1138@6641: if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL && peter1138@6641: (index == s->from || (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD && index == s->to))) { peter1138@6641: s->cargo_type = CT_INVALID; peter1138@6641: } truelight@0: } truelight@193: } truelight@0: } truelight@0: rubidium@4330: void DeleteSubsidyWithStation(StationID index) truelight@0: { truelight@0: Subsidy *s; truelight@0: bool dirty = false; truelight@193: tron@2952: for (s = _subsidies; s != endof(_subsidies); s++) { tron@2469: if (s->cargo_type != CT_INVALID && s->age >= 12 && truelight@0: (s->from == index || s->to == index)) { tron@2469: s->cargo_type = CT_INVALID; truelight@0: dirty = true; truelight@0: } truelight@193: } truelight@0: truelight@0: if (dirty) truelight@0: InvalidateWindow(WC_SUBSIDIES_LIST, 0); truelight@0: } truelight@0: rubidium@6574: struct FoundRoute { truelight@0: uint distance; Darkvater@3344: CargoID cargo; truelight@0: void *from; truelight@0: void *to; rubidium@6574: }; truelight@0: truelight@0: static void FindSubsidyPassengerRoute(FoundRoute *fr) truelight@0: { truelight@0: Town *from,*to; truelight@0: truelight@0: fr->distance = (uint)-1; truelight@0: truelight@4356: fr->from = from = GetRandomTown(); truelight@4356: if (from == NULL || from->population < 400) return; truelight@0: truelight@4356: fr->to = to = GetRandomTown(); truelight@4356: if (from == to || to == NULL || to->population < 400 || to->pct_pass_transported > 42) truelight@0: return; truelight@0: tron@1245: fr->distance = DistanceManhattan(from->xy, to->xy); truelight@0: } truelight@0: truelight@0: static void FindSubsidyCargoRoute(FoundRoute *fr) truelight@0: { truelight@0: Industry *i; truelight@0: int trans, total; Darkvater@3344: CargoID cargo; truelight@0: truelight@0: fr->distance = (uint)-1; truelight@0: truelight@4356: fr->from = i = GetRandomIndustry(); truelight@4356: if (i == NULL) return; truelight@0: belugas@6451: /* Randomize cargo type */ tron@2469: if (Random()&1 && i->produced_cargo[1] != CT_INVALID) { truelight@0: cargo = i->produced_cargo[1]; truelight@0: trans = i->pct_transported[1]; truelight@0: total = i->total_production[1]; truelight@0: } else { truelight@0: cargo = i->produced_cargo[0]; truelight@0: trans = i->pct_transported[0]; truelight@0: total = i->total_production[0]; truelight@0: } truelight@0: belugas@6451: /* Quit if no production in this industry belugas@6451: * or if the cargo type is passengers belugas@6451: * or if the pct transported is already large enough */ peter1138@6641: if (total == 0 || trans > 42 || cargo == CT_INVALID) return; peter1138@6641: peter1138@6641: const CargoSpec *cs = GetCargo(cargo); peter1138@6641: if (cs->town_effect == TE_PASSENGERS) return; truelight@0: truelight@0: fr->cargo = cargo; truelight@0: peter1138@6641: if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) { belugas@6451: /* The destination is a town */ truelight@4356: Town *t = GetRandomTown(); truelight@193: belugas@6451: /* Only want big towns */ truelight@4356: if (t == NULL || t->population < 900) return; truelight@4346: tron@1245: fr->distance = DistanceManhattan(i->xy, t->xy); truelight@0: fr->to = t; truelight@0: } else { belugas@6451: /* The destination is an industry */ truelight@4356: Industry *i2 = GetRandomIndustry(); truelight@193: belugas@6451: /* The industry must accept the cargo */ truelight@4356: if (i == i2 || i == NULL || truelight@0: (cargo != i2->accepts_cargo[0] && truelight@0: cargo != i2->accepts_cargo[1] && truelight@0: cargo != i2->accepts_cargo[2])) truelight@0: return; tron@1245: fr->distance = DistanceManhattan(i->xy, i2->xy); truelight@0: fr->to = i2; truelight@0: } truelight@0: } truelight@0: truelight@193: static bool CheckSubsidyDuplicate(Subsidy *s) truelight@0: { tron@2630: const Subsidy* ss; truelight@0: tron@2639: for (ss = _subsidies; ss != endof(_subsidies); ss++) { truelight@193: if (s != ss && truelight@193: ss->from == s->from && truelight@193: ss->to == s->to && truelight@0: ss->cargo_type == s->cargo_type) { tron@2469: s->cargo_type = CT_INVALID; truelight@0: return true; truelight@0: } truelight@0: } truelight@0: return false; truelight@0: } truelight@0: signde@239: rubidium@6573: static void SubsidyMonthlyHandler() truelight@0: { truelight@0: Subsidy *s; truelight@0: Pair pair; truelight@0: Station *st; truelight@0: uint n; truelight@0: FoundRoute fr; truelight@0: bool modified = false; truelight@0: tron@2952: for (s = _subsidies; s != endof(_subsidies); s++) { tron@2952: if (s->cargo_type == CT_INVALID) continue; truelight@0: truelight@0: if (s->age == 12-1) { truelight@0: pair = SetupSubsidyDecodeParam(s, 1); truelight@0: AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b); tron@2469: s->cargo_type = CT_INVALID; truelight@0: modified = true; truelight@0: } else if (s->age == 2*12-1) { truelight@919: st = GetStation(s->to); truelight@0: if (st->owner == _local_player) { truelight@0: pair = SetupSubsidyDecodeParam(s, 1); truelight@0: AddNewsItem(STR_202F_SUBSIDY_WITHDRAWN_SERVICE, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b); truelight@0: } tron@2469: s->cargo_type = CT_INVALID; truelight@0: modified = true; truelight@0: } else { truelight@0: s->age++; truelight@0: } truelight@0: } truelight@0: belugas@6451: /* 25% chance to go on */ truelight@543: if (CHANCE16(1,4)) { belugas@6451: /* Find a free slot*/ truelight@0: s = _subsidies; tron@2469: while (s->cargo_type != CT_INVALID) { truelight@0: if (++s == endof(_subsidies)) truelight@0: goto no_add; truelight@0: } truelight@193: truelight@0: n = 1000; truelight@0: do { truelight@0: FindSubsidyPassengerRoute(&fr); truelight@0: if (fr.distance <= 70) { truelight@0: s->cargo_type = CT_PASSENGERS; truelight@0: s->from = ((Town*)fr.from)->index; truelight@0: s->to = ((Town*)fr.to)->index; truelight@0: goto add_subsidy; truelight@0: } truelight@0: FindSubsidyCargoRoute(&fr); truelight@0: if (fr.distance <= 70) { truelight@0: s->cargo_type = fr.cargo; truelight@919: s->from = ((Industry*)fr.from)->index; peter1138@6641: { peter1138@6641: const CargoSpec *cs = GetCargo(fr.cargo); peter1138@6641: s->to = (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index; peter1138@6641: } truelight@0: add_subsidy: truelight@0: if (!CheckSubsidyDuplicate(s)) { truelight@0: s->age = 0; truelight@0: pair = SetupSubsidyDecodeParam(s, 0); truelight@0: AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b); truelight@0: modified = true; truelight@0: break; truelight@0: } truelight@0: } signde@239: } while (n--); truelight@0: } truelight@0: no_add:; truelight@0: if (modified) truelight@0: InvalidateWindow(WC_SUBSIDIES_LIST, 0); truelight@0: } truelight@0: Darkvater@1881: static const SaveLoad _subsidies_desc[] = { rubidium@4344: SLE_VAR(Subsidy, cargo_type, SLE_UINT8), rubidium@4344: SLE_VAR(Subsidy, age, SLE_UINT8), rubidium@4344: SLE_CONDVAR(Subsidy, from, SLE_FILE_U8 | SLE_VAR_U16, 0, 4), rubidium@4344: SLE_CONDVAR(Subsidy, from, SLE_UINT16, 5, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Subsidy, to, SLE_FILE_U8 | SLE_VAR_U16, 0, 4), rubidium@4344: SLE_CONDVAR(Subsidy, to, SLE_UINT16, 5, SL_MAX_VERSION), truelight@0: SLE_END() truelight@0: }; truelight@0: rubidium@6573: static void Save_SUBS() truelight@0: { truelight@0: int i; truelight@0: Subsidy *s; truelight@0: tron@2952: for (i = 0; i != lengthof(_subsidies); i++) { truelight@0: s = &_subsidies[i]; tron@2469: if (s->cargo_type != CT_INVALID) { truelight@0: SlSetArrayIndex(i); truelight@0: SlObject(s, _subsidies_desc); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@6573: static void Load_SUBS() truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) truelight@0: SlObject(&_subsidies[index], _subsidies_desc); truelight@0: } truelight@0: Darkvater@3344: int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, CargoID cargo_type) truelight@0: { peter1138@6417: const CargoSpec *cs = GetCargo(cargo_type); truelight@0: byte f; truelight@0: peter1138@6954: /* Use callback to calculate cargo profit, if available */ peter1138@6954: if (HASBIT(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) { peter1138@6954: uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24); peter1138@6954: uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs); peter1138@6954: if (callback != CALLBACK_FAILED) { peter1138@6954: int result = GB(callback, 0, 14); peter1138@6954: peter1138@6954: /* Simulate a 15 bit signed value */ peter1138@6954: if (HASBIT(callback, 14)) result = 0x4000 - result; peter1138@6954: peter1138@6954: /* "The result should be a signed multiplier that gets multiplied peter1138@6954: * by the amount of cargo moved and the price factor, then gets peter1138@6954: * divided by 8192." */ peter1138@6954: return result * num_pieces * _cargo_payment_rates[cargo_type] / 8192; peter1138@6954: } peter1138@6954: } peter1138@6954: truelight@0: /* zero the distance if it's the bank and very short transport. */ belugas@6683: if (_opt.landscape == LT_TEMPERATE && cs->label == 'VALU' && dist < 10) truelight@0: dist = 0; truelight@0: truelight@0: f = 255; peter1138@6417: if (transit_days > cs->transit_days[0]) { peter1138@6417: transit_days -= cs->transit_days[0]; truelight@0: f -= transit_days; truelight@193: peter1138@6417: if (transit_days > cs->transit_days[1]) { peter1138@6417: transit_days -= cs->transit_days[1]; truelight@0: tron@3017: if (f < transit_days) { truelight@0: f = 0; tron@3017: } else { truelight@0: f -= transit_days; tron@3017: } truelight@0: } truelight@0: } truelight@0: if (f < 31) f = 31; truelight@0: peter1138@6695: return BIGMULSS(dist * f * num_pieces, _cargo_payment_rates[cargo_type], 21); truelight@0: } truelight@0: Darkvater@3344: static void DeliverGoodsToIndustry(TileIndex xy, CargoID cargo_type, int num_pieces) truelight@0: { tron@3017: Industry* best = NULL; tron@3017: Industry* ind; tron@3017: uint u; truelight@0: belugas@6451: /* Check if there's an industry close to the station that accepts the cargo belugas@6451: * XXX - Think of something better to belugas@6451: * 1) Only deliver to industries which are withing the catchment radius belugas@6451: * 2) Distribute between industries if more then one is present */ celestar@3613: u = (_patches.station_spread + 8) * 2; truelight@830: FOR_ALL_INDUSTRIES(ind) { tron@3017: uint t; tron@3017: truelight@4346: if (( cargo_type == ind->accepts_cargo[0] || tron@3017: cargo_type == ind->accepts_cargo[1] || tron@3017: cargo_type == ind->accepts_cargo[2] tron@3017: ) && tron@3017: ind->produced_cargo[0] != CT_INVALID && tron@3017: ind->produced_cargo[0] != cargo_type && celestar@3613: (t = DistanceManhattan(ind->xy, xy)) < u) { truelight@0: u = t; truelight@0: best = ind; truelight@0: } truelight@0: } truelight@0: truelight@0: /* Found one? */ truelight@0: if (best != NULL) { truelight@0: best->was_cargo_delivered = true; truelight@0: best->cargo_waiting[0] = min(best->cargo_waiting[0] + num_pieces, 0xFFFF); truelight@0: } truelight@0: } truelight@0: Darkvater@3344: static bool CheckSubsidised(Station *from, Station *to, CargoID cargo_type) truelight@0: { truelight@0: Subsidy *s; truelight@0: TileIndex xy; truelight@0: Pair pair; truelight@0: Player *p; truelight@0: belugas@6451: /* check if there is an already existing subsidy that applies to us */ tron@2951: for (s = _subsidies; s != endof(_subsidies); s++) { truelight@0: if (s->cargo_type == cargo_type && truelight@0: s->age >= 12 && truelight@0: s->from == from->index && tron@2951: s->to == to->index) { truelight@0: return true; tron@2951: } truelight@0: } truelight@0: truelight@0: /* check if there's a new subsidy that applies.. */ tron@2951: for (s = _subsidies; s != endof(_subsidies); s++) { truelight@0: if (s->cargo_type == cargo_type && s->age < 12) { truelight@0: /* Check distance from source */ peter1138@6641: const CargoSpec *cs = GetCargo(cargo_type); peter1138@6641: if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) { truelight@919: xy = GetTown(s->from)->xy; truelight@0: } else { truelight@919: xy = (GetIndustry(s->from))->xy; truelight@0: } tron@2951: if (DistanceMax(xy, from->xy) > 9) continue; truelight@193: truelight@0: /* Check distance from dest */ peter1138@6641: switch (cs->town_effect) { peter1138@6641: case TE_PASSENGERS: peter1138@6641: case TE_MAIL: peter1138@6641: case TE_GOODS: peter1138@6641: case TE_FOOD: tron@2989: xy = GetTown(s->to)->xy; tron@2989: break; tron@2989: tron@2989: default: tron@2989: xy = GetIndustry(s->to)->xy; tron@2989: break; truelight@0: } tron@2951: if (DistanceMax(xy, to->xy) > 9) continue; truelight@0: truelight@0: /* Found a subsidy, change the values to indicate that it's in use */ truelight@0: s->age = 12; truelight@0: s->from = from->index; truelight@0: s->to = to->index; truelight@0: truelight@0: /* Add a news item */ truelight@0: pair = SetupSubsidyDecodeParam(s, 0); tron@1309: InjectDParam(2); truelight@0: celestar@1962: p = GetPlayer(_current_player); tron@534: SetDParam(0, p->name_1); tron@534: SetDParam(1, p->name_2); truelight@0: AddNewsItem( truelight@193: STR_2031_SERVICE_SUBSIDY_AWARDED + _opt.diff.subsidy_multiplier, truelight@0: NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), tron@2951: pair.a, pair.b tron@2951: ); truelight@0: truelight@0: InvalidateWindow(WC_SUBSIDIES_LIST, 0); truelight@0: return true; truelight@0: } truelight@0: } truelight@0: return false; truelight@0: } truelight@0: celestar@5934: static int32 DeliverGoods(int num_pieces, CargoID cargo_type, StationID source, StationID dest, TileIndex source_tile, byte days_in_transit) truelight@0: { truelight@0: bool subsidised; truelight@0: Station *s_from, *s_to; truelight@0: int32 profit; truelight@0: tron@4077: assert(num_pieces > 0); truelight@0: belugas@6451: /* Update player statistics */ truelight@0: { celestar@1962: Player *p = GetPlayer(_current_player); truelight@0: p->cur_economy.delivered_cargo += num_pieces; truelight@0: SETBIT(p->cargo_types, cargo_type); truelight@0: } truelight@0: belugas@6451: /* Get station pointers. */ truelight@919: s_from = GetStation(source); truelight@919: s_to = GetStation(dest); truelight@0: belugas@6451: /* Check if a subsidy applies. */ truelight@0: subsidised = CheckSubsidised(s_from, s_to, cargo_type); truelight@0: belugas@6451: /* Increase town's counter for some special goods types */ peter1138@6641: const CargoSpec *cs = GetCargo(cargo_type); peter1138@6641: if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces; peter1138@6641: if (cs->town_effect == TE_WATER) s_to->town->new_act_water += num_pieces; truelight@0: belugas@6451: /* Give the goods to the industry. */ truelight@0: DeliverGoodsToIndustry(s_to->xy, cargo_type, num_pieces); truelight@193: belugas@6451: /* Determine profit */ celestar@5934: profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(source_tile, s_to->xy), days_in_transit, cargo_type); truelight@0: belugas@6451: /* Modify profit if a subsidy is in effect */ truelight@0: if (subsidised) { tron@2989: switch (_opt.diff.subsidy_multiplier) { peter1138@3655: case 0: profit += profit >> 1; break; peter1138@3655: case 1: profit *= 2; break; peter1138@3655: case 2: profit *= 3; break; peter1138@3655: default: profit *= 4; break; truelight@0: } truelight@0: } truelight@0: truelight@0: return profit; truelight@0: } truelight@0: tron@523: /* tron@523: * Returns true if Vehicle v should wait loading because other vehicle is tron@523: * already loading the same cargo type tron@523: * v = vehicle to load, u = GetFirstInChain(v) tron@523: */ tron@2951: static bool LoadWait(const Vehicle* v, const Vehicle* u) tron@2951: { tron@523: const Vehicle *w; tron@523: bool has_any_cargo = false; tron@523: tron@555: if (!(u->current_order.flags & OF_FULL_LOAD)) return false; tron@523: tron@523: for (w = u; w != NULL; w = w->next) { tron@523: if (w->cargo_count != 0) { tron@523: if (v->cargo_type == w->cargo_type && tron@2951: u->last_station_visited == w->cargo_source) { tron@523: return false; tron@2951: } tron@523: has_any_cargo = true; tron@523: } tron@523: } tron@523: rubidium@6996: const Station *st = GetStation(u->last_station_visited); rubidium@6996: std::list::const_iterator iter; rubidium@6996: for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) { rubidium@6996: const Vehicle *x = *iter; rubidium@6996: if (!(x->vehstatus & (VS_STOPPED | VS_CRASHED)) && u != x) { tron@523: bool other_has_any_cargo = false; tron@523: bool has_space_for_same_type = false; tron@523: bool other_has_same_type = false; tron@523: tron@523: for (w = x; w != NULL; w = w->next) { tron@2951: if (w->cargo_count < w->cargo_cap && v->cargo_type == w->cargo_type) { tron@523: has_space_for_same_type = true; tron@2951: } tron@523: tron@523: if (w->cargo_count != 0) { tron@523: if (v->cargo_type == w->cargo_type && tron@2951: u->last_station_visited == w->cargo_source) { tron@523: other_has_same_type = true; tron@2951: } tron@523: other_has_any_cargo = true; tron@523: } tron@523: } tron@523: tron@523: if (has_space_for_same_type) { tron@523: if (other_has_same_type) return true; tron@523: if (other_has_any_cargo && !has_any_cargo) return true; tron@523: } tron@523: } tron@523: } tron@523: tron@523: return false; tron@523: } tron@523: rubidium@7076: static bool CanFillVehicle_FullLoadAny(Vehicle *v) rubidium@7076: { rubidium@7076: uint32 full = 0, not_full = 0; rubidium@7076: const GoodsEntry *ge = GetStation(v->last_station_visited)->goods; rubidium@7076: rubidium@7076: /* special handling of aircraft */ rubidium@7076: rubidium@7076: /* if the aircraft carries passengers and is NOT full, then rubidium@7076: *continue loading, no matter how much mail is in */ rubidium@7076: if (v->type == VEH_AIRCRAFT && rubidium@7076: IsCargoInClass(v->cargo_type, CC_PASSENGERS) && rubidium@7076: v->cargo_cap != v->cargo_count) { rubidium@7076: return true; rubidium@7076: } rubidium@7076: rubidium@7076: /* patch should return "true" to continue loading, i.e. when there is no cargo type that is fully loaded. */ rubidium@7076: do { rubidium@7076: /* Should never happen, but just in case future additions change this */ rubidium@7076: assert(v->cargo_type<32); rubidium@7076: rubidium@7076: if (v->cargo_cap != 0) { rubidium@7076: uint32 mask = 1 << v->cargo_type; rubidium@7076: rubidium@7076: if (!HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) && v->cargo_cap == v->cargo_count) { rubidium@7076: full |= mask; rubidium@7076: } else if (GB(ge[v->cargo_type].waiting_acceptance, 0, 12) > 0 || rubidium@7076: (HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) && (ge[v->cargo_type].waiting_acceptance & 0x8000))) { rubidium@7076: /* If there is any cargo waiting, or this vehicle is still unloading rubidium@7076: * and the station accepts the cargo, don't leave the station. */ rubidium@7076: return true; rubidium@7076: } else { rubidium@7076: not_full |= mask; rubidium@7076: } rubidium@7076: } rubidium@7076: } while ((v = v->next) != NULL); rubidium@7076: rubidium@7076: /* continue loading if there is a non full cargo type and no cargo type that is full */ rubidium@7076: return not_full && (full & ~not_full) == 0; rubidium@7076: } rubidium@7076: rubidium@7076: rubidium@7076: static bool CanFillVehicle(Vehicle *front_v) rubidium@7076: { rubidium@7076: TileIndex tile = front_v->tile; rubidium@7076: rubidium@7076: assert(IsTileType(tile, MP_STATION) || rubidium@7076: (front_v->type == VEH_SHIP && ( rubidium@7076: IsTileType(TILE_ADDXY(tile, 1, 0), MP_STATION) || rubidium@7076: IsTileType(TILE_ADDXY(tile, -1, 0), MP_STATION) || rubidium@7076: IsTileType(TILE_ADDXY(tile, 0, 1), MP_STATION) || rubidium@7076: IsTileType(TILE_ADDXY(tile, 0, -1), MP_STATION) || rubidium@7076: IsTileType(TILE_ADDXY(tile, -2, 0), MP_STATION) rubidium@7076: ))); rubidium@7076: rubidium@7076: bool full_load = HASBIT(front_v->current_order.flags, OFB_FULL_LOAD); rubidium@7076: rubidium@7076: /* If patch is active, use alternative CanFillVehicle-function */ rubidium@7076: if (_patches.full_load_any && full_load) return CanFillVehicle_FullLoadAny(front_v); rubidium@7076: rubidium@7076: Vehicle *v = front_v; rubidium@7076: do { rubidium@7076: if (HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) || (full_load && v->cargo_count != v->cargo_cap)) return true; rubidium@7076: } while ((v = v->next) != NULL); rubidium@7076: rubidium@7076: return !HASBIT(front_v->vehicle_flags, VF_LOADING_FINISHED); rubidium@7076: } rubidium@7076: rubidium@7055: /** rubidium@7055: * Performs the vehicle payment _and_ marks the vehicle to be unloaded. rubidium@7055: * @param front_v the vehicle to be unloaded rubidium@7055: */ rubidium@7061: void VehiclePayment(Vehicle *front_v) rubidium@7055: { rubidium@7055: int result = 0; rubidium@7055: rubidium@7055: int profit = 0; rubidium@7055: int total_veh_profit = 0; // accumulates the profit across the vehicle chain (used by trains) rubidium@7055: int32 route_profit = 0; // the grand total amount for the route. A-D of transfer chain A-B-C-D rubidium@7055: int virtual_profit = 0; // virtual profit of one vehicle element for feeder systems rubidium@7055: int virtual_profit_total = 0; // virtual profit for entire vehicle chain rubidium@7055: int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step rubidium@7055: rubidium@7055: int all_vehicles_cargo_feeder_share = front_v->cargo_feeder_share; // used to hold transfer value of complete vehicle chain - used by trains rubidium@7055: rubidium@7055: StationID last_visited = front_v->last_station_visited; rubidium@7055: Station *st = GetStation(last_visited); rubidium@7055: rubidium@7061: /* The owner of the train wants to be paid */ rubidium@7061: PlayerID old_player = _current_player; rubidium@7061: _current_player = front_v->owner; rubidium@7061: rubidium@7061: /* At this moment loading cannot be finished */ rubidium@7061: CLRBIT(front_v->vehicle_flags, VF_LOADING_FINISHED); rubidium@7061: rubidium@7061: /* Start unloading in at the first possible moment */ rubidium@7061: front_v->load_unload_time_rem = 1; rubidium@7061: rubidium@7055: for (Vehicle *v = front_v; v != NULL; v = v->next) { rubidium@7061: /* No cargo to unload */ rubidium@7055: if (v->cargo_cap == 0) continue; rubidium@7055: rubidium@7055: SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING); rubidium@7061: /* All cargo has already been paid for, no need to pay again */ rubidium@7055: if (v->cargo_count == v->cargo_paid_for) continue; rubidium@7055: rubidium@7055: GoodsEntry *ge = &st->goods[v->cargo_type]; rubidium@7055: rubidium@7055: if (v->cargo_source != last_visited && rubidium@7055: HASBIT(ge->waiting_acceptance, 15) && rubidium@7055: (front_v->current_order.flags & OF_TRANSFER) == 0) { rubidium@7055: /* Deliver goods to the station */ rubidium@7055: st->time_since_unload = 0; rubidium@7055: rubidium@7055: /* handle end of route payment */ rubidium@7055: profit += DeliverGoods(v->cargo_count - v->cargo_paid_for, v->cargo_type, v->cargo_source, last_visited, v->cargo_source_xy, v->cargo_days); rubidium@7055: v->cargo_paid_for = v->cargo_count; rubidium@7055: route_profit = profit; // display amount paid for final route delivery, A-D of a chain A-B-C-D rubidium@7055: total_veh_profit = profit - all_vehicles_cargo_feeder_share; // whole vehicle is not payed for transfers picked up earlier rubidium@7055: total_cargo_feeder_share = -all_vehicles_cargo_feeder_share; // total of transfer fees in vehicle chain needs to be zero at end of unload rubidium@7055: rubidium@7055: v->cargo_feeder_share = 0; // clear transfer cost per vehicle rubidium@7055: result |= 1; rubidium@7055: } else if (front_v->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) { rubidium@7055: if ((front_v->current_order.flags & OF_TRANSFER) != 0) { rubidium@7055: virtual_profit = GetTransportedGoodsIncome( rubidium@7055: v->cargo_count - v->cargo_paid_for, rubidium@7055: /* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */ rubidium@7055: DistanceManhattan(v->cargo_loaded_at_xy, GetStation(last_visited)->xy), rubidium@7055: v->cargo_days, rubidium@7055: v->cargo_type); rubidium@7055: rubidium@7097: front_v->profit_this_year += virtual_profit; rubidium@7097: ge->feeder_profit += v->cargo_feeder_share + virtual_profit; // transfer cargo transfer fees to station rubidium@7097: total_cargo_feeder_share -= v->cargo_feeder_share; // accumulate deduction of feeder shares rubidium@7097: v->cargo_feeder_share = 0; // clear transfer cost rubidium@7055: rubidium@7055: /* keep total of cargo unloaded (pending) for accurate cargoshare calculation on load */ rubidium@7055: SB(ge->unload_pending, 0, 12, GB(ge->unload_pending, 0, 12) + v->cargo_count); rubidium@7055: rubidium@7055: virtual_profit_total += virtual_profit; // accumulate transfer profits for whole vehicle rubidium@7055: v->cargo_paid_for = v->cargo_count; // record how much of the cargo has been paid for to eliminate double counting rubidium@7055: } rubidium@7055: result |= 2; rubidium@7055: } rubidium@7055: } rubidium@7055: rubidium@7055: /* Ensure a negative total is only applied to the vehicle if there is value to reduce. */ rubidium@7055: front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0); rubidium@7055: rubidium@7055: if (virtual_profit_total > 0) { rubidium@7055: ShowFeederIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, virtual_profit_total); rubidium@7055: } rubidium@7055: rubidium@7055: if (route_profit != 0) { rubidium@7055: front_v->profit_this_year += total_veh_profit; rubidium@7055: SubtractMoneyFromPlayer(-route_profit); rubidium@7055: rubidium@7055: if (IsLocalPlayer() && !PlayVehicleSound(front_v, VSE_LOAD_UNLOAD)) { rubidium@7055: SndPlayVehicleFx(SND_14_CASHTILL, front_v); rubidium@7055: } rubidium@7055: rubidium@7055: ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -total_veh_profit); rubidium@7055: } rubidium@7055: rubidium@7061: _current_player = old_player; rubidium@7055: } rubidium@7055: rubidium@7076: /** rubidium@7076: * Loads/unload the vehicle if possible. rubidium@7076: * @param v the vehicle to be (un)loaded rubidium@7076: * @return true if something was (un)loaded. False if the train is ready to leave. rubidium@7076: */ rubidium@7076: bool LoadUnloadVehicle(Vehicle *v) truelight@0: { rubidium@7076: if (!CanFillVehicle(v)) return false; rubidium@7076: truelight@0: int unloading_time = 20; truelight@0: Vehicle *u = v; truelight@0: int result = 0; truelight@0: int t; truelight@0: uint count, cap; rubidium@7061: tron@445: bool completely_empty = true; peter1138@5211: byte load_amount; peter1138@5251: bool anything_loaded = false; rubidium@7055: int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step truelight@0: tron@555: assert(v->current_order.type == OT_LOADING); truelight@0: truelight@0: v->cur_speed = 0; peter1138@5251: peter1138@5251: /* Loading can only have finished when all the cargo has been unloaded, and peter1138@5251: * there is nothing left to load. It's easier to clear this if the peter1138@5251: * conditions haven't been met than attempting to check them all before peter1138@5251: * enabling though. */ maedhros@6501: SETBIT(v->vehicle_flags, VF_LOADING_FINISHED); truelight@1539: richk@6524: StationID last_visited = v->last_station_visited; richk@6524: Station *st = GetStation(last_visited); richk@6524: tron@499: for (; v != NULL; v = v->next) { tron@2639: GoodsEntry* ge; peter1138@5211: load_amount = EngInfo(v->engine_type)->load_amount; maedhros@6553: if (_patches.gradual_loading && HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_LOAD_AMOUNT)) { peter1138@5211: uint16 cb_load_amount = GetVehicleCallback(CBID_VEHICLE_LOAD_AMOUNT, 0, 0, v->engine_type, v); peter1138@5211: if (cb_load_amount != CALLBACK_FAILED) load_amount = cb_load_amount & 0xFF; peter1138@5211: } tron@2639: tron@499: if (v->cargo_cap == 0) continue; truelight@193: truelight@0: ge = &st->goods[v->cargo_type]; peter1138@5211: count = GB(ge->waiting_acceptance, 0, 12); truelight@0: truelight@0: /* unload? */ maedhros@6501: if (v->cargo_count != 0 && HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING)) { peter1138@5251: uint16 amount_unloaded = _patches.gradual_loading ? min(v->cargo_count, load_amount) : v->cargo_count; peter1138@5251: maedhros@6501: CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED); peter1138@5251: tron@2639: if (v->cargo_source != last_visited && ge->waiting_acceptance & 0x8000 && !(u->current_order.flags & OF_TRANSFER)) { belugas@6451: /* deliver goods to the station */ truelight@1416: st->time_since_unload = 0; truelight@1416: belugas@6451: unloading_time += v->cargo_count; // TTDBUG: bug in original TTD richk@6524: truelight@0: result |= 1; peter1138@5251: v->cargo_count -= amount_unloaded; maedhros@6139: v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for); peter1138@5251: if (_patches.gradual_loading) continue; richk@6524: tron@2639: } else if (u->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) { truelight@0: /* unload goods and let it wait at the station */ truelight@0: st->time_since_unload = 0; richk@6524: celestar@1935: unloading_time += v->cargo_count; tron@2504: t = GB(ge->waiting_acceptance, 0, 12); tron@2504: if (t == 0) { belugas@6451: /* No goods waiting at station */ truelight@0: ge->enroute_time = v->cargo_days; truelight@0: ge->enroute_from = v->cargo_source; celestar@5934: ge->enroute_from_xy = v->cargo_source_xy; truelight@0: } else { belugas@6451: /* Goods already waiting at station. Set counters to the worst value. */ celestar@5934: if (v->cargo_days >= ge->enroute_time) ge->enroute_time = v->cargo_days; celestar@5934: celestar@5934: if (last_visited != ge->enroute_from) { truelight@0: ge->enroute_from = v->cargo_source; celestar@5934: ge->enroute_from_xy = v->cargo_source_xy; celestar@5934: } truelight@0: } belugas@6451: /* Update amount of waiting cargo */ peter1138@5211: SB(ge->waiting_acceptance, 0, 12, min(amount_unloaded + t, 0xFFF)); peter1138@4814: richk@6524: /* if there is not enough to unload from pending, ensure it does not go -ve richk@6524: * else deduct amount actually unloaded from unload_pending */ rubidium@6525: SB(ge->unload_pending, 0, 12, max(GB(ge->unload_pending, 0, 12) - amount_unloaded, 0U)); richk@6524: truelight@0: result |= 2; peter1138@5211: v->cargo_count -= amount_unloaded; maedhros@6139: v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for); peter1138@5251: if (_patches.gradual_loading) continue; truelight@0: } tron@445: tron@2951: if (v->cargo_count != 0) completely_empty = false; truelight@0: } truelight@193: peter1138@5251: /* The vehicle must have been unloaded because it is either empty, or maedhros@6501: * the UNLOADING bit is already clear in v->vehicle_flags. */ maedhros@6501: CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING); maedhros@6139: maedhros@6139: /* We cannot have paid for more cargo than there is on board. */ maedhros@6139: assert(v->cargo_paid_for <= v->cargo_count); peter1138@5251: truelight@0: /* don't pick up goods that we unloaded */ tron@555: if (u->current_order.flags & OF_UNLOAD) continue; truelight@0: truelight@0: /* update stats */ truelight@0: ge->days_since_pickup = 0; tron@2989: switch (u->type) { rubidium@6585: case VEH_TRAIN: t = u->u.rail.cached_max_speed; break; rubidium@6585: case VEH_ROAD: t = u->max_speed / 2; break; tron@2989: default: t = u->max_speed; break; tron@2989: } truelight@193: belugas@6451: /* if last speed is 0, we treat that as if no vehicle has ever visited the station. */ tron@2989: ge->last_speed = min(t, 255); rubidium@4329: ge->last_age = _cur_year - v->build_year; truelight@0: belugas@6451: /* If there's goods waiting at the station, and the vehicle belugas@6451: * has capacity for it, load it on the vehicle. */ tron@2504: if (count != 0 && truelight@0: (cap = v->cargo_cap - v->cargo_count) != 0) { celestar@1935: celestar@5934: if (v->cargo_count == 0) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO); tron@445: tron@523: /* Skip loading this vehicle if another train/vehicle is already handling tron@523: * the same cargo type at this station */ rubidium@7061: if (_patches.improved_load && (u->current_order.flags & OF_FULL_LOAD) && LoadWait(v,u)) { rubidium@7061: CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED); rubidium@7061: continue; rubidium@7061: } tron@523: tron@445: /* TODO: Regarding this, when we do gradual loading, we tron@445: * should first unload all vehicles and then start tron@445: * loading them. Since this will cause tron@445: * VEHICLE_TRIGGER_EMPTY to be called at the time when tron@445: * the whole vehicle chain is really totally empty, the belugas@6451: * completely_empty assignment can then be safely tron@445: * removed; that's how TTDPatch behaves too. --pasky */ tron@445: completely_empty = false; peter1138@5251: anything_loaded = true; tron@445: truelight@0: if (cap > count) cap = count; peter1138@5211: if (_patches.gradual_loading) cap = min(cap, load_amount); maedhros@6501: if (cap < count) CLRBIT(u->vehicle_flags, VF_LOADING_FINISHED); richk@6524: richk@6524: /* cargoshare is proportioned by the amount due to unload richk@6524: * Otherwise, with gradual loading, 100% of credits would be taken immediately, richk@6524: * even if the cargo volume represents a tiny percent of the whole. richk@6524: * ge->unload_pending holds the amount that has been credited, but has not yet been unloaded. richk@6524: */ richk@6524: int cargoshare = cap * 10000 / (ge->waiting_acceptance + ge->unload_pending); richk@6524: int feeder_profit_share = ge->feeder_profit * cargoshare / 10000; truelight@0: v->cargo_count += cap; truelight@0: ge->waiting_acceptance -= cap; richk@6524: richk@6524: total_cargo_feeder_share += feeder_profit_share; // store cost for later payment when cargo unloaded richk@6524: v->cargo_loaded_at_xy = st->xy; // retains location of where the cargo was picked up for intermediate payments richk@6524: celestar@1935: ge->feeder_profit -= feeder_profit_share; truelight@0: unloading_time += cap; truelight@0: st->time_since_load = 0; truelight@193: belugas@6451: /* And record the source of the cargo, and the days in travel. */ truelight@2815: v->cargo_source = ge->enroute_from; celestar@5934: v->cargo_source_xy = ge->enroute_from_xy; truelight@0: v->cargo_days = ge->enroute_time; truelight@0: result |= 2; celestar@3580: st->last_vehicle_type = v->type; truelight@0: } truelight@0: } truelight@0: peter1138@5211: v = u; celestar@1935: rubidium@7055: v->cargo_feeder_share += total_cargo_feeder_share; richk@6524: peter1138@5211: if (_patches.gradual_loading) { peter1138@5211: /* The time it takes to load one 'slice' of cargo or passengers depends peter1138@5211: * on the vehicle type - the values here are those found in TTDPatch */ peter1138@5211: uint gradual_loading_wait_time[] = { 40, 20, 10, 20 }; peter1138@5211: bjarni@6206: unloading_time = gradual_loading_wait_time[v->type]; maedhros@6501: if (HASBIT(v->vehicle_flags, VF_LOADING_FINISHED)) { peter1138@5251: if (anything_loaded) { peter1138@5251: unloading_time += 20; peter1138@5251: } else { peter1138@5251: unloading_time = 20; peter1138@5251: } peter1138@5251: } peter1138@5211: } truelight@0: rubidium@6585: if (v->type == VEH_TRAIN) { belugas@6451: /* Each platform tile is worth 2 rail vehicles. */ celestar@6324: int overhang = v->u.rail.cached_total_length - st->GetPlatformLength(v->tile) * TILE_SIZE; peter1138@2587: if (overhang > 0) { peter1138@2587: unloading_time <<= 1; peter1138@2587: unloading_time += (overhang * unloading_time) / 8; truelight@0: } truelight@0: } truelight@0: truelight@0: v->load_unload_time_rem = unloading_time; truelight@0: tron@445: if (completely_empty) { tron@445: TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY); tron@445: } tron@445: truelight@0: if (result != 0) { rubidium@7061: InvalidateWindow(v->GetVehicleListWindowClass(), v->owner); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); rubidium@7061: KUDr@5916: st->MarkTilesDirty(); rubidium@7061: v->MarkDirty(); truelight@0: tron@2951: if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited); truelight@0: } truelight@0: rubidium@7076: return true; truelight@0: } truelight@0: rubidium@6573: void PlayersMonthlyLoop() truelight@0: { truelight@0: PlayersGenStatistics(); rubidium@4293: if (_patches.inflation && _cur_year < MAX_YEAR) truelight@0: AddInflation(); truelight@0: PlayersPayInterest(); belugas@6451: /* Reset the _current_player flag */ signde@206: _current_player = OWNER_NONE; truelight@0: HandleEconomyFluctuations(); truelight@0: SubsidyMonthlyHandler(); truelight@0: } truelight@0: truelight@0: static void DoAcquireCompany(Player *p) truelight@0: { truelight@0: Player *owner; rubidium@5838: int i; truelight@200: int64 value; truelight@0: tron@534: SetDParam(0, p->name_1); tron@534: SetDParam(1, p->name_2); tron@534: SetDParam(2, p->bankrupt_value); Darkvater@4873: AddNewsItem( (StringID)(_current_player | NB_BMERGER), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0); truelight@0: belugas@6451: /* original code does this a little bit differently */ rubidium@5838: PlayerID pi = p->index; truelight@6588: ChangeNetworkOwner(pi, _current_player); truelight@0: ChangeOwnershipOfPlayerItems(pi, _current_player); truelight@0: truelight@0: if (p->bankrupt_value == 0) { celestar@1962: owner = GetPlayer(_current_player); truelight@0: owner->current_loan += p->current_loan; truelight@0: } truelight@0: truelight@0: value = CalculateCompanyValue(p) >> 2; tron@2952: for (i = 0; i != 4; i++) { Darkvater@4848: if (p->share_owners[i] != PLAYER_SPECTATOR) { celestar@1962: owner = GetPlayer(p->share_owners[i]); truelight@0: owner->money64 += value; truelight@0: owner->yearly_expenses[0][EXPENSES_OTHER] += value; truelight@0: UpdatePlayerMoney32(owner); truelight@0: } truelight@0: } truelight@0: truelight@0: p->is_active = false; truelight@0: truelight@0: DeletePlayerWindows(pi); rubidium@4434: RebuildVehicleLists(); //Updates the open windows to add the newly acquired vehicles to the lists truelight@0: } truelight@0: rubidium@5838: extern int GetAmountOwnedBy(const Player *p, PlayerID owner); truelight@599: Darkvater@1793: /** Acquire shares in an opposing company. tron@3491: * @param tile unused belugas@6928: * @param flags type of operation Darkvater@1793: * @param p1 player to buy the shares from Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdBuyShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Player *p; truelight@200: int64 cost; Darkvater@1793: Darkvater@1793: /* Check if buying shares is allowed (protection against modified clients */ Darkvater@4850: if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); rubidium@5838: p = GetPlayer((PlayerID)p1); tron@1019: Darkvater@1793: /* Protect new companies from hostile takeovers */ rubidium@4329: if (_cur_year - p->inaugurated_year < 6) return_cmd_error(STR_7080_PROTECTED); darkvater@930: truelight@599: /* Those lines are here for network-protection (clients can be slow) */ Darkvater@4848: if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 0) return 0; darkvater@930: Darkvater@1793: /* We can not buy out a real player (temporarily). TODO: well, enable it obviously */ Darkvater@4848: if (GetAmountOwnedBy(p, PLAYER_SPECTATOR) == 1 && !p->is_ai) return 0; tron@1019: truelight@0: cost = CalculateCompanyValue(p) >> 2; truelight@0: if (flags & DC_EXEC) { rubidium@5838: PlayerByte* b = p->share_owners; Darkvater@1793: int i; Darkvater@1793: Darkvater@4848: while (*b != PLAYER_SPECTATOR) b++; /* share owners is guaranteed to contain at least one PLAYER_SPECTATOR */ truelight@0: *b = _current_player; truelight@0: Darkvater@1793: for (i = 0; p->share_owners[i] == _current_player;) { truelight@0: if (++i == 4) { truelight@0: p->bankrupt_value = 0; truelight@0: DoAcquireCompany(p); truelight@0: break; truelight@0: } truelight@0: } tron@3017: InvalidateWindow(WC_COMPANY, p1); truelight@0: } truelight@0: return cost; truelight@0: } truelight@0: Darkvater@1793: /** Sell shares in an opposing company. tron@3491: * @param tile unused belugas@6928: * @param flags type of operation Darkvater@1793: * @param p1 player to sell the shares from Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Player *p; truelight@200: int64 cost; Darkvater@1793: Darkvater@1793: /* Check if buying shares is allowed (protection against modified clients */ Darkvater@4850: if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); rubidium@5838: p = GetPlayer((PlayerID)p1); truelight@0: truelight@653: /* Those lines are here for network-protection (clients can be slow) */ Darkvater@1793: if (GetAmountOwnedBy(p, _current_player) == 0) return 0; truelight@653: truelight@0: /* adjust it a little to make it less profitable to sell and buy */ truelight@0: cost = CalculateCompanyValue(p) >> 2; truelight@0: cost = -(cost - (cost >> 7)); truelight@0: truelight@0: if (flags & DC_EXEC) { rubidium@5838: PlayerByte* b = p->share_owners; belugas@6451: while (*b != _current_player) b++; // share owners is guaranteed to contain player Darkvater@4848: *b = PLAYER_SPECTATOR; tron@3017: InvalidateWindow(WC_COMPANY, p1); truelight@0: } truelight@0: return cost; truelight@0: } truelight@0: Darkvater@1793: /** Buy up another company. Darkvater@1793: * When a competing company is gone bankrupt you get the chance to purchase Darkvater@1793: * that company. Darkvater@1793: * @todo currently this only works for AI players tron@3491: * @param tile unused belugas@6928: * @param flags type of operation Darkvater@1793: * @param p1 player/company to buy up Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdBuyCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Player *p; celestar@6901: PlayerID pid = (PlayerID)p1; Darkvater@1793: Darkvater@1793: /* Disable takeovers in multiplayer games */ celestar@6901: if (!IsValidPlayer(pid) || _networking) return CMD_ERROR; celestar@6901: celestar@6901: /* Do not allow players to take over themselves */ celestar@6901: if (pid == _current_player) return CMD_ERROR; Darkvater@1793: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); celestar@6901: p = GetPlayer(pid); Darkvater@1793: Darkvater@1793: if (!p->is_ai) return CMD_ERROR; Darkvater@1793: Darkvater@1793: if (flags & DC_EXEC) { truelight@0: DoAcquireCompany(p); truelight@0: } truelight@0: return p->bankrupt_value; truelight@0: } truelight@0: belugas@6451: /** Prices */ rubidium@6573: static void SaveLoad_PRIC() truelight@0: { rubidium@4344: SlArray(&_price, NUM_PRICES, SLE_INT32); truelight@0: SlArray(&_price_frac, NUM_PRICES, SLE_UINT16); truelight@0: } truelight@0: belugas@6451: /** Cargo payment rates */ rubidium@6573: static void SaveLoad_CAPR() truelight@0: { peter1138@6959: uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO; peter1138@6959: SlArray(&_cargo_payment_rates, num_cargo, SLE_INT32); peter1138@6959: SlArray(&_cargo_payment_rates_frac, num_cargo, SLE_UINT16); truelight@0: } truelight@0: Darkvater@1881: static const SaveLoad _economy_desc[] = { rubidium@4344: SLE_VAR(Economy, max_loan, SLE_INT32), rubidium@4344: SLE_VAR(Economy, max_loan_unround, SLE_INT32), rubidium@4344: SLE_VAR(Economy, fluct, SLE_FILE_I16 | SLE_VAR_I32), rubidium@4344: SLE_VAR(Economy, interest_rate, SLE_UINT8), rubidium@4344: SLE_VAR(Economy, infl_amount, SLE_UINT8), rubidium@4344: SLE_VAR(Economy, infl_amount_pr, SLE_UINT8), truelight@0: SLE_END() truelight@0: }; truelight@0: belugas@6451: /** Economy variables */ rubidium@6573: static void SaveLoad_ECMY() truelight@0: { Darkvater@1881: SlObject(&_economy, _economy_desc); truelight@0: } truelight@0: rubidium@5838: extern const ChunkHandler _economy_chunk_handlers[] = { truelight@0: { 'PRIC', SaveLoad_PRIC, SaveLoad_PRIC, CH_RIFF | CH_AUTO_LENGTH}, truelight@0: { 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH}, rubidium@4344: { 'SUBS', Save_SUBS, Load_SUBS, CH_ARRAY}, truelight@0: { 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST}, truelight@0: };