2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/** @file players.cpp
|
2006-09-04 20:40:33 +00:00
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2008-03-31 00:17:39 +00:00
|
|
|
#include "engine_func.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_func.h"
|
|
|
|
#include "player_gui.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "town.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "saveload.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network/network.h"
|
2008-01-13 21:51:53 +00:00
|
|
|
#include "network/network_internal.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2008-04-17 21:21:01 +00:00
|
|
|
#include "cheat_func.h"
|
2005-09-07 15:10:11 +00:00
|
|
|
#include "ai/ai.h"
|
2007-03-02 01:17:11 +00:00
|
|
|
#include "player_face.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-12-19 19:44:29 +00:00
|
|
|
#include "window_func.h"
|
2007-12-19 23:26:02 +00:00
|
|
|
#include "tile_map.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "gfx_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_func.h"
|
|
|
|
#include "autoreplace_gui.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-01-09 16:40:02 +00:00
|
|
|
#include "ai/default/default.h"
|
|
|
|
#include "ai/trolly/trolly.h"
|
2008-01-09 21:05:03 +00:00
|
|
|
#include "road_func.h"
|
|
|
|
#include "rail.h"
|
2008-03-31 00:06:17 +00:00
|
|
|
#include "sprite.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/sprites.h"
|
|
|
|
|
2008-01-12 14:10:35 +00:00
|
|
|
Player _players[MAX_PLAYERS];
|
|
|
|
PlayerByte _local_player;
|
|
|
|
PlayerByte _current_player;
|
|
|
|
/* NOSAVE: can be determined from player structs */
|
|
|
|
byte _player_colors[MAX_PLAYERS];
|
|
|
|
PlayerFace _player_face; ///< for player face storage in openttd.cfg
|
|
|
|
HighScore _highscore_table[5][5]; // 4 difficulty-settings (+ network); top 5
|
|
|
|
|
2006-12-26 12:56:48 +00:00
|
|
|
/**
|
|
|
|
* Sets the local player and updates the patch settings that are set on a
|
|
|
|
* per-company (player) basis to reflect the core's state in the GUI.
|
|
|
|
* @param new_player the new player
|
|
|
|
* @pre IsValidPlayer(new_player) || new_player == PLAYER_SPECTATOR || new_player == OWNER_NONE
|
|
|
|
*/
|
|
|
|
void SetLocalPlayer(PlayerID new_player)
|
|
|
|
{
|
|
|
|
/* Player could also be PLAYER_SPECTATOR or OWNER_NONE */
|
|
|
|
assert(IsValidPlayer(new_player) || new_player == PLAYER_SPECTATOR || new_player == OWNER_NONE);
|
|
|
|
|
|
|
|
_local_player = new_player;
|
|
|
|
|
|
|
|
/* Do not update the patches if we are in the intro GUI */
|
|
|
|
if (IsValidPlayer(new_player) && _game_mode != GM_MENU) {
|
|
|
|
const Player *p = GetPlayer(new_player);
|
|
|
|
_patches.autorenew = p->engine_renew;
|
|
|
|
_patches.autorenew_months = p->engine_renew_months;
|
|
|
|
_patches.autorenew_money = p->engine_renew_money;
|
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-12 14:10:35 +00:00
|
|
|
bool IsHumanPlayer(PlayerID pi)
|
|
|
|
{
|
|
|
|
return !GetPlayer(pi)->is_ai;
|
|
|
|
}
|
|
|
|
|
2006-08-28 06:21:48 +00:00
|
|
|
|
|
|
|
uint16 GetDrawStringPlayerColor(PlayerID player)
|
|
|
|
{
|
2006-10-24 22:23:08 +00:00
|
|
|
/* Get the color for DrawString-subroutines which matches the color
|
|
|
|
* of the player */
|
|
|
|
if (!IsValidPlayer(player)) return _colour_gradient[COLOUR_WHITE][4] | IS_PALETTE_COLOR;
|
2006-08-29 19:26:13 +00:00
|
|
|
return (_colour_gradient[_player_colors[player]][4]) | IS_PALETTE_COLOR;
|
2006-08-28 06:21:48 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 11:49:18 +00:00
|
|
|
void DrawPlayerIcon(PlayerID p, int x, int y)
|
|
|
|
{
|
|
|
|
DrawSprite(SPR_PLAYER_ICON, PLAYER_SPRITE_COLOR(p), x, y);
|
|
|
|
}
|
|
|
|
|
2007-02-24 14:53:32 +00:00
|
|
|
/**
|
2007-03-02 01:17:11 +00:00
|
|
|
* Converts an old player face format to the new player face format
|
2007-02-24 14:53:32 +00:00
|
|
|
*
|
2007-03-02 01:17:11 +00:00
|
|
|
* Meaning of the bits in the old face (some bits are used in several times):
|
2007-02-24 14:53:32 +00:00
|
|
|
* - 4 and 5: chin
|
2007-02-25 09:06:13 +00:00
|
|
|
* - 6 to 9: eyebrows
|
2007-02-24 14:53:32 +00:00
|
|
|
* - 10 to 13: nose
|
|
|
|
* - 13 to 15: lips (also moustache for males)
|
|
|
|
* - 16 to 19: hair
|
2007-02-25 09:06:13 +00:00
|
|
|
* - 20 to 22: eye color
|
2007-02-24 14:53:32 +00:00
|
|
|
* - 20 to 27: tie, ear rings etc.
|
|
|
|
* - 28 to 30: glasses
|
|
|
|
* - 19, 26 and 27: race (bit 27 set and bit 19 equal to bit 26 = black, otherwise white)
|
|
|
|
* - 31: gender (0 = male, 1 = female)
|
|
|
|
*
|
2007-03-02 01:17:11 +00:00
|
|
|
* @param face the face in the old format
|
|
|
|
* @return the face in the new format
|
2007-02-24 14:53:32 +00:00
|
|
|
*/
|
2007-03-02 01:17:11 +00:00
|
|
|
PlayerFace ConvertFromOldPlayerFace(uint32 face)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-02 01:17:11 +00:00
|
|
|
PlayerFace pf = 0;
|
|
|
|
GenderEthnicity ge = GE_WM;
|
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
|
|
|
|
if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
|
2007-03-02 01:17:11 +00:00
|
|
|
|
|
|
|
SetPlayerFaceBits(pf, PFV_GEN_ETHN, ge, ge);
|
|
|
|
SetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
|
2007-11-19 21:02:30 +00:00
|
|
|
SetPlayerFaceBits(pf, PFV_EYE_COLOUR, ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
|
2007-03-02 01:17:11 +00:00
|
|
|
SetPlayerFaceBits(pf, PFV_CHIN, ge, ScalePlayerFaceValue(PFV_CHIN, ge, GB(face, 4, 2)));
|
|
|
|
SetPlayerFaceBits(pf, PFV_EYEBROWS, ge, ScalePlayerFaceValue(PFV_EYEBROWS, ge, GB(face, 6, 4)));
|
|
|
|
SetPlayerFaceBits(pf, PFV_HAIR, ge, ScalePlayerFaceValue(PFV_HAIR, ge, GB(face, 16, 4)));
|
|
|
|
SetPlayerFaceBits(pf, PFV_JACKET, ge, ScalePlayerFaceValue(PFV_JACKET, ge, GB(face, 20, 2)));
|
|
|
|
SetPlayerFaceBits(pf, PFV_COLLAR, ge, ScalePlayerFaceValue(PFV_COLLAR, ge, GB(face, 22, 2)));
|
|
|
|
SetPlayerFaceBits(pf, PFV_GLASSES, ge, GB(face, 28, 1));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-24 14:53:32 +00:00
|
|
|
uint lips = GB(face, 10, 4);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
|
2007-03-02 01:17:11 +00:00
|
|
|
SetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE, ge, true);
|
|
|
|
SetPlayerFaceBits(pf, PFV_MOUSTACHE, ge, max(lips, 1U) - 1);
|
|
|
|
} else {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(ge, GENDER_FEMALE)) {
|
2007-03-20 16:43:20 +00:00
|
|
|
lips = lips * 15 / 16;
|
2007-03-02 01:17:11 +00:00
|
|
|
lips -= 3;
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
|
2007-03-02 01:17:11 +00:00
|
|
|
} else {
|
|
|
|
lips = ScalePlayerFaceValue(PFV_LIPS, ge, lips);
|
2007-02-24 14:53:32 +00:00
|
|
|
}
|
2007-03-02 01:17:11 +00:00
|
|
|
SetPlayerFaceBits(pf, PFV_LIPS, ge, lips);
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
uint nose = GB(face, 13, 3);
|
|
|
|
if (ge == GE_WF) {
|
|
|
|
nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
|
|
|
|
} else {
|
|
|
|
nose = ScalePlayerFaceValue(PFV_NOSE, ge, nose);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-03-02 01:17:11 +00:00
|
|
|
SetPlayerFaceBits(pf, PFV_NOSE, ge, nose);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
uint tie_earring = GB(face, 24, 4);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
|
|
|
|
if (HasBit(ge, GENDER_FEMALE)) SetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge, true);
|
|
|
|
SetPlayerFaceBits(pf, PFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScalePlayerFaceValue(PFV_TIE_EARRING, ge, tie_earring / 2));
|
2007-02-24 14:53:32 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
return pf;
|
|
|
|
}
|
2007-02-24 14:53:32 +00:00
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a player's face is a valid encoding.
|
|
|
|
* Unused bits are not enforced to be 0.
|
|
|
|
* @param pf the fact to check
|
|
|
|
* @return true if and only if the face is valid
|
|
|
|
*/
|
|
|
|
bool IsValidPlayerFace(PlayerFace pf)
|
|
|
|
{
|
|
|
|
if (!ArePlayerFaceBitsValid(pf, PFV_GEN_ETHN, GE_WM)) return false;
|
|
|
|
|
|
|
|
GenderEthnicity ge = (GenderEthnicity)GetPlayerFaceBits(pf, PFV_GEN_ETHN, GE_WM);
|
2007-11-19 21:02:30 +00:00
|
|
|
bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetPlayerFaceBits(pf, PFV_HAS_MOUSTACHE, ge) != 0;
|
|
|
|
bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetPlayerFaceBits(pf, PFV_HAS_TIE_EARRING, ge) != 0;
|
2007-03-02 01:17:11 +00:00
|
|
|
bool has_glasses = GetPlayerFaceBits(pf, PFV_HAS_GLASSES, ge) != 0;
|
|
|
|
|
|
|
|
if (!ArePlayerFaceBitsValid(pf, PFV_EYE_COLOUR, ge)) return false;
|
|
|
|
for (PlayerFaceVariable pfv = PFV_CHEEKS; pfv < PFV_END; pfv++) {
|
|
|
|
switch (pfv) {
|
|
|
|
case PFV_MOUSTACHE: if (!has_moustache) continue; break;
|
|
|
|
case PFV_LIPS: /* FALL THROUGH */
|
|
|
|
case PFV_NOSE: if (has_moustache) continue; break;
|
|
|
|
case PFV_TIE_EARRING: if (!has_tie_earring) continue; break;
|
|
|
|
case PFV_GLASSES: if (!has_glasses) continue; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (!ArePlayerFaceBitsValid(pf, pfv, ge)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-02-24 14:53:32 +00:00
|
|
|
|
2007-03-02 01:17:11 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-09-30 20:37:25 +00:00
|
|
|
void InvalidatePlayerWindows(const Player *p)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-21 06:31:02 +00:00
|
|
|
PlayerID pid = p->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-21 06:31:02 +00:00
|
|
|
if (pid == _local_player) InvalidateWindow(WC_STATUS_BAR, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_FINANCES, pid);
|
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
bool CheckPlayerHasMoney(CommandCost cost)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 19:53:50 +00:00
|
|
|
if (cost.GetCost() > 0) {
|
2005-10-07 07:35:15 +00:00
|
|
|
PlayerID pid = _current_player;
|
2007-06-18 19:53:50 +00:00
|
|
|
if (IsValidPlayer(pid) && cost.GetCost() > GetPlayer(pid)->player_money) {
|
2007-06-21 17:25:17 +00:00
|
|
|
SetDParam(0, cost.GetCost());
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_0003_NOT_ENOUGH_CASH_REQUIRES;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
static void SubtractMoneyFromAnyPlayer(Player *p, CommandCost cost)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
if (cost.GetCost() == 0) return;
|
|
|
|
assert(cost.GetExpensesType() != INVALID_EXPENSES);
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
p->player_money -= cost.GetCost();
|
|
|
|
p->yearly_expenses[0][cost.GetExpensesType()] += cost.GetCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(1 << EXPENSES_TRAIN_INC |
|
2007-06-10 09:37:09 +00:00
|
|
|
1 << EXPENSES_ROADVEH_INC |
|
|
|
|
1 << EXPENSES_AIRCRAFT_INC |
|
2008-01-09 16:55:48 +00:00
|
|
|
1 << EXPENSES_SHIP_INC, cost.GetExpensesType())) {
|
2008-01-10 15:51:34 +00:00
|
|
|
p->cur_economy.income -= cost.GetCost();
|
2007-11-19 21:02:30 +00:00
|
|
|
} else if (HasBit(1 << EXPENSES_TRAIN_RUN |
|
2007-06-10 09:37:09 +00:00
|
|
|
1 << EXPENSES_ROADVEH_RUN |
|
|
|
|
1 << EXPENSES_AIRCRAFT_RUN |
|
|
|
|
1 << EXPENSES_SHIP_RUN |
|
|
|
|
1 << EXPENSES_PROPERTY |
|
2008-01-09 16:55:48 +00:00
|
|
|
1 << EXPENSES_LOAN_INT, cost.GetExpensesType())) {
|
2008-01-10 15:51:34 +00:00
|
|
|
p->cur_economy.expenses -= cost.GetCost();
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidatePlayerWindows(p);
|
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
void SubtractMoneyFromPlayer(CommandCost cost)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-12 23:46:01 +00:00
|
|
|
PlayerID pid = _current_player;
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2006-10-14 22:31:18 +00:00
|
|
|
if (IsValidPlayer(pid)) SubtractMoneyFromAnyPlayer(GetPlayer(pid), cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
void SubtractMoneyFromPlayerFract(PlayerID player, CommandCost cst)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-06-21 16:28:17 +00:00
|
|
|
Player *p = GetPlayer(player);
|
2004-08-09 17:04:08 +00:00
|
|
|
byte m = p->player_money_fraction;
|
2007-06-18 21:44:47 +00:00
|
|
|
Money cost = cst.GetCost();
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
p->player_money_fraction = m - (byte)cost;
|
|
|
|
cost >>= 8;
|
2006-06-27 21:25:53 +00:00
|
|
|
if (p->player_money_fraction > m) cost++;
|
2008-01-09 16:55:48 +00:00
|
|
|
if (cost != 0) SubtractMoneyFromAnyPlayer(p, CommandCost(cst.GetExpensesType(), cost));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-14 22:22:48 +00:00
|
|
|
void GetNameOfOwner(Owner owner, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(2, owner);
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (owner != OWNER_TOWN) {
|
2006-10-14 22:31:18 +00:00
|
|
|
if (!IsValidPlayer(owner)) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, STR_0150_SOMEONE);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2006-06-10 08:37:41 +00:00
|
|
|
const Player* p = GetPlayer(owner);
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, STR_COMPANY_NAME);
|
|
|
|
SetDParam(1, p->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2006-06-27 21:25:53 +00:00
|
|
|
const Town* t = ClosestTownFromTile(tile, (uint)-1);
|
|
|
|
|
2005-07-15 18:30:13 +00:00
|
|
|
SetDParam(0, STR_TOWN);
|
|
|
|
SetDParam(1, t->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
bool CheckOwnership(PlayerID owner)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-14 22:22:48 +00:00
|
|
|
assert(owner < OWNER_END);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (owner == _current_player) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_013B_OWNED_BY;
|
|
|
|
GetNameOfOwner(owner, 0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
bool CheckTileOwnership(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-14 22:22:48 +00:00
|
|
|
Owner owner = GetTileOwner(tile);
|
2005-06-04 11:56:32 +00:00
|
|
|
|
2006-10-14 22:22:48 +00:00
|
|
|
assert(owner < OWNER_END);
|
2005-06-04 11:56:32 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (owner == _current_player) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_013B_OWNED_BY;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* no need to get the name of the owner unless we're the local player (saves some time) */
|
2005-09-14 18:03:38 +00:00
|
|
|
if (IsLocalPlayer()) GetNameOfOwner(owner, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GenerateCompanyName(Player *p)
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
Town *t;
|
|
|
|
StringID str;
|
|
|
|
Player *pp;
|
|
|
|
uint32 strp;
|
|
|
|
char buffer[100];
|
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (p->name_1 != STR_SV_UNNAMED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
tile = p->last_build_coordinate;
|
2006-06-27 21:25:53 +00:00
|
|
|
if (tile == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
t = ClosestTownFromTile(tile, (uint)-1);
|
|
|
|
|
2008-04-18 04:37:06 +00:00
|
|
|
if (IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
|
|
|
|
strp = t->townnameparts;
|
|
|
|
|
|
|
|
verify_name:;
|
2007-03-21 17:42:43 +00:00
|
|
|
/* No player must have this name already */
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_PLAYERS(pp) {
|
2006-06-27 21:25:53 +00:00
|
|
|
if (pp->name_1 == str && pp->name_2 == strp) goto bad_town_name;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(buffer, str, lastof(buffer));
|
2006-09-16 13:20:14 +00:00
|
|
|
if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 150)
|
2004-08-09 17:04:08 +00:00
|
|
|
goto bad_town_name;
|
|
|
|
|
|
|
|
set_name:;
|
|
|
|
p->name_1 = str;
|
|
|
|
p->name_2 = strp;
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
2006-10-14 15:15:56 +00:00
|
|
|
if (!IsHumanPlayer(p->index)) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, t->index);
|
2008-03-28 08:53:36 +00:00
|
|
|
AddNewsItem((StringID)(p->index | NB_BNEWCOMPANY), NM_CALLBACK, NF_TILE, NT_COMPANY_INFO, DNC_BANKRUPCY, p->last_build_coordinate, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bad_town_name:;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (p->president_name_1 == SPECSTR_PRESIDENT_NAME) {
|
|
|
|
str = SPECSTR_ANDCO_NAME;
|
|
|
|
strp = p->president_name_2;
|
2004-09-11 09:55:19 +00:00
|
|
|
goto set_name;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
str = SPECSTR_ANDCO_NAME;
|
|
|
|
strp = Random();
|
|
|
|
goto verify_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-18 04:37:06 +00:00
|
|
|
#define COLOR_SWAP(i, j) do { byte t = colors[i];colors[i] = colors[j];colors[j] = t; } while(0)
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static const byte _color_sort[16] = {2, 2, 3, 2, 3, 2, 3, 2, 3, 2, 2, 2, 3, 1, 1, 1};
|
|
|
|
static const byte _color_similar_1[16] = {8, 6, 255, 12, 255, 0, 1, 1, 0, 13, 11, 10, 3, 9, 15, 14};
|
|
|
|
static const byte _color_similar_2[16] = {5, 7, 255, 255, 255, 8, 7, 6, 5, 12, 255, 255, 9, 255, 255, 255};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static byte GeneratePlayerColor()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte colors[16], pcolor, t2;
|
2008-04-18 04:37:06 +00:00
|
|
|
int i, j, n;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 r;
|
|
|
|
Player *p;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Initialize array */
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != 16; i++) colors[i] = i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* And randomize it */
|
2004-08-09 17:04:08 +00:00
|
|
|
n = 100;
|
|
|
|
do {
|
|
|
|
r = Random();
|
2005-07-20 15:29:28 +00:00
|
|
|
COLOR_SWAP(GB(r, 0, 4), GB(r, 4, 4));
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (--n);
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Bubble sort it according to the values in table 1 */
|
2004-08-09 17:04:08 +00:00
|
|
|
i = 16;
|
|
|
|
do {
|
2006-02-01 07:36:15 +00:00
|
|
|
for (j = 0; j != 15; j++) {
|
|
|
|
if (_color_sort[colors[j]] < _color_sort[colors[j + 1]]) {
|
|
|
|
COLOR_SWAP(j, j + 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (--i);
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Move the colors that look similar to each player's color to the side */
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_PLAYERS(p) if (p->is_active) {
|
|
|
|
pcolor = p->player_color;
|
2008-04-18 04:37:06 +00:00
|
|
|
for (i = 0; i != 16; i++) if (colors[i] == pcolor) {
|
2004-08-09 17:04:08 +00:00
|
|
|
colors[i] = 0xFF;
|
|
|
|
|
2004-09-11 09:55:19 +00:00
|
|
|
t2 = _color_similar_1[pcolor];
|
2004-08-09 17:04:08 +00:00
|
|
|
if (t2 == 0xFF) break;
|
2008-04-18 04:37:06 +00:00
|
|
|
for (i = 0; i != 15; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (colors[i] == t2) {
|
2008-04-18 04:37:06 +00:00
|
|
|
do COLOR_SWAP(i, i + 1); while (++i != 15);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-11 09:55:19 +00:00
|
|
|
t2 = _color_similar_2[pcolor];
|
2004-08-09 17:04:08 +00:00
|
|
|
if (t2 == 0xFF) break;
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 0; i != 15; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (colors[i] == t2) {
|
2006-02-01 07:36:15 +00:00
|
|
|
do COLOR_SWAP(i, i + 1); while (++i != 15);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Return the first available color */
|
2006-06-27 21:25:53 +00:00
|
|
|
for (i = 0;; i++) {
|
|
|
|
if (colors[i] != 0xFF) return colors[i];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GeneratePresidentName(Player *p)
|
|
|
|
{
|
|
|
|
Player *pp;
|
|
|
|
char buffer[100], buffer2[40];
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2004-08-09 17:04:08 +00:00
|
|
|
restart:;
|
|
|
|
|
|
|
|
p->president_name_2 = Random();
|
|
|
|
p->president_name_1 = SPECSTR_PRESIDENT_NAME;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, p->index);
|
|
|
|
GetString(buffer, STR_PLAYER_NAME, lastof(buffer));
|
2006-09-16 13:20:14 +00:00
|
|
|
if (strlen(buffer) >= 32 || GetStringBoundingBox(buffer).width >= 94)
|
2004-08-09 17:04:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
FOR_ALL_PLAYERS(pp) {
|
|
|
|
if (pp->is_active && p != pp) {
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, pp->index);
|
|
|
|
GetString(buffer2, STR_PLAYER_NAME, lastof(buffer2));
|
2005-02-05 22:50:33 +00:00
|
|
|
if (strcmp(buffer2, buffer) == 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static Player *AllocatePlayer()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Find a free slot */
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (!p->is_active) {
|
2007-01-10 18:56:51 +00:00
|
|
|
PlayerID i = p->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
memset(p, 0, sizeof(Player));
|
2008-01-09 16:40:02 +00:00
|
|
|
memset(&_players_ai[i], 0, sizeof(PlayerAI));
|
|
|
|
memset(&_players_ainew[i], 0, sizeof(PlayerAiNew));
|
2004-08-09 17:04:08 +00:00
|
|
|
p->index = i;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-09-15 12:27:00 +00:00
|
|
|
void ResetPlayerLivery(Player *p)
|
|
|
|
{
|
2007-01-10 18:56:51 +00:00
|
|
|
for (LiveryScheme scheme = LS_BEGIN; scheme < LS_END; scheme++) {
|
2006-09-15 12:27:00 +00:00
|
|
|
p->livery[scheme].in_use = false;
|
|
|
|
p->livery[scheme].colour1 = p->player_color;
|
|
|
|
p->livery[scheme].colour2 = p->player_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-15 19:59:27 +00:00
|
|
|
/**
|
|
|
|
* Create a new player and sets all player variables default values
|
|
|
|
*
|
|
|
|
* @param is_ai is a ai player?
|
|
|
|
* @return the player struct
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
Player *DoStartupNewPlayer(bool is_ai)
|
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
|
|
|
|
p = AllocatePlayer();
|
2006-06-10 08:37:41 +00:00
|
|
|
if (p == NULL) return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Make a color */
|
2004-08-09 17:04:08 +00:00
|
|
|
p->player_color = GeneratePlayerColor();
|
2006-09-15 12:27:00 +00:00
|
|
|
ResetPlayerLivery(p);
|
2005-09-07 15:10:11 +00:00
|
|
|
_player_colors[p->index] = p->player_color;
|
2004-08-09 17:04:08 +00:00
|
|
|
p->name_1 = STR_SV_UNNAMED;
|
|
|
|
p->is_active = true;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-06-18 21:00:14 +00:00
|
|
|
p->player_money = p->current_loan = 100000;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
p->is_ai = is_ai;
|
2008-01-09 16:40:02 +00:00
|
|
|
_players_ai[p->index].state = 5; // AIS_WANT_NEW_ROUTE
|
2006-10-14 15:49:43 +00:00
|
|
|
p->share_owners[0] = p->share_owners[1] = p->share_owners[2] = p->share_owners[3] = PLAYER_SPECTATOR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
p->avail_railtypes = GetPlayerRailtypes(p->index);
|
2007-05-25 08:13:01 +00:00
|
|
|
p->avail_roadtypes = GetPlayerRoadtypes(p->index);
|
2006-08-20 18:40:57 +00:00
|
|
|
p->inaugurated_year = _cur_year;
|
2007-10-15 19:59:27 +00:00
|
|
|
RandomPlayerFaceBits(p->face, (GenderEthnicity)Random(), false); // create a random player face
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
/* Engine renewal settings */
|
2006-01-12 15:52:18 +00:00
|
|
|
p->engine_renew_list = NULL;
|
2005-11-07 23:20:47 +00:00
|
|
|
p->renew_keep_length = false;
|
2007-11-11 21:05:50 +00:00
|
|
|
p->engine_renew = _patches_newgame.autorenew;
|
|
|
|
p->engine_renew_months = _patches_newgame.autorenew_months;
|
|
|
|
p->engine_renew_money = _patches_newgame.autorenew_money;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
GeneratePresidentName(p);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_GRAPH_LEGEND, 0);
|
|
|
|
InvalidateWindow(WC_TOOLBAR_MENU, 0);
|
2004-12-04 17:54:56 +00:00
|
|
|
InvalidateWindow(WC_CLIENT_LIST, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-07 15:10:11 +00:00
|
|
|
if (is_ai && (!_networking || _network_server) && _ai.enabled)
|
|
|
|
AI_StartNewAI(p->index);
|
|
|
|
|
2006-09-08 19:38:12 +00:00
|
|
|
memset(p->num_engines, 0, sizeof(p->num_engines));
|
2006-09-08 10:47:39 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void StartupPlayers()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-21 17:42:43 +00:00
|
|
|
/* The AI starts like in the setting with +2 month max */
|
2004-09-06 18:22:25 +00:00
|
|
|
_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + RandomRange(60 * DAY_TICKS) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void MaybeStartNewPlayer()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint n;
|
|
|
|
Player *p;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* count number of competitors */
|
2004-08-09 17:04:08 +00:00
|
|
|
n = 0;
|
2005-09-07 15:10:11 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
2006-06-10 08:37:41 +00:00
|
|
|
if (p->is_active && p->is_ai) n++;
|
2005-09-07 15:10:11 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* when there's a lot of computers in game, the probability that a new one starts is lower */
|
2006-06-10 08:37:41 +00:00
|
|
|
if (n < (uint)_opt.diff.max_no_competitors &&
|
|
|
|
n < (_network_server ?
|
|
|
|
InteractiveRandomRange(_opt.diff.max_no_competitors + 2) :
|
|
|
|
RandomRange(_opt.diff.max_no_competitors + 2)
|
|
|
|
)) {
|
|
|
|
/* Send a command to all clients to start up a new AI.
|
|
|
|
* Works fine for Multiplayer and Singleplayer */
|
|
|
|
DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
|
|
|
|
}
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* The next AI starts like the difficulty setting said, with +2 month max */
|
2005-07-17 13:34:19 +00:00
|
|
|
_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
|
2005-11-21 14:28:31 +00:00
|
|
|
_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializePlayers()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
memset(_players, 0, sizeof(_players));
|
2007-12-28 18:25:30 +00:00
|
|
|
for (PlayerID i = PLAYER_FIRST; i != MAX_PLAYERS; i++) {
|
|
|
|
_players[i].index = i;
|
|
|
|
for (uint j = 0; j < 4; j++) _players[i].share_owners[j] = PLAYER_SPECTATOR;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_player_tick_index = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void OnTick_Players()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
if (_game_mode == GM_EDITOR) return;
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
p = GetPlayer((PlayerID)_cur_player_tick_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
|
|
|
|
if (p->name_1 != 0) GenerateCompanyName(p);
|
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
if (AI_AllowNewAI() && _game_mode != GM_MENU && !--_next_competitor_start)
|
2004-09-11 09:55:19 +00:00
|
|
|
MaybeStartNewPlayer();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern void ShowPlayerFinances(PlayerID player);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void PlayersYearlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Copy statistics */
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (p->is_active) {
|
|
|
|
memmove(&p->yearly_expenses[1], &p->yearly_expenses[0], sizeof(p->yearly_expenses) - sizeof(p->yearly_expenses[0]));
|
|
|
|
memset(&p->yearly_expenses[0], 0, sizeof(p->yearly_expenses[0]));
|
|
|
|
InvalidateWindow(WC_FINANCES, p->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-14 15:49:43 +00:00
|
|
|
if (_patches.show_finances && _local_player != PLAYER_SPECTATOR) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowPlayerFinances(_local_player);
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(_local_player);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (p->num_valid_stat_ent > 5 && p->old_economy[0].performance_history < p->old_economy[4].performance_history) {
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayFx(SND_01_BAD_YEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayFx(SND_00_GOOD_YEAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-20 17:43:13 +00:00
|
|
|
static void DeletePlayerStuff(PlayerID pi)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
|
|
|
|
DeletePlayerWindows(pi);
|
2005-06-21 16:28:17 +00:00
|
|
|
p = GetPlayer(pi);
|
2008-01-12 19:58:06 +00:00
|
|
|
p->name_1 = STR_NULL;
|
|
|
|
p->president_name_1 = STR_NULL;
|
|
|
|
free(p->name);
|
|
|
|
free(p->president_name);
|
|
|
|
p->name = NULL;
|
|
|
|
p->president_name = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
/** Change engine renewal parameters
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-08-06 16:07:22 +00:00
|
|
|
* @param p1 bits 0-3 command
|
|
|
|
* - p1 = 0 - change auto renew bool
|
|
|
|
* - p1 = 1 - change auto renew months
|
|
|
|
* - p1 = 2 - change auto renew money
|
|
|
|
* - p1 = 3 - change auto renew array
|
|
|
|
* - p1 = 4 - change bool, months & money all together
|
2005-11-07 23:20:47 +00:00
|
|
|
* - p1 = 5 - change renew_keep_length
|
2005-08-06 16:07:22 +00:00
|
|
|
* @param p2 value to set
|
|
|
|
* if p1 = 0, then:
|
|
|
|
* - p2 = enable engine renewal
|
|
|
|
* if p1 = 1, then:
|
|
|
|
* - p2 = months left before engine expires to replace it
|
|
|
|
* if p1 = 2, then
|
|
|
|
* - p2 = minimum amount of money available
|
|
|
|
* if p1 = 3, then:
|
2007-05-19 09:40:18 +00:00
|
|
|
* - p1 bits 8-15 = engine group
|
2005-08-06 16:07:22 +00:00
|
|
|
* - p2 bits 0-15 = old engine type
|
|
|
|
* - p2 bits 16-31 = new engine type
|
|
|
|
* if p1 = 4, then:
|
|
|
|
* - p1 bit 15 = enable engine renewal
|
|
|
|
* - p1 bits 16-31 = months left before engine expires to replace it
|
|
|
|
* - p2 bits 0-31 = minimum amount of money available
|
2005-11-07 23:20:47 +00:00
|
|
|
* if p1 = 5, then
|
|
|
|
* - p2 = enable renew_keep_length
|
2005-08-06 16:07:22 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSetAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2005-08-06 16:07:22 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
2006-10-14 22:31:18 +00:00
|
|
|
if (!IsValidPlayer(_current_player)) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
|
|
|
p = GetPlayer(_current_player);
|
|
|
|
switch (GB(p1, 0, 3)) {
|
|
|
|
case 0:
|
2007-11-19 21:02:30 +00:00
|
|
|
if (p->engine_renew == HasBit(p2, 0))
|
2005-08-06 16:07:22 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-11-19 21:02:30 +00:00
|
|
|
p->engine_renew = HasBit(p2, 0);
|
2005-09-14 18:03:38 +00:00
|
|
|
if (IsLocalPlayer()) {
|
2005-08-06 16:07:22 +00:00
|
|
|
_patches.autorenew = p->engine_renew;
|
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2008-04-20 21:36:41 +00:00
|
|
|
if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
if (p->engine_renew_months == (int16)p2)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
p->engine_renew_months = (int16)p2;
|
2005-09-14 18:03:38 +00:00
|
|
|
if (IsLocalPlayer()) {
|
2005-08-06 16:07:22 +00:00
|
|
|
_patches.autorenew_months = p->engine_renew_months;
|
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2008-04-20 21:36:41 +00:00
|
|
|
if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
if (p->engine_renew_money == (uint32)p2)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
p->engine_renew_money = (uint32)p2;
|
2005-09-14 18:03:38 +00:00
|
|
|
if (IsLocalPlayer()) {
|
2005-08-06 16:07:22 +00:00
|
|
|
_patches.autorenew_money = p->engine_renew_money;
|
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: {
|
|
|
|
EngineID old_engine_type = GB(p2, 0, 16);
|
|
|
|
EngineID new_engine_type = GB(p2, 16, 16);
|
2007-05-19 12:46:48 +00:00
|
|
|
GroupID id_g = GB(p1, 16, 16);
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2007-07-14 23:10:27 +00:00
|
|
|
if (!IsValidGroupID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
if (new_engine_type != INVALID_ENGINE) {
|
|
|
|
/* First we make sure that it's a valid type the user requested
|
|
|
|
* check that it's an engine that is in the engine array */
|
2006-02-01 07:36:15 +00:00
|
|
|
if (!IsEngineIndex(new_engine_type))
|
2005-08-06 16:07:22 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* check that the new vehicle type is the same as the original one */
|
2005-08-06 16:07:22 +00:00
|
|
|
if (GetEngine(old_engine_type)->type != GetEngine(new_engine_type)->type)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* make sure that we do not replace a plane with a helicopter or vise versa */
|
2007-03-08 16:27:54 +00:00
|
|
|
if (GetEngine(new_engine_type)->type == VEH_AIRCRAFT &&
|
2006-06-14 11:05:30 +00:00
|
|
|
(AircraftVehInfo(old_engine_type)->subtype & AIR_CTOL) != (AircraftVehInfo(new_engine_type)->subtype & AIR_CTOL))
|
2005-08-06 16:07:22 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* make sure that the player can actually buy the new engine */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(GetEngine(new_engine_type)->player_avail, _current_player))
|
2005-08-06 16:07:22 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
cost = AddEngineReplacementForPlayer(p, old_engine_type, new_engine_type, id_g, flags);
|
2005-11-26 16:41:14 +00:00
|
|
|
} else {
|
2008-04-18 04:37:06 +00:00
|
|
|
cost = RemoveEngineReplacementForPlayer(p, old_engine_type, id_g, flags);
|
2005-08-06 16:07:22 +00:00
|
|
|
}
|
2006-01-02 19:55:09 +00:00
|
|
|
|
2007-06-27 20:40:20 +00:00
|
|
|
if (IsLocalPlayer()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
|
2006-01-02 19:55:09 +00:00
|
|
|
|
|
|
|
return cost;
|
2005-12-14 06:49:54 +00:00
|
|
|
}
|
|
|
|
|
2005-08-06 16:07:22 +00:00
|
|
|
case 4:
|
2008-04-20 21:36:41 +00:00
|
|
|
if (Clamp((int16)GB(p1, 16, 16), -12, 12) != (int16)GB(p1, 16, 16)) return CMD_ERROR;
|
|
|
|
if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-11-19 21:02:30 +00:00
|
|
|
p->engine_renew = HasBit(p1, 15);
|
2005-08-06 16:07:22 +00:00
|
|
|
p->engine_renew_months = (int16)GB(p1, 16, 16);
|
|
|
|
p->engine_renew_money = (uint32)p2;
|
|
|
|
|
2005-09-14 18:03:38 +00:00
|
|
|
if (IsLocalPlayer()) {
|
2005-08-06 16:07:22 +00:00
|
|
|
_patches.autorenew = p->engine_renew;
|
|
|
|
_patches.autorenew_months = p->engine_renew_months;
|
|
|
|
_patches.autorenew_money = p->engine_renew_money;
|
|
|
|
InvalidateWindow(WC_GAME_OPTIONS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-11-07 23:20:47 +00:00
|
|
|
case 5:
|
2007-11-19 21:02:30 +00:00
|
|
|
if (p->renew_keep_length == HasBit(p2, 0))
|
2005-11-07 23:20:47 +00:00
|
|
|
return CMD_ERROR;
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2005-11-07 23:20:47 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-11-19 21:02:30 +00:00
|
|
|
p->renew_keep_length = HasBit(p2, 0);
|
2005-11-07 23:20:47 +00:00
|
|
|
if (IsLocalPlayer()) {
|
2007-03-08 16:27:54 +00:00
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
|
2005-11-07 23:20:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2005-08-06 16:07:22 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
/** Control the players: add, delete, etc.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 21:09:38 +00:00
|
|
|
* @param flags operation to perform
|
2005-05-12 23:46:01 +00:00
|
|
|
* @param p1 various functionality
|
|
|
|
* - p1 = 0 - create a new player, Which player (network) it will be is in p2
|
|
|
|
* - p1 = 1 - create a new AI player
|
|
|
|
* - p1 = 2 - delete a player. Player is identified by p2
|
|
|
|
* - p1 = 3 - merge two companies together. Player to merge #1 with player #2. Identified by p2
|
|
|
|
* @param p2 various functionality, dictated by p1
|
|
|
|
* - p1 = 0 - ClientID of the newly created player
|
|
|
|
* - p1 = 2 - PlayerID of the that is getting deleted
|
|
|
|
* - p1 = 3 - #1 p2 = (bit 0-15) - player to merge (p2 & 0xFFFF)
|
|
|
|
* - #2 p2 = (bit 16-31) - player to be merged into ((p2>>16)&0xFFFF)
|
|
|
|
* @todo In the case of p1=0, create new player, the clientID of the new player is in parameter
|
|
|
|
* p2. This parameter is passed in at function DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
|
|
|
|
* on the server itself. First of all this is unbelievably ugly; second of all, well,
|
|
|
|
* it IS ugly! <b>Someone fix this up :)</b> So where to fix?@n
|
|
|
|
* @arg - network_server.c:838 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)@n
|
|
|
|
* @arg - network_client.c:536 DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP) from where the map has been received
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdPlayerCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-12 23:46:01 +00:00
|
|
|
if (flags & DC_EXEC) _current_player = OWNER_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
switch (p1) {
|
|
|
|
case 0: { /* Create a new player */
|
2006-11-10 17:52:51 +00:00
|
|
|
/* Joining Client:
|
|
|
|
* _local_player: PLAYER_SPECTATOR
|
|
|
|
* _network_playas/cid = requested company/player
|
|
|
|
*
|
|
|
|
* Other client(s)/server:
|
|
|
|
* _local_player/_network_playas: what they play as
|
|
|
|
* cid = requested company/player of joining client */
|
2005-05-12 23:46:01 +00:00
|
|
|
Player *p;
|
2007-01-16 15:20:22 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2006-10-15 23:35:26 +00:00
|
|
|
uint16 cid = p2; // ClientID
|
2007-01-16 15:20:22 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2006-11-10 17:52:51 +00:00
|
|
|
/* This command is only executed in a multiplayer game */
|
|
|
|
if (!_networking) return CMD_ERROR;
|
|
|
|
|
2006-12-26 11:47:31 +00:00
|
|
|
/* Has the network client a correct ClientID? */
|
2007-06-18 19:53:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CommandCost();
|
2006-12-31 11:12:49 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2007-06-18 19:53:50 +00:00
|
|
|
if (cid >= MAX_CLIENT_INFO) return CommandCost();
|
2006-12-31 11:12:49 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2006-11-10 17:52:51 +00:00
|
|
|
/* Delete multiplayer progress bar */
|
|
|
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
p = DoStartupNewPlayer(false);
|
2004-12-15 21:13:52 +00:00
|
|
|
|
2006-11-10 17:52:51 +00:00
|
|
|
/* A new player could not be created, revert to being a spectator */
|
|
|
|
if (p == NULL) {
|
2004-12-04 17:54:56 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
if (_network_server) {
|
2006-10-15 23:35:26 +00:00
|
|
|
NetworkClientInfo *ci = &_network_client_info[cid];
|
2006-11-10 17:52:51 +00:00
|
|
|
ci->client_playas = PLAYER_SPECTATOR;
|
2004-12-04 17:54:56 +00:00
|
|
|
NetworkUpdateClientInfo(ci->client_index);
|
2007-06-22 22:25:21 +00:00
|
|
|
} else if (_local_player == PLAYER_SPECTATOR) {
|
2006-12-26 12:56:48 +00:00
|
|
|
_network_playas = PLAYER_SPECTATOR;
|
2004-12-04 17:54:56 +00:00
|
|
|
}
|
2007-06-22 22:14:42 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2006-11-10 17:52:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the joining client who wants a new company */
|
2007-03-30 22:17:47 +00:00
|
|
|
if (_local_player != _network_playas && _network_playas == p->index) {
|
|
|
|
assert(_local_player == PLAYER_SPECTATOR);
|
2006-12-26 12:56:48 +00:00
|
|
|
SetLocalPlayer(p->index);
|
2008-01-11 00:30:32 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2007-12-02 14:48:26 +00:00
|
|
|
if (!StrEmpty(_network_default_company_pass)) {
|
|
|
|
char *password = _network_default_company_pass;
|
|
|
|
NetworkChangeCompanyPassword(1, &password);
|
|
|
|
}
|
2008-01-11 00:30:32 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2006-11-10 17:52:51 +00:00
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2006-11-11 23:08:07 +00:00
|
|
|
/* Now that we have a new player, broadcast its autorenew settings to
|
|
|
|
* all clients so everything is in sync */
|
|
|
|
DoCommand(0,
|
|
|
|
(_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4,
|
|
|
|
_patches.autorenew_money,
|
|
|
|
DC_EXEC,
|
|
|
|
CMD_SET_AUTOREPLACE
|
|
|
|
);
|
|
|
|
|
2006-11-10 17:52:51 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
if (_network_server) {
|
2006-06-27 21:25:53 +00:00
|
|
|
/* XXX - UGLY! p2 (pid) is mis-used to fetch the client-id, done at
|
|
|
|
* server-side in network_server.c:838, function
|
|
|
|
* DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND) */
|
2006-10-15 23:35:26 +00:00
|
|
|
NetworkClientInfo *ci = &_network_client_info[cid];
|
2006-11-10 17:52:51 +00:00
|
|
|
ci->client_playas = p->index;
|
2005-05-12 23:46:01 +00:00
|
|
|
NetworkUpdateClientInfo(ci->client_index);
|
2006-11-10 17:52:51 +00:00
|
|
|
|
|
|
|
if (IsValidPlayer(ci->client_playas)) {
|
|
|
|
PlayerID player_backup = _local_player;
|
|
|
|
_network_player_info[p->index].months_empty = 0;
|
|
|
|
|
|
|
|
/* XXX - When a client joins, we automatically set its name to the
|
|
|
|
* player's name (for some reason). As it stands now only the server
|
|
|
|
* knows the client's name, so it needs to send out a "broadcast" to
|
|
|
|
* do this. To achieve this we send a network command. However, it
|
|
|
|
* uses _local_player to execute the command as. To prevent abuse
|
|
|
|
* (eg. only yourself can change your name/company), we 'cheat' by
|
|
|
|
* impersonation _local_player as the server. Not the best solution;
|
|
|
|
* but it works.
|
|
|
|
* TODO: Perhaps this could be improved by when the client is ready
|
|
|
|
* with joining to let it send itself the command, and not the server?
|
|
|
|
* For example in network_client.c:534? */
|
|
|
|
_cmd_text = ci->client_name;
|
|
|
|
_local_player = ci->client_playas;
|
|
|
|
NetworkSend_Command(0, 0, 0, CMD_CHANGE_PRESIDENT_NAME, NULL);
|
|
|
|
_local_player = player_backup;
|
|
|
|
}
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2006-11-10 17:52:51 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2005-05-12 23:46:01 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case 1: /* Make a new AI player */
|
2007-06-18 19:53:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CommandCost();
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
DoStartupNewPlayer(true);
|
|
|
|
break;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
|
|
|
case 2: { /* Delete a player */
|
|
|
|
Player *p;
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
if (!IsValidPlayer((PlayerID)p2)) return CMD_ERROR;
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CommandCost();
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
p = GetPlayer((PlayerID)p2);
|
2004-12-16 11:36:57 +00:00
|
|
|
|
|
|
|
/* Only allow removal of HUMAN companies */
|
2006-10-14 15:15:56 +00:00
|
|
|
if (IsHumanPlayer(p->index)) {
|
2004-12-16 11:36:57 +00:00
|
|
|
/* Delete any open window of the company */
|
2005-05-12 23:46:01 +00:00
|
|
|
DeletePlayerWindows(p->index);
|
2004-12-16 11:36:57 +00:00
|
|
|
|
|
|
|
/* Show the bankrupt news */
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, p->index);
|
2008-03-28 08:53:36 +00:00
|
|
|
AddNewsItem((StringID)(p->index | NB_BBANKRUPT), NM_CALLBACK, NF_NONE, NT_COMPANY_INFO, DNC_BANKRUPCY, 0, 0);
|
2004-12-16 11:36:57 +00:00
|
|
|
|
|
|
|
/* Remove the company */
|
2006-10-14 15:49:43 +00:00
|
|
|
ChangeOwnershipOfPlayerItems(p->index, PLAYER_SPECTATOR);
|
2004-12-16 11:36:57 +00:00
|
|
|
p->is_active = false;
|
|
|
|
}
|
2006-01-12 15:52:18 +00:00
|
|
|
RemoveAllEngineReplacementForPlayer(p);
|
2008-01-09 21:27:39 +00:00
|
|
|
RemoveAllGroupsForPlayer(p->index);
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
} break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
case 3: { /* Merge a company (#1) into another company (#2), elimination company #1 */
|
2007-01-10 18:56:51 +00:00
|
|
|
PlayerID pid_old = (PlayerID)GB(p2, 0, 16);
|
|
|
|
PlayerID pid_new = (PlayerID)GB(p2, 16, 16);
|
2005-05-12 23:46:01 +00:00
|
|
|
|
2006-10-14 22:31:18 +00:00
|
|
|
if (!IsValidPlayer(pid_old) || !IsValidPlayer(pid_new)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
if (!(flags & DC_EXEC)) return CMD_ERROR;
|
|
|
|
|
|
|
|
ChangeOwnershipOfPlayerItems(pid_old, pid_new);
|
|
|
|
DeletePlayerStuff(pid_old);
|
|
|
|
} break;
|
2006-11-10 17:52:51 +00:00
|
|
|
|
2005-05-12 23:46:01 +00:00
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
static const StringID _endgame_perf_titles[] = {
|
2005-01-11 00:54:06 +00:00
|
|
|
STR_0213_BUSINESSMAN,
|
|
|
|
STR_0213_BUSINESSMAN,
|
|
|
|
STR_0213_BUSINESSMAN,
|
|
|
|
STR_0213_BUSINESSMAN,
|
|
|
|
STR_0213_BUSINESSMAN,
|
|
|
|
STR_0214_ENTREPRENEUR,
|
|
|
|
STR_0214_ENTREPRENEUR,
|
|
|
|
STR_0215_INDUSTRIALIST,
|
|
|
|
STR_0215_INDUSTRIALIST,
|
|
|
|
STR_0216_CAPITALIST,
|
|
|
|
STR_0216_CAPITALIST,
|
|
|
|
STR_0217_MAGNATE,
|
|
|
|
STR_0217_MAGNATE,
|
|
|
|
STR_0218_MOGUL,
|
|
|
|
STR_0218_MOGUL,
|
2006-06-27 21:25:53 +00:00
|
|
|
STR_0219_TYCOON_OF_THE_CENTURY
|
2005-01-11 00:54:06 +00:00
|
|
|
};
|
|
|
|
|
2005-01-15 00:46:45 +00:00
|
|
|
StringID EndGameGetPerformanceTitleFromValue(uint value)
|
2005-01-11 00:54:06 +00:00
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
value = minu(value / 64, lengthof(_endgame_perf_titles) - 1);
|
2005-11-07 13:30:43 +00:00
|
|
|
|
|
|
|
return _endgame_perf_titles[value];
|
2005-01-11 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Save the highscore for the player */
|
2005-01-13 16:28:47 +00:00
|
|
|
int8 SaveHighScoreValue(const Player *p)
|
2005-01-11 00:54:06 +00:00
|
|
|
{
|
|
|
|
HighScore *hs = _highscore_table[_opt.diff_level];
|
|
|
|
uint i;
|
|
|
|
uint16 score = p->old_economy[0].performance_history;
|
|
|
|
|
2005-01-15 00:46:45 +00:00
|
|
|
/* Exclude cheaters from the honour of being in the highscore table */
|
2006-06-10 08:37:41 +00:00
|
|
|
if (CheatHasBeenUsed()) return -1;
|
2005-01-15 00:46:45 +00:00
|
|
|
|
2005-01-11 00:54:06 +00:00
|
|
|
for (i = 0; i < lengthof(_highscore_table[0]); i++) {
|
|
|
|
/* You are in the TOP5. Move all values one down and save us there */
|
|
|
|
if (hs[i].score <= score) {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* move all elements one down starting from the replaced one */
|
2005-01-11 00:54:06 +00:00
|
|
|
memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, p->index);
|
|
|
|
SetDParam(1, p->index);
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(hs[i].company, STR_HIGHSCORE_NAME, lastof(hs[i].company)); // get manager/company name string
|
2005-01-11 00:54:06 +00:00
|
|
|
hs[i].score = score;
|
|
|
|
hs[i].title = EndGameGetPerformanceTitleFromValue(score);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1; // too bad; we did not make it into the top5
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Sort all players given their performance */
|
2005-01-13 16:28:47 +00:00
|
|
|
static int CDECL HighScoreSorter(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const Player *pa = *(const Player* const*)a;
|
|
|
|
const Player *pb = *(const Player* const*)b;
|
|
|
|
|
|
|
|
return pb->old_economy[0].performance_history - pa->old_economy[0].performance_history;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the highscores in a network game when it has ended */
|
|
|
|
#define LAST_HS_ITEM lengthof(_highscore_table) - 1
|
2007-03-07 11:47:46 +00:00
|
|
|
int8 SaveHighScoreValueNetwork()
|
2005-01-13 16:28:47 +00:00
|
|
|
{
|
2006-02-06 08:43:05 +00:00
|
|
|
const Player* p;
|
|
|
|
const Player* pl[MAX_PLAYERS];
|
2005-01-13 16:28:47 +00:00
|
|
|
size_t count = 0;
|
|
|
|
int8 player = -1;
|
|
|
|
|
|
|
|
/* Sort all active players with the highest score first */
|
2006-02-06 08:43:05 +00:00
|
|
|
FOR_ALL_PLAYERS(p) if (p->is_active) pl[count++] = p;
|
2006-02-06 23:28:07 +00:00
|
|
|
qsort((Player*)pl, count, sizeof(pl[0]), HighScoreSorter);
|
2005-01-13 16:28:47 +00:00
|
|
|
|
|
|
|
{
|
2006-02-06 08:43:05 +00:00
|
|
|
uint i;
|
2005-01-13 16:28:47 +00:00
|
|
|
|
|
|
|
memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
|
|
|
|
|
|
|
|
/* Copy over Top5 companies */
|
2005-10-03 21:20:01 +00:00
|
|
|
for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
|
2006-02-06 08:43:05 +00:00
|
|
|
HighScore* hs = &_highscore_table[LAST_HS_ITEM][i];
|
|
|
|
|
2007-06-25 13:30:38 +00:00
|
|
|
SetDParam(0, pl[i]->index);
|
|
|
|
SetDParam(1, pl[i]->index);
|
2006-10-21 23:31:34 +00:00
|
|
|
GetString(hs->company, STR_HIGHSCORE_NAME, lastof(hs->company)); // get manager/company name string
|
2006-02-06 08:43:05 +00:00
|
|
|
hs->score = pl[i]->old_economy[0].performance_history;
|
2005-01-13 16:28:47 +00:00
|
|
|
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* get the ranking of the local player */
|
2006-02-06 08:43:05 +00:00
|
|
|
if (pl[i]->index == _local_player) player = i;
|
2005-01-13 16:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add top5 players to highscore table */
|
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Save HighScore table to file */
|
2007-03-07 11:47:46 +00:00
|
|
|
void SaveToHighScore()
|
2005-01-11 00:54:06 +00:00
|
|
|
{
|
2005-11-07 13:30:43 +00:00
|
|
|
FILE *fp = fopen(_highscore_file, "wb");
|
2005-01-11 00:54:06 +00:00
|
|
|
|
|
|
|
if (fp != NULL) {
|
|
|
|
uint i;
|
|
|
|
HighScore *hs;
|
|
|
|
|
2005-01-13 16:28:47 +00:00
|
|
|
for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
|
2005-01-11 00:54:06 +00:00
|
|
|
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
|
|
|
|
/* First character is a command character, so strlen will fail on that */
|
2006-08-20 12:09:32 +00:00
|
|
|
byte length = min(sizeof(hs->company), (hs->company[0] == '\0') ? 0 : (int)strlen(&hs->company[1]) + 1);
|
2005-01-11 00:54:06 +00:00
|
|
|
|
|
|
|
fwrite(&length, sizeof(length), 1, fp); // write away string length
|
|
|
|
fwrite(hs->company, length, 1, fp);
|
|
|
|
fwrite(&hs->score, sizeof(hs->score), 1, fp);
|
2007-03-21 17:42:43 +00:00
|
|
|
fwrite("", 2, 1, fp); // XXX - placeholder for hs->title, not saved anymore; compatibility
|
2005-01-11 00:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/** Initialize the highscore table to 0 and if any file exists, load in values */
|
2007-03-07 11:47:46 +00:00
|
|
|
void LoadFromHighScore()
|
2005-01-11 00:54:06 +00:00
|
|
|
{
|
2005-11-07 13:30:43 +00:00
|
|
|
FILE *fp = fopen(_highscore_file, "rb");
|
2005-01-11 00:54:06 +00:00
|
|
|
|
|
|
|
memset(_highscore_table, 0, sizeof(_highscore_table));
|
|
|
|
|
|
|
|
if (fp != NULL) {
|
|
|
|
uint i;
|
|
|
|
HighScore *hs;
|
|
|
|
|
2005-01-13 16:28:47 +00:00
|
|
|
for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
|
2005-01-11 00:54:06 +00:00
|
|
|
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
|
|
|
|
byte length;
|
|
|
|
fread(&length, sizeof(length), 1, fp);
|
|
|
|
|
|
|
|
fread(hs->company, 1, length, fp);
|
|
|
|
fread(&hs->score, sizeof(hs->score), 1, fp);
|
2007-03-21 17:42:43 +00:00
|
|
|
fseek(fp, 2, SEEK_CUR); // XXX - placeholder for hs->title, not saved anymore; compatibility
|
2005-11-07 13:30:43 +00:00
|
|
|
hs->title = EndGameGetPerformanceTitleFromValue(hs->score);
|
2005-01-11 00:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-01-13 16:28:47 +00:00
|
|
|
/* Initialize end of game variable (when to show highscore chart) */
|
2006-08-15 14:52:17 +00:00
|
|
|
_patches.ending_year = 2051;
|
2005-01-11 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Save/load of players */
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _player_desc[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, name_2, SLE_UINT32),
|
|
|
|
SLE_VAR(Player, name_1, SLE_STRINGID),
|
2008-01-12 19:58:06 +00:00
|
|
|
SLE_CONDSTR(Player, name, SLE_STR, 0, 84, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-18 04:37:06 +00:00
|
|
|
SLE_VAR(Player, president_name_1, SLE_UINT16),
|
|
|
|
SLE_VAR(Player, president_name_2, SLE_UINT32),
|
2008-01-12 19:58:06 +00:00
|
|
|
SLE_CONDSTR(Player, president_name, SLE_STR, 0, 84, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, face, SLE_UINT32),
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* money was changed to a 64 bit field in savegame version 1. */
|
2007-06-18 22:49:55 +00:00
|
|
|
SLE_CONDVAR(Player, player_money, SLE_VAR_I64 | SLE_FILE_I32, 0, 0),
|
|
|
|
SLE_CONDVAR(Player, player_money, SLE_INT64, 1, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 22:49:55 +00:00
|
|
|
SLE_CONDVAR(Player, current_loan, SLE_VAR_I64 | SLE_FILE_I32, 0, 64),
|
|
|
|
SLE_CONDVAR(Player, current_loan, SLE_INT64, 65, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, player_color, SLE_UINT8),
|
|
|
|
SLE_VAR(Player, player_money_fraction, SLE_UINT8),
|
2007-05-01 16:35:14 +00:00
|
|
|
SLE_CONDVAR(Player, avail_railtypes, SLE_UINT8, 0, 57),
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, block_preview, SLE_UINT8),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, cargo_types, SLE_UINT16),
|
|
|
|
SLE_CONDVAR(Player, location_of_house, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Player, location_of_house, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Player, last_build_coordinate, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, inaugurated_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Player, inaugurated_year, SLE_INT32, 31, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_ARR(Player, share_owners, SLE_UINT8, 4),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, num_valid_stat_ent, SLE_UINT8),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Player, quarters_of_bankrupcy, SLE_UINT8),
|
|
|
|
SLE_VAR(Player, bankrupt_asked, SLE_UINT8),
|
|
|
|
SLE_VAR(Player, bankrupt_timeout, SLE_INT16),
|
2007-06-18 22:49:55 +00:00
|
|
|
SLE_CONDVAR(Player, bankrupt_value, SLE_VAR_I64 | SLE_FILE_I32, 0, 64),
|
|
|
|
SLE_CONDVAR(Player, bankrupt_value, SLE_INT64, 65, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* yearly expenses was changed to 64-bit in savegame version 2. */
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDARR(Player, yearly_expenses, SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
|
|
|
|
SLE_CONDARR(Player, yearly_expenses, SLE_INT64, 3 * 13, 2, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(Player, is_ai, SLE_BOOL, 2, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, is_active, SLE_BOOL, 4, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Engine renewal settings */
|
2006-03-16 00:20:33 +00:00
|
|
|
SLE_CONDNULL(512, 16, 18),
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDREF(Player, engine_renew_list, REF_ENGINE_RENEWS, 19, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, engine_renew, SLE_BOOL, 16, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, engine_renew_months, SLE_INT16, 16, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, engine_renew_money, SLE_UINT32, 16, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Player, renew_keep_length, SLE_BOOL, 2, SL_MAX_VERSION), // added with 16.1, but was blank since 2
|
2005-08-06 16:07:22 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* reserve extra space in savegame here. (currently 63 bytes) */
|
2006-03-16 00:20:33 +00:00
|
|
|
SLE_CONDNULL(63, 2, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _player_economy_desc[] = {
|
2007-03-21 17:42:43 +00:00
|
|
|
/* these were changed to 64-bit in savegame format 2 */
|
2007-06-18 22:49:55 +00:00
|
|
|
SLE_CONDVAR(PlayerEconomyEntry, income, SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
|
|
|
|
SLE_CONDVAR(PlayerEconomyEntry, income, SLE_INT64, 2, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(PlayerEconomyEntry, expenses, SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
|
|
|
|
SLE_CONDVAR(PlayerEconomyEntry, expenses, SLE_INT64, 2, SL_MAX_VERSION),
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(PlayerEconomyEntry, company_value, SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
|
|
|
|
SLE_CONDVAR(PlayerEconomyEntry, company_value, SLE_INT64, 2, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(PlayerEconomyEntry, delivered_cargo, SLE_INT32),
|
|
|
|
SLE_VAR(PlayerEconomyEntry, performance_history, SLE_INT32),
|
2004-09-11 09:55:19 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2006-09-15 12:27:00 +00:00
|
|
|
static const SaveLoad _player_livery_desc[] = {
|
|
|
|
SLE_CONDVAR(Livery, in_use, SLE_BOOL, 34, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
static void SaveLoad_PLYR(Player* p)
|
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
SlObject(p, _player_desc);
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Write AI? */
|
2006-10-14 15:15:56 +00:00
|
|
|
if (!IsHumanPlayer(p->index)) {
|
2008-01-09 16:40:02 +00:00
|
|
|
SaveLoad_AI(p->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Write economy */
|
2004-08-09 17:04:08 +00:00
|
|
|
SlObject(&p->cur_economy, _player_economy_desc);
|
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Write old economy entries. */
|
2006-02-06 08:43:05 +00:00
|
|
|
for (i = 0; i < p->num_valid_stat_ent; i++) {
|
|
|
|
SlObject(&p->old_economy[i], _player_economy_desc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-09-15 12:27:00 +00:00
|
|
|
|
2007-03-21 17:42:43 +00:00
|
|
|
/* Write each livery entry. */
|
2008-01-16 11:25:15 +00:00
|
|
|
int num_liveries = CheckSavegameVersion(63) ? LS_END - 4 : (CheckSavegameVersion(85) ? LS_END - 2: LS_END);
|
2007-05-27 09:33:41 +00:00
|
|
|
for (i = 0; i < num_liveries; i++) {
|
2006-09-15 12:27:00 +00:00
|
|
|
SlObject(&p->livery[i], _player_livery_desc);
|
|
|
|
}
|
2007-05-27 09:33:41 +00:00
|
|
|
|
2008-01-16 11:25:15 +00:00
|
|
|
if (num_liveries < LS_END) {
|
|
|
|
/* We want to insert some liveries somewhere in between. This means some have to be moved. */
|
|
|
|
memmove(&p->livery[LS_FREIGHT_WAGON], &p->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(p->livery[0]));
|
|
|
|
p->livery[LS_PASSENGER_WAGON_MONORAIL] = p->livery[LS_MONORAIL];
|
|
|
|
p->livery[LS_PASSENGER_WAGON_MAGLEV] = p->livery[LS_MAGLEV];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_liveries == LS_END - 4) {
|
2007-05-27 09:33:41 +00:00
|
|
|
/* Copy bus/truck liveries over to trams */
|
|
|
|
p->livery[LS_PASSENGER_TRAM] = p->livery[LS_BUS];
|
|
|
|
p->livery[LS_FREIGHT_TRAM] = p->livery[LS_TRUCK];
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Save_PLYR()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Player *p;
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
if (p->is_active) {
|
|
|
|
SlSetArrayIndex(p->index);
|
|
|
|
SlAutolength((AutolengthProc*)SaveLoad_PLYR, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Load_PLYR()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2007-01-10 18:56:51 +00:00
|
|
|
Player *p = GetPlayer((PlayerID)index);
|
2004-08-09 17:04:08 +00:00
|
|
|
SaveLoad_PLYR(p);
|
|
|
|
_player_colors[index] = p->player_color;
|
2005-09-07 15:10:11 +00:00
|
|
|
|
|
|
|
/* This is needed so an AI is attached to a loaded AI */
|
|
|
|
if (p->is_ai && (!_networking || _network_server) && _ai.enabled)
|
|
|
|
AI_StartNewAI(p->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const ChunkHandler _player_chunk_handlers[] = {
|
2004-08-09 17:04:08 +00:00
|
|
|
{ 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST},
|
|
|
|
};
|