Changeset - r4854:1c5a86166ae1
[Not reviewed]
master
0 7 0
Darkvater - 18 years ago 2006-10-15 17:01:19
darkvater@openttd.org
(svn r6780) -Codechange: Remove GPMI leftovers (-b impersonisation of AI in MP).
7 files changed with 7 insertions and 67 deletions:
0 comments (0 inline, 0 general)
ai/ai.c
Show inline comments
 
@@ -161,44 +161,36 @@ static void AI_RunTick(PlayerID player)
 
}
 

	
 

	
 
/**
 
 * The gameloop for AIs.
 
 *  Handles one tick for all the AIs.
 
 */
 
void AI_RunGameLoop(void)
 
{
 
	/* Don't do anything if ai is disabled */
 
	if (!_ai.enabled) return;
 

	
 
	/* Don't do anything if we are a network-client
 
	 *  (too bad when a client joins, he thinks the AIs are real, so it wants to control
 
	 *   them.. this avoids that, while loading a network game in singleplayer, does make
 
	 *   the AIs to continue ;))
 
	 */
 
	if (_networking && !_network_server && !_ai.network_client) return;
 
	/* Don't do anything if we are a network-client */
 
	if (_networking && !_network_server) return;
 

	
 
	/* New tick */
 
	_ai.tick++;
 

	
 
	/* Make sure the AI follows the difficulty rule.. */
 
	assert(_opt.diff.competitor_speed <= 4);
 
	if ((_ai.tick & ((1 << (4 - _opt.diff.competitor_speed)) - 1)) != 0) return;
 

	
 
	/* Check for AI-client (so joining a network with an AI) */
 
	if (_ai.network_client && _ai_player[_ai.network_playas].active) {
 
		/* Run the script */
 
		AI_DequeueCommands(_ai.network_playas);
 
		AI_RunTick(_ai.network_playas);
 
	} else if (!_networking || _network_server) {
 
	if (!_networking || _network_server) {
 
		/* Check if we want to run AIs (server or SP only) */
 
		const Player* p;
 

	
 
		FOR_ALL_PLAYERS(p) {
 
			if (p->is_active && p->is_ai) {
 
				/* This should always be true, else something went wrong... */
 
				assert(_ai_player[p->index].active);
 

	
 
				/* Run the script */
 
				AI_DequeueCommands(p->index);
 
				AI_RunTick(p->index);
 
			}
 
@@ -215,47 +207,39 @@ void AI_StartNewAI(PlayerID player)
 
{
 
	assert(IsValidPlayer(player));
 

	
 
	/* Called if a new AI is booted */
 
	_ai_player[player].active = true;
 
}
 

	
 
/**
 
 * This AI player died. Give it some chance to make a final puf.
 
 */
 
void AI_PlayerDied(PlayerID player)
 
{
 
	if (_ai.network_client && _ai.network_playas == player) {
 
		_ai.network_playas = PLAYER_SPECTATOR;
 
	}
 

	
 
	/* Called if this AI died */
 
	_ai_player[player].active = false;
 
}
 

	
 
/**
 
 * Initialize some AI-related stuff.
 
 */
 
void AI_Initialize(void)
 
{
 
	bool ai_network_client = _ai.network_client;
 

	
 
	/* First, make sure all AIs are DEAD! */
 
	AI_Uninitialize();
 

	
 
	memset(&_ai, 0, sizeof(_ai));
 
	memset(&_ai_player, 0, sizeof(_ai_player));
 

	
 
	_ai.network_client = ai_network_client;
 
	_ai.network_playas = PLAYER_SPECTATOR;
 
	_ai.enabled = true;
 
}
 

	
 
/**
 
 * Deinitializer for AI-related stuff.
 
 */
 
void AI_Uninitialize(void)
 
{
 
	const Player* p;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (p->is_active && p->is_ai) AI_PlayerDied(p->index);
ai/ai.h
Show inline comments
 
@@ -23,28 +23,24 @@ typedef struct AICommand {
 
/* The struct for an AIScript Player */
 
typedef struct AIPlayer {
 
	bool active;            ///< Is this AI active?
 
	AICommand *queue;       ///< The commands that he has in his queue
 
	AICommand *queue_tail;  ///< The tail of this queue
 
} AIPlayer;
 

	
 
/* The struct to keep some data about the AI in general */
 
typedef struct AIStruct {
 
	/* General */
 
	bool enabled;           ///< Is AI enabled?
 
	uint tick;              ///< The current tick (something like _frame_counter, only for AIs)
 

	
 
	/* For network-clients (a OpenTTD client who acts as an AI connected to a server) */
 
	bool network_client;    ///< Are we a network_client?
 
	uint8 network_playas;   ///< The current network player we are connected as
 
} AIStruct;
 

	
 
VARDEF AIStruct _ai;
 
VARDEF AIPlayer _ai_player[MAX_PLAYERS];
 

	
 
// ai.c
 
void AI_StartNewAI(PlayerID player);
 
void AI_PlayerDied(PlayerID player);
 
void AI_RunGameLoop(void);
 
void AI_Initialize(void);
 
void AI_Uninitialize(void);
 
int32 AI_DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 flags, uint procc);
ai/trolly/trolly.c
Show inline comments
 
@@ -39,27 +39,25 @@
 
#include "../ai.h"
 

	
 
// This function is called after StartUp. It is the init of an AI
 
static void AiNew_State_FirstTime(Player *p)
 
{
 
	// This assert is used to protect those function from misuse
 
	//   You have quickly a small mistake in the state-array
 
	//   With that, everything would go wrong. Finding that, is almost impossible
 
	//   With this assert, that problem can never happen.
 
	assert(p->ainew.state == AI_STATE_FIRST_TIME);
 
	// We first have to init some things
 

	
 
	if (_current_player == 1 || _ai.network_client) {
 
		ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 0);
 
	}
 
	if (_current_player == 1) ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 0);
 

	
 
	// The PathFinder (AyStar)
 
	// TODO: Maybe when an AI goes bankrupt, this is de-init
 
	//  or when coming from a savegame.. should be checked out!
 
	p->ainew.path_info.start_tile_tl = 0;
 
	p->ainew.path_info.start_tile_br = 0;
 
	p->ainew.path_info.end_tile_tl = 0;
 
	p->ainew.path_info.end_tile_br = 0;
 
	p->ainew.pathfinder = new_AyStar_AiPathFinder(12, &p->ainew.path_info);
 

	
 
	p->ainew.idle = 0;
 
	p->ainew.last_vehiclecheck_date = _date;
economy.c
Show inline comments
 
@@ -440,26 +440,24 @@ static void PlayersCheckBankrupt(Player 
 
					// Switch the player to spectator..
 
					_local_player = PLAYER_SPECTATOR;
 
				}
 
#endif /* ENABLE_NETWORK */
 

	
 
				/* Remove the player */
 
				ChangeOwnershipOfPlayerItems(owner, PLAYER_SPECTATOR);
 
				// Register the player as not-active
 
				p->is_active = false;
 

	
 
				if (!IsHumanPlayer(owner) && (!_networking || _network_server) && _ai.enabled)
 
					AI_PlayerDied(owner);
 
				if (IsHumanPlayer(owner) && owner == _local_player && _ai.network_client)
 
					AI_PlayerDied(owner);
 
			}
 
		}
 
	}
 
}
 

	
 
