2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-06 08:18:00 +00:00
|
|
|
#include "strings.h" // XXX InjectDParam()
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "news.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "vehicle.h"
|
|
|
|
#include "window.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "saveload.h"
|
|
|
|
#include "economy.h"
|
|
|
|
#include "industry.h"
|
|
|
|
#include "town.h"
|
2004-09-13 22:49:11 +00:00
|
|
|
#include "network.h"
|
2004-11-05 23:12:33 +00:00
|
|
|
#include "sound.h"
|
2004-11-17 08:52:47 +00:00
|
|
|
#include "engine.h"
|
2004-12-04 17:54:56 +00:00
|
|
|
#include "network_data.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-22 16:54:55 +00:00
|
|
|
// get a mask of the allowed currencies depending on the year
|
2004-12-22 19:48:27 +00:00
|
|
|
uint GetMaskOfAllowedCurrencies(void)
|
2004-12-22 16:54:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint mask = 0;
|
2004-12-22 19:48:27 +00:00
|
|
|
for (i = 0; i != lengthof(_currency_specs); i++) {
|
2004-12-22 16:54:55 +00:00
|
|
|
uint16 to_euro = _currency_specs[i].to_euro;
|
|
|
|
if (i == 23) mask |= (1 << 23); // always allow custom currency
|
2005-01-10 14:43:00 +00:00
|
|
|
if (to_euro != CF_NOEURO && to_euro != CF_ISEURO && _cur_year >= (to_euro-MAX_YEAR_BEGIN_REAL)) continue;
|
|
|
|
if (_cur_year < (2000-MAX_YEAR_BEGIN_REAL) && (to_euro == CF_ISEURO)) continue;
|
2004-12-22 16:54:55 +00:00
|
|
|
mask |= (1 << i);
|
|
|
|
}
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2004-12-22 19:48:27 +00:00
|
|
|
void CheckSwitchToEuro(void)
|
2004-12-22 16:54:55 +00:00
|
|
|
{
|
|
|
|
if (_currency_specs[_opt.currency].to_euro != CF_NOEURO &&
|
|
|
|
_currency_specs[_opt.currency].to_euro != CF_ISEURO &&
|
2005-01-10 14:43:00 +00:00
|
|
|
_cur_year >= (_currency_specs[_opt.currency].to_euro-MAX_YEAR_BEGIN_REAL)) {
|
2004-12-22 16:54:55 +00:00
|
|
|
_opt.currency = 2; // this is the index of euro above.
|
|
|
|
AddNewsItem(STR_EURO_INTRODUCE, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-01 21:54:12 +00:00
|
|
|
void UpdatePlayerHouse(Player *p, uint score)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte val;
|
|
|
|
uint tile = p->location_of_house;
|
|
|
|
|
|
|
|
if (tile == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
(val = 128, score < 170) ||
|
|
|
|
(val+= 4, score < 350) ||
|
|
|
|
(val+= 4, score < 520) ||
|
|
|
|
(val+= 4, score < 720) ||
|
|
|
|
(val+= 4, true);
|
|
|
|
|
|
|
|
/* house is already big enough */
|
|
|
|
if (val <= _map5[tile])
|
|
|
|
return;
|
|
|
|
|
|
|
|
_map5[tile + TILE_XY(0,0)] = val;
|
|
|
|
_map5[tile + TILE_XY(0,1)] = ++val;
|
|
|
|
_map5[tile + TILE_XY(1,0)] = ++val;
|
|
|
|
_map5[tile + TILE_XY(1,1)] = ++val;
|
|
|
|
|
|
|
|
MarkTileDirtyByTile(tile + TILE_XY(0,0));
|
|
|
|
MarkTileDirtyByTile(tile + TILE_XY(0,1));
|
|
|
|
MarkTileDirtyByTile(tile + TILE_XY(1,0));
|
|
|
|
MarkTileDirtyByTile(tile + TILE_XY(1,1));
|
|
|
|
}
|
|
|
|
|
2004-09-11 09:40:19 +00:00
|
|
|
int64 CalculateCompanyValue(Player *p) {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte owner = p->index;
|
2004-09-11 09:40:19 +00:00
|
|
|
int64 value;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
uint num = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0 && st->owner == owner) {
|
|
|
|
uint facil = st->facilities;
|
|
|
|
do num += (facil&1); while (facil >>= 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value = num * _price.station_value * 25;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner != owner)
|
|
|
|
continue;
|
|
|
|
if (v->type == VEH_Train ||
|
|
|
|
v->type == VEH_Road ||
|
|
|
|
(v->type == VEH_Aircraft && v->subtype<=2) ||
|
|
|
|
v->type == VEH_Ship) {
|
|
|
|
value += v->value * 3 >> 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-13 09:55:32 +00:00
|
|
|
value += p->money64 - p->current_loan; // add real money value
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-04-06 09:34:56 +00:00
|
|
|
return max64(value, 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
// if update is set to true, the economy is updated with this score
|
|
|
|
// (also the house is updated, should only be true in the on-tick event)
|
2004-09-01 21:54:12 +00:00
|
|
|
int UpdateCompanyRatingAndValue(Player *p, bool update)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte owner = p->index;
|
2004-08-23 10:59:03 +00:00
|
|
|
int score = 0;
|
|
|
|
|
|
|
|
memset(_score_part[owner], 0, sizeof(_score_part[owner]));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Count vehicles */
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
2005-03-01 19:32:55 +00:00
|
|
|
int32 min_profit = score_info[SCORE_MIN_PROFIT].needed;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint num = 0;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner != owner)
|
|
|
|
continue;
|
2005-01-19 19:15:03 +00:00
|
|
|
if ((v->type == VEH_Train && v->subtype == TS_Front_Engine) ||
|
2004-08-09 17:04:08 +00:00
|
|
|
v->type == VEH_Road ||
|
|
|
|
(v->type == VEH_Aircraft && v->subtype<=2) ||
|
|
|
|
v->type == VEH_Ship) {
|
|
|
|
num++;
|
|
|
|
if (v->age > 730) {
|
2004-08-23 10:59:03 +00:00
|
|
|
if (min_profit > v->profit_last_year)
|
2004-08-09 17:04:08 +00:00
|
|
|
min_profit = v->profit_last_year;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_VEHICLES] = num;
|
2005-02-23 14:12:32 +00:00
|
|
|
if (min_profit > 0)
|
|
|
|
_score_part[owner][SCORE_MIN_PROFIT] = min_profit;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Count stations */
|
|
|
|
{
|
|
|
|
uint num = 0;
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0 && st->owner == owner) {
|
|
|
|
int facil = st->facilities;
|
|
|
|
do num += facil&1; while (facil>>=1);
|
|
|
|
}
|
|
|
|
}
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_STATIONS] = num;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate statistics depending on recent income statistics */
|
|
|
|
{
|
|
|
|
PlayerEconomyEntry *pee;
|
|
|
|
int numec;
|
|
|
|
int32 min_income;
|
2004-12-04 17:54:56 +00:00
|
|
|
int32 max_income;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
numec = min(p->num_valid_stat_ent, 12);
|
|
|
|
if (numec != 0) {
|
|
|
|
min_income = 0x7FFFFFFF;
|
|
|
|
max_income = 0;
|
|
|
|
pee = p->old_economy;
|
|
|
|
do {
|
|
|
|
min_income = min(min_income, pee->income + pee->expenses);
|
|
|
|
max_income = max(max_income, pee->income + pee->expenses);
|
|
|
|
} while (++pee,--numec);
|
|
|
|
|
|
|
|
if (min_income > 0)
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_MIN_INCOME] = min_income;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_MAX_INCOME] = max_income;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate score depending on amount of transported cargo */
|
|
|
|
{
|
|
|
|
PlayerEconomyEntry *pee;
|
|
|
|
int numec;
|
|
|
|
uint32 total_delivered;
|
|
|
|
|
|
|
|
numec = min(p->num_valid_stat_ent, 4);
|
|
|
|
if (numec != 0) {
|
|
|
|
pee = p->old_economy;
|
|
|
|
total_delivered = 0;
|
|
|
|
do {
|
|
|
|
total_delivered += pee->delivered_cargo;
|
|
|
|
} while (++pee,--numec);
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_DELIVERED] = total_delivered;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-23 10:59:03 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Generate score for variety of cargo */
|
|
|
|
{
|
|
|
|
uint cargo = p->cargo_types;
|
|
|
|
uint num = 0;
|
|
|
|
do num += cargo&1; while (cargo>>=1);
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_CARGO] = num;
|
|
|
|
if (update)
|
|
|
|
p->cargo_types = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate score for player money */
|
|
|
|
{
|
|
|
|
int32 money = p->player_money;
|
|
|
|
if (money > 0) {
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_MONEY] = money;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate score for loan */
|
|
|
|
{
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_LOAN] = score_info[SCORE_LOAN].needed - p->current_loan;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
// Now we calculate the score for each item..
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int total_score = 0;
|
|
|
|
int s;
|
|
|
|
score = 0;
|
|
|
|
for (i=0;i<NUM_SCORE;i++) {
|
|
|
|
// Skip the total
|
|
|
|
if (i == SCORE_TOTAL) continue;
|
|
|
|
// Check the score
|
|
|
|
s = (_score_part[owner][i] >= score_info[i].needed) ? score_info[i].score : ((_score_part[owner][i] * score_info[i].score) / score_info[i].needed);
|
|
|
|
if (s < 0) s = 0;
|
|
|
|
score += s;
|
|
|
|
total_score += score_info[i].score;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
_score_part[owner][SCORE_TOTAL] = score;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
// We always want the score scaled to SCORE_MAX (1000)
|
|
|
|
if (total_score != SCORE_MAX)
|
|
|
|
score = score * SCORE_MAX / total_score;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
if (update) {
|
|
|
|
p->old_economy[0].performance_history = score;
|
|
|
|
UpdatePlayerHouse(p, score);
|
|
|
|
p->old_economy[0].company_value = CalculateCompanyValue(p);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
InvalidateWindow(WC_PERFORMANCE_DETAIL, 0);
|
2004-09-01 21:54:12 +00:00
|
|
|
return score;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use 255 as new_player to delete the player.
|
2005-05-12 23:47:45 +00:00
|
|
|
void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-12 23:47:45 +00:00
|
|
|
PlayerID old = _current_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
_current_player = old_player;
|
|
|
|
|
2005-05-12 23:47:45 +00:00
|
|
|
if (new_player == OWNER_SPECTATOR) {
|
2004-08-09 17:04:08 +00:00
|
|
|
Subsidy *s;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-05-12 23:47:45 +00:00
|
|
|
for (s = _subsidies; s != endof(_subsidies); s++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (s->cargo_type != 0xff && s->age >= 12) {
|
2005-05-12 23:47:45 +00:00
|
|
|
if (GetStation(s->to)->owner == old_player)
|
2004-08-09 17:04:08 +00:00
|
|
|
s->cargo_type = 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-12 23:47:45 +00:00
|
|
|
/* Take care of rating in towns */
|
|
|
|
{ Town *t;
|
|
|
|
if (new_player != OWNER_SPECTATOR) {
|
|
|
|
FOR_ALL_TOWNS(t) {
|
|
|
|
/* If a player takes over, give the ratings to that player. */
|
|
|
|
if (IsValidTown(t) && HASBIT(t->have_ratings, old_player)) {
|
|
|
|
if (HASBIT(t->have_ratings, new_player)) {
|
|
|
|
// use max of the two ratings.
|
|
|
|
t->ratings[new_player] = max(t->ratings[new_player], t->ratings[old_player]);
|
|
|
|
} else {
|
|
|
|
SETBIT(t->have_ratings, new_player);
|
|
|
|
t->ratings[new_player] = t->ratings[old_player];
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 23:47:45 +00:00
|
|
|
/* Reset ratings for the town */
|
|
|
|
if (IsValidTown(t)) {
|
|
|
|
t->ratings[old_player] = 500;
|
|
|
|
CLRBIT(t->have_ratings, old_player);
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int num_train = 0;
|
|
|
|
int num_road = 0;
|
|
|
|
int num_ship = 0;
|
|
|
|
int num_aircraft = 0;
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
// Determine Ids for the new vehicles
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner == new_player) {
|
2005-05-12 23:47:45 +00:00
|
|
|
switch (v->type) {
|
|
|
|
case VEH_Train:
|
|
|
|
if (v->subtype == TS_Front_Engine) num_train++;
|
|
|
|
break;
|
|
|
|
case VEH_Road:
|
|
|
|
num_road++;
|
|
|
|
break;
|
|
|
|
case VEH_Ship:
|
|
|
|
num_ship++;
|
|
|
|
break;
|
|
|
|
case VEH_Aircraft:
|
|
|
|
if (v->subtype <= 2) num_aircraft++;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_Train, VEH_Aircraft+1) ) {
|
2005-05-12 23:47:45 +00:00
|
|
|
if (new_player == OWNER_SPECTATOR) {
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
|
|
|
|
DeleteVehicle(v);
|
|
|
|
} else {
|
|
|
|
v->owner = new_player;
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->type == VEH_Train && v->subtype == TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
v->unitnumber = ++num_train;
|
|
|
|
else if (v->type == VEH_Road)
|
|
|
|
v->unitnumber = ++num_road;
|
|
|
|
else if (v->type == VEH_Ship)
|
|
|
|
v->unitnumber = ++num_ship;
|
|
|
|
else if (v->type == VEH_Aircraft && v->subtype <= 2)
|
|
|
|
v->unitnumber = ++num_aircraft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change ownership of tiles
|
|
|
|
{
|
2005-05-12 23:47:45 +00:00
|
|
|
TileIndex tile = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
ChangeTileOwner(tile, old_player, new_player);
|
2005-01-03 18:59:58 +00:00
|
|
|
} while (++tile != MapSize());
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change color of existing windows
|
2005-05-12 23:47:45 +00:00
|
|
|
if (new_player != OWNER_SPECTATOR) {
|
2004-08-09 17:04:08 +00:00
|
|
|
Window *w;
|
2005-05-12 23:47:45 +00:00
|
|
|
for (w = _windows; w != _last_window; w++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (w->caption_color == old_player)
|
|
|
|
w->caption_color = new_player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-23 14:02:40 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
/* Check for shares */
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
2005-05-12 23:47:45 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2004-12-23 14:02:40 +00:00
|
|
|
/* 'Sell' the share if this player has any */
|
|
|
|
if (p->share_owners[i] == _current_player)
|
|
|
|
p->share_owners[i] = 0xFF;
|
|
|
|
}
|
|
|
|
}
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(_current_player);
|
2004-12-23 14:02:40 +00:00
|
|
|
/* Sell all the shares that people have on this company */
|
2005-05-12 23:47:45 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2004-12-23 14:02:40 +00:00
|
|
|
p->share_owners[i] = 0xFF;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_current_player = old;
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void DeletePlayerWindows(int pi);
|
|
|
|
|
|
|
|
static void PlayersCheckBankrupt(Player *p)
|
|
|
|
{
|
|
|
|
int owner;
|
2004-09-11 09:40:19 +00:00
|
|
|
int64 val;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// If the player has money again, it does not go bankrupt
|
2004-08-09 17:04:08 +00:00
|
|
|
if (p->player_money >= 0) {
|
|
|
|
p->quarters_of_bankrupcy = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->quarters_of_bankrupcy++;
|
|
|
|
|
|
|
|
owner = p->index;
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
switch (p->quarters_of_bankrupcy) {
|
|
|
|
case 2:
|
|
|
|
AddNewsItem( (StringID)(owner + 16),
|
|
|
|
NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
|
|
|
|
break;
|
|
|
|
case 3: {
|
|
|
|
/* XXX - In multiplayer, should we ask other players if it wants to take
|
|
|
|
over when it is a human company? -- TrueLight */
|
|
|
|
if (IS_HUMAN_PLAYER(owner)) {
|
|
|
|
AddNewsItem( (StringID)(owner + 16),
|
|
|
|
NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
|
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// Check if the company has any value.. if not, declare it bankrupt
|
|
|
|
// right now
|
|
|
|
val = CalculateCompanyValue(p);
|
|
|
|
if (val > 0) {
|
|
|
|
p->bankrupt_value = val;
|
|
|
|
p->bankrupt_asked = 1 << owner; // Don't ask the owner
|
|
|
|
p->bankrupt_timeout = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Else, falltrue to case 4...
|
|
|
|
}
|
|
|
|
case 4: {
|
|
|
|
// Close everything the owner has open
|
|
|
|
DeletePlayerWindows(owner);
|
|
|
|
|
|
|
|
// Show bankrupt news
|
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
|
|
|
AddNewsItem( (StringID)(owner + 16*3), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
|
|
|
|
|
|
|
|
// If the player is human, and it is no network play, leave the player playing
|
|
|
|
if (IS_HUMAN_PLAYER(owner) && !_networking) {
|
|
|
|
p->bankrupt_asked = 255;
|
|
|
|
p->bankrupt_timeout = 0x456;
|
|
|
|
} else {
|
2004-12-13 17:58:53 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2004-12-04 17:54:56 +00:00
|
|
|
if (IS_HUMAN_PLAYER(owner) && _network_server) {
|
2004-12-16 11:36:00 +00:00
|
|
|
// If we are the server, make sure it is clear that his player is no
|
|
|
|
// longer with us!
|
2004-12-04 17:54:56 +00:00
|
|
|
NetworkClientInfo *ci;
|
2004-12-19 10:17:26 +00:00
|
|
|
NetworkClientState *cs;
|
2004-12-16 11:36:00 +00:00
|
|
|
/* Find all clients that were in control of this company */
|
|
|
|
FOR_ALL_CLIENTS(cs) {
|
|
|
|
ci = DEREF_CLIENT_INFO(cs);
|
|
|
|
if ((ci->client_playas-1) == owner) {
|
|
|
|
ci->client_playas = OWNER_SPECTATOR;
|
|
|
|
// Send the new info to all the clients
|
|
|
|
NetworkUpdateClientInfo(_network_own_client_index);
|
|
|
|
}
|
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
|
|
|
// Make sure the player no longer controls the company
|
|
|
|
if (IS_HUMAN_PLAYER(owner) && owner == _local_player) {
|
|
|
|
// Switch the player to spectator..
|
|
|
|
_local_player = OWNER_SPECTATOR;
|
|
|
|
}
|
2004-12-16 11:36:00 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
// Convert everything the player owns to NO_OWNER
|
|
|
|
p->money64 = p->player_money = 100000000;
|
|
|
|
ChangeOwnershipOfPlayerItems(owner, 0xFF); // 255 is no owner
|
|
|
|
// Register the player as not-active
|
|
|
|
p->is_active = false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNewsBankrupcy(Window *w)
|
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
DrawNewsBorder(w);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(WP(w,news_d).ni->string_id & 15);
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawPlayerFace(p->face, p->player_color, 2, 23);
|
|
|
|
GfxFillRect(3, 23, 3+91, 23+118, 0x4323);
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->president_name_1);
|
|
|
|
SetDParam(1, p->president_name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
DrawStringMultiCenter(49, 148, STR_7058_PRESIDENT, 94);
|
|
|
|
|
|
|
|
switch(WP(w,news_d).ni->string_id >> 4) {
|
|
|
|
case 1:
|
|
|
|
DrawStringCentered(w->width>>1, 1, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE, 0);
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
DrawStringMultiCenter(
|
|
|
|
((w->width - 101) >> 1) + 98,
|
|
|
|
90,
|
|
|
|
STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED,
|
|
|
|
w->width - 101);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: {
|
|
|
|
int32 price;
|
|
|
|
|
|
|
|
DrawStringCentered(w->width>>1, 1, STR_7059_TRANSPORT_COMPANY_MERGER, 0);
|
|
|
|
COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2);
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(2, p->name_1);
|
|
|
|
SetDParam(3, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
price = WP(w,news_d).ni->params[2];
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(4, price);
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawStringMultiCenter(
|
|
|
|
((w->width - 101) >> 1) + 98,
|
|
|
|
90,
|
|
|
|
price==0 ? STR_707F_HAS_BEEN_TAKEN_OVER_BY : STR_705A_HAS_BEEN_SOLD_TO_FOR,
|
|
|
|
w->width - 101);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
DrawStringCentered(w->width>>1, 1, STR_705C_BANKRUPT, 0);
|
|
|
|
COPY_IN_DPARAM(0,WP(w,news_d).ni->params, 2);
|
|
|
|
DrawStringMultiCenter(
|
|
|
|
((w->width - 101) >> 1) + 98,
|
|
|
|
90,
|
|
|
|
STR_705D_HAS_BEEN_CLOSED_DOWN_BY,
|
|
|
|
w->width - 101);
|
2004-09-10 19:02:27 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
DrawStringCentered(w->width>>1, 1, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED, 0);
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
COPY_IN_DPARAM(2,WP(w,news_d).ni->params, 2);
|
|
|
|
DrawStringMultiCenter(
|
|
|
|
((w->width - 101) >> 1) + 98,
|
|
|
|
90,
|
|
|
|
STR_705F_STARTS_CONSTRUCTION_NEAR,
|
|
|
|
w->width - 101);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringID GetNewsStringBankrupcy(NewsItem *ni)
|
|
|
|
{
|
2005-06-21 16:28:17 +00:00
|
|
|
Player *p = GetPlayer(ni->string_id & 0xF);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
switch(ni->string_id >> 4) {
|
|
|
|
case 1:
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, STR_7056_TRANSPORT_COMPANY_IN_TROUBLE);
|
|
|
|
SetDParam(1, STR_7057_WILL_BE_SOLD_OFF_OR_DECLARED);
|
|
|
|
SetDParam(2, p->name_1);
|
|
|
|
SetDParam(3, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
return STR_02B6;
|
|
|
|
case 2:
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, STR_7059_TRANSPORT_COMPANY_MERGER);
|
|
|
|
SetDParam(1, STR_705A_HAS_BEEN_SOLD_TO_FOR);
|
2004-08-09 17:04:08 +00:00
|
|
|
COPY_IN_DPARAM(2,ni->params, 2);
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(4, p->name_1);
|
|
|
|
SetDParam(5, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
COPY_IN_DPARAM(6,ni->params + 2, 1);
|
|
|
|
return STR_02B6;
|
|
|
|
case 3:
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, STR_705C_BANKRUPT);
|
|
|
|
SetDParam(1, STR_705D_HAS_BEEN_CLOSED_DOWN_BY);
|
2004-08-09 17:04:08 +00:00
|
|
|
COPY_IN_DPARAM(2,ni->params, 2);
|
|
|
|
return STR_02B6;
|
|
|
|
case 4:
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, STR_705E_NEW_TRANSPORT_COMPANY_LAUNCHED);
|
|
|
|
SetDParam(1, STR_705F_STARTS_CONSTRUCTION_NEAR);
|
|
|
|
SetDParam(2, p->name_1);
|
|
|
|
SetDParam(3, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
COPY_IN_DPARAM(4,ni->params, 2);
|
|
|
|
return STR_02B6;
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* useless, but avoids compiler warning this way */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void PlayersGenStatistics(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
Player *p;
|
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->xy != 0) {
|
|
|
|
_current_player = st->owner;
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
|
|
|
|
SubtractMoneyFromPlayer(_price.station_value >> 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HASBIT(1<<0|1<<3|1<<6|1<<9, _cur_month))
|
|
|
|
return;
|
|
|
|
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (p->is_active) {
|
|
|
|
memmove(&p->old_economy, &p->cur_economy, sizeof(p->old_economy));
|
|
|
|
memset(&p->cur_economy, 0, sizeof(p->cur_economy));
|
|
|
|
|
|
|
|
if (p->num_valid_stat_ent != 24)
|
|
|
|
p->num_valid_stat_ent++;
|
|
|
|
|
2004-08-23 10:59:03 +00:00
|
|
|
UpdateCompanyRatingAndValue(p, true);
|
2004-08-09 17:04:08 +00:00
|
|
|
PlayersCheckBankrupt(p);
|
|
|
|
|
|
|
|
if (p->block_preview != 0)
|
|
|
|
p->block_preview--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindow(WC_INCOME_GRAPH, 0);
|
|
|
|
InvalidateWindow(WC_OPERATING_PROFIT, 0);
|
|
|
|
InvalidateWindow(WC_DELIVERED_CARGO, 0);
|
|
|
|
InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
|
|
|
|
InvalidateWindow(WC_COMPANY_VALUE, 0);
|
|
|
|
InvalidateWindow(WC_COMPANY_LEAGUE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddSingleInflation(int32 *value, uint16 *frac, int32 amt)
|
|
|
|
{
|
|
|
|
int64 tmp;
|
|
|
|
int32 low;
|
|
|
|
tmp = BIGMULS(*value, amt);
|
|
|
|
*frac = (uint16)(low = (uint16)tmp + *frac);
|
|
|
|
*value += (int32)(tmp >> 16) + (low >> 16);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void AddInflation(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int32 inf = _economy.infl_amount * 54;
|
|
|
|
|
|
|
|
for(i=0; i!=NUM_PRICES; i++) {
|
|
|
|
AddSingleInflation( (int32*)&_price + i, _price_frac + i, inf );
|
|
|
|
}
|
|
|
|
|
|
|
|
_economy.max_loan_unround += BIGMULUS(_economy.max_loan_unround, inf, 16);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_economy.max_loan + 50000 <= _economy.max_loan_unround)
|
|
|
|
_economy.max_loan += 50000;
|
|
|
|
|
|
|
|
inf = _economy.infl_amount_pr * 54;
|
|
|
|
for(i=0; i!=NUM_CARGO; i++) {
|
2004-09-10 19:02:27 +00:00
|
|
|
AddSingleInflation(
|
2005-06-01 11:52:44 +00:00
|
|
|
(int32*)_cargo_payment_rates + i,
|
2004-08-09 17:04:08 +00:00
|
|
|
_cargo_payment_rates_frac + i,
|
|
|
|
inf
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindowClasses(WC_BUILD_VEHICLE);
|
2005-01-22 23:41:23 +00:00
|
|
|
InvalidateWindowClasses(WC_REPLACE_VEHICLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindowClasses(WC_VEHICLE_DETAILS);
|
|
|
|
InvalidateWindow(WC_PAYMENT_RATES, 0);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void PlayersPayInterest(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
int interest = _economy.interest_rate * 54;
|
|
|
|
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (!p->is_active)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
_current_player = p->index;
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_LOAN_INT);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SubtractMoneyFromPlayer(BIGMULUS(p->current_loan, interest, 16));
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
|
|
|
SubtractMoneyFromPlayer(_price.station_value >> 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void HandleEconomyFluctuations(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (_opt.diff.economy == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (--_economy.fluct == 0) {
|
|
|
|
_economy.fluct = - (int)(Random()&3);
|
|
|
|
AddNewsItem(STR_7073_WORLD_RECESSION_FINANCIAL, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0);
|
|
|
|
} else if (_economy.fluct == -12) {
|
|
|
|
_economy.fluct = (Random()&255) + 312;
|
|
|
|
AddNewsItem(STR_7074_RECESSION_OVER_UPTURN_IN, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static byte _price_category[NUM_PRICES] = {
|
|
|
|
0, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
2, 2, 1, 1, 1, 1, 1, 1,
|
|
|
|
2,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int32 _price_base[NUM_PRICES] = {
|
2004-08-10 16:49:56 +00:00
|
|
|
100, // station_value
|
|
|
|
100, // build_rail
|
|
|
|
95, // build_road
|
|
|
|
65, // build_signals
|
|
|
|
275, // build_bridge
|
|
|
|
600, // build_train_depot
|
|
|
|
500, // build_road_depot
|
|
|
|
700, // build_ship_depot
|
|
|
|
450, // build_tunnel
|
|
|
|
200, // train_station_track
|
|
|
|
180, // train_station_length
|
|
|
|
600, // build_airport
|
|
|
|
200, // build_bus_station
|
|
|
|
200, // build_truck_station
|
|
|
|
350, // build_dock
|
|
|
|
400000, // build_railvehicle
|
|
|
|
2000, // build_railwagon
|
|
|
|
700000, // aircraft_base
|
|
|
|
14000, // roadveh_base
|
|
|
|
65000, // ship_base
|
|
|
|
20, // build_trees
|
|
|
|
250, // terraform
|
|
|
|
20, // clear_1
|
|
|
|
40, // purchase_land
|
|
|
|
200, // clear_2
|
|
|
|
500, // clear_3
|
|
|
|
20, // remove_trees
|
|
|
|
-70, // remove_rail
|
|
|
|
10, // remove_signals
|
|
|
|
50, // clear_bridge
|
|
|
|
80, // remove_train_depot
|
|
|
|
80, // remove_road_depot
|
|
|
|
90, // remove_ship_depot
|
|
|
|
30, // clear_tunnel
|
|
|
|
10000, // clear_water
|
|
|
|
50, // remove_rail_station
|
|
|
|
30, // remove_airport
|
|
|
|
50, // remove_bus_station
|
|
|
|
50, // remove_truck_station
|
|
|
|
55, // remove_dock
|
|
|
|
1600, // remove_house
|
|
|
|
40, // remove_road
|
|
|
|
5600, // running_rail[0] railroad
|
|
|
|
5200, // running_rail[1] monorail
|
|
|
|
4800, // running_rail[2] maglev
|
|
|
|
9600, // aircraft_running
|
|
|
|
1600, // roadveh_running
|
|
|
|
5600, // ship_running
|
|
|
|
1000000, // build_industry
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void StartupEconomy(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
assert(sizeof(_price) == NUM_PRICES * sizeof(int32));
|
|
|
|
|
|
|
|
for(i=0; i!=NUM_PRICES; i++) {
|
|
|
|
int32 price = _price_base[i];
|
|
|
|
if (_price_category[i] != 0) {
|
|
|
|
uint mod = _price_category[i] == 1 ? _opt.diff.vehicle_costs : _opt.diff.construction_cost;
|
|
|
|
if (mod < 1) {
|
|
|
|
price = price * 3 >> 2;
|
|
|
|
} else if (mod > 1) {
|
|
|
|
price = price * 9 >> 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
((int32*)&_price)[i] = price;
|
|
|
|
_price_frac[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_economy.interest_rate = _opt.diff.initial_interest;
|
|
|
|
_economy.infl_amount = _opt.diff.initial_interest;
|
|
|
|
_economy.infl_amount_pr = max(0, _opt.diff.initial_interest - 1);
|
|
|
|
_economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000;
|
|
|
|
_economy.fluct = (byte)(Random()) + 168;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pair SetupSubsidyDecodeParam(Subsidy *s, bool mode)
|
|
|
|
{
|
|
|
|
int tile, tile2;
|
|
|
|
Industry *i;
|
|
|
|
Town *t;
|
|
|
|
Station *st;
|
|
|
|
Pair tp;
|
|
|
|
|
|
|
|
/* if mode is false, convert into plural */
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, _cargoc.names_s[s->cargo_type] + (mode?0:32));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (s->age < 12) {
|
|
|
|
if (!(s->cargo_type == CT_PASSENGERS || s->cargo_type == CT_MAIL)) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, STR_2029);
|
2005-01-06 22:31:58 +00:00
|
|
|
i = GetIndustry(s->from);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile = i->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(2, i->town->townnametype);
|
|
|
|
SetDParam(3, i->town->townnameparts);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(4, i->type + STR_4802_COAL_MINE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (s->cargo_type != CT_GOODS && s->cargo_type != CT_FOOD) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(5, STR_2029);
|
2005-01-06 22:31:58 +00:00
|
|
|
i = GetIndustry(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile2 = i->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(8, i->type + STR_4802_COAL_MINE);
|
|
|
|
SetDParam(6, i->town->townnametype);
|
|
|
|
SetDParam(7, i->town->townnameparts);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
t = GetTown(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile2 = t->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(5, t->townnametype);
|
|
|
|
SetDParam(6, t->townnameparts);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
t = GetTown(s->from);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile = t->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, t->townnametype);
|
|
|
|
SetDParam(2, t->townnameparts);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
t = GetTown(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile2 = t->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(3, t->townnametype);
|
|
|
|
SetDParam(4, t->townnameparts);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(s->from);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile = st->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
tile2 = st->xy;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(2, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tp.a = tile;
|
|
|
|
tp.b = tile2;
|
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
2004-12-28 17:40:15 +00:00
|
|
|
void DeleteSubsidyWithIndustry(uint16 index)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Subsidy *s;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
for(s=_subsidies; s != endof(_subsidies); s++) {
|
|
|
|
if (s->cargo_type != 0xFF && s->age < 12 &&
|
|
|
|
s->cargo_type != CT_PASSENGERS && s->cargo_type != CT_MAIL &&
|
|
|
|
(index == s->from || (s->cargo_type!=CT_GOODS && s->cargo_type!=CT_FOOD && index==s->to))) {
|
|
|
|
s->cargo_type = 0xFF;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 17:40:15 +00:00
|
|
|
void DeleteSubsidyWithStation(uint16 index)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Subsidy *s;
|
|
|
|
bool dirty = false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
for(s=_subsidies; s != endof(_subsidies); s++) {
|
|
|
|
if (s->cargo_type != 0xFF && s->age >= 12 &&
|
|
|
|
(s->from == index || s->to == index)) {
|
|
|
|
s->cargo_type = 0xFF;
|
|
|
|
dirty = true;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct FoundRoute {
|
|
|
|
uint distance;
|
|
|
|
byte cargo;
|
|
|
|
void *from;
|
|
|
|
void *to;
|
|
|
|
} FoundRoute;
|
|
|
|
|
|
|
|
static void FindSubsidyPassengerRoute(FoundRoute *fr)
|
|
|
|
{
|
|
|
|
Town *from,*to;
|
|
|
|
|
|
|
|
fr->distance = (uint)-1;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
fr->from = from = GetTown(RandomRange(_total_towns));
|
2004-08-09 17:04:08 +00:00
|
|
|
if (from->xy == 0 || from->population < 400)
|
|
|
|
return;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
fr->to = to = GetTown(RandomRange(_total_towns));
|
2004-08-09 17:04:08 +00:00
|
|
|
if (from==to || to->xy == 0 || to->population < 400 || to->pct_pass_transported > 42)
|
|
|
|
return;
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(from->xy, to->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void FindSubsidyCargoRoute(FoundRoute *fr)
|
|
|
|
{
|
|
|
|
Industry *i;
|
|
|
|
int trans, total;
|
|
|
|
byte cargo;
|
|
|
|
|
|
|
|
fr->distance = (uint)-1;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
fr->from = i = GetIndustry(RandomRange(_total_industries));
|
2004-08-09 17:04:08 +00:00
|
|
|
if (i->xy == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Randomize cargo type
|
2004-12-04 17:54:56 +00:00
|
|
|
if (Random()&1 && i->produced_cargo[1] != 0xFF) {
|
2004-08-09 17:04:08 +00:00
|
|
|
cargo = i->produced_cargo[1];
|
|
|
|
trans = i->pct_transported[1];
|
|
|
|
total = i->total_production[1];
|
|
|
|
} else {
|
|
|
|
cargo = i->produced_cargo[0];
|
|
|
|
trans = i->pct_transported[0];
|
|
|
|
total = i->total_production[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quit if no production in this industry
|
|
|
|
// or if the cargo type is passengers
|
|
|
|
// or if the pct transported is already large enough
|
|
|
|
if (total == 0 || trans > 42 || cargo == 0xFF || cargo == CT_PASSENGERS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fr->cargo = cargo;
|
|
|
|
|
|
|
|
if (cargo == CT_GOODS || cargo == CT_FOOD) {
|
|
|
|
// The destination is a town
|
2005-01-06 22:31:58 +00:00
|
|
|
Town *t = GetTown(RandomRange(_total_towns));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Only want big towns
|
|
|
|
if (t->xy == 0 || t->population < 900)
|
|
|
|
return;
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(i->xy, t->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
fr->to = t;
|
|
|
|
} else {
|
|
|
|
// The destination is an industry
|
2005-01-06 22:31:58 +00:00
|
|
|
Industry *i2 = GetIndustry(RandomRange(_total_industries));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// The industry must accept the cargo
|
2004-09-10 19:02:27 +00:00
|
|
|
if (i == i2 || i2->xy == 0 ||
|
2004-08-09 17:04:08 +00:00
|
|
|
(cargo != i2->accepts_cargo[0] &&
|
|
|
|
cargo != i2->accepts_cargo[1] &&
|
|
|
|
cargo != i2->accepts_cargo[2]))
|
|
|
|
return;
|
2005-01-31 07:23:15 +00:00
|
|
|
fr->distance = DistanceManhattan(i->xy, i2->xy);
|
2004-08-09 17:04:08 +00:00
|
|
|
fr->to = i2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static bool CheckSubsidyDuplicate(Subsidy *s)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Subsidy *ss;
|
|
|
|
|
|
|
|
for(ss=_subsidies; ss != endof(_subsidies); ss++) {
|
2004-09-10 19:02:27 +00:00
|
|
|
if (s != ss &&
|
|
|
|
// ss->age < 12 &&
|
|
|
|
ss->from == s->from &&
|
|
|
|
ss->to == s->to &&
|
2004-08-09 17:04:08 +00:00
|
|
|
ss->cargo_type == s->cargo_type) {
|
|
|
|
s->cargo_type = 0xFF;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-09-13 22:49:11 +00:00
|
|
|
void RemoteSubsidyAdd(Subsidy *s_new)
|
|
|
|
{
|
|
|
|
Subsidy *s;
|
|
|
|
Pair pair;
|
|
|
|
|
|
|
|
// search the first free subsidy
|
2004-09-13 23:43:54 +00:00
|
|
|
for(s=_subsidies; s != endof(_subsidies); s++)
|
2004-09-13 22:49:11 +00:00
|
|
|
if (s->cargo_type == 0xFF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
memcpy(s,s_new,sizeof(Subsidy));
|
|
|
|
|
|
|
|
pair = SetupSubsidyDecodeParam(s, 0);
|
|
|
|
AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void SubsidyMonthlyHandler(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Subsidy *s;
|
|
|
|
Pair pair;
|
|
|
|
Station *st;
|
|
|
|
uint n;
|
|
|
|
FoundRoute fr;
|
|
|
|
bool modified = false;
|
|
|
|
|
|
|
|
for(s=_subsidies; s != endof(_subsidies); s++) {
|
|
|
|
if (s->cargo_type == 0xFF)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (s->age == 12-1) {
|
|
|
|
pair = SetupSubsidyDecodeParam(s, 1);
|
|
|
|
AddNewsItem(STR_202E_OFFER_OF_SUBSIDY_EXPIRED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
|
|
|
|
s->cargo_type = 0xFF;
|
|
|
|
modified = true;
|
|
|
|
} else if (s->age == 2*12-1) {
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(s->to);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->owner == _local_player) {
|
|
|
|
pair = SetupSubsidyDecodeParam(s, 1);
|
|
|
|
AddNewsItem(STR_202F_SUBSIDY_WITHDRAWN_SERVICE, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
|
|
|
|
}
|
|
|
|
s->cargo_type = 0xFF;
|
|
|
|
modified = true;
|
|
|
|
} else {
|
|
|
|
s->age++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 25% chance to go on
|
2004-12-04 17:54:56 +00:00
|
|
|
if (CHANCE16(1,4)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// Find a free slot
|
|
|
|
s = _subsidies;
|
|
|
|
while (s->cargo_type != 0xFF) {
|
|
|
|
if (++s == endof(_subsidies))
|
|
|
|
goto no_add;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
n = 1000;
|
|
|
|
do {
|
|
|
|
FindSubsidyPassengerRoute(&fr);
|
|
|
|
if (fr.distance <= 70) {
|
|
|
|
s->cargo_type = CT_PASSENGERS;
|
|
|
|
s->from = ((Town*)fr.from)->index;
|
|
|
|
s->to = ((Town*)fr.to)->index;
|
|
|
|
goto add_subsidy;
|
|
|
|
}
|
|
|
|
FindSubsidyCargoRoute(&fr);
|
|
|
|
if (fr.distance <= 70) {
|
|
|
|
s->cargo_type = fr.cargo;
|
2005-01-06 22:31:58 +00:00
|
|
|
s->from = ((Industry*)fr.from)->index;
|
|
|
|
s->to = (fr.cargo == CT_GOODS || fr.cargo == CT_FOOD) ? ((Town*)fr.to)->index : ((Industry*)fr.to)->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
add_subsidy:
|
|
|
|
if (!CheckSubsidyDuplicate(s)) {
|
|
|
|
s->age = 0;
|
|
|
|
pair = SetupSubsidyDecodeParam(s, 0);
|
|
|
|
AddNewsItem(STR_2030_SERVICE_SUBSIDY_OFFERED, NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0), pair.a, pair.b);
|
|
|
|
modified = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-09-13 22:49:11 +00:00
|
|
|
} while (n--);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
no_add:;
|
|
|
|
if (modified)
|
|
|
|
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
|
|
|
|
}
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _subsidies_desc[] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_VAR(Subsidy,cargo_type, SLE_UINT8),
|
|
|
|
SLE_VAR(Subsidy,age, SLE_UINT8),
|
2004-12-28 17:40:15 +00:00
|
|
|
SLE_CONDVAR(Subsidy,from, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
|
|
|
|
SLE_CONDVAR(Subsidy,from, SLE_UINT16, 5, 255),
|
|
|
|
SLE_CONDVAR(Subsidy,to, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
|
|
|
|
SLE_CONDVAR(Subsidy,to, SLE_UINT16, 5, 255),
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Save_SUBS(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Subsidy *s;
|
|
|
|
|
|
|
|
for(i=0; i!=lengthof(_subsidies); i++) {
|
|
|
|
s = &_subsidies[i];
|
|
|
|
if (s->cargo_type != 0xFF) {
|
|
|
|
SlSetArrayIndex(i);
|
|
|
|
SlObject(s, _subsidies_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
static void Load_SUBS(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1)
|
|
|
|
SlObject(&_subsidies[index], _subsidies_desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, byte cargo_type)
|
|
|
|
{
|
|
|
|
int cargo = cargo_type;
|
|
|
|
byte f;
|
|
|
|
|
|
|
|
/* zero the distance if it's the bank and very short transport. */
|
|
|
|
if (_opt.landscape == LT_NORMAL && cargo == CT_VALUABLES && dist < 10)
|
|
|
|
dist = 0;
|
|
|
|
|
|
|
|
f = 255;
|
|
|
|
if (transit_days > _cargoc.transit_days_1[cargo]) {
|
|
|
|
transit_days -= _cargoc.transit_days_1[cargo];
|
|
|
|
f -= transit_days;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (transit_days > _cargoc.transit_days_2[cargo]) {
|
|
|
|
transit_days -= _cargoc.transit_days_2[cargo];
|
|
|
|
|
|
|
|
if (f < transit_days)
|
|
|
|
f = 0;
|
|
|
|
else
|
|
|
|
f -= transit_days;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f < 31) f = 31;
|
|
|
|
|
|
|
|
return BIGMULSS(dist * f * num_pieces, _cargo_payment_rates[cargo], 21);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DeliverGoodsToIndustry(TileIndex xy, byte cargo_type, int num_pieces)
|
|
|
|
{
|
2004-09-10 19:02:27 +00:00
|
|
|
Industry *ind, *best;
|
2004-08-09 17:04:08 +00:00
|
|
|
int t, u;
|
|
|
|
|
|
|
|
/* Check if there's an industry close to the station that accepts
|
|
|
|
* the cargo */
|
|
|
|
best = NULL;
|
2004-08-12 22:29:37 +00:00
|
|
|
u = _patches.station_spread + 8;
|
2004-12-30 10:03:35 +00:00
|
|
|
FOR_ALL_INDUSTRIES(ind) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (ind->xy != 0 && (cargo_type == ind->accepts_cargo[0] || cargo_type
|
|
|
|
== ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]) &&
|
|
|
|
ind->produced_cargo[0] != 0xFF &&
|
|
|
|
ind->produced_cargo[0] != cargo_type &&
|
2005-04-03 06:26:31 +00:00
|
|
|
(t = DistanceManhattan(ind->xy, xy)) < 2 * u) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u = t;
|
|
|
|
best = ind;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Found one? */
|
|
|
|
if (best != NULL) {
|
|
|
|
best->was_cargo_delivered = true;
|
|
|
|
best->cargo_waiting[0] = min(best->cargo_waiting[0] + num_pieces, 0xFFFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CheckSubsidised(Station *from, Station *to, byte cargo_type)
|
|
|
|
{
|
|
|
|
Subsidy *s;
|
|
|
|
TileIndex xy;
|
|
|
|
Pair pair;
|
|
|
|
Player *p;
|
|
|
|
|
|
|
|
// check if there is an already existing subsidy that applies to us
|
|
|
|
for(s=_subsidies; s != endof(_subsidies); s++) {
|
|
|
|
if (s->cargo_type == cargo_type &&
|
|
|
|
s->age >= 12 &&
|
|
|
|
s->from == from->index &&
|
|
|
|
s->to == to->index)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if there's a new subsidy that applies.. */
|
|
|
|
for(s=_subsidies; s != endof(_subsidies); s++) {
|
|
|
|
if (s->cargo_type == cargo_type && s->age < 12) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Check distance from source */
|
|
|
|
if (cargo_type == CT_PASSENGERS || cargo_type == CT_MAIL) {
|
2005-01-06 22:31:58 +00:00
|
|
|
xy = GetTown(s->from)->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
xy = (GetIndustry(s->from))->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceMax(xy, from->xy) > 9)
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Check distance from dest */
|
|
|
|
if (cargo_type == CT_PASSENGERS || cargo_type == CT_MAIL || cargo_type == CT_GOODS || cargo_type == CT_FOOD) {
|
2005-01-06 22:31:58 +00:00
|
|
|
xy = GetTown(s->to)->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
xy = (GetIndustry(s->to))->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceMax(xy, to->xy) > 9)
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Found a subsidy, change the values to indicate that it's in use */
|
|
|
|
s->age = 12;
|
|
|
|
s->from = from->index;
|
|
|
|
s->to = to->index;
|
|
|
|
|
|
|
|
/* Add a news item */
|
|
|
|
pair = SetupSubsidyDecodeParam(s, 0);
|
2005-02-06 08:18:00 +00:00
|
|
|
InjectDParam(2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(_current_player);
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem(
|
2004-09-10 19:02:27 +00:00
|
|
|
STR_2031_SERVICE_SUBSIDY_AWARDED + _opt.diff.subsidy_multiplier,
|
2004-08-09 17:04:08 +00:00
|
|
|
NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_SUBSIDIES, 0),
|
|
|
|
pair.a, pair.b);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_SUBSIDIES_LIST, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-02-02 16:16:43 +00:00
|
|
|
static int32 DeliverGoods(int num_pieces, byte cargo_type, uint16 source, uint16 dest, byte days_in_transit)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
bool subsidised;
|
|
|
|
Station *s_from, *s_to;
|
|
|
|
int32 profit;
|
|
|
|
|
|
|
|
assert(num_pieces > 0);
|
|
|
|
|
|
|
|
// Update player statistics
|
|
|
|
{
|
2005-06-21 16:28:17 +00:00
|
|
|
Player *p = GetPlayer(_current_player);
|
2004-08-09 17:04:08 +00:00
|
|
|
p->cur_economy.delivered_cargo += num_pieces;
|
|
|
|
SETBIT(p->cargo_types, cargo_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get station pointers.
|
2005-01-06 22:31:58 +00:00
|
|
|
s_from = GetStation(source);
|
|
|
|
s_to = GetStation(dest);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Check if a subsidy applies.
|
|
|
|
subsidised = CheckSubsidised(s_from, s_to, cargo_type);
|
|
|
|
|
|
|
|
// Increase town's counter for some special goods types
|
2005-06-15 16:58:15 +00:00
|
|
|
if (cargo_type == CT_FOOD) s_to->town->new_act_food += num_pieces;
|
|
|
|
if (cargo_type == CT_WATER) s_to->town->new_act_water += num_pieces;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Give the goods to the industry.
|
|
|
|
DeliverGoodsToIndustry(s_to->xy, cargo_type, num_pieces);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Determine profit
|
2005-06-15 16:58:15 +00:00
|
|
|
profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(s_from->xy, s_to->xy), days_in_transit, cargo_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Modify profit if a subsidy is in effect
|
|
|
|
if (subsidised) {
|
|
|
|
if (_opt.diff.subsidy_multiplier < 1) {
|
|
|
|
/* 1.5x */
|
|
|
|
profit += profit >> 1;
|
|
|
|
} else if (_opt.diff.subsidy_multiplier == 1) {
|
|
|
|
/* 2x */
|
|
|
|
profit *= 2;
|
|
|
|
} else if (_opt.diff.subsidy_multiplier == 2) {
|
|
|
|
/* 3x */
|
|
|
|
profit *= 3;
|
|
|
|
} else {
|
|
|
|
/* 4x */
|
|
|
|
profit *= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computers get 25% extra profit if they're intelligent.
|
|
|
|
if (_opt.diff.competitor_intelligence>=1 && !IS_HUMAN_PLAYER(_current_player))
|
|
|
|
profit += profit >> 2;
|
|
|
|
|
|
|
|
return profit;
|
|
|
|
}
|
|
|
|
|
2004-11-29 11:59:09 +00:00
|
|
|
/*
|
|
|
|
* Returns true if Vehicle v should wait loading because other vehicle is
|
|
|
|
* already loading the same cargo type
|
|
|
|
* v = vehicle to load, u = GetFirstInChain(v)
|
|
|
|
*/
|
|
|
|
static bool LoadWait(const Vehicle *v, const Vehicle *u) {
|
|
|
|
const Vehicle *w;
|
|
|
|
const Vehicle *x;
|
|
|
|
bool has_any_cargo = false;
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (!(u->current_order.flags & OF_FULL_LOAD)) return false;
|
2004-11-29 11:59:09 +00:00
|
|
|
|
|
|
|
for (w = u; w != NULL; w = w->next) {
|
|
|
|
if (w->cargo_count != 0) {
|
|
|
|
if (v->cargo_type == w->cargo_type &&
|
|
|
|
u->last_station_visited == w->cargo_source)
|
|
|
|
return false;
|
|
|
|
has_any_cargo = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(x) {
|
2005-01-19 19:15:03 +00:00
|
|
|
if ((x->type != VEH_Train || x->subtype == TS_Front_Engine) && // for all locs
|
2004-11-29 11:59:09 +00:00
|
|
|
u->last_station_visited == x->last_station_visited && // at the same station
|
|
|
|
!(x->vehstatus & VS_STOPPED) && // not stopped
|
2004-12-05 12:43:04 +00:00
|
|
|
x->current_order.type == OT_LOADING && // loading
|
2004-11-29 11:59:09 +00:00
|
|
|
u != x) { // not itself
|
|
|
|
bool other_has_any_cargo = false;
|
|
|
|
bool has_space_for_same_type = false;
|
|
|
|
bool other_has_same_type = false;
|
|
|
|
|
|
|
|
for (w = x; w != NULL; w = w->next) {
|
|
|
|
if (w->cargo_count < w->cargo_cap && v->cargo_type == w->cargo_type)
|
|
|
|
has_space_for_same_type = true;
|
|
|
|
|
|
|
|
if (w->cargo_count != 0) {
|
|
|
|
if (v->cargo_type == w->cargo_type &&
|
|
|
|
u->last_station_visited == w->cargo_source)
|
|
|
|
other_has_same_type = true;
|
|
|
|
other_has_any_cargo = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_space_for_same_type) {
|
|
|
|
if (other_has_same_type) return true;
|
|
|
|
if (other_has_any_cargo && !has_any_cargo) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
int LoadUnloadVehicle(Vehicle *v)
|
|
|
|
{
|
|
|
|
int profit = 0;
|
2005-06-15 16:58:15 +00:00
|
|
|
int v_profit; //virtual profit for feeder systems
|
|
|
|
int v_profit_total = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
int unloading_time = 20;
|
|
|
|
Vehicle *u = v;
|
|
|
|
int result = 0;
|
2005-02-02 16:16:43 +00:00
|
|
|
uint16 last_visited;
|
2004-08-09 17:04:08 +00:00
|
|
|
Station *st;
|
|
|
|
GoodsEntry *ge;
|
|
|
|
int t;
|
|
|
|
uint count, cap;
|
2004-09-15 18:36:33 +00:00
|
|
|
byte old_player;
|
2004-11-17 08:52:47 +00:00
|
|
|
bool completely_empty = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
assert(v->current_order.type == OT_LOADING);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cur_speed = 0;
|
2005-03-24 14:33:05 +00:00
|
|
|
|
2005-03-25 16:10:33 +00:00
|
|
|
old_player = _current_player;
|
|
|
|
_current_player = v->owner;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(last_visited = v->last_station_visited);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 12:47:25 +00:00
|
|
|
for (; v != NULL; v = v->next) {
|
|
|
|
if (v->cargo_cap == 0) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
ge = &st->goods[v->cargo_type];
|
|
|
|
|
|
|
|
/* unload? */
|
|
|
|
if (v->cargo_count != 0) {
|
2005-06-15 16:58:15 +00:00
|
|
|
if ( v->cargo_source != last_visited && ge->waiting_acceptance & 0x8000 && !(u->current_order.flags & OF_TRANSFER) ) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// deliver goods to the station
|
2005-03-03 14:56:28 +00:00
|
|
|
st->time_since_unload = 0;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
unloading_time += v->cargo_count; /* TTDBUG: bug in original TTD */
|
|
|
|
profit += DeliverGoods(v->cargo_count, v->cargo_type, v->cargo_source, last_visited, v->cargo_days);
|
|
|
|
result |= 1;
|
|
|
|
v->cargo_count = 0;
|
2005-06-15 16:58:15 +00:00
|
|
|
} else if (u->current_order.flags & ( OF_UNLOAD | OF_TRANSFER) ) {
|
2004-08-09 17:04:08 +00:00
|
|
|
/* unload goods and let it wait at the station */
|
|
|
|
st->time_since_unload = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-06-15 16:58:15 +00:00
|
|
|
v_profit = GetTransportedGoodsIncome(
|
|
|
|
v->cargo_count,
|
|
|
|
DistanceManhattan(GetStation(v->cargo_source)->xy, GetStation(last_visited)->xy),
|
|
|
|
v->cargo_days,
|
|
|
|
v->cargo_type) * 3 / 2;
|
|
|
|
|
|
|
|
v_profit_total += v_profit;
|
|
|
|
|
|
|
|
|
|
|
|
unloading_time += v->cargo_count;
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((t=ge->waiting_acceptance & 0xFFF) == 0) {
|
|
|
|
// No goods waiting at station
|
|
|
|
ge->enroute_time = v->cargo_days;
|
|
|
|
ge->enroute_from = v->cargo_source;
|
|
|
|
} else {
|
|
|
|
// Goods already waiting at station. Set counters to the worst value.
|
|
|
|
if (v->cargo_days >= ge->enroute_time)
|
|
|
|
ge->enroute_time = v->cargo_days;
|
2005-02-02 16:16:43 +00:00
|
|
|
if (last_visited != ge->enroute_from)
|
2004-08-09 17:04:08 +00:00
|
|
|
ge->enroute_from = v->cargo_source;
|
|
|
|
}
|
|
|
|
// Update amount of waiting cargo
|
|
|
|
ge->waiting_acceptance = (ge->waiting_acceptance &~0xFFF) | min(v->cargo_count + t, 0xFFF);
|
2005-06-15 16:58:15 +00:00
|
|
|
ge->feeder_profit += v_profit;
|
|
|
|
u->profit_this_year += v_profit;
|
2004-08-09 17:04:08 +00:00
|
|
|
result |= 2;
|
|
|
|
v->cargo_count = 0;
|
|
|
|
}
|
2004-11-17 08:52:47 +00:00
|
|
|
|
|
|
|
if (v->cargo_count != 0)
|
|
|
|
completely_empty = false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* don't pick up goods that we unloaded */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (u->current_order.flags & OF_UNLOAD) continue;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* update stats */
|
|
|
|
ge->days_since_pickup = 0;
|
|
|
|
t = u->max_speed;
|
|
|
|
if (u->type == VEH_Road) t >>=1;
|
2005-06-05 15:37:00 +00:00
|
|
|
if (u->type == VEH_Train) t = u->u.rail.cached_max_speed;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// if last speed is 0, we treat that as if no vehicle has ever visited the station.
|
|
|
|
ge->last_speed = t < 255 ? t : 255;
|
|
|
|
ge->last_age = _cur_year - v->build_year;
|
|
|
|
|
|
|
|
// If there's goods waiting at the station, and the vehicle
|
|
|
|
// has capacity for it, load it on the vehicle.
|
|
|
|
if ((count=ge->waiting_acceptance & 0xFFF) != 0 &&
|
|
|
|
(cap = v->cargo_cap - v->cargo_count) != 0) {
|
2005-06-15 16:58:15 +00:00
|
|
|
int cargoshare;
|
|
|
|
int feeder_profit_share;
|
|
|
|
|
2004-11-17 08:52:47 +00:00
|
|
|
if (v->cargo_count == 0)
|
|
|
|
TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
|
|
|
|
|
2004-11-29 11:59:09 +00:00
|
|
|
/* Skip loading this vehicle if another train/vehicle is already handling
|
|
|
|
* the same cargo type at this station */
|
|
|
|
if (_patches.improved_load && LoadWait(v,u)) continue;
|
|
|
|
|
2004-11-17 08:52:47 +00:00
|
|
|
/* TODO: Regarding this, when we do gradual loading, we
|
|
|
|
* should first unload all vehicles and then start
|
|
|
|
* loading them. Since this will cause
|
|
|
|
* VEHICLE_TRIGGER_EMPTY to be called at the time when
|
|
|
|
* the whole vehicle chain is really totally empty, the
|
|
|
|
* @completely_empty assignment can then be safely
|
|
|
|
* removed; that's how TTDPatch behaves too. --pasky */
|
|
|
|
completely_empty = false;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cap > count) cap = count;
|
2005-06-15 16:58:15 +00:00
|
|
|
cargoshare = cap * 10000 / ge->waiting_acceptance;
|
|
|
|
feeder_profit_share = ge->feeder_profit * cargoshare / 10000;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cargo_count += cap;
|
|
|
|
ge->waiting_acceptance -= cap;
|
2005-06-15 16:58:15 +00:00
|
|
|
v->profit_this_year -= feeder_profit_share;
|
|
|
|
ge->feeder_profit -= feeder_profit_share;
|
2004-08-09 17:04:08 +00:00
|
|
|
unloading_time += cap;
|
|
|
|
st->time_since_load = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// And record the source of the cargo, and the days in travel.
|
2005-06-15 16:58:15 +00:00
|
|
|
v->cargo_source = st->index; //changed this for feeder systems
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cargo_days = ge->enroute_time;
|
|
|
|
result |= 2;
|
|
|
|
st->last_vehicle = v->index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-15 16:58:15 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v = u;
|
|
|
|
|
2005-06-15 16:58:15 +00:00
|
|
|
if (v_profit_total > 0)
|
|
|
|
ShowFeederIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, v_profit_total);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->type == VEH_Train) {
|
|
|
|
int num = - (int)GetStationPlatforms(st, v->tile) * 2;
|
|
|
|
do num++; while ( (v=v->next) != NULL);
|
|
|
|
if (num > 0) {
|
|
|
|
unloading_time <<=1;
|
|
|
|
unloading_time += num * unloading_time;
|
|
|
|
}
|
|
|
|
v = u;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->load_unload_time_rem = unloading_time;
|
|
|
|
|
2004-11-17 08:52:47 +00:00
|
|
|
if (completely_empty) {
|
|
|
|
TriggerVehicle(v, VEHICLE_TRIGGER_EMPTY);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (result != 0) {
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
|
|
|
|
if (result & 2)
|
|
|
|
InvalidateWindow(WC_STATION_VIEW, last_visited);
|
|
|
|
|
|
|
|
if (profit != 0) {
|
|
|
|
v->profit_this_year += profit;
|
|
|
|
SubtractMoneyFromPlayer(-profit);
|
|
|
|
|
|
|
|
if (_current_player == _local_player)
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_14_CASHTILL, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, -profit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-15 18:36:33 +00:00
|
|
|
_current_player = old_player;
|
2004-08-09 17:04:08 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void PlayersMonthlyLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
PlayersGenStatistics();
|
2005-04-16 20:37:17 +00:00
|
|
|
if (_patches.inflation && _cur_year < MAX_YEAR_END)
|
2004-08-09 17:04:08 +00:00
|
|
|
AddInflation();
|
|
|
|
PlayersPayInterest();
|
2004-09-11 19:34:11 +00:00
|
|
|
// Reset the _current_player flag
|
|
|
|
_current_player = OWNER_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
HandleEconomyFluctuations();
|
|
|
|
SubsidyMonthlyHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DoAcquireCompany(Player *p)
|
|
|
|
{
|
|
|
|
Player *owner;
|
|
|
|
int i,pi;
|
2004-09-11 09:40:19 +00:00
|
|
|
int64 value;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, p->name_1);
|
|
|
|
SetDParam(1, p->name_2);
|
|
|
|
SetDParam(2, p->bankrupt_value);
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem( (StringID)(_current_player + 16*2), NEWS_FLAGS(NM_CALLBACK, 0, NT_COMPANY_INFO, DNC_BANKRUPCY),0,0);
|
|
|
|
|
|
|
|
// original code does this a little bit differently
|
|
|
|
pi = p->index;
|
|
|
|
ChangeOwnershipOfPlayerItems(pi, _current_player);
|
|
|
|
|
|
|
|
if (p->bankrupt_value == 0) {
|
2005-06-21 16:28:17 +00:00
|
|
|
owner = GetPlayer(_current_player);
|
2004-08-09 17:04:08 +00:00
|
|
|
owner->current_loan += p->current_loan;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = CalculateCompanyValue(p) >> 2;
|
|
|
|
for(i=0; i!=4; i++) {
|
|
|
|
if (p->share_owners[i] != 0xFF) {
|
2005-06-21 16:28:17 +00:00
|
|
|
owner = GetPlayer(p->share_owners[i]);
|
2004-08-09 17:04:08 +00:00
|
|
|
owner->money64 += value;
|
|
|
|
owner->yearly_expenses[0][EXPENSES_OTHER] += value;
|
|
|
|
UpdatePlayerMoney32(owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->is_active = false;
|
|
|
|
|
|
|
|
DeletePlayerWindows(pi);
|
2005-01-10 12:14:15 +00:00
|
|
|
RebuildVehicleLists(); //Updates the open windows to add the newly acquired vehicles to the lists
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-12 14:00:25 +00:00
|
|
|
extern int GetAmountOwnedBy(Player *p, byte owner);
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Acquire shares in an opposing company.
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 player to buy the shares from
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdBuyShareInCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Player *p;
|
2004-09-11 09:40:19 +00:00
|
|
|
int64 cost;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
/* Check if buying shares is allowed (protection against modified clients */
|
|
|
|
if (p1 >= MAX_PLAYERS || !_patches.allow_shares) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(p1);
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* Protect new companies from hostile takeovers */
|
|
|
|
if (_cur_year - p->inaugurated_year < 6) return_cmd_error(STR_7080_PROTECTED);
|
2005-01-07 23:59:59 +00:00
|
|
|
|
2004-12-12 14:00:25 +00:00
|
|
|
/* Those lines are here for network-protection (clients can be slow) */
|
2005-05-12 00:11:37 +00:00
|
|
|
if (GetAmountOwnedBy(p, OWNER_SPECTATOR) == 0) return 0;
|
2005-01-07 23:59:59 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* We can not buy out a real player (temporarily). TODO: well, enable it obviously */
|
|
|
|
if (GetAmountOwnedBy(p, OWNER_SPECTATOR) == 1 && !p->is_ai) return 0;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
cost = CalculateCompanyValue(p) >> 2;
|
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 00:11:37 +00:00
|
|
|
int i;
|
|
|
|
byte *b = p->share_owners;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
while (*b != 0xFF) b++; /* share owners is guaranteed to contain at least one 0xFF */
|
|
|
|
*b = _current_player;
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
for (i = 0; p->share_owners[i] == _current_player;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (++i == 4) {
|
|
|
|
p->bankrupt_value = 0;
|
|
|
|
DoAcquireCompany(p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InvalidateWindow(WC_COMPANY, (int)p1);
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Sell shares in an opposing company.
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 player to sell the shares from
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdSellShareInCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Player *p;
|
2004-09-11 09:40:19 +00:00
|
|
|
int64 cost;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
/* Check if buying shares is allowed (protection against modified clients */
|
|
|
|
if (p1 >= MAX_PLAYERS || !_patches.allow_shares) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-14 18:17:24 +00:00
|
|
|
/* Those lines are here for network-protection (clients can be slow) */
|
2005-05-12 00:11:37 +00:00
|
|
|
if (GetAmountOwnedBy(p, _current_player) == 0) return 0;
|
2004-12-14 18:17:24 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* adjust it a little to make it less profitable to sell and buy */
|
|
|
|
cost = CalculateCompanyValue(p) >> 2;
|
|
|
|
cost = -(cost - (cost >> 7));
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 00:11:37 +00:00
|
|
|
byte *b = p->share_owners;
|
2004-08-09 17:04:08 +00:00
|
|
|
while (*b != _current_player) b++; /* share owners is guaranteed to contain player */
|
|
|
|
*b = 0xFF;
|
|
|
|
InvalidateWindow(WC_COMPANY, (int)p1);
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Buy up another company.
|
|
|
|
* When a competing company is gone bankrupt you get the chance to purchase
|
|
|
|
* that company.
|
|
|
|
* @todo currently this only works for AI players
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 player/company to buy up
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdBuyCompany(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Player *p;
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
/* Disable takeovers in multiplayer games */
|
|
|
|
if (p1 >= MAX_PLAYERS || _networking) return CMD_ERROR;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_OTHER);
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(p1);
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
if (!p->is_ai) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2004-08-09 17:04:08 +00:00
|
|
|
DoAcquireCompany(p);
|
|
|
|
}
|
|
|
|
return p->bankrupt_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prices
|
2005-01-22 20:23:18 +00:00
|
|
|
static void SaveLoad_PRIC(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
SlArray(&_price, NUM_PRICES, SLE_INT32);
|
|
|
|
SlArray(&_price_frac, NUM_PRICES, SLE_UINT16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cargo payment rates
|
2005-01-22 20:23:18 +00:00
|
|
|
static void SaveLoad_CAPR(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
SlArray(&_cargo_payment_rates, NUM_CARGO, SLE_INT32);
|
|
|
|
SlArray(&_cargo_payment_rates_frac, NUM_CARGO, SLE_UINT16);
|
|
|
|
}
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _economy_desc[] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_VAR(Economy,max_loan, SLE_INT32),
|
|
|
|
SLE_VAR(Economy,max_loan_unround, SLE_INT32),
|
|
|
|
SLE_VAR(Economy,fluct, SLE_FILE_I16 | SLE_VAR_I32),
|
|
|
|
SLE_VAR(Economy,interest_rate, SLE_UINT8),
|
|
|
|
SLE_VAR(Economy,infl_amount, SLE_UINT8),
|
|
|
|
SLE_VAR(Economy,infl_amount_pr, SLE_UINT8),
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
// Economy variables
|
2005-01-22 20:23:18 +00:00
|
|
|
static void SaveLoad_ECMY(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-30 22:16:05 +00:00
|
|
|
SlObject(&_economy, _economy_desc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ChunkHandler _economy_chunk_handlers[] = {
|
|
|
|
{ 'PRIC', SaveLoad_PRIC, SaveLoad_PRIC, CH_RIFF | CH_AUTO_LENGTH},
|
|
|
|
{ 'CAPR', SaveLoad_CAPR, SaveLoad_CAPR, CH_RIFF | CH_AUTO_LENGTH},
|
|
|
|
{ 'SUBS', Save_SUBS, Load_SUBS, CH_ARRAY},
|
|
|
|
{ 'ECMY', SaveLoad_ECMY, SaveLoad_ECMY, CH_RIFF | CH_LAST},
|
|
|
|
};
|