|
@@ -283,101 +283,103 @@ int UpdateCompanyRatingAndValue(Company
|
|
|
void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
|
|
|
{
|
|
|
Town *t;
|
|
|
CompanyID old = _current_company;
|
|
|
|
|
|
assert(old_owner != new_owner);
|
|
|
|
|
|
{
|
|
|
Company *c;
|
|
|
uint i;
|
|
|
|
|
|
/* See if the old_owner had shares in other companies */
|
|
|
_current_company = old_owner;
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
if (c->share_owners[i] == old_owner) {
|
|
|
/* Sell his shares */
|
|
|
CommandCost res = DoCommand(0, c->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
|
|
|
/* Because we are in a DoCommand, we can't just execute an other one and
|
|
|
* expect the money to be removed. We need to do it ourself! */
|
|
|
SubtractMoneyFromCompany(res);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Sell all the shares that people have on this company */
|
|
|
c = Company::Get(old_owner);
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
_current_company = c->share_owners[i];
|
|
|
if (_current_company != INVALID_OWNER) {
|
|
|
/* Sell the shares */
|
|
|
CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
|
|
|
/* Because we are in a DoCommand, we can't just execute an other one and
|
|
|
* expect the money to be removed. We need to do it ourself! */
|
|
|
SubtractMoneyFromCompany(res);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
_current_company = old_owner;
|
|
|
|
|
|
/* Temporarily increase the company's money, to be sure that
|
|
|
* removing his/her property doesn't fail because of lack of money.
|
|
|
* Not too drastically though, because it could overflow */
|
|
|
if (new_owner == INVALID_OWNER) {
|
|
|
Company::Get(old_owner)->money = UINT64_MAX >> 2; // jackpot ;p
|
|
|
}
|
|
|
|
|
|
if (new_owner == INVALID_OWNER) {
|
|
|
Subsidy *s;
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
if (s->IsAwarded() && Station::Get(s->dst)->owner == old_owner) {
|
|
|
s->cargo_type = CT_INVALID;
|
|
|
Subsidy *s;
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
if (s->awarded == old_owner) {
|
|
|
if (new_owner == INVALID_OWNER) {
|
|
|
DeleteSubsidy(s);
|
|
|
} else {
|
|
|
s->awarded = new_owner;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Take care of rating in towns */
|
|
|
FOR_ALL_TOWNS(t) {
|
|
|
/* If a company takes over, give the ratings to that company. */
|
|
|
if (new_owner != INVALID_OWNER) {
|
|
|
if (HasBit(t->have_ratings, old_owner)) {
|
|
|
if (HasBit(t->have_ratings, new_owner)) {
|
|
|
/* use max of the two ratings. */
|
|
|
t->ratings[new_owner] = max(t->ratings[new_owner], t->ratings[old_owner]);
|
|
|
} else {
|
|
|
SetBit(t->have_ratings, new_owner);
|
|
|
t->ratings[new_owner] = t->ratings[old_owner];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Reset the ratings for the old owner */
|
|
|
t->ratings[old_owner] = RATING_INITIAL;
|
|
|
ClrBit(t->have_ratings, old_owner);
|
|
|
}
|
|
|
|
|
|
{
|
|
|
FreeUnitIDGenerator unitidgen[] = {
|
|
|
FreeUnitIDGenerator(VEH_TRAIN, new_owner), FreeUnitIDGenerator(VEH_ROAD, new_owner),
|
|
|
FreeUnitIDGenerator(VEH_SHIP, new_owner), FreeUnitIDGenerator(VEH_AIRCRAFT, new_owner)
|
|
|
};
|
|
|
|
|
|
Vehicle *v;
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
if (v->owner == old_owner && IsCompanyBuildableVehicleType(v->type)) {
|
|
|
if (new_owner == INVALID_OWNER) {
|
|
|
if (v->Previous() == NULL) delete v;
|
|
|
} else {
|
|
|
v->owner = new_owner;
|
|
|
v->colourmap = PAL_NONE;
|
|
|
if (v->IsEngineCountable()) Company::Get(new_owner)->num_engines[v->engine_type]++;
|
|
|
if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Change ownership of tiles */
|
|
|
{
|
|
|
TileIndex tile = 0;
|
|
@@ -871,232 +873,225 @@ Money GetTransportedGoodsIncome(uint num
|
|
|
/** The industries we've currently brought cargo to. */
|
|
|
static SmallIndustryList _cargo_delivery_destinations;
|
|
|
|
|
|
/**
|
|
|
* Transfer goods from station to industry.
|
|
|
* All cargo is delivered to the nearest (Manhattan) industry to the station sign, which is inside the acceptance rectangle and actually accepts the cargo.
|
|
|
* @param st The station that accepted the cargo
|
|
|
* @param cargo_type Type of cargo delivered
|
|
|
* @param nun_pieces Amount of cargo delivered
|
|
|
*/
|
|
|
static void DeliverGoodsToIndustry(const Station *st, CargoID cargo_type, int num_pieces)
|
|
|
{
|
|
|
/* Find the nearest industrytile to the station sign inside the catchment area, whose industry accepts the cargo.
|
|
|
* This fails in three cases:
|
|
|
* 1) The station accepts the cargo because there are enough houses around it accepting the cargo.
|
|
|
* 2) The industries in the catchment area temporarily reject the cargo, and the daily station loop has not yet updated station acceptance.
|
|
|
* 3) The results of callbacks CBID_INDUSTRY_REFUSE_CARGO and CBID_INDTILE_CARGO_ACCEPTANCE are inconsistent. (documented behaviour)
|
|
|
*/
|
|
|
|
|
|
for (uint i = 0; i < st->industries_near.Length(); i++) {
|
|
|
Industry *ind = st->industries_near[i];
|
|
|
const IndustrySpec *indspec = GetIndustrySpec(ind->type);
|
|
|
|
|
|
uint cargo_index;
|
|
|
for (cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
|
|
|
if (cargo_type == ind->accepts_cargo[cargo_index]) break;
|
|
|
}
|
|
|
/* Check if matching cargo has been found */
|
|
|
if (cargo_index >= lengthof(ind->accepts_cargo)) continue;
|
|
|
|
|
|
/* Check if industry temporarily refuses acceptance */
|
|
|
if (HasBit(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
|
|
|
uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO, 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile), ind, ind->type, ind->xy);
|
|
|
if (res == 0) continue;
|
|
|
}
|
|
|
|
|
|
/* Insert the industry into _cargo_delivery_destinations, if not yet contained */
|
|
|
_cargo_delivery_destinations.Include(ind);
|
|
|
|
|
|
ind->incoming_cargo_waiting[cargo_index] = min(num_pieces + ind->incoming_cargo_waiting[cargo_index], 0xFFFF);
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Delivers goods to industries/towns and calculates the payment
|
|
|
* @param num_pieces amount of cargo delivered
|
|
|
* @param source Originstation of the cargo
|
|
|
* @param dest Station the cargo has been unloaded
|
|
|
* @param source_tile The origin of the cargo for distance calculation
|
|
|
* @param days_in_transit Travel time
|
|
|
* @param company The company delivering the cargo
|
|
|
* The cargo is just added to the stockpile of the industry. It is due to the caller to trigger the industry's production machinery
|
|
|
* @param src_type Type of source of cargo (industry, town, headquarters)
|
|
|
* @param src Index of source of cargo
|
|
|
* @return Revenue for delivering cargo
|
|
|
* @note The cargo is just added to the stockpile of the industry. It is due to the caller to trigger the industry's production machinery
|
|
|
*/
|
|
|
static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID source, StationID dest, TileIndex source_tile, byte days_in_transit, Company *company)
|
|
|
static Money DeliverGoods(int num_pieces, CargoID cargo_type, StationID dest, TileIndex source_tile, byte days_in_transit, Company *company, SourceType src_type, SourceID src)
|
|
|
{
|
|
|
bool subsidised = false;
|
|
|
|
|
|
assert(num_pieces > 0);
|
|
|
|
|
|
/* Update company statistics */
|
|
|
company->cur_economy.delivered_cargo += num_pieces;
|
|
|
SetBit(company->cargo_types, cargo_type);
|
|
|
|
|
|
const Station *s_to = Station::Get(dest);
|
|
|
|
|
|
if (source != INVALID_STATION) {
|
|
|
const Station *s_from = Station::Get(source);
|
|
|
|
|
|
/* Check if a subsidy applies. */
|
|
|
subsidised = CheckSubsidised(s_from, s_to, cargo_type, company->index);
|
|
|
}
|
|
|
const Station *st = Station::Get(dest);
|
|
|
|
|
|
/* Increase town's counter for some special goods types */
|
|
|
const CargoSpec *cs = CargoSpec::Get(cargo_type);
|
|
|
if (cs->town_effect == TE_FOOD) s_to->town->new_act_food += num_pieces;
|
|
|
if (cs->town_effect == TE_WATER) s_to->town->new_act_water += num_pieces;
|
|
|
if (cs->town_effect == TE_FOOD) st->town->new_act_food += num_pieces;
|
|
|
if (cs->town_effect == TE_WATER) st->town->new_act_water += num_pieces;
|
|
|
|
|
|
/* Give the goods to the industry. */
|
|
|
DeliverGoodsToIndustry(s_to, cargo_type, num_pieces);
|
|
|
DeliverGoodsToIndustry(st, cargo_type, num_pieces);
|
|
|
|
|
|
/* Determine profit */
|
|
|
Money profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(source_tile, s_to->xy), days_in_transit, cargo_type);
|
|
|
Money profit = GetTransportedGoodsIncome(num_pieces, DistanceManhattan(source_tile, st->xy), days_in_transit, cargo_type);
|
|
|
|
|
|
/* Modify profit if a subsidy is in effect */
|
|
|
if (subsidised) {
|
|
|
if (CheckSubsidised(cargo_type, company->index, src_type, src, st)) {
|
|
|
switch (_settings_game.difficulty.subsidy_multiplier) {
|
|
|
case 0: profit += profit >> 1; break;
|
|
|
case 1: profit *= 2; break;
|
|
|
case 2: profit *= 3; break;
|
|
|
default: profit *= 4; break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return profit;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Inform the industry about just delivered cargo
|
|
|
* DeliverGoodsToIndustry() silently incremented incoming_cargo_waiting, now it is time to do something with the new cargo.
|
|
|
* @param i The industry to process
|
|
|
*/
|
|
|
static void TriggerIndustryProduction(Industry *i)
|
|
|
{
|
|
|
const IndustrySpec *indspec = GetIndustrySpec(i->type);
|
|
|
uint16 callback = indspec->callback_flags;
|
|
|
|
|
|
i->was_cargo_delivered = true;
|
|
|
i->last_cargo_accepted_at = _date;
|
|
|
|
|
|
if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
|
|
|
if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) {
|
|
|
IndustryProductionCallback(i, 0);
|
|
|
} else {
|
|
|
InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
|
|
|
}
|
|
|
} else {
|
|
|
for (uint cargo_index = 0; cargo_index < lengthof(i->incoming_cargo_waiting); cargo_index++) {
|
|
|
uint cargo_waiting = i->incoming_cargo_waiting[cargo_index];
|
|
|
if (cargo_waiting == 0) continue;
|
|
|
|
|
|
i->produced_cargo_waiting[0] = min(i->produced_cargo_waiting[0] + (cargo_waiting * indspec->input_cargo_multiplier[cargo_index][0] / 256), 0xFFFF);
|
|
|
i->produced_cargo_waiting[1] = min(i->produced_cargo_waiting[1] + (cargo_waiting * indspec->input_cargo_multiplier[cargo_index][1] / 256), 0xFFFF);
|
|
|
|
|
|
i->incoming_cargo_waiting[cargo_index] = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
TriggerIndustry(i, INDUSTRY_TRIGGER_RECEIVED_CARGO);
|
|
|
StartStopIndustryTileAnimation(i, IAT_INDUSTRY_RECEIVED_CARGO);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Makes us a new cargo payment helper.
|
|
|
* @param front The front of the train
|
|
|
* @param destinations List to add the destinations of 'our' cargo to
|
|
|
*/
|
|
|
CargoPayment::CargoPayment(Vehicle *front) :
|
|
|
front(front),
|
|
|
current_station(front->last_station_visited)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
CargoPayment::~CargoPayment()
|
|
|
{
|
|
|
if (this->CleaningPool()) return;
|
|
|
|
|
|
this->front->cargo_payment = NULL;
|
|
|
|
|
|
if (this->visual_profit == 0) return;
|
|
|
|
|
|
CompanyID old_company = _current_company;
|
|
|
_current_company = this->front->owner;
|
|
|
|
|
|
SubtractMoneyFromCompany(CommandCost(this->front->GetExpenseType(true), -this->route_profit));
|
|
|
this->front->profit_this_year += this->visual_profit << 8;
|
|
|
|
|
|
if (this->route_profit != 0) {
|
|
|
if (IsLocalCompany() && !PlayVehicleSound(this->front, VSE_LOAD_UNLOAD)) {
|
|
|
SndPlayVehicleFx(SND_14_CASHTILL, this->front);
|
|
|
}
|
|
|
|
|
|
ShowCostOrIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, -this->visual_profit);
|
|
|
} else {
|
|
|
ShowFeederIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, this->visual_profit);
|
|
|
}
|
|
|
|
|
|
_current_company = old_company;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Handle payment for final delivery of the given cargo packet.
|
|
|
* @param cp The cargo packet to pay for.
|
|
|
* @param count The number of packets to pay for.
|
|
|
*/
|
|
|
void CargoPayment::PayFinalDelivery(CargoPacket *cp, uint count)
|
|
|
{
|
|
|
if (this->owner == NULL) {
|
|
|
this->owner = Company::Get(this->front->owner);
|
|
|
}
|
|
|
|
|
|
/* Handle end of route payment */
|
|
|
Money profit = DeliverGoods(count, this->ct, cp->source, this->current_station, cp->source_xy, cp->days_in_transit, this->owner);
|
|
|
Money profit = DeliverGoods(count, this->ct, this->current_station, cp->source_xy, cp->days_in_transit, this->owner, cp->source_type, cp->source_id);
|
|
|
this->route_profit += profit;
|
|
|
|
|
|
/* The vehicle's profit is whatever route profit there is minus feeder shares. */
|
|
|
this->visual_profit += profit - cp->feeder_share;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Handle payment for transfer of the given cargo packet.
|
|
|
* @param cp The cargo packet to pay for.
|
|
|
* @param count The number of packets to pay for.
|
|
|
*/
|
|
|
void CargoPayment::PayTransfer(CargoPacket *cp, uint count)
|
|
|
{
|
|
|
Money profit = GetTransportedGoodsIncome(
|
|
|
count,
|
|
|
/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
|
|
|
DistanceManhattan(cp->loaded_at_xy, Station::Get(this->current_station)->xy),
|
|
|
cp->days_in_transit,
|
|
|
this->ct);
|
|
|
|
|
|
this->visual_profit += profit; // accumulate transfer profits for whole vehicle
|
|
|
cp->feeder_share += profit; // account for the (virtual) profit already made for the cargo packet
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Prepare the vehicle to be unloaded.
|
|
|
* @param front_v the vehicle to be unloaded
|
|
|
*/
|
|
|
void PrepareUnload(Vehicle *front_v)
|
|
|
{
|
|
|
/* At this moment loading cannot be finished */
|
|
|
ClrBit(front_v->vehicle_flags, VF_LOADING_FINISHED);
|
|
|
|
|
|
/* Start unloading in at the first possible moment */
|
|
|
front_v->load_unload_time_rem = 1;
|
|
|
|
|
|
if ((front_v->current_order.GetUnloadType() & OUFB_NO_UNLOAD) == 0) {
|
|
|
for (Vehicle *v = front_v; v != NULL; v = v->Next()) {
|
|
|
if (v->cargo_cap > 0 && !v->cargo.Empty()) {
|
|
|
SetBit(v->vehicle_flags, VF_CARGO_UNLOADING);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
assert(front_v->cargo_payment == NULL);
|
|
|
front_v->cargo_payment = new CargoPayment(front_v);
|
|
|
}
|
|
|
|