comparison src/economy.cpp @ 11922:0a4b63f3f3c3 draft

(svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
author smatz <smatz@openttd.org>
date Sat, 16 May 2009 23:34:14 +0000
parents a39da1e4c1f5
children fccc9cea27a7
comparison
equal deleted inserted replaced
11921:b53b2a07cc35 11922:0a4b63f3f3c3
292 } 292 }
293 } 293 }
294 } 294 }
295 295
296 /* Sell all the shares that people have on this company */ 296 /* Sell all the shares that people have on this company */
297 c = GetCompany(old_owner); 297 c = Company::Get(old_owner);
298 for (i = 0; i < 4; i++) { 298 for (i = 0; i < 4; i++) {
299 _current_company = c->share_owners[i]; 299 _current_company = c->share_owners[i];
300 if (_current_company != INVALID_OWNER) { 300 if (_current_company != INVALID_OWNER) {
301 /* Sell the shares */ 301 /* Sell the shares */
302 CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY); 302 CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
311 311
312 /* Temporarily increase the company's money, to be sure that 312 /* Temporarily increase the company's money, to be sure that
313 * removing his/her property doesn't fail because of lack of money. 313 * removing his/her property doesn't fail because of lack of money.
314 * Not too drastically though, because it could overflow */ 314 * Not too drastically though, because it could overflow */
315 if (new_owner == INVALID_OWNER) { 315 if (new_owner == INVALID_OWNER) {
316 GetCompany(old_owner)->money = UINT64_MAX >> 2; // jackpot ;p 316 Company::Get(old_owner)->money = UINT64_MAX >> 2; // jackpot ;p
317 } 317 }
318 318
319 if (new_owner == INVALID_OWNER) { 319 if (new_owner == INVALID_OWNER) {
320 Subsidy *s; 320 Subsidy *s;
321 321
322 for (s = _subsidies; s != endof(_subsidies); s++) { 322 for (s = _subsidies; s != endof(_subsidies); s++) {
323 if (s->cargo_type != CT_INVALID && s->age >= 12) { 323 if (s->cargo_type != CT_INVALID && s->age >= 12) {
324 if (GetStation(s->to)->owner == old_owner) s->cargo_type = CT_INVALID; 324 if (Station::Get(s->to)->owner == old_owner) s->cargo_type = CT_INVALID;
325 } 325 }
326 } 326 }
327 } 327 }
328 328
329 /* Take care of rating in towns */ 329 /* Take care of rating in towns */
358 if (new_owner == INVALID_OWNER) { 358 if (new_owner == INVALID_OWNER) {
359 if (v->Previous() == NULL) delete v; 359 if (v->Previous() == NULL) delete v;
360 } else { 360 } else {
361 v->owner = new_owner; 361 v->owner = new_owner;
362 v->colourmap = PAL_NONE; 362 v->colourmap = PAL_NONE;
363 if (IsEngineCountable(v)) GetCompany(new_owner)->num_engines[v->engine_type]++; 363 if (IsEngineCountable(v)) Company::Get(new_owner)->num_engines[v->engine_type]++;
364 if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID(); 364 if (v->IsPrimaryVehicle()) v->unitnumber = unitidgen[v->type].NextID();
365 } 365 }
366 } 366 }
367 } 367 }
368 } 368 }
416 } 416 }
417 } 417 }
418 418
419 /* In all cases clear replace engine rules. 419 /* In all cases clear replace engine rules.
420 * Even if it was copied, it could interfere with new owner's rules */ 420 * Even if it was copied, it could interfere with new owner's rules */
421 RemoveAllEngineReplacementForCompany(GetCompany(old_owner)); 421 RemoveAllEngineReplacementForCompany(Company::Get(old_owner));
422 422
423 if (new_owner == INVALID_OWNER) { 423 if (new_owner == INVALID_OWNER) {
424 RemoveAllGroupsForCompany(old_owner); 424 RemoveAllGroupsForCompany(old_owner);
425 } else { 425 } else {
426 Group *g; 426 Group *g;
870 870
871 if (s->age < 12) { 871 if (s->age < 12) {
872 if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) { 872 if (cs->town_effect != TE_PASSENGERS && cs->town_effect != TE_MAIL) {
873 SetDParam(1, STR_INDUSTRY); 873 SetDParam(1, STR_INDUSTRY);
874 SetDParam(2, s->from); 874 SetDParam(2, s->from);
875 tile = GetIndustry(s->from)->xy; 875 tile = Industry::Get(s->from)->xy;
876 876
877 if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) { 877 if (cs->town_effect != TE_GOODS && cs->town_effect != TE_FOOD) {
878 SetDParam(4, STR_INDUSTRY); 878 SetDParam(4, STR_INDUSTRY);
879 SetDParam(5, s->to); 879 SetDParam(5, s->to);
880 tile2 = GetIndustry(s->to)->xy; 880 tile2 = Industry::Get(s->to)->xy;
881 } else { 881 } else {
882 SetDParam(4, STR_TOWN); 882 SetDParam(4, STR_TOWN);
883 SetDParam(5, s->to); 883 SetDParam(5, s->to);
884 tile2 = GetTown(s->to)->xy; 884 tile2 = Town::Get(s->to)->xy;
885 } 885 }
886 } else { 886 } else {
887 SetDParam(1, STR_TOWN); 887 SetDParam(1, STR_TOWN);
888 SetDParam(2, s->from); 888 SetDParam(2, s->from);
889 tile = GetTown(s->from)->xy; 889 tile = Town::Get(s->from)->xy;
890 890
891 SetDParam(4, STR_TOWN); 891 SetDParam(4, STR_TOWN);
892 SetDParam(5, s->to); 892 SetDParam(5, s->to);
893 tile2 = GetTown(s->to)->xy; 893 tile2 = Town::Get(s->to)->xy;
894 } 894 }
895 } else { 895 } else {
896 SetDParam(1, s->from); 896 SetDParam(1, s->from);
897 tile = GetStation(s->from)->xy; 897 tile = Station::Get(s->from)->xy;
898 898
899 SetDParam(2, s->to); 899 SetDParam(2, s->to);
900 tile2 = GetStation(s->to)->xy; 900 tile2 = Station::Get(s->to)->xy;
901 } 901 }
902 902
903 tp.a = tile; 903 tp.a = tile;
904 tp.b = tile2; 904 tp.b = tile2;
905 905
1067 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, pair.a, pair.b); 1067 AddNewsItem(STR_NEWS_OFFER_OF_SUBSIDY_EXPIRED, NS_SUBSIDIES, pair.a, pair.b);
1068 s->cargo_type = CT_INVALID; 1068 s->cargo_type = CT_INVALID;
1069 modified = true; 1069 modified = true;
1070 AI::BroadcastNewEvent(new AIEventSubsidyOfferExpired(s - _subsidies)); 1070 AI::BroadcastNewEvent(new AIEventSubsidyOfferExpired(s - _subsidies));
1071 } else if (s->age == 2 * 12 - 1) { 1071 } else if (s->age == 2 * 12 - 1) {
1072 st = GetStation(s->to); 1072 st = Station::Get(s->to);
1073 if (st->owner == _local_company) { 1073 if (st->owner == _local_company) {
1074 pair = SetupSubsidyDecodeParam(s, 1); 1074 pair = SetupSubsidyDecodeParam(s, 1);
1075 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, pair.a, pair.b); 1075 AddNewsItem(STR_NEWS_SUBSIDY_WITHDRAWN_SERVICE, NS_SUBSIDIES, pair.a, pair.b);
1076 } 1076 }
1077 s->cargo_type = CT_INVALID; 1077 s->cargo_type = CT_INVALID;
1293 for (s = _subsidies; s != endof(_subsidies); s++) { 1293 for (s = _subsidies; s != endof(_subsidies); s++) {
1294 if (s->cargo_type == cargo_type && s->age < 12) { 1294 if (s->cargo_type == cargo_type && s->age < 12) {
1295 /* Check distance from source */ 1295 /* Check distance from source */
1296 const CargoSpec *cs = GetCargo(cargo_type); 1296 const CargoSpec *cs = GetCargo(cargo_type);
1297 if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) { 1297 if (cs->town_effect == TE_PASSENGERS || cs->town_effect == TE_MAIL) {
1298 xy = GetTown(s->from)->xy; 1298 xy = Town::Get(s->from)->xy;
1299 } else { 1299 } else {
1300 xy = GetIndustry(s->from)->xy; 1300 xy = Industry::Get(s->from)->xy;
1301 } 1301 }
1302 if (DistanceMax(xy, from->xy) > 9) continue; 1302 if (DistanceMax(xy, from->xy) > 9) continue;
1303 1303
1304 /* Check distance from dest */ 1304 /* Check distance from dest */
1305 switch (cs->town_effect) { 1305 switch (cs->town_effect) {
1306 case TE_PASSENGERS: 1306 case TE_PASSENGERS:
1307 case TE_MAIL: 1307 case TE_MAIL:
1308 case TE_GOODS: 1308 case TE_GOODS:
1309 case TE_FOOD: 1309 case TE_FOOD:
1310 xy = GetTown(s->to)->xy; 1310 xy = Town::Get(s->to)->xy;
1311 break; 1311 break;
1312 1312
1313 default: 1313 default:
1314 xy = GetIndustry(s->to)->xy; 1314 xy = Industry::Get(s->to)->xy;
1315 break; 1315 break;
1316 } 1316 }
1317 if (DistanceMax(xy, to->xy) > 9) continue; 1317 if (DistanceMax(xy, to->xy) > 9) continue;
1318 1318
1319 /* Found a subsidy, change the values to indicate that it's in use */ 1319 /* Found a subsidy, change the values to indicate that it's in use */
1358 1358
1359 assert(num_pieces > 0); 1359 assert(num_pieces > 0);
1360 1360
1361 /* Update company statistics */ 1361 /* Update company statistics */
1362 { 1362 {
1363 Company *c = GetCompany(_current_company); 1363 Company *c = Company::Get(_current_company);
1364 c->cur_economy.delivered_cargo += num_pieces; 1364 c->cur_economy.delivered_cargo += num_pieces;
1365 SetBit(c->cargo_types, cargo_type); 1365 SetBit(c->cargo_types, cargo_type);
1366 } 1366 }
1367 1367
1368 /* Get station pointers. */ 1368 /* Get station pointers. */
1369 s_from = GetStation(source); 1369 s_from = Station::Get(source);
1370 s_to = GetStation(dest); 1370 s_to = Station::Get(dest);
1371 1371
1372 /* Check if a subsidy applies. */ 1372 /* Check if a subsidy applies. */
1373 subsidised = CheckSubsidised(s_from, s_to, cargo_type); 1373 subsidised = CheckSubsidised(s_from, s_to, cargo_type);
1374 1374
1375 /* Increase town's counter for some special goods types */ 1375 /* Increase town's counter for some special goods types */
1442 Money vehicle_profit = 0; // Money paid to the train 1442 Money vehicle_profit = 0; // Money paid to the train
1443 Money route_profit = 0; // The grand total amount for the route. A-D of transfer chain A-B-C-D 1443 Money route_profit = 0; // The grand total amount for the route. A-D of transfer chain A-B-C-D
1444 Money virtual_profit = 0; // The virtual profit for entire vehicle chain 1444 Money virtual_profit = 0; // The virtual profit for entire vehicle chain
1445 1445
1446 StationID last_visited = front_v->last_station_visited; 1446 StationID last_visited = front_v->last_station_visited;
1447 Station *st = GetStation(last_visited); 1447 Station *st = Station::Get(last_visited);
1448 1448
1449 /* The owner of the train wants to be paid */ 1449 /* The owner of the train wants to be paid */
1450 CompanyID old_company = _current_company; 1450 CompanyID old_company = _current_company;
1451 _current_company = front_v->owner; 1451 _current_company = front_v->owner;
1452 1452
1494 } else if (front_v->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) { 1494 } else if (front_v->current_order.GetUnloadType() & (OUFB_UNLOAD | OUFB_TRANSFER)) {
1495 if (!cp->paid_for && (front_v->current_order.GetUnloadType() & OUFB_TRANSFER) != 0) { 1495 if (!cp->paid_for && (front_v->current_order.GetUnloadType() & OUFB_TRANSFER) != 0) {
1496 Money profit = GetTransportedGoodsIncome( 1496 Money profit = GetTransportedGoodsIncome(
1497 cp->count, 1497 cp->count,
1498 /* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */ 1498 /* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
1499 DistanceManhattan(cp->loaded_at_xy, GetStation(last_visited)->xy), 1499 DistanceManhattan(cp->loaded_at_xy, Station::Get(last_visited)->xy),
1500 cp->days_in_transit, 1500 cp->days_in_transit,
1501 v->cargo_type); 1501 v->cargo_type);
1502 1502
1503 front_v->profit_this_year += profit << 8; 1503 front_v->profit_this_year += profit << 8;
1504 virtual_profit += profit; // accumulate transfer profits for whole vehicle 1504 virtual_profit += profit; // accumulate transfer profits for whole vehicle
1560 } 1560 }
1561 return; 1561 return;
1562 } 1562 }
1563 1563
1564 StationID last_visited = v->last_station_visited; 1564 StationID last_visited = v->last_station_visited;
1565 Station *st = GetStation(last_visited); 1565 Station *st = Station::Get(last_visited);
1566 1566
1567 if (v->type == VEH_TRAIN && (!IsTileType(v->tile, MP_STATION) || GetStationIndex(v->tile) != st->index)) { 1567 if (v->type == VEH_TRAIN && (!IsTileType(v->tile, MP_STATION) || GetStationIndex(v->tile) != st->index)) {
1568 /* The train reversed in the station. Take the "easy" way 1568 /* The train reversed in the station. Take the "easy" way
1569 * out and let the train just leave as it always did. */ 1569 * out and let the train just leave as it always did. */
1570 SetBit(v->vehicle_flags, VF_LOADING_FINISHED); 1570 SetBit(v->vehicle_flags, VF_LOADING_FINISHED);
1835 int i; 1835 int i;
1836 Money value; 1836 Money value;
1837 CompanyID ci = c->index; 1837 CompanyID ci = c->index;
1838 1838
1839 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1); 1839 CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
1840 cni->FillData(c, GetCompany(_current_company)); 1840 cni->FillData(c, Company::Get(_current_company));
1841 1841
1842 SetDParam(0, STR_NEWS_COMPANY_MERGER_TITLE); 1842 SetDParam(0, STR_NEWS_COMPANY_MERGER_TITLE);
1843 SetDParam(1, c->bankrupt_value == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION); 1843 SetDParam(1, c->bankrupt_value == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION);
1844 SetDParamStr(2, cni->company_name); 1844 SetDParamStr(2, cni->company_name);
1845 SetDParamStr(3, cni->other_company_name); 1845 SetDParamStr(3, cni->other_company_name);
1850 /* original code does this a little bit differently */ 1850 /* original code does this a little bit differently */
1851 ChangeNetworkOwner(ci, _current_company); 1851 ChangeNetworkOwner(ci, _current_company);
1852 ChangeOwnershipOfCompanyItems(ci, _current_company); 1852 ChangeOwnershipOfCompanyItems(ci, _current_company);
1853 1853
1854 if (c->bankrupt_value == 0) { 1854 if (c->bankrupt_value == 0) {
1855 owner = GetCompany(_current_company); 1855 owner = Company::Get(_current_company);
1856 owner->current_loan += c->current_loan; 1856 owner->current_loan += c->current_loan;
1857 } 1857 }
1858 1858
1859 value = CalculateCompanyValue(c) >> 2; 1859 value = CalculateCompanyValue(c) >> 2;
1860 CompanyID old_company = _current_company; 1860 CompanyID old_company = _current_company;
1891 1891
1892 /* Check if buying shares is allowed (protection against modified clients) 1892 /* Check if buying shares is allowed (protection against modified clients)
1893 * Cannot buy own shares */ 1893 * Cannot buy own shares */
1894 if (!IsValidCompanyID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR; 1894 if (!IsValidCompanyID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR;
1895 1895
1896 Company *c = GetCompany((CompanyID)p1); 1896 Company *c = Company::Get((CompanyID)p1);
1897 1897
1898 /* Protect new companies from hostile takeovers */ 1898 /* Protect new companies from hostile takeovers */
1899 if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_PROTECTED); 1899 if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_PROTECTED);
1900 1900
1901 /* Those lines are here for network-protection (clients can be slow) */ 1901 /* Those lines are here for network-protection (clients can be slow) */
1934 { 1934 {
1935 /* Check if selling shares is allowed (protection against modified clients) 1935 /* Check if selling shares is allowed (protection against modified clients)
1936 * Cannot sell own shares */ 1936 * Cannot sell own shares */
1937 if (!IsValidCompanyID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR; 1937 if (!IsValidCompanyID((CompanyID)p1) || !_settings_game.economy.allow_shares || _current_company == (CompanyID)p1) return CMD_ERROR;
1938 1938
1939 Company *c = GetCompany((CompanyID)p1); 1939 Company *c = Company::Get((CompanyID)p1);
1940 1940
1941 /* Those lines are here for network-protection (clients can be slow) */ 1941 /* Those lines are here for network-protection (clients can be slow) */
1942 if (GetAmountOwnedBy(c, _current_company) == 0) return CommandCost(); 1942 if (GetAmountOwnedBy(c, _current_company) == 0) return CommandCost();
1943 1943
1944 /* adjust it a little to make it less profitable to sell and buy */ 1944 /* adjust it a little to make it less profitable to sell and buy */
1971 if (!IsValidCompanyID(cid) || _networking) return CMD_ERROR; 1971 if (!IsValidCompanyID(cid) || _networking) return CMD_ERROR;
1972 1972
1973 /* Do not allow companies to take over themselves */ 1973 /* Do not allow companies to take over themselves */
1974 if (cid == _current_company) return CMD_ERROR; 1974 if (cid == _current_company) return CMD_ERROR;
1975 1975
1976 Company *c = GetCompany(cid); 1976 Company *c = Company::Get(cid);
1977 1977
1978 if (!c->is_ai) return CMD_ERROR; 1978 if (!c->is_ai) return CMD_ERROR;
1979 1979
1980 if (flags & DC_EXEC) { 1980 if (flags & DC_EXEC) {
1981 DoAcquireCompany(c); 1981 DoAcquireCompany(c);