void DrawNewsBankrupcy(Window *w)
 
{
 
	Player *p;
 

	
 
	DrawNewsBorder(w);
 

	
 
	p = GetPlayer(WP(w,news_d).ni->string_id & 15);
network_client.c
Show inline comments
 
@@ -331,31 +331,24 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
	char unique_id[NETWORK_NAME_LENGTH];
 

	
 
	NetworkRecv_string(MY_CLIENT, p, name, sizeof(name));
 
	NetworkRecv_string(MY_CLIENT, p, unique_id, sizeof(unique_id));
 

	
 
	if (MY_CLIENT->quited)
 
		return NETWORK_RECV_STATUS_CONN_LOST;
 

	
 
	/* Do we receive a change of data? Most likely we changed playas */
 
	if (index == _network_own_client_index) {
 
		_network_playas = playas;
 

	
 
		/* Are we a ai-network-client? Are we not joining as a SPECTATOR (playas == 0, means SPECTATOR) */
 
		if (_ai.network_client && playas != 0) {
 
			if (_ai.network_playas == PLAYER_SPECTATOR)
 
				AI_StartNewAI(playas - 1);
 

	
 
			_ai.network_playas = playas - 1;
 
		}
 
	}
 

	
 
	ci = NetworkFindClientInfoFromIndex(index);
 
	if (ci != NULL) {
 
		if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
 
			// Client name changed, display the change
 
			NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, 1, false, ci->client_name, "%s", name);
 
		} else if (playas != ci->client_playas) {
 
			// The player changed from client-player..
 
			// Do not display that for now
 
		}
 

	
 
