players.c
changeset 4000 bab1ebc37da0
parent 3873 5ddd36fbebff
child 4023 b65af90447d2
equal deleted inserted replaced
3999:ea92235dd6bf 4000:bab1ebc37da0
   264 
   264 
   265 	if (owner != OWNER_TOWN) {
   265 	if (owner != OWNER_TOWN) {
   266 		if (owner >= 8)
   266 		if (owner >= 8)
   267 			SetDParam(0, STR_0150_SOMEONE);
   267 			SetDParam(0, STR_0150_SOMEONE);
   268 		else {
   268 		else {
   269 			Player *p = GetPlayer(owner);
   269 			const Player* p = GetPlayer(owner);
   270 			SetDParam(0, p->name_1);
   270 			SetDParam(0, p->name_1);
   271 			SetDParam(1, p->name_2);
   271 			SetDParam(1, p->name_2);
   272 		}
   272 		}
   273 	} else {
   273 	} else {
   274 		Town *t = ClosestTownFromTile(tile, (uint)-1);
   274 		Town *t = ClosestTownFromTile(tile, (uint)-1);
   477 Player *DoStartupNewPlayer(bool is_ai)
   477 Player *DoStartupNewPlayer(bool is_ai)
   478 {
   478 {
   479 	Player *p;
   479 	Player *p;
   480 
   480 
   481 	p = AllocatePlayer();
   481 	p = AllocatePlayer();
   482 	if (p == NULL)
   482 	if (p == NULL) return NULL;
   483 		return NULL;
       
   484 
   483 
   485 	// Make a color
   484 	// Make a color
   486 	p->player_color = GeneratePlayerColor();
   485 	p->player_color = GeneratePlayerColor();
   487 	_player_colors[p->index] = p->player_color;
   486 	_player_colors[p->index] = p->player_color;
   488 	p->name_1 = STR_SV_UNNAMED;
   487 	p->name_1 = STR_SV_UNNAMED;
   529 	Player *p;
   528 	Player *p;
   530 
   529 
   531 	// count number of competitors
   530 	// count number of competitors
   532 	n = 0;
   531 	n = 0;
   533 	FOR_ALL_PLAYERS(p) {
   532 	FOR_ALL_PLAYERS(p) {
   534 		if (p->is_active && p->is_ai)
   533 		if (p->is_active && p->is_ai) n++;
   535 			n++;
       
   536 	}
   534 	}
   537 
   535 
   538 	// when there's a lot of computers in game, the probability that a new one starts is lower
   536 	// when there's a lot of computers in game, the probability that a new one starts is lower
   539 	if (n < (uint)_opt.diff.max_no_competitors)
   537 	if (n < (uint)_opt.diff.max_no_competitors &&
   540 		if (n < (_network_server ? InteractiveRandomRange(_opt.diff.max_no_competitors + 2) : RandomRange(_opt.diff.max_no_competitors + 2)) )
   538 			n < (_network_server ?
   541 			/* Send a command to all clients to start  up a new AI. Works fine for Multiplayer and Singleplayer */
   539 				InteractiveRandomRange(_opt.diff.max_no_competitors + 2) :
   542 			DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
   540 				RandomRange(_opt.diff.max_no_competitors + 2)
       
   541 			)) {
       
   542 		/* Send a command to all clients to start up a new AI.
       
   543 		 * Works fine for Multiplayer and Singleplayer */
       
   544 		DoCommandP(0, 1, 0, NULL, CMD_PLAYER_CTRL);
       
   545 	}
   543 
   546 
   544 	// The next AI starts like the difficulty setting said, with +2 month max
   547 	// The next AI starts like the difficulty setting said, with +2 month max
   545 	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
   548 	_next_competitor_start = _opt.diff.competitor_start_time * 90 * DAY_TICKS + 1;
   546 	_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
   549 	_next_competitor_start += _network_server ? InteractiveRandomRange(60 * DAY_TICKS) : RandomRange(60 * DAY_TICKS);
   547 }
   550 }
   548 
   551 
   549 void InitializePlayers(void)
   552 void InitializePlayers(void)
   550 {
   553 {
   551 	int i;
   554 	uint i;
       
   555 
   552 	memset(_players, 0, sizeof(_players));
   556 	memset(_players, 0, sizeof(_players));
   553 	for (i = 0; i != MAX_PLAYERS; i++)
   557 	for (i = 0; i != MAX_PLAYERS; i++) _players[i].index = i;
   554 		_players[i].index=i;
       
   555 	_cur_player_tick_index = 0;
   558 	_cur_player_tick_index = 0;
   556 }
   559 }
   557 
   560 
   558 void OnTick_Players(void)
   561 void OnTick_Players(void)
   559 {
   562 {
   560 	Player *p;
   563 	Player *p;
   561 
   564 
   562 	if (_game_mode == GM_EDITOR)
   565 	if (_game_mode == GM_EDITOR) return;
   563 		return;
       
   564 
   566 
   565 	p = GetPlayer(_cur_player_tick_index);
   567 	p = GetPlayer(_cur_player_tick_index);
   566 	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
   568 	_cur_player_tick_index = (_cur_player_tick_index + 1) % MAX_PLAYERS;
   567 	if (p->name_1 != 0) GenerateCompanyName(p);
   569 	if (p->name_1 != 0) GenerateCompanyName(p);
   568 
   570 
   814 		if (!(flags & DC_EXEC) || pid >= MAX_PLAYERS) return 0;
   816 		if (!(flags & DC_EXEC) || pid >= MAX_PLAYERS) return 0;
   815 
   817 
   816 		p = DoStartupNewPlayer(false);
   818 		p = DoStartupNewPlayer(false);
   817 
   819 
   818 #ifdef ENABLE_NETWORK
   820 #ifdef ENABLE_NETWORK
   819 		if (_networking && !_network_server && _local_player == OWNER_SPECTATOR)
   821 		if (_networking && !_network_server && _local_player == OWNER_SPECTATOR) {
   820 			/* In case we are a client joining a server... */
   822 			/* In case we are a client joining a server... */
   821 			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
   823 			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
       
   824 		}
   822 #endif /* ENABLE_NETWORK */
   825 #endif /* ENABLE_NETWORK */
   823 
   826 
   824 		if (p != NULL) {
   827 		if (p != NULL) {
   825 			if (_local_player == OWNER_SPECTATOR && (!_ai.network_client || _ai.network_playas == OWNER_SPECTATOR)) {
   828 			if (_local_player == OWNER_SPECTATOR &&
       
   829 					(!_ai.network_client || _ai.network_playas == OWNER_SPECTATOR)) {
   826 				/* Check if we do not want to be a spectator in network */
   830 				/* Check if we do not want to be a spectator in network */
   827 				if (!_networking || (_network_server && !_network_dedicated) || _network_playas != OWNER_SPECTATOR || _ai.network_client) {
   831 				if (!_networking ||
       
   832 						(_network_server && !_network_dedicated) ||
       
   833 						_network_playas != OWNER_SPECTATOR ||
       
   834 						_ai.network_client) {
   828 					if (_ai.network_client) {
   835 					if (_ai.network_client) {
   829 						/* As ai-network-client, we have our own rulez (disable GUI and stuff) */
   836 						/* As ai-network-client, we have our own rulez (disable GUI and stuff) */
   830 						_ai.network_playas = p->index;
   837 						_ai.network_playas = p->index;
   831 						_local_player      = OWNER_SPECTATOR;
   838 						_local_player      = OWNER_SPECTATOR;
   832 						if (_ai.network_playas != OWNER_SPECTATOR) {
   839 						if (_ai.network_playas != OWNER_SPECTATOR) {
   980 	HighScore *hs = _highscore_table[_opt.diff_level];
   987 	HighScore *hs = _highscore_table[_opt.diff_level];
   981 	uint i;
   988 	uint i;
   982 	uint16 score = p->old_economy[0].performance_history;
   989 	uint16 score = p->old_economy[0].performance_history;
   983 
   990 
   984 	/* Exclude cheaters from the honour of being in the highscore table */
   991 	/* Exclude cheaters from the honour of being in the highscore table */
   985 	if (CheatHasBeenUsed())
   992 	if (CheatHasBeenUsed()) return -1;
   986 		return -1;
       
   987 
   993 
   988 	for (i = 0; i < lengthof(_highscore_table[0]); i++) {
   994 	for (i = 0; i < lengthof(_highscore_table[0]); i++) {
   989 		/* You are in the TOP5. Move all values one down and save us there */
   995 		/* You are in the TOP5. Move all values one down and save us there */
   990 		if (hs[i].score <= score) {
   996 		if (hs[i].score <= score) {
   991 			// move all elements one down starting from the replaced one
   997 			// move all elements one down starting from the replaced one
  1245 	SlObject(p, _player_desc);
  1251 	SlObject(p, _player_desc);
  1246 
  1252 
  1247 	// Write AI?
  1253 	// Write AI?
  1248 	if (!IS_HUMAN_PLAYER(p->index)) {
  1254 	if (!IS_HUMAN_PLAYER(p->index)) {
  1249 		SlObject(&p->ai, _player_ai_desc);
  1255 		SlObject(&p->ai, _player_ai_desc);
  1250 		for (i = 0; i != p->ai.num_build_rec; i++)
  1256 		for (i = 0; i != p->ai.num_build_rec; i++) {
  1251 			SlObject(&p->ai.src + i, _player_ai_build_rec_desc);
  1257 			SlObject(&p->ai.src + i, _player_ai_build_rec_desc);
       
  1258 		}
  1252 	}
  1259 	}
  1253 
  1260 
  1254 	// Write economy
  1261 	// Write economy
  1255 	SlObject(&p->cur_economy, _player_economy_desc);
  1262 	SlObject(&p->cur_economy, _player_economy_desc);
  1256 
  1263