src/economy.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
    65 	{
    65 	{
    66 		Station *st;
    66 		Station *st;
    67 		uint num = 0;
    67 		uint num = 0;
    68 
    68 
    69 		FOR_ALL_STATIONS(st) {
    69 		FOR_ALL_STATIONS(st) {
    70 			if (st->owner == owner) {
    70 			if (st->owner == owner) num += CountBitsSet(st->facilities);
    71 				uint facil = st->facilities;
       
    72 				do num += (facil&1); while (facil >>= 1);
       
    73 			}
       
    74 		}
    71 		}
    75 
    72 
    76 		value = num * _eco->GetPrice(CEconomy::STATION_VALUE) * 25;
    73 		value = num * _eco->GetPrice(CEconomy::STATION_VALUE) * 25;
    77 	}
    74 	}
    78 
    75 
   142 	{
   139 	{
   143 		uint num = 0;
   140 		uint num = 0;
   144 		const Station* st;
   141 		const Station* st;
   145 
   142 
   146 		FOR_ALL_STATIONS(st) {
   143 		FOR_ALL_STATIONS(st) {
   147 			if (st->owner == owner) {
   144 			if (st->owner == owner) num += CountBitsSet(st->facilities);
   148 				int facil = st->facilities;
       
   149 				do num += facil&1; while (facil>>=1);
       
   150 			}
       
   151 		}
   145 		}
   152 		_score_part[owner][SCORE_STATIONS] = num;
   146 		_score_part[owner][SCORE_STATIONS] = num;
   153 	}
   147 	}
   154 
   148 
   155 /* Generate statistics depending on recent income statistics */
   149 /* Generate statistics depending on recent income statistics */
   194 		}
   188 		}
   195 	}
   189 	}
   196 
   190 
   197 /* Generate score for variety of cargo */
   191 /* Generate score for variety of cargo */
   198 	{
   192 	{
   199 		uint cargo = p->cargo_types;
   193 		uint num = CountBitsSet(p->cargo_types);
   200 		uint num = 0;
       
   201 		do num += cargo&1; while (cargo>>=1);
       
   202 		_score_part[owner][SCORE_CARGO] = num;
   194 		_score_part[owner][SCORE_CARGO] = num;
   203 		if (update) p->cargo_types = 0;
   195 		if (update) p->cargo_types = 0;
   204 	}
   196 	}
   205 
   197 
   206 /* Generate score for player money */
   198 /* Generate score for player money */
   265 		_current_player = old_player;
   257 		_current_player = old_player;
   266 		FOR_ALL_PLAYERS(p) {
   258 		FOR_ALL_PLAYERS(p) {
   267 			for (i = 0; i < 4; i++) {
   259 			for (i = 0; i < 4; i++) {
   268 				if (p->share_owners[i] == old_player) {
   260 				if (p->share_owners[i] == old_player) {
   269 					/* Sell his shares */
   261 					/* Sell his shares */
   270 					int32 res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
   262 					CommandCost res = DoCommand(0, p->index, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
   271 					/* Because we are in a DoCommand, we can't just execute an other one and
   263 					/* Because we are in a DoCommand, we can't just execute an other one and
   272 					 *  expect the money to be removed. We need to do it ourself! */
   264 					 *  expect the money to be removed. We need to do it ourself! */
   273 					SubtractMoneyFromPlayer(res);
   265 					SubtractMoneyFromPlayer(res);
   274 				}
   266 				}
   275 			}
   267 			}
   279 		p = GetPlayer(old_player);
   271 		p = GetPlayer(old_player);
   280 		for (i = 0; i < 4; i++) {
   272 		for (i = 0; i < 4; i++) {
   281 			_current_player = p->share_owners[i];
   273 			_current_player = p->share_owners[i];
   282 			if (_current_player != PLAYER_SPECTATOR) {
   274 			if (_current_player != PLAYER_SPECTATOR) {
   283 				/* Sell the shares */
   275 				/* Sell the shares */
   284 				int32 res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
   276 				CommandCost res = DoCommand(0, old_player, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
   285 				/* Because we are in a DoCommand, we can't just execute an other one and
   277 				/* Because we are in a DoCommand, we can't just execute an other one and
   286 				 *  expect the money to be removed. We need to do it ourself! */
   278 				 *  expect the money to be removed. We need to do it ourself! */
   287 				SubtractMoneyFromPlayer(res);
   279 				SubtractMoneyFromPlayer(res);
   288 			}
   280 			}
   289 		}
   281 		}
   353 			if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_TRAIN, VEH_AIRCRAFT + 1)) {
   345 			if (v->owner == old_player && IS_BYTE_INSIDE(v->type, VEH_TRAIN, VEH_AIRCRAFT + 1)) {
   354 				if (new_player == PLAYER_SPECTATOR) {
   346 				if (new_player == PLAYER_SPECTATOR) {
   355 					DeleteWindowById(WC_VEHICLE_VIEW, v->index);
   347 					DeleteWindowById(WC_VEHICLE_VIEW, v->index);
   356 					DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
   348 					DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
   357 					DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
   349 					DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
   358 					DeleteVehicle(v);
   350 
       
   351 					if (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && IsFreeWagon(v))) {
       
   352 						switch (v->type) {
       
   353 							default: NOT_REACHED();
       
   354 
       
   355 							case VEH_TRAIN: {
       
   356 								Vehicle *u = v;
       
   357 								do {
       
   358 									Vehicle *next = GetNextVehicle(u);
       
   359 									DeleteVehicle(u);
       
   360 									u = next;
       
   361 								} while (u != NULL);
       
   362 							} break;
       
   363 
       
   364 							case VEH_ROAD:
       
   365 							case VEH_SHIP:
       
   366 								DeleteVehicle(v);
       
   367 								break;
       
   368 
       
   369 							case VEH_AIRCRAFT:
       
   370 								DeleteVehicleChain(v);
       
   371 								break;
       
   372 						}
       
   373 					}
   359 				} else {
   374 				} else {
   360 					v->owner = new_player;
   375 					v->owner = new_player;
   361 					v->group_id = DEFAULT_GROUP;
   376 					v->group_id = DEFAULT_GROUP;
   362 					if (IsEngineCountable(v)) GetPlayer(new_player)->num_engines[v->engine_type]++;
   377 					if (IsEngineCountable(v)) GetPlayer(new_player)->num_engines[v->engine_type]++;
   363 					switch (v->type) {
   378 					switch (v->type) {
  1226 
  1241 
  1227 	for (Vehicle *v = front_v; v != NULL; v = v->next) {
  1242 	for (Vehicle *v = front_v; v != NULL; v = v->next) {
  1228 		/* No cargo to unload */
  1243 		/* No cargo to unload */
  1229 		if (v->cargo_cap == 0 || v->cargo_count == 0) continue;
  1244 		if (v->cargo_cap == 0 || v->cargo_count == 0) continue;
  1230 
  1245 
  1231 		SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
       
  1232 		/* All cargo has already been paid for, no need to pay again */
  1246 		/* All cargo has already been paid for, no need to pay again */
  1233 		if (v->cargo_count == v->cargo_paid_for) continue;
  1247 		if (v->cargo_count == v->cargo_paid_for) {
       
  1248 			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
       
  1249 			continue;
       
  1250 		}
  1234 
  1251 
  1235 		GoodsEntry *ge = &st->goods[v->cargo_type];
  1252 		GoodsEntry *ge = &st->goods[v->cargo_type];
  1236 
  1253 
  1237 		if (v->cargo_source != last_visited &&
  1254 		if (v->cargo_source != last_visited &&
  1238 				HASBIT(ge->waiting_acceptance, 15) &&
  1255 				HASBIT(ge->waiting_acceptance, 15) &&
  1247 			total_veh_profit         = profit - all_vehicles_cargo_feeder_share; // whole vehicle is not payed for transfers picked up earlier
  1264 			total_veh_profit         = profit - all_vehicles_cargo_feeder_share; // whole vehicle is not payed for transfers picked up earlier
  1248 			total_cargo_feeder_share = -all_vehicles_cargo_feeder_share;         // total of transfer fees in vehicle chain needs to be zero at end of unload
  1265 			total_cargo_feeder_share = -all_vehicles_cargo_feeder_share;         // total of transfer fees in vehicle chain needs to be zero at end of unload
  1249 
  1266 
  1250 			v->cargo_feeder_share = 0;   // clear transfer cost per vehicle
  1267 			v->cargo_feeder_share = 0;   // clear transfer cost per vehicle
  1251 			result |= 1;
  1268 			result |= 1;
       
  1269 
       
  1270 			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
  1252 		} else if (front_v->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
  1271 		} else if (front_v->current_order.flags & (OF_UNLOAD | OF_TRANSFER)) {
  1253 			if ((front_v->current_order.flags & OF_TRANSFER) != 0) {
  1272 			if ((front_v->current_order.flags & OF_TRANSFER) != 0) {
  1254 				virtual_profit = GetTransportedGoodsIncome(
  1273 				virtual_profit = GetTransportedGoodsIncome(
  1255 					v->cargo_count - v->cargo_paid_for,
  1274 					v->cargo_count - v->cargo_paid_for,
  1256 					/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
  1275 					/* pay transfer vehicle for only the part of transfer it has done: ie. cargo_loaded_at_xy to here */
  1268 
  1287 
  1269 				virtual_profit_total += virtual_profit;   // accumulate transfer profits for whole vehicle
  1288 				virtual_profit_total += virtual_profit;   // accumulate transfer profits for whole vehicle
  1270 				v->cargo_paid_for = v->cargo_count;       // record how much of the cargo has been paid for to eliminate double counting
  1289 				v->cargo_paid_for = v->cargo_count;       // record how much of the cargo has been paid for to eliminate double counting
  1271 			}
  1290 			}
  1272 			result |= 2;
  1291 			result |= 2;
       
  1292 
       
  1293 			SETBIT(v->vehicle_flags, VF_CARGO_UNLOADING);
  1273 		}
  1294 		}
  1274 	}
  1295 	}
  1275 
  1296 
  1276 	/* Ensure a negative total is only applied to the vehicle if there is value to reduce. */
  1297 	/* Ensure a negative total is only applied to the vehicle if there is value to reduce. */
  1277 	front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0);
  1298 	front_v->cargo_feeder_share = max(front_v->cargo_feeder_share + total_cargo_feeder_share, 0);
  1631  * @param tile unused
  1652  * @param tile unused
  1632  * @param flags type of operation
  1653  * @param flags type of operation
  1633  * @param p1 player to buy the shares from
  1654  * @param p1 player to buy the shares from
  1634  * @param p2 unused
  1655  * @param p2 unused
  1635  */
  1656  */
  1636 int32 CmdBuyShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1657 CommandCost CmdBuyShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1637 {
  1658 {
  1638 	Player *p;
  1659 	Player *p;
  1639 	int64 cost;
  1660 	CommandCost cost;
  1640 
  1661 
  1641 	/* Check if buying shares is allowed (protection against modified clients */
  1662 	/* Check if buying shares is allowed (protection against modified clients */
  1642 	if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR;
  1663 	if (!IsValidPlayer((PlayerID)p1) || !_patches.allow_shares) return CMD_ERROR;
  1643 
  1664 
  1644 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1665 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
  1677  * @param tile unused
  1698  * @param tile unused
  1678  * @param flags type of operation
  1699  * @param flags type of operation
  1679  * @param p1 player to sell the shares from
  1700  * @param p1 player to sell the shares from
  1680  * @param p2 unused
  1701  * @param p2 unused
  1681  */
  1702  */
  1682 int32 CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1703 CommandCost CmdSellShareInCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1683 {
  1704 {
  1684 	Player *p;
  1705 	Player *p;
  1685 	int64 cost;
  1706 	int64 cost;
  1686 
  1707 
  1687 	/* Check if buying shares is allowed (protection against modified clients */
  1708 	/* Check if buying shares is allowed (protection against modified clients */
  1713  * @param tile unused
  1734  * @param tile unused
  1714  * @param flags type of operation
  1735  * @param flags type of operation
  1715  * @param p1 player/company to buy up
  1736  * @param p1 player/company to buy up
  1716  * @param p2 unused
  1737  * @param p2 unused
  1717  */
  1738  */
  1718 int32 CmdBuyCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1739 CommandCost CmdBuyCompany(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1719 {
  1740 {
  1720 	Player *p;
  1741 	Player *p;
  1721 	PlayerID pid = (PlayerID)p1;
  1742 	PlayerID pid = (PlayerID)p1;
  1722 
  1743 
  1723 	/* Disable takeovers in multiplayer games */
  1744 	/* Disable takeovers in multiplayer games */