@@ -531,35 +524,24 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER
 
				_local_player = 0;
 
				NetworkSend_Command(0, 0, 0, CMD_PLAYER_CTRL, NULL);
 
				_local_player = PLAYER_SPECTATOR;
 
			}
 
		} else {
 
			// take control over an existing company
 
			_local_player = _network_playas - 1;
 
			_patches.autorenew = GetPlayer(_local_player)->engine_renew;
 
			_patches.autorenew_months = GetPlayer(_local_player)->engine_renew_months;
 
			_patches.autorenew_money = GetPlayer(_local_player)->engine_renew_money;
 
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
		}
 

	
 
		/* Check if we are an ai-network-client, and if so, disable GUI */
 
		if (_ai.network_client) {
 
			_ai.network_playas = _local_player;
 
			_local_player      = PLAYER_SPECTATOR;
 

	
 
			if (_ai.network_playas != PLAYER_SPECTATOR) {
 
				/* If we didn't join the game as a spectator, activate the AI */
 
				AI_StartNewAI(_ai.network_playas);
 
			}
 
		}
 
	}
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
 
{
 
	_frame_counter_server = NetworkRecv_uint32(MY_CLIENT, p);
 
	_frame_counter_max = NetworkRecv_uint32(MY_CLIENT, p);
 
#ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
 
	// Test if the server supports this option
 
	//  and if we are at the frame the server is
openttd.c
Show inline comments
 
@@ -329,48 +329,47 @@ int ttd_main(int argc, char *argv[])
 
	musicdriver[0] = sounddriver[0] = videodriver[0] = 0;
 

	
 
	_game_mode = GM_MENU;
 
	_switch_mode = SM_MENU;
 
	_switch_mode_errorstr = INVALID_STRING_ID;
 
	_dedicated_forks = false;
 
	_config_file = NULL;
 

	
 
	// The last param of the following function means this:
 
	//   a letter means: it accepts that param (e.g.: -h)
 
	//   a ':' behind it means: it need a param (e.g.: -m<driver>)
 
	//   a '::' behind it means: it can optional have a param (e.g.: -d<debug>)
 
	optformat = "bm:s:v:hDn::eit:d::r:g::G:c:"
 
	optformat = "m:s:v:hDn::eit:d::r:g::G:c:"
 
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
 
		"f"
 
#endif
 
	;
 

	
 
	MyGetOptInit(&mgo, argc-1, argv+1, optformat);
 
	while ((i = MyGetOpt(&mgo)) != -1) {
 
		switch (i) {
 
		case 'm': ttd_strlcpy(musicdriver, mgo.opt, sizeof(musicdriver)); break;
 
		case 's': ttd_strlcpy(sounddriver, mgo.opt, sizeof(sounddriver)); break;
 
		case 'v': ttd_strlcpy(videodriver, mgo.opt, sizeof(videodriver)); break;
 
		case 'D':
 
			strcpy(musicdriver, "null");
 
			strcpy(sounddriver, "null");
 
			strcpy(videodriver, "dedicated");
 
			dedicated = true;
 
			break;
 
		case 'f': _dedicated_forks = true; break;
 
		case 'n':
 
			network = true;
 
			network_conn = mgo.opt; // optional IP parameter, NULL if unset
 
			break;
 
		case 'b': _ai.network_client = true; break;
 
		case 'r': ParseResolution(resolution, mgo.opt); break;
 
		case 't': startyear = atoi(mgo.opt); break;
 
		case 'd': {
 
#if defined(WIN32)
 
				CreateConsole();
 
#endif
 
				if (mgo.opt != NULL) SetDebugString(mgo.opt);
 
			} break;
 
		case 'e': _switch_mode = SM_EDITOR; break;
 
		case 'i': _use_dos_palette = true; break;
 
		case 'g':
 
			if (mgo.opt != NULL) {
 
@@ -380,29 +379,24 @@ int ttd_main(int argc, char *argv[])
 
				_switch_mode = SM_NEWGAME;
 
			}
 
			break;
 
		case 'G': generation_seed = atoi(mgo.opt); break;
 
		case 'c': _config_file = strdup(mgo.opt); break;
 
		case -2:
 
		case 'h':
 
			showhelp();
 
			return 0;
 
		}
 
	}
 

	
 
	if (_ai.network_client && !network) {
 
		_ai.network_client = false;
 
		DEBUG(ai, 0) ("[AI] Can't enable network-AI, because '-n' is not used\n");
 
	}
 

	
 
	DeterminePaths();
 
	CheckExternalFiles();
 

	
 
#if defined(UNIX) && !defined(__MORPHOS__)
 
	// We must fork here, or we'll end up without some resources we need (like sockets)
 
	if (_dedicated_forks)
 
		DedicatedFork();
 
#endif
 

	
 
	LoadFromConfig();
 
	CheckConfig();
 
	LoadFromHighScore();
players.c
Show inline comments
 
@@ -826,42 +826,30 @@ int32 CmdPlayerCtrl(TileIndex tile, uint
 
		if (!(flags & DC_EXEC) || !IsValidPlayer(pid)) return 0;
 

	
 
		p = DoStartupNewPlayer(false);
 

	
 
#ifdef ENABLE_NETWORK
 
		if (_networking && !_network_server && _local_player == PLAYER_SPECTATOR) {
 
			/* In case we are a client joining a server... */
 
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
 
		}
 
#endif /* ENABLE_NETWORK */
 

	
 
		if (p != NULL) {
 
			if (_local_player == PLAYER_SPECTATOR &&
 
					(!_ai.network_client || _ai.network_playas == PLAYER_SPECTATOR)) {
 
			if (_local_player == PLAYER_SPECTATOR) {
 
				/* Check if we do not want to be a spectator in network */
 
				if (!_networking ||
 
						(_network_server && !_network_dedicated) ||
 
						_network_playas != PLAYER_SPECTATOR ||
 
						_ai.network_client) {
 
					if (_ai.network_client) {
 
						/* As ai-network-client, we have our own rulez (disable GUI and stuff) */
 
						_ai.network_playas = p->index;
 
						_local_player      = PLAYER_SPECTATOR;
 
						if (_ai.network_playas != PLAYER_SPECTATOR) {
 
							/* If we didn't join the game as a spectator, activate the AI */
 
							AI_StartNewAI(_ai.network_playas);
 
						}
 
					} else {
 
						_network_playas != PLAYER_SPECTATOR) {
 
						_local_player = p->index;
 
					}
 
					MarkWholeScreenDirty();
 
				}
 
			} else if (p->index == _local_player) {
 
				DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
 
			}
 
#ifdef ENABLE_NETWORK
 
			if (_network_server) {
 
				/* 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) */
 
				NetworkClientInfo *ci = &_network_client_info[pid];
 
				ci->client_playas = p->index + 1;
0 comments (0 inline, 0 general)