tron@2186: /* $Id$ */ tron@2186: belugas@6125: /** @file economy.cpp */ belugas@6125: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2291: #include "currency.h" tron@2163: #include "functions.h" maedhros@6453: #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@5469: #include "network/network.h" tron@337: #include "sound.h" tron@445: #include "engine.h" rubidium@5469: #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@5854: #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@6091: #include "cargotype.h" rubidium@6190: #include "player_face.h" rubidium@6643: #include "group.h" truelight@0: belugas@6125: /* 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@5587: 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@6259: if (v->type == VEH_TRAIN || rubidium@6259: v->type == VEH_ROAD || rubidium@6259: (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) || rubidium@6259: 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@5601: return max(value, 1LL); truelight@0: } truelight@0: belugas@6125: /** if update is set to true, the economy is updated with this score belugas@6125: * (also the house is updated, should only be true in the on-tick event) belugas@6125: * @param update the economy with calculated score belugas@6125: * @param p player been evaluated belugas@6125: * @return actual score of this player belugas@6125: * */ 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@6259: if ((v->type == VEH_TRAIN && IsFrontEngine(v)) || rubidium@6259: v->type == VEH_ROAD || rubidium@6259: (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) || rubidium@6259: 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@6125: /* 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@5587: for (ScoreID i = SCORE_BEGIN; i < SCORE_END; i++) { belugas@6125: /* Skip the total */ dominik@116: if (i == SCORE_TOTAL) continue; belugas@6125: /* 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@6125: /* 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@6125: /* 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@6403: celestar@6405: assert(old_player != new_player); celestar@6405: truelight@6403: { truelight@6403: Player *p; truelight@6403: uint i; truelight@6403: truelight@6403: /* See if the old_player had shares in other companies */ truelight@6403: _current_player = old_player; truelight@6403: FOR_ALL_PLAYERS(p) { truelight@6403: for (i = 0; i < 4; i++) { truelight@6403: if (p->share_owners[i] == old_player) { truelight@6403: /* Sell his shares */ truelight@6403: int32 res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY); truelight@6403: /* Because we are in a DoCommand, we can't just execute an other one and truelight@6403: * expect the money to be removed. We need to do it ourself! */ truelight@6403: SubtractMoneyFromPlayer(res); truelight@6403: } truelight@6403: } truelight@6403: } truelight@6403: truelight@6403: /* Sell all the shares that people have on this company */ truelight@6403: p = GetPlayer(old_player); truelight@6403: for (i = 0; i < 4; i++) { truelight@6403: _current_player = p->share_owners[i]; truelight@6403: if (_current_player != PLAYER_SPECTATOR) { truelight@6403: /* Sell the shares */ truelight@6403: int32 res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY); truelight@6403: /* Because we are in a DoCommand, we can't just execute an other one and truelight@6403: * expect the money to be removed. We need to do it ourself! */ truelight@6403: SubtractMoneyFromPlayer(res); truelight@6403: } truelight@6403: } truelight@6403: } truelight@6403: 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@6125: /* 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@6259: case VEH_TRAIN: if (IsFrontEngine(v)) num_train++; break; rubidium@6259: case VEH_ROAD: num_road++; break; rubidium@6259: case VEH_SHIP: num_ship++; break; rubidium@6259: 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@6259: 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; rubidium@6643: v->group_id = DEFAULT_GROUP; bjarni@4621: if (IsEngineCountable(v)) GetPlayer(new_player)->num_engines[v->engine_type]++; tron@2989: switch (v->type) { rubidium@6259: case VEH_TRAIN: if (IsFrontEngine(v)) v->unitnumber = ++num_train; break; rubidium@6259: case VEH_ROAD: v->unitnumber = ++num_road; break; rubidium@6259: case VEH_SHIP: v->unitnumber = ++num_ship; break; rubidium@6259: case VEH_AIRCRAFT: if (IsNormalAircraft(v)) v->unitnumber = ++num_aircraft; break; rubidium@6621: default: NOT_REACHED(); tron@2989: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@6125: /* 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@6262: static void ChangeNetworkOwner(PlayerID current_player, PlayerID new_player) truelight@6262: { truelight@6262: #ifdef ENABLE_NETWORK truelight@6262: if (!_networking) return; truelight@6262: truelight@6262: if (current_player == _local_player) { truelight@6262: _network_playas = new_player; truelight@6262: SetLocalPlayer(new_player); truelight@6262: } truelight@6262: truelight@6262: if (!_network_server) return; truelight@6262: truelight@6262: /* The server has to handle all administrative issues, for example truelight@6262: * updating and notifying all clients of what has happened */ truelight@6262: NetworkTCPSocketHandler *cs; truelight@6262: NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX); truelight@6262: truelight@6262: /* The server has just changed from player */ truelight@6262: if (current_player == ci->client_playas) { truelight@6262: ci->client_playas = new_player; truelight@6262: NetworkUpdateClientInfo(NETWORK_SERVER_INDEX); truelight@6262: } truelight@6262: truelight@6262: /* Find all clients that were in control of this company, and mark them as new_player */ truelight@6262: FOR_ALL_CLIENTS(cs) { truelight@6262: ci = DEREF_CLIENT_INFO(cs); truelight@6262: if (current_player == ci->client_playas) { truelight@6262: ci->client_playas = new_player; truelight@6262: NetworkUpdateClientInfo(ci->client_index); truelight@6262: } truelight@6262: } truelight@6262: #endif /* ENABLE_NETWORK */ truelight@6262: } truelight@6262: truelight@0: static void PlayersCheckBankrupt(Player *p) truelight@0: { tron@2475: PlayerID owner; truelight@200: int64 val; truelight@0: belugas@6125: /* 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@6125: /* Check if the company has any value.. if not, declare it bankrupt belugas@6125: * 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@6125: /* Else, falltrue to case 4... */ truelight@0: } truelight@543: case 4: { belugas@6125: /* Close everything the owner has open */ truelight@543: DeletePlayerWindows(owner); truelight@0: belugas@6125: /* 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@6262: 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@5587: p = GetPlayer((PlayerID)GB(WP(w,news_d).ni->string_id, 0, 4)); truelight@0: DrawPlayerFace(p->face, p->player_color, 2, 23); peter1138@5668: 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@5587: 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@6247: 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@5426: memmove(&p->old_economy[1], &p->old_economy[0], sizeof(p->old_economy) - sizeof(p->old_economy[0])); Darkvater@5426: p->old_economy[0] = p->cur_economy; truelight@0: memset(&p->cur_economy, 0, sizeof(p->cur_economy)); truelight@0: Darkvater@5426: 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@5426: 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@6119: int64 tmp = (int64)*value * amt + *frac; tron@6119: *frac = GB(tmp, 0, 16); tron@6119: *value += tmp >> 16; truelight@0: } truelight@0: rubidium@6247: static void AddInflation() truelight@0: { tron@6119: /* Approximation for (100 + infl_amount)% ** (1 / 12) - 100% tron@6119: * scaled by 65536 tron@6119: * 12 -> months per year tron@6119: * This is only a good approxiamtion for small values tron@6119: */ truelight@0: int32 inf = _economy.infl_amount * 54; truelight@0: tron@6119: 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@6350: 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@6247: 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@6247: 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@6125: 100, ///< station_value belugas@6125: 100, ///< build_rail belugas@6125: 95, ///< build_road belugas@6125: 65, ///< build_signals belugas@6125: 275, ///< build_bridge belugas@6125: 600, ///< build_train_depot belugas@6125: 500, ///< build_road_depot belugas@6125: 700, ///< build_ship_depot belugas@6125: 450, ///< build_tunnel belugas@6125: 200, ///< train_station_track belugas@6125: 180, ///< train_station_length belugas@6125: 600, ///< build_airport belugas@6125: 200, ///< build_bus_station belugas@6125: 200, ///< build_truck_station belugas@6125: 350, ///< build_dock belugas@6125: 400000, ///< build_railvehicle belugas@6125: 2000, ///< build_railwagon belugas@6125: 700000, ///< aircraft_base belugas@6125: 14000, ///< roadveh_base belugas@6125: 65000, ///< ship_base belugas@6125: 20, ///< build_trees belugas@6125: 250, ///< terraform belugas@6125: 20, ///< clear_1 belugas@6125: 40, ///< purchase_land belugas@6125: 200, ///< clear_2 belugas@6125: 500, ///< clear_3 belugas@6125: 20, ///< remove_trees belugas@6125: -70, ///< remove_rail belugas@6125: 10, ///< remove_signals belugas@6125: 50, ///< clear_bridge belugas@6125: 80, ///< remove_train_depot belugas@6125: 80, ///< remove_road_depot belugas@6125: 90, ///< remove_ship_depot belugas@6125: 30, ///< clear_tunnel belugas@6125: 10000, ///< clear_water belugas@6125: 50, ///< remove_rail_station belugas@6125: 30, ///< remove_airport belugas@6125: 50, ///< remove_bus_station belugas@6125: 50, ///< remove_truck_station belugas@6125: 55, ///< remove_dock belugas@6125: 1600, ///< remove_house belugas@6125: 40, ///< remove_road belugas@6125: 5600, ///< running_rail[0] railroad belugas@6125: 5200, ///< running_rail[1] monorail belugas@6125: 4800, ///< running_rail[2] maglev belugas@6125: 9600, ///< aircraft_running belugas@6125: 1600, ///< roadveh_running belugas@6125: 5600, ///< ship_running belugas@6125: 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@6247: void ResetPriceBaseMultipliers() peter1138@2506: { peter1138@2508: uint i; peter1138@2506: belugas@6125: /* 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@6247: 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@6091: const CargoSpec *cs = GetCargo(s->cargo_type); peter1138@6091: SetDParam(0, mode ? cs->name_plural : cs->name); truelight@0: truelight@0: if (s->age < 12) { peter1138@6315: 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@6315: 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@5378: void DeleteSubsidyWithTown(TownID index) rubidium@5378: { rubidium@5378: Subsidy *s; rubidium@5378: rubidium@5378: for (s = _subsidies; s != endof(_subsidies); s++) { peter1138@6315: if (s->cargo_type != CT_INVALID && s->age < 12) { peter1138@6315: const CargoSpec *cs = GetCargo(s->cargo_type); peter1138@6315: if (((cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) && (index == s->from || index == s->to)) || peter1138@6315: ((cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) && index == s->to)) { peter1138@6315: s->cargo_type = CT_INVALID; peter1138@6315: } rubidium@5378: } rubidium@5378: } rubidium@5378: } rubidium@5378: rubidium@4330: void DeleteSubsidyWithIndustry(IndustryID index) truelight@0: { truelight@0: Subsidy *s; truelight@193: tron@2952: for (s = _subsidies; s != endof(_subsidies); s++) { peter1138@6315: if (s->cargo_type != CT_INVALID && s->age < 12) { peter1138@6315: const CargoSpec *cs = GetCargo(s->cargo_type); peter1138@6315: if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL && peter1138@6315: (index == s->from || (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD && index == s->to))) { peter1138@6315: s->cargo_type = CT_INVALID; peter1138@6315: } 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@6248: struct FoundRoute { truelight@0: uint distance; Darkvater@3344: CargoID cargo; truelight@0: void *from; truelight@0: void *to; rubidium@6248: }; 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; belugas@6636: const IndustrySpec *ind; 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; belugas@6636: ind = GetIndustrySpec(i->type); truelight@0: belugas@6125: /* Randomize cargo type */ belugas@6636: if (HASBIT(Random(), 0) && ind->produced_cargo[1] != CT_INVALID) { belugas@6636: cargo = ind->produced_cargo[1]; truelight@0: trans = i->pct_transported[1]; truelight@0: total = i->total_production[1]; truelight@0: } else { belugas@6636: cargo = ind->produced_cargo[0]; truelight@0: trans = i->pct_transported[0]; truelight@0: total = i->total_production[0]; truelight@0: } truelight@0: belugas@6125: /* Quit if no production in this industry belugas@6125: * or if the cargo type is passengers belugas@6125: * or if the pct transported is already large enough */ peter1138@6315: if (total == 0 || trans > 42 || cargo == CT_INVALID) return; peter1138@6315: peter1138@6315: const CargoSpec *cs = GetCargo(cargo); peter1138@6315: if (cs->town_effect == TE_PASSENGERS) return; truelight@0: truelight@0: fr->cargo = cargo; truelight@0: peter1138@6315: if (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) { belugas@6125: /* The destination is a town */ truelight@4356: Town *t = GetRandomTown(); truelight@193: belugas@6125: /* 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@6125: /* The destination is an industry */ truelight@4356: Industry *i2 = GetRandomIndustry(); belugas@6636: if (i2 == NULL) { belugas@6636: return; belugas@6636: } belugas@6636: belugas@6636: ind = GetIndustrySpec(i2->type); truelight@193: belugas@6125: /* The industry must accept the cargo */ belugas@6636: if (i == i2 || belugas@6636: (cargo != ind->accepts_cargo[0] && belugas@6636: cargo != ind->accepts_cargo[1] && belugas@6636: cargo != ind->accepts_cargo[2])) { truelight@0: return; belugas@6636: } 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@6247: 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@6125: /* 25% chance to go on */ truelight@543: if (CHANCE16(1,4)) { belugas@6125: /* 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@6315: { peter1138@6315: const CargoSpec *cs = GetCargo(fr.cargo); peter1138@6315: s->to = (cs->town_effect == TE_GOODS || cs->town_effect == TE_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index; peter1138@6315: } 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@6247: 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@6247: 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@6091: const CargoSpec *cs = GetCargo(cargo_type); truelight@0: byte f; truelight@0: peter1138@6458: /* Use callback to calculate cargo profit, if available */ peter1138@6458: if (HASBIT(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) { peter1138@6458: uint32 var18 = min(dist, 0xFFFF) | (min(num_pieces, 0xFF) << 16) | (transit_days << 24); peter1138@6458: uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs); peter1138@6458: if (callback != CALLBACK_FAILED) { peter1138@6458: int result = GB(callback, 0, 14); peter1138@6458: peter1138@6458: /* Simulate a 15 bit signed value */ peter1138@6458: if (HASBIT(callback, 14)) result = 0x4000 - result; peter1138@6458: peter1138@6458: /* "The result should be a signed multiplier that gets multiplied peter1138@6458: * by the amount of cargo moved and the price factor, then gets peter1138@6458: * divided by 8192." */ peter1138@6458: return result * num_pieces * _cargo_payment_rates[cargo_type] / 8192; peter1138@6458: } peter1138@6458: } peter1138@6458: truelight@0: /* zero the distance if it's the bank and very short transport. */ belugas@6357: if (_opt.landscape == LT_TEMPERATE && cs->label == 'VALU' && dist < 10) truelight@0: dist = 0; truelight@0: truelight@0: f = 255; peter1138@6091: if (transit_days > cs->transit_days[0]) { peter1138@6091: transit_days -= cs->transit_days[0]; truelight@0: f -= transit_days; truelight@193: peter1138@6091: if (transit_days > cs->transit_days[1]) { peter1138@6091: 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@6369: 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: { rubidium@6635: Industry *best = NULL; rubidium@6635: Industry *ind; rubidium@6635: const IndustrySpec *indspec; rubidium@6635: uint best_dist; belugas@6639: uint accepted_cargo_index = 0; ///< unlikely value, just for warning removing truelight@0: belugas@6125: /* Check if there's an industry close to the station that accepts the cargo belugas@6125: * XXX - Think of something better to belugas@6125: * 1) Only deliver to industries which are withing the catchment radius belugas@6125: * 2) Distribute between industries if more then one is present */ rubidium@6635: best_dist = (_patches.station_spread + 8) * 2; truelight@830: FOR_ALL_INDUSTRIES(ind) { rubidium@6635: indspec = GetIndustrySpec(ind->type); belugas@6639: uint i; tron@3017: rubidium@6635: if (indspec->produced_cargo[0] == CT_INVALID) continue; rubidium@6635: rubidium@6635: for (i = 0; i < lengthof(indspec->accepts_cargo); i++) { rubidium@6635: if (cargo_type == indspec->accepts_cargo[i] && rubidium@6635: (indspec->input_cargo_multiplier[i][0] != 0 || indspec->input_cargo_multiplier[i][1] != 0)) { rubidium@6635: break; rubidium@6635: } rubidium@6635: } rubidium@6635: belugas@6639: /* Check if matching cargo has been found */ rubidium@6635: if (i == lengthof(indspec->accepts_cargo)) continue; rubidium@6635: rubidium@6635: uint dist = DistanceManhattan(ind->xy, xy); rubidium@6635: rubidium@6635: if (dist < best_dist) { truelight@0: best = ind; rubidium@6635: best_dist = dist; rubidium@6635: accepted_cargo_index = i; truelight@0: } truelight@0: } truelight@0: truelight@0: /* Found one? */ truelight@0: if (best != NULL) { rubidium@6635: indspec = GetIndustrySpec(best->type); truelight@0: best->was_cargo_delivered = true; rubidium@6635: best->cargo_waiting[0] = min(best->cargo_waiting[0] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][0] / 256), 0xFFFF); rubidium@6635: best->cargo_waiting[1] = min(best->cargo_waiting[1] + (num_pieces * indspec->input_cargo_multiplier[accepted_cargo_index][1] / 256), 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@6125: /* 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@6315: const CargoSpec *cs = GetCargo(cargo_type); peter1138@6315: 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@6315: switch (cs->town_effect) { peter1138@6315: case TE_PASSENGERS: peter1138@6315: case TE_MAIL: peter1138@6315: case TE_GOODS: peter1138@6315: 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@5683: 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@6125: /* 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@6125: /* Get station pointers. */ truelight@919: s_from = GetStation(source); truelight@919: s_to = GetStation(dest); truelight@0: belugas@6125: /* Check if a subsidy applies. */ truelight@0: subsidised = CheckSubsidised(s_from, s_to, cargo_type); truelight@0: belugas@6125: /* Increase town's counter for some special goods types */ peter1138@6315: const CargoSpec *cs = GetCargo(cargo_type); peter1138@6315: if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces; peter1138@6315: if (cs->town_effect == TE_WATER) s_to->town->new_act_water += num_pieces; truelight@0: belugas@6125: /* Give the goods to the industry. */ truelight@0: DeliverGoodsToIndustry(s_to->xy, cargo_type, num_pieces); truelight@193: belugas@6125: /* Determine profit */ celestar@5683: profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(source_tile, s_to->xy), days_in_transit, cargo_type); truelight@0: belugas@6125: /* 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: rubidium@6559: /** rubidium@6559: * Performs the vehicle payment _and_ marks the vehicle to be unloaded. rubidium@6559: * @param front_v the vehicle to be unloaded rubidium@6559: */ rubidium@6565: void VehiclePayment(Vehicle *front_v) rubidium@6559: { rubidium@6559: int result = 0; rubidium@6559: rubidium@6559: int profit = 0; rubidium@6559: int total_veh_profit = 0; // accumulates the profit across the vehicle chain (used by trains) rubidium@6559: int32 route_profit = 0; // the grand total amount for the route. A-D of transfer chain A-B-C-D rubidium@6559: int virtual_profit = 0; // virtual profit of one vehicle element for feeder systems rubidium@6559: int virtual_profit_total = 0; // virtual profit for entire vehicle chain rubidium@6559: int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step rubidium@6559: rubidium@6559: int all_vehicles_cargo_feeder_share = front_v->cargo_feeder_share; // used to hold transfer value of complete vehicle chain - used by trains rubidium@6559: rubidium@6559: StationID last_visited = front_v->last_station_visited; rubidium@6559: Station *st = GetStation(last_visited); rubidium@6559: rubidium@6565: /* The owner of the train wants to be paid */ rubidium@6565: PlayerID old_player = _current_player; rubidium@6565: _current_player = front_v->owner; rubidium@6565: rubidium@6565: /* At this moment loading cannot be finished */ rubidium@6565: CLRBIT(front_v->vehicle_flags, VF_LOADING_FINISHED); rubidium@6565: rubidium@6565: /* Start unloading in at the first possible moment */ rubidium@6565: front_v->load_unload_time_rem = 1; rubidium@6565: rubidium@6559: for (Vehicle *v = front_v; v != NULL; v = v->next) { rubidium@6565: /* No cargo to unload */ rubidium@6611: if (v->cargo_cap == 0 || v->cargo_count == 0) continue; rubidium@6559: rubidium@6559: SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING); rubidium@6565: /* All cargo has already been paid for, no need to pay again */ rubidium@6559: if (v->cargo_count == v->cargo_paid_for) continue; rubidium@6559: rubidium@6559: GoodsEntry *ge = &st->goods[v->cargo_type]; rubidium@6559: rubidium@6559: if (v->cargo_source != last_visited && rubidium@6559: HASBIT(ge->waiting_acceptance, 15) && rubidium@6559: (front_v->current_order.flags & OF_TRANSFER) == 0) { rubidium@6559: /* Deliver goods to the station */ rubidium@6559: st->time_since_unload = 0; rubidium@6559: rubidium@6559: /* handle end of route payment */ rubidium@6559: 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@6559: v->cargo_paid_for = v->cargo_count; rubidium@6559: route_profit = profit; // display amount paid for final route delivery, A-D of a chain A-B-C-D rubidium@6559: total_veh_profit = profit - all_vehicles_cargo_feeder_share; // whole vehicle is not payed for transfers picked up earlier rubidium@6559: 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@6559: rubidium@6559: v->cargo_feeder_share = 0; // clear transfer cost per vehicle rubidium@6559: result |= 1; rubidium@6559: } else if (front_v->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) { rubidium@6559: if ((front_v->current_order.flags & OF_TRANSFER) != 0) { rubidium@6559: virtual_profit = GetTransportedGoodsIncome( rubidium@6559: v->cargo_count - v->cargo_paid_for, rubidium@6559: /* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */ rubidium@6559: DistanceManhattan(v->cargo_loaded_at_xy, GetStation(last_visited)->xy), rubidium@6559: v->cargo_days, rubidium@6559: v->cargo_type); rubidium@6559: rubidium@6601: front_v->profit_this_year += virtual_profit; rubidium@6601: ge->feeder_profit += v->cargo_feeder_share + virtual_profit; // transfer cargo transfer fees to station rubidium@6601: total_cargo_feeder_share -= v->cargo_feeder_share; // accumulate deduction of feeder shares rubidium@6601: v->cargo_feeder_share = 0; // clear transfer cost rubidium@6559: rubidium@6559: /* keep total of cargo unloaded (pending) for accurate cargoshare calculation on load */ rubidium@6559: SB(ge->unload_pending, 0, 12, GB(ge->unload_pending, 0, 12) + v->cargo_count); rubidium@6559: rubidium@6559: virtual_profit_total += virtual_profit; // accumulate transfer profits for whole vehicle rubidium@6559: v->cargo_paid_for = v->cargo_count; // record how much of the cargo has been paid for to eliminate double counting rubidium@6559: } rubidium@6559: result |= 2; rubidium@6559: } rubidium@6559: } rubidium@6559: rubidium@6559: /* Ensure a negative total is only applied to the vehicle if there is value to reduce. */ rubidium@6559: front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0); rubidium@6559: rubidium@6559: if (virtual_profit_total > 0) { rubidium@6559: ShowFeederIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, virtual_profit_total); rubidium@6559: } rubidium@6559: rubidium@6559: if (route_profit != 0) { rubidium@6559: front_v->profit_this_year += total_veh_profit; rubidium@6559: SubtractMoneyFromPlayer(-route_profit); rubidium@6559: rubidium@6559: if (IsLocalPlayer() && !PlayVehicleSound(front_v, VSE_LOAD_UNLOAD)) { rubidium@6559: SndPlayVehicleFx(SND_14_CASHTILL, front_v); rubidium@6559: } rubidium@6559: rubidium@6559: ShowCostOrIncomeAnimation(front_v->x_pos, front_v->y_pos, front_v->z_pos, -total_veh_profit); rubidium@6559: } rubidium@6559: rubidium@6565: _current_player = old_player; rubidium@6559: } rubidium@6559: rubidium@6580: /** rubidium@6580: * Loads/unload the vehicle if possible. rubidium@6580: * @param v the vehicle to be (un)loaded rubidium@6618: * @param cargo_left the amount of each cargo type that is rubidium@6618: * virtually left on the platform to be rubidium@6618: * picked up by another vehicle when all rubidium@6618: * previous vehicles have loaded. rubidium@6580: */ rubidium@6618: static void LoadUnloadVehicle(Vehicle *v, int *cargo_left) truelight@0: { rubidium@6616: assert(v->current_order.type == OT_LOADING); rubidium@6616: rubidium@6616: /* We have not waited enough time till the next round of loading/unloading */ rubidium@6618: if (--v->load_unload_time_rem != 0) { rubidium@6618: if (_patches.improved_load && HASBIT(v->current_order.flags, OFB_FULL_LOAD)) { rubidium@6618: /* 'Reserve' this cargo for this vehicle, because we were first. */ rubidium@6618: for (; v != NULL; v = v->next) { rubidium@6618: if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo_count; rubidium@6618: } rubidium@6618: } rubidium@6618: return; rubidium@6618: } rubidium@6616: rubidium@6611: int unloading_time = 0; truelight@0: Vehicle *u = v; truelight@0: int result = 0; rubidium@6620: int cap; rubidium@6565: rubidium@6611: bool completely_empty = true; rubidium@6611: bool anything_unloaded = false; rubidium@6611: bool anything_loaded = false; rubidium@6611: uint32 cargo_not_full = 0; rubidium@6611: uint32 cargo_full = 0; rubidium@6559: int total_cargo_feeder_share = 0; // the feeder cash amount for the goods being loaded/unloaded in this load step truelight@0: truelight@0: v->cur_speed = 0; peter1138@5251: richk@6198: StationID last_visited = v->last_station_visited; richk@6198: Station *st = GetStation(last_visited); richk@6198: tron@499: for (; v != NULL; v = v->next) { rubidium@6609: if (v->cargo_cap == 0) continue; rubidium@6609: rubidium@6609: byte load_amount = EngInfo(v->engine_type)->load_amount; maedhros@6227: 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: rubidium@6609: GoodsEntry *ge = &st->goods[v->cargo_type]; rubidium@6620: int count = GB(ge->waiting_acceptance, 0, 12); truelight@193: rubidium@6609: if (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: tron@2639: if (v->cargo_source != last_visited && ge->waiting_acceptance & 0x8000 && !(u->current_order.flags & OF_TRANSFER)) { truelight@0: result |= 1; tron@2639: } else if (u->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) { rubidium@6609: if (count == 0) { belugas@6125: /* No goods waiting at station */ rubidium@6609: ge->enroute_time = v->cargo_days; rubidium@6609: ge->enroute_from = v->cargo_source; celestar@5683: ge->enroute_from_xy = v->cargo_source_xy; truelight@0: } else { belugas@6125: /* Goods already waiting at station. Set counters to the worst value. */ celestar@5683: if (v->cargo_days >= ge->enroute_time) ge->enroute_time = v->cargo_days; celestar@5683: celestar@5683: if (last_visited != ge->enroute_from) { rubidium@6609: ge->enroute_from = v->cargo_source; celestar@5683: ge->enroute_from_xy = v->cargo_source_xy; celestar@5683: } truelight@0: } rubidium@6609: /* Update amount of waiting cargo. There is, however, no sense in rubidium@6609: * updating the count variable because this vehicle will not be rubidium@6609: * able to take the cargo. */ rubidium@6609: SB(ge->waiting_acceptance, 0, 12, min(amount_unloaded + count, 0xFFF)); peter1138@4814: richk@6198: /* if there is not enough to unload from pending, ensure it does not go -ve richk@6198: * else deduct amount actually unloaded from unload_pending */ rubidium@6199: SB(ge->unload_pending, 0, 12, max(GB(ge->unload_pending, 0, 12) - amount_unloaded, 0U)); richk@6198: truelight@0: result |= 2; rubidium@6609: } else { rubidium@6609: /* The order changed while unloading (unset unload/transfer) or the rubidium@6609: * station does not accept goods anymore. */ rubidium@6609: CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING); rubidium@6609: continue; truelight@0: } tron@445: rubidium@6609: /* Deliver goods to the station */ rubidium@6609: st->time_since_unload = 0; truelight@193: rubidium@6609: unloading_time += amount_unloaded; rubidium@6609: rubidium@6609: v->cargo_count -= amount_unloaded; rubidium@6609: v->cargo_paid_for -= min(amount_unloaded, v->cargo_paid_for); rubidium@6609: rubidium@6611: anything_unloaded = true; rubidium@6609: if (_patches.gradual_loading && v->cargo_count != 0) { rubidium@6609: completely_empty = false; rubidium@6609: } else { rubidium@6609: /* We have finished unloading (cargo count == 0) */ rubidium@6609: CLRBIT(v->vehicle_flags, VF_CARGO_UNLOADING); rubidium@6609: } rubidium@6609: rubidium@6609: continue; rubidium@6609: } maedhros@5888: maedhros@5888: /* We cannot have paid for more cargo than there is on board. */ maedhros@5888: assert(v->cargo_paid_for <= v->cargo_count); peter1138@5251: rubidium@6609: /* Do not pick up goods that we unloaded */ tron@555: if (u->current_order.flags & OF_UNLOAD) continue; truelight@0: truelight@0: /* update stats */ rubidium@6609: int t; tron@2989: switch (u->type) { rubidium@6259: case VEH_TRAIN: t = u->u.rail.cached_max_speed; break; rubidium@6259: case VEH_ROAD: t = u->max_speed / 2; break; tron@2989: default: t = u->max_speed; break; tron@2989: } truelight@193: belugas@6125: /* if last speed is 0, we treat that as if no vehicle has ever visited the station. */ rubidium@6609: ge->days_since_pickup = 0; tron@2989: ge->last_speed = min(t, 255); rubidium@4329: ge->last_age = _cur_year - v->build_year; truelight@0: belugas@6125: /* If there's goods waiting at the station, and the vehicle belugas@6125: * 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: tron@523: /* Skip loading this vehicle if another train/vehicle is already handling tron@523: * the same cargo type at this station */ rubidium@6698: if (_patches.improved_load && cargo_left[v->cargo_type] <= 0) { rubidium@6611: SETBIT(cargo_not_full, v->cargo_type); rubidium@6565: continue; rubidium@6565: } tron@523: rubidium@6618: if (cap > count) cap = count; rubidium@6618: if (_patches.gradual_loading) cap = min(cap, load_amount); rubidium@6618: if (_patches.improved_load) { rubidium@6618: /* Don't load stuff that is already 'reserved' for other vehicles */ rubidium@6618: cap = min(cargo_left[v->cargo_type], cap); rubidium@6618: cargo_left[v->cargo_type] -= cap; rubidium@6618: } rubidium@6618: rubidium@6618: if (v->cargo_count == 0) TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO); rubidium@6618: 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@6125: * 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: richk@6198: /* cargoshare is proportioned by the amount due to unload richk@6198: * Otherwise, with gradual loading, 100% of credits would be taken immediately, richk@6198: * even if the cargo volume represents a tiny percent of the whole. richk@6198: * ge->unload_pending holds the amount that has been credited, but has not yet been unloaded. richk@6198: */ richk@6198: int cargoshare = cap * 10000 / (ge->waiting_acceptance + ge->unload_pending); richk@6198: int feeder_profit_share = ge->feeder_profit * cargoshare / 10000; truelight@0: v->cargo_count += cap; truelight@0: ge->waiting_acceptance -= cap; richk@6198: richk@6198: total_cargo_feeder_share += feeder_profit_share; // store cost for later payment when cargo unloaded richk@6198: v->cargo_loaded_at_xy = st->xy; // retains location of where the cargo was picked up for intermediate payments richk@6198: celestar@1935: ge->feeder_profit -= feeder_profit_share; truelight@0: unloading_time += cap; truelight@0: st->time_since_load = 0; truelight@193: belugas@6125: /* And record the source of the cargo, and the days in travel. */ truelight@2815: v->cargo_source = ge->enroute_from; celestar@5683: 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: } rubidium@6611: rubidium@6611: if (v->cargo_count == v->cargo_cap) { rubidium@6611: SETBIT(cargo_full, v->cargo_type); rubidium@6611: } else { rubidium@6611: SETBIT(cargo_not_full, v->cargo_type); rubidium@6611: } truelight@0: } truelight@0: rubidium@6618: /* We update these variables here, so gradual loading still fills rubidium@6618: * all wagons at the same time instead of using the same 'improved' rubidium@6618: * loading algorithm for the wagons (only fill wagon when there is rubidium@6618: * enough to fill the previous wagons) */ rubidium@6618: if (_patches.improved_load && HASBIT(u->current_order.flags, OFB_FULL_LOAD)) { rubidium@6618: /* Update left cargo */ rubidium@6618: for (v = u; v != NULL; v = v->next) { rubidium@6618: if (v->cargo_cap != 0) cargo_left[v->cargo_type] -= v->cargo_cap - v->cargo_count; rubidium@6618: } rubidium@6618: } rubidium@6618: peter1138@5211: v = u; celestar@1935: rubidium@6559: v->cargo_feeder_share += total_cargo_feeder_share; richk@6198: rubidium@6611: if (anything_loaded || anything_unloaded) { rubidium@6611: if (_patches.gradual_loading) { rubidium@6611: /* The time it takes to load one 'slice' of cargo or passengers depends rubidium@6611: * on the vehicle type - the values here are those found in TTDPatch */ rubidium@6611: const uint gradual_loading_wait_time[] = { 40, 20, 10, 20 }; peter1138@5211: rubidium@6611: unloading_time = gradual_loading_wait_time[v->type]; rubidium@6611: } rubidium@6611: } else { rubidium@6611: bool finished_loading = true; rubidium@6611: if (HASBIT(v->current_order.flags, OFB_FULL_LOAD)) { rubidium@6611: if (_patches.full_load_any) { rubidium@6611: /* if the aircraft carries passengers and is NOT full, then rubidium@6611: * continue loading, no matter how much mail is in */ rubidium@6611: if ((v->type == VEH_AIRCRAFT && IsCargoInClass(v->cargo_type, CC_PASSENGERS) && v->cargo_cap != v->cargo_count) || rubidium@6611: (cargo_not_full && (cargo_full & ~cargo_not_full) == 0)) { // There are stull non-full cargos rubidium@6611: finished_loading = false; rubidium@6611: } rubidium@6611: } else if (cargo_not_full != 0) { rubidium@6611: finished_loading = false; peter1138@5251: } peter1138@5251: } rubidium@6611: unloading_time = 20; rubidium@6611: rubidium@6611: SB(v->vehicle_flags, VF_LOADING_FINISHED, 1, finished_loading); peter1138@5211: } truelight@0: rubidium@6259: if (v->type == VEH_TRAIN) { belugas@6125: /* Each platform tile is worth 2 rail vehicles. */ celestar@5998: 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@6565: InvalidateWindow(v->GetVehicleListWindowClass(), v->owner); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); rubidium@6565: KUDr@5665: st->MarkTilesDirty(); rubidium@6565: v->MarkDirty(); truelight@0: tron@2951: if (result & 2) InvalidateWindow(WC_STATION_VIEW, last_visited); truelight@0: } truelight@0: } truelight@0: rubidium@6616: /** rubidium@6616: * Load/unload the vehicles in this station according to the order rubidium@6616: * they entered. rubidium@6616: * @param st the station to do the loading/unloading for rubidium@6616: */ rubidium@6616: void LoadUnloadStation(Station *st) rubidium@6616: { rubidium@6618: int cargo_left[NUM_CARGO]; rubidium@6618: rubidium@6618: for (uint i = 0; i < NUM_CARGO; i++) cargo_left[i] = GB(st->goods[i].waiting_acceptance, 0, 12); rubidium@6618: rubidium@6616: std::list::iterator iter; rubidium@6616: for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) { rubidium@6616: Vehicle *v = *iter; rubidium@6618: if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v, cargo_left); rubidium@6616: } rubidium@6616: } rubidium@6616: rubidium@6247: void PlayersMonthlyLoop() truelight@0: { truelight@0: PlayersGenStatistics(); rubidium@4293: if (_patches.inflation && _cur_year < MAX_YEAR) truelight@0: AddInflation(); truelight@0: PlayersPayInterest(); belugas@6125: /* 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@5587: 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@6125: /* original code does this a little bit differently */ rubidium@5587: PlayerID pi = p->index; truelight@6262: 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@5587: extern int GetAmountOwnedBy(const Player *p, PlayerID owner); truelight@599: Darkvater@1793: /** Acquire shares in an opposing company. tron@3491: * @param tile unused belugas@6432: * @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@5587: 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@5587: 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@6432: * @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@5587: 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@5587: PlayerByte* b = p->share_owners; belugas@6125: 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@6432: * @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@6405: PlayerID pid = (PlayerID)p1; Darkvater@1793: Darkvater@1793: /* Disable takeovers in multiplayer games */ celestar@6405: if (!IsValidPlayer(pid) || _networking) return CMD_ERROR; celestar@6405: celestar@6405: /* Do not allow players to take over themselves */ celestar@6405: if (pid == _current_player) return CMD_ERROR; Darkvater@1793: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); celestar@6405: 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@6125: /** Prices */ rubidium@6247: 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@6125: /** Cargo payment rates */ rubidium@6247: static void SaveLoad_CAPR() truelight@0: { peter1138@6463: uint num_cargo = CheckSavegameVersion(55) ? 12 : NUM_CARGO; peter1138@6463: SlArray(&_cargo_payment_rates, num_cargo, SLE_INT32); peter1138@6463: 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@6125: /** Economy variables */ rubidium@6247: static void SaveLoad_ECMY() truelight@0: { Darkvater@1881: SlObject(&_economy, _economy_desc); truelight@0: } truelight@0: rubidium@5587: 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: };